mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-04 22:49:54 +00:00
Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3916dc6bdc | ||
|
|
ead0870d33 | ||
|
|
0aaef7a5bd | ||
|
|
be5fe3d36f | ||
|
|
f053740960 | ||
|
|
7581392111 | ||
|
|
04e8e61e82 | ||
|
|
bf95e83642 | ||
|
|
bec88fd149 | ||
|
|
1b34471bc8 | ||
|
|
44badab540 | ||
|
|
e209b4f6fd | ||
|
|
68de4560b5 | ||
|
|
b1c6aafc33 | ||
|
|
45299ba839 | ||
|
|
78ea592cf4 | ||
|
|
c1a20368dc | ||
|
|
32d4d32292 |
@@ -16,6 +16,7 @@ workspace = true
|
||||
[dependencies]
|
||||
wordcraft-doc = { workspace = true }
|
||||
wordcraft-engine = { workspace = true }
|
||||
wordcraft-fonts = { workspace = true }
|
||||
wordcraft-render = { workspace = true }
|
||||
wordcraft-mcp = { workspace = true }
|
||||
wordcraft-zotero = { workspace = true }
|
||||
|
||||
@@ -523,6 +523,7 @@ fn run_convert(rest: &[String], open: impl Fn(&str, Option<&str>) -> Result<Sess
|
||||
}
|
||||
let password = password_from(rest)?;
|
||||
let mut s = open(&i, password.as_deref())?;
|
||||
report_missing_metric_fonts();
|
||||
if rest.iter().any(|a| a == "--update-fields") {
|
||||
update_fields(&mut s)?;
|
||||
}
|
||||
@@ -563,6 +564,16 @@ fn run_source_paths(rest: &[String], script: Option<&script::Script>) -> (Option
|
||||
(from_file, from_template)
|
||||
}
|
||||
|
||||
fn report_missing_metric_fonts() {
|
||||
for (word, used) in wordcraft_fonts::word::missing_metric_fonts() {
|
||||
use std::io::Write as _;
|
||||
let _ = writeln!(
|
||||
std::io::stderr().lock(),
|
||||
"note: {word} and its metric-compatible twins aren't installed; the primary family is {used}, so lines and pages may not match Word's; uncovered characters may use glyph fallback"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Run the subcommand named by `args` (the command line after the program name). Files named
|
||||
/// there are opened by their real names, which may not be valid Unicode.
|
||||
fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
|
||||
@@ -14933,7 +14933,7 @@ fn export_png_refuses_a_path_that_is_not_png() {
|
||||
#[test]
|
||||
fn ime_body_epoch_marks_body_replacements_not_edits() {
|
||||
let mut s = s();
|
||||
let id = s.document_id();
|
||||
let document_id = s.document_id();
|
||||
run(&mut s, "text.insert", json!({"text": "one"}));
|
||||
let first = s.ime_body_epoch();
|
||||
run(&mut s, "text.insert", json!({"text": " two"}));
|
||||
@@ -14942,7 +14942,7 @@ fn ime_body_epoch_marks_body_replacements_not_edits() {
|
||||
run(&mut s, "document.setText", json!({"text": "same text"}));
|
||||
let replaced = s.ime_body_epoch();
|
||||
assert!(replaced > first, "setText replaces the body under the same document id");
|
||||
assert_eq!(s.document_id(), id, "the document id itself is unchanged");
|
||||
assert_eq!(s.document_id(), document_id, "the document id itself is unchanged");
|
||||
|
||||
// The same text again still replaces the body: a composition must not survive on content
|
||||
// equality.
|
||||
|
||||
@@ -483,3 +483,17 @@ fn named_instances_and_collection_members_are_capped() {
|
||||
// (under 0.1 s on a loaded machine) only backs up "read once", with room for a busy CI.
|
||||
assert!(started.elapsed() < std::time::Duration::from_secs(10), "{:?}", started.elapsed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_calibri_without_carlito_reports_the_drawn_fallback() {
|
||||
let db = FontDb::with_font_dirs(Vec::new());
|
||||
db.set_system_fallback(false);
|
||||
db.faces.write().unwrap().retain(|face| !face.family.eq_ignore_ascii_case("Carlito"));
|
||||
assert!(!db.has_family("Carlito"));
|
||||
|
||||
let missing = crate::word::missing_metric_fonts_in(&db);
|
||||
let used = missing.iter().find(|(family, _)| *family == "Calibri").map_or("", |(_, used)| used.as_str());
|
||||
assert!(!used.is_empty(), "Calibri should be reported without Carlito");
|
||||
assert_ne!(used, "Carlito");
|
||||
assert!(db.has_family(used), "the notice names the face actually drawn");
|
||||
}
|
||||
|
||||
@@ -76,10 +76,10 @@ fn substitutes_by_name(family: &str) -> (&'static [&'static str], bool) {
|
||||
}
|
||||
}
|
||||
const SERIF: &[&str] = &["Caladea", "Times New Roman", "Liberation Serif", "Georgia", "Noto Serif", "DejaVu Serif", "Source Serif 4"];
|
||||
const MONO: &[&str] = &["Courier New", "Liberation Mono", "Menlo", "Consolas", "DejaVu Sans Mono", "JetBrains Mono"];
|
||||
const ARIAL: &[&str] = &["Liberation Sans", "Helvetica", "Helvetica Neue", "Arimo", "Source Sans 3"];
|
||||
const ARIAL_UNICODE: &[&str] = &["Arial", "Liberation Sans", "Helvetica", "Helvetica Neue", "Arimo", "Source Sans 3"];
|
||||
const TIMES: &[&str] = &["Liberation Serif", "Times", "Tinos", "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"];
|
||||
// Persian / Arabic document fonts. Naskh (book) faces such as B Nazanin, B Lotus, B Zar and
|
||||
// Traditional Arabic, then the sans faces (B Yekan, B Titr, IRANSans) people use for
|
||||
// headings and screens. Open fonts with full Persian coverage first.
|
||||
@@ -494,6 +494,37 @@ fn substitute(db: &FontDb, family: &str) -> Option<&'static str> {
|
||||
.or_else(|| installed.first().copied())
|
||||
}
|
||||
|
||||
/// Word's core families, each with the open fonts that share its metrics. Without the family or
|
||||
/// a twin, text set in it wraps and paginates differently from Word (Times New Roman drawn in a
|
||||
/// wider serif fits fewer lines to a page).
|
||||
const CORE_METRIC_FONTS: &[(&str, &[&str])] = &[
|
||||
("Times New Roman", &["Liberation Serif", "Tinos"]),
|
||||
("Arial", &["Liberation Sans", "Arimo"]),
|
||||
("Courier New", &["Liberation Mono", "Cousine"]),
|
||||
("Calibri", &["Carlito"]),
|
||||
("Cambria", &["Caladea"]),
|
||||
];
|
||||
|
||||
/// Word's core families not drawn with themselves or a metric twin, paired with the primary
|
||||
/// resolved family. Glyphs the primary family cannot draw may still use glyph fallback.
|
||||
pub fn missing_metric_fonts() -> Vec<(&'static str, String)> {
|
||||
missing_metric_fonts_in(FontDb::global())
|
||||
}
|
||||
|
||||
pub(crate) fn missing_metric_fonts_in(db: &FontDb) -> Vec<(&'static str, String)> {
|
||||
CORE_METRIC_FONTS
|
||||
.iter()
|
||||
.filter_map(|(word, twins)| {
|
||||
let resolved = resolve_with_db(db, word, false, false);
|
||||
if has_metric_resolution(db.has_family(word), twins, &resolved.face.family) { None } else { Some((*word, resolved.face.family.clone())) }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn has_metric_resolution(installed_word_font: bool, twins: &[&str], resolved_family: &str) -> bool {
|
||||
installed_word_font || twins.iter().any(|twin| resolved_family.eq_ignore_ascii_case(twin))
|
||||
}
|
||||
|
||||
fn style_name(bold: bool, italic: bool) -> &'static str {
|
||||
match (bold, italic) {
|
||||
(false, false) => "Regular",
|
||||
@@ -705,9 +736,7 @@ pub fn resolve(family: &str, bold: bool, italic: bool) -> Resolved {
|
||||
return *r;
|
||||
}
|
||||
}
|
||||
let (fam, substituted) = resolve_family(db, family);
|
||||
let face = db.face(&fam, style_name(bold, italic));
|
||||
let r = Resolved { synth_bold: bold && face.weight < 600.0, synth_italic: italic && !face.italic, substituted, face: FaceRef::of(&face) };
|
||||
let r = resolve_with_db(db, family, bold, italic);
|
||||
if cacheable {
|
||||
let mut c = cache.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if c.0 == generation {
|
||||
@@ -728,6 +757,12 @@ pub fn resolve(family: &str, bold: bool, italic: bool) -> Resolved {
|
||||
r
|
||||
}
|
||||
|
||||
fn resolve_with_db(db: &FontDb, family: &str, bold: bool, italic: bool) -> Resolved {
|
||||
let (fam, substituted) = resolve_family(db, family);
|
||||
let face = db.face(&fam, style_name(bold, italic));
|
||||
Resolved { synth_bold: bold && face.weight < 600.0, synth_italic: italic && !face.italic, substituted, face: FaceRef::of(&face) }
|
||||
}
|
||||
|
||||
/// A line's (ascent, descent) from hhea values, both positive: the line gap goes above the ascent.
|
||||
fn hhea_line(ascender: i16, descender: i16, line_gap: i16) -> (f64, f64) {
|
||||
(f64::from(ascender) + f64::from(line_gap.max(0)), f64::from(descender).abs())
|
||||
@@ -1094,6 +1129,48 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn core_font_notice_uses_the_actual_installed_face() {
|
||||
const SERIF: &[u8] = include_bytes!("../../../assets/fonts/SourceSerif4-Regular.ttf");
|
||||
let database = |name: Option<&str>| {
|
||||
let db = FontDb::with_font_dirs(Vec::new());
|
||||
db.set_system_fallback(false);
|
||||
if let Some(name) = name {
|
||||
assert_eq!(db.add_document_fonts(vec![(SERIF.to_vec(), name.to_owned())]), 1);
|
||||
}
|
||||
db
|
||||
};
|
||||
|
||||
let original = database(Some("Times New Roman"));
|
||||
assert!(missing_metric_fonts_in(&original).iter().all(|(family, _)| *family != "Times New Roman"));
|
||||
assert_eq!(resolve_with_db(&original, "Times New Roman", false, false).face.family, "Times New Roman");
|
||||
|
||||
let twin = database(Some("Liberation Serif"));
|
||||
assert!(missing_metric_fonts_in(&twin).iter().all(|(family, _)| *family != "Times New Roman"));
|
||||
assert_eq!(resolve_with_db(&twin, "Times New Roman", false, false).face.family, "Liberation Serif");
|
||||
|
||||
let fallback = database(None);
|
||||
assert_eq!(
|
||||
missing_metric_fonts_in(&fallback).iter().find(|(family, _)| *family == "Times New Roman"),
|
||||
Some(&("Times New Roman", "Source Serif 4".to_owned()))
|
||||
);
|
||||
assert_eq!(resolve_with_db(&fallback, "Times New Roman", false, false).face.family, "Source Serif 4");
|
||||
|
||||
let no_carlito = database(Some("DejaVu Sans"));
|
||||
assert!(!no_carlito.has_family("Carlito"));
|
||||
assert_eq!(resolve_with_db(&no_carlito, "Calibri", false, false).face.family, "DejaVu Sans");
|
||||
assert_eq!(
|
||||
missing_metric_fonts_in(&no_carlito).iter().find(|(family, _)| *family == "Calibri"),
|
||||
Some(&("Calibri", "DejaVu Sans".to_owned()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_listed_but_uninstalled_carlito_does_not_hide_the_chosen_fallback() {
|
||||
assert!(!has_metric_resolution(false, &["Carlito"], "DejaVu Sans"));
|
||||
assert!(has_metric_resolution(false, &["Carlito"], "Carlito"));
|
||||
}
|
||||
|
||||
/// Semicolon-separated fallback lists resolve to the first installed family, or to a
|
||||
/// candidate's substitute when no listed family is installed.
|
||||
#[test]
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user