Merge pull request #9073 from dotCooCoo/feat/scripts-api-d1b-prefs-matrix-clipboard

Scripts: app gives scripts preferences, matrices and the clipboard
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-02 17:08:38 -08:00
commit cd502f08e8
7 files changed
+287 -21

No files matched your search

+171 -15
View File
@@ -1,9 +1,10 @@
//! The matrix functions of `app`, as the help pages
//! <https://ai-scripting.docsforadobe.dev/jsobjref/Matrix/> and
//! <https://ai-scripting.docsforadobe.dev/jsobjref/Application/> describe them. A matrix is a plain
//! record with `mValueA`, `mValueB`, `mValueC`, `mValueD`, `mValueTX` and `mValueTY` and the
//! `typename` `"Matrix"`; each function other than `isEqualMatrix` and `isSingularMatrix` gives a
//! new record, those two give a boolean, and none changes the records passed to it.
//! record with the finite numbers `mValueA`, `mValueB`, `mValueC`, `mValueD`, `mValueTX` and
//! `mValueTY`; a record passed in needs only those six, and each record a function gives also has
//! the `typename` `"Matrix"`. Each function other than `isEqualMatrix` and `isSingularMatrix` gives
//! a new record, those two give a boolean, and none changes the records passed to it.
//!
//! The record maps a point `(x, y)` to `(a·x + c·y + tx, b·x + d·y + ty)`, as a PDF `cm` matrix
//! does (ISO 32000-1, 8.3.4), in script coordinates with Y up.
@@ -19,7 +20,7 @@
//! - An argument that is not a finite number, or a matrix with a value that is not one, throws a
//! `TypeError`; a result with a value too large for a number throws a `RangeError`.
use vectorcraft_js::{Data, HostError};
use vectorcraft_js::{Data, HostError, MemberInfo};
use super::{Answer, Described, Host, Link, arg, method, named, num, returns, text};
@@ -54,6 +55,24 @@ pub(super) fn app_members(link: &Link) -> Vec<Described> {
("isEqualMatrix", method(l, is_equal), returns("boolean", &[("matrix", "Matrix"), ("secondMatrix", "Matrix")])),
("isSingularMatrix", method(l, is_singular), returns("boolean", &[("matrix", "Matrix")])),
])
.into_iter()
.map(|(name, member, info)| (name, member, with_defaults(info)))
.collect()
}
/// `info` with the defaults of its left-out number arguments, which `reflect` reports: 0 for a
/// translation and an angle, 100 for a horizontal scale. A left-out vertical scale is the
/// horizontal one, so it has no fixed default and its description says so.
fn with_defaults(mut info: MemberInfo) -> MemberInfo {
for a in &mut info.arguments {
match a.name.as_str() {
"deltaX" | "deltaY" | "angle" => a.default = Some(num(0.0)),
"scaleX" => a.default = Some(num(100.0)),
"scaleY" => a.description = Some("The vertical scale; when it is left out, the horizontal scale.".into()),
_ => {}
}
}
info
}
/// The record for `m`, or a `RangeError` when a value is not finite.
@@ -125,26 +144,29 @@ fn products_sum(x: f64, y: f64, z: f64, w: f64, t: f64) -> f64 {
// Each product is split into its rounded value and the part that rounding drops, both exact.
// The five parts are added one at a time into a list of values that do not overlap, smallest
// first, whose sum is exact (Shewchuk's expansion).
let product = |a: f64, b: f64| {
let (a, b) = (Wide::of(a), Wide::of(b));
let (hi, e) = (a.m * b.m, a.e.saturating_add(b.e));
[Wide::new(hi, e), Wide::new(a.m.mul_add(b.m, 0.0 - hi), e)]
};
let ([p, p_lost], [q, q_lost]) = (product(x, y), product(z, w));
let exact = [p, p_lost, q, q_lost, Wide::of(t)].into_iter().fold(Parts::EMPTY, grow);
nearest(exact)
}
/// `a·b` as its rounded value and the part that rounding drops, both exact.
fn product(a: f64, b: f64) -> [Wide; 2] {
let (a, b) = (Wide::of(a), Wide::of(b));
let (hi, e) = (a.m * b.m, a.e.saturating_add(b.e));
[Wide::new(hi, e), Wide::new(a.m.mul_add(b.m, 0.0 - hi), e)]
}
/// Values that do not overlap, smallest first, held without allocating. Each value added keeps at
/// most one more, so the five parts of a sum and the two of a comparison fit.
/// most one more, so the five parts of a sum, the two of a comparison and the sixteen of an
/// inverse's rounding test fit.
#[derive(Clone, Copy)]
struct Parts {
v: [Wide; 8],
v: [Wide; 16],
n: usize,
}
impl Parts {
const EMPTY: Self = Self { v: [Wide { m: 0.0, e: 0 }; 8], n: 0 };
const EMPTY: Self = Self { v: [Wide { m: 0.0, e: 0 }; 16], n: 0 };
fn as_slice(&self) -> &[Wide] {
&self.v[..self.n]
@@ -416,9 +438,143 @@ fn invert(_: &Host, a: &[Data]) -> Answer {
// Each value is divided by the determinant before it is brought back into the range of a
// number, and the translations are taken whole, so a finite inverse is not lost to an
// intermediate value. Adding 0 turns -0 into 0.
let over = |w: Wide| w.div(det).value() + 0.0;
let plain = |v: f64| over(Wide::of(v));
record([plain(d), plain(0.0 - b), plain(0.0 - c), plain(a), over(products_difference(c, f, d, e)), over(products_difference(b, e, a, f))])
let exact_det = || difference_parts(a, d, b, c);
let zero = Wide::of(0.0);
let plain = |v: f64| quotient(Wide::of(v), || [Wide::of(v), zero, zero, zero], det, exact_det);
record([
plain(d),
plain(0.0 - b),
plain(0.0 - c),
plain(a),
quotient(products_difference(c, f, d, e), || difference_parts(c, f, d, e), det, exact_det),
quotient(products_difference(b, e, a, f), || difference_parts(b, e, a, f), det, exact_det),
])
}
/// `a·d − b·c` as four exact parts.
fn difference_parts(a: f64, d: f64, b: f64, c: f64) -> [Wide; 4] {
let neg = |w: Wide| Wide { m: 0.0 - w.m, e: w.e };
let ([p, p_lost], [q, q_lost]) = (product(a, d), product(b, c));
[p, p_lost, neg(q), neg(q_lost)]
}
/// The quotient of `num` by `det`, from their rounded values `num` and `det` and their exact parts.
/// The result is the rounded quotient, which is within a few steps of the exact one, except where
/// that would decide overflow wrongly. When the rounded quotient overflows or lies within eight
/// steps of the largest number, the exact quotient is compared with 2^1024 − 2^970, the halfway
/// point between the largest number and 2^1024: at it or past it the result is infinite. Below it
/// a finite rounded quotient is kept, and an infinite one is replaced by the number the exact
/// quotient rounds to, found from the halfway points 2^1024 − j·2^970 (odd `j`) below, ties to the
/// even one. When the rounded quotient is below 2^-1023, where every number is a multiple of the
/// smallest number, the result is the multiple whose halfway points to its neighbors hold the exact
/// quotient, ties to the even one, so a quotient just past half the smallest number is not rounded
/// to 0. The exact parts are worked out only then. Adding 0 turns -0 into 0.
fn quotient(num: Wide, exact_num: impl Fn() -> [Wide; 4], det: Wide, exact_det: impl Fn() -> [Wide; 4]) -> f64 {
if num.m == 0.0 {
return 0.0;
}
let v = num.div(det).value() + 0.0;
// 2^1024 − 9·2^971, eight steps below the largest number.
let near_top = f64::MAX - 8.0 * pow2(971);
let tiny = f64::MIN_POSITIVE / 2.0;
if v.is_finite() && v.abs() < near_top && v.abs() >= tiny {
return v;
}
let (exact_num, exact_det) = (exact_num(), exact_det());
let sign = |parts: &[Wide]| match parts.iter().copied().fold(Parts::EMPTY, grow).as_slice().last() {
Some(w) if w.m < 0.0 => -1.0,
_ => 1.0,
};
let (sn, sd) = (sign(&exact_num), sign(&exact_det));
if v.abs() < tiny {
return small_quotient(v, &exact_num, &exact_det, sn, sd);
}
// The sign of |num| − (2^1024 − j·2^970)·|det|. Scaling by a power of 2 is exact, and each
// `j·m` is split into its rounded value and the part that rounding drops.
let past = |j: f64| {
let scaled = |w: Wide, s: f64, e: i32| Wide { m: w.m * s, e: w.e.saturating_add(e) };
let times_j = |w: Wide| {
let hi = j * w.m * sd;
let lo = (j * sd).mul_add(w.m, 0.0 - hi);
[Wide::new(hi, w.e.saturating_add(970)), Wide::new(lo, w.e.saturating_add(970))]
};
let sum = exact_num
.iter()
.map(|&w| scaled(w, sn, 0))
.chain(exact_det.iter().map(|&w| scaled(w, 0.0 - sd, 1024)))
.chain(exact_det.iter().flat_map(|&w| times_j(w)))
.fold(Parts::EMPTY, grow);
match sum.as_slice().last() {
Some(w) if w.m > 0.0 => 1,
Some(w) if w.m < 0.0 => -1,
_ => 0,
}
};
if past(1.0) >= 0 {
return f64::INFINITY;
}
if v.is_finite() {
return v;
}
let even = |x: f64| x.to_bits() & 1 == 0;
let mut top = f64::MAX;
for k in 1..16u8 {
let below = top.next_down();
match past(f64::from(2 * k + 1)) {
1 => return top * sn * sd,
0 => return if even(top) { top } else { below } * sn * sd,
_ => top = below,
}
}
top * sn * sd
}
/// The exact quotient of `exact_num` by `exact_det`, whose signs are `sn` and `sd`, rounded to a
/// multiple of the smallest number, ties to the even multiple, starting from the rounded quotient
/// `v`, which is below 2^-1023 and within a few steps of it.
fn small_quotient(v: f64, exact_num: &[Wide; 4], exact_det: &[Wide; 4], sn: f64, sd: f64) -> f64 {
let step = f64::from_bits(1);
// The sign of |num| − j·2^-1075·|det|, with each `j·m` split into its rounded value and the
// part that rounding drops. `j` is odd and below 2^53, so it is exact.
let past = |j: f64| {
let times_j = |w: Wide| {
let hi = j * w.m * sd;
let lo = (j * sd).mul_add(w.m, 0.0 - hi);
[Wide::new(0.0 - hi, w.e.saturating_sub(1075)), Wide::new(0.0 - lo, w.e.saturating_sub(1075))]
};
let sum = exact_num.iter().map(|&w| Wide { m: w.m * sn, e: w.e }).chain(exact_det.iter().flat_map(|&w| times_j(w))).fold(Parts::EMPTY, grow);
match sum.as_slice().last() {
Some(w) if w.m > 0.0 => 1,
Some(w) if w.m < 0.0 => -1,
_ => 0,
}
};
// `k` steps; dividing by the smallest number is exact.
let mut k = v.abs() / step;
let even = |k: f64| k % 2.0 == 0.0;
for _ in 0..8 {
match past(2.0 * k + 1.0) {
1 => k += 1.0,
0 => {
k = if even(k) { k } else { k + 1.0 };
break;
}
_ => {
if k == 0.0 {
break;
}
match past(2.0 * k - 1.0) {
-1 => k -= 1.0,
0 => {
k = if even(k) { k } else { k - 1.0 };
break;
}
_ => break,
}
}
}
}
k * step * sn * sd + 0.0
}
fn is_equal(_: &Host, a: &[Data]) -> Answer {