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