b2dLoadTTF

Category: 2D Overlay

Syntax:

b2dLoadTTF:Int(path:String, pixelHeight:Int)


Parameters

Returns

Int



Summary

Loads a TrueType/OpenType font (.ttf/.otf) at a given pixel height.

Takes path (path to the font file) and pixelHeight (the size to render glyphs at, in pixels).

Returns a font handle (positive integer for success, 0 for failure).

The handle is an ordinary font handle - the same kind b2dLoadFont returns - so b2dDrawText, b2dDrawTextEx, b2dGetStringWidth, b2dGetStringHeight, b2dGetFontWidth, b2dGetFontHeight and b2dFreeFont all take it unchanged. Unlike a bitmap font, glyphs are rasterised on demand into a dynamic texture atlas keyed by Unicode codepoint, so a program only pays for the characters it actually shows - which is what makes CJK practical.

Strings are decoded as UTF-8, so a multi-byte character is one glyph with its own advance rather than several. UTF-8 literals in a .bam source file work directly.

Each pixelHeight is its own handle with its own atlas; nothing is ever scaled from a shared one, so text stays crisp at every size. Load the same file twice for small body text and a large title.

b2dGetFontWidth returns the advance of 'M' and b2dGetFontHeight returns the line height. Note that a glyph whose ink rises above the font's declared ascent paints slightly above the line's top edge - that is the font's design, not a layout error, and it matches the Windows build.

Returns 0 if the file is missing or is not a valid font; that is safe to carry on with, since the draw functions ignore a 0 handle.

View detailed documentation online



Example
Local title:Int = b2dLoadTTF("Media/stencilla.ttf", 48)
Local body:Int  = b2dLoadTTF("Media/stencilla.ttf", 22)

b2dSetColor(255, 255, 255)
b2dDrawText("Hello, World!", 20, 20, 0, body)

; A TTF handle measures like any other font, so a centred line can be
; boxed from its own metrics.
Local msg:String = "Centred & measured"
Local w:Int = b2dGetStringWidth(msg, title)
b2dDrawText(msg, 400, 130, 1, title)

b2dFreeFont(title)
b2dFreeFont(body)

BambooBasic © 2026 Michael Denathorn