| 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. |
| 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 | if (outlines) |
| 125 | objtext(cat, "\n/PageMode /UseOutlines"); |
| 126 | objtext(cat, "\n>>\n"); |
| 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 /"); |
| 153 | objtext(font, fe->font->info->name); |
| 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 | |
| 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 | |
| 177 | if (!is_std_font(fe->font->info->name)){ |
| 178 | object *widths = new_object(&olist); |
| 179 | object *fontdesc = new_object(&olist); |
| 180 | int firstchar = -1, lastchar = -1; |
| 181 | char buf[80]; |
| 182 | font_info const *fi = fe->font->info; |
| 183 | int flags; |
| 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); |
| 192 | objref(font, widths); |
| 193 | objtext(font, "\n"); |
| 194 | objtext(widths, "[\n"); |
| 195 | for (i = firstchar; i <= lastchar; i++) { |
| 196 | double width; |
| 197 | if (fe->indices[i] < 0) |
| 198 | width = 0.0; |
| 199 | else |
| 200 | width = fi->widths[fe->indices[i]]; |
| 201 | sprintf(buf, "%g\n", 1000.0 * width / FUNITS_PER_PT); |
| 202 | objtext(widths, buf); |
| 203 | } |
| 204 | objtext(widths, "]\n"); |
| 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"); |
| 232 | } |
| 233 | |
| 234 | objtext(font, "\n>>\n"); |
| 235 | } |
| 236 | objtext(resources, ">>\n>>\n"); |
| 237 | |
| 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 | |
| 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 | */ |
| 262 | make_pages_node(pages, NULL, doc->pages, NULL, resources, mediabox); |
| 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; |
| 270 | rect *r; |
| 271 | text_fragment *frag, *frag_end; |
| 272 | char buf[256]; |
| 273 | int x, y, lx, ly; |
| 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. |
| 287 | * So we don't need a /Resources entry here. The same applies |
| 288 | * to /MediaBox. |
| 289 | */ |
| 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 | |
| 300 | /* |
| 301 | * Render any rectangles on the page. |
| 302 | */ |
| 303 | for (r = page->first_rect; r; r = r->next) { |
| 304 | char buf[512]; |
| 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); |
| 308 | objstream(cstr, buf); |
| 309 | } |
| 310 | |
| 311 | objstream(cstr, "BT\n"); |
| 312 | |
| 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 | */ |
| 339 | objstream(cstr, "/"); |
| 340 | objstream(cstr, frag->fe->name); |
| 341 | sprintf(buf, " %d Tf ", frag->fontsize); |
| 342 | objstream(cstr, buf); |
| 343 | |
| 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 ", |
| 351 | frag->x/FUNITS_PER_PT, frag->y/FUNITS_PER_PT); |
| 352 | } else { |
| 353 | sprintf(buf, "%g %g Td ", |
| 354 | (frag->x - lx)/FUNITS_PER_PT, |
| 355 | (frag->y - ly)/FUNITS_PER_PT); |
| 356 | } |
| 357 | objstream(cstr, buf); |
| 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 / |
| 378 | (FUNITS_PER_PT * frag->fontsize)); |
| 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 | } |
| 395 | } |
| 396 | } |
| 397 | objstream(cstr, "ET"); |
| 398 | |
| 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", |
| 417 | xr->lx / FUNITS_PER_PT, xr->by / FUNITS_PER_PT, |
| 418 | xr->rx / FUNITS_PER_PT, xr->ty / FUNITS_PER_PT); |
| 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 { |
| 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"); |
| 430 | } |
| 431 | |
| 432 | objtext(annot, ">>\n"); |
| 433 | } |
| 434 | |
| 435 | objtext(opage, "]\n"); |
| 436 | } |
| 437 | |
| 438 | objtext(opage, ">>\n"); |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Set up the outlines dictionary. |
| 443 | */ |
| 444 | if (outlines) { |
| 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 | /* |
| 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 | /* |
| 506 | * Header. I'm going to put the version IDs in the header as |
| 507 | * well, simply in PDF comments. |
| 508 | */ |
| 509 | fileoff = fprintf(fp, "%%PDF-1.3\n"); |
| 510 | for (p = sourceform; p; p = p->next) |
| 511 | if (p->type == para_VersionID) |
| 512 | fileoff += pdf_versionid(fp, p->words); |
| 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 | */ |
| 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); |
| 542 | fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff); |
| 543 | |
| 544 | fclose(fp); |
| 545 | |
| 546 | sfree(filename); |
| 547 | } |
| 548 | |
| 549 | static object *new_object(objlist *list) |
| 550 | { |
| 551 | object *obj = snew(object); |
| 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 | |
| 575 | static void objtext(object *o, char const *text) |
| 576 | { |
| 577 | rdaddsc(&o->main, text); |
| 578 | } |
| 579 | |
| 580 | static void objstream(object *o, char const *text) |
| 581 | { |
| 582 | rdaddsc(&o->stream, text); |
| 583 | } |
| 584 | |
| 585 | static 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 | |
| 592 | static 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 | |
| 599 | static 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 | |
| 607 | static void make_pages_node(object *node, object *parent, page_data *first, |
| 608 | page_data *last, object *resources, |
| 609 | object *mediabox) |
| 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); |
| 657 | make_pages_node(newnode, node, thisfirst, thislast, |
| 658 | NULL, NULL); |
| 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 | } |
| 685 | if (mediabox) { |
| 686 | objtext(node, "/MediaBox "); |
| 687 | objref(node, mediabox); |
| 688 | objtext(node, "\n"); |
| 689 | } |
| 690 | |
| 691 | objtext(node, ">>\n"); |
| 692 | } |
| 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 |
| 699 | * encodes in either PDFDocEncoding (a custom superset of |
| 700 | * ISO-8859-1) or UTF-16BE. |
| 701 | */ |
| 702 | static 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; |
| 716 | } |
| 717 | |
| 718 | if (!ret) { |
| 719 | ret = utoa_dup_len(s, CS_UTF16BE, len); |
| 720 | } else { |
| 721 | *len = strlen(ret); |
| 722 | } |
| 723 | |
| 724 | return ret; |
| 725 | } |
| 726 | |
| 727 | static 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; |
| 733 | |
| 734 | assert(n > 0); |
| 735 | |
| 736 | level = items->level; |
| 737 | |
| 738 | while (n > 0) { |
| 739 | char *title; |
| 740 | int titlelen; |
| 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 | |
| 749 | title = pdf_outline_convert(items->pdata->outline_title, &titlelen); |
| 750 | |
| 751 | totalcount++; |
| 752 | curr = new_object(parent->list); |
| 753 | if (!first) first = curr; |
| 754 | last = curr; |
| 755 | objtext(curr, "<<\n/Title "); |
| 756 | pdf_string_len(objtext, curr, title, titlelen); |
| 757 | sfree(title); |
| 758 | objtext(curr, "\n/Parent "); |
| 759 | objref(curr, parent); |
| 760 | objtext(curr, "\n/Dest ["); |
| 761 | objref(curr, (object *)items->pdata->first->page->spare); |
| 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 | } |
| 803 | |
| 804 | static 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: |
| 828 | text = utoa_dup(words->text, CS_ASCII); |
| 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 | } |
| 847 | |
| 848 | static void pdf_string_len(void (*add)(object *, char const *), |
| 849 | object *o, char const *str, int len) |
| 850 | { |
| 851 | char const *p; |
| 852 | |
| 853 | add(o, "("); |
| 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 | } |
| 865 | add(o, c); |
| 866 | } |
| 867 | add(o, ")"); |
| 868 | } |
| 869 | |
| 870 | static 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 | } |