| 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 | paragraph *p; |
| 14 | wchar_t *ufilename, *up; |
| 15 | int len; |
| 16 | |
| 17 | p = mknew(paragraph); |
| 18 | memset(p, 0, sizeof(*p)); |
| 19 | p->type = para_Config; |
| 20 | p->next = NULL; |
| 21 | p->fpos.filename = "<command line>"; |
| 22 | p->fpos.line = p->fpos.col = -1; |
| 23 | |
| 24 | ufilename = ufroma_dup(filename); |
| 25 | len = ustrlen(ufilename) + 2 + lenof(L"pdf-filename"); |
| 26 | p->keyword = mknewa(wchar_t, len); |
| 27 | up = p->keyword; |
| 28 | ustrcpy(up, L"pdf-filename"); |
| 29 | up = uadv(up); |
| 30 | ustrcpy(up, ufilename); |
| 31 | up = uadv(up); |
| 32 | *up = L'\0'; |
| 33 | assert(up - p->keyword < len); |
| 34 | sfree(ufilename); |
| 35 | |
| 36 | return p; |
| 37 | } |
| 38 | |
| 39 | typedef struct object_Tag object; |
| 40 | typedef struct objlist_Tag objlist; |
| 41 | |
| 42 | struct object_Tag { |
| 43 | objlist *list; |
| 44 | object *next; |
| 45 | int number; |
| 46 | rdstringc main, stream; |
| 47 | int size, fileoff; |
| 48 | char *final; |
| 49 | }; |
| 50 | |
| 51 | struct objlist_Tag { |
| 52 | int number; |
| 53 | object *head, *tail; |
| 54 | }; |
| 55 | |
| 56 | static object *new_object(objlist *list); |
| 57 | static void objtext(object *o, char const *text); |
| 58 | static void objstream(object *o, char const *text); |
| 59 | static void objref(object *o, object *dest); |
| 60 | |
| 61 | static void make_pages_node(object *node, object *parent, page_data *first, |
| 62 | page_data *last, object *resources); |
| 63 | |
| 64 | void pdf_backend(paragraph *sourceform, keywordlist *keywords, |
| 65 | indexdata *idx, void *vdoc) { |
| 66 | document *doc = (document *)vdoc; |
| 67 | int font_index; |
| 68 | font_encoding *fe; |
| 69 | page_data *page; |
| 70 | int pageno; |
| 71 | FILE *fp; |
| 72 | char *filename; |
| 73 | paragraph *p; |
| 74 | objlist olist; |
| 75 | object *o, *cat, *outlines, *pages, *resources; |
| 76 | int fileoff; |
| 77 | |
| 78 | IGNORE(keywords); |
| 79 | IGNORE(idx); |
| 80 | |
| 81 | filename = dupstr("output.pdf"); |
| 82 | for (p = sourceform; p; p = p->next) { |
| 83 | p->private_data = NULL; |
| 84 | if (p->type == para_Config && p->parent) { |
| 85 | if (!ustricmp(p->keyword, L"pdf-filename")) { |
| 86 | sfree(filename); |
| 87 | filename = utoa_dup(uadv(p->keyword)); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | olist.head = olist.tail = NULL; |
| 93 | olist.number = 1; |
| 94 | |
| 95 | cat = new_object(&olist); |
| 96 | outlines = new_object(&olist); |
| 97 | pages = new_object(&olist); |
| 98 | resources = new_object(&olist); |
| 99 | |
| 100 | /* |
| 101 | * We currently don't support outlines, so here's a null |
| 102 | * outlines dictionary. |
| 103 | */ |
| 104 | objtext(outlines, "<<\n/Type Outlines\n/Count 0\n>>\n"); |
| 105 | |
| 106 | /* |
| 107 | * The catalogue just contains references to the outlines and |
| 108 | * pages objects. |
| 109 | */ |
| 110 | objtext(cat, "<<\n/Type /Catalog\n/Outlines "); |
| 111 | objref(cat, outlines); |
| 112 | objtext(cat, "\n/Pages "); |
| 113 | objref(cat, pages); |
| 114 | objtext(cat, "\n>>\n"); |
| 115 | |
| 116 | /* |
| 117 | * Set up the resources dictionary, which mostly means |
| 118 | * providing all the font objects and names to call them by. |
| 119 | */ |
| 120 | font_index = 0; |
| 121 | objtext(resources, "<<\n/Font <<\n"); |
| 122 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
| 123 | char fname[40]; |
| 124 | int i; |
| 125 | object *font; |
| 126 | |
| 127 | sprintf(fname, "f%d", font_index++); |
| 128 | fe->name = dupstr(fname); |
| 129 | |
| 130 | font = new_object(&olist); |
| 131 | |
| 132 | objtext(resources, "/"); |
| 133 | objtext(resources, fe->name); |
| 134 | objtext(resources, " "); |
| 135 | objref(resources, font); |
| 136 | objtext(resources, "\n"); |
| 137 | |
| 138 | objtext(font, "<<\n/Type /Font\n/Subtype /Type1\n/Name /"); |
| 139 | objtext(font, fe->name); |
| 140 | objtext(font, "\n/BaseFont /"); |
| 141 | objtext(font, fe->font->name); |
| 142 | objtext(font, "\n/Encoding <<\n/Type /Encoding\n/Differences ["); |
| 143 | |
| 144 | for (i = 0; i < 256; i++) { |
| 145 | char buf[20]; |
| 146 | if (!fe->vector[i]) |
| 147 | continue; |
| 148 | sprintf(buf, "\n%d /", i); |
| 149 | objtext(font, buf); |
| 150 | objtext(font, fe->vector[i] ? fe->vector[i] : ".notdef"); |
| 151 | } |
| 152 | |
| 153 | objtext(font, "\n]\n>>\n"); |
| 154 | |
| 155 | { |
| 156 | object *widths = new_object(&olist); |
| 157 | objtext(font, "/FirstChar 0\n/LastChar 255\n/Widths "); |
| 158 | objref(font, widths); |
| 159 | objtext(font, "\n"); |
| 160 | objtext(widths, "[\n"); |
| 161 | for (i = 0; i < 256; i++) { |
| 162 | char buf[80]; |
| 163 | double width; |
| 164 | if (fe->indices[i] < 0) |
| 165 | width = 0.0; |
| 166 | else |
| 167 | width = fe->font->widths[fe->indices[i]]; |
| 168 | sprintf(buf, "%g\n", 1000.0 * width / 4096.0); |
| 169 | objtext(widths, buf); |
| 170 | } |
| 171 | objtext(widths, "]\n"); |
| 172 | } |
| 173 | |
| 174 | objtext(font, ">>\n"); |
| 175 | } |
| 176 | objtext(resources, ">>\n>>\n"); |
| 177 | |
| 178 | /* |
| 179 | * Define the page objects for each page, and get each one |
| 180 | * ready to have a `Parent' specification added to it. |
| 181 | */ |
| 182 | for (page = doc->pages; page; page = page->next) { |
| 183 | object *opage; |
| 184 | |
| 185 | opage = new_object(&olist); |
| 186 | page->spare = opage; |
| 187 | objtext(opage, "<<\n/Type /Page\n"); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Recursively build the page tree. |
| 192 | */ |
| 193 | make_pages_node(pages, NULL, doc->pages, NULL, resources); |
| 194 | |
| 195 | /* |
| 196 | * Create and render the individual pages. |
| 197 | */ |
| 198 | pageno = 0; |
| 199 | for (page = doc->pages; page; page = page->next) { |
| 200 | object *opage, *cstr; |
| 201 | text_fragment *frag; |
| 202 | char buf[256]; |
| 203 | |
| 204 | opage = (object *)page->spare; |
| 205 | /* |
| 206 | * At this point the page dictionary is already |
| 207 | * half-written, with /Type and /Parent already present. We |
| 208 | * continue from there. |
| 209 | */ |
| 210 | |
| 211 | /* |
| 212 | * The PDF spec says /Resources is required, but also says |
| 213 | * that it's inheritable and may be omitted if it's present |
| 214 | * in a Pages node. In our case it is: it's present in the |
| 215 | * topmost /Pages node because we carefully put it there. |
| 216 | * So we don't need a /Resources entry here. |
| 217 | */ |
| 218 | sprintf(buf, "/MediaBox [0 0 %g %g]\n", |
| 219 | doc->paper_width / 4096.0, doc->paper_height / 4096.0); |
| 220 | objtext(opage, buf); |
| 221 | |
| 222 | /* |
| 223 | * Now we're ready to define a content stream containing |
| 224 | * the actual text on the page. |
| 225 | */ |
| 226 | cstr = new_object(&olist); |
| 227 | objtext(opage, "/Contents "); |
| 228 | objref(opage, cstr); |
| 229 | objtext(opage, "\n"); |
| 230 | |
| 231 | objstream(cstr, "BT\n"); |
| 232 | for (frag = page->first_text; frag; frag = frag->next) { |
| 233 | char *c; |
| 234 | |
| 235 | objstream(cstr, "/"); |
| 236 | objstream(cstr, frag->fe->name); |
| 237 | sprintf(buf, " %d Tf 1 0 0 1 %g %g Tm (", frag->fontsize, |
| 238 | frag->x/4096.0, frag->y/4096.0); |
| 239 | objstream(cstr, buf); |
| 240 | |
| 241 | for (c = frag->text; *c; c++) { |
| 242 | if (*c == '(' || *c == ')' || *c == '\\') |
| 243 | objstream(cstr, "\\"); |
| 244 | buf[0] = *c; |
| 245 | buf[1] = '\0'; |
| 246 | objstream(cstr, buf); |
| 247 | } |
| 248 | |
| 249 | objstream(cstr, ") Tj\n"); |
| 250 | } |
| 251 | objstream(cstr, "ET"); |
| 252 | |
| 253 | /* |
| 254 | * Also, we want an annotation dictionary containing the |
| 255 | * cross-references from this page. |
| 256 | */ |
| 257 | if (page->first_xref) { |
| 258 | xref *xr; |
| 259 | objtext(opage, "/Annots [\n"); |
| 260 | |
| 261 | for (xr = page->first_xref; xr; xr = xr->next) { |
| 262 | object *annot; |
| 263 | char buf[256]; |
| 264 | |
| 265 | annot = new_object(&olist); |
| 266 | objref(opage, annot); |
| 267 | objtext(opage, "\n"); |
| 268 | |
| 269 | objtext(annot, "<<\n/Type /Annot\n/Subtype /Link\n/Rect ["); |
| 270 | sprintf(buf, "%g %g %g %g", |
| 271 | xr->lx / 4096.0, xr->by / 4096.0, |
| 272 | xr->rx / 4096.0, xr->ty / 4096.0); |
| 273 | objtext(annot, buf); |
| 274 | objtext(annot, "]\n/Border [0 0 0]\n"); |
| 275 | |
| 276 | if (xr->dest.type == PAGE) { |
| 277 | objtext(annot, "/Dest ["); |
| 278 | objref(annot, (object *)xr->dest.page->spare); |
| 279 | objtext(annot, " /XYZ null null null]\n"); |
| 280 | } else { |
| 281 | char *p; |
| 282 | |
| 283 | objtext(annot, "/A <<\n/Type /Action\n/S /URI\n/URI ("); |
| 284 | for (p = xr->dest.url; *p; p++) { |
| 285 | char c[2]; |
| 286 | c[0] = *p; |
| 287 | c[1] = '\0'; |
| 288 | if (*p == '(' || *p == ')' || *p == '\\') |
| 289 | objtext(annot, "\\"); |
| 290 | objtext(annot, c); |
| 291 | } |
| 292 | objtext(annot, ")\n>>\n"); |
| 293 | } |
| 294 | |
| 295 | objtext(annot, ">>\n"); |
| 296 | } |
| 297 | |
| 298 | objtext(opage, "]\n"); |
| 299 | } |
| 300 | |
| 301 | objtext(opage, ">>\n"); |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Assemble the final linear form of every object. |
| 306 | */ |
| 307 | for (o = olist.head; o; o = o->next) { |
| 308 | rdstringc rs = {0, 0, NULL}; |
| 309 | char text[80]; |
| 310 | |
| 311 | sprintf(text, "%d 0 obj\n", o->number); |
| 312 | rdaddsc(&rs, text); |
| 313 | |
| 314 | if (!o->main.text && o->stream.text) { |
| 315 | sprintf(text, "<<\n/Length %d\n>>\n", o->stream.pos); |
| 316 | rdaddsc(&o->main, text); |
| 317 | } |
| 318 | |
| 319 | assert(o->main.text); |
| 320 | rdaddsc(&rs, o->main.text); |
| 321 | sfree(o->main.text); |
| 322 | |
| 323 | if (rs.text[rs.pos-1] != '\n') |
| 324 | rdaddc(&rs, '\n'); |
| 325 | |
| 326 | if (o->stream.text) { |
| 327 | /* |
| 328 | * FIXME: If we ever start compressing stream data then |
| 329 | * it will have zero bytes in it, so we'll have to be |
| 330 | * more careful than this. |
| 331 | */ |
| 332 | rdaddsc(&rs, "stream\n"); |
| 333 | rdaddsc(&rs, o->stream.text); |
| 334 | rdaddsc(&rs, "\nendstream\n"); |
| 335 | sfree(o->stream.text); |
| 336 | } |
| 337 | |
| 338 | rdaddsc(&rs, "endobj\n"); |
| 339 | |
| 340 | o->final = rs.text; |
| 341 | o->size = rs.pos; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Write out the PDF file. |
| 346 | */ |
| 347 | |
| 348 | fp = fopen(filename, "wb"); |
| 349 | if (!fp) { |
| 350 | error(err_cantopenw, filename); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Header |
| 356 | */ |
| 357 | fileoff = fprintf(fp, "%%PDF-1.3\n"); |
| 358 | |
| 359 | /* |
| 360 | * Body |
| 361 | */ |
| 362 | for (o = olist.head; o; o = o->next) { |
| 363 | o->fileoff = fileoff; |
| 364 | fwrite(o->final, 1, o->size, fp); |
| 365 | fileoff += o->size; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Cross-reference table |
| 370 | */ |
| 371 | fprintf(fp, "xref\n"); |
| 372 | assert(olist.head->number == 1); |
| 373 | fprintf(fp, "0 %d\n", olist.tail->number + 1); |
| 374 | fprintf(fp, "0000000000 65535 f \n"); |
| 375 | for (o = olist.head; o; o = o->next) { |
| 376 | char entry[40]; |
| 377 | sprintf(entry, "%010d 00000 n \n", o->fileoff); |
| 378 | assert(strlen(entry) == 20); |
| 379 | fputs(entry, fp); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Trailer |
| 384 | */ |
| 385 | fprintf(fp, "trailer\n<<\n/Size %d\n/Root %d 0 R\n>>\n", |
| 386 | olist.tail->number + 1, cat->number); |
| 387 | fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff); |
| 388 | |
| 389 | fclose(fp); |
| 390 | |
| 391 | sfree(filename); |
| 392 | } |
| 393 | |
| 394 | static object *new_object(objlist *list) |
| 395 | { |
| 396 | object *obj = mknew(object); |
| 397 | |
| 398 | obj->list = list; |
| 399 | |
| 400 | obj->main.text = NULL; |
| 401 | obj->main.pos = obj->main.size = 0; |
| 402 | obj->stream.text = NULL; |
| 403 | obj->stream.pos = obj->stream.size = 0; |
| 404 | |
| 405 | obj->number = list->number++; |
| 406 | |
| 407 | obj->next = NULL; |
| 408 | if (list->tail) |
| 409 | list->tail->next = obj; |
| 410 | else |
| 411 | list->head = obj; |
| 412 | list->tail = obj; |
| 413 | |
| 414 | obj->size = 0; |
| 415 | obj->final = NULL; |
| 416 | |
| 417 | return obj; |
| 418 | } |
| 419 | |
| 420 | static void objtext(object *o, char const *text) |
| 421 | { |
| 422 | rdaddsc(&o->main, text); |
| 423 | } |
| 424 | |
| 425 | static void objstream(object *o, char const *text) |
| 426 | { |
| 427 | rdaddsc(&o->stream, text); |
| 428 | } |
| 429 | |
| 430 | static void objref(object *o, object *dest) |
| 431 | { |
| 432 | char buf[40]; |
| 433 | sprintf(buf, "%d 0 R", dest->number); |
| 434 | rdaddsc(&o->main, buf); |
| 435 | } |
| 436 | |
| 437 | static void make_pages_node(object *node, object *parent, page_data *first, |
| 438 | page_data *last, object *resources) |
| 439 | { |
| 440 | int count; |
| 441 | page_data *page; |
| 442 | char buf[80]; |
| 443 | |
| 444 | objtext(node, "<<\n/Type /Pages\n"); |
| 445 | if (parent) { |
| 446 | objtext(node, "/Parent "); |
| 447 | objref(node, parent); |
| 448 | objtext(node, "\n"); |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Count the pages in this stretch, to see if there are few |
| 453 | * enough to reference directly. |
| 454 | */ |
| 455 | count = 0; |
| 456 | for (page = first; page; page = page->next) { |
| 457 | count++; |
| 458 | if (page == last) |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | sprintf(buf, "/Count %d\n/Kids [\n", count); |
| 463 | objtext(node, buf); |
| 464 | |
| 465 | if (count > TREE_BRANCH) { |
| 466 | int i; |
| 467 | page_data *thisfirst, *thislast; |
| 468 | |
| 469 | page = first; |
| 470 | |
| 471 | for (i = 0; i < TREE_BRANCH; i++) { |
| 472 | int number = (i+1) * count / TREE_BRANCH - i * count / TREE_BRANCH; |
| 473 | thisfirst = page; |
| 474 | while (number--) { |
| 475 | thislast = page; |
| 476 | page = page->next; |
| 477 | } |
| 478 | |
| 479 | if (thisfirst == thislast) { |
| 480 | objref(node, (object *)thisfirst->spare); |
| 481 | objtext((object *)thisfirst->spare, "/Parent "); |
| 482 | objref((object *)thisfirst->spare, node); |
| 483 | objtext((object *)thisfirst->spare, "\n"); |
| 484 | } else { |
| 485 | object *newnode = new_object(node->list); |
| 486 | make_pages_node(newnode, node, thisfirst, thislast, NULL); |
| 487 | objref(node, newnode); |
| 488 | } |
| 489 | objtext(node, "\n"); |
| 490 | } |
| 491 | |
| 492 | assert(thislast == last || page == NULL); |
| 493 | |
| 494 | } else { |
| 495 | for (page = first; page; page = page->next) { |
| 496 | objref(node, (object *)page->spare); |
| 497 | objtext(node, "\n"); |
| 498 | objtext((object *)page->spare, "/Parent "); |
| 499 | objref((object *)page->spare, node); |
| 500 | objtext((object *)page->spare, "\n"); |
| 501 | if (page == last) |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | objtext(node, "]\n"); |
| 507 | |
| 508 | if (resources) { |
| 509 | objtext(node, "/Resources "); |
| 510 | objref(node, resources); |
| 511 | objtext(node, "\n"); |
| 512 | } |
| 513 | |
| 514 | objtext(node, ">>\n"); |
| 515 | } |