diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-08-19 15:31:29 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-08-19 15:52:15 +0200 |
| commit | a6f260ca39f70f82617eca87855789413715f47d (patch) | |
| tree | 08141ae619bd21e0544d21433bce759aebc7ba83 /src/library | |
| parent | fdab7158c91c52a4ace211c804fdd8e9110f56de (diff) | |
Refactor layouting a bit
Notably:
- Handle aspect ratio in fixed node
- Inline constraint inflation into pad node
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/elements.rs | 36 | ||||
| -rw-r--r-- | src/library/layout.rs | 15 | ||||
| -rw-r--r-- | src/library/mod.rs | 1 |
3 files changed, 25 insertions, 27 deletions
diff --git a/src/library/elements.rs b/src/library/elements.rs index f90363bb..6e71626a 100644 --- a/src/library/elements.rs +++ b/src/library/elements.rs @@ -64,18 +64,19 @@ fn rect_impl( body: Template, ) -> Value { Value::Template(Template::from_inline(move |state| { - let mut stack = body.to_stack(state); - stack.aspect = aspect; - - let mut node = FixedNode { width, height, child: stack.into() }.into(); + let mut node = LayoutNode::new(FixedNode { + width, + height, + aspect, + child: body.to_stack(state).into(), + }); if let Some(fill) = fill { - node = BackgroundNode { + node = LayoutNode::new(BackgroundNode { shape: BackgroundShape::Rect, fill: Paint::Color(fill), child: node, - } - .into(); + }); } node @@ -120,27 +121,22 @@ fn ellipse_impl( // perfectly into the ellipse. const PAD: f64 = 0.5 - SQRT_2 / 4.0; - let mut stack = body.to_stack(state); - stack.aspect = aspect; - - let mut node = FixedNode { + let mut node = LayoutNode::new(FixedNode { width, height, - child: PadNode { + aspect, + child: LayoutNode::new(PadNode { padding: Sides::splat(Relative::new(PAD).into()), - child: stack.into(), - } - .into(), - } - .into(); + child: body.to_stack(state).into(), + }), + }); if let Some(fill) = fill { - node = BackgroundNode { + node = LayoutNode::new(BackgroundNode { shape: BackgroundShape::Ellipse, fill: Paint::Color(fill), child: node, - } - .into(); + }); } node diff --git a/src/library/layout.rs b/src/library/layout.rs index b1510cb6..91e2e7f3 100644 --- a/src/library/layout.rs +++ b/src/library/layout.rs @@ -145,8 +145,12 @@ pub fn boxed(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> { let height = args.named("height")?; let body: Template = args.eat().unwrap_or_default(); Ok(Value::Template(Template::from_inline(move |state| { - let child = body.to_stack(state).into(); - FixedNode { width, height, child } + FixedNode { + width, + height, + aspect: None, + child: body.to_stack(state).into(), + } }))) } @@ -190,10 +194,7 @@ pub fn stack(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> { Ok(Value::Template(Template::from_block(move |state| { let children = children .iter() - .map(|child| { - let child = child.to_stack(state).into(); - StackChild::Any(child, state.aligns) - }) + .map(|child| StackChild::Any(child.to_stack(state).into(), state.aligns)) .collect(); let mut dirs = Gen::new(None, dir).unwrap_or(state.dirs); @@ -204,7 +205,7 @@ pub fn stack(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> { dirs.cross = state.dirs.main; } - StackNode { dirs, aspect: None, children } + StackNode { dirs, children } }))) } diff --git a/src/library/mod.rs b/src/library/mod.rs index dd1574f3..44f1f01f 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -21,6 +21,7 @@ use crate::diag::TypResult; use crate::eval::{Arguments, EvalContext, Scope, Str, Template, Value}; use crate::font::{FontFamily, FontStretch, FontStyle, FontWeight, VerticalFontMetric}; use crate::geom::*; +use crate::layout::LayoutNode; use crate::syntax::Spanned; /// Construct a scope containing all standard library definitions. |
