summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorMartin Haug <mhaug@live.de>2021-02-06 12:30:44 +0100
committerMartin Haug <mhaug@live.de>2021-02-06 12:30:44 +0100
commit80e076814dde330fb2136172580f11e939bc6601 (patch)
tree6cd3474ffa5e2aced38e47a7ef563ab8940612be /src/library
parent8469bad7487e111c8e5a0ec542f0232a0ebb4bdc (diff)
Merge `rect` and `box` 🦚
Diffstat (limited to 'src/library')
-rw-r--r--src/library/insert.rs37
-rw-r--r--src/library/layout.rs20
-rw-r--r--src/library/mod.rs1
3 files changed, 16 insertions, 42 deletions
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::<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/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);