Merge PR #4219: SVG import: set text in an installed font when the named ones are missing

This commit is contained in:
aiongg committed 2026-10-22 02:21:39 +00:00
commit de050bcf6b
1 file changed
+111
+111
View File
@@ -65,11 +65,75 @@ fn fonts() -> Arc<usvg::fontdb::Database> {
}
#[cfg(not(target_arch = "wasm32"))]
db.load_system_fonts();
resolve_generic_families(&mut db, system_generic_family);
Arc::new(db)
})
.clone()
}
/// Installed families that stand in for each generic one, best first: the metric-compatible
/// Liberation and Croscore fonts, then common Linux defaults, then the bundled craft fonts.
const SANS_SERIF: &[&str] = &["Liberation Sans", "Arimo", "Helvetica", "DejaVu Sans", "Noto Sans", photocraft_text::fonts::DEFAULT_FAMILY];
const SERIF: &[&str] = &["Liberation Serif", "Tinos", "Times", "DejaVu Serif", "Noto Serif"];
const MONOSPACE: &[&str] = &["Liberation Mono", "Cousine", "Courier", "DejaVu Sans Mono", "Noto Sans Mono", photocraft_text::fonts::MONO_FAMILY];
/// Points the generic families (`sans-serif`, `serif`, `monospace`, `cursive`, `fantasy`) at
/// fonts the database holds. fontdb names Windows fonts for them (Arial, Times New Roman,
/// Courier New…): on a system without those, every family of a usual
/// `font-family="Arial, Helvetica, sans-serif"` misses, and usvg drops the text although
/// hundreds of fonts are installed, where a browser sets it in a fallback font. A generic whose
/// font is installed is left alone. Otherwise the system's choice (`system`, Fontconfig on Linux)
/// wins when the database has it, then the first installed stand-in, then the sans-serif choice,
/// then any font at all.
fn resolve_generic_families(db: &mut usvg::fontdb::Database, system: impl Fn(&str) -> Option<String>) {
use usvg::fontdb::Family;
let has = |db: &usvg::fontdb::Database, name: &str| db.faces().any(|f| f.families.iter().any(|(n, _)| n.eq_ignore_ascii_case(name)));
let Some(any) = db.faces().find_map(|f| f.families.first()).map(|(n, _)| n.clone()) else { return };
let mut sans = None;
for (generic, css, stand_ins) in [
(Family::SansSerif, "sans-serif", SANS_SERIF),
(Family::Serif, "serif", SERIF),
(Family::Monospace, "monospace", MONOSPACE),
(Family::Cursive, "cursive", &[][..]),
(Family::Fantasy, "fantasy", &[][..]),
] {
let current = db.family_name(&generic).to_string();
let pick = if has(db, &current) {
current
} else {
system(css)
.filter(|n| has(db, n))
.or_else(|| stand_ins.iter().find(|n| has(db, n)).map(|n| n.to_string()))
.or_else(|| sans.clone())
.unwrap_or_else(|| any.clone())
};
match generic {
Family::SansSerif => db.set_sans_serif_family(pick.clone()),
Family::Serif => db.set_serif_family(pick.clone()),
Family::Monospace => db.set_monospace_family(pick.clone()),
Family::Cursive => db.set_cursive_family(pick.clone()),
_ => db.set_fantasy_family(pick.clone()),
}
if generic == Family::SansSerif {
sans = Some(pick);
}
}
}
/// The family Fontconfig sets a generic family in (`fc-match sans-serif`), as the rest of the
/// desktop does; `None` without Fontconfig.
#[cfg(all(target_os = "linux", not(target_arch = "wasm32")))]
fn system_generic_family(generic: &str) -> Option<String> {
let out = std::process::Command::new("fc-match").args(["--format=%{family[0]}", generic]).output().ok()?;
let name = String::from_utf8(out.stdout).ok()?;
Some(name.trim().to_string()).filter(|n| out.status.success() && !n.is_empty())
}
#[cfg(not(all(target_os = "linux", not(target_arch = "wasm32"))))]
fn system_generic_family(_generic: &str) -> Option<String> {
None
}
/// Parse options for an untrusted drawing. `<image href>` resolves only `data:` URLs: a file path
/// or relative href is never read, so opening an SVG cannot pull local files (`/etc/...`,
/// `~/.ssh/...`) into the document. Embedded SVG images get no `<image>` resolution at all
@@ -828,6 +892,53 @@ mod tests {
assert!(r.warnings.iter().any(|w| w.contains("clip-path")), "{:?}", r.warnings);
}
/// A database with the bundled Inter alone, generics resolved without Fontconfig: none of
/// fontdb's Windows defaults (Arial, Times New Roman, Courier New) is in it.
fn inter_only() -> Arc<usvg::fontdb::Database> {
let mut db = usvg::fontdb::Database::new();
db.load_font_data(include_bytes!("../../../assets/fonts/Inter-Regular.ttf").to_vec());
resolve_generic_families(&mut db, |_| None);
Arc::new(db)
}
/// Text asking for fonts that aren't installed (`Arial, Helvetica, sans-serif` on a Linux
/// without them) is set in an installed font, as a browser does, instead of being dropped.
#[test]
fn text_in_missing_fonts_falls_back_to_an_installed_one() {
for family in ["Arial, Helvetica, sans-serif", "Times New Roman", "Courier New, monospace", "cursive", "fantasy"] {
let svg = format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"120\" height=\"60\"><text x=\"5\" y=\"45\" font-size=\"48\" font-family=\"{family}\">II</text></svg>"
);
let parsed = parse_with(svg.as_bytes(), inter_only).unwrap();
assert!(!parsed.fontless_text);
let r = import_parsed("t", parsed, MAX_GROUP_DEPTH).unwrap();
assert_eq!(r.document.layers.first().map(|l| l.name.as_str()), Some("II"), "{family}: {:?}", r.warnings);
}
}
#[test]
fn generic_families_point_at_installed_fonts() {
use usvg::fontdb::Family;
let db = inter_only();
for g in [Family::SansSerif, Family::Serif, Family::Monospace, Family::Cursive, Family::Fantasy] {
assert_eq!(db.family_name(&g), "Inter", "{g:?}");
}
// The system's choice wins when installed; a generic whose font is installed stays.
let mut db = usvg::fontdb::Database::new();
db.load_font_data(include_bytes!("../../../assets/fonts/Inter-Regular.ttf").to_vec());
db.load_font_data(include_bytes!("../../../assets/fonts/JetBrainsMono-Regular.ttf").to_vec());
db.set_cursive_family("Inter");
resolve_generic_families(&mut db, |g| Some(if g == "serif" { "JetBrains Mono" } else { "Not Installed" }.to_string()));
assert_eq!(db.family_name(&Family::Serif), "JetBrains Mono");
assert_eq!(db.family_name(&Family::Monospace), "JetBrains Mono", "the bundled mono stand-in");
assert_eq!(db.family_name(&Family::SansSerif), "Inter");
assert_eq!(db.family_name(&Family::Cursive), "Inter");
// An empty database is left alone.
let mut empty = usvg::fontdb::Database::new();
resolve_generic_families(&mut empty, |_| Some("Inter".into()));
assert_eq!(empty.family_name(&Family::SansSerif), "Arial");
}
#[test]
fn rasterized_groups_stop_at_the_aggregate_pixel_budget() {
let mut svg = String::from("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"256\" height=\"256\">");