Skip to main content

devpage_design/display/
paint.rs

1//! The one paint entry point: an area of the list at a scale into a
2//! [`Target`] — the port both backends adapt — opening and closing exactly
3//! the layers each command's property state needs.
4
5use kurbo::{Affine, BezPath, Rect as KRect};
6
7use super::Draw;
8
9use super::*;
10
11/// What a backend does with the commands [`paint`] hands it, each already
12/// under the transform from its coordinates to the target's pixels. A
13/// backend translates each into its library's call and decides nothing.
14pub trait Target {
15    /// Paints `draw` under `t`.
16    fn draw(&mut self, draw: &Draw, t: Affine);
17    /// Opens a clip to `path` under `t`.
18    fn push_clip(&mut self, path: &BezPath, t: Affine);
19    /// A layer with `blend`, bounded by `bounds` under `t` — or, when
20    /// `None`, by `area`, the whole of what is painted, in the target's
21    /// pixels.
22    fn push_layer(&mut self, blend: Blend, bounds: Option<&BezPath>, t: Affine, area: KRect);
23    /// Closes the last clip or layer opened.
24    fn pop(&mut self);
25}
26
27/// A layer open on the target, by the property-tree node that opened it.
28#[derive(Clone, Copy, PartialEq, Debug)]
29pub(super) enum Layer {
30    /// A clip, by its node in the clip tree.
31    Clip(Id),
32    /// An effect's layer, by its node in the effect tree.
33    Effect(Id),
34}
35
36/// The engine's one paint entry point: `area` of the list — in the design's
37/// coordinates — at `scale` device pixels per design pixel into `target`,
38/// whose top-left is the area's. The commands that touch it, in paint order,
39/// each under exactly the layers its property state needs.
40pub fn paint(list: &List, area: KRect, scale: f64, target: &mut impl Target) {
41    let view = Affine::scale(scale) * Affine::translate((-area.x0, -area.y0));
42    let pixels = KRect::new(0.0, 0.0, area.width() * scale, area.height() * scale);
43    let mut open: Vec<Layer> = Vec::new();
44    let place = |transform: Id| view * Affine::translate(list.world(transform));
45    for (origin, _, command) in list.query(area, 1.0 / scale) {
46        let want = layers(list, command.clip, command.effect);
47        let keep = open.iter().zip(&want).take_while(|(a, b)| a == b).count();
48        while open.len() > keep {
49            open.pop();
50            target.pop();
51        }
52        for layer in &want[keep..] {
53            match *layer {
54                Layer::Clip(id) => {
55                    let c = list.trees.clips.get(id);
56                    target.push_clip(&c.path, place(c.transform));
57                }
58                Layer::Effect(id) => {
59                    let e = list.trees.effects.get(id);
60                    target.push_layer(e.blend, e.bounds.as_ref(), place(e.transform), pixels);
61                }
62            }
63            open.push(*layer);
64        }
65        target.draw(&command.draw, view * Affine::translate(origin));
66    }
67    for _ in open {
68        target.pop();
69    }
70}
71
72/// The layers a command under `clip` and `effect` is drawn in, outermost
73/// first: each effect from the root down, inside its own clip, then the
74/// command's clips inside the innermost effect.
75pub(super) fn layers(list: &List, clip: Id, effect: Option<Id>) -> Vec<Layer> {
76    let mut effects = Vec::new();
77    let mut at = effect;
78    while let Some(e) = at {
79        effects.push(e);
80        at = list.trees.effects.get(e).parent;
81    }
82    effects.reverse();
83    let mut out = Vec::new();
84    let mut inside: Option<Id> = None;
85    for e in effects {
86        let effect = list.trees.effects.get(e);
87        let to = effect.clip;
88        clips(list, inside, to, &mut out);
89        // A frame's effect at no opacity, kept so its opacity can change in
90        // place, is no layer.
91        if !(effect.blend == Blend::Opacity(1.0) && effect.bounds.is_none()) {
92            out.push(Layer::Effect(e));
93        }
94        inside = Some(to);
95    }
96    clips(list, inside, clip, &mut out);
97    out
98}
99
100/// The clips from just below `from` down to `to`, outermost first.
101pub(super) fn clips(list: &List, from: Option<Id>, to: Id, out: &mut Vec<Layer>) {
102    let mut chain = Vec::new();
103    let mut at = Some(to);
104    while let Some(c) = at {
105        if Some(c) == from {
106            break;
107        }
108        let clip = list.trees.clips.get(c);
109        // A frame's clip with no shape, kept so it can clip in place, cuts
110        // nothing.
111        if clip.path.elements().is_empty() {
112            at = clip.parent;
113            continue;
114        }
115        chain.push(Layer::Clip(c));
116        at = clip.parent;
117    }
118    out.extend(chain.into_iter().rev());
119}