mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 11:05:03 +00:00
Distort: keep blend steps, saved corners, previews and deep masks right
- A blend step whose interpolated corners and nearer key's corners both fail the check is placed as the nearer key is. - Corners an image can't take are dropped when a file is read, so a later transform does not make them its distortion. - The .ai editing data writes a distorted image at any nesting depth, as it does a gradient mesh, so an opacity mask at the limit keeps it. - A linked image's preview keeps the clipping path its file keeps, so a document reopened from its preview still knows the file's clip group and Update Links replaces it instead of nesting a new one. - Docs: `corners` in the plugin API covers only corners that are not a parallelogram; Relink's warning for distorted images needs `preserve`.
This commit is contained in:
@@ -39,7 +39,7 @@ pub mod jxlread;
|
||||
mod legacy_ai;
|
||||
mod load;
|
||||
mod mesh3d;
|
||||
pub(crate) use load::{MAX_RASTER_ALLOC, file_clipped, image_clip, size_note};
|
||||
pub(crate) use load::{MAX_RASTER_ALLOC, file_clipped, image_clip, preview_with_clip, size_note};
|
||||
mod heifread;
|
||||
mod hpgl;
|
||||
mod jp2read;
|
||||
|
||||
@@ -1214,6 +1214,20 @@ pub(crate) fn image_clip(blob: &ImageBlob) -> Option<vectorcraft_geom::PathData>
|
||||
vectorcraft_geom::path::with_anchor_budget(vectorcraft_geom::path::MAX_READ_ANCHORS, || serde_json::from_slice(json).ok())
|
||||
}
|
||||
|
||||
/// `blob` with its preview keeping the clipping path its PNG or TIFF keeps ([`image_clip`]), in a
|
||||
/// text chunk of the preview PNG as [`png_with_clip`] writes it: a document saved without its
|
||||
/// linked files holds the preview alone, and the Links commands then still know a clip group of
|
||||
/// that path as the file's. Unchanged without a preview or a clipping path.
|
||||
pub(crate) fn preview_with_clip(mut blob: ImageBlob) -> ImageBlob {
|
||||
let Some(proxy) = blob.proxy.clone().filter(|p| !std::sync::Arc::ptr_eq(p, &blob.bytes) && png_clip(p).is_none()) else { return blob };
|
||||
let Some(json) = image_clip(&blob).and_then(|clip| serde_json::to_string(&clip).ok()).filter(|j| j.len() <= MAX_PNG_CLIP) else {
|
||||
return blob;
|
||||
};
|
||||
let png = vectorcraft_render::encode::png::with_text(proxy.to_vec(), &[(CLIP_KEYWORD, json.into())]);
|
||||
blob.proxy = Some(std::sync::Arc::new(png));
|
||||
blob
|
||||
}
|
||||
|
||||
/// An image as a document of its physical size at the resolution it declares (as Place sizes it;
|
||||
/// 72 ppi, 1 px = 1 pt, when it declares none), the image named after the file.
|
||||
pub(super) fn raster_doc(name: &str, bytes: &[u8]) -> Result<Document> {
|
||||
|
||||
@@ -684,15 +684,68 @@ fn update_links_follows_the_clipping_path_of_a_distorted_psd() {
|
||||
assert_eq!(node(&s).image_corners().unwrap(), corners);
|
||||
}
|
||||
|
||||
/// A document saved without its linked files keeps a linked PSD file's image as a preview, smaller
|
||||
/// than the file. Reopened after the file's clipping path changed, Update Links still knows the
|
||||
/// clip group of the old clipping path as the file's and gives it the new one, for an image changed
|
||||
/// by Perspective Distort or Free Distort and for one that is not.
|
||||
#[test]
|
||||
fn update_links_after_reopening_from_the_preview_replaces_the_files_clipping_path() {
|
||||
let big = |resources: &[u8]| psd(1, 3, (400, 200), 8, 3, &[], resources, &[], &raw(&[&[255; 80000], &[0; 80000], &[0; 80000]]));
|
||||
for distorted in [false, true] {
|
||||
let dir = vectorcraft_testkit::TempDir::new(if distorted { "psd-preview-distorted" } else { "psd-preview" });
|
||||
let cut = write_file(&dir, "cut.psd", &big(&triangle_resources()));
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 200})).unwrap();
|
||||
let r = s.execute("file.place", &json!({"path": cut, "at": [200, 100]})).unwrap();
|
||||
let (group, image) = (NodeId(r["ids"][0].as_u64().unwrap()), NodeId(r["image"].as_u64().unwrap()));
|
||||
if distorted {
|
||||
s.execute("select.set", &json!({"ids": [group.0]})).unwrap();
|
||||
s.execute("object.distort", &json!({"corners": [[0, 0], [400, 30], [360, 200], [20, 180]]})).unwrap();
|
||||
}
|
||||
let node = |s: &Session| s.doc().unwrap().doc.node(image).unwrap().clone();
|
||||
let corners = node(&s).image_corners().unwrap();
|
||||
let doc_path = dir.join("poster.vectorcraft").to_string_lossy().into_owned();
|
||||
s.execute("document.save", &json!({"path": doc_path})).unwrap();
|
||||
std::fs::write(&cut, big(&square_resources())).unwrap();
|
||||
s.execute("document.open", &json!({"path": doc_path})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(im) = &d.node(image).unwrap().kind else { panic!("an image") };
|
||||
assert!(d.images[&im.key].is_proxy(), "the saved preview shows");
|
||||
let r = s.execute("links.update", &json!({"ids": [image.0]})).unwrap();
|
||||
assert_eq!(r["updated"], json!([image.0]), "{r}");
|
||||
let map = node(&s).image_map().unwrap();
|
||||
let squared: Vec<[f64; 2]> = [[100.0, 50.0], [300.0, 50.0], [300.0, 150.0], [100.0, 150.0]]
|
||||
.iter()
|
||||
.map(|[x, y]| map.apply(vectorcraft_geom::Point::new(*x, *y)).unwrap())
|
||||
.map(|p| [p.x, p.y])
|
||||
.collect();
|
||||
let (anchors, even_odd, held) = clip_group(&s.doc().unwrap().doc, group);
|
||||
let near =
|
||||
anchors.len() == squared.len() && anchors.iter().zip(&squared).all(|(p, q)| (p[0] - q[0]).abs() < 1e-6 && (p[1] - q[1]).abs() < 1e-6);
|
||||
assert!(near && even_odd && held == image, "distorted: {distorted}: {anchors:?} vs {squared:?}");
|
||||
assert_eq!(node(&s).image_corners().unwrap(), corners);
|
||||
}
|
||||
}
|
||||
|
||||
/// Unembed of a distorted image from a PSD file, with a path and with a folder, keeps the clip
|
||||
/// group of the clipping path that file names, drawn through its corners, and so does relinking it
|
||||
/// to the file written.
|
||||
/// to the file written. Update Links reads the clipping path the file on disk keeps then, and all
|
||||
/// four corners stay where they were.
|
||||
#[test]
|
||||
fn unembed_keeps_the_clipping_path_of_a_distorted_psd() {
|
||||
let dir = vectorcraft_testkit::TempDir::new("psd-unembed-distorted-clip");
|
||||
// An RGB file is kept as a PNG, a CMYK one as a TIFF.
|
||||
let cmyk = psd(1, 4, (100, 50), 8, 4, &[], &triangle_resources(), &[], &raw(&[&[0; 5000], &[255; 5000], &[255; 5000], &[0; 5000]]));
|
||||
let files = [("rgb", red_psd(&triangle_resources())), ("cmyk", cmyk)];
|
||||
let cmyk = |resources: &[u8]| psd(1, 4, (100, 50), 8, 4, &[], resources, &[], &raw(&[&[0; 5000], &[255; 5000], &[255; 5000], &[0; 5000]]));
|
||||
let files = [("rgb", red_psd(&triangle_resources())), ("cmyk", cmyk(&triangle_resources()))];
|
||||
// The file Unembed writes, as stored, for the same kind of file clipped to the square.
|
||||
let squared_file = |kind: &str| {
|
||||
let file = if kind == "rgb" { red_psd(&square_resources()) } else { cmyk(&square_resources()) };
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 100, "height": 50})).unwrap();
|
||||
let r = s.execute("file.place", &json!({"name": "square.psd", "dataBase64": vectorcraft_format::base64_encode(&file)})).unwrap();
|
||||
let r = s.execute("links.unembed", &json!({"id": r["image"]})).unwrap();
|
||||
vectorcraft_format::base64_decode(r["dataBase64"].as_str().unwrap()).unwrap()
|
||||
};
|
||||
for ((kind, file), folder) in files.iter().flat_map(|f| [(f, false), (f, true)]) {
|
||||
let data = vectorcraft_format::base64_encode(file);
|
||||
let mut s = Session::new();
|
||||
@@ -702,6 +755,7 @@ fn unembed_keeps_the_clipping_path_of_a_distorted_psd() {
|
||||
s.execute("select.set", &json!({"ids": [group.0]})).unwrap();
|
||||
s.execute("object.distort", &json!({"corners": [[0, 0], [100, 8], [90, 50], [6, 44]]})).unwrap();
|
||||
let before = clip_group(&s.doc().unwrap().doc, group);
|
||||
let corners = s.doc().unwrap().doc.node(image).unwrap().image_corners().unwrap();
|
||||
// Without an extension the image is written in the format it is stored in.
|
||||
let path = if folder {
|
||||
let out = dir.join(format!("folder-{kind}"));
|
||||
@@ -715,22 +769,31 @@ fn unembed_keeps_the_clipping_path_of_a_distorted_psd() {
|
||||
let ext = if *kind == "rgb" { ["png"].as_slice() } else { ["tif", "tiff"].as_slice() };
|
||||
assert!(ext.contains(&super::extension(&path).as_str()), "{kind}: {path}");
|
||||
for step in ["unembed", "relink", "update"] {
|
||||
let mut want = before.0.clone();
|
||||
if step == "relink" {
|
||||
s.execute("links.relink", &json!({"ids": [image.0], "path": path})).unwrap();
|
||||
}
|
||||
if step == "update" {
|
||||
// The file on disk now keeps the square: Update Links reads it from there.
|
||||
std::fs::write(&path, squared_file(kind)).unwrap();
|
||||
let r = s.execute("links.update", &json!({"ids": [image.0]})).unwrap();
|
||||
assert_eq!(r["updated"], json!([image.0]), "{kind}: {r}");
|
||||
let map = s.doc().unwrap().doc.node(image).unwrap().image_map().unwrap();
|
||||
want = [[25.0, 12.5], [75.0, 12.5], [75.0, 37.5], [25.0, 37.5]]
|
||||
.iter()
|
||||
.map(|[x, y]| map.apply(vectorcraft_geom::Point::new(*x, *y)).unwrap())
|
||||
.map(|p| [p.x, p.y])
|
||||
.collect();
|
||||
}
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(im) = &d.node(image).unwrap().kind else { panic!("an image") };
|
||||
assert!(im.link.is_some(), "linked to the file written");
|
||||
assert!(d.node(image).unwrap().is_distorted_image());
|
||||
// The clipping path is drawn through the corners again, within rounding.
|
||||
assert_eq!(d.node(image).unwrap().image_corners().unwrap(), corners, "{kind}, {step}, folder: {folder}");
|
||||
// The clipping path is drawn through the corners, within rounding.
|
||||
let (anchors, even_odd, held) = clip_group(d, group);
|
||||
let near = anchors.len() == before.0.len()
|
||||
&& anchors.iter().zip(&before.0).all(|(p, q)| (p[0] - q[0]).abs() < 1e-6 && (p[1] - q[1]).abs() < 1e-6);
|
||||
assert!(near && (even_odd, held) == (before.1, before.2), "{kind}, {step}, folder: {folder}: {anchors:?} vs {:?}", before.0);
|
||||
let near =
|
||||
anchors.len() == want.len() && anchors.iter().zip(&want).all(|(p, q)| (p[0] - q[0]).abs() < 1e-6 && (p[1] - q[1]).abs() < 1e-6);
|
||||
assert!(near && (even_odd, held) == (before.1, before.2), "{kind}, {step}, folder: {folder}: {anchors:?} vs {want:?}");
|
||||
}
|
||||
}
|
||||
// A path that converts the image writes a file without the clipping path: the clip group
|
||||
|
||||
@@ -72,7 +72,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Relink",
|
||||
[],
|
||||
None,
|
||||
"{ids?: [image or placed document ids] (default: the selected ones; with folder and nothing selected, every missing link), path | folder, scope?: selected (default: ids) | all (ids and every other object linked to the same file as one of them, in the layers, opacity-mask art, symbols, graph designs, patterns and the art of stored brushes) | missingInFolder (with path: ids, then every other missing link whose file name is in path's folder, each to the file of its name there; names not there are in notFound; with copy, only images are looked for) | each (with paths), paths?: [{ids, path}] (with scope each, in place of ids, path and folder: each link to its own file; an id in two entries is an error), copy?: false (with path or paths, for images of a saved document: copy the file into the document's folder and link to the copy; the names Name.png, Name 2.png … Name 99.png are tried in turn until one is free, which gets the copy, or is a file with the same contents, which is used; an error for a DCS file that reads plate files of its own), preserve?: transforms|bounds|fileDimensions|fit|fill (how the new file takes each image's place this time, as links.placementOptions describes; default: each image's Placement Options; fit keeps the new file's aspect ratio inside the old bounds: Replace)} link images (or placed documents, each showing the same artboard or page of the new file) to the file at path, or each to the file of its link's name in folder; embedded images become linked; each keeps its bounds. An image changed by Perspective Distort or Free Distort keeps its corners, with the new file's pixels between them, whatever preserve or its Placement Options say, and its clipping is handled as links.update handles it. One undo step → {relinked: [ids], notFound: [file names], copied?: [{from, to}], kept?: [{from, to}] (with copy; to is the file name alone when the document's folder is outside the automation read roots), warnings?: (as links.update's, and one naming the distorted images that preserve was not applied to)}",
|
||||
"{ids?: [image or placed document ids] (default: the selected ones; with folder and nothing selected, every missing link), path | folder, scope?: selected (default: ids) | all (ids and every other object linked to the same file as one of them, in the layers, opacity-mask art, symbols, graph designs, patterns and the art of stored brushes) | missingInFolder (with path: ids, then every other missing link whose file name is in path's folder, each to the file of its name there; names not there are in notFound; with copy, only images are looked for) | each (with paths), paths?: [{ids, path}] (with scope each, in place of ids, path and folder: each link to its own file; an id in two entries is an error), copy?: false (with path or paths, for images of a saved document: copy the file into the document's folder and link to the copy; the names Name.png, Name 2.png … Name 99.png are tried in turn until one is free, which gets the copy, or is a file with the same contents, which is used; an error for a DCS file that reads plate files of its own), preserve?: transforms|bounds|fileDimensions|fit|fill (how the new file takes each image's place this time, as links.placementOptions describes; default: each image's Placement Options; fit keeps the new file's aspect ratio inside the old bounds: Replace)} link images (or placed documents, each showing the same artboard or page of the new file) to the file at path, or each to the file of its link's name in folder; embedded images become linked; each keeps its bounds. An image changed by Perspective Distort or Free Distort keeps its corners, with the new file's pixels between them, whatever preserve or its Placement Options say, and its clipping is handled as links.update handles it. One undo step → {relinked: [ids], notFound: [file names], copied?: [{from, to}], kept?: [{from, to}] (with copy; to is the file name alone when the document's folder is outside the automation read roots), warnings?: (as links.update's, and, when preserve is given, one naming the distorted images it was not applied to)}",
|
||||
has_doc,
|
||||
relink
|
||||
),
|
||||
@@ -609,13 +609,14 @@ pub(crate) fn kept_unread(bytes: &[u8]) -> bool {
|
||||
/// `blob`, an image [`fileio::raster_image`] read from a file, ready to keep. When `unread` (the
|
||||
/// file is one [`kept_unread`] names), its pixels are decoded once to check that they can be read,
|
||||
/// and a `linked` image's preview is made from that decode ([`ImageBlob::checked_with_proxy`]);
|
||||
/// otherwise a `linked` image gets its preview ([`ImageBlob::with_proxy`]). An error when the
|
||||
/// pixels can't be read.
|
||||
/// otherwise a `linked` image gets its preview ([`ImageBlob::with_proxy`]). The preview keeps the
|
||||
/// clipping path the image keeps ([`fileio::preview_with_clip`]). An error when the pixels can't
|
||||
/// be read.
|
||||
pub(crate) fn readable(blob: ImageBlob, unread: bool, linked: bool) -> Result<ImageBlob> {
|
||||
if !unread {
|
||||
return Ok(if linked { blob.with_proxy() } else { blob });
|
||||
return Ok(if linked { fileio::preview_with_clip(blob.with_proxy()) } else { blob });
|
||||
}
|
||||
blob.checked_with_proxy(linked).ok_or_else(|| EngineError::Other("the image's pixels can't be read".into()))
|
||||
blob.checked_with_proxy(linked).map(fileio::preview_with_clip).ok_or_else(|| EngineError::Other("the image's pixels can't be read".into()))
|
||||
}
|
||||
|
||||
/// [`readable`], and the pixels it decoded are kept in the image store the renderers share
|
||||
@@ -626,6 +627,7 @@ pub(crate) fn readable_and_kept(blob: ImageBlob, unread: bool, linked: bool) ->
|
||||
return readable(blob, unread, linked);
|
||||
}
|
||||
let (blob, pixels) = blob.checked_with_pixels(linked).ok_or_else(|| EngineError::Other("the image's pixels can't be read".into()))?;
|
||||
let blob = fileio::preview_with_clip(blob);
|
||||
if let Some(pixels) = pixels {
|
||||
vectorcraft_render::keep_decoded_image(&blob, pixels);
|
||||
}
|
||||
@@ -1668,7 +1670,7 @@ fn resolve_links(d: &mut Document, base: LinkBase, update: bool, low_res_proxy_e
|
||||
store_image(d, &g.key, blob, true);
|
||||
} else if let Some(b) = d.images.get(&g.key).filter(|b| b.proxy.is_none()) {
|
||||
// Linked before links kept a preview.
|
||||
let b = b.clone().with_proxy();
|
||||
let b = fileio::preview_with_clip(b.clone().with_proxy());
|
||||
d.images.insert(g.key.clone(), b);
|
||||
}
|
||||
// Found away from its path, or read again: the link follows.
|
||||
|
||||
Reference in New Issue
Block a user