Skip to content

Commit

Permalink
stylo: Add Gecko bindings for <paint>, use for stroke/fill
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: 4QKKzJ1DVYP
  • Loading branch information
Manishearth committed Feb 18, 2017
1 parent fabc1b8 commit 51b03fb
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 14 deletions.
2 changes: 2 additions & 0 deletions components/style/build_gecko.rs
Expand Up @@ -365,6 +365,7 @@ mod bindings {
"nsStylePadding",
"nsStylePosition",
"nsStyleSVG",
"nsStyleSVGPaint",
"nsStyleSVGReset",
"nsStyleTable",
"nsStyleTableBorder",
Expand Down Expand Up @@ -568,6 +569,7 @@ mod bindings {
"nsStylePosition",
"nsStyleQuoteValues",
"nsStyleSVG",
"nsStyleSVGPaint",
"nsStyleSVGReset",
"nsStyleTable",
"nsStyleTableBorder",
Expand Down
14 changes: 14 additions & 0 deletions components/style/gecko_bindings/bindings.rs
Expand Up @@ -118,6 +118,9 @@ unsafe impl Sync for nsStyleQuoteValues {}
use gecko_bindings::structs::nsStyleSVG;
unsafe impl Send for nsStyleSVG {}
unsafe impl Sync for nsStyleSVG {}
use gecko_bindings::structs::nsStyleSVGPaint;
unsafe impl Send for nsStyleSVGPaint {}
unsafe impl Sync for nsStyleSVGPaint {}
use gecko_bindings::structs::nsStyleSVGReset;
unsafe impl Send for nsStyleSVGReset {}
unsafe impl Sync for nsStyleSVGReset {}
Expand Down Expand Up @@ -719,6 +722,17 @@ extern "C" {
pub fn Gecko_nsStyleFilter_SetURLValue(effects: *mut nsStyleFilter,
uri: ServoBundledURI);
}
extern "C" {
pub fn Gecko_nsStyleSVGPaint_CopyFrom(dest: *mut nsStyleSVGPaint,
src: *const nsStyleSVGPaint);
}
extern "C" {
pub fn Gecko_nsStyleSVGPaint_SetURLValue(paint: *mut nsStyleSVGPaint,
uri: ServoBundledURI);
}
extern "C" {
pub fn Gecko_nsStyleSVGPaint_Reset(paint: *mut nsStyleSVGPaint);
}
extern "C" {
pub fn Gecko_FillAllBackgroundLists(layers: *mut nsStyleImageLayers,
max_len: u32);
Expand Down
82 changes: 69 additions & 13 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -11,6 +11,7 @@
<%namespace name="helpers" file="/helpers.mako.rs" />

use app_units::Au;
use cssparser::Color;
use custom_properties::ComputedValuesMap;
use gecko_bindings::bindings;
% for style_struct in data.style_structs:
Expand Down Expand Up @@ -274,19 +275,27 @@ def set_gecko_property(ffi_name, expr):
}
</%def>


/// Convert a Servo color into an nscolor; with currentColor as 0
///
/// Call sites will need to be updated after https://bugzilla.mozilla.org/show_bug.cgi?id=760345
fn color_to_nscolor_zero_currentcolor(color: Color) -> structs::nscolor {
match color {
Color::RGBA(rgba) => {
convert_rgba_to_nscolor(&rgba)
},
Color::CurrentColor => 0,
}
}

<%def name="impl_color_setter(ident, gecko_ffi_name, complex_color=True)">
#[allow(unreachable_code)]
#[allow(non_snake_case)]
pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) {
% if complex_color:
let result = v.into();
% else:
use cssparser::Color;
let result = match v {
Color::RGBA(rgba) => convert_rgba_to_nscolor(&rgba),
// FIXME #13547
Color::CurrentColor => 0,
};
let result = color_to_nscolor_zero_currentcolor(v);
% endif
${set_gecko_property(gecko_ffi_name, "result")}
}
Expand All @@ -306,7 +315,6 @@ def set_gecko_property(ffi_name, expr):
% if complex_color:
${get_gecko_property(gecko_ffi_name)}.into()
% else:
use cssparser::Color;
Color::RGBA(convert_nscolor_to_rgba(${get_gecko_property(gecko_ffi_name)}))
% endif
}
Expand Down Expand Up @@ -369,6 +377,54 @@ def set_gecko_property(ffi_name, expr):
% endif
</%def>

