diff --git a/crates/engine/src/script/dom/print.rs b/crates/engine/src/script/dom/print.rs index 5ebfe77c27..bc3f3cefd5 100644 --- a/crates/engine/src/script/dom/print.rs +++ b/crates/engine/src/script/dom/print.rs @@ -49,7 +49,7 @@ //! `width` or `height` is assigned above 0. An assigned name that is empty or only spaces, //! or that holds `/`, `\` or a control character (also at either end), throws too. Each checked //! name goes to `file.print` without the spaces around it. `printerName` and `PPDName` take only the empty -//! string. +//! string. Paper `width` and `height` take 0 (not assigned) or 72 to 14,400 pt. //! - A run makes at most [`MAX_OPTION_OBJECTS`] option objects. //! - `app.printerList` is not available yet, and `app.PPDFileList` is empty: VectorCraft prints //! without PPD files. @@ -139,6 +139,8 @@ enum Kind { Number(f64, f64), /// A whole number from the first to the second. Whole(u64, u64), + /// A paper side in points: 0 (not assigned) or a custom size from 72 to 14,400. + PaperSize, Text, /// A value of the enumeration (its name and members); only the members in the last list are /// available. @@ -155,6 +157,12 @@ impl Kind { Kind::Bool => typed("boolean"), Kind::Number(min, max) => MemberInfo { min: Some(min), max: Some(max), ..typed("number") }, Kind::Whole(min, max) => MemberInfo { min: Some(min as f64), max: Some(max as f64), ..typed("number") }, + Kind::PaperSize => MemberInfo { + min: Some(0.0), + max: Some(MAX_PAPER), + description: Some(format!("0 (not assigned) or a custom size from {MIN_PAPER} to {MAX_PAPER} pt")), + ..typed("number") + }, Kind::Text => typed("string"), Kind::Choice(name, ..) | Kind::Child(name) => typed(name), Kind::File => typed("File"), @@ -186,7 +194,7 @@ impl Start { fn matches(self, kind: Kind, d: &Data) -> bool { match (self, d) { - (Start::Num(a), Data::Number(b)) => (a - b).abs() < 1e-9, + (Start::Num(a), Data::Number(b)) => a == *b, (Start::None, _) => false, _ => self.value(kind) == *d, } @@ -332,14 +340,18 @@ const MARKS: Class = Class { const PAPER: Class = Class { name: "PrintPaperOptions", fields: &[ - f("height", Kind::Number(0.0, 14_400.0), Start::Num(0.0)), + f("height", Kind::PaperSize, Start::Num(0.0)), f("name", Kind::Text, Start::None), kept("offset", Kind::Number(0.0, 14_400.0), Start::Num(0.0)), f("transverse", Kind::Bool, Start::Bool(false)), - f("width", Kind::Number(0.0, 14_400.0), Start::Num(0.0)), + f("width", Kind::PaperSize, Start::Num(0.0)), ], }; +/// The custom paper sizes `file.print` takes, in points. +const MIN_PAPER: f64 = 72.0; +const MAX_PAPER: f64 = 14_400.0; + const POSTSCRIPT: Class = Class { name: "PrintPostScriptOptions", fields: &[ @@ -417,6 +429,14 @@ fn checked(h: &Host, class: &Class, field: &Field, d: &Data) -> std::result::Res } _ => return Err(HostError::Type(format!("{what} must be a finite number"))), }, + Kind::PaperSize => match d { + Data::Number(n) if n.is_finite() => { + if *n != 0.0 && !(MIN_PAPER..=MAX_PAPER).contains(n) { + return Err(HostError::Range(format!("{what} must be 0 or from {MIN_PAPER} to {MAX_PAPER}"))); + } + } + _ => return Err(HostError::Type(format!("{what} must be a finite number"))), + }, Kind::Whole(lo, hi) => match d { Data::Number(n) => { if !whole(*n).is_some_and(|n| (lo..=hi).contains(&n)) { @@ -524,6 +544,8 @@ struct Job { custom: bool, /// `printArea` is assigned: the artboards print even when the base settings ignore them. print_area: bool, + /// `position` is assigned: an untiled job does not use the saved Print Tiling origin. + position: bool, /// The groups of `settings`: the top level and each nested object by its key. top: Map, groups: Vec<(&'static str, Map)>, @@ -784,6 +806,7 @@ fn map_coordinate(h: &Host, slot: usize, job: &mut Job) -> std::result::Result<( _ => "center", }; job.group("placement").insert("origin".into(), json!(origin)); + job.position = true; } _ => {} } @@ -907,11 +930,12 @@ fn doc_print(h: &Host, a: &[Data]) -> Answer { /// name (any case) and a paper by its id or label. An empty name, or one with a path separator or /// a control character, throws. fn check_names(h: &Host, doc: Doc, job: &mut Job) -> std::result::Result<(), HostError> { - let (count, saved_ignore, saved_fit, mut base_profile) = with_doc(h, doc, |_, st| { + let (count, saved_ignore, saved_fit, saved_tiled, mut base_profile) = with_doc(h, doc, |_, st| { let mode = st.doc.print_setup.as_ref().and_then(|v| v.get("artboards")).and_then(Value::as_str); - let fit = st.doc.print_setup.as_ref().and_then(|v| v.get("scaling")).and_then(Value::as_str) == Some("fit"); + let scaling = st.doc.print_setup.as_ref().and_then(|v| v.get("scaling")).and_then(Value::as_str); let profile = st.doc.print_setup.as_ref().and_then(|v| v.get("color")).and_then(|v| v.get("profile")).and_then(Value::as_str); - (st.doc.artboards.len(), mode == Some("ignore"), fit, profile.unwrap_or("").to_string()) + let tiled = matches!(scaling, Some("tileFull" | "tileImageable")); + (st.doc.artboards.len(), mode == Some("ignore"), scaling == Some("fit"), tiled, profile.unwrap_or("").to_string()) }); if let Some(range) = &job.range { vectorcraft_doc::range::parse_range(range, count).map_err(|e| HostError::Range(format!("PrintJobOptions.artboardRange: {e}")))?; @@ -919,17 +943,25 @@ fn check_names(h: &Host, doc: Doc, job: &mut Job) -> std::result::Result<(), Hos let s = h.session.borrow(); let mut base_ignore = saved_ignore; let mut base_fit = saved_fit; + let mut base_tiled = saved_tiled; if let Some(name) = job.preset.take() { let n = plain_name("PrintOptions.printPreset", "print preset", &name)?; let preset = s.print_preset_settings(n).ok_or_else(|| no_such("PrintOptions.printPreset", "print preset", &name))?; base_ignore = matches!(preset.artboards, vectorcraft_pdf::PrintArtboards::Ignore); base_fit = matches!(preset.scaling, vectorcraft_pdf::PrintScaling::Fit); + base_tiled = preset.scaling.tiles(); base_profile.clone_from(&preset.color.profile); job.preset = Some(n.to_string()); } if job.clear_fit && base_fit { job.top.insert("scaling".into(), json!("none")); } + // A placed Print Tiling origin replaces the placement, so an assigned `position` clears it + // for this job when the job does not tile; the saved settings keep it. + let tiled = job.top.get("scaling").and_then(Value::as_str).map_or(base_tiled, |s| matches!(s, "tileFull" | "tileImageable")); + if job.position && !tiled { + job.group("tileOrigin").insert("placed".into(), json!(false)); + } // An assigned `printArea` prints by artboard when the base settings ignore the artboards: the // assigned `artboardRange` when there is one, otherwise every artboard. if job.print_area && base_ignore && !job.top.contains_key("artboards") { diff --git a/crates/engine/src/script/dom/save_open.rs b/crates/engine/src/script/dom/save_open.rs index 2fa9aac1c8..4f2c5288ac 100644 --- a/crates/engine/src/script/dom/save_open.rs +++ b/crates/engine/src/script/dom/save_open.rs @@ -1182,7 +1182,11 @@ mod save_format_tests { for name in ["drawing", "drawing. . ", "drawing.svg\t", "drawing.svg\u{a0}", "folder.svg/drawing", "folder.svg\\drawing"] { assert!(save_format_for_name(name, true).is_none(), "{name}"); } + // Production passes `cfg!(windows)`; a Windows host still splits this name at the backslash. + #[cfg(unix)] assert_eq!(save_format_for_name("proof\\.svg", false).map(|f| f.id), Some("svg")); + #[cfg(windows)] + assert!(save_format_for_name("proof\\.svg", false).is_none()); for windows in [false, true] { assert_eq!(save_format_for_name(".pdf", windows).map(|f| f.id), Some("pdf")); assert!(save_format_for_name(".svg", windows).is_none()); diff --git a/crates/engine/src/tests/script_print.rs b/crates/engine/src/tests/script_print.rs index 9bf3b72bd8..b3e14608f6 100644 --- a/crates/engine/src/tests/script_print.rs +++ b/crates/engine/src/tests/script_print.rs @@ -398,6 +398,9 @@ fn print_options_refuse_bad_values() { ("c.horizontalScale = 1000.01;", "RangeError", "horizontalScale must be from 1 to 1000"), ("c.verticalScale = 1000.01;", "RangeError", "verticalScale must be from 1 to 1000"), ("m.trimMarksWeight = -1;", "RangeError", "trimMarksWeight must be from 0"), + ("o.paperOptions.width = 36;", "RangeError", "width must be 0 or from 72 to 14400"), + ("o.paperOptions.height = 71.9;", "RangeError", "height must be 0 or from 72 to 14400"), + ("o.paperOptions.width = 72; o.paperOptions.width = 0; o.paperOptions.width = 1;", "RangeError", "width must be 0 or from 72"), ("c.orientation = 'PrintOrientation.SIDEWAYS';", "TypeError", "orientation must be a PrintOrientation value"), ("c.orientation = 'PrintTiling.TILEFULLPAGES';", "TypeError", "orientation must be a PrintOrientation value"), ("j.file = 'C:/out.pdf';", "TypeError", "file must be a File"), @@ -438,6 +441,7 @@ fn options_that_are_not_available_throw() { ("o.flattenerOptions.rasterizationResolution = NaN;", "PrintFlattenerOptions.rasterizationResolution"), ("o.pageMarksOptions.bleedOffsetRect = [0, 0, 0, 0];", "PrintPageMarksOptions.bleedOffsetRect is not available yet"), ("o.paperOptions.offset = 10;", "PrintPaperOptions.offset is not available yet"), + ("j.bitmapResolution = 0.0000000005;", "PrintJobOptions.bitmapResolution is not available yet"), ("o.postScriptOptions.postScriptLevel = PrinterPostScriptLevelEnum.PSLEVEL1;", "PSLEVEL1 is not available yet"), ("o.postScriptOptions.binaryPrinting = true;", "PrintPostScriptOptions.binaryPrinting is not available yet"), (&format!("{file} o.coordinateOptions.horizontalScale = 2000; app.activeDocument.print(o);"), "horizontalScale must be from 1 to 1000"), @@ -546,14 +550,46 @@ fn assigned_job_and_scale_options_apply_over_the_saved_settings() { assert_eq!(print(&mut s, "ignore.pdf", ""), 1, "all the art as one page"); assert_eq!(print(&mut s, "area.pdf", "o.jobOptions.printArea = PrintingBounds.ARTBOARDBOUNDS;"), 2, "each artboard prints"); assert_eq!(print(&mut s, "range-ignored.pdf", "o.jobOptions.artboardRange = '2';"), 1, "a range alone keeps ignoring the artboards"); + s.execute("artboard.setProps", &json!({"index": 1, "width": 150})).unwrap(); let area_range = "o.jobOptions.printArea = PrintingBounds.ARTBOARDBOUNDS; o.jobOptions.artboardRange = '2';"; assert_eq!(print(&mut s, "area-range.pdf", area_range), 1, "printArea with a range prints the range"); + let info = vectorcraft_pdf::info(&std::fs::read(dir.join("area-range.pdf")).unwrap(), None).unwrap(); + let trim = info.pages[0].boxes.iter().find(|(k, _)| *k == vectorcraft_pdf::CropTo::Trim).map(|(_, r)| *r).unwrap(); + assert_eq!((trim.width().round(), trim.height().round()), (150.0, 200.0), "the page is artboard 2"); let scaled = "o.coordinateOptions.horizontalScale = 100; o.coordinateOptions.verticalScale = 100.0000000005;"; assert!(print(&mut s, "scale.pdf", scaled) >= 1, "factors that differ by a tiny amount print unlinked"); assert_eq!(s.execute("print.setup", &json!({})).unwrap()["settings"]["artboards"], json!("ignore"), "the saved settings stay"); let _ = std::fs::remove_dir_all(base); } +/// An assigned `position` places an untiled print even when the saved Print Tiling origin is +/// placed; the saved origin stays. +#[test] +fn an_assigned_position_replaces_a_placed_tile_origin() { + let base = folder("position"); + let mut s = session(&base); + let dir = data_folder(&mut s, &base); + let trim = |s: &mut Session, name: &str, body: &str| { + let file = quoted(&dir.join(name)); + logged( + s, + &base, + &format!("Folder.userData; var o = new PrintOptions(); o.jobOptions.file = new File({file}); {body} app.activeDocument.print(o);"), + ); + let info = vectorcraft_pdf::info(&std::fs::read(dir.join(name)).unwrap(), None).unwrap(); + let b = info.pages[0].boxes.iter().find(|(k, _)| *k == vectorcraft_pdf::CropTo::Trim).map(|(_, r)| *r).unwrap(); + [b.x0, b.y0, b.x1, b.y1].map(f64::round) + }; + let top_left = "o.coordinateOptions.position = PrintPosition.TRANSLATETOPLEFT;"; + s.execute("print.setup", &json!({"settings": {"artboards": "range", "range": "1"}})).unwrap(); + let plain = trim(&mut s, "plain.pdf", top_left); + s.execute("print.setup", &json!({"settings": {"tileOrigin": {"placed": true, "x": 100, "y": 0}}})).unwrap(); + assert_ne!(trim(&mut s, "placed.pdf", ""), plain, "the placed origin moves the art"); + assert_eq!(trim(&mut s, "assigned.pdf", top_left), plain, "the assigned position wins"); + assert_eq!(s.execute("print.setup", &json!({})).unwrap()["settings"]["tileOrigin"]["placed"], json!(true), "the saved origin stays"); + let _ = std::fs::remove_dir_all(base); +} + /// The format follows the name `jobOptions.file` gives, not the extension of the file a link in the /// granted folder leads to. #[cfg(unix)] diff --git a/docs/mcp.md b/docs/mcp.md index 25cd5c26a5..73ec9a9895 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3117,7 +3117,8 @@ through `$`: Each method sends command warnings to the Script Console with its name as the prefix: `Document.saveAs: `, `Document.exportFile: ` or `app.open: `. Without options, `saveAs` selects the format from the supplied name before following a link. `app.open(file, documentColorSpace?, options?)` runs `document.open` and gives the new `Document`; a file - larger than 256 MiB throws `Script document exceeds 256 MiB` before it is read + larger than 128 MiB, the most a script command reads, throws before it is read: above 256 MiB the error is + `Script document exceeds 256 MiB`, and otherwise it ends `file exceeds 128 MiB` (https://ai-scripting.docsforadobe.dev/jsobjref/Document/, https://ai-scripting.docsforadobe.dev/jsobjref/Application/). `file` is a `File`. `new IllustratorSaveOptions()`, `new EPSSaveOptions()` and `new PDFSaveOptions()` pick the format (https://ai-scripting.docsforadobe.dev/jsobjref/IllustratorSaveOptions/, @@ -3239,6 +3240,8 @@ through `$`: `printArea` prints the artboards an assigned `artboardRange` names, or each artboard without a range. With one of `horizontalScale` and `verticalScale` assigned, the other keeps its value in the settings `print` starts from. With no scale or tiling assigned, `fitToPage = false` clears a base fit mode and preserves other base scaling modes. + An assigned `position` places an untiled job even when the base settings hold a placed Print Tiling origin; the + saved settings keep that origin. `print` throws before anything is written when an assigned `artboardRange` names an artboard the document does not have, also with `printAllArtboards` true, when `printPreset` or `flattenerPreset` is not a preset's name (any case), when `colorManagementOptions.name` is not an RGB or CMYK profile's name (any @@ -3247,7 +3250,8 @@ through `$`: the base settings; otherwise `print` throws before writing. An assigned name that is empty or only spaces, or holds `/`, `\` or a control character (also at either end), throws too, and each checked name is sent without the spaces around it. `printerName` and `PPDName` take only `""`. A listed option that is not mapped, or a value that is not mapped such as `PrintingBounds.ARTWORKBOUNDS`, `INRIPSEPARATION` or `PSLEVEL1`, throws an error that says it - is not available yet. Print scale factors must be from 1 to 1000 percent. Each value is checked when it is assigned: a wrong type, a number that is not finite or is + is not available yet. Print scale factors must be from 1 to 1000 percent. Paper `width` and `height` take 0 (not + assigned) or 72 to 14,400 pt. Each value is checked when it is assigned: a wrong type, a number that is not finite or is out of range, a string longer than 4,096 bytes, an option object of the wrong class or from an earlier run, and a path given as a string all throw. A run makes at most 256 option objects. Each warning `file.print` gives goes to the Script Console as a line that starts with `Document.print: `. `vc.run` still cannot run `file.print` or