diff --git a/crates/text/src/layout/breaking.rs b/crates/text/src/layout/breaking.rs index e9f16a8a30..5f44087d19 100644 --- a/crates/text/src/layout/breaking.rs +++ b/crates/text/src/layout/breaking.rs @@ -415,7 +415,7 @@ pub(super) fn leader_outline(db: &FontDb, g: &SGlyph, leader: &str, origin: f64, /// Width of the word starting at glyph `i`: its advances up to the first place a line may break /// (spaces left out, the hyphen of a soft hyphen in), the least room a line needs not to break /// inside it. As in [`break_line`], kinsoku keeps a character that may not end a line with the one -/// after it, a No Break run (its spaces inside it included) is one word up to a forced line break, +/// after it, spaces between them included, a No Break run (its spaces inside it included) is one word up to a forced line break, /// and a cluster is never split. A soft hyphen just before a forced line break adds no hyphen. A list marker at `i` (glyphs of length 0) counts whole, with the word after it. A word /// that ends where a line may end is measured as the line is set: without the spaces at its end, /// and with its last glyph as narrow as `line_end` sets it; one that ends at a soft hyphen or past @@ -440,7 +440,15 @@ pub(super) fn word_width<'k>(g: &[SGlyph], i: usize, line_end: &[f64], kinsoku: return set(w, trailing, ink); } if gl.is_space() && (end == start || !splits_no_break(g, end)) { - return set(w, trailing, ink); + // A line breaks after one of these spaces only where kinsoku allows it, as in + // [`break_line`]; where it allows none (after 「, or before 」), the word goes on past them. + let run = end + g[end..].iter().take(start + MAX_GLYPHS - end).take_while(|h| h.is_space()).count(); + if end == start || (end..run).any(|k| g[k].break_after() && g[k].len > 0 && kinsoku_allows(g, k, kinsoku) && !splits_no_break(g, k + 1)) { + return set(w, trailing, ink); + } + let spaces: f64 = g[end..run].iter().map(|h| h.adv).sum(); + (w, trailing, end) = (w + spaces, trailing + spaces, run); + continue; } let forced_next = g.get(end + 1).is_some_and(|n| n.ch == '\u{2028}'); if gl.is_soft_hyphen() && !forced_next && kinsoku_allows(g, end, kinsoku) && !splits_no_break(g, end + 1) { diff --git a/crates/text/src/layout/wrapping_tests.rs b/crates/text/src/layout/wrapping_tests.rs index b35d8ac661..96ccc3e5c7 100644 --- a/crates/text/src/layout/wrapping_tests.rs +++ b/crates/text/src/layout/wrapping_tests.rs @@ -1577,6 +1577,43 @@ fn a_custom_set_keeps_its_own_characters_off_line_ends() { assert_eq!(ends, [1, 2, 4, 5]); } +/// The least room a word needs ([`breaking::word_width`]) runs past spaces where kinsoku allows no +/// break after them, to the first break [`break_line`] may take: after 「 or a custom set's 三 with +/// spaces after it, and before a 」 that follows a space. Where the break after the space is +/// allowed, the word still ends before the space. +#[test] +fn word_width_runs_past_spaces_kinsoku_does_not_break_after() { + use KinsokuShoriType::PushOutOnly; + let finish = custom_set("", "三", "", &[]); + let empty = custom_set("", "", "", &[]); + for (text, k, word) in [ + ("「 四五", breaking::KinsokuRules::from(Kinsoku::Hard), 30.0), + ("「 四五", Kinsoku::Hard.into(), 40.0), + ("a 」二", Kinsoku::Hard.into(), 30.0), + ("三 四五", breaking::KinsokuRules::Custom(&finish), 30.0), + ("三 四五", breaking::KinsokuRules::Custom(&empty), 10.0), + ("一 二三", Kinsoku::Hard.into(), 10.0), + ] { + let g = full_width(text); + assert_eq!(breaking::word_width(&g, 0, &[], k, Burasagari::None), word, "{text}"); + // In a measure that wide, break_line's first line is that word, drawn without the spaces + // at its end. + let mut end = break_line(text, &g, 0, word, &[], 0.0, None, Burasagari::None, k, PushOutOnly, false, false, &[], None).0; + while end > 0 && g[end - 1].is_space() { + end -= 1; + } + assert_eq!(g[..end].iter().map(|h| h.adv).sum::(), word, "{text}"); + } + // A No Break run of spaces after 「 longer than the glyphs a word is measured over stops the + // measure there. + let long: String = std::iter::once('「').chain(std::iter::repeat_n(' ', 2000)).chain(['四']).collect(); + let mut g = full_width(&long); + for h in g.iter_mut().skip(1) { + h.no_break = true; + } + assert_eq!(breaking::word_width(&g, 0, &[], Kinsoku::Hard, Burasagari::None), 10240.0); +} + /// Soft and a set made from it choose the same composer: small kana and the prolonged sound mark, /// which Soft lets start a line, do not send a paragraph that pushes in to the single-line /// composer with either, while Hard, which keeps them off a line's start, does.