gpui: Add helper methods for em width and em advance (#24036)

This PR adds two helpers methods to the `TextSystem`:

- `em_width`
- `em_advance`

These methods return the width and advance width for an `em`,
respectively.

We were using these definitions in a number of different spots, and by
unifying them we better canonicalize that an `em` is based on the `m`
character.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2025-01-31 12:47:44 -05:00 committed by GitHub
parent af6548c745
commit 990bdde5e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 47 deletions

View file

@ -195,6 +195,20 @@ impl TextSystem {
Ok(result * font_size)
}
/// Returns the width of an `em`.
///
/// Uses the width of the `m` character in the given font and size.
pub fn em_width(&self, font_id: FontId, font_size: Pixels) -> Result<Pixels> {
Ok(self.typographic_bounds(font_id, font_size, 'm')?.size.width)
}
/// Returns the advance width of an `em`.
///
/// Uses the advance width of the `m` character in the given font and size.
pub fn em_advance(&self, font_id: FontId, font_size: Pixels) -> Result<Pixels> {
Ok(self.advance(font_id, font_size, 'm')?.width)
}
/// Get the number of font size units per 'em square',
/// Per MDN: "an abstract square whose height is the intended distance between
/// lines of type in the same type size"