43341922 |
1 | /* |
2 | * PostScript backend for Halibut |
3 | */ |
4 | |
5 | #include <assert.h> |
6 | #include "halibut.h" |
7 | #include "paper.h" |
8 | |
b0f1f943 |
9 | static void ps_comment(FILE *fp, char const *leader, word *words); |
09358aa7 |
10 | |
43341922 |
11 | paragraph *ps_config_filename(char *filename) |
12 | { |
e4ea58f8 |
13 | return cmdline_cfg_simple("ps-filename", filename, NULL); |
43341922 |
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) { |
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. */ |
44407fea |
59 | if (!fe->font->info->fp) |
60 | fprintf(fp, "%%%%+ font %s\n", fe->font->info->name); |
7b980ef7 |
61 | fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 1\n"); |
44407fea |
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); |
43341922 |
66 | fprintf(fp, "%%%%EndComments\n"); |
67 | |
68 | fprintf(fp, "%%%%BeginProlog\n"); |
7b980ef7 |
69 | fprintf(fp, "%%%%BeginResource: procset Halibut 0 1\n"); |
7c8c4239 |
70 | /* |
71 | * Supply a prologue function which allows a reasonably |
72 | * compressed representation of the text on the pages. |
73 | * |
7b980ef7 |
74 | * "t" expects two arguments: a y-coordinate, and then an array. |
7c8c4239 |
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 |
7b980ef7 |
80 | * |
81 | * "r" takes four arguments, and behaves like "rectfill". |
7c8c4239 |
82 | */ |
83 | fprintf(fp, |
67f13fa7 |
84 | "/tdict 4 dict dup begin\n" |
85 | " /arraytype {aload pop scalefont setfont} bind def\n" |
86 | " /realtype {1 index moveto} bind def\n" |
87 | " /integertype /realtype load def\n" |
88 | " /stringtype {show} bind def\n" |
89 | "end def\n" |
7b980ef7 |
90 | "/t { tdict begin {dup type exec} forall end pop } bind def\n" |
91 | "/r { 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" |
92 | " neg 0 rlineto closepath fill } bind def\n"); |
7c8c4239 |
93 | |
b0f1f943 |
94 | fprintf(fp, "%%%%EndResource\n"); |
43341922 |
95 | fprintf(fp, "%%%%EndProlog\n"); |
96 | |
97 | fprintf(fp, "%%%%BeginSetup\n"); |
09358aa7 |
98 | |
99 | /* |
100 | * This is as good a place as any to put version IDs. |
101 | */ |
102 | for (p = sourceform; p; p = p->next) |
103 | if (p->type == para_VersionID) |
b0f1f943 |
104 | ps_comment(fp, "% ", p->words); |
105 | |
a10f193f |
106 | /* |
107 | * Request the correct page size. We might want to bracket this |
108 | * with "%%BeginFeature: *PageSize A4" or similar, and "%%EndFeature", |
109 | * but that would require us to have a way of getting the name of |
110 | * the page size given its dimensions. |
111 | */ |
112 | fprintf(fp, "/setpagedevice where {\n"); |
113 | fprintf(fp, " pop 2 dict dup /PageSize [%g %g] put setpagedevice\n", |
114 | doc->paper_width / FUNITS_PER_PT, |
115 | doc->paper_height / FUNITS_PER_PT); |
116 | fprintf(fp, "} if\n"); |
117 | |
44407fea |
118 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
b0f1f943 |
119 | /* XXX This may request the same font multiple times. */ |
44407fea |
120 | if (fe->font->info->fp) { |
121 | char buf[512]; |
122 | size_t len; |
123 | fprintf(fp, "%%%%BeginResource: font %s\n", fe->font->info->name); |
124 | rewind(fe->font->info->fp); |
125 | do { |
126 | len = fread(buf, 1, sizeof(buf), fe->font->info->fp); |
127 | fwrite(buf, 1, len, fp); |
128 | } while (len == sizeof(buf)); |
129 | fprintf(fp, "%%%%EndResource\n"); |
130 | } else { |
131 | fprintf(fp, "%%%%IncludeResource: font %s\n", |
132 | fe->font->info->name); |
133 | } |
134 | } |
09358aa7 |
135 | |
43341922 |
136 | /* |
32ca74bb |
137 | * Re-encode the fonts. |
43341922 |
138 | */ |
139 | font_index = 0; |
140 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
141 | char fname[40]; |
142 | int i; |
143 | |
144 | sprintf(fname, "f%d", font_index++); |
145 | fe->name = dupstr(fname); |
146 | |
ba0fe3ec |
147 | fprintf(fp, "/%s findfont dup length dict begin\n", |
148 | fe->font->info->name); |
43341922 |
149 | fprintf(fp, "{1 index /FID ne {def} {pop pop} ifelse} forall\n"); |
150 | fprintf(fp, "/Encoding [\n"); |
151 | for (i = 0; i < 256; i++) |
17c71b41 |
152 | fprintf(fp, "/%s%c", fe->vector[i] ? fe->vector[i] : ".notdef", |
153 | i % 4 == 3 ? '\n' : ' '); |
32ca74bb |
154 | fprintf(fp, "] def\n"); |
155 | fprintf(fp, "currentdict end\n"); |
43341922 |
156 | fprintf(fp, "/fontname-%s exch definefont /%s exch def\n\n", |
157 | fe->name, fe->name); |
158 | } |
159 | fprintf(fp, "%%%%EndSetup\n"); |
160 | |
161 | /* |
23765aeb |
162 | * Output the text and graphics. |
43341922 |
163 | */ |
164 | pageno = 0; |
165 | for (page = doc->pages; page; page = page->next) { |
7c8c4239 |
166 | text_fragment *frag, *frag_end; |
23765aeb |
167 | rect *r; |
3e32e06c |
168 | font_encoding *fe; |
169 | int fs; |
43341922 |
170 | |
171 | pageno++; |
172 | fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno); |
ae52aa5d |
173 | fprintf(fp, "save\n"); |
138d7ffb |
174 | #if 0 |
175 | { |
176 | xref *xr; |
177 | /* |
178 | * I used this diagnostic briefly to ensure that |
179 | * cross-reference rectangles were being put where they |
180 | * should be. |
181 | */ |
182 | for (xr = page->first_xref; xr; xr = xr->next) { |
183 | fprintf(fp, "gsave 0.7 setgray %g %g moveto", |
17c71b41 |
184 | xr->lx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT); |
138d7ffb |
185 | fprintf(fp, " %g %g lineto %g %g lineto", |
17c71b41 |
186 | xr->lx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT, |
187 | xr->rx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT); |
138d7ffb |
188 | fprintf(fp, " %g %g lineto closepath fill grestore\n", |
17c71b41 |
189 | xr->rx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT); |
138d7ffb |
190 | } |
191 | } |
192 | #endif |
193 | |
23765aeb |
194 | for (r = page->first_rect; r; r = r->next) { |
7b980ef7 |
195 | fprintf(fp, "%g %g %g %g r\n", |
17c71b41 |
196 | r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT, |
7b980ef7 |
197 | r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT); |
23765aeb |
198 | } |
199 | |
7c8c4239 |
200 | frag = page->first_text; |
3e32e06c |
201 | fe = NULL; |
202 | fs = -1; |
7c8c4239 |
203 | while (frag) { |
43341922 |
204 | char *c; |
205 | |
7c8c4239 |
206 | /* |
207 | * Collect all the adjacent text fragments with the |
208 | * same y-coordinate. |
209 | */ |
210 | for (frag_end = frag; |
211 | frag_end && frag_end->y == frag->y; |
212 | frag_end = frag_end->next); |
213 | |
17c71b41 |
214 | fprintf(fp, "%g[", frag->y / FUNITS_PER_PT); |
7c8c4239 |
215 | |
7c8c4239 |
216 | while (frag && frag != frag_end) { |
217 | |
218 | if (frag->fe != fe || frag->fontsize != fs) |
219 | fprintf(fp, "[%s %d]", frag->fe->name, frag->fontsize); |
220 | fe = frag->fe; |
221 | fs = frag->fontsize; |
222 | |
17c71b41 |
223 | fprintf(fp, "%g(", frag->x/FUNITS_PER_PT); |
7c8c4239 |
224 | for (c = frag->text; *c; c++) { |
225 | if (*c == '(' || *c == ')' || *c == '\\') |
226 | fputc('\\', fp); |
227 | fputc(*c, fp); |
228 | } |
229 | fprintf(fp, ")"); |
43341922 |
230 | |
7c8c4239 |
231 | frag = frag->next; |
43341922 |
232 | } |
233 | |
7c8c4239 |
234 | fprintf(fp, "]t\n"); |
43341922 |
235 | } |
236 | |
ae52aa5d |
237 | fprintf(fp, "restore showpage\n"); |
43341922 |
238 | } |
239 | |
240 | fprintf(fp, "%%%%EOF\n"); |
241 | |
242 | fclose(fp); |
243 | |
244 | sfree(filename); |
245 | } |
09358aa7 |
246 | |
b0f1f943 |
247 | static void ps_comment(FILE *fp, char const *leader, word *words) |
09358aa7 |
248 | { |
b0f1f943 |
249 | fprintf(fp, "%s", leader); |
09358aa7 |
250 | |
251 | for (; words; words = words->next) { |
252 | char *text; |
253 | int type; |
254 | |
255 | switch (words->type) { |
256 | case word_HyperLink: |
257 | case word_HyperEnd: |
258 | case word_UpperXref: |
259 | case word_LowerXref: |
260 | case word_XrefEnd: |
261 | case word_IndexRef: |
262 | continue; |
263 | } |
264 | |
265 | type = removeattr(words->type); |
266 | |
267 | switch (type) { |
268 | case word_Normal: |
e4ea58f8 |
269 | text = utoa_dup(words->text, CS_ASCII); |
09358aa7 |
270 | break; |
271 | case word_WhiteSpace: |
272 | text = dupstr(" "); |
273 | break; |
274 | case word_Quote: |
275 | text = dupstr("'"); |
276 | break; |
277 | } |
278 | |
279 | fputs(text, fp); |
280 | sfree(text); |
281 | } |
282 | |
283 | fprintf(fp, "\n"); |
284 | } |