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