Replace old transform code with new generic code

This commit is contained in:
Manish Goregaokar 2017-10-04 10:12:18 -07:00
parent 5ce2966bda
commit 6631594e28
No known key found for this signature in database
GPG key ID: 3BBF4D3E2EF79F98
10 changed files with 210 additions and 1268 deletions

View file

@ -571,533 +571,28 @@ ${helpers.predefined_type(
animation_value_type="ComputedValue"
flags="CREATES_STACKING_CONTEXT FIXPOS_CB"
spec="https://drafts.csswg.org/css-transforms/#propdef-transform">
use values::computed::{LengthOrPercentageOrNumber as ComputedLoPoNumber, LengthOrNumber as ComputedLoN};
use values::computed::{LengthOrPercentage as ComputedLoP, Length as ComputedLength};
use values::generics::transform::Matrix;
use values::specified::{Angle, Integer, Length, LengthOrPercentage};
use values::specified::{LengthOrNumber, LengthOrPercentageOrNumber as LoPoNumber, Number};
use style_traits::ToCss;
use values::generics::transform::Transform;
use std::fmt;
pub mod computed_value {
use values::CSSFloat;
use values::computed;
use values::computed::{Length, LengthOrPercentage};
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq)]
pub struct ComputedMatrix {
pub m11: CSSFloat, pub m12: CSSFloat, pub m13: CSSFloat, pub m14: CSSFloat,
pub m21: CSSFloat, pub m22: CSSFloat, pub m23: CSSFloat, pub m24: CSSFloat,
pub m31: CSSFloat, pub m32: CSSFloat, pub m33: CSSFloat, pub m34: CSSFloat,
pub m41: CSSFloat, pub m42: CSSFloat, pub m43: CSSFloat, pub m44: CSSFloat,
}
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq)]
pub struct ComputedMatrixWithPercents {
pub m11: CSSFloat, pub m12: CSSFloat, pub m13: CSSFloat, pub m14: CSSFloat,
pub m21: CSSFloat, pub m22: CSSFloat, pub m23: CSSFloat, pub m24: CSSFloat,
pub m31: CSSFloat, pub m32: CSSFloat, pub m33: CSSFloat, pub m34: CSSFloat,
pub m41: LengthOrPercentage, pub m42: LengthOrPercentage,
pub m43: Length, pub m44: CSSFloat,
}
impl ComputedMatrix {
pub fn identity() -> ComputedMatrix {
ComputedMatrix {
m11: 1.0, m12: 0.0, m13: 0.0, m14: 0.0,
m21: 0.0, m22: 1.0, m23: 0.0, m24: 0.0,
m31: 0.0, m32: 0.0, m33: 1.0, m34: 0.0,
m41: 0.0, m42: 0.0, m43: 0.0, m44: 1.0
}
}
}
impl ComputedMatrixWithPercents {
pub fn identity() -> ComputedMatrixWithPercents {
ComputedMatrixWithPercents {
m11: 1.0, m12: 0.0, m13: 0.0, m14: 0.0,
m21: 0.0, m22: 1.0, m23: 0.0, m24: 0.0,
m31: 0.0, m32: 0.0, m33: 1.0, m34: 0.0,
m41: LengthOrPercentage::zero(), m42: LengthOrPercentage::zero(),
m43: Length::new(0.), m44: 1.0
}
}
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
pub enum ComputedOperation {
Matrix(ComputedMatrix),
// For `-moz-transform` matrix and matrix3d.
MatrixWithPercents(ComputedMatrixWithPercents),
Skew(computed::Angle, computed::Angle),
Translate(computed::LengthOrPercentage,
computed::LengthOrPercentage,
computed::Length),
Scale(CSSFloat, CSSFloat, CSSFloat),
Rotate(CSSFloat, CSSFloat, CSSFloat, computed::Angle),
Perspective(computed::Length),
// For mismatched transform lists.
// A vector of |ComputedOperation| could contain an |InterpolateMatrix| and other
// |ComputedOperation|s, and multiple nested |InterpolateMatrix|s is acceptable.
// e.g.
// [ InterpolateMatrix { from_list: [ InterpolateMatrix { ... },
// Scale(...) ],
// to_list: [ AccumulateMatrix { from_list: ...,
// to_list: [ InterpolateMatrix,
// ... ],
// count: ... } ],
// progress: ... } ]
InterpolateMatrix { from_list: T,
to_list: T,
progress: computed::Percentage },
// For accumulate operation of mismatched transform lists.
AccumulateMatrix { from_list: T,
to_list: T,
count: computed::Integer },
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
pub struct T(pub Option<Vec<ComputedOperation>>);
pub use values::computed::transform::Transform as T;
pub use values::computed::transform::TransformOperation as ComputedOperation;
}
/// Describes a single parsed
/// [Transform Function](https://drafts.csswg.org/css-transforms/#typedef-transform-function).
///
/// Multiple transform functions compose a transformation.
///
/// Some transformations can be expressed by other more general functions.
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
pub enum SpecifiedOperation {
/// Represents a 2D 2x3 matrix.
Matrix(Matrix<Number>),
/// Represents a 3D 4x4 matrix with percentage and length values.
/// For `moz-transform`.
PrefixedMatrix(Matrix<Number, LoPoNumber>),
/// Represents a 3D 4x4 matrix.
Matrix3D {
m11: Number, m12: Number, m13: Number, m14: Number,
m21: Number, m22: Number, m23: Number, m24: Number,
m31: Number, m32: Number, m33: Number, m34: Number,
m41: Number, m42: Number, m43: Number, m44: Number,
},
/// Represents a 3D 4x4 matrix with percentage and length values.
/// For `moz-transform`.
PrefixedMatrix3D {
m11: Number, m12: Number, m13: Number, m14: Number,
m21: Number, m22: Number, m23: Number, m24: Number,
m31: Number, m32: Number, m33: Number, m34: Number,
m41: LoPoNumber, m42: LoPoNumber, m43: LengthOrNumber, m44: Number,
},
/// A 2D skew.
///
/// If the second angle is not provided it is assumed zero.
Skew(Angle, Option<Angle>),
SkewX(Angle),
SkewY(Angle),
Translate(LengthOrPercentage, Option<LengthOrPercentage>),
TranslateX(LengthOrPercentage),
TranslateY(LengthOrPercentage),
TranslateZ(Length),
Translate3D(LengthOrPercentage, LengthOrPercentage, Length),
/// A 2D scaling factor.
///
/// `scale(2)` is parsed as `Scale(Number::new(2.0), None)` and is equivalent to
/// writing `scale(2, 2)` (`Scale(Number::new(2.0), Some(Number::new(2.0)))`).
///
/// Negative values are allowed and flip the element.
Scale(Number, Option<Number>),
ScaleX(Number),
ScaleY(Number),
ScaleZ(Number),
Scale3D(Number, Number, Number),
/// Describes a 2D Rotation.
///
/// In a 3D scene `rotate(angle)` is equivalent to `rotateZ(angle)`.
Rotate(Angle),
/// Rotation in 3D space around the x-axis.
RotateX(Angle),
/// Rotation in 3D space around the y-axis.
RotateY(Angle),
/// Rotation in 3D space around the z-axis.
RotateZ(Angle),
/// Rotation in 3D space.
///
/// Generalization of rotateX, rotateY and rotateZ.
Rotate3D(Number, Number, Number, Angle),
/// Specifies a perspective projection matrix.
///
/// Part of CSS Transform Module Level 2 and defined at
/// [§ 13.1. 3D Transform Function](https://drafts.csswg.org/css-transforms-2/#funcdef-perspective).
///
/// The value must be greater than or equal to zero.
Perspective(specified::Length),
/// A intermediate type for interpolation of mismatched transform lists.
InterpolateMatrix { from_list: SpecifiedValue,
to_list: SpecifiedValue,
progress: computed::Percentage },
/// A intermediate type for accumulation of mismatched transform lists.
AccumulateMatrix { from_list: SpecifiedValue,
to_list: SpecifiedValue,
count: Integer },
}
impl ToCss for computed_value::T {
fn to_css<W>(&self, _: &mut W) -> fmt::Result where W: fmt::Write {
// TODO(pcwalton)
Ok(())
}
}
impl ToCss for SpecifiedOperation {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
SpecifiedOperation::Matrix(ref m) => m.to_css(dest),
SpecifiedOperation::PrefixedMatrix(ref m) => m.to_css(dest),
SpecifiedOperation::Matrix3D {
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44,
} => {
serialize_function!(dest, matrix3d(
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44,
))
}
SpecifiedOperation::PrefixedMatrix3D {
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
ref m41, ref m42, ref m43, m44,
} => {
serialize_function!(dest, matrix3d(
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44,
))
}
SpecifiedOperation::Skew(ax, None) => {
serialize_function!(dest, skew(ax))
}
SpecifiedOperation::Skew(ax, Some(ay)) => {
serialize_function!(dest, skew(ax, ay))
}
SpecifiedOperation::SkewX(angle) => {
serialize_function!(dest, skewX(angle))
}
SpecifiedOperation::SkewY(angle) => {
serialize_function!(dest, skewY(angle))
}
SpecifiedOperation::Translate(ref tx, None) => {
serialize_function!(dest, translate(tx))
}
SpecifiedOperation::Translate(ref tx, Some(ref ty)) => {
serialize_function!(dest, translate(tx, ty))
}
SpecifiedOperation::TranslateX(ref tx) => {
serialize_function!(dest, translateX(tx))
}
SpecifiedOperation::TranslateY(ref ty) => {
serialize_function!(dest, translateY(ty))
}
SpecifiedOperation::TranslateZ(ref tz) => {
serialize_function!(dest, translateZ(tz))
}
SpecifiedOperation::Translate3D(ref tx, ref ty, ref tz) => {
serialize_function!(dest, translate3d(tx, ty, tz))
}
SpecifiedOperation::Scale(factor, None) => {
serialize_function!(dest, scale(factor))
}
SpecifiedOperation::Scale(sx, Some(sy)) => {
serialize_function!(dest, scale(sx, sy))
}
SpecifiedOperation::ScaleX(sx) => {
serialize_function!(dest, scaleX(sx))
}
SpecifiedOperation::ScaleY(sy) => {
serialize_function!(dest, scaleY(sy))
}
SpecifiedOperation::ScaleZ(sz) => {
serialize_function!(dest, scaleZ(sz))
}
SpecifiedOperation::Scale3D(sx, sy, sz) => {
serialize_function!(dest, scale3d(sx, sy, sz))
}
SpecifiedOperation::Rotate(theta) => {
serialize_function!(dest, rotate(theta))
}
SpecifiedOperation::RotateX(theta) => {
serialize_function!(dest, rotateX(theta))
}
SpecifiedOperation::RotateY(theta) => {
serialize_function!(dest, rotateY(theta))
}
SpecifiedOperation::RotateZ(theta) => {
serialize_function!(dest, rotateZ(theta))
}
SpecifiedOperation::Rotate3D(x, y, z, theta) => {
serialize_function!(dest, rotate3d(x, y, z, theta))
}
SpecifiedOperation::Perspective(ref length) => {
serialize_function!(dest, perspective(length))
}
SpecifiedOperation::InterpolateMatrix { ref from_list, ref to_list, progress } => {
serialize_function!(dest, interpolatematrix(from_list, to_list, progress))
}
SpecifiedOperation::AccumulateMatrix { ref from_list, ref to_list, count } => {
serialize_function!(dest, accumulatematrix(from_list, to_list, count))
}
}
}
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
pub struct SpecifiedValue(Vec<SpecifiedOperation>);
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
if self.0.is_empty() {
return dest.write_str("none")
}
let mut first = true;
for operation in &self.0 {
if !first {
dest.write_str(" ")?;
}
first = false;
operation.to_css(dest)?
}
Ok(())
}
}
pub use values::specified::transform::Transform as SpecifiedValue;
pub use values::specified::transform::TransformOperation as SpecifiedOperation;
#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T(None)
Transform(vec![])
}
// Allow unitless zero angle for rotate() and skew() to align with gecko
fn parse_internal<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
prefixed: bool,
) -> Result<SpecifiedValue,ParseError<'i>> {
use style_traits::{Separator, Space};
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(SpecifiedValue(Vec::new()))
}
Ok(SpecifiedValue(Space::parse(input, |input| {
let function = input.expect_function()?.clone();
input.parse_nested_block(|input| {
let result = match_ignore_ascii_case! { &function,
"matrix" => {
let a = specified::parse_number(context, input)?;
input.expect_comma()?;
let b = specified::parse_number(context, input)?;
input.expect_comma()?;
let c = specified::parse_number(context, input)?;
input.expect_comma()?;
let d = specified::parse_number(context, input)?;
input.expect_comma()?;
if !prefixed {
// Standard matrix parsing.
let e = specified::parse_number(context, input)?;
input.expect_comma()?;
let f = specified::parse_number(context, input)?;
Ok(SpecifiedOperation::Matrix(Matrix { a, b, c, d, e, f }))
} else {
// Non-standard prefixed matrix parsing for -moz-transform.
let e = LoPoNumber::parse(context, input)?;
input.expect_comma()?;
let f = LoPoNumber::parse(context, input)?;
Ok(SpecifiedOperation::PrefixedMatrix(Matrix { a, b, c, d, e, f }))
}
},
"matrix3d" => {
let m11 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m12 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m13 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m14 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m21 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m22 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m23 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m24 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m31 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m32 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m33 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m34 = specified::parse_number(context, input)?;
input.expect_comma()?;
if !prefixed {
// Standard matrix3d parsing.
let m41 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m42 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m43 = specified::parse_number(context, input)?;
input.expect_comma()?;
let m44 = specified::parse_number(context, input)?;
Ok(SpecifiedOperation::Matrix3D {
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44,
})
} else {
// Non-standard prefixed matrix parsing for -moz-transform.
let m41 = LoPoNumber::parse(context, input)?;
input.expect_comma()?;
let m42 = LoPoNumber::parse(context, input)?;
input.expect_comma()?;
let m43 = LengthOrNumber::parse(context, input)?;
input.expect_comma()?;
let m44 = specified::parse_number(context, input)?;
Ok(SpecifiedOperation::PrefixedMatrix3D {
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44,
})
}
},
"translate" => {
let sx = specified::LengthOrPercentage::parse(context, input)?;
if input.try(|input| input.expect_comma()).is_ok() {
let sy = specified::LengthOrPercentage::parse(context, input)?;
Ok(SpecifiedOperation::Translate(sx, Some(sy)))
} else {
Ok(SpecifiedOperation::Translate(sx, None))
}
},
"translatex" => {
let tx = specified::LengthOrPercentage::parse(context, input)?;
Ok(SpecifiedOperation::TranslateX(tx))
},
"translatey" => {
let ty = specified::LengthOrPercentage::parse(context, input)?;
Ok(SpecifiedOperation::TranslateY(ty))
},
"translatez" => {
let tz = specified::Length::parse(context, input)?;
Ok(SpecifiedOperation::TranslateZ(tz))
},
"translate3d" => {
let tx = specified::LengthOrPercentage::parse(context, input)?;
input.expect_comma()?;
let ty = specified::LengthOrPercentage::parse(context, input)?;
input.expect_comma()?;
let tz = specified::Length::parse(context, input)?;
Ok(SpecifiedOperation::Translate3D(tx, ty, tz))
},
"scale" => {
let sx = specified::parse_number(context, input)?;
if input.try(|input| input.expect_comma()).is_ok() {
let sy = specified::parse_number(context, input)?;
Ok(SpecifiedOperation::Scale(sx, Some(sy)))
} else {
Ok(SpecifiedOperation::Scale(sx, None))
}
},
"scalex" => {
let sx = specified::parse_number(context, input)?;
Ok(SpecifiedOperation::ScaleX(sx))
},
"scaley" => {
let sy = specified::parse_number(context, input)?;
Ok(SpecifiedOperation::ScaleY(sy))
},
"scalez" => {
let sz = specified::parse_number(context, input)?;
Ok(SpecifiedOperation::ScaleZ(sz))
},
"scale3d" => {
let sx = specified::parse_number(context, input)?;
input.expect_comma()?;
let sy = specified::parse_number(context, input)?;
input.expect_comma()?;
let sz = specified::parse_number(context, input)?;
Ok(SpecifiedOperation::Scale3D(sx, sy, sz))
},
"rotate" => {
let theta = specified::Angle::parse_with_unitless(context, input)?;
Ok(SpecifiedOperation::Rotate(theta))
},
"rotatex" => {
let theta = specified::Angle::parse_with_unitless(context, input)?;
Ok(SpecifiedOperation::RotateX(theta))
},
"rotatey" => {
let theta = specified::Angle::parse_with_unitless(context, input)?;
Ok(SpecifiedOperation::RotateY(theta))
},
"rotatez" => {
let theta = specified::Angle::parse_with_unitless(context, input)?;
Ok(SpecifiedOperation::RotateZ(theta))
},
"rotate3d" => {
let ax = specified::parse_number(context, input)?;
input.expect_comma()?;
let ay = specified::parse_number(context, input)?;
input.expect_comma()?;
let az = specified::parse_number(context, input)?;
input.expect_comma()?;
let theta = specified::Angle::parse_with_unitless(context, input)?;
// TODO(gw): Check that the axis can be normalized.
Ok(SpecifiedOperation::Rotate3D(ax, ay, az, theta))
},
"skew" => {
let ax = specified::Angle::parse_with_unitless(context, input)?;
if input.try(|input| input.expect_comma()).is_ok() {
let ay = specified::Angle::parse_with_unitless(context, input)?;
Ok(SpecifiedOperation::Skew(ax, Some(ay)))
} else {
Ok(SpecifiedOperation::Skew(ax, None))
}
},
"skewx" => {
let theta = specified::Angle::parse_with_unitless(context, input)?;
Ok(SpecifiedOperation::SkewX(theta))
},
"skewy" => {
let theta = specified::Angle::parse_with_unitless(context, input)?;
Ok(SpecifiedOperation::SkewY(theta))
},
"perspective" => {
let d = specified::Length::parse_non_negative(context, input)?;
Ok(SpecifiedOperation::Perspective(d))
},
_ => Err(()),
};
result
.map_err(|()| input.new_custom_error(StyleParseErrorKind::UnexpectedFunction(function.clone())))
})
})?))
}
/// Parses `transform` property.
#[inline]
pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue,ParseError<'i>> {
parse_internal(context, input, false)
SpecifiedValue::parse_internal(context, input, false)
}
/// Parses `-moz-transform` property. This prefixed property also accepts LengthOrPercentage
@ -1105,341 +600,7 @@ ${helpers.predefined_type(
#[inline]
pub fn parse_prefixed<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue,ParseError<'i>> {
parse_internal(context, input, true)
}
impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T;
#[inline]
fn to_computed_value(&self, context: &Context) -> computed_value::T {
if self.0.is_empty() {
return computed_value::T(None)
}
let mut result = vec!();
for operation in &self.0 {
match *operation {
SpecifiedOperation::Matrix(Matrix { a, b, c, d, e, f }) => {
let mut comp = computed_value::ComputedMatrix::identity();
comp.m11 = a.to_computed_value(context);
comp.m12 = b.to_computed_value(context);
comp.m21 = c.to_computed_value(context);
comp.m22 = d.to_computed_value(context);
comp.m41 = e.to_computed_value(context);
comp.m42 = f.to_computed_value(context);
result.push(computed_value::ComputedOperation::Matrix(comp));
}
SpecifiedOperation::PrefixedMatrix(Matrix { a, b, c, d, ref e, ref f }) => {
let mut comp = computed_value::ComputedMatrixWithPercents::identity();
comp.m11 = a.to_computed_value(context);
comp.m12 = b.to_computed_value(context);
comp.m21 = c.to_computed_value(context);
comp.m22 = d.to_computed_value(context);
comp.m41 = lopon_to_lop(&e.to_computed_value(context));
comp.m42 = lopon_to_lop(&f.to_computed_value(context));
result.push(computed_value::ComputedOperation::MatrixWithPercents(comp));
}
SpecifiedOperation::Matrix3D {
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
ref m41, ref m42, ref m43, m44 } => {
let comp = computed_value::ComputedMatrix {
m11: m11.to_computed_value(context),
m12: m12.to_computed_value(context),
m13: m13.to_computed_value(context),
m14: m14.to_computed_value(context),
m21: m21.to_computed_value(context),
m22: m22.to_computed_value(context),
m23: m23.to_computed_value(context),
m24: m24.to_computed_value(context),
m31: m31.to_computed_value(context),
m32: m32.to_computed_value(context),
m33: m33.to_computed_value(context),
m34: m34.to_computed_value(context),
m41: m41.to_computed_value(context),
m42: m42.to_computed_value(context),
m43: m43.to_computed_value(context),
m44: m44.to_computed_value(context),
};
result.push(computed_value::ComputedOperation::Matrix(comp));
}
SpecifiedOperation::PrefixedMatrix3D {
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
ref m41, ref m42, ref m43, m44 } => {
let comp = computed_value::ComputedMatrixWithPercents {
m11: m11.to_computed_value(context),
m12: m12.to_computed_value(context),
m13: m13.to_computed_value(context),
m14: m14.to_computed_value(context),
m21: m21.to_computed_value(context),
m22: m22.to_computed_value(context),
m23: m23.to_computed_value(context),
m24: m24.to_computed_value(context),
m31: m31.to_computed_value(context),
m32: m32.to_computed_value(context),
m33: m33.to_computed_value(context),
m34: m34.to_computed_value(context),
m41: lopon_to_lop(&m41.to_computed_value(context)),
m42: lopon_to_lop(&m42.to_computed_value(context)),
m43: lon_to_length(&m43.to_computed_value(context)),
m44: m44.to_computed_value(context),
};
result.push(computed_value::ComputedOperation::MatrixWithPercents(comp));
}
SpecifiedOperation::Translate(ref tx, None) => {
let tx = tx.to_computed_value(context);
result.push(computed_value::ComputedOperation::Translate(
tx,
computed::length::LengthOrPercentage::zero(),
computed::length::Length::new(0.)));
}
SpecifiedOperation::Translate(ref tx, Some(ref ty)) => {
let tx = tx.to_computed_value(context);
let ty = ty.to_computed_value(context);
result.push(computed_value::ComputedOperation::Translate(
tx,
ty,
computed::length::Length::new(0.)));
}
SpecifiedOperation::TranslateX(ref tx) => {
let tx = tx.to_computed_value(context);
result.push(computed_value::ComputedOperation::Translate(
tx,
computed::length::LengthOrPercentage::zero(),
computed::length::Length::new(0.)));
}
SpecifiedOperation::TranslateY(ref ty) => {
let ty = ty.to_computed_value(context);
result.push(computed_value::ComputedOperation::Translate(
computed::length::LengthOrPercentage::zero(),
ty,
computed::length::Length::new(0.)));
}
SpecifiedOperation::TranslateZ(ref tz) => {
let tz = tz.to_computed_value(context);
result.push(computed_value::ComputedOperation::Translate(
computed::length::LengthOrPercentage::zero(),
computed::length::LengthOrPercentage::zero(),
tz));
}
SpecifiedOperation::Translate3D(ref tx, ref ty, ref tz) => {
let tx = tx.to_computed_value(context);
let ty = ty.to_computed_value(context);
let tz = tz.to_computed_value(context);
result.push(computed_value::ComputedOperation::Translate(tx, ty, tz));
}
SpecifiedOperation::Scale(factor, None) => {
let factor = factor.to_computed_value(context);
result.push(computed_value::ComputedOperation::Scale(factor, factor, 1.0));
}
SpecifiedOperation::Scale(sx, Some(sy)) => {
let sx = sx.to_computed_value(context);
let sy = sy.to_computed_value(context);
result.push(computed_value::ComputedOperation::Scale(sx, sy, 1.0));
}
SpecifiedOperation::ScaleX(sx) => {
let sx = sx.to_computed_value(context);
result.push(computed_value::ComputedOperation::Scale(sx, 1.0, 1.0));
}
SpecifiedOperation::ScaleY(sy) => {
let sy = sy.to_computed_value(context);
result.push(computed_value::ComputedOperation::Scale(1.0, sy, 1.0));
}
SpecifiedOperation::ScaleZ(sz) => {
let sz = sz.to_computed_value(context);
result.push(computed_value::ComputedOperation::Scale(1.0, 1.0, sz));
}
SpecifiedOperation::Scale3D(sx, sy, sz) => {
let sx = sx.to_computed_value(context);
let sy = sy.to_computed_value(context);
let sz = sz.to_computed_value(context);
result.push(computed_value::ComputedOperation::Scale(sx, sy, sz));
}
SpecifiedOperation::Rotate(theta) => {
let theta = theta.to_computed_value(context);
result.push(computed_value::ComputedOperation::Rotate(0.0, 0.0, 1.0, theta));
}
SpecifiedOperation::RotateX(theta) => {
let theta = theta.to_computed_value(context);
result.push(computed_value::ComputedOperation::Rotate(1.0, 0.0, 0.0, theta));
}
SpecifiedOperation::RotateY(theta) => {
let theta = theta.to_computed_value(context);
result.push(computed_value::ComputedOperation::Rotate(0.0, 1.0, 0.0, theta));
}
SpecifiedOperation::RotateZ(theta) => {
let theta = theta.to_computed_value(context);
result.push(computed_value::ComputedOperation::Rotate(0.0, 0.0, 1.0, theta));
}
SpecifiedOperation::Rotate3D(ax, ay, az, theta) => {
let ax = ax.to_computed_value(context);
let ay = ay.to_computed_value(context);
let az = az.to_computed_value(context);
let theta = theta.to_computed_value(context);
result.push(computed_value::ComputedOperation::Rotate(ax, ay, az, theta));
}
SpecifiedOperation::Skew(theta_x, None) => {
let theta_x = theta_x.to_computed_value(context);
result.push(computed_value::ComputedOperation::Skew(theta_x, computed::Angle::zero()));
}
SpecifiedOperation::Skew(theta_x, Some(theta_y)) => {
let theta_x = theta_x.to_computed_value(context);
let theta_y = theta_y.to_computed_value(context);
result.push(computed_value::ComputedOperation::Skew(theta_x, theta_y));
}
SpecifiedOperation::SkewX(theta_x) => {
let theta_x = theta_x.to_computed_value(context);
result.push(computed_value::ComputedOperation::Skew(theta_x, computed::Angle::zero()));
}
SpecifiedOperation::SkewY(theta_y) => {
let theta_y = theta_y.to_computed_value(context);
result.push(computed_value::ComputedOperation::Skew(computed::Angle::zero(), theta_y));
}
SpecifiedOperation::Perspective(ref d) => {
result.push(computed_value::ComputedOperation::Perspective(d.to_computed_value(context)));
}
SpecifiedOperation::InterpolateMatrix { ref from_list, ref to_list, progress } => {
result.push(computed_value::ComputedOperation::InterpolateMatrix {
from_list: from_list.to_computed_value(context),
to_list: to_list.to_computed_value(context),
progress: progress
});
}
SpecifiedOperation::AccumulateMatrix { ref from_list, ref to_list, count } => {
result.push(computed_value::ComputedOperation::AccumulateMatrix {
from_list: from_list.to_computed_value(context),
to_list: to_list.to_computed_value(context),
count: count.value()
});
}
};
}
computed_value::T(Some(result))
}
#[inline]
fn from_computed_value(computed: &computed_value::T) -> Self {
SpecifiedValue(computed.0.as_ref().map(|computed| {
let mut result = vec![];
for operation in computed {
match *operation {
computed_value::ComputedOperation::Matrix(ref computed) => {
result.push(SpecifiedOperation::Matrix3D {
m11: Number::from_computed_value(&computed.m11),
m12: Number::from_computed_value(&computed.m12),
m13: Number::from_computed_value(&computed.m13),
m14: Number::from_computed_value(&computed.m14),
m21: Number::from_computed_value(&computed.m21),
m22: Number::from_computed_value(&computed.m22),
m23: Number::from_computed_value(&computed.m23),
m24: Number::from_computed_value(&computed.m24),
m31: Number::from_computed_value(&computed.m31),
m32: Number::from_computed_value(&computed.m32),
m33: Number::from_computed_value(&computed.m33),
m34: Number::from_computed_value(&computed.m34),
m41: Number::from_computed_value(&computed.m41),
m42: Number::from_computed_value(&computed.m42),
m43: Number::from_computed_value(&computed.m43),
m44: Number::from_computed_value(&computed.m44),
});
}
computed_value::ComputedOperation::MatrixWithPercents(ref computed) => {
result.push(SpecifiedOperation::PrefixedMatrix3D {
m11: Number::from_computed_value(&computed.m11),
m12: Number::from_computed_value(&computed.m12),
m13: Number::from_computed_value(&computed.m13),
m14: Number::from_computed_value(&computed.m14),
m21: Number::from_computed_value(&computed.m21),
m22: Number::from_computed_value(&computed.m22),
m23: Number::from_computed_value(&computed.m23),
m24: Number::from_computed_value(&computed.m24),
m31: Number::from_computed_value(&computed.m31),
m32: Number::from_computed_value(&computed.m32),
m33: Number::from_computed_value(&computed.m33),
m34: Number::from_computed_value(&computed.m34),
m41: Either::Second(LengthOrPercentage::from_computed_value(&computed.m41)),
m42: Either::Second(LengthOrPercentage::from_computed_value(&computed.m42)),
m43: LengthOrNumber::from_computed_value(&Either::First(computed.m43)),
m44: Number::from_computed_value(&computed.m44),
});
}
computed_value::ComputedOperation::Translate(ref tx, ref ty, ref tz) => {
// XXXManishearth we lose information here; perhaps we should try to
// recover the original function? Not sure if this can be observed.
result.push(SpecifiedOperation::Translate3D(
ToComputedValue::from_computed_value(tx),
ToComputedValue::from_computed_value(ty),
ToComputedValue::from_computed_value(tz)));
}
computed_value::ComputedOperation::Scale(ref sx, ref sy, ref sz) => {
result.push(SpecifiedOperation::Scale3D(
Number::from_computed_value(sx),
Number::from_computed_value(sy),
Number::from_computed_value(sz)));
}
computed_value::ComputedOperation::Rotate(ref ax, ref ay, ref az, ref theta) => {
result.push(SpecifiedOperation::Rotate3D(
Number::from_computed_value(ax),
Number::from_computed_value(ay),
Number::from_computed_value(az),
specified::Angle::from_computed_value(theta)));
}
computed_value::ComputedOperation::Skew(ref theta_x, ref theta_y) => {
result.push(SpecifiedOperation::Skew(
specified::Angle::from_computed_value(theta_x),
Some(specified::Angle::from_computed_value(theta_y))))
}
computed_value::ComputedOperation::Perspective(ref d) => {
result.push(SpecifiedOperation::Perspective(
ToComputedValue::from_computed_value(d)
));
}
computed_value::ComputedOperation::InterpolateMatrix { ref from_list,
ref to_list,
progress } => {
result.push(SpecifiedOperation::InterpolateMatrix {
from_list: SpecifiedValue::from_computed_value(from_list),
to_list: SpecifiedValue::from_computed_value(to_list),
progress: progress
});
}
computed_value::ComputedOperation::AccumulateMatrix { ref from_list,
ref to_list,
count } => {
result.push(SpecifiedOperation::AccumulateMatrix {
from_list: SpecifiedValue::from_computed_value(from_list),
to_list: SpecifiedValue::from_computed_value(to_list),
count: Integer::new(count)
});
}
};
}
result
}).unwrap_or(Vec::new()))
}
}
// Converts computed LengthOrPercentageOrNumber into computed
// LengthOrPercentage. Number maps into Length (pixel unit)
fn lopon_to_lop(value: &ComputedLoPoNumber) -> ComputedLoP {
match *value {
Either::First(number) => ComputedLoP::Length(ComputedLength::new(number)),
Either::Second(length_or_percentage) => length_or_percentage,
}
}
// Converts computed LengthOrNumber into computed Length.
// Number maps into Length.
fn lon_to_length(value: &ComputedLoN) -> ComputedLength {
match *value {
Either::First(length) => length,
Either::Second(number) => ComputedLength::new(number),
}
SpecifiedValue::parse_internal(context, input, true)
}
</%helpers:longhand>