mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-24 11:23:43 +01:00
The Blend panel, the Object › Blend, Envelope Distort and Gradient Mesh menu items and their commands looked up every ancestor of each selected object by id to find its nearest blend, envelope or mesh, and dropped repeats with a linear scan. They now read the object's cached place in the tree once, test the nodes on that path, and keep a set of the ones already listed. The checks of whether one is selected stop at the first selected object that has one. An id that the lookup cache does not place on that path (a repeated id, or a cache older than the tree) is still looked up by id, so the results, their order and the errors are the same. Explicit ids given to the live commands drop nested ones with a set as well. With Select All of 102,200 objects (100,000 rectangles, 2,000 blends and 200 envelopes), a release build took a median of 16.5 ms of thread CPU time for the Blend check before and 0.5 ms after.
227 lines
8.9 KiB
Rust
227 lines
8.9 KiB
Rust
use std::collections::HashSet;
|
|
|
|
use super::mesh::whole;
|
|
use super::{
|
|
Arc, BlendEase, BlendEaseType, BlendOrientation, BlendSpacing, Document, EngineError, Node, NodeId, NodeKind, Result, Selection, Session, Value,
|
|
bad, check_numbers, has_selection, json, named_ids, selected_roots, switch_or, text_param,
|
|
};
|
|
|
|
pub(super) fn ids_json(ids: &[NodeId]) -> Value {
|
|
json!({ "ids": ids.iter().map(|i| i.0).collect::<Vec<_>>() })
|
|
}
|
|
|
|
/// Explicit `ids`, or `id` as `ids` of one (in paint order), or the selected roots.
|
|
pub(super) fn roots_param(s: &Session, p: &Value) -> Result<Vec<NodeId>> {
|
|
if let Some(ids) = named_ids(p)? {
|
|
let st = s.doc()?;
|
|
for id in &ids {
|
|
if st.doc.node(*id).is_none() {
|
|
return Err(EngineError::NoNode(*id));
|
|
}
|
|
}
|
|
let mut sel = Selection::default();
|
|
sel.set(ids.iter().copied().filter(|id| st.doc.node(*id).is_some_and(|n| !n.is_layer())));
|
|
let ordered = sel.in_paint_order(&st.doc);
|
|
let given: HashSet<NodeId> = ordered.iter().copied().collect();
|
|
// Drop ids nested inside other given ids.
|
|
return Ok(ordered
|
|
.iter()
|
|
.copied()
|
|
.filter(|id| {
|
|
let anc = st.doc.ancestry(*id).unwrap_or_default();
|
|
!anc[..anc.len().saturating_sub(1)].iter().any(|a| given.contains(a))
|
|
})
|
|
.collect());
|
|
}
|
|
selected_roots(s)
|
|
}
|
|
|
|
/// Selected nodes of a kind (the selection itself or an ancestor). None while an object of a
|
|
/// dynamic symbol instance's art is selected.
|
|
pub(super) fn selected_of(s: &Session, pred: fn(&Node) -> bool) -> Vec<NodeId> {
|
|
let Some(st) = s.active().filter(|st| st.selection.symbol_part.is_none()) else { return vec![] };
|
|
let mut find = st.doc.nearest_finder();
|
|
let mut seen = HashSet::new();
|
|
let mut out = vec![];
|
|
for id in &st.selection.objects {
|
|
if let Some(n) = find.nearest(*id, pred)
|
|
&& seen.insert(n.id)
|
|
{
|
|
out.push(n.id);
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// Whether some selected object's node of a kind ([`selected_of`]) passes `test`; stops at the
|
|
/// first one that does.
|
|
pub(super) fn any_selected_of(s: &Session, pred: fn(&Node) -> bool, test: impl Fn(&Node) -> bool) -> bool {
|
|
let Some(st) = s.active().filter(|st| st.selection.symbol_part.is_none()) else { return false };
|
|
let mut find = st.doc.nearest_finder();
|
|
st.selection.objects.iter().any(|id| find.nearest(*id, pred).is_some_and(&test))
|
|
}
|
|
|
|
pub(super) fn is_blend(n: &Node) -> bool {
|
|
matches!(n.kind, NodeKind::Blend { .. })
|
|
}
|
|
|
|
pub(super) fn is_envelope(n: &Node) -> bool {
|
|
matches!(n.kind, NodeKind::Envelope { .. })
|
|
}
|
|
|
|
pub(super) fn is_mesh(n: &Node) -> bool {
|
|
matches!(n.kind, NodeKind::Mesh(_))
|
|
}
|
|
|
|
pub(super) fn has_blend(s: &Session) -> std::result::Result<(), String> {
|
|
has_selection(s)?;
|
|
crate::cmd::edit::no_symbol_part(s)?;
|
|
if any_selected_of(s, is_blend, |_| true) { Ok(()) } else { Err("select a blend".into()) }
|
|
}
|
|
|
|
pub(super) fn has_envelope(s: &Session) -> std::result::Result<(), String> {
|
|
has_selection(s)?;
|
|
crate::cmd::edit::no_symbol_part(s)?;
|
|
if any_selected_of(s, is_envelope, |_| true) { Ok(()) } else { Err("select an envelope".into()) }
|
|
}
|
|
|
|
pub(super) fn has_mesh(s: &Session) -> std::result::Result<(), String> {
|
|
has_selection(s)?;
|
|
crate::cmd::edit::no_symbol_part(s)?;
|
|
if any_selected_of(s, is_mesh, |_| true) { Ok(()) } else { Err("select a gradient mesh".into()) }
|
|
}
|
|
|
|
/// Wrap `roots` (paint order) into a new node built by `make`, placed where the topmost root was.
|
|
pub(super) fn wrap(d: &mut Document, sel: &mut Selection, roots: &[NodeId], make: impl FnOnce(NodeId, Vec<Arc<Node>>) -> Node) -> Result<NodeId> {
|
|
if roots.is_empty() {
|
|
return Err(EngineError::Other("nothing to wrap".into()));
|
|
}
|
|
let id = d.alloc_id();
|
|
d.wrap(roots, |nodes| make(id, nodes))?;
|
|
sel.set([id]);
|
|
Ok(id)
|
|
}
|
|
|
|
/// Put `nodes` where `id` was (replacing it). Returns their ids.
|
|
pub(super) fn replace_with(d: &mut Document, id: NodeId, nodes: Vec<Node>) -> Result<Vec<NodeId>> {
|
|
let (par, idx, _) = d.position(id).ok_or(EngineError::NoNode(id))?;
|
|
d.remove(id)?;
|
|
let mut out = vec![];
|
|
for (k, n) in nodes.into_iter().enumerate() {
|
|
out.push(n.id);
|
|
d.insert(par, idx + k, n)?;
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// Fresh ids for any id-0 (generated) node in `n`'s subtree; keeps real ids.
|
|
pub(in super::super) fn fix_ids(d: &mut Document, n: &mut Node) {
|
|
if n.id == NodeId(0) {
|
|
n.id = d.alloc_id();
|
|
}
|
|
if let Some(ch) = n.children_mut() {
|
|
for c in ch.iter_mut() {
|
|
fix_ids(d, Arc::make_mut(c));
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------- Blend ----------
|
|
|
|
/// The Specified Steps and Specified Distance a spacing mode starts with.
|
|
pub(super) const DEFAULT_STEPS: u32 = 5;
|
|
|
|
pub(super) const DEFAULT_DISTANCE: f64 = 10.0;
|
|
|
|
pub(super) fn spacing_param(p: &Value, cmd: &str, current: BlendSpacing) -> Result<BlendSpacing> {
|
|
check_numbers(p, &["value", "steps", "distance"], cmd)?;
|
|
let smooth = switch_or(p, "smooth", false, cmd)?;
|
|
let num = |k: &str| p.get(k).and_then(Value::as_f64);
|
|
let steps = |k: &str, n: f64| whole(n, k, cmd).map(|n| BlendSpacing::Steps(n as u32));
|
|
let mode = text_param(p, "spacing", cmd)?;
|
|
let value = num("value");
|
|
if mode.is_none() && value.is_some() {
|
|
return Err(bad(cmd, "`value` needs `spacing` (steps|distance)"));
|
|
}
|
|
let sp = match mode {
|
|
Some("smooth") | Some("smoothColor") => BlendSpacing::SmoothColor,
|
|
Some("steps") => match value {
|
|
Some(v) => steps("value", v)?,
|
|
None => steps("steps", num("steps").unwrap_or(DEFAULT_STEPS as f64))?,
|
|
},
|
|
Some("distance") => BlendSpacing::Distance(value.or(num("distance")).unwrap_or(DEFAULT_DISTANCE)),
|
|
Some(o) => return Err(bad(cmd, format!("unknown spacing `{o}` (smooth|steps|distance)"))),
|
|
None => {
|
|
if let Some(n) = num("steps") {
|
|
steps("steps", n)?
|
|
} else if let Some(d) = num("distance") {
|
|
BlendSpacing::Distance(d)
|
|
} else if smooth {
|
|
BlendSpacing::SmoothColor
|
|
} else {
|
|
current
|
|
}
|
|
}
|
|
};
|
|
match sp {
|
|
BlendSpacing::Steps(n) if !(1..=1000).contains(&n) => Err(bad(cmd, "steps must be 1..1000")),
|
|
BlendSpacing::Distance(d) if !(d.is_finite() && d > 0.0) => Err(bad(cmd, "distance must be > 0")),
|
|
s => Ok(s),
|
|
}
|
|
}
|
|
|
|
/// `orientation`: `page` or `path`; missing or null keeps `current`, and any other value is an error.
|
|
pub(super) fn orientation_param(p: &Value, cmd: &str, current: BlendOrientation) -> Result<BlendOrientation> {
|
|
match p.get("orientation").filter(|v| !v.is_null()) {
|
|
None => Ok(current),
|
|
Some(v) => match v.as_str() {
|
|
Some("path") | Some("alignToPath") => Ok(BlendOrientation::AlignToPath),
|
|
Some("page") | Some("alignToPage") => Ok(BlendOrientation::AlignToPage),
|
|
_ => Err(bad(cmd, "orientation must be page or path")),
|
|
},
|
|
}
|
|
}
|
|
|
|
/// Blend panel Type and Ramp, validated before any document mutation.
|
|
pub(super) fn ease_param(p: &Value, cmd: &str, current: BlendEase) -> Result<BlendEase> {
|
|
let kind = match p.get("easingType").filter(|v| !v.is_null()) {
|
|
None => current.kind,
|
|
Some(v) => match v.as_str() {
|
|
Some("easeIn") => BlendEaseType::In,
|
|
Some("easeOut") => BlendEaseType::Out,
|
|
Some("easeInOut") => BlendEaseType::InOut,
|
|
Some("easeOutIn") => BlendEaseType::OutIn,
|
|
_ => return Err(bad(cmd, "easingType must be easeIn, easeOut, easeInOut or easeOutIn")),
|
|
},
|
|
};
|
|
Ok(BlendEase { kind, ramp: percent_param(p, cmd, "ramp", current.ramp * 100.0, 0.0)? / 100.0 })
|
|
}
|
|
|
|
/// Bounded percentage input; missing or null preserves the current value.
|
|
pub(super) fn percent_param(p: &Value, cmd: &str, key: &str, current: f64, min: f64) -> Result<f64> {
|
|
match p.get(key).filter(|v| !v.is_null()) {
|
|
None => Ok(current),
|
|
Some(v) => v
|
|
.as_f64()
|
|
.filter(|n| n.is_finite() && (min..=100.0).contains(n))
|
|
.ok_or_else(|| bad(cmd, format!("{key} must be a percentage {min}..100"))),
|
|
}
|
|
}
|
|
|
|
/// `starts`: per object of `ids` (else of `roots`), the anchor its blend starts from.
|
|
pub(super) fn starts_param(p: &Value, roots: &[NodeId]) -> Result<Vec<(NodeId, u32)>> {
|
|
const C: &str = "object.blend.make";
|
|
let Some(v) = p.get("starts").filter(|v| !v.is_null()) else { return Ok(vec![]) };
|
|
let list = v.as_array().ok_or_else(|| bad(C, "`starts` must be an array of anchor indices (or nulls)"))?;
|
|
let owners = named_ids(p)?.unwrap_or_else(|| roots.to_vec());
|
|
let mut out = vec![];
|
|
for (id, a) in owners.iter().zip(list) {
|
|
if a.is_null() {
|
|
continue;
|
|
}
|
|
let i = a.as_u64().and_then(|i| u32::try_from(i).ok()).ok_or_else(|| bad(C, "a start must be an anchor index ≥ 0 or null"))?;
|
|
out.push((*id, i));
|
|
}
|
|
Ok(out)
|
|
}
|