Merge main into feat/scripts-api-d8b-tracing

This commit is contained in:
dotCooCoo committed 2026-12-02 02:46:39 -08:00
commit 47a8bcde18
38 files changed
+2780 -190

No files matched your search

+35 -21
View File
@@ -1006,9 +1006,16 @@ pub(super) fn after_kept_command(h: &Host) {
/// selection cannot be put back, the edit is undone too. The points the run read of the item's
/// paths stay valid through the change ([`paths::see_all_points`]).
fn edit_item(h: &Host, item: Item, id: &str, params: &Value) -> std::result::Result<Data, HostError> {
edit_item_with(h, item, |h| command(h, id, params))
}
/// Runs `f` on the item's document as [`edit_item`] runs its command. `f` runs after the document
/// is made active, so bounds it reads take in type that the switch measured again (in the
/// document's own composite fonts, [`Session::set_active`]).
fn edit_item_with<T>(h: &Host, item: Item, f: impl FnOnce(&Host) -> std::result::Result<T, HostError>) -> std::result::Result<T, HostError> {
paths::see_all_points(h);
let kept = h.dom.editing.replace(Some(item));
let r = atomically(h, |h| on_doc(h, item.doc.uid, true, |h| command(h, id, params)));
let r = atomically(h, |h| on_doc(h, item.doc.uid, true, f));
h.dom.editing.set(kept);
r
}
@@ -2604,14 +2611,18 @@ fn move_to(h: &Host, a: &[Data], x: Option<f64>, y: Option<f64>) -> Answer {
let item = this_item(h, a)?;
refuse_protected_edit(h, item)?;
selection_ids(h, item.doc, arg(a, 0))?;
let (Some(r), f) = item_rect(h, item, false)? else { return Ok(Data::Undefined) };
let c = h.dom.coords.get();
let now = f.to_script(c, Point::new(r.x0, r.y0));
let to = f.to_document(c, [x.unwrap_or(now[0]), y.unwrap_or(now[1])]);
let (dx, dy) = (to.x - r.x0, to.y - r.y0);
if dx.abs() > 1e-9 || dy.abs() > 1e-9 {
edit_item(h, item, "object.move", &json!({"ids": [item.id.0], "dx": dx, "dy": dy}))?;
}
// Measured once the document is active.
edit_item_with(h, item, |h| {
let (Some(r), f) = item_rect(h, item, false)? else { return Ok(()) };
let c = h.dom.coords.get();
let now = f.to_script(c, Point::new(r.x0, r.y0));
let to = f.to_document(c, [x.unwrap_or(now[0]), y.unwrap_or(now[1])]);
let (dx, dy) = (to.x - r.x0, to.y - r.y0);
if dx.abs() > 1e-9 || dy.abs() > 1e-9 {
command(h, "object.move", &json!({"ids": [item.id.0], "dx": dx, "dy": dy}))?;
}
Ok(())
})?;
Ok(Data::Undefined)
}
@@ -2645,18 +2656,21 @@ fn resize(h: &Host, a: &[Data], width: bool) -> Answer {
}
refuse_protected_edit(h, item)?;
selection_ids(h, item.doc, arg(a, 0))?;
let (Some(r), _) = item_rect(h, item, false)? else { return Ok(Data::Undefined) };
let now = if width { r.width() } else { r.height() };
if now <= 0.0 {
return Err(HostError::Error(format!("the object has no {what} to scale")));
}
if (now - size).abs() < 1e-9 {
return Ok(Data::Undefined);
}
let pct = size / now * 100.0;
let (sx, sy) = if width { (pct, 100.0) } else { (100.0, pct) };
let p = json!({"ids": [item.id.0], "sx": sx, "sy": sy, "origin": [r.x0, r.y0], "basis": "page"});
edit_item(h, item, "object.scale", &p)?;
// Measured once the document is active, so the ratio applies to the bounds the scale works on.
edit_item_with(h, item, |h| {
let (Some(r), _) = item_rect(h, item, false)? else { return Ok(()) };
let now = if width { r.width() } else { r.height() };
if now <= 0.0 {
return Err(HostError::Error(format!("the object has no {what} to scale")));
}
if (now - size).abs() < 1e-9 {
return Ok(());
}
let pct = size / now * 100.0;
let (sx, sy) = if width { (pct, 100.0) } else { (100.0, pct) };
command(h, "object.scale", &json!({"ids": [item.id.0], "sx": sx, "sy": sy, "origin": [r.x0, r.y0], "basis": "page"}))?;
Ok(())
})?;
Ok(Data::Undefined)
}
+6 -1
View File
@@ -373,7 +373,12 @@ const AI_KEPT: &[(&str, Default)] = &[
];
fn ai_starts() -> Vec<(&'static str, Data)> {
vec![("compressed", Data::Bool(true)), ("pdfCompatible", Data::Bool(true)), ("embedICCProfile", Data::Bool(false))]
vec![
("compatibility", text("Compatibility.ILLUSTRATOR19")),
("compressed", Data::Bool(true)),
("pdfCompatible", Data::Bool(true)),
("embedICCProfile", Data::Bool(false)),
]
}
fn eps_map(name: &str, d: &Data, p: &mut Map<String, Value>) -> std::result::Result<bool, HostError> {
+1 -1
View File
@@ -1352,7 +1352,7 @@ fn encode(s: &str) -> String {
}
/// `s` with its `%xx` escapes read as UTF-8 bytes; `s` as it is when they do not make UTF-8.
fn decode(s: &str) -> String {
pub(crate) fn decode(s: &str) -> String {
let b = s.as_bytes();
let mut out = Vec::with_capacity(b.len());
let mut i = 0;
+45 -4
View File
@@ -359,6 +359,32 @@ fn has_parent(p: &str) -> bool {
p.split(['/', '\\']).any(|name| name == "..")
}
/// The paths `value` can name in a `File`'s URI notation, as `File.parent` and `File.fullName` give
/// them, other than `value` as written: on a platform that reads drive letters (`windows`), `/c/…`
/// is the drive path `C:/…`, and on every platform `%20` and the other escapes are decoded. Only a
/// path that starts with one `/` has them; `$.evalFile` tries them after `value` as written
/// misses. A `//host/share` path, and a form with a `..` name, is left out.
pub fn uri_forms(value: &str, windows: bool) -> Vec<String> {
if !value.starts_with('/') || value.starts_with("//") {
return Vec::new();
}
let drive = value.strip_prefix('/').filter(|_| windows && !value.starts_with("//")).and_then(|rest| {
let (first, after) = rest.split_once('/').unwrap_or((rest, ""));
let letter = first.as_bytes();
(letter.len() == 1 && letter[0].is_ascii_alphabetic()).then(|| format!("{}:/{after}", letter[0].to_ascii_uppercase() as char))
});
let mut forms = Vec::new();
for form in drive.iter().map(String::as_str).chain(drive.is_none().then_some(value)) {
let decoded = crate::script::files::decode(form);
for f in [decoded.as_str(), form] {
if f != value && !has_parent(f) && !(windows && f.replace('\\', "/").starts_with("//")) && !forms.iter().any(|g| g == f) {
forms.push(f.to_string());
}
}
}
forms
}
/// A real path as people read it.
fn display(path: &Path) -> Arc<str> {
Arc::from(crate::cmd::findfiles::plain(path.to_path_buf()).to_string_lossy().as_ref())
@@ -575,13 +601,18 @@ impl Files {
let refused = || (FailKind::Error, format!("script file read refused: {value}"));
// A script given as text has no folder, and so no file it may read.
let Some(dir) = dir.filter(|_| !has_parent(value)) else { return Err(refused()) };
// Each candidate, and whether the scope's refusal passes it over.
let mut candidates: Vec<(Option<PathBuf>, bool)> = vec![(Some(dir.join(path)), false)];
// Each candidate, and whether the scope's refusal passes it over. The path as written comes
// first; for `$.evalFile` only, when it misses, fails or is refused, the forms of a path in
// URI notation are tried, and when none of them is read either, the error of the path as written stands.
let forms = if search { Vec::new() } else { uri_forms(value, cfg!(windows)) };
let mut literal_error = None;
let mut candidates: Vec<(Option<PathBuf>, bool)> = vec![(Some(dir.join(path)), !forms.is_empty())];
candidates.extend(forms.into_iter().map(|f| (Some(dir.join(f)), true)));
if search && !path.is_absolute() {
candidates.extend(self.include_path.iter().map(|p| ((!has_parent(p)).then(|| dir.join(p).join(path)), false)));
candidates.extend(self.jsinclude.iter().filter(|p| p.is_absolute()).map(|p| (Some(p.join(path)), true)));
}
for (candidate, passed_over_when_refused) in candidates {
for (i, (candidate, passed_over_when_refused)) in candidates.into_iter().enumerate() {
let Some(candidate) = candidate else { return Err(refused()) };
self.probes += 1;
if self.probes > MAX_PROBES {
@@ -590,12 +621,22 @@ impl Files {
match self.scope.read(&candidate, crate::script::MAX_SOURCE_BYTES) {
Ok(found) => return Ok(found),
Err(ScriptRead::Missing) => {}
Err(ScriptRead::Refused) if passed_over_when_refused => {}
Err(ScriptRead::Refused) if passed_over_when_refused => {
if i == 0 {
literal_error = Some(refused());
}
}
Err(ScriptRead::Refused) => return Err(refused()),
Err(ScriptRead::TooLarge) => return Err((FailKind::Limit, format!("script file is too large: {value}"))),
Err(ScriptRead::Failed(e)) if i == 0 && passed_over_when_refused => {
literal_error = Some((FailKind::Error, format!("cannot read script file {value}: {e}")));
}
Err(ScriptRead::Failed(e)) => return Err((FailKind::Error, format!("cannot read script file {value}: {e}"))),
}
}
if let Some(error) = literal_error {
return Err(error);
}
Err((FailKind::Error, format!("include file not found: {value}")))
}