alpha2-engine 0.4.4

Player and exporter facade for alpha2.
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
//! A resolved layout's paint list: what its picture is drawn from, in
//! paint order. Backgrounds, borders and shapes are vector fills, batched
//! into as few scenes as the order allows; texts are their textures, placed
//! 1:1 at whole pixels; isolated nodes are groups.

use super::{NodeRects, ResolvedLayout, unit_of};
use alpha2_eval::{EvaluatedLayout, EvaluatedNode, NodeContent};
use alpha2_geometry::kurbo::{Affine, BezPath, Point, Vec2};
use alpha2_geometry::{CanvasSize, IRect, VectorOp, VectorScene, build_scene_in, scene_paint};
use alpha2_model::{FillRule, Paint, Rgba};
use alpha2_render::RenderSize;
use std::sync::Arc;

/// How an item's picture is placed on the layout's picture, in pixels: the
/// item's point `anchor` lands on `position`, scaled per axis about it, then
/// turned clockwise. An item's own pixels are the picture's, 1:1, before
/// this.
#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) struct Placement {
    pub(crate) anchor: [f64; 2],
    pub(crate) position: [f64; 2],
    pub(crate) scale: [f64; 2],
    pub(crate) rotation_degrees: f64,
    pub(crate) opacity: f32,
    /// Whether it may turn: a node's visual placement. A group does not
    /// collapse into an item that may turn, whatever the angle, so the graph
    /// keeps its shape while the angle animates.
    pub(crate) rotates: bool,
}

impl Placement {
    pub(crate) const IDENTITY: Self = Self {
        anchor: [0.0; 2],
        position: [0.0; 2],
        scale: [1.0; 2],
        rotation_degrees: 0.0,
        opacity: 1.0,
        rotates: false,
    };

    /// This placement applied after `inner`, which does not rotate: the
    /// same geometry as the two in turn, since both scales are diagonal.
    fn then(self, inner: Self) -> Self {
        let offset = [
            self.scale[0] * (inner.position[0] - self.anchor[0]),
            self.scale[1] * (inner.position[1] - self.anchor[1]),
        ];
        let (sine, cosine) = libm::sincos(self.rotation_degrees.to_radians());
        Self {
            anchor: inner.anchor,
            position: [
                self.position[0] + cosine * offset[0] - sine * offset[1],
                self.position[1] + sine * offset[0] + cosine * offset[1],
            ],
            scale: [
                self.scale[0] * inner.scale[0],
                self.scale[1] * inner.scale[1],
            ],
            rotation_degrees: self.rotation_degrees,
            opacity: self.opacity * inner.opacity,
            rotates: self.rotates,
        }
    }
}

/// One entry of a layout's paint list.
#[derive(Clone, Debug)]
pub(crate) enum PaintItem {
    /// Vector fills drawn together over the picture.
    Vector {
        scene: Arc<VectorScene>,
        place: Placement,
    },
    /// The text with this index in the layout's texts, its texture's pixel
    /// `(0, 0)` at `place`.
    Text { index: usize, place: Placement },
    /// An isolated node: `items` composited, kept inside `clip`, placed by
    /// `place`.
    Group {
        items: Vec<PaintItem>,
        clip: Option<Arc<VectorScene>>,
        place: Placement,
    },
}

/// A text node's texture as the paint list sees it.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) struct TextBox {
    /// Where its layout box's top-left corner lies in its texture.
    pub(crate) corner: [u32; 2],
    /// The texture's size.
    pub(crate) size: [u32; 2],
}

/// The list while it is built: vector ops not yet made scenes.
enum Building {
    Vector {
        ops: Vec<VectorOp>,
        bounds: IRect,
    },
    Text {
        index: usize,
        origin: [i32; 2],
        bounds: IRect,
    },
    Group {
        items: Vec<Building>,
        clip: Option<Vec<VectorOp>>,
        place: Placement,
    },
}

struct Builder<'a> {
    canvas: CanvasSize,
    unit: f64,
    rects: &'a [NodeRects],
    texts: &'a [Option<TextBox>],
    /// The next node's index in preorder, and the next text node's.
    node: usize,
    text: usize,
    time: alpha2_time::MediaTime,
}

