From 18c4d3146ac40e6b04829fe5c2f462e19d5e7cd3 Mon Sep 17 00:00:00 2001 From: Justin Capella <4584655+b1tninja@users.noreply.github.com> Date: Wed, 14 Oct 2026 18:03:09 -0700 Subject: [PATCH] Accept a one-sided request on a queue whose driver has no duplex feature The Windows path refused any explicit duplex request whose validated PrintTicket lacked the duplex feature. The Print dialog asks for one-sided by default, so every queue without a duplex unit refused with default settings, although one-sided is all such a queue can do and nothing was dropped. A one-sided request is now satisfied when the driver's PrintCapabilities offer no duplex feature at all. Two-sided requests without the feature, a driver that offers the feature but returns a ticket without the setting, and capabilities that cannot be read are still refused. Copies, collation, colour, paper, orientation and scaling are checked as before. Co-Authored-By: Claude Sonnet 5.5 --- crates/print/src/tests/windows_native.rs | 9 ++- crates/print/src/ticket.rs | 95 +++++++++++++++++++++--- crates/print/src/windows.rs | 15 +++- 3 files changed, 103 insertions(+), 16 deletions(-) diff --git a/crates/print/src/tests/windows_native.rs b/crates/print/src/tests/windows_native.rs index 157fe68b9..efb6310f6 100644 --- a/crates/print/src/tests/windows_native.rs +++ b/crates/print/src/tests/windows_native.rs @@ -429,6 +429,9 @@ fn windows_jobs_reach_the_spooler() { } else { None }; + // The Print dialog's default, an explicit one-sided request, on a queue with no duplex unit. + let simplex_only = listed.as_ref().is_some_and(|p| p.two_sided == Some(false)); + let one_sided = simplex_only.then(|| spool::submit(&mixed, &Job { duplex: Some(Duplex::Off), title: "simplex".into(), ..job.clone() })); let tabloid = test_pdf(&[TABLOID, LETTER], false); let shrink = Job { printer: Some(name.into()), scaling: Scaling::ShrinkToFit, title: "shrink".into(), ..Job::default() }; let shrunk = spool::submit(&tabloid, &shrink); @@ -451,12 +454,16 @@ fn windows_jobs_reach_the_spooler() { if let Some(refused) = duplex_refused { assert!(matches!(refused, Err(Error::Spool(ref m)) if m.contains("simplex/duplex") && m.contains("nothing was sent")), "{refused:?}"); } + if let Some(one_sided) = one_sided { + assert_eq!(one_sided.as_deref(), Ok("spooled 3 sheet(s)"), "one-sided is what a simplex-only queue does: {one_sided:?}"); + } assert_eq!(*processed.borrow(), [(0, 3), (1, 3), (2, 3), (3, 3)]); assert!(duplex_progress.is_empty(), "driver refusal before submission has no progress"); assert_eq!(shrunk.as_deref(), Ok("spooled 2 sheet(s)"), "{shrunk:?}"); assert!(matches!(exact_refused, Err(Error::Spool(ref m)) if m.contains("one paper size")), "{exact_refused:?}"); assert!(refused_progress.is_empty(), "rejection before a job starts has no progress: {refused_progress:?}"); - assert_eq!(queued.as_deref().map(str::trim), Ok("mixed.pdf 3|shrink.pdf 2"), "one job each, all sheets, in the paused queue: {queued:?}"); + let expected = if simplex_only { "mixed.pdf 3|simplex.pdf 3|shrink.pdf 2" } else { "mixed.pdf 3|shrink.pdf 2" }; + assert_eq!(queued.as_deref().map(str::trim), Ok(expected), "one job each, all sheets, in the paused queue: {queued:?}"); assert!(cleaned.is_ok(), "the test printer is removed again: {cleaned:?}"); // Asking for a printer that isn't there is an error, never a job on another one. let missing = spool::submit(&mixed, &Job { printer: Some(name.into()), ..Job::default() }); diff --git a/crates/print/src/ticket.rs b/crates/print/src/ticket.rs index 5f8c914bd..852368be2 100644 --- a/crates/print/src/ticket.rs +++ b/crates/print/src/ticket.rs @@ -174,7 +174,13 @@ pub(crate) fn verify_ticket(xml: &str, paper: Option<(u32, u32)>, landscape: boo /// Confirm explicit job options survived driver validation. Collation only changes output /// when both the document and copy count exceed one. Without a duplex request the queue's /// default applies; without forced grayscale the driver's normal color policy applies. -pub(crate) fn verify_job_options(xml: &str, job: &Job, pages: usize) -> Result<(), Error> { +/// +/// `duplex_offered` is what the driver's PrintCapabilities say about the duplex feature +/// ([`offers_duplex`]). A one-sided request to a driver that offers no duplex feature at all is +/// satisfied by construction (the queue can only print one side, so nothing was dropped), but a +/// two-sided request is not, and neither is a one-sided one to a driver that offers the feature +/// and returned a ticket without it. `None` (capabilities unreadable) stays strict. +pub(crate) fn verify_job_options(xml: &str, job: &Job, pages: usize, duplex_offered: Option) -> Result<(), Error> { let document = roxmltree::Document::parse(xml).map_err(spool_error)?; let root = document.root_element(); let option = |name: &str| { @@ -196,7 +202,8 @@ pub(crate) fn verify_job_options(xml: &str, job: &Job, pages: usize) -> Result<( Duplex::LongEdge => "TwoSidedLongEdge", Duplex::ShortEdge => "TwoSidedShortEdge", }; - if !option("JobDuplexAllDocumentsContiguously").is_some_and(|n| named(n, wanted)) { + let one_sided_only = duplex == Duplex::Off && duplex_offered == Some(false); + if !one_sided_only && !option("JobDuplexAllDocumentsContiguously").is_some_and(|n| named(n, wanted)) { return Err(refused("simplex/duplex setting")); } } @@ -206,6 +213,21 @@ pub(crate) fn verify_job_options(xml: &str, job: &Job, pages: usize) -> Result<( Ok(()) } +/// Whether PrintCapabilities offer any option for the duplex feature. `None` when the document +/// cannot be read as capabilities, so callers treat the answer as unknown. +pub(crate) fn offers_duplex(capabilities_xml: &str) -> Option { + let document = roxmltree::Document::parse(capabilities_xml).ok()?; + let root = document.root_element(); + if !root.has_tag_name((NS_PSF, "PrintCapabilities")) { + return None; + } + Some( + root.children() + .filter(|n| n.has_tag_name((NS_PSF, "Feature")) && named(*n, "JobDuplexAllDocumentsContiguously")) + .any(|f| f.children().any(|n| n.has_tag_name((NS_PSF, "Option")))), + ) +} + /// The widest unprintable margin of the paper, in micrometres, from PrintCapabilities computed /// for the job's ticket (`PageImageableSize`). The largest of the four, so content centred inside /// it is clear of every edge whichever way the driver measures. `None` when the driver doesn't say. @@ -301,31 +323,80 @@ mod tests { fn a_driver_that_drops_explicit_job_options_is_refused() { let job = Job { copies: 2, duplex: Some(Duplex::LongEdge), grayscale: true, ..Job::default() }; let xml = ticket_xml(&job, false); - assert!(verify_job_options(&xml, &job, 3).is_ok()); + assert!(verify_job_options(&xml, &job, 3, None).is_ok()); for (from, to) in [(">2<", ">1<"), ("psk:Collated", "psk:Uncollated"), ("psk:TwoSidedLongEdge", "psk:OneSided"), ("psk:Monochrome", "psk:Color")] { - assert!(verify_job_options(&xml.replace(from, to), &job, 3).is_err(), "{from}"); + assert!(verify_job_options(&xml.replace(from, to), &job, 3, None).is_err(), "{from}"); } - assert!(verify_job_options(&xml.replace("psk:Collated", "vendor:Collated"), &job, 3).is_err(), "namespace matters"); - assert!(verify_job_options(&xml.replace("psk:Monochrome", "psk:Grayscale"), &job, 3).is_ok()); + assert!(verify_job_options(&xml.replace("psk:Collated", "vendor:Collated"), &job, 3, None).is_err(), "namespace matters"); + assert!(verify_job_options(&xml.replace("psk:Monochrome", "psk:Grayscale"), &job, 3, None).is_ok()); for feature in ["DocumentCollate", "JobDuplexAllDocumentsContiguously", "PageOutputColor"] { let start = xml.find(&format!("").unwrap() + "".len(); let mut dropped = xml.clone(); dropped.replace_range(start..end, ""); - assert!(verify_job_options(&dropped, &job, 3).is_err(), "{feature}"); + assert!(verify_job_options(&dropped, &job, 3, None).is_err(), "{feature}"); if feature == "DocumentCollate" { - assert!(verify_job_options(&dropped, &job, 1).is_ok(), "one page has no collating order"); + assert!(verify_job_options(&dropped, &job, 1, None).is_ok(), "one page has no collating order"); } } let default = Job::default(); let xml = ticket_xml(&default, false); - assert!(verify_job_options(&xml, &default, 3).is_ok()); + assert!(verify_job_options(&xml, &default, 3, None).is_ok()); let simplex = Job { duplex: Some(Duplex::Off), ..Job::default() }; - assert!(verify_job_options(&ticket_xml(&simplex, false), &simplex, 3).is_ok()); - assert!(verify_job_options(&xml, &simplex, 3).is_err(), "explicit simplex must be confirmed"); - assert!(verify_job_options(" String { + let feature = if duplex { + r#""# + } else { + "" + }; + format!( + r#" +{feature} +"# + ) + } + + #[test] + fn a_driver_without_a_duplex_feature_prints_one_side_and_is_not_refused_for_it() { + let offered = |duplex| offers_duplex(&capabilities_xml(duplex)); + assert_eq!(offered(true), Some(true)); + assert_eq!(offered(false), Some(false)); + assert_eq!(offers_duplex(""), None, "another namespace is not capabilities"); + assert_eq!(offers_duplex(""# + ); + assert_eq!(offers_duplex(&empty), Some(false), "a feature with no option offers nothing"); + + let one_sided = Job { duplex: Some(Duplex::Off), ..Job::default() }; + let two_sided = Job { duplex: Some(Duplex::LongEdge), ..Job::default() }; + let kept = ticket_xml(&one_sided, false); + // The ticket a driver with no duplex feature returns: the setting is gone. + let none_kept = ticket_xml(&Job::default(), false); + + // The Print dialog's defaults (duplex Off) on a simplex-only queue. + assert!(verify_job_options(&none_kept, &one_sided, 3, offered(false)).is_ok()); + // Offered and kept. + assert!(verify_job_options(&kept, &one_sided, 3, offered(true)).is_ok()); + // Offered but the ticket lost it: the driver dropped the choice. + assert!(verify_job_options(&none_kept, &one_sided, 3, offered(true)).is_err()); + // Two-sided never rests on the missing feature. + assert!(verify_job_options(&none_kept, &two_sided, 3, offered(false)).is_err()); + // Unknown capabilities stay strict. + assert!(verify_job_options(&none_kept, &one_sided, 3, None).is_err()); + // The other options are still checked on a simplex-only queue. + let copies = Job { copies: 2, duplex: Some(Duplex::Off), ..Job::default() }; + assert!(verify_job_options(&none_kept, &copies, 3, offered(false)).is_err(), "copy count"); + let gray = Job { grayscale: true, duplex: Some(Duplex::Off), ..Job::default() }; + assert!(verify_job_options(&none_kept, &gray, 3, offered(false)).is_err(), "grayscale"); } #[test] diff --git a/crates/print/src/windows.rs b/crates/print/src/windows.rs index 45a23aa7b..8802e107d 100644 --- a/crates/print/src/windows.rs +++ b/crates/print/src/windows.rs @@ -45,6 +45,12 @@ fn two_sided(device: &PrinterDevice) -> Option { Some(capabilities.options_for_feature(duplex).any(|o| o.name.as_ref().is_some_and(|n| n.local_name.starts_with("TwoSided")))) } +/// What the driver's PrintCapabilities say about the duplex feature; `None` when they can't be read. +fn offered_duplex(device: &PrinterDevice) -> Option { + let xml = PrintCapabilities::fetch_xml_for_ticket(device, None).ok()?; + ticket::offers_duplex(std::str::from_utf8(&xml).ok()?) +} + /// The queue the job goes to: the one asked for, else the default. Never another one: a job /// for a queue that has gone must not come out of a different printer. fn select_device(devices: Vec, requested: Option<&str>, default: Option<&str>) -> Result { @@ -144,7 +150,10 @@ pub(crate) fn submit_with_progress(pdf: &[u8], job: &Job, progress: &mut dyn FnM let result = crate::progress::run(progress, move |sender| { let (ticket, paper) = driver_ticket(&device, job, first, landscape)?; let xml = std::str::from_utf8(ticket.get_xml()).map_err(spool_error)?; - ticket::verify_job_options(xml, job, count)?; + // Only a one-sided request can be satisfied by a driver with no duplex feature, so the + // capabilities are read for that case alone; unreadable stays unknown (strict). + let duplex_offered = if job.duplex == Some(crate::Duplex::Off) { offered_duplex(&device) } else { None }; + ticket::verify_job_options(xml, job, count, duplex_offered)?; let (fit, paper) = match job.scaling { Scaling::Exact => (Fit::Exact, first), Scaling::ShrinkToFit => { @@ -199,7 +208,7 @@ mod tests { let device = select_device(PrinterDevice::all().unwrap(), Some(&name), None).unwrap(); let job = Job { copies: 2, ..Job::default() }; let (ticket, paper) = driver_ticket(&device, &job, (612.0, 792.0), false).unwrap(); - ticket::verify_job_options(std::str::from_utf8(ticket.get_xml()).unwrap(), &job, 1).unwrap(); + ticket::verify_job_options(std::str::from_utf8(ticket.get_xml()).unwrap(), &job, 1, None).unwrap(); assert_eq!(paper, (215_900, 279_400)); eprintln!("unprintable margin: {} pt", imageable_margin(&device, &ticket)); if let Ok(path) = std::env::var("CRAFT_PRINT_TEST_TICKET_OUTPUT") { @@ -212,7 +221,7 @@ mod tests { ("DocumentCollate", "Collated", Job { copies: 2, ..Job::default() }), ] { let (ticket, _) = driver_ticket(&device, &requested, (612.0, 792.0), false).unwrap(); - let retained = ticket::verify_job_options(std::str::from_utf8(ticket.get_xml()).unwrap(), &requested, 3); + let retained = ticket::verify_job_options(std::str::from_utf8(ticket.get_xml()).unwrap(), &requested, 3, None); eprintln!("{feature}={wanted}: {retained:?}"); let offered = capabilities .options_for_feature(OwnedName::qualified(feature, NS_PSK, Some("psk")))