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
);
64 void pdf_backend(paragraph
*sourceform
, keywordlist
*keywords
,
65 indexdata
*idx
, void *vdoc
) {
66 document
*doc
= (document
*)vdoc
;
75 object
*o
, *cat
, *outlines
, *pages
, *resources
;
81 filename
= dupstr("output.pdf");
82 for (p
= sourceform
; p
; p
= p
->next
) {
83 p
->private_data
= NULL
;
84 if (p
->type
== para_Config
&& p
->parent
) {
85 if (!ustricmp(p
->keyword
, L
"pdf-filename")) {
87 filename
= utoa_dup(uadv(p
->keyword
));
92 olist
.head
= olist
.tail
= NULL
;
95 cat
= new_object(&olist
);
96 outlines
= new_object(&olist
);
97 pages
= new_object(&olist
);
98 resources
= new_object(&olist
);
101 * We currently don't support outlines, so here's a null
102 * outlines dictionary.
104 objtext(outlines
, "<<\n/Type Outlines\n/Count 0\n>>\n");
107 * The catalogue just contains references to the outlines and
110 objtext(cat
, "<<\n/Type /Catalog\n/Outlines ");
111 objref(cat
, outlines
);
112 objtext(cat
, "\n/Pages ");
114 objtext(cat
, "\n>>\n");
117 * Set up the resources dictionary, which mostly means
118 * providing all the font objects and names to call them by.
121 objtext(resources
, "<<\n/Font <<\n");
122 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
) {
127 sprintf(fname
, "f%d", font_index
++);
128 fe
->name
= dupstr(fname
);
130 font
= new_object(&olist
);
132 objtext(resources
, "/");
133 objtext(resources
, fe
->name
);
134 objtext(resources
, " ");
135 objref(resources
, font
);
136 objtext(resources
, "\n");
138 objtext(font
, "<<\n/Type /Font\n/Subtype /Type1\n/Name /");
139 objtext(font
, fe
->name
);
140 objtext(font
, "\n/BaseFont /");
141 objtext(font
, fe
->font
->name
);
142 objtext(font
, "\n/Encoding <<\n/Type /Encoding\n/Differences [");
144 for (i
= 0; i
< 256; i
++) {
148 sprintf(buf
, "\n%d /", i
);
150 objtext(font
, fe
->vector
[i
] ? fe
->vector
[i
] : ".notdef");
153 objtext(font
, "\n]\n>>\n");
156 object
*widths
= new_object(&olist
);
157 objtext(font
, "/FirstChar 0\n/LastChar 255\n/Widths ");
158 objref(font
, widths
);
160 objtext(widths
, "[\n");
161 for (i
= 0; i
< 256; i
++) {
164 if (fe
->indices
[i
] < 0)
167 width
= fe
->font
->widths
[fe
->indices
[i
]];
168 sprintf(buf
, "%g\n", 1000.0 * width
/ 4096.0);
169 objtext(widths
, buf
);
171 objtext(widths
, "]\n");
174 objtext(font
, ">>\n");
176 objtext(resources
, ">>\n>>\n");
179 * Define the page objects for each page, and get each one
180 * ready to have a `Parent' specification added to it.
182 for (page
= doc
->pages
; page
; page
= page
->next
) {
185 opage
= new_object(&olist
);
187 objtext(opage
, "<<\n/Type /Page\n");
191 * Recursively build the page tree.
193 make_pages_node(pages
, NULL
, doc
->pages
, NULL
, resources
);
196 * Create and render the individual pages.
199 for (page
= doc
->pages
; page
; page
= page
->next
) {
200 object
*opage
, *cstr
;
204 opage
= (object
*)page
->spare
;
206 * At this point the page dictionary is already
207 * half-written, with /Type and /Parent already present. We
208 * continue from there.
212 * The PDF spec says /Resources is required, but also says
213 * that it's inheritable and may be omitted if it's present
214 * in a Pages node. In our case it is: it's present in the
215 * topmost /Pages node because we carefully put it there.
216 * So we don't need a /Resources entry here.
218 sprintf(buf
, "/MediaBox [0 0 %g %g]\n",
219 doc
->paper_width
/ 4096.0, doc
->paper_height
/ 4096.0);
223 * Now we're ready to define a content stream containing
224 * the actual text on the page.
226 cstr
= new_object(&olist
);
227 objtext(opage
, "/Contents ");
229 objtext(opage
, "\n");
231 objstream(cstr
, "BT\n");
232 for (frag
= page
->first_text
; frag
; frag
= frag
->next
) {
235 objstream(cstr
, "/");
236 objstream(cstr
, frag
->fe
->name
);
237 sprintf(buf
, " %d Tf 1 0 0 1 %g %g Tm (", frag
->fontsize
,
238 frag
->x
/4096.0, frag
->y
/4096.0);
239 objstream(cstr
, buf
);
241 for (c
= frag
->text
; *c
; c
++) {
242 if (*c
== '(' || *c
== ')' || *c
== '\\')
243 objstream(cstr
, "\\");
246 objstream(cstr
, buf
);
249 objstream(cstr
, ") Tj\n");
251 objstream(cstr
, "ET");
254 * Also, we want an annotation dictionary containing the
255 * cross-references from this page.
257 if (page
->first_xref
) {
259 objtext(opage
, "/Annots [\n");
261 for (xr
= page
->first_xref
; xr
; xr
= xr
->next
) {
265 annot
= new_object(&olist
);
266 objref(opage
, annot
);
267 objtext(opage
, "\n");
269 objtext(annot
, "<<\n/Type /Annot\n/Subtype /Link\n/Rect [");
270 sprintf(buf
, "%g %g %g %g",
271 xr
->lx
/ 4096.0, xr
->by
/ 4096.0,
272 xr
->rx
/ 4096.0, xr
->ty
/ 4096.0);
274 objtext(annot
, "]\n/Border [0 0 0]\n");
276 if (xr
->dest
.type
== PAGE
) {
277 objtext(annot
, "/Dest [");
278 objref(annot
, (object
*)xr
->dest
.page
->spare
);
279 objtext(annot
, " /XYZ null null null]\n");
283 objtext(annot
, "/A <<\n/Type /Action\n/S /URI\n/URI (");
284 for (p
= xr
->dest
.url
; *p
; p
++) {
288 if (*p
== '(' || *p
== ')' || *p
== '\\')
289 objtext(annot
, "\\");
292 objtext(annot
, ")\n>>\n");
295 objtext(annot
, ">>\n");
298 objtext(opage
, "]\n");
301 objtext(opage
, ">>\n");
305 * Assemble the final linear form of every object.
307 for (o
= olist
.head
; o
; o
= o
->next
) {
308 rdstringc rs
= {0, 0, NULL
};
311 sprintf(text
, "%d 0 obj\n", o
->number
);
314 if (!o
->main
.text
&& o
->stream
.text
) {
315 sprintf(text
, "<<\n/Length %d\n>>\n", o
->stream
.pos
);
316 rdaddsc(&o
->main
, text
);
319 assert(o
->main
.text
);
320 rdaddsc(&rs
, o
->main
.text
);
323 if (rs
.text
[rs
.pos
-1] != '\n')
326 if (o
->stream
.text
) {
328 * FIXME: If we ever start compressing stream data then
329 * it will have zero bytes in it, so we'll have to be
330 * more careful than this.
332 rdaddsc(&rs
, "stream\n");
333 rdaddsc(&rs
, o
->stream
.text
);
334 rdaddsc(&rs
, "\nendstream\n");
335 sfree(o
->stream
.text
);
338 rdaddsc(&rs
, "endobj\n");
345 * Write out the PDF file.
348 fp
= fopen(filename
, "wb");
350 error(err_cantopenw
, filename
);
357 fileoff
= fprintf(fp
, "%%PDF-1.3\n");
362 for (o
= olist
.head
; o
; o
= o
->next
) {
363 o
->fileoff
= fileoff
;
364 fwrite(o
->final
, 1, o
->size
, fp
);
369 * Cross-reference table
371 fprintf(fp
, "xref\n");
372 assert(olist
.head
->number
== 1);
373 fprintf(fp
, "0 %d\n", olist
.tail
->number
+ 1);
374 fprintf(fp
, "0000000000 65535 f \n");
375 for (o
= olist
.head
; o
; o
= o
->next
) {
377 sprintf(entry
, "%010d 00000 n \n", o
->fileoff
);
378 assert(strlen(entry
) == 20);
385 fprintf(fp
, "trailer\n<<\n/Size %d\n/Root %d 0 R\n>>\n",
386 olist
.tail
->number
+ 1, cat
->number
);
387 fprintf(fp
, "startxref\n%d\n%%%%EOF\n", fileoff
);
394 static object
*new_object(objlist
*list
)
396 object
*obj
= mknew(object
);
400 obj
->main
.text
= NULL
;
401 obj
->main
.pos
= obj
->main
.size
= 0;
402 obj
->stream
.text
= NULL
;
403 obj
->stream
.pos
= obj
->stream
.size
= 0;
405 obj
->number
= list
->number
++;
409 list
->tail
->next
= obj
;
420 static void objtext(object
*o
, char const *text
)
422 rdaddsc(&o
->main
, text
);
425 static void objstream(object
*o
, char const *text
)
427 rdaddsc(&o
->stream
, text
);
430 static void objref(object
*o
, object
*dest
)
433 sprintf(buf
, "%d 0 R", dest
->number
);
434 rdaddsc(&o
->main
, buf
);
437 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
438 page_data
*last
, object
*resources
)
444 objtext(node
, "<<\n/Type /Pages\n");
446 objtext(node
, "/Parent ");
447 objref(node
, parent
);
452 * Count the pages in this stretch, to see if there are few
453 * enough to reference directly.
456 for (page
= first
; page
; page
= page
->next
) {
462 sprintf(buf
, "/Count %d\n/Kids [\n", count
);
465 if (count
> TREE_BRANCH
) {
467 page_data
*thisfirst
, *thislast
;
471 for (i
= 0; i
< TREE_BRANCH
; i
++) {
472 int number
= (i
+1) * count
/ TREE_BRANCH
- i
* count
/ TREE_BRANCH
;
479 if (thisfirst
== thislast
) {
480 objref(node
, (object
*)thisfirst
->spare
);
481 objtext((object
*)thisfirst
->spare
, "/Parent ");
482 objref((object
*)thisfirst
->spare
, node
);
483 objtext((object
*)thisfirst
->spare
, "\n");
485 object
*newnode
= new_object(node
->list
);
486 make_pages_node(newnode
, node
, thisfirst
, thislast
, NULL
);
487 objref(node
, newnode
);
492 assert(thislast
== last
|| page
== NULL
);
495 for (page
= first
; page
; page
= page
->next
) {
496 objref(node
, (object
*)page
->spare
);
498 objtext((object
*)page
->spare
, "/Parent ");
499 objref((object
*)page
->spare
, node
);
500 objtext((object
*)page
->spare
, "\n");
506 objtext(node
, "]\n");
509 objtext(node
, "/Resources ");
510 objref(node
, resources
);
514 objtext(node
, ">>\n");