2 * Support for sfnt-housed fonts for Halibut
4 * sfnt-housed fonts include TrueType, OpenType, sfnt-housed Type 1
5 * fonts and a couple of bitmap formats.
7 * The various tables that can appear in sfnt-housed fonts are defined
8 * in several places. These include:
10 * The OpenType Specification:
11 * <http://partners.adobe.com/public/developer/opentype/index_spec.html>
13 * The TrueType Reference Manual:
14 * <http://developer.apple.com/textfonts/TTRefMan/>
16 * Microsoft typography specifications:
17 * <http://www.microsoft.com/typography/SpecificationsOverview.mspx>
27 typedef struct sfnt_decode_Tag sfnt_decode
;
28 struct sfnt_decode_Tag
{
29 void (*decoder
)(void *src
, void *dest
);
35 static void decode_uint8(void *src
, void *dest
) {
36 *(unsigned int *)dest
= *(unsigned char *)src
;
38 #define d_uint8 decode_uint8, 1
42 static void decode_int8(void *src
, void *dest
) {
43 *(int *)dest
= *(signed char *)src
;
45 #define d_int8 decode_int8, 1
48 static void decode_uint16(void *src
, void *dest
) {
49 unsigned char *cp
= src
;
50 *(unsigned int *)dest
= (cp
[0] << 8) + cp
[1];
52 #define d_uint16 decode_uint16, 2
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];
59 #define d_int16 decode_int16, 2
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];
66 #define d_uint32 decode_uint32, 4
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];
73 #define d_int32 decode_int32, 4
75 static void decode_skip(void *src
, void *dest
) {
80 #define d_skip(n) decode_skip, (n), 0
82 static void decode_end(void *src
, void *dest
) {
87 #define d_end decode_end, 0, 0
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
;
99 static void *decoden(sfnt_decode
*dec
, void *src
, void *end
, void *dest
,
100 size_t size
, size_t n
) {
102 src
= decode(dec
, src
, end
, dest
);
103 dest
= (char *)dest
+ size
;
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
} };
113 /* Offset subdirectory -- the start of the file */
114 typedef struct offsubdir_Tag offsubdir
;
115 struct offsubdir_Tag
{
116 unsigned scaler_type
;
119 sfnt_decode offsubdir_decode
[] = {
120 { d_uint32
, offsetof(offsubdir
, scaler_type
) },
121 { d_uint16
, offsetof(offsubdir
, numTables
) },
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_maxp 0x6d617870
136 #define TAG_name 0x6e616d65
137 #define TAG_post 0x706f7374
138 #define sfnt_true 0x74727565
140 /* Table directory */
141 typedef struct tabledir_Tag tabledir
;
142 struct tabledir_Tag
{
148 sfnt_decode tabledir_decode
[] = {
149 { d_uint32
, offsetof(tabledir
, tag
) },
150 { d_uint32
, offsetof(tabledir
, checkSum
) },
151 { d_uint32
, offsetof(tabledir
, offset
) },
152 { d_uint32
, offsetof(tabledir
, length
) },
156 /* OS/2 and Windows compatibility table */
157 typedef struct t_OS_2_Tag t_OS_2
;
160 int sTypoAscender
, sTypoDescender
;
161 int sxHeight
, sCapHeight
;
163 sfnt_decode t_OS_2_v0_decode
[] = {
164 { d_uint16
, offsetof(t_OS_2
, version
) },
165 { d_skip(66) }, /* xAvgCharWidth, usWeightClass, usWidthClass, fsType, */
166 /* ySubscriptXSize, ySubscriptYSize, ySubscriptXOffset, */
167 /* ySubscriptYOffset, ySuperscriptXSize, ySuperscriptYSize, */
168 /* ySuperscriptXOffset, ySupercriptYOffset, sFamilyClass, panose, */
169 /* ulUnicodeRange1, ulUnicodeRange2, ulUnicodeRange3, ulUnicodeRange4, */
170 /* achVendID, fsSelection, usFirstCharIndex, usLastCharIndex */
173 sfnt_decode t_OS_2_v1_decode
[] = {
174 { d_uint16
, offsetof(t_OS_2
, version
) },
175 { d_skip(66) }, /* xAvgCharWidth, usWeightClass, usWidthClass, fsType, */
176 /* ySubscriptXSize, ySubscriptYSize, ySubscriptXOffset, */
177 /* ySubscriptYOffset, ySuperscriptXSize, ySuperscriptYSize, */
178 /* ySuperscriptXOffset, ySupercriptYOffset, sFamilyClass, panose, */
179 /* ulUnicodeRange1, ulUnicodeRange2, ulUnicodeRange3, ulUnicodeRange4, */
180 /* achVendID, fsSelection, usFirstCharIndex, usLastCharIndex */
181 { d_int16
, offsetof(t_OS_2
, sTypoAscender
) },
182 { d_int16
, offsetof(t_OS_2
, sTypoDescender
) },
183 { d_skip(14) }, /* sTypoLineGap, usWinAscent, usWinDescent, */
184 /* ulCodePageRange1, ulCodePageRange2 */
187 sfnt_decode t_OS_2_v2_decode
[] = {
188 { d_uint16
, offsetof(t_OS_2
, version
) },
189 { d_skip(66) }, /* xAvgCharWidth, usWeightClass, usWidthClass, fsType, */
190 /* ySubscriptXSize, ySubscriptYSize, ySubscriptXOffset, */
191 /* ySubscriptYOffset, ySuperscriptXSize, ySuperscriptYSize, */
192 /* ySuperscriptXOffset, ySupercriptYOffset, sFamilyClass, panose, */
193 /* ulUnicodeRange1, ulUnicodeRange2, ulUnicodeRange3, ulUnicodeRange4, */
194 /* achVendID, fsSelection, usFirstCharIndex, usLastCharIndex */
195 { d_int16
, offsetof(t_OS_2
, sTypoAscender
) },
196 { d_int16
, offsetof(t_OS_2
, sTypoDescender
) },
197 { d_skip(14) }, /* sTypoLineGap, usWinAscent, usWinDescent, */
198 /* ulCodePageRange1, ulCodePageRange2 */
199 { d_int16
, offsetof(t_OS_2
, sxHeight
) },
200 { d_int16
, offsetof(t_OS_2
, sCapHeight
) },
201 { d_skip(6) }, /* usDefaultChar, usBreakChar, usMaxContext */
205 /* Character to Glyph ('cmap') table */
206 typedef struct t_cmap_Tag t_cmap
;
210 sfnt_decode t_cmap_decode
[] = {
212 { d_uint16
, offsetof(t_cmap
, numTables
) },
215 typedef struct encodingrec_Tag encodingrec
;
216 struct encodingrec_Tag
{
221 sfnt_decode encodingrec_decode
[] = {
222 { d_uint16
, offsetof(encodingrec
, platformID
) },
223 { d_uint16
, offsetof(encodingrec
, encodingID
) },
224 { d_uint32
, offsetof(encodingrec
, offset
) },
227 typedef struct cmap4_Tag cmap4
;
232 sfnt_decode cmap4_decode
[] = {
233 { d_skip(2) }, /* format */
234 { d_uint16
, offsetof(cmap4
, length
) },
235 { d_skip(2) }, /* language */
236 { d_uint16
, offsetof(cmap4
, segCountX2
) },
237 { d_skip(6) }, /* searchRange, entrySelector, rangeShift */
241 /* Font Header ('head') table */
242 typedef struct t_head_Tag t_head
;
245 unsigned fontRevision
;
248 int xMin
, yMin
, xMax
, yMax
;
249 int indexToLocFormat
;
251 sfnt_decode t_head_decode
[] = {
252 { d_uint32
, offsetof(t_head
, version
) },
253 { d_uint32
, offsetof(t_head
, fontRevision
) },
254 { d_skip(8) }, /* checkSumAdjustment, magicNumber, flags */
255 { d_uint16
, offsetof(t_head
, flags
) },
256 { d_uint16
, offsetof(t_head
, unitsPerEm
) },
257 { d_skip(16) }, /* created, modified */
258 { d_int16
, offsetof(t_head
, xMin
) },
259 { d_int16
, offsetof(t_head
, yMin
) },
260 { d_int16
, offsetof(t_head
, xMax
) },
261 { d_int16
, offsetof(t_head
, yMax
) },
262 { d_skip(6) }, /* macStyle, lowestRecPPEM, fontDirectionHint */
263 { d_int16
, offsetof(t_head
, indexToLocFormat
) },
268 /* Horizontal Header ('hhea') table */
269 typedef struct t_hhea_Tag t_hhea
;
275 int metricDataFormat
;
276 unsigned numOfLongHorMetrics
;
278 sfnt_decode t_hhea_decode
[] = {
279 { d_uint32
, offsetof(t_hhea
, version
) },
280 { d_int16
, offsetof(t_hhea
, ascent
) },
281 { d_int16
, offsetof(t_hhea
, descent
) },
282 { d_int16
, offsetof(t_hhea
, lineGap
) },
284 { d_int16
, offsetof(t_hhea
, metricDataFormat
) },
285 { d_uint16
, offsetof(t_hhea
, numOfLongHorMetrics
) },
289 /* Horizontal Metrics ('hmtx') table */
290 sfnt_decode longhormetric_decode
[] = {
296 /* Kerning ('kern') table */
297 typedef struct t_kern_Tag t_kern
;
302 sfnt_decode t_kern_v0_decode
[] = {
303 { d_uint16
, offsetof(t_kern
, version
) },
304 { d_uint16
, offsetof(t_kern
, nTables
) },
307 typedef struct kern_v0_subhdr_Tag kern_v0_subhdr
;
308 struct kern_v0_subhdr_Tag
{
313 sfnt_decode kern_v0_subhdr_decode
[] = {
314 { d_uint16
, offsetof(kern_v0_subhdr
, version
) },
315 { d_uint16
, offsetof(kern_v0_subhdr
, length
) },
316 { d_uint16
, offsetof(kern_v0_subhdr
, coverage
) },
319 #define KERN_V0_HORIZ 0x0001
320 #define KERN_V0_MINIMUM 0x0002
321 #define KERN_V0_CROSSSTREAM 0x0004
322 #define KERN_V0_OVERRIDE 0x0008
323 #define KERN_V0_FORMAT 0xff00
324 #define KERN_V0_FORMAT_0 0x0000
325 sfnt_decode t_kern_v1_decode
[] = {
326 { d_uint32
, offsetof(t_kern
, version
) },
327 { d_uint32
, offsetof(t_kern
, nTables
) },
330 typedef struct kern_v1_subhdr_Tag kern_v1_subhdr
;
331 struct kern_v1_subhdr_Tag
{
335 sfnt_decode kern_v1_subhdr_decode
[] = {
336 { d_uint32
, offsetof(kern_v1_subhdr
, length
) },
337 { d_uint16
, offsetof(kern_v1_subhdr
, coverage
) },
338 { d_skip(2) }, /* tupleIndex */
341 #define KERN_V1_VERTICAL 0x8000
342 #define KERN_V1_CROSSSTREAM 0x4000
343 #define KERN_V1_VARIATION 0x2000
344 #define KERN_V1_FORMAT 0x00ff
345 #define KERN_V1_FORMAT_0 0x0000
346 typedef struct kern_f0_Tag kern_f0
;
350 sfnt_decode kern_f0_decode
[] = {
351 { d_uint16
, offsetof(kern_f0
, nPairs
) },
352 { d_skip(6) }, /* searchRange, entrySelector, rangeShift */
355 typedef struct kern_f0_pair_Tag kern_f0_pair
;
356 struct kern_f0_pair_Tag
{
361 sfnt_decode kern_f0_pair_decode
[] = {
362 { d_uint16
, offsetof(kern_f0_pair
, left
) },
363 { d_uint16
, offsetof(kern_f0_pair
, right
) },
364 { d_int16
, offsetof(kern_f0_pair
, value
) },
368 /* Maximum profile ('maxp') table */
369 typedef struct t_maxp_Tag t_maxp
;
374 sfnt_decode t_maxp_decode
[] = {
375 { d_uint32
, offsetof(t_maxp
, version
) },
376 { d_uint16
, offsetof(t_maxp
, numGlyphs
) },
380 /* Naming ('name') table */
381 typedef struct t_name_Tag t_name
;
382 typedef struct namerecord_Tag namerecord
;
386 unsigned stringOffset
;
387 namerecord
*nameRecord
;
389 sfnt_decode t_name_decode
[] = {
390 { d_uint16
, offsetof(t_name
, format
) },
391 { d_uint16
, offsetof(t_name
, count
) },
392 { d_uint16
, offsetof(t_name
, stringOffset
) },
395 struct namerecord_Tag
{
403 sfnt_decode namerecord_decode
[] = {
404 { d_uint16
, offsetof(namerecord
, platformID
) },
405 { d_uint16
, offsetof(namerecord
, encodingID
) },
406 { d_uint16
, offsetof(namerecord
, languageID
) },
407 { d_uint16
, offsetof(namerecord
, nameID
) },
408 { d_uint16
, offsetof(namerecord
, length
) },
409 { d_uint16
, offsetof(namerecord
, offset
) },
413 /* PostScript compatibility ('post') table */
414 typedef struct t_post_Tag t_post
;
418 int underlinePosition
;
419 int underlineThickness
;
420 unsigned isFixedPitch
;
421 unsigned minMemType42
;
422 unsigned maxMemType42
;
424 sfnt_decode t_post_decode
[] = {
425 { d_uint32
, offsetof(t_post
, format
) },
426 { d_int32
, offsetof(t_post
, italicAngle
) },
427 { d_int16
, offsetof(t_post
, underlinePosition
) },
428 { d_int16
, offsetof(t_post
, underlineThickness
) },
429 { d_uint32
, offsetof(t_post
, isFixedPitch
) },
430 { d_uint32
, offsetof(t_post
, minMemType42
) },
431 { d_uint32
, offsetof(t_post
, maxMemType42
) },
432 { d_skip(8) }, /* minMemType1, maxMemType1 */
438 unsigned short index
;
450 glyph
*glyphsbyindex
;
451 unsigned short *glyphsbyname
;
452 unsigned minmem
, maxmem
;
455 static int sfnt_findtable(sfnt
*sf
, unsigned tag
,
456 void **startp
, void **endp
) {
459 for (i
= 0; i
< sf
->osd
.numTables
; i
++) {
460 if (sf
->td
[i
].tag
== tag
) {
461 *startp
= (char *)sf
->data
+ sf
->td
[i
].offset
;
462 *endp
= (char *)*startp
+ sf
->td
[i
].length
;
469 static char *sfnt_psname(font_info
*fi
) {
470 sfnt
*sf
= fi
->fontfile
;
477 if (!sfnt_findtable(sf
, TAG_name
, &ptr
, &end
)) {
478 error(err_sfntnotable
, &sf
->pos
, "name");
481 ptr
= decode(t_name_decode
, ptr
, end
, &name
);
482 name
.nameRecord
= snewn(name
.count
, namerecord
);
483 ptr
= decoden(namerecord_decode
, ptr
, sf
->end
, name
.nameRecord
,
484 sizeof(*name
.nameRecord
), name
.count
);
485 for (i
= 0; i
< name
.count
; i
++) {
486 nr
= name
.nameRecord
+ i
;
487 if (nr
->nameID
== 6) {
488 /* PostScript name, but can we make sense of it? */
489 if (nr
->platformID
== 1 && nr
->encodingID
== 0) {
490 /* Mac Roman, which is ASCII for our purposes */
491 psname
= snewn(nr
->length
+ 1, char);
492 memcpy(psname
, (char *)ptr
+ nr
->offset
, nr
->length
);
493 psname
[nr
->length
] = 0;
494 sfree(name
.nameRecord
);
499 error(err_sfntnopsname
, &sf
->pos
);
503 static unsigned short *cmp_glyphsbyindex
;
504 static int glyphsbyname_cmp(void const *a
, void const *b
) {
505 glyph ga
= cmp_glyphsbyindex
[*(unsigned short *)a
];
506 glyph gb
= cmp_glyphsbyindex
[*(unsigned short *)b
];
507 if (ga
< gb
) return -1;
508 if (ga
> gb
) return 1;
511 static int glyphsbyname_cmp_search(void const *a
, void const *b
) {
512 glyph ga
= *(glyph
*)a
;
513 glyph gb
= cmp_glyphsbyindex
[*(unsigned short *)b
];
514 if (ga
< gb
) return -1;
515 if (ga
> gb
) return 1;
519 /* Generate an name for a glyph that doesn't have one. */
520 static glyph
genglyph(unsigned idx
) {
522 if (idx
== 0) return glyph_intern(".notdef");
523 sprintf(buf
, "glyph%u", idx
);
524 return glyph_intern(buf
);
528 * Extract data from the 'post' table (mostly glyph mappings)
530 * TODO: cope better with duplicated glyph names (usually .notdef)
531 * TODO: when presented with format 3.0, try to use 'CFF' if present.
533 static void sfnt_mapglyphs(font_info
*fi
) {
534 sfnt
*sf
= fi
->fontfile
;
540 unsigned nextras
, i
, g
;
542 sf
->glyphsbyname
= sf
->glyphsbyindex
= NULL
;
543 if (sfnt_findtable(sf
, TAG_post
, &ptr
, &end
)) {
544 ptr
= decode(t_post_decode
, ptr
, end
, &post
);
546 error(err_sfntbadtable
, &sf
->pos
, "post");
550 sf
->minmem
= post
.minMemType42
;
551 sf
->maxmem
= post
.maxMemType42
;
552 fi
->italicangle
= post
.italicAngle
/ 65536.0;
553 switch (post
.format
) {
555 if (sf
->nglyphs
!= 258) {
556 error(err_sfntbadtable
, &sf
->pos
, "post");
559 sf
->glyphsbyindex
= (glyph
*)tt_std_glyphs
;
562 if ((char *)ptr
+ 2 > (char *)end
) {
563 error(err_sfntbadtable
, &sf
->pos
, "post");
566 ptr
= (char *)ptr
+ 2;
567 if ((char *)ptr
+ 2*sf
->nglyphs
> (char *)end
) {
568 error(err_sfntbadtable
, &sf
->pos
, "post");
572 for (sptr
= (unsigned char *)ptr
+ 2*sf
->nglyphs
;
573 sptr
< (unsigned char *)end
;
576 extraglyphs
= snewn(nextras
, glyph
);
578 for (sptr
= (unsigned char *)ptr
+ 2*sf
->nglyphs
;
579 sptr
< (unsigned char *)end
;
581 memcpy(tmp
, sptr
+ 1, *sptr
);
584 extraglyphs
[i
++] = glyph_intern(tmp
);
586 sf
->glyphsbyindex
= snewn(sf
->nglyphs
, glyph
);
587 for (i
= 0; i
< sf
->nglyphs
; i
++) {
588 decode_uint16((char *)ptr
+ 2*i
, &g
);
590 sf
->glyphsbyindex
[i
] = tt_std_glyphs
[g
];
591 else if (g
< 258 + nextras
)
592 sf
->glyphsbyindex
[i
] = extraglyphs
[g
- 258];
594 error(err_sfntbadtable
, &sf
->pos
, "post");
595 sf
->glyphsbyindex
[i
] = genglyph(i
);
603 error(err_sfnttablevers
, &sf
->pos
, "post");
608 if (!sf
->glyphsbyindex
) {
609 sf
->glyphsbyindex
= snewn(sf
->nglyphs
, glyph
);
610 for (i
= 0; i
< sf
->nglyphs
; i
++)
611 sf
->glyphsbyindex
[i
] = genglyph(i
);
613 /* Construct glyphsbyname */
614 sf
->glyphsbyname
= snewn(sf
->nglyphs
, unsigned short);
615 for (i
= 0; i
< sf
->nglyphs
; i
++)
616 sf
->glyphsbyname
[i
] = i
;
617 cmp_glyphsbyindex
= sf
->glyphsbyindex
;
618 qsort(sf
->glyphsbyname
, sf
->nglyphs
, sizeof(*sf
->glyphsbyname
),
622 glyph
sfnt_indextoglyph(sfnt
*sf
, unsigned idx
) {
623 return sf
->glyphsbyindex
[idx
];
626 unsigned sfnt_nglyphs(sfnt
*sf
) {
630 unsigned sfnt_glyphtoindex(sfnt
*sf
, glyph g
) {
631 cmp_glyphsbyindex
= sf
->glyphsbyindex
;
632 return *(unsigned short *)bsearch(&g
, sf
->glyphsbyname
, sf
->nglyphs
,
633 sizeof(*sf
->glyphsbyname
),
634 glyphsbyname_cmp_search
);
638 * Get data from 'hhea', 'hmtx', and 'OS/2' tables
640 void sfnt_getmetrics(font_info
*fi
) {
641 sfnt
*sf
= fi
->fontfile
;
648 /* First, the bounding box from the 'head' table. */
649 fi
->fontbbox
[0] = sf
->head
.xMin
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
650 fi
->fontbbox
[1] = sf
->head
.yMin
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
651 fi
->fontbbox
[2] = sf
->head
.xMax
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
652 fi
->fontbbox
[3] = sf
->head
.yMax
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
653 if (!sfnt_findtable(sf
, TAG_hhea
, &ptr
, &end
)) {
654 error(err_sfntnotable
, &sf
->pos
, "hhea");
657 if (decode(t_hhea_decode
, ptr
, end
, &hhea
) == NULL
) {
658 error(err_sfntbadtable
, &sf
->pos
, "hhea");
661 if ((hhea
.version
& 0xffff0000) != 0x00010000) {
662 error(err_sfnttablevers
, &sf
->pos
, "hhea");
665 fi
->ascent
= hhea
.ascent
;
666 fi
->descent
= hhea
.descent
;
667 if (hhea
.metricDataFormat
!= 0) {
668 error(err_sfnttablevers
, &sf
->pos
, "hmtx");
671 if (!sfnt_findtable(sf
, TAG_hmtx
, &ptr
, &end
)) {
672 error(err_sfntnotable
, &sf
->pos
, "hmtx");
675 hmtx
= snewn(hhea
.numOfLongHorMetrics
, unsigned);
676 if (decoden(longhormetric_decode
, ptr
, end
, hmtx
, sizeof(*hmtx
),
677 hhea
.numOfLongHorMetrics
) == NULL
) {
678 error(err_sfntbadtable
, &sf
->pos
, "hmtx");
681 for (i
= 0; i
< sf
->nglyphs
; i
++) {
682 glyph_width
*w
= snew(glyph_width
);
683 w
->glyph
= sfnt_indextoglyph(sf
, i
);
684 j
= i
< hhea
.numOfLongHorMetrics ? i
: hhea
.numOfLongHorMetrics
- 1;
685 w
->width
= hmtx
[j
] * UNITS_PER_PT
/ sf
->head
.unitsPerEm
;
686 add234(fi
->widths
, w
);
688 /* Now see if the 'OS/2' table has any useful metrics */
689 if (!sfnt_findtable(sf
, TAG_OS_2
, &ptr
, &end
))
691 if (decode(uint16_decode
, ptr
, end
, &OS_2
.version
) == NULL
)
693 if (OS_2
.version
>= 2) {
694 if (decode(t_OS_2_v2_decode
, ptr
, end
, &OS_2
) == NULL
)
696 fi
->xheight
= OS_2
.sxHeight
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
697 fi
->capheight
= OS_2
.sCapHeight
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
698 } else if (OS_2
.version
== 1) {
699 if (decode(t_OS_2_v1_decode
, ptr
, end
, &OS_2
) == NULL
)
703 fi
->ascent
= OS_2
.sTypoAscender
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
704 fi
->descent
= OS_2
.sTypoDescender
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
707 error(err_sfntbadtable
, &sf
->pos
, "OS/2");
711 * Get kerning data from a 'kern' table
713 * 'kern' tables have two gratuitously different header formats, one
714 * used by Apple and one by Microsoft. Happily, the kerning tables
715 * themselves use the same formats. Halibut only supports simple kern
716 * pairs for horizontal kerning of horizontal text, and ignores
719 static void sfnt_getkern(font_info
*fi
) {
720 sfnt
*sf
= fi
->fontfile
;
722 unsigned version
, i
, j
;
725 if (!sfnt_findtable(sf
, TAG_kern
, &ptr
, &end
))
727 if (!decode(uint16_decode
, ptr
, end
, &version
))
730 ptr
= decode(t_kern_v0_decode
, ptr
, end
, &kern
);
731 else if (version
== 1)
732 ptr
= decode(t_kern_v1_decode
, ptr
, end
, &kern
);
734 if (ptr
== NULL
) goto bad
;
735 for (i
= 0; i
< kern
.nTables
; i
++) {
740 ptr
= decode(kern_v0_subhdr_decode
, ptr
, end
, &sub
);
741 if (ptr
== NULL
) goto bad
;
742 if (sub
.version
!= 0 ||
743 (sub
.coverage
& (KERN_V0_HORIZ
| KERN_V0_MINIMUM
|
744 KERN_V0_CROSSSTREAM
| KERN_V0_FORMAT
)) !=
745 (KERN_V0_HORIZ
| KERN_V0_FORMAT_0
)) {
746 ptr
= (char *)ptr
+ sub
.length
- 6;
751 ptr
= decode(kern_v1_subhdr_decode
, ptr
, end
, &sub
);
752 if (ptr
== NULL
) goto bad
;
753 if ((sub
.coverage
& (KERN_V1_VERTICAL
| KERN_V1_CROSSSTREAM
|
754 KERN_V1_VARIATION
| KERN_V1_FORMAT
)) !=
756 ptr
= (char *)ptr
+ sub
.length
- 8;
760 ptr
= decode(kern_f0_decode
, ptr
, end
, &f0
);
761 if (ptr
== NULL
) goto bad
;
762 kerns
= snewn(f0
.nPairs
, kern_pair
);
763 for (j
= 0; j
< f0
.nPairs
; j
++) {
765 kern_pair
*kp
= kerns
+ j
;
766 ptr
= decode(kern_f0_pair_decode
, ptr
, end
, &p
);
767 if (ptr
== NULL
) goto bad
;
768 if (p
.left
>= sf
->nglyphs
|| p
.right
>= sf
->nglyphs
) goto bad
;
769 kp
->left
= sfnt_indextoglyph(sf
, p
.left
);
770 kp
->right
= sfnt_indextoglyph(sf
, p
.right
);
771 kp
->kern
= p
.value
* UNITS_PER_PT
/ (int)sf
->head
.unitsPerEm
;
772 add234(fi
->kerns
, kp
);
777 error(err_sfntbadtable
, &sf
->pos
, "kern");
782 * Get mapping data from 'cmap' table
784 * We look for either a (0, 3), or (3, 1) table, both of these being
785 * versions of UCS-2. We only handle format 4 of this table, since
786 * that seems to be the only one in use.
788 void sfnt_getmap(font_info
*fi
) {
789 sfnt
*sf
= fi
->fontfile
;
792 void *base
, *ptr
, *end
;
797 for (i
= 0; i
< lenof(fi
->bmp
); i
++)
799 if (!sfnt_findtable(sf
, TAG_cmap
, &ptr
, &end
)) {
800 error(err_sfntnotable
, &sf
->pos
, "cmap");
803 ptr
= decode(t_cmap_decode
, ptr
, end
, &cmap
);
804 if (ptr
== NULL
) goto bad
;
805 esd
= snewn(cmap
.numTables
, encodingrec
);
806 ptr
= decoden(encodingrec_decode
, ptr
, end
, esd
, sizeof(*esd
),
808 if (ptr
== NULL
) goto bad
;
809 for (i
= 0; i
< cmap
.numTables
; i
++) {
810 if (!decode(uint16_decode
, (char *)base
+ esd
[i
].offset
, end
, &format
))
812 if ((esd
[i
].platformID
== 0 && esd
[i
].encodingID
== 3) ||
813 (esd
[i
].platformID
== 3 && esd
[i
].encodingID
== 1)) {
815 if (!decode(uint16_decode
, (char *)base
+ esd
[i
].offset
, end
,
819 unsigned *data
, *endCode
, *startCode
, *idDelta
, *idRangeOffset
;
820 unsigned *glyphIndexArray
;
821 unsigned segcount
, nword
, nglyphindex
, j
;
824 ptr
= decode(cmap4_decode
, (char *)base
+ esd
[i
].offset
, end
,
827 segcount
= cmap4
.segCountX2
/ 2;
828 nword
= cmap4
.length
/ 2 - 7;
829 data
= snewn(nword
, unsigned);
830 if (!decoden(uint16_decode
, ptr
, (char *)ptr
+ nword
* 2,
831 data
, sizeof(*data
), nword
)) goto bad
;
833 startCode
= data
+ segcount
+ 1;
834 idDelta
= startCode
+ segcount
;
835 idRangeOffset
= idDelta
+ segcount
;
836 glyphIndexArray
= idRangeOffset
+ segcount
;
837 nglyphindex
= nword
- segcount
* 4 - 1;
839 for (j
= 0; j
< segcount
; j
++) {
842 if (idRangeOffset
[j
] == 0) {
843 for (k
= startCode
[j
]; k
<= endCode
[j
]; k
++) {
844 idx
= (k
+ idDelta
[j
]) & 0xffff;
846 if (idx
> sf
->nglyphs
) goto bad
;
847 fi
->bmp
[k
] = sfnt_indextoglyph(sf
, idx
);
851 unsigned startidx
= idRangeOffset
[j
]/2 - segcount
+ j
;
852 for (k
= startCode
[j
]; k
<= endCode
[j
]; k
++) {
853 idx
= glyphIndexArray
[startidx
+ k
- startCode
[j
]];
855 idx
= (idx
+ idDelta
[j
]) & 0xffff;
856 if (idx
> sf
->nglyphs
) goto bad
;
857 fi
->bmp
[k
] = sfnt_indextoglyph(sf
, idx
);
867 error(err_sfntnounicmap
, &sf
->pos
);
870 error(err_sfntbadtable
, &sf
->pos
, "cmap");
873 void read_sfnt_file(input
*in
) {
874 sfnt
*sf
= snew(sfnt
);
876 FILE *fp
= in
->currfp
;
877 font_info
*fi
= snew(font_info
);
882 fi
->widths
= newtree234(width_cmp
);
883 fi
->kerns
= newtree234(kern_cmp
);
884 fi
->ligs
= newtree234(lig_cmp
);
885 fi
->fontbbox
[0] = fi
->fontbbox
[1] = fi
->fontbbox
[2] = fi
->fontbbox
[3] = 0;
886 fi
->capheight
= fi
->xheight
= fi
->ascent
= fi
->descent
= 0;
887 fi
->stemh
= fi
->stemv
= fi
->italicangle
= 0;
889 fi
->filetype
= TRUETYPE
;
892 sf
->data
= snewn(sf
->len
, unsigned char);
894 got
= fread((char *)sf
->data
+ off
, 1, sf
->len
- off
, fp
);
896 if (off
!= sf
->len
) break;
898 sf
->data
= sresize(sf
->data
, sf
->len
, unsigned char);
902 sf
->data
= sresize(sf
->data
, sf
->len
, unsigned char);
903 sf
->end
= (char *)sf
->data
+ sf
->len
;
907 ptr
= decode(offsubdir_decode
, sf
->data
, sf
->end
, &sf
->osd
);
909 error(err_sfntbadhdr
, &sf
->pos
);
912 sf
->td
= snewn(sf
->osd
.numTables
, tabledir
);
913 ptr
= decoden(tabledir_decode
, ptr
, sf
->end
, sf
->td
, sizeof(*sf
->td
),
916 error(err_sfntbadhdr
, &sf
->pos
);
919 if (!sfnt_findtable(sf
, TAG_head
, &ptr
, &end
)) {
920 error(err_sfntnotable
, &sf
->pos
, "head");
923 if (decode(t_head_decode
, ptr
, end
, &sf
->head
) == NULL
) {
924 error(err_sfntbadtable
, &sf
->pos
, "head");
927 if ((sf
->head
.version
& 0xffff0000) != 0x00010000) {
928 error(err_sfnttablevers
, &sf
->pos
, "head");
931 if (!sfnt_findtable(sf
, TAG_maxp
, &ptr
, &end
)) {
932 error(err_sfntnotable
, &sf
->pos
, "maxp");
935 if (decode(t_maxp_decode
, ptr
, end
, &maxp
) == NULL
) {
936 error(err_sfntbadtable
, &sf
->pos
, "maxp");
939 if (maxp
.version
< 0x00005000 || maxp
.version
> 0x0001ffff) {
940 error(err_sfnttablevers
, &sf
->pos
, "maxp");
943 sf
->nglyphs
= maxp
.numGlyphs
;
944 fi
->name
= sfnt_psname(fi
);
945 if (fi
->name
== NULL
) return;
950 fi
->next
= all_fonts
;
954 static int sizecmp(const void *a
, const void *b
) {
955 if (*(size_t *)a
< *(size_t *)b
) return -1;
956 if (*(size_t *)a
> *(size_t *)b
) return 1;
961 * The format for embedding TrueType fonts in Postscript is defined in
962 * Adobe Technical Note #5012: The Type 42 Font Format Specification.
963 * <http://partners.adobe.com/public/developer/en/font/5012.Type42_Spec.pdf>
966 void sfnt_writeps(font_info
const *fi
, FILE *ofp
) {
967 unsigned i
, j
, lastbreak
;
968 sfnt
*sf
= fi
->fontfile
;
969 size_t *breaks
, glyfoff
, glyflen
;
970 void *glyfptr
, *glyfend
, *locaptr
, *locaend
;
974 /* XXX Unclear that this is the correct format. */
975 fprintf(ofp
, "%%!PS-TrueTypeFont-%u-%u\n", sf
->osd
.scaler_type
,
976 sf
->head
.fontRevision
);
978 fprintf(ofp
, "%%%%VMUsage: %u %u\n", sf
->minmem
, sf
->maxmem
);
979 fprintf(ofp
, "9 dict dup begin\n");
980 fprintf(ofp
, "/FontType 42 def\n");
981 fprintf(ofp
, "/FontMatrix [1 0 0 1 0 0] def\n");
982 fprintf(ofp
, "/FontName /%s def\n", fi
->name
);
983 fprintf(ofp
, "/Encoding StandardEncoding def\n");
984 if ((sf
->head
.flags
& 0x0003) == 0x0003) {
986 * Sensible font with the origin in the right place, such that
987 * the bounding box is meaningful.
989 fprintf(ofp
, "/FontBBox [%g %g %g %g] readonly def\n",
990 (double)sf
->head
.xMin
/ sf
->head
.unitsPerEm
,
991 (double)sf
->head
.yMin
/ sf
->head
.unitsPerEm
,
992 (double)sf
->head
.xMax
/ sf
->head
.unitsPerEm
,
993 (double)sf
->head
.yMax
/ sf
->head
.unitsPerEm
);
995 /* Non-sensible font. */
996 fprintf(ofp
, "/FontBBox [0 0 0 0] readonly def\n");
998 fprintf(ofp
, "/PaintType 0 def\n");
999 fprintf(ofp
, "/CharStrings %u dict dup begin\n", sf
->nglyphs
);
1000 fprintf(ofp
, "0 1 %u{currentfile token pop exch def}bind for\n",
1002 for (i
= 0; i
< sf
->nglyphs
; i
++)
1003 ps_token(ofp
, &cc
, "/%s", glyph_extern(sfnt_indextoglyph(sf
, i
)));
1004 fprintf(ofp
, "\nend readonly def\n");
1005 fprintf(ofp
, "/sfnts [<");
1006 breaks
= snewn(sf
->osd
.numTables
+ sf
->nglyphs
, size_t);
1007 for (i
= 0; i
< sf
->osd
.numTables
; i
++) {
1008 breaks
[i
] = sf
->td
[i
].offset
;
1010 if (!sfnt_findtable(sf
, TAG_glyf
, &glyfptr
, &glyfend
)) {
1011 error(err_sfntnotable
, &sf
->pos
, "glyf");
1014 glyfoff
= (char *)glyfptr
- (char *)sf
->data
;
1015 glyflen
= (char *)glyfend
- (char *)glyfptr
;
1016 if (!sfnt_findtable(sf
, TAG_loca
, &locaptr
, &locaend
)) {
1017 error(err_sfntnotable
, &sf
->pos
, "loca");
1020 loca
= snewn(sf
->nglyphs
, unsigned);
1021 if (sf
->head
.indexToLocFormat
== 0) {
1022 if (!decoden(uint16_decode
, locaptr
, locaend
, loca
, sizeof(*loca
),
1023 sf
->nglyphs
)) goto badloca
;
1024 for (i
= 0; i
< sf
->nglyphs
; i
++) loca
[i
] *= 2;
1026 if (!decoden(uint32_decode
, locaptr
, locaend
, loca
, sizeof(*loca
),
1027 sf
->nglyphs
)) goto badloca
;
1029 for (i
= 1; i
< sf
->nglyphs
; i
++) {
1030 if (loca
[i
] > glyflen
) goto badloca
;
1031 breaks
[sf
->osd
.numTables
+ i
- 1] = loca
[i
] + glyfoff
;
1033 breaks
[sf
->osd
.numTables
+ sf
->nglyphs
- 1] = sf
->len
;
1034 qsort(breaks
, sf
->osd
.numTables
+ sf
->nglyphs
, sizeof(*breaks
), sizecmp
);
1036 for (i
= 0; i
< sf
->len
; i
++) {
1037 if ((i
- lastbreak
) % 38 == 0) fprintf(ofp
, "\n");
1038 if (i
== breaks
[j
]) {
1039 while (i
== breaks
[j
]) j
++;
1041 fprintf(ofp
, "00><\n");
1043 fprintf(ofp
, "%02x", *((unsigned char *)sf
->data
+ i
));
1045 fprintf(ofp
, "00>] readonly def\n");
1047 fprintf(ofp
, "end /%s exch definefont\n", fi
->name
);
1050 error(err_sfntbadtable
, &sf
->pos
, "loca");
1053 void sfnt_data(font_info
*fi
, char **bufp
, size_t *lenp
) {
1054 sfnt
*sf
= fi
->fontfile
;