Add support for using TrueType fonts, including embedding in PostScript but
[sgt/halibut] / in_pf.c
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>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "halibut.h"
16 #include "paper.h"
17
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 }
95
96 void read_pfa_file(input *in) {
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) {
164 rdstringc rsc = { 0, 0, NULL };
165 char *p;
166 size_t len;
167 char *fontname;
168 font_info *fi;
169 int c;
170 pfstate pfs, *pf = &pfs;
171
172 pf->data = tf->data;
173 pf_rewind(pf);
174 do {
175 c = pf_getc(pf);
176 if (c == EOF) {
177 sfree(rsc.text);
178 error(err_pfeof, &tf->pos);
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);
186 error(err_pfhead, &tf->pos);
187 return;
188 }
189 p++;
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;
195 sfree(rsc.text);
196
197 for (fi = all_fonts; fi; fi = fi->next) {
198 if (strcmp(fi->name, fontname) == 0) {
199 fi->fontfile = tf;
200 fi->filetype = TYPE1;
201 sfree(fontname);
202 return;
203 }
204 }
205 error(err_pfnoafm, &tf->pos, fontname);
206 sfree(fontname);
207 }
208
209 /*
210 * PostScript white space characters; PLRM3 table 3.1
211 */
212 static int pf_isspace(int c) {
213 return c == 000 || c == 011 || c == 012 || c == 014 || c == 015 ||
214 c == ' ';
215 }
216
217 /*
218 * PostScript special characters; PLRM3 page 27
219 */
220 static int pf_isspecial(int c) {
221 return c == '(' || c == ')' || c == '<' || c == '>' || c == '[' ||
222 c == ']' || c == '{' || c == '}' || c == '/' || c == '%';
223 }
224
225 static size_t pf_findtoken(t1_font *tf, size_t off, char const *needle) {
226 char *tok;
227 pfstate pfs, *pf = &pfs;
228
229 pf->data = tf->data;
230 pf_seek(pf, off);
231 for (;;) {
232 tok = pf_read_token(pf);
233 if (tok == NULL) {
234 if (pf->offset == 0 && pf->curblock->type == PFB_BINARY)
235 pf->curblock = pf->curblock->next;
236 else
237 return (size_t)-1;
238 } else {
239 if (strcmp(tok, needle) == 0) {
240 sfree(tok);
241 return pf_tell(pf);
242 }
243 sfree(tok);
244 }
245 }
246 }
247
248 static size_t pf_length1(t1_font *tf) {
249 size_t ret;
250
251 ret = pf_findtoken(tf, 0, "eexec");
252 if (ret == (size_t)-1) {
253 error(err_pfeof, &tf->pos);
254 return 0;
255 }
256 return ret;
257 }
258
259 static size_t pf_length2(t1_font *tf) {
260 size_t ret;
261
262 if (tf->length1 == 0)
263 tf->length1 = pf_length1(tf);
264 ret = pf_findtoken(tf, tf->length1, "cleartomark");
265 if (ret == (size_t)-1) {
266 error(err_pfeof, &tf->pos);
267 return 0;
268 }
269 return ret - 12 - tf->length1; /* backspace over "cleartomark\n" */
270 }
271
272 static void pf_getascii(t1_font *tf, size_t off, size_t len,
273 char **bufp, size_t *lenp) {
274 t1_data *td = tf->data;
275 size_t blk, i;
276 char *p;
277
278 while (td && off >= td->length) {
279 off -= td->length;
280 td = td->next;
281 }
282 *bufp = NULL;
283 *lenp = 0;
284 while (td && len) {
285 blk = len < td->length ? len : td->length;
286 if (td->type == PFB_ASCII) {
287 *bufp = sresize(*bufp, *lenp + blk, char);
288 memcpy(*bufp + *lenp, td->data + off, blk);
289 *lenp += blk;
290 } else {
291 *bufp = sresize(*bufp, *lenp + blk * 2 + blk / 39 + 3, char);
292 p = *bufp + *lenp;
293 for (i = 0; i < blk; i++) {
294 if (i % 39 == 0) p += sprintf(p, "\n");
295 p += sprintf(p, "%02x", td->data[off + i]);
296 }
297 p += sprintf(p, "\n");
298 *lenp = p - *bufp;
299 }
300 len -= blk;
301 td = td->next;
302 off = 0;
303 }
304 }
305
306 void pf_writeps(font_info const *fi, FILE *ofp) {
307 char *buf;
308 size_t len;
309
310 pf_getascii(fi->fontfile, 0, INT_MAX, &buf, &len);
311 fwrite(buf, 1, len, ofp);
312 sfree(buf);
313 }
314
315 static int hexval(char c) {
316 if (c >= '0' && c <= '9') return c - '0';
317 if (c >= 'A' && c <= 'F') return c - 'A' + 0xA;
318 if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
319 return 0;
320 }
321
322 static void pf_getbinary(t1_font *tf, size_t off, size_t len,
323 char **bufp, size_t *lenp) {
324 t1_data *td = tf->data;
325 size_t blk, i;
326 int havenybble = 0;
327 char *p, nybble;
328
329 while (td && off >= td->length) {
330 off -= td->length;
331 td = td->next;
332 }
333 *bufp = NULL;
334 *lenp = 0;
335 while (td && len) {
336 blk = len < td->length ? len : td->length;
337 if (td->type == PFB_BINARY) {
338 *bufp = sresize(*bufp, *lenp + blk, char);
339 memcpy(*bufp + *lenp, td->data + off, blk);
340 *lenp += blk;
341 } else {
342 *bufp = sresize(*bufp, *lenp + blk / 2 + 1, char);
343 p = *bufp + *lenp;
344 for (i = 0; i < blk; i++) {
345 if (pf_isspace(td->data[off + i])) continue;
346 if (!havenybble)
347 nybble = hexval(td->data[off+i]);
348 else
349 *p++ = (nybble << 4) | hexval(td->data[off+i]);
350 havenybble = !havenybble;
351 }
352 *lenp = p - *bufp;
353 }
354 len -= blk;
355 td = td->next;
356 off = 0;
357 }
358 }
359
360
361 /*
362 * Return the initial, unencrypted, part of a font.
363 */
364 void pf_part1(font_info *fi, char **bufp, size_t *lenp) {
365 t1_font *tf = fi->fontfile;
366
367 if (tf->length1 == 0)
368 tf->length1 = pf_length1(tf);
369 pf_getascii(tf, 0, tf->length1, bufp, lenp);
370 }
371
372 /*
373 * Return the middle, encrypted, part of a font.
374 */
375 void pf_part2(font_info *fi, char **bufp, size_t *lenp) {
376 t1_font *tf = fi->fontfile;
377
378 if (tf->length2 == 0)
379 tf->length2 = pf_length2(tf);
380 pf_getbinary(tf, tf->length1, tf->length2, bufp, lenp);
381 if (*lenp >= 256)
382 *lenp -= 256;
383 }
384
385 static char *pf_read_litstring(pfstate *pf) {
386 rdstringc rsc = { 0, 0, NULL };
387 int depth = 1;
388 int c;
389
390 rdaddc(&rsc, '(');
391 do {
392 c = pf_getc(pf);
393 switch (c) {
394 case '(':
395 depth++; break;
396 case ')':
397 depth--; break;
398 case '\\':
399 rdaddc(&rsc, '\\');
400 c = pf_getc(pf);
401 break;
402 }
403 if (c != EOF) rdaddc(&rsc, c);
404 } while (depth > 0 && c != EOF);
405 return rsc.text;
406 }
407
408 static char *pf_read_hexstring(pfstate *pf) {
409 rdstringc rsc = { 0, 0, NULL };
410 int c;
411
412 rdaddc(&rsc, '<');
413 do {
414 c = pf_getc(pf);
415 if (c != EOF) rdaddc(&rsc, c);
416 } while (c != '>' && c != EOF);
417 return rsc.text;
418 }
419
420 static char *pf_read_word(pfstate *pf, int c) {
421 rdstringc rsc = { 0, 0, NULL };
422
423 rdaddc(&rsc, c);
424 if (c == '{' || c == '}' || c == '[' || c == ']')
425 return rsc.text;
426 for (;;) {
427 c = pf_getc(pf);
428 if (pf_isspecial(c) || pf_isspace(c) || c == EOF) break;
429 rdaddc(&rsc, c);
430 }
431 if (pf_isspecial(c)) pf_ungetc(c, pf);
432 return rsc.text;
433 }
434
435 static char *pf_read_token(pfstate *pf) {
436 int c;
437
438 do {
439 c = pf_getc(pf);
440 } while (pf_isspace(c));
441 if (c == EOF) return NULL;
442 if (c == '%') {
443 do {
444 c = pf_getc(pf);
445 } while (c != 012 && c != 015);
446 return pf_read_token(pf);
447 }
448 if (c == '(') return pf_read_litstring(pf);
449 if (c == '<') return pf_read_hexstring(pf);
450 return pf_read_word(pf, c);
451 }