mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 05:22:58 -05:00
Merge main into feat/scripts-api-d8c-rasterize-capture
This commit is contained in:
commit
7b433a52fe
21 files changed
+978
-48
No files matched your search
@@ -3194,7 +3194,9 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
|
||||
for (name, members) in
|
||||
ENUMS.iter().chain(save_open::ENUMS).chain(print::ENUMS).chain(raster_capture::ENUMS).chain(images::ENUMS).chain(tracing::ENUMS)
|
||||
{
|
||||
let items = members.iter().map(|m| (m.to_string(), HostItem::Value(text(format!("{name}.{m}"))))).collect();
|
||||
let mut items: Vec<_> = members.iter().map(|m| (m.to_string(), HostItem::Value(text(format!("{name}.{m}"))))).collect();
|
||||
let aliases = save_open::ALIASES.iter().filter(|(e, _, _)| e == name);
|
||||
items.extend(aliases.map(|(_, alias, m)| (alias.to_string(), HostItem::Value(text(format!("{name}.{m}"))))));
|
||||
realm.define_host_object(name, items)?;
|
||||
}
|
||||
let blend_modes = BLEND_MODES.iter().map(|(m, _)| (m.to_string(), HostItem::Value(text(format!("BlendModes.{m}"))))).collect();
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
//! <https://ai-scripting.docsforadobe.dev/jsobjref/PDFSaveOptions/>,
|
||||
//! <https://ai-scripting.docsforadobe.dev/jsobjref/ExportOptionsPNG24/>,
|
||||
//! <https://ai-scripting.docsforadobe.dev/jsobjref/ExportOptionsJPEG/>,
|
||||
//! <https://ai-scripting.docsforadobe.dev/jsobjref/ExportOptionsSVG/> and
|
||||
//! <https://ai-scripting.docsforadobe.dev/jsobjref/ExportOptionsSVG/>,
|
||||
//! <https://ai-scripting.docsforadobe.dev/jsobjref/ExportOptionsAutoCAD/> and
|
||||
//! <https://ai-scripting.docsforadobe.dev/jsobjref/PDFFileOptions/> describe.
|
||||
//!
|
||||
//! Each method runs a command (`file.saveAs`, `document.export`, `document.open`) inside the run's
|
||||
@@ -27,6 +28,12 @@
|
||||
//! one file: the active artboard with `artBoardClipping`, otherwise the bounds of the visible art.
|
||||
//! An SVG with `embedRasterImages` false also writes the document's embedded images beside it,
|
||||
//! and when one of those files may not be written, none of them is.
|
||||
//! - `ExportType.AUTOCAD` is the same value as `ExportType.AutoCAD`. An AutoCAD export writes DXF
|
||||
//! (`.dxf`) only: `AutoCADExportFileFormat.DWG`, the default, throws an error that says DWG is not
|
||||
//! available. An option an `ExportOptionsAutoCAD` object leaves out takes the default its help page
|
||||
//! gives; `colors`, `rasterFormat`, `unit` and `unitScaleRatio`, which have none there, default to
|
||||
//! true colors, PNG, millimeters and 1. `unitScaleRatio` is the number of drawing units for one
|
||||
//! `unit`, more than 0 and at most 1000.
|
||||
//! - An option the classes here map is applied; a listed option that is not mapped is accepted at
|
||||
//! the default its help page gives and throws an error that says it is not available yet at any
|
||||
//! other value; a property the help pages do not list throws an error that names it, unless it is
|
||||
@@ -41,8 +48,8 @@
|
||||
//! save of one artboard writes only the file at the path, holding that artboard. Without
|
||||
//! `saveMultipleArtboards` the range is not used, but a value that is not written as a range of
|
||||
//! artboard numbers throws.
|
||||
//! - `Document.path` is a `Folder` for the folder of the document's file. A document that has no
|
||||
//! file has a `fullName` of a `File` for its name in `Folder.current` and a `path` of `""`.
|
||||
//! - `Document.path` is a `Folder` for the folder of the document's file. A file saved or opened
|
||||
//! under a relative path is in the process's working folder. A document that has no file has a `fullName` of a `File` for its name in `Folder.current` and a `path` of `""`.
|
||||
//! `fullName` and `path` of a document whose file the automation read roots do not allow throw
|
||||
//! `Script file access denied`. Changing `Document.saved` runs `document.setSaved`.
|
||||
//! Assigning its current value runs no command.
|
||||
@@ -65,6 +72,7 @@ use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use serde_json::{Map, Value, json};
|
||||
use vectorcraft_doc::Unit;
|
||||
use vectorcraft_js::{Data, HostClass, HostError, HostFunction};
|
||||
|
||||
use super::{
|
||||
@@ -87,6 +95,15 @@ pub(in crate::script) struct State {
|
||||
/// The enumerations these members use, as `dom`'s are made.
|
||||
pub(super) const ENUMS: &[(&str, &[&str])] = &[
|
||||
("ExportType", &["AutoCAD", "FLASH", "GIF", "JPEG", "PNG24", "PNG8", "SVG", "TIFF"]),
|
||||
("AutoCADColors", &["Max8Colors", "Max16Colors", "Max256Colors", "TrueColors"]),
|
||||
(
|
||||
"AutoCADCompatibility",
|
||||
&["AutoCADRelease13", "AutoCADRelease18", "AutoCADRelease14", "AutoCADRelease21", "AutoCADRelease15", "AutoCADRelease24"],
|
||||
),
|
||||
("AutoCADExportFileFormat", &["DXF", "DWG"]),
|
||||
("AutoCADExportOption", &["PreserveAppearance", "MaximizeEditability"]),
|
||||
("AutoCADRasterFormat", &["PNG", "JPEG"]),
|
||||
("AutoCADUnit", &["Points", "Picas", "Inches", "Millimeters", "Centimeters", "Pixels"]),
|
||||
(
|
||||
"Compatibility",
|
||||
&[
|
||||
@@ -183,6 +200,10 @@ pub(super) const ENUMS: &[(&str, &[&str])] = &[
|
||||
("SVGFontType", &["CEFFONT", "OUTLINEFONT", "SVGFONT"]),
|
||||
];
|
||||
|
||||
/// Other names of enumeration members: `(enumeration, name, member)` makes `enumeration.name` the
|
||||
/// same value as `enumeration.member`.
|
||||
pub(super) const ALIASES: &[(&str, &str, &str)] = &[("ExportType", "AUTOCAD", "AutoCAD")];
|
||||
|
||||
const DISPLAY: &str = "UserInteractionLevel.DISPLAYALERTS";
|
||||
const QUIET: &str = "UserInteractionLevel.DONTDISPLAYALERTS";
|
||||
|
||||
@@ -197,7 +218,8 @@ enum Default {
|
||||
Member(&'static str, &'static str),
|
||||
/// The help page gives no default: any value throws.
|
||||
Unset,
|
||||
/// White as an `RGBColor`: red, green and blue each 255.
|
||||
/// White as an `RGBColor`: red, green and blue each 255 (its `typename`, when it has one,
|
||||
/// `RGBColor`).
|
||||
White,
|
||||
}
|
||||
|
||||
@@ -210,7 +232,7 @@ impl Default {
|
||||
(Default::Member(e, m), Data::String(b)) => b.split_once('.').is_some_and(|(be, bm)| be == e && bm == m),
|
||||
(Default::White, Data::Object(items)) => {
|
||||
let get = |k: &str| items.iter().find(|(name, _)| name == k).map(|(_, v)| v);
|
||||
matches!(get("typename"), Some(Data::String(t)) if t == "RGBColor")
|
||||
get("typename").is_none_or(|t| matches!(t, Data::String(t) if t == "RGBColor"))
|
||||
&& ["red", "green", "blue"].iter().all(|k| matches!(get(k), Some(Data::Number(n)) if *n == 255.0))
|
||||
}
|
||||
_ => false,
|
||||
@@ -657,9 +679,118 @@ fn svg_starts() -> Vec<(&'static str, Data)> {
|
||||
]
|
||||
}
|
||||
|
||||
/// The members of the enumeration `name` in [`ENUMS`].
|
||||
fn members_of(name: &str) -> &'static [&'static str] {
|
||||
ENUMS.iter().find(|(n, _)| *n == name).map_or(&[][..], |(_, m)| *m)
|
||||
}
|
||||
|
||||
fn autocad_map(name: &str, d: &Data, p: &mut Map<String, Value>) -> std::result::Result<bool, HostError> {
|
||||
let what = format!("ExportOptionsAutoCAD.{name}");
|
||||
let pick = |enumeration: &str| member(d, enumeration, members_of(enumeration), &what);
|
||||
match name {
|
||||
"alterPathsForAppearance" => {
|
||||
p.insert("alterPaths".into(), json!(boolean(d, &what)?));
|
||||
}
|
||||
"colors" => {
|
||||
let colors = match pick("AutoCADColors")? {
|
||||
"Max8Colors" => json!(8),
|
||||
"Max16Colors" => json!(16),
|
||||
"Max256Colors" => json!(256),
|
||||
_ => json!("true"),
|
||||
};
|
||||
p.insert("colors".into(), colors);
|
||||
}
|
||||
"convertTextToOutlines" => {
|
||||
p.insert("outlineText".into(), json!(boolean(d, &what)?));
|
||||
}
|
||||
"exportFileFormat" => {
|
||||
if pick("AutoCADExportFileFormat")? == "DWG" {
|
||||
return Err(HostError::Error(
|
||||
"AutoCADExportFileFormat.DWG is not available: set exportFileFormat to AutoCADExportFileFormat.DXF".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
"exportOption" => {
|
||||
let preserve = if pick("AutoCADExportOption")? == "PreserveAppearance" { "appearance" } else { "editability" };
|
||||
p.insert("preserve".into(), json!(preserve));
|
||||
}
|
||||
"exportSelectedArtOnly" => {
|
||||
p.insert("selectedOnly".into(), json!(boolean(d, &what)?));
|
||||
}
|
||||
"rasterFormat" => {
|
||||
p.insert("rasterFormat".into(), json!(if pick("AutoCADRasterFormat")? == "JPEG" { "jpeg" } else { "png" }));
|
||||
}
|
||||
"scaleLineweights" => {
|
||||
p.insert("scaleLineweights".into(), json!(boolean(d, &what)?));
|
||||
}
|
||||
"unit" => {
|
||||
let unit = match pick("AutoCADUnit")? {
|
||||
"Points" => Unit::Points,
|
||||
"Picas" => Unit::Picas,
|
||||
"Inches" => Unit::Inches,
|
||||
"Millimeters" => Unit::Millimeters,
|
||||
"Centimeters" => Unit::Centimeters,
|
||||
_ => Unit::Pixels,
|
||||
};
|
||||
p.insert("unit".into(), json!(unit.key()));
|
||||
}
|
||||
"unitScaleRatio" => {
|
||||
let n = match d {
|
||||
Data::Number(n) if n.is_finite() => *n,
|
||||
_ => return Err(HostError::Type(format!("{what} must be a finite number"))),
|
||||
};
|
||||
if !(n > 0.0 && n <= 1000.0) {
|
||||
return Err(HostError::Range(format!("{what} must be more than 0 and at most 1000")));
|
||||
}
|
||||
p.insert("scale".into(), json!(n));
|
||||
}
|
||||
"version" => {
|
||||
let version = match pick("AutoCADCompatibility")? {
|
||||
"AutoCADRelease13" => "R13",
|
||||
"AutoCADRelease14" => "R14",
|
||||
"AutoCADRelease15" => "2000",
|
||||
"AutoCADRelease18" => "2004",
|
||||
"AutoCADRelease21" => "2007",
|
||||
_ => "2010",
|
||||
};
|
||||
p.insert("version".into(), json!(version));
|
||||
}
|
||||
_ => return Ok(false),
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn autocad_starts() -> Vec<(&'static str, Data)> {
|
||||
vec![
|
||||
("alterPathsForAppearance", Data::Bool(false)),
|
||||
("convertTextToOutlines", Data::Bool(false)),
|
||||
("exportFileFormat", text("AutoCADExportFileFormat.DWG")),
|
||||
("exportOption", text("AutoCADExportOption.MaximizeEditability")),
|
||||
("exportSelectedArtOnly", Data::Bool(false)),
|
||||
("scaleLineweights", Data::Bool(false)),
|
||||
("version", text("AutoCADCompatibility.AutoCADRelease24")),
|
||||
]
|
||||
}
|
||||
|
||||
/// The options object `d` with the options of a new `class` object it leaves out, or holds as
|
||||
/// `undefined`, added.
|
||||
fn with_starts(class: &OptionClass, d: &Data) -> Data {
|
||||
let Data::Object(items) = d else { return d.clone() };
|
||||
let mut items = items.clone();
|
||||
for (k, v) in (class.starts)() {
|
||||
match items.iter_mut().find(|(n, _)| n == k) {
|
||||
Some((_, held)) if matches!(held, Data::Undefined) => *held = v,
|
||||
Some(_) => {}
|
||||
None => items.push((k.to_string(), v)),
|
||||
}
|
||||
}
|
||||
Data::Object(items)
|
||||
}
|
||||
|
||||
const PNG24: OptionClass = OptionClass { name: "ExportOptionsPNG24", map: png24_map, kept: PNG24_KEPT, starts: png24_starts };
|
||||
const JPEG: OptionClass = OptionClass { name: "ExportOptionsJPEG", map: jpeg_map, kept: JPEG_KEPT, starts: jpeg_starts };
|
||||
const SVG: OptionClass = OptionClass { name: "ExportOptionsSVG", map: svg_map, kept: SVG_KEPT, starts: svg_starts };
|
||||
const AUTOCAD: OptionClass = OptionClass { name: "ExportOptionsAutoCAD", map: autocad_map, kept: &[], starts: autocad_starts };
|
||||
|
||||
/// An `ExportType`: the format `document.export` writes, the extensions a file name may already
|
||||
/// end with (the first is added), and its option class (`None`: no options are taken).
|
||||
@@ -670,9 +801,9 @@ struct ExportKind {
|
||||
}
|
||||
|
||||
fn export_kind(d: &Data) -> std::result::Result<ExportKind, HostError> {
|
||||
let all = ENUMS.iter().find(|(n, _)| *n == "ExportType").map_or(&[][..], |(_, m)| *m);
|
||||
let raster = |format, extensions| ExportKind { format, extensions, options: None };
|
||||
Ok(match member(d, "ExportType", all, "exportType")? {
|
||||
Ok(match member(d, "ExportType", members_of("ExportType"), "exportType")? {
|
||||
"AutoCAD" => ExportKind { format: "dxf", extensions: &["dxf"], options: Some(&AUTOCAD) },
|
||||
"PNG24" => ExportKind { format: "png", extensions: &["png"], options: Some(&PNG24) },
|
||||
"JPEG" => ExportKind { format: "jpg", extensions: &["jpg", "jpeg"], options: Some(&JPEG) },
|
||||
"SVG" => ExportKind { format: "svg", extensions: &["svg"], options: Some(&SVG) },
|
||||
@@ -867,11 +998,26 @@ fn doc_export_file(h: &Host, a: &[Data]) -> Answer {
|
||||
None => Map::new(),
|
||||
},
|
||||
Some(o) => match kind.options {
|
||||
// An AutoCAD option left out takes its help page's default, not the DXF writer's.
|
||||
Some(class) if class.name == AUTOCAD.name => options_params(class, &with_starts(class, o))?,
|
||||
Some(class) => options_params(class, o)?,
|
||||
None if matches!(o, Data::Null) => return Err(HostError::Type("the options must be an object".into())),
|
||||
None => return Err(HostError::Error(format!("options for {} are not available yet", kind.format))),
|
||||
},
|
||||
};
|
||||
// A DXF version before 2004 holds no true color: with `colors` left out it takes 256 indexed
|
||||
// colors, and `AutoCADColors.TrueColors` with it throws.
|
||||
if kind.format == "dxf" && matches!(p.get("version").and_then(Value::as_str), Some("R13" | "R14" | "2000")) {
|
||||
match p.get("colors") {
|
||||
None => {
|
||||
p.insert("colors".into(), json!(256));
|
||||
}
|
||||
Some(c) if c.as_str() == Some("true") => {
|
||||
return Err(HostError::Error("AutoCADColors.TrueColors needs AutoCADRelease18 or a later version".into()));
|
||||
}
|
||||
Some(_) => {}
|
||||
}
|
||||
}
|
||||
let svgz = p.remove("_svgz").and_then(|v| v.as_bool()).unwrap_or(false);
|
||||
let clip = p.remove("_clip").and_then(|v| v.as_bool()).unwrap_or(false);
|
||||
let (hs, vs) = (p.remove("_h").and_then(|v| v.as_f64()).unwrap_or(100.0), p.remove("_v").and_then(|v| v.as_f64()).unwrap_or(100.0));
|
||||
@@ -879,7 +1025,8 @@ fn doc_export_file(h: &Host, a: &[Data]) -> Answer {
|
||||
return Err(HostError::Error("different horizontalScale and verticalScale are not available yet".into()));
|
||||
}
|
||||
// A raster file has one pixel per point at 100%, whatever the document's raster effects resolution.
|
||||
if kind.format != "svg" {
|
||||
// A DXF's `scale` is its drawing units per unit, from `unitScaleRatio`.
|
||||
if !matches!(kind.format, "svg" | "dxf") {
|
||||
p.insert("scale".into(), json!(hs / 100.0));
|
||||
}
|
||||
let format = if svgz { "svgz" } else { kind.format };
|
||||
@@ -1159,7 +1306,7 @@ pub(super) fn document_members(l: &Link) -> Vec<Described> {
|
||||
/// The option classes: each `new Name()` gives a record with its `typename` and the defaults its
|
||||
/// help page gives for the options mapped here.
|
||||
pub(super) fn classes(link: &Link) -> Vec<HostClass> {
|
||||
let all = SAVE_CLASSES.iter().map(|(c, _)| c).chain([&PNG24, &JPEG, &SVG]);
|
||||
let all = SAVE_CLASSES.iter().map(|(c, _)| c).chain([&PNG24, &JPEG, &SVG, &AUTOCAD]);
|
||||
all.map(|c| {
|
||||
let l = link.clone();
|
||||
let f: HostFunction = Rc::new(move |_: Vec<Data>| {
|
||||
|
||||
Reference in new issue
Block a user