Stylo - support image-orientation property

First, we need to make Servo's image-orientation parser to be agreed with Gecko's.
Numbers without any AngleUnit, including unitless 0 angle, should be invalid
for image-orientation. However, rotate() and skew() for transform properties
accept unitless 0 angle. In order to make all these properties work properly,
I fixed Angle::parse() to match Gecko. For the existing users of Angle::parse(),
I create Angle::parse_with_unitless() and use it as an alternative for them.
Once https://github.com/w3c/csswg-drafts/issues/1162 is resolved, we shall be
able to use an unified version of Angle::parse() then.

The parser of image-orientation is also fixed to report parsing errors on
empty string.

Then, with the newly added binding functions support in Gecko side, we shall
reuse the same methods from Gecko to pass the computed value from Servo to Gecko.

Gecko bug: Bug 1341758
This commit is contained in:
Jeremy Chen 2017-04-06 16:11:36 +08:00
parent b5a003246e
commit 5332d4b265
5 changed files with 66 additions and 21 deletions

View file

@ -2776,6 +2776,34 @@ fn static_assert() {
} }
</%self:impl_trait> </%self:impl_trait>
<%self:impl_trait style_struct_name="InheritedBox"
skip_longhands="image-orientation">
// FIXME: Gecko uses a tricky way to store computed value of image-orientation
// within an u8. We could inline following glue codes by implementing all
// those tricky parts for Servo as well. But, it's not done yet just for
// convenience.
pub fn set_image_orientation(&mut self, v: longhands::image_orientation::computed_value::T) {
use properties::longhands::image_orientation::computed_value::T;
match v {
T::FromImage => {
unsafe {
bindings::Gecko_SetImageOrientationAsFromImage(&mut self.gecko);
}
},
T::AngleWithFlipped(ref angle, flipped) => {
unsafe {
bindings::Gecko_SetImageOrientation(&mut self.gecko, angle.radians() as f64, flipped);
}
}
}
}
pub fn copy_image_orientation_from(&mut self, other: &Self) {
unsafe {
bindings::Gecko_CopyImageOrientationFrom(&mut self.gecko, &other.gecko);
}
}
</%self:impl_trait>
<%self:impl_trait style_struct_name="InheritedTable" <%self:impl_trait style_struct_name="InheritedTable"
skip_longhands="border-spacing"> skip_longhands="border-spacing">
@ -2789,7 +2817,6 @@ fn static_assert() {
self.gecko.mBorderSpacingCol = other.gecko.mBorderSpacingCol; self.gecko.mBorderSpacingCol = other.gecko.mBorderSpacingCol;
self.gecko.mBorderSpacingRow = other.gecko.mBorderSpacingRow; self.gecko.mBorderSpacingRow = other.gecko.mBorderSpacingRow;
} }
</%self:impl_trait> </%self:impl_trait>

View file

