mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 05:22:58 -05:00
1734 lines
75 KiB
Rust
1734 lines
75 KiB
Rust
//! Layers, groups, compound paths and artboards in the script object model, as the help pages
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/Layer/>,
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/Layers/>,
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/GroupItem/>,
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/GroupItems/>,
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/CompoundPathItem/>,
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/Artboard/> and
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/Artboards/> describe them.
|
|
//!
|
|
//! A layer is named by its id and an artboard by its artboard id, so a handle keeps naming the same
|
|
//! one after others are added, moved or removed, and throws once it is gone.
|
|
//!
|
|
//! VectorCraft's own rules, which the help pages do not state:
|
|
//! - `layers` lists layers top first, as the Layers panel shows them. A layer's or group's
|
|
//! `pageItems`, `groupItems`, `compoundPathItems`, `pathItems` and `textFrames`, and a compound path's
|
|
//! `pathItems`, list the objects directly in it, topmost first. The document's `groupItems` and `compoundPathItems`
|
|
//! hold every group or compound path at any depth, in the order of the document's `pageItems`.
|
|
//! - `layers.add()` on the document makes a layer above every top-level layer; on a layer it makes
|
|
//! a sublayer on top of that layer's contents. The new layer becomes the current one.
|
|
//! - `locked` true means the layer is locked.
|
|
//! - `clipped = true` makes the group's topmost object, which must be a path, a compound path or
|
|
//! type, its clipping path, as the Layers panel's clipping mask button does; `false` releases it.
|
|
//! - A layer's `color` is an `RGBColor` object with values from 0 to 255.
|
|
//! - `CompoundPathItems.add()` makes an empty compound path. The first path a script moves into it
|
|
//! gives it its paint, as Make Compound Path takes the paint of a path it joins.
|
|
//! - `artboards.add` and `artboards.insert` stop at [`super::MAX_ARTBOARDS`] artboards in a
|
|
//! document, the limit `Documents.add` uses. Two artboards may have the same name.
|
|
//! - A layer's or artboard's `name` takes a number or a boolean as `String()` writes it.
|
|
//! - `Artboard.rulerOrigin` is `[x, y]` from the artboard's top-left corner with Y down; with global
|
|
//! rulers, setting it moves the document's zero point.
|
|
//! - `setActiveArtboardIndex` changes the view's active artboard: no undo step, and the run's report
|
|
//! lists it in `activeArtboards` for the app to show. The report also lists a document whose
|
|
//! active artboard has another index at the end of the run, because artboards were inserted,
|
|
//! removed or reordered before it.
|
|
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
use serde_json::{Value, json};
|
|
use vectorcraft_js::{Data, HostClass, HostError, MemberInfo, host_ref, host_ref_parts};
|
|
|
|
use super::{
|
|
Answer, Described, Doc, Item, MAX_LISTED, MAX_TEXT_BYTES, NOT_FOUND, ON_LOCKED_ARTBOARD, Placement, STALE_DOCUMENT, STALE_ITEM, active_artboard,
|
|
arg, boolean, command, command_value, doc_of, doc_ref, editable, find_doc, float, frame, get, handle_of, id_of, item_of, item_ref, later, method,
|
|
named, named_text, num, number, on_doc, on_doc_layer, page_items, placement_of, property, reported_opacity, returns, text, typed, typename,
|
|
whole, with_doc,
|
|
};
|
|
use crate::script::{Host, Link};
|
|
use crate::{DocState, Document, NodeId, NodeKind};
|
|
|
|
/// The error for a layer that was deleted, or that came from an earlier run.
|
|
pub const STALE_LAYER: &str = "Stale layer";
|
|
/// The error for an artboard that was deleted, or that came from an earlier run.
|
|
pub const STALE_ARTBOARD: &str = "Stale artboard";
|
|
/// The longest name a layer takes, in characters, as Layer Options keeps it.
|
|
pub const MAX_LAYER_NAME: usize = 512;
|
|
|
|
type Result<T> = std::result::Result<T, HostError>;
|
|
|
|
/// What a collection of art lists.
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
enum Kind {
|
|
/// Every page item.
|
|
Any,
|
|
/// Groups (`GroupItem`).
|
|
Group,
|
|
/// Compound paths (`CompoundPathItem`).
|
|
Compound,
|
|
/// Type (`TextFrame`), whose collection's other members are in `text.rs`.
|
|
Text,
|
|
}
|
|
|
|
impl Kind {
|
|
fn class(self) -> &'static str {
|
|
match self {
|
|
Kind::Any => "PageItems",
|
|
Kind::Group => "GroupItems",
|
|
Kind::Compound => "CompoundPathItems",
|
|
Kind::Text => "TextFrames",
|
|
}
|
|
}
|
|
|
|
fn holds(self, n: &crate::doc::Node) -> bool {
|
|
match self {
|
|
Kind::Any => typename(n).is_some(),
|
|
Kind::Group => typename(n) == Some("GroupItem"),
|
|
Kind::Compound => typename(n) == Some("CompoundPathItem"),
|
|
Kind::Text => typename(n) == Some("TextFrame"),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn type_error(what: &str) -> HostError {
|
|
HostError::Type(format!("this is not a {what}"))
|
|
}
|
|
|
|
/// The whole number under `key` in a command's own result (not the script's copy, whose numbers
|
|
/// are exact only up to 2^53), such as the id of the object it made.
|
|
fn result_id(v: &Value, key: &str) -> Option<u64> {
|
|
v.get(key).and_then(Value::as_u64)
|
|
}
|
|
|
|
// ---- Layer ----
|
|
|
|
/// The layer `d` names, which must still be a layer of its document.
|
|
pub(super) fn layer_of(h: &Host, d: &Data) -> Result<(Doc, NodeId)> {
|
|
let Some(handle) = handle_of(d, "Layer") else { return Err(type_error("Layer")) };
|
|
let [run, uid, high, low, _] = handle.as_slice() else { return Err(type_error("Layer")) };
|
|
let doc = find_doc(h, *run, *uid).ok_or_else(|| HostError::Error(STALE_LAYER.into()))?;
|
|
id_of(*high, *low).ok_or_else(|| HostError::Error(STALE_LAYER.into()))?;
|
|
// A layer read before its document was reverted is stale as the page items read then are.
|
|
let (doc, id) = super::node_of(h, &handle).filter(|(d, _)| d.uid == doc.uid).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
|
|
held_layer(h, doc, id)
|
|
}
|
|
|
|
/// The layer `id` of `doc`, which must still be a layer of the document.
|
|
fn held_layer(h: &Host, doc: Doc, id: NodeId) -> Result<(Doc, NodeId)> {
|
|
let ok = with_doc(h, doc, |_, st| st.doc.node(id).is_some_and(crate::doc::Node::is_layer));
|
|
if !ok {
|
|
return Err(HostError::Error(STALE_LAYER.into()));
|
|
}
|
|
Ok((doc, id))
|
|
}
|
|
|
|
fn this_layer(h: &Host, a: &[Data]) -> Result<(Doc, NodeId)> {
|
|
layer_of(h, arg(a, 0))
|
|
}
|
|
|
|
pub(super) fn layer_ref(h: &Host, uid: u64, id: NodeId) -> Data {
|
|
let s = h.session.borrow();
|
|
match s.documents().iter().find(|d| d.uid == uid) {
|
|
Some(st) => host_ref("Layer", &super::node_handle(h, st, id)),
|
|
None => Data::Null,
|
|
}
|
|
}
|
|
|
|
/// Reads the layer `this` names with `f`.
|
|
fn read_layer(h: &Host, a: &[Data], f: impl FnOnce(&DocState, &crate::doc::Node) -> Answer) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
with_doc(h, doc, |_, st| match st.doc.node(id) {
|
|
Some(n) => f(st, n),
|
|
None => Err(HostError::Error(STALE_LAYER.into())),
|
|
})
|
|
}
|
|
|
|
/// Runs `id` with `params` and the layer's id on its document, unless `unchanged` says the layer
|
|
/// already has what they set.
|
|
fn set_layer(h: &Host, a: &[Data], command_id: &str, unchanged: impl FnOnce(&crate::doc::Node) -> bool, params: Value) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
let same = with_doc(h, doc, |_, st| st.doc.node(id).is_some_and(unchanged));
|
|
if !same {
|
|
let mut p = params;
|
|
if let Some(o) = p.as_object_mut() {
|
|
o.insert("ids".into(), json!([id.0]));
|
|
}
|
|
on_doc(h, doc.uid, true, |h| command(h, command_id, &p))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn layer_flags(n: &crate::doc::Node) -> (bool, bool, Option<u8>, crate::doc::LayerColor) {
|
|
match &n.kind {
|
|
NodeKind::Layer { printable, preview, dim_images, color, .. } => (*printable, *preview, *dim_images, *color),
|
|
_ => (true, true, None, crate::doc::LayerColor::Preset(0)),
|
|
}
|
|
}
|
|
|
|
fn layer_name(h: &Host, a: &[Data]) -> Answer {
|
|
read_layer(h, a, |_, n| Ok(text(n.name.clone().unwrap_or_default())))
|
|
}
|
|
|
|
/// A name a script assigns: a string, or a number or boolean as `String()` writes it.
|
|
fn name_text(d: &Data) -> Result<String> {
|
|
match d {
|
|
Data::String(s) => Ok(s.clone()),
|
|
Data::Number(n) => Ok(vectorcraft_js::number_to_string(*n)),
|
|
Data::Bool(b) => Ok(b.to_string()),
|
|
_ => Err(HostError::Type("name must be a string".into())),
|
|
}
|
|
}
|
|
|
|
fn layer_set_name(h: &Host, a: &[Data]) -> Answer {
|
|
let name = name_text(arg(a, 1))?;
|
|
if name.trim().is_empty() {
|
|
return Err(HostError::Range("a layer's name cannot be empty".into()));
|
|
}
|
|
if name.chars().count() > MAX_LAYER_NAME {
|
|
return Err(HostError::Range(format!("a layer's name has at most {MAX_LAYER_NAME} characters")));
|
|
}
|
|
let params = json!({"name": &name});
|
|
set_layer(h, a, "layer.setProps", |n| n.name.as_deref() == Some(name.as_str()), params)
|
|
}
|
|
|
|
fn layer_visible(h: &Host, a: &[Data]) -> Answer {
|
|
read_layer(h, a, |_, n| Ok(Data::Bool(n.visible)))
|
|
}
|
|
|
|
fn layer_set_visible(h: &Host, a: &[Data]) -> Answer {
|
|
let v = boolean(arg(a, 1), "visible")?;
|
|
set_layer(h, a, "layer.setProps", |n| n.visible == v, json!({"visible": v}))
|
|
}
|
|
|
|
fn layer_locked(h: &Host, a: &[Data]) -> Answer {
|
|
read_layer(h, a, |_, n| Ok(Data::Bool(n.locked)))
|
|
}
|
|
|
|
fn layer_set_locked(h: &Host, a: &[Data]) -> Answer {
|
|
let v = boolean(arg(a, 1), "locked")?;
|
|
set_layer(h, a, "layer.setProps", |n| n.locked == v, json!({"locked": v}))
|
|
}
|
|
|
|
fn layer_printable(h: &Host, a: &[Data]) -> Answer {
|
|
read_layer(h, a, |_, n| Ok(Data::Bool(layer_flags(n).0)))
|
|
}
|
|
|
|
fn layer_set_printable(h: &Host, a: &[Data]) -> Answer {
|
|
let v = boolean(arg(a, 1), "printable")?;
|
|
set_layer(h, a, "layer.setProps", |n| layer_flags(n).0 == v, json!({"printable": v}))
|
|
}
|
|
|
|
fn layer_preview(h: &Host, a: &[Data]) -> Answer {
|
|
read_layer(h, a, |_, n| Ok(Data::Bool(layer_flags(n).1)))
|
|
}
|
|
|
|
fn layer_set_preview(h: &Host, a: &[Data]) -> Answer {
|
|
let v = boolean(arg(a, 1), "preview")?;
|
|
set_layer(h, a, "layer.setProps", |n| layer_flags(n).1 == v, json!({"preview": v}))
|
|
}
|
|
|
|
fn layer_dim(h: &Host, a: &[Data]) -> Answer {
|
|
read_layer(h, a, |_, n| Ok(Data::Bool(layer_flags(n).2.is_some())))
|
|
}
|
|
|
|
fn layer_set_dim(h: &Host, a: &[Data]) -> Answer {
|
|
let v = boolean(arg(a, 1), "dimPlacedImages")?;
|
|
set_layer(h, a, "layer.setProps", |n| layer_flags(n).2.is_some() == v, json!({"dimImages": v}))
|
|
}
|
|
|
|
fn layer_opacity(h: &Host, a: &[Data]) -> Answer {
|
|
read_layer(h, a, |_, n| Ok(num(reported_opacity(n.opacity))))
|
|
}
|
|
|
|
fn layer_set_opacity(h: &Host, a: &[Data]) -> Answer {
|
|
let v = number(arg(a, 1), "opacity")?;
|
|
if !(0.0..=100.0).contains(&v) {
|
|
return Err(HostError::Range("opacity must be from 0 to 100".into()));
|
|
}
|
|
// The number the getter reports, or one that stores as the opacity the layer has, leaves the
|
|
// layer as it is.
|
|
set_layer(h, a, "object.setProps", |n| reported_opacity(n.opacity) == v || n.opacity == (v / 100.0) as f32, json!({"opacity": v}))
|
|
}
|
|
|
|
fn layer_color(h: &Host, a: &[Data]) -> Answer {
|
|
read_layer(h, a, |_, n| {
|
|
let [r, g, b] = layer_flags(n).3.rgb();
|
|
Ok(super::colors::color_object("RGBColor", &[("red", f64::from(r)), ("green", f64::from(g)), ("blue", f64::from(b))]))
|
|
})
|
|
}
|
|
|
|
/// `color = {red, green, blue}`: each a whole number from 0 to 255.
|
|
fn layer_set_color(h: &Host, a: &[Data]) -> Answer {
|
|
let Data::Object(o) = arg(a, 1) else { return Err(HostError::Type("color must be an object with red, green and blue".into())) };
|
|
let mut rgb = [0u8; 3];
|
|
for (slot, key) in rgb.iter_mut().zip(["red", "green", "blue"]) {
|
|
let v = o.iter().find(|(k, _)| k == key).map(|(_, v)| v).unwrap_or(&Data::Undefined);
|
|
let n = number(v, &format!("color.{key}"))?;
|
|
if n.fract() != 0.0 || !(0.0..=255.0).contains(&n) {
|
|
return Err(HostError::Range(format!("color.{key} must be a whole number from 0 to 255")));
|
|
}
|
|
*slot = u8::try_from(whole(n).unwrap_or(0)).unwrap_or(0);
|
|
}
|
|
let hex = format!("#{:02x}{:02x}{:02x}", rgb[0], rgb[1], rgb[2]);
|
|
set_layer(h, a, "layer.setProps", |n| layer_flags(n).3.rgb() == rgb, json!({"color": hex}))
|
|
}
|
|
|
|
fn layer_parent(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
Ok(with_doc(h, doc, |_, st| match st.doc.parent_of(id) {
|
|
Some(p) => layer_ref(h, st.uid, p),
|
|
None => doc_ref(h, st.uid),
|
|
}))
|
|
}
|
|
|
|
fn layer_to_string(h: &Host, a: &[Data]) -> Answer {
|
|
named_text("Layer", layer_name(h, a))
|
|
}
|
|
|
|
fn layer_typename(h: &Host, a: &[Data]) -> Answer {
|
|
this_layer(h, a)?;
|
|
Ok(text("Layer"))
|
|
}
|
|
|
|
/// The layer's place among the layers beside it, from 1 at the bottom. Art beside a sublayer is
|
|
/// not counted. The list comes from [`layer_ids`], so reading it for each layer of a loop
|
|
/// lists the layers once. Each read is still charged for the search through the list.
|
|
fn layer_z_position(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
let at = with_doc(h, doc, |_, st| {
|
|
let list = layer_ids(h, st, st.doc.parent_of(id))?;
|
|
// The scan is charged by itself: a list kept from an earlier read was charged once.
|
|
h.charge_call(list.len() as u64)?;
|
|
Ok::<_, HostError>(list.iter().position(|l| *l == id).map(|i| list.len() - i))
|
|
})?;
|
|
at.map(|i| num(float(i as u64))).ok_or_else(|| HostError::Error(STALE_LAYER.into()))
|
|
}
|
|
|
|
fn layer_has_selected(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
let selected = with_doc(h, doc, |_, st| {
|
|
// The scan is charged by every selected object it looks at.
|
|
let objects = &st.selection.objects;
|
|
let found = objects.iter().position(|o| st.doc.ancestry(*o).is_some_and(|a| a.contains(&id)));
|
|
h.charge_call(found.map_or(objects.len(), |i| i + 1) as u64)?;
|
|
Ok::<_, HostError>(found.is_some())
|
|
})?;
|
|
Ok(Data::Bool(selected))
|
|
}
|
|
|
|
/// Collects visible, unlocked art through non-template sublayers, bottom first, and counts every
|
|
/// child visited, including skipped children.
|
|
fn collect_editable_art(n: &crate::doc::Node, out: &mut Vec<NodeId>) -> u64 {
|
|
let mut count = 0;
|
|
for child in n.children().into_iter().flatten() {
|
|
count += 1;
|
|
if child.is_ruler_guide() || !child.visible || child.locked || child.is_template() {
|
|
continue;
|
|
}
|
|
if child.is_layer() {
|
|
count += collect_editable_art(child, out);
|
|
} else {
|
|
out.push(child.id);
|
|
}
|
|
}
|
|
count
|
|
}
|
|
|
|
/// `hasSelectedArtwork = false` deselects the layer's art with `select.toggle`; `true` adds its
|
|
/// visible, unlocked art with `select.add`. The rest of the selection, ruler guides included, stays,
|
|
/// and no command runs when there is nothing to deselect or add.
|
|
fn layer_set_has_selected(h: &Host, a: &[Data]) -> Answer {
|
|
let on = boolean(arg(a, 1), "hasSelectedArtwork")?;
|
|
let (doc, id) = this_layer(h, a)?;
|
|
let (ids, over) = with_doc(h, doc, |s, st| {
|
|
let inside = |o: &NodeId| st.doc.ancestry(*o).is_some_and(|a| a.contains(&id));
|
|
let selected = &st.selection.objects;
|
|
h.charge_call(selected.len() as u64)?;
|
|
if !on {
|
|
return Ok::<_, HostError>((selected.iter().copied().filter(inside).collect::<Vec<_>>(), false));
|
|
}
|
|
let locks = st.doc.locked_artboards();
|
|
let mut art = Vec::new();
|
|
let count = st.doc.node(id).map_or(0, |n| collect_editable_art(n, &mut art));
|
|
// The walk is charged by every node it looks at, hidden and locked ones included, whether
|
|
// or not anything is added.
|
|
h.charge_call(count)?;
|
|
// While an object is isolated, the art that holds it is replaced by the isolated object,
|
|
// or by what an isolated group or other container holds, as Select All takes it.
|
|
let isolated = st.isolation.and_then(|i| st.doc.node(i));
|
|
let path = isolated.and_then(|c| st.doc.ancestry(c.id)).unwrap_or_default();
|
|
let editable = path.iter().all(|i| st.doc.node(*i).is_some_and(|n| n.visible && !n.locked && !n.is_template()));
|
|
let mut candidates = Vec::with_capacity(art.len());
|
|
for o in art {
|
|
match isolated {
|
|
Some(_) if path.contains(&o) && !editable => {}
|
|
Some(c) if path.contains(&o) && c.is_container() => {
|
|
// The same walk as the layer's, through any sublayer the container holds.
|
|
let visited = collect_editable_art(c, &mut candidates);
|
|
h.charge_call(visited)?;
|
|
}
|
|
Some(c) if path.contains(&o) => candidates.push(c.id),
|
|
_ => candidates.push(o),
|
|
}
|
|
}
|
|
let mut have: std::collections::HashSet<NodeId> = selected.iter().copied().collect();
|
|
let mut ids = Vec::new();
|
|
for o in candidates {
|
|
if have.contains(&o)
|
|
|| !crate::cmd::select::is_selectable(&st.doc, st.isolation, &locks, o)
|
|
|| (s.guides_locked() && crate::is_guide_path(&st.doc, o))
|
|
{
|
|
continue;
|
|
}
|
|
// Only art that would be added counts toward the limit.
|
|
if selected.len() + ids.len() >= MAX_LISTED {
|
|
return Ok((ids, true));
|
|
}
|
|
have.insert(o);
|
|
ids.push(o);
|
|
}
|
|
Ok((ids, false))
|
|
})?;
|
|
if over {
|
|
return Err(HostError::Range(format!("a selection takes at most {MAX_LISTED} objects")));
|
|
}
|
|
if ids.is_empty() {
|
|
return Ok(Data::Undefined);
|
|
}
|
|
let command_id = if on { "select.add" } else { "select.toggle" };
|
|
on_doc(h, doc.uid, false, |h| command(h, command_id, &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})))?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn layer_is_isolated(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
Ok(Data::Bool(with_doc(h, doc, |_, st| st.isolation == Some(id))))
|
|
}
|
|
|
|
fn layer_remove(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
on_doc(h, doc.uid, true, |h| command(h, "layer.delete", &json!({"ids": [id.0]})))?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// The layers of `parent` (`None`: the top level), without its ruler guides and art.
|
|
fn art_count(d: &Document, parent: Option<NodeId>) -> usize {
|
|
d.children(parent).map_or(0, |c| crate::doc::Node::guide_run_start(c))
|
|
}
|
|
|
|
/// Moves the layer `id` to `index` of `parent` with `node.move`, unless it is there already.
|
|
fn put_layer(h: &Host, doc: Doc, id: NodeId, parent: Option<NodeId>, index: usize) -> Result<()> {
|
|
let now = with_doc(h, doc, |_, st| st.doc.position(id));
|
|
if now.is_some_and(|(p, i, _)| p == parent && i == index) {
|
|
return Ok(());
|
|
}
|
|
let parent = parent.map_or(Value::Null, |p| json!(p.0));
|
|
on_doc(h, doc.uid, true, |h| command(h, "node.move", &json!({"id": id.0, "parent": parent, "index": index})))?;
|
|
Ok(())
|
|
}
|
|
|
|
/// `zOrder(method)`: the layer goes to the top or the bottom of the layers beside it, or one step.
|
|
fn layer_z_order(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
let method = match arg(a, 1) {
|
|
Data::String(s) => s.as_str(),
|
|
_ => "",
|
|
};
|
|
let (parent, index) = with_doc(h, doc, |_, st| {
|
|
let d = &st.doc;
|
|
let (parent, i, _) = d.position(id).ok_or_else(|| HostError::Error(STALE_LAYER.into()))?;
|
|
let floor = usize::from(parent.and_then(|p| d.node(p)).is_some_and(crate::doc::Node::clips));
|
|
let count = art_count(d, parent);
|
|
let last = count.saturating_sub(1);
|
|
// One step passes the next layer beside it, and the art between them.
|
|
let layer_at = |j: &usize| d.children(parent).and_then(|c| c.get(*j)).is_some_and(|n| n.is_layer());
|
|
let to = match method {
|
|
"ZOrderMethod.BRINGTOFRONT" => last,
|
|
"ZOrderMethod.BRINGFORWARD" => (i + 1..count).find(layer_at).unwrap_or(i),
|
|
"ZOrderMethod.SENDBACKWARD" => (floor.min(i)..i).rev().find(layer_at).unwrap_or(i),
|
|
"ZOrderMethod.SENDTOBACK" => floor.min(i),
|
|
_ => return Err(HostError::Type("zOrder needs a ZOrderMethod value".into())),
|
|
};
|
|
Ok((parent, to))
|
|
})?;
|
|
put_layer(h, doc, id, parent, index)?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// `move(relativeObject, insertionLocation?)`: the layer goes into another layer (on top of its
|
|
/// contents, or at the bottom with `PLACEATEND`), to the top level of a document, or directly
|
|
/// above or below another layer.
|
|
fn layer_move(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
let how = placement_of(arg(a, 2))?;
|
|
let target = arg(a, 1);
|
|
let parent = if let Some(handle) = handle_of(target, "Document") {
|
|
let [run, uid] = handle.as_slice() else { return Err(type_error("Document")) };
|
|
let to = find_doc(h, *run, *uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?;
|
|
if to.uid != doc.uid {
|
|
return Err(HostError::Error(super::CROSS_DOCUMENT.into()));
|
|
}
|
|
if matches!(how, Placement::Above | Placement::Below) {
|
|
return Err(HostError::Error("a layer goes above or below another layer, not a document".into()));
|
|
}
|
|
None
|
|
} else if handle_of(target, "Layer").is_some() {
|
|
let (to, t) = layer_of(h, target)?;
|
|
if to.uid != doc.uid {
|
|
return Err(HostError::Error(super::CROSS_DOCUMENT.into()));
|
|
}
|
|
Some(t)
|
|
} else if matches!(target, Data::Undefined) {
|
|
return Err(HostError::Type("move needs a relative object".into()));
|
|
} else {
|
|
return Err(HostError::Type("a layer's relative object must be a layer or a document".into()));
|
|
};
|
|
let (into, index) = with_doc(h, doc, |_, st| {
|
|
let d = &st.doc;
|
|
if let Some(t) = parent
|
|
&& d.ancestry(t).is_some_and(|a| a.contains(&id))
|
|
&& !(t == id && matches!(how, Placement::Above | Placement::Below))
|
|
{
|
|
return Err(HostError::Error("a layer cannot go into itself".into()));
|
|
}
|
|
let (into, beside) = match (how, parent) {
|
|
(Placement::Above | Placement::Below, Some(t)) => (d.parent_of(t), Some(t)),
|
|
(_, p) => (p, None),
|
|
};
|
|
if into.is_some_and(|p| !editable(d, p)) {
|
|
return Err(HostError::Error("the layer is hidden or locked".into()));
|
|
}
|
|
let from = d.position(id);
|
|
let here = |p: Option<NodeId>| from.is_some_and(|(q, ..)| q == p);
|
|
let count = art_count(d, into) - usize::from(here(into));
|
|
let floor = usize::from(into.and_then(|p| d.node(p)).is_some_and(crate::doc::Node::clips) && count > 0);
|
|
let index = match (how, beside) {
|
|
(Placement::Above | Placement::Below, Some(b)) => {
|
|
if b == id {
|
|
return Ok((into, from.map_or(0, |(_, i, _)| i)));
|
|
}
|
|
let (_, mut i, _) = d.position(b).ok_or_else(|| HostError::Error(STALE_LAYER.into()))?;
|
|
if let Some((_, j, _)) = from
|
|
&& here(into)
|
|
&& j < i
|
|
{
|
|
i -= 1;
|
|
}
|
|
if how == Placement::Above { i + 1 } else { i }
|
|
}
|
|
(Placement::Bottom, _) => 0,
|
|
_ => count,
|
|
};
|
|
Ok((into, index.max(floor)))
|
|
})?;
|
|
put_layer(h, doc, id, into, index)?;
|
|
Ok(arg(a, 0).clone())
|
|
}
|
|
|
|
// ---- Layers ----
|
|
|
|
/// The document and the layer (`None`: the top level) a `Layers` object lists.
|
|
fn layers_scope(h: &Host, d: &Data) -> Result<(Doc, Option<NodeId>)> {
|
|
let Some(handle) = handle_of(d, "Layers") else { return Err(type_error("Layers")) };
|
|
match handle.as_slice() {
|
|
[run, uid] => Ok((find_doc(h, *run, *uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?, None)),
|
|
[_, _, _, _, _] => {
|
|
// A collection read before its document was reverted is stale, as its layer is.
|
|
let (doc, id) = layer_of(h, &host_ref("Layer", &handle))?;
|
|
Ok((doc, Some(id)))
|
|
}
|
|
_ => Err(type_error("Layers")),
|
|
}
|
|
}
|
|
|
|
/// The layers of `parent` (`None`: the top level), top first.
|
|
fn layer_list(d: &Document, parent: Option<NodeId>) -> Vec<NodeId> {
|
|
let list = match parent {
|
|
None => Some(&d.layers),
|
|
Some(p) => d.children(Some(p)),
|
|
};
|
|
#[cfg(test)]
|
|
super::LAYER_SCANS.with(|c| c.set(c.get() + list.map_or(0, |l| l.len() as u64)));
|
|
list.into_iter().flatten().rev().filter(|n| n.is_layer()).map(|n| n.id).collect()
|
|
}
|
|
|
|
/// A document's uid, the document as it was, a parent layer (`None`: the top level) and its layers.
|
|
pub(super) type LayerList = (u64, Arc<Document>, Option<NodeId>, Rc<Vec<NodeId>>);
|
|
|
|
/// How many lists of layers [`super::State`] keeps at once.
|
|
pub(super) const LAYER_LISTS: usize = 4;
|
|
|
|
/// [`layer_list`], kept while the document is unchanged for the last [`LAYER_LISTS`] parents, so a
|
|
/// loop over `layers` builds the list once. Each new list is charged by the objects it looks at.
|
|
fn layer_ids(h: &Host, st: &DocState, parent: Option<NodeId>) -> Result<Rc<Vec<NodeId>>> {
|
|
{
|
|
let mut lists = h.dom.layer_lists.borrow_mut();
|
|
if let Some(at) = lists.iter().position(|(uid, doc, p, _)| *uid == st.uid && Arc::ptr_eq(doc, &st.doc) && *p == parent) {
|
|
let list = lists.remove(at);
|
|
let ids = list.3.clone();
|
|
lists.push(list);
|
|
return Ok(ids);
|
|
}
|
|
}
|
|
let looked = match parent {
|
|
None => st.doc.layers.len(),
|
|
Some(p) => st.doc.children(Some(p)).map_or(0, Vec::len),
|
|
};
|
|
h.charge_call(looked as u64)?;
|
|
let ids = Rc::new(layer_list(&st.doc, parent));
|
|
let mut lists = h.dom.layer_lists.borrow_mut();
|
|
lists.retain(|(uid, doc, ..)| *uid != st.uid || Arc::ptr_eq(doc, &st.doc));
|
|
if lists.len() >= LAYER_LISTS {
|
|
lists.remove(0);
|
|
}
|
|
lists.push((st.uid, st.doc.clone(), parent, ids.clone()));
|
|
Ok(ids)
|
|
}
|
|
|
|
fn layers_of(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, id) = this_layer(h, a)?;
|
|
Ok(host_ref("Layers", &super::scoped_handle(h, doc.uid, id)))
|
|
}
|
|
|
|
fn layers_length(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, parent) = layers_scope(h, arg(a, 0))?;
|
|
Ok(num(float(with_doc(h, doc, |_, st| layer_ids(h, st, parent).map(|l| l.len()))? as u64)))
|
|
}
|
|
|
|
fn layers_parent(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, parent) = layers_scope(h, arg(a, 0))?;
|
|
Ok(parent.map_or_else(|| doc_ref(h, doc.uid), |p| layer_ref(h, doc.uid, p)))
|
|
}
|
|
|
|
fn layers_typename(h: &Host, a: &[Data]) -> Answer {
|
|
layers_scope(h, arg(a, 0))?;
|
|
Ok(text("Layers"))
|
|
}
|
|
|
|
/// `layers[i]`, and `layers["name"]` as `getByName` reads it. A number key that is not an array
|
|
/// index, such as `-1`, is looked up as a name, as `pageItems` looks it up.
|
|
fn layers_at(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, parent) = layers_scope(h, arg(a, 0))?;
|
|
if let Data::String(_) = arg(a, 1) {
|
|
return super::by_name_or_probe(a, layers_by_name(h, a));
|
|
}
|
|
if let Some(named) = super::number_named(a) {
|
|
return super::named_or_undefined(layers_by_name(h, &named));
|
|
}
|
|
let Data::Number(n) = arg(a, 1) else { return Ok(Data::Undefined) };
|
|
let Some(i) = super::position(*n) else { return Ok(Data::Undefined) };
|
|
with_doc(h, doc, |_, st| Ok(layer_ids(h, st, parent)?.get(i).map_or(Data::Undefined, |id| layer_ref(h, st.uid, *id))))
|
|
}
|
|
|
|
fn layers_by_name(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, parent) = layers_scope(h, arg(a, 0))?;
|
|
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 = layer_ids(h, st, parent)?;
|
|
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| layer_ref(h, st.uid, *id)).ok_or_else(|| HostError::Error(NOT_FOUND.into()))
|
|
})
|
|
}
|
|
|
|
fn layers_index(h: &Host, a: &[Data]) -> Answer {
|
|
match arg(a, 1) {
|
|
Data::String(_) => layers_by_name(h, a),
|
|
// A number is a position here, never a name ([`layers_at`]).
|
|
Data::Number(n) if super::position(*n).is_none() => Err(HostError::Error(NOT_FOUND.into())),
|
|
Data::Number(_) => match layers_at(h, a)? {
|
|
Data::Undefined => Err(HostError::Error(NOT_FOUND.into())),
|
|
d => Ok(d),
|
|
},
|
|
_ => Err(HostError::Type("give a name or an index".into())),
|
|
}
|
|
}
|
|
|
|
/// `layers.add()`: a new layer above every top-level layer, or a sublayer on top of the layer's
|
|
/// contents. → the new layer, which is the current one.
|
|
fn layers_add(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, parent) = layers_scope(h, arg(a, 0))?;
|
|
if let Some(p) = parent {
|
|
let ok = with_doc(h, doc, |_, st| editable(&st.doc, p));
|
|
if !ok {
|
|
return Err(HostError::Error("the layer is hidden or locked".into()));
|
|
}
|
|
}
|
|
let (_, r) = on_doc_layer(h, doc.uid, true, false, |h| match parent {
|
|
None => command_value(h, "layer.new", &json!({"top": true})),
|
|
Some(p) => command_value(h, "layer.newSublayer", &json!({"parent": p.0})),
|
|
})?;
|
|
let id = result_id(&r, "id").ok_or_else(|| HostError::Error("the layer was not made".into()))?;
|
|
Ok(layer_ref(h, doc.uid, NodeId(id)))
|
|
}
|
|
|
|
/// `layers.removeAll()`: deletes every layer of the collection in one command. A document keeps a
|
|
/// layer, so on the top-level layers it throws before anything is deleted.
|
|
fn layers_remove_all(h: &Host, a: &[Data]) -> Answer {
|
|
let (doc, parent) = layers_scope(h, arg(a, 0))?;
|
|
if parent.is_none() {
|
|
return Err(HostError::Error("a document needs at least one layer".into()));
|
|
}
|
|
let ids = with_doc(h, doc, |_, st| layer_ids(h, st, parent))?;
|
|
// `layer.delete` takes at most this many rows; more is refused before anything is deleted.
|
|
if ids.len() > crate::cmd::layer::MAX_ROWS {
|
|
return Err(HostError::Range(format!("removeAll deletes at most {} layers at once", crate::cmd::layer::MAX_ROWS)));
|
|
}
|
|
if !ids.is_empty() {
|
|
on_doc(h, doc.uid, true, |h| command(h, "layer.delete", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
// ---- GroupItems, CompoundPathItems and a container's PageItems ----
|
|
|
|
/// The document and the layer, group or compound path (`None`: the whole document) an art
|
|
/// collection of `class` lists.
|
|
fn art_scope(h: &Host, d: &Data, class: &str) -> Result<(Doc, Option<NodeId>)> {
|
|
let Some(handle) = handle_of(d, class) else { return Err(type_error(class)) };
|
|
match handle.as_slice() {
|
|
[run, uid] => Ok((find_doc(h, *run, *uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?, None)),
|
|
[_, _, _, _, _] => {
|
|
// A collection read before its document was reverted is stale, as its container is.
|
|
let (doc, id) = super::node_of(h, &handle).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
|
|
let holds = with_doc(h, doc, |_, st| {
|
|
st.doc.node(id).is_some_and(|n| matches!(n.kind, NodeKind::Layer { .. } | NodeKind::Group { .. } | NodeKind::Compound { .. }))
|
|
});
|
|
if !holds {
|
|
return Err(HostError::Error(STALE_ITEM.into()));
|
|
}
|
|
Ok((doc, Some(id)))
|
|
}
|
|
_ => Err(type_error(class)),
|
|
}
|
|
}
|
|
|
|
/// A document's uid, the document as it was, the [`Kind`] and the container of an art collection,
|
|
/// and the ids it listed then.
|
|
pub(super) type ArtList = (u64, Arc<Document>, u8, Option<NodeId>, Rc<Vec<NodeId>>);
|
|
|
|
/// How many lists of art [`super::State`] keeps at once.
|
|
pub(super) const ART_LISTS: usize = 4;
|
|
|
|
/// The objects of `kind` in `container` (directly), topmost first, or in the whole document at any
|
|
/// depth, in the order of the document's `pageItems`. The last [`ART_LISTS`] lists are kept while
|
|
/// their documents are unchanged, so a loop over groups that reads each group's `pageItems` builds
|
|
/// each list once. Each new list is charged by the objects it looks at.
|
|
fn art_list(h: &Host, st: &DocState, kind: Kind, container: Option<NodeId>) -> Result<Rc<Vec<NodeId>>> {
|
|
{
|
|
let mut lists = h.dom.art.borrow_mut();
|
|
let at = lists.iter().position(|(uid, doc, k, c, _)| *uid == st.uid && Arc::ptr_eq(doc, &st.doc) && *k == kind as u8 && *c == container);
|
|
if let Some(at) = at {
|
|
let list = lists.remove(at);
|
|
let ids = list.4.clone();
|
|
lists.push(list);
|
|
return Ok(ids);
|
|
}
|
|
}
|
|
#[cfg(test)]
|
|
super::ART_SCANS.with(|c| c.set(c.get() + 1));
|
|
let ids: Vec<NodeId> = match container {
|
|
None => {
|
|
let all = page_items(h, st)?;
|
|
h.charge_call(all.len() as u64)?;
|
|
all.iter().copied().filter(|i| st.doc.node(*i).is_some_and(|n| kind.holds(n))).collect()
|
|
}
|
|
Some(c) => {
|
|
let children: &[Arc<crate::doc::Node>] = st.doc.node(c).and_then(|n| n.children()).map_or(&[], Vec::as_slice);
|
|
h.charge_call(children.len() as u64)?;
|
|
children.iter().rev().filter(|n| kind.holds(n)).map(|n| n.id).collect()
|
|
}
|
|
};
|
|
let ids = Rc::new(ids);
|
|
let mut lists = h.dom.art.borrow_mut();
|
|
lists.retain(|(uid, doc, ..)| *uid != st.uid || Arc::ptr_eq(doc, &st.doc));
|
|
if lists.len() >= ART_LISTS {
|
|
lists.remove(0);
|
|
}
|
|
lists.push((st.uid, st.doc.clone(), kind as u8, container, ids.clone()));
|
|
Ok(ids)
|
|
}
|
|
|
|
/// The `PageItems` of a layer, group or compound path: a [`super`] `PageItems` object whose handle
|
|
/// names the container.
|
|
pub(super) fn is_scoped(a: &[Data]) -> bool {
|
|
host_ref_parts(arg(a, 0)).is_some_and(|(c, handle)| c == "PageItems" && handle.len() == 5)
|
|
}
|
|
|
|
fn collection(h: &Host, kind: Kind, container: Option<NodeId>, uid: u64) -> Data {
|
|
match container {
|
|
Some(c) => host_ref(kind.class(), &super::scoped_handle(h, uid, c)),
|
|
None => host_ref(kind.class(), &[float(h.run), float(uid)]),
|
|
}
|
|
}
|
|
|
|
fn art_length(h: &Host, a: &[Data], kind: Kind) -> Answer {
|
|
let (doc, c) = art_scope(h, arg(a, 0), kind.class())?;
|
|
let n = with_doc(h, doc, |_, st| art_list(h, st, kind, c).map(|l| l.len()))?;
|
|
Ok(num(float(n as u64)))
|
|
}
|
|
|
|
fn art_parent(h: &Host, a: &[Data], kind: Kind) -> Answer {
|
|
let (doc, c) = art_scope(h, arg(a, 0), kind.class())?;
|
|
Ok(with_doc(h, doc, |_, st| c.map_or_else(|| doc_ref(h, st.uid), |c| item_ref(h, st, c))))
|
|
}
|
|
|
|
/// `collection[i]`, and `collection["name"]` as `getByName` reads it. A number key that is not an
|
|
/// array index, such as `-1`, is looked up as a name, as the document's `pageItems` looks it up.
|
|
fn art_at(h: &Host, a: &[Data], kind: Kind) -> Answer {
|
|
let (doc, c) = art_scope(h, arg(a, 0), kind.class())?;
|
|
if let Data::String(_) = arg(a, 1) {
|
|
return super::by_name_or_probe(a, art_by_name(h, a, kind));
|
|
}
|
|
if let Some(named) = super::number_named(a) {
|
|
return super::named_or_undefined(art_by_name(h, &named, kind));
|
|
}
|
|
let Data::Number(n) = arg(a, 1) else { return Ok(Data::Undefined) };
|
|
let Some(i) = super::position(*n) else { return Ok(Data::Undefined) };
|
|
with_doc(h, doc, |_, st| Ok(art_list(h, st, kind, c)?.get(i).map_or(Data::Undefined, |id| item_ref(h, st, *id))))
|
|
}
|
|
|
|
fn art_by_name(h: &Host, a: &[Data], kind: Kind) -> Answer {
|
|
let (doc, c) = art_scope(h, arg(a, 0), kind.class())?;
|
|
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 = art_list(h, st, kind, c)?;
|
|
// The scan is charged by itself: a list kept from an earlier read was charged once.
|
|
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 art_index(h: &Host, a: &[Data], kind: Kind) -> Answer {
|
|
match arg(a, 1) {
|
|
Data::String(_) => art_by_name(h, a, kind),
|
|
// A number is a position here, never a name ([`art_at`]). A stale collection throws its
|
|
// stale error first.
|
|
Data::Number(n) if super::position(*n).is_none() => {
|
|
art_scope(h, arg(a, 0), kind.class())?;
|
|
Err(HostError::Error(NOT_FOUND.into()))
|
|
}
|
|
Data::Number(_) => match art_at(h, a, kind)? {
|
|
Data::Undefined => Err(HostError::Error(NOT_FOUND.into())),
|
|
d => Ok(d),
|
|
},
|
|
_ => Err(HostError::Type("give a name or an index".into())),
|
|
}
|
|
}
|
|
|
|
/// `removeAll()`: deletes every object the collection lists (those inside another listed one go
|
|
/// with it) in one command. A hidden or locked one, one in a hidden or locked layer or group, or
|
|
/// one on a locked artboard stops it before anything is deleted, as the document's
|
|
/// `pageItems.removeAll()` is stopped, and so does an image the deletion would leave first in an
|
|
/// ordinary group named as a tracing.
|
|
fn art_remove_all(h: &Host, a: &[Data], kind: Kind) -> Answer {
|
|
let (doc, c) = art_scope(h, arg(a, 0), kind.class())?;
|
|
let (ids, refused, on_locked, masks) = with_doc(h, doc, |_, st| {
|
|
let all = art_list(h, st, kind, c)?;
|
|
let d = &st.doc;
|
|
let listed: std::collections::HashSet<NodeId> = all.iter().copied().collect();
|
|
let ids: Vec<NodeId> =
|
|
all.iter().copied().filter(|i| !d.ancestry(*i).is_some_and(|a| a.iter().any(|x| *x != *i && listed.contains(x)))).collect();
|
|
let whole = |id: &NodeId| editable(d, *id) && d.node(*id).is_some_and(super::all_editable);
|
|
let refused: Vec<u64> = ids.iter().filter(|i| !whole(i)).map(|i| i.0).take(20).collect();
|
|
let locks = d.locked_artboards();
|
|
let on_locked: Vec<u64> = ids.iter().filter(|i| locks.holds_id(**i)).map(|i| i.0).take(20).collect();
|
|
let masks = kept_clipping_paths(d, &ids);
|
|
Ok::<_, HostError>((ids, refused, on_locked, masks))
|
|
})?;
|
|
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}")));
|
|
}
|
|
refuse_kept_clipping_paths(&masks)?;
|
|
// Deleting the objects above an image in an ordinary group named as a tracing would make it one.
|
|
if with_doc(h, doc, |_, st| super::removal_makes_tracing(&st.doc, &ids)) {
|
|
return Err(HostError::Error(super::NOT_A_TRACING.into()));
|
|
}
|
|
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<_>>(), "keepEmpty": true})))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// The ids (at most 20) among `ids` that are the clipping path of a group or layer that keeps other
|
|
/// objects once `ids` are deleted. Deleting one would leave the next object clipping the rest.
|
|
/// Deleting every path of a compound path would leave it empty, so a compound path that clips
|
|
/// counts as deleted too.
|
|
pub(super) fn kept_clipping_paths(d: &Document, ids: &[NodeId]) -> Vec<u64> {
|
|
let mut going: std::collections::HashSet<NodeId> = ids.iter().copied().collect();
|
|
let mut seen = std::collections::HashSet::new();
|
|
let mut emptied: Vec<NodeId> = Vec::new();
|
|
for p in ids.iter().filter_map(|i| d.parent_of(*i)) {
|
|
if going.contains(&p) || !seen.insert(p) {
|
|
continue;
|
|
}
|
|
let all_go = |n: &crate::doc::Node| n.children().is_some_and(|k| k.iter().all(|k| going.contains(&k.id)));
|
|
if d.node(p).is_some_and(|n| matches!(n.kind, NodeKind::Compound { .. }) && all_go(n)) {
|
|
emptied.push(p);
|
|
}
|
|
}
|
|
going.extend(emptied.iter().copied());
|
|
ids.iter()
|
|
.chain(&emptied)
|
|
.filter(|i| {
|
|
d.parent_of(**i).and_then(|p| d.node(p)).is_some_and(|p| {
|
|
let kids = p.children().map_or(&[][..], Vec::as_slice);
|
|
p.clips()
|
|
&& kids.first().is_some_and(|f| f.id == **i)
|
|
&& kids.iter().any(|k| !going.contains(&k.id) && !matches!(k.kind, NodeKind::RulerGuide { .. }))
|
|
})
|
|
})
|
|
.map(|i| i.0)
|
|
.take(20)
|
|
.collect()
|
|
}
|
|
|
|
/// The error of a `removeAll()` that would delete the clipping paths `masks`, or `Ok` when there
|
|
/// are none.
|
|
pub(super) fn refuse_kept_clipping_paths(masks: &[u64]) -> std::result::Result<(), HostError> {
|
|
if masks.is_empty() {
|
|
return Ok(());
|
|
}
|
|
let list = masks.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
|
|
Err(HostError::Error(format!(
|
|
"cannot delete a clipping path while its group or layer keeps other objects; release the clipping mask first: {list}"
|
|
)))
|
|
}
|
|
|
|
/// The scoped `PageItems` of a container, which `PageItems`' members hand over to when their
|
|
/// handle names one ([`is_scoped`]).
|
|
pub(super) fn scoped_length(h: &Host, a: &[Data]) -> Answer {
|
|
art_length(h, a, Kind::Any)
|
|
}
|
|
|
|
pub(super) fn scoped_parent(h: &Host, a: &[Data]) -> Answer {
|
|
art_parent(h, a, Kind::Any)
|
|
}
|
|
|
|
pub(super) fn scoped_at(h: &Host, a: &[Data]) -> Answer {
|
|
art_at(h, a, Kind::Any)
|
|
}
|
|
|
|
pub(super) fn scoped_by_name(h: &Host, a: &[Data]) -> Answer {
|
|
art_by_name(h, a, Kind::Any)
|
|
}
|
|
|
|
pub(super) fn scoped_index(h: &Host, a: &[Data]) -> Answer {
|
|
art_index(h, a, Kind::Any)
|
|
}
|
|
|
|
pub(super) fn scoped_remove_all(h: &Host, a: &[Data]) -> Answer {
|
|
art_remove_all(h, a, Kind::Any)
|
|
}
|
|
|
|
fn groups_length(h: &Host, a: &[Data]) -> Answer {
|
|
art_length(h, a, Kind::Group)
|
|
}
|
|
fn groups_parent(h: &Host, a: &[Data]) -> Answer {
|
|
art_parent(h, a, Kind::Group)
|
|
}
|
|
fn groups_at(h: &Host, a: &[Data]) -> Answer {
|
|
art_at(h, a, Kind::Group)
|
|
}
|
|
fn groups_by_name(h: &Host, a: &[Data]) -> Answer {
|
|
art_by_name(h, a, Kind::Group)
|
|
}
|
|
fn groups_index(h: &Host, a: &[Data]) -> Answer {
|
|
art_index(h, a, Kind::Group)
|
|
}
|
|
fn groups_remove_all(h: &Host, a: &[Data]) -> Answer {
|
|
art_remove_all(h, a, Kind::Group)
|
|
}
|
|
fn compounds_length(h: &Host, a: &[Data]) -> Answer {
|
|
art_length(h, a, Kind::Compound)
|
|
}
|
|
fn compounds_parent(h: &Host, a: &[Data]) -> Answer {
|
|
art_parent(h, a, Kind::Compound)
|
|
}
|
|
fn compounds_at(h: &Host, a: &[Data]) -> Answer {
|
|
art_at(h, a, Kind::Compound)
|
|
}
|
|
fn compounds_by_name(h: &Host, a: &[Data]) -> Answer {
|
|
art_by_name(h, a, Kind::Compound)
|
|
}
|
|
fn compounds_index(h: &Host, a: &[Data]) -> Answer {
|
|
art_index(h, a, Kind::Compound)
|
|
}
|
|
fn compounds_remove_all(h: &Host, a: &[Data]) -> Answer {
|
|
art_remove_all(h, a, Kind::Compound)
|
|
}
|
|
|
|
fn art_typename(h: &Host, a: &[Data], kind: Kind) -> Answer {
|
|
art_scope(h, arg(a, 0), kind.class())?;
|
|
Ok(text(kind.class()))
|
|
}
|
|
|
|
pub(super) fn scoped_typename(h: &Host, a: &[Data]) -> Answer {
|
|
art_typename(h, a, Kind::Any)
|
|
}
|
|
fn groups_typename(h: &Host, a: &[Data]) -> Answer {
|
|
art_typename(h, a, Kind::Group)
|
|
}
|
|
fn compounds_typename(h: &Host, a: &[Data]) -> Answer {
|
|
art_typename(h, a, Kind::Compound)
|
|
}
|
|
|
|
/// `groupItems.add()` and `compoundPathItems.add()`: an empty group or compound path on top of the
|
|
/// layer's or group's contents (the document's: the current layer's), with `object.newGroup`. A
|
|
/// Live Paint group takes none. → the new object.
|
|
fn container_add(h: &Host, a: &[Data], kind: Kind) -> Answer {
|
|
let (doc, c) = art_scope(h, arg(a, 0), kind.class())?;
|
|
if let Some(c) = c {
|
|
let (compound, live) = with_doc(h, doc, |_, st| {
|
|
(st.doc.node(c).is_some_and(|n| matches!(n.kind, NodeKind::Compound { .. })), super::paths::is_live_paint_group(st, c))
|
|
});
|
|
if compound {
|
|
return Err(HostError::Error("a compound path holds only paths".into()));
|
|
}
|
|
if live {
|
|
return Err(HostError::Error(super::paths::LIVE_PAINT_CONTAINER.into()));
|
|
}
|
|
}
|
|
let mut params = c.map_or_else(|| json!({}), |c| json!({"parent": c.0}));
|
|
if kind == Kind::Compound
|
|
&& let Some(o) = params.as_object_mut()
|
|
{
|
|
o.insert("compound".into(), json!(true));
|
|
}
|
|
let (_, r) = on_doc(h, doc.uid, true, |h| command_value(h, "object.newGroup", ¶ms))?;
|
|
let id = result_id(&r, "id").ok_or_else(|| HostError::Error("the object was not made".into()))?;
|
|
let s = h.session.borrow();
|
|
let st = s.documents().iter().find(|d| d.uid == doc.uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?;
|
|
Ok(item_ref(h, st, NodeId(id)))
|
|
}
|
|
|
|
fn groups_add(h: &Host, a: &[Data]) -> Answer {
|
|
container_add(h, a, Kind::Group)
|
|
}
|
|
|
|
fn compounds_add(h: &Host, a: &[Data]) -> Answer {
|
|
container_add(h, a, Kind::Compound)
|
|
}
|
|
|
|
// ---- The art collections of a document, a layer, a group and a compound path ----
|
|
|
|
/// The collection of `kind` the object `this` holds: a document's (`Document`), a layer's
|
|
/// (`Layer`) or a page item's.
|
|
fn held(h: &Host, a: &[Data], kind: Kind) -> Answer {
|
|
let this = arg(a, 0);
|
|
if handle_of(this, "Document").is_some() {
|
|
let doc = doc_of(h, this, "Document")?;
|
|
return Ok(collection(h, kind, None, doc.uid));
|
|
}
|
|
if handle_of(this, "Layer").is_some() {
|
|
let (doc, id) = layer_of(h, this)?;
|
|
return Ok(collection(h, kind, Some(id), doc.uid));
|
|
}
|
|
let item = item_of(h, this)?;
|
|
Ok(collection(h, kind, Some(item.id), item.doc.uid))
|
|
}
|
|
|
|
fn held_page_items(h: &Host, a: &[Data]) -> Answer {
|
|
held(h, a, Kind::Any)
|
|
}
|
|
|
|
fn held_groups(h: &Host, a: &[Data]) -> Answer {
|
|
held(h, a, Kind::Group)
|
|
}
|
|
|
|
fn held_compounds(h: &Host, a: &[Data]) -> Answer {
|
|
held(h, a, Kind::Compound)
|
|
}
|
|
|
|
/// A layer's or group's `pathItems`: the paths directly in it ([`super::paths`]).
|
|
fn held_paths(h: &Host, a: &[Data]) -> Answer {
|
|
let this = arg(a, 0);
|
|
if handle_of(this, "Layer").is_some() {
|
|
let (doc, id) = layer_of(h, this)?;
|
|
return Ok(super::paths::held_path_items(h, doc.uid, id));
|
|
}
|
|
let item = this_item_of(h, a, "GroupItem")?;
|
|
Ok(super::paths::held_path_items(h, item.doc.uid, item.id))
|
|
}
|
|
|
|
/// A compound path's `pathItems`: its paths, which `add()` and the shape methods make inside it
|
|
/// ([`super::paths`]).
|
|
fn compound_paths(h: &Host, a: &[Data]) -> Answer {
|
|
let item = this_item_of(h, a, "CompoundPathItem")?;
|
|
Ok(super::paths::held_path_items(h, item.doc.uid, item.id))
|
|
}
|
|
|
|
fn held_text(h: &Host, a: &[Data]) -> Answer {
|
|
held(h, a, Kind::Text)
|
|
}
|
|
|
|
/// The document and the layer or group (`None`: the whole document) a `TextFrames` lists.
|
|
pub(super) fn text_scope(h: &Host, d: &Data) -> Result<(Doc, Option<NodeId>)> {
|
|
art_scope(h, d, Kind::Text.class())
|
|
}
|
|
|
|
/// The type objects directly in `container`, topmost first.
|
|
pub(super) fn text_list(h: &Host, st: &DocState, container: NodeId) -> Result<Rc<Vec<NodeId>>> {
|
|
art_list(h, st, Kind::Text, Some(container))
|
|
}
|
|
|
|
// ---- GroupItem and CompoundPathItem ----
|
|
|
|
fn this_item_of(h: &Host, a: &[Data], what: &str) -> Result<Item> {
|
|
let item = item_of(h, arg(a, 0))?;
|
|
let ok = with_doc(h, item.doc, |_, st| st.doc.node(item.id).and_then(typename) == Some(what));
|
|
if !ok {
|
|
return Err(type_error(what));
|
|
}
|
|
Ok(item)
|
|
}
|
|
|
|
/// The layer the object is in.
|
|
fn item_layer(h: &Host, a: &[Data]) -> Answer {
|
|
let item = item_of(h, arg(a, 0))?;
|
|
Ok(with_doc(h, item.doc, |_, st| st.doc.layer_containing(item.id).map_or(Data::Null, |l| layer_ref(h, st.uid, l))))
|
|
}
|
|
|
|
fn group_clipped(h: &Host, a: &[Data]) -> Answer {
|
|
let item = this_item_of(h, a, "GroupItem")?;
|
|
Ok(Data::Bool(with_doc(h, item.doc, |_, st| st.doc.node(item.id).is_some_and(crate::doc::Node::clips))))
|
|
}
|
|
|
|
/// `clipped = true`: the group's topmost object becomes its clipping path; `false` releases the
|
|
/// clipping mask. The group keeps its id either way. A hidden or locked topmost object is refused
|
|
/// before it loses its paint.
|
|
fn group_set_clipped(h: &Host, a: &[Data]) -> Answer {
|
|
let on = boolean(arg(a, 1), "clipped")?;
|
|
let item = this_item_of(h, a, "GroupItem")?;
|
|
let (now, ok, top) = with_doc(h, item.doc, |_, st| {
|
|
let d = &st.doc;
|
|
let top = d.node(item.id).and_then(|n| n.children()).and_then(|c| c.iter().rev().find(|c| !c.is_ruler_guide())).map(|c| c.id);
|
|
(d.node(item.id).is_some_and(crate::doc::Node::clips), editable(d, item.id), top.filter(|t| !editable(d, *t)))
|
|
});
|
|
if now == on {
|
|
return Ok(Data::Undefined);
|
|
}
|
|
super::refuse_protected_edit(h, item)?;
|
|
if !ok {
|
|
return Err(HostError::Error(format!("Cannot change GroupItem {}: it is hidden or locked", item.id.0)));
|
|
}
|
|
if let Some(t) = top.filter(|_| on) {
|
|
return Err(HostError::Error(format!("Cannot clip GroupItem {}: its topmost object {} is hidden or locked", item.id.0, t.0)));
|
|
}
|
|
on_doc(h, item.doc.uid, true, |h| command(h, "layer.clippingMask.toggle", &json!({"id": item.id.0})))?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// The members `layers.rs` adds to `PageItem`'s.
|
|
pub(super) fn page_item_members(l: &Link) -> Vec<Described> {
|
|
named(vec![("layer", get(l, item_layer), typed("Layer"))])
|
|
}
|
|
|
|
/// The members a page item class adds to `PageItem`'s.
|
|
pub(super) fn members(class: &str, l: &Link) -> Vec<Described> {
|
|
match class {
|
|
"GroupItem" => {
|
|
let mut m = named(vec![
|
|
("clipped", property(l, group_clipped, group_set_clipped), typed("boolean")),
|
|
("pageItems", get(l, held_page_items), listed("PageItems")),
|
|
("groupItems", get(l, held_groups), listed("GroupItems")),
|
|
("compoundPathItems", get(l, held_compounds), listed("CompoundPathItems")),
|
|
("pathItems", get(l, held_paths), listed("PathItems")),
|
|
("textFrames", get(l, held_text), listed("TextFrames")),
|
|
]);
|
|
m.extend(["symbolItems", "placedItems", "rasterItems"].map(|m| later(l, "GroupItem", m)));
|
|
m
|
|
}
|
|
"CompoundPathItem" => named(vec![("pathItems", get(l, compound_paths), listed("PathItems"))]),
|
|
_ => Vec::new(),
|
|
}
|
|
}
|
|
|
|
/// What `reflect` tells of a member whose value is a collection of class `data_type`.
|
|
fn listed(data_type: &str) -> MemberInfo {
|
|
MemberInfo { collection: true, ..typed(data_type) }
|
|
}
|
|
|
|
// ---- Document ----
|
|
|
|
fn doc_layers(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = doc_of(h, arg(a, 0), "Document")?;
|
|
Ok(host_ref("Layers", &[float(h.run), float(doc.uid)]))
|
|
}
|
|
|
|
fn doc_active_layer(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = doc_of(h, arg(a, 0), "Document")?;
|
|
Ok(with_doc(h, doc, |_, st| st.current_layer().or_else(|| st.doc.default_layer()).map_or(Data::Null, |l| layer_ref(h, st.uid, l))))
|
|
}
|
|
|
|
/// `activeLayer = layer`: the layer becomes the current one, where new art goes.
|
|
fn doc_set_active_layer(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = doc_of(h, arg(a, 0), "Document")?;
|
|
let (to, id) = layer_of(h, arg(a, 1))?;
|
|
if to.uid != doc.uid {
|
|
return Err(HostError::Error("the layer is in another document".into()));
|
|
}
|
|
let now = with_doc(h, doc, |_, st| st.current_layer());
|
|
if now != Some(id) {
|
|
on_doc_layer(h, doc.uid, true, false, |h| command(h, "layer.setCurrent", &json!({"id": id.0})))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn doc_artboards(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = doc_of(h, arg(a, 0), "Document")?;
|
|
Ok(host_ref("Artboards", &[float(h.run), float(doc.uid)]))
|
|
}
|
|
|
|
/// The members `layers.rs` adds to `Document`'s.
|
|
pub(super) fn document_members(l: &Link) -> Vec<Described> {
|
|
named(vec![
|
|
("layers", get(l, doc_layers), listed("Layers")),
|
|
("activeLayer", property(l, doc_active_layer, doc_set_active_layer), typed("Layer")),
|
|
("groupItems", get(l, held_groups), listed("GroupItems")),
|
|
("compoundPathItems", get(l, held_compounds), listed("CompoundPathItems")),
|
|
("artboards", get(l, doc_artboards), listed("Artboards")),
|
|
])
|
|
}
|
|
|
|
// ---- Artboard and Artboards ----
|
|
|
|
/// The document of an artboard.
|
|
#[derive(Clone, Copy)]
|
|
struct Board {
|
|
doc: Doc,
|
|
}
|
|
|
|
fn board_index(st: &DocState, id: u32) -> Option<usize> {
|
|
st.doc.artboards.iter().position(|a| a.id == id)
|
|
}
|
|
|
|
/// The artboard `d` names. An artboard read before its document was reverted is stale, as a page
|
|
/// item is: the loaded document's artboard with the same id is another artboard.
|
|
fn board_of(h: &Host, d: &Data) -> Result<(Board, usize)> {
|
|
let Some(handle) = handle_of(d, "Artboard") else { return Err(type_error("Artboard")) };
|
|
let [run, uid, id, reverts] = handle.as_slice() else { return Err(type_error("Artboard")) };
|
|
let doc = find_doc(h, *run, *uid).ok_or_else(|| HostError::Error(STALE_ARTBOARD.into()))?;
|
|
let id = whole(*id).and_then(|i| u32::try_from(i).ok()).ok_or_else(|| HostError::Error(STALE_ARTBOARD.into()))?;
|
|
let index = with_doc(h, doc, |_, st| board_index(st, id).filter(|_| whole(*reverts) == Some(st.reverts)))
|
|
.ok_or_else(|| HostError::Error(STALE_ARTBOARD.into()))?;
|
|
Ok((Board { doc }, index))
|
|
}
|
|
|
|
/// An `Artboard` for the artboard `id` of the document `uid`, with the document's revert count.
|
|
fn board_ref(h: &Host, uid: u64, id: u32) -> Data {
|
|
let reverts = h.session.borrow().documents().iter().find(|d| d.uid == uid).map_or(0, |d| d.reverts);
|
|
host_ref("Artboard", &[float(h.run), float(uid), f64::from(id), float(reverts)])
|
|
}
|
|
|
|
fn this_board(h: &Host, a: &[Data]) -> Result<(Board, usize)> {
|
|
board_of(h, arg(a, 0))
|
|
}
|
|
|
|
/// Reads the artboard `this` names with `f`.
|
|
fn read_board(h: &Host, a: &[Data], f: impl FnOnce(&DocState, &crate::doc::Artboard, usize) -> Answer) -> Answer {
|
|
let (b, i) = this_board(h, a)?;
|
|
with_doc(h, b.doc, |_, st| match st.doc.artboards.get(i) {
|
|
Some(ab) => f(st, ab, i),
|
|
None => Err(HostError::Error(STALE_ARTBOARD.into())),
|
|
})
|
|
}
|
|
|
|
/// Runs `artboard.setProps` with `props` for the artboard, unless `unchanged` says it has them.
|
|
fn set_board(h: &Host, a: &[Data], unchanged: impl FnOnce(&crate::doc::Artboard) -> bool, props: Value) -> Answer {
|
|
let (b, i) = this_board(h, a)?;
|
|
let same = with_doc(h, b.doc, |_, st| st.doc.artboards.get(i).is_some_and(unchanged));
|
|
if !same {
|
|
let mut p = props;
|
|
if let Some(o) = p.as_object_mut() {
|
|
o.insert("index".into(), json!(i));
|
|
}
|
|
on_doc(h, b.doc.uid, true, |h| command(h, "artboard.setProps", &p))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn board_rect(h: &Host, a: &[Data]) -> Answer {
|
|
read_board(h, a, |st, ab, _| Ok(frame(st)?.bounds(h.dom.coords.get(), ab.rect)))
|
|
}
|
|
|
|
/// A rectangle `[left, top, right, bottom]` a script gave, in document coordinates, checked: finite,
|
|
/// inside the canvas, and from 1 to [`super::MAX_SIZE`] points on each side, the sides the artboard
|
|
/// commands keep.
|
|
fn rect_arg(h: &Host, st: &DocState, d: &Data, what: &str) -> Result<vectorcraft_geom::Rect> {
|
|
let Data::Array(v) = d else { return Err(HostError::Type(format!("{what} must be an array of four numbers"))) };
|
|
let [l, t, r, b] = v.as_slice() else { return Err(HostError::Type(format!("{what} must be an array of four numbers"))) };
|
|
let n = |d: &Data| super::coordinate(d, what);
|
|
let (l, t, r, b) = (n(l)?, n(t)?, n(r)?, n(b)?);
|
|
let f = frame(st)?;
|
|
let c = h.dom.coords.get();
|
|
let p = f.to_document(c, [l, t]);
|
|
let q = f.to_document(c, [r, b]);
|
|
let rect = vectorcraft_geom::Rect::new(p.x.min(q.x), p.y.min(q.y), p.x.max(q.x), p.y.max(q.y));
|
|
if q.x <= p.x || q.y <= p.y {
|
|
return Err(HostError::Range(format!("{what} must have its right side right of its left side and its bottom below its top")));
|
|
}
|
|
if rect.width() < 1.0 || rect.height() < 1.0 {
|
|
return Err(HostError::Range(format!("{what} must be at least 1 point wide and tall")));
|
|
}
|
|
if rect.width() > super::MAX_SIZE || rect.height() > super::MAX_SIZE {
|
|
return Err(HostError::Range(format!("{what} must be at most 3999856 points wide and tall")));
|
|
}
|
|
if [rect.x0, rect.y0, rect.x1, rect.y1].iter().any(|v| v.abs() > crate::MAX_COORD) {
|
|
return Err(HostError::Range(format!("{what} is outside the canvas")));
|
|
}
|
|
Ok(rect)
|
|
}
|
|
|
|
fn board_set_rect(h: &Host, a: &[Data]) -> Answer {
|
|
let (b, _) = this_board(h, a)?;
|
|
let r = with_doc(h, b.doc, |_, st| rect_arg(h, st, arg(a, 1), "artboardRect"))?;
|
|
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
|
|
set_board(
|
|
h,
|
|
a,
|
|
|ab| close(ab.rect.x0, r.x0) && close(ab.rect.y0, r.y0) && close(ab.rect.x1, r.x1) && close(ab.rect.y1, r.y1),
|
|
json!({"x": r.x0, "y": r.y0, "width": r.width(), "height": r.height()}),
|
|
)
|
|
}
|
|
|
|
fn board_name(h: &Host, a: &[Data]) -> Answer {
|
|
read_board(h, a, |_, ab, _| Ok(text(ab.name.clone())))
|
|
}
|
|
|
|
/// A new artboard name: at most [`MAX_TEXT_BYTES`] bytes; a number or boolean as `String()` writes
|
|
/// it. Another artboard may have the same name. → the name, trimmed as `artboard.setProps` trims
|
|
/// it; an empty one gives the artboard its default name, `Artboard N` for the Nth artboard.
|
|
fn board_name_arg(d: &Data, index: usize) -> Result<String> {
|
|
let name = name_text(d)?;
|
|
if name.len() > MAX_TEXT_BYTES {
|
|
return Err(HostError::Range("the name is too long".into()));
|
|
}
|
|
let name = name.trim();
|
|
Ok(if name.is_empty() { format!("Artboard {}", index + 1) } else { name.to_string() })
|
|
}
|
|
|
|
fn board_set_name(h: &Host, a: &[Data]) -> Answer {
|
|
let (_, i) = this_board(h, a)?;
|
|
let name = board_name_arg(arg(a, 1), i)?;
|
|
set_board(h, a, |ab| ab.name == name, json!({"name": &name}))
|
|
}
|
|
|
|
fn board_parent(h: &Host, a: &[Data]) -> Answer {
|
|
let (b, _) = this_board(h, a)?;
|
|
Ok(doc_ref(h, b.doc.uid))
|
|
}
|
|
|
|
fn board_typename(h: &Host, a: &[Data]) -> Answer {
|
|
this_board(h, a)?;
|
|
Ok(text("Artboard"))
|
|
}
|
|
|
|
fn board_to_string(h: &Host, a: &[Data]) -> Answer {
|
|
named_text("Artboard", board_name(h, a))
|
|
}
|
|
|
|
/// `rulerOrigin`: where the rulers measure from with this artboard active, from its top-left corner
|
|
/// with Y down.
|
|
fn board_ruler_origin(h: &Host, a: &[Data]) -> Answer {
|
|
read_board(h, a, |st, ab, i| {
|
|
let o = st.doc.ruler_origin_for(i);
|
|
Ok(super::pair([o.x - ab.rect.x0, o.y - ab.rect.y0]))
|
|
})
|
|
}
|
|
|
|
fn board_set_ruler_origin(h: &Host, a: &[Data]) -> Answer {
|
|
let p = super::point(arg(a, 1), "rulerOrigin")?;
|
|
let (b, i) = this_board(h, a)?;
|
|
let (to, now) = with_doc(h, b.doc, |_, st| {
|
|
let ab = &st.doc.artboards[i];
|
|
(vectorcraft_geom::Point::new(ab.rect.x0 + p[0], ab.rect.y0 + p[1]), st.doc.ruler_origin_for(i))
|
|
});
|
|
if to.distance(now) > 1e-9 {
|
|
on_doc(h, b.doc.uid, false, |h| command(h, "document.setRulerOrigin", &json!({"x": to.x, "y": to.y, "artboard": i})))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn board_par(h: &Host, a: &[Data]) -> Answer {
|
|
read_board(h, a, |_, ab, _| Ok(num(ab.video_ruler_pixel_aspect_ratio)))
|
|
}
|
|
|
|
fn board_set_par(h: &Host, a: &[Data]) -> Answer {
|
|
let v = number(arg(a, 1), "rulerPAR")?;
|
|
if !crate::doc::VIDEO_PIXEL_ASPECT_RANGE.contains(&v) {
|
|
return Err(HostError::Range("rulerPAR must be from 0.1 to 10".into()));
|
|
}
|
|
set_board(h, a, |ab| (ab.video_ruler_pixel_aspect_ratio - v).abs() < 1e-12, json!({"videoRulerPixelAspectRatio": v}))
|
|
}
|
|
|
|
fn board_center(h: &Host, a: &[Data]) -> Answer {
|
|
read_board(h, a, |_, ab, _| Ok(Data::Bool(ab.show_center_mark)))
|
|
}
|
|
|
|
fn board_set_center(h: &Host, a: &[Data]) -> Answer {
|
|
let v = boolean(arg(a, 1), "showCenter")?;
|
|
set_board(h, a, |ab| ab.show_center_mark == v, json!({"showCenterMark": v}))
|
|
}
|
|
|
|
fn board_cross_hairs(h: &Host, a: &[Data]) -> Answer {
|
|
read_board(h, a, |_, ab, _| Ok(Data::Bool(ab.show_cross_hairs)))
|
|
}
|
|
|
|
fn board_set_cross_hairs(h: &Host, a: &[Data]) -> Answer {
|
|
let v = boolean(arg(a, 1), "showCrossHairs")?;
|
|
set_board(h, a, |ab| ab.show_cross_hairs == v, json!({"showCrossHairs": v}))
|
|
}
|
|
|
|
fn board_safe_areas(h: &Host, a: &[Data]) -> Answer {
|
|
read_board(h, a, |_, ab, _| Ok(Data::Bool(ab.show_video_safe_areas)))
|
|
}
|
|
|
|
fn board_set_safe_areas(h: &Host, a: &[Data]) -> Answer {
|
|
let v = boolean(arg(a, 1), "showSafeAreas")?;
|
|
set_board(h, a, |ab| ab.show_video_safe_areas == v, json!({"showVideoSafeAreas": v}))
|
|
}
|
|
|
|
fn board_remove(h: &Host, a: &[Data]) -> Answer {
|
|
let (b, i) = this_board(h, a)?;
|
|
on_doc(h, b.doc.uid, true, |h| command(h, "artboard.delete", &json!({"index": i})))?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn boards_doc(h: &Host, a: &[Data]) -> Result<Doc> {
|
|
doc_of(h, arg(a, 0), "Artboards")
|
|
}
|
|
|
|
fn boards_length(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = boards_doc(h, a)?;
|
|
Ok(num(float(with_doc(h, doc, |_, st| st.doc.artboards.len()) as u64)))
|
|
}
|
|
|
|
fn boards_parent(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = boards_doc(h, a)?;
|
|
Ok(doc_ref(h, doc.uid))
|
|
}
|
|
|
|
fn boards_typename(h: &Host, a: &[Data]) -> Answer {
|
|
boards_doc(h, a)?;
|
|
Ok(text("Artboards"))
|
|
}
|
|
|
|
/// `toString()` of a collection: `[Class]`, after the same checks of the collection as `typename`.
|
|
fn layers_to_string(h: &Host, a: &[Data]) -> Answer {
|
|
layers_scope(h, arg(a, 0))?;
|
|
Ok(text("[Layers]"))
|
|
}
|
|
fn groups_to_string(h: &Host, a: &[Data]) -> Answer {
|
|
check_groups(h, arg(a, 0))?;
|
|
Ok(text("[GroupItems]"))
|
|
}
|
|
/// Fails when `d` is not a `GroupItems` collection or the document, layer or group it lists is gone.
|
|
pub(super) fn check_groups(h: &Host, d: &Data) -> Result<()> {
|
|
art_scope(h, d, Kind::Group.class()).map(|_| ())
|
|
}
|
|
fn compounds_to_string(h: &Host, a: &[Data]) -> Answer {
|
|
art_scope(h, arg(a, 0), Kind::Compound.class())?;
|
|
Ok(text("[CompoundPathItems]"))
|
|
}
|
|
fn boards_to_string(h: &Host, a: &[Data]) -> Answer {
|
|
boards_doc(h, a)?;
|
|
Ok(text("[Artboards]"))
|
|
}
|
|
|
|
/// An artboard index argument: a whole number below `len` (`len + 1` with `insert`).
|
|
fn board_index_arg(d: &Data, len: usize, what: &str) -> Result<usize> {
|
|
let n = number(d, what)?;
|
|
whole(n)
|
|
.and_then(|i| usize::try_from(i).ok())
|
|
.filter(|i| *i < len)
|
|
.ok_or_else(|| HostError::Range(format!("{what} must be a whole number from 0 to {}", len.saturating_sub(1))))
|
|
}
|
|
|
|
/// `artboards[i]`, and `artboards["name"]` as `getByName` reads it. A number key that is not an
|
|
/// array index, such as `-1`, is looked up as a name, as `pageItems` looks it up.
|
|
fn boards_at(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = boards_doc(h, a)?;
|
|
if let Data::String(_) = arg(a, 1) {
|
|
return super::by_name_or_probe(a, boards_by_name(h, a));
|
|
}
|
|
if let Some(named) = super::number_named(a) {
|
|
return super::named_or_undefined(boards_by_name(h, &named));
|
|
}
|
|
let Data::Number(n) = arg(a, 1) else { return Ok(Data::Undefined) };
|
|
let Some(i) = super::position(*n) else { return Ok(Data::Undefined) };
|
|
Ok(with_doc(h, doc, |_, st| st.doc.artboards.get(i).map_or(Data::Undefined, |ab| board_ref(h, st.uid, ab.id))))
|
|
}
|
|
|
|
fn boards_by_name(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = boards_doc(h, a)?;
|
|
let Data::String(name) = arg(a, 1) else { return Err(HostError::Type("give a name, as a string".into())) };
|
|
with_doc(h, doc, |_, st| {
|
|
h.charge_call(st.doc.artboards.len() as u64)?;
|
|
let ab = st.doc.artboards.iter().find(|ab| ab.name == *name);
|
|
ab.map(|ab| board_ref(h, st.uid, ab.id)).ok_or_else(|| HostError::Error(NOT_FOUND.into()))
|
|
})
|
|
}
|
|
|
|
fn boards_index(h: &Host, a: &[Data]) -> Answer {
|
|
match arg(a, 1) {
|
|
Data::String(_) => boards_by_name(h, a),
|
|
// A number is a position here, never a name ([`boards_at`]).
|
|
Data::Number(n) if super::position(*n).is_none() => Err(HostError::Error(NOT_FOUND.into())),
|
|
Data::Number(_) => match boards_at(h, a)? {
|
|
Data::Undefined => Err(HostError::Error(NOT_FOUND.into())),
|
|
d => Ok(d),
|
|
},
|
|
_ => Err(HostError::Type("give a name or an index".into())),
|
|
}
|
|
}
|
|
|
|
/// A new artboard at `rect` with `artboard.new`, moved to `at` (`None`: it stays last) with
|
|
/// `artboard.reorder`; when either fails, the document is as it was. → the artboard.
|
|
fn add_board(h: &Host, a: &[Data], at: Option<&Data>) -> Answer {
|
|
let doc = boards_doc(h, a)?;
|
|
let (r, len, name) = with_doc(h, doc, |_, st| {
|
|
let boards = &st.doc.artboards;
|
|
h.charge_call(boards.len() as u64)?;
|
|
// The first "Artboard N" from one past the count that no artboard has.
|
|
let name = (boards.len() + 1..).map(|n| format!("Artboard {n}")).find(|n| !boards.iter().any(|ab| ab.name == *n)).unwrap_or_default();
|
|
Ok::<_, HostError>((rect_arg(h, st, arg(a, 1), "artboardRect")?, boards.len(), name))
|
|
})?;
|
|
let to = at.map(|d| board_index_arg(d, len + 1, "index")).transpose()?;
|
|
let max = whole(super::MAX_ARTBOARDS).and_then(|m| usize::try_from(m).ok()).unwrap_or(0);
|
|
if len >= max {
|
|
return Err(HostError::Range(format!("a document has at most {max} artboards")));
|
|
}
|
|
let id = super::atomically(h, |h| {
|
|
on_doc(h, doc.uid, true, |h| {
|
|
let (_, made) = command_value(h, "artboard.new", &json!({"x": r.x0, "y": r.y0, "width": r.width(), "height": r.height(), "name": name}))?;
|
|
let index = result_id(&made, "index")
|
|
.and_then(|i| usize::try_from(i).ok())
|
|
.ok_or_else(|| HostError::Error("the artboard was not made".into()))?;
|
|
if let Some(to) = to
|
|
&& to != index
|
|
{
|
|
command(h, "artboard.reorder", &json!({"index": index, "to": to}))?;
|
|
}
|
|
let s = h.session.borrow();
|
|
let st = s.documents().iter().find(|d| d.uid == doc.uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?;
|
|
st.doc.artboards.get(to.unwrap_or(index)).map(|ab| ab.id).ok_or_else(|| HostError::Error("the artboard was not made".into()))
|
|
})
|
|
})?;
|
|
Ok(board_ref(h, doc.uid, id))
|
|
}
|
|
|
|
/// `artboards.add(artboardRect)`: a new artboard, last in the list.
|
|
fn boards_add(h: &Host, a: &[Data]) -> Answer {
|
|
add_board(h, a, None)
|
|
}
|
|
|
|
/// `artboards.insert(artboardRect, index)`: a new artboard at `index`. → nothing.
|
|
fn boards_insert(h: &Host, a: &[Data]) -> Answer {
|
|
if matches!(arg(a, 2), Data::Undefined) {
|
|
return Err(HostError::Type("insert needs an index".into()));
|
|
}
|
|
add_board(h, a, Some(arg(a, 2)))?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn boards_remove(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = boards_doc(h, a)?;
|
|
let len = with_doc(h, doc, |_, st| st.doc.artboards.len());
|
|
let i = board_index_arg(arg(a, 1), len, "index")?;
|
|
on_doc(h, doc.uid, true, |h| command(h, "artboard.delete", &json!({"index": i})))?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn boards_active(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = boards_doc(h, a)?;
|
|
Ok(num(float(with_doc(h, doc, |_, st| active_artboard(st)) as u64)))
|
|
}
|
|
|
|
/// `setActiveArtboardIndex(index)`: the artboard becomes the view's active one. Nothing in the
|
|
/// document changes, and no command runs. The artboard is kept by its id even when it is already
|
|
/// active only as the fallback (no artboard chosen yet), so an artboard inserted before it later
|
|
/// in the run moves it to the next index instead of taking its place.
|
|
fn boards_set_active(h: &Host, a: &[Data]) -> Answer {
|
|
let doc = boards_doc(h, a)?;
|
|
let (len, now) = with_doc(h, doc, |_, st| (st.doc.artboards.len(), active_artboard(st)));
|
|
let i = board_index_arg(arg(a, 1), len, "index")?;
|
|
{
|
|
let mut s = h.session.borrow_mut();
|
|
let st = s.document_mut(doc.uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?;
|
|
st.active_artboard = st.doc.artboards.get(i).map(|ab| ab.id);
|
|
}
|
|
{
|
|
let mut chosen = h.dom.chosen_artboards.borrow_mut();
|
|
let reverts = with_doc(h, doc, |_, st| st.reverts);
|
|
if let Some((_, generation)) = chosen.iter_mut().find(|(uid, _)| *uid == doc.uid) {
|
|
*generation = reverts;
|
|
} else {
|
|
chosen.push((doc.uid, reverts));
|
|
}
|
|
}
|
|
// A document made during the run has no index from the start of the run, so the first choice
|
|
// records one; an artboard inserted before it later still puts it in the report.
|
|
if !h.dom.boards_at_start.iter().any(|(uid, _)| *uid == doc.uid) {
|
|
let mut made = h.dom.boards_chosen_in_new.borrow_mut();
|
|
if !made.iter().any(|(uid, _)| *uid == doc.uid) {
|
|
made.push((doc.uid, i));
|
|
}
|
|
}
|
|
if i != now {
|
|
let mut moved = h.dom.active_artboards.borrow_mut();
|
|
if !moved.contains(&doc.uid) {
|
|
moved.push(doc.uid);
|
|
}
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// The documents whose active artboard the run changed, as `script.run`'s report gives them:
|
|
/// `[{document, artboard}]`, each an index, with the active artboard each document has at the end
|
|
/// of the run, or `None` when there are none. They are the documents in `moved`, then those whose
|
|
/// active artboard's index differs from the one in `start` (an artboard added, inserted, removed or
|
|
/// reordered before it).
|
|
pub(crate) fn active_artboards(moved: &[u64], start: &[(u64, usize)], docs: &[DocState]) -> Option<Value> {
|
|
let mut uids = moved.to_vec();
|
|
for (uid, at) in start {
|
|
let shifted = docs.iter().find(|d| d.uid == *uid).is_some_and(|st| active_artboard(st) != *at);
|
|
if shifted && !uids.contains(uid) {
|
|
uids.push(*uid);
|
|
}
|
|
}
|
|
let list: Vec<Value> = uids
|
|
.iter()
|
|
.filter_map(|uid| {
|
|
let document = docs.iter().position(|d| d.uid == *uid)?;
|
|
Some(json!({"document": document, "artboard": active_artboard(&docs[document])}))
|
|
})
|
|
.collect();
|
|
(!list.is_empty()).then_some(Value::Array(list))
|
|
}
|
|
|
|
/// The classes of layers, art collections and artboards.
|
|
pub(super) fn classes(l: &Link) -> Vec<HostClass> {
|
|
let class = super::class;
|
|
let mut layer = named(vec![
|
|
("typename", get(l, layer_typename), typed("string")),
|
|
("name", property(l, layer_name, layer_set_name), typed("string")),
|
|
("visible", property(l, layer_visible, layer_set_visible), typed("boolean")),
|
|
("locked", property(l, layer_locked, layer_set_locked), typed("boolean")),
|
|
("printable", property(l, layer_printable, layer_set_printable), typed("boolean")),
|
|
("preview", property(l, layer_preview, layer_set_preview), typed("boolean")),
|
|
("dimPlacedImages", property(l, layer_dim, layer_set_dim), typed("boolean")),
|
|
("opacity", property(l, layer_opacity, layer_set_opacity), MemberInfo { min: Some(0.0), max: Some(100.0), ..typed("number") }),
|
|
("color", property(l, layer_color, layer_set_color), typed("RGBColor")),
|
|
("parent", get(l, layer_parent), typed("*")),
|
|
("layers", get(l, layers_of), listed("Layers")),
|
|
("pageItems", get(l, held_page_items), listed("PageItems")),
|
|
("groupItems", get(l, held_groups), listed("GroupItems")),
|
|
("compoundPathItems", get(l, held_compounds), listed("CompoundPathItems")),
|
|
("pathItems", get(l, held_paths), listed("PathItems")),
|
|
("textFrames", get(l, held_text), listed("TextFrames")),
|
|
("zOrderPosition", get(l, layer_z_position), typed("number")),
|
|
("hasSelectedArtwork", property(l, layer_has_selected, layer_set_has_selected), typed("boolean")),
|
|
("isIsolated", get(l, layer_is_isolated), typed("boolean")),
|
|
("move", method(l, layer_move), returns("Layer", &[("relativeObject", "*"), ("insertionLocation", "ElementPlacement")])),
|
|
("remove", method(l, layer_remove), returns("undefined", &[])),
|
|
("zOrder", method(l, layer_z_order), returns("undefined", &[("zOrderCmd", "ZOrderMethod")])),
|
|
("toString", method(l, layer_to_string), returns("string", &[])),
|
|
]);
|
|
layer.extend(["symbolItems", "placedItems", "rasterItems", "blendingMode", "artworkKnockout", "sliced"].map(|m| later(l, "Layer", m)));
|
|
vec![
|
|
class("Layer", None, layer, None),
|
|
class(
|
|
"Layers",
|
|
None,
|
|
named(vec![
|
|
("typename", get(l, layers_typename), typed("string")),
|
|
("length", get(l, layers_length), typed("number")),
|
|
("parent", get(l, layers_parent), typed("*")),
|
|
("index", method(l, layers_index), returns("Layer", &[("itemKey", "*")])),
|
|
("getByName", method(l, layers_by_name), returns("Layer", &[("name", "string")])),
|
|
("add", method(l, layers_add), returns("Layer", &[])),
|
|
("removeAll", method(l, layers_remove_all), returns("undefined", &[])),
|
|
("toString", method(l, layers_to_string), returns("string", &[])),
|
|
]),
|
|
Some(super::function(l, layers_at)),
|
|
),
|
|
class(
|
|
"GroupItems",
|
|
None,
|
|
{
|
|
let mut m = named(vec![
|
|
("typename", get(l, groups_typename), typed("string")),
|
|
("length", get(l, groups_length), typed("number")),
|
|
("parent", get(l, groups_parent), typed("*")),
|
|
("index", method(l, groups_index), returns("GroupItem", &[("itemKey", "*")])),
|
|
("getByName", method(l, groups_by_name), returns("GroupItem", &[("name", "string")])),
|
|
("add", method(l, groups_add), returns("GroupItem", &[])),
|
|
("removeAll", method(l, groups_remove_all), returns("undefined", &[])),
|
|
("toString", method(l, groups_to_string), returns("string", &[])),
|
|
]);
|
|
m.push(later(l, "GroupItems", "createFromFile"));
|
|
m
|
|
},
|
|
Some(super::function(l, groups_at)),
|
|
),
|
|
class(
|
|
"CompoundPathItems",
|
|
None,
|
|
named(vec![
|
|
("typename", get(l, compounds_typename), typed("string")),
|
|
("length", get(l, compounds_length), typed("number")),
|
|
("parent", get(l, compounds_parent), typed("*")),
|
|
("index", method(l, compounds_index), returns("CompoundPathItem", &[("itemKey", "*")])),
|
|
("getByName", method(l, compounds_by_name), returns("CompoundPathItem", &[("name", "string")])),
|
|
("add", method(l, compounds_add), returns("CompoundPathItem", &[])),
|
|
("removeAll", method(l, compounds_remove_all), returns("undefined", &[])),
|
|
("toString", method(l, compounds_to_string), returns("string", &[])),
|
|
]),
|
|
Some(super::function(l, compounds_at)),
|
|
),
|
|
class(
|
|
"Artboard",
|
|
None,
|
|
named(vec![
|
|
("typename", get(l, board_typename), typed("string")),
|
|
("artboardRect", property(l, board_rect, board_set_rect), typed("Array")),
|
|
("name", property(l, board_name, board_set_name), typed("string")),
|
|
("parent", get(l, board_parent), typed("Document")),
|
|
("rulerOrigin", property(l, board_ruler_origin, board_set_ruler_origin), typed("Array")),
|
|
("rulerPAR", property(l, board_par, board_set_par), MemberInfo { min: Some(0.1), max: Some(10.0), ..typed("number") }),
|
|
("showCenter", property(l, board_center, board_set_center), typed("boolean")),
|
|
("showCrossHairs", property(l, board_cross_hairs, board_set_cross_hairs), typed("boolean")),
|
|
("showSafeAreas", property(l, board_safe_areas, board_set_safe_areas), typed("boolean")),
|
|
("remove", method(l, board_remove), returns("undefined", &[])),
|
|
("toString", method(l, board_to_string), returns("string", &[])),
|
|
]),
|
|
None,
|
|
),
|
|
class(
|
|
"Artboards",
|
|
None,
|
|
named(vec![
|
|
("typename", get(l, boards_typename), typed("string")),
|
|
("length", get(l, boards_length), typed("number")),
|
|
("parent", get(l, boards_parent), typed("Document")),
|
|
("index", method(l, boards_index), returns("Artboard", &[("itemKey", "*")])),
|
|
("getByName", method(l, boards_by_name), returns("Artboard", &[("name", "string")])),
|
|
("add", method(l, boards_add), returns("Artboard", &[("artboardRect", "Array")])),
|
|
("insert", method(l, boards_insert), returns("undefined", &[("artboardRect", "Array"), ("index", "number")])),
|
|
("remove", method(l, boards_remove), returns("undefined", &[("index", "number")])),
|
|
("getActiveArtboardIndex", method(l, boards_active), returns("number", &[])),
|
|
("setActiveArtboardIndex", method(l, boards_set_active), returns("undefined", &[("index", "number")])),
|
|
("toString", method(l, boards_to_string), returns("string", &[])),
|
|
]),
|
|
Some(super::function(l, boards_at)),
|
|
),
|
|
]
|
|
}
|