whisker-engine 0.15.2

Host-independent retained scene and frame production for Whisker.
Documentation
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Lowering from computed style into the Host-independent paint protocol.

use whisker_protocol::{
    BorderLineStyle, BoxClip, BoxPaint, BoxShadow, ClipShape, FillRule, ImageRendering,
    OverflowClip, PaintBox, PaintColor, PaintCoordinate, PaintCornerRadius, PaintCorners,
    PaintEdges, PaintLengthPercentage, PaintPosition, PathCommand, Transform, Visibility,
    VisualEffects,
};
use whisker_style::{
    BorderStyleValue, ClipBoxValue, ClipFillRuleValue, ColorValue, ComputedClipPath,
    ComputedClipPathCommand, ComputedClipPoint, ComputedClipShape, ComputedLayoutStyle,
    ComputedLengthPercentage, ComputedOffsetPathValue as OffsetPathValue, ComputedPaintStyle,
    ComputedTransformFunction, ComputedTransformStyle, ImageRenderingValue, MotionPathCommandValue,
    OffsetRotateValue, OverflowValue, VisibilityValue,
};

use crate::color::named_color_srgb;

mod motion_path;

use motion_path::motion_path_state;
#[cfg(test)]
use motion_path::{MotionCurve, MotionSegment, append_rotated_ellipse, point_line_distance};

/// Complete common presentation values derived from one computed node style.
#[derive(Clone, Debug, PartialEq)]
pub struct LoweredPaint {
    /// Background and border paint.
    pub box_paint: BoxPaint,
    /// Visual effects applied by the Host compositor.
    pub visual_effects: VisualEffects,
    /// Descendant overflow clip.
    pub clip: BoxClip,
    /// Transform retained until the border-box size is known.
    pub transform: ComputedTransformStyle,
    /// Group opacity.
    pub opacity: f32,
    /// Paint visibility.
    pub visibility: Visibility,
    /// Sibling stacking key.
    pub z_order: i32,
}

/// Lowers renderer-independent computed style into protocol-owned values.
pub fn lower_paint(style: &ComputedPaintStyle, layout: &ComputedLayoutStyle) -> LoweredPaint {
    LoweredPaint {
        box_paint: BoxPaint {
            background_color: lower_color(&style.background_color),
            border_widths: edges(&layout.border, length),
            border_colors: PaintEdges {
                top: effective_border_color(&style.border_colors.top, style.border_styles.top),
                right: effective_border_color(
                    &style.border_colors.right,
                    style.border_styles.right,
                ),
                bottom: effective_border_color(
                    &style.border_colors.bottom,
                    style.border_styles.bottom,
                ),
                left: effective_border_color(&style.border_colors.left, style.border_styles.left),
            },
            border_styles: edges(&style.border_styles, lower_border_style),
            border_radii: PaintCorners {
                top_left: corner_radius(&style.border_radii.top_left),
                top_right: corner_radius(&style.border_radii.top_right),
                bottom_right: corner_radius(&style.border_radii.bottom_right),
                bottom_left: corner_radius(&style.border_radii.bottom_left),
            },
        },
        visual_effects: VisualEffects {
            backdrop_blur: style.backdrop_blur.map(|value| value.get()),
            box_shadows: style
                .box_shadows
                .iter()
                .map(|shadow| BoxShadow {
                    offset_x: shadow.offset_x.get(),
                    offset_y: shadow.offset_y.get(),
                    blur_radius: shadow.blur_radius.get(),
                    spread_radius: shadow.spread_radius.get(),
                    color: lower_color(&shadow.color),
                    inset: shadow.inset,
                })
                .collect(),
            clip_path: style.clip_path.as_ref().map(lower_clip_path),
            image_rendering: match style.image_rendering {
                ImageRenderingValue::Auto => ImageRendering::Auto,
                ImageRenderingValue::Pixelated => ImageRendering::Pixelated,
                ImageRenderingValue::CrispEdges => ImageRendering::CrispEdges,
            },
            ..VisualEffects::default()
        },
        clip: BoxClip {
            horizontal: lower_overflow(style.overflow_x),
            vertical: lower_overflow(style.overflow_y),
        },
        transform: style.transform.clone(),
        opacity: style.opacity.get(),
        visibility: match style.visibility {
            VisibilityValue::Visible => Visibility::Visible,
            VisibilityValue::Hidden => Visibility::Hidden,
        },
        z_order: style.z_index,
    }
}

fn lower_clip_path(value: &ComputedClipPath) -> (PaintBox, ClipShape) {
    let reference_box = match value.reference_box {
        ClipBoxValue::Border => PaintBox::Border,
        ClipBoxValue::Padding => PaintBox::Padding,
        ClipBoxValue::Content => PaintBox::Content,
        ClipBoxValue::Fill => PaintBox::Fill,
        ClipBoxValue::Stroke => PaintBox::Stroke,
        ClipBoxValue::View => PaintBox::View,
    };
    let shape = match &value.shape {
        ComputedClipShape::Inset { offsets, radii } => ClipShape::Inset {
            edges: edges(offsets, coordinate),
            radii: PaintCorners {
                top_left: corner_radius(&radii.top_left),
                top_right: corner_radius(&radii.top_right),
                bottom_right: corner_radius(&radii.bottom_right),
                bottom_left: corner_radius(&radii.bottom_left),
            },
        },
        ComputedClipShape::Circle { radius, center } => ClipShape::Circle {
            radius: length(radius),
            center: clip_position(center),
        },
        ComputedClipShape::Ellipse {
            radius_x,
            radius_y,
            center,
        } => ClipShape::Ellipse {
            radius_x: length(radius_x),
            radius_y: length(radius_y),
            center: clip_position(center),
        },
        ComputedClipShape::Path {
            fill_rule,
            commands,
        } => ClipShape::Path {
            fill_rule: match fill_rule {
                ClipFillRuleValue::NonZero => FillRule::NonZero,
                ClipFillRuleValue::EvenOdd => FillRule::EvenOdd,
            },
            commands: commands
                .iter()
                .map(|command| match command {
                    ComputedClipPathCommand::MoveTo(value) => {
                        PathCommand::MoveTo(clip_position(value))
                    }
                    ComputedClipPathCommand::LineTo(value) => {
                        PathCommand::LineTo(clip_position(value))
                    }
                    ComputedClipPathCommand::QuadraticTo { control, end } => {
                        PathCommand::QuadraticTo {
                            control: clip_position(control),
                            end: clip_position(end),
                        }
                    }
                    ComputedClipPathCommand::CubicTo {
                        control_1,
                        control_2,
                        end,
                    } => PathCommand::CubicTo {
                        control_1: clip_position(control_1),
                        control_2: clip_position(control_2),
                        end: clip_position(end),
                    },
                    ComputedClipPathCommand::Close => PathCommand::Close,
                })
                .collect(),
        },
    };
    (reference_box, shape)
}

fn coordinate(value: &ComputedLengthPercentage) -> PaintCoordinate {
    PaintCoordinate {
        length: value.length(),
        fraction: value.fraction(),
    }
}

fn clip_position(value: &ComputedClipPoint) -> PaintPosition {
    PaintPosition {
        x: coordinate(&value.x),
        y: coordinate(&value.y),
    }
}

/// Resolves a computed transform against one node's border-box size.
///
/// Hosts receive only the resulting matrix; percentages and transform-origin
/// never cross the renderer boundary.
pub fn lower_transform(
    style: &ComputedTransformStyle,
    border_width: f32,
    border_height: f32,
) -> Option<Transform> {
    if !border_width.is_finite()
        || !border_height.is_finite()
        || border_width < 0.0
        || border_height < 0.0
    {
        return None;
    }

    // Whisker applies `perspective` to the current node. Prepending it makes
    // the node's transform functions run
    // first and the perspective divide run afterward.
    let mut matrix = style
        .perspective
        .map(|distance| perspective(distance.get().max(1.0)))
        .unwrap_or_else(identity);
    let motion = match &style.offset_path {
        OffsetPathValue::None => None,
        path => Some(motion_path_state(
            path,
            style.offset_distance.get(),
            border_width,
            border_height,
        )?),
    };
    if let Some((_, _, tangent)) = motion {
        let angle = match style.offset_rotate {
            OffsetRotateValue::Auto => tangent,
            OffsetRotateValue::Angle(angle) => angle.get(),
        };
        matrix = multiply(matrix, rotation_z(angle));
    }
    for function in &style.functions {
        matrix = multiply(
            matrix,
            function_matrix(function, border_width, border_height)?,
        );
    }
    let origin_x = resolve_box_length(style.origin_x, border_width);
    let origin_y = resolve_box_length(style.origin_y, border_height);
    let around_origin = multiply(
        multiply(translation(origin_x, origin_y, 0.0), matrix),
        translation(-origin_x, -origin_y, 0.0),
    );
    // Every Host currently flattens at the node boundary. Preserve the exact
    // X/Y/W projection of points on the local z=0 plane, but canonicalize the
    // unused depth row and column so GPU clip-space depth cannot discard CSS
    // pixels and descendants cannot accidentally share a 3-D context.
    let positioned = motion.map_or(around_origin, |(x, y, _)| {
        multiply(translation(x, y, 0.0), around_origin)
    });
    let flat_plane = [
        positioned[0],
        positioned[1],
        0.0,
        positioned[3],
        positioned[4],
        positioned[5],
        0.0,
        positioned[7],
        0.0,
        0.0,
        1.0,
        0.0,
        positioned[12],
        positioned[13],
        0.0,
        positioned[15],
    ];
    flat_plane
        .iter()
        .all(|value| value.is_finite())
        .then_some(Transform(flat_plane))
}

