GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Preserve Editing: the native document as an embedded file, found again (also under its legacy
//! name) and checked against the pages it was written with.

use serde_json::json;
use vectorcraft_color::{Color, Paint};
use vectorcraft_doc::{Appearance, Document, Node};
use vectorcraft_geom::{Rect, shapes};

use crate::*;

fn doc() -> Document {
    let mut d = Document::new(100.0, 100.0);
    let layer = d.default_layer().unwrap();
    let id = d.alloc_id();
    let n = Node::path(id, shapes::rectangle(Rect::new(10.0, 10.0, 50.0, 50.0)), Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0));
    d.insert(Some(layer), 0, n).unwrap();
    d
}

/// The document's PDF with `native` embedded (content streams and the payload uncompressed when
/// `compress` is false).
fn pdf_with(native: Option<&[u8]>, compress: bool) -> ExportReport {
    let mut settings: PdfSettings = serde_json::from_value(json!({"preserveEditing": true})).unwrap();
    settings.compression.compress_text = compress;
    export_with_report(&doc(), &PdfOptions { settings, native: native.map(<[u8]>::to_vec), created: Some(0), ..Default::default() }).unwrap()
}

/// `pdf` with `from` replaced by `to` (the same length, so the cross-reference stays valid).
fn patched(pdf: &[u8], from: &[u8], to: &[u8]) -> Vec<u8> {
    assert_eq!(from.len(), to.len());
    let at = pdf.windows(from.len()).position(|w| w == from).unwrap_or_else(|| panic!("no {:?}", String::from_utf8_lossy(from)));
    let mut out = pdf.to_vec();
    out[at..at + to.len()].copy_from_slice(to);
    out
}

#[test]
fn the_native_document_comes_back_intact() {
    for compress in [true, false] {
        let r = pdf_with(Some(b"{\"native\": true}"), compress);
        assert!(r.warnings.is_empty(), "{:?}", r.warnings);
        assert!(r.bytes.starts_with(b"%PDF-"));
        let e = editing(&r.bytes).unwrap();
        assert_eq!((e.data.as_slice(), e.intact), (&b"{\"native\": true}"[..], true));
        // The import reports it too, and the pages still import as artwork.
        let imported = import_with_report(&r.bytes, &ImportOptions::default()).unwrap();
        assert_eq!(imported.native, Some(e));
        assert_eq!(imported.document.artboards.len(), 1);
    }
}

#[test]
fn the_native_documents_modification_date_is_the_time_of_writing() {
    // Created in 1970: the information dictionary's dates say so, the embedded file's doesn't.
    let year = |t: Option<i64>| vectorcraft_doc::metadata::civil(t.unwrap())[0];
    let before = year(vectorcraft_doc::metadata::output_time());
    let text = String::from_utf8_lossy(&pdf_with(Some(b"native"), false).bytes).into_owned();
    let after = year(vectorcraft_doc::metadata::output_time());
    assert!(text.contains("/CreationDate(D:1970"), "the file's creation date");
    assert!([before, after].iter().any(|y| text.contains(&format!("/ModDate(D:{y:04}"))), "the embedded file is dated when it is written");
}

#[test]
fn changed_pages_are_noticed() {
    let r = pdf_with(Some(b"native"), false);
    let resized = patched(&r.bytes, b"/MediaBox[0 0 100 100]", b"/MediaBox[0 0 100 101]");
    assert!(!editing(&resized).unwrap().intact, "another page size");
    // Drawing changed elsewhere: the rectangle's first corner moves.
    let moved = patched(&r.bytes, b"10 10 m", b"10 11 m");
    assert!(!editing(&moved).unwrap().intact, "other page content");
    // The description (not part of the hash) can change: re-saving keeps the data usable.
    assert!(editing(&r.bytes).unwrap().intact);
}

/// `pdf` with its editing file's description rewritten by another app as "hash " and the hash it
/// held, in the text string encoding `bom` starts (UTF-16BE or UTF-8), padded with spaces to its
/// length.
fn described(pdf: &[u8], bom: &[u8]) -> Vec<u8> {
    let from = b"(VectorCraft editing data; page hash ";
    let at = pdf.windows(from.len()).position(|w| w == from).unwrap_or_else(|| panic!("no description"));
    let (hash, len) = (&pdf[at + from.len()..at + from.len() + 16], from.len() + 17);
    let mut desc = [b"(", bom].concat();
    for &c in b"hash ".iter().chain(hash) {
        if bom.len() == 2 {
            desc.push(0);
        }
        desc.push(c);
    }
    desc.push(b')');
    desc.resize(len, b' ');
    patched(pdf, &pdf[at..at + len], &desc)
}

