Allow loading "Segoe Fluent Icons" font on macOS (#9421)

This PR updates the `TextSystem` on macOS to allow loading the "Segoe
Fluent Icons" font.

We're using this font in the Storybook to render the `TitleBar` as it
would appear on Windows despite us running it on macOS. This is to make
things easier for iterating on UI design without needing to test on each
individual platform.

However, the "Segoe Fluent Icons" font does not have a glyph for the `m`
character, causing it to run afoul of a precautionary check added in
#4029, which ultimately results in the font not being loaded (and thus
rendering as a missing glyph).

We work around this by simply ignoring this check if the font we're
trying to load is specifically "Segoe Fluent Icons".

I think longer-term we'll need to revisit the behavior in the editor
that is causing the panics when the `m` glyph is missing from the font,
but that's a problem for a different day.

#### Before

<img width="1283" alt="Screenshot 2024-03-15 at 3 34 38 PM"
src="https://github.com/zed-industries/zed/assets/1486634/c0ddd46d-8599-4729-ac98-75522b33e25b">

#### After

<img width="1113" alt="Screenshot 2024-03-15 at 5 12 36 PM"
src="https://github.com/zed-industries/zed/assets/1486634/183c2b43-5e4f-4516-8856-7a2d45ed8b2e">

Note that you currently need to install the "Segoe Fluent Icons" font
yourself—either installing it globally or placing the `.ttf` file in the
`assets/fonts` directory—in order to see the icons rendered. I'd like to
look into getting this, but there are restrictions on the distribution
of the font on non-Windows platforms that will need to be followed.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-03-15 17:35:10 -04:00 committed by GitHub
parent 41071b093c
commit 0329b4a5cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 5 deletions

View file

@ -233,9 +233,34 @@ impl MacTextSystemState {
let mut font = font.load()?;
open_type::apply_features(&mut font, features);
let Some(_) = font.glyph_for_char('m') else {
continue;
};
// This block contains a precautionary fix to guard against loading fonts
// that might cause panics due to `.unwrap()`s up the chain.
{
// We use the 'm' character for text measurements in various spots
// (e.g., the editor). However, at time of writing some of those usages
// will panic if the font has no 'm' glyph.
//
// Therefore, we check up front that the font has the necessary glyph.
let has_m_glyph = font.glyph_for_char('m').is_some();
// HACK: The 'Segoe Fluent Icons' font does not have an 'm' glyph,
// but we need to be able to load it for rendering Windows icons in
// the Storybook (on macOS).
let is_segoe_fluent_icons = font.full_name() == "Segoe Fluent Icons";
if !has_m_glyph && !is_segoe_fluent_icons {
// I spent far too long trying to track down why a font missing the 'm'
// character wasn't loading. This log statement will hopefully save
// someone else from suffering the same fate.
log::warn!(
"font '{}' has no 'm' character and was not loaded",
font.full_name()
);
continue;
}
}
// We've seen a number of panics in production caused by calling font.properties()
// which unwraps a downcast to CFNumber. This is an attempt to avoid the panic,
// and to try and identify the incalcitrant font.