Skip to content
draw_text_on_path

draw_text_on_path

Mojo function 🡭

draw_text_on_path

fn def draw_text_on_path(mut canvas: Canvas, path: Path, text: String, color: Color, size: Float64, offset: Float64 = 0, family: String = "Sans", slant: FontSlant = FontSlant.NORMAL, weight: FontWeight = FontWeight.NORMAL, align: TextAlign = TextAlign.LEFT, kerning: Bool = True, ligatures: Bool = True)

Draw text along a curve, resolving fonts for this call.

Args:

  • canvas (Canvas): Canvas to draw into.
  • path (Path): Curve the baseline follows.
  • text (String): Text to draw, treated as a single line.
  • color (Color): Text color.
  • size (Float64): Font size in points.
  • offset (Float64): Distance along the path the string is placed at.
  • family (String): Font family name or generic alias.
  • slant (FontSlant): Requested upright/italic/oblique style.
  • weight (FontWeight): Requested normal/bold weight.
  • align (TextAlign): Where offset sits in the string – its start, middle or end.
  • kerning (Bool): Apply the font’s pair kerning between adjacent glyphs.
  • ligatures (Bool): Apply the font’s GSUB shaping – ligatures and Arabic contextual forms. False lays out one glyph per character.

Raises:

fn def draw_text_on_path(mut canvas: Canvas, path: Path, text: String, color: Color, size: Float64, offset: Float64 = 0, family: String = "Sans", slant: FontSlant = FontSlant.NORMAL, weight: FontWeight = FontWeight.NORMAL, align: TextAlign = TextAlign.LEFT, kerning: Bool = True, ligatures: Bool = True, *, mut cache: FontCache)

Draw text with its baseline running along path: a label around a donut segment, a curved axis label on a polar grid.

path replaces draw_text’s (x, y) anchor and its rotation both: offset is a distance measured along the path rather than a point, and each glyph turns to the path’s tangent where it sits, so a string on a circle leans round it. align says where offset falls in the string – LEFT starts it there, CENTER centers its total advance about it, RIGHT ends it there.

Arc length is measured over the path flattened the way stroke_path_aa flattens it, so the distance a glyph is placed at is a distance along the curve that gets drawn. Sub-paths add end to end with no distance between them.

A glyph whose center falls before the start or past the end of the path is not drawn (SVG’s textPath rule): text longer than the curve loses its overflow rather than piling it up at the end. A string with no glyphs left on the path draws nothing.

text is one line – an embedded “\n” gets no line-break handling, since a path gives no second baseline to break onto.

Args:

  • canvas (Canvas): Canvas to draw into.
  • path (Path): Curve the baseline follows.
  • text (String): Text to draw, treated as a single line.
  • color (Color): Text color.
  • size (Float64): Font size in points.
  • offset (Float64): Distance along the path the string is placed at.
  • family (String): Font family name or generic alias.
  • slant (FontSlant): Requested upright/italic/oblique style.
  • weight (FontWeight): Requested normal/bold weight.
  • align (TextAlign): Where offset sits in the string – its start, middle or end.
  • kerning (Bool): Apply the font’s pair kerning between adjacent glyphs.
  • ligatures (Bool): Apply the font’s GSUB shaping – ligatures and Arabic contextual forms. False lays out one glyph per character.
  • cache (FontCache): Shared cache for font resolution and parsed faces.

Raises: