1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! Opaque terminal output handle.
use std::ffi::OsString;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::scene::RenderedScene;
use crate::{Error, PngCompression, PngOptions, PngSizing};
/// Opaque handle to one scene that has already been rendered.
///
/// Each output method borrows this handle, publishes its requested file immediately
/// and returns a shared handle to the same frozen scene for chaining. Publication
/// failure leaves the original handle usable; cloning never renders again.
///
/// Terminal methods complete the final native filename with their conventional
/// lowercase suffix. A filename already ending in the complete expected suffix
/// under ASCII case-insensitive comparison is preserved exactly; otherwise the
/// suffix is appended literally. Other filename text is never interpreted or
/// replaced, so `plot.v1` becomes `plot.v1.svg` and `plot.png` passed to
/// [`Output::svg`] becomes `plot.png.svg`.
///
/// Suffix completion happens only after format conversion succeeds. The caller
/// path must have a filename and must not currently identify a directory. Its
/// parent must already exist.
#[derive(Clone)]
pub struct Output {
scene: Arc<RenderedScene>,
}
impl Output {
pub(crate) fn new(scene: RenderedScene) -> Self {
Self {
scene: Arc::new(scene),
}
}
/// Frozen canonical scene width in integral CSS pixels.
///
/// This is the SVG root width, independent of PNG raster sizing. Inspecting
/// it does not measure, render, or alter the retained scene.
#[must_use]
pub fn width(&self) -> u32 {
self.scene.dimensions().0
}
/// Frozen canonical scene height in integral CSS pixels.
///
/// This is the SVG root height, independent of PNG raster sizing. All
/// chained publications retain the same canonical dimensions.
#[must_use]
pub fn height(&self) -> u32 {
self.scene.dimensions().1
}
/// Publish a self-contained outlined SVG atomically.
///
/// After text outlining succeeds, the final native filename is completed
/// with `.svg`. A complete ASCII case-insensitive match such as `.SVG` is
/// preserved exactly; otherwise `.svg` is appended without interpreting an
/// existing extension.
pub fn svg(&self, path: impl AsRef<Path>) -> Result<Self, Error> {
let bytes = self.scene.svg_with_text_as_paths()?;
let path = complete_suffix(path.as_ref(), ".svg")?;
crate::orchestrator::write_atomic(&path, &bytes)?;
Ok(self.clone())
}
/// Publish SVG with selectable and searchable text atomically.
///
/// After borrowing the canonical SVG bytes, the final native filename is
/// completed with `.svg` using the literal rule documented on [`Output`].
pub fn svg_preserve_text(&self, path: impl AsRef<Path>) -> Result<Self, Error> {
let bytes = self.scene.svg_bytes();
let path = complete_suffix(path.as_ref(), ".svg")?;
crate::orchestrator::write_atomic(&path, bytes)?;
Ok(self.clone())
}
/// Publish a 300-DPI balanced-compression PNG atomically.
///
/// The final native filename is completed with `.png` by
/// [`Output::png_with_options`] after rasterization succeeds.
pub fn png(&self, path: impl AsRef<Path>) -> Result<Self, Error> {
self.png_with_options(path, PngOptions::default())
}
/// Publish a source-sized balanced-compression PNG without density metadata.
///
/// The final native filename is completed with `.png` by
/// [`Output::png_with_options`] after rasterization succeeds.
pub fn png_preview(&self, path: impl AsRef<Path>) -> Result<Self, Error> {
self.png_with_options(
path,
PngOptions {
sizing: PngSizing::Source,
compression: PngCompression::Balanced,
},
)
}
/// Publish a PNG atomically using the exact caller-provided options.
///
/// After rasterization succeeds, the final native filename is completed
/// with `.png`. A complete ASCII case-insensitive match such as `.PNG` is
/// preserved exactly; otherwise `.png` is appended without interpreting an
/// existing extension.
pub fn png_with_options(
&self,
path: impl AsRef<Path>,
options: PngOptions,
) -> Result<Self, Error> {
let bytes = self.scene.png_bytes(&options)?;
let path = complete_suffix(path.as_ref(), ".png")?;
crate::orchestrator::write_atomic(&path, &bytes)?;
Ok(self.clone())
}
}
fn complete_suffix(path: &Path, expected: &str) -> Result<PathBuf, Error> {
if path.is_dir() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!(
"render output path {} identifies a directory",
path.display()
),
)
.into());
}
let file_name = path.file_name().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
format!("render output path {} has no file name", path.display()),
)
})?;
let encoded = file_name.as_encoded_bytes();
if encoded
.get(encoded.len().saturating_sub(expected.len())..)
.is_some_and(|ending| ending.eq_ignore_ascii_case(expected.as_bytes()))
{
return Ok(path.to_path_buf());
}
let mut completed = OsString::from(file_name);
completed.push(expected);
Ok(path.with_file_name(completed))
}