Initial work on PS and PDF output. Because these two backends share
[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"
8
9#define TREE_BRANCH 2 /* max branching factor in page tree */
10
11paragraph *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
39typedef struct object_Tag object;
40typedef struct objlist_Tag objlist;
41
42struct object_Tag {
43 objlist *list;
44 object *next;
45 int number;
46 rdstringc main, stream;
47 int size, fileoff;
48 char *final;
49};
50
51struct objlist_Tag {
52 int number;
53 object *head, *tail;
54};
55
56static object *new_object(objlist *list);
57static void objtext(object *o, char const *text);
58static void objstream(object *o, char const *text);
59static void objref(object *o, object *dest);
60
61static void make_pages_node(object *node, object *parent, page_data *first,
62 page_data *last, object *resources);
63
64void 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 objtext(opage, ">>\n");
254 }
255
256 /*
257 * Assemble the final linear form of every object.
258 */
259 for (o = olist.head; o; o = o->next) {
260 rdstringc rs = {0, 0, NULL};
261 char text[80];
262
263 sprintf(text, "%d 0 obj\n", o->number);
264 rdaddsc(&rs, text);
265
266 if (!o->main.text && o->stream.text) {
267 sprintf(text, "<<\n/Length %d\n>>\n", o->stream.pos);
268 rdaddsc(&o->main, text);
269 }
270
271 assert(o->main.text);
272 rdaddsc(&rs, o->main.text);
273 sfree(o->main.text);
274
275 if (rs.text[rs.pos-1] != '\n')
276 rdaddc(&rs, '\n');
277
278 if (o->stream.text) {
279 /*
280 * FIXME: If we ever start compressing stream data then
281 * it will have zero bytes in it, so we'll have to be
282 * more careful than this.
283 */
284 rdaddsc(&rs, "stream\n");
285 rdaddsc(&rs, o->stream.text);
286 rdaddsc(&rs, "\nendstream\n");
287 sfree(o->stream.text);
288 }
289
290 rdaddsc(&rs, "endobj\n");
291
292 o->final = rs.text;
293 o->size = rs.pos;
294 }
295
296 /*
297 * Write out the PDF file.
298 */
299
300 fp = fopen(filename, "wb");
301 if (!fp) {
302 error(err_cantopenw, filename);
303 return;
304 }
305
306 /*
307 * Header
308 */
309 fileoff = fprintf(fp, "%%PDF-1.3\n");
310
311 /*
312 * Body
313 */
314 for (o = olist.head; o; o = o->next) {
315 o->fileoff = fileoff;
316 fwrite(o->final, 1, o->size, fp);
317 fileoff += o->size;
318 }
319
320 /*
321 * Cross-reference table
322 */
323 fprintf(fp, "xref\n");
324 assert(olist.head->number == 1);
325 fprintf(fp, "0 %d\n", olist.tail->number + 1);
326 fprintf(fp, "0000000000 65535 f \n");
327 for (o = olist.head; o; o = o->next) {
328 char entry[40];
329 sprintf(entry, "%010d 00000 n \n", o->fileoff);
330 assert(strlen(entry) == 20);
331 fputs(entry, fp);
332 }
333
334 /*
335 * Trailer
336 */
337 fprintf(fp, "trailer\n<<\n/Size %d\n/Root %d 0 R\n>>\n",
338 olist.tail->number + 1, cat->number);
339 fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff);
340
341 fclose(fp);
342
343 sfree(filename);
344}
345
346static object *new_object(objlist *list)
347{
348 object *obj = mknew(object);
349
350 obj->list = list;
351
352 obj->main.text = NULL;
353 obj->main.pos = obj->main.size = 0;
354 obj->stream.text = NULL;
355 obj->stream.pos = obj->stream.size = 0;
356
357 obj->number = list->number++;
358
359 obj->next = NULL;
360 if (list->tail)
361 list->tail->next = obj;
362 else
363 list->head = obj;
364 list->tail = obj;
365
366 obj->size = 0;
367 obj->final = NULL;
368
369 return obj;
370}
371
372static void objtext(object *o, char const *text)
373{
374 rdaddsc(&o->main, text);
375}
376
377static void objstream(object *o, char const *text)
378{
379 rdaddsc(&o->stream, text);
380}
381
382static void objref(object *o, object *dest)
383{
384 char buf[40];
385 sprintf(buf, "%d 0 R", dest->number);
386 rdaddsc(&o->main, buf);
387}
388
389static void make_pages_node(object *node, object *parent, page_data *first,
390 page_data *last, object *resources)
391{
392 int count;
393 page_data *page;
394 char buf[80];
395
396 objtext(node, "<<\n/Type /Pages\n");
397 if (parent) {
398 objtext(node, "/Parent ");
399 objref(node, parent);
400 objtext(node, "\n");
401 }
402
403 /*
404 * Count the pages in this stretch, to see if there are few
405 * enough to reference directly.
406 */
407 count = 0;
408 for (page = first; page; page = page->next) {
409 count++;
410 if (page == last)
411 break;
412 }
413
414 sprintf(buf, "/Count %d\n/Kids [\n", count);
415 objtext(node, buf);
416
417 if (count > TREE_BRANCH) {
418 int i;
419 page_data *thisfirst, *thislast;
420
421 page = first;
422
423 for (i = 0; i < TREE_BRANCH; i++) {
424 int number = (i+1) * count / TREE_BRANCH - i * count / TREE_BRANCH;
425 thisfirst = page;
426 while (number--) {
427 thislast = page;
428 page = page->next;
429 }
430
431 if (thisfirst == thislast) {
432 objref(node, (object *)thisfirst->spare);
433 objtext((object *)thisfirst->spare, "/Parent ");
434 objref((object *)thisfirst->spare, node);
435 objtext((object *)thisfirst->spare, "\n");
436 } else {
437 object *newnode = new_object(node->list);
438 make_pages_node(newnode, node, thisfirst, thislast, NULL);
439 objref(node, newnode);
440 }
441 objtext(node, "\n");
442 }
443
444 assert(thislast == last || page == NULL);
445
446 } else {
447 for (page = first; page; page = page->next) {
448 objref(node, (object *)page->spare);
449 objtext(node, "\n");
450 objtext((object *)page->spare, "/Parent ");
451 objref((object *)page->spare, node);
452 objtext((object *)page->spare, "\n");
453 if (page == last)
454 break;
455 }
456 }
457
458 objtext(node, "]\n");
459
460 if (resources) {
461 objtext(node, "/Resources ");
462 objref(node, resources);
463 objtext(node, "\n");
464 }
465
466 objtext(node, ">>\n");
467}