Make SVGPaint generic

This commit is contained in:
Manish Goregaokar 2017-06-05 16:50:16 -07:00 committed by Manish Goregaokar
parent 9987cb159f
commit 74f3284c69
4 changed files with 148 additions and 178 deletions

View file

@ -11,6 +11,7 @@ use parser::{Parse, ParserContext};
use std::fmt;
use style_traits::{OneOrMoreCommaSeparated, ToCss};
use super::CustomIdent;
use values::specified::url::SpecifiedUrl;
pub mod background;
pub mod basic_shape;
@ -268,3 +269,133 @@ impl ToCss for FontSettingTagFloat {
self.0.to_css(dest)
}
}
/// An SVG paint value
///
/// https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct SVGPaint<ColorType> {
/// The paint source
pub kind: SVGPaintKind<ColorType>,
/// The fallback color
pub fallback: Option<ColorType>,
}
/// 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.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum SVGPaintKind<ColorType> {
/// `none`
None,
/// `<color>`
Color(ColorType),
/// `url(...)`
PaintServer(SpecifiedUrl),
/// `context-fill`
ContextFill,
/// `context-stroke`
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(input: &mut Parser) -> Result<Self, ()> {
Ok(match_ignore_ascii_case! { &input.expect_ident()?,
"none" => SVGPaintKind::None,
"context-fill" => SVGPaintKind::ContextFill,
"context-stroke" => SVGPaintKind::ContextStroke,
_ => return Err(())
})
}
}
impl<ColorType: Parse> Parse for SVGPaint<ColorType> {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if let Ok(url) = input.try(|i| SpecifiedUrl::parse(context, i)) {
let fallback = input.try(|i| ColorType::parse(context, i));
Ok(SVGPaint {
kind: SVGPaintKind::PaintServer(url),
fallback: fallback.ok(),
})
} else if let Ok(kind) = input.try(SVGPaintKind::parse_ident) {
if let SVGPaintKind::None = kind {
Ok(SVGPaint {
kind: kind,
fallback: None,
})
} else {
let fallback = input.try(|i| ColorType::parse(context, i));
Ok(SVGPaint {
kind: kind,
fallback: fallback.ok(),
})
}
} else if let Ok(color) = input.try(|i| ColorType::parse(context, i)) {
Ok(SVGPaint {
kind: SVGPaintKind::Color(color),
fallback: None,
})
} else {
Err(())
}
}
}
impl<ColorType: ToCss> ToCss for SVGPaintKind<ColorType> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
SVGPaintKind::None => dest.write_str("none"),
SVGPaintKind::ContextStroke => dest.write_str("context-stroke"),
SVGPaintKind::ContextFill => dest.write_str("context-fill"),
SVGPaintKind::Color(ref color) => color.to_css(dest),
SVGPaintKind::PaintServer(ref server) => server.to_css(dest),
}
}
}
impl<ColorType: ToCss> ToCss for SVGPaint<ColorType> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.kind.to_css(dest)?;
if let Some(ref fallback) = self.fallback {
fallback.to_css(dest)?;
}
Ok(())
}
}