diff --git a/crates/compose/src/breaker.rs b/crates/compose/src/breaker.rs index bd9351119..f240182c2 100644 --- a/crates/compose/src/breaker.rs +++ b/crates/compose/src/breaker.rs @@ -360,6 +360,18 @@ 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 { + 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. 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 +499,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 +1027,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..4425b680e 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -8125,6 +8125,33 @@ 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.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:?}"); + } + } +} + #[test] fn a_line_that_exactly_fills_the_measure_fits() { use designcraft_doc::Composer;