summaryrefslogtreecommitdiff
path: root/src/geom/em.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-07-02 19:59:52 +0200
committerLaurenz <laurmaedje@gmail.com>2023-07-02 20:07:43 +0200
commitebfdb1dafa430786db10dad2ef7d5467c1bdbed1 (patch)
tree2bbc24ddb4124c4bb14dec0e536129d4de37b056 /src/geom/em.rs
parent3ab19185093d7709f824b95b979060ce125389d8 (diff)
Move everything into `crates/` directory
Diffstat (limited to 'src/geom/em.rs')
-rw-r--r--src/geom/em.rs153
1 files changed, 0 insertions, 153 deletions
diff --git a/src/geom/em.rs b/src/geom/em.rs
deleted file mode 100644
index 8dda9ff6..00000000
--- a/src/geom/em.rs
+++ /dev/null
@@ -1,153 +0,0 @@
-use super::*;
-
-/// A length that is relative to the font size.
-///
-/// `1em` is the same as the font size.
-#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
-pub struct Em(Scalar);
-
-impl Em {
- /// The zero em length.
- pub const fn zero() -> Self {
- Self(Scalar(0.0))
- }
-
- /// The font size.
- pub const fn one() -> Self {
- Self(Scalar(1.0))
- }
-
- /// Create a font-relative length.
- pub const fn new(em: f64) -> Self {
- Self(Scalar(em))
- }
-
- /// Create an em length from font units at the given units per em.
- pub fn from_units(units: impl Into<f64>, units_per_em: f64) -> Self {
- Self(Scalar(units.into() / units_per_em))
- }
-
- /// Create an em length from a length at the given font size.
- pub fn from_length(length: Abs, font_size: Abs) -> Self {
- let result = length / font_size;
- if result.is_finite() {
- Self(Scalar(result))
- } else {
- Self::zero()
- }
- }
-
- /// The number of em units.
- pub const fn get(self) -> f64 {
- (self.0).0
- }
-
- /// The absolute value of this em length.
- pub fn abs(self) -> Self {
- Self::new(self.get().abs())
- }
-
- /// Convert to an absolute length at the given font size.
- pub fn at(self, font_size: Abs) -> Abs {
- let resolved = font_size * self.get();
- if resolved.is_finite() {
- resolved
- } else {
- Abs::zero()
- }
- }
-}
-
-impl Numeric for Em {
- fn zero() -> Self {
- Self::zero()
- }
-
- fn is_finite(self) -> bool {
- self.0.is_finite()
- }
-}
-
-impl Debug for Em {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "{}em", self.get())
- }
-}
-
-impl Neg for Em {
- type Output = Self;
-
- fn neg(self) -> Self {
- Self(-self.0)
- }
-}
-
-impl Add for Em {
- type Output = Self;
-
- fn add(self, other: Self) -> Self {
- Self(self.0 + other.0)
- }
-}
-
-sub_impl!(Em - Em -> Em);
-
-impl Mul<f64> for Em {
- type Output = Self;
-
- fn mul(self, other: f64) -> Self {
- Self(self.0 * other)
- }
-}
-
-impl Mul<Em> for f64 {
- type Output = Em;
-
- fn mul(self, other: Em) -> Em {
- other * self
- }
-}
-
-impl Div<f64> for Em {
- type Output = Self;
-
- fn div(self, other: f64) -> Self {
- Self(self.0 / other)
- }
-}
-
-impl Div for Em {
- type Output = f64;
-
- fn div(self, other: Self) -> f64 {
- self.get() / other.get()
- }
-}
-
-assign_impl!(Em += Em);
-assign_impl!(Em -= Em);
-assign_impl!(Em *= f64);
-assign_impl!(Em /= f64);
-
-impl Sum for Em {
- fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
- Self(iter.map(|s| s.0).sum())
- }
-}
-
-cast! {
- Em,
- self => Value::Length(self.into()),
-}
-
-impl Resolve for Em {
- type Output = Abs;
-
- fn resolve(self, styles: StyleChain) -> Self::Output {
- if self.is_zero() {
- Abs::zero()
- } else {
- self.at(item!(em)(styles))
- }
- }
-}