fn function_matrix(
    function: &ComputedTransformFunction,
    border_width: f32,
    border_height: f32,
) -> Option<[f32; 16]> {
    let matrix = match function {
        ComputedTransformFunction::Translate { x, y, z } => translation(
            resolve_box_length(*x, border_width),
            resolve_box_length(*y, border_height),
            z.get(),
        ),
        ComputedTransformFunction::RotateX(degrees) => {
            let radians = degrees.get().to_radians();
            let (sin, cos) = radians.sin_cos();
            [
                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,
            ]
        }
        ComputedTransformFunction::RotateY(degrees) => {
            let radians = degrees.get().to_radians();
            let (sin, cos) = radians.sin_cos();
            [
                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,
            ]
        }
        ComputedTransformFunction::RotateZ(degrees) => rotation_z(degrees.get()),
        ComputedTransformFunction::Scale { x, y, z } => [
            x.get(),
            0.0,
            0.0,
            0.0,
            0.0,
            y.get(),
            0.0,
            0.0,
            0.0,
            0.0,
            z.get(),
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
        ],
        ComputedTransformFunction::Skew {
            x_degrees,
            y_degrees,
        } => [
            1.0,
            y_degrees.get().to_radians().tan(),
            0.0,
            0.0,
            x_degrees.get().to_radians().tan(),
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
        ],
        ComputedTransformFunction::Matrix(values) => values.map(|value| value.get()),
    };
    matrix
        .iter()
        .all(|value| value.is_finite())
        .then_some(matrix)
}

fn rotation_z(degrees: f32) -> [f32; 16] {
    let radians = degrees.to_radians();
    let (sin, cos) = radians.sin_cos();
    [
        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,
    ]
}

fn identity() -> [f32; 16] {
    [
        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,
    ]
}

fn translation(x: f32, y: f32, z: f32) -> [f32; 16] {
    [
        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,
    ]
}

fn perspective(distance: f32) -> [f32; 16] {
    let mut matrix = identity();
    matrix[11] = -1.0 / distance;
    matrix
}

fn multiply(left: [f32; 16], right: [f32; 16]) -> [f32; 16] {
    let mut output = [0.0; 16];
    for column in 0..4 {
        for row in 0..4 {
            output[column * 4 + row] = (0..4)
                .map(|index| left[index * 4 + row] * right[column * 4 + index])
                .sum();
        }
    }
    output
}

fn resolve_box_length(value: ComputedLengthPercentage, extent: f32) -> f32 {
    value.length() + value.fraction() * extent
}

fn edges<T, U>(input: &whisker_style::Edges<T>, map: impl Fn(&T) -> U) -> PaintEdges<U> {
    PaintEdges {
        top: map(&input.top),
        right: map(&input.right),
        bottom: map(&input.bottom),
        left: map(&input.left),
    }
}

fn length(value: &ComputedLengthPercentage) -> PaintLengthPercentage {
    PaintLengthPercentage {
        length: value.length(),
        fraction: value.fraction(),
    }
}

pub(crate) fn corner_radius(value: &whisker_style::ComputedCornerRadius) -> PaintCornerRadius {
    PaintCornerRadius {
        horizontal: length(&value.horizontal),
        vertical: length(&value.vertical),
    }
}

/// Lowers a renderer-independent computed color into the paint protocol.
pub fn lower_color(color: &ColorValue) -> PaintColor {
    match color {
        ColorValue::Named(name) => named_color_srgb(name).map_or_else(
            || PaintColor::Named(name.clone()),
            |[red, green, blue]| PaintColor::Srgba {
                red,
                green,
                blue,
                alpha: 1.0,
            },
        ),
        ColorValue::Rgba {
            red,
            green,
            blue,
            alpha,
        } => PaintColor::Srgba {
            red: *red,
            green: *green,
            blue: *blue,
            alpha: alpha.get(),
        },
        ColorValue::Hsla {
            hue_degrees,
            saturation,
            lightness,
            alpha,
        } => PaintColor::Hsla {
            hue_degrees: hue_degrees.get(),
            saturation: saturation.get(),
            lightness: lightness.get(),
            alpha: alpha.get(),
        },
    }
}

fn lower_border_style(value: &BorderStyleValue) -> BorderLineStyle {
    match value {
        BorderStyleValue::None => BorderLineStyle::None,
        BorderStyleValue::Hidden => BorderLineStyle::Hidden,
        BorderStyleValue::Solid => BorderLineStyle::Solid,
        BorderStyleValue::Dashed => BorderLineStyle::Dashed,
        BorderStyleValue::Dotted => BorderLineStyle::Dotted,
        BorderStyleValue::Double => BorderLineStyle::Double,
        BorderStyleValue::Groove => BorderLineStyle::Groove,
        BorderStyleValue::Ridge => BorderLineStyle::Ridge,
        BorderStyleValue::Inset => BorderLineStyle::Inset,
        BorderStyleValue::Outset => BorderLineStyle::Outset,
    }
}

fn effective_border_color(color: &ColorValue, style: BorderStyleValue) -> PaintColor {
    if matches!(style, BorderStyleValue::None | BorderStyleValue::Hidden) {
        PaintColor::Srgba {
            red: 0,
            green: 0,
            blue: 0,
            alpha: 0.0,
        }
    } else {
        lower_color(color)
    }
}

fn lower_overflow(value: OverflowValue) -> OverflowClip {
    match value {
        OverflowValue::Visible => OverflowClip::Visible,
        OverflowValue::Hidden => OverflowClip::Hidden,
    }
}

#[cfg(test)]
mod tests;