diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index f8e7274ef3..5f547cf45f 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -193,6 +193,14 @@ impl TextPipeline { } buffer.shape_until_scroll(font_system, false); + // Workaround for alignment not working for unbounded text. + // See https://github.com/pop-os/cosmic-text/issues/343 + if bounds.width.is_none() && justify != JustifyText::Left { + let dimensions = buffer_dimensions(buffer); + // `set_size` causes a re-layout to occur. + buffer.set_size(font_system, Some(dimensions.x), bounds.height); + } + // Recover the spans buffer. spans.clear(); self.spans_buffer = spans diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index 56c2d3dc63..d5092400eb 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -111,10 +111,11 @@ fn setup(mut commands: Commands, asset_server: Res) { // Demonstrate font smoothing off commands.spawn(( - Text2d::new("FontSmoothing::None"), + Text2d::new("This text has\nFontSmoothing::None\nAnd JustifyText::Center"), slightly_smaller_text_font .clone() .with_font_smoothing(FontSmoothing::None), + TextLayout::new_with_justify(JustifyText::Center), Transform::from_translation(Vec3::new(-400.0, -250.0, 0.0)), ));