Apple’s TrueType fonts can carry bitmaps of their glyphs in two tables:
bloc (bitmap location) says where each bitmap is and bdat
(bitmap data) holds them. A font that has bitmaps and no outlines also replaces its
head table with bhed. The layout is close to that of the
OpenType EBLC and EBDT
tables. This page follows Apple’s TrueType Reference Manual; all numbers are
big-endian.
bloc: bitmap location
bloc and bdat only work together. The table starts with a
version (fixed, 0x00020000) and
numSizes (uint32), then one bitmapSizeTable of
48 bytes for each strike. A strike is the set of bitmaps made for one size. The
strikes must be sorted in ascending order of size.
| Type | Field | Meaning |
|---|---|---|
uint32 | indexSubTableArrayOffset | From the start of bloc to the strike’s index subtable array. |
uint32 | indexTableSize | Bytes of the array and its index subtables. |
uint32 | numberOfIndexSubTables | Records in the array. |
uint32 | colorRef | Set to 0. See the depths below. |
sbitLineMetrics | hori | Line metrics for horizontal text (12 bytes). |
sbitLineMetrics | vert | Line metrics for vertical text (12 bytes). |
uint16 | startGlyphIndex | Lowest glyph of the strike. |
uint16 | endGlyphIndex | Highest glyph of the strike. |
uint8 | ppemX | Horizontal pixels per em. |
uint8 | ppemY | Vertical pixels per em. |
uint8 | bitDepth | Bits per pixel. |
uint8 | flags | 0x01 the small metrics are horizontal, 0x02 they are vertical. The scaler makes up the other direction. |
Depth and colorRef
bitDepth1 withcolorRef0: one bit for each pixel. AAT fonts should use only this.bitDepth2, 4, 8, 16 or 24 withcolorRef0: black and white glyphs already expanded for drawing at that screen depth.- A
colorRefother than 0: glyphs of more than one bit whose meaning comes from that value, such as colour or grey levels. The font must then have acolrtable, which is used with it.
Line metrics
An sbitLineMetrics record is ascender and
descender (int8), widthMax
(uint8), caretSlopeNumerator (the rise, usually 1 for an
upright font), caretSlopeDenominator (the run, usually 0),
caretOffset, minOriginSB, minAdvanceSB,
maxBeforeBL, minAfterBL (all int8) and two
bytes of padding.
| Field | Horizontal text | Vertical text |
|---|---|---|
minOriginSB | Smallest horiBearingX | Smallest vertBearingY |
minAdvanceSB | Smallest horiAdvance − horiBearingX + width | Smallest vertAdvance − vertBearingY + height |
maxBeforeBL | Largest horiBearingY | Largest vertBearingY |
minAfterBL | Smallest horiBearingY − height | Smallest vertBearingX − width |
The table gives these as the manual does. macOS does not use the last four fields.
Index subtables
Each strike has an array of records: firstGlyphIndex and
lastGlyphIndex (uint16) and
additionalOffsetToIndexSubtable (uint32), which is added to
indexSubTableArrayOffset. The first array follows the last
bitmapSizeTable; the strike’s index subtables follow it, then the next
strike’s array. Each index subtable starts on a four-byte boundary, so a format 3
offset list sometimes needs an extra uint16 of padding.
A subtable starts with indexFormat, imageFormat
(uint16) and imageDataOffset (uint32), the place
in bdat that its glyphs are counted from.
| indexFormat | Contents after the header |
|---|---|
| 1 | Glyphs of differing sizes: a uint32 offset for each glyph of the range, and one more to end the last. The size of a bitmap is the difference of two neighbouring offsets; a difference of zero means no bitmap. |
| 2 | Glyphs of one size: imageSize (uint32) and a bigGlyphMetrics record shared by all. Glyph g is at imageDataOffset + imageSize × (g − firstGlyphIndex). |
| 3 | As format 1 with uint16 offsets. |
Formats 4 and 5 of the OpenType EBLC table, for scattered glyph numbers,
are not in Apple’s chapter. The viewer reads them as OpenType describes them and says
so.
Glyph metrics
A bigGlyphMetrics record has eight bytes: height,
width (uint8), horiBearingX,
horiBearingY (int8), horiAdvance
(uint8), vertBearingX, vertBearingY
(int8) and vertAdvance (uint8). Vertical text is
taken to run from top to bottom.
A smallGlyphMetrics record has five: height,
width, bearingX, bearingY and
advance. It serves one direction only, which the strike’s
flags give. For vertical text bearingX runs from the vertical
origin to the top edge and bearingY from the vertical origin to the left
edge.
bdat: bitmap data
bdat starts with a version (fixed,
0x00020000). The rest is glyph bitmaps, found through bloc.
The index subtable gives the image format of each.
| Format | Metrics | Image |
|---|---|---|
| 1 | Small, before the image | Each row starts on a byte boundary. |
| 2 | Small, before the image | Pixels follow one another without row padding; the whole image is padded to a byte. |
| 3 | Not used. | |
| 4 | Compressed, see below. | |
| 5 | In the index subtable (format 2) | Packed bits, padded to a byte at the end. The box need not be tight: there can be blank rows or columns. |
| 6 | Big, before the image | Each row starts on a byte boundary. |
| 7 | Big, before the image | Packed bits, padded to a byte at the end. |
Format 4
Apple calls format 4 a modified Huffman coding: a black-and-white image written as
run lengths, each coded with a binary tree. It goes with an index subtable of format 2,
whose bigMetrics give the width and height of every glyph. The block at the
subtable’s imageDataOffset is laid out as follows. All offsets count from
the start of the block.
| Field | Type | Meaning |
|---|---|---|
whiteTreeOffset | uint32 | Start of the tree of white run lengths. It ends where the black tree starts. |
blackTreeOffset | uint32 | Start of the tree of black run lengths. It ends at the first glyph’s data. |
glyphDataOffset[n + 1] | uint32 | One offset for each glyph of the subtable’s range and one more. A glyph’s codes run from its offset to the next one. |
| The two trees and the coded glyphs. |
A tree is made of 16-bit nodes, and its first node is the root. A node with the top bit set is a leaf, and its other 15 bits are a run length. Any other node is a signed offset: for a 0 bit the next node is 2 bytes further on; for a 1 bit it is the offset plus 2 bytes further on.
A glyph’s bits are read from the most significant bit of each byte. The image starts with a white run. Starting at the root of the tree of the current colour, one branch is taken for each bit until a leaf is reached. The leaf gives the number of pixels of that colour. A run shorter than 64 is followed by a run of the other colour; a run of 64 or more is followed by another run of the same colour. The pixels fill the box row after row with no padding between rows, so a run can continue on the next row, and the image is complete after width × height pixels.
bhed: bitmap font header
bhed is byte for byte the same as the
head table. Its
presence tells macOS that the font has bitmaps only. A font without outlines must have
it, and one with outlines must not. A font with bhed must have
bdat and bloc, and no head or
glyf.
In the viewer
The viewer shows the strikes, index subtables and every located bitmap with its
metrics, draws a strike as a sheet of glyphs, and reads bhed as a header
table. Departures from Apple’s rules are listed as notes or problems.
Divergence from spec
Offset lists in Apple’s own fonts
The manual makes the size of a bitmap the difference of two offsets that follow each other. Bitmap fonts shipped with macOS, such as Geneva, have offset lists that end with 0 or start again from a lower value. The viewer notes the first offset that is lower than the one before it.
Glyphs without bitmaps
The chapter says that every strike has bitmaps for as many glyphs as the
maxp table counts, and also describes equal offsets as the way to give a
glyph no bitmap. Strikes that cover only part of a font are noted by the viewer, not
rejected.
Image formats 3 and 4
The bdat chapter gives only the three offsets of format 4, followed by
“trees and glyph data”. The array of glyph offsets, the nodes of the trees and the
coding of the runs described above are what the font library of macOS does. The
OpenType specification does not support either format. The same library draws no glyph
for format 3.
The magic number of bhed
As a copy of head, bhed should have the magic number
0x5F0F3CF5. A font of the Chinese standard GB 18030 shipped with macOS,
NISC18030, has another value. The viewer notes it.
Sources
Apple, TrueType Reference
Manual, chapters bloc,
bdat and
bhed.
OpenType specification, EBDT.
The code of macOS’s font library, libFontParser.dylib (functions
sbit_IsThereAGlyphImage and sbit_GetBitMapData), for image
formats 3 and 4. The fonts shipped with macOS, for the other divergences above.