| 1 | /* |
| 2 | * PostScript backend for Halibut |
| 3 | */ |
| 4 | |
| 5 | #include <assert.h> |
| 6 | #include "halibut.h" |
| 7 | #include "paper.h" |
| 8 | |
| 9 | static void ps_comment(FILE *fp, char const *leader, word *words); |
| 10 | static void ps_string(FILE *fp, char const *str); |
| 11 | |
| 12 | paragraph *ps_config_filename(char *filename) |
| 13 | { |
| 14 | return cmdline_cfg_simple("ps-filename", filename, NULL); |
| 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) { |
| 33 | if (p->type == para_Config) { |
| 34 | if (!ustricmp(p->keyword, L"ps-filename")) { |
| 35 | sfree(filename); |
| 36 | filename = dupstr(adv(p->origkeyword)); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fp = fopen(filename, "w"); |
| 42 | if (!fp) { |
| 43 | error(err_cantopenw, filename); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | fprintf(fp, "%%!PS-Adobe-3.0\n"); |
| 48 | fprintf(fp, "%%%%Creator: Halibut, %s\n", version); |
| 49 | fprintf(fp, "%%%%DocumentData: Clean7Bit\n"); |
| 50 | fprintf(fp, "%%%%LanguageLevel: 1\n"); |
| 51 | for (pageno = 0, page = doc->pages; page; page = page->next) |
| 52 | pageno++; |
| 53 | fprintf(fp, "%%%%Pages: %d\n", pageno); |
| 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. */ |
| 60 | if (!fe->font->info->fp) |
| 61 | fprintf(fp, "%%%%+ font %s\n", fe->font->info->name); |
| 62 | fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 2\n"); |
| 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); |
| 67 | fprintf(fp, "%%%%EndComments\n"); |
| 68 | |
| 69 | fprintf(fp, "%%%%BeginProlog\n"); |
| 70 | fprintf(fp, "%%%%BeginResource: procset Halibut 0 2\n"); |
| 71 | /* |
| 72 | * Supply a prologue function which allows a reasonably |
| 73 | * compressed representation of the text on the pages. |
| 74 | * |
| 75 | * "t" expects two arguments: a y-coordinate, and then an array. |
| 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 |
| 81 | * |
| 82 | * "r" takes four arguments, and behaves like "rectfill". |
| 83 | */ |
| 84 | fprintf(fp, |
| 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" |
| 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"); |
| 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"); |
| 115 | |
| 116 | fprintf(fp, "%%%%EndResource\n"); |
| 117 | fprintf(fp, "%%%%EndProlog\n"); |
| 118 | |
| 119 | fprintf(fp, "%%%%BeginSetup\n"); |
| 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) |
| 126 | ps_comment(fp, "% ", p->words); |
| 127 | |
| 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 | |
| 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 | |
| 146 | for (fe = doc->fonts->head; fe; fe = fe->next) { |
| 147 | /* XXX This may request the same font multiple times. */ |
| 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 | } |
| 163 | |
| 164 | /* |
| 165 | * Re-encode the fonts. |
| 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 | |
| 175 | fprintf(fp, "/%s findfont dup length dict begin\n", |
| 176 | fe->font->info->name); |
| 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++) |
| 180 | fprintf(fp, "/%s%c", fe->vector[i] ? fe->vector[i] : ".notdef", |
| 181 | i % 4 == 3 ? '\n' : ' '); |
| 182 | fprintf(fp, "] def\n"); |
| 183 | fprintf(fp, "currentdict end\n"); |
| 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 | /* |
| 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 | /* |
| 202 | * Output the text and graphics. |
| 203 | */ |
| 204 | pageno = 0; |
| 205 | for (page = doc->pages; page; page = page->next) { |
| 206 | text_fragment *frag, *frag_end; |
| 207 | rect *r; |
| 208 | xref *xr; |
| 209 | font_encoding *fe; |
| 210 | int fs; |
| 211 | |
| 212 | pageno++; |
| 213 | fprintf(fp, "%%%%Page: %d %d\n", pageno, pageno); |
| 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"); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | for (r = page->first_rect; r; r = r->next) { |
| 229 | fprintf(fp, "%g %g %g %g r\n", |
| 230 | r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT, |
| 231 | r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT); |
| 232 | } |
| 233 | |
| 234 | frag = page->first_text; |
| 235 | fe = NULL; |
| 236 | fs = -1; |
| 237 | while (frag) { |
| 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 | |
| 246 | fprintf(fp, "%g[", frag->y / FUNITS_PER_PT); |
| 247 | |
| 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 | |
| 255 | fprintf(fp, "%g", frag->x/FUNITS_PER_PT); |
| 256 | ps_string(fp, frag->text); |
| 257 | |
| 258 | frag = frag->next; |
| 259 | } |
| 260 | |
| 261 | fprintf(fp, "]t\n"); |
| 262 | } |
| 263 | |
| 264 | fprintf(fp, "restore showpage\n"); |
| 265 | } |
| 266 | |
| 267 | fprintf(fp, "%%%%EOF\n"); |
| 268 | |
| 269 | fclose(fp); |
| 270 | |
| 271 | sfree(filename); |
| 272 | } |
| 273 | |
| 274 | static void ps_comment(FILE *fp, char const *leader, word *words) |
| 275 | { |
| 276 | fprintf(fp, "%s", leader); |
| 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: |
| 296 | text = utoa_dup(words->text, CS_ASCII); |
| 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 | } |
| 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 > '~') { |
| 319 | fprintf(fp, "\\%03o", 0xFF & (int)*c); |
| 320 | } else { |
| 321 | if (*c == '(' || *c == ')' || *c == '\\') |
| 322 | fputc('\\', fp); |
| 323 | fputc(*c, fp); |
| 324 | } |
| 325 | } |
| 326 | fprintf(fp, ")"); |
| 327 | } |