From 363bc05edd007cf1d5aa870bfbf694a7be8496f4 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 23:28:58 +0700 Subject: [PATCH] font-catalog: a language the typographic family records leave out keeps its legacy family name in localized_families --- crates/font-catalog/src/scan.rs | 4 +++- crates/font-catalog/src/tests.rs | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/font-catalog/src/scan.rs b/crates/font-catalog/src/scan.rs index f8570ea57..096603f03 100644 --- a/crates/font-catalog/src/scan.rs +++ b/crates/font-catalog/src/scan.rs @@ -282,8 +282,10 @@ pub(crate) fn font_faces(font: &skrifa::FontRef<'_>, outlines: Outlines, index: // `name` read past the record cap `localized` keeps, so it decides whether a typographic family exists. let has_typo_family = name(font, &[StringId::TYPOGRAPHIC_FAMILY_NAME]).is_some(); let family_records = if has_typo_family { &typo_family } else { &legacy_family }; + // A language the typographic records leave out keeps its legacy name (`宋体` beside an English-only typographic `SimSun`). + let legacy_only = legacy_family.iter().filter(|(language, _)| has_typo_family && !typo_family.iter().any(|(l, _)| l == language)); let mut localized_families: Vec = Vec::new(); - for (language, n) in family_records { + for (language, n) in family_records.iter().chain(legacy_only) { if *n != family && !localized_families.iter().any(|l| l.name == *n) { localized_families.push(LocalizedName { language: language.clone(), name: n.clone() }); } diff --git a/crates/font-catalog/src/tests.rs b/crates/font-catalog/src/tests.rs index 6c7b052bf..5441f2e85 100644 --- a/crates/font-catalog/src/tests.rs +++ b/crates/font-catalog/src/tests.rs @@ -20,6 +20,8 @@ struct Spec { legacy: Option<(&'static str, &'static str)>, /// Japanese family name. ja: Option<&'static str>, + /// Japanese legacy family name (name ID 1), beside a typographic family. + legacy_ja: Option<&'static str>, weight: u16, width: u16, italic: bool, @@ -30,7 +32,7 @@ struct Spec { } fn spec(family: &'static str, style: &'static str, weight: u16) -> Spec { - Spec { family, style, legacy: None, ja: None, weight, width: 5, italic: false, fs_type: 0, instances: vec![], outlines: true } + Spec { family, style, legacy: None, ja: None, legacy_ja: None, weight, width: 5, italic: false, fs_type: 0, instances: vec![], outlines: true } } fn font(s: &Spec) -> Vec { @@ -54,6 +56,9 @@ fn font(s: &Spec) -> Vec { if let Some(ja) = s.ja { add(0x411, if s.legacy.is_some() { 16 } else { 1 }, ja.into()); } + if let Some(ja) = s.legacy_ja { + add(0x411, 1, ja.into()); + } let mut instances = Vec::new(); for (next_id, (style, wght)) in (256..).zip(&s.instances) { add(0x409, next_id, (*style).into()); @@ -411,3 +416,18 @@ fn a_confined_folder_follows_only_links_that_stay_inside_it() { assert_eq!(families(true), (true, false), "a confined folder follows no link out of it"); let _ = std::fs::remove_dir_all(&root); } + +/// A language the typographic family records leave out names the family by its legacy record, so an English-only +/// typographic family keeps its Japanese legacy name; a language they cover keeps the typographic name. +#[test] +fn a_language_without_a_typographic_family_name_keeps_its_legacy_name() { + let localized = |s: &Spec| { + let faces = crate::scan::data_faces(&font(s), 1, None, Source::System); + faces[0].info.localized_families.iter().map(|l| l.name.clone()).collect::>() + }; + let typographic_english = Spec { legacy: Some(("Test Mincho", "Regular")), ..spec("Test Mincho", "Regular", 400) }; + assert_eq!(localized(&Spec { legacy_ja: Some("テスト明朝"), ..typographic_english.clone() }), ["テスト明朝"]); + let both = Spec { ja: Some("テスト明朝 Pro"), legacy_ja: Some("テスト明朝"), ..typographic_english.clone() }; + assert_eq!(localized(&both), ["テスト明朝 Pro"], "the typographic record of a language wins over its legacy one"); + assert!(localized(&typographic_english).is_empty()); +}