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::values::distance::{ComputeSquaredDistance, SquaredDistance};
8use crate::values::CSSFloat;
9use crate::Zero;
10use std::f64::consts::PI;
11use std::fmt::{self, Write};
12use std::{f32, f64};
13use style_traits::{CssWriter, ToCss};
14
15/// A computed angle in degrees.
16#[derive(
17    Add,
18    Animate,
19    Clone,
20    Copy,
21    Debug,
22    Deserialize,
23    MallocSizeOf,
24    PartialEq,
25    PartialOrd,
26    Serialize,
27    ToAnimatedZero,
28    ToResolvedValue,
29)]
30#[repr(C)]
31pub struct Angle(CSSFloat);
32
33impl ToCss for Angle {
34    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
35    where
36        W: Write,
37    {
38        self.degrees().to_css(dest)?;
39        dest.write_str("deg")
40    }
41}
42
43const RAD_PER_DEG: f64 = PI / 180.0;
44
45impl Angle {
46    /// Creates a computed `Angle` value from a radian amount.
47    pub fn from_radians(radians: CSSFloat) -> Self {
48        Angle(radians / RAD_PER_DEG as f32)
49    }
50
51    /// Creates a computed `Angle` value from a degrees amount.
52    #[inline]
53    pub fn from_degrees(degrees: CSSFloat) -> Self {
54        Angle(degrees)
55    }
56
57    /// Returns the amount of radians this angle represents.
58    #[inline]
59    pub fn radians(&self) -> CSSFloat {
60        self.radians64().min(f32::MAX as f64).max(f32::MIN as f64) as f32
61    }
62
63    /// Returns the amount of radians this angle represents as a `f64`.
64    ///
65    /// Gecko stores angles as singles, but does this computation using doubles.
66    ///
67    /// This is significant enough to mess up rounding to the nearest
68    /// quarter-turn for 225 degrees, for example.
69    #[inline]
70    pub fn radians64(&self) -> f64 {
71        self.0 as f64 * RAD_PER_DEG
72    }
73
74    /// Return the value in degrees.
75    #[inline]
76    pub fn degrees(&self) -> CSSFloat {
77        self.0
78    }
79}
80
81impl Zero for Angle {
82    #[inline]
83    fn zero() -> Self {
84        Angle(0.0)
85    }
86
87    #[inline]
88    fn is_zero(&self) -> bool {
89        self.0 == 0.
90    }
91}
92
93impl ComputeSquaredDistance for Angle {
94    #[inline]
95    fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
96        // Use the formula for calculating the distance between angles defined in SVG:
97        // https://www.w3.org/TR/SVG/animate.html#complexDistances
98        self.radians64()
99            .compute_squared_distance(&other.radians64())
100    }
101}