#[test]
fn the_hash_is_read_from_a_description_in_any_encoding() {
    let r = pdf_with(Some(b"native"), false);
    let resized = patched(&r.bytes, b"/MediaBox[0 0 100 100]", b"/MediaBox[0 0 100 101]");
    for bom in [&[0xFE, 0xFF][..], &[0xEF, 0xBB, 0xBF]] {
        assert!(editing(&described(&r.bytes, bom)).unwrap().intact, "{bom:?}: the same pages");
        assert!(!editing(&described(&resized, bom)).unwrap().intact, "{bom:?}: another page size");
    }
}

#[test]
fn the_legacy_name_is_read_and_plain_pdfs_have_no_editing_data() {
    let r = pdf_with(Some(b"legacy"), false);
    let name = format!("({EDITING_FILE})");
    let legacy = format!("({LEGACY_EDITING_FILE}){}", " ".repeat(EDITING_FILE.len() - LEGACY_EDITING_FILE.len()));
    let mut old = r.bytes.clone();
    // Every place the name is written (/F, /UF and the name tree key).
    while let Some(at) = old.windows(name.len()).position(|w| w == name.as_bytes()) {
        old[at..at + legacy.len()].copy_from_slice(legacy.as_bytes());
    }
    let e = editing(&old).unwrap();
    assert_eq!((e.data.as_slice(), e.intact), (&b"legacy"[..], true));
    // Off, or with no native document given: nothing embedded (the latter warns).
    let plain = export(&doc(), &PdfOptions::default()).unwrap();
    assert_eq!(editing(&plain), None);
    assert_eq!(import_with_report(&plain, &ImportOptions::default()).unwrap().native, None);
    let r = pdf_with(None, true);
    assert!(r.warnings.is_empty(), "{:?}", r.warnings);
    assert!(r.setting_notes.len() == 1 && r.setting_notes[0].contains("Preserve editing"), "{:?}", r.setting_notes);
    assert_eq!(editing(&r.bytes), None);
}

#[test]
fn pdf_a_carries_no_editing_data() {
    let s: PdfSettings = serde_json::from_value(json!({"standard": "pdfA2b", "preserveEditing": true})).unwrap();
    assert!(matches!(s.check(), Err(PdfError::BadSetting(m)) if m.contains("editing")));
    assert!(matches!(s.check_values(), Err(PdfError::BadSetting(_))));
}

/// A tiny PDF made of the given objects (numbered from 1), with the cross-reference table written.
fn raw_pdf(objects: &[String]) -> Vec<u8> {
    raw_pdf_bytes(objects.iter().map(|o| o.clone().into_bytes()))
}

/// [`raw_pdf`] of objects that may hold binary data.
fn raw_pdf_bytes(objects: impl IntoIterator<Item = Vec<u8>>) -> Vec<u8> {
    let mut out = b"%PDF-1.7\n".to_vec();
    let mut offsets = Vec::new();
    for (i, body) in objects.into_iter().enumerate() {
        offsets.push(out.len());
        out.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes());
        out.extend_from_slice(&body);
        out.extend_from_slice(b"\nendobj\n");
    }
    let xref = out.len();
    out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", offsets.len() + 1).as_bytes());
    for at in &offsets {
        out.extend_from_slice(format!("{at:010} 00000 n \n").as_bytes());
    }
    out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", offsets.len() + 1).as_bytes());
    out
}

