Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / fileio / writeps.cpp
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,2015,2017,2018 by the GROMACS development team.
7  * Copyright (c) 2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source 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 #include "gmxpre.h"
39
40 #include "writeps.h"
41
42 #include "gromacs/fileio/gmxfio.h"
43 #include "gromacs/utility/fatalerror.h"
44 #include "gromacs/utility/futil.h"
45
46 using namespace gmx;
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 t_psdata ps_open(const char* fn, real x1, real y1, real x2, real y2)
55 {
56     t_psdata ps;
57
58     ps.fp = gmx_fio_fopen(fn, "w");
59     fprintf(ps.fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
60     fprintf(ps.fp, "%%%%Creator: GROMACS\n");
61     fprintf(ps.fp, "%%%%Title: %s\n", fn);
62     fprintf(ps.fp, "%%%%BoundingBox: %g %g %g %g\n", x1, y1, x2, y2);
63     fprintf(ps.fp, "%%%%EndComments\n");
64     fprintf(ps.fp, "/m {moveto} bind def\n");
65     fprintf(ps.fp, "/l {lineto} bind def\n");
66     fprintf(ps.fp, "/rm {rmoveto} bind def\n");
67     fprintf(ps.fp, "/r  {rlineto} bind def\n");
68     fprintf(ps.fp, "/f {fill} bind def\n");
69     fprintf(ps.fp, "/s {stroke} bind def\n");
70
71     return ps;
72 }
73
74 void ps_linewidth(t_psdata* ps, int lw)
75 {
76     fprintf(ps->fp, "%d setlinewidth\n", lw);
77 }
78
79 static void ps_defcolor(t_psdata* ps, real r, real g, real b, char* cname)
80 {
81     fprintf(ps->fp, "/%s {%g %g %g setrgbcolor} bind def\n", cname, r, g, b);
82 }
83
84 static void ps_selcolor(t_psdata* ps, char* cname)
85 {
86     fprintf(ps->fp, "%s\n", cname);
87 }
88
89 static gmx::index search_col(t_psdata* ps, real r, real g, real b)
90 {
91     for (gmx::index i = 0; ssize(ps->rgb); ++i)
92     {
93         if ((ps->rgb[i].r == r) && (ps->rgb[i].g == g) && (ps->rgb[i].b == b))
94         {
95             return i;
96         }
97     }
98
99     char buf[12];
100     int  indexToBackElement = static_cast<int>(ssize(ps->rgb));
101     sprintf(buf, "C%d", indexToBackElement);
102     ps_defcolor(ps, r, g, b, buf);
103     fprintf(ps->fp, "/B%zu {%s b} bind def\n", ps->rgb.size(), buf);
104     ps->rgb.emplace_back(t_rgb{ r, g, b });
105
106     return indexToBackElement;
107 }
108
109 void ps_color(t_psdata* ps, real r, real g, real b)
110 {
111     char buf[12];
112     int  indexToElement = static_cast<int>(search_col(ps, r, g, b));
113
114     sprintf(buf, "C%d", indexToElement);
115     ps_selcolor(ps, buf);
116 }
117
118 void ps_rgb(t_psdata* ps, const t_rgb* rgb)
119 {
120     ps_color(ps, rgb->r, rgb->g, rgb->b);
121 }
122
123
124 void ps_init_rgb_nbox(t_psdata* ps, real xbox, real ybox)
125 {
126     ps->gen_ybox = ybox;
127     fprintf(ps->fp,
128             "/by {def currentpoint "
129             "%g y r %g %g r %g y neg r %g %g r f y add moveto} bind def\n",
130             0.0, xbox, 0.0, 0.0, -xbox, 0.0);
131     /* macro bn is used in ps_rgb_nbox to draw rectangular boxes */
132 }
133
134 void ps_rgb_nbox(t_psdata* ps, t_rgb* rgb, real n)
135 {
136     int i;
137
138     if (n > 2)
139     {
140         ps_rgb(ps, rgb);
141         fprintf(ps->fp, "/y %g by\n", n * ps->gen_ybox);
142         /* macro by is defined in ps_init_rgb_nbox */
143     }
144     else
145     {
146         for (i = 0; (i < n); i++)
147         {
148             ps_rgb_box(ps, rgb);
149         }
150     }
151 }
152
153 void ps_init_rgb_box(t_psdata* ps, real xbox, real ybox)
154 {
155     fprintf(ps->fp,
156             "/b {currentpoint "
157             "%g %g r %g %g r %g %g r %g %g r f %g add moveto} bind def\n",
158             0.0, ybox, xbox, 0.0, 0.0, -ybox, -xbox, 0.0, ybox);
159     /* macro b is used in search_col to define macro B */
160 }
161
162 void ps_rgb_box(t_psdata* ps, t_rgb* rgb)
163 {
164     fprintf(ps->fp, "B%zd\n", search_col(ps, rgb->r, rgb->g, rgb->b));
165     /* macro B is defined in search_col from macro b */
166 }
167
168 void ps_lineto(t_psdata* ps, real x, real y)
169 {
170     fprintf(ps->fp, "%g %g l\n", x, y);
171 }
172
173 void ps_linerel(t_psdata* ps, real dx, real dy)
174 {
175     fprintf(ps->fp, "%g %g r\n", dx, dy);
176 }
177
178 void ps_moveto(t_psdata* ps, real x, real y)
179 {
180     fprintf(ps->fp, "%g %g m\n", x, y);
181 }
182
183 void ps_moverel(t_psdata* ps, real dx, real dy)
184 {
185     fprintf(ps->fp, "%g %g rm\n", dx, dy);
186 }
187
188 void ps_line(t_psdata* ps, real x1, real y1, real x2, real y2)
189 {
190     ps_moveto(ps, x1, y1);
191     ps_lineto(ps, x2, y2);
192     fprintf(ps->fp, "s\n");
193 }
194
195 static void do_box(t_psdata* ps, real x1, real y1, real x2, real y2)
196 {
197     ps_moveto(ps, x1, y1);
198     ps_linerel(ps, 0, static_cast<real>(y2 - y1));
199     ps_linerel(ps, static_cast<real>(x2 - x1), 0);
200     ps_linerel(ps, 0, static_cast<real>(y1 - y2));
201     ps_linerel(ps, static_cast<real>(x1 - x2), 0);
202 }
203
204 void ps_box(t_psdata* ps, real x1, real y1, real x2, real y2)
205 {
206     do_box(ps, x1, y1, x2, y2);
207     fprintf(ps->fp, "s\n");
208 }
209
210 void ps_fillbox(t_psdata* ps, real x1, real y1, real x2, real y2)
211 {
212     do_box(ps, x1, y1, x2, y2);
213     fprintf(ps->fp, "f\n");
214 }
215
216 void ps_arc(t_psdata* ps, real x1, real y1, real rad, real a0, real a1)
217 {
218     fprintf(ps->fp, "%g %g %g %g %g arc s\n", x1, y1, rad, a0, a1);
219 }
220
221 void ps_fillarc(t_psdata* ps, real x1, real y1, real rad, real a0, real a1)
222 {
223     fprintf(ps->fp, "%g %g %g %g %g arc f\n", x1, y1, rad, a0, a1);
224 }
225
226 void ps_arcslice(t_psdata* ps, real xc, real yc, real rad1, real rad2, real a0, real a1)
227 {
228     fprintf(ps->fp, "newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath s\n", xc, yc, rad1,
229             a0, a1, xc, yc, rad2, a1, a0);
230 }
231
232 void ps_fillarcslice(t_psdata* ps, real xc, real yc, real rad1, real rad2, real a0, real a1)
233 {
234     fprintf(ps->fp, "newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath f\n", xc, yc, rad1,
235             a0, a1, xc, yc, rad2, a1, a0);
236 }
237
238 void ps_circle(t_psdata* ps, real x1, real y1, real rad)
239 {
240     ps_arc(ps, x1, y1, rad, 0, 360);
241 }
242
243 void ps_font(t_psdata* ps, int font, real size)
244 {
245
246     if ((font < 0) || (font > efontNR))
247     {
248         fprintf(stderr, "Invalid Font: %d, using %s\n", font, fontnm[0]);
249         font = 0;
250     }
251     fprintf(ps->fp, "/%s findfont\n", fontnm[font]);
252     fprintf(ps->fp, "%g scalefont setfont\n", size);
253 }
254
255 void ps_strfont(t_psdata* ps, char* font, real size)
256 {
257     fprintf(ps->fp, "/%s findfont\n", font);
258     fprintf(ps->fp, "%g scalefont setfont\n", size);
259 }
260
261 void ps_text(t_psdata* ps, real x1, real y1, const std::string& str)
262 {
263     ps_moveto(ps, x1, y1);
264     fprintf(ps->fp, "(%s) show\n", str.c_str());
265 }
266
267 void ps_flip(t_psdata* ps, gmx_bool bPlus)
268 {
269     if (bPlus)
270     {
271         fprintf(ps->fp, "612.5 0 translate 90 rotate\n");
272     }
273     else
274     {
275         fprintf(ps->fp, "-90 rotate -612.5 0 translate\n");
276     }
277 }
278
279 void ps_rotate(t_psdata* ps, real angle)
280 {
281     fprintf(ps->fp, "%f rotate\n", angle);
282 }
283
284 void ps_ctext(t_psdata* ps, real x1, real y1, const std::string& str, int expos)
285 {
286     if (expos == eXLeft)
287     {
288         ps_text(ps, x1, y1, str);
289         return;
290     }
291     ps_moveto(ps, x1, y1);
292     fprintf(ps->fp, "(%s) stringwidth\n", str.c_str());
293     switch (expos)
294     {
295         case eXLeft: fprintf(ps->fp, "exch 0 exch pop exch\n"); break;
296         case eXCenter: fprintf(ps->fp, "exch 2 div neg exch\n"); break;
297         case eXRight: fprintf(ps->fp, "exch neg exch\n"); break;
298         default: gmx_fatal(FARGS, "invalid position index (expos=%d)", expos);
299     }
300     fprintf(ps->fp, "rmoveto (%s) show\n", str.c_str());
301 }
302
303 void ps_translate(t_psdata* ps, real x, real y)
304 {
305     fprintf(ps->fp, "%g %g translate\n", x, y);
306 }
307
308 void ps_setorigin(t_psdata* ps)
309 {
310     fprintf(ps->fp, "currentpoint dup 3 -1 roll dup 4 1 roll exch translate\n");
311     ps->ostack++;
312 }
313
314 void ps_unsetorigin(t_psdata* ps)
315 {
316     if (ps->ostack <= 0)
317     {
318         gmx_fatal(FARGS, "No origin on stack!\n");
319     }
320     fprintf(ps->fp, "neg exch neg exch translate\n");
321     ps->ostack--;
322 }
323
324 void ps_close(t_psdata* ps)
325 {
326     fprintf(ps->fp, "%%showpage\n");
327     fprintf(ps->fp, "%%%%EOF\n");
328     gmx_fio_fclose(ps->fp);
329 }
330
331 void ps_comment(t_psdata* ps, const char* s)
332 {
333     fprintf(ps->fp, "%%%% %s\n", s);
334 }