From fb043299bfa94e93623cd11a460b631066d8fe44 Mon Sep 17 00:00:00 2001 From: LeonGuoHOC <114897879+LeonGuoHOC@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:31:48 +1100 Subject: [PATCH 1/2] A line can break after a dot between letters, as in a web address InDesign breaks a web address after a dot followed by a letter (example.org), as it does after a slash; a decimal (12.50) stays whole. Co-Authored-By: Claude Opus 5.5 --- crates/compose/src/breaker.rs | 10 +++++++++- crates/compose/src/tests.rs | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/compose/src/breaker.rs b/crates/compose/src/breaker.rs index bd9351119..4630cf884 100644 --- a/crates/compose/src/breaker.rs +++ b/crates/compose/src/breaker.rs @@ -360,6 +360,10 @@ fn right_hang(g: &Glyph, sp: &Spacing) -> f64 { g.cjk_hang.max(hang_right(g, sp)) - g.moji.end } +fn dot_break(glyphs: &[Glyph], i: usize) -> bool { + glyphs[i].ch == '.' && i > 0 && glyphs[i - 1].ch.is_alphabetic() && glyphs.get(i + 1).is_some_and(|n| n.ch.is_alphabetic() && !is_cjk(n.ch)) +} + /// The part of a hyphen after `g` (of width `hw`) that hangs past the line's end. fn hyphen_hang(g: &Glyph, hw: f64, sp: &Spacing) -> f64 { if sp.optical { hw * hang('-').1 * optical_scale(g, sp) } else { 0.0 } @@ -487,7 +491,10 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S p.hang = right_hang(g, sp); p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only()); push_inword(&mut it, &mut ig, p, i + 1, sp); - } else if matches!(g.ch, '\u{2013}' | '\u{2014}' | '/') || g.break_after.unwrap_or_else(|| cjk_break(g, &glyphs[i + 1], sp)) { + } else if matches!(g.ch, '\u{2013}' | '\u{2014}' | '/') + || dot_break(glyphs, i) + || g.break_after.unwrap_or_else(|| cjk_break(g, &glyphs[i + 1], sp)) + { let mut p = Item::penalty(0.0, 0.0, false); p.hang = right_hang(g, sp); p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only()); @@ -1012,6 +1019,7 @@ pub fn greedy( } } } else if matches!(g.ch, '-' | '\u{2010}' | '\u{2013}' | '\u{2014}' | '/') + || dot_break(glyphs, i) || g.break_after.unwrap_or_else(|| cjk_break(g, &glyphs[i + 1], sp)) { last_ok = Some((i + 1, false)); diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 0a1f67184..9ac6af7a4 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -8125,6 +8125,25 @@ fn a_word_longer_than_the_line_breaks_at_the_column_edge() { } } +#[test] +fn a_web_address_breaks_after_a_dot_between_letters_but_a_decimal_stays_whole() { + use designcraft_doc::Composer; + let width = |t: &str| { + let l = &tracked_lines(t, 1e5, ParaAttrs::default(), 0.0)[0]; + l.end_x - l.x0 + }; + let host = "visit example.org/x1"; + let measure = width("visit example.") + 1.0; + for composer in [Composer::Paragraph, Composer::SingleLine] { + let para = ParaAttrs { composer: Some(composer), ..Default::default() }; + let lines = tracked_lines(host, measure, para.clone(), 0.0); + assert_eq!(&host[lines[0].range.clone()].trim_end(), &"visit example.", "{composer:?}"); + let dec = "pay 12.50 now"; + let lines = tracked_lines(dec, width("pay 12.") + 1.0, para, 0.0); + assert!(lines.iter().all(|l| !dec[l.range.clone()].trim().ends_with('.')), "{composer:?}"); + } +} + #[test] fn a_line_that_exactly_fills_the_measure_fits() { use designcraft_doc::Composer; From a23aed2bccbf1a5d40f0c485b7ca97f307e1bc6e Mon Sep 17 00:00:00 2001 From: "autocraft-bot[bot]" <354215646+autocraft-bot[bot]@users.noreply.github.com> Date: Mon, 30 Nov 2026 09:22:36 +0000 Subject: [PATCH 2/2] Part 1: `dot_break` in crates/compose/src/breaker.rs now allows a break Part 1: `dot_break` in crates/compose/src/breaker.rs now allows a break after a dot only with at least three letters right before it and two right after it. A ligature counts the further letters it covers, so a short ending set as one glyph, such as ".fi", still counts as two. "e.g.", "U.S.A.", "Ph.D." and decimals now stay whole, and "visit example.org/x1" still breaks after "example.". Both composers use this rule. I extended the existing web-address test to check that those three abbreviations stay whole in both composers. With the old rule restored it fails by splitting "e.g.", and with the new rule it passes. `cargo fmt --all --check`, clippy on designcraft-compose with `-D warnings`, `cargo test -p designcraft-compose` (510 passed, 1 ignored) and `cargo xtask layers` pass. The rule only counts letters, so it has two limits. A dot with too few letters beside it, such as the one after "co" in "bbc.co.uk", is no longer a break point. An unspaced abbreviation with enough letters on each side, such as "rer.nat.", can still break. The pull request description still says a letter on each side is enough. --- crates/compose/src/breaker.rs | 10 +++++++++- crates/compose/src/tests.rs | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/compose/src/breaker.rs b/crates/compose/src/breaker.rs index 4630cf884..f240182c2 100644 --- a/crates/compose/src/breaker.rs +++ b/crates/compose/src/breaker.rs @@ -360,8 +360,16 @@ fn right_hang(g: &Glyph, sp: &Spacing) -> f64 { g.cjk_hang.max(hang_right(g, sp)) - g.moji.end } +/// May a line break after the dot at glyph `i`, as in a web address? It takes three letters right +/// before the dot and two right after it, so that abbreviations ("e.g.", "U.S.A.", "Ph.D.") and +/// decimals stay whole. fn dot_break(glyphs: &[Glyph], i: usize) -> bool { - glyphs[i].ch == '.' && i > 0 && glyphs[i - 1].ch.is_alphabetic() && glyphs.get(i + 1).is_some_and(|n| n.ch.is_alphabetic() && !is_cjk(n.ch)) + let letter = |g: &&Glyph| g.ch.is_alphabetic() && !is_cjk(g.ch); + // A ligature is one glyph for several letters: its further bytes count too. + let letters = |g: &Glyph| 1 + g.len.saturating_sub(g.ch.len_utf8()); + glyphs[i].ch == '.' + && glyphs[..i].iter().rev().take_while(letter).filter(|g| g.len > 0).take(3).map(letters).sum::() >= 3 + && glyphs[i + 1..].iter().take_while(letter).filter(|g| g.len > 0).take(2).map(letters).sum::() >= 2 } /// The part of a hyphen after `g` (of width `hw`) that hangs past the line's end. diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 9ac6af7a4..4425b680e 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -8139,8 +8139,16 @@ fn a_web_address_breaks_after_a_dot_between_letters_but_a_decimal_stays_whole() let lines = tracked_lines(host, measure, para.clone(), 0.0); assert_eq!(&host[lines[0].range.clone()].trim_end(), &"visit example.", "{composer:?}"); let dec = "pay 12.50 now"; - let lines = tracked_lines(dec, width("pay 12.") + 1.0, para, 0.0); + let lines = tracked_lines(dec, width("pay 12.") + 1.0, para.clone(), 0.0); assert!(lines.iter().all(|l| !dec[l.range.clone()].trim().ends_with('.')), "{composer:?}"); + let para = ParaAttrs { hyphenate: Some(false), ..para }; + for (text, head) in + [("see the e.g. note", "see the e."), ("made in the U.S.A. today", "made in the U."), ("she holds a Ph.D. now", "she holds a Ph.")] + { + let lines = tracked_lines(text, width(head) + 1.0, para.clone(), 0.0); + let words: Vec<_> = lines.iter().flat_map(|l| text[l.range.clone()].split_whitespace()).collect(); + assert_eq!(words, text.split_whitespace().collect::>(), "{composer:?}"); + } } }