Rename geometric eval and apply to resolve ✏

This commit is contained in:
Laurenz 2020-10-13 13:24:33 +02:00
parent 22697f0c0c
commit 8680fcd490
11 changed files with 27 additions and 28 deletions

View File

@ -173,7 +173,7 @@ impl EvalContext {
self.start_group(ParGroup { self.start_group(ParGroup {
dirs: self.state.dirs, dirs: self.state.dirs,
aligns: self.state.aligns, aligns: self.state.aligns,
line_spacing: self.state.par.line_spacing.eval(em), line_spacing: self.state.par.line_spacing.resolve(em),
}); });
} }
@ -299,7 +299,7 @@ impl Eval for SynNode {
SynNode::Space => { SynNode::Space => {
let em = ctx.state.font.font_size(); let em = ctx.state.font.font_size();
ctx.push(Spacing { ctx.push(Spacing {
amount: ctx.state.par.word_spacing.eval(em), amount: ctx.state.par.word_spacing.resolve(em),
softness: Softness::Soft, softness: Softness::Soft,
}); });
} }
@ -318,7 +318,7 @@ impl Eval for SynNode {
ctx.end_par_group(); ctx.end_par_group();
let em = ctx.state.font.font_size(); let em = ctx.state.font.font_size();
ctx.push(Spacing { ctx.push(Spacing {
amount: ctx.state.par.par_spacing.eval(em), amount: ctx.state.par.par_spacing.resolve(em),
softness: Softness::Soft, softness: Softness::Soft,
}); });
ctx.start_par_group(); ctx.start_par_group();
@ -358,7 +358,7 @@ impl Eval for NodeRaw {
families.flatten(); families.flatten();
let em = ctx.state.font.font_size(); let em = ctx.state.font.font_size();
let line_spacing = ctx.state.par.line_spacing.eval(em); let line_spacing = ctx.state.par.line_spacing.resolve(em);
let mut children = vec![]; let mut children = vec![];
for line in &self.lines { for line in &self.lines {

View File

@ -121,7 +121,7 @@ pub struct FontState {
impl FontState { impl FontState {
/// The absolute font size. /// The absolute font size.
pub fn font_size(&self) -> Length { pub fn font_size(&self) -> Length {
self.scale.eval(self.size) self.scale.resolve(self.size)
} }
} }

View File

@ -13,7 +13,7 @@ pub enum Align {
impl Align { impl Align {
/// Returns the position of this alignment in the given range. /// Returns the position of this alignment in the given range.
pub fn apply(self, range: Range<Length>) -> Length { pub fn resolve(self, range: Range<Length>) -> Length {
match self { match self {
Self::Start => range.start, Self::Start => range.start,
Self::Center => (range.start + range.end) / 2.0, Self::Center => (range.start + range.end) / 2.0,

View File

@ -21,10 +21,9 @@ impl Linear {
Self { rel, abs } Self { rel, abs }
} }
/// Evaluate the linear length with `one` being `100%` for the relative /// Resolve this relative to the given `length`.
/// part. pub fn resolve(self, length: Length) -> Length {
pub fn eval(self, one: Length) -> Length { self.rel.resolve(length) + self.abs
self.rel.eval(one) + self.abs
} }
/// Whether this linear's relative part is zero. /// Whether this linear's relative part is zero.

View File

@ -26,9 +26,9 @@ impl Relative {
self.0 self.0
} }
/// Evaluate the relative length with `one` being `100%`. /// Resolve this relative to the given `length`.
pub fn eval(self, one: Length) -> Length { pub fn resolve(self, length: Length) -> Length {
self.get() * one self.get() * length
} }
} }

View File

@ -34,13 +34,13 @@ impl<T> Sides<T> {
} }
impl Sides<Linear> { impl Sides<Linear> {
/// Evaluate the linear values in this container. /// Resolve the linear margins relative to the given `size`.
pub fn eval(self, size: Size) -> Sides<Length> { pub fn resolve(self, size: Size) -> Sides<Length> {
Sides { Sides {
left: self.left.eval(size.width), left: self.left.resolve(size.width),
top: self.top.eval(size.height), top: self.top.resolve(size.height),
right: self.right.eval(size.width), right: self.right.resolve(size.width),
bottom: self.bottom.eval(size.height), bottom: self.bottom.resolve(size.height),
} }
} }
} }

View File

@ -16,8 +16,8 @@ impl Layout for Fixed {
fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted { fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
let Area { rem, full } = areas.current; let Area { rem, full } = areas.current;
let size = Size::new( let size = Size::new(
self.width.map(|w| w.eval(full.width)).unwrap_or(rem.width), self.width.map(|w| w.resolve(full.width)).unwrap_or(rem.width),
self.height.map(|h| h.eval(full.height)).unwrap_or(rem.height), self.height.map(|h| h.resolve(full.height)).unwrap_or(rem.height),
); );
let areas = Areas::once(size); let areas = Areas::once(size);

View File

@ -31,7 +31,7 @@ impl Layout for Pad {
/// Shrink all areas by the padding. /// Shrink all areas by the padding.
fn shrink_areas(areas: &Areas, padding: Sides<Linear>) -> Areas { fn shrink_areas(areas: &Areas, padding: Sides<Linear>) -> Areas {
let shrink = |size| size - padding.eval(size).size(); let shrink = |size| size - padding.resolve(size).size();
Areas { Areas {
current: Area { current: Area {
rem: shrink(areas.current.rem), rem: shrink(areas.current.rem),
@ -44,7 +44,7 @@ fn shrink_areas(areas: &Areas, padding: Sides<Linear>) -> Areas {
/// Enlarge the box and move all elements inwards. /// Enlarge the box and move all elements inwards.
fn pad_layout(layout: &mut BoxLayout, padding: Sides<Linear>) { fn pad_layout(layout: &mut BoxLayout, padding: Sides<Linear>) {
let padding = padding.eval(layout.size); let padding = padding.resolve(layout.size);
let origin = Point::new(padding.left, padding.top); let origin = Point::new(padding.left, padding.top);
layout.size += padding.size(); layout.size += padding.size();

View File

@ -125,7 +125,7 @@ impl<'a> ParLayouter<'a> {
let child_cross_size = layout.size.get(self.cross); let child_cross_size = layout.size.get(self.cross);
// Position along the cross axis. // Position along the cross axis.
let cross = align.apply(if self.dirs.cross.is_positive() { let cross = align.resolve(if self.dirs.cross.is_positive() {
let after_with_self = self.run_size.cross - before; let after_with_self = self.run_size.cross - before;
before .. full_size.cross - after_with_self before .. full_size.cross - after_with_self
} else { } else {
@ -164,7 +164,7 @@ impl<'a> ParLayouter<'a> {
}; };
// Align along the cross axis. // Align along the cross axis.
let cross = cross_align.apply(if self.dirs.cross.is_positive() { let cross = cross_align.resolve(if self.dirs.cross.is_positive() {
Length::ZERO .. size.cross - child_size.cross Length::ZERO .. size.cross - child_size.cross
} else { } else {
size.cross - child_size.cross .. Length::ZERO size.cross - child_size.cross .. Length::ZERO

View File

@ -117,7 +117,7 @@ impl<'a> StackLayouter<'a> {
let child_size = layout.size.switch(self.dirs); let child_size = layout.size.switch(self.dirs);
// Align along the main axis. // Align along the main axis.
let main = aligns.main.apply(if self.dirs.main.is_positive() { let main = aligns.main.resolve(if self.dirs.main.is_positive() {
let after_with_self = self.used.main - before; let after_with_self = self.used.main - before;
before .. full_size.main - after_with_self before .. full_size.main - after_with_self
} else { } else {
@ -127,7 +127,7 @@ impl<'a> StackLayouter<'a> {
}); });
// Align along the cross axis. // Align along the cross axis.
let cross = aligns.cross.apply(if self.dirs.cross.is_positive() { let cross = aligns.cross.resolve(if self.dirs.cross.is_positive() {
Length::ZERO .. full_size.cross - child_size.cross Length::ZERO .. full_size.cross - child_size.cross
} else { } else {
full_size.cross - child_size.cross .. Length::ZERO full_size.cross - child_size.cross .. Length::ZERO

View File

@ -24,7 +24,7 @@ fn spacing(mut args: Args, ctx: &mut EvalContext, axis: SpecAxis) -> Value {
args.done(ctx); args.done(ctx);
if let Some(linear) = spacing { if let Some(linear) = spacing {
let amount = linear.eval(ctx.state.font.font_size()); let amount = linear.resolve(ctx.state.font.font_size());
let spacing = Spacing { amount, softness: Softness::Hard }; let spacing = Spacing { amount, softness: Softness::Hard };
if ctx.state.dirs.main.axis() == axis { if ctx.state.dirs.main.axis() == axis {
ctx.end_par_group(); ctx.end_par_group();