DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
FONT5: diagnose missing glyphs in generated text with frame locations
Shao Wenbin39482d1b8c5d
diff --git a/ROADMAP.md b/ROADMAP.mdindex 2fc7e107c..ae8fe49ad 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -1,5 +1,7 @@# DesignCraft roadmap+- FONT5: missing-glyph preflight inspects rendered characters in generated labels, including zero-length continuation glyphs, and identifies the owning page frame for nested text.+- FONT4: generated note labels resolve composite character mappings and glyph fallback while retaining a single source anchor; composed glyphs retain the actual rendered character for diagnostics.- AR1.1: Kashida justification tests verify space fallback without Tatweel fonts using an isolated font database, while retaining elongation assertions when fonts support it.diff --git a/crates/engine/src/cmd/preflight.rs b/crates/engine/src/cmd/preflight.rsindex 7e603206d..03b6412c9 100644--- a/crates/engine/src/cmd/preflight.rs+++ b/crates/engine/src/cmd/preflight.rs@@ -38,35 +38,35 @@ fn page_of(d: &Document, sr: SpreadRef, it: &Item) -> Option<usize> {/// Characters a missing-glyph report names before "and N more".const MISSING_GLYPHS_LISTED: usize = 20;-/// Add the characters drawn as their font's missing glyph (.notdef) in `cs`, composed from-/// `text` (table cells and footnotes included), to `out` by face, each once. Text in a missing-/// font is left to the missing-font report; generated text (list labels, page numbers) and-/// markers have no character of their own to name.-fn missing_glyphs(cs: &ComposedStory, text: &str, depth: usize, out: &mut Vec<(FaceRef, Vec<char>)>) {- // Cells and footnotes hold composed stories of their own; tables nest only so deep.+/// Inspect rendered characters, including zero-length glyphs generated by a source marker.+/// Nested notes and cells report the owning page frame, not their synthetic frame zero.+fn missing_glyphs(+ cs: &ComposedStory,+ owner: Option<designcraft_doc::ItemId>,+ depth: usize,+ out: &mut Vec<(FaceRef, Vec<char>, designcraft_doc::ItemId)>,+) {if depth > 16 {return;}for f in &cs.frames {- for g in f.lines.iter().flat_map(|l| &l.glyphs).filter(|g| g.visible && g.gid == 0 && g.len > 0) {- if cs.styles.get(g.style as usize).is_none_or(|st| st.missing_font) {- continue;- }- let Some(c) = text.get(g.byte..).and_then(|t| t.chars().next()) else { continue };+ let owner = owner.unwrap_or(f.frame);+ for g in f.lines.iter().flat_map(|l| &l.glyphs).filter(|g| g.visible && g.gid == 0) {+ let c = g.rendered_char;if c.is_control() || c.is_whitespace() || ('\u{E000}'..='\u{E1FF}').contains(&c) {continue;}- match out.iter_mut().find(|(face, _)| *face == g.face) {- Some((_, chars)) if !chars.contains(&c) => chars.push(c),+ match out.iter_mut().find(|(face, _, item)| *face == g.face && *item == owner) {+ Some((_, chars, _)) if !chars.contains(&c) => chars.push(c),Some(_) => {}- None => out.push((g.face, vec![c])),+ None => out.push((g.face, vec![c], owner)),}}for c in f.tables.iter().flat_map(|t| &t.cells) {- missing_glyphs(&c.text, &c.source, depth + 1, out);+ missing_glyphs(&c.text, Some(owner), depth + 1, out);}for n in &f.notes {- missing_glyphs(&n.text, &n.source, depth + 1, out);+ missing_glyphs(&n.text, Some(owner), depth + 1, out);}}}@@ -78,10 +78,10 @@ pub fn check(s: &Session, min_ppi: f64) -> Vec<Issue> {// Stories: overset and fonts.let mut missing_fonts: Vec<String> = Vec::new();let mut unsupported_typography = std::collections::BTreeSet::new();- let mut missing: Vec<(FaceRef, Vec<char>)> = Vec::new();+ let mut missing: Vec<(FaceRef, Vec<char>, designcraft_doc::ItemId)> = Vec::new();for story in d.stories.values() {let cs = s.cache.get(d, story.id, None);- missing_glyphs(&cs, &story.text, 0, &mut missing);+ missing_glyphs(&cs, None, 0, &mut missing);if cs.is_overset() {let last = story.frames.last().copied();let page = last.and_then(|f| d.find(f).zip(d.item(f))).and_then(|(loc, it)| page_of(d, loc.spread, it));@@ -144,7 +144,7 @@ pub fn check(s: &Session, min_ppi: f64) -> Vec<Issue> {for message in unsupported_typography {out.push(Issue { severity: "warning", kind: "unsupportedTypography", message, item: None, page: None });}- for (face, chars) in missing {+ for (face, chars, item) in missing {let listed: Vec<String> = chars.iter().take(MISSING_GLYPHS_LISTED).map(char::to_string).collect();let more = chars.len().saturating_sub(MISSING_GLYPHS_LISTED);let more = if more > 0 { format!(" and {more} more") } else { String::new() };@@ -152,8 +152,8 @@ pub fn check(s: &Session, min_ppi: f64) -> Vec<Issue> {severity: "error",kind: "missingGlyphs",message: format!("Missing glyphs in {} {}: {}{more}", face.family, face.style, listed.join(" ")),- item: None,- page: None,+ item: Some(item.0),+ page: d.find(item).zip(d.item(item)).and_then(|(loc, it)| page_of(d, loc.spread, it)),});}// Items.@@ -317,3 +317,26 @@ mod missing_glyph_tests {assert!(found[0].1.ends_with(" and 10 more"), "{}", found[0].1);}}++#[cfg(test)]+mod generated_glyph_tests {+ use super::*;+ use std::sync::Arc;++ #[test]+ fn generated_suffix_missing_glyph_is_reported_at_its_page_frame() {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let created = s.execute("frame.create", &json!({"rect":[20,20,300,400],"content":"text","text":"Body"})).unwrap();+ let d = Arc::make_mut(&mut s.doc_mut().unwrap().doc);+ let sid = *d.stories.keys().next().unwrap();+ d.footnote_options.affix_in = designcraft_doc::notes::AffixIn::Reference;+ d.footnote_options.suffix = "\u{10fffd}".into();+ d.story_mut(sid).unwrap().insert_note(4, "Note", designcraft_doc::ParaFormat::default());+ let report = s.execute("preflight.run", &json!({})).unwrap();+ let issue = report["issues"].as_array().unwrap().iter().find(|i| i["kind"] == "missingGlyphs").unwrap();+ assert!(issue["message"].as_str().unwrap().contains('\u{10fffd}'));+ assert_eq!(issue["item"], created["id"]);+ assert_eq!(issue["page"], 0);+ }+}