summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-04-07 14:32:35 +0200
committerLaurenz <laurmaedje@gmail.com>2022-04-07 16:19:51 +0200
commit1192132dc0a9e991953fd29e93f87c8437a53ea0 (patch)
tree67061e75a5a15c7997a3ac2de349b5e9ce234434 /src/geom
parenteb22eed31b08874fbbbee68d2ae59f0d02f9e95d (diff)
Rename length-related types
`Fractional` => `Fraction` `Relative` => `Ratio` `Linear` => `Relative`
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/em.rs2
-rw-r--r--src/geom/fraction.rs (renamed from src/geom/fr.rs)46
-rw-r--r--src/geom/linear.rs190
-rw-r--r--src/geom/mod.rs8
-rw-r--r--src/geom/ratio.rs125
-rw-r--r--src/geom/relative.rs153
-rw-r--r--src/geom/sides.rs4
-rw-r--r--src/geom/transform.rs22
8 files changed, 267 insertions, 283 deletions
diff --git a/src/geom/em.rs b/src/geom/em.rs
index d1cf3288..d1e90e04 100644
--- a/src/geom/em.rs
+++ b/src/geom/em.rs
@@ -17,7 +17,7 @@ impl Em {
Self(Scalar(1.0))
}
- /// Create an font-relative length.
+ /// Create a font-relative length.
pub const fn new(em: f64) -> Self {
Self(Scalar(em))
}
diff --git a/src/geom/fr.rs b/src/geom/fraction.rs
index 6aa0f5c7..7da85779 100644
--- a/src/geom/fr.rs
+++ b/src/geom/fraction.rs
@@ -1,41 +1,41 @@
use super::*;
-/// A fractional length.
+/// A fraction of remaining space.
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
-pub struct Fractional(Scalar);
+pub struct Fraction(Scalar);
-impl Fractional {
+impl Fraction {
/// Takes up zero space: `0fr`.
pub const fn zero() -> Self {
Self(Scalar(0.0))
}
- /// Takes up as much space as all other items with this fractional size: `1fr`.
+ /// Takes up as much space as all other items with this fraction: `1fr`.
pub const fn one() -> Self {
Self(Scalar(1.0))
}
- /// Create a new fractional value.
+ /// Create a new fraction.
pub const fn new(ratio: f64) -> Self {
Self(Scalar(ratio))
}
- /// Get the underlying ratio.
+ /// Get the underlying number.
pub const fn get(self) -> f64 {
(self.0).0
}
- /// Whether the ratio is zero.
+ /// Whether the fraction is zero.
pub fn is_zero(self) -> bool {
self.0 == 0.0
}
- /// The absolute value of the this fractional.
+ /// The absolute value of the this fraction.
pub fn abs(self) -> Self {
Self::new(self.get().abs())
}
- /// Resolve this fractionals share in the remaining space.
+ /// Resolve this fraction's share in the remaining space.
pub fn resolve(self, total: Self, remaining: Length) -> Length {
let ratio = self / total;
if ratio.is_finite() && remaining.is_finite() {
@@ -46,13 +46,13 @@ impl Fractional {
}
}
-impl Debug for Fractional {
+impl Debug for Fraction {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}fr", round_2(self.get()))
}
}
-impl Neg for Fractional {
+impl Neg for Fraction {
type Output = Self;
fn neg(self) -> Self {
@@ -60,7 +60,7 @@ impl Neg for Fractional {
}
}
-impl Add for Fractional {
+impl Add for Fraction {
type Output = Self;
fn add(self, other: Self) -> Self {
@@ -68,9 +68,9 @@ impl Add for Fractional {
}
}
-sub_impl!(Fractional - Fractional -> Fractional);
+sub_impl!(Fraction - Fraction -> Fraction);
-impl Mul<f64> for Fractional {
+impl Mul<f64> for Fraction {
type Output = Self;
fn mul(self, other: f64) -> Self {
@@ -78,15 +78,15 @@ impl Mul<f64> for Fractional {
}
}
-impl Mul<Fractional> for f64 {
- type Output = Fractional;
+impl Mul<Fraction> for f64 {
+ type Output = Fraction;
- fn mul(self, other: Fractional) -> Fractional {
+ fn mul(self, other: Fraction) -> Fraction {
other * self
}
}
-impl Div<f64> for Fractional {
+impl Div<f64> for Fraction {
type Output = Self;
fn div(self, other: f64) -> Self {
@@ -94,7 +94,7 @@ impl Div<f64> for Fractional {
}
}
-impl Div for Fractional {
+impl Div for Fraction {
type Output = f64;
fn div(self, other: Self) -> f64 {
@@ -102,7 +102,7 @@ impl Div for Fractional {
}
}
-assign_impl!(Fractional += Fractional);
-assign_impl!(Fractional -= Fractional);
-assign_impl!(Fractional *= f64);
-assign_impl!(Fractional /= f64);
+assign_impl!(Fraction += Fraction);
+assign_impl!(Fraction -= Fraction);
+assign_impl!(Fraction *= f64);
+assign_impl!(Fraction /= f64);
diff --git a/src/geom/linear.rs b/src/geom/linear.rs
deleted file mode 100644
index 78602d8b..00000000
--- a/src/geom/linear.rs
+++ /dev/null
@@ -1,190 +0,0 @@
-use super::*;
-
-/// A combined relative and absolute length.
-#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
-pub struct Linear {
- /// The relative part.
- pub rel: Relative,
- /// The absolute part.
- pub abs: Length,
-}
-
-impl Linear {
- /// The zero linear.
- pub const fn zero() -> Self {
- Self {
- rel: Relative::zero(),
- abs: Length::zero(),
- }
- }
-
- /// The linear with a relative part of `100%` and no absolute part.
- pub const fn one() -> Self {
- Self {
- rel: Relative::one(),
- abs: Length::zero(),
- }
- }
-
- /// Create a new linear.
- pub const fn new(rel: Relative, abs: Length) -> Self {
- Self { rel, abs }
- }
-
- /// Resolve this linear's relative component to the given `length`.
- pub fn resolve(self, length: Length) -> Length {
- self.rel.resolve(length) + self.abs
- }
-
- /// Compose with another linear.
- ///
- /// The resulting linear is (self ∘ inner)(x) = self(inner(x)).
- pub fn compose(self, inner: Self) -> Self {
- Self {
- rel: self.rel * inner.rel,
- abs: self.rel.resolve(inner.abs) + self.abs,
- }
- }
-
- /// Whether both parts are zero.
- pub fn is_zero(self) -> bool {
- self.rel.is_zero() && self.abs.is_zero()
- }
-
- /// Whether there is a linear component.
- pub fn is_relative(self) -> bool {
- !self.rel.is_zero()
- }
-}
-
-impl Debug for Linear {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "{:?} + {:?}", self.rel, self.abs)
- }
-}
-
-impl From<Length> for Linear {
- fn from(abs: Length) -> Self {
- Self { rel: Relative::zero(), abs }
- }
-}
-
-impl From<Relative> for Linear {
- fn from(rel: Relative) -> Self {
- Self { rel, abs: Length::zero() }
- }
-}
-
-impl Neg for Linear {
- type Output = Self;
-
- fn neg(self) -> Self {
- Self { rel: -self.rel, abs: -self.abs }
- }
-}
-
-impl Add for Linear {
- type Output = Self;
-
- fn add(self, other: Self) -> Self {
- Self {
- rel: self.rel + other.rel,
- abs: self.abs + other.abs,
- }
- }
-}
-
-impl Add<Relative> for Length {
- type Output = Linear;
-
- fn add(self, other: Relative) -> Linear {
- Linear { rel: other, abs: self }
- }
-}
-
-impl Add<Length> for Relative {
- type Output = Linear;
-
- fn add(self, other: Length) -> Linear {
- other + self
- }
-}
-
-impl Add<Length> for Linear {
- type Output = Self;
-
- fn add(self, other: Length) -> Self {
- Self { rel: self.rel, abs: self.abs + other }
- }
-}
-
-impl Add<Linear> for Length {
- type Output = Linear;
-
- fn add(self, other: Linear) -> Linear {
- other + self
- }
-}
-
-impl Add<Relative> for Linear {
- type Output = Self;
-
- fn add(self, other: Relative) -> Self {
- Self { rel: self.rel + other, abs: self.abs }
- }
-}
-
-impl Add<Linear> for Relative {
- type Output = Linear;
-
- fn add(self, other: Linear) -> Linear {
- other + self
- }
-}
-
-sub_impl!(Linear - Linear -> Linear);
-sub_impl!(Length - Relative -> Linear);
-sub_impl!(Relative - Length -> Linear);
-sub_impl!(Linear - Length -> Linear);
-sub_impl!(Length - Linear -> Linear);
-sub_impl!(Linear - Relative -> Linear);
-sub_impl!(Relative - Linear -> Linear);
-
-impl Mul<f64> for Linear {
- type Output = Self;
-
- fn mul(self, other: f64) -> Self {
- Self {
- rel: self.rel * other,
- abs: self.abs * other,
- }
- }
-}
-
-impl Mul<Linear> for f64 {
- type Output = Linear;
-
- fn mul(self, other: Linear) -> Linear {
- other * self
- }
-}
-
-impl Div<f64> for Linear {
- type Output = Self;
-
- fn div(self, other: f64) -> Self {
- Self {
- rel: self.rel / other,
- abs: self.abs / other,
- }
- }
-}
-
-assign_impl!(Linear += Linear);
-assign_impl!(Linear += Length);
-assign_impl!(Linear += Relative);
-assign_impl!(Linear -= Linear);
-assign_impl!(Linear -= Length);
-assign_impl!(Linear -= Relative);
-assign_impl!(Linear *= f64);
-assign_impl!(Linear /= f64);
diff --git a/src/geom/mod.rs b/src/geom/mod.rs
index 1a48534c..8d7759b4 100644
--- a/src/geom/mod.rs
+++ b/src/geom/mod.rs
@@ -6,13 +6,13 @@ mod align;
mod angle;
mod dir;
mod em;
-mod fr;
+mod fraction;
mod gen;
mod length;
-mod linear;
mod paint;
mod path;
mod point;
+mod ratio;
mod relative;
mod scalar;
mod sides;
@@ -23,13 +23,13 @@ pub use align::*;
pub use angle::*;
pub use dir::*;
pub use em::*;
-pub use fr::*;
+pub use fraction::*;
pub use gen::*;
pub use length::*;
-pub use linear::*;
pub use paint::*;
pub use path::*;
pub use point::*;
+pub use ratio::*;
pub use relative::*;
pub use scalar::*;
pub use sides::*;
diff --git a/src/geom/ratio.rs b/src/geom/ratio.rs
new file mode 100644
index 00000000..d035c33e
--- /dev/null
+++ b/src/geom/ratio.rs
@@ -0,0 +1,125 @@
+use super::*;
+
+/// A ratio of a whole.
+///
+/// _Note_: `50%` is represented as `0.5` here, but stored as `50.0` in the
+/// corresponding [literal](crate::syntax::ast::LitKind::Percent).
+#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
+pub struct Ratio(Scalar);
+
+impl Ratio {
+ /// A ratio of `0%` represented as `0.0`.
+ pub const fn zero() -> Self {
+ Self(Scalar(0.0))
+ }
+
+ /// A ratio of `100%` represented as `1.0`.
+ pub const fn one() -> Self {
+ Self(Scalar(1.0))
+ }
+
+ /// Create a new ratio from a value, whre `1.0` means `100%`.
+ pub const fn new(ratio: f64) -> Self {
+ Self(Scalar(ratio))
+ }
+
+ /// Get the underlying ratio.
+ pub const fn get(self) -> f64 {
+ (self.0).0
+ }
+
+ /// Resolve this relative to the given `length`.
+ pub fn resolve(self, length: Length) -> Length {
+ // We don't want NaNs.
+ if length.is_infinite() {
+ Length::zero()
+ } else {
+ self.get() * length
+ }
+ }
+
+ /// Whether the ratio is zero.
+ pub fn is_zero(self) -> bool {
+ self.0 == 0.0
+ }
+
+ /// Whether the ratio is one.
+ pub fn is_one(self) -> bool {
+ self.0 == 1.0
+ }
+
+ /// The absolute value of the this ratio.
+ pub fn abs(self) -> Self {
+ Self::new(self.get().abs())
+ }
+}
+
+impl Debug for Ratio {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(f, "{}%", round_2(100.0 * self.get()))
+ }
+}
+
+impl Neg for Ratio {
+ type Output = Self;
+
+ fn neg(self) -> Self {
+ Self(-self.0)
+ }
+}
+
+impl Add for Ratio {
+ type Output = Self;
+
+ fn add(self, other: Self) -> Self {
+ Self(self.0 + other.0)
+ }
+}
+
+sub_impl!(Ratio - Ratio -> Ratio);
+
+impl Mul for Ratio {
+ type Output = Self;
+
+ fn mul(self, other: Self) -> Self {
+ Self(self.0 * other.0)
+ }
+}
+
+impl Mul<f64> for Ratio {
+ type Output = Self;
+
+ fn mul(self, other: f64) -> Self {
+ Self(self.0 * other)
+ }
+}
+
+impl Mul<Ratio> for f64 {
+ type Output = Ratio;
+
+ fn mul(self, other: Ratio) -> Ratio {
+ other * self
+ }
+}
+
+impl Div<f64> for Ratio {
+ type Output = Self;
+
+ fn div(self, other: f64) -> Self {
+ Self(self.0 / other)
+ }
+}
+
+impl Div for Ratio {
+ type Output = f64;
+
+ fn div(self, other: Self) -> f64 {
+ self.get() / other.get()
+ }
+}
+
+assign_impl!(Ratio += Ratio);
+assign_impl!(Ratio -= Ratio);
+assign_impl!(Ratio *= Ratio);
+assign_impl!(Ratio *= f64);
+assign_impl!(Ratio /= f64);
diff --git a/src/geom/relative.rs b/src/geom/relative.rs
index 885de34a..fa2ec7fc 100644
--- a/src/geom/relative.rs
+++ b/src/geom/relative.rs
@@ -1,62 +1,61 @@
use super::*;
/// A relative length.
-///
-/// _Note_: `50%` is represented as `0.5` here, but stored as `50.0` in the
-/// corresponding [literal](crate::syntax::ast::LitKind::Percent).
-#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
-pub struct Relative(Scalar);
+#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
+pub struct Relative {
+ /// The relative part.
+ pub rel: Ratio,
+ /// The absolute part.
+ pub abs: Length,
+}
impl Relative {
- /// A ratio of `0%` represented as `0.0`.
+ /// The zero relative length.
pub const fn zero() -> Self {
- Self(Scalar(0.0))
+ Self { rel: Ratio::zero(), abs: Length::zero() }
}
- /// A ratio of `100%` represented as `1.0`.
+ /// A relative length with a ratio of `100%` and no absolute part.
pub const fn one() -> Self {
- Self(Scalar(1.0))
- }
-
- /// Create a new relative value.
- pub const fn new(ratio: f64) -> Self {
- Self(Scalar(ratio))
+ Self { rel: Ratio::one(), abs: Length::zero() }
}
- /// Get the underlying ratio.
- pub const fn get(self) -> f64 {
- (self.0).0
+ /// Create a new relative length from its parts.
+ pub const fn new(rel: Ratio, abs: Length) -> Self {
+ Self { rel, abs }
}
- /// Resolve this relative to the given `length`.
+ /// Resolve this length relative to the given `length`.
pub fn resolve(self, length: Length) -> Length {
- // We don't want NaNs.
- if length.is_infinite() {
- Length::zero()
- } else {
- self.get() * length
- }
+ self.rel.resolve(length) + self.abs
}
- /// Whether the ratio is zero.
+ /// Whether both parts are zero.
pub fn is_zero(self) -> bool {
- self.0 == 0.0
- }
-
- /// Whether the ratio is one.
- pub fn is_one(self) -> bool {
- self.0 == 1.0
+ self.rel.is_zero() && self.abs.is_zero()
}
- /// The absolute value of the this relative.
- pub fn abs(self) -> Self {
- Self::new(self.get().abs())
+ /// Whether there is a relative part.
+ pub fn is_relative(self) -> bool {
+ !self.rel.is_zero()
}
}
impl Debug for Relative {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "{}%", round_2(100.0 * self.get()))
+ write!(f, "{:?} + {:?}", self.rel, self.abs)
+ }
+}
+
+impl From<Length> for Relative {
+ fn from(abs: Length) -> Self {
+ Self { rel: Ratio::zero(), abs }
+ }
+}
+
+impl From<Ratio> for Relative {
+ fn from(rel: Ratio) -> Self {
+ Self { rel, abs: Length::zero() }
}
}
@@ -64,7 +63,7 @@ impl Neg for Relative {
type Output = Self;
fn neg(self) -> Self {
- Self(-self.0)
+ Self { rel: -self.rel, abs: -self.abs }
}
}
@@ -72,25 +71,77 @@ impl Add for Relative {
type Output = Self;
fn add(self, other: Self) -> Self {
- Self(self.0 + other.0)
+ Self {
+ rel: self.rel + other.rel,
+ abs: self.abs + other.abs,
+ }
}
}
-sub_impl!(Relative - Relative -> Relative);
+impl Add<Ratio> for Length {
+ type Output = Relative;
+
+ fn add(self, other: Ratio) -> Relative {
+ Relative { rel: other, abs: self }
+ }
+}
+
+impl Add<Length> for Ratio {
+ type Output = Relative;
+
+ fn add(self, other: Length) -> Relative {
+ other + self
+ }
+}
-impl Mul for Relative {
+impl Add<Length> for Relative {
type Output = Self;
- fn mul(self, other: Self) -> Self {
- Self(self.0 * other.0)
+ fn add(self, other: Length) -> Self {
+ Self { rel: self.rel, abs: self.abs + other }
}
}
+impl Add<Relative> for Length {
+ type Output = Relative;
+
+ fn add(self, other: Relative) -> Relative {
+ other + self
+ }
+}
+
+impl Add<Ratio> for Relative {
+ type Output = Self;
+
+ fn add(self, other: Ratio) -> Self {
+ Self { rel: self.rel + other, abs: self.abs }
+ }
+}
+
+impl Add<Relative> for Ratio {
+ type Output = Relative;
+
+ fn add(self, other: Relative) -> Relative {
+ other + self
+ }
+}
+
+sub_impl!(Relative - Relative -> Relative);
+sub_impl!(Length - Ratio -> Relative);
+sub_impl!(Ratio - Length -> Relative);
+sub_impl!(Relative - Length -> Relative);
+sub_impl!(Length - Relative -> Relative);
+sub_impl!(Relative - Ratio -> Relative);
+sub_impl!(Ratio - Relative -> Relative);
+
impl Mul<f64> for Relative {
type Output = Self;
fn mul(self, other: f64) -> Self {
- Self(self.0 * other)
+ Self {
+ rel: self.rel * other,
+ abs: self.abs * other,
+ }
}
}
@@ -106,20 +157,18 @@ impl Div<f64> for Relative {
type Output = Self;
fn div(self, other: f64) -> Self {
- Self(self.0 / other)
- }
-}
-
-impl Div for Relative {
- type Output = f64;
-
- fn div(self, other: Self) -> f64 {
- self.get() / other.get()
+ Self {
+ rel: self.rel / other,
+ abs: self.abs / other,
+ }
}
}
assign_impl!(Relative += Relative);
+assign_impl!(Relative += Length);
+assign_impl!(Relative += Ratio);
assign_impl!(Relative -= Relative);
-assign_impl!(Relative *= Relative);
+assign_impl!(Relative -= Length);
+assign_impl!(Relative -= Ratio);
assign_impl!(Relative *= f64);
assign_impl!(Relative /= f64);
diff --git a/src/geom/sides.rs b/src/geom/sides.rs
index c0929fc7..45420c18 100644
--- a/src/geom/sides.rs
+++ b/src/geom/sides.rs
@@ -43,8 +43,8 @@ where
}
}
-impl Sides<Linear> {
- /// Resolve the linear sides relative to the given `size`.
+impl Sides<Relative> {
+ /// Resolve the sides relative to the given `size`.
pub fn resolve(self, size: Size) -> Sides<Length> {
Sides {
left: self.left.resolve(size.x),
diff --git a/src/geom/transform.rs b/src/geom/transform.rs
index 5bc05d84..8d64ebcf 100644
--- a/src/geom/transform.rs
+++ b/src/geom/transform.rs
@@ -3,10 +3,10 @@ use super::*;
/// A scale-skew-translate transformation.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Transform {
- pub sx: Relative,
- pub ky: Relative,
- pub kx: Relative,
- pub sy: Relative,
+ pub sx: Ratio,
+ pub ky: Ratio,
+ pub kx: Ratio,
+ pub sy: Ratio,
pub tx: Length,
pub ty: Length,
}
@@ -15,10 +15,10 @@ impl Transform {
/// The identity transformation.
pub const fn identity() -> Self {
Self {
- sx: Relative::one(),
- ky: Relative::zero(),
- kx: Relative::zero(),
- sy: Relative::one(),
+ sx: Ratio::one(),
+ ky: Ratio::zero(),
+ kx: Ratio::zero(),
+ sy: Ratio::one(),
tx: Length::zero(),
ty: Length::zero(),
}
@@ -30,14 +30,14 @@ impl Transform {
}
/// A scaling transform.
- pub const fn scale(sx: Relative, sy: Relative) -> Self {
+ pub const fn scale(sx: Ratio, sy: Ratio) -> Self {
Self { sx, sy, ..Self::identity() }
}
/// A rotation transform.
pub fn rotation(angle: Angle) -> Self {
- let cos = Relative::new(angle.cos());
- let sin = Relative::new(angle.sin());
+ let cos = Ratio::new(angle.cos());
+ let sin = Ratio::new(angle.sin());
Self {
sx: cos,
ky: sin,