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); |
3f167769 |
10 | static void ps_string(FILE *fp, char const *str); |
09358aa7 |
11 | |
43341922 |
12 | paragraph *ps_config_filename(char *filename) |
13 | { |
e4ea58f8 |
14 | return cmdline_cfg_simple("ps-filename", filename, NULL); |
43341922 |
15 | } |
16 | |
17 | void ps_backend(paragraph *sourceform, keywordlist *keywords, |
18 | indexdata *idx, void *vdoc) { |
19 | document *doc = (document *)vdoc; |
20 | int font_index; |
21 | font_encoding *fe; |
22 | page_data *page; |
23 | int pageno; |
24 | FILE *fp; |
25 | char *filename; |
26 | paragraph *p; |
27 | |
28 | IGNORE(keywords); |
29 | IGNORE(idx); |
30 | |
31 | filename = dupstr("output.ps"); |
32 | for (p = sourceform; p; p = p->next) { |
7738a2fb |
33 | if (p->type == para_Config) { |
43341922 |
34 | if (!ustricmp(p->keyword, L"ps-filename")) { |
35 | sfree(filename); |
e4ea58f8 |
36 | filename = dupstr(adv(p->origkeyword)); |
43341922 |
37 | } |
38 | } |
39 | } |
40 | |
41 | fp = fopen(filename, "w"); |
42 | if (!fp) { |
43 | error(err_cantopenw, filename); |
44 | return; |
45 | } |
46 | |
b0f1f943 |
47 | fprintf(fp, "%%!PS-Adobe-3.0\n"); |
48 | fprintf(fp, "%%%%Creator: Halibut, %s\n", version); |
49 | fprintf(fp, "%%%%DocumentData: Clean8Bit\n"); |
50 | fprintf(fp, "%%%%LanguageLevel: 1\n"); |
43341922 |
51 | for (pageno = 0, page = doc->pages; page; page = page->next) |
52 | pageno++; |
53 | fprintf(fp, "%%%%Pages: %d\n", pageno); |
b0f1f943 |
54 | for (p = sourceform; p; p = p->next) |
55 | if (p->type == para_Title) |
56 | ps_comment(fp, "%%Title: ", p->words); |
57 | fprintf(fp, "%%%%DocumentNeededResources:\n"); |
58 | for (fe = doc->fonts->head; fe; fe = fe->next) |
59 | /* XXX This may request the same font multiple times. */ |
44407fea |
60 | if (!fe->font->info->fp) |
61 | fprintf(fp, "%%%%+ font %s\n", fe->font->info->name); |
3f167769 |
62 | fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 2\n"); |
44407fea |
63 | for (fe = doc->fonts->head; fe; fe = fe->next) |
64 | /* XXX This may request the same font multiple times. */ |
65 | if (fe->font->info->fp) |
66 | fprintf(fp, "%%%%+ font %s\n", fe->font->info->name); |
43341922 |
67 | fprintf(fp, "%%%%EndComments\n"); |
68 | |
69 | fprintf(fp, "%%%%BeginProlog\n"); |
3f167769 |
70 | fprintf(fp, "%%%%BeginResource: procset Halibut 0 2\n"); |
7c8c4239 |
71 | /* |
72 | * Supply a prologue function which allows a reasonably |
73 | * compressed representation of the text on the pages. |
74 | * |
7b980ef7 |
75 | * "t" expects two arguments: a y-coordinate, and then an array. |
7c8c4239 |
76 | * Elements of the array are processed sequentially as follows: |
77 | * |
78 | * - a number is treated as an x-coordinate |
79 | * - an array is treated as a (font, size) pair |
80 | * - a string is shown |
7b980ef7 |
81 | * |
82 | * "r" takes four arguments, and behaves like "rectfill". |
7c8c4239 |
83 | */ |
84 | fprintf(fp, |
67f13fa7 |
85 | "/tdict 4 dict dup begin\n" |
86 | " /arraytype {aload pop scalefont setfont} bind def\n" |
87 | " /realtype {1 index moveto} bind def\n" |
88 | " /integertype /realtype load def\n" |
89 | " /stringtype {show} bind def\n" |
90 | "end def\n" |
7b980ef7 |
91 | "/t { tdict begin {dup type exec} forall end pop } bind def\n" |
92 | "/r { 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" |
93 | " neg 0 rlineto closepath fill } bind def\n"); |
3f167769 |
94 | /* |
95 | * pdfmark wrappers |
96 | * |
97 | * "p" generates a named destination referencing this page. |
98 | * "x" generates a link to a named destination. |
99 | * "u" generates a link to a URI. |
100 | * |
101 | * They all do nothing if pdfmark is undefined. |
102 | */ |
103 | fprintf(fp, |
104 | "/pdfmark where { pop\n" |
105 | " /p { [ /Dest 3 -1 roll /View [ /XYZ null null null ]\n" |
106 | " /DEST pdfmark } bind def\n" |
107 | " /x { [ /Dest 3 -1 roll /Rect 5 -1 roll /Border [0 0 0 0]\n" |
108 | " /Subtype /Link /ANN pdfmark } bind def\n" |
109 | " /u { 2 dict dup /Subtype /URI put dup /URI 4 -1 roll put\n" |
110 | " [ /Action 3 -1 roll /Rect 5 -1 roll /Border [0 0 0 0]\n" |
111 | " /Subtype /Link /ANN pdfmark } bind def\n" |
112 | "} {\n" |
113 | " [/p /x /u] { null cvx def } forall\n" |
114 | "} ifelse\n"); |
7c8c4239 |
115 | |
b0f1f943 |
116 | fprintf(fp, "%%%%EndResource\n"); |
43341922 |
117 | fprintf(fp, "%%%%EndProlog\n"); |
118 | |
119 | fprintf(fp, "%%%%BeginSetup\n"); |
09358aa7 |
120 | |
121 | /* |
122 | * This is as good a place as any to put version IDs. |
123 | */ |
124 | for (p = sourceform; p; p = p->next) |
125 | if (p->type == para_VersionID) |
b0f1f943 |
126 | ps_comment(fp, "% ", p->words); |
127 | |
a10f193f |
128 | /* |
129 | * Request the correct page size. We might want to bracket this |
130 | * with "%%BeginFeature: *PageSize A4" or similar, and "%%EndFeature", |
131 | * but that would require us to have a way of getting the name of |
132 | * the page size given its dimensions. |
133 | */ |
134 | fprintf(fp, "/setpagedevice where {\n"); |
135 | fprintf(fp, " pop 2 dict dup /PageSize [%g %g] put setpagedevice\n", |
136 | doc->paper_width / FUNITS_PER_PT, |
137 | doc->paper_height / FUNITS_PER_PT); |
138 | fprintf(fp, "} if\n"); |
139 | |
3f167769 |
140 | /* Request outline view if the document is converted to PDF. */ |
141 | fprintf(fp, |
142 | "/pdfmark where {\n" |
143 | " pop [ /PageMode /UseOutlines /DOCVIEW pdfmark\n" |
144 | "} if\n"); |
145 | |
44407fea |
146 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
b0f1f943 |
147 | /* XXX This may request the same font multiple times. */ |
44407fea |
148 | if (fe->font->info->fp) { |
149 | char buf[512]; |
150 | size_t len; |
151 | fprintf(fp, "%%%%BeginResource: font %s\n", fe->font->info->name); |
152 | rewind(fe->font->info->fp); |
153 | do { |
154 | len = fread(buf, 1, sizeof(buf), fe->font->info->fp); |
155 | fwrite(buf, 1, len, fp); |
156 | } while (len == sizeof(buf)); |
157 | fprintf(fp, "%%%%EndResource\n"); |
158 | } else { |
159 | fprintf(fp, "%%%%IncludeResource: font %s\n", |
160 | fe->font->info->name); |
161 | } |
162 | } |
09358aa7 |
163 | |
43341922 |
164 | /* |
32ca74bb |
165 | * Re-encode the fonts. |
43341922 |
166 | */ |
167 | font_index = 0; |
168 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
169 | char fname[40]; |
170 | int i; |
171 | |
172 | sprintf(fname, "f%d", font_index++); |
173 | fe->name = dupstr(fname); |
174 | |
ba0fe3ec |
175 | fprintf(fp, "/%s findfont dup length dict begin\n", |
176 | fe->font->info->name); |
43341922 |
177 | fprintf(fp, "{1 index /FID ne {def} {pop pop} ifelse} forall\n"); |
178 | fprintf(fp, "/Encoding [\n"); |
179 | for (i = 0; i < 256; i++) |
17c71b41 |
180 | fprintf(fp, "/%s%c", fe->vector[i] ? fe->vector[i] : ".notdef", |
181 | i % 4 == 3 ? '\n' : ' '); |
32ca74bb |
182 | fprintf(fp, "] def\n"); |
183 | fprintf(fp, "currentdict end\n"); |
43341922 |
184 | fprintf(fp, "/fontname-%s exch definefont /%s exch def\n\n", |
185 | fe->name, fe->name); |
186 | } |
187 | fprintf(fp, "%%%%EndSetup\n"); |
188 | |
189 | /* |
3f167769 |
190 | * Assign a destination name to each page for pdfmark purposes. |
191 | */ |
192 | pageno = 0; |
193 | for (page = doc->pages; page; page = page->next) { |
194 | char *buf; |
195 | pageno++; |
196 | buf = snewn(12, char); |
197 | sprintf(buf, "/p%d", pageno); |
198 | page->spare = buf; |
199 | } |
200 | |
201 | /* |
23765aeb |
202 | * Output the text and graphics. |
43341922 |
203 | */ |
204 | pageno = 0; |
205 | for (page = doc->pages; page; page = page->next) { |
7c8c4239 |
206 | text_fragment *frag, *frag_end; |
23765aeb |
207 | rect *r; |
3f167769 |
208 | xref *xr; |
3e32e06c |
209 | font_encoding *fe; |
210 | int fs; |
43341922 |
211 | |
212 | pageno++; |
213 | fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno); |
3f167769 |
214 | fprintf(fp, "save %s p\n", (char *)page->spare); |
215 | |
216 | for (xr = page->first_xref; xr; xr = xr->next) { |
217 | fprintf(fp, "[%g %g %g %g]", |
218 | xr->lx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT, |
219 | xr->rx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT); |
220 | if (xr->dest.type == PAGE) { |
221 | fprintf(fp, "%s x\n", (char *)xr->dest.page->spare); |
222 | } else { |
223 | ps_string(fp, xr->dest.url); |
224 | fprintf(fp, "u\n"); |
138d7ffb |
225 | } |
226 | } |
138d7ffb |
227 | |
23765aeb |
228 | for (r = page->first_rect; r; r = r->next) { |
7b980ef7 |
229 | fprintf(fp, "%g %g %g %g r\n", |
17c71b41 |
230 | r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT, |
7b980ef7 |
231 | r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT); |
23765aeb |
232 | } |
233 | |
7c8c4239 |
234 | frag = page->first_text; |
3e32e06c |
235 | fe = NULL; |
236 | fs = -1; |
7c8c4239 |
237 | while (frag) { |
7c8c4239 |
238 | /* |
239 | * Collect all the adjacent text fragments with the |
240 | * same y-coordinate. |
241 | */ |
242 | for (frag_end = frag; |
243 | frag_end && frag_end->y == frag->y; |
244 | frag_end = frag_end->next); |
245 | |
17c71b41 |
246 | fprintf(fp, "%g[", frag->y / FUNITS_PER_PT); |
7c8c4239 |
247 | |
7c8c4239 |
248 | while (frag && frag != frag_end) { |
249 | |
250 | if (frag->fe != fe || frag->fontsize != fs) |
251 | fprintf(fp, "[%s %d]", frag->fe->name, frag->fontsize); |
252 | fe = frag->fe; |
253 | fs = frag->fontsize; |
254 | |
3f167769 |
255 | fprintf(fp, "%g", frag->x/FUNITS_PER_PT); |
256 | ps_string(fp, frag->text); |
43341922 |
257 | |
7c8c4239 |
258 | frag = frag->next; |
43341922 |
259 | } |
260 | |
7c8c4239 |
261 | fprintf(fp, "]t\n"); |
43341922 |
262 | } |
263 | |
ae52aa5d |
264 | fprintf(fp, "restore showpage\n"); |
43341922 |
265 | } |
266 | |
267 | fprintf(fp, "%%%%EOF\n"); |
268 | |
269 | fclose(fp); |
270 | |
271 | sfree(filename); |
272 | } |
09358aa7 |
273 | |
b0f1f943 |
274 | static void ps_comment(FILE *fp, char const *leader, word *words) |
09358aa7 |
275 | { |
b0f1f943 |
276 | fprintf(fp, "%s", leader); |
09358aa7 |
277 | |
278 | for (; words; words = words->next) { |
279 | char *text; |
280 | int type; |
281 | |
282 | switch (words->type) { |
283 | case word_HyperLink: |
284 | case word_HyperEnd: |
285 | case word_UpperXref: |
286 | case word_LowerXref: |
287 | case word_XrefEnd: |
288 | case word_IndexRef: |
289 | continue; |
290 | } |
291 | |
292 | type = removeattr(words->type); |
293 | |
294 | switch (type) { |
295 | case word_Normal: |
e4ea58f8 |
296 | text = utoa_dup(words->text, CS_ASCII); |
09358aa7 |
297 | break; |
298 | case word_WhiteSpace: |
299 | text = dupstr(" "); |
300 | break; |
301 | case word_Quote: |
302 | text = dupstr("'"); |
303 | break; |
304 | } |
305 | |
306 | fputs(text, fp); |
307 | sfree(text); |
308 | } |
309 | |
310 | fprintf(fp, "\n"); |
311 | } |
3f167769 |
312 | |
313 | static void ps_string(FILE *fp, char const *str) { |
314 | char const *c; |
315 | |
316 | fprintf(fp, "("); |
317 | for (c = str; *c; c++) { |
318 | if (*c == '(' || *c == ')' || *c == '\\') |
319 | fputc('\\', fp); |
320 | fputc(*c, fp); |
321 | } |
322 | fprintf(fp, ")"); |
323 | } |