GHC new build system megapatch
[ghc-hetmet.git] / utils / hp2ps / PsFile.c
1 #include "Main.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include "Defines.h"
5 #include "Dimensions.h"
6 #include "Curves.h"
7 #include "HpFile.h"
8 #include "Axes.h"
9 #include "Key.h"
10 #include "Marks.h"
11 #include "Utilities.h"
12
13 /* own stuff */
14 #include "PsFile.h"
15
16 static void Prologue PROTO((void)); /* forward */
17 static void Variables PROTO((void)); /* forward */
18 static void BorderOutlineBox PROTO((void)); /* forward */
19 static void BigTitleOutlineBox PROTO((void)); /* forward */
20 static void TitleOutlineBox PROTO((void)); /* forward */
21 static void BigTitleText PROTO((void)); /* forward */
22 static void TitleText PROTO((void)); /* forward */
23
24 static void DoTitleAndBox()
25 {
26     BorderOutlineBox();
27
28     if (bflag) {
29         BigTitleOutlineBox();
30         BigTitleText();
31     } else {
32         TitleOutlineBox();
33         TitleText();
34     }
35 }
36
37 static void Landscape PROTO((void));                    /* forward */
38 static void Portrait  PROTO((void));                    /* forward */
39
40 void NextPage() {
41     fprintf(psfp, "showpage\n");
42     if (gflag) Portrait(); else Landscape();
43     DoTitleAndBox();
44 }
45
46 void
47 PutPsFile()
48 {
49     Prologue();
50     Variables();
51
52     CurvesInit();
53
54     DoTitleAndBox();
55
56     if (multipageflag) {
57       Key(); // print multi-page key even if there are more than 20 bands 
58       NextPage();
59     }
60
61     Axes();
62
63     if (!multipageflag && (TWENTY != 0)) Key();
64
65     Curves();
66
67     if (!yflag) Marks();
68
69     fprintf(psfp, "showpage\n");
70 }
71
72
73 static void StandardSpecialComments PROTO((void));      /* forward */
74 static void EPSFSpecialComments PROTO((floatish));      /* forward */
75 static void Scaling   PROTO((floatish));                /* forward */
76
77 static void
78 Prologue()
79 {
80     if (eflag) {
81         floatish epsfscale = epsfwidth / (floatish) borderwidth;
82         EPSFSpecialComments(epsfscale);
83         Scaling(epsfscale);
84     } else {
85         StandardSpecialComments();
86         if (gflag) Portrait(); else Landscape();
87     }
88 }
89
90 extern char *jobstring;
91 extern char *datestring;
92
93 static void
94 StandardSpecialComments()
95 {
96     fprintf(psfp, "%%!PS-Adobe-2.0\n");
97     fprintf(psfp, "%%%%Title: %s\n", jobstring);
98     fprintf(psfp, "%%%%Creator: %s (version %s)\n", programname, VERSION);
99     fprintf(psfp, "%%%%CreationDate: %s\n", datestring);
100     fprintf(psfp, "%%%%EndComments\n");
101
102
103 static void
104 EPSFSpecialComments(epsfscale)
105   floatish epsfscale;
106 {
107     fprintf(psfp, "%%!PS-Adobe-2.0\n");
108     fprintf(psfp, "%%%%Title: %s\n", jobstring);
109     fprintf(psfp, "%%%%Creator: %s (version %s)\n", programname, VERSION);
110     fprintf(psfp, "%%%%CreationDate: %s\n", datestring);
111     fprintf(psfp, "%%%%BoundingBox: 0 0 %d %d\n", 
112                 (int) (borderwidth  * epsfscale + 0.5), 
113                 (int) (borderheight * epsfscale + 0.5) );
114     fprintf(psfp, "%%%%EndComments\n");
115
116
117
118
119 static void
120 Landscape()
121 {
122     fprintf(psfp, "-90 rotate\n");
123     fprintf(psfp, "%f %f translate\n", -(borderwidth + (floatish) START_Y), 
124                   (floatish) START_X); 
125 }
126
127 static void
128 Portrait()
129 {
130     fprintf(psfp, "%f %f translate\n", (floatish) START_X, (floatish) START_Y); 
131 }
132
133 static void
134 Scaling(epsfscale)
135   floatish epsfscale;
136 {
137     fprintf(psfp, "%f %f scale\n", epsfscale, epsfscale);
138 }
139
140
141 static void
142 Variables()
143 {
144     fprintf(psfp, "/HE%d /Helvetica findfont %d scalefont def\n",
145                   NORMAL_FONT, NORMAL_FONT);
146
147     fprintf(psfp, "/HE%d /Helvetica findfont %d scalefont def\n", 
148                   LARGE_FONT, LARGE_FONT);
149 }
150
151
152 static void
153 BorderOutlineBox()
154 {
155     fprintf(psfp, "newpath\n");
156     fprintf(psfp, "0 0 moveto\n");
157     fprintf(psfp, "0 %f rlineto\n", borderheight);
158     fprintf(psfp, "%f 0 rlineto\n", borderwidth);
159     fprintf(psfp, "0 %f rlineto\n", -borderheight);
160     fprintf(psfp, "closepath\n");
161     fprintf(psfp, "%f setlinewidth\n", borderthick);
162     fprintf(psfp, "stroke\n");
163 }
164
165 static void
166 BigTitleOutlineBox()
167 {
168     fprintf(psfp, "newpath\n");
169     fprintf(psfp, "%f %f moveto\n", borderspace,
170                   borderheight - titleheight - borderspace);
171     fprintf(psfp, "0 %f rlineto\n", titleheight);
172     fprintf(psfp, "%f 0 rlineto\n", titlewidth);
173     fprintf(psfp, "0 %f rlineto\n", -titleheight);
174     fprintf(psfp, "closepath\n");
175     fprintf(psfp, "%f setlinewidth\n", borderthick);
176     fprintf(psfp, "stroke\n");
177
178     fprintf(psfp, "%f %f moveto\n", borderspace,
179                   borderheight - titleheight / 2 - borderspace);
180     fprintf(psfp, "%f 0 rlineto\n", titlewidth);
181     fprintf(psfp, "stroke\n");
182 }
183
184
185 static void
186 TitleOutlineBox()
187 {
188     fprintf(psfp, "newpath\n");
189     fprintf(psfp, "%f %f moveto\n", borderspace, 
190                   borderheight - titleheight - borderspace);
191     fprintf(psfp, "0 %f rlineto\n", titleheight);
192     fprintf(psfp, "%f 0 rlineto\n", titlewidth);
193     fprintf(psfp, "0 %f rlineto\n", -titleheight);
194     fprintf(psfp, "closepath\n");
195     fprintf(psfp, "%f setlinewidth\n", borderthick);
196     fprintf(psfp, "stroke\n");
197 }
198
199 static void EscapePrint PROTO((char *, int));   /* forward */
200
201 static void
202 BigTitleText()
203 {
204     floatish x, y;
205
206     x = borderspace + titletextspace;
207     y = borderheight - titleheight / 2 - borderspace + titletextspace;
208
209     /* job identifier goes on top at the far left */
210
211     fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT);
212     fprintf(psfp, "%f %f moveto\n", x, y);
213     fputc('(', psfp); 
214     EscapePrint(jobstring, BIG_JOB_STRING_WIDTH);
215     fprintf(psfp, ") show\n");
216
217     y = borderheight - titleheight - borderspace + titletextspace;
218
219     /* area below curve gows at the botton, far left */
220
221     fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT);
222     fprintf(psfp, "%f %f moveto\n", x, y);
223     fputc('(', psfp);
224     CommaPrint(psfp, (intish)areabelow);
225     fprintf(psfp, " %s x %s)\n", valueunitstring, sampleunitstring); 
226     fprintf(psfp, "show\n");
227
228     /* date goes at far right */
229
230     fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT);
231     fprintf(psfp, "(%s)\n", datestring);
232     fprintf(psfp, "dup stringwidth pop\n");
233     fprintf(psfp, "%f\n", (titlewidth + borderspace) - titletextspace); 
234     fprintf(psfp, "exch sub\n");
235     fprintf(psfp, "%f moveto\n", y);
236     fprintf(psfp, "show\n");
237 }
238
239
240 static void
241 TitleText()
242 {
243     floatish x, y;
244  
245     x = borderspace + titletextspace;
246     y = borderheight - titleheight - borderspace + titletextspace;
247  
248     /* job identifier goes at far left */
249  
250     fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT);
251     fprintf(psfp, "%f %f moveto\n", x, y);
252     fputc('(', psfp); 
253     EscapePrint(jobstring, SMALL_JOB_STRING_WIDTH);
254     fprintf(psfp, ") show\n");
255  
256     /* area below curve is centered */
257  
258     fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT);
259     fputc('(', psfp);
260     CommaPrint(psfp, (intish) areabelow);
261     fprintf(psfp, " %s x %s)\n", valueunitstring, sampleunitstring);
262  
263     fprintf(psfp, "dup stringwidth pop\n");
264     fprintf(psfp, "2 div\n");
265     fprintf(psfp, "%f\n", titlewidth / 2);
266     fprintf(psfp, "exch sub\n");
267     fprintf(psfp, "%f moveto\n", y);
268     fprintf(psfp, "show\n");
269  
270     /* date goes at far right */
271  
272     fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT);
273     fprintf(psfp, "(%s)\n", datestring);
274     fprintf(psfp, "dup stringwidth pop\n");
275     fprintf(psfp, "%f\n", (titlewidth + borderspace) - titletextspace);
276     fprintf(psfp, "exch sub\n");
277     fprintf(psfp, "%f moveto\n", y);
278     fprintf(psfp, "show\n");
279 }
280
281 /*
282  *      Print a string s in width w, escaping characters where necessary.
283  */
284
285 static void
286 EscapePrint(s,w)
287   char* s; int w;
288 {
289     for ( ; *s && w > 0; s++, w--) {
290         if (*s == '(') {                /* escape required */
291             fputc('\\', psfp);
292         } else if (*s == ')') {
293             fputc('\\', psfp);
294         }
295
296         fputc(*s, psfp);
297     }
298 }