Learning path

Full curriculum

Full curriculum

Unit content

TrueType hinting bytecode and its interpreter

TrueType hinting is unusual because a font file can contain instructions that are executed by a font interpreter while glyphs are prepared for rasterization.

The font is therefore partly data and partly a program for a small virtual machine.

Instruction streams

TrueType instructions are encoded as opcodes with operands. The instruction set includes operations for

  • pushing values onto an interpreter stack;
  • reading and writing storage;
  • accessing a control-value table;
  • changing graphics-state settings;
  • moving and measuring outline points;
  • defining and calling functions;
  • conditional control flow.

A stack machine

Many operations consume values from and place results onto an evaluation stack.

Conceptually:

push 21
push 4
operation

has the same broad execution pattern as other stack-oriented virtual machines: instructions manipulate an implicit last-in, first-out operand stack rather than naming a large set of general-purpose registers.

Different programs in one font

Instructions can occur at several levels.

A font program defines functions and instructions available across the font. A control-value program runs when relevant rendering conditions such as size or transformation change. Individual glyphs can also carry their own instruction sequences.

This lets common rules be shared while still allowing glyph-specific adjustments.

Graphics state

The interpreter maintains state controlling how point-moving instructions are interpreted: projection and freedom vectors, rounding modes, reference points and related settings.

Instructions can therefore express rules such as aligning important features to the pixel grid or preserving relationships between distances at particular sizes.

Sandboxed execution

These instructions are not native CPU machine code and are not arbitrary application code. A rasterizer executes them inside the semantics and limits of the TrueType virtual machine.

That distinction is important when saying a font file is “a program”: it contains executable bytecode for a specialized interpreter, not a general native executable.

Why put a program in a font?

A purely declarative outline describes ideal scalable geometry, but low-resolution rasterization introduces device-dependent rounding problems. A small program gives the font designer procedural control over how that geometry should be adjusted under different rendering conditions.

This is a recurring systems idea: a file format can become more expressive by embedding instructions interpreted by the consumer.