Skip to main content

devpage_design/display/
trees.rs

1//! The property trees: transform, clip and effect nodes in arenas whose
2//! slots are reused. A command names its clip and effect; its chunk names
3//! its transform.
4
5use kurbo::{BezPath, Vec2};
6
7/// A node in one of the property trees, by its slot.
8pub type Id = u32;
9
10/// A scene node's place: its offset from its parent's — or, for a child of
11/// a [run](super::Run), the run's place for it, read from the run's prefix
12/// sums whenever it is asked for, so a sibling's change of size moves it
13/// without touching it.
14#[derive(Clone, Debug)]
15pub struct Transform {
16    /// The parent node its offset is from; `None` at the root.
17    pub parent: Option<Id>,
18    /// Its offset from its parent's place.
19    pub offset: Vec2,
20    /// For a child of a run, the run and its key there, whose place is read
21    /// from the run's prefix sums; `None` for any other node.
22    pub item: Option<(super::RunId, crate::tree::OrderKey)>,
23}
24
25/// A clipping shape, in the coordinates of its transform node.
26#[derive(Clone)]
27pub struct Clip {
28    /// The clip it lies inside; `None` at the root.
29    pub parent: Option<Id>,
30    /// The transform node whose coordinates `path` is in.
31    pub transform: Id,
32    /// Its shape; an empty one cuts nothing.
33    pub path: BezPath,
34}
35
36/// How an effect's layer meets what is under it.
37#[derive(Clone, Copy, Debug, PartialEq)]
38pub enum Blend {
39    /// Drawn over it at an opacity.
40    Opacity(f32),
41    /// Added to it, color and alpha alike, so two fills in such layers that
42    /// meet edge to edge sum to exactly what is in them, with no seam.
43    Add,
44}
45
46/// A layer: its blend, the bound nothing in it reaches — in its transform's
47/// coordinates, or, when `None`, the whole area being painted — and the clip
48/// it is drawn inside.
49#[derive(Clone)]
50pub struct Effect {
51    /// The layer it lies inside; `None` at the root.
52    pub parent: Option<Id>,
53    /// The clip it is drawn inside.
54    pub clip: Id,
55    /// The transform node whose coordinates `bounds` is in.
56    pub transform: Id,
57    /// How it meets what is under it.
58    pub blend: Blend,
59    /// The bound nothing in it reaches; `None` for the whole area being
60    /// painted.
61    pub bounds: Option<BezPath>,
62}
63
64/// A store of property-tree nodes whose slots are reused.
65#[derive(Clone)]
66pub struct Arena<T> {
67    /// Each slot's node, by id; `None` for a free slot.
68    pub(super) slots: Vec<Option<T>>,
69    /// The free slots, the next to reuse last.
70    pub(super) free: Vec<Id>,
71}
72
73impl<T> Default for Arena<T> {
74    fn default() -> Self {
75        Arena {
76            slots: Vec::new(),
77            free: Vec::new(),
78        }
79    }
80}
81
82impl<T> Arena<T> {
83    /// Adds `t` in a free slot, or a new one: its id.
84    pub fn add(&mut self, t: T) -> Id {
85        match self.free.pop() {
86            Some(id) => {
87                self.slots[id as usize] = Some(t);
88                id
89            }
90            None => {
91                self.slots.push(Some(t));
92                (self.slots.len() - 1) as Id
93            }
94        }
95    }
96
97    /// Removes node `id`, freeing its slot for reuse; nothing when it is not
98    /// live.
99    pub fn remove(&mut self, id: Id) {
100        if self
101            .slots
102            .get_mut(id as usize)
103            .and_then(Option::take)
104            .is_some()
105        {
106            self.free.push(id);
107        }
108    }
109
110    /// How many nodes are live.
111    pub fn live(&self) -> usize {
112        self.slots.len() - self.free.len()
113    }
114
115    /// Node `id`; panics when it is not live.
116    pub fn get(&self, id: Id) -> &T {
117        self.slots[id as usize].as_ref().expect("a live node")
118    }
119
120    /// Node `id`, to change; panics when it is not live.
121    pub fn get_mut(&mut self, id: Id) -> &mut T {
122        self.slots[id as usize].as_mut().expect("a live node")
123    }
124}
125
126/// The three property trees.
127#[derive(Clone, Default)]
128pub struct Trees {
129    /// Each scene node's place.
130    pub transforms: Arena<Transform>,
131    /// Every clipping shape, each in its transform node's coordinates.
132    pub clips: Arena<Clip>,
133    /// Every layer: its blend, its bound and the clip it is drawn inside.
134    pub effects: Arena<Effect>,
135}