1854893ab4452b8aedefbdce4413d8c67404a805
[alexxy/gromacs.git] / src / 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     if ((ps->rgb[i].r == r) && (ps->rgb[i].g == g) && (ps->rgb[i].b == b))
117       return i;
118   
119   if (ps->nrgb >= ps->maxrgb) {
120     ps->maxrgb+=100;
121     srenew(ps->rgb,ps->maxrgb);
122   }
123   
124   sprintf(buf,"C%d",ps->nrgb);
125   ps_defcolor(ps,r,g,b,buf);
126   fprintf(ps->fp,"/B%d {%s b} bind def\n",ps->nrgb,buf);
127   ps->rgb[i].r=r;
128   ps->rgb[i].g=g;
129   ps->rgb[i].b=b;
130   ps->nrgb++;
131   
132   return (ps->nrgb-1);
133 }
134
135 void ps_color(t_psdata ps,real r,real g,real b)
136 {
137   char buf[12];
138   int i;
139   
140   i=search_col(ps,r,g,b);
141   
142   sprintf(buf,"C%d",i);
143   ps_selcolor(ps,buf);
144 }
145
146 void ps_rgb(t_psdata ps,t_rgb *rgb)
147 {
148   ps_color(ps,rgb->r,rgb->g,rgb->b);
149 }
150
151
152 void ps_init_rgb_nbox(t_psdata ps,real xbox, real ybox)
153 {
154   ps->gen_ybox=ybox;
155   fprintf(ps->fp,"/by {def currentpoint "
156           "%g y r %g %g r %g y neg r %g %g r f y add moveto} bind def\n",
157           0.0,xbox,0.0,0.0,-xbox,0.0);
158   /* macro bn is used in ps_rgb_nbox to draw rectangular boxes */
159 }
160
161 void ps_rgb_nbox(t_psdata ps,t_rgb *rgb,real n)
162 {
163   int i;
164   
165   if ( n>2 ) {
166     ps_rgb(ps,rgb);
167     fprintf(ps->fp,"/y %g by\n",n*ps->gen_ybox);
168     /* macro by is defined in ps_init_rgb_nbox */
169   } else
170     for (i=0; (i<n); i++)
171       ps_rgb_box(ps, rgb);
172   
173 }
174
175 void ps_init_rgb_box(t_psdata ps,real xbox, real ybox)
176 {
177   fprintf(ps->fp,"/b {currentpoint "
178           "%g %g r %g %g r %g %g r %g %g r f %g add moveto} bind def\n",
179           0.0,ybox,xbox,0.0,0.0,-ybox,-xbox,0.0,ybox);
180   /* macro b is used in search_col to define macro B */
181 }
182
183 void ps_rgb_box(t_psdata ps,t_rgb *rgb)
184 {
185   fprintf(ps->fp,"B%d\n",search_col(ps,rgb->r,rgb->g,rgb->b));
186   /* macro B is defined in search_col from macro b */
187 }
188
189 void ps_lineto(t_psdata ps,real x,real y)
190 {
191   fprintf(ps->fp,"%g %g l\n",x,y);
192 }
193
194 void ps_linerel(t_psdata ps,real dx,real dy)
195 {
196   fprintf(ps->fp,"%g %g r\n",dx,dy);
197 }
198
199 void ps_moveto(t_psdata ps,real x,real y)
200 {
201   fprintf(ps->fp,"%g %g m\n",x,y);
202 }
203
204 void ps_moverel(t_psdata ps,real dx,real dy)
205 {
206   fprintf(ps->fp,"%g %g rm\n",dx,dy);
207 }
208
209 void ps_line(t_psdata ps,real x1,real y1,real x2,real y2)
210 {
211   ps_moveto(ps,x1,y1);
212   ps_lineto(ps,x2,y2);
213   fprintf(ps->fp,"s\n");
214 }
215
216 static void do_box(t_psdata ps,real x1,real y1,real x2,real y2)
217 {
218   ps_moveto(ps,x1,y1);
219   ps_linerel(ps,0,(real)(y2-y1));
220   ps_linerel(ps,(real)(x2-x1),0);
221   ps_linerel(ps,0,(real)(y1-y2));
222   ps_linerel(ps,(real)(x1-x2),0);
223 }
224
225 void ps_box(t_psdata ps,real x1,real y1,real x2,real y2)
226 {
227   do_box(ps,x1,y1,x2,y2);
228   fprintf(ps->fp,"s\n");
229 }
230
231 void ps_fillbox(t_psdata ps,real x1,real y1,real x2,real y2)
232 {
233   do_box(ps,x1,y1,x2,y2);
234   fprintf(ps->fp,"f\n");
235 }
236
237 void ps_arc(t_psdata ps,real x1,real y1,real rad,real a0,real a1)
238 {
239   fprintf(ps->fp,"%g %g %g %g %g arc s\n",x1,y1,rad,a0,a1);
240 }
241
242 void ps_fillarc(t_psdata ps,real x1,real y1,real rad,real a0,real a1)
243 {
244   fprintf(ps->fp,"%g %g %g %g %g arc f\n",x1,y1,rad,a0,a1);
245 }
246
247 void ps_arcslice(t_psdata ps,real xc,real yc,
248                  real rad1,real rad2,real a0,real a1)
249 {
250   fprintf(ps->fp,"newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath s\n",
251           xc,yc,rad1,a0,a1,xc,yc,rad2,a1,a0);
252 }
253   
254 void ps_fillarcslice(t_psdata ps,real xc,real yc,
255                      real rad1,real rad2,real a0,real a1)
256 {
257   fprintf(ps->fp,"newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath f\n",
258           xc,yc,rad1,a0,a1,xc,yc,rad2,a1,a0);
259 }
260   
261 void ps_circle(t_psdata ps,real x1,real y1,real rad)
262 {
263   ps_arc(ps,x1,y1,rad,0,360);
264 }
265
266 void ps_font(t_psdata ps,int font,real size)
267 {
268   
269   if ((font < 0) || (font > efontNR)) {
270     fprintf(stderr,"Invalid Font: %d, using %s\n",font,fontnm[0]);
271     font=0;
272   }
273   fprintf(ps->fp,"/%s findfont\n",fontnm[font]);
274   fprintf(ps->fp,"%g scalefont setfont\n",size);
275 }
276
277 void ps_strfont(t_psdata ps,char *font,real size)
278 {
279   fprintf(ps->fp,"/%s findfont\n",font);
280   fprintf(ps->fp,"%g scalefont setfont\n",size);
281 }
282
283 void ps_text(t_psdata ps,real x1,real y1,const char *str)
284 {
285   ps_moveto(ps,x1,y1);
286   fprintf(ps->fp,"(%s) show\n",str);
287 }
288
289 void ps_flip(t_psdata ps,gmx_bool bPlus)
290 {
291   if (bPlus) 
292     fprintf(ps->fp,"612.5 0 translate 90 rotate\n");
293   else
294     fprintf(ps->fp,"-90 rotate -612.5 0 translate\n");
295 }
296
297 void ps_rotate(t_psdata ps,real angle)
298 {
299   fprintf(ps->fp,"%f rotate\n",angle);
300 }
301
302 void ps_ctext(t_psdata ps,real x1,real y1,const char *str,int expos)
303 {
304   if (expos == eXLeft) {
305     ps_text(ps,x1,y1,str);
306     return;
307   }
308   ps_moveto(ps,x1,y1);
309   fprintf(ps->fp,"(%s) stringwidth\n",str);
310   switch (expos) {
311   case eXLeft:
312     fprintf(ps->fp,"exch 0 exch pop exch\n");
313     break;
314   case eXCenter:
315     fprintf(ps->fp,"exch 2 div neg exch\n");
316     break;
317   case eXRight:
318     fprintf(ps->fp,"exch neg exch\n");
319     break;
320   default:
321     gmx_fatal(FARGS,"invalid position index (expos=%d)",expos);
322   }
323   fprintf(ps->fp,"rmoveto (%s) show\n",str);
324 }
325
326 void ps_translate(t_psdata ps,real x,real y)
327 {
328   fprintf(ps->fp,"%g %g translate\n",x,y);
329 }
330
331 void ps_setorigin(t_psdata ps)
332 {
333   fprintf(ps->fp,"currentpoint dup 3 -1 roll dup 4 1 roll exch translate\n");
334   ps->ostack++;
335 }
336
337 void ps_unsetorigin(t_psdata ps)
338 {
339   if (ps->ostack <= 0)
340     gmx_fatal(FARGS,"No origin on stack!\n");
341   fprintf(ps->fp,"neg exch neg exch translate\n");
342   ps->ostack--;
343 }
344
345 void ps_close(t_psdata ps)
346 {
347   fprintf(ps->fp,"%%showpage\n");
348   fprintf(ps->fp,"%%%%EOF\n");
349   gmx_fio_fclose(ps->fp);
350   sfree(ps->rgb);
351   sfree(ps);
352 }
353
354 void ps_comment(t_psdata ps,const char *s)
355 {
356   fprintf(ps->fp,"%%%% %s\n",s);
357 }