#[test]
fn a_name_tree_node_shared_by_many_kids_is_searched_once() {
    // Objects 4..=15 are a chain of name tree nodes, each listing the next one ten times and
    // holding no attachment. Walking every path would take 10^12 steps; the editing file sits in
    // the leaf (object 16) listed after the chain.
    let mut objects = vec![
        "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
        "<< /Type /Pages /Kids [19 0 R] /Count 1 >>".to_string(),
        "<< /Kids [4 0 R 16 0 R] >>".to_string(),
    ];
    for node in 4..=14 {
        objects.push(format!("<< /Kids [{}] >>", format!("{} 0 R ", node + 1).repeat(10)));
    }
    objects.push("<< /Names [] >>".to_string());
    objects.push("<< /Names [(a) 17 0 R] >>".to_string());
    objects.push(format!("<< /Type /Filespec /UF ({EDITING_FILE}) /EF << /F 18 0 R >> >>"));
    objects.push("<< /Length 5 >>\nstream\nhello\nendstream".to_string());
    objects.push("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string());
    let e = editing(&raw_pdf(&objects)).unwrap();
    assert_eq!((e.data.as_slice(), e.intact), (&b"hello"[..], true));
}

#[test]
fn pages_sharing_a_large_content_stream_are_hashed_within_a_budget() {
    use std::io::Write;
    // 300 pages showing one stream of 1 MiB of spaces: 300 MiB decoded, past the 256 MiB hashed.
    let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best());
    z.write_all(&vec![b' '; 1 << 20]).unwrap();
    let packed = z.finish().unwrap();
    let pages = 300;
    let kids: String = (0..pages).map(|i| format!("{} 0 R ", 7 + i)).collect();
    let mut objects = vec![
        "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
        format!("<< /Type /Pages /Kids [{kids}] /Count {pages} >>"),
        "<< /Names [(a) 4 0 R] >>".to_string(),
        format!("<< /Type /Filespec /UF ({EDITING_FILE}) /Desc (hash 0000000000000000) /EF << /F 5 0 R >> >>"),
        "<< /Length 5 >>\nstream\nhello\nendstream".to_string(),
        String::new(),
    ];
    for _ in 0..pages {
        objects.push("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] /Contents 6 0 R >>".to_string());
    }
    let mut stream = format!("<< /Length {} /Filter /FlateDecode >>\nstream\n", packed.len()).into_bytes();
    stream.extend_from_slice(&packed);
    stream.extend_from_slice(b"\nendstream");
    let bytes = raw_pdf_bytes(objects.into_iter().enumerate().map(|(i, o)| if i == 5 { stream.clone() } else { o.into_bytes() }));
    let start = std::time::Instant::now();
    let e = editing(&bytes).unwrap();
    assert_eq!((e.data.as_slice(), e.intact), (&b"hello"[..], false));
    assert!(start.elapsed() < std::time::Duration::from_secs(60), "{:?}", start.elapsed());
}

#[test]
fn direct_name_tree_kids_are_all_searched() {
    // Direct kids report the object holding them as their id; none of them may be taken as seen.
    let objects = [
        "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
        "<< /Type /Pages /Kids [6 0 R] /Count 1 >>".to_string(),
        "<< /Kids [<< /Names [] >> << /Kids [<< /Names [] >> << /Names [(a) 4 0 R] >>] >>] >>".to_string(),
        format!("<< /Type /Filespec /UF ({EDITING_FILE}) /EF << /F 5 0 R >> >>"),
        "<< /Length 5 >>\nstream\nhello\nendstream".to_string(),
        "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
    ];
    let e = editing(&raw_pdf(&objects)).unwrap();
    assert_eq!(e.data, b"hello");
}

#[test]
fn a_file_name_written_as_utf16_is_found() {
    let utf16: String = EDITING_FILE.bytes().map(|c| format!("00{c:02X}")).collect();
    for key in ["UF", "F"] {
        let objects = [
            "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
            "<< /Type /Pages /Kids [6 0 R] /Count 1 >>".to_string(),
            "<< /Names [(a) 4 0 R] >>".to_string(),
            format!("<< /Type /Filespec /{key} <FEFF{utf16}> /EF << /F 5 0 R >> >>"),
            "<< /Length 5 >>\nstream\nhello\nendstream".to_string(),
            "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
        ];
        let e = editing(&raw_pdf(&objects)).unwrap_or_else(|| panic!("/{key}: not found"));
        assert_eq!(e.data, b"hello", "/{key}");
    }
}