/// The paint list of `layout`, resolved as `resolved`, on a picture of
/// `size`. `texts` gives each text node's texture, in preorder; `None` for
/// empty text, which draws nothing.
pub(crate) fn paint_list(
    layout: &EvaluatedLayout,
    resolved: &ResolvedLayout,
    size: RenderSize,
    texts: &[Option<TextBox>],
) -> Vec<PaintItem> {
    let mut builder = Builder {
        canvas: CanvasSize::new(size.width, size.height),
        unit: f64::from(unit_of(size)),
        rects: &resolved.nodes,
        texts,
        node: 0,
        text: 0,
        time: layout.time,
    };
    let mut items = Vec::new();
    builder.node(&layout.root, &mut items);
    items
        .into_iter()
        .filter_map(|item| finish(item, builder.canvas))
        .collect()
}

impl Builder<'_> {
    fn node(&mut self, node: &EvaluatedNode, out: &mut Vec<Building>) {
        let Some(&rects) = self.rects.get(self.node) else {
            return;
        };
        self.node += 1;
        let border = rects.border;
        let radius = f64::from(node.paint.border_radius) * self.unit;
        let placed = node.visual.map(|visual| {
            let center = [
                f64::from(border[0] + border[2]) * 0.5,
                f64::from(border[1] + border[3]) * 0.5,
            ];
            Placement {
                anchor: center,
                position: [
                    center[0] + f64::from(visual.translate.x) * self.unit,
                    center[1] + f64::from(visual.translate.y) * self.unit,
                ],
                scale: [f64::from(visual.scale.x), f64::from(visual.scale.y)],
                rotation_degrees: f64::from(visual.rotation_degrees),
                opacity: visual.opacity,
                // Any visual may turn: presence decides, so the graph keeps its shape.
                rotates: true,
            }
        });
        let mut own = Vec::new();
        let target = if placed.is_some() {
            &mut own
        } else {
            &mut *out
        };
        if let Some(paint) = &node.paint.background {
            let path = rounded(border, radius);
            self.fill(target, path, FillRule::Nonzero, paint, border);
        }
        // The padding box, rounded by what of the radius the border leaves.
        let inner_radius = (radius - f64::from(rects.padding[0] - border[0])).max(0.0);
        if let Some((width, paint)) = &node.paint.border
            && *width > 0.0
        {
            let mut path = rounded(border, radius);
            path.extend(rounded(rects.padding, inner_radius));
            self.fill(target, path, FillRule::Evenodd, paint, border);
        }
        let clip = node.clip.then(|| {
            let path = rounded(rects.padding, inner_radius);
            let inner = rects.padding;
            let white = Paint::Solid {
                color: Rgba::new(1.0, 1.0, 1.0, 1.0),
            };
            fill_op(self.canvas, path, FillRule::Nonzero, &white, inner)
                .into_iter()
                .collect::<Vec<_>>()
        });
        let mut content = Vec::new();
        let content_target = if clip.is_some() {
            &mut content
        } else {
            &mut *target
        };
        self.content(node, rects, content_target);
        if let Some(clip) = clip {
            target.push(Building::Group {
                items: content,
                clip: Some(clip),
                place: Placement::IDENTITY,
            });
        }
        if let Some(place) = placed {
            out.push(Building::Group {
                items: own,
                clip: None,
                place,
            });
        }
    }

    fn content(&mut self, node: &EvaluatedNode, rects: NodeRects, out: &mut Vec<Building>) {
        match &node.content {
            NodeContent::Empty => {}
            NodeContent::Children(children) => {
                for child in children {
                    self.node(child, out);
                }
            }
            NodeContent::Text(_) => {
                let index = self.text;
                self.text += 1;
                if let Some(Some(text)) = self.texts.get(index) {
                    #[allow(clippy::cast_possible_truncation)]
                    let corner = [rects.content[0] as i32, rects.content[1] as i32];
                    let origin = [
                        corner[0].saturating_sub_unsigned(text.corner[0]),
                        corner[1].saturating_sub_unsigned(text.corner[1]),
                    ];
                    let bounds = IRect::new(
                        origin[0],
                        origin[1],
                        origin[0].saturating_add_unsigned(text.size[0]),
                        origin[1].saturating_add_unsigned(text.size[1]),
                    );
                    out.push(Building::Text {
                        index,
                        origin,
                        bounds,
                    });
                }
            }
            NodeContent::Shape(plan) => {
                let [left, top, right, bottom] = rects.content;
                let (width, height) = (right - left, bottom - top);
                if width < 1.0 || height < 1.0 {
                    return;
                }
                #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
                let space = CanvasSize::new(width as u32, height as u32);
                let items = plan.evaluate(self.time, f64::from(width) / f64::from(height));
                let ops = build_scene_in(
                    &items,
                    space,
                    Vec2::new(f64::from(left), f64::from(top)),
                    self.canvas,
                );
                let bounds = ops
                    .iter()
                    .filter_map(|op| match op {
                        VectorOp::Fill { bounds, .. } => Some(*bounds),
                        _ => None,
                    })
                    .fold(IRect::default(), IRect::union);
                add_vector(out, ops, bounds);
            }
        }
    }

    /// Adds a fill of `path` with `paint`, its points normalized to `space`.
    fn fill(
        &self,
        out: &mut Vec<Building>,
        path: BezPath,
        rule: FillRule,
        paint: &Paint,
        space: [f32; 4],
    ) {
        if let Some(op) = fill_op(self.canvas, path, rule, paint, space) {
            let VectorOp::Fill { bounds, .. } = op else {
                return;
            };
            add_vector(out, vec![op], bounds);
        }
    }
}

