Skip to content

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

CJK-MOJI5: balance spacing priorities and fit discrete compression endpoints

Shao Wenbina35f03c12639
diff --git a/ROADMAP.md b/ROADMAP.md
index 9c452b9ad..1897b38e2 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -1,5 +1,6 @@
# DesignCraft roadmap
+- 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.
- CJK-MOJI3: use the composition rule resolver in preflight, report unknown Mojikumi presets and retain regression coverage for supported and unsupported settings.
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 947ec97f6..4b9e6f28f 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -1668,7 +1668,10 @@ fn layout_line(
};
let justify_this = align.is_justified() && (!last || align == Align::FullyJustified || forced_mid) && !has_tab;
// A justified paragraph's last line may have been composed with shrunk spaces: shrink it too.
- let squeeze_last = !justify_this && !has_tab && extra < 0.0 && (align.is_justified() || line.iter().any(|g| g.moji.shrink > 0.0));
+ let squeeze_last = !justify_this
+ && !has_tab
+ && extra < 0.0
+ && (align.is_justified() || line.iter().any(|g| g.moji.shrink > 0.0 || mojikumi::start_elastic(g, false, true)[1] > 0.0));
// Extra advance per glyph (word spaces and letter gaps) and horizontal scale per glyph.
let mut add = vec![0.0; line.len()];
let mut scale = vec![1.0; line.len()];
diff --git a/crates/compose/src/mojikumi.rs b/crates/compose/src/mojikumi.rs
index 339b60856..23dde00e3 100644
--- a/crates/compose/src/mojikumi.rs
+++ b/crates/compose/src/mojikumi.rs
@@ -104,46 +104,38 @@ pub fn edges(line: &mut [Glyph]) {
pub fn distribute(line: &mut [Glyph], extra: f64, add: &mut [f64]) -> f64 {
let mut rem = extra.abs();
let sign = extra.signum();
- for priority in 0..=10 {
- // Start and following gaps share the same priority queue. Moving a
- // leading gap also moves the glyph outline, not just the next glyph.
- for before in [true, false] {
- let properties = |g: &Glyph| {
- if before {
- let a = g.moji.start_aki;
- let cap = if !g.moji.at_start || g.moji.explicit_before {
- 0.0
- } else if sign < 0.0 {
- (a.desired - a.min).max(0.0) * em(g)
- } else {
- (a.max - a.desired).max(0.0) * em(g)
- };
- (a.priority, a.discrete, cap)
- } else {
- (g.moji.priority, g.moji.discrete, if sign < 0.0 { g.moji.shrink } else { g.moji.stretch })
- }
+ let properties = |g: &Glyph, before: bool| {
+ if before {
+ let a = g.moji.start_aki;
+ let cap = if !g.moji.at_start || g.moji.explicit_before {
+ 0.0
+ } else if sign < 0.0 {
+ (a.desired - a.min).max(0.0) * em(g)
+ } else {
+ (a.max - a.desired).max(0.0) * em(g)
};
+ (a.priority, a.discrete, cap)
+ } else {
+ (g.moji.priority, g.moji.discrete, if sign < 0.0 { g.moji.shrink } else { g.moji.stretch })
+ }
+ };
+ for priority in 0..=10 {
+ if rem <= 1e-9 {
+ break;
+ }
+ // Leading and following continuous gaps participate in one pool.
+ // Equal priorities must not depend on whether a gap precedes a glyph.
+ let total: f64 = line
+ .iter()
+ .flat_map(|g| [true, false].map(|before| properties(g, before)))
+ .filter(|&(p, discrete, _)| p == priority && !discrete)
+ .map(|(_, _, cap)| cap)
+ .sum();
+ if total > 0.0 {
+ let take = rem.min(total);
for (g, a) in line.iter_mut().zip(add.iter_mut()) {
- let (p, discrete, cap) = properties(g);
- if p == priority && discrete && cap <= rem + 1e-9 {
- *a += sign * cap;
- if before {
- g.dx += sign * cap;
- }
- rem = (rem - cap).max(0.0);
- }
- }
- let total: f64 = line
- .iter()
- .map(|g| {
- let (p, discrete, cap) = properties(g);
- if p == priority && !discrete { cap } else { 0.0 }
- })
- .sum();
- if total > 0.0 {
- let take = rem.min(total);
- for (g, a) in line.iter_mut().zip(add.iter_mut()) {
- let (p, discrete, cap) = properties(g);
+ for before in [true, false] {
+ let (p, discrete, cap) = properties(g, before);
if p == priority && !discrete {
let amount = sign * take * cap / total;
*a += amount;
@@ -152,7 +144,22 @@ pub fn distribute(line: &mut [Glyph], extra: f64, add: &mut [f64]) -> f64 {
}
}
}
- rem -= take;
+ }
+ rem -= take;
+ }
+ // Never interpolate a non-floating rule. Compression may have to use
+ // its entire endpoint even when that leaves a little unused line width;
+ // refusing it would contradict the capacity accepted by the breaker.
+ for (g, a) in line.iter_mut().zip(add.iter_mut()) {
+ for before in [true, false] {
+ let (p, discrete, cap) = properties(g, before);
+ if rem > 1e-9 && p == priority && discrete && cap > 0.0 && (sign < 0.0 || cap <= rem + 1e-9) {
+ *a += sign * cap;
+ if before {
+ g.dx += sign * cap;
+ }
+ rem -= cap;
+ }
}
}
}
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index b9589b060..e967940d2 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -2228,3 +2228,68 @@ fn mojikumi_priority_and_elastic_line_start_are_used_in_placement() {
assert!((line.end_x - 30.0).abs() < 0.001);
assert!(line.glyphs[0].x > 10.0, "Leading gap must move the first outline too");
}
+
+#[test]
+fn mojikumi_equal_priority_shares_leading_and_internal_space() {
+ use designcraft_doc::cjk::{MojikumiAki, MojikumiTable};
+ let (mut d, sid, _) = doc_with("A1", Rect::new(0.0, 0.0, 40.0, 100.0), ParaAttrs::default());
+ let natural = compose_story(&d, sid, &ComposeOptions::default());
+ let old = all_lines(&natural)[0];
+ let extra = 40.0 - old.end_x;
+ let old_second = old.glyphs[1].x;
+ Arc::make_mut(&mut d.styles).mojikumi_tables.push(MojikumiTable {
+ name: "Shared".into(),
+ based_on: "SimpChineseDefault".into(),
+ overrides: [(18, 23, false), (18, 25, true)]
+ .into_iter()
+ .map(|(target_class, side_class, after)| MojikumiAki {
+ target_class,
+ side_class,
+ after,
+ maximum: 10.0,
+ priority: 1,
+ ..Default::default()
+ })
+ .collect(),
+ });
+ let para = &mut d.story_mut(sid).unwrap().paras[0].para;
+ para.mojikumi = Some("MojikumiTable/Shared".into());
+ para.align = Some(Align::FullyJustified);
+ let result = compose_story(&d, sid, &ComposeOptions::default());
+ let line = all_lines(&result)[0];
+ assert!((line.glyphs[0].x - extra / 2.0).abs() < 0.001);
+ assert!((line.glyphs[1].x - old_second - extra).abs() < 0.001);
+ assert!((line.end_x - 40.0).abs() < 0.001);
+}
+
+#[test]
+fn mojikumi_nonfloating_and_leading_only_compression_fit_ragged_lines() {
+ use designcraft_doc::cjk::{MojikumiAki, MojikumiTable};
+ for composer in [Composer::SingleLine, Composer::Paragraph] {
+ for leading in [true, false] {
+ let (mut d, sid, _) = doc_with(
+ "A",
+ Rect::new(0.0, 0.0, 15.0, 100.0),
+ ParaAttrs { composer: Some(composer), mojikumi: Some("MojikumiTable/Endpoint".into()), ..Default::default() },
+ );
+ Arc::make_mut(&mut d.styles).mojikumi_tables.push(MojikumiTable {
+ name: "Endpoint".into(),
+ based_on: "SimpChineseDefault".into(),
+ overrides: vec![MojikumiAki {
+ target_class: 18,
+ side_class: if leading { 23 } else { 22 },
+ after: !leading,
+ minimum: 0.0,
+ desired: 1.0,
+ maximum: 1.0,
+ priority: 1,
+ does_not_float: true,
+ }],
+ });
+ let result = compose_story(&d, sid, &ComposeOptions::default());
+ let line = all_lines(&result)[0];
+ assert!(line.end_x <= 15.0, "{composer:?}, leading={leading}: {}", line.end_x);
+ assert!(line.glyphs[0].x.abs() < 0.001, "Discrete leading gap must reach its endpoint");
+ }
+ }
+}