From ab03688994515b81312c2472f2a2c65bbccaf366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 11 Feb 2020 22:03:53 +0000 Subject: [PATCH] style: Use cbindgen for shape-outside and clip-path. Differential Revision: https://phabricator.services.mozilla.com/D62372 --- components/style/gecko/conversions.rs | 153 ---------------------- components/style/properties/gecko.mako.rs | 82 +----------- 2 files changed, 1 insertion(+), 234 deletions(-) diff --git a/components/style/gecko/conversions.rs b/components/style/gecko/conversions.rs index 3a061c97c53..8fbe3f47540 100644 --- a/components/style/gecko/conversions.rs +++ b/components/style/gecko/conversions.rs @@ -15,159 +15,6 @@ use crate::stylesheets::RulesMutateError; use crate::values::computed::transform::Matrix3D; use crate::values::computed::TextAlign; -pub mod basic_shape { - //! Conversions from and to CSS shape representations. - use crate::gecko_bindings::structs::{ - StyleGeometryBox, StyleShapeSource, StyleShapeSourceType, - }; - use crate::values::computed::basic_shape::{BasicShape, ClippingShape, FloatAreaShape}; - use crate::values::computed::motion::OffsetPath; - use crate::values::generics::basic_shape::{ShapeGeometryBox, Path, ShapeBox, ShapeSource}; - use crate::values::specified::SVGPathData; - - impl StyleShapeSource { - /// Convert StyleShapeSource to ShapeSource except URL and Image - /// types. - fn to_shape_source( - &self, - ) -> Option> - where - ReferenceBox: From + Default + PartialEq, - { - match self.mType { - StyleShapeSourceType::None => Some(ShapeSource::None), - StyleShapeSourceType::Box => Some(ShapeSource::Box(self.mReferenceBox.into())), - StyleShapeSourceType::Shape => { - let other_shape = unsafe { &*self.__bindgen_anon_1.mBasicShape.as_ref().mPtr }; - let shape = Box::new(other_shape.clone()); - let reference_box = self.mReferenceBox.into(); - Some(ShapeSource::Shape(shape, reference_box)) - }, - StyleShapeSourceType::Image => None, - StyleShapeSourceType::Path => { - let path = self.to_svg_path().expect("expect an SVGPathData"); - let fill = unsafe { &*self.__bindgen_anon_1.mSVGPath.as_ref().mPtr }.mFillRule; - Some(ShapeSource::Path(Path { fill, path })) - }, - } - } - - /// Generate a SVGPathData from StyleShapeSource if possible. - fn to_svg_path(&self) -> Option { - match self.mType { - StyleShapeSourceType::Path => { - let gecko_path = unsafe { &*self.__bindgen_anon_1.mSVGPath.as_ref().mPtr }; - Some(SVGPathData(gecko_path.mPath.clone())) - }, - _ => None, - } - } - } - - impl<'a> From<&'a StyleShapeSource> for ClippingShape { - fn from(other: &'a StyleShapeSource) -> Self { - match other.mType { - StyleShapeSourceType::Image => unsafe { - use crate::values::generics::image::Image as GenericImage; - - let shape_image = &*other.__bindgen_anon_1.mShapeImage.as_ref().mPtr; - match *shape_image { - GenericImage::Url(ref url) => ShapeSource::ImageOrUrl(url.0.clone()), - _ => panic!("ClippingShape doesn't support non-url images"), - } - }, - _ => other - .to_shape_source() - .expect("Couldn't convert to StyleSource!"), - } - } - } - - impl<'a> From<&'a StyleShapeSource> for FloatAreaShape { - fn from(other: &'a StyleShapeSource) -> Self { - match other.mType { - StyleShapeSourceType::Image => unsafe { - let shape_image = &*other.__bindgen_anon_1.mShapeImage.as_ref().mPtr; - ShapeSource::ImageOrUrl(shape_image.clone()) - }, - _ => other - .to_shape_source() - .expect("Couldn't convert to StyleSource!"), - } - } - } - - impl<'a> From<&'a StyleShapeSource> for OffsetPath { - fn from(other: &'a StyleShapeSource) -> Self { - use crate::values::generics::motion::GenericOffsetPath; - match other.mType { - StyleShapeSourceType::Path => GenericOffsetPath::Path( - other.to_svg_path().expect("Cannot convert to SVGPathData"), - ), - StyleShapeSourceType::None => OffsetPath::none(), - StyleShapeSourceType::Shape | - StyleShapeSourceType::Box | - StyleShapeSourceType::Image => unreachable!("Unsupported offset-path type"), - } - } - } - - impl From for StyleGeometryBox { - fn from(reference: ShapeBox) -> Self { - use crate::gecko_bindings::structs::StyleGeometryBox::*; - match reference { - ShapeBox::ContentBox => ContentBox, - ShapeBox::PaddingBox => PaddingBox, - ShapeBox::BorderBox => BorderBox, - ShapeBox::MarginBox => MarginBox, - } - } - } - - impl From for StyleGeometryBox { - fn from(reference: ShapeGeometryBox) -> Self { - use crate::gecko_bindings::structs::StyleGeometryBox::*; - match reference { - ShapeGeometryBox::ShapeBox(shape_box) => From::from(shape_box), - ShapeGeometryBox::FillBox => FillBox, - ShapeGeometryBox::StrokeBox => StrokeBox, - ShapeGeometryBox::ViewBox => ViewBox, - ShapeGeometryBox::ElementDependent => NoBox, - } - } - } - - impl From for ShapeGeometryBox { - fn from(reference: StyleGeometryBox) -> Self { - use crate::gecko_bindings::structs::StyleGeometryBox::*; - match reference { - ContentBox => ShapeGeometryBox::ShapeBox(ShapeBox::ContentBox), - PaddingBox => ShapeGeometryBox::ShapeBox(ShapeBox::PaddingBox), - BorderBox => ShapeGeometryBox::ShapeBox(ShapeBox::BorderBox), - MarginBox => ShapeGeometryBox::ShapeBox(ShapeBox::MarginBox), - FillBox => ShapeGeometryBox::FillBox, - StrokeBox => ShapeGeometryBox::StrokeBox, - ViewBox => ShapeGeometryBox::ViewBox, - NoBox => ShapeGeometryBox::ElementDependent, - NoClip | Text | MozAlmostPadding => unreachable!(), - } - } - } - - impl From for ShapeBox { - fn from(reference: StyleGeometryBox) -> Self { - use crate::gecko_bindings::structs::StyleGeometryBox::*; - match reference { - ContentBox => ShapeBox::ContentBox, - PaddingBox => ShapeBox::PaddingBox, - BorderBox => ShapeBox::BorderBox, - MarginBox => ShapeBox::MarginBox, - _ => panic!("Unexpected StyleGeometryBox while converting to ShapeBox"), - } - } - } -} - impl From for nsresult { fn from(other: RulesMutateError) -> Self { match other { diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index ba959c65f15..400f27fa44b 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -41,7 +41,6 @@ use std::mem::{forget, MaybeUninit}; use std::{cmp, ops, ptr}; use crate::values::{self, CustomIdent, Either, KeyframesName, None_}; use crate::values::computed::{Percentage, TransitionProperty}; -use crate::values::computed::url::ComputedImageUrl; use crate::values::computed::BorderStyle; use crate::values::computed::font::FontSize; use crate::values::generics::column::ColumnCount; @@ -1433,7 +1432,7 @@ fn static_assert() { animation-iteration-count animation-timing-function clear transition-duration transition-delay transition-timing-function transition-property - shape-outside -webkit-line-clamp""" %> + -webkit-line-clamp""" %> <%self:impl_trait style_struct_name="Box" skip_longhands="${skip_box_longhands}"> #[inline] pub fn set_display(&mut self, v: longhands::display::computed_value::T) { @@ -1693,8 +1692,6 @@ fn static_assert() { ${impl_animation_timing_function()} - <% impl_shape_source("shape_outside", "mShapeOutside") %> - #[allow(non_snake_case)] pub fn set__webkit_line_clamp(&mut self, v: longhands::_webkit_line_clamp::computed_value::T) { self.gecko.mLineClamp = match v { @@ -2202,84 +2199,8 @@ fn static_assert() { } -// Set SVGPathData to StyleShapeSource. -fn set_style_svg_path( - shape_source: &mut structs::mozilla::StyleShapeSource, - servo_path: values::specified::svg_path::SVGPathData, - fill: values::generics::basic_shape::FillRule, -) { - // Setup path. - unsafe { - bindings::Gecko_SetToSVGPath( - shape_source, - servo_path.0.forget(), - fill, - ); - } -} - -<%def name="impl_shape_source(ident, gecko_ffi_name)"> - pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) { - use crate::values::generics::basic_shape::ShapeSource; - use crate::gecko_bindings::structs::StyleShapeSourceType; - - let ref mut ${ident} = self.gecko.${gecko_ffi_name}; - - // clean up existing struct. - unsafe { bindings::Gecko_DestroyShapeSource(${ident}) }; - - ${ident}.mType = StyleShapeSourceType::None; - - match v { - ShapeSource::None => {} // don't change the type - ShapeSource::ImageOrUrl(image) => { - % if ident == "clip_path": - use crate::values::generics::image::Image; - - let image = Image::Url(ComputedImageUrl(image)); - % endif - unsafe { - bindings::Gecko_NewShapeImage(${ident}); - let style_image = &mut *${ident}.__bindgen_anon_1.mShapeImage.as_mut().mPtr; - *style_image = image; - } - } - ShapeSource::Box(reference) => { - ${ident}.mReferenceBox = reference.into(); - ${ident}.mType = StyleShapeSourceType::Box; - } - ShapeSource::Path(p) => set_style_svg_path(${ident}, p.path, p.fill), - ShapeSource::Shape(servo_shape, reference_box) => { - unsafe { - ${ident}.__bindgen_anon_1.mBasicShape.as_mut().mPtr = - Box::into_raw(servo_shape); - } - ${ident}.mReferenceBox = reference_box.into(); - ${ident}.mType = StyleShapeSourceType::Shape; - } - } - - } - - pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { - (&self.gecko.${gecko_ffi_name}).into() - } - - pub fn copy_${ident}_from(&mut self, other: &Self) { - use crate::gecko_bindings::bindings::Gecko_CopyShapeSourceFrom; - unsafe { - Gecko_CopyShapeSourceFrom(&mut self.gecko.${gecko_ffi_name}, &other.gecko.${gecko_ffi_name}); - } - } - - pub fn reset_${ident}(&mut self, other: &Self) { - self.copy_${ident}_from(other) - } - - <% skip_svg_longhands = """ mask-mode mask-repeat mask-clip mask-origin mask-composite mask-position-x mask-position-y mask-size mask-image -clip-path """ %> <%self:impl_trait style_struct_name="SVG" @@ -2288,7 +2209,6 @@ clip-path <% impl_common_image_layer_properties("mask") %> <% impl_simple_image_array_property("mode", "mask", "mMask", "mMaskMode", "SVG") %> <% impl_simple_image_array_property("composite", "mask", "mMask", "mComposite", "SVG") %> - <% impl_shape_source("clip_path", "mClipPath") %> <%self:impl_trait style_struct_name="InheritedSVG"