@ -1332,6 +1332,7 @@ ${helpers.predefined_type("scroll-snap-coordinate",
computed_value::T(None) computed_value::T(None)
} }
// Allow unitless zero angle for rotate() and skew() to align with gecko
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> { pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
if input.try(|input| input.expect_ident_matching("none")).is_ok() { if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(SpecifiedValue(Vec::new())) return Ok(SpecifiedValue(Vec::new()))
@ -1469,28 +1470,28 @@ ${helpers.predefined_type("scroll-snap-coordinate",
}, },
"rotate" => { "rotate" => {
try!(input.parse_nested_block(|input| { try!(input.parse_nested_block(|input| {
let theta = try!(specified::Angle::parse(context,input)); let theta = try!(specified::Angle::parse_with_unitless(context,input));
result.push(SpecifiedOperation::Rotate(theta)); result.push(SpecifiedOperation::Rotate(theta));
Ok(()) Ok(())
})) }))
}, },
"rotatex" => { "rotatex" => {
try!(input.parse_nested_block(|input| { try!(input.parse_nested_block(|input| {
let theta = try!(specified::Angle::parse(context,input)); let theta = try!(specified::Angle::parse_with_unitless(context,input));
result.push(SpecifiedOperation::RotateX(theta)); result.push(SpecifiedOperation::RotateX(theta));
Ok(()) Ok(())
})) }))
}, },
"rotatey" => { "rotatey" => {
try!(input.parse_nested_block(|input| { try!(input.parse_nested_block(|input| {
let theta = try!(specified::Angle::parse(context,input)); let theta = try!(specified::Angle::parse_with_unitless(context,input));
result.push(SpecifiedOperation::RotateY(theta)); result.push(SpecifiedOperation::RotateY(theta));
Ok(()) Ok(())
})) }))
}, },
"rotatez" => { "rotatez" => {
try!(input.parse_nested_block(|input| { try!(input.parse_nested_block(|input| {
let theta = try!(specified::Angle::parse(context,input)); let theta = try!(specified::Angle::parse_with_unitless(context,input));
result.push(SpecifiedOperation::RotateZ(theta)); result.push(SpecifiedOperation::RotateZ(theta));
Ok(()) Ok(())
})) }))
@ -1503,7 +1504,7 @@ ${helpers.predefined_type("scroll-snap-coordinate",
try!(input.expect_comma()); try!(input.expect_comma());
let az = try!(specified::parse_number(input)); let az = try!(specified::parse_number(input));
try!(input.expect_comma()); try!(input.expect_comma());
let theta = try!(specified::Angle::parse(context,input)); let theta = try!(specified::Angle::parse_with_unitless(context,input));
// TODO(gw): Check the axis can be normalized!! // TODO(gw): Check the axis can be normalized!!
result.push(SpecifiedOperation::Rotate3D(ax, ay, az, theta)); result.push(SpecifiedOperation::Rotate3D(ax, ay, az, theta));
Ok(()) Ok(())
@ -1511,9 +1512,9 @@ ${helpers.predefined_type("scroll-snap-coordinate",
}, },
"skew" => { "skew" => {
try!(input.parse_nested_block(|input| { try!(input.parse_nested_block(|input| {
let theta_x = try!(specified::Angle::parse(context, input)); let theta_x = try!(specified::Angle::parse_with_unitless(context, input));
if input.try(|input| input.expect_comma()).is_ok() { if input.try(|input| input.expect_comma()).is_ok() {
let theta_y = try!(specified::Angle::parse(context, input)); let theta_y = try!(specified::Angle::parse_with_unitless(context, input));
result.push(SpecifiedOperation::Skew(theta_x, Some(theta_y))); result.push(SpecifiedOperation::Skew(theta_x, Some(theta_y)));
} else { } else {
result.push(SpecifiedOperation::Skew(theta_x, None)); result.push(SpecifiedOperation::Skew(theta_x, None));
@ -1523,14 +1524,14 @@ ${helpers.predefined_type("scroll-snap-coordinate",
}, },
"skewx" => { "skewx" => {
try!(input.parse_nested_block(|input| { try!(input.parse_nested_block(|input| {
let theta_x = try!(specified::Angle::parse(context,input)); let theta_x = try!(specified::Angle::parse_with_unitless(context,input));
result.push(SpecifiedOperation::SkewX(theta_x)); result.push(SpecifiedOperation::SkewX(theta_x));
Ok(()) Ok(())
})) }))
}, },
"skewy" => { "skewy" => {
try!(input.parse_nested_block(|input| { try!(input.parse_nested_block(|input| {
let theta_y = try!(specified::Angle::parse(context,input)); let theta_y = try!(specified::Angle::parse_with_unitless(context,input));
result.push(SpecifiedOperation::SkewY(theta_y)); result.push(SpecifiedOperation::SkewY(theta_y));
Ok(()) Ok(())
})) }))

View file

@ -83,7 +83,7 @@ ${helpers.single_keyword("image-rendering",
// Image Orientation // Image Orientation
<%helpers:longhand name="image-orientation" <%helpers:longhand name="image-orientation"
products="None" products="gecko"
animation_type="none" animation_type="none"
spec="https://drafts.csswg.org/css-images/#propdef-image-orientation, \ spec="https://drafts.csswg.org/css-images/#propdef-image-orientation, \
/// additional values in https://developer.mozilla.org/en-US/docs/Web/CSS/image-orientation"> /// additional values in https://developer.mozilla.org/en-US/docs/Web/CSS/image-orientation">
@ -199,21 +199,23 @@ ${helpers.single_keyword("image-rendering",
} }
} }
// from-image | <angle> | [<angle>? flip]
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> { pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if input.try(|input| input.expect_ident_matching("from-image")).is_ok() { if input.try(|input| input.expect_ident_matching("from-image")).is_ok() {
// Handle from-image // Handle from-image
Ok(SpecifiedValue { angle: None, flipped: false }) Ok(SpecifiedValue { angle: None, flipped: false })
} else if input.try(|input| input.expect_ident_matching("flip")).is_ok() {
// Handle flip
Ok(SpecifiedValue { angle: Some(Angle::zero()), flipped: true })
} else { } else {
// Handle <angle> | <angle>? flip // Handle <angle> | <angle> flip
let angle = input.try(|input| Angle::parse(context, input)).ok(); let angle = input.try(|input| Angle::parse(context, input)).ok();
let flipped = input.try(|input| input.expect_ident_matching("flip")).is_ok(); if angle.is_none() {
let explicit_angle = if angle.is_none() && !flipped { return Err(());
Some(Angle::zero()) }
} else {
angle
};
Ok(SpecifiedValue { angle: explicit_angle, flipped: flipped }) let flipped = input.try(|input| input.expect_ident_matching("flip")).is_ok();
Ok(SpecifiedValue { angle: angle, flipped: flipped })
} }
} }
</%helpers:longhand> </%helpers:longhand>

View file

@ -430,7 +430,6 @@ impl Parse for Angle {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> { fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
match try!(input.next()) { match try!(input.next()) {
Token::Dimension(ref value, ref unit) => Angle::parse_dimension(value.value, unit), Token::Dimension(ref value, ref unit) => Angle::parse_dimension(value.value, unit),
Token::Number(ref value) if value.value == 0. => Ok(Angle::zero()),
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => { Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
input.parse_nested_block(CalcLengthOrPercentage::parse_angle) input.parse_nested_block(CalcLengthOrPercentage::parse_angle)
}, },
@ -451,6 +450,22 @@ impl Angle {
}; };
Ok(angle) Ok(angle)
} }
/// Parse an angle, including unitless 0 degree.
/// Note that numbers without any AngleUnit, including unitless 0
/// angle, should be invalid. However, some properties still accept
/// unitless 0 angle and stores it as '0deg'. We can remove this and
/// get back to the unified version Angle::parse once
/// https://github.com/w3c/csswg-drafts/issues/1162 is resolved.
pub fn parse_with_unitless(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
match try!(input.next()) {
Token::Dimension(ref value, ref unit) => Angle::parse_dimension(value.value, unit),
Token::Number(ref value) if value.value == 0. => Ok(Angle::zero()),
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
input.parse_nested_block(CalcLengthOrPercentage::parse_angle)
},
_ => Err(())
}
}
} }
#[allow(missing_docs)] #[allow(missing_docs)]

View file

@ -17,7 +17,7 @@ fn image_orientation_longhand_should_parse_properly() {
assert_eq!(from_image, SpecifiedValue { angle: None, flipped: false }); assert_eq!(from_image, SpecifiedValue { angle: None, flipped: false });
let flip = parse_longhand!(image_orientation, "flip"); let flip = parse_longhand!(image_orientation, "flip");
assert_eq!(flip, SpecifiedValue { angle: None, flipped: true }); assert_eq!(flip, SpecifiedValue { angle: Some(Angle::from_degrees(0.0)), flipped: true });
let zero = parse_longhand!(image_orientation, "0deg"); let zero = parse_longhand!(image_orientation, "0deg");
assert_eq!(zero, SpecifiedValue { angle: Some(Angle::from_degrees(0.0)), flipped: false }); assert_eq!(zero, SpecifiedValue { angle: Some(Angle::from_degrees(0.0)), flipped: false });