Skip to main content

lichtung_plot/layout/
paint.rs

1//! Semantic paint batches captured before serialization.
2
3use super::{InkExtent, PanelPath, Rect};
4use crate::elements::LayerSlot;
5
6/// Semantic role of an actual closed drawing batch.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[non_exhaustive]
9pub enum PaintRole {
10    /// Root paper surface.
11    RootSurface,
12    /// Local plot paper surface.
13    PanelSurface,
14    /// Actual grid stroke batch.
15    Grid,
16    /// One closed scientific body containing combined dependent parts.
17    Body,
18    /// Scientific primary layer.
19    Primary,
20    /// Independently attached scientific layers.
21    Overlay,
22    /// Reference marks or panel annotations.
23    Annotation,
24    /// Significance marks.
25    Significance,
26    /// Scientific axes and frame.
27    Axes,
28    /// A local legend.
29    Legend,
30    /// Plot-owned prose.
31    Prose,
32    /// Independent text panel.
33    TextPanel,
34    /// One fixed local image and its independent clipping scope.
35    ImagePanel,
36    /// One fixed native output and its independent clipping scope.
37    FrozenPanel,
38    /// Canvas column and row headers.
39    Headers,
40    /// Canvas row titles.
41    RowTitles,
42    /// Facet strip.
43    FacetStrip,
44    /// Shared axis labels.
45    SharedAxes,
46    /// Shared legend.
47    SharedLegend,
48    /// Root title, subtitle and caption.
49    RootProse,
50}
51
52/// Semantic identity of a closed paint batch, independent of serializer offsets.
53#[derive(Debug, Clone, PartialEq, Eq, Hash)]
54pub struct PaintBatchRef {
55    owner: PanelPath,
56    role: PaintRole,
57    layer: Option<LayerSlot>,
58}
59
60impl PaintBatchRef {
61    pub(crate) fn new(owner: &PanelPath, role: PaintRole, layer: Option<LayerSlot>) -> Self {
62        Self {
63            owner: owner.clone(),
64            role,
65            layer,
66        }
67    }
68
69    /// Actual native panel or shared root owner.
70    pub fn owner(&self) -> &PanelPath {
71        &self.owner
72    }
73
74    /// Actual painting role.
75    pub const fn role(&self) -> PaintRole {
76        self.role
77    }
78
79    /// An actual declaration slot when this closed batch has a single such owner.
80    pub const fn layer(&self) -> Option<LayerSlot> {
81        self.layer
82    }
83}
84
85/// A closed batch in physical paint order, with its actual rectangular clips.
86#[derive(Clone, PartialEq)]
87pub struct PaintBatch {
88    pub(crate) reference: PaintBatchRef,
89    pub(crate) clips: Vec<Rect>,
90    pub(crate) ink: InkExtent,
91    // The four actual native emission buffers. The offsets identify the closed
92    // insertion gap in the retained native representation, never a public ID.
93    pub(crate) buffer: usize,
94    pub(crate) range: std::ops::Range<usize>,
95}
96
97impl std::fmt::Debug for PaintBatch {
98    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        formatter
100            .debug_struct("PaintBatch")
101            .field("reference", &self.reference)
102            .field("clips", &self.clips)
103            .field("ink", &self.ink)
104            .finish_non_exhaustive()
105    }
106}
107
108impl PaintBatch {
109    /// Semantic reference to the batch.
110    pub fn reference(&self) -> &PaintBatchRef {
111        &self.reference
112    }
113
114    /// Actual owner.
115    pub fn owner(&self) -> &PanelPath {
116        self.reference.owner()
117    }
118
119    /// Painting role.
120    pub const fn role(&self) -> PaintRole {
121        self.reference.role()
122    }
123
124    /// Actual rectangular clip scopes reached by this batch.
125    pub fn clips(&self) -> &[Rect] {
126        &self.clips
127    }
128
129    /// Reliable painted bounds, empty or unknown.
130    pub const fn ink(&self) -> InkExtent {
131        self.ink
132    }
133}
134
135/// Reference to one actual gap between adjacent closed native batches.
136#[derive(Debug, Clone, PartialEq, Eq, Hash)]
137pub struct BoundaryRef {
138    pub(super) owner: PanelPath,
139    pub(super) behind: Option<PaintBatchRef>,
140    pub(super) ahead: Option<PaintBatchRef>,
141}
142
143impl BoundaryRef {
144    pub(crate) fn between(behind: Option<&PaintBatch>, ahead: Option<&PaintBatch>) -> Self {
145        // All currently exposed gaps close their layer and clip scopes in the
146        // actual root. Neither neighboring batch lends its local clip.
147        Self {
148            owner: PanelPath::root(),
149            behind: behind.map(|batch| batch.reference.clone()),
150            ahead: ahead.map(|batch| batch.reference.clone()),
151        }
152    }
153
154    /// Common closed paint scope of the adjacent batches.
155    pub fn owner(&self) -> &PanelPath {
156        &self.owner
157    }
158
159    /// Actual immediately preceding batch, absent at the root's first gap.
160    pub fn behind(&self) -> Option<&PaintBatchRef> {
161        self.behind.as_ref()
162    }
163
164    /// Actual immediately following batch, absent at the root's final gap.
165    pub fn ahead(&self) -> Option<&PaintBatchRef> {
166        self.ahead.as_ref()
167    }
168}