use std::ffi::OsString;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::operation::OperationOptions;
use crate::scene::RenderedScene;
use crate::{
output::{Format, PngCompression, PngOptions, PngSizing},
Error,
};
#[derive(Debug, Clone)]
pub struct Output {
scene: Arc<RenderedScene>,
}
impl Output {
pub(crate) fn scene(&self) -> &RenderedScene {
&self.scene
}
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 layout_report(&self) -> Result<&crate::layout::LayoutReport, Error> {
self.scene.layout_report()
}
pub fn image_font_substitutions(&self) -> Vec<crate::composition::ImageFontSubstitution> {
let mut substitutions = Vec::new();
self.scene
.collect_image_font_substitutions(&mut substitutions);
substitutions
}
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> {
self.save_with_operation(path, Format::svg(), options)
}
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> {
self.save_with_operation(path, Format::svg().preserve_text(true), options)
}
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> {
self.save_with_operation(path, options, operation)
}
}
pub(super) 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))
}