From 5594868f8b7a3ca5155804653fc2f2d2fb84a48e Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 25 Sep 2021 23:35:27 +0200 Subject: [PATCH] Prevent paragraph from overflowing due to trailing linebreak --- src/layout/grid.rs | 2 -- src/layout/par.rs | 10 +++++++--- src/layout/stack.rs | 3 +-- tests/ref/layout/grid-5.png | Bin 0 -> 1427 bytes tests/typ/layout/grid-5.typ | 11 +++++++++++ 5 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 tests/ref/layout/grid-5.png create mode 100644 tests/typ/layout/grid-5.typ diff --git a/src/layout/grid.rs b/src/layout/grid.rs index 35706498c..91cc0014a 100644 --- a/src/layout/grid.rs +++ b/src/layout/grid.rs @@ -179,8 +179,6 @@ impl<'a> GridLayouter<'a> { // Sum of fractions of all fractional tracks. let mut fr = Fractional::zero(); - - // Resolve the size of all linear columns and compute the sum of all // fractional tracks. for (&col, rcol) in self.cols.iter().zip(&mut self.rcols) { diff --git a/src/layout/par.rs b/src/layout/par.rs index dc2ce0eff..18a701cd4 100644 --- a/src/layout/par.rs +++ b/src/layout/par.rs @@ -218,21 +218,25 @@ impl<'a> ParLayouter<'a> { stack.constraints.max.y.set_min(stack.size.h + line.size.h); stack.finish_region(ctx); } + // If the line does not fit horizontally or we have a mandatory // line break (i.e. due to "\n"), we push the line into the // stack. if mandatory || !stack.regions.current.w.fits(line.size.w) { - stack.push(line); start = end; last = None; + stack.push(line); stack.constraints.min.y = Some(stack.size.h); // If there is a trailing line break at the end of the // paragraph, we want to force an empty line. if mandatory && end == self.bidi.text.len() { - stack.push(LineLayout::new(ctx, &self, end .. end)); - stack.constraints.min.y = Some(stack.size.h); + let line = LineLayout::new(ctx, &self, end .. end); + if stack.regions.current.h.fits(line.size.h) { + stack.push(line); + stack.constraints.min.y = Some(stack.size.h); + } } } else { // Otherwise, the line fits both horizontally and vertically diff --git a/src/layout/stack.rs b/src/layout/stack.rs index 4afe9c640..4b148ad89 100644 --- a/src/layout/stack.rs +++ b/src/layout/stack.rs @@ -173,8 +173,7 @@ impl<'a> StackLayouter<'a> { let expand = self.expand; let used = self.used.to_size(self.block); - // Determine the stack's size dependening on whether the region is - // fixed. + // Determine the stack's size dependening on whether the region expands. let size = Size::new( if expand.x { self.constraints.exact.x = Some(self.full.w); diff --git a/tests/ref/layout/grid-5.png b/tests/ref/layout/grid-5.png new file mode 100644 index 0000000000000000000000000000000000000000..a43408f8aea84bd1dd4130c6d59d002ff2573696 GIT binary patch literal 1427 zcmeAS@N?(olHy`uVBq!ia0y~yU}OPe1`ePAL&2x;0}KqTQJyZ2Ar-gY-hG?9+DoMM z;oZiU4CM{h5>^Lf9oUyJzi23FuvU=mv7RBctVQ$TUVDau1wjv_&dBej{epP!HiEj(5<6JnLD#}?D%KnyGD*VVU-utwt zU~m3T35SA-1yS<^b}>#1p03SwZ&jjqCc|^q9VY!8!K?;vZ~MI4&3xwBoZ`GX7mZ(E z_lv!6+91za&T?DaQqJPDn}<6~{@>Y(I%cwhi{>U;2R!L2xY_>t=0cq`kHe1*7jBpI z5HoV+2;Z#7&~ovOvXjNlrFzWqt{+`jyj7kUQ|Nv0tbXc;SQd}VZ~oda`fUFGndeBR zgwo0C_mjWhb5D?LdwDfa-E^}G!uucE1uwm2swv$m9^bHS-Rml*7vC>VJEj%kr(bsc z%l&OFM^9~i=3%j>;Az7@-?Z7cYgcHND{?&SXppzR%&2(x+We!liha29m$R6bzQ_t=8D=G|i5b6zvJPw!=7J#sCli=oiq_4j^RkEP7B%xC7_ z%U{}W?7qqxm{J@%+*vODy_%)Jzx~5azIU_c2?X8ePyL(Z`b%DM&THLPMjqw>TiXY7 z&oKst?zyIV_`KKWI`346EqOA>H&?r4$ewrobEmxh zhIs#-!Rt7VA1_@L8@XHU4M%_&=Yu(~!jm2V=jzZ#{dB(fe!E zbguiZ4eJcFts43_->hu9GAnZOqpfRRSxkBNCQ5GGl^}pi>c#=$J-Cxob8dLV5#)r4D()H6NgRlUwv;1 zep|-1Q*lECw@daj)*1Cb+pgRZ{KhAE_LlDAx%Y(=H%LZYQ@(f0=&{|s>-T@oyq)W= zP*;0Tzfvh@eXfD`18D`5)0afJk92<(-^{C6`u5KJO*YmHy_FnG&t5n$aH{k>he71j zLz@(@2tO!xjabuq?KbN<&R1`@%6+|8neG~KZU1^B*9BWkGTFCv^eVvRfYol}{k$GJ=_;+lA>>TagBrQfTg#lDAUdi=N?{qpO-iq~qQw>G^? zKVbbls-w49*I>#sr;F!yDy2xGltZw9Mr2PMF+rAsdoqrYlF;&*{8xWr?qy3pNrFmS MPgg&ebxsLQ02wc5w*UYD literal 0 HcmV?d00001 diff --git a/tests/typ/layout/grid-5.typ b/tests/typ/layout/grid-5.typ new file mode 100644 index 000000000..cd1c8dc94 --- /dev/null +++ b/tests/typ/layout/grid-5.typ @@ -0,0 +1,11 @@ + +--- +// Test that trailing linebreak doesn't overflow the region. +#page(height: 2cm) +#grid[ + Hello \ + Hello \ + Hello \ + + World +]