Skip to main content

draw_clipped

Function draw_clipped 

Source
pub fn draw_clipped(
    surface: &mut Surface<'_>,
    at: impl Into<Pos>,
    width: u16,
    text: &str,
    align: Align,
    style: Style,
) -> u16
Expand description

Truncate text to width columns, align it within those columns per align, and print it into surface at at. Returns the printed text’s display width in columns.

This is the truncate -> align -> print sequence every single-line widget in this crate needs, collapsed to one call: truncate_measured to fit width and get its own display width back in the same pass (a wide character can make the truncated text narrower than width, never wider, so that width isn’t just width itself), then Surface::print_aligned to place and print it. Align is a plain re-export of the HAlign that print_aligned itself takes, so no conversion is needed. Delegating keeps the offset math in one place instead of a second copy here: text is already fitted to width before it reaches print_aligned, so its own internal width measurement agrees with clipped_width and produces the same offset.

Reach for this instead of re-deriving the sequence by hand, the same way fill_rect is reached for instead of a hand-rolled fill loop; see Text’s and PrintLine’s own render for the base case.