Files
vectorcraft/crates/engine/src/cmd/fileio/tests_exportas.rs
T
dotCooCoo d04a19ac53 Export: warnings name an option the chosen format doesn't read
`document.export`, `document.serialize` and `document.exportSelection`
accept a key that only another format takes. `{"format": "pdf",
"quality": 80}` wrote the PDF and gave no warning about `quality`.

The reply's `warnings` now name each such key, in the wording of a
save's note: "`quality` was not used: PDF files don't read it".

- The format's own options, the commands' own keys, null values and an
  exporter plug-in's format get no note.
- The note names the kind of file written. A legacy `.ai` file
  (`aiVersion: "7"`) reads `outlineText`, `hiddenObjects`,
  `transparentImages`, the artboard choice and `useArtboards`, and
  beside `aiVersion` no other option. The other `.ai` files don't read
  those three options or `preserveEditing`, which is always on for
  them.
- An export doesn't read `separateArtboards`, or `range` for a
  `vectorcraft` or `template` file. A save reads them to write each
  artboard to a file of its own.
- In the app, an export that automation asked for (`app.export` with a
  path, `file.exportAs` run as a command) gives the same notes in its
  reply and in the status bar. A Background Export replies before the
  file is written, and that reply has the notes. An export that a
  person starts from the File menu gets none.
- Export As gives a template export no `useArtboards` or `range`: a
  template doesn't read them.
- `vectorcraft-cli convert` sends `outlineText` only with
  `--outline-text`, and `hiddenLayers` only to the formats that read
  it. A conversion then warns only about an option the command line
  gave.

`document.save` gives its notes as before: it does not apply the `.ai`
rules above.

Docs: docs/mcp.md, docs/control-protocol.md,
docs/target-app-parity.md, the params text of the three commands.
2026-11-13 06:58:28 -08:00

