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