43341922 |
1 | /* |
2 | * PostScript backend for Halibut |
3 | */ |
4 | |
5 | #include <assert.h> |
2bf6e538 |
6 | #include <stdarg.h> |
43341922 |
7 | #include "halibut.h" |
8 | #include "paper.h" |
9 | |
2bf6e538 |
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 | |
b0f1f943 |
15 | static void ps_comment(FILE *fp, char const *leader, word *words); |
2bf6e538 |
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); |
09358aa7 |
19 | |
43341922 |
20 | paragraph *ps_config_filename(char *filename) |
21 | { |
e4ea58f8 |
22 | return cmdline_cfg_simple("ps-filename", filename, NULL); |
43341922 |
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; |
f8194b21 |
35 | outline_element *oe; |
36 | int noe; |
2bf6e538 |
37 | int cc; /* Character count on current line */ |
43341922 |
38 | |
39 | IGNORE(keywords); |
40 | IGNORE(idx); |
41 | |
42 | filename = dupstr("output.ps"); |
43 | for (p = sourceform; p; p = p->next) { |
7738a2fb |
44 | if (p->type == para_Config) { |
43341922 |
45 | if (!ustricmp(p->keyword, L"ps-filename")) { |
46 | sfree(filename); |
e4ea58f8 |
47 | filename = dupstr(adv(p->origkeyword)); |
43341922 |
48 | } |
49 | } |
50 | } |
51 | |
52 | fp = fopen(filename, "w"); |
53 | if (!fp) { |
54 | error(err_cantopenw, filename); |
55 | return; |
56 | } |
57 | |
b0f1f943 |
58 | fprintf(fp, "%%!PS-Adobe-3.0\n"); |
59 | fprintf(fp, "%%%%Creator: Halibut, %s\n", version); |
d4319ca3 |
60 | fprintf(fp, "%%%%DocumentData: Clean7Bit\n"); |
b0f1f943 |
61 | fprintf(fp, "%%%%LanguageLevel: 1\n"); |
43341922 |
62 | for (pageno = 0, page = doc->pages; page; page = page->next) |
63 | pageno++; |
64 | fprintf(fp, "%%%%Pages: %d\n", pageno); |
b0f1f943 |
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. */ |
3e2dd889 |
71 | if (!fe->font->info->fontfile) |
44407fea |
72 | fprintf(fp, "%%%%+ font %s\n", fe->font->info->name); |
7d7f8de9 |
73 | fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 3\n"); |
44407fea |
74 | for (fe = doc->fonts->head; fe; fe = fe->next) |
75 | /* XXX This may request the same font multiple times. */ |
3e2dd889 |
76 | if (fe->font->info->fontfile) |
44407fea |
77 | fprintf(fp, "%%%%+ font %s\n", fe->font->info->name); |
43341922 |
78 | fprintf(fp, "%%%%EndComments\n"); |
79 | |
80 | fprintf(fp, "%%%%BeginProlog\n"); |
7d7f8de9 |
81 | fprintf(fp, "%%%%BeginResource: procset Halibut 0 3\n"); |
7c8c4239 |
82 | /* |
83 | * Supply a prologue function which allows a reasonably |
84 | * compressed representation of the text on the pages. |
85 | * |
7b980ef7 |
86 | * "t" expects two arguments: a y-coordinate, and then an array. |
7c8c4239 |
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 |
7b980ef7 |
92 | * |
93 | * "r" takes four arguments, and behaves like "rectfill". |
7c8c4239 |
94 | */ |
95 | fprintf(fp, |
67f13fa7 |
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" |
7b980ef7 |
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"); |
3f167769 |
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. |
7d7f8de9 |
111 | * "o" generates an outline entry. |
112 | * "m" generates a general pdfmark. |
3f167769 |
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" |
766c3188 |
120 | " /x { [ /Dest 3 -1 roll /Rect 5 -1 roll /Border [0 0 0]\n" |
3f167769 |
121 | " /Subtype /Link /ANN pdfmark } bind def\n" |
122 | " /u { 2 dict dup /Subtype /URI put dup /URI 4 -1 roll put\n" |
766c3188 |
123 | " [ /Action 3 -1 roll /Rect 5 -1 roll /Border [0 0 0]\n" |
3f167769 |
124 | " /Subtype /Link /ANN pdfmark } bind def\n" |
7d7f8de9 |
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" |
a80ef2f1 |
130 | " /p { pop } bind def\n" |
131 | " /x { pop pop } bind def\n" |
132 | " /u /x load def\n" |
7d7f8de9 |
133 | " /o { pop pop pop } bind def\n" |
134 | " /m /cleartomark load def\n" |
3f167769 |
135 | "} ifelse\n"); |
7c8c4239 |
136 | |
b0f1f943 |
137 | fprintf(fp, "%%%%EndResource\n"); |
43341922 |
138 | fprintf(fp, "%%%%EndProlog\n"); |
139 | |
140 | fprintf(fp, "%%%%BeginSetup\n"); |
09358aa7 |
141 | |
142 | /* |
f8194b21 |
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 | /* |
09358aa7 |
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) |
b0f1f943 |
159 | ps_comment(fp, "% ", p->words); |
160 | |
2bf6e538 |
161 | cc = 0; |
a10f193f |
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 | */ |
2bf6e538 |
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"); |
a10f193f |
173 | |
7d7f8de9 |
174 | ps_token(fp, &cc, "[/PageMode/UseOutlines/DOCVIEW m\n"); |
f8194b21 |
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); |
66f70c4f |
181 | if (oe->level == 0) { |
7d7f8de9 |
182 | ps_token(fp, &cc, "[/Title"); |
2bf6e538 |
183 | ps_string_len(fp, &cc, title, titlelen); |
7d7f8de9 |
184 | ps_token(fp, &cc, "/DOCINFO m\n"); |
66f70c4f |
185 | } |
f8194b21 |
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 | |
2bf6e538 |
193 | ps_string_len(fp, &cc, title, titlelen); |
f8194b21 |
194 | sfree(title); |
7d7f8de9 |
195 | ps_token(fp, &cc, "%s %d o\n", |
f8194b21 |
196 | (char *)oe->pdata->first->page->spare, count); |
197 | } |
3f167769 |
198 | |
44407fea |
199 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
b0f1f943 |
200 | /* XXX This may request the same font multiple times. */ |
3e2dd889 |
201 | if (fe->font->info->fontfile) { |
44407fea |
202 | fprintf(fp, "%%%%BeginResource: font %s\n", fe->font->info->name); |
3e2dd889 |
203 | pf_writeps(fe->font->info, fp); |
44407fea |
204 | fprintf(fp, "%%%%EndResource\n"); |
205 | } else { |
206 | fprintf(fp, "%%%%IncludeResource: font %s\n", |
207 | fe->font->info->name); |
208 | } |
209 | } |
09358aa7 |
210 | |
43341922 |
211 | /* |
32ca74bb |
212 | * Re-encode the fonts. |
43341922 |
213 | */ |
214 | font_index = 0; |
215 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
216 | char fname[40]; |
217 | int i; |
218 | |
219 | sprintf(fname, "f%d", font_index++); |
220 | fe->name = dupstr(fname); |
221 | |
2bf6e538 |
222 | ps_token(fp, &cc, "/%s findfont dup length dict begin\n", |
ba0fe3ec |
223 | fe->font->info->name); |
2bf6e538 |
224 | ps_token(fp, &cc, "{1 index /FID ne {def} {pop pop} ifelse} forall\n"); |
225 | ps_token(fp, &cc, "/Encoding [\n"); |
43341922 |
226 | for (i = 0; i < 256; i++) |
d5bc1c48 |
227 | ps_token(fp, &cc, "/%s", glyph_extern(fe->vector[i])); |
2bf6e538 |
228 | ps_token(fp, &cc, "] def\n"); |
229 | ps_token(fp, &cc, "currentdict end\n"); |
230 | ps_token(fp, &cc, "/fontname-%s exch definefont /%s exch def\n", |
231 | fe->name, fe->name); |
43341922 |
232 | } |
233 | fprintf(fp, "%%%%EndSetup\n"); |
234 | |
235 | /* |
23765aeb |
236 | * Output the text and graphics. |
43341922 |
237 | */ |
238 | pageno = 0; |
239 | for (page = doc->pages; page; page = page->next) { |
7c8c4239 |
240 | text_fragment *frag, *frag_end; |
23765aeb |
241 | rect *r; |
3f167769 |
242 | xref *xr; |
3e32e06c |
243 | font_encoding *fe; |
244 | int fs; |
43341922 |
245 | |
246 | pageno++; |
247 | fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno); |
2bf6e538 |
248 | cc = 0; |
249 | ps_token(fp, &cc, "save %s p\n", (char *)page->spare); |
3f167769 |
250 | |
251 | for (xr = page->first_xref; xr; xr = xr->next) { |
2bf6e538 |
252 | ps_token(fp, &cc, "[%g %g %g %g]", |
3f167769 |
253 | xr->lx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT, |
254 | xr->rx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT); |
255 | if (xr->dest.type == PAGE) { |
2bf6e538 |
256 | ps_token(fp, &cc, "%s x\n", (char *)xr->dest.page->spare); |
3f167769 |
257 | } else { |
2bf6e538 |
258 | ps_string(fp, &cc, xr->dest.url); |
259 | ps_token(fp, &cc, "u\n"); |
138d7ffb |
260 | } |
261 | } |
138d7ffb |
262 | |
23765aeb |
263 | for (r = page->first_rect; r; r = r->next) { |
2bf6e538 |
264 | ps_token(fp, &cc, "%g %g %g %g r\n", |
17c71b41 |
265 | r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT, |
7b980ef7 |
266 | r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT); |
23765aeb |
267 | } |
268 | |
7c8c4239 |
269 | frag = page->first_text; |
3e32e06c |
270 | fe = NULL; |
271 | fs = -1; |
7c8c4239 |
272 | while (frag) { |
7c8c4239 |
273 | /* |
274 | * Collect all the adjacent text fragments with the |
275 | * same y-coordinate. |
276 | */ |
277 | for (frag_end = frag; |
278 | frag_end && frag_end->y == frag->y; |
279 | frag_end = frag_end->next); |
280 | |
2bf6e538 |
281 | ps_token(fp, &cc, "%g[", frag->y / FUNITS_PER_PT); |
7c8c4239 |
282 | |
7c8c4239 |
283 | while (frag && frag != frag_end) { |
284 | |
285 | if (frag->fe != fe || frag->fontsize != fs) |
2bf6e538 |
286 | ps_token(fp, &cc, "[%s %d]", |
287 | frag->fe->name, frag->fontsize); |
7c8c4239 |
288 | fe = frag->fe; |
289 | fs = frag->fontsize; |
290 | |
2bf6e538 |
291 | ps_token(fp, &cc, "%g", frag->x/FUNITS_PER_PT); |
292 | ps_string(fp, &cc, frag->text); |
43341922 |
293 | |
7c8c4239 |
294 | frag = frag->next; |
43341922 |
295 | } |
296 | |
2bf6e538 |
297 | ps_token(fp, &cc, "]t\n"); |
43341922 |
298 | } |
299 | |
2bf6e538 |
300 | ps_token(fp, &cc, "restore showpage\n"); |
43341922 |
301 | } |
302 | |
303 | fprintf(fp, "%%%%EOF\n"); |
304 | |
305 | fclose(fp); |
306 | |
307 | sfree(filename); |
308 | } |
09358aa7 |
309 | |
2bf6e538 |
310 | static void ps_comment(FILE *fp, char const *leader, word *words) { |
311 | int cc = 0; |
312 | |
313 | cc += fprintf(fp, "%s", leader); |
09358aa7 |
314 | |
315 | for (; words; words = words->next) { |
316 | char *text; |
317 | int type; |
318 | |
319 | switch (words->type) { |
320 | case word_HyperLink: |
321 | case word_HyperEnd: |
322 | case word_UpperXref: |
323 | case word_LowerXref: |
324 | case word_XrefEnd: |
325 | case word_IndexRef: |
326 | continue; |
327 | } |
328 | |
329 | type = removeattr(words->type); |
330 | |
331 | switch (type) { |
332 | case word_Normal: |
e4ea58f8 |
333 | text = utoa_dup(words->text, CS_ASCII); |
09358aa7 |
334 | break; |
335 | case word_WhiteSpace: |
336 | text = dupstr(" "); |
337 | break; |
338 | case word_Quote: |
339 | text = dupstr("'"); |
340 | break; |
341 | } |
342 | |
2bf6e538 |
343 | if (cc + strlen(text) > PS_MAXWIDTH) |
344 | text[PS_MAXWIDTH - cc] = 0; |
345 | cc += fprintf(fp, "%s", text); |
09358aa7 |
346 | sfree(text); |
347 | } |
348 | |
349 | fprintf(fp, "\n"); |
350 | } |
3f167769 |
351 | |
2bf6e538 |
352 | static void ps_token(FILE *fp, int *cc, char const *fmt, ...) { |
353 | va_list ap; |
354 | |
355 | va_start(ap, fmt); |
356 | if (*cc >= PS_WIDTH - 10) { |
357 | fprintf(fp, "\n"); |
358 | *cc = 0; |
359 | } |
360 | *cc += vfprintf(fp, fmt, ap); |
361 | /* Assume that \n only occurs at the end of a string */ |
362 | if (fmt[strlen(fmt) - 1] == '\n') |
363 | *cc = 0; |
364 | } |
365 | |
366 | static void ps_string_len(FILE *fp, int *cc, char const *str, int len) { |
3f167769 |
367 | char const *c; |
f8194b21 |
368 | int score = 0; |
369 | |
370 | for (c = str; c < str+len; c++) { |
371 | if (*c < ' ' || *c > '~') |
372 | score += 2; |
373 | else if (*c == '(' || *c == ')' || *c == '\\') |
374 | score += 0; |
375 | else |
376 | score -= 1; |
377 | } |
378 | if (score > 0) { |
2bf6e538 |
379 | ps_token(fp, cc, "<"); |
f8194b21 |
380 | for (c = str; c < str+len; c++) { |
2bf6e538 |
381 | ps_token(fp, cc, "%02X", 0xFF & (int)*c); |
d4319ca3 |
382 | } |
2bf6e538 |
383 | ps_token(fp, cc, ">"); |
f8194b21 |
384 | } else { |
2bf6e538 |
385 | *cc += fprintf(fp, "("); |
f8194b21 |
386 | for (c = str; c < str+len; c++) { |
2bf6e538 |
387 | if (*cc >= PS_WIDTH - 4) { |
388 | fprintf(fp, "\\\n"); |
389 | *cc = 0; |
390 | } |
f8194b21 |
391 | if (*c < ' ' || *c > '~') { |
2bf6e538 |
392 | *cc += fprintf(fp, "\\%03o", 0xFF & (int)*c); |
f8194b21 |
393 | } else { |
2bf6e538 |
394 | if (*c == '(' || *c == ')' || *c == '\\') { |
f8194b21 |
395 | fputc('\\', fp); |
2bf6e538 |
396 | (*cc)++; |
397 | } |
f8194b21 |
398 | fputc(*c, fp); |
2bf6e538 |
399 | (*cc)++; |
f8194b21 |
400 | } |
401 | } |
2bf6e538 |
402 | *cc += fprintf(fp, ")"); |
3f167769 |
403 | } |
f8194b21 |
404 | } |
405 | |
2bf6e538 |
406 | static void ps_string(FILE *fp, int *cc, char const *str) { |
407 | ps_string_len(fp, cc, str, strlen(str)); |
3f167769 |
408 | } |