diff --git a/crates/typst-layout/src/stack.rs b/crates/typst-layout/src/stack.rs index c468945eb..196f9390a 100644 --- a/crates/typst-layout/src/stack.rs +++ b/crates/typst-layout/src/stack.rs @@ -1,6 +1,8 @@ use typst_library::diag::{bail, SourceResult}; use typst_library::engine::Engine; -use typst_library::foundations::{Content, Packed, Resolve, StyleChain, StyledElem}; +use typst_library::foundations::{ + Content, Packed, Resolve, Smart, StyleChain, StyledElem, +}; use typst_library::introspection::{Locator, SplitLocator}; use typst_library::layout::{ Abs, AlignElem, Axes, Axis, Dir, FixedAlignment, Fr, Fragment, Frame, HElem, Point, @@ -27,6 +29,8 @@ pub fn layout_stack( let spacing = elem.spacing(styles); let mut deferred = None; + let align = elem.align(styles); + for child in &elem.children { match child { StackChild::Spacing(kind) => { @@ -52,7 +56,15 @@ pub fn layout_stack( layouter.layout_spacing(kind); } - layouter.layout_block(engine, block, styles)?; + if let Smart::Custom(alignment) = align { + layouter.layout_block( + engine, + &block.clone().aligned(alignment), + styles, + )?; + } else { + layouter.layout_block(engine, block, styles)?; + } deferred = spacing; } } diff --git a/crates/typst-library/src/layout/stack.rs b/crates/typst-library/src/layout/stack.rs index 5fc78480e..3660b50ed 100644 --- a/crates/typst-library/src/layout/stack.rs +++ b/crates/typst-library/src/layout/stack.rs @@ -2,8 +2,10 @@ use std::fmt::{self, Debug, Formatter}; use crate::diag::SourceResult; use crate::engine::Engine; -use crate::foundations::{cast, elem, Content, NativeElement, Packed, Show, StyleChain}; -use crate::layout::{BlockElem, Dir, Spacing}; +use crate::foundations::{ + cast, elem, Content, NativeElement, Packed, Show, Smart, StyleChain, +}; +use crate::layout::{Alignment, BlockElem, Dir, Spacing}; /// Arranges content and spacing horizontally or vertically. /// @@ -42,6 +44,11 @@ pub struct StackElem { /// Spacing to insert between items where no explicit spacing was provided. pub spacing: Option, + /// How to align the items. + /// + /// If set to `{auto}`, the outer alignment is used. + pub align: Smart, + /// The children to stack along the axis. #[variadic] pub children: Vec, diff --git a/tests/ref/stack-align.png b/tests/ref/stack-align.png new file mode 100644 index 000000000..2e4912db8 Binary files /dev/null and b/tests/ref/stack-align.png differ diff --git a/tests/suite/layout/stack.typ b/tests/suite/layout/stack.typ index aad273a5b..9d64188c0 100644 --- a/tests/suite/layout/stack.typ +++ b/tests/suite/layout/stack.typ @@ -89,3 +89,10 @@ World! 🌍 // Error: 3-40 stack spacing is infinite stack(spacing: infinite-length)[A][B] }) + +--- stack-align --- +#set align(center) +#set stack(align: end) +#stack[I][am][aligned][to][the][right!] +#stack(align: auto)[I][am][aligned][to][the][center!] +#stack(dir: rtl, align: horizon)[Aligned \ at][horizon.]