mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
style: Always compute angle values to degrees.
This matches the spec, https://drafts.csswg.org/css-values/#angles, which says: > All <angle> units are compatible, and deg is their canonical unit. And https://drafts.csswg.org/css-values/#compat, which says: >When serializing computed values [...], compatible units [...] are converted into a single canonical unit. And also other implementations (Blink always serializes angles as degrees in computed style for example). Also allows us to get rid of quite a bit of code, and makes computed angle value representation just a number, which is nice. Differential Revision: https://phabricator.services.mozilla.com/D8619
This commit is contained in:
parent
11fedf18d9
commit
42def5a011
11 changed files with 142 additions and 209 deletions
|
@ -5,38 +5,41 @@
|
|||
//! Computed angles.
|
||||
|
||||
use num_traits::Zero;
|
||||
use std::fmt::{self, Write};
|
||||
use std::{f32, f64};
|
||||
use std::f64::consts::PI;
|
||||
use std::ops::Add;
|
||||
use style_traits::{CssWriter, ToCss};
|
||||
use values::CSSFloat;
|
||||
use values::animated::{Animate, Procedure};
|
||||
use values::distance::{ComputeSquaredDistance, SquaredDistance};
|
||||
|
||||
/// A computed angle.
|
||||
#[animate(fallback = "Self::animate_fallback")]
|
||||
/// A computed angle in degrees.
|
||||
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
|
||||
#[derive(
|
||||
Animate, Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToAnimatedZero, ToCss,
|
||||
)]
|
||||
pub enum Angle {
|
||||
/// An angle with degree unit.
|
||||
#[css(dimension)]
|
||||
Deg(CSSFloat),
|
||||
/// An angle with gradian unit.
|
||||
#[css(dimension)]
|
||||
Grad(CSSFloat),
|
||||
/// An angle with radian unit.
|
||||
#[css(dimension)]
|
||||
Rad(CSSFloat),
|
||||
/// An angle with turn unit.
|
||||
#[css(dimension)]
|
||||
Turn(CSSFloat),
|
||||
#[derive(Animate, Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToAnimatedZero)]
|
||||
pub struct Angle(CSSFloat);
|
||||
|
||||
impl ToCss for Angle {
|
||||
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
|
||||
where
|
||||
W: Write,
|
||||
{
|
||||
self.degrees().to_css(dest)?;
|
||||
dest.write_str("deg")
|
||||
}
|
||||
}
|
||||
|
||||
const RAD_PER_DEG: f64 = PI / 180.0;
|
||||
|
||||
impl Angle {
|
||||
/// Creates a computed `Angle` value from a radian amount.
|
||||
pub fn from_radians(radians: CSSFloat) -> Self {
|
||||
Angle::Rad(radians)
|
||||
Angle(radians / RAD_PER_DEG as f32)
|
||||
}
|
||||
|
||||
/// Creates a computed `Angle` value from a degrees amount.
|
||||
#[inline]
|
||||
pub fn from_degrees(degrees: CSSFloat) -> Self {
|
||||
Angle(degrees)
|
||||
}
|
||||
|
||||
/// Returns the amount of radians this angle represents.
|
||||
|
@ -48,43 +51,18 @@ impl Angle {
|
|||
/// Returns the amount of radians this angle represents as a `f64`.
|
||||
///
|
||||
/// Gecko stores angles as singles, but does this computation using doubles.
|
||||
/// See nsCSSValue::GetAngleValueInRadians.
|
||||
///
|
||||
/// This is significant enough to mess up rounding to the nearest
|
||||
/// quarter-turn for 225 degrees, for example.
|
||||
#[inline]
|
||||
pub fn radians64(&self) -> f64 {
|
||||
const RAD_PER_DEG: f64 = PI / 180.0;
|
||||
const RAD_PER_GRAD: f64 = PI / 200.0;
|
||||
const RAD_PER_TURN: f64 = PI * 2.0;
|
||||
|
||||
let radians = match *self {
|
||||
Angle::Deg(val) => val as f64 * RAD_PER_DEG,
|
||||
Angle::Grad(val) => val as f64 * RAD_PER_GRAD,
|
||||
Angle::Turn(val) => val as f64 * RAD_PER_TURN,
|
||||
Angle::Rad(val) => val as f64,
|
||||
};
|
||||
radians.min(f64::MAX).max(f64::MIN)
|
||||
self.0 as f64 * RAD_PER_DEG as f64
|
||||
}
|
||||
|
||||
/// Return the value in degrees.
|
||||
pub fn degrees(&self) -> f32 {
|
||||
use std::f32::consts::PI;
|
||||
self.radians() * 360. / (2. * PI)
|
||||
}
|
||||
|
||||
/// <https://drafts.csswg.org/css-transitions/#animtype-number>
|
||||
#[inline]
|
||||
fn animate_fallback(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
|
||||
Ok(Angle::from_radians(
|
||||
self.radians().animate(&other.radians(), procedure)?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<Angle> for Angle {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> &Self {
|
||||
self
|
||||
pub fn degrees(&self) -> CSSFloat {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,27 +71,19 @@ impl Add for Angle {
|
|||
|
||||
#[inline]
|
||||
fn add(self, rhs: Self) -> Self {
|
||||
match (self, rhs) {
|
||||
(Angle::Deg(x), Angle::Deg(y)) => Angle::Deg(x + y),
|
||||
(Angle::Grad(x), Angle::Grad(y)) => Angle::Grad(x + y),
|
||||
(Angle::Turn(x), Angle::Turn(y)) => Angle::Turn(x + y),
|
||||
(Angle::Rad(x), Angle::Rad(y)) => Angle::Rad(x + y),
|
||||
_ => Angle::from_radians(self.radians() + rhs.radians()),
|
||||
}
|
||||
Angle(self.0 + rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Zero for Angle {
|
||||
#[inline]
|
||||
fn zero() -> Self {
|
||||
Angle::from_radians(0.0)
|
||||
Angle(0.0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_zero(&self) -> bool {
|
||||
match *self {
|
||||
Angle::Deg(val) | Angle::Grad(val) | Angle::Turn(val) | Angle::Rad(val) => val == 0.,
|
||||
}
|
||||
self.0 == 0.
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -122,7 +92,6 @@ impl ComputeSquaredDistance for Angle {
|
|||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
||||
// Use the formula for calculating the distance between angles defined in SVG:
|
||||
// https://www.w3.org/TR/SVG/animate.html#complexDistances
|
||||
self.radians64()
|
||||
.compute_squared_distance(&other.radians64())
|
||||
self.radians64().compute_squared_distance(&other.radians64())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue