43341922 |
1 | /* |
2 | * PDF backend for Halibut |
3 | */ |
4 | |
5 | #include <assert.h> |
6 | #include "halibut.h" |
7 | #include "paper.h" |
8 | |
9 | #define TREE_BRANCH 2 /* max branching factor in page tree */ |
10 | |
11 | paragraph *pdf_config_filename(char *filename) |
12 | { |
13 | paragraph *p; |
14 | wchar_t *ufilename, *up; |
15 | int len; |
16 | |
17 | p = mknew(paragraph); |
18 | memset(p, 0, sizeof(*p)); |
19 | p->type = para_Config; |
20 | p->next = NULL; |
21 | p->fpos.filename = "<command line>"; |
22 | p->fpos.line = p->fpos.col = -1; |
23 | |
24 | ufilename = ufroma_dup(filename); |
25 | len = ustrlen(ufilename) + 2 + lenof(L"pdf-filename"); |
26 | p->keyword = mknewa(wchar_t, len); |
27 | up = p->keyword; |
28 | ustrcpy(up, L"pdf-filename"); |
29 | up = uadv(up); |
30 | ustrcpy(up, ufilename); |
31 | up = uadv(up); |
32 | *up = L'\0'; |
33 | assert(up - p->keyword < len); |
34 | sfree(ufilename); |
35 | |
36 | return p; |
37 | } |
38 | |
39 | typedef struct object_Tag object; |
40 | typedef struct objlist_Tag objlist; |
41 | |
42 | struct object_Tag { |
43 | objlist *list; |
44 | object *next; |
45 | int number; |
46 | rdstringc main, stream; |
47 | int size, fileoff; |
48 | char *final; |
49 | }; |
50 | |
51 | struct objlist_Tag { |
52 | int number; |
53 | object *head, *tail; |
54 | }; |
55 | |
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); |
60 | |
61 | static void make_pages_node(object *node, object *parent, page_data *first, |
62 | page_data *last, object *resources); |
f0e51ce1 |
63 | static int make_outline(object *parent, outline_element *start, int n, |
64 | int open); |
43341922 |
65 | |
66 | void pdf_backend(paragraph *sourceform, keywordlist *keywords, |
67 | indexdata *idx, void *vdoc) { |
68 | document *doc = (document *)vdoc; |
69 | int font_index; |
70 | font_encoding *fe; |
71 | page_data *page; |
72 | int pageno; |
73 | FILE *fp; |
74 | char *filename; |
75 | paragraph *p; |
76 | objlist olist; |
77 | object *o, *cat, *outlines, *pages, *resources; |
78 | int fileoff; |
79 | |
80 | IGNORE(keywords); |
81 | IGNORE(idx); |
82 | |
83 | filename = dupstr("output.pdf"); |
84 | for (p = sourceform; p; p = p->next) { |
43341922 |
85 | if (p->type == para_Config && p->parent) { |
86 | if (!ustricmp(p->keyword, L"pdf-filename")) { |
87 | sfree(filename); |
88 | filename = utoa_dup(uadv(p->keyword)); |
89 | } |
90 | } |
91 | } |
92 | |
93 | olist.head = olist.tail = NULL; |
94 | olist.number = 1; |
95 | |
96 | cat = new_object(&olist); |
97 | outlines = new_object(&olist); |
98 | pages = new_object(&olist); |
99 | resources = new_object(&olist); |
100 | |
101 | /* |
43341922 |
102 | * The catalogue just contains references to the outlines and |
103 | * pages objects. |
104 | */ |
105 | objtext(cat, "<<\n/Type /Catalog\n/Outlines "); |
106 | objref(cat, outlines); |
107 | objtext(cat, "\n/Pages "); |
108 | objref(cat, pages); |
f0e51ce1 |
109 | objtext(cat, "\n/PageMode /UseOutlines\n>>\n"); |
43341922 |
110 | |
111 | /* |
112 | * Set up the resources dictionary, which mostly means |
113 | * providing all the font objects and names to call them by. |
114 | */ |
115 | font_index = 0; |
116 | objtext(resources, "<<\n/Font <<\n"); |
117 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
118 | char fname[40]; |
119 | int i; |
120 | object *font; |
121 | |
122 | sprintf(fname, "f%d", font_index++); |
123 | fe->name = dupstr(fname); |
124 | |
125 | font = new_object(&olist); |
126 | |
127 | objtext(resources, "/"); |
128 | objtext(resources, fe->name); |
129 | objtext(resources, " "); |
130 | objref(resources, font); |
131 | objtext(resources, "\n"); |
132 | |
133 | objtext(font, "<<\n/Type /Font\n/Subtype /Type1\n/Name /"); |
134 | objtext(font, fe->name); |
135 | objtext(font, "\n/BaseFont /"); |
136 | objtext(font, fe->font->name); |
137 | objtext(font, "\n/Encoding <<\n/Type /Encoding\n/Differences ["); |
138 | |
139 | for (i = 0; i < 256; i++) { |
140 | char buf[20]; |
141 | if (!fe->vector[i]) |
142 | continue; |
143 | sprintf(buf, "\n%d /", i); |
144 | objtext(font, buf); |
145 | objtext(font, fe->vector[i] ? fe->vector[i] : ".notdef"); |
146 | } |
147 | |
148 | objtext(font, "\n]\n>>\n"); |
149 | |
150 | { |
151 | object *widths = new_object(&olist); |
152 | objtext(font, "/FirstChar 0\n/LastChar 255\n/Widths "); |
153 | objref(font, widths); |
154 | objtext(font, "\n"); |
155 | objtext(widths, "[\n"); |
156 | for (i = 0; i < 256; i++) { |
157 | char buf[80]; |
158 | double width; |
159 | if (fe->indices[i] < 0) |
160 | width = 0.0; |
161 | else |
162 | width = fe->font->widths[fe->indices[i]]; |
163 | sprintf(buf, "%g\n", 1000.0 * width / 4096.0); |
164 | objtext(widths, buf); |
165 | } |
166 | objtext(widths, "]\n"); |
167 | } |
168 | |
169 | objtext(font, ">>\n"); |
170 | } |
171 | objtext(resources, ">>\n>>\n"); |
172 | |
173 | /* |
174 | * Define the page objects for each page, and get each one |
175 | * ready to have a `Parent' specification added to it. |
176 | */ |
177 | for (page = doc->pages; page; page = page->next) { |
178 | object *opage; |
179 | |
180 | opage = new_object(&olist); |
181 | page->spare = opage; |
182 | objtext(opage, "<<\n/Type /Page\n"); |
183 | } |
184 | |
185 | /* |
186 | * Recursively build the page tree. |
187 | */ |
188 | make_pages_node(pages, NULL, doc->pages, NULL, resources); |
189 | |
190 | /* |
191 | * Create and render the individual pages. |
192 | */ |
193 | pageno = 0; |
194 | for (page = doc->pages; page; page = page->next) { |
195 | object *opage, *cstr; |
23765aeb |
196 | rect *r; |
43341922 |
197 | text_fragment *frag; |
198 | char buf[256]; |
199 | |
200 | opage = (object *)page->spare; |
201 | /* |
202 | * At this point the page dictionary is already |
203 | * half-written, with /Type and /Parent already present. We |
204 | * continue from there. |
205 | */ |
206 | |
207 | /* |
208 | * The PDF spec says /Resources is required, but also says |
209 | * that it's inheritable and may be omitted if it's present |
210 | * in a Pages node. In our case it is: it's present in the |
211 | * topmost /Pages node because we carefully put it there. |
212 | * So we don't need a /Resources entry here. |
213 | */ |
214 | sprintf(buf, "/MediaBox [0 0 %g %g]\n", |
215 | doc->paper_width / 4096.0, doc->paper_height / 4096.0); |
216 | objtext(opage, buf); |
217 | |
218 | /* |
219 | * Now we're ready to define a content stream containing |
220 | * the actual text on the page. |
221 | */ |
222 | cstr = new_object(&olist); |
223 | objtext(opage, "/Contents "); |
224 | objref(opage, cstr); |
225 | objtext(opage, "\n"); |
226 | |
23765aeb |
227 | /* |
228 | * Render any rectangles on the page. |
229 | */ |
230 | for (r = page->first_rect; r; r = r->next) { |
231 | char buf[512]; |
232 | sprintf(buf, "%g %g %g %g re f\n", r->x / 4096.0, |
233 | r->y / 4096.0, r->w / 4096.0, r->h / 4096.0); |
234 | objstream(cstr, buf); |
235 | } |
236 | |
43341922 |
237 | objstream(cstr, "BT\n"); |
238 | for (frag = page->first_text; frag; frag = frag->next) { |
239 | char *c; |
240 | |
241 | objstream(cstr, "/"); |
242 | objstream(cstr, frag->fe->name); |
243 | sprintf(buf, " %d Tf 1 0 0 1 %g %g Tm (", frag->fontsize, |
244 | frag->x/4096.0, frag->y/4096.0); |
245 | objstream(cstr, buf); |
246 | |
247 | for (c = frag->text; *c; c++) { |
248 | if (*c == '(' || *c == ')' || *c == '\\') |
249 | objstream(cstr, "\\"); |
250 | buf[0] = *c; |
251 | buf[1] = '\0'; |
252 | objstream(cstr, buf); |
253 | } |
254 | |
255 | objstream(cstr, ") Tj\n"); |
256 | } |
257 | objstream(cstr, "ET"); |
258 | |
138d7ffb |
259 | /* |
260 | * Also, we want an annotation dictionary containing the |
261 | * cross-references from this page. |
262 | */ |
263 | if (page->first_xref) { |
264 | xref *xr; |
265 | objtext(opage, "/Annots [\n"); |
266 | |
267 | for (xr = page->first_xref; xr; xr = xr->next) { |
268 | object *annot; |
269 | char buf[256]; |
270 | |
271 | annot = new_object(&olist); |
272 | objref(opage, annot); |
273 | objtext(opage, "\n"); |
274 | |
275 | objtext(annot, "<<\n/Type /Annot\n/Subtype /Link\n/Rect ["); |
276 | sprintf(buf, "%g %g %g %g", |
277 | xr->lx / 4096.0, xr->by / 4096.0, |
278 | xr->rx / 4096.0, xr->ty / 4096.0); |
279 | objtext(annot, buf); |
280 | objtext(annot, "]\n/Border [0 0 0]\n"); |
281 | |
282 | if (xr->dest.type == PAGE) { |
283 | objtext(annot, "/Dest ["); |
284 | objref(annot, (object *)xr->dest.page->spare); |
285 | objtext(annot, " /XYZ null null null]\n"); |
286 | } else { |
287 | char *p; |
288 | |
289 | objtext(annot, "/A <<\n/Type /Action\n/S /URI\n/URI ("); |
290 | for (p = xr->dest.url; *p; p++) { |
291 | char c[2]; |
292 | c[0] = *p; |
293 | c[1] = '\0'; |
294 | if (*p == '(' || *p == ')' || *p == '\\') |
295 | objtext(annot, "\\"); |
296 | objtext(annot, c); |
297 | } |
298 | objtext(annot, ")\n>>\n"); |
299 | } |
300 | |
301 | objtext(annot, ">>\n"); |
302 | } |
303 | |
304 | objtext(opage, "]\n"); |
305 | } |
306 | |
43341922 |
307 | objtext(opage, ">>\n"); |
308 | } |
309 | |
310 | /* |
f0e51ce1 |
311 | * Set up the outlines dictionary. |
312 | */ |
313 | { |
314 | int topcount; |
315 | char buf[80]; |
316 | |
317 | objtext(outlines, "<<\n/Type /Outlines\n"); |
318 | topcount = make_outline(outlines, doc->outline_elements, |
319 | doc->n_outline_elements, TRUE); |
320 | sprintf(buf, "/Count %d\n>>\n", topcount); |
321 | objtext(outlines, buf); |
322 | } |
323 | |
324 | /* |
43341922 |
325 | * Assemble the final linear form of every object. |
326 | */ |
327 | for (o = olist.head; o; o = o->next) { |
328 | rdstringc rs = {0, 0, NULL}; |
329 | char text[80]; |
330 | |
331 | sprintf(text, "%d 0 obj\n", o->number); |
332 | rdaddsc(&rs, text); |
333 | |
334 | if (!o->main.text && o->stream.text) { |
335 | sprintf(text, "<<\n/Length %d\n>>\n", o->stream.pos); |
336 | rdaddsc(&o->main, text); |
337 | } |
338 | |
339 | assert(o->main.text); |
340 | rdaddsc(&rs, o->main.text); |
341 | sfree(o->main.text); |
342 | |
343 | if (rs.text[rs.pos-1] != '\n') |
344 | rdaddc(&rs, '\n'); |
345 | |
346 | if (o->stream.text) { |
347 | /* |
348 | * FIXME: If we ever start compressing stream data then |
349 | * it will have zero bytes in it, so we'll have to be |
350 | * more careful than this. |
351 | */ |
352 | rdaddsc(&rs, "stream\n"); |
353 | rdaddsc(&rs, o->stream.text); |
354 | rdaddsc(&rs, "\nendstream\n"); |
355 | sfree(o->stream.text); |
356 | } |
357 | |
358 | rdaddsc(&rs, "endobj\n"); |
359 | |
360 | o->final = rs.text; |
361 | o->size = rs.pos; |
362 | } |
363 | |
364 | /* |
365 | * Write out the PDF file. |
366 | */ |
367 | |
368 | fp = fopen(filename, "wb"); |
369 | if (!fp) { |
370 | error(err_cantopenw, filename); |
371 | return; |
372 | } |
373 | |
374 | /* |
375 | * Header |
376 | */ |
377 | fileoff = fprintf(fp, "%%PDF-1.3\n"); |
378 | |
379 | /* |
380 | * Body |
381 | */ |
382 | for (o = olist.head; o; o = o->next) { |
383 | o->fileoff = fileoff; |
384 | fwrite(o->final, 1, o->size, fp); |
385 | fileoff += o->size; |
386 | } |
387 | |
388 | /* |
389 | * Cross-reference table |
390 | */ |
391 | fprintf(fp, "xref\n"); |
392 | assert(olist.head->number == 1); |
393 | fprintf(fp, "0 %d\n", olist.tail->number + 1); |
394 | fprintf(fp, "0000000000 65535 f \n"); |
395 | for (o = olist.head; o; o = o->next) { |
396 | char entry[40]; |
397 | sprintf(entry, "%010d 00000 n \n", o->fileoff); |
398 | assert(strlen(entry) == 20); |
399 | fputs(entry, fp); |
400 | } |
401 | |
402 | /* |
403 | * Trailer |
404 | */ |
405 | fprintf(fp, "trailer\n<<\n/Size %d\n/Root %d 0 R\n>>\n", |
406 | olist.tail->number + 1, cat->number); |
407 | fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff); |
408 | |
409 | fclose(fp); |
410 | |
411 | sfree(filename); |
412 | } |
413 | |
414 | static object *new_object(objlist *list) |
415 | { |
416 | object *obj = mknew(object); |
417 | |
418 | obj->list = list; |
419 | |
420 | obj->main.text = NULL; |
421 | obj->main.pos = obj->main.size = 0; |
422 | obj->stream.text = NULL; |
423 | obj->stream.pos = obj->stream.size = 0; |
424 | |
425 | obj->number = list->number++; |
426 | |
427 | obj->next = NULL; |
428 | if (list->tail) |
429 | list->tail->next = obj; |
430 | else |
431 | list->head = obj; |
432 | list->tail = obj; |
433 | |
434 | obj->size = 0; |
435 | obj->final = NULL; |
436 | |
437 | return obj; |
438 | } |
439 | |
440 | static void objtext(object *o, char const *text) |
441 | { |
442 | rdaddsc(&o->main, text); |
443 | } |
444 | |
445 | static void objstream(object *o, char const *text) |
446 | { |
447 | rdaddsc(&o->stream, text); |
448 | } |
449 | |
450 | static void objref(object *o, object *dest) |
451 | { |
452 | char buf[40]; |
453 | sprintf(buf, "%d 0 R", dest->number); |
454 | rdaddsc(&o->main, buf); |
455 | } |
456 | |
457 | static void make_pages_node(object *node, object *parent, page_data *first, |
458 | page_data *last, object *resources) |
459 | { |
460 | int count; |
461 | page_data *page; |
462 | char buf[80]; |
463 | |
464 | objtext(node, "<<\n/Type /Pages\n"); |
465 | if (parent) { |
466 | objtext(node, "/Parent "); |
467 | objref(node, parent); |
468 | objtext(node, "\n"); |
469 | } |
470 | |
471 | /* |
472 | * Count the pages in this stretch, to see if there are few |
473 | * enough to reference directly. |
474 | */ |
475 | count = 0; |
476 | for (page = first; page; page = page->next) { |
477 | count++; |
478 | if (page == last) |
479 | break; |
480 | } |
481 | |
482 | sprintf(buf, "/Count %d\n/Kids [\n", count); |
483 | objtext(node, buf); |
484 | |
485 | if (count > TREE_BRANCH) { |
486 | int i; |
487 | page_data *thisfirst, *thislast; |
488 | |
489 | page = first; |
490 | |
491 | for (i = 0; i < TREE_BRANCH; i++) { |
492 | int number = (i+1) * count / TREE_BRANCH - i * count / TREE_BRANCH; |
493 | thisfirst = page; |
494 | while (number--) { |
495 | thislast = page; |
496 | page = page->next; |
497 | } |
498 | |
499 | if (thisfirst == thislast) { |
500 | objref(node, (object *)thisfirst->spare); |
501 | objtext((object *)thisfirst->spare, "/Parent "); |
502 | objref((object *)thisfirst->spare, node); |
503 | objtext((object *)thisfirst->spare, "\n"); |
504 | } else { |
505 | object *newnode = new_object(node->list); |
506 | make_pages_node(newnode, node, thisfirst, thislast, NULL); |
507 | objref(node, newnode); |
508 | } |
509 | objtext(node, "\n"); |
510 | } |
511 | |
512 | assert(thislast == last || page == NULL); |
513 | |
514 | } else { |
515 | for (page = first; page; page = page->next) { |
516 | objref(node, (object *)page->spare); |
517 | objtext(node, "\n"); |
518 | objtext((object *)page->spare, "/Parent "); |
519 | objref((object *)page->spare, node); |
520 | objtext((object *)page->spare, "\n"); |
521 | if (page == last) |
522 | break; |
523 | } |
524 | } |
525 | |
526 | objtext(node, "]\n"); |
527 | |
528 | if (resources) { |
529 | objtext(node, "/Resources "); |
530 | objref(node, resources); |
531 | objtext(node, "\n"); |
532 | } |
533 | |
534 | objtext(node, ">>\n"); |
535 | } |
f0e51ce1 |
536 | |
537 | /* |
538 | * In text on the page, PDF uses the PostScript font model, which |
539 | * means that glyphs are identified by PS strings and hence font |
540 | * encoding can be managed independently of the supplied encoding |
541 | * of the font. However, in the document outline, the PDF spec |
542 | * simply asks for ordinary text strings without mentioning what |
543 | * character set they are supposed to be interpreted in. |
544 | * |
545 | * Therefore, for the moment, I'm going to assume they're US-ASCII |
546 | * only. If anyone knows better, they should let me know :-/ |
547 | */ |
548 | static int pdf_convert(wchar_t *s, char **result) { |
549 | int doing = (result != 0); |
550 | int ok = TRUE; |
551 | char *p = NULL; |
552 | int plen = 0, psize = 0; |
553 | |
554 | for (; *s; s++) { |
555 | wchar_t c = *s; |
556 | char outc; |
557 | |
558 | if (c >= 32 && c <= 126) { |
559 | /* Char is OK. */ |
560 | outc = (char)c; |
561 | } else { |
562 | /* Char is not OK. */ |
563 | ok = FALSE; |
564 | outc = 0xBF; /* approximate the good old DEC `uh?' */ |
565 | } |
566 | if (doing) { |
567 | if (plen >= psize) { |
568 | psize = plen + 256; |
569 | p = resize(p, psize); |
570 | } |
571 | p[plen++] = outc; |
572 | } |
573 | } |
574 | if (doing) { |
575 | p = resize(p, plen+1); |
576 | p[plen] = '\0'; |
577 | *result = p; |
578 | } |
579 | return ok; |
580 | } |
581 | |
f0e51ce1 |
582 | static int make_outline(object *parent, outline_element *items, int n, |
583 | int open) |
584 | { |
585 | int level, totalcount = 0; |
586 | outline_element *itemp; |
587 | object *curr, *prev = NULL, *first = NULL, *last = NULL; |
f0e51ce1 |
588 | |
589 | assert(n > 0); |
590 | |
591 | level = items->level; |
592 | |
593 | while (n > 0) { |
be76d597 |
594 | char *title, *p; |
f0e51ce1 |
595 | |
596 | /* |
597 | * Here we expect to be sitting on an item at the given |
598 | * level. So we start by constructing an outline entry for |
599 | * that item. |
600 | */ |
601 | assert(items->level == level); |
602 | |
be76d597 |
603 | pdf_convert(items->pdata->outline_title, &title); |
f0e51ce1 |
604 | |
605 | totalcount++; |
606 | curr = new_object(parent->list); |
607 | if (!first) first = curr; |
608 | last = curr; |
609 | objtext(curr, "<<\n/Title ("); |
be76d597 |
610 | for (p = title; *p; p++) { |
f0e51ce1 |
611 | char c[2]; |
612 | if (*p == '\\' || *p == '(' || *p == ')') |
613 | objtext(curr, "\\"); |
614 | c[0] = *p; |
615 | c[1] = '\0'; |
616 | objtext(curr, c); |
617 | } |
618 | objtext(curr, ")\n/Parent "); |
619 | objref(curr, parent); |
620 | objtext(curr, "\n/Dest ["); |
be76d597 |
621 | objref(curr, (object *)items->pdata->first->page->spare); |
f0e51ce1 |
622 | objtext(curr, " /XYZ null null null]\n"); |
623 | if (prev) { |
624 | objtext(curr, "/Prev "); |
625 | objref(curr, prev); |
626 | objtext(curr, "\n"); |
627 | |
628 | objtext(prev, "/Next "); |
629 | objref(prev, curr); |
630 | objtext(prev, "\n>>\n"); |
631 | } |
632 | prev = curr; |
633 | |
634 | items++, n--; |
635 | for (itemp = items; itemp < items+n && itemp->level > level; |
636 | itemp++); |
637 | |
638 | if (itemp > items) { |
639 | char buf[80]; |
640 | int count = make_outline(curr, items, itemp - items, FALSE); |
641 | if (!open) |
642 | count = -count; |
643 | else |
644 | totalcount += count; |
645 | sprintf(buf, "/Count %d\n", count); |
646 | objtext(curr, buf); |
647 | } |
648 | |
649 | n -= itemp - items; |
650 | items = itemp; |
651 | } |
652 | objtext(prev, ">>\n"); |
653 | |
654 | assert(first && last); |
655 | objtext(parent, "/First "); |
656 | objref(parent, first); |
657 | objtext(parent, "\n/Last "); |
658 | objref(parent, last); |
659 | objtext(parent, "\n"); |
660 | |
661 | return totalcount; |
662 | } |