2 * PDF backend for Halibut
9 #define TREE_BRANCH 2 /* max branching factor in page tree */
11 paragraph
*pdf_config_filename(char *filename
)
14 wchar_t *ufilename
, *up
;
18 memset(p
, 0, sizeof(*p
));
19 p
->type
= para_Config
;
21 p
->fpos
.filename
= "<command line>";
22 p
->fpos
.line
= p
->fpos
.col
= -1;
24 ufilename
= ufroma_dup(filename
);
25 len
= ustrlen(ufilename
) + 2 + lenof(L
"pdf-filename");
26 p
->keyword
= mknewa(wchar_t, len
);
28 ustrcpy(up
, L
"pdf-filename");
30 ustrcpy(up
, ufilename
);
33 assert(up
- p
->keyword
< len
);
39 typedef struct object_Tag object
;
40 typedef struct objlist_Tag objlist
;
46 rdstringc main
, stream
;
56 static object
*new_object(objlist
*list
);
57 static void objtext(object
*o
, char const *text
);
58 static void objstream(object
*o
, char const *text
);
59 static void objref(object
*o
, object
*dest
);
61 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
62 page_data
*last
, object
*resources
);
63 static int make_outline(object
*parent
, outline_element
*start
, int n
,
65 static int pdf_versionid(FILE *fp
, word
*words
);
67 void pdf_backend(paragraph
*sourceform
, keywordlist
*keywords
,
68 indexdata
*idx
, void *vdoc
) {
69 document
*doc
= (document
*)vdoc
;
78 object
*o
, *cat
, *outlines
, *pages
, *resources
;
84 filename
= dupstr("output.pdf");
85 for (p
= sourceform
; p
; p
= p
->next
) {
86 if (p
->type
== para_Config
&& p
->parent
) {
87 if (!ustricmp(p
->keyword
, L
"pdf-filename")) {
89 filename
= utoa_dup(uadv(p
->keyword
));
94 olist
.head
= olist
.tail
= NULL
;
97 cat
= new_object(&olist
);
98 outlines
= new_object(&olist
);
99 pages
= new_object(&olist
);
100 resources
= new_object(&olist
);
103 * The catalogue just contains references to the outlines and
106 objtext(cat
, "<<\n/Type /Catalog\n/Outlines ");
107 objref(cat
, outlines
);
108 objtext(cat
, "\n/Pages ");
110 objtext(cat
, "\n/PageMode /UseOutlines\n>>\n");
113 * Set up the resources dictionary, which mostly means
114 * providing all the font objects and names to call them by.
117 objtext(resources
, "<<\n/Font <<\n");
118 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
) {
123 sprintf(fname
, "f%d", font_index
++);
124 fe
->name
= dupstr(fname
);
126 font
= new_object(&olist
);
128 objtext(resources
, "/");
129 objtext(resources
, fe
->name
);
130 objtext(resources
, " ");
131 objref(resources
, font
);
132 objtext(resources
, "\n");
134 objtext(font
, "<<\n/Type /Font\n/Subtype /Type1\n/Name /");
135 objtext(font
, fe
->name
);
136 objtext(font
, "\n/BaseFont /");
137 objtext(font
, fe
->font
->name
);
138 objtext(font
, "\n/Encoding <<\n/Type /Encoding\n/Differences [");
140 for (i
= 0; i
< 256; i
++) {
144 sprintf(buf
, "\n%d /", i
);
146 objtext(font
, fe
->vector
[i
] ? fe
->vector
[i
] : ".notdef");
149 objtext(font
, "\n]\n>>\n");
152 object
*widths
= new_object(&olist
);
153 objtext(font
, "/FirstChar 0\n/LastChar 255\n/Widths ");
154 objref(font
, widths
);
156 objtext(widths
, "[\n");
157 for (i
= 0; i
< 256; i
++) {
160 if (fe
->indices
[i
] < 0)
163 width
= fe
->font
->widths
[fe
->indices
[i
]];
164 sprintf(buf
, "%g\n", 1000.0 * width
/ 4096.0);
165 objtext(widths
, buf
);
167 objtext(widths
, "]\n");
170 objtext(font
, ">>\n");
172 objtext(resources
, ">>\n>>\n");
175 * Define the page objects for each page, and get each one
176 * ready to have a `Parent' specification added to it.
178 for (page
= doc
->pages
; page
; page
= page
->next
) {
181 opage
= new_object(&olist
);
183 objtext(opage
, "<<\n/Type /Page\n");
187 * Recursively build the page tree.
189 make_pages_node(pages
, NULL
, doc
->pages
, NULL
, resources
);
192 * Create and render the individual pages.
195 for (page
= doc
->pages
; page
; page
= page
->next
) {
196 object
*opage
, *cstr
;
201 opage
= (object
*)page
->spare
;
203 * At this point the page dictionary is already
204 * half-written, with /Type and /Parent already present. We
205 * continue from there.
209 * The PDF spec says /Resources is required, but also says
210 * that it's inheritable and may be omitted if it's present
211 * in a Pages node. In our case it is: it's present in the
212 * topmost /Pages node because we carefully put it there.
213 * So we don't need a /Resources entry here.
215 sprintf(buf
, "/MediaBox [0 0 %g %g]\n",
216 doc
->paper_width
/ 4096.0, doc
->paper_height
/ 4096.0);
220 * Now we're ready to define a content stream containing
221 * the actual text on the page.
223 cstr
= new_object(&olist
);
224 objtext(opage
, "/Contents ");
226 objtext(opage
, "\n");
229 * Render any rectangles on the page.
231 for (r
= page
->first_rect
; r
; r
= r
->next
) {
233 sprintf(buf
, "%g %g %g %g re f\n", r
->x
/ 4096.0,
234 r
->y
/ 4096.0, r
->w
/ 4096.0, r
->h
/ 4096.0);
235 objstream(cstr
, buf
);
238 objstream(cstr
, "BT\n");
239 for (frag
= page
->first_text
; frag
; frag
= frag
->next
) {
242 objstream(cstr
, "/");
243 objstream(cstr
, frag
->fe
->name
);
244 sprintf(buf
, " %d Tf 1 0 0 1 %g %g Tm (", frag
->fontsize
,
245 frag
->x
/4096.0, frag
->y
/4096.0);
246 objstream(cstr
, buf
);
248 for (c
= frag
->text
; *c
; c
++) {
249 if (*c
== '(' || *c
== ')' || *c
== '\\')
250 objstream(cstr
, "\\");
253 objstream(cstr
, buf
);
256 objstream(cstr
, ") Tj\n");
258 objstream(cstr
, "ET");
261 * Also, we want an annotation dictionary containing the
262 * cross-references from this page.
264 if (page
->first_xref
) {
266 objtext(opage
, "/Annots [\n");
268 for (xr
= page
->first_xref
; xr
; xr
= xr
->next
) {
272 annot
= new_object(&olist
);
273 objref(opage
, annot
);
274 objtext(opage
, "\n");
276 objtext(annot
, "<<\n/Type /Annot\n/Subtype /Link\n/Rect [");
277 sprintf(buf
, "%g %g %g %g",
278 xr
->lx
/ 4096.0, xr
->by
/ 4096.0,
279 xr
->rx
/ 4096.0, xr
->ty
/ 4096.0);
281 objtext(annot
, "]\n/Border [0 0 0]\n");
283 if (xr
->dest
.type
== PAGE
) {
284 objtext(annot
, "/Dest [");
285 objref(annot
, (object
*)xr
->dest
.page
->spare
);
286 objtext(annot
, " /XYZ null null null]\n");
290 objtext(annot
, "/A <<\n/Type /Action\n/S /URI\n/URI (");
291 for (p
= xr
->dest
.url
; *p
; p
++) {
295 if (*p
== '(' || *p
== ')' || *p
== '\\')
296 objtext(annot
, "\\");
299 objtext(annot
, ")\n>>\n");
302 objtext(annot
, ">>\n");
305 objtext(opage
, "]\n");
308 objtext(opage
, ">>\n");
312 * Set up the outlines dictionary.
318 objtext(outlines
, "<<\n/Type /Outlines\n");
319 topcount
= make_outline(outlines
, doc
->outline_elements
,
320 doc
->n_outline_elements
, TRUE
);
321 sprintf(buf
, "/Count %d\n>>\n", topcount
);
322 objtext(outlines
, buf
);
326 * Assemble the final linear form of every object.
328 for (o
= olist
.head
; o
; o
= o
->next
) {
329 rdstringc rs
= {0, 0, NULL
};
332 sprintf(text
, "%d 0 obj\n", o
->number
);
335 if (!o
->main
.text
&& o
->stream
.text
) {
336 sprintf(text
, "<<\n/Length %d\n>>\n", o
->stream
.pos
);
337 rdaddsc(&o
->main
, text
);
340 assert(o
->main
.text
);
341 rdaddsc(&rs
, o
->main
.text
);
344 if (rs
.text
[rs
.pos
-1] != '\n')
347 if (o
->stream
.text
) {
349 * FIXME: If we ever start compressing stream data then
350 * it will have zero bytes in it, so we'll have to be
351 * more careful than this.
353 rdaddsc(&rs
, "stream\n");
354 rdaddsc(&rs
, o
->stream
.text
);
355 rdaddsc(&rs
, "\nendstream\n");
356 sfree(o
->stream
.text
);
359 rdaddsc(&rs
, "endobj\n");
366 * Write out the PDF file.
369 fp
= fopen(filename
, "wb");
371 error(err_cantopenw
, filename
);
376 * Header. I'm going to put the version IDs in the header as
377 * well, simply in PDF comments.
379 fileoff
= fprintf(fp
, "%%PDF-1.3\n");
380 for (p
= sourceform
; p
; p
= p
->next
)
381 if (p
->type
== para_VersionID
)
382 fileoff
+= pdf_versionid(fp
, p
->words
);
387 for (o
= olist
.head
; o
; o
= o
->next
) {
388 o
->fileoff
= fileoff
;
389 fwrite(o
->final
, 1, o
->size
, fp
);
394 * Cross-reference table
396 fprintf(fp
, "xref\n");
397 assert(olist
.head
->number
== 1);
398 fprintf(fp
, "0 %d\n", olist
.tail
->number
+ 1);
399 fprintf(fp
, "0000000000 65535 f \n");
400 for (o
= olist
.head
; o
; o
= o
->next
) {
402 sprintf(entry
, "%010d 00000 n \n", o
->fileoff
);
403 assert(strlen(entry
) == 20);
410 fprintf(fp
, "trailer\n<<\n/Size %d\n/Root %d 0 R\n>>\n",
411 olist
.tail
->number
+ 1, cat
->number
);
412 fprintf(fp
, "startxref\n%d\n%%%%EOF\n", fileoff
);
419 static object
*new_object(objlist
*list
)
421 object
*obj
= mknew(object
);
425 obj
->main
.text
= NULL
;
426 obj
->main
.pos
= obj
->main
.size
= 0;
427 obj
->stream
.text
= NULL
;
428 obj
->stream
.pos
= obj
->stream
.size
= 0;
430 obj
->number
= list
->number
++;
434 list
->tail
->next
= obj
;
445 static void objtext(object
*o
, char const *text
)
447 rdaddsc(&o
->main
, text
);
450 static void objstream(object
*o
, char const *text
)
452 rdaddsc(&o
->stream
, text
);
455 static void objref(object
*o
, object
*dest
)
458 sprintf(buf
, "%d 0 R", dest
->number
);
459 rdaddsc(&o
->main
, buf
);
462 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
463 page_data
*last
, object
*resources
)
469 objtext(node
, "<<\n/Type /Pages\n");
471 objtext(node
, "/Parent ");
472 objref(node
, parent
);
477 * Count the pages in this stretch, to see if there are few
478 * enough to reference directly.
481 for (page
= first
; page
; page
= page
->next
) {
487 sprintf(buf
, "/Count %d\n/Kids [\n", count
);
490 if (count
> TREE_BRANCH
) {
492 page_data
*thisfirst
, *thislast
;
496 for (i
= 0; i
< TREE_BRANCH
; i
++) {
497 int number
= (i
+1) * count
/ TREE_BRANCH
- i
* count
/ TREE_BRANCH
;
504 if (thisfirst
== thislast
) {
505 objref(node
, (object
*)thisfirst
->spare
);
506 objtext((object
*)thisfirst
->spare
, "/Parent ");
507 objref((object
*)thisfirst
->spare
, node
);
508 objtext((object
*)thisfirst
->spare
, "\n");
510 object
*newnode
= new_object(node
->list
);
511 make_pages_node(newnode
, node
, thisfirst
, thislast
, NULL
);
512 objref(node
, newnode
);
517 assert(thislast
== last
|| page
== NULL
);
520 for (page
= first
; page
; page
= page
->next
) {
521 objref(node
, (object
*)page
->spare
);
523 objtext((object
*)page
->spare
, "/Parent ");
524 objref((object
*)page
->spare
, node
);
525 objtext((object
*)page
->spare
, "\n");
531 objtext(node
, "]\n");
534 objtext(node
, "/Resources ");
535 objref(node
, resources
);
539 objtext(node
, ">>\n");
543 * In text on the page, PDF uses the PostScript font model, which
544 * means that glyphs are identified by PS strings and hence font
545 * encoding can be managed independently of the supplied encoding
546 * of the font. However, in the document outline, the PDF spec
547 * simply asks for ordinary text strings without mentioning what
548 * character set they are supposed to be interpreted in.
550 * Therefore, for the moment, I'm going to assume they're US-ASCII
551 * only. If anyone knows better, they should let me know :-/
553 static int pdf_convert(wchar_t *s
, char **result
) {
554 int doing
= (result
!= 0);
557 int plen
= 0, psize
= 0;
563 if (c
>= 32 && c
<= 126) {
567 /* Char is not OK. */
569 outc
= 0xBF; /* approximate the good old DEC `uh?' */
574 p
= resize(p
, psize
);
580 p
= resize(p
, plen
+1);
587 static int make_outline(object
*parent
, outline_element
*items
, int n
,
590 int level
, totalcount
= 0;
591 outline_element
*itemp
;
592 object
*curr
, *prev
= NULL
, *first
= NULL
, *last
= NULL
;
596 level
= items
->level
;
602 * Here we expect to be sitting on an item at the given
603 * level. So we start by constructing an outline entry for
606 assert(items
->level
== level
);
608 pdf_convert(items
->pdata
->outline_title
, &title
);
611 curr
= new_object(parent
->list
);
612 if (!first
) first
= curr
;
614 objtext(curr
, "<<\n/Title (");
615 for (p
= title
; *p
; p
++) {
617 if (*p
== '\\' || *p
== '(' || *p
== ')')
623 objtext(curr
, ")\n/Parent ");
624 objref(curr
, parent
);
625 objtext(curr
, "\n/Dest [");
626 objref(curr
, (object
*)items
->pdata
->first
->page
->spare
);
627 objtext(curr
, " /XYZ null null null]\n");
629 objtext(curr
, "/Prev ");
633 objtext(prev
, "/Next ");
635 objtext(prev
, "\n>>\n");
640 for (itemp
= items
; itemp
< items
+n
&& itemp
->level
> level
;
645 int count
= make_outline(curr
, items
, itemp
- items
, FALSE
);
650 sprintf(buf
, "/Count %d\n", count
);
657 objtext(prev
, ">>\n");
659 assert(first
&& last
);
660 objtext(parent
, "/First ");
661 objref(parent
, first
);
662 objtext(parent
, "\n/Last ");
663 objref(parent
, last
);
664 objtext(parent
, "\n");
669 static int pdf_versionid(FILE *fp
, word
*words
)
673 ret
= fprintf(fp
, "%% ");
675 for (; words
; words
= words
->next
) {
679 switch (words
->type
) {
689 type
= removeattr(words
->type
);
693 text
= utoa_dup(words
->text
);
695 case word_WhiteSpace
:
708 ret
+= fprintf(fp
, "\n");