From 8a33a6a6ce48fb95e5e8334e725e2d2794b6d804 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 30 Mar 2025 20:03:47 +0200 Subject: [PATCH] Make the section index private so we don't need to calculate it corectly --- crates/epaint/src/text/fonts.rs | 16 ++++++------- crates/epaint/src/text/text_layout.rs | 9 +++----- crates/epaint/src/text/text_layout_types.rs | 25 +++++++++------------ 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/crates/epaint/src/text/fonts.rs b/crates/epaint/src/text/fonts.rs index f92c5c103..c78ca99a6 100644 --- a/crates/epaint/src/text/fonts.rs +++ b/crates/epaint/src/text/fonts.rs @@ -1094,17 +1094,17 @@ mod tests { for (i, row) in whole.rows.iter().enumerate() { println!( - "Whole row {i}: section_index_at_start={}, first glyph section_index: {:?}", - row.row.section_index_at_start, - row.row.glyphs.first().map(|g| g.section_index) - ); + "Whole row {i}: section_index_at_start={}, first glyph section_index: {:?}", + row.row.section_index_at_start, + row.row.glyphs.first().map(|g| g.section_index) + ); } for (i, row) in split.rows.iter().enumerate() { println!( - "Split row {i}: section_index_at_start={}, first glyph section_index: {:?}", - row.row.section_index_at_start, - row.row.glyphs.first().map(|g| g.section_index) - ); + "Split row {i}: section_index_at_start={}, first glyph section_index: {:?}", + row.row.section_index_at_start, + row.row.glyphs.first().map(|g| g.section_index) + ); } // Don't compare for equaliity; but format with a specific precision and make sure we hit that. diff --git a/crates/epaint/src/text/text_layout.rs b/crates/epaint/src/text/text_layout.rs index 26bf6860c..b2dba96fc 100644 --- a/crates/epaint/src/text/text_layout.rs +++ b/crates/epaint/src/text/text_layout.rs @@ -664,13 +664,10 @@ fn galley_from_rows( mesh_bounds.union(row.visuals.mesh_bounds.translate(placed_row.pos.to_vec2())); num_vertices += row.visuals.mesh.vertices.len(); num_indices += row.visuals.mesh.indices.len(); - } - // Fill in correct section_index_at_start for each row: - for placed in &mut rows { - let row = Arc::make_mut(&mut placed.row); - if let Some(first_glyph) = row.glyphs.first_mut() { - row.section_index_at_start = first_glyph.section_index; + row.section_index_at_start = u32::MAX; // No longer in use. + for glyph in &mut row.glyphs { + glyph.section_index = u32::MAX; // No longer in use. } } diff --git a/crates/epaint/src/text/text_layout_types.rs b/crates/epaint/src/text/text_layout_types.rs index 5ee1fe142..c1e54b35a 100644 --- a/crates/epaint/src/text/text_layout_types.rs +++ b/crates/epaint/src/text/text_layout_types.rs @@ -578,8 +578,12 @@ impl std::ops::Deref for PlacedRow { #[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Row { - /// This is included in case there are no glyphs - pub section_index_at_start: u32, + /// This is included in case there are no glyphs. + /// + /// Only used during layout, then set to an invalid value in order to + /// enable the paragraph-concat optimization path without having to + /// adjust `section_index` when concatting. + pub(crate) section_index_at_start: u32, /// One for each `char`. pub glyphs: Vec, @@ -669,7 +673,11 @@ pub struct Glyph { pub uv_rect: UvRect, /// Index into [`LayoutJob::sections`]. Decides color etc. - pub section_index: u32, + /// + /// Only used during layout, then set to an invalid value in order to + /// enable the paragraph-concat optimization path without having to + /// adjust `section_index` when concatting. + pub(crate) section_index: u32, } impl Glyph { @@ -841,17 +849,6 @@ impl Galley { merged_galley.elided |= galley.elided; } - for placed in &mut merged_galley.rows { - let section_index_at_start = placed.row.glyphs.first().map(|g| g.section_index); - if let Some(section_index_at_start) = section_index_at_start { - if placed.row.section_index_at_start != section_index_at_start { - // This `make_mut` is slow, so only do it when necessary. - let row = Arc::make_mut(&mut placed.row); - row.section_index_at_start = section_index_at_start; - } - } - } - if merged_galley.job.round_output_to_gui { merged_galley.round_output_to_gui(); }