Skip to content

Commit 0981789

Browse files
committed
Fixes incorrect glyph positioning for text2d (#6273)
# Objective Fixes #6272 ## Solution Revert to old way of positioning text for Text2D rendered text. Co-authored-by: Michel van der Hulst <hulstmichel@gmail.com>
1 parent c6e0da4 commit 0981789

File tree

6 files changed

+35
-5
lines changed

6 files changed

+35
-5
lines changed

crates/bevy_text/src/glyph_brush.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use glyph_brush_layout::{
77
FontId, GlyphPositioner, Layout, SectionGeometry, SectionGlyph, SectionText, ToSectionText,
88
};
99

10-
use crate::{error::TextError, Font, FontAtlasSet, GlyphAtlasInfo, TextAlignment, TextSettings};
10+
use crate::{
11+
error::TextError, Font, FontAtlasSet, GlyphAtlasInfo, TextAlignment, TextSettings,
12+
YAxisOrientation,
13+
};
1114

1215
pub struct GlyphBrush {
1316
fonts: Vec<FontArc>,
@@ -53,6 +56,7 @@ impl GlyphBrush {
5356
texture_atlases: &mut Assets<TextureAtlas>,
5457
textures: &mut Assets<Image>,
5558
text_settings: &TextSettings,
59+
y_axis_orientation: YAxisOrientation,
5660
) -> Result<Vec<PositionedGlyph>, TextError> {
5761
if glyphs.is_empty() {
5862
return Ok(Vec::new());
@@ -75,15 +79,18 @@ impl GlyphBrush {
7579

7680
let mut min_x = std::f32::MAX;
7781
let mut min_y = std::f32::MAX;
82+
let mut max_y = std::f32::MIN;
7883
for sg in &glyphs {
7984
let glyph = &sg.glyph;
8085

8186
let scaled_font = sections_data[sg.section_index].3;
8287
min_x = min_x.min(glyph.position.x);
8388
min_y = min_y.min(glyph.position.y - scaled_font.ascent());
89+
max_y = max_y.max(glyph.position.y - scaled_font.descent());
8490
}
8591
min_x = min_x.floor();
8692
min_y = min_y.floor();
93+
max_y = max_y.floor();
8794

8895
let mut positioned_glyphs = Vec::new();
8996
for sg in glyphs {
@@ -120,7 +127,12 @@ impl GlyphBrush {
120127
let size = Vec2::new(glyph_rect.width(), glyph_rect.height());
121128

122129
let x = bounds.min.x + size.x / 2.0 - min_x;
123-
let y = bounds.min.y + size.y / 2.0 - min_y;
130+
131+
let y = match y_axis_orientation {
132+
YAxisOrientation::BottomToTop => max_y - bounds.max.y + size.y / 2.0,
133+
YAxisOrientation::TopToBottom => bounds.min.y + size.y / 2.0 - min_y,
134+
};
135+
124136
let position = adjust.position(Vec2::new(x, y));
125137

126138
positioned_glyphs.push(PositionedGlyph {

crates/bevy_text/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ impl Default for TextSettings {
5656
}
5757
}
5858

59+
/// Text is rendered for two different view projections, normal `Text2DBundle` is rendered with a
60+
/// `BottomToTop` y axis, and UI is rendered with a `TopToBottom` y axis. This matters for text because
61+
/// the glyph positioning is different in either layout.
62+
pub enum YAxisOrientation {
63+
TopToBottom,
64+
BottomToTop,
65+
}
66+
5967
impl Plugin for TextPlugin {
6068
fn build(&self, app: &mut App) {
6169
app.add_asset::<Font>()

crates/bevy_text/src/pipeline.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use glyph_brush_layout::{FontId, SectionText};
1111

1212
use crate::{
1313
error::TextError, glyph_brush::GlyphBrush, scale_value, Font, FontAtlasSet, PositionedGlyph,
14-
TextAlignment, TextSection, TextSettings,
14+
TextAlignment, TextSection, TextSettings, YAxisOrientation,
1515
};
1616

1717
#[derive(Default, Resource)]
@@ -50,6 +50,7 @@ impl TextPipeline {
5050
texture_atlases: &mut Assets<TextureAtlas>,
5151
textures: &mut Assets<Image>,
5252
text_settings: &TextSettings,
53+
y_axis_orientation: YAxisOrientation,
5354
) -> Result<TextLayoutInfo, TextError> {
5455
let mut scaled_fonts = Vec::new();
5556
let sections = sections
@@ -105,6 +106,7 @@ impl TextPipeline {
105106
texture_atlases,
106107
textures,
107108
text_settings,
109+
y_axis_orientation,
108110
)?;
109111

110112
Ok(TextLayoutInfo { glyphs, size })

crates/bevy_text/src/text2d.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use bevy_window::{WindowId, WindowScaleFactorChanged, Windows};
2323

2424
use crate::{
2525
Font, FontAtlasSet, HorizontalAlign, Text, TextError, TextLayoutInfo, TextPipeline,
26-
TextSettings, VerticalAlign,
26+
TextSettings, VerticalAlign, YAxisOrientation,
2727
};
2828

2929
/// The calculated size of text drawn in 2D scene.
@@ -182,6 +182,7 @@ pub fn update_text2d_layout(
182182
),
183183
None => Vec2::new(f32::MAX, f32::MAX),
184184
};
185+
185186
match text_pipeline.queue_text(
186187
&fonts,
187188
&text.sections,
@@ -192,6 +193,7 @@ pub fn update_text2d_layout(
192193
&mut *texture_atlases,
193194
&mut *textures,
194195
text_settings.as_ref(),
196+
YAxisOrientation::BottomToTop,
195197
) {
196198
Err(TextError::NoSuchFont) => {
197199
// There was an error processing the text layout, let's add this entity to the

crates/bevy_ui/src/widget/text.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use bevy_ecs::{
88
use bevy_math::Vec2;
99
use bevy_render::texture::Image;
1010
use bevy_sprite::TextureAtlas;
11-
use bevy_text::{Font, FontAtlasSet, Text, TextError, TextLayoutInfo, TextPipeline, TextSettings};
11+
use bevy_text::{
12+
Font, FontAtlasSet, Text, TextError, TextLayoutInfo, TextPipeline, TextSettings,
13+
YAxisOrientation,
14+
};
1215
use bevy_window::Windows;
1316

1417
#[derive(Debug, Default)]
@@ -118,6 +121,7 @@ pub fn text_system(
118121
&mut *texture_atlases,
119122
&mut *textures,
120123
text_settings.as_ref(),
124+
YAxisOrientation::TopToBottom,
121125
) {
122126
Err(TextError::NoSuchFont) => {
123127
// There was an error processing the text layout, let's add this entity to the

examples/2d/text2d.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ fn main() {
1919

2020
#[derive(Component)]
2121
struct AnimateTranslation;
22+
2223
#[derive(Component)]
2324
struct AnimateRotation;
25+
2426
#[derive(Component)]
2527
struct AnimateScale;
2628

0 commit comments

Comments
 (0)