Stack function

This commit is contained in:
Laurenz 2021-05-23 22:36:34 +02:00
parent 5ced5a69c0
commit cd25b40281
7 changed files with 56 additions and 4 deletions

View File

@ -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));

View File

@ -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);

View File

@ -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 });
})
}

View File

@ -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 {

40
src/library/stack.rs Normal file
View File

@ -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,
});
})
}

BIN
tests/ref/library/stack.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 B

View File

@ -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),
)