mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-03 22:40:27 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de7d17fb7d | ||
|
|
caf58cd52a | ||
|
|
0c461485fb | ||
|
|
db6a9105de | ||
|
|
d6a8db16b4 | ||
|
|
7addc314f9 |
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
Reference in New Issue
Block a user