#[test]
fn an_editing_file_whose_data_cannot_be_resolved_is_still_listed() {
    // No `EF` dictionary, an `EF` without `F` or `UF`, and an `F` naming no object.
    for ef in ["", "/EF << >>", "/EF << /F 9 0 R >>"] {
        let objects = [
            "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
            "<< /Type /Pages /Kids [5 0 R] /Count 1 >>".to_string(),
            "<< /Names [(a) 4 0 R] >>".to_string(),
            format!("<< /Type /Filespec /UF ({EDITING_FILE}) {ef} >>"),
            "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
        ];
        let opened = open_pdf(&raw_pdf(&objects), None).unwrap();
        assert_eq!(opened.editing(), None, "{ef:?}");
        assert!(opened.has_editing_file(), "{ef:?}");
    }
    // A name tree key naming it, with a file specification that names no object or isn't one.
    for spec in ["9 0 R", "(not a file specification)"] {
        let objects = [
            "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
            "<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_string(),
            format!("<< /Names [(a) 9 0 R ({EDITING_FILE}) {spec}] >>"),
            "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
        ];
        let opened = open_pdf(&raw_pdf(&objects), None).unwrap();
        assert_eq!(opened.editing(), None, "{spec}");
        assert!(opened.has_editing_file(), "{spec}");
    }
    // The key written as an indirect string.
    let objects = [
        "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
        "<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_string(),
        "<< /Names [5 0 R 9 0 R] >>".to_string(),
        "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
        format!("({EDITING_FILE})"),
    ];
    assert!(open_pdf(&raw_pdf(&objects), None).unwrap().has_editing_file());
    // A PDF without the editing file, and one with it.
    assert!(!open_pdf(&export(&doc(), &PdfOptions::default()).unwrap(), None).unwrap().has_editing_file());
    let opened = open_pdf(&pdf_with(Some(b"native"), false).bytes, None).unwrap();
    assert!(opened.editing().is_some() && opened.has_editing_file());
}

#[test]
fn an_editing_file_deep_in_the_name_tree_is_found() {
    // Objects 4..=203 are a chain of 200 name tree nodes, each listing the next one; the editing
    // file sits in the leaf (object 204), with its data or naming data that can't be resolved.
    for ef in ["/EF << /F 206 0 R >>", "/EF << /F 999 0 R >>"] {
        let mut objects = vec![
            "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
            "<< /Type /Pages /Kids [207 0 R] /Count 1 >>".to_string(),
            "<< /Kids [4 0 R] >>".to_string(),
        ];
        for node in 4..=203 {
            objects.push(format!("<< /Kids [{} 0 R] >>", node + 1));
        }
        objects.push("<< /Names [(a) 205 0 R] >>".to_string());
        objects.push(format!("<< /Type /Filespec /UF ({EDITING_FILE}) {ef} >>"));
        objects.push("<< /Length 5 >>\nstream\nhello\nendstream".to_string());
        objects.push("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string());
        let opened = open_pdf(&raw_pdf(&objects), None).unwrap();
        assert!(opened.has_editing_file(), "{ef}");
        let data = opened.editing().map(|e| e.data);
        assert_eq!(data.as_deref(), ef.contains("206").then_some(&b"hello"[..]), "{ef}");
    }
    // A name tree with more nodes than are searched may list it.
    let objects = [
        "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
        "<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_string(),
        format!("<< /Kids [{}] >>", "<< /Names [] >> ".repeat(70_000)),
        "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
    ];
    let opened = open_pdf(&raw_pdf(&objects), None).unwrap();
    assert_eq!(opened.editing(), None);
    assert!(opened.has_editing_file() && opened.editing_search_cut());
    // One that lists a readable editing file before the nodes left unsearched and another after
    // them: the first is read, and the search reads as cut short.
    let leaf = |stream: u32| format!("<< /Names [({EDITING_FILE}) << /Type /Filespec /UF ({EDITING_FILE}) /EF << /F {stream} 0 R >> >>] >>");
    let objects = [
        "<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
        "<< /Type /Pages /Kids [8 0 R] /Count 1 >>".to_string(),
        format!("<< /Kids [4 0 R {}5 0 R] >>", "<< /Names [] >> ".repeat(70_000)),
        leaf(6),
        leaf(7),
        "<< /Length 5 >>\nstream\nfirst\nendstream".to_string(),
        "<< /Length 6 >>\nstream\nsecond\nendstream".to_string(),
        "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
    ];
    let opened = open_pdf(&raw_pdf(&objects), None).unwrap();
    assert_eq!(opened.editing().map(|e| e.data).as_deref(), Some(&b"first"[..]));
    assert!(opened.has_editing_file() && opened.editing_search_cut());
    // The same, with the nodes past the limit in a subtree after the one holding the first.
    let mut objects = objects;
    objects[2] = format!("<< /Kids [4 0 R << /Kids [{}5 0 R] >>] >>", "<< /Names [] >> ".repeat(70_000));
    let opened = open_pdf(&raw_pdf(&objects), None).unwrap();
    assert_eq!(opened.editing().map(|e| e.data).as_deref(), Some(&b"first"[..]));
    assert!(opened.has_editing_file() && opened.editing_search_cut());
    // A tree searched to its end.
    let opened = open_pdf(&pdf_with(Some(b"native"), false).bytes, None).unwrap();
    assert!(opened.editing().is_some() && !opened.editing_search_cut());
}