1use whisker_protocol::{
4 BorderLineStyle, BoxClip, BoxPaint, BoxShadow, ClipShape, FillRule, ImageRendering,
5 OverflowClip, PaintBox, PaintColor, PaintCoordinate, PaintCornerRadius, PaintCorners,
6 PaintEdges, PaintLengthPercentage, PaintPosition, PathCommand, Transform, Visibility,
7 VisualEffects,
8};
9use whisker_style::{
10 BorderStyleValue, ClipBoxValue, ClipFillRuleValue, ColorValue, ComputedClipPath,
11 ComputedClipPathCommand, ComputedClipPoint, ComputedClipShape, ComputedLayoutStyle,
12 ComputedLengthPercentage, ComputedOffsetPathValue as OffsetPathValue, ComputedPaintStyle,
13 ComputedTransformFunction, ComputedTransformStyle, ImageRenderingValue, MotionPathCommandValue,
14 OffsetRotateValue, OverflowValue, VisibilityValue,
15};
16
17use crate::color::named_color_srgb;
18
19mod motion_path;
20
21use motion_path::motion_path_state;
22#[cfg(test)]
23use motion_path::{MotionCurve, MotionSegment, append_rotated_ellipse, point_line_distance};
24
25#[derive(Clone, Debug, PartialEq)]
27pub struct LoweredPaint {
28 pub box_paint: BoxPaint,
30 pub visual_effects: VisualEffects,
32 pub clip: BoxClip,
34 pub transform: ComputedTransformStyle,
36 pub opacity: f32,
38 pub visibility: Visibility,
40 pub z_order: i32,
42}
43
44pub fn lower_paint(style: &ComputedPaintStyle, layout: &ComputedLayoutStyle) -> LoweredPaint {
46 LoweredPaint {
47 box_paint: BoxPaint {
48 background_color: lower_color(&style.background_color),
49 border_widths: edges(&layout.border, length),
50 border_colors: PaintEdges {
51 top: effective_border_color(&style.border_colors.top, style.border_styles.top),
52 right: effective_border_color(
53 &style.border_colors.right,
54 style.border_styles.right,
55 ),
56 bottom: effective_border_color(
57 &style.border_colors.bottom,
58 style.border_styles.bottom,
59 ),
60 left: effective_border_color(&style.border_colors.left, style.border_styles.left),
61 },
62 border_styles: edges(&style.border_styles, lower_border_style),
63 border_radii: PaintCorners {
64 top_left: corner_radius(&style.border_radii.top_left),
65 top_right: corner_radius(&style.border_radii.top_right),
66 bottom_right: corner_radius(&style.border_radii.bottom_right),
67 bottom_left: corner_radius(&style.border_radii.bottom_left),
68 },
69 },
70 visual_effects: VisualEffects {
71 backdrop_blur: style.backdrop_blur.map(|value| value.get()),
72 box_shadows: style
73 .box_shadows
74 .iter()
75 .map(|shadow| BoxShadow {
76 offset_x: shadow.offset_x.get(),
77 offset_y: shadow.offset_y.get(),
78 blur_radius: shadow.blur_radius.get(),
79 spread_radius: shadow.spread_radius.get(),
80 color: lower_color(&shadow.color),
81 inset: shadow.inset,
82 })
83 .collect(),
84 clip_path: style.clip_path.as_ref().map(lower_clip_path),
85 image_rendering: match style.image_rendering {
86 ImageRenderingValue::Auto => ImageRendering::Auto,
87 ImageRenderingValue::Pixelated => ImageRendering::Pixelated,
88 ImageRenderingValue::CrispEdges => ImageRendering::CrispEdges,
89 },
90 ..VisualEffects::default()
91 },
92 clip: BoxClip {
93 horizontal: lower_overflow(style.overflow_x),
94 vertical: lower_overflow(style.overflow_y),
95 },
96 transform: style.transform.clone(),
97 opacity: style.opacity.get(),
98 visibility: match style.visibility {
99 VisibilityValue::Visible => Visibility::Visible,
100 VisibilityValue::Hidden => Visibility::Hidden,
101 },
102 z_order: style.z_index,
103 }
104}
105
106fn lower_clip_path(value: &ComputedClipPath) -> (PaintBox, ClipShape) {
107 let reference_box = match value.reference_box {
108 ClipBoxValue::Border => PaintBox::Border,
109 ClipBoxValue::Padding => PaintBox::Padding,
110 ClipBoxValue::Content => PaintBox::Content,
111 ClipBoxValue::Fill => PaintBox::Fill,
112 ClipBoxValue::Stroke => PaintBox::Stroke,
113 ClipBoxValue::View => PaintBox::View,
114 };
115 let shape = match &value.shape {
116 ComputedClipShape::Inset { offsets, radii } => ClipShape::Inset {
117 edges: edges(offsets, coordinate),
118 radii: PaintCorners {
119 top_left: corner_radius(&radii.top_left),
120 top_right: corner_radius(&radii.top_right),
121 bottom_right: corner_radius(&radii.bottom_right),
122 bottom_left: corner_radius(&radii.bottom_left),
123 },
124 },
125 ComputedClipShape::Circle { radius, center } => ClipShape::Circle {
126 radius: length(radius),
127 center: clip_position(center),
128 },
129 ComputedClipShape::Ellipse {
130 radius_x,
131 radius_y,
132 center,
133 } => ClipShape::Ellipse {
134 radius_x: length(radius_x),
135 radius_y: length(radius_y),
136 center: clip_position(center),
137 },
138 ComputedClipShape::Path {
139 fill_rule,
140 commands,
141 } => ClipShape::Path {
142 fill_rule: match fill_rule {
143 ClipFillRuleValue::NonZero => FillRule::NonZero,
144 ClipFillRuleValue::EvenOdd => FillRule::EvenOdd,
145 },
146 commands: commands
147 .iter()
148 .map(|command| match command {
149 ComputedClipPathCommand::MoveTo(value) => {
150 PathCommand::MoveTo(clip_position(value))
151 }
152 ComputedClipPathCommand::LineTo(value) => {
153 PathCommand::LineTo(clip_position(value))
154 }
155 ComputedClipPathCommand::QuadraticTo { control, end } => {
156 PathCommand::QuadraticTo {
157 control: clip_position(control),
158 end: clip_position(end),
159 }
160 }
161 ComputedClipPathCommand::CubicTo {
162 control_1,
163 control_2,
164 end,
165 } => PathCommand::CubicTo {
166 control_1: clip_position(control_1),
167 control_2: clip_position(control_2),
168 end: clip_position(end),
169 },
170 ComputedClipPathCommand::Close => PathCommand::Close,
171 })
172 .collect(),
173 },
174 };
175 (reference_box, shape)
176}
177
178fn coordinate(value: &ComputedLengthPercentage) -> PaintCoordinate {
179 PaintCoordinate {
180 length: value.length(),
181 fraction: value.fraction(),
182 }
183}
184
185fn clip_position(value: &ComputedClipPoint) -> PaintPosition {
186 PaintPosition {
187 x: coordinate(&value.x),
188 y: coordinate(&value.y),
189 }
190}
191
192pub fn lower_transform(
197 style: &ComputedTransformStyle,
198 border_width: f32,
199 border_height: f32,
200) -> Option<Transform> {
201 if !border_width.is_finite()
202 || !border_height.is_finite()
203 || border_width < 0.0
204 || border_height < 0.0
205 {
206 return None;
207 }
208
209 let mut matrix = style
213 .perspective
214 .map(|distance| perspective(distance.get().max(1.0)))
215 .unwrap_or_else(identity);
216 let motion = match &style.offset_path {
217 OffsetPathValue::None => None,
218 path => Some(motion_path_state(
219 path,
220 style.offset_distance.get(),
221 border_width,
222 border_height,
223 )?),
224 };
225 if let Some((_, _, tangent)) = motion {
226 let angle = match style.offset_rotate {
227 OffsetRotateValue::Auto => tangent,
228 OffsetRotateValue::Angle(angle) => angle.get(),
229 };
230 matrix = multiply(matrix, rotation_z(angle));
231 }
232 for function in &style.functions {
233 matrix = multiply(
234 matrix,
235 function_matrix(function, border_width, border_height)?,
236 );
237 }
238 let origin_x = resolve_box_length(style.origin_x, border_width);
239 let origin_y = resolve_box_length(style.origin_y, border_height);
240 let around_origin = multiply(
241 multiply(translation(origin_x, origin_y, 0.0), matrix),
242 translation(-origin_x, -origin_y, 0.0),
243 );
244 let positioned = motion.map_or(around_origin, |(x, y, _)| {
249 multiply(translation(x, y, 0.0), around_origin)
250 });
251 let flat_plane = [
252 positioned[0],
253 positioned[1],
254 0.0,
255 positioned[3],
256 positioned[4],
257 positioned[5],
258 0.0,
259 positioned[7],
260 0.0,
261 0.0,
262 1.0,
263 0.0,
264 positioned[12],
265 positioned[13],
266 0.0,
267 positioned[15],
268 ];
269 flat_plane
270 .iter()
271 .all(|value| value.is_finite())
272 .then_some(Transform(flat_plane))
273}
274
275fn function_matrix(
276 function: &ComputedTransformFunction,
277 border_width: f32,
278 border_height: f32,
279) -> Option<[f32; 16]> {
280 let matrix = match function {
281 ComputedTransformFunction::Translate { x, y, z } => translation(
282 resolve_box_length(*x, border_width),
283 resolve_box_length(*y, border_height),
284 z.get(),
285 ),
286 ComputedTransformFunction::RotateX(degrees) => {
287 let radians = degrees.get().to_radians();
288 let (sin, cos) = radians.sin_cos();
289 [
290 1.0, 0.0, 0.0, 0.0, 0.0, cos, sin, 0.0, 0.0, -sin, cos, 0.0, 0.0, 0.0, 0.0, 1.0,
291 ]
292 }
293 ComputedTransformFunction::RotateY(degrees) => {
294 let radians = degrees.get().to_radians();
295 let (sin, cos) = radians.sin_cos();
296 [
297 cos, 0.0, -sin, 0.0, 0.0, 1.0, 0.0, 0.0, sin, 0.0, cos, 0.0, 0.0, 0.0, 0.0, 1.0,
298 ]
299 }
300 ComputedTransformFunction::RotateZ(degrees) => rotation_z(degrees.get()),
301 ComputedTransformFunction::Scale { x, y, z } => [
302 x.get(),
303 0.0,
304 0.0,
305 0.0,
306 0.0,
307 y.get(),
308 0.0,
309 0.0,
310 0.0,
311 0.0,
312 z.get(),
313 0.0,
314 0.0,
315 0.0,
316 0.0,
317 1.0,
318 ],
319 ComputedTransformFunction::Skew {
320 x_degrees,
321 y_degrees,
322 } => [
323 1.0,
324 y_degrees.get().to_radians().tan(),
325 0.0,
326 0.0,
327 x_degrees.get().to_radians().tan(),
328 1.0,
329 0.0,
330 0.0,
331 0.0,
332 0.0,
333 1.0,
334 0.0,
335 0.0,
336 0.0,
337 0.0,
338 1.0,
339 ],
340 ComputedTransformFunction::Matrix(values) => values.map(|value| value.get()),
341 };
342 matrix
343 .iter()
344 .all(|value| value.is_finite())
345 .then_some(matrix)
346}
347
348fn rotation_z(degrees: f32) -> [f32; 16] {
349 let radians = degrees.to_radians();
350 let (sin, cos) = radians.sin_cos();
351 [
352 cos, sin, 0.0, 0.0, -sin, cos, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
353 ]
354}
355
356fn identity() -> [f32; 16] {
357 [
358 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
359 ]
360}
361
362fn translation(x: f32, y: f32, z: f32) -> [f32; 16] {
363 [
364 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, x, y, z, 1.0,
365 ]
366}
367
368fn perspective(distance: f32) -> [f32; 16] {
369 let mut matrix = identity();
370 matrix[11] = -1.0 / distance;
371 matrix
372}
373
374fn multiply(left: [f32; 16], right: [f32; 16]) -> [f32; 16] {
375 let mut output = [0.0; 16];
376 for column in 0..4 {
377 for row in 0..4 {
378 output[column * 4 + row] = (0..4)
379 .map(|index| left[index * 4 + row] * right[column * 4 + index])
380 .sum();
381 }
382 }
383 output
384}
385
386fn resolve_box_length(value: ComputedLengthPercentage, extent: f32) -> f32 {
387 value.length() + value.fraction() * extent
388}
389
390fn edges<T, U>(input: &whisker_style::Edges<T>, map: impl Fn(&T) -> U) -> PaintEdges<U> {
391 PaintEdges {
392 top: map(&input.top),
393 right: map(&input.right),
394 bottom: map(&input.bottom),
395 left: map(&input.left),
396 }
397}
398
399fn length(value: &ComputedLengthPercentage) -> PaintLengthPercentage {
400 PaintLengthPercentage {
401 length: value.length(),
402 fraction: value.fraction(),
403 }
404}
405
406pub(crate) fn corner_radius(value: &whisker_style::ComputedCornerRadius) -> PaintCornerRadius {
407 PaintCornerRadius {
408 horizontal: length(&value.horizontal),
409 vertical: length(&value.vertical),
410 }
411}
412
413pub fn lower_color(color: &ColorValue) -> PaintColor {
415 match color {
416 ColorValue::Named(name) => named_color_srgb(name).map_or_else(
417 || PaintColor::Named(name.clone()),
418 |[red, green, blue]| PaintColor::Srgba {
419 red,
420 green,
421 blue,
422 alpha: 1.0,
423 },
424 ),
425 ColorValue::Rgba {
426 red,
427 green,
428 blue,
429 alpha,
430 } => PaintColor::Srgba {
431 red: *red,
432 green: *green,
433 blue: *blue,
434 alpha: alpha.get(),
435 },
436 ColorValue::Hsla {
437 hue_degrees,
438 saturation,
439 lightness,
440 alpha,
441 } => PaintColor::Hsla {
442 hue_degrees: hue_degrees.get(),
443 saturation: saturation.get(),
444 lightness: lightness.get(),
445 alpha: alpha.get(),
446 },
447 }
448}
449
450fn lower_border_style(value: &BorderStyleValue) -> BorderLineStyle {
451 match value {
452 BorderStyleValue::None => BorderLineStyle::None,
453 BorderStyleValue::Hidden => BorderLineStyle::Hidden,
454 BorderStyleValue::Solid => BorderLineStyle::Solid,
455 BorderStyleValue::Dashed => BorderLineStyle::Dashed,
456 BorderStyleValue::Dotted => BorderLineStyle::Dotted,
457 BorderStyleValue::Double => BorderLineStyle::Double,
458 BorderStyleValue::Groove => BorderLineStyle::Groove,
459 BorderStyleValue::Ridge => BorderLineStyle::Ridge,
460 BorderStyleValue::Inset => BorderLineStyle::Inset,
461 BorderStyleValue::Outset => BorderLineStyle::Outset,
462 }
463}
464
465fn effective_border_color(color: &ColorValue, style: BorderStyleValue) -> PaintColor {
466 if matches!(style, BorderStyleValue::None | BorderStyleValue::Hidden) {
467 PaintColor::Srgba {
468 red: 0,
469 green: 0,
470 blue: 0,
471 alpha: 0.0,
472 }
473 } else {
474 lower_color(color)
475 }
476}
477
478fn lower_overflow(value: OverflowValue) -> OverflowClip {
479 match value {
480 OverflowValue::Visible => OverflowClip::Visible,
481 OverflowValue::Hidden => OverflowClip::Hidden,
482 }
483}
484
485#[cfg(test)]
486mod tests;