From cd25b402816b0b4db0b310e3fff179f2a4fd7751 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 23 May 2021 22:36:34 +0200 Subject: [PATCH] Stack function --- src/exec/context.rs | 2 +- src/library/mod.rs | 3 +++ src/library/pad.rs | 2 +- src/library/shapes.rs | 4 ++-- src/library/stack.rs | 40 ++++++++++++++++++++++++++++++++++++ tests/ref/library/stack.png | Bin 0 -> 264 bytes tests/typ/library/stack.typ | 9 ++++++++ 7 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/library/stack.rs create mode 100644 tests/ref/library/stack.png create mode 100644 tests/typ/library/stack.typ diff --git a/src/exec/context.rs b/src/exec/context.rs index b5298313b..93ffaf966 100644 --- a/src/exec/context.rs +++ b/src/exec/context.rs @@ -52,7 +52,7 @@ impl<'a> ExecContext<'a> { } /// Execute a template and return the result as a stack node. - pub fn exec_group(&mut self, template: &TemplateValue) -> StackNode { + pub fn exec_template(&mut self, template: &TemplateValue) -> StackNode { let snapshot = self.state.clone(); let page = self.page.take(); let stack = mem::replace(&mut self.stack, StackBuilder::new(&self.state)); diff --git a/src/library/mod.rs b/src/library/mod.rs index 6e7966a06..a6628315e 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -15,6 +15,7 @@ mod page; mod par; mod shapes; mod spacing; +mod stack; pub use self::image::*; pub use align::*; @@ -28,6 +29,7 @@ pub use page::*; pub use par::*; pub use shapes::*; pub use spacing::*; +pub use stack::*; use std::fmt::{self, Display, Formatter}; @@ -81,6 +83,7 @@ pub fn new() -> Scope { func!("repr", repr); func!("rgb", rgb); func!("square", square); + func!("stack", stack); func!("type", type_); func!("v", v); diff --git a/src/library/pad.rs b/src/library/pad.rs index bdc188a77..4b6236d05 100644 --- a/src/library/pad.rs +++ b/src/library/pad.rs @@ -31,7 +31,7 @@ pub fn pad(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { ); Value::template("pad", move |ctx| { - let child = ctx.exec_group(&body).into(); + let child = ctx.exec_template(&body).into(); ctx.push(PadNode { padding, child }); }) } diff --git a/src/library/shapes.rs b/src/library/shapes.rs index cbd9a2112..ce8f634b5 100644 --- a/src/library/shapes.rs +++ b/src/library/shapes.rs @@ -59,7 +59,7 @@ fn rect_impl( body: TemplateValue, ) -> Value { Value::template(name, move |ctx| { - let mut stack = ctx.exec_group(&body); + let mut stack = ctx.exec_template(&body); stack.aspect = aspect; let fixed = FixedNode { width, height, child: stack.into() }; @@ -135,7 +135,7 @@ fn ellipse_impl( // perfectly into the ellipse. const PAD: f64 = 0.5 - SQRT_2 / 4.0; - let mut stack = ctx.exec_group(&body); + let mut stack = ctx.exec_template(&body); stack.aspect = aspect; let fixed = FixedNode { diff --git a/src/library/stack.rs b/src/library/stack.rs new file mode 100644 index 000000000..b6f92e168 --- /dev/null +++ b/src/library/stack.rs @@ -0,0 +1,40 @@ +use super::*; +use crate::layout::{StackChild, StackNode}; + +/// `stack`: Stack children along an axis. +/// +/// # Positional parameters +/// - Children: variadic, of type `template`. +/// +/// # Named parameters +/// - Stacking direction: `dir`, of type `direction`. +/// +/// # Return value +/// A template that places its children along the specified layouting axis. +/// +/// # Relevant types and constants +/// - Type `direction` +/// - `ltr` +/// - `rtl` +/// - `ttb` +/// - `btt` +pub fn stack(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { + let dir = args.eat_named::(ctx, "dir").unwrap_or(Dir::TTB); + let children = args.eat_all::(ctx); + + Value::template("stack", move |ctx| { + let children = children + .iter() + .map(|child| { + let child = ctx.exec_template(child).into(); + StackChild::Any(child, ctx.state.aligns) + }) + .collect(); + + ctx.push(StackNode { + dirs: Gen::new(ctx.state.lang.dir, dir), + aspect: None, + children, + }); + }) +} diff --git a/tests/ref/library/stack.png b/tests/ref/library/stack.png new file mode 100644 index 0000000000000000000000000000000000000000..684905f73f002286a1ada6935afc9e9f7229a26d GIT binary patch literal 264 zcmeAS@N?(olHy`uVBq!ia0y~yU}OQZpRq6l$%A*FfjImDJ|V9E|No!=I>>pUx>m9j zP|PaL?LUy(;_2cTQgQ3;xj^0q0|C|z>W!Nln1pm5#0ztMb8fgjDa^6BboV9M03eVM zz2xEu1&L1&{k*Vn0dBA%OY!D@+|mmc7-*@ioVe=NCo@*upjDIWe-OxO5QtcN!3Su2 c;-N#l-d?`Z?^+hs1KrQy>FVdQ&MBb@05Xna!~g&Q literal 0 HcmV?d00001 diff --git a/tests/typ/library/stack.typ b/tests/typ/library/stack.typ new file mode 100644 index 000000000..c773ec79b --- /dev/null +++ b/tests/typ/library/stack.typ @@ -0,0 +1,9 @@ +// Test the `stack` function. + +--- +#let rect(width, color) = rect(width: width, height: 1cm, fill: color) +#stack( + rect(2cm, #2a631a), + rect(3cm, #43a127), + rect(1cm, #9feb52), +)