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