From 5ddcbfa93a3d5e15ccccfa41144c3acf04cf65e9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 14 Nov 2026 22:52:46 -0800 Subject: [PATCH] Type: a line's first word ends after hanging punctuation with Burasagari The first word, which places a line in a frame of uneven width, now ends after a custom set's hanging punctuation with Burasagari on, where the line itself may end, so the line is not moved past a span it fits. --- crates/text/src/layout/breaking.rs | 9 ++++++--- crates/text/src/layout/flow.rs | 4 ++-- crates/text/src/layout/shaped.rs | 2 +- crates/text/src/layout/wrapping_tests.rs | 22 +++++++++++++--------- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/crates/text/src/layout/breaking.rs b/crates/text/src/layout/breaking.rs index 793597c83a..8711d0b278 100644 --- a/crates/text/src/layout/breaking.rs +++ b/crates/text/src/layout/breaking.rs @@ -419,8 +419,8 @@ pub(super) fn leader_outline(db: &FontDb, g: &SGlyph, leader: &str, origin: f64, /// 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 -/// the measured glyphs is not. -pub(super) fn word_width<'k>(g: &[SGlyph], i: usize, line_end: &[f64], kinsoku: impl Into>) -> f64 { +/// the measured glyphs is not. With `burasagari`, the word may end after hanging punctuation. +pub(super) fn word_width<'k>(g: &[SGlyph], i: usize, line_end: &[f64], kinsoku: impl Into>, burasagari: Burasagari) -> f64 { let kinsoku = kinsoku.into(); /// Glyphs measured at most: a word wrapped over many lines is measured again from each line's /// start, and one this long is broken inside whatever cell it meets. @@ -454,7 +454,10 @@ pub(super) fn word_width<'k>(g: &[SGlyph], i: usize, line_end: &[f64], kinsoku: (trailing, ink) = (0.0, end); } let cluster_ends = g.get(end).is_none_or(|next| next.byte != gl.byte); - if gl.break_after() && cluster_ends && kinsoku_allows(g, end - 1, kinsoku) && !splits_no_break(g, end) { + // Hanging punctuation of one glyph may end the line with Burasagari, as in [`break_line`]. + let alone = cluster_ends && end.checked_sub(2).and_then(|k| g.get(k)).is_none_or(|p| p.byte != gl.byte); + let hang_break = burasagari != Burasagari::None && alone && hangs(gl, kinsoku); + if (gl.break_after() || hang_break) && cluster_ends && kinsoku_allows(g, end - 1, kinsoku) && !splits_no_break(g, end) { return set(w, trailing, ink); } } diff --git a/crates/text/src/layout/flow.rs b/crates/text/src/layout/flow.rs index aa9fbeb7fe..dcc3fe174d 100644 --- a/crates/text/src/layout/flow.rs +++ b/crates/text/src/layout/flow.rs @@ -107,7 +107,7 @@ fn compose_para( // Only the first line's first word is known here. Later lines are placed as the composed // lines need: each fits its width, so its first word does too. It is measured as the line // loop measures it, with Mojikumi's narrower start and end. - let word = if first { word_width(sg, 0, line_end, kinsoku) - start_credit(0) } else { 0.0 }; + let word = if first { word_width(sg, 0, line_end, kinsoku, para.burasagari) - start_credit(0) } else { 0.0 }; let Some((b, x0, x1, _)) = sim.place(m, ind_l, para.right_indent, word) else { break }; let w = x1 - x0 - ind_l - para.right_indent; widths.push(w); @@ -430,7 +430,7 @@ fn place_flow_line(cx: &mut Ctx<'_>, pen: &mut Pen<'_>, context: &mut PlacementC } // The line's first word, as narrow as the line sets it at both ends. let kinsoku = KinsokuRules::of(para); - let word = word_width(sg, *i, line_end, kinsoku); + let word = word_width(sg, *i, line_end, kinsoku, para.burasagari); // A composed line in a frame whose lines are not all as wide is placed at its own tallest // metrics, as the composer placed it (`shaped::Shaped`): its room is the room across its height. let composed_line = composed.as_ref().and_then(|lines| lines.get(*para_line)).is_some_and(|&(end, _)| end > *i); diff --git a/crates/text/src/layout/shaped.rs b/crates/text/src/layout/shaped.rs index d955779b78..86caea418f 100644 --- a/crates/text/src/layout/shaped.rs +++ b/crates/text/src/layout/shaped.rs @@ -278,7 +278,7 @@ fn past_end<'r>(p: &Para<'_>, start: &Start<'r>) -> Option> { /// frame's end ([`past_end`]) when no frame cell has room for it. fn place<'r>(p: &Para<'_>, start: &Start<'r>, m: Metrics) -> Result>, Exhausted> { let (ind_l, ind_r) = line_indents(p.para, start.first, p.mirror); - let word = word_width(p.sg, start.pos, p.line_end, KinsokuRules::of(p.para)) - (p.start_credit)(start.pos); + let word = word_width(p.sg, start.pos, p.line_end, KinsokuRules::of(p.para), p.para.burasagari) - (p.start_credit)(start.pos); let mut pen = start.pen.clone(); loop { match pen.place(m, ind_l, ind_r, word) { diff --git a/crates/text/src/layout/wrapping_tests.rs b/crates/text/src/layout/wrapping_tests.rs index 2fe361f8c5..f061073dc0 100644 --- a/crates/text/src/layout/wrapping_tests.rs +++ b/crates/text/src/layout/wrapping_tests.rs @@ -300,12 +300,12 @@ fn word_width_measures_across_a_soft_hyphen_inside_a_no_break_run() { for g in &mut glyphs { g.sx = 0.001; } - let plain = breaking::word_width(&glyphs, 0, &[], Kinsoku::None); + let plain = breaking::word_width(&glyphs, 0, &[], Kinsoku::None, Burasagari::None); assert!(plain < 30.0, "without the mark the word ends at the soft hyphen: {plain}"); for g in &mut glyphs { g.no_break = true; } - let marked = breaking::word_width(&glyphs, 0, &[], Kinsoku::None); + let marked = breaking::word_width(&glyphs, 0, &[], Kinsoku::None, Burasagari::None); assert!((marked - 30.0).abs() < 1e-9, "the marked run is measured whole: {marked}"); } @@ -317,7 +317,7 @@ fn word_width_stops_at_a_forced_line_break() { for g in &mut glyphs { g.no_break = true; } - let marked = breaking::word_width(&glyphs, 0, &[], Kinsoku::None); + let marked = breaking::word_width(&glyphs, 0, &[], Kinsoku::None, Burasagari::None); assert!((marked - 10.0).abs() < 1e-9, "the marked run ends at the forced line break: {marked}"); let mut glyphs = synthetic_glyphs(&[(0, 1, 10.0, 'A'), (1, 2, 0.0, '\u{00ad}'), (3, 3, 5.0, '\u{2028}'), (6, 1, 10.0, 'B')]); @@ -325,12 +325,12 @@ fn word_width_stops_at_a_forced_line_break() { for g in &mut glyphs { g.sx = 0.001; } - let plain = breaking::word_width(&glyphs, 0, &[], Kinsoku::None); + let plain = breaking::word_width(&glyphs, 0, &[], Kinsoku::None, Burasagari::None); assert!((plain - 10.0).abs() < 1e-9, "no hyphen before the forced line break: {plain}"); for g in &mut glyphs { g.no_break = true; } - let marked = breaking::word_width(&glyphs, 0, &[], Kinsoku::None); + let marked = breaking::word_width(&glyphs, 0, &[], Kinsoku::None, Burasagari::None); assert!((marked - 10.0).abs() < 1e-9, "the marked run ends at the forced line break: {marked}"); } @@ -1253,8 +1253,8 @@ fn korean_words_break_at_spaces_unless_line_break_anywhere() { let cands = candidates(text, &off, None, Kinsoku::Hard); assert_eq!(compose(&off, &|_| 50.0, &|_| 0.0, &|_| 0.0, &cands, &[], ¶ms), Some(vec![(4, false), (7, false)])); // The first word is measured whole, as the line loop measures it. - assert_eq!(breaking::word_width(&off, 0, &[], Kinsoku::Hard), 30.0); - assert_eq!(breaking::word_width(&korean(text, true), 0, &[], Kinsoku::Hard), 10.0); + assert_eq!(breaking::word_width(&off, 0, &[], Kinsoku::Hard, Burasagari::None), 30.0); + assert_eq!(breaking::word_width(&korean(text, true), 0, &[], Kinsoku::Hard, Burasagari::None), 10.0); // A word wider than the line still breaks, at the syllable that overflows. assert_eq!(greedy(&korean("가나다라마바", false), 25.0), 2); // A soft return still ends the line. @@ -1360,8 +1360,8 @@ fn bunri_kinshi_keeps_jlreq_pairs_in_both_composers() { }; assert_eq!(greedy(&pairs(text, false), 35.0), 3, "off: the line ends between the dashes"); assert_eq!(greedy(&pairs(text, true), 35.0), 2, "on: both dashes go to the next line"); - assert_eq!(breaking::word_width(&pairs("——あ", true), 0, &[], Kinsoku::Hard), 20.0); - assert_eq!(breaking::word_width(&pairs("——あ", false), 0, &[], Kinsoku::Hard), 10.0); + assert_eq!(breaking::word_width(&pairs("——あ", true), 0, &[], Kinsoku::Hard, Burasagari::None), 20.0); + assert_eq!(breaking::word_width(&pairs("——あ", false), 0, &[], Kinsoku::Hard, Burasagari::None), 10.0); let params = Params { justify_last: false, ragged: true, tolerance: f64::INFINITY, uniform_from: 0, hyphen_limit: 0 }; let on = pairs("あい——う", true); let cands = candidates("あい——う", &on, None, Kinsoku::Hard); @@ -1532,6 +1532,10 @@ fn a_custom_set_hangs_its_own_hanging_punctuation() { }; assert_eq!(wide(Burasagari::Standard), 4, "! fits and ends the line"); assert_eq!(wide(Burasagari::None), 3, "without Burasagari ! does not hang, and no line ends after it"); + // The first word, which places a line in a frame of uneven width, ends after ! as the line may. + let word = |b: Burasagari| breaking::word_width(&full_width("AB!CD"), 0, &[], breaking::KinsokuRules::Custom(&bang), b); + assert_eq!(word(Burasagari::Standard), 30.0); + assert_eq!(word(Burasagari::None), 50.0); // A hanging character that starts an inseparable sequence kept with Bunri-Kinshi does not hang // the rest of the sequence with it: the sequence goes to the next line. let joined = custom_set("", "", "!", &["!あ"]);