From f6c652dd9c4b0813ad56e26d7a84010da8af1c0b Mon Sep 17 00:00:00 2001 From: Shing Lyu Date: Tue, 20 Jun 2017 16:39:10 +0800 Subject: [PATCH] Convert -webkit-linear-gradient coordinate system for Stylo --- components/style/values/computed/image.rs | 83 ++++++++++++++++++++--- components/style/values/generics/image.rs | 4 +- 2 files changed, 76 insertions(+), 11 deletions(-) diff --git a/components/style/values/computed/image.rs b/components/style/values/computed/image.rs index 3718be2fbd3..05640c94f6f 100644 --- a/components/style/values/computed/image.rs +++ b/components/style/values/computed/image.rs @@ -18,7 +18,9 @@ use values::generics::image::{CompatMode, ColorStop as GenericColorStop, EndingS use values::generics::image::{Gradient as GenericGradient, GradientItem as GenericGradientItem}; use values::generics::image::{Image as GenericImage, GradientKind as GenericGradientKind}; use values::generics::image::{ImageRect as GenericImageRect, LineDirection as GenericLineDirection}; -use values::specified::image::LineDirection as SpecifiedLineDirection; +use values::generics::position::Position as GenericPosition; +use values::specified::image::{Gradient as SpecifiedGradient, LineDirection as SpecifiedLineDirection}; +use values::specified::image::{GradientKind as SpecifiedGradientKind}; use values::specified::position::{X, Y}; /// A computed image layer. @@ -93,25 +95,38 @@ impl GenericLineDirection for LineDirection { } } -impl ToComputedValue for SpecifiedLineDirection { - type ComputedValue = LineDirection; +impl SpecifiedLineDirection { + /// Takes a modern linear gradient angle and convert it to Gecko's old coordinate for + /// webkit-prefixed version + fn to_gecko_coordinate(modern_angle: f32, _compat_mode: CompatMode) -> f32 { + #[cfg(feature = "gecko")] + { + return match _compat_mode { + CompatMode::Modern => modern_angle, + CompatMode::WebKit => -modern_angle + 270. + } + } + #[cfg(feature = "servo")] + modern_angle + } - fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { + /// Manually derived to_computed_value + fn to_computed_value(&self, context: &Context, compat_mode: CompatMode) -> LineDirection { match *self { SpecifiedLineDirection::Angle(ref angle) => { LineDirection::Angle(angle.to_computed_value(context)) }, SpecifiedLineDirection::Horizontal(X::Left) => { - LineDirection::Angle(Angle::Degree(270.)) + LineDirection::Angle(Angle::Degree(SpecifiedLineDirection::to_gecko_coordinate(270., compat_mode))) }, SpecifiedLineDirection::Horizontal(X::Right) => { - LineDirection::Angle(Angle::Degree(90.)) + LineDirection::Angle(Angle::Degree(SpecifiedLineDirection::to_gecko_coordinate(90., compat_mode))) }, SpecifiedLineDirection::Vertical(Y::Top) => { - LineDirection::Angle(Angle::Degree(0.)) + LineDirection::Angle(Angle::Degree(SpecifiedLineDirection::to_gecko_coordinate(0., compat_mode))) }, SpecifiedLineDirection::Vertical(Y::Bottom) => { - LineDirection::Angle(Angle::Degree(180.)) + LineDirection::Angle(Angle::Degree(SpecifiedLineDirection::to_gecko_coordinate(180., compat_mode))) }, SpecifiedLineDirection::Corner(x, y) => { LineDirection::Corner(x, y) @@ -119,7 +134,7 @@ impl ToComputedValue for SpecifiedLineDirection { } } - fn from_computed_value(computed: &Self::ComputedValue) -> Self { + fn from_computed_value(computed: &LineDirection) -> Self { match *computed { LineDirection::Angle(ref angle) => { SpecifiedLineDirection::Angle(ToComputedValue::from_computed_value(angle)) @@ -130,3 +145,53 @@ impl ToComputedValue for SpecifiedLineDirection { } } } + +impl ToComputedValue for SpecifiedGradient { + type ComputedValue = Gradient; + + fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { + Self::ComputedValue { + kind: self.kind.to_computed_value(context, self.compat_mode), + items: self.items.to_computed_value(context), + repeating: self.repeating, + compat_mode: self.compat_mode + } + } + + fn from_computed_value(computed: &Self::ComputedValue) -> Self { + Self { + kind: SpecifiedGradientKind::from_computed_value(&computed.kind), + items: ToComputedValue::from_computed_value(&computed.items), + repeating: computed.repeating, + compat_mode: computed.compat_mode + } + } +} + +impl SpecifiedGradientKind { + /// Manually derived to_computed_value + pub fn to_computed_value(&self, context: &Context, compat_mode: CompatMode) -> GradientKind { + match self { + &GenericGradientKind::Linear(ref line_direction) => { + GenericGradientKind::Linear(line_direction.to_computed_value(context, compat_mode)) + }, + &GenericGradientKind::Radial(ref ending_shape, ref position) => { + GenericGradientKind::Radial(ending_shape.to_computed_value(context), + position.to_computed_value(context)) + } + } + } + + /// Manually derived from_computed_value + pub fn from_computed_value(computed: &GradientKind) -> SpecifiedGradientKind { + match *computed { + GenericGradientKind::Linear(line_direction) => { + GenericGradientKind::Linear(SpecifiedLineDirection::from_computed_value(&line_direction)) + }, + GenericGradientKind::Radial(ending_shape, position) => { + GenericGradientKind::Radial(GenericEndingShape::from_computed_value(&ending_shape), + GenericPosition::from_computed_value(&position)) + } + } + } +} diff --git a/components/style/values/generics/image.rs b/components/style/values/generics/image.rs index 20094e75935..a8a5d10a81c 100644 --- a/components/style/values/generics/image.rs +++ b/components/style/values/generics/image.rs @@ -35,7 +35,7 @@ pub enum Image { /// A CSS gradient. /// https://drafts.csswg.org/css-images/#gradients -#[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToComputedValue)] +#[derive(Clone, Debug, HasViewportPercentage, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct Gradient { /// Gradients can be linear or radial. @@ -59,7 +59,7 @@ pub enum CompatMode { } /// A gradient kind. -#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)] +#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum GradientKind { /// A linear gradient.