mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 14:14:50 +00:00
Save: read the images and placed documents in stored brush art
A native save now sets the stored brushes' art on the document while it tells linked images from embedded ones and while it converts placed documents for older versions, then writes the changed brushes back. - A linked image shown only in a brush's art is saved as its preview, or with Include Linked Files as its file's bytes; an image key that the layers or a brush's art shows embedded keeps its full bytes. - Saving for an older version replaces placed documents in a brush's art with groups of their art and its resources, before a precision rounds it, and the precision preview and PDF use the same conversion. - Data Recovery copies count the images in brush art the same way. Docs: docs/target-app-parity.md, docs/mcp.md.
This commit is contained in:
@@ -236,15 +236,13 @@ pub(super) fn encode_to(cmd: &str, f: &Format, doc: &Document, p: &Value, w: imp
|
||||
}
|
||||
|
||||
/// `doc` with its path geometry rounded to `decimals` places, as [`vectorcraft_format::save_to`]
|
||||
/// writes it for `version`: placed documents become groups of their art for older versions.
|
||||
/// writes it for `version`: placed documents, those in the stored brushes' art included, become
|
||||
/// groups of their art for older versions.
|
||||
fn rounded(doc: &Document, version: u32, decimals: u8) -> Document {
|
||||
let mut d = if version < vectorcraft_format::VERSION && doc.has_placed() {
|
||||
let mut d = doc.with_placed_art().into_owned();
|
||||
d.placed_as_groups();
|
||||
d
|
||||
} else {
|
||||
doc.clone()
|
||||
};
|
||||
let mut d = doc.clone();
|
||||
if version < vectorcraft_format::VERSION {
|
||||
vectorcraft_format::placed_as_groups(&mut d);
|
||||
}
|
||||
d.round_geometry(decimals.min(vectorcraft_format::MAX_PRECISION));
|
||||
d
|
||||
}
|
||||
|
||||
@@ -999,8 +999,9 @@ impl RecoveryJob {
|
||||
pub fn write(&self) -> std::result::Result<Value, String> {
|
||||
let store = self.store.as_ref();
|
||||
let doc = self.save.doc();
|
||||
// A linked image keeps its preview, as a save writes it.
|
||||
let linked = doc.linked_only_images();
|
||||
// A linked image keeps its preview, as a save writes it (the images in the stored brushes'
|
||||
// art counted too).
|
||||
let linked = super::links::with_brush_art(doc).linked_only_images();
|
||||
let mut lean = Document::clone(doc);
|
||||
let blobs = std::mem::take(&mut lean.images);
|
||||
let enc = self.save.encode_doc(&lean).map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -1003,6 +1003,78 @@ fn saved_for_older_apps_with_a_precision_the_placed_art_is_rounded_too() {
|
||||
assert_ne!(preview, crate::cmd::fileio::preview_png(&s.doc().unwrap().doc).unwrap());
|
||||
}
|
||||
|
||||
/// Saved for an older version, a placed document held only in stored brushes' art (a scatter
|
||||
/// brush's art and a pattern brush's tile) is saved as the art it shows, with the resources that art
|
||||
/// uses, and a precision save's preview shows that art as the file holds it, rounded. The current
|
||||
/// version keeps it placed.
|
||||
#[test]
|
||||
fn saved_for_older_apps_placed_documents_in_brush_art_become_their_art() {
|
||||
let dir = Folder::new("placed-brush-older");
|
||||
let src = dir.file("badge.vectorcraft");
|
||||
// A symbol `Star` and a triangle off the whole-point grid.
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let layer = d.layers[0].id;
|
||||
let star = Node::path(vectorcraft_doc::NodeId(900), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), look(BLUE));
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Star".into(), art: std::sync::Arc::new(star) });
|
||||
let id = d.alloc_id();
|
||||
d.insert(Some(layer), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Star".into(), xf: vectorcraft_geom::Affine::IDENTITY })).unwrap();
|
||||
let points = [(3.4876474632797176, 10.5), (96.5, 2.4876), (40.4876, 97.5)].map(|(x, y)| vectorcraft_geom::Point::new(x, y));
|
||||
let path = vectorcraft_geom::PathData::single(vectorcraft_geom::path::SubPath::polyline(&points, true));
|
||||
let id = d.alloc_id();
|
||||
d.insert(Some(layer), 1, Node::path(id, path, look(RED))).unwrap();
|
||||
write(&src, &vectorcraft_format::save_file(&d));
|
||||
let mut s = session();
|
||||
let id = place(&mut s, &src);
|
||||
let art = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
s.execute("edit.clear", &json!({"ids": [id.0]})).unwrap();
|
||||
{
|
||||
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
let scatter = vectorcraft_brush::Scatter { art: art.clone(), ..Default::default() };
|
||||
let pattern = vectorcraft_brush::PatternBrush { side: art, ..Default::default() };
|
||||
vectorcraft_brush::store(
|
||||
d,
|
||||
&[
|
||||
vectorcraft_brush::Brush { name: "Badge Dots".into(), kind: vectorcraft_brush::BrushKind::Scatter(scatter) },
|
||||
vectorcraft_brush::Brush { name: "Badge Band".into(), kind: vectorcraft_brush::BrushKind::Pattern(pattern) },
|
||||
],
|
||||
);
|
||||
assert!(!d.has_placed(), "only the brushes hold it");
|
||||
}
|
||||
let line = s.execute("shape.line", &json!({"x1": 20, "y1": 150, "x2": 380, "y2": 150})).unwrap()["id"].clone();
|
||||
s.execute("brush.apply", &json!({"name": "Badge Dots", "ids": [line]})).unwrap();
|
||||
// The placed documents in each brush's art.
|
||||
let placed_in_brushes = |d: &Document| {
|
||||
let mut n = 0;
|
||||
for b in vectorcraft_brush::library(d) {
|
||||
for art in b.art() {
|
||||
art.walk(&mut |c| n += usize::from(matches!(c.kind, NodeKind::PlacedDocument(_))));
|
||||
}
|
||||
}
|
||||
n
|
||||
};
|
||||
let prefix = vectorcraft_doc::placed_document::RESOURCE_PREFIX;
|
||||
let older = vectorcraft_format::VERSION - 1;
|
||||
let old =
|
||||
vectorcraft_format::load(&decode(&s.execute("document.serialize", &json!({"format": "vectorcraft", "version": older})).unwrap())).unwrap();
|
||||
assert_eq!(placed_in_brushes(&old), 0, "{}", old.unknown[vectorcraft_brush::DOC_KEY]);
|
||||
assert_eq!(vectorcraft_brush::library(&old).len(), 2);
|
||||
for b in vectorcraft_brush::library(&old) {
|
||||
assert!(matches!(b.art()[0].kind, NodeKind::Group { .. }), "{}: {}", b.name, b.art()[0].kind_label());
|
||||
}
|
||||
assert!(old.symbols.iter().any(|s| s.name.starts_with(prefix)), "the art's symbol is saved with it");
|
||||
let current = vectorcraft_format::load(&decode(&s.execute("document.serialize", &json!({"format": "vectorcraft"})).unwrap())).unwrap();
|
||||
assert_eq!(placed_in_brushes(¤t), 2, "the current version keeps them placed");
|
||||
assert!(current.symbols.iter().all(|s| !s.name.starts_with(prefix)));
|
||||
// With a precision, the preview shows the brushes' art as the file holds it, rounded.
|
||||
let file = decode(&s.execute("file.saveCopy", &json!({"version": older, "precision": 0, "preview": true})).unwrap());
|
||||
let saved = vectorcraft_format::load(&file).unwrap();
|
||||
assert_eq!(placed_in_brushes(&saved), 0);
|
||||
let preview = vectorcraft_format::preview(&file);
|
||||
assert!(preview.is_some());
|
||||
assert_eq!(preview, crate::cmd::fileio::preview_png(&saved).unwrap());
|
||||
assert_ne!(preview, crate::cmd::fileio::preview_png(&s.doc().unwrap().doc).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn break_link_gives_the_editable_copy_placing_without_link_gives() {
|
||||
let dir = Folder::new("placed-break");
|
||||
|
||||
@@ -1532,6 +1532,47 @@ fn images_sharing_an_entry_are_written_once() {
|
||||
assert_eq!(store.images_written().len(), 1, "restored into this session's area once");
|
||||
}
|
||||
|
||||
/// A linked image shown only in a stored brush's art is kept as its preview, as a save writes it;
|
||||
/// an image key that a brush's art embeds keeps its full bytes though the layers show it linked.
|
||||
#[test]
|
||||
fn recovery_brush_links_keep_proxy_and_embedded_full_bytes() {
|
||||
let (full, proxy) = (vec![1u8; 40], vec![2u8; 10]);
|
||||
for brush_linked in [true, false] {
|
||||
let store = Arc::new(MemoryStore::default());
|
||||
let mut s = session_in(&store);
|
||||
{
|
||||
let d = Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
let blob =
|
||||
vectorcraft_doc::ImageBlob { proxy: Some(Arc::new(proxy.clone())), ..vectorcraft_doc::ImageBlob::new("image/jpeg", full.clone()) };
|
||||
d.images.insert("k".into(), blob);
|
||||
let image = |link: Option<vectorcraft_doc::LinkInfo>| {
|
||||
let im = vectorcraft_doc::ImageObject {
|
||||
key: "k".into(),
|
||||
width: 4,
|
||||
height: 4,
|
||||
xf: vectorcraft_geom::Affine::IDENTITY,
|
||||
link,
|
||||
placement: Default::default(),
|
||||
pixels: None,
|
||||
};
|
||||
vectorcraft_doc::Node::new(NodeId(0), vectorcraft_doc::NodeKind::Image(Box::new(im)))
|
||||
};
|
||||
let link = Some(vectorcraft_doc::LinkInfo::new("/work/k.jpg"));
|
||||
if !brush_linked {
|
||||
let (mut n, layer) = (image(link.clone()), d.layers[0].id);
|
||||
n.id = d.alloc_id();
|
||||
d.insert(Some(layer), 0, n).unwrap();
|
||||
}
|
||||
let art = image(if brush_linked { link } else { None });
|
||||
let scatter = vectorcraft_brush::Scatter { art, ..Default::default() };
|
||||
vectorcraft_brush::store(d, &[vectorcraft_brush::Brush { name: "Dots".into(), kind: vectorcraft_brush::BrushKind::Scatter(scatter) }]);
|
||||
}
|
||||
let r = s.execute("file.recovery.save", &json!({})).unwrap();
|
||||
let want = if brush_linked { proxy.len() } else { full.len() };
|
||||
assert_eq!(r["saved"][0]["imageBytes"], want, "linked in the brush: {brush_linked}: {r}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copies_from_before_images_were_kept_apart_still_restore() {
|
||||
let store = Arc::new(MemoryStore::default());
|
||||
|
||||
Reference in New Issue
Block a user