43341922 |
1 | /* |
2 | * PDF backend for Halibut |
3 | */ |
4 | |
5 | #include <assert.h> |
6 | #include "halibut.h" |
7 | #include "paper.h" |
7e2417cc |
8 | #include "deflate.h" |
43341922 |
9 | |
7157d7e1 |
10 | #define TREE_BRANCH 8 /* max branching factor in page tree */ |
43341922 |
11 | |
12 | paragraph *pdf_config_filename(char *filename) |
13 | { |
e4ea58f8 |
14 | return cmdline_cfg_simple("pdf-filename", filename, NULL); |
43341922 |
15 | } |
16 | |
43341922 |
17 | struct object_Tag { |
18 | objlist *list; |
19 | object *next; |
20 | int number; |
21 | rdstringc main, stream; |
22 | int size, fileoff; |
23 | char *final; |
24 | }; |
25 | |
26 | struct objlist_Tag { |
27 | int number; |
28 | object *head, *tail; |
29 | }; |
30 | |
7c8c4239 |
31 | static void pdf_string(void (*add)(object *, char const *), |
32 | object *, char const *); |
ce1b04aa |
33 | static void pdf_string_len(void (*add)(object *, char const *), |
34 | object *, char const *, int); |
43341922 |
35 | static void objref(object *o, object *dest); |
3e7284e8 |
36 | static void objdest(object *o, page_data *p); |
43341922 |
37 | |
3600720b |
38 | static int is_std_font(char const *name); |
39 | |
43341922 |
40 | static void make_pages_node(object *node, object *parent, page_data *first, |
2ff8419e |
41 | page_data *last, object *resources, |
42 | object *mediabox); |
f0e51ce1 |
43 | static int make_outline(object *parent, outline_element *start, int n, |
44 | int open); |
09358aa7 |
45 | static int pdf_versionid(FILE *fp, word *words); |
43341922 |
46 | |
47 | void pdf_backend(paragraph *sourceform, keywordlist *keywords, |
48 | indexdata *idx, void *vdoc) { |
49 | document *doc = (document *)vdoc; |
50 | int font_index; |
51 | font_encoding *fe; |
52 | page_data *page; |
43341922 |
53 | FILE *fp; |
54 | char *filename; |
55 | paragraph *p; |
56 | objlist olist; |
993ade7a |
57 | object *o, *info, *cat, *outlines, *pages, *resources, *mediabox; |
43341922 |
58 | int fileoff; |
59 | |
60 | IGNORE(keywords); |
61 | IGNORE(idx); |
62 | |
63 | filename = dupstr("output.pdf"); |
64 | for (p = sourceform; p; p = p->next) { |
7738a2fb |
65 | if (p->type == para_Config) { |
43341922 |
66 | if (!ustricmp(p->keyword, L"pdf-filename")) { |
67 | sfree(filename); |
e4ea58f8 |
68 | filename = dupstr(adv(p->origkeyword)); |
43341922 |
69 | } |
70 | } |
71 | } |
72 | |
73 | olist.head = olist.tail = NULL; |
74 | olist.number = 1; |
75 | |
993ade7a |
76 | { |
77 | char buf[256]; |
78 | |
79 | info = new_object(&olist); |
80 | objtext(info, "<<\n"); |
81 | if (doc->n_outline_elements > 0) { |
82 | char *title; |
83 | int titlelen; |
84 | |
85 | title = |
86 | pdf_outline_convert(doc->outline_elements->pdata->outline_title, |
87 | &titlelen); |
88 | objtext(info, "/Title "); |
89 | pdf_string_len(objtext, info, title, titlelen); |
90 | sfree(title); |
91 | objtext(info, "\n"); |
92 | } |
93 | objtext(info, "/Producer "); |
94 | sprintf(buf, "Halibut, %s", version); |
95 | pdf_string(objtext, info, buf); |
96 | objtext(info, "\n>>\n"); |
97 | } |
98 | |
43341922 |
99 | cat = new_object(&olist); |
a615a778 |
100 | if (doc->n_outline_elements > 0) |
101 | outlines = new_object(&olist); |
102 | else |
103 | outlines = NULL; |
43341922 |
104 | pages = new_object(&olist); |
105 | resources = new_object(&olist); |
106 | |
107 | /* |
43341922 |
108 | * The catalogue just contains references to the outlines and |
b7d31949 |
109 | * pages objects, and the pagelabels dictionary. |
43341922 |
110 | */ |
a615a778 |
111 | objtext(cat, "<<\n/Type /Catalog"); |
112 | if (outlines) { |
113 | objtext(cat, "\n/Outlines "); |
114 | objref(cat, outlines); |
115 | } |
43341922 |
116 | objtext(cat, "\n/Pages "); |
117 | objref(cat, pages); |
b7d31949 |
118 | /* Halibut just numbers pages 1, 2, 3, ... */ |
119 | objtext(cat, "\n/PageLabels<</Nums[0<</S/D>>]>>"); |
a615a778 |
120 | if (outlines) |
121 | objtext(cat, "\n/PageMode /UseOutlines"); |
122 | objtext(cat, "\n>>\n"); |
43341922 |
123 | |
124 | /* |
125 | * Set up the resources dictionary, which mostly means |
126 | * providing all the font objects and names to call them by. |
127 | */ |
128 | font_index = 0; |
c2b15a94 |
129 | objtext(resources, "<<\n/ProcSet [/PDF/Text]\n/Font <<\n"); |
43341922 |
130 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
131 | char fname[40]; |
dde3b989 |
132 | char buf[80]; |
b17a4ef3 |
133 | int i, prev; |
dde3b989 |
134 | object *font, *fontdesc; |
135 | int flags; |
136 | font_info const *fi = fe->font->info; |
43341922 |
137 | |
138 | sprintf(fname, "f%d", font_index++); |
139 | fe->name = dupstr(fname); |
140 | |
141 | font = new_object(&olist); |
142 | |
143 | objtext(resources, "/"); |
144 | objtext(resources, fe->name); |
145 | objtext(resources, " "); |
146 | objref(resources, font); |
147 | objtext(resources, "\n"); |
148 | |
43341922 |
149 | |
dde3b989 |
150 | /* |
151 | * Construct those parts of the font descriptor that don't dependd |
152 | * on the file format. |
153 | */ |
154 | if (!is_std_font(fe->font->info->name)) { |
155 | fontdesc = new_object(&olist); |
43341922 |
156 | |
255b7ff3 |
157 | #define FF_FIXEDPITCH 0x00000001 |
158 | #define FF_SERIF 0x00000002 |
159 | #define FF_SYMBOLIC 0x00000004 |
160 | #define FF_SCRIPT 0x00000008 |
161 | #define FF_NONSYMBOLIC 0x00000020 |
162 | #define FF_ITALIC 0x00000040 |
163 | #define FF_ALLCAP 0x00010000 |
164 | #define FF_SMALLCAP 0x00020000 |
165 | #define FF_FORCEBOLD 0x00040000 |
166 | |
255b7ff3 |
167 | objtext(fontdesc, "<<\n/Type /FontDescriptor\n/Name /"); |
168 | objtext(fontdesc, fi->name); |
169 | flags = 0; |
170 | if (fi->italicangle) flags |= FF_ITALIC; |
171 | flags |= FF_NONSYMBOLIC; |
172 | sprintf(buf, "\n/Flags %d\n", flags); |
173 | objtext(fontdesc, buf); |
174 | sprintf(buf, "/FontBBox [%g %g %g %g]\n", fi->fontbbox[0], |
175 | fi->fontbbox[1], fi->fontbbox[2], fi->fontbbox[3]); |
176 | objtext(fontdesc, buf); |
177 | sprintf(buf, "/ItalicAngle %g\n", fi->italicangle); |
178 | objtext(fontdesc, buf); |
179 | sprintf(buf, "/Ascent %g\n", fi->ascent); |
180 | objtext(fontdesc, buf); |
181 | sprintf(buf, "/Descent %g\n", fi->descent); |
182 | objtext(fontdesc, buf); |
183 | sprintf(buf, "/CapHeight %g\n", fi->capheight); |
184 | objtext(fontdesc, buf); |
185 | sprintf(buf, "/XHeight %g\n", fi->xheight); |
186 | objtext(fontdesc, buf); |
187 | sprintf(buf, "/StemH %g\n", fi->stemh); |
188 | objtext(fontdesc, buf); |
189 | sprintf(buf, "/StemV %g\n", fi->stemv); |
190 | objtext(fontdesc, buf); |
dde3b989 |
191 | } |
192 | |
193 | objtext(font, "<<\n/Type /Font\n/BaseFont /"); |
194 | objtext(font, fe->font->info->name); |
195 | if (fe->font->info->filetype == TRUETYPE) { |
196 | object *cidfont = new_object(&olist); |
197 | object *cmap = new_object(&olist); |
1cd00fea |
198 | unsigned short ranges[256]; |
199 | unsigned startidx, nranges, nchars; |
200 | int start; |
201 | |
dde3b989 |
202 | objtext(font, "/Subtype/Type0\n/Encoding "); |
bb8bfd66 |
203 | objtext(cmap, "<</Type/CMap\n/CMapName/"); |
204 | objtext(cmap, fe->name); |
205 | objtext(cmap, "\n/CIDSystemInfo<</Registry(Adobe)" |
206 | "/Ordering(Identity)/Supplement 0>>\n"); |
207 | objstream(cmap, "%!PS-Adobe-3.0 Resource-CMap\n" |
208 | "%%DocumentNeededResources: procset CIDInit\n" |
209 | "%%IncludeResource: procset CIDInit\n" |
210 | "%%BeginResource: CMap "); |
211 | objstream(cmap, fe->name); |
212 | objstream(cmap, "\n%%Title ("); |
213 | objstream(cmap, fe->name); |
214 | objstream(cmap, " Adobe Identity 0)\n%%Version: 1\n%%EndComments\n"); |
215 | objstream(cmap, "/CIDInit/ProcSet findresource begin\n"); |
216 | objstream(cmap, "12 dict begin begincmap\n"); |
217 | objstream(cmap, "/CIDSystemInfo 3 dict dup begin\n" |
218 | "/Registry(Adobe)def/Ordering(Identity)def" |
219 | "/Supplement 0 def end def\n"); |
220 | objstream(cmap, "/CMapName/"); |
221 | objstream(cmap, fe->name); |
222 | objstream(cmap, " def/CMapType 0 def/WMode 0 def\n"); |
223 | objstream(cmap, "1 begincodespacerange<00><FF>" |
224 | "endcodespacerange\n"); |
1cd00fea |
225 | start = -1; nranges = nchars = 0; |
bb8bfd66 |
226 | for (i = 0; i < 256; i++) { |
1cd00fea |
227 | unsigned idx; |
228 | |
229 | ranges[i] = 0; |
bb8bfd66 |
230 | if (fe->vector[i] == NOGLYPH) |
231 | continue; |
1cd00fea |
232 | idx = sfnt_glyphtoindex(fe->font->info->fontfile, |
233 | fe->vector[i]); |
f4944c7c |
234 | if (start >= 0 && idx - startidx == (unsigned)(i - start)) { |
1cd00fea |
235 | if (ranges[start] == 1) { |
236 | nranges++; nchars--; |
237 | } |
238 | ranges[start] = i - start + 1; |
239 | } else { |
240 | ranges[i] = 1; |
241 | start = i; |
242 | startidx = idx; |
243 | nchars++; |
244 | } |
245 | } |
246 | i = 0; |
247 | while (nranges) { |
248 | unsigned blk = nranges > 100 ? 100 : nranges; |
249 | nranges -= blk; |
250 | sprintf(buf, "%u ", blk); |
bb8bfd66 |
251 | objstream(cmap, buf); |
1cd00fea |
252 | objstream(cmap, "begincidrange\n"); |
253 | while (blk) { |
254 | if (ranges[i] > 1) { |
255 | sprintf(buf, "<%02X>", i); |
256 | objstream(cmap, buf); |
257 | sprintf(buf, "<%02X>", i + ranges[i] - 1); |
258 | objstream(cmap, buf); |
259 | sprintf(buf, "%hu\n", |
260 | sfnt_glyphtoindex(fe->font->info->fontfile, |
261 | fe->vector[i])); |
262 | objstream(cmap, buf); |
263 | blk--; |
264 | } |
265 | i++; |
266 | } |
267 | objstream(cmap, "endcidrange\n"); |
268 | } |
269 | i = 0; |
270 | while (nchars) { |
271 | unsigned blk = nchars > 100 ? 100 : nchars; |
272 | nchars -= blk; |
273 | sprintf(buf, "%u ", blk); |
bb8bfd66 |
274 | objstream(cmap, buf); |
1cd00fea |
275 | objstream(cmap, "begincidchar\n"); |
276 | while (blk) { |
277 | if (ranges[i] == 1) { |
278 | sprintf(buf, "<%02X>", i); |
279 | objstream(cmap, buf); |
280 | sprintf(buf, "%hu\n", |
281 | sfnt_glyphtoindex(fe->font->info->fontfile, |
282 | fe->vector[i])); |
283 | objstream(cmap, buf); |
284 | blk--; |
285 | } |
286 | i++; |
287 | } |
288 | objstream(cmap, "endcidchar\n"); |
bb8bfd66 |
289 | } |
290 | objstream(cmap, "endcmap CMapName currentdict /CMap " |
291 | "defineresource pop end end\n%%EndResource\n%%EOF\n"); |
1cd00fea |
292 | |
dde3b989 |
293 | objref(font, cmap); |
294 | objtext(font, "\n/DescendantFonts["); |
295 | objref(font, cidfont); |
296 | objtext(font, "]\n"); |
297 | objtext(cidfont, "<<\n/Type/Font\n/Subtype/CIDFontType2\n" |
298 | "/BaseFont/"); |
299 | objtext(cidfont, fe->font->info->name); |
300 | objtext(cidfont, "\n/CIDSystemInfo<</Registry(Adobe)" |
301 | "/Ordering(Identity)/Supplement 0>>\n"); |
302 | objtext(cidfont, "/FontDescriptor "); |
303 | objref(cidfont, fontdesc); |
e7d4c88f |
304 | objtext(cidfont, "\n/W[0["); |
f4944c7c |
305 | for (i = 0; i < (int)sfnt_nglyphs(fe->font->info->fontfile); i++) { |
e7d4c88f |
306 | char buf[20]; |
307 | double width; |
308 | width = find_width(fe->font, |
309 | sfnt_indextoglyph(fe->font->info->fontfile, i)); |
310 | sprintf(buf, "%g ", 1000.0 * width / FUNITS_PER_PT); |
311 | objtext(cidfont, buf); |
312 | } |
313 | objtext(cidfont, "]]>>\n"); |
dde3b989 |
314 | } else { |
315 | objtext(font, "/Subtype /Type1\n"); |
316 | objtext(font, "\n/Encoding <<\n/Type /Encoding\n/Differences ["); |
317 | |
318 | for (i = 0; i < 256; i++) { |
319 | char buf[20]; |
320 | if (fe->vector[i] == NOGLYPH) |
321 | continue; |
322 | if (i != prev + 1) { |
323 | sprintf(buf, "\n%d", i); |
324 | objtext(font, buf); |
325 | } |
326 | objtext(font, i % 8 ? "/" : "\n/"); |
327 | objtext(font, glyph_extern(fe->vector[i])); |
328 | prev = i; |
329 | } |
330 | |
331 | objtext(font, "\n]\n>>\n"); |
332 | if (!is_std_font(fe->font->info->name)){ |
333 | object *widths = new_object(&olist); |
334 | int firstchar = -1, lastchar = -1; |
335 | for (i = 0; i < 256; i++) |
336 | if (fe->vector[i] != NOGLYPH) { |
337 | if (firstchar < 0) firstchar = i; |
338 | lastchar = i; |
339 | } |
340 | sprintf(buf, "/FirstChar %d\n/LastChar %d\n/Widths ", |
341 | firstchar, lastchar); |
342 | objtext(font, buf); |
343 | objref(font, widths); |
344 | objtext(font, "\n"); |
345 | objtext(widths, "[\n"); |
346 | for (i = firstchar; i <= lastchar; i++) { |
347 | double width; |
348 | if (fe->vector[i] == NOGLYPH) |
349 | width = 0.0; |
350 | else |
351 | width = find_width(fe->font, fe->vector[i]); |
352 | sprintf(buf, "%g\n", 1000.0 * width / FUNITS_PER_PT); |
353 | objtext(widths, buf); |
354 | } |
355 | objtext(widths, "]\n"); |
356 | objtext(font, "/FontDescriptor "); |
357 | objref(font, fontdesc); |
358 | } |
359 | |
360 | } |
361 | |
362 | if (!is_std_font(fe->font->info->name)) { |
babfe3e2 |
363 | if (fi->fontfile && fi->filetype == TYPE1) { |
44407fea |
364 | object *fontfile = new_object(&olist); |
44407fea |
365 | size_t len; |
c885c2ff |
366 | char *ffbuf; |
367 | |
368 | pf_part1((font_info *)fi, &ffbuf, &len); |
369 | objstream_len(fontfile, ffbuf, len); |
370 | sfree(ffbuf); |
371 | sprintf(buf, "<<\n/Length1 %lu\n", (unsigned long)len); |
372 | objtext(fontfile, buf); |
373 | pf_part2((font_info *)fi, &ffbuf, &len); |
374 | objstream_len(fontfile, ffbuf, len); |
375 | sfree(ffbuf); |
376 | sprintf(buf, "/Length2 %lu\n", (unsigned long)len); |
377 | objtext(fontfile, buf); |
378 | objtext(fontfile, "/Length3 0\n"); |
44407fea |
379 | objtext(fontdesc, "/FontFile "); |
380 | objref(fontdesc, fontfile); |
dde3b989 |
381 | } else if (fi->fontfile && fi->filetype == TRUETYPE) { |
382 | object *fontfile = new_object(&olist); |
383 | size_t len; |
384 | char *ffbuf; |
385 | |
386 | sfnt_data((font_info *)fi, &ffbuf, &len); |
387 | objstream_len(fontfile, ffbuf, len); |
388 | sprintf(buf, "<<\n/Length1 %lu\n", (unsigned long)len); |
389 | objtext(fontfile, buf); |
390 | objtext(fontdesc, "/FontFile2 "); |
391 | objref(fontdesc, fontfile); |
44407fea |
392 | } |
393 | objtext(fontdesc, "\n>>\n"); |
43341922 |
394 | } |
395 | |
255b7ff3 |
396 | objtext(font, "\n>>\n"); |
43341922 |
397 | } |
398 | objtext(resources, ">>\n>>\n"); |
399 | |
2ff8419e |
400 | { |
401 | char buf[255]; |
402 | mediabox = new_object(&olist); |
403 | sprintf(buf, "[0 0 %g %g]\n", |
404 | doc->paper_width / FUNITS_PER_PT, |
405 | doc->paper_height / FUNITS_PER_PT); |
406 | objtext(mediabox, buf); |
407 | } |
408 | |
43341922 |
409 | /* |
410 | * Define the page objects for each page, and get each one |
411 | * ready to have a `Parent' specification added to it. |
412 | */ |
413 | for (page = doc->pages; page; page = page->next) { |
414 | object *opage; |
415 | |
416 | opage = new_object(&olist); |
417 | page->spare = opage; |
418 | objtext(opage, "<<\n/Type /Page\n"); |
419 | } |
420 | |
421 | /* |
422 | * Recursively build the page tree. |
423 | */ |
2ff8419e |
424 | make_pages_node(pages, NULL, doc->pages, NULL, resources, mediabox); |
43341922 |
425 | |
426 | /* |
427 | * Create and render the individual pages. |
428 | */ |
43341922 |
429 | for (page = doc->pages; page; page = page->next) { |
430 | object *opage, *cstr; |
23765aeb |
431 | rect *r; |
7c8c4239 |
432 | text_fragment *frag, *frag_end; |
43341922 |
433 | char buf[256]; |
7c8c4239 |
434 | int x, y, lx, ly; |
43341922 |
435 | |
436 | opage = (object *)page->spare; |
437 | /* |
438 | * At this point the page dictionary is already |
439 | * half-written, with /Type and /Parent already present. We |
440 | * continue from there. |
441 | */ |
442 | |
443 | /* |
444 | * The PDF spec says /Resources is required, but also says |
445 | * that it's inheritable and may be omitted if it's present |
446 | * in a Pages node. In our case it is: it's present in the |
447 | * topmost /Pages node because we carefully put it there. |
2ff8419e |
448 | * So we don't need a /Resources entry here. The same applies |
449 | * to /MediaBox. |
43341922 |
450 | */ |
43341922 |
451 | |
452 | /* |
453 | * Now we're ready to define a content stream containing |
454 | * the actual text on the page. |
455 | */ |
456 | cstr = new_object(&olist); |
457 | objtext(opage, "/Contents "); |
458 | objref(opage, cstr); |
459 | objtext(opage, "\n"); |
460 | |
23765aeb |
461 | /* |
462 | * Render any rectangles on the page. |
463 | */ |
464 | for (r = page->first_rect; r; r = r->next) { |
465 | char buf[512]; |
17c71b41 |
466 | sprintf(buf, "%g %g %g %g re f\n", |
467 | r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT, |
468 | r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT); |
23765aeb |
469 | objstream(cstr, buf); |
470 | } |
471 | |
43341922 |
472 | objstream(cstr, "BT\n"); |
43341922 |
473 | |
7c8c4239 |
474 | /* |
475 | * PDF tracks two separate current positions: the position |
476 | * given in the `line matrix' and the position given in the |
477 | * `text matrix'. We must therefore track both as well. |
478 | * They start off at -1 (unset). |
479 | */ |
480 | lx = ly = -1; |
481 | x = y = -1; |
482 | |
483 | frag = page->first_text; |
484 | while (frag) { |
485 | /* |
486 | * For compactness, I'm going to group text fragments |
487 | * into subsequences that use the same font+size. So |
488 | * first find the end of this subsequence. |
489 | */ |
490 | for (frag_end = frag; |
491 | (frag_end && |
492 | frag_end->fe == frag->fe && |
493 | frag_end->fontsize == frag->fontsize); |
494 | frag_end = frag_end->next); |
495 | |
496 | /* |
497 | * Now select the text fragment, and prepare to display |
498 | * the text. |
499 | */ |
43341922 |
500 | objstream(cstr, "/"); |
501 | objstream(cstr, frag->fe->name); |
7c8c4239 |
502 | sprintf(buf, " %d Tf ", frag->fontsize); |
43341922 |
503 | objstream(cstr, buf); |
504 | |
7c8c4239 |
505 | while (frag && frag != frag_end) { |
506 | /* |
507 | * Place the text position for the first piece of |
508 | * text. |
509 | */ |
510 | if (lx < 0) { |
511 | sprintf(buf, "1 0 0 1 %g %g Tm ", |
17c71b41 |
512 | frag->x/FUNITS_PER_PT, frag->y/FUNITS_PER_PT); |
7c8c4239 |
513 | } else { |
514 | sprintf(buf, "%g %g Td ", |
17c71b41 |
515 | (frag->x - lx)/FUNITS_PER_PT, |
516 | (frag->y - ly)/FUNITS_PER_PT); |
7c8c4239 |
517 | } |
43341922 |
518 | objstream(cstr, buf); |
7c8c4239 |
519 | lx = x = frag->x; |
520 | ly = y = frag->y; |
521 | |
522 | /* |
523 | * See if we're going to use Tj (show a single |
524 | * string) or TJ (show an array of strings with |
525 | * x-spacings between them). We determine this by |
526 | * seeing if there's more than one text fragment in |
527 | * sequence with the same y-coordinate. |
528 | */ |
529 | if (frag->next && frag->next != frag_end && |
530 | frag->next->y == y) { |
531 | /* |
532 | * The TJ strategy. |
533 | */ |
534 | objstream(cstr, "["); |
535 | while (frag && frag != frag_end && frag->y == y) { |
536 | if (frag->x != x) { |
537 | sprintf(buf, "%g", |
538 | (x - frag->x) * 1000.0 / |
17c71b41 |
539 | (FUNITS_PER_PT * frag->fontsize)); |
7c8c4239 |
540 | objstream(cstr, buf); |
541 | } |
542 | pdf_string(objstream, cstr, frag->text); |
543 | x = frag->x + frag->width; |
544 | frag = frag->next; |
545 | } |
546 | objstream(cstr, "]TJ\n"); |
547 | } else |
548 | { |
549 | /* |
550 | * The Tj strategy. |
551 | */ |
552 | pdf_string(objstream, cstr, frag->text); |
553 | objstream(cstr, "Tj\n"); |
554 | frag = frag->next; |
555 | } |
43341922 |
556 | } |
43341922 |
557 | } |
558 | objstream(cstr, "ET"); |
559 | |
138d7ffb |
560 | /* |
561 | * Also, we want an annotation dictionary containing the |
562 | * cross-references from this page. |
563 | */ |
564 | if (page->first_xref) { |
565 | xref *xr; |
566 | objtext(opage, "/Annots [\n"); |
567 | |
568 | for (xr = page->first_xref; xr; xr = xr->next) { |
138d7ffb |
569 | char buf[256]; |
570 | |
6e4d0fd8 |
571 | objtext(opage, "<</Subtype/Link\n/Rect["); |
138d7ffb |
572 | sprintf(buf, "%g %g %g %g", |
17c71b41 |
573 | xr->lx / FUNITS_PER_PT, xr->by / FUNITS_PER_PT, |
574 | xr->rx / FUNITS_PER_PT, xr->ty / FUNITS_PER_PT); |
6e4d0fd8 |
575 | objtext(opage, buf); |
576 | objtext(opage, "]/Border[0 0 0]\n"); |
138d7ffb |
577 | |
578 | if (xr->dest.type == PAGE) { |
3e7284e8 |
579 | objtext(opage, "/Dest"); |
580 | objdest(opage, xr->dest.page); |
138d7ffb |
581 | } else { |
6e4d0fd8 |
582 | objtext(opage, "/A<</S/URI/URI"); |
583 | pdf_string(objtext, opage, xr->dest.url); |
584 | objtext(opage, ">>"); |
138d7ffb |
585 | } |
586 | |
6e4d0fd8 |
587 | objtext(opage, ">>\n"); |
138d7ffb |
588 | } |
589 | |
590 | objtext(opage, "]\n"); |
591 | } |
592 | |
43341922 |
593 | objtext(opage, ">>\n"); |
594 | } |
595 | |
596 | /* |
f0e51ce1 |
597 | * Set up the outlines dictionary. |
598 | */ |
a615a778 |
599 | if (outlines) { |
f0e51ce1 |
600 | int topcount; |
601 | char buf[80]; |
602 | |
603 | objtext(outlines, "<<\n/Type /Outlines\n"); |
604 | topcount = make_outline(outlines, doc->outline_elements, |
605 | doc->n_outline_elements, TRUE); |
606 | sprintf(buf, "/Count %d\n>>\n", topcount); |
607 | objtext(outlines, buf); |
608 | } |
609 | |
610 | /* |
43341922 |
611 | * Assemble the final linear form of every object. |
612 | */ |
613 | for (o = olist.head; o; o = o->next) { |
614 | rdstringc rs = {0, 0, NULL}; |
615 | char text[80]; |
7e2417cc |
616 | deflate_compress_ctx *zcontext; |
617 | void *zbuf; |
618 | int zlen; |
43341922 |
619 | |
620 | sprintf(text, "%d 0 obj\n", o->number); |
621 | rdaddsc(&rs, text); |
622 | |
c885c2ff |
623 | if (o->stream.text) { |
3e2dd889 |
624 | if (!o->main.text) |
625 | rdaddsc(&o->main, "<<\n"); |
626 | #ifdef PDF_NOCOMPRESS |
627 | zlen = o->stream.pos; |
628 | zbuf = snewn(zlen, char); |
629 | memcpy(zbuf, o->stream.text, zlen); |
630 | sprintf(text, "/Length %d\n>>\n", zlen); |
bb8bfd66 |
631 | #else |
7e2417cc |
632 | zcontext = deflate_compress_new(DEFLATE_TYPE_ZLIB); |
633 | deflate_compress_data(zcontext, o->stream.text, o->stream.pos, |
634 | DEFLATE_END_OF_DATA, &zbuf, &zlen); |
635 | deflate_compress_free(zcontext); |
c885c2ff |
636 | sprintf(text, "/Filter/FlateDecode\n/Length %d\n>>\n", zlen); |
3e2dd889 |
637 | #endif |
43341922 |
638 | rdaddsc(&o->main, text); |
639 | } |
640 | |
641 | assert(o->main.text); |
642 | rdaddsc(&rs, o->main.text); |
643 | sfree(o->main.text); |
644 | |
645 | if (rs.text[rs.pos-1] != '\n') |
646 | rdaddc(&rs, '\n'); |
647 | |
648 | if (o->stream.text) { |
43341922 |
649 | rdaddsc(&rs, "stream\n"); |
7e2417cc |
650 | rdaddsn(&rs, zbuf, zlen); |
43341922 |
651 | rdaddsc(&rs, "\nendstream\n"); |
652 | sfree(o->stream.text); |
7e2417cc |
653 | sfree(zbuf); |
43341922 |
654 | } |
655 | |
656 | rdaddsc(&rs, "endobj\n"); |
657 | |
658 | o->final = rs.text; |
659 | o->size = rs.pos; |
660 | } |
661 | |
662 | /* |
663 | * Write out the PDF file. |
664 | */ |
665 | |
0041ffdb |
666 | if (!strcmp(filename, "-")) |
667 | fp = stdout; |
668 | else |
669 | fp = fopen(filename, "wb"); |
43341922 |
670 | if (!fp) { |
671 | error(err_cantopenw, filename); |
672 | return; |
673 | } |
674 | |
675 | /* |
09358aa7 |
676 | * Header. I'm going to put the version IDs in the header as |
7e2417cc |
677 | * well, simply in PDF comments. The PDF Reference also suggests |
678 | * that binary PDF files contain four top-bit-set characters in |
679 | * the second line. |
43341922 |
680 | */ |
7e2417cc |
681 | fileoff = fprintf(fp, "%%PDF-1.3\n%% L\xc3\xba\xc3\xb0""a\n"); |
09358aa7 |
682 | for (p = sourceform; p; p = p->next) |
683 | if (p->type == para_VersionID) |
684 | fileoff += pdf_versionid(fp, p->words); |
43341922 |
685 | |
686 | /* |
687 | * Body |
688 | */ |
689 | for (o = olist.head; o; o = o->next) { |
690 | o->fileoff = fileoff; |
691 | fwrite(o->final, 1, o->size, fp); |
692 | fileoff += o->size; |
693 | } |
694 | |
695 | /* |
696 | * Cross-reference table |
697 | */ |
698 | fprintf(fp, "xref\n"); |
699 | assert(olist.head->number == 1); |
700 | fprintf(fp, "0 %d\n", olist.tail->number + 1); |
701 | fprintf(fp, "0000000000 65535 f \n"); |
702 | for (o = olist.head; o; o = o->next) { |
703 | char entry[40]; |
704 | sprintf(entry, "%010d 00000 n \n", o->fileoff); |
705 | assert(strlen(entry) == 20); |
706 | fputs(entry, fp); |
707 | } |
708 | |
709 | /* |
710 | * Trailer |
711 | */ |
993ade7a |
712 | fprintf(fp, "trailer\n<<\n/Size %d\n/Root %d 0 R\n/Info %d 0 R\n>>\n", |
713 | olist.tail->number + 1, cat->number, info->number); |
43341922 |
714 | fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff); |
715 | |
0041ffdb |
716 | if (fp != stdout) |
717 | fclose(fp); |
43341922 |
718 | |
719 | sfree(filename); |
720 | } |
721 | |
dde3b989 |
722 | object *new_object(objlist *list) |
43341922 |
723 | { |
f1530049 |
724 | object *obj = snew(object); |
43341922 |
725 | |
726 | obj->list = list; |
727 | |
728 | obj->main.text = NULL; |
729 | obj->main.pos = obj->main.size = 0; |
730 | obj->stream.text = NULL; |
731 | obj->stream.pos = obj->stream.size = 0; |
732 | |
733 | obj->number = list->number++; |
734 | |
735 | obj->next = NULL; |
736 | if (list->tail) |
737 | list->tail->next = obj; |
738 | else |
739 | list->head = obj; |
740 | list->tail = obj; |
741 | |
742 | obj->size = 0; |
743 | obj->final = NULL; |
744 | |
745 | return obj; |
746 | } |
747 | |
dde3b989 |
748 | void objtext(object *o, char const *text) |
43341922 |
749 | { |
750 | rdaddsc(&o->main, text); |
751 | } |
752 | |
dde3b989 |
753 | void objstream_len(object *o, char const *text, size_t len) |
c885c2ff |
754 | { |
755 | rdaddsn(&o->stream, text, len); |
756 | } |
757 | |
dde3b989 |
758 | void objstream(object *o, char const *text) |
43341922 |
759 | { |
760 | rdaddsc(&o->stream, text); |
761 | } |
762 | |
763 | static void objref(object *o, object *dest) |
764 | { |
765 | char buf[40]; |
766 | sprintf(buf, "%d 0 R", dest->number); |
767 | rdaddsc(&o->main, buf); |
768 | } |
769 | |
3e7284e8 |
770 | static void objdest(object *o, page_data *p) { |
771 | objtext(o, "["); |
772 | objref(o, (object *)p->spare); |
773 | objtext(o, "/XYZ null null null]"); |
774 | } |
775 | |
3600720b |
776 | static char const * const stdfonts[] = { |
777 | "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic", |
778 | "Helvetica", "Helvetica-Bold", "Helvetica-Oblique","Helvetica-BoldOblique", |
779 | "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique", |
780 | "Symbol", "ZapfDingbats" |
781 | }; |
782 | |
783 | static int is_std_font(char const *name) { |
784 | unsigned i; |
785 | for (i = 0; i < lenof(stdfonts); i++) |
786 | if (strcmp(name, stdfonts[i]) == 0) |
787 | return TRUE; |
788 | return FALSE; |
789 | } |
790 | |
43341922 |
791 | static void make_pages_node(object *node, object *parent, page_data *first, |
2ff8419e |
792 | page_data *last, object *resources, |
793 | object *mediabox) |
43341922 |
794 | { |
795 | int count; |
796 | page_data *page; |
797 | char buf[80]; |
798 | |
799 | objtext(node, "<<\n/Type /Pages\n"); |
800 | if (parent) { |
801 | objtext(node, "/Parent "); |
802 | objref(node, parent); |
803 | objtext(node, "\n"); |
804 | } |
805 | |
806 | /* |
807 | * Count the pages in this stretch, to see if there are few |
808 | * enough to reference directly. |
809 | */ |
810 | count = 0; |
811 | for (page = first; page; page = page->next) { |
812 | count++; |
813 | if (page == last) |
814 | break; |
815 | } |
816 | |
817 | sprintf(buf, "/Count %d\n/Kids [\n", count); |
818 | objtext(node, buf); |
819 | |
820 | if (count > TREE_BRANCH) { |
821 | int i; |
822 | page_data *thisfirst, *thislast; |
823 | |
824 | page = first; |
825 | |
826 | for (i = 0; i < TREE_BRANCH; i++) { |
827 | int number = (i+1) * count / TREE_BRANCH - i * count / TREE_BRANCH; |
828 | thisfirst = page; |
829 | while (number--) { |
830 | thislast = page; |
831 | page = page->next; |
832 | } |
833 | |
834 | if (thisfirst == thislast) { |
835 | objref(node, (object *)thisfirst->spare); |
836 | objtext((object *)thisfirst->spare, "/Parent "); |
837 | objref((object *)thisfirst->spare, node); |
838 | objtext((object *)thisfirst->spare, "\n"); |
839 | } else { |
840 | object *newnode = new_object(node->list); |
2ff8419e |
841 | make_pages_node(newnode, node, thisfirst, thislast, |
842 | NULL, NULL); |
43341922 |
843 | objref(node, newnode); |
844 | } |
845 | objtext(node, "\n"); |
846 | } |
847 | |
848 | assert(thislast == last || page == NULL); |
849 | |
850 | } else { |
851 | for (page = first; page; page = page->next) { |
852 | objref(node, (object *)page->spare); |
853 | objtext(node, "\n"); |
854 | objtext((object *)page->spare, "/Parent "); |
855 | objref((object *)page->spare, node); |
856 | objtext((object *)page->spare, "\n"); |
857 | if (page == last) |
858 | break; |
859 | } |
860 | } |
861 | |
862 | objtext(node, "]\n"); |
863 | |
864 | if (resources) { |
865 | objtext(node, "/Resources "); |
866 | objref(node, resources); |
867 | objtext(node, "\n"); |
868 | } |
2ff8419e |
869 | if (mediabox) { |
870 | objtext(node, "/MediaBox "); |
871 | objref(node, mediabox); |
872 | objtext(node, "\n"); |
873 | } |
43341922 |
874 | |
875 | objtext(node, ">>\n"); |
876 | } |
f0e51ce1 |
877 | |
878 | /* |
879 | * In text on the page, PDF uses the PostScript font model, which |
880 | * means that glyphs are identified by PS strings and hence font |
881 | * encoding can be managed independently of the supplied encoding |
882 | * of the font. However, in the document outline, the PDF spec |
ce1b04aa |
883 | * encodes in either PDFDocEncoding (a custom superset of |
884 | * ISO-8859-1) or UTF-16BE. |
f0e51ce1 |
885 | */ |
f8194b21 |
886 | char *pdf_outline_convert(wchar_t *s, int *len) { |
ce1b04aa |
887 | char *ret; |
888 | |
889 | ret = utoa_careful_dup(s, CS_PDF); |
890 | |
891 | /* |
892 | * Very silly special case: if the returned string begins with |
893 | * FE FF, then the PDF reader will mistake it for a UTF-16BE |
894 | * string. So in this case we give up on PDFDocEncoding and |
895 | * encode it in UTF-16 straight away. |
896 | */ |
897 | if (ret && ret[0] == '\xFE' && ret[1] == '\xFF') { |
898 | sfree(ret); |
899 | ret = NULL; |
f0e51ce1 |
900 | } |
ce1b04aa |
901 | |
902 | if (!ret) { |
903 | ret = utoa_dup_len(s, CS_UTF16BE, len); |
904 | } else { |
905 | *len = strlen(ret); |
f0e51ce1 |
906 | } |
ce1b04aa |
907 | |
908 | return ret; |
f0e51ce1 |
909 | } |
910 | |
f0e51ce1 |
911 | static int make_outline(object *parent, outline_element *items, int n, |
912 | int open) |
913 | { |
914 | int level, totalcount = 0; |
915 | outline_element *itemp; |
916 | object *curr, *prev = NULL, *first = NULL, *last = NULL; |
f0e51ce1 |
917 | |
918 | assert(n > 0); |
919 | |
920 | level = items->level; |
921 | |
922 | while (n > 0) { |
7c8c4239 |
923 | char *title; |
ce1b04aa |
924 | int titlelen; |
f0e51ce1 |
925 | |
926 | /* |
927 | * Here we expect to be sitting on an item at the given |
928 | * level. So we start by constructing an outline entry for |
929 | * that item. |
930 | */ |
931 | assert(items->level == level); |
932 | |
ce1b04aa |
933 | title = pdf_outline_convert(items->pdata->outline_title, &titlelen); |
f0e51ce1 |
934 | |
935 | totalcount++; |
936 | curr = new_object(parent->list); |
937 | if (!first) first = curr; |
938 | last = curr; |
7c8c4239 |
939 | objtext(curr, "<<\n/Title "); |
ce1b04aa |
940 | pdf_string_len(objtext, curr, title, titlelen); |
993ade7a |
941 | sfree(title); |
7c8c4239 |
942 | objtext(curr, "\n/Parent "); |
f0e51ce1 |
943 | objref(curr, parent); |
3e7284e8 |
944 | objtext(curr, "\n/Dest"); |
945 | objdest(curr, items->pdata->first->page); |
946 | objtext(curr, "\n"); |
f0e51ce1 |
947 | if (prev) { |
948 | objtext(curr, "/Prev "); |
949 | objref(curr, prev); |
950 | objtext(curr, "\n"); |
951 | |
952 | objtext(prev, "/Next "); |
953 | objref(prev, curr); |
954 | objtext(prev, "\n>>\n"); |
955 | } |
956 | prev = curr; |
957 | |
958 | items++, n--; |
959 | for (itemp = items; itemp < items+n && itemp->level > level; |
960 | itemp++); |
961 | |
962 | if (itemp > items) { |
963 | char buf[80]; |
964 | int count = make_outline(curr, items, itemp - items, FALSE); |
965 | if (!open) |
966 | count = -count; |
967 | else |
968 | totalcount += count; |
969 | sprintf(buf, "/Count %d\n", count); |
970 | objtext(curr, buf); |
971 | } |
972 | |
973 | n -= itemp - items; |
974 | items = itemp; |
975 | } |
976 | objtext(prev, ">>\n"); |
977 | |
978 | assert(first && last); |
979 | objtext(parent, "/First "); |
980 | objref(parent, first); |
981 | objtext(parent, "\n/Last "); |
982 | objref(parent, last); |
983 | objtext(parent, "\n"); |
984 | |
985 | return totalcount; |
986 | } |
09358aa7 |
987 | |
988 | static int pdf_versionid(FILE *fp, word *words) |
989 | { |
990 | int ret; |
991 | |
992 | ret = fprintf(fp, "%% "); |
993 | |
994 | for (; words; words = words->next) { |
995 | char *text; |
996 | int type; |
997 | |
998 | switch (words->type) { |
999 | case word_HyperLink: |
1000 | case word_HyperEnd: |
1001 | case word_UpperXref: |
1002 | case word_LowerXref: |
1003 | case word_XrefEnd: |
1004 | case word_IndexRef: |
1005 | continue; |
1006 | } |
1007 | |
1008 | type = removeattr(words->type); |
1009 | |
1010 | switch (type) { |
1011 | case word_Normal: |
e4ea58f8 |
1012 | text = utoa_dup(words->text, CS_ASCII); |
09358aa7 |
1013 | break; |
1014 | case word_WhiteSpace: |
1015 | text = dupstr(" "); |
1016 | break; |
1017 | case word_Quote: |
1018 | text = dupstr("'"); |
1019 | break; |
1020 | } |
1021 | |
1022 | fputs(text, fp); |
1023 | ret += strlen(text); |
1024 | sfree(text); |
1025 | } |
1026 | |
1027 | ret += fprintf(fp, "\n"); |
1028 | |
1029 | return ret; |
1030 | } |
7c8c4239 |
1031 | |
ce1b04aa |
1032 | static void pdf_string_len(void (*add)(object *, char const *), |
1033 | object *o, char const *str, int len) |
7c8c4239 |
1034 | { |
1035 | char const *p; |
1036 | |
1037 | add(o, "("); |
ce1b04aa |
1038 | for (p = str; len > 0; p++, len--) { |
1039 | char c[10]; |
1040 | if (*p < ' ' || *p > '~') { |
1041 | sprintf(c, "\\%03o", 0xFF & (int)*p); |
1042 | } else { |
1043 | int n = 0; |
1044 | if (*p == '\\' || *p == '(' || *p == ')') |
1045 | c[n++] = '\\'; |
1046 | c[n++] = *p; |
1047 | c[n] = '\0'; |
1048 | } |
7c8c4239 |
1049 | add(o, c); |
1050 | } |
1051 | add(o, ")"); |
1052 | } |
ce1b04aa |
1053 | |
1054 | static void pdf_string(void (*add)(object *, char const *), |
1055 | object *o, char const *str) |
1056 | { |
1057 | pdf_string_len(add, o, str, strlen(str)); |
1058 | } |