3e2dd889 |
1 | /* |
2 | * PostScript Type 1 font file support for Halibut |
3 | */ |
4 | /* |
5 | * Type 1 font file formats are specified by Adobe Technical Note |
6 | * #5040: "Supporting Downloadable PostScript Language Fonts". |
7 | * Halibut supports hexadecimal format (section 3.1) and IBM PC format |
8 | * (section 3.3), commonly called PFA and PFB respectively. |
9 | */ |
10 | |
11 | #include <assert.h> |
12 | #include <limits.h> |
44407fea |
13 | #include <stdio.h> |
3e2dd889 |
14 | #include <stdlib.h> |
44407fea |
15 | #include "halibut.h" |
16 | #include "paper.h" |
17 | |
3e2dd889 |
18 | #define PFB_ASCII 1 |
19 | #define PFB_BINARY 2 |
20 | #define PFB_EOF 3 |
21 | |
22 | typedef struct t1_font_Tag t1_font; |
23 | typedef struct t1_data_Tag t1_data; |
24 | |
25 | struct t1_font_Tag { |
26 | t1_data *data; |
27 | size_t length1; |
28 | size_t length2; |
29 | filepos pos; |
30 | }; |
31 | |
32 | struct t1_data_Tag { |
33 | char type; |
34 | size_t length; |
35 | unsigned char *data; |
36 | t1_data *next; |
37 | }; |
38 | |
39 | typedef struct pfstate_Tag { |
40 | t1_data *data; |
41 | t1_data *curblock; |
42 | size_t offset; |
43 | } pfstate; |
44 | |
45 | static void pf_identify(t1_font *tf); |
46 | |
47 | static t1_data *load_pfb_file(FILE *fp, filepos *pos) { |
48 | t1_data *head = NULL, *tail = NULL; |
49 | int c, i; |
50 | char type; |
51 | |
52 | pos->line = 0; |
53 | for (;;) { |
54 | if (fgetc(fp) != 128) abort(); |
55 | type = fgetc(fp); |
56 | if (type == PFB_EOF) return head; |
57 | if (tail) { |
58 | tail->next = snew(t1_data); |
59 | tail = tail->next; |
60 | } else { |
61 | head = snew(t1_data); |
62 | tail = head; |
63 | } |
64 | tail->type = type; |
65 | tail->length = 0; |
66 | for (i = 0; i < 4; i++) { |
67 | c = fgetc(fp); |
68 | if (c == EOF) abort(); |
69 | tail->length |= c << (8 * i); |
70 | } |
71 | tail->data = snewn(tail->length, unsigned char); |
72 | if (fread(tail->data, 1, tail->length, fp) != tail->length) abort(); |
73 | } |
74 | } |
75 | |
76 | static t1_data *load_pfa_file(FILE *fp, filepos *pos) { |
77 | t1_data *ret = snew(t1_data); |
78 | size_t off = 0, len, got; |
79 | |
80 | pos->line = 0; |
81 | ret->type = PFB_ASCII; |
82 | len = 32768; |
83 | ret->data = snewn(len, unsigned char); |
84 | for (;;) { |
85 | got = fread(ret->data + off, 1, len - off, fp); |
86 | off += got; |
87 | if (off != len) break; |
88 | len *= 2; |
89 | ret->data = sresize(ret->data, len, unsigned char); |
90 | } |
91 | ret->data = sresize(ret->data, off, unsigned char); |
92 | ret->length = off; |
93 | return ret; |
94 | } |
c885c2ff |
95 | |
44407fea |
96 | void read_pfa_file(input *in) { |
3e2dd889 |
97 | t1_font *tf = snew(t1_font); |
98 | |
99 | tf->data = load_pfa_file(in->currfp, &in->pos); |
100 | tf->pos = in->pos; |
101 | tf->length1 = tf->length2 = 0; |
102 | fclose(in->currfp); |
103 | pf_identify(tf); |
104 | } |
105 | |
106 | void read_pfb_file(input *in) { |
107 | t1_font *tf = snew(t1_font); |
108 | |
109 | tf->data = load_pfb_file(in->currfp, &in->pos); |
110 | tf->pos = in->pos; |
111 | tf->length1 = tf->length2 = 0; |
112 | fclose(in->currfp); |
113 | pf_identify(tf); |
114 | } |
115 | static char *pf_read_token(pfstate *); |
116 | |
117 | /* |
118 | * Read a character from the initial plaintext part of a Type 1 font |
119 | */ |
120 | static int pf_getc(pfstate *pf) { |
121 | if (pf->offset == pf->curblock->length) { |
122 | if (pf->curblock->next == NULL) return EOF; |
123 | pf->curblock = pf->curblock->next; |
124 | pf->offset = 0; |
125 | } |
126 | if (pf->curblock->type != PFB_ASCII) return EOF; |
127 | return pf->curblock->data[pf->offset++]; |
128 | } |
129 | |
130 | static void pf_ungetc(int c, pfstate *pf) { |
131 | assert(pf->offset > 0); |
132 | pf->offset--; |
133 | assert(c == pf->curblock->data[pf->offset]); |
134 | } |
135 | |
136 | static void pf_rewind(pfstate *pf) { |
137 | pf->curblock = pf->data; |
138 | pf->offset = 0; |
139 | } |
140 | |
141 | static void pf_seek(pfstate *pf, size_t off) { |
142 | t1_data *td = pf->data; |
143 | |
144 | while (td->length < off) { |
145 | off -= td->length; |
146 | td = td->next; |
147 | } |
148 | pf->curblock = td; |
149 | pf->offset = off; |
150 | } |
151 | |
152 | static size_t pf_tell(pfstate *pf) { |
153 | t1_data *td = pf->data; |
154 | size_t o = 0; |
155 | |
156 | while (td != pf->curblock) { |
157 | o += td->length; |
158 | td = td->next; |
159 | } |
160 | return o + pf->offset; |
161 | } |
162 | |
163 | static void pf_identify(t1_font *tf) { |
c885c2ff |
164 | rdstringc rsc = { 0, 0, NULL }; |
165 | char *p; |
44407fea |
166 | size_t len; |
167 | char *fontname; |
168 | font_info *fi; |
c885c2ff |
169 | int c; |
3e2dd889 |
170 | pfstate pfs, *pf = &pfs; |
44407fea |
171 | |
3e2dd889 |
172 | pf->data = tf->data; |
173 | pf_rewind(pf); |
c885c2ff |
174 | do { |
3e2dd889 |
175 | c = pf_getc(pf); |
c885c2ff |
176 | if (c == EOF) { |
177 | sfree(rsc.text); |
3e2dd889 |
178 | error(err_pfeof, &tf->pos); |
c885c2ff |
179 | return; |
180 | } |
181 | rdaddc(&rsc, c); |
182 | } while (c != 012 && c != 015); |
183 | p = rsc.text; |
184 | if ((p = strchr(p, ':')) == NULL) { |
185 | sfree(rsc.text); |
3e2dd889 |
186 | error(err_pfhead, &tf->pos); |
44407fea |
187 | return; |
c885c2ff |
188 | } |
189 | p++; |
44407fea |
190 | p += strspn(p, " \t"); |
191 | len = strcspn(p, " \t"); |
192 | fontname = snewn(len + 1, char); |
193 | memcpy(fontname, p, len); |
194 | fontname[len] = 0; |
c885c2ff |
195 | sfree(rsc.text); |
196 | |
44407fea |
197 | for (fi = all_fonts; fi; fi = fi->next) { |
198 | if (strcmp(fi->name, fontname) == 0) { |
3e2dd889 |
199 | fi->fontfile = tf; |
44407fea |
200 | sfree(fontname); |
201 | return; |
202 | } |
203 | } |
3e2dd889 |
204 | error(err_pfnoafm, &tf->pos, fontname); |
44407fea |
205 | sfree(fontname); |
206 | } |
c885c2ff |
207 | |
208 | /* |
209 | * PostScript white space characters; PLRM3 table 3.1 |
210 | */ |
211 | static int pf_isspace(int c) { |
212 | return c == 000 || c == 011 || c == 012 || c == 014 || c == 015 || |
213 | c == ' '; |
214 | } |
215 | |
216 | /* |
217 | * PostScript special characters; PLRM3 page 27 |
218 | */ |
219 | static int pf_isspecial(int c) { |
220 | return c == '(' || c == ')' || c == '<' || c == '>' || c == '[' || |
221 | c == ']' || c == '{' || c == '}' || c == '/' || c == '%'; |
222 | } |
223 | |
3e2dd889 |
224 | static size_t pf_findtoken(t1_font *tf, size_t off, char const *needle) { |
c885c2ff |
225 | char *tok; |
3e2dd889 |
226 | pfstate pfs, *pf = &pfs; |
c885c2ff |
227 | |
3e2dd889 |
228 | pf->data = tf->data; |
229 | pf_seek(pf, off); |
230 | for (;;) { |
231 | tok = pf_read_token(pf); |
232 | if (tok == NULL) { |
233 | if (pf->offset == 0 && pf->curblock->type == PFB_BINARY) |
234 | pf->curblock = pf->curblock->next; |
235 | else |
236 | return (size_t)-1; |
237 | } else { |
238 | if (strcmp(tok, needle) == 0) { |
239 | sfree(tok); |
240 | return pf_tell(pf); |
241 | } |
242 | sfree(tok); |
243 | } |
c885c2ff |
244 | } |
3e2dd889 |
245 | } |
246 | |
247 | static size_t pf_length1(t1_font *tf) { |
248 | size_t ret; |
249 | |
250 | ret = pf_findtoken(tf, 0, "eexec"); |
251 | if (ret == (size_t)-1) { |
252 | error(err_pfeof, &tf->pos); |
c885c2ff |
253 | return 0; |
254 | } |
3e2dd889 |
255 | return ret; |
c885c2ff |
256 | } |
257 | |
3e2dd889 |
258 | static size_t pf_length2(t1_font *tf) { |
259 | size_t ret; |
260 | |
261 | if (tf->length1 == 0) |
262 | tf->length1 = pf_length1(tf); |
263 | ret = pf_findtoken(tf, tf->length1, "cleartomark"); |
264 | if (ret == (size_t)-1) { |
265 | error(err_pfeof, &tf->pos); |
266 | return 0; |
267 | } |
268 | return ret - 12 - tf->length1; /* backspace over "cleartomark\n" */ |
269 | } |
270 | |
271 | static void pf_getascii(t1_font *tf, size_t off, size_t len, |
272 | char **bufp, size_t *lenp) { |
273 | t1_data *td = tf->data; |
274 | size_t blk, i; |
275 | char *p; |
276 | |
277 | while (td && off >= td->length) { |
278 | off -= td->length; |
279 | td = td->next; |
c885c2ff |
280 | } |
3e2dd889 |
281 | *bufp = NULL; |
282 | *lenp = 0; |
283 | while (td && len) { |
284 | blk = len < td->length ? len : td->length; |
285 | if (td->type == PFB_ASCII) { |
286 | *bufp = sresize(*bufp, *lenp + blk, char); |
287 | memcpy(*bufp + *lenp, td->data + off, blk); |
288 | *lenp += blk; |
289 | } else { |
290 | *bufp = sresize(*bufp, *lenp + blk * 2 + blk / 39 + 3, char); |
291 | p = *bufp + *lenp; |
292 | for (i = 0; i < blk; i++) { |
293 | if (i % 39 == 0) p += sprintf(p, "\n"); |
294 | p += sprintf(p, "%02x", td->data[off + i]); |
295 | } |
296 | p += sprintf(p, "\n"); |
297 | *lenp = p - *bufp; |
298 | } |
299 | len -= blk; |
300 | td = td->next; |
301 | off = 0; |
302 | } |
303 | } |
304 | |
305 | void pf_writeps(font_info const *fi, FILE *ofp) { |
306 | char *buf; |
307 | size_t len; |
308 | |
309 | pf_getascii(fi->fontfile, 0, INT_MAX, &buf, &len); |
310 | fwrite(buf, 1, len, ofp); |
311 | sfree(buf); |
c885c2ff |
312 | } |
313 | |
314 | static int hexval(char c) { |
315 | if (c >= '0' && c <= '9') return c - '0'; |
316 | if (c >= 'A' && c <= 'F') return c - 'A' + 0xA; |
317 | if (c >= 'a' && c <= 'f') return c - 'a' + 0xa; |
318 | return 0; |
319 | } |
320 | |
3e2dd889 |
321 | static void pf_getbinary(t1_font *tf, size_t off, size_t len, |
322 | char **bufp, size_t *lenp) { |
323 | t1_data *td = tf->data; |
324 | size_t blk, i; |
325 | int havenybble = 0; |
326 | char *p, nybble; |
327 | |
328 | while (td && off >= td->length) { |
329 | off -= td->length; |
330 | td = td->next; |
331 | } |
332 | *bufp = NULL; |
333 | *lenp = 0; |
334 | while (td && len) { |
335 | blk = len < td->length ? len : td->length; |
336 | if (td->type == PFB_BINARY) { |
337 | *bufp = sresize(*bufp, *lenp + blk, char); |
338 | memcpy(*bufp + *lenp, td->data + off, blk); |
339 | *lenp += blk; |
340 | } else { |
341 | *bufp = sresize(*bufp, *lenp + blk / 2 + 1, char); |
342 | p = *bufp + *lenp; |
343 | for (i = 0; i < blk; i++) { |
344 | if (pf_isspace(td->data[off + i])) continue; |
345 | if (!havenybble) |
346 | nybble = hexval(td->data[off+i]); |
347 | else |
348 | *p++ = (nybble << 4) | hexval(td->data[off+i]); |
349 | havenybble = !havenybble; |
350 | } |
351 | *lenp = p - *bufp; |
352 | } |
353 | len -= blk; |
354 | td = td->next; |
355 | off = 0; |
356 | } |
357 | } |
358 | |
359 | |
360 | /* |
361 | * Return the initial, unencrypted, part of a font. |
362 | */ |
363 | void pf_part1(font_info *fi, char **bufp, size_t *lenp) { |
364 | t1_font *tf = fi->fontfile; |
365 | |
366 | if (tf->length1 == 0) |
367 | tf->length1 = pf_length1(tf); |
368 | pf_getascii(tf, 0, tf->length1, bufp, lenp); |
369 | } |
370 | |
c885c2ff |
371 | /* |
372 | * Return the middle, encrypted, part of a font. |
373 | */ |
374 | void pf_part2(font_info *fi, char **bufp, size_t *lenp) { |
3e2dd889 |
375 | t1_font *tf = fi->fontfile; |
c885c2ff |
376 | |
3e2dd889 |
377 | if (tf->length2 == 0) |
378 | tf->length2 = pf_length2(tf); |
379 | pf_getbinary(tf, tf->length1, tf->length2, bufp, lenp); |
380 | if (*lenp >= 256) |
381 | *lenp -= 256; |
c885c2ff |
382 | } |
383 | |
3e2dd889 |
384 | static char *pf_read_litstring(pfstate *pf) { |
c885c2ff |
385 | rdstringc rsc = { 0, 0, NULL }; |
386 | int depth = 1; |
387 | int c; |
388 | |
389 | rdaddc(&rsc, '('); |
390 | do { |
3e2dd889 |
391 | c = pf_getc(pf); |
c885c2ff |
392 | switch (c) { |
393 | case '(': |
394 | depth++; break; |
395 | case ')': |
396 | depth--; break; |
397 | case '\\': |
398 | rdaddc(&rsc, '\\'); |
3e2dd889 |
399 | c = pf_getc(pf); |
c885c2ff |
400 | break; |
401 | } |
402 | if (c != EOF) rdaddc(&rsc, c); |
403 | } while (depth > 0 && c != EOF); |
404 | return rsc.text; |
405 | } |
406 | |
3e2dd889 |
407 | static char *pf_read_hexstring(pfstate *pf) { |
c885c2ff |
408 | rdstringc rsc = { 0, 0, NULL }; |
409 | int c; |
410 | |
411 | rdaddc(&rsc, '<'); |
412 | do { |
3e2dd889 |
413 | c = pf_getc(pf); |
c885c2ff |
414 | if (c != EOF) rdaddc(&rsc, c); |
415 | } while (c != '>' && c != EOF); |
416 | return rsc.text; |
417 | } |
418 | |
3e2dd889 |
419 | static char *pf_read_word(pfstate *pf, int c) { |
c885c2ff |
420 | rdstringc rsc = { 0, 0, NULL }; |
421 | |
422 | rdaddc(&rsc, c); |
423 | if (c == '{' || c == '}' || c == '[' || c == ']') |
424 | return rsc.text; |
425 | for (;;) { |
3e2dd889 |
426 | c = pf_getc(pf); |
c885c2ff |
427 | if (pf_isspecial(c) || pf_isspace(c) || c == EOF) break; |
428 | rdaddc(&rsc, c); |
429 | } |
3e2dd889 |
430 | if (pf_isspecial(c)) pf_ungetc(c, pf); |
c885c2ff |
431 | return rsc.text; |
432 | } |
433 | |
3e2dd889 |
434 | static char *pf_read_token(pfstate *pf) { |
c885c2ff |
435 | int c; |
436 | |
437 | do { |
3e2dd889 |
438 | c = pf_getc(pf); |
c885c2ff |
439 | } while (pf_isspace(c)); |
440 | if (c == EOF) return NULL; |
441 | if (c == '%') { |
442 | do { |
3e2dd889 |
443 | c = pf_getc(pf); |
c885c2ff |
444 | } while (c != 012 && c != 015); |
3e2dd889 |
445 | return pf_read_token(pf); |
c885c2ff |
446 | } |
3e2dd889 |
447 | if (c == '(') return pf_read_litstring(pf); |
448 | if (c == '<') return pf_read_hexstring(pf); |
449 | return pf_read_word(pf, c); |
c885c2ff |
450 | } |