Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

FONT4: resolve composite fonts and fallback for generated note labels

Shao Wenbin06ed81e547d5
diff --git a/ROADMAP.md b/ROADMAP.md
index 9a319dc24..2fc7e107c 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -1,5 +1,7 @@
# DesignCraft roadmap
+- 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.
- AR1: Arabic IDML controls now retain character direction, kashida switches, diacritic offsets/forms, paragraph policy values and independent story/table directions. Shaping preserves joining context and script/language selection; paragraph-level bidi, contextual digit conversion, mark-safe elongation, RTL columns/tables and physical column insertion have regression coverage. Vendor-specific justification and diacritic presets remain explicit preflight warnings; see docs/arabic-typography.md.
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 5a3056d8f..0a67a3186 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -94,6 +94,8 @@ impl RunStyle {
/// A positioned glyph. `x` is absolute in frame inner space; `y` is relative to the line baseline.
#[derive(Clone, Debug)]
pub struct PlacedGlyph {
+ /// Actual character drawn, including generated labels.
+ pub rendered_char: char,
pub face: designcraft_fonts::FaceRef,
pub gid: u32,
pub x: f64,
@@ -1867,6 +1869,7 @@ fn tab_leader(tab: &Glyph, leader: &str, x: f64, w: f64, origin: f64, out: &mut
while at + pw <= end + 0.01 && n < 2000 {
for &(gid, adv) in &glyphs {
out.push(PlacedGlyph {
+ rendered_char: '\t',
face: tab.face,
gid,
x: at,
@@ -1970,6 +1973,7 @@ fn place(g: &Glyph, x: f64) -> PlacedGlyph {
|| g.ch == shape::SOFT_HYPHEN
|| g.ch == shape::HIDDEN);
PlacedGlyph {
+ rendered_char: g.rendered_char,
face: g.face,
gid: g.gid,
x: x + g.dx,
diff --git a/crates/compose/src/ruby.rs b/crates/compose/src/ruby.rs
index cea4a5545..b39f11eef 100644
--- a/crates/compose/src/ruby.rs
+++ b/crates/compose/src/ruby.rs
@@ -22,6 +22,7 @@ fn set(base: &PlacedGlyph, text: &str, size: f64, base_size: f64) -> (Vec<Placed
let ch = text[sg.cluster..].chars().next().unwrap_or(' ');
let adv = sg.x_advance as f64 * k;
out.push(PlacedGlyph {
+ rendered_char: ch,
gid: sg.gid,
x: x + sg.x_offset as f64 * k,
y: y - sg.y_offset as f64 * k,
diff --git a/crates/compose/src/shape.rs b/crates/compose/src/shape.rs
index fa38901dd..fdba938b0 100644
--- a/crates/compose/src/shape.rs
+++ b/crates/compose/src/shape.rs
@@ -54,6 +54,8 @@ pub struct Glyph {
pub leading_model: designcraft_doc::cjk::LeadingModel,
pub character_direction: designcraft_doc::arabic::CharacterDirection,
pub display_char: char,
+ /// Actual shaped character, including generated labels, for glyph diagnostics.
+ pub rendered_char: char,
pub bidi_offset: usize,
pub bidi_end: usize,
pub allow_kashidas: bool,
@@ -300,7 +302,32 @@ pub(crate) fn shape_para(
if m.starts_with(designcraft_doc::ENDNOTE_REF) {
shape_run(db, &story.text, i..e, &eprops, eenv, estyle, sub, &mut glyphs);
} else {
- shape_run(db, &story.text, i..e, &rprops, renv, rstyle, sub, &mut glyphs);
+ if let Some(composite) = styles.composite_font(&rprops.font_family) {
+ let label = sub.notes.get(&i).map_or("#", String::as_str);
+ let begin = glyphs.len();
+ let mut segments: Vec<(usize, usize, Option<&designcraft_doc::cjk::CompositeFontEntry>)> = Vec::new();
+ for (at, c) in label.char_indices() {
+ let entry = composite.entry(c);
+ if let Some((_, end, _)) = segments.last_mut().filter(|(_, _, previous)| *previous == entry) {
+ *end = at + c.len_utf8();
+ } else {
+ segments.push((at, at + c.len_utf8(), entry));
+ }
+ }
+ for (a, b, entry) in segments {
+ let props = composite_props(db, &rprops, entry);
+ let style = table.intern(db, &props);
+ let env = table.env_for(auto_leading, style);
+ let face = db.face(&props.font_family, &props.font_style);
+ shape_segment(db, &story.text, i..e, label.get(a..b), &props, &face, env, style, &mut glyphs, sub.vertical);
+ }
+ for (index, glyph) in glyphs[begin..].iter_mut().enumerate() {
+ glyph.len = if index == 0 { e - i } else { 0 };
+ glyph.no_break = true;
+ }
+ } else {
+ shape_run(db, &story.text, i..e, &rprops, renv, rstyle, sub, &mut glyphs);
+ }
}
k = e;
}
@@ -778,6 +805,7 @@ fn control_glyph(face: &Arc<FontFace>, p: &CharProps, auto_leading: TypeEnv, sty
character_alignment: p.character_alignment,
leading_model: p.leading_model,
character_direction: p.character_direction,
+ rendered_char: ch,
display_char: ch,
bidi_offset: 0,
bidi_end: 0,
@@ -802,7 +830,32 @@ fn shape_segment(
out: &mut Vec<Glyph>,
vertical: bool,
) {
- let _ = db;
+ // Generated labels need the same coverage fallback as ordinary text. Keep the
+ // original marker address and one carrier even when several faces draw the label.
+ if let Some(label) = replacement.filter(|_| auto_leading.glyph_fallback) {
+ let lang = designcraft_doc::language_tag(&p.language);
+ let mut segments: Vec<(usize, usize, Arc<FontFace>)> = Vec::new();
+ for (at, c) in label.char_indices() {
+ let selected = if face.covers(c) { face.clone() } else { db.fallback_for(c, face.id(), lang).unwrap_or_else(|| face.clone()) };
+ if let Some((_, end, previous)) = segments.last_mut().filter(|(_, _, previous)| previous.id() == selected.id()) {
+ *end = at + c.len_utf8();
+ let _ = previous;
+ } else {
+ segments.push((at, at + c.len_utf8(), selected));
+ }
+ }
+ if segments.iter().any(|(_, _, selected)| selected.id() != face.id()) {
+ let begin = out.len();
+ for (a, b, selected) in segments {
+ shape_segment(db, text, range.clone(), label.get(a..b), p, &selected, auto_leading, style, out, vertical);
+ }
+ for (i, glyph) in out[begin..].iter_mut().enumerate() {
+ glyph.len = if i == 0 { range.len() } else { 0 };
+ glyph.no_break = true;
+ }
+ return;
+ }
+ }
let (size, k, ascent, descent, leading, cap, xh, shift) = metrics(face, p, auto_leading);
let hs = p.h_scale;
let tracking = p.tracking / 1000.0 * p.size;
@@ -896,6 +949,7 @@ fn shape_segment(
character_alignment: p.character_alignment,
leading_model: p.leading_model,
character_direction: p.character_direction,
+ rendered_char: src.get(sg.cluster..).and_then(|s| s.chars().next()).unwrap_or(ch),
display_char: if replacement.is_some() && matches!(ch, '0'..='9' | '\u{0660}'..='\u{0669}' | '\u{06F0}'..='\u{06F9}') {
src.chars().next().unwrap_or(ch)
} else {
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index fe779d053..860f9284c 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -1916,3 +1916,46 @@ fn composite_metrics_and_character_mappings_apply_inside_footnotes() {
assert!((style.size - 9.6).abs() < 1e-8);
assert!(!note.text.styles.iter().any(|s| s.missing_font));
}
+
+#[test]
+fn generated_note_labels_resolve_composite_components_and_keep_one_anchor() {
+ use designcraft_doc::cjk::{CompositeFont, CompositeFontEntry};
+ let (mut d, sid, _) = doc_with("Body", Rect::new(0.0, 0.0, 300.0, 300.0), ParaAttrs::default());
+ d.styles_mut().composite_fonts.push(CompositeFont {
+ name: "Generated Mixed".into(),
+ entries: vec![
+ CompositeFontEntry { family: "Source Serif 4".into(), ..Default::default() },
+ CompositeFontEntry { characters: "[]".into(), family: "Inter".into(), relative_size: 0.8, ..Default::default() },
+ ],
+ });
+ d.footnote_options.affix_in = designcraft_doc::notes::AffixIn::Both;
+ d.footnote_options.prefix = "[".into();
+ d.footnote_options.suffix = "]".into();
+ let story = d.story_mut(sid).unwrap();
+ story.insert_note(4, "Note", ParaFormat::default());
+ story.format_chars(4..7, |f| f.over.font_family = Some("Generated Mixed".into()));
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let glyphs: Vec<_> = cs.frames[0].lines.iter().flat_map(|l| &l.glyphs).filter(|g| g.byte == 4).collect();
+ assert_eq!(glyphs.iter().filter(|g| g.len > 0).count(), 1);
+ assert_eq!(glyphs.iter().map(|g| g.rendered_char).collect::<String>(), "[1]");
+ assert!(glyphs.iter().all(|g| g.gid != 0));
+ assert!(glyphs.iter().filter(|g| matches!(g.rendered_char, '[' | ']')).all(|g| g.face.family == "Inter"));
+ assert_eq!(glyphs.iter().find(|g| g.rendered_char == '1').unwrap().face.family, "Source Serif 4");
+ assert_eq!(cs.frames[0].notes.len(), 1);
+}
+
+#[test]
+fn generated_note_labels_use_coverage_fallback() {
+ let font = designcraft_fonts::testing::font_with("Generated Label Coverage", &['\u{E888}']).unwrap();
+ designcraft_fonts::FontDb::global().add_font(font);
+ let (mut d, sid, _) = doc_with("Body", Rect::new(0.0, 0.0, 300.0, 300.0), ParaAttrs::default());
+ d.footnote_options.affix_in = designcraft_doc::notes::AffixIn::Both;
+ d.footnote_options.prefix = "\u{E888}".into();
+ d.footnote_options.suffix = "".into();
+ d.settings.glyph_fallback = true;
+ d.story_mut(sid).unwrap().insert_note(4, "Note", ParaFormat::default());
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let glyph = cs.frames[0].lines.iter().flat_map(|l| &l.glyphs).find(|g| g.rendered_char == '\u{E888}').unwrap();
+ assert_ne!(glyph.gid, 0);
+ assert_eq!(glyph.face.family, "Generated Label Coverage");
+}