Skip to main content

wrap_text

Function wrap_text 

Source
pub fn wrap_text(
    text: &str,
    max_width: f64,
    font: &FontMetrics,
    text_indent: f64,
    line_height_override: Option<f64>,
) -> TextLayout
Expand description

Wrap text to fit within a given width, and compute the resulting size.

Parameters:

  • text: the source text. Explicit \n characters always start a new paragraph.
  • max_width: maximum available width in points for non-indented lines.
  • font: font metrics used to measure words and spaces.
  • text_indent: first-line indent in points, subtracted from the first line of each paragraph only.
  • line_height_override: optional baseline-to-baseline distance in points. When None, font.line_height_pt() is used.

Returns:

  • TextLayout.lines: wrapped lines in visual order.
  • TextLayout.first_line_of_para: flags indicating whether each output line is the first line of a paragraph.
  • TextLayout.size: width of the longest emitted line and total block height.

Edge cases:

  • Empty input returns no lines and a zero-size block.
  • Empty paragraphs caused by consecutive \n are preserved as blank lines.
  • Whitespace inside a paragraph is normalized by split_whitespace(), so runs of spaces do not survive wrapping.
  • Words are never split internally; a word wider than max_width is placed on its own line and may overflow horizontally.
  • If text_indent >= max_width, the first line’s available width clamps to 0 so the paragraph still wraps deterministically.