Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text2d misalignment fix #14270

Closed
wants to merge 12 commits into from
16 changes: 15 additions & 1 deletion crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy_ecs::{
query::{Changed, Without},
system::{Commands, Local, Query, Res, ResMut},
};
use bevy_math::Vec2;
use bevy_math::{FloatOrd, Vec2};
use bevy_render::{
primitives::Aabb,
texture::Image,
Expand Down Expand Up @@ -206,7 +206,21 @@ pub fn update_text2d_layout(
Err(e @ (TextError::FailedToAddGlyph(_) | TextError::FailedToGetGlyphImage(_))) => {
panic!("Fatal error when processing text: {e}.");
}

Ok(()) => {
// Translate all the glyphs horizontally so that the left edge of the leftmost glyph is at 0.
// This a temporary fix for layout issues with the `Text2d` API (https://github.com/bevyengine/bevy/issues/14266)
if let Some(min_x) = text_layout_info
.glyphs
.iter()
.map(|glyph| FloatOrd(glyph.position.x - 0.5 * glyph.size.x))
.min()
{
for glyph in text_layout_info.glyphs.iter_mut() {
glyph.position.x -= min_x.0;
}
}

text_layout_info.size.x =
scale_value(text_layout_info.size.x, inverse_scale_factor);
text_layout_info.size.y =
Expand Down