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