Truly independent web browser
LibWeb: Resolve transforms and offset paths against given box geometry
Aliaksandr Kalenik743e8414d609
diff --git a/Libraries/LibWeb/Rust/src/painting/scrollable_overflow.rs b/Libraries/LibWeb/Rust/src/painting/scrollable_overflow.rsindex cf7340dc4ed..5c5ae70b9a7 100644--- a/Libraries/LibWeb/Rust/src/painting/scrollable_overflow.rs+++ b/Libraries/LibWeb/Rust/src/painting/scrollable_overflow.rs@@ -68,13 +68,18 @@ struct AxisDirection {pub(crate) fn physical_overflow_directions(layout_arena: &LayoutNodeArena,node: NodeSlotId,+) -> PhysicalOverflowDirections {+ layout_arena.node_style_if_live(node).map_or_else(+ PhysicalOverflowDirections::default,+ physical_overflow_directions_of_style,+ )+}++pub(crate) fn physical_overflow_directions_of_style(+ style: crate::css::computed_value_views::ComputedValuesView<'_>,) -> PhysicalOverflowDirections {// A scrolling box of a viewport or element has two overflow directions, which are the block-end and inline-end// directions for that viewport or element.- let Some(style) = layout_arena.node_style_if_live(node) else {- return PhysicalOverflowDirections::default();- };-let mut inline_axis = AxisDirection {is_horizontal: style.writing_mode() == writing_mode::HORIZONTAL_TB,is_reverse: style.inline_axis_is_reverse(),diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rsindex bc53a014f87..5db9db8f12b 100644--- a/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs@@ -49,14 +49,84 @@ pub(crate) fn visual_viewport_transform_data(inputs: &FfiVisualContextTreeInputs}}+pub(crate) trait TransformGeometry {+ fn content_box(&self) -> CssPixelRect;+ fn border_box(&self) -> 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,+}++impl<Arena: PaintRead> TransformGeometry for CommittedTransformGeometry<'_, Arena> {+ fn content_box(&self) -> CssPixelRect {+ paintable_geometry::absolute_rect(self.arena, self.node)+ }++ fn border_box(&self) -> CssPixelRect {+ paintable_geometry::absolute_border_box_rect(self.arena, self.node)+ }++ fn nearest_svg_viewport_user_rect(&self) -> Option<CssPixelRect> {+ crate::painting::svg_viewport::nearest_svg_viewport_user_rect(self.arena, self.node).map(|rect| {+ CssPixelRect::new(+ CssPixels::default(),+ CssPixels::default(),+ CssPixels::nearest_value_for(rect.width as f64),+ CssPixels::nearest_value_for(rect.height as f64),+ )+ })+ }++ fn containing_block_box(&self, coord_box: u16) -> CssPixelRect {+ use crate::css::css_enums::keyword;+ let Some(containing_block) = self.arena.node_containing_block_if_live(self.node) else {+ return CssPixelRect::default();+ };+ match coord_box {+ keyword::CONTENT_BOX | keyword::FILL_BOX => paintable_geometry::absolute_rect(self.arena, containing_block),+ keyword::PADDING_BOX => paintable_geometry::absolute_padding_box_rect(self.arena, containing_block),+ _ => paintable_geometry::absolute_border_box_rect(self.arena, containing_block),+ }+ }++ fn svg_additional_element_transform(&self) -> Option<AffineTransform> {+ let kind = self.arena.node_kind_if_live(self.node)?;+ if !style_queries::kind_is_svg_element_box(kind) {+ return None;+ }+ paintable_geometry::committed_svg_additional_element_transform(self.arena, self.node).map(Into::into)+ }+}+pub(crate) fn transform_reference_box(style: ComputedValuesView<'_>,layout_arena: &impl PaintRead,slot: NodeSlotId,+) -> CssPixelRect {+ let is_svg = layout_arena.node_kind_if_live(slot).is_some_and(node_painting::is_svg);+ transform_reference_box_of(+ style,+ is_svg,+ &CommittedTransformGeometry {+ arena: layout_arena,+ node: slot,+ },+ )+}++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 layout_arena.node_kind_if_live(slot).is_some_and(node_painting::is_svg) {+ if is_svg {transform_box = match transform_box {CONTENT_BOX => FILL_BOX,BORDER_BOX => STROKE_BOX,@@ -70,18 +140,11 @@ pub(crate) fn transform_reference_box(};}match transform_box {- CONTENT_BOX | FILL_BOX => paintable_geometry::absolute_rect(layout_arena, slot),- VIEW_BOX => crate::painting::svg_viewport::nearest_svg_viewport_user_rect(layout_arena, slot)- .map(|rect| {- CssPixelRect::new(- CssPixels::default(),- CssPixels::default(),- CssPixels::nearest_value_for(rect.width as f64),- CssPixels::nearest_value_for(rect.height as f64),- )- })- .unwrap_or_else(|| paintable_geometry::absolute_border_box_rect(layout_arena, slot)),- _ => paintable_geometry::absolute_border_box_rect(layout_arena, slot),+ CONTENT_BOX | FILL_BOX => geometry.content_box(),+ VIEW_BOX => geometry+ .nearest_svg_viewport_user_rect()+ .unwrap_or_else(|| geometry.border_box()),+ _ => geometry.border_box(),}}@@ -138,16 +201,28 @@ pub(crate) fn compute_transform(layout_arena: &impl PaintRead,node: NodeSlotId,pixel_ratio: f64,+) -> Option<(TransformData, bool)> {+ compute_transform_against(+ layout_arena,+ node,+ &CommittedTransformGeometry {+ arena: layout_arena,+ node,+ },+ pixel_ratio,+ )+}++pub(crate) fn compute_transform_against(+ layout_arena: &impl PaintRead,+ node: NodeSlotId,+ geometry: &impl TransformGeometry,+ pixel_ratio: f64,) -> Option<(TransformData, bool)> {let row = layout_arena.node(node)?;let (style, node_kind) = (row.style()?, row.kind());- let additional_element_transform = if style_queries::kind_is_svg_element_box(node_kind) {- crate::painting::paintable_geometry::committed_svg_additional_element_transform(layout_arena, node)- .map(Into::into)- } else {- None- };+ let additional_element_transform = geometry.svg_additional_element_transform();let transform_values = style.transform();let style_has_transform = transform_values.resolved_transforms.length != 0 || transform_values.has_offset_path;@@ -160,7 +235,7 @@ pub(crate) fn compute_transform(// The transformation matrix is computed from the transform, transform-origin, translate, rotate, scale, and// offset properties as follows:- let reference_box = transform_reference_box(style, layout_arena, node);+ let reference_box = transform_reference_box_of(style, node_painting::is_svg(node_kind), geometry);let origin_lp = |handle: &ComputedStyleValueHandle| {handle.length_percentage()@@ -198,6 +273,7 @@ pub(crate) fn compute_transform(style,layout_arena,node,+ geometry,reference_box,(origin_x.to_double(), origin_y.to_double()),) {diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/offset_transform.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/offset_transform.rsindex 184181732c3..9986d2889a5 100644--- a/Libraries/LibWeb/Rust/src/painting/visual_context/offset_transform.rs+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/offset_transform.rs@@ -13,7 +13,7 @@ use crate::css::css_pixels::{CssPixelRect, CssPixels};use crate::css::style_value::{BasicShapeData, OwnedBasicShapeData, RetainedStyleValueData, StyleValueData};use crate::layout::node_data::NodeSlotId;use crate::painting::paint_read::PaintRead;-use crate::painting::{paintable_geometry, style_queries};+use crate::painting::style_queries;use libgfx_rust::{FloatMatrix4x4, FloatPoint, rotation_matrix, translation_matrix};use super::basic_shapes::{basic_shape_kind, position_resolved};@@ -50,21 +50,6 @@ fn position_value_at(point: (f64, f64), rect: CssPixelRect) -> StyleValueData {}}-// https://drafts.csswg.org/motion-1/#offset-path-property-// In CSS contexts, the boxes being referenced are from the element that establishes the containing block for this-// element.-// FIXME: In SVG contexts, all values behave as view-box.-fn containing_block_reference_box(arena: &impl PaintRead, node: NodeSlotId, coord_box: u16) -> CssPixelRect {- let Some(containing_block) = arena.node_containing_block_if_live(node) else {- return CssPixelRect::default();- };- match coord_box {- keyword::CONTENT_BOX | keyword::FILL_BOX => paintable_geometry::absolute_rect(arena, containing_block),- keyword::PADDING_BOX => paintable_geometry::absolute_padding_box_rect(arena, containing_block),- _ => paintable_geometry::absolute_border_box_rect(arena, containing_block),- }-}-/// inset(0 round X), where X is the border-radius of the containing block of `node`.fn containing_block_inset_shape(arena: &impl PaintRead, node: NodeSlotId) -> OwnedBasicShapeData {let zero = || {@@ -168,6 +153,7 @@ pub(crate) fn compute_offset_transform(style: ComputedValuesView<'_>,arena: &impl PaintRead,node: NodeSlotId,+ geometry: &impl super::node_values::TransformGeometry,reference_box: CssPixelRect,origin: (f64, f64),) -> Option<FloatMatrix4x4> {@@ -189,8 +175,12 @@ pub(crate) fn compute_offset_transform(path => (Some(path), keyword::BORDER_BOX),};- let border_box = paintable_geometry::absolute_border_box_rect(arena, node);- let containing_box = containing_block_reference_box(arena, node, coord_box);+ // https://drafts.csswg.org/motion-1/#offset-path-property+ // In CSS contexts, the boxes being referenced are from the element that establishes the containing block for this+ // element.+ // FIXME: In SVG contexts, all values behave as view-box.+ let border_box = geometry.border_box();+ let containing_box = geometry.containing_block_box(coord_box);// https://drafts.csswg.org/motion-1/#offset-position-propertylet offset_position = style_queries::handle_value(&transform_values.offset_position);