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