Skip to content

Font ​

Fonts: bundles, TTF baking, pixel fonts and measuring.

ts
import { Font, FontTtfConfig, FontBakeOptions, FontMeasureStyle, FontMeasureResult } from "willow";

Font ​

ts
class Font

Font handle backed by a willow::FontFamily. Constructed via the static factories (Font.load, Font.system, Font.fromBuffer). Direct new Font(...) raises a TypeError.

Font: properties ​

MemberDescription
readonly name: stringFamily name reported by the bundle.

Font: methods ​

MemberDescription
dispose(): voidReleases this font handle. Destroying the handle returned by Font.system() does NOT free willow's bundled default family. Only this wrapper retires. Idempotent and silent, like every other dispose(). The one method this class carries, renderCount(), raises "InvalidStateError: Font.renderCount: font has been disposed" afterwards. The name field also raises InvalidStateError once disposed.
renderCount(): floatReturns the cumulative number of glyph renders this font has performed. float, not int: the underlying counter never resets and would silently wrap an int32 after roughly 2.1 billion glyphs. Counts glyphs actually emitted by the render pipeline (once per frame a Text node using this font is on-screen and not served from a CacheAsTree replay) — not distinct glyphs, and not frames where the node was cached.
measure(text: string, size: float, style?: FontMeasureStyle): FontMeasureResultMeasures text directly without creating a scene node.
lineHeight(size: float, style?: FontMeasureStyle): floatReturns willow's native line height for a display size and face style.

Font: static methods ​

MemberDescription
static load(path: string): FontLoads a font bundle from path (resolved via the engine asset roots).
static fromBuffer(buffer: Buffer): FontLoads a font bundle from an in-memory byte buffer.
static fromPixelFont(texture: Texture | string, cellWidth: int, cellHeight: int, chars: string): FontWraps a pixel font spritesheet into a Font. cellWidth and cellHeight specify the fixed cell size in the texture. chars maps cells in row-major order to characters.
static fromTTF(buffer: Buffer, config?: FontTtfConfig): FontBakes a TTF/OTF buffer at load time. No baking occurs per frame.
static loadTTF(path: string, config?: FontTtfConfig): FontLoads and bakes a project-root-confined TTF/OTF file at load time.
static loadTTFCached(ttf: string | Buffer, cacheDir: string, options?: FontBakeOptions): FontSynchronously loads a TTF/OTF (a project-root-confined path or a Buffer) through an on-disk bake cache. The first call bakes and writes <cacheDir>/<key>.fontbundle (atomically); later calls with the same font bytes and options load that file without baking. An empty or unwritable cacheDir bakes in memory.
static cachedBakeCount(): floatNumber of bakes Font.loadTTFCached has performed in this process; cache hits do not count.
static system(name?: string): FontReturns a handle to willow's bundled default font. The optional name argument is accepted for signature compatibility and ignored.

FontTtfConfig ​

ts
class FontTtfConfig

Runtime TTF/OTF baking options. Baking happens once while loading.

FontTtfConfig: properties ​

MemberDescription
sizes?: Array<float>Point sizes to bake ahead of time. Unset uses the font family's default size set.
sdfSpread?: floatSDF spread in pixels for the baked distance field. Unset uses the baker's default.
atlasWidth?: intBaked atlas page width in pixels. Unset uses the baker's default.
atlasHeight?: intBaked atlas page height in pixels. Unset uses the baker's default.
chars?: stringCharacter set to bake glyphs for. Unset bakes the baker's default set.

FontBakeOptions ​

ts
class FontBakeOptions

Options for Font.loadTTFCached; they map one to one onto the font baker's options and are part of the cache key.

FontBakeOptions: properties ​

MemberDescription
sizes?: Array<float>Em sizes in pixels to bake. Unset bakes 64 and 256.
distanceRange?: floatSDF distance range in pixels for every size. Unset or 0 uses size / 8.
atlasWidth?: intAtlas page width in pixels. Unset bakes 1024.
charset?: stringCharacters to bake: ascii, latin, full, U+hhhh-hhhh ranges joined by commas, or literal characters. Unset bakes ASCII.
effectSize?: floatEm size of the extra effects entry for outline, glow and shadow. Unset or 0 bakes none.
effectRange?: floatDistance range of the effects entry. Unset or 0 uses effectSize / 2.

FontMeasureStyle ​

ts
class FontMeasureStyle

Optional face selection for direct font measurement.

FontMeasureStyle: properties ​

MemberDescription
bold?: booleanMeasure using the bold face, when available.
italic?: booleanMeasure using the italic face, when available.

FontMeasureResult ​

ts
class FontMeasureResult

Dimensions returned by direct font measurement.

FontMeasureResult: properties ​

MemberDescription
readonly width: floatMeasured width in pixels.
readonly height: floatMeasured height in pixels.