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

Bidirectional text overflows buffer instead of wrapping

Open lucieleblanc opened this issue 2 months ago • 0 comments

cosmic-text version: 0.11.2 System info: Ubuntu 22.04, GNOME on Wayland

Hello! I'm an engineer working on Warp terminal, which uses cosmic-text for text layout on Linux. While working on custom prompt support, I noticed the layout_to_buffer function produces lines that overflow the buffer width when given bidirectional text.

I added a test with mixed English and Arabic text to tests/wrap_stability.rs that reproduces the issue on this branch. Here's the snippet:

#[test]
fn wrap_bidirectional() {
	let mut font_system = FontSystem::new();
	let font_size = 14.0;
	let line_height = 20.0;
	let buffer_width = 80.0;

	let metrics = Metrics::new(font_size, line_height);

	let mut plain_buffer = Buffer::new(&mut font_system, metrics);

	let mut buffer = plain_buffer.borrow_with(&mut font_system);

	// Add some text
	buffer.set_wrap(Wrap::WordOrGlyph);
	buffer.set_text("breakfast, إفطار, lunch (غداء) and dinner - عشاء", Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced);

	// Set a size for the text buffer, in pixels
	buffer.set_size(buffer_width, 1000.0);

	let layout_line = buffer.line_layout(0).unwrap().first().unwrap();
	assert!(layout_line.w <= buffer_width, "layout line width ({}) was greater than buffer width ({})", layout_line.w, buffer_width);
}

When run, the above test fails with:

thread 'wrap_bidirectional' panicked at tests/wrap_stability.rs:135:5:
layout line width (97.90201) was greater than buffer width (80)

In Warp, this issue becomes visible when editing mixed-direction text in the input editor. Here's a screenshot with the same text from the test above, where the first Arabic word is cut off: Screenshot from 2024-05-06 16-15-49

I haven't seen this issue with left-to-right text, only mixed text directions, but it could be related to https://github.com/pop-os/cosmic-text/issues/219. Is this a known bug? Thank you in advance for your help!

lucieleblanc avatar May 06 '24 20:05 lucieleblanc