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_name 0x6e616d65
136 #define TAG_post 0x706f7374
137 #define sfnt_true 0x74727565
139 /* Table directory */
140 typedef struct tabledir_Tag tabledir
;
141 struct tabledir_Tag
{
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
) },
155 /* OS/2 and Windows compatibility table */
156 typedef struct t_OS_2_Tag t_OS_2
;
159 int sTypoAscender
, sTypoDescender
;
160 int sxHeight
, sCapHeight
;
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 */
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 */
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 */
204 /* Character to Glyph ('cmap') table */
205 typedef struct t_cmap_Tag t_cmap
;
209 sfnt_decode t_cmap_decode
[] = {
211 { d_uint16
, offsetof(t_cmap
, numTables
) },
214 typedef struct encodingrec_Tag encodingrec
;
215 struct encodingrec_Tag
{
220 sfnt_decode encodingrec_decode
[] = {
221 { d_uint16
, offsetof(encodingrec
, platformID
) },
222 { d_uint16
, offsetof(encodingrec
, encodingID
) },
223 { d_uint32
, offsetof(encodingrec
, offset
) },
226 typedef struct cmap4_Tag cmap4
;
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 */
240 /* Font Header ('head') table */
241 typedef struct t_head_Tag t_head
;
244 unsigned fontRevision
;
247 int xMin
, yMin
, xMax
, yMax
;
248 int indexToLocFormat
;
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
) },
267 /* Horizontal Header ('hhea') table */
268 typedef struct t_hhea_Tag t_hhea
;
274 int metricDataFormat
;
275 unsigned numOfLongHorMetrics
;
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
) },
283 { d_int16
, offsetof(t_hhea
, metricDataFormat
) },
284 { d_uint16
, offsetof(t_hhea
, numOfLongHorMetrics
) },
288 /* Horizontal Metrics ('hmtx') table */
289 sfnt_decode longhormetric_decode
[] = {
295 /* Kerning ('kern') table */
296 typedef struct t_kern_Tag t_kern
;
301 sfnt_decode t_kern_v0_decode
[] = {
302 { d_uint16
, offsetof(t_kern
, version
) },
303 { d_uint16
, offsetof(t_kern
, nTables
) },
306 typedef struct kern_v0_subhdr_Tag kern_v0_subhdr
;
307 struct kern_v0_subhdr_Tag
{
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
) },
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
) },
329 typedef struct kern_v1_subhdr_Tag kern_v1_subhdr
;
330 struct kern_v1_subhdr_Tag
{
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 */
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
;
349 sfnt_decode kern_f0_decode
[] = {
350 { d_uint16
, offsetof(kern_f0
, nPairs
) },
351 { d_skip(6) }, /* searchRange, entrySelector, rangeShift */
354 typedef struct kern_f0_pair_Tag kern_f0_pair
;
355 struct kern_f0_pair_Tag
{
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
) },
367 /* Naming ('name') table */
368 typedef struct t_name_Tag t_name
;
369 typedef struct namerecord_Tag namerecord
;
373 unsigned stringOffset
;
374 namerecord
*nameRecord
;
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
) },
382 struct namerecord_Tag
{
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
) },
400 /* PostScript compatibility ('post') table */
401 typedef struct t_post_Tag t_post
;
405 int underlinePosition
;
406 int underlineThickness
;
407 unsigned isFixedPitch
;
408 unsigned minMemType42
;
409 unsigned maxMemType42
;
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 */
425 unsigned short index
;
437 glyph
*glyphsbyindex
;
438 unsigned short *glyphsbyname
;
439 unsigned minmem
, maxmem
;
442 static int sfnt_findtable(sfnt
*sf
, unsigned tag
,
443 void **startp
, void **endp
) {
446 for (i
= 0; i
< sf
->osd
.numTables
; i
++) {
447 if (sf
->td
[i
].tag
== tag
) {
448 *startp
= (char *)sf
->data
+ sf
->td
[i
].offset
;
449 *endp
= (char *)*startp
+ sf
->td
[i
].length
;
456 static char *sfnt_psname(font_info
*fi
) {
457 sfnt
*sf
= fi
->fontfile
;
464 if (!sfnt_findtable(sf
, TAG_name
, &ptr
, &end
)) {
465 error(err_sfntnotable
, &sf
->pos
, "name");
468 ptr
= decode(t_name_decode
, ptr
, end
, &name
);
469 name
.nameRecord
= snewn(name
.count
, namerecord
);
470 ptr
= decoden(namerecord_decode
, ptr
, sf
->end
, name
.nameRecord
,
471 sizeof(*name
.nameRecord
), name
.count
);
472 for (i
= 0; i
< name
.count
; i
++) {
473 nr
= name
.nameRecord
+ i
;
474 if (nr
->nameID
== 6) {
475 /* PostScript name, but can we make sense of it? */
476 if (nr
->platformID
== 1 && nr
->encodingID
== 0) {
477 /* Mac Roman, which is ASCII for our purposes */
478 psname
= snewn(nr
->length
+ 1, char);
479 memcpy(psname
, (char *)ptr
+ nr
->offset
, nr
->length
);
480 psname
[nr
->length
] = 0;
481 sfree(name
.nameRecord
);
486 error(err_sfntnopsname
, &sf
->pos
);
490 static unsigned short *cmp_glyphsbyindex
;
491 static int glyphsbyname_cmp(void const *a
, void const *b
) {
492 glyph ga
= cmp_glyphsbyindex
[*(unsigned short *)a
];
493 glyph gb
= cmp_glyphsbyindex
[*(unsigned short *)b
];
494 if (ga
< gb
) return -1;
495 if (ga
> gb
) return 1;
498 static int glyphsbyname_cmp_search(void const *a
, void const *b
) {
499 glyph ga
= *(glyph
*)a
;
500 glyph gb
= cmp_glyphsbyindex
[*(unsigned short *)b
];
501 if (ga
< gb
) return -1;
502 if (ga
> gb
) return 1;
507 * Extract data from the 'post' table (mostly glyph mappings)
509 * TODO: cope better with duplicated glyph names (usually .notdef)
510 * TODO: when presented with format 3.0, try to use 'CFF' if present.
512 static void sfnt_mapglyphs(font_info
*fi
) {
513 sfnt
*sf
= fi
->fontfile
;
519 unsigned nextras
, i
, g
;
521 sf
->glyphsbyname
= sf
->glyphsbyindex
= NULL
;
522 if (!sfnt_findtable(sf
, TAG_post
, &ptr
, &end
)) {
523 error(err_sfntnotable
, &sf
->pos
, "post");
526 ptr
= decode(t_post_decode
, ptr
, end
, &post
);
528 error(err_sfntbadtable
, &sf
->pos
, "post");
532 sf
->minmem
= post
.minMemType42
;
533 sf
->maxmem
= post
.maxMemType42
;
534 fi
->italicangle
= post
.italicAngle
/ 65536.0;
535 switch (post
.format
) {
538 sf
->glyphsbyindex
= (glyph
*)tt_std_glyphs
;
541 if ((char *)ptr
+ 2 > (char *)end
) return;
542 decode_uint16(ptr
, &sf
->nglyphs
);
543 ptr
= (char *)ptr
+ 2;
544 if ((char *)ptr
+ 2*sf
->nglyphs
> (char *)end
) return;
546 for (sptr
= (unsigned char *)ptr
+ 2*sf
->nglyphs
;
547 sptr
< (unsigned char *)end
;
550 extraglyphs
= snewn(nextras
, glyph
);
552 for (sptr
= (unsigned char *)ptr
+ 2*sf
->nglyphs
;
553 sptr
< (unsigned char *)end
;
555 memcpy(tmp
, sptr
+ 1, *sptr
);
558 extraglyphs
[i
++] = glyph_intern(tmp
);
560 sf
->glyphsbyindex
= snewn(sf
->nglyphs
, glyph
);
561 for (i
= 0; i
< sf
->nglyphs
; i
++) {
562 decode_uint16((char *)ptr
+ 2*i
, &g
);
564 sf
->glyphsbyindex
[i
] = tt_std_glyphs
[g
];
565 else if (g
< 258 + nextras
)
566 sf
->glyphsbyindex
[i
] = extraglyphs
[g
- 258];
568 sf
->glyphsbyindex
[i
] = NOGLYPH
;
573 error(err_sfnttablevers
, &sf
->pos
, "post");
576 /* Construct glyphsbyname */
577 sf
->glyphsbyname
= snewn(sf
->nglyphs
, unsigned short);
578 for (i
= 0; i
< sf
->nglyphs
; i
++)
579 sf
->glyphsbyname
[i
] = i
;
580 cmp_glyphsbyindex
= sf
->glyphsbyindex
;
581 qsort(sf
->glyphsbyname
, sf
->nglyphs
, sizeof(*sf
->glyphsbyname
),
585 glyph
sfnt_indextoglyph(sfnt
*sf
, unsigned idx
) {
586 return sf
->glyphsbyindex
[idx
];
589 unsigned sfnt_nglyphs(sfnt
*sf
) {
593 unsigned sfnt_glyphtoindex(sfnt
*sf
, glyph g
) {
594 cmp_glyphsbyindex
= sf
->glyphsbyindex
;
595 return *(unsigned short *)bsearch(&g
, sf
->glyphsbyname
, sf
->nglyphs
,
596 sizeof(*sf
->glyphsbyname
),
597 glyphsbyname_cmp_search
);
601 * Get data from 'hhea', 'hmtx', and 'OS/2' tables
603 void sfnt_getmetrics(font_info
*fi
) {
604 sfnt
*sf
= fi
->fontfile
;
611 /* First, the bounding box from the 'head' table. */
612 fi
->fontbbox
[0] = sf
->head
.xMin
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
613 fi
->fontbbox
[1] = sf
->head
.yMin
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
614 fi
->fontbbox
[2] = sf
->head
.xMax
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
615 fi
->fontbbox
[3] = sf
->head
.yMax
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
616 if (!sfnt_findtable(sf
, TAG_hhea
, &ptr
, &end
)) {
617 error(err_sfntnotable
, &sf
->pos
, "hhea");
620 if (decode(t_hhea_decode
, ptr
, end
, &hhea
) == NULL
) {
621 error(err_sfntbadtable
, &sf
->pos
, "hhea");
624 if ((hhea
.version
& 0xffff0000) != 0x00010000) {
625 error(err_sfnttablevers
, &sf
->pos
, "hhea");
628 fi
->ascent
= hhea
.ascent
;
629 fi
->descent
= hhea
.descent
;
630 if (hhea
.metricDataFormat
!= 0) {
631 error(err_sfnttablevers
, &sf
->pos
, "hmtx");
634 if (!sfnt_findtable(sf
, TAG_hmtx
, &ptr
, &end
)) {
635 error(err_sfntnotable
, &sf
->pos
, "hmtx");
638 hmtx
= snewn(hhea
.numOfLongHorMetrics
, unsigned);
639 if (decoden(longhormetric_decode
, ptr
, end
, hmtx
, sizeof(*hmtx
),
640 hhea
.numOfLongHorMetrics
) == NULL
) {
641 error(err_sfntbadtable
, &sf
->pos
, "hmtx");
644 for (i
= 0; i
< sf
->nglyphs
; i
++) {
645 glyph_width
*w
= snew(glyph_width
);
646 w
->glyph
= sfnt_indextoglyph(sf
, i
);
647 j
= i
< hhea
.numOfLongHorMetrics ? i
: hhea
.numOfLongHorMetrics
- 1;
648 w
->width
= hmtx
[j
] * UNITS_PER_PT
/ sf
->head
.unitsPerEm
;
649 add234(fi
->widths
, w
);
651 /* Now see if the 'OS/2' table has any useful metrics */
652 if (!sfnt_findtable(sf
, TAG_OS_2
, &ptr
, &end
))
654 if (decode(uint16_decode
, ptr
, end
, &OS_2
.version
) == NULL
)
656 if (OS_2
.version
>= 2) {
657 if (decode(t_OS_2_v2_decode
, ptr
, end
, &OS_2
) == NULL
)
659 fi
->xheight
= OS_2
.sxHeight
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
660 fi
->capheight
= OS_2
.sCapHeight
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
661 } else if (OS_2
.version
== 1) {
662 if (decode(t_OS_2_v1_decode
, ptr
, end
, &OS_2
) == NULL
)
666 fi
->ascent
= OS_2
.sTypoAscender
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
667 fi
->descent
= OS_2
.sTypoDescender
* FUNITS_PER_PT
/ sf
->head
.unitsPerEm
;
670 error(err_sfntbadtable
, &sf
->pos
, "OS/2");
674 * Get kerning data from a 'kern' table
676 * 'kern' tables have two gratuitously different header formats, one
677 * used by Apple and one by Microsoft. Happily, the kerning tables
678 * themselves use the same formats. Halibut only supports simple kern
679 * pairs for horizontal kerning of horizontal text, and ignores
682 static void sfnt_getkern(font_info
*fi
) {
683 sfnt
*sf
= fi
->fontfile
;
685 unsigned version
, i
, j
;
688 if (!sfnt_findtable(sf
, TAG_kern
, &ptr
, &end
))
690 if (!decode(uint16_decode
, ptr
, end
, &version
))
693 ptr
= decode(t_kern_v0_decode
, ptr
, end
, &kern
);
694 else if (version
== 1)
695 ptr
= decode(t_kern_v1_decode
, ptr
, end
, &kern
);
697 if (ptr
== NULL
) goto bad
;
698 for (i
= 0; i
< kern
.nTables
; i
++) {
703 ptr
= decode(kern_v0_subhdr_decode
, ptr
, end
, &sub
);
704 if (ptr
== NULL
) goto bad
;
705 if (sub
.version
!= 0 ||
706 (sub
.coverage
& (KERN_V0_HORIZ
| KERN_V0_MINIMUM
|
707 KERN_V0_CROSSSTREAM
| KERN_V0_FORMAT
)) !=
708 (KERN_V0_HORIZ
| KERN_V0_FORMAT_0
)) {
709 ptr
= (char *)ptr
+ sub
.length
- 6;
714 ptr
= decode(kern_v1_subhdr_decode
, ptr
, end
, &sub
);
715 if (ptr
== NULL
) goto bad
;
716 if ((sub
.coverage
& (KERN_V1_VERTICAL
| KERN_V1_CROSSSTREAM
|
717 KERN_V1_VARIATION
| KERN_V1_FORMAT
)) !=
719 ptr
= (char *)ptr
+ sub
.length
- 8;
723 ptr
= decode(kern_f0_decode
, ptr
, end
, &f0
);
724 if (ptr
== NULL
) goto bad
;
725 kerns
= snewn(f0
.nPairs
, kern_pair
);
726 for (j
= 0; j
< f0
.nPairs
; j
++) {
728 kern_pair
*kp
= kerns
+ j
;
729 ptr
= decode(kern_f0_pair_decode
, ptr
, end
, &p
);
730 if (ptr
== NULL
) goto bad
;
731 if (p
.left
>= sf
->nglyphs
|| p
.right
>= sf
->nglyphs
) goto bad
;
732 kp
->left
= sfnt_indextoglyph(sf
, p
.left
);
733 kp
->right
= sfnt_indextoglyph(sf
, p
.right
);
734 kp
->kern
= p
.value
* UNITS_PER_PT
/ (int)sf
->head
.unitsPerEm
;
735 add234(fi
->kerns
, kp
);
740 error(err_sfntbadtable
, &sf
->pos
, "kern");
745 * Get mapping data from 'cmap' table
747 * We look for either a (0, 3), or (3, 1) table, both of these being
748 * versions of UCS-2. We only handle format 4 of this table, since
749 * that seems to be the only one in use.
751 void sfnt_getmap(font_info
*fi
) {
752 sfnt
*sf
= fi
->fontfile
;
755 void *base
, *ptr
, *end
;
760 for (i
= 0; i
< lenof(fi
->bmp
); i
++)
762 if (!sfnt_findtable(sf
, TAG_cmap
, &ptr
, &end
)) {
763 error(err_sfntnotable
, &sf
->pos
, "cmap");
766 ptr
= decode(t_cmap_decode
, ptr
, end
, &cmap
);
767 if (ptr
== NULL
) goto bad
;
768 esd
= snewn(cmap
.numTables
, encodingrec
);
769 ptr
= decoden(encodingrec_decode
, ptr
, end
, esd
, sizeof(*esd
),
771 if (ptr
== NULL
) goto bad
;
772 for (i
= 0; i
< cmap
.numTables
; i
++) {
773 if (!decode(uint16_decode
, (char *)base
+ esd
[i
].offset
, end
, &format
))
775 if ((esd
[i
].platformID
== 0 && esd
[i
].encodingID
== 3) ||
776 (esd
[i
].platformID
== 3 && esd
[i
].encodingID
== 1)) {
778 if (!decode(uint16_decode
, (char *)base
+ esd
[i
].offset
, end
,
782 unsigned *data
, *endCode
, *startCode
, *idDelta
, *idRangeOffset
;
783 unsigned *glyphIndexArray
;
784 unsigned segcount
, nword
, nglyphindex
, j
;
787 ptr
= decode(cmap4_decode
, (char *)base
+ esd
[i
].offset
, end
,
790 segcount
= cmap4
.segCountX2
/ 2;
791 nword
= cmap4
.length
/ 2 - 7;
792 data
= snewn(nword
, unsigned);
793 if (!decoden(uint16_decode
, ptr
, (char *)ptr
+ nword
* 2,
794 data
, sizeof(*data
), nword
)) goto bad
;
796 startCode
= data
+ segcount
+ 1;
797 idDelta
= startCode
+ segcount
;
798 idRangeOffset
= idDelta
+ segcount
;
799 glyphIndexArray
= idRangeOffset
+ segcount
;
800 nglyphindex
= nword
- segcount
* 4 - 1;
802 for (j
= 0; j
< segcount
; j
++) {
805 if (idRangeOffset
[j
] == 0) {
806 for (k
= startCode
[j
]; k
<= endCode
[j
]; k
++) {
807 idx
= (k
+ idDelta
[j
]) & 0xffff;
809 if (idx
> sf
->nglyphs
) goto bad
;
810 fi
->bmp
[k
] = sfnt_indextoglyph(sf
, idx
);
814 unsigned startidx
= idRangeOffset
[j
]/2 - segcount
+ j
;
815 for (k
= startCode
[j
]; k
<= endCode
[j
]; k
++) {
816 idx
= glyphIndexArray
[startidx
+ k
- startCode
[j
]];
818 idx
= (idx
+ idDelta
[j
]) & 0xffff;
819 if (idx
> sf
->nglyphs
) goto bad
;
820 fi
->bmp
[k
] = sfnt_indextoglyph(sf
, idx
);
830 error(err_sfntnounicmap
, &sf
->pos
);
833 error(err_sfntbadtable
, &sf
->pos
, "cmap");
836 void read_sfnt_file(input
*in
) {
837 sfnt
*sf
= snew(sfnt
);
839 FILE *fp
= in
->currfp
;
840 font_info
*fi
= snew(font_info
);
844 fi
->widths
= newtree234(width_cmp
);
845 fi
->kerns
= newtree234(kern_cmp
);
846 fi
->ligs
= newtree234(lig_cmp
);
847 fi
->fontbbox
[0] = fi
->fontbbox
[1] = fi
->fontbbox
[2] = fi
->fontbbox
[3] = 0;
848 fi
->capheight
= fi
->xheight
= fi
->ascent
= fi
->descent
= 0;
849 fi
->stemh
= fi
->stemv
= fi
->italicangle
= 0;
851 fi
->filetype
= TRUETYPE
;
854 sf
->data
= snewn(sf
->len
, unsigned char);
856 got
= fread((char *)sf
->data
+ off
, 1, sf
->len
- off
, fp
);
858 if (off
!= sf
->len
) break;
860 sf
->data
= sresize(sf
->data
, sf
->len
, unsigned char);
864 sf
->data
= sresize(sf
->data
, sf
->len
, unsigned char);
865 sf
->end
= (char *)sf
->data
+ sf
->len
;
869 ptr
= decode(offsubdir_decode
, sf
->data
, sf
->end
, &sf
->osd
);
871 error(err_sfntbadhdr
, &sf
->pos
);
874 sf
->td
= snewn(sf
->osd
.numTables
, tabledir
);
875 ptr
= decoden(tabledir_decode
, ptr
, sf
->end
, sf
->td
, sizeof(*sf
->td
),
878 error(err_sfntbadhdr
, &sf
->pos
);
881 if (!sfnt_findtable(sf
, TAG_head
, &ptr
, &end
)) {
882 error(err_sfntnotable
, &sf
->pos
, "head");
885 if (decode(t_head_decode
, ptr
, end
, &sf
->head
) == NULL
) {
886 error(err_sfntbadtable
, &sf
->pos
, "head");
889 if ((sf
->head
.version
& 0xffff0000) != 0x00010000) {
890 error(err_sfnttablevers
, &sf
->pos
, "head");
893 fi
->name
= sfnt_psname(fi
);
894 if (fi
->name
== NULL
) return;
899 fi
->next
= all_fonts
;
903 static int sizecmp(const void *a
, const void *b
) {
904 if (*(size_t *)a
< *(size_t *)b
) return -1;
905 if (*(size_t *)a
> *(size_t *)b
) return 1;
910 * The format for embedding TrueType fonts in Postscript is defined in
911 * Adobe Technical Note #5012: The Type 42 Font Format Specification.
912 * <http://partners.adobe.com/public/developer/en/font/5012.Type42_Spec.pdf>
915 void sfnt_writeps(font_info
const *fi
, FILE *ofp
) {
916 unsigned i
, j
, lastbreak
;
917 sfnt
*sf
= fi
->fontfile
;
918 size_t *breaks
, glyfoff
, glyflen
;
919 void *glyfptr
, *glyfend
, *locaptr
, *locaend
;
923 /* XXX Unclear that this is the correct format. */
924 fprintf(ofp
, "%%!PS-TrueTypeFont-%u-%u\n", sf
->osd
.scaler_type
,
925 sf
->head
.fontRevision
);
927 fprintf(ofp
, "%%%%VMUsage: %u %u\n", sf
->minmem
, sf
->maxmem
);
928 fprintf(ofp
, "9 dict dup begin\n");
929 fprintf(ofp
, "/FontType 42 def\n");
930 fprintf(ofp
, "/FontMatrix [1 0 0 1 0 0] def\n");
931 fprintf(ofp
, "/FontName /%s def\n", fi
->name
);
932 fprintf(ofp
, "/Encoding StandardEncoding def\n");
933 if ((sf
->head
.flags
& 0x0003) == 0x0003) {
935 * Sensible font with the origin in the right place, such that
936 * the bounding box is meaningful.
938 fprintf(ofp
, "/FontBBox [%g %g %g %g] readonly def\n",
939 (double)sf
->head
.xMin
/ sf
->head
.unitsPerEm
,
940 (double)sf
->head
.yMin
/ sf
->head
.unitsPerEm
,
941 (double)sf
->head
.xMax
/ sf
->head
.unitsPerEm
,
942 (double)sf
->head
.yMax
/ sf
->head
.unitsPerEm
);
944 /* Non-sensible font. */
945 fprintf(ofp
, "/FontBBox [0 0 0 0] readonly def\n");
947 fprintf(ofp
, "/PaintType 0 def\n");
948 fprintf(ofp
, "/CharStrings %u dict dup begin\n", sf
->nglyphs
);
949 fprintf(ofp
, "0 1 %u{currentfile token pop exch def}bind for\n",
951 for (i
= 0; i
< sf
->nglyphs
; i
++)
952 ps_token(ofp
, &cc
, "/%s", glyph_extern(sfnt_indextoglyph(sf
, i
)));
953 fprintf(ofp
, "\nend readonly def\n");
954 fprintf(ofp
, "/sfnts [<");
955 breaks
= snewn(sf
->osd
.numTables
+ sf
->nglyphs
, size_t);
956 for (i
= 0; i
< sf
->osd
.numTables
; i
++) {
957 breaks
[i
] = sf
->td
[i
].offset
;
959 if (!sfnt_findtable(sf
, TAG_glyf
, &glyfptr
, &glyfend
)) {
960 error(err_sfnt_notable
, &sf
->pos
, "glyf");
963 glyfoff
= (char *)glyfptr
- (char *)sf
->data
;
964 glyflen
= (char *)glyfend
- (char *)glyfptr
;
965 if (!sfnt_findtable(sf
, TAG_loca
, &locaptr
, &locaend
)) {
966 error(err_sfnt_notable
, &sf
->pos
, "loca");
969 loca
= snewn(sf
->nglyphs
, unsigned);
970 if (sf
->head
.indexToLocFormat
== 0) {
971 if (!decoden(uint16_decode
, locaptr
, locaend
, loca
, sizeof(*loca
),
972 sf
->nglyphs
)) goto badloca
;
973 for (i
= 0; i
< sf
->nglyphs
; i
++) loca
[i
] *= 2;
975 if (!decoden(uint32_decode
, locaptr
, locaend
, loca
, sizeof(*loca
),
976 sf
->nglyphs
)) goto badloca
;
978 for (i
= 1; i
< sf
->nglyphs
; i
++) {
979 if (loca
[i
] > glyflen
) goto badloca
;
980 breaks
[sf
->osd
.numTables
+ i
- 1] = loca
[i
] + glyfoff
;
982 breaks
[sf
->osd
.numTables
+ sf
->nglyphs
- 1] = sf
->len
;
983 qsort(breaks
, sf
->osd
.numTables
+ sf
->nglyphs
, sizeof(*breaks
), sizecmp
);
985 for (i
= 0; i
< sf
->len
; i
++) {
986 if ((i
- lastbreak
) % 38 == 0) fprintf(ofp
, "\n");
987 if (i
== breaks
[j
]) {
988 while (i
== breaks
[j
]) j
++;
990 fprintf(ofp
, "00><\n");
992 fprintf(ofp
, "%02x", *((unsigned char *)sf
->data
+ i
));
994 fprintf(ofp
, "00>] readonly def\n");
996 fprintf(ofp
, "end /%s exch definefont\n", fi
->name
);
999 error(err_sfntbadtable
, &sf
->pos
, "loca");
1002 void sfnt_data(font_info
*fi
, char **bufp
, size_t *lenp
) {
1003 sfnt
*sf
= fi
->fontfile
;