Skip to main content

fit_width

Function fit_width 

Source
pub fn fit_width<'a>(
    text: &'a str,
    max_width: f32,
    width: impl FnMut(&str) -> f32,
) -> Cow<'a, str>
Expand description

text shortened to fit max_width as width measures it, ending in ... when it had to cut.

The property this holds, whatever width is: the result’s measured width is at most max_width, and a text that already fits comes back unchanged and borrowed. When not even ... fits, the result is empty.

Cut on character boundaries, so a multi-byte name is never split inside a code point. The longest fitting prefix is found by bisection and then checked, stepping down while it does not fit, so a measurer that is not monotone in the prefix length (kerning can make it so) still cannot return a result wider than asked.