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