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