Skip to content

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

Match placed SVG font-family against legacy family and full names

NetterSilver41a64bc10f2a
diff --git a/Cargo.lock b/Cargo.lock
index c89a20002..f63305bb3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1504,6 +1504,7 @@ dependencies = [
"log",
"quick-xml 0.41.0",
"resvg",
+ "skrifa 0.47.0",
"tiff",
"zune-core",
"zune-jpeg",
diff --git a/crates/images/Cargo.toml b/crates/images/Cargo.toml
index 48dc5955d..685c7d947 100644
--- a/crates/images/Cargo.toml
+++ b/crates/images/Cargo.toml
@@ -14,6 +14,7 @@ designcraft-fonts = { workspace = true }
image = { workspace = true }
tiff = { workspace = true }
resvg = { workspace = true }
+skrifa = { workspace = true }
quick-xml = { workspace = true }
flate2 = "1"
log = { workspace = true }
diff --git a/crates/images/src/lib.rs b/crates/images/src/lib.rs
index 97067ac41..3f9a23d63 100644
--- a/crates/images/src/lib.rs
+++ b/crates/images/src/lib.rs
@@ -104,7 +104,10 @@ fn svg_options() -> &'static usvg::Options<'static> {
/// family's but a face's PostScript name (as Illustrator's SVG export writes them:
/// `font-family="'MyriadPro-Regular'"`) sets the text in that face, whatever font-weight and
/// font-style say: the name already picks the weight and style, and a family's other faces would
-/// contradict it. Families listed before it come first.
+/// contradict it. A name that is a face's legacy family name (name ID 1) or full name (ID 4), as
+/// Windows names faces outside the regular/bold/italic quartet (`'Segoe UI Semibold'`, whose
+/// typographic family is `Segoe UI`), sets the text in that face (the closest to font-weight and
+/// font-style when the legacy family has several). Families listed before it come first.
fn select_font() -> usvg::FontSelectionFn<'static> {
use usvg::fontdb::{Family, Query, Stretch, Style, Weight};
Box::new(move |font, db| {
@@ -112,27 +115,67 @@ fn select_font() -> usvg::FontSelectionFn<'static> {
let spelled = |db: &usvg::fontdb::Database, name: &str| {
db.faces().flat_map(|f| &f.families).find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(n, _)| n.clone())
};
- for family in font.families() {
+ // (index in the font-family list, face) of the faces a name names by its legacy names.
+ let mut legacy: Vec<(usize, usvg::fontdb::ID)> = Vec::new();
+ for (i, family) in font.families().iter().enumerate() {
if let usvg::FontFamily::Named(name) = family
&& spelled(db, name).is_none()
{
if fonts.has_family(name) {
for style in fonts.styles(name) {
- add_face(db, fonts.face(name, &style));
+ let face = fonts.face(name, &style);
+ let named = has_legacy_name(&face, name);
+ if let Some(id) = add_face(db, face)
+ && named
+ {
+ legacy.push((i, id));
+ }
+ }
+ } else if postscript_face(db, name).is_none() {
+ if let Some((family, style)) = fonts.postscript_face(name) {
+ add_face(db, fonts.face(&family, &style));
+ } else {
+ // A full name: the family is the name without its last words ("Bold").
+ let mut base = name.as_str();
+ for _ in 0..3 {
+ let Some((head, _)) = base.trim_end().rsplit_once(' ') else { break };
+ base = head;
+ if fonts.has_family(base) {
+ for style in fonts.styles(base) {
+ let face = fonts.face(base, &style);
+ if has_legacy_name(&face, name)
+ && let Some(id) = add_face(db, face)
+ {
+ legacy.push((i, id));
+ }
+ }
+ break;
+ }
+ }
}
- } else if postscript_face(db, name).is_none()
- && let Some((family, style)) = fonts.postscript_face(name)
- {
- add_face(db, fonts.face(&family, &style));
}
}
}
- for family in font.families() {
+ let want_style = match font.style() {
+ usvg::FontStyle::Normal => Style::Normal,
+ usvg::FontStyle::Italic => Style::Italic,
+ usvg::FontStyle::Oblique => Style::Oblique,
+ };
+ for (i, family) in font.families().iter().enumerate() {
match family {
usvg::FontFamily::Named(name) if spelled(db, name).is_none() => {
if let Some(id) = postscript_face(db, name) {
return Some(id);
}
+ let closest = legacy
+ .iter()
+ .filter(|(at, _)| *at == i)
+ .filter_map(|(_, id)| db.face(*id))
+ .min_by_key(|f| (f.style != want_style, f.weight.0.abs_diff(font.weight())))
+ .map(|f| f.id);
+ if closest.is_some() {
+ return closest;
+ }
}
// A family (or generic family) that matches: the query below.
_ => break,
@@ -173,15 +216,22 @@ fn select_font() -> usvg::FontSelectionFn<'static> {
usvg::FontStretch::ExtraExpanded => Stretch::ExtraExpanded,
usvg::FontStretch::UltraExpanded => Stretch::UltraExpanded,
};
- let style = match font.style() {
- usvg::FontStyle::Normal => Style::Normal,
- usvg::FontStyle::Italic => Style::Italic,
- usvg::FontStyle::Oblique => Style::Oblique,
- };
- db.query(&Query { families: &families, weight: Weight(font.weight()), stretch, style })
+ db.query(&Query { families: &families, weight: Weight(font.weight()), stretch, style: want_style })
})
}
+/// Is `name` (ignoring case) a legacy family name (name ID 1) or full name (ID 4) of `face`?
+fn has_legacy_name(face: &designcraft_fonts::FontFace, name: &str) -> bool {
+ use skrifa::MetadataProvider;
+ use skrifa::string::StringId;
+ let Some(f) = face.skrifa() else { return false };
+ [StringId::FAMILY_NAME, StringId::FULL_NAME]
+ .into_iter()
+ .flat_map(|id| f.localized_strings(id))
+ .take(256)
+ .any(|s| s.chars().map(|c| c.to_ascii_lowercase()).eq(name.chars().map(|c| c.to_ascii_lowercase())))
+}
+
/// The face in the SVG's font database with PostScript name `name` (ignoring case).
fn postscript_face(db: &usvg::fontdb::Database, name: &str) -> Option<usvg::fontdb::ID> {
db.faces().find(|f| f.post_script_name.eq_ignore_ascii_case(name)).map(|f| f.id)
diff --git a/crates/images/src/tests.rs b/crates/images/src/tests.rs
index dd09f2577..d69a6f504 100644
--- a/crates/images/src/tests.rs
+++ b/crates/images/src/tests.rs
@@ -248,6 +248,34 @@ fn svg_text_finds_fonts_by_postscript_name() {
assert_eq!(text_face(r#" font-family="NoSuch-Font""#), "SourceSerif4-Regular");
}
+#[test]
+fn svg_text_matches_legacy_family_names() {
+ use designcraft_fonts::testing::{font_with_names, with_table_u16};
+ // Laid out as Windows names faces outside the regular/bold/italic quartet (seguisb.ttf): the
+ // weight is in the legacy family (ID 1) and full name (ID 4), the typographic family (ID 16)
+ // is the base name.
+ let face = |names: &[(u16, &str)], weight: u16| with_table_u16(font_with_names(names, &['a', 'b']).unwrap(), b"OS/2", 4, weight).unwrap();
+ let fonts = designcraft_fonts::FontDb::global();
+ fonts.add_font(face(&[(1, "DC Test Legacy"), (2, "Regular"), (4, "DC Test Legacy Regular"), (6, "DCTestLegacy-Regular")], 400));
+ fonts.add_font(face(&[(1, "DC Test Legacy"), (2, "Bold"), (4, "DC Test Legacy Bold"), (6, "DCTestLegacy-Bold")], 700));
+ for (name, weight) in [("Semibold", 600), ("Light", 300)] {
+ let legacy = format!("DC Test Legacy {name}");
+ let ps = format!("DCTestLegacy-{name}");
+ fonts.add_font(face(&[(1, &legacy), (2, "Regular"), (4, &legacy), (6, &ps), (16, "DC Test Legacy"), (17, name)], weight));
+ }
+ assert_eq!(text_face(r#" font-family="DC Test Legacy Semibold""#), "DCTestLegacy-Semibold");
+ assert_eq!(text_face(r#" font-family="dc test legacy light""#), "DCTestLegacy-Light");
+ assert_eq!(text_face(r#" font-family="'DC Test Legacy Semibold', sans-serif""#), "DCTestLegacy-Semibold");
+ assert_eq!(text_face(r#" font-family="NoSuch Font, 'DC Test Legacy Light', serif""#), "DCTestLegacy-Light");
+ // The full name (ID 4) of a face of the quartet.
+ assert_eq!(text_face(r#" font-family="DC Test Legacy Bold""#), "DCTestLegacy-Bold");
+ // The typographic family still picks by weight.
+ assert_eq!(text_face(r#" font-family="DC Test Legacy" font-weight="600""#), "DCTestLegacy-Semibold");
+ assert_eq!(text_face(r#" font-family="DC Test Legacy""#), "DCTestLegacy-Regular");
+ // A name nobody has still falls to the serif.
+ assert_eq!(text_face(r#" font-family="DC Test Legacy Nothing""#), "SourceSerif4-Regular");
+}
+
/// A minimal Photoshop file: 2×1 RGB, no layers, raw merged image.
fn tiny_psd() -> Vec<u8> {
let mut b = Vec::new();