From 7f7e14d95f7240727e0163451190ff63b230e393 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Tue, 28 Dec 2021 00:23:42 +0100 Subject: [PATCH] Minor changes to columns --- src/library/columns.rs | 59 ++++++++++++++++++------------------ tests/typ/layout/columns.typ | 6 +--- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/library/columns.rs b/src/library/columns.rs index 225cba47a..25d6da9d7 100644 --- a/src/library/columns.rs +++ b/src/library/columns.rs @@ -43,18 +43,19 @@ impl Layout for ColumnsNode { return self.child.layout(ctx, regions); } - // All gutters in the document. (Can be different because the relative + // Gutter width for each region. (Can be different because the relative // component is calculated seperately for each region.) let mut gutters = vec![]; - // Sizes of all columns resulting from `region.current` and - // `region.backlog`. + + // Sizes of all columns resulting from `region.current`, + // `region.backlog` and `regions.last`. let mut sizes = vec![]; let columns = self.columns.get(); - for (current, base) in std::iter::once((regions.current, regions.base)) - .chain(regions.backlog.as_slice().iter().map(|&s| (s, s))) - .chain(regions.last.iter().map(|&s| (s, s))) + for (current, base) in regions + .iter() + .take(1 + regions.backlog.len() + if regions.last.is_some() { 1 } else { 0 }) { let gutter = self.gutter.resolve(base.x); gutters.push(gutter); @@ -68,9 +69,11 @@ impl Layout for ColumnsNode { } let first = sizes.remove(0); - let mut pod = - Regions::one(first, Size::new(first.x, regions.base.y), regions.expand); - pod.expand.x = true; + let mut pod = Regions::one( + first, + Size::new(first.x, regions.base.y), + Spec::new(true, regions.expand.y), + ); // Retrieve elements for the last region from the vectors. let last_gutter = if regions.last.is_some() { @@ -84,24 +87,18 @@ impl Layout for ColumnsNode { pod.backlog = sizes.into_iter(); - let frames = self.child.layout(ctx, &pod); + let mut frames = self.child.layout(ctx, &pod).into_iter(); let dir = ctx.styles.get(ParNode::DIR); - let to = |cursor: Length, width: Length, regions: &Regions| { - if dir.is_positive() { - cursor - } else { - regions.current.x - cursor - width - } - }; - let mut cursor = Length::zero(); - - let mut frames = frames.into_iter(); - let mut res = vec![]; + let mut finished = vec![]; let total_regions = (frames.len() as f32 / columns as f32).ceil() as usize; - for (i, (current, base)) in regions.iter().take(total_regions).enumerate() { + for ((current, base), gutter) in regions + .iter() + .take(total_regions) + .zip(gutters.into_iter().chain(last_gutter.into_iter().cycle())) + { // The height should be the parent height if the node shall expand. // Otherwise its the maximum column height for the frame. In that // case, the frame is first created with zero height and then @@ -109,6 +106,8 @@ impl Layout for ColumnsNode { let mut height = if regions.expand.y { current.y } else { Length::zero() }; let mut frame = Frame::new(Spec::new(regions.current.x, height)); + let mut cursor = Length::zero(); + for _ in 0 .. columns { let child_frame = match frames.next() { Some(frame) => frame.item, @@ -118,16 +117,19 @@ impl Layout for ColumnsNode { let width = child_frame.size.x; if !regions.expand.y { - height = height.max(child_frame.size.y); + height.set_max(child_frame.size.y); } frame.push_frame( - Point::new(to(cursor, width, ®ions), Length::zero()), + Point::with_x(if dir.is_positive() { + cursor + } else { + regions.current.x - cursor - width + }), child_frame, ); - cursor += width - + gutters.get(i).copied().unwrap_or_else(|| last_gutter.unwrap()); + cursor += width + gutter; } frame.size.y = height; @@ -135,10 +137,9 @@ impl Layout for ColumnsNode { let mut cts = Constraints::new(regions.expand); cts.base = base.map(Some); cts.exact = current.map(Some); - res.push(frame.constrain(cts)); - cursor = Length::zero(); + finished.push(frame.constrain(cts)); } - res + finished } } diff --git a/tests/typ/layout/columns.typ b/tests/typ/layout/columns.typ index 7683c55e3..bf954d930 100644 --- a/tests/typ/layout/columns.typ +++ b/tests/typ/layout/columns.typ @@ -81,7 +81,7 @@ D Arbitrary horizontal growth. --- -// Test columns in an infinitely wide frame. +// Test columns in an infinitely high frame. #set page(width: 7.05cm, columns: 2) There can be as much content as you want in the left column @@ -102,7 +102,3 @@ This is a normal page. Very normal. // Test a page with zero columns. // Error: 49-50 must be positive #set page(height: auto, width: 7.05cm, columns: 0) - -This makes less sense. - -// colbreak in auto stroke box on sized page that should be higher than box \ No newline at end of file