Replace the hardcoded instances of "4096" and "4096.0" in the paper backends
[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 "/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");
84
85 fprintf(fp, "%%%%EndResource\n");
86 fprintf(fp, "%%%%EndProlog\n");
87
88 fprintf(fp, "%%%%BeginSetup\n");
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)
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);
100
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%c", fe->vector[i] ? fe->vector[i] : ".notdef",
117 i % 4 == 3 ? '\n' : ' ');
118 fprintf(fp, "] def /Metrics 256 dict dup begin\n");
119 for (i = 0; i < 256; i++) {
120 if (fe->indices[i] >= 0) {
121 double width = fe->font->widths[fe->indices[i]];
122 fprintf(fp, "/%s %g def\n", fe->vector[i],
123 1000.0 * width / FUNITS_PER_PT);
124 }
125 }
126 fprintf(fp, "end def currentdict end\n");
127 fprintf(fp, "/fontname-%s exch definefont /%s exch def\n\n",
128 fe->name, fe->name);
129 }
130 fprintf(fp, "%%%%EndSetup\n");
131
132 /*
133 * Output the text and graphics.
134 */
135 pageno = 0;
136 for (page = doc->pages; page; page = page->next) {
137 text_fragment *frag, *frag_end;
138 rect *r;
139
140 pageno++;
141 fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno);
142 fprintf(fp, "save\n");
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/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
155 fprintf(fp, " %g %g lineto %g %g lineto",
156 xr->lx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT,
157 xr->rx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT);
158 fprintf(fp, " %g %g lineto closepath fill grestore\n",
159 xr->rx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
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 / FUNITS_PER_PT, r->y / FUNITS_PER_PT,
168 r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT,
169 r->w / FUNITS_PER_PT);
170 }
171
172 frag = page->first_text;
173 while (frag) {
174 font_encoding *fe;
175 int fs;
176 char *c;
177
178 /*
179 * Collect all the adjacent text fragments with the
180 * same y-coordinate.
181 */
182 for (frag_end = frag;
183 frag_end && frag_end->y == frag->y;
184 frag_end = frag_end->next);
185
186 fprintf(fp, "%g[", frag->y / FUNITS_PER_PT);
187
188 fe = NULL;
189 fs = -1;
190
191 while (frag && frag != frag_end) {
192
193 if (frag->fe != fe || frag->fontsize != fs)
194 fprintf(fp, "[%s %d]", frag->fe->name, frag->fontsize);
195 fe = frag->fe;
196 fs = frag->fontsize;
197
198 fprintf(fp, "%g(", frag->x/FUNITS_PER_PT);
199 for (c = frag->text; *c; c++) {
200 if (*c == '(' || *c == ')' || *c == '\\')
201 fputc('\\', fp);
202 fputc(*c, fp);
203 }
204 fprintf(fp, ")");
205
206 frag = frag->next;
207 }
208
209 fprintf(fp, "]t\n");
210 }
211
212 fprintf(fp, "restore showpage\n");
213 }
214
215 fprintf(fp, "%%%%EOF\n");
216
217 fclose(fp);
218
219 sfree(filename);
220 }
221
222 static void ps_comment(FILE *fp, char const *leader, word *words)
223 {
224 fprintf(fp, "%s", leader);
225
226 for (; words; words = words->next) {
227 char *text;
228 int type;
229
230 switch (words->type) {
231 case word_HyperLink:
232 case word_HyperEnd:
233 case word_UpperXref:
234 case word_LowerXref:
235 case word_XrefEnd:
236 case word_IndexRef:
237 continue;
238 }
239
240 type = removeattr(words->type);
241
242 switch (type) {
243 case word_Normal:
244 text = utoa_dup(words->text, CS_ASCII);
245 break;
246 case word_WhiteSpace:
247 text = dupstr(" ");
248 break;
249 case word_Quote:
250 text = dupstr("'");
251 break;
252 }
253
254 fputs(text, fp);
255 sfree(text);
256 }
257
258 fprintf(fp, "\n");
259 }