Text styles should be inheritable #13511
Labels
A-Text
Rendering and layout for characters
C-Enhancement
A new feature
X-Controversial
There is active debate or serious implications around merging this PR
What problem does this solve or what need does it fill?
In the HTML/CSS standard, certain style properties are inheritable, meaning that if you set the style on a parent element, the style applies to the descendants of that element as well (unless explicitly overridden).
The number of styles that are inheritable is fairly small, and are mostly the ones that deal with text, such as font face, font size, font style, and text color. The reasoning is fairly simple: there are typically many text spans and it's cumbersome and inefficient to have to style each one, especially given the fact that most spans have identical or at least similar properties.
Although games typically have fewer text nodes than a web page, the problem still comes up in practice, especially in character dialogs, help pages and tutorials - but even simple uses of text such as buttons and menus can benefit.
What solution would you like?
The basic properties we want to inherit are:
The basic suggestion is that you'd have a set of standard ECS components: either a single combined component such as
TextStyles
or individual components for the various style properties likeFontWeight
. When preparing text for rendering, it would look up the node hierarchy to see whether or not there were any styles defined on the ancestors of the text node.Having to do an entity ancestor traversal might seem like it might be inefficient, but it should be possible to optimize this, and similar issues have been overcome for things like node transforms and visibility.
There are potential efficiency gains to be considered as well: particularly around font handles. Most games only need one font, and it is far easier for users to be able to configure the font a single time, at the root of the hierarchy, than to have to insert the font handle into every text node. This of course assumes we have also implement font-families and dynamic font selection by style: being able to specify "MyCoolFont" at the root level does little good if you also have to override "MyCoolFont_Italic" every time you have an italic span.
Alternatively, this behavior could be "opt in": users would have to add a special component into their text sections to enable this behavior.
What alternative(s) have you considered?
It's possible to implement this as an external crate, using a system to update the text styles; in fact I have done this. However, it would be better if this was standard Bevy behavior so that everyone could use the same API for specifying text styles.
Additional context
I'm bringing this up now because of the recent Discord discussion around text rendering, but it's something I have been thinking about for a long time.
The text was updated successfully, but these errors were encountered: