2026-11-17 00:12:24 -08:00
|
|
|
//! Select a concrete fixed-pitch family; GPUI does not resolve CSS font aliases.
|
|
|
|
|
|
|
|
|
|
use gpui::{Font, TextSystem, font, px};
|
|
|
|
|
|
|
|
|
|
// Nerd Fonts publishes its patched Hermit under the name "Hurmit".
|
|
|
|
|
const PREFERRED: &[&str] = &[
|
|
|
|
|
"Hermit",
|
|
|
|
|
"Hurmit Nerd Font Mono",
|
|
|
|
|
"Hurmit Nerd Font",
|
|
|
|
|
"Hurmit NF",
|
|
|
|
|
"SF Mono",
|
|
|
|
|
"Noto Sans Mono",
|
|
|
|
|
"Iosevka",
|
|
|
|
|
"Iosevka Fixed",
|
|
|
|
|
"Iosevka Term",
|
|
|
|
|
];
|
|
|
|
|
|
2026-11-17 14:49:35 -08:00
|
|
|
const DEFAULT_SIZE: f32 = 15.;
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct TerminalFont {
|
|
|
|
|
pub face: Font,
|
|
|
|
|
pub size: f32,
|
|
|
|
|
pub cell_width: f32,
|
|
|
|
|
pub line_height: f32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TerminalFont {
|
|
|
|
|
fn new(face: Font, size: f32, text_system: &TextSystem) -> Self {
|
|
|
|
|
let cell_width = text_system
|
|
|
|
|
.advance(text_system.resolve_font(&face), px(size), 'M')
|
|
|
|
|
.map(|advance| f32::from(advance.width))
|
|
|
|
|
.unwrap_or(size * 0.6)
|
|
|
|
|
.max(1.);
|
|
|
|
|
Self {
|
|
|
|
|
face,
|
|
|
|
|
size,
|
|
|
|
|
cell_width,
|
2026-11-17 18:12:04 -08:00
|
|
|
line_height: (size * 1.8).round(),
|
2026-11-17 14:49:35 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn adjust_size(&mut self, delta: f32, text_system: &TextSystem) -> bool {
|
|
|
|
|
let size = (self.size + delta).clamp(6., 72.);
|
|
|
|
|
if size == self.size {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*self = Self::new(self.face.clone(), size, text_system);
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn terminal_font(text_system: &TextSystem) -> TerminalFont {
|
2026-11-17 00:12:24 -08:00
|
|
|
let families = text_system.all_font_names();
|
|
|
|
|
let family = select_family(&families, |family| is_monospace(text_system, family)).expect(
|
|
|
|
|
"No usable monospace font found; install Hermit, Noto Sans Mono, or DejaVu Sans Mono",
|
|
|
|
|
);
|
|
|
|
|
log::info!("Terminal font: {family}");
|
2026-11-17 14:49:35 -08:00
|
|
|
TerminalFont::new(font(family.to_owned()), DEFAULT_SIZE, text_system)
|
2026-11-17 00:12:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn select_family(families: &[String], mut is_monospace: impl FnMut(&str) -> bool) -> Option<&str> {
|
|
|
|
|
for preferred in PREFERRED {
|
|
|
|
|
if let Some(family) = families
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|family| family.eq_ignore_ascii_case(preferred))
|
|
|
|
|
&& is_monospace(family)
|
|
|
|
|
{
|
|
|
|
|
return Some(family);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Font discovery order varies by platform. Try likely fixed-pitch names
|
|
|
|
|
// first, then inspect the remaining families, including names such as
|
|
|
|
|
// "Inconsolata" that contain neither "Mono" nor "Fixed".
|
|
|
|
|
let mut fallback: Vec<_> = families
|
|
|
|
|
.iter()
|
|
|
|
|
.map(String::as_str)
|
|
|
|
|
.filter(|name| !name.starts_with('.') && !name.eq_ignore_ascii_case("monospace"))
|
|
|
|
|
.filter(|name| {
|
|
|
|
|
!PREFERRED
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|preferred| name.eq_ignore_ascii_case(preferred))
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
fallback.sort_by_cached_key(|name| {
|
|
|
|
|
let lower = name.to_lowercase();
|
|
|
|
|
let likely = lower.contains("mono") || lower.contains("fixed");
|
|
|
|
|
(!likely, lower)
|
|
|
|
|
});
|
|
|
|
|
fallback.into_iter().find(|name| is_monospace(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_monospace(text_system: &TextSystem, family: &str) -> bool {
|
|
|
|
|
let id = text_system.resolve_font(&font(family.to_owned()));
|
|
|
|
|
// all_font_names also includes GPUI's fallback names, which might not be
|
|
|
|
|
// installed. Reject a request that resolved to a different family.
|
|
|
|
|
if !text_system
|
|
|
|
|
.get_font_for_id(id)
|
|
|
|
|
.is_some_and(|resolved| resolved.family.as_ref().eq_ignore_ascii_case(family))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
let Ok(reference) = text_system.advance(id, px(15.), 'M') else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
let reference = f32::from(reference.width);
|
|
|
|
|
if !reference.is_finite() || reference <= 0. {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Checking all printable ASCII catches proportional fonts with tabular
|
|
|
|
|
// digits as well as fonts whose names misleadingly contain "Mono".
|
|
|
|
|
(' '..='~').all(|character| {
|
|
|
|
|
text_system
|
|
|
|
|
.advance(id, px(15.), character)
|
|
|
|
|
.is_ok_and(|advance| (f32::from(advance.width) - reference).abs() < 0.01)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
fn families(names: &[&str]) -> Vec<String> {
|
|
|
|
|
names.iter().map(|name| (*name).into()).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn preferred_order_is_independent_of_discovery_order() {
|
|
|
|
|
let mut installed = families(&["Iosevka", "Noto Sans Mono", "SF Mono", "Hermit"]);
|
|
|
|
|
for expected in ["Hermit", "SF Mono", "Noto Sans Mono", "Iosevka"] {
|
|
|
|
|
assert_eq!(select_family(&installed, |_| true), Some(expected));
|
|
|
|
|
installed.retain(|name| name != expected);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn patched_hermit_has_the_same_preference_as_hermit() {
|
|
|
|
|
let installed = families(&["SF Mono", "Noto Sans Mono", "Hurmit Nerd Font Mono"]);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
select_family(&installed, |_| true),
|
|
|
|
|
Some("Hurmit Nerd Font Mono")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn skips_unusable_preferred_fonts() {
|
|
|
|
|
let installed = families(&["Hermit", "SF Mono", "Iosevka"]);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
select_family(&installed, |name| name != "Hermit"),
|
|
|
|
|
Some("SF Mono")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn fallback_checks_metrics_instead_of_relying_on_names() {
|
|
|
|
|
let installed = families(&[
|
|
|
|
|
"Fake Mono",
|
|
|
|
|
"Arial",
|
|
|
|
|
"Inconsolata",
|
|
|
|
|
"monospace",
|
|
|
|
|
".SystemUIFont",
|
|
|
|
|
]);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
select_family(&installed, |name| name == "Inconsolata"),
|
|
|
|
|
Some("Inconsolata")
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(select_family(&installed, |_| false), None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn synthetic_aliases_are_never_chosen() {
|
|
|
|
|
let installed = families(&["monospace", ".ZedMono", ".SystemUIFont"]);
|
|
|
|
|
assert_eq!(select_family(&installed, |_| true), None);
|
|
|
|
|
}
|
|
|
|
|
}
|