<%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 self::structs::nsStyleSVGPaintType;

let ref mut paint = ${get_gecko_property(gecko_ffi_name)};
unsafe {
bindings::Gecko_nsStyleSVGPaint_Reset(paint);
}
let fallback = v.fallback.take();
match v.kind {
SVGPaintKind::None => return,
SVGPaintKind::ContextFill => {
paint.mType = nsStyleSVGPaintType::eStyleSVGPaintType_ContextFill;
}
SVGPaintKind::ContextStroke => {
paint.mType = nsStyleSVGPaintType::eStyleSVGPaintType_ContextStroke;
}
SVGPaintKind::PaintServer(url) => {
unsafe {
bindings::Gecko_nsStyleSVGPaint_SetURLValue(paint, url.for_ffi());
}
}
SVGPaintKind::Color(color) => {
paint.mType = nsStyleSVGPaintType::eStyleSVGPaintType_Color;
unsafe {
*paint.mPaint.mColor.as_mut() = color_to_nscolor_zero_currentcolor(color);
}
}
}

if let Some(fallback) = fallback {
paint.mFallbackColor = color_to_nscolor_zero_currentcolor(fallback);
}
}

#[allow(non_snake_case)]
pub fn copy_${ident}_from(&mut self, other: &Self) {
unsafe {
bindings::Gecko_nsStyleSVGPaint_CopyFrom(
&mut ${get_gecko_property(gecko_ffi_name)},
& ${get_gecko_property(gecko_ffi_name, "other")}
);
}
}
</%def>

