diff --git a/crates/typst-library/src/layout/par.rs b/crates/typst-library/src/layout/par.rs index d3cd6cbff..5ae7db761 100644 --- a/crates/typst-library/src/layout/par.rs +++ b/crates/typst-library/src/layout/par.rs @@ -1,3 +1,5 @@ +use std::iter::Peekable; + use icu_properties::{maps::CodePointMapData, LineBreak}; use icu_provider::AsDeserializingBufferProvider; use icu_provider_adapters::fork::ForkByKeyProvider; @@ -933,18 +935,6 @@ fn linebreak_optimized<'a>(vt: &Vt, p: &'a Preparation<'a>, width: Abs) -> Vec 0 { - if let Some(s_pred) = table.get(i + 1) { - let next_start = s_pred.line.end; - if !p.bidi.text[start..next_start] - .contains(|c: char| !c.is_whitespace()) - { - continue; - } - } - } - let attempt = line(vt, p, start..end, mandatory, hyphen); // Determine how much the line's spaces would need to be stretched @@ -1103,7 +1093,7 @@ fn breakpoints<'a>(p: &'a Preparation<'a>) -> Breakpoints<'a> { linebreaks.next(); Breakpoints { p, - linebreaks, + linebreaks: linebreaks.peekable(), syllables: None, offset: 0, suffix: 0, @@ -1117,7 +1107,7 @@ struct Breakpoints<'a> { /// The paragraph's items. p: &'a Preparation<'a>, /// The inner iterator over the unicode line break opportunities. - linebreaks: LineBreakIteratorUtf8<'a, 'a>, + linebreaks: Peekable>, /// Iterator over syllables of the current word. syllables: Option>, /// The current text offset. @@ -1179,6 +1169,20 @@ impl Iterator for Breakpoints<'_> { } } + // Fix for https://github.com/unicode-org/icu4x/issues/3811 + if !self.mandatory { + while let Some(&next) = self.linebreaks.peek() { + if !self.p.bidi.text[self.end..next] + .contains(|c: char| !c.is_whitespace()) + { + self.end = next; + self.linebreaks.next(); + } else { + break; + } + } + } + self.offset = self.end; Some((self.end, self.mandatory, false)) } diff --git a/tests/ref/layout/par-justify.png b/tests/ref/layout/par-justify.png index 5745f0817..0cd9cbcdc 100644 Binary files a/tests/ref/layout/par-justify.png and b/tests/ref/layout/par-justify.png differ diff --git a/tests/typ/layout/par-justify.typ b/tests/typ/layout/par-justify.typ index e72a2a07a..69576c7d8 100644 --- a/tests/typ/layout/par-justify.typ +++ b/tests/typ/layout/par-justify.typ @@ -50,3 +50,16 @@ This text can be fitted in one line. lorem ipsum 1234, lorem ipsum dolor sit amet #" leading whitespace should still be displayed" + +--- +// Test that justification doesn't break code blocks + +#set par(justify: true) + +```cpp +int main() { + printf("Hello world\n"); + return 0; +} +``` +