From c3b1e32d11c8485194070710431b081c52a64c45 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 28 Oct 2023 18:02:16 +0200 Subject: [PATCH] Fix justified linebreaking without justifiables --- crates/typst-library/src/layout/par.rs | 6 ++++-- tests/ref/bugs/linebreak-no-justifiables.png | Bin 0 -> 1829 bytes tests/typ/bugs/linebreak-no-justifiables.typ | 5 +++++ 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 tests/ref/bugs/linebreak-no-justifiables.png create mode 100644 tests/typ/bugs/linebreak-no-justifiables.typ diff --git a/crates/typst-library/src/layout/par.rs b/crates/typst-library/src/layout/par.rs index 9b5f641b..ee47b579 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 0000000000000000000000000000000000000000..3f934592abfc10b9bab5c07e4f762f87729a2a07 GIT binary patch literal 1829 zcmZ{jXEYm%0>>jn5<9g^tSBj3#NH#3*aS6NBe6n-qIS&KRjaMlONCxk+$dVXb(Nx5 z?NOt~Q?+ijs;G5K>-NigAKp9foZtDM|M%bdW!u~0xH&Fz0001P3v-ME0Kmfb^ZP(p zf3Br{UI+jH{BCdUWOjajetdj!c@R7CwWFgWW_&+-Y&UxJ zOVr2?lgW%4{@mKy8acEbIru4J@MFa5t=j`1nwy)$Uu}laZ9x;k>_0{Qp(+S=Nnj<+>6H9_rjH```!w#@`GrvqEx zR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q1Vg1^kb;asPK|iRf7?brxOEEh{MbhhVYB+DsotS84 ziKSdkukauw+3iEWkHmW~gEdW zXKTk_Y1b{w%Vg?jm(s=#jiW&;%;TUF=Casviv2Qs+Mq$tBZhG{R|a6P71eti(AuqC zIQ8a`Dw%{ug70jI?-uC~Bgo6#?gy53BzGh1$tFC0ReBZ=-2f+t4(eny^$tO7^Ehao zT%}8J^vGgQUN}-v8Tr5^Wo*>AK89)|kvhoh%1w02-X0CFJ6%~)6jC40q%Dh|RvWCn z-)~gr^zxcobbR0HzVN2wg~cQBtu|XjJMT_WvTB-|=^3lC&a(E|cDj~V9GxgKP+@d* z)GRNWK`f8$E0Py;sUVa`H75~hVaoyM1&+47dy)xE)fLp*Io?b1wnXCFCeH-3lfUt1 z+Iw@trv|;CN^#wT+2q6|xKf!6lKMaNc4H>aMFdS?>i;&G*8M??FNe3R#~}{&vwDDq LnJuQ#ltBFlgM1}c literal 0 HcmV?d00001 diff --git a/tests/typ/bugs/linebreak-no-justifiables.typ b/tests/typ/bugs/linebreak-no-justifiables.typ new file mode 100644 index 00000000..ab1b2732 --- /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))