natiny.font.get_glyph

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

Returns 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

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

Example

Lua
local pen = 20

for _, code in utf8.codes(message) do
    local advance = natiny.font.get_glyph(font, code)
    if not advance then break end
    local bob = math.sin(time * 4 + pen * 0.05) * 6
    natiny.render.text(font, pen, 100 + bob, utf8.char(code))
    pen = pen + advance + natiny.font.get_letter_spacing(font)
end

Notes

  • y is normally negative because the glyph image sits above the baseline. natiny.font.get_ascent converts a line-top coordinate to a baseline.
  • advance includes neither kerning nor the font's letter spacing. Add the current letter spacing explicitly; text placed this way remains unkerned.
  • The call bakes an uncached glyph. Atlas coordinates remain valid until the atlas is re-baked at another display scale.
  • Follows the fallback chain, and atlas says which font's atlas the glyph actually came from.
  • A missing code point returns the primary font's missing-glyph symbol rather than nil.