Skip to content

Commit

Permalink
style: Support shape-image: <image>
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: GSCZIMEpCS2
  • Loading branch information
aethanyc committed Nov 26, 2017
1 parent b8b5c53 commit f3dbaad
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 27 deletions.
68 changes: 52 additions & 16 deletions components/style/gecko/conversions.rs
Expand Up @@ -596,7 +596,7 @@ pub mod basic_shape {
use gecko_bindings::sugar::ns_style_coord::{CoordDataMut, CoordDataValue};
use std::borrow::Borrow;
use values::computed::ComputedUrl;
use values::computed::basic_shape::{BasicShape, ShapeRadius};
use values::computed::basic_shape::{BasicShape, ClippingShape, FloatAreaShape, ShapeRadius};
use values::computed::border::{BorderCornerRadius, BorderRadius};
use values::computed::length::LengthOrPercentage;
use values::computed::position;
Expand All @@ -606,32 +606,68 @@ pub mod basic_shape {
use values::generics::border::BorderRadius as GenericBorderRadius;
use values::generics::rect::Rect;

impl<'a, ReferenceBox> From<&'a StyleShapeSource> for ShapeSource<BasicShape, ReferenceBox, ComputedUrl>
where
ReferenceBox: From<StyleGeometryBox>,
impl StyleShapeSource {
/// Convert StyleShapeSource to ShapeSource except URL and Image
/// types.
fn into_shape_source<ReferenceBox, ImageOrUrl>(
&self
) -> Option<ShapeSource<BasicShape, ReferenceBox, ImageOrUrl>>
where
ReferenceBox: From<StyleGeometryBox>
{
match self.mType {
StyleShapeSourceType::None => Some(ShapeSource::None),
StyleShapeSourceType::Box => Some(ShapeSource::Box(self.mReferenceBox.into())),
StyleShapeSourceType::Shape => {
let other_shape = unsafe { &*self.mBasicShape.mPtr };
let shape = other_shape.into();
let reference_box = if self.mReferenceBox == StyleGeometryBox::NoBox {
None
} else {
Some(self.mReferenceBox.into())
};
Some(ShapeSource::Shape(shape, reference_box))
},
StyleShapeSourceType::URL | StyleShapeSourceType::Image => None,
}
}
}

impl<'a> From<&'a StyleShapeSource> for ClippingShape
{
fn from(other: &'a StyleShapeSource) -> Self {
match other.mType {
StyleShapeSourceType::None => ShapeSource::None,
StyleShapeSourceType::Box => ShapeSource::Box(other.mReferenceBox.into()),
StyleShapeSourceType::URL => {
unsafe {
let shape_image = &*other.mShapeImage.mPtr;
let other_url = &(**shape_image.__bindgen_anon_1.mURLValue.as_ref());
let url = ComputedUrl::from_url_value_data(&other_url._base).unwrap();
ShapeSource::Url(url)
ShapeSource::ImageOrUrl(url)
}
},
StyleShapeSourceType::Shape => {
let other_shape = unsafe { &*other.mBasicShape.mPtr };
let shape = other_shape.into();
let reference_box = if other.mReferenceBox == StyleGeometryBox::NoBox {
None
} else {
Some(other.mReferenceBox.into())
};
ShapeSource::Shape(shape, reference_box)
StyleShapeSourceType::Image => {
unreachable!("ClippingShape doesn't support Image!");
}
_ => other.into_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::URL => {
unreachable!("FloatAreaShape doesn't support URL!");
},
StyleShapeSourceType::Image => {
unsafe {
let shape_image = &*other.mShapeImage.mPtr;
let image = shape_image.into_image().expect("Cannot convert to Image");
ShapeSource::ImageOrUrl(image)
}
}
_ => other.into_shape_source().expect("Couldn't convert to StyleSource!")
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -5008,18 +5008,32 @@ fn static_assert() {
use gecko::conversions::basic_shape::set_corners_from_radius;
use gecko::values::GeckoStyleCoordConvertible;
use values::generics::basic_shape::{BasicShape, FillRule, ShapeSource};

let ref mut ${ident} = self.gecko.${gecko_ffi_name};

// clean up existing struct
unsafe { Gecko_DestroyShapeSource(${ident}) };

${ident}.mType = StyleShapeSourceType::None;

match v {
ShapeSource::Url(ref url) => {
% if ident == "clip_path":
ShapeSource::ImageOrUrl(ref url) => {
unsafe {
bindings::Gecko_StyleShapeSource_SetURLValue(${ident}, url.for_ffi())
}
}
% elif ident == "shape_outside":
ShapeSource::ImageOrUrl(image) => {
unsafe {
bindings::Gecko_NewShapeImage(${ident});
let style_image = &mut *${ident}.mShapeImage.mPtr;
style_image.set(image);
}
}
% else:
<% raise Exception("Unknown property: %s" % ident) %>
}
% endif
ShapeSource::None => {} // don't change the type
ShapeSource::Box(reference) => {
${ident}.mReferenceBox = reference.into();
Expand Down
4 changes: 2 additions & 2 deletions components/style/values/computed/basic_shape.rs
Expand Up @@ -9,7 +9,7 @@

use std::fmt;
use style_traits::ToCss;
use values::computed::{LengthOrPercentage, ComputedUrl};
use values::computed::{LengthOrPercentage, ComputedUrl, Image};
use values::generics::basic_shape::{BasicShape as GenericBasicShape};
use values::generics::basic_shape::{Circle as GenericCircle, ClippingShape as GenericClippingShape};
use values::generics::basic_shape::{Ellipse as GenericEllipse, FloatAreaShape as GenericFloatAreaShape};
Expand All @@ -19,7 +19,7 @@ use values::generics::basic_shape::{InsetRect as GenericInsetRect, ShapeRadius a
pub type ClippingShape = GenericClippingShape<BasicShape, ComputedUrl>;

/// A computed float area shape.
pub type FloatAreaShape = GenericFloatAreaShape<BasicShape, ComputedUrl>;
pub type FloatAreaShape = GenericFloatAreaShape<BasicShape, Image>;

/// A computed basic shape.
pub type BasicShape = GenericBasicShape<LengthOrPercentage, LengthOrPercentage, LengthOrPercentage>;
Expand Down
6 changes: 3 additions & 3 deletions components/style/values/generics/basic_shape.rs
Expand Up @@ -27,7 +27,7 @@ pub enum GeometryBox {
}

/// A float area shape, for `shape-outside`.
pub type FloatAreaShape<BasicShape, Url> = ShapeSource<BasicShape, ShapeBox, Url>;
pub type FloatAreaShape<BasicShape, Image> = ShapeSource<BasicShape, ShapeBox, Image>;

// https://drafts.csswg.org/css-shapes-1/#typedef-shape-box
define_css_keyword_enum!(ShapeBox:
Expand All @@ -41,9 +41,9 @@ add_impls_for_keyword_enum!(ShapeBox);
/// A shape source, for some reference box.
#[allow(missing_docs)]
#[derive(Animate, Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToCss)]
pub enum ShapeSource<BasicShape, ReferenceBox, Url> {
pub enum ShapeSource<BasicShape, ReferenceBox, ImageOrUrl> {
#[animation(error)]
Url(Url),
ImageOrUrl(ImageOrUrl),
Shape(
BasicShape,
#[animation(constant)]
Expand Down
13 changes: 9 additions & 4 deletions components/style/values/specified/basic_shape.rs
Expand Up @@ -22,14 +22,15 @@ use values::generics::basic_shape::{Polygon as GenericPolygon, ShapeRadius as Ge
use values::generics::rect::Rect;
use values::specified::LengthOrPercentage;
use values::specified::border::BorderRadius;
use values::specified::image::Image;
use values::specified::position::{HorizontalPosition, Position, PositionComponent, Side, VerticalPosition};
use values::specified::url::SpecifiedUrl;

/// A specified clipping shape.
pub type ClippingShape = GenericClippingShape<BasicShape, SpecifiedUrl>;

/// A specified float area shape.
pub type FloatAreaShape = GenericFloatAreaShape<BasicShape, SpecifiedUrl>;
pub type FloatAreaShape = GenericFloatAreaShape<BasicShape, Image>;

/// A specified basic shape.
pub type BasicShape = GenericBasicShape<HorizontalPosition, VerticalPosition, LengthOrPercentage>;
Expand All @@ -49,14 +50,18 @@ pub type ShapeRadius = GenericShapeRadius<LengthOrPercentage>;
/// The specified value of `Polygon`
pub type Polygon = GenericPolygon<LengthOrPercentage>;

impl<ReferenceBox: Parse> Parse for ShapeSource<BasicShape, ReferenceBox, SpecifiedUrl> {
impl<ReferenceBox, ImageOrUrl> Parse for ShapeSource<BasicShape, ReferenceBox, ImageOrUrl>
where
ReferenceBox: Parse,
ImageOrUrl: Parse,
{
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
if input.try(|i| i.expect_ident_matching("none")).is_ok() {
return Ok(ShapeSource::None)
}

if let Ok(url) = input.try(|i| SpecifiedUrl::parse(context, i)) {
return Ok(ShapeSource::Url(url))
if let Ok(image_or_url) = input.try(|i| ImageOrUrl::parse(context, i)) {
return Ok(ShapeSource::ImageOrUrl(image_or_url))
}

fn parse_component<U: Parse>(context: &ParserContext, input: &mut Parser,
Expand Down

0 comments on commit f3dbaad

Please sign in to comment.