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