Skip to content

Truly independent web browser

LibWeb: Clip CSS mask layers to the element’s rounded corners

sideshowbarker4288a5da4ad1
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
index a61bdb3db97..14903d77845 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
@@ -329,35 +329,55 @@ fn paint_background_layers<O: Observer>(
painted_mask_layer = true;
}
+ let mut layer_inline_clips = Vec::new();
+ if !is_root_element {
+ let unshrunken_clip_float_rect = unshrunken_clip_rect.to_float();
+ if layer.clip_follows_corners {
+ let corner_radii = clip_box.radii.corners_unconditionally(&converter);
+ if let Some(clip) = contour_clip(css_clip_rect) {
+ layer_inline_clips.push(clip);
+ } else if corner_radii.has_any_radius() {
+ layer_inline_clips.push(PendingInlineClip::intersecting_rounded_rect(
+ unshrunken_clip_float_rect,
+ corner_radii,
+ ));
+ }
+ }
+ layer_inline_clips.push(PendingInlineClip::intersecting_float_rect(unshrunken_clip_float_rect));
+ }
+
let layer_erases_uncovered_destination =
operator_erases_destination_outside_the_drawn_geometry(compositing_and_blending_operator);
if layer_erases_uncovered_destination {
// https://drafts.fxtf.org/css-masking-1/#the-mask-composite
// The composite must erase the accumulated mask outside the drawn geometry, but only
// within the layer's clip: the layer plays as an isolated group composited with the
- // operator, and the command's own clip bounds the erase.
- let group = recorder.recorder.begin_isolated_group();
- recorder.trace_paint(Operation::Named(Some(paintable), "mask-layer"), |recorder| {
- if layer.image.is_some() {
- paint_image_layer(
- recorder,
- paintable,
- layer,
- inputs.image_rendering,
- css_clip_rect,
- clip_rect,
- CompositingAndBlendingOperator::Normal,
- backdrop,
- inputs.visible_device_rect,
- );
- }
+ // operator, and the command's own clip bounds the erase. The group takes the layer's
+ // inline clips as its own, so a rounded clip bounds the erase to the box's curve.
+ recorder.record_with_inline_clips(&layer_inline_clips, |recorder| {
+ let group = recorder.recorder.begin_isolated_group();
+ recorder.trace_paint(Operation::Named(Some(paintable), "mask-layer"), |recorder| {
+ if layer.image.is_some() {
+ paint_image_layer(
+ recorder,
+ paintable,
+ layer,
+ inputs.image_rendering,
+ css_clip_rect,
+ clip_rect,
+ CompositingAndBlendingOperator::Normal,
+ backdrop,
+ inputs.visible_device_rect,
+ );
+ }
+ });
+ recorder.recorder.finish_isolated_group(
+ group,
+ unshrunken_clip_rect.to_float(),
+ compositing_and_blending_operator,
+ MaskKind::Alpha,
+ );
});
- recorder.recorder.finish_isolated_group(
- group,
- unshrunken_clip_rect.to_float(),
- compositing_and_blending_operator,
- MaskKind::Alpha,
- );
continue;
}
@@ -377,23 +397,7 @@ fn paint_background_layers<O: Observer>(
}
};
- if is_root_element {
- paint_layer(recorder);
- } else {
- let unshrunken_clip_float_rect = unshrunken_clip_rect.to_float();
- let corner_radii = clip_box.radii.corners_unconditionally(&converter);
- let mut layer_inline_clips = Vec::new();
- if let Some(clip) = contour_clip(css_clip_rect) {
- layer_inline_clips.push(clip);
- } else if corner_radii.has_any_radius() {
- layer_inline_clips.push(PendingInlineClip::intersecting_rounded_rect(
- unshrunken_clip_float_rect,
- corner_radii,
- ));
- }
- layer_inline_clips.push(PendingInlineClip::intersecting_float_rect(unshrunken_clip_float_rect));
- recorder.record_with_inline_clips(&layer_inline_clips, paint_layer);
- }
+ recorder.record_with_inline_clips(&layer_inline_clips, paint_layer);
}
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs
index d554b9e2807..4c2f65646be 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs
@@ -51,6 +51,8 @@ struct ComputedLayer<'a> {
image: Option<LayerImageSource<'a>>,
attachment: u8,
clip: u8,
+ // False for a mask layer with mask-clip: no-clip, which isn't clipped to its box's curve.
+ clip_follows_corners: bool,
origin: u8,
position_x: LengthPercentageRef<'a>,
position_y: LengthPercentageRef<'a>,
@@ -65,6 +67,7 @@ pub(crate) struct ResolvedBackgroundLayer<'a> {
pub image: Option<LayerImageSource<'a>>,
pub attachment: u8,
pub clip: u8,
+ pub clip_follows_corners: bool,
pub position_x: CssPixels,
pub position_y: CssPixels,
pub background_positioning_area: CssPixelRect,
@@ -386,6 +389,7 @@ fn computed_background_layers(style: ComputedValuesView<'_>, facts_owner: NodeSl
.expect("computed background-blend-mode holds a blend keyword"),
clip: css_enums::keyword_to_background_box(keyword_of(cycled(&clip_items, index)))
.expect("computed background-clip holds a box keyword"),
+ clip_follows_corners: true,
origin: css_enums::keyword_to_background_box(keyword_of(cycled(&origin_items, index)))
.expect("computed background-origin holds a box keyword"),
position_x: edge_offset(cycled(&position_x_items, index)),
@@ -447,8 +451,8 @@ fn computed_mask_layers(style: ComputedValuesView<'_>, facts_owner: NodeSlotId)
let StyleValueData::Position { edge_x, edge_y } = cycled(&position_items, index) else {
unreachable!("computed mask-position holds a position value");
};
- let clip = css_enums::keyword_to_background_box(keyword_of(cycled(&clip_items, index)))
- .unwrap_or(css_enums::background_box::BORDER_BOX);
+ let clip_keyword = keyword_of(cycled(&clip_items, index));
+ let clip = css_enums::keyword_to_background_box(clip_keyword).unwrap_or(css_enums::background_box::BORDER_BOX);
let origin = css_enums::keyword_to_background_box(keyword_of(cycled(&origin_items, index)))
.unwrap_or(css_enums::background_box::BORDER_BOX);
layers.push(ComputedLayer {
@@ -456,6 +460,9 @@ fn computed_mask_layers(style: ComputedValuesView<'_>, facts_owner: NodeSlotId)
attachment: css_enums::background_attachment::SCROLL,
blend_mode: mix_blend_mode::NORMAL,
clip,
+ // FIXME: A mask layer with mask-clip: no-clip shouldn't be clipped at all, but it's still clipped to the
+ // border box's rectangle.
+ clip_follows_corners: clip_keyword != css_enums::keyword::NO_CLIP,
origin,
position_x: edge_offset(edge_x.data()),
position_y: edge_offset(edge_y.data()),
@@ -525,6 +532,7 @@ fn resolve_layers<'a, O: Observer>(
image: None,
attachment: 0,
clip: layer.clip,
+ clip_follows_corners: layer.clip_follows_corners,
position_x: CssPixels::from_raw(0),
position_y: CssPixels::from_raw(0),
background_positioning_area: CssPixelRect::default(),
@@ -702,6 +710,7 @@ fn resolve_layers<'a, O: Observer>(
image: Some(image),
attachment: layer.attachment,
clip: layer.clip,
+ clip_follows_corners: layer.clip_follows_corners,
position_x,
position_y,
background_positioning_area,
diff --git a/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs b/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs
index 890b1c35d82..a50d91afe16 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs
@@ -17,7 +17,7 @@ use crate::painting::record::{PaintPhase, PaintRecorder};
use crate::painting::visual_context::build::{
BoxFacts, compute_svg_viewport_transform_data, svg_viewport_transform_of,
};
-use crate::painting::visual_context::node_values::{MaskLayerPresenceEntry, mask_layer_presence};
+use crate::painting::visual_context::node_values::{MaskLayerPresenceEntry, border_radii_data, mask_layer_presence};
use crate::painting::visual_context::{ClipData, EffectNodeData, EffectNodeIndex, MaskData, MaskLayerOrigin};
use libgfx_rust::path::Path;
use libgfx_rust::{AffineTransform, CompositingAndBlendingOperator, FloatPoint, FloatRect, MaskKind, multiply_affine};
@@ -246,7 +246,10 @@ impl<O: Observer> PaintRecorder<'_, O> {
// FIXME: Respect `image-rendering` here.
let inputs = crate::painting::record::paint::background_resolution::BackgroundPaintInputs {
resolved,
- border_radii: crate::painting::border_radii::BorderRadii::default(),
+ // https://drafts.csswg.org/css-backgrounds-3/#corner-clipping
+ // Although border images are not affected by border-radius, other effects that clip painting or event
+ // handling to the border, padding, or content edge must clip to their respective curves.
+ border_radii: border_radii_data(style, layout_arena, paintable),
image_rendering: crate::css::css_enums::image_rendering::AUTO,
is_root_element,
visible_device_rect: None,
diff --git a/Tests/LibWeb/Ref/expected/css-mask-clip-follows-border-radius-ref.html b/Tests/LibWeb/Ref/expected/css-mask-clip-follows-border-radius-ref.html
new file mode 100644
index 00000000000..b2830969b36
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css-mask-clip-follows-border-radius-ref.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<style>
+ * { margin: 0; }
+ div { position: absolute; top: 20px; width: 100px; height: 100px; padding: 10px; border-radius: 50%; }
+ .content { left: 20px; background: green content-box; }
+ .exclude { left: 160px; padding: 0; border: 10px solid green; }
+ .subtract { left: 300px; padding: 0; border: 10px solid green; }
+ .no-clip { left: 440px; width: 120px; height: 120px; padding: 0; border-radius: 0; background: green; }
+</style>
+<div class="content"></div>
+<div class="exclude"></div>
+<div class="subtract"></div>
+<div class="no-clip"></div>
diff --git a/Tests/LibWeb/Ref/input/css-mask-clip-follows-border-radius.html b/Tests/LibWeb/Ref/input/css-mask-clip-follows-border-radius.html
new file mode 100644
index 00000000000..e969d060c60
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/css-mask-clip-follows-border-radius.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/css-mask-clip-follows-border-radius-ref.html" />
+<meta name="fuzzy" content="maxDifference=0-80;totalPixels=0-1600" />
+<style>
+ * { margin: 0; }
+ div { position: absolute; top: 20px; width: 100px; height: 100px; padding: 10px; border-radius: 50%; background: green; }
+ .content { left: 20px; mask: linear-gradient(#fff 0 0) content-box; }
+ .exclude { left: 160px; mask: linear-gradient(#fff 0 0) content-box exclude, linear-gradient(#fff 0 0); }
+ .subtract { left: 300px; mask: linear-gradient(#fff 0 0) content-box subtract, linear-gradient(#fff 0 0); }
+ .no-clip { left: 440px; background: none; mask: linear-gradient(#fff 0 0) no-clip; }
+ .no-clip > div { position: static; padding: 0; border-radius: 0; width: 120px; height: 120px; margin: -10px; }
+</style>
+<div class="content"></div>
+<div class="exclude"></div>
+<div class="subtract"></div>
+<div class="no-clip"><div></div></div>