summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Fellenz <matt@felle.nz>2023-08-08 02:27:15 -0700
committerGitHub <noreply@github.com>2023-08-08 11:27:15 +0200
commitf56060c1b3fd029f1744322025b89253c5cf5457 (patch)
tree76b4658f1427b93bd0cec46740613168e17883c6
parentda8367e189b02918a8fe1a98fd3059fd11a82cd9 (diff)
Penalize runts (#1858)
-rw-r--r--crates/typst-library/src/layout/par.rs6
-rw-r--r--tests/ref/layout/par-justify.pngbin41627 -> 19571 bytes
-rw-r--r--tests/typ/layout/par-justify.typ9
3 files changed, 15 insertions, 0 deletions
diff --git a/crates/typst-library/src/layout/par.rs b/crates/typst-library/src/layout/par.rs
index 271d9467..9e5d4a97 100644
--- a/crates/typst-library/src/layout/par.rs
+++ b/crates/typst-library/src/layout/par.rs
@@ -908,6 +908,7 @@ fn linebreak_optimized<'a>(vt: &Vt, p: &'a Preparation<'a>, width: Abs) -> Vec<L
// Cost parameters.
const HYPH_COST: Cost = 0.5;
+ const RUNT_COST: Cost = 0.5;
const CONSECUTIVE_DASH_COST: Cost = 300.0;
const MAX_COST: Cost = 1_000_000.0;
const MIN_RATIO: f64 = -1.0;
@@ -984,6 +985,11 @@ fn linebreak_optimized<'a>(vt: &Vt, p: &'a Preparation<'a>, width: Abs) -> Vec<L
ratio.powi(3).abs()
};
+ // Penalize runts.
+ if k == i + 1 && eof {
+ cost += RUNT_COST;
+ }
+
// Penalize hyphens.
if hyphen {
cost += HYPH_COST;
diff --git a/tests/ref/layout/par-justify.png b/tests/ref/layout/par-justify.png
index 9f92034f..fd25c5b7 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 8d985cbf..2e5b4806 100644
--- a/tests/typ/layout/par-justify.typ
+++ b/tests/typ/layout/par-justify.typ
@@ -31,3 +31,12 @@ D E F #linebreak(justify: true)
#set page(width: 155pt)
#set par(justify: true)
This text can be fitted in one line.
+
+---
+// Test that runts are avoided when it's not too costly to do so.
+#set page(width: 124pt)
+#set par(justify: true)
+#for i in range(0, 20) {
+ "a b c "
+}
+#"d"