mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 18:25:34 +01:00
Follow-up to #9288. - Removing the last anchor of a closed path with its closing segment selected also deselects the first anchor, the segment's other end. - Removing a point puts back new art's paint, so a default stroke width a script set still applies to the next path it draws. - A removal that leaves the direct selection as it was waits with the other point changes, unless every remaining anchor is now selected, the selection refers to points that are gone, or the path is in an Objects on Path group. - Reading the points of a path first writes the waiting point changes of another path in the same Objects on Path group. - Writing a path in an Objects on Path group checks the other tracked paths in the outermost such group, so their PathPoint handles go stale when the group moves them, even after an undo. Docs: docs/mcp.md, docs/target-app-parity.md.
1957 lines
90 KiB
Rust
1957 lines
90 KiB
Rust
//! Paths and their points in the script object model, as the help pages
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/PathItems/>,
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/PathItem/>,
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/PathPoints/> and
|
|
//! <https://ai-scripting.docsforadobe.dev/jsobjref/PathPoint/> describe them.
|
|
//!
|
|
//! A `PathPoints` object holds its path's handle, and a `PathPoint` the path's handle and the
|
|
//! point's index, counted across the path's subpaths in order. Each read looks the path up again.
|
|
//! Every change runs one command inside the run's one undo step, and a change that changes nothing
|
|
//! runs no command. Changes to the points of one path wait in the run's [`super::State`] and are
|
|
//! written together with one `path.setAnchors` ([`flush`]) before any other member, command or
|
|
//! path's points are read or changed, and when the script ends.
|
|
//!
|
|
//! VectorCraft's own rules, which the help pages do not state:
|
|
//! - `Document.pathItems` lists the document's paths in the order of `pageItems`, paths inside
|
|
//! groups and compound paths included.
|
|
//! - A change to the geometry or paint of a path that is hidden or locked, or that is in a hidden
|
|
//! or locked layer or group, throws `Cannot edit` with nothing changed.
|
|
//! - A path in a compound path or compound shape reads that owner's paint, which the paint
|
|
//! commands change in its place, and a gradient that `filled = true` gives it is placed on that
|
|
//! owner's bounds. A paint or stroke change leaves the paint for new art, the active proxy and
|
|
//! the recent colors as they were. `fillOverprint` and `strokeOverprint` read and change the top
|
|
//! fill or stroke only.
|
|
//! - `closed` is true when every subpath of the path is closed, and assigning it opens or closes
|
|
//! them all.
|
|
//! - `pathItems.add()` and the shape creators make their path on top of the active layer, whatever
|
|
//! the drawing mode, painted as new art is; `add()` makes a path with no points. `filled` and `stroked` set to true give a path with no fill or stroke new art's fill or
|
|
//! stroke (solid, gradient or pattern), or black when new art has none. `PathPoints.add()` adds
|
|
//! a corner point with no handles at the last point, or at `[0, 0]` on a path with no points.
|
|
//! Removing the last points leaves the path, with no points.
|
|
//! - A point whose index is past the end of its path throws `Stale path point`, and so does a
|
|
//! point read before the run removed a point of its path or replaced its points
|
|
//! (`PathPoint.remove()`, `PathPoints.removeAll()`, `setEntirePath`), or before anything other
|
|
//! than the object model changed the path's points (a command run with `vc.run`, such as
|
|
//! `path.setAnchors`, `object.move` or `edit.undo`). Points stay valid through the object
|
|
//! model's other changes to the path or to an object that holds it, such as `translate()`.
|
|
//! - Shape creators take sizes and radii that are finite and not negative; `sides` is a whole
|
|
//! number from 3 to 1000 and `points` one from 2 to 1000. `inscribed` true throws an error that
|
|
//! says it is not available yet. `rotateAbout` and `scaleAbout` default to
|
|
//! `Transformation.CENTER`. A transform that leaves the path where it is (`rotate(360)`,
|
|
//! `resize(100, 100)`) checks its arguments and runs no command, on a path with no points too.
|
|
//! - `PathPoint.selected` and `selectedPathPoints` count every point of a path selected as a whole
|
|
//! as a selected anchor point, and not the endpoints of selected segments; a path in a selected
|
|
//! group or compound path is selected as a whole. Direction handles are not selected on their
|
|
//! own, so `LEFTDIRECTION`, `RIGHTDIRECTION` and `LEFTRIGHTPOINT` throw an error that says they
|
|
//! are not available yet, and so does `NOSELECTION` on a point of a path in a selected group or
|
|
//! compound path. Selecting a point of a path with selected segments replaces that path's
|
|
//! segment selection. Selecting a point of a guide while guides are locked throws an error. A
|
|
//! clipping path cannot be made a guide. Assigning `guides` a value the path does not have
|
|
//! throws an error that says it is not available yet on a path inside a compound path, throws
|
|
//! `Cannot edit` on a path that cannot be edited or selected, and throws an error while guides
|
|
//! are locked; assigning the value the path has runs no command.
|
|
//! - The transforms take one setting for fill and stroke patterns, so `changeFillPatterns` and
|
|
//! `changeStrokePattern` given as different values throw an error; gradients move with the art,
|
|
//! so `changeFillGradients` false throws an error that says it is not available yet.
|
|
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
use serde_json::{Value, json};
|
|
use vectorcraft_color::{Color, Paint};
|
|
use vectorcraft_doc::pattern::pattern_paint;
|
|
use vectorcraft_doc::{GraphicStyle, LineCap, LineJoin};
|
|
use vectorcraft_geom::path::{Anchor, AnchorKind, FillRule, PathData, SubPath};
|
|
use vectorcraft_geom::{Point, Rect};
|
|
use vectorcraft_js::{Data, HostClass, HostError, HostItem, HostMember, MemberInfo, Realm, RunError, host_ref};
|
|
use vectorcraft_tools::params::gradient_params;
|
|
|
|
use super::{
|
|
Answer, Coords, Described, Doc, Item, Link, NOT_FOUND, STALE_ITEM, arg, boolean, class, colors, command, command_value, coordinate, doc_of,
|
|
edit_item, editable, find_doc, float, frame, get, given, handle_of, id_of, id_parts, item_of, item_rect, later, method, named, num, number,
|
|
on_doc, pair, point, point_get, point_method, point_property, property, returns, this_doc, typed, whole, with_doc,
|
|
};
|
|
use crate::{DocState, NodeId, NodeKind};
|
|
|
|
/// The error of a point whose index is past the end of its path, or that was read before the run
|
|
/// removed or replaced points of its path.
|
|
pub const STALE_POINT: &str = "Stale path point";
|
|
/// The most points `setEntirePath` takes.
|
|
pub const MAX_POINTS: usize = 100_000;
|
|
/// The most lengths `strokeDashes` takes.
|
|
pub const MAX_DASHES: usize = 64;
|
|
/// The largest stroke weight a script may set, in points.
|
|
pub const MAX_STROKE_WIDTH: f64 = 1000.0;
|
|
|
|
/// The enumerations of paths, as frozen objects whose members are `"Name.MEMBER"` strings.
|
|
pub(super) const ENUMS: &[(&str, &[&str])] = &[
|
|
("PointType", &["CORNER", "SMOOTH"]),
|
|
("PathPointSelection", &["ANCHORPOINT", "LEFTDIRECTION", "LEFTRIGHTPOINT", "NOSELECTION", "RIGHTDIRECTION"]),
|
|
("StrokeCap", &["BUTTENDCAP", "PROJECTINGENDCAP", "ROUNDENDCAP"]),
|
|
("StrokeJoin", &["BEVELENDJOIN", "MITERENDJOIN", "ROUNDENDJOIN"]),
|
|
("PolarityValues", &["NEGATIVE", "POSITIVE"]),
|
|
("Transformation", &["BOTTOM", "BOTTOMLEFT", "BOTTOMRIGHT", "CENTER", "DOCUMENTORIGIN", "LEFT", "RIGHT", "TOP", "TOPLEFT", "TOPRIGHT"]),
|
|
];
|
|
|
|
/// Adds the enumerations of [`ENUMS`] to `realm`.
|
|
pub(super) fn install_enums(realm: &mut Realm) -> std::result::Result<(), RunError> {
|
|
for (name, members) in ENUMS {
|
|
let items = members.iter().map(|m| (m.to_string(), HostItem::Value(Data::String(format!("{name}.{m}"))))).collect();
|
|
realm.define_host_object(name, items)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn cannot_edit(id: NodeId) -> HostError {
|
|
HostError::Error(format!("Cannot edit PathItem {}: it, or a layer or group it is in, is hidden or locked", id.0))
|
|
}
|
|
|
|
/// The path `d` names, which must still be in its document.
|
|
fn path_of(h: &super::Host, d: &Data) -> std::result::Result<Item, HostError> {
|
|
let item = item_of(h, d)?;
|
|
let is_path = with_doc(h, item.doc, |_, st| st.doc.node(item.id).is_some_and(|n| matches!(n.kind, NodeKind::Path { .. })));
|
|
if is_path { Ok(item) } else { Err(HostError::Type("this is not a PathItem".into())) }
|
|
}
|
|
|
|
fn this_path(h: &super::Host, a: &[Data]) -> std::result::Result<Item, HostError> {
|
|
path_of(h, arg(a, 0))
|
|
}
|
|
|
|
/// The path of the run `run`, document `uid` and id parts `high` and `low`, as a handle gives them.
|
|
fn path_at(h: &super::Host, run: f64, uid: f64, high: f64, low: f64) -> std::result::Result<Item, HostError> {
|
|
let doc = find_doc(h, run, uid).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
|
|
let id = id_of(high, low).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
|
|
path_of(h, &path_ref(h, Item { doc, id }))
|
|
}
|
|
|
|
/// Reads the path of `item` with `f`.
|
|
fn read_path<T>(h: &super::Host, item: Item, f: impl FnOnce(&DocState, &crate::doc::Node, &PathData) -> T) -> std::result::Result<T, HostError> {
|
|
with_doc(h, item.doc, |_, st| match st.doc.node(item.id) {
|
|
Some(n) => match n.path_data() {
|
|
Some(pd) => Ok(f(st, n, pd)),
|
|
None => Err(HostError::Type("this is not a PathItem".into())),
|
|
},
|
|
None => Err(HostError::Error(STALE_ITEM.into())),
|
|
})
|
|
}
|
|
|
|
fn path_ref(h: &super::Host, item: Item) -> Data {
|
|
let s = h.session.borrow();
|
|
match s.documents().iter().find(|d| d.uid == item.doc.uid) {
|
|
Some(st) => host_ref("PathItem", &super::node_handle(h, st, item.id)),
|
|
None => Data::Null,
|
|
}
|
|
}
|
|
|
|
/// The index of the first point of each subpath of `subs`, followed by the number of points.
|
|
fn starts(subs: &[SubPath]) -> Vec<usize> {
|
|
let mut out = Vec::with_capacity(subs.len() + 1);
|
|
let mut n = 0;
|
|
for sp in subs {
|
|
out.push(n);
|
|
n += sp.anchors.len();
|
|
}
|
|
out.push(n);
|
|
out
|
|
}
|
|
|
|
/// The subpath and the place in it of point `i`, given the [`starts`] of a path's subpaths.
|
|
fn locate_at(starts: &[usize], i: usize) -> Option<(usize, usize)> {
|
|
if i >= *starts.last()? {
|
|
return None;
|
|
}
|
|
let s = starts.partition_point(|first| *first <= i).checked_sub(1)?;
|
|
Some((s, i - starts.get(s)?))
|
|
}
|
|
|
|
/// What the [`starts`] that [`super::State`] keeps were counted from: a path's node as its document
|
|
/// holds it, by document uid, or the waiting point changes of a path ([`edit_points`]).
|
|
pub(super) enum Counted {
|
|
Node(u64, Arc<crate::doc::Node>),
|
|
Waiting(Item),
|
|
}
|
|
|
|
/// The [`starts`] of the path of `item`, its waiting point changes included. They are kept while
|
|
/// the path's node is the same one, or until the waiting changes add or remove points.
|
|
fn point_starts(h: &super::Host, item: Item) -> std::result::Result<Rc<Vec<usize>>, HostError> {
|
|
let waiting = matches!(&*h.dom.pending.borrow(), Some((w, _)) if same_path(*w, item));
|
|
if waiting {
|
|
if let Some((Counted::Waiting(k), kept)) = &*h.dom.point_starts.borrow()
|
|
&& same_path(*k, item)
|
|
{
|
|
return Ok(kept.clone());
|
|
}
|
|
let counted = Rc::new(read_points(h, item, starts)?);
|
|
*h.dom.point_starts.borrow_mut() = Some((Counted::Waiting(item), counted.clone()));
|
|
return Ok(counted);
|
|
}
|
|
let node = with_doc(h, item.doc, |_, st| node_arc(&st.doc, item.id).cloned()).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
|
|
if let Some((Counted::Node(uid, kept_node), kept)) = &*h.dom.point_starts.borrow()
|
|
&& *uid == item.doc.uid
|
|
&& Arc::ptr_eq(kept_node, &node)
|
|
{
|
|
return Ok(kept.clone());
|
|
}
|
|
let counted = Rc::new(starts(&node.path_data().ok_or_else(|| HostError::Type("this is not a PathItem".into()))?.subpaths));
|
|
*h.dom.point_starts.borrow_mut() = Some((Counted::Node(item.doc.uid, node), counted.clone()));
|
|
Ok(counted)
|
|
}
|
|
|
|
/// How many points the path of `item` has, its waiting point changes included ([`point_starts`]).
|
|
fn points_of(h: &super::Host, item: Item) -> std::result::Result<usize, HostError> {
|
|
Ok(point_starts(h, item)?.last().copied().unwrap_or(0))
|
|
}
|
|
|
|
/// The subpath and the place in it of point `i`, among the subpaths `subs`.
|
|
fn locate(subs: &[SubPath], mut i: usize) -> Option<(usize, usize)> {
|
|
for (s, sp) in subs.iter().enumerate() {
|
|
if i < sp.anchors.len() {
|
|
return Some((s, i));
|
|
}
|
|
i -= sp.anchors.len();
|
|
}
|
|
None
|
|
}
|
|
|
|
fn anchor_json(a: &Anchor) -> Value {
|
|
json!({"x": a.p.x, "y": a.p.y, "in": [a.h_in.x, a.h_in.y], "out": [a.h_out.x, a.h_out.y], "smooth": a.kind == AnchorKind::Smooth})
|
|
}
|
|
|
|
fn same_path(a: Item, b: Item) -> bool {
|
|
a.doc.uid == b.doc.uid && a.id == b.id
|
|
}
|
|
|
|
/// Reads the subpaths of the path of `item` with `f`, its waiting point changes included.
|
|
fn read_points<T>(h: &super::Host, item: Item, f: impl FnOnce(&[SubPath]) -> T) -> std::result::Result<T, HostError> {
|
|
if let Some((waiting, subs)) = &*h.dom.pending.borrow()
|
|
&& same_path(*waiting, item)
|
|
{
|
|
return Ok(f(subs));
|
|
}
|
|
read_path(h, item, |_, _, pd| f(&pd.subpaths))
|
|
}
|
|
|
|
/// Changes the subpaths of the path of `item` with `f`. The change waits in [`super::State`] with
|
|
/// the path's other point changes until [`flush`] writes them. The first change of a path writes
|
|
/// the waiting changes of another path first, and throws `Cannot edit` when the path is hidden or
|
|
/// locked, unless it changes nothing. A path that is aligned to the pixel grid, or is in an object
|
|
/// that is, is written at once, so that its points read as the grid snaps them.
|
|
fn edit_points<T>(h: &super::Host, item: Item, f: impl FnOnce(&mut Vec<SubPath>) -> T) -> std::result::Result<T, HostError> {
|
|
forget_waiting_starts(h);
|
|
if let Some((waiting, subs)) = &mut *h.dom.pending.borrow_mut()
|
|
&& same_path(*waiting, item)
|
|
{
|
|
return Ok(f(subs));
|
|
}
|
|
flush(h)?;
|
|
let mut subs = read_path(h, item, |_, _, pd| pd.subpaths.clone())?;
|
|
let r = f(&mut subs);
|
|
let (same, ok, aligned) = read_path(h, item, |st, _, pd| {
|
|
let d = &st.doc;
|
|
let aligned = d.ancestry(item.id).is_some_and(|a| a.iter().chain([&item.id]).any(|i| d.node(*i).is_some_and(|n| n.extras().pixel_aligned)));
|
|
(pd.subpaths == subs, editable(d, item.id), aligned)
|
|
})?;
|
|
if !same {
|
|
if !ok {
|
|
return Err(cannot_edit(item.id));
|
|
}
|
|
*h.dom.pending.borrow_mut() = Some((item, subs));
|
|
if aligned {
|
|
flush(h)?;
|
|
}
|
|
}
|
|
Ok(r)
|
|
}
|
|
|
|
/// Drops the [`starts`] that [`super::State`] keeps of waiting point changes, which a change can
|
|
/// make wrong.
|
|
fn forget_waiting_starts(h: &super::Host) {
|
|
let mut kept = h.dom.point_starts.borrow_mut();
|
|
if matches!(&*kept, Some((Counted::Waiting(_), _))) {
|
|
*kept = None;
|
|
}
|
|
}
|
|
|
|
/// Changes the anchor at index `i` of the path of `item` with `f` ([`edit_points`]). The change
|
|
/// adds and removes no points, so the [`starts`] of the path stay kept.
|
|
fn edit_anchor(h: &super::Host, item: Item, i: usize, f: impl FnOnce(&mut Anchor)) -> std::result::Result<(), HostError> {
|
|
let counted = point_starts(h, item)?;
|
|
let at = locate_at(&counted, i);
|
|
edit_points(h, item, |subs| {
|
|
if let Some((s, j)) = at
|
|
&& let Some(an) = subs.get_mut(s).and_then(|sp| sp.anchors.get_mut(j))
|
|
{
|
|
f(an);
|
|
}
|
|
})?;
|
|
if matches!(&*h.dom.pending.borrow(), Some((w, _)) if same_path(*w, item)) {
|
|
*h.dom.point_starts.borrow_mut() = Some((Counted::Waiting(item), counted));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Gives the path of `item` the subpaths `subs` ([`edit_points`]).
|
|
fn write_path(h: &super::Host, item: Item, subs: Vec<SubPath>) -> std::result::Result<(), HostError> {
|
|
edit_points(h, item, |s| *s = subs)
|
|
}
|
|
|
|
/// Writes the waiting point changes of a path ([`edit_points`]) with one `path.setAnchors`, unless
|
|
/// the path has those points already.
|
|
pub(super) fn flush(h: &super::Host) -> std::result::Result<(), HostError> {
|
|
let Some((item, subs)) = h.dom.pending.borrow_mut().take() else { return Ok(()) };
|
|
forget_waiting_starts(h);
|
|
let same = {
|
|
let s = h.session.borrow();
|
|
let node = s.documents().iter().find(|d| d.uid == item.doc.uid).and_then(|st| st.doc.node(item.id));
|
|
node.and_then(|n| n.path_data()).map(|pd| pd.subpaths == subs)
|
|
};
|
|
match same {
|
|
Some(true) => return Ok(()),
|
|
Some(false) => {}
|
|
None => return Err(HostError::Error(STALE_ITEM.into())),
|
|
}
|
|
let subpaths: Vec<Value> =
|
|
subs.iter().map(|sp| json!({"anchors": sp.anchors.iter().map(anchor_json).collect::<Vec<_>>(), "closed": sp.closed})).collect();
|
|
// Changes made before this one are looked at in full first.
|
|
see_all_points(h);
|
|
let kept = h.dom.one_path.replace(Some(item));
|
|
let r = edit_item(h, item, "path.setAnchors", &json!({"id": item.id.0, "subpaths": subpaths}));
|
|
h.dom.one_path.set(kept);
|
|
r?;
|
|
Ok(())
|
|
}
|
|
|
|
/// The object whose paint the path `n` with id `id` shows: its compound path or compound shape
|
|
/// when it is a member of one, since the paint commands paint that owner in its place.
|
|
fn paint_owner<'a>(d: &'a crate::Document, id: NodeId, n: &'a crate::doc::Node) -> &'a crate::doc::Node {
|
|
d.parent_of(id).and_then(|p| d.node(p)).filter(|p| matches!(p.kind, NodeKind::Compound { .. }) || p.is_compound_shape()).unwrap_or(n)
|
|
}
|
|
|
|
/// Runs `f` with the drawing mode set to normal, so that the paths it makes go on top of the active
|
|
/// layer, then puts the drawing mode back.
|
|
fn drawing_normally<T>(h: &super::Host, f: impl FnOnce() -> T) -> T {
|
|
let kept = std::mem::replace(&mut h.session.borrow_mut().draw_mode, crate::DrawMode::Normal);
|
|
let r = f();
|
|
h.session.borrow_mut().draw_mode = kept;
|
|
r
|
|
}
|
|
|
|
/// Runs `id` with `params` and `ids: [item]` (unless `params` names its `ids`) on the item, which
|
|
/// must be one the user could edit.
|
|
/// The paint for new art, the active Fill or Stroke proxy, the last color and gradient and the
|
|
/// recent colors, which `paint.setFill`, `paint.setStroke` and `stroke.set` also set, are put
|
|
/// back afterwards, so the edit changes only the path.
|
|
fn edit_path(h: &super::Host, item: Item, id: &str, mut params: Value) -> std::result::Result<Data, HostError> {
|
|
let ok = with_doc(h, item.doc, |_, st| editable(&st.doc, item.id));
|
|
if !ok {
|
|
return Err(cannot_edit(item.id));
|
|
}
|
|
if let Some(o) = params.as_object_mut() {
|
|
o.entry("ids").or_insert_with(|| json!([item.id.0]));
|
|
}
|
|
keeping_paint(h, || edit_item(h, item, id, ¶ms))
|
|
}
|
|
|
|
/// Runs `f`, then puts back the paint for new art, the active Fill or Stroke proxy, the last color
|
|
/// and gradient and the recent colors as they were before it.
|
|
fn keeping_paint<T>(h: &super::Host, f: impl FnOnce() -> T) -> T {
|
|
let kept = {
|
|
let s = h.session.borrow();
|
|
(s.paint.clone(), s.fill_active, s.last_solid, s.last_gradient.clone(), s.recent_colors.clone())
|
|
};
|
|
let r = f();
|
|
let mut s = h.session.borrow_mut();
|
|
(s.paint, s.fill_active, s.last_solid, s.last_gradient, s.recent_colors) = kept;
|
|
r
|
|
}
|
|
|
|
// ---- Document.pathItems ----
|
|
|
|
/// The ids of a document's paths, in the order of its page items.
|
|
fn path_items(h: &super::Host, st: &DocState) -> Rc<Vec<NodeId>> {
|
|
if let Some(ids) = super::kept_list(&h.dom.paths, st) {
|
|
return ids;
|
|
}
|
|
let all = super::page_items(h, st);
|
|
let ids: Rc<Vec<NodeId>> =
|
|
Rc::new(all.iter().copied().filter(|i| st.doc.node(*i).is_some_and(|n| matches!(n.kind, NodeKind::Path { .. }))).collect());
|
|
super::keep_list(&h.dom.paths, st, &ids);
|
|
ids
|
|
}
|
|
|
|
pub(super) fn doc_path_items(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = this_doc(h, a)?;
|
|
Ok(host_ref("PathItems", &[float(h.run), float(doc.uid)]))
|
|
}
|
|
|
|
fn items_doc(h: &super::Host, a: &[Data]) -> std::result::Result<Doc, HostError> {
|
|
doc_of(h, arg(a, 0), "PathItems")
|
|
}
|
|
|
|
/// `typename` of the paths of a document that is still open.
|
|
fn items_typename(h: &super::Host, a: &[Data]) -> Answer {
|
|
items_doc(h, a)?;
|
|
Ok(Data::String("PathItems".into()))
|
|
}
|
|
|
|
fn items_length(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
Ok(num(float(with_doc(h, doc, |_, st| path_items(h, st).len()) as u64)))
|
|
}
|
|
|
|
fn items_parent(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
Ok(host_ref("Document", &[float(h.run), float(doc.uid)]))
|
|
}
|
|
|
|
fn items_at(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
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) };
|
|
Ok(with_doc(h, doc, |_, st| path_items(h, st).get(i).map_or(Data::Undefined, |id| super::item_ref(h, st, *id))))
|
|
}
|
|
|
|
fn items_by_name(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_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| {
|
|
let ids = path_items(h, st);
|
|
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| super::item_ref(h, st, *id)).ok_or_else(|| HostError::Error(NOT_FOUND.into()))
|
|
})
|
|
}
|
|
|
|
fn items_index(h: &super::Host, a: &[Data]) -> Answer {
|
|
match arg(a, 1) {
|
|
Data::String(_) => items_by_name(h, a),
|
|
Data::Number(_) => match items_at(h, a)? {
|
|
Data::Undefined => Err(HostError::Error(NOT_FOUND.into())),
|
|
d => Ok(d),
|
|
},
|
|
_ => Err(HostError::Type("give a name or an index".into())),
|
|
}
|
|
}
|
|
|
|
/// `pathItems.removeAll()`: deletes every path of the document in one command. A hidden or locked
|
|
/// path, or one in a hidden or locked layer or group, stops it before anything is deleted.
|
|
fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
let (ids, refused) = with_doc(h, doc, |_, st| {
|
|
let ids = path_items(h, st);
|
|
let refused: Vec<u64> = ids.iter().filter(|i| !editable(&st.doc, **i)).map(|i| i.0).take(20).collect();
|
|
(ids, refused)
|
|
});
|
|
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 paths: {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)
|
|
}
|
|
|
|
/// The id in the result of a command that made an object, read from the command's own result so
|
|
/// that an id above 2^53 stays exact.
|
|
fn made_id(r: &Value) -> std::result::Result<NodeId, HostError> {
|
|
r["id"].as_u64().map(NodeId).ok_or_else(|| HostError::Error("the path was not made".into()))
|
|
}
|
|
|
|
/// Runs the command `id` that makes a path in `doc` on top of the active layer, the selection kept,
|
|
/// and reverses the new path's direction when `reversed`. → the new path. Selecting the new path
|
|
/// and putting the selection back load the selected objects' paint and stroke width into new art,
|
|
/// so new art's paint from before, the defaults a script set included, is put back.
|
|
fn make(h: &super::Host, doc: Doc, id: &str, params: &Value, reversed: bool) -> Answer {
|
|
let made = keeping_paint(h, || {
|
|
on_doc(h, doc.uid, true, |h| {
|
|
let id = made_id(&drawing_normally(h, || command_value(h, id, params))?.1)?;
|
|
if reversed {
|
|
command(h, "path.reverse", &json!({"ids": [id.0]}))?;
|
|
}
|
|
Ok(id)
|
|
})
|
|
})?;
|
|
let s = h.session.borrow();
|
|
let st = s.documents().iter().find(|d| d.uid == doc.uid).ok_or_else(|| HostError::Error(super::STALE_DOCUMENT.into()))?;
|
|
Ok(super::item_ref(h, st, made))
|
|
}
|
|
|
|
/// Argument `i`, or `default` when it is left out.
|
|
fn number_or(a: &[Data], i: usize, default: f64, what: &str) -> std::result::Result<f64, HostError> {
|
|
given(arg(a, i)).map_or(Ok(default), |d| number(d, what))
|
|
}
|
|
|
|
/// A size or radius: finite and not negative.
|
|
fn size(a: &[Data], i: usize, default: Option<f64>, what: &str) -> std::result::Result<f64, HostError> {
|
|
let v = match (given(arg(a, i)), default) {
|
|
(Some(d), _) => number(d, what)?,
|
|
(None, Some(v)) => v,
|
|
(None, None) => return Err(HostError::Type(format!("{what} must be a finite number"))),
|
|
};
|
|
if !(0.0..=super::MAX_SIZE).contains(&v) {
|
|
return Err(HostError::Range(format!("{what} must be from 0 to 3999856 points")));
|
|
}
|
|
Ok(v)
|
|
}
|
|
|
|
/// A whole number argument from `min` to `max`.
|
|
fn count(a: &[Data], i: usize, default: f64, min: f64, max: f64, what: &str) -> std::result::Result<f64, HostError> {
|
|
let v = number_or(a, i, default, what)?;
|
|
if v.fract() != 0.0 || !(min..=max).contains(&v) {
|
|
return Err(HostError::Range(format!("{what} must be a whole number from {min} to {max}")));
|
|
}
|
|
Ok(v)
|
|
}
|
|
|
|
fn flag(a: &[Data], i: usize, what: &str) -> std::result::Result<bool, HostError> {
|
|
given(arg(a, i)).map_or(Ok(false), |d| boolean(d, what))
|
|
}
|
|
|
|
/// The top-left corner, in document coordinates, of a box whose top-left corner the script gives.
|
|
fn corner(h: &super::Host, doc: Doc, left: f64, top: f64) -> std::result::Result<Point, HostError> {
|
|
with_doc(h, doc, |_, st| Ok(frame(st)?.to_document(h.dom.coords.get(), [left, top])))
|
|
}
|
|
|
|
/// `rectangle(top, left, width, height, reversed?)`.
|
|
fn items_rectangle(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
let top = coordinate(arg(a, 1), "top")?;
|
|
let left = coordinate(arg(a, 2), "left")?;
|
|
let (w, ht) = (size(a, 3, None, "width")?, size(a, 4, None, "height")?);
|
|
let reversed = flag(a, 5, "reversed")?;
|
|
let p = corner(h, doc, left, top)?;
|
|
make(h, doc, "shape.rectangle", &json!({"x": p.x, "y": p.y, "width": w, "height": ht}), reversed)
|
|
}
|
|
|
|
/// The anchors of a rectangle `r` with elliptical corners of radii `rx` and `ry`, clockwise on the
|
|
/// page from the top of the left side.
|
|
fn rounded(r: Rect, rx: f64, ry: f64) -> Vec<Anchor> {
|
|
const K: f64 = 0.552_284_749_830_793_4;
|
|
let (rx, ry) = (rx.min(r.width() / 2.0), ry.min(r.height() / 2.0));
|
|
let (kx, ky) = (rx * K, ry * K);
|
|
let a = |p: Point, h_in: Point, h_out: Point| Anchor { p, h_in, h_out, kind: AnchorKind::Corner };
|
|
let (x0, y0, x1, y1) = (r.x0, r.y0, r.x1, r.y1);
|
|
let mut out = vec![
|
|
a(Point::new(x0, y0 + ry), Point::new(x0, y0 + ry), Point::new(x0, y0 + ry - ky)),
|
|
a(Point::new(x0 + rx, y0), Point::new(x0 + rx - kx, y0), Point::new(x0 + rx, y0)),
|
|
a(Point::new(x1 - rx, y0), Point::new(x1 - rx, y0), Point::new(x1 - rx + kx, y0)),
|
|
a(Point::new(x1, y0 + ry), Point::new(x1, y0 + ry - ky), Point::new(x1, y0 + ry)),
|
|
a(Point::new(x1, y1 - ry), Point::new(x1, y1 - ry), Point::new(x1, y1 - ry + ky)),
|
|
a(Point::new(x1 - rx, y1), Point::new(x1 - rx + kx, y1), Point::new(x1 - rx, y1)),
|
|
a(Point::new(x0 + rx, y1), Point::new(x0 + rx, y1), Point::new(x0 + rx - kx, y1)),
|
|
a(Point::new(x0, y1 - ry), Point::new(x0, y1 - ry + ky), Point::new(x0, y1 - ry)),
|
|
];
|
|
out.dedup_by(|b, a| {
|
|
b.p.distance(a.p) < 1e-9 && {
|
|
a.h_out = b.h_out;
|
|
true
|
|
}
|
|
});
|
|
if out.len() > 1
|
|
&& out.first().zip(out.last()).is_some_and(|(f, l)| f.p.distance(l.p) < 1e-9)
|
|
&& let Some(l) = out.pop()
|
|
&& let Some(f) = out.first_mut()
|
|
{
|
|
f.h_in = l.h_in;
|
|
}
|
|
out
|
|
}
|
|
|
|
/// `roundedRectangle(top, left, width, height, horizontalRadius = 15, verticalRadius = 20,
|
|
/// reversed = false)`: a closed path with `path.create`.
|
|
fn items_rounded_rectangle(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
let top = coordinate(arg(a, 1), "top")?;
|
|
let left = coordinate(arg(a, 2), "left")?;
|
|
let (w, ht) = (size(a, 3, None, "width")?, size(a, 4, None, "height")?);
|
|
let (rx, ry) = (size(a, 5, Some(15.0), "horizontalRadius")?, size(a, 6, Some(20.0), "verticalRadius")?);
|
|
let reversed = flag(a, 7, "reversed")?;
|
|
let p = corner(h, doc, left, top)?;
|
|
let anchors: Vec<Value> = rounded(Rect::new(p.x, p.y, p.x + w, p.y + ht), rx, ry).iter().map(anchor_json).collect();
|
|
make(h, doc, "path.create", &json!({"anchors": anchors, "closed": true}), reversed)
|
|
}
|
|
|
|
/// `ellipse(top = 100, left = 100, width = 50, height = 100, reversed = false, inscribed)`.
|
|
fn items_ellipse(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
let top = given(arg(a, 1)).map_or(Ok(100.0), |d| coordinate(d, "top"))?;
|
|
let left = given(arg(a, 2)).map_or(Ok(100.0), |d| coordinate(d, "left"))?;
|
|
let (w, ht) = (size(a, 3, Some(50.0), "width")?, size(a, 4, Some(100.0), "height")?);
|
|
let reversed = flag(a, 5, "reversed")?;
|
|
if flag(a, 6, "inscribed")? {
|
|
return Err(HostError::Error("ellipse with inscribed true is not available yet".into()));
|
|
}
|
|
let p = corner(h, doc, left, top)?;
|
|
make(h, doc, "shape.ellipse", &json!({"x": p.x, "y": p.y, "width": w, "height": ht}), reversed)
|
|
}
|
|
|
|
/// The center of a polygon or star, in document coordinates.
|
|
fn center(h: &super::Host, doc: Doc, a: &[Data]) -> std::result::Result<Point, HostError> {
|
|
let x = given(arg(a, 1)).map_or(Ok(200.0), |d| coordinate(d, "centerX"))?;
|
|
let y = given(arg(a, 2)).map_or(Ok(300.0), |d| coordinate(d, "centerY"))?;
|
|
corner(h, doc, x, y)
|
|
}
|
|
|
|
/// `polygon(centerX = 200, centerY = 300, radius = 50, sides = 8, reversed = false)`.
|
|
fn items_polygon(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
let c = center(h, doc, a)?;
|
|
let r = size(a, 3, Some(50.0), "radius")?;
|
|
let sides = count(a, 4, 8.0, 3.0, 1000.0, "sides")?;
|
|
let reversed = flag(a, 5, "reversed")?;
|
|
make(h, doc, "shape.polygon", &json!({"cx": c.x, "cy": c.y, "radius": r, "sides": sides}), reversed)
|
|
}
|
|
|
|
/// `star(centerX = 200, centerY = 300, radius = 50, innerRadius = 20, points = 5, reversed =
|
|
/// false)`.
|
|
fn items_star(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
let c = center(h, doc, a)?;
|
|
let r1 = size(a, 3, Some(50.0), "radius")?;
|
|
let r2 = size(a, 4, Some(20.0), "innerRadius")?;
|
|
let points = count(a, 5, 5.0, 2.0, 1000.0, "points")?;
|
|
let reversed = flag(a, 6, "reversed")?;
|
|
make(h, doc, "shape.star", &json!({"cx": c.x, "cy": c.y, "radius1": r1, "radius2": r2, "points": points}), reversed)
|
|
}
|
|
|
|
/// `pathItems.add()`: a path with no points on top of the active layer, from `path.create` with
|
|
/// empty path data, painted as new art. When new art has no fill and no stroke, the black stroke `path.create` gives an open
|
|
/// path is set back to none in the same step, and new art keeps the paint it had before.
|
|
fn items_add(h: &super::Host, a: &[Data]) -> Answer {
|
|
let doc = items_doc(h, a)?;
|
|
// New art's paint is read with the document active, since activating a document can load its
|
|
// selection's appearance into new art. Selecting the new path loads its temporary stroke into
|
|
// new art, and putting the selection back does not undo that, so new art's paint from before
|
|
// the path is put back.
|
|
let made = keeping_paint(h, || {
|
|
on_doc(h, doc.uid, true, |h| {
|
|
let unpainted = {
|
|
let ap = h.session.borrow().new_art();
|
|
ap.fill_paint().is_none() && ap.stroke_paint().is_none()
|
|
};
|
|
let id = made_id(&drawing_normally(h, || command_value(h, "path.create", &json!({"d": ""})))?.1)?;
|
|
if unpainted {
|
|
command(h, "paint.setStroke", &json!({"none": true, "ids": [id.0]}))?;
|
|
}
|
|
Ok(id)
|
|
})
|
|
})?;
|
|
let s = h.session.borrow();
|
|
let st = s.documents().iter().find(|d| d.uid == doc.uid).ok_or_else(|| HostError::Error(super::STALE_DOCUMENT.into()))?;
|
|
Ok(super::item_ref(h, st, made))
|
|
}
|
|
|
|
// ---- PathItem geometry ----
|
|
|
|
fn path_points(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = this_path(h, a)?;
|
|
let [high, low] = id_parts(item.id);
|
|
Ok(host_ref("PathPoints", &[float(h.run), float(item.doc.uid), high, low]))
|
|
}
|
|
|
|
fn path_closed(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = this_path(h, a)?;
|
|
read_points(h, item, |subs| Data::Bool(!subs.is_empty() && subs.iter().all(|sp| sp.closed)))
|
|
}
|
|
|
|
fn path_set_closed(h: &super::Host, a: &[Data]) -> Answer {
|
|
let closed = boolean(arg(a, 1), "closed")?;
|
|
let item = this_path(h, a)?;
|
|
edit_points(h, item, |subs| subs.iter_mut().for_each(|sp| sp.closed = closed))?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// Whether the path fills by the even-odd rule: its compound path's rule when it is a member of one,
|
|
/// as `path.setFillRule` sets it there.
|
|
fn path_evenodd(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = this_path(h, a)?;
|
|
read_path(h, item, |st, n, _| {
|
|
let owner = st.doc.parent_of(item.id).and_then(|p| st.doc.node(p)).filter(|p| matches!(p.kind, NodeKind::Compound { .. })).unwrap_or(n);
|
|
Data::Bool(matches!(owner.kind, NodeKind::Path { rule: FillRule::EvenOdd, .. } | NodeKind::Compound { rule: FillRule::EvenOdd, .. }))
|
|
})
|
|
}
|
|
|
|
fn path_set_evenodd(h: &super::Host, a: &[Data]) -> Answer {
|
|
let on = boolean(arg(a, 1), "evenodd")?;
|
|
let item = this_path(h, a)?;
|
|
if path_evenodd(h, a)? != Data::Bool(on) {
|
|
edit_path(h, item, "path.setFillRule", json!({"rule": if on { "evenOdd" } else { "nonZero" }}))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// The area in square points: negative when the path runs counterclockwise on the page.
|
|
fn path_area(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = this_path(h, a)?;
|
|
read_path(h, item, |_, _, pd| num(pd.subpaths.iter().map(SubPath::signed_area).sum()))
|
|
}
|
|
|
|
fn path_length(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = this_path(h, a)?;
|
|
read_path(h, item, |_, _, pd| num(pd.length()))
|
|
}
|
|
|
|
fn path_guides(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = this_path(h, a)?;
|
|
read_path(h, item, |_, n, _| Data::Bool(matches!(n.kind, NodeKind::Path { guide: true, .. })))
|
|
}
|
|
|
|
/// The error of assigning `guides` on a path inside a compound path.
|
|
pub const COMPOUND_MEMBER_GUIDES: &str = "Changing guides on a path inside a compound path is not available yet";
|
|
|
|
/// `guides = true` makes the path a guide with `view.guides.make`, and `false` makes it a path again
|
|
/// with `view.guides.release`; the selection stays as it was. A value the path has already runs no
|
|
/// command and throws no error. Otherwise a clipping path cannot be a guide, a path inside a
|
|
/// compound path throws [`COMPOUND_MEMBER_GUIDES`], and while guides are locked both values throw
|
|
/// an error.
|
|
fn path_set_guides(h: &super::Host, a: &[Data]) -> Answer {
|
|
let on = boolean(arg(a, 1), "guides")?;
|
|
let item = this_path(h, a)?;
|
|
let (now, clipping) = read_path(h, item, |_, n, _| match &n.kind {
|
|
NodeKind::Path { guide, clipping, .. } => (*guide, *clipping),
|
|
_ => (false, false),
|
|
})?;
|
|
if now == on {
|
|
return Ok(Data::Undefined);
|
|
}
|
|
let (member, editable, selectable) = with_doc(h, item.doc, |_, st| {
|
|
let member = st.doc.parent_of(item.id).and_then(|p| st.doc.node(p)).is_some_and(|p| matches!(p.kind, NodeKind::Compound { .. }));
|
|
let selectable = crate::cmd::select::is_selectable(&st.doc, st.isolation, &st.doc.locked_artboards(), item.id);
|
|
(member, editable(&st.doc, item.id), selectable)
|
|
});
|
|
if member {
|
|
return Err(HostError::Error(COMPOUND_MEMBER_GUIDES.into()));
|
|
}
|
|
if !editable || !selectable {
|
|
return Err(cannot_edit(item.id));
|
|
}
|
|
if h.session.borrow().menu.guides_locked {
|
|
return Err(HostError::Error(format!("Cannot change guides on PathItem {}: guides are locked", item.id.0)));
|
|
}
|
|
if !on {
|
|
edit_path(h, item, "view.guides.release", json!({}))?;
|
|
return Ok(Data::Undefined);
|
|
}
|
|
if clipping {
|
|
return Err(HostError::Error(format!("Cannot make PathItem {} a guide: it is a clipping path", item.id.0)));
|
|
}
|
|
see_all_points(h);
|
|
let kept = h.dom.editing.replace(Some(item));
|
|
let r = on_doc(h, item.doc.uid, true, |h| {
|
|
command(h, "select.set", &json!({"ids": [item.id.0]}))?;
|
|
command(h, "view.guides.make", &json!({}))
|
|
});
|
|
h.dom.editing.set(kept);
|
|
r?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn path_clipping(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = this_path(h, a)?;
|
|
read_path(h, item, |_, n, _| Data::Bool(matches!(n.kind, NodeKind::Path { clipping: true, .. })))
|
|
}
|
|
|
|
/// `setEntirePath([[x, y], ...])`: corner points with no handles in place of every point, in one
|
|
/// subpath that stays closed when the path was.
|
|
fn path_set_entire(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = this_path(h, a)?;
|
|
let Data::Array(list) = arg(a, 1) else { return Err(HostError::Type("setEntirePath needs an array of [x, y] points".into())) };
|
|
if list.len() > MAX_POINTS {
|
|
return Err(HostError::Range(format!("setEntirePath takes at most {MAX_POINTS} points")));
|
|
}
|
|
let script: Vec<[f64; 2]> = list.iter().map(|d| point(d, "each point")).collect::<std::result::Result<_, _>>()?;
|
|
let f = with_doc(h, item.doc, |_, st| frame(st))?;
|
|
let closed = read_points(h, item, |subs| !subs.is_empty() && subs.iter().all(|sp| sp.closed))?;
|
|
let c = h.dom.coords.get();
|
|
let anchors: Vec<Anchor> =
|
|
script.iter().map(|p| on_canvas(f.to_document(c, *p), "each point").map(Anchor::corner)).collect::<std::result::Result<_, _>>()?;
|
|
let subs = if anchors.is_empty() { Vec::new() } else { vec![SubPath::new(anchors, closed)] };
|
|
write_path(h, item, subs)?;
|
|
cut_points(h, item);
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
// ---- PathItem paint ----
|
|
|
|
/// The top fill's paint and the top stroke's paint, width, cap, join, miter limit, dashes and
|
|
/// offset, and the overprint flags of both.
|
|
struct Look {
|
|
fill: Paint,
|
|
fill_overprint: bool,
|
|
stroke: Paint,
|
|
width: f64,
|
|
cap: LineCap,
|
|
join: LineJoin,
|
|
miter: f64,
|
|
dashes: Vec<f64>,
|
|
offset: f64,
|
|
stroke_overprint: bool,
|
|
}
|
|
|
|
/// The look of `item`, read from its compound path or compound shape when it is a member of one,
|
|
/// since the paint commands paint that owner in its place.
|
|
fn look(h: &super::Host, item: Item) -> std::result::Result<Look, HostError> {
|
|
read_path(h, item, |st, n, _| {
|
|
let ap = &paint_owner(&st.doc, item.id, n).appearance;
|
|
let fill = ap.fill();
|
|
let stroke = ap.stroke();
|
|
Look {
|
|
fill: fill.map_or(Paint::None, |f| f.paint.clone()),
|
|
fill_overprint: fill.is_some_and(|f| f.overprint),
|
|
stroke: stroke.map_or(Paint::None, |s| s.paint.clone()),
|
|
width: stroke.map_or(0.0, |s| s.width),
|
|
cap: stroke.map_or(LineCap::default(), |s| s.cap),
|
|
join: stroke.map_or(LineJoin::default(), |s| s.join),
|
|
miter: stroke.map_or(10.0, |s| s.miter_limit),
|
|
dashes: stroke.and_then(|s| s.dash.as_ref()).map_or_else(Vec::new, |d| d.pattern.clone()),
|
|
offset: stroke.and_then(|s| s.dash.as_ref()).map_or(0.0, |d| d.offset),
|
|
stroke_overprint: stroke.is_some_and(|s| s.overprint),
|
|
}
|
|
})
|
|
}
|
|
|
|
fn this_look(h: &super::Host, a: &[Data]) -> std::result::Result<(Item, Look), HostError> {
|
|
let item = this_path(h, a)?;
|
|
Ok((item, look(h, item)?))
|
|
}
|
|
|
|
fn path_filled(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(Data::Bool(!this_look(h, a)?.1.fill.is_none()))
|
|
}
|
|
|
|
fn path_stroked(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(Data::Bool(!this_look(h, a)?.1.stroke.is_none()))
|
|
}
|
|
|
|
/// Sets the fill (`fill`) or the stroke to `color`, or to none, with `paint.setFill` or
|
|
/// `paint.setStroke`.
|
|
fn set_paint(h: &super::Host, item: Item, fill: bool, color: Option<Color>) -> std::result::Result<(), HostError> {
|
|
let id = if fill { "paint.setFill" } else { "paint.setStroke" };
|
|
let params = match color {
|
|
Some(c) => json!({"color": colors::param(&c)}),
|
|
None => json!({"none": true}),
|
|
};
|
|
edit_path(h, item, id, params)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// The paint new art gets for its fill or stroke, as `paint.setFill` or `paint.setStroke` params for
|
|
/// the path `item`: a solid color, linked to its global or spot swatch while the document has that
|
|
/// swatch; a gradient, a placed one moved onto the bounds of the path or of the compound path or
|
|
/// compound shape whose paint it shows, as new art takes it; or a pattern.
|
|
/// A fill or stroke of none gives black. A pattern that its swatch does not give as new art has it
|
|
/// (its own placement, or a pattern the document does not have) throws an error.
|
|
fn new_art_params(h: &super::Host, item: Item, fill: bool) -> std::result::Result<Value, HostError> {
|
|
let mut ap = h.session.borrow().new_art();
|
|
let top = |ap: &vectorcraft_doc::Appearance| if fill { ap.fill_paint() } else { ap.stroke_paint() };
|
|
let what = if fill { "filled" } else { "stroked" };
|
|
let paint = top(&ap);
|
|
match &paint {
|
|
Paint::None => Ok(json!({"color": colors::param(&Color::BLACK)})),
|
|
Paint::Solid { color, swatch: Some(name), tint } => {
|
|
let linked = read_path(h, item, |st, _, _| st.doc.swatch(name).is_some())?;
|
|
Ok(match linked {
|
|
true if *tint < 1.0 => json!({"swatch": name, "tint": f64::from(*tint) * 100.0}),
|
|
true => json!({"swatch": name}),
|
|
false => json!({"color": colors::param(color)}),
|
|
})
|
|
}
|
|
Paint::Solid { color, .. } => Ok(json!({"color": colors::param(color)})),
|
|
Paint::Gradient(g) => {
|
|
let mut g = g.clone();
|
|
if g.geom.is_some() || g.freeform.is_some() {
|
|
match read_path(h, item, |st, n, _| paint_owner(&st.doc, item.id, n).geometric_bounds())? {
|
|
Some(b) => {
|
|
ap.rebase_gradients(GraphicStyle::UNIT_BOX, b);
|
|
if let Paint::Gradient(placed) = top(&ap) {
|
|
g = placed;
|
|
}
|
|
}
|
|
None => (g.geom, g.freeform) = (None, None),
|
|
}
|
|
}
|
|
Ok(json!({"gradient": gradient_params(&g), "keepModel": true}))
|
|
}
|
|
Paint::Pattern { pattern, .. } => {
|
|
let given = read_path(h, item, |st, _, _| {
|
|
let d = &st.doc;
|
|
d.swatch(pattern).map(|sw| sw.paint.clone()).or_else(|| d.pattern(pattern).map(|_| pattern_paint(pattern)))
|
|
})?;
|
|
match given {
|
|
Some(p) if p == paint => Ok(json!({"swatch": pattern})),
|
|
Some(_) => Err(HostError::Error(format!(
|
|
"Cannot set {what}: new art's pattern `{pattern}` has a placement of its own, which is not available yet"
|
|
))),
|
|
None => Err(HostError::Error(format!("Cannot set {what}: new art's pattern `{pattern}` is not in this document"))),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn set_painted(h: &super::Host, a: &[Data], fill: bool) -> Answer {
|
|
let on = boolean(arg(a, 1), if fill { "filled" } else { "stroked" })?;
|
|
let (item, look) = this_look(h, a)?;
|
|
let now = if fill { !look.fill.is_none() } else { !look.stroke.is_none() };
|
|
if now != on {
|
|
let params = if on { new_art_params(h, item, fill)? } else { json!({"none": true}) };
|
|
edit_path(h, item, if fill { "paint.setFill" } else { "paint.setStroke" }, params)?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn path_set_filled(h: &super::Host, a: &[Data]) -> Answer {
|
|
set_painted(h, a, true)
|
|
}
|
|
|
|
fn path_set_stroked(h: &super::Host, a: &[Data]) -> Answer {
|
|
set_painted(h, a, false)
|
|
}
|
|
|
|
fn path_fill_color(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(colors::of_paint(&this_look(h, a)?.1.fill))
|
|
}
|
|
|
|
fn path_stroke_color(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(colors::of_paint(&this_look(h, a)?.1.stroke))
|
|
}
|
|
|
|
fn set_color(h: &super::Host, a: &[Data], fill: bool) -> Answer {
|
|
let color = colors::assigned(arg(a, 1), if fill { "fillColor" } else { "strokeColor" })?;
|
|
let (item, look) = this_look(h, a)?;
|
|
let now = if fill { &look.fill } else { &look.stroke };
|
|
if !colors::unchanged(arg(a, 1), color, now, |c| c) {
|
|
set_paint(h, item, fill, color)?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn path_set_fill_color(h: &super::Host, a: &[Data]) -> Answer {
|
|
set_color(h, a, true)
|
|
}
|
|
|
|
fn path_set_stroke_color(h: &super::Host, a: &[Data]) -> Answer {
|
|
set_color(h, a, false)
|
|
}
|
|
|
|
/// Runs `stroke.set` with `params` on the path, unless `same`.
|
|
fn set_stroke_option(h: &super::Host, item: Item, same: bool, params: Value) -> Answer {
|
|
if !same {
|
|
edit_path(h, item, "stroke.set", params)?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn path_stroke_width(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(num(this_look(h, a)?.1.width))
|
|
}
|
|
|
|
fn path_set_stroke_width(h: &super::Host, a: &[Data]) -> Answer {
|
|
let w = number(arg(a, 1), "strokeWidth")?;
|
|
if !(0.0..=MAX_STROKE_WIDTH).contains(&w) {
|
|
return Err(HostError::Range("strokeWidth must be from 0 to 1000 points".into()));
|
|
}
|
|
let (item, look) = this_look(h, a)?;
|
|
set_stroke_option(h, item, (look.width - w).abs() < 1e-9, json!({"weight": w}))
|
|
}
|
|
|
|
/// Each stroke cap, its `StrokeCap` value and its name in `stroke.set`.
|
|
pub(super) const CAPS: &[(LineCap, &str, &str)] = &[
|
|
(LineCap::Butt, "StrokeCap.BUTTENDCAP", "butt"),
|
|
(LineCap::Round, "StrokeCap.ROUNDENDCAP", "round"),
|
|
(LineCap::Square, "StrokeCap.PROJECTINGENDCAP", "square"),
|
|
];
|
|
|
|
/// Each stroke join, its `StrokeJoin` value and its name in `stroke.set`.
|
|
pub(super) const JOINS: &[(LineJoin, &str, &str)] = &[
|
|
(LineJoin::Bevel, "StrokeJoin.BEVELENDJOIN", "bevel"),
|
|
(LineJoin::Round, "StrokeJoin.ROUNDENDJOIN", "round"),
|
|
(LineJoin::Miter, "StrokeJoin.MITERENDJOIN", "miter"),
|
|
];
|
|
|
|
fn path_stroke_cap(h: &super::Host, a: &[Data]) -> Answer {
|
|
let cap = this_look(h, a)?.1.cap;
|
|
Ok(Data::String(CAPS.iter().find(|c| c.0 == cap).map_or("StrokeCap.BUTTENDCAP", |c| c.1).into()))
|
|
}
|
|
|
|
fn path_set_stroke_cap(h: &super::Host, a: &[Data]) -> Answer {
|
|
let found = match arg(a, 1) {
|
|
Data::String(s) => CAPS.iter().find(|c| c.1 == s),
|
|
_ => None,
|
|
};
|
|
let Some(&(cap, _, name)) = found else { return Err(HostError::Type("strokeCap must be a StrokeCap value".into())) };
|
|
let (item, look) = this_look(h, a)?;
|
|
set_stroke_option(h, item, look.cap == cap, json!({"cap": name}))
|
|
}
|
|
|
|
fn path_stroke_join(h: &super::Host, a: &[Data]) -> Answer {
|
|
let join = this_look(h, a)?.1.join;
|
|
Ok(Data::String(JOINS.iter().find(|j| j.0 == join).map_or("StrokeJoin.MITERENDJOIN", |j| j.1).into()))
|
|
}
|
|
|
|
fn path_set_stroke_join(h: &super::Host, a: &[Data]) -> Answer {
|
|
let found = match arg(a, 1) {
|
|
Data::String(s) => JOINS.iter().find(|j| j.1 == s),
|
|
_ => None,
|
|
};
|
|
let Some(&(join, _, name)) = found else { return Err(HostError::Type("strokeJoin must be a StrokeJoin value".into())) };
|
|
let (item, look) = this_look(h, a)?;
|
|
set_stroke_option(h, item, look.join == join, json!({"join": name}))
|
|
}
|
|
|
|
fn path_miter(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(num(this_look(h, a)?.1.miter))
|
|
}
|
|
|
|
fn path_set_miter(h: &super::Host, a: &[Data]) -> Answer {
|
|
let v = number(arg(a, 1), "strokeMiterLimit")?;
|
|
if !(1.0..=500.0).contains(&v) {
|
|
return Err(HostError::Range("strokeMiterLimit must be from 1 to 500".into()));
|
|
}
|
|
let (item, look) = this_look(h, a)?;
|
|
set_stroke_option(h, item, (look.miter - v).abs() < 1e-9, json!({"miterLimit": v}))
|
|
}
|
|
|
|
fn path_dashes(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(Data::Array(this_look(h, a)?.1.dashes.into_iter().map(num).collect()))
|
|
}
|
|
|
|
/// The dash and gap lengths a script assigned to `what`: at most [`MAX_DASHES`] finite lengths
|
|
/// that are not negative and not all 0; `[]` or `{}` gives a solid line.
|
|
pub(super) fn dash_lengths(d: &Data, what: &str) -> std::result::Result<Vec<f64>, HostError> {
|
|
let lengths: Vec<f64> = match d {
|
|
Data::Object(o) if o.is_empty() => Vec::new(),
|
|
Data::Array(list) if list.len() > MAX_DASHES => return Err(HostError::Range(format!("{what} takes at most {MAX_DASHES} lengths"))),
|
|
Data::Array(list) => list.iter().map(|d| number(d, "each dash length")).collect::<std::result::Result<_, _>>()?,
|
|
_ => return Err(HostError::Type(format!("{what} must be an array of lengths"))),
|
|
};
|
|
use crate::cmd::newart::DashFault;
|
|
match crate::cmd::newart::dash_fault(&lengths) {
|
|
None => Ok(lengths),
|
|
Some(DashFault::TooMany) => Err(HostError::Range(format!("{what} takes at most {MAX_DASHES} lengths"))),
|
|
Some(DashFault::OutOfRange) => Err(HostError::Range("each dash length must be from 0 to 1000000 points".into())),
|
|
Some(DashFault::AllZero) => Err(HostError::Range(format!("{what} needs a length that is not 0"))),
|
|
}
|
|
}
|
|
|
|
/// `strokeDashes = [lengths]`, as [`dash_lengths`] takes them.
|
|
fn path_set_dashes(h: &super::Host, a: &[Data]) -> Answer {
|
|
let lengths = dash_lengths(arg(a, 1), "strokeDashes")?;
|
|
let (item, look) = this_look(h, a)?;
|
|
let dash = if lengths.is_empty() { Value::Null } else { json!(lengths) };
|
|
set_stroke_option(h, item, look.dashes == lengths, json!({"dash": dash}))
|
|
}
|
|
|
|
fn path_dash_offset(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(num(this_look(h, a)?.1.offset))
|
|
}
|
|
|
|
fn path_set_dash_offset(h: &super::Host, a: &[Data]) -> Answer {
|
|
let v = number(arg(a, 1), "strokeDashOffset")?;
|
|
if v.abs() > MAX_STROKE_WIDTH * 1000.0 {
|
|
return Err(HostError::Range("strokeDashOffset must be from -1000000 to 1000000 points".into()));
|
|
}
|
|
let (item, look) = this_look(h, a)?;
|
|
if look.dashes.is_empty() && v != 0.0 {
|
|
return Err(HostError::Error("strokeDashOffset needs strokeDashes first".into()));
|
|
}
|
|
set_stroke_option(h, item, (look.offset - v).abs() < 1e-9, json!({"dashOffset": v}))
|
|
}
|
|
|
|
fn path_fill_overprint(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(Data::Bool(this_look(h, a)?.1.fill_overprint))
|
|
}
|
|
|
|
fn path_stroke_overprint(h: &super::Host, a: &[Data]) -> Answer {
|
|
Ok(Data::Bool(this_look(h, a)?.1.stroke_overprint))
|
|
}
|
|
|
|
fn set_overprint(h: &super::Host, a: &[Data], fill: bool) -> Answer {
|
|
let key = if fill { "fill" } else { "stroke" };
|
|
let on = boolean(arg(a, 1), if fill { "fillOverprint" } else { "strokeOverprint" })?;
|
|
let (item, look) = this_look(h, a)?;
|
|
let (now, paint) = if fill { (look.fill_overprint, &look.fill) } else { (look.stroke_overprint, &look.stroke) };
|
|
if now != on {
|
|
// The top fill or stroke of the object `look` reads, as `item` names it to the command.
|
|
let top = read_path(h, item, |st, n, _| {
|
|
let owner = paint_owner(&st.doc, item.id, n);
|
|
owner.appearance.items.iter().rposition(|i| i.is_fill() == fill).map(|index| (owner.id, index))
|
|
})?;
|
|
let (Some((owner, index)), false) = (top, paint.is_none()) else {
|
|
return Err(HostError::Error(format!("the path has no {key} to overprint")));
|
|
};
|
|
edit_path(h, item, "object.setOverprint", json!({key: on, "item": index, "ids": [owner.0]}))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
fn path_set_fill_overprint(h: &super::Host, a: &[Data]) -> Answer {
|
|
set_overprint(h, a, true)
|
|
}
|
|
|
|
fn path_set_stroke_overprint(h: &super::Host, a: &[Data]) -> Answer {
|
|
set_overprint(h, a, false)
|
|
}
|
|
|
|
// ---- PathItem transforms ----
|
|
|
|
/// The member of `Transformation` that `d` names (left out: `CENTER`).
|
|
fn about_name(d: &Data) -> std::result::Result<&'static str, HostError> {
|
|
let name = match d {
|
|
Data::Undefined => "Transformation.CENTER",
|
|
Data::String(s) => s.as_str(),
|
|
_ => return Err(HostError::Type("give a Transformation value".into())),
|
|
};
|
|
let members = ENUMS.iter().find(|(e, _)| *e == "Transformation").map_or(&[][..], |(_, m)| *m);
|
|
let member = name.strip_prefix("Transformation.").and_then(|m| members.iter().find(|x| **x == m));
|
|
member.copied().ok_or_else(|| HostError::Type("give a Transformation value".into()))
|
|
}
|
|
|
|
/// The point of the path's geometric bounds `r`, in document coordinates, that the `Transformation`
|
|
/// member `name` ([`about_name`]) names.
|
|
fn about(h: &super::Host, item: Item, name: &str, r: Rect) -> std::result::Result<Point, HostError> {
|
|
let (cx, cy) = ((r.x0 + r.x1) / 2.0, (r.y0 + r.y1) / 2.0);
|
|
Ok(match name {
|
|
"CENTER" => Point::new(cx, cy),
|
|
"TOP" => Point::new(cx, r.y0),
|
|
"BOTTOM" => Point::new(cx, r.y1),
|
|
"LEFT" => Point::new(r.x0, cy),
|
|
"RIGHT" => Point::new(r.x1, cy),
|
|
"TOPLEFT" => Point::new(r.x0, r.y0),
|
|
"TOPRIGHT" => Point::new(r.x1, r.y0),
|
|
"BOTTOMLEFT" => Point::new(r.x0, r.y1),
|
|
"BOTTOMRIGHT" => Point::new(r.x1, r.y1),
|
|
"DOCUMENTORIGIN" => with_doc(h, item.doc, |_, st| Ok::<_, HostError>(frame(st)?.to_document(Coords::Document, [0.0, 0.0])))?,
|
|
_ => return Err(HostError::Type("give a Transformation value".into())),
|
|
})
|
|
}
|
|
|
|
/// The `objects` and `patterns` options of a transform command from the four flags from `i`:
|
|
/// objects, fill patterns, fill gradients and stroke patterns, each left out or true or false.
|
|
fn transform_options(a: &[Data], i: usize, params: &mut Value) -> std::result::Result<(), HostError> {
|
|
let names = ["changePositions", "changeFillPatterns", "changeFillGradients", "changeStrokePattern"];
|
|
let mut flags = [None; 4];
|
|
for (k, name) in names.iter().enumerate() {
|
|
flags[k] = given(arg(a, i + k)).map(|d| boolean(d, name)).transpose()?;
|
|
}
|
|
if flags[2] == Some(false) {
|
|
return Err(HostError::Error(format!("{} false is not available yet: gradients move with the art", names[2])));
|
|
}
|
|
if let (Some(f), Some(s)) = (flags[1], flags[3])
|
|
&& f != s
|
|
{
|
|
return Err(HostError::Error(format!("{} and {} must be the same here", names[1], names[3])));
|
|
}
|
|
if let Some(o) = params.as_object_mut() {
|
|
if let Some(b) = flags[0] {
|
|
o.insert("objects".into(), json!(b));
|
|
}
|
|
if let Some(b) = flags[1].or(flags[3]) {
|
|
o.insert("patterns".into(), json!(b));
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// `translate(deltaX = 0, deltaY = 0, transformObjects?, transformFillPatterns?, ...)`.
|
|
fn path_translate(h: &super::Host, a: &[Data]) -> Answer {
|
|
let dx = given(arg(a, 1)).map_or(Ok(0.0), |d| coordinate(d, "deltaX"))?;
|
|
let dy = given(arg(a, 2)).map_or(Ok(0.0), |d| coordinate(d, "deltaY"))?;
|
|
let item = this_path(h, a)?;
|
|
let mut params = json!({"dx": dx, "dy": if h.dom.coords.get() == Coords::Document { -dy } else { dy }});
|
|
transform_options(a, 3, &mut params)?;
|
|
if dx != 0.0 || dy != 0.0 {
|
|
edit_path(h, item, "object.move", params)?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// `rotate(angle, changePositions?, ..., rotateAbout?)`: counterclockwise on the page for a
|
|
/// positive angle.
|
|
fn path_rotate(h: &super::Host, a: &[Data]) -> Answer {
|
|
let angle = number(arg(a, 1), "angle")?;
|
|
let item = this_path(h, a)?;
|
|
let place = about_name(arg(a, 6))?;
|
|
let mut params = json!({"angle": angle});
|
|
transform_options(a, 2, &mut params)?;
|
|
if angle.rem_euclid(360.0) != 0.0
|
|
&& let (Some(r), _) = item_rect(h, item, false)?
|
|
{
|
|
let origin = about(h, item, place, r)?;
|
|
params["origin"] = json!([origin.x, origin.y]);
|
|
edit_path(h, item, "object.rotate", params)?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// `resize(scaleX, scaleY, changePositions?, ..., changeLineWidths?, scaleAbout?)`: 100 is 100%;
|
|
/// `changeLineWidths` scales the stroke weight by that percent.
|
|
fn path_resize(h: &super::Host, a: &[Data]) -> Answer {
|
|
let sx = number(arg(a, 1), "scaleX")?;
|
|
let sy = number(arg(a, 2), "scaleY")?;
|
|
if sx == 0.0 || sy == 0.0 || sx.abs() > 100_000.0 || sy.abs() > 100_000.0 {
|
|
return Err(HostError::Range("scaleX and scaleY must not be 0 and at most 100000 percent".into()));
|
|
}
|
|
let widths = given(arg(a, 7)).map(|d| number(d, "changeLineWidths")).transpose()?;
|
|
if widths.is_some_and(|w| !(0.0..=100_000.0).contains(&w)) {
|
|
return Err(HostError::Range("changeLineWidths must be from 0 to 100000 percent".into()));
|
|
}
|
|
let item = this_path(h, a)?;
|
|
let place = about_name(arg(a, 8))?;
|
|
let mut params = json!({"sx": sx, "sy": sy, "basis": "page", "strokes": false});
|
|
transform_options(a, 3, &mut params)?;
|
|
let width = look(h, item)?.width;
|
|
let new_width = widths.map(|w| width * w / 100.0);
|
|
if new_width.is_some_and(|w| w > MAX_STROKE_WIDTH) {
|
|
return Err(HostError::Range("changeLineWidths would make the stroke weight more than 1000 points".into()));
|
|
}
|
|
if (sx != 100.0 || sy != 100.0)
|
|
&& let (Some(r), _) = item_rect(h, item, false)?
|
|
{
|
|
let origin = about(h, item, place, r)?;
|
|
params["origin"] = json!([origin.x, origin.y]);
|
|
edit_path(h, item, "object.scale", params)?;
|
|
}
|
|
if let Some(w) = new_width
|
|
&& (w - width).abs() > 1e-9
|
|
{
|
|
edit_path(h, item, "stroke.set", json!({"weight": w}))?;
|
|
}
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
// ---- PathPoints ----
|
|
|
|
fn points_path(h: &super::Host, a: &[Data]) -> std::result::Result<Item, HostError> {
|
|
let Some(handle) = handle_of(arg(a, 0), "PathPoints") else { return Err(HostError::Type("this is not a PathPoints".into())) };
|
|
let [run, uid, high, low] = handle.as_slice() else { return Err(HostError::Type("this is not a PathPoints".into())) };
|
|
let item = path_at(h, *run, *uid, *high, *low)?;
|
|
flush_other(h, item)?;
|
|
Ok(item)
|
|
}
|
|
|
|
/// Writes the waiting point changes of another path in the same Objects on Path group as `item`.
|
|
fn flush_other(h: &super::Host, item: Item) -> std::result::Result<(), HostError> {
|
|
let waiting = h.dom.pending.borrow().as_ref().map(|(w, _)| *w);
|
|
let other = waiting.is_some_and(|w| {
|
|
w.doc.uid == item.doc.uid
|
|
&& w.id != item.id
|
|
&& with_doc(h, item.doc, |_, st| {
|
|
let d = &st.doc;
|
|
let read = d.ancestry(item.id).unwrap_or_default();
|
|
d.ancestry(w.id).is_some_and(|a| {
|
|
a.iter().any(|g| read.contains(g) && d.node(*g).is_some_and(vectorcraft_doc::objects_on_path::is_objects_on_path))
|
|
})
|
|
})
|
|
});
|
|
if other { flush(h) } else { Ok(()) }
|
|
}
|
|
|
|
/// `typename` of the points of a path that is still in its document.
|
|
fn points_typename(h: &super::Host, a: &[Data]) -> Answer {
|
|
points_path(h, a)?;
|
|
Ok(Data::String("PathPoints".into()))
|
|
}
|
|
|
|
fn points_length(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = points_path(h, a)?;
|
|
Ok(num(float(points_of(h, item)? as u64)))
|
|
}
|
|
|
|
fn points_parent(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = points_path(h, a)?;
|
|
Ok(path_ref(h, item))
|
|
}
|
|
|
|
fn point_ref(h: &super::Host, item: Item, i: usize) -> Data {
|
|
let [high, low] = id_parts(item.id);
|
|
host_ref("PathPoint", &[float(h.run), float(item.doc.uid), high, low, float(i as u64), float(point_cuts(h, item))])
|
|
}
|
|
|
|
/// How many times the points read of `item` have gone stale, after [`see_points`] notes a change
|
|
/// made to them since the run last saw them.
|
|
fn point_cuts(h: &super::Host, item: Item) -> u64 {
|
|
let key = (item.doc.uid, item.id.0);
|
|
see_points(h, key, || false);
|
|
h.dom.point_cuts.borrow().get(&key).copied().unwrap_or(0)
|
|
}
|
|
|
|
/// Notes that the run removed or replaced points of `item`, which makes the points read before
|
|
/// stale.
|
|
fn cut_points(h: &super::Host, item: Item) {
|
|
cut_key(h, (item.doc.uid, item.id.0));
|
|
}
|
|
|
|
/// [`cut_points`] for the path of document uid and path id `key`.
|
|
fn cut_key(h: &super::Host, key: (u64, u64)) {
|
|
let mut cuts = h.dom.point_cuts.borrow_mut();
|
|
let n = cuts.entry(key).or_insert(0);
|
|
*n = n.saturating_add(1);
|
|
}
|
|
|
|
#[cfg(test)]
|
|
thread_local! {
|
|
/// How many times this thread looked at a path's points ([`see_points`]), for the tests.
|
|
pub(crate) static POINT_LOOKS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
|
|
}
|
|
|
|
/// The node of `id` in `d`, as the document holds it.
|
|
fn node_arc(d: &crate::Document, id: NodeId) -> Option<&Arc<crate::doc::Node>> {
|
|
let at = d.index_path(id)?;
|
|
let (first, rest) = at.split_first()?;
|
|
let mut cur = d.layers.get(*first)?;
|
|
for &i in rest {
|
|
cur = cur.children()?.get(i)?;
|
|
}
|
|
Some(cur)
|
|
}
|
|
|
|
/// Keeps the path of document uid and path id `key` as it is now, the first time the run reads its
|
|
/// points and after each later look. When its points differ from the ones the run last saw, the
|
|
/// points read before go stale, unless `own`: the object model made the change
|
|
/// ([`see_all_points`]), which `own` is asked only after a change. The kept node is shared with
|
|
/// the document, so a node whose pointer is unchanged has unchanged points, and any change to it
|
|
/// makes a new node.
|
|
fn see_points(h: &super::Host, key: (u64, u64), own: impl FnOnce() -> bool) {
|
|
#[cfg(test)]
|
|
POINT_LOOKS.with(|n| n.set(n.get() + 1));
|
|
let node = {
|
|
let s = h.session.borrow();
|
|
s.documents().iter().find(|d| d.uid == key.0).and_then(|st| node_arc(&st.doc, NodeId(key.1)).cloned())
|
|
};
|
|
// A path that is gone makes its points stale, so that an undo that brings it back leaves them
|
|
// stale.
|
|
let Some(node) = node else {
|
|
if h.dom.point_nodes.borrow_mut().get_mut(&key.0).and_then(|paths| paths.remove(&key.1)).is_some() {
|
|
cut_key(h, key);
|
|
}
|
|
return;
|
|
};
|
|
let changed = match h.dom.point_nodes.borrow_mut().entry(key.0).or_default().insert(key.1, node.clone()) {
|
|
Some(old) => !Arc::ptr_eq(&old, &node) && old.path_data().map(|pd| &pd.subpaths) != node.path_data().map(|pd| &pd.subpaths),
|
|
None => false,
|
|
};
|
|
if changed && !own() {
|
|
cut_key(h, key);
|
|
}
|
|
}
|
|
|
|
/// [`see_points`] for each path whose points the run has read, after each command, so that a
|
|
/// change that a later command reverses still makes the points stale. Paths under the object that
|
|
/// the object model is changing ([`super::State`]), or that object itself, take the change as the
|
|
/// object model's own, and their points stay valid. A document that is the one the run last looked
|
|
/// at is skipped, as are its paths. While the object model writes one path's points
|
|
/// ([`super::State`]'s `one_path`), only that path and tracked paths in its outermost enclosing
|
|
/// Objects on Path group are looked at in its document.
|
|
pub(super) fn see_all_points(h: &super::Host) {
|
|
let keys: Vec<(u64, u64)> = {
|
|
let seen = h.dom.point_nodes.borrow();
|
|
if seen.values().all(|paths| paths.is_empty()) {
|
|
return;
|
|
}
|
|
let s = h.session.borrow();
|
|
let mut docs = h.dom.point_docs.borrow_mut();
|
|
let one = h.dom.one_path.get();
|
|
let mut keys = Vec::new();
|
|
// The documents that changed since the last look, kept as they are now.
|
|
for (uid, paths) in seen.iter().filter(|(_, paths)| !paths.is_empty()) {
|
|
// Editing a path in an Objects on Path group can move other paths in the group.
|
|
let mut moved = Vec::new();
|
|
match s.documents().iter().find(|d| d.uid == *uid) {
|
|
Some(st) if docs.get(uid).is_some_and(|d| Arc::ptr_eq(d, &st.doc)) => continue,
|
|
Some(st) => {
|
|
if let Some(item) = one.filter(|i| i.doc.uid == *uid) {
|
|
let d = &st.doc;
|
|
let group = d.ancestry(item.id).and_then(|a| {
|
|
a.into_iter().filter_map(|id| d.node(id)).find(|g| vectorcraft_doc::objects_on_path::is_objects_on_path(g))
|
|
});
|
|
if let Some(g) = group {
|
|
g.walk(&mut |n: &crate::doc::Node| {
|
|
if n.id != item.id && paths.contains_key(&n.id.0) {
|
|
moved.push((*uid, n.id.0));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
docs.insert(*uid, st.doc.clone());
|
|
}
|
|
None => {
|
|
docs.remove(uid);
|
|
}
|
|
}
|
|
match one {
|
|
Some(item) if item.doc.uid == *uid => {
|
|
keys.extend(paths.contains_key(&item.id.0).then_some((*uid, item.id.0)));
|
|
keys.append(&mut moved);
|
|
}
|
|
_ => keys.extend(paths.keys().map(|id| (*uid, *id))),
|
|
}
|
|
}
|
|
keys
|
|
};
|
|
let editing = h.dom.editing.get();
|
|
for (uid, id) in keys {
|
|
let own = || match editing {
|
|
Some(e) if e.doc.uid == uid => {
|
|
let s = h.session.borrow();
|
|
let id = NodeId(id);
|
|
id == e.id || s.documents().iter().find(|d| d.uid == uid).and_then(|st| st.doc.ancestry(id)).is_some_and(|a| a.contains(&e.id))
|
|
}
|
|
_ => false,
|
|
};
|
|
see_points(h, (uid, id), own);
|
|
}
|
|
}
|
|
|
|
fn points_at(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = points_path(h, a)?;
|
|
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) };
|
|
let n = points_of(h, item)?;
|
|
Ok(if i < n { point_ref(h, item, i) } else { Data::Undefined })
|
|
}
|
|
|
|
/// `index(key)`: the points are not named, so a key that is not a number throws `Not found`.
|
|
fn points_index(h: &super::Host, a: &[Data]) -> Answer {
|
|
match arg(a, 1) {
|
|
Data::Number(_) => match points_at(h, a)? {
|
|
Data::Undefined => Err(HostError::Error(NOT_FOUND.into())),
|
|
d => Ok(d),
|
|
},
|
|
_ => Err(HostError::Error(NOT_FOUND.into())),
|
|
}
|
|
}
|
|
|
|
fn points_add(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = points_path(h, a)?;
|
|
let origin = with_doc(h, item.doc, |_, st| frame(st).map(|f| f.to_document(h.dom.coords.get(), [0.0, 0.0])));
|
|
let mut counted = point_starts(h, item)?;
|
|
let n = counted.last().copied().unwrap_or(0);
|
|
if n >= MAX_POINTS {
|
|
return Err(HostError::Range(format!("PathPoints.add takes a path to at most {MAX_POINTS} points")));
|
|
}
|
|
let last = read_points(h, item, |subs| subs.iter().rev().find_map(|sp| sp.anchors.last()).map(|a| a.p))?;
|
|
let p = match last {
|
|
Some(p) => p,
|
|
None => origin?,
|
|
};
|
|
// The kept starts are dropped, so that `counted` is their only copy and grows in place.
|
|
*h.dom.point_starts.borrow_mut() = None;
|
|
edit_points(h, item, |subs| match subs.last_mut() {
|
|
Some(sp) => sp.anchors.push(Anchor::corner(p)),
|
|
None => subs.push(SubPath::new(vec![Anchor::corner(p)], false)),
|
|
})?;
|
|
if matches!(&*h.dom.pending.borrow(), Some((w, _)) if same_path(*w, item)) {
|
|
let grown = Rc::make_mut(&mut counted);
|
|
if grown.len() > 1 {
|
|
if let Some(total) = grown.last_mut() {
|
|
*total += 1;
|
|
}
|
|
} else {
|
|
*grown = vec![0, 1];
|
|
}
|
|
*h.dom.point_starts.borrow_mut() = Some((Counted::Waiting(item), counted));
|
|
}
|
|
Ok(point_ref(h, item, n))
|
|
}
|
|
|
|
fn points_remove_all(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = points_path(h, a)?;
|
|
write_path(h, item, Vec::new())?;
|
|
cut_points(h, item);
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
// ---- PathPoint ----
|
|
|
|
/// The path and the index of the point `this` names, which must still be in the path, with no
|
|
/// point of the path removed or replaced since `this` was read.
|
|
pub(super) fn this_point(h: &super::Host, a: &[Data]) -> std::result::Result<(Item, usize), HostError> {
|
|
let Some(handle) = handle_of(arg(a, 0), "PathPoint") else { return Err(HostError::Type("this is not a PathPoint".into())) };
|
|
let [run, uid, high, low, i, cuts] = handle.as_slice() else { return Err(HostError::Type("this is not a PathPoint".into())) };
|
|
let item = path_at(h, *run, *uid, *high, *low)?;
|
|
flush_other(h, item)?;
|
|
if whole(*cuts) != Some(point_cuts(h, item)) {
|
|
return Err(HostError::Error(STALE_POINT.into()));
|
|
}
|
|
let i = whole(*i).and_then(|i| usize::try_from(i).ok()).ok_or_else(|| HostError::Error(STALE_POINT.into()))?;
|
|
if i >= points_of(h, item)? {
|
|
return Err(HostError::Error(STALE_POINT.into()));
|
|
}
|
|
Ok((item, i))
|
|
}
|
|
|
|
/// Reads the anchor `this` names with `f`, given the frame and the coordinate system.
|
|
fn read_point(h: &super::Host, a: &[Data], f: impl FnOnce(&super::Frame, Coords, &Anchor) -> Data) -> Answer {
|
|
let (item, i) = this_point(h, a)?;
|
|
let c = h.dom.coords.get();
|
|
let fr = with_doc(h, item.doc, |_, st| frame(st))?;
|
|
let at = locate_at(&point_starts(h, item)?, i);
|
|
read_points(h, item, |subs| {
|
|
let anchor = at.and_then(|(s, j)| subs.get(s).and_then(|sp| sp.anchors.get(j)));
|
|
anchor.map(|an| f(&fr, c, an)).ok_or_else(|| HostError::Error(STALE_POINT.into()))
|
|
})?
|
|
}
|
|
|
|
/// Changes the anchor `this` names with `f`, given the point the script assigned in document
|
|
/// coordinates (for a point property) or the value itself.
|
|
fn edit_point(h: &super::Host, a: &[Data], f: impl FnOnce(&mut Anchor)) -> Answer {
|
|
let (item, i) = this_point(h, a)?;
|
|
edit_anchor(h, item, i, f)?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// The point the script assigned to a point property, in document coordinates.
|
|
fn assigned_point(h: &super::Host, a: &[Data], what: &str) -> std::result::Result<Point, HostError> {
|
|
let p = point(arg(a, 1), what)?;
|
|
let (item, _) = this_point(h, a)?;
|
|
let fr = with_doc(h, item.doc, |_, st| frame(st))?;
|
|
on_canvas(fr.to_document(h.dom.coords.get(), p), what)
|
|
}
|
|
|
|
/// `p`, a point a script gave in document coordinates, when it is on the canvas (within
|
|
/// [`crate::MAX_COORD`] of the origin); a `RangeError` otherwise.
|
|
fn on_canvas(p: Point, what: &str) -> std::result::Result<Point, HostError> {
|
|
if p.x.is_finite() && p.y.is_finite() && p.x.abs() <= crate::MAX_COORD && p.y.abs() <= crate::MAX_COORD {
|
|
Ok(p)
|
|
} else {
|
|
Err(HostError::Range(format!("{what} is outside the canvas")))
|
|
}
|
|
}
|
|
|
|
fn pt_anchor(h: &super::Host, a: &[Data]) -> Answer {
|
|
read_point(h, a, |f, c, an| pair(f.to_script(c, an.p)))
|
|
}
|
|
|
|
fn pt_set_anchor(h: &super::Host, a: &[Data]) -> Answer {
|
|
let p = assigned_point(h, a, "anchor")?;
|
|
edit_point(h, a, |an| an.p = p)
|
|
}
|
|
|
|
fn pt_left(h: &super::Host, a: &[Data]) -> Answer {
|
|
read_point(h, a, |f, c, an| pair(f.to_script(c, an.h_in)))
|
|
}
|
|
|
|
fn pt_set_left(h: &super::Host, a: &[Data]) -> Answer {
|
|
let p = assigned_point(h, a, "leftDirection")?;
|
|
edit_point(h, a, |an| an.h_in = p)
|
|
}
|
|
|
|
fn pt_right(h: &super::Host, a: &[Data]) -> Answer {
|
|
read_point(h, a, |f, c, an| pair(f.to_script(c, an.h_out)))
|
|
}
|
|
|
|
fn pt_set_right(h: &super::Host, a: &[Data]) -> Answer {
|
|
let p = assigned_point(h, a, "rightDirection")?;
|
|
edit_point(h, a, |an| an.h_out = p)
|
|
}
|
|
|
|
fn pt_type(h: &super::Host, a: &[Data]) -> Answer {
|
|
read_point(h, a, |_, _, an| Data::String(if an.kind == AnchorKind::Smooth { "PointType.SMOOTH" } else { "PointType.CORNER" }.into()))
|
|
}
|
|
|
|
fn pt_set_type(h: &super::Host, a: &[Data]) -> Answer {
|
|
let kind = match arg(a, 1) {
|
|
Data::String(s) if s == "PointType.CORNER" => AnchorKind::Corner,
|
|
Data::String(s) if s == "PointType.SMOOTH" => AnchorKind::Smooth,
|
|
_ => return Err(HostError::Type("pointType must be a PointType value".into())),
|
|
};
|
|
edit_point(h, a, |an| an.kind = kind)
|
|
}
|
|
|
|
fn pt_parent(h: &super::Host, a: &[Data]) -> Answer {
|
|
let (item, _) = this_point(h, a)?;
|
|
Ok(path_ref(h, item))
|
|
}
|
|
|
|
/// `typename` of a point that is still on its path.
|
|
fn pt_typename(h: &super::Host, a: &[Data]) -> Answer {
|
|
this_point(h, a)?;
|
|
Ok(Data::String("PathPoint".into()))
|
|
}
|
|
|
|
/// Whether a group or compound path that holds the path `id` is selected, which selects the path
|
|
/// as a whole whatever the path's own selection is.
|
|
fn held_selected(d: &crate::Document, sel: &crate::doc::Selection, id: NodeId) -> bool {
|
|
d.ancestry(id).is_some_and(|a| a.iter().any(|i| *i != id && sel.contains(*i) && d.node(*i).is_some_and(|n| !n.is_layer())))
|
|
}
|
|
|
|
/// The point at index `i` of `subs` is a selected anchor point of the path `id` in `sel`: every
|
|
/// point of a path selected as a whole is (a path is when a group or compound path that holds it
|
|
/// is selected, `held`), and of a path with direct-selected anchors only those anchors are. The
|
|
/// endpoints of selected segments are not.
|
|
fn anchor_selected(sel: &crate::doc::Selection, id: NodeId, held: bool, subs: &[SubPath], i: usize) -> bool {
|
|
if held {
|
|
return true;
|
|
}
|
|
if !sel.contains(id) || sel.segments.contains_key(&id) {
|
|
return false;
|
|
}
|
|
match sel.partial(id) {
|
|
None => true,
|
|
Some(picked) => locate(subs, i).is_some_and(|r| picked.contains(&r)),
|
|
}
|
|
}
|
|
|
|
/// Whether the subpaths `a` and `b` have the same number of points in the same subpaths, so that a
|
|
/// point index names the same anchor in both.
|
|
fn same_layout(a: &[SubPath], b: &[SubPath]) -> bool {
|
|
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x.anchors.len() == y.anchors.len())
|
|
}
|
|
|
|
/// `selected`: `PathPointSelection.ANCHORPOINT` when the point is a selected anchor point
|
|
/// ([`anchor_selected`]), otherwise `PathPointSelection.NOSELECTION`. The point changes waiting in
|
|
/// [`super::State`] are written first only when they add or remove points of this path, since
|
|
/// the selection names anchors by their place in the document's path.
|
|
fn pt_selected(h: &super::Host, a: &[Data]) -> Answer {
|
|
let (item, i) = this_point(h, a)?;
|
|
let moved = match &*h.dom.pending.borrow() {
|
|
Some((waiting, subs)) if same_path(*waiting, item) => !read_path(h, item, |_, _, pd| same_layout(subs, &pd.subpaths))?,
|
|
_ => false,
|
|
};
|
|
if moved {
|
|
super::flush(h)?;
|
|
}
|
|
let on =
|
|
read_path(h, item, |st, _, pd| anchor_selected(&st.selection, item.id, held_selected(&st.doc, &st.selection, item.id), &pd.subpaths, i))?;
|
|
Ok(Data::String(if on { "PathPointSelection.ANCHORPOINT" } else { "PathPointSelection.NOSELECTION" }.into()))
|
|
}
|
|
|
|
/// The `PathPointSelection` values that select a point's direction handles.
|
|
const HANDLE_SELECTIONS: &[&str] = &["PathPointSelection.LEFTDIRECTION", "PathPointSelection.RIGHTDIRECTION", "PathPointSelection.LEFTRIGHTPOINT"];
|
|
|
|
/// `selected = PathPointSelection.ANCHORPOINT` adds the point to the direct-selected anchors, and
|
|
/// `NOSELECTION` removes it, with `select.anchorsMany`; the other selected objects and anchors stay
|
|
/// selected, except that selecting an anchor of a path with selected segments replaces that path's
|
|
/// segment selection, as `select.anchorsMany` does. A value the point has already runs no command.
|
|
/// Direction handles cannot be selected, so `LEFTDIRECTION`, `RIGHTDIRECTION` and
|
|
/// `LEFTRIGHTPOINT` throw an error, and so does `NOSELECTION` on a path selected through a group or
|
|
/// compound path that holds it, and `ANCHORPOINT` on a guide while guides are locked. The setter
|
|
/// writes the waiting point changes first, as every member that is not a point function does.
|
|
fn pt_set_selected(h: &super::Host, a: &[Data]) -> Answer {
|
|
let on = match arg(a, 1) {
|
|
Data::String(s) if s == "PathPointSelection.ANCHORPOINT" => true,
|
|
Data::String(s) if s == "PathPointSelection.NOSELECTION" => false,
|
|
Data::String(s) if HANDLE_SELECTIONS.contains(&s.as_str()) => {
|
|
return Err(HostError::Error(format!("PathPoint.selected = {s} is not available yet: only anchor points can be selected")));
|
|
}
|
|
_ => return Err(HostError::Type("selected must be a PathPointSelection value".into())),
|
|
};
|
|
let (item, i) = this_point(h, a)?;
|
|
let (now, at, selectable, held, guide) = with_doc(h, item.doc, |_, st| {
|
|
let node = st.doc.node(item.id);
|
|
let subs = node.and_then(|n| n.path_data()).map_or(&[][..], |pd| &pd.subpaths[..]);
|
|
let guide = node.is_some_and(|n| matches!(n.kind, NodeKind::Path { guide: true, .. }));
|
|
let selectable = crate::cmd::select::is_selectable(&st.doc, st.isolation, &st.doc.locked_artboards(), item.id);
|
|
let held = held_selected(&st.doc, &st.selection, item.id);
|
|
(anchor_selected(&st.selection, item.id, held, subs, i), locate(subs, i), selectable, held, guide)
|
|
});
|
|
if now == on {
|
|
return Ok(Data::Undefined);
|
|
}
|
|
let Some((s, j)) = at else { return Err(HostError::Error(STALE_POINT.into())) };
|
|
if held {
|
|
return Err(HostError::Error(format!(
|
|
"PathPoint.selected = PathPointSelection.NOSELECTION is not available yet on PathItem {}: the group or compound path that holds it is selected",
|
|
item.id.0
|
|
)));
|
|
}
|
|
if on && !selectable {
|
|
return Err(HostError::Error(format!("Cannot select PathItem {}", item.id.0)));
|
|
}
|
|
if on && guide && h.session.borrow().menu.guides_locked {
|
|
return Err(HostError::Error(format!("Cannot select PathItem {}: guides are locked", item.id.0)));
|
|
}
|
|
let params = json!({"items": [{"id": item.id.0, "anchors": [[s, j]]}], "mode": if on { "add" } else { "subtract" }});
|
|
on_doc(h, item.doc.uid, false, |h| command(h, "select.anchorsMany", ¶ms))?;
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// `selectedPathPoints`: the points that are selected anchor points ([`anchor_selected`]), in the
|
|
/// order of `pathPoints`.
|
|
fn path_selected_points(h: &super::Host, a: &[Data]) -> Answer {
|
|
let item = this_path(h, a)?;
|
|
let picked: Vec<usize> = read_path(h, item, |st, _, pd| {
|
|
let sel = &st.selection;
|
|
let held = held_selected(&st.doc, sel, item.id);
|
|
if !held && (!sel.contains(item.id) || sel.segments.contains_key(&item.id)) {
|
|
return Vec::new();
|
|
}
|
|
let mut out = Vec::new();
|
|
let mut first = 0;
|
|
for (s, sp) in pd.subpaths.iter().enumerate() {
|
|
let n = sp.anchors.len();
|
|
out.extend((0..n).filter(|j| held || sel.partial(item.id).is_none_or(|p| p.contains(&(s, *j)))).map(|j| first + j));
|
|
first += n;
|
|
}
|
|
out
|
|
})?;
|
|
h.charge_call(picked.len() as u64)?;
|
|
let [high, low] = id_parts(item.id);
|
|
let cuts = float(point_cuts(h, item));
|
|
let points = picked.into_iter().map(|i| host_ref("PathPoint", &[float(h.run), float(item.doc.uid), high, low, float(i as u64), cuts])).collect();
|
|
Ok(Data::Array(points))
|
|
}
|
|
|
|
/// `remove()`: deletes the point; a subpath left with no points goes too.
|
|
fn pt_remove(h: &super::Host, a: &[Data]) -> Answer {
|
|
let (item, i) = this_point(h, a)?;
|
|
// A direct selection of the path's points is renumbered after the removal.
|
|
let selected = {
|
|
let s = h.session.borrow();
|
|
let st = s.documents().iter().find(|d| d.uid == item.doc.uid).ok_or_else(|| HostError::Error(super::STALE_DOCUMENT.into()))?;
|
|
let sel = &st.selection;
|
|
(sel.anchors.contains_key(&item.id) || sel.segments.contains_key(&item.id)).then(|| sel.clone())
|
|
};
|
|
// Writing the points can prune the selection, and selecting loads the selected paths' paint
|
|
// into new art, so new art's paint, the defaults a script set included, is put back.
|
|
keeping_paint(h, || remove_point(h, item, i, selected))?;
|
|
cut_points(h, item);
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// Removes point `i` of the path of `item` for [`pt_remove`], and renumbers `selected`, the
|
|
/// direct selection of the path's points before the removal.
|
|
fn remove_point(h: &super::Host, item: Item, i: usize, selected: Option<crate::doc::Selection>) -> std::result::Result<(), HostError> {
|
|
let removed = edit_points(h, item, |subs| {
|
|
let (s, j) = locate(subs, i)?;
|
|
let sp = subs.get_mut(s)?;
|
|
// The segment that closes a closed subpath runs from its last anchor to its first.
|
|
let last = sp.closed.then(|| sp.anchors.len() - 1);
|
|
sp.anchors.remove(j);
|
|
let gone = sp.anchors.is_empty();
|
|
if gone {
|
|
subs.remove(s);
|
|
}
|
|
Some((s, j, gone, last))
|
|
})?;
|
|
if let (Some((s, j, gone, last)), Some(before)) = (removed, selected) {
|
|
let mut sel = before.clone();
|
|
renumber_selected(&mut sel, item.id, s, j, gone, last);
|
|
// A selection the removal leaves as it was waits with the points, so that removals in a
|
|
// row are written together, unless its anchors are now all of the path's, which selects
|
|
// the path whole, or the path is in an Objects on Path group, which places it again.
|
|
let wait = sel == before && {
|
|
let valid = read_points(h, item, |subs| {
|
|
sel.anchors.get(&item.id).is_none_or(|a| a.iter().all(|(p, k)| subs.get(*p).is_some_and(|sp| *k < sp.anchors.len())))
|
|
&& sel.segments.get(&item.id).is_none_or(|a| a.iter().all(|(p, k)| subs.get(*p).is_some_and(|sp| sp.has_segment(*k, true))))
|
|
})?;
|
|
let total = points_of(h, item)?;
|
|
let all = sel.anchors.get(&item.id).is_some_and(|a| a.len() >= total) && !sel.segments.contains_key(&item.id);
|
|
let on_path = with_doc(h, item.doc, |_, st| {
|
|
let d = &st.doc;
|
|
d.ancestry(item.id).is_some_and(|a| a.iter().any(|g| d.node(*g).is_some_and(vectorcraft_doc::objects_on_path::is_objects_on_path)))
|
|
});
|
|
valid && !all && !on_path
|
|
};
|
|
if !wait {
|
|
// The points are written first, and the selection is then set with selection
|
|
// commands, which the journal records with the edit.
|
|
flush(h)?;
|
|
on_doc(h, item.doc.uid, false, |h| {
|
|
let doc = h.session.borrow().active().map(|st| st.doc.clone()).ok_or_else(|| HostError::Error(super::STALE_DOCUMENT.into()))?;
|
|
super::restore_selection(h, &doc, &sel)
|
|
})?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Updates the direct selection `sel` of the path `id` after the anchor `j` of its subpath `s` was
|
|
/// removed, and the subpath with it when `gone`: the anchor, the segments it ended and their other
|
|
/// ends leave the selection, unless a segment still selected ends there, and the anchors, segments
|
|
/// and subpaths after it keep the points they selected. `last` is the index of the subpath's last
|
|
/// anchor before the removal when the subpath is closed: its closing segment runs from that anchor
|
|
/// to anchor 0.
|
|
fn renumber_selected(sel: &mut crate::doc::Selection, id: NodeId, s: usize, j: usize, gone: bool, last: Option<usize>) {
|
|
// The closing segment ends at the removed anchor when the removed anchor is the first one.
|
|
let closing = last.filter(|_| j == 0);
|
|
// An anchor (or a segment, by its first anchor) after the removed one moves down one place.
|
|
let shift = |(p, k): (usize, usize), segment: bool| -> Option<(usize, usize)> {
|
|
match p.cmp(&s) {
|
|
std::cmp::Ordering::Less => Some((p, k)),
|
|
std::cmp::Ordering::Greater => Some((if gone { p - 1 } else { p }, k)),
|
|
std::cmp::Ordering::Equal if gone => None,
|
|
std::cmp::Ordering::Equal if k == j || (segment && (k + 1 == j || closing == Some(k))) => None,
|
|
std::cmp::Ordering::Equal => Some((p, if k > j { k - 1 } else { k })),
|
|
}
|
|
};
|
|
// The other ends of the selected segments that the removal joins, in the new numbering: the
|
|
// anchor before the removed one and the one after it.
|
|
let mut ends: Vec<(usize, usize)> = Vec::new();
|
|
if let Some(segments) = sel.segments.get_mut(&id) {
|
|
let cut: Vec<(usize, usize)> = segments.iter().copied().filter(|g| shift(*g, true).is_none() && g.0 == s && !gone).collect();
|
|
for (p, k) in cut {
|
|
// Segment k runs from anchor k to anchor k + 1; one of the two is the removed one.
|
|
// The closing segment of a closed subpath ends at anchor 0.
|
|
let other = if k != j {
|
|
k
|
|
} else if last == Some(k) {
|
|
0
|
|
} else {
|
|
k + 1
|
|
};
|
|
// The closing segment's other end is its first anchor, the old last one.
|
|
let other = if closing == Some(k) { k } else { other };
|
|
ends.extend(shift((p, other), false));
|
|
}
|
|
*segments = segments.iter().filter_map(|g| shift(*g, true)).collect();
|
|
if segments.is_empty() {
|
|
sel.segments.remove(&id);
|
|
}
|
|
}
|
|
let kept_ends: Vec<(usize, usize)> =
|
|
sel.segments.get(&id).map(|g| g.iter().flat_map(|(p, k)| [(*p, *k), (*p, k + 1)]).collect()).unwrap_or_default();
|
|
if let Some(anchors) = sel.anchors.get_mut(&id) {
|
|
*anchors = anchors.iter().filter_map(|a| shift(*a, false)).filter(|a| !ends.contains(a) || kept_ends.contains(a)).collect();
|
|
}
|
|
if let Some((key, a)) = sel.key_anchor
|
|
&& key == id
|
|
{
|
|
sel.key_anchor = shift(a, false).filter(|a| sel.anchors.get(&id).is_some_and(|set| set.contains(a))).map(|a| (key, a));
|
|
}
|
|
}
|
|
|
|
// ---- Registration ----
|
|
|
|
/// The members `PathItem` adds to `PageItem`'s.
|
|
pub(super) fn path_item_members(l: &Link) -> Vec<Described> {
|
|
let transform = |first: &[(&'static str, &'static str)]| {
|
|
let mut args: Vec<(&str, &str)> = first.to_vec();
|
|
args.extend([
|
|
("changePositions", "boolean"),
|
|
("changeFillPatterns", "boolean"),
|
|
("changeFillGradients", "boolean"),
|
|
("changeStrokePattern", "boolean"),
|
|
]);
|
|
args
|
|
};
|
|
let mut resize = transform(&[("scaleX", "number"), ("scaleY", "number")]);
|
|
resize.extend([("changeLineWidths", "number"), ("scaleAbout", "Transformation")]);
|
|
let mut rotate = transform(&[("angle", "number")]);
|
|
rotate.push(("rotateAbout", "Transformation"));
|
|
let translate = [
|
|
("deltaX", "number"),
|
|
("deltaY", "number"),
|
|
("transformObjects", "boolean"),
|
|
("transformFillPatterns", "boolean"),
|
|
("transformFillGradients", "boolean"),
|
|
("transformStrokePattern", "boolean"),
|
|
];
|
|
let mut m = named(vec![
|
|
("pathPoints", point_get(l, path_points), MemberInfo { collection: true, ..typed("PathPoints") }),
|
|
("closed", point_property(l, path_closed, path_set_closed), typed("boolean")),
|
|
("evenodd", property(l, path_evenodd, path_set_evenodd), typed("boolean")),
|
|
("area", get(l, path_area), typed("number")),
|
|
("length", get(l, path_length), typed("number")),
|
|
("guides", property(l, path_guides, path_set_guides), typed("boolean")),
|
|
("clipping", get(l, path_clipping), typed("boolean")),
|
|
("setEntirePath", point_method(l, path_set_entire), returns("undefined", &[("pathPoints", "Array")])),
|
|
("filled", property(l, path_filled, path_set_filled), typed("boolean")),
|
|
("fillColor", property(l, path_fill_color, path_set_fill_color), typed("Color")),
|
|
("fillOverprint", property(l, path_fill_overprint, path_set_fill_overprint), typed("boolean")),
|
|
("stroked", property(l, path_stroked, path_set_stroked), typed("boolean")),
|
|
("strokeColor", property(l, path_stroke_color, path_set_stroke_color), typed("Color")),
|
|
("strokeOverprint", property(l, path_stroke_overprint, path_set_stroke_overprint), typed("boolean")),
|
|
(
|
|
"strokeWidth",
|
|
property(l, path_stroke_width, path_set_stroke_width),
|
|
MemberInfo { min: Some(0.0), max: Some(MAX_STROKE_WIDTH), ..typed("number") },
|
|
),
|
|
("strokeCap", property(l, path_stroke_cap, path_set_stroke_cap), typed("StrokeCap")),
|
|
("strokeJoin", property(l, path_stroke_join, path_set_stroke_join), typed("StrokeJoin")),
|
|
("strokeMiterLimit", property(l, path_miter, path_set_miter), typed("number")),
|
|
("strokeDashes", property(l, path_dashes, path_set_dashes), typed("Array")),
|
|
("strokeDashOffset", property(l, path_dash_offset, path_set_dash_offset), typed("number")),
|
|
("translate", method(l, path_translate), returns("undefined", &translate)),
|
|
("rotate", method(l, path_rotate), returns("undefined", &rotate)),
|
|
("resize", method(l, path_resize), returns("undefined", &resize)),
|
|
("selectedPathPoints", get(l, path_selected_points), typed("Array")),
|
|
]);
|
|
m.extend(["polarity", "resolution"].into_iter().map(|n| later(l, "PathItem", n)));
|
|
m
|
|
}
|
|
|
|
/// `PathItems`, `PathPoints` and `PathPoint`.
|
|
pub(super) fn classes(l: &Link) -> Vec<HostClass> {
|
|
let corner = [("top", "number"), ("left", "number"), ("width", "number"), ("height", "number")];
|
|
let center = [("centerX", "number"), ("centerY", "number"), ("radius", "number")];
|
|
let with = |first: &[(&'static str, &'static str)], rest: &[(&'static str, &'static str)]| -> Vec<(&'static str, &'static str)> {
|
|
first.iter().chain(rest).copied().collect()
|
|
};
|
|
vec![
|
|
class(
|
|
"PathItems",
|
|
None,
|
|
named(vec![
|
|
("typename", get(l, items_typename), typed("string")),
|
|
("length", get(l, items_length), typed("number")),
|
|
("parent", get(l, items_parent), typed("Document")),
|
|
("index", method(l, items_index), returns("PathItem", &[("itemKey", "*")])),
|
|
("getByName", method(l, items_by_name), returns("PathItem", &[("name", "string")])),
|
|
("removeAll", method(l, items_remove_all), returns("undefined", &[])),
|
|
("add", method(l, items_add), returns("PathItem", &[])),
|
|
("rectangle", method(l, items_rectangle), returns("PathItem", &with(&corner, &[("reversed", "boolean")]))),
|
|
(
|
|
"roundedRectangle",
|
|
method(l, items_rounded_rectangle),
|
|
returns("PathItem", &with(&corner, &[("horizontalRadius", "number"), ("verticalRadius", "number"), ("reversed", "boolean")])),
|
|
),
|
|
("ellipse", method(l, items_ellipse), returns("PathItem", &with(&corner, &[("reversed", "boolean"), ("inscribed", "boolean")]))),
|
|
("polygon", method(l, items_polygon), returns("PathItem", &with(¢er, &[("sides", "number"), ("reversed", "boolean")]))),
|
|
(
|
|
"star",
|
|
method(l, items_star),
|
|
returns("PathItem", &with(¢er, &[("innerRadius", "number"), ("points", "number"), ("reversed", "boolean")])),
|
|
),
|
|
]),
|
|
Some(super::function(l, items_at)),
|
|
),
|
|
class(
|
|
"PathPoints",
|
|
None,
|
|
named(vec![
|
|
("typename", point_get(l, points_typename), typed("string")),
|
|
("length", point_get(l, points_length), typed("number")),
|
|
("parent", point_get(l, points_parent), typed("PathItem")),
|
|
("index", point_method(l, points_index), returns("PathPoint", &[("itemKey", "*")])),
|
|
("add", point_method(l, points_add), returns("PathPoint", &[])),
|
|
("removeAll", point_method(l, points_remove_all), returns("undefined", &[])),
|
|
]),
|
|
Some(super::point_function(l, points_at)),
|
|
),
|
|
class(
|
|
"PathPoint",
|
|
None,
|
|
named(vec![
|
|
("typename", point_get(l, pt_typename), typed("string")),
|
|
("parent", point_get(l, pt_parent), typed("PathItem")),
|
|
("anchor", point_property(l, pt_anchor, pt_set_anchor), typed("Array")),
|
|
("leftDirection", point_property(l, pt_left, pt_set_left), typed("Array")),
|
|
("rightDirection", point_property(l, pt_right, pt_set_right), typed("Array")),
|
|
("pointType", point_property(l, pt_type, pt_set_type), typed("PointType")),
|
|
(
|
|
"selected",
|
|
HostMember::Property { get: super::point_function(l, pt_selected), set: super::function(l, pt_set_selected) },
|
|
typed("PathPointSelection"),
|
|
),
|
|
("remove", point_method(l, pt_remove), returns("undefined", &[])),
|
|
]),
|
|
None,
|
|
),
|
|
]
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn a_made_id_above_two_to_the_53_stays_exact() {
|
|
assert_eq!(made_id(&json!({"id": 9_007_199_254_740_993_u64})), Ok(NodeId(9_007_199_254_740_993)));
|
|
assert_eq!(made_id(&json!({"id": 7})), Ok(NodeId(7)));
|
|
assert!(made_id(&json!({"id": 1.5})).is_err());
|
|
assert!(made_id(&json!({})).is_err());
|
|
}
|
|
}
|