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", |
0ca5b0e1 |
260 | (unsigned short) |
1cd00fea |
261 | sfnt_glyphtoindex(fe->font->info->fontfile, |
262 | fe->vector[i])); |
263 | objstream(cmap, buf); |
264 | blk--; |
265 | } |
266 | i++; |
267 | } |
268 | objstream(cmap, "endcidrange\n"); |
269 | } |
270 | i = 0; |
271 | while (nchars) { |
272 | unsigned blk = nchars > 100 ? 100 : nchars; |
273 | nchars -= blk; |
274 | sprintf(buf, "%u ", blk); |
bb8bfd66 |
275 | objstream(cmap, buf); |
1cd00fea |
276 | objstream(cmap, "begincidchar\n"); |
277 | while (blk) { |
278 | if (ranges[i] == 1) { |
279 | sprintf(buf, "<%02X>", i); |
280 | objstream(cmap, buf); |
281 | sprintf(buf, "%hu\n", |
0ca5b0e1 |
282 | (unsigned short) |
1cd00fea |
283 | sfnt_glyphtoindex(fe->font->info->fontfile, |
284 | fe->vector[i])); |
285 | objstream(cmap, buf); |
286 | blk--; |
287 | } |
288 | i++; |
289 | } |
290 | objstream(cmap, "endcidchar\n"); |
bb8bfd66 |
291 | } |
292 | objstream(cmap, "endcmap CMapName currentdict /CMap " |
293 | "defineresource pop end end\n%%EndResource\n%%EOF\n"); |
1cd00fea |
294 | |
dde3b989 |
295 | objref(font, cmap); |
296 | objtext(font, "\n/DescendantFonts["); |
297 | objref(font, cidfont); |
298 | objtext(font, "]\n"); |
299 | objtext(cidfont, "<<\n/Type/Font\n/Subtype/CIDFontType2\n" |
300 | "/BaseFont/"); |
301 | objtext(cidfont, fe->font->info->name); |
302 | objtext(cidfont, "\n/CIDSystemInfo<</Registry(Adobe)" |
303 | "/Ordering(Identity)/Supplement 0>>\n"); |
304 | objtext(cidfont, "/FontDescriptor "); |
305 | objref(cidfont, fontdesc); |
e7d4c88f |
306 | objtext(cidfont, "\n/W[0["); |
f4944c7c |
307 | for (i = 0; i < (int)sfnt_nglyphs(fe->font->info->fontfile); i++) { |
e7d4c88f |
308 | char buf[20]; |
309 | double width; |
310 | width = find_width(fe->font, |
311 | sfnt_indextoglyph(fe->font->info->fontfile, i)); |
312 | sprintf(buf, "%g ", 1000.0 * width / FUNITS_PER_PT); |
313 | objtext(cidfont, buf); |
314 | } |
315 | objtext(cidfont, "]]>>\n"); |
dde3b989 |
316 | } else { |
317 | objtext(font, "/Subtype /Type1\n"); |
318 | objtext(font, "\n/Encoding <<\n/Type /Encoding\n/Differences ["); |
319 | |
320 | for (i = 0; i < 256; i++) { |
321 | char buf[20]; |
322 | if (fe->vector[i] == NOGLYPH) |
323 | continue; |
324 | if (i != prev + 1) { |
325 | sprintf(buf, "\n%d", i); |
326 | objtext(font, buf); |
327 | } |
328 | objtext(font, i % 8 ? "/" : "\n/"); |
329 | objtext(font, glyph_extern(fe->vector[i])); |
330 | prev = i; |
331 | } |
332 | |
333 | objtext(font, "\n]\n>>\n"); |
334 | if (!is_std_font(fe->font->info->name)){ |
335 | object *widths = new_object(&olist); |
336 | int firstchar = -1, lastchar = -1; |
337 | for (i = 0; i < 256; i++) |
338 | if (fe->vector[i] != NOGLYPH) { |
339 | if (firstchar < 0) firstchar = i; |
340 | lastchar = i; |
341 | } |
342 | sprintf(buf, "/FirstChar %d\n/LastChar %d\n/Widths ", |
343 | firstchar, lastchar); |
344 | objtext(font, buf); |
345 | objref(font, widths); |
346 | objtext(font, "\n"); |
347 | objtext(widths, "[\n"); |
348 | for (i = firstchar; i <= lastchar; i++) { |
349 | double width; |
350 | if (fe->vector[i] == NOGLYPH) |
351 | width = 0.0; |
352 | else |
353 | width = find_width(fe->font, fe->vector[i]); |
354 | sprintf(buf, "%g\n", 1000.0 * width / FUNITS_PER_PT); |
355 | objtext(widths, buf); |
356 | } |
357 | objtext(widths, "]\n"); |
358 | objtext(font, "/FontDescriptor "); |
359 | objref(font, fontdesc); |
360 | } |
361 | |
362 | } |
363 | |
364 | if (!is_std_font(fe->font->info->name)) { |
babfe3e2 |
365 | if (fi->fontfile && fi->filetype == TYPE1) { |
44407fea |
366 | object *fontfile = new_object(&olist); |
44407fea |
367 | size_t len; |
c885c2ff |
368 | char *ffbuf; |
369 | |
370 | pf_part1((font_info *)fi, &ffbuf, &len); |
371 | objstream_len(fontfile, ffbuf, len); |
372 | sfree(ffbuf); |
373 | sprintf(buf, "<<\n/Length1 %lu\n", (unsigned long)len); |
374 | objtext(fontfile, buf); |
375 | pf_part2((font_info *)fi, &ffbuf, &len); |
376 | objstream_len(fontfile, ffbuf, len); |
377 | sfree(ffbuf); |
378 | sprintf(buf, "/Length2 %lu\n", (unsigned long)len); |
379 | objtext(fontfile, buf); |
380 | objtext(fontfile, "/Length3 0\n"); |
44407fea |
381 | objtext(fontdesc, "/FontFile "); |
382 | objref(fontdesc, fontfile); |
dde3b989 |
383 | } else if (fi->fontfile && fi->filetype == TRUETYPE) { |
384 | object *fontfile = new_object(&olist); |
385 | size_t len; |
386 | char *ffbuf; |
387 | |
388 | sfnt_data((font_info *)fi, &ffbuf, &len); |
389 | objstream_len(fontfile, ffbuf, len); |
390 | sprintf(buf, "<<\n/Length1 %lu\n", (unsigned long)len); |
391 | objtext(fontfile, buf); |
392 | objtext(fontdesc, "/FontFile2 "); |
393 | objref(fontdesc, fontfile); |
44407fea |
394 | } |
395 | objtext(fontdesc, "\n>>\n"); |
43341922 |
396 | } |
397 | |
255b7ff3 |
398 | objtext(font, "\n>>\n"); |
43341922 |
399 | } |
400 | objtext(resources, ">>\n>>\n"); |
401 | |
2ff8419e |
402 | { |
403 | char buf[255]; |
404 | mediabox = new_object(&olist); |
405 | sprintf(buf, "[0 0 %g %g]\n", |
406 | doc->paper_width / FUNITS_PER_PT, |
407 | doc->paper_height / FUNITS_PER_PT); |
408 | objtext(mediabox, buf); |
409 | } |
410 | |
43341922 |
411 | /* |
412 | * Define the page objects for each page, and get each one |
413 | * ready to have a `Parent' specification added to it. |
414 | */ |
415 | for (page = doc->pages; page; page = page->next) { |
416 | object *opage; |
417 | |
418 | opage = new_object(&olist); |
419 | page->spare = opage; |
420 | objtext(opage, "<<\n/Type /Page\n"); |
421 | } |
422 | |
423 | /* |
424 | * Recursively build the page tree. |
425 | */ |
2ff8419e |
426 | make_pages_node(pages, NULL, doc->pages, NULL, resources, mediabox); |
43341922 |
427 | |
428 | /* |
429 | * Create and render the individual pages. |
430 | */ |
43341922 |
431 | for (page = doc->pages; page; page = page->next) { |
432 | object *opage, *cstr; |
23765aeb |
433 | rect *r; |
7c8c4239 |
434 | text_fragment *frag, *frag_end; |
43341922 |
435 | char buf[256]; |
7c8c4239 |
436 | int x, y, lx, ly; |
43341922 |
437 | |
438 | opage = (object *)page->spare; |
439 | /* |
440 | * At this point the page dictionary is already |
441 | * half-written, with /Type and /Parent already present. We |
442 | * continue from there. |
443 | */ |
444 | |
445 | /* |
446 | * The PDF spec says /Resources is required, but also says |
447 | * that it's inheritable and may be omitted if it's present |
448 | * in a Pages node. In our case it is: it's present in the |
449 | * topmost /Pages node because we carefully put it there. |
2ff8419e |
450 | * So we don't need a /Resources entry here. The same applies |
451 | * to /MediaBox. |
43341922 |
452 | */ |
43341922 |
453 | |
454 | /* |
455 | * Now we're ready to define a content stream containing |
456 | * the actual text on the page. |
457 | */ |
458 | cstr = new_object(&olist); |
459 | objtext(opage, "/Contents "); |
460 | objref(opage, cstr); |
461 | objtext(opage, "\n"); |
462 | |
23765aeb |
463 | /* |
464 | * Render any rectangles on the page. |
465 | */ |
466 | for (r = page->first_rect; r; r = r->next) { |
467 | char buf[512]; |
17c71b41 |
468 | sprintf(buf, "%g %g %g %g re f\n", |
469 | r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT, |
470 | r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT); |
23765aeb |
471 | objstream(cstr, buf); |
472 | } |
473 | |
43341922 |
474 | objstream(cstr, "BT\n"); |
43341922 |
475 | |
7c8c4239 |
476 | /* |
477 | * PDF tracks two separate current positions: the position |
478 | * given in the `line matrix' and the position given in the |
479 | * `text matrix'. We must therefore track both as well. |
480 | * They start off at -1 (unset). |
481 | */ |
482 | lx = ly = -1; |
483 | x = y = -1; |
484 | |
485 | frag = page->first_text; |
486 | while (frag) { |
487 | /* |
488 | * For compactness, I'm going to group text fragments |
489 | * into subsequences that use the same font+size. So |
490 | * first find the end of this subsequence. |
491 | */ |
492 | for (frag_end = frag; |
493 | (frag_end && |
494 | frag_end->fe == frag->fe && |
495 | frag_end->fontsize == frag->fontsize); |
496 | frag_end = frag_end->next); |
497 | |
498 | /* |
499 | * Now select the text fragment, and prepare to display |
500 | * the text. |
501 | */ |
43341922 |
502 | objstream(cstr, "/"); |
503 | objstream(cstr, frag->fe->name); |
7c8c4239 |
504 | sprintf(buf, " %d Tf ", frag->fontsize); |
43341922 |
505 | objstream(cstr, buf); |
506 | |
7c8c4239 |
507 | while (frag && frag != frag_end) { |
508 | /* |
509 | * Place the text position for the first piece of |
510 | * text. |
511 | */ |
512 | if (lx < 0) { |
513 | sprintf(buf, "1 0 0 1 %g %g Tm ", |
17c71b41 |
514 | frag->x/FUNITS_PER_PT, frag->y/FUNITS_PER_PT); |
7c8c4239 |
515 | } else { |
516 | sprintf(buf, "%g %g Td ", |
17c71b41 |
517 | (frag->x - lx)/FUNITS_PER_PT, |
518 | (frag->y - ly)/FUNITS_PER_PT); |
7c8c4239 |
519 | } |
43341922 |
520 | objstream(cstr, buf); |
7c8c4239 |
521 | lx = x = frag->x; |
522 | ly = y = frag->y; |
523 | |
524 | /* |
525 | * See if we're going to use Tj (show a single |
526 | * string) or TJ (show an array of strings with |
527 | * x-spacings between them). We determine this by |
528 | * seeing if there's more than one text fragment in |
529 | * sequence with the same y-coordinate. |
530 | */ |
531 | if (frag->next && frag->next != frag_end && |
532 | frag->next->y == y) { |
533 | /* |
534 | * The TJ strategy. |
535 | */ |
536 | objstream(cstr, "["); |
537 | while (frag && frag != frag_end && frag->y == y) { |
538 | if (frag->x != x) { |
539 | sprintf(buf, "%g", |
540 | (x - frag->x) * 1000.0 / |
17c71b41 |
541 | (FUNITS_PER_PT * frag->fontsize)); |
7c8c4239 |
542 | objstream(cstr, buf); |
543 | } |
544 | pdf_string(objstream, cstr, frag->text); |
545 | x = frag->x + frag->width; |
546 | frag = frag->next; |
547 | } |
548 | objstream(cstr, "]TJ\n"); |
549 | } else |
550 | { |
551 | /* |
552 | * The Tj strategy. |
553 | */ |
554 | pdf_string(objstream, cstr, frag->text); |
555 | objstream(cstr, "Tj\n"); |
556 | frag = frag->next; |
557 | } |
43341922 |
558 | } |
43341922 |
559 | } |
560 | objstream(cstr, "ET"); |
561 | |
138d7ffb |
562 | /* |
563 | * Also, we want an annotation dictionary containing the |
564 | * cross-references from this page. |
565 | */ |
566 | if (page->first_xref) { |
567 | xref *xr; |
568 | objtext(opage, "/Annots [\n"); |
569 | |
570 | for (xr = page->first_xref; xr; xr = xr->next) { |
138d7ffb |
571 | char buf[256]; |
572 | |
6e4d0fd8 |
573 | objtext(opage, "<</Subtype/Link\n/Rect["); |
138d7ffb |
574 | sprintf(buf, "%g %g %g %g", |
17c71b41 |
575 | xr->lx / FUNITS_PER_PT, xr->by / FUNITS_PER_PT, |
576 | xr->rx / FUNITS_PER_PT, xr->ty / FUNITS_PER_PT); |
6e4d0fd8 |
577 | objtext(opage, buf); |
578 | objtext(opage, "]/Border[0 0 0]\n"); |
138d7ffb |
579 | |
580 | if (xr->dest.type == PAGE) { |
3e7284e8 |
581 | objtext(opage, "/Dest"); |
582 | objdest(opage, xr->dest.page); |
138d7ffb |
583 | } else { |
6e4d0fd8 |
584 | objtext(opage, "/A<</S/URI/URI"); |
585 | pdf_string(objtext, opage, xr->dest.url); |
586 | objtext(opage, ">>"); |
138d7ffb |
587 | } |
588 | |
6e4d0fd8 |
589 | objtext(opage, ">>\n"); |
138d7ffb |
590 | } |
591 | |
592 | objtext(opage, "]\n"); |
593 | } |
594 | |
43341922 |
595 | objtext(opage, ">>\n"); |
596 | } |
597 | |
598 | /* |
f0e51ce1 |
599 | * Set up the outlines dictionary. |
600 | */ |
a615a778 |
601 | if (outlines) { |
f0e51ce1 |
602 | int topcount; |
603 | char buf[80]; |
604 | |
605 | objtext(outlines, "<<\n/Type /Outlines\n"); |
606 | topcount = make_outline(outlines, doc->outline_elements, |
607 | doc->n_outline_elements, TRUE); |
608 | sprintf(buf, "/Count %d\n>>\n", topcount); |
609 | objtext(outlines, buf); |
610 | } |
611 | |
612 | /* |
43341922 |
613 | * Assemble the final linear form of every object. |
614 | */ |
615 | for (o = olist.head; o; o = o->next) { |
616 | rdstringc rs = {0, 0, NULL}; |
617 | char text[80]; |
7e2417cc |
618 | deflate_compress_ctx *zcontext; |
619 | void *zbuf; |
620 | int zlen; |
43341922 |
621 | |
622 | sprintf(text, "%d 0 obj\n", o->number); |
623 | rdaddsc(&rs, text); |
624 | |
c885c2ff |
625 | if (o->stream.text) { |
3e2dd889 |
626 | if (!o->main.text) |
627 | rdaddsc(&o->main, "<<\n"); |
628 | #ifdef PDF_NOCOMPRESS |
629 | zlen = o->stream.pos; |
630 | zbuf = snewn(zlen, char); |
631 | memcpy(zbuf, o->stream.text, zlen); |
632 | sprintf(text, "/Length %d\n>>\n", zlen); |
bb8bfd66 |
633 | #else |
7e2417cc |
634 | zcontext = deflate_compress_new(DEFLATE_TYPE_ZLIB); |
635 | deflate_compress_data(zcontext, o->stream.text, o->stream.pos, |
636 | DEFLATE_END_OF_DATA, &zbuf, &zlen); |
637 | deflate_compress_free(zcontext); |
c885c2ff |
638 | sprintf(text, "/Filter/FlateDecode\n/Length %d\n>>\n", zlen); |
3e2dd889 |
639 | #endif |
43341922 |
640 | rdaddsc(&o->main, text); |
641 | } |
642 | |
643 | assert(o->main.text); |
644 | rdaddsc(&rs, o->main.text); |
645 | sfree(o->main.text); |
646 | |
647 | if (rs.text[rs.pos-1] != '\n') |
648 | rdaddc(&rs, '\n'); |
649 | |
650 | if (o->stream.text) { |
43341922 |
651 | rdaddsc(&rs, "stream\n"); |
7e2417cc |
652 | rdaddsn(&rs, zbuf, zlen); |
43341922 |
653 | rdaddsc(&rs, "\nendstream\n"); |
654 | sfree(o->stream.text); |
7e2417cc |
655 | sfree(zbuf); |
43341922 |
656 | } |
657 | |
658 | rdaddsc(&rs, "endobj\n"); |
659 | |
660 | o->final = rs.text; |
661 | o->size = rs.pos; |
662 | } |
663 | |
664 | /* |
665 | * Write out the PDF file. |
666 | */ |
667 | |
0041ffdb |
668 | if (!strcmp(filename, "-")) |
669 | fp = stdout; |
670 | else |
671 | fp = fopen(filename, "wb"); |
43341922 |
672 | if (!fp) { |
673 | error(err_cantopenw, filename); |
674 | return; |
675 | } |
676 | |
677 | /* |
09358aa7 |
678 | * Header. I'm going to put the version IDs in the header as |
7e2417cc |
679 | * well, simply in PDF comments. The PDF Reference also suggests |
680 | * that binary PDF files contain four top-bit-set characters in |
681 | * the second line. |
43341922 |
682 | */ |
7e2417cc |
683 | fileoff = fprintf(fp, "%%PDF-1.3\n%% L\xc3\xba\xc3\xb0""a\n"); |
09358aa7 |
684 | for (p = sourceform; p; p = p->next) |
685 | if (p->type == para_VersionID) |
686 | fileoff += pdf_versionid(fp, p->words); |
43341922 |
687 | |
688 | /* |
689 | * Body |
690 | */ |
691 | for (o = olist.head; o; o = o->next) { |
692 | o->fileoff = fileoff; |
693 | fwrite(o->final, 1, o->size, fp); |
694 | fileoff += o->size; |
695 | } |
696 | |
697 | /* |
698 | * Cross-reference table |
699 | */ |
700 | fprintf(fp, "xref\n"); |
701 | assert(olist.head->number == 1); |
702 | fprintf(fp, "0 %d\n", olist.tail->number + 1); |
703 | fprintf(fp, "0000000000 65535 f \n"); |
704 | for (o = olist.head; o; o = o->next) { |
705 | char entry[40]; |
706 | sprintf(entry, "%010d 00000 n \n", o->fileoff); |
707 | assert(strlen(entry) == 20); |
708 | fputs(entry, fp); |
709 | } |
710 | |
711 | /* |
712 | * Trailer |
713 | */ |
993ade7a |
714 | fprintf(fp, "trailer\n<<\n/Size %d\n/Root %d 0 R\n/Info %d 0 R\n>>\n", |
715 | olist.tail->number + 1, cat->number, info->number); |
43341922 |
716 | fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff); |
717 | |
0041ffdb |
718 | if (fp != stdout) |
719 | fclose(fp); |
43341922 |
720 | |
721 | sfree(filename); |
722 | } |
723 | |
dde3b989 |
724 | object *new_object(objlist *list) |
43341922 |
725 | { |
f1530049 |
726 | object *obj = snew(object); |
43341922 |
727 | |
728 | obj->list = list; |
729 | |
730 | obj->main.text = NULL; |
731 | obj->main.pos = obj->main.size = 0; |
732 | obj->stream.text = NULL; |
733 | obj->stream.pos = obj->stream.size = 0; |
734 | |
735 | obj->number = list->number++; |
736 | |
737 | obj->next = NULL; |
738 | if (list->tail) |
739 | list->tail->next = obj; |
740 | else |
741 | list->head = obj; |
742 | list->tail = obj; |
743 | |
744 | obj->size = 0; |
745 | obj->final = NULL; |
746 | |
747 | return obj; |
748 | } |
749 | |
dde3b989 |
750 | void objtext(object *o, char const *text) |
43341922 |
751 | { |
752 | rdaddsc(&o->main, text); |
753 | } |
754 | |
dde3b989 |
755 | void objstream_len(object *o, char const *text, size_t len) |
c885c2ff |
756 | { |
757 | rdaddsn(&o->stream, text, len); |
758 | } |
759 | |
dde3b989 |
760 | void objstream(object *o, char const *text) |
43341922 |
761 | { |
762 | rdaddsc(&o->stream, text); |
763 | } |
764 | |
765 | static void objref(object *o, object *dest) |
766 | { |
767 | char buf[40]; |
768 | sprintf(buf, "%d 0 R", dest->number); |
769 | rdaddsc(&o->main, buf); |
770 | } |
771 | |
3e7284e8 |
772 | static void objdest(object *o, page_data *p) { |
773 | objtext(o, "["); |
774 | objref(o, (object *)p->spare); |
775 | objtext(o, "/XYZ null null null]"); |
776 | } |
777 | |
3600720b |
778 | static char const * const stdfonts[] = { |
779 | "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic", |
780 | "Helvetica", "Helvetica-Bold", "Helvetica-Oblique","Helvetica-BoldOblique", |
781 | "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique", |
782 | "Symbol", "ZapfDingbats" |
783 | }; |
784 | |
785 | static int is_std_font(char const *name) { |
786 | unsigned i; |
787 | for (i = 0; i < lenof(stdfonts); i++) |
788 | if (strcmp(name, stdfonts[i]) == 0) |
789 | return TRUE; |
790 | return FALSE; |
791 | } |
792 | |
43341922 |
793 | static void make_pages_node(object *node, object *parent, page_data *first, |
2ff8419e |
794 | page_data *last, object *resources, |
795 | object *mediabox) |
43341922 |
796 | { |
797 | int count; |
798 | page_data *page; |
799 | char buf[80]; |
800 | |
801 | objtext(node, "<<\n/Type /Pages\n"); |
802 | if (parent) { |
803 | objtext(node, "/Parent "); |
804 | objref(node, parent); |
805 | objtext(node, "\n"); |
806 | } |
807 | |
808 | /* |
809 | * Count the pages in this stretch, to see if there are few |
810 | * enough to reference directly. |
811 | */ |
812 | count = 0; |
813 | for (page = first; page; page = page->next) { |
814 | count++; |
815 | if (page == last) |
816 | break; |
817 | } |
818 | |
819 | sprintf(buf, "/Count %d\n/Kids [\n", count); |
820 | objtext(node, buf); |
821 | |
822 | if (count > TREE_BRANCH) { |
823 | int i; |
824 | page_data *thisfirst, *thislast; |
825 | |
826 | page = first; |
827 | |
828 | for (i = 0; i < TREE_BRANCH; i++) { |
829 | int number = (i+1) * count / TREE_BRANCH - i * count / TREE_BRANCH; |
830 | thisfirst = page; |
831 | while (number--) { |
832 | thislast = page; |
833 | page = page->next; |
834 | } |
835 | |
836 | if (thisfirst == thislast) { |
837 | objref(node, (object *)thisfirst->spare); |
838 | objtext((object *)thisfirst->spare, "/Parent "); |
839 | objref((object *)thisfirst->spare, node); |
840 | objtext((object *)thisfirst->spare, "\n"); |
841 | } else { |
842 | object *newnode = new_object(node->list); |
2ff8419e |
843 | make_pages_node(newnode, node, thisfirst, thislast, |
844 | NULL, NULL); |
43341922 |
845 | objref(node, newnode); |
846 | } |
847 | objtext(node, "\n"); |
848 | } |
849 | |
850 | assert(thislast == last || page == NULL); |
851 | |
852 | } else { |
853 | for (page = first; page; page = page->next) { |
854 | objref(node, (object *)page->spare); |
855 | objtext(node, "\n"); |
856 | objtext((object *)page->spare, "/Parent "); |
857 | objref((object *)page->spare, node); |
858 | objtext((object *)page->spare, "\n"); |
859 | if (page == last) |
860 | break; |
861 | } |
862 | } |
863 | |
864 | objtext(node, "]\n"); |
865 | |
866 | if (resources) { |
867 | objtext(node, "/Resources "); |
868 | objref(node, resources); |
869 | objtext(node, "\n"); |
870 | } |
2ff8419e |
871 | if (mediabox) { |
872 | objtext(node, "/MediaBox "); |
873 | objref(node, mediabox); |
874 | objtext(node, "\n"); |
875 | } |
43341922 |
876 | |
877 | objtext(node, ">>\n"); |
878 | } |
f0e51ce1 |
879 | |
880 | /* |
881 | * In text on the page, PDF uses the PostScript font model, which |
882 | * means that glyphs are identified by PS strings and hence font |
883 | * encoding can be managed independently of the supplied encoding |
884 | * of the font. However, in the document outline, the PDF spec |
ce1b04aa |
885 | * encodes in either PDFDocEncoding (a custom superset of |
886 | * ISO-8859-1) or UTF-16BE. |
f0e51ce1 |
887 | */ |
f8194b21 |
888 | char *pdf_outline_convert(wchar_t *s, int *len) { |
ce1b04aa |
889 | char *ret; |
890 | |
891 | ret = utoa_careful_dup(s, CS_PDF); |
892 | |
893 | /* |
894 | * Very silly special case: if the returned string begins with |
895 | * FE FF, then the PDF reader will mistake it for a UTF-16BE |
896 | * string. So in this case we give up on PDFDocEncoding and |
897 | * encode it in UTF-16 straight away. |
898 | */ |
899 | if (ret && ret[0] == '\xFE' && ret[1] == '\xFF') { |
900 | sfree(ret); |
901 | ret = NULL; |
f0e51ce1 |
902 | } |
ce1b04aa |
903 | |
904 | if (!ret) { |
905 | ret = utoa_dup_len(s, CS_UTF16BE, len); |
906 | } else { |
907 | *len = strlen(ret); |
f0e51ce1 |
908 | } |
ce1b04aa |
909 | |
910 | return ret; |
f0e51ce1 |
911 | } |
912 | |
f0e51ce1 |
913 | static int make_outline(object *parent, outline_element *items, int n, |
914 | int open) |
915 | { |
916 | int level, totalcount = 0; |
917 | outline_element *itemp; |
918 | object *curr, *prev = NULL, *first = NULL, *last = NULL; |
f0e51ce1 |
919 | |
920 | assert(n > 0); |
921 | |
922 | level = items->level; |
923 | |
924 | while (n > 0) { |
7c8c4239 |
925 | char *title; |
ce1b04aa |
926 | int titlelen; |
f0e51ce1 |
927 | |
928 | /* |
929 | * Here we expect to be sitting on an item at the given |
930 | * level. So we start by constructing an outline entry for |
931 | * that item. |
932 | */ |
933 | assert(items->level == level); |
934 | |
ce1b04aa |
935 | title = pdf_outline_convert(items->pdata->outline_title, &titlelen); |
f0e51ce1 |
936 | |
937 | totalcount++; |
938 | curr = new_object(parent->list); |
939 | if (!first) first = curr; |
940 | last = curr; |
7c8c4239 |
941 | objtext(curr, "<<\n/Title "); |
ce1b04aa |
942 | pdf_string_len(objtext, curr, title, titlelen); |
993ade7a |
943 | sfree(title); |
7c8c4239 |
944 | objtext(curr, "\n/Parent "); |
f0e51ce1 |
945 | objref(curr, parent); |
3e7284e8 |
946 | objtext(curr, "\n/Dest"); |
947 | objdest(curr, items->pdata->first->page); |
948 | objtext(curr, "\n"); |
f0e51ce1 |
949 | if (prev) { |
950 | objtext(curr, "/Prev "); |
951 | objref(curr, prev); |
952 | objtext(curr, "\n"); |
953 | |
954 | objtext(prev, "/Next "); |
955 | objref(prev, curr); |
956 | objtext(prev, "\n>>\n"); |
957 | } |
958 | prev = curr; |
959 | |
960 | items++, n--; |
961 | for (itemp = items; itemp < items+n && itemp->level > level; |
962 | itemp++); |
963 | |
964 | if (itemp > items) { |
965 | char buf[80]; |
966 | int count = make_outline(curr, items, itemp - items, FALSE); |
967 | if (!open) |
968 | count = -count; |
969 | else |
970 | totalcount += count; |
971 | sprintf(buf, "/Count %d\n", count); |
972 | objtext(curr, buf); |
973 | } |
974 | |
975 | n -= itemp - items; |
976 | items = itemp; |
977 | } |
978 | objtext(prev, ">>\n"); |
979 | |
980 | assert(first && last); |
981 | objtext(parent, "/First "); |
982 | objref(parent, first); |
983 | objtext(parent, "\n/Last "); |
984 | objref(parent, last); |
985 | objtext(parent, "\n"); |
986 | |
987 | return totalcount; |
988 | } |
09358aa7 |
989 | |
990 | static int pdf_versionid(FILE *fp, word *words) |
991 | { |
992 | int ret; |
993 | |
994 | ret = fprintf(fp, "%% "); |
995 | |
996 | for (; words; words = words->next) { |
997 | char *text; |
998 | int type; |
999 | |
1000 | switch (words->type) { |
1001 | case word_HyperLink: |
1002 | case word_HyperEnd: |
1003 | case word_UpperXref: |
1004 | case word_LowerXref: |
1005 | case word_XrefEnd: |
1006 | case word_IndexRef: |
1007 | continue; |
1008 | } |
1009 | |
1010 | type = removeattr(words->type); |
1011 | |
1012 | switch (type) { |
1013 | case word_Normal: |
e4ea58f8 |
1014 | text = utoa_dup(words->text, CS_ASCII); |
09358aa7 |
1015 | break; |
1016 | case word_WhiteSpace: |
1017 | text = dupstr(" "); |
1018 | break; |
1019 | case word_Quote: |
1020 | text = dupstr("'"); |
1021 | break; |
1022 | } |
1023 | |
1024 | fputs(text, fp); |
1025 | ret += strlen(text); |
1026 | sfree(text); |
1027 | } |
1028 | |
1029 | ret += fprintf(fp, "\n"); |
1030 | |
1031 | return ret; |
1032 | } |
7c8c4239 |
1033 | |
ce1b04aa |
1034 | static void pdf_string_len(void (*add)(object *, char const *), |
1035 | object *o, char const *str, int len) |
7c8c4239 |
1036 | { |
1037 | char const *p; |
1038 | |
1039 | add(o, "("); |
ce1b04aa |
1040 | for (p = str; len > 0; p++, len--) { |
1041 | char c[10]; |
1042 | if (*p < ' ' || *p > '~') { |
1043 | sprintf(c, "\\%03o", 0xFF & (int)*p); |
1044 | } else { |
1045 | int n = 0; |
1046 | if (*p == '\\' || *p == '(' || *p == ')') |
1047 | c[n++] = '\\'; |
1048 | c[n++] = *p; |
1049 | c[n] = '\0'; |
1050 | } |
7c8c4239 |
1051 | add(o, c); |
1052 | } |
1053 | add(o, ")"); |
1054 | } |
ce1b04aa |
1055 | |
1056 | static void pdf_string(void (*add)(object *, char const *), |
1057 | object *o, char const *str) |
1058 | { |
1059 | pdf_string_len(add, o, str, strlen(str)); |
1060 | } |