mirror of
https://github.com/bevyengine/bevy
synced 2024-12-04 10:29:14 +00:00
c7ca7dd225
# Objective In both Text2d and Bevy UI text because of incorrect text size and alignment calculations if a block of text has empty leading lines then those lines are ignored. Also, depending on the font size when leading empty lines are ignored the same number of lines of text can go missing from the bottom of the text block. ## Example (from murtaugh on discord) ```rust use bevy::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands) { commands.spawn(Camera2dBundle::default()); let text = "\nfirst line\nsecond line\nthird line\n"; commands.spawn(TextBundle { text: Text::from_section( text.to_string(), TextStyle { font_size: 60.0, color: Color::YELLOW, ..Default::default() }, ), style: Style { position_type: PositionType::Absolute, ..Default::default() }, background_color: BackgroundColor(Color::RED), ..Default::default() }); } ``` ![](https://cdn.discordapp.com/attachments/1128294384954257499/1128295142072254525/image.png) ## Solution `TextPipeline::queue_text`, `TextMeasureInfo::compute_size_from_section_texts` and `GlyphBrush::process_glyphs` each have a nearly duplicate section of code that calculates the minimum bounds around a list of text sections. The first two functions don't apply any rounding, but `process_glyphs` also floors all the values. It seems like this difference can cause conflicts where the text gets incorrectly shaped. Also when Bevy computes the text bounds it chooses the smallest possible rect that fits all the glyphs, ignoring white space. The glyphs are then realigned vertically so the first glyph is on the top line. Any empty leading lines are missed. This PR adds a function `compute_text_bounds` that replaces the duplicate code, so the text bounds are rounded the same way by each function. Also, since Bevy doesn't use `ab_glyph` to control vertical alignment, the minimum y bound is just always set to 0 which ensures no leading empty lines will be missed. There is another problem in that trailing empty lines are also ignored, but that's more difficult to deal with and much less important than the other issues, so I'll leave it for another PR. <img width="462" alt="fixed_text_align_bounds" src="https://github.com/bevyengine/bevy/assets/27962798/85e32e2c-d68f-4677-8e87-38e27ade4487"> --- ## Changelog Added a new function `compute_text_bounds` to the `glyph_brush` module that replaces the text size and bounds calculations in `TextPipeline::queue_text`, `TextMeasureInfo::compute_size_from_section_texts` and `GlyphBrush::process_glyphs`. The text bounds are calculated identically in each function and the minimum y bound is not derived from the glyphs but is always set to 0. |
||
---|---|---|
.. | ||
src | ||
Cargo.toml |