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