Skip to content

Commit

Permalink
Fixes incorrect glyph positioning for text2d
Browse files Browse the repository at this point in the history
  • Loading branch information
mahulst committed Oct 16, 2022
1 parent 92ba622 commit 244e1dc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
14 changes: 12 additions & 2 deletions crates/bevy_text/src/glyph_brush.rs
Expand Up @@ -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<FontArc>,
Expand Down Expand Up @@ -53,6 +55,7 @@ impl GlyphBrush {
texture_atlases: &mut Assets<TextureAtlas>,
textures: &mut Assets<Image>,
text_settings: &TextSettings,
text_type: TextType,
) -> Result<Vec<PositionedGlyph>, TextError> {
if glyphs.is_empty() {
return Ok(Vec::new());
Expand All @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_text/src/lib.rs
Expand Up @@ -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::<Font>()
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_text/src/pipeline.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -50,6 +50,7 @@ impl TextPipeline {
texture_atlases: &mut Assets<TextureAtlas>,
textures: &mut Assets<Image>,
text_settings: &TextSettings,
text_type: TextType,
) -> Result<TextLayoutInfo, TextError> {
let mut scaled_fonts = Vec::new();
let sections = sections
Expand Down Expand Up @@ -105,6 +106,7 @@ impl TextPipeline {
texture_atlases,
textures,
text_settings,
text_type,
)?;

Ok(TextLayoutInfo { glyphs, size })
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_text/src/text2d.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -182,6 +182,7 @@ pub fn update_text2d_layout(
),
None => Vec2::new(f32::MAX, f32::MAX),
};

match text_pipeline.queue_text(
&fonts,
&text.sections,
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_ui/src/widget/text.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/2d/text2d.rs
Expand Up @@ -19,8 +19,10 @@ fn main() {

#[derive(Component)]
struct AnimateTranslation;

#[derive(Component)]
struct AnimateRotation;

#[derive(Component)]
struct AnimateScale;

Expand Down

0 comments on commit 244e1dc

Please sign in to comment.