mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-04 13:03:19 -05:00
* 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * Lock craft-cms at the craft-libs commit craft-print now uses Co-Authored-By: Claude Opus 5.5 <[email protected]> * 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 <[email protected]> --------- Co-authored-by: KenM76 <KenM76> Co-authored-by: Claude Opus 5.5 <[email protected]>
356 lines
13 KiB
Rust
356 lines
13 KiB
Rust
use super::spool;
|
||
use crate::matching_paper;
|
||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||
pub enum Orientation {
|
||
#[default]
|
||
Auto,
|
||
Portrait,
|
||
Landscape,
|
||
}
|
||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Default)]
|
||
pub enum SizeMode {
|
||
#[default]
|
||
Fit,
|
||
Actual,
|
||
Shrink,
|
||
/// Percent.
|
||
Custom(f64),
|
||
}
|
||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||
pub enum PageOrder {
|
||
#[default]
|
||
Horizontal,
|
||
HorizontalReversed,
|
||
Vertical,
|
||
VerticalReversed,
|
||
/// Single-sided sheets: cut into cell piles, then stack them in row-major order.
|
||
CutStack,
|
||
}
|
||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||
pub enum BookletSubset {
|
||
#[default]
|
||
BothSides,
|
||
FrontOnly,
|
||
BackOnly,
|
||
}
|
||
|
||
/// How a booklet goes through the printer (#2707): it decides two-sided printing itself, as
|
||
/// Acrobat does (its Print dialog hides Two-sided while Booklet is chosen; Using Acrobat X Pro,
|
||
/// Print dialog box options: "Acrobat automatically enables duplex printing, if available, for
|
||
/// booklet printing"). Both sides flip on the short edge, the fold of a 2-up sheet turned
|
||
/// sideways; front side only or back side only is one pass of a manual duplex and prints
|
||
/// single-sided.
|
||
pub fn booklet_duplex(subset: BookletSubset) -> spool::Duplex {
|
||
match subset {
|
||
BookletSubset::BothSides => spool::Duplex::ShortEdge,
|
||
BookletSubset::FrontOnly | BookletSubset::BackOnly => spool::Duplex::Off,
|
||
}
|
||
}
|
||
|
||
/// What a booklet's job asks the printer for, given whether its driver takes two-sided settings
|
||
/// from other programs (`two_sided`, as the printer list reports it): [`booklet_duplex`], only as
|
||
/// Acrobat enables two-sided printing "if available". A driver that doesn't (`Some(false)`) would
|
||
/// refuse a two-sided job and print nothing, so both sides ask for nothing and its own setting
|
||
/// applies, such as manual two-sided printing chosen in its Properties (#5772). A driver of
|
||
/// unknown support is asked for two-sided as before; one side on its own is always one-sided.
|
||
pub fn booklet_job_duplex(subset: BookletSubset, two_sided: Option<bool>) -> Option<spool::Duplex> {
|
||
match booklet_duplex(subset) {
|
||
spool::Duplex::Off => Some(spool::Duplex::Off),
|
||
_ if two_sided == Some(false) => None,
|
||
two_sided_duplex => Some(two_sided_duplex),
|
||
}
|
||
}
|
||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||
pub enum Binding {
|
||
#[default]
|
||
Left,
|
||
Right,
|
||
}
|
||
|
||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||
pub enum Layout {
|
||
Size(SizeMode),
|
||
Multiple {
|
||
cols: usize,
|
||
rows: usize,
|
||
/// Source pages on each sheet, at most `cols × rows`: three per sheet uses a 2 × 2 grid
|
||
/// with one cell left blank.
|
||
per_sheet: usize,
|
||
order: PageOrder,
|
||
border: bool,
|
||
auto_rotate: bool,
|
||
/// A custom grid (the Print dialog's Custom pages per sheet): `cols` across and `rows`
|
||
/// down the sheet whatever its orientation, which is not picked for the grid, so Auto
|
||
/// prints portrait. A predefined count (`false`, [`Layout::multiple`]) is a grid on a
|
||
/// portrait sheet that turns to landscape when that suits it.
|
||
custom: bool,
|
||
},
|
||
/// `creep` (points, per sheet) moves sheet k's pages toward the fold by `k × creep`, sheet 0
|
||
/// being the outermost, so the margins match once the nested sheets are folded and trimmed.
|
||
Booklet {
|
||
subset: BookletSubset,
|
||
binding: Binding,
|
||
creep: f64,
|
||
},
|
||
/// Tile scale (percent), overlap (points), cut marks.
|
||
Poster {
|
||
scale: f64,
|
||
overlap: f64,
|
||
cut_marks: bool,
|
||
},
|
||
/// Shipping label: each page cut to its [`Settings::crops`] box, turned so its text reads
|
||
/// upright on the sheet and fitted with [`LABEL_MARGIN`]; `flip` turns it a further 180°.
|
||
Label {
|
||
flip: bool,
|
||
},
|
||
}
|
||
|
||
impl Default for Layout {
|
||
fn default() -> Self {
|
||
Layout::Size(SizeMode::Fit)
|
||
}
|
||
}
|
||
|
||
impl Layout {
|
||
/// Acrobat's pages-per-sheet choices as a grid (columns × rows on a portrait sheet).
|
||
pub fn multiple(per_sheet: usize) -> Layout {
|
||
let (cols, rows) = match per_sheet {
|
||
2 => (1, 2),
|
||
4 => (2, 2),
|
||
6 => (2, 3),
|
||
9 => (3, 3),
|
||
16 => (4, 4),
|
||
n => {
|
||
let c = (n as f64).sqrt().ceil().max(1.0) as usize;
|
||
(c, n.div_ceil(c).max(1))
|
||
}
|
||
};
|
||
Layout::Multiple { cols, rows, per_sheet: per_sheet.max(1), order: PageOrder::Horizontal, border: false, auto_rotate: true, custom: false }
|
||
}
|
||
|
||
/// A custom grid of `cols` across by `rows` down, each 1 to [`MAX_GRID`]; every cell holds a
|
||
/// page. `None` for a count outside that.
|
||
pub fn grid(cols: usize, rows: usize) -> Option<Layout> {
|
||
if !(1..=MAX_GRID).contains(&cols) || !(1..=MAX_GRID).contains(&rows) {
|
||
return None;
|
||
}
|
||
// At most 99 × 99, so this cannot overflow.
|
||
let per_sheet = cols.checked_mul(rows)?;
|
||
Some(Layout::Multiple { cols, rows, per_sheet, order: PageOrder::Horizontal, border: false, auto_rotate: true, custom: true })
|
||
}
|
||
}
|
||
|
||
/// The most columns, or rows, a custom pages-per-sheet grid can have.
|
||
pub const MAX_GRID: usize = 99;
|
||
|
||
/// Acrobat's Comments & Forms choices.
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||
pub enum Content {
|
||
/// Page content and form fields.
|
||
Document,
|
||
/// Page content, form fields and every printable comment.
|
||
#[default]
|
||
DocumentAndMarkups,
|
||
/// Page content, form fields and stamps.
|
||
DocumentAndStamps,
|
||
/// Form fields only.
|
||
FormFieldsOnly,
|
||
}
|
||
|
||
#[derive(Clone, Debug, PartialEq)]
|
||
pub struct Settings {
|
||
/// Source pages (0-based) in print order; see [`select_pages`].
|
||
pub pages: Vec<usize>,
|
||
/// Paper (width, height) in points, portrait.
|
||
pub paper: (f64, f64),
|
||
pub orientation: Orientation,
|
||
pub layout: Layout,
|
||
pub content: Content,
|
||
/// For [`Layout::Label`], per source page: the area to keep (display space) and whether its
|
||
/// text runs bottom-to-top (so a quarter turn clockwise makes it upright). `None` or a
|
||
/// missing entry keeps the whole page.
|
||
pub crops: Vec<Option<Crop>>,
|
||
/// Print as image (Acrobat's Advanced Print Setup): every page rendered to a picture at this
|
||
/// resolution before it is placed on the paper. The engine does the rendering; `impose` is
|
||
/// then given the image-only document.
|
||
pub raster: Option<Raster>,
|
||
/// Print a selected area (Acrobat's "print selected area"): only this rectangle of the one
|
||
/// page in `pages`, in that page's display space (points, origin at the bottom-left of the
|
||
/// page as shown, y up), sized like a page of its own by a [`Layout::Size`] choice.
|
||
pub area: Option<[f64; 4]>,
|
||
}
|
||
|
||
/// How pages are rendered when printing as image.
|
||
#[derive(Clone, Debug, PartialEq)]
|
||
pub struct Raster {
|
||
/// Dots per inch, 72–600 (Acrobat offers 150, 300 and 600).
|
||
pub dpi: f64,
|
||
/// Render the pictures in grey, so the print-ready PDF itself is grayscale.
|
||
pub grayscale: bool,
|
||
/// App-managed colour: the pictures are converted into the printer's profile. `None` leaves
|
||
/// colour to the printer.
|
||
pub colour: Option<crate::colour::ManagedColour>,
|
||
}
|
||
|
||
impl Default for Raster {
|
||
fn default() -> Self {
|
||
Raster { dpi: 300.0, grayscale: false, colour: None }
|
||
}
|
||
}
|
||
|
||
/// Whether a print asked to be `grayscale` needs the printer's driver to print in grayscale.
|
||
/// Not when the pages go as grey pictures (`raster` with [`Raster::grayscale`]): they are grey
|
||
/// already, so a driver that keeps no grayscale setting (Microsoft Print to PDF, many inkjets)
|
||
/// still prints them instead of the job being refused (#5806).
|
||
pub fn driver_needs_grayscale(grayscale: bool, raster: Option<&Raster>) -> bool {
|
||
grayscale && !raster.is_some_and(|r| r.grayscale)
|
||
}
|
||
|
||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||
pub struct Crop {
|
||
pub rect: [f64; 4],
|
||
pub text_up: bool,
|
||
}
|
||
|
||
impl Default for Settings {
|
||
fn default() -> Self {
|
||
Settings {
|
||
pages: Vec::new(),
|
||
paper: PAPERS[0].1,
|
||
orientation: Orientation::Auto,
|
||
layout: Layout::default(),
|
||
content: Content::default(),
|
||
raster: None,
|
||
crops: Vec::new(),
|
||
area: None,
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Common paper sizes (points, portrait).
|
||
pub const PAPERS: [(&str, (f64, f64)); 23] = [
|
||
("US Letter", (612.0, 792.0)),
|
||
("US Legal", (612.0, 1008.0)),
|
||
("Tabloid", (792.0, 1224.0)),
|
||
("A3", (841.89, 1190.55)),
|
||
("A4", (595.28, 841.89)),
|
||
("A5", (419.53, 595.28)),
|
||
("A6", (297.64, 419.53)),
|
||
("ARCH A (9 x 12 in)", (648.0, 864.0)),
|
||
("ARCH B (12 x 18 in)", (864.0, 1296.0)),
|
||
("ARCH C (18 x 24 in)", (1296.0, 1728.0)),
|
||
("ARCH D (24 x 36 in)", (1728.0, 2592.0)),
|
||
("ARCH E1 (30 x 42 in)", (2160.0, 3024.0)),
|
||
("ARCH E (36 x 48 in)", (2592.0, 3456.0)),
|
||
("ANSI C (17 x 22 in)", (1224.0, 1584.0)),
|
||
("ANSI D (22 x 34 in)", (1584.0, 2448.0)),
|
||
("ANSI E (34 x 44 in)", (2448.0, 3168.0)),
|
||
("A2", (1190.55, 1683.78)),
|
||
("A1", (1683.78, 2383.94)),
|
||
("A0", (2383.94, 3370.39)),
|
||
("4 x 6 in label (101.6 x 152.4 mm)", (288.0, 432.0)),
|
||
("US #10 Envelope (4.125 x 9.5 in)", (297.0, 684.0)),
|
||
("Folio (8.5 x 13 in)", (612.0, 936.0)),
|
||
("F4 (210 x 330 mm)", (595.28, 935.43)),
|
||
];
|
||
|
||
/// `PAPERS` index of the 4 × 6 in shipping label.
|
||
pub const LABEL_PAPER: usize = 19;
|
||
|
||
/// The CUPS media name for `PAPERS[index]`, so the spooler feeds the sheet the PDF was laid out
|
||
/// on rather than the queue's default; `None` only past the end of `PAPERS`. The common sizes
|
||
/// use their PPD keywords (`w288h432` is the usual 4×6 label, `Env10` the US #10 envelope).
|
||
/// The rest use PWG 5101.1 names (ARCH E1 is its `wide-format` size), spelled exactly as the
|
||
/// spec and the IANA registry have them: a driverless printer may take only the names it
|
||
/// advertises. They carry their dimensions, so CUPS feeds the PPD size of those dimensions
|
||
/// whatever the driver calls it (`ARCHD`, `AnsiD`) (#3752).
|
||
pub fn cups_media(index: usize) -> Option<&'static str> {
|
||
Some(match PAPERS.get(index)?.0 {
|
||
"US Letter" => "Letter",
|
||
"US Legal" => "Legal",
|
||
"Tabloid" => "Tabloid",
|
||
"A3" => "A3",
|
||
"A4" => "A4",
|
||
"A5" => "A5",
|
||
"A6" => "iso_a6_105x148mm",
|
||
"ARCH A (9 x 12 in)" => "na_arch-a_9x12in",
|
||
"ARCH B (12 x 18 in)" => "na_arch-b_12x18in",
|
||
"ARCH C (18 x 24 in)" => "na_arch-c_18x24in",
|
||
"ARCH D (24 x 36 in)" => "na_arch-d_24x36in",
|
||
"ARCH E1 (30 x 42 in)" => "na_wide-format_30x42in",
|
||
"ARCH E (36 x 48 in)" => "na_arch-e_36x48in",
|
||
"ANSI C (17 x 22 in)" => "na_c_17x22in",
|
||
"ANSI D (22 x 34 in)" => "na_d_22x34in",
|
||
"ANSI E (34 x 44 in)" => "na_e_34x44in",
|
||
"A2" => "iso_a2_420x594mm",
|
||
"A1" => "iso_a1_594x841mm",
|
||
"A0" => "iso_a0_841x1189mm",
|
||
"US #10 Envelope (4.125 x 9.5 in)" => "Env10",
|
||
"Folio (8.5 x 13 in)" => "na_foolscap_8.5x13in",
|
||
"F4 (210 x 330 mm)" => "om_folio_210x330mm",
|
||
_ if index == LABEL_PAPER => "w288h432",
|
||
// A size added to PAPERS without a name here: its tests fail, and meanwhile the queue's
|
||
// paper stands, as it did for every size before #1583.
|
||
_ => return None,
|
||
})
|
||
}
|
||
|
||
/// The job option that feeds `PAPERS[index]` (`media`, see [`cups_media`]), for the Print
|
||
/// dialog's jobs and `doc_print`'s alike.
|
||
pub fn media_option(index: usize) -> Option<(String, String)> {
|
||
cups_media(index).map(|m| ("media".to_string(), m.to_string()))
|
||
}
|
||
|
||
/// Format custom dimensions in points as a CUPS custom media string (e.g. `Custom.300x500`).
|
||
pub fn custom_media(size: (f64, f64)) -> String {
|
||
let format_dim = |d: f64| {
|
||
if (d.round() - d).abs() < 1e-4 {
|
||
format!("{:.0}", d.round())
|
||
} else {
|
||
let s = format!("{d:.2}");
|
||
s.trim_end_matches('0').trim_end_matches('.').to_string()
|
||
}
|
||
};
|
||
format!("Custom.{}x{}", format_dim(size.0), format_dim(size.1))
|
||
}
|
||
|
||
/// The CUPS media name for a paper by its name and dimensions.
|
||
///
|
||
/// Matches standard [`PAPERS`] presets by size first, then a clean driver-reported keyword,
|
||
/// falling back to custom media dimensions (`Custom.<width>x<height>`).
|
||
pub fn paper_media(name: Option<&str>, size: (f64, f64)) -> String {
|
||
if let Some(index) = matching_paper(size)
|
||
&& let Some(media) = cups_media(index)
|
||
{
|
||
return media.to_string();
|
||
}
|
||
if let Some(n) = name {
|
||
let is_dim = n.split_once(['x', 'X', '*']).is_some_and(|(w, h)| {
|
||
let parse_dim = |d: &str| {
|
||
let d = d.trim().trim_start_matches("Custom.").trim_start_matches("custom.").trim_end_matches("points").trim_end_matches("pt").trim();
|
||
d.parse::<f64>().is_ok()
|
||
};
|
||
parse_dim(w) && parse_dim(h)
|
||
});
|
||
let is_clean = !n.is_empty()
|
||
&& n.len() <= 64
|
||
&& !n.contains(' ')
|
||
&& !is_dim
|
||
&& n.chars().all(|c| c.is_ascii_graphic() && !matches!(c, '/' | ':' | '=' | '"' | '\'' | '\\' | '{'));
|
||
if is_clean {
|
||
return n.to_string();
|
||
}
|
||
}
|
||
custom_media(size)
|
||
}
|
||
|
||
/// The media job option (`("media", <name>)`) for a paper by its optional name and dimensions.
|
||
pub fn paper_media_option(name: Option<&str>, size: (f64, f64)) -> (String, String) {
|
||
("media".to_string(), paper_media(name, size))
|
||
}
|