From 69e8a0a39214b7456e6f7f4f6ac1d402a41a5366 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 09:33:49 -0800 Subject: [PATCH] Fonts: offer a split PostScript name's file only when installing it resolves the name The joined-name check now matches the face's PostScript name only, as `FontDb::canonical` does for cataloged faces, so a file that carries the joined name only as a full name is no longer offered. A legacy family of the wanted name now decides the result, looking for the style in the family of each of its faces, since `canonical` does not join the name when the legacy family resolves it. Tests cover the full-name and legacy cases against `FontDb::resolve`, and a collection whose faces share a legacy family in either order. --- crates/text/src/fontdb/wanted.rs | 14 +++++---- crates/text/src/tests/sysfonts.rs | 48 +++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/crates/text/src/fontdb/wanted.rs b/crates/text/src/fontdb/wanted.rs index 0e6c4dfbaf..8d72bdc3ab 100644 --- a/crates/text/src/fontdb/wanted.rs +++ b/crates/text/src/fontdb/wanted.rs @@ -136,15 +136,19 @@ fn provides(w: &WantedFont, faces: &[FaceStyle]) -> bool { }) { return true; } - // A legacy family with another style: that style of the family. - if faces.iter().find(|f| f.keys.legacy.iter().any(|(family, _)| *family == key)).is_some_and(|f| styled(&f.family)) { - return true; + // A legacy family with another style: that style of the family, among every face of the + // legacy family (in a collection they can be of different families). Once installed, the + // legacy family resolves the name, and `FontDb::canonical` doesn't join it. + let mut legacy = faces.iter().filter(|f| f.keys.legacy.iter().any(|(family, _)| *family == key)).peekable(); + if legacy.peek().is_some() { + return legacy.any(|f| styled(&f.family)); } // A family and style split from a PostScript name by an importer that didn't have the font // (`Gilroy-Black` → Gilroy, Black), as `FontDb::canonical` joins them again: a face whose - // PostScript or full name is that name, whatever family and style the file gives it. + // PostScript name is that name, whatever family and style the file gives it. Of a cataloged + // face, `canonical` reads the PostScript name only, so a full name alone isn't enough. let joined = joined(w); - !sk.is_empty() && faces.iter().any(|f| f.keys.postscript.contains(&joined) || f.keys.full_names.contains(&joined)) + !sk.is_empty() && faces.iter().any(|f| f.keys.postscript.contains(&joined)) } /// The family and style of `w` as one normalized name (`Gilroy` and `Black` → `gilroyblack`). diff --git a/crates/text/src/tests/sysfonts.rs b/crates/text/src/tests/sysfonts.rs index f456a29926..2f15ffe119 100644 --- a/crates/text/src/tests/sysfonts.rs +++ b/crates/text/src/tests/sysfonts.rs @@ -1256,11 +1256,47 @@ fn a_search_finds_the_file_of_a_postscript_name_split_into_family_and_style() { assert_eq!(wanted("Black").provided_by(&file), [0]); assert!(wanted("Bold").provided_by(&file).is_empty(), "another style of the family is another file"); assert!(wanted("").provided_by(&file).is_empty(), "without a style, the family alone is not the PostScript name"); - // A file that carries the joined name only as its full name is found the same way. - let data = named(&[(3, EN, 1, "Gilroy Heavy"), (3, EN, 4, "Gilroy Heavy"), (3, EN, 6, "GilroyHvy")]); - let file = dir.join("Gilroy-Heavy.ttf"); - std::fs::write(&file, &data).unwrap(); - assert_eq!(wanted("Heavy").provided_by(&file), [0]); - assert!(wanted("Black").provided_by(&file).is_empty()); let _ = std::fs::remove_dir_all(dir); + // The search offers a file only when installing it resolves the name exactly: not when the + // joined name is only a full name, and not when a legacy family of the same name resolves it + // first, to another style. + let cases = [ + ("full-name", vec![(3, EN, 1, "Gilroy Black"), (3, EN, 4, "Gilroy Black"), (3, EN, 6, "GilroyBlk")]), + ( + "legacy", + vec![(3, EN, 1, "Gilroy"), (3, EN, 2, "Regular"), (3, EN, 16, "Gilroy Display"), (3, EN, 17, "Regular"), (3, EN, 6, "Gilroy-Black")], + ), + ]; + for (name, records) in cases { + let dir = std::env::temp_dir().join(format!("vc-sysfonts-{}-split-ps-{name}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join(format!("{name}.ttf")); + std::fs::write(&file, named(&records)).unwrap(); + let db = FontDb::with_font_dirs(vec![dir.clone()]); + let exact = db.resolve("Gilroy", "Black").is_some_and(|(_, matched)| matched == FontMatch::Exact); + assert!(!exact, "{name}: installing the file doesn't resolve Gilroy Black exactly"); + assert!(wanted("Black").provided_by(&file).is_empty(), "{name}: so the search doesn't offer it"); + let _ = std::fs::remove_dir_all(dir); + } +} + +/// In a collection whose faces share a legacy family but not a typographic one, the search looks +/// for the style asked for in each face's family, in either order. +#[test] +fn a_search_finds_a_style_among_the_faces_of_a_shared_legacy_family() { + let regular = named(&[(3, EN, 1, "Gilroy"), (3, EN, 2, "Regular"), (3, EN, 16, "Gilroy Text"), (3, EN, 17, "Regular")]); + let black = + named(&[(3, EN, 1, "Gilroy"), (3, EN, 2, "Regular"), (3, EN, 16, "Gilroy Display"), (3, EN, 17, "Black"), (3, EN, 6, "Gilroy-Black")]); + for (order, faces) in [("regular-first", [regular.clone(), black.clone()]), ("black-first", [black.clone(), regular.clone()])] { + let dir = std::env::temp_dir().join(format!("vc-sysfonts-{}-shared-legacy-{order}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("shared.ttc"); + std::fs::write(&file, collection(&faces)).unwrap(); + let db = FontDb::with_font_dirs(vec![dir.clone()]); + let (face, matched) = db.resolve("Gilroy", "Black").unwrap(); + assert_eq!(matched, FontMatch::Exact, "{order}: {face:?}"); + let wanted = WantedFonts::new(&[WantedFont { family: "Gilroy".into(), style: "Black".into(), installed: None }]); + assert_eq!(wanted.provided_by(&file), [0], "{order}"); + let _ = std::fs::remove_dir_all(dir); + } }