Files
vectorcraft/crates/engine/src/cmd/addons/cleanup.rs
T
dotCooCoo 05527ee0f1 Artwork Cleanup: path commands take locked and hidden art named by ids
Simplify Anchors, Deep Ungroup and Rejoin Path Pieces now follow the
rule the text tools use: with `ids`, they act on the locked and hidden
paths and groups the ids name, and on those inside the groups and
layers they name, but not on art on a locked artboard. From the
selection they still take only art that can be edited.

- Rejoin Path Pieces joins only pieces alike in being hidden and in
  being locked (counting the groups and layers above them), so a joined
  path does not show hidden art or unlock locked art. Locked and hidden
  paths it makes are not selected. A compound path member named
  directly stays out, as members reached through the compound do.
- Deep Ungroup takes apart named locked and hidden groups, and the
  groups inside a named group or layer; a named layer stays. Objects of
  a hidden group become hidden, and objects of a locked group become
  locked.
- Simplify Anchors reports every named path it took in `ids`.
2026-11-14 18:49:20 -08:00

452 lines
22 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Artwork Cleanup (`addon.cleanup.*`), a built-in add-on: Simplify Anchors (Keep Shape), the
//! Document Health Check (check, show and fix the issues it finds), Deep Ungroup and Rejoin Path
//! Pieces, and its text tools ([`text`]). Its panel and its submenu, Object › Plug-ins › Artwork
//! Cleanup, are the UI's.
mod checks;
#[cfg(test)]
mod tests;
mod text;
use serde_json::{Map, Value, json};
use vectorcraft_doc::{Document, LockedArtboards, Node, NodeId, NodeKind};
use vectorcraft_geom::{PathData, SubPath};
use vectorcraft_pathops::addons as po;
use self::checks::{Kind, MAX_OBJECTS, Outcome, Scope, set_path};
use super::super::edit::selected_roots;
use super::super::{CommandSpec, bad, checked_ids_or_selection, cmd, has_doc, has_selection, number_or, switch_or, text_param};
use crate::{EngineError, Result, Session};
/// Simplify Anchors' tolerance by default.
const TOLERANCE: f64 = 20.0;
/// Rejoin Path Pieces' gap by default, and the widest (pt).
const GAP: f64 = 0.1;
const MAX_GAP: f64 = 6.0;
/// Most anchors Simplify Anchors takes at once.
const MAX_ANCHORS: usize = 2_000_000;
/// Most rounds of fixes `addon.cleanup.fix` runs.
const FIX_ROUNDS: usize = 3;
/// Most ids a finding lists (its count counts them all).
const MAX_LISTED: usize = 1000;
pub fn specs() -> Vec<CommandSpec> {
let mut v = vec![
cmd!(
"addon.cleanup.simplify",
"Simplify Anchors (Keep Shape)…",
["Object", "Plug-ins", "Artwork Cleanup"],
None,
"{tolerance?: 1..100 (20; higher removes more anchors and lets the outline move further from where it was, up to tolerance × 0.02 pt), protectCorners?: bool (false; anchors where the path turns by more than 30° stay), ids?: [id…] (default: the selection)} remove the anchors each path under the objects can do without, refitting the handles beside each so the outline keeps its shape, as one undo step when a path changes; open ends stay; from the selection, locked and hidden paths are left out, and named by `ids`, the paths of the objects, groups and layers it names are taken even when locked or hidden, but not those on a locked artboard; a changed path loses its selected anchors and segments → {ids, before, after} anchor counts",
has_selection,
simplify
),
cmd!(
query "addon.cleanup.check",
"Health Check",
["Object", "Plug-ins", "Artwork Cleanup"],
None,
"{scope?: \"selection\"|\"document\" (default: the selection, or the document while nothing is selected), kinds?: [kind…] (default: all: emptyText, strayPoints (paths of one anchor), unpainted, zeroOpacity, duplicateObjects (a path stacked on an identical one, or type on the same type within 1 pt), offArtboard, duplicateAnchors (neighbours within 0.01 pt), tinyHandles (under 0.01 pt), collinearAnchors (corners on a straight run, turning less than sine 0.005), nearPoints (anchors within 0.01 pt of other paths' anchors), nearlyClosed (ends within 0.01 pt), filledOpenPaths (filled, unstroked open paths), needlessClipGroups (an unpainted clip that clips nothing, hides everything, or is a rectangle round all its art), needlessCompounds (one path or none), needlessGroups (one member or none, nothing of their own), emptyArtboards (the document only))} find issues without changing anything; locked and hidden objects are left out → {scope, total, findings: [{kind, label, count, ids (the objects, at most 1000), artboards (indexes)}]} for the kinds found",
has_doc,
check
),
cmd!(
query "addon.cleanup.show",
"Show Issue",
["Object", "Plug-ins", "Artwork Cleanup"],
None,
"{kind: kind (see addon.cleanup.check), scope?: \"selection\"|\"document\"} select the objects with that issue → {kind, count, ids, artboards}",
has_doc,
show
),
cmd!(
"addon.cleanup.fix",
"Fix All Issues",
["Object", "Plug-ins", "Artwork Cleanup"],
None,
"{scope?: \"selection\"|\"document\", kinds?: [kind…] (default: all; see addon.cleanup.check)} fix the issues addon.cleanup.check finds, in up to three rounds while a round still finds some (a fix can leave issues for another kind), as one undo step: what is empty, unpainted, invisible, duplicated or off the artboards goes, anchors merge, snap or go, tiny handles retract, paths close and needless groups, compound paths and clipping groups give way to their contents → {scope, total, fixed: {kind: count}}",
has_doc,
fix
),
cmd!(
"addon.cleanup.deepUngroup",
"Deep Ungroup…",
["Object", "Plug-ins", "Artwork Cleanup"],
None,
"{includeClipGroups?: bool (false: clipping groups, and the art inside them, stay as they are; true: they are ungrouped too and their clipping paths become plain paths), ids?: [id…] (default: the selection)} ungroup every level of the groups among the objects, as one undo step when a group goes; each object keeps the opacity of the groups it was in; from the selection, locked and hidden groups stay whole; named by `ids`, the groups it names and the groups in the groups and layers it names are ungrouped even when locked or hidden (a named layer stays), but not those on a locked artboard, and their objects become hidden or locked when a group was; locked and hidden objects stay locked or hidden and are not selected → {ids (the objects selected: those left, or with no group to take apart the selection as it was), ungrouped (groups)}",
has_selection,
deep_ungroup
),
cmd!(
"addon.cleanup.rejoin",
"Rejoin Path Pieces…",
["Object", "Plug-ins", "Artwork Cleanup"],
None,
"{gap?: pt 0..6 (0.1), ids?: [id…] (default: the selection)} weld the open paths under the objects whose ends lie within the gap of each other back into paths: an end joins one other end, the one the path turns least to go on into, pieces may run either way, and a chain whose ends meet closes; each path takes the place and paint of its topmost piece, as one undo step when a path is made; from the selection, locked and hidden paths are left out, and named by `ids`, they are taken, but not those on a locked artboard; only pieces alike in being hidden (by themselves or a group or layer above them) and in being locked join; locked and hidden paths made are not selected → {ids, joined (paths made), removed (pieces merged into them)}",
has_selection,
rejoin
),
];
v.extend(text::specs());
v
}
/// Whether a request names its objects with `ids` (present and not null) rather than taking the
/// selection.
fn named(p: &Value) -> bool {
p.get("ids").is_some_and(|v| !v.is_null())
}
/// Keep the items of `items` whose object (`id`) a command acts on: when `named`, every one not on
/// a locked artboard, locked and hidden ones (and ones in a locked or hidden group or layer)
/// included; else only those that can be edited.
fn retain_targets<T>(d: &Document, items: &mut Vec<T>, named: bool, id: impl Fn(&T) -> NodeId) {
if named {
let locks = d.locked_artboards();
items.retain(|t| !locks.holds_id(id(t)));
} else {
let editable = d.editable();
items.retain(|t| editable(id(t)));
}
}
/// The paths (not guides) under `roots`, compound path members included, that a command acts on
/// ([`retain_targets`]).
fn paths_under(d: &Document, roots: &[NodeId], named: bool) -> Vec<NodeId> {
let mut out = vec![];
for n in roots.iter().filter_map(|id| d.node(*id)) {
n.walk(&mut |c| {
if matches!(c.kind, NodeKind::Path { guide: false, .. }) {
out.push(c.id);
}
});
}
retain_targets(d, &mut out, named, |id| *id);
out
}
fn simplify(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "addon.cleanup.simplify";
let tolerance = number_or(p, "tolerance", TOLERANCE, C)?;
if !(1.0..=100.0).contains(&tolerance) {
return Err(bad(C, "`tolerance` must be from 1 to 100"));
}
let protect = switch_or(p, "protectCorners", false, C)?;
let roots = checked_ids_or_selection(s, p, C)?;
let d = &s.doc()?.doc;
let ids = paths_under(d, &roots, named(p));
let paths: Vec<(NodeId, &PathData)> = ids.iter().filter_map(|id| Some((*id, d.node(*id)?.path_data()?))).collect();
let before: usize = paths.iter().map(|(_, p)| p.anchor_count()).sum();
if before > MAX_ANCHORS {
return Err(EngineError::Other("Simplify Anchors: the paths have too many anchors to simplify at once".into()));
}
let changed: Vec<(NodeId, PathData)> = paths
.iter()
.filter_map(|(id, path)| {
let np = po::simplify_keep_shape(path, tolerance * po::PT_PER_TOLERANCE, protect);
(np != **path).then_some((*id, np))
})
.collect();
let removed: usize = changed.iter().filter_map(|(id, np)| Some(d.node(*id)?.path_data()?.anchor_count().saturating_sub(np.anchor_count()))).sum();
let after = before.saturating_sub(removed);
// When no anchor can go, the document and its undo history stay as they are.
if !changed.is_empty() {
s.edit("Simplify Anchors (Keep Shape)", |d, sel| {
for (id, np) in changed {
// The anchors left are new ones: the path is selected whole.
sel.anchors.remove(&id);
sel.segments.remove(&id);
set_path(d, id, np);
}
Ok(())
})?;
}
Ok(json!({ "ids": ids.iter().map(|i| i.0).collect::<Vec<_>>(), "before": before, "after": after }))
}
/// The scope a check, show or fix request names (by default the selection, or the document while
/// nothing is selected) and its name.
fn scope_of(s: &Session, p: &Value, cmd: &str) -> Result<(Scope, &'static str)> {
let roots = selected_roots(s)?;
match text_param(p, "scope", cmd)? {
Some("document") => Ok((Scope::Document, "document")),
None if roots.is_empty() => Ok((Scope::Document, "document")),
Some("selection") if roots.is_empty() => Err(bad(cmd, "nothing selected")),
Some("selection") | None => Ok((Scope::Objects(roots), "selection")),
Some(other) => Err(bad(cmd, format!("`scope` must be \"selection\" or \"document\", not {other:?}"))),
}
}
/// The kinds a request names (all by default), in the order Fix runs them.
fn kinds_of(p: &Value, cmd: &str) -> Result<Vec<Kind>> {
let given: Vec<Kind> = match p.get("kinds") {
None | Some(Value::Null) => return Ok(Kind::ALL.to_vec()),
Some(Value::Array(a)) => {
a.iter().map(|v| v.as_str().and_then(Kind::parse).ok_or_else(|| bad(cmd, format!("unknown issue kind {v}")))).collect::<Result<_>>()?
}
Some(_) => return Err(bad(cmd, "`kinds` must be a list of issue kinds")),
};
Ok(Kind::ALL.into_iter().filter(|k| given.contains(k)).collect())
}
/// Kind `kind`'s fix run on a copy of the active document: what Fix would change.
fn dry_run(s: &Session, kind: Kind, scope: &Scope) -> Result<Outcome> {
let st = s.doc()?;
let (mut d, mut sel, mut scope) = ((*st.doc).clone(), st.selection.clone(), scope.clone());
kind.fix(&mut d, &mut sel, &mut scope)
}
fn finding(kind: Kind, o: &Outcome) -> Value {
let ids: Vec<u64> = o.ids.iter().take(MAX_LISTED).map(|i| i.0).collect();
json!({ "kind": kind.id(), "label": kind.label(), "count": o.count, "ids": ids, "artboards": o.artboards })
}
fn check(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "addon.cleanup.check";
let (scope, name) = scope_of(s, p, C)?;
let mut findings = vec![];
let mut total = 0;
for kind in kinds_of(p, C)? {
let o = dry_run(s, kind, &scope)?;
if o.count > 0 {
total += o.count;
findings.push(finding(kind, &o));
}
}
Ok(json!({ "scope": name, "total": total, "findings": findings }))
}
fn show(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "addon.cleanup.show";
let id = text_param(p, "kind", C)?.ok_or_else(|| bad(C, "missing `kind`"))?;
let kind = Kind::parse(id).ok_or_else(|| bad(C, format!("unknown issue kind {id:?}")))?;
let (scope, _) = scope_of(s, p, C)?;
let o = dry_run(s, kind, &scope)?;
if o.count == 0 {
return Err(EngineError::Other("None of these issues are left: check again".into()));
}
// Artboards are no objects to select: the selection stays as it is for them.
let ids = o.ids.clone();
if !ids.is_empty() {
s.select(|_, sel| sel.set(ids))?;
}
Ok(finding(kind, &o))
}
fn fix(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "addon.cleanup.fix";
let (mut scope, name) = scope_of(s, p, C)?;
let kinds = kinds_of(p, C)?;
// Fixed on a copy, kind after kind (what one leaves, the next cleans up), then kept as one step.
let st = s.doc()?;
let (mut d, mut sel) = ((*st.doc).clone(), st.selection.clone());
let mut fixed = Map::new();
let mut total = 0;
// A fix can leave issues for a kind that ran before it (groups unwrapped side by side may hold
// duplicates): the kinds run again, a few rounds at most, until a round finds nothing.
for _ in 0..FIX_ROUNDS {
let before = total;
for &kind in &kinds {
let o = kind.fix(&mut d, &mut sel, &mut scope)?;
if o.count > 0 {
total += o.count;
let n = fixed.get(kind.id()).and_then(Value::as_u64).unwrap_or(0);
fixed.insert(kind.id().into(), json!(n + o.count as u64));
}
}
if total == before {
break;
}
}
// With nothing to fix, the document and its undo history stay as they are.
if total > 0 {
sel.prune(&d);
s.edit("Fix Issues", |doc, selection| {
*doc = d;
*selection = sel;
Ok(())
})?;
}
Ok(json!({ "scope": name, "total": total, "fixed": fixed }))
}
/// Is `n` a group Deep Ungroup takes apart (a clipping group only with `clips`)?
fn ungroupable(n: &Node, clips: bool) -> bool {
matches!(n.kind, NodeKind::Group { clip, .. } if clips || !clip)
}
/// `n` with every level of grouping Deep Ungroup takes apart removed, into `out`; `count` counts
/// the groups. Members take on the opacity of the groups they were in, are hidden when one of them
/// was hidden and locked when one of them was locked, and a clipping path becomes a plain path. A
/// locked or hidden group stays whole unless `named`.
fn flatten(n: Node, clips: bool, named: bool, out: &mut Vec<Node>, count: &mut usize) {
if !ungroupable(&n, clips) || (!named && (n.locked || !n.visible)) {
out.push(n);
return;
}
let NodeKind::Group { children, clip } = &n.kind else { return };
*count += 1;
for (k, c) in children.iter().enumerate() {
let mut c = (**c).clone();
if *clip
&& k == 0
&& let NodeKind::Path { clipping, .. } = &mut c.kind
{
*clipping = false;
}
c.opacity *= n.opacity;
c.visible &= n.visible;
c.locked |= n.locked;
flatten(c, clips, named, out, count);
}
}
/// The outermost groups Deep Ungroup takes apart in layer `n`, its sublayers included, in paint
/// order, leaving out those on a locked artboard.
fn groups_in_layer(d: &Document, n: &Node, clips: bool, out: &mut Vec<NodeId>) {
let locks = d.locked_artboards();
fn visit(n: &Node, clips: bool, locks: &LockedArtboards, out: &mut Vec<NodeId>) {
for c in n.children().into_iter().flatten() {
if c.is_layer() {
visit(c, clips, locks, out);
} else if ungroupable(c, clips) && !locks.holds_id(c.id) {
out.push(c.id);
}
}
}
visit(n, clips, &locks, out);
}
fn deep_ungroup(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "addon.cleanup.deepUngroup";
let clips = switch_or(p, "includeClipGroups", false, C)?;
let named = named(p);
let mut roots = checked_ids_or_selection(s, p, C)?;
let d = &s.doc()?.doc;
// From the selection, locked and hidden objects, and those in locked or hidden groups, stay as
// they are; named, only those on a locked artboard do.
retain_targets(d, &mut roots, named, |id| *id);
// A named layer stays: the groups in it, and in its sublayers, are taken apart.
let mut groups = vec![];
for id in roots {
match d.node(id) {
Some(n) if named && n.is_layer() => {
let at = groups.len();
groups_in_layer(d, n, clips, &mut groups);
if groups.len() == at {
groups.push(id);
}
}
_ => groups.push(id),
}
}
let (mut items, mut selected, mut count) = (vec![], vec![], 0);
for id in &groups {
match d.node(*id).filter(|n| ungroupable(n, clips)) {
Some(n) => {
let mut out = vec![];
flatten(n.clone(), clips, named, &mut out, &mut count);
selected.extend(out.iter().map(|n| n.id));
items.push((*id, out));
}
None => selected.push(*id),
}
}
// With no group to take apart, the document, the selection and the undo history stay as they
// are, and the reply lists the selection.
if count == 0 {
selected = s.doc()?.selection.objects.to_vec();
} else {
selected = s.edit("Deep Ungroup", move |d, sel| {
d.insert_beside(items, true)?;
// Members that are locked or hidden themselves keep that, and are not selected.
let editable = d.editable();
selected.retain(|id| editable(*id));
sel.set(selected.iter().copied());
Ok(selected)
})?;
}
Ok(json!({ "ids": selected.iter().map(|i| i.0).collect::<Vec<_>>(), "ungrouped": count }))
}
/// One path Rejoin Path Pieces takes: its id, its subpath, and whether it and every group and layer
/// above it are visible, and whether they are all unlocked.
type Piece = (NodeId, SubPath, (bool, bool));
/// The open single-subpath paths under `roots` that Rejoin Path Pieces takes, in paint order
/// (compound path members, clipping paths and guides stay out; [`retain_targets`]).
fn pieces(d: &Document, roots: &[NodeId], named: bool) -> Vec<Piece> {
fn visit(n: &Node, above: (bool, bool), out: &mut Vec<Piece>) {
let state = (above.0 && n.visible, above.1 && !n.locked);
match &n.kind {
NodeKind::Path { path, guide: false, clipping: false, .. } => {
if let [sp] = path.subpaths.as_slice()
&& !sp.closed
{
out.push((n.id, sp.clone(), state));
}
}
NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } => children.iter().for_each(|c| visit(c, state, out)),
_ => {}
}
}
let mut out = vec![];
for (id, n) in roots.iter().filter_map(|id| Some((*id, d.node(*id)?))) {
// The groups and layers above the root, and their state.
let above = d.ancestry(id).unwrap_or_default();
let above = above.split_last().map_or(&[][..], |(_, up)| up);
if above.last().and_then(|i| d.node(*i)).is_some_and(|parent| matches!(parent.kind, NodeKind::Compound { .. })) {
continue;
}
let state = above.iter().filter_map(|i| d.node(*i)).fold((true, true), |(v, u), a| (v && a.visible, u && !a.locked));
visit(n, state, &mut out);
}
retain_targets(d, &mut out, named, |piece| piece.0);
out
}
fn rejoin(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "addon.cleanup.rejoin";
let gap = number_or(p, "gap", GAP, C)?;
if !(0.0..=MAX_GAP).contains(&gap) {
return Err(bad(C, "`gap` must be from 0 to 6 pt"));
}
let roots = checked_ids_or_selection(s, p, C)?;
let found = pieces(&s.doc()?.doc, &roots, named(p));
if found.len() > MAX_OBJECTS {
return Err(EngineError::Other("Rejoin Path Pieces: too many paths to rejoin at once".into()));
}
// Only pieces alike in being shown and in being unlocked join, so that a joined path neither
// shows hidden art nor hides shown art, nor locks or unlocks a piece.
let (mut kept, mut gone) = (vec![], vec![]);
for state in [(true, true), (true, false), (false, true), (false, false)] {
let part: Vec<&Piece> = found.iter().filter(|f| f.2 == state).collect();
if part.is_empty() {
continue;
}
let subpaths: Vec<SubPath> = part.iter().map(|f| f.1.clone()).collect();
// Each chain becomes its topmost piece (the last in paint order); the others go.
for j in po::rejoin(&subpaths, gap) {
let ids: Vec<NodeId> = j.members.iter().filter_map(|&m| part.get(m).map(|f| f.0)).collect();
let Some(top) = j.members.iter().max().and_then(|&m| part.get(m).map(|f| f.0)) else { continue };
gone.extend(ids.into_iter().filter(|id| *id != top));
kept.push((top, j.path));
}
}
if !kept.is_empty() {
let ids: Vec<NodeId> = kept.iter().map(|k| k.0).collect();
s.edit("Rejoin Path Pieces", |d, sel| {
for (id, sp) in &kept {
set_path(d, *id, PathData::single(sp.clone()));
}
d.remove_many(&gone)?;
// Locked and hidden paths, and those in locked or hidden groups, are not selected.
let editable = d.editable();
sel.set(ids.into_iter().filter(|id| editable(*id)));
Ok(())
})?;
}
let ids: Vec<u64> = kept.iter().map(|k| k.0.0).collect();
Ok(json!({ "ids": ids, "joined": kept.len(), "removed": gone.len() }))
}