font
font
¶
Text rendering via SDL_ttf 3.x.
A TrueType string is rasterized once to a white texture with TTF_RenderText_Blended and kept in a per-font cache, reused across frames with the draw color applied as a tint, so the same string is not uploaded again every frame. Drawing is one quad. UTF-8 in, no rune-pointer juggling.
newFont ¶
proc newFont(filename: string; size: cint): Font
Open a TrueType font at the given pixel size. Raises IOError when the
file cannot be opened.
Parameters
-
filename(string) -
size(cint)
Returns
Font
newImageFont ¶
proc newImageFont(nim2d: Nim2d; data: ImageData; glyphs: string; spacing: int32 = 1): Font
A bitmap font from a glyph sheet. The glyphs sit in a row separated by
columns of the sheet's top-left pixel color, and glyphs lists the
characters in that order. It is sampled crisply (nearest) and tinted by the
current color, like the TTF path. Good for pixel-art text.
Parameters
-
nim2d(Nim2d) -
data(ImageData) -
glyphs(string) -
spacing(int32)
Returns
Font
newImageFont ¶
proc newImageFont(nim2d: Nim2d; filename, glyphs: string; spacing: int32 = 1): Font
A bitmap font loaded from an image file. See the other overload.
Parameters
-
nim2d(Nim2d) -
filename(string) -
glyphs(string) -
spacing(int32)
Returns
Font
destroy ¶
proc destroy(nim2d: Nim2d; font: Font)
Free a font's handles right away rather than waiting for it to be collected.
The font is unusable afterwards. Use it when you load and drop many fonts, say on a level change; otherwise a font frees itself when it goes out of use.
Parameters
-
nim2d(Nim2d) -
font(Font)
getAscent ¶
proc getAscent(font: Font): int
How far the font reaches above the baseline, in pixels. A bitmap font
reports its glyph height, since it draws from the top.
Parameters
-
font(Font)
Returns
int
getDescent ¶
proc getDescent(font: Font): int
How far the font reaches below the baseline, as a negative pixel count.
A bitmap font reports 0.
Parameters
-
font(Font)
Returns
int
getHeight ¶
proc getHeight(font: Font): int
getSize ¶
proc getSize(font: Font; text: string): tuple[w, h: int32]
The width and height in pixels that text would take when printed.
Parameters
-
font(Font) -
text(string)
Returns
tuple[w, h: int32]
print ¶
proc print(nim2d: Nim2d; text: string; x, y: float; angle: float = 0; sx: float = 1; sy: float = 1)
Draw text in the current color using the current font. Call inside draw.
Parameters
-
nim2d(Nim2d) -
text(string) -
x(float) -
y(float) -
angle(float) -
sx(float) -
sy(float)