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