mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Scripts: retracing refuses added artwork; PlacedItem.file resolves linked paths
- Retracing a tracing that holds artwork added after the trace throws before the document changes; the retrace limit counts retraces, not mappings. - Image Trace warnings from trace and option changes go to the console. - PlacedItem.file resolves the link against the document's folder. - saveAs picks the format from the name it was given before resolving aliases. - controlBounds includes handles inside blends, envelopes and other containers.
This commit is contained in:
1 parent
466debdc8f
commit
4edede500d
9 files changed
+258
-9
No files matched your search
@@ -2388,16 +2388,12 @@ fn item_control(h: &Host, a: &[Data]) -> Answer {
|
||||
}
|
||||
|
||||
/// The bounds of the anchors and handles of `n`'s paths: a path's own, and those of the shown
|
||||
/// paths in a group or compound path (a clip group's from its clipping path). `None` for other
|
||||
/// objects.
|
||||
/// paths in its children (a clip group's from its clipping path).
|
||||
fn handle_bounds(n: &crate::doc::Node) -> Option<Rect> {
|
||||
match &n.kind {
|
||||
NodeKind::Path { path, .. } => path.control_bounds(),
|
||||
NodeKind::Group { children, clip: true } => children.first().and_then(|c| handle_bounds(c)),
|
||||
NodeKind::Group { children, .. } | NodeKind::Compound { children, .. } => {
|
||||
children.iter().filter(|c| c.visible).filter_map(|c| handle_bounds(c)).reduce(|a, b| a.union(b))
|
||||
}
|
||||
_ => None,
|
||||
_ => n.children().and_then(|children| children.iter().filter(|c| c.visible).filter_map(|c| handle_bounds(c)).reduce(|a, b| a.union(b))),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -358,7 +358,14 @@ fn placed_file(h: &Host, a: &[Data]) -> Answer {
|
||||
return Ok(Data::Null);
|
||||
}
|
||||
}
|
||||
let path = read_image(h, a, "PlacedItem", |_, _, im| Ok(text(im.link.as_ref().map(|l| l.path.clone()).unwrap_or_default())))?;
|
||||
let path = read_image(h, a, "PlacedItem", |st, _, im| {
|
||||
let path = im.link.as_ref().and_then(|link| {
|
||||
let base = crate::cmd::links::LinkBase::of(st);
|
||||
crate::cmd::links::located_path(link, base).or_else(|| crate::cmd::fileio::document_path(&link.path, base.doc).ok())
|
||||
});
|
||||
let path = path.ok_or_else(|| HostError::Error(super::super::files::ACCESS_DENIED.into()))?;
|
||||
Ok(text(path))
|
||||
})?;
|
||||
let Data::String(path) = path else { return Ok(Data::Null) };
|
||||
if !may_read(h, &path) {
|
||||
return Err(HostError::Error(super::super::files::ACCESS_DENIED.into()));
|
||||
|
||||
@@ -665,6 +665,8 @@ fn doc_save_as(h: &Host, a: &[Data]) -> Answer {
|
||||
} else if !q.is_empty() {
|
||||
p.insert("options".into(), Value::Object(q));
|
||||
}
|
||||
} else if let Some(format) = crate::cmd::fileio::format_for_name(path) {
|
||||
p.insert("format".into(), json!(format.id));
|
||||
}
|
||||
let real = h.disk.borrow().save_target(path).map_err(|m| refused(m, path))?;
|
||||
if given_options.is_none() && extension.as_deref().is_some_and(|e| !e.is_empty()) && crate::cmd::fileio::format_for_name(path).is_none() {
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
//! the saved presets, so any other name throws. A preset name has at most [`MAX_PRESET_NAME`]
|
||||
//! bytes and none of `/`, `\`, `:` or control characters.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -116,6 +116,8 @@ const NOT_IMPLEMENTED: &[&str] = &[
|
||||
pub(super) struct State {
|
||||
next: RefCell<HashMap<(u64, u64), u64>>,
|
||||
sources: RefCell<HashMap<(u64, u64), NodeId>>,
|
||||
descendants: RefCell<HashMap<(u64, u64), HashSet<NodeId>>>,
|
||||
retraces: Cell<usize>,
|
||||
}
|
||||
|
||||
/// `id` while the document `uid` holds it (a run never gives an id out twice, so an undo that put
|
||||
@@ -150,6 +152,15 @@ pub(super) fn remember_source(h: &Host, uid: u64, group: &Node) {
|
||||
if let Some(source) = group.children().and_then(|c| c.first()) {
|
||||
h.dom.tracing.sources.borrow_mut().entry((uid, group.id.0)).or_insert(source.id);
|
||||
}
|
||||
h.dom.tracing.descendants.borrow_mut().entry((uid, group.id.0)).or_insert_with(|| {
|
||||
let mut ids = HashSet::new();
|
||||
group.walk(&mut |n| {
|
||||
if n.id != group.id {
|
||||
ids.insert(n.id);
|
||||
}
|
||||
});
|
||||
ids
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn same_source(h: &Host, uid: u64, original: NodeId, group: &Node) -> bool {
|
||||
@@ -242,6 +253,9 @@ fn replace(h: &Host, item: Item, id: &str, params: &Value, key: &str) -> Result<
|
||||
let mut before = with_doc(h, item.doc, |_, st| st.selection.clone());
|
||||
on_doc(h, item.doc.uid, false, |h| {
|
||||
let (_, r) = command_value(h, id, params)?;
|
||||
for w in r.get("warnings").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str) {
|
||||
super::super::each_line(&format!("{id}: {w}"), |line, cut| h.line(super::super::console::Entry::Log(line), cut));
|
||||
}
|
||||
let new = result_id(&r, key).ok_or_else(|| HostError::Error(format!("{id} gave no object")))?;
|
||||
let remap = |i: NodeId| if i == item.id { new } else { i };
|
||||
before.objects = before.objects.into_iter().map(remap).collect();
|
||||
@@ -265,11 +279,27 @@ fn replace(h: &Host, item: Item, id: &str, params: &Value, key: &str) -> Result<
|
||||
/// Traces the Image Trace object of `item` again with `params` (`id` is added) and makes its
|
||||
/// script objects follow the new one.
|
||||
fn retrace(h: &Host, item: Item, mut params: Map<String, Value>) -> Result<(), HostError> {
|
||||
if h.dom.tracing.next.borrow().len() >= MAX_RETRACES {
|
||||
if h.dom.tracing.retraces.get() >= MAX_RETRACES {
|
||||
return Err(HostError::Range(format!("a run traces a tracing again at most {MAX_RETRACES} times")));
|
||||
}
|
||||
read_group(h, item, |_, group| {
|
||||
remember_source(h, item.doc.uid, group);
|
||||
let descendants = h.dom.tracing.descendants.borrow();
|
||||
let ids = descendants.get(&(item.doc.uid, item.id.0)).ok_or_else(stale)?;
|
||||
let mut added = false;
|
||||
group.walk(&mut |n| added |= n.id != group.id && !ids.contains(&n.id));
|
||||
if added {
|
||||
return Err(HostError::Error("Cannot retrace a tracing containing added artwork".into()));
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
params.insert("id".into(), json!(item.id.0));
|
||||
let new = replace(h, item, "imageTrace.make", &Value::Object(params), "id")?;
|
||||
read_group(h, Item { id: new, ..item }, |_, group| {
|
||||
remember_source(h, item.doc.uid, group);
|
||||
Ok(())
|
||||
})?;
|
||||
h.dom.tracing.retraces.set(h.dom.tracing.retraces.get() + 1);
|
||||
replaced(h, item.doc.uid, item.id, new);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user