summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorMartin Haug <mhaug@live.de>2021-02-04 21:30:18 +0100
committerMartin Haug <mhaug@live.de>2021-02-04 21:30:18 +0100
commit8469bad7487e111c8e5a0ec542f0232a0ebb4bdc (patch)
tree6d2781a8c6e95a10de048d39f5bd6ebaa82bc6ea /src/library
parentdacd7dadc04d4538f1063a86afd676695c7471ab (diff)
Add rectangle function 🎛
Diffstat (limited to 'src/library')
-rw-r--r--src/library/insert.rs37
-rw-r--r--src/library/mod.rs1
2 files changed, 38 insertions, 0 deletions
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::<ValueTemplate>(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);