Compare commits

..
Author SHA1 Message Date
autocraft-bot[bot] 297e0f5ac9 Merge main (c658b7a3) into the pull request
I resolved the one conflict, in crates/fonts/src/word.rs. SERIF and MONO

I resolved the one conflict, in crates/fonts/src/word.rs. SERIF and MONO stay at module level, as the pull request has them. Main's Cousine entry is added to MONO, and main's Arimo and Tinos ordering is kept in the Arial, Arial Unicode and Times lists. No conflict markers remain. cargo fmt --check passes, clippy passes for wordcraft-fonts and wordcraft-layout, and the wordcraft-fonts tests pass. The four wordcraft-layout tests that match missing_font pass, including the pull request's a_missing_font_is_drawn_in_its_font_table_alternate. I did not run the full wordcraft-layout suite this time.
2026-12-05 21:57:55 +00:00
vangel76 9bb2d72898 Keep serif and monospace substitutes for missing unclassified fonts 2026-12-05 18:36:41 +01:00
3 changed files with 33 additions and 18 deletions
+8 -2
View File
@@ -38,7 +38,15 @@ pub fn has_named_substitutes(family: &str) -> bool {
substitutes_by_name(charset_base_family(family)).1
}
/// Whether the resolver uses its generic serif or monospace list for the family.
pub fn uses_generic_serif_or_monospace_fallback(family: &str) -> bool {
let candidates = substitutes(charset_base_family(family));
candidates == SERIF || candidates == MONO
}
const SANS: &[&str] = &["Carlito", "Helvetica Neue", "Arial", "Liberation Sans", "Noto Sans", "DejaVu Sans", "Source Sans 3"];
const SERIF: &[&str] = &["Caladea", "Times New Roman", "Liberation Serif", "Georgia", "Noto Serif", "DejaVu Serif", "Source Serif 4"];
const MONO: &[&str] = &["Courier New", "Liberation Mono", "Cousine", "Menlo", "Consolas", "DejaVu Sans Mono", "JetBrains Mono"];
/// Word's symbol fonts (Symbol, Wingdings…) are proprietary: their characters are drawn as their
/// Unicode equivalents (`wordcraft_doc::symbol_fonts`) from these faces or any face that has them.
@@ -75,8 +83,6 @@ fn substitutes_by_name(family: &str) -> (&'static [&'static str], bool) {
return substitutes_by_name(first);
}
}
const SERIF: &[&str] = &["Caladea", "Times New Roman", "Liberation Serif", "Georgia", "Noto Serif", "DejaVu Serif", "Source Serif 4"];
const MONO: &[&str] = &["Courier New", "Liberation Mono", "Cousine", "Menlo", "Consolas", "DejaVu Sans Mono", "JetBrains Mono"];
const ARIAL: &[&str] = &["Liberation Sans", "Arimo", "Helvetica", "Helvetica Neue", "Source Sans 3"];
const ARIAL_UNICODE: &[&str] = &["Arial", "Liberation Sans", "Arimo", "Helvetica", "Helvetica Neue", "Source Sans 3"];
const TIMES: &[&str] = &["Liberation Serif", "Tinos", "Times", "Source Serif 4"];
+6 -3
View File
@@ -143,6 +143,9 @@ pub(super) const MISSING_SANS_FONT: &str = "Calibri";
/// 4. in [`MISSING_SANS_FONT`] otherwise: a swiss font with or without an alternate, and a modern
/// one with none.
///
/// With no roman or modern hint, a family with a serif or monospace fallback keeps its name so
/// the font resolver can use that fallback instead of Calibri.
///
/// Rules 2 to 4 skip a Thai font (drawn in a Thai face, #1549), a symbol font, and a font with
/// substitutes of its own (Times New Roman is roman in every font table, and where it isn't
/// installed Liberation Serif has its metrics; Word never meets it missing): those keep `font`,
@@ -186,7 +189,7 @@ fn lookup<'f>(doc: &'f Document, font: &'f str, run_font: bool) -> &'f str {
if let Some(alt) = alternate.map(str::trim).filter(drawable) {
return alt;
}
let modern_alternate_missing = alternate.is_some() && listed(&settings.modern_fonts);
let modern = listed(&settings.modern_fonts);
// Thai, symbol and fonts with substitutes of their own keep their name: those substitutes
// draw them, Cambria and Calibri wouldn't.
let keeps_name = wordcraft_fonts::word::is_thai_family(font)
@@ -196,9 +199,9 @@ fn lookup<'f>(doc: &'f Document, font: &'f str, run_font: bool) -> &'f str {
font
} else if let Some(core) = run_font.then(|| core_font_named_by(font)).flatten() {
core
} else if roman || modern_alternate_missing {
} else if roman || (modern && alternate.is_some()) {
MISSING_ROMAN_FONT
} else if run_font && !font.contains(';') {
} else if run_font && !font.contains(';') && (modern || !wordcraft_fonts::word::uses_generic_serif_or_monospace_fallback(font)) {
MISSING_SANS_FONT
} else {
font
+19 -13
View File
@@ -1508,32 +1508,31 @@ fn a_missing_font_is_drawn_in_its_font_table_alternate() {
// or modern font, Calibri for a swiss one.
#[derive(Clone, Copy)]
enum Family {
Swiss,
Unclassified,
Roman,
Modern,
}
let face_of = |alt: Option<&str>, family: Family| {
let face_of = |font: &str, alt: Option<&str>, family: Family| {
let mut d = Document::from_text("Lanterns");
if let Some(alt) = alt {
d.settings.font_alt_names = vec![("No Such Brand Font".into(), alt.into())];
d.settings.font_alt_names = vec![(font.into(), alt.into())];
}
match family {
Family::Roman => d.settings.roman_fonts = vec!["No Such Brand Font".into()],
Family::Modern => d.settings.modern_fonts = vec!["No Such Brand Font".into()],
Family::Swiss => {}
Family::Roman => d.settings.roman_fonts = vec![font.into()],
Family::Modern => d.settings.modern_fonts = vec![font.into()],
Family::Unclassified => {}
}
let p = d.para_mut(StoryRef::Body, &Path::top(0)).unwrap();
p.runs[0].props.font = Some("No Such Brand Font".into());
p.runs[0].props.font = Some(font.into());
let l = lay(&d);
let Some(Placed::Lines { para, .. }) = l.pages[0].items.iter().find(|i| matches!(i, Placed::Lines { .. })) else { panic!("no text") };
let first = para.clusters.first().expect("a cluster");
para.styles[first.style as usize].face
};
let face = |alt: Option<&str>, roman: bool| face_of(alt, if roman { Family::Roman } else { Family::Swiss });
let face = |alt: Option<&str>, roman: bool| face_of("No Such Brand Font", alt, if roman { Family::Roman } else { Family::Unclassified });
let resolve = |f: &str| wordcraft_fonts::word::resolve(f, false, false).face;
let (courier, times, calibri, cambria) = (resolve("Courier New"), resolve("Times New Roman"), resolve("Calibri"), resolve("Cambria"));
// A missing font that isn't roman is drawn in Calibri (Word for Windows 16.0.20625): here the
// bundled face Calibri resolves to, on Windows Calibri itself.
// A missing font with no recognized family is drawn in Calibri, here its bundled substitute.
let generic = face(None, false);
assert_eq!(generic, calibri);
// Serif and sans faces are bundled, so these differ on any machine.
@@ -1557,9 +1556,16 @@ fn a_missing_font_is_drawn_in_its_font_table_alternate() {
assert_eq!(face(Some("No Such Alternate, Times New Roman"), true), cambria);
// A modern font: its installed alternate draws it, a missing alternate leaves it in Cambria,
// and with no alternate at all it is drawn in Calibri.
assert_eq!(face_of(Some("Courier New"), Family::Modern), courier);
assert_eq!(face_of(Some("No Such Alternate"), Family::Modern), cambria);
assert_eq!(face_of(None, Family::Modern), generic);
assert_eq!(face_of("No Such Brand Font", Some("Courier New"), Family::Modern), courier);
assert_eq!(face_of("No Such Brand Font", Some("No Such Alternate"), Family::Modern), cambria);
assert_eq!(face_of("No Such Brand Font", None, Family::Modern), generic);
for font in ["Liberation Serif", "Georgia", "Liberation Mono", "Source Code Pro"] {
let fallback = resolve(font);
assert_ne!(fallback, generic, "{font} has a different substitute from Calibri");
assert_eq!(face_of(font, None, Family::Unclassified), fallback, "{font} keeps its substitute without a recorded class");
}
assert_eq!(face_of("Liberation Mono", None, Family::Modern), generic, "a recorded class still applies");
assert_eq!(face_of("Georgia", Some("Courier New"), Family::Unclassified), courier, "a drawable alternate still wins");
// An alternate that names another missing font with an alternate of its own: the chain isn't
// followed.
let mut d = Document::from_text("Lanterns");