<%def name="impl_app_units(ident, gecko_ffi_name, need_clone, round_to_pixels=False)">
#[allow(non_snake_case)]
pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) {
Expand Down Expand Up @@ -543,6 +599,7 @@ impl Debug for ${style_struct.gecko_struct_name} {
"Number": impl_simple,
"Opacity": impl_simple,
"CSSColor": impl_color,
"SVGPaint": impl_svg_paint,
}

def longhand_method(longhand):
Expand Down Expand Up @@ -2130,7 +2187,6 @@ fn static_assert() {
<%self:impl_trait style_struct_name="Effects"
skip_longhands="box-shadow filter">
pub fn set_box_shadow(&mut self, v: longhands::box_shadow::computed_value::T) {
use cssparser::Color;

self.gecko.mBoxShadow.replace_with_new(v.0.len() as u32);

Expand Down Expand Up @@ -2161,8 +2217,6 @@ fn static_assert() {
}

pub fn clone_box_shadow(&self) -> longhands::box_shadow::computed_value::T {
use cssparser::Color;

let buf = self.gecko.mBoxShadow.iter().map(|shadow| {
longhands::box_shadow::single_value::computed_value::T {
offset_x: Au(shadow.mXOffset),
Expand All @@ -2177,7 +2231,6 @@ fn static_assert() {
}

pub fn set_filter(&mut self, v: longhands::filter::computed_value::T) {
use cssparser::Color;
use properties::longhands::filter::computed_value::Filter::*;
use gecko_bindings::structs::nsCSSShadowArray;
use gecko_bindings::structs::nsStyleFilter;
Expand Down Expand Up @@ -2304,7 +2357,6 @@ fn static_assert() {
${impl_keyword('text_align', 'mTextAlign', text_align_keyword, need_clone=False)}

pub fn set_text_shadow(&mut self, v: longhands::text_shadow::computed_value::T) {
use cssparser::Color;
self.gecko.mTextShadow.replace_with_new(v.0.len() as u32);

for (servo, gecko_shadow) in v.0.into_iter()
Expand Down Expand Up @@ -2332,7 +2384,6 @@ fn static_assert() {
}

pub fn clone_text_shadow(&self) -> longhands::text_shadow::computed_value::T {
use cssparser::Color;

let buf = self.gecko.mTextShadow.iter().map(|shadow| {
longhands::text_shadow::computed_value::TextShadow {
Expand Down Expand Up @@ -2720,6 +2771,11 @@ clip-path
}
</%self:impl_trait>

<%self:impl_trait style_struct_name="InheritedSVG"
skip_longhands=""
skip_additionals="*">
</%self:impl_trait>

<%self:impl_trait style_struct_name="Color"
skip_longhands="*">
pub fn set_color(&mut self, v: longhands::color::computed_value::T) {
Expand Down
16 changes: 16 additions & 0 deletions components/style/properties/longhand/inherited_svg.mako.rs
Expand Up @@ -33,6 +33,14 @@ ${helpers.single_keyword("color-interpolation-filters", "auto sRGB linearRGB",
animatable=False,
spec="https://www.w3.org/TR/SVG11/painting.html#ColorInterpolationFiltersProperty")}

${helpers.predefined_type(
"fill", "SVGPaint",
"::values::computed::SVGPaint::black()",
products="gecko",
animatable=False,
boxed=True,
spec="https://www.w3.org/TR/SVG2/painting.html#SpecifyingFillPaint")}

${helpers.predefined_type("fill-opacity", "Opacity", "1.0",
products="gecko", animatable=False,
spec="https://www.w3.org/TR/SVG11/painting.html#FillOpacityProperty")}
Expand All @@ -49,6 +57,14 @@ ${helpers.single_keyword("shape-rendering",
animatable=False,
spec="https://www.w3.org/TR/SVG11/painting.html#ShapeRenderingProperty")}

${helpers.predefined_type(
"stroke", "SVGPaint",
"Default::default()",
products="gecko",
animatable=False,
boxed=True,
spec="https://www.w3.org/TR/SVG2/painting.html#SpecifyingStrokePaint")}

${helpers.single_keyword("stroke-linecap", "butt round square",
products="gecko", animatable=False,
spec="https://www.w3.org/TR/SVG11/painting.html#StrokeLinecapProperty")}
Expand Down
22 changes: 21 additions & 1 deletion components/style/values/computed/mod.rs
Expand Up @@ -10,7 +10,7 @@ use font_metrics::FontMetricsProvider;
use properties::ComputedValues;
use std::fmt;
use style_traits::ToCss;
use super::{CSSFloat, specified};
use super::{CSSFloat, RGBA, specified};

pub use cssparser::Color as CSSColor;
pub use self::image::{AngleOrCorner, EndingShape as GradientShape, Gradient, GradientKind, Image};
Expand Down Expand Up @@ -197,6 +197,26 @@ pub struct SVGPaint {
pub fallback: Option<CSSColor>,
}

impl Default for SVGPaint {
fn default() -> Self {
SVGPaint {
kind: SVGPaintKind::None,
fallback: None,
}
}
}

impl SVGPaint {
/// Opaque black color
pub fn black() -> Self {
let rgba = RGBA::from_floats(0., 0., 0., 1.);
SVGPaint {
kind: SVGPaintKind::Color(CSSColor::RGBA(rgba)),
fallback: None,
}
}
}

/// An SVG paint value without the fallback
///
/// Whereas the spec only allows PaintServer
Expand Down
2 changes: 2 additions & 0 deletions components/style/values/specified/mod.rs
Expand Up @@ -670,6 +670,8 @@ impl Shadow {
}
}

no_viewport_percentage!(SVGPaint);

/// An SVG paint value
///
/// https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint
Expand Down

0 comments on commit 51b03fb

Please sign in to comment.