283 lines
14 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Export As: `useArtboards` writes one file per artboard (all or a range) or, off, the bounds of
//! the visible art.
use serde_json::{Value, json};
use super::*;
fn session(width: f64, height: f64, artboards: usize) -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": width, "height": height, "artboards": artboards})).unwrap();
s
}
fn b64(v: &Value) -> Vec<u8> {
vectorcraft_format::base64_decode(v["dataBase64"].as_str().expect("dataBase64")).unwrap()
}
fn rgba(bytes: &[u8]) -> image::RgbaImage {
image::load_from_memory(bytes).unwrap().to_rgba8()
}
fn tmp_dir(tag: &str) -> std::path::PathBuf {
let d = std::env::temp_dir().join(format!("vc-exportas-{tag}-{}", std::process::id()));
std::fs::create_dir_all(&d).unwrap();
d
}
#[test]
fn use_artboards_writes_one_file_per_artboard_in_the_range() {
let mut s = session(60.0, 40.0, 5);
let dir = tmp_dir("range");
let path = dir.join("Logo.png").to_string_lossy().to_string();
let r = s.execute("document.export", &json!({"path": path, "useArtboards": true, "range": "1-3,5"})).unwrap();
let files: Vec<&str> = r["files"].as_array().unwrap().iter().map(|f| f.as_str().unwrap()).collect();
let names: Vec<&str> = files.iter().map(|f| f.rsplit(['/', '\\']).next().unwrap()).collect();
assert_eq!(names, ["Logo-Artboard-1.png", "Logo-Artboard-2.png", "Logo-Artboard-3.png", "Logo-Artboard-5.png"], "{r}");
assert_eq!(r["path"], files[0]);
for f in files {
assert_eq!(rgba(&std::fs::read(f).unwrap()).dimensions(), (60, 40));
}
// Without a range: every artboard; without a path the files come back named after the document.
let r = s.execute("document.export", &json!({"format": "jpg", "useArtboards": true})).unwrap();
let files = r["files"].as_array().unwrap();
assert_eq!(files.len(), 5);
assert_eq!(files[0]["name"], "Untitled-1-Artboard-1.jpg");
assert_eq!(&b64(&files[4])[..2], [0xFF, 0xD8]);
// One artboard is one file, written where asked; a PDF holds the range as pages of one file.
let r = s.execute("document.export", &json!({"format": "webp", "useArtboards": true, "artboards": [3]})).unwrap();
assert!(r.get("files").is_none() && !b64(&r).is_empty(), "{r}");
let r = s.execute("document.export", &json!({"format": "pdf", "useArtboards": true, "range": "2-4"})).unwrap();
assert_eq!(vectorcraft_pdf::import(&b64(&r)).unwrap().artboards.len(), 3);
// Without Use Artboards a raster export holds one artboard.
assert!(s.execute("document.export", &json!({"format": "png", "range": "1-2"})).is_err());
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn per_artboard_names_keep_the_folder_and_extension() {
let mut s = session(20.0, 20.0, 3);
s.execute("artboard.setProps", &json!({"index": 0, "name": "Hero / Wide"})).unwrap();
s.execute("artboard.setProps", &json!({"index": 1, "name": "hero---wide"})).unwrap();
let doc = s.doc().unwrap().doc.clone();
let enc = encode_all(&doc, "jpg", &json!({"useArtboards": true})).unwrap();
let names: Vec<String> = enc.named(&doc, "C:\\out\\shot.jpeg").into_iter().map(|(n, _)| n).collect();
assert_eq!(names, ["C:\\out\\shot-Hero---Wide.jpeg", "C:\\out\\shot-hero---wide-2.jpeg", "C:\\out\\shot-Artboard-3.jpeg"]);
}
#[test]
fn bad_artboard_choices_error() {
let mut s = session(20.0, 20.0, 3);
for p in [
json!({"useArtboards": true, "range": ""}),
json!({"useArtboards": true, "range": "4"}),
json!({"useArtboards": true, "artboards": []}),
json!({"useArtboards": "yes"}),
json!({"format": "pdf", "useArtboards": "yes"}),
] {
assert!(s.execute("document.export", &p).is_err(), "{p}");
}
}
#[test]
fn without_artboards_the_export_covers_the_visible_art() {
let mut s = session(200.0, 100.0, 1);
s.execute("paint.setStroke", &json!({"none": true})).unwrap();
s.execute("shape.rectangle", &json!({"x": 20, "y": 10, "width": 30, "height": 20})).unwrap();
s.execute("shape.rectangle", &json!({"x": 90, "y": 40, "width": 40, "height": 35})).unwrap();
let hidden = s.execute("shape.rectangle", &json!({"x": 150, "y": 0, "width": 40, "height": 99})).unwrap()["id"].as_u64().unwrap();
s.execute("select.set", &json!({"ids": [hidden]})).unwrap();
s.execute("object.hide", &json!({})).unwrap();
// The union of the two visible rectangles: 20..130 × 10..75.
let png = b64(&s.execute("document.export", &json!({"format": "png", "useArtboards": false})).unwrap());
let img = rgba(&png);
assert_eq!(img.dimensions(), (110, 65));
assert_eq!(img.get_pixel(0, 0)[3], 255, "the art starts at the corner");
assert_eq!(
rgba(&b64(&s.execute("document.export", &json!({"format": "png", "useArtboards": false, "ppi": 144})).unwrap())).dimensions(),
(220, 130)
);
// A PDF takes the art's bounds too (SVG has its own `useArtboards` option).
let pdf = vectorcraft_pdf::import(&b64(&s.execute("document.export", &json!({"format": "pdf", "useArtboards": false})).unwrap())).unwrap();
let page = pdf.artboards[0].rect;
assert_eq!((page.width().round(), page.height().round()), (110.0, 65.0));
let mut empty = session(50.0, 50.0, 1);
let e = empty.execute("document.export", &json!({"format": "png", "useArtboards": false})).unwrap_err().to_string();
assert!(e.contains("no visible art"), "{e}");
}
#[test]
fn formats_list_use_artboards() {
let mut s = Session::new();
let r = s.execute("document.formats", &json!({})).unwrap();
let opts = |id: &str| r["formats"].as_array().unwrap().iter().find(|f| f["id"] == id).unwrap()["options"].clone();
for id in ["svg", "pdf", "png", "jpg", "webp"] {
assert!(opts(id).get("useArtboards").is_some(), "{id} lists useArtboards");
assert!(opts(id).get("range").is_some(), "{id} lists range");
}
assert!(opts("vectorcraft").get("useArtboards").is_none());
}
/// An export's reply gives what the file loses before its notes on the options, whatever order
/// the encoder gave them: the WebP encoder notes that lossy WebP isn't applied before the export
/// notes the type it drew in the fallback font. With only notes on the options, they keep their
/// order.
#[test]
fn what_the_file_loses_comes_before_a_note_on_the_options() {
let lossy = "lossy WebP isn't available yet: the file is lossless (exact, but larger)";
let warnings = |s: &mut Session, p: Value| -> Vec<String> {
let r = s.execute("document.export", &p).unwrap();
r["warnings"].as_array().unwrap().iter().filter_map(Value::as_str).map(str::to_string).collect()
};
let mut s = session(60.0, 40.0, 1);
assert_eq!(warnings(&mut s, json!({"format": "webp", "lossless": false})), [lossy]);
s.execute("text.create", &json!({"x": 5, "y": 20, "text": "Ab", "font": "Nonexistent Sans Probe"})).unwrap();
let w = warnings(&mut s, json!({"format": "webp", "lossless": false}));
assert_eq!(w.len(), 2, "{w:?}");
assert!(w[0].contains("fallback font") && w[0].contains("Nonexistent Sans Probe"), "{w:?}");
assert_eq!(w[1], lossy);
// document.serialize gives them in the same order.
let r = s.execute("document.serialize", &json!({"format": "webp", "lossless": false})).unwrap();
assert_eq!(r["warnings"], json!(w));
}
/// A session with one artboard and a selected rectangle on it.
fn with_art() -> Session {
let mut s = session(60.0, 40.0, 1);
s.execute("shape.rectangle", &json!({"x": 5, "y": 5, "width": 20, "height": 10})).unwrap();
s
}
/// The warnings of reply `r` that name an option the file doesn't read.
fn unused(r: &Value) -> Vec<String> {
r["warnings"].as_array().unwrap().iter().filter_map(Value::as_str).filter(|w| w.contains("was not used")).map(str::to_string).collect()
}
/// An option that only another format reads is accepted, and the reply's warnings name it in the
/// words of a save's note; `document.export`, `document.serialize` and `document.exportSelection`
/// give the same note.
#[test]
fn an_option_only_another_format_reads_gets_a_note() {
let (mut s, notes) = (with_art(), unused);
for cmd in ["document.export", "document.serialize", "document.exportSelection"] {
let r = s.execute(cmd, &json!({"format": "pdf", "quality": 80, "outlineText": true})).unwrap();
assert_eq!(
notes(&r),
["`outlineText` was not used: PDF files don't read it", "`quality` was not used: PDF files don't read it"],
"{cmd}: {r}"
);
}
// The format's own options, the keys every export takes and null values get none.
for p in [
json!({"format": "jpg", "quality": 80, "scale": 2}),
json!({"format": "ai", "compress": true, "compatibility": "1.7"}),
json!({"format": "pdf", "quality": null, "selectedOnly": false, "includeArtboardFill": false, "useArtboards": true}),
json!({"format": "svg", "outlineText": true, "svg": {"decimals": 2}}),
] {
let r = s.execute("document.export", &p).unwrap();
assert!(notes(&r).is_empty(), "{p}: {r}");
}
let r = s.execute("document.serialize", &json!({"format": "png", "compress": true, "marks": {"trim": true}, "svg": {"minify": true}})).unwrap();
assert_eq!(
notes(&r),
[
"`compress` was not used: PNG files don't read it",
"`marks` was not used: PNG files don't read it",
"`svg` was not used: PNG files don't read it"
],
"{r}"
);
// The note names the kind of file written, as a save's does.
let r = s.execute("document.export", &json!({"format": "ai", "pdfCompatible": false, "quality": 80})).unwrap();
assert_eq!(notes(&r), ["`quality` was not used: non-PDF-compatible .ai files don't read it"], "{r}");
let r = s.execute("document.export", &json!({"format": "pdf", "preserveEditing": false, "range": "1"})).unwrap();
assert!(notes(&r).is_empty(), "{r}");
let r = s.execute("document.export", &json!({"format": "vectorcraft", "artboard": 0})).unwrap();
assert_eq!(notes(&r), ["`artboard` was not used: VectorCraft files don't read it"], "{r}");
}
/// A legacy `.ai` file reads its own options and the artboards to write, not the other `.ai`
/// options or the PDF options.
#[test]
fn a_legacy_ai_export_notes_the_options_of_the_other_ai_files() {
let (mut s, notes) = (with_art(), unused);
let r = s.execute("document.export", &json!({"format": "ai", "aiVersion": "7", "compress": true, "marks": {"trim": true}})).unwrap();
assert_eq!(
notes(&r),
["`compress` was not used: legacy .ai (version 7) files don't read it", "`marks` was not used: legacy .ai (version 7) files don't read it"],
"{r}"
);
let r =
s.execute("document.export", &json!({"format": "ai", "aiVersion": "7", "outlineText": true, "hiddenObjects": "omit", "range": "1"})).unwrap();
assert!(notes(&r).is_empty(), "{r}");
let r = s.execute("document.export", &json!({"format": "ai", "aiVersion": "7", "useArtboards": false})).unwrap();
assert!(notes(&r).is_empty(), "{r}");
}
/// The `.ai` files other than the legacy one don't read the legacy file's own options.
#[test]
fn a_pdf_compatible_ai_export_notes_the_legacy_options() {
let (mut s, notes) = (with_art(), unused);
let unread = json!({"format": "ai", "aiVersion": "pdf", "outlineText": true, "hiddenObjects": "omit", "transparentImages": "omit"});
let r = s.execute("document.export", &unread).unwrap();
assert_eq!(
notes(&r),
[
"`hiddenObjects` was not used: PDF-compatible .ai files don't read it",
"`outlineText` was not used: PDF-compatible .ai files don't read it",
"`transparentImages` was not used: PDF-compatible .ai files don't read it"
],
"{r}"
);
}
/// `preserveEditing` is always on for a `.ai` file, so an export that gives it gets a note.
#[test]
fn an_ai_export_notes_preserve_editing() {
let (mut s, notes) = (with_art(), unused);
let r = s.execute("document.export", &json!({"format": "ai", "preserveEditing": false, "range": "1"})).unwrap();
assert_eq!(notes(&r), ["`preserveEditing` was not used: PDF-compatible .ai files don't read it"], "{r}");
}
/// An export writes one file: only a save writes each artboard to a file of its own, so an export
/// notes `separateArtboards`, and the `range` that names those files for a native file.
#[test]
fn an_export_notes_the_options_that_only_a_save_reads() {
let (mut s, notes) = (with_art(), unused);
let r = s.execute("document.export", &json!({"format": "ai", "separateArtboards": true, "range": "1"})).unwrap();
assert_eq!(notes(&r), ["`separateArtboards` was not used: PDF-compatible .ai files don't read it"], "{r}");
for (format, label) in [("vectorcraft", "VectorCraft"), ("template", "VectorCraft Template")] {
let r = s.execute("document.export", &json!({"format": format, "separateArtboards": true, "range": "1", "compress": true})).unwrap();
assert_eq!(
notes(&r),
[format!("`range` was not used: {label} files don't read it"), format!("`separateArtboards` was not used: {label} files don't read it")],
"{r}"
);
}
}
/// The legacy `.ai` options that the notes go by are the ones the `.ai` option table describes as
/// the legacy file's ("with aiVersion 7"), so an option added to the table is not noted as unread
/// on a legacy export by mistake.
#[test]
fn the_legacy_ai_options_are_the_ones_the_option_table_names() {
let described: Vec<&str> = native::AI_OPTIONS.iter().filter(|o| o.description.starts_with("with aiVersion 7")).map(|o| o.name).collect();
assert_eq!(described, legacy_ai::LEGACY_ONLY);
for key in described.iter().chain(&ARTBOARD_PARAMS).chain(&["aiVersion", "useArtboards"]) {
assert!(legacy_ai::reads(key), "{key}");
assert!(native::AI_OPTIONS.iter().any(|o| o.name == *key), "{key} is not a .ai option");
}
for key in ["compress", "pdfCompatible", "separateArtboards", "editingData", "marks"] {
assert!(!legacy_ai::reads(key), "{key}");
}
}
#[test]
fn note_texts_give_the_losses_first_each_kind_in_order() {
use super::encode::{Note, note_texts};
let notes = [Note::setting("s1"), Note::loss("l1"), Note::setting("s2"), Note::loss("l2")];
assert_eq!(note_texts(&notes), ["l1", "l2", "s1", "s2"]);
assert_eq!(note_texts(&[Note::setting("s1"), Note::setting("s2")]), ["s1", "s2"]);
assert!(note_texts(&[]).is_empty());
}