PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge current PhotoCraft and enforce print-disable guard on both transports
skylar taylor-barrickf7a3262531fe
diff --cc crates/engine/src/print_cmds.rsindex 419ffd55a,6a5d71ca0..0235d3af1--- a/crates/engine/src/print_cmds.rs+++ b/crates/engine/src/print_cmds.rs@@@ -390,20 -388,6 +390,26 @@@ fn render_and_spool(doc: &Document, p:let send = p.get("send").and_then(Value::as_bool).unwrap_or(output.is_none());let copies = crate::commands::int(p, "copies").unwrap_or(1).clamp(1, 999) as u32;let dry = p.get("dryRun").and_then(Value::as_bool).unwrap_or(false);- // Windows has no `lp`: print through the driver (GDI, ICM off) instead of spooling a PDF.++ // Apply the process-wide test/fuzz guard before either native transport can reach a driver.++ if send && !dry && printing_disabled() {++ return Err(other(++ "printing is disabled in this process (photocraft_engine::print_cmds::disable_printing or PHOTOCRAFT_NO_PRINT); use dryRun: true",++ ));++ }++ // Shared RGB transport handles compatible Windows pages; PDF-only content retains its route.+ // Sending follows the shared command contract; fuzz tests explicitly request dryRun.+ // The worker returns its result before repeat settings and hooks are applied once.+ let handling = p.get("colorHandling").and_then(Value::as_str).unwrap_or("printerManages");+ if cfg!(windows) && send && !dry && output.is_none() && native_rgb_compatible(&page, handling) {+ let printed = print_windows(&page, p, &doc.name, copies)?;+ return Ok(json!({+ "pdf": Value::Null, "paper": [pw, ph], "imageRect": [x, y, w, h], "emulsionDown": emulsion_down, "scale": scale * 100.0,+ "copies": copies, "command": Value::Null, "sent": true, "spooler": printed, "color": color,+ }));+ }+ if cfg!(windows) && send && !dry && output.is_none() && printer_settings(p)?.is_some() {+ return Err(other("the selected color profile or page text requires PDF printing. Save as PDF, or clear Print Settings to use PDF printing."));+ }let pdf = print_pdf(&page);let job = craft_print::Job {printer: p.get("printer").and_then(Value::as_str).filter(|v| !v.is_empty()).map(str::to_string),@@@ -454,101 -436,20 +460,115 @@@ fn remember_print(s: &mut Session, p: &s.file_menu.last_print = Some(remembered);}+/// The `printer` parameter, or the system's default printer.+fn printer_name(p: &Value) -> Result<String> {+ if let Some(name) = p.get("printer").and_then(Value::as_str).filter(|v| !v.trim().is_empty()) {+ return Ok(name.to_string());+ }+ craft_print::rgb::default_printer().map_err(other)?.ok_or_else(|| other("no printer chosen and no default printer is set"))+}++/// The driver settings chosen with Print Settings… (`printerSettings`, hex), when any.+fn printer_settings(p: &Value) -> Result<Option<craft_print::rgb::DevMode>> {+ match p.get("printerSettings").and_then(Value::as_str).filter(|v| !v.is_empty()) {+ Some(hex) => craft_print::rgb::DevMode::from_hex(hex).map(Some).map_err(|e| bad("file.print", e.to_string())),+ None => Ok(None),+ }+}++/// The page's image as 8-bit RGB rows (gray expanded), mirrored for Emulsion Down.+pub fn page_rgb(page: &PrintPage) -> Result<Vec<u8>> {+ let img = &page.image;+ let (w, h) = (img.width as usize, img.height as usize);+ let mut rgb = match img.channels {+ 3 => img.data.clone(),+ 1 => img.data.iter().flat_map(|g| [*g, *g, *g]).collect(),+ _ => return Err(other("this printer profile is CMYK; Windows printers take RGB, so choose the printer's own (RGB) profile")),+ };+ let row = w.checked_mul(3).ok_or_else(|| other("print image is too large"))?;+ if row == 0 || row.checked_mul(h) != Some(rgb.len()) {+ return Err(other("nothing to print"));+ }+ if page.emulsion_down {+ for row in rgb.chunks_exact_mut(row) {+ row.as_chunks_mut::<3>().0.reverse();+ }+ }+ Ok(rgb)+}++/// The image box with its origin at the sheet's top-left (GDI's), mirrored for Emulsion Down.+pub fn page_rect_top_left(page: &PrintPage) -> (f64, f64, f64, f64) {+ let (pw, ph) = page.paper;+ let (x, y, w, h) = page.rect;+ let x = if page.emulsion_down { pw - x - w } else { x };+ (x, ph - y - h, w, h)+}++/// Prints `page` on a Windows printer. Corner/centre crop and registration marks are drawn with+/// GDI vector calls, matching the PDF path's geometry; description and label text are still+/// PDF-only (no font/text layout in the GDI path yet).+fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: u32) -> Result<Value> {+ let printer = printer_name(p)?;+ let settings = printer_settings(p)?;+ let device_page = craft_print::rgb::page(&printer, settings.as_ref()).map_err(other)?;+ if (device_page.paper.0 - page.paper.0).abs() > 2.0 || (device_page.paper.1 - page.paper.1).abs() > 2.0 {+ return Err(other("the driver paper or orientation differs from the print layout; choose the matching paper in Print Settings before printing"));+ }+ let rgb = page_rgb(page)?;+ let image = craft_print::rgb::RgbImage { width: page.image.width, height: page.image.height, data: &rgb };+ let rect = page_rect_top_left(page);+ let primitives = windows_marks::primitives(page.marks, rect);+ let color_policy = match p.get("colorHandling").and_then(Value::as_str) {+ Some("photocraftManages" | "photoshopManages") => craft_print::rgb::ColorPolicy::ApplicationManaged,+ Some("noColorManagement") => craft_print::rgb::ColorPolicy::Unmanaged,+ _ => craft_print::rgb::ColorPolicy::DriverManaged,+ };+ let job = craft_print::rgb::Job {+ printer: &printer,+ settings: settings.as_ref(),+ title,+ output_file: None,+ copies: copies.clamp(1, 999),+ color_policy,+ primitives: &primitives,+ };+ let done = craft_print::rgb::print(&job, &image, rect).map_err(other)?;+ Ok(+ json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3]}),+ )+}++fn printers(_: &mut Session, _: &Value) -> Result<Value> {+ match craft_print::rgb::printers() {+ Ok(list) => Ok(json!({"supported": true, "printers": list.iter().map(|p| json!({"name": p.name, "default": p.default})).collect::<Vec<_>>()})),+ Err(craft_print::rgb::PrintError::Unsupported) => Ok(json!({"supported": false, "printers": []})),+ Err(e) => Err(other(e)),+ }+}++fn printer_page(_: &mut Session, p: &Value) -> Result<Value> {+ let printer = printer_name(p)?;+ let pg = craft_print::rgb::page(&printer, printer_settings(p)?.as_ref()).map_err(other)?;+ Ok(+ json!({"printer": printer, "paper": [pg.paper.0, pg.paper.1], "printable": [pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3], "dpi": [pg.dpi.0, pg.dpi.1]}),+ )+}++ /// Set once by test harnesses (the panic hunt) so no command in the process can reach a printer.+ static PRINTING_DISABLED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);++ /// Refuse to spool anything from this process from now on (tests, fuzzing). The+ /// `PHOTOCRAFT_NO_PRINT` environment variable does the same for a whole shell (CI).+ pub fn disable_printing() {+ PRINTING_DISABLED.store(true, std::sync::atomic::Ordering::Relaxed);+ }++ /// Whether spooling is blocked by [`disable_printing`] or `PHOTOCRAFT_NO_PRINT`.+ pub fn printing_disabled() -> bool {+ PRINTING_DISABLED.load(std::sync::atomic::Ordering::Relaxed) || std::env::var_os("PHOTOCRAFT_NO_PRINT").is_some_and(|v| !v.is_empty() && v != "0")+ }+fn print(s: &mut Session, p: &Value) -> Result<Value> {do_print(s, p, "file.print")}