Skip to content

Conversation

@ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented Jan 20, 2026

Objective

Add responsive font sizes supporting rem and viewport units to bevy_text with minimal changes to the APIs and systems.

Solution

Introduce a new FontSize enum:

pub enum FontSize {
    /// Font Size in logical pixels.
    Px(f32),
    /// Font size as a percentage of the viewport width.
    Vw(f32),
    /// Font size as a percentage of the viewport height.
    Vh(f32),
    /// Font size as a percentage of the smaller of the viewport width and height.
    VMin(f32),
    /// Font size as a percentage of the larger of the viewport width and height.
    VMax(f32),
    /// Font Size relative to the value of the `RemSize` resource.
    Rem(f32),
}

This replaces the f32 value of TextFont's font_size field.

The viewport variants work the same way as their respective Val counterparts.

Rem values are multiplied by the value of the RemSize resource (which newtypes an f32).

FontSize provides an eval method that takes a logical viewport size and rem base size and returns an f32 logical font size. The resolved logical font size is then written into the Attributes passed to Cosmic Text by TextPipeline::update_buffer.

Any text implementation using bevy_text must now provide viewport and rem base values when calling TextPipeline::update_buffer or create_measure.

Text2d uses the size of the primary window to resolve viewport values (or Vec2::splat(1000) if no primary window is found). This is a deliberate compromise, a single Text2d can be rendered to multiple viewports using RenderLayers, so it's difficult to find a rule for which viewport size should be chosen.

Limitations

There are some limitations because we don't have any sort of font style inheritance yet:

  • FontSize values are only reevaluated on changes to TextFont atm, not when viewport or rem base values change. This can be observed with the text example. Its "hello bevy" message has font size Vh(20.), but this is only based on the window's initial size. The size of the message is not updated when you change the size of the window. Complete change detection could be implemented without inheritance, but would significantly complicate this PR and the changes would need to rewritten once inheritance is introduced.
  • "rem" units aren't proper rem units, and just based on the value of a resource.
  • "em" units are resolved based on inherited font size, so can't be implemented without inheritance support.

Notes

  • This PR is quite small and not very technical. Reviewers don't need to be especially familiar with bevy_text. Most of the changes are to the examples.

  • We could consider using Val instead of FontSize, that would be more ergonomic but some variants might not have sensible interpretations in both UI and Text2d contexts. Also we'd have to make Val accessible to bevy_text.

Testing

The changes to the text systems are relatively trivial and easy to understand. I already added a minor change to the text example to use Vh font size for the "hello bevy" text in the bottom right corner.

Most of the example migrations were automated using regular expressions, and there are bound to be mistakes in those changes. It's infeasible to check every single example thoroughly, but it's early enough in the release cycle that I don't think we should be too worried if a few bugs slip in.

@ickshonpe ickshonpe added C-Feature A new feature, making something new possible A-UI Graphical user interfaces, styles, layouts, and widgets A-Text Rendering and layout for characters M-Deliberate-Rendering-Change An intentional change to how tests and examples are rendered labels Jan 20, 2026
@alice-i-cecile alice-i-cecile added M-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 20, 2026
@ickshonpe ickshonpe added the D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes label Jan 20, 2026
@mnmaita
Copy link
Member

mnmaita commented Jan 21, 2026

Wondering if this PR supersedes #9524?

@ickshonpe
Copy link
Contributor Author

ickshonpe commented Jan 21, 2026

Wondering if this PR supersedes #9524?

Yeah got a bunch of open PRs that I need to clean up. Closed it.

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-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Deliberate-Rendering-Change An intentional change to how tests and examples are rendered M-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants