From 8469bad7487e111c8e5a0ec542f0232a0ebb4bdc Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Thu, 4 Feb 2021 21:30:18 +0100 Subject: =?UTF-8?q?Add=20rectangle=20function=20=F0=9F=8E=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/library/insert.rs | 37 +++++++++++++++++++++++++++++++++++++ src/library/mod.rs | 1 + 2 files changed, 38 insertions(+) (limited to 'src/library') diff --git a/src/library/insert.rs b/src/library/insert.rs index 58e8a11c..169fad97 100644 --- a/src/library/insert.rs +++ b/src/library/insert.rs @@ -4,6 +4,43 @@ use crate::env::{ImageResource, ResourceId}; use crate::layout::*; use crate::prelude::*; +/// `rect`: Layout content into a rectangle that also might have a fill. +/// +/// # Named arguments +/// - Width of the box: `width`, of type `linear` relative to parent width. +/// - Height of the box: `height`, of type `linear` relative to parent height. +pub fn rect(ctx: &mut EvalContext, args: &mut Args) -> Value { + let snapshot = ctx.state.clone(); + + let width = args.get(ctx, "width"); + let height = args.get(ctx, "height"); + let color = args.get(ctx, "color"); + + let dirs = ctx.state.dirs; + let align = ctx.state.align; + + ctx.start_content_group(); + + if let Some(body) = args.find::(ctx) { + body.eval(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())); + + ctx.push(NodeRect { + width, + height, + color, + child: NodeStack { dirs, align, expand, children }.into(), + }); + + ctx.state = snapshot; + Value::None +} + /// `image`: Insert an image. /// /// Supports PNG and JPEG files. diff --git a/src/library/mod.rs b/src/library/mod.rs index 48da093b..7e20f5fb 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -38,6 +38,7 @@ pub fn new() -> Scope { set!(func: "image", image); set!(func: "page", page); set!(func: "pagebreak", pagebreak); + set!(func: "rect", rect); set!(func: "rgb", rgb); set!(func: "type", type_); set!(func: "v", v); -- cgit v1.2.3