Add support for extracting kerning tables from 'kern' tables. Only format 0
[sgt/halibut] / in_sfnt.c
1 /*
2 * Support for sfnt-housed fonts for Halibut
3 *
4 * sfnt-housed fonts include TrueType, OpenType, sfnt-housed Type 1
5 * fonts and a couple of bitmap formats.
6 *
7 * The various tables that can appear in sfnt-housed fonts are defined
8 * in several places. These include:
9 *
10 * The OpenType Specification:
11 * <http://partners.adobe.com/public/developer/opentype/index_spec.html>
12 *
13 * The TrueType Reference Manual:
14 * <http://developer.apple.com/textfonts/TTRefMan/>
15 *
16 * Microsoft typography specifications:
17 * <http://www.microsoft.com/typography/SpecificationsOverview.mspx>
18 */
19
20 #include <assert.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "halibut.h"
25 #include "paper.h"
26
27 typedef struct sfnt_decode_Tag sfnt_decode;
28 struct sfnt_decode_Tag {
29 void (*decoder)(void *src, void *dest);
30 size_t src_len;
31 size_t dest_offset;
32 };
33
34 #if 0 /* unused */
35 static void decode_uint8(void *src, void *dest) {
36 *(unsigned int *)dest = *(unsigned char *)src;
37 }
38 #define d_uint8 decode_uint8, 1
39 #endif
40
41 #if 0 /* unused */
42 static void decode_int8(void *src, void *dest) {
43 *(int *)dest = *(signed char *)src;
44 }
45 #define d_int8 decode_int8, 1
46 #endif
47
48 static void decode_uint16(void *src, void *dest) {
49 unsigned char *cp = src;
50 *(unsigned int *)dest = (cp[0] << 8) + cp[1];
51 }
52 #define d_uint16 decode_uint16, 2
53
54 static void decode_int16(void *src, void *dest) {
55 signed char *cp = src;
56 unsigned char *ucp = src;
57 *(int *)dest = (cp[0] << 8) + ucp[1];
58 }
59 #define d_int16 decode_int16, 2
60
61 static void decode_uint32(void *src, void *dest) {
62 unsigned char *cp = src;
63 *(unsigned int *)dest =
64 (cp[0] << 24) + (cp[1] << 16) + (cp[2] << 8) + cp[3];
65 }
66 #define d_uint32 decode_uint32, 4
67
68 static void decode_int32(void *src, void *dest) {
69 signed char *cp = src;
70 unsigned char *ucp = src;
71 *(int *)dest = (cp[0] << 24) + (ucp[1] << 16) + (ucp[2] << 8) + ucp[3];
72 }
73 #define d_int32 decode_int32, 4
74
75 static void decode_skip(void *src, void *dest) {
76 IGNORE(src);
77 IGNORE(dest);
78 /* do nothing */
79 }
80 #define d_skip(n) decode_skip, (n), 0
81
82 static void decode_end(void *src, void *dest) {
83 IGNORE(src);
84 IGNORE(dest);
85 /* never called */
86 }
87 #define d_end decode_end, 0, 0
88
89 static void *decode(sfnt_decode *dec, void *src, void *end, void *dest) {
90 while (dec->decoder != decode_end) {
91 if ((char *)src + dec->src_len > (char *)end) return NULL;
92 dec->decoder(src, (char *)dest + dec->dest_offset);
93 src = (char *)src + dec->src_len;
94 dec++;
95 }
96 return src;
97 }
98
99 static void *decoden(sfnt_decode *dec, void *src, void *end, void *dest,
100 size_t size, size_t n) {
101 while (n-- && src) {
102 src = decode(dec, src, end, dest);
103 dest = (char *)dest + size;
104 }
105 return src;
106 }
107
108 /* Decoding specs for simple data types */
109 sfnt_decode uint16_decode[] = { { d_uint16, 0 }, { d_end } };
110 sfnt_decode int16_decode[] = { { d_int16, 0 }, { d_end } };
111 sfnt_decode uint32_decode[] = { { d_uint32, 0 }, { d_end } };
112
113 /* Offset subdirectory -- the start of the file */
114 typedef struct offsubdir_Tag offsubdir;
115 struct offsubdir_Tag {
116 unsigned scaler_type;
117 unsigned numTables;
118 };
119 sfnt_decode offsubdir_decode[] = {
120 { d_uint32, offsetof(offsubdir, scaler_type) },
121 { d_uint16, offsetof(offsubdir, numTables) },
122 { d_skip(6) },
123 { d_end }
124 };
125
126 #define sfnt_00010000 0x00010000
127 #define TAG_OS_2 0x4f532f32
128 #define TAG_cmap 0x636d6170
129 #define TAG_glyf 0x676c7966
130 #define TAG_head 0x68656164
131 #define TAG_hhea 0x68686561
132 #define TAG_hmtx 0x686d7478
133 #define TAG_kern 0x6b65726e
134 #define TAG_loca 0x6c6f6361
135 #define TAG_name 0x6e616d65
136 #define TAG_post 0x706f7374
137 #define sfnt_true 0x74727565
138
139 /* Table directory */
140 typedef struct tabledir_Tag tabledir;
141 struct tabledir_Tag {
142 unsigned tag;
143 unsigned checkSum;
144 unsigned offset;
145 unsigned length;
146 };
147 sfnt_decode tabledir_decode[] = {
148 { d_uint32, offsetof(tabledir, tag) },
149 { d_uint32, offsetof(tabledir, checkSum) },
150 { d_uint32, offsetof(tabledir, offset) },
151 { d_uint32, offsetof(tabledir, length) },
152 { d_end }
153 };
154
155 /* OS/2 and Windows compatibility table */
156 typedef struct t_OS_2_Tag t_OS_2;
157 struct t_OS_2_Tag {
158 unsigned version;
159 int sTypoAscender, sTypoDescender;
160 int sxHeight, sCapHeight;
161 };
162 sfnt_decode t_OS_2_v0_decode[] = {
163 { d_uint16, offsetof(t_OS_2, version) },
164 { d_skip(66) }, /* xAvgCharWidth, usWeightClass, usWidthClass, fsType, */
165 /* ySubscriptXSize, ySubscriptYSize, ySubscriptXOffset, */
166 /* ySubscriptYOffset, ySuperscriptXSize, ySuperscriptYSize, */
167 /* ySuperscriptXOffset, ySupercriptYOffset, sFamilyClass, panose, */
168 /* ulUnicodeRange1, ulUnicodeRange2, ulUnicodeRange3, ulUnicodeRange4, */
169 /* achVendID, fsSelection, usFirstCharIndex, usLastCharIndex */
170 { d_end }
171 };
172 sfnt_decode t_OS_2_v1_decode[] = {
173 { d_uint16, offsetof(t_OS_2, version) },
174 { d_skip(66) }, /* xAvgCharWidth, usWeightClass, usWidthClass, fsType, */
175 /* ySubscriptXSize, ySubscriptYSize, ySubscriptXOffset, */
176 /* ySubscriptYOffset, ySuperscriptXSize, ySuperscriptYSize, */
177 /* ySuperscriptXOffset, ySupercriptYOffset, sFamilyClass, panose, */
178 /* ulUnicodeRange1, ulUnicodeRange2, ulUnicodeRange3, ulUnicodeRange4, */
179 /* achVendID, fsSelection, usFirstCharIndex, usLastCharIndex */
180 { d_int16, offsetof(t_OS_2, sTypoAscender) },
181 { d_int16, offsetof(t_OS_2, sTypoDescender) },
182 { d_skip(14) }, /* sTypoLineGap, usWinAscent, usWinDescent, */
183 /* ulCodePageRange1, ulCodePageRange2 */
184 { d_end }
185 };
186 sfnt_decode t_OS_2_v2_decode[] = {
187 { d_uint16, offsetof(t_OS_2, version) },
188 { d_skip(66) }, /* xAvgCharWidth, usWeightClass, usWidthClass, fsType, */
189 /* ySubscriptXSize, ySubscriptYSize, ySubscriptXOffset, */
190 /* ySubscriptYOffset, ySuperscriptXSize, ySuperscriptYSize, */
191 /* ySuperscriptXOffset, ySupercriptYOffset, sFamilyClass, panose, */
192 /* ulUnicodeRange1, ulUnicodeRange2, ulUnicodeRange3, ulUnicodeRange4, */
193 /* achVendID, fsSelection, usFirstCharIndex, usLastCharIndex */
194 { d_int16, offsetof(t_OS_2, sTypoAscender) },
195 { d_int16, offsetof(t_OS_2, sTypoDescender) },
196 { d_skip(14) }, /* sTypoLineGap, usWinAscent, usWinDescent, */
197 /* ulCodePageRange1, ulCodePageRange2 */
198 { d_int16, offsetof(t_OS_2, sxHeight) },
199 { d_int16, offsetof(t_OS_2, sCapHeight) },
200 { d_skip(6) }, /* usDefaultChar, usBreakChar, usMaxContext */
201 { d_end }
202 };
203
204 /* Character to Glyph ('cmap') table */
205 typedef struct t_cmap_Tag t_cmap;
206 struct t_cmap_Tag {
207 unsigned numTables;
208 };
209 sfnt_decode t_cmap_decode[] = {
210 { d_skip(2) },
211 { d_uint16, offsetof(t_cmap, numTables) },
212 { d_end }
213 };
214 typedef struct encodingrec_Tag encodingrec;
215 struct encodingrec_Tag {
216 unsigned platformID;
217 unsigned encodingID;
218 unsigned offset;
219 };
220 sfnt_decode encodingrec_decode[] = {
221 { d_uint16, offsetof(encodingrec, platformID) },
222 { d_uint16, offsetof(encodingrec, encodingID) },
223 { d_uint32, offsetof(encodingrec, offset) },
224 { d_end }
225 };
226 typedef struct cmap4_Tag cmap4;
227 struct cmap4_Tag {
228 unsigned length;
229 unsigned segCountX2;
230 };
231 sfnt_decode cmap4_decode[] = {
232 { d_skip(2) }, /* format */
233 { d_uint16, offsetof(cmap4, length) },
234 { d_skip(2) }, /* language */
235 { d_uint16, offsetof(cmap4, segCountX2) },
236 { d_skip(6) }, /* searchRange, entrySelector, rangeShift */
237 { d_end }
238 };
239
240 /* Font Header ('head') table */
241 typedef struct t_head_Tag t_head;
242 struct t_head_Tag {
243 unsigned version;
244 unsigned fontRevision;
245 unsigned flags;
246 unsigned unitsPerEm;
247 int xMin, yMin, xMax, yMax;
248 int indexToLocFormat;
249 };
250 sfnt_decode t_head_decode[] = {
251 { d_uint32, offsetof(t_head, version) },
252 { d_uint32, offsetof(t_head, fontRevision) },
253 { d_skip(8) }, /* checkSumAdjustment, magicNumber, flags */
254 { d_uint16, offsetof(t_head, flags) },
255 { d_uint16, offsetof(t_head, unitsPerEm) },
256 { d_skip(16) }, /* created, modified */
257 { d_int16, offsetof(t_head, xMin) },
258 { d_int16, offsetof(t_head, yMin) },
259 { d_int16, offsetof(t_head, xMax) },
260 { d_int16, offsetof(t_head, yMax) },
261 { d_skip(6) }, /* macStyle, lowestRecPPEM, fontDirectionHint */
262 { d_int16, offsetof(t_head, indexToLocFormat) },
263 { d_skip(2) },
264 { d_end }
265 };
266
267 /* Horizontal Header ('hhea') table */
268 typedef struct t_hhea_Tag t_hhea;
269 struct t_hhea_Tag {
270 unsigned version;
271 int ascent;
272 int descent;
273 int lineGap;
274 int metricDataFormat;
275 unsigned numOfLongHorMetrics;
276 };
277 sfnt_decode t_hhea_decode[] = {
278 { d_uint32, offsetof(t_hhea, version) },
279 { d_int16, offsetof(t_hhea, ascent) },
280 { d_int16, offsetof(t_hhea, descent) },
281 { d_int16, offsetof(t_hhea, lineGap) },
282 { d_skip(22) },
283 { d_int16, offsetof(t_hhea, metricDataFormat) },
284 { d_uint16, offsetof(t_hhea, numOfLongHorMetrics) },
285 { d_end }
286 };
287
288 /* Horizontal Metrics ('hmtx') table */
289 sfnt_decode longhormetric_decode[] = {
290 { d_uint16, 0 },
291 { d_skip(2) },
292 { d_end }
293 };
294
295 /* Kerning ('kern') table */
296 typedef struct t_kern_Tag t_kern;
297 struct t_kern_Tag {
298 unsigned version;
299 unsigned nTables;
300 };
301 sfnt_decode t_kern_v0_decode[] = {
302 { d_uint16, offsetof(t_kern, version) },
303 { d_uint16, offsetof(t_kern, nTables) },
304 { d_end }
305 };
306 typedef struct kern_v0_subhdr_Tag kern_v0_subhdr;
307 struct kern_v0_subhdr_Tag {
308 unsigned version;
309 unsigned length;
310 unsigned coverage;
311 };
312 sfnt_decode kern_v0_subhdr_decode[] = {
313 { d_uint16, offsetof(kern_v0_subhdr, version) },
314 { d_uint16, offsetof(kern_v0_subhdr, length) },
315 { d_uint16, offsetof(kern_v0_subhdr, coverage) },
316 { d_end }
317 };
318 #define KERN_V0_HORIZ 0x0001
319 #define KERN_V0_MINIMUM 0x0002
320 #define KERN_V0_CROSSSTREAM 0x0004
321 #define KERN_V0_OVERRIDE 0x0008
322 #define KERN_V0_FORMAT 0xff00
323 #define KERN_V0_FORMAT_0 0x0000
324 sfnt_decode t_kern_v1_decode[] = {
325 { d_uint32, offsetof(t_kern, version) },
326 { d_uint32, offsetof(t_kern, nTables) },
327 { d_end }
328 };
329 typedef struct kern_v1_subhdr_Tag kern_v1_subhdr;
330 struct kern_v1_subhdr_Tag {
331 unsigned length;
332 unsigned coverage;
333 };
334 sfnt_decode kern_v1_subhdr_decode[] = {
335 { d_uint32, offsetof(kern_v1_subhdr, length) },
336 { d_uint16, offsetof(kern_v1_subhdr, coverage) },
337 { d_skip(2) }, /* tupleIndex */
338 { d_end }
339 };
340 #define KERN_V1_VERTICAL 0x8000
341 #define KERN_V1_CROSSSTREAM 0x4000
342 #define KERN_V1_VARIATION 0x2000
343 #define KERN_V1_FORMAT 0x00ff
344 #define KERN_V1_FORMAT_0 0x0000
345 typedef struct kern_f0_Tag kern_f0;
346 struct kern_f0_Tag {
347 unsigned nPairs;
348 };
349 sfnt_decode kern_f0_decode[] = {
350 { d_uint16, offsetof(kern_f0, nPairs) },
351 { d_skip(6) }, /* searchRange, entrySelector, rangeShift */
352 { d_end }
353 };
354 typedef struct kern_f0_pair_Tag kern_f0_pair;
355 struct kern_f0_pair_Tag {
356 unsigned left;
357 unsigned right;
358 int value;
359 };
360 sfnt_decode kern_f0_pair_decode[] = {
361 { d_uint16, offsetof(kern_f0_pair, left) },
362 { d_uint16, offsetof(kern_f0_pair, right) },
363 { d_int16, offsetof(kern_f0_pair, value) },
364 { d_end }
365 };
366
367 /* Naming ('name') table */
368 typedef struct t_name_Tag t_name;
369 typedef struct namerecord_Tag namerecord;
370 struct t_name_Tag {
371 unsigned format;
372 unsigned count;
373 unsigned stringOffset;
374 namerecord *nameRecord;
375 };
376 sfnt_decode t_name_decode[] = {
377 { d_uint16, offsetof(t_name, format) },
378 { d_uint16, offsetof(t_name, count) },
379 { d_uint16, offsetof(t_name, stringOffset) },
380 { d_end }
381 };
382 struct namerecord_Tag {
383 unsigned platformID;
384 unsigned encodingID;
385 unsigned languageID;
386 unsigned nameID;
387 unsigned length;
388 unsigned offset;
389 };
390 sfnt_decode namerecord_decode[] = {
391 { d_uint16, offsetof(namerecord, platformID) },
392 { d_uint16, offsetof(namerecord, encodingID) },
393 { d_uint16, offsetof(namerecord, languageID) },
394 { d_uint16, offsetof(namerecord, nameID) },
395 { d_uint16, offsetof(namerecord, length) },
396 { d_uint16, offsetof(namerecord, offset) },
397 { d_end }
398 };
399
400 /* PostScript compatibility ('post') table */
401 typedef struct t_post_Tag t_post;
402 struct t_post_Tag {
403 unsigned format;
404 int italicAngle;
405 int underlinePosition;
406 int underlineThickness;
407 unsigned isFixedPitch;
408 unsigned minMemType42;
409 unsigned maxMemType42;
410 };
411 sfnt_decode t_post_decode[] = {
412 { d_uint32, offsetof(t_post, format) },
413 { d_int32, offsetof(t_post, italicAngle) },
414 { d_int16, offsetof(t_post, underlinePosition) },
415 { d_int16, offsetof(t_post, underlineThickness) },
416 { d_uint32, offsetof(t_post, isFixedPitch) },
417 { d_uint32, offsetof(t_post, minMemType42) },
418 { d_uint32, offsetof(t_post, maxMemType42) },
419 { d_skip(8) }, /* minMemType1, maxMemType1 */
420 { d_end }
421 };
422
423 typedef struct {
424 glyph name;
425 unsigned short index;
426 } glyphmap;
427
428 struct sfnt_Tag {
429 void *data;
430 size_t len;
431 void *end;
432 offsubdir osd;
433 tabledir *td;
434 t_head head;
435 unsigned nglyphs;
436 glyph *glyphsbyindex;
437 unsigned short *glyphsbyname;
438 unsigned minmem, maxmem;
439 };
440
441 static int sfnt_findtable(sfnt *sf, unsigned tag,
442 void **startp, void **endp) {
443 size_t i;
444
445 for (i = 0; i < sf->osd.numTables; i++) {
446 if (sf->td[i].tag == tag) {
447 *startp = (char *)sf->data + sf->td[i].offset;
448 *endp = (char *)*startp + sf->td[i].length;
449 return TRUE;
450 }
451 }
452 return FALSE;
453 }
454
455 static char *sfnt_psname(font_info *fi) {
456 sfnt *sf = fi->fontfile;
457 t_name name;
458 void *ptr, *end;
459 size_t i;
460 char *psname;
461 namerecord *nr;
462
463 if (!sfnt_findtable(sf, TAG_name, &ptr, &end))
464 abort();
465 ptr = decode(t_name_decode, ptr, end, &name);
466 name.nameRecord = snewn(name.count, namerecord);
467 ptr = decoden(namerecord_decode, ptr, sf->end, name.nameRecord,
468 sizeof(*name.nameRecord), name.count);
469 for (i = 0; i < name.count; i++) {
470 nr = name.nameRecord + i;
471 if (nr->nameID == 6) {
472 /* PostScript name, but can we make sense of it? */
473 if (nr->platformID == 1 && nr->encodingID == 0) {
474 /* Mac Roman, which is ASCII for our purposes */
475 psname = snewn(nr->length + 1, char);
476 memcpy(psname, (char *)ptr + nr->offset, nr->length);
477 psname[nr->length] = 0;
478 sfree(name.nameRecord);
479 return psname;
480 }
481 }
482 }
483 return NULL;
484 }
485
486 static unsigned short *cmp_glyphsbyindex;
487 static int glyphsbyname_cmp(void const *a, void const *b) {
488 glyph ga = cmp_glyphsbyindex[*(unsigned short *)a];
489 glyph gb = cmp_glyphsbyindex[*(unsigned short *)b];
490 if (ga < gb) return -1;
491 if (ga > gb) return 1;
492 return 0;
493 }
494 static int glyphsbyname_cmp_search(void const *a, void const *b) {
495 glyph ga = *(glyph *)a;
496 glyph gb = cmp_glyphsbyindex[*(unsigned short *)b];
497 if (ga < gb) return -1;
498 if (ga > gb) return 1;
499 return 0;
500 }
501
502 /*
503 * Extract data from the 'post' table (mostly glyph mappings)
504 *
505 * TODO: cope better with duplicated glyph names (usually .notdef)
506 * TODO: when presented with format 3.0, try to use 'CFF' if present.
507 */
508 static void sfnt_mapglyphs(font_info *fi) {
509 sfnt *sf = fi->fontfile;
510 t_post post;
511 void *ptr, *end;
512 unsigned char *sptr;
513 char tmp[256];
514 glyph *extraglyphs;
515 unsigned nextras, i, g;
516
517 sf->glyphsbyname = sf->glyphsbyindex = NULL;
518 if (!sfnt_findtable(sf, TAG_post, &ptr, &end))
519 abort();
520 ptr = decode(t_post_decode, ptr, end, &post);
521
522 sf->minmem = post.minMemType42;
523 sf->maxmem = post.maxMemType42;
524 fi->italicangle = post.italicAngle / 65536.0;
525 if (ptr == NULL) abort();
526 switch (post.format) {
527 case 0x00010000:
528 sf->nglyphs = 258;
529 sf->glyphsbyindex = (glyph *)tt_std_glyphs;
530 break;
531 case 0x00020000:
532 if ((char *)ptr + 2 > (char *)end) return;
533 decode_uint16(ptr, &sf->nglyphs);
534 ptr = (char *)ptr + 2;
535 if ((char *)ptr + 2*sf->nglyphs > (char *)end) return;
536 nextras = 0;
537 for (sptr = (unsigned char *)ptr + 2*sf->nglyphs;
538 sptr < (unsigned char *)end;
539 sptr += *sptr+1)
540 nextras++;
541 extraglyphs = snewn(nextras, glyph);
542 i = 0;
543 for (sptr = (unsigned char *)ptr + 2*sf->nglyphs;
544 sptr < (unsigned char *)end;
545 sptr += *sptr+1) {
546 memcpy(tmp, sptr + 1, *sptr);
547 tmp[*sptr] = 0;
548 assert(i < nextras);
549 extraglyphs[i++] = glyph_intern(tmp);
550 }
551 sf->glyphsbyindex = snewn(sf->nglyphs, glyph);
552 for (i = 0; i < sf->nglyphs; i++) {
553 decode_uint16((char *)ptr + 2*i, &g);
554 if (g <= 257)
555 sf->glyphsbyindex[i] = tt_std_glyphs[g];
556 else if (g < 258 + nextras)
557 sf->glyphsbyindex[i] = extraglyphs[g - 258];
558 else
559 sf->glyphsbyindex[i] = NOGLYPH;
560 }
561 sfree(extraglyphs);
562 break;
563 default:
564 abort();
565 }
566 /* Construct glyphsbyname */
567 sf->glyphsbyname = snewn(sf->nglyphs, unsigned short);
568 for (i = 0; i < sf->nglyphs; i++)
569 sf->glyphsbyname[i] = i;
570 cmp_glyphsbyindex = sf->glyphsbyindex;
571 qsort(sf->glyphsbyname, sf->nglyphs, sizeof(*sf->glyphsbyname),
572 glyphsbyname_cmp);
573 }
574
575 glyph sfnt_indextoglyph(sfnt *sf, unsigned idx) {
576 return sf->glyphsbyindex[idx];
577 }
578
579 unsigned sfnt_nglyphs(sfnt *sf) {
580 return sf->nglyphs;
581 }
582
583 unsigned sfnt_glyphtoindex(sfnt *sf, glyph g) {
584 cmp_glyphsbyindex = sf->glyphsbyindex;
585 return *(unsigned short *)bsearch(&g, sf->glyphsbyname, sf->nglyphs,
586 sizeof(*sf->glyphsbyname),
587 glyphsbyname_cmp_search);
588 }
589
590 /*
591 * Get data from 'hhea', 'hmtx', and 'OS/2' tables
592 */
593 void sfnt_getmetrics(font_info *fi) {
594 sfnt *sf = fi->fontfile;
595 t_hhea hhea;
596 t_OS_2 OS_2;
597 void *ptr, *end;
598 unsigned i, j;
599 unsigned *hmtx;
600
601 /* First, the bounding box from the 'head' table. */
602 fi->fontbbox[0] = sf->head.xMin * FUNITS_PER_PT / sf->head.unitsPerEm;
603 fi->fontbbox[1] = sf->head.yMin * FUNITS_PER_PT / sf->head.unitsPerEm;
604 fi->fontbbox[2] = sf->head.xMax * FUNITS_PER_PT / sf->head.unitsPerEm;
605 fi->fontbbox[3] = sf->head.yMax * FUNITS_PER_PT / sf->head.unitsPerEm;
606 if (!sfnt_findtable(sf, TAG_hhea, &ptr, &end))
607 abort();
608 if (decode(t_hhea_decode, ptr, end, &hhea) == NULL)
609 abort();
610 if ((hhea.version & 0xffff0000) != 0x00010000)
611 abort();
612 fi->ascent = hhea.ascent;
613 fi->descent = hhea.descent;
614 if (hhea.metricDataFormat != 0)
615 abort();
616 if (!sfnt_findtable(sf, TAG_hmtx, &ptr, &end))
617 abort();
618 hmtx = snewn(hhea.numOfLongHorMetrics, unsigned);
619 if (decoden(longhormetric_decode, ptr, end, hmtx, sizeof(*hmtx),
620 hhea.numOfLongHorMetrics) == NULL)
621 abort();
622 for (i = 0; i < sf->nglyphs; i++) {
623 glyph_width *w = snew(glyph_width);
624 w->glyph = sfnt_indextoglyph(sf, i);
625 j = i < hhea.numOfLongHorMetrics ? i : hhea.numOfLongHorMetrics - 1;
626 w->width = hmtx[j] * UNITS_PER_PT / sf->head.unitsPerEm;
627 add234(fi->widths, w);
628 }
629 /* Now see if the 'OS/2' table has any useful metrics */
630 if (!sfnt_findtable(sf, TAG_OS_2, &ptr, &end))
631 return;
632 if (decode(uint16_decode, ptr, end, &OS_2.version) == NULL)
633 return;
634 if (OS_2.version >= 2) {
635 if (decode(t_OS_2_v2_decode, ptr, end, &OS_2) == NULL)
636 return;
637 fi->xheight = OS_2.sxHeight * FUNITS_PER_PT / sf->head.unitsPerEm;
638 fi->capheight = OS_2.sCapHeight * FUNITS_PER_PT / sf->head.unitsPerEm;
639 } else if (OS_2.version == 1) {
640 if (decode(t_OS_2_v1_decode, ptr, end, &OS_2) == NULL)
641 return;
642 } else
643 return;
644 fi->ascent = OS_2.sTypoAscender * FUNITS_PER_PT / sf->head.unitsPerEm;
645 fi->descent = OS_2.sTypoDescender * FUNITS_PER_PT / sf->head.unitsPerEm;
646 }
647
648 /*
649 * Get kerning data from a 'kern' table
650 *
651 * 'kern' tables have two gratuitously different header formats, one
652 * used by Apple and one by Microsoft. Happily, the kerning tables
653 * themselves use the same formats. Halibut only supports simple kern
654 * pairs for horizontal kerning of horizontal text, and ignores
655 * everything else.
656 */
657 static void sfnt_getkern(font_info *fi) {
658 sfnt *sf = fi->fontfile;
659 t_kern kern;
660 unsigned version, i, j;
661 void *ptr, *end;
662
663 if (!sfnt_findtable(sf, TAG_kern, &ptr, &end))
664 return;
665 if (!decode(uint16_decode, ptr, end, &version))
666 return;
667 if (version == 0)
668 ptr = decode(t_kern_v0_decode, ptr, end, &kern);
669 else if (version == 1)
670 ptr = decode(t_kern_v1_decode, ptr, end, &kern);
671 else return;
672 if (ptr == NULL) return;
673 for (i = 0; i < kern.nTables; i++) {
674 kern_f0 f0;
675 kern_pair *kerns;
676 if (version == 0) {
677 kern_v0_subhdr sub;
678 ptr = decode(kern_v0_subhdr_decode, ptr, end, &sub);
679 if (ptr == NULL) return;
680 if (sub.version != 0 ||
681 (sub.coverage & (KERN_V0_HORIZ | KERN_V0_MINIMUM |
682 KERN_V0_CROSSSTREAM | KERN_V0_FORMAT)) !=
683 (KERN_V0_HORIZ | KERN_V0_FORMAT_0)) {
684 ptr = (char *)ptr + sub.length - 6;
685 continue;
686 }
687 } else {
688 kern_v1_subhdr sub;
689 ptr = decode(kern_v1_subhdr_decode, ptr, end, &sub);
690 if (ptr == NULL) return;
691 if ((sub.coverage & (KERN_V1_VERTICAL | KERN_V1_CROSSSTREAM |
692 KERN_V1_VARIATION | KERN_V1_FORMAT)) !=
693 KERN_V0_FORMAT_0) {
694 ptr = (char *)ptr + sub.length - 8;
695 continue;
696 }
697 }
698 ptr = decode(kern_f0_decode, ptr, end, &f0);
699 if (ptr == NULL) return;
700 kerns = snewn(f0.nPairs, kern_pair);
701 for (j = 0; j < f0.nPairs; j++) {
702 kern_f0_pair p;
703 kern_pair *kp = kerns + j;
704 ptr = decode(kern_f0_pair_decode, ptr, end, &p);
705 if (ptr == NULL) return;
706 kp->left = sfnt_indextoglyph(sf, p.left);
707 kp->right = sfnt_indextoglyph(sf, p.right);
708 kp->kern = p.value * UNITS_PER_PT / (int)sf->head.unitsPerEm;
709 add234(fi->kerns, kp);
710 }
711 }
712 }
713
714 /*
715 * Get mapping data from 'cmap' table
716 *
717 * We look for either a (0, 3), or (3, 1) table, both of these being
718 * versions of UCS-2. We only handle format 4 of this table, since
719 * that seems to be the only one in use.
720 */
721 void sfnt_getmap(font_info *fi) {
722 sfnt *sf = fi->fontfile;
723 t_cmap cmap;
724 encodingrec *esd;
725 void *base, *ptr, *end;
726 unsigned i;
727 unsigned format;
728
729
730 for (i = 0; i < lenof(fi->bmp); i++)
731 fi->bmp[i] = 0xFFFF;
732 if (!sfnt_findtable(sf, TAG_cmap, &ptr, &end))
733 abort();
734 base = ptr;
735 ptr = decode(t_cmap_decode, ptr, end, &cmap);
736 if (ptr == NULL) abort();
737 esd = snewn(cmap.numTables, encodingrec);
738 ptr = decoden(encodingrec_decode, ptr, end, esd, sizeof(*esd),
739 cmap.numTables);
740 if (ptr == NULL) abort();
741 for (i = 0; i < cmap.numTables; i++) {
742 if (!decode(uint16_decode, (char *)base + esd[i].offset, end, &format))
743 abort();
744 if ((esd[i].platformID == 0 && esd[i].encodingID == 3) ||
745 (esd[i].platformID == 3 && esd[i].encodingID == 1)) {
746 /* UCS-2 encoding */
747 if (!decode(uint16_decode, (char *)base + esd[i].offset, end,
748 &format))
749 abort();
750 if (format == 4) {
751 unsigned *data, *endCode, *startCode, *idDelta, *idRangeOffset;
752 unsigned *glyphIndexArray;
753 unsigned segcount, nword, nglyphindex, j;
754 cmap4 cmap4;
755
756 ptr = decode(cmap4_decode, (char *)base + esd[i].offset, end,
757 &cmap4);
758 if (!ptr) abort();
759 segcount = cmap4.segCountX2 / 2;
760 nword = cmap4.length / 2 - 7;
761 data = snewn(nword, unsigned);
762 if (!decoden(uint16_decode, ptr, (char *)ptr + nword * 2,
763 data, sizeof(*data), nword)) abort();
764 endCode = data;
765 startCode = data + segcount + 1;
766 idDelta = startCode + segcount;
767 idRangeOffset = idDelta + segcount;
768 glyphIndexArray = idRangeOffset + segcount;
769 nglyphindex = nword - segcount * 4 - 1;
770
771 for (j = 0; j < segcount; j++) {
772 unsigned k, idx;
773
774 if (idRangeOffset[j] == 0) {
775 for (k = startCode[j]; k <= endCode[j]; k++) {
776 idx = (k + idDelta[j]) & 0xffff;
777 if (idx != 0) {
778 if (idx > sf->nglyphs) abort();
779 fi->bmp[k] = sfnt_indextoglyph(sf, idx);
780 }
781 }
782 } else {
783 unsigned startidx = idRangeOffset[j]/2 - segcount + j;
784 for (k = startCode[j]; k <= endCode[j]; k++) {
785 idx = glyphIndexArray[startidx + k - startCode[j]];
786 if (idx != 0) {
787 idx = (idx + idDelta[j]) & 0xffff;
788 if (idx > sf->nglyphs) abort();
789 fi->bmp[k] = sfnt_indextoglyph(sf, idx);
790 }
791 }
792 }
793 }
794
795 sfree(data);
796 }
797 }
798 }
799 }
800
801 void read_sfnt_file(input *in) {
802 sfnt *sf = snew(sfnt);
803 size_t off = 0, got;
804 FILE *fp = in->currfp;
805 font_info *fi = snew(font_info);
806 /* size_t i; */
807 void *ptr, *end;
808
809 fi->name = NULL;
810 fi->widths = newtree234(width_cmp);
811 fi->kerns = newtree234(kern_cmp);
812 fi->ligs = newtree234(lig_cmp);
813 fi->fontbbox[0] = fi->fontbbox[1] = fi->fontbbox[2] = fi->fontbbox[3] = 0;
814 fi->capheight = fi->xheight = fi->ascent = fi->descent = 0;
815 fi->stemh = fi->stemv = fi->italicangle = 0;
816 fi->fontfile = sf;
817 fi->filetype = TRUETYPE;
818
819 sf->len = 32768;
820 sf->data = snewn(sf->len, unsigned char);
821 for (;;) {
822 got = fread((char *)sf->data + off, 1, sf->len - off, fp);
823 off += got;
824 if (off != sf->len) break;
825 sf->len *= 2;
826 sf->data = sresize(sf->data, sf->len, unsigned char);
827 }
828 sf->len = off;
829 sf->data = sresize(sf->data, sf->len, unsigned char);
830 sf->end = (char *)sf->data + sf->len;
831 decode(offsubdir_decode, sf->data, sf->end, &sf->osd);
832 /*
833 fprintf(stderr, "scaler type = 0x%x; numTables = %u\n",
834 sf->osd.scaler_type, sf->osd.numTables);
835 */
836 sf->td = snewn(sf->osd.numTables, tabledir);
837 decoden(tabledir_decode, (char *)sf->data + 12, sf->end,
838 sf->td, sizeof(*sf->td), sf->osd.numTables);
839 /*
840 for (i = 0; i < sf->osd.numTables; i++)
841 fprintf(stderr, "table tag = '%c%c%c%c'; offset = %#10x; length = %#10x\n",
842 sf->td[i].tag >> 24, sf->td[i].tag >> 16, sf->td[i].tag >> 8, sf->td[i].tag, sf->td[i].offset, sf->td[i].length);
843 */
844 if (!sfnt_findtable(sf, TAG_head, &ptr, &end))
845 abort();
846 if (decode(t_head_decode, ptr, end, &sf->head) == NULL)
847 abort();
848 if ((sf->head.version & 0xffff0000) != 0x00010000)
849 abort();
850 fi->name = sfnt_psname(fi);
851 sfnt_mapglyphs(fi);
852 sfnt_getmetrics(fi);
853 sfnt_getkern(fi);
854 sfnt_getmap(fi);
855 fi->next = all_fonts;
856 all_fonts = fi;
857 fclose(in->currfp);
858 }
859
860 static int sizecmp(const void *a, const void *b) {
861 if (*(size_t *)a < *(size_t *)b) return -1;
862 if (*(size_t *)a > *(size_t *)b) return 1;
863 return 0;
864 }
865
866 /*
867 * The format for embedding TrueType fonts in Postscript is defined in
868 * Adobe Technical Note #5012: The Type 42 Font Format Specification.
869 * <http://partners.adobe.com/public/developer/en/font/5012.Type42_Spec.pdf>
870 */
871
872 void sfnt_writeps(font_info const *fi, FILE *ofp) {
873 unsigned i, j, lastbreak;
874 sfnt *sf = fi->fontfile;
875 size_t *breaks, glyfoff, glyflen;
876 void *glyfptr, *glyfend, *locaptr, *locaend;
877 unsigned *loca;
878 int cc = 0;
879
880 /* XXX Unclear that this is the correct format. */
881 fprintf(ofp, "%%!PS-TrueTypeFont-%u-%u\n", sf->osd.scaler_type,
882 sf->head.fontRevision);
883 if (sf->minmem)
884 fprintf(ofp, "%%%%VMUsage: %u %u\n", sf->minmem, sf->maxmem);
885 fprintf(ofp, "9 dict dup begin\n");
886 fprintf(ofp, "/FontType 42 def\n");
887 fprintf(ofp, "/FontMatrix [1 0 0 1 0 0] def\n");
888 fprintf(ofp, "/FontName /%s def\n", fi->name);
889 fprintf(ofp, "/Encoding StandardEncoding def\n");
890 if ((sf->head.flags & 0x0003) == 0x0003) {
891 /*
892 * Sensible font with the origin in the right place, such that
893 * the bounding box is meaningful.
894 */
895 fprintf(ofp, "/FontBBox [%g %g %g %g] readonly def\n",
896 (double)sf->head.xMin / sf->head.unitsPerEm,
897 (double)sf->head.yMin / sf->head.unitsPerEm,
898 (double)sf->head.xMax / sf->head.unitsPerEm,
899 (double)sf->head.yMax / sf->head.unitsPerEm);
900 } else {
901 /* Non-sensible font. */
902 fprintf(ofp, "/FontBBox [0 0 0 0] readonly def\n");
903 }
904 fprintf(ofp, "/PaintType 0 def\n");
905 fprintf(ofp, "/CharStrings %u dict dup begin\n", sf->nglyphs);
906 fprintf(ofp, "0 1 %u{currentfile token pop exch def}bind for\n",
907 sf->nglyphs - 1);
908 for (i = 0; i < sf->nglyphs; i++)
909 ps_token(ofp, &cc, "/%s", glyph_extern(sfnt_indextoglyph(sf, i)));
910 fprintf(ofp, "\nend readonly def\n");
911 fprintf(ofp, "/sfnts [<");
912 breaks = snewn(sf->osd.numTables + sf->nglyphs, size_t);
913 for (i = 0; i < sf->osd.numTables; i++) {
914 breaks[i] = sf->td[i].offset;
915 }
916 if (!sfnt_findtable(sf, TAG_glyf, &glyfptr, &glyfend))
917 abort();
918 glyfoff = (char *)glyfptr - (char *)sf->data;
919 glyflen = (char *)glyfend - (char *)glyfptr;
920 if (!sfnt_findtable(sf, TAG_loca, &locaptr, &locaend))
921 abort();
922 loca = snewn(sf->nglyphs, unsigned);
923 if (sf->head.indexToLocFormat == 0) {
924 if (!decoden(uint16_decode, locaptr, locaend, loca, sizeof(*loca),
925 sf->nglyphs)) abort();
926 for (i = 0; i < sf->nglyphs; i++) loca[i] *= 2;
927 } else {
928 if (!decoden(uint32_decode, locaptr, locaend, loca, sizeof(*loca),
929 sf->nglyphs)) abort();
930 }
931 for (i = 1; i < sf->nglyphs; i++) {
932 if (loca[i] > glyflen) abort();
933 breaks[sf->osd.numTables + i - 1] = loca[i] + glyfoff;
934 }
935 breaks[sf->osd.numTables + sf->nglyphs - 1] = sf->len;
936 qsort(breaks, sf->osd.numTables + sf->nglyphs, sizeof(*breaks), sizecmp);
937 j = lastbreak = 0;
938 for (i = 0; i < sf->len; i++) {
939 if ((i - lastbreak) % 38 == 0) fprintf(ofp, "\n");
940 if (i == breaks[j]) {
941 while (i == breaks[j]) j++;
942 lastbreak = i;
943 fprintf(ofp, "00><\n");
944 }
945 fprintf(ofp, "%02x", *((unsigned char *)sf->data + i));
946 }
947 fprintf(ofp, "00>] readonly def\n");
948 sfree(breaks);
949 fprintf(ofp, "end /%s exch definefont\n", fi->name);
950 }
951
952 void sfnt_data(font_info *fi, char **bufp, size_t *lenp) {
953 sfnt *sf = fi->fontfile;
954 *bufp = sf->data;
955 *lenp = sf->len;
956 }