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