draw_text
Mojo function š”
draw_text
fn def draw_text(mut canvas: Canvas, x: Int, y: Int, text: String, color: Color, size: Float64, family: String = "Sans", slant: FontSlant = FontSlant.NORMAL, weight: FontWeight = FontWeight.NORMAL, rotation: Float64 = 0, align: TextAlign = TextAlign.LEFT, kerning: Bool = True, ligatures: Bool = True)Render text (one or more “\n”-separated lines) anchored at (x, y) in family at size points, compositing onto canvas in color. AA coverage combines with color.a through the same set_pixel() blend every other filled shape uses.
rotation (radians) rotates the whole block – every line together
– around the (x, y) anchor. Transform2D’s rotation
(geometry.mojo) instead tilts a whole data-to-pixel mapping.
Builds a FontCache per call and throws it away, so every call
rescans every font file installed on the machine. That scan is tens
of milliseconds where a cached label is tens of microseconds, and it
does not shrink with the length of the string – a chart labeling
its axes through this overload spends nearly all of its time in font
discovery. Construct one FontCache and pass it to the cache=
overload below for anything past a couple of strings.
Args:
- canvas (
Canvas): Canvas to draw into. - x (
Int): Anchor x – baseline left end for LEFT alignment. - y (
Int): Anchor y – baseline. - text (
String): Text to draw, “\n”-separated lines. - color (
Color): Text color. - size (
Float64): Font size in points. - family (
String): Font family name or generic alias. - slant (
FontSlant): Requested upright/italic/oblique style. - weight (
FontWeight): Requested normal/bold weight. - rotation (
Float64): Radians, rotating the whole block around the anchor. - align (
TextAlign): Horizontal alignment of each line. - kerning (
Bool): Apply the font’s pair kerning between adjacent glyphs. - ligatures (
Bool): Apply the font’sGSUBshaping – ligatures and Arabic contextual forms. False lays out one glyph per character.
Raises:
fn def draw_text(mut canvas: Canvas, x: Int, y: Int, text: String, color: Color, size: Float64, family: String = "Sans", slant: FontSlant = FontSlant.NORMAL, weight: FontWeight = FontWeight.NORMAL, rotation: Float64 = 0, align: TextAlign = TextAlign.LEFT, kerning: Bool = True, ligatures: Bool = True, *, mut cache: FontCache)Like draw_text above, but resolving fonts through cache rather than fresh every call. Whole-pixel anchor; delegates to the sub-pixel overload below.
Args:
- canvas (
Canvas): Canvas to draw into. - x (
Int): Anchor x – baseline left end for LEFT alignment. - y (
Int): Anchor y – baseline. - text (
String): Text to draw, “\n”-separated lines. - color (
Color): Text color. - size (
Float64): Font size in points. - family (
String): Font family name or generic alias. - slant (
FontSlant): Requested upright/italic/oblique style. - weight (
FontWeight): Requested normal/bold weight. - rotation (
Float64): Radians, rotating the whole block around the anchor. - align (
TextAlign): Horizontal alignment of each line. - kerning (
Bool): Apply the font’s pair kerning between adjacent glyphs. - ligatures (
Bool): Apply the font’sGSUBshaping – ligatures and Arabic contextual forms. False lays out one glyph per character. - cache (
FontCache): Shared cache for font resolution and parsed faces.
Raises:
fn def draw_text(mut canvas: Canvas, x: Float64, y: Float64, text: String, color: Color, size: Float64, family: String = "Sans", slant: FontSlant = FontSlant.NORMAL, weight: FontWeight = FontWeight.NORMAL, rotation: Float64 = 0, align: TextAlign = TextAlign.LEFT, kerning: Bool = True, ligatures: Bool = True)Draw text at a sub-pixel anchor, resolving fonts for this call.
Args:
- canvas (
Canvas): Canvas to draw into. - x (
Float64): Anchor x, sub-pixel – baseline left end for LEFT alignment. - y (
Float64): Anchor y, sub-pixel – baseline. - text (
String): Text to draw, “\n”-separated lines. - color (
Color): Text color. - size (
Float64): Font size in points. - family (
String): Font family name or generic alias. - slant (
FontSlant): Requested upright/italic/oblique style. - weight (
FontWeight): Requested normal/bold weight. - rotation (
Float64): Radians, rotating the whole block around the anchor. - align (
TextAlign): Horizontal alignment of each line. - kerning (
Bool): Apply the font’s pair kerning between adjacent glyphs. - ligatures (
Bool): Apply the font’sGSUBshaping – ligatures and Arabic contextual forms. False lays out one glyph per character.
Raises:
fn def draw_text(mut canvas: Canvas, x: Float64, y: Float64, text: String, color: Color, size: Float64, family: String = "Sans", slant: FontSlant = FontSlant.NORMAL, weight: FontWeight = FontWeight.NORMAL, rotation: Float64 = 0, align: TextAlign = TextAlign.LEFT, kerning: Bool = True, ligatures: Bool = True, *, mut cache: FontCache)Draw text at a sub-pixel anchor using cache for font resolution.
A sub-pixel anchor places a label against something itself at a fractional position – a tick at x = 103.7, a label centered on a bar whose midpoint is not a whole pixel. Rounding the anchor first shifts the whole string.
Args:
- canvas (
Canvas): Canvas to draw into. - x (
Float64): Anchor x – baseline left end for LEFT alignment. - y (
Float64): Anchor y – baseline. - text (
String): Text to draw, “\n”-separated lines. - color (
Color): Text color. - size (
Float64): Font size in points. - family (
String): Font family name or generic alias. - slant (
FontSlant): Requested upright/italic/oblique style. - weight (
FontWeight): Requested normal/bold weight. - rotation (
Float64): Radians, rotating the whole block around the anchor. - align (
TextAlign): Horizontal alignment of each line. - kerning (
Bool): Apply the font’s pair kerning between adjacent glyphs. - ligatures (
Bool): Apply the font’sGSUBshaping – ligatures and Arabic contextual forms. False lays out one glyph per character. - cache (
FontCache): Shared cache for font resolution and parsed faces.
Raises: