There's no need to reset the font every line if it hasn't changed. This saves
[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 if (!fe->font->info->fp)
60 fprintf(fp, "%%%%+ font %s\n", fe->font->info->name);
61 fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 0\n");
62 for (fe = doc->fonts->head; fe; fe = fe->next)
63 /* XXX This may request the same font multiple times. */
64 if (fe->font->info->fp)
65 fprintf(fp, "%%%%+ font %s\n", fe->font->info->name);
66 fprintf(fp, "%%%%EndComments\n");
67
68 fprintf(fp, "%%%%BeginProlog\n");
69 fprintf(fp, "%%%%BeginResource: procset Halibut 0 0\n");
70 /*
71 * Supply a prologue function which allows a reasonably
72 * compressed representation of the text on the pages.
73 *
74 * Expects two arguments: a y-coordinate, and then an array.
75 * Elements of the array are processed sequentially as follows:
76 *
77 * - a number is treated as an x-coordinate
78 * - an array is treated as a (font, size) pair
79 * - a string is shown
80 */
81 fprintf(fp,
82 "/tdict 4 dict dup begin\n"
83 " /arraytype {aload pop scalefont setfont} bind def\n"
84 " /realtype {1 index moveto} bind def\n"
85 " /integertype /realtype load def\n"
86 " /stringtype {show} bind def\n"
87 "end def\n"
88 "/t { tdict begin {dup type exec} forall end pop } bind def\n");
89
90 fprintf(fp, "%%%%EndResource\n");
91 fprintf(fp, "%%%%EndProlog\n");
92
93 fprintf(fp, "%%%%BeginSetup\n");
94
95 /*
96 * This is as good a place as any to put version IDs.
97 */
98 for (p = sourceform; p; p = p->next)
99 if (p->type == para_VersionID)
100 ps_comment(fp, "% ", p->words);
101
102 for (fe = doc->fonts->head; fe; fe = fe->next) {
103 /* XXX This may request the same font multiple times. */
104 if (fe->font->info->fp) {
105 char buf[512];
106 size_t len;
107 fprintf(fp, "%%%%BeginResource: font %s\n", fe->font->info->name);
108 rewind(fe->font->info->fp);
109 do {
110 len = fread(buf, 1, sizeof(buf), fe->font->info->fp);
111 fwrite(buf, 1, len, fp);
112 } while (len == sizeof(buf));
113 fprintf(fp, "%%%%EndResource\n");
114 } else {
115 fprintf(fp, "%%%%IncludeResource: font %s\n",
116 fe->font->info->name);
117 }
118 }
119
120 /*
121 * Re-encode the fonts.
122 */
123 font_index = 0;
124 for (fe = doc->fonts->head; fe; fe = fe->next) {
125 char fname[40];
126 int i;
127
128 sprintf(fname, "f%d", font_index++);
129 fe->name = dupstr(fname);
130
131 fprintf(fp, "/%s findfont dup length dict begin\n",
132 fe->font->info->name);
133 fprintf(fp, "{1 index /FID ne {def} {pop pop} ifelse} forall\n");
134 fprintf(fp, "/Encoding [\n");
135 for (i = 0; i < 256; i++)
136 fprintf(fp, "/%s%c", fe->vector[i] ? fe->vector[i] : ".notdef",
137 i % 4 == 3 ? '\n' : ' ');
138 fprintf(fp, "] def\n");
139 fprintf(fp, "currentdict end\n");
140 fprintf(fp, "/fontname-%s exch definefont /%s exch def\n\n",
141 fe->name, fe->name);
142 }
143 fprintf(fp, "%%%%EndSetup\n");
144
145 /*
146 * Output the text and graphics.
147 */
148 pageno = 0;
149 for (page = doc->pages; page; page = page->next) {
150 text_fragment *frag, *frag_end;
151 rect *r;
152 font_encoding *fe;
153 int fs;
154
155 pageno++;
156 fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno);
157 fprintf(fp, "save\n");
158
159 #if 0
160 {
161 xref *xr;
162 /*
163 * I used this diagnostic briefly to ensure that
164 * cross-reference rectangles were being put where they
165 * should be.
166 */
167 for (xr = page->first_xref; xr; xr = xr->next) {
168 fprintf(fp, "gsave 0.7 setgray %g %g moveto",
169 xr->lx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
170 fprintf(fp, " %g %g lineto %g %g lineto",
171 xr->lx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT,
172 xr->rx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT);
173 fprintf(fp, " %g %g lineto closepath fill grestore\n",
174 xr->rx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
175 }
176 }
177 #endif
178
179 for (r = page->first_rect; r; r = r->next) {
180 fprintf(fp, "%g %g moveto %g 0 rlineto 0 %g rlineto "
181 "-%g 0 rlineto closepath fill\n",
182 r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT,
183 r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT,
184 r->w / FUNITS_PER_PT);
185 }
186
187 frag = page->first_text;
188 fe = NULL;
189 fs = -1;
190 while (frag) {
191 char *c;
192
193 /*
194 * Collect all the adjacent text fragments with the
195 * same y-coordinate.
196 */
197 for (frag_end = frag;
198 frag_end && frag_end->y == frag->y;
199 frag_end = frag_end->next);
200
201 fprintf(fp, "%g[", frag->y / FUNITS_PER_PT);
202
203 while (frag && frag != frag_end) {
204
205 if (frag->fe != fe || frag->fontsize != fs)
206 fprintf(fp, "[%s %d]", frag->fe->name, frag->fontsize);
207 fe = frag->fe;
208 fs = frag->fontsize;
209
210 fprintf(fp, "%g(", frag->x/FUNITS_PER_PT);
211 for (c = frag->text; *c; c++) {
212 if (*c == '(' || *c == ')' || *c == '\\')
213 fputc('\\', fp);
214 fputc(*c, fp);
215 }
216 fprintf(fp, ")");
217
218 frag = frag->next;
219 }
220
221 fprintf(fp, "]t\n");
222 }
223
224 fprintf(fp, "restore showpage\n");
225 }
226
227 fprintf(fp, "%%%%EOF\n");
228
229 fclose(fp);
230
231 sfree(filename);
232 }
233
234 static void ps_comment(FILE *fp, char const *leader, word *words)
235 {
236 fprintf(fp, "%s", leader);
237
238 for (; words; words = words->next) {
239 char *text;
240 int type;
241
242 switch (words->type) {
243 case word_HyperLink:
244 case word_HyperEnd:
245 case word_UpperXref:
246 case word_LowerXref:
247 case word_XrefEnd:
248 case word_IndexRef:
249 continue;
250 }
251
252 type = removeattr(words->type);
253
254 switch (type) {
255 case word_Normal:
256 text = utoa_dup(words->text, CS_ASCII);
257 break;
258 case word_WhiteSpace:
259 text = dupstr(" ");
260 break;
261 case word_Quote:
262 text = dupstr("'");
263 break;
264 }
265
266 fputs(text, fp);
267 sfree(text);
268 }
269
270 fprintf(fp, "\n");
271 }