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::{Repr, Resolve, StyleChain, Value, cast, repr};
9use crate::layout::{Abs, Length};
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    /// Creates a font-relative length.
30    pub const fn new(em: f64) -> Self {
31        Self(Scalar::new(em))
32    }
33
34    /// Creates 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    /// Creates an em length from an absolute length at the given font size.
40    pub fn from_abs(length: Abs, font_size: Abs) -> Self {
41        let result = length / font_size;
42        if result.is_finite() { Self(Scalar::new(result)) } else { Self::zero() }
43    }
44
45    /// Creates an em length from a length at the given font size.
46    pub fn from_length(length: Length, font_size: Abs) -> Em {
47        length.em + Self::from_abs(length.abs, font_size)
48    }
49
50    /// The number of em units.
51    pub const fn get(self) -> f64 {
52        (self.0).get()
53    }
54
55    /// The absolute value of this em length.
56    pub fn abs(self) -> Self {
57        Self::new(self.get().abs())
58    }
59
60    /// Converts to an absolute length at the given font size.
61    pub fn at(self, font_size: Abs) -> Abs {
62        let resolved = font_size * self.get();
63        if resolved.is_finite() { resolved } else { Abs::zero() }
64    }
65}
66
67impl Numeric for Em {
68    fn zero() -> Self {
69        Self::zero()
70    }
71
72    fn is_finite(self) -> bool {
73        self.0.is_finite()
74    }
75}
76
77impl Debug for Em {
78    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
79        write!(f, "{:?}em", self.get())
80    }
81}
82
83impl Repr for Em {
84    fn repr(&self) -> EcoString {
85        repr::format_float_with_unit(self.get(), "em")
86    }
87}
88
89impl Neg for Em {
90    type Output = Self;
91
92    fn neg(self) -> Self {
93        Self(-self.0)
94    }
95}
96
97impl Add for Em {
98    type Output = Self;
99
100    fn add(self, other: Self) -> Self {
101        Self(self.0 + other.0)
102    }
103}
104
105typst_utils::sub_impl!(Em - Em -> Em);
106
107impl Mul<f64> for Em {
108    type Output = Self;
109
110    fn mul(self, other: f64) -> Self {
111        Self(self.0 * other)
112    }
113}
114
115impl Mul<Em> for f64 {
116    type Output = Em;
117
118    fn mul(self, other: Em) -> Em {
119        other * self
120    }
121}
122
123impl Div<f64> for Em {
124    type Output = Self;
125
126    fn div(self, other: f64) -> Self {
127        Self(self.0 / other)
128    }
129}
130
131impl Div for Em {
132    type Output = f64;
133
134    fn div(self, other: Self) -> f64 {
135        self.get() / other.get()
136    }
137}
138
139typst_utils::assign_impl!(Em += Em);
140typst_utils::assign_impl!(Em -= Em);
141typst_utils::assign_impl!(Em *= f64);
142typst_utils::assign_impl!(Em /= f64);
143
144impl Sum for Em {
145    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
146        Self(iter.map(|s| s.0).sum())
147    }
148}
149
150cast! {
151     Em,
152     self => Value::Length(self.into()),
153}
154
155impl Resolve for Em {
156    type Output = Abs;
157
158    fn resolve(self, styles: StyleChain) -> Self::Output {
159        if self.is_zero() { Abs::zero() } else { self.at(styles.resolve(TextElem::size)) }
160    }
161}