diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 65ee7860d33..f77a90b9e19 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -579,7 +579,7 @@ def set_gecko_property(ffi_name, expr): <%def name="impl_svg_paint(ident, gecko_ffi_name, need_clone=False)"> #[allow(non_snake_case)] pub fn set_${ident}(&mut self, mut v: longhands::${ident}::computed_value::T) { - use values::generics::SVGPaintKind; + use values::generics::svg::SVGPaintKind; use self::structs::nsStyleSVGPaintType; use self::structs::nsStyleSVGFallbackType; @@ -632,7 +632,7 @@ def set_gecko_property(ffi_name, expr): #[allow(non_snake_case)] pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { - use values::generics::{SVGPaint, SVGPaintKind}; + use values::generics::svg::{SVGPaint, SVGPaintKind}; use values::specified::url::SpecifiedUrl; use self::structs::nsStyleSVGPaintType; use self::structs::nsStyleSVGFallbackType; diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index 1b070e74879..a5ccad0830c 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -43,10 +43,10 @@ use values::computed::{Angle, LengthOrPercentageOrAuto, LengthOrPercentageOrNone use values::computed::{BorderCornerRadius, ClipRect}; use values::computed::{CalcLengthOrPercentage, Color, Context, ComputedValueAsSpecified}; use values::computed::{LengthOrPercentage, MaxLength, MozLength, Percentage, ToComputedValue}; -use values::generics::{SVGPaint, SVGPaintKind}; use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius; use values::generics::effects::Filter; use values::generics::position as generic_position; +use values::generics::svg::{SVGPaint, SVGPaintKind}; /// A trait used to implement various procedures used during animation. pub trait Animatable: Sized { diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index e9ae9474469..f345128d43e 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -17,7 +17,7 @@ use std::f64; use std::f64::consts::PI; use std::fmt; use style_traits::ToCss; -use super::{CSSFloat, CSSInteger, RGBA}; +use super::{CSSFloat, CSSInteger}; use super::generics::grid::{TrackBreadth as GenericTrackBreadth, TrackSize as GenericTrackSize}; use super::generics::grid::GridTemplateComponent as GenericGridTemplateComponent; use super::generics::grid::TrackList as GenericTrackList; @@ -44,6 +44,7 @@ pub use super::specified::url::SpecifiedUrl; pub use self::length::{CalcLengthOrPercentage, Length, LengthOrNone, LengthOrNumber, LengthOrPercentage}; pub use self::length::{LengthOrPercentageOrAuto, LengthOrPercentageOrNone, MaxLength, MozLength, Percentage}; pub use self::position::Position; +pub use self::svg::{SVGPaint, SVGPaintKind}; pub use self::text::{InitialLetter, LetterSpacing, LineHeight, WordSpacing}; pub use self::transform::{TimingFunction, TransformOrigin}; @@ -61,6 +62,7 @@ pub mod gecko; pub mod length; pub mod position; pub mod rect; +pub mod svg; pub mod text; pub mod transform; @@ -454,31 +456,6 @@ impl IntegerOrAuto { } } -/// Computed SVG Paint value -pub type SVGPaint = ::values::generics::SVGPaint; -/// Computed SVG Paint Kind value -pub type SVGPaintKind = ::values::generics::SVGPaintKind; - -impl Default for SVGPaint { - fn default() -> Self { - SVGPaint { - kind: ::values::generics::SVGPaintKind::None, - fallback: None, - } - } -} - -impl SVGPaint { - /// Opaque black color - pub fn black() -> Self { - let rgba = RGBA::from_floats(0., 0., 0., 1.); - SVGPaint { - kind: ::values::generics::SVGPaintKind::Color(rgba), - fallback: None, - } - } -} - /// | | pub type LengthOrPercentageOrNumber = Either; diff --git a/components/style/values/computed/svg.rs b/components/style/values/computed/svg.rs new file mode 100644 index 00000000000..8b3247aad31 --- /dev/null +++ b/components/style/values/computed/svg.rs @@ -0,0 +1,33 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +//! Computed types for SVG properties. + +use values::RGBA; +use values::generics::svg as generic; + +/// Computed SVG Paint value +pub type SVGPaint = generic::SVGPaint; +/// Computed SVG Paint Kind value +pub type SVGPaintKind = generic::SVGPaintKind; + +impl Default for SVGPaint { + fn default() -> Self { + SVGPaint { + kind: generic::SVGPaintKind::None, + fallback: None, + } + } +} + +impl SVGPaint { + /// Opaque black color + pub fn black() -> Self { + let rgba = RGBA::from_floats(0., 0., 0., 1.); + SVGPaint { + kind: generic::SVGPaintKind::Color(rgba), + fallback: None, + } + } +} diff --git a/components/style/values/generics/mod.rs b/components/style/values/generics/mod.rs index 1ad56e08d53..9456f0061c5 100644 --- a/components/style/values/generics/mod.rs +++ b/components/style/values/generics/mod.rs @@ -11,7 +11,6 @@ use parser::{Parse, ParserContext}; use std::fmt; use style_traits::{Comma, OneOrMoreSeparated, ParseError, StyleParseError, ToCss}; use super::CustomIdent; -use values::specified::url::SpecifiedUrl; pub mod background; pub mod basic_shape; @@ -24,6 +23,7 @@ pub mod grid; pub mod image; pub mod position; pub mod rect; +pub mod svg; pub mod text; pub mod transform; @@ -252,90 +252,3 @@ impl ToCss for FontSettingTagFloat { self.0.to_css(dest) } } - - -/// An SVG paint value -/// -/// https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[derive(Clone, Debug, PartialEq, ToAnimatedValue, ToComputedValue, ToCss)] -pub struct SVGPaint { - /// The paint source - pub kind: SVGPaintKind, - /// The fallback color - pub fallback: Option, -} - -/// An SVG paint value without the fallback -/// -/// Whereas the spec only allows PaintServer -/// to have a fallback, Gecko lets the context -/// properties have a fallback as well. -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[derive(Clone, Debug, PartialEq, ToAnimatedValue, ToComputedValue, ToCss)] -pub enum SVGPaintKind { - /// `none` - None, - /// `` - Color(ColorType), - /// `url(...)` - PaintServer(SpecifiedUrl), - /// `context-fill` - ContextFill, - /// `context-stroke` - ContextStroke, -} - -impl SVGPaintKind { - /// Parse a keyword value only - fn parse_ident<'i, 't>(input: &mut Parser<'i, 't>) -> Result> { - try_match_ident_ignore_ascii_case! { input.expect_ident()?, - "none" => Ok(SVGPaintKind::None), - "context-fill" => Ok(SVGPaintKind::ContextFill), - "context-stroke" => Ok(SVGPaintKind::ContextStroke), - } - } -} - -/// Parse SVGPaint's fallback. -/// fallback is keyword(none) or Color. -/// https://svgwg.org/svg2-draft/painting.html#SpecifyingPaint -fn parse_fallback<'i, 't, ColorType: Parse>(context: &ParserContext, - input: &mut Parser<'i, 't>) - -> Option { - if input.try(|i| i.expect_ident_matching("none")).is_ok() { - None - } else { - input.try(|i| ColorType::parse(context, i)).ok() - } -} - -impl Parse for SVGPaint { - fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { - if let Ok(url) = input.try(|i| SpecifiedUrl::parse(context, i)) { - Ok(SVGPaint { - kind: SVGPaintKind::PaintServer(url), - fallback: parse_fallback(context, input), - }) - } else if let Ok(kind) = input.try(SVGPaintKind::parse_ident) { - if let SVGPaintKind::None = kind { - Ok(SVGPaint { - kind: kind, - fallback: None, - }) - } else { - Ok(SVGPaint { - kind: kind, - fallback: parse_fallback(context, input), - }) - } - } else if let Ok(color) = input.try(|i| ColorType::parse(context, i)) { - Ok(SVGPaint { - kind: SVGPaintKind::Color(color), - fallback: None, - }) - } else { - Err(StyleParseError::UnspecifiedError.into()) - } - } -} diff --git a/components/style/values/generics/svg.rs b/components/style/values/generics/svg.rs new file mode 100644 index 00000000000..f1a27c60903 --- /dev/null +++ b/components/style/values/generics/svg.rs @@ -0,0 +1,96 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +//! Generic types for CSS values in SVG + +use cssparser::Parser; +use parser::{Parse, ParserContext}; +use style_traits::{ParseError, StyleParseError}; +use values::specified::url::SpecifiedUrl; + +/// An SVG paint value +/// +/// https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +#[derive(Clone, Debug, PartialEq, ToAnimatedValue, ToComputedValue, ToCss)] +pub struct SVGPaint { + /// The paint source + pub kind: SVGPaintKind, + /// The fallback color + pub fallback: Option, +} + +/// An SVG paint value without the fallback +/// +/// Whereas the spec only allows PaintServer +/// to have a fallback, Gecko lets the context +/// properties have a fallback as well. +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +#[derive(Clone, Debug, PartialEq, ToAnimatedValue, ToComputedValue, ToCss)] +pub enum SVGPaintKind { + /// `none` + None, + /// `` + Color(ColorType), + /// `url(...)` + PaintServer(SpecifiedUrl), + /// `context-fill` + ContextFill, + /// `context-stroke` + ContextStroke, +} + +impl SVGPaintKind { + /// Parse a keyword value only + fn parse_ident<'i, 't>(input: &mut Parser<'i, 't>) -> Result> { + try_match_ident_ignore_ascii_case! { input.expect_ident()?, + "none" => Ok(SVGPaintKind::None), + "context-fill" => Ok(SVGPaintKind::ContextFill), + "context-stroke" => Ok(SVGPaintKind::ContextStroke), + } + } +} + +/// Parse SVGPaint's fallback. +/// fallback is keyword(none) or Color. +/// https://svgwg.org/svg2-draft/painting.html#SpecifyingPaint +fn parse_fallback<'i, 't, ColorType: Parse>(context: &ParserContext, + input: &mut Parser<'i, 't>) + -> Option { + if input.try(|i| i.expect_ident_matching("none")).is_ok() { + None + } else { + input.try(|i| ColorType::parse(context, i)).ok() + } +} + +impl Parse for SVGPaint { + fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { + if let Ok(url) = input.try(|i| SpecifiedUrl::parse(context, i)) { + Ok(SVGPaint { + kind: SVGPaintKind::PaintServer(url), + fallback: parse_fallback(context, input), + }) + } else if let Ok(kind) = input.try(SVGPaintKind::parse_ident) { + if let SVGPaintKind::None = kind { + Ok(SVGPaint { + kind: kind, + fallback: None, + }) + } else { + Ok(SVGPaint { + kind: kind, + fallback: parse_fallback(context, input), + }) + } + } else if let Ok(color) = input.try(|i| ColorType::parse(context, i)) { + Ok(SVGPaint { + kind: SVGPaintKind::Color(color), + fallback: None, + }) + } else { + Err(StyleParseError::UnspecifiedError.into()) + } + } +} diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index 7c51c3868f8..348b98e2655 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -43,6 +43,7 @@ pub use self::length::{LengthOrPercentageOrNone, MaxLength, MozLength}; pub use self::length::{NoCalcLength, Percentage, ViewportPercentageLength}; pub use self::rect::LengthOrNumberRect; pub use self::position::{Position, PositionComponent}; +pub use self::svg::{SVGPaint, SVGPaintKind}; pub use self::text::{InitialLetter, LetterSpacing, LineHeight, WordSpacing}; pub use self::transform::{TimingFunction, TransformOrigin}; pub use super::generics::grid::GridLine; @@ -64,6 +65,7 @@ pub mod image; pub mod length; pub mod position; pub mod rect; +pub mod svg; pub mod text; pub mod transform; @@ -703,14 +705,6 @@ pub type TrackList = GenericTrackList; /// ` | ` pub type GridTemplateComponent = GenericGridTemplateComponent; -no_viewport_percentage!(SVGPaint); - -/// Specified SVG Paint value -pub type SVGPaint = ::values::generics::SVGPaint; - -/// Specified SVG Paint Kind value -pub type SVGPaintKind = ::values::generics::SVGPaintKind; - /// | | pub type LengthOrPercentageOrNumber = Either; diff --git a/components/style/values/specified/svg.rs b/components/style/values/specified/svg.rs new file mode 100644 index 00000000000..2390c383c40 --- /dev/null +++ b/components/style/values/specified/svg.rs @@ -0,0 +1,16 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +//! Specified types for SVG properties. + +use values::generics::svg as generic; +use values::specified::color::RGBAColor; + +/// Specified SVG Paint value +pub type SVGPaint = generic::SVGPaint; + +no_viewport_percentage!(SVGPaint); + +/// Specified SVG Paint Kind value +pub type SVGPaintKind = generic::SVGPaintKind;