Generate rather more compact /Differences tables for font encodings, mostly
[sgt/halibut] / bk_pdf.c
CommitLineData
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
11paragraph *pdf_config_filename(char *filename)
12{
e4ea58f8 13 return cmdline_cfg_simple("pdf-filename", filename, NULL);
43341922 14}
15
16typedef struct object_Tag object;
17typedef struct objlist_Tag objlist;
18
19struct object_Tag {
20 objlist *list;
21 object *next;
22 int number;
23 rdstringc main, stream;
24 int size, fileoff;
25 char *final;
26};
27
28struct objlist_Tag {
29 int number;
30 object *head, *tail;
31};
32
33static object *new_object(objlist *list);
34static void objtext(object *o, char const *text);
35static void objstream(object *o, char const *text);
7c8c4239 36static void pdf_string(void (*add)(object *, char const *),
37 object *, char const *);
ce1b04aa 38static void pdf_string_len(void (*add)(object *, char const *),
39 object *, char const *, int);
43341922 40static void objref(object *o, object *dest);
993ade7a 41static char *pdf_outline_convert(wchar_t *s, int *len);
43341922 42
3600720b 43static int is_std_font(char const *name);
44
43341922 45static void make_pages_node(object *node, object *parent, page_data *first,
2ff8419e 46 page_data *last, object *resources,
47 object *mediabox);
f0e51ce1 48static int make_outline(object *parent, outline_element *start, int n,
49 int open);
09358aa7 50static int pdf_versionid(FILE *fp, word *words);
43341922 51
52void pdf_backend(paragraph *sourceform, keywordlist *keywords,
53 indexdata *idx, void *vdoc) {
54 document *doc = (document *)vdoc;
55 int font_index;
56 font_encoding *fe;
57 page_data *page;
58 int pageno;
59 FILE *fp;
60 char *filename;
61 paragraph *p;
62 objlist olist;
993ade7a 63 object *o, *info, *cat, *outlines, *pages, *resources, *mediabox;
43341922 64 int fileoff;
65
66 IGNORE(keywords);
67 IGNORE(idx);
68
69 filename = dupstr("output.pdf");
70 for (p = sourceform; p; p = p->next) {
7738a2fb 71 if (p->type == para_Config) {
43341922 72 if (!ustricmp(p->keyword, L"pdf-filename")) {
73 sfree(filename);
e4ea58f8 74 filename = dupstr(adv(p->origkeyword));
43341922 75 }
76 }
77 }
78
79 olist.head = olist.tail = NULL;
80 olist.number = 1;
81
993ade7a 82 {
83 char buf[256];
84
85 info = new_object(&olist);
86 objtext(info, "<<\n");
87 if (doc->n_outline_elements > 0) {
88 char *title;
89 int titlelen;
90
91 title =
92 pdf_outline_convert(doc->outline_elements->pdata->outline_title,
93 &titlelen);
94 objtext(info, "/Title ");
95 pdf_string_len(objtext, info, title, titlelen);
96 sfree(title);
97 objtext(info, "\n");
98 }
99 objtext(info, "/Producer ");
100 sprintf(buf, "Halibut, %s", version);
101 pdf_string(objtext, info, buf);
102 objtext(info, "\n>>\n");
103 }
104
43341922 105 cat = new_object(&olist);
a615a778 106 if (doc->n_outline_elements > 0)
107 outlines = new_object(&olist);
108 else
109 outlines = NULL;
43341922 110 pages = new_object(&olist);
111 resources = new_object(&olist);
112
113 /*
43341922 114 * The catalogue just contains references to the outlines and
115 * pages objects.
116 */
a615a778 117 objtext(cat, "<<\n/Type /Catalog");
118 if (outlines) {
119 objtext(cat, "\n/Outlines ");
120 objref(cat, outlines);
121 }
43341922 122 objtext(cat, "\n/Pages ");
123 objref(cat, pages);
a615a778 124 if (outlines)
125 objtext(cat, "\n/PageMode /UseOutlines");
126 objtext(cat, "\n>>\n");
43341922 127
128 /*
129 * Set up the resources dictionary, which mostly means
130 * providing all the font objects and names to call them by.
131 */
132 font_index = 0;
133 objtext(resources, "<<\n/Font <<\n");
134 for (fe = doc->fonts->head; fe; fe = fe->next) {
135 char fname[40];
b17a4ef3 136 int i, prev;
43341922 137 object *font;
138
139 sprintf(fname, "f%d", font_index++);
140 fe->name = dupstr(fname);
141
142 font = new_object(&olist);
143
144 objtext(resources, "/");
145 objtext(resources, fe->name);
146 objtext(resources, " ");
147 objref(resources, font);
148 objtext(resources, "\n");
149
150 objtext(font, "<<\n/Type /Font\n/Subtype /Type1\n/Name /");
151 objtext(font, fe->name);
152 objtext(font, "\n/BaseFont /");
ba0fe3ec 153 objtext(font, fe->font->info->name);
43341922 154 objtext(font, "\n/Encoding <<\n/Type /Encoding\n/Differences [");
155
156 for (i = 0; i < 256; i++) {
157 char buf[20];
158 if (!fe->vector[i])
159 continue;
b17a4ef3 160 if (i != prev + 1) {
161 sprintf(buf, "\n%d", i);
162 objtext(font, buf);
163 }
164 objtext(font, i % 8 ? "/" : "\n/");
165 objtext(font, fe->vector[i]);
166 prev = i;
43341922 167 }
168
169 objtext(font, "\n]\n>>\n");
170
255b7ff3 171#define FF_FIXEDPITCH 0x00000001
172#define FF_SERIF 0x00000002
173#define FF_SYMBOLIC 0x00000004
174#define FF_SCRIPT 0x00000008
175#define FF_NONSYMBOLIC 0x00000020
176#define FF_ITALIC 0x00000040
177#define FF_ALLCAP 0x00010000
178#define FF_SMALLCAP 0x00020000
179#define FF_FORCEBOLD 0x00040000
180
3600720b 181 if (!is_std_font(fe->font->info->name)){
43341922 182 object *widths = new_object(&olist);
255b7ff3 183 object *fontdesc = new_object(&olist);
9fb062fc 184 int firstchar = -1, lastchar = -1;
185 char buf[80];
255b7ff3 186 font_info const *fi = fe->font->info;
187 int flags;
9fb062fc 188 for (i = 0; i < 256; i++)
189 if (fe->indices[i] >= 0) {
190 if (firstchar < 0) firstchar = i;
191 lastchar = i;
192 }
193 sprintf(buf, "/FirstChar %d\n/LastChar %d\n/Widths ",
194 firstchar, lastchar);
195 objtext(font, buf);
43341922 196 objref(font, widths);
197 objtext(font, "\n");
198 objtext(widths, "[\n");
9fb062fc 199 for (i = firstchar; i <= lastchar; i++) {
43341922 200 double width;
201 if (fe->indices[i] < 0)
202 width = 0.0;
203 else
255b7ff3 204 width = fi->widths[fe->indices[i]];
17c71b41 205 sprintf(buf, "%g\n", 1000.0 * width / FUNITS_PER_PT);
43341922 206 objtext(widths, buf);
207 }
208 objtext(widths, "]\n");
255b7ff3 209 objtext(font, "/FontDescriptor ");
210 objref(font, fontdesc);
211 objtext(fontdesc, "<<\n/Type /FontDescriptor\n/Name /");
212 objtext(fontdesc, fi->name);
213 flags = 0;
214 if (fi->italicangle) flags |= FF_ITALIC;
215 flags |= FF_NONSYMBOLIC;
216 sprintf(buf, "\n/Flags %d\n", flags);
217 objtext(fontdesc, buf);
218 sprintf(buf, "/FontBBox [%g %g %g %g]\n", fi->fontbbox[0],
219 fi->fontbbox[1], fi->fontbbox[2], fi->fontbbox[3]);
220 objtext(fontdesc, buf);
221 sprintf(buf, "/ItalicAngle %g\n", fi->italicangle);
222 objtext(fontdesc, buf);
223 sprintf(buf, "/Ascent %g\n", fi->ascent);
224 objtext(fontdesc, buf);
225 sprintf(buf, "/Descent %g\n", fi->descent);
226 objtext(fontdesc, buf);
227 sprintf(buf, "/CapHeight %g\n", fi->capheight);
228 objtext(fontdesc, buf);
229 sprintf(buf, "/XHeight %g\n", fi->xheight);
230 objtext(fontdesc, buf);
231 sprintf(buf, "/StemH %g\n", fi->stemh);
232 objtext(fontdesc, buf);
233 sprintf(buf, "/StemV %g\n", fi->stemv);
234 objtext(fontdesc, buf);
44407fea 235 if (fi->fp) {
236 object *fontfile = new_object(&olist);
237 char buf[513];
238 size_t len;
239 rewind(fi->fp);
240 do {
241 len = fread(buf, 1, sizeof(buf)-1, fi->fp);
242 buf[len] = 0;
243 objstream(fontfile, buf);
244 } while (len == sizeof(buf)-1);
245 objtext(fontdesc, "/FontFile ");
246 objref(fontdesc, fontfile);
247 }
248 objtext(fontdesc, "\n>>\n");
43341922 249 }
250
255b7ff3 251 objtext(font, "\n>>\n");
43341922 252 }
253 objtext(resources, ">>\n>>\n");
254
2ff8419e 255 {
256 char buf[255];
257 mediabox = new_object(&olist);
258 sprintf(buf, "[0 0 %g %g]\n",
259 doc->paper_width / FUNITS_PER_PT,
260 doc->paper_height / FUNITS_PER_PT);
261 objtext(mediabox, buf);
262 }
263
43341922 264 /*
265 * Define the page objects for each page, and get each one
266 * ready to have a `Parent' specification added to it.
267 */
268 for (page = doc->pages; page; page = page->next) {
269 object *opage;
270
271 opage = new_object(&olist);
272 page->spare = opage;
273 objtext(opage, "<<\n/Type /Page\n");
274 }
275
276 /*
277 * Recursively build the page tree.
278 */
2ff8419e 279 make_pages_node(pages, NULL, doc->pages, NULL, resources, mediabox);
43341922 280
281 /*
282 * Create and render the individual pages.
283 */
284 pageno = 0;
285 for (page = doc->pages; page; page = page->next) {
286 object *opage, *cstr;
23765aeb 287 rect *r;
7c8c4239 288 text_fragment *frag, *frag_end;
43341922 289 char buf[256];
7c8c4239 290 int x, y, lx, ly;
43341922 291
292 opage = (object *)page->spare;
293 /*
294 * At this point the page dictionary is already
295 * half-written, with /Type and /Parent already present. We
296 * continue from there.
297 */
298
299 /*
300 * The PDF spec says /Resources is required, but also says
301 * that it's inheritable and may be omitted if it's present
302 * in a Pages node. In our case it is: it's present in the
303 * topmost /Pages node because we carefully put it there.
2ff8419e 304 * So we don't need a /Resources entry here. The same applies
305 * to /MediaBox.
43341922 306 */
43341922 307
308 /*
309 * Now we're ready to define a content stream containing
310 * the actual text on the page.
311 */
312 cstr = new_object(&olist);
313 objtext(opage, "/Contents ");
314 objref(opage, cstr);
315 objtext(opage, "\n");
316
23765aeb 317 /*
318 * Render any rectangles on the page.
319 */
320 for (r = page->first_rect; r; r = r->next) {
321 char buf[512];
17c71b41 322 sprintf(buf, "%g %g %g %g re f\n",
323 r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT,
324 r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT);
23765aeb 325 objstream(cstr, buf);
326 }
327
43341922 328 objstream(cstr, "BT\n");
43341922 329
7c8c4239 330 /*
331 * PDF tracks two separate current positions: the position
332 * given in the `line matrix' and the position given in the
333 * `text matrix'. We must therefore track both as well.
334 * They start off at -1 (unset).
335 */
336 lx = ly = -1;
337 x = y = -1;
338
339 frag = page->first_text;
340 while (frag) {
341 /*
342 * For compactness, I'm going to group text fragments
343 * into subsequences that use the same font+size. So
344 * first find the end of this subsequence.
345 */
346 for (frag_end = frag;
347 (frag_end &&
348 frag_end->fe == frag->fe &&
349 frag_end->fontsize == frag->fontsize);
350 frag_end = frag_end->next);
351
352 /*
353 * Now select the text fragment, and prepare to display
354 * the text.
355 */
43341922 356 objstream(cstr, "/");
357 objstream(cstr, frag->fe->name);
7c8c4239 358 sprintf(buf, " %d Tf ", frag->fontsize);
43341922 359 objstream(cstr, buf);
360
7c8c4239 361 while (frag && frag != frag_end) {
362 /*
363 * Place the text position for the first piece of
364 * text.
365 */
366 if (lx < 0) {
367 sprintf(buf, "1 0 0 1 %g %g Tm ",
17c71b41 368 frag->x/FUNITS_PER_PT, frag->y/FUNITS_PER_PT);
7c8c4239 369 } else {
370 sprintf(buf, "%g %g Td ",
17c71b41 371 (frag->x - lx)/FUNITS_PER_PT,
372 (frag->y - ly)/FUNITS_PER_PT);
7c8c4239 373 }
43341922 374 objstream(cstr, buf);
7c8c4239 375 lx = x = frag->x;
376 ly = y = frag->y;
377
378 /*
379 * See if we're going to use Tj (show a single
380 * string) or TJ (show an array of strings with
381 * x-spacings between them). We determine this by
382 * seeing if there's more than one text fragment in
383 * sequence with the same y-coordinate.
384 */
385 if (frag->next && frag->next != frag_end &&
386 frag->next->y == y) {
387 /*
388 * The TJ strategy.
389 */
390 objstream(cstr, "[");
391 while (frag && frag != frag_end && frag->y == y) {
392 if (frag->x != x) {
393 sprintf(buf, "%g",
394 (x - frag->x) * 1000.0 /
17c71b41 395 (FUNITS_PER_PT * frag->fontsize));
7c8c4239 396 objstream(cstr, buf);
397 }
398 pdf_string(objstream, cstr, frag->text);
399 x = frag->x + frag->width;
400 frag = frag->next;
401 }
402 objstream(cstr, "]TJ\n");
403 } else
404 {
405 /*
406 * The Tj strategy.
407 */
408 pdf_string(objstream, cstr, frag->text);
409 objstream(cstr, "Tj\n");
410 frag = frag->next;
411 }
43341922 412 }
43341922 413 }
414 objstream(cstr, "ET");
415
138d7ffb 416 /*
417 * Also, we want an annotation dictionary containing the
418 * cross-references from this page.
419 */
420 if (page->first_xref) {
421 xref *xr;
422 objtext(opage, "/Annots [\n");
423
424 for (xr = page->first_xref; xr; xr = xr->next) {
425 object *annot;
426 char buf[256];
427
428 annot = new_object(&olist);
429 objref(opage, annot);
430 objtext(opage, "\n");
431
432 objtext(annot, "<<\n/Type /Annot\n/Subtype /Link\n/Rect [");
433 sprintf(buf, "%g %g %g %g",
17c71b41 434 xr->lx / FUNITS_PER_PT, xr->by / FUNITS_PER_PT,
435 xr->rx / FUNITS_PER_PT, xr->ty / FUNITS_PER_PT);
138d7ffb 436 objtext(annot, buf);
437 objtext(annot, "]\n/Border [0 0 0]\n");
438
439 if (xr->dest.type == PAGE) {
440 objtext(annot, "/Dest [");
441 objref(annot, (object *)xr->dest.page->spare);
442 objtext(annot, " /XYZ null null null]\n");
443 } else {
7c8c4239 444 objtext(annot, "/A <<\n/Type /Action\n/S /URI\n/URI ");
445 pdf_string(objtext, annot, xr->dest.url);
446 objtext(annot, "\n>>\n");
138d7ffb 447 }
448
449 objtext(annot, ">>\n");
450 }
451
452 objtext(opage, "]\n");
453 }
454
43341922 455 objtext(opage, ">>\n");
456 }
457
458 /*
f0e51ce1 459 * Set up the outlines dictionary.
460 */
a615a778 461 if (outlines) {
f0e51ce1 462 int topcount;
463 char buf[80];
464
465 objtext(outlines, "<<\n/Type /Outlines\n");
466 topcount = make_outline(outlines, doc->outline_elements,
467 doc->n_outline_elements, TRUE);
468 sprintf(buf, "/Count %d\n>>\n", topcount);
469 objtext(outlines, buf);
470 }
471
472 /*
43341922 473 * Assemble the final linear form of every object.
474 */
475 for (o = olist.head; o; o = o->next) {
476 rdstringc rs = {0, 0, NULL};
477 char text[80];
478
479 sprintf(text, "%d 0 obj\n", o->number);
480 rdaddsc(&rs, text);
481
482 if (!o->main.text && o->stream.text) {
483 sprintf(text, "<<\n/Length %d\n>>\n", o->stream.pos);
484 rdaddsc(&o->main, text);
485 }
486
487 assert(o->main.text);
488 rdaddsc(&rs, o->main.text);
489 sfree(o->main.text);
490
491 if (rs.text[rs.pos-1] != '\n')
492 rdaddc(&rs, '\n');
493
494 if (o->stream.text) {
495 /*
496 * FIXME: If we ever start compressing stream data then
497 * it will have zero bytes in it, so we'll have to be
498 * more careful than this.
499 */
500 rdaddsc(&rs, "stream\n");
501 rdaddsc(&rs, o->stream.text);
502 rdaddsc(&rs, "\nendstream\n");
503 sfree(o->stream.text);
504 }
505
506 rdaddsc(&rs, "endobj\n");
507
508 o->final = rs.text;
509 o->size = rs.pos;
510 }
511
512 /*
513 * Write out the PDF file.
514 */
515
516 fp = fopen(filename, "wb");
517 if (!fp) {
518 error(err_cantopenw, filename);
519 return;
520 }
521
522 /*
09358aa7 523 * Header. I'm going to put the version IDs in the header as
524 * well, simply in PDF comments.
43341922 525 */
526 fileoff = fprintf(fp, "%%PDF-1.3\n");
09358aa7 527 for (p = sourceform; p; p = p->next)
528 if (p->type == para_VersionID)
529 fileoff += pdf_versionid(fp, p->words);
43341922 530
531 /*
532 * Body
533 */
534 for (o = olist.head; o; o = o->next) {
535 o->fileoff = fileoff;
536 fwrite(o->final, 1, o->size, fp);
537 fileoff += o->size;
538 }
539
540 /*
541 * Cross-reference table
542 */
543 fprintf(fp, "xref\n");
544 assert(olist.head->number == 1);
545 fprintf(fp, "0 %d\n", olist.tail->number + 1);
546 fprintf(fp, "0000000000 65535 f \n");
547 for (o = olist.head; o; o = o->next) {
548 char entry[40];
549 sprintf(entry, "%010d 00000 n \n", o->fileoff);
550 assert(strlen(entry) == 20);
551 fputs(entry, fp);
552 }
553
554 /*
555 * Trailer
556 */
993ade7a 557 fprintf(fp, "trailer\n<<\n/Size %d\n/Root %d 0 R\n/Info %d 0 R\n>>\n",
558 olist.tail->number + 1, cat->number, info->number);
43341922 559 fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff);
560
561 fclose(fp);
562
563 sfree(filename);
564}
565
566static object *new_object(objlist *list)
567{
f1530049 568 object *obj = snew(object);
43341922 569
570 obj->list = list;
571
572 obj->main.text = NULL;
573 obj->main.pos = obj->main.size = 0;
574 obj->stream.text = NULL;
575 obj->stream.pos = obj->stream.size = 0;
576
577 obj->number = list->number++;
578
579 obj->next = NULL;
580 if (list->tail)
581 list->tail->next = obj;
582 else
583 list->head = obj;
584 list->tail = obj;
585
586 obj->size = 0;
587 obj->final = NULL;
588
589 return obj;
590}
591
592static void objtext(object *o, char const *text)
593{
594 rdaddsc(&o->main, text);
595}
596
597static void objstream(object *o, char const *text)
598{
599 rdaddsc(&o->stream, text);
600}
601
602static void objref(object *o, object *dest)
603{
604 char buf[40];
605 sprintf(buf, "%d 0 R", dest->number);
606 rdaddsc(&o->main, buf);
607}
608
3600720b 609static char const * const stdfonts[] = {
610 "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",
611 "Helvetica", "Helvetica-Bold", "Helvetica-Oblique","Helvetica-BoldOblique",
612 "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique",
613 "Symbol", "ZapfDingbats"
614};
615
616static int is_std_font(char const *name) {
617 unsigned i;
618 for (i = 0; i < lenof(stdfonts); i++)
619 if (strcmp(name, stdfonts[i]) == 0)
620 return TRUE;
621 return FALSE;
622}
623
43341922 624static void make_pages_node(object *node, object *parent, page_data *first,
2ff8419e 625 page_data *last, object *resources,
626 object *mediabox)
43341922 627{
628 int count;
629 page_data *page;
630 char buf[80];
631
632 objtext(node, "<<\n/Type /Pages\n");
633 if (parent) {
634 objtext(node, "/Parent ");
635 objref(node, parent);
636 objtext(node, "\n");
637 }
638
639 /*
640 * Count the pages in this stretch, to see if there are few
641 * enough to reference directly.
642 */
643 count = 0;
644 for (page = first; page; page = page->next) {
645 count++;
646 if (page == last)
647 break;
648 }
649
650 sprintf(buf, "/Count %d\n/Kids [\n", count);
651 objtext(node, buf);
652
653 if (count > TREE_BRANCH) {
654 int i;
655 page_data *thisfirst, *thislast;
656
657 page = first;
658
659 for (i = 0; i < TREE_BRANCH; i++) {
660 int number = (i+1) * count / TREE_BRANCH - i * count / TREE_BRANCH;
661 thisfirst = page;
662 while (number--) {
663 thislast = page;
664 page = page->next;
665 }
666
667 if (thisfirst == thislast) {
668 objref(node, (object *)thisfirst->spare);
669 objtext((object *)thisfirst->spare, "/Parent ");
670 objref((object *)thisfirst->spare, node);
671 objtext((object *)thisfirst->spare, "\n");
672 } else {
673 object *newnode = new_object(node->list);
2ff8419e 674 make_pages_node(newnode, node, thisfirst, thislast,
675 NULL, NULL);
43341922 676 objref(node, newnode);
677 }
678 objtext(node, "\n");
679 }
680
681 assert(thislast == last || page == NULL);
682
683 } else {
684 for (page = first; page; page = page->next) {
685 objref(node, (object *)page->spare);
686 objtext(node, "\n");
687 objtext((object *)page->spare, "/Parent ");
688 objref((object *)page->spare, node);
689 objtext((object *)page->spare, "\n");
690 if (page == last)
691 break;
692 }
693 }
694
695 objtext(node, "]\n");
696
697 if (resources) {
698 objtext(node, "/Resources ");
699 objref(node, resources);
700 objtext(node, "\n");
701 }
2ff8419e 702 if (mediabox) {
703 objtext(node, "/MediaBox ");
704 objref(node, mediabox);
705 objtext(node, "\n");
706 }
43341922 707
708 objtext(node, ">>\n");
709}
f0e51ce1 710
711/*
712 * In text on the page, PDF uses the PostScript font model, which
713 * means that glyphs are identified by PS strings and hence font
714 * encoding can be managed independently of the supplied encoding
715 * of the font. However, in the document outline, the PDF spec
ce1b04aa 716 * encodes in either PDFDocEncoding (a custom superset of
717 * ISO-8859-1) or UTF-16BE.
f0e51ce1 718 */
ce1b04aa 719static char *pdf_outline_convert(wchar_t *s, int *len) {
720 char *ret;
721
722 ret = utoa_careful_dup(s, CS_PDF);
723
724 /*
725 * Very silly special case: if the returned string begins with
726 * FE FF, then the PDF reader will mistake it for a UTF-16BE
727 * string. So in this case we give up on PDFDocEncoding and
728 * encode it in UTF-16 straight away.
729 */
730 if (ret && ret[0] == '\xFE' && ret[1] == '\xFF') {
731 sfree(ret);
732 ret = NULL;
f0e51ce1 733 }
ce1b04aa 734
735 if (!ret) {
736 ret = utoa_dup_len(s, CS_UTF16BE, len);
737 } else {
738 *len = strlen(ret);
f0e51ce1 739 }
ce1b04aa 740
741 return ret;
f0e51ce1 742}
743
f0e51ce1 744static int make_outline(object *parent, outline_element *items, int n,
745 int open)
746{
747 int level, totalcount = 0;
748 outline_element *itemp;
749 object *curr, *prev = NULL, *first = NULL, *last = NULL;
f0e51ce1 750
751 assert(n > 0);
752
753 level = items->level;
754
755 while (n > 0) {
7c8c4239 756 char *title;
ce1b04aa 757 int titlelen;
f0e51ce1 758
759 /*
760 * Here we expect to be sitting on an item at the given
761 * level. So we start by constructing an outline entry for
762 * that item.
763 */
764 assert(items->level == level);
765
ce1b04aa 766 title = pdf_outline_convert(items->pdata->outline_title, &titlelen);
f0e51ce1 767
768 totalcount++;
769 curr = new_object(parent->list);
770 if (!first) first = curr;
771 last = curr;
7c8c4239 772 objtext(curr, "<<\n/Title ");
ce1b04aa 773 pdf_string_len(objtext, curr, title, titlelen);
993ade7a 774 sfree(title);
7c8c4239 775 objtext(curr, "\n/Parent ");
f0e51ce1 776 objref(curr, parent);
777 objtext(curr, "\n/Dest [");
be76d597 778 objref(curr, (object *)items->pdata->first->page->spare);
f0e51ce1 779 objtext(curr, " /XYZ null null null]\n");
780 if (prev) {
781 objtext(curr, "/Prev ");
782 objref(curr, prev);
783 objtext(curr, "\n");
784
785 objtext(prev, "/Next ");
786 objref(prev, curr);
787 objtext(prev, "\n>>\n");
788 }
789 prev = curr;
790
791 items++, n--;
792 for (itemp = items; itemp < items+n && itemp->level > level;
793 itemp++);
794
795 if (itemp > items) {
796 char buf[80];
797 int count = make_outline(curr, items, itemp - items, FALSE);
798 if (!open)
799 count = -count;
800 else
801 totalcount += count;
802 sprintf(buf, "/Count %d\n", count);
803 objtext(curr, buf);
804 }
805
806 n -= itemp - items;
807 items = itemp;
808 }
809 objtext(prev, ">>\n");
810
811 assert(first && last);
812 objtext(parent, "/First ");
813 objref(parent, first);
814 objtext(parent, "\n/Last ");
815 objref(parent, last);
816 objtext(parent, "\n");
817
818 return totalcount;
819}
09358aa7 820
821static int pdf_versionid(FILE *fp, word *words)
822{
823 int ret;
824
825 ret = fprintf(fp, "%% ");
826
827 for (; words; words = words->next) {
828 char *text;
829 int type;
830
831 switch (words->type) {
832 case word_HyperLink:
833 case word_HyperEnd:
834 case word_UpperXref:
835 case word_LowerXref:
836 case word_XrefEnd:
837 case word_IndexRef:
838 continue;
839 }
840
841 type = removeattr(words->type);
842
843 switch (type) {
844 case word_Normal:
e4ea58f8 845 text = utoa_dup(words->text, CS_ASCII);
09358aa7 846 break;
847 case word_WhiteSpace:
848 text = dupstr(" ");
849 break;
850 case word_Quote:
851 text = dupstr("'");
852 break;
853 }
854
855 fputs(text, fp);
856 ret += strlen(text);
857 sfree(text);
858 }
859
860 ret += fprintf(fp, "\n");
861
862 return ret;
863}
7c8c4239 864
ce1b04aa 865static void pdf_string_len(void (*add)(object *, char const *),
866 object *o, char const *str, int len)
7c8c4239 867{
868 char const *p;
869
870 add(o, "(");
ce1b04aa 871 for (p = str; len > 0; p++, len--) {
872 char c[10];
873 if (*p < ' ' || *p > '~') {
874 sprintf(c, "\\%03o", 0xFF & (int)*p);
875 } else {
876 int n = 0;
877 if (*p == '\\' || *p == '(' || *p == ')')
878 c[n++] = '\\';
879 c[n++] = *p;
880 c[n] = '\0';
881 }
7c8c4239 882 add(o, c);
883 }
884 add(o, ")");
885}
ce1b04aa 886
887static void pdf_string(void (*add)(object *, char const *),
888 object *o, char const *str)
889{
890 pdf_string_len(add, o, str, strlen(str));
891}