Files
effectcraft/crates/pdf/src/tests/vectorcraft.rs
T
.CooCoo 7cbefaf70f Import: VectorCraft documents import through the PDF they carry (#2901)
* Import: VectorCraft documents import through the PDF they carry

File > Import reads a .vectorcraft file through the PDF that VectorCraft
saves in it with Create PDF-Compatible File, one page per artboard, as
footage or as a composition with one layer per PDF layer. The PDF is read
with craft-doc from craft-libs (plain or compressed files, the PDF inline
or after the JSON). A PDF after the JSON may be as large as a PDF file
EffectCraft imports (512 MiB). A document saved without that option gives
an import error that names it.

craft-doc, craft-vectorcraft-format (tests only) and craft-ecproj-format
are pinned to craft-libs fa1a6574; craft-ecproj-format is unchanged
between ca1b5e2e and that commit.

* Import: show file errors returned by the import dialog

* Keep dropped-file import errors alongside local drop errors

* Import: preserve every file error across background jobs and UI notices

* Import: queue pending errors with bounded notification details

* Import: a damaged VectorCraft preview no longer stops the carried PDF

The importer read every rendition with craft_doc::renditions and then
picked the PDF, so a preview whose base64 was invalid or whose data lay
past the end of the file made the whole import fail. It now reads only
the PDF with craft_doc::rendition, from storytold/craft-libs#546, and
never decodes the preview. A document without a PDF still reports
NoCarriedPdf.

* Import: dismissing a picked import's error status acknowledges its details

A picked or menu import queued its error details with the status
report_import_errors set ("first (and 1 more)"), and show_result_errors
then replaced that status with "first; second". The two never matched,
so clicking the status left the details queued and they showed again.
Import commands now move the queued details to the status that replaces
it, also when After Effects project errors lead it. Other commands'
statuses never take over an import's details.
2026-11-15 03:45:56 -05:00

141 lines
6.5 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.
//! VectorCraft documents read through the PDF they carry (Create PDF-Compatible File), written
//! with craft-vectorcraft-format's envelope types: plain and compressed, the PDF inline as base64
//! (version 3) and after the JSON (version 4).
use std::io::Write;
use craft_vectorcraft_format::{BLOB_MAGIC, File, INLINE_MAX, Image};
use crate::write::pdf;
use crate::*;
/// Two artboards: a 200×100 page with the layers "Background" (green) and "Art" (a red left
/// half), and a 100×50 blue page. `pad` adds an unused object so the PDF is long enough to go
/// after the JSON.
fn artboards_pdf(pad: bool) -> Vec<u8> {
let first = "/OC /L0 BDC\n0 1 0 rg 0 0 200 100 re f\nEMC\n/OC /L1 BDC\n1 0 0 rg 0 0 100 100 re f\nEMC\n";
let mut objs = vec![
(1, "<< /Type /Catalog /Pages 2 0 R >>".to_string(), None),
(2, "<< /Type /Pages /Kids [3 0 R 8 0 R] /Count 2 >>".to_string(), None),
(3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Resources << /Properties << /L0 5 0 R /L1 6 0 R >> >> /Contents 4 0 R >>".to_string(), None),
(4, "<< >>".to_string(), Some(first.as_bytes().to_vec())),
(5, "<< /Type /OCG /Name (Background) >>".to_string(), None),
(6, "<< /Type /OCG /Name (Art) >>".to_string(), None),
(7, "<< >>".to_string(), Some(b"0 0 1 rg 0 0 100 50 re f\n".to_vec())),
(8, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 50] /Contents 7 0 R >>".to_string(), None),
];
if pad {
// Written as given (the writer compresses streams without a `/Filter`); never read.
objs.push((9, "<< /Filter /ASCIIHexDecode >>".to_string(), Some(vec![b'0'; INLINE_MAX + 1024])));
}
pdf(&objs, 1)
}
/// A `.vectorcraft` file of `version` holding an empty document and `carried` as its `pdf`
/// member, gzip-compressed when `gzip` is set (Use Compression).
fn vectorcraft(carried: Option<&[u8]>, version: u32, gzip: bool) -> Vec<u8> {
vectorcraft_with_preview(carried, None, version, gzip)
}
/// [`vectorcraft`] with `preview` as the file's `preview` member.
fn vectorcraft_with_preview(carried: Option<&[u8]>, preview: Option<Image>, version: u32, gzip: bool) -> Vec<u8> {
let mut file = File::new(serde_json::json!({"artboards": [], "layers": []}));
file.version = version;
file.generator = Some("VectorCraft 0.1.0".into());
file.preview = preview;
let mut tail = file.tail();
file.pdf = carried.map(|b| tail.image("application/pdf", b, false));
let mut out = serde_json::to_vec(&file).unwrap();
tail.write_to(&mut out).unwrap();
if !gzip {
return out;
}
let mut gz = flate2::write::GzEncoder::new(vec![], flate2::Compression::default());
gz.write_all(&out).unwrap();
gz.finish().unwrap()
}
fn pixels(doc: &Doc) -> Vec<[f32; 4]> {
let (w, h) = doc.pixel_size();
effectcraft_svg::rasterize(doc, w, h, 1.0).data
}
#[test]
fn a_vectorcraft_document_reads_as_the_pdf_it_carries() {
for (version, gzip, pad) in [(3, false, false), (3, true, false), (4, false, true), (4, true, true)] {
let carried = artboards_pdf(pad);
let file = vectorcraft(Some(&carried), version, gzip);
let case = format!("version {version}, gzip {gzip}");
if !gzip {
let after_json = file.windows(BLOB_MAGIC.len()).any(|w| w == BLOB_MAGIC);
assert_eq!(after_json, version == 4, "{case}: the PDF goes after the JSON in version 4");
}
assert_eq!(sniff(&file), Some(Format::VectorCraft), "{case}");
assert_eq!(codec("art.vectorcraft", &file), Some("VectorCraft"), "{case}");
assert_eq!(page_count(&file), 2, "{case}");
let doc = parse(&file).unwrap();
assert_eq!((doc.width, doc.height), (200.0, 100.0), "{case}");
assert_eq!(layer_names(&doc), vec!["Background", "Art"], "{case}");
assert_eq!(pixels(&doc), pixels(&parse(&carried).unwrap()), "{case}: drawn as the PDF itself");
let second = parse_page(&file, 1).unwrap();
assert_eq!((second.width, second.height), (100.0, 50.0), "{case}");
assert_eq!(parse_page(&file, 2).err(), Some(Error::PageOutOfRange(2)), "{case}");
}
}
#[test]
fn a_vectorcraft_document_without_a_pdf_names_create_pdf_compatible_file() {
for gzip in [false, true] {
let file = vectorcraft(None, 4, gzip);
assert_eq!(sniff(&file), Some(Format::VectorCraft));
assert_eq!(page_count(&file), 0);
let e = parse(&file).err();
assert_eq!(e, Some(Error::NoCarriedPdf));
assert!(e.map(|e| e.to_string()).unwrap_or_default().contains("Create PDF-Compatible File"));
}
}
#[test]
fn a_damaged_preview_does_not_stop_the_carried_pdf_from_reading() {
let mut not_base64 = Image::default();
not_base64.mime = "image/png".into();
not_base64.data = "not base64!".into();
let mut past_the_end = Image::default();
past_the_end.mime = "image/png".into();
past_the_end.blob = Some([u64::MAX / 2, 16]);
for (preview, what) in [(not_base64, "not base64"), (past_the_end, "past the end")] {
for (version, gzip, pad) in [(3, false, false), (4, false, true), (4, true, true)] {
let carried = artboards_pdf(pad);
let file = vectorcraft_with_preview(Some(&carried), Some(preview.clone()), version, gzip);
let case = format!("{what}, version {version}, gzip {gzip}");
assert_eq!(page_count(&file), 2, "{case}");
let doc = parse(&file).unwrap_or_else(|e| panic!("{case}: {e}"));
assert_eq!(layer_names(&doc), vec!["Background", "Art"], "{case}");
}
}
}
#[test]
fn a_carried_pdf_may_be_as_large_as_a_pdf_file() {
let limits = carried_limits();
assert_eq!(limits.max_entry, craft_pdf::MAX_INPUT_BYTES as u64);
assert_eq!(limits.max_json, craft_doc::Limits::default().max_json);
assert_eq!(limits.max_total, limits.max_json + 2 * limits.max_entry);
}
#[test]
fn a_vectorcraft_document_read_wrongly_is_an_error() {
// A newer format version.
let newer = vectorcraft(Some(&artboards_pdf(false)), craft_vectorcraft_format::VERSION + 1, false);
let e = parse(&newer).err().map(|e| e.to_string()).unwrap_or_default();
assert!(e.contains("newer"), "{e}");
// A `pdf` member that holds something else.
let not_pdf = vectorcraft(Some(b"not a PDF"), 4, false);
assert!(matches!(parse(&not_pdf), Err(Error::Read(_))));
// A cut-off file.
let file = vectorcraft(Some(&artboards_pdf(true)), 4, true);
assert!(parse(&file[..file.len() / 2]).is_err());
// Other JSON is not a VectorCraft document.
assert_eq!(sniff(br#"{"format":"other","version":1}"#), None);
}