If you're going to change the metrics of a font, it's necessary to leave space
[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. */
59 fprintf(fp, "%%%%+ font %s\n", fe->font->name);
60 fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 0\n");
43341922 61 fprintf(fp, "%%%%EndComments\n");
62
63 fprintf(fp, "%%%%BeginProlog\n");
b0f1f943 64 fprintf(fp, "%%%%BeginResource: procset Halibut 0 0\n");
7c8c4239 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,
67f13fa7 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"
ac71b3f5 83 "/t { tdict begin {dup type exec} forall end pop } bind def\n");
7c8c4239 84
b0f1f943 85 fprintf(fp, "%%%%EndResource\n");
43341922 86 fprintf(fp, "%%%%EndProlog\n");
87
88 fprintf(fp, "%%%%BeginSetup\n");
09358aa7 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)
b0f1f943 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);
09358aa7 100
43341922 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
55b2eb15 112 fprintf(fp, "/%s findfont dup length 1 add dict begin\n",
113 fe->font->name);
43341922 114 fprintf(fp, "{1 index /FID ne {def} {pop pop} ifelse} forall\n");
115 fprintf(fp, "/Encoding [\n");
116 for (i = 0; i < 256; i++)
17c71b41 117 fprintf(fp, "/%s%c", fe->vector[i] ? fe->vector[i] : ".notdef",
118 i % 4 == 3 ? '\n' : ' ');
43341922 119 fprintf(fp, "] def /Metrics 256 dict dup begin\n");
120 for (i = 0; i < 256; i++) {
121 if (fe->indices[i] >= 0) {
122 double width = fe->font->widths[fe->indices[i]];
123 fprintf(fp, "/%s %g def\n", fe->vector[i],
17c71b41 124 1000.0 * width / FUNITS_PER_PT);
43341922 125 }
126 }
127 fprintf(fp, "end def currentdict end\n");
128 fprintf(fp, "/fontname-%s exch definefont /%s exch def\n\n",
129 fe->name, fe->name);
130 }
131 fprintf(fp, "%%%%EndSetup\n");
132
133 /*
23765aeb 134 * Output the text and graphics.
43341922 135 */
136 pageno = 0;
137 for (page = doc->pages; page; page = page->next) {
7c8c4239 138 text_fragment *frag, *frag_end;
23765aeb 139 rect *r;
43341922 140
141 pageno++;
142 fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno);
ae52aa5d 143 fprintf(fp, "save\n");
43341922 144
138d7ffb 145#if 0
146 {
147 xref *xr;
148 /*
149 * I used this diagnostic briefly to ensure that
150 * cross-reference rectangles were being put where they
151 * should be.
152 */
153 for (xr = page->first_xref; xr; xr = xr->next) {
154 fprintf(fp, "gsave 0.7 setgray %g %g moveto",
17c71b41 155 xr->lx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
138d7ffb 156 fprintf(fp, " %g %g lineto %g %g lineto",
17c71b41 157 xr->lx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT,
158 xr->rx/FUNITS_PER_PT, xr->by/FUNITS_PER_PT);
138d7ffb 159 fprintf(fp, " %g %g lineto closepath fill grestore\n",
17c71b41 160 xr->rx/FUNITS_PER_PT, xr->ty/FUNITS_PER_PT);
138d7ffb 161 }
162 }
163#endif
164
23765aeb 165 for (r = page->first_rect; r; r = r->next) {
166 fprintf(fp, "%g %g moveto %g 0 rlineto 0 %g rlineto "
167 "-%g 0 rlineto closepath fill\n",
17c71b41 168 r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT,
169 r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT,
170 r->w / FUNITS_PER_PT);
23765aeb 171 }
172
7c8c4239 173 frag = page->first_text;
174 while (frag) {
175 font_encoding *fe;
176 int fs;
43341922 177 char *c;
178
7c8c4239 179 /*
180 * Collect all the adjacent text fragments with the
181 * same y-coordinate.
182 */
183 for (frag_end = frag;
184 frag_end && frag_end->y == frag->y;
185 frag_end = frag_end->next);
186
17c71b41 187 fprintf(fp, "%g[", frag->y / FUNITS_PER_PT);
7c8c4239 188
189 fe = NULL;
190 fs = -1;
191
192 while (frag && frag != frag_end) {
193
194 if (frag->fe != fe || frag->fontsize != fs)
195 fprintf(fp, "[%s %d]", frag->fe->name, frag->fontsize);
196 fe = frag->fe;
197 fs = frag->fontsize;
198
17c71b41 199 fprintf(fp, "%g(", frag->x/FUNITS_PER_PT);
7c8c4239 200 for (c = frag->text; *c; c++) {
201 if (*c == '(' || *c == ')' || *c == '\\')
202 fputc('\\', fp);
203 fputc(*c, fp);
204 }
205 fprintf(fp, ")");
43341922 206
7c8c4239 207 frag = frag->next;
43341922 208 }
209
7c8c4239 210 fprintf(fp, "]t\n");
43341922 211 }
212
ae52aa5d 213 fprintf(fp, "restore showpage\n");
43341922 214 }
215
216 fprintf(fp, "%%%%EOF\n");
217
218 fclose(fp);
219
220 sfree(filename);
221}
09358aa7 222
b0f1f943 223static void ps_comment(FILE *fp, char const *leader, word *words)
09358aa7 224{
b0f1f943 225 fprintf(fp, "%s", leader);
09358aa7 226
227 for (; words; words = words->next) {
228 char *text;
229 int type;
230
231 switch (words->type) {
232 case word_HyperLink:
233 case word_HyperEnd:
234 case word_UpperXref:
235 case word_LowerXref:
236 case word_XrefEnd:
237 case word_IndexRef:
238 continue;
239 }
240
241 type = removeattr(words->type);
242
243 switch (type) {
244 case word_Normal:
e4ea58f8 245 text = utoa_dup(words->text, CS_ASCII);
09358aa7 246 break;
247 case word_WhiteSpace:
248 text = dupstr(" ");
249 break;
250 case word_Quote:
251 text = dupstr("'");
252 break;
253 }
254
255 fputs(text, fp);
256 sfree(text);
257 }
258
259 fprintf(fp, "\n");
260}