43341922 |
1 | /* |
2 | * PDF backend for Halibut |
3 | */ |
4 | |
5 | #include <assert.h> |
6 | #include "halibut.h" |
7 | #include "paper.h" |
8 | |
9 | #define TREE_BRANCH 2 /* max branching factor in page tree */ |
10 | |
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; |
23765aeb |
201 | rect *r; |
43341922 |
202 | text_fragment *frag; |
203 | char buf[256]; |
204 | |
205 | opage = (object *)page->spare; |
206 | /* |
207 | * At this point the page dictionary is already |
208 | * half-written, with /Type and /Parent already present. We |
209 | * continue from there. |
210 | */ |
211 | |
212 | /* |
213 | * The PDF spec says /Resources is required, but also says |
214 | * that it's inheritable and may be omitted if it's present |
215 | * in a Pages node. In our case it is: it's present in the |
216 | * topmost /Pages node because we carefully put it there. |
217 | * So we don't need a /Resources entry here. |
218 | */ |
219 | sprintf(buf, "/MediaBox [0 0 %g %g]\n", |
220 | doc->paper_width / 4096.0, doc->paper_height / 4096.0); |
221 | objtext(opage, buf); |
222 | |
223 | /* |
224 | * Now we're ready to define a content stream containing |
225 | * the actual text on the page. |
226 | */ |
227 | cstr = new_object(&olist); |
228 | objtext(opage, "/Contents "); |
229 | objref(opage, cstr); |
230 | objtext(opage, "\n"); |
231 | |
23765aeb |
232 | /* |
233 | * Render any rectangles on the page. |
234 | */ |
235 | for (r = page->first_rect; r; r = r->next) { |
236 | char buf[512]; |
237 | sprintf(buf, "%g %g %g %g re f\n", r->x / 4096.0, |
238 | r->y / 4096.0, r->w / 4096.0, r->h / 4096.0); |
239 | objstream(cstr, buf); |
240 | } |
241 | |
43341922 |
242 | objstream(cstr, "BT\n"); |
243 | for (frag = page->first_text; frag; frag = frag->next) { |
244 | char *c; |
245 | |
246 | objstream(cstr, "/"); |
247 | objstream(cstr, frag->fe->name); |
248 | sprintf(buf, " %d Tf 1 0 0 1 %g %g Tm (", frag->fontsize, |
249 | frag->x/4096.0, frag->y/4096.0); |
250 | objstream(cstr, buf); |
251 | |
252 | for (c = frag->text; *c; c++) { |
253 | if (*c == '(' || *c == ')' || *c == '\\') |
254 | objstream(cstr, "\\"); |
255 | buf[0] = *c; |
256 | buf[1] = '\0'; |
257 | objstream(cstr, buf); |
258 | } |
259 | |
260 | objstream(cstr, ") Tj\n"); |
261 | } |
262 | objstream(cstr, "ET"); |
263 | |
138d7ffb |
264 | /* |
265 | * Also, we want an annotation dictionary containing the |
266 | * cross-references from this page. |
267 | */ |
268 | if (page->first_xref) { |
269 | xref *xr; |
270 | objtext(opage, "/Annots [\n"); |
271 | |
272 | for (xr = page->first_xref; xr; xr = xr->next) { |
273 | object *annot; |
274 | char buf[256]; |
275 | |
276 | annot = new_object(&olist); |
277 | objref(opage, annot); |
278 | objtext(opage, "\n"); |
279 | |
280 | objtext(annot, "<<\n/Type /Annot\n/Subtype /Link\n/Rect ["); |
281 | sprintf(buf, "%g %g %g %g", |
282 | xr->lx / 4096.0, xr->by / 4096.0, |
283 | xr->rx / 4096.0, xr->ty / 4096.0); |
284 | objtext(annot, buf); |
285 | objtext(annot, "]\n/Border [0 0 0]\n"); |
286 | |
287 | if (xr->dest.type == PAGE) { |
288 | objtext(annot, "/Dest ["); |
289 | objref(annot, (object *)xr->dest.page->spare); |
290 | objtext(annot, " /XYZ null null null]\n"); |
291 | } else { |
292 | char *p; |
293 | |
294 | objtext(annot, "/A <<\n/Type /Action\n/S /URI\n/URI ("); |
295 | for (p = xr->dest.url; *p; p++) { |
296 | char c[2]; |
297 | c[0] = *p; |
298 | c[1] = '\0'; |
299 | if (*p == '(' || *p == ')' || *p == '\\') |
300 | objtext(annot, "\\"); |
301 | objtext(annot, c); |
302 | } |
303 | objtext(annot, ")\n>>\n"); |
304 | } |
305 | |
306 | objtext(annot, ">>\n"); |
307 | } |
308 | |
309 | objtext(opage, "]\n"); |
310 | } |
311 | |
43341922 |
312 | objtext(opage, ">>\n"); |
313 | } |
314 | |
315 | /* |
316 | * Assemble the final linear form of every object. |
317 | */ |
318 | for (o = olist.head; o; o = o->next) { |
319 | rdstringc rs = {0, 0, NULL}; |
320 | char text[80]; |
321 | |
322 | sprintf(text, "%d 0 obj\n", o->number); |
323 | rdaddsc(&rs, text); |
324 | |
325 | if (!o->main.text && o->stream.text) { |
326 | sprintf(text, "<<\n/Length %d\n>>\n", o->stream.pos); |
327 | rdaddsc(&o->main, text); |
328 | } |
329 | |
330 | assert(o->main.text); |
331 | rdaddsc(&rs, o->main.text); |
332 | sfree(o->main.text); |
333 | |
334 | if (rs.text[rs.pos-1] != '\n') |
335 | rdaddc(&rs, '\n'); |
336 | |
337 | if (o->stream.text) { |
338 | /* |
339 | * FIXME: If we ever start compressing stream data then |
340 | * it will have zero bytes in it, so we'll have to be |
341 | * more careful than this. |
342 | */ |
343 | rdaddsc(&rs, "stream\n"); |
344 | rdaddsc(&rs, o->stream.text); |
345 | rdaddsc(&rs, "\nendstream\n"); |
346 | sfree(o->stream.text); |
347 | } |
348 | |
349 | rdaddsc(&rs, "endobj\n"); |
350 | |
351 | o->final = rs.text; |
352 | o->size = rs.pos; |
353 | } |
354 | |
355 | /* |
356 | * Write out the PDF file. |
357 | */ |
358 | |
359 | fp = fopen(filename, "wb"); |
360 | if (!fp) { |
361 | error(err_cantopenw, filename); |
362 | return; |
363 | } |
364 | |
365 | /* |
366 | * Header |
367 | */ |
368 | fileoff = fprintf(fp, "%%PDF-1.3\n"); |
369 | |
370 | /* |
371 | * Body |
372 | */ |
373 | for (o = olist.head; o; o = o->next) { |
374 | o->fileoff = fileoff; |
375 | fwrite(o->final, 1, o->size, fp); |
376 | fileoff += o->size; |
377 | } |
378 | |
379 | /* |
380 | * Cross-reference table |
381 | */ |
382 | fprintf(fp, "xref\n"); |
383 | assert(olist.head->number == 1); |
384 | fprintf(fp, "0 %d\n", olist.tail->number + 1); |
385 | fprintf(fp, "0000000000 65535 f \n"); |
386 | for (o = olist.head; o; o = o->next) { |
387 | char entry[40]; |
388 | sprintf(entry, "%010d 00000 n \n", o->fileoff); |
389 | assert(strlen(entry) == 20); |
390 | fputs(entry, fp); |
391 | } |
392 | |
393 | /* |
394 | * Trailer |
395 | */ |
396 | fprintf(fp, "trailer\n<<\n/Size %d\n/Root %d 0 R\n>>\n", |
397 | olist.tail->number + 1, cat->number); |
398 | fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff); |
399 | |
400 | fclose(fp); |
401 | |
402 | sfree(filename); |
403 | } |
404 | |
405 | static object *new_object(objlist *list) |
406 | { |
407 | object *obj = mknew(object); |
408 | |
409 | obj->list = list; |
410 | |
411 | obj->main.text = NULL; |
412 | obj->main.pos = obj->main.size = 0; |
413 | obj->stream.text = NULL; |
414 | obj->stream.pos = obj->stream.size = 0; |
415 | |
416 | obj->number = list->number++; |
417 | |
418 | obj->next = NULL; |
419 | if (list->tail) |
420 | list->tail->next = obj; |
421 | else |
422 | list->head = obj; |
423 | list->tail = obj; |
424 | |
425 | obj->size = 0; |
426 | obj->final = NULL; |
427 | |
428 | return obj; |
429 | } |
430 | |
431 | static void objtext(object *o, char const *text) |
432 | { |
433 | rdaddsc(&o->main, text); |
434 | } |
435 | |
436 | static void objstream(object *o, char const *text) |
437 | { |
438 | rdaddsc(&o->stream, text); |
439 | } |
440 | |
441 | static void objref(object *o, object *dest) |
442 | { |
443 | char buf[40]; |
444 | sprintf(buf, "%d 0 R", dest->number); |
445 | rdaddsc(&o->main, buf); |
446 | } |
447 | |
448 | static void make_pages_node(object *node, object *parent, page_data *first, |
449 | page_data *last, object *resources) |
450 | { |
451 | int count; |
452 | page_data *page; |
453 | char buf[80]; |
454 | |
455 | objtext(node, "<<\n/Type /Pages\n"); |
456 | if (parent) { |
457 | objtext(node, "/Parent "); |
458 | objref(node, parent); |
459 | objtext(node, "\n"); |
460 | } |
461 | |
462 | /* |
463 | * Count the pages in this stretch, to see if there are few |
464 | * enough to reference directly. |
465 | */ |
466 | count = 0; |
467 | for (page = first; page; page = page->next) { |
468 | count++; |
469 | if (page == last) |
470 | break; |
471 | } |
472 | |
473 | sprintf(buf, "/Count %d\n/Kids [\n", count); |
474 | objtext(node, buf); |
475 | |
476 | if (count > TREE_BRANCH) { |
477 | int i; |
478 | page_data *thisfirst, *thislast; |
479 | |
480 | page = first; |
481 | |
482 | for (i = 0; i < TREE_BRANCH; i++) { |
483 | int number = (i+1) * count / TREE_BRANCH - i * count / TREE_BRANCH; |
484 | thisfirst = page; |
485 | while (number--) { |
486 | thislast = page; |
487 | page = page->next; |
488 | } |
489 | |
490 | if (thisfirst == thislast) { |
491 | objref(node, (object *)thisfirst->spare); |
492 | objtext((object *)thisfirst->spare, "/Parent "); |
493 | objref((object *)thisfirst->spare, node); |
494 | objtext((object *)thisfirst->spare, "\n"); |
495 | } else { |
496 | object *newnode = new_object(node->list); |
497 | make_pages_node(newnode, node, thisfirst, thislast, NULL); |
498 | objref(node, newnode); |
499 | } |
500 | objtext(node, "\n"); |
501 | } |
502 | |
503 | assert(thislast == last || page == NULL); |
504 | |
505 | } else { |
506 | for (page = first; page; page = page->next) { |
507 | objref(node, (object *)page->spare); |
508 | objtext(node, "\n"); |
509 | objtext((object *)page->spare, "/Parent "); |
510 | objref((object *)page->spare, node); |
511 | objtext((object *)page->spare, "\n"); |
512 | if (page == last) |
513 | break; |
514 | } |
515 | } |
516 | |
517 | objtext(node, "]\n"); |
518 | |
519 | if (resources) { |
520 | objtext(node, "/Resources "); |
521 | objref(node, resources); |
522 | objtext(node, "\n"); |
523 | } |
524 | |
525 | objtext(node, ">>\n"); |
526 | } |