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