SWF font parsing: DefineFont3 to TrueType
Implemented in CreationEngineFormats/Swf/, without Skyrim-specific knowledge. References:
- SWF File Format Specification, version 19
(Adobe, “SWF19”): container,
RECT,SHAPE,DefineFont3,DefineFontName. - OpenType specification (Microsoft):
the sfnt container and the
cmap,glyf,head,hhea,hmtx,loca,maxp,name,OS/2andposttables.
The exporter side (which SWFs, which fonts) is in Font export.
SWF container
SwfReader: a 3-byte signature, a version byte and a uint32 file length, then the body.
| Signature | Body | Support |
|---|---|---|
FWS | Uncompressed | Read |
CWS | zlib | Read; the only kind in the interface archive |
ZWS | LZMA | Throws NotSupportedException |
The body starts with a bit-packed RECT, a frame rate and a frame count, all skipped, then the
tags. A tag header is a uint16: code in the top 10 bits, length in the low 6; a length of 0x3F
means a uint32 length follows. SwfBitReader reads the MSB-first bit fields.
DefineFont3
DefineFont3Reader (tag 75), the only font tag found in Skyrim SWFs. DefineFontName (tag 88)
supplies each font’s name.
| Field | Notes |
|---|---|
| Font id | uint16 |
| Flags | Has layout 0x80, wide offsets 0x08, wide codes 0x04, italic 0x02, bold 0x01 |
| Language code, name | Skipped: DefineFontName carries the name |
| Glyph count | uint16 |
| Offset table | uint16 or uint32 per glyph, then the code table offset |
| Glyph shapes | SHAPE records: move, straight and quadratic curve edges |
| Code table | uint8 or uint16 per glyph |
| Layout | Only with the layout flag: ascent, descent, leading, advances, bounds, kerning (not read) |
Quadratic curves are the same curve type as TrueType glyf, so no curve fitting is needed.
Three properties aren’t stated by SWF19 and were checked on real files, see
Spec deviations: the EM square is 20480
units, Y points down, and the per-glyph bounds are always zero.
TrueType writer
SfntBuilder writes the table directory, padding and checksums, including
head.checkSumAdjustment. TrueTypeFontWriter builds the tables:
| Choice | Why |
|---|---|
| 2048 units per EM | 20480 exceeds the head limit of 16384; dividing by 10 is exact |
Synthesized .notdef at glyph 0 | Required by TrueType; SWF glyphs follow in order |
glyf bounds from the contour points | The SWF bounds are unusable |
cmap format 4, format 12 when format 4’s uint16 length overflows | Firefox rejects format 6; format 12 covers the 17,514-glyph Chinese font |
Format 4 through idRangeOffset and glyphIdArray | Glyph order follows the SWF, not character codes |
OS/2 version 0 | Smallest version with usWinAscent and usWinDescent, which set line height |
post version 3.0 | No glyph names needed |
One hmtx entry per glyph | Simpler; the fonts are small |
Generated fonts pass fc-validate and fc-scan (FreeType). FreeType accepts cmap formats
browsers don’t: format 6 passed FreeType but Firefox refused it. Any change to the table formats
needs a check through @font-face in a browser. Glyph winding hasn’t been checked visually.