diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-02-06 13:07:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-06 13:07:25 +0100 |
| commit | bfc2f5aefc6c407de0b699b31dafd835fc2c9be3 (patch) | |
| tree | 67c23ec9df3b9f535faf5fbd443e85d9a7813d37 /src/library | |
| parent | dacd7dadc04d4538f1063a86afd676695c7471ab (diff) | |
| parent | a6cae89b47246a235ed7b1093747c6f3bcb64da4 (diff) | |
Merge pull request #17 from typst/rects
Add rectangle function 🎛
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/layout.rs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/library/layout.rs b/src/library/layout.rs index 0e04c507..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; -use crate::layout::{Expansion, NodeFixed, NodeSpacing, NodeStack}; +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. /// @@ -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: Node::Any(fixed_node.into()), + }) + } else { + ctx.push(fixed_node); + } + ctx.state = snapshot; Value::None |
