Skip to content

Text background colors #18892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented Apr 21, 2025

Objective

Add background colors for text.

Fixes #18889

Solution

New component TextBackgroundColor, add it to any UI Text or TextSpan entity to add a background color to its text.
New field on TextLayoutInfo section_rects holds the list of bounding rects for each text section.

The bounding rects are generated in TextPipeline::queue_text during text layout, extract_text_background_colors extracts the colored background rects for rendering.

Didn't include Text2d support because of z-order issues.

The section rects can also be used to implement interactions targeting individual text sections.

Testing

Includes a basic example that can be used for testing:

cargo run --example text_background_colors

Showcase

tbcm

Using a proportional font with kerning the results aren't so tidy (since the bounds of adjacent glyphs can overlap) but it still works fine:

tbc

@ickshonpe ickshonpe added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen A-Accessibility A problem that prevents users with disabilities from using Bevy A-UI Graphical user interfaces, styles, layouts, and widgets S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 21, 2025
Copy link
Contributor

@Olle-Lukowski Olle-Lukowski left a comment

Choose a reason for hiding this comment

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

Other than my one comment this looks great!

Copy link
Contributor

@Henauxg Henauxg left a comment

Choose a reason for hiding this comment

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

I'm not very familiar with render entity extraction so my review of extract_text_background_colors was pretty basic.

In queue_text, we could optionally move the computation of section_rects in the already existing main glyphs loop to avoid having a second linear scan of all the glyphs.

@ickshonpe ickshonpe 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 Apr 26, 2025
@ickshonpe ickshonpe added the M-Needs-Release-Note Work that should be called out in the blog due to impact label Apr 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Accessibility A problem that prevents users with disabilities from using Bevy A-Rendering Drawing game state to the screen A-UI Graphical user interfaces, styles, layouts, and widgets C-Feature A new feature, making something new possible M-Needs-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

TextBackgroundColors
3 participants