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 From 80e076814dde330fb2136172580f11e939bc6601 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Sat, 6 Feb 2021 12:30:44 +0100 Subject: =?UTF-8?q?Merge=20`rect`=20and=20`box`=20=F0=9F=A6=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/library/insert.rs | 37 ------------------------------------- src/library/layout.rs | 20 ++++++++++++++++---- src/library/mod.rs | 1 - 3 files changed, 16 insertions(+), 42 deletions(-) (limited to 'src/library') diff --git a/src/library/insert.rs b/src/library/insert.rs index 169fad97..58e8a11c 100644 --- a/src/library/insert.rs +++ b/src/library/insert.rs @@ -4,43 +4,6 @@ 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/layout.rs b/src/library/layout.rs index 0e04c507..bbcd7898 100644 --- a/src/library/layout.rs +++ b/src/library/layout.rs @@ -1,7 +1,7 @@ use std::fmt::{self, Display, Formatter}; -use crate::eval::Softness; -use crate::layout::{Expansion, NodeFixed, NodeSpacing, NodeStack}; +use crate::{eval::Softness, layout::NodeBackground}; +use crate::layout::{Expansion, Fill, NodeFixed, NodeSpacing, NodeStack}; use crate::paper::{Paper, PaperClass}; use crate::prelude::*; @@ -175,6 +175,7 @@ impl Display for Alignment { /// # 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. +/// - Background color of the box: `color`, of type `color`. pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value { let snapshot = ctx.state.clone(); @@ -182,6 +183,7 @@ pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value { let height = args.get(ctx, "height"); let main = args.get(ctx, "main-dir"); let cross = args.get(ctx, "cross-dir"); + let color = args.get(ctx, "color"); ctx.set_dirs(Gen::new(main, cross)); @@ -199,11 +201,21 @@ pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value { 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(NodeFixed { + let fixed_node = NodeFixed { width, height, child: NodeStack { dirs, align, expand, children }.into(), - }); + }; + + if let Some(color) = color { + ctx.push(NodeBackground { + fill: Fill::Color(color), + child: fixed_node, + }) + } else { + ctx.push(fixed_node); + } + ctx.state = snapshot; Value::None diff --git a/src/library/mod.rs b/src/library/mod.rs index 7e20f5fb..48da093b 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -38,7 +38,6 @@ 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 From a6cae89b47246a235ed7b1093747c6f3bcb64da4 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Sat, 6 Feb 2021 12:54:44 +0100 Subject: =?UTF-8?q?Generalize=20child=20of=20NodeBackground=20=F0=9F=8D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/library/layout.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/library') diff --git a/src/library/layout.rs b/src/library/layout.rs index bbcd7898..2812a48f 100644 --- a/src/library/layout.rs +++ b/src/library/layout.rs @@ -1,9 +1,9 @@ use std::fmt::{self, Display, Formatter}; -use crate::{eval::Softness, layout::NodeBackground}; use crate::layout::{Expansion, Fill, NodeFixed, NodeSpacing, NodeStack}; use crate::paper::{Paper, PaperClass}; use crate::prelude::*; +use crate::{eval::Softness, layout::NodeBackground}; /// `align`: Align content along the layouting axes. /// @@ -210,7 +210,7 @@ pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value { if let Some(color) = color { ctx.push(NodeBackground { fill: Fill::Color(color), - child: fixed_node, + child: Node::Any(fixed_node.into()), }) } else { ctx.push(fixed_node); -- cgit v1.2.3