Fix improper line wrapping in the presence of medial newlines (#2056)

Fixes #2019.
This commit is contained in:
bluebear94 2023-09-04 05:46:17 -04:00 committed by GitHub
parent ba043a8d70
commit 499c5f2444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 14 deletions

View File

@ -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<L
// Layout the line.
let start = pred.line.end;
// Fix for https://github.com/unicode-org/icu4x/issues/3811
if i > 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<LineBreakIteratorUtf8<'a, 'a>>,
/// Iterator over syllables of the current word.
syllables: Option<hypher::Syllables<'a>>,
/// 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))
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 51 KiB

View File

@ -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;
}
```