Initial GPUI Wayland terminal with Alacritty core
This commit is contained in:
+141
@@ -0,0 +1,141 @@
|
||||
//! 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",
|
||||
];
|
||||
|
||||
pub fn terminal_font(text_system: &TextSystem) -> Font {
|
||||
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}");
|
||||
font(family.to_owned())
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user