From 1003d320d40aa46a0a3fba49b09425ead1b4b584 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 17 May 2021 15:00:29 +0200 Subject: [PATCH] Rename expand to fixed and switch to bools --- src/layout/fixed.rs | 9 ++----- src/layout/frame.rs | 1 + src/layout/mod.rs | 66 +++++++++++++++++---------------------------- src/layout/par.rs | 5 ++-- src/layout/stack.rs | 8 +++--- 5 files changed, 34 insertions(+), 55 deletions(-) diff --git a/src/layout/fixed.rs b/src/layout/fixed.rs index 04ea5a3a9..5905fa952 100644 --- a/src/layout/fixed.rs +++ b/src/layout/fixed.rs @@ -31,13 +31,8 @@ impl Layout for FixedNode { size = Size::new(width, width / aspect); } - let fill_if = |cond| if cond { Expand::Fill } else { Expand::Fit }; - let expand = Spec::new( - fill_if(self.width.is_some()), - fill_if(self.height.is_some()), - ); - - let areas = Areas::once(size, full, expand).with_aspect(self.aspect); + let fixed = Spec::new(self.width.is_some(), self.height.is_some()); + let areas = Areas::once(size, full, fixed).with_aspect(self.aspect); self.child.layout(ctx, &areas) } } diff --git a/src/layout/frame.rs b/src/layout/frame.rs index ea0f6aa89..0a07ca0c6 100644 --- a/src/layout/frame.rs +++ b/src/layout/frame.rs @@ -18,6 +18,7 @@ pub struct Frame { impl Frame { /// Create a new, empty frame. pub fn new(size: Size, baseline: Length) -> Self { + assert!(size.is_finite()); Self { size, baseline, elements: vec![] } } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 6f28fcb9b..dad378f5c 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -54,7 +54,11 @@ pub struct PageRun { impl PageRun { /// Layout the page run. pub fn layout(&self, ctx: &mut LayoutContext) -> Vec { - let areas = Areas::repeat(self.size, Spec::uniform(Expand::Fill)); + // When one of the lengths is infinite the page fits its content along + // that axis. + let Size { width, height } = self.size; + let fixed = Spec::new(width.is_finite(), height.is_finite()); + let areas = Areas::repeat(self.size, fixed); self.child.layout(ctx, &areas) } } @@ -146,12 +150,11 @@ pub struct Areas { pub backlog: Vec, /// The final area that is repeated when the backlog is empty. pub last: Option, - /// Whether the frames resulting from layouting into this areas should be - /// shrunk to fit their content or expanded to fill the area. + /// Whether the frames resulting from layouting into this areas should + /// expand to the fixed size defined by `current`. /// - /// This property is handled partially by the par layouter and fully by the - /// stack layouter. - pub expand: Spec, + /// If this is false, the frame will shrink to fit its content. + pub fixed: Spec, /// The aspect ratio the resulting frame should respect. /// /// This property is only handled by the stack layouter. @@ -159,26 +162,26 @@ pub struct Areas { } impl Areas { - /// Create a new sequence of areas that repeats `area` indefinitely. - pub fn repeat(size: Size, expand: Spec) -> Self { - Self { - current: size, - full: size, - backlog: vec![], - last: Some(size), - expand, - aspect: None, - } - } - /// Create a new length-1 sequence of areas with just one `area`. - pub fn once(size: Size, full: Size, expand: Spec) -> Self { + pub fn once(size: Size, full: Size, fixed: Spec) -> Self { Self { current: size, full, backlog: vec![], last: None, - expand, + fixed, + aspect: None, + } + } + + /// Create a new sequence of areas that repeats `area` indefinitely. + pub fn repeat(size: Size, fixed: Spec) -> Self { + Self { + current: size, + full: size, + backlog: vec![], + last: Some(size), + fixed, aspect: None, } } @@ -199,7 +202,7 @@ impl Areas { full: f(self.full), backlog: self.backlog.iter().copied().map(|s| f(s)).collect(), last: self.last.map(f), - expand: self.expand, + fixed: self.fixed, aspect: self.aspect, } } @@ -222,24 +225,3 @@ impl Areas { }) } } - -/// Whether to expand or shrink a node along an axis. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub enum Expand { - /// Fit the content. - Fit, - /// Fill the available space. - Fill, -} - -impl Expand { - /// Resolve the expansion to either the `fit` or `fill` length. - /// - /// Prefers `fit` if `fill` is infinite. - pub fn resolve(self, fit: Length, fill: Length) -> Length { - match self { - Self::Fill if fill.is_finite() => fill, - _ => fit, - } - } -} diff --git a/src/layout/par.rs b/src/layout/par.rs index f7d679810..7d7f59514 100644 --- a/src/layout/par.rs +++ b/src/layout/par.rs @@ -301,8 +301,9 @@ impl<'a> LineStack<'a> { } fn finish_area(&mut self, ctx: &mut LayoutContext) { - let expand = self.areas.expand.horizontal; - self.size.width = expand.resolve(self.size.width, self.areas.full.width); + if self.areas.fixed.horizontal { + self.size.width = self.areas.current.width; + } let mut output = Frame::new(self.size, self.size.height); let mut first = true; diff --git a/src/layout/stack.rs b/src/layout/stack.rs index b69936ba8..a73abeeb9 100644 --- a/src/layout/stack.rs +++ b/src/layout/stack.rs @@ -104,12 +104,12 @@ impl StackLayouter { fn finish_area(&mut self) { let full_size = { - let Areas { current, full, expand, .. } = self.areas; - let used = self.size.switch(self.main).to_size(); + let Areas { current, full, fixed, .. } = self.areas; + let used = self.size.switch(self.main).to_size(); let mut size = Size::new( - expand.horizontal.resolve(used.width, full.width), - expand.vertical.resolve(used.height, full.height), + if fixed.horizontal { full.width } else { used.width }, + if fixed.vertical { full.height } else { used.height }, ); if let Some(aspect) = self.areas.aspect {