Skip to content

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

SVG: a font family named in any case finds the installed font

KnottyDyes0613948d3d1d
diff --git a/crates/images/src/lib.rs b/crates/images/src/lib.rs
index 28a42522d..ece1697f1 100644
--- a/crates/images/src/lib.rs
+++ b/crates/images/src/lib.rs
@@ -73,14 +73,18 @@ fn svg_options() -> &'static usvg::Options<'static> {
}
/// A family the SVG names that isn't bundled comes from the shared fonts (installed ones too),
-/// as it would in a text frame. A name that is no 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.
+/// as it would in a text frame, and matches in any case, as it does there. A name that is no
+/// 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.
fn select_font() -> usvg::FontSelectionFn<'static> {
- let bundled = usvg::FontResolver::default_font_selector();
+ use usvg::fontdb::{Family, Query, Stretch, Style, Weight};
Box::new(move |font, db| {
let fonts = designcraft_fonts::FontDb::global();
+ 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() {
if let usvg::FontFamily::Named(name) = family
&& !has_family(db, name)
@@ -103,11 +107,51 @@ fn select_font() -> usvg::FontSelectionFn<'static> {
return Some(id);
}
}
- // A family (or generic family) that matches: usvg's own selection.
+ // A family (or generic family) that matches: the query below.
_ => break,
}
}
- bundled(font, db)
+ // usvg's default query, but with each named family spelled as the database spells it:
+ // fontdb compares names exactly, so "DeJaVu SaNs" would quietly fall to the serif.
+ let names: Vec<Option<String>> = font
+ .families()
+ .iter()
+ .map(|f| match f {
+ usvg::FontFamily::Named(name) => Some(spelled(db, name).unwrap_or_else(|| name.clone())),
+ _ => None,
+ })
+ .collect();
+ let mut families: Vec<Family> = font
+ .families()
+ .iter()
+ .zip(&names)
+ .map(|(f, name)| match f {
+ usvg::FontFamily::Serif => Family::Serif,
+ usvg::FontFamily::SansSerif => Family::SansSerif,
+ usvg::FontFamily::Cursive => Family::Cursive,
+ usvg::FontFamily::Fantasy => Family::Fantasy,
+ usvg::FontFamily::Monospace => Family::Monospace,
+ usvg::FontFamily::Named(_) => Family::Name(name.as_deref().unwrap_or_default()),
+ })
+ .collect();
+ families.push(Family::Serif);
+ let stretch = match font.stretch() {
+ usvg::FontStretch::UltraCondensed => Stretch::UltraCondensed,
+ usvg::FontStretch::ExtraCondensed => Stretch::ExtraCondensed,
+ usvg::FontStretch::Condensed => Stretch::Condensed,
+ usvg::FontStretch::SemiCondensed => Stretch::SemiCondensed,
+ usvg::FontStretch::Normal => Stretch::Normal,
+ usvg::FontStretch::SemiExpanded => Stretch::SemiExpanded,
+ usvg::FontStretch::Expanded => Stretch::Expanded,
+ 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 })
})
}
@@ -518,6 +562,27 @@ mod tests {
assert_eq!(text_face(r#" font-family="NoSuch-Font""#), "SourceSerif4-Regular");
}
+ #[test]
+ fn svg_family_names_match_in_any_case() {
+ let font = designcraft_fonts::testing::font_with("DC Test SVG Casing", &['A', 'b']).unwrap();
+ designcraft_fonts::FontDb::global().add_font(font);
+ let text = |family: &str, attrs: &str| {
+ format!(
+ r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="40"><text y="30" font-size="20" font-family="{family}"{attrs}>Ab</text></svg>"#
+ )
+ };
+ for family in ["DC Test SVG Casing", "dc test svg casing", "DC TEST svg Casing", "Nope, dc test SVG casing, serif"] {
+ let set = glyph_families(&text(family, ""));
+ assert!(set.len() == 2 && set.iter().all(|(f, gid)| f == "DC Test SVG Casing" && *gid != 0), "{family}: {set:?}");
+ }
+ // Weight and style still reach the query: there's only a regular face, so it sets the text.
+ let set = glyph_families(&text("dc test svg casing", r#" font-weight="700" font-style="italic""#));
+ assert!(set.len() == 2 && set.iter().all(|(f, _)| f == "DC Test SVG Casing"), "{set:?}");
+ // A family nobody has still falls to the bundled serif.
+ let set = glyph_families(&text("DC Test SVG Nobody", ""));
+ assert!(set.len() == 2 && set.iter().all(|(f, _)| f == "Source Serif 4"), "{set:?}");
+ }
+
/// A minimal Photoshop file: 2×1 RGB, no layers, raw merged image.
fn tiny_psd() -> Vec<u8> {
let mut b = Vec::new();