Stack function
This commit is contained in:
parent
5ced5a69c0
commit
cd25b40281
@ -52,7 +52,7 @@ impl<'a> ExecContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a template and return the result as a stack node.
|
/// 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 snapshot = self.state.clone();
|
||||||
let page = self.page.take();
|
let page = self.page.take();
|
||||||
let stack = mem::replace(&mut self.stack, StackBuilder::new(&self.state));
|
let stack = mem::replace(&mut self.stack, StackBuilder::new(&self.state));
|
||||||
|
@ -15,6 +15,7 @@ mod page;
|
|||||||
mod par;
|
mod par;
|
||||||
mod shapes;
|
mod shapes;
|
||||||
mod spacing;
|
mod spacing;
|
||||||
|
mod stack;
|
||||||
|
|
||||||
pub use self::image::*;
|
pub use self::image::*;
|
||||||
pub use align::*;
|
pub use align::*;
|
||||||
@ -28,6 +29,7 @@ pub use page::*;
|
|||||||
pub use par::*;
|
pub use par::*;
|
||||||
pub use shapes::*;
|
pub use shapes::*;
|
||||||
pub use spacing::*;
|
pub use spacing::*;
|
||||||
|
pub use stack::*;
|
||||||
|
|
||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
|
|
||||||
@ -81,6 +83,7 @@ pub fn new() -> Scope {
|
|||||||
func!("repr", repr);
|
func!("repr", repr);
|
||||||
func!("rgb", rgb);
|
func!("rgb", rgb);
|
||||||
func!("square", square);
|
func!("square", square);
|
||||||
|
func!("stack", stack);
|
||||||
func!("type", type_);
|
func!("type", type_);
|
||||||
func!("v", v);
|
func!("v", v);
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ pub fn pad(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
);
|
);
|
||||||
|
|
||||||
Value::template("pad", move |ctx| {
|
Value::template("pad", move |ctx| {
|
||||||
let child = ctx.exec_group(&body).into();
|
let child = ctx.exec_template(&body).into();
|
||||||
ctx.push(PadNode { padding, child });
|
ctx.push(PadNode { padding, child });
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ fn rect_impl(
|
|||||||
body: TemplateValue,
|
body: TemplateValue,
|
||||||
) -> Value {
|
) -> Value {
|
||||||
Value::template(name, move |ctx| {
|
Value::template(name, move |ctx| {
|
||||||
let mut stack = ctx.exec_group(&body);
|
let mut stack = ctx.exec_template(&body);
|
||||||
stack.aspect = aspect;
|
stack.aspect = aspect;
|
||||||
|
|
||||||
let fixed = FixedNode { width, height, child: stack.into() };
|
let fixed = FixedNode { width, height, child: stack.into() };
|
||||||
@ -135,7 +135,7 @@ fn ellipse_impl(
|
|||||||
// perfectly into the ellipse.
|
// perfectly into the ellipse.
|
||||||
const PAD: f64 = 0.5 - SQRT_2 / 4.0;
|
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;
|
stack.aspect = aspect;
|
||||||
|
|
||||||
let fixed = FixedNode {
|
let fixed = FixedNode {
|
||||||
|
40
src/library/stack.rs
Normal file
40
src/library/stack.rs
Normal 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
BIN
tests/ref/library/stack.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 264 B |
9
tests/typ/library/stack.typ
Normal file
9
tests/typ/library/stack.typ
Normal 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),
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user