mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Replace old transform code with new generic code
This commit is contained in:
parent
5ce2966bda
commit
6631594e28
10 changed files with 210 additions and 1268 deletions
|
@ -6,8 +6,6 @@
|
|||
|
||||
use app_units::Au;
|
||||
use euclid::{Rect, Transform3D, Vector3D};
|
||||
use properties::longhands::transform::computed_value::{ComputedOperation, ComputedMatrix};
|
||||
use properties::longhands::transform::computed_value::T as TransformList;
|
||||
use std::f32;
|
||||
use super::{CSSFloat, Either};
|
||||
use values::animated::ToAnimatedZero;
|
||||
|
@ -45,29 +43,6 @@ impl TransformOrigin {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<ComputedMatrix> for Transform3D<CSSFloat> {
|
||||
#[inline]
|
||||
fn from(m: ComputedMatrix) -> Self {
|
||||
Transform3D::row_major(
|
||||
m.m11, m.m12, m.m13, m.m14,
|
||||
m.m21, m.m22, m.m23, m.m24,
|
||||
m.m31, m.m32, m.m33, m.m34,
|
||||
m.m41, m.m42, m.m43, m.m44)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Transform3D<CSSFloat>> for ComputedMatrix {
|
||||
#[inline]
|
||||
fn from(m: Transform3D<CSSFloat>) -> Self {
|
||||
ComputedMatrix {
|
||||
m11: m.m11, m12: m.m12, m13: m.m13, m14: m.m14,
|
||||
m21: m.m21, m22: m.m22, m23: m.m23, m24: m.m24,
|
||||
m31: m.m31, m32: m.m32, m33: m.m33, m34: m.m34,
|
||||
m41: m.m41, m42: m.m42, m43: m.m43, m44: m.m44
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// computed value of matrix3d()
|
||||
pub type Matrix3D = GenericMatrix3D<Number>;
|
||||
/// computed value of matrix3d() in -moz-transform
|
||||
|
@ -141,6 +116,18 @@ impl From<Matrix3D> for Transform3D<CSSFloat> {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<Transform3D<CSSFloat>> for Matrix3D {
|
||||
#[inline]
|
||||
fn from(m: Transform3D<CSSFloat>) -> Self {
|
||||
Matrix3D {
|
||||
m11: m.m11, m12: m.m12, m13: m.m13, m14: m.m14,
|
||||
m21: m.m21, m22: m.m22, m23: m.m23, m24: m.m24,
|
||||
m31: m.m31, m32: m.m32, m33: m.m33, m34: m.m34,
|
||||
m41: m.m41, m42: m.m42, m43: m.m43, m44: m.m44
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Matrix> for Transform3D<CSSFloat> {
|
||||
#[inline]
|
||||
fn from(m: Matrix) -> Self {
|
||||
|
@ -152,118 +139,6 @@ impl From<Matrix> for Transform3D<CSSFloat> {
|
|||
}
|
||||
}
|
||||
|
||||
impl TransformList {
|
||||
/// Return the equivalent 3d matrix of this transform list.
|
||||
/// If |reference_box| is None, we will drop the percent part from translate because
|
||||
/// we can resolve it without the layout info.
|
||||
pub fn to_transform_3d_matrix(&self, reference_box: Option<&Rect<Au>>)
|
||||
-> Option<Transform3D<CSSFloat>> {
|
||||
let mut transform = Transform3D::identity();
|
||||
let list = match self.0.as_ref() {
|
||||
Some(list) => list,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
let extract_pixel_length = |lop: &LengthOrPercentage| {
|
||||
match *lop {
|
||||
LengthOrPercentage::Length(px) => px.px(),
|
||||
LengthOrPercentage::Percentage(_) => 0.,
|
||||
LengthOrPercentage::Calc(calc) => calc.length().px(),
|
||||
}
|
||||
};
|
||||
|
||||
for operation in list {
|
||||
let matrix = match *operation {
|
||||
ComputedOperation::Rotate(ax, ay, az, theta) => {
|
||||
let theta = Angle::from_radians(2.0f32 * f32::consts::PI - theta.radians());
|
||||
let (ax, ay, az, theta) =
|
||||
Self::get_normalized_vector_and_angle(ax, ay, az, theta);
|
||||
Transform3D::create_rotation(ax, ay, az, theta.into())
|
||||
}
|
||||
ComputedOperation::Perspective(d) => {
|
||||
Self::create_perspective_matrix(d.px())
|
||||
}
|
||||
ComputedOperation::Scale(sx, sy, sz) => {
|
||||
Transform3D::create_scale(sx, sy, sz)
|
||||
}
|
||||
ComputedOperation::Translate(tx, ty, tz) => {
|
||||
let (tx, ty) = match reference_box {
|
||||
Some(relative_border_box) => {
|
||||
(tx.to_pixel_length(relative_border_box.size.width).px(),
|
||||
ty.to_pixel_length(relative_border_box.size.height).px())
|
||||
},
|
||||
None => {
|
||||
// If we don't have reference box, we cannot resolve the used value,
|
||||
// so only retrieve the length part. This will be used for computing
|
||||
// distance without any layout info.
|
||||
(extract_pixel_length(&tx), extract_pixel_length(&ty))
|
||||
}
|
||||
};
|
||||
let tz = tz.px();
|
||||
Transform3D::create_translation(tx, ty, tz)
|
||||
}
|
||||
ComputedOperation::Matrix(m) => {
|
||||
m.into()
|
||||
}
|
||||
ComputedOperation::MatrixWithPercents(_) => {
|
||||
// `-moz-transform` is not implemented in Servo yet.
|
||||
unreachable!()
|
||||
}
|
||||
ComputedOperation::Skew(theta_x, theta_y) => {
|
||||
Transform3D::create_skew(theta_x.into(), theta_y.into())
|
||||
}
|
||||
ComputedOperation::InterpolateMatrix { .. } |
|
||||
ComputedOperation::AccumulateMatrix { .. } => {
|
||||
// TODO: Convert InterpolateMatrix/AccmulateMatrix into a valid Transform3D by
|
||||
// the reference box and do interpolation on these two Transform3D matrices.
|
||||
// Both Gecko and Servo don't support this for computing distance, and Servo
|
||||
// doesn't support animations on InterpolateMatrix/AccumulateMatrix, so
|
||||
// return None.
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
transform = transform.pre_mul(&matrix);
|
||||
}
|
||||
|
||||
Some(transform)
|
||||
}
|
||||
|
||||
/// Return the transform matrix from a perspective length.
|
||||
#[inline]
|
||||
pub fn create_perspective_matrix(d: CSSFloat) -> Transform3D<f32> {
|
||||
// TODO(gw): The transforms spec says that perspective length must
|
||||
// be positive. However, there is some confusion between the spec
|
||||
// and browser implementations as to handling the case of 0 for the
|
||||
// perspective value. Until the spec bug is resolved, at least ensure
|
||||
// that a provided perspective value of <= 0.0 doesn't cause panics
|
||||
// and behaves as it does in other browsers.
|
||||
// See https://lists.w3.org/Archives/Public/www-style/2016Jan/0020.html for more details.
|
||||
if d <= 0.0 {
|
||||
Transform3D::identity()
|
||||
} else {
|
||||
Transform3D::create_perspective(d)
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the normalized direction vector and its angle for Rotate3D.
|
||||
pub fn get_normalized_vector_and_angle(x: f32, y: f32, z: f32, angle: Angle)
|
||||
-> (f32, f32, f32, Angle) {
|
||||
use euclid::approxeq::ApproxEq;
|
||||
use euclid::num::Zero;
|
||||
let vector = DirectionVector::new(x, y, z);
|
||||
if vector.square_length().approx_eq(&f32::zero()) {
|
||||
// https://www.w3.org/TR/css-transforms-1/#funcdef-rotate3d
|
||||
// A direction vector that cannot be normalized, such as [0, 0, 0], will cause the
|
||||
// rotation to not be applied, so we use identity matrix (i.e. rotate3d(0, 0, 1, 0)).
|
||||
(0., 0., 1., Angle::zero())
|
||||
} else {
|
||||
let vector = vector.normalize();
|
||||
(vector.x, vector.y, vector.z, angle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build an equivalent 'identity transform function list' based
|
||||
/// on an existing transform list.
|
||||
/// http://dev.w3.org/csswg/css-transforms/#none-transform-animation
|
||||
|
@ -345,7 +220,7 @@ impl ToAnimatedZero for TransformOperation {
|
|||
Ok(GenericTransformOperation::ScaleZ(1.0))
|
||||
},
|
||||
GenericTransformOperation::Rotate3D(x, y, z, a) => {
|
||||
let (x, y, z, _) = TransformList::get_normalized_vector_and_angle(x, y, z, a);
|
||||
let (x, y, z, _) = Transform::get_normalized_vector_and_angle(x, y, z, a);
|
||||
Ok(GenericTransformOperation::Rotate3D(x, y, z, Angle::zero()))
|
||||
},
|
||||
GenericTransformOperation::RotateX(_) => {
|
||||
|
@ -377,6 +252,16 @@ impl ToAnimatedZero for TransformOperation {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
impl ToAnimatedZero for Transform {
|
||||
#[inline]
|
||||
fn to_animated_zero(&self) -> Result<Self, ()> {
|
||||
Ok(GenericTransform(
|
||||
self.0.iter().map(|op| op.to_animated_zero()).collect::<Result<Vec<_>, _>>()?
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Transform {
|
||||
/// Return the equivalent 3d matrix of this transform list.
|
||||
/// If |reference_box| is None, we will drop the percent part from translate because
|
||||
|
@ -567,4 +452,3 @@ impl Transform {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,9 +22,7 @@ pub struct Matrix<T, U = T> {
|
|||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[derive(Clone, Copy, Debug, ToComputedValue, PartialEq)]
|
||||
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue)]
|
||||
pub struct Matrix3D<T, U = T, V = T> {
|
||||
pub m11: T, pub m12: T, pub m13: T, pub m14: T,
|
||||
pub m21: T, pub m22: T, pub m23: T, pub m24: T,
|
||||
|
@ -149,9 +147,7 @@ impl TimingKeyword {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
|
||||
#[derive(ToComputedValue)]
|
||||
/// A single operation in the list of a `transform` value
|
||||
pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrNumber, LengthOrPercentage, LoPoNumber> {
|
||||
|
@ -247,9 +243,7 @@ pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrNumber, Leng
|
|||
}
|
||||
|
||||
#[derive(Animate, ToComputedValue)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
|
||||
/// A value of the `transform` property
|
||||
pub struct Transform<T>(pub Vec<T>);
|
||||
|
||||
|
|
|
@ -46,6 +46,7 @@ impl Transform {
|
|||
Ok(GenericTransform(Space::parse(input, |input| {
|
||||
let function = input.expect_function()?.clone();
|
||||
input.parse_nested_block(|input| {
|
||||
let location = input.current_source_location();
|
||||
let result = match_ignore_ascii_case! { &function,
|
||||
"matrix" => {
|
||||
let a = specified::parse_number(context, input)?;
|
||||
|
@ -236,7 +237,7 @@ impl Transform {
|
|||
_ => Err(()),
|
||||
};
|
||||
result
|
||||
.map_err(|()| StyleParseError::UnexpectedFunction(function.clone()).into())
|
||||
.map_err(|()| location.new_custom_error(StyleParseErrorKind::UnexpectedFunction(function.clone())))
|
||||
})
|
||||
})?))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue