Skip to content

Truly independent web browser

LibWeb: Resolve the area of an SVG mask during layout

Aliaksandr Kalenikb652687d7c94
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index e7d731672bd..6baecf4529c 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -349,15 +349,6 @@ pub const SVG_GEOMETRY_KIND_LINE: u8 = 5;
pub const SVG_GEOMETRY_KIND_POLYLINE: u8 = 6;
pub const SVG_GEOMETRY_KIND_POLYGON: u8 = 7;
-#[derive(Clone, Copy, Debug, PartialEq)]
-pub(crate) struct SvgMaskAreaFacts {
- pub units_are_object_bounding_box: bool,
- pub x: FfiSvgNumberPercentage,
- pub y: FfiSvgNumberPercentage,
- pub width: FfiSvgNumberPercentage,
- pub height: FfiSvgNumberPercentage,
-}
-
impl FfiSvgNumberPercentage {
pub(crate) fn resolve_relative_to(self, length: f32) -> f32 {
if self.is_percentage {
@@ -2939,24 +2930,65 @@ impl<'pass> SvgFormattingContext<'pass> {
}
fn commit_svg_element_facts(&self, node: Node, facts: SvgElementFacts, attributes: FfiSvgAttributeFacts) {
- let mask_area_facts = (self.node_kind(node) == NodeKind::SVGMaskBox).then_some(SvgMaskAreaFacts {
- units_are_object_bounding_box: attributes.mask_units == SVG_UNITS_OBJECT_BOUNDING_BOX,
- x: attributes.mask_x,
- y: attributes.mask_y,
- width: attributes.mask_width,
- height: attributes.mask_height,
- });
self.update_svg_payload_of(node, |svg| {
let svg = &mut svg.facts;
svg.additional_element_transform =
(!facts.additional_element_transform.is_identity()).then_some(facts.additional_element_transform);
- svg.mask_area_facts = mask_area_facts;
svg.resource_content_units_are_object_bounding_box =
attributes.content_units == SVG_UNITS_OBJECT_BOUNDING_BOX;
svg.author_path_length = attributes.has_path_length.then_some(attributes.path_length);
});
}
+ /// Commits what painting reads of how `resource` applies to the element it is laid out for, whose object bounding
+ /// box is `target_bounding_box`.
+ fn commit_resource_facts_for_target(
+ &self,
+ resource: Node,
+ attributes: &FfiSvgAttributeFacts,
+ target_bounding_box: FloatRect,
+ ) {
+ let mask_area = (self.node_kind(resource) == NodeKind::SVGMaskBox)
+ .then(|| self.mask_area_in_target_user_space(attributes, target_bounding_box));
+ self.update_svg_payload_of(resource, |svg| {
+ svg.facts.object_bounding_box_of_resource_target = Some(target_bounding_box);
+ svg.facts.mask_area_in_target_user_space = mask_area;
+ });
+ }
+
+ /// The masking area of a mask, in the user space of the element it masks, whose object bounding box is
+ /// `target_bounding_box`. Percentages in a userSpaceOnUse masking area resolve against the SVG viewport. A negative
+ /// value or a value of zero disables rendering of the element, which an empty area tells.
+ fn mask_area_in_target_user_space(
+ &self,
+ attributes: &FfiSvgAttributeFacts,
+ target_bounding_box: FloatRect,
+ ) -> FloatRect {
+ let masking_area = if attributes.mask_units == SVG_UNITS_OBJECT_BOUNDING_BOX {
+ let target_bounding_box = css_pixel_rect_of(target_bounding_box);
+ let target_width = target_bounding_box.width.to_float();
+ let target_height = target_bounding_box.height.to_float();
+ FloatRect::new(
+ target_bounding_box.x.to_float() + (attributes.mask_x.value * target_width),
+ target_bounding_box.y.to_float() + (attributes.mask_y.value * target_height),
+ attributes.mask_width.value * target_width,
+ attributes.mask_height.value * target_height,
+ )
+ } else {
+ let viewport = self.viewport_user_rect;
+ FloatRect::new(
+ attributes.mask_x.resolve_relative_to(viewport.width),
+ attributes.mask_y.resolve_relative_to(viewport.height),
+ attributes.mask_width.resolve_relative_to(viewport.width),
+ attributes.mask_height.resolve_relative_to(viewport.height),
+ )
+ };
+ if masking_area.is_empty() {
+ return FloatRect::default();
+ }
+ masking_area
+ }
+
/// The geometry of `node`, whose fill covers `object_bounding_box` and whose box is `stroke_bounding_box`, in this
/// context's user units.
fn geometry_of(&self, node: Node, object_bounding_box: FloatRect, stroke_bounding_box: FloatRect) -> SvgGeometry {
@@ -3321,8 +3353,14 @@ impl<'pass> SvgFormattingContext<'pass> {
return geometry;
}
let facts = self.svg_facts(graphics_box);
- self.commit_svg_element_facts(graphics_box, facts, self.svg_attributes(graphics_box));
+ let attributes = self.svg_attributes(graphics_box);
+ self.commit_svg_element_facts(graphics_box, facts, attributes);
let kind = self.node_kind(graphics_box);
+ // NB: A mask box laid out as a group is the mask of the resource box it is a child of, whose object bounding
+ // box is the viewport that box gives its content.
+ if kind == NodeKind::SVGMaskBox {
+ self.commit_resource_facts_for_target(graphics_box, &attributes, self.viewport_user_rect);
+ }
// https://svgwg.org/svg2-draft/struct.html#GroupsOverview
// container element
@@ -3580,6 +3618,7 @@ impl<'pass> SvgFormattingContext<'pass> {
// FIXME: Somehow limit <clipPath> contents to: shape elements, <text>, and <use>.
let resource_cells = self.cells_of_svg_box(resource);
self.commit_svg_element_facts(resource, facts, attributes);
+ self.commit_resource_facts_for_target(resource, &attributes, target_bounding_box);
let (width, height) = if kind == NodeKind::SVGPatternBox && attributes.has_active_view_box {
if attributes.pattern_units == SVG_UNITS_USER_SPACE_ON_USE {
diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rs
index 0e1602b9775..36da4248ddb 100644
--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs
+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs
@@ -122,7 +122,12 @@ impl LineDataState {
pub(crate) struct CommittedSvgFacts {
pub(crate) viewport_transform: Option<svg_formatting_context::FfiAffineTransform>,
pub(crate) additional_element_transform: Option<svg_formatting_context::FfiAffineTransform>,
- pub(crate) mask_area_facts: Option<svg_formatting_context::SvgMaskAreaFacts>,
+ /// The object bounding box of the element that a mask, clip path or pattern box was laid out for, in that
+ /// element's user space.
+ pub(crate) object_bounding_box_of_resource_target: Option<libgfx_rust::FloatRect>,
+ /// The masking area of a mask box, in the user space of the element it masks. An empty area disables rendering of
+ /// that element.
+ pub(crate) mask_area_in_target_user_space: Option<libgfx_rust::FloatRect>,
pub(crate) resource_content_units_are_object_bounding_box: bool,
/// The `pathLength` of a geometry element, where its element has one.
pub(crate) author_path_length: Option<f32>,
diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs
index b97f228850a..d672a656b07 100644
--- a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs
@@ -219,12 +219,24 @@ pub(crate) fn committed_svg_additional_element_transform(
})
}
-pub(crate) fn committed_svg_mask_area_facts(
+/// The masking area a mask box committed, in the user space of the element it masks.
+pub(crate) fn committed_svg_mask_area_in_target_user_space(
arena: &impl GeometryRead,
slot: NodeSlotId,
-) -> Option<svg_formatting_context::SvgMaskAreaFacts> {
+) -> Option<libgfx_rust::FloatRect> {
arena.with_committed_fragment_link(slot, |link| {
- link.and_then(|link| link.fragment.svg()?.facts.mask_area_facts)
+ link.and_then(|link| link.fragment.svg()?.facts.mask_area_in_target_user_space)
+ })
+}
+
+/// The object bounding box of the element that a mask, clip path or pattern box was laid out for, as the box committed
+/// it, in that element's user space.
+pub(crate) fn committed_object_bounding_box_of_svg_resource_target(
+ arena: &impl GeometryRead,
+ slot: NodeSlotId,
+) -> Option<libgfx_rust::FloatRect> {
+ arena.with_committed_fragment_link(slot, |link| {
+ link.and_then(|link| link.fragment.svg()?.facts.object_bounding_box_of_resource_target)
})
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs b/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs
index 2a5598943d3..6a7533c2d61 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs
@@ -183,10 +183,6 @@ impl<O: Observer> PaintRecorder<'_, O> {
crate::painting::svg_masking::first_child_paintable_of_kind(self.source, paintable, kind)
}
- fn object_bounding_box_content_units_transform(&self, target: NodeSlotId) -> AffineTransform {
- crate::painting::svg_masking::object_bounding_box_content_units_transform(self.source, target)
- }
-
fn record_referenced_svg_mask_or_clip_content(
&mut self,
target: NodeSlotId,
@@ -208,7 +204,7 @@ impl<O: Observer> PaintRecorder<'_, O> {
resource_box,
);
let mut content_units_transform_in_recorded_space = if content_units_object_bbox {
- self.object_bounding_box_content_units_transform(target)
+ crate::painting::svg_masking::object_bounding_box_content_units_transform(self.source, resource_box)
} else {
AffineTransform::identity()
};
diff --git a/Libraries/LibWeb/Rust/src/painting/svg_masking.rs b/Libraries/LibWeb/Rust/src/painting/svg_masking.rs
index 8e0cbad54c9..43af2fcfd5c 100644
--- a/Libraries/LibWeb/Rust/src/painting/svg_masking.rs
+++ b/Libraries/LibWeb/Rust/src/painting/svg_masking.rs
@@ -25,20 +25,16 @@ pub(crate) fn first_child_paintable_of_kind(
arena.paintable_row_is_populated(node).then_some(node)
}
-/// The object bounding box of `target` in its user space, as layout committed it. A foreign object's box comes from a
-/// run of its own and carries no SVG geometry, so its border box stands in.
-pub(crate) fn target_user_space_object_bounding_box(arena: &impl PaintRead, target: NodeSlotId) -> CssPixelRect {
- paintable_geometry::committed_svg_geometry(arena, target).map_or_else(
- || paintable_geometry::absolute_border_box_rect(arena, target),
- |geometry| css_pixel_rect_of(geometry.object_bounding_box),
- )
-}
-
+/// The transform from objectBoundingBox content units of `resource_box`, a mask, clip path or pattern box, to the user
+/// space of the element it was laid out for.
pub(crate) fn object_bounding_box_content_units_transform(
arena: &impl PaintRead,
- target: NodeSlotId,
+ resource_box: NodeSlotId,
) -> AffineTransform {
- let bounding_box = target_user_space_object_bounding_box(arena, target);
+ let bounding_box = css_pixel_rect_of(
+ paintable_geometry::committed_object_bounding_box_of_svg_resource_target(arena, resource_box)
+ .unwrap_or_default(),
+ );
AffineTransform {
values: [
bounding_box.width.to_float(),
@@ -51,10 +47,8 @@ pub(crate) fn object_bounding_box_content_units_transform(
}
}
-/// The masking area of the mask that `target` references, in the target's user space. Percentages
-/// in a userSpaceOnUse masking area resolve against the SVG viewport. An empty area conveys that
-/// a negative value or a value of zero disables rendering of the element; a mask layer with an
-/// empty area clips the target away entirely.
+/// The masking area of the mask that `target` references, in the target's user space, as layout resolved it. A mask
+/// layer with an empty area clips the target away entirely.
pub(crate) fn mask_area(arena: &impl PaintRead, target: NodeSlotId) -> Option<CssPixelRect> {
if !arena
.node_kind_if_live(target)
@@ -63,38 +57,7 @@ pub(crate) fn mask_area(arena: &impl PaintRead, target: NodeSlotId) -> Option<Cs
return None;
}
let mask_box = first_child_paintable_of_kind(arena, target, NodeKind::SVGMaskBox)?;
- let facts = paintable_geometry::committed_svg_mask_area_facts(arena, mask_box)?;
- let viewport = paintable_geometry::committed_svg_geometry(arena, mask_box)
- .map(|geometry| geometry.viewport_user_rect)
- .unwrap_or_default();
- let target_bounding_box = target_user_space_object_bounding_box(arena, target);
- let masking_area = if facts.units_are_object_bounding_box {
- let target_x = target_bounding_box.x.to_float();
- let target_y = target_bounding_box.y.to_float();
- let target_width = target_bounding_box.width.to_float();
- let target_height = target_bounding_box.height.to_float();
- FloatRect::new(
- target_x + (facts.x.value * target_width),
- target_y + (facts.y.value * target_height),
- facts.width.value * target_width,
- facts.height.value * target_height,
- )
- } else {
- let user_space_masking_area = FloatRect::new(
- facts.x.resolve_relative_to(viewport.width),
- facts.y.resolve_relative_to(viewport.height),
- facts.width.resolve_relative_to(viewport.width),
- facts.height.resolve_relative_to(viewport.height),
- );
- if user_space_masking_area.is_empty() {
- return Some(CssPixelRect::default());
- }
- user_space_masking_area
- };
- if masking_area.is_empty() {
- return Some(CssPixelRect::default());
- }
- Some(css_pixel_rect_of(masking_area))
+ paintable_geometry::committed_svg_mask_area_in_target_user_space(arena, mask_box).map(css_pixel_rect_of)
}
fn committed_local_transform(arena: &impl PaintRead, slot: NodeSlotId) -> AffineTransform {
@@ -201,7 +164,7 @@ pub(crate) fn clip_area(arena: &impl PaintRead, target: NodeSlotId) -> Option<Cs
let mut clip_path_transform = committed_local_transform(arena, clip_box);
if paintable_geometry::committed_svg_resource_content_units_are_object_bounding_box(arena, clip_box) {
clip_path_transform = multiply_affine(
- object_bounding_box_content_units_transform(arena, target),
+ object_bounding_box_content_units_transform(arena, clip_box),
clip_path_transform,
);
}