Fairly ropey font-embedding support. In particular, the PDF output is
[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
44407fea 102 for (fe = doc->fonts->head; fe; fe = fe->next) {
b0f1f943 103 /* XXX This may request the same font multiple times. */
44407fea 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 }
09358aa7 119
43341922 120 /*
32ca74bb 121 * Re-encode the fonts.
43341922 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
ba0fe3ec 131 fprintf(fp, "/%s findfont dup length dict begin\n",
132 fe->font->info->name);
43341922 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++)
17c71b41 136 fprintf(fp, "/%s%c", fe->vector[i] ? fe->vector[i] : ".notdef",
137 i % 4 == 3 ? '\n' : ' ');
32ca74bb 138 fprintf(fp, "] def\n");
139 fprintf(fp, "currentdict end\n");
43341922 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 /*
23765aeb 146 * Output the text and graphics.
43341922 147 */
148 pageno = 0;
149 for (page = doc->pages; page; page = page->next) {
7c8c4239 150 text_fragment *frag, *frag_end;
23765aeb 151 rect *r;
43341922 152
153 pageno++;
154 fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno);
ae52aa5d 155 fprintf(fp, "save\n");
43341922 156
138d7ffb 157#if 0
158 {
159 xref *xr;
160 /*
161 * I used this diagnostic briefly to ensure that
162 * cross-reference rectangles were being put where they
163 * should be.
164 */
165 for (xr = page->first_xref; xr; xr = xr->next) {
166 fprintf(fp, "gsave 0.7 setgray %g %g moveto",
17c71b41 167 xr->lx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
138d7ffb 168 fprintf(fp, " %g %g lineto %g %g lineto",
17c71b41 169 xr->lx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT,
170 xr->rx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT);
138d7ffb 171 fprintf(fp, " %g %g lineto closepath fill grestore\n",
17c71b41 172 xr->rx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
138d7ffb 173 }
174 }
175#endif
176
23765aeb 177 for (r = page->first_rect; r; r = r->next) {
178 fprintf(fp, "%g %g moveto %g 0 rlineto 0 %g rlineto "
179 "-%g 0 rlineto closepath fill\n",
17c71b41 180 r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT,
181 r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT,
182 r->w / FUNITS_PER_PT);
23765aeb 183 }
184
7c8c4239 185 frag = page->first_text;
186 while (frag) {
187 font_encoding *fe;
188 int fs;
43341922 189 char *c;
190
7c8c4239 191 /*
192 * Collect all the adjacent text fragments with the
193 * same y-coordinate.
194 */
195 for (frag_end = frag;
196 frag_end && frag_end->y == frag->y;
197 frag_end = frag_end->next);
198
17c71b41 199 fprintf(fp, "%g[", frag->y / FUNITS_PER_PT);
7c8c4239 200
201 fe = NULL;
202 fs = -1;
203
204 while (frag && frag != frag_end) {
205
206 if (frag->fe != fe || frag->fontsize != fs)
207 fprintf(fp, "[%s %d]", frag->fe->name, frag->fontsize);
208 fe = frag->fe;
209 fs = frag->fontsize;
210
17c71b41 211 fprintf(fp, "%g(", frag->x/FUNITS_PER_PT);
7c8c4239 212 for (c = frag->text; *c; c++) {
213 if (*c == '(' || *c == ')' || *c == '\\')
214 fputc('\\', fp);
215 fputc(*c, fp);
216 }
217 fprintf(fp, ")");
43341922 218
7c8c4239 219 frag = frag->next;
43341922 220 }
221
7c8c4239 222 fprintf(fp, "]t\n");
43341922 223 }
224
ae52aa5d 225 fprintf(fp, "restore showpage\n");
43341922 226 }
227
228 fprintf(fp, "%%%%EOF\n");
229
230 fclose(fp);
231
232 sfree(filename);
233}
09358aa7 234
b0f1f943 235static void ps_comment(FILE *fp, char const *leader, word *words)
09358aa7 236{
b0f1f943 237 fprintf(fp, "%s", leader);
09358aa7 238
239 for (; words; words = words->next) {
240 char *text;
241 int type;
242
243 switch (words->type) {
244 case word_HyperLink:
245 case word_HyperEnd:
246 case word_UpperXref:
247 case word_LowerXref:
248 case word_XrefEnd:
249 case word_IndexRef:
250 continue;
251 }
252
253 type = removeattr(words->type);
254
255 switch (type) {
256 case word_Normal:
e4ea58f8 257 text = utoa_dup(words->text, CS_ASCII);
09358aa7 258 break;
259 case word_WhiteSpace:
260 text = dupstr(" ");
261 break;
262 case word_Quote:
263 text = dupstr("'");
264 break;
265 }
266
267 fputs(text, fp);
268 sfree(text);
269 }
270
271 fprintf(fp, "\n");
272}