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 objref(object
*o
, object
*dest
);
40 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
41 page_data
*last
, object
*resources
);
42 static int make_outline(object
*parent
, outline_element
*start
, int n
,
44 static int pdf_versionid(FILE *fp
, word
*words
);
46 void pdf_backend(paragraph
*sourceform
, keywordlist
*keywords
,
47 indexdata
*idx
, void *vdoc
) {
48 document
*doc
= (document
*)vdoc
;
57 object
*o
, *cat
, *outlines
, *pages
, *resources
;
63 filename
= dupstr("output.pdf");
64 for (p
= sourceform
; p
; p
= p
->next
) {
65 if (p
->type
== para_Config
&& p
->parent
) {
66 if (!ustricmp(p
->keyword
, L
"pdf-filename")) {
68 filename
= dupstr(adv(p
->origkeyword
));
73 olist
.head
= olist
.tail
= NULL
;
76 cat
= new_object(&olist
);
77 outlines
= new_object(&olist
);
78 pages
= new_object(&olist
);
79 resources
= new_object(&olist
);
82 * The catalogue just contains references to the outlines and
85 objtext(cat
, "<<\n/Type /Catalog\n/Outlines ");
86 objref(cat
, outlines
);
87 objtext(cat
, "\n/Pages ");
89 objtext(cat
, "\n/PageMode /UseOutlines\n>>\n");
92 * Set up the resources dictionary, which mostly means
93 * providing all the font objects and names to call them by.
96 objtext(resources
, "<<\n/Font <<\n");
97 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
) {
102 sprintf(fname
, "f%d", font_index
++);
103 fe
->name
= dupstr(fname
);
105 font
= new_object(&olist
);
107 objtext(resources
, "/");
108 objtext(resources
, fe
->name
);
109 objtext(resources
, " ");
110 objref(resources
, font
);
111 objtext(resources
, "\n");
113 objtext(font
, "<<\n/Type /Font\n/Subtype /Type1\n/Name /");
114 objtext(font
, fe
->name
);
115 objtext(font
, "\n/BaseFont /");
116 objtext(font
, fe
->font
->name
);
117 objtext(font
, "\n/Encoding <<\n/Type /Encoding\n/Differences [");
119 for (i
= 0; i
< 256; i
++) {
123 sprintf(buf
, "\n%d /", i
);
125 objtext(font
, fe
->vector
[i
] ? fe
->vector
[i
] : ".notdef");
128 objtext(font
, "\n]\n>>\n");
131 object
*widths
= new_object(&olist
);
132 objtext(font
, "/FirstChar 0\n/LastChar 255\n/Widths ");
133 objref(font
, widths
);
135 objtext(widths
, "[\n");
136 for (i
= 0; i
< 256; i
++) {
139 if (fe
->indices
[i
] < 0)
142 width
= fe
->font
->widths
[fe
->indices
[i
]];
143 sprintf(buf
, "%g\n", 1000.0 * width
/ 4096.0);
144 objtext(widths
, buf
);
146 objtext(widths
, "]\n");
149 objtext(font
, ">>\n");
151 objtext(resources
, ">>\n>>\n");
154 * Define the page objects for each page, and get each one
155 * ready to have a `Parent' specification added to it.
157 for (page
= doc
->pages
; page
; page
= page
->next
) {
160 opage
= new_object(&olist
);
162 objtext(opage
, "<<\n/Type /Page\n");
166 * Recursively build the page tree.
168 make_pages_node(pages
, NULL
, doc
->pages
, NULL
, resources
);
171 * Create and render the individual pages.
174 for (page
= doc
->pages
; page
; page
= page
->next
) {
175 object
*opage
, *cstr
;
177 text_fragment
*frag
, *frag_end
;
181 opage
= (object
*)page
->spare
;
183 * At this point the page dictionary is already
184 * half-written, with /Type and /Parent already present. We
185 * continue from there.
189 * The PDF spec says /Resources is required, but also says
190 * that it's inheritable and may be omitted if it's present
191 * in a Pages node. In our case it is: it's present in the
192 * topmost /Pages node because we carefully put it there.
193 * So we don't need a /Resources entry here.
195 sprintf(buf
, "/MediaBox [0 0 %g %g]\n",
196 doc
->paper_width
/ 4096.0, doc
->paper_height
/ 4096.0);
200 * Now we're ready to define a content stream containing
201 * the actual text on the page.
203 cstr
= new_object(&olist
);
204 objtext(opage
, "/Contents ");
206 objtext(opage
, "\n");
209 * Render any rectangles on the page.
211 for (r
= page
->first_rect
; r
; r
= r
->next
) {
213 sprintf(buf
, "%g %g %g %g re f\n", r
->x
/ 4096.0,
214 r
->y
/ 4096.0, r
->w
/ 4096.0, r
->h
/ 4096.0);
215 objstream(cstr
, buf
);
218 objstream(cstr
, "BT\n");
221 * PDF tracks two separate current positions: the position
222 * given in the `line matrix' and the position given in the
223 * `text matrix'. We must therefore track both as well.
224 * They start off at -1 (unset).
229 frag
= page
->first_text
;
232 * For compactness, I'm going to group text fragments
233 * into subsequences that use the same font+size. So
234 * first find the end of this subsequence.
236 for (frag_end
= frag
;
238 frag_end
->fe
== frag
->fe
&&
239 frag_end
->fontsize
== frag
->fontsize
);
240 frag_end
= frag_end
->next
);
243 * Now select the text fragment, and prepare to display
246 objstream(cstr
, "/");
247 objstream(cstr
, frag
->fe
->name
);
248 sprintf(buf
, " %d Tf ", frag
->fontsize
);
249 objstream(cstr
, buf
);
251 while (frag
&& frag
!= frag_end
) {
253 * Place the text position for the first piece of
257 sprintf(buf
, "1 0 0 1 %g %g Tm ",
258 frag
->x
/4096.0, frag
->y
/4096.0);
260 sprintf(buf
, "%g %g Td ",
261 (frag
->x
- lx
)/4096.0, (frag
->y
- ly
)/4096.0);
263 objstream(cstr
, buf
);
268 * See if we're going to use Tj (show a single
269 * string) or TJ (show an array of strings with
270 * x-spacings between them). We determine this by
271 * seeing if there's more than one text fragment in
272 * sequence with the same y-coordinate.
274 if (frag
->next
&& frag
->next
!= frag_end
&&
275 frag
->next
->y
== y
) {
279 objstream(cstr
, "[");
280 while (frag
&& frag
!= frag_end
&& frag
->y
== y
) {
283 (x
- frag
->x
) * 1000.0 /
284 (4096.0 * frag
->fontsize
));
285 objstream(cstr
, buf
);
287 pdf_string(objstream
, cstr
, frag
->text
);
288 x
= frag
->x
+ frag
->width
;
291 objstream(cstr
, "]TJ\n");
297 pdf_string(objstream
, cstr
, frag
->text
);
298 objstream(cstr
, "Tj\n");
303 objstream(cstr
, "ET");
306 * Also, we want an annotation dictionary containing the
307 * cross-references from this page.
309 if (page
->first_xref
) {
311 objtext(opage
, "/Annots [\n");
313 for (xr
= page
->first_xref
; xr
; xr
= xr
->next
) {
317 annot
= new_object(&olist
);
318 objref(opage
, annot
);
319 objtext(opage
, "\n");
321 objtext(annot
, "<<\n/Type /Annot\n/Subtype /Link\n/Rect [");
322 sprintf(buf
, "%g %g %g %g",
323 xr
->lx
/ 4096.0, xr
->by
/ 4096.0,
324 xr
->rx
/ 4096.0, xr
->ty
/ 4096.0);
326 objtext(annot
, "]\n/Border [0 0 0]\n");
328 if (xr
->dest
.type
== PAGE
) {
329 objtext(annot
, "/Dest [");
330 objref(annot
, (object
*)xr
->dest
.page
->spare
);
331 objtext(annot
, " /XYZ null null null]\n");
333 objtext(annot
, "/A <<\n/Type /Action\n/S /URI\n/URI ");
334 pdf_string(objtext
, annot
, xr
->dest
.url
);
335 objtext(annot
, "\n>>\n");
338 objtext(annot
, ">>\n");
341 objtext(opage
, "]\n");
344 objtext(opage
, ">>\n");
348 * Set up the outlines dictionary.
354 objtext(outlines
, "<<\n/Type /Outlines\n");
355 topcount
= make_outline(outlines
, doc
->outline_elements
,
356 doc
->n_outline_elements
, TRUE
);
357 sprintf(buf
, "/Count %d\n>>\n", topcount
);
358 objtext(outlines
, buf
);
362 * Assemble the final linear form of every object.
364 for (o
= olist
.head
; o
; o
= o
->next
) {
365 rdstringc rs
= {0, 0, NULL
};
368 sprintf(text
, "%d 0 obj\n", o
->number
);
371 if (!o
->main
.text
&& o
->stream
.text
) {
372 sprintf(text
, "<<\n/Length %d\n>>\n", o
->stream
.pos
);
373 rdaddsc(&o
->main
, text
);
376 assert(o
->main
.text
);
377 rdaddsc(&rs
, o
->main
.text
);
380 if (rs
.text
[rs
.pos
-1] != '\n')
383 if (o
->stream
.text
) {
385 * FIXME: If we ever start compressing stream data then
386 * it will have zero bytes in it, so we'll have to be
387 * more careful than this.
389 rdaddsc(&rs
, "stream\n");
390 rdaddsc(&rs
, o
->stream
.text
);
391 rdaddsc(&rs
, "\nendstream\n");
392 sfree(o
->stream
.text
);
395 rdaddsc(&rs
, "endobj\n");
402 * Write out the PDF file.
405 fp
= fopen(filename
, "wb");
407 error(err_cantopenw
, filename
);
412 * Header. I'm going to put the version IDs in the header as
413 * well, simply in PDF comments.
415 fileoff
= fprintf(fp
, "%%PDF-1.3\n");
416 for (p
= sourceform
; p
; p
= p
->next
)
417 if (p
->type
== para_VersionID
)
418 fileoff
+= pdf_versionid(fp
, p
->words
);
423 for (o
= olist
.head
; o
; o
= o
->next
) {
424 o
->fileoff
= fileoff
;
425 fwrite(o
->final
, 1, o
->size
, fp
);
430 * Cross-reference table
432 fprintf(fp
, "xref\n");
433 assert(olist
.head
->number
== 1);
434 fprintf(fp
, "0 %d\n", olist
.tail
->number
+ 1);
435 fprintf(fp
, "0000000000 65535 f \n");
436 for (o
= olist
.head
; o
; o
= o
->next
) {
438 sprintf(entry
, "%010d 00000 n \n", o
->fileoff
);
439 assert(strlen(entry
) == 20);
446 fprintf(fp
, "trailer\n<<\n/Size %d\n/Root %d 0 R\n>>\n",
447 olist
.tail
->number
+ 1, cat
->number
);
448 fprintf(fp
, "startxref\n%d\n%%%%EOF\n", fileoff
);
455 static object
*new_object(objlist
*list
)
457 object
*obj
= mknew(object
);
461 obj
->main
.text
= NULL
;
462 obj
->main
.pos
= obj
->main
.size
= 0;
463 obj
->stream
.text
= NULL
;
464 obj
->stream
.pos
= obj
->stream
.size
= 0;
466 obj
->number
= list
->number
++;
470 list
->tail
->next
= obj
;
481 static void objtext(object
*o
, char const *text
)
483 rdaddsc(&o
->main
, text
);
486 static void objstream(object
*o
, char const *text
)
488 rdaddsc(&o
->stream
, text
);
491 static void objref(object
*o
, object
*dest
)
494 sprintf(buf
, "%d 0 R", dest
->number
);
495 rdaddsc(&o
->main
, buf
);
498 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
499 page_data
*last
, object
*resources
)
505 objtext(node
, "<<\n/Type /Pages\n");
507 objtext(node
, "/Parent ");
508 objref(node
, parent
);
513 * Count the pages in this stretch, to see if there are few
514 * enough to reference directly.
517 for (page
= first
; page
; page
= page
->next
) {
523 sprintf(buf
, "/Count %d\n/Kids [\n", count
);
526 if (count
> TREE_BRANCH
) {
528 page_data
*thisfirst
, *thislast
;
532 for (i
= 0; i
< TREE_BRANCH
; i
++) {
533 int number
= (i
+1) * count
/ TREE_BRANCH
- i
* count
/ TREE_BRANCH
;
540 if (thisfirst
== thislast
) {
541 objref(node
, (object
*)thisfirst
->spare
);
542 objtext((object
*)thisfirst
->spare
, "/Parent ");
543 objref((object
*)thisfirst
->spare
, node
);
544 objtext((object
*)thisfirst
->spare
, "\n");
546 object
*newnode
= new_object(node
->list
);
547 make_pages_node(newnode
, node
, thisfirst
, thislast
, NULL
);
548 objref(node
, newnode
);
553 assert(thislast
== last
|| page
== NULL
);
556 for (page
= first
; page
; page
= page
->next
) {
557 objref(node
, (object
*)page
->spare
);
559 objtext((object
*)page
->spare
, "/Parent ");
560 objref((object
*)page
->spare
, node
);
561 objtext((object
*)page
->spare
, "\n");
567 objtext(node
, "]\n");
570 objtext(node
, "/Resources ");
571 objref(node
, resources
);
575 objtext(node
, ">>\n");
579 * In text on the page, PDF uses the PostScript font model, which
580 * means that glyphs are identified by PS strings and hence font
581 * encoding can be managed independently of the supplied encoding
582 * of the font. However, in the document outline, the PDF spec
583 * simply asks for ordinary text strings without mentioning what
584 * character set they are supposed to be interpreted in.
586 * Therefore, for the moment, I'm going to assume they're US-ASCII
587 * only. If anyone knows better, they should let me know :-/
589 static int pdf_convert(wchar_t *s
, char **result
) {
590 int doing
= (result
!= 0);
593 int plen
= 0, psize
= 0;
599 if (c
>= 32 && c
<= 126) {
603 /* Char is not OK. */
605 outc
= 0xBF; /* approximate the good old DEC `uh?' */
610 p
= resize(p
, psize
);
616 p
= resize(p
, plen
+1);
623 static int make_outline(object
*parent
, outline_element
*items
, int n
,
626 int level
, totalcount
= 0;
627 outline_element
*itemp
;
628 object
*curr
, *prev
= NULL
, *first
= NULL
, *last
= NULL
;
632 level
= items
->level
;
638 * Here we expect to be sitting on an item at the given
639 * level. So we start by constructing an outline entry for
642 assert(items
->level
== level
);
644 pdf_convert(items
->pdata
->outline_title
, &title
);
647 curr
= new_object(parent
->list
);
648 if (!first
) first
= curr
;
650 objtext(curr
, "<<\n/Title ");
651 pdf_string(objtext
, curr
, title
);
652 objtext(curr
, "\n/Parent ");
653 objref(curr
, parent
);
654 objtext(curr
, "\n/Dest [");
655 objref(curr
, (object
*)items
->pdata
->first
->page
->spare
);
656 objtext(curr
, " /XYZ null null null]\n");
658 objtext(curr
, "/Prev ");
662 objtext(prev
, "/Next ");
664 objtext(prev
, "\n>>\n");
669 for (itemp
= items
; itemp
< items
+n
&& itemp
->level
> level
;
674 int count
= make_outline(curr
, items
, itemp
- items
, FALSE
);
679 sprintf(buf
, "/Count %d\n", count
);
686 objtext(prev
, ">>\n");
688 assert(first
&& last
);
689 objtext(parent
, "/First ");
690 objref(parent
, first
);
691 objtext(parent
, "\n/Last ");
692 objref(parent
, last
);
693 objtext(parent
, "\n");
698 static int pdf_versionid(FILE *fp
, word
*words
)
702 ret
= fprintf(fp
, "%% ");
704 for (; words
; words
= words
->next
) {
708 switch (words
->type
) {
718 type
= removeattr(words
->type
);
722 text
= utoa_dup(words
->text
, CS_ASCII
);
724 case word_WhiteSpace
:
737 ret
+= fprintf(fp
, "\n");
742 static void pdf_string(void (*add
)(object
*, char const *),
743 object
*o
, char const *str
)
748 for (p
= str
; *p
; p
++) {
750 if (*p
== '\\' || *p
== '(' || *p
== ')')