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