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:
Emilio Cobos Álvarez 2018-10-13 00:41:03 +00:00
parent 11fedf18d9
commit 42def5a011
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
11 changed files with 142 additions and 209 deletions

View file

@ -10,6 +10,7 @@ use num_traits::Zero;
use values::{computed, CSSFloat};
use values::computed::length::Length as ComputedLength;
use values::computed::length::LengthOrPercentage as ComputedLengthOrPercentage;
use values::specified::angle::Angle as SpecifiedAngle;
use values::specified::length::Length as SpecifiedLength;
use values::specified::length::LengthOrPercentage as SpecifiedLengthOrPercentage;
@ -394,10 +395,30 @@ pub trait ToMatrix {
fn to_3d_matrix(&self, reference_box: Option<&Rect<Au>>) -> Result<Transform3D<f64>, ()>;
}
/// A little helper to deal with both specified and computed angles.
pub trait ToRadians {
/// Return the radians value as a 64-bit floating point value.
fn radians64(&self) -> f64;
}
impl ToRadians for computed::angle::Angle {
#[inline]
fn radians64(&self) -> f64 {
computed::angle::Angle::radians64(self)
}
}
impl ToRadians for SpecifiedAngle {
#[inline]
fn radians64(&self) -> f64 {
computed::angle::Angle::from_degrees(self.degrees()).radians64()
}
}
impl<Angle, Number, Length, Integer, LoP> ToMatrix
for TransformOperation<Angle, Number, Length, Integer, LoP>
where
Angle: Copy + AsRef<computed::angle::Angle>,
Angle: ToRadians + Copy,
Number: Copy + Into<f32> + Into<f64>,
Length: ToAbsoluteLength,
LoP: ToAbsoluteLength,
@ -426,7 +447,7 @@ where
let reference_height = reference_box.map(|v| v.size.height);
let matrix = match *self {
Rotate3D(ax, ay, az, theta) => {
let theta = TWO_PI - theta.as_ref().radians64();
let theta = TWO_PI - theta.radians64();
let (ax, ay, az, theta) =
get_normalized_vector_and_angle(ax.into(), ay.into(), az.into(), theta);
Transform3D::create_rotation(
@ -437,15 +458,15 @@ where
)
},
RotateX(theta) => {
let theta = euclid::Angle::radians(TWO_PI - theta.as_ref().radians64());
let theta = euclid::Angle::radians(TWO_PI - theta.radians64());
Transform3D::create_rotation(1., 0., 0., theta)
},
RotateY(theta) => {
let theta = euclid::Angle::radians(TWO_PI - theta.as_ref().radians64());
let theta = euclid::Angle::radians(TWO_PI - theta.radians64());
Transform3D::create_rotation(0., 1., 0., theta)
},
RotateZ(theta) | Rotate(theta) => {
let theta = euclid::Angle::radians(TWO_PI - theta.as_ref().radians64());
let theta = euclid::Angle::radians(TWO_PI - theta.radians64());
Transform3D::create_rotation(0., 0., 1., theta)
},
Perspective(ref d) => {
@ -479,16 +500,16 @@ where
Transform3D::create_translation(0., 0., z.to_pixel_length(None)? as f64)
},
Skew(theta_x, theta_y) => Transform3D::create_skew(
euclid::Angle::radians(theta_x.as_ref().radians64()),
euclid::Angle::radians(theta_y.map_or(0., |a| a.as_ref().radians64())),
euclid::Angle::radians(theta_x.radians64()),
euclid::Angle::radians(theta_y.map_or(0., |a| a.radians64())),
),
SkewX(theta) => Transform3D::create_skew(
euclid::Angle::radians(theta.as_ref().radians64()),
euclid::Angle::radians(theta.radians64()),
euclid::Angle::radians(0.),
),
SkewY(theta) => Transform3D::create_skew(
euclid::Angle::radians(0.),
euclid::Angle::radians(theta.as_ref().radians64()),
euclid::Angle::radians(theta.radians64()),
),
Matrix3D(m) => m.into(),
Matrix(m) => m.into(),