Skip to content

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

CJK-MOJI8: preserve narrow punctuation and manual body compression

Shao Wenbin1e32767cef3c
diff --git a/ROADMAP.md b/ROADMAP.md
index 72d2186ad..27d2e8928 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -1,5 +1,6 @@
# DesignCraft roadmap
+- CJK-MOJI8: preserve shaped narrow punctuation and manual Tsume instead of restoring nonexistent full-width blank space.
- CJK-MOJI7: reject malformed Mojikumi interchange values and check nested note/cell paragraph rules in preflight.
- 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.
diff --git a/crates/compose/src/mojikumi.rs b/crates/compose/src/mojikumi.rs
index c0d92779e..0c44e546e 100644
--- a/crates/compose/src/mojikumi.rs
+++ b/crates/compose/src/mojikumi.rs
@@ -10,6 +10,10 @@ pub struct Metrics {
pub active: bool,
/// Shaped body width before tracking and manual character spacing.
pub body: f64,
+ pub tsume_before: f64,
+ pub tsume_after: f64,
+ pub blank_before: f64,
+ pub blank_after: f64,
/// Desired internal gap attached to this glyph's advance.
pub gap: f64,
pub stretch: f64,
@@ -67,10 +71,12 @@ pub fn apply(glyphs: &mut [Glyph], styles: &Styles, name: &str) {
// 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 before = if cluster[0].moji.explicit_before { 0.0 } else { (table.leading(class) * unit - cluster[0].moji.tsume_before).max(0.0) };
+ let after = if cluster[0].moji.explicit_after { 0.0 } else { (table.trailing(class) * unit - cluster[0].moji.tsume_after).max(0.0) };
let trim = before + after;
if let Some(owner) = cluster.iter().position(|g| g.adv >= trim) {
+ cluster[0].moji.blank_before = before / unit.max(1e-9);
+ cluster[end - start - 1].moji.blank_after = after / unit.max(1e-9);
cluster[owner].adv -= trim;
for (i, g) in cluster.iter_mut().enumerate() {
g.dx += if i > owner { trim - before } else { -before };
@@ -78,7 +84,7 @@ pub fn apply(glyphs: &mut [Glyph], styles: &Styles, name: &str) {
}
}
let head = &mut cluster[0];
- head.moji.start_aki = table.pair(if start == 0 { 23 } else { 22 }, class);
+ head.moji.start_aki = table.pair_with_bodies(if start == 0 { 23 } else { 22 }, class, 0.0, head.moji.blank_before);
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() {
@@ -93,11 +99,16 @@ pub fn apply(glyphs: &mut [Glyph], styles: &Styles, name: &str) {
&& !glyphs[end - 1].moji.explicit_after
&& !glyphs[next].moji.explicit_before
{
- gap = table.pair(class, rules::class(glyphs[next].rendered_char));
+ gap = table.pair_with_bodies(
+ class,
+ rules::class(glyphs[next].rendered_char),
+ glyphs[end - 1].moji.blank_after,
+ glyphs[next].moji.blank_before,
+ );
}
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);
+ g.moji.end_aki = table.pair_with_bodies(class, 22, g.moji.blank_after, 0.0);
let desired = (gap.desired * unit).max(-width);
g.moji.gap = desired;
g.moji.stretch = (gap.max * unit - desired).max(0.0);
diff --git a/crates/compose/src/shape.rs b/crates/compose/src/shape.rs
index e1d46aa25..bbf91cbb8 100644
--- a/crates/compose/src/shape.rs
+++ b/crates/compose/src/shape.rs
@@ -477,6 +477,8 @@ fn shape_run(
let tsume = p.tsume.clamp(0.0, 1.0);
let before = p.leading_aki.unwrap_or(0.0).clamp(0.0, 100.0) * em;
let after = p.trailing_aki.unwrap_or(0.0).clamp(0.0, 100.0) * em;
+ g.moji.tsume_before = left * tsume;
+ g.moji.tsume_after = right * tsume;
g.dx += before - left * tsume;
g.adv += before + after - (left + right) * tsume;
}
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 53c9dbcce..69e74f1a7 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -2323,3 +2323,22 @@ fn mojikumi_places_custom_gap_after_combining_marks() {
assert!((after.x - before.x - expected).abs() < 0.001, "Glyph {:?} moved incorrectly", before.rendered_char);
}
}
+
+#[test]
+fn mojikumi_does_not_restore_fullwidth_blanks_to_narrow_tracked_punctuation() {
+ let family = "Mojikumi Narrow Body";
+ let font = designcraft_fonts::testing::font_mapping(family, &[('(', 'i'), ('A', 'A')]).unwrap();
+ designcraft_fonts::FontDb::global().add_font(font);
+ let (mut d, sid, _) = doc_with("(A", Rect::new(0.0, 0.0, 200.0, 100.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().format_chars(0.."(A".len(), |f| {
+ f.over.font_family = Some(family.into());
+ f.over.tracking = Some(1000.0);
+ });
+ let natural = compose_story(&d, sid, &ComposeOptions::default());
+ d.story_mut(sid).unwrap().paras[0].para.mojikumi = Some("SimpChineseDefault".into());
+ let result = compose_story(&d, sid, &ComposeOptions::default());
+ for (a, b) in all_lines(&natural)[0].glyphs.iter().zip(&all_lines(&result)[0].glyphs) {
+ assert!((a.x - b.x).abs() < 0.001);
+ assert!((a.adv - b.adv).abs() < 0.001);
+ }
+}
diff --git a/crates/doc/src/mojikumi.rs b/crates/doc/src/mojikumi.rs
index 14e4433c3..aaedd355a 100644
--- a/crates/doc/src/mojikumi.rs
+++ b/crates/doc/src/mojikumi.rs
@@ -136,13 +136,35 @@ impl<'a> Rules<'a> {
self.preset.trailing(class)
}
- pub fn pair(&self, left: i16, right: i16) -> Aki {
- if let Some(row) = self
- .rows
+ fn override_pair(&self, left: i16, right: i16) -> Option<&MojikumiAki> {
+ self.rows
.iter()
.rev()
.find(|r| if r.after { r.target_class == left && r.side_class == right } else { r.target_class == right && r.side_class == left })
- {
+ }
+
+ /// Preset spacing restores only blank removed from shaped punctuation bodies.
+ /// Explicit custom rows retain their requested dimensions.
+ pub fn pair_with_bodies(&self, left: i16, right: i16, after_left: f64, before_right: f64) -> Aki {
+ let mut aki = self.pair(left, right);
+ if self.override_pair(left, right).is_none() {
+ let missing = if matches!(left, 22 | 23) {
+ self.leading(right) - before_right
+ } else if right == 22 {
+ self.trailing(left) - after_left
+ } else {
+ self.trailing(left).max(self.leading(right)) - after_left.max(before_right)
+ }
+ .max(0.0);
+ aki.min = (aki.min - missing).max(0.0);
+ aki.desired = (aki.desired - missing).max(0.0);
+ aki.max = (aki.max - missing).max(0.0);
+ }
+ aki
+ }
+
+ pub fn pair(&self, left: i16, right: i16) -> Aki {
+ if let Some(row) = self.override_pair(left, right) {
return Aki {
min: row.minimum,
desired: row.desired,
@@ -283,6 +305,8 @@ mod tests {
assert_eq!(simplified.pair(12, class('。')).desired, 0.0);
assert_eq!(simplified.leading(class(':')), 0.0);
assert_eq!(simplified.trailing(class(':')), 0.5);
+ assert!((simplified.pair_with_bodies(6, 12, 0.1, 0.0).desired - 0.1).abs() < 1e-9);
+ assert_eq!(simplified.pair_with_bodies(22, 27, 0.0, 0.0).desired, 0.0);
}
#[test]
@@ -328,6 +352,7 @@ mod tests {
});
let rule = Rules::resolve(&styles, "MojikumiTable/Pair").unwrap().unwrap();
assert_eq!(rule.pair(18, 12).desired, 0.2);
+ assert_eq!(rule.pair_with_bodies(18, 12, 0.0, 0.0).desired, 0.2);
assert_eq!(rule.pair(12, 18).desired, 0.25);
assert!(Rules::resolve(&styles, "Unknown").is_err());
assert!(Rules::resolve(&styles, "Nothing").unwrap().is_none());