Skip to content

Commit fcc6b4d

Browse files
rparrettmockersf
authored andcommitted
Fix text alignment for unbounded text (#17270)
Fixes #16783 Works around a `cosmic-text` bug or limitation by triggering a re-layout with the calculated width from the first layout run. See linked issue. Credit to @ickshonpe for the clever solution. This has a significant performance impact only on unbounded text that are not `JustifyText::Left`, which is still a bit of a bummer because text2d performance in 0.15.1 is already not great. But this seems better than alignment not working. ||many_text2d nfc re|many_text2d nfc re center| |-|-|-| |unbounded-layout-no-fix|3.06|3.10| |unbounded-layout-fix|3.05 ⬜ -0.2%|2.71 🟥 -12.5%| I added a centered text to the `text2d` example. `cargo run --example text2d` We should look at other text examples and stress tests. I haven't tested as thoroughly as I would like, so help testing that this doesn't break something in UI would be appreciated.
1 parent 835f572 commit fcc6b4d

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

crates/bevy_text/src/pipeline.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ impl TextPipeline {
193193
}
194194
buffer.shape_until_scroll(font_system, false);
195195

196+
// Workaround for alignment not working for unbounded text.
197+
// See https://github.com/pop-os/cosmic-text/issues/343
198+
if bounds.width.is_none() && justify != JustifyText::Left {
199+
let dimensions = buffer_dimensions(buffer);
200+
// `set_size` causes a re-layout to occur.
201+
buffer.set_size(font_system, Some(dimensions.x), bounds.height);
202+
}
203+
196204
// Recover the spans buffer.
197205
spans.clear();
198206
self.spans_buffer = spans

examples/2d/text2d.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
111111

112112
// Demonstrate font smoothing off
113113
commands.spawn((
114-
Text2d::new("FontSmoothing::None"),
114+
Text2d::new("This text has\nFontSmoothing::None\nAnd JustifyText::Center"),
115115
slightly_smaller_text_font
116116
.clone()
117117
.with_font_smoothing(FontSmoothing::None),
118+
TextLayout::new_with_justify(JustifyText::Center),
118119
Transform::from_translation(Vec3::new(-400.0, -250.0, 0.0)),
119120
));
120121

0 commit comments

Comments
 (0)