DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
CJK-MOJI6: respect glyph clusters and regional punctuation metrics
Shao Wenbind9f24b6ad59f
diff --git a/ROADMAP.md b/ROADMAP.mdindex 1897b38e2..4946a9f1a 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -1,5 +1,6 @@# DesignCraft roadmap+- CJK-MOJI6: apply spacing at shaped-cluster boundaries and distinguish centered Traditional Chinese punctuation from Simplified Chinese side bearings.- CJK-MOJI5: share equal-priority boundary spacing and honor discrete compression endpoints, including leading-only ragged-line fitting.- CJK-MOJI4: resolve all public Mojikumi preset names and their distinct paragraph/line-edge policies; correct consecutive punctuation defaults and small-kana classes.diff --git a/crates/compose/src/mojikumi.rs b/crates/compose/src/mojikumi.rsindex 23dde00e3..c0d92779e 100644--- a/crates/compose/src/mojikumi.rs+++ b/crates/compose/src/mojikumi.rs@@ -8,6 +8,8 @@ use designcraft_doc::{#[derive(Clone, Copy, Debug, Default)]pub struct Metrics {pub active: bool,+ /// Shaped body width before tracking and manual character spacing.+ pub body: f64,/// Desired internal gap attached to this glyph's advance.pub gap: f64,pub stretch: f64,@@ -38,42 +40,68 @@ fn em(g: &Glyph) -> f64 {pub fn apply(glyphs: &mut [Glyph], styles: &Styles, name: &str) {let Ok(Some(table)) = Rules::resolve(styles, name) else { return };- // Remove punctuation's built-in blank half-body. The table then contributes- // the desired spacing once per boundary, rather than adding it twice.- for g in glyphs.iter_mut().filter(|g| eligible(g)) {- g.moji.active = true;- let class = rules::class(g.rendered_char);- let unit = em(g);- if g.adv >= unit * 0.8 {- let before = if g.moji.explicit_before { 0.0 } else { rules::leading(class) * unit };- let after = if g.moji.explicit_after { 0.0 } else { rules::trailing(class) * unit };- g.adv = (g.adv - before - after).max(0.0);- g.dx -= before;+ // Boundaries belong to shaped clusters, not individual glyphs. In particular+ // the advance carrying a following gap must come after all attached marks.+ let mut clusters = Vec::new();+ let mut start = 0;+ while start < glyphs.len() {+ let byte = glyphs[start].byte;+ let mut end = start + 1;+ while end < glyphs.len() && glyphs[end].byte == byte {+ end += 1;}+ clusters.push((start, end));+ start = end;}- for i in 0..glyphs.len() {- let (left, right) = glyphs.split_at_mut(i + 1);- let Some(g) = left.last_mut() else { continue };- if !g.moji.active {+ for &(start, end) in &clusters {+ let cluster = &mut glyphs[start..end];+ if !cluster.iter().any(eligible) {continue;}- let c = rules::class(g.rendered_char);- let unit = em(g);- let start = table.pair(if i == 0 { 23 } else { 22 }, c);- g.moji.start_aki = start;- g.moji.start = if g.moji.explicit_before { 0.0 } else { start.desired * unit };- g.moji.end_aki = table.pair(c, 22);+ let class = rules::class(cluster[0].rendered_char);+ let unit = em(&cluster[0]);+ let body: f64 = cluster.iter().map(|g| g.moji.body).sum();+ for g in cluster.iter_mut() {+ g.moji.active = true;+ }+ // Tracking must not make a proportional or half-width glyph appear to+ // contain a removable half-em body. Shaping features already affect body.+ if body >= unit * 0.8 {+ let before = if cluster[0].moji.explicit_before { 0.0 } else { table.leading(class) * unit };+ let after = if cluster[0].moji.explicit_after { 0.0 } else { table.trailing(class) * unit };+ let trim = before + after;+ if let Some(owner) = cluster.iter().position(|g| g.adv >= trim) {+ cluster[owner].adv -= trim;+ for (i, g) in cluster.iter_mut().enumerate() {+ g.dx += if i > owner { trim - before } else { -before };+ }+ }+ }+ let head = &mut cluster[0];+ head.moji.start_aki = table.pair(if start == 0 { 23 } else { 22 }, class);+ head.moji.start = if head.moji.explicit_before { 0.0 } else { head.moji.start_aki.desired * unit };+ }+ for (index, &(start, end)) in clusters.iter().enumerate() {+ if !glyphs[start].moji.active {+ continue;+ }+ let class = rules::class(glyphs[start].rendered_char);+ let unit = em(&glyphs[start]);let mut gap = Aki::default();- if let Some(next) = right.first().filter(|n| n.moji.active && n.byte != g.byte)- && !g.moji.explicit_after- && !next.moji.explicit_before+ if let Some(&(next, _)) = clusters.get(index + 1)+ && glyphs[next].moji.active+ && !glyphs[end - 1].moji.explicit_after+ && !glyphs[next].moji.explicit_before{- gap = table.pair(c, rules::class(next.rendered_char));+ gap = table.pair(class, rules::class(glyphs[next].rendered_char));}- let desired = (gap.desired * unit).max(-g.adv);+ let width: f64 = glyphs[start..end].iter().map(|g| g.adv).sum();+ let g = &mut glyphs[end - 1];+ g.moji.end_aki = table.pair(class, 22);+ let desired = (gap.desired * unit).max(-width);g.moji.gap = desired;g.moji.stretch = (gap.max * unit - desired).max(0.0);- g.moji.shrink = (desired - gap.min * unit).max(0.0).min(g.adv + desired);+ g.moji.shrink = (desired - gap.min * unit).max(0.0).min(width + desired);g.moji.priority = gap.priority;g.moji.discrete = gap.discrete;g.moji.end = if g.moji.explicit_after { 0.0 } else { g.moji.end_aki.desired * unit } - desired;diff --git a/crates/compose/src/shape.rs b/crates/compose/src/shape.rsindex 9391f3a19..e1d46aa25 100644--- a/crates/compose/src/shape.rs+++ b/crates/compose/src/shape.rs@@ -913,6 +913,7 @@ fn shape_segment((sg.x_offset as f64 * k * hs, -(sg.y_offset as f64) * k * p.v_scale)};let (ascent, descent) = if up { (em_top * k_v, -em_bottom * k_v) } else { (ascent, descent) };+ let body = adv;if ch == SOFT_HYPHEN {adv = 0.0;} else if last_in_cluster {@@ -950,6 +951,7 @@ fn shape_segment(break_after: None,cjk_hang: 0.0,moji: crate::mojikumi::Metrics {+ body,explicit_before: p.leading_aki.is_some(),explicit_after: p.trailing_aki.is_some(),..Default::default()diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rsindex e967940d2..53c9dbcce 100644--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@ -2293,3 +2293,33 @@ fn mojikumi_nonfloating_and_leading_only_compression_fit_ragged_lines() {}}}++#[test]+fn mojikumi_places_custom_gap_after_combining_marks() {+ use designcraft_doc::cjk::{MojikumiAki, MojikumiTable};+ let (mut d, sid, _) = doc_with("A\u{0323}\u{0301}1", Rect::new(0.0, 0.0, 200.0, 100.0), ParaAttrs::default());+ let natural = compose_story(&d, sid, &ComposeOptions::default());+ let old = &all_lines(&natural)[0].glyphs;+ assert!(old.len() >= 3, "Exercise a cluster with a separate combining glyph");+ Arc::make_mut(&mut d.styles).mojikumi_tables.push(MojikumiTable {+ name: "Clusters".into(),+ based_on: "SimpChineseDefault".into(),+ overrides: vec![MojikumiAki {+ target_class: 18,+ side_class: 25,+ after: true,+ minimum: 1.0,+ desired: 1.0,+ maximum: 1.0,+ ..Default::default()+ }],+ });+ d.story_mut(sid).unwrap().paras[0].para.mojikumi = Some("MojikumiTable/Clusters".into());+ let result = compose_story(&d, sid, &ComposeOptions::default());+ let new = &all_lines(&result)[0].glyphs;+ assert_eq!(new.len(), old.len());+ for (before, after) in old.iter().zip(new) {+ let expected = if before.rendered_char == '1' { 12.0 } else { 0.0 };+ assert!((after.x - before.x - expected).abs() < 0.001, "Glyph {:?} moved incorrectly", before.rendered_char);+ }+}diff --git a/crates/doc/src/mojikumi.rs b/crates/doc/src/mojikumi.rsindex bff676c9f..eda698371 100644--- a/crates/doc/src/mojikumi.rs+++ b/crates/doc/src/mojikumi.rs@@ -49,12 +49,30 @@ impl Preset {(1..=16).contains(&n).then_some(Self(n))})}+ fn leading(self, c: i16) -> f64 {+ if self.0 == 15 && matches!(c, 6 | 21 | 30 | 31) {+ 0.25+ } else if self.0 == 16 && c == 32 {+ 0.0+ } else {+ leading(c)+ }+ }+ fn trailing(self, c: i16) -> f64 {+ if self.0 == 15 && matches!(c, 6 | 21 | 30 | 31) {+ 0.25+ } else if self.0 == 16 && c == 32 {+ 0.5+ } else {+ trailing(c)+ }+ }fn boundary(self, left: i16, right: i16) -> Option<Aki> {let n = self.0;let open = |c| matches!(c, 1 | 26 | 27);if left == 23 {if n >= 15 {- return Some(Aki::fixed(leading(right)));+ return Some(Aki::fixed(self.leading(right)));}let indent = if matches!(n, 1 | 10 | 11 | 14) { 0.0 } else { 1.0 };let opening = match n {@@ -63,13 +81,13 @@ impl Preset {4 | 6 | 9 | 12 => 1.0,_ => 0.0,};- return Some(Aki::fixed(if open(right) { opening } else { indent + leading(right) }));+ return Some(Aki::fixed(if open(right) { opening } else { indent + self.leading(right) }));}if left == 22 {- return Some(Aki::fixed(if n >= 15 || matches!(n, 4 | 5 | 10) { leading(right) } else { 0.0 }));+ return Some(Aki::fixed(if n >= 15 || matches!(n, 4 | 5 | 10) { self.leading(right) } else { 0.0 }));}if right == 22 {- let width = trailing(left);+ let width = self.trailing(left);let full = n >= 15 || matches!(n, 4 | 5 | 10) || (matches!(n, 12..=14) && matches!(left, 6 | 31));let discrete = n == 6 || (matches!(n, 7 | 8 | 11) && matches!(left, 2 | 6 | 21 | 28..=31));return Some(if full {@@ -114,6 +132,14 @@ impl<'a> Rules<'a> {Ok(Some(Self { preset, rows }))}+ /// Blank body portions for the preset's regional punctuation convention.+ pub fn leading(&self, class: i16) -> f64 {+ self.preset.leading(class)+ }+ pub fn trailing(&self, class: i16) -> f64 {+ self.preset.trailing(class)+ }+pub fn pair(&self, left: i16, right: i16) -> Aki {if let Some(row) = self.rows@@ -139,7 +165,7 @@ impl<'a> Rules<'a> {{return Aki::fixed(0.0);}- let punctuation = trailing(left).max(leading(right));+ let punctuation = self.trailing(left).max(self.leading(right));if punctuation > 0.0 {return Aki { min: 0.0, desired: punctuation, max: punctuation, priority: 1, discrete: false };}@@ -239,6 +265,21 @@ mod tests {use super::*;use crate::cjk::MojikumiTable;+ #[test]+ fn regional_punctuation_retains_the_correct_side_bearings() {+ let styles = Styles::default();+ let traditional = Rules::resolve(&styles, "TradChineseDefault").unwrap().unwrap();+ let simplified = Rules::resolve(&styles, "SimpChineseDefault").unwrap().unwrap();+ assert_eq!(traditional.leading(class('。')), 0.25);+ assert_eq!(traditional.trailing(class('。')), 0.25);+ assert_eq!(simplified.leading(class('。')), 0.0);+ assert_eq!(simplified.trailing(class('。')), 0.5);+ assert_eq!(traditional.pair(12, class('。')).desired, 0.25);+ assert_eq!(simplified.pair(12, class('。')).desired, 0.0);+ assert_eq!(simplified.leading(class(':')), 0.0);+ assert_eq!(simplified.trailing(class(':')), 0.5);+ }+#[test]fn all_public_presets_have_distinct_documented_edges() {let styles = Styles::default();