diff --git a/crates/typst-layout/src/inline/line.rs b/crates/typst-layout/src/inline/line.rs index ca991467..0010c585 100644 --- a/crates/typst-layout/src/inline/line.rs +++ b/crates/typst-layout/src/inline/line.rs @@ -149,6 +149,7 @@ pub fn line<'a>( // Trim the line at the end, if necessary for this breakpoint. let trim = range.start + breakpoint.trim(full).len(); + let trimmed_range = range.start..trim; // Collect the items for the line. let mut items = Items::new(); @@ -179,7 +180,8 @@ pub fn line<'a>( trim_weak_spacing(&mut items); // Deal with CJ characters at line boundaries. - adjust_cj_at_line_boundaries(p, full, &mut items); + // Use the trimmed range for robust boundary checks. + adjust_cj_at_line_boundaries(p, trimmed_range, &mut items); // Compute the line's width. let width = items.iter().map(Item::natural_width).sum(); @@ -319,7 +321,11 @@ fn collect_range<'a>( /// /// See Requirements for Chinese Text Layout, Section 3.1.6.3 Compression of /// punctuation marks at line start or line end. -fn adjust_cj_at_line_boundaries(p: &Preparation, text: &str, items: &mut Items) { +/// +/// The `range` should only contain regular texts, with linebreaks trimmed. +fn adjust_cj_at_line_boundaries(p: &Preparation, range: Range, items: &mut Items) { + let text = &p.text[range]; + if text.starts_with(BEGIN_PUNCT_PAT) || (p.config.cjk_latin_spacing && text.starts_with(is_of_cj_script)) { diff --git a/tests/ref/issue-6539-cjk-latin-spacing-at-manual-linebreak.png b/tests/ref/issue-6539-cjk-latin-spacing-at-manual-linebreak.png new file mode 100644 index 00000000..cc572f7a Binary files /dev/null and b/tests/ref/issue-6539-cjk-latin-spacing-at-manual-linebreak.png differ diff --git a/tests/suite/layout/inline/cjk.typ b/tests/suite/layout/inline/cjk.typ index 0540cd19..75002f25 100644 --- a/tests/suite/layout/inline/cjk.typ +++ b/tests/suite/layout/inline/cjk.typ @@ -84,6 +84,19 @@ abc字#linebreak() abc字\ 母 +--- issue-6539-cjk-latin-spacing-at-manual-linebreak --- +// Issue #6539 +#set text(cjk-latin-spacing: auto) +#set box(width: 2.3em, stroke: (x: green)) + +#box(align(end)[甲国\ T国]) + +#box(align(end)[乙国 \ T国]) + +#box(align(end)[丙国 T国]) + +#box(align(end)[丁国T国]) + --- issue-2650-cjk-latin-spacing-meta --- 测a试