mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Handle clip-path in stylo
This commit is contained in:
parent
1d32422fe8
commit
3895b7118e
12 changed files with 432 additions and 49 deletions
|
@ -79,3 +79,182 @@ impl From<nsStyleCoord_CalcValue> for LengthOrPercentage {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod basic_shape {
|
||||
use euclid::size::Size2D;
|
||||
use gecko_bindings::structs::StyleClipPathGeometryBox;
|
||||
use gecko_bindings::structs::{StyleBasicShape, StyleBasicShapeType, StyleFillRule};
|
||||
use gecko_bindings::structs::{nsStyleCoord, nsStyleCorners, nsStyleImageLayers_Position};
|
||||
use gecko_bindings::sugar::ns_style_coord::{CoordDataMut, CoordDataValue};
|
||||
use gecko_values::GeckoStyleCoordConvertible;
|
||||
use std::borrow::Borrow;
|
||||
use values::computed::basic_shape::*;
|
||||
use values::computed::position::Position;
|
||||
use values::computed::{BorderRadiusSize, LengthOrPercentage};
|
||||
|
||||
// using Borrow so that we can have a non-moving .into()
|
||||
impl<T: Borrow<StyleBasicShape>> From<T> for BasicShape {
|
||||
fn from(other: T) -> Self {
|
||||
let other = other.borrow();
|
||||
match other.mType {
|
||||
StyleBasicShapeType::Inset => {
|
||||
let t = LengthOrPercentage::from_gecko_style_coord(&other.mCoordinates[0]);
|
||||
let r = LengthOrPercentage::from_gecko_style_coord(&other.mCoordinates[1]);
|
||||
let b = LengthOrPercentage::from_gecko_style_coord(&other.mCoordinates[2]);
|
||||
let l = LengthOrPercentage::from_gecko_style_coord(&other.mCoordinates[3]);
|
||||
let round = (&other.mRadius).into();
|
||||
BasicShape::Inset(InsetRect {
|
||||
top: t.expect("inset() offset should be a length, percentage, or calc value"),
|
||||
right: r.expect("inset() offset should be a length, percentage, or calc value"),
|
||||
bottom: b.expect("inset() offset should be a length, percentage, or calc value"),
|
||||
left: l.expect("inset() offset should be a length, percentage, or calc value"),
|
||||
round: Some(round),
|
||||
})
|
||||
}
|
||||
StyleBasicShapeType::Circle => {
|
||||
BasicShape::Circle(Circle {
|
||||
radius: (&other.mCoordinates[0]).into(),
|
||||
position: (&other.mPosition).into()
|
||||
})
|
||||
}
|
||||
StyleBasicShapeType::Ellipse => {
|
||||
BasicShape::Ellipse(Ellipse {
|
||||
semiaxis_x: (&other.mCoordinates[0]).into(),
|
||||
semiaxis_y: (&other.mCoordinates[1]).into(),
|
||||
position: (&other.mPosition).into()
|
||||
})
|
||||
}
|
||||
StyleBasicShapeType::Polygon => {
|
||||
let fill_rule = if other.mFillRule == StyleFillRule::Evenodd {
|
||||
FillRule::EvenOdd
|
||||
} else {
|
||||
FillRule::NonZero
|
||||
};
|
||||
let mut coords = Vec::with_capacity(other.mCoordinates.len() / 2);
|
||||
for i in 0..(other.mCoordinates.len() / 2) {
|
||||
let x = 2 * i;
|
||||
let y = x + 1;
|
||||
coords.push((LengthOrPercentage::from_gecko_style_coord(&other.mCoordinates[x])
|
||||
.expect("polygon() coordinate should be a length, percentage, or calc value"),
|
||||
LengthOrPercentage::from_gecko_style_coord(&other.mCoordinates[y])
|
||||
.expect("polygon() coordinate should be a length, percentage, or calc value")
|
||||
))
|
||||
}
|
||||
BasicShape::Polygon(Polygon {
|
||||
fill: fill_rule,
|
||||
coordinates: coords,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Borrow<nsStyleCorners>> From<T> for BorderRadius {
|
||||
fn from(other: T) -> Self {
|
||||
let other = other.borrow();
|
||||
let get_corner = |index| {
|
||||
BorderRadiusSize(Size2D::new(
|
||||
LengthOrPercentage::from_gecko_style_coord(&other.data_at(index))
|
||||
.expect("<border-radius> should be a length, percentage, or calc value"),
|
||||
LengthOrPercentage::from_gecko_style_coord(&other.data_at(index + 1))
|
||||
.expect("<border-radius> should be a length, percentage, or calc value")))
|
||||
};
|
||||
|
||||
BorderRadius {
|
||||
top_left: get_corner(0),
|
||||
top_right: get_corner(2),
|
||||
bottom_right: get_corner(4),
|
||||
bottom_left: get_corner(6),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Can't be a From impl since we need to set an existing
|
||||
// nsStyleCorners, not create a new one
|
||||
impl BorderRadius {
|
||||
pub fn set_corners(&self, other: &mut nsStyleCorners) {
|
||||
let mut set_corner = |field: &BorderRadiusSize, index| {
|
||||
field.0.width.to_gecko_style_coord(&mut other.data_at_mut(index));
|
||||
field.0.height.to_gecko_style_coord(&mut other.data_at_mut(index + 1));
|
||||
};
|
||||
set_corner(&self.top_left, 0);
|
||||
set_corner(&self.top_right, 2);
|
||||
set_corner(&self.bottom_right, 4);
|
||||
set_corner(&self.bottom_left, 6);
|
||||
}
|
||||
}
|
||||
|
||||
/// We use None for a nonexistant radius, but Gecko uses (0 0 0 0 / 0 0 0 0)
|
||||
pub fn set_corners_from_radius(radius: Option<BorderRadius>, other: &mut nsStyleCorners) {
|
||||
if let Some(radius) = radius {
|
||||
radius.set_corners(other);
|
||||
} else {
|
||||
for i in 0..8 {
|
||||
other.data_at_mut(i).set_value(CoordDataValue::Coord(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Can't be a From impl since we need to set an existing
|
||||
// nsStyleImageLayers_Position, not create a new one
|
||||
impl From<Position> for nsStyleImageLayers_Position {
|
||||
fn from(other: Position) -> Self {
|
||||
nsStyleImageLayers_Position {
|
||||
mXPosition: other.horizontal.into(),
|
||||
mYPosition: other.vertical.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Borrow<nsStyleCoord>> From<T> for ShapeRadius {
|
||||
fn from(other: T) -> Self {
|
||||
let other = other.borrow();
|
||||
ShapeRadius::from_gecko_style_coord(other)
|
||||
.expect("<shape-radius> should be a length, percentage, calc, or keyword value")
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Borrow<nsStyleImageLayers_Position>> From<T> for Position {
|
||||
fn from(other: T) -> Self {
|
||||
let other = other.borrow();
|
||||
Position {
|
||||
horizontal: other.mXPosition.into(),
|
||||
vertical: other.mYPosition.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GeometryBox> for StyleClipPathGeometryBox {
|
||||
fn from(reference: GeometryBox) -> Self {
|
||||
use gecko_bindings::structs::StyleClipPathGeometryBox::*;
|
||||
match reference {
|
||||
GeometryBox::ShapeBox(ShapeBox::Content) => Content,
|
||||
GeometryBox::ShapeBox(ShapeBox::Padding) => Padding,
|
||||
GeometryBox::ShapeBox(ShapeBox::Border) => Border,
|
||||
GeometryBox::ShapeBox(ShapeBox::Margin) => Margin,
|
||||
GeometryBox::Fill => Fill,
|
||||
GeometryBox::Stroke => Stroke,
|
||||
GeometryBox::View => View,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Will panic on NoBox
|
||||
// Ideally these would be implemented on Option<T>,
|
||||
// but coherence doesn't like that and TryFrom isn't stable
|
||||
impl From<StyleClipPathGeometryBox> for GeometryBox {
|
||||
fn from(reference: StyleClipPathGeometryBox) -> Self {
|
||||
use gecko_bindings::structs::StyleClipPathGeometryBox::*;
|
||||
match reference {
|
||||
NoBox => panic!("Shouldn't convert NoBox to GeometryBox"),
|
||||
Content => GeometryBox::ShapeBox(ShapeBox::Content),
|
||||
Padding => GeometryBox::ShapeBox(ShapeBox::Padding),
|
||||
Border => GeometryBox::ShapeBox(ShapeBox::Border),
|
||||
Margin => GeometryBox::ShapeBox(ShapeBox::Margin),
|
||||
Fill => GeometryBox::Fill,
|
||||
Stroke => GeometryBox::Stroke,
|
||||
View => GeometryBox::View,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,12 +7,13 @@
|
|||
use app_units::Au;
|
||||
use cssparser::RGBA;
|
||||
use gecko_bindings::structs::nsStyleCoord;
|
||||
use gecko_bindings::structs::{NS_RADIUS_CLOSEST_SIDE, NS_RADIUS_FARTHEST_SIDE};
|
||||
use gecko_bindings::sugar::ns_style_coord::{CoordDataValue, CoordData, CoordDataMut};
|
||||
use std::cmp::max;
|
||||
use values::computed::Angle;
|
||||
use values::computed::basic_shape::ShapeRadius;
|
||||
use values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto, LengthOrPercentageOrNone};
|
||||
|
||||
|
||||
pub trait StyleCoordHelpers {
|
||||
fn set<T: GeckoStyleCoordConvertible>(&mut self, val: T);
|
||||
}
|
||||
|
@ -94,6 +95,28 @@ impl GeckoStyleCoordConvertible for LengthOrPercentageOrNone {
|
|||
}
|
||||
}
|
||||
|
||||
impl GeckoStyleCoordConvertible for ShapeRadius {
|
||||
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
|
||||
match *self {
|
||||
ShapeRadius::ClosestSide => {
|
||||
coord.set_value(CoordDataValue::Enumerated(NS_RADIUS_CLOSEST_SIDE))
|
||||
}
|
||||
ShapeRadius::FarthestSide => {
|
||||
coord.set_value(CoordDataValue::Enumerated(NS_RADIUS_FARTHEST_SIDE))
|
||||
}
|
||||
ShapeRadius::Length(lop) => lop.to_gecko_style_coord(coord),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
|
||||
match coord.as_value() {
|
||||
CoordDataValue::Enumerated(NS_RADIUS_CLOSEST_SIDE) => Some(ShapeRadius::ClosestSide),
|
||||
CoordDataValue::Enumerated(NS_RADIUS_FARTHEST_SIDE) => Some(ShapeRadius::FarthestSide),
|
||||
_ => LengthOrPercentage::from_gecko_style_coord(coord).map(ShapeRadius::Length),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: GeckoStyleCoordConvertible> GeckoStyleCoordConvertible for Option<T> {
|
||||
fn to_gecko_style_coord<U: CoordDataMut>(&self, coord: &mut U) {
|
||||
if let Some(ref me) = *self {
|
||||
|
|
|
@ -1265,7 +1265,7 @@ fn static_assert() {
|
|||
</%self:impl_trait>
|
||||
|
||||
<%self:impl_trait style_struct_name="SVG"
|
||||
skip_longhands="flood-color lighting-color stop-color"
|
||||
skip_longhands="flood-color lighting-color stop-color clip-path"
|
||||
skip_additionals="*">
|
||||
|
||||
<% impl_color("flood_color", "mFloodColor") %>
|
||||
|
@ -1274,6 +1274,140 @@ fn static_assert() {
|
|||
|
||||
<% impl_color("stop_color", "mStopColor") %>
|
||||
|
||||
pub fn set_clip_path(&mut self, v: longhands::clip_path::computed_value::T) {
|
||||
use gecko_bindings::bindings::{Gecko_NewBasicShape, Gecko_DestroyClipPath};
|
||||
use gecko_bindings::structs::StyleClipPathGeometryBox;
|
||||
use gecko_bindings::structs::{StyleBasicShape, StyleBasicShapeType, StyleShapeSourceType};
|
||||
use gecko_bindings::structs::{StyleClipPath, StyleFillRule};
|
||||
use gecko_conversions::basic_shape::set_corners_from_radius;
|
||||
use gecko_values::GeckoStyleCoordConvertible;
|
||||
use values::computed::basic_shape::*;
|
||||
let ref mut clip_path = self.gecko.mClipPath;
|
||||
// clean up existing struct
|
||||
unsafe { Gecko_DestroyClipPath(clip_path) };
|
||||
|
||||
clip_path.mType = StyleShapeSourceType::None_;
|
||||
|
||||
match v {
|
||||
ShapeSource::Url(..) => println!("stylo: clip-path: url() not yet implemented"),
|
||||
ShapeSource::None => {} // don't change the type
|
||||
ShapeSource::Box(reference) => {
|
||||
clip_path.mReferenceBox = reference.into();
|
||||
clip_path.mType = StyleShapeSourceType::Box;
|
||||
}
|
||||
ShapeSource::Shape(servo_shape, maybe_box) => {
|
||||
clip_path.mReferenceBox = maybe_box.map(Into::into)
|
||||
.unwrap_or(StyleClipPathGeometryBox::NoBox);
|
||||
clip_path.mType = StyleShapeSourceType::Shape;
|
||||
|
||||
fn init_shape(clip_path: &mut StyleClipPath, ty: StyleBasicShapeType) -> &mut StyleBasicShape {
|
||||
unsafe {
|
||||
// We have to be very careful to avoid a copy here!
|
||||
let ref mut union = clip_path.StyleShapeSource_nsStyleStruct_h_unnamed_26;
|
||||
let mut shape: &mut *mut StyleBasicShape = union.mBasicShape.as_mut();
|
||||
*shape = Gecko_NewBasicShape(ty);
|
||||
&mut **shape
|
||||
}
|
||||
}
|
||||
match servo_shape {
|
||||
BasicShape::Inset(rect) => {
|
||||
let mut shape = init_shape(clip_path, StyleBasicShapeType::Inset);
|
||||
unsafe { shape.mCoordinates.set_len(4) };
|
||||
|
||||
// set_len() can't call constructors, so the coordinates
|
||||
// can contain any value. set_value() attempts to free
|
||||
// allocated coordinates, so we don't want to feed it
|
||||
// garbage values which it may misinterpret.
|
||||
// Instead, we use leaky_set_value to blindly overwrite
|
||||
// the garbage data without
|
||||
// attempting to clean up.
|
||||
shape.mCoordinates[0].leaky_set_null();
|
||||
rect.top.to_gecko_style_coord(&mut shape.mCoordinates[0]);
|
||||
shape.mCoordinates[1].leaky_set_null();
|
||||
rect.right.to_gecko_style_coord(&mut shape.mCoordinates[1]);
|
||||
shape.mCoordinates[2].leaky_set_null();
|
||||
rect.bottom.to_gecko_style_coord(&mut shape.mCoordinates[2]);
|
||||
shape.mCoordinates[3].leaky_set_null();
|
||||
rect.left.to_gecko_style_coord(&mut shape.mCoordinates[3]);
|
||||
|
||||
set_corners_from_radius(rect.round, &mut shape.mRadius);
|
||||
}
|
||||
BasicShape::Circle(circ) => {
|
||||
let mut shape = init_shape(clip_path, StyleBasicShapeType::Circle);
|
||||
unsafe { shape.mCoordinates.set_len(1) };
|
||||
shape.mCoordinates[0].leaky_set_null();
|
||||
circ.radius.to_gecko_style_coord(&mut shape.mCoordinates[0]);
|
||||
|
||||
shape.mPosition = circ.position.into();
|
||||
}
|
||||
BasicShape::Ellipse(el) => {
|
||||
let mut shape = init_shape(clip_path, StyleBasicShapeType::Ellipse);
|
||||
unsafe { shape.mCoordinates.set_len(2) };
|
||||
shape.mCoordinates[0].leaky_set_null();
|
||||
el.semiaxis_x.to_gecko_style_coord(&mut shape.mCoordinates[0]);
|
||||
shape.mCoordinates[1].leaky_set_null();
|
||||
el.semiaxis_y.to_gecko_style_coord(&mut shape.mCoordinates[1]);
|
||||
|
||||
shape.mPosition = el.position.into();
|
||||
}
|
||||
BasicShape::Polygon(poly) => {
|
||||
let mut shape = init_shape(clip_path, StyleBasicShapeType::Polygon);
|
||||
unsafe {
|
||||
shape.mCoordinates.set_len(poly.coordinates.len() as u32 * 2);
|
||||
}
|
||||
for (i, coord) in poly.coordinates.iter().enumerate() {
|
||||
shape.mCoordinates[2 * i].leaky_set_null();
|
||||
shape.mCoordinates[2 * i + 1].leaky_set_null();
|
||||
coord.0.to_gecko_style_coord(&mut shape.mCoordinates[2 * i]);
|
||||
coord.1.to_gecko_style_coord(&mut shape.mCoordinates[2 * i + 1]);
|
||||
}
|
||||
shape.mFillRule = if poly.fill == FillRule::EvenOdd {
|
||||
StyleFillRule::Evenodd
|
||||
} else {
|
||||
StyleFillRule::Nonzero
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn copy_clip_path_from(&mut self, other: &Self) {
|
||||
use gecko_bindings::bindings::Gecko_CopyClipPathValueFrom;
|
||||
unsafe {
|
||||
Gecko_CopyClipPathValueFrom(&mut self.gecko.mClipPath, &other.gecko.mClipPath);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clone_clip_path(&self) -> longhands::clip_path::computed_value::T {
|
||||
use gecko_bindings::structs::StyleShapeSourceType;
|
||||
use gecko_bindings::structs::StyleClipPathGeometryBox;
|
||||
use values::computed::basic_shape::*;
|
||||
let ref clip_path = self.gecko.mClipPath;
|
||||
|
||||
match clip_path.mType {
|
||||
StyleShapeSourceType::None_ => ShapeSource::None,
|
||||
StyleShapeSourceType::Box => {
|
||||
ShapeSource::Box(clip_path.mReferenceBox.into())
|
||||
}
|
||||
StyleShapeSourceType::URL => {
|
||||
warn!("stylo: clip-path: url() not implemented yet");
|
||||
Default::default()
|
||||
}
|
||||
StyleShapeSourceType::Shape => {
|
||||
let reference = if let StyleClipPathGeometryBox::NoBox = clip_path.mReferenceBox {
|
||||
None
|
||||
} else {
|
||||
Some(clip_path.mReferenceBox.into())
|
||||
};
|
||||
let union = clip_path.StyleShapeSource_nsStyleStruct_h_unnamed_26;
|
||||
let shape = unsafe { &**union.mBasicShape.as_ref() };
|
||||
ShapeSource::Shape(shape.into(), reference)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</%self:impl_trait>
|
||||
|
||||
<%self:impl_trait style_struct_name="Color"
|
||||
|
|
|
@ -11,9 +11,9 @@ use cssparser::ToCss;
|
|||
use properties::shorthands::serialize_four_sides;
|
||||
use std::fmt;
|
||||
use url::Url;
|
||||
use values::computed::UrlExtraData;
|
||||
use values::computed::position::Position;
|
||||
use values::computed::{BorderRadiusSize, LengthOrPercentage};
|
||||
use values::computed::UrlExtraData;
|
||||
|
||||
pub use values::specified::basic_shape::{FillRule, GeometryBox, ShapeBox};
|
||||
|
||||
|
|
|
@ -14,9 +14,9 @@ use std::fmt;
|
|||
use url::Url;
|
||||
use values::computed::basic_shape as computed_basic_shape;
|
||||
use values::computed::{Context, ToComputedValue, ComputedValueAsSpecified};
|
||||
use values::specified::UrlExtraData;
|
||||
use values::specified::position::Position;
|
||||
use values::specified::{BorderRadiusSize, LengthOrPercentage, Percentage};
|
||||
use values::specified::UrlExtraData;
|
||||
|
||||
/// A shape source, for some reference box
|
||||
///
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue