From 5886c70c8da9856d14caa9cabc9a5de139581f99 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 05:52:44 -0800 Subject: [PATCH] Print: maxPages turns a job away before raster effects are flattened file.print with maxPages flattened every raster effect in the document before it counted the pages, so a job it then turned away still paid for rendering each effect. The page count from the artboards now runs first, on the artboards alone, and raster effects are flattened only for a job that fits. Follows up on #9087. --- crates/engine/src/cmd/print.rs | 40 ++++++++++++++----------- crates/engine/src/cmd/rasterfx.rs | 8 +++++ crates/engine/src/tests/script_print.rs | 33 ++++++++++++++++++++ docs/mcp.md | 3 +- 4 files changed, 65 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/cmd/print.rs b/crates/engine/src/cmd/print.rs index 5d37ec4e9c..77e6d03395 100644 --- a/crates/engine/src/cmd/print.rs +++ b/crates/engine/src/cmd/print.rs @@ -240,15 +240,7 @@ fn print(s: &mut Session, p: &Value) -> Result { // The artboards' background colors print behind the art. let filled = super::fileio::with_artboard_fills(&full, p, 0..full.artboards.len()).map_err(|e| bad(PRINT, e))?; let doc = &*filled; - // Raster effects print as images at the document's raster effects resolution. - let flat = super::rasterfx::flatten_raster_effects(doc); - let doc = flat.as_ref().unwrap_or(doc); let bitmap = set.advanced.print_as_bitmap && set.output.mode == vectorcraft_pdf::OutputMode::Composite; - // The flattener expands pattern fills whose tile images need flattening, in the layers that print. - let tile_images = postscript - && !bitmap - && super::printadvanced::flattener(PRINT, &set, &s.prefs.flattener_presets)? - .is_some_and(|o| has_transparent_tile_images(&vectorcraft_pdf::printed_document(doc, &set), &o)); let level = match p.get("level").filter(|v| postscript && !v.is_null()) { Some(v) => { let t = super::fileio::dxf::text(v); @@ -256,20 +248,32 @@ fn print(s: &mut Session, p: &Value) -> Result { } None => Level::default(), }; - // A job past `maxPages` is turned away before Print as Bitmap or a flattener preset prepares it, - // once the options are checked, when its pages follow from the artboards alone (composite, no - // `skipBlank`, artboards not ignored); the pages are counted again on the prepared art. - super::printadvanced::flattener(PRINT, &set, &s.prefs.flattener_presets)?; + // A job past `maxPages` is turned away before its raster effects are flattened and before Print + // as Bitmap or a flattener preset prepares it, once the options are checked, when its pages + // follow from the artboards alone (composite, no `skipBlank`, artboards not ignored); the pages + // are counted again on the prepared art. + let flattener = super::printadvanced::flattener(PRINT, &set, &s.prefs.flattener_presets)?; let by_artboards = !set.skip_blank && !matches!(set.artboards, vectorcraft_pdf::PrintArtboards::Ignore) && set.output.mode == vectorcraft_pdf::OutputMode::Composite; - if max_pages.is_some() - && by_artboards - && let Ok(pv) = vectorcraft_pdf::preview(doc, &set) - && too_many(pv.pages) - { - return Ok(json!({ "pages": pv.pages, "format": if postscript { "postscript" } else { "pdf" } })); + // Those pages are counted on the artboards with no art, which is not prepared for the count. + if max_pages.is_some() && by_artboards { + let mut bare = Document::new(0.0, 0.0); + bare.artboards = doc.artboards.clone(); + bare.setup.bleed = doc.setup.bleed; + bare.color_mode = doc.color_mode; + bare.layers.clear(); + let pv = vectorcraft_pdf::preview(&bare, &set).map_err(|e| pdf_error(PRINT, e))?; + if too_many(pv.pages) { + return Ok(json!({ "pages": pv.pages, "format": if postscript { "postscript" } else { "pdf" } })); + } } + // Raster effects print as images at the document's raster effects resolution. + let flat = super::rasterfx::flatten_raster_effects(doc); + let doc = flat.as_ref().unwrap_or(doc); + // The flattener expands pattern fills whose tile images need flattening, in the layers that print. + let tile_images = + postscript && !bitmap && flattener.is_some_and(|o| has_transparent_tile_images(&vectorcraft_pdf::printed_document(doc, &set), &o)); // Print as Bitmap and the flattener preset (Advanced) render and flatten it first. let prepared = super::printadvanced::prepare(PRINT, doc, &set, &s.prefs.flattener_presets, s.prefs.black_output == "rich")?; let path = str_param(p, "path"); diff --git a/crates/engine/src/cmd/rasterfx.rs b/crates/engine/src/cmd/rasterfx.rs index f035ff55f5..97dd890457 100644 --- a/crates/engine/src/cmd/rasterfx.rs +++ b/crates/engine/src/cmd/rasterfx.rs @@ -817,9 +817,17 @@ fn staged_all(out: &mut Document, nodes: &[Arc], report: &mut FlattenRepor /// type, images, symbol instances, live objects) and on single fills and strokes, in the layers, /// opacity masks, symbol definitions and pattern tiles. pub fn flatten_raster_effects(doc: &Document) -> Option { + #[cfg(test)] + FLATTENED.with(|n| n.set(n.get() + 1)); flatten(doc, &mut FlattenReport::new(Which::All, doc)) } +#[cfg(test)] +thread_local! { + /// How many times this thread called [`flatten_raster_effects`]. + pub(crate) static FLATTENED: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + /// A copy of `doc` in which the objects with Photoshop-style raster effects (Radial Blur, Unsharp /// Mask…) or Rasterize are images as [`flatten_raster_effects`] makes them (SVG export: its /// filters draw the other raster effects live), or `None` when there are none. diff --git a/crates/engine/src/tests/script_print.rs b/crates/engine/src/tests/script_print.rs index 1c15b3e9f0..06ca34fa45 100644 --- a/crates/engine/src/tests/script_print.rs +++ b/crates/engine/src/tests/script_print.rs @@ -389,6 +389,39 @@ fn file_print_max_pages_composes_only_what_fits() { let _ = std::fs::remove_dir_all(base); } +/// A job past `maxPages` is turned away before its raster effects are flattened. +#[test] +fn file_print_max_pages_turns_a_job_away_before_raster_effects_are_flattened() { + use crate::cmd::rasterfx::FLATTENED; + let base = folder("max-pages-raster"); + let mut s = session(&base); + s.execute("effect.apply", &json!({"effect": "stylize.dropShadow"})).unwrap(); + let before = FLATTENED.with(std::cell::Cell::get); + let r = s.execute("file.print", &json!({"maxPages": 5, "settings": {"copies": 3}})).unwrap(); + assert_eq!((&r["pages"], r.get("dataBase64").is_none()), (&json!(6), true), "{r}"); + assert_eq!(FLATTENED.with(std::cell::Cell::get), before, "nothing was flattened"); + let r = s.execute("file.print", &json!({"maxPages": 6, "settings": {"copies": 3}})).unwrap(); + assert!(r["dataBase64"].is_string(), "{r}"); + assert!(FLATTENED.with(std::cell::Cell::get) > before, "a job that fits is flattened"); + let _ = std::fs::remove_dir_all(base); +} + +/// A job past the page limit throws before its raster effects are flattened. +#[test] +fn file_print_max_pages_returns_a_page_cap_error_before_raster_effects_are_flattened() { + use crate::cmd::rasterfx::FLATTENED; + let base = folder("max-pages-raster-cap"); + let mut s = session(&base); + s.execute("file.new", &json!({"width": 300, "height": 200, "artboards": 3})).unwrap(); + s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 100, "height": 50})).unwrap(); + s.execute("effect.apply", &json!({"effect": "stylize.dropShadow"})).unwrap(); + let before = FLATTENED.with(std::cell::Cell::get); + let e = s.execute("file.print", &json!({"maxPages": 5, "settings": {"copies": 700}})).unwrap_err().to_string(); + assert!(e.contains("more than 2000 pages"), "{e}"); + assert_eq!(FLATTENED.with(std::cell::Cell::get), before, "nothing was flattened"); + let _ = std::fs::remove_dir_all(base); +} + /// Bad values throw when they are assigned, and nothing prints. #[test] fn print_options_refuse_bad_values() { diff --git a/docs/mcp.md b/docs/mcp.md index 27ec011196..25e0a5d587 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -13221,7 +13221,8 @@ and, with copies per sheet, each copy's artboard on the page (`copies`), plus `t `inks` of a separation and `warnings`. `file.print {settings?, path?}` writes the job as a print-ready PDF (one page per sheet, tile, ink and copy); headless it answers `dataBase64`. With `maxPages`, the job is composed only when it prints at most that many pages, copies counted; with more, `file.print` composes and writes nothing and answers -`{pages, format}`. In the app, `file.print` with no params opens the Print +`{pages, format}`. A composite job without `skipBlank` that prints its artboards is counted on the artboards before +its raster effects are rendered. In the app, `file.print` with no params opens the Print dialog, and with params it prints through the system (`printer`: a name from `print.printers`, default the system's default printer) or, with `toFile`, a `path` or no printing available, saves the PDF.