GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Native files: `document.save {compress, version, preview}`, the Use Compression preference,
//! atomic writes, unused images left out, keys from newer versions and colour profiles kept.

use std::path::PathBuf;

use serde_json::{Value, json};
use vectorcraft_format::{is_compressed, sniff};

use crate::*;

fn session() -> Session {
    let mut s = Session::new();
    s.execute("file.new", &json!({"width": 400, "height": 200})).unwrap();
    s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 100, "height": 50})).unwrap();
    s
}

fn tmp_dir(tag: &str) -> PathBuf {
    let d = std::env::temp_dir().join(format!("vc-native-{tag}-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&d);
    std::fs::create_dir_all(&d).unwrap();
    d
}

fn bytes_of(r: &Value) -> Vec<u8> {
    vectorcraft_format::base64_decode(r["dataBase64"].as_str().expect("dataBase64")).unwrap()
}

/// The file `document.save` (no path) gives back with `p`.
fn saved(s: &mut Session, p: Value) -> Vec<u8> {
    bytes_of(&s.execute("document.save", &p).unwrap())
}

fn open(s: &mut Session, name: &str, bytes: &[u8]) -> Value {
    s.execute("document.open", &json!({"name": name, "dataBase64": vectorcraft_format::base64_encode(bytes)})).unwrap()
}

/// The file's JSON (unpacked when compressed).
fn json_of(bytes: &[u8]) -> Value {
    let mut text = Vec::new();
    if is_compressed(bytes) {
        std::io::Read::read_to_end(&mut flate2::read::GzDecoder::new(bytes), &mut text).unwrap();
    } else {
        text = bytes.to_vec();
    }
    serde_json::from_slice(&text).unwrap()
}

#[test]
fn compressed_saves_open_by_their_content() {
    let mut s = session();
    let packed = saved(&mut s, json!({"compress": true}));
    assert!(is_compressed(&packed) && sniff(&packed));
    let plain = saved(&mut s, json!({}));
    assert!(!is_compressed(&plain) && packed.len() < plain.len());
    // Recognised by its content, whatever the name says.
    for name in ["a.vectorcraft", "a.drawcraft", "a.svgz", "a.bin"] {
        let r = open(&mut s, name, &packed);
        assert_eq!(r["format"], "vectorcraft", "{name}");
        assert_eq!(s.doc().unwrap().doc.node_count(), 2);
    }
}

#[test]
fn use_compression_preference_sets_the_default() {
    let mut s = session();
    s.execute("prefs.set", &json!({"key": "useCompression", "value": true})).unwrap();
    assert!(is_compressed(&saved(&mut s, json!({}))));
    assert!(!is_compressed(&saved(&mut s, json!({"compress": false}))), "the param wins");
    // Older versions can't read compressed files: those saves stay plain.
    assert!(!is_compressed(&saved(&mut s, json!({"version": 2}))));
    assert!(s.execute("document.save", &json!({"version": 2, "compress": true})).is_err());
    assert!(is_compressed(&bytes_of(&s.execute("file.saveAsTemplate", &json!({})).unwrap())));
    // Exports write what they're told.
    let r = s.execute("document.serialize", &json!({"format": "vectorcraft"})).unwrap();
    assert!(!is_compressed(&bytes_of(&r)));
}

#[test]
fn saves_for_older_versions() {
    let mut s = session();
    for v in [1, 2] {
        let bytes = saved(&mut s, json!({"version": v}));
        let j = json_of(&bytes);
        assert_eq!((j["format"].as_str(), j["version"].as_u64()), (Some("drawcraft"), Some(v)));
        open(&mut s, "old.drawcraft", &bytes);
        assert_eq!(s.doc().unwrap().doc.node_count(), 2);
    }
    for bad in [json!({"version": 0}), json!({"version": 9}), json!({"version": "two"})] {
        assert!(s.execute("document.save", &bad).is_err(), "{bad}");
    }
    let formats = s.execute("document.formats", &json!({})).unwrap();
    let native = formats["formats"].as_array().unwrap().iter().find(|f| f["id"] == "vectorcraft").unwrap();
    for o in ["compress", "version", "preview"] {
        assert!(native["options"].get(o).is_some(), "{o} documented");
    }
}

#[test]
fn an_older_version_is_pretty_only_when_its_own_json_fits() {
    // One path whose anchors fit the pretty size limit in the current version but not as
    // version 1 writes them (both handles of every anchor as `{x, y}` maps).
    use vectorcraft_geom::Point;
    use vectorcraft_geom::path::{PathData, SubPath};
    let path_doc = |anchors: usize| {
        let mut b = vectorcraft_testkit::fixtures::DocBuilder::new(1000.0, 1000.0);
        let points: Vec<Point> = (0..anchors).map(|j| Point::new(j as f64, (j % 2) as f64)).collect();
        let node = b.path_node(PathData::single(SubPath::polyline(&points, false)), Default::default());
        b.add(node);
        b.doc
    };
    let per_anchor = serde_json::to_vec_pretty(&path_doc(1000)).unwrap().len() / 1000;
    let d = path_doc(vectorcraft_format::PRETTY_MAX_BYTES / 2 / per_anchor);
    let mut s = Session::new();
    open(&mut s, "traced.vectorcraft", &vectorcraft_format::save_file(&d));
    let is_pretty = |bytes: &[u8]| bytes.windows(3).any(|w| w == b"\n  ");
    assert!(is_pretty(&saved(&mut s, json!({"compress": false}))));
    let old = saved(&mut s, json!({"version": 1}));
    assert!(!is_pretty(&old), "{} bytes", old.len());
    open(&mut s, "old.drawcraft", &old);
    assert_eq!(s.doc().unwrap().doc.node_count(), 2);
}

#[test]
fn preview_is_at_most_256_pixels() {
    let mut s = session();
    let bytes = saved(&mut s, json!({"preview": true, "compress": true}));
    let png = vectorcraft_format::preview(&bytes).expect("a preview");
    let img = craft_image_codecs::decode(&png)
        .map(|__cimg| {
            let (__w, __h) = __cimg.dimensions();
            match (__cimg.layout(), __cimg.sample_type()) {
                (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
                    image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
                }
                (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
                    image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
                }
                (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
                    image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
                }
                _ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
            }
        })
        .unwrap();
    assert_eq!((img.width(), img.height()), (256, 128), "the 400×200 artboard fitted into 256 px");
    assert!(vectorcraft_format::preview(&saved(&mut s, json!({}))).is_none(), "off by default");
}

#[test]
fn saving_writes_through_a_temporary_file() {
    let d = tmp_dir("atomic");
    let path = d.join("doc.vectorcraft");
    std::fs::write(&path, b"an older version of the file").unwrap();
    let mut s = session();
    s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap();
    assert!(sniff(&std::fs::read(&path).unwrap()));
    let names: Vec<String> = std::fs::read_dir(&d).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().to_string()).collect();
    assert_eq!(names, ["doc.vectorcraft"], "no temporary file left behind");
    assert!(!s.doc().unwrap().is_dirty());
    // A save that can't be written fails, and the document stays modified.
    s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 5, "height": 5})).unwrap();
    let missing = d.join("missing").join("doc.vectorcraft");
    assert!(s.execute("document.save", &json!({"path": missing.to_string_lossy()})).is_err());
    assert!(s.doc().unwrap().is_dirty());
    let _ = std::fs::remove_dir_all(&d);
}

