use std::path::{Path, PathBuf};
use std::sync::Mutex;
use super::{
DeviceCapabilities, JobId, JobStatus, PrinterId, PrinterInfo, SpoolJob, SpoolPayload, Spooler,
};
use crate::print::compose::SheetRaster;
use crate::print::paper::Margins;
use crate::print::{ComposeOptions, PrintError, PrintOptions};
pub const FILE_PRINTER: &str = "file";
#[derive(Debug, Clone, PartialEq)]
pub struct RecordedJob {
pub id: JobId,
pub printer: PrinterId,
pub title: String,
pub files: Vec<PathBuf>,
pub pass_through: bool,
pub options: PrintOptions,
pub compose: Option<ComposeOptions>,
}
#[derive(Debug)]
pub struct FileSpooler {
dir: PathBuf,
jobs: Mutex<Vec<RecordedJob>>,
sheets_written: Mutex<u32>,
}
impl FileSpooler {
#[must_use]
pub fn new(dir: impl Into<PathBuf>) -> Self {
Self {
dir: dir.into(),
jobs: Mutex::new(Vec::new()),
sheets_written: Mutex::new(0),
}
}
#[must_use]
pub fn dir(&self) -> &Path {
&self.dir
}
#[must_use]
pub fn recorded(&self) -> Vec<RecordedJob> {
lock(&self.jobs).clone()
}
#[must_use]
pub fn last_job(&self) -> Option<RecordedJob> {
lock(&self.jobs).last().cloned()
}
pub fn submit_rasters(
&self,
printer: &PrinterId,
title: &str,
options: &PrintOptions,
compose: ComposeOptions,
rasters: &[SheetRaster],
) -> Result<JobId, PrintError> {
self.submit_raster_stream(printer, title, options, compose, rasters.iter().map(Ok))
}
pub fn submit_raster_stream<R, I>(
&self,
printer: &PrinterId,
title: &str,
options: &PrintOptions,
compose: ComposeOptions,
rasters: I,
) -> Result<JobId, PrintError>
where
R: std::borrow::Borrow<SheetRaster>,
I: IntoIterator<Item = Result<R, PrintError>>,
{
std::fs::create_dir_all(&self.dir)?;
let mut files = Vec::new();
let mut numbers = Vec::new();
let write_all = || -> Result<(), PrintError> {
for raster in rasters {
let raster = raster?;
let number = self.next_sheet_number();
let path = self.dir.join(sheet_name(number));
numbers.push(number);
files.push(path.clone());
write_sheet_png(&path, raster.borrow())?;
}
Ok(())
};
if let Err(error) = write_all() {
self.discard_sheets(&files, &numbers);
return Err(error);
}
Ok(self.record(printer, title, options, files, false, Some(compose)))
}
fn next_sheet_number(&self) -> u32 {
let mut count = lock(&self.sheets_written);
*count += 1;
*count
}
fn discard_sheets(&self, files: &[PathBuf], numbers: &[u32]) {
for path in files {
let _ = std::fs::remove_file(path);
}
let (Some(&first), Some(&last)) = (numbers.first(), numbers.last()) else {
return;
};
let mut count = lock(&self.sheets_written);
let contiguous = (last - first) as usize + 1 == numbers.len();
if contiguous && *count == last {
*count = first - 1;
}
}
fn record(
&self,
printer: &PrinterId,
title: &str,
options: &PrintOptions,
files: Vec<PathBuf>,
pass_through: bool,
compose: Option<ComposeOptions>,
) -> JobId {
let mut jobs = lock(&self.jobs);
let id = JobId(format!("{FILE_PRINTER}-{}", jobs.len() + 1));
jobs.push(RecordedJob {
id: id.clone(),
printer: printer.clone(),
title: title.to_string(),
files,
pass_through,
options: options.clone(),
compose,
});
id
}
fn job_count(&self) -> usize {
lock(&self.jobs).len()
}
}
impl Spooler for FileSpooler {
fn printers(&self) -> Result<Vec<PrinterInfo>, PrintError> {
Ok(vec![PrinterInfo {
id: PrinterId::new(FILE_PRINTER),
description: Some(format!("File spooler ({})", self.dir.display())),
location: Some(self.dir.display().to_string()),
is_default: true,
accepting_jobs: true,
}])
}
fn capabilities(&self, _printer: &PrinterId) -> Result<DeviceCapabilities, PrintError> {
Ok(DeviceCapabilities {
hardware_margins: Margins::ZERO,
supports_duplex: true,
supports_color: true,
resolution_dpi: Some(ComposeOptions::default().dpi),
accepts_pdf: true,
})
}
fn submit(&self, job: SpoolJob<'_>) -> Result<JobId, PrintError> {
match job.payload {
SpoolPayload::PassThroughPdf(bytes) => {
std::fs::create_dir_all(&self.dir)?;
let path = self
.dir
.join(format!("document-{:04}.pdf", self.job_count() + 1));
std::fs::write(&path, bytes)?;
Ok(self.record(
&job.printer,
&job.title,
job.options,
vec![path],
true,
None,
))
}
SpoolPayload::Sheets {
session,
sheets,
compose,
} => {
let rasters = sheets
.iter()
.map(|sheet| crate::print::compose::compose_sheet(session, sheet, &compose));
self.submit_raster_stream(&job.printer, &job.title, job.options, compose, rasters)
}
}
}
fn status(&self, _job: &JobId) -> Result<JobStatus, PrintError> {
Ok(JobStatus::Completed)
}
fn cancel(&self, _job: &JobId) -> Result<(), PrintError> {
Ok(())
}
}
fn sheet_name(number: u32) -> String {
format!("sheet-{number:04}.png")
}
pub fn write_sheet_png(path: &Path, raster: &SheetRaster) -> Result<(), PrintError> {
let color = match raster.channels {
1 => png::ColorType::Grayscale,
3 => png::ColorType::Rgb,
4 => png::ColorType::Rgba,
other => {
return Err(PrintError::Spool(format!(
"cannot encode a {other}-channel sheet raster as PNG"
)));
}
};
let expected = u64::from(raster.width) * u64::from(raster.height) * u64::from(raster.channels);
if expected != raster.pixels.len() as u64 {
return Err(PrintError::Spool(format!(
"sheet raster is {} bytes, expected {expected} for {}x{}x{}",
raster.pixels.len(),
raster.width,
raster.height,
raster.channels
)));
}
let file = std::fs::File::create(path)?;
let mut encoder = png::Encoder::new(std::io::BufWriter::new(file), raster.width, raster.height);
encoder.set_color(color);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().map_err(png_error)?;
writer.write_image_data(&raster.pixels).map_err(png_error)?;
writer.finish().map_err(png_error)?;
Ok(())
}
fn png_error(error: png::EncodingError) -> PrintError {
match error {
png::EncodingError::IoError(io) => PrintError::Io(io),
other => PrintError::Spool(format!("png encoding failed: {other}")),
}
}
fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
mutex
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}