diff --git a/crates/typst/src/layout/stack.rs b/crates/typst/src/layout/stack.rs index e1c266254..caa78264a 100644 --- a/crates/typst/src/layout/stack.rs +++ b/crates/typst/src/layout/stack.rs @@ -4,8 +4,8 @@ use crate::diag::SourceResult; use crate::engine::Engine; use crate::foundations::{cast, elem, Content, Packed, Resolve, StyleChain, StyledElem}; use crate::layout::{ - Abs, AlignElem, Axes, Axis, Dir, FixedAlignment, Fr, Fragment, Frame, LayoutMultiple, - Point, Regions, Size, Spacing, + Abs, AlignElem, Axes, Axis, Dir, FixedAlignment, Fr, Fragment, Frame, HElem, + LayoutMultiple, Point, Regions, Size, Spacing, VElem, }; use crate::util::{Get, Numeric}; @@ -60,6 +60,7 @@ impl LayoutMultiple for Packed { regions: Regions, ) -> SourceResult { let mut layouter = StackLayouter::new(self.dir(styles), regions, styles); + let axis = layouter.dir.axis(); // Spacing to insert before the next block. let spacing = self.spacing(styles); @@ -72,6 +73,20 @@ impl LayoutMultiple for Packed { deferred = None; } StackChild::Block(block) => { + // Transparently handle `h`. + if let (Axis::X, Some(h)) = (axis, block.to_packed::()) { + layouter.layout_spacing(*h.amount()); + deferred = None; + continue; + } + + // Transparently handle `v`. + if let (Axis::Y, Some(v)) = (axis, block.to_packed::()) { + layouter.layout_spacing(*v.amount()); + deferred = None; + continue; + } + if let Some(kind) = deferred { layouter.layout_spacing(kind); } diff --git a/tests/ref/bugs/1240-stack-fr.png b/tests/ref/bugs/1240-stack-fr.png new file mode 100644 index 000000000..29df5d44a Binary files /dev/null and b/tests/ref/bugs/1240-stack-fr.png differ diff --git a/tests/typ/bugs/1240-stack-fr.typ b/tests/typ/bugs/1240-stack-fr.typ new file mode 100644 index 000000000..fa49dce76 --- /dev/null +++ b/tests/typ/bugs/1240-stack-fr.typ @@ -0,0 +1,18 @@ +// This issue is sort of horrible: When you write `h(1fr)` in a `stack` instead +// of directly `1fr`, things go awry. To fix this, we now transparently detect +// h/v children. +// +// https://github.com/typst/typst/issues/1240 + +--- +#stack(dir: ltr, [a], 1fr, [b], 1fr, [c]) +#stack(dir: ltr, [a], h(1fr), [b], h(1fr), [c]) + +--- +#set page(height: 60pt) +#stack( + dir: ltr, + spacing: 1fr, + stack([a], 1fr, [b]), + stack([a], v(1fr), [b]), +)