lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! Semantic paint batches captured before serialization.

use super::{InkExtent, PanelPath, Rect};
use crate::elements::LayerSlot;

/// Semantic role of an actual closed drawing batch.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PaintRole {
    /// Root paper surface.
    RootSurface,
    /// Local plot paper surface.
    PanelSurface,
    /// Actual grid stroke batch.
    Grid,
    /// One closed scientific body containing combined dependent parts.
    Body,
    /// Scientific primary layer.
    Primary,
    /// Independently attached scientific layers.
    Overlay,
    /// Reference marks or panel annotations.
    Annotation,
    /// Significance marks.
    Significance,
    /// Scientific axes and frame.
    Axes,
    /// A local legend.
    Legend,
    /// Plot-owned prose.
    Prose,
    /// Independent text panel.
    TextPanel,
    /// One fixed local image and its independent clipping scope.
    ImagePanel,
    /// One fixed native output and its independent clipping scope.
    FrozenPanel,
    /// Canvas column and row headers.
    Headers,
    /// Canvas row titles.
    RowTitles,
    /// Facet strip.
    FacetStrip,
    /// Shared axis labels.
    SharedAxes,
    /// Shared legend.
    SharedLegend,
    /// Root title, subtitle and caption.
    RootProse,
}

/// Semantic identity of a closed paint batch, independent of serializer offsets.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PaintBatchRef {
    owner: PanelPath,
    role: PaintRole,
    layer: Option<LayerSlot>,
}

impl PaintBatchRef {
    pub(crate) fn new(owner: &PanelPath, role: PaintRole, layer: Option<LayerSlot>) -> Self {
        Self {
            owner: owner.clone(),
            role,
            layer,
        }
    }

    /// Actual native panel or shared root owner.
    pub fn owner(&self) -> &PanelPath {
        &self.owner
    }

    /// Actual painting role.
    pub const fn role(&self) -> PaintRole {
        self.role
    }

    /// An actual declaration slot when this closed batch has a single such owner.
    pub const fn layer(&self) -> Option<LayerSlot> {
        self.layer
    }
}

/// A closed batch in physical paint order, with its actual rectangular clips.
#[derive(Clone, PartialEq)]
pub struct PaintBatch {
    pub(crate) reference: PaintBatchRef,
    pub(crate) clips: Vec<Rect>,
    pub(crate) ink: InkExtent,
    // The four actual native emission buffers. The offsets identify the closed
    // insertion gap in the retained native representation, never a public ID.
    pub(crate) buffer: usize,
    pub(crate) range: std::ops::Range<usize>,
}

impl std::fmt::Debug for PaintBatch {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("PaintBatch")
            .field("reference", &self.reference)
            .field("clips", &self.clips)
            .field("ink", &self.ink)
            .finish_non_exhaustive()
    }
}

impl PaintBatch {
    /// Semantic reference to the batch.
    pub fn reference(&self) -> &PaintBatchRef {
        &self.reference
    }

    /// Actual owner.
    pub fn owner(&self) -> &PanelPath {
        self.reference.owner()
    }

    /// Painting role.
    pub const fn role(&self) -> PaintRole {
        self.reference.role()
    }

    /// Actual rectangular clip scopes reached by this batch.
    pub fn clips(&self) -> &[Rect] {
        &self.clips
    }

    /// Reliable painted bounds, empty or unknown.
    pub const fn ink(&self) -> InkExtent {
        self.ink
    }
}

/// Reference to one actual gap between adjacent closed native batches.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BoundaryRef {
    pub(super) owner: PanelPath,
    pub(super) behind: Option<PaintBatchRef>,
    pub(super) ahead: Option<PaintBatchRef>,
}

impl BoundaryRef {
    pub(crate) fn between(behind: Option<&PaintBatch>, ahead: Option<&PaintBatch>) -> Self {
        // All currently exposed gaps close their layer and clip scopes in the
        // actual root. Neither neighboring batch lends its local clip.
        Self {
            owner: PanelPath::root(),
            behind: behind.map(|batch| batch.reference.clone()),
            ahead: ahead.map(|batch| batch.reference.clone()),
        }
    }

    /// Common closed paint scope of the adjacent batches.
    pub fn owner(&self) -> &PanelPath {
        &self.owner
    }

    /// Actual immediately preceding batch, absent at the root's first gap.
    pub fn behind(&self) -> Option<&PaintBatchRef> {
        self.behind.as_ref()
    }

    /// Actual immediately following batch, absent at the root's final gap.
    pub fn ahead(&self) -> Option<&PaintBatchRef> {
        self.ahead.as_ref()
    }
}