From 6574fedb87921808472f59aa99de3c0d40c0e26c Mon Sep 17 00:00:00 2001 From: dfgodvan <180286157+dfgodvan@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:15:13 +0300 Subject: [PATCH] Line breaks: a question mark keeps its break in CJK text, a path after a space wraps whole Review follow-up. The break after '?' is dropped only before an ASCII letter, digit or underscore, so a '?' in CJK text keeps the break UAX #14 gives it. The break before a slash that starts a word now also takes '/_': with the break after '/' before '_' gone, "caption /_back" had no break left there and split mid-word when it did not fit. --- crates/layout/src/para/paragraph.rs | 22 +++++++++++++----- crates/layout/src/tests.rs | 35 +++++++++++++++++++++++++++++ docs/layout-parity.md | 2 +- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/crates/layout/src/para/paragraph.rs b/crates/layout/src/para/paragraph.rs index 59df08f841..b6da050323 100644 --- a/crates/layout/src/para/paragraph.rs +++ b/crates/layout/src/para/paragraph.rs @@ -812,14 +812,24 @@ fn break_text<'a>(source: &'a str, skip: &[std::ops::Range]) -> (std::bor } } +/// A character that continues a word or a path segment after a slash: a letter, a digit or `_`. +fn word_char(c: char) -> bool { + c.is_alphanumeric() || c == '_' +} + fn break_positions(text: &str, rp: &wordcraft_doc::resolve::ResolvedPara) -> Vec { let mut found = Vec::new(); for (i, o) in unicode_linebreak::linebreaks(text) { - // Word keeps "and/or" and web addresses whole: no break right after a slash, or the question - // mark of a query ("/?type="), before a letter, digit or underscore ("/_back") (a word too - // long for the line still breaks anywhere). - let after_slash = text.get(..i).is_some_and(|t| t.ends_with(['/', '?'])) - && text.get(i..).and_then(|t| t.chars().next()).is_some_and(|c| c.is_alphanumeric() || c == '_'); + // Word keeps "and/or" and web addresses whole: no break right after a slash before a letter, + // digit or underscore ("/_back"), nor after the question mark of a query ("/?type="), which + // takes an ASCII one (a question mark in CJK text keeps its break). A word too long for + // the line still breaks anywhere. + let next = text.get(i..).and_then(|t| t.chars().next()); + let after_slash = match text.get(..i).and_then(|t| t.chars().next_back()) { + Some('/') => next.is_some_and(word_char), + Some('?') => next.is_some_and(|c| c.is_ascii_alphanumeric() || c == '_'), + _ => false, + }; if (o == unicode_linebreak::BreakOpportunity::Allowed || i < text.len()) && !after_slash { found.push(i); } @@ -831,7 +841,7 @@ fn break_positions(text: &str, rp: &wordcraft_doc::resolve::ResolvedPara) -> Vec .match_indices('/') .filter(|&(i, _)| { text.get(..i).and_then(|t| t.chars().next_back()).is_some_and(|c| c.is_whitespace() && !matches!(c, '\u{a0}' | '\u{2007}' | '\u{202f}')) - && text.get(i + 1..).and_then(|t| t.chars().next()).is_some_and(char::is_alphanumeric) + && text.get(i + 1..).and_then(|t| t.chars().next()).is_some_and(word_char) }) .map(|(i, _)| i) .collect(); diff --git a/crates/layout/src/tests.rs b/crates/layout/src/tests.rs index 49b16c05eb..87837efe5e 100644 --- a/crates/layout/src/tests.rs +++ b/crates/layout/src/tests.rs @@ -7744,6 +7744,41 @@ fn no_line_break_inside_a_query_address() { } } +/// A path that starts after a space ("caption /_back") wraps whole: UAX #14 has no break between +/// the space and the slash, so the break before the slash is the only one there. +#[test] +fn a_path_after_a_space_wraps_whole() { + let text = "enregistrement /_back_office_reports"; + let mut wrapped = 0; + // Down to the width where the path still fits on a line (it no longer does at 425 pt). + for m in 100..=420 { + let mut d = Document::from_text(text); + d.last_section.margin_right = m as f32; + let lines = body_lines(&lay(&d), text); + if lines.len() < 2 { + continue; // fits on one line + } + wrapped += 1; + assert_eq!(lines, ["enregistrement ", "/_back_office_reports"], "the path is split at margin {m}"); + } + assert!(wrapped > 50, "{wrapped} widths wrapped"); +} + +/// A question mark in CJK text keeps the break after it: only a query's `?` before an ASCII +/// letter, digit or underscore loses it. +#[test] +fn a_question_mark_in_cjk_text_keeps_its_break() { + let text = "你好?世界".repeat(40); + let mut ended = false; + for m in 300..=440 { + let mut d = Document::from_text(&text); + d.last_section.margin_right = m as f32; + let lines = body_lines(&lay(&d), &text); + ended |= lines[..lines.len() - 1].iter().any(|l| l.ends_with('?')); + } + assert!(ended, "no line ever ends after the question mark"); +} + /// #2963: a word after a slash ("caption /заполняется") wraps whole, as in Word. UAX #14 forbids a /// break between the space and the slash, and the break after the slash is dropped (Word keeps /// "and/or" whole), so the slash word had no break before it and split between two letters. diff --git a/docs/layout-parity.md b/docs/layout-parity.md index 331242b525..35dfd88534 100644 --- a/docs/layout-parity.md +++ b/docs/layout-parity.md @@ -528,7 +528,7 @@ The front-of-text opt-out is retained from the earlier implementation; this patc | 2026-11-02 | trivial | Beyond Word, for ODT documents: a table's own space above and below (`TableProps::space_before`/`space_after`) moves the text around it, and a header (footer) taller than its margin keeps its spacing between it and the body (`SectionProps::header_gap`/`footer_gap`; Word keeps none, so DOCX documents are unchanged) (`tests::a_tables_own_space_above_and_below_moves_the_text`, `tests::body_starts_right_below_a_tall_header`) | | 2026-11-02 | trivial | A page break before the paragraph in a table's first row moves the whole table to the next page, as in Word (it was ignored); emphasis marks (Font › Emphasis mark) are drawn over or under each character and make the line taller | | 2026-11-12 | minor | Added layout.fitOnePage: heuristic search over discrete font half-point steps and spacing compression with anti-clutter safeguards (line-spacing floor and font-size floor) to condense multi-page documents by one page while preserving hierarchy | -| 2026-10-31 | trivial | Web addresses: no line break after the `?` of a query (`/?type=`) or after a slash before an underscore (`/portal/_back`). Word for Mac 16.93 kept such an address whole and moved it to the next line; WordCraft broke it there (UAX #14 allows both). With the address moved along the line one word at a time (60 paragraphs, Times New Roman 12pt), WordCraft's lines now match Word's line for line; 23 (query) and 19 (underscore) paragraphs broke inside the address before. `layout::tests::no_line_break_inside_a_query_address` | +| 2026-10-31 | trivial | Web addresses: no line break after the `?` of a query (`/?type=`) or after a slash before an underscore (`/portal/_back`). Word for Mac 16.93 kept such an address whole and moved it to the next line; WordCraft broke it there (UAX #14 allows both). With the address moved along the line one word at a time (60 paragraphs, Times New Roman 12pt), WordCraft's lines now match Word's line for line; 23 (query) and 19 (underscore) paragraphs broke inside the address before. A `?` keeps its break before anything but an ASCII letter, digit or underscore (CJK text), and a path after a space (`caption /_back`) wraps whole. `layout::tests::no_line_break_inside_a_query_address`, `a_path_after_a_space_wraps_whole`, `a_question_mark_in_cjk_text_keeps_its_break` | Maintenance on 2026-10-12 retains current tracked-deletion, endnote, footer-total, text-box and balloon layout while supplying explicit cell identity at every box call. The native Word