Remove unnecessary config.h includes
[alexxy/gromacs.git] / src / gromacs / fileio / writeps.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "writeps.h"
40
41 #include <stdio.h>
42
43 #include "gromacs/utility/fatalerror.h"
44 #include "gromacs/utility/smalloc.h"
45
46 #include "gromacs/utility/futil.h"
47 #include "gromacs/fileio/gmxfio.h"
48
49 const char *fontnm[efontNR] = {
50     "Times-Roman", "Times-Italic",     "Times-Bold",    "Times-BoldItalic",
51     "Helvetica",  "Helvetica-Oblique", "Helvetica-Bold", "Helvetica-BoldOblique",
52     "Courier",    "Courier-Oblique",  "Courier-Bold",  "Courier-BoldOblique"
53 };
54
55
56 /* Internal psdata structure (abstract datatype)
57  * to maintain the current state of the ps engine.
58  */
59 struct t_int_psdata  {
60     FILE   *fp;
61     int     maxrgb;
62     int     nrgb;
63     t_rgb  *rgb;
64     real    gen_ybox;
65     int     ostack;
66 };
67
68
69 t_psdata ps_open(const char *fn, real x1, real y1, real x2, real y2)
70 {
71     t_psdata ps;
72
73     snew(ps, 1);
74
75     ps->fp = gmx_fio_fopen(fn, "w");
76     fprintf(ps->fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
77     fprintf(ps->fp, "%%%%Creator: GROMACS\n");
78     fprintf(ps->fp, "%%%%Title: %s\n", fn);
79     fprintf(ps->fp, "%%%%BoundingBox: %g %g %g %g\n", x1, y1, x2, y2);
80     fprintf(ps->fp, "%%%%EndComments\n");
81     fprintf(ps->fp, "/m {moveto} bind def\n");
82     fprintf(ps->fp, "/l {lineto} bind def\n");
83     fprintf(ps->fp, "/rm {rmoveto} bind def\n");
84     fprintf(ps->fp, "/r  {rlineto} bind def\n");
85     fprintf(ps->fp, "/f {fill} bind def\n");
86     fprintf(ps->fp, "/s {stroke} bind def\n");
87
88     ps->nrgb     = 0;
89     ps->maxrgb   = 0;
90     ps->rgb      = NULL;
91     ps->gen_ybox = 0;
92     ps->ostack   = 0;
93
94     return ps;
95 }
96
97 void ps_linewidth(t_psdata ps, int lw)
98 {
99     fprintf(ps->fp, "%d setlinewidth\n", lw);
100 }
101
102 static void ps_defcolor(t_psdata ps, real r, real g, real b, char *cname)
103 {
104     fprintf(ps->fp, "/%s {%g %g %g setrgbcolor} bind def\n", cname, r, g, b);
105 }
106
107 static void ps_selcolor(t_psdata ps, char *cname)
108 {
109     fprintf(ps->fp, "%s\n", cname);
110 }
111
112 static int search_col(t_psdata ps, real r, real g, real b)
113 {
114     int  i;
115     char buf[12];
116
117     for (i = 0; (i < ps->nrgb); i++)
118     {
119         if ((ps->rgb[i].r == r) && (ps->rgb[i].g == g) && (ps->rgb[i].b == b))
120         {
121             return i;
122         }
123     }
124
125     if (ps->nrgb >= ps->maxrgb)
126     {
127         ps->maxrgb += 100;
128         srenew(ps->rgb, ps->maxrgb);
129     }
130
131     sprintf(buf, "C%d", ps->nrgb);
132     ps_defcolor(ps, r, g, b, buf);
133     fprintf(ps->fp, "/B%d {%s b} bind def\n", ps->nrgb, buf);
134     ps->rgb[i].r = r;
135     ps->rgb[i].g = g;
136     ps->rgb[i].b = b;
137     ps->nrgb++;
138
139     return (ps->nrgb-1);
140 }
141
142 void ps_color(t_psdata ps, real r, real g, real b)
143 {
144     char buf[12];
145     int  i;
146
147     i = search_col(ps, r, g, b);
148
149     sprintf(buf, "C%d", i);
150     ps_selcolor(ps, buf);
151 }
152
153 void ps_rgb(t_psdata ps, t_rgb *rgb)
154 {
155     ps_color(ps, rgb->r, rgb->g, rgb->b);
156 }
157
158
159 void ps_init_rgb_nbox(t_psdata ps, real xbox, real ybox)
160 {
161     ps->gen_ybox = ybox;
162     fprintf(ps->fp, "/by {def currentpoint "
163             "%g y r %g %g r %g y neg r %g %g r f y add moveto} bind def\n",
164             0.0, xbox, 0.0, 0.0, -xbox, 0.0);
165     /* macro bn is used in ps_rgb_nbox to draw rectangular boxes */
166 }
167
168 void ps_rgb_nbox(t_psdata ps, t_rgb *rgb, real n)
169 {
170     int i;
171
172     if (n > 2)
173     {
174         ps_rgb(ps, rgb);
175         fprintf(ps->fp, "/y %g by\n", n*ps->gen_ybox);
176         /* macro by is defined in ps_init_rgb_nbox */
177     }
178     else
179     {
180         for (i = 0; (i < n); i++)
181         {
182             ps_rgb_box(ps, rgb);
183         }
184     }
185
186 }
187
188 void ps_init_rgb_box(t_psdata ps, real xbox, real ybox)
189 {
190     fprintf(ps->fp, "/b {currentpoint "
191             "%g %g r %g %g r %g %g r %g %g r f %g add moveto} bind def\n",
192             0.0, ybox, xbox, 0.0, 0.0, -ybox, -xbox, 0.0, ybox);
193     /* macro b is used in search_col to define macro B */
194 }
195
196 void ps_rgb_box(t_psdata ps, t_rgb *rgb)
197 {
198     fprintf(ps->fp, "B%d\n", search_col(ps, rgb->r, rgb->g, rgb->b));
199     /* macro B is defined in search_col from macro b */
200 }
201
202 void ps_lineto(t_psdata ps, real x, real y)
203 {
204     fprintf(ps->fp, "%g %g l\n", x, y);
205 }
206
207 void ps_linerel(t_psdata ps, real dx, real dy)
208 {
209     fprintf(ps->fp, "%g %g r\n", dx, dy);
210 }
211
212 void ps_moveto(t_psdata ps, real x, real y)
213 {
214     fprintf(ps->fp, "%g %g m\n", x, y);
215 }
216
217 void ps_moverel(t_psdata ps, real dx, real dy)
218 {
219     fprintf(ps->fp, "%g %g rm\n", dx, dy);
220 }
221
222 void ps_line(t_psdata ps, real x1, real y1, real x2, real y2)
223 {
224     ps_moveto(ps, x1, y1);
225     ps_lineto(ps, x2, y2);
226     fprintf(ps->fp, "s\n");
227 }
228
229 static void do_box(t_psdata ps, real x1, real y1, real x2, real y2)
230 {
231     ps_moveto(ps, x1, y1);
232     ps_linerel(ps, 0, (real)(y2-y1));
233     ps_linerel(ps, (real)(x2-x1), 0);
234     ps_linerel(ps, 0, (real)(y1-y2));
235     ps_linerel(ps, (real)(x1-x2), 0);
236 }
237
238 void ps_box(t_psdata ps, real x1, real y1, real x2, real y2)
239 {
240     do_box(ps, x1, y1, x2, y2);
241     fprintf(ps->fp, "s\n");
242 }
243
244 void ps_fillbox(t_psdata ps, real x1, real y1, real x2, real y2)
245 {
246     do_box(ps, x1, y1, x2, y2);
247     fprintf(ps->fp, "f\n");
248 }
249
250 void ps_arc(t_psdata ps, real x1, real y1, real rad, real a0, real a1)
251 {
252     fprintf(ps->fp, "%g %g %g %g %g arc s\n", x1, y1, rad, a0, a1);
253 }
254
255 void ps_fillarc(t_psdata ps, real x1, real y1, real rad, real a0, real a1)
256 {
257     fprintf(ps->fp, "%g %g %g %g %g arc f\n", x1, y1, rad, a0, a1);
258 }
259
260 void ps_arcslice(t_psdata ps, real xc, real yc,
261                  real rad1, real rad2, real a0, real a1)
262 {
263     fprintf(ps->fp, "newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath s\n",
264             xc, yc, rad1, a0, a1, xc, yc, rad2, a1, a0);
265 }
266
267 void ps_fillarcslice(t_psdata ps, real xc, real yc,
268                      real rad1, real rad2, real a0, real a1)
269 {
270     fprintf(ps->fp, "newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath f\n",
271             xc, yc, rad1, a0, a1, xc, yc, rad2, a1, a0);
272 }
273
274 void ps_circle(t_psdata ps, real x1, real y1, real rad)
275 {
276     ps_arc(ps, x1, y1, rad, 0, 360);
277 }
278
279 void ps_font(t_psdata ps, int font, real size)
280 {
281
282     if ((font < 0) || (font > efontNR))
283     {
284         fprintf(stderr, "Invalid Font: %d, using %s\n", font, fontnm[0]);
285         font = 0;
286     }
287     fprintf(ps->fp, "/%s findfont\n", fontnm[font]);
288     fprintf(ps->fp, "%g scalefont setfont\n", size);
289 }
290
291 void ps_strfont(t_psdata ps, char *font, real size)
292 {
293     fprintf(ps->fp, "/%s findfont\n", font);
294     fprintf(ps->fp, "%g scalefont setfont\n", size);
295 }
296
297 void ps_text(t_psdata ps, real x1, real y1, const char *str)
298 {
299     ps_moveto(ps, x1, y1);
300     fprintf(ps->fp, "(%s) show\n", str);
301 }
302
303 void ps_flip(t_psdata ps, gmx_bool bPlus)
304 {
305     if (bPlus)
306     {
307         fprintf(ps->fp, "612.5 0 translate 90 rotate\n");
308     }
309     else
310     {
311         fprintf(ps->fp, "-90 rotate -612.5 0 translate\n");
312     }
313 }
314
315 void ps_rotate(t_psdata ps, real angle)
316 {
317     fprintf(ps->fp, "%f rotate\n", angle);
318 }
319
320 void ps_ctext(t_psdata ps, real x1, real y1, const char *str, int expos)
321 {
322     if (expos == eXLeft)
323     {
324         ps_text(ps, x1, y1, str);
325         return;
326     }
327     ps_moveto(ps, x1, y1);
328     fprintf(ps->fp, "(%s) stringwidth\n", str);
329     switch (expos)
330     {
331         case eXLeft:
332             fprintf(ps->fp, "exch 0 exch pop exch\n");
333             break;
334         case eXCenter:
335             fprintf(ps->fp, "exch 2 div neg exch\n");
336             break;
337         case eXRight:
338             fprintf(ps->fp, "exch neg exch\n");
339             break;
340         default:
341             gmx_fatal(FARGS, "invalid position index (expos=%d)", expos);
342     }
343     fprintf(ps->fp, "rmoveto (%s) show\n", str);
344 }
345
346 void ps_translate(t_psdata ps, real x, real y)
347 {
348     fprintf(ps->fp, "%g %g translate\n", x, y);
349 }
350
351 void ps_setorigin(t_psdata ps)
352 {
353     fprintf(ps->fp, "currentpoint dup 3 -1 roll dup 4 1 roll exch translate\n");
354     ps->ostack++;
355 }
356
357 void ps_unsetorigin(t_psdata ps)
358 {
359     if (ps->ostack <= 0)
360     {
361         gmx_fatal(FARGS, "No origin on stack!\n");
362     }
363     fprintf(ps->fp, "neg exch neg exch translate\n");
364     ps->ostack--;
365 }
366
367 void ps_close(t_psdata ps)
368 {
369     fprintf(ps->fp, "%%showpage\n");
370     fprintf(ps->fp, "%%%%EOF\n");
371     gmx_fio_fclose(ps->fp);
372     sfree(ps->rgb);
373     sfree(ps);
374 }
375
376 void ps_comment(t_psdata ps, const char *s)
377 {
378     fprintf(ps->fp, "%%%% %s\n", s);
379 }