Skip to content

Fix text alignment for unbounded text#17270

Merged
alice-i-cecile merged 3 commits intobevyengine:mainfrom
rparrett:unbounded-layout-fix
Jan 11, 2025
Merged

Fix text alignment for unbounded text#17270
alice-i-cecile merged 3 commits intobevyengine:mainfrom
rparrett:unbounded-layout-fix

Conversation

@rparrett
Copy link
Copy Markdown
Contributor

@rparrett rparrett commented Jan 9, 2025

Objective

Fixes #16783

Solution

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.

Performance

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%

Testing

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.

@rparrett rparrett changed the title Unbounded layout fix Fix text alignment for unbounded text Jan 9, 2025
@rparrett rparrett added this to the 0.15.2 milestone Jan 9, 2025
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-UI Graphical user interfaces, styles, layouts, and widgets A-Text Rendering and layout for characters S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 9, 2025
Copy link
Copy Markdown
Contributor

@ickshonpe ickshonpe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a check in update_buffer is exactly what I was thinking of. It's a very simple change and shouldn't affect UI text at all since UI text is always bounded by the size of its UI node.

I don't think we can be fussy about the performance regression, it's only during recomputation and users can manually add TextBounds themselves for anything performance critical.

My other suggestion to do a post-layout relayout and recalculate interaction coords would be a lot more involved and much more complicated and fragile. It isn't worth it, we'd be better off contributing the time to finding an upstream solution in cosmic-text.

Wouldn't expect to see any problems but I checked out all the text examples to be safe and didn't see anything wrong.

@rparrett rparrett added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 10, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 11, 2025
Merged via the queue into bevyengine:main with commit 5c0e13f Jan 11, 2025
mrchantey pushed a commit to mrchantey/bevy that referenced this pull request Feb 4, 2025
# Objective

Fixes bevyengine#16783

## Solution

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.

## Performance

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%|

## Testing

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.
mockersf pushed a commit that referenced this pull request Feb 6, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Text Rendering and layout for characters A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text2d and TextSpan is not center-aligned when JustifyText::Center is specified.

4 participants