diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-03-10 17:42:47 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-03-10 17:42:47 +0100 |
| commit | 4e5f85aa4ac0d6b51323bb2a6e1fbd3f4f46babb (patch) | |
| tree | 87f157c2058800e510e5538b0815c947c14fe22d /src/library | |
| parent | b0446cbdd189a8cc3b6a7321579f4aa89415a8e0 (diff) | |
Pad function 🔲
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/mod.rs | 3 | ||||
| -rw-r--r-- | src/library/pad.rs | 38 | ||||
| -rw-r--r-- | src/library/shapes.rs | 22 |
3 files changed, 47 insertions, 16 deletions
diff --git a/src/library/mod.rs b/src/library/mod.rs index f846e6ee..54d6a14c 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -7,6 +7,7 @@ mod align; mod base; mod font; mod image; +mod pad; mod page; mod shapes; mod spacing; @@ -15,6 +16,7 @@ pub use self::image::*; pub use align::*; pub use base::*; pub use font::*; +pub use pad::*; pub use page::*; pub use shapes::*; pub use spacing::*; @@ -46,6 +48,7 @@ pub fn new() -> Scope { set!(func: "font", font); set!(func: "h", h); set!(func: "image", image); + set!(func: "pad", pad); set!(func: "page", page); set!(func: "pagebreak", pagebreak); set!(func: "repr", repr); diff --git a/src/library/pad.rs b/src/library/pad.rs new file mode 100644 index 00000000..f9e4386d --- /dev/null +++ b/src/library/pad.rs @@ -0,0 +1,38 @@ +use super::*; + +/// `pad`: Pad content at the sides. +/// +/// # Positional arguments +/// - Padding for all sides: `padding`, of type `linear` relative to sides. +/// - Body: of type `template`. +/// +/// # Named arguments +/// - Left padding: `left`, of type `linear` relative to parent width. +/// - Right padding: `right`, of type `linear` relative to parent width. +/// - Top padding: `top`, of type `linear` relative to parent height. +/// - Bottom padding: `bottom`, of type `linear` relative to parent height. +pub fn pad(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value { + let all = args.find(ctx); + let left = args.get(ctx, "left"); + let top = args.get(ctx, "top"); + let right = args.get(ctx, "right"); + let bottom = args.get(ctx, "bottom"); + let body = args.require::<ValueTemplate>(ctx, "body").unwrap_or_default(); + + let padding = Sides::new( + left.or(all).unwrap_or_default(), + top.or(all).unwrap_or_default(), + right.or(all).unwrap_or_default(), + bottom.or(all).unwrap_or_default(), + ); + + Value::template("pad", move |ctx| { + let snapshot = ctx.state.clone(); + + let expand = Spec::uniform(Expansion::Fit); + let child = ctx.exec_body(&body, expand); + ctx.push(NodePad { padding, child }); + + ctx.state = snapshot; + }) +} diff --git a/src/library/shapes.rs b/src/library/shapes.rs index f9685fce..5e638c01 100644 --- a/src/library/shapes.rs +++ b/src/library/shapes.rs @@ -24,28 +24,18 @@ pub fn box_(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value { let main = args.get(ctx, "main-dir"); let cross = args.get(ctx, "cross-dir"); let color = args.get(ctx, "color"); - let body = args.find::<ValueTemplate>(ctx); + let body = args.find::<ValueTemplate>(ctx).unwrap_or_default(); + + let fill_if = |c| if c { Expansion::Fill } else { Expansion::Fit }; + let expand = Spec::new(fill_if(width.is_some()), fill_if(height.is_some())); Value::template("box", move |ctx| { let snapshot = ctx.state.clone(); ctx.set_dirs(Gen::new(main, cross)); - let dirs = ctx.state.dirs; - let align = ctx.state.align; - - ctx.start_content_group(); - if let Some(body) = &body { - body.exec(ctx); - } - let children = ctx.end_content_group(); - let fill_if = |c| if c { Expansion::Fill } else { Expansion::Fit }; - let expand = Spec::new(fill_if(width.is_some()), fill_if(height.is_some())); - let fixed = NodeFixed { - width, - height, - child: NodeStack { dirs, align, expand, children }.into(), - }; + let child = ctx.exec_body(&body, expand); + let fixed = NodeFixed { width, height, child }; if let Some(color) = color { ctx.push(NodeBackground { |
