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