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