Skip to content

Commit

Permalink
Fix bug in size calculation of truncated text (emilk#5076)
Browse files Browse the repository at this point in the history
The width of the elision character (`…`) was never included in the size
calculation
  • Loading branch information
emilk authored Sep 6, 2024
1 parent 7cb61f8 commit b2dcb7d
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions crates/epaint/src/text/text_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ pub fn layout(fonts: &mut FontsImpl, job: Arc<LayoutJob>) -> 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();
}
}
}

Expand Down Expand Up @@ -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<_>>(),
vec!["# DNA…"]
);
let row = &galley.rows[0];
assert_eq!(row.rect.max.x, row.glyphs.last().unwrap().max_x());
}
}

0 comments on commit b2dcb7d

Please sign in to comment.