Files
pdfcraft/crates/print/src/lib.rs
T
d749fd8062 Print colour management: the app converts printed pictures into a printer profile (#6269) (#6853)
* Print: app-managed colour converts printed pictures into a printer profile (#6269)

doc_print takes color_management (printer, the default, or app). With app,
color_profile (an ICC file or an installed profile's name), rendering_intent
and black_point_compensation convert each rendered page picture from sRGB
into the printer's RGB or grey profile before it is sent. app prints the
pages as pictures; CMYK and non-destination profiles are refused.
print_color_profiles lists the installed RGB and grey profiles.

The conversion is craft-cms at craft-print's rev, in pdfcraft_print::colour,
hooked into print_raster::rasterize. Raster carries the colour and is Clone
rather than Copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Print colour: look for installed profiles inside the read root when confined

With automation roots, print_color_profiles and doc_print's color_profile
name lookup read the system's colour folders, outside the roots. They now
look in the read root instead (as text_font_files does), check each file
against it before reading it, and are refused without read authority.
Under --root they look in the root.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* automation: keep colour profile discovery inside --root, links followed

Under --root alone, print_color_profiles and the color_profile name
lookup listed the root's .icc files without checking each against the
root, so a link inside it could read a profile outside. Each discovered
file now goes through the same --root resolution as any other read
(links followed) before it is opened.

colour_profile_links_out_of_the_root_are_not_read makes such a link and
checks it is neither listed nor found by name; it fails without the
check (the link is listed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* print: as_chunks for the grey fill; area print keeps managed colour

Clippy 1.99 (`chunks_exact_to_as_chunks`) flags the constant-width
`chunks_exact_mut(4)` in the grey conversion; use `as_chunks_mut::<4>()`.

main's print-area path built `Raster` without the new `colour` field;
build it through `PrintDraft::raster()` so a printed area is colour
managed like a printed page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Lock craft-cms at the craft-libs commit craft-print now uses

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(print): read only regular files as printer profiles, and no more than the cap

A FIFO or device named `.icc`/`.icm` in a profile folder was listed and
read, and opening a FIFO for reading waits for a writer. The listing now
keeps regular files only (links followed), `PrinterProfile::read` refuses
anything else before opening it, and the read goes through a limit one
byte past `MAX_PROFILE_BYTES`, so a file that grew after it was measured
is refused rather than read whole.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: KenM76 <KenM76>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 06:28:18 +01:00

163 lines
6.4 KiB
Rust

//! Printing (execution plan M10.5). Layer L4.
//!
//! [`impose`] turns the pages to print into a print-ready PDF of *sheets*, laid out the way
//! Acrobat's Print dialog describes them:
//! - **Size**: one page per sheet, Fit, Actual size, Shrink oversized pages or a custom scale,
//! centred;
//! - **Multiple**: pages per sheet in a grid (2, 4, 6, 9, 16 or custom), four page orders,
//! optional page borders and auto-rotation;
//! - **Booklet**: saddle-stitched spreads (both sides, front or back only; left or right
//! binding);
//! - **Poster**: each page enlarged across several sheets with overlap and cut marks;
//! - orientation (auto, portrait, landscape) and what to print (the document, with markups,
//! with stamps, or form fields only; annotations print only with their Print flag).
//!
//! The sheets are written as a new, unencrypted, garbage-collected PDF; [`spool`] hands it to
//! the system's print spooler (CUPS on macOS and Linux).
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
use pdfcraft_content::Matrix;
use pdfcraft_cos::{DecodeBudget, Dict, Document, ObjRef, Object, SaveOptions, Stream, write_full};
pub mod colour;
mod font_names;
pub mod range;
pub mod spool;
#[cfg(test)]
mod tests;
pub use range::{Subset, position, select_listed, select_pages};
mod error;
mod imposition;
mod settings;
mod sheets;
pub use error::{PrintError, WORDINGS, Words, in_english};
#[cfg(test)]
use imposition::{PAGES_LISTED, appearance, appearance_matrix, decoded_contents, page_form, prints};
pub use imposition::{impose, keep_printed, sheet_pages};
pub use settings::{
Binding, BookletSubset, Content, Crop, LABEL_PAPER, Layout, MAX_GRID, Orientation, PAPERS, PageOrder, Raster, Settings, SizeMode, booklet_duplex,
booklet_job_duplex, cups_media, custom_media, driver_needs_grayscale, media_option, paper_media, paper_media_option,
};
#[cfg(test)]
use sheets::text_direction;
pub use sheets::{
LABEL_MARGIN, MARGIN, MAX_CREEP, Placement, Sheet, add_cups_orientation, cups_orientation, layout, matching_paper, text_reads_up, texts_read_up,
};
pub use spool::{open_printer_preferences, printer_default_paper, printer_paper_info, printer_papers, show_printer_properties};
/// The names of the Print dialog's choices, shared by the `doc_print` tool and the settings
/// file, so a saved preference reads like a tool argument and there is one vocabulary to keep.
/// Each `parse_*` returns `None` for an unknown name (settings and tool arguments are
/// untrusted input; the caller keeps its default).
pub mod names {
use super::{Binding, Content, SizeMode, spool::Duplex};
/// Two-sided printing as `doc_print` names it: `None` leaves it to the printer (`printer`).
pub fn duplex(duplex: Option<Duplex>) -> &'static str {
match duplex {
None => "printer",
Some(Duplex::Off) => "off",
Some(Duplex::LongEdge) => "long-edge",
Some(Duplex::ShortEdge) => "short-edge",
}
}
/// [`duplex`] read back; `None` for a name it doesn't give.
pub fn parse_duplex(name: &str) -> Option<Option<Duplex>> {
match name {
"printer" => Some(None),
"off" => Some(Some(Duplex::Off)),
"long-edge" => Some(Some(Duplex::LongEdge)),
"short-edge" => Some(Some(Duplex::ShortEdge)),
_ => None,
}
}
/// Acrobat's Comments & Forms choices.
pub fn content(content: Content) -> &'static str {
match content {
Content::Document => "document",
Content::DocumentAndMarkups => "document-and-markups",
Content::DocumentAndStamps => "document-and-stamps",
Content::FormFieldsOnly => "form-fields-only",
}
}
pub fn parse_content(name: &str) -> Option<Content> {
match name {
"document" => Some(Content::Document),
"document-and-markups" => Some(Content::DocumentAndMarkups),
"document-and-stamps" => Some(Content::DocumentAndStamps),
"form-fields-only" => Some(Content::FormFieldsOnly),
_ => None,
}
}
/// The page sizing choices; a custom scale is named `custom` and carries its percent
/// separately (see [`parse_size_mode`]).
pub fn size_mode(mode: SizeMode) -> &'static str {
match mode {
SizeMode::Fit => "fit",
SizeMode::Actual => "actual",
SizeMode::Shrink => "shrink",
SizeMode::Custom(_) => "custom",
}
}
/// `custom` takes `scale` as its percent; the caller has validated it.
pub fn parse_size_mode(name: &str, scale: f64) -> Option<SizeMode> {
match name {
"fit" => Some(SizeMode::Fit),
"actual" => Some(SizeMode::Actual),
"shrink" => Some(SizeMode::Shrink),
"custom" => Some(SizeMode::Custom(scale)),
_ => None,
}
}
/// A booklet's binding side.
pub fn binding(binding: Binding) -> &'static str {
match binding {
Binding::Left => "left",
Binding::Right => "right",
}
}
pub fn parse_binding(name: &str) -> Option<Binding> {
match name {
"left" => Some(Binding::Left),
"right" => Some(Binding::Right),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_choice_round_trips_through_its_name_and_unknown_names_are_none() {
for d in [None, Some(Duplex::Off), Some(Duplex::LongEdge), Some(Duplex::ShortEdge)] {
assert_eq!(parse_duplex(duplex(d)), Some(d));
}
for c in [Content::Document, Content::DocumentAndMarkups, Content::DocumentAndStamps, Content::FormFieldsOnly] {
assert_eq!(parse_content(content(c)), Some(c));
}
for m in [SizeMode::Fit, SizeMode::Actual, SizeMode::Shrink, SizeMode::Custom(62.5)] {
assert_eq!(parse_size_mode(size_mode(m), 62.5), Some(m));
}
for b in [Binding::Left, Binding::Right] {
assert_eq!(parse_binding(binding(b)), Some(b));
}
assert_eq!(parse_binding("Right"), None);
assert_eq!(parse_duplex("Long-Edge"), None);
assert_eq!(parse_content(""), None);
assert_eq!(parse_size_mode("fit-to-page", 100.0), None);
}
}
}