natiny.font.get_glyph

Lua
natiny.font.get_glyph(font, codepoint)
    -> advance, x, y, w, h, u0, v0, u1, v1, atlas

Gets one glyph's metrics and its rectangle in the atlas, for drawing it by hand.

Parameters

Name Type Description
font number Font handle from natiny.font.create
codepoint number or string Unicode code point, or a UTF-8 string whose first character is used

Returns

Type Description
number advance: pen advance in drawing units
number x: glyph left edge relative to the pen, in drawing units
number y: glyph top edge relative to the baseline, in drawing units
number w: glyph width in drawing units
number h: glyph height in drawing units
number u0: normalized atlas left edge
number v0: normalized atlas top edge
number u1: normalized atlas right edge
number v1: normalized atlas bottom edge
number atlas: texture handle containing the glyph, including a fallback font's atlas

Returns only nil if the font is invalid or the glyph cannot be resolved or packed into the atlas.

Example

Lua
local advance, x, y, w, h, u0, v0, u1, v1, atlas =
    natiny.font.get_glyph(font, 0x41)

if advance then
    local baseline = line_y + natiny.font.get_ascent(font)
    local left = pen + x
    local top = baseline + y
    -- u0, v0, u1, and v1 select the region in atlas.
    pen = pen + advance + natiny.font.get_letter_spacing(font)
end

Notes

  • The call bakes an uncached glyph. Atlas coordinates remain valid until the atlas is re-baked at another display scale.