From b2dcb7d8db32ab588f084763b6e08424c328b902 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 6 Sep 2024 11:30:32 +0200 Subject: [PATCH] Fix bug in size calculation of truncated text (#5076) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The width of the elision character (`…`) was never included in the size calculation --- crates/epaint/src/text/text_layout.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/epaint/src/text/text_layout.rs b/crates/epaint/src/text/text_layout.rs index d8f59381f91..b3e6953015d 100644 --- a/crates/epaint/src/text/text_layout.rs +++ b/crates/epaint/src/text/text_layout.rs @@ -98,6 +98,9 @@ pub fn layout(fonts: &mut FontsImpl, job: Arc) -> Galley { if elided { if let Some(last_row) = rows.last_mut() { replace_last_glyph_with_overflow_character(fonts, &job, last_row); + if let Some(last) = last_row.glyphs.last() { + last_row.rect.max.x = last.max_x(); + } } } @@ -1115,4 +1118,21 @@ mod tests { vec!["日本語とEnglish", "の混在した文章"] ); } + + #[test] + fn test_truncate_width() { + let mut fonts = FontsImpl::new(1.0, 1024, FontDefinitions::default()); + let mut layout_job = + LayoutJob::single_section("# DNA\nMore text".into(), TextFormat::default()); + layout_job.wrap.max_width = f32::INFINITY; + layout_job.wrap.max_rows = 1; + let galley = layout(&mut fonts, layout_job.into()); + assert!(galley.elided); + assert_eq!( + galley.rows.iter().map(|row| row.text()).collect::>(), + vec!["# DNA…"] + ); + let row = &galley.rows[0]; + assert_eq!(row.rect.max.x, row.glyphs.last().unwrap().max_x()); + } }