lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! Decoration orchestration for single-panel plots.

use crate::Error;

use crate::decorations::plan::MeasuredPlotPlan;
use crate::layout::{PaintRole, PanelPath, PixelRect};
use crate::render::backend::{
    with_layer, with_layer_role, with_paint, Backend, LayerRole, SurfaceRole,
};

pub(crate) fn root_surface(
    backend: &mut dyn Backend,
    bounds: PixelRect,
    paint: crate::Color,
) -> Result<(), Error> {
    with_paint(
        backend,
        &PanelPath::root(),
        PaintRole::RootSurface,
        None,
        |backend| backend.draw_surface(bounds, paint, SurfaceRole::CanvasRoot),
    )
}

pub(crate) fn panel_surface(
    plan: &MeasuredPlotPlan,
    backend: &mut dyn Backend,
) -> Result<(), Error> {
    with_paint(
        backend,
        &plan.panel_path,
        PaintRole::PanelSurface,
        None,
        |backend| backend.draw_surface(plan.panel_box, plan.surface, SurfaceRole::PlotPanel),
    )
}

/// Draw one panel's grid before its marks.
pub(crate) fn apply_grid(
    layer: &str,
    plan: &MeasuredPlotPlan,
    backend: &mut dyn Backend,
) -> Result<(), Error> {
    with_paint(
        backend,
        &plan.panel_path,
        PaintRole::Grid,
        None,
        |backend| {
            with_layer_role(backend, layer, LayerRole::Grid, |backend| {
                plan.result
                    .axes
                    .as_ref()
                    .ok_or_else(|| {
                        Error::Render("standard panel plan is missing immutable axes".into())
                    })?
                    .draw_grid(backend)
            })
        },
    )
}

/// Draw one panel's axes after its marks.
pub(crate) fn apply_axes(
    layer: &str,
    plan: &MeasuredPlotPlan,
    backend: &mut dyn Backend,
) -> Result<(), Error> {
    with_paint(
        backend,
        &plan.panel_path,
        PaintRole::Axes,
        None,
        |backend| {
            with_layer(backend, layer, |backend| {
                let axes = plan.result.axes.as_ref().ok_or_else(|| {
                    Error::Render("standard panel plan is missing immutable axes".into())
                })?;
                axes.draw(backend)
            })
        },
    )
}

pub(crate) fn draw_body(
    plan: &MeasuredPlotPlan,
    source: &crate::builder::PlotDescription,
    backend: &mut dyn Backend,
) -> Result<crate::spec::DecorationMode, Error> {
    let mode = plan.result.draw_body(&plan.panel_path, source, backend)?;
    with_paint(
        backend,
        &plan.panel_path,
        PaintRole::Significance,
        None,
        |backend| crate::significance::render_selected(&plan.result, backend),
    )?;
    Ok(mode)
}