From 3209937f3642eca05385db64d0d6fce47ca204cb Mon Sep 17 00:00:00 2001 From: con3code Date: Mon, 30 Nov 2026 22:50:52 +0900 Subject: [PATCH 1/2] Compose: a justified CJK paragraph fills its lines from the first A justified line of CJK text has no word space, and outside its punctuation no stretch, so every line that had to open a little rated the same as one a few characters long. The paragraph composer then broke wherever the last line happened to fit: a first line of a few characters spread across the measure, then full lines. Such a line now rates its stretch against its characters' widths, a quarter em each counting like word spaces at their maximum. --- crates/compose/src/breaker.rs | 32 +++++++++++++++++++++++++++++--- crates/compose/src/tests.rs | 15 +++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/crates/compose/src/breaker.rs b/crates/compose/src/breaker.rs index f240182c2..6e1434f33 100644 --- a/crates/compose/src/breaker.rs +++ b/crates/compose/src/breaker.rs @@ -132,8 +132,20 @@ impl Spacing { } g.space * (self.word_max - self.word_desired).max(OVERFLOW_STRETCH) } + /// The unit in which a justified line without word spaces rates stretch past every tier, for + /// its CJK character `g`. Such a line is set by opening its characters evenly (追い出し), so a + /// line a little short of its measure is far better than one a few characters long; without a + /// unit both rated [`INF`], and the paragraph broke wherever its last line happened to fit. + fn cjk_overflow(&self, g: &Glyph) -> f64 { + if self.justify && !g.locked_advance && is_cjk(g.ch) { g.adv.max(0.0) * CJK_OVERFLOW } else { 0.0 } + } } +/// Room a CJK character gives a justified line that has stretched past every tier and has no +/// word space to take the rest, as a fraction of its width: the line's characters open evenly, and +/// a quarter em each rates like word spaces at their maximum. +const CJK_OVERFLOW: f64 = 0.25; + /// Minimum stretch of a word space, as a fraction of its width, for rating lines that stretch /// past the maximum word spacing (the default range's 133% − 100%). const OVERFLOW_STRETCH: f64 = 0.33; @@ -199,6 +211,9 @@ struct Item { emergency: bool, /// Glue: stretch past the maximum word spacing ([`Spacing::space_overflow`]). over: f64, + /// Boxes: a CJK character's room to open on a line with no word space + /// ([`Spacing::cjk_overflow`]). + cjk: f64, } const NONE: usize = usize::MAX; @@ -219,6 +234,7 @@ impl Item { zone_from: NONE, emergency: false, over: 0.0, + cjk: 0.0, } } fn glue(w: f64, st: f64, sh: f64) -> Item { @@ -236,6 +252,7 @@ impl Item { zone_from: NONE, emergency: false, over: 0.0, + cjk: 0.0, } } fn penalty(w: f64, p: f64, flagged: bool) -> Item { @@ -253,6 +270,7 @@ impl Item { zone_from: NONE, emergency: false, over: 0.0, + cjk: 0.0, } } } @@ -490,6 +508,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let mut bx = Item::boxed(g.adv, st, sh); bx.hang = hang_left(g, sp) - g.moji.start; bx.start_elastic = crate::mojikumi::start_elastic(g, sp.justify, !sp.push_out_only()); + bx.cjk = sp.cjk_overflow(g); it.push(bx); ig.push(i); let next_is_space = glyphs.get(i + 1).is_none_or(|n| n.is_space()); @@ -715,6 +734,7 @@ struct PrefixSums { sy: Vec<[f64; 3]>, sz: Vec<[f64; 3]>, so: Vec, + sc: Vec, } fn prefix_sums(items: &[Item]) -> PrefixSums { @@ -722,6 +742,7 @@ fn prefix_sums(items: &[Item]) -> PrefixSums { let mut sy = vec![[0.0; 3]; items.len() + 1]; let mut sz = vec![[0.0; 3]; items.len() + 1]; let mut so = vec![0.0; items.len() + 1]; + let mut sc = vec![0.0; items.len() + 1]; for (i, it) in items.iter().enumerate() { let (w, y, z) = match it.kind { Kind::Box | Kind::Glue => (it.w, it.st, it.sh), @@ -729,18 +750,19 @@ fn prefix_sums(items: &[Item]) -> PrefixSums { }; sw[i + 1] = sw[i] + w; so[i + 1] = so[i] + it.over; + sc[i + 1] = sc[i] + it.cjk; for t in 0..3 { sy[i + 1][t] = sy[i][t] + y[t]; sz[i + 1][t] = sz[i][t] + z[t]; } } - PrefixSums { sw, sy, sz, so } + PrefixSums { sw, sy, sz, so, sc } } fn kp_pass(input: PassInput<'_>) -> Option> { let PassInput { items, item_glyphs, glyphs, spacing: sp, width, tolerance: tol, emergency } = input; let m = items.len(); - let PrefixSums { sw, sy, sz, so } = prefix_sums(items); + let PrefixSums { sw, sy, sz, so, sc } = prefix_sums(items); let first_box = items.iter().find(|i| i.kind == Kind::Box); let first_hang = first_box.map_or(0.0, |b| b.hang); let first_elastic = first_box.map_or([0.0; 2], |b| b.start_elastic); @@ -791,7 +813,11 @@ fn kp_pass(input: PassInput<'_>) -> Option> { // A line that overruns its measure by a rounding error fits exactly. let d = target - l; let d = if d < 0.0 && d > -FIT_EPS { 0.0 } else { d }; - let (mut r, feasible) = tiered_ratio(d, y, z, so[b] - n.to); + // Word spaces take the stretch past every tier; a line without one opens its CJK + // characters (every box after the break at `n.pos` is on this line). + let over = so[b] - n.to; + let over = if over > 1e-9 { over } else { sc[b] - sc[n.pos] }; + let (mut r, feasible) = tiered_ratio(d, y, z, over); if it.emergency && sp.justify && r > 0.0 { // A piece of an overlong word usually has no elastic material: rate it by the // space it leaves, so the fullest piece wins (the break at the last glyph that fits). diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 4425b680e..a25fbb181 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -7088,6 +7088,21 @@ fn mojikumi_justifies_cjk_without_spaces_and_respects_fixed_spacing() { assert!((all_lines(&cs)[0].end_x - 100.0).abs() < 0.001, "{}", all_lines(&cs)[0].end_x); } +#[test] +fn a_justified_cjk_paragraph_fills_its_lines_from_the_first() { + // No line holds a whole number of characters, so each full line has to open its characters a + // little. Rated all alike, such lines left the breaks to chance: a first line of a few + // characters spread across the measure, then full lines. + let counts = |align| { + let (d, sid, _) = doc_with(&"あ".repeat(50), Rect::new(0.0, 0.0, 233.3, 200.0), ParaAttrs { align: Some(align), ..Default::default() }); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + all_lines(&cs).iter().map(|l| l.glyphs.len()).collect::>() + }; + let ragged = counts(Align::Left); + assert!(ragged.len() > 1 && ragged[0] > ragged[ragged.len() - 1], "{ragged:?}"); + assert_eq!(counts(Align::LeftJustified), ragged); +} + #[test] fn mojikumi_fixed_pairs_are_not_stretched_by_single_word_justification() { use designcraft_doc::cjk::{MojikumiAki, MojikumiTable}; From c82cf3684d3d39de3cbb29bff85919a911dfdfd3 Mon Sep 17 00:00:00 2001 From: con3code Date: Mon, 30 Nov 2026 23:57:13 +0900 Subject: [PATCH 2/2] Compose: a line with a no-break space rates as it did A no-break space is a word space set as a box, so it carries no overflow unit and its line fell to the CJK characters' unit. Such a line keeps the rating it had. --- crates/compose/src/breaker.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/compose/src/breaker.rs b/crates/compose/src/breaker.rs index 6e1434f33..04d992f4f 100644 --- a/crates/compose/src/breaker.rs +++ b/crates/compose/src/breaker.rs @@ -214,6 +214,8 @@ struct Item { /// Boxes: a CJK character's room to open on a line with no word space /// ([`Spacing::cjk_overflow`]). cjk: f64, + /// A word space set as a box (a no-break space): its line has a word space. + space_box: bool, } const NONE: usize = usize::MAX; @@ -235,6 +237,7 @@ impl Item { emergency: false, over: 0.0, cjk: 0.0, + space_box: false, } } fn glue(w: f64, st: f64, sh: f64) -> Item { @@ -253,6 +256,7 @@ impl Item { emergency: false, over: 0.0, cjk: 0.0, + space_box: false, } } fn penalty(w: f64, p: f64, flagged: bool) -> Item { @@ -271,6 +275,7 @@ impl Item { emergency: false, over: 0.0, cjk: 0.0, + space_box: false, } } } @@ -509,6 +514,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S bx.hang = hang_left(g, sp) - g.moji.start; bx.start_elastic = crate::mojikumi::start_elastic(g, sp.justify, !sp.push_out_only()); bx.cjk = sp.cjk_overflow(g); + bx.space_box = g.is_justify_space(); it.push(bx); ig.push(i); let next_is_space = glyphs.get(i + 1).is_none_or(|n| n.is_space()); @@ -735,6 +741,8 @@ struct PrefixSums { sz: Vec<[f64; 3]>, so: Vec, sc: Vec, + /// Word spaces set as boxes before each item. + sb: Vec, } fn prefix_sums(items: &[Item]) -> PrefixSums { @@ -743,6 +751,7 @@ fn prefix_sums(items: &[Item]) -> PrefixSums { let mut sz = vec![[0.0; 3]; items.len() + 1]; let mut so = vec![0.0; items.len() + 1]; let mut sc = vec![0.0; items.len() + 1]; + let mut sb = vec![0; items.len() + 1]; for (i, it) in items.iter().enumerate() { let (w, y, z) = match it.kind { Kind::Box | Kind::Glue => (it.w, it.st, it.sh), @@ -751,18 +760,19 @@ fn prefix_sums(items: &[Item]) -> PrefixSums { sw[i + 1] = sw[i] + w; so[i + 1] = so[i] + it.over; sc[i + 1] = sc[i] + it.cjk; + sb[i + 1] = sb[i] + u32::from(it.space_box); for t in 0..3 { sy[i + 1][t] = sy[i][t] + y[t]; sz[i + 1][t] = sz[i][t] + z[t]; } } - PrefixSums { sw, sy, sz, so, sc } + PrefixSums { sw, sy, sz, so, sc, sb } } fn kp_pass(input: PassInput<'_>) -> Option> { let PassInput { items, item_glyphs, glyphs, spacing: sp, width, tolerance: tol, emergency } = input; let m = items.len(); - let PrefixSums { sw, sy, sz, so, sc } = prefix_sums(items); + let PrefixSums { sw, sy, sz, so, sc, sb } = prefix_sums(items); let first_box = items.iter().find(|i| i.kind == Kind::Box); let first_hang = first_box.map_or(0.0, |b| b.hang); let first_elastic = first_box.map_or([0.0; 2], |b| b.start_elastic); @@ -814,9 +824,10 @@ fn kp_pass(input: PassInput<'_>) -> Option> { let d = target - l; let d = if d < 0.0 && d > -FIT_EPS { 0.0 } else { d }; // Word spaces take the stretch past every tier; a line without one opens its CJK - // characters (every box after the break at `n.pos` is on this line). + // characters (every box after the break at `n.pos` is on this line). A no-break + // space is a word space set as a box: its line rates as it did. let over = so[b] - n.to; - let over = if over > 1e-9 { over } else { sc[b] - sc[n.pos] }; + let over = if over > 1e-9 || sb[b] > sb[n.pos] { over } else { sc[b] - sc[n.pos] }; let (mut r, feasible) = tiered_ratio(d, y, z, over); if it.emergency && sp.justify && r > 0.0 { // A piece of an overlong word usually has no elastic material: rate it by the