summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/angle.rs18
-rw-r--r--src/geom/em.rs12
-rw-r--r--src/geom/fraction.rs4
-rw-r--r--src/geom/macros.rs10
-rw-r--r--src/geom/mod.rs6
-rw-r--r--src/geom/point.rs4
-rw-r--r--src/geom/ratio.rs8
-rw-r--r--src/geom/relative.rs33
-rw-r--r--src/geom/sides.rs12
-rw-r--r--src/geom/transform.rs4
10 files changed, 63 insertions, 48 deletions
diff --git a/src/geom/angle.rs b/src/geom/angle.rs
index a0900ce5..888442f7 100644
--- a/src/geom/angle.rs
+++ b/src/geom/angle.rs
@@ -16,18 +16,18 @@ impl Angle {
}
/// Create an angle from a value in a unit.
- pub fn with_unit(val: f64, unit: AngularUnit) -> Self {
+ pub fn with_unit(val: f64, unit: AngleUnit) -> Self {
Self(Scalar(val * unit.raw_scale()))
}
/// Create an angle from a number of radians.
pub fn rad(rad: f64) -> Self {
- Self::with_unit(rad, AngularUnit::Rad)
+ Self::with_unit(rad, AngleUnit::Rad)
}
/// Create an angle from a number of degrees.
pub fn deg(deg: f64) -> Self {
- Self::with_unit(deg, AngularUnit::Deg)
+ Self::with_unit(deg, AngleUnit::Deg)
}
/// Get the value of this angle in raw units.
@@ -36,18 +36,18 @@ impl Angle {
}
/// Get the value of this length in unit.
- pub fn to_unit(self, unit: AngularUnit) -> f64 {
+ pub fn to_unit(self, unit: AngleUnit) -> f64 {
self.to_raw() / unit.raw_scale()
}
/// Convert this to a number of radians.
pub fn to_rad(self) -> f64 {
- self.to_unit(AngularUnit::Rad)
+ self.to_unit(AngleUnit::Rad)
}
/// Convert this to a number of degrees.
pub fn to_deg(self) -> f64 {
- self.to_unit(AngularUnit::Deg)
+ self.to_unit(AngleUnit::Deg)
}
/// The absolute value of the this angle.
@@ -145,14 +145,14 @@ impl Sum for Angle {
/// Different units of angular measurement.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
-pub enum AngularUnit {
+pub enum AngleUnit {
/// Radians.
Rad,
/// Degrees.
Deg,
}
-impl AngularUnit {
+impl AngleUnit {
/// How many raw units correspond to a value of `1.0` in this unit.
fn raw_scale(self) -> f64 {
match self {
@@ -162,7 +162,7 @@ impl AngularUnit {
}
}
-impl Debug for AngularUnit {
+impl Debug for AngleUnit {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self {
Self::Rad => "rad",
diff --git a/src/geom/em.rs b/src/geom/em.rs
index 3c772d96..ec1cfbda 100644
--- a/src/geom/em.rs
+++ b/src/geom/em.rs
@@ -29,7 +29,12 @@ impl Em {
/// Create an em length from a length at the given font size.
pub fn from_length(length: Length, font_size: Length) -> Self {
- Self(Scalar(length / font_size))
+ let result = length / font_size;
+ if result.is_finite() {
+ Self(Scalar(result))
+ } else {
+ Self::zero()
+ }
}
/// The number of em units.
@@ -38,8 +43,9 @@ impl Em {
}
/// Convert to a length at the given font size.
- pub fn resolve(self, font_size: Length) -> Length {
- self.get() * font_size
+ pub fn at(self, font_size: Length) -> Length {
+ let resolved = font_size * self.get();
+ if resolved.is_finite() { resolved } else { Length::zero() }
}
}
diff --git a/src/geom/fraction.rs b/src/geom/fraction.rs
index 2f33a134..f7188603 100644
--- a/src/geom/fraction.rs
+++ b/src/geom/fraction.rs
@@ -30,8 +30,8 @@ impl Fraction {
Self::new(self.get().abs())
}
- /// Resolve this fraction's share in the remaining space.
- pub fn resolve(self, total: Self, remaining: Length) -> Length {
+ /// Determine this fraction's share in the remaining space.
+ pub fn share(self, total: Self, remaining: Length) -> Length {
let ratio = self / total;
if ratio.is_finite() && remaining.is_finite() {
ratio * remaining
diff --git a/src/geom/macros.rs b/src/geom/macros.rs
index 615eb31c..b1b50e22 100644
--- a/src/geom/macros.rs
+++ b/src/geom/macros.rs
@@ -1,7 +1,7 @@
/// Implement the `Sub` trait based on existing `Neg` and `Add` impls.
macro_rules! sub_impl {
($a:ident - $b:ident -> $c:ident) => {
- impl Sub<$b> for $a {
+ impl std::ops::Sub<$b> for $a {
type Output = $c;
fn sub(self, other: $b) -> $c {
@@ -14,7 +14,7 @@ macro_rules! sub_impl {
/// Implement an assign trait based on an existing non-assign trait.
macro_rules! assign_impl {
($a:ident += $b:ident) => {
- impl AddAssign<$b> for $a {
+ impl std::ops::AddAssign<$b> for $a {
fn add_assign(&mut self, other: $b) {
*self = *self + other;
}
@@ -22,7 +22,7 @@ macro_rules! assign_impl {
};
($a:ident -= $b:ident) => {
- impl SubAssign<$b> for $a {
+ impl std::ops::SubAssign<$b> for $a {
fn sub_assign(&mut self, other: $b) {
*self = *self - other;
}
@@ -30,7 +30,7 @@ macro_rules! assign_impl {
};
($a:ident *= $b:ident) => {
- impl MulAssign<$b> for $a {
+ impl std::ops::MulAssign<$b> for $a {
fn mul_assign(&mut self, other: $b) {
*self = *self * other;
}
@@ -38,7 +38,7 @@ macro_rules! assign_impl {
};
($a:ident /= $b:ident) => {
- impl DivAssign<$b> for $a {
+ impl std::ops::DivAssign<$b> for $a {
fn div_assign(&mut self, other: $b) {
*self = *self / other;
}
diff --git a/src/geom/mod.rs b/src/geom/mod.rs
index bfb450a1..a6f53c87 100644
--- a/src/geom/mod.rs
+++ b/src/geom/mod.rs
@@ -72,15 +72,15 @@ pub trait Numeric:
+ Mul<f64, Output = Self>
+ Div<f64, Output = Self>
{
- /// The identity element.
+ /// The identity element for addition.
fn zero() -> Self;
- /// Whether `self` is the identity element.
+ /// Whether `self` is zero.
fn is_zero(self) -> bool {
self == Self::zero()
}
- /// Whether `self` contains only finite parts.
+ /// Whether `self` consists only of finite parts.
fn is_finite(self) -> bool;
}
diff --git a/src/geom/point.rs b/src/geom/point.rs
index afce68ba..dd89fbf5 100644
--- a/src/geom/point.rs
+++ b/src/geom/point.rs
@@ -38,8 +38,8 @@ impl Point {
/// Transform the point with the given transformation.
pub fn transform(self, ts: Transform) -> Self {
Self::new(
- ts.sx.resolve(self.x) + ts.kx.resolve(self.y) + ts.tx,
- ts.ky.resolve(self.x) + ts.sy.resolve(self.y) + ts.ty,
+ ts.sx.of(self.x) + ts.kx.of(self.y) + ts.tx,
+ ts.ky.of(self.x) + ts.sy.of(self.y) + ts.ty,
)
}
}
diff --git a/src/geom/ratio.rs b/src/geom/ratio.rs
index 7dca53c2..69f06dd2 100644
--- a/src/geom/ratio.rs
+++ b/src/geom/ratio.rs
@@ -3,7 +3,7 @@ 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).
+/// corresponding [literal](crate::syntax::ast::LitKind::Numeric).
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Ratio(Scalar);
@@ -38,13 +38,13 @@ impl Ratio {
self.0 == 1.0
}
- /// The absolute value of the this ratio.
+ /// The absolute value of this ratio.
pub fn abs(self) -> Self {
Self::new(self.get().abs())
}
- /// Resolve this relative to the given `whole`.
- pub fn resolve<T: Numeric>(self, whole: T) -> T {
+ /// Return the ratio of the given `whole`.
+ pub fn of<T: Numeric>(self, whole: T) -> T {
let resolved = whole * self.get();
if resolved.is_finite() { resolved } else { T::zero() }
}
diff --git a/src/geom/relative.rs b/src/geom/relative.rs
index 066b8c15..fc77fb9f 100644
--- a/src/geom/relative.rs
+++ b/src/geom/relative.rs
@@ -27,12 +27,17 @@ impl<T: Numeric> Relative<T> {
/// Whether both parts are zero.
pub fn is_zero(self) -> bool {
- self.rel.is_zero() && self.abs.is_zero()
+ self.rel.is_zero() && self.abs == T::zero()
}
- /// Resolve this relative to the given `whole`.
- pub fn resolve(self, whole: T) -> T {
- self.rel.resolve(whole) + self.abs
+ /// Whether the relative part is one and the absolute part is zero.
+ pub fn is_one(self) -> bool {
+ self.rel.is_one() && self.abs == T::zero()
+ }
+
+ /// Evaluate this relative to the given `whole`.
+ pub fn relative_to(self, whole: T) -> T {
+ self.rel.of(whole) + self.abs
}
/// Map the absolute part with `f`.
@@ -120,27 +125,31 @@ impl<T: Numeric> Div<f64> for Relative<T> {
}
}
-impl<T: Numeric> AddAssign for Relative<T> {
+impl<T: Numeric + AddAssign> AddAssign for Relative<T> {
fn add_assign(&mut self, other: Self) {
- *self = *self + other;
+ self.rel += other.rel;
+ self.abs += other.abs;
}
}
-impl<T: Numeric> SubAssign for Relative<T> {
+impl<T: Numeric + SubAssign> SubAssign for Relative<T> {
fn sub_assign(&mut self, other: Self) {
- *self = *self - other;
+ self.rel -= other.rel;
+ self.abs -= other.abs;
}
}
-impl<T: Numeric> MulAssign<f64> for Relative<T> {
+impl<T: Numeric + MulAssign<f64>> MulAssign<f64> for Relative<T> {
fn mul_assign(&mut self, other: f64) {
- *self = *self * other;
+ self.rel *= other;
+ self.abs *= other;
}
}
-impl<T: Numeric> DivAssign<f64> for Relative<T> {
+impl<T: Numeric + DivAssign<f64>> DivAssign<f64> for Relative<T> {
fn div_assign(&mut self, other: f64) {
- *self = *self * other;
+ self.rel /= other;
+ self.abs /= other;
}
}
diff --git a/src/geom/sides.rs b/src/geom/sides.rs
index 4539728f..3584a1ce 100644
--- a/src/geom/sides.rs
+++ b/src/geom/sides.rs
@@ -44,13 +44,13 @@ where
}
impl Sides<Relative<Length>> {
- /// Resolve the sides relative to the given `size`.
- pub fn resolve(self, size: Size) -> Sides<Length> {
+ /// Evaluate the sides relative to the given `size`.
+ pub fn relative_to(self, size: Size) -> Sides<Length> {
Sides {
- left: self.left.resolve(size.x),
- top: self.top.resolve(size.y),
- right: self.right.resolve(size.x),
- bottom: self.bottom.resolve(size.y),
+ left: self.left.relative_to(size.x),
+ top: self.top.relative_to(size.y),
+ right: self.right.relative_to(size.x),
+ bottom: self.bottom.relative_to(size.y),
}
}
}
diff --git a/src/geom/transform.rs b/src/geom/transform.rs
index c0a06e33..28a1af80 100644
--- a/src/geom/transform.rs
+++ b/src/geom/transform.rs
@@ -59,8 +59,8 @@ impl Transform {
ky: self.ky * prev.sx + self.sy * prev.ky,
kx: self.sx * prev.kx + self.kx * prev.sy,
sy: self.ky * prev.kx + self.sy * prev.sy,
- tx: self.sx.resolve(prev.tx) + self.kx.resolve(prev.ty) + self.tx,
- ty: self.ky.resolve(prev.tx) + self.sy.resolve(prev.ty) + self.ty,
+ tx: self.sx.of(prev.tx) + self.kx.of(prev.ty) + self.tx,
+ ty: self.ky.of(prev.tx) + self.sy.of(prev.ty) + self.ty,
}
}
}