Two small tweaks to the prologue:
[sgt/halibut] / bk_ps.c
1 /*
2 * PostScript backend for Halibut
3 */
4
5 #include <assert.h>
6 #include "halibut.h"
7 #include "paper.h"
8
9 static void ps_comment(FILE *fp, char const *leader, word *words);
10
11 paragraph *ps_config_filename(char *filename)
12 {
13 return cmdline_cfg_simple("ps-filename", filename, NULL);
14 }
15
16 void 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) {
32 if (p->type == para_Config) {
33 if (!ustricmp(p->keyword, L"ps-filename")) {
34 sfree(filename);
35 filename = dupstr(adv(p->origkeyword));
36 }
37 }
38 }
39
40 fp = fopen(filename, "w");
41 if (!fp) {
42 error(err_cantopenw, filename);
43 return;
44 }
45
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");
50 for (pageno = 0, page = doc->pages; page; page = page->next)
51 pageno++;
52 fprintf(fp, "%%%%Pages: %d\n", pageno);
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");
61 fprintf(fp, "%%%%EndComments\n");
62
63 fprintf(fp, "%%%%BeginProlog\n");
64 fprintf(fp, "%%%%BeginResource: procset Halibut 0 0\n");
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,
77 "/t {\n"
78 " exch /y exch def {\n"
79 " /x exch def\n"
80 " x type /arraytype eq {x aload pop scalefont setfont} if\n"
81 " x type dup /integertype eq exch /realtype eq or "
82 "{x y moveto} if\n"
83 " x type /stringtype eq {x show} if\n"
84 " } forall\n"
85 "} bind def\n");
86
87 fprintf(fp, "%%%%EndResource\n");
88 fprintf(fp, "%%%%EndProlog\n");
89
90 fprintf(fp, "%%%%BeginSetup\n");
91
92 /*
93 * This is as good a place as any to put version IDs.
94 */
95 for (p = sourceform; p; p = p->next)
96 if (p->type == para_VersionID)
97 ps_comment(fp, "% ", p->words);
98
99 for (fe = doc->fonts->head; fe; fe = fe->next)
100 /* XXX This may request the same font multiple times. */
101 fprintf(fp, "%%%%IncludeResource: font %s\n", fe->font->name);
102
103 /*
104 * Re-encode and re-metric the fonts.
105 */
106 font_index = 0;
107 for (fe = doc->fonts->head; fe; fe = fe->next) {
108 char fname[40];
109 int i;
110
111 sprintf(fname, "f%d", font_index++);
112 fe->name = dupstr(fname);
113
114 fprintf(fp, "/%s findfont dup length dict begin\n", fe->font->name);
115 fprintf(fp, "{1 index /FID ne {def} {pop pop} ifelse} forall\n");
116 fprintf(fp, "/Encoding [\n");
117 for (i = 0; i < 256; i++)
118 fprintf(fp, "/%s\n", fe->vector[i] ? fe->vector[i] : ".notdef");
119 fprintf(fp, "] def /Metrics 256 dict dup begin\n");
120 for (i = 0; i < 256; i++) {
121 if (fe->indices[i] >= 0) {
122 double width = fe->font->widths[fe->indices[i]];
123 fprintf(fp, "/%s %g def\n", fe->vector[i],
124 1000.0 * width / 4096.0);
125 }
126 }
127 fprintf(fp, "end def currentdict end\n");
128 fprintf(fp, "/fontname-%s exch definefont /%s exch def\n\n",
129 fe->name, fe->name);
130 }
131 fprintf(fp, "%%%%EndSetup\n");
132
133 /*
134 * Output the text and graphics.
135 */
136 pageno = 0;
137 for (page = doc->pages; page; page = page->next) {
138 text_fragment *frag, *frag_end;
139 rect *r;
140
141 pageno++;
142 fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno);
143
144 #if 0
145 {
146 xref *xr;
147 /*
148 * I used this diagnostic briefly to ensure that
149 * cross-reference rectangles were being put where they
150 * should be.
151 */
152 for (xr = page->first_xref; xr; xr = xr->next) {
153 fprintf(fp, "gsave 0.7 setgray %g %g moveto",
154 xr->lx/4096.0, xr->ty/4096.0);
155 fprintf(fp, " %g %g lineto %g %g lineto",
156 xr->lx/4096.0, xr->by/4096.0,
157 xr->rx/4096.0, xr->by/4096.0);
158 fprintf(fp, " %g %g lineto closepath fill grestore\n",
159 xr->rx/4096.0, xr->ty/4096.0);
160 }
161 }
162 #endif
163
164 for (r = page->first_rect; r; r = r->next) {
165 fprintf(fp, "%g %g moveto %g 0 rlineto 0 %g rlineto "
166 "-%g 0 rlineto closepath fill\n",
167 r->x / 4096.0, r->y / 4096.0, r->w / 4096.0,
168 r->h / 4096.0, r->w / 4096.0);
169 }
170
171 frag = page->first_text;
172 while (frag) {
173 font_encoding *fe;
174 int fs;
175 char *c;
176
177 /*
178 * Collect all the adjacent text fragments with the
179 * same y-coordinate.
180 */
181 for (frag_end = frag;
182 frag_end && frag_end->y == frag->y;
183 frag_end = frag_end->next);
184
185 fprintf(fp, "%g[", frag->y / 4096.0);
186
187 fe = NULL;
188 fs = -1;
189
190 while (frag && frag != frag_end) {
191
192 if (frag->fe != fe || frag->fontsize != fs)
193 fprintf(fp, "[%s %d]", frag->fe->name, frag->fontsize);
194 fe = frag->fe;
195 fs = frag->fontsize;
196
197 fprintf(fp, "%g(", frag->x/4096.0);
198 for (c = frag->text; *c; c++) {
199 if (*c == '(' || *c == ')' || *c == '\\')
200 fputc('\\', fp);
201 fputc(*c, fp);
202 }
203 fprintf(fp, ")");
204
205 frag = frag->next;
206 }
207
208 fprintf(fp, "]t\n");
209 }
210
211 fprintf(fp, "showpage\n");
212 }
213
214 fprintf(fp, "%%%%EOF\n");
215
216 fclose(fp);
217
218 sfree(filename);
219 }
220
221 static void ps_comment(FILE *fp, char const *leader, word *words)
222 {
223 fprintf(fp, "%s", leader);
224
225 for (; words; words = words->next) {
226 char *text;
227 int type;
228
229 switch (words->type) {
230 case word_HyperLink:
231 case word_HyperEnd:
232 case word_UpperXref:
233 case word_LowerXref:
234 case word_XrefEnd:
235 case word_IndexRef:
236 continue;
237 }
238
239 type = removeattr(words->type);
240
241 switch (type) {
242 case word_Normal:
243 text = utoa_dup(words->text, CS_ASCII);
244 break;
245 case word_WhiteSpace:
246 text = dupstr(" ");
247 break;
248 case word_Quote:
249 text = dupstr("'");
250 break;
251 }
252
253 fputs(text, fp);
254 sfree(text);
255 }
256
257 fprintf(fp, "\n");
258 }