/// A fill op of `path` on `canvas`, its paint normalized to the box
/// `space`; `None` when it covers nothing.
fn fill_op(
    canvas: CanvasSize,
    path: BezPath,
    rule: FillRule,
    paint: &Paint,
    space: [f32; 4],
) -> Option<VectorOp> {
    let canvas_rect = IRect::new(
        0,
        0,
        i32::try_from(canvas.width).unwrap_or(i32::MAX),
        i32::try_from(canvas.height).unwrap_or(i32::MAX),
    );
    let bounds = IRect::covering(&path, canvas_rect);
    let (width, height) = (space[2] - space[0], space[3] - space[1]);
    if bounds.is_empty() || width <= 0.0 || height <= 0.0 {
        return None;
    }
    let inverse = Affine::translate((-f64::from(space[0]), -f64::from(space[1])));
    Some(VectorOp::Fill {
        path: Arc::new(path),
        rule,
        paint: scene_paint(paint, inverse, [width, height]),
        opacity: 1.0,
        bounds,
    })
}

/// The rounded rectangle `[left, top, right, bottom]`, its radius capped at
/// half its shorter side.
fn rounded(rect: [f32; 4], radius: f64) -> BezPath {
    let [left, top, right, bottom] = rect.map(f64::from);
    alpha2_geometry::shapes::rectangle(
        Point::new(f64::midpoint(left, right), f64::midpoint(top, bottom)),
        Vec2::new(right - left, bottom - top),
        radius,
        0.0,
    )
}

/// Adds vector ops covering `bounds`: to the last vector batch when nothing
/// drawn after it meets them, else as a new batch. Compositing is
/// associative and what is skipped is transparent where the ops draw, so the
/// picture is the same up to intermediate rounding.
fn add_vector(out: &mut Vec<Building>, ops: Vec<VectorOp>, bounds: IRect) {
    if ops.is_empty() {
        return;
    }
    for item in out.iter_mut().rev() {
        match item {
            Building::Vector {
                ops: batch,
                bounds: covered,
            } => {
                batch.extend(ops);
                *covered = covered.union(bounds);
                return;
            }
            Building::Text { bounds: text, .. } if text.intersect(bounds).is_empty() => {}
            Building::Text { .. } | Building::Group { .. } => break,
        }
    }
    out.push(Building::Vector { ops, bounds });
}

/// The built item as drawn: vector batches made scenes, and a group with no
/// clip and one item that does not rotate collapsed into that item.
fn finish(item: Building, canvas: CanvasSize) -> Option<PaintItem> {
    let scene = |ops| Arc::new(VectorScene::new(ops, canvas.width, canvas.height));
    Some(match item {
        Building::Vector { ops, .. } => PaintItem::Vector {
            scene: scene(ops),
            place: Placement::IDENTITY,
        },
        Building::Text { index, origin, .. } => PaintItem::Text {
            index,
            place: Placement {
                position: origin.map(f64::from),
                ..Placement::IDENTITY
            },
        },
        Building::Group { items, clip, place } => {
            let mut items = items
                .into_iter()
                .filter_map(|item| finish(item, canvas))
                .collect::<Vec<_>>();
            if items.is_empty() {
                return None;
            }
            if clip.is_none() && items.len() == 1 {
                match items.pop() {
                    Some(PaintItem::Vector {
                        scene,
                        place: inner,
                    }) if !inner.rotates => {
                        return Some(PaintItem::Vector {
                            scene,
                            place: place.then(inner),
                        });
                    }
                    Some(PaintItem::Text {
                        index,
                        place: inner,
                    }) if !inner.rotates => {
                        return Some(PaintItem::Text {
                            index,
                            place: place.then(inner),
                        });
                    }
                    Some(other) => items.push(other),
                    None => {}
                }
            }
            PaintItem::Group {
                items,
                clip: clip.map(scene),
                place,
            }
        }
    })
}