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