Skip to content

Truly independent web browser

LibWeb: Combine matrix() pairs in transform lists on their own

Andreas Kling6123cadaaf09
diff --git a/Libraries/LibWeb/Rust/src/css/animation.rs b/Libraries/LibWeb/Rust/src/css/animation.rs
index 2aae5178654..2af22e71332 100644
--- a/Libraries/LibWeb/Rust/src/css/animation.rs
+++ b/Libraries/LibWeb/Rust/src/css/animation.rs
@@ -5880,16 +5880,22 @@ fn interpolate_transform_list(
let Some((transform_function, from_arguments, to_arguments)) =
convert_transform_pair_to_common_primitive(*from_function, from_arguments, *to_function, to_arguments)
else {
+ // https://drafts.csswg.org/css-transforms-2/#interpolation-of-transform-functions
+ // The transform functions <matrix()>, matrix3d() and perspective() get converted into 4x4 matrices first and
+ // interpolated as defined in section Interpolation of Matrices afterwards.
+ let is_matrix_pair = from_function == to_function
+ && matches!(*from_function, TRANSFORM_FUNCTION_MATRIX | TRANSFORM_FUNCTION_MATRIX_3D);
// https://drafts.csswg.org/css-transforms-1/#interpolation-of-transforms
// * If the pair do not have a common name or primitive transform function, post-multiply the remaining
// transform functions in each of Va and Vb respectively to produce two 4x4 matrices. Interpolate these two
// matrices as described in § 11 Interpolation of Matrices, append the result to Vresult, and cease
// iterating over Va and Vb.
+ let end = if is_matrix_pair { index + 1 } else { from_values.len() };
let transformation = interpolate_transform_matrix_suffix(
context,
*from_property,
- &from_values[index..],
- &to_values[index..],
+ &from_values[index..end],
+ &to_values[index..end],
delta,
);
match transformation {
@@ -5905,6 +5911,9 @@ fn interpolate_transform_list(
return Some(None);
}
}
+ if is_matrix_pair {
+ continue;
+ }
break;
};
@@ -6323,14 +6332,29 @@ fn composite_transform_list(
values.push(value);
continue;
}
- let underlying_matrix = post_multiply_transformations(Some(context), &underlying_values[index..])?;
- let animated_matrix = post_multiply_transformations(Some(context), &animated_values[index..])?;
+ let is_matrix_pair = matches!(
+ (underlying_value.data(), animated_value.data()),
+ (
+ StyleValueData::Transformation { transform_function: underlying_function, .. },
+ StyleValueData::Transformation { transform_function: animated_function, .. },
+ ) if underlying_function == animated_function
+ && matches!(*underlying_function, TRANSFORM_FUNCTION_MATRIX | TRANSFORM_FUNCTION_MATRIX_3D)
+ );
+ let end = if is_matrix_pair {
+ index + 1
+ } else {
+ underlying_values.len()
+ };
+ let underlying_matrix = post_multiply_transformations(Some(context), &underlying_values[index..end])?;
+ let animated_matrix = post_multiply_transformations(Some(context), &animated_values[index..end])?;
// NB: Accumulating a non-invertible matrix falls back to replacing the underlying value.
let matrix = accumulate_matrices(underlying_matrix, animated_matrix)?;
values.push(RetainedStyleValueData::from_arc(Arc::new(matrix_transformation(
*property, matrix,
))));
- break;
+ if !is_matrix_pair {
+ break;
+ }
}
Some(transform_list_from_values(values, separator, collapsible))
}
diff --git a/Tests/LibWeb/TestStyleValueEquality.cpp b/Tests/LibWeb/TestStyleValueEquality.cpp
index c3adc9b8a42..a370e98b4e3 100644
--- a/Tests/LibWeb/TestStyleValueEquality.cpp
+++ b/Tests/LibWeb/TestStyleValueEquality.cpp
@@ -1272,27 +1272,28 @@ TEST_CASE(rust_interpolates_terminal_matrix_transform_functions)
EXPECT_APPROXIMATE(arguments[13]->as_number().number(), 20.0);
}
+static NonnullRefPtr<StyleValueList const> make_identity_matrix_and_translate_x(TransformFunction matrix_function, double translation)
+{
+ StyleValueVector arguments;
+ auto argument_count = matrix_function == TransformFunction::Matrix ? 6u : 16u;
+ for (u32 index = 0; index < argument_count; ++index) {
+ auto is_diagonal = matrix_function == TransformFunction::Matrix ? index == 0 || index == 3 : index % 5 == 0;
+ arguments.append(NumberStyleValue::create(is_diagonal ? 1 : 0));
+ }
+ return StyleValueList::create(
+ { TransformationStyleValue::create(PropertyID::Transform, matrix_function, move(arguments)),
+ TransformationStyleValue::create(
+ PropertyID::Transform,
+ TransformFunction::TranslateX,
+ { LengthStyleValue::create(Length::make_px(translation)) }) },
+ StyleValueList::Separator::Space);
+}
+
TEST_CASE(rust_post_multiplies_transform_matrix_suffixes)
{
- auto make_transform = [](double translation) {
- return StyleValueList::create(
- { TransformationStyleValue::create(
- PropertyID::Transform,
- TransformFunction::Matrix,
- { NumberStyleValue::create(1),
- NumberStyleValue::create(0),
- NumberStyleValue::create(0),
- NumberStyleValue::create(1),
- NumberStyleValue::create(0),
- NumberStyleValue::create(0) }),
- TransformationStyleValue::create(
- PropertyID::Transform,
- TransformFunction::TranslateX,
- { LengthStyleValue::create(Length::make_px(translation)) }) },
- StyleValueList::Separator::Space);
- };
- auto from = make_transform(100);
- auto to = make_transform(200);
+ // NB: matrix() and matrix3d() have no common name, so they and every function after them are post-multiplied.
+ auto from = make_identity_matrix_and_translate_x(TransformFunction::Matrix, 100);
+ auto to = make_identity_matrix_and_translate_x(TransformFunction::Matrix3d, 200);
auto result = StyleValueFFI::rust_interpolate_scalar_style_value(
nullptr,
@@ -1309,6 +1310,34 @@ TEST_CASE(rust_post_multiplies_transform_matrix_suffixes)
EXPECT_APPROXIMATE(arguments[12]->as_number().number(), 150.0);
}
+TEST_CASE(rust_interpolates_matrix_pairs_on_their_own)
+{
+ auto from = make_identity_matrix_and_translate_x(TransformFunction::Matrix, 100);
+ auto to = make_identity_matrix_and_translate_x(TransformFunction::Matrix, 200);
+
+ auto result = StyleValueFFI::rust_interpolate_scalar_style_value(
+ nullptr,
+ to_underlying(PropertyID::Transform),
+ from->rust_style_value_data(),
+ to->rust_style_value_data(),
+ 0.5f);
+ EXPECT(result.handled);
+ auto interpolated = StyleValue::adopt_rust_style_value_data(result.value);
+ auto transformations = interpolated->as_value_list().values();
+ EXPECT_EQ(transformations.size(), 2u);
+ EXPECT_EQ(transformations[0]->as_transformation().transform_function(), TransformFunction::Matrix3d);
+ auto matrix = transformations[0]->as_transformation().values();
+ EXPECT_EQ(matrix.size(), 16u);
+ for (size_t index = 0; index < matrix.size(); ++index)
+ EXPECT_APPROXIMATE(matrix[index]->as_number().number(), index % 5 == 0 ? 1.0 : 0.0);
+ EXPECT_EQ(transformations[1]->as_transformation().transform_function(), TransformFunction::Translate);
+ auto translation = transformations[1]->as_transformation().values();
+ EXPECT_EQ(translation.size(), 2u);
+ EXPECT_APPROXIMATE(translation[0]->as_length().length().raw_value(), 150.0);
+ EXPECT(translation[0]->as_length().length().is_px());
+ EXPECT_APPROXIMATE(translation[1]->as_length().length().raw_value(), 0.0);
+}
+
TEST_CASE(rust_interpolates_transform_functions_without_a_common_primitive)
{
auto from = StyleValueList::create(
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-transforms/animation/transform-interpolation-verify-reftests.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-transforms/animation/transform-interpolation-verify-reftests.txt
index b5013d72cb5..527dbcf2749 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-transforms/animation/transform-interpolation-verify-reftests.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-transforms/animation/transform-interpolation-verify-reftests.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 176 tests
-172 Pass
-4 Fail
+176 Pass
Pass CSS Transitions: property <transform> from [translateX(0px)] to [translateX(50px)] at (0.5) should be [translateX(25px)]
Pass CSS Transitions with transition: all: property <transform> from [translateX(0px)] to [translateX(50px)] at (0.5) should be [translateX(25px)]
Pass CSS Animations: property <transform> from [translateX(0px)] to [translateX(50px)] at (0.5) should be [translateX(25px)]
@@ -140,10 +139,10 @@ Pass CSS Transitions: property <transform> from [skewX(0deg) rotate(0deg)] to [s
Pass CSS Transitions with transition: all: property <transform> from [skewX(0deg) rotate(0deg)] to [skewY(0deg) rotate(360deg)] at (0.5) should be [matrix(1, 0, 0, 1, 0, 0)]
Pass CSS Animations: property <transform> from [skewX(0deg) rotate(0deg)] to [skewY(0deg) rotate(360deg)] at (0.5) should be [matrix(1, 0, 0, 1, 0, 0)]
Pass Web Animations: property <transform> from [skewX(0deg) rotate(0deg)] to [skewY(0deg) rotate(360deg)] at (0.5) should be [matrix(1, 0, 0, 1, 0, 0)]
-Fail CSS Transitions: property <transform> from [matrix(1,0,0,1,0,0) rotate(0deg)] to [matrix(2,0,0,2,0,0) rotate(360deg)] at (0.5) should be [matrix(1.5,0,0,1.5,0,0) rotate(180deg)]
-Fail CSS Transitions with transition: all: property <transform> from [matrix(1,0,0,1,0,0) rotate(0deg)] to [matrix(2,0,0,2,0,0) rotate(360deg)] at (0.5) should be [matrix(1.5,0,0,1.5,0,0) rotate(180deg)]
-Fail CSS Animations: property <transform> from [matrix(1,0,0,1,0,0) rotate(0deg)] to [matrix(2,0,0,2,0,0) rotate(360deg)] at (0.5) should be [matrix(1.5,0,0,1.5,0,0) rotate(180deg)]
-Fail Web Animations: property <transform> from [matrix(1,0,0,1,0,0) rotate(0deg)] to [matrix(2,0,0,2,0,0) rotate(360deg)] at (0.5) should be [matrix(1.5,0,0,1.5,0,0) rotate(180deg)]
+Pass CSS Transitions: property <transform> from [matrix(1,0,0,1,0,0) rotate(0deg)] to [matrix(2,0,0,2,0,0) rotate(360deg)] at (0.5) should be [matrix(1.5,0,0,1.5,0,0) rotate(180deg)]
+Pass CSS Transitions with transition: all: property <transform> from [matrix(1,0,0,1,0,0) rotate(0deg)] to [matrix(2,0,0,2,0,0) rotate(360deg)] at (0.5) should be [matrix(1.5,0,0,1.5,0,0) rotate(180deg)]
+Pass CSS Animations: property <transform> from [matrix(1,0,0,1,0,0) rotate(0deg)] to [matrix(2,0,0,2,0,0) rotate(360deg)] at (0.5) should be [matrix(1.5,0,0,1.5,0,0) rotate(180deg)]
+Pass Web Animations: property <transform> from [matrix(1,0,0,1,0,0) rotate(0deg)] to [matrix(2,0,0,2,0,0) rotate(360deg)] at (0.5) should be [matrix(1.5,0,0,1.5,0,0) rotate(180deg)]
Pass CSS Transitions: property <transform> from [perspective(none) translateZ(15px)] to [perspective(none) translateZ(15px)] at (0.5) should be [perspective(none) translateZ(15px)]
Pass CSS Transitions with transition: all: property <transform> from [perspective(none) translateZ(15px)] to [perspective(none) translateZ(15px)] at (0.5) should be [perspective(none) translateZ(15px)]
Pass CSS Animations: property <transform> from [perspective(none) translateZ(15px)] to [perspective(none) translateZ(15px)] at (0.5) should be [perspective(none) translateZ(15px)]