summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/exec/context.rs2
-rw-r--r--src/library/mod.rs3
-rw-r--r--src/library/pad.rs2
-rw-r--r--src/library/shapes.rs4
-rw-r--r--src/library/stack.rs40
-rw-r--r--tests/ref/library/stack.pngbin0 -> 264 bytes
-rw-r--r--tests/typ/library/stack.typ9
7 files changed, 56 insertions, 4 deletions
diff --git a/src/exec/context.rs b/src/exec/context.rs
index b5298313..93ffaf96 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 6e7966a0..a6628315 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 bdc188a7..4b6236d0 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 cbd9a211..ce8f634b 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 00000000..b6f92e16
--- /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::<Dir>(ctx, "dir").unwrap_or(Dir::TTB);
+ let children = args.eat_all::<TemplateValue>(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 00000000..684905f7
--- /dev/null
+++ b/tests/ref/library/stack.png
Binary files differ
diff --git a/tests/typ/library/stack.typ b/tests/typ/library/stack.typ
new file mode 100644
index 00000000..c773ec79
--- /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),
+)