#[test]
fn unused_images_are_left_out_of_saves() {
    let mut s = session();
    let mut png = vec![];
    {
        let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([1, 2, 3, 255]))).into();
        let (__w, __h) = (__dyn.width(), __dyn.height());
        let (__layout, __sample, __raw) = match __dyn {
            image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
            image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
            image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
            image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
            image::DynamicImage::ImageLuma16(b) => (
                craft_image_codecs::ChannelLayout::Gray,
                craft_image_codecs::SampleType::U16,
                b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
            ),
            image::DynamicImage::ImageLumaA16(b) => (
                craft_image_codecs::ChannelLayout::GrayA,
                craft_image_codecs::SampleType::U16,
                b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
            ),
            image::DynamicImage::ImageRgb16(b) => (
                craft_image_codecs::ChannelLayout::Rgb,
                craft_image_codecs::SampleType::U16,
                b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
            ),
            image::DynamicImage::ImageRgba16(b) => (
                craft_image_codecs::ChannelLayout::Rgba,
                craft_image_codecs::SampleType::U16,
                b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
            ),
            other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
        };
        let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
        craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
            .map(|__b| png.extend_from_slice(&__b))
    }
    .unwrap();
    let r = s.execute("file.place", &json!({"name": "a.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
    let id = r["ids"][0].as_u64().unwrap();
    assert_eq!(json_of(&saved(&mut s, json!({})))["images"].as_object().unwrap().len(), 1);
    s.execute("edit.clear", &json!({"ids": [id]})).unwrap();
    assert_eq!(s.doc().unwrap().doc.images.len(), 1, "kept in memory (undo can bring the image back)");
    assert!(json_of(&saved(&mut s, json!({})))["images"].as_object().unwrap().is_empty());
    s.execute("edit.undo", &json!({})).unwrap();
    let bytes = saved(&mut s, json!({}));
    open(&mut s, "back.vectorcraft", &bytes);
    assert_eq!(s.doc().unwrap().doc.images.len(), 1);
}

#[test]
fn newer_keys_and_legacy_profiles_survive_open_and_save() {
    let mut s = session();
    let mut j = json_of(&saved(&mut s, json!({"version": 2})));
    j["document"]["fromTheFuture"] = json!({"x": 1});
    j["document"]["unknown"] = json!({"colorProfiles": {"rgb": "sRGB IEC61966-2.1", "cmyk": null}});
    open(&mut s, "old.vectorcraft", &serde_json::to_vec(&j).unwrap());
    let d = &s.doc().unwrap().doc;
    assert_eq!(d.color_profiles.rgb.as_deref(), Some("sRGB IEC61966-2.1"), "profiles migrate");
    assert!(d.unknown.is_empty());
    assert_eq!(cmd::colormgmt::doc_profiles(d).0.as_deref(), Some("sRGB IEC61966-2.1"));
    let again = json_of(&saved(&mut s, json!({})));
    assert_eq!(again["document"]["fromTheFuture"], json!({"x": 1}));
    assert_eq!(again["document"]["color_profiles"]["rgb"], "sRGB IEC61966-2.1");
    // Saved for v2 again: back where v2 apps read them.
    let v2 = json_of(&saved(&mut s, json!({"version": 2})));
    assert_eq!(v2["document"]["unknown"]["colorProfiles"]["rgb"], "sRGB IEC61966-2.1");
}

/// A version 1 document saved before VectorCraft 0.21.0 turned its Twist effects counterclockwise
/// for a positive angle. It opens with the angles negated, so its art turns as it did; saved
/// again it is a document of the current version, and saved for an older file version it is version 1 again.
#[test]
fn a_version_1_documents_twist_turns_as_it_was_saved() {
    use vectorcraft_doc::{Appearance, Document, Effect, Node};
    use vectorcraft_geom::{PathData, Point, Rect, SubPath};
    let mut d = Document::new(100.0, 100.0);
    let l = d.layers[0].id;
    let id = d.alloc_id();
    let right = PathData::single(SubPath::polyline(&[Point::new(55.0, 50.0), Point::new(56.0, 50.0)], false));
    let mut a = Appearance::default_art();
    a.effects.push(Effect { id: "distort.twist".into(), params: json!({"angle": 45}), visible: true });
    d.insert(Some(l), 0, Node::path(id, right.clone(), a)).unwrap();
    let mut j = json_of(&vectorcraft_format::save(&d, false));
    j["document"]["version"] = json!(1);
    j["generator"] = json!("VectorCraft 0.20.0");
    let mut s = Session::new();
    open(&mut s, "old.vectorcraft", &serde_json::to_vec(&j).unwrap());
    let doc = &s.doc().unwrap().doc;
    let n = doc.layers[0].children().and_then(|c| c.first()).cloned().unwrap();
    let turned = vectorcraft_render::effects::apply_geometry(&n.appearance.effects, &right, Rect::new(0.0, 0.0, 100.0, 100.0));
    // Counterclockwise on the page takes a point right of the center up (y runs down).
    let p = turned.subpaths[0].anchors[0].p;
    assert!(p.y < 49.5, "{p:?}");
    let again = json_of(&saved(&mut s, json!({})));
    assert_eq!(again["document"]["version"], vectorcraft_doc::FORMAT_VERSION);
    assert!(again.to_string().contains(r#""angle":-45.0"#), "{again}");
    // Saved for an older file version, it is version 1 with its angle as 0.20.0 saved it.
    let older = json_of(&saved(&mut s, json!({"version": 3})));
    assert_eq!(older["document"]["version"], 1);
    assert!(older.to_string().contains(r#""angle":45.0"#), "{older}");
}

/// A save records the date the document was made in, so the same commands twice gave different
/// bytes (#2880). A pinned date makes them identical — `file.new {created}` here, and
/// `SOURCE_DATE_EPOCH` for an export that takes no date param. The PDF a native file carries by
/// default is dated by the document's dates, not by the time of writing.
#[test]
fn a_pinned_date_makes_saves_byte_identical() {
    let once = |t: i64| {
        let mut s = Session::new();
        s.execute("file.new", &json!({"width": 400, "height": 200, "created": t})).unwrap();
        s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 100, "height": 50})).unwrap();
        saved(&mut s, json!({}))
    };
    let at = 1_791_208_980;
    let pdf = vectorcraft_format::pdf_content(&once(at)).expect("a native save carries a PDF by default");
    let info = vectorcraft_pdf::file_info(&pdf, None).unwrap();
    assert_eq!((info.created, info.modified), (Some(at), Some(at)), "the PDF has the document's dates");
    assert_eq!(once(at), once(at), "the same document and date give the same bytes");
    // The date really is in the bytes, so the comparison above is not vacuous.
    assert_ne!(once(at), once(at + 1), "another date shows in the bytes");
}