diff --git a/crates/compose/src/breaker.rs b/crates/compose/src/breaker.rs index 5d4a0f465..792d3f51f 100644 --- a/crates/compose/src/breaker.rs +++ b/crates/compose/src/breaker.rs @@ -133,8 +133,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; @@ -200,6 +212,11 @@ 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, + /// 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; @@ -220,6 +237,8 @@ impl Item { zone_from: NONE, emergency: false, over: 0.0, + cjk: 0.0, + space_box: false, } } fn glue(w: f64, st: f64, sh: f64) -> Item { @@ -237,6 +256,8 @@ impl Item { zone_from: NONE, emergency: false, over: 0.0, + cjk: 0.0, + space_box: false, } } fn penalty(w: f64, p: f64, flagged: bool) -> Item { @@ -254,6 +275,8 @@ impl Item { zone_from: NONE, emergency: false, over: 0.0, + cjk: 0.0, + space_box: false, } } } @@ -516,6 +539,8 @@ fn items(text: &str, glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[b 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); + 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()); @@ -741,6 +766,9 @@ struct PrefixSums { sy: Vec<[f64; 3]>, sz: Vec<[f64; 3]>, so: Vec, + sc: Vec, + /// Word spaces set as boxes before each item. + sb: Vec, } fn prefix_sums(items: &[Item]) -> PrefixSums { @@ -748,6 +776,8 @@ 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]; + 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), @@ -755,18 +785,20 @@ 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 } + 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 } = 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); @@ -817,7 +849,12 @@ 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). 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 || 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 // 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 ae21db89f..92ef077ec 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -7533,6 +7533,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};