mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
A save for format versions 1 to 3 wrote `live: {shape: "star"}`, which
those versions' readers reject, so the file did not open there. These
saves now drop the live star and keep the outline it draws as a plain
path, in the layers, symbols, graph designs, patterns, opacity masks
and brush definitions. The current version keeps the star live.
430 lines
19 KiB
Rust
430 lines
19 KiB
Rust
//! `.vectorcraft` v3: compression, pruning unused images, keys from newer versions, colour
|
|
//! profiles as a document field, saving for older versions and the embedded preview.
|
|
|
|
use std::io::Write as _;
|
|
use std::sync::Arc;
|
|
|
|
use serde_json::{Value, json};
|
|
use vectorcraft_doc::{ColorProfiles, Composer, Document, ImageBlob, ImageObject, Node, NodeKind, OpacityMask, Symbol};
|
|
use vectorcraft_format::{FormatError, SaveOptions, VERSION, is_compressed, load, preview, save, save_with, sniff};
|
|
use vectorcraft_geom::Affine;
|
|
use vectorcraft_testkit::fixtures;
|
|
use vectorcraft_testkit::invariants::{check_native_roundtrip, doc_json, json_approx_eq};
|
|
|
|
fn rich_doc() -> Document {
|
|
(*fixtures::rich_session().doc().unwrap().doc).clone()
|
|
}
|
|
|
|
fn json_of(bytes: &[u8]) -> Value {
|
|
serde_json::from_slice(bytes).unwrap()
|
|
}
|
|
|
|
/// `v` (a document's JSON) with every live star's `live` taken off its path.
|
|
fn without_live_stars(v: &mut Value) {
|
|
match v {
|
|
Value::Object(o) => {
|
|
if o.get("live").and_then(|l| l.get("shape")).and_then(Value::as_str) == Some("star") {
|
|
o.remove("live");
|
|
}
|
|
o.values_mut().for_each(without_live_stars);
|
|
}
|
|
Value::Array(a) => a.iter_mut().for_each(without_live_stars),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fn gzip(bytes: &[u8]) -> Vec<u8> {
|
|
let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
|
|
gz.write_all(bytes).unwrap();
|
|
gz.finish().unwrap()
|
|
}
|
|
|
|
fn blob(bytes: &[u8]) -> ImageBlob {
|
|
ImageBlob::new("image/png", bytes.to_vec())
|
|
}
|
|
|
|
fn image_node(d: &mut Document, key: &str) -> Node {
|
|
let id = d.alloc_id();
|
|
Node::new(
|
|
id,
|
|
NodeKind::Image(Box::new(ImageObject {
|
|
key: key.into(),
|
|
width: 2,
|
|
height: 2,
|
|
xf: Affine::IDENTITY,
|
|
link: None,
|
|
placement: Default::default(),
|
|
pixels: None,
|
|
})),
|
|
)
|
|
}
|
|
|
|
fn opts(version: u32, compress: bool) -> SaveOptions {
|
|
SaveOptions { version, compress, ..SaveOptions::default() }
|
|
}
|
|
|
|
#[test]
|
|
fn area_type_vertical_alignment_round_trips() {
|
|
let d = rich_doc();
|
|
let text = String::from_utf8(save(&d, false)).unwrap();
|
|
assert!(text.contains("\"verticalAlign\":\"center\""), "the fixture's area type is centred");
|
|
let back = load(text.as_bytes()).unwrap();
|
|
assert_eq!(doc_json(&back), doc_json(&d));
|
|
let area = |d: &Document| {
|
|
let mut found = None;
|
|
d.walk(|n| {
|
|
// The fixture's centred area type, by its text (the fixture has other area type).
|
|
if let NodeKind::Text(t) = &n.kind
|
|
&& matches!(t.kind, vectorcraft_doc::TextKind::Area { .. })
|
|
&& t.runs.iter().map(|r| r.text.as_str()).collect::<String>() == "Centred area type"
|
|
{
|
|
found = Some(t.area.vertical_align);
|
|
}
|
|
});
|
|
found.unwrap()
|
|
};
|
|
assert_eq!(area(&back), vectorcraft_doc::VerticalAlign::Center);
|
|
// Files from before the option open top-aligned.
|
|
// (Every area type the fixture has writes its options, so the key goes from all of them.)
|
|
fn strip(v: &mut Value) {
|
|
match v {
|
|
Value::Object(m) => {
|
|
m.remove("verticalAlign");
|
|
m.values_mut().for_each(strip);
|
|
}
|
|
Value::Array(a) => a.iter_mut().for_each(strip),
|
|
_ => {}
|
|
}
|
|
}
|
|
let mut v = json_of(text.as_bytes());
|
|
strip(&mut v);
|
|
let old = serde_json::to_string(&v).unwrap();
|
|
assert!(!old.contains("verticalAlign"));
|
|
assert_eq!(area(&load(old.as_bytes()).unwrap()), vectorcraft_doc::VerticalAlign::Top);
|
|
}
|
|
|
|
#[test]
|
|
fn compressed_files_load() {
|
|
let d = rich_doc();
|
|
let packed = save_with(&d, &SaveOptions { compress: true, pretty: true, ..SaveOptions::default() }).unwrap();
|
|
assert!(is_compressed(&packed) && sniff(&packed));
|
|
let plain = save(&d, false);
|
|
assert!(packed.len() * 3 < plain.len(), "compressed {} vs {} bytes", packed.len(), plain.len());
|
|
assert_eq!(doc_json(&load(&packed).unwrap()), doc_json(&load(&plain).unwrap()));
|
|
// Compressed files are compact even when pretty was asked for.
|
|
assert!(!flate2_unpack(&packed).contains(&b'\n'));
|
|
// A legacy (v2, pre-rename) file compressed by hand opens too.
|
|
let text = String::from_utf8(plain.clone()).unwrap();
|
|
let legacy = text.replacen(&format!("\"format\":\"vectorcraft\",\"version\":{VERSION}"), "\"format\":\"drawcraft\",\"version\":2", 1);
|
|
assert_ne!(legacy, text);
|
|
let legacy = gzip(legacy.as_bytes());
|
|
assert!(sniff(&legacy));
|
|
assert_eq!(doc_json(&load(&legacy).unwrap()), doc_json(&load(&plain).unwrap()));
|
|
// Other gzip data (an SVGZ) is not a native file.
|
|
assert!(!sniff(&gzip(b"<svg xmlns=\"http://www.w3.org/2000/svg\"/>")));
|
|
assert!(load(&packed[..packed.len() / 2]).is_err(), "a truncated file is an error");
|
|
}
|
|
|
|
fn flate2_unpack(bytes: &[u8]) -> Vec<u8> {
|
|
use std::io::Read as _;
|
|
let mut out = vec![];
|
|
flate2::read::GzDecoder::new(bytes).read_to_end(&mut out).unwrap();
|
|
out
|
|
}
|
|
|
|
#[test]
|
|
fn unused_images_are_pruned() {
|
|
let mut d = Document::new(100.0, 100.0);
|
|
let layer = d.layers[0].id;
|
|
for (k, b) in [("used", &[1u8, 2][..]), ("unused", &[3, 4]), ("in symbol", &[5]), ("in brush", &[6]), ("in mask", &[7])] {
|
|
d.images.insert(k.into(), blob(b));
|
|
}
|
|
let mut n = image_node(&mut d, "used");
|
|
n.extras_mut().mask = Some(Box::new(OpacityMask::new(image_node(&mut d, "in mask"), false)));
|
|
d.insert(Some(layer), 0, n).unwrap();
|
|
let art = image_node(&mut d, "in symbol");
|
|
d.symbols.push(Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
|
// Image objects inside foreign data (a brush library kept as JSON) keep theirs too.
|
|
d.unknown.insert("brushes".into(), json!([{"art": {"type": "image", "key": "in brush"}}]));
|
|
let back = load(&save(&d, true)).unwrap();
|
|
let mut keys: Vec<&str> = back.images.keys().map(String::as_str).collect();
|
|
keys.sort_unstable();
|
|
assert_eq!(keys, ["in brush", "in mask", "in symbol", "used"]);
|
|
assert_eq!(back.images["used"].bytes.as_slice(), &[1, 2]);
|
|
let v = json_of(&save(&d, false));
|
|
assert!(v["images"].get("unused").is_none() && v["document"]["images"].as_object().is_some_and(|m| m.is_empty()));
|
|
}
|
|
|
|
#[test]
|
|
fn keys_from_newer_versions_round_trip() {
|
|
let mut v = json_of(&save(&rich_doc(), false));
|
|
v["document"]["futureFeature"] = json!({"a": [1, 2, 3], "b": "x"});
|
|
v["document"]["futureFlag"] = json!(true);
|
|
let d = load(&serde_json::to_vec(&v).unwrap()).unwrap();
|
|
assert_eq!(d.extra["futureFeature"]["a"], json!([1, 2, 3]));
|
|
assert!(!d.unknown.contains_key("futureFeature"), "kept apart from `unknown`");
|
|
for pretty in [false, true] {
|
|
let again = json_of(&save(&d, pretty));
|
|
assert_eq!(again["document"]["futureFeature"], v["document"]["futureFeature"]);
|
|
assert_eq!(again["document"]["futureFlag"], json!(true));
|
|
}
|
|
check_native_roundtrip(&d).unwrap();
|
|
}
|
|
|
|
/// An opacity-mask editing state in a file drops the editing layer it names only when that is a
|
|
/// top-level layer, as files saved mid-edit have it: a sublayer it names loads and stays.
|
|
#[test]
|
|
fn saved_mask_editing_drops_only_a_top_level_layer() {
|
|
let mut d = Document::new(612.0, 792.0);
|
|
let host = d.layers[0].id;
|
|
let sub = d.add_layer(Some("Sub"));
|
|
d.move_node(sub, Some(host), 0).unwrap();
|
|
let top = d.add_layer(Some("Mask"));
|
|
for (layer, kept) in [(sub, true), (top, false)] {
|
|
let mut v = json_of(&save(&d, false));
|
|
v["document"]["mask_edit"] = json!({"object": host, "layer": layer});
|
|
let back = load(&serde_json::to_vec(&v).unwrap()).unwrap();
|
|
assert_eq!(back.node(layer).is_some(), kept, "layer {layer:?}");
|
|
assert!(back.mask_edit.is_none() && back.node(host).is_some());
|
|
}
|
|
}
|
|
|
|
/// The paragraph composer is saved only when it isn't the default (Every-line) and reads back.
|
|
#[test]
|
|
fn text_composer_round_trips() {
|
|
let d = rich_doc();
|
|
let composers = |d: &Document| {
|
|
let mut v = vec![];
|
|
d.walk(|n| {
|
|
if let NodeKind::Text(t) = &n.kind {
|
|
v.push(t.para.composer);
|
|
}
|
|
});
|
|
v
|
|
};
|
|
// The point type set to Single-line comes first; the fixture's other type keeps the default.
|
|
let single_then_default = |c: &[Composer]| c.len() > 1 && c[0] == Composer::SingleLine && c[1..].iter().all(|c| *c == Composer::EveryLine);
|
|
assert!(single_then_default(&composers(&d)), "{:?}", composers(&d));
|
|
let bytes = save(&d, false);
|
|
assert!(String::from_utf8_lossy(&bytes).contains("\"composer\":\"singleLine\""));
|
|
assert_eq!(composers(&load(&bytes).unwrap()), composers(&d));
|
|
// Files without the key (older files, Every-line text) read as Every-line.
|
|
fn strip(v: &mut Value) {
|
|
match v {
|
|
Value::Object(m) => {
|
|
m.remove("composer");
|
|
m.values_mut().for_each(strip);
|
|
}
|
|
Value::Array(a) => a.iter_mut().for_each(strip),
|
|
_ => {}
|
|
}
|
|
}
|
|
let mut v = json_of(&bytes);
|
|
strip(&mut v);
|
|
let d2 = load(&serde_json::to_vec(&v).unwrap()).unwrap();
|
|
assert!(composers(&d2).iter().all(|c| *c == Composer::EveryLine), "{:?}", composers(&d2));
|
|
assert!(!String::from_utf8_lossy(&save(&d2, false)).contains("composer"));
|
|
}
|
|
|
|
/// Every composer reads back as it was saved (the two Standard ones as their own values), and the
|
|
/// ME&SA names are the ones files have always used: `everyLine` stays the unwritten default and
|
|
/// `singleLine` keeps its spelling, so a document that never picks a Standard composer is unchanged.
|
|
#[test]
|
|
fn all_four_composers_round_trip() {
|
|
use vectorcraft_doc::{TextKind, TextObject};
|
|
let one = |c: Composer| {
|
|
let mut d = Document::new(200.0, 200.0);
|
|
let mut t = TextObject::point(vectorcraft_geom::Point::ZERO, "ક\u{ACD}ક", Default::default());
|
|
t.para.composer = c;
|
|
t.kind = TextKind::Point;
|
|
let id = d.alloc_id();
|
|
d.insert(None, usize::MAX, Node::new(id, NodeKind::Text(Box::new(t)))).unwrap();
|
|
d
|
|
};
|
|
for (c, written) in [
|
|
(Composer::SingleLine, Some("singleLine")),
|
|
(Composer::EveryLine, None),
|
|
(Composer::StandardSingleLine, Some("standardSingleLine")),
|
|
(Composer::StandardEveryLine, Some("standardEveryLine")),
|
|
] {
|
|
let bytes = save(&one(c), false);
|
|
let text = String::from_utf8_lossy(&bytes);
|
|
match written {
|
|
Some(w) => assert!(text.contains(&format!("\"composer\":\"{w}\"")), "{c:?}"),
|
|
None => assert!(!text.contains("\"composer\""), "{c:?}"),
|
|
}
|
|
let back = load(&bytes).unwrap();
|
|
let mut got = None;
|
|
back.walk(|n| {
|
|
if let NodeKind::Text(t) = &n.kind {
|
|
got = Some(t.para.composer);
|
|
}
|
|
});
|
|
assert_eq!(got, Some(c));
|
|
}
|
|
}
|
|
|
|
/// A document as the first version wrote it (format name `drawcraft`, anchors as maps).
|
|
const V1_FILE: &str = r#"{"format":"drawcraft","version":1,"generator":"DrawCraft 0.1.0","document":{"version":1,"title":"Old","artboards":[{"id":1,"name":"Artboard 1","rect":{"x0":0.0,"y0":0.0,"x1":100.0,"y1":80.0}}],
|
|
"layers":[{"id":1,"name":"Layer 1","visible":true,"locked":false,"opacity":1.0,"kind":{"type":"layer","children":[
|
|
{"id":2,"visible":true,"locked":false,"opacity":1.0,"kind":{"type":"path","path":{"subpaths":[{"anchors":[
|
|
{"p":{"x":10.0,"y":10.0},"in":{"x":10.0,"y":10.0},"out":{"x":30.0,"y":0.0},"kind":"Smooth"},
|
|
{"p":{"x":50.0,"y":40.0},"in":{"x":50.0,"y":40.0},"out":{"x":50.0,"y":40.0},"kind":"Corner"}],"closed":false}]},"rule":"NonZero","clipping":false,"guide":false}}],"color":{"Preset":0}}}],
|
|
"next_id":3,"unknown":{"colorProfiles":{"rgb":"sRGB IEC61966-2.1","cmyk":null}}},"images":{}}"#;
|
|
|
|
#[test]
|
|
fn version_1_files_load() {
|
|
let d = load(V1_FILE.as_bytes()).unwrap();
|
|
assert_eq!(d.title, "Old");
|
|
let NodeKind::Path { path, .. } = &d.layers[0].children().unwrap()[0].kind else { panic!("not a path") };
|
|
let a = &path.subpaths[0].anchors;
|
|
assert_eq!((a[0].h_out.x, a[0].h_out.y, a[1].p.x), (30.0, 0.0, 50.0));
|
|
// Profiles migrate out of `unknown`.
|
|
assert_eq!(d.color_profiles, ColorProfiles { rgb: Some("sRGB IEC61966-2.1".into()), cmyk: None });
|
|
assert!(d.unknown.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn saves_down_to_older_versions() {
|
|
let mut d = rich_doc();
|
|
d.color_profiles = ColorProfiles { rgb: None, cmyk: Some("Coated".into()) };
|
|
let mut current = doc_json(&load(&save(&d, false)).unwrap());
|
|
// Older versions get live stars as plain paths.
|
|
without_live_stars(&mut current);
|
|
for version in [1, 2] {
|
|
let bytes = save_with(&d, &opts(version, false)).unwrap();
|
|
let v = json_of(&bytes);
|
|
assert_eq!((v["format"].as_str(), v["version"].as_u64()), (Some("drawcraft"), Some(version as u64)), "older apps know the old name");
|
|
// Profiles where those versions keep them.
|
|
assert_eq!(v["document"]["unknown"]["colorProfiles"], json!({"rgb": null, "cmyk": "Coated"}));
|
|
assert!(v["document"].get("color_profiles").is_none());
|
|
let back = load(&bytes).unwrap();
|
|
assert!(json_approx_eq(&doc_json(&back), ¤t, 1e-12), "v{version} loses nothing it can hold");
|
|
let text = String::from_utf8(bytes).unwrap();
|
|
// v1 anchors are maps with every member; v2 anchors are compact.
|
|
assert_eq!(text.contains("\"p\":{\"x\""), version == 1);
|
|
assert_eq!(text.contains("\"p\":["), version == 2);
|
|
}
|
|
let v3 = json_of(&save(&d, false));
|
|
assert_eq!((v3["format"].as_str(), v3["version"].as_u64()), (Some("vectorcraft"), Some(VERSION as u64)));
|
|
assert_eq!(v3["document"]["color_profiles"], json!({"cmyk": "Coated"}));
|
|
assert!(v3["document"]["unknown"].get("colorProfiles").is_none());
|
|
// Out of range, and compression older apps can't read.
|
|
assert!(matches!(save_with(&d, &opts(0, false)), Err(FormatError::BadVersion(0))));
|
|
assert!(matches!(save_with(&d, &opts(VERSION + 1, false)), Err(FormatError::BadVersion(_))));
|
|
assert!(matches!(save_with(&d, &opts(2, true)), Err(FormatError::CompressedTooOld(2))));
|
|
}
|
|
|
|
/// Versions before [`VERSION`] don't know live stars: a save for them writes the star's outline as
|
|
/// a plain path, which those versions read; the current version keeps the star live.
|
|
#[test]
|
|
fn older_versions_get_a_live_star_as_its_path() {
|
|
let mut d = Document::new(100.0, 100.0);
|
|
let star = vectorcraft_doc::LiveShape::Star {
|
|
r1: 40.0,
|
|
r2: 20.0,
|
|
points: 5,
|
|
xf: Affine::translate((50.0, 50.0)),
|
|
radii: vec![],
|
|
kinds: vec![],
|
|
radii_relative: false,
|
|
};
|
|
let id = d.alloc_id();
|
|
let mut node = Node::path(id, star.to_path(), Default::default());
|
|
if let NodeKind::Path { live, .. } = &mut node.kind {
|
|
*live = Some(Box::new(star.clone()));
|
|
}
|
|
let layer = d.layers[0].id;
|
|
d.insert(Some(layer), usize::MAX, node).unwrap();
|
|
for version in 1..VERSION {
|
|
let bytes = save_with(&d, &opts(version, false)).unwrap();
|
|
let text = String::from_utf8(bytes.clone()).unwrap();
|
|
assert!(!text.contains("\"star\""), "v{version} writes no live star");
|
|
let back = load(&bytes).unwrap();
|
|
let mut anchors = None;
|
|
back.walk(|n| {
|
|
if let NodeKind::Path { path, live, .. } = &n.kind {
|
|
assert!(live.is_none(), "v{version}");
|
|
anchors = Some(path.anchor_count());
|
|
}
|
|
});
|
|
assert_eq!(anchors, Some(10), "v{version} keeps the star's outline");
|
|
}
|
|
let back = load(&save(&d, false)).unwrap();
|
|
let mut live = None;
|
|
back.walk(|n| live = live.clone().or(n.live_shape().cloned()));
|
|
assert_eq!(live, Some(star));
|
|
}
|
|
|
|
#[test]
|
|
fn preview_is_embedded_and_read_back() {
|
|
let d = Document::new(10.0, 10.0);
|
|
assert_eq!(preview(&save(&d, false)), None);
|
|
let png = vec![0x89, b'P', b'N', b'G', 1, 2, 3];
|
|
for compress in [false, true] {
|
|
let bytes = save_with(&d, &SaveOptions { compress, preview: Some(png.clone()), ..SaveOptions::default() }).unwrap();
|
|
assert_eq!(preview(&bytes), Some(png.clone()));
|
|
assert!(load(&bytes).is_ok());
|
|
}
|
|
assert_eq!(preview(b"not a file"), None);
|
|
}
|
|
|
|
#[test]
|
|
fn pretty_files_are_indented_like_serde_json() {
|
|
let d = rich_doc();
|
|
let bytes = save(&d, true);
|
|
let text = String::from_utf8(bytes.clone()).unwrap();
|
|
assert!(text.starts_with("{\n \"format\": \"vectorcraft\",\n \"version\": "));
|
|
assert!(text.contains("\n \"document\": {\n \""));
|
|
assert!(text.ends_with("\n}"));
|
|
// The same JSON as the compact file.
|
|
assert_eq!(json_of(&bytes), json_of(&save(&d, false)));
|
|
}
|
|
|
|
/// #451: a guide's artboard is written only for artboard guides, so canvas guides save as they
|
|
/// did, and a file without it (an older version's) opens with canvas guides.
|
|
#[test]
|
|
fn artboard_guides_round_trip_and_canvas_guides_save_as_before() {
|
|
use vectorcraft_doc::Guide;
|
|
let mut d = Document::new(100.0, 100.0);
|
|
let id = d.artboards[0].id;
|
|
d.add_ruler_guide(&Guide::new(true, 10.0));
|
|
d.add_ruler_guide(&Guide { artboard: Some(id), ..Guide::new(false, 20.0) });
|
|
let mut v = json_of(&save(&d, false));
|
|
let lines: Vec<_> = v["document"]["guides"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|g| json!({"vertical": g["vertical"], "pos": g["pos"], "artboard": g.get("artboard")}))
|
|
.collect();
|
|
assert_eq!(lines, [json!({"vertical": true, "pos": 10.0, "artboard": null}), json!({"vertical": false, "pos": 20.0, "artboard": id})]);
|
|
assert_eq!(load(&save(&d, false)).unwrap().ruler_guides(), d.ruler_guides());
|
|
v["document"]["guides"] = json!([{"vertical": false, "pos": 20.0}]);
|
|
let old = load(&serde_json::to_vec(&v).unwrap()).unwrap();
|
|
let old_lines: Vec<_> = old.ruler_guides().into_iter().map(|g| (g.vertical, g.pos, g.artboard)).collect();
|
|
assert_eq!(old_lines, [(false, 20.0, None)]);
|
|
}
|
|
|
|
#[test]
|
|
fn inline_graphics_in_text_round_trip() {
|
|
let d = rich_doc();
|
|
let inline = |d: &Document| {
|
|
let mut found = vec![];
|
|
d.walk(|n| {
|
|
if let NodeKind::Text(t) = &n.kind {
|
|
found.extend(t.runs.iter().filter_map(|r| r.inline.clone().map(|a| (r.text.clone(), a.symbol, a.scale, a.baseline_shift))));
|
|
}
|
|
});
|
|
found
|
|
};
|
|
let want = inline(&d);
|
|
assert_eq!(want, vec![("\u{FFFC}".to_string(), "Dot".to_string(), 1.0, 0.0)]);
|
|
let bytes = save(&d, false);
|
|
let saved = json_of(&bytes).to_string();
|
|
assert!(saved.contains("\"inline\":{") && saved.contains("\"symbol\":\"Dot\""), "{saved}");
|
|
let back = load(&bytes).unwrap();
|
|
assert_eq!(inline(&back), want);
|
|
assert_eq!(doc_json(&back), doc_json(&d));
|
|
check_native_roundtrip(&d).unwrap();
|
|
}
|