bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.38k stars 3.59k forks source link

Support line-height / letter-spacing in bevy_text #8781

Open mattzque opened 1 year ago

mattzque commented 1 year ago

What problem does this solve or what need does it fill?

There seems to be no way to adjust the line spacing and letter spacing / glyph spacing. To make text more readable I'd like to increase the line height to 1.5, which is typically used to make longer paragraphs more readable.

What solution would you like?

Something similar to line-height(mdn) and letter-spacing (mdn) in CSS would be great. Ideally it should take the font size into account instead of just an absolute (logical) pixel value, but any way to adjust it would already be great. Letter spacing would most likely be more challenging than line height, since it might affect line breaking and should be calculated before the line breaking occurs, but since they seem related I thought I make an issue about both.

What alternative(s) have you considered?

I don't see any way to achieve this myself since TextSection already implements line breaking I think it should handle this. But perhaps there is a way to do this already?

Additional context

I believe the glyph positioning is calculated here: https://github.com/bevyengine/bevy/blob/main/crates/bevy_text/src/glyph_brush.rs#L34 using the glyph_brush crate: https://github.com/alexheretic/glyph-brush/blob/main/layout/src/builtin.rs#L135 There is also a related issue I found: https://github.com/alexheretic/glyph-brush/issues/132

I'm not sure if this is something we can adjust in bevy, given the calculated glyph information provided by glyph-brush or if this has to be added upstream first.

ickshonpe commented 1 year ago

It's not hard at all to implement a basic version that just adds absolute space between lines.

I'm not quite sure what to do about a version that takes font size into account because a TextBundle can have multiple fonts. Perhaps it should just use the size of the largest font in the TextBundle.

Character spacing shouldn't be difficult either but might be better left until after the switch to Cosmic Text.

ickshonpe commented 1 year ago

Just looked and Cosmic Text has built-in support for line-height. So this is something that should be included in Bevy 0.12.

arendjr commented 1 year ago

I just noticed #7616 mentions implementing support for line height, which would be great. Hopefully letter-spacing can follow soon after, since it's practically impossible to implement designs using custom letter-spacing now.