cosmic-text icon indicating copy to clipboard operation
cosmic-text copied to clipboard

Whitespace isn't wrapped correctly

Open tamewild opened this issue 2 months ago • 0 comments

If you have a line with a lot of whitespaces at the end, it might not wrap properly. The test case below fails as the line doesn't get wrapped.

Tested this on cosmic-text 0.11.2 and master.

This bug also causes weird behavior in iced (master) and I've replicated it before in cosmic-edit (though that was kinda an old version)

Test case (fails assertion, line was 34.51465 and not a valid value with the given buffer width of 32.0)

use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping};

fn main() {
    let text = "         ";

    let mut font_system = FontSystem::new();

    let mut buf = Buffer::new(&mut font_system, Metrics::new(14.0, 14.0 * 1.5));

    buf.set_text(&mut font_system, text, Attrs::new(), Shaping::Advanced);

    buf.set_size(&mut font_system, 32.0, f32::MAX);

    for x in buf.layout_runs() {
        let start = x.glyphs.first().map(|x| x.start).unwrap();
        let end = x.glyphs.last().map(|x| x.end).unwrap();

        println!("{:?}", &text[start..end]);

        assert!(x.line_w <= 32.0, "line_w was {} compared to the buffer width of {}", x.line_w, 32.0);
    }
}

tamewild avatar May 03 '24 11:05 tamewild