Skip to main content

wrap_text_with_tracking

Function wrap_text_with_tracking 

Source
pub fn wrap_text_with_tracking(
    text: &str,
    font: &Font,
    emoji_font: &Option<Font>,
    max_width: Option<f32>,
    letter_spacing: f32,
) -> Vec<String>
Expand description

Wrap text respecting emoji font fallback for accurate measurement, honouring letter_spacing in the fit test itself (issue #125).

This is the correct entry point for any caller whose paint step applies non-zero letter-spacing (i.e. it also calls measure_text_with_fallback / draw_text_with_fallback with a non-zero letter_spacing): the word-fits-on-this-line test below now measures with the same tracking that will actually be painted, so the line count this function returns is the line count the paint step will agree with.

Before this existed, every caller went through wrap_text_with_fallback, whose fit test always measured at zero tracking regardless of the real value. That is harmless when letter_spacing == 0.0, but wrong otherwise in a specific and dangerous way for negative tracking (the register this engine targets: tight negative tracking on 200–400px display type): negative tracking makes the real painted line narrower than the zero-tracking fit test believes, so the fit test can decide a line needs to wrap when the real, tighter text would still have fit on one line — and because the box that holds the text is sized from one width sample while the paint step re-derives the same (still zero-tracking) wrap decision against a different available width later in layout, the two passes can disagree on the line count, leaving painted lines centered inside a box sized for a different number of lines.