typst_library/layout/
abs.rs1use std::fmt::{self, Debug, Formatter};
2use std::iter::Sum;
3use std::ops::{Add, Div, Mul, Neg, Rem};
4
5use ecow::EcoString;
6use typst_utils::{Numeric, NumericLength, Scalar};
7
8use crate::foundations::{Fold, Repr, Value, cast, repr};
9
10#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
12pub struct Abs(Scalar);
13
14impl Abs {
15 pub const fn zero() -> Self {
17 Self(Scalar::ZERO)
18 }
19
20 pub const fn inf() -> Self {
22 Self(Scalar::INFINITY)
23 }
24
25 pub const fn raw(raw: f64) -> Self {
27 Self(Scalar::new(raw))
28 }
29
30 pub fn with_unit(val: f64, unit: AbsUnit) -> Self {
32 Self(Scalar::new(val * unit.raw_scale()))
33 }
34
35 pub fn pt(pt: f64) -> Self {
37 Self::with_unit(pt, AbsUnit::Pt)
38 }
39
40 pub fn mm(mm: f64) -> Self {
42 Self::with_unit(mm, AbsUnit::Mm)
43 }
44
45 pub fn cm(cm: f64) -> Self {
47 Self::with_unit(cm, AbsUnit::Cm)
48 }
49
50 pub fn inches(inches: f64) -> Self {
52 Self::with_unit(inches, AbsUnit::In)
53 }
54
55 pub const fn to_raw(self) -> f64 {
57 self.0.get()
58 }
59
60 pub const fn scalar(self) -> Scalar {
62 self.0
63 }
64
65 pub fn to_unit(self, unit: AbsUnit) -> f64 {
67 self.to_raw() / unit.raw_scale()
68 }
69
70 pub fn to_pt(self) -> f64 {
72 self.to_unit(AbsUnit::Pt)
73 }
74
75 pub fn to_mm(self) -> f64 {
77 self.to_unit(AbsUnit::Mm)
78 }
79
80 pub fn to_cm(self) -> f64 {
82 self.to_unit(AbsUnit::Cm)
83 }
84
85 pub fn to_inches(self) -> f64 {
87 self.to_unit(AbsUnit::In)
88 }
89
90 pub fn abs(self) -> Self {
92 Self::raw(self.to_raw().abs())
93 }
94
95 pub fn min(self, other: Self) -> Self {
97 Self(self.0.min(other.0))
98 }
99
100 pub fn set_min(&mut self, other: Self) {
102 *self = (*self).min(other);
103 }
104
105 pub fn max(self, other: Self) -> Self {
107 Self(self.0.max(other.0))
108 }
109
110 pub fn set_max(&mut self, other: Self) {
112 *self = (*self).max(other);
113 }
114
115 pub fn fits(self, other: Self) -> bool {
118 self.0 + AbsUnit::EPS >= other.0
119 }
120
121 pub fn approx_eq(self, other: Self) -> bool {
123 self == other || (self - other).to_raw().abs() < AbsUnit::EPS
124 }
125
126 pub fn approx_empty(self) -> bool {
128 self.to_raw() <= AbsUnit::EPS
129 }
130
131 pub fn signum(self) -> f64 {
133 self.0.get().signum()
134 }
135}
136
137impl NumericLength for Abs {}
138
139impl Numeric for Abs {
140 fn zero() -> Self {
141 Self::zero()
142 }
143
144 fn is_finite(self) -> bool {
145 self.0.is_finite()
146 }
147}
148
149impl Debug for Abs {
150 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
151 write!(f, "{:?}pt", self.to_pt())
152 }
153}
154
155impl Repr for Abs {
156 fn repr(&self) -> EcoString {
157 repr::format_float_with_unit(self.to_pt(), "pt")
158 }
159}
160
161impl Neg for Abs {
162 type Output = Self;
163
164 fn neg(self) -> Self {
165 Self(-self.0)
166 }
167}
168
169impl Add for Abs {
170 type Output = Self;
171
172 fn add(self, other: Self) -> Self {
173 Self(self.0 + other.0)
174 }
175}
176
177typst_utils::sub_impl!(Abs - Abs -> Abs);
178
179impl Mul<f64> for Abs {
180 type Output = Self;
181
182 fn mul(self, other: f64) -> Self {
183 Self(self.0 * other)
184 }
185}
186
187impl Mul<Abs> for f64 {
188 type Output = Abs;
189
190 fn mul(self, other: Abs) -> Abs {
191 other * self
192 }
193}
194
195impl Div<f64> for Abs {
196 type Output = Self;
197
198 fn div(self, other: f64) -> Self {
199 Self(self.0 / other)
200 }
201}
202
203impl Div for Abs {
204 type Output = f64;
205
206 fn div(self, other: Self) -> f64 {
207 self.to_raw() / other.to_raw()
208 }
209}
210
211typst_utils::assign_impl!(Abs += Abs);
212typst_utils::assign_impl!(Abs -= Abs);
213typst_utils::assign_impl!(Abs *= f64);
214typst_utils::assign_impl!(Abs /= f64);
215
216impl Rem for Abs {
217 type Output = Self;
218
219 fn rem(self, other: Self) -> Self::Output {
220 Self(self.0 % other.0)
221 }
222}
223
224impl Sum for Abs {
225 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
226 Self(iter.map(|s| s.0).sum())
227 }
228}
229
230impl<'a> Sum<&'a Self> for Abs {
231 fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
232 Self(iter.map(|s| s.0).sum())
233 }
234}
235
236impl Fold for Abs {
237 fn fold(self, _: Self) -> Self {
238 self
239 }
240}
241
242cast! {
243 Abs,
244 self => Value::Length(self.into()),
245}
246
247#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
249pub enum AbsUnit {
250 Pt,
252 Mm,
254 Cm,
256 In,
258}
259
260impl AbsUnit {
261 const EPS: f64 = 1e-4;
263
264 const fn raw_scale(self) -> f64 {
266 match self {
270 AbsUnit::Pt => 127.0,
271 AbsUnit::Mm => 360.0,
272 AbsUnit::Cm => 3600.0,
273 AbsUnit::In => 9144.0,
274 }
275 }
276}
277
278#[cfg(test)]
279mod tests {
280 use super::*;
281
282 #[test]
283 fn test_length_unit_conversion() {
284 assert!((Abs::mm(150.0).to_cm() - 15.0) < 1e-4);
285 }
286}