FONT (File Format)

The FONT format defines fonts that can be used to display text in-game.

Format
Note: All measurements are in points.

Header
The header has a couple minor differences depending on the version. Version 1 (MP1 Kiosk Demo) is missing the value at 0x18, and starting in Version 5 (MP3/DKCR), the texture ID is 64 bits. Those are the only changes, though (and for that matter, those are the only differences in version 1/5).

Glyphs
This section of the file details each glyph making up the font. One glyph corresponds to one character. To map the character with the part of the font texture that contains that character, each glyph contains a set of texture coordinates. Since the UV map is always rectangular, the file only stores the min/max X/Y coordinates; they should be mixed and matched to get the absolute values.

In addition, glyphs contain a vertical offset. This offset is necessary because the textures are usually so tightly packed that there's no extra space for vertical spacing. After applying the offset, the vertical offset from the FONT header is applied. This aligns the glyphs at their intended position.

The kerning index value is the index of the first kerning table entry for the given character. Since the table is sorted in alphabetical order, this can be used to speed up lookup time.

This section of the file changes somewhat from version 2 to 4. Starting in version 4, glyphs are able to be encoded to only one particular RGBA channel, which allows multiple glyphs to be encoded in the same space on the texture. A "channel index" value was introduced to track which channel the glyph is on. Additionally, a lot of 32-bit values were reduced to 8- or 16-bit. Aside from that, though, the actual layout is still the same, with the same values in the same places.

Kerning Table
This table is at the end of the file, and defines kerning pairs. Kerning pairs horizontally adjust the second character of the pair whenever that particular sequence of characters is encountered. The table is case-sensitively sorted in alphabetical order, which allows for fast lookup times using the glyph kerning indices. After a 32-bit count value, each pair follows this simple structure:

Textures
Textures used by FONT files are encoded in a bit of an unorthodox manner. The font format allows for multiple glyphs to be encoded in the same spot on the texture by splitting up the texture into multiple layers. The way this is handled on the textures is by encoding the textures with palettes (as C4), but modifying the palette indices at runtime and loading each layer one at a time as a separate texture. Each bit of the palette index corresponds to a layer, so up to four layers per font are supported. The MSB refers to layer 0, while the LSB refers to layer 3. In DKCR, font textures are encoded as C8, so theoretically the game could use up to eight layers per font; however, it still never actually uses more than four.

The texture format is determined by a value in the header. These are the possible settings:

To dump out every layer into a single texture by encoding each layer on a separate RGBA channel, the following palette colors could be substituted into the texture: