diff options
Diffstat (limited to 'src/library/layout')
| -rw-r--r-- | src/library/layout/columns.rs | 2 | ||||
| -rw-r--r-- | src/library/layout/flow.rs | 14 | ||||
| -rw-r--r-- | src/library/layout/grid.rs | 82 | ||||
| -rw-r--r-- | src/library/layout/pad.rs | 6 | ||||
| -rw-r--r-- | src/library/layout/page.rs | 10 | ||||
| -rw-r--r-- | src/library/layout/spacing.rs | 20 | ||||
| -rw-r--r-- | src/library/layout/stack.rs | 24 |
7 files changed, 80 insertions, 78 deletions
diff --git a/src/library/layout/columns.rs b/src/library/layout/columns.rs index b5ec7de9..56e55d57 100644 --- a/src/library/layout/columns.rs +++ b/src/library/layout/columns.rs @@ -14,7 +14,7 @@ pub struct ColumnsNode { #[node] impl ColumnsNode { /// The size of the gutter space between each column. - pub const GUTTER: Linear = Relative::new(0.04).into(); + pub const GUTTER: Relative = Ratio::new(0.04).into(); fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { Ok(Content::block(Self { diff --git a/src/library/layout/flow.rs b/src/library/layout/flow.rs index 9f398277..ffda6925 100644 --- a/src/library/layout/flow.rs +++ b/src/library/layout/flow.rs @@ -100,8 +100,8 @@ pub struct FlowLayouter { full: Size, /// The size used by the frames for the current region. used: Size, - /// The sum of fractional ratios in the current region. - fr: Fractional, + /// The sum of fractions in the current region. + fr: Fraction, /// Spacing and layouted nodes. items: Vec<FlowItem>, /// Finished frames for previous regions. @@ -113,7 +113,7 @@ enum FlowItem { /// Absolute spacing between other items. Absolute(Length), /// Fractional spacing between other items. - Fractional(Fractional), + Fractional(Fraction), /// A frame for a layouted child node and how to align it. Frame(Arc<Frame>, Spec<Align>), /// An absolutely placed frame. @@ -135,7 +135,7 @@ impl FlowLayouter { expand, full, used: Size::zero(), - fr: Fractional::zero(), + fr: Fraction::zero(), items: vec![], finished: vec![], } @@ -144,8 +144,8 @@ impl FlowLayouter { /// Layout spacing. pub fn layout_spacing(&mut self, spacing: Spacing) { match spacing { - Spacing::Linear(v) => { - // Resolve the linear and limit it to the remaining space. + Spacing::Relative(v) => { + // Resolve the spacing and limit it to the remaining space. let resolved = v.resolve(self.full.y); let limited = resolved.min(self.regions.first.y); self.regions.first.y -= limited; @@ -254,7 +254,7 @@ impl FlowLayouter { self.regions.next(); self.full = self.regions.first; self.used = Size::zero(); - self.fr = Fractional::zero(); + self.fr = Fraction::zero(); self.finished.push(Arc::new(output)); } diff --git a/src/library/layout/grid.rs b/src/library/layout/grid.rs index 716ac853..ee485bd2 100644 --- a/src/library/layout/grid.rs +++ b/src/library/layout/grid.rs @@ -54,22 +54,24 @@ impl Layout for GridNode { /// Defines how to size a grid cell along an axis. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum TrackSizing { - /// Fit the cell to its contents. + /// A track that fits its cell's contents. Auto, - /// A length stated in absolute values and/or relative to the parent's size. - Linear(Linear), - /// A length that is the fraction of the remaining free space in the parent. - Fractional(Fractional), + /// A track size specified in absolute terms and relative to the parent's + /// size. + Relative(Relative), + /// A track size specified as a fraction of the remaining free space in the + /// parent. + Fractional(Fraction), } castable! { Vec<TrackSizing>, - Expected: "integer or (auto, linear, fractional, or array thereof)", + Expected: "integer, auto, relative length, fraction, or array of the latter three)", Value::Auto => vec![TrackSizing::Auto], - Value::Length(v) => vec![TrackSizing::Linear(v.into())], - Value::Relative(v) => vec![TrackSizing::Linear(v.into())], - Value::Linear(v) => vec![TrackSizing::Linear(v)], - Value::Fractional(v) => vec![TrackSizing::Fractional(v)], + Value::Length(v) => vec![TrackSizing::Relative(v.into())], + Value::Ratio(v) => vec![TrackSizing::Relative(v.into())], + Value::Relative(v) => vec![TrackSizing::Relative(v)], + Value::Fraction(v) => vec![TrackSizing::Fractional(v)], Value::Int(v) => vec![TrackSizing::Auto; Value::Int(v).cast::<NonZeroUsize>()?.get()], Value::Array(values) => values .into_iter() @@ -79,12 +81,12 @@ castable! { castable! { TrackSizing, - Expected: "auto, linear, or fractional", + Expected: "auto, relative length, or fraction", Value::Auto => Self::Auto, - Value::Length(v) => Self::Linear(v.into()), - Value::Relative(v) => Self::Linear(v.into()), - Value::Linear(v) => Self::Linear(v), - Value::Fractional(v) => Self::Fractional(v), + Value::Length(v) => Self::Relative(v.into()), + Value::Ratio(v) => Self::Relative(v.into()), + Value::Relative(v) => Self::Relative(v), + Value::Fraction(v) => Self::Fractional(v), } /// Performs grid layout. @@ -108,19 +110,19 @@ pub struct GridLayouter<'a> { /// The used-up size of the current region. The horizontal size is /// determined once after columns are resolved and not touched again. used: Size, - /// The sum of fractional ratios in the current region. - fr: Fractional, + /// The sum of fractions in the current region. + fr: Fraction, /// Frames for finished regions. finished: Vec<Arc<Frame>>, } -/// Produced by initial row layout, auto and linear rows are already finished, +/// Produced by initial row layout, auto and relative rows are already finished, /// fractional rows not yet. enum Row { - /// Finished row frame of auto or linear row. + /// Finished row frame of auto or relative row. Frame(Frame), - /// Ratio of a fractional row and y index of the track. - Fr(Fractional, usize), + /// Fractional row with y index. + Fr(Fraction, usize), } impl<'a> GridLayouter<'a> { @@ -150,7 +152,7 @@ impl<'a> GridLayouter<'a> { }; let auto = TrackSizing::Auto; - let zero = TrackSizing::Linear(Linear::zero()); + let zero = TrackSizing::Relative(Relative::zero()); let get_or = |tracks: &[_], idx, default| { tracks.get(idx).or(tracks.last()).copied().unwrap_or(default) }; @@ -190,7 +192,7 @@ impl<'a> GridLayouter<'a> { lrows, full, used: Size::zero(), - fr: Fractional::zero(), + fr: Fraction::zero(), finished: vec![], } } @@ -208,7 +210,7 @@ impl<'a> GridLayouter<'a> { match self.rows[y] { TrackSizing::Auto => self.layout_auto_row(ctx, y)?, - TrackSizing::Linear(v) => self.layout_linear_row(ctx, v, y)?, + TrackSizing::Relative(v) => self.layout_relative_row(ctx, v, y)?, TrackSizing::Fractional(v) => { self.lrows.push(Row::Fr(v, y)); self.fr += v; @@ -222,28 +224,28 @@ impl<'a> GridLayouter<'a> { /// Determine all column sizes. fn measure_columns(&mut self, ctx: &mut Context) -> TypResult<()> { - // Sum of sizes of resolved linear tracks. - let mut linear = Length::zero(); + // Sum of sizes of resolved relative tracks. + let mut rel = Length::zero(); // Sum of fractions of all fractional tracks. - let mut fr = Fractional::zero(); + let mut fr = Fraction::zero(); - // Resolve the size of all linear columns and compute the sum of all + // Resolve the size of all relative columns and compute the sum of all // fractional tracks. for (&col, rcol) in self.cols.iter().zip(&mut self.rcols) { match col { TrackSizing::Auto => {} - TrackSizing::Linear(v) => { + TrackSizing::Relative(v) => { let resolved = v.resolve(self.regions.base.x); *rcol = resolved; - linear += resolved; + rel += resolved; } TrackSizing::Fractional(v) => fr += v, } } // Size that is not used by fixed-size columns. - let available = self.regions.first.x - linear; + let available = self.regions.first.x - rel; if available >= Length::zero() { // Determine size of auto columns. let (auto, count) = self.measure_auto_columns(ctx, available)?; @@ -289,10 +291,10 @@ impl<'a> GridLayouter<'a> { let mut pod = Regions::one(size, self.regions.base, Spec::splat(false)); - // For linear rows, we can already resolve the correct + // For relative rows, we can already resolve the correct // base, for auto it's already correct and for fr we could // only guess anyway. - if let TrackSizing::Linear(v) = self.rows[y] { + if let TrackSizing::Relative(v) = self.rows[y] { pod.base.y = v.resolve(self.regions.base.y); } @@ -310,7 +312,7 @@ impl<'a> GridLayouter<'a> { } /// Distribute remaining space to fractional columns. - fn grow_fractional_columns(&mut self, remaining: Length, fr: Fractional) { + fn grow_fractional_columns(&mut self, remaining: Length, fr: Fraction) { for (&col, rcol) in self.cols.iter().zip(&mut self.rcols) { if let TrackSizing::Fractional(v) = col { *rcol = v.resolve(fr, remaining); @@ -415,12 +417,12 @@ impl<'a> GridLayouter<'a> { Ok(()) } - /// Layout a row with linear height. Such a row cannot break across multiple - /// regions, but it may force a region break. - fn layout_linear_row( + /// Layout a row with relative height. Such a row cannot break across + /// multiple regions, but it may force a region break. + fn layout_relative_row( &mut self, ctx: &mut Context, - v: Linear, + v: Relative, y: usize, ) -> TypResult<()> { let resolved = v.resolve(self.regions.base.y); @@ -457,7 +459,7 @@ impl<'a> GridLayouter<'a> { let size = Size::new(rcol, height); // Set the base to the region's base for auto rows and to the - // size for linear and fractional rows. + // size for relative and fractional rows. let base = Spec::new(self.cols[x], self.rows[y]) .map(|s| s == TrackSizing::Auto) .select(self.regions.base, size); @@ -555,7 +557,7 @@ impl<'a> GridLayouter<'a> { self.regions.next(); self.full = self.regions.first.y; self.used.y = Length::zero(); - self.fr = Fractional::zero(); + self.fr = Fraction::zero(); Ok(()) } diff --git a/src/library/layout/pad.rs b/src/library/layout/pad.rs index 664c63ac..1ec5f124 100644 --- a/src/library/layout/pad.rs +++ b/src/library/layout/pad.rs @@ -4,7 +4,7 @@ use crate::library::prelude::*; #[derive(Debug, Hash)] pub struct PadNode { /// The amount of padding. - pub padding: Sides<Linear>, + pub padding: Sides<Relative>, /// The child node whose sides to pad. pub child: LayoutNode, } @@ -54,7 +54,7 @@ impl Layout for PadNode { } /// Shrink a size by padding relative to the size itself. -fn shrink(size: Size, padding: Sides<Linear>) -> Size { +fn shrink(size: Size, padding: Sides<Relative>) -> Size { size - padding.resolve(size).sum_by_axis() } @@ -77,7 +77,7 @@ fn shrink(size: Size, padding: Sides<Linear>) -> Size { /// <=> w - p.rel * w - p.abs = s /// <=> (1 - p.rel) * w = s + p.abs /// <=> w = (s + p.abs) / (1 - p.rel) -fn grow(size: Size, padding: Sides<Linear>) -> Size { +fn grow(size: Size, padding: Sides<Relative>) -> Size { size.zip(padding.sum_by_axis()) .map(|(s, p)| (s + p.abs).safe_div(1.0 - p.rel.get())) } diff --git a/src/library/layout/page.rs b/src/library/layout/page.rs index c8af4843..abe1786f 100644 --- a/src/library/layout/page.rs +++ b/src/library/layout/page.rs @@ -16,13 +16,13 @@ impl PageNode { /// Whether the page is flipped into landscape orientation. pub const FLIPPED: bool = false; /// The left margin. - pub const LEFT: Smart<Linear> = Smart::Auto; + pub const LEFT: Smart<Relative> = Smart::Auto; /// The right margin. - pub const RIGHT: Smart<Linear> = Smart::Auto; + pub const RIGHT: Smart<Relative> = Smart::Auto; /// The top margin. - pub const TOP: Smart<Linear> = Smart::Auto; + pub const TOP: Smart<Relative> = Smart::Auto; /// The bottom margin. - pub const BOTTOM: Smart<Linear> = Smart::Auto; + pub const BOTTOM: Smart<Relative> = Smart::Auto; /// The page's background color. pub const FILL: Option<Paint> = None; /// How many columns the page has. @@ -90,7 +90,7 @@ impl PageNode { } // Determine the margins. - let default = Linear::from(0.1190 * min); + let default = Relative::from(0.1190 * min); let padding = Sides { left: styles.get(Self::LEFT).unwrap_or(default), right: styles.get(Self::RIGHT).unwrap_or(default), diff --git a/src/library/layout/spacing.rs b/src/library/layout/spacing.rs index 8aea780c..633093e9 100644 --- a/src/library/layout/spacing.rs +++ b/src/library/layout/spacing.rs @@ -23,10 +23,10 @@ impl VNode { /// Kinds of spacing. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum Spacing { - /// A length stated in absolute values and/or relative to the parent's size. - Linear(Linear), - /// A length that is the fraction of the remaining free space in the parent. - Fractional(Fractional), + /// Spacing specified in absolute terms and relative to the parent's size. + Relative(Relative), + /// Spacing specified as a fraction of the remaining free space in the parent. + Fractional(Fraction), } impl Spacing { @@ -38,15 +38,15 @@ impl Spacing { impl From<Length> for Spacing { fn from(length: Length) -> Self { - Self::Linear(length.into()) + Self::Relative(length.into()) } } castable! { Spacing, - Expected: "linear or fractional", - Value::Length(v) => Self::Linear(v.into()), - Value::Relative(v) => Self::Linear(v.into()), - Value::Linear(v) => Self::Linear(v), - Value::Fractional(v) => Self::Fractional(v), + Expected: "relative length or fraction", + Value::Length(v) => Self::Relative(v.into()), + Value::Ratio(v) => Self::Relative(v.into()), + Value::Relative(v) => Self::Relative(v), + Value::Fraction(v) => Self::Fractional(v), } diff --git a/src/library/layout/stack.rs b/src/library/layout/stack.rs index 11d45bb4..b0e2e160 100644 --- a/src/library/layout/stack.rs +++ b/src/library/layout/stack.rs @@ -76,11 +76,11 @@ impl Debug for StackChild { castable! { StackChild, - Expected: "linear, fractional or content", - Value::Length(v) => Self::Spacing(Spacing::Linear(v.into())), - Value::Relative(v) => Self::Spacing(Spacing::Linear(v.into())), - Value::Linear(v) => Self::Spacing(Spacing::Linear(v)), - Value::Fractional(v) => Self::Spacing(Spacing::Fractional(v)), + Expected: "relative length, fraction, or content", + Value::Length(v) => Self::Spacing(Spacing::Relative(v.into())), + Value::Ratio(v) => Self::Spacing(Spacing::Relative(v.into())), + Value::Relative(v) => Self::Spacing(Spacing::Relative(v)), + Value::Fraction(v) => Self::Spacing(Spacing::Fractional(v)), Value::Content(v) => Self::Node(v.pack()), } @@ -98,8 +98,8 @@ pub struct StackLayouter { full: Size, /// The generic size used by the frames for the current region. used: Gen<Length>, - /// The sum of fractional ratios in the current region. - fr: Fractional, + /// The sum of fractions in the current region. + fr: Fraction, /// Already layouted items whose exact positions are not yet known due to /// fractional spacing. items: Vec<StackItem>, @@ -112,7 +112,7 @@ enum StackItem { /// Absolute spacing between other items. Absolute(Length), /// Fractional spacing between other items. - Fractional(Fractional), + Fractional(Fraction), /// A frame for a layouted child node. Frame(Arc<Frame>, Align), } @@ -135,7 +135,7 @@ impl StackLayouter { expand, full, used: Gen::zero(), - fr: Fractional::zero(), + fr: Fraction::zero(), items: vec![], finished: vec![], } @@ -144,8 +144,8 @@ impl StackLayouter { /// Add spacing along the spacing direction. pub fn layout_spacing(&mut self, spacing: Spacing) { match spacing { - Spacing::Linear(v) => { - // Resolve the linear and limit it to the remaining space. + Spacing::Relative(v) => { + // Resolve the spacing and limit it to the remaining space. let resolved = v.resolve(self.regions.base.get(self.axis)); let remaining = self.regions.first.get_mut(self.axis); let limited = resolved.min(*remaining); @@ -247,7 +247,7 @@ impl StackLayouter { self.regions.next(); self.full = self.regions.first; self.used = Gen::zero(); - self.fr = Fractional::zero(); + self.fr = Fraction::zero(); self.finished.push(Arc::new(output)); } |
