Add character "hitbox" to `PositionedGlyph`
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## What problem does this solve or what need does it fill?
The way users currently retrieve information about glyphs in a text layout is through `PositionedGlyph` via `TextLayoutInfo`.
```rust
pub struct TextLayoutInfo {
pub glyphs: Vec,
pub size: Vec2,
}
pub struct PositionedGlyph {
pub position: Vec2,
pub size: Vec2,
pub atlas_info: GlyphAtlasInfo,
pub span_index: usize,
byte_index: usize,
}
```
At a glance, this looks like a lot of useful information for a user who wants to implement a text input or text picking.
However, `position` and `size` refer to the position and size of "the glyph texture", which is not the same as the position and size of "the character."
Here's a *rough* illustration with the string `A. bc `. Blue is the bounds of the entire text. Pink is the `size` indicated in `PositionedGlyph`, and cyan is the `position`.

I am not sure if these particular bounds are useful information for users. In fact, I don't believe that `PositionedGlyph::size` is even used anywhere in **Bevy**. That information is already encoded in the texture atlas.
The data for space characters are particularly not useful, having a width of `2px` and a height of `0px`.
It's sort of possible to infer the hitbox through the `x` position of a glyph and the next glyph. But this breaks down at the last glyph where you need to then need to use the overall text bounds or something.
## What solution would you like?
Practically, I want to be able to position a cursor properly within some text, or to determine what glyph has been hit by a particular point.
I think that we should take a proper inventory of what information Bevy and Users need about the positioning and size of glyphs, what we already provide, and what we might want to change.
Based on my current understanding, I think that we should replace `size` with minimum and maximum bounds for a character.
## What alternative(s) have you considered?
Maybe we just want better documentation about what these values are.
It's possible that my analysis is incorrect, I may have missed a use-case or misunderstood something.
## Additional context
I think that this information might be available by way of the cosmic buffer (made accessible recently by #17748), but if so, retrieving it seems pretty tedious and inefficient.
It's also possible that the "hit test" use-case is also covered by making the cosmic buffer public by way of its `hit` method.
Contributor guide
Research direction
Start with PositionedGlyph and TextLayoutInfo, then inspect the cosmic buffer made accessible by #17748 and its hit method. Determine which positioning and size data Bevy and users need for cursor placement or glyph hit testing, and compare exposing character bounds with documenting the existing values. Done means the issue's chosen approach provides a reliable character hitbox, including for the last glyph and spaces.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- game-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100