mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
745 lines
34 KiB
Rust
745 lines
34 KiB
Rust
//! Placed and raster art in the script object model: `Document.placedItems` and
|
|
//! `Document.rasterItems`, and the members of `PlacedItem` and `RasterItem`, as the help pages
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/PlacedItems/>,
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/PlacedItem/>,
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/RasterItems/> and
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/RasterItem/> describe them.
|
|
//!
|
|
//! A `PlacedItem` is an image linked to its file, and a `RasterItem` an image whose pixels are in
|
|
//! the document. Placing runs `file.place` with `link`, relinking `links.relink` and embedding
|
|
//! `links.embed`, so each joins the run's transaction.
|
|
//!
|
|
//! VectorCraft's own rules, which the help pages do not state:
|
|
//! - `PlacedItems.add()` gives a placed item with no file. Its `file` may be assigned once, which
|
|
//! places the file linked; after that, and on every other placed item, `file` is read-only and
|
|
//! `relink(file)` changes it. Every other member of an item with no file throws
|
|
//! [`FILE_REQUIRED`]; an item with no file is not in the document and is dropped when the run
|
|
//! ends.
|
|
//! - A file to place or relink is a `File` the script may read ([`crate::file_access::ScriptGate`])
|
|
//! that holds an image, of at most [`MAX_PLACED_BYTES`]. Its size is read before any of it is.
|
|
//! - `PlacedItem.file` throws `Script file access denied`, without the path, when the script may
|
|
//! not read the linked file.
|
|
//! - `embed()` turns the placed item into a raster item with the same id: the placed item is stale
|
|
//! after it, and `rasterItems` lists the raster item. An image whose file is missing throws
|
|
//! [`MISSING_LINK`] and stays linked.
|
|
//! - `bitsPerChannel`, `channels`, `imageColorSpace` and `transparent` come from the header of the
|
|
//! image's encoded pixels; `transparent` is true for an image with an alpha channel or a
|
|
//! transparent palette entry. `RasterItem.file` is `null`, `embedded` is true and takes only
|
|
//! `true`, and `status` is `RasterLinkState.NODATA`, since a raster item has no linked file.
|
|
//! - `matrix` maps the image's pixels (row 0 at the top) to script coordinates, as a record of
|
|
//! `mValueA` to `mValueTY`; a distorted image has none. Assigning it runs `object.transform`,
|
|
//! and assigning the matrix it reads runs nothing; a matrix with no area, one that puts a
|
|
//! corner of the image outside the canvas, or one a nearly singular image cannot reach exactly
|
|
//! is refused.
|
|
//! `boundingBox` is the pixel rectangle at one point a pixel, in the run's coordinate system.
|
|
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
use serde_json::json;
|
|
use vectorcraft_geom::{Affine, Point};
|
|
use vectorcraft_js::{Data, HostClass, HostError, MemberInfo, file_ref, file_ref_path, host_ref, host_ref_parts};
|
|
|
|
use super::{
|
|
Answer, Coords, Described, Doc, Host, Item, Link, NOT_FOUND, ON_LOCKED_ARTBOARD, STALE_ITEM, all_editable, arg, class, command, command_value,
|
|
doc_of, doc_ref, edit_item, editable, find_doc, float, frame, get, item_of, item_ref, later, method, named, num, on_doc, page_items, property,
|
|
returns, text, typed, typename_of, variables, whole, with_doc,
|
|
};
|
|
use crate::{DocState, Document, NodeId, NodeKind};
|
|
|
|
/// The error of a member of a `PlacedItems.add()` result whose `file` was not assigned.
|
|
pub const FILE_REQUIRED: &str = "PlacedItem.file is required";
|
|
/// The error of assigning `file` to a placed item that has one.
|
|
pub const FILE_READ_ONLY: &str = "PlacedItem.file is read-only: use relink";
|
|
/// The error of embedding an image whose linked file cannot be found.
|
|
pub const MISSING_LINK: &str = "Linked image is missing";
|
|
/// The error of placing or relinking a file that is not an image.
|
|
pub const NOT_AN_IMAGE: &str = "The file is not an image";
|
|
/// The largest file a script places or relinks, in bytes.
|
|
pub const MAX_PLACED_BYTES: u64 = 256 << 20;
|
|
/// The most `PlacedItems.add()` results one run keeps.
|
|
pub const MAX_PENDING: usize = 1024;
|
|
/// The error of the `matrix` of an image drawn distorted.
|
|
const DISTORTED: &str = "the image is distorted and has no matrix";
|
|
/// The smallest determinant a `matrix` takes, in square points a square pixel.
|
|
const MIN_DETERMINANT: f64 = 1e-12;
|
|
/// The members of a `Matrix` record, in the order of an affine matrix's six numbers.
|
|
const MATRIX_KEYS: [&str; 6] = ["mValueA", "mValueB", "mValueC", "mValueD", "mValueTX", "mValueTY"];
|
|
|
|
/// The enumerations the members here give values of, as the help page
|
|
/// <https://ai-scripting.docsforadobe.dev/jsobjref/scripting-constants/> lists them.
|
|
pub(super) const ENUMS: &[(&str, &[&str])] = &[
|
|
("ImageColorSpace", &["CMYK", "DeviceN", "Grayscale", "Indexed", "LAB", "RGB", "Separation"]),
|
|
("RasterLinkState", &["DATAFROMFILE", "DATAMODIFIED", "NODATA"]),
|
|
];
|
|
|
|
/// The classes this module defines, in place of the plain page item classes.
|
|
pub(super) const CLASSES: &[&str] = &["PlacedItem", "RasterItem"];
|
|
|
|
/// A document's uid, the document as it was, and the ids of its linked and embedded images then.
|
|
type Lists = (u64, Arc<Document>, Rc<Vec<NodeId>>, Rc<Vec<NodeId>>);
|
|
|
|
/// What a run keeps for placed and raster items: the `PlacedItems.add()` results and the last
|
|
/// lists of a document's images.
|
|
#[derive(Default)]
|
|
pub(super) struct State {
|
|
pending: RefCell<Vec<Pending>>,
|
|
lists: RefCell<Option<Lists>>,
|
|
}
|
|
|
|
/// A `PlacedItems.add()` result: its document, how many times that document had been reverted then,
|
|
/// and its image once `file` placed one.
|
|
struct Pending {
|
|
uid: u64,
|
|
reverts: u64,
|
|
bound: Option<NodeId>,
|
|
}
|
|
|
|
pub(super) fn bound_ref(h: &Host, st: &DocState, id: NodeId) -> Option<Data> {
|
|
let pending = h.dom.images.pending.borrow();
|
|
let slot = pending.iter().position(|p| p.uid == st.uid && p.reverts == st.reverts && p.bound == Some(id))?;
|
|
Some(host_ref("PlacedItem", &[float(h.run), float(st.uid), 0.0, 0.0, float(st.reverts), float(slot as u64)]))
|
|
}
|
|
|
|
/// The slot of a `PlacedItems.add()` result in this run, from its handle. A result whose document
|
|
/// was closed or reverted since is stale.
|
|
fn slot_of(h: &Host, run: f64, uid: f64, slot: f64) -> Result<usize, HostError> {
|
|
let stale = || HostError::Error(STALE_ITEM.into());
|
|
let doc = find_doc(h, run, uid).ok_or_else(stale)?;
|
|
let reverts = h.session.borrow().documents().get(doc.index).map(|st| st.reverts);
|
|
let k = whole(slot).and_then(|k| usize::try_from(k).ok()).ok_or_else(stale)?;
|
|
match h.dom.images.pending.borrow().get(k) {
|
|
Some(p) if p.uid == doc.uid && Some(p.reverts) == reverts => Ok(k),
|
|
_ => Err(stale()),
|
|
}
|
|
}
|
|
|
|
/// The id of the image a `PlacedItems.add()` result placed.
|
|
pub(super) fn pending_id(h: &Host, run: f64, uid: f64, slot: f64) -> Result<NodeId, HostError> {
|
|
let k = slot_of(h, run, uid, slot)?;
|
|
match h.dom.images.pending.borrow().get(k).and_then(|p| p.bound) {
|
|
Some(id) => Ok(id),
|
|
None => Err(HostError::Error(FILE_REQUIRED.into())),
|
|
}
|
|
}
|
|
|
|
/// The handle's run, uid and slot when `d` is a `PlacedItems.add()` result. Its handle has six
|
|
/// numbers, where a page item's has five.
|
|
fn pending_handle(d: &Data) -> Option<[f64; 3]> {
|
|
match host_ref_parts(d) {
|
|
Some(("PlacedItem", handle)) => match handle.as_slice() {
|
|
[run, uid, _, _, _, slot] => Some([*run, *uid, *slot]),
|
|
_ => None,
|
|
},
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// The ids of a document's linked (`placed`) or embedded images, topmost first as [`page_items`]
|
|
/// lists them.
|
|
fn images(h: &Host, st: &DocState, placed: bool) -> Result<Rc<Vec<NodeId>>, HostError> {
|
|
let pick = |l: &Lists| if placed { l.2.clone() } else { l.3.clone() };
|
|
if let Some(l) = &*h.dom.images.lists.borrow()
|
|
&& l.0 == st.uid
|
|
&& Arc::ptr_eq(&l.1, &st.doc)
|
|
{
|
|
return Ok(pick(l));
|
|
}
|
|
let all = page_items(h, st)?;
|
|
h.charge_call(all.len() as u64)?;
|
|
let (mut linked, mut embedded) = (Vec::new(), Vec::new());
|
|
for id in all.iter() {
|
|
if let Some(NodeKind::Image(im)) = st.doc.node(*id).map(|n| &n.kind) {
|
|
if im.link.is_some() { linked.push(*id) } else { embedded.push(*id) }
|
|
}
|
|
}
|
|
let l = (st.uid, st.doc.clone(), Rc::new(linked), Rc::new(embedded));
|
|
let out = pick(&l);
|
|
*h.dom.images.lists.borrow_mut() = Some(l);
|
|
Ok(out)
|
|
}
|
|
|
|
fn field<'a>(d: &'a Data, k: &str) -> Option<&'a Data> {
|
|
match d {
|
|
Data::Object(o) => o.iter().find(|(n, _)| n == k).map(|(_, v)| v),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
// ---- PlacedItems and RasterItems ----
|
|
|
|
fn collection(placed: bool) -> &'static str {
|
|
if placed { "PlacedItems" } else { "RasterItems" }
|
|
}
|
|
|
|
fn list_doc(h: &Host, a: &[Data], placed: bool) -> Result<Doc, HostError> {
|
|
doc_of(h, arg(a, 0), collection(placed))
|
|
}
|
|
|
|
fn list_length(h: &Host, a: &[Data], placed: bool) -> Answer {
|
|
let doc = list_doc(h, a, placed)?;
|
|
let n = with_doc(h, doc, |_, st| images(h, st, placed).map(|l| l.len()))?;
|
|
Ok(num(float(n as u64)))
|
|
}
|
|
|
|
fn list_at(h: &Host, a: &[Data], placed: bool) -> Answer {
|
|
let doc = list_doc(h, a, placed)?;
|
|
let Data::Number(n) = arg(a, 1) else { return Ok(Data::Undefined) };
|
|
let Some(i) = whole(*n).and_then(|i| usize::try_from(i).ok()) else { return Ok(Data::Undefined) };
|
|
with_doc(h, doc, |_, st| Ok(images(h, st, placed)?.get(i).map_or(Data::Undefined, |id| item_ref(h, st, *id))))
|
|
}
|
|
|
|
fn list_by_name(h: &Host, a: &[Data], placed: bool) -> Answer {
|
|
let doc = list_doc(h, a, placed)?;
|
|
let Data::String(name) = arg(a, 1) else { return Err(HostError::Type("give a name, as a string".into())) };
|
|
with_doc(h, doc, |_, st| {
|
|
let ids = images(h, st, placed)?;
|
|
h.charge_call(ids.len() as u64)?;
|
|
let id = ids.iter().find(|i| st.doc.node(**i).and_then(|n| n.name.as_deref()) == Some(name.as_str()));
|
|
id.map(|id| item_ref(h, st, *id)).ok_or_else(|| HostError::Error(NOT_FOUND.into()))
|
|
})
|
|
}
|
|
|
|
fn list_index(h: &Host, a: &[Data], placed: bool) -> Answer {
|
|
match arg(a, 1) {
|
|
Data::String(_) => list_by_name(h, a, placed),
|
|
Data::Number(_) => match list_at(h, a, placed)? {
|
|
Data::Undefined => Err(HostError::Error(NOT_FOUND.into())),
|
|
d => Ok(d),
|
|
},
|
|
_ => Err(HostError::Type("give a name or an index".into())),
|
|
}
|
|
}
|
|
|
|
/// `removeAll()`: deletes every image of the collection in one command. A hidden or locked one, or
|
|
/// one in a hidden or locked layer or group, stops it before anything is deleted, and so does one on
|
|
/// a locked artboard.
|
|
fn list_remove_all(h: &Host, a: &[Data], placed: bool) -> Answer {
|
|
let doc = list_doc(h, a, placed)?;
|
|
let (ids, refused, on_locked) = with_doc(h, doc, |_, st| {
|
|
let ids = images(h, st, placed)?;
|
|
let whole = |id: &NodeId| editable(&st.doc, *id) && st.doc.node(*id).is_some_and(all_editable);
|
|
let refused: Vec<u64> = ids.iter().filter(|i| !whole(i)).map(|i| i.0).take(20).collect();
|
|
let locks = st.doc.locked_artboards();
|
|
let on_locked: Vec<u64> = ids.iter().filter(|i| locks.holds_id(**i)).map(|i| i.0).take(20).collect();
|
|
Ok::<_, HostError>((ids, refused, on_locked))
|
|
})?;
|
|
if !refused.is_empty() {
|
|
let list = refused.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
|
|
return Err(HostError::Error(format!("cannot delete hidden or locked objects: {list}")));
|
|
}
|
|
if !on_locked.is_empty() {
|
|
let list = on_locked.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
|
|
return Err(HostError::Error(format!("{ON_LOCKED_ARTBOARD}: {list}")));
|
|
}
|
|
if !ids.is_empty() {
|
|
on_doc(h, doc.uid, true, |h| command(h, "edit.clear", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// `PlacedItems.add()`: a placed item with no file yet, which assigning `file` places.
|
|
fn placed_add(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = list_doc(h, a, true)?;
|
|
let reverts = h.session.borrow().documents().get(doc.index).map_or(0, |st| st.reverts);
|
|
let mut pending = h.dom.images.pending.borrow_mut();
|
|
if pending.len() >= MAX_PENDING {
|
|
return Err(HostError::Range(format!("a run makes at most {MAX_PENDING} placed items with PlacedItems.add")));
|
|
}
|
|
pending.push(Pending { uid: doc.uid, reverts, bound: None });
|
|
Ok(host_ref("PlacedItem", &[float(h.run), float(doc.uid), 0.0, 0.0, float(reverts), float(pending.len() as u64 - 1)]))
|
|
}
|
|
|
|
macro_rules! both {
|
|
($($placed:ident, $raster:ident => $f:ident;)*) => {
|
|
$(
|
|
fn $placed(h: &Host, a: &[Data]) -> Answer {
|
|
$f(h, a, true)
|
|
}
|
|
fn $raster(h: &Host, a: &[Data]) -> Answer {
|
|
$f(h, a, false)
|
|
}
|
|
)*
|
|
};
|
|
}
|
|
|
|
both! {
|
|
placed_length, raster_length => list_length;
|
|
placed_at, raster_at => list_at;
|
|
placed_by_name, raster_by_name => list_by_name;
|
|
placed_index, raster_index => list_index;
|
|
placed_remove_all, raster_remove_all => list_remove_all;
|
|
}
|
|
|
|
fn placed_parent(h: &Host, a: &[Data]) -> Answer {
|
|
Ok(doc_ref(h, list_doc(h, a, true)?.uid))
|
|
}
|
|
|
|
fn raster_parent(h: &Host, a: &[Data]) -> Answer {
|
|
Ok(doc_ref(h, list_doc(h, a, false)?.uid))
|
|
}
|
|
|
|
fn doc_placed(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = doc_of(h, arg(a, 0), "Document")?;
|
|
Ok(host_ref("PlacedItems", &[float(h.run), float(doc.uid)]))
|
|
}
|
|
|
|
fn doc_raster(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = doc_of(h, arg(a, 0), "Document")?;
|
|
Ok(host_ref("RasterItems", &[float(h.run), float(doc.uid)]))
|
|
}
|
|
|
|
// ---- files ----
|
|
|
|
/// Whether the script may read `path` now by its own read scope and the read roots
|
|
/// ([`crate::file_access::ScriptGate`]), without the open documents' files, or the files they link
|
|
/// to, that a command may also read.
|
|
fn may_read(h: &Host, path: &str) -> bool {
|
|
let gate = h.disk.borrow().gate(Vec::new(), crate::file_access::LinkedFiles::default());
|
|
crate::file_access::with_script_gate(gate, || crate::file_access::check_read(path)).is_ok()
|
|
}
|
|
|
|
/// The real path of the `File` `d` gives as an image to place or relink: one the script may read
|
|
/// ([`super::super::files::Disk::open_target`], as `app.open` reads its file), a regular file of
|
|
/// at most [`MAX_PLACED_BYTES`]. Its size is read before any of it is, and no error names the path.
|
|
/// What the file holds is checked by the command that places or relinks it, which reads it once.
|
|
fn image_path(h: &Host, d: &Data, what: &str) -> Result<String, HostError> {
|
|
let path = file_ref_path(d).ok_or_else(|| HostError::Type(format!("{what} must be a File")))?;
|
|
if path.len() > super::super::files::MAX_PATH_CHARS {
|
|
return Err(HostError::Range(super::super::files::PATH_TOO_LONG.into()));
|
|
}
|
|
let unc = path.starts_with("\\\\") || path.starts_with("//");
|
|
if path.is_empty() || path.contains('\0') || unc || path.split(['/', '\\']).any(|c| c == "..") {
|
|
return Err(HostError::Error(super::super::files::ACCESS_DENIED.into()));
|
|
}
|
|
let real = h.disk.borrow().open_target(path).map_err(|m| HostError::Error(m.into()))?;
|
|
let meta = std::fs::metadata(&real).map_err(|_| HostError::Error(super::super::files::NOT_FOUND.into()))?;
|
|
if !meta.is_file() {
|
|
return Err(HostError::Error(super::super::files::NOT_REGULAR.into()));
|
|
}
|
|
if meta.len() > MAX_PLACED_BYTES {
|
|
return Err(HostError::Range(format!("{what} is larger than {} MiB", MAX_PLACED_BYTES >> 20)));
|
|
}
|
|
Ok(crate::cmd::findfiles::plain(real).to_string_lossy().into_owned())
|
|
}
|
|
|
|
/// Runs `f`, which places or relinks a file `image_path` gave, with `file.place` and `links.relink`
|
|
/// refusing a file that holds no image before the document changes: that refusal is
|
|
/// [`NOT_AN_IMAGE`], which does not name the path. The command reads the file once, for its check
|
|
/// and for the image it places.
|
|
fn image_only<T>(h: &Host, f: impl FnOnce(&Host) -> Result<T, HostError>) -> Result<T, HostError> {
|
|
let kept = h.session.borrow().image_only.replace(Some(true));
|
|
let r = f(h);
|
|
let refused = h.session.borrow().image_only.replace(kept) == Some(false);
|
|
match r {
|
|
Err(HostError::Error(_)) if refused => Err(HostError::Error(NOT_AN_IMAGE.into())),
|
|
r => r,
|
|
}
|
|
}
|
|
|
|
// ---- PlacedItem ----
|
|
|
|
/// The item `this` names when it is a `cls` (`PlacedItem` or `RasterItem`).
|
|
fn this_of(h: &Host, a: &[Data], cls: &str) -> Result<Item, HostError> {
|
|
match host_ref_parts(arg(a, 0)) {
|
|
Some((c, _)) if c == cls => item_of(h, arg(a, 0)),
|
|
_ => Err(HostError::Type(format!("this is not a {cls}"))),
|
|
}
|
|
}
|
|
|
|
/// Reads the image of the `cls` `this` names with `f`.
|
|
fn read_image(h: &Host, a: &[Data], cls: &str, f: impl FnOnce(&DocState, &crate::doc::Node, &crate::doc::ImageObject) -> Answer) -> Answer {
|
|
let item = this_of(h, a, cls)?;
|
|
with_doc(h, item.doc, |_, st| match st.doc.node(item.id) {
|
|
Some(n) => match &n.kind {
|
|
NodeKind::Image(im) => f(st, n, im),
|
|
_ => Err(HostError::Error(STALE_ITEM.into())),
|
|
},
|
|
None => Err(HostError::Error(STALE_ITEM.into())),
|
|
})
|
|
}
|
|
|
|
fn placed_file(h: &Host, a: &[Data]) -> Answer {
|
|
if let Some([run, uid, slot]) = pending_handle(arg(a, 0)) {
|
|
let k = slot_of(h, run, uid, slot)?;
|
|
if h.dom.images.pending.borrow().get(k).is_some_and(|p| p.bound.is_none()) {
|
|
// Null while its document is open; stale once that document is closed.
|
|
find_doc(h, run, uid).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
|
|
return Ok(Data::Null);
|
|
}
|
|
}
|
|
let path = read_image(h, a, "PlacedItem", |st, _, im| {
|
|
let path = im.link.as_ref().and_then(|link| {
|
|
let base = crate::cmd::links::LinkBase::of(st);
|
|
crate::cmd::links::located_path(link, base).or_else(|| crate::cmd::fileio::document_path(&link.path, base.doc).ok())
|
|
});
|
|
let path = path.ok_or_else(|| HostError::Error(super::super::files::ACCESS_DENIED.into()))?;
|
|
Ok(text(path))
|
|
})?;
|
|
let Data::String(path) = path else { return Ok(Data::Null) };
|
|
if !may_read(h, &path) {
|
|
return Err(HostError::Error(super::super::files::ACCESS_DENIED.into()));
|
|
}
|
|
Ok(file_ref(&path))
|
|
}
|
|
|
|
/// `file = File`: places the file, linked, for a `PlacedItems.add()` result that has none.
|
|
fn placed_set_file(h: &Host, a: &[Data]) -> Answer {
|
|
let Some([run, uid, slot]) = pending_handle(arg(a, 0)) else {
|
|
if host_ref_parts(arg(a, 0)).is_some_and(|(c, _)| c == "PlacedItem") {
|
|
this_of(h, a, "PlacedItem")?;
|
|
return Err(HostError::Error(FILE_READ_ONLY.into()));
|
|
}
|
|
return Err(HostError::Type("this is not a PlacedItem".into()));
|
|
};
|
|
let k = slot_of(h, run, uid, slot)?;
|
|
if h.dom.images.pending.borrow().get(k).is_some_and(|p| p.bound.is_some()) {
|
|
this_of(h, a, "PlacedItem")?;
|
|
return Err(HostError::Error(FILE_READ_ONLY.into()));
|
|
}
|
|
let doc = find_doc(h, run, uid).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
|
|
let path = image_path(h, arg(a, 1), "PlacedItem.file")?;
|
|
// The ids are read from the command's own result, where every id is exact (a script number
|
|
// holds integers exactly only up to 2^53).
|
|
// The placement and its check run under one savepoint, so an error after the place leaves
|
|
// no linked image behind.
|
|
let id = super::atomically(h, |h| {
|
|
let (_, placed) = image_only(h, |h| on_doc(h, doc.uid, true, |h| command_value(h, "file.place", &json!({"path": path, "link": true}))))?;
|
|
let first = |v: &serde_json::Value| v.as_u64().or_else(|| v.as_array().and_then(|a| a.first()).and_then(serde_json::Value::as_u64));
|
|
let id = first(&placed["image"]).or_else(|| first(&placed["ids"])).map(NodeId);
|
|
let linked =
|
|
|id: NodeId| with_doc(h, doc, |_, st| matches!(st.doc.node(id).map(|n| &n.kind), Some(NodeKind::Image(im)) if im.link.is_some()));
|
|
id.filter(|id| linked(*id)).ok_or_else(|| HostError::Error(NOT_AN_IMAGE.into()))
|
|
})?;
|
|
if let Some(p) = h.dom.images.pending.borrow_mut().get_mut(k) {
|
|
p.bound = Some(id);
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// `relink(file)`: `links.relink` of the item to the image in `file`.
|
|
fn placed_relink(h: &Host, a: &[Data]) -> Answer {
|
|
let item = this_of(h, a, "PlacedItem")?;
|
|
let path = image_path(h, arg(a, 1), "relink's file")?;
|
|
image_only(h, |h| edit_item(h, item, "links.relink", &json!({"ids": [item.id.0], "path": path})))?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// `embed()`: `links.embed` of the item, which becomes a raster item.
|
|
fn placed_embed(h: &Host, a: &[Data]) -> Answer {
|
|
let item = this_of(h, a, "PlacedItem")?;
|
|
let info = on_doc(h, item.doc.uid, false, |h| command(h, "links.info", &json!({"id": item.id.0})))?;
|
|
if matches!(field(&info, "status"), Some(Data::String(s)) if s == "missing") {
|
|
return Err(HostError::Error(MISSING_LINK.into()));
|
|
}
|
|
edit_item(h, item, "links.embed", &json!({"ids": [item.id.0]}))?;
|
|
let embedded = with_doc(h, item.doc, |_, st| matches!(st.doc.node(item.id).map(|n| &n.kind), Some(NodeKind::Image(im)) if im.link.is_none()));
|
|
if !embedded {
|
|
return Err(HostError::Error(MISSING_LINK.into()));
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
// ---- both ----
|
|
|
|
/// `matrix`: the image's pixels to script coordinates.
|
|
fn matrix(h: &Host, a: &[Data], cls: &str) -> Answer {
|
|
read_image(h, a, cls, |st, n, im| {
|
|
if n.is_distorted_image() {
|
|
return Err(HostError::Error(DISTORTED.into()));
|
|
}
|
|
let f = frame(st)?;
|
|
let [a0, b0, c0, d0, e0, f0] = im.xf.as_coeffs();
|
|
let m = match h.dom.coords.get() {
|
|
Coords::Document => [a0, -b0, c0, -d0, e0 - f.left0, f.bottom0 - f0],
|
|
Coords::Artboard => [a0, b0, c0, d0, e0 - f.left, f0 - f.top],
|
|
};
|
|
let mut items = vec![("typename".to_string(), text("Matrix"))];
|
|
items.extend(MATRIX_KEYS.iter().zip(m).map(|(k, v)| (k.to_string(), num(v))));
|
|
Ok(Data::Object(items))
|
|
})
|
|
}
|
|
|
|
/// The six numbers of the `Matrix` record `d`, `mValueA` to `mValueTY`, each finite.
|
|
fn matrix_value(d: &Data) -> Result<[f64; 6], HostError> {
|
|
let mut m = [0.0; 6];
|
|
for (k, out) in MATRIX_KEYS.iter().zip(m.iter_mut()) {
|
|
*out = match field(d, k) {
|
|
Some(Data::Number(n)) if n.is_finite() => *n,
|
|
_ => return Err(HostError::Type(format!("matrix must be a Matrix with a finite {k}"))),
|
|
};
|
|
}
|
|
Ok(m)
|
|
}
|
|
|
|
/// Whether `m` gives a pixel an area of at least [`MIN_DETERMINANT`].
|
|
fn area_of(m: Affine) -> bool {
|
|
let det = m.determinant();
|
|
!det.is_nan() && det.abs() >= MIN_DETERMINANT
|
|
}
|
|
|
|
/// `matrix = Matrix`: `object.transform` of the image by what takes its pixels to the new matrix.
|
|
/// A matrix with no area, one that puts a corner of the image outside the canvas, or a distorted
|
|
/// image is refused before anything changes, and so is a locked or hidden image or one on a locked
|
|
/// artboard, as the transform methods refuse them; the matrix the image has already changes nothing.
|
|
fn set_matrix(h: &Host, a: &[Data], cls: &str) -> Answer {
|
|
let item = this_of(h, a, cls)?;
|
|
let m = matrix_value(arg(a, 1))?;
|
|
let delta = with_doc(h, item.doc, |_, st| {
|
|
let Some((n, im)) = st.doc.node(item.id).and_then(|n| if let NodeKind::Image(im) = &n.kind { Some((n, im)) } else { None }) else {
|
|
return Err(HostError::Error(STALE_ITEM.into()));
|
|
};
|
|
if n.is_distorted_image() {
|
|
return Err(HostError::Error(DISTORTED.into()));
|
|
}
|
|
// The transform methods' refusals: a locked or hidden image, one inside a locked or hidden
|
|
// layer or group, and one on a locked artboard, whether or not the matrix changes.
|
|
let d = &st.doc;
|
|
let line = d.ancestry(item.id);
|
|
let any = |f: fn(&vectorcraft_doc::Node) -> bool| {
|
|
line.as_ref().is_none_or(|l| l.iter().chain(std::iter::once(&item.id)).any(|i| d.node(*i).is_none_or(f)))
|
|
};
|
|
if any(|n| n.locked) {
|
|
return Err(HostError::Error(format!("{}: {}", super::transform::LOCKED, item.id.0)));
|
|
}
|
|
if any(|n| !n.visible) {
|
|
return Err(HostError::Error(format!("{}: {}", super::transform::HIDDEN, item.id.0)));
|
|
}
|
|
if super::on_locked_artboard(h, st, item.id) {
|
|
return Err(HostError::Error(format!("{}: {}", super::transform::ON_LOCKED_ARTBOARD, item.id.0)));
|
|
}
|
|
let f = frame(st)?;
|
|
let target = Affine::new(match h.dom.coords.get() {
|
|
Coords::Document => [m[0], -m[1], m[2], -m[3], m[4] + f.left0, f.bottom0 - m[5]],
|
|
Coords::Artboard => [m[0], m[1], m[2], m[3], m[4] + f.left, m[5] + f.top],
|
|
});
|
|
if !area_of(target) {
|
|
return Err(HostError::Range("the matrix gives the image no area".into()));
|
|
}
|
|
let (w, ht) = (f64::from(im.width), f64::from(im.height));
|
|
let limit = crate::MAX_COORD * 2.0;
|
|
let corners = [(0.0, 0.0), (w, 0.0), (0.0, ht), (w, ht)].map(|(x, y)| target * Point::new(x, y));
|
|
if corners.iter().any(|p| !(p.x.abs() <= limit && p.y.abs() <= limit)) {
|
|
return Err(HostError::Range("the matrix puts the image outside the canvas".into()));
|
|
}
|
|
let old = im.xf;
|
|
// Unchanged means the numbers the `matrix` getter reports, compared exactly, so a small
|
|
// change such as `mValueTX` from 0 to 5e-10 still moves the image. A change that the
|
|
// conversion to document space rounds away leaves the image's matrix as it is and runs nothing.
|
|
// A coefficient the script left as the getter reports keeps the image's own value, so the
|
|
// conversion back does not shift an untouched axis.
|
|
let olds = old.as_coeffs();
|
|
let [a0, b0, c0, d0, e0, f0] = olds;
|
|
let reported = match h.dom.coords.get() {
|
|
Coords::Document => [a0, -b0, c0, -d0, e0 - f.left0, f.bottom0 - f0],
|
|
Coords::Artboard => [a0, b0, c0, d0, e0 - f.left, f0 - f.top],
|
|
};
|
|
let mut wanted = target.as_coeffs();
|
|
for ((w, o), (r, v)) in wanted.iter_mut().zip(olds).zip(reported.iter().zip(m.iter())) {
|
|
if r == v {
|
|
*w = o;
|
|
}
|
|
}
|
|
if wanted == olds {
|
|
return Ok(None);
|
|
}
|
|
if !area_of(old) {
|
|
return Err(HostError::Error("the image has no area to transform".into()));
|
|
}
|
|
let target = Affine::new(wanted);
|
|
let [ta, tb, tc, td, te, tf] = wanted;
|
|
if [ta, tb, tc, td] == [a0, b0, c0, d0] {
|
|
// A move alone: the offset difference, without the error an inverse adds.
|
|
return Ok(Some([1.0, 0.0, 0.0, 1.0, te - e0, tf - f0]));
|
|
}
|
|
// A nearly singular image loses precision through the inverse; refuse a change that would
|
|
// not land where the script asked rather than move the image somewhere else.
|
|
let delta = target * old.inverse();
|
|
let landed = delta * old;
|
|
if landed.as_coeffs() == olds {
|
|
return Ok(None);
|
|
}
|
|
let close = [(0.0, 0.0), (w, 0.0), (0.0, ht), (w, ht)].iter().all(|&(x, y)| {
|
|
let (p, q) = (landed * Point::new(x, y), target * Point::new(x, y));
|
|
(p.x - q.x).abs() <= 1e-6 && (p.y - q.y).abs() <= 1e-6
|
|
});
|
|
if !close || !area_of(landed) {
|
|
return Err(HostError::Range("the matrix cannot be applied exactly to this image".into()));
|
|
}
|
|
Ok(Some(delta.as_coeffs()))
|
|
})?;
|
|
if let Some(d) = delta {
|
|
edit_item(h, item, "object.transform", &json!({"ids": [item.id.0], "matrix": d, "strokes": false}))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// `boundingBox`: the pixel rectangle at one point a pixel, `[left, top, right, bottom]`.
|
|
fn bounding_box(h: &Host, a: &[Data], cls: &str) -> Answer {
|
|
read_image(h, a, cls, |_, _, im| {
|
|
let (w, ht) = (f64::from(im.width), f64::from(im.height));
|
|
Ok(Data::Array(match h.dom.coords.get() {
|
|
Coords::Document => vec![num(0.0), num(ht), num(w), num(0.0)],
|
|
Coords::Artboard => vec![num(0.0), num(0.0), num(w), num(ht)],
|
|
}))
|
|
})
|
|
}
|
|
|
|
fn placed_matrix(h: &Host, a: &[Data]) -> Answer {
|
|
matrix(h, a, "PlacedItem")
|
|
}
|
|
|
|
fn raster_matrix(h: &Host, a: &[Data]) -> Answer {
|
|
matrix(h, a, "RasterItem")
|
|
}
|
|
|
|
fn placed_set_matrix(h: &Host, a: &[Data]) -> Answer {
|
|
set_matrix(h, a, "PlacedItem")
|
|
}
|
|
|
|
fn raster_set_matrix(h: &Host, a: &[Data]) -> Answer {
|
|
set_matrix(h, a, "RasterItem")
|
|
}
|
|
|
|
fn placed_box(h: &Host, a: &[Data]) -> Answer {
|
|
bounding_box(h, a, "PlacedItem")
|
|
}
|
|
|
|
fn raster_box(h: &Host, a: &[Data]) -> Answer {
|
|
bounding_box(h, a, "RasterItem")
|
|
}
|
|
|
|
// ---- RasterItem ----
|
|
|
|
/// The header of the raster item's encoded pixels.
|
|
fn header(h: &Host, a: &[Data], f: impl FnOnce(&craft_image_codecs::RasterInspection) -> Data) -> Answer {
|
|
read_image(h, a, "RasterItem", |st, _, im| {
|
|
let blob = st.doc.images.get(&im.key).ok_or_else(|| HostError::Error("the image's pixels are not in the document".into()))?;
|
|
match craft_image_codecs::inspect_raster(&blob.bytes) {
|
|
Ok(Some(i)) => Ok(f(&i)),
|
|
_ => Err(HostError::Error("the image's header does not describe its pixels".into())),
|
|
}
|
|
})
|
|
}
|
|
|
|
fn raster_bits(h: &Host, a: &[Data]) -> Answer {
|
|
header(h, a, |i| num(f64::from(i.bits_per_sample)))
|
|
}
|
|
|
|
fn raster_channels(h: &Host, a: &[Data]) -> Answer {
|
|
header(h, a, |i| num(f64::from(i.channels)))
|
|
}
|
|
|
|
fn raster_transparent(h: &Host, a: &[Data]) -> Answer {
|
|
use craft_image_codecs::RasterColorType as T;
|
|
header(h, a, |i| Data::Bool(matches!(i.color_type, T::GrayAlpha | T::Rgba | T::CmykAlpha) || i.transparent_index.is_some()))
|
|
}
|
|
|
|
fn raster_color_space(h: &Host, a: &[Data]) -> Answer {
|
|
use craft_image_codecs::RasterColorType as T;
|
|
let space = header(h, a, |i| {
|
|
text(match i.color_type {
|
|
T::Bitmap | T::Gray | T::GrayAlpha => "ImageColorSpace.Grayscale",
|
|
T::Rgb | T::Rgba => "ImageColorSpace.RGB",
|
|
T::Cmyk | T::CmykAlpha => "ImageColorSpace.CMYK",
|
|
T::Indexed => "ImageColorSpace.Indexed",
|
|
T::Lab => "ImageColorSpace.LAB",
|
|
T::Other => "",
|
|
})
|
|
})?;
|
|
match space {
|
|
Data::String(s) if s.is_empty() => Err(HostError::Error("the image's header does not describe its color space".into())),
|
|
d => Ok(d),
|
|
}
|
|
}
|
|
|
|
fn raster_colorized(h: &Host, a: &[Data]) -> Answer {
|
|
read_image(h, a, "RasterItem", |st, n, im| {
|
|
let gray = st.doc.images.get(&im.key).is_some_and(|b| b.gray_source().is_some());
|
|
Ok(Data::Bool(gray && n.extras().colorize.is_some()))
|
|
})
|
|
}
|
|
|
|
fn raster_embedded(h: &Host, a: &[Data]) -> Answer {
|
|
this_of(h, a, "RasterItem")?;
|
|
Ok(Data::Bool(true))
|
|
}
|
|
|
|
/// `embedded = true` changes nothing; a raster item cannot be linked by assigning `false`.
|
|
fn raster_set_embedded(h: &Host, a: &[Data]) -> Answer {
|
|
this_of(h, a, "RasterItem")?;
|
|
match arg(a, 1) {
|
|
Data::Bool(true) => Ok(Data::Undefined),
|
|
Data::Bool(false) => Err(HostError::Error("a raster item cannot be linked by setting embedded to false".into())),
|
|
_ => Err(HostError::Type("embedded must be true or false".into())),
|
|
}
|
|
}
|
|
|
|
fn raster_file(h: &Host, a: &[Data]) -> Answer {
|
|
this_of(h, a, "RasterItem")?;
|
|
Ok(Data::Null)
|
|
}
|
|
|
|
fn raster_status(h: &Host, a: &[Data]) -> Answer {
|
|
this_of(h, a, "RasterItem")?;
|
|
Ok(text("RasterLinkState.NODATA"))
|
|
}
|
|
|
|
/// `Document.placedItems` and `Document.rasterItems`.
|
|
pub(super) fn document_members(l: &Link) -> Vec<Described> {
|
|
named(vec![
|
|
("placedItems", get(l, doc_placed), MemberInfo { collection: true, ..typed("PlacedItems") }),
|
|
("rasterItems", get(l, doc_raster), MemberInfo { collection: true, ..typed("RasterItems") }),
|
|
])
|
|
}
|
|
|
|
/// The collections, `PlacedItem` and `RasterItem`, after `PageItem`.
|
|
pub(super) fn classes(l: &Link) -> Vec<HostClass> {
|
|
let placed_list = named(vec![
|
|
("typename", typename_of("PlacedItems"), typed("string")),
|
|
("length", get(l, placed_length), typed("number")),
|
|
("parent", get(l, placed_parent), typed("Document")),
|
|
("index", method(l, placed_index), returns("PlacedItem", &[("itemKey", "*")])),
|
|
("getByName", method(l, placed_by_name), returns("PlacedItem", &[("name", "string")])),
|
|
("removeAll", method(l, placed_remove_all), returns("undefined", &[])),
|
|
("add", method(l, placed_add), returns("PlacedItem", &[])),
|
|
]);
|
|
let raster_list = named(vec![
|
|
("typename", typename_of("RasterItems"), typed("string")),
|
|
("length", get(l, raster_length), typed("number")),
|
|
("parent", get(l, raster_parent), typed("Document")),
|
|
("index", method(l, raster_index), returns("RasterItem", &[("itemKey", "*")])),
|
|
("getByName", method(l, raster_by_name), returns("RasterItem", &[("name", "string")])),
|
|
("removeAll", method(l, raster_remove_all), returns("undefined", &[])),
|
|
]);
|
|
let mut placed = named(vec![
|
|
("file", property(l, placed_file, placed_set_file), typed("File")),
|
|
("relink", method(l, placed_relink), returns("undefined", &[("relinkFrom", "File")])),
|
|
("embed", method(l, placed_embed), returns("undefined", &[])),
|
|
("matrix", property(l, placed_matrix, placed_set_matrix), typed("Matrix")),
|
|
("boundingBox", get(l, placed_box), typed("Array")),
|
|
]);
|
|
placed.extend(variables::item_members(l, "PlacedItem"));
|
|
let mut raster = named(vec![
|
|
("bitsPerChannel", get(l, raster_bits), typed("number")),
|
|
("channels", get(l, raster_channels), typed("number")),
|
|
("colorizedGrayscale", get(l, raster_colorized), typed("boolean")),
|
|
("embedded", property(l, raster_embedded, raster_set_embedded), typed("boolean")),
|
|
("file", get(l, raster_file), typed("File")),
|
|
("imageColorSpace", get(l, raster_color_space), typed("ImageColorSpace")),
|
|
("matrix", property(l, raster_matrix, raster_set_matrix), typed("Matrix")),
|
|
("status", get(l, raster_status), typed("RasterLinkState")),
|
|
("transparent", get(l, raster_transparent), typed("boolean")),
|
|
("boundingBox", get(l, raster_box), typed("Array")),
|
|
]);
|
|
raster.extend(variables::item_members(l, "RasterItem"));
|
|
raster.extend(["colorants", "colorize", "overprint"].into_iter().map(|m| later(l, "RasterItem", m)));
|
|
vec![
|
|
class("PlacedItems", None, placed_list, Some(super::function(l, placed_at))),
|
|
class("RasterItems", None, raster_list, Some(super::function(l, raster_at))),
|
|
class("PlacedItem", Some("PageItem"), placed, None),
|
|
class("RasterItem", Some("PageItem"), raster, None),
|
|
]
|
|
}
|