linebreak_optimized: Handle breakpoints separated only by spaces (#1821)

This commit is contained in:
bluebear94 2023-08-23 07:39:22 -04:00 committed by GitHub
parent a4ad407a79
commit b59b8bfb35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View File

@ -929,9 +929,22 @@ fn linebreak_optimized<'a>(vt: &Vt, p: &'a Preparation<'a>, width: Abs) -> Vec<L
let mut best: Option<Entry> = None;
// Find the optimal predecessor.
for (i, pred) in table.iter_mut().enumerate().skip(active) {
for (i, pred) in table.iter().enumerate().skip(active) {
// 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
@ -996,7 +1009,7 @@ fn linebreak_optimized<'a>(vt: &Vt, p: &'a Preparation<'a>, width: Abs) -> Vec<L
}
// In Knuth paper, cost = (1 + 100|r|^3 + p)^2 + a,
// where r is the ratio, p=50 is penaty, and a=3000 is consecutive penaty.
// where r is the ratio, p=50 is the penalty, and a=3000 is consecutive the penalty.
// We divide the whole formula by 10, resulting (0.01 + |r|^3 + p)^2 + a,
// where p=0.5 and a=300
cost = (0.01 + cost).powi(2);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

@ -40,3 +40,13 @@ This text can be fitted in one line.
"a b c "
}
#"d"
---
// Test that justification cannot lead to a leading space
#set par(justify: true)
#set text(size: 12pt)
#set page(width: 45mm, height: auto)
lorem ipsum 1234, lorem ipsum dolor sit amet
#" leading whitespace should still be displayed"