Auto merge of #17177 - Manishearth:generic-paint, r=upsuper

Make SVGPaint generic

r=xidorn https://bugzilla.mozilla.org/show_bug.cgi?id=1369277

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17177)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-06-05 21:17:57 -07:00 committed by GitHub
commit 6b78a514f8
4 changed files with 148 additions and 178 deletions

View file

@ -416,7 +416,7 @@ fn color_to_nscolor_zero_currentcolor(color: Color) -> structs::nscolor {
<%def name="impl_svg_paint(ident, gecko_ffi_name, need_clone=False, complex_color=True)">
#[allow(non_snake_case)]
pub fn set_${ident}(&mut self, mut v: longhands::${ident}::computed_value::T) {
use values::computed::SVGPaintKind;
use values::generics::SVGPaintKind;
use self::structs::nsStyleSVGPaintType;
use self::structs::nsStyleSVGFallbackType;

View file

@ -532,23 +532,15 @@ impl IntegerOrAuto {
}
}
/// 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 {
/// The paint source
pub kind: SVGPaintKind,
/// The fallback color
pub fallback: Option<CSSColor>,
}
/// Computed SVG Paint value
pub type SVGPaint = ::values::generics::SVGPaint<CSSColor>;
/// Computed SVG Paint Kind value
pub type SVGPaintKind = ::values::generics::SVGPaintKind<CSSColor>;
impl Default for SVGPaint {
fn default() -> Self {
SVGPaint {
kind: SVGPaintKind::None,
kind: ::values::generics::SVGPaintKind::None,
fallback: None,
}
}
@ -559,54 +551,12 @@ impl SVGPaint {
pub fn black() -> Self {
let rgba = RGBA::from_floats(0., 0., 0., 1.);
SVGPaint {
kind: SVGPaintKind::Color(CSSColor::RGBA(rgba)),
kind: ::values::generics::SVGPaintKind::Color(CSSColor::RGBA(rgba)),
fallback: None,
}
}
}
/// 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 {
/// `none`
None,
/// `<color>`
Color(CSSColor),
/// `url(...)`
PaintServer(SpecifiedUrl),
/// `context-fill`
ContextFill,
/// `context-stroke`
ContextStroke,
}
impl ToCss for SVGPaintKind {
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 ToCss for SVGPaint {
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(())
}
}
/// <length> | <percentage> | <number>
pub type LengthOrPercentageOrNumber = Either<Number, LengthOrPercentage>;

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(())
}
}

View file

@ -933,103 +933,11 @@ impl Shadow {
no_viewport_percentage!(SVGPaint);
/// 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 {
/// The paint source
pub kind: SVGPaintKind,
/// The fallback color
pub fallback: Option<CSSColor>,
}
/// 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 {
/// `none`
None,
/// `<color>`
Color(CSSColor),
/// `url(...)`
PaintServer(SpecifiedUrl),
/// `context-fill`
ContextFill,
/// `context-stroke`
ContextStroke,
}
impl SVGPaintKind {
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 Parse for SVGPaint {
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| CSSColor::parse(context, i));
Ok(SVGPaint {
kind: SVGPaintKind::PaintServer(url),
fallback: fallback.ok(),
})
} else if let Ok(kind) = input.try(SVGPaintKind::parse_ident) {
if kind == SVGPaintKind::None {
Ok(SVGPaint {
kind: kind,
fallback: None,
})
} else {
let fallback = input.try(|i| CSSColor::parse(context, i));
Ok(SVGPaint {
kind: kind,
fallback: fallback.ok(),
})
}
} else if let Ok(color) = input.try(|i| CSSColor::parse(context, i)) {
Ok(SVGPaint {
kind: SVGPaintKind::Color(color),
fallback: None,
})
} else {
Err(())
}
}
}
impl ToCss for SVGPaintKind {
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 ToCss for SVGPaint {
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(())
}
}
/// Specified SVG Paint value
pub type SVGPaint = ::values::generics::SVGPaint<CSSColor>;
/// Specified SVG Paint Kind value
pub type SVGPaintKind = ::values::generics::SVGPaintKind<CSSColor>;
impl ToComputedValue for SVGPaint {
type ComputedValue = super::computed::SVGPaint;
@ -1056,36 +964,17 @@ impl ToComputedValue for SVGPaintKind {
#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self {
SVGPaintKind::None => super::computed::SVGPaintKind::None,
SVGPaintKind::ContextStroke => super::computed::SVGPaintKind::ContextStroke,
SVGPaintKind::ContextFill => super::computed::SVGPaintKind::ContextFill,
SVGPaintKind::Color(ref color) => {
let color = match color.parsed {
Color::CurrentColor => cssparser::Color::RGBA(context.style().get_color().clone_color()),
_ => color.to_computed_value(context),
};
super::computed::SVGPaintKind::Color(color)
self.convert(|color| {
match color.parsed {
Color::CurrentColor => cssparser::Color::RGBA(context.style().get_color().clone_color()),
_ => color.to_computed_value(context),
}
SVGPaintKind::PaintServer(ref server) => {
super::computed::SVGPaintKind::PaintServer(server.to_computed_value(context))
}
}
})
}
#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
match *computed {
super::computed::SVGPaintKind::None => SVGPaintKind::None,
super::computed::SVGPaintKind::ContextStroke => SVGPaintKind::ContextStroke,
super::computed::SVGPaintKind::ContextFill => SVGPaintKind::ContextFill,
super::computed::SVGPaintKind::Color(ref color) => {
SVGPaintKind::Color(ToComputedValue::from_computed_value(color))
}
super::computed::SVGPaintKind::PaintServer(ref server) => {
SVGPaintKind::PaintServer(ToComputedValue::from_computed_value(server))
}
}
computed.convert(ToComputedValue::from_computed_value)
}
}