Make the PostScript prologue look less like it was written by a C programmer.
[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);
09358aa7 10
43341922 11paragraph *ps_config_filename(char *filename)
12{
e4ea58f8 13 return cmdline_cfg_simple("ps-filename", filename, NULL);
43341922 14}
15
16void ps_backend(paragraph *sourceform, keywordlist *keywords,
17 indexdata *idx, void *vdoc) {
18 document *doc = (document *)vdoc;
19 int font_index;
20 font_encoding *fe;
21 page_data *page;
22 int pageno;
23 FILE *fp;
24 char *filename;
25 paragraph *p;
26
27 IGNORE(keywords);
28 IGNORE(idx);
29
30 filename = dupstr("output.ps");
31 for (p = sourceform; p; p = p->next) {
7738a2fb 32 if (p->type == para_Config) {
43341922 33 if (!ustricmp(p->keyword, L"ps-filename")) {
34 sfree(filename);
e4ea58f8 35 filename = dupstr(adv(p->origkeyword));
43341922 36 }
37 }
38 }
39
40 fp = fopen(filename, "w");
41 if (!fp) {
42 error(err_cantopenw, filename);
43 return;
44 }
45
b0f1f943 46 fprintf(fp, "%%!PS-Adobe-3.0\n");
47 fprintf(fp, "%%%%Creator: Halibut, %s\n", version);
48 fprintf(fp, "%%%%DocumentData: Clean8Bit\n");
49 fprintf(fp, "%%%%LanguageLevel: 1\n");
43341922 50 for (pageno = 0, page = doc->pages; page; page = page->next)
51 pageno++;
52 fprintf(fp, "%%%%Pages: %d\n", pageno);
b0f1f943 53 for (p = sourceform; p; p = p->next)
54 if (p->type == para_Title)
55 ps_comment(fp, "%%Title: ", p->words);
56 fprintf(fp, "%%%%DocumentNeededResources:\n");
57 for (fe = doc->fonts->head; fe; fe = fe->next)
58 /* XXX This may request the same font multiple times. */
59 fprintf(fp, "%%%%+ font %s\n", fe->font->name);
60 fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 0\n");
43341922 61 fprintf(fp, "%%%%EndComments\n");
62
63 fprintf(fp, "%%%%BeginProlog\n");
b0f1f943 64 fprintf(fp, "%%%%BeginResource: procset Halibut 0 0\n");
7c8c4239 65 /*
66 * Supply a prologue function which allows a reasonably
67 * compressed representation of the text on the pages.
68 *
69 * Expects two arguments: a y-coordinate, and then an array.
70 * Elements of the array are processed sequentially as follows:
71 *
72 * - a number is treated as an x-coordinate
73 * - an array is treated as a (font, size) pair
74 * - a string is shown
75 */
76 fprintf(fp,
67f13fa7 77 "/tdict 4 dict dup begin\n"
78 " /arraytype {aload pop scalefont setfont} bind def\n"
79 " /realtype {1 index moveto} bind def\n"
80 " /integertype /realtype load def\n"
81 " /stringtype {show} bind def\n"
82 "end def\n"
83 "/t { tdict begin {dup type exec} forall end } bind def\n");
7c8c4239 84
b0f1f943 85 fprintf(fp, "%%%%EndResource\n");
43341922 86 fprintf(fp, "%%%%EndProlog\n");
87
88 fprintf(fp, "%%%%BeginSetup\n");
09358aa7 89
90 /*
91 * This is as good a place as any to put version IDs.
92 */
93 for (p = sourceform; p; p = p->next)
94 if (p->type == para_VersionID)
b0f1f943 95 ps_comment(fp, "% ", p->words);
96
97 for (fe = doc->fonts->head; fe; fe = fe->next)
98 /* XXX This may request the same font multiple times. */
99 fprintf(fp, "%%%%IncludeResource: font %s\n", fe->font->name);
09358aa7 100
43341922 101 /*
102 * Re-encode and re-metric the fonts.
103 */
104 font_index = 0;
105 for (fe = doc->fonts->head; fe; fe = fe->next) {
106 char fname[40];
107 int i;
108
109 sprintf(fname, "f%d", font_index++);
110 fe->name = dupstr(fname);
111
112 fprintf(fp, "/%s findfont dup length dict begin\n", fe->font->name);
113 fprintf(fp, "{1 index /FID ne {def} {pop pop} ifelse} forall\n");
114 fprintf(fp, "/Encoding [\n");
115 for (i = 0; i < 256; i++)
116 fprintf(fp, "/%s\n", fe->vector[i] ? fe->vector[i] : ".notdef");
117 fprintf(fp, "] def /Metrics 256 dict dup begin\n");
118 for (i = 0; i < 256; i++) {
119 if (fe->indices[i] >= 0) {
120 double width = fe->font->widths[fe->indices[i]];
121 fprintf(fp, "/%s %g def\n", fe->vector[i],
122 1000.0 * width / 4096.0);
123 }
124 }
125 fprintf(fp, "end def currentdict end\n");
126 fprintf(fp, "/fontname-%s exch definefont /%s exch def\n\n",
127 fe->name, fe->name);
128 }
129 fprintf(fp, "%%%%EndSetup\n");
130
131 /*
23765aeb 132 * Output the text and graphics.
43341922 133 */
134 pageno = 0;
135 for (page = doc->pages; page; page = page->next) {
7c8c4239 136 text_fragment *frag, *frag_end;
23765aeb 137 rect *r;
43341922 138
139 pageno++;
140 fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno);
43341922 141
138d7ffb 142#if 0
143 {
144 xref *xr;
145 /*
146 * I used this diagnostic briefly to ensure that
147 * cross-reference rectangles were being put where they
148 * should be.
149 */
150 for (xr = page->first_xref; xr; xr = xr->next) {
151 fprintf(fp, "gsave 0.7 setgray %g %g moveto",
152 xr->lx/4096.0, xr->ty/4096.0);
153 fprintf(fp, " %g %g lineto %g %g lineto",
154 xr->lx/4096.0, xr->by/4096.0,
155 xr->rx/4096.0, xr->by/4096.0);
156 fprintf(fp, " %g %g lineto closepath fill grestore\n",
157 xr->rx/4096.0, xr->ty/4096.0);
158 }
159 }
160#endif
161
23765aeb 162 for (r = page->first_rect; r; r = r->next) {
163 fprintf(fp, "%g %g moveto %g 0 rlineto 0 %g rlineto "
164 "-%g 0 rlineto closepath fill\n",
165 r->x / 4096.0, r->y / 4096.0, r->w / 4096.0,
166 r->h / 4096.0, r->w / 4096.0);
167 }
168
7c8c4239 169 frag = page->first_text;
170 while (frag) {
171 font_encoding *fe;
172 int fs;
43341922 173 char *c;
174
7c8c4239 175 /*
176 * Collect all the adjacent text fragments with the
177 * same y-coordinate.
178 */
179 for (frag_end = frag;
180 frag_end && frag_end->y == frag->y;
181 frag_end = frag_end->next);
182
183 fprintf(fp, "%g[", frag->y / 4096.0);
184
185 fe = NULL;
186 fs = -1;
187
188 while (frag && frag != frag_end) {
189
190 if (frag->fe != fe || frag->fontsize != fs)
191 fprintf(fp, "[%s %d]", frag->fe->name, frag->fontsize);
192 fe = frag->fe;
193 fs = frag->fontsize;
194
195 fprintf(fp, "%g(", frag->x/4096.0);
196 for (c = frag->text; *c; c++) {
197 if (*c == '(' || *c == ')' || *c == '\\')
198 fputc('\\', fp);
199 fputc(*c, fp);
200 }
201 fprintf(fp, ")");
43341922 202
7c8c4239 203 frag = frag->next;
43341922 204 }
205
7c8c4239 206 fprintf(fp, "]t\n");
43341922 207 }
208
209 fprintf(fp, "showpage\n");
210 }
211
212 fprintf(fp, "%%%%EOF\n");
213
214 fclose(fp);
215
216 sfree(filename);
217}
09358aa7 218
b0f1f943 219static void ps_comment(FILE *fp, char const *leader, word *words)
09358aa7 220{
b0f1f943 221 fprintf(fp, "%s", leader);
09358aa7 222
223 for (; words; words = words->next) {
224 char *text;
225 int type;
226
227 switch (words->type) {
228 case word_HyperLink:
229 case word_HyperEnd:
230 case word_UpperXref:
231 case word_LowerXref:
232 case word_XrefEnd:
233 case word_IndexRef:
234 continue;
235 }
236
237 type = removeattr(words->type);
238
239 switch (type) {
240 case word_Normal:
e4ea58f8 241 text = utoa_dup(words->text, CS_ASCII);
09358aa7 242 break;
243 case word_WhiteSpace:
244 text = dupstr(" ");
245 break;
246 case word_Quote:
247 text = dupstr("'");
248 break;
249 }
250
251 fputs(text, fp);
252 sfree(text);
253 }
254
255 fprintf(fp, "\n");
256}