From 1a70cb6a330990dc0ab373905d12458ef87afbad Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 12 Oct 2020 18:01:22 +0200 Subject: =?UTF-8?q?Naming=20and=20grammar=20=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/document.rs | 2 +- src/layout/mod.rs | 21 +++++++++++---------- src/layout/node.rs | 10 +++++----- src/layout/pad.rs | 16 ++++++++-------- src/layout/par.rs | 44 +++++++++++++++++++++++--------------------- src/layout/stack.rs | 46 +++++++++++++++++++++++----------------------- src/layout/text.rs | 2 +- 7 files changed, 72 insertions(+), 69 deletions(-) (limited to 'src/layout') diff --git a/src/layout/document.rs b/src/layout/document.rs index 407dcda4..52d0d124 100644 --- a/src/layout/document.rs +++ b/src/layout/document.rs @@ -34,6 +34,6 @@ impl Pages { pub fn layout(&self, ctx: &mut LayoutContext) -> Vec { let areas = Areas::repeat(self.size); let layouted = self.child.layout(ctx, &areas); - layouted.into_boxes() + layouted.into_layouts() } } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index de112083..362e5a7f 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -37,7 +37,7 @@ pub struct LayoutContext { /// Layout a node. pub trait Layout { - /// Layout the node in the given layout context. + /// Layout the node into the given areas. fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted; } @@ -120,26 +120,27 @@ impl Expansion { } } -/// An item that is produced by [layouting] a node. +/// The result of [layouting] a node. /// /// [layouting]: trait.Layout.html#method.layout #[derive(Debug, Clone, PartialEq)] pub enum Layouted { /// Spacing that should be added to the parent. Spacing(Length), - /// A box that should be added to and aligned in the parent. - Boxed(BoxLayout, Gen), - /// Multiple boxes. - Boxes(Vec<(BoxLayout, Gen)>), + /// A layout that should be added to and aligned in the parent. + Layout(BoxLayout, Gen), + /// Multiple layouts. + Layouts(Vec, Gen), } impl Layouted { - /// Return the box if this if its a box variant. - pub fn into_boxes(self) -> Vec { + /// Return all layouts contained in this variant (zero, one or arbitrarily + /// many). + pub fn into_layouts(self) -> Vec { match self { Self::Spacing(_) => vec![], - Self::Boxed(boxed, _) => vec![boxed], - Self::Boxes(boxes) => boxes.into_iter().map(|p| p.0).collect(), + Self::Layout(layout, _) => vec![layout], + Self::Layouts(layouts, _) => layouts, } } } diff --git a/src/layout/node.rs b/src/layout/node.rs index aa4b990f..6b264eb0 100644 --- a/src/layout/node.rs +++ b/src/layout/node.rs @@ -29,7 +29,7 @@ impl Layout for LayoutNode { match self { Self::Spacing(spacing) => spacing.layout(ctx, areas), Self::Text(text) => text.layout(ctx, areas), - Self::Dyn(boxed) => boxed.layout(ctx, areas), + Self::Dyn(dynamic) => dynamic.layout(ctx, areas), } } } @@ -39,16 +39,16 @@ impl Debug for LayoutNode { match self { Self::Spacing(spacing) => spacing.fmt(f), Self::Text(text) => text.fmt(f), - Self::Dyn(boxed) => boxed.fmt(f), + Self::Dyn(dynamic) => dynamic.fmt(f), } } } -/// A wrapper around a boxed dynamic node. +/// A wrapper around a boxed node trait object. /// /// _Note_: This is needed because the compiler can't `derive(PartialEq)` for -/// [`LayoutNode`] when directly putting the boxed node in there, see -/// the [Rust Issue]. +/// [`LayoutNode`] when directly putting the `Box` in there, see the +/// [Rust Issue]. /// /// [`LayoutNode`]: enum.LayoutNode.html /// [Rust Issue]: https://github.com/rust-lang/rust/issues/31740 diff --git a/src/layout/pad.rs b/src/layout/pad.rs index b26bea71..f574e823 100644 --- a/src/layout/pad.rs +++ b/src/layout/pad.rs @@ -17,10 +17,10 @@ impl Layout for Pad { let mut layouted = self.child.layout(ctx, &areas); match &mut layouted { Layouted::Spacing(_) => {} - Layouted::Boxed(boxed, _) => pad_box(boxed, self.padding), - Layouted::Boxes(boxes) => { - for (boxed, _) in boxes { - pad_box(boxed, self.padding); + Layouted::Layout(layout, _) => pad_layout(layout, self.padding), + Layouted::Layouts(layouts, _) => { + for layout in layouts { + pad_layout(layout, self.padding); } } } @@ -43,12 +43,12 @@ fn shrink_areas(areas: &Areas, padding: Sides) -> Areas { } /// Enlarge the box and move all elements inwards. -fn pad_box(boxed: &mut BoxLayout, padding: Sides) { - let padding = padding.eval(boxed.size); +fn pad_layout(layout: &mut BoxLayout, padding: Sides) { + let padding = padding.eval(layout.size); let origin = Point::new(padding.left, padding.top); - boxed.size += padding.size(); - for (point, _) in &mut boxed.elements { + layout.size += padding.size(); + for (point, _) in &mut layout.elements { *point += origin; } } diff --git a/src/layout/par.rs b/src/layout/par.rs index ad71cffc..b1bba790 100644 --- a/src/layout/par.rs +++ b/src/layout/par.rs @@ -23,16 +23,18 @@ impl Layout for Par { let mut layouter = ParLayouter::new(self, areas.clone()); for child in &self.children { match child.layout(ctx, &layouter.areas) { - Layouted::Spacing(spacing) => layouter.spacing(spacing), - Layouted::Boxed(boxed, aligns) => layouter.boxed(boxed, aligns.cross), - Layouted::Boxes(boxes) => { - for (boxed, aligns) in boxes { - layouter.boxed(boxed, aligns.cross); + Layouted::Spacing(spacing) => layouter.push_spacing(spacing), + Layouted::Layout(layout, aligns) => { + layouter.push_layout(layout, aligns.cross) + } + Layouted::Layouts(layouts, aligns) => { + for layout in layouts { + layouter.push_layout(layout, aligns.cross); } } } } - Layouted::Boxes(layouter.finish()) + Layouted::Layouts(layouter.finish(), self.aligns) } } @@ -48,7 +50,7 @@ struct ParLayouter<'a> { cross: SpecAxis, dirs: Gen, areas: Areas, - layouted: Vec<(BoxLayout, Gen)>, + finished: Vec, lines: Vec<(Length, BoxLayout, Align)>, lines_size: Gen, run: Vec<(Length, BoxLayout, Align)>, @@ -64,7 +66,7 @@ impl<'a> ParLayouter<'a> { cross: par.dirs.cross.axis(), dirs: par.dirs, areas, - layouted: vec![], + finished: vec![], lines: vec![], lines_size: Gen::ZERO, run: vec![], @@ -73,12 +75,12 @@ impl<'a> ParLayouter<'a> { } } - fn spacing(&mut self, amount: Length) { - let cross_full = self.areas.current.rem.get(self.cross); - self.run_size.cross = (self.run_size.cross + amount).min(cross_full); + fn push_spacing(&mut self, amount: Length) { + let cross_max = self.areas.current.rem.get(self.cross); + self.run_size.cross = (self.run_size.cross + amount).min(cross_max); } - fn boxed(&mut self, layout: BoxLayout, align: Align) { + fn push_layout(&mut self, layout: BoxLayout, align: Align) { if self.run_ruler > align { self.finish_run(); } @@ -112,12 +114,12 @@ impl<'a> ParLayouter<'a> { } fn finish_run(&mut self) { - let size = Gen::new(self.run_size.main, match self.par.cross_expansion { + let full_size = Gen::new(self.run_size.main, match self.par.cross_expansion { Expansion::Fill => self.areas.current.full.get(self.cross), Expansion::Fit => self.run_size.cross, }); - let mut output = BoxLayout::new(size.switch(self.dirs).to_size()); + let mut output = BoxLayout::new(full_size.switch(self.dirs).to_size()); for (before, layout, align) in std::mem::take(&mut self.run) { let child_cross_size = layout.size.get(self.cross); @@ -125,11 +127,11 @@ impl<'a> ParLayouter<'a> { // Position along the cross axis. let cross = align.apply(if self.dirs.cross.is_positive() { let after_with_self = self.run_size.cross - before; - before .. size.cross - after_with_self + before .. full_size.cross - after_with_self } else { let before_with_self = before + child_cross_size; let after = self.run_size.cross - (before + child_cross_size); - size.cross - before_with_self .. after + full_size.cross - before_with_self .. after }); let pos = Gen::new(Length::ZERO, cross).switch(self.dirs).to_point(); @@ -138,10 +140,10 @@ impl<'a> ParLayouter<'a> { self.lines.push((self.lines_size.main, output, self.run_ruler)); - let main_offset = size.main + self.par.line_spacing; + let main_offset = full_size.main + self.par.line_spacing; *self.areas.current.rem.get_mut(self.main) -= main_offset; self.lines_size.main += main_offset; - self.lines_size.cross = self.lines_size.cross.max(size.cross); + self.lines_size.cross = self.lines_size.cross.max(full_size.cross); self.run_size = Gen::ZERO; self.run_ruler = Align::Start; @@ -172,15 +174,15 @@ impl<'a> ParLayouter<'a> { output.push_layout(pos, run); } - self.layouted.push((output, self.par.aligns)); + self.finished.push(output); self.areas.next(); self.lines_size = Gen::ZERO; } - fn finish(mut self) -> Vec<(BoxLayout, Gen)> { + fn finish(mut self) -> Vec { self.finish_run(); self.finish_area(); - self.layouted + self.finished } } diff --git a/src/layout/stack.rs b/src/layout/stack.rs index 228293bc..13c585e3 100644 --- a/src/layout/stack.rs +++ b/src/layout/stack.rs @@ -21,16 +21,16 @@ impl Layout for Stack { let mut layouter = StackLayouter::new(self, areas.clone()); for child in &self.children { match child.layout(ctx, &layouter.areas) { - Layouted::Spacing(spacing) => layouter.spacing(spacing), - Layouted::Boxed(boxed, aligns) => layouter.boxed(boxed, aligns), - Layouted::Boxes(boxes) => { - for (boxed, aligns) in boxes { - layouter.boxed(boxed, aligns); + Layouted::Spacing(spacing) => layouter.push_spacing(spacing), + Layouted::Layout(layout, aligns) => layouter.push_layout(layout, aligns), + Layouted::Layouts(layouts, aligns) => { + for layout in layouts { + layouter.push_layout(layout, aligns); } } } } - Layouted::Boxes(layouter.finish()) + Layouted::Layouts(layouter.finish(), self.aligns) } } @@ -45,8 +45,8 @@ struct StackLayouter<'a> { main: SpecAxis, dirs: Gen, areas: Areas, - layouted: Vec<(BoxLayout, Gen)>, - boxes: Vec<(Length, BoxLayout, Gen)>, + finished: Vec, + layouts: Vec<(Length, BoxLayout, Gen)>, used: Gen, ruler: Align, } @@ -58,21 +58,21 @@ impl<'a> StackLayouter<'a> { main: stack.dirs.main.axis(), dirs: stack.dirs, areas, - layouted: vec![], - boxes: vec![], + finished: vec![], + layouts: vec![], used: Gen::ZERO, ruler: Align::Start, } } - fn spacing(&mut self, amount: Length) { + fn push_spacing(&mut self, amount: Length) { let main_rest = self.areas.current.rem.get_mut(self.main); let capped = amount.min(*main_rest); *main_rest -= capped; self.used.main += capped; } - fn boxed(&mut self, layout: BoxLayout, aligns: Gen) { + fn push_layout(&mut self, layout: BoxLayout, aligns: Gen) { if self.ruler > aligns.main { self.finish_area(); } @@ -88,7 +88,7 @@ impl<'a> StackLayouter<'a> { } let size = layout.size.switch(self.dirs); - self.boxes.push((self.used.main, layout, aligns)); + self.layouts.push((self.used.main, layout, aligns)); *self.areas.current.rem.get_mut(self.main) -= size.main; self.used.main += size.main; @@ -97,7 +97,7 @@ impl<'a> StackLayouter<'a> { } fn finish_area(&mut self) { - let size = { + let full_size = { let full = self.areas.current.full.switch(self.dirs); Gen::new( match self.stack.expansion.main { @@ -111,41 +111,41 @@ impl<'a> StackLayouter<'a> { ) }; - let mut output = BoxLayout::new(size.switch(self.dirs).to_size()); + let mut output = BoxLayout::new(full_size.switch(self.dirs).to_size()); - for (before, layout, aligns) in std::mem::take(&mut self.boxes) { + for (before, layout, aligns) in std::mem::take(&mut self.layouts) { let child_size = layout.size.switch(self.dirs); // Align along the main axis. let main = aligns.main.apply(if self.dirs.main.is_positive() { let after_with_self = self.used.main - before; - before .. size.main - after_with_self + before .. full_size.main - after_with_self } else { let before_with_self = before + child_size.main; let after = self.used.main - (before + child_size.main); - size.main - before_with_self .. after + full_size.main - before_with_self .. after }); // Align along the cross axis. let cross = aligns.cross.apply(if self.dirs.cross.is_positive() { - Length::ZERO .. size.cross - child_size.cross + Length::ZERO .. full_size.cross - child_size.cross } else { - size.cross - child_size.cross .. Length::ZERO + full_size.cross - child_size.cross .. Length::ZERO }); let pos = Gen::new(main, cross).switch(self.dirs).to_point(); output.push_layout(pos, layout); } - self.layouted.push((output, self.stack.aligns)); + self.finished.push(output); self.areas.next(); self.used = Gen::ZERO; self.ruler = Align::Start; } - fn finish(mut self) -> Vec<(BoxLayout, Gen)> { + fn finish(mut self) -> Vec { self.finish_area(); - self.layouted + self.finished } } diff --git a/src/layout/text.rs b/src/layout/text.rs index 31ae19cd..0ded4f9d 100644 --- a/src/layout/text.rs +++ b/src/layout/text.rs @@ -26,7 +26,7 @@ pub struct Text { impl Layout for Text { fn layout(&self, ctx: &mut LayoutContext, _: &Areas) -> Layouted { let mut loader = ctx.loader.borrow_mut(); - Layouted::Boxed( + Layouted::Layout( shaping::shape( &mut loader, &self.text, -- cgit v1.2.3