From b9359607a72caf7e0a19d0312fd7e8e182a3708a Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Fri, 14 Jun 2024 16:55:19 +0100 Subject: [PATCH] fix non-exact text alignment --- crates/bevy_text/src/glyph_brush.rs | 3 ++- crates/bevy_text/src/pipeline.rs | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/bevy_text/src/glyph_brush.rs b/crates/bevy_text/src/glyph_brush.rs index 3b17252cc167b..a6189a30be41e 100644 --- a/crates/bevy_text/src/glyph_brush.rs +++ b/crates/bevy_text/src/glyph_brush.rs @@ -64,6 +64,7 @@ impl GlyphBrush { textures: &mut Assets, text_settings: &TextSettings, y_axis_orientation: YAxisOrientation, + h_anchor: f32, ) -> Result, TextError> { if glyphs.is_empty() { return Ok(Vec::new()); @@ -126,7 +127,7 @@ impl GlyphBrush { let glyph_rect = texture_atlas.textures[atlas_info.glyph_index]; let size = glyph_rect.size().as_vec2(); - let x = bounds.min.x + size.x / 2.0 - text_bounds.min.x; + let x = bounds.min.x + size.x / 2.0 + h_anchor; let y = match y_axis_orientation { YAxisOrientation::BottomToTop => { diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index 26c547466e05f..e6d945a7a9b84 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -87,6 +87,13 @@ impl TextPipeline { let size = compute_text_bounds(§ion_glyphs, |index| scaled_fonts[index]).size(); + let h_anchor = match text_alignment { + JustifyText::Left => 0.0, + JustifyText::Center => bounds.x * 0.5, + JustifyText::Right => bounds.x * 1.0, + } + .floor(); + let glyphs = self.brush.process_glyphs( section_glyphs, §ions, @@ -96,6 +103,7 @@ impl TextPipeline { textures, text_settings, y_axis_orientation, + h_anchor, )?; Ok(TextLayoutInfo {