Imported Upstream version 8278
[sgt-puzzles/ydirson.git] / ps.c
blob9f2c17f0e5bb02d075da5ccb0c29a8c06753361c
1 /*
2 * ps.c: PostScript printing functions.
3 */
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <string.h>
8 #include <assert.h>
10 #include "puzzles.h"
12 #define ROOT2 1.414213562
14 struct psdata {
15 FILE *fp;
16 int colour;
17 int ytop;
18 int clipped;
19 float hatchthick, hatchspace;
20 int gamewidth, gameheight;
21 drawing *drawing;
24 static void ps_printf(psdata *ps, char *fmt, ...)
26 va_list ap;
28 va_start(ap, fmt);
29 vfprintf(ps->fp, fmt, ap);
30 va_end(ap);
33 static void ps_fill(psdata *ps, int colour)
35 int hatch;
36 float r, g, b;
38 print_get_colour(ps->drawing, colour, ps->colour, &hatch, &r, &g, &b);
40 if (hatch < 0) {
41 if (ps->colour)
42 ps_printf(ps, "%g %g %g setrgbcolor fill\n", r, g, b);
43 else
44 ps_printf(ps, "%g setgray fill\n", r);
45 } else {
46 /* Clip to the region. */
47 ps_printf(ps, "gsave clip\n");
48 /* Hatch the entire game printing area. */
49 ps_printf(ps, "newpath\n");
50 if (hatch == HATCH_VERT || hatch == HATCH_PLUS)
51 ps_printf(ps, "0 %g %d {\n"
52 " 0 moveto 0 %d rlineto\n"
53 "} for\n", ps->hatchspace, ps->gamewidth,
54 ps->gameheight);
55 if (hatch == HATCH_HORIZ || hatch == HATCH_PLUS)
56 ps_printf(ps, "0 %g %d {\n"
57 " 0 exch moveto %d 0 rlineto\n"
58 "} for\n", ps->hatchspace, ps->gameheight,
59 ps->gamewidth);
60 if (hatch == HATCH_SLASH || hatch == HATCH_X)
61 ps_printf(ps, "%d %g %d {\n"
62 " 0 moveto %d dup rlineto\n"
63 "} for\n", -ps->gameheight, ps->hatchspace * ROOT2,
64 ps->gamewidth, max(ps->gamewidth, ps->gameheight));
65 if (hatch == HATCH_BACKSLASH || hatch == HATCH_X)
66 ps_printf(ps, "0 %g %d {\n"
67 " 0 moveto %d neg dup neg rlineto\n"
68 "} for\n", ps->hatchspace * ROOT2,
69 ps->gamewidth+ps->gameheight,
70 max(ps->gamewidth, ps->gameheight));
71 ps_printf(ps, "0 setgray %g setlinewidth stroke grestore\n",
72 ps->hatchthick);
76 static void ps_setcolour_internal(psdata *ps, int colour, char *suffix)
78 int hatch;
79 float r, g, b;
81 print_get_colour(ps->drawing, colour, ps->colour, &hatch, &r, &g, &b);
84 * Stroking in hatched colours is not permitted.
86 assert(hatch < 0);
88 if (ps->colour)
89 ps_printf(ps, "%g %g %g setrgbcolor%s\n", r, g, b, suffix);
90 else
91 ps_printf(ps, "%g setgray%s\n", r, suffix);
94 static void ps_setcolour(psdata *ps, int colour)
96 ps_setcolour_internal(ps, colour, "");
99 static void ps_stroke(psdata *ps, int colour)
101 ps_setcolour_internal(ps, colour, " stroke");
104 static void ps_draw_text(void *handle, int x, int y, int fonttype,
105 int fontsize, int align, int colour, char *text)
107 psdata *ps = (psdata *)handle;
109 y = ps->ytop - y;
110 ps_setcolour(ps, colour);
111 ps_printf(ps, "/%s findfont %d scalefont setfont\n",
112 fonttype == FONT_FIXED ? "Courier" : "Helvetica",
113 fontsize);
114 if (align & ALIGN_VCENTRE) {
115 ps_printf(ps, "newpath 0 0 moveto (X) true charpath flattenpath"
116 " pathbbox\n"
117 "3 -1 roll add 2 div %d exch sub %d exch moveto pop pop\n",
118 y, x);
119 } else {
120 ps_printf(ps, "%d %d moveto\n", x, y);
122 ps_printf(ps, "(");
123 while (*text) {
124 if (*text == '\\' || *text == '(' || *text == ')')
125 ps_printf(ps, "\\");
126 ps_printf(ps, "%c", *text);
127 text++;
129 ps_printf(ps, ") ");
130 if (align & (ALIGN_HCENTRE | ALIGN_HRIGHT))
131 ps_printf(ps, "dup stringwidth pop %sneg 0 rmoveto show\n",
132 (align & ALIGN_HCENTRE) ? "2 div " : "");
133 else
134 ps_printf(ps, "show\n");
137 static void ps_draw_rect(void *handle, int x, int y, int w, int h, int colour)
139 psdata *ps = (psdata *)handle;
141 y = ps->ytop - y;
143 * Offset by half a pixel for the exactness requirement.
145 ps_printf(ps, "newpath %g %g moveto %d 0 rlineto 0 %d rlineto"
146 " %d 0 rlineto closepath\n", x - 0.5, y + 0.5, w, -h, -w);
147 ps_fill(ps, colour);
150 static void ps_draw_line(void *handle, int x1, int y1, int x2, int y2,
151 int colour)
153 psdata *ps = (psdata *)handle;
155 y1 = ps->ytop - y1;
156 y2 = ps->ytop - y2;
157 ps_printf(ps, "newpath %d %d moveto %d %d lineto\n", x1, y1, x2, y2);
158 ps_stroke(ps, colour);
161 static void ps_draw_polygon(void *handle, int *coords, int npoints,
162 int fillcolour, int outlinecolour)
164 psdata *ps = (psdata *)handle;
166 int i;
168 ps_printf(ps, "newpath %d %d moveto\n", coords[0], ps->ytop - coords[1]);
170 for (i = 1; i < npoints; i++)
171 ps_printf(ps, "%d %d lineto\n", coords[i*2], ps->ytop - coords[i*2+1]);
173 ps_printf(ps, "closepath\n");
175 if (fillcolour >= 0) {
176 ps_printf(ps, "gsave\n");
177 ps_fill(ps, fillcolour);
178 ps_printf(ps, "grestore\n");
180 ps_stroke(ps, outlinecolour);
183 static void ps_draw_circle(void *handle, int cx, int cy, int radius,
184 int fillcolour, int outlinecolour)
186 psdata *ps = (psdata *)handle;
188 cy = ps->ytop - cy;
190 ps_printf(ps, "newpath %d %d %d 0 360 arc closepath\n", cx, cy, radius);
192 if (fillcolour >= 0) {
193 ps_printf(ps, "gsave\n");
194 ps_fill(ps, fillcolour);
195 ps_printf(ps, "grestore\n");
197 ps_stroke(ps, outlinecolour);
200 static void ps_unclip(void *handle)
202 psdata *ps = (psdata *)handle;
204 assert(ps->clipped);
205 ps_printf(ps, "grestore\n");
206 ps->clipped = FALSE;
209 static void ps_clip(void *handle, int x, int y, int w, int h)
211 psdata *ps = (psdata *)handle;
213 if (ps->clipped)
214 ps_unclip(ps);
216 y = ps->ytop - y;
218 * Offset by half a pixel for the exactness requirement.
220 ps_printf(ps, "gsave\n");
221 ps_printf(ps, "newpath %g %g moveto %d 0 rlineto 0 %d rlineto"
222 " %d 0 rlineto closepath\n", x - 0.5, y + 0.5, w, -h, -w);
223 ps_printf(ps, "clip\n");
224 ps->clipped = TRUE;
227 static void ps_line_width(void *handle, float width)
229 psdata *ps = (psdata *)handle;
231 ps_printf(ps, "%g setlinewidth\n", width);
234 static void ps_begin_doc(void *handle, int pages)
236 psdata *ps = (psdata *)handle;
238 fputs("%!PS-Adobe-3.0\n", ps->fp);
239 fputs("%%Creator: Simon Tatham's Portable Puzzle Collection\n", ps->fp);
240 fputs("%%DocumentData: Clean7Bit\n", ps->fp);
241 fputs("%%LanguageLevel: 1\n", ps->fp);
242 fprintf(ps->fp, "%%%%Pages: %d\n", pages);
243 fputs("%%DocumentNeededResources:\n", ps->fp);
244 fputs("%%+ font Helvetica\n", ps->fp);
245 fputs("%%+ font Courier\n", ps->fp);
246 fputs("%%EndComments\n", ps->fp);
247 fputs("%%BeginSetup\n", ps->fp);
248 fputs("%%IncludeResource: font Helvetica\n", ps->fp);
249 fputs("%%IncludeResource: font Courier\n", ps->fp);
250 fputs("%%EndSetup\n", ps->fp);
253 static void ps_begin_page(void *handle, int number)
255 psdata *ps = (psdata *)handle;
257 fprintf(ps->fp, "%%%%Page: %d %d\ngsave save\n%g dup scale\n",
258 number, number, 72.0 / 25.4);
261 static void ps_begin_puzzle(void *handle, float xm, float xc,
262 float ym, float yc, int pw, int ph, float wmm)
264 psdata *ps = (psdata *)handle;
266 fprintf(ps->fp, "gsave\n"
267 "clippath flattenpath pathbbox pop pop translate\n"
268 "clippath flattenpath pathbbox 4 2 roll pop pop\n"
269 "exch %g mul %g add exch dup %g mul %g add sub translate\n"
270 "%g dup scale\n"
271 "0 -%d translate\n", xm, xc, ym, yc, wmm/pw, ph);
272 ps->ytop = ph;
273 ps->clipped = FALSE;
274 ps->gamewidth = pw;
275 ps->gameheight = ph;
276 ps->hatchthick = 0.2 * pw / wmm;
277 ps->hatchspace = 1.0 * pw / wmm;
280 static void ps_end_puzzle(void *handle)
282 psdata *ps = (psdata *)handle;
284 fputs("grestore\n", ps->fp);
287 static void ps_end_page(void *handle, int number)
289 psdata *ps = (psdata *)handle;
291 fputs("restore grestore showpage\n", ps->fp);
294 static void ps_end_doc(void *handle)
296 psdata *ps = (psdata *)handle;
298 fputs("%%EOF\n", ps->fp);
301 static const struct drawing_api ps_drawing = {
302 ps_draw_text,
303 ps_draw_rect,
304 ps_draw_line,
305 ps_draw_polygon,
306 ps_draw_circle,
307 NULL /* draw_update */,
308 ps_clip,
309 ps_unclip,
310 NULL /* start_draw */,
311 NULL /* end_draw */,
312 NULL /* status_bar */,
313 NULL /* blitter_new */,
314 NULL /* blitter_free */,
315 NULL /* blitter_save */,
316 NULL /* blitter_load */,
317 ps_begin_doc,
318 ps_begin_page,
319 ps_begin_puzzle,
320 ps_end_puzzle,
321 ps_end_page,
322 ps_end_doc,
323 ps_line_width,
326 psdata *ps_init(FILE *outfile, int colour)
328 psdata *ps = snew(psdata);
330 ps->fp = outfile;
331 ps->colour = colour;
332 ps->ytop = 0;
333 ps->clipped = FALSE;
334 ps->hatchthick = ps->hatchspace = ps->gamewidth = ps->gameheight = 0;
335 ps->drawing = drawing_new(&ps_drawing, NULL, ps);
337 return ps;
340 void ps_free(psdata *ps)
342 drawing_free(ps->drawing);
343 sfree(ps);
346 drawing *ps_drawing_api(psdata *ps)
348 return ps->drawing;