From 954b44b867e07cc6eaa79a609e2d8ef7110bc684 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 23 Feb 2023 11:10:14 +0000 Subject: [PATCH 01/12] changes: * multiply `text_layout_info.size` by the reciprocal of the scale factor after generating text2d's text layout so it relects the actual size of the text. * reorder the operations in `extract_2d_sprite` so that the alignment offset is applied before the scale factor scaling. --- crates/bevy_text/src/text2d.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 26ecf5796ac2d..f690ab45e9eb5 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -107,6 +107,9 @@ pub fn extract_text2d_sprite( let alignment_offset = text_layout_info.size * text_anchor; let mut color = Color::WHITE; let mut current_section = usize::MAX; + let text_transform = *text_transform + * Transform::from_translation((alignment_offset).extend(0.)) + * GlobalTransform::from_scale(Vec3::splat(scale_factor.recip())); for text_glyph in text_glyphs { if text_glyph.section_index != current_section { color = text.sections[text_glyph.section_index] @@ -121,13 +124,7 @@ pub fn extract_text2d_sprite( let handle = atlas.texture.clone_weak(); let index = text_glyph.atlas_info.glyph_index; let rect = Some(atlas.textures[index]); - - let glyph_transform = - Transform::from_translation((alignment_offset + text_glyph.position).extend(0.)); - - let transform = *text_transform - * GlobalTransform::from_scale(Vec3::splat(scale_factor.recip())) - * glyph_transform; + let transform = text_transform * Transform::from_translation((text_glyph.position).extend(0.)); extracted_sprites.sprites.push(ExtractedSprite { entity, @@ -187,7 +184,6 @@ pub fn update_text2d_layout( scale_value(bounds.size.x, scale_factor), scale_value(bounds.size.y, scale_factor), ); - match text_pipeline.queue_text( &fonts, &text.sections, @@ -210,10 +206,16 @@ pub fn update_text2d_layout( Err(e @ TextError::FailedToAddGlyph(_)) => { panic!("Fatal error when processing text: {e}."); } - Ok(info) => match text_layout_info { - Some(mut t) => *t = info, - None => { - commands.entity(entity).insert(info); + Ok(mut info) => { + info.size = Vec2::new( + scale_value(info.size.x, scale_factor.recip()), + scale_value(info.size.y, scale_factor.recip()), + ); + match text_layout_info { + Some(mut t) => *t = info, + None => { + commands.entity(entity).insert(info); + } } }, } From f6c9a5639831558ea5b1ec9d70b59152365653ba Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 23 Feb 2023 11:46:30 +0000 Subject: [PATCH 02/12] cargo fmt --all --- crates/bevy_text/src/text2d.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index f690ab45e9eb5..600a5cba466ac 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -124,7 +124,8 @@ pub fn extract_text2d_sprite( let handle = atlas.texture.clone_weak(); let index = text_glyph.atlas_info.glyph_index; let rect = Some(atlas.textures[index]); - let transform = text_transform * Transform::from_translation((text_glyph.position).extend(0.)); + let transform = + text_transform * Transform::from_translation((text_glyph.position).extend(0.)); extracted_sprites.sprites.push(ExtractedSprite { entity, @@ -217,7 +218,7 @@ pub fn update_text2d_layout( commands.entity(entity).insert(info); } } - }, + } } } } From 9fd08bb614890cf2206aa8d9fe0c1346f6ed3e08 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Fri, 21 Apr 2023 17:34:29 +0100 Subject: [PATCH 03/12] Add comment to `Text2dBounds` adding that uses logical pixels. --- crates/bevy_text/src/text2d.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index b86ebf61b1bea..cb8596bdcd2f8 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -27,7 +27,7 @@ use crate::{ TextPipeline, TextSettings, YAxisOrientation, }; -/// The maximum width and height of text. The text will wrap according to the specified size. +/// The maximum width and height of text in logical pixels. The text will wrap according to the specified size. /// Characters out of the bounds after wrapping will be truncated. Text is aligned according to the /// specified `TextAlignment`. /// @@ -37,6 +37,7 @@ use crate::{ #[derive(Component, Copy, Clone, Debug, Reflect)] #[reflect(Component)] pub struct Text2dBounds { + /// The maximum width and height of text in logical pixels. pub size: Vec2, } @@ -200,10 +201,6 @@ pub fn update_text2d_layout( panic!("Fatal error when processing text: {e}."); } Ok(mut info) => { - info.size = Vec2::new( - scale_value(info.size.x, scale_factor.recip()), - scale_value(info.size.y, scale_factor.recip()), - ); *text_layout_info = info; } } From cd8c1aa5988ad055c1292aa1ec98c6562dbc52df Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Fri, 21 Apr 2023 18:06:14 +0100 Subject: [PATCH 04/12] * renamed `Text2dBounds` and `TextLayoutInfo` fields to `logical_size` * convert `TextLayoutInfo` size to logical coords --- crates/bevy_text/src/text2d.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index cb8596bdcd2f8..60c6dea25a3e7 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -107,7 +107,7 @@ pub fn extract_text2d_sprite( } let text_anchor = -(anchor.as_vec() + 0.5); - let alignment_translation = text_layout_info.size * text_anchor; + let alignment_translation = text_layout_info.size * scale_factor * text_anchor; let transform = *global_transform * scaling * GlobalTransform::from_translation(alignment_translation.extend(0.)); @@ -172,6 +172,8 @@ pub fn update_text2d_layout( .map(|window| window.resolution.scale_factor()) .unwrap_or(1.0); + let inverse_scale_factor = scale_factor.recip(); + for (entity, text, bounds, mut text_layout_info) in &mut text_query { if factor_changed || text.is_changed() || bounds.is_changed() || queue.remove(&entity) { let text_bounds = Vec2::new( @@ -201,6 +203,8 @@ pub fn update_text2d_layout( 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.x, inverse_scale_factor); *text_layout_info = info; } } From a9941d4e46588de744f1929e32a4f26aadeaaad5 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Fri, 21 Apr 2023 18:06:21 +0100 Subject: [PATCH 05/12] * renamed `Text2dBounds` and `TextLayoutInfo` fields to `logical_size` * converted `TextLayoutInfo` size to logical coords --- crates/bevy_text/src/pipeline.rs | 4 ++-- crates/bevy_text/src/text2d.rs | 14 +++++++------- examples/2d/text2d.rs | 4 ++-- examples/stress_tests/many_glyphs.rs | 2 +- examples/stress_tests/text_pipeline.rs | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index e71e49c030e67..2fc8787ddb25f 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -26,7 +26,7 @@ pub struct TextPipeline { #[derive(Component, Clone, Default, Debug)] pub struct TextLayoutInfo { pub glyphs: Vec, - pub size: Vec2, + pub logical_size: Vec2, } impl TextPipeline { @@ -115,7 +115,7 @@ impl TextPipeline { y_axis_orientation, )?; - Ok(TextLayoutInfo { glyphs, size }) + Ok(TextLayoutInfo { glyphs, logical_size: size }) } pub fn create_text_measure( diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 60c6dea25a3e7..a054f33fee0c1 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -38,7 +38,7 @@ use crate::{ #[reflect(Component)] pub struct Text2dBounds { /// The maximum width and height of text in logical pixels. - pub size: Vec2, + pub logical_size: Vec2, } impl Default for Text2dBounds { @@ -51,7 +51,7 @@ impl Default for Text2dBounds { impl Text2dBounds { /// Unbounded text will not be truncated or wrapped. pub const UNBOUNDED: Self = Self { - size: Vec2::splat(f32::INFINITY), + logical_size: Vec2::splat(f32::INFINITY), }; } @@ -107,7 +107,7 @@ pub fn extract_text2d_sprite( } let text_anchor = -(anchor.as_vec() + 0.5); - let alignment_translation = text_layout_info.size * scale_factor * text_anchor; + let alignment_translation = text_layout_info.logical_size * scale_factor * text_anchor; let transform = *global_transform * scaling * GlobalTransform::from_translation(alignment_translation.extend(0.)); @@ -177,8 +177,8 @@ pub fn update_text2d_layout( for (entity, text, bounds, mut text_layout_info) in &mut text_query { if factor_changed || text.is_changed() || bounds.is_changed() || queue.remove(&entity) { let text_bounds = Vec2::new( - scale_value(bounds.size.x, scale_factor), - scale_value(bounds.size.y, scale_factor), + scale_value(bounds.logical_size.x, scale_factor), + scale_value(bounds.logical_size.y, scale_factor), ); match text_pipeline.queue_text( &fonts, @@ -203,8 +203,8 @@ pub fn update_text2d_layout( 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.x, inverse_scale_factor); + info.logical_size.x = scale_value(info.logical_size.x, inverse_scale_factor); + info.logical_size.y = scale_value(info.logical_size.x, inverse_scale_factor); *text_layout_info = info; } } diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index 8fe1b4b05b642..e379352d41d5e 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -96,7 +96,7 @@ fn setup(mut commands: Commands, asset_server: Res) { }, text_2d_bounds: Text2dBounds { // Wrap text in the rectangle - size: box_size, + logical_size: box_size, }, // ensure the text is drawn on top of the box transform: Transform::from_translation(Vec3::Z), @@ -128,7 +128,7 @@ fn setup(mut commands: Commands, asset_server: Res) { }, text_2d_bounds: Text2dBounds { // Wrap text in the rectangle - size: other_box_size, + logical_size: other_box_size, }, // ensure the text is drawn on top of the box transform: Transform::from_translation(Vec3::Z), diff --git a/examples/stress_tests/many_glyphs.rs b/examples/stress_tests/many_glyphs.rs index f4cf88f33107c..5ff9cc4d88489 100644 --- a/examples/stress_tests/many_glyphs.rs +++ b/examples/stress_tests/many_glyphs.rs @@ -66,7 +66,7 @@ fn setup(mut commands: Commands, asset_server: Res) { text, text_anchor: bevy::sprite::Anchor::Center, text_2d_bounds: Text2dBounds { - size: Vec2::new(1000., f32::INFINITY), + logical_size: Vec2::new(1000., f32::INFINITY), }, ..Default::default() }); diff --git a/examples/stress_tests/text_pipeline.rs b/examples/stress_tests/text_pipeline.rs index c44f11ca8fdac..b7b5319fb1d16 100644 --- a/examples/stress_tests/text_pipeline.rs +++ b/examples/stress_tests/text_pipeline.rs @@ -63,6 +63,6 @@ fn spawn(mut commands: Commands, asset_server: Res) { fn update_text_bounds(time: Res