Skip to content

Commit

Permalink
style: Properly track whether <angle> or <time> values came from calc…
Browse files Browse the repository at this point in the history
…() expressions.
  • Loading branch information
emilio committed Mar 27, 2017
1 parent c654c05 commit fc72f09
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 90 deletions.
2 changes: 1 addition & 1 deletion components/layout/webrender_helpers.rs
Expand Up @@ -200,7 +200,7 @@ impl ToFilterOps for filter::T {
Filter::Brightness(amount) => result.push(webrender_traits::FilterOp::Brightness(amount)),
Filter::Contrast(amount) => result.push(webrender_traits::FilterOp::Contrast(amount)),
Filter::Grayscale(amount) => result.push(webrender_traits::FilterOp::Grayscale(amount)),
Filter::HueRotate(angle) => result.push(webrender_traits::FilterOp::HueRotate(angle.0)),
Filter::HueRotate(angle) => result.push(webrender_traits::FilterOp::HueRotate(angle.radians())),
Filter::Invert(amount) => result.push(webrender_traits::FilterOp::Invert(amount)),
Filter::Opacity(amount) => result.push(webrender_traits::FilterOp::Opacity(amount.into())),
Filter::Saturate(amount) => result.push(webrender_traits::FilterOp::Saturate(amount)),
Expand Down
4 changes: 2 additions & 2 deletions components/style/animation.rs
Expand Up @@ -343,7 +343,7 @@ impl PropertyAnimation {

#[inline]
fn does_animate(&self) -> bool {
self.property.does_animate() && self.duration != Time(0.0)
self.property.does_animate() && self.duration.seconds() != 0.0
}

/// Whether this animation has the same end value as another one.
Expand Down Expand Up @@ -681,7 +681,7 @@ pub fn update_style_for_animation(context: &SharedStyleContext,
transition_property, name);
match PropertyAnimation::from_transition_property(*transition_property,
timing_function,
Time(relative_duration as f32),
Time::from_seconds(relative_duration as f32),
&from_style,
&target_style) {
Some(property_animation) => {
Expand Down
Expand Up @@ -36,7 +36,6 @@ use values::computed::{CalcLengthOrPercentage, Context, LengthOrPercentage};
use values::computed::{MaxLength, MinLength};
use values::computed::position::{HorizontalPosition, Position, VerticalPosition};
use values::computed::ToComputedValue;
use values::specified::Angle as SpecifiedAngle;



Expand Down Expand Up @@ -455,7 +454,7 @@ impl Interpolate for i32 {
impl Interpolate for Angle {
#[inline]
fn interpolate(&self, other: &Angle, progress: f64) -> Result<Self, ()> {
self.radians().interpolate(&other.radians(), progress).map(Angle)
self.radians().interpolate(&other.radians(), progress).map(Angle::from_radians)
}
}

Expand Down Expand Up @@ -952,7 +951,7 @@ fn build_identity_transform_list(list: &[TransformOperation]) -> Vec<TransformOp
result.push(TransformOperation::Matrix(identity));
}
TransformOperation::Skew(..) => {
result.push(TransformOperation::Skew(Angle(0.0), Angle(0.0)));
result.push(TransformOperation::Skew(Angle::zero(), Angle::zero()))
}
TransformOperation::Translate(..) => {
result.push(TransformOperation::Translate(LengthOrPercentage::zero(),
Expand All @@ -963,7 +962,7 @@ fn build_identity_transform_list(list: &[TransformOperation]) -> Vec<TransformOp
result.push(TransformOperation::Scale(1.0, 1.0, 1.0));
}
TransformOperation::Rotate(..) => {
result.push(TransformOperation::Rotate(0.0, 0.0, 1.0, Angle(0.0)));
result.push(TransformOperation::Rotate(0.0, 0.0, 1.0, Angle::zero()));
}
TransformOperation::Perspective(..) => {
// http://dev.w3.org/csswg/css-transforms/#identity-transform-function
Expand Down Expand Up @@ -1052,7 +1051,7 @@ fn interpolate_transform_list(from_list: &[TransformOperation],
}

/// https://drafts.csswg.org/css-transforms/#Rotate3dDefined
fn rotate_to_matrix(x: f32, y: f32, z: f32, a: SpecifiedAngle) -> ComputedMatrix {
fn rotate_to_matrix(x: f32, y: f32, z: f32, a: Angle) -> ComputedMatrix {
let half_rad = a.radians() / 2.0;
let sc = (half_rad).sin() * (half_rad).cos();
let sq = (half_rad).sin().powi(2);
Expand Down
28 changes: 16 additions & 12 deletions components/style/properties/longhand/box.mako.rs
Expand Up @@ -419,13 +419,13 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
}

#[inline]
pub fn get_initial_value() -> Time {
Time(0.0)
pub fn get_initial_value() -> computed_value::T {
computed_value::T::zero()
}

#[inline]
pub fn get_initial_specified_value() -> SpecifiedValue {
SpecifiedValue(0.0)
Time::zero()
}

pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
Expand Down Expand Up @@ -1221,7 +1221,7 @@ ${helpers.predefined_type("scroll-snap-coordinate",
let second = input.try(|input| {
try!(input.expect_comma());
specified::Angle::parse(context, input)
}).unwrap_or(specified::Angle(0.0));
}).unwrap_or(specified::Angle::zero());
Ok((first, second))
}

Expand Down Expand Up @@ -1581,14 +1581,14 @@ ${helpers.predefined_type("scroll-snap-coordinate",
"skewx" => {
try!(input.parse_nested_block(|input| {
let theta_x = try!(specified::Angle::parse(context,input));
result.push(SpecifiedOperation::Skew(theta_x, specified::Angle(0.0)));
result.push(SpecifiedOperation::Skew(theta_x, specified::Angle::zero()));
Ok(())
}))
},
"skewy" => {
try!(input.parse_nested_block(|input| {
let theta_y = try!(specified::Angle::parse(context,input));
result.push(SpecifiedOperation::Skew(specified::Angle(0.0), theta_y));
result.push(SpecifiedOperation::Skew(specified::Angle::zero(), theta_y));
Ok(())
}))
},
Expand Down Expand Up @@ -1640,11 +1640,13 @@ ${helpers.predefined_type("scroll-snap-coordinate",
let ax = ax.to_computed_value(context);
let ay = ay.to_computed_value(context);
let az = az.to_computed_value(context);
let theta = theta.to_computed_value(context);
let len = (ax * ax + ay * ay + az * az).sqrt();
result.push(computed_value::ComputedOperation::Rotate(ax / len, ay / len, az / len, theta));
}
SpecifiedOperation::Skew(theta_x, theta_y) => {
result.push(computed_value::ComputedOperation::Skew(theta_x, theta_y));
result.push(computed_value::ComputedOperation::Skew(theta_x.to_computed_value(context),
theta_y.to_computed_value(context)));
}
SpecifiedOperation::Perspective(ref d) => {
result.push(computed_value::ComputedOperation::Perspective(d.to_computed_value(context)));
Expand All @@ -1658,7 +1660,7 @@ ${helpers.predefined_type("scroll-snap-coordinate",
#[inline]
fn from_computed_value(computed: &computed_value::T) -> Self {
SpecifiedValue(computed.0.as_ref().map(|computed| {
let mut result = vec!();
let mut result = vec![];
for operation in computed {
match *operation {
computed_value::ComputedOperation::Matrix(ref matrix) => {
Expand All @@ -1678,15 +1680,17 @@ ${helpers.predefined_type("scroll-snap-coordinate",
Number::from_computed_value(sy),
Number::from_computed_value(sz)));
}
computed_value::ComputedOperation::Rotate(ref ax, ref ay, ref az, theta) => {
computed_value::ComputedOperation::Rotate(ref ax, ref ay, ref az, ref theta) => {
result.push(SpecifiedOperation::Rotate(
Number::from_computed_value(ax),
Number::from_computed_value(ay),
Number::from_computed_value(az),
theta));
specified::Angle::from_computed_value(theta)));
}
computed_value::ComputedOperation::Skew(theta_x, theta_y) => {
result.push(SpecifiedOperation::Skew(theta_x, theta_y));
computed_value::ComputedOperation::Skew(ref theta_x, ref theta_y) => {
result.push(SpecifiedOperation::Skew(
specified::Angle::from_computed_value(theta_x),
specified::Angle::from_computed_value(theta_y)))
}
computed_value::ComputedOperation::Perspective(ref d) => {
result.push(SpecifiedOperation::Perspective(
Expand Down
29 changes: 16 additions & 13 deletions components/style/properties/longhand/inherited_box.mako.rs
Expand Up @@ -96,7 +96,7 @@ ${helpers.single_keyword("image-rendering",

use std::f32::consts::PI;
use values::CSSFloat;
const TWO_PI: CSSFloat = 2.0*PI;
const TWO_PI: CSSFloat = 2.0 * PI;

#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
Expand Down Expand Up @@ -125,7 +125,7 @@ ${helpers.single_keyword("image-rendering",
}

pub mod computed_value {
use values::specified::Angle;
use values::computed::Angle;

#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
Expand All @@ -135,36 +135,35 @@ ${helpers.single_keyword("image-rendering",
}
}

const INITIAL_ANGLE: Angle = Angle(0.0);

#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T::AngleWithFlipped(INITIAL_ANGLE, false)
computed_value::T::AngleWithFlipped(computed::Angle::zero(), false)
}

// According to CSS Content Module Level 3:
// The computed value of the property is calculated by rounding the specified angle
// to the nearest quarter-turn, rounding away from 0, then moduloing the value by 1 turn.
#[inline]
fn normalize_angle(angle: &Angle) -> Angle {
fn normalize_angle(angle: &computed::Angle) -> computed::Angle {
let radians = angle.radians();
let rounded_quarter_turns = (4.0 * radians / TWO_PI).round();
let normalized_quarter_turns = (rounded_quarter_turns % 4.0 + 4.0) % 4.0;
let normalized_radians = normalized_quarter_turns/4.0 * TWO_PI;
Angle::from_radians(normalized_radians)
computed::Angle::from_radians(normalized_radians)
}

impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T;

#[inline]
fn to_computed_value(&self, _: &Context) -> computed_value::T {
fn to_computed_value(&self, context: &Context) -> computed_value::T {
if let Some(ref angle) = self.angle {
let normalized_angle = normalize_angle(angle);
let angle = angle.to_computed_value(context);
let normalized_angle = normalize_angle(&angle);
computed_value::T::AngleWithFlipped(normalized_angle, self.flipped)
} else {
if self.flipped {
computed_value::T::AngleWithFlipped(INITIAL_ANGLE, true)
computed_value::T::AngleWithFlipped(computed::Angle::zero(), true)
} else {
computed_value::T::FromImage
}
Expand All @@ -175,8 +174,12 @@ ${helpers.single_keyword("image-rendering",
fn from_computed_value(computed: &computed_value::T) -> Self {
match *computed {
computed_value::T::FromImage => SpecifiedValue { angle: None, flipped: false },
computed_value::T::AngleWithFlipped(angle, flipped) =>
SpecifiedValue { angle: Some(angle), flipped: flipped },
computed_value::T::AngleWithFlipped(ref angle, flipped) => {
SpecifiedValue {
angle: Some(Angle::from_computed_value(angle)),
flipped: flipped,
}
}
}
}
}
Expand Down Expand Up @@ -205,7 +208,7 @@ ${helpers.single_keyword("image-rendering",
let angle = input.try(|input| Angle::parse(context, input)).ok();
let flipped = input.try(|input| input.expect_ident_matching("flip")).is_ok();
let explicit_angle = if angle.is_none() && !flipped {
Some(INITIAL_ANGLE)
Some(Angle::zero())
} else {
angle
};
Expand Down
18 changes: 9 additions & 9 deletions components/style/values/computed/image.rs
Expand Up @@ -537,27 +537,27 @@ impl ToComputedValue for specified::AngleOrCorner {
type ComputedValue = AngleOrCorner;

#[inline]
fn to_computed_value(&self, _: &Context) -> AngleOrCorner {
fn to_computed_value(&self, context: &Context) -> AngleOrCorner {
match *self {
specified::AngleOrCorner::None => {
AngleOrCorner::Angle(Angle(PI))
AngleOrCorner::Angle(Angle::from_radians(PI))
},
specified::AngleOrCorner::Angle(angle) => {
AngleOrCorner::Angle(angle)
AngleOrCorner::Angle(angle.to_computed_value(context))
},
specified::AngleOrCorner::Corner(horizontal, vertical) => {
match (horizontal, vertical) {
(None, Some(VerticalDirection::Top)) => {
AngleOrCorner::Angle(Angle(0.0))
AngleOrCorner::Angle(Angle::from_radians(0.0))
},
(Some(HorizontalDirection::Right), None) => {
AngleOrCorner::Angle(Angle(PI * 0.5))
AngleOrCorner::Angle(Angle::from_radians(PI * 0.5))
},
(None, Some(VerticalDirection::Bottom)) => {
AngleOrCorner::Angle(Angle(PI))
AngleOrCorner::Angle(Angle::from_radians(PI))
},
(Some(HorizontalDirection::Left), None) => {
AngleOrCorner::Angle(Angle(PI * 1.5))
AngleOrCorner::Angle(Angle::from_radians(PI * 1.5))
},
(Some(horizontal), Some(vertical)) => {
AngleOrCorner::Corner(horizontal, vertical)
Expand All @@ -573,8 +573,8 @@ impl ToComputedValue for specified::AngleOrCorner {
#[inline]
fn from_computed_value(computed: &AngleOrCorner) -> Self {
match *computed {
AngleOrCorner::Angle(angle) => {
specified::AngleOrCorner::Angle(angle)
AngleOrCorner::Angle(ref angle) => {
specified::AngleOrCorner::Angle(specified::Angle::from_computed_value(angle))
},
AngleOrCorner::Corner(horizontal, vertical) => {
specified::AngleOrCorner::Corner(Some(horizontal), Some(vertical))
Expand Down
72 changes: 71 additions & 1 deletion components/style/values/computed/mod.rs
Expand Up @@ -20,7 +20,7 @@ pub use self::image::{LengthOrKeyword, LengthOrPercentageOrKeyword};
pub use super::{Auto, Either, None_};
#[cfg(feature = "gecko")]
pub use super::specified::{AlignItems, AlignJustifyContent, AlignJustifySelf, JustifyItems};
pub use super::specified::{Angle, BorderStyle, GridLine, Percentage, Time, UrlOrNone};
pub use super::specified::{BorderStyle, GridLine, Percentage, UrlOrNone};
pub use super::specified::url::SpecifiedUrl;
pub use self::length::{CalcLengthOrPercentage, Length, LengthOrNumber, LengthOrPercentage, LengthOrPercentageOrAuto};
pub use self::length::{LengthOrPercentageOrAutoOrContent, LengthOrPercentageOrNone, LengthOrNone};
Expand Down Expand Up @@ -114,6 +114,76 @@ impl<T> ToComputedValue for T
}
}

/// A computed `<angle>` value.
#[derive(Clone, PartialEq, PartialOrd, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct Angle {
radians: CSSFloat,
}

impl Angle {
/// Construct a computed `Angle` value from a radian amount.
pub fn from_radians(radians: CSSFloat) -> Self {
Angle {
radians: radians,
}
}

/// Return the amount of radians this angle represents.
#[inline]
pub fn radians(&self) -> CSSFloat {
self.radians
}

/// Returns an angle that represents a rotation of zero radians.
pub fn zero() -> Self {
Self::from_radians(0.0)
}
}

impl ToCss for Angle {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write,
{
write!(dest, "{}rad", self.radians())
}
}

/// A computed `<time>` value.
#[derive(Clone, PartialEq, PartialOrd, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct Time {
seconds: CSSFloat,
}

impl Time {
/// Construct a computed `Time` value from a seconds amount.
pub fn from_seconds(seconds: CSSFloat) -> Self {
Time {
seconds: seconds,
}
}

/// Construct a computed `Time` value that represents zero seconds.
pub fn zero() -> Self {
Self::from_seconds(0.0)
}

/// Return the amount of seconds this time represents.
#[inline]
pub fn seconds(&self) -> CSSFloat {
self.seconds
}
}

impl ToCss for Time {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write,
{
write!(dest, "{}s", self.seconds())
}
}

impl ToComputedValue for specified::Color {
type ComputedValue = RGBA;

Expand Down

0 comments on commit fc72f09

Please sign in to comment.