2 * PDF backend for Halibut
10 #define TREE_BRANCH 8 /* max branching factor in page tree */
12 paragraph
*pdf_config_filename(char *filename
)
14 return cmdline_cfg_simple("pdf-filename", filename
, NULL
);
17 typedef struct object_Tag object
;
18 typedef struct objlist_Tag objlist
;
24 rdstringc main
, stream
;
34 static object
*new_object(objlist
*list
);
35 static void objtext(object
*o
, char const *text
);
36 static void objstream(object
*o
, char const *text
);
37 static void objstream_len(object
*o
, char const *text
, size_t len
);
38 static void pdf_string(void (*add
)(object
*, char const *),
39 object
*, char const *);
40 static void pdf_string_len(void (*add
)(object
*, char const *),
41 object
*, char const *, int);
42 static void objref(object
*o
, object
*dest
);
43 static void objdest(object
*o
, page_data
*p
);
45 static int is_std_font(char const *name
);
47 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
48 page_data
*last
, object
*resources
,
50 static int make_outline(object
*parent
, outline_element
*start
, int n
,
52 static int pdf_versionid(FILE *fp
, word
*words
);
54 void pdf_backend(paragraph
*sourceform
, keywordlist
*keywords
,
55 indexdata
*idx
, void *vdoc
) {
56 document
*doc
= (document
*)vdoc
;
65 object
*o
, *info
, *cat
, *outlines
, *pages
, *resources
, *mediabox
;
71 filename
= dupstr("output.pdf");
72 for (p
= sourceform
; p
; p
= p
->next
) {
73 if (p
->type
== para_Config
) {
74 if (!ustricmp(p
->keyword
, L
"pdf-filename")) {
76 filename
= dupstr(adv(p
->origkeyword
));
81 olist
.head
= olist
.tail
= NULL
;
87 info
= new_object(&olist
);
88 objtext(info
, "<<\n");
89 if (doc
->n_outline_elements
> 0) {
94 pdf_outline_convert(doc
->outline_elements
->pdata
->outline_title
,
96 objtext(info
, "/Title ");
97 pdf_string_len(objtext
, info
, title
, titlelen
);
101 objtext(info
, "/Producer ");
102 sprintf(buf
, "Halibut, %s", version
);
103 pdf_string(objtext
, info
, buf
);
104 objtext(info
, "\n>>\n");
107 cat
= new_object(&olist
);
108 if (doc
->n_outline_elements
> 0)
109 outlines
= new_object(&olist
);
112 pages
= new_object(&olist
);
113 resources
= new_object(&olist
);
116 * The catalogue just contains references to the outlines and
117 * pages objects, and the pagelabels dictionary.
119 objtext(cat
, "<<\n/Type /Catalog");
121 objtext(cat
, "\n/Outlines ");
122 objref(cat
, outlines
);
124 objtext(cat
, "\n/Pages ");
126 /* Halibut just numbers pages 1, 2, 3, ... */
127 objtext(cat
, "\n/PageLabels<</Nums[0<</S/D>>]>>");
129 objtext(cat
, "\n/PageMode /UseOutlines");
130 objtext(cat
, "\n>>\n");
133 * Set up the resources dictionary, which mostly means
134 * providing all the font objects and names to call them by.
137 objtext(resources
, "<<\n/ProcSet [/PDF/Text]\n/Font <<\n");
138 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
) {
143 sprintf(fname
, "f%d", font_index
++);
144 fe
->name
= dupstr(fname
);
146 font
= new_object(&olist
);
148 objtext(resources
, "/");
149 objtext(resources
, fe
->name
);
150 objtext(resources
, " ");
151 objref(resources
, font
);
152 objtext(resources
, "\n");
154 objtext(font
, "<<\n/Type /Font\n/Subtype /Type1\n/Name /");
155 objtext(font
, fe
->name
);
156 objtext(font
, "\n/BaseFont /");
157 objtext(font
, fe
->font
->info
->name
);
158 objtext(font
, "\n/Encoding <<\n/Type /Encoding\n/Differences [");
160 for (i
= 0; i
< 256; i
++) {
162 if (fe
->vector
[i
] == NOGLYPH
)
165 sprintf(buf
, "\n%d", i
);
168 objtext(font
, i
% 8 ?
"/" : "\n/");
169 objtext(font
, glyph_extern(fe
->vector
[i
]));
173 objtext(font
, "\n]\n>>\n");
175 #define FF_FIXEDPITCH 0x00000001
176 #define FF_SERIF 0x00000002
177 #define FF_SYMBOLIC 0x00000004
178 #define FF_SCRIPT 0x00000008
179 #define FF_NONSYMBOLIC 0x00000020
180 #define FF_ITALIC 0x00000040
181 #define FF_ALLCAP 0x00010000
182 #define FF_SMALLCAP 0x00020000
183 #define FF_FORCEBOLD 0x00040000
185 if (!is_std_font(fe
->font
->info
->name
)){
186 object
*widths
= new_object(&olist
);
187 object
*fontdesc
= new_object(&olist
);
188 int firstchar
= -1, lastchar
= -1;
190 font_info
const *fi
= fe
->font
->info
;
192 for (i
= 0; i
< 256; i
++)
193 if (fe
->vector
[i
] != NOGLYPH
) {
194 if (firstchar
< 0) firstchar
= i
;
197 sprintf(buf
, "/FirstChar %d\n/LastChar %d\n/Widths ",
198 firstchar
, lastchar
);
200 objref(font
, widths
);
202 objtext(widths
, "[\n");
203 for (i
= firstchar
; i
<= lastchar
; i
++) {
205 if (fe
->vector
[i
] == NOGLYPH
)
208 width
= find_width(fe
->font
, fe
->vector
[i
]);
209 sprintf(buf
, "%g\n", 1000.0 * width
/ FUNITS_PER_PT
);
210 objtext(widths
, buf
);
212 objtext(widths
, "]\n");
213 objtext(font
, "/FontDescriptor ");
214 objref(font
, fontdesc
);
215 objtext(fontdesc
, "<<\n/Type /FontDescriptor\n/Name /");
216 objtext(fontdesc
, fi
->name
);
218 if (fi
->italicangle
) flags
|= FF_ITALIC
;
219 flags
|= FF_NONSYMBOLIC
;
220 sprintf(buf
, "\n/Flags %d\n", flags
);
221 objtext(fontdesc
, buf
);
222 sprintf(buf
, "/FontBBox [%g %g %g %g]\n", fi
->fontbbox
[0],
223 fi
->fontbbox
[1], fi
->fontbbox
[2], fi
->fontbbox
[3]);
224 objtext(fontdesc
, buf
);
225 sprintf(buf
, "/ItalicAngle %g\n", fi
->italicangle
);
226 objtext(fontdesc
, buf
);
227 sprintf(buf
, "/Ascent %g\n", fi
->ascent
);
228 objtext(fontdesc
, buf
);
229 sprintf(buf
, "/Descent %g\n", fi
->descent
);
230 objtext(fontdesc
, buf
);
231 sprintf(buf
, "/CapHeight %g\n", fi
->capheight
);
232 objtext(fontdesc
, buf
);
233 sprintf(buf
, "/XHeight %g\n", fi
->xheight
);
234 objtext(fontdesc
, buf
);
235 sprintf(buf
, "/StemH %g\n", fi
->stemh
);
236 objtext(fontdesc
, buf
);
237 sprintf(buf
, "/StemV %g\n", fi
->stemv
);
238 objtext(fontdesc
, buf
);
240 object
*fontfile
= new_object(&olist
);
244 pf_part1((font_info
*)fi
, &ffbuf
, &len
);
245 objstream_len(fontfile
, ffbuf
, len
);
247 sprintf(buf
, "<<\n/Length1 %lu\n", (unsigned long)len
);
248 objtext(fontfile
, buf
);
249 pf_part2((font_info
*)fi
, &ffbuf
, &len
);
250 objstream_len(fontfile
, ffbuf
, len
);
252 sprintf(buf
, "/Length2 %lu\n", (unsigned long)len
);
253 objtext(fontfile
, buf
);
254 objtext(fontfile
, "/Length3 0\n");
255 objtext(fontdesc
, "/FontFile ");
256 objref(fontdesc
, fontfile
);
258 objtext(fontdesc
, "\n>>\n");
261 objtext(font
, "\n>>\n");
263 objtext(resources
, ">>\n>>\n");
267 mediabox
= new_object(&olist
);
268 sprintf(buf
, "[0 0 %g %g]\n",
269 doc
->paper_width
/ FUNITS_PER_PT
,
270 doc
->paper_height
/ FUNITS_PER_PT
);
271 objtext(mediabox
, buf
);
275 * Define the page objects for each page, and get each one
276 * ready to have a `Parent' specification added to it.
278 for (page
= doc
->pages
; page
; page
= page
->next
) {
281 opage
= new_object(&olist
);
283 objtext(opage
, "<<\n/Type /Page\n");
287 * Recursively build the page tree.
289 make_pages_node(pages
, NULL
, doc
->pages
, NULL
, resources
, mediabox
);
292 * Create and render the individual pages.
295 for (page
= doc
->pages
; page
; page
= page
->next
) {
296 object
*opage
, *cstr
;
298 text_fragment
*frag
, *frag_end
;
302 opage
= (object
*)page
->spare
;
304 * At this point the page dictionary is already
305 * half-written, with /Type and /Parent already present. We
306 * continue from there.
310 * The PDF spec says /Resources is required, but also says
311 * that it's inheritable and may be omitted if it's present
312 * in a Pages node. In our case it is: it's present in the
313 * topmost /Pages node because we carefully put it there.
314 * So we don't need a /Resources entry here. The same applies
319 * Now we're ready to define a content stream containing
320 * the actual text on the page.
322 cstr
= new_object(&olist
);
323 objtext(opage
, "/Contents ");
325 objtext(opage
, "\n");
328 * Render any rectangles on the page.
330 for (r
= page
->first_rect
; r
; r
= r
->next
) {
332 sprintf(buf
, "%g %g %g %g re f\n",
333 r
->x
/ FUNITS_PER_PT
, r
->y
/ FUNITS_PER_PT
,
334 r
->w
/ FUNITS_PER_PT
, r
->h
/ FUNITS_PER_PT
);
335 objstream(cstr
, buf
);
338 objstream(cstr
, "BT\n");
341 * PDF tracks two separate current positions: the position
342 * given in the `line matrix' and the position given in the
343 * `text matrix'. We must therefore track both as well.
344 * They start off at -1 (unset).
349 frag
= page
->first_text
;
352 * For compactness, I'm going to group text fragments
353 * into subsequences that use the same font+size. So
354 * first find the end of this subsequence.
356 for (frag_end
= frag
;
358 frag_end
->fe
== frag
->fe
&&
359 frag_end
->fontsize
== frag
->fontsize
);
360 frag_end
= frag_end
->next
);
363 * Now select the text fragment, and prepare to display
366 objstream(cstr
, "/");
367 objstream(cstr
, frag
->fe
->name
);
368 sprintf(buf
, " %d Tf ", frag
->fontsize
);
369 objstream(cstr
, buf
);
371 while (frag
&& frag
!= frag_end
) {
373 * Place the text position for the first piece of
377 sprintf(buf
, "1 0 0 1 %g %g Tm ",
378 frag
->x
/FUNITS_PER_PT
, frag
->y
/FUNITS_PER_PT
);
380 sprintf(buf
, "%g %g Td ",
381 (frag
->x
- lx
)/FUNITS_PER_PT
,
382 (frag
->y
- ly
)/FUNITS_PER_PT
);
384 objstream(cstr
, buf
);
389 * See if we're going to use Tj (show a single
390 * string) or TJ (show an array of strings with
391 * x-spacings between them). We determine this by
392 * seeing if there's more than one text fragment in
393 * sequence with the same y-coordinate.
395 if (frag
->next
&& frag
->next
!= frag_end
&&
396 frag
->next
->y
== y
) {
400 objstream(cstr
, "[");
401 while (frag
&& frag
!= frag_end
&& frag
->y
== y
) {
404 (x
- frag
->x
) * 1000.0 /
405 (FUNITS_PER_PT
* frag
->fontsize
));
406 objstream(cstr
, buf
);
408 pdf_string(objstream
, cstr
, frag
->text
);
409 x
= frag
->x
+ frag
->width
;
412 objstream(cstr
, "]TJ\n");
418 pdf_string(objstream
, cstr
, frag
->text
);
419 objstream(cstr
, "Tj\n");
424 objstream(cstr
, "ET");
427 * Also, we want an annotation dictionary containing the
428 * cross-references from this page.
430 if (page
->first_xref
) {
432 objtext(opage
, "/Annots [\n");
434 for (xr
= page
->first_xref
; xr
; xr
= xr
->next
) {
437 objtext(opage
, "<</Subtype/Link\n/Rect[");
438 sprintf(buf
, "%g %g %g %g",
439 xr
->lx
/ FUNITS_PER_PT
, xr
->by
/ FUNITS_PER_PT
,
440 xr
->rx
/ FUNITS_PER_PT
, xr
->ty
/ FUNITS_PER_PT
);
442 objtext(opage
, "]/Border[0 0 0]\n");
444 if (xr
->dest
.type
== PAGE
) {
445 objtext(opage
, "/Dest");
446 objdest(opage
, xr
->dest
.page
);
448 objtext(opage
, "/A<</S/URI/URI");
449 pdf_string(objtext
, opage
, xr
->dest
.url
);
450 objtext(opage
, ">>");
453 objtext(opage
, ">>\n");
456 objtext(opage
, "]\n");
459 objtext(opage
, ">>\n");
463 * Set up the outlines dictionary.
469 objtext(outlines
, "<<\n/Type /Outlines\n");
470 topcount
= make_outline(outlines
, doc
->outline_elements
,
471 doc
->n_outline_elements
, TRUE
);
472 sprintf(buf
, "/Count %d\n>>\n", topcount
);
473 objtext(outlines
, buf
);
477 * Assemble the final linear form of every object.
479 for (o
= olist
.head
; o
; o
= o
->next
) {
480 rdstringc rs
= {0, 0, NULL
};
482 deflate_compress_ctx
*zcontext
;
486 sprintf(text
, "%d 0 obj\n", o
->number
);
489 if (o
->stream
.text
) {
491 rdaddsc(&o
->main
, "<<\n");
492 #ifdef PDF_NOCOMPRESS
493 zlen
= o
->stream
.pos
;
494 zbuf
= snewn(zlen
, char);
495 memcpy(zbuf
, o
->stream
.text
, zlen
);
496 sprintf(text
, "/Length %d\n>>\n", zlen
);
498 zcontext
= deflate_compress_new(DEFLATE_TYPE_ZLIB
);
499 deflate_compress_data(zcontext
, o
->stream
.text
, o
->stream
.pos
,
500 DEFLATE_END_OF_DATA
, &zbuf
, &zlen
);
501 deflate_compress_free(zcontext
);
502 sprintf(text
, "/Filter/FlateDecode\n/Length %d\n>>\n", zlen
);
504 rdaddsc(&o
->main
, text
);
507 assert(o
->main
.text
);
508 rdaddsc(&rs
, o
->main
.text
);
511 if (rs
.text
[rs
.pos
-1] != '\n')
514 if (o
->stream
.text
) {
515 rdaddsc(&rs
, "stream\n");
516 rdaddsn(&rs
, zbuf
, zlen
);
517 rdaddsc(&rs
, "\nendstream\n");
518 sfree(o
->stream
.text
);
522 rdaddsc(&rs
, "endobj\n");
529 * Write out the PDF file.
532 fp
= fopen(filename
, "wb");
534 error(err_cantopenw
, filename
);
539 * Header. I'm going to put the version IDs in the header as
540 * well, simply in PDF comments. The PDF Reference also suggests
541 * that binary PDF files contain four top-bit-set characters in
544 fileoff
= fprintf(fp
, "%%PDF-1.3\n%% L\xc3\xba\xc3\xb0""a\n");
545 for (p
= sourceform
; p
; p
= p
->next
)
546 if (p
->type
== para_VersionID
)
547 fileoff
+= pdf_versionid(fp
, p
->words
);
552 for (o
= olist
.head
; o
; o
= o
->next
) {
553 o
->fileoff
= fileoff
;
554 fwrite(o
->final
, 1, o
->size
, fp
);
559 * Cross-reference table
561 fprintf(fp
, "xref\n");
562 assert(olist
.head
->number
== 1);
563 fprintf(fp
, "0 %d\n", olist
.tail
->number
+ 1);
564 fprintf(fp
, "0000000000 65535 f \n");
565 for (o
= olist
.head
; o
; o
= o
->next
) {
567 sprintf(entry
, "%010d 00000 n \n", o
->fileoff
);
568 assert(strlen(entry
) == 20);
575 fprintf(fp
, "trailer\n<<\n/Size %d\n/Root %d 0 R\n/Info %d 0 R\n>>\n",
576 olist
.tail
->number
+ 1, cat
->number
, info
->number
);
577 fprintf(fp
, "startxref\n%d\n%%%%EOF\n", fileoff
);
584 static object
*new_object(objlist
*list
)
586 object
*obj
= snew(object
);
590 obj
->main
.text
= NULL
;
591 obj
->main
.pos
= obj
->main
.size
= 0;
592 obj
->stream
.text
= NULL
;
593 obj
->stream
.pos
= obj
->stream
.size
= 0;
595 obj
->number
= list
->number
++;
599 list
->tail
->next
= obj
;
610 static void objtext(object
*o
, char const *text
)
612 rdaddsc(&o
->main
, text
);
615 static void objstream_len(object
*o
, char const *text
, size_t len
)
617 rdaddsn(&o
->stream
, text
, len
);
620 static void objstream(object
*o
, char const *text
)
622 rdaddsc(&o
->stream
, text
);
625 static void objref(object
*o
, object
*dest
)
628 sprintf(buf
, "%d 0 R", dest
->number
);
629 rdaddsc(&o
->main
, buf
);
632 static void objdest(object
*o
, page_data
*p
) {
634 objref(o
, (object
*)p
->spare
);
635 objtext(o
, "/XYZ null null null]");
638 static char const * const stdfonts
[] = {
639 "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",
640 "Helvetica", "Helvetica-Bold", "Helvetica-Oblique","Helvetica-BoldOblique",
641 "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique",
642 "Symbol", "ZapfDingbats"
645 static int is_std_font(char const *name
) {
647 for (i
= 0; i
< lenof(stdfonts
); i
++)
648 if (strcmp(name
, stdfonts
[i
]) == 0)
653 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
654 page_data
*last
, object
*resources
,
661 objtext(node
, "<<\n/Type /Pages\n");
663 objtext(node
, "/Parent ");
664 objref(node
, parent
);
669 * Count the pages in this stretch, to see if there are few
670 * enough to reference directly.
673 for (page
= first
; page
; page
= page
->next
) {
679 sprintf(buf
, "/Count %d\n/Kids [\n", count
);
682 if (count
> TREE_BRANCH
) {
684 page_data
*thisfirst
, *thislast
;
688 for (i
= 0; i
< TREE_BRANCH
; i
++) {
689 int number
= (i
+1) * count
/ TREE_BRANCH
- i
* count
/ TREE_BRANCH
;
696 if (thisfirst
== thislast
) {
697 objref(node
, (object
*)thisfirst
->spare
);
698 objtext((object
*)thisfirst
->spare
, "/Parent ");
699 objref((object
*)thisfirst
->spare
, node
);
700 objtext((object
*)thisfirst
->spare
, "\n");
702 object
*newnode
= new_object(node
->list
);
703 make_pages_node(newnode
, node
, thisfirst
, thislast
,
705 objref(node
, newnode
);
710 assert(thislast
== last
|| page
== NULL
);
713 for (page
= first
; page
; page
= page
->next
) {
714 objref(node
, (object
*)page
->spare
);
716 objtext((object
*)page
->spare
, "/Parent ");
717 objref((object
*)page
->spare
, node
);
718 objtext((object
*)page
->spare
, "\n");
724 objtext(node
, "]\n");
727 objtext(node
, "/Resources ");
728 objref(node
, resources
);
732 objtext(node
, "/MediaBox ");
733 objref(node
, mediabox
);
737 objtext(node
, ">>\n");
741 * In text on the page, PDF uses the PostScript font model, which
742 * means that glyphs are identified by PS strings and hence font
743 * encoding can be managed independently of the supplied encoding
744 * of the font. However, in the document outline, the PDF spec
745 * encodes in either PDFDocEncoding (a custom superset of
746 * ISO-8859-1) or UTF-16BE.
748 char *pdf_outline_convert(wchar_t *s
, int *len
) {
751 ret
= utoa_careful_dup(s
, CS_PDF
);
754 * Very silly special case: if the returned string begins with
755 * FE FF, then the PDF reader will mistake it for a UTF-16BE
756 * string. So in this case we give up on PDFDocEncoding and
757 * encode it in UTF-16 straight away.
759 if (ret
&& ret
[0] == '\xFE' && ret
[1] == '\xFF') {
765 ret
= utoa_dup_len(s
, CS_UTF16BE
, len
);
773 static int make_outline(object
*parent
, outline_element
*items
, int n
,
776 int level
, totalcount
= 0;
777 outline_element
*itemp
;
778 object
*curr
, *prev
= NULL
, *first
= NULL
, *last
= NULL
;
782 level
= items
->level
;
789 * Here we expect to be sitting on an item at the given
790 * level. So we start by constructing an outline entry for
793 assert(items
->level
== level
);
795 title
= pdf_outline_convert(items
->pdata
->outline_title
, &titlelen
);
798 curr
= new_object(parent
->list
);
799 if (!first
) first
= curr
;
801 objtext(curr
, "<<\n/Title ");
802 pdf_string_len(objtext
, curr
, title
, titlelen
);
804 objtext(curr
, "\n/Parent ");
805 objref(curr
, parent
);
806 objtext(curr
, "\n/Dest");
807 objdest(curr
, items
->pdata
->first
->page
);
810 objtext(curr
, "/Prev ");
814 objtext(prev
, "/Next ");
816 objtext(prev
, "\n>>\n");
821 for (itemp
= items
; itemp
< items
+n
&& itemp
->level
> level
;
826 int count
= make_outline(curr
, items
, itemp
- items
, FALSE
);
831 sprintf(buf
, "/Count %d\n", count
);
838 objtext(prev
, ">>\n");
840 assert(first
&& last
);
841 objtext(parent
, "/First ");
842 objref(parent
, first
);
843 objtext(parent
, "\n/Last ");
844 objref(parent
, last
);
845 objtext(parent
, "\n");
850 static int pdf_versionid(FILE *fp
, word
*words
)
854 ret
= fprintf(fp
, "%% ");
856 for (; words
; words
= words
->next
) {
860 switch (words
->type
) {
870 type
= removeattr(words
->type
);
874 text
= utoa_dup(words
->text
, CS_ASCII
);
876 case word_WhiteSpace
:
889 ret
+= fprintf(fp
, "\n");
894 static void pdf_string_len(void (*add
)(object
*, char const *),
895 object
*o
, char const *str
, int len
)
900 for (p
= str
; len
> 0; p
++, len
--) {
902 if (*p
< ' ' || *p
> '~') {
903 sprintf(c
, "\\%03o", 0xFF & (int)*p
);
906 if (*p
== '\\' || *p
== '(' || *p
== ')')
916 static void pdf_string(void (*add
)(object
*, char const *),
917 object
*o
, char const *str
)
919 pdf_string_len(add
, o
, str
, strlen(str
));