1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! Modern CSS visual effects: Backdrop filters and arbitrary vector clip-paths.
use strata_primitives::geom::Point;
/// Individual filter operation applied in backdrop-filter or filter pipelines.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FilterOp {
/// Gaussian blur radius in pixels.
Blur(f32),
/// Linear multiplier for element brightness (1.0 = normal).
Brightness(f32),
/// Linear multiplier for element contrast (1.0 = normal).
Contrast(f32),
/// Grayscale conversion fraction (0.0 = full color, 1.0 = full grayscale).
Grayscale(f32),
/// Color inversion fraction (0.0 = normal, 1.0 = completely inverted).
Invert(f32),
/// Alpha transparency fraction (0.0 = fully transparent, 1.0 = fully opaque).
Opacity(f32),
/// Color saturation multiplier (1.0 = normal, 0.0 = desaturated).
Saturate(f32),
/// Sepia toning fraction (0.0 = normal, 1.0 = full sepia).
Sepia(f32),
/// Drop shadow filter operation with offsets, blur radius, and color.
DropShadow {
offset_x: f32,
offset_y: f32,
blur_radius: f32,
color: crate::types::Color,
},
/// Hue rotation in degrees.
HueRotate(f32),
}
/// Composite filter specification applied to an element or underneath it.
pub type FilterList = BackdropFilter;
/// Composite backdrop filter specification applied to pixels underneath the element.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct BackdropFilter {
pub operations: Vec<FilterOp>,
}
impl BackdropFilter {
pub fn new(operations: Vec<FilterOp>) -> Self {
Self { operations }
}
/// Convenience constructor for Gaussian blur backdrop.
pub fn blur(radius: f32) -> Self {
Self {
operations: vec![FilterOp::Blur(radius)],
}
}
pub fn is_empty(&self) -> bool {
self.operations.is_empty()
}
}
/// CSS clip-path geometric masking shape.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum ClipPath {
#[default]
None,
/// Inset rectangular clip mask with optional rounded corners.
Inset {
top: f32,
right: f32,
bottom: f32,
left: f32,
border_radius: f32,
},
/// Circular clip mask with radius and center coordinates.
Circle {
radius: f32,
center_x: f32,
center_y: f32,
},
/// Elliptical clip mask with x/y radii and center coordinates.
Ellipse {
radius_x: f32,
radius_y: f32,
center_x: f32,
center_y: f32,
},
/// Polygon mask defined by a sequence of 2D boundary vertices.
Polygon(Vec<Point<f32>>),
/// Arbitrary SVG path string.
Path(String),
}
impl ClipPath {
pub fn inset(top: f32, right: f32, bottom: f32, left: f32, border_radius: f32) -> Self {
Self::Inset {
top,
right,
bottom,
left,
border_radius,
}
}
pub fn circle(radius: f32, center_x: f32, center_y: f32) -> Self {
Self::Circle {
radius,
center_x,
center_y,
}
}
pub fn ellipse(radius_x: f32, radius_y: f32, center_x: f32, center_y: f32) -> Self {
Self::Ellipse {
radius_x,
radius_y,
center_x,
center_y,
}
}
pub fn polygon(vertices: Vec<Point<f32>>) -> Self {
Self::Polygon(vertices)
}
pub fn path(path_str: impl Into<String>) -> Self {
Self::Path(path_str.into())
}
pub fn is_none(&self) -> bool {
matches!(self, ClipPath::None)
}
/// Returns whether a test point (x, y) falls inside the clipping boundary.
pub fn contains_point(&self, x: f32, y: f32, container_w: f32, container_h: f32) -> bool {
match self {
ClipPath::None => true,
ClipPath::Inset {
top,
right,
bottom,
left,
..
} => {
x >= *left
&& x <= (container_w - *right)
&& y >= *top
&& y <= (container_h - *bottom)
}
ClipPath::Circle {
radius,
center_x,
center_y,
} => {
let dx = x - *center_x;
let dy = y - *center_y;
(dx * dx + dy * dy) <= (radius * radius)
}
ClipPath::Ellipse {
radius_x,
radius_y,
center_x,
center_y,
} => {
if *radius_x <= 0.0 || *radius_y <= 0.0 {
return false;
}
let norm_x = (x - *center_x) / radius_x;
let norm_y = (y - *center_y) / radius_y;
(norm_x * norm_x + norm_y * norm_y) <= 1.0
}
ClipPath::Polygon(vertices) => {
if vertices.len() < 3 {
return true;
}
// Ray casting algorithm for point-in-polygon
let mut inside = false;
let mut j = vertices.len() - 1;
for i in 0..vertices.len() {
let pi = vertices[i];
let pj = vertices[j];
if ((pi.y > y) != (pj.y > y))
&& (x < (pj.x - pi.x) * (y - pi.y) / (pj.y - pi.y + 1e-6) + pi.x)
{
inside = !inside;
}
j = i;
}
inside
}
ClipPath::Path(_) => true, // Evaluated via GPU path mask
}
}
}
/// Continuously interpolates between two `ClipPath` shapes per W3C CSS Masking Module Level 1.
pub fn interpolate_clip_path(from: &ClipPath, to: &ClipPath, t: f32) -> ClipPath {
let t = t.clamp(0.0, 1.0);
match (from, to) {
(
ClipPath::Inset {
top: t1,
right: r1,
bottom: b1,
left: l1,
border_radius: br1,
},
ClipPath::Inset {
top: t2,
right: r2,
bottom: b2,
left: l2,
border_radius: br2,
},
) => ClipPath::Inset {
top: t1 + (t2 - t1) * t,
right: r1 + (r2 - r1) * t,
bottom: b1 + (b2 - b1) * t,
left: l1 + (l2 - l1) * t,
border_radius: br1 + (br2 - br1) * t,
},
(
ClipPath::Circle {
radius: r1,
center_x: x1,
center_y: y1,
},
ClipPath::Circle {
radius: r2,
center_x: x2,
center_y: y2,
},
) => ClipPath::Circle {
radius: r1 + (r2 - r1) * t,
center_x: x1 + (x2 - x1) * t,
center_y: y1 + (y2 - y1) * t,
},
(
ClipPath::Ellipse {
radius_x: rx1,
radius_y: ry1,
center_x: cx1,
center_y: cy1,
},
ClipPath::Ellipse {
radius_x: rx2,
radius_y: ry2,
center_x: cx2,
center_y: cy2,
},
) => ClipPath::Ellipse {
radius_x: rx1 + (rx2 - rx1) * t,
radius_y: ry1 + (ry2 - ry1) * t,
center_x: cx1 + (cx2 - cx1) * t,
center_y: cy1 + (cy2 - cy1) * t,
},
(ClipPath::Polygon(v1), ClipPath::Polygon(v2)) => {
if v1.len() == v2.len() {
let mut out = Vec::with_capacity(v1.len());
for (p1, p2) in v1.iter().zip(v2.iter()) {
out.push(Point::new(
p1.x + (p2.x - p1.x) * t,
p1.y + (p2.y - p1.y) * t,
));
}
ClipPath::Polygon(out)
} else if t < 0.5 {
from.clone()
} else {
to.clone()
}
}
_ => {
if t < 0.5 {
from.clone()
} else {
to.clone()
}
}
}
}