James should apparently have been removed from this file years ago. Fix that.
[sgt/halibut] / bk_ps.c
1 /*
2 * PostScript backend for Halibut
3 */
4
5 #include <assert.h>
6 #include <stdarg.h>
7 #include "halibut.h"
8 #include "paper.h"
9
10 /* Ideal number of characters per line, for use in PostScript code */
11 #define PS_WIDTH 79
12 /* Absolute maxiumum characters per line, for use in DSC comments */
13 #define PS_MAXWIDTH 255
14
15 static void ps_comment(FILE *fp, char const *leader, word *words);
16 static void ps_token(FILE *fp, int *cc, char const *fmt, ...);
17 static void ps_string_len(FILE *fp, int *cc, char const *str, int len);
18 static void ps_string(FILE *fp, int *cc, char const *str);
19
20 paragraph *ps_config_filename(char *filename)
21 {
22 return cmdline_cfg_simple("ps-filename", filename, NULL);
23 }
24
25 void ps_backend(paragraph *sourceform, keywordlist *keywords,
26 indexdata *idx, void *vdoc) {
27 document *doc = (document *)vdoc;
28 int font_index;
29 font_encoding *fe;
30 page_data *page;
31 int pageno;
32 FILE *fp;
33 char *filename;
34 paragraph *p;
35 outline_element *oe;
36 int noe;
37 int cc; /* Character count on current line */
38
39 IGNORE(keywords);
40 IGNORE(idx);
41
42 filename = dupstr("output.ps");
43 for (p = sourceform; p; p = p->next) {
44 if (p->type == para_Config) {
45 if (!ustricmp(p->keyword, L"ps-filename")) {
46 sfree(filename);
47 filename = dupstr(adv(p->origkeyword));
48 }
49 }
50 }
51
52 fp = fopen(filename, "w");
53 if (!fp) {
54 error(err_cantopenw, filename);
55 return;
56 }
57
58 fprintf(fp, "%%!PS-Adobe-3.0\n");
59 fprintf(fp, "%%%%Creator: Halibut, %s\n", version);
60 fprintf(fp, "%%%%DocumentData: Clean7Bit\n");
61 fprintf(fp, "%%%%LanguageLevel: 1\n");
62 for (pageno = 0, page = doc->pages; page; page = page->next)
63 pageno++;
64 fprintf(fp, "%%%%Pages: %d\n", pageno);
65 for (p = sourceform; p; p = p->next)
66 if (p->type == para_Title)
67 ps_comment(fp, "%%Title: ", p->words);
68 fprintf(fp, "%%%%DocumentNeededResources:\n");
69 for (fe = doc->fonts->head; fe; fe = fe->next)
70 /* XXX This may request the same font multiple times. */
71 if (!fe->font->info->fp)
72 fprintf(fp, "%%%%+ font %s\n", fe->font->info->name);
73 fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 3\n");
74 for (fe = doc->fonts->head; fe; fe = fe->next)
75 /* XXX This may request the same font multiple times. */
76 if (fe->font->info->fp)
77 fprintf(fp, "%%%%+ font %s\n", fe->font->info->name);
78 fprintf(fp, "%%%%EndComments\n");
79
80 fprintf(fp, "%%%%BeginProlog\n");
81 fprintf(fp, "%%%%BeginResource: procset Halibut 0 3\n");
82 /*
83 * Supply a prologue function which allows a reasonably
84 * compressed representation of the text on the pages.
85 *
86 * "t" expects two arguments: a y-coordinate, and then an array.
87 * Elements of the array are processed sequentially as follows:
88 *
89 * - a number is treated as an x-coordinate
90 * - an array is treated as a (font, size) pair
91 * - a string is shown
92 *
93 * "r" takes four arguments, and behaves like "rectfill".
94 */
95 fprintf(fp,
96 "/tdict 4 dict dup begin\n"
97 " /arraytype {aload pop scalefont setfont} bind def\n"
98 " /realtype {1 index moveto} bind def\n"
99 " /integertype /realtype load def\n"
100 " /stringtype {show} bind def\n"
101 "end def\n"
102 "/t { tdict begin {dup type exec} forall end pop } bind def\n"
103 "/r { 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
104 " neg 0 rlineto closepath fill } bind def\n");
105 /*
106 * pdfmark wrappers
107 *
108 * "p" generates a named destination referencing this page.
109 * "x" generates a link to a named destination.
110 * "u" generates a link to a URI.
111 * "o" generates an outline entry.
112 * "m" generates a general pdfmark.
113 *
114 * They all do nothing if pdfmark is undefined.
115 */
116 fprintf(fp,
117 "/pdfmark where { pop\n"
118 " /p { [ /Dest 3 -1 roll /View [ /XYZ null null null ]\n"
119 " /DEST pdfmark } bind def\n"
120 " /x { [ /Dest 3 -1 roll /Rect 5 -1 roll /Border [0 0 0]\n"
121 " /Subtype /Link /ANN pdfmark } bind def\n"
122 " /u { 2 dict dup /Subtype /URI put dup /URI 4 -1 roll put\n"
123 " [ /Action 3 -1 roll /Rect 5 -1 roll /Border [0 0 0]\n"
124 " /Subtype /Link /ANN pdfmark } bind def\n"
125 " /o { [ /Count 3 -1 roll /Dest 5 -1 roll /Title 7 -1 roll\n"
126 " /OUT pdfmark } bind def\n"
127 " /m /pdfmark load def\n"
128 "}\n");
129 fprintf(fp, "{\n"
130 " /p { pop } bind def\n"
131 " /x { pop pop } bind def\n"
132 " /u /x load def\n"
133 " /o { pop pop pop } bind def\n"
134 " /m /cleartomark load def\n"
135 "} ifelse\n");
136
137 fprintf(fp, "%%%%EndResource\n");
138 fprintf(fp, "%%%%EndProlog\n");
139
140 fprintf(fp, "%%%%BeginSetup\n");
141
142 /*
143 * Assign a destination name to each page for pdfmark purposes.
144 */
145 pageno = 0;
146 for (page = doc->pages; page; page = page->next) {
147 char *buf;
148 pageno++;
149 buf = snewn(12, char);
150 sprintf(buf, "/p%d", pageno);
151 page->spare = buf;
152 }
153
154 /*
155 * This is as good a place as any to put version IDs.
156 */
157 for (p = sourceform; p; p = p->next)
158 if (p->type == para_VersionID)
159 ps_comment(fp, "% ", p->words);
160
161 cc = 0;
162 /*
163 * Request the correct page size. We might want to bracket this
164 * with "%%BeginFeature: *PageSize A4" or similar, and "%%EndFeature",
165 * but that would require us to have a way of getting the name of
166 * the page size given its dimensions.
167 */
168 ps_token(fp, &cc, "/setpagedevice where {\n");
169 ps_token(fp, &cc, " pop 2 dict dup /PageSize [%g %g] put setpagedevice\n",
170 doc->paper_width / FUNITS_PER_PT,
171 doc->paper_height / FUNITS_PER_PT);
172 ps_token(fp, &cc, "} if\n");
173
174 ps_token(fp, &cc, "[/PageMode/UseOutlines/DOCVIEW m\n");
175 noe = doc->n_outline_elements;
176 for (oe = doc->outline_elements; noe; oe++, noe--) {
177 char *title;
178 int titlelen, count, i;
179
180 title = pdf_outline_convert(oe->pdata->outline_title, &titlelen);
181 if (oe->level == 0) {
182 ps_token(fp, &cc, "[/Title");
183 ps_string_len(fp, &cc, title, titlelen);
184 ps_token(fp, &cc, "/DOCINFO m\n");
185 }
186
187 count = 0;
188 for (i = 1; i < noe && oe[i].level > oe->level; i++)
189 if (oe[i].level == oe->level + 1)
190 count++;
191 if (oe->level > 0) count = -count;
192
193 ps_string_len(fp, &cc, title, titlelen);
194 sfree(title);
195 ps_token(fp, &cc, "%s %d o\n",
196 (char *)oe->pdata->first->page->spare, count);
197 }
198
199 for (fe = doc->fonts->head; fe; fe = fe->next) {
200 /* XXX This may request the same font multiple times. */
201 if (fe->font->info->fp) {
202 char buf[512];
203 size_t len;
204 fprintf(fp, "%%%%BeginResource: font %s\n", fe->font->info->name);
205 rewind(fe->font->info->fp);
206 do {
207 len = fread(buf, 1, sizeof(buf), fe->font->info->fp);
208 fwrite(buf, 1, len, fp);
209 } while (len == sizeof(buf));
210 fprintf(fp, "%%%%EndResource\n");
211 } else {
212 fprintf(fp, "%%%%IncludeResource: font %s\n",
213 fe->font->info->name);
214 }
215 }
216
217 /*
218 * Re-encode the fonts.
219 */
220 font_index = 0;
221 for (fe = doc->fonts->head; fe; fe = fe->next) {
222 char fname[40];
223 int i;
224
225 sprintf(fname, "f%d", font_index++);
226 fe->name = dupstr(fname);
227
228 ps_token(fp, &cc, "/%s findfont dup length dict begin\n",
229 fe->font->info->name);
230 ps_token(fp, &cc, "{1 index /FID ne {def} {pop pop} ifelse} forall\n");
231 ps_token(fp, &cc, "/Encoding [\n");
232 for (i = 0; i < 256; i++)
233 ps_token(fp, &cc, "/%s",
234 fe->vector[i] ? fe->vector[i] : ".notdef");
235 ps_token(fp, &cc, "] def\n");
236 ps_token(fp, &cc, "currentdict end\n");
237 ps_token(fp, &cc, "/fontname-%s exch definefont /%s exch def\n",
238 fe->name, fe->name);
239 }
240 fprintf(fp, "%%%%EndSetup\n");
241
242 /*
243 * Output the text and graphics.
244 */
245 pageno = 0;
246 for (page = doc->pages; page; page = page->next) {
247 text_fragment *frag, *frag_end;
248 rect *r;
249 xref *xr;
250 font_encoding *fe;
251 int fs;
252
253 pageno++;
254 fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno);
255 cc = 0;
256 ps_token(fp, &cc, "save %s p\n", (char *)page->spare);
257
258 for (xr = page->first_xref; xr; xr = xr->next) {
259 ps_token(fp, &cc, "[%g %g %g %g]",
260 xr->lx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT,
261 xr->rx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
262 if (xr->dest.type == PAGE) {
263 ps_token(fp, &cc, "%s x\n", (char *)xr->dest.page->spare);
264 } else {
265 ps_string(fp, &cc, xr->dest.url);
266 ps_token(fp, &cc, "u\n");
267 }
268 }
269
270 for (r = page->first_rect; r; r = r->next) {
271 ps_token(fp, &cc, "%g %g %g %g r\n",
272 r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT,
273 r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT);
274 }
275
276 frag = page->first_text;
277 fe = NULL;
278 fs = -1;
279 while (frag) {
280 /*
281 * Collect all the adjacent text fragments with the
282 * same y-coordinate.
283 */
284 for (frag_end = frag;
285 frag_end && frag_end->y == frag->y;
286 frag_end = frag_end->next);
287
288 ps_token(fp, &cc, "%g[", frag->y / FUNITS_PER_PT);
289
290 while (frag && frag != frag_end) {
291
292 if (frag->fe != fe || frag->fontsize != fs)
293 ps_token(fp, &cc, "[%s %d]",
294 frag->fe->name, frag->fontsize);
295 fe = frag->fe;
296 fs = frag->fontsize;
297
298 ps_token(fp, &cc, "%g", frag->x/FUNITS_PER_PT);
299 ps_string(fp, &cc, frag->text);
300
301 frag = frag->next;
302 }
303
304 ps_token(fp, &cc, "]t\n");
305 }
306
307 ps_token(fp, &cc, "restore showpage\n");
308 }
309
310 fprintf(fp, "%%%%EOF\n");
311
312 fclose(fp);
313
314 sfree(filename);
315 }
316
317 static void ps_comment(FILE *fp, char const *leader, word *words) {
318 int cc = 0;
319
320 cc += fprintf(fp, "%s", leader);
321
322 for (; words; words = words->next) {
323 char *text;
324 int type;
325
326 switch (words->type) {
327 case word_HyperLink:
328 case word_HyperEnd:
329 case word_UpperXref:
330 case word_LowerXref:
331 case word_XrefEnd:
332 case word_IndexRef:
333 continue;
334 }
335
336 type = removeattr(words->type);
337
338 switch (type) {
339 case word_Normal:
340 text = utoa_dup(words->text, CS_ASCII);
341 break;
342 case word_WhiteSpace:
343 text = dupstr(" ");
344 break;
345 case word_Quote:
346 text = dupstr("'");
347 break;
348 }
349
350 if (cc + strlen(text) > PS_MAXWIDTH)
351 text[PS_MAXWIDTH - cc] = 0;
352 cc += fprintf(fp, "%s", text);
353 sfree(text);
354 }
355
356 fprintf(fp, "\n");
357 }
358
359 static void ps_token(FILE *fp, int *cc, char const *fmt, ...) {
360 va_list ap;
361
362 va_start(ap, fmt);
363 if (*cc >= PS_WIDTH - 10) {
364 fprintf(fp, "\n");
365 *cc = 0;
366 }
367 *cc += vfprintf(fp, fmt, ap);
368 /* Assume that \n only occurs at the end of a string */
369 if (fmt[strlen(fmt) - 1] == '\n')
370 *cc = 0;
371 }
372
373 static void ps_string_len(FILE *fp, int *cc, char const *str, int len) {
374 char const *c;
375 int score = 0;
376
377 for (c = str; c < str+len; c++) {
378 if (*c < ' ' || *c > '~')
379 score += 2;
380 else if (*c == '(' || *c == ')' || *c == '\\')
381 score += 0;
382 else
383 score -= 1;
384 }
385 if (score > 0) {
386 ps_token(fp, cc, "<");
387 for (c = str; c < str+len; c++) {
388 ps_token(fp, cc, "%02X", 0xFF & (int)*c);
389 }
390 ps_token(fp, cc, ">");
391 } else {
392 *cc += fprintf(fp, "(");
393 for (c = str; c < str+len; c++) {
394 if (*cc >= PS_WIDTH - 4) {
395 fprintf(fp, "\\\n");
396 *cc = 0;
397 }
398 if (*c < ' ' || *c > '~') {
399 *cc += fprintf(fp, "\\%03o", 0xFF & (int)*c);
400 } else {
401 if (*c == '(' || *c == ')' || *c == '\\') {
402 fputc('\\', fp);
403 (*cc)++;
404 }
405 fputc(*c, fp);
406 (*cc)++;
407 }
408 }
409 *cc += fprintf(fp, ")");
410 }
411 }
412
413 static void ps_string(FILE *fp, int *cc, char const *str) {
414 ps_string_len(fp, cc, str, strlen(str));
415 }