2 * PDF backend for Halibut
9 #define TREE_BRANCH 2 /* max branching factor in page tree */
11 paragraph
*pdf_config_filename(char *filename
)
13 return cmdline_cfg_simple("pdf-filename", filename
, NULL
);
16 typedef struct object_Tag object
;
17 typedef struct objlist_Tag objlist
;
23 rdstringc main
, stream
;
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
);
36 static void pdf_string(void (*add
)(object
*, char const *),
37 object
*, char const *);
38 static void pdf_string_len(void (*add
)(object
*, char const *),
39 object
*, char const *, int);
40 static void objref(object
*o
, object
*dest
);
42 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
43 page_data
*last
, object
*resources
);
44 static int make_outline(object
*parent
, outline_element
*start
, int n
,
46 static int pdf_versionid(FILE *fp
, word
*words
);
48 void pdf_backend(paragraph
*sourceform
, keywordlist
*keywords
,
49 indexdata
*idx
, void *vdoc
) {
50 document
*doc
= (document
*)vdoc
;
59 object
*o
, *cat
, *outlines
, *pages
, *resources
;
65 filename
= dupstr("output.pdf");
66 for (p
= sourceform
; p
; p
= p
->next
) {
67 if (p
->type
== para_Config
&& p
->parent
) {
68 if (!ustricmp(p
->keyword
, L
"pdf-filename")) {
70 filename
= dupstr(adv(p
->origkeyword
));
75 olist
.head
= olist
.tail
= NULL
;
78 cat
= new_object(&olist
);
79 outlines
= new_object(&olist
);
80 pages
= new_object(&olist
);
81 resources
= new_object(&olist
);
84 * The catalogue just contains references to the outlines and
87 objtext(cat
, "<<\n/Type /Catalog\n/Outlines ");
88 objref(cat
, outlines
);
89 objtext(cat
, "\n/Pages ");
91 objtext(cat
, "\n/PageMode /UseOutlines\n>>\n");
94 * Set up the resources dictionary, which mostly means
95 * providing all the font objects and names to call them by.
98 objtext(resources
, "<<\n/Font <<\n");
99 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
) {
104 sprintf(fname
, "f%d", font_index
++);
105 fe
->name
= dupstr(fname
);
107 font
= new_object(&olist
);
109 objtext(resources
, "/");
110 objtext(resources
, fe
->name
);
111 objtext(resources
, " ");
112 objref(resources
, font
);
113 objtext(resources
, "\n");
115 objtext(font
, "<<\n/Type /Font\n/Subtype /Type1\n/Name /");
116 objtext(font
, fe
->name
);
117 objtext(font
, "\n/BaseFont /");
118 objtext(font
, fe
->font
->name
);
119 objtext(font
, "\n/Encoding <<\n/Type /Encoding\n/Differences [");
121 for (i
= 0; i
< 256; i
++) {
125 sprintf(buf
, "\n%d /", i
);
127 objtext(font
, fe
->vector
[i
] ? fe
->vector
[i
] : ".notdef");
130 objtext(font
, "\n]\n>>\n");
133 object
*widths
= new_object(&olist
);
134 objtext(font
, "/FirstChar 0\n/LastChar 255\n/Widths ");
135 objref(font
, widths
);
137 objtext(widths
, "[\n");
138 for (i
= 0; i
< 256; i
++) {
141 if (fe
->indices
[i
] < 0)
144 width
= fe
->font
->widths
[fe
->indices
[i
]];
145 sprintf(buf
, "%g\n", 1000.0 * width
/ 4096.0);
146 objtext(widths
, buf
);
148 objtext(widths
, "]\n");
151 objtext(font
, ">>\n");
153 objtext(resources
, ">>\n>>\n");
156 * Define the page objects for each page, and get each one
157 * ready to have a `Parent' specification added to it.
159 for (page
= doc
->pages
; page
; page
= page
->next
) {
162 opage
= new_object(&olist
);
164 objtext(opage
, "<<\n/Type /Page\n");
168 * Recursively build the page tree.
170 make_pages_node(pages
, NULL
, doc
->pages
, NULL
, resources
);
173 * Create and render the individual pages.
176 for (page
= doc
->pages
; page
; page
= page
->next
) {
177 object
*opage
, *cstr
;
179 text_fragment
*frag
, *frag_end
;
183 opage
= (object
*)page
->spare
;
185 * At this point the page dictionary is already
186 * half-written, with /Type and /Parent already present. We
187 * continue from there.
191 * The PDF spec says /Resources is required, but also says
192 * that it's inheritable and may be omitted if it's present
193 * in a Pages node. In our case it is: it's present in the
194 * topmost /Pages node because we carefully put it there.
195 * So we don't need a /Resources entry here.
197 sprintf(buf
, "/MediaBox [0 0 %g %g]\n",
198 doc
->paper_width
/ 4096.0, doc
->paper_height
/ 4096.0);
202 * Now we're ready to define a content stream containing
203 * the actual text on the page.
205 cstr
= new_object(&olist
);
206 objtext(opage
, "/Contents ");
208 objtext(opage
, "\n");
211 * Render any rectangles on the page.
213 for (r
= page
->first_rect
; r
; r
= r
->next
) {
215 sprintf(buf
, "%g %g %g %g re f\n", r
->x
/ 4096.0,
216 r
->y
/ 4096.0, r
->w
/ 4096.0, r
->h
/ 4096.0);
217 objstream(cstr
, buf
);
220 objstream(cstr
, "BT\n");
223 * PDF tracks two separate current positions: the position
224 * given in the `line matrix' and the position given in the
225 * `text matrix'. We must therefore track both as well.
226 * They start off at -1 (unset).
231 frag
= page
->first_text
;
234 * For compactness, I'm going to group text fragments
235 * into subsequences that use the same font+size. So
236 * first find the end of this subsequence.
238 for (frag_end
= frag
;
240 frag_end
->fe
== frag
->fe
&&
241 frag_end
->fontsize
== frag
->fontsize
);
242 frag_end
= frag_end
->next
);
245 * Now select the text fragment, and prepare to display
248 objstream(cstr
, "/");
249 objstream(cstr
, frag
->fe
->name
);
250 sprintf(buf
, " %d Tf ", frag
->fontsize
);
251 objstream(cstr
, buf
);
253 while (frag
&& frag
!= frag_end
) {
255 * Place the text position for the first piece of
259 sprintf(buf
, "1 0 0 1 %g %g Tm ",
260 frag
->x
/4096.0, frag
->y
/4096.0);
262 sprintf(buf
, "%g %g Td ",
263 (frag
->x
- lx
)/4096.0, (frag
->y
- ly
)/4096.0);
265 objstream(cstr
, buf
);
270 * See if we're going to use Tj (show a single
271 * string) or TJ (show an array of strings with
272 * x-spacings between them). We determine this by
273 * seeing if there's more than one text fragment in
274 * sequence with the same y-coordinate.
276 if (frag
->next
&& frag
->next
!= frag_end
&&
277 frag
->next
->y
== y
) {
281 objstream(cstr
, "[");
282 while (frag
&& frag
!= frag_end
&& frag
->y
== y
) {
285 (x
- frag
->x
) * 1000.0 /
286 (4096.0 * frag
->fontsize
));
287 objstream(cstr
, buf
);
289 pdf_string(objstream
, cstr
, frag
->text
);
290 x
= frag
->x
+ frag
->width
;
293 objstream(cstr
, "]TJ\n");
299 pdf_string(objstream
, cstr
, frag
->text
);
300 objstream(cstr
, "Tj\n");
305 objstream(cstr
, "ET");
308 * Also, we want an annotation dictionary containing the
309 * cross-references from this page.
311 if (page
->first_xref
) {
313 objtext(opage
, "/Annots [\n");
315 for (xr
= page
->first_xref
; xr
; xr
= xr
->next
) {
319 annot
= new_object(&olist
);
320 objref(opage
, annot
);
321 objtext(opage
, "\n");
323 objtext(annot
, "<<\n/Type /Annot\n/Subtype /Link\n/Rect [");
324 sprintf(buf
, "%g %g %g %g",
325 xr
->lx
/ 4096.0, xr
->by
/ 4096.0,
326 xr
->rx
/ 4096.0, xr
->ty
/ 4096.0);
328 objtext(annot
, "]\n/Border [0 0 0]\n");
330 if (xr
->dest
.type
== PAGE
) {
331 objtext(annot
, "/Dest [");
332 objref(annot
, (object
*)xr
->dest
.page
->spare
);
333 objtext(annot
, " /XYZ null null null]\n");
335 objtext(annot
, "/A <<\n/Type /Action\n/S /URI\n/URI ");
336 pdf_string(objtext
, annot
, xr
->dest
.url
);
337 objtext(annot
, "\n>>\n");
340 objtext(annot
, ">>\n");
343 objtext(opage
, "]\n");
346 objtext(opage
, ">>\n");
350 * Set up the outlines dictionary.
356 objtext(outlines
, "<<\n/Type /Outlines\n");
357 topcount
= make_outline(outlines
, doc
->outline_elements
,
358 doc
->n_outline_elements
, TRUE
);
359 sprintf(buf
, "/Count %d\n>>\n", topcount
);
360 objtext(outlines
, buf
);
364 * Assemble the final linear form of every object.
366 for (o
= olist
.head
; o
; o
= o
->next
) {
367 rdstringc rs
= {0, 0, NULL
};
370 sprintf(text
, "%d 0 obj\n", o
->number
);
373 if (!o
->main
.text
&& o
->stream
.text
) {
374 sprintf(text
, "<<\n/Length %d\n>>\n", o
->stream
.pos
);
375 rdaddsc(&o
->main
, text
);
378 assert(o
->main
.text
);
379 rdaddsc(&rs
, o
->main
.text
);
382 if (rs
.text
[rs
.pos
-1] != '\n')
385 if (o
->stream
.text
) {
387 * FIXME: If we ever start compressing stream data then
388 * it will have zero bytes in it, so we'll have to be
389 * more careful than this.
391 rdaddsc(&rs
, "stream\n");
392 rdaddsc(&rs
, o
->stream
.text
);
393 rdaddsc(&rs
, "\nendstream\n");
394 sfree(o
->stream
.text
);
397 rdaddsc(&rs
, "endobj\n");
404 * Write out the PDF file.
407 fp
= fopen(filename
, "wb");
409 error(err_cantopenw
, filename
);
414 * Header. I'm going to put the version IDs in the header as
415 * well, simply in PDF comments.
417 fileoff
= fprintf(fp
, "%%PDF-1.3\n");
418 for (p
= sourceform
; p
; p
= p
->next
)
419 if (p
->type
== para_VersionID
)
420 fileoff
+= pdf_versionid(fp
, p
->words
);
425 for (o
= olist
.head
; o
; o
= o
->next
) {
426 o
->fileoff
= fileoff
;
427 fwrite(o
->final
, 1, o
->size
, fp
);
432 * Cross-reference table
434 fprintf(fp
, "xref\n");
435 assert(olist
.head
->number
== 1);
436 fprintf(fp
, "0 %d\n", olist
.tail
->number
+ 1);
437 fprintf(fp
, "0000000000 65535 f \n");
438 for (o
= olist
.head
; o
; o
= o
->next
) {
440 sprintf(entry
, "%010d 00000 n \n", o
->fileoff
);
441 assert(strlen(entry
) == 20);
448 fprintf(fp
, "trailer\n<<\n/Size %d\n/Root %d 0 R\n>>\n",
449 olist
.tail
->number
+ 1, cat
->number
);
450 fprintf(fp
, "startxref\n%d\n%%%%EOF\n", fileoff
);
457 static object
*new_object(objlist
*list
)
459 object
*obj
= mknew(object
);
463 obj
->main
.text
= NULL
;
464 obj
->main
.pos
= obj
->main
.size
= 0;
465 obj
->stream
.text
= NULL
;
466 obj
->stream
.pos
= obj
->stream
.size
= 0;
468 obj
->number
= list
->number
++;
472 list
->tail
->next
= obj
;
483 static void objtext(object
*o
, char const *text
)
485 rdaddsc(&o
->main
, text
);
488 static void objstream(object
*o
, char const *text
)
490 rdaddsc(&o
->stream
, text
);
493 static void objref(object
*o
, object
*dest
)
496 sprintf(buf
, "%d 0 R", dest
->number
);
497 rdaddsc(&o
->main
, buf
);
500 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
501 page_data
*last
, object
*resources
)
507 objtext(node
, "<<\n/Type /Pages\n");
509 objtext(node
, "/Parent ");
510 objref(node
, parent
);
515 * Count the pages in this stretch, to see if there are few
516 * enough to reference directly.
519 for (page
= first
; page
; page
= page
->next
) {
525 sprintf(buf
, "/Count %d\n/Kids [\n", count
);
528 if (count
> TREE_BRANCH
) {
530 page_data
*thisfirst
, *thislast
;
534 for (i
= 0; i
< TREE_BRANCH
; i
++) {
535 int number
= (i
+1) * count
/ TREE_BRANCH
- i
* count
/ TREE_BRANCH
;
542 if (thisfirst
== thislast
) {
543 objref(node
, (object
*)thisfirst
->spare
);
544 objtext((object
*)thisfirst
->spare
, "/Parent ");
545 objref((object
*)thisfirst
->spare
, node
);
546 objtext((object
*)thisfirst
->spare
, "\n");
548 object
*newnode
= new_object(node
->list
);
549 make_pages_node(newnode
, node
, thisfirst
, thislast
, NULL
);
550 objref(node
, newnode
);
555 assert(thislast
== last
|| page
== NULL
);
558 for (page
= first
; page
; page
= page
->next
) {
559 objref(node
, (object
*)page
->spare
);
561 objtext((object
*)page
->spare
, "/Parent ");
562 objref((object
*)page
->spare
, node
);
563 objtext((object
*)page
->spare
, "\n");
569 objtext(node
, "]\n");
572 objtext(node
, "/Resources ");
573 objref(node
, resources
);
577 objtext(node
, ">>\n");
581 * In text on the page, PDF uses the PostScript font model, which
582 * means that glyphs are identified by PS strings and hence font
583 * encoding can be managed independently of the supplied encoding
584 * of the font. However, in the document outline, the PDF spec
585 * encodes in either PDFDocEncoding (a custom superset of
586 * ISO-8859-1) or UTF-16BE.
588 static char *pdf_outline_convert(wchar_t *s
, int *len
) {
591 ret
= utoa_careful_dup(s
, CS_PDF
);
594 * Very silly special case: if the returned string begins with
595 * FE FF, then the PDF reader will mistake it for a UTF-16BE
596 * string. So in this case we give up on PDFDocEncoding and
597 * encode it in UTF-16 straight away.
599 if (ret
&& ret
[0] == '\xFE' && ret
[1] == '\xFF') {
605 ret
= utoa_dup_len(s
, CS_UTF16BE
, len
);
613 static int make_outline(object
*parent
, outline_element
*items
, int n
,
616 int level
, totalcount
= 0;
617 outline_element
*itemp
;
618 object
*curr
, *prev
= NULL
, *first
= NULL
, *last
= NULL
;
622 level
= items
->level
;
629 * Here we expect to be sitting on an item at the given
630 * level. So we start by constructing an outline entry for
633 assert(items
->level
== level
);
635 title
= pdf_outline_convert(items
->pdata
->outline_title
, &titlelen
);
638 curr
= new_object(parent
->list
);
639 if (!first
) first
= curr
;
641 objtext(curr
, "<<\n/Title ");
642 pdf_string_len(objtext
, curr
, title
, titlelen
);
643 objtext(curr
, "\n/Parent ");
644 objref(curr
, parent
);
645 objtext(curr
, "\n/Dest [");
646 objref(curr
, (object
*)items
->pdata
->first
->page
->spare
);
647 objtext(curr
, " /XYZ null null null]\n");
649 objtext(curr
, "/Prev ");
653 objtext(prev
, "/Next ");
655 objtext(prev
, "\n>>\n");
660 for (itemp
= items
; itemp
< items
+n
&& itemp
->level
> level
;
665 int count
= make_outline(curr
, items
, itemp
- items
, FALSE
);
670 sprintf(buf
, "/Count %d\n", count
);
677 objtext(prev
, ">>\n");
679 assert(first
&& last
);
680 objtext(parent
, "/First ");
681 objref(parent
, first
);
682 objtext(parent
, "\n/Last ");
683 objref(parent
, last
);
684 objtext(parent
, "\n");
689 static int pdf_versionid(FILE *fp
, word
*words
)
693 ret
= fprintf(fp
, "%% ");
695 for (; words
; words
= words
->next
) {
699 switch (words
->type
) {
709 type
= removeattr(words
->type
);
713 text
= utoa_dup(words
->text
, CS_ASCII
);
715 case word_WhiteSpace
:
728 ret
+= fprintf(fp
, "\n");
733 static void pdf_string_len(void (*add
)(object
*, char const *),
734 object
*o
, char const *str
, int len
)
739 for (p
= str
; len
> 0; p
++, len
--) {
741 if (*p
< ' ' || *p
> '~') {
742 sprintf(c
, "\\%03o", 0xFF & (int)*p
);
745 if (*p
== '\\' || *p
== '(' || *p
== ')')
755 static void pdf_string(void (*add
)(object
*, char const *),
756 object
*o
, char const *str
)
758 pdf_string_len(add
, o
, str
, strlen(str
));