typst_library/layout/
em.rs

1use std::fmt::{self, Debug, Formatter};
2use std::iter::Sum;
3use std::ops::{Add, Div, Mul, Neg};
4
5use ecow::EcoString;
6use typst_utils::{Numeric, Scalar};
7
8use crate::foundations::{cast, repr, Repr, Resolve, StyleChain, Value};
9use crate::layout::Abs;
10use crate::text::TextElem;
11
12/// A length that is relative to the font size.
13///
14/// `1em` is the same as the font size.
15#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
16pub struct Em(Scalar);
17
18impl Em {
19    /// The zero em length.
20    pub const fn zero() -> Self {
21        Self(Scalar::ZERO)
22    }
23
24    /// The font size.
25    pub const fn one() -> Self {
26        Self(Scalar::ONE)
27    }
28
29    /// Create a font-relative length.
30    pub const fn new(em: f64) -> Self {
31        Self(Scalar::new(em))
32    }
33
34    /// Create an em length from font units at the given units per em.
35    pub fn from_units(units: impl Into<f64>, units_per_em: f64) -> Self {
36        Self(Scalar::new(units.into() / units_per_em))
37    }
38
39    /// Create an em length from a length at the given font size.
40    pub fn from_length(length: Abs, font_size: Abs) -> Self {
41        let result = length / font_size;
42        if result.is_finite() {
43            Self(Scalar::new(result))
44        } else {
45            Self::zero()
46        }
47    }
48
49    /// The number of em units.
50    pub const fn get(self) -> f64 {
51        (self.0).get()
52    }
53
54    /// The absolute value of this em length.
55    pub fn abs(self) -> Self {
56        Self::new(self.get().abs())
57    }
58
59    /// Convert to an absolute length at the given font size.
60    pub fn at(self, font_size: Abs) -> Abs {
61        let resolved = font_size * self.get();
62        if resolved.is_finite() {
63            resolved
64        } else {
65            Abs::zero()
66        }
67    }
68}
69
70impl Numeric for Em {
71    fn zero() -> Self {
72        Self::zero()
73    }
74
75    fn is_finite(self) -> bool {
76        self.0.is_finite()
77    }
78}
79
80impl Debug for Em {
81    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
82        write!(f, "{:?}em", self.get())
83    }
84}
85
86impl Repr for Em {
87    fn repr(&self) -> EcoString {
88        repr::format_float_with_unit(self.get(), "em")
89    }
90}
91
92impl Neg for Em {
93    type Output = Self;
94
95    fn neg(self) -> Self {
96        Self(-self.0)
97    }
98}
99
100impl Add for Em {
101    type Output = Self;
102
103    fn add(self, other: Self) -> Self {
104        Self(self.0 + other.0)
105    }
106}
107
108typst_utils::sub_impl!(Em - Em -> Em);
109
110impl Mul<f64> for Em {
111    type Output = Self;
112
113    fn mul(self, other: f64) -> Self {
114        Self(self.0 * other)
115    }
116}
117
118impl Mul<Em> for f64 {
119    type Output = Em;
120
121    fn mul(self, other: Em) -> Em {
122        other * self
123    }
124}
125
126impl Div<f64> for Em {
127    type Output = Self;
128
129    fn div(self, other: f64) -> Self {
130        Self(self.0 / other)
131    }
132}
133
134impl Div for Em {
135    type Output = f64;
136
137    fn div(self, other: Self) -> f64 {
138        self.get() / other.get()
139    }
140}
141
142typst_utils::assign_impl!(Em += Em);
143typst_utils::assign_impl!(Em -= Em);
144typst_utils::assign_impl!(Em *= f64);
145typst_utils::assign_impl!(Em /= f64);
146
147impl Sum for Em {
148    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
149        Self(iter.map(|s| s.0).sum())
150    }
151}
152
153cast! {
154     Em,
155     self => Value::Length(self.into()),
156}
157
158impl Resolve for Em {
159    type Output = Abs;
160
161    fn resolve(self, styles: StyleChain) -> Self::Output {
162        if self.is_zero() {
163            Abs::zero()
164        } else {
165            self.at(TextElem::size_in(styles))
166        }
167    }
168}