use std::ffi::OsString;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::errors::OperationPhase;
use crate::operation::{self, OperationOptions};
use crate::scene::RenderedScene;
use crate::{
output::{PngCompression, PngOptions, PngSizing},
Error,
};
#[derive(Clone)]
pub struct Output {
scene: Arc<RenderedScene>,
}
impl Output {
pub(crate) fn new(scene: RenderedScene) -> Self {
Self {
scene: Arc::new(scene),
}
}
#[must_use]
pub fn width(&self) -> f64 {
self.scene
.size()
.dimensions()
.expect("frozen scene geometry")
.0
}
#[must_use]
pub fn height(&self) -> f64 {
self.scene
.size()
.dimensions()
.expect("frozen scene geometry")
.1
}
pub fn physical_size(&self) -> Option<crate::layout::PhysicalSize> {
self.scene.size().physical_size()
}
pub fn svg(&self, path: impl AsRef<Path>) -> Result<Self, Error> {
self.svg_with_operation(path, &OperationOptions::default())
}
pub fn svg_with_operation(
&self,
path: impl AsRef<Path>,
options: &OperationOptions,
) -> Result<Self, Error> {
operation::run(options, || self.publish_svg(path, false))
}
pub fn svg_preserve_text(&self, path: impl AsRef<Path>) -> Result<Self, Error> {
self.svg_preserve_text_with_operation(path, &OperationOptions::default())
}
pub fn svg_preserve_text_with_operation(
&self,
path: impl AsRef<Path>,
options: &OperationOptions,
) -> Result<Self, Error> {
operation::run(options, || self.publish_svg(path, true))
}
pub(crate) fn publish_svg(
&self,
path: impl AsRef<Path>,
preserve_text: bool,
) -> Result<Self, Error> {
operation::phase(OperationPhase::Write)?;
let bytes = if preserve_text {
self.scene.svg_bytes()
} else {
self.scene.outlined_svg_bytes()?
};
operation::checkpoint()?;
let path = operation::external(|| complete_suffix(path.as_ref(), ".svg"))?;
crate::orchestrator::write_atomic(&path, bytes)?;
Ok(self.clone())
}
pub fn png(&self, path: impl AsRef<Path>) -> Result<Self, Error> {
self.png_with_operation(path, &OperationOptions::default())
}
pub fn png_with_operation(
&self,
path: impl AsRef<Path>,
options: &OperationOptions,
) -> Result<Self, Error> {
self.png_with_options_with_operation(path, PngOptions::default(), options)
}
pub fn png_preview(&self, path: impl AsRef<Path>) -> Result<Self, Error> {
self.png_preview_with_operation(path, &OperationOptions::default())
}
pub fn png_preview_with_operation(
&self,
path: impl AsRef<Path>,
options: &OperationOptions,
) -> Result<Self, Error> {
self.png_with_options_with_operation(
path,
PngOptions {
sizing: PngSizing::Source,
compression: PngCompression::Balanced,
},
options,
)
}
pub fn png_with_options(
&self,
path: impl AsRef<Path>,
options: PngOptions,
) -> Result<Self, Error> {
self.png_with_options_with_operation(path, options, &OperationOptions::default())
}
pub fn png_with_options_with_operation(
&self,
path: impl AsRef<Path>,
options: PngOptions,
operation: &OperationOptions,
) -> Result<Self, Error> {
operation::run(operation, || self.publish_png(path, &options))
}
pub(crate) fn publish_png(
&self,
path: impl AsRef<Path>,
options: &PngOptions,
) -> Result<Self, Error> {
operation::phase(OperationPhase::Write)?;
let bytes = self.scene.png_bytes(options)?;
operation::checkpoint()?;
let path = operation::external(|| 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))
}