1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Exclude \n when splitting Galleys (#7316)

* Follow up to #7146 

Previously when galleys were splitted, each exept the last had an extra
empty row that had to be removed when they were concated. This changes
it to remove the `\n` from the layout jobs when splitting.
This commit is contained in:
Lucas Meurer
2025-07-09 14:53:19 +02:00
committed by GitHub
parent a7f14ca176
commit 207e71c2ae
4 changed files with 146 additions and 60 deletions

View File

@@ -204,20 +204,12 @@ fn calculate_intrinsic_size(
) -> Vec2 {
let mut intrinsic_size = Vec2::ZERO;
for (idx, paragraph) in paragraphs.iter().enumerate() {
if paragraph.glyphs.is_empty() {
if idx == 0 {
intrinsic_size.y += point_scale.round_to_pixel(paragraph.empty_paragraph_height);
}
continue;
}
intrinsic_size.x = f32::max(
paragraph
.glyphs
.last()
.map(|l| l.max_x())
.unwrap_or_default(),
intrinsic_size.x,
);
let width = paragraph
.glyphs
.last()
.map(|l| l.max_x())
.unwrap_or_default();
intrinsic_size.x = f32::max(intrinsic_size.x, width);
let mut height = paragraph
.glyphs
@@ -253,7 +245,7 @@ fn rows_from_paragraphs(
if paragraph.glyphs.is_empty() {
rows.push(PlacedRow {
pos: Pos2::ZERO,
pos: pos2(0.0, f32::NAN),
row: Arc::new(Row {
section_index_at_start: paragraph.section_index_at_start,
glyphs: vec![],
@@ -659,12 +651,12 @@ fn galley_from_rows(
let mut cursor_y = 0.0;
for placed_row in &mut rows {
let mut max_row_height = first_row_min_height.max(placed_row.rect().height());
let mut max_row_height = first_row_min_height.at_least(placed_row.height());
let row = Arc::make_mut(&mut placed_row.row);
first_row_min_height = 0.0;
for glyph in &row.glyphs {
max_row_height = max_row_height.max(glyph.line_height);
max_row_height = max_row_height.at_least(glyph.line_height);
}
max_row_height = point_scale.round_to_pixel(max_row_height);
@@ -1212,4 +1204,72 @@ mod tests {
assert_eq!(row.pos, Pos2::ZERO);
assert_eq!(row.rect().max.x, row.glyphs.last().unwrap().max_x());
}
#[test]
fn test_empty_row() {
let mut fonts = FontsImpl::new(
1.0,
1024,
AlphaFromCoverage::default(),
FontDefinitions::default(),
);
let font_id = FontId::default();
let font_height = fonts.font(&font_id).row_height();
let job = LayoutJob::simple(String::new(), font_id, Color32::WHITE, f32::INFINITY);
let galley = layout(&mut fonts, job.into());
assert_eq!(galley.rows.len(), 1, "Expected one row");
assert_eq!(
galley.rows[0].row.glyphs.len(),
0,
"Expected no glyphs in the empty row"
);
assert_eq!(
galley.size(),
Vec2::new(0.0, font_height.round()),
"Unexpected galley size"
);
assert_eq!(
galley.intrinsic_size(),
Vec2::new(0.0, font_height.round()),
"Unexpected intrinsic size"
);
}
#[test]
fn test_end_with_newline() {
let mut fonts = FontsImpl::new(
1.0,
1024,
AlphaFromCoverage::default(),
FontDefinitions::default(),
);
let font_id = FontId::default();
let font_height = fonts.font(&font_id).row_height();
let job = LayoutJob::simple("Hi!\n".to_owned(), font_id, Color32::WHITE, f32::INFINITY);
let galley = layout(&mut fonts, job.into());
assert_eq!(galley.rows.len(), 2, "Expected two rows");
assert_eq!(
galley.rows[1].row.glyphs.len(),
0,
"Expected no glyphs in the empty row"
);
assert_eq!(
galley.size().round(),
Vec2::new(17.0, font_height.round() * 2.0),
"Unexpected galley size"
);
assert_eq!(
galley.intrinsic_size().round(),
Vec2::new(17.0, font_height.round() * 2.0),
"Unexpected intrinsic size"
);
}
}