Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Integration: fonts match a face's own style name before other faces' aliases (#126)

aiongg3692eba3c4ca
diff --git a/crates/fonts/src/fontdb.rs b/crates/fonts/src/fontdb.rs
index 70a9a1e31..da2e4f00b 100644
--- a/crates/fonts/src/fontdb.rs
+++ b/crates/fonts/src/fontdb.rs
@@ -1619,7 +1619,12 @@ impl ScopedFonts<'_> {
return None;
}
let ns = norm(style);
- if let Some(f) = cands.iter().find(|f| norm(&f.style) == ns || (f.coords.is_empty() && f.style_aliases.iter().any(|alias| norm(alias) == ns)))
+ // A face's own style name first: another face's legacy subfamily ("Black" faces named
+ // "Regular" in their RIBBI family) or localized name must not shadow it.
+ if let Some(f) = cands
+ .iter()
+ .find(|f| norm(&f.style) == ns)
+ .or_else(|| cands.iter().find(|f| f.coords.is_empty() && f.style_aliases.iter().any(|alias| norm(alias) == ns)))
{
return Some((*f).clone());
}
diff --git a/crates/fonts/src/lib.rs b/crates/fonts/src/lib.rs
index b591c8686..df66a6c24 100644
--- a/crates/fonts/src/lib.rs
+++ b/crates/fonts/src/lib.rs
@@ -581,6 +581,25 @@ mod resolution_tests {
assert_eq!(scope.styles("别名字体"), ["Regular"]);
}
+ #[test]
+ fn a_style_name_beats_another_face_s_legacy_subfamily() {
+ // Static families often name each weight's legacy (RIBBI) family "Legacy Sans Black" with
+ // subfamily "Regular": that alias must not shadow the face actually styled Regular.
+ let face =
+ |records: &[(u16, u16, u16, u16, &str)]| testing::with_names(&testing::font_with("Legacy Sans", &['A']).unwrap(), records).unwrap();
+ let db = FontDb::with_font_dirs(Vec::new());
+ db.add_font(face(&[
+ (3, 1, 0x409, 1, "Legacy Sans Black"),
+ (3, 1, 0x409, 2, "Regular"),
+ (3, 1, 0x409, 16, "Legacy Sans"),
+ (3, 1, 0x409, 17, "Black"),
+ ]));
+ db.add_font(face(&[(3, 1, 0x409, 1, "Legacy Sans"), (3, 1, 0x409, 2, "Regular")]));
+ let scope = db.scoped(0);
+ assert_eq!(scope.face("Legacy Sans", "Regular").style, "Regular");
+ assert_eq!(scope.face("Legacy Sans", "Black").style, "Black");
+ }
+
#[test]
fn known_substitution_preserves_missing_original_status() {
let db = FontDb::with_font_dirs(Vec::new());