From c454af8d9648bd76658884d35b39e684624fc6b4 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 5 Oct 2026 17:47:20 -0400 Subject: [PATCH] Text: deterministic font family order; hide private system families (fixes a flaky test) (#264) (#264) --- crates/text/src/fonts.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/text/src/fonts.rs b/crates/text/src/fonts.rs index 883bb8272b..5198f072ba 100644 --- a/crates/text/src/fonts.rs +++ b/crates/text/src/fonts.rs @@ -143,6 +143,10 @@ impl FontDb { names.push(n.to_string()); } } + // Deterministic order, user-facing families first: a collection's registration order isn't + // stable, and macOS ships private UI faces (".Hiragino Kaku Gothic Interface") whose + // metrics differ from the public family's. + names.sort_by_key(|n| (is_hidden_family(n), n.to_lowercase())); self.ps_cache.clear(); self.refresh_generics(); names @@ -175,7 +179,8 @@ impl FontDb { /// All family names, sorted. pub fn families(&mut self) -> Vec { - let mut v: Vec = self.fcx.collection.family_names().map(str::to_string).collect(); + // Private system faces (a leading '.', e.g. macOS ".SF NS") are hidden, as in Photoshop. + let mut v: Vec = self.fcx.collection.family_names().filter(|n| !is_hidden_family(n)).map(str::to_string).collect(); v.sort_by_key(|s| s.to_lowercase()); v.dedup(); v @@ -359,8 +364,23 @@ fn system_font_dirs() -> Vec { v } +/// A platform-private family (macOS names its UI faces with a leading '.'): never listed or +/// picked by default. +pub fn is_hidden_family(name: &str) -> bool { + name.starts_with('.') +} + #[cfg(all(test, not(target_arch = "wasm32")))] mod tests { + #[test] + fn hidden_families_sort_last_and_are_not_listed() { + assert!(super::is_hidden_family(".Hiragino Kaku Gothic Interface")); + assert!(!super::is_hidden_family("Hiragino Sans")); + let mut v = [".Hiragino Kaku Gothic Interface".to_string(), "Hiragino Kaku Gothic ProN".to_string()]; + v.sort_by_key(|n| (super::is_hidden_family(n), n.to_lowercase())); + assert_eq!(v[0], "Hiragino Kaku Gothic ProN"); + } + #[test] #[cfg(target_os = "linux")] fn scans_flatpak_host_fonts() {