Compare commits

...
Author SHA1 Message Date
autocraft-bot[bot] de7d17fb7d Merge main (a67df42a) into the pull request
I resolved both conflicts. In crates/layout/src/para.rs I kept the pull 

I resolved both conflicts. In crates/layout/src/para.rs I kept the pull request's three font-lookup tests and main's shrink_line_spaces test. In docs/layout-parity.md I took main's header line (updated 2026-11-26) and put the pull request's #7064 entry at the front of its change list, and I kept both changelog table rows, main's first. No conflict markers remain, and cargo fmt --check was clean.

The full workspace would not load here because the pinned winit fork revision could not be fetched. I tested in a temporary copy outside the tree, limited to the geom, doc, fonts and layout crates and without the winit patch, and removed the copy afterwards. Clippy on wordcraft-layout with all targets finished without errors.

The wordcraft-layout unit tests gave 858 passed, 6 failed and 2 ignored. The pull request's new tests and main's shrink_line_spaces test passed. Three failures are timing or linear-time limits (a_megabyte_long_word_lays_out_quickly, a_long_unbreakable_word_lays_out_in_linear_time, context_mode_on_a_very_long_digit_run_stays_linear), probably because this machine is slow. The other three found no hyphenation points (auto_hyphenation_breaks_long_words, hyphenation_and_proofing_follow_each_runs_language, fragmented_hidden_text_lays_out_like_the_visible_text), probably because the language data is not available here. I did not run these six without the merge to confirm either explanation.
2026-12-03 16:48:25 +00:00
Mauro MazzieriandClaude Opus 5.5 caf58cd52a Merge main into fix/missing-sans-font-calibri (#7064)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-21 18:52:40 +01:00
Mauro MazzieriandClaude Opus 5.5 0c461485fb Fonts: a complex-script run is shaped in the face its script font lookup gives (#7064)
A complex-script segment that the run's complex-script face covers kept
no face of its own, so the shaping loop let style() look the font up
again as a run font, through lookup_family, which now draws a missing
font in Calibri or in the core font its name starts with. The text
could then be shaped in a face other than the one lookup_script_family
chose, checked for coverage and reported as the substitute (a missing
w:cs "Courier Foo" came out in Courier New's face). Such a segment now
passes that face as an override, as East Asian text does with its
font's face.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-20 13:42:52 +01:00
mmazzieriandClaude Opus 5.5 db6a9105de docs: layout parity row for missing fonts named after a core font (#7064)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 14:31:06 +01:00
mmazzieriandClaude Opus 5.5 d6a8db16b4 Fonts: a missing font named after a core font is drawn in that font, as in Word (#7064)
Word for Windows 16.0.20625 draws a missing font whose name starts with
Arial or Helv in Arial (Arial MT, ArialFoo, Arial Narrow Foo, Helvetica
Foo), with Times in Times New Roman, with Courier in Courier New and with
Segoe UI in Segoe UI, whatever its font table family class; the font
table alternate still comes first. Other names (Verdana MT, Tahoma Foo,
Georgia Foo, Foo Arial) are drawn in Calibri.

Without this, the Calibri rule of the previous commit drew real documents
set in "Arial MT" in Calibri, where Word draws Arial.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 14:29:32 +01:00
mmazzieriandClaude Opus 5.5 7addc314f9 Fonts: a missing font that isn't roman is drawn in Calibri, as in Word (#7064)
A run font that isn't installed, has no font table alternate that can be
drawn and isn't classed roman fell through lookup_family to the generic sans
substitutes (Carlito, Helvetica Neue, Arial...), which on Windows, where
Carlito usually isn't installed, land on Arial. Word for Windows 16.0.20625
draws such a font in Calibri for every family class but roman (swiss,
modern, auto, decorative, script). lookup_family now looks it up as Calibri,
which resolves to Calibri where it is installed and to Carlito or the
generic list elsewhere.

East Asian and complex-script fonts go through lookup_script_family, which
keeps the alternate and roman rules but not Calibri, which has no CJK,
Arabic or Hebrew. Thai, symbol, fallback lists and fonts with substitutes of
their own keep their name as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 13:37:40 +01:00
6 changed files with 177 additions and 28 deletions
+73
View File
@@ -31,6 +31,8 @@ pub use bidi::{bidi_levels, visual_order};
pub(crate) use builder::left_out;
#[cfg(test)]
use chars::{MISSING_ROMAN_FONT, lookup_family};
#[cfg(test)]
use chars::{MISSING_SANS_FONT, lookup_script_family};
pub(crate) use chars::{caps_char, push_caps};
pub use chars::{draws_something, east_asian_substitute_face, reported_font_name, substitute_face};
#[cfg(test)]
@@ -491,6 +493,77 @@ mod tests {
assert_eq!(lookup_family(&d, "No Such Roman Font"), MISSING_ROMAN_FONT);
}
#[test]
fn a_missing_font_not_classed_roman_is_looked_up_as_calibri() {
// Measured in Word for Windows 16.0.20625: a run font that isn't installed and has no
// alternate it can draw is drawn in Calibri whether the font table classes it as swiss,
// modern, auto, decorative or script. The generic sans substitutes don't start with
// Calibri (Carlito first, then Arial), so on Windows such a run came out in Arial.
crate::test_fonts();
let mut d = Document::new();
assert_eq!(lookup_family(&d, "No Such Swiss Font"), MISSING_SANS_FONT);
d.settings.modern_fonts = vec!["No Such Modern Font".into()];
assert_eq!(lookup_family(&d, "No Such Modern Font"), MISSING_SANS_FONT);
// An alternate that can't be drawn leaves a font that isn't roman in Calibri too.
d.settings.font_alt_names = vec![("No Such Brand Font".into(), "No Such Alternate".into())];
assert_eq!(lookup_family(&d, "No Such Brand Font"), MISSING_SANS_FONT);
// The other rules still come first, and these fonts keep their name.
d.settings.font_alt_names.push(("No Such Mono Brand".into(), "Courier New".into()));
assert_eq!(lookup_family(&d, "No Such Mono Brand"), "Courier New");
d.settings.roman_fonts = vec!["No Such Roman Font".into()];
assert_eq!(lookup_family(&d, "No Such Roman Font"), MISSING_ROMAN_FONT);
let installed = wordcraft_fonts::FALLBACK_FAMILY;
assert_eq!(lookup_family(&d, installed), installed);
for kept in ["Wingdings", "Angsana New", "Times New Roman", "Liberation Sans;Arial"] {
assert_eq!(lookup_family(&d, kept), kept, "{kept}");
}
}
#[test]
fn a_missing_font_named_after_a_core_font_is_drawn_in_that_font() {
// Measured in Word for Windows 16.0.20625: a missing font whose name starts with Arial or
// Helv is drawn in Arial, with Times in Times New Roman, with Courier in Courier New, with
// Segoe UI in Segoe UI, whatever its family class; its font table alternate still comes
// first. Other names (Verdana MT, Georgia Foo, Foo Arial) are drawn in Calibri.
crate::test_fonts();
let mut d = Document::new();
d.settings.roman_fonts = vec!["Arial Foo".into(), "Times MT Missing".into()];
d.settings.modern_fonts = vec!["Helvetica Foo".into()];
for (font, drawn) in [
("Arial MT", "Arial"),
("ArialFoo", "Arial"),
("Arial Foo", "Arial"),
("Helvetica Foo", "Arial"),
("Helv Foo", "Arial"),
("Times MT Missing", "Times New Roman"),
("Times New Roman Foo", "Times New Roman"),
("Courier Foo", "Courier New"),
("Segoe UI Foo", "Segoe UI"),
("Verdana MT", MISSING_SANS_FONT),
("Georgia Foo", MISSING_SANS_FONT),
("Foo Arial", MISSING_SANS_FONT),
] {
assert_eq!(lookup_family(&d, font), drawn, "{font}");
}
d.settings.font_alt_names = vec![("Arial Foo".into(), "Courier New".into())];
assert_eq!(lookup_family(&d, "Arial Foo"), "Courier New");
}
#[test]
fn a_missing_east_asian_or_complex_script_font_keeps_its_name() {
// Calibri has no CJK, Arabic or Hebrew: a missing East Asian or complex-script font with no
// alternate keeps its name, so the script's own fallback draws it (Word: MS Mincho for
// Chinese text in a missing `w:eastAsia` font).
crate::test_fonts();
let mut d = Document::new();
assert_eq!(lookup_script_family(&d, "No Such East Asian Font"), "No Such East Asian Font");
// Its alternate and the roman rule still apply.
d.settings.font_alt_names = vec![("No Such East Asian Font".into(), "Courier New".into())];
assert_eq!(lookup_script_family(&d, "No Such East Asian Font"), "Courier New");
d.settings.roman_fonts = vec!["No Such Arabic Font".into()];
assert_eq!(lookup_script_family(&d, "No Such Arabic Font"), MISSING_ROMAN_FONT);
}
/// #5847: shrink_line_spaces is a no-op when there are no spaces after the line's last tab,
/// guarding against division by zero, negative room, or shifting pre-tab clusters.
#[test]
+9 -5
View File
@@ -1,7 +1,7 @@
use super::bidi::{Dir, bidi_levels, strong_rtl, visual_order};
use super::chars::{
SegKey, caps_expands, context_digit_dirs, draws_something, is_khmer, is_mark, is_numeral_digit, ligature_features, lookup_family, remap_range,
reported_font_name, thai_run_face, upper_char,
SegKey, caps_expands, context_digit_dirs, draws_something, is_khmer, is_mark, is_numeral_digit, ligature_features, lookup_family,
lookup_script_family, remap_range, reported_font_name, thai_run_face, upper_char,
};
use super::grid::east_asian;
use super::model::{ClKind, Cluster, Enclosed, Glyph, ParaEnv, RubyLayout, StyleRun, SymbolRun};
@@ -261,7 +261,7 @@ impl<'a> Builder<'a> {
// A font isn't installed when it was substituted, or looked up as a missing roman font or
// its font table alternate (#2702).
let resolved_cs = rc_cs.as_ref().map(|r| {
let family = lookup_family(self.env.doc, &r.font);
let family = lookup_script_family(self.env.doc, &r.font);
let resolved = wordcraft_fonts::word::resolve(family, r.bold, r.italic);
(resolved.face, resolved.substituted || !family.eq_ignore_ascii_case(&r.font))
});
@@ -269,7 +269,7 @@ impl<'a> Builder<'a> {
// Chinese, Japanese and Korean characters use the run's East Asian font (`w:eastAsia`) when
// it, or the font table alternate it is drawn in (#2702), is installed and has them.
let wanted_ea = rc.font_ea.as_deref().filter(|f| !f.eq_ignore_ascii_case(&rc.font) && text.chars().any(east_asian)).map(|f| {
let family = lookup_family(self.env.doc, f);
let family = lookup_script_family(self.env.doc, f);
let resolved = wordcraft_fonts::word::resolve(family, rc.bold, rc.italic);
(f, resolved, resolved.substituted || !family.eq_ignore_ascii_case(f))
});
@@ -336,7 +336,11 @@ impl<'a> Builder<'a> {
};
let rc = &rc;
let small = key.small;
let si = self.style(rc, key.face, small);
// A complex-script segment the run's complex-script face covers is drawn in that face,
// looked up as a script font ([`lookup_script_family`]): `style` would look its font
// up as a run font, in Calibri or a core font, which have no Arabic or Hebrew.
let face = key.face.or(primary_cs.filter(|_| key.complex));
let si = self.style(rc, face, small);
let Some(st) = self.styles.get(si as usize).cloned() else { continue };
let upper = rc.caps || small;
// All Caps and Small Caps draw a letter's full capital, which can be several
+61 -17
View File
@@ -121,22 +121,44 @@ pub(crate) fn remap_range(map_starts: &[usize], orig_of: &[usize], orig_len: usi
}
/// Word draws a font that isn't installed in Cambria when the document's font table classes it
/// as roman (measured in Word; Calibri otherwise, which our own substitutes already match).
/// as roman (measured in Word).
pub(super) const MISSING_ROMAN_FONT: &str = "Cambria";
/// The family to look `font` up as. A font that isn't installed is drawn, as Word draws it
/// (measured in Word 2024, #2702):
/// Word draws a run font that isn't installed in Calibri when the font table doesn't class it as
/// roman and it has no alternate that can be drawn: measured in Word for Windows 16.0.20625 for a
/// font classed swiss, modern, auto, decorative or script. The generic sans substitutes start
/// with Carlito, Calibri's metric twin, then Arial, so without this rule Windows drew Arial.
pub(super) const MISSING_SANS_FONT: &str = "Calibri";
/// The family to look a run's font `font` up as. A font that isn't installed is drawn, as Word
/// draws it (measured in Word 2024, #2702):
/// 1. in the alternate the font table names for it (`w:altName`), when that one can be drawn:
/// installed, or a font one of whose own substitutes is installed. That comes before the
/// family class: a roman font with an installed alternate is drawn in the alternate;
/// 2. in [`MISSING_ROMAN_FONT`] when the font table classes it as roman, or as modern with an
/// alternate that can't be drawn either (a modern font with no alternate, and a swiss one with
/// or without, take the generic substitutes, as Calibri does in Word). Not for a Thai font
/// (drawn in a Thai face, #1549), a symbol font, or 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);
/// 3. else as `font`, which [`wordcraft_fonts::word::resolve`] substitutes.
/// 2. in the core font its name starts with ([`core_font_named_by`]: `Arial MT` in Arial), whatever
/// its family class;
/// 3. in [`MISSING_ROMAN_FONT`] when the font table classes it as roman, or as modern with an
/// alternate that can't be drawn either;
/// 4. in [`MISSING_SANS_FONT`] otherwise: a swiss font with or without an alternate, and a modern
/// one with none.
///
/// 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`,
/// which [`wordcraft_fonts::word::resolve`] substitutes. Rules 2 and 4 also skip a fallback list
/// (`Liberation Sans;Arial`, #5843), which its own candidates draw.
pub(super) fn lookup_family<'f>(doc: &'f Document, font: &'f str) -> &'f str {
lookup(doc, font, true)
}
/// The family to look a run's East Asian (`w:eastAsia`) or complex-script (`w:cs`) font up as:
/// rules 1 and 3 of [`lookup_family`], not the core fonts of rule 2 nor the Calibri of rule 4,
/// which have no CJK; a missing one keeps its name, and the script's own fallback draws it.
pub(super) fn lookup_script_family<'f>(doc: &'f Document, font: &'f str) -> &'f str {
lookup(doc, font, false)
}
fn lookup<'f>(doc: &'f Document, font: &'f str, run_font: bool) -> &'f str {
let settings = &doc.settings;
// A fallback list (`Liberation Serif;Times New Roman`) is in a class when one of its
// candidates is.
@@ -145,7 +167,7 @@ pub(super) fn lookup_family<'f>(doc: &'f Document, font: &'f str) -> &'f str {
};
let alternate = settings.font_alt_names.iter().find(|(f, _)| f.eq_ignore_ascii_case(font)).map(|(_, alt)| alt.as_str());
let roman = listed(&settings.roman_fonts);
if alternate.is_none() && !roman {
if alternate.is_none() && !roman && (!run_font || font.trim().is_empty()) {
return font;
}
let db = wordcraft_fonts::FontDb::global();
@@ -165,18 +187,40 @@ pub(super) fn lookup_family<'f>(doc: &'f Document, font: &'f str) -> &'f str {
}
let modern_alternate_missing = alternate.is_some() && listed(&settings.modern_fonts);
// Thai, symbol and fonts with substitutes of their own keep their name: those substitutes
// draw them, Cambria wouldn't.
if (roman || modern_alternate_missing)
&& !wordcraft_fonts::word::is_thai_family(font)
&& !wordcraft_doc::symbol_fonts::is_symbol_font(font)
&& !wordcraft_fonts::word::has_named_substitutes(font)
{
// draw them, Cambria and Calibri wouldn't.
let keeps_name = wordcraft_fonts::word::is_thai_family(font)
|| wordcraft_doc::symbol_fonts::is_symbol_font(font)
|| wordcraft_fonts::word::has_named_substitutes(font);
if keeps_name {
font
} else if let Some(core) = run_font.then(|| core_font_named_by(font)).flatten() {
core
} else if roman || modern_alternate_missing {
MISSING_ROMAN_FONT
} else if run_font && !font.contains(';') {
MISSING_SANS_FONT
} else {
font
}
}
/// The font Word draws a missing font in when its name starts with a core font's (measured in
/// Word for Windows 16.0.20625, whatever the family class): Arial for `Arial MT`, `ArialFoo`,
/// `Arial Narrow Foo` and the Helvetica names (`Helv…`), Times New Roman for `Times…`, Courier New
/// for `Courier…`, Segoe UI for `Segoe UI…`. Only the start counts (`Foo Arial` is drawn in
/// Calibri), and no other installed font is matched this way (`Verdana MT`, `Tahoma Foo` and
/// `Georgia Foo` are drawn in Calibri). A fallback list (`;`) is left to its candidates.
fn core_font_named_by(font: &str) -> Option<&'static str> {
if font.contains(';') {
return None;
}
let f = font.trim().to_ascii_lowercase();
[("arial", "Arial"), ("helv", "Arial"), ("times", "Times New Roman"), ("courier", "Courier New"), ("segoe ui", "Segoe UI")]
.into_iter()
.find(|(prefix, _)| f.starts_with(prefix))
.map(|(_, core)| core)
}
/// One face for a run in a Thai font that isn't installed (#1549): Word substitutes one font
/// (Cordia New) for the whole run, so the Latin letters, digits and ฿ keep the Thai letters' face
/// and size. `None` unless `font` is a Thai family, it was substituted (or drawn in its font
+8 -5
View File
@@ -1502,7 +1502,7 @@ fn a_missing_font_is_drawn_in_its_font_table_alternate() {
// Measured in Word 2024 and Word for Windows 16.0.20527 (#2702): a font that isn't installed is
// drawn in the alternate its font table names (`w:altName`), before the roman rule; an
// alternate that can't be drawn either leaves the family class in charge: Cambria for a roman
// or modern font, the generic substitutes (Calibri in Word) for a swiss one.
// or modern font, Calibri for a swiss one.
#[derive(Clone, Copy)]
enum Family {
Swiss,
@@ -1529,7 +1529,10 @@ fn a_missing_font_is_drawn_in_its_font_table_alternate() {
let face = |alt: Option<&str>, roman: bool| face_of(alt, if roman { Family::Roman } else { Family::Swiss });
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.
let generic = face(None, false);
assert_eq!(generic, calibri);
// Serif and sans faces are bundled, so these differ on any machine.
assert_ne!(times, generic);
assert_ne!(cambria, generic);
@@ -1541,16 +1544,16 @@ fn a_missing_font_is_drawn_in_its_font_table_alternate() {
// D: the alternate beats the roman rule.
assert_eq!(face(Some("Times New Roman"), true), times);
assert_eq!(face(Some("Courier New"), true), courier);
// E: an alternate that is missing too, with no substitutes of its own: the roman rule, or the
// generic substitutes for a font that isn't roman.
// E: an alternate that is missing too, with no substitutes of its own: the roman rule, or
// Calibri for a font that isn't roman.
assert_eq!(face(Some("No Such Alternate"), true), cambria);
assert_eq!(face(Some("No Such Alternate"), false), generic);
// Word reads a value with a comma as one name, not a list (MS-OE376 2.1.264): no font is
// called that, so the roman rule or the generic substitutes draw it.
// called that, so the roman rule or Calibri draws it.
assert_eq!(face(Some("No Such Alternate, Times New Roman"), false), generic);
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 takes the generic substitutes (Calibri in Word).
// 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);
+23
View File
@@ -355,3 +355,26 @@ fn khmer_ignores_the_ligature_setting() {
assert_eq!(gids(Some(hd)), gids(None));
assert_eq!(gids(Ligatures::from_ooxml("none")), gids(None));
}
#[test]
fn a_complex_script_run_is_drawn_in_its_script_font_lookup() {
crate::test_fonts();
// A missing complex-script font is looked up as a script font (#7064): it keeps its name,
// and neither the core font its name starts with nor Calibri draws it. The text it covers is
// shaped in that face, the one checked for coverage and reported as the substitute, not in
// the run font lookup's (Courier New for a missing `Courier Foo`).
let font = "Courier Foo 7064";
let mut d = Document::from_text("Hello");
let p = d.para_mut(StoryRef::Body, &Path::top(0)).unwrap();
p.runs[0].props.cs = Some(true);
p.runs[0].props.font_cs = Some(font.into());
let l = lay(&d);
let pl = para_layout(&l, 0);
let script = wordcraft_fonts::word::resolve(font, false, false).face;
let core = wordcraft_fonts::word::resolve("Courier New", false, false).face;
assert_ne!(script.id(), core.id(), "the two lookups differ on this machine");
assert!(script.covers('H'));
for c in &pl.clusters {
assert_eq!(pl.styles[c.style as usize].face.id(), script.id(), "{}", pl.styles[c.style as usize].face.family);
}
}
File diff suppressed because one or more lines are too long