Skip to content

Commit

Permalink
Derive ToComputedValue for SVGPaint
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Jul 20, 2017
1 parent 20a3b02 commit 1eb51da
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 65 deletions.
33 changes: 2 additions & 31 deletions components/style/values/generics/mod.rs
Expand Up @@ -255,7 +255,7 @@ impl ToCss for FontSettingTagFloat {
///
/// https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToAnimatedValue)]
#[derive(Clone, Debug, PartialEq, ToAnimatedValue, ToComputedValue)]
pub struct SVGPaint<ColorType> {
/// The paint source
pub kind: SVGPaintKind<ColorType>,
Expand All @@ -269,7 +269,7 @@ pub struct SVGPaint<ColorType> {
/// 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, ToCss)]
#[derive(Clone, Debug, PartialEq, ToAnimatedValue, ToComputedValue, ToCss)]
pub enum SVGPaintKind<ColorType> {
/// `none`
None,
Expand All @@ -283,35 +283,6 @@ pub enum SVGPaintKind<ColorType> {
ContextStroke,
}

impl<ColorType> SVGPaintKind<ColorType> {
/// Convert to a value with a different kind of color
pub fn convert<F, OtherColor>(&self, f: F) -> SVGPaintKind<OtherColor>
where F: Fn(&ColorType) -> OtherColor {
match *self {
SVGPaintKind::None => SVGPaintKind::None,
SVGPaintKind::ContextStroke => SVGPaintKind::ContextStroke,
SVGPaintKind::ContextFill => SVGPaintKind::ContextFill,
SVGPaintKind::Color(ref color) => {
SVGPaintKind::Color(f(color))
}
SVGPaintKind::PaintServer(ref server) => {
SVGPaintKind::PaintServer(server.clone())
}
}
}
}

impl<ColorType> SVGPaint<ColorType> {
/// Convert to a value with a different kind of color
pub fn convert<F, OtherColor>(&self, f: F) -> SVGPaint<OtherColor>
where F: Fn(&ColorType) -> OtherColor {
SVGPaint {
kind: self.kind.convert(&f),
fallback: self.fallback.as_ref().map(|color| f(color))
}
}
}

impl<ColorType> SVGPaintKind<ColorType> {
/// Parse a keyword value only
fn parse_ident<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
Expand Down
34 changes: 0 additions & 34 deletions components/style/values/specified/mod.rs
Expand Up @@ -701,40 +701,6 @@ pub type SVGPaint = ::values::generics::SVGPaint<RGBAColor>;
/// Specified SVG Paint Kind value
pub type SVGPaintKind = ::values::generics::SVGPaintKind<RGBAColor>;

impl ToComputedValue for SVGPaint {
type ComputedValue = super::computed::SVGPaint;

#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
super::computed::SVGPaint {
kind: self.kind.to_computed_value(context),
fallback: self.fallback.as_ref().map(|f| f.to_computed_value(context))
}
}

#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
SVGPaint {
kind: ToComputedValue::from_computed_value(&computed.kind),
fallback: computed.fallback.as_ref().map(ToComputedValue::from_computed_value)
}
}
}

impl ToComputedValue for SVGPaintKind {
type ComputedValue = super::computed::SVGPaintKind;

#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
self.convert(|color| color.to_computed_value(context))
}

#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
computed.convert(ToComputedValue::from_computed_value)
}
}

/// <length> | <percentage> | <number>
pub type LengthOrPercentageOrNumber = Either<Number, LengthOrPercentage>;

Expand Down

0 comments on commit 1eb51da

Please sign in to comment.