Skip to main content

style/values/computed/
angle.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Computed angles.
6
7use crate::derives::*;
8use crate::typed_om::{NumericType, NumericValue, ToTyped, TypedValue, UnitValue};
9use crate::values::distance::{ComputeSquaredDistance, SquaredDistance};
10use crate::values::CSSFloat;
11use crate::Zero;
12use std::f64::consts::PI;
13use std::fmt::{self, Write};
14use std::ops::{AddAssign, Neg};
15use std::{f32, f64};
16use style_traits::{CssString, CssWriter, ToCss};
17use thin_vec::ThinVec;
18
19/// A computed angle in degrees.
20#[derive(
21    Add,
22    Animate,
23    Clone,
24    Copy,
25    Debug,
26    Deserialize,
27    MallocSizeOf,
28    PartialEq,
29    PartialOrd,
30    Serialize,
31    ToAnimatedZero,
32    ToResolvedValue,
33)]
34#[repr(C)]
35pub struct Angle(CSSFloat);
36
37impl ToCss for Angle {
38    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
39    where
40        W: Write,
41    {
42        self.degrees().to_css(dest)?;
43        dest.write_str("deg")
44    }
45}
46
47impl ToTyped for Angle {
48    fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
49        dest.push(TypedValue::Numeric(NumericValue::Unit(UnitValue {
50            numeric_type: NumericType::angle(),
51            value: self.degrees(),
52            unit: CssString::from("deg"),
53        })));
54        Ok(())
55    }
56}
57
58const RAD_PER_DEG: f64 = PI / 180.0;
59
60impl Angle {
61    /// Creates a computed `Angle` value from a radian amount.
62    pub fn from_radians(radians: CSSFloat) -> Self {
63        Angle(radians / RAD_PER_DEG as f32)
64    }
65
66    /// Creates a computed `Angle` value from a degrees amount.
67    #[inline]
68    pub fn from_degrees(degrees: CSSFloat) -> Self {
69        Angle(degrees)
70    }
71
72    /// Returns the amount of radians this angle represents.
73    #[inline]
74    pub fn radians(&self) -> CSSFloat {
75        self.radians64().min(f32::MAX as f64).max(f32::MIN as f64) as f32
76    }
77
78    /// Returns the amount of radians this angle represents as a `f64`.
79    ///
80    /// Gecko stores angles as singles, but does this computation using doubles.
81    ///
82    /// This is significant enough to mess up rounding to the nearest
83    /// quarter-turn for 225 degrees, for example.
84    #[inline]
85    pub fn radians64(&self) -> f64 {
86        self.0 as f64 * RAD_PER_DEG
87    }
88
89    /// Return the value in degrees.
90    #[inline]
91    pub fn degrees(&self) -> CSSFloat {
92        self.0
93    }
94}
95
96impl Zero for Angle {
97    #[inline]
98    fn zero() -> Self {
99        Angle(0.0)
100    }
101
102    #[inline]
103    fn is_zero(&self) -> bool {
104        self.0 == 0.
105    }
106}
107
108impl ComputeSquaredDistance for Angle {
109    #[inline]
110    fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
111        // Use the formula for calculating the distance between angles defined in SVG:
112        // https://www.w3.org/TR/SVG/animate.html#complexDistances
113        self.radians64()
114            .compute_squared_distance(&other.radians64())
115    }
116}
117
118impl Neg for Angle {
119    type Output = Angle;
120
121    #[inline]
122    fn neg(self) -> Angle {
123        Angle(-self.0)
124    }
125}
126
127impl AddAssign for Angle {
128    fn add_assign(&mut self, rhs: Self) {
129        self.0 += rhs.0
130    }
131}