Skip to main content

lichtung_plot/orchestrator/
output.rs

1//! Plot terminal output rendering.
2
3use std::path::Path;
4
5use crate::builder::Plot;
6use crate::{
7    operation::{self, OperationOptions},
8    Error, Output,
9};
10
11impl<K: crate::kind::Marker> Plot<K> {
12    /// Prepare and freeze one complete scene without creating an output file.
13    ///
14    /// Subsequent publication of the returned snapshot never repeats scientific
15    /// preparation, numeric callbacks, or layout.
16    pub fn render(&self) -> Result<Output, Error> {
17        self.render_with_operation(&OperationOptions::default())
18    }
19
20    /// Freeze one complete scene using this call's operation options.
21    pub fn render_with_operation(&self, options: &OperationOptions) -> Result<Output, Error> {
22        operation::run(options, || self.freeze())
23    }
24
25    /// Freeze the figure and capture its scientific values from the same preparation.
26    pub fn render_with_results(&self) -> Result<crate::stats::RenderWithResults, Error> {
27        self.render_with_results_with_operation(&OperationOptions::default())
28    }
29
30    /// Freeze with same-operation scientific results and explicit resource options.
31    pub fn render_with_results_with_operation(
32        &self,
33        options: &OperationOptions,
34    ) -> Result<crate::stats::RenderWithResults, Error> {
35        operation::run(options, || {
36            super::render_scene_with_results(super::root::RootSource::Plot(self.description()))
37        })
38    }
39
40    fn freeze(&self) -> Result<Output, Error> {
41        Ok(Output::new(super::render_scene(
42            super::root::RootSource::Plot(self.description()),
43        )?))
44    }
45
46    /// Borrow this configuration, render once and publish an outlined SVG.
47    /// Each call performs a new render and returns a reusable frozen output.
48    /// The output filename is completed with `.svg` after conversion.
49    pub fn svg(&self, path: impl AsRef<Path>) -> Result<Output, Error> {
50        self.svg_with_operation(path, &OperationOptions::default())
51    }
52
53    /// Render and publish SVG using this call's operation options.
54    pub fn svg_with_operation(
55        &self,
56        path: impl AsRef<Path>,
57        options: &OperationOptions,
58    ) -> Result<Output, Error> {
59        operation::run(options, || self.freeze()?.publish_svg(path, false))
60    }
61
62    /// Render once, publish SVG with selectable text, and return a reusable output.
63    /// The output filename is completed with `.svg` after rendering.
64    pub fn svg_preserve_text(&self, path: impl AsRef<Path>) -> Result<Output, Error> {
65        self.svg_preserve_text_with_operation(path, &OperationOptions::default())
66    }
67
68    /// Render and publish SVG using this call's operation options.
69    pub fn svg_preserve_text_with_operation(
70        &self,
71        path: impl AsRef<Path>,
72        options: &OperationOptions,
73    ) -> Result<Output, Error> {
74        operation::run(options, || self.freeze()?.publish_svg(path, true))
75    }
76
77    /// Render once, publish a balanced 300-DPI PNG, and return a reusable output.
78    /// The output filename is completed with `.png` after rasterization.
79    pub fn png(&self, path: impl AsRef<Path>) -> Result<Output, Error> {
80        self.png_with_operation(path, &OperationOptions::default())
81    }
82
83    /// Render and publish PNG using this call's operation options.
84    pub fn png_with_operation(
85        &self,
86        path: impl AsRef<Path>,
87        options: &OperationOptions,
88    ) -> Result<Output, Error> {
89        self.png_with_options_with_operation(path, crate::output::PngOptions::default(), options)
90    }
91
92    /// Render once, publish a balanced source-sized PNG without density metadata.
93    /// The output filename is completed with `.png` after rasterization.
94    pub fn png_preview(&self, path: impl AsRef<Path>) -> Result<Output, Error> {
95        self.png_preview_with_operation(path, &OperationOptions::default())
96    }
97
98    /// Render and publish PNG using this call's operation options.
99    pub fn png_preview_with_operation(
100        &self,
101        path: impl AsRef<Path>,
102        options: &OperationOptions,
103    ) -> Result<Output, Error> {
104        self.png_with_options_with_operation(
105            path,
106            crate::output::PngOptions {
107                sizing: crate::output::PngSizing::Source,
108                compression: crate::output::PngCompression::Balanced,
109            },
110            options,
111        )
112    }
113
114    /// Render once and publish a PNG using the exact caller-provided options.
115    /// The output filename is completed with `.png` after rasterization.
116    pub fn png_with_options(
117        &self,
118        path: impl AsRef<Path>,
119        options: crate::output::PngOptions,
120    ) -> Result<Output, Error> {
121        self.png_with_options_with_operation(path, options, &OperationOptions::default())
122    }
123
124    /// Render and publish PNG with device options and a single operation budget.
125    pub fn png_with_options_with_operation(
126        &self,
127        path: impl AsRef<Path>,
128        options: crate::output::PngOptions,
129        operation: &OperationOptions,
130    ) -> Result<Output, Error> {
131        operation::run(operation, || self.freeze()?.publish_png(path, &options))
132    }
133}