From e2916fbad101e98c51a9b924dc48845a6b643fa9 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Fri, 2 Feb 2024 20:01:01 +0000 Subject: [PATCH] Subtract 1 from text positions to account for glyph texture padding. (#11662) # Objective Glyph positions don't account for padding added to the font texture atlas, resulting in them being off by one physical pixel in both axis. ## Example ```rust use bevy::{ prelude::*, window::WindowResolution }; fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { resolution: WindowResolution::default().with_scale_factor_override(1.), ..Default::default() }), ..Default::default() })) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2dBundle::default()); commands.spawn( TextBundle::from_section( "QQQQQ", TextStyle { font: asset_server.load("FiraMono-Medium.ttf"), font_size: 14.0, ..default() }, ) .with_style(Style { left:Val::Px(10.), top: Val::Px(10.), ..default() }) .with_background_color(Color::RED) ); } ``` QQQQQ-bad The coordinates are off by one in physical coordinates, not logical. So the difference only becomes obvious with `UiScale` and the window scale factor set to low values. ## Solution Translate glyph positions by -1 in both axes. QQQQQ-good --- ## Changelog * Translate the positions for each glyph by -1 in both axes in `bevy_text::glyph_brush::process_glyphs` --------- Co-authored-by: Alice Cecile --- crates/bevy_text/src/glyph_brush.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/bevy_text/src/glyph_brush.rs b/crates/bevy_text/src/glyph_brush.rs index f49211d610..f75a225b08 100644 --- a/crates/bevy_text/src/glyph_brush.rs +++ b/crates/bevy_text/src/glyph_brush.rs @@ -136,7 +136,9 @@ impl GlyphBrush { } }; - let position = adjust.position(Vec2::new(x, y)); + // We must offset by 1 to account for glyph texture padding. + // See https://github.com/bevyengine/bevy/pull/11662 + let position = adjust.position(Vec2::new(x, y) - 1.); positioned_glyphs.push(PositionedGlyph { position,