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