Skip to content

Truly independent web browser

LibWeb: Move CSS transform math out of the visual context

Aliaksandr Kalenik5eaea51a59c2
diff --git a/Libraries/LibWeb/Rust/src/css/mod.rs b/Libraries/LibWeb/Rust/src/css/mod.rs
index bf6c90415c1..0851a8f7f80 100644
--- a/Libraries/LibWeb/Rust/src/css/mod.rs
+++ b/Libraries/LibWeb/Rust/src/css/mod.rs
@@ -68,6 +68,7 @@ pub(crate) mod style_value;
pub(crate) mod style_value_arc;
pub mod table_group_builder;
pub(crate) mod thin_slice;
+pub(crate) mod transform_functions;
pub mod transition;
pub mod typed_om;
pub(crate) mod value_codes;
@@ -75,7 +76,3 @@ pub(crate) mod value_codes;
pub(crate) mod dimension_units {
include!(concat!(env!("OUT_DIR"), "/dimension_units_generated.rs"));
}
-
-pub(crate) mod transform_functions {
- include!(concat!(env!("OUT_DIR"), "/transform_functions_generated.rs"));
-}
diff --git a/Libraries/LibWeb/Rust/src/css/resolved_value.rs b/Libraries/LibWeb/Rust/src/css/resolved_value.rs
index 2cd9c2577b9..e04582ffaaf 100644
--- a/Libraries/LibWeb/Rust/src/css/resolved_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/resolved_value.rs
@@ -35,11 +35,11 @@ use crate::css::style_value::{
};
use crate::css::style_value_arc::StyleValueArc;
use crate::css::table_group_builder::group_index;
+use crate::css::transform_functions::multiply_transform_functions;
use crate::css::value_codes::{COLOR_SYNTAX_MODERN, COLOR_TYPE_RGB};
use crate::layout::node_data::NodeSlotId;
use crate::layout::script_entries::FfiAnchorSizedProperty;
use crate::painting::ffi::FfiBoxModelMetrics;
-use crate::painting::visual_context::node_values::multiply_transform_functions;
use crate::render_state::DocumentHost;
const VALUE_LIST_SEPARATOR_SPACE: u8 = 0;
diff --git a/Libraries/LibWeb/Rust/src/css/transform_functions.rs b/Libraries/LibWeb/Rust/src/css/transform_functions.rs
new file mode 100644
index 00000000000..8bb19728125
--- /dev/null
+++ b/Libraries/LibWeb/Rust/src/css/transform_functions.rs
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use crate::css::computed_value_types::{ComputedResolvedTransform, ComputedStyleValueHandle};
+use crate::css::css_pixels::{CssPixelRect, CssPixels};
+use libgfx_rust::{FloatMatrix4x4, translation_matrix};
+
+include!(concat!(env!("OUT_DIR"), "/transform_functions_generated.rs"));
+
+fn resolved_translate_axis_px(px: f32, percentage: &ComputedStyleValueHandle, reference: CssPixels) -> f32 {
+ let Some(length_percentage) = percentage.length_percentage() else {
+ return px;
+ };
+ if length_percentage.is_calculated() {
+ return CssPixels::nearest_value_for(crate::css::computed_value_views::resolve_calc_to_px_without_rounding(
+ length_percentage.calculated_pointer(),
+ reference,
+ ))
+ .to_float();
+ }
+ CssPixels::nearest_value_for(reference.to_double() * length_percentage.as_fraction()).to_float()
+}
+
+fn resolved_transform_to_matrix(
+ entry: &ComputedResolvedTransform,
+ reference_width: CssPixels,
+ reference_height: CssPixels,
+) -> FloatMatrix4x4 {
+ if entry.is_translate {
+ return translation_matrix(
+ resolved_translate_axis_px(entry.x_px, &entry.x_percentage, reference_width),
+ resolved_translate_axis_px(entry.y_px, &entry.y_percentage, reference_height),
+ entry.z_px,
+ );
+ }
+ let mut elements = [[0.0f32; 4]; 4];
+ for (index, value) in entry.matrix.into_iter().enumerate() {
+ elements[index / 4][index % 4] = value;
+ }
+ FloatMatrix4x4 { elements }
+}
+
+pub(crate) fn multiply_transform_functions(
+ mut matrix: FloatMatrix4x4,
+ entries: &[ComputedResolvedTransform],
+ reference_box: CssPixelRect,
+) -> FloatMatrix4x4 {
+ for entry in entries {
+ matrix = matrix.multiplied(resolved_transform_to_matrix(
+ entry,
+ reference_box.width,
+ reference_box.height,
+ ));
+ }
+ matrix
+}
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
index f48ba7e333b..9d514d91763 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
@@ -13,10 +13,11 @@ use crate::layout::geometry::{Frame, Sides};
use crate::layout::inline_content::{InlineContent, PhysicalPlane};
use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};
use crate::layout::node_facts::{self, NodeShape};
+use crate::layout::used_transform::TransformGeometry;
use crate::layout::used_values::FfiCssPixelSize;
use crate::painting::paint_read::{GeometryRead, PaintRead, PaintRow};
use crate::painting::scrollable_overflow::physical_overflow_directions_of_style;
-use crate::painting::visual_context::node_values::{self, TransformGeometry};
+use crate::painting::visual_context::node_values;
use crate::painting::{node_painting, scroll_snap, style_queries};
use libgfx_rust::AffineTransform;
diff --git a/Libraries/LibWeb/Rust/src/layout/mod.rs b/Libraries/LibWeb/Rust/src/layout/mod.rs
index 397da7faaeb..6b2bb7bb98b 100644
--- a/Libraries/LibWeb/Rust/src/layout/mod.rs
+++ b/Libraries/LibWeb/Rust/src/layout/mod.rs
@@ -71,6 +71,7 @@ mod tree_builder;
pub(crate) mod tree_mutation;
pub(crate) mod tree_update_marks;
mod update_layout;
+pub(crate) mod used_transform;
pub mod used_values;
pub(crate) mod viewport_propagation;
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index 6648aabdfa7..baac0220d68 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -1835,7 +1835,7 @@ impl<'pass> SvgFormattingContext<'pass> {
additional_element_transform: FfiAffineTransform,
) -> FfiAffineTransform {
let style = self.style(node);
- let matrix = crate::painting::visual_context::node_values::multiply_transform_functions(
+ let matrix = crate::css::transform_functions::multiply_transform_functions(
libgfx_rust::FloatMatrix4x4::identity(),
style.transform().resolved_transforms.as_slice(),
CssPixelRect::default(),
diff --git a/Libraries/LibWeb/Rust/src/layout/used_transform.rs b/Libraries/LibWeb/Rust/src/layout/used_transform.rs
new file mode 100644
index 00000000000..ae7d4d76c15
--- /dev/null
+++ b/Libraries/LibWeb/Rust/src/layout/used_transform.rs
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use crate::css::computed_value_types::ComputedStyleValueHandle;
+use crate::css::computed_value_views::ComputedValuesView;
+use crate::css::css_enums;
+use crate::css::css_pixels::{CssPixelPoint, CssPixelRect, CssPixels};
+use crate::css::transform_functions::multiply_transform_functions;
+use libgfx_rust::{AffineTransform, FloatMatrix4x4, affine_to_matrix, translation_matrix};
+
+/// The boxes of a box that its transform reference box and offset path are taken from, in one coordinate space.
+pub(crate) trait TransformGeometry {
+ fn content_box(&self) -> CssPixelRect;
+ fn border_box(&self) -> CssPixelRect;
+ fn scrollable_overflow_rect(&self) -> Option<CssPixelRect>;
+ fn nearest_svg_viewport_user_rect(&self) -> Option<CssPixelRect>;
+ fn containing_block_box(&self, coord_box: u16) -> CssPixelRect;
+ fn svg_additional_element_transform(&self) -> Option<AffineTransform>;
+}
+
+pub(crate) fn transform_reference_box_of(
+ style: ComputedValuesView<'_>,
+ is_svg: bool,
+ geometry: &impl TransformGeometry,
+) -> CssPixelRect {
+ use css_enums::transform_box::{BORDER_BOX, CONTENT_BOX, FILL_BOX, STROKE_BOX, VIEW_BOX};
+ let mut transform_box = style.transform().transform_box;
+ if is_svg {
+ transform_box = match transform_box {
+ CONTENT_BOX => FILL_BOX,
+ BORDER_BOX => STROKE_BOX,
+ other => other,
+ };
+ } else {
+ transform_box = match transform_box {
+ FILL_BOX => CONTENT_BOX,
+ STROKE_BOX | VIEW_BOX => BORDER_BOX,
+ other => other,
+ };
+ }
+ match transform_box {
+ CONTENT_BOX | FILL_BOX => geometry.content_box(),
+ VIEW_BOX => geometry
+ .nearest_svg_viewport_user_rect()
+ .unwrap_or_else(|| geometry.border_box()),
+ _ => geometry.border_box(),
+ }
+}
+
+/// A box's transformation matrix, about its transform origin.
+pub(crate) struct UsedTransform {
+ /// The matrix in the coordinate space whose origin is the transform origin.
+ pub(crate) matrix: FloatMatrix4x4,
+ /// The transform origin, in the coordinate space of the reference box.
+ pub(crate) origin: CssPixelPoint,
+}
+
+// https://drafts.csswg.org/css-transforms-2/#ctm
+/// The transformation matrix of a box with `style`, against its transform reference box. `offset_transform` gives the
+/// transform of the box's offset path for its transform origin within the reference box, and `additional_transform`
+/// is a <use> element's translation.
+pub(crate) fn transform_about_origin(
+ style: ComputedValuesView<'_>,
+ reference_box: CssPixelRect,
+ offset_transform: impl FnOnce((f64, f64)) -> Option<FloatMatrix4x4>,
+ additional_transform: Option<AffineTransform>,
+) -> UsedTransform {
+ let transform_values = style.transform();
+ // The transformation matrix is computed from the transform, transform-origin, translate, rotate, scale, and
+ // offset properties as follows:
+ let origin_lp = |handle: &ComputedStyleValueHandle| {
+ handle
+ .length_percentage()
+ .expect("computed transform-origin lost its style value")
+ };
+ let origin_x = origin_lp(&transform_values.transform_origin_x).to_px(reference_box.width);
+ let origin_y = origin_lp(&transform_values.transform_origin_y).to_px(reference_box.height);
+ let origin_z = origin_lp(&transform_values.transform_origin_z)
+ .to_px(CssPixels::from_raw(0))
+ .to_float();
+
+ // 1. Start with the identity matrix.
+ // 2. Translate by the computed X, Y, and Z values of transform-origin.
+ // 3. Translate by the computed X, Y, and Z values of translate.
+ // 4. Rotate by the computed <angle> about the specified axis of rotate.
+ // 5. Scale by the computed X, Y, and Z values of scale.
+ // 6. Translate and rotate by the transform specified by offset.
+ // 7. Multiply by each of the transform functions in transform from left to right.
+ // NB: The resolved transform list carries translate, rotate, scale, and the
+ // transform functions pre-lowered in exactly that order.
+ let resolved_transforms = transform_values.resolved_transforms.as_slice();
+ let mut matrix = translation_matrix(0.0, 0.0, origin_z);
+ if transform_values.has_offset_path {
+ let individual_transform_count = [
+ &transform_values.translate,
+ &transform_values.rotate,
+ &transform_values.scale,
+ ]
+ .into_iter()
+ .filter(|handle| !handle.pointer.is_null())
+ .count();
+ let (individual_transforms, transform_functions) = resolved_transforms.split_at(individual_transform_count);
+ matrix = multiply_transform_functions(matrix, individual_transforms, reference_box);
+ if let Some(offset_transform) = offset_transform((origin_x.to_double(), origin_y.to_double())) {
+ matrix = matrix.multiplied(offset_transform);
+ }
+ matrix = multiply_transform_functions(matrix, transform_functions, reference_box);
+ } else {
+ matrix = multiply_transform_functions(matrix, resolved_transforms, reference_box);
+ }
+
+ // The x and y properties of <use> define an additional translation applied after any
+ // transformations specified with other properties.
+ if let Some(additional) = additional_transform {
+ matrix = matrix.multiplied(affine_to_matrix(additional));
+ }
+
+ // 8. Translate by the negated computed X, Y and Z values of transform-origin.
+ matrix = matrix.multiplied(translation_matrix(0.0, 0.0, -origin_z));
+ UsedTransform {
+ matrix,
+ origin: CssPixelPoint {
+ x: reference_box.x + origin_x,
+ y: reference_box.y + origin_y,
+ },
+ }
+}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
index 1a1cb4e85dc..49184741368 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
@@ -404,7 +404,7 @@ fn pattern_paint_style<O: Observer>(
pattern_box,
);
Some(
- crate::painting::visual_context::node_values::multiply_transform_functions(
+ crate::css::transform_functions::multiply_transform_functions(
libgfx_rust::FloatMatrix4x4::identity(),
&pattern.css_transform,
reference_box,
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs
index addfad2e6b2..8a96a06fc04 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs
@@ -14,6 +14,7 @@ use crate::css::style_value::BorderRadiusPayload;
use crate::css::style_value::StyleValueData;
use crate::layout::node_data::NodeSlotId;
use crate::layout::node_facts::{self, NodeShape};
+use crate::layout::used_transform::{TransformGeometry, transform_about_origin, transform_reference_box_of};
use crate::painting::border_radii::BorderRadii;
use crate::painting::display_list::device_pixels::DevicePixelConverter;
use crate::painting::host::FfiVisualContextTreeInputs;
@@ -51,15 +52,6 @@ pub(crate) fn visual_viewport_transform_data(inputs: &FfiVisualContextTreeInputs
}
}
-pub(crate) trait TransformGeometry {
- fn content_box(&self) -> CssPixelRect;
- fn border_box(&self) -> CssPixelRect;
- fn scrollable_overflow_rect(&self) -> Option<CssPixelRect>;
- fn nearest_svg_viewport_user_rect(&self) -> Option<CssPixelRect>;
- fn containing_block_box(&self, coord_box: u16) -> CssPixelRect;
- fn svg_additional_element_transform(&self) -> Option<AffineTransform>;
-}
-
pub(crate) struct CommittedTransformGeometry<'arena, Arena> {
pub(crate) arena: &'arena Arena,
pub(crate) node: NodeSlotId,
@@ -126,83 +118,6 @@ pub(crate) fn transform_reference_box(
)
}
-pub(crate) fn transform_reference_box_of(
- style: ComputedValuesView<'_>,
- is_svg: bool,
- geometry: &impl TransformGeometry,
-) -> CssPixelRect {
- use css_enums::transform_box::{BORDER_BOX, CONTENT_BOX, FILL_BOX, STROKE_BOX, VIEW_BOX};
- let mut transform_box = style.transform().transform_box;
- if is_svg {
- transform_box = match transform_box {
- CONTENT_BOX => FILL_BOX,
- BORDER_BOX => STROKE_BOX,
- other => other,
- };
- } else {
- transform_box = match transform_box {
- FILL_BOX => CONTENT_BOX,
- STROKE_BOX | VIEW_BOX => BORDER_BOX,
- other => other,
- };
- }
- match transform_box {
- CONTENT_BOX | FILL_BOX => geometry.content_box(),
- VIEW_BOX => geometry
- .nearest_svg_viewport_user_rect()
- .unwrap_or_else(|| geometry.border_box()),
- _ => geometry.border_box(),
- }
-}
-
-fn resolved_translate_axis_px(px: f32, percentage: &ComputedStyleValueHandle, reference: CssPixels) -> f32 {
- let Some(length_percentage) = percentage.length_percentage() else {
- return px;
- };
- if length_percentage.is_calculated() {
- return CssPixels::nearest_value_for(crate::css::computed_value_views::resolve_calc_to_px_without_rounding(
- length_percentage.calculated_pointer(),
- reference,
- ))
- .to_float();
- }
- CssPixels::nearest_value_for(reference.to_double() * length_percentage.as_fraction()).to_float()
-}
-
-fn resolved_transform_to_matrix(
- entry: &crate::css::computed_value_types::ComputedResolvedTransform,
- reference_width: CssPixels,
- reference_height: CssPixels,
-) -> libgfx_rust::FloatMatrix4x4 {
- if entry.is_translate {
- return translation_matrix(
- resolved_translate_axis_px(entry.x_px, &entry.x_percentage, reference_width),
- resolved_translate_axis_px(entry.y_px, &entry.y_percentage, reference_height),
- entry.z_px,
- );
- }
- let mut elements = [[0.0f32; 4]; 4];
- for (index, value) in entry.matrix.into_iter().enumerate() {
- elements[index / 4][index % 4] = value;
- }
- libgfx_rust::FloatMatrix4x4 { elements }
-}
-
-pub(crate) fn multiply_transform_functions(
- mut matrix: libgfx_rust::FloatMatrix4x4,
- entries: &[crate::css::computed_value_types::ComputedResolvedTransform],
- reference_box: CssPixelRect,
-) -> libgfx_rust::FloatMatrix4x4 {
- for entry in entries {
- matrix = matrix.multiplied(resolved_transform_to_matrix(
- entry,
- reference_box.width,
- reference_box.height,
- ));
- }
- matrix
-}
-
// https://drafts.csswg.org/css-transforms-2/#ctm
pub(crate) fn compute_transform(
layout_arena: &impl PaintRead,
@@ -240,72 +155,29 @@ pub(crate) fn compute_transform_against(
return None;
}
- // The transformation matrix is computed from the transform, transform-origin, translate, rotate, scale, and
- // offset properties as follows:
let reference_box = transform_reference_box_of(style, node_painting::is_svg(node_kind), geometry);
- let origin_lp = |handle: &ComputedStyleValueHandle| {
- handle
- .length_percentage()
- .expect("computed transform-origin lost its style value")
- };
- let origin_x = origin_lp(&transform_values.transform_origin_x).to_px(reference_box.width);
- let origin_y = origin_lp(&transform_values.transform_origin_y).to_px(reference_box.height);
- let origin_z = origin_lp(&transform_values.transform_origin_z)
- .to_px(CssPixels::from_raw(0))
- .to_float();
-
- // 1. Start with the identity matrix.
- // 2. Translate by the computed X, Y, and Z values of transform-origin.
- // 3. Translate by the computed X, Y, and Z values of translate.
- // 4. Rotate by the computed <angle> about the specified axis of rotate.
- // 5. Scale by the computed X, Y, and Z values of scale.
- // 6. Translate and rotate by the transform specified by offset.
- // 7. Multiply by each of the transform functions in transform from left to right.
- // NB: The resolved transform list carries translate, rotate, scale, and the
- // transform functions pre-lowered in exactly that order.
- let resolved_transforms = transform_values.resolved_transforms.as_slice();
- let mut matrix = translation_matrix(0.0, 0.0, origin_z);
- if transform_values.has_offset_path {
- let individual_transform_count = [
- &transform_values.translate,
- &transform_values.rotate,
- &transform_values.scale,
- ]
- .into_iter()
- .filter(|handle| !handle.pointer.is_null())
- .count();
- let (individual_transforms, transform_functions) = resolved_transforms.split_at(individual_transform_count);
- matrix = multiply_transform_functions(matrix, individual_transforms, reference_box);
- if let Some(offset_transform) = super::offset_transform::compute_offset_transform(
- style,
- layout_arena,
- node,
- geometry,
- reference_box,
- (origin_x.to_double(), origin_y.to_double()),
- ) {
- matrix = matrix.multiplied(offset_transform);
- }
- matrix = multiply_transform_functions(matrix, transform_functions, reference_box);
- } else {
- matrix = multiply_transform_functions(matrix, resolved_transforms, reference_box);
- }
-
- // The x and y properties of <use> define an additional translation applied after any
- // transformations specified with other properties.
- if let Some(additional) = additional_element_transform {
- matrix = matrix.multiplied(affine_to_matrix(additional));
- }
-
- // 8. Translate by the negated computed X, Y and Z values of transform-origin.
- matrix = matrix.multiplied(translation_matrix(0.0, 0.0, -origin_z));
+ let used = transform_about_origin(
+ style,
+ reference_box,
+ |origin| {
+ super::offset_transform::compute_offset_transform(
+ style,
+ layout_arena,
+ node,
+ geometry,
+ reference_box,
+ origin,
+ )
+ },
+ additional_element_transform,
+ );
let scale = pixel_ratio as f32;
let device_origin = FloatPoint {
- x: (reference_box.x + origin_x).to_float() * scale,
- y: (reference_box.y + origin_y).to_float() * scale,
+ x: used.origin.x.to_float() * scale,
+ y: used.origin.y.to_float() * scale,
};
- let matrix = scale_matrix_for_device_pixels(matrix, scale);
+ let matrix = scale_matrix_for_device_pixels(used.matrix, scale);
let is_invertible = matrix.is_invertible();
let mut bounds = geometry.border_box();
bounds.unite(reference_box);
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/offset_transform.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/offset_transform.rs
index 2aec504ec92..a980c4d5d3d 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_context/offset_transform.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/offset_transform.rs
@@ -159,7 +159,7 @@ pub(crate) fn compute_offset_transform(
style: ComputedValuesView<'_>,
arena: &impl PaintRead,
node: NodeSlotId,
- geometry: &impl super::node_values::TransformGeometry,
+ geometry: &impl crate::layout::used_transform::TransformGeometry,
reference_box: CssPixelRect,
origin: (f64, f64),
) -> Option<FloatMatrix4x4> {