font-catalog: a language the typographic family records leave out keeps its legacy family name in localized_families

This commit is contained in:
bflatastic
2026-11-22 23:28:58 +07:00
parent 52d1ec7402
commit 363bc05edd
2 changed files with 24 additions and 2 deletions
+3 -1
View File
@@ -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<LocalizedName> = 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() });
}
+21 -1
View File
@@ -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<u8> {
@@ -54,6 +56,9 @@ fn font(s: &Spec) -> Vec<u8> {
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::<Vec<_>>()
};
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());
}