Skip to content

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

Integration: a WWS family that is also the legacy family name finds the optical-size face

aiongg7ff975280550
diff --git a/crates/fonts/src/fontdb.rs b/crates/fonts/src/fontdb.rs
index a7e40c39c..f65dd700e 100644
--- a/crates/fonts/src/fontdb.rs
+++ b/crates/fonts/src/fontdb.rs
@@ -1574,32 +1574,38 @@ impl ScopedFonts<'_> {
}
/// Resolve localized and legacy family names without confusing replacement fonts with
- /// installed originals. Document-local aliases take precedence over shared fonts.
+ /// installed originals. Document-local names take precedence over shared fonts.
+ ///
+ /// In each scope (the document's faces, loaded faces, the catalog) an exact typographic name
+ /// wins, then a WWS family name (an optical-size face's, see `find`), which names its faces as
+ /// it is, then the aliases. An optical-size face's legacy family name (name ID 1) is usually
+ /// its WWS name, so the aliases must not claim it first.
pub fn canonical_family(&self, family: &str) -> Option<String> {
- let matches =
- |f: &&Arc<FontFace>| f.family.eq_ignore_ascii_case(family) || f.family_aliases.iter().any(|alias| alias.eq_ignore_ascii_case(family));
- if let Some(face) = self.own().iter().find(matches) {
- return Some(face.family.clone());
- }
- if let Some(face) = self.db.read_faces().iter().find(matches) {
- return Some(face.family.clone());
+ let is = |name: &str| name.eq_ignore_ascii_case(family);
+ let in_faces = |faces: &[Arc<FontFace>]| -> Option<String> {
+ if let Some(f) = faces.iter().find(|f| is(&f.family)) {
+ return Some(f.family.clone());
+ }
+ if let Some(w) = faces.iter().find_map(|f| f.wws.as_ref().filter(|w| is(&w.0))) {
+ return Some(w.0.clone());
+ }
+ faces.iter().find(|f| f.family_aliases.iter().any(|alias| is(alias))).map(|f| f.family.clone())
+ };
+ if let Some(name) = in_faces(self.own()).or_else(|| in_faces(&self.db.read_faces())) {
+ return Some(name);
}
#[cfg(not(target_arch = "wasm32"))]
- if let Some(face) = self
- .db
- .read_catalog()
- .iter()
- .find(|f| f.family.eq_ignore_ascii_case(family) || f.aliases.iter().any(|alias| alias.eq_ignore_ascii_case(family)))
{
- return Some(face.family.clone());
- }
- // A WWS family name (an optical-size face's, see `find`) names its faces as it is.
- if let Some(face) = self.own().iter().chain(self.db.read_faces().iter()).find(|f| has_wws_family(f, family)) {
- return face.wws.as_ref().map(|w| w.0.clone());
- }
- #[cfg(not(target_arch = "wasm32"))]
- if let Some(c) = self.db.read_catalog().iter().find(|c| c.wws.as_ref().is_some_and(|w| w.0.eq_ignore_ascii_case(family))) {
- return c.wws.as_ref().map(|w| w.0.clone());
+ let catalog = self.db.read_catalog();
+ if let Some(c) = catalog.iter().find(|c| is(&c.family)) {
+ return Some(c.family.clone());
+ }
+ if let Some(w) = catalog.iter().find_map(|c| c.wws.as_ref().filter(|w| is(&w.0))) {
+ return Some(w.0.clone());
+ }
+ if let Some(c) = catalog.iter().find(|c| c.aliases.iter().any(|alias| is(alias))) {
+ return Some(c.family.clone());
+ }
}
None
}
@@ -1610,7 +1616,10 @@ impl ScopedFonts<'_> {
return false;
}
let face = self.face(family, style);
- norm(&face.style) == norm(style) || (face.coords.is_empty() && face.style_aliases.iter().any(|alias| norm(alias) == norm(style)))
+ norm(&face.style) == norm(style)
+ || (face.coords.is_empty() && face.style_aliases.iter().any(|alias| norm(alias) == norm(style)))
+ // An optical-size face asked for by its WWS names.
+ || face.wws.as_ref().is_some_and(|w| w.0.eq_ignore_ascii_case(family) && norm(&w.1) == norm(style))
}
/// Prefer known metric-compatible substitutes, then an appropriate script family. This
diff --git a/crates/fonts/src/testing.rs b/crates/fonts/src/testing.rs
index 2844e2d88..283767e25 100644
--- a/crates/fonts/src/testing.rs
+++ b/crates/fonts/src/testing.rs
@@ -72,15 +72,19 @@ pub fn font_mapping(family: &str, glyphs: &[(char, char)]) -> Option<Vec<u8>> {
/// ("Kepler Std" / "Light Display" and "Kepler Std Display" / "Light"). `None` if the bundled font
/// can't be read.
pub fn font_with_wws(family: &str, style: &str, wws_family: &str, wws_style: &str, chars: &[char]) -> Option<Vec<u8>> {
- let base = font_with(family, chars)?;
- let entries = [(1u16, family), (2, style), (16, family), (17, style), (21, wws_family), (22, wws_style)];
+ font_with_names(&[(1, family), (2, style), (16, family), (17, style), (21, wws_family), (22, wws_style)], chars)
+}
+
+/// [`font_with`] whose name table holds exactly `entries`: (name ID, Windows English name).
+pub fn font_with_names(entries: &[(u16, &str)], chars: &[char]) -> Option<Vec<u8>> {
+ let base = font_with(entries.first()?.1, chars)?;
let count = u16::try_from(entries.len()).ok()?;
let mut name = Vec::new();
for v in [0u16, count, 6 + count * 12] {
name.extend_from_slice(&v.to_be_bytes());
}
let mut at = 0usize;
- for (id, s) in entries {
+ for &(id, s) in entries {
let len = s.encode_utf16().count() * 2;
for v in [3u16, 1, 0x409, id, u16::try_from(len).ok()?, u16::try_from(at).ok()?] {
name.extend_from_slice(&v.to_be_bytes());
diff --git a/crates/fonts/src/tests_docfonts.rs b/crates/fonts/src/tests_docfonts.rs
index f7bad7947..67b1d8748 100644
--- a/crates/fonts/src/tests_docfonts.rs
+++ b/crates/fonts/src/tests_docfonts.rs
@@ -5,7 +5,7 @@
use std::path::{Path, PathBuf};
use super::*;
-use crate::testing::{font_with, font_with_wws, with_fs_type};
+use crate::testing::{font_with, font_with_names, font_with_wws, with_fs_type};
/// An empty temporary folder unique to `name` and this process.
fn temp_dir(name: &str) -> PathBuf {
@@ -210,3 +210,23 @@ fn optical_size_face_is_found_by_its_wws_names() {
assert_eq!(fonts.styles("DocFont Kepler"), ["Light Display"]);
let _ = std::fs::remove_dir_all(&folder);
}
+
+#[test]
+fn a_wws_family_that_is_also_the_legacy_family_finds_the_optical_size_face() {
+ // Optical-size faces usually carry their WWS family as the legacy family (name ID 1) too.
+ let folder = temp_dir("wws-legacy");
+ let display =
+ [(1, "DocFont Kepler Display"), (2, "Regular"), (16, "DocFont Kepler"), (17, "Light Display"), (21, "DocFont Kepler Display"), (22, "Light")];
+ let text = [(1, "DocFont Kepler Light"), (2, "Regular"), (16, "DocFont Kepler"), (17, "Light")];
+ std::fs::write(folder.join("light-display.otf"), font_with_names(&display, &['k']).unwrap()).unwrap();
+ std::fs::write(folder.join("light.otf"), font_with_names(&text, &['k']).unwrap()).unwrap();
+ let db = FontDb::with_font_dirs(vec![]);
+ let r = db.load_document_fonts(&folder, &|_| Ok(()));
+ assert_eq!((r.faces, r.skipped.len()), (2, 0), "{:?}", r.skipped);
+ let fonts = db.scoped(r.scope);
+ assert_eq!(fonts.face("DocFont Kepler Display", "Light").style, "Light Display", "the display face, not the text size");
+ assert_eq!(fonts.styles("DocFont Kepler Display"), ["Light"]);
+ assert!(fonts.has_style("DocFont Kepler Display", "Light"));
+ assert_eq!(fonts.face("DocFont Kepler", "Light").style, "Light");
+ let _ = std::fs::remove_dir_all(&folder);
+}