diff --git a/crates/typst-library/src/layout/par.rs b/crates/typst-library/src/layout/par.rs index 9b5f641b6..ee47b5799 100644 --- a/crates/typst-library/src/layout/par.rs +++ b/crates/typst-library/src/layout/par.rs @@ -1001,8 +1001,10 @@ fn linebreak_optimized<'a>(vt: &Vt, p: &'a Preparation<'a>, width: Abs) -> Vec 1.0 { - // We should stretch the line above its stretchability. Now calculate the extra amount. - let extra_stretch = (delta - adjust) / attempt.justifiables() as f64; + // We should stretch the line above its stretchability. Now + // calculate the extra amount. Also, don't divide by zero. + let extra_stretch = + (delta - adjust) / attempt.justifiables().max(1) as f64; // Normalize the amount by half Em size. ratio = 1.0 + extra_stretch / (em / 2.0); } diff --git a/tests/ref/bugs/linebreak-no-justifiables.png b/tests/ref/bugs/linebreak-no-justifiables.png new file mode 100644 index 000000000..3f934592a Binary files /dev/null and b/tests/ref/bugs/linebreak-no-justifiables.png differ diff --git a/tests/typ/bugs/linebreak-no-justifiables.typ b/tests/typ/bugs/linebreak-no-justifiables.typ new file mode 100644 index 000000000..ab1b2732b --- /dev/null +++ b/tests/typ/bugs/linebreak-no-justifiables.typ @@ -0,0 +1,5 @@ +// Test breaking a line without justifiables. + +--- +#set par(justify: true) +#block(width: 1cm, fill: aqua, lorem(2))