lichtung-plot 0.2.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! Plot terminal output rendering.

use std::path::Path;

use crate::builder::Plot;
use crate::{
    operation::{self, OperationOptions},
    Error, Output,
};

impl<K: crate::kind::Marker> Plot<K> {
    /// Prepare and freeze one complete scene without creating an output file.
    ///
    /// Subsequent publication of the returned snapshot never repeats scientific
    /// preparation, numeric callbacks, or layout.
    pub fn render(&self) -> Result<Output, Error> {
        self.render_with_operation(&OperationOptions::default())
    }

    /// Freeze one complete scene using this call's operation options.
    pub fn render_with_operation(&self, options: &OperationOptions) -> Result<Output, Error> {
        operation::run(options, || self.freeze())
    }

    /// Freeze the figure and capture its scientific values from the same preparation.
    pub fn render_with_results(&self) -> Result<crate::stats::RenderWithResults, Error> {
        self.render_with_results_with_operation(&OperationOptions::default())
    }

    /// Freeze with same-operation scientific results and explicit resource options.
    pub fn render_with_results_with_operation(
        &self,
        options: &OperationOptions,
    ) -> Result<crate::stats::RenderWithResults, Error> {
        operation::run(options, || {
            super::render_scene_with_results(super::root::RootSource::Plot(self.description()))
        })
    }

    fn freeze(&self) -> Result<Output, Error> {
        Ok(Output::new(super::render_scene(
            super::root::RootSource::Plot(self.description()),
        )?))
    }

    /// Borrow this configuration, render once and publish an outlined SVG.
    /// Each call performs a new render and returns a reusable frozen output.
    /// The output filename is completed with `.svg` after conversion.
    pub fn svg(&self, path: impl AsRef<Path>) -> Result<Output, Error> {
        self.svg_with_operation(path, &OperationOptions::default())
    }

    /// Render and publish SVG using this call's operation options.
    pub fn svg_with_operation(
        &self,
        path: impl AsRef<Path>,
        options: &OperationOptions,
    ) -> Result<Output, Error> {
        operation::run(options, || self.freeze()?.publish_svg(path, false))
    }

    /// Render once, publish SVG with selectable text, and return a reusable output.
    /// The output filename is completed with `.svg` after rendering.
    pub fn svg_preserve_text(&self, path: impl AsRef<Path>) -> Result<Output, Error> {
        self.svg_preserve_text_with_operation(path, &OperationOptions::default())
    }

    /// Render and publish SVG using this call's operation options.
    pub fn svg_preserve_text_with_operation(
        &self,
        path: impl AsRef<Path>,
        options: &OperationOptions,
    ) -> Result<Output, Error> {
        operation::run(options, || self.freeze()?.publish_svg(path, true))
    }

    /// Render once, publish a balanced 300-DPI PNG, and return a reusable output.
    /// The output filename is completed with `.png` after rasterization.
    pub fn png(&self, path: impl AsRef<Path>) -> Result<Output, Error> {
        self.png_with_operation(path, &OperationOptions::default())
    }

    /// Render and publish PNG using this call's operation options.
    pub fn png_with_operation(
        &self,
        path: impl AsRef<Path>,
        options: &OperationOptions,
    ) -> Result<Output, Error> {
        self.png_with_options_with_operation(path, crate::output::PngOptions::default(), options)
    }

    /// Render once, publish a balanced source-sized PNG without density metadata.
    /// The output filename is completed with `.png` after rasterization.
    pub fn png_preview(&self, path: impl AsRef<Path>) -> Result<Output, Error> {
        self.png_preview_with_operation(path, &OperationOptions::default())
    }

    /// Render and publish PNG using this call's operation options.
    pub fn png_preview_with_operation(
        &self,
        path: impl AsRef<Path>,
        options: &OperationOptions,
    ) -> Result<Output, Error> {
        self.png_with_options_with_operation(
            path,
            crate::output::PngOptions {
                sizing: crate::output::PngSizing::Source,
                compression: crate::output::PngCompression::Balanced,
            },
            options,
        )
    }

    /// Render once and publish a PNG using the exact caller-provided options.
    /// The output filename is completed with `.png` after rasterization.
    pub fn png_with_options(
        &self,
        path: impl AsRef<Path>,
        options: crate::output::PngOptions,
    ) -> Result<Output, Error> {
        self.png_with_options_with_operation(path, options, &OperationOptions::default())
    }

    /// Render and publish PNG with device options and a single operation budget.
    pub fn png_with_options_with_operation(
        &self,
        path: impl AsRef<Path>,
        options: crate::output::PngOptions,
        operation: &OperationOptions,
    ) -> Result<Output, Error> {
        operation::run(operation, || self.freeze()?.publish_png(path, &options))
    }
}