From ea0eec98a3cd238e97505a0bb7c978330f4f9791 Mon Sep 17 00:00:00 2001 From: Brian Torres Date: Fri, 16 Oct 2026 08:42:52 -0700 Subject: [PATCH] macOS: list fonts activated outside the font folders (#6683) The installed-font scan read only /System/Library/Fonts, /Library/Fonts and ~/Library/Fonts (plus PingFang). Fonts macOS offers from anywhere else, such as those a font manager, Adobe Fonts or Office activated, never reached the font menus although other apps showed them. load_system_fonts now also registers every file Core Text offers. The list comes from fontique's Core Text backend, enabled for macOS only, used as a scratch collection whose families are discarded: only its file paths are read and registered in our own collection, so generic-family defaults and the merge of installed faces into the bundled families are unchanged. The reserved UI fonts under /System/Library/PrivateFrameworks are skipped (fontique cannot draw their outlines). On a stock macOS install the folder scan already listed all 183 Core Text families; the gap is fonts Core Text knows from elsewhere. With Hack activated from a scratch folder via CTFontManagerRegisterFontsForURL, the list had 313 families before and 314 after (Hack added, none removed), type.fonts lists it and a type layer renders in it. The scan stays 37-57 ms. Tests: font_files_of_lists_each_font_file_once (all platforms); coretext_lists_the_system_font_files and every_family_coretext_offers_is_listed (macOS). The last fails without the call when a font is activated elsewhere ("lacks: Hack") and passes with it. Cargo.lock gains fontique's optional per-platform dependencies for the `system` feature; `cargo tree` shows the feature only for aarch64-apple-darwin. Still open: Windows fonts outside the two folders (#3336) and the web build. Co-Authored-By: Claude Sonnet 5.5 --- Cargo.lock | 38 ++++++++++++-- crates/text/Cargo.toml | 8 +++ crates/text/src/fonts.rs | 106 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aff38ac01b..5d7788208c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1234,7 +1234,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=3d37da389a3618ab9b7610 dependencies = [ "craft-winprint-gdi", "miniz_oxide 0.8.9", - "roxmltree", + "roxmltree 0.20.0", "tempfile", "winprint", "winsafe", @@ -2116,7 +2116,7 @@ version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbc773e24e02d4ddd8395fd30dc147524273a83e54e0f312d986ea30de5f5646" dependencies = [ - "roxmltree", + "roxmltree 0.20.0", ] [[package]] @@ -2141,9 +2141,17 @@ dependencies = [ "hashbrown 0.17.1", "linebender_resource_handle", "memmap2", + "objc2 0.6.4", + "objc2-core-foundation", + "objc2-core-text", + "objc2-foundation 0.3.2", "parlance", "read-fonts", + "roxmltree 0.21.1", "smallvec", + "windows", + "windows-core", + "yeslogic-fontconfig-sys", ] [[package]] @@ -4824,7 +4832,7 @@ dependencies = [ "photocraft-text", "photocraft-vector", "rayon", - "roxmltree", + "roxmltree 0.20.0", "same-file", "serde", "serde_json", @@ -4912,7 +4920,7 @@ dependencies = [ "photocraft-vector", "proptest", "resvg", - "roxmltree", + "roxmltree 0.20.0", "serde_json", "thiserror 2.0.21", ] @@ -5958,6 +5966,15 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" +[[package]] +name = "roxmltree" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1964b10c76125c36f8afe190065a4bf9a87bf324842c05701330bba9f1cacbb" +dependencies = [ + "memchr", +] + [[package]] name = "rustc-hash" version = "1.1.0" @@ -7458,7 +7475,7 @@ dependencies = [ "kurbo 0.11.3", "log", "pico-args", - "roxmltree", + "roxmltree 0.20.0", "rustybuzz", "simplecss", "siphasher", @@ -8704,6 +8721,17 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5a4b21e1a62b67a2970e6831bc091d7b87e119e7f9791aef9702e3bef04448" +[[package]] +name = "yeslogic-fontconfig-sys" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d8b8abf912b9a29ff112e1671c97c33636903d13a69712037190e6805af4f76" +dependencies = [ + "dlib", + "once_cell", + "pkg-config", +] + [[package]] name = "yoke" version = "0.8.3" diff --git a/crates/text/Cargo.toml b/crates/text/Cargo.toml index 985f627e90..f7d4150fa1 100644 --- a/crates/text/Cargo.toml +++ b/crates/text/Cargo.toml @@ -28,6 +28,14 @@ flate2 = { workspace = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] parley = { version = "0.11", default-features = false, features = ["std", "complex-scripts"] } +# macOS only: the `system` feature adds fontique's Core Text backend, which lists every font the +# system offers wherever its file lives (fonts a font manager or Adobe Fonts activated, Office's +# own, Apple's on-demand downloads). `fonts.rs` reads only its list of files and registers them +# like the folder scan's, so our own collection keeps its behaviour (generic families, merging +# installed faces into the bundled families). It uses no fontconfig, unlike `system` on Linux. +[target.'cfg(target_os = "macos")'.dependencies] +parley = { version = "0.11", default-features = false, features = ["std", "complex-scripts", "system"] } + [build-dependencies] # Deflates the bundled fonts (build.rs); `flate2` above inflates them. flate2 = { workspace = true } diff --git a/crates/text/src/fonts.rs b/crates/text/src/fonts.rs index 756ce87455..720d2b77eb 100644 --- a/crates/text/src/fonts.rs +++ b/crates/text/src/fonts.rs @@ -10,6 +10,8 @@ use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, LazyLock}; use parley::FontContext; +#[cfg(any(target_os = "macos", all(test, not(target_arch = "wasm32"))))] +use parley::fontique::SourceKind; use parley::fontique::{Blob, Collection, CollectionOptions, FontInfoOverride, FontStyle, FontWeight, FontWidth, GenericFamily, SourceCache}; use skrifa::raw::{FileRef, TableProvider, types::Tag}; use skrifa::{MetadataProvider, string::StringId}; @@ -254,6 +256,10 @@ impl FontDb { } #[cfg(target_os = "linux")] files.extend(fontconfig_font_paths()); + // Core Text knows every font macOS offers, also those activated from outside the + // font folders (#6683). + #[cfg(target_os = "macos")] + files.extend(coretext_font_paths()); // PingFang (the macOS Chinese UI font) lives outside the font folders. if cfg!(target_os = "macos") { files.extend(crate::cjk::mac_pingfang()); @@ -761,6 +767,49 @@ mod fontconfig_tests { } } +/// The font files Core Text offers: the installed fonts and those activated from any other folder +/// (a font manager, Adobe Fonts, Office, Apple's on-demand downloads). A scratch collection with +/// fontique's Core Text backend lists them; its fonts are not used, only their files, so our own +/// collection registers them like any scanned file. +#[cfg(target_os = "macos")] +fn coretext_font_paths() -> Vec { + // Reading arbitrary fonts must not take the scan down: no files is the folder scan's result. + std::panic::catch_unwind(|| { + let mut system = Collection::new(CollectionOptions { shared: false, system_fonts: true }); + let mut files = font_files_of(&mut system); + files.retain(|f| !f.starts_with(MACOS_PRIVATE_FONTS)); + files + }) + .unwrap_or_default() +} + +/// Fonts reserved for the system UI (PingFangUI.ttc among them). Core Text offers them, but +/// fontique can't draw their outlines (it works around PingFangUI for the same reason), so they +/// are not listed. +#[cfg(target_os = "macos")] +const MACOS_PRIVATE_FONTS: &str = "/System/Library/PrivateFrameworks"; + +/// The files `collection`'s fonts are read from, each once and in a fixed order. Fonts +/// registered from memory have no file. +#[cfg(any(target_os = "macos", all(test, not(target_arch = "wasm32"))))] +fn font_files_of(collection: &mut Collection) -> Vec { + let names: Vec = collection.family_names().map(str::to_string).collect(); + let mut files = HashSet::new(); + for name in names { + let Some(family) = collection.family_by_name(&name) else { + continue; + }; + for font in family.fonts() { + if let SourceKind::Path(path) = font.source().kind() { + files.insert(path.to_path_buf()); + } + } + } + let mut files: Vec<_> = files.into_iter().collect(); + files.sort(); + files +} + #[cfg(not(target_arch = "wasm32"))] pub(crate) fn system_font_dirs() -> Vec { use std::path::PathBuf; @@ -903,6 +952,63 @@ mod tests { assert_eq!(super::localized_name("X", &[]), None); } + /// The files a collection's fonts come from (what Core Text's fonts are loaded from, #6683): + /// each once, none for fonts held in memory. + #[test] + fn font_files_of_lists_each_font_file_once() { + use super::{Blob, Collection, CollectionOptions}; + let dir = std::env::temp_dir().join(format!("photocraft-font-files-{}", std::process::id())); + let activated = dir.join("activated"); + std::fs::create_dir_all(&activated).unwrap(); + let (inter, mono) = (dir.join("inter.ttf"), activated.join("mono.ttf")); + std::fs::write(&inter, super::INTER_REGULAR.as_slice()).unwrap(); + std::fs::write(&mono, super::JETBRAINS_MONO_REGULAR.as_slice()).unwrap(); + let mut collection = Collection::new(CollectionOptions { shared: false, system_fonts: false }); + collection.load_fonts_from_paths([&dir]); + // The same file again and a font from memory add no files. + collection.load_fonts_from_paths([&inter]); + collection.register_fonts(Blob::new(std::sync::Arc::new(super::INTER_MEDIUM.clone())), None); + let files = super::font_files_of(&mut collection); + let _ = std::fs::remove_dir_all(&dir); + let mut expected = vec![inter, mono]; + expected.sort(); + assert_eq!(files, expected); + } + + /// Core Text lists the system's font files, all readable, none of the reserved UI fonts. + #[test] + #[cfg(target_os = "macos")] + fn coretext_lists_the_system_font_files() { + let files = super::coretext_font_paths(); + assert!(files.iter().any(|f| f.starts_with("/System/Library/Fonts")), "{} files", files.len()); + assert!(files.iter().all(|f| f.is_file() && !f.starts_with("/System/Library/PrivateFrameworks"))); + } + + /// Every family macOS offers is in the font list, not only those in the font folders (#6683). + #[test] + #[cfg(target_os = "macos")] + fn every_family_coretext_offers_is_listed() { + use super::{Collection, CollectionOptions, SourceKind}; + let mut system = Collection::new(CollectionOptions { shared: false, system_fonts: true }); + // Its names include localized aliases of a family ("Systemschrift"); the family has one name. + let aliases: Vec = system.family_names().map(str::to_string).collect(); + let mut offered = Vec::new(); + for family in aliases.iter().filter_map(|n| system.family_by_name(n)) { + // A family only the reserved UI fonts provide (PingFang before its download) is not listed. + let listed_file = + |font: &parley::fontique::FontInfo| matches!(font.source().kind(), SourceKind::Path(p) if !p.starts_with(super::MACOS_PRIVATE_FONTS)); + if !super::is_hidden_family(family.name()) && family.fonts().iter().any(listed_file) { + offered.push(family.name().to_string()); + } + } + offered.sort(); + offered.dedup(); + assert!(!offered.is_empty()); + let listed = super::FontDb::with_system_fonts().families(); + let missing: Vec<&String> = offered.iter().filter(|n| !listed.contains(n)).collect(); + assert!(missing.is_empty(), "macOS offers these families, the font list lacks them: {missing:?}"); + } + #[test] fn missing_font_dirs_are_skipped() { let mut files = Vec::new();