diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index a67e79f663..39cd5dcc65 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -145,6 +145,7 @@ impl TextPipeline { #[allow(clippy::too_many_arguments)] pub fn queue_text( &mut self, + layout_info: &mut TextLayoutInfo, fonts: &Assets, sections: &[TextSection], scale_factor: f64, @@ -156,9 +157,12 @@ impl TextPipeline { textures: &mut Assets, y_axis_orientation: YAxisOrientation, buffer: &mut CosmicBuffer, - ) -> Result { + ) -> Result<(), TextError> { + layout_info.glyphs.clear(); + layout_info.size = Default::default(); + if sections.is_empty() { - return Ok(TextLayoutInfo::default()); + return Ok(()); } self.update_buffer( @@ -175,14 +179,14 @@ impl TextPipeline { let font_system = &mut self.font_system.0; let swash_cache = &mut self.swash_cache.0; - let glyphs = buffer + buffer .layout_runs() .flat_map(|run| { run.glyphs .iter() .map(move |layout_glyph| (layout_glyph, run.line_y)) }) - .map(|(layout_glyph, line_y)| { + .try_for_each(|(layout_glyph, line_y)| { let section_index = layout_glyph.metadata; let font_handle = sections[section_index].style.font.clone_weak(); @@ -224,14 +228,12 @@ impl TextPipeline { // when glyphs are not limited to single byte representation, relevant for #1319 let pos_glyph = PositionedGlyph::new(position, glyph_size.as_vec2(), atlas_info, section_index); - Ok(pos_glyph) - }) - .collect::, _>>()?; + layout_info.glyphs.push(pos_glyph); + Ok(()) + })?; - Ok(TextLayoutInfo { - glyphs, - size: box_size, - }) + layout_info.size = box_size; + Ok(()) } /// Queues text for measurement diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 8c28fe3079..2c0570836b 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -170,7 +170,7 @@ pub fn update_text2d_layout( let inverse_scale_factor = scale_factor.recip(); - for (entity, text, bounds, mut text_layout_info, mut buffer) in &mut text_query { + for (entity, text, bounds, text_layout_info, mut buffer) in &mut text_query { if factor_changed || text.is_changed() || bounds.is_changed() || queue.remove(&entity) { let text_bounds = TextBounds { width: if text.linebreak_behavior == BreakLineOn::NoWrap { @@ -183,7 +183,9 @@ pub fn update_text2d_layout( .map(|height| scale_value(height, scale_factor)), }; + let text_layout_info = text_layout_info.into_inner(); match text_pipeline.queue_text( + text_layout_info, &fonts, &text.sections, scale_factor.into(), @@ -204,10 +206,11 @@ pub fn update_text2d_layout( Err(e @ (TextError::FailedToAddGlyph(_) | TextError::FailedToGetGlyphImage(_))) => { panic!("Fatal error when processing text: {e}."); } - Ok(mut info) => { - info.size.x = scale_value(info.size.x, inverse_scale_factor); - info.size.y = scale_value(info.size.y, inverse_scale_factor); - *text_layout_info = info; + Ok(()) => { + text_layout_info.size.x = + scale_value(text_layout_info.size.x, inverse_scale_factor); + text_layout_info.size.y = + scale_value(text_layout_info.size.y, inverse_scale_factor); } } } diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index e32296c12c..3cb0dbfbe3 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -224,7 +224,7 @@ fn queue_text( text: &Text, node: Ref, mut text_flags: Mut, - mut text_layout_info: Mut, + text_layout_info: Mut, buffer: &mut CosmicBuffer, ) { // Skip the text node if it is waiting for a new measure func @@ -240,7 +240,9 @@ fn queue_text( ) }; + let text_layout_info = text_layout_info.into_inner(); match text_pipeline.queue_text( + text_layout_info, fonts, &text.sections, scale_factor.into(), @@ -260,10 +262,11 @@ fn queue_text( Err(e @ (TextError::FailedToAddGlyph(_) | TextError::FailedToGetGlyphImage(_))) => { panic!("Fatal error when processing text: {e}."); } - Ok(mut info) => { - info.size.x = scale_value(info.size.x, inverse_scale_factor); - info.size.y = scale_value(info.size.y, inverse_scale_factor); - *text_layout_info = info; + Ok(()) => { + text_layout_info.size.x = + scale_value(text_layout_info.size.x, inverse_scale_factor); + text_layout_info.size.y = + scale_value(text_layout_info.size.y, inverse_scale_factor); text_flags.needs_recompute = false; } }