From 979a2798d5ae140e447f4a0b3720ac1fe9eb3f03 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 19 Aug 2016 18:25:23 +0530 Subject: [PATCH] Make Position a gecko-only vector longhand --- components/servo/Cargo.lock | 1 + components/style/Cargo.toml | 1 + components/style/lib.rs | 1 + components/style/properties/gecko.mako.rs | 47 ++++++++++++++----- .../helpers/animated_properties.mako.rs | 43 ++++++++++++----- .../properties/longhand/background.mako.rs | 46 ++++++++++++++++-- ports/cef/Cargo.lock | 1 + ports/geckolib/Cargo.lock | 9 ++++ 8 files changed, 123 insertions(+), 26 deletions(-) diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 2d8398feda0e..c6da44feb702 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -2225,6 +2225,7 @@ dependencies = [ "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "ordered-float 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "plugins 0.0.1", diff --git a/components/style/Cargo.toml b/components/style/Cargo.toml index 4f0d53cce208..daceb3178d19 100644 --- a/components/style/Cargo.toml +++ b/components/style/Cargo.toml @@ -34,6 +34,7 @@ heapsize_plugin = {version = "0.1.2", optional = true} lazy_static = "0.2" log = "0.3.5" matches = "0.1" +num-integer = "0.1.32" num-traits = "0.1.32" ordered-float = "0.2.2" quickersort = "2.0.0" diff --git a/components/style/lib.rs b/components/style/lib.rs index 3386f8121866..6ccf57f2df62 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -56,6 +56,7 @@ extern crate log; #[allow(unused_extern_crates)] #[macro_use] extern crate matches; +extern crate num_integer; extern crate num_traits; extern crate ordered_float; extern crate quickersort; diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index e58727fae677..f076c9a59058 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -944,7 +944,7 @@ fn static_assert() { } for (layer, other) in self.gecko.mImage.mLayers.iter_mut() .zip(other.gecko.mImage.mLayers.iter()) - .take(other.gecko.mImage.${field_name}Count) { + .take(other.gecko.mImage.${field_name}Count as usize) { layer.${field_name} = other.${field_name}; } self.gecko.mImage.${field_name}Count = other.gecko.mImage.${field_name}Count; @@ -1033,23 +1033,48 @@ fn static_assert() { self.gecko.mImage.mPositionYCount = cmp::min(1, other.gecko.mImage.mPositionYCount); self.gecko.mImage.mLayers.mFirstElement.mPosition = other.gecko.mImage.mLayers.mFirstElement.mPosition; + unsafe { + Gecko_EnsureImageLayersLength(&mut self.gecko.mImage, other.gecko.mImage.mLayers.len()); + } + for (layer, other) in self.gecko.mImage.mLayers.iter_mut() + .zip(other.gecko.mImage.mLayers.iter()) + .take(other.gecko.mImage.mPositionXCount as usize) { + layer.mPosition.mXPosition = other.mPosition.mXPosition; + } + for (layer, other) in self.gecko.mImage.mLayers.iter_mut() + .zip(other.gecko.mImage.mLayers.iter()) + .take(other.gecko.mImage.mPositionYCount as usize) { + layer.mPosition.mYPosition = other.mPosition.mYPosition; + } + self.gecko.mImage.mPositionXCount = other.gecko.mImage.mPositionXCount; + self.gecko.mImage.mPositionYCount = other.gecko.mImage.mPositionYCount; } pub fn clone_background_position(&self) -> longhands::background_position::computed_value::T { use values::computed::position::Position; - let position = &self.gecko.mImage.mLayers.mFirstElement.mPosition; - Position { - horizontal: position.mXPosition.into(), - vertical: position.mYPosition.into(), - } + longhands::background_position::computed_value::T( + self.gecko.mImage.mLayers.iter() + .take(self.gecko.mImage.mPositionXCount as usize) + .take(self.gecko.mImage.mPositionYCount as usize) + .map(|position| Position { + horizontal: position.mPosition.mXPosition.into(), + vertical: position.mPosition.mYPosition.into(), + }) + .collect() + ) } pub fn set_background_position(&mut self, v: longhands::background_position::computed_value::T) { - let position = &mut self.gecko.mImage.mLayers.mFirstElement.mPosition; - position.mXPosition = v.horizontal.into(); - position.mYPosition = v.vertical.into(); - self.gecko.mImage.mPositionXCount = 1; - self.gecko.mImage.mPositionYCount = 1; + unsafe { + Gecko_EnsureImageLayersLength(&mut self.gecko.mImage, v.0.len()); + } + + self.gecko.mImage.mPositionXCount = v.0.len() as u32; + self.gecko.mImage.mPositionYCount = v.0.len() as u32; + for (servo, geckolayer) in v.0.into_iter().zip(self.gecko.mImage.mLayers.iter_mut()) { + geckolayer.mPosition.mXPosition = servo.horizontal.into(); + geckolayer.mPosition.mYPosition = servo.vertical.into(); + } } pub fn copy_background_image_from(&mut self, other: &Self) { diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index a45b68b6816e..6eb79de97e3a 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -155,6 +155,25 @@ pub trait Interpolate: Sized { fn interpolate(&self, other: &Self, time: f64) -> Result; } +/// https://drafts.csswg.org/css-transitions/#animtype-repeatable-list +pub trait RepeatableListInterpolate: Interpolate {} + +impl Interpolate for Vec { + fn interpolate(&self, other: &Self, time: f64) -> Result { + use num_integer::lcm; + let len = lcm(self.len(), other.len()); + let ret = self.iter().cycle().zip(other.iter().cycle()) + .take(len) + .filter_map(|(ref me, ref you)| { + me.interpolate(you, time).ok() + }).collect::(); + if ret.len() == len { + Ok(ret) + } else { + Err(()) + } + } +} /// https://drafts.csswg.org/css-transitions/#animtype-number impl Interpolate for Au { #[inline] @@ -296,6 +315,14 @@ impl Interpolate for BorderSpacing { } } +impl Interpolate for BackgroundSize { + #[inline] + fn interpolate(&self, other: &Self, time: f64) -> Result { + self.0.interpolate(&other.0, time).map(BackgroundSize) + } +} + + /// https://drafts.csswg.org/css-transitions/#animtype-color impl Interpolate for RGBA { #[inline] @@ -496,18 +523,12 @@ impl Interpolate for Position { } } -impl Interpolate for BackgroundSize { +impl RepeatableListInterpolate for Position {} + +impl Interpolate for BackgroundPosition { + #[inline] fn interpolate(&self, other: &Self, time: f64) -> Result { - use properties::longhands::background_size::computed_value::ExplicitSize; - match (self, other) { - (&BackgroundSize::Explicit(ref me), &BackgroundSize::Explicit(ref other)) => { - Ok(BackgroundSize::Explicit(ExplicitSize { - width: try!(me.width.interpolate(&other.width, time)), - height: try!(me.height.interpolate(&other.height, time)), - })) - } - _ => Err(()), - } + Ok(BackgroundPosition(try!(self.0.interpolate(&other.0, time)))) } } diff --git a/components/style/properties/longhand/background.mako.rs b/components/style/properties/longhand/background.mako.rs index 3b72d1cc0efa..fbf4c8c3fd42 100644 --- a/components/style/properties/longhand/background.mako.rs +++ b/components/style/properties/longhand/background.mako.rs @@ -54,6 +54,10 @@ ${helpers.predefined_type("background-color", "CSSColor", pub fn get_initial_value() -> computed_value::T { computed_value::T(None) } + #[inline] + pub fn get_initial_specified_value() -> SpecifiedValue { + SpecifiedValue(None) + } pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { if input.try(|input| input.expect_ident_matching("none")).is_ok() { Ok(SpecifiedValue(None)) @@ -75,7 +79,7 @@ ${helpers.predefined_type("background-color", "CSSColor", } -<%helpers:longhand name="background-position" animatable="True"> +<%helpers:vector_longhand name="background-position" gecko_only="True" animatable="True"> use cssparser::ToCss; use std::fmt; use values::LocalToCss; @@ -84,6 +88,7 @@ ${helpers.predefined_type("background-color", "CSSColor", pub mod computed_value { use values::computed::position::Position; + use properties::animated_properties::{Interpolate, RepeatableListInterpolate}; pub type T = Position; } @@ -98,12 +103,20 @@ ${helpers.predefined_type("background-color", "CSSColor", vertical: computed::LengthOrPercentage::Percentage(0.0), } } + #[inline] + pub fn get_initial_specified_value() -> SpecifiedValue { + use values::specified::Percentage; + Position { + horizontal: specified::LengthOrPercentage::Percentage(Percentage(0.0)), + vertical: specified::LengthOrPercentage::Percentage(Percentage(0.0)), + } + } pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result { Ok(try!(Position::parse(input))) } - + ${helpers.single_keyword("background-repeat", "repeat repeat-x repeat-y no-repeat", @@ -129,7 +142,7 @@ ${helpers.single_keyword("background-origin", gecko_only=True, animatable=False)} -<%helpers:longhand name="background-size" animatable="True"> +<%helpers:vector_longhand name="background-size" animatable="True"> use cssparser::{ToCss, Token}; use std::ascii::AsciiExt; use std::fmt; @@ -137,6 +150,7 @@ ${helpers.single_keyword("background-origin", pub mod computed_value { use values::computed::LengthOrPercentageOrAuto; + use properties::animated_properties::{Interpolate, RepeatableListInterpolate}; #[derive(PartialEq, Clone, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] @@ -152,6 +166,23 @@ ${helpers.single_keyword("background-origin", Cover, Contain, } + + impl RepeatableListInterpolate for T {} + + impl Interpolate for T { + fn interpolate(&self, other: &Self, time: f64) -> Result { + use properties::longhands::background_size::single_value::computed_value::ExplicitSize; + match (self, other) { + (&T::Explicit(ref me), &T::Explicit(ref other)) => { + Ok(T::Explicit(ExplicitSize { + width: try!(me.width.interpolate(&other.width, time)), + height: try!(me.height.interpolate(&other.height, time)), + })) + } + _ => Err(()), + } + } + } } impl ToCss for computed_value::T { @@ -245,6 +276,13 @@ ${helpers.single_keyword("background-origin", height: computed::LengthOrPercentageOrAuto::Auto, }) } + #[inline] + pub fn get_initial_specified_value() -> SpecifiedValue { + SpecifiedValue::Explicit(SpecifiedExplicitSize { + width: specified::LengthOrPercentageOrAuto::Auto, + height: specified::LengthOrPercentageOrAuto::Auto, + }) + } pub fn parse(_: &ParserContext, input: &mut Parser) -> Result { let width; @@ -282,4 +320,4 @@ ${helpers.single_keyword("background-origin", height: height, })) } - + diff --git a/ports/cef/Cargo.lock b/ports/cef/Cargo.lock index 341e24b7b192..12c50e327ec4 100644 --- a/ports/cef/Cargo.lock +++ b/ports/cef/Cargo.lock @@ -2108,6 +2108,7 @@ dependencies = [ "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "ordered-float 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "plugins 0.0.1", diff --git a/ports/geckolib/Cargo.lock b/ports/geckolib/Cargo.lock index d29ee9a5347d..69baaa7d7d36 100644 --- a/ports/geckolib/Cargo.lock +++ b/ports/geckolib/Cargo.lock @@ -234,6 +234,14 @@ dependencies = [ "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num-integer" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "num-traits" version = "0.1.35" @@ -363,6 +371,7 @@ dependencies = [ "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "ordered-float 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "quickersort 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",