From 244e1dc41da4f920df7b9f1384a85f22389471ea Mon Sep 17 00:00:00 2001 From: mahulst Date: Sun, 16 Oct 2022 20:09:32 +0200 Subject: [PATCH 1/4] Fixes incorrect glyph positioning for text2d --- crates/bevy_text/src/glyph_brush.rs | 14 ++++++++++++-- crates/bevy_text/src/lib.rs | 5 +++++ crates/bevy_text/src/pipeline.rs | 4 +++- crates/bevy_text/src/text2d.rs | 4 +++- crates/bevy_ui/src/widget/text.rs | 5 ++++- examples/2d/text2d.rs | 2 ++ 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/crates/bevy_text/src/glyph_brush.rs b/crates/bevy_text/src/glyph_brush.rs index e9961d3e49f4c..276ea44386673 100644 --- a/crates/bevy_text/src/glyph_brush.rs +++ b/crates/bevy_text/src/glyph_brush.rs @@ -7,7 +7,9 @@ use glyph_brush_layout::{ FontId, GlyphPositioner, Layout, SectionGeometry, SectionGlyph, SectionText, ToSectionText, }; -use crate::{error::TextError, Font, FontAtlasSet, GlyphAtlasInfo, TextAlignment, TextSettings}; +use crate::{ + error::TextError, Font, FontAtlasSet, GlyphAtlasInfo, TextAlignment, TextSettings, TextType, +}; pub struct GlyphBrush { fonts: Vec, @@ -53,6 +55,7 @@ impl GlyphBrush { texture_atlases: &mut Assets, textures: &mut Assets, text_settings: &TextSettings, + text_type: TextType, ) -> Result, TextError> { if glyphs.is_empty() { return Ok(Vec::new()); @@ -75,12 +78,14 @@ impl GlyphBrush { let mut min_x = std::f32::MAX; let mut min_y = std::f32::MAX; + let mut max_y = std::f32::MIN; for sg in &glyphs { let glyph = &sg.glyph; let scaled_font = sections_data[sg.section_index].3; min_x = min_x.min(glyph.position.x); min_y = min_y.min(glyph.position.y - scaled_font.ascent()); + max_y = max_y.max(glyph.position.y - scaled_font.descent()); } min_x = min_x.floor(); min_y = min_y.floor(); @@ -120,7 +125,12 @@ impl GlyphBrush { let size = Vec2::new(glyph_rect.width(), glyph_rect.height()); let x = bounds.min.x + size.x / 2.0 - min_x; - let y = bounds.min.y + size.y / 2.0 - min_y; + + let y = match text_type { + TextType::Text2D => max_y - bounds.max.y + size.y / 2.0, + TextType::Ui => bounds.min.y + size.y / 2.0 - min_y, + }; + let position = adjust.position(Vec2::new(x, y)); positioned_glyphs.push(PositionedGlyph { diff --git a/crates/bevy_text/src/lib.rs b/crates/bevy_text/src/lib.rs index 277a51dd47c81..552b2d3e2b038 100644 --- a/crates/bevy_text/src/lib.rs +++ b/crates/bevy_text/src/lib.rs @@ -56,6 +56,11 @@ impl Default for TextSettings { } } +pub enum TextType { + Ui, + Text2D, +} + impl Plugin for TextPlugin { fn build(&self, app: &mut App) { app.add_asset::() diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index c3b85317e1c59..df3080736a9ab 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -11,7 +11,7 @@ use glyph_brush_layout::{FontId, SectionText}; use crate::{ error::TextError, glyph_brush::GlyphBrush, scale_value, Font, FontAtlasSet, PositionedGlyph, - TextAlignment, TextSection, TextSettings, + TextAlignment, TextSection, TextSettings, TextType, }; #[derive(Default, Resource)] @@ -50,6 +50,7 @@ impl TextPipeline { texture_atlases: &mut Assets, textures: &mut Assets, text_settings: &TextSettings, + text_type: TextType, ) -> Result { let mut scaled_fonts = Vec::new(); let sections = sections @@ -105,6 +106,7 @@ impl TextPipeline { texture_atlases, textures, text_settings, + text_type, )?; Ok(TextLayoutInfo { glyphs, size }) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 8097db3e5cddc..ead32c0714cb1 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -23,7 +23,7 @@ use bevy_window::{WindowId, WindowScaleFactorChanged, Windows}; use crate::{ Font, FontAtlasSet, HorizontalAlign, Text, TextError, TextLayoutInfo, TextPipeline, - TextSettings, VerticalAlign, + TextSettings, TextType, VerticalAlign, }; /// The calculated size of text drawn in 2D scene. @@ -182,6 +182,7 @@ pub fn update_text2d_layout( ), None => Vec2::new(f32::MAX, f32::MAX), }; + match text_pipeline.queue_text( &fonts, &text.sections, @@ -192,6 +193,7 @@ pub fn update_text2d_layout( &mut *texture_atlases, &mut *textures, text_settings.as_ref(), + TextType::Text2D, ) { Err(TextError::NoSuchFont) => { // There was an error processing the text layout, let's add this entity to the diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index ddc7567f1b5e4..e20a33f2d0616 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -8,7 +8,9 @@ use bevy_ecs::{ use bevy_math::Vec2; use bevy_render::texture::Image; use bevy_sprite::TextureAtlas; -use bevy_text::{Font, FontAtlasSet, Text, TextError, TextLayoutInfo, TextPipeline, TextSettings}; +use bevy_text::{ + Font, FontAtlasSet, Text, TextError, TextLayoutInfo, TextPipeline, TextSettings, TextType, +}; use bevy_window::Windows; #[derive(Debug, Default)] @@ -118,6 +120,7 @@ pub fn text_system( &mut *texture_atlases, &mut *textures, text_settings.as_ref(), + TextType::Ui, ) { Err(TextError::NoSuchFont) => { // There was an error processing the text layout, let's add this entity to the diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index f5dce2403701d..e0093aae5dd9a 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -19,8 +19,10 @@ fn main() { #[derive(Component)] struct AnimateTranslation; + #[derive(Component)] struct AnimateRotation; + #[derive(Component)] struct AnimateScale; From a4540c235fcb3c6571eab02fda44211985c3ed8d Mon Sep 17 00:00:00 2001 From: mahulst Date: Mon, 17 Oct 2022 15:38:07 +0200 Subject: [PATCH 2/4] Add comment and rename text orientation enum --- crates/bevy_text/src/glyph_brush.rs | 12 +++++++----- crates/bevy_text/src/lib.rs | 9 ++++++--- crates/bevy_text/src/pipeline.rs | 6 +++--- crates/bevy_text/src/text2d.rs | 4 ++-- crates/bevy_ui/src/widget/text.rs | 5 +++-- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/crates/bevy_text/src/glyph_brush.rs b/crates/bevy_text/src/glyph_brush.rs index 276ea44386673..b7ae83e52565a 100644 --- a/crates/bevy_text/src/glyph_brush.rs +++ b/crates/bevy_text/src/glyph_brush.rs @@ -8,7 +8,8 @@ use glyph_brush_layout::{ }; use crate::{ - error::TextError, Font, FontAtlasSet, GlyphAtlasInfo, TextAlignment, TextSettings, TextType, + error::TextError, Font, FontAtlasSet, GlyphAtlasInfo, TextAlignment, TextSettings, + YAxisOrientation, }; pub struct GlyphBrush { @@ -55,7 +56,7 @@ impl GlyphBrush { texture_atlases: &mut Assets, textures: &mut Assets, text_settings: &TextSettings, - text_type: TextType, + y_axis_orientation: YAxisOrientation, ) -> Result, TextError> { if glyphs.is_empty() { return Ok(Vec::new()); @@ -89,6 +90,7 @@ impl GlyphBrush { } min_x = min_x.floor(); min_y = min_y.floor(); + max_y = max_y.floor(); let mut positioned_glyphs = Vec::new(); for sg in glyphs { @@ -126,9 +128,9 @@ impl GlyphBrush { let x = bounds.min.x + size.x / 2.0 - min_x; - let y = match text_type { - TextType::Text2D => max_y - bounds.max.y + size.y / 2.0, - TextType::Ui => bounds.min.y + size.y / 2.0 - min_y, + let y = match y_axis_orientation { + YAxisOrientation::BottomToTop => max_y - bounds.max.y + size.y / 2.0, + YAxisOrientation::TopToBottom => bounds.min.y + size.y / 2.0 - min_y, }; let position = adjust.position(Vec2::new(x, y)); diff --git a/crates/bevy_text/src/lib.rs b/crates/bevy_text/src/lib.rs index 552b2d3e2b038..f2ce599b682d4 100644 --- a/crates/bevy_text/src/lib.rs +++ b/crates/bevy_text/src/lib.rs @@ -56,9 +56,12 @@ impl Default for TextSettings { } } -pub enum TextType { - Ui, - Text2D, +/// Text is rendered for two different view projections, normal Text2DBundle is rendered with a +/// BottomToTop YAxis, and UI is rendered with a TopToBottom YAxis. This matters for text because of +/// the glyph positioning is different in either layout. +pub enum YAxisOrientation { + TopToBottom, + BottomToTop, } impl Plugin for TextPlugin { diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index df3080736a9ab..4f252e8e13dd2 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -11,7 +11,7 @@ use glyph_brush_layout::{FontId, SectionText}; use crate::{ error::TextError, glyph_brush::GlyphBrush, scale_value, Font, FontAtlasSet, PositionedGlyph, - TextAlignment, TextSection, TextSettings, TextType, + TextAlignment, TextSection, TextSettings, YAxisOrientation, }; #[derive(Default, Resource)] @@ -50,7 +50,7 @@ impl TextPipeline { texture_atlases: &mut Assets, textures: &mut Assets, text_settings: &TextSettings, - text_type: TextType, + y_axis_orientation: YAxisOrientation, ) -> Result { let mut scaled_fonts = Vec::new(); let sections = sections @@ -106,7 +106,7 @@ impl TextPipeline { texture_atlases, textures, text_settings, - text_type, + y_axis_orientation, )?; Ok(TextLayoutInfo { glyphs, size }) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index ead32c0714cb1..530812cea1e7d 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -23,7 +23,7 @@ use bevy_window::{WindowId, WindowScaleFactorChanged, Windows}; use crate::{ Font, FontAtlasSet, HorizontalAlign, Text, TextError, TextLayoutInfo, TextPipeline, - TextSettings, TextType, VerticalAlign, + TextSettings, VerticalAlign, YAxisOrientation, }; /// The calculated size of text drawn in 2D scene. @@ -193,7 +193,7 @@ pub fn update_text2d_layout( &mut *texture_atlases, &mut *textures, text_settings.as_ref(), - TextType::Text2D, + YAxisOrientation::BottomToTop, ) { Err(TextError::NoSuchFont) => { // There was an error processing the text layout, let's add this entity to the diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index e20a33f2d0616..c2b3f6ef68e21 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -9,7 +9,8 @@ use bevy_math::Vec2; use bevy_render::texture::Image; use bevy_sprite::TextureAtlas; use bevy_text::{ - Font, FontAtlasSet, Text, TextError, TextLayoutInfo, TextPipeline, TextSettings, TextType, + Font, FontAtlasSet, Text, TextError, TextLayoutInfo, TextPipeline, TextSettings, + YAxisOrientation, }; use bevy_window::Windows; @@ -120,7 +121,7 @@ pub fn text_system( &mut *texture_atlases, &mut *textures, text_settings.as_ref(), - TextType::Ui, + YAxisOrientation::TopToBottom, ) { Err(TextError::NoSuchFont) => { // There was an error processing the text layout, let's add this entity to the From 88e88db142bf3c80eb39566bb884af918aee7779 Mon Sep 17 00:00:00 2001 From: mahulst Date: Mon, 17 Oct 2022 16:27:39 +0200 Subject: [PATCH 3/4] Fix docs --- crates/bevy_text/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_text/src/lib.rs b/crates/bevy_text/src/lib.rs index f2ce599b682d4..053eef6c69acf 100644 --- a/crates/bevy_text/src/lib.rs +++ b/crates/bevy_text/src/lib.rs @@ -56,8 +56,8 @@ impl Default for TextSettings { } } -/// Text is rendered for two different view projections, normal Text2DBundle is rendered with a -/// BottomToTop YAxis, and UI is rendered with a TopToBottom YAxis. This matters for text because of +/// Text is rendered for two different view projections, normal `Text2DBundle` is rendered with a +/// `BottomToTop` y axis, and UI is rendered with a `TopToBottom` y axis. This matters for text because of /// the glyph positioning is different in either layout. pub enum YAxisOrientation { TopToBottom, From 5f5e3200dae1b5ceef54e927f988301559a5cb99 Mon Sep 17 00:00:00 2001 From: Michel van der Hulst Date: Mon, 17 Oct 2022 17:53:19 +0200 Subject: [PATCH 4/4] Update crates/bevy_text/src/lib.rs Co-authored-by: Alice Cecile --- crates/bevy_text/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_text/src/lib.rs b/crates/bevy_text/src/lib.rs index 053eef6c69acf..cd5b8abb754e6 100644 --- a/crates/bevy_text/src/lib.rs +++ b/crates/bevy_text/src/lib.rs @@ -57,7 +57,7 @@ impl Default for TextSettings { } /// Text is rendered for two different view projections, normal `Text2DBundle` is rendered with a -/// `BottomToTop` y axis, and UI is rendered with a `TopToBottom` y axis. This matters for text because of +/// `BottomToTop` y axis, and UI is rendered with a `TopToBottom` y axis. This matters for text because /// the glyph positioning is different in either layout. pub enum YAxisOrientation { TopToBottom,