Remove accidentally-committed deflate-debugging code.
[sgt/halibut] / bk_ps.c
CommitLineData
43341922 1/*
2 * PostScript backend for Halibut
3 */
4
5#include <assert.h>
6#include "halibut.h"
7#include "paper.h"
8
b0f1f943 9static void ps_comment(FILE *fp, char const *leader, word *words);
f8194b21 10static void ps_string_len(FILE *fp, char const *str, int len);
3f167769 11static void ps_string(FILE *fp, char const *str);
09358aa7 12
43341922 13paragraph *ps_config_filename(char *filename)
14{
e4ea58f8 15 return cmdline_cfg_simple("ps-filename", filename, NULL);
43341922 16}
17
18void ps_backend(paragraph *sourceform, keywordlist *keywords,
19 indexdata *idx, void *vdoc) {
20 document *doc = (document *)vdoc;
21 int font_index;
22 font_encoding *fe;
23 page_data *page;
24 int pageno;
25 FILE *fp;
26 char *filename;
27 paragraph *p;
f8194b21 28 outline_element *oe;
29 int noe;
30
43341922 31
32 IGNORE(keywords);
33 IGNORE(idx);
34
35 filename = dupstr("output.ps");
36 for (p = sourceform; p; p = p->next) {
7738a2fb 37 if (p->type == para_Config) {
43341922 38 if (!ustricmp(p->keyword, L"ps-filename")) {
39 sfree(filename);
e4ea58f8 40 filename = dupstr(adv(p->origkeyword));
43341922 41 }
42 }
43 }
44
45 fp = fopen(filename, "w");
46 if (!fp) {
47 error(err_cantopenw, filename);
48 return;
49 }
50
b0f1f943 51 fprintf(fp, "%%!PS-Adobe-3.0\n");
52 fprintf(fp, "%%%%Creator: Halibut, %s\n", version);
d4319ca3 53 fprintf(fp, "%%%%DocumentData: Clean7Bit\n");
b0f1f943 54 fprintf(fp, "%%%%LanguageLevel: 1\n");
43341922 55 for (pageno = 0, page = doc->pages; page; page = page->next)
56 pageno++;
57 fprintf(fp, "%%%%Pages: %d\n", pageno);
b0f1f943 58 for (p = sourceform; p; p = p->next)
59 if (p->type == para_Title)
60 ps_comment(fp, "%%Title: ", p->words);
61 fprintf(fp, "%%%%DocumentNeededResources:\n");
62 for (fe = doc->fonts->head; fe; fe = fe->next)
63 /* XXX This may request the same font multiple times. */
44407fea 64 if (!fe->font->info->fp)
65 fprintf(fp, "%%%%+ font %s\n", fe->font->info->name);
3f167769 66 fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 2\n");
44407fea 67 for (fe = doc->fonts->head; fe; fe = fe->next)
68 /* XXX This may request the same font multiple times. */
69 if (fe->font->info->fp)
70 fprintf(fp, "%%%%+ font %s\n", fe->font->info->name);
43341922 71 fprintf(fp, "%%%%EndComments\n");
72
73 fprintf(fp, "%%%%BeginProlog\n");
3f167769 74 fprintf(fp, "%%%%BeginResource: procset Halibut 0 2\n");
7c8c4239 75 /*
76 * Supply a prologue function which allows a reasonably
77 * compressed representation of the text on the pages.
78 *
7b980ef7 79 * "t" expects two arguments: a y-coordinate, and then an array.
7c8c4239 80 * Elements of the array are processed sequentially as follows:
81 *
82 * - a number is treated as an x-coordinate
83 * - an array is treated as a (font, size) pair
84 * - a string is shown
7b980ef7 85 *
86 * "r" takes four arguments, and behaves like "rectfill".
7c8c4239 87 */
88 fprintf(fp,
67f13fa7 89 "/tdict 4 dict dup begin\n"
90 " /arraytype {aload pop scalefont setfont} bind def\n"
91 " /realtype {1 index moveto} bind def\n"
92 " /integertype /realtype load def\n"
93 " /stringtype {show} bind def\n"
94 "end def\n"
7b980ef7 95 "/t { tdict begin {dup type exec} forall end pop } bind def\n"
96 "/r { 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
97 " neg 0 rlineto closepath fill } bind def\n");
3f167769 98 /*
99 * pdfmark wrappers
100 *
101 * "p" generates a named destination referencing this page.
102 * "x" generates a link to a named destination.
103 * "u" generates a link to a URI.
104 *
105 * They all do nothing if pdfmark is undefined.
106 */
107 fprintf(fp,
108 "/pdfmark where { pop\n"
109 " /p { [ /Dest 3 -1 roll /View [ /XYZ null null null ]\n"
110 " /DEST pdfmark } bind def\n"
766c3188 111 " /x { [ /Dest 3 -1 roll /Rect 5 -1 roll /Border [0 0 0]\n"
3f167769 112 " /Subtype /Link /ANN pdfmark } bind def\n"
113 " /u { 2 dict dup /Subtype /URI put dup /URI 4 -1 roll put\n"
766c3188 114 " [ /Action 3 -1 roll /Rect 5 -1 roll /Border [0 0 0]\n"
3f167769 115 " /Subtype /Link /ANN pdfmark } bind def\n"
116 "} {\n"
a80ef2f1 117 " /p { pop } bind def\n"
118 " /x { pop pop } bind def\n"
119 " /u /x load def\n"
3f167769 120 "} ifelse\n");
7c8c4239 121
b0f1f943 122 fprintf(fp, "%%%%EndResource\n");
43341922 123 fprintf(fp, "%%%%EndProlog\n");
124
125 fprintf(fp, "%%%%BeginSetup\n");
09358aa7 126
127 /*
f8194b21 128 * Assign a destination name to each page for pdfmark purposes.
129 */
130 pageno = 0;
131 for (page = doc->pages; page; page = page->next) {
132 char *buf;
133 pageno++;
134 buf = snewn(12, char);
135 sprintf(buf, "/p%d", pageno);
136 page->spare = buf;
137 }
138
139 /*
09358aa7 140 * This is as good a place as any to put version IDs.
141 */
142 for (p = sourceform; p; p = p->next)
143 if (p->type == para_VersionID)
b0f1f943 144 ps_comment(fp, "% ", p->words);
145
a10f193f 146 /*
147 * Request the correct page size. We might want to bracket this
148 * with "%%BeginFeature: *PageSize A4" or similar, and "%%EndFeature",
149 * but that would require us to have a way of getting the name of
150 * the page size given its dimensions.
151 */
152 fprintf(fp, "/setpagedevice where {\n");
153 fprintf(fp, " pop 2 dict dup /PageSize [%g %g] put setpagedevice\n",
154 doc->paper_width / FUNITS_PER_PT,
155 doc->paper_height / FUNITS_PER_PT);
156 fprintf(fp, "} if\n");
157
f8194b21 158 /* Outline etc, only if pdfmark is supported */
159 fprintf(fp, "/pdfmark where { pop %% if\n");
160 fprintf(fp, " [/PageMode/UseOutlines/DOCVIEW pdfmark\n");
161 noe = doc->n_outline_elements;
162 for (oe = doc->outline_elements; noe; oe++, noe--) {
163 char *title;
164 int titlelen, count, i;
165
166 title = pdf_outline_convert(oe->pdata->outline_title, &titlelen);
167
168 count = 0;
169 for (i = 1; i < noe && oe[i].level > oe->level; i++)
170 if (oe[i].level == oe->level + 1)
171 count++;
172 if (oe->level > 0) count = -count;
173
174 fprintf(fp, " [/Title");
175 ps_string_len(fp, title, titlelen);
176 sfree(title);
177 fprintf(fp, "/Dest%s/Count %d/OUT pdfmark\n",
178 (char *)oe->pdata->first->page->spare, count);
179 }
180 fprintf(fp, "} if\n");
3f167769 181
44407fea 182 for (fe = doc->fonts->head; fe; fe = fe->next) {
b0f1f943 183 /* XXX This may request the same font multiple times. */
44407fea 184 if (fe->font->info->fp) {
185 char buf[512];
186 size_t len;
187 fprintf(fp, "%%%%BeginResource: font %s\n", fe->font->info->name);
188 rewind(fe->font->info->fp);
189 do {
190 len = fread(buf, 1, sizeof(buf), fe->font->info->fp);
191 fwrite(buf, 1, len, fp);
192 } while (len == sizeof(buf));
193 fprintf(fp, "%%%%EndResource\n");
194 } else {
195 fprintf(fp, "%%%%IncludeResource: font %s\n",
196 fe->font->info->name);
197 }
198 }
09358aa7 199
43341922 200 /*
32ca74bb 201 * Re-encode the fonts.
43341922 202 */
203 font_index = 0;
204 for (fe = doc->fonts->head; fe; fe = fe->next) {
205 char fname[40];
206 int i;
207
208 sprintf(fname, "f%d", font_index++);
209 fe->name = dupstr(fname);
210
ba0fe3ec 211 fprintf(fp, "/%s findfont dup length dict begin\n",
212 fe->font->info->name);
43341922 213 fprintf(fp, "{1 index /FID ne {def} {pop pop} ifelse} forall\n");
214 fprintf(fp, "/Encoding [\n");
215 for (i = 0; i < 256; i++)
17c71b41 216 fprintf(fp, "/%s%c", fe->vector[i] ? fe->vector[i] : ".notdef",
217 i % 4 == 3 ? '\n' : ' ');
32ca74bb 218 fprintf(fp, "] def\n");
219 fprintf(fp, "currentdict end\n");
43341922 220 fprintf(fp, "/fontname-%s exch definefont /%s exch def\n\n",
221 fe->name, fe->name);
222 }
223 fprintf(fp, "%%%%EndSetup\n");
224
225 /*
23765aeb 226 * Output the text and graphics.
43341922 227 */
228 pageno = 0;
229 for (page = doc->pages; page; page = page->next) {
7c8c4239 230 text_fragment *frag, *frag_end;
23765aeb 231 rect *r;
3f167769 232 xref *xr;
3e32e06c 233 font_encoding *fe;
234 int fs;
43341922 235
236 pageno++;
237 fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno);
3f167769 238 fprintf(fp, "save %s p\n", (char *)page->spare);
239
240 for (xr = page->first_xref; xr; xr = xr->next) {
241 fprintf(fp, "[%g %g %g %g]",
242 xr->lx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT,
243 xr->rx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
244 if (xr->dest.type == PAGE) {
245 fprintf(fp, "%s x\n", (char *)xr->dest.page->spare);
246 } else {
247 ps_string(fp, xr->dest.url);
248 fprintf(fp, "u\n");
138d7ffb 249 }
250 }
138d7ffb 251
23765aeb 252 for (r = page->first_rect; r; r = r->next) {
7b980ef7 253 fprintf(fp, "%g %g %g %g r\n",
17c71b41 254 r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT,
7b980ef7 255 r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT);
23765aeb 256 }
257
7c8c4239 258 frag = page->first_text;
3e32e06c 259 fe = NULL;
260 fs = -1;
7c8c4239 261 while (frag) {
7c8c4239 262 /*
263 * Collect all the adjacent text fragments with the
264 * same y-coordinate.
265 */
266 for (frag_end = frag;
267 frag_end && frag_end->y == frag->y;
268 frag_end = frag_end->next);
269
17c71b41 270 fprintf(fp, "%g[", frag->y / FUNITS_PER_PT);
7c8c4239 271
7c8c4239 272 while (frag && frag != frag_end) {
273
274 if (frag->fe != fe || frag->fontsize != fs)
275 fprintf(fp, "[%s %d]", frag->fe->name, frag->fontsize);
276 fe = frag->fe;
277 fs = frag->fontsize;
278
3f167769 279 fprintf(fp, "%g", frag->x/FUNITS_PER_PT);
280 ps_string(fp, frag->text);
43341922 281
7c8c4239 282 frag = frag->next;
43341922 283 }
284
7c8c4239 285 fprintf(fp, "]t\n");
43341922 286 }
287
ae52aa5d 288 fprintf(fp, "restore showpage\n");
43341922 289 }
290
291 fprintf(fp, "%%%%EOF\n");
292
293 fclose(fp);
294
295 sfree(filename);
296}
09358aa7 297
b0f1f943 298static void ps_comment(FILE *fp, char const *leader, word *words)
09358aa7 299{
b0f1f943 300 fprintf(fp, "%s", leader);
09358aa7 301
302 for (; words; words = words->next) {
303 char *text;
304 int type;
305
306 switch (words->type) {
307 case word_HyperLink:
308 case word_HyperEnd:
309 case word_UpperXref:
310 case word_LowerXref:
311 case word_XrefEnd:
312 case word_IndexRef:
313 continue;
314 }
315
316 type = removeattr(words->type);
317
318 switch (type) {
319 case word_Normal:
e4ea58f8 320 text = utoa_dup(words->text, CS_ASCII);
09358aa7 321 break;
322 case word_WhiteSpace:
323 text = dupstr(" ");
324 break;
325 case word_Quote:
326 text = dupstr("'");
327 break;
328 }
329
330 fputs(text, fp);
331 sfree(text);
332 }
333
334 fprintf(fp, "\n");
335}
3f167769 336
f8194b21 337static void ps_string_len(FILE *fp, char const *str, int len) {
3f167769 338 char const *c;
f8194b21 339 int score = 0;
340
341 for (c = str; c < str+len; c++) {
342 if (*c < ' ' || *c > '~')
343 score += 2;
344 else if (*c == '(' || *c == ')' || *c == '\\')
345 score += 0;
346 else
347 score -= 1;
348 }
349 if (score > 0) {
350 fprintf(fp, "<");
351 for (c = str; c < str+len; c++) {
352 fprintf(fp, "%02X", 0xFF & (int)*c);
d4319ca3 353 }
f8194b21 354 fprintf(fp, ">");
355 } else {
356 fprintf(fp, "(");
357 for (c = str; c < str+len; c++) {
358 if (*c < ' ' || *c > '~') {
359 fprintf(fp, "\\%03o", 0xFF & (int)*c);
360 } else {
361 if (*c == '(' || *c == ')' || *c == '\\')
362 fputc('\\', fp);
363 fputc(*c, fp);
364 }
365 }
366 fprintf(fp, ")");
3f167769 367 }
f8194b21 368}
369
370static void ps_string(FILE *fp, char const *str) {
371 ps_string_len(fp, str, strlen(str));
3f167769 372}