summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/typst-library/src/layout/par.rs17
-rw-r--r--tests/ref/layout/par-justify.pngbin19571 -> 27297 bytes
-rw-r--r--tests/typ/layout/par-justify.typ10
3 files changed, 25 insertions, 2 deletions
diff --git a/crates/typst-library/src/layout/par.rs b/crates/typst-library/src/layout/par.rs
index 9e5d4a97..23579105 100644
--- a/crates/typst-library/src/layout/par.rs
+++ b/crates/typst-library/src/layout/par.rs
@@ -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);
diff --git a/tests/ref/layout/par-justify.png b/tests/ref/layout/par-justify.png
index fd25c5b7..5745f081 100644
--- a/tests/ref/layout/par-justify.png
+++ b/tests/ref/layout/par-justify.png
Binary files differ
diff --git a/tests/typ/layout/par-justify.typ b/tests/typ/layout/par-justify.typ
index 2e5b4806..e72a2a07 100644
--- a/tests/typ/layout/par-justify.typ
+++ b/tests/typ/layout/par-justify.typ
@@ -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"