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