Skip to content
stroke_text

stroke_text

Mojo function 🡭

stroke_text

fn def stroke_text(mut canvas: Canvas, x: Float64, y: Float64, text: String, color: Color, size: Float64, width: Float64 = 1, family: String = "Sans", slant: FontSlant = FontSlant.NORMAL, weight: FontWeight = FontWeight.NORMAL, rotation: Float64 = 0, align: TextAlign = TextAlign.LEFT, join: LineJoin = LineJoin.ROUND, miter_limit: Float64 = 4, kerning: Bool = True, ligatures: Bool = True)

Stroke text outlines, resolving fonts for this call.

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): Stroke color.
  • size (Float64): Font size in points.
  • width (Float64): Stroke width in pixels.
  • 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.
  • join (LineJoin): How a corner of the outline is turned – see LineJoin.
  • miter_limit (Float64): Ratio past which a MITER join falls back to BEVEL, as a multiple of half the stroke width.
  • 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 stroke_text(mut canvas: Canvas, x: Float64, y: Float64, text: String, color: Color, size: Float64, width: Float64 = 1, family: String = "Sans", slant: FontSlant = FontSlant.NORMAL, weight: FontWeight = FontWeight.NORMAL, rotation: Float64 = 0, align: TextAlign = TextAlign.LEFT, join: LineJoin = LineJoin.ROUND, miter_limit: Float64 = 4, kerning: Bool = True, ligatures: Bool = True, *, mut cache: FontCache)

Outline text rather than filling it: the layout draw_text produces, with each glyph’s outline handed to stroke_path_aa instead of fill_path_aa. Cairo’s text_path + stroke and the HTML5 canvas’s strokeText; an outlined label reads over a busy background where a filled one does not.

Everything but the rasterization step is draw_text’s: the same shaping, kerning, line breaking, alignment and rotation about the (x, y) anchor, so a stroked string and a filled one sit on the same baseline and occupy the same box, give or take half a stroke width of outline on each side.

No glyph mask cache. It holds a glyph’s coverage, which is a property of the filled shape; the stroke around that shape is a different figure and would need a cache keyed by width, join and limit as well.

cap is not a parameter: every TrueType contour is closed (ttf.mojo’s outline_to_path closes each one), so a glyph outline has no ends to finish and stroke_path_aa would ignore the value.

The canvas transform applies through stroke_path_aa, so the stroke width is in user space and scales with the transform, as it does for any other stroked path.

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): Stroke color.
  • size (Float64): Font size in points.
  • width (Float64): Stroke width in pixels.
  • 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.
  • join (LineJoin): How a corner of the outline is turned – see LineJoin.
  • miter_limit (Float64): Ratio past which a MITER join falls back to BEVEL, as a multiple of half the stroke width.
  • 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: