Sort all includes in src/gromacs
[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/fileio/gmxfio.h"
44 #include "gromacs/utility/fatalerror.h"
45 #include "gromacs/utility/futil.h"
46 #include "gromacs/utility/smalloc.h"
47
48 const char *fontnm[efontNR] = {
49     "Times-Roman", "Times-Italic",     "Times-Bold",    "Times-BoldItalic",
50     "Helvetica",  "Helvetica-Oblique", "Helvetica-Bold", "Helvetica-BoldOblique",
51     "Courier",    "Courier-Oblique",  "Courier-Bold",  "Courier-BoldOblique"
52 };
53
54
55 /* Internal psdata structure (abstract datatype)
56  * to maintain the current state of the ps engine.
57  */
58 struct t_int_psdata  {
59     FILE   *fp;
60     int     maxrgb;
61     int     nrgb;
62     t_rgb  *rgb;
63     real    gen_ybox;
64     int     ostack;
65 };
66
67
68 t_psdata ps_open(const char *fn, real x1, real y1, real x2, real y2)
69 {
70     t_psdata ps;
71
72     snew(ps, 1);
73
74     ps->fp = gmx_fio_fopen(fn, "w");
75     fprintf(ps->fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
76     fprintf(ps->fp, "%%%%Creator: GROMACS\n");
77     fprintf(ps->fp, "%%%%Title: %s\n", fn);
78     fprintf(ps->fp, "%%%%BoundingBox: %g %g %g %g\n", x1, y1, x2, y2);
79     fprintf(ps->fp, "%%%%EndComments\n");
80     fprintf(ps->fp, "/m {moveto} bind def\n");
81     fprintf(ps->fp, "/l {lineto} bind def\n");
82     fprintf(ps->fp, "/rm {rmoveto} bind def\n");
83     fprintf(ps->fp, "/r  {rlineto} bind def\n");
84     fprintf(ps->fp, "/f {fill} bind def\n");
85     fprintf(ps->fp, "/s {stroke} bind def\n");
86
87     ps->nrgb     = 0;
88     ps->maxrgb   = 0;
89     ps->rgb      = NULL;
90     ps->gen_ybox = 0;
91     ps->ostack   = 0;
92
93     return ps;
94 }
95
96 void ps_linewidth(t_psdata ps, int lw)
97 {
98     fprintf(ps->fp, "%d setlinewidth\n", lw);
99 }
100
101 static void ps_defcolor(t_psdata ps, real r, real g, real b, char *cname)
102 {
103     fprintf(ps->fp, "/%s {%g %g %g setrgbcolor} bind def\n", cname, r, g, b);
104 }
105
106 static void ps_selcolor(t_psdata ps, char *cname)
107 {
108     fprintf(ps->fp, "%s\n", cname);
109 }
110
111 static int search_col(t_psdata ps, real r, real g, real b)
112 {
113     int  i;
114     char buf[12];
115
116     for (i = 0; (i < ps->nrgb); i++)
117     {
118         if ((ps->rgb[i].r == r) && (ps->rgb[i].g == g) && (ps->rgb[i].b == b))
119         {
120             return i;
121         }
122     }
123
124     if (ps->nrgb >= ps->maxrgb)
125     {
126         ps->maxrgb += 100;
127         srenew(ps->rgb, ps->maxrgb);
128     }
129
130     sprintf(buf, "C%d", ps->nrgb);
131     ps_defcolor(ps, r, g, b, buf);
132     fprintf(ps->fp, "/B%d {%s b} bind def\n", ps->nrgb, buf);
133     ps->rgb[i].r = r;
134     ps->rgb[i].g = g;
135     ps->rgb[i].b = b;
136     ps->nrgb++;
137
138     return (ps->nrgb-1);
139 }
140
141 void ps_color(t_psdata ps, real r, real g, real b)
142 {
143     char buf[12];
144     int  i;
145
146     i = search_col(ps, r, g, b);
147
148     sprintf(buf, "C%d", i);
149     ps_selcolor(ps, buf);
150 }
151
152 void ps_rgb(t_psdata ps, t_rgb *rgb)
153 {
154     ps_color(ps, rgb->r, rgb->g, rgb->b);
155 }
156
157
158 void ps_init_rgb_nbox(t_psdata ps, real xbox, real ybox)
159 {
160     ps->gen_ybox = ybox;
161     fprintf(ps->fp, "/by {def currentpoint "
162             "%g y r %g %g r %g y neg r %g %g r f y add moveto} bind def\n",
163             0.0, xbox, 0.0, 0.0, -xbox, 0.0);
164     /* macro bn is used in ps_rgb_nbox to draw rectangular boxes */
165 }
166
167 void ps_rgb_nbox(t_psdata ps, t_rgb *rgb, real n)
168 {
169     int i;
170
171     if (n > 2)
172     {
173         ps_rgb(ps, rgb);
174         fprintf(ps->fp, "/y %g by\n", n*ps->gen_ybox);
175         /* macro by is defined in ps_init_rgb_nbox */
176     }
177     else
178     {
179         for (i = 0; (i < n); i++)
180         {
181             ps_rgb_box(ps, rgb);
182         }
183     }
184
185 }
186
187 void ps_init_rgb_box(t_psdata ps, real xbox, real ybox)
188 {
189     fprintf(ps->fp, "/b {currentpoint "
190             "%g %g r %g %g r %g %g r %g %g r f %g add moveto} bind def\n",
191             0.0, ybox, xbox, 0.0, 0.0, -ybox, -xbox, 0.0, ybox);
192     /* macro b is used in search_col to define macro B */
193 }
194
195 void ps_rgb_box(t_psdata ps, t_rgb *rgb)
196 {
197     fprintf(ps->fp, "B%d\n", search_col(ps, rgb->r, rgb->g, rgb->b));
198     /* macro B is defined in search_col from macro b */
199 }
200
201 void ps_lineto(t_psdata ps, real x, real y)
202 {
203     fprintf(ps->fp, "%g %g l\n", x, y);
204 }
205
206 void ps_linerel(t_psdata ps, real dx, real dy)
207 {
208     fprintf(ps->fp, "%g %g r\n", dx, dy);
209 }
210
211 void ps_moveto(t_psdata ps, real x, real y)
212 {
213     fprintf(ps->fp, "%g %g m\n", x, y);
214 }
215
216 void ps_moverel(t_psdata ps, real dx, real dy)
217 {
218     fprintf(ps->fp, "%g %g rm\n", dx, dy);
219 }
220
221 void ps_line(t_psdata ps, real x1, real y1, real x2, real y2)
222 {
223     ps_moveto(ps, x1, y1);
224     ps_lineto(ps, x2, y2);
225     fprintf(ps->fp, "s\n");
226 }
227
228 static void do_box(t_psdata ps, real x1, real y1, real x2, real y2)
229 {
230     ps_moveto(ps, x1, y1);
231     ps_linerel(ps, 0, (real)(y2-y1));
232     ps_linerel(ps, (real)(x2-x1), 0);
233     ps_linerel(ps, 0, (real)(y1-y2));
234     ps_linerel(ps, (real)(x1-x2), 0);
235 }
236
237 void ps_box(t_psdata ps, real x1, real y1, real x2, real y2)
238 {
239     do_box(ps, x1, y1, x2, y2);
240     fprintf(ps->fp, "s\n");
241 }
242
243 void ps_fillbox(t_psdata ps, real x1, real y1, real x2, real y2)
244 {
245     do_box(ps, x1, y1, x2, y2);
246     fprintf(ps->fp, "f\n");
247 }
248
249 void ps_arc(t_psdata ps, real x1, real y1, real rad, real a0, real a1)
250 {
251     fprintf(ps->fp, "%g %g %g %g %g arc s\n", x1, y1, rad, a0, a1);
252 }
253
254 void ps_fillarc(t_psdata ps, real x1, real y1, real rad, real a0, real a1)
255 {
256     fprintf(ps->fp, "%g %g %g %g %g arc f\n", x1, y1, rad, a0, a1);
257 }
258
259 void ps_arcslice(t_psdata ps, real xc, real yc,
260                  real rad1, real rad2, real a0, real a1)
261 {
262     fprintf(ps->fp, "newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath s\n",
263             xc, yc, rad1, a0, a1, xc, yc, rad2, a1, a0);
264 }
265
266 void ps_fillarcslice(t_psdata ps, real xc, real yc,
267                      real rad1, real rad2, real a0, real a1)
268 {
269     fprintf(ps->fp, "newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath f\n",
270             xc, yc, rad1, a0, a1, xc, yc, rad2, a1, a0);
271 }
272
273 void ps_circle(t_psdata ps, real x1, real y1, real rad)
274 {
275     ps_arc(ps, x1, y1, rad, 0, 360);
276 }
277
278 void ps_font(t_psdata ps, int font, real size)
279 {
280
281     if ((font < 0) || (font > efontNR))
282     {
283         fprintf(stderr, "Invalid Font: %d, using %s\n", font, fontnm[0]);
284         font = 0;
285     }
286     fprintf(ps->fp, "/%s findfont\n", fontnm[font]);
287     fprintf(ps->fp, "%g scalefont setfont\n", size);
288 }
289
290 void ps_strfont(t_psdata ps, char *font, real size)
291 {
292     fprintf(ps->fp, "/%s findfont\n", font);
293     fprintf(ps->fp, "%g scalefont setfont\n", size);
294 }
295
296 void ps_text(t_psdata ps, real x1, real y1, const char *str)
297 {
298     ps_moveto(ps, x1, y1);
299     fprintf(ps->fp, "(%s) show\n", str);
300 }
301
302 void ps_flip(t_psdata ps, gmx_bool bPlus)
303 {
304     if (bPlus)
305     {
306         fprintf(ps->fp, "612.5 0 translate 90 rotate\n");
307     }
308     else
309     {
310         fprintf(ps->fp, "-90 rotate -612.5 0 translate\n");
311     }
312 }
313
314 void ps_rotate(t_psdata ps, real angle)
315 {
316     fprintf(ps->fp, "%f rotate\n", angle);
317 }
318
319 void ps_ctext(t_psdata ps, real x1, real y1, const char *str, int expos)
320 {
321     if (expos == eXLeft)
322     {
323         ps_text(ps, x1, y1, str);
324         return;
325     }
326     ps_moveto(ps, x1, y1);
327     fprintf(ps->fp, "(%s) stringwidth\n", str);
328     switch (expos)
329     {
330         case eXLeft:
331             fprintf(ps->fp, "exch 0 exch pop exch\n");
332             break;
333         case eXCenter:
334             fprintf(ps->fp, "exch 2 div neg exch\n");
335             break;
336         case eXRight:
337             fprintf(ps->fp, "exch neg exch\n");
338             break;
339         default:
340             gmx_fatal(FARGS, "invalid position index (expos=%d)", expos);
341     }
342     fprintf(ps->fp, "rmoveto (%s) show\n", str);
343 }
344
345 void ps_translate(t_psdata ps, real x, real y)
346 {
347     fprintf(ps->fp, "%g %g translate\n", x, y);
348 }
349
350 void ps_setorigin(t_psdata ps)
351 {
352     fprintf(ps->fp, "currentpoint dup 3 -1 roll dup 4 1 roll exch translate\n");
353     ps->ostack++;
354 }
355
356 void ps_unsetorigin(t_psdata ps)
357 {
358     if (ps->ostack <= 0)
359     {
360         gmx_fatal(FARGS, "No origin on stack!\n");
361     }
362     fprintf(ps->fp, "neg exch neg exch translate\n");
363     ps->ostack--;
364 }
365
366 void ps_close(t_psdata ps)
367 {
368     fprintf(ps->fp, "%%showpage\n");
369     fprintf(ps->fp, "%%%%EOF\n");
370     gmx_fio_fclose(ps->fp);
371     sfree(ps->rgb);
372     sfree(ps);
373 }
374
375 void ps_comment(t_psdata ps, const char *s)
376 {
377     fprintf(ps->fp, "%%%% %s\n", s);
378 }