mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
Merge remote-tracking branch 'origin/main' into fine-nudge-5258
This commit is contained in:
commit
7ccd849e14
710 files changed
+78759
-4826
No files matched your search
Generated
+222
-18
@@ -975,12 +975,12 @@ source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7
|
||||
[[package]]
|
||||
name = "craft-fonts-build"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=bb8320f5fa5d20fa78a026d0de06fe4e2f08a099#bb8320f5fa5d20fa78a026d0de06fe4e2f08a099"
|
||||
|
||||
[[package]]
|
||||
name = "craft-i18n"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=bb8320f5fa5d20fa78a026d0de06fe4e2f08a099#bb8320f5fa5d20fa78a026d0de06fe4e2f08a099"
|
||||
dependencies = [
|
||||
"fixed_decimal",
|
||||
"icu_decimal",
|
||||
@@ -1036,9 +1036,20 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
name = "craft-proof"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
dependencies = [
|
||||
"log",
|
||||
"miniz_oxide 0.9.1",
|
||||
"toml",
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=bb8320f5fa5d20fa78a026d0de06fe4e2f08a099#bb8320f5fa5d20fa78a026d0de06fe4e2f08a099"
|
||||
dependencies = [
|
||||
"craft-fonts-build",
|
||||
"craft-i18n",
|
||||
@@ -1059,7 +1070,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -1085,7 +1096,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-vectorcraft-format"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb#9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"serde",
|
||||
@@ -2844,6 +2855,182 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-bitstream"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b480e752277e29eb4054f69546887a9b84656fe78c08f54ba5850ced98a378fe"
|
||||
dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-coding"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd972bcd125e776f1eb241ac50e39f956095a1c2770c64736c968f8946bd9a3c"
|
||||
dependencies = [
|
||||
"jxl-bitstream",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-color"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f316b1358c1711755b3ee8e8cb5c4a1dad12e796233088a7a513440782de80b2"
|
||||
dependencies = [
|
||||
"jxl-bitstream",
|
||||
"jxl-coding",
|
||||
"jxl-grid",
|
||||
"jxl-image",
|
||||
"jxl-oxide-common",
|
||||
"jxl-threadpool",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-frame"
|
||||
version = "0.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d967c6fd669c7c01060b5022d8835fa82fd46b06ffc98b549f17600a097c2b3"
|
||||
dependencies = [
|
||||
"jxl-bitstream",
|
||||
"jxl-coding",
|
||||
"jxl-grid",
|
||||
"jxl-image",
|
||||
"jxl-modular",
|
||||
"jxl-oxide-common",
|
||||
"jxl-threadpool",
|
||||
"jxl-vardct",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-grid"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01671307879a033bfa52e6e8784b941aca770b3f3a7d33830b455b6844f793fb"
|
||||
dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-image"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c5f752d62577c702a94dbbce4045caf08cb58639e8a4d56464b40ecf33ffe565"
|
||||
dependencies = [
|
||||
"jxl-bitstream",
|
||||
"jxl-grid",
|
||||
"jxl-oxide-common",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-jbr"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e35d032bcec660647828527ff42c6f5776d2fd44b8357f9f6d9ac6dc07218e46"
|
||||
dependencies = [
|
||||
"brotli-decompressor",
|
||||
"jxl-bitstream",
|
||||
"jxl-frame",
|
||||
"jxl-grid",
|
||||
"jxl-image",
|
||||
"jxl-modular",
|
||||
"jxl-oxide-common",
|
||||
"jxl-threadpool",
|
||||
"jxl-vardct",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-modular"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a2f045b24c738dd91d482be385512b512721ae08a671bd4b27bf1c47f215235"
|
||||
dependencies = [
|
||||
"jxl-bitstream",
|
||||
"jxl-coding",
|
||||
"jxl-grid",
|
||||
"jxl-oxide-common",
|
||||
"jxl-threadpool",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-oxide"
|
||||
version = "0.12.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d36c662923f47586880211f3bc7c0d83fb3a9b410d278c7bde93450748abeef3"
|
||||
dependencies = [
|
||||
"brotli-decompressor",
|
||||
"jxl-bitstream",
|
||||
"jxl-color",
|
||||
"jxl-frame",
|
||||
"jxl-grid",
|
||||
"jxl-image",
|
||||
"jxl-jbr",
|
||||
"jxl-oxide-common",
|
||||
"jxl-render",
|
||||
"jxl-threadpool",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-oxide-common"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b62394c5021b3a9e7e0dbb2d639d555d019090c9946c39f6d3b09d390db4157b"
|
||||
dependencies = [
|
||||
"jxl-bitstream",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-render"
|
||||
version = "0.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d34386bfdb6a19b5a30cc9beb4d475d537422c31ae8c39bb69640fcce3fcaf19"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"jxl-bitstream",
|
||||
"jxl-coding",
|
||||
"jxl-color",
|
||||
"jxl-frame",
|
||||
"jxl-grid",
|
||||
"jxl-image",
|
||||
"jxl-modular",
|
||||
"jxl-oxide-common",
|
||||
"jxl-threadpool",
|
||||
"jxl-vardct",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-threadpool"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25f15eb830aa77a7f21148d72e153562a26bfe570139bd4922eab1908dd499d3"
|
||||
dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jxl-vardct"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce72a18c6d3a47172ab6c479be2bdb56f22066b5d7092663f03b4490820b4511"
|
||||
dependencies = [
|
||||
"jxl-bitstream",
|
||||
"jxl-coding",
|
||||
"jxl-grid",
|
||||
"jxl-modular",
|
||||
"jxl-oxide-common",
|
||||
"jxl-threadpool",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "keyboard-types"
|
||||
version = "0.8.3"
|
||||
@@ -6180,6 +6367,13 @@ dependencies = [
|
||||
"ruzstd",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-apischema"
|
||||
version = "0.21.0"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-brush"
|
||||
version = "0.21.0"
|
||||
@@ -6287,11 +6481,15 @@ dependencies = [
|
||||
"cfb",
|
||||
"chrono",
|
||||
"craft-docpath",
|
||||
"craft-proof",
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
"hayro-jpeg2000",
|
||||
"heic-rs",
|
||||
"image",
|
||||
"jxl-color",
|
||||
"jxl-grid",
|
||||
"jxl-oxide",
|
||||
"kurbo 0.13.1",
|
||||
"log",
|
||||
"proptest",
|
||||
@@ -6301,8 +6499,10 @@ dependencies = [
|
||||
"ruzstd",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"skrifa 0.47.0",
|
||||
"thiserror 2.0.21",
|
||||
"vectorcraft-affinity",
|
||||
"vectorcraft-apischema",
|
||||
"vectorcraft-brush",
|
||||
"vectorcraft-cad",
|
||||
"vectorcraft-color",
|
||||
@@ -6323,6 +6523,8 @@ dependencies = [
|
||||
"vectorcraft-tools",
|
||||
"vectorcraft-trace",
|
||||
"wat",
|
||||
"zune-core 0.5.3",
|
||||
"zune-jpegxl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -6330,6 +6532,7 @@ name = "vectorcraft-eps"
|
||||
version = "0.21.0"
|
||||
dependencies = [
|
||||
"craft-pdf",
|
||||
"encoding_rs",
|
||||
"fax",
|
||||
"flate2",
|
||||
"hayro-ccitt",
|
||||
@@ -6449,6 +6652,8 @@ dependencies = [
|
||||
"kurbo 0.13.1",
|
||||
"log",
|
||||
"md-5",
|
||||
"objc2-core-foundation",
|
||||
"objc2-core-graphics",
|
||||
"roxmltree 0.21.2",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -6470,6 +6675,8 @@ dependencies = [
|
||||
name = "vectorcraft-plugins"
|
||||
version = "0.21.0"
|
||||
dependencies = [
|
||||
"craft-vectorcraft-format",
|
||||
"image",
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -6480,6 +6687,7 @@ dependencies = [
|
||||
"vectorcraft-geom",
|
||||
"wasmi",
|
||||
"wat",
|
||||
"web-time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -8040,7 +8248,6 @@ dependencies = [
|
||||
"nix",
|
||||
"typed-path",
|
||||
"windows-sys 0.61.2",
|
||||
"zopfli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -8055,18 +8262,6 @@ version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||
|
||||
[[package]]
|
||||
name = "zopfli"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aaf7fc5d30c28483d93805c4a5e12b05bbb52407fa67c5f8bd552374cd01fb11"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"crc32fast",
|
||||
"log",
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zune-core"
|
||||
version = "0.4.12"
|
||||
@@ -8097,6 +8292,15 @@ dependencies = [
|
||||
"zune-core 0.5.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zune-jpegxl"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cba11ecd07cc23351500e840ae03841b7e4997923ec8e4832ee3ae199ea0b6b4"
|
||||
dependencies = [
|
||||
"zune-core 0.5.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zvariant"
|
||||
version = "5.15.1"
|
||||
|
||||
+18
-3
@@ -14,15 +14,16 @@ repository = "https://github.com/storytold/vectorcraft"
|
||||
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb", default-features = false, features = ["interpret", "write"] }
|
||||
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb", features = ["serde"] }
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "bb8320f5fa5d20fa78a026d0de06fe4e2f08a099", features = ["serde"] }
|
||||
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
craft-proof = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
# Decides who draws the window frame (desktop vs. app) so the suite shares one native titlebar per
|
||||
# desktop. The `egui` feature adds `craft_window::decorate`. (craft-libs#217.)
|
||||
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb", features = ["egui"] }
|
||||
# The `.vectorcraft` envelope: its members, version, embedded images and the data after the JSON.
|
||||
craft-vectorcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "9be9a63782298ab59cd9b7a42ee9cba4f42a5ccb" }
|
||||
craft-vectorcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
|
||||
# craft-update: app-specific pretend versions apply to startup and manual checks.
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["egui"] }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
|
||||
# internal
|
||||
vectorcraft-geom = { path = "crates/geom" }
|
||||
vectorcraft-color = { path = "crates/color" }
|
||||
@@ -54,6 +55,7 @@ vectorcraft-plugins = { path = "crates/plugins" }
|
||||
vectorcraft-affinity = { path = "crates/affinity" }
|
||||
vectorcraft-scriptcheck = { path = "crates/scriptcheck" }
|
||||
vectorcraft-js = { path = "crates/js" }
|
||||
vectorcraft-apischema = { path = "crates/apischema" }
|
||||
|
||||
# foundation
|
||||
kurbo = { version = "0.13", features = ["serde"] }
|
||||
@@ -99,12 +101,25 @@ ruzstd = { version = "0.9.1", default-features = false, features = ["std"] }
|
||||
hayro-ccitt = "0.3"
|
||||
# Substring search for the EPS reader's PostScript `search`.
|
||||
memchr = "2"
|
||||
# The WHATWG Encoding Standard's legacy encodings: names in legacy .ai files read in the code page
|
||||
# the user picks (Mac OS Roman, Korean, Japanese, Chinese).
|
||||
encoding_rs = "0.8.42"
|
||||
jpeg-encoder = { version = "0.6", default-features = false, features = ["std"] }
|
||||
gif = { version = "0.14", default-features = false, features = ["std", "raii_no_panic"] }
|
||||
# CMYK image samples (vectorcraft-doc's cmyk module): the image crate's own TIFF and JPEG
|
||||
# decoders, read directly (the image crate converts CMYK to RGB).
|
||||
tiff = "0.11"
|
||||
zune-jpeg = "0.5"
|
||||
# JPEG XL decoder (pure Rust, MIT OR Apache-2.0): File › Open and Place read `.jxl` images. It is
|
||||
# built without its default `rayon` feature and decodes on one thread.
|
||||
jxl-oxide = { version = "0.12.6", default-features = false }
|
||||
# jxl-oxide's color conversions, run on colors after their alpha is divided out.
|
||||
jxl-color = "0.11.0"
|
||||
# jxl-oxide's buffer allocation error, told apart from other decoding errors.
|
||||
jxl-grid = "0.6.2"
|
||||
# Tests: a lossless JPEG XL encoder (pure Rust, MIT OR Apache-2.0 OR Zlib) for the `.jxl` test files.
|
||||
zune-jpegxl = { version = "0.5.2", default-features = false, features = ["std"] }
|
||||
zune-core = "0.5"
|
||||
|
||||
# ui
|
||||
egui = "0.37"
|
||||
|
||||
@@ -237,7 +237,7 @@ and what's next; [`docs/target-app-parity.md`](docs/target-app-parity.md) the fu
|
||||
(92.5% of menu items do something), feature depth is ~75%, and we estimate ~65% ready for real work for a mainstream illustrator (~62% across all tracked features): everyday
|
||||
vector illustration is close to usable: drawing and path tools,
|
||||
Pathfinder and Shape Builder, paint, gradients, appearance and transparency, type with styles, threading and Hebrew/Arabic bidirectional layout, and
|
||||
files (SVG, PDF and PDF-compatible `.ai` with PDF/X, legacy version 7 `.ai` keeping layers, groups and type, EPS, DXF, EMF/WMF, raster formats, PSD out and PSD/PSB in as their merged image, Print, Package).
|
||||
files (SVG, PDF and PDF-compatible `.ai` with PDF/X, legacy version 7 `.ai` keeping layers, groups and type, EPS, DXF, EMF/WMF, raster formats, PSD out and PSD/PSB in as their merged image, JPEG XL in, Print, Package).
|
||||
Affinity documents (`.af` from Affinity 3, `.afdesign`, `.afpub` and, by their content, `.afphoto` from Affinity 1 and 2) open
|
||||
and place natively: layers, groups, artboards and pages, curves and shapes, fills, gradients and strokes, clipping
|
||||
and masks, text with each paragraph's indents, spacing and leading, tables (as text frames and lines), master
|
||||
|
||||
+15
-19
@@ -1,21 +1,8 @@
|
||||
# VectorCraft Roadmap
|
||||
|
||||
- Contour add-on (beyond Illustrator): drag a selected shape's edge outward or inward for a live, editable offset copy, with a floating bar for distance and Sharp, Round or Square corners; Bake turns it into paths. Scores and estimates unchanged.
|
||||
- Compound Shapes add-on (beyond Illustrator): a menu for Make, Release and Expand Compound Shape and a setting, off by default, that makes the Pathfinder panel's Shape Mode buttons create compound shapes on a click. Scores and estimates unchanged.
|
||||
|
||||
- Paint: new gradients use the document's color model for default stops, including Gradient tool creation. Scores and estimates unchanged.
|
||||
- UI: Window → Help Bar toggles the tool-help strip separately from the status bar and saves visibility with workspaces. Scores and estimates unchanged.
|
||||
|
||||
- PDF.R1: editable PDF import uses the shared reader, scans page content in windows, and rejects known incomplete input before returning a document.
|
||||
- PDF.W1: PDF export and print composition use the shared optional writer for configuration, metadata, font preparation and guarded finalization; drawing and output policy remain app-owned.
|
||||
|
||||
- UI.5: Shared docking adds tab reordering, docking into other groups, dock splits and resizable floating panel groups. Panel positions and return locations persist with workspaces; the native icon rail and Tools panel remain available.
|
||||
|
||||
- UI.4: Dock and floating-panel headers use shared accessible tabs with bounded overflow. Widget-state theme application uses `craft-ui`; all four palettes, font stacks and preferences remain app-owned.
|
||||
|
||||
**Stage: alpha** · next: beta, ~13 points (ready for real work ~62% → ~75%, and reliable `.ai` exchange) and ~170–270 h away
|
||||
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-06 · **Change:** minor (Tools stays beside the canvas with left-side panels; Hindi catalog draft complete under the translation tests; contributor loanword terminology reviewed; wider proofreading welcome; Compound Shapes add-on, beyond Illustrator; scores and estimates unchanged; Contour add-on, beyond Illustrator; scores and estimates unchanged; new gradients follow document color mode; scores and estimates unchanged) · **Target:** Adobe Illustrator 2026 (30.x)
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-10 · **Change:** minor (docs tidy-up, no content change: stray notes at the top moved into the revision table; scores and estimates unchanged; previously minor: Raster effects 85% → 88%, gallery thumbnails and in-dialog preview in; headline numbers unchanged; Symbols area ~87% → ~88%: dynamic instance parts take opacity, blend, gradient and pattern on the canvas; G5 docs sync for Overlap and Pattern auto corners; previously minor: Tools stays beside the canvas with left-side panels; Hindi catalog draft complete under the translation tests; contributor loanword terminology reviewed; wider proofreading welcome; Compound Shapes add-on, beyond Illustrator; scores and estimates unchanged; Contour add-on, beyond Illustrator; scores and estimates unchanged; new gradients follow document color mode; scores and estimates unchanged) · **Target:** Adobe Illustrator 2026 (30.x)
|
||||
|
||||
VectorCraft is a clean-room, open-source, pure-Rust reimplementation of the Adobe Illustrator workflow. It runs
|
||||
on macOS, Windows, Linux, FreeBSD and the web (WASM), and agents can drive all of it over MCP, the CLI and a JSON
|
||||
@@ -35,7 +22,6 @@ blocking it ([Alpha gate](docs/roadmap.md#alpha-gate)).
|
||||
|
||||
| | Value | Kind |
|
||||
|---|---|---|
|
||||
| 2026-11-03 | minor | Docked Tools stays between left-side panels and the canvas; redocking accepts canvas and window edges. Requested layout choice; scores and estimates unchanged. |
|
||||
| **Feature breadth** (Illustrator's menu items, tools, effects, formats exist) | **~88%** | measured in part: menu items 348/375 wired (92.8%), tools 89/92, Illustrator effects 46/50, Photoshop effects 57/57, 3D 4/4 (initial types, no materials) |
|
||||
| **Feature depth** (weighted by use, scored by behaviour) | **~75%** | estimated |
|
||||
| **To beta** | **~170–270 h** one agent · ~50–80 h with 4–6 agents | estimated |
|
||||
@@ -83,11 +69,11 @@ The dimension hours overlap (feature rows hold some UI, format and AI work), so
|
||||
| Strokes, brushes, width profiles | 88% | 8–15 |
|
||||
| Appearance, transparency, graphic styles, masks | 97% | 1–2 |
|
||||
| Live vector effects | 85% | 4–8 |
|
||||
| Raster effects (Effect Gallery) — [effects-parity.md](docs/effects-parity.md) | 85% | 1–2 |
|
||||
| Raster effects (Effect Gallery) — [effects-parity.md](docs/effects-parity.md) | 88% | — |
|
||||
| 3D and Materials | ~25% | 30–55 |
|
||||
| Type core — [type-parity.md](docs/type-parity.md) | 80% | 14–19 |
|
||||
| Type advanced (CJK, spelling, Touch Type) | 50% | 20–28 |
|
||||
| Symbols, blends, envelopes, Repeat, perspective | 87% | 5–9 |
|
||||
| Symbols, blends, envelopes, Repeat, perspective | 88% | 4–7 |
|
||||
| Image Trace, graphs, image tools | 75% | 5–8 |
|
||||
| Layers, artboards, document setup | 78% | 5–8 |
|
||||
| View & navigation | 70% | 9–14 |
|
||||
@@ -131,8 +117,7 @@ Ranked; detail in [docs/roadmap.md](docs/roadmap.md) and [docs/gaps.md](docs/gap
|
||||
2. **`.ai` files from real users** (G9): layer structure, guides, speed: 10–20 h.
|
||||
3. **Launch and stability on real machines** (G10): 20–35 h.
|
||||
4. **Core-tool correctness** (G11: Shape Builder regions, Asset Export borders): 8–15 h.
|
||||
5. **Raster effects** (G2) alongside: the Effect Gallery's filter thumbnails and in-dialog preview area (the gallery and every Photoshop-style filter are in), 1–2 h.
|
||||
6. **Then:** type core and advanced type (G4), the Illustrator 2026 additions (G13), 3D (G3).
|
||||
5. **Then:** type core and advanced type (G4), the Illustrator 2026 additions (G13), 3D (G3). The Effect Gallery's filter thumbnails and in-dialog preview are in (G2).
|
||||
|
||||
## Progress log
|
||||
|
||||
@@ -226,6 +211,13 @@ The full earlier record of what landed is kept in [docs/roadmap.md](docs/roadmap
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-11-10 | minor | Tidy-up, no content change: the unheaded change bullets at the top of the file and a revision row inside the headline-numbers table move to this table under their commit dates. Scores and estimates unchanged. |
|
||||
| 2026-11-10 | minor | Raster effects: Effect Gallery thumbnails and an in-dialog preview (an original drawing through each filter). Features-table Raster effects 85% → 88%, hours 1–2 → none. Headline numbers unchanged, so the progress log is unchanged. |
|
||||
| 2026-11-05 | minor | Compound Shapes add-on (beyond Illustrator): a menu for Make, Release and Expand Compound Shape and a setting, off by default, that makes the Pathfinder panel's Shape Mode buttons create compound shapes on a click. Scores and estimates unchanged. |
|
||||
| 2026-11-04 | minor | Contour add-on (beyond Illustrator): drag a selected shape's edge outward or inward for a live, editable offset copy, with a floating bar for distance and Sharp, Round or Square corners; Bake turns it into paths. Scores and estimates unchanged. |
|
||||
| 2026-11-03 | minor | Paint: new gradients use the document's color model for default stops, including Gradient tool creation. Scores and estimates unchanged. |
|
||||
| 2026-11-03 | minor | UI: Window → Help Bar toggles the tool-help strip separately from the status bar and saves visibility with workspaces. Scores and estimates unchanged. |
|
||||
| 2026-11-03 | minor | Docked Tools stays between left-side panels and the canvas; redocking accepts canvas and window edges. Requested layout choice; scores and estimates unchanged. |
|
||||
| 2026-11-02 | minor | Selection QoL: saved marquee modes, screen drag direction with a 3 px dead zone, and translated mode/E feedback (#5313); VectorCraft addition, scores and estimates unchanged. |
|
||||
| 2026-11-06 | minor | Hindi (`hi`): 7,877-row machine-assisted catalog, zero/one singular plurals, desktop font fallback and optional Devanagari craft-fonts fallback; all 55 translation tests pass; native review and a web font remain; scores and estimates unchanged |
|
||||
| 2026-11-05 | minor | Progress log row for the Type Tools add-on's Release Type on a Path (#5532); headline numbers unchanged. |
|
||||
@@ -262,10 +254,14 @@ The full earlier record of what landed is kept in [docs/roadmap.md](docs/roadmap
|
||||
| 2026-10-13 | minor | Bounded synthetic control input (#1709): counts and queued events validated before mutation; control-channel bursts yield between frames. Scores and hours unchanged. |
|
||||
| 2026-10-14 | minor | Progress log row for the merges of 2026-10-14 (#1641–#1808 as listed there) and Image Trace's #1647 and #1657; the 2026-10-13 row now says that a guide's position in the Transform panel (#1659) is not established by the documentation found and is kept as a convenience pending the owner's decision; headline numbers unchanged. |
|
||||
| 2026-10-13 | minor | Control transport deadlines (#1726): idle and partial clients and blocked replies release connection slots; parity scores and estimates unchanged. |
|
||||
| 2026-10-13 | minor | PDF.R1: editable PDF import uses the shared reader, scans page content in windows, and rejects known incomplete input before returning a document. |
|
||||
| 2026-10-13 | minor | PDF.W1: PDF export and print composition use the shared optional writer for configuration, metadata, font preparation and guarded finalization; drawing and output policy remain app-owned. |
|
||||
| 2026-10-10 | minor | Method changed, no new evidence: full ready for real work is the additive weighted sum, ~62%; hours per audience; beta ~13 points away |
|
||||
| 2026-10-10 | minor | Added the essentials-user readiness score (~75%) |
|
||||
| 2026-10-10 | minor | Ready for real work re-examined against the issue tracker and user feedback: 50% → 55% (full), 65% mainstream added; see target-app-parity.md |
|
||||
| 2026-10-10 | minor | Applied the core-workflow gate (docs/roadmap.md › Alpha gate): passes, stage stays alpha |
|
||||
| 2026-10-10 | major | Re-measured against Illustrator 2026 (30.x) from code counts, 71 open issues and public docs; stage set to alpha; moved the parity estimate, honest assessment and "Shipped so far" to `docs/target-app-parity.md`, the gap list to `docs/gaps.md`, milestones to `docs/roadmap.md`, the `.ai` scope to `docs/file-format-parity.md` |
|
||||
| 2026-10-10 | minor | UI.5: Shared docking adds tab reordering, docking into other groups, dock splits and resizable floating panel groups. Panel positions and return locations persist with workspaces; the native icon rail and Tools panel remain available. |
|
||||
| 2026-10-10 | minor | UI.4: Dock and floating-panel headers use shared accessible tabs with bounded overflow. Widget-state theme application uses `craft-ui`; all four palettes, font stacks and preferences remain app-owned. |
|
||||
| 2026-10-05 | major | Honest assessment by dimension and the prioritized gap list |
|
||||
| 2026-10-02 | major | First 22-area weighted parity estimate |
|
||||
@@ -5,7 +5,7 @@
|
||||
//! vectorcraft-cli mcp [--connect 127.0.0.1:7979 | --headless] [--automation-read-root DIR]... [--automation-write-root DIR]...
|
||||
//! vectorcraft-cli run [--in FILE] [--cmd id [--params '{json}'] | --commands FILE|-]... [--export out.svg]... [--scale 2] [--ppi 300]
|
||||
//! vectorcraft-cli commands
|
||||
//! vectorcraft-cli convert IN OUT [--scale 2] [--ppi 300] [--artboard 0 | --range 1-3,5] [--outline-text] [--decimals N] [--replace-lossy] [--editing-data] [--preserve-editing] [--ai-version 7|pdf] [--hidden-objects layer|omit|visible] [--transparent-images flatten|omit]
|
||||
//! vectorcraft-cli convert IN OUT [--scale 2] [--ppi 300] [--artboard 0 | --range 1-3,5] [--outline-text] [--decimals N] [--replace-lossy] [--editing-data] [--legacy-name-encoding ENC] [--preserve-editing] [--ai-version 7|pdf] [--hidden-objects layer|omit|visible] [--transparent-images flatten|omit]
|
||||
//! vectorcraft-cli info FILE
|
||||
//! vectorcraft-cli bench FILE [--size 2880x1800] [--iters 5]
|
||||
//! vectorcraft-cli perf [--paths 50000] [--deep]
|
||||
@@ -78,7 +78,7 @@ USAGE:
|
||||
Safe Mode is on. Plug-ins stay installed. `--safe-mode mcp` serves a headless session and
|
||||
does not take --connect.
|
||||
|
||||
vectorcraft-cli mcp [--connect ADDR | --headless] [--automation-read-root DIR]... [--automation-write-root DIR]...
|
||||
vectorcraft-cli mcp [--connect ADDR | --headless] [--automation-read-root DIR]... [--automation-write-root DIR]... [--allow-plugin-grants]
|
||||
Run the MCP server on stdio. Default: connect to a running app at 127.0.0.1:7979
|
||||
(vectorcraft --control 7979), falling back to a headless in-process session.
|
||||
--automation-read-root / --automation-write-root confine the files the agent's commands read
|
||||
@@ -86,8 +86,18 @@ USAGE:
|
||||
DIR, links followed; each may be given more than once, a path inside any of its folders being
|
||||
allowed, and a kind left out grants none of its access. They imply --headless: a running app
|
||||
enforces its own (vectorcraft --control PORT --automation-read-root DIR …).
|
||||
--allow-plugin-grants lets plugin.install's grant and allowHeavy take effect in this session,
|
||||
so whoever sends the commands can give a plug-in the permissions it lists, and a heavy budget,
|
||||
with nobody approving them. Without it they are refused, and a plug-in that lists permissions
|
||||
installs only when its hash is already recorded in grants.json, by a person's approval in the
|
||||
desktop app or by a grant accepted under this flag. A grant accepted under this flag stays
|
||||
recorded and counts as approved in later sessions. It implies --headless. It can't be used
|
||||
with --automation-read-root or --automation-write-root: a server confined to folders has no
|
||||
plug-in store, so it reads no grants.json, and a plug-in that lists permissions or asks for a
|
||||
heavy budget does not install there. With VECTORCRAFT_NO_PREFS set there is no store either,
|
||||
and a grant accepted under this flag lasts for that session only.
|
||||
|
||||
vectorcraft-cli run [--in FILE] [--cmd ID [--params JSON] | --commands FILE|-]... [--export FILE]... [--scale N] [--ppi N]
|
||||
vectorcraft-cli run [--in FILE] [--cmd ID [--params JSON] | --commands FILE|-]... [--export FILE]... [--scale N] [--ppi N] [--allow-plugin-grants]
|
||||
Headless batch: open FILE (any readable format) or start a new document, run commands in
|
||||
order, export each FILE in the format its extension picks (see Writable formats; a .ait FILE
|
||||
is a template, written as file.saveAsTemplate writes it). Raster exports use the document's
|
||||
@@ -97,11 +107,13 @@ USAGE:
|
||||
--cmd and --export steps: a JSON array of {\"command\": ID, \"params\": {...}} (or \"id\" for
|
||||
\"command\"), or one such object per line (JSON Lines), at most 64 MiB and 100000 steps.
|
||||
Errors name the step, counting from 0 as \"$N\" references do.
|
||||
--allow-plugin-grants lets plugin.install's grant and allowHeavy take effect in this run, as
|
||||
for mcp.
|
||||
|
||||
vectorcraft-cli commands
|
||||
Print the command catalogue as JSON.
|
||||
|
||||
vectorcraft-cli convert IN OUT [--scale N] [--ppi N] [--artboard I | --range R] [--outline-text] [--decimals N] [--replace-lossy] [--editing-data] [--preserve-editing] [--ai-version 7|pdf] [--hidden-objects layer|omit|visible] [--transparent-images flatten|omit]
|
||||
vectorcraft-cli convert IN OUT [--scale N] [--ppi N] [--artboard I | --range R] [--outline-text] [--decimals N] [--replace-lossy] [--editing-data] [--legacy-name-encoding ENC] [--preserve-editing] [--ai-version 7|pdf] [--hidden-objects layer|omit|visible] [--transparent-images flatten|omit]
|
||||
Open IN (any readable format) and export OUT in the format its extension picks (see Writable
|
||||
formats). Raster resolution: --ppi, else 72 times --scale, else the document's raster effects
|
||||
resolution; other formats leave them out. --artboard is 0-based, --range 1-based (\"1-3,5\"); a PDF gets every artboard
|
||||
@@ -114,7 +126,10 @@ USAGE:
|
||||
IN left things out (hidden text, art or layers it could not read; the notes say which) unless
|
||||
--replace-lossy is given. --editing-data opens a .ai or an EPS with editing data from its editing copy
|
||||
even where that draws differently from its PDF part or page (which can be older), as long as
|
||||
it can be read. A PDF OUT omits the embedded native document unless --preserve-editing is
|
||||
it can be read. --legacy-name-encoding reads the names in a .ai's or an EPS's editing data
|
||||
that aren't UTF-8 (layers, objects, artboards, colors, gradients, patterns, graphic styles,
|
||||
swatches, brushes, symbols, variables and data sets, and data sets' text values) in
|
||||
windows-1252 (the default), macintosh, windows-949, shift_jis, gbk or big5. A PDF OUT omits the embedded native document unless --preserve-editing is
|
||||
given (the app's Save As PDF still defaults to keeping it). With --preserve-editing, when the
|
||||
attachment is written the result's warnings say so (it is left out, with its own warning, for
|
||||
--artboard or a --range without every artboard).
|
||||
@@ -221,12 +236,24 @@ fn main() -> ExitCode {
|
||||
}
|
||||
|
||||
/// A headless session that remembers what the user keeps between runs: the PDF presets saved, the
|
||||
/// ICC profiles loaded and the plug-ins installed (#2168, #2174, #2541), in the folders next to the preferences that the desktop
|
||||
/// app uses too. `VECTORCRAFT_NO_PREFS` and `VECTORCRAFT_CONFIG_DIR` apply as they do there.
|
||||
/// ICC profiles loaded, the plug-ins installed (#2168, #2174, #2541), the custom dictionary's words (#4936) and the Color Settings (#6335), in the folders
|
||||
/// and files next to the preferences that the desktop app uses too, and lists the scripts of the Scripts folder there
|
||||
/// (`script.listInstalled`). `VECTORCRAFT_NO_PREFS` and `VECTORCRAFT_CONFIG_DIR` apply as they do there.
|
||||
fn with_stores(mut h: Headless) -> Headless {
|
||||
prefs_dir::attach_scripts_dir(&mut h.session);
|
||||
prefs_dir::attach_profile_store(&mut h.session);
|
||||
prefs_dir::attach_color_settings(&mut h.session);
|
||||
prefs_dir::attach_plugin_store(&mut h.session);
|
||||
prefs_dir::attach_pdf_preset_store(&mut h.session);
|
||||
prefs_dir::attach_custom_dictionary(&mut h.session);
|
||||
h
|
||||
}
|
||||
|
||||
/// Makes `h`'s `plugin.install` accept `grant` and `allowHeavy` when `allow` (`--allow-plugin-grants`).
|
||||
fn accepting_grants(mut h: Headless, allow: bool) -> Headless {
|
||||
if allow {
|
||||
h.session.allow_plugin_grants_from_requests();
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
@@ -238,6 +265,7 @@ fn mcp(args: &[String], safe_mode: bool) -> Result<(), String> {
|
||||
}
|
||||
let mut connect: Option<String> = None;
|
||||
let mut headless = false;
|
||||
let mut allow_grants = false;
|
||||
let (mut read_roots, mut write_roots): (Vec<String>, Vec<String>) = (vec![], vec![]);
|
||||
let mut it = args.iter();
|
||||
while let Some(a) = it.next() {
|
||||
@@ -249,6 +277,7 @@ fn mcp(args: &[String], safe_mode: bool) -> Result<(), String> {
|
||||
match flag {
|
||||
"--connect" => connect = Some(it.next().cloned().ok_or("--connect needs an address")?),
|
||||
"--headless" => headless = true,
|
||||
"--allow-plugin-grants" => allow_grants = true,
|
||||
"--automation-read-root" | "--automation-write-root" => {
|
||||
let dir = inline.or_else(|| it.next().cloned()).ok_or_else(|| format!("{flag} needs a folder"))?;
|
||||
if flag == "--automation-read-root" { read_roots.push(dir) } else { write_roots.push(dir) }
|
||||
@@ -259,6 +288,12 @@ fn mcp(args: &[String], safe_mode: bool) -> Result<(), String> {
|
||||
if headless && connect.is_some() {
|
||||
return Err("use either --connect or --headless".into());
|
||||
}
|
||||
if allow_grants && connect.is_some() {
|
||||
return Err("--allow-plugin-grants applies to the headless server; in a running app a person approves each plug-in".into());
|
||||
}
|
||||
if allow_grants && !(read_roots.is_empty() && write_roots.is_empty()) {
|
||||
return Err("--allow-plugin-grants can't be used with --automation-read-root or --automation-write-root: a server confined to folders has no plug-in store to record a grant in".into());
|
||||
}
|
||||
if safe_mode && connect.is_some() {
|
||||
return Err("--safe-mode runs a headless session without plug-ins; a running app loads its own: start it with `vectorcraft --safe-mode --control <port>`".into());
|
||||
}
|
||||
@@ -273,9 +308,9 @@ fn mcp(args: &[String], safe_mode: bool) -> Result<(), String> {
|
||||
// Every thread of this process, not only the server's: nothing reaches past the roots.
|
||||
file_access::confine_process(roots.clone());
|
||||
eprintln!("vectorcraft-cli: files confined: {}", roots.describe());
|
||||
Box::new(Headless::with_document().with_automation_roots(Some(roots)))
|
||||
} else if headless || safe_mode {
|
||||
Box::new(with_stores(Headless::with_document()))
|
||||
Box::new(accepting_grants(Headless::with_document().with_automation_roots(Some(roots)), allow_grants))
|
||||
} else if headless || safe_mode || allow_grants {
|
||||
Box::new(accepting_grants(with_stores(Headless::with_document()), allow_grants))
|
||||
} else if let Some(addr) = connect {
|
||||
// Explicit address: fail loudly if the app isn't there.
|
||||
Box::new(Remote::connect(&addr).map_err(|e| format!("cannot connect to {addr}: {e}"))?)
|
||||
@@ -302,8 +337,20 @@ fn commands() -> Result<(), String> {
|
||||
}
|
||||
|
||||
fn convert(args: &[String]) -> Result<(), String> {
|
||||
let valued =
|
||||
["--scale", "-s", "--ppi", "--artboard", "-a", "--range", "-r", "--decimals", "--ai-version", "--hidden-objects", "--transparent-images"];
|
||||
let valued = [
|
||||
"--scale",
|
||||
"-s",
|
||||
"--ppi",
|
||||
"--artboard",
|
||||
"-a",
|
||||
"--range",
|
||||
"-r",
|
||||
"--decimals",
|
||||
"--ai-version",
|
||||
"--hidden-objects",
|
||||
"--transparent-images",
|
||||
"--legacy-name-encoding",
|
||||
];
|
||||
if help_requested(args, &valued) {
|
||||
return help();
|
||||
}
|
||||
@@ -312,6 +359,9 @@ fn convert(args: &[String]) -> Result<(), String> {
|
||||
(None::<f64>, None::<u64>, None::<String>, false, false, false, false);
|
||||
let mut ppi = None::<f64>;
|
||||
let mut decimals = None::<u64>;
|
||||
// The encoding of a legacy `.ai`'s names that aren't UTF-8, passed on as `document.open`'s
|
||||
// `legacyNameEncoding`; the engine checks it.
|
||||
let mut legacy_names = None::<String>;
|
||||
// Legacy `.ai` options, passed on as the export's own (`aiVersion`, `hiddenObjects`,
|
||||
// `transparentImages`); the engine checks their values.
|
||||
let mut legacy: Vec<(&str, String)> = vec![];
|
||||
@@ -334,6 +384,9 @@ fn convert(args: &[String]) -> Result<(), String> {
|
||||
"--replace-lossy" => replace_lossy = true,
|
||||
"--editing-data" => editing_data = true,
|
||||
"--preserve-editing" => preserve_editing = true,
|
||||
"--legacy-name-encoding" => {
|
||||
legacy_names = Some(it.next().cloned().ok_or("--legacy-name-encoding needs an encoding such as windows-949")?);
|
||||
}
|
||||
"--ai-version" => legacy.push(("aiVersion", it.next().cloned().ok_or("--ai-version needs 7 or pdf")?)),
|
||||
"--hidden-objects" => legacy.push(("hiddenObjects", it.next().cloned().ok_or("--hidden-objects needs layer, omit or visible")?)),
|
||||
"--transparent-images" => legacy.push(("transparentImages", it.next().cloned().ok_or("--transparent-images needs flatten or omit")?)),
|
||||
@@ -346,8 +399,14 @@ fn convert(args: &[String]) -> Result<(), String> {
|
||||
return Err("a .ait template holds every artboard: --artboard and --range don't apply".into());
|
||||
}
|
||||
let mut h = Headless::new();
|
||||
let opened = if editing_data {
|
||||
let params = json!({"path": input, "editingData": "always"});
|
||||
let opened = if editing_data || legacy_names.is_some() {
|
||||
let mut params = json!({"path": input});
|
||||
if editing_data {
|
||||
params["editingData"] = json!("always");
|
||||
}
|
||||
if let Some(e) = &legacy_names {
|
||||
params["legacyNameEncoding"] = json!(e);
|
||||
}
|
||||
h.call("engine.execute", json!({"command": "document.open", "params": params})).map_err(|e| format!("open {input}: {e}"))?
|
||||
} else {
|
||||
h.call("app.open", json!({"path": input})).map_err(|e| format!("open {input}: {e}"))?
|
||||
@@ -534,6 +593,7 @@ fn run(args: &[String]) -> Result<(), String> {
|
||||
let mut stdin_read = false;
|
||||
let mut scale = None::<f64>;
|
||||
let mut ppi = None::<f64>;
|
||||
let mut allow_grants = false;
|
||||
let mut it = args.iter();
|
||||
while let Some(a) = it.next() {
|
||||
let mut val = |what: &str| it.next().cloned().ok_or_else(|| format!("{a} needs {what}"));
|
||||
@@ -568,6 +628,7 @@ fn run(args: &[String]) -> Result<(), String> {
|
||||
steps.push(Step::Export(val("a file")?));
|
||||
params_for = None;
|
||||
}
|
||||
"--allow-plugin-grants" => allow_grants = true,
|
||||
"--scale" | "-s" => {
|
||||
let raw = val("a number")?;
|
||||
scale = Some(raw.parse::<f64>().map_err(|_| format!("--scale {raw}: not a number"))?);
|
||||
@@ -585,7 +646,7 @@ fn run(args: &[String]) -> Result<(), String> {
|
||||
}
|
||||
}
|
||||
|
||||
let mut h = with_stores(Headless::new());
|
||||
let mut h = accepting_grants(with_stores(Headless::new()), allow_grants);
|
||||
let mut out = std::io::stdout().lock();
|
||||
// A reader that stopped early (a closed pipe) isn't an error, and the batch goes on: its later
|
||||
// steps (an export…) still run, unprinted.
|
||||
|
||||
@@ -19,6 +19,77 @@ fn empty_dir(dir: std::path::PathBuf) -> std::path::PathBuf {
|
||||
dir
|
||||
}
|
||||
|
||||
/// `run` and `mcp --headless` refuse `plugin.install` with this param unless the process was started
|
||||
/// with `--allow-plugin-grants`.
|
||||
fn approval_param_needs_the_start_up_flag(key: &str, value: Value) {
|
||||
let module = vectorcraft_format::base64_encode(include_bytes!("../../../../crates/plugins/tests/fixtures/desaturate.wasm"));
|
||||
let steps = tmp(&format!("{key}.json"));
|
||||
std::fs::write(&steps, json!([{"command": "plugin.install", "params": {"dataBase64": module, key: value}}]).to_string()).unwrap();
|
||||
let config = empty_dir(std::env::temp_dir().join(format!("vectorcraft-cli-test-{key}-{}", std::process::id())));
|
||||
let run = |extra: &[&str]| {
|
||||
Command::new(BIN).env("VECTORCRAFT_CONFIG_DIR", &config).args(["run", "--commands", steps.to_str().unwrap()]).args(extra).output().unwrap()
|
||||
};
|
||||
let out = run(&[]);
|
||||
assert!(!out.status.success());
|
||||
let shown = String::from_utf8_lossy(&out.stderr).into_owned() + &String::from_utf8_lossy(&out.stdout);
|
||||
assert!(shown.contains("--allow-plugin-grants"), "{shown}");
|
||||
let out = run(&["--allow-plugin-grants"]);
|
||||
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
||||
|
||||
let mcp_config = empty_dir(std::env::temp_dir().join(format!("vectorcraft-cli-test-mcp-{key}-{}", std::process::id())));
|
||||
let mcp_install = |extra: &[&str]| -> Value {
|
||||
let mut child = Command::new(BIN)
|
||||
.env("VECTORCRAFT_CONFIG_DIR", &mcp_config)
|
||||
.args(["mcp", "--headless"])
|
||||
.args(extra)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
{
|
||||
let mut stdin = child.stdin.take().unwrap();
|
||||
let params = json!({"dataBase64": module, key: value});
|
||||
for m in [
|
||||
json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"0"}}}),
|
||||
json!({"jsonrpc":"2.0","method":"notifications/initialized"}),
|
||||
json!({"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"run_command","arguments":{"command":"plugin.install","params":params}}}),
|
||||
] {
|
||||
writeln!(stdin, "{m}").unwrap();
|
||||
}
|
||||
}
|
||||
let out = child.wait_with_output().unwrap();
|
||||
assert!(out.status.success());
|
||||
let replies: Vec<Value> = String::from_utf8(out.stdout).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
|
||||
replies.into_iter().find(|r| r["id"] == 2).unwrap()["result"].take()
|
||||
};
|
||||
let refused = mcp_install(&[]);
|
||||
assert_eq!(refused["isError"], true, "without the flag: {refused}");
|
||||
assert!(refused["content"][0]["text"].as_str().unwrap().contains("--allow-plugin-grants"), "{refused}");
|
||||
let accepted = mcp_install(&["--allow-plugin-grants"]);
|
||||
assert_eq!(accepted["isError"], false, "with the flag: {accepted}");
|
||||
}
|
||||
|
||||
/// `run` and `mcp --headless` refuse `plugin.install`'s `grant` and `allowHeavy` unless the process was
|
||||
/// started with `--allow-plugin-grants`, which `mcp` won't take with `--connect` or the automation roots.
|
||||
#[test]
|
||||
fn plugin_grants_need_the_start_up_flag() {
|
||||
approval_param_needs_the_start_up_flag("grant", json!(["doc.read"]));
|
||||
approval_param_needs_the_start_up_flag("allowHeavy", json!(true));
|
||||
|
||||
let out = Command::new(BIN).args(["mcp", "--allow-plugin-grants", "--connect", "127.0.0.1:1"]).output().unwrap();
|
||||
assert!(!out.status.success());
|
||||
assert!(String::from_utf8_lossy(&out.stderr).contains("--allow-plugin-grants applies to the headless server"));
|
||||
let root = std::env::temp_dir();
|
||||
for flag in ["--automation-read-root", "--automation-write-root"] {
|
||||
let out = Command::new(BIN).args(["mcp", "--allow-plugin-grants", flag, root.to_str().unwrap()]).stdin(Stdio::null()).output().unwrap();
|
||||
assert!(!out.status.success(), "{flag}");
|
||||
assert!(String::from_utf8_lossy(&out.stderr).contains("--allow-plugin-grants can't be used with"), "{flag}");
|
||||
}
|
||||
let usage = Command::new(BIN).arg("help").output().unwrap();
|
||||
assert!(String::from_utf8_lossy(&usage.stdout).contains("--allow-plugin-grants lets plugin.install's grant and allowHeavy take effect"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commands_prints_catalogue() {
|
||||
let out = Command::new(BIN).arg("commands").output().unwrap();
|
||||
@@ -526,7 +597,7 @@ fn exercise_mcp_core(
|
||||
assert!(r["result"]["completion"]["values"].as_array().is_some_and(|v| v.contains(&json!("svg"))), "{r}");
|
||||
|
||||
mcp_send(stdin, json!({"jsonrpc":"2.0","id":5,"method":"resources/list","params":{}}));
|
||||
assert!(mcp_recv(lines, notes, 5)["result"]["resources"].as_array().is_some_and(|v| v.len() == 3));
|
||||
assert!(mcp_recv(lines, notes, 5)["result"]["resources"].as_array().is_some_and(|v| v.len() == 4));
|
||||
mcp_send(stdin, json!({"jsonrpc":"2.0","id":6,"method":"resources/templates/list","params":{}}));
|
||||
assert!(mcp_recv(lines, notes, 6)["result"]["resourceTemplates"].as_array().is_some_and(|v| v.len() >= 4));
|
||||
|
||||
|
||||
@@ -5,3 +5,4 @@
|
||||
mod cli;
|
||||
mod fileio;
|
||||
mod mcp_e2e;
|
||||
mod script;
|
||||
@@ -0,0 +1,39 @@
|
||||
use std::process::Command;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
const BIN: &str = env!("CARGO_BIN_EXE_vectorcraft-cli");
|
||||
|
||||
/// `run` with `script.run` after `file.new`, with `params`.
|
||||
fn run_script(params: &Value) -> std::process::Output {
|
||||
Command::new(BIN).args(["run", "--cmd", "file.new", "--cmd", "script.run", "--params", ¶ms.to_string()]).output().unwrap()
|
||||
}
|
||||
|
||||
/// `run` runs a script headless: the reply holds the report, with what the script logged and the
|
||||
/// documents it changed.
|
||||
#[test]
|
||||
fn run_gives_the_report_of_a_script() {
|
||||
let source = "vc.run('shape.rectangle', {x: 0, y: 0, width: 10, height: 10}); vc.log(vc.args.n * 2);";
|
||||
let out = run_script(&json!({"source": source, "name": "double.js", "args": {"n": 21}}));
|
||||
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
||||
let lines: Vec<Value> = String::from_utf8_lossy(&out.stdout).lines().map(|l| serde_json::from_str(l).unwrap()).collect();
|
||||
assert_eq!(lines.len(), 2, "{lines:?}");
|
||||
assert_eq!(lines[1]["command"], "script.run");
|
||||
let report = &lines[1]["result"];
|
||||
assert_eq!(report["ok"], true, "{report}");
|
||||
assert_eq!(report["name"], "double.js");
|
||||
assert_eq!(report["log"], json!([42]), "{report}");
|
||||
assert_eq!(report["changed"].as_array().map(Vec::len), Some(1), "{report}");
|
||||
}
|
||||
|
||||
/// A script that throws fails its step with the script's message, and the steps before it ran.
|
||||
#[test]
|
||||
fn run_fails_the_step_of_a_script_that_throws() {
|
||||
let out = run_script(&json!({"source": "vc.run('shape.rectangle', {x: 0, y: 0, width: 10, height: 10}); throw new Error('no margin given');"}));
|
||||
assert!(!out.status.success());
|
||||
let stderr = String::from_utf8_lossy(&out.stderr);
|
||||
assert!(stderr.contains("step 1 (script.run)") && stderr.contains("no margin given"), "{stderr}");
|
||||
let printed: Vec<Value> = String::from_utf8_lossy(&out.stdout).lines().map(|l| serde_json::from_str(l).unwrap()).collect();
|
||||
assert_eq!(printed.len(), 1, "only file.new printed: {printed:?}");
|
||||
assert_eq!(printed[0]["command"], "file.new");
|
||||
}
|
||||
@@ -26,7 +26,7 @@ log = { workspace = true }
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
# `wgpu` pulls egui-wgpu's defaults: WebGPU plus the WebGL2 fallback.
|
||||
eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit"] }
|
||||
rfd = { workspace = true }
|
||||
rfd = { workspace = true, features = ["file-handle-inner"] }
|
||||
wasm-bindgen = "0.2"
|
||||
serde_json = { workspace = true }
|
||||
wasm-bindgen-futures = "0.4"
|
||||
|
||||
@@ -9,6 +9,9 @@ use vectorcraft_engine::Session;
|
||||
use vectorcraft_engine::cmd::clipboard::{FILE_HEAD, Flavour, PASTE_ORDER, file_flavour, is_address};
|
||||
use vectorcraft_engine::cmd::fileio;
|
||||
use vectorcraft_engine::cmd::recovery::{self, Hold, RecoveryStore};
|
||||
use vectorcraft_engine::cmd::scripts::RUN_EXTS;
|
||||
use vectorcraft_engine::script::MAX_SOURCE_BYTES;
|
||||
use vectorcraft_ui_egui::dialogs::script_run::ScriptInbox;
|
||||
use vectorcraft_ui_egui::graphics::GraphicsLoss;
|
||||
use vectorcraft_ui_egui::i18n;
|
||||
use vectorcraft_ui_egui::place::{DropTarget, PlaceArrival, PlaceInbox};
|
||||
@@ -47,6 +50,7 @@ struct Host {
|
||||
runner: eframe::WebRunner,
|
||||
inbox: Inbox,
|
||||
place_inbox: PlaceInbox,
|
||||
script_inbox: ScriptInbox,
|
||||
drag: DragPos,
|
||||
pasted: Rc<RefCell<Pasted>>,
|
||||
/// Pen and finger input read from the page's Pointer Events (`crate::pencil`).
|
||||
@@ -75,6 +79,7 @@ pub fn start() {
|
||||
runner: eframe::WebRunner::new(),
|
||||
inbox: Arc::default(),
|
||||
place_inbox: Arc::default(),
|
||||
script_inbox: Arc::default(),
|
||||
drag: DragPos::default(),
|
||||
pasted: Rc::default(),
|
||||
pointers: SharedPointers::default(),
|
||||
@@ -120,7 +125,8 @@ async fn run(host: Host, canvas: web_sys::HtmlCanvasElement, moving: Option<Movi
|
||||
}
|
||||
host.pasted.borrow_mut().ctx = Some(ctx.clone());
|
||||
let stylus = host.pointers.borrow().stylus();
|
||||
let services = services(host.inbox.clone(), host.place_inbox.clone(), ctx.clone(), host.locks.clone(), stylus);
|
||||
let services =
|
||||
services(host.inbox.clone(), host.place_inbox.clone(), host.script_inbox.clone(), ctx.clone(), host.locks.clone(), stylus);
|
||||
let app = match taken.borrow_mut().take() {
|
||||
Some(mut app) => {
|
||||
// The old services asked the old context for frames.
|
||||
@@ -425,12 +431,35 @@ impl eframe::App for WebShell {
|
||||
}
|
||||
}
|
||||
|
||||
fn services(inbox: Inbox, place_inbox: PlaceInbox, ctx: egui::Context, locks: Option<WebLocks>, stylus: StylusFeed) -> Services {
|
||||
fn services(
|
||||
inbox: Inbox,
|
||||
place_inbox: PlaceInbox,
|
||||
script_inbox: ScriptInbox,
|
||||
ctx: egui::Context,
|
||||
locks: Option<WebLocks>,
|
||||
stylus: StylusFeed,
|
||||
) -> Services {
|
||||
let open_inbox = inbox.clone();
|
||||
let picked = place_inbox.clone();
|
||||
let place_ctx = ctx.clone();
|
||||
let screen_color = eye_dropper(ctx.clone());
|
||||
let (script_picked, script_ctx) = (script_inbox.clone(), ctx.clone());
|
||||
Services {
|
||||
// File › Scripts › Other Script…: the picked file goes to the warning before it runs.
|
||||
script_async: Some(Box::new(move || {
|
||||
let inbox = script_picked.clone();
|
||||
let ctx = script_ctx.clone();
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let filter = vectorcraft_ui_egui::i18n::tr_ctx(vectorcraft_ui_egui::i18n::current(), "file-filter", "Scripts");
|
||||
let Some(file) = rfd::AsyncFileDialog::new().add_filter(filter, RUN_EXTS).pick_file().await else {
|
||||
return;
|
||||
};
|
||||
let bytes = if file.inner().size() > MAX_SOURCE_BYTES as f64 { None } else { Some(file.read().await) };
|
||||
inbox.lock().unwrap_or_else(|e| e.into_inner()).push((file.file_name(), bytes));
|
||||
ctx.request_repaint();
|
||||
});
|
||||
})),
|
||||
script_inbox: Some(script_inbox),
|
||||
// File → Place…: the picked files go to the Place dialog.
|
||||
place_async: Some(Box::new(move || {
|
||||
let inbox = picked.clone();
|
||||
|
||||
@@ -67,7 +67,7 @@ vectorcraft-render = { workspace = true }
|
||||
# with default features off). Pinned exactly: review the diff before bumping.
|
||||
muda = { version = "=0.21.2", default-features = false }
|
||||
objc2 = "0.6"
|
||||
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "block2", "objc2-core-foundation", "objc2-quartz-core", "NSApplication", "NSButton", "NSColor", "NSColorSampler", "NSColorSpace", "NSControl", "NSMenu", "NSEvent", "NSPasteboard", "NSPopUpButton", "NSResponder", "NSSavePanel", "NSTextField", "NSTextInputContext", "NSView", "NSWindow"] }
|
||||
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "block2", "objc2-core-foundation", "objc2-quartz-core", "NSApplication", "NSCursor", "NSImage", "NSImageRep", "NSBitmapImageRep", "NSButton", "NSColor", "NSColorSampler", "NSColorSpace", "NSControl", "NSMenu", "NSEvent", "NSPasteboard", "NSPopUpButton", "NSResponder", "NSSavePanel", "NSTextField", "NSTextInputContext", "NSView", "NSWindow"] }
|
||||
# Files opened from Finder and the Dock (src/open_documents.rs), pasted bitmaps (src/clipboard.rs) and the control channel's App Nap activity (src/control_server.rs); both already come with winit, muda and rfd.
|
||||
objc2-foundation = { version = "0.3", default-features = false, features = ["std", "block2", "NSArray", "NSData", "NSGeometry", "NSNotification", "NSOperation", "NSProcessInfo", "NSString", "NSURL"] }
|
||||
block2 = "0.6"
|
||||
|
||||
@@ -92,12 +92,21 @@ impl eframe::App for App {
|
||||
}
|
||||
fn on_exit(&mut self) {
|
||||
file_access::unconfined(|| save_prefs(&self.app));
|
||||
// A ready update installs once the process has exited. The macOS exit below skips
|
||||
// dropping the app, so the helper is started here.
|
||||
self.app.start_pending_update();
|
||||
#[cfg(target_os = "macos")]
|
||||
super::mac_cursor::release();
|
||||
end_before_teardown(self.frames);
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
fn logic_frame(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
#[cfg(target_os = "macos")]
|
||||
if self.winit_window.as_ref().is_some_and(|w| !w.has_focus() || w.is_visible() == Some(false) || w.is_minimized() == Some(true)) {
|
||||
super::mac_cursor::release();
|
||||
}
|
||||
self.frames = ctx.cumulative_frame_nr();
|
||||
if let Some(why) = self.graphics_loss.take() {
|
||||
// eframe can't give a window a new device: the user saves and starts again.
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
//! AppKit delivery of the UI's existing cursor artwork, independent of canvas presentation.
|
||||
#![forbid(unsafe_code)]
|
||||
// Interim #2292: remove when pinned winit correctly handles macOS cursor image and hotspot sizing.
|
||||
|
||||
use egui::{Context, FullOutput, Plugin, ViewportId};
|
||||
use objc2::{AllocAnyThread, MainThreadMarker, rc::Retained};
|
||||
use objc2_app_kit::{NSApplication, NSBitmapImageRep, NSCursor, NSImage, NSWindow};
|
||||
use objc2_foundation::{NSData, NSPoint, NSSize};
|
||||
use std::cell::RefCell;
|
||||
use vectorcraft_ui_egui::cursors::native_request::{self, Bitmap, Key, Request};
|
||||
|
||||
const CAPACITY: usize = 128;
|
||||
|
||||
fn build(bitmap: &Bitmap) -> Option<Retained<NSCursor>> {
|
||||
let one = &bitmap.representations[0];
|
||||
let size = NSSize::new(f64::from(one.size[0]), f64::from(one.size[1]));
|
||||
let image = NSImage::initWithSize(NSImage::alloc(), size);
|
||||
for bitmap in &bitmap.representations {
|
||||
let pixels = image::RgbaImage::from_raw(u32::from(bitmap.size[0]), u32::from(bitmap.size[1]), bitmap.rgba.to_vec())?;
|
||||
let mut png = std::io::Cursor::new(Vec::new());
|
||||
pixels.write_to(&mut png, image::ImageFormat::Png).ok()?;
|
||||
let rep = NSBitmapImageRep::imageRepWithData(&NSData::with_bytes(png.get_ref()))?;
|
||||
rep.setSize(size);
|
||||
image.addRepresentation(&rep);
|
||||
}
|
||||
Some(NSCursor::initWithImage_hotSpot(NSCursor::alloc(), &image, NSPoint::new(f64::from(one.hotspot[0]), f64::from(one.hotspot[1]))))
|
||||
}
|
||||
|
||||
/// The ownership protocol also runs against a headless backend in tests.
|
||||
trait Backend {
|
||||
type Window: Clone + PartialEq;
|
||||
type Cursor: Clone + PartialEq;
|
||||
fn window(&self) -> Option<Self::Window>;
|
||||
fn enabled(&self, window: &Self::Window) -> bool;
|
||||
fn suspend(&mut self, window: &Self::Window);
|
||||
fn restore(&mut self, window: &Self::Window);
|
||||
fn build(&mut self, request: &Request) -> Option<Self::Cursor>;
|
||||
fn current(&self) -> Option<Self::Cursor>;
|
||||
fn set(&mut self, cursor: &Self::Cursor);
|
||||
}
|
||||
|
||||
struct State<B: Backend> {
|
||||
backend: B,
|
||||
candidate: Option<B::Window>,
|
||||
cache: Vec<(Key, Option<B::Cursor>)>,
|
||||
// Retain the active object independently of cache eviction.
|
||||
active: Option<(Key, B::Window, B::Cursor)>,
|
||||
}
|
||||
|
||||
impl<B: Backend> State<B> {
|
||||
fn new(backend: B) -> Self {
|
||||
Self { backend, candidate: None, cache: Vec::new(), active: None }
|
||||
}
|
||||
|
||||
fn begin(&mut self) {
|
||||
self.candidate = self.backend.window();
|
||||
}
|
||||
|
||||
fn release(&mut self) {
|
||||
self.candidate = None;
|
||||
if let Some((_, window, _)) = self.active.take() {
|
||||
self.backend.restore(&window);
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&mut self, request: Option<Request>) {
|
||||
let window = self.candidate.take().filter(|w| self.backend.window().as_ref() == Some(w));
|
||||
let (Some(request), Some(window)) = (request, window) else {
|
||||
self.release();
|
||||
return;
|
||||
};
|
||||
if let Some((key, owned, cursor)) = &self.active
|
||||
&& *key == request.key
|
||||
&& *owned == window
|
||||
{
|
||||
if self.backend.current().as_ref() != Some(cursor) {
|
||||
self.backend.set(cursor);
|
||||
}
|
||||
return;
|
||||
}
|
||||
self.release();
|
||||
// Never take another component's suspension (a sheet or native tracking session).
|
||||
if !self.backend.enabled(&window) {
|
||||
return;
|
||||
}
|
||||
let cursor = if let Some((_, cursor)) = self.cache.iter().find(|(key, _)| *key == request.key) {
|
||||
cursor.clone()
|
||||
} else {
|
||||
let cursor = self.backend.build(&request);
|
||||
if self.cache.len() >= CAPACITY {
|
||||
self.cache.clear();
|
||||
}
|
||||
self.cache.push((request.key, cursor.clone()));
|
||||
cursor
|
||||
};
|
||||
if let Some(cursor) = cursor {
|
||||
self.backend.suspend(&window);
|
||||
self.backend.set(&cursor);
|
||||
self.active = Some((request.key, window, cursor));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct AppKit {
|
||||
root: std::sync::Weak<winit::window::Window>,
|
||||
}
|
||||
impl Backend for AppKit {
|
||||
type Window = Retained<NSWindow>;
|
||||
type Cursor = Retained<NSCursor>;
|
||||
fn window(&self) -> Option<Self::Window> {
|
||||
let mtm = MainThreadMarker::new()?;
|
||||
// Bind the candidate to the host's actual root window without converting raw pointers.
|
||||
// A floating native panel may be key while egui still has stale root focus input.
|
||||
let root = self.root.upgrade()?;
|
||||
if !root.has_focus() || root.is_visible() == Some(false) || root.is_minimized() == Some(true) {
|
||||
return None;
|
||||
}
|
||||
NSApplication::sharedApplication(mtm).keyWindow().filter(|w| w.isVisible() && w.isKeyWindow())
|
||||
}
|
||||
fn enabled(&self, window: &Self::Window) -> bool {
|
||||
window.areCursorRectsEnabled()
|
||||
}
|
||||
fn suspend(&mut self, window: &Self::Window) {
|
||||
window.disableCursorRects();
|
||||
}
|
||||
fn restore(&mut self, window: &Self::Window) {
|
||||
window.enableCursorRects();
|
||||
window.resetCursorRects();
|
||||
// egui-winit may skip an unchanged fallback icon. Rebuild its stored rectangle anyway.
|
||||
if let Some(view) = window.contentView() {
|
||||
window.invalidateCursorRectsForView(&view);
|
||||
}
|
||||
}
|
||||
fn build(&mut self, request: &Request) -> Option<Self::Cursor> {
|
||||
build(&request.bitmap)
|
||||
}
|
||||
fn current(&self) -> Option<Self::Cursor> {
|
||||
Some(NSCursor::currentCursor())
|
||||
}
|
||||
fn set(&mut self, cursor: &Self::Cursor) {
|
||||
cursor.set();
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! { static STATE: RefCell<State<AppKit>> = RefCell::new(State::new(AppKit::default())); }
|
||||
|
||||
fn with_state(f: impl FnOnce(&mut State<AppKit>)) {
|
||||
// All production AppKit access, including restoration, stays on the main thread.
|
||||
if MainThreadMarker::new().is_some() {
|
||||
STATE.with(|state| {
|
||||
if let Ok(mut state) = state.try_borrow_mut() {
|
||||
f(&mut state);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Also called synchronously when native menu tracking begins and before process teardown.
|
||||
pub(crate) fn release() {
|
||||
with_state(State::release);
|
||||
}
|
||||
|
||||
struct EndedPass {
|
||||
viewport: ViewportId,
|
||||
discarded: bool,
|
||||
request: Option<Request>,
|
||||
}
|
||||
|
||||
struct Delivery {
|
||||
begin: fn(),
|
||||
finish: fn(Option<Request>),
|
||||
release: fn(),
|
||||
tracking: fn() -> bool,
|
||||
ended: Vec<EndedPass>,
|
||||
}
|
||||
|
||||
impl Plugin for Delivery {
|
||||
fn debug_name(&self) -> &'static str {
|
||||
"macOS canvas cursor"
|
||||
}
|
||||
fn on_begin_pass(&mut self, ui: &mut egui::Ui) {
|
||||
native_request::clear(ui.ctx());
|
||||
// Floating panels must not acquire or release the main canvas's window.
|
||||
if ui.ctx().viewport_id() == ViewportId::ROOT {
|
||||
(self.begin)();
|
||||
}
|
||||
}
|
||||
fn on_end_pass(&mut self, ui: &mut egui::Ui) {
|
||||
// egui restores the parent viewport before output_hook. Capture this pass here so
|
||||
// a floating panel's output cannot consume or release its parent's cursor.
|
||||
self.ended.push(EndedPass { viewport: ui.ctx().viewport_id(), discarded: ui.ctx().will_discard(), request: native_request::take(ui.ctx()) });
|
||||
}
|
||||
fn input_hook(&mut self, _: &Context, input: &mut egui::RawInput) {
|
||||
if input.viewport_id == ViewportId::ROOT
|
||||
&& (!input.focused || input.viewports.get(&ViewportId::ROOT).is_some_and(|v| v.minimized == Some(true)))
|
||||
{
|
||||
(self.release)();
|
||||
}
|
||||
}
|
||||
fn output_hook(&mut self, ctx: &Context, output: &mut FullOutput) {
|
||||
let Some(ended) = self.ended.pop() else { return };
|
||||
if ended.viewport != ViewportId::ROOT {
|
||||
return;
|
||||
}
|
||||
if ended.discarded
|
||||
|| (output.platform_output.requested_discard() && output.platform_output.num_completed_passes < ctx.options(|o| o.max_passes.get()))
|
||||
{
|
||||
return;
|
||||
}
|
||||
let request = ended.request.filter(|r| {
|
||||
r.viewport == ended.viewport
|
||||
&& r.pass.checked_add(1) == Some(ctx.cumulative_pass_nr_for(ended.viewport))
|
||||
&& ctx.input(|i| i.focused && i.pointer.hover_pos().is_some())
|
||||
&& output.platform_output.cursor_icon == r.fallback
|
||||
&& output.platform_output.cursor_image.is_none()
|
||||
&& !(self.tracking)()
|
||||
});
|
||||
// This is BEFORE egui-winit's platform output. In pinned winit, fallback changes update
|
||||
// the view's stored cursor and invalidate rectangles. Our owned suspension is intended
|
||||
// to keep that update from replacing the native cursor; native timing needs hand testing.
|
||||
(self.finish)(request);
|
||||
}
|
||||
fn on_exit(&mut self, _: &Context) {
|
||||
(self.release)();
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn install(ctx: &Context, window: Option<&std::sync::Arc<winit::window::Window>>) {
|
||||
with_state(|state| state.backend.root = window.map(std::sync::Arc::downgrade).unwrap_or_default());
|
||||
ctx.add_plugin(Delivery {
|
||||
begin: || with_state(State::begin),
|
||||
finish: |request| with_state(|state| state.finish(request)),
|
||||
release,
|
||||
tracking: super::mac_menu::is_tracking,
|
||||
ended: Vec::new(),
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,236 @@
|
||||
use super::*;
|
||||
use egui::{CursorIcon, RawInput};
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Fake {
|
||||
window: Option<u8>,
|
||||
suspended: Vec<u8>,
|
||||
current: Option<Key>,
|
||||
sets: usize,
|
||||
builds: usize,
|
||||
restores: Vec<u8>,
|
||||
fail: bool,
|
||||
}
|
||||
impl Backend for Fake {
|
||||
type Window = u8;
|
||||
type Cursor = Key;
|
||||
fn window(&self) -> Option<u8> {
|
||||
self.window
|
||||
}
|
||||
fn enabled(&self, w: &u8) -> bool {
|
||||
!self.suspended.contains(w)
|
||||
}
|
||||
fn suspend(&mut self, w: &u8) {
|
||||
self.suspended.push(*w);
|
||||
}
|
||||
fn restore(&mut self, w: &u8) {
|
||||
self.suspended.retain(|v| v != w);
|
||||
self.restores.push(*w);
|
||||
self.current = None;
|
||||
}
|
||||
fn build(&mut self, r: &Request) -> Option<Key> {
|
||||
self.builds += 1;
|
||||
(!self.fail).then_some(r.key)
|
||||
}
|
||||
fn current(&self) -> Option<Key> {
|
||||
self.current
|
||||
}
|
||||
fn set(&mut self, cursor: &Key) {
|
||||
self.current = Some(*cursor);
|
||||
self.sets += 1;
|
||||
}
|
||||
}
|
||||
fn fake_state() -> State<Fake> {
|
||||
State::new(Fake { window: Some(1), ..Default::default() })
|
||||
}
|
||||
fn raw(id: ViewportId, focused: bool) -> RawInput {
|
||||
let mut input = RawInput { viewport_id: id, focused, events: vec![egui::Event::PointerMoved(egui::pos2(10.0, 10.0))], ..Default::default() };
|
||||
input.viewports.entry(id).or_default();
|
||||
input
|
||||
}
|
||||
fn publish(ctx: &Context, scale: f32) {
|
||||
let mut session = vectorcraft_engine::Session::new();
|
||||
let cursor = session.cursor(Default::default(), Default::default(), Default::default());
|
||||
let fallback = native_request::request(ctx, cursor, CursorIcon::Default, true, scale);
|
||||
ctx.set_cursor_icon(fallback);
|
||||
}
|
||||
fn request(scale: f32) -> Request {
|
||||
let ctx = Context::default();
|
||||
let _ = headless(&ctx, raw(ViewportId::ROOT, true), |ui| publish(ui.ctx(), scale));
|
||||
native_request::take(&ctx).unwrap()
|
||||
}
|
||||
fn deliver(state: &mut State<Fake>, request: Option<Request>) {
|
||||
state.begin();
|
||||
state.finish(request);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mac_cursor_ownership_deduplicates_reasserts_and_restores() {
|
||||
let req = request(1.0);
|
||||
let mut state = fake_state();
|
||||
deliver(&mut state, Some(req.clone()));
|
||||
assert_eq!(state.backend.suspended, [1]);
|
||||
assert_eq!((state.backend.builds, state.backend.sets), (1, 1));
|
||||
deliver(&mut state, Some(req.clone()));
|
||||
assert_eq!(state.backend.sets, 1);
|
||||
// Simulate a platform fallback replacing the cursor after finalization. The next pass
|
||||
// reasserts; this proves the protocol, not AppKit timing or absence of transient flicker.
|
||||
state.backend.current = None;
|
||||
deliver(&mut state, Some(req.clone()));
|
||||
assert_eq!(state.backend.sets, 2);
|
||||
deliver(&mut state, None);
|
||||
assert_eq!(state.backend.restores, [1]);
|
||||
deliver(&mut state, Some(req.clone()));
|
||||
state.backend.window = None;
|
||||
deliver(&mut state, Some(req.clone()));
|
||||
assert!(state.active.is_none());
|
||||
state.backend.window = Some(1);
|
||||
deliver(&mut state, Some(req.clone()));
|
||||
state.begin();
|
||||
state.backend.window = Some(2);
|
||||
state.finish(Some(req));
|
||||
assert!(state.active.is_none(), "never acquire a different key window mid-pass");
|
||||
assert_eq!(state.backend.restores, [1, 1, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mac_cursor_respects_other_suspensions_failures_and_cache_eviction() {
|
||||
let mut state = fake_state();
|
||||
state.backend.suspended.push(1);
|
||||
deliver(&mut state, Some(request(1.0)));
|
||||
assert_eq!(state.backend.builds, 0);
|
||||
state.release();
|
||||
assert_eq!(state.backend.suspended, [1]);
|
||||
assert!(state.backend.restores.is_empty());
|
||||
state.backend.suspended.clear();
|
||||
state.backend.fail = true;
|
||||
for _ in 0..2 {
|
||||
deliver(&mut state, Some(request(1.0)));
|
||||
}
|
||||
assert_eq!(state.backend.builds, 1, "cache failed constructions");
|
||||
state.backend.fail = false;
|
||||
for i in 1..140 {
|
||||
deliver(&mut state, Some(request(1.0 + i as f32 / 1000.0)));
|
||||
}
|
||||
assert!(state.cache.len() <= CAPACITY);
|
||||
let active = state.active.unwrap();
|
||||
state.cache.clear();
|
||||
assert_eq!(state.active, Some(active));
|
||||
state.release();
|
||||
assert!(state.backend.suspended.is_empty());
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static FAKE: RefCell<State<Fake>> = RefCell::new(fake_state());
|
||||
static TRACKING: Cell<bool> = const { Cell::new(false) };
|
||||
}
|
||||
fn install_fake(ctx: &Context) {
|
||||
ctx.add_plugin(Delivery {
|
||||
begin: || FAKE.with(|s| s.borrow_mut().begin()),
|
||||
finish: |r| FAKE.with(|s| s.borrow_mut().finish(r)),
|
||||
release: || FAKE.with(|s| s.borrow_mut().release()),
|
||||
tracking: || TRACKING.with(Cell::get),
|
||||
ended: Vec::new(),
|
||||
});
|
||||
}
|
||||
#[test]
|
||||
fn mac_cursor_plugin_consumes_passes_and_releases_for_menu_focus_and_overrides() {
|
||||
FAKE.with(|s| *s.borrow_mut() = fake_state());
|
||||
TRACKING.with(|t| t.set(false));
|
||||
let ctx = Context::default();
|
||||
install_fake(&ctx);
|
||||
install_fake(&ctx);
|
||||
let root = ViewportId::ROOT;
|
||||
let _ = headless(&ctx, raw(root, true), |ui| publish(ui.ctx(), 1.0));
|
||||
FAKE.with(|s| assert_eq!(s.borrow().backend.sets, 1, "one installed plugin consumes the request"));
|
||||
let _ = headless(&ctx, raw(ViewportId::from_hash_of("floating-panel"), true), |_| {});
|
||||
FAKE.with(|s| assert!(s.borrow().active.is_some()));
|
||||
let _ = headless(&ctx, raw(root, true), |_| {});
|
||||
FAKE.with(|s| assert!(s.borrow().active.is_none(), "empty pass hands back even an unchanged Default icon"));
|
||||
let _ = headless(&ctx, raw(root, true), |ui| publish(ui.ctx(), 1.0));
|
||||
let _ = headless(&ctx, raw(root, true), |ui| {
|
||||
publish(ui.ctx(), 1.0);
|
||||
ui.ctx().set_cursor_icon(CursorIcon::Text);
|
||||
});
|
||||
FAKE.with(|s| assert!(s.borrow().active.is_none(), "changed-icon handback"));
|
||||
let _ = headless(&ctx, raw(root, true), |ui| publish(ui.ctx(), 1.0));
|
||||
// The notification calls release without waiting for another UI pass.
|
||||
TRACKING.with(|t| t.set(true));
|
||||
FAKE.with(|s| s.borrow_mut().release());
|
||||
FAKE.with(|s| assert!(s.borrow().active.is_none()));
|
||||
let _ = headless(&ctx, raw(root, true), |ui| publish(ui.ctx(), 1.0));
|
||||
FAKE.with(|s| assert!(s.borrow().active.is_none()));
|
||||
TRACKING.with(|t| t.set(false));
|
||||
let _ = headless(&ctx, raw(root, true), |ui| publish(ui.ctx(), 1.0));
|
||||
let _ = headless(&ctx, raw(root, false), |_| {});
|
||||
FAKE.with(|s| assert!(s.borrow().active.is_none()));
|
||||
let _ = headless(&ctx, raw(root, true), |ui| publish(ui.ctx(), 1.0));
|
||||
FAKE.with(|s| assert!(s.borrow().active.is_some()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mac_cursor_discarded_pass_does_not_commit_and_installation_does_not_repaint() {
|
||||
FAKE.with(|s| *s.borrow_mut() = fake_state());
|
||||
TRACKING.with(|t| t.set(false));
|
||||
let ctx = Context::default();
|
||||
install_fake(&ctx);
|
||||
let mut passes = 0;
|
||||
let _ = headless(&ctx, raw(ViewportId::ROOT, true), |ui| {
|
||||
passes += 1;
|
||||
if passes == 1 {
|
||||
publish(ui.ctx(), 1.0);
|
||||
ui.ctx().request_discard("cursor regression");
|
||||
}
|
||||
});
|
||||
assert_eq!(passes, 2);
|
||||
FAKE.with(|s| assert_eq!(s.borrow().backend.sets, 0));
|
||||
let idle = || {
|
||||
let mut input = raw(ViewportId::ROOT, true);
|
||||
input.events.clear();
|
||||
input.time = Some(ctx.input(|i| i.time) + 0.1);
|
||||
input
|
||||
};
|
||||
for _ in 0..3 {
|
||||
let _ = headless(&ctx, idle(), |ui| publish(ui.ctx(), 1.0));
|
||||
}
|
||||
let out = headless(&ctx, idle(), |ui| publish(ui.ctx(), 1.0));
|
||||
assert!(out.viewport_output[&ViewportId::ROOT].repaint_delay > std::time::Duration::ZERO);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mac_cursor_appkit_representations_have_pixel_sizes_and_point_hotspots() {
|
||||
// Only construct and inspect image/cursor objects: no NSApplication, window or cursor.set().
|
||||
for scale in [0.75, 1.0, 1.25, 1.5, 2.0] {
|
||||
let request = request(scale);
|
||||
let cursor = build(&request.bitmap).unwrap();
|
||||
let image = cursor.image();
|
||||
let one = &request.bitmap.representations[0];
|
||||
let size = NSSize::new(f64::from(one.size[0]), f64::from(one.size[1]));
|
||||
assert_eq!(image.size(), size);
|
||||
assert_eq!(cursor.hotSpot(), NSPoint::new(f64::from(one.hotspot[0]), f64::from(one.hotspot[1])));
|
||||
let reps = image.representations();
|
||||
assert_eq!(reps.len(), 2);
|
||||
let mut dimensions: Vec<_> = reps
|
||||
.iter()
|
||||
.map(|rep| {
|
||||
assert_eq!(rep.size(), size);
|
||||
(rep.pixelsWide(), rep.pixelsHigh())
|
||||
})
|
||||
.collect();
|
||||
dimensions.sort_unstable();
|
||||
assert_eq!(
|
||||
dimensions,
|
||||
[
|
||||
(isize::try_from(one.size[0]).unwrap(), isize::try_from(one.size[1]).unwrap()),
|
||||
(isize::try_from(one.size[0]).unwrap() * 2, isize::try_from(one.size[1]).unwrap() * 2)
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn headless(ctx: &egui::Context, input: egui::RawInput, draw: impl FnMut(&mut egui::Ui)) -> egui::FullOutput {
|
||||
let mut output = ctx.run_ui(input, draw);
|
||||
output.textures_delta.clear();
|
||||
output
|
||||
}
|
||||
@@ -31,6 +31,10 @@ use vectorcraft_ui_egui::native_menu::{self, Backend, Event, MenuBar, MenuRole,
|
||||
// asynchronous menu presentation). Keep the native tracking state separate from egui popups.
|
||||
static TRACKING: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
pub(crate) fn is_tracking() -> bool {
|
||||
TRACKING.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
fn observe_tracking(ctx: &egui::Context) {
|
||||
static INSTALL: Once = Once::new();
|
||||
INSTALL.call_once(|| {
|
||||
@@ -41,13 +45,17 @@ fn observe_tracking(ctx: &egui::Context) {
|
||||
let repaint = ctx.clone();
|
||||
let block = RcBlock::new(move |_: std::ptr::NonNull<NSNotification>| {
|
||||
TRACKING.store(tracking, Ordering::Relaxed);
|
||||
if tracking {
|
||||
super::mac_cursor::release();
|
||||
}
|
||||
// Apply any deferred menu updates as soon as the tracking session ends.
|
||||
if !tracking {
|
||||
repaint.request_repaint();
|
||||
}
|
||||
});
|
||||
// SAFETY: these are AppKit's notification names. The notification center retains
|
||||
// the observer and block for the application's lifetime; callbacks update an atomic and request repaint.
|
||||
// the observer and block for the application's lifetime; callbacks update tracking, restore
|
||||
// owned cursor rectangles and request repaint.
|
||||
#[allow(unsafe_code)]
|
||||
unsafe {
|
||||
NSNotificationCenter::defaultCenter().addObserverForName_object_queue_usingBlock(Some(name), None, None, &block);
|
||||
|
||||
@@ -36,6 +36,8 @@ mod logging;
|
||||
#[cfg(all(target_os = "macos", feature = "wgpu"))]
|
||||
mod mac_color_space;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod mac_cursor;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod mac_fonts;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod mac_menu;
|
||||
@@ -97,7 +99,7 @@ use titlebar::opted_into_system_title_bar;
|
||||
#[cfg(windows)]
|
||||
use titlebar::set_taskbar_icon;
|
||||
#[cfg(all(test, feature = "wgpu"))]
|
||||
use titlebar::{app_draws_frame, opted_into_app_title_bar, start_frame};
|
||||
use titlebar::{app_draws_frame, opted_into_app_title_bar, start_frame, system_title_bar_applies};
|
||||
use titlebar::{custom_titlebar, native_options, titlebar_from_env};
|
||||
|
||||
struct App {
|
||||
@@ -454,11 +456,15 @@ fn main() -> std::process::ExitCode {
|
||||
let mut session = Session::new();
|
||||
// The profiles loaded before come back before the preferences name any of them.
|
||||
prefs_dir::attach_profile_store(&mut session);
|
||||
// Then Edit › Color Settings, which may name one of those profiles.
|
||||
prefs_dir::attach_color_settings(&mut session);
|
||||
// Before any plug-in loads: this launch's marker in the Launch folder next to the
|
||||
// preferences (none for runs without preferences), and Safe Mode.
|
||||
let launch_dir = prefs_path().filter(|_| prefs_enabled()).and_then(|p| Some(p.parent()?.join("Launch")));
|
||||
let stopped = safe_mode::begin_launch(launch_dir.as_deref(), safe_mode_flag);
|
||||
prefs_dir::attach_plugin_store(&mut session);
|
||||
// File › Scripts lists the scripts of the Scripts folder next to the preferences.
|
||||
prefs_dir::attach_scripts_dir(&mut session);
|
||||
let mut app = VectorcraftApp::new(session, services(cc.winit_window().cloned(), cc.egui_ctx.clone()));
|
||||
app.set_startup_update_check(true);
|
||||
load_prefs(&mut app, saved);
|
||||
@@ -466,6 +472,8 @@ fn main() -> std::process::ExitCode {
|
||||
// The saved PDF presets live in their own folder (also read by vectorcraft-cli),
|
||||
// which takes over from the copy the preferences file used to hold.
|
||||
prefs_dir::attach_pdf_preset_store(&mut app.session);
|
||||
// The custom dictionary's words, kept in their own file next to the preferences.
|
||||
prefs_dir::attach_custom_dictionary(&mut app.session);
|
||||
// Fit the window to its monitor, or put it back where it was (still hidden).
|
||||
let winit_window = cc.winit_window().cloned();
|
||||
if let Some(w) = &winit_window {
|
||||
@@ -549,6 +557,7 @@ fn main() -> std::process::ExitCode {
|
||||
if !in_window_menus {
|
||||
app.services.native_menu = mac_menu::install(&cc.egui_ctx, &app);
|
||||
}
|
||||
mac_cursor::install(&cc.egui_ctx, winit_window.as_ref());
|
||||
}
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
|
||||
@@ -41,6 +41,14 @@ pub fn attach_profile_store(session: &mut vectorcraft_engine::Session) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Make Edit › Color Settings kept in `Color Settings.json` the active ones (#6335), and keep them
|
||||
/// there when they change. After [`attach_profile_store`]: they may name a loaded profile.
|
||||
pub fn attach_color_settings(session: &mut vectorcraft_engine::Session) {
|
||||
if let Some(path) = store_dir("Color Settings.json") {
|
||||
session.attach_color_settings(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// Install the plug-ins Install Plug-in kept in the `Plug-ins` folder (#2541), and keep the ones
|
||||
/// installed from now on there.
|
||||
pub fn attach_plugin_store(session: &mut vectorcraft_engine::Session) {
|
||||
@@ -49,6 +57,14 @@ pub fn attach_plugin_store(session: &mut vectorcraft_engine::Session) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Have File › Scripts and `script.listInstalled` list the scripts of the `Scripts` folder. The
|
||||
/// folder is only read: the user makes it.
|
||||
pub fn attach_scripts_dir(session: &mut vectorcraft_engine::Session) {
|
||||
if let Some(dir) = store_dir("Scripts") {
|
||||
session.attach_scripts_dir(dir);
|
||||
}
|
||||
}
|
||||
|
||||
/// Take the saved PDF presets from the `PDF Presets` folder (#2168), and keep the ones saved from
|
||||
/// now on there. After the preferences were restored: the folder is the one place the presets live.
|
||||
pub fn attach_pdf_preset_store(session: &mut vectorcraft_engine::Session) {
|
||||
@@ -57,6 +73,14 @@ pub fn attach_pdf_preset_store(session: &mut vectorcraft_engine::Session) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep the custom dictionary (Edit › Spelling › Add, Edit Custom Dictionary) in the
|
||||
/// `Custom Dictionary.txt` file next to the settings, one word per line, and take in its words.
|
||||
pub fn attach_custom_dictionary(session: &mut vectorcraft_engine::Session) {
|
||||
if let Some(path) = store_dir("Custom Dictionary.txt") {
|
||||
session.attach_custom_dictionary(path);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -193,7 +193,8 @@ fn the_system_title_bar_preference_keeps_the_window_decorations() {
|
||||
|
||||
/// Preferences › User Interface › VectorCraft Title Bar on Every Desktop gives the app frame where
|
||||
/// the probe chose the desktop's (KDE, X11, a window opened through XWayland); without it the probe
|
||||
/// decides, and `VECTORCRAFT_TITLEBAR` takes precedence over both.
|
||||
/// decides, and `VECTORCRAFT_TITLEBAR` set to `app`, `system` or `desktop` takes precedence over
|
||||
/// both.
|
||||
#[test]
|
||||
fn the_app_title_bar_preference_gives_the_app_frame_on_desktop_frames() {
|
||||
assert!(!super::opted_into_app_title_bar(None), "no preferences: the default");
|
||||
@@ -245,6 +246,79 @@ fn the_start_frame_applies_the_app_title_bar_preference() {
|
||||
assert!(!super::custom_titlebar_from(&both, true), "System Title Bar takes precedence over the app title bar preference");
|
||||
}
|
||||
|
||||
/// `VECTORCRAFT_TITLEBAR` chooses the frame for the run it is set in, with System Title Bar off and
|
||||
/// on. The saved preference still decides while the variable is unset.
|
||||
#[test]
|
||||
fn the_titlebar_variable_takes_precedence_over_system_title_bar() {
|
||||
use craft_window::Frame;
|
||||
assert!(super::system_title_bar_applies(true, false));
|
||||
assert!(!super::system_title_bar_applies(true, true));
|
||||
assert!(!super::system_title_bar_applies(false, false));
|
||||
assert!(!super::system_title_bar_applies(false, true));
|
||||
// VECTORCRAFT_TITLEBAR=app with System Title Bar on: VectorCraft draws its own title bar.
|
||||
let frame = super::start_frame(Frame::Desktop, Some(true), false, false);
|
||||
assert_eq!(super::custom_titlebar_from(&frame, super::system_title_bar_applies(true, true)), !cfg!(target_os = "macos"));
|
||||
// VECTORCRAFT_TITLEBAR=desktop with System Title Bar off keeps the desktop's frame.
|
||||
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
||||
{
|
||||
let frame = super::start_frame(Frame::App(craft_window::Layout::default()), Some(false), false, false);
|
||||
assert!(!super::custom_titlebar_from(&frame, super::system_title_bar_applies(false, true)));
|
||||
}
|
||||
}
|
||||
|
||||
/// `VECTORCRAFT_TITLEBAR` read from the environment at startup, with System Title Bar off and on.
|
||||
/// The variable is process-wide and read once, so each value runs in a separate test process.
|
||||
#[test]
|
||||
fn the_titlebar_variable_decides_the_frame_before_the_window_opens() {
|
||||
const CHILD: &str = "VECTORCRAFT_TEST_TITLEBAR_CHILD";
|
||||
if std::env::var_os(CHILD).is_none() {
|
||||
for value in ["app", "system", "desktop", "unknown"] {
|
||||
// Linux and the BSDs: hold `spawning` while the child starts, so it does not keep a
|
||||
// copy of a GPU start test's locked start file (see `CHILD_SPAWN`).
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))]
|
||||
let spawning = super::spawning();
|
||||
let child = std::process::Command::new(std::env::current_exe().unwrap())
|
||||
.args(["--exact", "tests::the_titlebar_variable_decides_the_frame_before_the_window_opens", "--nocapture"])
|
||||
.env(CHILD, "1")
|
||||
.env("VECTORCRAFT_TITLEBAR", value)
|
||||
.stdin(std::process::Stdio::null())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.spawn();
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))]
|
||||
drop(spawning);
|
||||
let output = child.unwrap().wait_with_output().unwrap();
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"VECTORCRAFT_TITLEBAR={value}\n{}\n{}",
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let value = std::env::var("VECTORCRAFT_TITLEBAR").unwrap();
|
||||
let not_read = cfg!(any(target_os = "macos", target_os = "windows"));
|
||||
assert_eq!(super::titlebar_from_env(), !not_read && value != "unknown", "VECTORCRAFT_TITLEBAR={value}");
|
||||
let off = ui_state_with_prefs(serde_json::json!({"systemTitleBar": false}));
|
||||
let on = ui_state_with_prefs(serde_json::json!({"systemTitleBar": true}));
|
||||
for (saved, opted_into_system) in [(None, false), (Some(&off), false), (Some(&on), true)] {
|
||||
let custom = super::custom_titlebar(saved);
|
||||
let expected = match value.as_str() {
|
||||
_ if not_read => !cfg!(target_os = "macos") && !opted_into_system,
|
||||
"app" => true,
|
||||
"system" | "desktop" => false,
|
||||
// Not a value the variable takes: System Title Bar decides when it is on. Otherwise the
|
||||
// `craft-window` probe decides, the same with System Title Bar off as with no saved
|
||||
// preferences.
|
||||
_ if opted_into_system => false,
|
||||
_ => super::custom_titlebar(None),
|
||||
};
|
||||
assert_eq!(custom, expected, "{value}, preference {opted_into_system}");
|
||||
assert_eq!(super::native_options(custom).viewport.decorations, Some(!custom), "{value}, preference {opted_into_system}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The file extensions the macOS bundle declares: its document types and its own exported type.
|
||||
fn plist_extensions(plist: &str) -> Vec<&str> {
|
||||
["<key>CFBundleTypeExtensions</key>", "<key>public.filename-extension</key>"]
|
||||
|
||||
@@ -41,7 +41,8 @@ pub(crate) const CUSTOM_TITLEBAR: bool = !cfg!(target_os = "macos");
|
||||
|
||||
/// Whether the user asked for the system title bar in Preferences › User Interface › System Title
|
||||
/// Bar. Read from the saved preferences before the window opens; a missing or unreadable value is
|
||||
/// `false` (the default). This preference forces the desktop/native frame back on.
|
||||
/// `false` (the default). This preference forces the desktop/native frame back on unless
|
||||
/// `VECTORCRAFT_TITLEBAR` is set to `app`, `system` or `desktop` ([`system_title_bar_applies`]).
|
||||
pub(crate) fn opted_into_system_title_bar(saved: Option<&vectorcraft_ui_egui::UiState>) -> bool {
|
||||
saved.map(|ui| ui.engine_prefs.get("systemTitleBar").and_then(serde_json::Value::as_bool).unwrap_or(false)).unwrap_or(false)
|
||||
}
|
||||
@@ -55,8 +56,8 @@ pub(crate) fn opted_into_app_title_bar(saved: Option<&vectorcraft_ui_egui::UiSta
|
||||
|
||||
/// Whether the app draws the frame on this start, before the System Title Bar preference is
|
||||
/// applied ([`custom_titlebar_from`]). `probed_app` is the `craft-window` probe's answer and
|
||||
/// `overridden` its `VECTORCRAFT_TITLEBAR` override. In order: the override decides when it is
|
||||
/// set; VectorCraft Title Bar on Every Desktop (`opted_into_app`) gives the app frame; a window
|
||||
/// `overridden` its `VECTORCRAFT_TITLEBAR` override. In order: the override decides when the
|
||||
/// variable is set to `app`, `system` or `desktop`;VectorCraft Title Bar on Every Desktop (`opted_into_app`) gives the app frame; a window
|
||||
/// that opens through XWayland (`forced_to_x11`) gets the desktop frame; otherwise the probe
|
||||
/// decides.
|
||||
pub(crate) fn app_draws_frame(probed_app: bool, overridden: Option<bool>, forced_to_x11: bool, opted_into_app: bool) -> bool {
|
||||
@@ -87,12 +88,19 @@ fn probed_choice() -> &'static craft_window::Choice {
|
||||
}
|
||||
|
||||
/// Whether `VECTORCRAFT_TITLEBAR` chose this window's frame (Linux and the BSDs), so Preferences ›
|
||||
/// User Interface › VectorCraft Title Bar on Every Desktop does not apply while it is set. Always
|
||||
/// `false` on macOS and Windows.
|
||||
/// User Interface › System Title Bar and VectorCraft Title Bar on Every Desktop do not apply while
|
||||
/// it is set to `app`, `system` or `desktop`. Always `false` on macOS and Windows.
|
||||
pub(crate) fn titlebar_from_env() -> bool {
|
||||
cfg!(not(any(target_os = "macos", target_os = "windows"))) && probed_choice().overridden().is_some()
|
||||
}
|
||||
|
||||
/// Whether System Title Bar decides this start: `opted_into_system` unless `VECTORCRAFT_TITLEBAR`
|
||||
/// is set to `app`, `system` or `desktop` (`from_env`), which chooses the frame for the run it is
|
||||
/// set in.
|
||||
pub(crate) fn system_title_bar_applies(opted_into_system: bool, from_env: bool) -> bool {
|
||||
opted_into_system && !from_env
|
||||
}
|
||||
|
||||
/// Whether this start draws its own title bar, given the frame the shared `craft-window` crate
|
||||
/// decided and the user's System Title Bar preference.
|
||||
///
|
||||
@@ -100,9 +108,10 @@ pub(crate) fn titlebar_from_env() -> bool {
|
||||
/// ([`craft_window::Frame::Desktop`]) on macOS, Windows, X11 and any Wayland compositor with
|
||||
/// server-side decorations (KDE, Sway, Hyprland, COSMIC); the app draws it
|
||||
/// ([`craft_window::Frame::App`]) only where the compositor leaves it to the app (GNOME's Mutter),
|
||||
/// or when `VECTORCRAFT_TITLEBAR` overrides the probe. VectorCraft draws its own title bar exactly
|
||||
/// or when `VECTORCRAFT_TITLEBAR` set to `app`, `system` or `desktop` overrides the probe. VectorCraft draws its own title bar exactly
|
||||
/// when `craft-window` says the app owns the frame **and** the user has not opted into the system
|
||||
/// title bar; the preference always forces the native frame back on.
|
||||
/// title bar; the preference forces the native frame back on unless `VECTORCRAFT_TITLEBAR` is set
|
||||
/// to one of those values ([`system_title_bar_applies`]).
|
||||
///
|
||||
/// macOS and Windows keep today's behavior and are decided by [`CUSTOM_TITLEBAR`] rather than the
|
||||
/// probe: `craft-window` returns `Frame::Desktop` for both (the OS always draws a frame), but
|
||||
@@ -124,16 +133,18 @@ pub(crate) fn custom_titlebar_from(_frame: &craft_window::Frame, opted_into_syst
|
||||
|
||||
/// Whether this start draws its own title bar. Probes the shared `craft-window` crate once (its
|
||||
/// override variable is `VECTORCRAFT_TITLEBAR`) and combines its decision with the saved System
|
||||
/// Title Bar preference. Call once at startup, before [`native_options`].
|
||||
/// Title Bar preference, which does not apply while the variable is set to `app`, `system` or
|
||||
/// `desktop`. Call once at startup,
|
||||
/// before [`native_options`].
|
||||
///
|
||||
/// On Linux, Preferences › Performance › Linux Display Server › X11 opens the window through
|
||||
/// XWayland (below). A GNOME/Wayland probe would say `Frame::App` there, but the window really runs
|
||||
/// on X11, whose window manager draws the frame — so when XWayland is being forced, keep the native
|
||||
/// frame, the same as a real X11 session. An explicit `VECTORCRAFT_TITLEBAR` override still wins,
|
||||
/// frame, the same as a real X11 session. A recognized `VECTORCRAFT_TITLEBAR` override still wins,
|
||||
/// and so does VectorCraft Title Bar on Every Desktop ([`app_draws_frame`]).
|
||||
pub(crate) fn custom_titlebar(saved: Option<&vectorcraft_ui_egui::UiState>) -> bool {
|
||||
let choice = probed_choice();
|
||||
let opted_into_system = opted_into_system_title_bar(saved);
|
||||
let opted_into_system = system_title_bar_applies(opted_into_system_title_bar(saved), titlebar_from_env());
|
||||
// macOS and Windows never read VectorCraft Title Bar on Every Desktop (`custom_titlebar_from`
|
||||
// ignores the frame there).
|
||||
let opted_into_app = cfg!(not(any(target_os = "macos", target_os = "windows"))) && opted_into_app_title_bar(saved);
|
||||
|
||||
@@ -220,3 +220,8 @@ real_name = "Sven Lafebre"
|
||||
[people.kylekanouse]
|
||||
real_name = "Kyle Kanouse"
|
||||
sync_github_name = true
|
||||
|
||||
[people.Hassan1305]
|
||||
real_name = "Hassan Nawaz Khan"
|
||||
display_name = "Hassan Nawaz Khan"
|
||||
sync_github_name = false
|
||||
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "vectorcraft-apischema"
|
||||
description = "Machine-readable parameter schemas for VectorCraft commands, with a validator and a params text writer"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde_json = { workspace = true }
|
||||
@@ -0,0 +1,174 @@
|
||||
//! For tests: a check of JSON Schema 2020-12 documents, and a validator of instances against them,
|
||||
//! written from the keyword rules of the Core and Validation specifications
|
||||
//! (<https://json-schema.org/draft/2020-12>). It reads only the JSON and shares no code with the
|
||||
//! writer it checks. It covers the keywords the writer uses and reports any other keyword that
|
||||
//! isn't an `x-` annotation.
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
const TYPES: &[&str] = &["null", "boolean", "object", "array", "number", "string", "integer"];
|
||||
|
||||
fn unique(items: &[Value]) -> bool {
|
||||
items.iter().enumerate().all(|(i, x)| items.iter().take(i).all(|y| y != x))
|
||||
}
|
||||
|
||||
fn schema_list(items: &Value, at: &str, key: &str) -> Result<(), String> {
|
||||
let list = items.as_array().filter(|l| !l.is_empty()).ok_or_else(|| format!("{at}: `{key}` must be a nonempty array"))?;
|
||||
for (i, x) in list.iter().enumerate() {
|
||||
check_schema(x, &format!("{at}/{key}/{i}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Whether `schema` follows the 2020-12 rules for the keywords it uses, with where it doesn't.
|
||||
pub fn check_schema(schema: &Value, at: &str) -> Result<(), String> {
|
||||
let object = match schema {
|
||||
Value::Bool(_) => return Ok(()),
|
||||
Value::Object(o) => o,
|
||||
other => return Err(format!("{at}: a schema must be an object or a boolean, not {other}")),
|
||||
};
|
||||
for (key, value) in object {
|
||||
let number = || if value.is_number() { Ok(()) } else { Err(format!("{at}: `{key}` must be a number")) };
|
||||
match key.as_str() {
|
||||
"$schema" => {
|
||||
let uri = value.as_str().ok_or_else(|| format!("{at}: `$schema` must be a string"))?;
|
||||
if !uri.contains("://") || uri.ends_with('#') {
|
||||
return Err(format!("{at}: `$schema` must be a normalized URI"));
|
||||
}
|
||||
}
|
||||
"type" => {
|
||||
let known = |v: &Value| v.as_str().is_some_and(|s| TYPES.contains(&s));
|
||||
let ok = known(value) || value.as_array().is_some_and(|l| !l.is_empty() && unique(l) && l.iter().all(known));
|
||||
if !ok {
|
||||
return Err(format!("{at}: `type` must name JSON types, not {value}"));
|
||||
}
|
||||
}
|
||||
"enum" => {
|
||||
if !value.as_array().is_some_and(|l| !l.is_empty() && unique(l)) {
|
||||
return Err(format!("{at}: `enum` must be a nonempty array of unique values"));
|
||||
}
|
||||
}
|
||||
"properties" => {
|
||||
let props = value.as_object().ok_or_else(|| format!("{at}: `properties` must be an object"))?;
|
||||
for (name, x) in props {
|
||||
check_schema(x, &format!("{at}/properties/{name}"))?;
|
||||
}
|
||||
}
|
||||
"required" => {
|
||||
if !value.as_array().is_some_and(|l| unique(l) && l.iter().all(Value::is_string)) {
|
||||
return Err(format!("{at}: `required` must be an array of unique strings"));
|
||||
}
|
||||
}
|
||||
"additionalProperties" | "items" => check_schema(value, &format!("{at}/{key}"))?,
|
||||
"prefixItems" | "anyOf" => schema_list(value, at, key)?,
|
||||
"minimum" | "maximum" | "exclusiveMinimum" | "exclusiveMaximum" => number()?,
|
||||
"minItems" => {
|
||||
if value.as_u64().is_none() {
|
||||
return Err(format!("{at}: `minItems` must be a non-negative integer"));
|
||||
}
|
||||
}
|
||||
"description" => {
|
||||
if !value.is_string() {
|
||||
return Err(format!("{at}: `description` must be a string"));
|
||||
}
|
||||
}
|
||||
"deprecated" => {
|
||||
if !value.is_boolean() {
|
||||
return Err(format!("{at}: `deprecated` must be a boolean"));
|
||||
}
|
||||
}
|
||||
"default" => {
|
||||
if !instance_valid(schema, value) {
|
||||
return Err(format!("{at}: `default` {value} does not validate against its schema"));
|
||||
}
|
||||
}
|
||||
k if k.starts_with("x-") => {}
|
||||
other => return Err(format!("{at}: unexpected keyword `{other}`")),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn has_type(v: &Value, name: &str) -> bool {
|
||||
match name {
|
||||
"null" => v.is_null(),
|
||||
"boolean" => v.is_boolean(),
|
||||
"object" => v.is_object(),
|
||||
"array" => v.is_array(),
|
||||
"string" => v.is_string(),
|
||||
"number" => v.is_number(),
|
||||
"integer" => v.is_i64() || v.is_u64() || v.as_f64().is_some_and(|x| x.fract() == 0.0),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the instance `v` is valid against `schema`.
|
||||
pub fn instance_valid(schema: &Value, v: &Value) -> bool {
|
||||
let s = match schema {
|
||||
Value::Bool(b) => return *b,
|
||||
Value::Object(o) => o,
|
||||
_ => return false,
|
||||
};
|
||||
let type_ok = match s.get("type") {
|
||||
None => true,
|
||||
Some(Value::String(t)) => has_type(v, t),
|
||||
Some(Value::Array(l)) => l.iter().filter_map(Value::as_str).any(|t| has_type(v, t)),
|
||||
Some(_) => false,
|
||||
};
|
||||
if !type_ok {
|
||||
return false;
|
||||
}
|
||||
if let Some(list) = s.get("enum").and_then(Value::as_array)
|
||||
&& !list.contains(v)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if let Some(any) = s.get("anyOf").and_then(Value::as_array)
|
||||
&& !any.iter().any(|x| instance_valid(x, v))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if let Some(x) = v.as_f64() {
|
||||
let bound = |key: &str| s.get(key).and_then(Value::as_f64);
|
||||
if bound("minimum").is_some_and(|m| x < m)
|
||||
|| bound("maximum").is_some_and(|m| x > m)
|
||||
|| bound("exclusiveMinimum").is_some_and(|m| x <= m)
|
||||
|| bound("exclusiveMaximum").is_some_and(|m| x >= m)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if let Some(items) = v.as_array() {
|
||||
if s.get("minItems").and_then(Value::as_u64).is_some_and(|n| (items.len() as u64) < n) {
|
||||
return false;
|
||||
}
|
||||
let prefix = s.get("prefixItems").and_then(Value::as_array).map_or(&[][..], Vec::as_slice);
|
||||
if items.iter().zip(prefix).any(|(x, p)| !instance_valid(p, x)) {
|
||||
return false;
|
||||
}
|
||||
if let Some(rest) = s.get("items")
|
||||
&& items.iter().skip(prefix.len()).any(|x| !instance_valid(rest, x))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if let Some(object) = v.as_object() {
|
||||
let required = s.get("required").and_then(Value::as_array).map_or(&[][..], Vec::as_slice);
|
||||
if required.iter().filter_map(Value::as_str).any(|k| !object.contains_key(k)) {
|
||||
return false;
|
||||
}
|
||||
let props = s.get("properties").and_then(Value::as_object);
|
||||
for (key, x) in object {
|
||||
match props.and_then(|p| p.get(key)) {
|
||||
Some(p) if !instance_valid(p, x) => return false,
|
||||
Some(_) => {}
|
||||
None => {
|
||||
if s.get("additionalProperties").is_some_and(|a| !instance_valid(a, x)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
@@ -0,0 +1,940 @@
|
||||
//! Machine-readable parameter schemas for VectorCraft commands. A [`ParamSchema`] lists what each
|
||||
//! parameter of a command is ([`Param`], [`Type`]): its type, whether it is required, what an
|
||||
//! omitted value means, whether null counts as omitted, and the API levels it was added and
|
||||
//! deprecated at. From one schema come:
|
||||
//!
|
||||
//! - [`ParamSchema::validate`], which checks a params object and names the parameter of the first
|
||||
//! problem it finds ([`ParamError`]), and fills in the defaults of omitted values;
|
||||
//! - [`ParamSchema::definition_problem`], which checks the schema itself;
|
||||
//! - [`ParamSchema::params_text`], the params text a command lists;
|
||||
//! - [`ParamSchema::to_json`], the schema as a JSON object in the shape of a JSON Schema 2020-12
|
||||
//! object schema (<https://json-schema.org/draft/2020-12>), with VectorCraft's own fields as
|
||||
//! `x-` keywords;
|
||||
//! - [`hash`], a hash of a set of command schemas, which changes whenever any of them changes.
|
||||
//!
|
||||
//! Checking a value walks the schema, so its depth is the schema's, at most [`MAX_DEPTH`]. An error
|
||||
//! shows a bounded part of a refused value and of a refused key, and the value is not written out
|
||||
//! first, so a huge or deeply nested value costs no more to refuse than a small one.
|
||||
//!
|
||||
//! The crate knows no command and depends on no other VectorCraft crate.
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use serde_json::{Map, Value, json};
|
||||
|
||||
/// The JSON Schema dialect [`ParamSchema::to_json`] names in `$schema`.
|
||||
pub const DIALECT: &str = "https://json-schema.org/draft/2020-12/schema";
|
||||
|
||||
/// How many objects and lists may nest inside a schema's params object.
|
||||
pub const MAX_DEPTH: usize = 16;
|
||||
|
||||
/// The largest object id [`Type::Id`] takes: 2^53 - 1, the largest integer every JSON reader holds
|
||||
/// exactly.
|
||||
pub const MAX_ID: u64 = (1 << 53) - 1;
|
||||
|
||||
/// How many characters of a refused value an error shows.
|
||||
const SHOWN_CHARS: usize = 40;
|
||||
|
||||
/// How many characters of a key an error shows.
|
||||
const KEY_CHARS: usize = 64;
|
||||
|
||||
/// One end of a number's range.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum Bound {
|
||||
/// The value may equal the bound.
|
||||
Inclusive(f64),
|
||||
/// The value must be past the bound.
|
||||
Exclusive(f64),
|
||||
}
|
||||
|
||||
/// The type of a parameter's value.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum Type {
|
||||
/// A JSON number in the range the bounds give, measured in `unit` (`"pt"`, `"deg"`) when set.
|
||||
Number { min: Option<Bound>, max: Option<Bound>, unit: Option<&'static str> },
|
||||
/// A JSON number with no fractional part, in the range given. [`ParamSchema::validate`] gives
|
||||
/// the command `2.0` as `2`.
|
||||
Integer { min: Option<i64>, max: Option<i64> },
|
||||
/// `true` or `false`.
|
||||
Boolean,
|
||||
/// Any string.
|
||||
String,
|
||||
/// One of the strings listed.
|
||||
Enum(&'static [&'static str]),
|
||||
/// An object id: an integer from 0 to [`MAX_ID`], read as [`Type::Integer`] reads one.
|
||||
Id,
|
||||
/// A list of object ids.
|
||||
Ids,
|
||||
/// A color: a string (`"#rrggbb"`, a color key), `[r, g, b]` or an object. The command reads
|
||||
/// which color it is.
|
||||
Color,
|
||||
/// A point: `[x, y]`, two numbers.
|
||||
Point,
|
||||
/// An object with the parameters listed and no other key.
|
||||
Object(&'static [Param]),
|
||||
/// A list of values of one type.
|
||||
Array(&'static Type),
|
||||
}
|
||||
|
||||
impl Type {
|
||||
/// A number with no range and no unit.
|
||||
pub const NUMBER: Type = Type::Number { min: None, max: None, unit: None };
|
||||
}
|
||||
|
||||
/// What an omitted value means.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum Omitted {
|
||||
/// The parameter is required, or the command describes the omitted case in its summary.
|
||||
Nothing,
|
||||
/// The value, as JSON text, that [`ParamSchema::validate`] fills in.
|
||||
Value(&'static str),
|
||||
/// A default the command works out itself (`"toggle"`, `"the selected text"`).
|
||||
Note(&'static str),
|
||||
}
|
||||
|
||||
/// What a command changes when it runs.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Writes {
|
||||
/// Nothing: the command only reads.
|
||||
Nothing,
|
||||
/// State of the running app outside the document and the preferences (view state, the Caps
|
||||
/// Lock state).
|
||||
Session,
|
||||
/// The preferences.
|
||||
Preferences,
|
||||
/// The document, with an undo step.
|
||||
Document,
|
||||
}
|
||||
|
||||
impl Writes {
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Writes::Nothing => "nothing",
|
||||
Writes::Session => "session",
|
||||
Writes::Preferences => "preferences",
|
||||
Writes::Document => "document",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One parameter of a command.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Param {
|
||||
pub name: &'static str,
|
||||
pub ty: Type,
|
||||
pub required: bool,
|
||||
pub omitted: Omitted,
|
||||
/// A null value is read as if the parameter were omitted. Otherwise null is refused as a value
|
||||
/// of the wrong type.
|
||||
pub null_omitted: bool,
|
||||
/// What the params text says about the value, in parentheses after its type.
|
||||
pub doc: &'static str,
|
||||
/// The API level the parameter was added at.
|
||||
pub since: u32,
|
||||
/// The API level the parameter was deprecated at.
|
||||
pub deprecated: Option<u32>,
|
||||
/// A value of the wrong type or out of range is not refused here: the command reads it as it
|
||||
/// did before it had a schema. [`ParamSchema::to_json`] still writes the documented type, so a
|
||||
/// JSON Schema validator refuses a value this passes.
|
||||
pub lenient: bool,
|
||||
}
|
||||
|
||||
impl Param {
|
||||
/// A required parameter, added at API level 1.
|
||||
pub const fn new(name: &'static str, ty: Type) -> Param {
|
||||
Param { name, ty, required: true, omitted: Omitted::Nothing, null_omitted: false, doc: "", since: 1, deprecated: None, lenient: false }
|
||||
}
|
||||
/// The parameter may be omitted.
|
||||
pub const fn optional(self) -> Param {
|
||||
Param { required: false, ..self }
|
||||
}
|
||||
/// The parameter may be omitted, and then takes the value `json` (JSON text).
|
||||
pub const fn defaults_to(self, json: &'static str) -> Param {
|
||||
Param { required: false, omitted: Omitted::Value(json), ..self }
|
||||
}
|
||||
/// The parameter may be omitted, and the command then works out the value `note` describes.
|
||||
pub const fn default_note(self, note: &'static str) -> Param {
|
||||
Param { required: false, omitted: Omitted::Note(note), ..self }
|
||||
}
|
||||
/// A null value reads as an omitted one.
|
||||
pub const fn null_omitted(self) -> Param {
|
||||
Param { null_omitted: true, ..self }
|
||||
}
|
||||
pub const fn doc(self, doc: &'static str) -> Param {
|
||||
Param { doc, ..self }
|
||||
}
|
||||
pub const fn since(self, level: u32) -> Param {
|
||||
Param { since: level, ..self }
|
||||
}
|
||||
pub const fn deprecated(self, level: u32) -> Param {
|
||||
Param { deprecated: Some(level), ..self }
|
||||
}
|
||||
pub const fn lenient(self) -> Param {
|
||||
Param { lenient: true, ..self }
|
||||
}
|
||||
}
|
||||
|
||||
/// The parameters of one command, and what the command does with them.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct ParamSchema {
|
||||
pub params: &'static [Param],
|
||||
/// What the command does, after the params object in the params text.
|
||||
pub summary: &'static str,
|
||||
/// What the command returns, after `→` in the params text; empty when the text leaves it out.
|
||||
pub returns: &'static str,
|
||||
/// The API level the command was added at.
|
||||
pub since: u32,
|
||||
/// The API level the command was deprecated at.
|
||||
pub deprecated: Option<u32>,
|
||||
/// What one run costs, in units of a light command (1), for callers that limit a script's work.
|
||||
pub cost: u32,
|
||||
pub writes: Writes,
|
||||
}
|
||||
|
||||
impl ParamSchema {
|
||||
/// A command added at API level 1, costing 1, that changes the document.
|
||||
pub const fn new(params: &'static [Param], summary: &'static str) -> ParamSchema {
|
||||
ParamSchema { params, summary, returns: "", since: 1, deprecated: None, cost: 1, writes: Writes::Document }
|
||||
}
|
||||
pub const fn returns(self, returns: &'static str) -> ParamSchema {
|
||||
ParamSchema { returns, ..self }
|
||||
}
|
||||
pub const fn writes(self, writes: Writes) -> ParamSchema {
|
||||
ParamSchema { writes, ..self }
|
||||
}
|
||||
pub const fn cost(self, cost: u32) -> ParamSchema {
|
||||
ParamSchema { cost, ..self }
|
||||
}
|
||||
pub const fn since(self, level: u32) -> ParamSchema {
|
||||
ParamSchema { since: level, ..self }
|
||||
}
|
||||
pub const fn deprecated(self, level: u32) -> ParamSchema {
|
||||
ParamSchema { deprecated: Some(level), ..self }
|
||||
}
|
||||
|
||||
/// The parameter named `name`.
|
||||
pub fn param(&self, name: &str) -> Option<&'static Param> {
|
||||
self.params.iter().find(|p| p.name == name)
|
||||
}
|
||||
|
||||
/// The names of the top-level parameters, in order.
|
||||
pub fn names(&self) -> Vec<&'static str> {
|
||||
self.params.iter().map(|p| p.name).collect()
|
||||
}
|
||||
|
||||
/// `params` checked against the schema: the first problem as an error naming the parameter,
|
||||
/// or, when it has none, `params` with the defaults of its omitted values filled in (`None`
|
||||
/// when no default was filled in, so `params` is used as it is). Null params read as `{}`.
|
||||
pub fn validate(&self, params: &Value) -> Result<Option<Value>, ParamError> {
|
||||
self.validate_as(params, false)
|
||||
}
|
||||
|
||||
/// [`ParamSchema::validate`] for saved steps recorded by this version or an older one: a key
|
||||
/// the schema doesn't name passes, at any depth, and the command does not read it. Values of
|
||||
/// the parameters the schema names are checked as `validate` checks them.
|
||||
pub fn validate_saved(&self, params: &Value) -> Result<Option<Value>, ParamError> {
|
||||
self.validate_as(params, true)
|
||||
}
|
||||
|
||||
fn validate_as(&self, params: &Value, saved: bool) -> Result<Option<Value>, ParamError> {
|
||||
static EMPTY: std::sync::OnceLock<Map<String, Value>> = std::sync::OnceLock::new();
|
||||
let given = match params {
|
||||
Value::Object(o) => o,
|
||||
Value::Null => EMPTY.get_or_init(Map::new),
|
||||
v => return Err(ParamError { path: String::new(), problem: Problem::NotAnObject { got: shown(v) } }),
|
||||
};
|
||||
Ok(check_object(&At::Root, 0, self.params, given, saved)?.map(Value::Object))
|
||||
}
|
||||
|
||||
/// What is wrong with the schema itself, or `None`: a parameter name that is empty, repeated
|
||||
/// or holds `.`, `[` or `]`; a required parameter with a default; a default that isn't JSON or
|
||||
/// doesn't pass its parameter's type; an enum with no names or a repeated name; a range with
|
||||
/// an end that isn't finite or that no value can be in; nesting past [`MAX_DEPTH`].
|
||||
pub fn definition_problem(&self) -> Option<String> {
|
||||
object_problem("", self.params, 0)
|
||||
}
|
||||
|
||||
/// The params text: the params object (`{name?: string, zoom: number > 0}`), then the summary,
|
||||
/// then `→` and what the command returns.
|
||||
pub fn params_text(&self) -> String {
|
||||
let mut s = object_text(self.params, 0);
|
||||
if !self.summary.is_empty() {
|
||||
s.push(' ');
|
||||
s.push_str(self.summary);
|
||||
}
|
||||
if !self.returns.is_empty() {
|
||||
s.push_str(" → ");
|
||||
s.push_str(self.returns);
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// The schema as a JSON Schema 2020-12 object schema. Defaults written as JSON go in
|
||||
/// `default`, a parameter that accepts null as omitted is an `anyOf` with `{"type": "null"}`,
|
||||
/// and VectorCraft's own fields are `x-since`, `x-deprecatedSince`, `x-lenient`, `x-unit`,
|
||||
/// `x-kind`, `x-cost`, `x-writes` and `x-returns`. The root `type` is `["object", "null"]` when
|
||||
/// no parameter is required, because null params read as `{}`, and `"object"` otherwise. A JSON
|
||||
/// Schema validator accepts the values [`ParamSchema::validate`] accepts, except that an
|
||||
/// `x-lenient` parameter's schema states only the documented type.
|
||||
pub fn to_json(&self) -> Value {
|
||||
let mut o = object_schema(self.params, 0);
|
||||
if !self.params.iter().any(|p| p.required) {
|
||||
o.insert("type".into(), json!(["object", "null"]));
|
||||
}
|
||||
o.insert("$schema".into(), DIALECT.into());
|
||||
if !self.summary.is_empty() {
|
||||
o.insert("description".into(), self.summary.into());
|
||||
}
|
||||
if !self.returns.is_empty() {
|
||||
o.insert("x-returns".into(), self.returns.into());
|
||||
}
|
||||
o.insert("x-since".into(), self.since.into());
|
||||
if let Some(level) = self.deprecated {
|
||||
o.insert("deprecated".into(), true.into());
|
||||
o.insert("x-deprecatedSince".into(), level.into());
|
||||
}
|
||||
o.insert("x-cost".into(), self.cost.into());
|
||||
o.insert("x-writes".into(), self.writes.name().into());
|
||||
Value::Object(o)
|
||||
}
|
||||
}
|
||||
|
||||
/// A problem [`ParamSchema::validate`] found, at `path`: the parameter's name, with the keys and
|
||||
/// indices leading to a value inside one (`edits[1].name`). The path is empty for the params
|
||||
/// object itself.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ParamError {
|
||||
pub path: String,
|
||||
pub problem: Problem,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Problem {
|
||||
/// The params are not an object.
|
||||
NotAnObject { got: String },
|
||||
/// The key at `path` is not a parameter of the object it is in, which takes `keys`.
|
||||
Unknown { keys: Vec<&'static str> },
|
||||
/// A required parameter is missing.
|
||||
Missing,
|
||||
/// The value is not of the parameter's type: `expected` says what it must be.
|
||||
WrongType { expected: String, got: String },
|
||||
/// The schema nests deeper than [`MAX_DEPTH`] here ([`ParamSchema::definition_problem`]).
|
||||
TooDeep,
|
||||
}
|
||||
|
||||
impl fmt::Display for ParamError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let path = &self.path;
|
||||
match &self.problem {
|
||||
Problem::NotAnObject { got } => write!(f, "the params must be an object, not {got}"),
|
||||
Problem::Unknown { keys } if keys.is_empty() => write!(f, "unknown parameter `{path}`; the object takes no keys"),
|
||||
Problem::Unknown { keys } => write!(f, "unknown parameter `{path}`; it takes: {}", keys.join(", ")),
|
||||
Problem::Missing => write!(f, "missing `{path}`"),
|
||||
Problem::WrongType { expected, got } => write!(f, "`{path}` must be {expected}, not {got}"),
|
||||
Problem::TooDeep => write!(f, "`{path}` is nested too deeply"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ParamError {}
|
||||
|
||||
/// FNV-1a over the bytes of `text`, from `h`.
|
||||
fn fnv1a(mut h: u64, text: &str) -> u64 {
|
||||
for b in text.bytes() {
|
||||
h ^= u64::from(b);
|
||||
h = h.wrapping_mul(0x0000_0100_0000_01b3);
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
/// `v` as JSON text with the keys of each object in sorted order, whichever order the map keeps
|
||||
/// them in.
|
||||
fn canonical(v: &Value, out: &mut String) {
|
||||
match v {
|
||||
Value::Object(o) => {
|
||||
let mut keys: Vec<&String> = o.keys().collect();
|
||||
keys.sort();
|
||||
out.push('{');
|
||||
for (i, k) in keys.into_iter().enumerate() {
|
||||
if i > 0 {
|
||||
out.push(',');
|
||||
}
|
||||
out.push_str(&Value::from(k.as_str()).to_string());
|
||||
out.push(':');
|
||||
if let Some(x) = o.get(k) {
|
||||
canonical(x, out);
|
||||
}
|
||||
}
|
||||
out.push('}');
|
||||
}
|
||||
Value::Array(a) => {
|
||||
out.push('[');
|
||||
for (i, x) in a.iter().enumerate() {
|
||||
if i > 0 {
|
||||
out.push(',');
|
||||
}
|
||||
canonical(x, out);
|
||||
}
|
||||
out.push(']');
|
||||
}
|
||||
other => out.push_str(&other.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// A 64-bit FNV-1a hash of the commands' schemas, taken in the order of their ids: each id and
|
||||
/// its [`ParamSchema::to_json`] text, with the keys of each object sorted. Any change to a schema,
|
||||
/// or a schema added or removed, changes it.
|
||||
pub fn hash<'a>(schemas: impl IntoIterator<Item = (&'a str, &'a ParamSchema)>) -> u64 {
|
||||
let mut all: Vec<(&str, &ParamSchema)> = schemas.into_iter().collect();
|
||||
all.sort_by_key(|(id, _)| *id);
|
||||
all.iter().fold(0xcbf2_9ce4_8422_2325, |h, (id, schema)| {
|
||||
let mut text = String::new();
|
||||
canonical(&schema.to_json(), &mut text);
|
||||
let h = fnv1a(h, id);
|
||||
let h = fnv1a(h, "\n");
|
||||
let h = fnv1a(h, &text);
|
||||
fnv1a(h, "\n")
|
||||
})
|
||||
}
|
||||
|
||||
/// `key` as an error message shows it: cut to [`KEY_CHARS`] characters.
|
||||
pub fn shown_key(key: &str) -> String {
|
||||
let mut chars = key.chars();
|
||||
let head: String = chars.by_ref().take(KEY_CHARS).collect();
|
||||
if chars.next().is_some() { format!("{head}…") } else { head }
|
||||
}
|
||||
|
||||
/// The JSON text of a value, stopping when its characters run out.
|
||||
struct Shown {
|
||||
text: String,
|
||||
left: usize,
|
||||
}
|
||||
|
||||
impl Shown {
|
||||
/// Adds `s`; false when it didn't fit.
|
||||
fn push(&mut self, s: &str) -> bool {
|
||||
for c in s.chars() {
|
||||
if self.left == 0 {
|
||||
return false;
|
||||
}
|
||||
self.left -= 1;
|
||||
self.text.push(c);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Adds the JSON text of string `s`, reading no more of it than can be shown.
|
||||
fn push_string(&mut self, s: &str) -> bool {
|
||||
let head: String = s.chars().take(self.left + 1).collect();
|
||||
self.push(&Value::from(head).to_string())
|
||||
}
|
||||
|
||||
/// Adds the JSON text of `v`; false when it didn't fit. Each object or list writes a bracket,
|
||||
/// so the characters left bound the recursion.
|
||||
fn push_value(&mut self, v: &Value) -> bool {
|
||||
match v {
|
||||
Value::String(s) => self.push_string(s),
|
||||
Value::Array(items) => {
|
||||
self.push("[") && items.iter().enumerate().all(|(i, x)| (i == 0 || self.push(",")) && self.push_value(x)) && self.push("]")
|
||||
}
|
||||
Value::Object(o) => {
|
||||
self.push("{")
|
||||
&& o.iter()
|
||||
.enumerate()
|
||||
.all(|(i, (k, x))| (i == 0 || self.push(",")) && self.push_string(k) && self.push(":") && self.push_value(x))
|
||||
&& self.push("}")
|
||||
}
|
||||
other => self.push(&other.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A value as an error message shows it: its JSON, cut to [`SHOWN_CHARS`] characters. Only what is
|
||||
/// shown is read, however large the value is.
|
||||
fn shown(v: &Value) -> String {
|
||||
let mut out = Shown { text: String::new(), left: SHOWN_CHARS };
|
||||
if !out.push_value(v) {
|
||||
out.text.push('…');
|
||||
}
|
||||
out.text
|
||||
}
|
||||
|
||||
fn join(path: &str, key: &str) -> String {
|
||||
if path.is_empty() { key.to_string() } else { format!("{path}.{key}") }
|
||||
}
|
||||
|
||||
/// Where a value is, from the value up to the params object. The path's text is written only for
|
||||
/// an error.
|
||||
#[derive(Clone, Copy)]
|
||||
enum At<'a> {
|
||||
Root,
|
||||
Key(&'a At<'a>, &'a str),
|
||||
Index(&'a At<'a>, usize),
|
||||
}
|
||||
|
||||
impl At<'_> {
|
||||
/// The path, `edits[1].name`, with each key as [`shown_key`] shows it.
|
||||
fn text(&self) -> String {
|
||||
let mut steps = Vec::new();
|
||||
let mut at = self;
|
||||
while let At::Key(up, _) | At::Index(up, _) = at {
|
||||
steps.push(at);
|
||||
at = up;
|
||||
}
|
||||
let mut out = String::new();
|
||||
for step in steps.into_iter().rev() {
|
||||
match step {
|
||||
At::Key(_, key) => {
|
||||
if !out.is_empty() {
|
||||
out.push('.');
|
||||
}
|
||||
out.push_str(&shown_key(key));
|
||||
}
|
||||
At::Index(_, i) => out.push_str(&format!("[{i}]")),
|
||||
At::Root => {}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `v` nests no deeper than [`MAX_DEPTH`] counted from `depth`. The walk stops at the
|
||||
/// limit, so it recurses at most [`MAX_DEPTH`] levels and allocates nothing.
|
||||
fn nests_within(v: &Value, depth: usize) -> bool {
|
||||
match v {
|
||||
Value::Array(a) => depth < MAX_DEPTH && a.iter().all(|x| nests_within(x, depth + 1)),
|
||||
Value::Object(o) => depth < MAX_DEPTH && o.values().all(|x| nests_within(x, depth + 1)),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// The object `given` checked against `params` (unknown keys first, then each parameter in
|
||||
/// order): with the defaults of omitted values filled in when there are any. `saved` keeps a key
|
||||
/// the schema doesn't name, unless its value nests past [`MAX_DEPTH`].
|
||||
fn check_object(
|
||||
at: &At<'_>,
|
||||
depth: usize,
|
||||
params: &'static [Param],
|
||||
given: &Map<String, Value>,
|
||||
saved: bool,
|
||||
) -> Result<Option<Map<String, Value>>, ParamError> {
|
||||
let refused = |key: &String, v: &Value| !params.iter().any(|p| p.name == key.as_str()) && (!saved || !nests_within(v, depth));
|
||||
if let Some((key, _)) = given.iter().find(|(k, v)| refused(k, v)) {
|
||||
return Err(ParamError { path: At::Key(at, key).text(), problem: Problem::Unknown { keys: params.iter().map(|p| p.name).collect() } });
|
||||
}
|
||||
// The changes are applied once every value has passed, so no refused value is ever copied.
|
||||
let mut changes: Vec<(&'static str, Value)> = Vec::new();
|
||||
for p in params {
|
||||
let here = At::Key(at, p.name);
|
||||
match given.get(p.name) {
|
||||
Some(Value::Null) if p.null_omitted => {}
|
||||
Some(v) => {
|
||||
if let Some(changed) = check_value(&here, depth, &p.ty, v, p.lenient, saved)? {
|
||||
changes.push((p.name, changed));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
None if p.required => return Err(ParamError { path: here.text(), problem: Problem::Missing }),
|
||||
None => {}
|
||||
}
|
||||
// Omitted, or null read as omitted.
|
||||
if let Omitted::Value(json) = p.omitted
|
||||
&& let Ok(v) = serde_json::from_str::<Value>(json)
|
||||
{
|
||||
changes.push((p.name, v));
|
||||
}
|
||||
}
|
||||
if changes.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut filled = given.clone();
|
||||
for (name, v) in changes {
|
||||
filled.insert(name.to_string(), v);
|
||||
}
|
||||
Ok(Some(filled))
|
||||
}
|
||||
|
||||
/// What a value of type `ty` must be, as an error message says it.
|
||||
fn expected(ty: &Type) -> String {
|
||||
match ty {
|
||||
Type::Number { min, max, unit } => {
|
||||
format!("a number{}{}", range_text(min.as_ref(), max.as_ref()), unit.map(|u| format!(" ({u})")).unwrap_or_default())
|
||||
}
|
||||
Type::Integer { min, max } => {
|
||||
format!("an integer{}", range_text(min.map(|m| Bound::Inclusive(m as f64)).as_ref(), max.map(|m| Bound::Inclusive(m as f64)).as_ref()))
|
||||
}
|
||||
Type::Boolean => "true or false".into(),
|
||||
Type::String => "a string".into(),
|
||||
Type::Enum(names) => format!("one of {}", names.iter().map(|n| format!("\"{n}\"")).collect::<Vec<_>>().join(", ")),
|
||||
Type::Id => "an object id (a non-negative integer)".into(),
|
||||
Type::Ids => "a list of object ids".into(),
|
||||
Type::Color => "a color (a string, [r, g, b] or an object)".into(),
|
||||
Type::Point => "[x, y] (two numbers)".into(),
|
||||
Type::Object(_) => "an object".into(),
|
||||
Type::Array(_) => "a list".into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// ` > 0`, ` 0 to 100`, ` >= 1 and < 10`: the range text after a number's type.
|
||||
fn range_text(min: Option<&Bound>, max: Option<&Bound>) -> String {
|
||||
match (min, max) {
|
||||
(Some(Bound::Inclusive(a)), Some(Bound::Inclusive(b))) => format!(" {a} to {b}"),
|
||||
(Some(a), Some(b)) => format!("{} and{}", low_text(a), high_text(b)),
|
||||
(Some(a), None) => low_text(a),
|
||||
(None, Some(b)) => high_text(b),
|
||||
(None, None) => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn low_text(b: &Bound) -> String {
|
||||
match b {
|
||||
Bound::Inclusive(a) => format!(" >= {a}"),
|
||||
Bound::Exclusive(a) => format!(" > {a}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn high_text(b: &Bound) -> String {
|
||||
match b {
|
||||
Bound::Inclusive(a) => format!(" <= {a}"),
|
||||
Bound::Exclusive(a) => format!(" < {a}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn in_range(x: f64, min: Option<&Bound>, max: Option<&Bound>) -> bool {
|
||||
let low = match min {
|
||||
Some(Bound::Inclusive(a)) => x >= *a,
|
||||
Some(Bound::Exclusive(a)) => x > *a,
|
||||
None => true,
|
||||
};
|
||||
let high = match max {
|
||||
Some(Bound::Inclusive(b)) => x <= *b,
|
||||
Some(Bound::Exclusive(b)) => x < *b,
|
||||
None => true,
|
||||
};
|
||||
low && high
|
||||
}
|
||||
|
||||
/// The integer a JSON number with no fractional part stands for, as an integer value when it fits
|
||||
/// in one (`2.0` is `2`); `None` for anything else.
|
||||
fn whole(v: &Value) -> Option<Value> {
|
||||
if v.is_i64() || v.is_u64() {
|
||||
return Some(v.clone());
|
||||
}
|
||||
const TWO_63: f64 = 9_223_372_036_854_775_808.0;
|
||||
const TWO_64: f64 = 18_446_744_073_709_551_616.0;
|
||||
let x = v.as_f64().filter(|x| x.fract() == 0.0)?;
|
||||
Some(if (-TWO_63..TWO_63).contains(&x) {
|
||||
json!(x as i64)
|
||||
} else if (0.0..TWO_64).contains(&x) {
|
||||
json!(x as u64)
|
||||
} else {
|
||||
v.clone()
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the [`whole`] value `w` is in the range.
|
||||
fn whole_in_range(w: &Value, min: Option<i64>, max: Option<i64>) -> bool {
|
||||
match (w.as_i64(), w.as_u64(), w.as_f64()) {
|
||||
(Some(x), _, _) => min.is_none_or(|m| x >= m) && max.is_none_or(|m| x <= m),
|
||||
// Past i64::MAX: above any i64 bound.
|
||||
(None, Some(_), _) => max.is_none(),
|
||||
// Past what u64 holds: above any i64 bound.
|
||||
(None, None, Some(x)) if x > 0.0 => max.is_none(),
|
||||
// Below what i64 holds: under any i64 bound, so it meets every maximum and no minimum.
|
||||
(None, None, Some(_)) => min.is_none(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_number(v: &Value) -> bool {
|
||||
v.as_f64().is_some_and(f64::is_finite)
|
||||
}
|
||||
|
||||
/// `v` checked against `ty` at `at`: with the defaults of omitted values inside it filled in, and
|
||||
/// whole numbers written with a fraction made integers, when there are any. A lenient value of
|
||||
/// the wrong type passes as it is, unless it nests past [`MAX_DEPTH`].
|
||||
fn check_value(at: &At<'_>, depth: usize, ty: &Type, v: &Value, lenient: bool, saved: bool) -> Result<Option<Value>, ParamError> {
|
||||
let done = |ok: bool| {
|
||||
if ok || (lenient && nests_within(v, depth)) {
|
||||
Ok(None)
|
||||
} else {
|
||||
Err(ParamError { path: at.text(), problem: Problem::WrongType { expected: expected(ty), got: shown(v) } })
|
||||
}
|
||||
};
|
||||
let too_deep = || ParamError { path: at.text(), problem: Problem::TooDeep };
|
||||
if matches!(ty, Type::Ids | Type::Color | Type::Point) && v.is_array() && depth >= MAX_DEPTH {
|
||||
return Err(too_deep());
|
||||
}
|
||||
match ty {
|
||||
Type::Number { min, max, .. } => done(v.as_f64().is_some_and(|x| x.is_finite() && in_range(x, min.as_ref(), max.as_ref()))),
|
||||
Type::Integer { min, max } => match whole(v).filter(|w| whole_in_range(w, *min, *max)) {
|
||||
Some(w) => Ok((w != *v).then_some(w)),
|
||||
None => done(false),
|
||||
},
|
||||
Type::Boolean => done(v.is_boolean()),
|
||||
Type::String => done(v.is_string()),
|
||||
Type::Enum(names) => done(v.as_str().is_some_and(|s| names.contains(&s))),
|
||||
Type::Id => match whole(v).filter(|w| w.as_u64().is_some_and(|x| x <= MAX_ID)) {
|
||||
Some(w) => Ok((w != *v).then_some(w)),
|
||||
None => done(false),
|
||||
},
|
||||
Type::Ids => match v.as_array() {
|
||||
Some(items) => check_items(at, depth, &Type::Id, items, lenient, saved),
|
||||
None => done(false),
|
||||
},
|
||||
Type::Color => done(match v {
|
||||
Value::String(_) => true,
|
||||
Value::Object(_) => nests_within(v, depth),
|
||||
Value::Array(a) => a.len() >= 3 && a.iter().all(is_number),
|
||||
_ => false,
|
||||
}),
|
||||
Type::Point => done(v.as_array().is_some_and(|a| a.len() == 2 && a.iter().all(is_number))),
|
||||
Type::Object(params) => match v {
|
||||
Value::Object(_) if depth >= MAX_DEPTH => Err(too_deep()),
|
||||
Value::Object(o) => Ok(check_object(at, depth + 1, params, o, saved)?.map(Value::Object)),
|
||||
_ => done(false),
|
||||
},
|
||||
Type::Array(item) => match v.as_array() {
|
||||
Some(_) if depth >= MAX_DEPTH => Err(too_deep()),
|
||||
Some(items) => check_items(at, depth + 1, item, items, lenient, saved),
|
||||
None => done(false),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Each of `items` checked against `item`: the list with what [`check_value`] changed, when it
|
||||
/// changed anything.
|
||||
fn check_items(at: &At<'_>, depth: usize, item: &Type, items: &[Value], lenient: bool, saved: bool) -> Result<Option<Value>, ParamError> {
|
||||
let mut changes: Vec<(usize, Value)> = Vec::new();
|
||||
for (i, x) in items.iter().enumerate() {
|
||||
if let Some(changed) = check_value(&At::Index(at, i), depth, item, x, lenient, saved)? {
|
||||
changes.push((i, changed));
|
||||
}
|
||||
}
|
||||
if changes.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut filled = items.to_vec();
|
||||
for (i, v) in changes {
|
||||
if let Some(slot) = filled.get_mut(i) {
|
||||
*slot = v;
|
||||
}
|
||||
}
|
||||
Ok(Some(Value::Array(filled)))
|
||||
}
|
||||
|
||||
/// What is wrong with `params`, the object at `path` nested `depth` objects and lists deep.
|
||||
fn object_problem(path: &str, params: &[Param], depth: usize) -> Option<String> {
|
||||
if depth > MAX_DEPTH {
|
||||
return Some(format!("`{path}` nests deeper than {MAX_DEPTH} levels"));
|
||||
}
|
||||
for (i, p) in params.iter().enumerate() {
|
||||
let at = join(path, p.name);
|
||||
if p.name.is_empty() || p.name.contains(['.', '[', ']']) {
|
||||
return Some(format!("the parameter name `{at}` is empty or holds a path character"));
|
||||
}
|
||||
if params.iter().take(i).any(|q| q.name == p.name) {
|
||||
return Some(format!("`{at}` is listed twice"));
|
||||
}
|
||||
if p.required && p.omitted != Omitted::Nothing {
|
||||
return Some(format!("`{at}` is required and has a default"));
|
||||
}
|
||||
if let Some(problem) = type_problem(&at, &p.ty, depth) {
|
||||
return Some(problem);
|
||||
}
|
||||
if let Omitted::Value(text) = p.omitted {
|
||||
let Ok(v) = serde_json::from_str::<Value>(text) else { return Some(format!("the default of `{at}` isn't JSON: {text}")) };
|
||||
if let Err(e) = check_value(&At::Root, 0, &p.ty, &v, false, false) {
|
||||
return Some(format!("the default of `{at}` doesn't pass its type: {e}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn type_problem(at: &str, ty: &Type, depth: usize) -> Option<String> {
|
||||
let bound = |b: &Bound| match *b {
|
||||
Bound::Inclusive(x) => (x, false),
|
||||
Bound::Exclusive(x) => (x, true),
|
||||
};
|
||||
match ty {
|
||||
Type::Number { min, max, .. } => {
|
||||
let (low, high) = (min.as_ref().map(bound), max.as_ref().map(bound));
|
||||
if low.iter().chain(high.iter()).any(|(x, _)| !x.is_finite()) {
|
||||
return Some(format!("the range of `{at}` has an end that isn't finite"));
|
||||
}
|
||||
match (low, high) {
|
||||
(Some((a, open_a)), Some((b, open_b))) if a > b || (a == b && (open_a || open_b)) => {
|
||||
Some(format!("no value can be in the range of `{at}`"))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
Type::Integer { min: Some(a), max: Some(b) } if a > b => Some(format!("no value can be in the range of `{at}`")),
|
||||
Type::Enum([]) => Some(format!("the enum of `{at}` has no names")),
|
||||
Type::Enum(names) if names.iter().enumerate().any(|(i, n)| names.iter().take(i).any(|m| m == n)) => {
|
||||
Some(format!("the enum of `{at}` repeats a name"))
|
||||
}
|
||||
Type::Object(params) => object_problem(at, params, depth + 1),
|
||||
Type::Array(_) if depth >= MAX_DEPTH => Some(format!("`{at}` nests deeper than {MAX_DEPTH} levels")),
|
||||
Type::Array(item) => type_problem(&format!("{at}[]"), item, depth + 1),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// `{a: number, b?: bool}`: the params object of a params text, `depth` objects and lists deep.
|
||||
fn object_text(params: &[Param], depth: usize) -> String {
|
||||
format!("{{{}}}", params.iter().map(|p| param_text(p, depth)).collect::<Vec<_>>().join(", "))
|
||||
}
|
||||
|
||||
fn param_text(p: &Param, depth: usize) -> String {
|
||||
let mut s = format!("{}{}: {}", p.name, if p.required { "" } else { "?" }, type_text(&p.ty, depth));
|
||||
match p.omitted {
|
||||
Omitted::Value(v) | Omitted::Note(v) => s.push_str(&format!(" (default: {v})")),
|
||||
Omitted::Nothing => {}
|
||||
}
|
||||
if !p.doc.is_empty() {
|
||||
s.push_str(&format!(" ({})", p.doc));
|
||||
}
|
||||
if p.deprecated.is_some() {
|
||||
s.push_str(" (deprecated)");
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn type_text(ty: &Type, depth: usize) -> String {
|
||||
match ty {
|
||||
Type::Object(_) | Type::Array(_) if depth >= MAX_DEPTH => "…".into(),
|
||||
Type::Number { min, max, unit } => {
|
||||
format!("number{}{}", range_text(min.as_ref(), max.as_ref()), unit.map(|u| format!(" {u}")).unwrap_or_default())
|
||||
}
|
||||
Type::Integer { min, max } => {
|
||||
format!("integer{}", range_text(min.map(|m| Bound::Inclusive(m as f64)).as_ref(), max.map(|m| Bound::Inclusive(m as f64)).as_ref()))
|
||||
}
|
||||
Type::Boolean => "bool".into(),
|
||||
Type::String => "string".into(),
|
||||
Type::Enum(names) => names.iter().map(|n| format!("\"{n}\"")).collect::<Vec<_>>().join("|"),
|
||||
Type::Id => "id".into(),
|
||||
Type::Ids => "[id…]".into(),
|
||||
Type::Color => "color".into(),
|
||||
Type::Point => "[x, y]".into(),
|
||||
Type::Object(params) => object_text(params, depth + 1),
|
||||
Type::Array(item) => format!("[{}…]", type_text(item, depth + 1)),
|
||||
}
|
||||
}
|
||||
|
||||
/// `{"type": "object", "properties": …, "required": […], "additionalProperties": false}`.
|
||||
fn object_schema(params: &[Param], depth: usize) -> Map<String, Value> {
|
||||
let mut o = Map::new();
|
||||
o.insert("type".into(), "object".into());
|
||||
o.insert("properties".into(), Value::Object(params.iter().map(|p| (p.name.to_string(), param_schema(p, depth))).collect()));
|
||||
let required: Vec<Value> = params.iter().filter(|p| p.required).map(|p| p.name.into()).collect();
|
||||
if !required.is_empty() {
|
||||
o.insert("required".into(), required.into());
|
||||
}
|
||||
o.insert("additionalProperties".into(), false.into());
|
||||
o
|
||||
}
|
||||
|
||||
fn param_schema(p: &Param, depth: usize) -> Value {
|
||||
let inner = type_schema(&p.ty, depth);
|
||||
let mut o = if p.null_omitted {
|
||||
let mut o = Map::new();
|
||||
o.insert("anyOf".into(), json!([inner, {"type": "null"}]));
|
||||
o
|
||||
} else {
|
||||
match inner {
|
||||
Value::Object(o) => o,
|
||||
_ => Map::new(),
|
||||
}
|
||||
};
|
||||
if !p.doc.is_empty() {
|
||||
o.insert("description".into(), p.doc.into());
|
||||
}
|
||||
match p.omitted {
|
||||
Omitted::Value(json) => {
|
||||
if let Ok(v) = serde_json::from_str::<Value>(json) {
|
||||
o.insert("default".into(), v);
|
||||
}
|
||||
}
|
||||
Omitted::Note(note) => {
|
||||
o.insert("x-default".into(), note.into());
|
||||
}
|
||||
Omitted::Nothing => {}
|
||||
}
|
||||
o.insert("x-since".into(), p.since.into());
|
||||
if let Some(level) = p.deprecated {
|
||||
o.insert("deprecated".into(), true.into());
|
||||
o.insert("x-deprecatedSince".into(), level.into());
|
||||
}
|
||||
if p.lenient {
|
||||
o.insert("x-lenient".into(), true.into());
|
||||
}
|
||||
Value::Object(o)
|
||||
}
|
||||
|
||||
fn bound_keywords(o: &mut Map<String, Value>, min: Option<&Bound>, max: Option<&Bound>) {
|
||||
let low = match min {
|
||||
Some(Bound::Inclusive(a)) => Some(("minimum", a)),
|
||||
Some(Bound::Exclusive(a)) => Some(("exclusiveMinimum", a)),
|
||||
None => None,
|
||||
};
|
||||
let high = match max {
|
||||
Some(Bound::Inclusive(b)) => Some(("maximum", b)),
|
||||
Some(Bound::Exclusive(b)) => Some(("exclusiveMaximum", b)),
|
||||
None => None,
|
||||
};
|
||||
for (key, x) in low.into_iter().chain(high) {
|
||||
o.insert(key.into(), json!(x));
|
||||
}
|
||||
}
|
||||
|
||||
fn type_schema(ty: &Type, depth: usize) -> Value {
|
||||
match ty {
|
||||
Type::Object(_) | Type::Array(_) if depth >= MAX_DEPTH => Value::Bool(true),
|
||||
Type::Number { min, max, unit } => {
|
||||
let mut o = Map::new();
|
||||
o.insert("type".into(), "number".into());
|
||||
bound_keywords(&mut o, min.as_ref(), max.as_ref());
|
||||
if let Some(u) = unit {
|
||||
o.insert("x-unit".into(), (*u).into());
|
||||
}
|
||||
Value::Object(o)
|
||||
}
|
||||
Type::Integer { min, max } => {
|
||||
let mut o = Map::new();
|
||||
o.insert("type".into(), "integer".into());
|
||||
if let Some(m) = min {
|
||||
o.insert("minimum".into(), (*m).into());
|
||||
}
|
||||
if let Some(m) = max {
|
||||
o.insert("maximum".into(), (*m).into());
|
||||
}
|
||||
Value::Object(o)
|
||||
}
|
||||
Type::Boolean => json!({"type": "boolean"}),
|
||||
Type::String => json!({"type": "string"}),
|
||||
Type::Enum(names) => json!({"enum": names}),
|
||||
Type::Id => json!({"type": "integer", "minimum": 0, "maximum": MAX_ID, "x-kind": "id"}),
|
||||
Type::Ids => json!({"type": "array", "items": {"type": "integer", "minimum": 0, "maximum": MAX_ID, "x-kind": "id"}}),
|
||||
Type::Color => json!({
|
||||
"anyOf": [{"type": "string"}, {"type": "array", "items": {"type": "number"}, "minItems": 3}, {"type": "object"}],
|
||||
"x-kind": "color",
|
||||
}),
|
||||
Type::Point => json!({
|
||||
"type": "array",
|
||||
"prefixItems": [{"type": "number"}, {"type": "number"}],
|
||||
"items": false,
|
||||
"minItems": 2,
|
||||
"x-kind": "point",
|
||||
}),
|
||||
Type::Object(params) => Value::Object(object_schema(params, depth + 1)),
|
||||
Type::Array(item) => json!({"type": "array", "items": type_schema(item, depth + 1)}),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod conformance;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,536 @@
|
||||
//! Tests of the validator, the params text and the JSON form, on schemas written here.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// The schema with the one parameter `x` of type `ty`.
|
||||
fn one(ty: Type) -> ParamSchema {
|
||||
let params: &'static [Param] = Box::leak(Box::new([Param::new("x", ty)]));
|
||||
ParamSchema::new(params, "")
|
||||
}
|
||||
|
||||
/// Whether `{x: v}` passes the schema of `ty`; on a refusal, the error must name `x`.
|
||||
fn passes(ty: Type, v: Value) -> bool {
|
||||
match one(ty).validate(&json!({ "x": v })) {
|
||||
Ok(_) => true,
|
||||
Err(e) => {
|
||||
assert!(e.path.starts_with('x'), "{e}");
|
||||
assert!(e.to_string().contains("`x"), "{e}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbers_are_checked_against_their_range() {
|
||||
let any = Type::NUMBER;
|
||||
assert!(passes(any, json!(-3.5)) && passes(any, json!(7)));
|
||||
assert!(!passes(any, json!("7")) && !passes(any, json!(null)) && !passes(any, json!(true)));
|
||||
let positive = Type::Number { min: Some(Bound::Exclusive(0.0)), max: None, unit: None };
|
||||
assert!(passes(positive, json!(0.001)));
|
||||
assert!(!passes(positive, json!(0)) && !passes(positive, json!(-1)));
|
||||
let percent = Type::Number { min: Some(Bound::Inclusive(0.0)), max: Some(Bound::Inclusive(100.0)), unit: Some("%") };
|
||||
assert!(passes(percent, json!(0)) && passes(percent, json!(100)));
|
||||
assert!(!passes(percent, json!(100.5)));
|
||||
let below = Type::Number { min: None, max: Some(Bound::Exclusive(1.0)), unit: None };
|
||||
assert!(passes(below, json!(0.99)) && !passes(below, json!(1)));
|
||||
assert_eq!(one(positive).validate(&json!({"x": 0})).unwrap_err().to_string(), "`x` must be a number > 0, not 0");
|
||||
assert_eq!(one(percent).validate(&json!({"x": 101})).unwrap_err().to_string(), "`x` must be a number 0 to 100 (%), not 101");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integers_take_no_fraction_and_keep_their_range() {
|
||||
let any = Type::Integer { min: None, max: None };
|
||||
assert!(passes(any, json!(-4)) && passes(any, json!(u64::MAX)));
|
||||
assert!(!passes(any, json!(1.5)) && !passes(any, json!("2")));
|
||||
let small = Type::Integer { min: Some(1), max: Some(25) };
|
||||
assert!(passes(small, json!(1)) && passes(small, json!(25)));
|
||||
assert!(!passes(small, json!(0)) && !passes(small, json!(26)) && !passes(small, json!(u64::MAX)));
|
||||
assert!(passes(small, json!(25.0)) && !passes(small, json!(26.0)) && !passes(small, json!(1e300)) && !passes(small, json!(-1e300)));
|
||||
let negative = Type::Integer { min: None, max: Some(-1_000_000_000_000_000_000) };
|
||||
assert!(passes(negative, json!(-1e20)) && !passes(negative, json!(1e20)));
|
||||
let lowest = Type::Integer { min: Some(i64::MIN), max: None };
|
||||
assert!(!passes(lowest, json!(-1e20)) && passes(lowest, json!(1e20)));
|
||||
}
|
||||
|
||||
/// JSON Schema counts a number with no fractional part as an integer: `2.0` passes, and the
|
||||
/// command gets `2`.
|
||||
#[test]
|
||||
fn a_whole_number_written_with_a_fraction_is_an_integer() {
|
||||
let any = Type::Integer { min: None, max: None };
|
||||
assert_eq!(one(any).validate(&json!({"x": 2.0})).unwrap(), Some(json!({"x": 2})));
|
||||
assert_eq!(one(any).validate(&json!({"x": 2})).unwrap(), None);
|
||||
assert!(passes(any, json!(1e300)));
|
||||
assert_eq!(one(Type::Id).validate(&json!({"x": 7.0})).unwrap(), Some(json!({"x": 7})));
|
||||
assert_eq!(one(Type::Ids).validate(&json!({"x": [1, 2.0]})).unwrap(), Some(json!({"x": [1, 2]})));
|
||||
assert!(passes(Type::Id, json!(MAX_ID)) && !passes(Type::Id, json!(MAX_ID + 1)) && !passes(Type::Id, json!(1e300)));
|
||||
assert_eq!(one(Type::Ids).validate(&json!({"x": [1, "2"]})).unwrap_err().path, "x[1]");
|
||||
}
|
||||
|
||||
/// A value with no finite number in it reads as null in JSON, and a number refuses it; the largest
|
||||
/// finite numbers pass a number with no range.
|
||||
#[test]
|
||||
fn numbers_at_the_edges() {
|
||||
assert!(!passes(Type::NUMBER, json!(f64::NAN)) && !passes(Type::NUMBER, json!(f64::INFINITY)));
|
||||
assert!(passes(Type::NUMBER, json!(f64::MAX)) && passes(Type::NUMBER, json!(f64::MIN)));
|
||||
let unit = Type::Number { min: Some(Bound::Inclusive(0.0)), max: Some(Bound::Inclusive(1.0)), unit: None };
|
||||
assert!(passes(unit, json!(0.0)) && passes(unit, json!(1.0)) && !passes(unit, json!(-1e-300)) && !passes(unit, json!(1.0000001)));
|
||||
}
|
||||
|
||||
/// A list too long to name one path per entry costs one pass, and the error names the entry.
|
||||
#[test]
|
||||
fn a_huge_list_is_checked_once_and_names_the_entry() {
|
||||
let mut ids: Vec<Value> = (0..200_000u64).map(Value::from).collect();
|
||||
assert_eq!(one(Type::Ids).validate(&json!({ "x": ids })).unwrap(), None);
|
||||
ids.push(json!("no"));
|
||||
let e = one(Type::Ids).validate(&json!({ "x": ids })).unwrap_err();
|
||||
assert_eq!(e.path, "x[200000]");
|
||||
assert!(e.to_string().chars().count() < 120, "{e}");
|
||||
}
|
||||
|
||||
/// A list nested far past anything the schema can take: dropping or writing it out recurses per
|
||||
/// level, which is why the test leaks it rather than dropping it.
|
||||
fn nested_lists(levels: usize) -> Value {
|
||||
(0..levels).fold(json!(1), |inner, _| Value::Array(vec![inner]))
|
||||
}
|
||||
|
||||
/// A refused value is shown from its start only, however deep or long it is, so an error costs
|
||||
/// the same for a hostile value as for a small one.
|
||||
#[test]
|
||||
fn a_refused_value_is_read_only_as_far_as_it_is_shown() {
|
||||
let deep = nested_lists(500_000);
|
||||
let params = json!({});
|
||||
let mut map = params.as_object().unwrap().clone();
|
||||
map.insert("x".into(), deep);
|
||||
let params = Value::Object(map);
|
||||
let e = one(Type::String).validate(¶ms).unwrap_err();
|
||||
assert!(e.to_string().starts_with("`x` must be a string, not [[[[") && e.to_string().ends_with('…'), "{e}");
|
||||
assert!(e.to_string().chars().count() <= 100, "{e}");
|
||||
std::mem::forget(params);
|
||||
|
||||
let huge = "x".repeat(5_000_000);
|
||||
let e = one(Type::Boolean).validate(&json!({ "x": huge })).unwrap_err();
|
||||
assert!(e.to_string().chars().count() <= 100, "{}", e.to_string().len());
|
||||
assert!(e.to_string().ends_with('…'));
|
||||
|
||||
let e = one(Type::Boolean).validate(&json!({"x": {"a": 1, "b": [1, 2]}})).unwrap_err();
|
||||
assert_eq!(e.to_string(), "`x` must be true or false, not {\"a\":1,\"b\":[1,2]}");
|
||||
let e = ParamSchema::new(&[], "").validate(&nested_lists(3)).unwrap_err();
|
||||
assert_eq!(e.to_string(), "the params must be an object, not [[[1]]]");
|
||||
}
|
||||
|
||||
/// A list entry made an integer, and a default filled in, don't copy a value that is still to be
|
||||
/// checked: copying recurses per level.
|
||||
#[test]
|
||||
fn a_list_with_a_change_is_not_copied_before_all_of_it_is_checked() {
|
||||
let params = Value::Object([("x".to_string(), Value::Array(vec![json!(2.0), nested_lists(500_000)]))].into_iter().collect());
|
||||
assert_eq!(one(Type::Ids).validate(¶ms).unwrap_err().path, "x[1]");
|
||||
std::mem::forget(params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_object_with_a_default_is_not_copied_before_all_of_it_is_checked() {
|
||||
const FILLED_FIRST: &[Param] = &[Param::new("a", Type::NUMBER).defaults_to("0"), Param::new("b", Type::String)];
|
||||
let params = Value::Object([("b".to_string(), nested_lists(500_000))].into_iter().collect());
|
||||
assert_eq!(ParamSchema::new(FILLED_FIRST, "").validate(¶ms).unwrap_err().path, "b");
|
||||
std::mem::forget(params);
|
||||
}
|
||||
|
||||
/// A value the schema doesn't look inside is measured before a default copies the object it is in.
|
||||
const WITH_DEFAULT: &[Param] = &[Param::new("a", Type::NUMBER).defaults_to("0"), Param::new("b", Type::Color).optional()];
|
||||
const LENIENT_AFTER_DEFAULT: &[Param] = &[Param::new("a", Type::NUMBER).defaults_to("0"), Param::new("b", Type::Boolean).optional().lenient()];
|
||||
|
||||
fn holding(key: &str, v: Value) -> Value {
|
||||
Value::Object([(key.to_string(), v)].into_iter().collect())
|
||||
}
|
||||
|
||||
/// The lists of fixed shape count toward the nesting limit as the other lists do.
|
||||
#[test]
|
||||
fn id_lists_points_and_color_lists_count_toward_the_nesting_limit() {
|
||||
let check = |depth, ty: &Type, v: &Value| check_value(&At::Root, depth, ty, v, false, false);
|
||||
for (ty, v) in [(Type::Ids, json!([1])), (Type::Point, json!([1, 2])), (Type::Color, json!([1, 0, 0]))] {
|
||||
assert!(check(MAX_DEPTH - 1, &ty, &v).is_ok(), "{ty:?}");
|
||||
assert_eq!(check(MAX_DEPTH, &ty, &v).unwrap_err().problem, Problem::TooDeep, "{ty:?}");
|
||||
}
|
||||
assert!(check(MAX_DEPTH, &Type::Color, &json!("#ff0000")).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_saved_retired_key_past_the_nesting_limit_is_refused() {
|
||||
let params = holding("older", nested_lists(500_000));
|
||||
let e = ParamSchema::new(WITH_DEFAULT, "").validate_saved(¶ms).unwrap_err();
|
||||
assert_eq!(e.path, "older");
|
||||
assert!(matches!(e.problem, Problem::Unknown { .. }));
|
||||
std::mem::forget(params);
|
||||
let kept = ParamSchema::new(WITH_DEFAULT, "").validate_saved(&holding("older", nested_lists(MAX_DEPTH))).unwrap().unwrap();
|
||||
assert_eq!(kept["a"], json!(0));
|
||||
assert_eq!(kept["older"], nested_lists(MAX_DEPTH));
|
||||
assert!(ParamSchema::new(WITH_DEFAULT, "").validate_saved(&holding("older", nested_lists(MAX_DEPTH + 1))).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_color_object_past_the_nesting_limit_is_refused() {
|
||||
let params = holding("b", holding("k", nested_lists(500_000)));
|
||||
let e = ParamSchema::new(WITH_DEFAULT, "").validate(¶ms).unwrap_err();
|
||||
assert_eq!(e.path, "b");
|
||||
assert!(matches!(e.problem, Problem::WrongType { .. }));
|
||||
std::mem::forget(params);
|
||||
assert!(ParamSchema::new(WITH_DEFAULT, "").validate(&json!({"b": {"r": 1, "g": 0, "b": 0}})).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lenient_value_past_the_nesting_limit_is_refused() {
|
||||
let params = holding("b", nested_lists(500_000));
|
||||
let e = ParamSchema::new(LENIENT_AFTER_DEFAULT, "").validate(¶ms).unwrap_err();
|
||||
assert_eq!(e.path, "b");
|
||||
assert!(matches!(e.problem, Problem::WrongType { .. }));
|
||||
std::mem::forget(params);
|
||||
assert_eq!(ParamSchema::new(LENIENT_AFTER_DEFAULT, "").validate(&json!({"b": [1]})).unwrap().unwrap()["b"], json!([1]));
|
||||
}
|
||||
|
||||
/// A key is cut in the error, and an unknown key is refused whatever it holds.
|
||||
#[test]
|
||||
fn a_hostile_key_is_cut_and_refused() {
|
||||
let key = "k".repeat(1_000_000);
|
||||
let e = one(Type::Boolean).validate(&json!({ key: true, "x": true })).unwrap_err();
|
||||
assert_eq!(e.path.chars().count(), KEY_CHARS + 1);
|
||||
assert!(e.path.ends_with('…') && e.to_string().chars().count() < 200, "{}", e.path.len());
|
||||
for key in ["", "a.b", "a[0]", "`", "\u{0}", "名前"] {
|
||||
let e = one(Type::Boolean).validate(&json!({ key: true, "x": true })).unwrap_err();
|
||||
assert_eq!(e.path, key);
|
||||
assert!(matches!(e.problem, Problem::Unknown { .. }));
|
||||
}
|
||||
assert_eq!(shown_key("short"), "short");
|
||||
}
|
||||
|
||||
/// A schema that refers to itself, which only a mistake can write: checking values stops at the
|
||||
/// nesting limit, and the writers finish.
|
||||
static ENDLESS: Type = Type::Array(&ENDLESS);
|
||||
|
||||
#[test]
|
||||
fn a_schema_that_refers_to_itself_stops_at_the_nesting_limit() {
|
||||
let s = one(ENDLESS);
|
||||
let e = s.validate(&json!({ "x": nested_lists(MAX_DEPTH + 2) })).unwrap_err();
|
||||
assert_eq!(e.problem, Problem::TooDeep);
|
||||
assert!(e.path.starts_with("x[0][0]"));
|
||||
assert!(s.definition_problem().is_some_and(|p| p.contains("nests deeper")));
|
||||
assert!(s.params_text().contains('…'));
|
||||
assert!(s.to_json()["properties"]["x"].is_object());
|
||||
assert_ne!(hash([("a", &s)]), 0);
|
||||
}
|
||||
|
||||
const BAD_DEFAULT: &[Param] = &[Param::new("x", Type::NUMBER).defaults_to("\"zero\"")];
|
||||
const NOT_JSON: &[Param] = &[Param::new("x", Type::NUMBER).defaults_to("{")];
|
||||
const TWICE: &[Param] = &[Param::new("x", Type::NUMBER), Param::new("x", Type::String)];
|
||||
const EMPTY_NAME: &[Param] = &[Param::new("", Type::NUMBER)];
|
||||
const PATH_NAME: &[Param] = &[Param::new("a.b", Type::NUMBER)];
|
||||
const NO_NAMES: &[Param] = &[Param::new("x", Type::Enum(&[]))];
|
||||
const SAME_NAMES: &[Param] = &[Param::new("x", Type::Enum(&["a", "a"]))];
|
||||
const EMPTY_RANGE: &[Param] = &[Param::new("x", Type::Number { min: Some(Bound::Exclusive(1.0)), max: Some(Bound::Inclusive(1.0)), unit: None })];
|
||||
const BACKWARD: &[Param] = &[Param::new("x", Type::Integer { min: Some(2), max: Some(1) })];
|
||||
const NOT_FINITE: &[Param] = &[Param::new("x", Type::Number { min: None, max: Some(Bound::Inclusive(f64::INFINITY)), unit: None })];
|
||||
const REQUIRED_DEFAULT: &[Param] = &[Param { required: true, omitted: Omitted::Value("1"), ..Param::new("x", Type::NUMBER) }];
|
||||
const DEEP_FAULT: &[Param] = &[Param::new("edits", Type::Array(&Type::Object(TWICE)))];
|
||||
|
||||
#[test]
|
||||
fn a_schema_that_is_wrong_in_itself_is_reported() {
|
||||
for (params, why) in [
|
||||
(BAD_DEFAULT, "doesn't pass its type"),
|
||||
(NOT_JSON, "isn't JSON"),
|
||||
(TWICE, "listed twice"),
|
||||
(EMPTY_NAME, "empty or holds"),
|
||||
(PATH_NAME, "empty or holds"),
|
||||
(NO_NAMES, "no names"),
|
||||
(SAME_NAMES, "repeats"),
|
||||
(EMPTY_RANGE, "no value can be"),
|
||||
(BACKWARD, "no value can be"),
|
||||
(NOT_FINITE, "isn't finite"),
|
||||
(REQUIRED_DEFAULT, "required"),
|
||||
(DEEP_FAULT, "`edits[].x` is listed twice"),
|
||||
] {
|
||||
let problem = ParamSchema::new(params, "").definition_problem();
|
||||
assert!(problem.as_deref().is_some_and(|p| p.contains(why)), "{params:?}: {problem:?}");
|
||||
}
|
||||
assert_eq!(ParamSchema::new(VIEW, "").definition_problem(), None);
|
||||
assert_eq!(ParamSchema::new(&[], "").definition_problem(), None);
|
||||
}
|
||||
|
||||
/// Saved steps keep keys the schema doesn't name, at any depth; the values it names are checked.
|
||||
#[test]
|
||||
fn saved_steps_pass_retired_keys_and_check_their_values() {
|
||||
let edits = one(Type::Array(&Type::Object(EDIT)));
|
||||
let retired = json!({"x": [{"name": "A", "old": 1}], "older": true});
|
||||
assert!(edits.validate(&retired).is_err());
|
||||
let filled = edits.validate_saved(&retired).unwrap().unwrap();
|
||||
assert_eq!(filled["x"][0]["delete"], json!(false), "defaults are filled in");
|
||||
assert_eq!(filled["older"], json!(true), "the keys stay");
|
||||
assert_eq!(edits.validate_saved(&json!({"x": [{"name": 5}], "older": true})).unwrap_err().path, "x[0].name");
|
||||
assert_eq!(edits.validate_saved(&json!({"older": true})).unwrap_err().problem, Problem::Missing);
|
||||
}
|
||||
|
||||
/// Every type's JSON form follows the 2020-12 rules, and an independent validator of those rules
|
||||
/// agrees with [`ParamSchema::validate`] on each value.
|
||||
#[test]
|
||||
fn the_json_form_and_the_validator_agree_with_the_2020_12_rules() {
|
||||
const NULLABLE: &[Param] = &[Param::new("a", Type::String).optional().null_omitted(), Param::new("b", Type::Boolean).defaults_to("true")];
|
||||
let types = [
|
||||
Type::NUMBER,
|
||||
Type::Number { min: Some(Bound::Exclusive(0.0)), max: Some(Bound::Inclusive(1.0)), unit: Some("pt") },
|
||||
Type::Number { min: Some(Bound::Inclusive(-1.0)), max: Some(Bound::Exclusive(1.0)), unit: None },
|
||||
Type::Integer { min: None, max: None },
|
||||
Type::Integer { min: Some(1), max: Some(25) },
|
||||
Type::Boolean,
|
||||
Type::String,
|
||||
Type::Enum(&["a", "b"]),
|
||||
Type::Id,
|
||||
Type::Ids,
|
||||
Type::Color,
|
||||
Type::Point,
|
||||
Type::Object(EDIT),
|
||||
Type::Object(NULLABLE),
|
||||
Type::Object(&[]),
|
||||
Type::Array(&Type::NUMBER),
|
||||
Type::Array(&Type::Object(EDIT)),
|
||||
];
|
||||
let values = [
|
||||
json!(null),
|
||||
json!(true),
|
||||
json!(0),
|
||||
json!(1),
|
||||
json!(2.0),
|
||||
json!(25.5),
|
||||
json!(-1),
|
||||
json!(-1.0),
|
||||
json!(1e300),
|
||||
json!(MAX_ID),
|
||||
json!(MAX_ID + 1),
|
||||
json!(u64::MAX),
|
||||
json!(""),
|
||||
json!("a"),
|
||||
json!("c"),
|
||||
json!([]),
|
||||
json!([1, 2]),
|
||||
json!([1, 2, 3]),
|
||||
json!([1, "2"]),
|
||||
json!([[1]]),
|
||||
json!({}),
|
||||
json!({"name": "A"}),
|
||||
json!({"name": "A", "delete": false}),
|
||||
json!({"name": "A", "delete": null}),
|
||||
json!({"name": "A", "other": 1}),
|
||||
json!({"a": null, "b": false}),
|
||||
json!({"a": 5}),
|
||||
json!([{"name": "A"}]),
|
||||
json!([{"name": 1}]),
|
||||
];
|
||||
for ty in types {
|
||||
let schema = one(ty);
|
||||
let written = schema.to_json();
|
||||
conformance::check_schema(&written, "#").unwrap_or_else(|e| panic!("{ty:?}: {e}\n{written:#}"));
|
||||
assert_eq!(written["$schema"], "https://json-schema.org/draft/2020-12/schema");
|
||||
for v in &values {
|
||||
let params = json!({ "x": v });
|
||||
let local = schema.validate(¶ms).is_ok();
|
||||
assert_eq!(local, conformance::instance_valid(&written, ¶ms), "{ty:?} on {v}: {written}");
|
||||
}
|
||||
}
|
||||
for params in [VIEW, EDIT, NULLABLE] {
|
||||
let schema = ParamSchema::new(params, "").deprecated(1);
|
||||
conformance::check_schema(&schema.to_json(), "#").unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// The independent checker refuses what the 2020-12 rules refuse, so its passes above mean
|
||||
/// something.
|
||||
#[test]
|
||||
fn the_2020_12_checker_refuses_invalid_schemas() {
|
||||
for bad in [
|
||||
json!(5),
|
||||
json!({"type": "float"}),
|
||||
json!({"enum": []}),
|
||||
json!({"enum": [1, 1]}),
|
||||
json!({"required": ["a", "a"]}),
|
||||
json!({"required": [1]}),
|
||||
json!({"properties": []}),
|
||||
json!({"properties": {"a": 5}}),
|
||||
json!({"items": 1}),
|
||||
json!({"prefixItems": []}),
|
||||
json!({"anyOf": []}),
|
||||
json!({"minimum": "0"}),
|
||||
json!({"minItems": -1}),
|
||||
json!({"description": 5}),
|
||||
json!({"deprecated": "yes"}),
|
||||
json!({"type": "number", "default": "zero"}),
|
||||
json!({"$schema": "not a uri"}),
|
||||
json!({"minimun": 0}),
|
||||
] {
|
||||
assert!(conformance::check_schema(&bad, "#").is_err(), "{bad}");
|
||||
}
|
||||
assert!(conformance::instance_valid(&json!({"type": "integer"}), &json!(2.0)));
|
||||
assert!(!conformance::instance_valid(&json!({"type": "integer"}), &json!(2.5)));
|
||||
assert!(!conformance::instance_valid(&json!({"prefixItems": [{"type": "number"}], "items": false}), &json!([1, 2])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn booleans_and_strings_take_only_their_own_type() {
|
||||
assert!(passes(Type::Boolean, json!(true)) && passes(Type::Boolean, json!(false)));
|
||||
assert!(!passes(Type::Boolean, json!("true")) && !passes(Type::Boolean, json!(1)) && !passes(Type::Boolean, json!(null)));
|
||||
assert!(passes(Type::String, json!("")) && passes(Type::String, json!("View 1")));
|
||||
assert!(!passes(Type::String, json!(5)) && !passes(Type::String, json!(["a"])));
|
||||
assert_eq!(one(Type::Boolean).validate(&json!({"x": "yes"})).unwrap_err().to_string(), "`x` must be true or false, not \"yes\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_enum_takes_one_of_its_names() {
|
||||
let modes = Type::Enum(&["rgb", "cmyk"]);
|
||||
assert!(passes(modes, json!("rgb")) && passes(modes, json!("cmyk")));
|
||||
assert!(!passes(modes, json!("RGB")) && !passes(modes, json!("lab")) && !passes(modes, json!(0)));
|
||||
assert_eq!(one(modes).validate(&json!({"x": "lab"})).unwrap_err().to_string(), "`x` must be one of \"rgb\", \"cmyk\", not \"lab\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ids_are_non_negative_integers() {
|
||||
assert!(passes(Type::Id, json!(0)) && passes(Type::Id, json!(42)));
|
||||
assert!(!passes(Type::Id, json!(-1)) && !passes(Type::Id, json!(1.5)) && !passes(Type::Id, json!("42")));
|
||||
assert!(passes(Type::Ids, json!([])) && passes(Type::Ids, json!([3, 4])));
|
||||
assert!(!passes(Type::Ids, json!(3)) && !passes(Type::Ids, json!([3, "4"])) && !passes(Type::Ids, json!([-3])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn colors_and_points_have_their_shapes() {
|
||||
assert!(passes(Type::Color, json!("#ff0000")) && passes(Type::Color, json!([1, 0, 0])) && passes(Type::Color, json!({"gray": 50})));
|
||||
assert!(!passes(Type::Color, json!(5)) && !passes(Type::Color, json!([1, 0])) && !passes(Type::Color, json!([1, 0, "x"])));
|
||||
assert!(passes(Type::Point, json!([0, -2.5])));
|
||||
assert!(!passes(Type::Point, json!([1])) && !passes(Type::Point, json!([1, 2, 3])) && !passes(Type::Point, json!([1, "2"])));
|
||||
assert!(!passes(Type::Point, json!({"x": 1, "y": 2})));
|
||||
}
|
||||
|
||||
const EDIT: &[Param] = &[Param::new("name", Type::String), Param::new("delete", Type::Boolean).defaults_to("false")];
|
||||
|
||||
#[test]
|
||||
fn objects_and_arrays_name_the_path_of_the_value_inside() {
|
||||
let edits = Type::Array(&Type::Object(EDIT));
|
||||
assert!(passes(edits, json!([])) && passes(edits, json!([{"name": "A"}, {"name": "B", "delete": true}])));
|
||||
let err = |v: Value| one(edits).validate(&json!({ "x": v })).unwrap_err();
|
||||
assert_eq!(err(json!([{"name": "A"}, {"name": "B", "delete": "no"}])).path, "x[1].delete");
|
||||
assert_eq!(err(json!([{"name": "A"}, {"delete": true}])).to_string(), "missing `x[1].name`");
|
||||
let unknown = err(json!([{"name": "A", "nmae": "B"}]));
|
||||
assert_eq!((unknown.path.as_str(), unknown.problem), ("x[0].nmae", Problem::Unknown { keys: vec!["name", "delete"] }));
|
||||
assert_eq!(err(json!({"name": "A"})).to_string(), "`x` must be a list, not {\"name\":\"A\"}");
|
||||
assert_eq!(err(json!(["A"])).to_string(), "`x[0]` must be an object, not \"A\"");
|
||||
// Defaults inside are filled in too.
|
||||
let filled = one(edits).validate(&json!({"x": [{"name": "A"}]})).unwrap();
|
||||
assert_eq!(filled, Some(json!({"x": [{"name": "A", "delete": false}]})));
|
||||
}
|
||||
|
||||
const VIEW: &[Param] = &[
|
||||
Param::new("name", Type::String).default_note("\"View N\"").null_omitted(),
|
||||
Param::new("center", Type::Point),
|
||||
Param::new("rotation", Type::Number { min: None, max: None, unit: Some("deg") }).defaults_to("0"),
|
||||
];
|
||||
|
||||
#[test]
|
||||
fn keys_requirements_nulls_and_defaults() {
|
||||
let s = ParamSchema::new(VIEW, "save a view").returns("{name}");
|
||||
// Unknown keys are refused first, naming the key and the keys the command takes.
|
||||
let e = s.validate(&json!({"centre": [0, 0]})).unwrap_err();
|
||||
assert_eq!((e.path.as_str(), e.problem), ("centre", Problem::Unknown { keys: vec!["name", "center", "rotation"] }));
|
||||
assert_eq!(s.validate(&json!({})).unwrap_err(), ParamError { path: "center".into(), problem: Problem::Missing });
|
||||
// Null counts as omitted only where the schema says so.
|
||||
assert_eq!(s.validate(&json!({"center": [1, 2], "name": null, "rotation": 15})).unwrap(), None);
|
||||
assert_eq!(s.validate(&json!({"center": [1, 2], "rotation": null})).unwrap_err().path, "rotation");
|
||||
// An omitted value with a default gets it; one with a note is left to the command.
|
||||
assert_eq!(s.validate(&json!({"center": [1, 2]})).unwrap(), Some(json!({"center": [1, 2], "rotation": 0})));
|
||||
// Params that aren't an object are refused; null params are `{}`.
|
||||
assert_eq!(s.validate(&json!(5)).unwrap_err().to_string(), "the params must be an object, not 5");
|
||||
assert_eq!(s.validate(&Value::Null).unwrap_err().path, "center");
|
||||
let none = ParamSchema::new(&[], "");
|
||||
assert_eq!(none.validate(&Value::Null).unwrap(), None);
|
||||
assert_eq!(none.validate(&json!({"a": 1})).unwrap_err().to_string(), "unknown parameter `a`; the object takes no keys");
|
||||
}
|
||||
|
||||
const LENIENT: &[Param] = &[Param::new("on", Type::Boolean).optional().lenient(), Param::new("size", Type::NUMBER).optional().lenient()];
|
||||
|
||||
#[test]
|
||||
fn lenient_parameters_pass_values_of_another_type_to_the_command() {
|
||||
let s = ParamSchema::new(LENIENT, "");
|
||||
assert_eq!(s.validate(&json!({"on": "yes", "size": "12"})).unwrap(), None);
|
||||
// Unknown keys are still refused.
|
||||
assert!(s.validate(&json!({"of": true})).is_err());
|
||||
// The JSON form states the documented type, so a validator of that form refuses what passes here.
|
||||
let written = s.to_json();
|
||||
assert_eq!(written["properties"]["on"]["x-lenient"], true);
|
||||
assert!(!conformance::instance_valid(&written, &json!({"on": "yes"})));
|
||||
assert!(conformance::instance_valid(&written, &json!({"on": true})));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_params_text_lists_each_parameter_with_its_type() {
|
||||
let s = ParamSchema::new(VIEW, "save a view").returns("{name}");
|
||||
assert_eq!(s.params_text(), "{name?: string (default: \"View N\"), center: [x, y], rotation?: number deg (default: 0)} save a view → {name}");
|
||||
const ALL: &[Param] = &[
|
||||
Param::new("a", Type::Integer { min: Some(1), max: Some(25) }),
|
||||
Param::new("b", Type::Enum(&["x", "y"])).optional(),
|
||||
Param::new("c", Type::Ids).optional().doc("instead of the selection"),
|
||||
Param::new("d", Type::Color).optional().deprecated(2),
|
||||
Param::new("e", Type::Array(&Type::Object(EDIT))).optional(),
|
||||
Param::new("f", Type::Number { min: Some(Bound::Exclusive(0.0)), max: Some(Bound::Inclusive(10.0)), unit: Some("pt") }).optional(),
|
||||
];
|
||||
assert_eq!(
|
||||
ParamSchema::new(ALL, "").params_text(),
|
||||
"{a: integer 1 to 25, b?: \"x\"|\"y\", c?: [id…] (instead of the selection), d?: color (deprecated), \
|
||||
e?: [{name: string, delete?: bool (default: false)}…], f?: number > 0 and <= 10 pt}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_json_form_is_an_object_schema() {
|
||||
let s = ParamSchema::new(VIEW, "save a view").returns("{name}").writes(Writes::Document).cost(2);
|
||||
let j = s.to_json();
|
||||
assert_eq!(j["$schema"], DIALECT);
|
||||
assert_eq!(j["type"], "object");
|
||||
assert_eq!(j["additionalProperties"], false);
|
||||
assert_eq!(j["required"], json!(["center"]));
|
||||
assert_eq!(j["properties"]["name"]["anyOf"], json!([{"type": "string"}, {"type": "null"}]));
|
||||
assert_eq!(j["properties"]["name"]["x-default"], "\"View N\"");
|
||||
assert_eq!(j["properties"]["rotation"], json!({"type": "number", "x-unit": "deg", "default": 0, "x-since": 1}));
|
||||
assert_eq!(j["properties"]["center"]["prefixItems"], json!([{"type": "number"}, {"type": "number"}]));
|
||||
assert_eq!((j["x-cost"].clone(), j["x-writes"].clone(), j["x-returns"].clone()), (json!(2), json!("document"), json!("{name}")));
|
||||
let range = one(Type::Number { min: Some(Bound::Exclusive(0.0)), max: Some(Bound::Inclusive(1.0)), unit: None }).to_json();
|
||||
assert_eq!(range["properties"]["x"], json!({"type": "number", "exclusiveMinimum": 0.0, "maximum": 1.0, "x-since": 1}));
|
||||
}
|
||||
|
||||
/// Null params read as `{}`, so the root of the JSON form allows null exactly when `{}` passes.
|
||||
/// A nested object does not read null as `{}`.
|
||||
#[test]
|
||||
fn the_json_form_allows_null_params_when_no_parameter_is_required() {
|
||||
const OPTIONAL: &[Param] = &[Param::new("inner", Type::Object(&[Param::new("y", Type::Boolean).optional()])).optional()];
|
||||
let optional = ParamSchema::new(OPTIONAL, "");
|
||||
let written = optional.to_json();
|
||||
conformance::check_schema(&written, "#").unwrap();
|
||||
assert_eq!(written["type"], json!(["object", "null"]));
|
||||
assert_eq!(written["properties"]["inner"]["type"], "object");
|
||||
let required = one(Type::Boolean);
|
||||
assert_eq!(required.to_json()["type"], "object");
|
||||
for schema in [optional, required] {
|
||||
let written = schema.to_json();
|
||||
for params in [json!(null), json!({}), json!(5), json!([]), json!("x"), json!({"inner": null}), json!({"inner": {}})] {
|
||||
assert_eq!(schema.validate(¶ms).is_ok(), conformance::instance_valid(&written, ¶ms), "{params} against {written}");
|
||||
}
|
||||
}
|
||||
assert!(optional.validate(&json!(null)).is_ok());
|
||||
assert!(required.validate(&json!(null)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_hash_follows_every_schema_but_not_their_order() {
|
||||
let a = ParamSchema::new(VIEW, "save a view");
|
||||
let b = ParamSchema::new(LENIENT, "");
|
||||
let h = hash([("view.a", &a), ("view.b", &b)]);
|
||||
assert_eq!(h, hash([("view.b", &b), ("view.a", &a)]));
|
||||
assert_ne!(h, hash([("view.a", &a)]));
|
||||
assert_ne!(h, hash([("view.a", &a), ("view.c", &b)]));
|
||||
let changed = ParamSchema::new(VIEW, "save a view").since(2);
|
||||
assert_ne!(h, hash([("view.a", &changed), ("view.b", &b)]));
|
||||
}
|
||||
@@ -456,6 +456,41 @@ impl Gradient {
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
/// A gradient whose stops are all CMYK as a ramp of C, M, Y, K and opacity: the stops' own
|
||||
/// inks, an off-center midpoint halfway between its two stops' inks, and a Perceptual
|
||||
/// gradient's curve as [`Self::ink_mixes`] mixes the inks. `None` when a stop is in another
|
||||
/// model, there are no stops, or [`Self::ink_mixes`] gives none for the curve.
|
||||
fn ink_ramp(&self) -> Option<Ramp<5>> {
|
||||
let inks: Vec<[f32; 4]> = self
|
||||
.stops
|
||||
.iter()
|
||||
.map(|s| match s.color {
|
||||
Color::Cmyk { c, m, y, k } => Some([c, m, y, k]),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Option<_>>()?;
|
||||
if inks.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let entry = |i: usize, w: f32, o: f32| {
|
||||
let (a, b) = (inks[i], inks.get(i + 1).copied().unwrap_or(inks[i]));
|
||||
let [c, m, y, k]: [f32; 4] = std::array::from_fn(|n| a[n] + (b[n] - a[n]) * w);
|
||||
[c, m, y, k, o]
|
||||
};
|
||||
if !self.method.is_classic() {
|
||||
return Some(Ramp { stops: self.ink_mixes()?.into_iter().map(|(t, i, w, o)| (t, entry(i, w, o))).collect() });
|
||||
}
|
||||
let mut stops = Vec::with_capacity(inks.len());
|
||||
for (i, s) in self.stops.iter().enumerate() {
|
||||
stops.push((s.offset, entry(i, 0.0, s.opacity)));
|
||||
if let Some(n) = self.stops.get(i + 1)
|
||||
&& (s.midpoint - 0.5).abs() > 1e-3
|
||||
{
|
||||
stops.push((s.offset + (n.offset - s.offset) * s.midpoint, entry(i, 0.5, (s.opacity + n.opacity) / 2.0)));
|
||||
}
|
||||
}
|
||||
Some(Ramp { stops })
|
||||
}
|
||||
/// The same ramp the other way round: the stops in reverse order at mirrored offsets, each
|
||||
/// segment's midpoint mirrored with it.
|
||||
pub fn reverse(&mut self) {
|
||||
@@ -895,19 +930,45 @@ impl GradientPaint {
|
||||
fill: bool,
|
||||
rgb: &dyn Fn(&Color) -> [f32; 3],
|
||||
) -> impl Iterator<Item = [u8; 4]> + use<> {
|
||||
self.grid(Ramp::new(&self.gradient, rgb), bounds, area, cols, rows, cells, fill, false)
|
||||
}
|
||||
/// [`Self::pixels`] of a linear or radial gradient whose stops are all CMYK, as C, M, Y, K
|
||||
/// and opacity levels mixed from the stops' inks; `None` when a stop is in another model,
|
||||
/// there are no stops, or a Perceptual curve has too many stops for [`Gradient::ink_mixes`].
|
||||
/// Dither's noise adds no ink where the ramp has none.
|
||||
pub fn ink_pixels(&self, bounds: Rect, area: Rect, cols: u16, rows: u16, fill: bool) -> Option<impl Iterator<Item = [u8; 5]> + use<>> {
|
||||
let ramp = self.gradient.ink_ramp()?;
|
||||
Some(self.grid(ramp, bounds, area, cols, rows, [0..cols, 0..rows], fill, true))
|
||||
}
|
||||
/// `ramp` sampled at each cell's center in columns `cells[0]` and rows `cells[1]` of the grid,
|
||||
/// its last channel (opacity) rounded and each other channel with dither's noise, except
|
||||
/// (`inks`) where that channel is empty.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn grid<const N: usize>(
|
||||
&self,
|
||||
ramp: Ramp<N>,
|
||||
bounds: Rect,
|
||||
area: Rect,
|
||||
cols: u16,
|
||||
rows: u16,
|
||||
cells: [std::ops::Range<u16>; 2],
|
||||
fill: bool,
|
||||
inks: bool,
|
||||
) -> impl Iterator<Item = [u8; N]> + use<N> {
|
||||
let (kind, geom, dither) = (self.gradient.kind, self.resolve(bounds), fill && self.gradient.dither);
|
||||
let ramp = Ramp::new(&self.gradient, rgb);
|
||||
let (cw, ch) = (area.width() / f64::from(cols.max(1)), area.height() / f64::from(rows.max(1)));
|
||||
let (x0, y0) = (area.x0, area.y0);
|
||||
let [across, down] = cells.map(|r| u32::from(r.start)..u32::from(r.end));
|
||||
down.flat_map(move |j| across.clone().map(move |i| (i, j))).map(move |(i, j)| {
|
||||
let p = Point::new(x0 + (f64::from(i) + 0.5) * cw, y0 + (f64::from(j) + 0.5) * ch);
|
||||
let c = ramp.at(geom.param_at(kind, p) as f32);
|
||||
let level = |k: usize| {
|
||||
let noise = if dither { dither_noise(i, j, k as u32) } else { 0.0 };
|
||||
std::array::from_fn(|k| {
|
||||
if k + 1 == N {
|
||||
return (c[k].clamp(0.0, 1.0) * 255.0).round() as u8;
|
||||
}
|
||||
let noise = if dither && !(inks && c[k] <= 0.0) { dither_noise(i, j, k as u32) } else { 0.0 };
|
||||
(c[k].clamp(0.0, 1.0) * 255.0 + noise).round().clamp(0.0, 255.0) as u8
|
||||
};
|
||||
[level(0), level(1), level(2), (c[3].clamp(0.0, 1.0) * 255.0).round() as u8]
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -916,9 +977,9 @@ impl GradientPaint {
|
||||
/// (display RGB, or an ink's coverage): its stops, midpoints and a Perceptual gradient's
|
||||
/// in-between colours as stops (as [`Gradient::expanded_stops`]), each turned into that space
|
||||
/// once, with colour and opacity mixed linearly between them, each on its own (as the renderer's
|
||||
/// vector gradients mix them).
|
||||
pub struct Ramp {
|
||||
stops: Vec<(f32, [f32; 4])>,
|
||||
/// vector gradients mix them). `N` counts the channels, with opacity last.
|
||||
pub struct Ramp<const N: usize = 4> {
|
||||
stops: Vec<(f32, [f32; N])>,
|
||||
}
|
||||
|
||||
impl Ramp {
|
||||
@@ -932,10 +993,13 @@ impl Ramp {
|
||||
.collect();
|
||||
Self { stops }
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> Ramp<N> {
|
||||
/// Unpremultiplied colour and opacity at `t`, padded past the ends (transparent black
|
||||
/// without stops).
|
||||
pub fn at(&self, t: f32) -> [f32; 4] {
|
||||
let Some(&(t0, first)) = self.stops.first() else { return [0.0; 4] };
|
||||
pub fn at(&self, t: f32) -> [f32; N] {
|
||||
let Some(&(t0, first)) = self.stops.first() else { return [0.0; N] };
|
||||
// Not after the first stop (or not a number): its colour.
|
||||
if t.partial_cmp(&t0) != Some(std::cmp::Ordering::Greater) {
|
||||
return first;
|
||||
@@ -947,7 +1011,7 @@ impl Ramp {
|
||||
return std::array::from_fn(|k| ca[k] + (cb[k] - ca[k]) * u);
|
||||
}
|
||||
}
|
||||
self.stops.last().map_or([0.0; 4], |s| s.1)
|
||||
self.stops.last().map_or([0.0; N], |s| s.1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -604,3 +604,96 @@ fn ink_mixes_use_the_mix_position_when_the_lightness_turns_back() {
|
||||
}
|
||||
assert!(mixes.windows(2).filter(|p| p[1].1 == 0).all(|p| p[0].2 <= p[1].2), "the weights rise: {mixes:?}");
|
||||
}
|
||||
|
||||
fn cmyk_stop(offset: f32, c: f32, m: f32, y: f32, k: f32) -> GradientStop {
|
||||
GradientStop::new(offset, Color::cmyk(c, m, y, k))
|
||||
}
|
||||
|
||||
/// The `n` × 1 grid of `g` as a left-to-right linear gradient's ink pixels.
|
||||
fn ink_row(g: Gradient, n: u16, fill: bool) -> Vec<[u8; 5]> {
|
||||
let b = Rect::new(0.0, 0.0, f64::from(n), 1.0);
|
||||
GradientPaint::new(g).ink_pixels(b, b, n, 1, fill).expect("CMYK stops").collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ink_pixels_mix_the_stops_inks_in_cmyk() {
|
||||
let mut g = Gradient::new(GradientKind::Linear, vec![cmyk_stop(0.0, 1.0, 0.0, 0.0, 0.0), cmyk_stop(1.0, 0.0, 0.0, 0.0, 1.0)]);
|
||||
g.stops[1].opacity = 0.5;
|
||||
let px = ink_row(g.clone(), 10, true);
|
||||
let at = |x: f32| (x * 255.0).round() as u8;
|
||||
for (i, p) in px.iter().enumerate() {
|
||||
let t = (i as f32 + 0.5) / 10.0;
|
||||
assert_eq!(*p, [at(1.0 - t), 0, 0, at(t), at(1.0 - t * 0.5)], "pixel {i}");
|
||||
}
|
||||
// An off-center midpoint is halfway between the two stops' inks, not a display RGB mix.
|
||||
g.stops[0].midpoint = 0.25;
|
||||
let px = ink_row(g.clone(), 4, false);
|
||||
assert_eq!(&px[0][..4], &[at(0.75), 0, 0, at(0.25)], "halfway to the midpoint");
|
||||
assert_eq!(&px[1][..4], &[at(0.5 * 5.0 / 6.0), 0, 0, at(0.5 + 0.5 / 6.0)], "a sixth of the way past it");
|
||||
// A hard stop keeps both sides.
|
||||
let hard = Gradient::new(
|
||||
GradientKind::Linear,
|
||||
vec![
|
||||
cmyk_stop(0.0, 1.0, 0.0, 0.0, 0.0),
|
||||
cmyk_stop(0.5, 1.0, 0.0, 0.0, 0.0),
|
||||
cmyk_stop(0.5, 0.0, 1.0, 0.0, 0.0),
|
||||
cmyk_stop(1.0, 0.0, 1.0, 0.0, 0.0),
|
||||
],
|
||||
);
|
||||
let px = ink_row(hard, 4, false);
|
||||
assert_eq!((&px[1][..4], &px[2][..4]), (&[255, 0, 0, 0][..], &[0, 255, 0, 0][..]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ink_pixels_dither_only_the_inks_the_ramp_has() {
|
||||
let mut g = Gradient::new(GradientKind::Linear, vec![cmyk_stop(0.0, 0.2, 0.0, 0.0, 0.4), cmyk_stop(1.0, 0.8, 0.0, 0.0, 0.4)]);
|
||||
g.dither = true;
|
||||
let noisy = ink_row(g.clone(), 64, true);
|
||||
let plain = ink_row(g.clone(), 64, false);
|
||||
assert_ne!(noisy, plain, "a fill has noise, a stroke none");
|
||||
for (n, p) in noisy.iter().zip(&plain) {
|
||||
assert_eq!((n[1], n[2]), (0, 0), "no ink on the empty plates: {n:?}");
|
||||
assert!(n[0].abs_diff(p[0]) <= 1 && n[3].abs_diff(p[3]) <= 1, "{n:?} {p:?}");
|
||||
assert_eq!(n[4], 255, "opacity has no noise");
|
||||
}
|
||||
assert!(noisy.iter().zip(&plain).any(|(n, p)| n[3] != p[3]), "K gets its own noise");
|
||||
assert!(noisy.iter().zip(&plain).any(|(n, p)| (n[0] != p[0]) != (n[3] != p[3])), "the noise differs from channel to channel");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ink_pixels_follow_the_perceptual_curve_as_ink_mixes() {
|
||||
let mut g = Gradient::new(GradientKind::Linear, vec![cmyk_stop(0.0, 0.0, 0.0, 0.0, 0.0), cmyk_stop(1.0, 1.0, 0.0, 0.0, 0.0)]);
|
||||
g.method = GradientMethod::Perceptual;
|
||||
let mixes = g.ink_mixes().unwrap();
|
||||
let ramp = g.ink_ramp().unwrap();
|
||||
assert_eq!(ramp.stops.len(), mixes.len());
|
||||
for ((t, ink), (o, i, w, _)) in ramp.stops.iter().zip(&mixes) {
|
||||
assert_eq!((*t, ink[0]), (*o, if *i == 0 { *w } else { 1.0 }), "cyan at {t}");
|
||||
}
|
||||
let px = ink_row(g, 2, false);
|
||||
assert!(px[0][0] > 0 && px[0][0] < px[1][0], "{px:?}");
|
||||
assert!(px.iter().all(|p| p[1] == 0 && p[2] == 0 && p[3] == 0), "{px:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ink_pixels_need_every_stop_in_cmyk() {
|
||||
let b = Rect::new(0.0, 0.0, 2.0, 1.0);
|
||||
let with = |stop: GradientStop| GradientPaint::new(Gradient::new(GradientKind::Linear, vec![cmyk_stop(0.0, 1.0, 0.0, 0.0, 0.0), stop]));
|
||||
assert!(with(cmyk_stop(1.0, 0.0, 0.0, 0.0, 1.0)).ink_pixels(b, b, 2, 1, true).is_some());
|
||||
for other in [Color::rgb(1.0, 0.0, 0.0), Color::gray(0.5), Color::lab(50.0, 10.0, 10.0)] {
|
||||
assert!(with(GradientStop::new(1.0, other)).ink_pixels(b, b, 2, 1, true).is_none(), "{other:?}");
|
||||
}
|
||||
assert!(GradientPaint::new(Gradient::new(GradientKind::Linear, vec![])).ink_pixels(b, b, 2, 1, true).is_none(), "no stops");
|
||||
}
|
||||
|
||||
/// A Perceptual curve over the limit of [`Gradient::ink_mixes`] has no inks; a Classic gradient of as many stops does.
|
||||
#[test]
|
||||
fn ink_pixels_stop_at_the_ink_mixes_limit() {
|
||||
let stops: Vec<_> = (0..=MAX_INK_TINTS).map(|i| cmyk_stop(i as f32 / MAX_INK_TINTS as f32, (i % 2) as f32, 0.0, 0.0, 0.0)).collect();
|
||||
let classic = Gradient::new(GradientKind::Linear, stops);
|
||||
let perceptual = Gradient { method: GradientMethod::Perceptual, ..classic.clone() };
|
||||
assert!(!perceptual.fits_ink_tints());
|
||||
let b = Rect::new(0.0, 0.0, 2.0, 1.0);
|
||||
assert!(GradientPaint::new(perceptual).ink_pixels(b, b, 2, 1, false).is_none());
|
||||
assert!(GradientPaint::new(classic).ink_pixels(b, b, 2, 1, false).is_some());
|
||||
}
|
||||
+42
-30
@@ -143,38 +143,11 @@ impl Color {
|
||||
}
|
||||
/// HSB with hue in degrees 0..360, saturation and brightness 0..1.
|
||||
pub fn to_hsb(&self) -> [f32; 3] {
|
||||
let [r, g, b] = self.to_rgb();
|
||||
let max = r.max(g).max(b);
|
||||
let min = r.min(g).min(b);
|
||||
let d = max - min;
|
||||
let h = if d <= 0.0 {
|
||||
0.0
|
||||
} else if max == r {
|
||||
60.0 * (((g - b) / d).rem_euclid(6.0))
|
||||
} else if max == g {
|
||||
60.0 * ((b - r) / d + 2.0)
|
||||
} else {
|
||||
60.0 * ((r - g) / d + 4.0)
|
||||
};
|
||||
// f32 rounding in rem_euclid can yield exactly 360; wrap back to 0.
|
||||
let h = if h >= 360.0 { 0.0 } else { h };
|
||||
let s = if max <= 0.0 { 0.0 } else { d / max };
|
||||
[h, s, max]
|
||||
rgb_to_hsb(self.to_rgb())
|
||||
}
|
||||
pub fn from_hsb(h: f32, s: f32, v: f32) -> Self {
|
||||
let h = h.rem_euclid(360.0) / 60.0;
|
||||
let c = v * s;
|
||||
let x = c * (1.0 - (h % 2.0 - 1.0).abs());
|
||||
let (r, g, b) = match h as u32 {
|
||||
0 => (c, x, 0.0),
|
||||
1 => (x, c, 0.0),
|
||||
2 => (0.0, c, x),
|
||||
3 => (0.0, x, c),
|
||||
4 => (x, 0.0, c),
|
||||
_ => (c, 0.0, x),
|
||||
};
|
||||
let m = v - c;
|
||||
Color::Rgb { r: r + m, g: g + m, b: b + m }
|
||||
let [r, g, b] = hsb_to_rgb([h, s, v]);
|
||||
Color::Rgb { r, g, b }
|
||||
}
|
||||
/// `#rrggbb` (display RGB).
|
||||
pub fn to_hex(&self) -> String {
|
||||
@@ -327,6 +300,45 @@ impl Color {
|
||||
}
|
||||
}
|
||||
|
||||
/// HSB (hue in degrees 0..360, saturation and brightness 0..1) of RGB numbers 0..1, with no color
|
||||
/// conversion ([`Color::to_hsb`] reads the display color first).
|
||||
pub fn rgb_to_hsb([r, g, b]: [f32; 3]) -> [f32; 3] {
|
||||
let max = r.max(g).max(b);
|
||||
let min = r.min(g).min(b);
|
||||
let d = max - min;
|
||||
let h = if d <= 0.0 {
|
||||
0.0
|
||||
} else if max == r {
|
||||
60.0 * (((g - b) / d).rem_euclid(6.0))
|
||||
} else if max == g {
|
||||
60.0 * ((b - r) / d + 2.0)
|
||||
} else {
|
||||
60.0 * ((r - g) / d + 4.0)
|
||||
};
|
||||
// f32 rounding in rem_euclid can yield exactly 360; wrap back to 0.
|
||||
let h = if h >= 360.0 { 0.0 } else { h };
|
||||
let s = if max <= 0.0 { 0.0 } else { d / max };
|
||||
[h, s, max]
|
||||
}
|
||||
|
||||
/// RGB numbers 0..1 of HSB `[h, s, v]` (hue in degrees, saturation and brightness 0..1): the inverse
|
||||
/// of [`rgb_to_hsb`].
|
||||
pub fn hsb_to_rgb([h, s, v]: [f32; 3]) -> [f32; 3] {
|
||||
let h = h.rem_euclid(360.0) / 60.0;
|
||||
let c = v * s;
|
||||
let x = c * (1.0 - (h % 2.0 - 1.0).abs());
|
||||
let (r, g, b) = match h as u32 {
|
||||
0 => (c, x, 0.0),
|
||||
1 => (x, c, 0.0),
|
||||
2 => (0.0, c, x),
|
||||
3 => (0.0, x, c),
|
||||
4 => (x, 0.0, c),
|
||||
_ => (c, 0.0, x),
|
||||
};
|
||||
let m = v - c;
|
||||
[r + m, g + m, b + m]
|
||||
}
|
||||
|
||||
/// `new` expressed in the colour model of `orig` ([`Color::in_model`]): results of colour operations
|
||||
/// (harmonies, blends, inversions, recolouring) keep the model of the colour they came from.
|
||||
pub fn keep_model(orig: Color, new: Color) -> Color {
|
||||
|
||||
@@ -8,7 +8,6 @@ mod layers;
|
||||
mod profile;
|
||||
mod stroke_style;
|
||||
|
||||
pub(crate) use layers::HEAD_REACH;
|
||||
pub use layers::{Effect, FillLayer, StrokeGradientMode, StrokeLayer, stroke_paint_bounds};
|
||||
pub use profile::{AngleProfile, PressureProfile, ProfilePreset, SavedProfile, WidthProfile};
|
||||
pub use stroke_style::{ArrowAlign, Arrowhead, Dash, LineCap, LineJoin, StrokeAlign};
|
||||
|
||||
@@ -139,10 +139,6 @@ impl StrokeGradientMode {
|
||||
}
|
||||
}
|
||||
|
||||
/// How far any arrowhead reaches from its tip, in units of its weight (the open arrow's arms are
|
||||
/// cut square a little behind its 4 × 4 box).
|
||||
pub(crate) const HEAD_REACH: f64 = 4.6;
|
||||
|
||||
fn ten() -> f64 {
|
||||
10.0
|
||||
}
|
||||
@@ -188,7 +184,8 @@ impl StrokeLayer {
|
||||
}
|
||||
}
|
||||
/// Weight of the start (`end == false`) or end arrowhead: stroke weight × its scale, at least
|
||||
/// a quarter point. A head of weight `hw` fits a box about `4·hw` long and wide.
|
||||
/// a quarter point. A head of weight `hw` fits a box about `4·hw` long and wide (see
|
||||
/// [`Arrowhead::reach`] and [`Arrowhead::half_width`]).
|
||||
pub fn arrow_weight(&self, end: bool) -> f64 {
|
||||
let pct = if end { self.arrow_scale.1 } else { self.arrow_scale.0 };
|
||||
(self.width * pct / 100.0).max(0.25)
|
||||
@@ -197,7 +194,7 @@ impl StrokeLayer {
|
||||
/// without one): the head's diagonal, or with [`ArrowAlign::Extend`] its length, plus the cap.
|
||||
pub fn head_reach(&self, end: bool) -> f64 {
|
||||
let head = if end { self.end_arrow } else { self.start_arrow };
|
||||
head.map_or(0.0, |_| HEAD_REACH * self.arrow_weight(end) + self.width / 2.0)
|
||||
head.map_or(0.0, |k| k.reach() * self.arrow_weight(end) + self.width / 2.0)
|
||||
}
|
||||
/// How far the arrowheads can reach from the path's end points (0 without heads).
|
||||
pub fn arrow_reach(&self) -> f64 {
|
||||
|
||||
@@ -49,6 +49,10 @@ impl Dash {
|
||||
}
|
||||
}
|
||||
|
||||
/// How far the arrowheads drawn in the 4 × 4 box reach from their tip, in units of their weight
|
||||
/// (the open arrow's arms are cut square a little behind the box).
|
||||
const HEAD_REACH: f64 = 4.6;
|
||||
|
||||
/// An arrowhead shape. Saved by variant name: new shapes are appended, never renamed or reordered.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub enum Arrowhead {
|
||||
@@ -94,13 +98,18 @@ pub enum Arrowhead {
|
||||
Kite,
|
||||
TriangleBar,
|
||||
Oval,
|
||||
/// Two arms of the head's weight (the line's weight at 100 % scale, for lines of 0.25 pt or
|
||||
/// more) meeting at right angles in a sharp tip, cut square at their ends. Twice as wide as
|
||||
/// the other heads.
|
||||
LineArrow,
|
||||
}
|
||||
|
||||
impl Arrowhead {
|
||||
/// Every arrowhead, in menu order (arrows, then outlined shapes, then bars and marks).
|
||||
pub const ALL: [Arrowhead; 40] = [
|
||||
pub const ALL: [Arrowhead; 41] = [
|
||||
Arrowhead::Arrow,
|
||||
Arrowhead::ArrowOpen,
|
||||
Arrowhead::LineArrow,
|
||||
Arrowhead::Barbed,
|
||||
Arrowhead::Concave,
|
||||
Arrowhead::DoubleArrow,
|
||||
@@ -141,6 +150,22 @@ impl Arrowhead {
|
||||
Arrowhead::Fork,
|
||||
];
|
||||
|
||||
/// How far any part of the head can be from its tip, in units of its weight.
|
||||
pub fn reach(self) -> f64 {
|
||||
match self {
|
||||
// Its farthest corners are 4 + ½√2 back and 4 − ½√2 out.
|
||||
Arrowhead::LineArrow => 5.75,
|
||||
_ => HEAD_REACH,
|
||||
}
|
||||
}
|
||||
/// How far the head spreads either side of its line, in units of its weight.
|
||||
pub fn half_width(self) -> f64 {
|
||||
match self {
|
||||
Arrowhead::LineArrow => 4.0,
|
||||
_ => 2.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// The name the arrowhead menus show.
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
@@ -184,6 +209,7 @@ impl Arrowhead {
|
||||
Arrowhead::Kite => "Kite",
|
||||
Arrowhead::TriangleBar => "Triangle to Bar",
|
||||
Arrowhead::Oval => "Oval",
|
||||
Arrowhead::LineArrow => "Line Arrow",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ fn profile_presets_round_trip_their_ids() {
|
||||
#[test]
|
||||
fn arrowhead_names_are_stable_and_every_one_is_listed_once() {
|
||||
// Saved files store these names: they may only ever be appended to.
|
||||
const SAVED: [&str; 40] = [
|
||||
const SAVED: [&str; 41] = [
|
||||
"Triangle",
|
||||
"TriangleOpen",
|
||||
"Circle",
|
||||
@@ -260,6 +260,7 @@ fn arrowhead_names_are_stable_and_every_one_is_listed_once() {
|
||||
"Kite",
|
||||
"TriangleBar",
|
||||
"Oval",
|
||||
"LineArrow",
|
||||
];
|
||||
for name in SAVED {
|
||||
let a: Arrowhead = serde_json::from_value(serde_json::json!(name)).unwrap();
|
||||
@@ -293,9 +294,15 @@ fn outset_covers_arrowheads() {
|
||||
st.arrow_scale = (100.0, 200.0);
|
||||
assert_eq!(st.arrow_weight(false), 4.0);
|
||||
assert_eq!(st.arrow_weight(true), 8.0);
|
||||
assert_eq!((st.head_reach(false), st.head_reach(true)), (0.0, HEAD_REACH * 8.0 + 2.0));
|
||||
let reach = Arrowhead::Triangle.reach();
|
||||
assert_eq!((st.head_reach(false), st.head_reach(true)), (0.0, reach * 8.0 + 2.0));
|
||||
// A 32 pt head whose tip sits up to its length (plus the cap) past the end point.
|
||||
assert_eq!(a.outset(), HEAD_REACH * 8.0 + 2.0);
|
||||
assert_eq!(a.outset(), reach * 8.0 + 2.0);
|
||||
// The line arrow's arms spread twice as wide, so it reaches further from its tip.
|
||||
a.stroke_mut().unwrap().end_arrow = Some(Arrowhead::LineArrow);
|
||||
assert!(Arrowhead::LineArrow.reach() > reach);
|
||||
assert_eq!(a.outset(), Arrowhead::LineArrow.reach() * 8.0 + 2.0);
|
||||
a.stroke_mut().unwrap().end_arrow = Some(Arrowhead::Triangle);
|
||||
a.stroke_mut().unwrap().width = 0.01;
|
||||
assert_eq!(a.stroke().unwrap().arrow_weight(true), 0.25, "heads keep a minimum size");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{Arc, Deserialize, Node, NodeId, NodeKind, Rect, Serialize, SymbolBoxes, skip};
|
||||
use super::{Arc, Deserialize, LayerColor, Node, NodeId, NodeKind, Rect, Serialize, SymbolBoxes, skip};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Artboard {
|
||||
@@ -39,6 +39,18 @@ pub struct Artboard {
|
||||
/// it behind the art ([`super::Document::with_artboard_fills`]).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub color: Option<vectorcraft_color::Color>,
|
||||
/// Guide label (Artboard Options): the color the canvas draws the artboard's own ruler guides
|
||||
/// in (a guide whose [`super::Guide::artboard`] is this artboard's id), a [`LayerColor`] preset
|
||||
/// or a custom RGB. None (No label) draws them in the guide color preference, as canvas guides
|
||||
/// are. Only the canvas uses it: the paper, exports and print don't. Written only when set; a
|
||||
/// file's preset outside [`super::LAYER_COLORS`] reads as no label.
|
||||
#[serde(default, deserialize_with = "label_within_palette", skip_serializing_if = "Option::is_none")]
|
||||
pub guide_label: Option<LayerColor>,
|
||||
}
|
||||
|
||||
/// A file's guide label: a preset index outside [`super::LAYER_COLORS`] reads as no label.
|
||||
fn label_within_palette<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<LayerColor>, D::Error> {
|
||||
Ok(Option::<LayerColor>::deserialize(d)?.filter(|c| !matches!(c, LayerColor::Preset(i) if usize::from(*i) >= super::LAYER_COLORS.len())))
|
||||
}
|
||||
|
||||
/// The Video Ruler Pixel Aspect Ratios an artboard takes (Artboard Options): from a pixel ten times
|
||||
|
||||
@@ -750,6 +750,7 @@ fn lerp_text(o: &mut TextObject, x: &TextObject, y: &TextObject, t: f64, tc: f64
|
||||
if resized {
|
||||
o.cached_bounds = None;
|
||||
o.cached_baselines.clear();
|
||||
o.cached_ink = None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -904,12 +905,16 @@ pub struct Spine {
|
||||
cum: Vec<f64>,
|
||||
anchors: Vec<f64>,
|
||||
closed: bool,
|
||||
path: PathData,
|
||||
/// The subpath the spine was flattened from, kept so [`Spine::at_length`] can read the
|
||||
/// original curves' tangents. Only the first subpath is stored: the others never affect a
|
||||
/// spine.
|
||||
subpath: SubPath,
|
||||
}
|
||||
|
||||
impl Spine {
|
||||
/// Build a spine from the first subpath of `path`: flatten it to a polyline, record cumulative
|
||||
/// arc lengths, and the arc length at each anchor.
|
||||
/// arc lengths, and the arc length at each anchor. `None` when `path` has no subpath, or when
|
||||
/// the first one flattens to fewer than two points (so it has no length to walk).
|
||||
pub fn new(path: &PathData) -> Option<Self> {
|
||||
let sp = path.subpaths.first()?;
|
||||
let mut pts: Vec<Point> = Vec::new();
|
||||
@@ -946,7 +951,7 @@ impl Spine {
|
||||
if pts.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
Some(Self { pts, cum, anchors, closed: sp.closed, path: path.clone() })
|
||||
Some(Self { pts, cum, anchors, closed: sp.closed, subpath: sp.clone() })
|
||||
}
|
||||
/// The spine's flattened length in arc length.
|
||||
pub fn length(&self) -> f64 {
|
||||
@@ -1012,7 +1017,7 @@ impl Spine {
|
||||
/// `Align to Path` jump between chords instead of following the curve smoothly.
|
||||
pub fn at_length(&self, s: f64) -> (Point, f64) {
|
||||
let s = if s.is_finite() { s.clamp(0.0, self.length()) } else { 0.0 };
|
||||
let Some(sp) = self.path.subpaths.first() else { return (self.pts.first().copied().unwrap_or_default(), 0.0) };
|
||||
let sp = &self.subpath;
|
||||
let n = sp.segment_count();
|
||||
let mut start = 0.0;
|
||||
for i in 0..n {
|
||||
|
||||
+436
-15
@@ -1,7 +1,7 @@
|
||||
//! Persistent dimension recipes. Children are ordinary paths and type for all renderers/exporters.
|
||||
use crate::{Document, NodeId, Unit};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use vectorcraft_geom::{Affine, Point, Vec2};
|
||||
use vectorcraft_geom::{Affine, Anchor, PathData, Point, Vec2};
|
||||
|
||||
/// What a dimension measures.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
@@ -25,6 +25,17 @@ impl DimensionMode {
|
||||
}
|
||||
}
|
||||
|
||||
/// The direction a linear dimension measures in: along the line through its two points, or only
|
||||
/// their horizontal or vertical distance.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Orientation {
|
||||
#[default]
|
||||
Aligned,
|
||||
Horizontal,
|
||||
Vertical,
|
||||
}
|
||||
|
||||
/// The unit a length is labelled in.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
@@ -295,6 +306,12 @@ pub enum DimensionSource {
|
||||
a: Option<PointRef>,
|
||||
b: Option<PointRef>,
|
||||
vertex: Option<PointRef>,
|
||||
/// The first point when it is not an anchor, held against a path's anchors.
|
||||
#[serde(default, rename = "freeA", skip_serializing_if = "Option::is_none")]
|
||||
free_a: Option<FreePoint>,
|
||||
/// The second point when it is not an anchor.
|
||||
#[serde(default, rename = "freeB", skip_serializing_if = "Option::is_none")]
|
||||
free_b: Option<FreePoint>,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -304,7 +321,11 @@ impl DimensionSource {
|
||||
Self::Segment { edge } | Self::Arc { edge } => vec![edge.node],
|
||||
Self::Angle { first, second } => vec![first.node, second.node],
|
||||
Self::Circle { node } => vec![*node],
|
||||
Self::Points { a, b, vertex } => [a, b, vertex].into_iter().filter_map(|p| p.as_ref().map(|r| r.node)).collect(),
|
||||
Self::Points { a, b, vertex, free_a, free_b } => [a, b, vertex]
|
||||
.into_iter()
|
||||
.filter_map(|p| p.as_ref().map(|r| r.node))
|
||||
.chain([free_a, free_b].into_iter().filter_map(|p| p.as_ref().map(FreePoint::node)))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
/// Anchor indexes cannot safely follow a topology replacement. Ordinary moves and handle
|
||||
@@ -321,6 +342,34 @@ impl DimensionSource {
|
||||
old.is_some() && old != counts(after)
|
||||
})
|
||||
}
|
||||
/// The map that took the measured art from `before` to `after`, when every path it measures
|
||||
/// went through one [`art_transform`] that reflects it (a negative determinant). None when a
|
||||
/// measured path is unchanged, or when two of them went through different maps.
|
||||
pub fn reflection(&self, before: &Document, after: &Document) -> Option<Affine> {
|
||||
let mut first: Option<Affine> = None;
|
||||
for id in self.nodes() {
|
||||
let (old, new) = (before.node(id)?, after.node(id)?);
|
||||
if std::ptr::eq(old, new) || old.path_data() == new.path_data() {
|
||||
return None;
|
||||
}
|
||||
let xf = fitted(before, after, id, |xf| xf.determinant() < 0.0)?;
|
||||
match first {
|
||||
None => first = Some(xf),
|
||||
Some(f) => {
|
||||
// The linear parts and the translations, each against its own size.
|
||||
let (a, b) = (f.as_coeffs(), xf.as_coeffs());
|
||||
let differ = |range: std::ops::Range<usize>, tolerance: f64| {
|
||||
let size = a[range.clone()].iter().map(|c| c.abs()).fold(1.0, f64::max);
|
||||
a[range.clone()].iter().zip(&b[range]).any(|(p, q)| (p - q).abs() > tolerance * size)
|
||||
};
|
||||
if differ(0..4, 1e-9) || differ(4..6, 1e-9) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
first
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
@@ -337,6 +386,235 @@ impl PointRef {
|
||||
}
|
||||
}
|
||||
|
||||
/// The point of an anchor that an [`AnchorWeight`] names: the anchor, or one of its handles.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum AnchorPart {
|
||||
#[default]
|
||||
Point,
|
||||
In,
|
||||
Out,
|
||||
}
|
||||
|
||||
impl AnchorPart {
|
||||
fn of(self, a: &Anchor) -> Point {
|
||||
match self {
|
||||
Self::Point => a.p,
|
||||
Self::In => a.h_in,
|
||||
Self::Out => a.h_out,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One anchor's share of a [`FreePoint`].
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AnchorWeight {
|
||||
pub subpath: usize,
|
||||
pub anchor: usize,
|
||||
/// The anchor itself, or its in or out handle.
|
||||
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
|
||||
pub part: AnchorPart,
|
||||
pub weight: f64,
|
||||
}
|
||||
|
||||
/// A measured point that is not an anchor (a point on a segment, or a point a Smart Guide gave),
|
||||
/// linked to one path so that it moves with it.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind", rename_all = "camelCase")]
|
||||
pub enum FreePoint {
|
||||
/// A point on a segment at curve parameter `t`, 0 at the segment's first anchor and 1 at its
|
||||
/// last. It stays on the segment when the segment's anchors or handles are edited.
|
||||
OnSegment { edge: SegmentRef, t: f64 },
|
||||
/// A weighted sum of two or three anchors or handles of one path, the weights adding up to 1,
|
||||
/// plus `across` times the vector from the first anchor to the second turned a quarter turn,
|
||||
/// from (x, y) to (-y, x), so that a vector pointing right in the y-down document space
|
||||
/// turns to point down (two anchors only). When the
|
||||
/// path is moved, scaled, rotated, reflected or sheared, a point held by three moves with
|
||||
/// it. A point held by two and `across`, beside a straight path, moves, turns and scales
|
||||
/// uniformly with it, and a reflection or a non-uniform scale moves it as a half turn or a
|
||||
/// uniform scale would.
|
||||
Anchors {
|
||||
node: NodeId,
|
||||
anchors: Vec<AnchorWeight>,
|
||||
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
|
||||
across: f64,
|
||||
},
|
||||
}
|
||||
|
||||
/// A point of a path: (subpath, anchor, part, place).
|
||||
type PathPoint = (usize, usize, AnchorPart, Point);
|
||||
|
||||
/// Every anchor of `path` in order, each followed by its in and out handles when `handles` is set.
|
||||
fn path_points(path: &PathData, handles: bool) -> impl Iterator<Item = PathPoint> + Clone + '_ {
|
||||
let parts: &'static [AnchorPart] = if handles { &[AnchorPart::Point, AnchorPart::In, AnchorPart::Out] } else { &[AnchorPart::Point] };
|
||||
path.subpaths
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(move |(si, sp)| sp.anchors.iter().enumerate().flat_map(move |(ai, a)| parts.iter().map(move |&part| (si, ai, part, part.of(a)))))
|
||||
}
|
||||
|
||||
/// Three points that span `points`: the first, the one farthest from it, and the one farthest
|
||||
/// from the line through those two. None when every point is in one place.
|
||||
fn spanning(points: impl Iterator<Item = PathPoint> + Clone) -> Option<[PathPoint; 3]> {
|
||||
let first = points.clone().next()?;
|
||||
let o = first.3;
|
||||
let second = points.clone().max_by(|x, y| x.3.distance(o).total_cmp(&y.3.distance(o)))?;
|
||||
let u = second.3 - o;
|
||||
let span = u.hypot();
|
||||
if !span.is_finite() || span <= 1e-6 {
|
||||
return None;
|
||||
}
|
||||
let third = points.max_by(|x, y| u.cross(x.3 - o).abs().total_cmp(&u.cross(y.3 - o).abs()))?;
|
||||
Some([first, second, third])
|
||||
}
|
||||
|
||||
/// The map that takes the anchors and handles of path `node` in `before` to where they are in
|
||||
/// `after`, when one affine map does that for every one of them: the path was moved, scaled,
|
||||
/// rotated, reflected or sheared as a whole. None when its anchors changed in number, moved in
|
||||
/// another way, or lie on one line with all their handles, as on a straight line: a reflection
|
||||
/// across a line through such a path puts its points where a turn can put them too.
|
||||
pub fn art_transform(before: &Document, after: &Document, node: NodeId) -> Option<Affine> {
|
||||
fitted(before, after, node, |_| true)
|
||||
}
|
||||
|
||||
/// [`art_transform`] when the map passes `wanted`. The map is found from three points, and
|
||||
/// `wanted` is asked before every other point is checked against it.
|
||||
fn fitted(before: &Document, after: &Document, node: NodeId, wanted: impl Fn(&Affine) -> bool) -> Option<Affine> {
|
||||
let (old, new) = (before.node(node)?.path_data()?, after.node(node)?.path_data()?);
|
||||
if old.subpaths.len() != new.subpaths.len() || old.subpaths.iter().zip(&new.subpaths).any(|(o, n)| o.anchors.len() != n.anchors.len()) {
|
||||
return None;
|
||||
}
|
||||
let [o, f, g] = spanning(path_points(old, true))?;
|
||||
let place = |q: PathPoint| new.subpaths.get(q.0).and_then(|sp| sp.anchors.get(q.1)).map(|a| q.2.of(a));
|
||||
let (o2, f2, g2) = (place(o)?, place(f)?, place(g)?);
|
||||
let (u, v) = (f.3 - o.3, g.3 - o.3);
|
||||
let span = u.hypot();
|
||||
if u.cross(v).abs() <= 1e-9 * span * span {
|
||||
return None;
|
||||
}
|
||||
let basis = |u: Vec2, v: Vec2| Affine::new([u.x, u.y, v.x, v.y, 0.0, 0.0]);
|
||||
let linear = basis(f2 - o2, g2 - o2) * basis(u, v).inverse();
|
||||
let xf = Affine::translate(o2.to_vec2()) * linear * Affine::translate(-o.3.to_vec2());
|
||||
if !xf.as_coeffs().iter().all(|c| c.is_finite()) || !wanted(&xf) {
|
||||
return None;
|
||||
}
|
||||
let tolerance = 1e-6 * span.max(1.0);
|
||||
path_points(old, true).zip(path_points(new, true)).all(|(o, n)| (xf * o.3).distance(n.3) <= tolerance).then_some(xf)
|
||||
}
|
||||
|
||||
impl FreePoint {
|
||||
/// Largest weight: a point far outside the anchors that hold it is not held.
|
||||
const MAX_WEIGHT: f64 = 1e4;
|
||||
|
||||
/// The path it is linked to.
|
||||
pub fn node(&self) -> NodeId {
|
||||
match self {
|
||||
Self::OnSegment { edge, .. } => edge.node,
|
||||
Self::Anchors { node, .. } => *node,
|
||||
}
|
||||
}
|
||||
|
||||
/// `p` held against path `node`: three anchors that span the path; when all its anchors lie
|
||||
/// on one line or in one place, as on an arch, three of its anchors and handles; when its
|
||||
/// handles lie on that line too, or so nearly that the weights would be too large, two anchors
|
||||
/// and `p`'s distance across that line. None when none of these holds `p`.
|
||||
pub fn on(doc: &Document, node: NodeId, p: Point) -> Option<Self> {
|
||||
let path = doc.node(node)?.path_data()?;
|
||||
let weight = |(subpath, anchor, part, _): PathPoint, weight| AnchorWeight { subpath, anchor, part, weight };
|
||||
for handles in [false, true] {
|
||||
let Some([o, f, g]) = spanning(path_points(path, handles)) else { continue };
|
||||
let (u, v) = (f.3 - o.3, g.3 - o.3);
|
||||
let span = u.hypot();
|
||||
let det = u.cross(v);
|
||||
if det.abs() > 1e-9 * span * span {
|
||||
let s = (p - o.3).cross(v) / det;
|
||||
let t = u.cross(p - o.3) / det;
|
||||
let anchors = vec![weight(o, 1.0 - s - t), weight(f, s), weight(g, t)];
|
||||
// A nearly flat set of points gives weights too large to keep: the next fit is tried.
|
||||
if let Some(held) = (Self::Anchors { node, anchors, across: 0.0 }).holding(doc, p, span) {
|
||||
return Some(held);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Every anchor and handle on one line, or nearly: along it from the first anchor, and across it.
|
||||
let [o, f, _] = spanning(path_points(path, false))?;
|
||||
let u = f.3 - o.3;
|
||||
let span = u.hypot();
|
||||
let (t, across) = ((p - o.3).dot(u) / (span * span), u.cross(p - o.3) / (span * span));
|
||||
Self::Anchors { node, anchors: vec![weight(o, 1.0 - t), weight(f, t)], across }.holding(doc, p, span)
|
||||
}
|
||||
|
||||
/// `p` on the segment `edge`, at the curve parameter of the segment's point nearest to it.
|
||||
/// None when the segment is gone or `p` is not on it.
|
||||
pub fn on_segment(doc: &Document, edge: &SegmentRef, p: Point) -> Option<Self> {
|
||||
use vectorcraft_geom::kurbo::ParamCurveNearest;
|
||||
let sp = doc.node(edge.node)?.path_data()?.subpaths.get(edge.subpath)?;
|
||||
if edge.segment >= sp.segment_count() {
|
||||
return None;
|
||||
}
|
||||
let (a, b) = edge.endpoints(doc)?;
|
||||
let t = sp.segment(edge.segment).nearest(p, 1e-12).t;
|
||||
Self::OnSegment { edge: edge.clone(), t }.holding(doc, p, a.distance(b))
|
||||
}
|
||||
|
||||
/// Itself when it is [`Self::valid`] and puts the point at `p`, within a tolerance that grows
|
||||
/// with `span`, the size of the art that holds it.
|
||||
fn holding(self, doc: &Document, p: Point, span: f64) -> Option<Self> {
|
||||
let at = self.position(doc)?;
|
||||
(at.distance(p) <= 1e-6 * span.max(1.0)).then_some(self)
|
||||
}
|
||||
|
||||
/// Whether its numbers can be used: a finite `t` from 0 to 1 on a segment; or two or three
|
||||
/// weights that add up to 1, with `across` set only for two, each finite and at most
|
||||
/// [`Self::MAX_WEIGHT`] across.
|
||||
pub fn valid(&self) -> bool {
|
||||
match self {
|
||||
Self::OnSegment { t, .. } => (0.0..=1.0).contains(t),
|
||||
Self::Anchors { anchors, across, .. } => {
|
||||
(2..=3).contains(&anchors.len())
|
||||
&& anchors.iter().all(|w| w.weight.is_finite() && w.weight.abs() <= Self::MAX_WEIGHT)
|
||||
&& (anchors.iter().map(|w| w.weight).sum::<f64>() - 1.0).abs() <= 1e-9
|
||||
&& across.is_finite()
|
||||
&& across.abs() <= Self::MAX_WEIGHT
|
||||
&& (*across == 0.0 || anchors.len() == 2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the point is now. None when its segment or an anchor is gone or the point is not
|
||||
/// [`Self::valid`].
|
||||
pub fn position(&self, doc: &Document) -> Option<Point> {
|
||||
use vectorcraft_geom::kurbo::ParamCurve;
|
||||
if !self.valid() {
|
||||
return None;
|
||||
}
|
||||
let at = match self {
|
||||
Self::OnSegment { edge, t } => {
|
||||
let sp = doc.node(edge.node)?.path_data()?.subpaths.get(edge.subpath)?;
|
||||
if edge.segment >= sp.segment_count() {
|
||||
return None;
|
||||
}
|
||||
sp.segment(edge.segment).eval(*t)
|
||||
}
|
||||
Self::Anchors { node, anchors, across } => {
|
||||
let path = doc.node(*node)?.path_data()?;
|
||||
let place = |w: &AnchorWeight| path.subpaths.get(w.subpath).and_then(|sp| sp.anchors.get(w.anchor)).map(|a| w.part.of(a).to_vec2());
|
||||
let mut at = Vec2::ZERO;
|
||||
for w in anchors {
|
||||
at += place(w)? * w.weight;
|
||||
}
|
||||
if *across != 0.0 {
|
||||
let u = place(anchors.get(1)?)? - place(anchors.first()?)?;
|
||||
at += Vec2::new(-u.y, u.x) * *across;
|
||||
}
|
||||
at.to_point()
|
||||
}
|
||||
};
|
||||
(at.x.is_finite() && at.y.is_finite()).then_some(at)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Dimension {
|
||||
@@ -349,6 +627,10 @@ pub struct Dimension {
|
||||
pub length: f64,
|
||||
pub options: DimensionOptions,
|
||||
pub source: Option<DimensionSource>,
|
||||
/// What a linear dimension measures: the distance between its points (aligned), or only its
|
||||
/// horizontal or vertical part. Other modes ignore it.
|
||||
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
|
||||
pub orientation: Orientation,
|
||||
/// How far a linked dimension was moved on its own since the last refresh: the refresh turns
|
||||
/// it into a new placement unless the measured art moved too.
|
||||
#[serde(skip)]
|
||||
@@ -359,16 +641,137 @@ impl Dimension {
|
||||
/// A point on the drawn dimension line (linear), arc (angular) or radius line.
|
||||
pub fn handle(&self) -> Point {
|
||||
match self.mode {
|
||||
DimensionMode::Linear => self.a.midpoint(self.b) + self.normal() * self.offset,
|
||||
DimensionMode::Linear => {
|
||||
let (a, b) = self.line_ends();
|
||||
a.midpoint(b)
|
||||
}
|
||||
DimensionMode::Angular => self.vertex + self.bisector() * self.offset.abs().max(12.0),
|
||||
DimensionMode::Radius | DimensionMode::Diameter => self.b,
|
||||
}
|
||||
}
|
||||
/// The left normal of a → b.
|
||||
/// The unit direction a linear dimension measures along: a → b when aligned, else the x axis
|
||||
/// (horizontal) or the y axis (vertical), whichever way the points lie.
|
||||
pub fn direction(&self) -> Vec2 {
|
||||
match self.orientation {
|
||||
Orientation::Horizontal if self.mode == DimensionMode::Linear => Vec2::new(1.0, 0.0),
|
||||
Orientation::Vertical if self.mode == DimensionMode::Linear => Vec2::new(0.0, 1.0),
|
||||
_ => {
|
||||
let u = self.b - self.a;
|
||||
let len = u.hypot();
|
||||
if len > 1e-9 { u / len } else { Vec2::new(1.0, 0.0) }
|
||||
}
|
||||
}
|
||||
}
|
||||
/// The left normal of [`Self::direction`].
|
||||
pub fn normal(&self) -> Vec2 {
|
||||
let u = self.b - self.a;
|
||||
let len = u.hypot();
|
||||
if len > 1e-9 { Vec2::new(-u.y, u.x) / len } else { Vec2::new(0.0, 1.0) }
|
||||
let u = self.direction();
|
||||
Vec2::new(-u.y, u.x)
|
||||
}
|
||||
/// The length a linear dimension shows: the distance between its points along
|
||||
/// [`Self::direction`].
|
||||
pub fn measured(&self) -> f64 {
|
||||
(self.b - self.a).dot(self.direction()).abs()
|
||||
}
|
||||
/// The two ends of a linear dimension's line: the measured points moved onto one line along
|
||||
/// [`Self::direction`], `offset` from a along the normal.
|
||||
pub fn line_ends(&self) -> (Point, Point) {
|
||||
let (u, n) = (self.direction(), self.normal());
|
||||
let a = self.a + n * self.offset;
|
||||
(a, a + u * (self.b - self.a).dot(u))
|
||||
}
|
||||
/// A horizontal or vertical dimension whose points moved from `old`'s keeps its line as far
|
||||
/// outside them as it was, on the same side: the gap is measured from the outermost point
|
||||
/// on the line's side, so the line stays clear of art that turns. A line that lay between
|
||||
/// the two points keeps its distance from the first point.
|
||||
pub fn keep_gap(&mut self, old: &Dimension) {
|
||||
if self.mode != DimensionMode::Linear
|
||||
|| self.orientation == Orientation::Aligned
|
||||
|| old.orientation != self.orientation
|
||||
|| (self.a, self.b) == (old.a, old.b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
let n = self.normal();
|
||||
let across = |p: Point| p.to_vec2().dot(n);
|
||||
let line = across(old.a) + old.offset;
|
||||
let (low, high) = (across(old.a).min(across(old.b)), across(old.a).max(across(old.b)));
|
||||
let (new_low, new_high) = (across(self.a).min(across(self.b)), across(self.a).max(across(self.b)));
|
||||
let at = if line >= high {
|
||||
new_high + (line - high)
|
||||
} else if line <= low {
|
||||
new_low + (line - low)
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
let offset = at - across(self.a);
|
||||
if offset.is_finite() && offset.abs() <= 1e6 {
|
||||
self.offset = offset;
|
||||
}
|
||||
}
|
||||
/// A linear dimension whose art was reflected, its points now moved from `old`'s: its line goes
|
||||
/// to the side of the points that `image` (where the reflection put `old`'s line) is on, as far
|
||||
/// past the outermost point on that side as `old`'s line was. A line that lay between the two
|
||||
/// points is left as it is.
|
||||
pub fn mirror_line(&mut self, old: &Dimension, image: Point) {
|
||||
if self.mode != DimensionMode::Linear || old.mode != DimensionMode::Linear {
|
||||
return;
|
||||
}
|
||||
let gap = {
|
||||
let n = old.normal();
|
||||
let across = |p: Point| p.to_vec2().dot(n);
|
||||
let line = across(old.a) + old.offset;
|
||||
let (low, high) = (across(old.a).min(across(old.b)), across(old.a).max(across(old.b)));
|
||||
if line >= high {
|
||||
line - high
|
||||
} else if line <= low {
|
||||
low - line
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
};
|
||||
let n = self.normal();
|
||||
let across = |p: Point| p.to_vec2().dot(n);
|
||||
let (low, high) = (across(self.a).min(across(self.b)), across(self.a).max(across(self.b)));
|
||||
let at = if 2.0 * across(image) >= low + high { high + gap } else { low - gap };
|
||||
let offset = at - across(self.a);
|
||||
if offset.is_finite() && offset.abs() <= 1e6 {
|
||||
self.offset = offset;
|
||||
}
|
||||
}
|
||||
/// Whether a linear dimension's line lies strictly between its two points along the normal.
|
||||
fn line_between_points(&self) -> bool {
|
||||
let n = self.normal();
|
||||
let across = |p: Point| p.to_vec2().dot(n);
|
||||
let line = across(self.a) + self.offset;
|
||||
across(self.a).min(across(self.b)) < line && line < across(self.a).max(across(self.b))
|
||||
}
|
||||
/// Makes a linear dimension measure in `orientation`, with its line on the side of the points
|
||||
/// it was on. The line goes through the middle of the old line when that point lies outside
|
||||
/// the two points along the new normal; otherwise it lies as far past the outermost point on
|
||||
/// that side as the old line lay from its points.
|
||||
pub fn set_orientation(&mut self, orientation: Orientation) {
|
||||
if self.mode != DimensionMode::Linear || self.orientation == orientation {
|
||||
self.orientation = orientation;
|
||||
return;
|
||||
}
|
||||
let (handle, gap) = (self.handle(), self.offset.abs());
|
||||
self.orientation = orientation;
|
||||
let n = self.normal();
|
||||
let across = |p: Point| p.to_vec2().dot(n);
|
||||
let (low, high) = (across(self.a).min(across(self.b)), across(self.a).max(across(self.b)));
|
||||
let h = across(handle);
|
||||
let at = if h >= high || h <= low {
|
||||
h
|
||||
} else if 2.0 * h >= low + high {
|
||||
high + gap
|
||||
} else {
|
||||
low - gap
|
||||
};
|
||||
let offset = at - across(self.a);
|
||||
if offset.is_finite() && offset.abs() <= 1e6 {
|
||||
self.offset = offset;
|
||||
}
|
||||
self.length = self.measured();
|
||||
}
|
||||
fn bisector(&self) -> Vec2 {
|
||||
let start = (self.a - self.vertex).atan2();
|
||||
@@ -397,28 +800,46 @@ impl Dimension {
|
||||
self.moved = Some((xf * at) - at + self.moved.unwrap_or_default());
|
||||
return;
|
||||
}
|
||||
// A horizontal or vertical dimension keeps measuring along its axis, between the points
|
||||
// where they now are, and its line goes through where the transform puts it. When that
|
||||
// puts the line between the points (a quarter turn), the line keeps its gap from them.
|
||||
let before = (self.mode == DimensionMode::Linear && self.orientation != Orientation::Aligned).then(|| (self.clone(), xf * self.handle()));
|
||||
self.a = xf * self.a;
|
||||
self.b = xf * self.b;
|
||||
self.vertex = xf * self.vertex;
|
||||
if let Some((old, p)) = before {
|
||||
let offset = (p - self.a).dot(self.normal());
|
||||
if offset.is_finite() && offset.abs() <= 1e6 {
|
||||
self.offset = offset;
|
||||
}
|
||||
if self.line_between_points() && !old.line_between_points() {
|
||||
self.keep_gap(&old);
|
||||
}
|
||||
}
|
||||
if self.mode == DimensionMode::Linear {
|
||||
self.length = self.a.distance(self.b);
|
||||
self.length = self.measured();
|
||||
}
|
||||
}
|
||||
pub fn resolve(&mut self, doc: &Document) -> Result<(), String> {
|
||||
match &self.source {
|
||||
Some(DimensionSource::Points { a, b, vertex }) => {
|
||||
for (reference, point) in [(a, &mut self.a), (b, &mut self.b), (vertex, &mut self.vertex)] {
|
||||
if let Some(r) = reference {
|
||||
*point = r.position(doc).ok_or("Measured anchor no longer exists")?;
|
||||
Some(DimensionSource::Points { a, b, vertex, free_a, free_b }) => {
|
||||
for (reference, free, point) in [(a, free_a, &mut self.a), (b, free_b, &mut self.b), (vertex, &None, &mut self.vertex)] {
|
||||
match (reference, free) {
|
||||
// An end is an anchor or a free point, never both.
|
||||
(Some(_), Some(_)) => return Err("Invalid measured point".into()),
|
||||
(Some(r), None) => *point = r.position(doc).ok_or("Measured anchor no longer exists")?,
|
||||
(None, Some(f)) if !f.valid() => return Err("Invalid measured point".into()),
|
||||
(None, Some(f)) => *point = f.position(doc).ok_or("Measured anchor no longer exists")?,
|
||||
(None, None) => {}
|
||||
}
|
||||
}
|
||||
self.length = self.a.distance(self.b);
|
||||
self.length = if self.mode == DimensionMode::Linear { self.measured() } else { self.a.distance(self.b) };
|
||||
}
|
||||
Some(DimensionSource::Segment { edge }) => {
|
||||
let (a, b) = edge.endpoints(doc).ok_or("Measured segment no longer exists")?;
|
||||
self.a = a;
|
||||
self.b = b;
|
||||
self.length = a.distance(b);
|
||||
self.length = self.measured();
|
||||
}
|
||||
Some(DimensionSource::Angle { first, second }) => {
|
||||
for edge in [first, second] {
|
||||
@@ -486,7 +907,7 @@ impl Dimension {
|
||||
DimensionMode::Angular => self.angle().abs().to_degrees(),
|
||||
DimensionMode::Radius => self.a.distance(self.b),
|
||||
DimensionMode::Diameter => self.a.distance(self.b) * 2.0,
|
||||
DimensionMode::Linear => self.a.distance(self.b),
|
||||
DimensionMode::Linear => self.measured(),
|
||||
};
|
||||
self.options.label(value, self.mode == DimensionMode::Angular)
|
||||
}
|
||||
|
||||
@@ -75,6 +75,7 @@ impl Document {
|
||||
dimensions,
|
||||
retired_artboard_id: _,
|
||||
opened_artboard: _,
|
||||
units_from_file: _,
|
||||
index: _,
|
||||
} = self;
|
||||
*version == other.version
|
||||
@@ -208,6 +209,7 @@ impl Document {
|
||||
show_video_safe_areas: false,
|
||||
video_ruler_pixel_aspect_ratio: 1.0,
|
||||
color: None,
|
||||
guide_label: None,
|
||||
}],
|
||||
layers: vec![],
|
||||
swatches: swatches.into(),
|
||||
@@ -256,6 +258,7 @@ impl Document {
|
||||
dimensions: false,
|
||||
retired_artboard_id: Default::default(),
|
||||
opened_artboard: None,
|
||||
units_from_file: false,
|
||||
index: Default::default(),
|
||||
};
|
||||
let id = d.alloc_id();
|
||||
@@ -276,6 +279,11 @@ impl Document {
|
||||
self.next_id
|
||||
}
|
||||
|
||||
/// Makes the next id at least `id`, so ids below it are not given out again.
|
||||
pub fn raise_next_id(&mut self, id: u64) {
|
||||
self.next_id = self.next_id.max(id);
|
||||
}
|
||||
|
||||
/// Ensure `next_id` is above every id in the tree (after deserializing foreign data).
|
||||
pub fn fix_next_id(&mut self) {
|
||||
// Ids from [`MAX_NEXT_ID`] up (a damaged or hand-made file: #1718) are left out, as is a
|
||||
|
||||
@@ -123,18 +123,33 @@ impl Document {
|
||||
std::borrow::Cow::Owned(d)
|
||||
}
|
||||
|
||||
/// Does `wants` hold for a node anywhere in the document's art: the layers, symbol definitions,
|
||||
/// graph designs, pattern swatches (the one being edited included) and their opacity masks?
|
||||
pub fn any_node(&self, wants: impl Fn(&Node) -> bool) -> bool {
|
||||
let roots = self
|
||||
.layers
|
||||
/// The roots of every tree of the document's art: the layers, symbol definitions, graph designs,
|
||||
/// pattern swatches and the original of the pattern being edited. Opacity masks hang off nodes
|
||||
/// in these trees.
|
||||
pub fn art_roots(&self) -> impl Iterator<Item = &Arc<Node>> {
|
||||
self.layers
|
||||
.iter()
|
||||
.chain(self.symbols.iter().map(|s| &s.art))
|
||||
.chain(self.graph_designs.iter().map(|g| &g.art))
|
||||
.chain(self.patterns.iter().flat_map(|p| p.art.iter()))
|
||||
.chain(self.pattern_edit.iter().filter_map(|pe| pe.original.as_ref()).flat_map(|p| p.art.iter()));
|
||||
.chain(self.pattern_edit.iter().filter_map(|pe| pe.original.as_ref()).flat_map(|p| p.art.iter()))
|
||||
}
|
||||
|
||||
/// [`Self::art_roots`], to change.
|
||||
pub fn art_roots_mut(&mut self) -> impl Iterator<Item = &mut Arc<Node>> {
|
||||
self.layers
|
||||
.iter_mut()
|
||||
.chain(self.symbols.iter_mut().map(|s| &mut s.art))
|
||||
.chain(self.graph_designs.iter_mut().map(|g| &mut g.art))
|
||||
.chain(self.patterns.iter_mut().flat_map(|p| p.art.iter_mut()))
|
||||
.chain(self.pattern_edit.iter_mut().filter_map(|pe| pe.original.as_mut()).flat_map(|p| p.art.iter_mut()))
|
||||
}
|
||||
|
||||
/// Does `wants` hold for a node anywhere in the document's art ([`Self::art_roots`]) or its
|
||||
/// opacity masks?
|
||||
pub fn any_node(&self, wants: impl Fn(&Node) -> bool) -> bool {
|
||||
let mut any = false;
|
||||
for root in roots {
|
||||
for root in self.art_roots() {
|
||||
crate::swatches::walk_with_masks(root, &mut |n| any |= wants(n));
|
||||
}
|
||||
any
|
||||
@@ -144,14 +159,7 @@ impl Document {
|
||||
/// [`Self::any_node`] looks). Only the trees that hold such a node are copied out of the art
|
||||
/// they share.
|
||||
pub fn edit_nodes_where(&mut self, wants: impl Fn(&Node) -> bool, mut f: impl FnMut(&mut Node)) {
|
||||
let roots = self
|
||||
.layers
|
||||
.iter_mut()
|
||||
.chain(self.symbols.iter_mut().map(|s| &mut s.art))
|
||||
.chain(self.graph_designs.iter_mut().map(|g| &mut g.art))
|
||||
.chain(self.patterns.iter_mut().flat_map(|p| p.art.iter_mut()))
|
||||
.chain(self.pattern_edit.iter_mut().filter_map(|pe| pe.original.as_mut()).flat_map(|p| p.art.iter_mut()));
|
||||
for root in roots {
|
||||
for root in self.art_roots_mut() {
|
||||
edit_branches_where(root, &wants, &mut f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,6 +347,7 @@ impl Document {
|
||||
t.first_para_continues = false;
|
||||
t.cached_bounds = None;
|
||||
t.cached_baselines.clear();
|
||||
t.cached_ink = None;
|
||||
}
|
||||
let on_path = n.extras().objects_on_path.is_some();
|
||||
let mut renamed = std::collections::HashMap::new();
|
||||
|
||||
+24
-4
@@ -136,9 +136,11 @@ impl crate::Document {
|
||||
}
|
||||
}
|
||||
|
||||
/// The character style a graph's labels keep through regeneration once formatted (Format the text in graphs): the
|
||||
/// value axis numbers, the category labels (category axis, radar spokes, pie names) and the legend labels (beside or
|
||||
/// across the top, or in pie wedges). `None` draws the default 9 pt black.
|
||||
/// The character style and anti-aliasing method a graph's labels keep through regeneration once formatted (Format
|
||||
/// the text in graphs): the value axis numbers, the category labels (category axis, radar spokes, pie names) and the
|
||||
/// legend labels (beside or across the top, or in pie wedges). `None` draws the default 9 pt black. The `*_para`
|
||||
/// fields are the paragraph attributes each kind keeps the same way (`None`: the defaults); the graph still sets each
|
||||
/// label's alignment.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct LabelStyles {
|
||||
@@ -148,6 +150,22 @@ pub struct LabelStyles {
|
||||
pub categories: Option<crate::CharStyle>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub legend: Option<crate::CharStyle>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub values_para: Option<crate::ParaStyle>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub categories_para: Option<crate::ParaStyle>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub legend_para: Option<crate::ParaStyle>,
|
||||
/// The anti-aliasing method the value axis numbers keep once set (Character panel › Anti-aliasing Method).
|
||||
/// `None` draws them Sharp.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub values_anti_alias: Option<crate::text::TypeAntiAlias>,
|
||||
/// The anti-aliasing method the category labels keep once set. `None` draws them Sharp.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub categories_anti_alias: Option<crate::text::TypeAntiAlias>,
|
||||
/// The anti-aliasing method the legend labels keep once set. `None` draws them Sharp.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub legend_anti_alias: Option<crate::text::TypeAntiAlias>,
|
||||
}
|
||||
|
||||
impl LabelStyles {
|
||||
@@ -525,7 +543,9 @@ impl GraphSpec {
|
||||
out.push_str(&field(self.categories.get(i).map(String::as_str).unwrap_or_default(), true));
|
||||
out.push(',');
|
||||
}
|
||||
out.push_str(&r.iter().map(|v| v.map(|v| v.to_string()).unwrap_or_default()).collect::<Vec<_>>().join(","));
|
||||
let line = r.iter().map(|v| v.map(|v| v.to_string()).unwrap_or_default()).collect::<Vec<_>>().join(",");
|
||||
// A lone blank value would be an empty line, which readers skip; a quoted empty field keeps the row.
|
||||
out.push_str(if line.is_empty() && !labelled { "\"\"" } else { &line });
|
||||
out.push('\n');
|
||||
}
|
||||
out
|
||||
|
||||
+24
-9
@@ -84,8 +84,9 @@ impl Default for HitOptions {
|
||||
|
||||
/// The shapes that an image's own fills and strokes paint, each with its fill rule and the fill or
|
||||
/// stroke that paints it (none for fills and strokes that paint nothing). Effects are evaluated
|
||||
/// above this crate, so the engine registers one with [`set_image_paint`].
|
||||
pub type ImagePaint = fn(&Node) -> Vec<(BezPath, FillRule, AppearanceItem)>;
|
||||
/// above this crate, so the engine registers one with [`set_image_paint`]. It looks up swatches and
|
||||
/// patterns in the document it is given.
|
||||
pub type ImagePaint = fn(Option<&Document>, &Node) -> Vec<(BezPath, FillRule, AppearanceItem)>;
|
||||
|
||||
static IMAGE_PAINT: OnceLock<ImagePaint> = OnceLock::new();
|
||||
|
||||
@@ -98,8 +99,8 @@ pub fn set_image_paint(f: ImagePaint) {
|
||||
|
||||
/// The shapes that the image `n`'s own fills and strokes paint ([`set_image_paint`]), none while
|
||||
/// nothing is registered.
|
||||
pub(crate) fn image_paint(n: &Node) -> Vec<(BezPath, FillRule, AppearanceItem)> {
|
||||
IMAGE_PAINT.get().map_or_else(Vec::new, |f| f(n))
|
||||
pub(crate) fn image_paint(doc: Option<&Document>, n: &Node) -> Vec<(BezPath, FillRule, AppearanceItem)> {
|
||||
IMAGE_PAINT.get().map_or_else(Vec::new, |f| f(doc, n))
|
||||
}
|
||||
|
||||
/// One level of evaluation of a live object in a document, as the canvas draws it: type in an
|
||||
@@ -127,11 +128,14 @@ fn live_inputs() -> u64 {
|
||||
}
|
||||
|
||||
/// A live object evaluated by [`live_art`] on this thread: the object, its document's symbols (by
|
||||
/// name and art) and patterns and the [`LiveInputs`] as they were, the art and its [`art_size`].
|
||||
/// name and art), patterns, swatches and color groups and the [`LiveInputs`] as they were, the art
|
||||
/// and its [`art_size`].
|
||||
struct EvaluatedLive {
|
||||
node: Node,
|
||||
symbols: Vec<(String, Arc<Node>)>,
|
||||
patterns: Vec<crate::PatternDef>,
|
||||
swatches: crate::Shared<Vec<vectorcraft_color::Swatch>>,
|
||||
swatch_groups: crate::Shared<Vec<vectorcraft_color::SwatchGroup>>,
|
||||
inputs: u64,
|
||||
art: Rc<Vec<Node>>,
|
||||
size: usize,
|
||||
@@ -145,6 +149,8 @@ impl EvaluatedLive {
|
||||
&& self.symbols.iter().zip(&doc.symbols).all(|((name, art), s)| *name == s.name && Arc::ptr_eq(art, &s.art))
|
||||
&& self.node == *n
|
||||
&& self.patterns == doc.patterns
|
||||
&& self.swatches == doc.swatches
|
||||
&& self.swatch_groups == doc.swatch_groups
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,8 +189,8 @@ thread_local! {
|
||||
|
||||
/// The art live object `n` of `doc` draws ([`set_live_art`]); [`crate::live::expand_live`] while
|
||||
/// nothing is registered. The art is evaluated again when `n`, a symbol of `doc`, a pattern of
|
||||
/// `doc` or the [`LiveInputs`] have changed since, and on every click while it or the document's
|
||||
/// symbols or patterns hold a placed document ([`holds_placed`]).
|
||||
/// `doc`, its swatches or color groups, or the [`LiveInputs`] have changed since, and on every
|
||||
/// click while it or the document's symbols or patterns hold a placed document ([`holds_placed`]).
|
||||
fn live_art(doc: &Document, n: &Node) -> Rc<Vec<Node>> {
|
||||
let inputs = live_inputs();
|
||||
let kept = EVALUATED.with_borrow_mut(|kept| {
|
||||
@@ -205,7 +211,16 @@ fn live_art(doc: &Document, n: &Node) -> Rc<Vec<Node>> {
|
||||
return art;
|
||||
}
|
||||
let symbols = doc.symbols.iter().map(|s| (s.name.clone(), s.art.clone())).collect();
|
||||
let e = EvaluatedLive { node: n.clone(), symbols, patterns: doc.patterns.clone(), inputs, art: art.clone(), size };
|
||||
let e = EvaluatedLive {
|
||||
node: n.clone(),
|
||||
symbols,
|
||||
patterns: doc.patterns.clone(),
|
||||
swatches: doc.swatches.clone(),
|
||||
swatch_groups: doc.swatch_groups.clone(),
|
||||
inputs,
|
||||
art: art.clone(),
|
||||
size,
|
||||
};
|
||||
EVALUATED.with_borrow_mut(|kept| {
|
||||
kept.insert(0, e);
|
||||
kept.truncate(LIVE_KEPT);
|
||||
@@ -871,7 +886,7 @@ fn hit_leaf(n: &Node, p: Point, opt: HitOptions, symbols: &SymbolBoxes) -> Optio
|
||||
if opt.outline {
|
||||
return None;
|
||||
}
|
||||
image_paint(n).iter().rev().find_map(|(bp, rule, item)| match item {
|
||||
image_paint(Some(symbols.document()), n).iter().rev().find_map(|(bp, rule, item)| match item {
|
||||
AppearanceItem::Stroke(s) => s.hits(bp, *rule, p, opt.tol).then_some(HitKind::Stroke),
|
||||
AppearanceItem::Fill(_) => (!opt.path_only && fill_contains(bp, *rule, p)).then_some(HitKind::Fill),
|
||||
})
|
||||
|
||||
@@ -605,11 +605,41 @@ fn evaluated_art_is_not_kept_while_a_placed_document_may_show_in_it() {
|
||||
fn a_kept_evaluation_is_stale_after_the_live_inputs_change() {
|
||||
let d = Document::new(100.0, 100.0);
|
||||
let n = Node::path(NodeId(3), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default_art());
|
||||
let e = EvaluatedLive { node: n.clone(), symbols: vec![], patterns: vec![], inputs: 7, art: std::rc::Rc::new(vec![]), size: 0 };
|
||||
let e = kept_evaluation(&d, &n);
|
||||
assert!(e.is_of(&d, &n, 7));
|
||||
assert!(!e.is_of(&d, &n, 8));
|
||||
}
|
||||
|
||||
/// An evaluation of `n` in `d`, with inputs 7, holding no symbols, patterns or art.
|
||||
fn kept_evaluation(d: &Document, n: &Node) -> EvaluatedLive {
|
||||
EvaluatedLive {
|
||||
node: n.clone(),
|
||||
symbols: vec![],
|
||||
patterns: vec![],
|
||||
swatches: d.swatches.clone(),
|
||||
swatch_groups: d.swatch_groups.clone(),
|
||||
inputs: 7,
|
||||
art: std::rc::Rc::new(vec![]),
|
||||
size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// A kept evaluation is stale once a swatch changes, in a color group too: a painted live effect's
|
||||
/// pieces name swatches.
|
||||
#[test]
|
||||
fn a_kept_evaluation_is_stale_after_a_swatch_changes() {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let n = Node::path(NodeId(3), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default_art());
|
||||
let e = kept_evaluation(&d, &n);
|
||||
assert!(e.is_of(&d, &n, 7));
|
||||
d.swatches.first_mut().unwrap().name = "Renamed".into();
|
||||
assert!(!e.is_of(&d, &n, 7));
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let e = kept_evaluation(&d, &n);
|
||||
d.swatch_groups.push(vectorcraft_color::SwatchGroup { name: "Added".into(), swatches: vec![] });
|
||||
assert!(!e.is_of(&d, &n, 7));
|
||||
}
|
||||
|
||||
/// A rectangle (40, 40)–(140, 140) clipping a filled 200 × 200 square drawn over its edges, in a
|
||||
/// clip group (or, `layer`, as the first layer's clipping mask): the group (or the layer), the
|
||||
/// clipping path and the art.
|
||||
|
||||
+41
-5
@@ -110,8 +110,9 @@ fn visit_paint(p: &mut Paint, f: &mut ColorVisitor) {
|
||||
}
|
||||
|
||||
/// Visit every colour of a node tree (fills, strokes, gradient stops, text runs, mesh points, an
|
||||
/// image's colorize color, not opacity masks) with its swatch link and tint. Mesh points have no
|
||||
/// link: changes to it are dropped.
|
||||
/// image's colorize color, the fills and strokes a dynamic symbol instance keeps for objects of its
|
||||
/// art, not opacity masks) with its swatch link and tint. Mesh points have no link: changes to it
|
||||
/// are dropped.
|
||||
pub fn visit_node_colors(n: &mut Node, f: &mut ColorVisitor) {
|
||||
for it in &mut n.appearance.items {
|
||||
match it {
|
||||
@@ -119,8 +120,13 @@ pub fn visit_node_colors(n: &mut Node, f: &mut ColorVisitor) {
|
||||
AppearanceItem::Stroke(l) => visit_paint(&mut l.paint, f),
|
||||
}
|
||||
}
|
||||
if let Some(p) = n.extras.0.as_deref_mut().and_then(|e| e.colorize.as_deref_mut()) {
|
||||
visit_paint(p, f);
|
||||
if let Some(e) = n.extras.0.as_deref_mut() {
|
||||
if let Some(p) = e.colorize.as_deref_mut() {
|
||||
visit_paint(p, f);
|
||||
}
|
||||
for p in e.symbol_overrides.iter_mut().flat_map(|o| [&mut o.fill, &mut o.stroke]).flatten() {
|
||||
visit_paint(p, f);
|
||||
}
|
||||
}
|
||||
match &mut n.kind {
|
||||
NodeKind::Text(t) => {
|
||||
@@ -144,7 +150,8 @@ pub fn visit_node_colors(n: &mut Node, f: &mut ColorVisitor) {
|
||||
}
|
||||
|
||||
/// Apply `f` to every colour of a node tree (fills, strokes, gradient stops, text runs, mesh
|
||||
/// points) given its swatch link and tint, keeping the links.
|
||||
/// points, a dynamic symbol instance's own fills and strokes) given its swatch link and tint,
|
||||
/// keeping the links.
|
||||
pub fn map_node_colors(n: &mut Node, f: &mut dyn FnMut(&Color, Link) -> Color) {
|
||||
visit_node_colors(n, &mut |c, swatch, tint| *c = f(c, swatch.as_deref().map(|s| (s, *tint))));
|
||||
}
|
||||
@@ -158,3 +165,32 @@ pub fn map_document_colors(doc: &mut Document, f: &mut dyn FnMut(&Color, Link) -
|
||||
map_node_colors(Arc::make_mut(&mut s.art), f);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{NodeId, SymbolOverride};
|
||||
use vectorcraft_geom::Affine;
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instances_own_fills_and_strokes_are_visited() {
|
||||
let red = Color::rgb(1.0, 0.0, 0.0);
|
||||
let blue = Color::rgb(0.0, 0.0, 1.0);
|
||||
let spot = Paint::Solid { color: red, swatch: Some("Spot".into()), tint: 0.5 };
|
||||
let mut inst = Node::new(NodeId(1), NodeKind::SymbolInstance { symbol: "S".into(), xf: Affine::IDENTITY });
|
||||
inst.extras_mut().symbol_overrides = vec![
|
||||
SymbolOverride { path: vec![0], fill: Some(spot), stroke: Some(Paint::solid(red)), ..Default::default() },
|
||||
SymbolOverride { path: vec![1], opacity: Some(0.5), ..Default::default() },
|
||||
];
|
||||
let mut links = vec![];
|
||||
map_node_colors(&mut inst, &mut |_, link| {
|
||||
links.push(link.map(|(s, t)| (s.to_string(), t)));
|
||||
blue
|
||||
});
|
||||
assert_eq!(links, vec![Some(("Spot".to_string(), 0.5)), None], "each override paint is visited once, with its link");
|
||||
let o = &inst.extras().symbol_overrides;
|
||||
assert_eq!(o[0].fill, Some(Paint::Solid { color: blue, swatch: Some("Spot".into()), tint: 0.5 }), "the link is kept");
|
||||
assert_eq!(o[0].stroke, Some(Paint::solid(blue)));
|
||||
assert_eq!((o[1].fill.as_ref(), o[1].stroke.as_ref()), (None, None), "an override without paint gets none");
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,7 @@ impl Document {
|
||||
}
|
||||
if changed {
|
||||
t.cached_bounds = None;
|
||||
t.cached_ink = None;
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
@@ -339,6 +339,11 @@ pub struct Document {
|
||||
/// names it as the file's active artboard. `None`: the first. Native files do not keep it.
|
||||
#[serde(skip)]
|
||||
pub opened_artboard: Option<u32>,
|
||||
/// The file this document was read from recorded its units (a `.ai` file's
|
||||
/// `%AI5_RulerUnits`): it opens in them. A file that records none opens in Preferences ›
|
||||
/// Units › General (#5820). Native files do not keep it: they always keep their units.
|
||||
#[serde(skip)]
|
||||
pub units_from_file: bool,
|
||||
/// Some object is a dimension (the Dimension tool): edits refresh dimensions only while this
|
||||
/// is set, so documents without any pay nothing for them.
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
|
||||
@@ -643,6 +643,168 @@ mod tests {
|
||||
assert!(e.p1.distance(Point::new(30.0, 0.0)) < 1e-6 && e.p2.distance(Point::new(60.0, 0.0)) < 1e-6, "{e:?}");
|
||||
}
|
||||
|
||||
/// The polylines of `bp` flattened to 0.01 pt.
|
||||
fn flattened(bp: &BezPath) -> Vec<Vec<Point>> {
|
||||
let mut out: Vec<Vec<Point>> = vec![];
|
||||
vectorcraft_geom::kurbo::flatten(bp.iter(), 0.01, |el| match el {
|
||||
vectorcraft_geom::PathEl::MoveTo(p) => out.push(vec![p]),
|
||||
vectorcraft_geom::PathEl::LineTo(p) => out.last_mut().into_iter().for_each(|v| v.push(p)),
|
||||
vectorcraft_geom::PathEl::ClosePath => out.last_mut().into_iter().for_each(|v| v.push(v[0])),
|
||||
_ => {}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// The distance from `p` to the nearest of `lines`.
|
||||
fn off_lines(lines: &[Vec<Point>], p: Point) -> f64 {
|
||||
lines
|
||||
.iter()
|
||||
.flat_map(|v| v.windows(2))
|
||||
.map(|w| {
|
||||
let d = w[1] - w[0];
|
||||
let t = if d.hypot2() > 0.0 { ((p - w[0]).dot(d) / d.hypot2()).clamp(0.0, 1.0) } else { 0.0 };
|
||||
p.distance(w[0] + d * t)
|
||||
})
|
||||
.fold(f64::INFINITY, f64::min)
|
||||
}
|
||||
|
||||
/// How far apart the border of `m` and the outline of `path` are, in points: the larger of the
|
||||
/// farthest a point of either lies from the other.
|
||||
fn mesh_border_off_path(m: &GradientMesh, path: &PathData) -> f64 {
|
||||
let (border, outline) = (flattened(&m.outline().to_bezpath()), flattened(&path.to_bezpath()));
|
||||
let one = border.iter().flatten().map(|p| off_lines(&outline, *p)).fold(0.0, f64::max);
|
||||
let other = outline.iter().flatten().map(|p| off_lines(&border, *p)).fold(0.0, f64::max);
|
||||
one.max(other)
|
||||
}
|
||||
|
||||
/// #2752: a gradient mesh keeps the exact outline of the path it is made from, sharp corners
|
||||
/// included, whether made by Create Gradient Mesh (1×1 and 4×4) or by a Mesh tool click at a
|
||||
/// point inside. Before, the border of each mesh was one cubic per edge between four corners
|
||||
/// near the bounding box's: up to 49 pt off a five-point star, 54 pt off a notched rectangle.
|
||||
#[test]
|
||||
fn a_mesh_keeps_the_outline_of_every_shape() {
|
||||
const OFF: f64 = 0.1;
|
||||
let poly = |pts: &[(f64, f64)]| PathData::single(SubPath::polyline(&pts.iter().map(|&(x, y)| Point::new(x, y)).collect::<Vec<_>>(), true));
|
||||
let curves = |start: (f64, f64), cubics: &[[(f64, f64); 3]]| {
|
||||
let mut bp = BezPath::new();
|
||||
bp.move_to(start);
|
||||
for [a, b, c] in cubics {
|
||||
bp.curve_to(*a, *b, *c);
|
||||
}
|
||||
bp.close_path();
|
||||
PathData::from_bezpath(&bp)
|
||||
};
|
||||
let box_ = Rect::new(100.0, 100.0, 500.0, 400.0);
|
||||
let heart = curves(
|
||||
(300.0, 400.0),
|
||||
&[
|
||||
[(200.0, 320.0), (100.0, 250.0), (100.0, 170.0)],
|
||||
[(100.0, 100.0), (180.0, 70.0), (230.0, 90.0)],
|
||||
[(270.0, 105.0), (290.0, 130.0), (300.0, 150.0)],
|
||||
[(310.0, 130.0), (330.0, 105.0), (370.0, 90.0)],
|
||||
[(420.0, 70.0), (500.0, 100.0), (500.0, 170.0)],
|
||||
[(500.0, 250.0), (400.0, 320.0), (300.0, 400.0)],
|
||||
],
|
||||
);
|
||||
let teardrop = curves(
|
||||
(300.0, 100.0),
|
||||
&[
|
||||
[(350.0, 200.0), (420.0, 250.0), (420.0, 320.0)],
|
||||
[(420.0, 386.0), (366.0, 440.0), (300.0, 440.0)],
|
||||
[(234.0, 440.0), (180.0, 386.0), (180.0, 320.0)],
|
||||
[(180.0, 250.0), (250.0, 200.0), (300.0, 100.0)],
|
||||
],
|
||||
);
|
||||
let shapes: [(&str, PathData, Point); 14] = [
|
||||
("rectangle", shapes::rectangle(box_), Point::new(260.0, 230.0)),
|
||||
("rotated rectangle", shapes::rectangle(box_).transformed(Affine::rotate_about(0.5, box_.center())), Point::new(280.0, 240.0)),
|
||||
("triangle", poly(&[(300.0, 100.0), (500.0, 400.0), (100.0, 400.0)]), Point::new(300.0, 320.0)),
|
||||
("five-point star", shapes::star(Point::new(300.0, 300.0), 200.0, 80.0, 5, 0.0), Point::new(300.0, 300.0)),
|
||||
("twelve-point star", shapes::star(Point::new(300.0, 300.0), 200.0, 140.0, 12, 0.0), Point::new(300.0, 300.0)),
|
||||
(
|
||||
"L-shape",
|
||||
poly(&[(100.0, 100.0), (200.0, 100.0), (200.0, 300.0), (400.0, 300.0), (400.0, 400.0), (100.0, 400.0)]),
|
||||
Point::new(150.0, 350.0),
|
||||
),
|
||||
("rounded rectangle", shapes::rounded_rectangle(box_, 40.0), Point::new(260.0, 230.0)),
|
||||
("heart", heart, Point::new(300.0, 250.0)),
|
||||
("teardrop", teardrop, Point::new(300.0, 330.0)),
|
||||
("ellipse", shapes::ellipse(box_), Point::new(200.0, 330.0)),
|
||||
("thin rectangle", shapes::rectangle(Rect::new(100.0, 100.0, 500.0, 110.0)), Point::new(260.0, 104.0)),
|
||||
(
|
||||
"notched rectangle",
|
||||
poly(&[(100.0, 100.0), (250.0, 100.0), (300.0, 200.0), (350.0, 100.0), (500.0, 100.0), (500.0, 400.0), (100.0, 400.0)]),
|
||||
Point::new(260.0, 300.0),
|
||||
),
|
||||
// A notch shorter than the steps along the outline an edge is checked at, and deeper
|
||||
// than the tolerance.
|
||||
(
|
||||
"rectangle with a tiny notch",
|
||||
poly(&[(100.0, 100.0), (301.1, 100.0), (301.2, 100.3), (301.3, 100.0), (500.0, 100.0), (500.0, 400.0), (100.0, 400.0)]),
|
||||
Point::new(260.0, 300.0),
|
||||
),
|
||||
// An outline that crosses itself, whose two lobes' areas cancel.
|
||||
("bow tie", poly(&[(100.0, 100.0), (500.0, 400.0), (500.0, 100.0), (100.0, 400.0)]), Point::new(150.0, 250.0)),
|
||||
];
|
||||
// Every shape and way of making the mesh is checked, and all that stray are named.
|
||||
let mut strays = vec![];
|
||||
for (name, path, click) in shapes {
|
||||
for (rows, cols) in [(1, 1), (4, 4)] {
|
||||
let m = GradientMesh::for_path(&path, rows, cols, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
let off = mesh_border_off_path(&m, &path);
|
||||
if off >= OFF {
|
||||
strays.push(format!("{name}, {rows}×{cols}: border {off:.2} pt off the outline"));
|
||||
}
|
||||
}
|
||||
let mut m = GradientMesh::for_path(&path, 1, 1, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
assert!(m.add_lines_at(click, 0.0).is_some(), "{name}: the click at {click:?} is outside the mesh");
|
||||
let off = mesh_border_off_path(&m, &path);
|
||||
if off >= OFF {
|
||||
strays.push(format!("{name}, Mesh tool click: border {off:.2} pt off the outline"));
|
||||
}
|
||||
}
|
||||
assert!(strays.is_empty(), "{strays:#?}");
|
||||
}
|
||||
|
||||
/// The lines a mesh takes to keep a path's outline are as few as do it: a rectangle's and an
|
||||
/// ellipse's meshes keep the rows and columns asked for, and a star's 1×1 mesh takes lines
|
||||
/// through its points, at most one each way per anchor. Paths without an outline to follow
|
||||
/// still give a mesh (an open path closes straight; a line there and back maps onto its box),
|
||||
/// and a point that isn't finite gives none.
|
||||
#[test]
|
||||
fn a_mesh_takes_lines_only_where_the_outline_needs_them() {
|
||||
let box_ = Rect::new(100.0, 100.0, 500.0, 400.0);
|
||||
for path in [shapes::rectangle(box_), shapes::ellipse(box_)] {
|
||||
for (rows, cols) in [(1, 1), (4, 4), (2, 5)] {
|
||||
let m = GradientMesh::for_path(&path, rows, cols, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
assert_eq!((m.rows, m.cols), (rows, cols));
|
||||
}
|
||||
}
|
||||
let star = shapes::star(Point::new(300.0, 300.0), 200.0, 80.0, 5, 0.0);
|
||||
let m = GradientMesh::for_path(&star, 1, 1, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
assert!(m.rows > 1 && m.cols > 1 && m.rows <= 10 && m.cols <= 10 && m.is_valid(), "{}×{}", m.rows, m.cols);
|
||||
// Every anchor of the star is a point of the mesh.
|
||||
for a in &star.subpaths[0].anchors {
|
||||
assert!(m.points.iter().any(|q| q.p.distance(a.p) < 1e-6), "{:?} is not a mesh point", a.p);
|
||||
}
|
||||
// An open path closes with a straight segment.
|
||||
let open = PathData::single(SubPath::polyline(&[Point::new(0.0, 0.0), Point::new(100.0, 0.0), Point::new(100.0, 80.0)], false));
|
||||
let m = GradientMesh::for_path(&open, 2, 2, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
let closed = PathData::single(SubPath::polyline(&[Point::new(0.0, 0.0), Point::new(100.0, 0.0), Point::new(100.0, 80.0)], true));
|
||||
assert!(mesh_border_off_path(&m, &closed) < 0.1);
|
||||
// A line there and back.
|
||||
let line = PathData::single(SubPath::polyline(&[Point::new(0.0, 0.0), Point::new(100.0, 50.0)], true));
|
||||
let m = GradientMesh::for_path(&line, 1, 1, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
assert!(m.is_valid() && m.points.iter().all(|q| q.p.is_finite()));
|
||||
let mut nan = shapes::rectangle(box_);
|
||||
nan.subpaths[0].anchors[1].p.x = f64::NAN;
|
||||
assert!(GradientMesh::for_path(&nan, 1, 1, Color::BLACK, MeshAppearance::Flat, 0.0).is_none());
|
||||
// A path of many anchors stays within the line limit and keeps its outline.
|
||||
let many = shapes::star(Point::new(300.0, 300.0), 200.0, 190.0, 400, 0.0);
|
||||
let m = GradientMesh::for_path(&many, 1, 1, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
assert!(m.is_valid() && m.rows as usize <= mesh::MAX_BORDER_LINES && m.cols as usize <= mesh::MAX_BORDER_LINES, "{}×{}", m.rows, m.cols);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mesh_insert_and_remove_lines() {
|
||||
let mut m =
|
||||
|
||||
@@ -236,6 +236,7 @@ impl TypeInks {
|
||||
text.xf = Affine::new([a, b, c, d, 0.0, 0.0]);
|
||||
text.cached_bounds = None;
|
||||
text.cached_baselines.clear();
|
||||
text.cached_ink = None;
|
||||
let content = content_hash(&text).map(|hash| (hash, by_node.1, inputs));
|
||||
match content.and_then(|key| self.content.get(&key)) {
|
||||
Some(ink) => {
|
||||
|
||||
+481
-8
@@ -470,6 +470,384 @@ fn fit_edge(side: &[Point], span: (f64, f64), ends: (Point, Point)) -> Option<(V
|
||||
fit
|
||||
}
|
||||
|
||||
/// The cubic from `p0` to `p3` leaving `p0` along `d0` and reaching `p3` against `d1` (unit
|
||||
/// vectors; `d1` points from `p3` back into the curve) that comes closest (least squares) to the
|
||||
/// points `qs`, which lie along the way in order; `None` when no such cubic has handles of positive
|
||||
/// length. Each point is matched to the curve's nearest point, refined over a few rounds, as in
|
||||
/// [`fit_edge`].
|
||||
fn fit_tangents(qs: &[Point], (p0, d0): (Point, Vec2), (p3, d1): (Point, Vec2)) -> Option<CubicBez> {
|
||||
const ROUNDS: usize = 6;
|
||||
let n = qs.len() + 1;
|
||||
let mut ts: Vec<f64> = (1..n).map(|i| i as f64 / n as f64).collect();
|
||||
let mut fit = None;
|
||||
for _ in 0..ROUNDS {
|
||||
let (mut c11, mut c12, mut c22, mut x1, mut x2) = (0.0, 0.0, 0.0, 0.0, 0.0);
|
||||
for (q, &t) in qs.iter().zip(&ts) {
|
||||
let s = 1.0 - t;
|
||||
let (b0, b1, b2, b3) = (s * s * s, 3.0 * s * s * t, 3.0 * s * t * t, t * t * t);
|
||||
let (a1, a2) = (d0 * b1, d1 * b2);
|
||||
let r = q.to_vec2() - (b0 + b1) * p0.to_vec2() - (b2 + b3) * p3.to_vec2();
|
||||
c11 += a1.dot(a1);
|
||||
c12 += a1.dot(a2);
|
||||
c22 += a2.dot(a2);
|
||||
x1 += r.dot(a1);
|
||||
x2 += r.dot(a2);
|
||||
}
|
||||
let det = c11 * c22 - c12 * c12;
|
||||
if det.abs() < 1e-12 {
|
||||
break;
|
||||
}
|
||||
let (l1, l2) = ((c22 * x1 - c12 * x2) / det, (c11 * x2 - c12 * x1) / det);
|
||||
if !(l1.is_finite() && l2.is_finite() && l1 > 0.0 && l2 > 0.0) {
|
||||
break;
|
||||
}
|
||||
let c = CubicBez::new(p0, p0 + d0 * l1, p3 + d1 * l2, p3);
|
||||
fit = Some(c);
|
||||
let (a, b, k) = (c.p1 - c.p0, c.p2 - c.p1, c.p3 - c.p2);
|
||||
for (q, t) in qs.iter().zip(ts.iter_mut()) {
|
||||
let s = 1.0 - *t;
|
||||
let v = 3.0 * (s * s * a + 2.0 * s * *t * b + *t * *t * k);
|
||||
let acc = 6.0 * (s * (b - a) + *t * (k - b));
|
||||
let e = c.eval(*t) - *q;
|
||||
let den = v.hypot2() + e.dot(acc);
|
||||
if den.abs() > 1e-12 {
|
||||
*t = (*t - e.dot(v) / den).clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
fit
|
||||
}
|
||||
|
||||
/// How far (in points) a border edge of a mesh made from a path may stray from the path's outline
|
||||
/// before the mesh takes a line through an anchor of the outline there; a thousandth of the outline's
|
||||
/// size when that is less.
|
||||
const BORDER_TOLERANCE: f64 = 0.1;
|
||||
|
||||
/// The most lines each way a mesh made from a path takes to keep its border on the outline
|
||||
/// ([`BORDER_TOLERANCE`]); past that, each border edge keeps its closest fit.
|
||||
pub(super) const MAX_BORDER_LINES: usize = 256;
|
||||
|
||||
/// The most rounds of new lines a mesh made from a path takes to keep its border on the outline.
|
||||
const MAX_BORDER_ROUNDS: usize = 64;
|
||||
|
||||
/// An anchor whose directions in and out differ by more than this (radians) is a corner: a border
|
||||
/// edge that can't follow the outline takes a mesh line through its sharpest corner first.
|
||||
const SHARP_TURN: f64 = 0.01;
|
||||
|
||||
/// Steps of each segment's parameter at which [`Outline`] measures the segment's length.
|
||||
const LENGTH_STEPS: usize = 32;
|
||||
|
||||
/// Points of the outline each border edge of a mesh made from a path is fitted to
|
||||
/// ([`Outline::fit`]).
|
||||
const FIT_POINTS: usize = 32;
|
||||
|
||||
/// Points of the outline, and of the fitted edge, at which [`Outline::fit`] measures how far the
|
||||
/// two stray from each other.
|
||||
const CHECK_POINTS: usize = 128;
|
||||
|
||||
/// The outline a gradient mesh is made from: the largest subpath of a path, running clockwise on
|
||||
/// screen, an open one closed by a straight segment, without segments of no length. A place on it
|
||||
/// is a length from its first anchor, measured along a polyline through even steps of each
|
||||
/// segment's parameter; the same length always gives the same point.
|
||||
struct Outline {
|
||||
segs: Vec<CubicBez>,
|
||||
/// Where each segment starts, and the whole length last.
|
||||
starts: Vec<f64>,
|
||||
/// For each segment, the length from its start at each of [`LENGTH_STEPS`] + 1 even steps of
|
||||
/// its parameter.
|
||||
table: Vec<[f64; LENGTH_STEPS + 1]>,
|
||||
/// Lengths closer than this are the same place.
|
||||
eps: f64,
|
||||
}
|
||||
|
||||
/// One side of the patch a mesh made from a path spans: the stretch of the outline from `start`,
|
||||
/// `len` long, run backwards when `reversed` (the bottom and left sides run against the outline).
|
||||
#[derive(Clone, Copy)]
|
||||
struct Side {
|
||||
start: f64,
|
||||
len: f64,
|
||||
reversed: bool,
|
||||
}
|
||||
|
||||
impl Side {
|
||||
/// The place on the outline at `f` (0 to 1) along the side.
|
||||
fn at(&self, f: f64) -> f64 {
|
||||
self.start + if self.reversed { (1.0 - f) * self.len } else { f * self.len }
|
||||
}
|
||||
}
|
||||
|
||||
/// The direction a cubic leaves its start in (`false`) or reaches its end in (`true`), from the
|
||||
/// first handle or point that is not at the end; zero when all its points are together.
|
||||
fn cubic_tangent(c: &CubicBez, end: bool) -> Vec2 {
|
||||
let near = 1e-12 * (c.p0.to_vec2().hypot() + c.p3.to_vec2().hypot() + 1.0);
|
||||
let options = if end { [c.p3 - c.p2, c.p3 - c.p1, c.p3 - c.p0] } else { [c.p1 - c.p0, c.p2 - c.p0, c.p3 - c.p0] };
|
||||
options.into_iter().find(|v| v.hypot() > near).unwrap_or(Vec2::ZERO)
|
||||
}
|
||||
|
||||
impl Outline {
|
||||
/// The outline of `path`; `None` when it has no subpath of two anchors or more, a point that
|
||||
/// isn't finite, or all its points on one line (a line there and back).
|
||||
fn new(path: &PathData) -> Option<Self> {
|
||||
let sp = path.subpaths.iter().filter(|s| s.anchors.len() >= 2).max_by(|a, b| a.area().abs().total_cmp(&b.area().abs()))?;
|
||||
if !sp.anchors.iter().all(|a| a.p.is_finite() && a.h_in.is_finite() && a.h_out.is_finite()) {
|
||||
return None;
|
||||
}
|
||||
let mut sp = sp.clone();
|
||||
if sp.signed_area() < 0.0 {
|
||||
sp.reverse();
|
||||
}
|
||||
// A straight segment's parameter runs evenly along it.
|
||||
let line = |a: Point, b: Point| CubicBez::new(a, a.lerp(b, 1.0 / 3.0), a.lerp(b, 2.0 / 3.0), b);
|
||||
let mut segs: Vec<CubicBez> =
|
||||
(0..sp.segment_count()).map(|i| if sp.segment_is_line(i) { line(sp.segment(i).p0, sp.segment(i).p3) } else { sp.segment(i) }).collect();
|
||||
if !sp.closed
|
||||
&& let (Some(a), Some(b)) = (sp.anchors.last(), sp.anchors.first())
|
||||
{
|
||||
segs.push(line(a.p, b.p));
|
||||
}
|
||||
segs.retain(|c| [c.p1, c.p2, c.p3].iter().any(|p| p.distance(c.p0) > 1e-9));
|
||||
if segs.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
let mut starts = Vec::with_capacity(segs.len() + 1);
|
||||
let mut table = Vec::with_capacity(segs.len());
|
||||
let mut total = 0.0;
|
||||
for c in &segs {
|
||||
let mut lens = [0.0; LENGTH_STEPS + 1];
|
||||
let mut prev = c.p0;
|
||||
for (k, len) in lens.iter_mut().enumerate().skip(1) {
|
||||
let p = c.eval(k as f64 / LENGTH_STEPS as f64);
|
||||
*len = prev.distance(p);
|
||||
prev = p;
|
||||
}
|
||||
for k in 1..=LENGTH_STEPS {
|
||||
lens[k] += lens[k - 1];
|
||||
}
|
||||
starts.push(total);
|
||||
total += lens[LENGTH_STEPS];
|
||||
table.push(lens);
|
||||
}
|
||||
starts.push(total);
|
||||
if !(total.is_finite() && total > 0.0) {
|
||||
return None;
|
||||
}
|
||||
// An outline whose points all lie on one line encloses nothing; one that crosses itself
|
||||
// (a bow tie) may have areas that cancel, and is still followed.
|
||||
let pts: Vec<Point> = segs.iter().flat_map(|c| [c.p0, c.p1, c.p2, c.p3]).collect();
|
||||
let first = pts.first().copied()?;
|
||||
let far = pts.iter().copied().max_by(|a, b| a.distance_squared(first).total_cmp(&b.distance_squared(first)))?;
|
||||
let dir = far - first;
|
||||
let width = pts.iter().map(|p| ((*p - first).cross(dir) / dir.hypot()).abs()).fold(0.0, f64::max);
|
||||
if !(dir.hypot() > 0.0 && width > 1e-9 * total) {
|
||||
return None;
|
||||
}
|
||||
Some(Self { segs, starts, table, eps: total * 1e-9 })
|
||||
}
|
||||
|
||||
fn total(&self) -> f64 {
|
||||
self.starts.last().copied().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The bounding box of the outline.
|
||||
fn bounds(&self) -> Option<Rect> {
|
||||
self.segs.iter().map(|c| c.bounding_box()).reduce(|a, b| a.union(b))
|
||||
}
|
||||
|
||||
/// The parameter of segment `i` `local` along it (clamped to the segment).
|
||||
fn seg_t(&self, i: usize, local: f64) -> f64 {
|
||||
let Some(lens) = self.table.get(i) else { return 0.0 };
|
||||
let k = lens.partition_point(|x| *x <= local).clamp(1, LENGTH_STEPS) - 1;
|
||||
let (l0, l1) = (lens[k], lens[k + 1]);
|
||||
let f = if l1 > l0 { ((local - l0) / (l1 - l0)).clamp(0.0, 1.0) } else { 0.0 };
|
||||
(k as f64 + f) / LENGTH_STEPS as f64
|
||||
}
|
||||
|
||||
/// The segment and its parameter at place `s` (wrapping around); a place at the very end of a
|
||||
/// segment is the start of the next.
|
||||
fn locate(&self, s: f64) -> (usize, f64) {
|
||||
let n = self.segs.len();
|
||||
let s = s.rem_euclid(self.total());
|
||||
let mut i = self.starts.partition_point(|x| *x <= s).clamp(1, n) - 1;
|
||||
if self.starts.get(i + 1).is_some_and(|end| end - s <= self.eps) {
|
||||
i = (i + 1) % n;
|
||||
return (i, 0.0);
|
||||
}
|
||||
(i, self.seg_t(i, s - self.starts[i]))
|
||||
}
|
||||
|
||||
fn point(&self, s: f64) -> Point {
|
||||
let (i, t) = self.locate(s);
|
||||
self.segs.get(i).map_or(Point::ZERO, |c| c.eval(t))
|
||||
}
|
||||
|
||||
/// The anchors strictly inside the stretch from `a`, `len` long: each one's distance from `a`
|
||||
/// and the segment it starts.
|
||||
fn anchors_in(&self, a: f64, len: f64) -> Vec<(f64, usize)> {
|
||||
let (total, n) = (self.total(), self.segs.len());
|
||||
let a = a.rem_euclid(total);
|
||||
let (lo, hi) = (a + self.eps, a + len - self.eps);
|
||||
let mut out = vec![];
|
||||
for lap in [0.0, total] {
|
||||
let from = self.starts[..n].partition_point(|x| x + lap <= lo);
|
||||
for (i, start) in self.starts[..n].iter().enumerate().skip(from) {
|
||||
let p = start + lap;
|
||||
if p >= hi {
|
||||
break;
|
||||
}
|
||||
out.push((p - a, i));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The piece of the outline from `a`, `len` long, which no anchor lies inside.
|
||||
fn piece(&self, a: f64, len: f64) -> CubicBez {
|
||||
let (i, t0) = self.locate(a);
|
||||
let Some(c) = self.segs.get(i) else { return CubicBez::new(Point::ZERO, Point::ZERO, Point::ZERO, Point::ZERO) };
|
||||
// At a segment's start (a place at the end of the one before counts as that), none of it is behind.
|
||||
let behind = if t0 == 0.0 { 0.0 } else { a.rem_euclid(self.total()) - self.starts[i] };
|
||||
let t1 = self.seg_t(i, behind + len);
|
||||
c.subsegment(t0..t1.max(t0))
|
||||
}
|
||||
|
||||
/// The direction the outline leaves place `s` in (`into` false) or reaches it in (`into` true).
|
||||
fn tangent(&self, s: f64, into: bool) -> Vec2 {
|
||||
let n = self.segs.len();
|
||||
let (i, t) = self.locate(s);
|
||||
let c = match (into, t <= 0.0) {
|
||||
(true, true) => self.segs[(i + n - 1) % n],
|
||||
(true, false) => self.segs[i].subsegment(0.0..t),
|
||||
(false, _) => self.segs[i].subsegment(t..1.0),
|
||||
};
|
||||
cubic_tangent(&c, into)
|
||||
}
|
||||
|
||||
/// How sharply the outline turns at the anchor starting segment `i` (radians, 0 to π).
|
||||
fn turn(&self, i: usize) -> f64 {
|
||||
let s = self.starts[i];
|
||||
let (din, dout) = (self.tangent(s, true), self.tangent(s, false));
|
||||
din.cross(dout).atan2(din.dot(dout)).abs()
|
||||
}
|
||||
|
||||
/// The place on the outline nearest `q`.
|
||||
fn nearest(&self, q: Point) -> f64 {
|
||||
use vectorcraft_geom::kurbo::ParamCurveNearest;
|
||||
let mut best = (f64::INFINITY, 0.0);
|
||||
for (i, c) in self.segs.iter().enumerate() {
|
||||
let near = c.nearest(q, 1e-9);
|
||||
if near.distance_sq < best.0 {
|
||||
// The length at that parameter, from the table.
|
||||
let lens = &self.table[i];
|
||||
let x = near.t * LENGTH_STEPS as f64;
|
||||
let k = (x.floor() as usize).min(LENGTH_STEPS - 1);
|
||||
let local = lens[k] + (lens[k + 1] - lens[k]) * (x - k as f64);
|
||||
best = (near.distance_sq, self.starts[i] + local);
|
||||
}
|
||||
}
|
||||
best.1
|
||||
}
|
||||
|
||||
/// The cubic standing for the stretch from `a`, `len` long, and how far it strays from it: the
|
||||
/// piece of the segment when no anchor lies inside, otherwise a cubic fitted to the outline with
|
||||
/// its ends along the outline's own directions there.
|
||||
fn fit(&self, a: f64, len: f64) -> (CubicBez, f64) {
|
||||
let (p0, p3) = (self.point(a), self.point(a + len));
|
||||
if len <= self.eps {
|
||||
return (CubicBez::new(p0, p0, p0, p0), 0.0);
|
||||
}
|
||||
let anchors = self.anchors_in(a, len);
|
||||
if anchors.is_empty() {
|
||||
return (self.piece(a, len), 0.0);
|
||||
}
|
||||
let qs: Vec<Point> = (1..FIT_POINTS).map(|i| self.point(a + len * i as f64 / FIT_POINTS as f64)).collect();
|
||||
let (d0, d1) = (self.tangent(a, false), -self.tangent(a + len, true));
|
||||
let unit = |v: Vec2| if v.hypot() > 0.0 { Some(v / v.hypot()) } else { None };
|
||||
let straight = CubicBez::new(p0, p0.lerp(p3, 1.0 / 3.0), p0.lerp(p3, 2.0 / 3.0), p3);
|
||||
let c = match (unit(d0), unit(d1)) {
|
||||
(Some(d0), Some(d1)) => fit_tangents(&qs, (p0, d0), (p3, d1)),
|
||||
_ => None,
|
||||
}
|
||||
.unwrap_or(straight);
|
||||
// Both ways: the outline's points from the cubic, and the cubic's points from the outline.
|
||||
// The outline is checked at even steps and at every anchor inside, so a spike or a notch
|
||||
// narrower than a step is not missed.
|
||||
use vectorcraft_geom::kurbo::ParamCurveNearest;
|
||||
let mut at: Vec<f64> = (0..=CHECK_POINTS).map(|i| len * i as f64 / CHECK_POINTS as f64).chain(anchors.iter().map(|(o, _)| *o)).collect();
|
||||
at.sort_by(f64::total_cmp);
|
||||
let line: Vec<Point> = at.iter().map(|o| self.point(a + o)).collect();
|
||||
let off_outline = line.iter().map(|q| c.nearest(*q, 1e-9).distance_sq.sqrt()).fold(0.0, f64::max);
|
||||
let off_cubic = (0..=CHECK_POINTS)
|
||||
.map(|i| {
|
||||
let p = c.eval(i as f64 / CHECK_POINTS as f64);
|
||||
line.windows(2)
|
||||
.map(|w| {
|
||||
let (u, v) = (w[0], w[1]);
|
||||
let d = v - u;
|
||||
let t = if d.hypot2() > 0.0 { ((p - u).dot(d) / d.hypot2()).clamp(0.0, 1.0) } else { 0.0 };
|
||||
p.distance(u + d * t)
|
||||
})
|
||||
.fold(f64::INFINITY, f64::min)
|
||||
})
|
||||
.fold(0.0, f64::max);
|
||||
(c, off_outline.max(off_cubic))
|
||||
}
|
||||
|
||||
/// The border edge of `side` from `f0` to `f1` (0 to 1 along it, rising), run the way the side
|
||||
/// runs, and when it strays from the outline by more than `tol`, where along the side (0 to 1)
|
||||
/// a mesh line through an anchor would help: the sharpest corner inside it, or the anchor
|
||||
/// nearest its middle when none is sharp.
|
||||
fn edge(&self, side: &Side, f0: f64, f1: f64, tol: f64) -> (CubicBez, Option<f64>) {
|
||||
let len = (f1 - f0) * side.len;
|
||||
let a = side.at(if side.reversed { f1 } else { f0 });
|
||||
let (c, off) = self.fit(a, len);
|
||||
let c = if side.reversed { CubicBez::new(c.p3, c.p2, c.p1, c.p0) } else { c };
|
||||
if off <= tol || side.len <= 0.0 {
|
||||
return (c, None);
|
||||
}
|
||||
let anchors = self.anchors_in(a, len);
|
||||
let sharpest = anchors.iter().map(|&(o, i)| (self.turn(i), o)).max_by(|x, y| x.0.total_cmp(&y.0));
|
||||
let at = match sharpest {
|
||||
Some((turn, o)) if turn > SHARP_TURN => Some(o),
|
||||
_ => anchors.iter().map(|&(o, _)| o).min_by(|x, y| (x - len / 2.0).abs().total_cmp(&(y - len / 2.0).abs())),
|
||||
};
|
||||
let split = at.map(|o| if side.reversed { f1 - o / side.len } else { f0 + o / side.len });
|
||||
(c, split.filter(|f| *f > f0 && *f < f1))
|
||||
}
|
||||
|
||||
/// The cuts (0 to 1) of the lines across sides `one` and `other`: `even`, and as many more as
|
||||
/// keep every border edge along them within `tol` of the outline (see [`Self::edge`]), at most
|
||||
/// [`MAX_BORDER_LINES`] in all.
|
||||
fn cuts(&self, one: &Side, other: &Side, even: Vec<f64>, tol: f64) -> Vec<f64> {
|
||||
let mut cuts = even;
|
||||
for _ in 0..MAX_BORDER_ROUNDS {
|
||||
let mut add: Vec<f64> =
|
||||
[one, other].iter().flat_map(|s| cuts.windows(2).filter_map(|w| self.edge(s, w[0], w[1], tol).1).collect::<Vec<_>>()).collect();
|
||||
add.retain(|f| cuts.iter().all(|c| (c - f).abs() > 1e-9));
|
||||
add.truncate((MAX_BORDER_LINES + 1).saturating_sub(cuts.len()));
|
||||
if add.is_empty() {
|
||||
break;
|
||||
}
|
||||
cuts.extend(add);
|
||||
cuts.sort_by(f64::total_cmp);
|
||||
cuts.dedup_by(|x, y| (*x - *y).abs() <= 1e-9);
|
||||
}
|
||||
cuts
|
||||
}
|
||||
|
||||
/// The points along `side` at `knots` (rising, 0 to 1).
|
||||
fn side_points(&self, side: &Side, knots: &[f64]) -> Vec<Point> {
|
||||
knots.iter().map(|f| self.point(side.at(*f))).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// The sorted union of `cuts` and `SIDE` even steps.
|
||||
fn side_knots(cuts: &[f64]) -> Vec<f64> {
|
||||
let mut k: Vec<f64> = (0..SIDE).map(|i| i as f64 / (SIDE - 1) as f64).chain(cuts.iter().copied()).collect();
|
||||
k.sort_by(f64::total_cmp);
|
||||
k.dedup_by(|x, y| (*x - *y).abs() <= 1e-9);
|
||||
k
|
||||
}
|
||||
|
||||
/// The handles of one cubic standing for two edges that meet at point `mid.0`, whose handles
|
||||
/// towards them are `mid.1` (towards `a`) and `mid.2` (towards `b`), as offsets from their outer
|
||||
/// ends: each outer handle grown back by the share of the curve its edge had, which undoes the
|
||||
@@ -599,19 +977,104 @@ impl GradientMesh {
|
||||
|
||||
/// Create Gradient Mesh for a path: the outline split at its corners, coloured per `appearance`.
|
||||
pub fn for_path(path: &PathData, rows: u32, cols: u32, base: Color, appearance: MeshAppearance, highlight: f64) -> Option<Self> {
|
||||
let mut m = Self::for_path_with(path, rows, cols, &|_| (base, 1.0))?;
|
||||
m.highlight(appearance, highlight);
|
||||
Self::for_path_shaded(path, rows, cols, &|_| (base, 1.0), appearance, highlight)
|
||||
}
|
||||
|
||||
/// As [`Self::for_path_with`], then lightened per `appearance` ([`Self::highlight`]), measured
|
||||
/// by where each line crosses the patch.
|
||||
pub fn for_path_shaded(
|
||||
path: &PathData,
|
||||
rows: u32,
|
||||
cols: u32,
|
||||
color_at: &dyn Fn(Point) -> (Color, f32),
|
||||
appearance: MeshAppearance,
|
||||
highlight: f64,
|
||||
) -> Option<Self> {
|
||||
let (mut m, us, vs) = Self::build_for_path(path, rows, cols, color_at)?;
|
||||
m.highlight_at(appearance, highlight, &us, &vs);
|
||||
Some(m)
|
||||
}
|
||||
|
||||
/// Create Gradient Mesh for a path: the outline split at its corners, each point taking the
|
||||
/// colour and opacity `color_at` gives at its position (a gradient fill's, say).
|
||||
pub fn for_path_with(path: &PathData, rows: u32, cols: u32, color_at: &dyn Fn(Point) -> (Color, f32)) -> Option<Self> {
|
||||
let coons = Coons::from_path(path)?;
|
||||
Self::build_for_path(path, rows, cols, color_at).map(|(m, ..)| m)
|
||||
}
|
||||
|
||||
/// The mesh of [`Self::for_path_with`] and where its column and row lines cross the patch (0
|
||||
/// to 1). Its border lies on the outline of the path's largest subpath (#2752): the patch's
|
||||
/// corners are the outline's places nearest the corners of its bounding box (when those don't
|
||||
/// come in order round the outline, the top left one and the places a quarter, a half and three
|
||||
/// quarters of the way round from it), its points on the
|
||||
/// border sit on the outline, and each border edge is the piece of the outline between its
|
||||
/// points, a single cubic. Where the outline bends through an anchor between two of the `rows`
|
||||
/// × `cols` lines and no single cubic follows it within [`BORDER_TOLERANCE`], the mesh takes a
|
||||
/// line through that anchor (its sharpest corner first), as many as that needs up to
|
||||
/// [`MAX_BORDER_LINES`] each way. `None` when the path has no outline or a point that isn't
|
||||
/// finite; an outline whose points all lie on one line gives the mesh of its bounding box.
|
||||
fn build_for_path(path: &PathData, rows: u32, cols: u32, color_at: &dyn Fn(Point) -> (Color, f32)) -> Option<(Self, Vec<f64>, Vec<f64>)> {
|
||||
if !path.subpaths.iter().flat_map(|s| &s.anchors).all(|a| a.p.is_finite() && a.h_in.is_finite() && a.h_out.is_finite()) {
|
||||
return None;
|
||||
}
|
||||
let (us, vs) = (even_steps(cols), even_steps(rows));
|
||||
let Some((o, sides)) = Outline::new(path).and_then(|o| {
|
||||
let r = o.bounds()?;
|
||||
let [tl, mut tr, mut br, mut bl] = [(r.x0, r.y0), (r.x1, r.y0), (r.x1, r.y1), (r.x0, r.y1)].map(|(x, y)| o.nearest(Point::new(x, y)));
|
||||
let span = |a: f64, b: f64| (b - a).rem_euclid(o.total());
|
||||
// Corners met out of order around the outline would make sides that overlap: the
|
||||
// other three then go a quarter of the way round from the top left each.
|
||||
let around = span(tl, tr) + span(tr, br) + span(br, bl) + span(bl, tl);
|
||||
if (around - o.total()).abs() > 1e-6 * o.total() {
|
||||
let quarter = o.total() / 4.0;
|
||||
(tr, br, bl) = (tl + quarter, tl + 2.0 * quarter, tl + 3.0 * quarter);
|
||||
}
|
||||
let sides = [
|
||||
Side { start: tl, len: span(tl, tr), reversed: false },
|
||||
Side { start: tr, len: span(tr, br), reversed: false },
|
||||
Side { start: br, len: span(br, bl), reversed: true },
|
||||
Side { start: bl, len: span(bl, tl), reversed: true },
|
||||
];
|
||||
Some((o, sides))
|
||||
}) else {
|
||||
// No outline to follow: the patch of the flattened outline (its bounding box when it
|
||||
// encloses nothing), with each border edge fitted.
|
||||
let coons = Coons::from_path(path)?;
|
||||
let mut m = Self::from_grid(&us, &vs, &|u, v| coons.eval(u, v), &|_, _, p| color_at(p));
|
||||
m.fit_border(&coons, &us, &vs);
|
||||
return Some((m, us, vs));
|
||||
};
|
||||
let [top, right, bottom, left] = sides;
|
||||
let size = o.bounds().map_or(0.0, |r| r.width() + r.height());
|
||||
let tol = BORDER_TOLERANCE.min(size * 1e-3).max(o.eps);
|
||||
let us = o.cuts(&top, &bottom, us, tol);
|
||||
let vs = o.cuts(&left, &right, vs, tol);
|
||||
let (ku, kv) = (side_knots(&us), side_knots(&vs));
|
||||
let coons = Coons {
|
||||
top: o.side_points(&top, &ku),
|
||||
right: o.side_points(&right, &kv),
|
||||
bottom: o.side_points(&bottom, &ku),
|
||||
left: o.side_points(&left, &kv),
|
||||
knots: Some(Box::new([ku.clone(), kv.clone(), ku, kv])),
|
||||
};
|
||||
let mut m = Self::from_grid(&us, &vs, &|u, v| coons.eval(u, v), &|_, _, p| color_at(p));
|
||||
m.fit_border(&coons, &us, &vs);
|
||||
Some(m)
|
||||
let (rows, cols) = (m.rows as usize, m.cols as usize);
|
||||
let edges = [(0, &top), (rows, &bottom)]
|
||||
.map(|(r, side)| (r, side, true))
|
||||
.into_iter()
|
||||
.chain([(0, &left), (cols, &right)].map(|(c, side)| (c, side, false)));
|
||||
for (line, side, along_u) in edges {
|
||||
let steps = if along_u { &us } else { &vs };
|
||||
for (k, w) in steps.windows(2).enumerate() {
|
||||
let (a, b, out, back) =
|
||||
if along_u { (m.idx(line, k), m.idx(line, k + 1), H_RIGHT, H_LEFT) } else { (m.idx(k, line), m.idx(k + 1, line), H_DOWN, H_UP) };
|
||||
let (c, _) = o.edge(side, w[0], w[1], tol);
|
||||
m.points[a].p = c.p0;
|
||||
m.points[b].p = c.p3;
|
||||
m.points[a].handles[out] = c.p1 - c.p0;
|
||||
m.points[b].handles[back] = c.p2 - c.p3;
|
||||
}
|
||||
}
|
||||
Some((m, us, vs))
|
||||
}
|
||||
|
||||
/// Give each edge on the mesh's border the handles that make it follow the outline it stands
|
||||
@@ -657,11 +1120,21 @@ impl GradientMesh {
|
||||
/// the edges, by at most `highlight` %. Each point lightens towards white in its own colour
|
||||
/// model (paper white, no ink, for CMYK), so a CMYK or grey mesh keeps its inks (#3232).
|
||||
pub fn highlight(&mut self, appearance: MeshAppearance, highlight: f64) {
|
||||
let even = |n: u32| (0..=n).map(|i| f64::from(i) / f64::from(n)).collect::<Vec<_>>();
|
||||
let (us, vs) = (even(self.cols), even(self.rows));
|
||||
self.highlight_at(appearance, highlight, &us, &vs);
|
||||
}
|
||||
|
||||
/// As [`Self::highlight`], with the column lines at `us` and the row lines at `vs` (0 to 1
|
||||
/// across the patch); a mesh whose lines don't match them is left as it is.
|
||||
fn highlight_at(&mut self, appearance: MeshAppearance, highlight: f64, us: &[f64], vs: &[f64]) {
|
||||
let h = (highlight / 100.0).clamp(0.0, 1.0) as f32;
|
||||
let (rows, cols) = (self.rows as usize, self.cols as usize);
|
||||
for r in 0..=rows {
|
||||
for c in 0..=cols {
|
||||
let (u, v) = (c as f64 / cols as f64, r as f64 / rows as f64);
|
||||
if !self.is_valid() || us.len() != cols + 1 || vs.len() != rows + 1 {
|
||||
return;
|
||||
}
|
||||
for (r, &v) in vs.iter().enumerate() {
|
||||
for (c, &u) in us.iter().enumerate() {
|
||||
let d = ((u - 0.5).abs().max((v - 0.5).abs()) * 2.0) as f32; // 0 centre … 1 edge
|
||||
let w = match appearance {
|
||||
MeshAppearance::Flat => 0.0,
|
||||
|
||||
@@ -56,4 +56,30 @@ impl LayerColor {
|
||||
LayerColor::Custom(c) => c,
|
||||
}
|
||||
}
|
||||
|
||||
/// The color as text: a preset's name from [`LAYER_COLORS`], else `#rrggbb` (lowercase).
|
||||
pub fn label_text(self) -> String {
|
||||
let preset = match self {
|
||||
LayerColor::Preset(i) => LAYER_COLORS.get(usize::from(i)),
|
||||
LayerColor::Custom(_) => None,
|
||||
};
|
||||
preset.map_or_else(
|
||||
|| {
|
||||
let [r, g, b] = self.rgb();
|
||||
format!("#{r:02x}{g:02x}{b:02x}")
|
||||
},
|
||||
|(name, _)| (*name).to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
/// The color [`LayerColor::label_text`] names: a preset's name exactly as [`LAYER_COLORS`]
|
||||
/// spells it, or `#rrggbb` with six hex digits. None for anything else.
|
||||
pub fn from_label_text(s: &str) -> Option<Self> {
|
||||
if let Some(i) = LAYER_COLORS.iter().position(|(name, _)| *name == s) {
|
||||
return u8::try_from(i).ok().map(LayerColor::Preset);
|
||||
}
|
||||
let hex = s.strip_prefix('#').filter(|h| h.len() == 6 && h.bytes().all(|b| b.is_ascii_hexdigit()))?;
|
||||
let byte = |k: usize| u8::from_str_radix(&hex[k..k + 2], 16).ok();
|
||||
Some(LayerColor::Custom([byte(0)?, byte(2)?, byte(4)?]))
|
||||
}
|
||||
}
|
||||
@@ -239,10 +239,12 @@ impl Node {
|
||||
NodeKind::Intertwine { children } => children.iter().fold(None, |acc, c| vectorcraft_geom::union_opt(acc, c.reach(symbols))),
|
||||
// The frame and each shape the image's own fills and strokes paint, a stroke's grown by
|
||||
// the farthest it can reach from its shape.
|
||||
NodeKind::Image(_) => crate::hit::image_paint(self).iter().fold(self.geometric(symbols), |acc, (bp, _, item)| {
|
||||
let o = if let crate::AppearanceItem::Stroke(s) = item { s.reach() } else { 0.0 };
|
||||
vectorcraft_geom::union_opt(acc, (!bp.elements().is_empty()).then(|| bp.bounding_box().inflate(o, o)))
|
||||
}),
|
||||
NodeKind::Image(_) => {
|
||||
crate::hit::image_paint(symbols.map(SymbolBoxes::document), self).iter().fold(self.geometric(symbols), |acc, (bp, _, item)| {
|
||||
let o = if let crate::AppearanceItem::Stroke(s) = item { s.reach() } else { 0.0 };
|
||||
vectorcraft_geom::union_opt(acc, (!bp.elements().is_empty()).then(|| bp.bounding_box().inflate(o, o)))
|
||||
})
|
||||
}
|
||||
// Keys and steps placed on a spine, turned to it with Align to Path, can reach past the
|
||||
// keys and the spine.
|
||||
NodeKind::Blend { children, spec } => {
|
||||
@@ -371,7 +373,10 @@ impl Node {
|
||||
NodeKind::SymbolInstance { symbol, xf } => {
|
||||
let b = match symbols.and_then(|s| Some((s, s.art(symbol)?))) {
|
||||
Some((s, art)) if !s.art_strokes() => xf.transform_rect_bbox(art.geometric),
|
||||
Some((_, art)) if moves_only(*xf) => xf.transform_rect_bbox(art.visual),
|
||||
// A symbol drawn at other than its natural size goes through its 9-slice guides.
|
||||
Some((s, art)) if moves_only(*xf) && s.document().symbol_slice_map_of(symbol, art.geometric, *xf).is_none() => {
|
||||
xf.transform_rect_bbox(art.visual)
|
||||
}
|
||||
Some((s, art)) => {
|
||||
let placed = xf.transform_rect_bbox(art.geometric);
|
||||
match s.placed_visual(symbol, *xf).or_else(|| s.sliced_visual(symbol, *xf)) {
|
||||
|
||||
@@ -60,6 +60,38 @@ impl ImageBlob {
|
||||
img.write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png).ok()?;
|
||||
Some(Self::png(png))
|
||||
}
|
||||
|
||||
/// The image as a grayscale PNG: `f` gives each pixel's gray level from its (straight) RGB, and
|
||||
/// the PNG has an alpha channel only when some pixel isn't opaque. `f` runs once per distinct
|
||||
/// color. `None` when the image can't be decoded, or when its file stores grayscale already
|
||||
/// and no visible pixel changed.
|
||||
pub fn map_gray(&self, mut f: impl FnMut([u8; 3]) -> u8) -> Option<ImageBlob> {
|
||||
let img = rgba_within(&self.bytes, MAX_RASTER_ALLOC)?;
|
||||
let (width, height) = img.dimensions();
|
||||
let mut memo: HashMap<[u8; 3], u8> = HashMap::new();
|
||||
let mut changed = self.gray_source().is_none();
|
||||
let mut opaque = true;
|
||||
let mut gray_alpha = Vec::with_capacity(img.len() / 2);
|
||||
for px in img.pixels() {
|
||||
let rgb = [px.0[0], px.0[1], px.0[2]];
|
||||
let level = *memo.entry(rgb).or_insert_with(|| f(rgb));
|
||||
changed |= px.0[3] > 0 && [level; 3] != rgb;
|
||||
opaque &= px.0[3] == 255;
|
||||
gray_alpha.extend_from_slice(&[level, px.0[3]]);
|
||||
}
|
||||
if !changed {
|
||||
return None;
|
||||
}
|
||||
let mut png = Vec::new();
|
||||
let mut out = Cursor::new(&mut png);
|
||||
if opaque {
|
||||
let levels = gray_alpha.iter().step_by(2).copied().collect();
|
||||
image::GrayImage::from_raw(width, height, levels)?.write_to(&mut out, image::ImageFormat::Png).ok()?;
|
||||
} else {
|
||||
image::GrayAlphaImage::from_raw(width, height, gray_alpha)?.write_to(&mut out, image::ImageFormat::Png).ok()?;
|
||||
}
|
||||
Some(Self::png(png))
|
||||
}
|
||||
}
|
||||
|
||||
/// How a grayscale image's file stores its pixels ([`ImageBlob::gray_source`]).
|
||||
|
||||
+394
-45
@@ -1,13 +1,12 @@
|
||||
//! Where a path's strokes paint, from the path's geometry without building their outlines: the
|
||||
//! visual bounds and the clicks of a stroke take in its alignment, its width profile, miter spikes
|
||||
//! up to the miter limit, projecting caps and arrowheads.
|
||||
//! up to the miter limit, projecting caps and arrowheads. Butt caps end at the path's end points.
|
||||
|
||||
use vectorcraft_geom::bez::{CORNER_COS, has_length, kink, segments, subpaths, tangent, unit};
|
||||
use vectorcraft_geom::hit::fill_contains;
|
||||
use vectorcraft_geom::kurbo::{ParamCurveArclen, ParamCurveNearest};
|
||||
use vectorcraft_geom::kurbo::{ParamCurveArclen, ParamCurveExtrema, ParamCurveNearest};
|
||||
use vectorcraft_geom::{BezPath, FillRule, ParamCurve, PathSeg, Point, Rect, Shape, Vec2};
|
||||
|
||||
use crate::appearance::HEAD_REACH;
|
||||
use crate::{Appearance, ArrowAlign, Dash, LineCap, LineJoin, StrokeAlign, StrokeLayer, WidthProfile};
|
||||
|
||||
/// Arc-length accuracy for placing a point along a profile (document points).
|
||||
@@ -101,6 +100,82 @@ fn profile_at(profile: &WidthProfile, segs: &[PathSeg], closed: bool, i: usize,
|
||||
w
|
||||
}
|
||||
|
||||
/// Does the curvature of `seg` stay below one over `side` all along it? A curve's first derivative
|
||||
/// is a mix of its three control differences, and its second a mix of their two differences, which
|
||||
/// bound the curvature from above. Then the edges of a stroke reaching `side` from it do not fold
|
||||
/// back, and those edges reach farthest at the ends of `seg` or where its tangent is square to an
|
||||
/// axis.
|
||||
fn gentle(seg: &PathSeg, side: f64) -> bool {
|
||||
let c = seg.to_cubic();
|
||||
let d = [c.p1 - c.p0, c.p2 - c.p1, c.p3 - c.p2].map(|v| v * 3.0);
|
||||
let along = unit(d[0] + d[1] + d[2]);
|
||||
let slowest = d.iter().map(|v| v.dot(along)).fold(f64::INFINITY, f64::min);
|
||||
let bend = 2.0 * (d[1] - d[0]).hypot().max((d[2] - d[1]).hypot());
|
||||
slowest > 0.0 && side * bend < slowest * slowest
|
||||
}
|
||||
|
||||
/// The box a stroke reaching `side` from `seg` paints across it, from the directions its tangent
|
||||
/// can take: the tangent is a mix of the nonzero control differences, so when their directions all
|
||||
/// lie within a quarter turn of the directions' sum, every point across the stroke is in `seg`'s
|
||||
/// box moved `side` along a normal of one of those directions. `None` when they do not. A handle on
|
||||
/// its end point adds no direction, so a straight curve with one measures as a line.
|
||||
fn turning_box(seg: &PathSeg, side: f64) -> Option<Rect> {
|
||||
let c = seg.to_cubic();
|
||||
let d = [c.p1 - c.p0, c.p2 - c.p1, c.p3 - c.p2];
|
||||
let moving = || d.iter().map(|v| (*v, v.hypot())).filter(|(_, h)| *h > 0.0).map(|(v, h)| v / h);
|
||||
let along = unit(moving().fold(Vec2::ZERO, |s, v| s + v));
|
||||
if moving().any(|v| v.dot(along) <= 0.0) {
|
||||
return None;
|
||||
}
|
||||
let angle = |v: &Vec2| along.cross(*v).atan2(along.dot(*v));
|
||||
let lo = moving().min_by(|a, b| angle(a).total_cmp(&angle(b)))?;
|
||||
let hi = moving().max_by(|a, b| angle(a).total_cmp(&angle(b)))?;
|
||||
let n = |t: Vec2| (Vec2::new(t.y, -t.x) * side).to_point();
|
||||
let one_side = round_join_box(Point::ZERO, lo, hi, side).unwrap_or_else(|| Rect::from_points(n(lo), n(hi)));
|
||||
let reach = one_side.union(Rect::new(-one_side.x1, -one_side.y1, -one_side.x0, -one_side.y0));
|
||||
let b = ParamCurveExtrema::bounding_box(seg);
|
||||
Some(Rect::new(b.x0 + reach.x0, b.y0 + reach.y0, b.x1 + reach.x1, b.y1 + reach.y1))
|
||||
}
|
||||
|
||||
/// The box a stroke reaching `side` from `seg` paints when it stops square at both ends of `seg`:
|
||||
/// the corners at the ends and the points across the stroke where the tangent is square to an axis.
|
||||
/// A curve that bends tighter than `side` is halved up to `depth` times, and a piece still bending
|
||||
/// that much is measured by [`turning_box`], or grown by `side` on every side when its tangent
|
||||
/// turns too far.
|
||||
fn swept_box(seg: &PathSeg, side: f64, depth: u32) -> Rect {
|
||||
if !matches!(seg, PathSeg::Line(_)) && !gentle(seg, side) {
|
||||
if depth == 0 {
|
||||
return turning_box(seg, side).unwrap_or_else(|| ParamCurveExtrema::bounding_box(seg).inflate(side, side));
|
||||
}
|
||||
return swept_box(&seg.subsegment(0.0..0.5), side, depth - 1).union(swept_box(&seg.subsegment(0.5..1.0), side, depth - 1));
|
||||
}
|
||||
let across = |t: f64| {
|
||||
let (p, tn) = (seg.eval(t), tangent(seg, t));
|
||||
let n = Vec2::new(-tn.y, tn.x) * side;
|
||||
[p + n, p - n]
|
||||
};
|
||||
let [a, b] = across(0.0);
|
||||
seg.extrema().into_iter().chain([1.0]).flat_map(across).fold(Rect::from_points(a, b), |r, p| r.union_pt(p))
|
||||
}
|
||||
|
||||
/// The box of the arc a round join paints `side` from `at`, where the path turns from tangent
|
||||
/// `t_in` to `t_out`: the arc runs on the outer side of the turn, and its box comes from its ends
|
||||
/// and the points on it square to an axis. A joint where the path turns back on itself is given
|
||||
/// the whole disc's box, and one where it does not turn paints no arc (`None`).
|
||||
fn round_join_box(at: Point, t_in: Vec2, t_out: Vec2, side: f64) -> Option<Rect> {
|
||||
let (t_in, t_out) = (unit(t_in), unit(t_out));
|
||||
let turn = t_in.cross(t_out);
|
||||
if turn.abs() < 1e-12 {
|
||||
return (t_in.dot(t_out) < 0.0).then(|| Rect::from_center_size(at, (2.0 * side, 2.0 * side)));
|
||||
}
|
||||
let s = turn.signum();
|
||||
let outer = |t: Vec2| Vec2::new(t.y, -t.x) * s;
|
||||
let (a, b) = (outer(t_in), outer(t_out));
|
||||
let axes = [Vec2::new(1.0, 0.0), Vec2::new(-1.0, 0.0), Vec2::new(0.0, 1.0), Vec2::new(0.0, -1.0)];
|
||||
let arc = Rect::from_points(at + a * side, at + b * side);
|
||||
Some(axes.into_iter().filter(|d| a.cross(*d) * s >= 0.0 && d.cross(b) * s >= 0.0).fold(arc, |r, d| r.union_pt(at + d * side)))
|
||||
}
|
||||
|
||||
/// Is `p` inside the convex polygon `pts` (either winding), or within `tol` of it?
|
||||
fn in_convex(pts: &[Point], p: Point, tol: f64) -> bool {
|
||||
let n = pts.len();
|
||||
@@ -115,39 +190,81 @@ fn in_convex(pts: &[Point], p: Point, tol: f64) -> bool {
|
||||
|
||||
impl StrokeLayer {
|
||||
/// Does bounding this stroke need its path's shape: miter spikes, projecting caps, arrowheads,
|
||||
/// or an alignment that depends on whether the path is closed? Else it reaches evenly round it.
|
||||
/// an alignment that depends on whether the path is closed, or butt caps, which stop at the
|
||||
/// ends of an open path? Else it reaches evenly round it.
|
||||
fn needs_shape(&self) -> bool {
|
||||
self.join == LineJoin::Miter
|
||||
|| self.cap == LineCap::Square
|
||||
|| self.cap != LineCap::Round
|
||||
|| self.start_arrow.is_some()
|
||||
|| self.end_arrow.is_some()
|
||||
|| self.align != StrokeAlign::Center
|
||||
}
|
||||
|
||||
/// The box this stroke paints along `bp`, whose geometric bounds are `bounds`: the bounds grown
|
||||
/// by the body's reach, with the tips of the miter spikes the limit keeps, the corners of
|
||||
/// The box this stroke paints along `segs`, the segments with length of an open subpath, when
|
||||
/// its caps are butt caps: each segment's [`swept_box`] and, with round joins, the arc each
|
||||
/// joint between segments paints ([`round_join_box`]), a shallow one included.
|
||||
/// `None` when `segs` is empty. Butt dashes stop inside the same box. The heads and miter tips
|
||||
/// are measured apart. A segment in the middle is measured only when its own box grown by
|
||||
/// `side`, which holds everything it paints, is not inside the box so far.
|
||||
fn butt_body(&self, segs: &[PathSeg], side: f64) -> Option<Rect> {
|
||||
let (first, last) = (segs.first()?, segs.last()?);
|
||||
let ends = swept_box(first, side, 3).union(swept_box(last, side, 3));
|
||||
let body = segs.iter().skip(1).take(segs.len().saturating_sub(2)).fold(ends, |b, s| {
|
||||
if b.contains_rect(ParamCurveExtrema::bounding_box(s).inflate(side, side)) { b } else { b.union(swept_box(s, side, 3)) }
|
||||
});
|
||||
if self.join != LineJoin::Round {
|
||||
return Some(body);
|
||||
}
|
||||
Some(joints(segs, false).fold(body, |b, (at, t_in, t_out)| round_join_box(at, t_in, t_out, side).map_or(b, |j| b.union(j))))
|
||||
}
|
||||
|
||||
/// The box this stroke paints along `bp` (geometric bounds `bounds`): an open subpath with butt
|
||||
/// caps by [`Self::butt_body`], every other subpath by the box of its segments grown by its
|
||||
/// reach, with `tips` and, when `measure_tips`, the miter spikes the limit keeps, the corners of
|
||||
/// projecting caps and the reach of the arrowheads round their end points.
|
||||
pub fn bounds_on(&self, bp: &BezPath, bounds: Rect) -> Rect {
|
||||
fn reach_along(&self, bp: &BezPath, bounds: Rect, tips: Option<Rect>, measure_tips: bool) -> Rect {
|
||||
let els = bp.elements();
|
||||
let mut out = bounds;
|
||||
let subs = subpaths(bp);
|
||||
let mut out = tips.map_or(bounds, |t| bounds.union(t));
|
||||
let mut side_max: f64 = 0.0;
|
||||
// Every subpath reaches evenly unless one is open and butt-capped; only then is each
|
||||
// subpath's own box needed.
|
||||
let even_only = self.cap != LineCap::Butt || subs.iter().all(|s| s.1);
|
||||
let mut even = even_only.then_some(bounds);
|
||||
// Dashes put a cap on every dash: projecting ones reach a corner's diagonal anywhere.
|
||||
let capped_dashes = self.cap == LineCap::Square && self.dash.as_ref().is_some_and(Dash::is_dashed);
|
||||
for (r, closed) in subpaths(bp) {
|
||||
for (r, closed) in subs {
|
||||
let side = self.side_reach(closed);
|
||||
side_max = side_max.max(if capped_dashes { side * std::f64::consts::SQRT_2 } else { side });
|
||||
let butt = self.cap == LineCap::Butt && !closed;
|
||||
if !butt {
|
||||
side_max = side_max.max(if capped_dashes { side * std::f64::consts::SQRT_2 } else { side });
|
||||
}
|
||||
if side <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let square = self.cap == LineCap::Square && !closed;
|
||||
let miters = measure_tips && self.join == LineJoin::Miter;
|
||||
if !(butt || square || miters || !even_only) {
|
||||
continue;
|
||||
}
|
||||
let segs = drawn(&els[r]);
|
||||
if self.join == LineJoin::Miter {
|
||||
if miters {
|
||||
for (at, t_in, t_out) in joints(&segs, closed) {
|
||||
if let Some([_, tip, _]) = miter(at, t_in, t_out, side, self.miter_limit) {
|
||||
out = out.union_pt(tip);
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.cap == LineCap::Square && !closed {
|
||||
if butt {
|
||||
if let Some(body) = self.butt_body(&segs, side) {
|
||||
out = out.union(body);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !even_only {
|
||||
even = vectorcraft_geom::union_opt(even, segs.iter().map(ParamCurveExtrema::bounding_box).reduce(|a, b| a.union(b)));
|
||||
}
|
||||
if square {
|
||||
for (at, dir) in ends(&segs).into_iter().flatten() {
|
||||
let n = Vec2::new(-dir.y, dir.x) * side;
|
||||
out = out.union_pt(at + dir * side + n).union_pt(at + dir * side - n);
|
||||
@@ -158,7 +275,15 @@ impl StrokeLayer {
|
||||
let r = self.head_reach(end);
|
||||
out = out.union(Rect::from_center_size(at, (2.0 * r, 2.0 * r)));
|
||||
}
|
||||
out.union(bounds.inflate(side_max, side_max))
|
||||
even.map_or(out, |e| out.union(e.inflate(side_max, side_max)))
|
||||
}
|
||||
|
||||
/// The box this stroke paints along `bp`, whose geometric bounds are `bounds`: the bounds grown
|
||||
/// by the body's reach, with the tips of the miter spikes the limit keeps, the corners of
|
||||
/// projecting caps and the reach of the arrowheads round their end points. An open subpath
|
||||
/// with butt caps does not reach past its ends ([`Self::butt_body`]).
|
||||
pub fn bounds_on(&self, bp: &BezPath, bounds: Rect) -> Rect {
|
||||
self.reach_along(bp, bounds, None, true)
|
||||
}
|
||||
|
||||
/// The tip of the miter spike this stroke paints where segment `b` follows segment `a` in a
|
||||
@@ -176,30 +301,7 @@ impl StrokeLayer {
|
||||
/// every joint of `bp` ([`Self::miter_tip`]) or `None` when there are none, instead of measured
|
||||
/// along `bp`. It measures the projecting caps and arrowheads as [`Self::bounds_on`] does.
|
||||
pub fn bounds_with_tips(&self, bp: &BezPath, bounds: Rect, tips: Option<Rect>) -> Rect {
|
||||
let els = bp.elements();
|
||||
let mut out = tips.map_or(bounds, |t| bounds.union(t));
|
||||
let mut side_max: f64 = 0.0;
|
||||
// Dashes put a cap on every dash: projecting ones reach a corner's diagonal anywhere.
|
||||
let capped_dashes = self.cap == LineCap::Square && self.dash.as_ref().is_some_and(Dash::is_dashed);
|
||||
for (r, closed) in subpaths(bp) {
|
||||
let side = self.side_reach(closed);
|
||||
side_max = side_max.max(if capped_dashes { side * std::f64::consts::SQRT_2 } else { side });
|
||||
if side <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
if self.cap == LineCap::Square && !closed {
|
||||
let segs = drawn(&els[r]);
|
||||
for (at, dir) in ends(&segs).into_iter().flatten() {
|
||||
let n = Vec2::new(-dir.y, dir.x) * side;
|
||||
out = out.union_pt(at + dir * side + n).union_pt(at + dir * side - n);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (at, _, end) in head_ends(self, bp) {
|
||||
let r = self.head_reach(end);
|
||||
out = out.union(Rect::from_center_size(at, (2.0 * r, 2.0 * r)));
|
||||
}
|
||||
out.union(bounds.inflate(side_max, side_max))
|
||||
self.reach_along(bp, bounds, tips, false)
|
||||
}
|
||||
|
||||
/// Does this stroke along `bp` (filled with `rule`) paint within `tol` of `p`? Inside and
|
||||
@@ -283,18 +385,19 @@ impl StrokeLayer {
|
||||
}
|
||||
|
||||
/// Is `p` within `tol` of where the start (`end == false`) or end arrowhead at `at` (the path
|
||||
/// leaving along `out`) can paint? Heads are at most about 4.6 weights long and 4 wide. With
|
||||
/// leaving along `out`) can paint? A head reaches [`crate::Arrowhead::reach`] weights from its tip and
|
||||
/// [`crate::Arrowhead::half_width`] either side of its line (4.6 and 2 for most). With
|
||||
/// [`ArrowAlign::Extend`] a head points along `out` with its tip up to its length (or the cap)
|
||||
/// past the end; with [`ArrowAlign::Tip`] its tip is on the end and it lies back along the
|
||||
/// chord the path bends through under it, so anywhere within its length of the end.
|
||||
fn head_contains(&self, at: Point, out: Vec2, end: bool, p: Point, tol: f64) -> bool {
|
||||
let hw = self.arrow_weight(end);
|
||||
let v = p - at;
|
||||
let Some(kind) = (if end { self.end_arrow } else { self.start_arrow }) else { return false };
|
||||
let (hw, v) = (self.arrow_weight(end), p - at);
|
||||
match self.arrow_align {
|
||||
ArrowAlign::Tip => v.hypot() <= HEAD_REACH * hw + tol,
|
||||
ArrowAlign::Tip => v.hypot() <= kind.reach() * hw + tol,
|
||||
ArrowAlign::Extend => {
|
||||
let along = v.dot(out);
|
||||
v.cross(out).abs() <= 2.0 * hw + tol && along <= 4.0 * hw + self.width / 2.0 + tol && along >= -HEAD_REACH * hw - tol
|
||||
v.cross(out).abs() <= kind.half_width() * hw + tol && along <= 4.0 * hw + self.width / 2.0 + tol && along >= -kind.reach() * hw - tol
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -314,7 +417,7 @@ impl Appearance {
|
||||
}
|
||||
|
||||
/// [`Self::stroked_bounds`] with the box of each visible stroke that needs the path's shape
|
||||
/// (miter spikes, projecting caps, arrowheads or an alignment) given by `shaped`.
|
||||
/// (miter spikes, projecting or butt caps, arrowheads or an alignment) given by `shaped`.
|
||||
pub fn stroked_bounds_by(&self, bounds: Rect, mut shaped: impl FnMut(&StrokeLayer) -> Rect) -> Rect {
|
||||
self.painted_strokes().fold(bounds, |acc, s| {
|
||||
if !s.needs_shape() {
|
||||
@@ -325,6 +428,30 @@ impl Appearance {
|
||||
})
|
||||
}
|
||||
|
||||
/// Does this appearance have a visible butt-capped stroke?
|
||||
pub fn has_butt_stroke(&self) -> bool {
|
||||
self.painted_strokes().any(|s| s.cap == LineCap::Butt)
|
||||
}
|
||||
|
||||
/// Does a visible butt-capped stroke of this appearance run along an open subpath of `bp` with
|
||||
/// a segment that starts or ends at a slant, not square to the axes, once placed by `a`? The
|
||||
/// visual box's margin at the stroke's corners there depends on their direction, which a scale
|
||||
/// out of proportion changes.
|
||||
pub fn has_slanted_butt_stroke(&self, bp: &BezPath, a: vectorcraft_geom::Affine) -> bool {
|
||||
if !self.has_butt_stroke() {
|
||||
return false;
|
||||
}
|
||||
let slanted = |v: Vec2| {
|
||||
let d = unit(a * v.to_point() - a * Point::ZERO);
|
||||
(d.x * d.y).abs() > 1e-9
|
||||
};
|
||||
let els = bp.elements();
|
||||
subpaths(bp)
|
||||
.into_iter()
|
||||
.filter(|(_, closed)| !closed)
|
||||
.any(|(r, _)| drawn(&els[r]).iter().any(|s| slanted(tangent(s, 0.0)) || slanted(tangent(s, 1.0))))
|
||||
}
|
||||
|
||||
/// Does a visible stroke of this appearance along `bp` (filled with `rule`) paint within
|
||||
/// `tol` of `p` ([`StrokeLayer::hits`])?
|
||||
pub fn stroke_hit(&self, bp: &BezPath, rule: FillRule, p: Point, tol: f64) -> bool {
|
||||
@@ -332,6 +459,31 @@ impl Appearance {
|
||||
}
|
||||
}
|
||||
|
||||
/// How many symbol instances [`crate::Node::has_slanted_butt_stroke`] looks into for one object
|
||||
/// before it answers yes without looking further.
|
||||
pub const SLANT_INSTANCES: usize = 4096;
|
||||
|
||||
impl crate::Node {
|
||||
/// Does a visible butt-capped stroke of this object, of an object in it or of an object in a
|
||||
/// symbol instance's art from `symbols`, placed by `a`, run along an open path with a segment
|
||||
/// at a slant ([`Appearance::has_slanted_butt_stroke`])? Past [`SLANT_INSTANCES`] instances it
|
||||
/// answers yes.
|
||||
pub fn has_slanted_butt_stroke(&self, symbols: &crate::SymbolBoxes, a: vectorcraft_geom::Affine) -> bool {
|
||||
self.slanted_butt(symbols, a, &std::cell::Cell::new(SLANT_INSTANCES))
|
||||
}
|
||||
|
||||
/// [`Self::has_slanted_butt_stroke`] with `left` instances still to look into.
|
||||
fn slanted_butt(&self, symbols: &crate::SymbolBoxes, a: vectorcraft_geom::Affine, left: &std::cell::Cell<usize>) -> bool {
|
||||
if let crate::NodeKind::SymbolInstance { symbol, xf } = &self.kind {
|
||||
let Some(l) = left.get().checked_sub(1) else { return true };
|
||||
left.set(l);
|
||||
return symbols.measure_art(symbol, |art| art.slanted_butt(symbols, a * *xf, left)).unwrap_or(false);
|
||||
}
|
||||
(self.appearance.has_butt_stroke() && self.stroke_path().is_some_and(|bp| self.appearance.has_slanted_butt_stroke(&bp, a)))
|
||||
|| self.children().is_some_and(|c| c.iter().any(|c| c.slanted_butt(symbols, a, left)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
@@ -378,7 +530,7 @@ mod tests {
|
||||
let (small, big) = (at(100.0).bounds_on(&l, b), at(300.0).bounds_on(&l, b));
|
||||
assert!(big.x1 > small.x1 + 15.0 && big.y1 > small.y1 + 7.0, "{small:?} → {big:?}");
|
||||
// A 300% head on a 2 pt stroke is 24 pt long and wide: its tip, past the end, is inside.
|
||||
assert!(big.x1 >= 100.0 + 24.0 && big.y1 >= 12.0 && big.x0 == -1.0, "{big:?}");
|
||||
assert!(big.x1 >= 100.0 + 24.0 && big.y1 >= 12.0 && big.x0 == 0.0, "{big:?}");
|
||||
// The start has no head.
|
||||
assert_eq!(at(300.0).head_reach(false), 0.0);
|
||||
}
|
||||
@@ -405,6 +557,203 @@ mod tests {
|
||||
assert_eq!(stroke(4.0, |s| s.align = StrokeAlign::Outside).bounds_on(&sq, b), b.inflate(4.0, 4.0));
|
||||
}
|
||||
|
||||
/// An open path through `points`.
|
||||
fn polyline(points: &[(f64, f64)]) -> BezPath {
|
||||
let mut bp = BezPath::new();
|
||||
for (i, p) in points.iter().enumerate() {
|
||||
if i == 0 { bp.move_to(*p) } else { bp.line_to(*p) }
|
||||
}
|
||||
bp
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn butt_caps_stop_at_the_ends_of_an_open_path() {
|
||||
let butt = |f: fn(&mut StrokeLayer)| {
|
||||
stroke(2.0, |s| {
|
||||
s.cap = LineCap::Butt;
|
||||
f(s);
|
||||
})
|
||||
};
|
||||
// A vertical line reaches sideways only.
|
||||
let v = polyline(&[(50.0, 10.0), (50.0, 60.0)]);
|
||||
assert_eq!(butt(|_| {}).bounds_on(&v, v.bounding_box()), Rect::new(49.0, 10.0, 51.0, 60.0));
|
||||
// A 3-4-5 diagonal: its corners reach 0.8 pt across x and 0.6 pt across y.
|
||||
let d = polyline(&[(0.0, 0.0), (30.0, 40.0)]);
|
||||
let b = butt(|_| {}).bounds_on(&d, d.bounding_box());
|
||||
for (got, want) in [(b.x0, -0.8), (b.y0, -0.6), (b.x1, 30.8), (b.y1, 40.6)] {
|
||||
assert!((got - want).abs() < 1e-9, "{b:?}");
|
||||
}
|
||||
// The dashes of a butt stroke stop inside the line as well, and an open path ignores the
|
||||
// alignment.
|
||||
let dashed = butt(|s| s.dash = Some(Dash { pattern: vec![4.0, 2.0], offset: 0.0, align_corners: false }));
|
||||
assert_eq!(dashed.bounds_on(&v, v.bounding_box()), Rect::new(49.0, 10.0, 51.0, 60.0));
|
||||
assert_eq!(butt(|s| s.align = StrokeAlign::Inside).bounds_on(&v, v.bounding_box()), Rect::new(49.0, 10.0, 51.0, 60.0));
|
||||
// A round cap reaches half the weight past each end, a square one the corners.
|
||||
let round = butt(|s| s.cap = LineCap::Round).bounds_on(&v, v.bounding_box());
|
||||
assert_eq!(round, Rect::new(49.0, 9.0, 51.0, 61.0));
|
||||
let square = butt(|s| s.cap = LineCap::Square).bounds_on(&v, v.bounding_box());
|
||||
assert_eq!(square, Rect::new(49.0, 9.0, 51.0, 61.0));
|
||||
// Two strokes union.
|
||||
let mut ap = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 2.0);
|
||||
ap.stroke_mut().unwrap().cap = LineCap::Butt;
|
||||
let wide = stroke(10.0, |s| s.cap = LineCap::Round);
|
||||
ap.items.push(crate::AppearanceItem::Stroke(wide));
|
||||
let vb = v.bounding_box();
|
||||
assert_eq!(ap.stroked_bounds(vb, || v.clone()), Rect::new(45.0, 5.0, 55.0, 65.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_butt_paths_joins_and_curves_stay_inside_the_box() {
|
||||
// A corner in the middle: the round join reaches a full half weight straight down, outside
|
||||
// the turn, the ends do not, and a miter reaches its tip.
|
||||
let zig = polyline(&[(0.0, 0.0), (20.0, 20.0), (40.0, 0.0)]);
|
||||
let round = stroke(4.0, |s| {
|
||||
s.cap = LineCap::Butt;
|
||||
s.join = LineJoin::Round;
|
||||
})
|
||||
.bounds_on(&zig, zig.bounding_box());
|
||||
assert!((round.y1 - 22.0).abs() < 1e-9, "the round join's reach: {round:?}");
|
||||
assert!((round.x0 + 2.0_f64.sqrt()).abs() < 1e-9 && (round.x1 - 40.0 - 2.0_f64.sqrt()).abs() < 1e-9, "the ends' corners: {round:?}");
|
||||
// A round join paints its arc on the outer side of a turn too shallow to count as a
|
||||
// corner, and nothing where the path goes straight on.
|
||||
let round_butt = stroke(4.0, |s| {
|
||||
s.cap = LineCap::Butt;
|
||||
s.join = LineJoin::Round;
|
||||
});
|
||||
let shallow = polyline(&[(0.0, 0.25), (50.0, 0.0), (100.0, 0.25)]);
|
||||
let b = round_butt.bounds_on(&shallow, shallow.bounding_box());
|
||||
assert!((b.y0 + 2.0).abs() < 1e-12, "{b:?}");
|
||||
let straight = polyline(&[(0.0, 0.0), (0.1, 0.0), (100.0, 0.0)]);
|
||||
assert_eq!(round_butt.bounds_on(&straight, straight.bounding_box()), Rect::new(0.0, -2.0, 100.0, 2.0));
|
||||
let miter = stroke(4.0, |s| s.cap = LineCap::Butt).bounds_on(&zig, zig.bounding_box());
|
||||
assert!((miter.y1 - (20.0 + 2.0 * 2.0_f64.sqrt())).abs() < 1e-9, "the miter tip: {miter:?}");
|
||||
// An arc over the top whose ends leave the page straight down: the top reaches half the
|
||||
// weight past the path, and the bottom stops at the ends.
|
||||
let mut arc = BezPath::new();
|
||||
arc.move_to((0.0, 50.0));
|
||||
arc.curve_to((0.0, 22.4), (22.4, 0.0), (50.0, 0.0));
|
||||
arc.curve_to((77.6, 0.0), (100.0, 22.4), (100.0, 50.0));
|
||||
let b = stroke(4.0, |s| s.cap = LineCap::Butt).bounds_on(&arc, arc.bounding_box());
|
||||
assert!((b.y0 + 2.0).abs() < 1e-9 && (b.x0 + 2.0).abs() < 1e-9 && (b.x1 - 102.0).abs() < 1e-9, "{b:?}");
|
||||
assert!((b.y1 - 50.0).abs() < 1e-9, "{b:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_butt_curve_with_a_handle_on_its_end_point_stops_near_that_end() {
|
||||
let butt = stroke(2.0, |s| s.cap = LineCap::Butt);
|
||||
// A straight curve whose first or last handle sits on its end point measures like a line.
|
||||
let mut first = BezPath::new();
|
||||
first.move_to((50.0, 10.0));
|
||||
first.curve_to((50.0, 10.0), (50.0, 40.0), (50.0, 60.0));
|
||||
let mut last = BezPath::new();
|
||||
last.move_to((50.0, 10.0));
|
||||
last.curve_to((50.0, 20.0), (50.0, 60.0), (50.0, 60.0));
|
||||
for bp in [first, last] {
|
||||
assert_eq!(butt.bounds_on(&bp, bp.bounding_box()), Rect::new(49.0, 10.0, 51.0, 60.0));
|
||||
}
|
||||
// A bend that leaves its start straight up from a handle on the start point: the stroke
|
||||
// reaches only a little below the start, not half the weight.
|
||||
let mut bend = BezPath::new();
|
||||
bend.move_to((0.0, 50.0));
|
||||
bend.curve_to((0.0, 50.0), (0.0, 0.0), (50.0, 0.0));
|
||||
let b = stroke(4.0, |s| s.cap = LineCap::Butt).bounds_on(&bend, bend.bounding_box());
|
||||
assert!(b.y1 >= 50.0 && b.y1 < 50.2, "{b:?}");
|
||||
assert!((b.x0 + 2.0).abs() < 1e-9 && (b.y0 + 2.0).abs() < 1e-9 && (b.x1 - 50.0).abs() < 1e-9, "{b:?}");
|
||||
// Nearly a cusp: the curve turns back at its lowest point, where its tangent is level and
|
||||
// the stroke reaches the full half weight below it.
|
||||
let (a0, a1, a2) = (1e5 * 81.0 / 256.0, -1e5 * 9.0 / 8.0, 1e5);
|
||||
let x = |i: f64| 2e-9 * i / 3.0;
|
||||
let mut cusp = BezPath::new();
|
||||
cusp.move_to((x(0.0), a0));
|
||||
cusp.curve_to((x(1.0), a0 + a1 / 3.0), (x(2.0), a0 + 2.0 * a1 / 3.0 + a2 / 3.0), (x(3.0), a0 + a1 + a2));
|
||||
let b = butt.bounds_on(&cusp, cusp.bounding_box());
|
||||
assert!(b.y0 <= -1.0 + 1e-6, "{b:?}");
|
||||
// A curve far shorter than the weight still reaches half the weight above where its
|
||||
// tangent is level.
|
||||
let mut tiny = BezPath::new();
|
||||
tiny.move_to((0.0, 0.9e-12));
|
||||
tiny.curve_to((0.3e-12, -0.5e-12), (1.7e-12, -0.2e-12), (2e-12, 1e-12));
|
||||
let b = butt.bounds_on(&tiny, tiny.bounding_box());
|
||||
assert!(b.y0 <= -1.0 + 1e-9, "{b:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_butt_stroke_is_slanted_where_a_segment_starts_or_ends_at_a_slant() {
|
||||
let mut ap = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 2.0);
|
||||
ap.stroke_mut().unwrap().cap = LineCap::Butt;
|
||||
let id = vectorcraft_geom::Affine::IDENTITY;
|
||||
assert!(!ap.has_slanted_butt_stroke(&polyline(&[(0.0, 0.0), (0.0, 50.0), (30.0, 50.0)]), id));
|
||||
// Square ends, slanted segments in the middle.
|
||||
let apex = polyline(&[(0.0, 50.0), (0.0, 40.0), (10.0, 0.0), (20.0, 40.0), (20.0, 50.0)]);
|
||||
assert!(ap.has_slanted_butt_stroke(&apex, id));
|
||||
// A rotation slants a vertical line; a round cap is never slanted.
|
||||
let v = polyline(&[(0.0, 0.0), (0.0, 50.0)]);
|
||||
assert!(ap.has_slanted_butt_stroke(&v, vectorcraft_geom::Affine::rotate(0.3)));
|
||||
ap.stroke_mut().unwrap().cap = LineCap::Round;
|
||||
assert!(!ap.has_slanted_butt_stroke(&apex, id));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_slant_check_looks_into_at_most_4096_symbol_instances() {
|
||||
// S0 is a vertical butt line. Each of S1 to S12 holds two instances of the symbol before
|
||||
// it, so an instance of S12 holds 8,191 instances and none of them is slanted. Past 4,096
|
||||
// the check answers yes.
|
||||
let mut d = Document::new(400.0, 400.0);
|
||||
let mut ap = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 2.0);
|
||||
ap.stroke_mut().unwrap().cap = LineCap::Butt;
|
||||
let line = Node::path(d.alloc_id(), PathData::from_bezpath(&polyline(&[(0.0, 0.0), (0.0, 10.0)])), ap);
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "S0".into(), art: std::sync::Arc::new(line) });
|
||||
for k in 1..=12 {
|
||||
let mut instance = |dx: f64| {
|
||||
let xf = vectorcraft_geom::Affine::translate((dx, 0.0));
|
||||
std::sync::Arc::new(Node::new(d.alloc_id(), crate::NodeKind::SymbolInstance { symbol: format!("S{}", k - 1), xf }))
|
||||
};
|
||||
let pair = vec![instance(0.0), instance(20.0)];
|
||||
let art = Node::group(d.alloc_id(), pair);
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: format!("S{k}"), art: std::sync::Arc::new(art) });
|
||||
}
|
||||
let top = Node::new(d.alloc_id(), crate::NodeKind::SymbolInstance { symbol: "S12".into(), xf: vectorcraft_geom::Affine::IDENTITY });
|
||||
let symbols = d.symbol_boxes();
|
||||
assert!(top.has_slanted_butt_stroke(&symbols, vectorcraft_geom::Affine::IDENTITY));
|
||||
assert!(symbols.art_measured() <= SLANT_INSTANCES, "{}", symbols.art_measured());
|
||||
// One instance of S1 holds three instances, all looked into.
|
||||
let one = Node::new(d.alloc_id(), crate::NodeKind::SymbolInstance { symbol: "S1".into(), xf: vectorcraft_geom::Affine::IDENTITY });
|
||||
assert!(!one.has_slanted_butt_stroke(&d.symbol_boxes(), vectorcraft_geom::Affine::IDENTITY));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compound_path_measures_each_of_its_subpaths_by_its_own_ends() {
|
||||
// A closed square at the top and an open line below it: only the square reaches past its
|
||||
// own edges.
|
||||
let mut bp = BezPath::new();
|
||||
bp.extend(square().elements().iter().copied());
|
||||
bp.move_to((50.0, 150.0));
|
||||
bp.line_to((50.0, 200.0));
|
||||
let st = stroke(4.0, |s| s.cap = LineCap::Butt);
|
||||
assert_eq!(st.bounds_on(&bp, bp.bounding_box()), Rect::new(-2.0, -2.0, 102.0, 200.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_middle_segment_can_set_the_box_of_a_butt_path() {
|
||||
// The flat middle segment reaches a full half weight above the path, and the ends' corners
|
||||
// reach 10/51 of it below.
|
||||
let hat = polyline(&[(0.0, 50.0), (10.0, 0.0), (20.0, 0.0), (30.0, 50.0)]);
|
||||
let b = stroke(2.0, |s| {
|
||||
s.cap = LineCap::Butt;
|
||||
s.join = LineJoin::Bevel;
|
||||
})
|
||||
.bounds_on(&hat, hat.bounding_box());
|
||||
let corner = 10.0 / (10.0_f64 * 10.0 + 50.0 * 50.0).sqrt();
|
||||
assert!((b.y0 + 1.0).abs() < 1e-9 && (b.y1 - 50.0 - corner).abs() < 1e-9, "{b:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bounds_with_tips_measures_a_butt_line_as_bounds_on_does() {
|
||||
let v = polyline(&[(50.0, 10.0), (50.0, 60.0)]);
|
||||
let st = stroke(2.0, |s| s.cap = LineCap::Butt);
|
||||
assert_eq!(st.bounds_with_tips(&v, v.bounding_box(), None), Rect::new(49.0, 10.0, 51.0, 60.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clicks_hit_outside_strokes_on_their_side_only() {
|
||||
let sq = square();
|
||||
|
||||
@@ -158,6 +158,8 @@ pub const STYLE_LIBRARIES: &[BuiltinStyleLibrary] = &[
|
||||
BuiltinStyleLibrary { id: "gradient-finishes", name: "Gradient Finishes", make: gradient_finishes },
|
||||
BuiltinStyleLibrary { id: "shape-effects", name: "Shape Effects", make: shape_effects },
|
||||
BuiltinStyleLibrary { id: "blends-transparency", name: "Blends and Transparency", make: blends_transparency },
|
||||
BuiltinStyleLibrary { id: "game-hud", name: "Game HUD", make: game_hud },
|
||||
BuiltinStyleLibrary { id: "status-effects", name: "Status Effects", make: status_effects },
|
||||
];
|
||||
|
||||
/// Built-in library `id`, made once.
|
||||
@@ -453,6 +455,71 @@ fn blends_transparency() -> Vec<GraphicStyle> {
|
||||
]
|
||||
}
|
||||
|
||||
/// Game HUD: fills and glows for health, mana, damage and quest chrome (original arcade/HUD look).
|
||||
fn game_hud() -> Vec<GraphicStyle> {
|
||||
vec![
|
||||
style("Health Fill", vec![F(fill("#e74c3c")), S(stroke("#5c1a1a", 1.25))], vec![]),
|
||||
style(
|
||||
"Mana Glow",
|
||||
vec![F(fill("#3498db"))],
|
||||
vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 85.0, "blur": 7.0, "color": "#5dade2"}))],
|
||||
),
|
||||
style(
|
||||
"Damage Flash",
|
||||
vec![F(fill("#ff6b35"))],
|
||||
vec![fx("stylize.innerGlow", json!({"mode": "screen", "opacity": 80.0, "blur": 8.0, "color": "#ffe66d", "source": "edge"}))],
|
||||
),
|
||||
style("Quest Outline", vec![F(fill("#f7f3e8")), S(round("#2c3e50", 3.0))], vec![]),
|
||||
style(
|
||||
"Coin Shine",
|
||||
vec![F(fill("#f1c40f")), S(stroke("#b7950b", 1.5))],
|
||||
vec![fx("stylize.dropShadow", json!({"mode": "multiply", "opacity": 40.0, "x": 2.0, "y": 3.0, "blur": 4.0, "color": "#1d1b2a"}))],
|
||||
),
|
||||
style(
|
||||
"Boss Alert",
|
||||
vec![F(fill("#1a1a2e")), S(round("#e94560", 2.5))],
|
||||
vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 75.0, "blur": 6.0, "color": "#e94560"}))],
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/// Status Effects: buffs and debuffs for RPG/gamedev art (original, catalogue stylize effects only).
|
||||
fn status_effects() -> Vec<GraphicStyle> {
|
||||
let stealth = GraphicStyle {
|
||||
opacity: 0.45,
|
||||
blend: BlendMode::Normal,
|
||||
..style("Stealth Fade", vec![F(fill("#7f8c8d")), S(stroke("#2c3e50", 1.0))], vec![])
|
||||
};
|
||||
vec![
|
||||
style(
|
||||
"Poison Tint",
|
||||
vec![F(fill("#2ecc71"))],
|
||||
vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 80.0, "blur": 6.0, "color": "#58d68d"}))],
|
||||
),
|
||||
style(
|
||||
"Freeze Edge",
|
||||
vec![F(fill("#aed6f1")), S(stroke("#5dade2", 2.0))],
|
||||
vec![fx("stylize.innerGlow", json!({"mode": "screen", "opacity": 70.0, "blur": 5.0, "color": "#d6eaf8", "source": "edge"}))],
|
||||
),
|
||||
style(
|
||||
"Burn Aura",
|
||||
vec![F(fill("#e67e22"))],
|
||||
vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 85.0, "blur": 8.0, "color": "#f39c12"}))],
|
||||
),
|
||||
style(
|
||||
"Stun Pulse",
|
||||
vec![F(fill("#f4d03f"))],
|
||||
vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 75.0, "blur": 7.0, "color": "#f7dc6f"}))],
|
||||
),
|
||||
stealth,
|
||||
style(
|
||||
"Shield Buff",
|
||||
vec![F(fill("#85c1e9")), S(round("#2874a6", 2.0))],
|
||||
vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 70.0, "blur": 5.0, "color": "#5dade2"}))],
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -475,6 +542,14 @@ mod tests {
|
||||
}
|
||||
assert!(Arc::ptr_eq(&builtin_style_library("hand-drawn").unwrap(), &builtin_style_library("hand-drawn").unwrap()), "made once");
|
||||
assert!(builtin_style_library("nope").is_none());
|
||||
let game = builtin_style_library("game-hud").unwrap();
|
||||
assert_eq!(game.name, "Game HUD");
|
||||
let names: Vec<&str> = game.styles.iter().map(|g| g.name.as_str()).collect();
|
||||
assert_eq!(names, ["Health Fill", "Mana Glow", "Damage Flash", "Quest Outline", "Coin Shine", "Boss Alert"]);
|
||||
let status = builtin_style_library("status-effects").unwrap();
|
||||
assert_eq!(status.name, "Status Effects");
|
||||
let status_names: Vec<&str> = status.styles.iter().map(|g| g.name.as_str()).collect();
|
||||
assert_eq!(status_names, ["Poison Tint", "Freeze Edge", "Burn Aura", "Stun Pulse", "Stealth Fade", "Shield Buff"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -5,6 +5,52 @@ mod lookups;
|
||||
mod slices;
|
||||
mod units;
|
||||
|
||||
/// A guide label's text is a layer color's name or `#rrggbb`, and reads back as the same color;
|
||||
/// other spellings, an alpha part and a short form are not labels.
|
||||
#[test]
|
||||
fn guide_label_text_names_a_layer_color_or_rgb() {
|
||||
for (i, (name, _)) in LAYER_COLORS.iter().enumerate() {
|
||||
let c = LayerColor::Preset(u8::try_from(i).unwrap());
|
||||
assert_eq!(c.label_text(), *name);
|
||||
assert_eq!(LayerColor::from_label_text(name), Some(c));
|
||||
}
|
||||
let custom = LayerColor::Custom([0x0a, 0xbc, 0xff]);
|
||||
assert_eq!(custom.label_text(), "#0abcff");
|
||||
assert_eq!(LayerColor::from_label_text("#0ABCff"), Some(custom));
|
||||
// A preset past the layer colors has no name: its text is the color it draws in.
|
||||
let [r, g, b] = LayerColor::Preset(200).rgb();
|
||||
assert_eq!(LayerColor::Preset(200).label_text(), format!("#{r:02x}{g:02x}{b:02x}"));
|
||||
for bad in ["red", "Red ", "", "#", "#fff", "#0abcff00", "0abcff", "#0abcfg", "#0abcff"] {
|
||||
assert_eq!(LayerColor::from_label_text(bad), None, "{bad:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// An artboard's guide label: none unless set, left out of the JSON when none, and a preset past
|
||||
/// the layer colors reading as none.
|
||||
#[test]
|
||||
fn artboard_guide_label_defaults_and_skips_in_json() {
|
||||
let mut a = Document::new(100.0, 100.0).artboards[0].clone();
|
||||
assert_eq!(a.guide_label, None);
|
||||
let plain = serde_json::to_value(&a).unwrap();
|
||||
assert!(plain.get("guide_label").is_none(), "{plain}");
|
||||
a.guide_label = Some(LayerColor::Custom([1, 2, 3]));
|
||||
let v = serde_json::to_value(&a).unwrap();
|
||||
assert_eq!(v["guide_label"], serde_json::json!({"Custom": [1, 2, 3]}));
|
||||
assert_eq!(serde_json::from_value::<Artboard>(v).unwrap(), a);
|
||||
let read = |label: serde_json::Value| {
|
||||
let mut v = plain.clone();
|
||||
v["guide_label"] = label;
|
||||
serde_json::from_value::<Artboard>(v).map(|a| a.guide_label)
|
||||
};
|
||||
assert_eq!(read(serde_json::json!({"Preset": 26})).unwrap(), Some(LayerColor::Preset(26)));
|
||||
assert_eq!(read(serde_json::json!({"Preset": 27})).unwrap(), None);
|
||||
assert_eq!(read(serde_json::json!({"Preset": 255})).unwrap(), None);
|
||||
assert_eq!(read(serde_json::Value::Null).unwrap(), None);
|
||||
assert!(read(serde_json::json!({"Preset": -1})).is_err());
|
||||
assert!(read(serde_json::json!({"Tint": 1})).is_err());
|
||||
assert_eq!(serde_json::from_value::<Artboard>(plain).unwrap().guide_label, None, "older files have none");
|
||||
}
|
||||
|
||||
/// Zooming in on an inch ruler divided the inch into tenths (#2827). Its steps below an inch
|
||||
/// are binary fractions now, and the minor ticks between them stay binary too; every other
|
||||
/// unit is still read in tenths.
|
||||
@@ -146,6 +192,7 @@ fn art_on_a_locked_artboard_cannot_be_selected_or_edited() {
|
||||
show_video_safe_areas: false,
|
||||
video_ruler_pixel_aspect_ratio: 1.0,
|
||||
color: None,
|
||||
guide_label: None,
|
||||
});
|
||||
let rect = |d: &mut Document, r: Rect, appearance: Appearance| {
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -121,6 +121,12 @@ pub struct TextObject {
|
||||
/// vertical column's centre line), filled in with `cached_bounds` (not serialized).
|
||||
#[serde(skip)]
|
||||
pub cached_baselines: Vec<(Point, Point)>,
|
||||
/// Cached ink of the glyphs in text space, underline and strikethrough bars included
|
||||
/// (`vectorcraft_effects::preview::laid_out_ink`, `None` when nothing draws),
|
||||
/// with the `cached_bounds` it was measured with: filled in with `cached_bounds` by the engine's
|
||||
/// text edits, and stale once `cached_bounds` differs ([`Self::ink`]). Not serialized.
|
||||
#[serde(skip)]
|
||||
pub cached_ink: Option<(Rect, Option<Rect>)>,
|
||||
}
|
||||
|
||||
/// Conversion from stored character attributes to document-space type controls.
|
||||
|
||||
@@ -22,10 +22,10 @@ pub struct CharStyle {
|
||||
/// Leading in points; None = Auto (120% of size).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub leading: Option<f64>,
|
||||
/// Tracking in 1/1000 em.
|
||||
/// Tracking in 1/1000 em; in horizontal type, of the em as [`Self::h_scale`] draws it.
|
||||
#[serde(default)]
|
||||
pub tracking: f64,
|
||||
/// Manual pair spacing in 1/1000 em; None uses the automatic mode.
|
||||
/// Manual pair spacing in 1/1000 em, as [`Self::tracking`]; None uses the automatic mode.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub kerning: Option<f64>,
|
||||
/// Use glyph shapes for automatic pair spacing; false uses font metrics.
|
||||
|
||||
@@ -71,6 +71,7 @@ impl TextObject {
|
||||
self.xf *= Affine::scale_non_uniform(1.0 / sx, 1.0 / sy);
|
||||
self.cached_bounds = None;
|
||||
self.cached_baselines.clear();
|
||||
self.cached_ink = None;
|
||||
true
|
||||
}
|
||||
|
||||
@@ -97,6 +98,7 @@ impl TextObject {
|
||||
list_level_counts: Vec::new(),
|
||||
cached_bounds: None,
|
||||
cached_baselines: Vec::new(),
|
||||
cached_ink: None,
|
||||
}
|
||||
}
|
||||
/// The list counts the story's earlier frames carry into this frame at each level
|
||||
@@ -288,6 +290,12 @@ impl TextObject {
|
||||
TextKind::Point => Some(self.xf.transform_rect_bbox(self.cached_bounds.unwrap_or_else(|| self.estimate_bounds()))),
|
||||
}
|
||||
}
|
||||
/// The cached ink of the glyphs in text space ([`Self::cached_ink`]): `Some(None)` when nothing
|
||||
/// draws, and `None` when it was not measured with the current `cached_bounds`.
|
||||
pub fn ink(&self) -> Option<Option<Rect>> {
|
||||
let (bounds, ink) = self.cached_ink?;
|
||||
(self.cached_bounds == Some(bounds)).then_some(ink)
|
||||
}
|
||||
/// How far the characters' strokes paint past their glyphs, in document space: they scale
|
||||
/// with the type.
|
||||
pub fn char_stroke_outset(&self) -> f64 {
|
||||
@@ -359,6 +367,24 @@ impl TextObject {
|
||||
pub fn move_path_anchors(&mut self, refs: &[(usize, usize)], d: Vec2) -> bool {
|
||||
matches!(self.kind, TextKind::OnPath { .. }) && self.reshape_local(|path, to_text| move_anchors(path, to_text, refs, d))
|
||||
}
|
||||
/// Move the out (`out`) or in direction handle of anchor `ai` of subpath `si` of type on a
|
||||
/// path's path to `pos` in document space, as a path's anchor takes it
|
||||
/// ([`vectorcraft_geom::Anchor::set_handle`]; `independent`: that side only, the anchor a
|
||||
/// corner): a Direct Selection drag of a handle (#3565). The handle is set in document space,
|
||||
/// so a smooth anchor keeps its other handle's length as drawn; the brackets keep their
|
||||
/// fractions of the path. False, changing nothing, for other type, when the anchor doesn't
|
||||
/// exist, a degenerate `xf` or a path that would not be finite.
|
||||
pub fn set_path_handle(&mut self, (si, ai): (usize, usize), out: bool, pos: Point, independent: bool) -> bool {
|
||||
let xf = self.xf;
|
||||
matches!(self.kind, TextKind::OnPath { .. })
|
||||
&& self.reshape_local(|path, to_text| {
|
||||
let Some(a) = path.anchor_mut(si, ai) else { return false };
|
||||
let mut drawn = a.transform(xf);
|
||||
drawn.set_handle(out, pos, independent);
|
||||
*a = drawn.transform(to_text);
|
||||
true
|
||||
})
|
||||
}
|
||||
/// Edit a copy of the area frame with `f` (given the document → text space map) and keep it
|
||||
/// when `f` succeeds and the result is finite.
|
||||
fn reshape_area(&mut self, f: impl FnOnce(&mut PathData, Affine) -> bool) -> bool {
|
||||
|
||||
@@ -468,12 +468,14 @@ pub fn adjust(n: &Node, outer: Option<&ColorMap>, swatches: &SwatchKinds, images
|
||||
}
|
||||
|
||||
/// [`adjust`] for export and Expand Appearance: recoloured embedded images become new images in
|
||||
/// `d` (named by their content), and a symbol instance stands for its symbol's art.
|
||||
pub fn adjust_in_document(d: &mut Document, n: &Node) -> Option<Node> {
|
||||
/// `d` (named by their content), and a symbol instance stands for its symbol's art: `symbol_art`
|
||||
/// when given, otherwise the symbol's art with the instance's own appearance changes.
|
||||
pub fn adjust_in_document(d: &mut Document, n: &Node, symbol_art: Option<Node>) -> Option<Node> {
|
||||
let art;
|
||||
let src = match &n.kind {
|
||||
NodeKind::SymbolInstance { symbol, .. } if has_adjustment(n) => {
|
||||
let symbol = d.symbols.iter().find(|s| s.name == *symbol).map(|s| vectorcraft_doc::symbol_overrides::dynamic_art(&s.art, n));
|
||||
let symbol = symbol_art
|
||||
.or_else(|| d.symbols.iter().find(|s| s.name == *symbol).map(|s| vectorcraft_doc::symbol_overrides::dynamic_art(&s.art, n)));
|
||||
art = crate::reshape::as_art(Some(d), n, symbol.as_ref())?;
|
||||
&art
|
||||
}
|
||||
|
||||
+19
-18
@@ -112,7 +112,7 @@ fn bake_node(d: &mut Document, n: &Node) -> Option<Node> {
|
||||
}
|
||||
// Colour adjustments: the object recoloured (inside it too), then baked as it is.
|
||||
if has_adjustment(n)
|
||||
&& let Some(m) = adjust_in_document(d, n)
|
||||
&& let Some(m) = adjust_in_document(d, n, None)
|
||||
{
|
||||
return Some(bake_node(d, &m).unwrap_or(m));
|
||||
}
|
||||
@@ -124,7 +124,7 @@ fn bake_node(d: &mut Document, n: &Node) -> Option<Node> {
|
||||
if let Some(m) = crate::addons::halftone::halftone_art(d, n) {
|
||||
return Some(bake_pieces(d, m));
|
||||
}
|
||||
if let Some(m) = crate::revolve_art(n).or_else(|| crate::solid_art(n)) {
|
||||
if let Some(m) = crate::revolve_art_in(Some(d), n).or_else(|| crate::solid_art_in(Some(d), n)) {
|
||||
let m = evaluate_container(Some(d), &m).unwrap_or(m);
|
||||
return Some(bake_pieces(d, m));
|
||||
}
|
||||
@@ -348,16 +348,16 @@ fn moved_type_group(n: &Node) -> Option<Node> {
|
||||
/// outlined as [`evaluate_container`] outlines it, and the other members become what it makes of
|
||||
/// them. `None` when no type stays type.
|
||||
fn container_keeping_type(d: &Document, n: &Node) -> Option<Node> {
|
||||
fn keep(n: &mut Node, moves: &[serde_json::Value], clipping: bool, kept: &mut bool) {
|
||||
fn keep(d: &Document, n: &mut Node, moves: &[serde_json::Value], clipping: bool, kept: &mut bool) {
|
||||
let clips = n.clips();
|
||||
let Some(ch) = n.children_mut() else { return };
|
||||
for (i, c) in ch.iter_mut().enumerate() {
|
||||
let clipping = clipping || (clips && i == 0);
|
||||
if !matches!(c.kind, NodeKind::Text(_)) {
|
||||
if has_pathfinder(c) || has_own_paint(c) || !geometry_effects(c).is_empty() {
|
||||
outline_nested_text(Arc::make_mut(c), clipping);
|
||||
outline_nested_text(Some(d), Arc::make_mut(c), clipping);
|
||||
} else if c.children().is_some() {
|
||||
keep(Arc::make_mut(c), moves, clipping, kept);
|
||||
keep(d, Arc::make_mut(c), moves, clipping, kept);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -372,7 +372,7 @@ fn container_keeping_type(d: &Document, n: &Node) -> Option<Node> {
|
||||
}
|
||||
}
|
||||
None if clipping => outline_text(c).map(|o| Node { visible: c.visible, ..o }),
|
||||
None => as_art(None, c, None),
|
||||
None => as_art(Some(d), c, None),
|
||||
};
|
||||
if let Some(o) = art {
|
||||
*c = Arc::new(o);
|
||||
@@ -391,11 +391,11 @@ fn container_keeping_type(d: &Document, n: &Node) -> Option<Node> {
|
||||
}
|
||||
let mut m = crate::reshape::evaluated(Some(d), n);
|
||||
let mut kept = false;
|
||||
keep(&mut m, &moves, false, &mut kept);
|
||||
keep(d, &mut m, &moves, false, &mut kept);
|
||||
if !kept {
|
||||
return None;
|
||||
}
|
||||
reshape_leaves(&mut m, &fx);
|
||||
reshape_leaves(&mut m, &fx, &n.appearance.effects, Some(d));
|
||||
m.appearance.effects.retain(|e| !is_pathfinder(&e.id) && !is_geometry(&e.id));
|
||||
m.appearance.items.clear();
|
||||
m.appearance.contents_index = None;
|
||||
@@ -481,7 +481,7 @@ pub type StrokeArt<'a> = &'a mut dyn FnMut(&mut Document, &PathData, FillRule, &
|
||||
/// without transparency or effects of its own takes `n`'s place itself. Hidden fills, strokes and
|
||||
/// effects are dropped. `None` for other kinds of objects.
|
||||
pub fn expand_leaf(d: &mut Document, n: &Node, stroke_art: StrokeArt) -> Option<Node> {
|
||||
if let Some(m) = crate::revolve::expand_revolve_art(n).or_else(|| crate::solid_art(n)) {
|
||||
if let Some(m) = crate::revolve::expand_revolve_art_in(Some(d), n).or_else(|| crate::solid_art_in(Some(d), n)) {
|
||||
let m = evaluate_container(Some(d), &m).unwrap_or(m);
|
||||
return Some(bake_pieces(d, m));
|
||||
}
|
||||
@@ -547,10 +547,11 @@ pub fn expand_art(d: &mut Document, n: &mut Node, stroke_art: StrokeArt) {
|
||||
/// its gradient meshes clipped to the outline; a brushed stroke stays a stroke), in paint order, as
|
||||
/// the members of a group keeping `n`'s id, transparency, opacity mask and raster effects. A path
|
||||
/// with [`has_transform_art`](crate::has_transform_art) is first made into the art [`reshape`]
|
||||
/// makes of it, and each copy is baked this way. `None` when it has neither a stroke to outline nor
|
||||
/// a geometry effect, so bending first changes nothing.
|
||||
pub fn bake_appearance(n: &Node) -> Option<Node> {
|
||||
if let Some(m) = crate::revolve_art(n).or_else(|| crate::solid_art(n)) {
|
||||
/// makes of it, and each copy is baked this way; the swatches and patterns that live-effect plug-ins
|
||||
/// painting their pieces name are looked up in `doc`. `None` when it has neither a stroke to
|
||||
/// outline nor a geometry effect, so bending first changes nothing.
|
||||
pub fn bake_appearance(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
if let Some(m) = crate::revolve_art_in(doc, n).or_else(|| crate::solid_art_in(doc, n)) {
|
||||
return Some(m);
|
||||
}
|
||||
if !matches!(n.kind, NodeKind::Path { clipping: false, guide: false, .. } | NodeKind::Compound { .. }) {
|
||||
@@ -560,9 +561,9 @@ pub fn bake_appearance(n: &Node) -> Option<Node> {
|
||||
// patterns: the art Expand Appearance makes of it (each copy an object with its own strokes,
|
||||
// patterns and gradients), each piece baked in turn.
|
||||
if crate::has_transform_art(n)
|
||||
&& let Some(mut m) = reshape(None, n, None)
|
||||
&& let Some(mut m) = reshape(doc, n, None)
|
||||
{
|
||||
bake_appearance_pieces(&mut m);
|
||||
bake_appearance_pieces(doc, &mut m);
|
||||
return Some(m);
|
||||
}
|
||||
let outlined = |i: &AppearanceItem| paints(i) && matches!(i, AppearanceItem::Stroke(s) if s.brush.is_none());
|
||||
@@ -617,15 +618,15 @@ pub fn bake_appearance(n: &Node) -> Option<Node> {
|
||||
}
|
||||
|
||||
/// [`bake_appearance`] every path and compound path in `n` that it changes, in place.
|
||||
fn bake_appearance_pieces(n: &mut Node) {
|
||||
fn bake_appearance_pieces(doc: Option<&Document>, n: &mut Node) {
|
||||
if matches!(n.kind, NodeKind::Path { .. } | NodeKind::Compound { .. }) {
|
||||
if let Some(m) = bake_appearance(n) {
|
||||
if let Some(m) = bake_appearance(doc, n) {
|
||||
*n = m;
|
||||
}
|
||||
return;
|
||||
}
|
||||
for c in n.children_mut().into_iter().flatten() {
|
||||
bake_appearance_pieces(Arc::make_mut(c));
|
||||
bake_appearance_pieces(doc, Arc::make_mut(c));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -536,7 +536,7 @@ fn a_baked_stroke_keeps_its_raster_effects() {
|
||||
s.effects.push(fx("path.offsetPath", json!({"offset": 5.0})));
|
||||
s.effects.push(fx("stylize.dropShadow", json!({})));
|
||||
}
|
||||
let m = bake_appearance(&n).unwrap();
|
||||
let m = bake_appearance(None, &n).unwrap();
|
||||
let ch = m.children().unwrap();
|
||||
let Some(AppearanceItem::Fill(f)) = ch[1].appearance.items.first() else { panic!("the outline is filled") };
|
||||
assert_eq!(f.effects.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(), ["stylize.dropShadow"]);
|
||||
@@ -551,7 +551,7 @@ fn a_baked_stroke_leaves_dither_behind() {
|
||||
g.gradient.dither = true;
|
||||
s.paint = Paint::Gradient(Box::new(g));
|
||||
}
|
||||
let m = bake_appearance(&n).unwrap();
|
||||
let m = bake_appearance(None, &n).unwrap();
|
||||
let ch = m.children().unwrap();
|
||||
let Some(AppearanceItem::Fill(f)) = ch[1].appearance.items.first() else { panic!("the outline is filled") };
|
||||
let Paint::Gradient(g) = &f.paint else { panic!("a gradient fill") };
|
||||
@@ -567,7 +567,7 @@ fn a_baked_stroke_with_a_gradient_along_it_becomes_meshes_clipped_to_its_outline
|
||||
s.gradient_mode = vectorcraft_doc::StrokeGradientMode::Along;
|
||||
s.opacity = 0.5;
|
||||
}
|
||||
let m = bake_appearance(&n).unwrap();
|
||||
let m = bake_appearance(None, &n).unwrap();
|
||||
let ch = m.children().unwrap();
|
||||
let NodeKind::Group { children, clip: true } = &ch[1].kind else { panic!("a clip group: {:?}", ch[1].kind) };
|
||||
assert!(matches!(children[0].kind, NodeKind::Path { clipping: true, .. }));
|
||||
@@ -600,10 +600,10 @@ fn a_baked_transform_copy_keeps_its_scaled_stroke() {
|
||||
// Distort Appearance: the 200% copy's stroke is 4 pt, as outside the envelope, so its outline
|
||||
// is 204 pt wide.
|
||||
let n = transformed(Paint::None, Paint::solid(Color::BLACK), 2.0, json!({"scaleH": 200, "scaleV": 200, "copies": 1, "strokes": true}));
|
||||
let m = bake_appearance(&n).unwrap();
|
||||
let m = bake_appearance(None, &n).unwrap();
|
||||
let w = widths(&m);
|
||||
assert_eq!(w.len(), 2, "{w:?}");
|
||||
assert!((w[0] - 102.0).abs() < 0.5 && (w[1] - 204.0).abs() < 0.5, "{w:?}");
|
||||
assert!((w[1] - 102.0).abs() < 0.5 && (w[0] - 204.0).abs() < 0.5, "the original on top: {w:?}");
|
||||
assert!(baked_paths(&m).iter().all(|p| p.appearance.stroke().is_none()), "the strokes are outlined");
|
||||
}
|
||||
|
||||
@@ -611,7 +611,7 @@ fn a_baked_transform_copy_keeps_its_scaled_stroke() {
|
||||
fn a_baked_transform_copy_keeps_its_moved_pattern() {
|
||||
let paint = Paint::Pattern { pattern: "p".into(), xf: vectorcraft_geom::Affine::IDENTITY };
|
||||
let n = transformed(paint, Paint::None, 0.0, json!({"moveH": 30, "copies": 1, "patterns": true}));
|
||||
let m = bake_appearance(&n).unwrap();
|
||||
let m = bake_appearance(None, &n).unwrap();
|
||||
let xs: Vec<f64> = baked_paths(&m)
|
||||
.iter()
|
||||
.map(|p| match p.appearance.fill_paint() {
|
||||
@@ -619,14 +619,14 @@ fn a_baked_transform_copy_keeps_its_moved_pattern() {
|
||||
p => panic!("not a pattern: {p:?}"),
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(xs, vec![0.0, 30.0]);
|
||||
assert_eq!(xs, vec![30.0, 0.0], "the original on top");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_baked_transform_without_its_options_outlines_the_copies_together() {
|
||||
// Without Scale Strokes & Effects the copy keeps the 2 pt stroke: one outline of both squares.
|
||||
let n = transformed(Paint::None, Paint::solid(Color::BLACK), 2.0, json!({"scaleH": 200, "scaleV": 200, "copies": 1}));
|
||||
let m = bake_appearance(&n).unwrap();
|
||||
let m = bake_appearance(None, &n).unwrap();
|
||||
let w = widths(&m);
|
||||
assert_eq!(w.len(), 1, "{w:?}");
|
||||
assert!((w[0] - 202.0).abs() < 0.5, "{w:?}");
|
||||
|
||||
@@ -247,7 +247,7 @@ pub fn effect_catalog() -> Vec<EffectInfo> {
|
||||
"distort.transform",
|
||||
"Transform…",
|
||||
DT,
|
||||
"{scaleH: % (100), scaleV: % (100), moveH: pt (0; right = +), moveV: pt (0; down = +), rotate: deg (0; counter-clockwise; when the effect turns, reflects or scales the object unevenly, a gradient fitted to the object is fixed to its fit and turns with the object and with each copy), copies: 0..1000 (0; each copy transforms the last again; placed gradients transform with the object and with each copy), reflectX: bool (false; flips left to right), reflectY: bool (false; flips top to bottom), reference: 0..8 (4; the point of the bounds' 9-point grid it scales, rotates and reflects about: 0 top left, 4 centre, 8 bottom right), random: bool (false; each scale goes a random share of the way from 100 % to its value, each move and the angle a random share of theirs, differently for each object and the same on every redraw), objects: bool (true; Transform Objects: off, the geometry stays where it is and makes no copies, and only the two options below apply, once), patterns: bool (false; Transform Patterns: pattern fills and strokes transform along; with copies the original keeps its pattern and copy k takes the transformation k times), strokes: bool (false; Scale Strokes & Effects: stroke weights and dashes, and the distances of the effects on the fills and strokes, scale by the mean scale √|scaleH·scaleV|, copy k by its k-th power)}",
|
||||
"{scaleH: % (100), scaleV: % (100), moveH: pt (0; right = +), moveV: pt (0; down = +), rotate: deg (0; counter-clockwise; when the effect turns, reflects or scales the object unevenly, a gradient fitted to the object is fixed to its fit and turns with the object and with each copy), copies: 0..1000 (0; each copy transforms the last again and is painted below it, the object on top; a group's copies each hold all its members, and a group with a member that carries a geometry, Pathfinder or 3D effect copies each member in its place; placed gradients transform with the object and with each copy), reflectX: bool (false; flips left to right), reflectY: bool (false; flips top to bottom), reference: 0..8 (4; the point of the bounds' 9-point grid it scales, rotates and reflects about: 0 top left, 4 centre, 8 bottom right), random: bool (false; each scale goes a random share of the way from 100 % to its value, each move and the angle a random share of theirs, differently for each object and the same on every redraw), objects: bool (true; Transform Objects: off, the geometry stays where it is and makes no copies, and only the two options below apply, once), patterns: bool (false; Transform Patterns: pattern fills and strokes transform along; with copies the original keeps its pattern and copy k takes the transformation k times), strokes: bool (false; Scale Strokes & Effects: stroke weights and dashes, and the distances of the effects on the fills and strokes, scale by the mean scale √|scaleH·scaleV|, copy k by its k-th power)}",
|
||||
json!({"scaleH": 100.0, "scaleV": 100.0, "moveH": 0.0, "moveV": 0.0, "rotate": 0.0, "copies": 0, "reflectX": false, "reflectY": false, "reference": 4, "random": false, "objects": true, "patterns": false, "strokes": false}),
|
||||
),
|
||||
g(
|
||||
|
||||
@@ -3,6 +3,7 @@ use super::{
|
||||
Appearance, AppearanceItem, BezPath, Effect, FillRule, Node, NodeKind, PathData, Rect, StrokeLayer, Value, WarpStyle, distort, stroke, stylize,
|
||||
util, warp,
|
||||
};
|
||||
use vectorcraft_doc::Document;
|
||||
|
||||
/// Context the geometry effects may need from the object's appearance.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
@@ -22,6 +23,13 @@ pub struct GeomContext<'a> {
|
||||
/// The index in `appearance.items` of the fill or stroke whose own effects these are; `None`:
|
||||
/// the object's effects.
|
||||
pub item: Option<usize>,
|
||||
/// The document that the swatches and patterns named by a live-effect plug-in that paints its
|
||||
/// pieces are looked up in. Without one, a result that names any is refused.
|
||||
pub doc: Option<&'a Document>,
|
||||
/// The object, fill or stroke and effect index a deferred live-effect plug-in is listed by
|
||||
/// ([`vectorcraft_plugins::deferred`]). When `None`, it is the object `seed` names, `item` and
|
||||
/// the effect's index in the list being applied.
|
||||
pub stage: Option<vectorcraft_plugins::deferred::Stage>,
|
||||
}
|
||||
|
||||
impl<'a> GeomContext<'a> {
|
||||
@@ -32,7 +40,17 @@ impl<'a> GeomContext<'a> {
|
||||
_ => FillRule::NonZero,
|
||||
};
|
||||
let stroke = n.appearance.stroke().filter(|s| !s.paint.is_none() && s.width > 0.0);
|
||||
Self { stroke, rule, seed: n.id.0, appearance: Some(&n.appearance), item: None }
|
||||
Self { stroke, rule, seed: n.id.0, appearance: Some(&n.appearance), item: None, doc: None, stage: None }
|
||||
}
|
||||
|
||||
/// The context of effect `e`, one of `list`: unless [`Self::stage`] is set, a live-effect
|
||||
/// plug-in there is listed by `e`'s index in `list`.
|
||||
pub(crate) fn at(self, list: &[Effect], e: &Effect) -> Self {
|
||||
let stage = self.stage.or_else(|| {
|
||||
let effect = list.iter().position(|x| std::ptr::eq(x, e))?;
|
||||
Some(vectorcraft_plugins::deferred::Stage { object: self.seed, effect, item: self.item })
|
||||
});
|
||||
Self { stage, ..self }
|
||||
}
|
||||
|
||||
/// The context of the effects on `item`, one of the same object's appearance items: a
|
||||
@@ -331,7 +349,7 @@ pub(crate) fn apply_geometry_carrying_within(
|
||||
}
|
||||
let p = merged_params(&e.id, &e.params);
|
||||
let limit = *limit.get_or_insert_with(|| budget.at_least(Budget::of(path)));
|
||||
if let Some(next) = apply_one(&e.id, &p, &cur, b, ctx, limit) {
|
||||
if let Some(next) = apply_one(&e.id, &p, &cur, b, &ctx.at(effects, e), limit) {
|
||||
if let Some(c) = carry.as_deref_mut() {
|
||||
c.push(Carry::of(&e.id, &p, b, cur.bounds(), next.bounds(), ctx.seed));
|
||||
}
|
||||
@@ -373,8 +391,8 @@ fn builds_within(id: &str) -> bool {
|
||||
fn apply_unchecked(id: &str, p: &Value, path: &PathData, b: Rect, ctx: &GeomContext, max: usize) -> PathData {
|
||||
use util::{join, num};
|
||||
if let Some(plugin) = vectorcraft_plugins::effect::plugin_id(id) {
|
||||
let paints = vectorcraft_plugins::effect::Paints { appearance: ctx.appearance, item: ctx.item };
|
||||
return vectorcraft_plugins::effect::apply(plugin, p, path, b, ctx.rule, paints).unwrap_or_else(|| path.clone());
|
||||
let paints = vectorcraft_plugins::effect::Paints { appearance: ctx.appearance, item: ctx.item, at: ctx.stage };
|
||||
return vectorcraft_plugins::effect::apply(plugin, p, path, b, ctx.rule, paints, ctx.doc).unwrap_or_else(|| path.clone());
|
||||
}
|
||||
match id {
|
||||
"distort.freeDistort" => distort::free_distort(path, b, p, max),
|
||||
|
||||
+60
-46
@@ -67,25 +67,28 @@ pub type OutlineHook<'a> = &'a dyn Fn(&Node) -> Option<Node>;
|
||||
/// back to front: a compound shape as its area, a blend, an envelope or an object with a warp as
|
||||
/// the art it draws, and type and symbol instances as their art through `hook`. Past
|
||||
/// [`SYMBOL_ART_MAX_NODES`](crate::SYMBOL_ART_MAX_NODES) objects of symbol art, further
|
||||
/// instances add nothing.
|
||||
pub fn member_leaves(n: &Node, hook: Option<OutlineHook>) -> Vec<Node> {
|
||||
/// instances add nothing. The swatches and patterns that live-effect plug-ins painting their
|
||||
/// pieces name are looked up in `doc` ([`shape_art`]).
|
||||
pub fn member_leaves(doc: Option<&Document>, n: &Node, hook: Option<OutlineHook>) -> Vec<Node> {
|
||||
let mut out = vec![];
|
||||
leaves(n, hook, &SymbolNodes::default(), &mut out);
|
||||
leaves(doc, n, hook, &SymbolNodes::default(), &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
/// [`member_leaves`], with the symbol art given so far counted in `nodes`.
|
||||
fn leaves(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes, out: &mut Vec<Node>) {
|
||||
fn leaves(doc: Option<&Document>, n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes, out: &mut Vec<Node>) {
|
||||
if !n.visible {
|
||||
return;
|
||||
}
|
||||
// A blend, an envelope or an object with a warp takes part as the art it draws.
|
||||
if let Some(art) = shape_art_in(n, hook, nodes) {
|
||||
if let Some(art) = shape_art_in(doc, n, hook, nodes) {
|
||||
let first = out.len();
|
||||
leaves(&art, hook, nodes, out);
|
||||
// Every path of a live object's, type's or symbol instance's art is drawn inside the
|
||||
// object's opacity and blend mode.
|
||||
if crate::needs_outline(n) {
|
||||
leaves(doc, &art, hook, nodes, out);
|
||||
// Every path of a live object's, type's or symbol instance's art, and every piece of a path
|
||||
// whose live-effect plug-in paints its pieces, is drawn inside the object's opacity and
|
||||
// blend mode.
|
||||
let pieces = matches!(n.kind, NodeKind::Path { .. } | NodeKind::Compound { .. }) && matches!(art.kind, NodeKind::Group { .. });
|
||||
if crate::needs_outline(n) || pieces {
|
||||
for l in out.iter_mut().skip(first) {
|
||||
within(l, n);
|
||||
}
|
||||
@@ -95,11 +98,11 @@ fn leaves(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes, out: &mut Ve
|
||||
match &n.kind {
|
||||
NodeKind::Path { guide: false, .. } | NodeKind::Compound { .. } => out.push(n.clone()),
|
||||
// A compound shape takes part as its area, styled like it.
|
||||
NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } => match compound_shape_art_in(n, hook, nodes) {
|
||||
NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } => match compound_shape_art_in(doc, n, hook, nodes) {
|
||||
Some(art) => out.push(art),
|
||||
None => {
|
||||
for c in children {
|
||||
leaves(c, hook, nodes, out);
|
||||
leaves(doc, c, hook, nodes, out);
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -114,7 +117,7 @@ fn leaves(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes, out: &mut Ve
|
||||
let art = match &n.kind {
|
||||
NodeKind::SymbolInstance { symbol, .. } if nodes.charge(&art) => {
|
||||
let mut art = crate::reshape::symbol_art_with(&art, h, nodes, symbol);
|
||||
crate::reshape::outline_nested_text(&mut art, false);
|
||||
crate::reshape::outline_nested_text(doc, &mut art, false);
|
||||
transparency_into_members(&mut art);
|
||||
art
|
||||
}
|
||||
@@ -122,7 +125,7 @@ fn leaves(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes, out: &mut Ve
|
||||
_ => art,
|
||||
};
|
||||
let first = out.len();
|
||||
leaves(&art, None, nodes, out);
|
||||
leaves(doc, &art, None, nodes, out);
|
||||
for l in out.iter_mut().skip(first) {
|
||||
within(l, n);
|
||||
}
|
||||
@@ -176,29 +179,37 @@ fn transparency_into_members(n: &mut Node) {
|
||||
/// [`outline_object`](crate::outline_object) gives a symbol instance its symbol's art. The art
|
||||
/// keeps the effects of `n` other than its geometry effects, and the groups inside it pass their
|
||||
/// opacity and blend mode to the objects they hold. `None` when `n` is none of these, for images,
|
||||
/// for symbol instances without geometry effects, and for one without art from `hook`.
|
||||
pub fn shape_art(n: &Node, hook: Option<OutlineHook>) -> Option<Node> {
|
||||
shape_art_in(n, hook, &SymbolNodes::default())
|
||||
/// for symbol instances without geometry effects, and for one without art from `hook`. A
|
||||
/// live-effect plug-in that paints its pieces makes a path, and each path of a group's or symbol's
|
||||
/// art, a group of its pieces, whose swatches and patterns must be in `doc` (without one, pieces
|
||||
/// that name one are refused and the path stays as it is).
|
||||
pub fn shape_art(doc: Option<&Document>, n: &Node, hook: Option<OutlineHook>) -> Option<Node> {
|
||||
shape_art_in(doc, n, hook, &SymbolNodes::default())
|
||||
}
|
||||
|
||||
/// [`shape_art`], with the symbol art given so far counted in `nodes`.
|
||||
fn shape_art_in(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Option<Node> {
|
||||
let mut art = reshaped_art(n, hook, nodes)?;
|
||||
fn shape_art_in(doc: Option<&Document>, n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Option<Node> {
|
||||
let mut art = reshaped_art(doc, n, hook, nodes)?;
|
||||
transparency_into_members(&mut art);
|
||||
Some(art)
|
||||
}
|
||||
|
||||
/// [`shape_art_in`] before the groups inside the art pass on their transparency.
|
||||
fn reshaped_art(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Option<Node> {
|
||||
fn reshaped_art(doc: Option<&Document>, n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Option<Node> {
|
||||
let fx = crate::reshape::geometry_effects(n);
|
||||
let outline = hook.unwrap_or(&crate::outline_text);
|
||||
match &n.kind {
|
||||
NodeKind::Path { guide: false, .. } | NodeKind::Compound { .. } if !fx.is_empty() => Some(reshaped(n, n)),
|
||||
NodeKind::Group { .. } | NodeKind::Layer { .. } if !fx.is_empty() => match compound_shape_art_in(n, hook, nodes) {
|
||||
Some(area) => Some(reshaped(&area, n)),
|
||||
// A path with a live-effect plug-in that paints its pieces: the group of its pieces, each
|
||||
// with its own paints, as the renderer draws it.
|
||||
NodeKind::Path { guide: false, .. } | NodeKind::Compound { .. } if !fx.is_empty() => {
|
||||
let painted = crate::reshape::has_painted_effects(n).then(|| crate::reshape::reshape(doc, n, None)).flatten();
|
||||
Some(painted.unwrap_or_else(|| reshaped(n, n, doc)))
|
||||
}
|
||||
NodeKind::Group { .. } | NodeKind::Layer { .. } if !fx.is_empty() => match compound_shape_art_in(doc, n, hook, nodes) {
|
||||
Some(area) => Some(reshaped(&area, n, doc)),
|
||||
None => {
|
||||
let mut m = crate::reshape::outlined_members_with(None, n, outline, nodes);
|
||||
let carry = crate::reshape::reshape_leaves_carrying(&mut m, &fx);
|
||||
let mut m = crate::reshape::outlined_members_with(None, doc, n, outline, nodes);
|
||||
let carry = crate::reshape::reshape_leaves_carrying(&mut m, &fx, &n.appearance.effects, doc);
|
||||
// The group's own fills and strokes follow the effects as its members do.
|
||||
if carry.moves(&m.appearance) {
|
||||
carry.apply(&mut m.appearance);
|
||||
@@ -213,8 +224,8 @@ fn reshaped_art(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Opt
|
||||
let h = hook?;
|
||||
let art = h(n).filter(|art| nodes.charge(art))?;
|
||||
let mut art = crate::reshape::symbol_art_with(&art, h, nodes, symbol);
|
||||
crate::reshape::outline_nested_text(&mut art, false);
|
||||
let carry = crate::reshape::reshape_leaves_carrying(&mut art, &fx);
|
||||
crate::reshape::outline_nested_text(doc, &mut art, false);
|
||||
let carry = crate::reshape::reshape_leaves_carrying(&mut art, &fx, &n.appearance.effects, doc);
|
||||
// The art's own fills and strokes follow the effects as its members do.
|
||||
if carry.moves(&art.appearance) {
|
||||
carry.apply(&mut art.appearance);
|
||||
@@ -226,19 +237,21 @@ fn reshaped_art(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Opt
|
||||
}
|
||||
NodeKind::Image(_) | NodeKind::SymbolInstance { .. } => None,
|
||||
_ if crate::needs_outline(n) && (vectorcraft_doc::live::is_live(n) || !fx.is_empty()) => {
|
||||
crate::reshape::as_art_with(None, n, None, outline, nodes)
|
||||
crate::reshape::as_art_with(None, doc, n, None, outline, nodes)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// `src`, a path, compound path or compound shape's area, as one path reshaped by the visible
|
||||
/// geometry effects of `n`, without those effects.
|
||||
fn reshaped(src: &Node, n: &Node) -> Node {
|
||||
/// geometry effects of `n`, without those effects. The swatches and patterns a live-effect plug-in
|
||||
/// that paints its pieces names are looked up in `doc`.
|
||||
fn reshaped(src: &Node, n: &Node, doc: Option<&Document>) -> Node {
|
||||
let mut m = src.clone();
|
||||
if let Some((path, rule)) = geometry(src) {
|
||||
let bounds = vectorcraft_geom::Shape::bounding_box(&path.to_bezpath());
|
||||
let (g, carry) = crate::apply_geometry_carrying(&n.appearance.effects, &path, bounds, &crate::GeomContext::of(n));
|
||||
let (g, carry) =
|
||||
crate::apply_geometry_carrying(&n.appearance.effects, &path, bounds, &crate::GeomContext { doc, ..crate::GeomContext::of(n) });
|
||||
m.kind = NodeKind::Path { path: g, rule, live: None, clipping: false, guide: false };
|
||||
// Its gradients move with the outline, as the canvas paints them.
|
||||
if carry.moves(&m.appearance) {
|
||||
@@ -282,12 +295,12 @@ fn shape_op(mode: ShapeMode) -> po::BoolOp {
|
||||
/// it by the member's shape mode; the backmost member starts the area. `None` when `n` is not a
|
||||
/// compound shape, or its members are past a live Pathfinder's budget ([`PATHFINDER_MAX_SEGMENTS`],
|
||||
/// [`PATHFINDER_MAX_OVERLAPS`], [`SYMBOL_ART_MAX_NODES`](crate::SYMBOL_ART_MAX_NODES)).
|
||||
pub fn compound_shape_area(n: &Node, hook: Option<OutlineHook>) -> Option<PathData> {
|
||||
compound_shape_area_in(n, hook, &SymbolNodes::default())
|
||||
pub fn compound_shape_area(doc: Option<&Document>, n: &Node, hook: Option<OutlineHook>) -> Option<PathData> {
|
||||
compound_shape_area_in(doc, n, hook, &SymbolNodes::default())
|
||||
}
|
||||
|
||||
/// [`compound_shape_area`], with the symbol art given so far counted in `nodes`.
|
||||
fn compound_shape_area_in(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Option<PathData> {
|
||||
fn compound_shape_area_in(doc: Option<&Document>, n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Option<PathData> {
|
||||
if !n.is_compound_shape() {
|
||||
return None;
|
||||
}
|
||||
@@ -296,7 +309,7 @@ fn compound_shape_area_in(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNod
|
||||
let mut members: Vec<(po::BoolOp, usize)> = vec![];
|
||||
for c in n.children()? {
|
||||
let before = all.len();
|
||||
leaves(c, hook, nodes, &mut all);
|
||||
leaves(doc, c, hook, nodes, &mut all);
|
||||
if all.len() > before {
|
||||
members.push((shape_op(c.extras().shape_mode), all.len() - before));
|
||||
}
|
||||
@@ -323,13 +336,13 @@ fn compound_shape_area_in(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNod
|
||||
|
||||
/// Compound shape `n` as one path of its area ([`compound_shape_area`]) with its appearance,
|
||||
/// opacity and blend mode: how it takes part in a Pathfinder operation.
|
||||
pub fn compound_shape_art(n: &Node, hook: Option<OutlineHook>) -> Option<Node> {
|
||||
compound_shape_art_in(n, hook, &SymbolNodes::default())
|
||||
pub fn compound_shape_art(doc: Option<&Document>, n: &Node, hook: Option<OutlineHook>) -> Option<Node> {
|
||||
compound_shape_art_in(doc, n, hook, &SymbolNodes::default())
|
||||
}
|
||||
|
||||
/// [`compound_shape_art`], with the symbol art given so far counted in `nodes`.
|
||||
fn compound_shape_art_in(n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Option<Node> {
|
||||
let area = compound_shape_area_in(n, hook, nodes)?;
|
||||
fn compound_shape_art_in(doc: Option<&Document>, n: &Node, hook: Option<OutlineHook>, nodes: &SymbolNodes) -> Option<Node> {
|
||||
let area = compound_shape_area_in(doc, n, hook, nodes)?;
|
||||
let mut art = Node::path(n.id, area, Appearance { items: n.appearance.items.clone(), ..Default::default() });
|
||||
(art.opacity, art.blend) = (n.opacity, n.blend);
|
||||
Some(art)
|
||||
@@ -435,15 +448,16 @@ fn within_budget<'a>(leaves: impl IntoIterator<Item = &'a Node>) -> bool {
|
||||
/// Evaluate the visible Pathfinder effects of `group` in order: the resulting child objects.
|
||||
/// `None` when the group has no Pathfinder effect. Past the budget ([`PATHFINDER_MAX_SEGMENTS`],
|
||||
/// [`PATHFINDER_MAX_OVERLAPS`], [`SYMBOL_ART_MAX_NODES`](crate::SYMBOL_ART_MAX_NODES) for
|
||||
/// each effect) it returns the children as they are.
|
||||
pub fn pathfinder_children(group: &Node, hook: Option<OutlineHook>) -> Option<Vec<Arc<Node>>> {
|
||||
/// each effect) it returns the children as they are. The swatches and patterns that live-effect
|
||||
/// plug-ins painting their pieces name are looked up in `doc` ([`shape_art`]).
|
||||
pub fn pathfinder_children(doc: Option<&Document>, group: &Node, hook: Option<OutlineHook>) -> Option<Vec<Arc<Node>>> {
|
||||
if !has_pathfinder(group) {
|
||||
return None;
|
||||
}
|
||||
let source: &[Arc<Node>] = group.children()?;
|
||||
let mut stack: Vec<Arc<Node>> = if group.is_compound_shape() {
|
||||
// The group's own appearance paints the area ([`evaluate_container`]), so the art has none.
|
||||
match compound_shape_area(group, hook) {
|
||||
match compound_shape_area(doc, group, hook) {
|
||||
Some(area) if area.is_empty() => vec![],
|
||||
Some(area) => vec![Arc::new(Node::path(group.id, area, Appearance::default()))],
|
||||
None => return Some(source.to_vec()),
|
||||
@@ -455,7 +469,7 @@ pub fn pathfinder_children(group: &Node, hook: Option<OutlineHook>) -> Option<Ve
|
||||
if e.id == crate::trap::TRAP {
|
||||
let (mut lv, nodes) = (vec![], SymbolNodes::default());
|
||||
for c in &stack {
|
||||
leaves(c, hook, &nodes, &mut lv);
|
||||
leaves(doc, c, hook, &nodes, &mut lv);
|
||||
}
|
||||
if nodes.spent() || !within_budget(&lv) {
|
||||
return Some(source.to_vec());
|
||||
@@ -479,7 +493,7 @@ pub fn pathfinder_children(group: &Node, hook: Option<OutlineHook>) -> Option<Ve
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let mut lv = vec![];
|
||||
leaves(c, hook, &nodes, &mut lv);
|
||||
leaves(doc, c, hook, &nodes, &mut lv);
|
||||
lv
|
||||
})
|
||||
.collect();
|
||||
@@ -633,7 +647,7 @@ pub fn member_shapes(doc: Option<&Document>, n: &Node) -> Vec<(PathData, FillRul
|
||||
NodeKind::Path { guide: false, .. } | NodeKind::Compound { .. } => out.extend(crate::reshape::leaf_geometry(n)),
|
||||
NodeKind::Group { .. } if n.is_compound_shape() => {
|
||||
let hook = |c: &Node| crate::outline_object(doc, c);
|
||||
match compound_shape_area(n, Some(&hook)) {
|
||||
match compound_shape_area(doc, n, Some(&hook)) {
|
||||
Some(area) => out.push((area, FillRule::NonZero)),
|
||||
None => members(doc, n, out),
|
||||
}
|
||||
@@ -715,7 +729,7 @@ pub fn evaluate_container(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
let mut m = n.clone();
|
||||
if pathfinder {
|
||||
let hook = |c: &Node| crate::outline_object(doc, c);
|
||||
let result = pathfinder_children(n, Some(&hook)).unwrap_or_default();
|
||||
let result = pathfinder_children(doc, n, Some(&hook)).unwrap_or_default();
|
||||
if let Some(ch) = m.children_mut() {
|
||||
*ch = result;
|
||||
}
|
||||
@@ -724,7 +738,7 @@ pub fn evaluate_container(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
}
|
||||
if !fx.is_empty() {
|
||||
m = crate::reshape::outlined_members(doc, &m);
|
||||
let carry = crate::reshape::reshape_leaves_carrying(&mut m, &fx);
|
||||
let carry = crate::reshape::reshape_leaves_carrying(&mut m, &fx, &n.appearance.effects, doc);
|
||||
// The group's own fills and strokes paint the members as a whole: their gradients follow
|
||||
// the effects as the members' do.
|
||||
if paint && carry.moves(&m.appearance) {
|
||||
|
||||
@@ -27,7 +27,7 @@ fn area(n: &Node) -> f64 {
|
||||
}
|
||||
|
||||
fn overlap_cmyk(g: &Node) -> [f32; 4] {
|
||||
let faces = pathfinder_children(g, None).unwrap();
|
||||
let faces = pathfinder_children(None, g, None).unwrap();
|
||||
assert_eq!(faces.len(), 3);
|
||||
let overlap = faces
|
||||
.iter()
|
||||
@@ -61,7 +61,7 @@ fn trap_effect_adds_an_overprinting_strip_above_the_members() {
|
||||
let mut g = Node::group(NodeId(10), vec![sq(1, 0.0, yellow), sq(2, 10.0, blue)]);
|
||||
let params = serde_json::json!({"thickness": 1.0, "tintReduction": 100});
|
||||
g.appearance.effects.push(Effect { id: crate::trap::TRAP.into(), params, visible: true });
|
||||
let out = pathfinder_children(&g, None).unwrap();
|
||||
let out = pathfinder_children(None, &g, None).unwrap();
|
||||
assert_eq!(out.len(), 3, "both squares and one trap");
|
||||
assert_eq!((out[0].id, out[1].id), (NodeId(1), NodeId(2)));
|
||||
let b = out[2].geometric_bounds().unwrap();
|
||||
@@ -89,17 +89,17 @@ fn soft_mix_blends_cmyk_bottom_to_top_and_drops_strokes() {
|
||||
|
||||
#[test]
|
||||
fn shape_modes_follow_the_pathfinder_panel() {
|
||||
let add = pathfinder_children(&group_with("pathfinder.add"), None).unwrap();
|
||||
let add = pathfinder_children(None, &group_with("pathfinder.add"), None).unwrap();
|
||||
assert_eq!(add.len(), 1);
|
||||
assert!((area(&add[0]) - 150.0).abs() < 1e-3);
|
||||
// Add keeps the front object's paint, Subtract the back one's.
|
||||
assert_eq!(add[0].id, NodeId(2));
|
||||
let sub = pathfinder_children(&group_with("pathfinder.subtract"), None).unwrap();
|
||||
let sub = pathfinder_children(None, &group_with("pathfinder.subtract"), None).unwrap();
|
||||
assert_eq!(sub[0].id, NodeId(1));
|
||||
assert!((area(&sub[0]) - 50.0).abs() < 1e-3);
|
||||
let int = pathfinder_children(&group_with("pathfinder.intersect"), None).unwrap();
|
||||
let int = pathfinder_children(None, &group_with("pathfinder.intersect"), None).unwrap();
|
||||
assert!((area(&int[0]) - 50.0).abs() < 1e-3);
|
||||
let div = pathfinder_children(&group_with("pathfinder.divide"), None).unwrap();
|
||||
let div = pathfinder_children(None, &group_with("pathfinder.divide"), None).unwrap();
|
||||
assert_eq!(div.len(), 3);
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ fn divide_and_outline_draw_nothing_for_unpainted_members() {
|
||||
for effect in ["pathfinder.divide", "pathfinder.outline"] {
|
||||
let mut g = Node::group(NodeId(10), vec![sq(1, 0.0, Color::rgb(1.0, 0.0, 0.0)), unpainted.clone()]);
|
||||
g.appearance.effects.push(Effect { id: effect.into(), params: serde_json::json!({}), visible: true });
|
||||
let out = pathfinder_children(&g, None).unwrap();
|
||||
let out = pathfinder_children(None, &g, None).unwrap();
|
||||
assert!(out.iter().any(|n| n.id == NodeId(1)) && out.iter().any(|n| n.id == NodeId(2)), "{effect}: {out:#?}");
|
||||
for n in out.iter().filter(|n| n.id == NodeId(2)) {
|
||||
assert!(n.appearance.items.iter().all(|i| i.paint().is_none()), "{effect}: {n:#?}");
|
||||
@@ -138,7 +138,7 @@ fn square_and(front: Option<Node>, effect: &str) -> Node {
|
||||
|
||||
/// Returns the Pathfinder results of `g`, with type outlined as the renderer outlines it.
|
||||
fn results(g: &Node) -> Vec<Arc<Node>> {
|
||||
pathfinder_children(g, crate::text_outliner()).unwrap()
|
||||
pathfinder_children(None, g, crate::text_outliner()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -167,7 +167,7 @@ fn type_changes_the_result_of_every_pathfinder_effect() {
|
||||
assert!((with_type("pathfinder.intersect") + with_type("pathfinder.minusBack") - glyph_area).abs() < 0.5, "{glyph_area}");
|
||||
// Outline art nested one group deeper takes part as well.
|
||||
let nested: OutlineHook = &|n: &Node| crate::outline_text(n).map(|g| Node::group(n.id, vec![Arc::new(g)]));
|
||||
let sub = pathfinder_children(&square_and(Some(type_h()), "pathfinder.subtract"), Some(nested)).unwrap();
|
||||
let sub = pathfinder_children(None, &square_and(Some(type_h()), "pathfinder.subtract"), Some(nested)).unwrap();
|
||||
assert!((sub.iter().map(|n| area(n)).sum::<f64>() - with_type("pathfinder.subtract")).abs() < 1e-6);
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ fn type_in_a_group_keeps_its_look_when_the_groups_geometry_effects_outline_it()
|
||||
fn hidden_effect_or_plain_group_is_untouched() {
|
||||
let mut g = group_with("pathfinder.add");
|
||||
g.appearance.effects[0].visible = false;
|
||||
assert!(pathfinder_children(&g, None).is_none());
|
||||
assert!(pathfinder_children(None, &g, None).is_none());
|
||||
assert!(!has_pathfinder(&sq(1, 0.0, Color::BLACK)));
|
||||
}
|
||||
|
||||
@@ -320,12 +320,12 @@ fn shape_art_moves_gradients_with_a_transform_effect() {
|
||||
};
|
||||
let mut path = Node::path(NodeId(1), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::basic(placed.clone(), Paint::None, 0.0));
|
||||
path.appearance.effects.push(moved());
|
||||
let got = starts(&shape_art(&path, None).unwrap());
|
||||
let got = starts(&shape_art(None, &path, None).unwrap());
|
||||
assert!(got.len() == 1 && (got[0] - 40.0).abs() < 1e-6, "the path's gradient: {got:?}");
|
||||
let mut g = Node::group(NodeId(10), vec![sq(2, 0.0, Color::BLACK)]);
|
||||
g.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(placed)));
|
||||
g.appearance.effects.push(moved());
|
||||
let got = starts(&shape_art(&g, None).unwrap());
|
||||
let got = starts(&shape_art(None, &g, None).unwrap());
|
||||
assert!(!got.is_empty() && got.iter().all(|s| (s - 40.0).abs() < 1e-6), "the group's own fill: {got:?}");
|
||||
}
|
||||
|
||||
@@ -343,7 +343,7 @@ fn shape_art_moves_the_gradient_of_a_symbol_instances_art_with_its_transform() {
|
||||
let hook = move |_: &Node| Some(art.clone());
|
||||
let mut instance = Node::new(NodeId(1), NodeKind::SymbolInstance { symbol: "s".into(), xf: vectorcraft_geom::Affine::IDENTITY });
|
||||
instance.appearance.effects.push(Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 30}), visible: true });
|
||||
let out = shape_art(&instance, Some(&hook)).unwrap();
|
||||
let out = shape_art(None, &instance, Some(&hook)).unwrap();
|
||||
let Paint::Gradient(g) = out.appearance.fill_paint() else { panic!("a gradient: {:?}", out.appearance) };
|
||||
assert!((g.geom.unwrap().start.x - 30.0).abs() < 1e-6, "{:?}", g.geom);
|
||||
assert!((out.geometric_bounds().unwrap().x0 - 30.0).abs() < 1e-6, "the art moves");
|
||||
@@ -377,11 +377,11 @@ fn shape_art_moves_the_gradients_of_a_symbol_instances_art_and_a_compound_shape_
|
||||
let hook = move |_: &Node| Some(art.clone());
|
||||
let mut instance = Node::new(NodeId(1), NodeKind::SymbolInstance { symbol: "s".into(), xf: vectorcraft_geom::Affine::IDENTITY });
|
||||
instance.appearance.effects.push(warp());
|
||||
check(&shape_art(&instance, Some(&hook)).unwrap(), Rect::new(0.0, 0.0, 10.0, 10.0), &format!("warp.{style} on a symbol instance"));
|
||||
check(&shape_art(None, &instance, Some(&hook)).unwrap(), Rect::new(0.0, 0.0, 10.0, 10.0), &format!("warp.{style} on a symbol instance"));
|
||||
let mut shape = compound_shape(ShapeMode::Add);
|
||||
shape.appearance = Appearance::basic(placed.clone(), Paint::None, 0.0).into();
|
||||
shape.appearance.effects.push(warp());
|
||||
check(&shape_art(&shape, None).unwrap(), Rect::new(0.0, 0.0, 15.0, 10.0), &format!("warp.{style} on a compound shape"));
|
||||
check(&shape_art(None, &shape, None).unwrap(), Rect::new(0.0, 0.0, 15.0, 10.0), &format!("warp.{style} on a compound shape"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -594,7 +594,7 @@ fn a_groups_transform_moves_the_gradients_placed_on_an_images_own_fills() {
|
||||
};
|
||||
for kind in [image as fn() -> NodeKind, instance] {
|
||||
assert_eq!(starts(kind(), serde_json::json!({"moveH": 30})), [30.0], "{:?}", kind());
|
||||
assert_eq!(starts(kind(), serde_json::json!({"moveH": 30, "copies": 2})), [0.0, 30.0, 60.0], "{:?}", kind());
|
||||
assert_eq!(starts(kind(), serde_json::json!({"moveH": 30, "copies": 2})), [60.0, 30.0, 0.0], "{:?}", kind());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -677,7 +677,8 @@ fn a_transform_on_a_group_paints_each_copy_with_its_own_gradient() {
|
||||
let art = evaluate_container(None, &g).unwrap();
|
||||
let copies = art.children().unwrap()[0].children().expect("the object and its copies are objects of their own");
|
||||
assert_eq!(copies.len(), 3);
|
||||
for (i, c) in copies.iter().enumerate() {
|
||||
// The original is on top, after the copies below it.
|
||||
for (i, c) in copies.iter().rev().enumerate() {
|
||||
// Each copy's own box: an unplaced gradient fits it, a placed one moved with it.
|
||||
assert!((c.geometric_bounds().unwrap().x0 - 30.0 * i as f64).abs() < 1e-6);
|
||||
match c.appearance.fill_paint() {
|
||||
@@ -714,7 +715,7 @@ fn pathfinder_past_its_budget_draws_the_source_art() {
|
||||
// n² crossings past the cap: no evaluation (#3591), the source art as it is.
|
||||
for n in [130, 800] {
|
||||
let g = crossing_bars(n, "pathfinder.divide");
|
||||
let out = pathfinder_children(&g, None).unwrap();
|
||||
let out = pathfinder_children(None, &g, None).unwrap();
|
||||
assert_eq!(out.len(), 2 * n, "{n} x {n} bars");
|
||||
let ids: Vec<NodeId> = out.iter().map(|c| c.id).collect();
|
||||
let src: Vec<NodeId> = g.children().unwrap().iter().map(|c| c.id).collect();
|
||||
@@ -729,7 +730,7 @@ fn pathfinder_within_its_budget_still_divides() {
|
||||
// n x n bars divide into 3·n² pieces, and 128 x 128 bars, which overlap in exactly
|
||||
// `PATHFINDER_MAX_OVERLAPS` pairs, must still divide.
|
||||
for n in [8, 128] {
|
||||
let out = pathfinder_children(&crossing_bars(n, "pathfinder.divide"), None).unwrap();
|
||||
let out = pathfinder_children(None, &crossing_bars(n, "pathfinder.divide"), None).unwrap();
|
||||
assert_eq!(out.len(), 3 * n * n, "{n} x {n} bars");
|
||||
}
|
||||
}
|
||||
@@ -747,7 +748,7 @@ fn pathfinder_on_many_stacked_bars_still_runs() {
|
||||
.collect();
|
||||
let mut g = Node::group(NodeId(10), children);
|
||||
g.appearance.effects.push(Effect { id: "pathfinder.add".into(), params: serde_json::json!({}), visible: true });
|
||||
let out = pathfinder_children(&g, None).unwrap();
|
||||
let out = pathfinder_children(None, &g, None).unwrap();
|
||||
assert_eq!(out.len(), 1);
|
||||
}
|
||||
|
||||
@@ -768,9 +769,9 @@ fn compound_shapes_draw_the_area_their_shape_modes_make() {
|
||||
for (mode, want) in [(ShapeMode::Add, 150.0), (ShapeMode::Subtract, 50.0), (ShapeMode::Intersect, 50.0), (ShapeMode::Exclude, 100.0)] {
|
||||
let g = compound_shape(mode);
|
||||
assert!(has_pathfinder(&g), "{mode:?}");
|
||||
let area_path = compound_shape_area(&g, None).unwrap();
|
||||
let area_path = compound_shape_area(None, &g, None).unwrap();
|
||||
assert!((po::area(&area_path, FillRule::NonZero).abs() - want).abs() < 0.01, "{mode:?}");
|
||||
let out = pathfinder_children(&g, None).unwrap();
|
||||
let out = pathfinder_children(None, &g, None).unwrap();
|
||||
assert_eq!(out.len(), 1, "{mode:?}");
|
||||
assert!(out[0].appearance.items.is_empty(), "the group's own appearance paints it");
|
||||
let art = evaluate_container(None, &g).unwrap();
|
||||
@@ -787,7 +788,7 @@ fn compound_shapes_draw_the_area_their_shape_modes_make() {
|
||||
back.extras_mut().shape_mode = ShapeMode::Subtract;
|
||||
ch[0] = Arc::new(back);
|
||||
}
|
||||
assert!((po::area(&compound_shape_area(&g, None).unwrap(), FillRule::NonZero).abs() - 150.0).abs() < 0.01);
|
||||
assert!((po::area(&compound_shape_area(None, &g, None).unwrap(), FillRule::NonZero).abs() - 150.0).abs() < 0.01);
|
||||
// Hidden members take no part; the mark on a clip group is ignored.
|
||||
let mut g = compound_shape(ShapeMode::Subtract);
|
||||
if let Some(ch) = g.children_mut() {
|
||||
@@ -795,10 +796,10 @@ fn compound_shapes_draw_the_area_their_shape_modes_make() {
|
||||
front.visible = false;
|
||||
ch[1] = Arc::new(front);
|
||||
}
|
||||
assert!((po::area(&compound_shape_area(&g, None).unwrap(), FillRule::NonZero).abs() - 100.0).abs() < 0.01);
|
||||
assert!((po::area(&compound_shape_area(None, &g, None).unwrap(), FillRule::NonZero).abs() - 100.0).abs() < 0.01);
|
||||
let mut clip = compound_shape(ShapeMode::Add);
|
||||
clip.kind = NodeKind::Group { children: clip.children().unwrap().clone(), clip: true };
|
||||
assert!(!clip.is_compound_shape() && compound_shape_area(&clip, None).is_none());
|
||||
assert!(!clip.is_compound_shape() && compound_shape_area(None, &clip, None).is_none());
|
||||
}
|
||||
|
||||
/// A compound shape inside another (and inside an Effect › Pathfinder group) takes part as its area,
|
||||
@@ -811,10 +812,10 @@ fn nested_compound_shapes_take_part_as_their_area() {
|
||||
let mut outer = Node::group(NodeId(20), vec![Arc::new(inner.clone()), Arc::new(cutter)]);
|
||||
outer.extras_mut().compound_shape = true;
|
||||
// (0–5 of the inner area) ∩ (2–12) = 2–5 by 10 high.
|
||||
assert!((po::area(&compound_shape_area(&outer, None).unwrap(), FillRule::NonZero).abs() - 30.0).abs() < 0.01);
|
||||
assert!((po::area(&compound_shape_area(None, &outer, None).unwrap(), FillRule::NonZero).abs() - 30.0).abs() < 0.01);
|
||||
let mut effect = Node::group(NodeId(30), vec![Arc::new(inner)]);
|
||||
effect.appearance.effects.push(Effect { id: "pathfinder.add".into(), params: serde_json::json!({}), visible: true });
|
||||
let out = pathfinder_children(&effect, None).unwrap();
|
||||
let out = pathfinder_children(None, &effect, None).unwrap();
|
||||
assert_eq!(out.len(), 1);
|
||||
assert!((area(&out[0]) - 50.0).abs() < 0.01);
|
||||
assert_eq!(out[0].appearance.fill_paint().color(), Some(Color::rgb(0.0, 1.0, 0.0)), "styled like the compound shape");
|
||||
|
||||
@@ -80,14 +80,15 @@ pub use marks::{CROP_MARKS, crop_marks_art, has_crop_marks};
|
||||
pub use pixel::{PIXEL_EFFECTS, PixelFx, PixelSpace};
|
||||
pub use raster::{RASTERIZE, RasterFx, Rasterize, color_param, outset, raster_effects};
|
||||
pub use reshape::{
|
||||
SYMBOL_ART_MAX_NODES, expand_outlined, gives_image_shape, has_gradient_copies, has_item_geometry, has_transform_art, has_transform_copies,
|
||||
has_transform_options, image_paint, needs_outline, outline_art, outline_text, reshape,
|
||||
SYMBOL_ART_MAX_NODES, expand_outlined, gives_image_shape, has_gradient_copies, has_item_geometry, has_painted_effects, has_transform_art,
|
||||
has_transform_copies, has_transform_options, image_paint, needs_outline, outline_art, outline_text, reshape,
|
||||
};
|
||||
pub use revolve::{REVOLVE, has_revolve, revolve_art, revolve_options, validate_revolve};
|
||||
pub use revolve::{REVOLVE, has_revolve, revolve_art, revolve_art_in, revolve_options, validate_revolve, validate_revolve_in};
|
||||
pub use solids::{
|
||||
CLASSIC_EXTRUDE, EXTRUDE, INFLATE, ROTATE, ROTATION_PRESETS, SHADING_COLORS, SOLIDS, SolidMesh, THREE_D_TYPES, classic_extrude_options,
|
||||
classic_shading, extrude_options, has_solid, inflate_options, is_three_d, solid_art, three_d_meshes, three_d_meshes_of, three_d_params_as,
|
||||
validate_solid, validate_three_d, validate_three_d_params, view_options,
|
||||
classic_shading, extrude_options, has_solid, inflate_options, is_three_d, solid_art, solid_art_in, three_d_meshes, three_d_meshes_in,
|
||||
three_d_meshes_of, three_d_meshes_of_in, three_d_params_as, validate_solid, validate_solid_in, validate_three_d, validate_three_d_at,
|
||||
validate_three_d_at_in, validate_three_d_in, validate_three_d_params, view_options,
|
||||
};
|
||||
pub use stylize::{ScribbleSetting, scribble_setting_id, scribble_settings};
|
||||
pub use svg_filter::SVG_FILTER;
|
||||
@@ -107,5 +108,6 @@ pub use geometry::{
|
||||
#[cfg(test)]
|
||||
pub(crate) use geometry::{MAX_EFFECT_LINES, apply_geometry_within};
|
||||
pub use params::{
|
||||
default_params, has_geometry, has_plugin_effect, is_geometry, is_raster, merged_params, new_effect, plugin_revision, scale_effect, upgrade_params,
|
||||
any_plugin_paints_output, default_params, has_geometry, has_painted_plugin_effect, has_plugin_effect, is_geometry, is_raster, merged_params,
|
||||
new_effect, plugin_revision, scale_effect, upgrade_params,
|
||||
};
|
||||
@@ -43,7 +43,8 @@ fn with_hooks<R>(doc: Option<&Document>, f: impl FnOnce(Hooks) -> R) -> R {
|
||||
vectorcraft_text::register_envelope_ink();
|
||||
let outline = |n: &Node| outline_object(doc, n);
|
||||
let tiles = |name: &str, xf: Affine, region: Rect| Some(doc?.pattern(name)?.instances_in(xf, region));
|
||||
f(Hooks { outline: Some(&outline), appearance: Some(&bake_appearance), pattern: Some(&tiles), symbols: doc })
|
||||
let appearance = |n: &Node| bake_appearance(doc, n);
|
||||
f(Hooks { outline: Some(&outline), appearance: Some(&appearance), pattern: Some(&tiles), symbols: doc })
|
||||
}
|
||||
|
||||
/// One level of evaluation of live object `n` ([`live::expand_live_hooks`]).
|
||||
|
||||
@@ -148,7 +148,21 @@ pub fn has_plugin_effect(n: &Node) -> bool {
|
||||
n.appearance.effects.iter().any(plugin) || n.appearance.items.iter().any(|i| i.effects().iter().any(plugin))
|
||||
}
|
||||
|
||||
/// The plug-in registry's revision, which changes whenever a plug-in is installed or removed.
|
||||
/// Does `n` have a visible live-effect plug-in effect, its own or a fill's or stroke's, whose
|
||||
/// plug-in paints the pieces it returns? What such an effect draws also depends on the document's
|
||||
/// swatches and patterns, which its pieces may name.
|
||||
pub fn has_painted_plugin_effect(n: &Node) -> bool {
|
||||
let painted = |e: &Effect| e.visible && vectorcraft_plugins::effect::plugin_id(&e.id).is_some_and(vectorcraft_plugins::effect::paints_output);
|
||||
n.appearance.effects.iter().any(painted) || n.appearance.items.iter().any(|i| i.effects().iter().any(painted))
|
||||
}
|
||||
|
||||
/// Is a live-effect plug-in that paints the pieces it returns installed?
|
||||
pub fn any_plugin_paints_output() -> bool {
|
||||
vectorcraft_plugins::registry::list().iter().any(|p| vectorcraft_plugins::effect::paints_output(&p.manifest().id))
|
||||
}
|
||||
|
||||
/// Changes whenever a plug-in is installed or removed (the registry's revision), and when deferred
|
||||
/// live-effect plug-in stages give results ([`vectorcraft_plugins::deferred::revision`]).
|
||||
pub fn plugin_revision() -> u64 {
|
||||
vectorcraft_plugins::registry::revision()
|
||||
vectorcraft_plugins::registry::revision() ^ vectorcraft_plugins::deferred::revision().rotate_left(32)
|
||||
}
|
||||
@@ -17,6 +17,8 @@
|
||||
//! [`Selection::anchor_bounds`] measure the objects themselves. Raster effects (shadows, glows,
|
||||
//! blurs) are not measured. In one object, the art of at most 4,096 symbol instances is measured
|
||||
//! with its geometry effects, and the other instances in it are measured without them.
|
||||
//! [`bounds_of_with`], [`oriented_bounds_with`] and [`anchor_bounds_with`] measure as these do and,
|
||||
//! with [`Measure::glyphs`], take point type by the ink of its glyphs (Measure Type by Glyph Bounds).
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::collections::HashMap;
|
||||
@@ -74,6 +76,121 @@ pub fn anchor_bounds(selection: &Selection, doc: &Document, preview: bool) -> Op
|
||||
union_opt(bounds_of(doc, &selection.whole(), preview), selection.anchors_box(doc))
|
||||
}
|
||||
|
||||
/// How [`bounds_of_with`], [`oriented_bounds_with`] and [`anchor_bounds_with`] measure objects:
|
||||
/// `preview` as in [`bounds_of`], and with `glyphs` (Preferences › Type › Measure Type by Glyph
|
||||
/// Bounds), point type, horizontal or vertical, by the ink of its glyphs instead of its em box
|
||||
/// ([`text_ink_in`]).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Measure {
|
||||
pub preview: bool,
|
||||
pub glyphs: bool,
|
||||
}
|
||||
|
||||
impl Measure {
|
||||
/// See [`by_glyphs`].
|
||||
fn by_glyphs(self, drawn: &Drawn, n: &Node) -> bool {
|
||||
self.glyphs
|
||||
&& matches!(&n.kind, NodeKind::Text(t) if matches!(t.kind, vectorcraft_doc::TextKind::Point))
|
||||
&& n.projection().is_none()
|
||||
&& !(self.preview && drawn.draws_geometry(n))
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `m` measures `n` by the ink of its glyphs: point type, not in perspective, and with
|
||||
/// `preview` only when no geometry effect changes what it draws.
|
||||
pub fn by_glyphs(symbols: &SymbolBoxes, n: &Node, m: Measure) -> bool {
|
||||
m.by_glyphs(&Drawn::new(symbols), n)
|
||||
}
|
||||
|
||||
/// [`bounds_of`] measured by `m`.
|
||||
pub fn bounds_of_with(doc: &Document, ids: &[NodeId], m: Measure) -> Option<Rect> {
|
||||
if !m.glyphs {
|
||||
return bounds_of(doc, ids, m.preview);
|
||||
}
|
||||
let symbols = doc.symbol_boxes();
|
||||
let drawn = Drawn::new(&symbols);
|
||||
if !ids.iter().filter_map(|id| doc.node(*id)).any(|n| m.by_glyphs(&drawn, n)) {
|
||||
return bounds_of(doc, ids, m.preview);
|
||||
}
|
||||
ids.iter().filter_map(|id| doc.node(*id)).fold(None, |acc, n| union_opt(acc, measured_in(&drawn, n, Affine::IDENTITY, m)))
|
||||
}
|
||||
|
||||
/// [`oriented_bounds`] measured by `m`.
|
||||
pub fn oriented_bounds_with(doc: &Document, ids: &[NodeId], m: Measure) -> Option<OrientedBox> {
|
||||
if !m.glyphs {
|
||||
return oriented_bounds(doc, ids, m.preview);
|
||||
}
|
||||
let symbols = doc.symbol_boxes();
|
||||
let drawn = Drawn::new(&symbols);
|
||||
if !ids.iter().filter_map(|id| doc.node(*id)).any(|n| m.by_glyphs(&drawn, n)) {
|
||||
return oriented_bounds(doc, ids, m.preview);
|
||||
}
|
||||
let angle = doc.bbox_angle(ids);
|
||||
let to_local = if angle == 0.0 { Affine::IDENTITY } else { OrientedBox { angle, rect: Rect::ZERO }.to_doc().inverse() };
|
||||
let rect = ids.iter().filter_map(|id| doc.node(*id)).fold(None, |acc, n| union_opt(acc, measured_in(&drawn, n, to_local, m)))?;
|
||||
Some(OrientedBox { angle, rect })
|
||||
}
|
||||
|
||||
/// [`anchor_bounds`] with the objects selected whole measured by `m`.
|
||||
pub fn anchor_bounds_with(selection: &Selection, doc: &Document, m: Measure) -> Option<Rect> {
|
||||
if selection.anchors.is_empty() {
|
||||
return None;
|
||||
}
|
||||
union_opt(bounds_of_with(doc, &selection.whole(), m), selection.anchors_box(doc))
|
||||
}
|
||||
|
||||
/// The box of `n` as `a` would place it, without changing it, measured by `m`: [`bounds_in`] with
|
||||
/// `preview`, else [`Node::bounds_in`] of its geometric bounds, and point type by its glyphs as
|
||||
/// [`Measure`] says.
|
||||
pub fn bounds_in_with(symbols: &SymbolBoxes, n: &Node, a: Affine, m: Measure) -> Option<Rect> {
|
||||
measured_in(&Drawn::new(symbols), n, a, m)
|
||||
}
|
||||
|
||||
/// See [`bounds_in_with`].
|
||||
fn measured_in(drawn: &Drawn, n: &Node, a: Affine, m: Measure) -> Option<Rect> {
|
||||
if m.by_glyphs(drawn, n)
|
||||
&& let NodeKind::Text(t) = &n.kind
|
||||
&& let Some(r) = text_ink_in(t, a)
|
||||
{
|
||||
// The fills' and characters' strokes paint around the glyphs, as in `Node::visual_bounds`.
|
||||
let o = if m.preview { n.appearance.outset().max(t.char_stroke_outset()) } else { 0.0 };
|
||||
return Some(r.inflate(o, o));
|
||||
}
|
||||
match (m.preview, a == Affine::IDENTITY) {
|
||||
(true, _) => drawn.next().bounds_in(n, a),
|
||||
(false, true) => n.geometric_bounds_with(drawn.symbols),
|
||||
(false, false) => n.bounds_in(drawn.symbols, a, false),
|
||||
}
|
||||
}
|
||||
|
||||
/// The box of the ink of type `t`'s glyphs with its text space mapped by `a * t.xf`: the cached
|
||||
/// ink ([`vectorcraft_doc::TextObject::ink`]) when that map keeps a rectangle square, else the
|
||||
/// glyph outlines and the underline and strikethrough bars mapped point by point
|
||||
/// ([`vectorcraft_text::envelope_ink`]). `None` when nothing draws (no text, only spaces without
|
||||
/// underline or strikethrough, or glyphs without outlines).
|
||||
pub fn text_ink_in(t: &vectorcraft_doc::TextObject, a: Affine) -> Option<Rect> {
|
||||
let m = a * t.xf;
|
||||
let [p, q, r, s, _, _] = m.as_coeffs();
|
||||
let square = (q.abs() <= 1e-12 && r.abs() <= 1e-12) || (p.abs() <= 1e-12 && s.abs() <= 1e-12);
|
||||
if square {
|
||||
let ink = match t.ink() {
|
||||
Some(ink) => ink,
|
||||
None => vectorcraft_text::envelope_ink(t, Affine::IDENTITY, None),
|
||||
};
|
||||
return ink.map(|i| m.transform_rect_bbox(i));
|
||||
}
|
||||
vectorcraft_text::envelope_ink(t, m, None)
|
||||
}
|
||||
|
||||
/// The ink of type `t` laid out as `layout`, in text space: its glyphs
|
||||
/// ([`vectorcraft_text::TextLayout::ink_bounds`]) and its underline and strikethrough bars
|
||||
/// ([`vectorcraft_text::decorations`]), what [`text_ink_in`] measures. `None` when nothing draws.
|
||||
pub fn laid_out_ink(layout: &vectorcraft_text::TextLayout, t: &vectorcraft_doc::TextObject) -> Option<Rect> {
|
||||
vectorcraft_text::decorations(layout, vectorcraft_text::FontDb::global(), t)
|
||||
.into_iter()
|
||||
.fold(layout.ink_bounds(), |acc, (_, bar)| union_opt(acc, Some(bar.bounding_box())))
|
||||
}
|
||||
|
||||
/// The visual bounds of `n` as drawn: those of its [`drawn_art`], else
|
||||
/// [`Node::visual_bounds_with`].
|
||||
pub fn visual_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
|
||||
|
||||
+473
-87
@@ -46,8 +46,8 @@ pub fn has_gradient_copies(n: &Node) -> bool {
|
||||
/// Is `n` a path or compound path with a visible Transform effect that makes copies and fills and
|
||||
/// strokes that paint the copies differently as one path than as separate objects
|
||||
/// ([`paints_copies_apart`])? Such an object is drawn through its art ([`outline_art`]), each copy
|
||||
/// an object of its own above the one before ([`copy_leaf`]), as Expand Appearance makes it
|
||||
/// (#5920).
|
||||
/// an object of its own below the one before ([`copy_leaf`]), as Expand Appearance makes it
|
||||
/// (#5920, #6316).
|
||||
pub(crate) fn has_layered_copies(n: &Node) -> bool {
|
||||
leaf_geometry(n).is_some_and(|(_, rule)| paints_copies_apart(&n.appearance, rule)) && has_transform_copies(n)
|
||||
}
|
||||
@@ -103,10 +103,27 @@ fn reaches_appearance(e: &Effect) -> bool {
|
||||
}
|
||||
|
||||
/// Is `n` a path or compound path that its Transform effects make into art of separate objects
|
||||
/// ([`has_gradient_copies`], [`has_layered_copies`], [`has_transform_options`])? It is drawn, baked and expanded through
|
||||
/// that art ([`reshape`]).
|
||||
/// ([`has_gradient_copies`], [`has_layered_copies`], [`has_transform_options`]), or whose
|
||||
/// live-effect plug-ins paint what they return ([`has_painted_effects`])? It is drawn, baked and
|
||||
/// expanded through that art ([`reshape`]).
|
||||
pub fn has_transform_art(n: &Node) -> bool {
|
||||
has_gradient_copies(n) || has_layered_copies(n) || has_transform_options(n)
|
||||
has_gradient_copies(n) || has_layered_copies(n) || has_transform_options(n) || has_painted_effects(n)
|
||||
}
|
||||
|
||||
/// Is `e` a visible live-effect plug-in whose manifest declares `paintOutput`: its id's plug-in id,
|
||||
/// else `None`.
|
||||
fn painted_plugin(e: &Effect) -> Option<&str> {
|
||||
let id = vectorcraft_plugins::effect::plugin_id(&e.id).filter(|_| e.visible)?;
|
||||
vectorcraft_plugins::effect::paints_output(id).then_some(id)
|
||||
}
|
||||
|
||||
/// Is `n` a path or compound path with a live-effect plug-in that paints what it returns
|
||||
/// ([`painted_plugin`]), its own or on a fill or stroke that paints? Each piece the plug-in returns
|
||||
/// is then an object of its own with its own paints ([`paint_leaf`], [`transform_item_leaf`]).
|
||||
pub fn has_painted_effects(n: &Node) -> bool {
|
||||
matches!(n.kind, NodeKind::Path { guide: false, clipping: false, .. } | NodeKind::Compound { .. })
|
||||
&& (n.appearance.effects.iter().any(|e| painted_plugin(e).is_some())
|
||||
|| n.appearance.items.iter().any(|i| crate::paints(i) && i.effects().iter().any(|e| painted_plugin(e).is_some())))
|
||||
}
|
||||
|
||||
/// Is `n` type without inline graphics that has a fill or stroke of its own that paints and has a
|
||||
@@ -163,17 +180,18 @@ fn image_rows(items: &[AppearanceItem]) -> Vec<AppearanceItem> {
|
||||
/// The shapes that the image `n`'s own fills and strokes paint ([`image_rows`]), each with its fill
|
||||
/// rule and the fill or stroke that paints it: the paths (or compound paths) along its frame that
|
||||
/// [`reshape`] makes, with each fill's or stroke's geometry effects applied as the renderer applies
|
||||
/// them. Empty for anything but an image with such fills or strokes.
|
||||
pub fn image_paint(n: &Node) -> Vec<(BezPath, FillRule, AppearanceItem)> {
|
||||
/// them. Empty for anything but an image with such fills or strokes. The swatches and patterns that
|
||||
/// live-effect plug-ins painting their pieces name are looked up in `doc`.
|
||||
pub fn image_paint(doc: Option<&Document>, n: &Node) -> Vec<(BezPath, FillRule, AppearanceItem)> {
|
||||
if !matches!(n.kind, NodeKind::Image(_)) || !has_item_geometry(n) {
|
||||
return vec![];
|
||||
}
|
||||
let Some(art) = reshape(None, n, None) else { return vec![] };
|
||||
let Some(art) = reshape(doc, n, None) else { return vec![] };
|
||||
let mut out = vec![];
|
||||
art.walk(&mut |c| {
|
||||
let (NodeKind::Path { rule, .. } | NodeKind::Compound { rule, .. }) = &c.kind else { return };
|
||||
let Some(g) = c.stroke_path() else { return };
|
||||
let ctx = GeomContext::of(c);
|
||||
let ctx = GeomContext { doc, ..GeomContext::of(c) };
|
||||
for item in c.appearance.items.iter().filter(|i| crate::paints(i)) {
|
||||
let fx = item.effects();
|
||||
let shape = if crate::has_geometry(fx) && !g.elements().is_empty() {
|
||||
@@ -286,14 +304,17 @@ fn with_id(art: &Node, id: NodeId) -> Node {
|
||||
/// through the guides could hold more than [`vectorcraft_doc::MAX_SYMBOL_NODES`] objects. A Repeat counts the Repeats in the art of
|
||||
/// `doc`'s symbols that its instances show toward its instance budget.
|
||||
pub fn outline_art(doc: Option<&Document>, n: &Node, symbol_art: Option<&Node>) -> Option<Node> {
|
||||
outline_art_with(doc, n, symbol_art, &outline_text, &SymbolNodes::default(), false)
|
||||
outline_art_with(doc, doc, n, symbol_art, &outline_text, &SymbolNodes::default(), false)
|
||||
}
|
||||
|
||||
/// [`outline_art`] with live objects evaluated through `outline` ([`evaluated_with`]); with
|
||||
/// `apart`, also a path or compound path whose Transform effect makes copies
|
||||
/// ([`has_transform_copies`]), as the one piece the copies are made from.
|
||||
/// ([`has_transform_copies`]), as the one piece the copies are made from. `symbols` is the document
|
||||
/// the symbols of the art come from; `paint_doc` is the one the swatches and patterns of painted
|
||||
/// pieces are looked up in.
|
||||
fn outline_art_with(
|
||||
doc: Option<&Document>,
|
||||
symbols: Option<&Document>,
|
||||
paint_doc: Option<&Document>,
|
||||
n: &Node,
|
||||
symbol_art: Option<&Node>,
|
||||
outline: OutlineHook,
|
||||
@@ -303,7 +324,7 @@ fn outline_art_with(
|
||||
let mut art = match &n.kind {
|
||||
NodeKind::Text(_) => outline_text(n)?,
|
||||
NodeKind::SymbolInstance { symbol, xf } => {
|
||||
let a = match doc {
|
||||
let a = match symbols {
|
||||
Some(d) => d.place_symbol_art(symbol, symbol_art?, *xf)?,
|
||||
None => {
|
||||
let mut a = symbol_art?.clone();
|
||||
@@ -323,10 +344,10 @@ fn outline_art_with(
|
||||
one.appearance.effects.clear();
|
||||
Node::group(n.id, vec![Arc::new(one)])
|
||||
}
|
||||
_ if vectorcraft_doc::live::is_live(n) => evaluated_with(doc, n, outline, nodes),
|
||||
_ if vectorcraft_doc::live::is_live(n) => evaluated_with(symbols, n, outline, nodes),
|
||||
_ => return None,
|
||||
};
|
||||
outline_nested_text(&mut art, false);
|
||||
outline_nested_text(paint_doc, &mut art, false);
|
||||
art.opacity = 1.0;
|
||||
art.blend = Default::default();
|
||||
art.isolate = false;
|
||||
@@ -433,21 +454,22 @@ fn instances_as_art(doc: Option<&Document>, n: &mut Node, outline: OutlineHook,
|
||||
/// Replace type anywhere inside `n` by its outlines, as art that keeps the type's look
|
||||
/// ([`as_art`]). Type in a clip group's clipping path (all of `n` when `clipping` is set) becomes
|
||||
/// its plain outlines ([`outline_text`]) with its visibility, which is what
|
||||
/// [`crate::clip_outline`] clips by.
|
||||
pub(crate) fn outline_nested_text(n: &mut Node, clipping: bool) {
|
||||
/// [`crate::clip_outline`] clips by. The swatches and patterns that live-effect plug-ins painting
|
||||
/// their pieces name are looked up in `doc`.
|
||||
pub(crate) fn outline_nested_text(doc: Option<&Document>, n: &mut Node, clipping: bool) {
|
||||
let clips = n.clips();
|
||||
let Some(ch) = n.children_mut() else { return };
|
||||
for (i, c) in ch.iter_mut().enumerate() {
|
||||
let clipping = clipping || (clips && i == 0);
|
||||
let art = match &c.kind {
|
||||
NodeKind::Text(_) if clipping => outline_text(c).map(|o| Node { visible: c.visible, ..o }),
|
||||
NodeKind::Text(_) => as_art(None, c, None),
|
||||
NodeKind::Text(_) => as_art(doc, c, None),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(o) = art {
|
||||
*c = Arc::new(o);
|
||||
} else if c.children().is_some() {
|
||||
outline_nested_text(Arc::make_mut(c), clipping);
|
||||
outline_nested_text(doc, Arc::make_mut(c), clipping);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -480,7 +502,7 @@ pub fn expand_outlined(doc: Option<&Document>, n: &Node, symbol_art: Option<&Nod
|
||||
// effect in a group of their own (copies of copies nest), as the reference app expands them
|
||||
// (#5612).
|
||||
if has_transform_art(n) || has_transform_copies(n) {
|
||||
return as_art_opts(doc, n, symbol_art, &outline_text, &SymbolNodes::default(), true);
|
||||
return as_art_opts(doc, doc, n, symbol_art, &outline_text, &SymbolNodes::default(), true);
|
||||
}
|
||||
let own_paint = match n.kind {
|
||||
NodeKind::Text(_) => !n.appearance.items.is_empty(),
|
||||
@@ -494,17 +516,34 @@ pub fn expand_outlined(doc: Option<&Document>, n: &Node, symbol_art: Option<&Nod
|
||||
|
||||
/// [`reshape`] without its checks (no geometry effects: just the art).
|
||||
pub(crate) fn as_art(doc: Option<&Document>, n: &Node, symbol_art: Option<&Node>) -> Option<Node> {
|
||||
as_art_with(doc, n, symbol_art, &outline_text, &SymbolNodes::default())
|
||||
as_art_with(doc, doc, n, symbol_art, &outline_text, &SymbolNodes::default())
|
||||
}
|
||||
|
||||
/// [`as_art`] with live objects evaluated through `outline` ([`evaluated_with`]).
|
||||
pub(crate) fn as_art_with(doc: Option<&Document>, n: &Node, symbol_art: Option<&Node>, outline: OutlineHook, nodes: &SymbolNodes) -> Option<Node> {
|
||||
as_art_opts(doc, n, symbol_art, outline, nodes, false)
|
||||
/// [`as_art`] with live objects evaluated through `outline` ([`evaluated_with`]). `symbols` is the
|
||||
/// document the symbols of the art come from; `paint_doc` is the one the swatches and patterns of
|
||||
/// painted pieces are looked up in.
|
||||
pub(crate) fn as_art_with(
|
||||
symbols: Option<&Document>,
|
||||
paint_doc: Option<&Document>,
|
||||
n: &Node,
|
||||
symbol_art: Option<&Node>,
|
||||
outline: OutlineHook,
|
||||
nodes: &SymbolNodes,
|
||||
) -> Option<Node> {
|
||||
as_art_opts(symbols, paint_doc, n, symbol_art, outline, nodes, false)
|
||||
}
|
||||
|
||||
/// [`as_art_with`]; with `apart`, a path's or compound path's Transform effects make each copy an
|
||||
/// object of its own ([`copy_leaf`]) rather than one path of every copy.
|
||||
fn as_art_opts(doc: Option<&Document>, n: &Node, symbol_art: Option<&Node>, outline: OutlineHook, nodes: &SymbolNodes, apart: bool) -> Option<Node> {
|
||||
fn as_art_opts(
|
||||
symbols: Option<&Document>,
|
||||
paint_doc: Option<&Document>,
|
||||
n: &Node,
|
||||
symbol_art: Option<&Node>,
|
||||
outline: OutlineHook,
|
||||
nodes: &SymbolNodes,
|
||||
apart: bool,
|
||||
) -> Option<Node> {
|
||||
let fx = geometry_effects(n);
|
||||
let mut art = match &n.kind {
|
||||
NodeKind::Image(im) => {
|
||||
@@ -532,13 +571,13 @@ fn as_art_opts(doc: Option<&Document>, n: &Node, symbol_art: Option<&Node>, outl
|
||||
Node::group(n.id, rows(below).into_iter().chain([Arc::new(image)]).chain(rows(above)).collect())
|
||||
}
|
||||
}
|
||||
_ => outline_art_with(doc, n, symbol_art, outline, nodes, apart)?,
|
||||
_ => outline_art_with(symbols, paint_doc, n, symbol_art, outline, nodes, apart)?,
|
||||
};
|
||||
reshape_leaves_apart(&mut art, &fx, Budget::EFFECTS, apart);
|
||||
reshape_leaves_apart(&mut art, &fx, &n.appearance.effects, Budget::EFFECTS, apart, paint_doc);
|
||||
if apart {
|
||||
hoist_copies(&mut art);
|
||||
}
|
||||
transform_items(&mut art);
|
||||
transform_items(&mut art, paint_doc);
|
||||
// Type and symbols in perspective: the art projected onto their plane.
|
||||
if let Some(h) = n.projection() {
|
||||
project_leaves(&mut art, &h);
|
||||
@@ -596,20 +635,23 @@ pub(crate) fn geometry_effects(n: &Node) -> Vec<&Effect> {
|
||||
/// symbol instances follow a Transform effect the same way ([`transform_placed`]); other geometry
|
||||
/// effects leave them as they are (a raster follows a non-affine map only as a mesh warp). The
|
||||
/// paths keep to one [`Budget::EFFECTS`] together (or to what they cost, when that is more): a path
|
||||
/// whose effect would pass what the others leave of it keeps its shape.
|
||||
pub(crate) fn reshape_leaves(art: &mut Node, fx: &[&Effect]) {
|
||||
reshape_leaves_within(art, fx, Budget::EFFECTS);
|
||||
/// whose effect would pass what the others leave of it keeps its shape. The swatches and patterns
|
||||
/// that live-effect plug-ins painting their pieces name are looked up in `doc`, and a deferred one
|
||||
/// is listed by its index in `owner`, the effect list `fx` was taken from ([`reshape_leaves_apart`]).
|
||||
pub(crate) fn reshape_leaves(art: &mut Node, fx: &[&Effect], owner: &[Effect], doc: Option<&Document>) {
|
||||
reshape_leaves_apart(art, fx, owner, Budget::EFFECTS, false, doc);
|
||||
}
|
||||
|
||||
/// [`reshape_leaves`], and how gradients that paint the art as a whole (a group's own fills and
|
||||
/// strokes) follow the effects ([`crate::Carry`]).
|
||||
pub(crate) fn reshape_leaves_carrying(art: &mut Node, fx: &[&Effect]) -> crate::Carry {
|
||||
reshape_leaves_within(art, fx, Budget::EFFECTS)
|
||||
pub(crate) fn reshape_leaves_carrying(art: &mut Node, fx: &[&Effect], owner: &[Effect], doc: Option<&Document>) -> crate::Carry {
|
||||
reshape_leaves_apart(art, fx, owner, Budget::EFFECTS, false, doc)
|
||||
}
|
||||
|
||||
/// [`reshape_leaves_carrying`] whose paths keep to `budget` together, or to what they cost.
|
||||
pub(crate) fn reshape_leaves_within(art: &mut Node, fx: &[&Effect], budget: Budget) -> crate::Carry {
|
||||
reshape_leaves_apart(art, fx, budget, false)
|
||||
#[cfg(test)]
|
||||
pub(crate) fn reshape_leaves_within(art: &mut Node, fx: &[&Effect], budget: Budget, doc: Option<&Document>) -> crate::Carry {
|
||||
reshape_leaves_apart(art, fx, &[], budget, false, doc)
|
||||
}
|
||||
|
||||
/// `art`, the group of the one piece a path's copies are made from ([`outline_art_with`]), holding
|
||||
@@ -628,10 +670,15 @@ fn hoist_copies(art: &mut Node) {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`reshape_leaves_within`]; with `apart`, a Transform effect's copies of each path are objects
|
||||
/// of their own ([`copy_leaf`]) whatever paints them, as Expand Appearance makes them. Returns how
|
||||
/// gradients that paint the art as a whole follow the effects.
|
||||
fn reshape_leaves_apart(art: &mut Node, fx: &[&Effect], budget: Budget, apart: bool) -> crate::Carry {
|
||||
/// [`reshape_leaves_carrying`] whose paths keep to `budget` together, or to what they cost; with
|
||||
/// `apart`, a Transform effect's copies of each path are objects
|
||||
/// of their own ([`copy_leaf`]) whatever paints them, as Expand Appearance makes them. A live-effect
|
||||
/// plug-in that paints what it returns makes each path a group of its pieces ([`paint_leaf`]),
|
||||
/// whose swatches and patterns must be in `doc` (without one, pieces that name one are refused).
|
||||
/// Returns how gradients that paint the art as a whole follow the effects. A deferred live-effect
|
||||
/// plug-in is listed by its index in `owner`, the effect list `fx` was taken from, or in `fx` when
|
||||
/// `owner` does not hold it.
|
||||
fn reshape_leaves_apart(art: &mut Node, fx: &[&Effect], owner: &[Effect], budget: Budget, apart: bool, doc: Option<&Document>) -> crate::Carry {
|
||||
let mut whole = crate::Carry::default();
|
||||
if fx.is_empty() {
|
||||
return whole;
|
||||
@@ -650,14 +697,31 @@ fn reshape_leaves_apart(art: &mut Node, fx: &[&Effect], budget: Budget, apart: b
|
||||
});
|
||||
let art_stroke = art.appearance.stroke().is_some_and(|s| !s.paint.is_none() && s.width > 0.0);
|
||||
let style_safe = leaves_safe && !art_stroke;
|
||||
let listed = fx;
|
||||
let opt_fx = crate::geometry::optimize_geometry_effects(fx, style_safe);
|
||||
let fx = opt_fx.as_slice();
|
||||
let seed = art.id.0;
|
||||
let mut used = art_cost(art);
|
||||
let max = budget.at_least(used);
|
||||
// Read before the effects run: a path's own copies don't make it art of several objects.
|
||||
let copy_whole = copies_whole(art);
|
||||
for e in fx {
|
||||
let Some(bounds) = art.geometric_bounds() else { break };
|
||||
let params = merged_params(&e.id, &e.params);
|
||||
// Where a live-effect plug-in sits, which it is listed by when it is deferred.
|
||||
let effect = owner.iter().position(|x| std::ptr::eq(x, *e)).or_else(|| listed.iter().position(|x| std::ptr::eq(*x, *e)));
|
||||
if let Some(plugin) = painted_plugin(e) {
|
||||
let at = effect.map(|effect| vectorcraft_plugins::deferred::Stage { object: seed, effect, item: None });
|
||||
for_each_leaf(art, &mut |leaf| {
|
||||
let Some((path, _)) = leaf_geometry(leaf) else { return };
|
||||
let before = Budget::of(&path);
|
||||
let room = max.minus(used.minus(before));
|
||||
if let Some(made) = paint_leaf(leaf, plugin, ¶ms, bounds, room, doc, at) {
|
||||
used = used.minus(before).plus(made);
|
||||
}
|
||||
});
|
||||
continue;
|
||||
}
|
||||
// Placed gradients follow a Transform effect, Free Distort and the Warp effects
|
||||
// ([`crate::Carry`]): under Free Distort and Warp (`boxes`) each path's move and scale from
|
||||
// its bounds before the effect to its bounds after it. Under those and the other effects,
|
||||
@@ -684,6 +748,14 @@ fn reshape_leaves_apart(art: &mut Node, fx: &[&Effect], budget: Budget, apart: b
|
||||
let options = crate::distort::TransformOptions::of(¶ms);
|
||||
(m, copies, options, crate::distort::transform_affine(bounds, ¶ms, seed))
|
||||
});
|
||||
// Art of several objects is copied whole, so the copies don't interleave member by member.
|
||||
if let Some((m, copies, options, _)) = transform
|
||||
&& copies > 0
|
||||
&& copy_whole
|
||||
{
|
||||
used = copy_art(art, m, copies, copy_scaling(options), max);
|
||||
continue;
|
||||
}
|
||||
for_each_leaf(art, &mut |leaf| {
|
||||
let Some((path, rule)) = leaf_geometry(leaf) else { return };
|
||||
// What the other paths leave of the budget.
|
||||
@@ -693,7 +765,7 @@ fn reshape_leaves_apart(art: &mut Node, fx: &[&Effect], budget: Budget, apart: b
|
||||
// Transform Patterns and Scale Strokes & Effects change each copy's fills and
|
||||
// strokes; a gradient paints each copy by itself, not the one piece the copies
|
||||
// make together; and Expand Appearance (`apart`) makes each copy an object.
|
||||
// Fills and strokes that stack paint each copy above the one before (#5920).
|
||||
// Fills and strokes that stack paint each copy below the one before (#5920, #6316).
|
||||
let separate = copies > 0
|
||||
&& (apart
|
||||
|| leaf.appearance.has_unplaced_gradient()
|
||||
@@ -714,7 +786,8 @@ fn reshape_leaves_apart(art: &mut Node, fx: &[&Effect], budget: Budget, apart: b
|
||||
return;
|
||||
}
|
||||
}
|
||||
let Some(out) = apply_one(&e.id, ¶ms, &path, bounds, &GeomContext { seed, ..GeomContext::of(leaf) }, room) else {
|
||||
let stage = effect.map(|effect| vectorcraft_plugins::deferred::Stage { object: seed, effect, item: None });
|
||||
let Some(out) = apply_one(&e.id, ¶ms, &path, bounds, &GeomContext { seed, stage, ..GeomContext::of(leaf) }, room) else {
|
||||
kept_ids.insert(leaf.id);
|
||||
return;
|
||||
};
|
||||
@@ -759,6 +832,19 @@ fn reshape_leaves_apart(art: &mut Node, fx: &[&Effect], budget: Budget, apart: b
|
||||
/// doesn't grow with them (a horizontal line stretched vertically).
|
||||
const MAX_STROKE_SCALE: f64 = 1.0e6;
|
||||
|
||||
/// How a Transform effect with `options` scales what it maps: strokes, effects and patterns as
|
||||
/// its options say.
|
||||
fn copy_scaling(options: crate::distort::TransformOptions) -> vectorcraft_doc::Scaling {
|
||||
vectorcraft_doc::Scaling {
|
||||
strokes: options.strokes,
|
||||
effects: options.strokes.then_some(crate::scale_effect as fn(&mut Effect, f64)),
|
||||
patterns: options.patterns,
|
||||
// A map that flattens the path leaves its fitted gradients fitted, as `fixes_fit` does.
|
||||
keep_fit_when_flat: true,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// A Transform effect whose Transform Patterns or Scale Strokes & Effects is on (`options`), with
|
||||
/// map `m` and `copies`, applied to `leaf` (a path or compound path with geometry `path`) as Object
|
||||
/// › Transform with those options transforms an object (#885): the strokes' weights and dashes
|
||||
@@ -775,14 +861,7 @@ fn transform_leaf(
|
||||
options: crate::distort::TransformOptions,
|
||||
budget: Budget,
|
||||
) -> (Budget, bool) {
|
||||
let scaling = vectorcraft_doc::Scaling {
|
||||
strokes: options.strokes,
|
||||
effects: options.strokes.then_some(crate::scale_effect as fn(&mut Effect, f64)),
|
||||
patterns: options.patterns,
|
||||
// A map that flattens the path leaves its fitted gradients fitted, as `fixes_fit` does.
|
||||
keep_fit_when_flat: true,
|
||||
..Default::default()
|
||||
};
|
||||
let scaling = copy_scaling(options);
|
||||
if !options.objects {
|
||||
if options.strokes
|
||||
&& let Some(k) = vectorcraft_doc::Scaling::factor(m).filter(|k| k.is_finite() && *k > 0.0)
|
||||
@@ -807,51 +886,298 @@ fn transform_leaf(
|
||||
(copy_leaf(leaf, path, m, copies, scaling, budget), true)
|
||||
}
|
||||
|
||||
/// Replace `leaf` (a path or compound path with geometry `path`) by a group of it and `copies`
|
||||
/// more, each mapped once more by `m` than the last (with `scaling`), as a Transform effect copies
|
||||
/// an object. Each copy is an object of its own, so its gradients fit it (a placed gradient moves
|
||||
/// with it) rather than spanning every copy, and its strokes and patterns can differ from the
|
||||
/// others'. The copies stop where [`crate::distort::transform_within`]'s would, before they pass
|
||||
/// `budget`, and before one's strokes would scale past [`MAX_STROKE_SCALE`]. Returns what the
|
||||
/// copies cost, the original included.
|
||||
fn copy_leaf(leaf: &mut Node, path: &PathData, m: Affine, copies: usize, scaling: vectorcraft_doc::Scaling, budget: Budget) -> Budget {
|
||||
let mut made = Budget::of(path);
|
||||
let mut placements: Vec<Arc<Node>> = vec![Arc::new(leaf.clone())];
|
||||
/// Does a Transform effect's copies of `art` copy it whole ([`copy_art`])? They do when `art` is a
|
||||
/// group or layer of several objects, at least one of which paints, with no type left in it and no
|
||||
/// fill or stroke of its own that paints. Type that stays type is copied in its place by the baking
|
||||
/// that keeps it, a clipping group's first member is its clip, and the art's own fills and strokes
|
||||
/// paint the shape of everything under it, copies included. A member that [`makes_art`] is copied
|
||||
/// in its place as well: each copy would make that art again, past what [`copy_art`] counts.
|
||||
fn copies_whole(art: &Node) -> bool {
|
||||
fn parts(n: &Node, count: &mut usize, painted: &mut bool, text: &mut bool) {
|
||||
match &n.kind {
|
||||
NodeKind::Text(_) => *text = true,
|
||||
NodeKind::Path { guide: false, .. } | NodeKind::Compound { .. } => {
|
||||
*count += 1;
|
||||
*painted |= n.appearance.items.iter().any(crate::paints);
|
||||
}
|
||||
NodeKind::Image(_) | NodeKind::SymbolInstance { .. } => {
|
||||
*count += 1;
|
||||
*painted = true;
|
||||
}
|
||||
NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } => {
|
||||
*painted |= n.appearance.items.iter().any(crate::paints);
|
||||
children.iter().for_each(|c| parts(c, count, painted, text));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if art.clips() || !matches!(art.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }) || art.appearance.items.iter().any(crate::paints) {
|
||||
return false;
|
||||
}
|
||||
let (mut count, mut painted, mut text) = (0, false, false);
|
||||
parts(art, &mut count, &mut painted, &mut text);
|
||||
count > 1 && painted && !text && !art.children().into_iter().flatten().any(|c| makes_art(c))
|
||||
}
|
||||
|
||||
/// Does `n` or anything visible under it, the art of its opacity masks included, carry a visible
|
||||
/// effect that makes art when it is drawn (its own or that of a fill or stroke that paints), or is
|
||||
/// any of them a compound shape? Raster effects, color adjustments and unknown effects make none.
|
||||
fn makes_art(n: &Node) -> bool {
|
||||
let makes = |e: &Effect| {
|
||||
e.visible
|
||||
&& (is_geometry(&e.id)
|
||||
|| crate::is_pathfinder(&e.id)
|
||||
|| crate::is_three_d(&e.id)
|
||||
|| e.id == crate::CROP_MARKS
|
||||
|| e.id == crate::addons::halftone::HALFTONE)
|
||||
};
|
||||
n.visible
|
||||
&& (n.is_compound_shape()
|
||||
|| n.appearance.effects.iter().any(makes)
|
||||
|| n.appearance.items.iter().any(|i| crate::paints(i) && i.effects().iter().any(makes))
|
||||
|| n.extras().mask.as_deref().is_some_and(|m| makes_art(&m.art))
|
||||
|| n.children().into_iter().flatten().any(|c| makes_art(c)))
|
||||
}
|
||||
|
||||
/// What the paths under `n` cost together, those of its opacity masks included.
|
||||
fn copy_cost(n: &Node) -> Budget {
|
||||
let mut cost = Budget { anchors: 0, lines: 0 };
|
||||
vectorcraft_doc::swatches::walk_with_masks(n, &mut |c| {
|
||||
if let NodeKind::Path { guide: false, path, .. } = &c.kind {
|
||||
cost = cost.plus(Budget::of(path));
|
||||
}
|
||||
});
|
||||
cost
|
||||
}
|
||||
|
||||
/// What the geometry effects of `leaf` (with geometry `path`) and of its fills and strokes add to
|
||||
/// what `path` costs when it is drawn.
|
||||
fn effects_cost(leaf: &Node, path: &PathData) -> Budget {
|
||||
let none = Budget { anchors: 0, lines: 0 };
|
||||
let items: Vec<&AppearanceItem> = leaf.appearance.items.iter().filter(|i| crate::paints(i) && crate::has_geometry(i.effects())).collect();
|
||||
if items.is_empty() && !crate::has_geometry(&leaf.appearance.effects) {
|
||||
return none;
|
||||
}
|
||||
let Some(bounds) = path.bounds() else { return none };
|
||||
let ctx = GeomContext { seed: leaf.id.0, ..GeomContext::of(leaf) };
|
||||
let own = crate::apply_geometry_with(&leaf.appearance.effects, path, bounds, &ctx);
|
||||
let item_cost = |i: &&AppearanceItem| {
|
||||
let b = own.bounds()?;
|
||||
Some(Budget::of(&crate::apply_geometry_with(i.effects(), &own, b, &ctx.item(i))))
|
||||
};
|
||||
// The object's geometry is drawn as it is only by what has no geometry effect of its own.
|
||||
let plain = items.is_empty() || leaf.appearance.items.iter().any(|i| crate::paints(i) && !crate::has_geometry(i.effects()));
|
||||
let base = if plain { Budget::of(&own) } else { none };
|
||||
base.plus(items.iter().filter_map(item_cost).fold(none, Budget::plus)).minus(Budget::of(path))
|
||||
}
|
||||
|
||||
/// Remove the guides anywhere under `n`.
|
||||
fn without_guides(n: &mut Node) {
|
||||
let Some(children) = n.children_mut() else { return };
|
||||
children.retain(|c| !matches!(c.kind, NodeKind::Path { guide: true, .. }));
|
||||
children.iter_mut().for_each(|c| without_guides(Arc::make_mut(c)));
|
||||
}
|
||||
|
||||
/// Replace the members of `art` by groups of them: the original on top and `copies` more below it,
|
||||
/// each mapped once more by `m` than the last (with `scaling`), as a Transform effect copies a
|
||||
/// group, without its guides. The copies stop where [`copy_leaf`]'s would, before they pass `budget` or
|
||||
/// [`crate::distort::MAX_COPY_OBJECTS`] nodes in all, the art of opacity masks counted. Returns what
|
||||
/// the art costs after.
|
||||
fn copy_art(art: &mut Node, m: Affine, copies: usize, scaling: vectorcraft_doc::Scaling, budget: Budget) -> Budget {
|
||||
let Some(members) = art.children().cloned() else { return art_cost(art) };
|
||||
let original = Node::group(art.id, members);
|
||||
let mut made = copy_cost(&original);
|
||||
let mut drawn = original.clone();
|
||||
without_guides(&mut drawn);
|
||||
let mut nodes = original.count_with_masks();
|
||||
let one = drawn.count_with_masks();
|
||||
let mut placements: Vec<Arc<Node>> = vec![];
|
||||
let mut xf = Affine::IDENTITY;
|
||||
for _ in 0..copies {
|
||||
xf *= m;
|
||||
// A copy scaled up is drawn with more lines.
|
||||
let cost = Budget::of(&path.transformed(xf));
|
||||
if !budget.fits(made.plus(cost)) {
|
||||
break;
|
||||
}
|
||||
if scaling.strokes && vectorcraft_doc::Scaling::factor(xf).is_some_and(|k| !(k.is_finite() && k <= MAX_STROKE_SCALE)) {
|
||||
break;
|
||||
}
|
||||
let mut copy = leaf.clone();
|
||||
nodes = nodes.saturating_add(one);
|
||||
if nodes > crate::distort::MAX_COPY_OBJECTS {
|
||||
break;
|
||||
}
|
||||
let mut copy = drawn.clone();
|
||||
copy.transform(xf, scaling);
|
||||
if !copy.geometric_bounds().is_some_and(crate::distort::within_reach) {
|
||||
let cost = copy_cost(©);
|
||||
if !budget.fits(made.plus(cost)) || !copy.geometric_bounds().is_some_and(crate::distort::within_reach) {
|
||||
break;
|
||||
}
|
||||
placements.push(Arc::new(copy));
|
||||
made = made.plus(cost);
|
||||
}
|
||||
// The copies become nodes of their own under a group with the object's id and name.
|
||||
// The last copy is the lowest.
|
||||
placements.reverse();
|
||||
placements.push(Arc::new(original));
|
||||
if let Some(children) = art.children_mut() {
|
||||
*children = placements;
|
||||
}
|
||||
made
|
||||
}
|
||||
|
||||
/// Replace `leaf` (a path or compound path with geometry `path`) by a group of it and `copies`
|
||||
/// more below it, each mapped once more by `m` than the last (with `scaling`), as a Transform effect
|
||||
/// copies an object. Each copy is an object of its own, so its gradients fit it (a placed gradient
|
||||
/// moves with it) rather than spanning every copy, and its strokes and patterns can differ from the
|
||||
/// others'. The copies stop where [`crate::distort::transform_within`]'s would, before they pass
|
||||
/// `budget` with what each copy's own geometry effects make ([`effects_cost`]), and before one's
|
||||
/// strokes would scale past [`MAX_STROKE_SCALE`]. Returns what the copies cost, the original
|
||||
/// included.
|
||||
fn copy_leaf(leaf: &mut Node, path: &PathData, m: Affine, copies: usize, scaling: vectorcraft_doc::Scaling, budget: Budget) -> Budget {
|
||||
let mut made = Budget::of(path).plus(effects_cost(leaf, path));
|
||||
let mut placements: Vec<Arc<Node>> = vec![Arc::new(leaf.clone())];
|
||||
let mut xf = Affine::IDENTITY;
|
||||
for _ in 0..copies {
|
||||
xf *= m;
|
||||
// A copy scaled up is drawn with more lines.
|
||||
let moved = path.transformed(xf);
|
||||
if scaling.strokes && vectorcraft_doc::Scaling::factor(xf).is_some_and(|k| !(k.is_finite() && k <= MAX_STROKE_SCALE)) {
|
||||
break;
|
||||
}
|
||||
let mut copy = leaf.clone();
|
||||
copy.transform(xf, scaling);
|
||||
// Each copy's own geometry effects make a different amount, as the copy and its effects scale.
|
||||
let cost = Budget::of(&moved).plus(effects_cost(©, &moved));
|
||||
if !budget.fits(made.plus(cost)) || !copy.geometric_bounds().is_some_and(crate::distort::within_reach) {
|
||||
break;
|
||||
}
|
||||
placements.push(Arc::new(copy));
|
||||
made = made.plus(cost);
|
||||
}
|
||||
// The copies become nodes of their own under a group with the object's id and name, the object
|
||||
// on top.
|
||||
placements.reverse();
|
||||
let mut g = Node::group(leaf.id, placements);
|
||||
g.name = leaf.name.clone();
|
||||
*leaf = g;
|
||||
made
|
||||
}
|
||||
|
||||
/// Live-effect plug-in `plugin` (one that paints what it returns) with `params` run on `leaf`, a
|
||||
/// path or compound path, as one of its object's effects with reference box `bounds`: `leaf`
|
||||
/// becomes a group with its id and name of one object for each piece returned, in order, each with
|
||||
/// the piece's geometry and the leaf's appearance with the piece's fills and strokes in place of
|
||||
/// its own ([`vectorcraft_plugins::painted::appearance`]). The swatches and patterns the pieces
|
||||
/// name must be in `doc`, and without one the pieces may name none. Returns what the pieces cost
|
||||
/// ([`piece_cost`]); `None` (the leaf stays as it is) when the plug-in fails, the pieces are
|
||||
/// refused, or they would pass `budget`. A deferred run is listed by `at`.
|
||||
fn paint_leaf(
|
||||
leaf: &mut Node,
|
||||
plugin: &str,
|
||||
params: &serde_json::Value,
|
||||
bounds: Rect,
|
||||
budget: Budget,
|
||||
doc: Option<&Document>,
|
||||
at: Option<vectorcraft_plugins::deferred::Stage>,
|
||||
) -> Option<Budget> {
|
||||
let (path, rule) = leaf_geometry(leaf)?;
|
||||
let paints = vectorcraft_plugins::effect::Paints { appearance: Some(&leaf.appearance), item: None, at };
|
||||
let pieces = vectorcraft_plugins::effect::apply_painted(plugin, params, &path, bounds, rule, paints, doc)?;
|
||||
let made = pieces.iter().map(|p| piece_cost(&p.path)).fold(Budget { anchors: 0, lines: 0 }, Budget::plus);
|
||||
if !budget.fits(made) {
|
||||
return None;
|
||||
}
|
||||
let children = pieces
|
||||
.iter()
|
||||
.map(|p| {
|
||||
let mut one = leaf.clone();
|
||||
one.name = None;
|
||||
one.appearance = vectorcraft_plugins::painted::appearance(&leaf.appearance, p).into();
|
||||
set_geometry(&mut one, p.path.clone(), p.rule);
|
||||
Arc::new(one)
|
||||
})
|
||||
.collect();
|
||||
let mut g = Node::group(leaf.id, children);
|
||||
g.name = leaf.name.clone();
|
||||
*leaf = g;
|
||||
Some(made)
|
||||
}
|
||||
|
||||
/// What a painted piece with geometry `path` costs: its anchors and lines, plus a share of the
|
||||
/// effects' budget for the object it becomes, so that a budget of [`Budget::EFFECTS`] holds at most
|
||||
/// [`MAX_OBJECTS`](vectorcraft_plugins::objects::MAX_OBJECTS) pieces however small their paths.
|
||||
fn piece_cost(path: &PathData) -> Budget {
|
||||
let max = vectorcraft_plugins::objects::MAX_OBJECTS;
|
||||
Budget::of(path).plus(Budget { anchors: Budget::EFFECTS.anchors / max, lines: Budget::EFFECTS.lines / max })
|
||||
}
|
||||
|
||||
/// Live-effect plug-in `plugin` (one that paints what it returns) with `params`, a later effect of
|
||||
/// `item`, run on `leaf`, a path of [`transform_piece`] painted by copies of `item`, with reference
|
||||
/// box `bounds`: `leaf` becomes a group of one path for each piece returned, in order. A piece that
|
||||
/// gives fills (on a fill) or strokes (on a stroke) is painted by copies of `item`, the leaf's first
|
||||
/// fill or stroke, with those paints ([`vectorcraft_plugins::painted::items`]), under the leaf's
|
||||
/// other fills and strokes; one that gives none keeps the leaf's. Returns
|
||||
/// what the paths cost ([`piece_cost`]); `None` (the leaf stays as it is) when the plug-in fails,
|
||||
/// the pieces are refused (a swatch or pattern not in `doc`, or any without one), or they would pass
|
||||
/// `budget`. A deferred run is listed by `at`.
|
||||
fn paint_piece(
|
||||
leaf: &mut Node,
|
||||
item: &AppearanceItem,
|
||||
(plugin, params): (&str, &serde_json::Value),
|
||||
bounds: Rect,
|
||||
budget: Budget,
|
||||
doc: Option<&Document>,
|
||||
at: Option<vectorcraft_plugins::deferred::Stage>,
|
||||
) -> Option<Budget> {
|
||||
let (path, rule) = leaf_geometry(leaf)?;
|
||||
let paints = vectorcraft_plugins::effect::Paints { appearance: Some(&leaf.appearance), item: Some(0), at };
|
||||
let pieces = vectorcraft_plugins::effect::apply_painted(plugin, params, &path, bounds, rule, paints, doc)?;
|
||||
let made = pieces.iter().map(|p| piece_cost(&p.path)).fold(Budget { anchors: 0, lines: 0 }, Budget::plus);
|
||||
if !budget.fits(made) {
|
||||
return None;
|
||||
}
|
||||
let mut children = vec![];
|
||||
for p in pieces.iter() {
|
||||
let gives = match item {
|
||||
AppearanceItem::Fill(_) => p.fills.is_some(),
|
||||
AppearanceItem::Stroke(_) => p.strokes.is_some(),
|
||||
};
|
||||
let items = if gives {
|
||||
let mut items = vectorcraft_plugins::painted::items(item, p);
|
||||
items.extend_from_slice(leaf.appearance.items.get(1..).unwrap_or_default());
|
||||
items
|
||||
} else {
|
||||
leaf.appearance.items.clone()
|
||||
};
|
||||
if items.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut one = leaf.clone();
|
||||
one.appearance.items = items;
|
||||
set_geometry(&mut one, p.path.clone(), p.rule);
|
||||
children.push(Arc::new(one));
|
||||
}
|
||||
*leaf = Node::group(leaf.id, children);
|
||||
Some(made)
|
||||
}
|
||||
|
||||
/// Every path and compound path under `art` (other than clipping paths) with a fill or stroke whose
|
||||
/// own Transform effect has Transform Patterns or Scale Strokes & Effects on
|
||||
/// ([`item_transform_options`]), made into a group of objects of its own ([`transform_item_leaf`]).
|
||||
/// The new paths keep to one [`Budget::EFFECTS`] together (or to what `art` costs, when that is
|
||||
/// more).
|
||||
fn transform_items(art: &mut Node) {
|
||||
/// ([`item_transform_options`]), or whose own live-effect plug-in paints what it returns
|
||||
/// ([`painted_plugin`]), made into a group of objects of its own ([`transform_item_leaf`]). The
|
||||
/// new paths keep to one [`Budget::EFFECTS`] together (or to what `art` costs, when that is more).
|
||||
/// The swatches and patterns painted pieces name must be in `doc`; without one they may name none.
|
||||
fn transform_items(art: &mut Node, doc: Option<&Document>) {
|
||||
let mut used = art_cost(art);
|
||||
let max = Budget::EFFECTS.at_least(used);
|
||||
for_each_leaf(art, &mut |leaf| transform_item_leaf(leaf, max, &mut used));
|
||||
for_each_leaf(art, &mut |leaf| transform_item_leaf(leaf, max, &mut used, doc));
|
||||
}
|
||||
|
||||
/// Is `e` an effect that makes a fill's or stroke's piece of a path an object of its own: a
|
||||
/// Transform effect that [`reaches_appearance`], or a live-effect plug-in that paints what it
|
||||
/// returns ([`painted_plugin`])?
|
||||
fn makes_piece(e: &Effect) -> bool {
|
||||
reaches_appearance(e) || painted_plugin(e).is_some()
|
||||
}
|
||||
|
||||
/// Does `item` paint and carry an effect that [`makes_piece`]?
|
||||
fn item_makes_piece(item: &AppearanceItem) -> bool {
|
||||
crate::paints(item) && item.effects().iter().any(makes_piece)
|
||||
}
|
||||
|
||||
/// `leaf`, a path or compound path with a fill or stroke whose own Transform effect has Transform
|
||||
@@ -866,8 +1192,17 @@ fn transform_items(art: &mut Node) {
|
||||
/// effects stay on each of them. The other fills and strokes stay together on paths of the leaf's
|
||||
/// geometry between them, with their own effects. `max` is the budget of the whole art and `used`
|
||||
/// what it has spent.
|
||||
fn transform_item_leaf(leaf: &mut Node, max: Budget, used: &mut Budget) {
|
||||
if matches!(leaf.kind, NodeKind::Path { clipping: true, .. }) || !leaf.appearance.items.iter().any(item_transform_options) {
|
||||
///
|
||||
/// A fill or stroke whose first such effect is a live-effect plug-in that paints what it returns
|
||||
/// ([`painted_plugin`]) becomes, in place of a transformed path, one path for each piece the
|
||||
/// plug-in returns, painted by copies of the fill or stroke with the piece's paints
|
||||
/// ([`vectorcraft_plugins::painted::items`]); the plug-in is given the geometry before it and told
|
||||
/// which fill or stroke it is on. When it fails (or names a swatch or pattern not in `doc`, or any
|
||||
/// without a document), the
|
||||
/// fill or stroke paints that geometry as it is. The geometry effects after it then apply to all
|
||||
/// the pieces as one ([`transform_piece`]).
|
||||
fn transform_item_leaf(leaf: &mut Node, max: Budget, used: &mut Budget, doc: Option<&Document>) {
|
||||
if matches!(leaf.kind, NodeKind::Path { clipping: true, .. }) || !leaf.appearance.items.iter().any(item_makes_piece) {
|
||||
return;
|
||||
}
|
||||
let Some((path, rule)) = leaf_geometry(leaf) else { return };
|
||||
@@ -895,12 +1230,12 @@ fn transform_item_leaf(leaf: &mut Node, max: Budget, used: &mut Budget) {
|
||||
let mut pieces: Vec<Arc<Node>> = vec![];
|
||||
let mut plain: Vec<AppearanceItem> = vec![];
|
||||
// Which items become a transformed piece of their own; every other run of items is one plain piece.
|
||||
let transformed: Vec<bool> = original.appearance.items.iter().map(|i| crate::paints(i) && i.effects().iter().any(reaches_appearance)).collect();
|
||||
let transformed: Vec<bool> = original.appearance.items.iter().map(item_makes_piece).collect();
|
||||
// The number of pieces after item `i` in paint order: each costs at least the leaf's geometry.
|
||||
let later = |i: usize| (i + 1..transformed.len()).filter(|&j| transformed[j] || j == i + 1 || transformed[j - 1]).count();
|
||||
for (i, item) in original.appearance.items.iter().enumerate() {
|
||||
let fx = item.effects();
|
||||
let Some(at) = fx.iter().position(reaches_appearance).filter(|_| crate::paints(item)) else {
|
||||
let Some(at) = fx.iter().position(makes_piece).filter(|_| crate::paints(item)) else {
|
||||
plain.push(item.clone());
|
||||
continue;
|
||||
};
|
||||
@@ -923,6 +1258,36 @@ fn transform_item_leaf(leaf: &mut Node, max: Budget, used: &mut Budget) {
|
||||
};
|
||||
let Some(tb) = g.bounds() else { continue };
|
||||
let params = merged_params(&fx[at].id, &fx[at].params);
|
||||
// Where each effect the piece keeps sits among the item's effects.
|
||||
let kept: Vec<usize> = (0..at).filter(|&k| !is_geometry(&fx[k].id)).chain(at + 1..fx.len()).collect();
|
||||
if let Some(plugin) = painted_plugin(&fx[at]) {
|
||||
let mut it = item.clone();
|
||||
let rest: Vec<Effect> = before.iter().filter(|e| !is_geometry(&e.id)).chain(&fx[at + 1..]).cloned().collect();
|
||||
*it.effects_mut() = rest;
|
||||
let mut one = Appearance { items: vec![it.clone()], ..Default::default() };
|
||||
if carry.moves(&one) {
|
||||
carry.apply(&mut one);
|
||||
}
|
||||
let it = one.items.pop().unwrap_or(it);
|
||||
let stage = vectorcraft_plugins::deferred::Stage { object: ctx.seed, effect: at, item: Some(i) };
|
||||
let paints = vectorcraft_plugins::effect::Paints { appearance: Some(&original.appearance), item: Some(i), at: Some(stage) };
|
||||
let painted = vectorcraft_plugins::effect::apply_painted(plugin, ¶ms, &g, tb, rule, paints, doc)
|
||||
.map(|pieces| pieces.iter().map(|p| (p.path.clone(), p.rule, vectorcraft_plugins::painted::items(&it, p))).collect::<Vec<_>>())
|
||||
.filter(|pieces| room.fits(pieces.iter().map(|(p, ..)| piece_cost(p)).fold(Budget { anchors: 0, lines: 0 }, Budget::plus)));
|
||||
// A failed run draws the fill or stroke on the geometry it was given.
|
||||
let returned = painted.unwrap_or_else(|| vec![(g.clone(), rule, vec![it])]);
|
||||
let mut group: Vec<Arc<Node>> = vec![];
|
||||
for (path, rule, items) in returned.into_iter().filter(|(_, _, items)| !items.is_empty()) {
|
||||
*used = used.plus(piece_cost(&path));
|
||||
let mut p = piece(items);
|
||||
set_geometry(&mut p, path, rule);
|
||||
group.push(Arc::new(p));
|
||||
}
|
||||
let mut p = Node::group(leaf.id, group);
|
||||
transform_piece(&mut p, (ctx.seed, i, &kept), limit, used, doc);
|
||||
pieces.push(Arc::new(p));
|
||||
continue;
|
||||
}
|
||||
let (_, copies) = crate::distort::transform_map(tb, ¶ms, ctx.seed);
|
||||
let options = crate::distort::TransformOptions::of(¶ms);
|
||||
let map = crate::distort::transform_affine(tb, ¶ms, ctx.seed);
|
||||
@@ -936,7 +1301,7 @@ fn transform_item_leaf(leaf: &mut Node, max: Budget, used: &mut Budget) {
|
||||
set_geometry(&mut p, g.clone(), rule);
|
||||
let (made, _) = transform_leaf(&mut p, &g, map, copies, options, room);
|
||||
*used = used.plus(made);
|
||||
transform_piece(&mut p, ctx.seed, limit, used);
|
||||
transform_piece(&mut p, (ctx.seed, i, &kept), limit, used, doc);
|
||||
pieces.push(Arc::new(p));
|
||||
}
|
||||
if !plain.is_empty() {
|
||||
@@ -952,12 +1317,17 @@ fn transform_item_leaf(leaf: &mut Node, max: Budget, used: &mut Budget) {
|
||||
/// Apply the visible geometry effects left on the one fill or stroke of each path under `p` (a
|
||||
/// transformed piece of [`transform_item_leaf`] and its copies) to all of `p`'s paths as one piece:
|
||||
/// each effect takes the bounds of all the paths as its reference box and `seed`, as an object's
|
||||
/// own effects reshape all its paths ([`reshape_leaves_within`]). Each path applies its own copy of
|
||||
/// own effects reshape all its paths ([`reshape_leaves_carrying`]). Each path applies its own copy of
|
||||
/// the effect, whose distances its Scale Strokes & Effects scaled. A Transform effect with
|
||||
/// Transform Patterns or Scale Strokes & Effects on transforms each path as [`transform_leaf`] does.
|
||||
/// The geometry effects then leave the fills and strokes; their other effects stay. The paths keep
|
||||
/// to `max` together with the rest of the art (`used`).
|
||||
fn transform_piece(p: &mut Node, seed: u64, max: Budget, used: &mut Budget) {
|
||||
/// A live-effect plug-in that paints what it returns ([`painted_plugin`]) makes each path one path
|
||||
/// for each piece it returns ([`paint_piece`]). The geometry effects then leave the fills and
|
||||
/// strokes; their other effects stay. The paths keep to `max` together with the rest of the art
|
||||
/// (`used`), and the swatches and patterns painted pieces name must be in `doc`. `(seed, source,
|
||||
/// kept)`: the object's seed, the index in the object's appearance of the fill or stroke the
|
||||
/// piece was made from, and the index among that item's effects of each effect the piece kept,
|
||||
/// which a deferred live-effect plug-in is listed by.
|
||||
fn transform_piece(p: &mut Node, (seed, source, kept): (u64, usize, &[usize]), max: Budget, used: &mut Budget, doc: Option<&Document>) {
|
||||
let mut count = 0;
|
||||
for_each_leaf(p, &mut |l| {
|
||||
if let Some(item) = l.appearance.items.first() {
|
||||
@@ -973,6 +1343,13 @@ fn transform_piece(p: &mut Node, seed: u64, max: Budget, used: &mut Budget) {
|
||||
// What the other paths leave of the budget.
|
||||
let before = Budget::of(&path);
|
||||
let room = max.minus(used.minus(before));
|
||||
let stage = vectorcraft_plugins::deferred::Stage { object: seed, effect: kept.get(j).copied().unwrap_or(j), item: Some(source) };
|
||||
if let Some(plugin) = painted_plugin(&e) {
|
||||
if let Some(made) = paint_piece(leaf, &item, (plugin, &merged_params(&e.id, &e.params)), bounds, room, doc, Some(stage)) {
|
||||
*used = used.minus(before).plus(made);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if reaches_appearance(&e) {
|
||||
let params = merged_params(&e.id, &e.params);
|
||||
let (_, copies) = crate::distort::transform_map(bounds, ¶ms, seed);
|
||||
@@ -987,7 +1364,7 @@ fn transform_piece(p: &mut Node, seed: u64, max: Budget, used: &mut Budget) {
|
||||
AppearanceItem::Stroke(s) => Some(s),
|
||||
AppearanceItem::Fill(_) => None,
|
||||
};
|
||||
let ctx = GeomContext { stroke, rule, seed, appearance: Some(&appearance), item: Some(0) };
|
||||
let ctx = GeomContext { stroke, rule, seed, appearance: Some(&appearance), item: Some(0), doc, stage: Some(stage) };
|
||||
let mut carry = crate::Carry::default();
|
||||
let out = crate::geometry::apply_geometry_carrying_within(std::slice::from_ref(&e), &path, bounds, &ctx, room, Some(&mut carry));
|
||||
*used = used.minus(before).plus(Budget::of(&out));
|
||||
@@ -1262,6 +1639,8 @@ fn copy_placed(n: &mut Node, m: Affine, copies: usize, room: &mut usize) {
|
||||
placements.push(Arc::new(copy));
|
||||
}
|
||||
*room -= placements.len() - 1;
|
||||
// The object stays on top of its copies, as a path does.
|
||||
placements.reverse();
|
||||
let mut g = Node::group(src.id, placements);
|
||||
g.name = src.name.clone();
|
||||
*c = Arc::new(g);
|
||||
@@ -1276,14 +1655,21 @@ fn placed_count(n: &Node) -> usize {
|
||||
/// `n` with type anywhere inside it outlined and live objects evaluated, so geometry effects on
|
||||
/// it reach every member.
|
||||
pub(crate) fn outlined_members(doc: Option<&Document>, n: &Node) -> Node {
|
||||
outlined_members_with(doc, n, &outline_text, &SymbolNodes::default())
|
||||
outlined_members_with(doc, doc, n, &outline_text, &SymbolNodes::default())
|
||||
}
|
||||
|
||||
/// [`outlined_members`] with live objects and symbol instances given as art through `outline`
|
||||
/// ([`evaluated_with`]).
|
||||
pub(crate) fn outlined_members_with(doc: Option<&Document>, n: &Node, outline: OutlineHook, nodes: &SymbolNodes) -> Node {
|
||||
let mut art = evaluated_with(doc, n, outline, nodes);
|
||||
outline_nested_text(&mut art, false);
|
||||
/// ([`evaluated_with`]), with the symbols of the art from `symbols` and the swatches and patterns
|
||||
/// of painted pieces looked up in `paint_doc`.
|
||||
pub(crate) fn outlined_members_with(
|
||||
symbols: Option<&Document>,
|
||||
paint_doc: Option<&Document>,
|
||||
n: &Node,
|
||||
outline: OutlineHook,
|
||||
nodes: &SymbolNodes,
|
||||
) -> Node {
|
||||
let mut art = evaluated_with(symbols, n, outline, nodes);
|
||||
outline_nested_text(paint_doc, &mut art, false);
|
||||
art
|
||||
}
|
||||
|
||||
|
||||
+823
-59
File diff suppressed because it is too large.
Load diff
+253
-67
@@ -4,7 +4,7 @@ use std::sync::Arc;
|
||||
|
||||
use serde_json::Value;
|
||||
use vectorcraft_doc::color::{BlendMode, Paint};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Effect, Node, NodeKind, StrokeLayer};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Document, Effect, Node, NodeId, NodeKind, OpacityMask, StrokeLayer};
|
||||
use vectorcraft_geom::{FillRule, PathData, Rect};
|
||||
use vectorcraft_three_d::{Bevel, BevelShape, Extrude, Facet, Inflate, Mesh, View};
|
||||
|
||||
@@ -17,6 +17,9 @@ use crate::{GeomContext, apply_geometry_with, is_geometry};
|
||||
mod classic;
|
||||
pub use classic::{CLASSIC_EXTRUDE, SHADING_COLORS, classic_extrude_options, classic_shading};
|
||||
|
||||
mod source;
|
||||
pub(crate) use source::{resolve, resolve_for};
|
||||
|
||||
pub const EXTRUDE: &str = "threeD.extrude";
|
||||
pub const INFLATE: &str = "threeD.inflate";
|
||||
pub const ROTATE: &str = "threeD.rotate";
|
||||
@@ -138,7 +141,7 @@ pub fn inflate_options(p: &Value) -> Inflate {
|
||||
}
|
||||
|
||||
/// The shape a solid is made from: a path's or a compound path's outline.
|
||||
fn profile(n: &Node) -> Option<(PathData, FillRule)> {
|
||||
pub(crate) fn profile(n: &Node) -> Option<(PathData, FillRule)> {
|
||||
match &n.kind {
|
||||
NodeKind::Path { path, rule, guide: false, clipping: false, .. } => Some((path.clone(), *rule)),
|
||||
NodeKind::Compound { children, rule } => {
|
||||
@@ -156,7 +159,7 @@ fn profile(n: &Node) -> Option<(PathData, FillRule)> {
|
||||
|
||||
/// What a 3D effect paints `n` with: its own fills and strokes, else, for type that has none, the
|
||||
/// fill and stroke of its first painted run (its characters' colours).
|
||||
fn solid_paints(n: &Node) -> Vec<AppearanceItem> {
|
||||
pub(crate) fn solid_paints(n: &Node) -> Vec<AppearanceItem> {
|
||||
let own: Vec<AppearanceItem> = n.appearance.items.iter().filter(|i| crate::paints(i)).cloned().collect();
|
||||
if !own.is_empty() || !matches!(n.kind, NodeKind::Text(_)) {
|
||||
return own;
|
||||
@@ -198,38 +201,120 @@ fn validate_solid_params(id: &str, p: &Value) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The name an error gives 3D effect `id`.
|
||||
pub(crate) fn label_of(id: &str) -> &'static str {
|
||||
crate::effect_info(id).map_or("This effect", |e| e.label.trim_end_matches('…'))
|
||||
}
|
||||
|
||||
/// Validate before a command changes the document; the evaluator bounds loaded files too.
|
||||
pub fn validate_solid(n: &Node, id: &str, p: &Value) -> Result<(), String> {
|
||||
validate_solid_in(None, n, id, p)
|
||||
}
|
||||
|
||||
/// [`validate_solid`] with the symbols of `doc`: a symbol instance, a blend and a clipping group
|
||||
/// build from the art they show ([`resolve`]).
|
||||
pub fn validate_solid_in(doc: Option<&Document>, n: &Node, id: &str, p: &Value) -> Result<(), String> {
|
||||
validate_solid_params(id, p)?;
|
||||
let label = crate::effect_info(id).map_or("This effect", |e| e.label.trim_end_matches('…'));
|
||||
let label = label_of(id);
|
||||
let n = &*source::resolve_for(doc, n, label, id == ROTATE)?;
|
||||
if is_container(n) {
|
||||
let shapes = members(n, label)?;
|
||||
let b = union_bounds(&shapes).ok_or_else(|| format!("{label} requires a group holding a shape with at least two anchors"))?;
|
||||
return shapes.iter().try_for_each(|s| facets(id, &s.path, s.rule, b, p).map(|_| ()));
|
||||
}
|
||||
let (path, rule) = profile(n).ok_or_else(|| {
|
||||
format!("{label} currently requires a path, compound path, type or group. Convert images and other objects to paths first.")
|
||||
})?;
|
||||
let (path, rule) = object_profile(n, label)?;
|
||||
let b = path.bounds().ok_or_else(|| format!("{label} requires a shape with at least two anchors"))?;
|
||||
facets(id, &path, rule, b, p).map(|_| ())
|
||||
}
|
||||
|
||||
/// [`profile`], or the error a command reports for object `n`: type with no outlines to build from
|
||||
/// (empty, or only spaces) is a shape without anchors, an image is traced first, and any other
|
||||
/// object is the wrong kind.
|
||||
pub(crate) fn object_profile(n: &Node, label: &str) -> Result<(PathData, FillRule), String> {
|
||||
profile(n).ok_or_else(|| match n.kind {
|
||||
NodeKind::Text(_) => format!("{label} requires a shape with at least two anchors"),
|
||||
NodeKind::Image(_) => source::image_refusal(label),
|
||||
_ => format!("{label} currently requires a path, compound path, type, group, blend or symbol instance. Expand other objects first."),
|
||||
})
|
||||
}
|
||||
|
||||
/// A group in a 3D effect's group, or a member of it, whose opacity is below 100%, whose blending
|
||||
/// mode is not Normal or that has an opacity mask. The members of one group share its `key`.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) struct Scope {
|
||||
pub(crate) key: usize,
|
||||
pub(crate) opacity: f32,
|
||||
pub(crate) blend: BlendMode,
|
||||
/// The object's opacity mask, unless it has none or it is disabled. The mask art is in
|
||||
/// document coordinates, as the solids' faces are.
|
||||
pub(crate) mask: Option<Arc<OpacityMask>>,
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
/// The scope of `n` with key `key`, or `None` when `n` is opaque, blends normally and has no
|
||||
/// opacity mask that applies.
|
||||
fn of(n: &Node, key: usize) -> Option<Self> {
|
||||
let mask = n.extras().mask.as_deref().filter(|m| !m.disabled);
|
||||
(n.opacity < 1.0 || n.blend != BlendMode::Normal || mask.is_some()).then(|| Scope {
|
||||
key,
|
||||
opacity: n.opacity,
|
||||
blend: n.blend,
|
||||
mask: mask.map(|m| Arc::new(m.clone())),
|
||||
})
|
||||
}
|
||||
|
||||
/// A group of `children` with id `id` and this scope's opacity, blending mode and opacity mask.
|
||||
pub(crate) fn group(&self, id: NodeId, children: Vec<Arc<Node>>) -> Node {
|
||||
let mut group = Node::group(id, children);
|
||||
group.opacity = self.opacity;
|
||||
group.blend = self.blend;
|
||||
if let Some(mask) = &self.mask {
|
||||
group.extras_mut().mask = Some(Box::new((**mask).clone()));
|
||||
}
|
||||
group
|
||||
}
|
||||
}
|
||||
|
||||
/// `surfaces`, each with the scopes of the member it was built from, in groups with the opacity
|
||||
/// masks of those scopes, so each mask applies to the faces of the members it holds. The members'
|
||||
/// opacity is already in their paint ([`Profile::opacity`]), so these groups are opaque.
|
||||
fn masked(id: NodeId, surfaces: Vec<(&[Scope], Arc<Node>)>) -> Vec<Arc<Node>> {
|
||||
let masks: Vec<(Vec<Scope>, Arc<Node>)> = surfaces
|
||||
.into_iter()
|
||||
.map(|(scopes, s)| {
|
||||
let masks = scopes.iter().filter(|s| s.mask.is_some()).map(|s| Scope { opacity: 1.0, blend: BlendMode::Normal, ..s.clone() }).collect();
|
||||
(masks, s)
|
||||
})
|
||||
.collect();
|
||||
crate::revolve::scoped(id, masks.iter().map(|(scopes, s)| (scopes.as_slice(), s.clone())).collect(), 0)
|
||||
}
|
||||
|
||||
/// Does any of `shapes` sit in a scope with an opacity mask?
|
||||
fn has_masks(shapes: &[Profile]) -> bool {
|
||||
shapes.iter().any(|s| s.scopes.iter().any(|s| s.mask.is_some()))
|
||||
}
|
||||
|
||||
/// One shape a solid is built from: its outline and fill rule, the fills and strokes that paint
|
||||
/// it, and the opacity of the objects it is in (1 for the object with the effect itself).
|
||||
struct Profile {
|
||||
path: PathData,
|
||||
rule: FillRule,
|
||||
items: Vec<AppearanceItem>,
|
||||
opacity: f32,
|
||||
/// it, and the opacity of the objects it is in multiplied together (1 for the object with the
|
||||
/// effect itself). `scopes` lists those objects with transparency or an opacity mask of their own
|
||||
/// ([`Scope`]), from the outermost group down to the member itself (none for the object itself).
|
||||
/// Group Revolve applies each once, to the surfaces of the members it holds together; the other 3D
|
||||
/// effects apply each opacity mask that way.
|
||||
pub(crate) struct Profile {
|
||||
pub(crate) path: PathData,
|
||||
pub(crate) rule: FillRule,
|
||||
pub(crate) items: Vec<AppearanceItem>,
|
||||
pub(crate) opacity: f32,
|
||||
pub(crate) scopes: Vec<Scope>,
|
||||
}
|
||||
|
||||
/// Is `n` a group or layer (a 3D effect on it turns its members about one shared axis)?
|
||||
fn is_container(n: &Node) -> bool {
|
||||
pub(crate) fn is_container(n: &Node) -> bool {
|
||||
matches!(n.kind, NodeKind::Group { clip: false, .. } | NodeKind::Layer { .. })
|
||||
}
|
||||
|
||||
/// The bounds of every shape together: the centre a group's members turn about.
|
||||
fn union_bounds(shapes: &[Profile]) -> Option<Rect> {
|
||||
pub(crate) fn union_bounds(shapes: &[Profile]) -> Option<Rect> {
|
||||
shapes.iter().filter_map(|s| s.path.bounds()).reduce(|a, c| a.union(c))
|
||||
}
|
||||
|
||||
@@ -241,51 +326,95 @@ const MAX_MEMBERS: usize = 4096;
|
||||
const MAX_MESH_TRIANGLES: usize = 2_000_000;
|
||||
|
||||
/// The shapes a 3D effect on group or layer `n` builds its solids from: every visible path,
|
||||
/// compound path and type in it, at any depth of its groups, each through its own geometry
|
||||
/// effects and painted with its own fills and strokes. Like the effect on a group in the
|
||||
/// compound path and type in it (after [`resolve`] gives it the art of its symbol instances, blends
|
||||
/// and clipping groups), at any depth of its groups, each through its own geometry
|
||||
/// effects, then those of each group it is in (on that group's members together), and painted
|
||||
/// with its own fills and strokes. Like the effect on a group in the
|
||||
/// reference app, they turn about one shared axis (the centre of their bounds together) rather
|
||||
/// than each about its own. An error names what the group holds that can't become a solid.
|
||||
fn members(n: &Node, label: &str) -> Result<Vec<Profile>, String> {
|
||||
fn walk(n: &Node, opacity: f32, label: &str, out: &mut Vec<Profile>) -> Result<(), String> {
|
||||
pub(crate) fn members(n: &Node, label: &str) -> Result<Vec<Profile>, String> {
|
||||
struct Walk<'a> {
|
||||
label: &'a str,
|
||||
out: Vec<Profile>,
|
||||
keys: usize,
|
||||
}
|
||||
fn walk(n: &Node, opacity: f32, scopes: &[Scope], w: &mut Walk) -> Result<(), String> {
|
||||
let label = w.label;
|
||||
for c in n.children().into_iter().flatten() {
|
||||
if !c.visible || matches!(c.kind, NodeKind::Path { guide: true, .. }) {
|
||||
continue;
|
||||
}
|
||||
let opacity = opacity * c.opacity;
|
||||
let mut scopes = scopes.to_vec();
|
||||
if let Some(scope) = Scope::of(c, w.keys + 1) {
|
||||
w.keys += 1;
|
||||
scopes.push(scope);
|
||||
}
|
||||
if is_container(c) {
|
||||
walk(c, opacity, label, out)?;
|
||||
let first = w.out.len();
|
||||
walk(c, opacity, &scopes, w)?;
|
||||
// The group's own geometry effects reshape its members together, as it is drawn.
|
||||
if let Some(shapes) = w.out.get_mut(first..) {
|
||||
reshape_members(c, &c.appearance.effects, shapes);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let Some((path, rule)) = profile(c) else {
|
||||
let what = match &c.kind {
|
||||
NodeKind::Group { clip: true, .. } => "clipping groups",
|
||||
NodeKind::Image(_) => "images",
|
||||
NodeKind::SymbolInstance { .. } => "symbol instances",
|
||||
NodeKind::Image(_) => return Err(source::image_refusal(label)),
|
||||
NodeKind::Path { clipping: true, .. } => "clipping paths",
|
||||
_ => "objects other than paths, compound paths and type",
|
||||
_ => "other objects",
|
||||
};
|
||||
return Err(format!("{label} on a group needs paths, compound paths and type: release or expand its {what} first."));
|
||||
return Err(format!(
|
||||
"{label} on a group needs paths, compound paths, type, groups, clipping groups, blends and symbol instances: release or expand its {what} first."
|
||||
));
|
||||
};
|
||||
if out.len() >= MAX_MEMBERS {
|
||||
if w.out.len() >= MAX_MEMBERS {
|
||||
return Err(format!("{label} on a group takes at most {MAX_MEMBERS} objects"));
|
||||
}
|
||||
let path = geometry(&path, &c.appearance.effects, &GeomContext::of(c));
|
||||
if path.bounds().is_some() {
|
||||
out.push(Profile { path, rule, items: solid_paints(c), opacity });
|
||||
w.out.push(Profile { path, rule, items: solid_paints(c), opacity, scopes });
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
let mut out = vec![];
|
||||
walk(n, 1.0, label, &mut out)?;
|
||||
Ok(out)
|
||||
let mut w = Walk { label, out: vec![], keys: 0 };
|
||||
walk(n, 1.0, &[], &mut w)?;
|
||||
Ok(w.out)
|
||||
}
|
||||
|
||||
/// Can 3D effect `id` (Revolve included, [`is_three_d`]) with parameters `p` draw `n`? Checked
|
||||
/// before a command changes the document: Revolve by [`crate::validate_revolve`], the others by
|
||||
/// [`validate_solid`].
|
||||
pub fn validate_three_d(n: &Node, id: &str, p: &Value) -> Result<(), String> {
|
||||
if id == crate::REVOLVE { crate::validate_revolve(n, p) } else { validate_solid(n, id, p) }
|
||||
validate_three_d_in(None, n, id, p)
|
||||
}
|
||||
|
||||
/// [`validate_three_d`] with the symbols of `doc` ([`validate_solid_in`]).
|
||||
pub fn validate_three_d_in(doc: Option<&Document>, n: &Node, id: &str, p: &Value) -> Result<(), String> {
|
||||
if id == crate::REVOLVE { crate::revolve::validate_revolve_in(doc, n, p) } else { validate_solid_in(doc, n, id, p) }
|
||||
}
|
||||
|
||||
/// [`validate_three_d`] for an effect at `(item, index)`, index `index` of the effects of
|
||||
/// appearance item `item` of `n` (the effects' length for a new effect), or on the object itself
|
||||
/// when `at` is `None`. A Revolve on a fill or stroke is checked in that item's paint, after the
|
||||
/// item's geometry effects before it, together with the object's other items that carry a Revolve.
|
||||
/// A group's or layer's fills and strokes take no 3D effect: only the group's own effect draws.
|
||||
pub fn validate_three_d_at(n: &Node, id: &str, p: &Value, at: Option<(usize, usize)>) -> Result<(), String> {
|
||||
validate_three_d_at_in(None, n, id, p, at)
|
||||
}
|
||||
|
||||
/// [`validate_three_d_at`] with the symbols of `doc` ([`validate_solid_in`]). A symbol instance, a
|
||||
/// blend and a clipping group take a 3D effect as groups do: on the object itself.
|
||||
pub fn validate_three_d_at_in(doc: Option<&Document>, n: &Node, id: &str, p: &Value, at: Option<(usize, usize)>) -> Result<(), String> {
|
||||
match at {
|
||||
Some(_) if is_container(n) || source::resolves(n) => {
|
||||
Err("A 3D effect on a group or layer goes on the group itself, not on its fill or stroke".into())
|
||||
}
|
||||
Some((item, index)) if id == crate::REVOLVE => crate::revolve::validate_item_revolve(n, p, item, index),
|
||||
_ => validate_three_d_in(doc, n, id, p),
|
||||
}
|
||||
}
|
||||
|
||||
/// Are 3D effect `id`'s parameter values `p` valid? The checks of [`validate_three_d`] that do not
|
||||
@@ -355,6 +484,21 @@ fn paint_of(item: &AppearanceItem, member: f32) -> (Paint, f32, BlendMode) {
|
||||
}
|
||||
}
|
||||
|
||||
/// `item`'s paint, borrowed (for checks that must not copy it).
|
||||
fn paint_ref(item: &AppearanceItem) -> &Paint {
|
||||
match item {
|
||||
AppearanceItem::Fill(f) => &f.paint,
|
||||
AppearanceItem::Stroke(s) => &s.paint,
|
||||
}
|
||||
}
|
||||
|
||||
/// Paint copies `surface` makes for `facets`: two per facet (fill and seam stroke), six when colored
|
||||
/// lights give each facet derived paints (`lighting::solid_surface`).
|
||||
fn facet_copies(facets: &[Facet], paint: &Paint, colored: bool) -> usize {
|
||||
let derived = colored && paint.color().is_some() && facets.iter().any(|f| f.rgb_gradient.is_some());
|
||||
facets.len().saturating_mul(if derived { 6 } else { 2 })
|
||||
}
|
||||
|
||||
/// The area `item` paints on `path`, and the rule that fills it: the path itself for a fill, the
|
||||
/// stroke's outline (whose contours nest as its holes do) for a stroke.
|
||||
fn region(path: &PathData, rule: FillRule, item: &AppearanceItem) -> (PathData, FillRule) {
|
||||
@@ -364,28 +508,32 @@ fn region(path: &PathData, rule: FillRule, item: &AppearanceItem) -> (PathData,
|
||||
}
|
||||
}
|
||||
|
||||
/// One part of [`solid_parts`]: a fill or stroke, its shape's opacity, the region it paints and its
|
||||
/// fill rule, and its shape's scopes.
|
||||
type Part<'a> = (&'a AppearanceItem, f32, PathData, FillRule, &'a [Scope]);
|
||||
|
||||
/// The parts Extrude & Bevel and Inflate build solids from: each shape's top fill and its strokes,
|
||||
/// a group's members from the back up, each with its shape's opacity and the region it paints.
|
||||
/// The solids share the bounds of all their parts (`fallback` when none has any): one center to
|
||||
/// turn, twist and taper about, and Inflate's grid reaches over the strokes' outlines.
|
||||
fn solid_parts(shapes: &[Profile], fallback: Rect) -> (Vec<(&AppearanceItem, f32, PathData, FillRule)>, Rect) {
|
||||
let parts: Vec<(&AppearanceItem, f32, PathData, FillRule)> = shapes
|
||||
/// a group's members from the back up, each with its shape's opacity, the region it paints and its
|
||||
/// shape's scopes. The solids share the bounds of all their parts (`fallback` when none has any):
|
||||
/// one center to turn, twist and taper about, and Inflate's grid reaches over the strokes' outlines.
|
||||
fn solid_parts(shapes: &[Profile], fallback: Rect) -> (Vec<Part<'_>>, Rect) {
|
||||
let parts: Vec<Part> = shapes
|
||||
.iter()
|
||||
.flat_map(|s| {
|
||||
let top_fill = s.items.iter().rposition(|i| matches!(i, AppearanceItem::Fill(_)));
|
||||
s.items.iter().enumerate().filter(move |(k, i)| Some(*k) == top_fill || matches!(i, AppearanceItem::Stroke(_))).map(|(_, i)| {
|
||||
let (area, rule) = region(&s.path, s.rule, i);
|
||||
(i, s.opacity, area, rule)
|
||||
(i, s.opacity, area, rule, s.scopes.as_slice())
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let bounds = parts.iter().filter_map(|(_, _, area, _)| area.bounds()).reduce(|a, c| a.union(c)).unwrap_or(fallback);
|
||||
let bounds = parts.iter().filter_map(|(_, _, area, _, _)| area.bounds()).reduce(|a, c| a.union(c)).unwrap_or(fallback);
|
||||
(parts, bounds)
|
||||
}
|
||||
|
||||
/// `shapes`, the members of group or layer `n`, reshaped by the visible geometry effects `before`
|
||||
/// its 3D effect, about the bounds of all of them.
|
||||
fn reshape_members(n: &Node, before: &[Effect], shapes: &mut [Profile]) {
|
||||
pub(crate) fn reshape_members(n: &Node, before: &[Effect], shapes: &mut [Profile]) {
|
||||
if before.iter().any(|e| e.visible && is_geometry(&e.id))
|
||||
&& let Some(b) = union_bounds(shapes)
|
||||
{
|
||||
@@ -402,12 +550,16 @@ fn reshape_members(n: &Node, before: &[Effect], shapes: &mut [Profile]) {
|
||||
/// each in its own paint. When every one is opaque with normal blending and no colored light makes
|
||||
/// a surface one composite or masks it, their faces are drawn in one back-to-front order (where
|
||||
/// depths tie, as for the caps turned toward the viewer, in the order of the appearance);
|
||||
/// otherwise each solid is a surface of its own, in that order.
|
||||
/// otherwise each solid is a surface of its own, in that order. A member's opacity mask, or that of
|
||||
/// a group it is in, masks the surfaces of the members it holds ([`masked`]).
|
||||
fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>> {
|
||||
let b = union_bounds(shapes)?;
|
||||
let p = crate::merged_params(&fx.id, &fx.params);
|
||||
let classic = fx.id == CLASSIC_EXTRUDE;
|
||||
let colored = !classic && view_options(&p).has_colored_lights();
|
||||
let masks = has_masks(shapes);
|
||||
// The paint copies of every solid together: past the allowance the source path is drawn.
|
||||
let mut paint_bytes = 0usize;
|
||||
if fx.id == ROTATE {
|
||||
// Every part turns about the centre of the object's own bounds (a group's members about
|
||||
// the centre of theirs), so they stay together.
|
||||
@@ -417,24 +569,28 @@ fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>>
|
||||
let (paint, opacity, blend) = paint_of(item, s.opacity);
|
||||
let (area, rule) = region(&s.path, s.rule, item);
|
||||
let f = facets(ROTATE, &area, rule, b, &p).ok()?;
|
||||
out.push(Arc::new(surface(n, f, &paint, opacity, blend, colored)));
|
||||
if !crate::revolve::paint_copies_add(&mut paint_bytes, facet_copies(&f, &paint, colored), &paint) {
|
||||
return None;
|
||||
}
|
||||
out.push((s.scopes.as_slice(), Arc::new(surface(n, f, &paint, opacity, blend, colored))));
|
||||
}
|
||||
}
|
||||
return Some(out);
|
||||
return Some(if masks { masked(n.id, out) } else { out.into_iter().map(|(_, s)| s).collect() });
|
||||
}
|
||||
let (parts, bounds) = solid_parts(shapes, b);
|
||||
let built = if fx.id == INFLATE {
|
||||
// One eye for every puff, as far away as the highest needs.
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule)| (area, *rule)).collect();
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule, _)| (area, *rule)).collect();
|
||||
vectorcraft_three_d::inflate_together(&shapes, bounds, inflate_options(&p), view_options(&p))
|
||||
} else {
|
||||
parts.iter().map(|(_, _, area, rule)| facets(&fx.id, area, *rule, bounds, &p)).collect()
|
||||
parts.iter().map(|(_, _, area, rule, _)| facets(&fx.id, area, *rule, bounds, &p)).collect()
|
||||
};
|
||||
let mut solids = vec![];
|
||||
for ((item, opacity, _, _), f) in parts.iter().zip(built) {
|
||||
for ((item, opacity, _, _, scopes), f) in parts.iter().zip(built) {
|
||||
let item: &AppearanceItem = item;
|
||||
match f {
|
||||
Ok(f) => solids.push((item, *opacity, f)),
|
||||
Ok(f) if !crate::revolve::paint_copies_add(&mut paint_bytes, facet_copies(&f, paint_ref(item), colored), paint_ref(item)) => return None,
|
||||
Ok(f) => solids.push((item, *opacity, *scopes, f)),
|
||||
// A stroke's outline past the limits is left out; the fill's solid still draws.
|
||||
Err(_) if matches!(item, AppearanceItem::Stroke(_)) => {}
|
||||
// A loaded file or a shape edited past the limits keeps drawing its source path
|
||||
@@ -446,30 +602,33 @@ fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>>
|
||||
if solids.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let opaque = solids.iter().all(|(item, member, _)| {
|
||||
let opaque = solids.iter().all(|(item, member, _, _)| {
|
||||
let (_, opacity, blend) = paint_of(item, *member);
|
||||
opacity >= 1.0 && blend == BlendMode::Normal
|
||||
});
|
||||
let surfaces: Vec<(Vec<f64>, Node)> = solids
|
||||
let surfaces: Vec<(Vec<f64>, Node, &[Scope])> = solids
|
||||
.into_iter()
|
||||
.map(|(item, member, f)| {
|
||||
.map(|(item, member, scopes, f)| {
|
||||
let depths: Vec<f64> = f.iter().map(|f| f.depth).collect();
|
||||
let (paint, opacity, blend) = paint_of(item, member);
|
||||
let art = if classic { classic::surface(n, f, &paint, opacity, blend, &p) } else { surface(n, f, &paint, opacity, blend, colored) };
|
||||
(depths, art)
|
||||
(depths, art, scopes)
|
||||
})
|
||||
.collect();
|
||||
if masks {
|
||||
return Some(masked(n.id, surfaces.into_iter().map(|(_, s, scopes)| (scopes, Arc::new(s))).collect()));
|
||||
}
|
||||
// One order takes a plain group of faces from each solid, one face for each facet. With
|
||||
// colored lights a surface can instead be one clipped composite of all its facets, or carry an
|
||||
// opacity mask, and then it stays whole.
|
||||
let plain = surfaces.iter().all(|(depths, s)| {
|
||||
let plain = surfaces.iter().all(|(depths, s, _)| {
|
||||
s.extras().mask.is_none() && matches!(&s.kind, NodeKind::Group { children, clip: false } if children.len() == depths.len())
|
||||
});
|
||||
if !opaque || !plain {
|
||||
return Some(surfaces.into_iter().map(|(_, s)| Arc::new(s)).collect());
|
||||
return Some(surfaces.into_iter().map(|(_, s, _)| Arc::new(s)).collect());
|
||||
}
|
||||
let mut faces = vec![];
|
||||
for (depths, s) in surfaces {
|
||||
for (depths, s, _) in surfaces {
|
||||
if let NodeKind::Group { children, .. } = s.kind {
|
||||
faces.extend(depths.into_iter().zip(children));
|
||||
}
|
||||
@@ -488,9 +647,17 @@ fn geometry(path: &PathData, fx: &[Effect], ctx: &GeomContext) -> PathData {
|
||||
/// On a group or layer the effect builds a solid from each path, compound path and type in it,
|
||||
/// all turned about one shared axis ([`members`]).
|
||||
pub fn solid_art(n: &Node) -> Option<Node> {
|
||||
solid_art_in(None, n)
|
||||
}
|
||||
|
||||
/// [`solid_art`] with the symbols of `doc`: a symbol instance, a blend and a clipping group, alone
|
||||
/// or in a group, build from the art they show ([`resolve`]). `None` too when that art can't be
|
||||
/// resolved.
|
||||
pub fn solid_art_in(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
if !has_solid(n) {
|
||||
return None;
|
||||
}
|
||||
let n = &*resolve(doc, n, "").ok()?;
|
||||
if is_container(n) {
|
||||
return group_solid_art(n);
|
||||
}
|
||||
@@ -501,7 +668,7 @@ pub fn solid_art(n: &Node) -> Option<Node> {
|
||||
let remaining: Vec<Effect>;
|
||||
if let Some((index, fx)) = solid(&n.appearance.effects) {
|
||||
let path = geometry(&path, n.appearance.effects.get(..index)?, &ctx);
|
||||
children.extend(art_for(n, fx, &[Profile { path, rule, items: paints, opacity: 1.0 }])?);
|
||||
children.extend(art_for(n, fx, &[Profile { path, rule, items: paints, opacity: 1.0, scopes: vec![] }])?);
|
||||
// Later geometry effects act on the projected art as a whole.
|
||||
remaining = n
|
||||
.appearance
|
||||
@@ -517,7 +684,7 @@ pub fn solid_art(n: &Node) -> Option<Node> {
|
||||
for item in &paints {
|
||||
if let Some((index, fx)) = solid(item.effects()) {
|
||||
let p = geometry(&path, item.effects().get(..index)?, &ctx.item(item));
|
||||
for mut art in art_for(n, fx, &[Profile { path: p, rule, items: vec![item.clone()], opacity: 1.0 }])? {
|
||||
for mut art in art_for(n, fx, &[Profile { path: p, rule, items: vec![item.clone()], opacity: 1.0, scopes: vec![] }])? {
|
||||
Arc::make_mut(&mut art).appearance.effects =
|
||||
item.effects().iter().skip(index + 1).filter(|e| !is_three_d(&e.id)).cloned().collect();
|
||||
children.push(art);
|
||||
@@ -613,11 +780,11 @@ fn meshes_for(fx: &Effect, shapes: &[Profile], total: &mut usize, limit: usize)
|
||||
// Inflate lays out every puff at once (they share one eye); each Inflate and Extrude & Bevel
|
||||
// solid and its mesh are built as the loop reaches them.
|
||||
let mut inflated = (fx.id == INFLATE).then(|| {
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule)| (area, *rule)).collect();
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule, _)| (area, *rule)).collect();
|
||||
vectorcraft_three_d::inflate_meshes(&shapes, bounds, inflate_options(&p), view)
|
||||
});
|
||||
let extrude = extrude_options(&p);
|
||||
for (item, member, area, rule) in &parts {
|
||||
for (item, member, area, rule, _) in &parts {
|
||||
let item: &AppearanceItem = item;
|
||||
let mesh = match inflated.as_mut() {
|
||||
Some(built) => built.next().unwrap_or_else(|| Ok(Mesh::default())),
|
||||
@@ -642,28 +809,44 @@ fn meshes_for(fx: &Effect, shapes: &[Profile], total: &mut usize, limit: usize)
|
||||
/// such effect, its solids can't be built, none has a triangle, or together they have more than
|
||||
/// [`MAX_MESH_TRIANGLES`].
|
||||
pub fn three_d_meshes(n: &Node) -> Result<Vec<SolidMesh>, String> {
|
||||
meshes_within(n, &mut 0, MAX_MESH_TRIANGLES)
|
||||
three_d_meshes_in(None, n)
|
||||
}
|
||||
|
||||
/// [`three_d_meshes`] with the symbols of `doc`: a symbol instance, a blend and a clipping group
|
||||
/// build from the art they show ([`resolve`]).
|
||||
pub fn three_d_meshes_in(doc: Option<&Document>, n: &Node) -> Result<Vec<SolidMesh>, String> {
|
||||
meshes_within(doc, n, &mut 0, MAX_MESH_TRIANGLES)
|
||||
}
|
||||
|
||||
/// The meshes of every object in `nodes` ([`three_d_meshes`]), in order, as one 3D asset exports
|
||||
/// them: the objects share one budget of [`MAX_MESH_TRIANGLES`].
|
||||
pub fn three_d_meshes_of<'a>(nodes: impl IntoIterator<Item = &'a Node>) -> Result<Vec<SolidMesh>, String> {
|
||||
meshes_of_within(nodes, MAX_MESH_TRIANGLES)
|
||||
three_d_meshes_of_in(None, nodes)
|
||||
}
|
||||
|
||||
/// [`three_d_meshes_of`] with a budget of `limit` triangles.
|
||||
fn meshes_of_within<'a>(nodes: impl IntoIterator<Item = &'a Node>, limit: usize) -> Result<Vec<SolidMesh>, String> {
|
||||
/// [`three_d_meshes_of`] with the symbols of `doc` ([`three_d_meshes_in`]).
|
||||
pub fn three_d_meshes_of_in<'a>(doc: Option<&Document>, nodes: impl IntoIterator<Item = &'a Node>) -> Result<Vec<SolidMesh>, String> {
|
||||
meshes_of_within(doc, nodes, MAX_MESH_TRIANGLES)
|
||||
}
|
||||
|
||||
/// [`three_d_meshes_of_in`] with a budget of `limit` triangles.
|
||||
fn meshes_of_within<'a>(doc: Option<&Document>, nodes: impl IntoIterator<Item = &'a Node>, limit: usize) -> Result<Vec<SolidMesh>, String> {
|
||||
let mut total = 0usize;
|
||||
let mut out = vec![];
|
||||
for n in nodes {
|
||||
out.extend(meshes_within(n, &mut total, limit)?);
|
||||
out.extend(meshes_within(doc, n, &mut total, limit)?);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// [`three_d_meshes`], with the triangles of every solid added to `total` as it is built and an
|
||||
/// [`three_d_meshes_in`], with the triangles of every solid added to `total` as it is built and an
|
||||
/// error as soon as `total` passes `limit`.
|
||||
fn meshes_within(n: &Node, total: &mut usize, limit: usize) -> Result<Vec<SolidMesh>, String> {
|
||||
fn meshes_within(doc: Option<&Document>, n: &Node, total: &mut usize, limit: usize) -> Result<Vec<SolidMesh>, String> {
|
||||
if !crate::has_revolve(n) && !has_solid(n) {
|
||||
return Err("the object has no 3D effect (Extrude & Bevel, Revolve, Inflate or Rotate)".into());
|
||||
}
|
||||
let label = n.appearance.effects.iter().find(|e| e.visible && is_three_d(&e.id)).map_or("This effect", |e| label_of(&e.id));
|
||||
let n = &*resolve(doc, n, label)?;
|
||||
let meshes = if crate::has_revolve(n) {
|
||||
crate::revolve::revolve_meshes(n, total, limit)?
|
||||
} else if !has_solid(n) {
|
||||
@@ -675,19 +858,19 @@ fn meshes_within(n: &Node, total: &mut usize, limit: usize) -> Result<Vec<SolidM
|
||||
reshape_members(n, n.appearance.effects.get(..index).unwrap_or_default(), &mut shapes);
|
||||
meshes_for(fx, &shapes, total, limit)?
|
||||
} else {
|
||||
let (path, rule) = profile(n).ok_or("3D export needs a path, compound path, type or group")?;
|
||||
let (path, rule) = object_profile(n, label)?;
|
||||
let ctx = GeomContext::of(n);
|
||||
let paints = solid_paints(n);
|
||||
if let Some((index, fx)) = solid(&n.appearance.effects) {
|
||||
let path = geometry(&path, n.appearance.effects.get(..index).unwrap_or_default(), &ctx);
|
||||
meshes_for(fx, &[Profile { path, rule, items: paints, opacity: 1.0 }], total, limit)?
|
||||
meshes_for(fx, &[Profile { path, rule, items: paints, opacity: 1.0, scopes: vec![] }], total, limit)?
|
||||
} else {
|
||||
let path = geometry(&path, &n.appearance.effects, &ctx);
|
||||
let mut out = vec![];
|
||||
for item in &paints {
|
||||
if let Some((index, fx)) = solid(item.effects()) {
|
||||
let p = geometry(&path, item.effects().get(..index).unwrap_or_default(), &ctx.item(item));
|
||||
out.extend(meshes_for(fx, &[Profile { path: p, rule, items: vec![item.clone()], opacity: 1.0 }], total, limit)?);
|
||||
out.extend(meshes_for(fx, &[Profile { path: p, rule, items: vec![item.clone()], opacity: 1.0, scopes: vec![] }], total, limit)?);
|
||||
}
|
||||
}
|
||||
out
|
||||
@@ -712,3 +895,6 @@ mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_mesh;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_sources;
|
||||
@@ -0,0 +1,455 @@
|
||||
//! The art a 3D effect builds its solids from when its object, or a member of its group, shows art
|
||||
//! it does not hold as paths: a symbol instance its symbol's art placed by the instance, a blend its
|
||||
//! key objects and steps, a clipping group (or a layer with a clipping mask) its art inside the
|
||||
//! clipping path. Each becomes a plain group of that art, keeping its transparency, so the solids
|
||||
//! build from what the object shows. Images build no solid: they are traced first.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::sync::Arc;
|
||||
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Document, FillLayer, Node, NodeKind};
|
||||
use vectorcraft_geom::{FillRule, PathData, Rect};
|
||||
use vectorcraft_pathops as po;
|
||||
|
||||
use super::{MAX_MEMBERS, Scope, geometry, is_container, members, profile, region, reshape_members, solid_paints};
|
||||
use crate::{GeomContext, is_geometry};
|
||||
|
||||
/// The deepest nesting of symbol instances, blends and clipping groups resolved for one 3D effect.
|
||||
const MAX_DEPTH: usize = 64;
|
||||
|
||||
/// The most objects resolving one 3D effect's art may visit or make: every object listed (hidden
|
||||
/// ones and guides included), every symbol instance, blend and clipping group resolved, every blend
|
||||
/// step made and every object cut to a clipping path. A symbol that holds instances of another
|
||||
/// repeats that symbol's art at every level, so art with few visible objects can still take this
|
||||
/// many steps.
|
||||
pub(super) const MAX_WORK: usize = 1 << 18;
|
||||
|
||||
/// The error for an image a 3D effect is asked to build from.
|
||||
pub(crate) fn image_refusal(label: &str) -> String {
|
||||
format!("{label} can't build a solid from an image: trace it first with Object › Image Trace › Make.")
|
||||
}
|
||||
|
||||
/// Is `n` a symbol instance, a blend or a clipping group or layer?
|
||||
pub(crate) fn resolves(n: &Node) -> bool {
|
||||
matches!(n.kind, NodeKind::SymbolInstance { .. } | NodeKind::Blend { .. }) || n.clips()
|
||||
}
|
||||
|
||||
/// Is `n`, or a visible member of `n` at any depth of its groups and layers, one [`resolves`] takes?
|
||||
fn needs(n: &Node) -> bool {
|
||||
let mut stack = vec![n];
|
||||
while let Some(n) = stack.pop() {
|
||||
if resolves(n) {
|
||||
return true;
|
||||
}
|
||||
if is_container(n) {
|
||||
stack.extend(kids(n).iter().filter(|c| c.visible).map(|c| &**c));
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// `n` as a 3D effect builds from it: unchanged when neither it nor a visible member of its groups
|
||||
/// is a symbol instance, a blend or a clipping group, otherwise a group with `n`'s id, appearance
|
||||
/// and transparency holding that art (a symbol instance's symbol art from `doc`). An error names
|
||||
/// what can't be resolved: an image, a missing symbol, art nested or numbering past the limits, or
|
||||
/// a clipping path too complex to cut the art with. `label` is the effect's name. The art is cut
|
||||
/// for the solid effect `n` carries ([`resolve_for`]).
|
||||
pub(crate) fn resolve<'a>(doc: Option<&Document>, n: &'a Node, label: &str) -> Result<Cow<'a, Node>, String> {
|
||||
let every_fill = super::solid(&n.appearance.effects).is_some_and(|(_, fx)| fx.id == super::ROTATE);
|
||||
resolve_for(doc, n, label, every_fill)
|
||||
}
|
||||
|
||||
/// [`resolve`] for the 3D effect that builds from `n`: `every_fill` for Rotate, which turns every
|
||||
/// fill, so each fill a clipping path cuts keeps its own place among the strokes. Otherwise the
|
||||
/// effect builds from the top fill, and the fills cut go together in the top fill's place.
|
||||
pub(crate) fn resolve_for<'a>(doc: Option<&Document>, n: &'a Node, label: &str, every_fill: bool) -> Result<Cow<'a, Node>, String> {
|
||||
if !needs(n) {
|
||||
return Ok(Cow::Borrowed(n));
|
||||
}
|
||||
let mut r = Resolver { doc, label, every_fill, leaves: 0, work: 0, shown: None };
|
||||
let children = r.content(n, 0);
|
||||
#[cfg(test)]
|
||||
WORK.with(|w| w.set(r.work));
|
||||
let mut out = n.clone();
|
||||
out.kind = NodeKind::Group { children: children?, clip: false };
|
||||
Ok(Cow::Owned(out))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// The objects the last [`resolve`] on this thread visited or made ([`Resolver::work`]).
|
||||
pub(super) static WORK: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
struct Resolver<'a> {
|
||||
doc: Option<&'a Document>,
|
||||
label: &'a str,
|
||||
/// Does each fill a clipping path cuts become a piece of its own ([`resolve_for`])?
|
||||
every_fill: bool,
|
||||
/// The objects other than groups and layers kept so far, counted against [`MAX_MEMBERS`].
|
||||
leaves: usize,
|
||||
/// The objects visited or made so far, counted against [`MAX_WORK`].
|
||||
work: usize,
|
||||
/// Inside clipping groups, the box of what their clipping paths show together.
|
||||
shown: Option<Rect>,
|
||||
}
|
||||
|
||||
impl Resolver<'_> {
|
||||
/// Count `steps` more objects visited or made, or an error past [`MAX_WORK`].
|
||||
fn spend(&mut self, steps: usize) -> Result<(), String> {
|
||||
self.work = self.work.saturating_add(steps);
|
||||
if self.work > MAX_WORK {
|
||||
return Err(self.too_much_work());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn too_much_work(&self) -> String {
|
||||
format!("{} can't build solids from this art: its symbol instances, blends and clipping groups hold too many objects", self.label)
|
||||
}
|
||||
|
||||
/// `f` run on the art of `c`. The effects of an object or group can move its art into a clip,
|
||||
/// so inside one with effects no image is left out for lying outside the clip ([`Self::shown`]).
|
||||
fn inside<T>(&mut self, c: &Node, f: impl FnOnce(&mut Self) -> Result<T, String>) -> Result<T, String> {
|
||||
let outer = self.shown;
|
||||
if has_effects(c) {
|
||||
self.shown = None;
|
||||
}
|
||||
let out = f(self);
|
||||
self.shown = outer;
|
||||
out
|
||||
}
|
||||
|
||||
/// The art `n`, a symbol instance, a blend, a clipping group or layer, or a group holding one,
|
||||
/// shows, as the members of a plain group.
|
||||
fn content(&mut self, n: &Node, depth: usize) -> Result<Vec<Arc<Node>>, String> {
|
||||
let label = self.label;
|
||||
if depth > MAX_DEPTH {
|
||||
return Err(self.too_deep());
|
||||
}
|
||||
self.spend(1)?;
|
||||
match &n.kind {
|
||||
NodeKind::SymbolInstance { symbol, xf } => {
|
||||
let missing = || format!("{label} can't build a solid from the instance of symbol \"{symbol}\": the symbol's art isn't available");
|
||||
let doc = self.doc.ok_or_else(missing)?;
|
||||
let sym = doc.symbols.iter().find(|s| s.name == *symbol).ok_or_else(missing)?;
|
||||
// Counted before the art is placed, so a symbol past the limit is never copied:
|
||||
// first its own objects, which the instance's overrides keep, then the most
|
||||
// objects placing makes, with those overrides and the symbol's 9-slice guides.
|
||||
if self.work.saturating_add(sym.art.count_with_masks()) > MAX_WORK {
|
||||
return Err(self.too_much_work());
|
||||
}
|
||||
let art = vectorcraft_doc::symbol_overrides::dynamic_art(&sym.art, n);
|
||||
let most = doc.symbol_slice_map(symbol, *xf).map_or(art.count_with_masks() as u64, |m| m.most_placed(&art, doc));
|
||||
if (self.work as u64).saturating_add(most) > MAX_WORK as u64 {
|
||||
return Err(self.too_much_work());
|
||||
}
|
||||
let placed = doc.place_symbol_art(symbol, &art, *xf).ok_or_else(missing)?;
|
||||
Ok(self.list(&[Arc::new(placed)], depth + 1)?.0)
|
||||
}
|
||||
NodeKind::Blend { children, spec } => {
|
||||
// Counted before the steps are made, so a blend past the limit is never expanded.
|
||||
let made = vectorcraft_doc::blend::blend_steps(children, spec).into_iter().fold(children.len(), usize::saturating_add);
|
||||
if self.leaves.saturating_add(made) > MAX_MEMBERS {
|
||||
return Err(self.too_many());
|
||||
}
|
||||
self.spend(made)?;
|
||||
let steps: Vec<Arc<Node>> = crate::expand_live(self.doc, n).into_iter().map(Arc::new).collect();
|
||||
Ok(self.list(&steps, depth + 1)?.0)
|
||||
}
|
||||
_ if n.clips() => {
|
||||
let Some((first, rest)) = kids(n).split_first() else { return Ok(vec![]) };
|
||||
if clip_nested_past(first, MAX_DEPTH.saturating_sub(depth)) {
|
||||
return Err(self.too_deep());
|
||||
}
|
||||
let clip = clip_area(first);
|
||||
// An image wholly outside every clip around it shows nothing and is left out.
|
||||
let outer = self.shown;
|
||||
let here = clip.0.bounds().unwrap_or(Rect::ZERO);
|
||||
self.shown = Some(outer.map_or(here, |o| o.intersect(here)));
|
||||
let listed = self.list(rest, depth + 1);
|
||||
self.shown = outer;
|
||||
let (inner, _) = listed?;
|
||||
let mut out = vec![];
|
||||
for c in &inner {
|
||||
if let Some(c) = self.clipped(c, &clip, depth + 1)? {
|
||||
out.push(Arc::new(c));
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
_ => Ok(self.list(kids(n), depth + 1)?.0),
|
||||
}
|
||||
}
|
||||
|
||||
/// The visible members of `children` as plain art: images refused, guides left out, symbol
|
||||
/// instances, blends and clipping groups resolved into groups with their transparency and
|
||||
/// geometry effects, groups holding them rebuilt. Also whether any member changed: a group
|
||||
/// whose members did not is kept as it is. Each object is visited once.
|
||||
fn list(&mut self, children: &[Arc<Node>], depth: usize) -> Result<(Vec<Arc<Node>>, bool), String> {
|
||||
let mut out = Vec::with_capacity(children.len());
|
||||
let mut changed = false;
|
||||
for c in children {
|
||||
self.spend(1)?;
|
||||
if !c.visible || matches!(c.kind, NodeKind::Path { guide: true, .. }) {
|
||||
continue;
|
||||
}
|
||||
if matches!(c.kind, NodeKind::Image(_)) {
|
||||
// Only an image with no effects or fills and strokes of its own is measured by its
|
||||
// frame; any other one may draw inside the clip.
|
||||
let plain = !has_effects(c) && c.appearance.items.iter().all(|i| !i.visible());
|
||||
let hidden = plain && self.shown.is_some_and(|k| c.visual_bounds().is_none_or(|b| b.intersect(k).area() <= 0.0));
|
||||
if hidden {
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
return Err(image_refusal(self.label));
|
||||
}
|
||||
if resolves(c) {
|
||||
let kids = self.inside(c, |r| r.content(c, depth))?;
|
||||
out.push(Arc::new(with_effects(wrap(c, kids), c)));
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if is_container(c) {
|
||||
// At the depth limit a group is kept, and not walked further, only when it holds no
|
||||
// group with members and nothing in it needs resolving.
|
||||
if depth >= MAX_DEPTH {
|
||||
if nested_past(c, 1) || needs(c) {
|
||||
return Err(self.too_deep());
|
||||
}
|
||||
out.push(c.clone());
|
||||
continue;
|
||||
}
|
||||
let (kids, inner) = self.inside(c, |r| r.list(kids(c), depth + 1))?;
|
||||
if inner {
|
||||
out.push(Arc::new(with_effects(wrap(c, kids), c)));
|
||||
changed = true;
|
||||
} else {
|
||||
out.push(c.clone());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
self.leaves += 1;
|
||||
if self.leaves > MAX_MEMBERS {
|
||||
return Err(self.too_many());
|
||||
}
|
||||
out.push(c.clone());
|
||||
}
|
||||
Ok((out, changed))
|
||||
}
|
||||
|
||||
fn too_complex(&self) -> String {
|
||||
format!("{}: the clipping path is too complex to cut the art with", self.label)
|
||||
}
|
||||
|
||||
fn too_many(&self) -> String {
|
||||
format!("{} takes at most {MAX_MEMBERS} objects", self.label)
|
||||
}
|
||||
|
||||
fn too_deep(&self) -> String {
|
||||
format!("{} can't build solids from art nested more than {MAX_DEPTH} levels deep", self.label)
|
||||
}
|
||||
|
||||
/// `n`, plain art inside a clipping group, cut to `clip`: each shape's fills as its area inside
|
||||
/// the clip and each stroke as the part of its outline inside it, filled in the stroke's paint,
|
||||
/// after the geometry effects of the shape and of the groups it is in; hidden objects and guides
|
||||
/// left out. `None` when nothing of it is inside. Objects that build no solid stay as they are.
|
||||
fn clipped(&mut self, n: &Node, clip: &(PathData, FillRule), depth: usize) -> Result<Option<Node>, String> {
|
||||
if depth > MAX_DEPTH {
|
||||
return Err(self.too_deep());
|
||||
}
|
||||
self.spend(1)?;
|
||||
if is_container(n) {
|
||||
let mut kids = vec![];
|
||||
if n.appearance.effects.iter().any(|e| e.visible && is_geometry(&e.id)) {
|
||||
// The group's geometry effects reshape its members together before the clip cuts
|
||||
// them, as they are drawn: each member becomes its reshaped outline.
|
||||
if nested_past(n, MAX_DEPTH.saturating_sub(depth)) {
|
||||
return Err(self.too_deep());
|
||||
}
|
||||
let mut shapes = members(n, self.label)?;
|
||||
reshape_members(n, &n.appearance.effects, &mut shapes);
|
||||
let mut cut = vec![];
|
||||
for s in shapes {
|
||||
if let Some(c) = self.clipped(&shape(n, s.path, s.rule, s.items), clip, depth + 1)? {
|
||||
cut.push((s.scopes, c));
|
||||
}
|
||||
}
|
||||
kids = nest(n, cut, 0);
|
||||
} else {
|
||||
for c in n.children().into_iter().flatten() {
|
||||
if !c.visible || matches!(c.kind, NodeKind::Path { guide: true, .. }) {
|
||||
continue;
|
||||
}
|
||||
if let Some(c) = self.clipped(c, clip, depth + 1)? {
|
||||
kids.push(Arc::new(c));
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok((!kids.is_empty()).then(|| wrap(n, kids)));
|
||||
}
|
||||
let Some((path, rule)) = profile(n) else { return Ok(Some(n.clone())) };
|
||||
let items = solid_paints(n);
|
||||
// The fills' piece goes where the top fill is, the one Extrude & Bevel and Inflate build
|
||||
// their solid from, so the strokes above and below it keep their places. Rotate turns
|
||||
// every fill, so each fill is a piece in its own place.
|
||||
let top_fill = items.iter().rposition(|i| matches!(i, AppearanceItem::Fill(_)));
|
||||
let fill_pieces =
|
||||
if self.every_fill { items.iter().filter(|i| matches!(i, AppearanceItem::Fill(_))).count() } else { usize::from(top_fill.is_some()) };
|
||||
// Each cut makes a piece, counted before it is made: the shape was counted once already.
|
||||
let made = fill_pieces + items.iter().filter(|i| matches!(i, AppearanceItem::Stroke(_))).count();
|
||||
self.spend(made)?;
|
||||
self.leaves = self.leaves.saturating_add(made.saturating_sub(1));
|
||||
if self.leaves > MAX_MEMBERS {
|
||||
return Err(self.too_many());
|
||||
}
|
||||
let path = geometry(&path, &n.appearance.effects, &GeomContext::of(n));
|
||||
let cut = |area: &PathData, rule: FillRule| -> Result<Option<PathData>, String> {
|
||||
let inside =
|
||||
po::try_boolean(area, rule, &clip.0, clip.1, po::BoolOp::Intersect, po::DEFAULT_PRECISION).map_err(|_| self.too_complex())?;
|
||||
Ok(inside.bounds().is_some().then_some(inside))
|
||||
};
|
||||
let fills: Vec<AppearanceItem> = items.iter().filter(|i| matches!(i, AppearanceItem::Fill(_))).cloned().collect();
|
||||
// Every fill paints the same area: it is cut once.
|
||||
let mut fill_area: Option<Option<PathData>> = None;
|
||||
let mut pieces = vec![];
|
||||
for (k, item) in items.iter().enumerate() {
|
||||
match item {
|
||||
AppearanceItem::Fill(_) if self.every_fill || Some(k) == top_fill => {
|
||||
let area = match &fill_area {
|
||||
Some(area) => area.clone(),
|
||||
None => fill_area.insert(cut(&path, rule)?).clone(),
|
||||
};
|
||||
if let Some(area) = area {
|
||||
let painted = if self.every_fill { vec![item.clone()] } else { fills.clone() };
|
||||
pieces.push(Arc::new(shape(n, area, FillRule::NonZero, painted)));
|
||||
}
|
||||
}
|
||||
AppearanceItem::Fill(_) => {}
|
||||
AppearanceItem::Stroke(s) => {
|
||||
let (outline, outline_rule) = region(&path, rule, item);
|
||||
if let Some(area) = cut(&outline, outline_rule)? {
|
||||
let mut fill = FillLayer::new(s.paint.clone());
|
||||
fill.opacity = s.opacity;
|
||||
fill.blend = s.blend;
|
||||
pieces.push(Arc::new(shape(n, area, FillRule::NonZero, vec![AppearanceItem::Fill(fill)])));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok((!pieces.is_empty()).then(|| wrap(n, pieces)))
|
||||
}
|
||||
}
|
||||
|
||||
/// The area clipping path `first` keeps, as the renderer clips by it ([`crate::clip_outline`]): a
|
||||
/// nested clipping group's own clipping path, the union of a group's members. Empty when it keeps
|
||||
/// nothing.
|
||||
fn clip_area(first: &Node) -> (PathData, FillRule) {
|
||||
crate::clip_outline(first).map_or_else(|| (PathData::default(), FillRule::NonZero), |(bp, rule)| (PathData::from_bezpath(&bp), rule))
|
||||
}
|
||||
|
||||
/// `pieces`, each with the [`Scope`]s of the groups it was in, back in groups of `n`'s id with
|
||||
/// those groups' opacity, blending mode and opacity mask: consecutive pieces sharing the scope at
|
||||
/// `level` go into one group, so its transparency applies to them together. `level` stays below the
|
||||
/// nesting the pieces came from ([`nested_past`]).
|
||||
fn nest(n: &Node, pieces: Vec<(Vec<Scope>, Node)>, level: usize) -> Vec<Arc<Node>> {
|
||||
let mut out = vec![];
|
||||
let mut run: Vec<(Vec<Scope>, Node)> = vec![];
|
||||
let mut key: Option<Scope> = None;
|
||||
let close = |run: &mut Vec<(Vec<Scope>, Node)>, scope: Option<Scope>, out: &mut Vec<Arc<Node>>| {
|
||||
let Some(scope) = scope else { return };
|
||||
out.push(Arc::new(scope.group(n.id, nest(n, std::mem::take(run), level + 1))));
|
||||
};
|
||||
for (scopes, piece) in pieces {
|
||||
let here = scopes.get(level).cloned();
|
||||
if here.as_ref().map(|s| s.key) != key.as_ref().map(|s| s.key) {
|
||||
close(&mut run, key.take(), &mut out);
|
||||
key = here.clone();
|
||||
}
|
||||
match here {
|
||||
Some(_) => run.push((scopes, piece)),
|
||||
None => out.push(Arc::new(piece)),
|
||||
}
|
||||
}
|
||||
close(&mut run, key, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
/// Does `n` carry a visible effect?
|
||||
fn has_effects(n: &Node) -> bool {
|
||||
n.appearance.effects.iter().any(|e| e.visible)
|
||||
}
|
||||
|
||||
/// Does the group `n` hold shown objects nested more than `levels` deep in groups and layers?
|
||||
/// Only the members [`members`] walks are followed: hidden members are skipped, and the paths of
|
||||
/// a compound path are one shape. Walked without recursion.
|
||||
fn nested_past(n: &Node, levels: usize) -> bool {
|
||||
let mut stack = vec![(n, 0usize)];
|
||||
while let Some((n, level)) = stack.pop() {
|
||||
if level > levels {
|
||||
return true;
|
||||
}
|
||||
if level == 0 || is_container(n) {
|
||||
stack.extend(kids(n).iter().filter(|c| c.visible).map(|c| (&**c, level + 1)));
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Does the clipping path `n` reach objects nested more than `levels` deep along the members that
|
||||
/// [`Node::clip_shapes`] visits: the first member of a clipping group, the shown members of other
|
||||
/// groups and layers (not a group's shaper), and every member of a live object. Walked without
|
||||
/// recursion.
|
||||
fn clip_nested_past(n: &Node, levels: usize) -> bool {
|
||||
let mut stack = vec![(n, 0usize)];
|
||||
while let Some((n, level)) = stack.pop() {
|
||||
if level > levels {
|
||||
return true;
|
||||
}
|
||||
match &n.kind {
|
||||
NodeKind::Group { children, clip: true } => stack.extend(children.first().map(|c| (&**c, level + 1))),
|
||||
NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } => {
|
||||
let skip = usize::from(n.extras().shaper.is_some());
|
||||
stack.extend(children.iter().skip(skip).filter(|c| c.visible).map(|c| (&**c, level + 1)));
|
||||
}
|
||||
NodeKind::Compound { .. } | NodeKind::SymbolInstance { .. } => {}
|
||||
_ => stack.extend(kids(n).iter().map(|c| (&**c, level + 1))),
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// The members of `n`, none for an object without members.
|
||||
fn kids(n: &Node) -> &[Arc<Node>] {
|
||||
n.children().map_or(&[], Vec::as_slice)
|
||||
}
|
||||
|
||||
/// A path of `n`'s id on `path`, painted by `items` under `rule`.
|
||||
fn shape(n: &Node, path: PathData, rule: FillRule, items: Vec<AppearanceItem>) -> Node {
|
||||
let mut piece = Node::path(n.id, path, Appearance { items, ..Default::default() });
|
||||
if let NodeKind::Path { rule: r, .. } = &mut piece.kind {
|
||||
*r = rule;
|
||||
}
|
||||
piece
|
||||
}
|
||||
|
||||
/// A plain group of `children` with `n`'s id, visibility, opacity, blending mode and opacity mask.
|
||||
fn wrap(n: &Node, children: Vec<Arc<Node>>) -> Node {
|
||||
let mut g = Node::group(n.id, children);
|
||||
g.name = n.name.clone();
|
||||
g.opacity = n.opacity;
|
||||
g.blend = n.blend;
|
||||
if let Some(mask) = &n.extras().mask {
|
||||
g.extras_mut().mask = Some(mask.clone());
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
/// `g`, the group `n` was resolved into, with `n`'s appearance: a 3D effect's group reshapes its
|
||||
/// members with its geometry effects ([`members`]), which read its stroke as they do when `n` is
|
||||
/// drawn. A group's own fills and strokes build no solid.
|
||||
fn with_effects(mut g: Node, n: &Node) -> Node {
|
||||
g.appearance = n.appearance.clone();
|
||||
g
|
||||
}
|
||||
@@ -205,12 +205,12 @@ fn a_stroked_open_path_builds_a_ribbon() {
|
||||
/// still draws.
|
||||
#[test]
|
||||
fn a_stroke_past_the_limits_is_left_out() {
|
||||
// 1,500 corners: within the 2,048 points of the fill's solid, but not the stroke's outline.
|
||||
let points: Vec<Point> = (0..1500)
|
||||
// 40,000 corners: within the 65,536 points of the fill's solid, but not the stroke's outline.
|
||||
let points: Vec<Point> = (0..40_000)
|
||||
.map(|k| {
|
||||
let (s, c) = (k as f64 * std::f64::consts::TAU / 1500.0).sin_cos();
|
||||
let r = if k % 2 == 0 { 100.0 } else { 96.0 };
|
||||
Point::new(200.0 + r * c, 200.0 + r * s)
|
||||
let (s, c) = (k as f64 * std::f64::consts::TAU / 40_000.0).sin_cos();
|
||||
let r = if k % 2 == 0 { 10_000.0 } else { 9_999.0 };
|
||||
Point::new(20_000.0 + r * c, 20_000.0 + r * s)
|
||||
})
|
||||
.collect();
|
||||
let path = PathData::single(vectorcraft_geom::SubPath::polyline(&points, true));
|
||||
@@ -330,7 +330,7 @@ fn export_and_expand_bake_faces_with_their_own_ids() {
|
||||
let mut n = square_node();
|
||||
n.id = d.alloc_id();
|
||||
n.appearance.effects.push(fx(id, json!({"bevel": "rounded"})));
|
||||
assert_eq!(crate::bake_appearance(&n), solid_art(&n), "{id}");
|
||||
assert_eq!(crate::bake_appearance(None, &n), solid_art(&n), "{id}");
|
||||
let mut stroke = |_: &mut Document, _: &PathData, _: FillRule, _: &StrokeLayer| None;
|
||||
let art = crate::expand_leaf(&mut d, &n, &mut stroke).unwrap();
|
||||
let mut ids = std::collections::HashSet::new();
|
||||
@@ -356,7 +356,7 @@ fn a_twisted_tapered_hollow_inflate_exports_and_expands() {
|
||||
let params = json!({"twist": 120, "taper": 50, "cap": false, "bothSides": true, "rotationX": -20, "rotationY": 35});
|
||||
n.appearance.effects.push(fx(INFLATE, params));
|
||||
let art = solid_art(&n).unwrap();
|
||||
assert_eq!(crate::bake_appearance(&n), Some(art.clone()));
|
||||
assert_eq!(crate::bake_appearance(None, &n), Some(art.clone()));
|
||||
assert!(faces(&art).len() > 20, "{} faces", faces(&art).len());
|
||||
let mut stroke = |_: &mut Document, _: &PathData, _: FillRule, _: &StrokeLayer| None;
|
||||
let expanded = crate::expand_leaf(&mut d, &n, &mut stroke).unwrap();
|
||||
@@ -591,13 +591,14 @@ fn a_3d_effect_on_a_group_turns_every_member_about_one_shared_axis() {
|
||||
// Hidden members and guides are left out; a group holding what can't become a solid is
|
||||
// refused with what to do.
|
||||
let mut g = group(EXTRUDE, json!({}));
|
||||
let mut clip = Node::group(NodeId(5), vec![Arc::new(square_at(6, 0.0, RED))]);
|
||||
clip.kind = NodeKind::Group { children: clip.children().unwrap().clone(), clip: true };
|
||||
let mut woven = Node::group(NodeId(5), vec![Arc::new(square_at(6, 0.0, RED))]);
|
||||
woven.kind = NodeKind::Intertwine { children: woven.children().unwrap().clone() };
|
||||
if let NodeKind::Group { children, .. } = &mut g.kind {
|
||||
children.push(Arc::new(clip));
|
||||
children.push(Arc::new(woven));
|
||||
}
|
||||
let err = validate_solid(&g, EXTRUDE, &json!({})).unwrap_err();
|
||||
assert!(err.contains("clipping groups"), "{err}");
|
||||
assert!(err.contains("release or expand its other objects"), "{err}");
|
||||
assert!(err.contains("type, groups, clipping groups, blends and symbol instances"), "{err}");
|
||||
assert!(solid_art(&g).is_none(), "drawn as the group it is");
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ fn the_meshes_of_one_object_share_one_triangle_budget() {
|
||||
assert!(keep_mesh(&mut out, &mut total, meshes[1].clone(), first).is_err());
|
||||
assert_eq!(out.len(), 1);
|
||||
// Building past the budget stops with an error instead of holding every mesh.
|
||||
let shapes = [Profile { path: profile(&n).unwrap().0, rule: FillRule::NonZero, items: solid_paints(&n), opacity: 1.0 }];
|
||||
let shapes = [Profile { path: profile(&n).unwrap().0, rule: FillRule::NonZero, items: solid_paints(&n), opacity: 1.0, scopes: vec![] }];
|
||||
let Some(effect) = n.appearance.effects.first() else { panic!("no effect") };
|
||||
let mut total = 0;
|
||||
assert!(meshes_for(effect, &shapes, &mut total, MAX_MESH_TRIANGLES).is_ok());
|
||||
@@ -79,7 +79,7 @@ fn revolve_stops_at_the_first_mesh_past_the_budget() {
|
||||
assert!(whole.as_ref().is_err_and(|e| !e.contains("to export")), "the stroke's own error: {whole:?}");
|
||||
// With room for less than the fill's mesh, the stroke's is never built.
|
||||
let mut total = 0;
|
||||
let capped = meshes_within(&n, &mut total, triangles - 1);
|
||||
let capped = meshes_within(None, &n, &mut total, triangles - 1);
|
||||
assert!(capped.as_ref().is_err_and(|e| e.contains("too complex to export")), "{capped:?}");
|
||||
assert_eq!(total, triangles);
|
||||
}
|
||||
@@ -92,10 +92,10 @@ fn the_objects_of_one_asset_share_one_triangle_budget() {
|
||||
b.appearance.effects.push(fx(EXTRUDE, json!({})));
|
||||
let triangles = |n: &Node| three_d_meshes(n).unwrap().iter().map(|m| m.mesh.triangles.len()).sum::<usize>();
|
||||
let (ta, tb) = (triangles(&a), triangles(&b));
|
||||
assert!(meshes_of_within([&a], ta).is_ok());
|
||||
assert!(meshes_of_within([&b], tb).is_ok());
|
||||
assert!(meshes_of_within([&a, &b], ta.max(tb)).is_err_and(|e| e.contains("too complex to export")));
|
||||
assert_eq!(meshes_of_within([&a, &b], ta + tb).map(|m| m.len()), Ok(4));
|
||||
assert!(meshes_of_within(None, [&a], ta).is_ok());
|
||||
assert!(meshes_of_within(None, [&b], tb).is_ok());
|
||||
assert!(meshes_of_within(None, [&a, &b], ta.max(tb)).is_err_and(|e| e.contains("too complex to export")));
|
||||
assert_eq!(meshes_of_within(None, [&a, &b], ta + tb).map(|m| m.len()), Ok(4));
|
||||
assert_eq!(three_d_meshes_of([&a, &b]).map(|m| m.len()), Ok(4));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,596 @@
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::color::Color;
|
||||
use vectorcraft_doc::{AppearanceItem, BlendSpacing, BlendSpec, Document, FillLayer, ImageObject, NodeId, StrokeLayer, Symbol};
|
||||
use vectorcraft_geom::{Affine, shapes};
|
||||
|
||||
use super::*;
|
||||
|
||||
const TYPES: [&str; 5] = [EXTRUDE, CLASSIC_EXTRUDE, INFLATE, ROTATE, crate::REVOLVE];
|
||||
|
||||
/// Rotate facing the viewer: the art drawn where it is.
|
||||
fn front() -> Value {
|
||||
json!({"rotationX": 0.0, "rotationY": 0.0, "rotationZ": 0.0})
|
||||
}
|
||||
|
||||
fn rect(id: u64, x0: f64, y0: f64, x1: f64, y1: f64, stroke: f64) -> Node {
|
||||
let path = shapes::rectangle(Rect::new(x0, y0, x1, y1));
|
||||
let stroke_paint = if stroke > 0.0 { Paint::solid(Color::BLACK) } else { Paint::None };
|
||||
Node::path(NodeId(id), path, Appearance::basic(Paint::solid(Color::rgb(0.2, 0.5, 0.8)), stroke_paint, stroke))
|
||||
}
|
||||
|
||||
fn with(mut n: Node, id: &str, params: Value) -> Node {
|
||||
n.appearance.effects.push(crate::new_effect(id, ¶ms).unwrap());
|
||||
n
|
||||
}
|
||||
|
||||
fn art(d: &Document, n: &Node, id: &str) -> Option<Node> {
|
||||
if id == crate::REVOLVE { crate::revolve_art_in(Some(d), n) } else { solid_art_in(Some(d), n) }
|
||||
}
|
||||
|
||||
fn bounds(n: &Node) -> Rect {
|
||||
n.visual_bounds().unwrap()
|
||||
}
|
||||
|
||||
fn close(a: Rect, b: Rect, tolerance: f64) -> bool {
|
||||
[(a.x0, b.x0), (a.y0, b.y0), (a.x1, b.x1), (a.y1, b.y1)].iter().all(|(p, q)| (p - q).abs() <= tolerance)
|
||||
}
|
||||
|
||||
fn document() -> Document {
|
||||
let mut d = Document::new(1000.0, 1000.0);
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Box".into(), art: Arc::new(rect(0, 0.0, 0.0, 100.0, 100.0, 2.0)) });
|
||||
d
|
||||
}
|
||||
|
||||
/// A rectangular clipping path covering `clip`.
|
||||
fn clipping_path(id: u64, clip: Rect) -> Node {
|
||||
let mut path = Node::path(NodeId(id), shapes::rectangle(clip), Appearance::default());
|
||||
if let NodeKind::Path { clipping, .. } = &mut path.kind {
|
||||
*clipping = true;
|
||||
}
|
||||
path
|
||||
}
|
||||
|
||||
/// A clipping group: `art` inside a rectangular clipping path covering `clip`.
|
||||
fn clip_group(id: u64, clip: Rect, art: Node) -> Node {
|
||||
Node::new(NodeId(id), NodeKind::Group { children: vec![Arc::new(clipping_path(id + 1, clip)), Arc::new(art)], clip: true })
|
||||
}
|
||||
|
||||
fn blend(id: u64, steps: u32) -> Node {
|
||||
let keys = vec![Arc::new(rect(id + 1, 100.0, 100.0, 140.0, 140.0, 0.0)), Arc::new(rect(id + 2, 300.0, 120.0, 360.0, 180.0, 0.0))];
|
||||
let spec = BlendSpec { spacing: BlendSpacing::Steps(steps), ..Default::default() };
|
||||
Node::new(NodeId(id), NodeKind::Blend { children: keys, spec: Box::new(spec) })
|
||||
}
|
||||
|
||||
fn instance(id: u64, symbol: &str, xf: Affine) -> Node {
|
||||
Node::new(NodeId(id), NodeKind::SymbolInstance { symbol: symbol.into(), xf })
|
||||
}
|
||||
|
||||
fn image(id: u64) -> Node {
|
||||
Node::new(
|
||||
NodeId(id),
|
||||
NodeKind::Image(Box::new(ImageObject {
|
||||
key: "k".into(),
|
||||
width: 10,
|
||||
height: 10,
|
||||
xf: Affine::IDENTITY,
|
||||
link: None,
|
||||
placement: Default::default(),
|
||||
pixels: None,
|
||||
})),
|
||||
)
|
||||
}
|
||||
|
||||
fn sources() -> [(&'static str, Node); 3] {
|
||||
[
|
||||
("clipping group", clip_group(10, Rect::new(100.0, 100.0, 150.0, 200.0), rect(12, 100.0, 100.0, 200.0, 200.0, 2.0))),
|
||||
("blend", blend(20, 3)),
|
||||
("symbol instance", instance(30, "Box", Affine::translate((300.0, 0.0)))),
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_three_d_type_builds_clipping_groups_blends_and_symbol_instances_alone_and_in_a_group() {
|
||||
let d = document();
|
||||
for (what, source) in sources() {
|
||||
for id in TYPES {
|
||||
let n = with(source.clone(), id, json!({}));
|
||||
assert_eq!(validate_three_d_in(Some(&d), &n, id, &json!({})), Ok(()), "{id} on a {what}");
|
||||
assert!(art(&d, &n, id).is_some_and(|a| a.visual_bounds().is_some()), "{id} on a {what} draws");
|
||||
assert!(three_d_meshes_in(Some(&d), &n).is_ok_and(|m| !m.is_empty()), "{id} on a {what} exports");
|
||||
let g = with(Node::group(NodeId(90), vec![Arc::new(source.clone()), Arc::new(rect(91, 600.0, 600.0, 650.0, 650.0, 0.0))]), id, json!({}));
|
||||
assert_eq!(validate_three_d_in(Some(&d), &g, id, &json!({})), Ok(()), "{id} on a group holding a {what}");
|
||||
assert!(art(&d, &g, id).is_some(), "{id} on a group holding a {what} draws");
|
||||
assert!(three_d_meshes_in(Some(&d), &g).is_ok_and(|m| !m.is_empty()), "{id} on a group holding a {what} exports");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_clipping_groups_solid_is_built_from_its_art_inside_the_clip() {
|
||||
let d = document();
|
||||
let clip = Rect::new(100.0, 100.0, 150.0, 200.0);
|
||||
let fill_only = with(clip_group(10, clip, rect(12, 100.0, 100.0, 200.0, 200.0, 0.0)), ROTATE, front());
|
||||
let cut = with(Node::group(NodeId(10), vec![Arc::new(rect(12, 100.0, 100.0, 150.0, 200.0, 0.0))]), ROTATE, front());
|
||||
assert!(close(bounds(&solid_art_in(Some(&d), &fill_only).unwrap()), bounds(&solid_art(&cut).unwrap()), 0.01));
|
||||
// The stroke is cut to the clip too: nothing reaches past it but the faces' seams.
|
||||
let stroked = with(clip_group(10, clip, rect(12, 100.0, 100.0, 200.0, 200.0, 8.0)), EXTRUDE, front());
|
||||
let b = bounds(&solid_art_in(Some(&d), &stroked).unwrap());
|
||||
let outside = b.x0 < clip.x0 - 1.0 || b.x1 > clip.x1 + 1.0;
|
||||
let extruded =
|
||||
bounds(&solid_art(&with(Node::group(NodeId(10), vec![Arc::new(rect(12, 100.0, 100.0, 150.0, 200.0, 0.0))]), EXTRUDE, front())).unwrap());
|
||||
assert!(!outside && close(b, extruded, 1.0), "{b:?} within {clip:?}");
|
||||
// A clip inside a clip keeps what both show.
|
||||
let nested =
|
||||
with(clip_group(40, Rect::new(120.0, 0.0, 400.0, 400.0), clip_group(10, clip, rect(12, 100.0, 100.0, 200.0, 200.0, 0.0))), ROTATE, front());
|
||||
let both = with(Node::group(NodeId(10), vec![Arc::new(rect(12, 120.0, 100.0, 150.0, 200.0, 0.0))]), ROTATE, front());
|
||||
assert!(close(bounds(&solid_art_in(Some(&d), &nested).unwrap()), bounds(&solid_art(&both).unwrap()), 0.01));
|
||||
// A clipping group used as the clipping path clips by its own clipping path, not by its art.
|
||||
let mask = Arc::new(clip_group(70, clip, rect(72, 100.0, 100.0, 110.0, 110.0, 0.0)));
|
||||
let by_mask = with(
|
||||
Node::new(NodeId(10), NodeKind::Group { children: vec![mask, Arc::new(rect(12, 100.0, 100.0, 200.0, 200.0, 0.0))], clip: true }),
|
||||
ROTATE,
|
||||
front(),
|
||||
);
|
||||
assert!(close(bounds(&solid_art_in(Some(&d), &by_mask).unwrap()), bounds(&solid_art(&cut).unwrap()), 0.01));
|
||||
// A stroke between two fills keeps its place below the top fill's solid.
|
||||
let mut layered = rect(12, 100.0, 100.0, 150.0, 200.0, 0.0);
|
||||
layered.appearance = Appearance {
|
||||
items: vec![
|
||||
AppearanceItem::Fill(FillLayer::new(Paint::solid(Color::rgb(1.0, 0.0, 0.0)))),
|
||||
AppearanceItem::Stroke(StrokeLayer::new(Paint::solid(Color::rgb(0.0, 1.0, 0.0)), 6.0)),
|
||||
AppearanceItem::Fill(FillLayer::new(Paint::solid(Color::rgb(0.0, 0.0, 1.0)))),
|
||||
],
|
||||
..Default::default()
|
||||
}
|
||||
.into();
|
||||
let colors = |n: &Node| three_d_meshes_in(Some(&d), &with(n.clone(), EXTRUDE, json!({}))).unwrap().iter().map(|m| m.color).collect::<Vec<_>>();
|
||||
let plain = colors(&Node::group(NodeId(10), vec![Arc::new(layered.clone())]));
|
||||
assert_eq!(plain.len(), 2, "the stroke's solid and the top fill's");
|
||||
assert_eq!(colors(&clip_group(10, Rect::new(0.0, 0.0, 400.0, 400.0), layered.clone())), plain);
|
||||
// Rotate turns both fills and the stroke, each in its own place.
|
||||
let turned = |n: &Node| three_d_meshes_in(Some(&d), &with(n.clone(), ROTATE, front())).unwrap().iter().map(|m| m.color).collect::<Vec<_>>();
|
||||
let plain = turned(&Node::group(NodeId(10), vec![Arc::new(layered.clone())]));
|
||||
assert_eq!(plain.len(), 3, "both fills' surfaces and the stroke's");
|
||||
let clipped = clip_group(10, Rect::new(0.0, 0.0, 400.0, 400.0), layered);
|
||||
assert_eq!(turned(&clipped), plain);
|
||||
assert_eq!(validate_three_d_in(Some(&d), &clipped, ROTATE, &json!({})), Ok(()));
|
||||
// Each fill Rotate turns counts against the limit on objects; another effect takes the top
|
||||
// fill only, whatever 3D effect the object carries before it changes type.
|
||||
let mut many = rect(12, 100.0, 100.0, 150.0, 200.0, 0.0);
|
||||
many.appearance =
|
||||
Appearance { items: vec![AppearanceItem::Fill(FillLayer::new(Paint::solid(Color::BLACK))); MAX_MEMBERS + 1], ..Default::default() }.into();
|
||||
let rotated = with(clip_group(10, Rect::new(0.0, 0.0, 400.0, 400.0), many), ROTATE, front());
|
||||
assert!(validate_three_d_in(Some(&d), &rotated, ROTATE, &json!({})).is_err_and(|e| e.contains("takes at most")));
|
||||
for id in [EXTRUDE, crate::REVOLVE] {
|
||||
assert_eq!(validate_three_d_in(Some(&d), &rotated, id, &json!({})), Ok(()), "Rotate changed to {id}");
|
||||
}
|
||||
// Art nested past the limit inside a clipping group is refused, not followed to any depth.
|
||||
let mut deep = rect(12, 100.0, 100.0, 200.0, 200.0, 0.0);
|
||||
for i in 0..200 {
|
||||
deep = Node::group(NodeId(1000 + i), vec![Arc::new(deep)]);
|
||||
}
|
||||
let n = with(clip_group(10, clip, deep.clone()), ROTATE, front());
|
||||
assert!(validate_three_d_in(Some(&d), &n, ROTATE, &json!({})).is_err_and(|e| e.contains("levels deep")));
|
||||
assert!(solid_art_in(Some(&d), &n).is_none());
|
||||
// So is a clipping path nested past the limit, and a symbol's art nested past it.
|
||||
let by_deep =
|
||||
Node::new(NodeId(10), NodeKind::Group { children: vec![Arc::new(deep.clone()), Arc::new(rect(12, 0.0, 0.0, 10.0, 10.0, 0.0))], clip: true });
|
||||
let mut deep_symbol = d.clone();
|
||||
deep_symbol.symbols.push(Symbol { dynamic: false, name: "Deep".into(), art: Arc::new(deep) });
|
||||
for n in [with(by_deep, ROTATE, front()), with(instance(30, "Deep", Affine::IDENTITY), ROTATE, front())] {
|
||||
assert!(validate_three_d_in(Some(&deep_symbol), &n, ROTATE, &json!({})).is_err_and(|e| e.contains("levels deep")), "{:?}", n.id);
|
||||
assert!(three_d_meshes_in(Some(&deep_symbol), &n).is_err());
|
||||
}
|
||||
// A compound path counts as one shape at the depth limit: a symbol's art nested as deep as a
|
||||
// path may be also takes a compound path there.
|
||||
let square = rect(80, 0.0, 0.0, 10.0, 10.0, 0.0);
|
||||
let mut compound =
|
||||
Node::new(NodeId(81), NodeKind::Compound { children: vec![Arc::new(square.clone())], rule: vectorcraft_geom::FillRule::NonZero });
|
||||
compound.appearance = square.appearance.clone();
|
||||
let mut seen = vec![];
|
||||
for levels in 60..70 {
|
||||
let ok = |leaf: &Node| {
|
||||
let mut art = leaf.clone();
|
||||
for i in 0..levels {
|
||||
art = Node::group(NodeId(2000 + i), vec![Arc::new(art)]);
|
||||
}
|
||||
let mut doc = d.clone();
|
||||
doc.symbols.push(Symbol { dynamic: false, name: "Nested".into(), art: Arc::new(art) });
|
||||
validate_three_d_in(Some(&doc), &with(instance(30, "Nested", Affine::IDENTITY), ROTATE, front()), ROTATE, &json!({})).is_ok()
|
||||
};
|
||||
let path_ok = ok(&square);
|
||||
assert_eq!(ok(&compound), path_ok, "{levels} levels");
|
||||
seen.push(path_ok);
|
||||
}
|
||||
assert!(seen.contains(&true) && seen.contains(&false), "the levels cross the limit: {seen:?}");
|
||||
// A clipping group used as the clipping path clips by its first member only, so deep art it
|
||||
// clips is not walked for the clip.
|
||||
let mask = Node::new(
|
||||
NodeId(70),
|
||||
NodeKind::Group { children: vec![Arc::new(clipping_path(71, clip)), Arc::new(deep_symbol.symbols[1].art.as_ref().clone())], clip: true },
|
||||
);
|
||||
let by_deep_mask = with(
|
||||
Node::new(NodeId(10), NodeKind::Group { children: vec![Arc::new(mask), Arc::new(rect(12, 100.0, 100.0, 200.0, 200.0, 0.0))], clip: true }),
|
||||
ROTATE,
|
||||
front(),
|
||||
);
|
||||
assert_eq!(validate_three_d_in(Some(&d), &by_deep_mask, ROTATE, &json!({})), Ok(()));
|
||||
assert!(close(bounds(&solid_art_in(Some(&d), &by_deep_mask).unwrap()), bounds(&solid_art(&cut).unwrap()), 0.01));
|
||||
// A clip that shows none of the art builds nothing, and says so.
|
||||
let empty = clip_group(10, Rect::new(500.0, 500.0, 550.0, 550.0), rect(12, 100.0, 100.0, 200.0, 200.0, 2.0));
|
||||
for id in TYPES {
|
||||
let n = with(empty.clone(), id, json!({}));
|
||||
assert!(validate_three_d_in(Some(&d), &n, id, &json!({})).is_err_and(|e| e.contains("at least two anchors")), "{id}");
|
||||
assert!(art(&d, &n, id).is_none(), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symbol_instances_solid_is_its_symbols_art_where_the_instance_places_it() {
|
||||
let d = document();
|
||||
let placed = with(instance(30, "Box", Affine::translate((300.0, 50.0))), EXTRUDE, json!({}));
|
||||
let same = with(Node::group(NodeId(30), vec![Arc::new(rect(31, 300.0, 50.0, 400.0, 150.0, 2.0))]), EXTRUDE, json!({}));
|
||||
assert!(close(bounds(&solid_art_in(Some(&d), &placed).unwrap()), bounds(&solid_art(&same).unwrap()), 0.01));
|
||||
// A redefined symbol reshapes the solid.
|
||||
let mut wider = d.clone();
|
||||
wider.symbols[0].art = Arc::new(rect(0, 0.0, 0.0, 200.0, 100.0, 2.0));
|
||||
assert!(bounds(&solid_art_in(Some(&wider), &placed).unwrap()).width() > bounds(&solid_art_in(Some(&d), &placed).unwrap()).width() + 50.0);
|
||||
// Without the symbol there is nothing to build from.
|
||||
let missing = with(instance(30, "Gone", Affine::IDENTITY), EXTRUDE, json!({}));
|
||||
assert!(validate_three_d_in(Some(&d), &missing, EXTRUDE, &json!({})).is_err_and(|e| e.contains("\"Gone\"")));
|
||||
assert!(validate_three_d(&placed, EXTRUDE, &json!({})).is_err(), "no document: no symbol art");
|
||||
// A symbol holding an instance of itself is refused, not followed without end.
|
||||
let mut looped = d.clone();
|
||||
let inner = Arc::new(instance(0, "Loop", Affine::translate((10.0, 10.0))));
|
||||
looped.symbols.push(Symbol {
|
||||
dynamic: false,
|
||||
name: "Loop".into(),
|
||||
art: Arc::new(Node::group(NodeId(0), vec![Arc::new(rect(0, 0.0, 0.0, 10.0, 10.0, 0.0)), inner])),
|
||||
});
|
||||
let n = with(instance(30, "Loop", Affine::IDENTITY), EXTRUDE, json!({}));
|
||||
assert!(validate_three_d_in(Some(&looped), &n, EXTRUDE, &json!({})).is_err());
|
||||
assert!(solid_art_in(Some(&looped), &n).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_blends_solid_is_built_from_its_keys_and_steps() {
|
||||
let d = document();
|
||||
for steps in [1, 3, 8] {
|
||||
let n = with(blend(20, steps), ROTATE, front());
|
||||
let expanded = Node::group(NodeId(20), crate::expand_live(Some(&d), &blend(20, steps)).into_iter().map(Arc::new).collect());
|
||||
assert_eq!(expanded.children().unwrap().len(), steps as usize + 2);
|
||||
let same = with(expanded, ROTATE, front());
|
||||
let (a, b) = (solid_art_in(Some(&d), &n).unwrap(), solid_art(&same).unwrap());
|
||||
assert_eq!(a.children().unwrap().len(), b.children().unwrap().len(), "a surface for each key and step");
|
||||
assert!(close(bounds(&a), bounds(&b), 0.01));
|
||||
}
|
||||
// A blend making more objects than a 3D effect takes is refused.
|
||||
let keys = (0..6).map(|i| Arc::new(rect(21 + i, 100.0 * i as f64, 0.0, 100.0 * i as f64 + 40.0, 40.0, 0.0))).collect();
|
||||
let spec = BlendSpec { spacing: BlendSpacing::Steps(1000), ..Default::default() };
|
||||
let n = with(Node::new(NodeId(20), NodeKind::Blend { children: keys, spec: Box::new(spec) }), ROTATE, front());
|
||||
assert!(validate_three_d_in(Some(&d), &n, ROTATE, &json!({})).is_err_and(|e| e.contains("at most 4096 objects")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn images_are_refused_alone_and_in_a_group_with_image_trace_named() {
|
||||
let d = document();
|
||||
for id in TYPES {
|
||||
for n in [
|
||||
with(image(50), id, json!({})),
|
||||
with(Node::group(NodeId(51), vec![Arc::new(rect(52, 0.0, 0.0, 10.0, 10.0, 0.0)), Arc::new(image(53))]), id, json!({})),
|
||||
with(clip_group(60, Rect::new(0.0, 0.0, 10.0, 10.0), image(62)), id, json!({})),
|
||||
] {
|
||||
let err = validate_three_d_in(Some(&d), &n, id, &json!({})).unwrap_err();
|
||||
assert!(err.contains("Object › Image Trace › Make"), "{id}: {err}");
|
||||
assert!(art(&d, &n, id).is_none(), "{id}");
|
||||
assert!(three_d_meshes_in(Some(&d), &n).is_err_and(|e| e.contains("Image Trace")), "{id}");
|
||||
}
|
||||
// An image wholly outside the clip, alone or in a group, shows nothing and is left out;
|
||||
// one the clip shows part of is refused.
|
||||
let clip = Rect::new(100.0, 100.0, 150.0, 150.0);
|
||||
let masked = |inside: bool| {
|
||||
let mut img = image(63);
|
||||
if let NodeKind::Image(i) = &mut img.kind {
|
||||
i.xf = Affine::translate(if inside { (95.0, 95.0) } else { (300.0, 300.0) });
|
||||
}
|
||||
img
|
||||
};
|
||||
for img in [masked(false), Node::group(NodeId(64), vec![Arc::new(masked(false))])] {
|
||||
let children = vec![Arc::new(clipping_path(61, clip)), Arc::new(rect(62, 100.0, 100.0, 200.0, 200.0, 0.0)), Arc::new(img)];
|
||||
let n = with(Node::new(NodeId(60), NodeKind::Group { children, clip: true }), id, json!({}));
|
||||
assert_eq!(validate_three_d_in(Some(&d), &n, id, &json!({})), Ok(()), "{id}");
|
||||
assert!(art(&d, &n, id).is_some(), "{id}");
|
||||
}
|
||||
let partly = with(clip_group(60, clip, masked(true)), id, json!({}));
|
||||
assert!(validate_three_d_in(Some(&d), &partly, id, &json!({})).is_err_and(|e| e.contains("Image Trace")), "{id}");
|
||||
// An effect on the image, or on a group holding it, can move it into the clip: refused.
|
||||
let moved = |n: Node| with(n, "distort.transform", json!({"moveH": -200.0, "moveV": -200.0}));
|
||||
for img in [moved(masked(false)), moved(Node::group(NodeId(64), vec![Arc::new(masked(false))]))] {
|
||||
let n = with(clip_group(60, clip, img), id, json!({}));
|
||||
assert!(validate_three_d_in(Some(&d), &n, id, &json!({})).is_err_and(|e| e.contains("Image Trace")), "{id}");
|
||||
}
|
||||
// A hidden image is not drawn, so it builds nothing and refuses nothing.
|
||||
let mut hidden = image(53);
|
||||
hidden.visible = false;
|
||||
let g = with(Node::group(NodeId(51), vec![Arc::new(rect(52, 0.0, 0.0, 10.0, 10.0, 0.0)), Arc::new(hidden)]), id, json!({}));
|
||||
assert_eq!(validate_three_d_in(Some(&d), &g, id, &json!({})), Ok(()), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Symbols `L0` to `L{levels}`: `L0` holds `leaf`, each later one `fan` instances of the one
|
||||
/// before it.
|
||||
fn nested_symbols(levels: usize, fan: usize, leaf: Node) -> Document {
|
||||
let mut d = document();
|
||||
d.symbols.push(Symbol { dynamic: false, name: "L0".into(), art: Arc::new(Node::group(NodeId(0), vec![Arc::new(leaf)])) });
|
||||
for level in 1..=levels {
|
||||
let below = format!("L{}", level - 1);
|
||||
let members = (0..fan).map(|i| Arc::new(instance(0, &below, Affine::translate((i as f64 * 2.0, 0.0))))).collect();
|
||||
d.symbols.push(Symbol { dynamic: false, name: format!("L{level}"), art: Arc::new(Node::group(NodeId(0), members)) });
|
||||
}
|
||||
d
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_symbol_instances_that_repeat_hidden_art_are_refused_past_the_work_limit() {
|
||||
// Four instances at each of ten levels repeat the hidden path about a million times, with no
|
||||
// object to count against the 4,096-object limit.
|
||||
let mut hidden = rect(0, 0.0, 0.0, 10.0, 10.0, 0.0);
|
||||
hidden.visible = false;
|
||||
let d = nested_symbols(10, 4, hidden);
|
||||
for id in TYPES {
|
||||
let n = with(instance(30, "L10", Affine::IDENTITY), id, json!({}));
|
||||
let err = validate_three_d_in(Some(&d), &n, id, &json!({})).unwrap_err();
|
||||
assert!(err.contains("hold too many objects"), "{id}: {err}");
|
||||
assert!(source::WORK.with(std::cell::Cell::get) <= source::MAX_WORK + 1, "{id}: stopped at the limit");
|
||||
assert!(art(&d, &n, id).is_none(), "{id}");
|
||||
assert!(three_d_meshes_in(Some(&d), &n).is_err(), "{id}");
|
||||
}
|
||||
// A clipped shape with more strokes than a 3D effect takes objects is refused before its
|
||||
// strokes are cut into pieces.
|
||||
let mut stroked = rect(12, 100.0, 100.0, 200.0, 200.0, 0.0);
|
||||
let strokes = (0..5000).map(|_| AppearanceItem::Stroke(StrokeLayer::new(Paint::solid(Color::BLACK), 2.0))).collect();
|
||||
stroked.appearance = Appearance { items: strokes, ..Default::default() }.into();
|
||||
let n = with(clip_group(10, Rect::new(100.0, 100.0, 150.0, 200.0), stroked), ROTATE, front());
|
||||
let err = validate_three_d_in(Some(&d), &n, ROTATE, &json!({})).unwrap_err();
|
||||
assert!(err.contains("takes at most 4096 objects") && !err.contains("on a group"), "{err}");
|
||||
// The same nesting with 1,024 visible squares stays far below the limit and builds: each
|
||||
// square, group and instance is visited once (an instance twice: listed, then resolved).
|
||||
let d = nested_symbols(5, 4, rect(0, 0.0, 0.0, 10.0, 10.0, 0.0));
|
||||
let n = with(instance(30, "L5", Affine::IDENTITY), ROTATE, front());
|
||||
assert_eq!(validate_three_d_in(Some(&d), &n, ROTATE, &json!({})), Ok(()));
|
||||
let work = source::WORK.with(std::cell::Cell::get);
|
||||
assert!(work <= 1024 + 1365 * 3, "{work}");
|
||||
assert!(solid_art_in(Some(&d), &n).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symbol_holding_more_objects_than_the_work_limit_is_refused_before_its_art_is_placed() {
|
||||
let mut d = document();
|
||||
let members = (0..=source::MAX_WORK).map(|i| Arc::new(rect(0, i as f64, 0.0, i as f64 + 1.0, 1.0, 0.0))).collect();
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Big".into(), art: Arc::new(Node::group(NodeId(0), members)) });
|
||||
let n = with(instance(30, "Big", Affine::IDENTITY), EXTRUDE, json!({}));
|
||||
let err = validate_three_d_in(Some(&d), &n, EXTRUDE, &json!({})).unwrap_err();
|
||||
assert!(err.contains("hold too many objects"), "{err}");
|
||||
let work = source::WORK.with(std::cell::Cell::get);
|
||||
assert!(work <= 2, "refused before the symbol's art is placed and listed: {work}");
|
||||
}
|
||||
|
||||
/// A gradient fill: a path filled with one is drawn in clip groups where it crosses 9-slice guides.
|
||||
fn gradient() -> Paint {
|
||||
use vectorcraft_doc::color::{Gradient, GradientPaint};
|
||||
Paint::Gradient(Box::new(GradientPaint::new(Gradient::default())))
|
||||
}
|
||||
|
||||
/// A document whose symbol "Sliced" has 9-slice guides and holds `count` paths across all of them,
|
||||
/// each filled with `fill`, and an Extrude on an instance of it at twice its size.
|
||||
fn sliced(count: usize, fill: impl Fn() -> Paint) -> (Document, Node) {
|
||||
let mut d = document();
|
||||
let members = (0..count).map(|i| {
|
||||
Arc::new(Node::path(NodeId(i as u64 + 1), shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), Appearance::basic(fill(), Paint::None, 0.0)))
|
||||
});
|
||||
d.symbols.push(Symbol { dynamic: true, name: "Sliced".into(), art: Arc::new(Node::group(NodeId(0), members.collect())) });
|
||||
d.set_symbol_nine_slice("Sliced", Some(vectorcraft_doc::SymbolNineSlice::default()));
|
||||
let n = with(instance(30, "Sliced", Affine::scale(2.0)), EXTRUDE, json!({}));
|
||||
(d, n)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symbol_whose_9_slice_guides_would_place_too_many_objects_is_refused_before_its_art_is_placed() {
|
||||
// Each gradient path becomes a group of nine clip groups, so the art placed is far past the
|
||||
// limit while the symbol itself holds less than a tenth of it.
|
||||
let (d, n) = sliced(source::MAX_WORK / 20, gradient);
|
||||
assert!(d.symbols[1].art.count_with_masks() < source::MAX_WORK);
|
||||
let err = validate_three_d_in(Some(&d), &n, EXTRUDE, &json!({})).unwrap_err();
|
||||
assert!(err.contains("hold too many objects"), "{err}");
|
||||
let work = source::WORK.with(std::cell::Cell::get);
|
||||
assert!(work <= 2, "refused before the symbol's art is placed and listed: {work}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_symbol_whose_overrides_would_place_too_many_objects_is_refused_before_its_art_is_placed() {
|
||||
// Solid paths are cut at the guides and stay one path each; the instance's overrides fill them
|
||||
// with a gradient, which places each in nine clip groups.
|
||||
let (d, mut n) = sliced(source::MAX_WORK / 20, || Paint::solid(Color::BLACK));
|
||||
n.extras_mut().symbol_overrides = (0..source::MAX_WORK / 20)
|
||||
.map(|i| vectorcraft_doc::SymbolOverride { path: vec![i as u32], fill: Some(gradient()), ..Default::default() })
|
||||
.collect();
|
||||
let err = validate_three_d_in(Some(&d), &n, EXTRUDE, &json!({})).unwrap_err();
|
||||
assert!(err.contains("hold too many objects"), "{err}");
|
||||
let work = source::WORK.with(std::cell::Cell::get);
|
||||
assert!(work <= 2, "refused before the instance's art is placed and listed: {work}");
|
||||
// A few such paths build.
|
||||
let (d, n) = sliced(4, || Paint::solid(Color::BLACK));
|
||||
assert!(solid_art_in(Some(&d), &n).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_sources_and_sources_inside_sources_build_or_refuse_cleanly() {
|
||||
let mut d = document();
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Empty".into(), art: Arc::new(Node::group(NodeId(0), vec![])) });
|
||||
let clip = Rect::new(0.0, 0.0, 50.0, 100.0);
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Clipped".into(), art: Arc::new(clip_group(0, clip, rect(0, 0.0, 0.0, 100.0, 100.0, 0.0))) });
|
||||
let empty_clip = Node::new(NodeId(10), NodeKind::Group { children: vec![], clip: true });
|
||||
let empty_symbol = instance(30, "Empty", Affine::IDENTITY);
|
||||
let no_keys = Node::new(NodeId(20), NodeKind::Blend { children: vec![], spec: Box::default() });
|
||||
for id in TYPES {
|
||||
// Alone, an empty clipping group, an instance of an empty symbol and a blend without keys
|
||||
// build nothing and say so.
|
||||
for empty in [&empty_clip, &empty_symbol, &no_keys] {
|
||||
let n = with(empty.clone(), id, json!({}));
|
||||
let err = validate_three_d_in(Some(&d), &n, id, &json!({})).unwrap_err();
|
||||
assert!(err.contains("at least two anchors"), "{id} on {:?}: {err}", empty.kind);
|
||||
assert!(art(&d, &n, id).is_none(), "{id}");
|
||||
assert!(three_d_meshes_in(Some(&d), &n).is_err(), "{id}");
|
||||
// In a group they add nothing, and the group's other members still build.
|
||||
let g = with(Node::group(NodeId(90), vec![Arc::new(empty.clone()), Arc::new(rect(91, 0.0, 0.0, 50.0, 50.0, 0.0))]), id, json!({}));
|
||||
assert_eq!(validate_three_d_in(Some(&d), &g, id, &json!({})), Ok(()), "{id} on a group holding {:?}", empty.kind);
|
||||
assert!(art(&d, &g, id).is_some(), "{id}");
|
||||
}
|
||||
// A blend set to no steps draws one step, and builds from its keys and that step.
|
||||
let n = with(blend(20, 0), id, json!({}));
|
||||
assert_eq!(validate_three_d_in(Some(&d), &n, id, &json!({})), Ok(()), "{id}");
|
||||
}
|
||||
let drawn = Node::group(NodeId(20), crate::expand_live(Some(&d), &blend(20, 0)).into_iter().map(Arc::new).collect());
|
||||
assert_eq!(drawn.children().unwrap().len(), 3);
|
||||
let (a, b) = (solid_art_in(Some(&d), &with(blend(20, 0), ROTATE, front())).unwrap(), solid_art(&with(drawn, ROTATE, front())).unwrap());
|
||||
assert_eq!(a.children().unwrap().len(), b.children().unwrap().len());
|
||||
assert!(close(bounds(&a), bounds(&b), 0.01));
|
||||
// A symbol holding a clipping group builds from the art inside the clip, where the instance
|
||||
// places it.
|
||||
let placed = with(instance(30, "Clipped", Affine::translate((200.0, 0.0))), ROTATE, front());
|
||||
let cut = with(Node::group(NodeId(30), vec![Arc::new(rect(31, 200.0, 0.0, 250.0, 100.0, 0.0))]), ROTATE, front());
|
||||
assert!(close(bounds(&solid_art_in(Some(&d), &placed).unwrap()), bounds(&solid_art(&cut).unwrap()), 0.01));
|
||||
}
|
||||
|
||||
/// The id of the art of [`masked_by`]'s opacity masks.
|
||||
const MASK_ART: NodeId = NodeId(900);
|
||||
|
||||
/// `n` with an opacity mask of white art covering `r`.
|
||||
fn masked_by(mut n: Node, r: Rect) -> Node {
|
||||
let art = Node::path(MASK_ART, shapes::rectangle(r), Appearance::basic(Paint::solid(Color::WHITE), Paint::None, 0.0));
|
||||
n.extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(art, true)));
|
||||
n
|
||||
}
|
||||
|
||||
/// The faces (paths) of `art`, and how many of them are inside an object [`masked_by`] masks with
|
||||
/// a mask that is not disabled.
|
||||
fn masked_faces(art: &Node) -> (usize, usize) {
|
||||
fn walk(n: &Node, masked: bool, out: &mut (usize, usize)) {
|
||||
let masked = masked || n.extras().mask.as_ref().is_some_and(|m| m.art.id == MASK_ART && !m.disabled);
|
||||
match n.children() {
|
||||
Some(children) => children.iter().for_each(|c| walk(c, masked, out)),
|
||||
None => {
|
||||
out.0 += 1;
|
||||
out.1 += usize::from(masked);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = (0, 0);
|
||||
walk(art, false, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_opacity_mask_on_a_member_or_a_group_in_a_three_d_group_masks_its_solids() {
|
||||
let d = document();
|
||||
let mask = Rect::new(0.0, 0.0, 400.0, 400.0);
|
||||
let mut members = vec![("a path", rect(1, 100.0, 100.0, 150.0, 200.0, 2.0))];
|
||||
members.extend(sources());
|
||||
for (what, member) in members {
|
||||
let other = || Arc::new(rect(91, 600.0, 600.0, 650.0, 650.0, 0.0));
|
||||
let in_group = Node::group(NodeId(80), vec![Arc::new(member.clone())]);
|
||||
let cases = [
|
||||
("masked", Node::group(NodeId(90), vec![Arc::new(masked_by(member.clone(), mask)), other()])),
|
||||
("in a masked group", Node::group(NodeId(90), vec![Arc::new(masked_by(in_group, mask)), other()])),
|
||||
];
|
||||
for (how, g) in cases {
|
||||
for id in TYPES {
|
||||
let g = with(g.clone(), id, json!({}));
|
||||
let (faces, masked) = masked_faces(&art(&d, &g, id).expect("drawn"));
|
||||
assert!(masked > 0, "{id}: the faces of {what} {how} are masked");
|
||||
assert!(masked < faces, "{id}: the other member's faces are not ({masked} of {faces})");
|
||||
}
|
||||
}
|
||||
// A disabled mask masks nothing.
|
||||
let mut off = masked_by(member.clone(), mask);
|
||||
if let Some(m) = off.extras_mut().mask.as_mut() {
|
||||
m.disabled = true;
|
||||
}
|
||||
for id in TYPES {
|
||||
let g = with(Node::group(NodeId(90), vec![Arc::new(off.clone()), other()]), id, json!({}));
|
||||
assert_eq!(masked_faces(&art(&d, &g, id).expect("drawn")).1, 0, "{id}: {what} with a disabled mask");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolved_objects_and_groups_in_a_three_d_group_keep_their_geometry_effects() {
|
||||
let mut d = document();
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Left".into(), art: Arc::new(rect(0, 0.0, 100.0, 50.0, 200.0, 0.0)) });
|
||||
let moved = |n: Node| with(n, "distort.transform", json!({"moveH": 100.0}));
|
||||
let clip = Rect::new(100.0, 100.0, 150.0, 200.0);
|
||||
let target = rect(1, 100.0, 100.0, 150.0, 200.0, 0.0);
|
||||
let expected = bounds(&solid_art(&with(Node::group(NodeId(90), vec![Arc::new(target.clone())]), ROTATE, front())).unwrap());
|
||||
let left = || rect(2, 0.0, 100.0, 50.0, 200.0, 0.0);
|
||||
let members = [
|
||||
("a symbol instance", moved(instance(30, "Left", Affine::IDENTITY))),
|
||||
("a clipping group", moved(clip_group(40, Rect::new(0.0, 100.0, 50.0, 200.0), left()))),
|
||||
("a plain group", moved(Node::group(NodeId(50), vec![Arc::new(left())]))),
|
||||
("a group in a clipping group", clip_group(60, clip, moved(Node::group(NodeId(62), vec![Arc::new(left())])))),
|
||||
];
|
||||
for (what, member) in members {
|
||||
for id in TYPES {
|
||||
let g = with(Node::group(NodeId(90), vec![Arc::new(member.clone())]), id, json!({}));
|
||||
assert_eq!(validate_three_d_in(Some(&d), &g, id, &json!({})), Ok(()), "{id} on a group holding {what}");
|
||||
}
|
||||
let g = with(Node::group(NodeId(90), vec![Arc::new(member)]), ROTATE, front());
|
||||
assert!(close(bounds(&solid_art_in(Some(&d), &g).unwrap()), expected, 0.01), "{what} moved by its Transform effect");
|
||||
}
|
||||
// A blend's effect moves its keys and steps together.
|
||||
let blended = with(Node::group(NodeId(90), vec![Arc::new(moved(blend(20, 3)))]), ROTATE, front());
|
||||
let unmoved = with(Node::group(NodeId(90), vec![Arc::new(blend(20, 3))]), ROTATE, front());
|
||||
let (a, b) = (bounds(&solid_art_in(Some(&d), &blended).unwrap()), bounds(&solid_art_in(Some(&d), &unmoved).unwrap()));
|
||||
assert!(close(a, Rect::new(b.x0 + 100.0, b.y0, b.x1 + 100.0, b.y1), 0.01), "{a:?} is {b:?} moved");
|
||||
// A geometry effect that reads the object's stroke reads the resolved object's, as a group's.
|
||||
let stroked = |kind: NodeKind| {
|
||||
let mut n = Node::new(NodeId(20), kind);
|
||||
n.appearance = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 10.0).into();
|
||||
with(n, "path.outlineStroke", json!({}))
|
||||
};
|
||||
let NodeKind::Blend { children, spec } = blend(20, 3).kind else { panic!("a blend") };
|
||||
let steps = crate::expand_live(Some(&d), &blend(20, 3)).into_iter().map(Arc::new).collect();
|
||||
let group_of = |n: Node| with(Node::group(NodeId(90), vec![Arc::new(n)]), ROTATE, front());
|
||||
let resolved = bounds(&solid_art_in(Some(&d), &group_of(stroked(NodeKind::Blend { children, spec }))).unwrap());
|
||||
let plain = bounds(&solid_art(&group_of(stroked(NodeKind::Group { children: steps, clip: false }))).unwrap());
|
||||
assert!(close(resolved, plain, 0.01), "{resolved:?} {plain:?}");
|
||||
// A member's opacity and blending mode stay on it when its group's effects reshape it in a clip.
|
||||
let mut multiplied = left();
|
||||
multiplied.opacity = 0.5;
|
||||
multiplied.blend = BlendMode::Multiply;
|
||||
let n = clip_group(60, clip, moved(Node::group(NodeId(62), vec![Arc::new(multiplied), Arc::new(left())])));
|
||||
let resolved = resolve(Some(&d), &n, "").unwrap().into_owned();
|
||||
let mut found = vec![];
|
||||
resolved.walk(&mut |c| {
|
||||
if c.blend == BlendMode::Multiply {
|
||||
found.push((c.opacity, c.children().map_or(0, Vec::len)));
|
||||
}
|
||||
});
|
||||
assert_eq!(found, vec![(0.5, 1)], "one Multiply group at 50% holding the one member's piece");
|
||||
// Groups nested past the limit under a group with geometry effects in a clipping group are
|
||||
// refused before the effects reshape them.
|
||||
let mut deep = left();
|
||||
for i in 0..200 {
|
||||
deep = Node::group(NodeId(1000 + i), vec![Arc::new(deep)]);
|
||||
}
|
||||
let n = with(clip_group(60, clip, moved(deep.clone())), ROTATE, front());
|
||||
assert!(validate_three_d_in(Some(&d), &n, ROTATE, &json!({})).is_err_and(|e| e.contains("levels deep")));
|
||||
// A hidden member nested past the limit is left out, as it is drawn, and does not refuse.
|
||||
deep.visible = false;
|
||||
let n = with(clip_group(60, clip, moved(Node::group(NodeId(62), vec![Arc::new(deep), Arc::new(left())]))), ROTATE, front());
|
||||
assert_eq!(validate_three_d_in(Some(&d), &n, ROTATE, &json!({})), Ok(()));
|
||||
// Hidden members of a group inside a clipping group build nothing.
|
||||
let mut hidden = rect(63, 100.0, 100.0, 150.0, 200.0, 0.0);
|
||||
hidden.visible = false;
|
||||
let n = with(clip_group(60, clip, Node::group(NodeId(62), vec![Arc::new(hidden)])), ROTATE, front());
|
||||
assert!(validate_three_d_in(Some(&d), &n, ROTATE, &json!({})).is_err_and(|e| e.contains("at least two anchors")));
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//! Arrowheads. A head of weight `hw` (stroke weight × scale) fits a box `4·hw` long and `4·hw`
|
||||
//! wide. Hollow kinds are rings (`hw/2` walls) or, for the open arrow, a chevron with `hw` arms.
|
||||
//! wide; the line arrow's arms reach twice as far out ([`Arrowhead::half_width`]). Hollow kinds are rings (`hw/2` walls) or, for the open arrow, a chevron with `hw` arms.
|
||||
//! The stroke ends under the head at the head's *inset* from the tip: halfway into solid heads,
|
||||
//! in the back wall of hollow ones (with the cap kept out of the hole) and at the chevron's inner
|
||||
//! corner. With [`ArrowAlign::Tip`] the tip sits on the end point and the stroke is shortened by
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use std::f64::consts::{FRAC_PI_2, PI};
|
||||
use std::f64::consts::{FRAC_1_SQRT_2, FRAC_PI_2, PI};
|
||||
|
||||
use kurbo::{Affine, BezPath, ParamCurve, ParamCurveArclen, PathSeg, Point, Shape, Vec2};
|
||||
use vectorcraft_doc::{ArrowAlign, Arrowhead, StrokeLayer};
|
||||
@@ -252,6 +252,7 @@ const HEX: f64 = 2.0 * 1.732_050_807_568_877_2;
|
||||
/// Flattening tolerance of the curved local heads, in units.
|
||||
const LOCAL_TOL: f64 = 1e-3;
|
||||
|
||||
/// Where the stroke ends under a head drawn in local units ([`local`]), in those units.
|
||||
fn local_stop(kind: Arrowhead) -> Stop {
|
||||
match kind {
|
||||
Arrowhead::Target | Arrowhead::TagOpen | Arrowhead::DiamondOpen => Stop::Wall(4.0),
|
||||
@@ -261,7 +262,7 @@ fn local_stop(kind: Arrowhead) -> Stop {
|
||||
Arrowhead::Fork => Stop::At(2.7),
|
||||
Arrowhead::HalfArrowLeft | Arrowhead::HalfArrowRight | Arrowhead::Feather => Stop::At(1.3),
|
||||
Arrowhead::Swallowtail => Stop::At(1.4),
|
||||
Arrowhead::Chevron => Stop::At(1.0),
|
||||
Arrowhead::Chevron | Arrowhead::LineArrow => Stop::At(1.0),
|
||||
Arrowhead::Slash => Stop::At(1.2),
|
||||
Arrowhead::DoubleSlash => Stop::At(1.05),
|
||||
Arrowhead::DoubleArrow => Stop::At(1.7),
|
||||
@@ -273,8 +274,9 @@ fn local_stop(kind: Arrowhead) -> Stop {
|
||||
}
|
||||
|
||||
/// The outline of a head added after the first ten, in local units: x from the tip (0) back
|
||||
/// along the line (to at most 4), y across it (−2 to 2, positive to the right of the direction
|
||||
/// the head points). Every shape is generated here.
|
||||
/// along the line (to at most 4; the line arrow's square-cut arms 4.71), y across it (−2 to 2,
|
||||
/// the line arrow −4 to 4; positive to the right of the direction the head points). Every shape
|
||||
/// is generated here.
|
||||
fn local(kind: Arrowhead) -> BezPath {
|
||||
let mut p = BezPath::new();
|
||||
let poly = |p: &mut BezPath, pts: &[(f64, f64)]| polygon(p, &pts.iter().map(|&q| Point::from(q)).collect::<Vec<_>>());
|
||||
@@ -363,6 +365,14 @@ fn local(kind: Arrowhead) -> BezPath {
|
||||
poly(&mut p, &[(0.0, 2.0), (0.6, 2.0), (0.6, 0.25), (4.0, 1.8), (4.0, -1.8), (0.6, -0.25), (0.6, -2.0), (0.0, -2.0)])
|
||||
}
|
||||
Arrowhead::Oval => p.extend(kurbo::Ellipse::new((2.0, 0.0), (2.0, 1.2), 0.0).path_elements(LOCAL_TOL)),
|
||||
Arrowhead::LineArrow => {
|
||||
// Arms one unit thick (the head's weight: the line's at 100 % for lines of 0.25 pt or
|
||||
// more) at right angles to each other, from a sharp tip to their outer corners at
|
||||
// (4, ±4), cut square across: a unit across an arm at 45° is ½√2 along each axis, and
|
||||
// the arms' inner edges meet √2 behind the tip.
|
||||
let t = FRAC_1_SQRT_2;
|
||||
poly(&mut p, &[(0.0, 0.0), (4.0, 4.0), (4.0 + t, 4.0 - t), (2.0 * t, 0.0), (4.0 + t, t - 4.0), (4.0, -4.0)]);
|
||||
}
|
||||
// The first ten are drawn by `shape`.
|
||||
Arrowhead::Triangle
|
||||
| Arrowhead::TriangleOpen
|
||||
|
||||
@@ -14,6 +14,10 @@ use super::{ARCLEN_ACCURACY, kink, push_seg, segments, subpaths, tangent};
|
||||
|
||||
/// More dashes than this (a tiny pattern on a long path) draws the line solid instead.
|
||||
const MAX_DASHES: f64 = 200_000.0;
|
||||
/// Pattern entries walked in all (every subpath and every corner restart starts the pattern over, and
|
||||
/// zero-length entries cost no path length, so a long pattern is walked in full even on a short
|
||||
/// run); past this the line is drawn solid. About 100x what real art needs.
|
||||
const MAX_STEPS: f64 = 20_000_000.0;
|
||||
/// A pattern whose longest entry is shorter than this can't advance along the path (each entry
|
||||
/// ends within the generator's tolerance of where it starts), so it draws the line solid.
|
||||
const MIN_ENTRY: f64 = 1e-6;
|
||||
@@ -80,6 +84,10 @@ pub fn dash_kept(bp: &BezPath, d: &Dash, keep: &dyn Fn(&PathSeg) -> bool) -> Opt
|
||||
if plans.iter().map(|p| p.periods).sum::<f64>() * pat.len() as f64 > MAX_DASHES {
|
||||
return None;
|
||||
}
|
||||
let restarts = |p: &Plan| p.restart.iter().filter(|r| **r).count() as f64;
|
||||
if plans.iter().map(|p| (p.periods.ceil().max(1.0) + restarts(p)) * pat.len() as f64).sum::<f64>() > MAX_STEPS {
|
||||
return None;
|
||||
}
|
||||
// Where an exact pattern starts: `offset` into it.
|
||||
let mut off = d.offset.rem_euclid(period);
|
||||
let mut ix0 = 0;
|
||||
@@ -89,8 +97,13 @@ pub fn dash_kept(bp: &BezPath, d: &Dash, keep: &dyn Fn(&PathSeg) -> bool) -> Opt
|
||||
}
|
||||
let phase = Phase { ix: ix0, rem: pat[ix0] - off, on: ix0 % 2 == 0 };
|
||||
let mut out = Dashed::default();
|
||||
// The estimates above trust the fitted scales; this counts the pattern entries actually walked,
|
||||
// so entries that fit down to nothing can't keep a subpath looping.
|
||||
let mut budget = MAX_STEPS as usize;
|
||||
for plan in &plans {
|
||||
dash_subpath(&mut out, plan, &pat, phase, keep);
|
||||
if !dash_subpath(&mut out, plan, &pat, phase, keep, &mut budget) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
@@ -183,7 +196,8 @@ impl Piece {
|
||||
}
|
||||
}
|
||||
|
||||
fn dash_subpath(out: &mut Dashed, plan: &Plan, pat: &[f64], mut ph: Phase, keep: &dyn Fn(&PathSeg) -> bool) {
|
||||
/// Dash one subpath; `false` when `budget` (pattern entries left to walk, shared by all subpaths) runs out.
|
||||
fn dash_subpath(out: &mut Dashed, plan: &Plan, pat: &[f64], mut ph: Phase, keep: &dyn Fn(&PathSeg) -> bool, budget: &mut usize) -> bool {
|
||||
let first_dot = out.dots.len();
|
||||
let total: f64 = plan.lens.iter().sum();
|
||||
// Fraction of the subpath's length at `x` along the plan.
|
||||
@@ -244,6 +258,8 @@ fn dash_subpath(out: &mut Dashed, plan: &Plan, pat: &[f64], mut ph: Phase, keep:
|
||||
push_dot(out, first_dot, Dot { at: seg.eval(t), dir: tangent(seg, t), t: frac(acc + end) });
|
||||
}
|
||||
}
|
||||
let Some(rest) = budget.checked_sub(1) else { return false };
|
||||
*budget = rest;
|
||||
cuts += 1;
|
||||
s = end;
|
||||
ph.ix = (ph.ix + 1) % pat.len();
|
||||
@@ -273,7 +289,7 @@ fn dash_subpath(out: &mut Dashed, plan: &Plan, pat: &[f64], mut ph: Phase, keep:
|
||||
write(&mut out.path, &d.segs);
|
||||
out.path.close_path();
|
||||
out.spans.push((frac(0.0), frac(0.0) + 1.0));
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
if starts_on && ends_on && dashes.len() > 1 {
|
||||
// The dash running through the start point is one dash.
|
||||
@@ -291,6 +307,7 @@ fn dash_subpath(out: &mut Dashed, plan: &Plan, pat: &[f64], mut ph: Phase, keep:
|
||||
let t0 = frac(d.start);
|
||||
out.spans.push((t0, if total > 0.0 { t0 + (d.end - d.start) / total } else { t0 }));
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Add `dot` unless the subpath's last dot (from index `first`) is already there: a run ending on
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Every arrowhead's outline: drawn, inside its box (4 × 4 weights; the open arrow's square-cut
|
||||
//! arms reach a little further back), without self-intersections, and covering the end of the
|
||||
//! stroke under it.
|
||||
//! Every arrowhead's outline: drawn, inside its reach and half width (4 × 4 weights for most; the
|
||||
//! open arrow's square-cut arms reach a little further back, the line arrow's twice as far out),
|
||||
//! without self-intersections, and covering the end of the stroke under it.
|
||||
|
||||
use kurbo::{BezPath, PathEl, Point, Shape};
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
@@ -53,7 +53,13 @@ fn every_outline_is_drawn_inside_its_box_without_self_intersections() {
|
||||
let h = head(kind);
|
||||
assert!(h.outline.area().abs() > 4.0, "{kind:?} is drawn");
|
||||
let b = h.outline.bounding_box();
|
||||
assert!(b.x0 >= 91.0 - 1e-6 && b.x1 <= 100.0 + 1e-6 && b.y0 >= -4.0 - 1e-6 && b.y1 <= 4.0 + 1e-6, "{kind:?} leaves its box: {b:?}");
|
||||
let (reach, side) = (2.0 * kind.reach(), 2.0 * kind.half_width());
|
||||
assert!(b.x1 <= 100.0 + 1e-6 && b.y0 >= -side - 1e-6 && b.y1 <= side + 1e-6, "{kind:?} leaves its box: {b:?}");
|
||||
if kind.half_width() == 2.0 {
|
||||
assert!(b.x0 >= 91.0 - 1e-6, "{kind:?} is longer than the 4 × 4 box: {b:?}");
|
||||
}
|
||||
let far = contours(&h.outline).iter().flatten().map(|p| p.distance(h.tip)).fold(0.0, f64::max);
|
||||
assert!(far <= reach + 1e-6, "{kind:?} reaches {far} from its tip, past {reach}");
|
||||
let edges: Vec<(Point, Point)> = contours(&h.outline).iter().flat_map(|c| (0..c.len()).map(|i| (c[i], c[(i + 1) % c.len()]))).collect();
|
||||
assert!(edges.len() >= 3, "{kind:?}");
|
||||
for (i, a) in edges.iter().enumerate() {
|
||||
@@ -74,6 +80,31 @@ fn the_stroke_ends_inside_every_head() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_line_arrow_is_a_right_angle_of_two_arms_as_thick_as_the_line() {
|
||||
// Weight 2 at 100 %: a local unit (x back from the tip, y across) is 2 pt.
|
||||
let h = head(Arrowhead::LineArrow);
|
||||
let at = |x: f64, y: f64| Point::new(100.0 - 2.0 * x, 2.0 * y);
|
||||
// A sharp tip on the end point; outer corners 4 units back and out, square-cut ends.
|
||||
let pts: Vec<Point> = contours(&h.outline).concat();
|
||||
for (corner, name) in [(at(0.0, 0.0), "tip"), (at(4.0, 4.0), "right corner"), (at(4.0, -4.0), "left corner")] {
|
||||
assert!(pts.iter().any(|p| p.distance(corner) < 1e-9), "{name} {corner:?} in {pts:?}");
|
||||
}
|
||||
// Open between the arms, solid at the tip.
|
||||
assert_eq!(h.outline.winding(at(2.5, 0.0)), 0);
|
||||
assert_ne!(h.outline.winding(at(0.7, 0.0)), 0);
|
||||
// Halfway along each arm, 45° off the line, it is one unit (the line's weight) across.
|
||||
let s = std::f64::consts::FRAC_1_SQRT_2;
|
||||
for side in [1.0, -1.0] {
|
||||
let (mx, my) = (2.0 + 0.5 * s, side * (2.0 - 0.5 * s));
|
||||
let (nx, ny) = (-side * s, s);
|
||||
for (d, inside) in [(0.45, true), (-0.45, true), (0.55, false), (-0.55, false)] {
|
||||
let w = h.outline.winding(at(mx + nx * d, my + ny * d));
|
||||
assert_eq!(w != 0, inside, "arm {side} at {d} across");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn half_arrows_barb_on_their_own_side() {
|
||||
// Pointing along +x on a y-down page, left is −y.
|
||||
|
||||
@@ -222,3 +222,17 @@ fn smooth_points_of_curves_stay_smooth_with_any_join() {
|
||||
assert!((a - want).abs() / want < 0.005, "{join:?}: {a} vs {want}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_long_pattern_restarted_on_many_short_subpaths_draws_solid() {
|
||||
// Each subpath walks the zero-length entries in full although it is far shorter than a period.
|
||||
let mut pattern = vec![0.0; 2000];
|
||||
pattern[0] = 1.0;
|
||||
let mut b = BezPath::new();
|
||||
for i in 0..20_000 {
|
||||
b.move_to((i as f64 * 2.0, 0.0));
|
||||
b.line_to((i as f64 * 2.0 + 0.01, 0.0));
|
||||
}
|
||||
let d = Dash { pattern, offset: 0.0, align_corners: false };
|
||||
assert!(dash(&b, &d).is_none());
|
||||
}
|
||||
@@ -56,6 +56,28 @@ fn check(bp: &BezPath, st: &StrokeLayer, what: &str) {
|
||||
assert!(checked > 0, "{what}: no samples");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_butt_capped_open_path_is_measured_where_it_paints() {
|
||||
let mut vertical = BezPath::new();
|
||||
vertical.move_to((50.0, 10.0));
|
||||
vertical.line_to((50.0, 90.0));
|
||||
let mut diagonal = BezPath::new();
|
||||
diagonal.move_to((10.0, 10.0));
|
||||
diagonal.line_to((40.0, 50.0));
|
||||
let mut arc = BezPath::new();
|
||||
arc.move_to((10.0, 50.0));
|
||||
arc.curve_to((10.0, 22.4), (32.4, 0.0), (60.0, 0.0));
|
||||
let mut stroke = StrokeLayer::new(Paint::solid(Color::BLACK), 4.0);
|
||||
stroke.cap = LineCap::Butt;
|
||||
for (name, bp) in [("vertical", vertical), ("diagonal", diagonal), ("arc", arc)] {
|
||||
let painted = outline_region(&PathData::from_bezpath(&bp), FillRule::NonZero, &stroke).to_bezpath().bounding_box();
|
||||
let bounds = stroke.bounds_on(&bp, bp.bounding_box());
|
||||
for (got, want) in [(bounds.x0, painted.x0), (bounds.y0, painted.y0), (bounds.x1, painted.x1), (bounds.y1, painted.y1)] {
|
||||
assert!((got - want).abs() < 0.05, "{name}: measured {bounds:?}, paints {painted:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bounds_and_clicks_cover_what_every_stroke_paints() {
|
||||
let profiles = [None, Some(WidthProfile::lens()), Some(WidthProfile { points: vec![(0.0, 2.0, 0.5), (0.6, 0.8, 1.6), (1.0, 1.2, 0.3)] })];
|
||||
|
||||
@@ -1101,8 +1101,9 @@ pub fn with_id(xml: &str, id: &str) -> Option<String> {
|
||||
let f = filter_element(&doc).ok()?;
|
||||
let at = f.attributes().find(|a| a.name() == "id" && a.namespace().is_none())?.range_value();
|
||||
let (start, end) = (at.start.checked_sub(head)?, at.end.checked_sub(head)?);
|
||||
// The value may sit in single quotes: those are escaped too.
|
||||
let id = escaped(id).replace('\'', "'");
|
||||
// The value may sit in single quotes: those are escaped too. A tab, line feed or carriage
|
||||
// return is written as a character reference, which attribute-value normalization keeps.
|
||||
let id = escaped(id).replace('\'', "'").replace('\t', "	").replace('\n', " ").replace('\r', " ");
|
||||
Some(format!("{}{}{}", xml.get(..start)?, id, xml.get(end..)?))
|
||||
}
|
||||
|
||||
|
||||
+11
-11
@@ -412,7 +412,7 @@ fn stacked_effects_on_art_stay_within_the_anchor_budget() {
|
||||
let leaf = |x: f64| Arc::new(Node::path(id, shapes::rectangle(Rect::new(x, 0.0, x + 100.0, 100.0)), Appearance::default()));
|
||||
let mut art = Node::group(id, (0..10).map(|k| leaf(k as f64 * 120.0)).collect());
|
||||
let rough = fx("distort.roughen", json!({"size": 50, "detail": 100}));
|
||||
crate::reshape::reshape_leaves_within(&mut art, &[&rough, &rough, &rough], Budget { anchors: 5_000, lines: usize::MAX });
|
||||
crate::reshape::reshape_leaves_within(&mut art, &[&rough, &rough, &rough], Budget { anchors: 5_000, lines: usize::MAX }, None);
|
||||
let mut total = 0;
|
||||
let mut reshaped = 0;
|
||||
for c in art.children().into_iter().flatten() {
|
||||
@@ -451,12 +451,12 @@ fn art_past_the_budget_keeps_its_own_gradients_in_place() {
|
||||
for params in [json!({"scaleH": 1000, "scaleV": 1000}), json!({"scaleH": 1000, "scaleV": 1000, "strokes": true})] {
|
||||
let grow = fx("distort.transform", params.clone());
|
||||
let mut art = Node::group(id, vec![circle()]);
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&grow], Budget::EFFECTS);
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&grow], Budget::EFFECTS, None);
|
||||
let end = own_end(&carry).unwrap();
|
||||
assert!((end - 550.0).abs() < 1e-6, "{params}: within the budget the group's gradient grows with the circle: {end}");
|
||||
// No room beyond what the circle costs: scaled up, it would be drawn with more lines.
|
||||
let mut art = Node::group(id, vec![circle()]);
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&grow], Budget { anchors: usize::MAX, lines: 0 });
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&grow], Budget { anchors: usize::MAX, lines: 0 }, None);
|
||||
let b = art.geometric_bounds().unwrap();
|
||||
assert!(b.width() < 101.0 && b.height() < 101.0, "{params}: the circle keeps its shape: {b:?}");
|
||||
assert_eq!(own_end(&carry), Some(100.0), "{params}: the group's gradient stays where it was placed");
|
||||
@@ -465,7 +465,7 @@ fn art_past_the_budget_keeps_its_own_gradients_in_place() {
|
||||
let mut inner = Node::group(NodeId(2), vec![square]);
|
||||
inner.appearance.items.push(AppearanceItem::Fill(FillLayer::new(placed.clone())));
|
||||
let mut art = Node::group(id, vec![circle(), Arc::new(inner)]);
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&grow], Budget { anchors: usize::MAX, lines: 0 });
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&grow], Budget { anchors: usize::MAX, lines: 0 }, None);
|
||||
assert_eq!(own_end(&carry), Some(100.0), "{params}: the art's own gradient stays");
|
||||
let inner = &art.children().unwrap()[1];
|
||||
let Paint::Gradient(g) = inner.appearance.fill_paint() else { panic!("a gradient") };
|
||||
@@ -509,7 +509,7 @@ fn a_hidden_path_past_the_budget_leaves_a_layers_gradients_free_to_move() {
|
||||
let corners = json!({"corners": [[-4.5, -4.5], [5.5, -4.5], [5.5, 5.5], [-4.5, 5.5]]});
|
||||
for grow in [fx("distort.transform", json!({"scaleH": 1000, "scaleV": 1000})), fx("distort.freeDistort", corners)] {
|
||||
let mut art = layer();
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&grow], budget);
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&grow], budget, None);
|
||||
let circle = art.children().unwrap()[1].geometric_bounds().unwrap();
|
||||
assert!(circle.width() < 101.0, "{}: the hidden circle keeps its shape: {circle:?}", grow.id);
|
||||
let square = art.children().unwrap()[0].geometric_bounds().unwrap();
|
||||
@@ -518,7 +518,7 @@ fn a_hidden_path_past_the_budget_leaves_a_layers_gradients_free_to_move() {
|
||||
carry.apply(&mut own);
|
||||
assert_eq!(end(&own), Some(550.0), "{}: the layer's gradient, as the art", grow.id);
|
||||
let mut art = Node::group(NodeId(9), vec![Arc::new(layer())]);
|
||||
crate::reshape::reshape_leaves_within(&mut art, &[&grow], budget);
|
||||
crate::reshape::reshape_leaves_within(&mut art, &[&grow], budget, None);
|
||||
assert_eq!(end(&art.children().unwrap()[0].appearance), Some(550.0), "{}: the layer's gradient, inside the art", grow.id);
|
||||
}
|
||||
}
|
||||
@@ -549,7 +549,7 @@ fn edited_freeform_points_follow_a_transform_effect_and_free_distort() {
|
||||
inner.appearance.items.push(AppearanceItem::Fill(FillLayer::new(freeform.clone())));
|
||||
let mut art = Node::group(NodeId(9), vec![square(Appearance::basic(freeform.clone(), Paint::None, 0.0)), Arc::new(inner)]);
|
||||
art.appearance.items.push(AppearanceItem::Fill(FillLayer::new(freeform.clone())));
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&e], Budget::EFFECTS);
|
||||
let carry = crate::reshape::reshape_leaves_within(&mut art, &[&e], Budget::EFFECTS, None);
|
||||
let members = art.children().unwrap();
|
||||
assert_eq!(xs(&members[0].appearance), Some(vec![30.0, 130.0]), "{}: the path's points", e.id);
|
||||
assert_eq!(xs(&members[1].appearance), Some(vec![30.0, 130.0]), "{}: the inner group's points", e.id);
|
||||
@@ -625,7 +625,7 @@ fn reshape_leaves_within_optimizes_transform_before_terminal_offset_path() {
|
||||
let offset = fx("path.offsetPath", json!({"offset": 4.0, "joins": "round"}));
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
crate::reshape::reshape_leaves_within(&mut art, &[&transform, &offset], Budget::EFFECTS);
|
||||
crate::reshape::reshape_leaves_within(&mut art, &[&transform, &offset], Budget::EFFECTS, None);
|
||||
let elapsed = start.elapsed();
|
||||
assert!(elapsed < std::time::Duration::from_secs(2));
|
||||
|
||||
@@ -652,7 +652,7 @@ fn reshape_leaves_within_preserves_order_when_not_terminal_or_not_style_safe() {
|
||||
let mut art_evenodd = Node::group(id, vec![Arc::new(leaf)]);
|
||||
let transform = fx("distort.transform", json!({"moveH": 2.0, "moveV": 2.0, "copies": 2}));
|
||||
let offset = fx("path.offsetPath", json!({"offset": 4.0}));
|
||||
crate::reshape::reshape_leaves_within(&mut art_evenodd, &[&transform, &offset], Budget::EFFECTS);
|
||||
crate::reshape::reshape_leaves_within(&mut art_evenodd, &[&transform, &offset], Budget::EFFECTS, None);
|
||||
let p = art_evenodd.children().unwrap()[0].path_data().unwrap();
|
||||
assert_eq!(p.subpaths.len(), 1, "EvenOdd does not swap; copies union into 1 outer subpath");
|
||||
|
||||
@@ -660,7 +660,7 @@ fn reshape_leaves_within_preserves_order_when_not_terminal_or_not_style_safe() {
|
||||
let leaf_round = Arc::new(Node::path(id, shapes::rectangle(Rect::new(0.0, 0.0, 20.0, 20.0)), Appearance::default()));
|
||||
let mut art_round = Node::group(id, vec![leaf_round]);
|
||||
let round_corners = fx("stylize.roundCorners", json!({"radius": 2.0}));
|
||||
crate::reshape::reshape_leaves_within(&mut art_round, &[&transform, &offset, &round_corners], Budget::EFFECTS);
|
||||
crate::reshape::reshape_leaves_within(&mut art_round, &[&transform, &offset, &round_corners], Budget::EFFECTS, None);
|
||||
let p = art_round.children().unwrap()[0].path_data().unwrap();
|
||||
assert_eq!(p.subpaths.len(), 1, "Non-terminal Offset Path does not swap; copies union into 1 outer subpath");
|
||||
|
||||
@@ -668,7 +668,7 @@ fn reshape_leaves_within_preserves_order_when_not_terminal_or_not_style_safe() {
|
||||
let leaf_miter = Arc::new(Node::path(id, shapes::rectangle(Rect::new(0.0, 0.0, 20.0, 20.0)), Appearance::default()));
|
||||
let mut art_miter = Node::group(id, vec![leaf_miter]);
|
||||
let offset_miter = fx("path.offsetPath", json!({"offset": 4.0, "joins": "miter"}));
|
||||
crate::reshape::reshape_leaves_within(&mut art_miter, &[&transform, &offset_miter], Budget::EFFECTS);
|
||||
crate::reshape::reshape_leaves_within(&mut art_miter, &[&transform, &offset_miter], Budget::EFFECTS, None);
|
||||
let p = art_miter.children().unwrap()[0].path_data().unwrap();
|
||||
assert_eq!(p.subpaths.len(), 1, "Miter join does not swap; copies union into 1 outer subpath");
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ fn each_copy_scales_its_stroke_once_more() {
|
||||
let n = stroked(json!({"scaleH": 200, "scaleV": 200, "copies": 2, "strokes": true}));
|
||||
let art = reshape(None, &n, None).unwrap();
|
||||
let widths: Vec<f64> = leaves(&art).iter().map(|p| stroke_of(p).0).collect();
|
||||
assert_eq!(widths, vec![2.0, 4.0, 8.0]);
|
||||
assert_eq!(widths, vec![8.0, 4.0, 2.0], "the original on top");
|
||||
// Without the option the copies keep the original's weight.
|
||||
let plain = stroked(json!({"scaleH": 200, "scaleV": 200, "copies": 2}));
|
||||
assert!(!has_transform_art(&plain));
|
||||
@@ -127,7 +127,7 @@ fn transform_patterns_moves_each_copys_pattern() {
|
||||
assert!(has_transform_options(&n));
|
||||
let art = reshape(None, &n, None).unwrap();
|
||||
let xs: Vec<f64> = leaves(&art).iter().map(|p| pattern_xf(p).as_coeffs()[4]).collect();
|
||||
assert_eq!(xs, vec![0.0, 30.0, 60.0]);
|
||||
assert_eq!(xs, vec![60.0, 30.0, 0.0], "the original on top");
|
||||
// Without it, every copy's pattern stays put (the tiles don't follow the copies).
|
||||
let n = patterned(json!({"moveH": 30, "patterns": false, "strokes": false, "objects": true}));
|
||||
assert!(!has_transform_art(&n));
|
||||
@@ -218,7 +218,7 @@ fn each_copy_of_a_strokes_own_transform_scales_its_weight_once_more() {
|
||||
let n = stroke_with_effect(json!({"scaleH": 200, "scaleV": 200, "copies": 2, "strokes": true}));
|
||||
let art = reshape(None, &n, None).unwrap();
|
||||
let widths: Vec<f64> = stroked_leaves(&art).iter().map(|p| stroke_of(p).0).collect();
|
||||
assert_eq!(widths, vec![2.0, 4.0, 8.0]);
|
||||
assert_eq!(widths, vec![8.0, 4.0, 2.0], "the original on top");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -268,7 +268,7 @@ fn a_fills_own_transform_moves_each_copys_pattern_with_transform_patterns() {
|
||||
assert!(has_transform_options(&n));
|
||||
let art = reshape(None, &n, None).unwrap();
|
||||
let xs: Vec<f64> = pattern_leaves(&art).iter().map(|p| pattern_xf(p).as_coeffs()[4]).collect();
|
||||
assert_eq!(xs, vec![0.0, 30.0, 60.0]);
|
||||
assert_eq!(xs, vec![60.0, 30.0, 0.0], "the original on top");
|
||||
// Off, the pattern stays put.
|
||||
assert!(!has_transform_art(&fill_with_effect(json!({"moveH": 30, "copies": 2}))));
|
||||
}
|
||||
@@ -294,7 +294,7 @@ fn a_strokes_own_transform_draws_bakes_expands_and_measures_alike() {
|
||||
let n = stroke_with_effect(json!({"scaleH": 200, "scaleV": 200, "copies": 1, "strokes": true}));
|
||||
let drawn = reshape(None, &n, None).unwrap();
|
||||
let widths = |art: &Node| stroked_leaves(art).iter().map(|p| stroke_of(p).0).collect::<Vec<f64>>();
|
||||
assert_eq!(widths(&drawn), vec![2.0, 4.0]);
|
||||
assert_eq!(widths(&drawn), vec![4.0, 2.0], "the original on top");
|
||||
// Expand Appearance.
|
||||
assert_eq!(widths(&expand_outlined(None, &n, None).unwrap()), widths(&drawn));
|
||||
// Export.
|
||||
@@ -394,9 +394,9 @@ fn filled_and_stroked(fill: Paint, params: Value) -> Node {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copies_with_a_fill_and_a_stroke_are_drawn_one_above_another() {
|
||||
// Black fill, white stroke, each copy scaled and turned (#5920): each copy is an object of its
|
||||
// own above the one before, so its fill covers the strokes of the copies below.
|
||||
fn copies_with_a_fill_and_a_stroke_are_drawn_one_below_another() {
|
||||
// Black fill, white stroke, each copy scaled and turned (#5920, #6316): each copy is an object
|
||||
// of its own below the one before, so the fills above cover the strokes of the copies below.
|
||||
let params = json!({"scaleH": 97, "scaleV": 97, "rotate": 10, "copies": 25});
|
||||
let n = filled_and_stroked(Paint::solid(Color::BLACK), params.clone());
|
||||
assert!(has_transform_art(&n));
|
||||
@@ -407,10 +407,10 @@ fn copies_with_a_fill_and_a_stroke_are_drawn_one_above_another() {
|
||||
assert!(matches!(p.kind, NodeKind::Path { .. }), "one shape each, not one path of every copy");
|
||||
assert!(!p.appearance.fill_paint().is_none() && p.appearance.stroke().is_some());
|
||||
}
|
||||
// The original is at the bottom, the smallest copy on top.
|
||||
assert_eq!(paths[0].geometric_bounds().unwrap(), Rect::new(0.0, 0.0, 100.0, 100.0));
|
||||
let top = paths[25].geometric_bounds().unwrap();
|
||||
assert!(top.width() < 100.0 && top.height() < 100.0, "{top:?}");
|
||||
// The smallest copy is at the bottom, the original on top.
|
||||
assert_eq!(paths[25].geometric_bounds().unwrap(), Rect::new(0.0, 0.0, 100.0, 100.0));
|
||||
let bottom = paths[0].geometric_bounds().unwrap();
|
||||
assert!(bottom.width() < 100.0 && bottom.height() < 100.0, "{bottom:?}");
|
||||
// Drawn as Expand Appearance makes it.
|
||||
assert_eq!(leaves(&expand_outlined(None, &n, None).unwrap()).len(), 26);
|
||||
|
||||
@@ -427,3 +427,252 @@ fn copies_with_a_fill_and_a_stroke_are_drawn_one_above_another() {
|
||||
// No copies: nothing to stack.
|
||||
assert!(!has_transform_art(&filled_and_stroked(Paint::solid(Color::BLACK), json!({"rotate": 10}))));
|
||||
}
|
||||
|
||||
/// A group of two squares side by side, each with a black fill and a white stroke, with the
|
||||
/// Transform effect `params`.
|
||||
fn two_squares(params: Value) -> Node {
|
||||
let square = |id, x| {
|
||||
let a = Appearance::basic(Paint::solid(Color::BLACK), Paint::solid(Color::WHITE), 4.0);
|
||||
std::sync::Arc::new(Node::path(NodeId(id), shapes::rectangle(Rect::new(x, 0.0, x + 100.0, 100.0)), a))
|
||||
};
|
||||
let mut g = Node::group(NodeId(20), vec![square(21, 0.0), square(22, 120.0)]);
|
||||
g.appearance.effects.push(transform(params));
|
||||
g
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_groups_copies_are_whole_groups_below_the_original() {
|
||||
let art = crate::evaluate_container(None, &two_squares(json!({"scaleH": 95, "scaleV": 95, "copies": 10}))).unwrap();
|
||||
let copies = art.children().unwrap();
|
||||
assert_eq!(copies.len(), 11, "the original and 10 copies");
|
||||
for c in copies {
|
||||
assert_eq!(c.children().map(Vec::len), Some(2), "each copy holds both squares");
|
||||
}
|
||||
// The smallest copy is at the bottom, the original on top with its members in their order.
|
||||
let width = |i: usize| copies[i].geometric_bounds().unwrap().width();
|
||||
assert!((0..10).all(|i| width(i) < width(i + 1)), "{:?}", (0..11).map(width).collect::<Vec<_>>());
|
||||
assert_eq!(copies[10].geometric_bounds().unwrap(), Rect::new(0.0, 0.0, 220.0, 100.0));
|
||||
let members = copies[10].children().unwrap();
|
||||
assert_eq!(members[0].geometric_bounds().unwrap(), Rect::new(0.0, 0.0, 100.0, 100.0));
|
||||
assert_eq!(members[1].geometric_bounds().unwrap(), Rect::new(120.0, 0.0, 220.0, 100.0));
|
||||
|
||||
// Without copies the members stay members of the art.
|
||||
let art = crate::evaluate_container(None, &two_squares(json!({"scaleH": 50, "scaleV": 50}))).unwrap();
|
||||
assert_eq!(art.children().unwrap().iter().map(|c| c.id).collect::<Vec<_>>(), [NodeId(21), NodeId(22)]);
|
||||
|
||||
// A group's own fill paints the shape of all its copies, so each member keeps its place.
|
||||
let mut own_fill = two_squares(json!({}));
|
||||
own_fill.appearance = Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0).into();
|
||||
let copies = transform(json!({"moveH": 300, "copies": 2}));
|
||||
crate::reshape::reshape_leaves_within(&mut own_fill, &[&copies], crate::geometry::Budget::EFFECTS, None);
|
||||
assert_eq!(own_fill.children().map(Vec::len), Some(2), "a stack of copies per member");
|
||||
|
||||
// A guide in the group stays one guide.
|
||||
let mut guided = two_squares(json!({}));
|
||||
let mut guide = Node::path(NodeId(23), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default());
|
||||
if let NodeKind::Path { guide: on, .. } = &mut guide.kind {
|
||||
*on = true;
|
||||
}
|
||||
if let Some(members) = guided.children_mut() {
|
||||
members.push(std::sync::Arc::new(guide));
|
||||
}
|
||||
guided.appearance.effects.clear();
|
||||
crate::reshape::reshape_leaves_within(&mut guided, &[&copies], crate::geometry::Budget::EFFECTS, None);
|
||||
assert_eq!(guided.children().map(Vec::len), Some(3), "the original and 2 copies");
|
||||
let mut guides = 0;
|
||||
guided.walk(&mut |n| guides += usize::from(matches!(n.kind, NodeKind::Path { guide: true, .. })));
|
||||
assert_eq!(guides, 1);
|
||||
|
||||
// A clipping group's first member stays its clip.
|
||||
let mut clipping = two_squares(json!({}));
|
||||
if let NodeKind::Group { clip, .. } = &mut clipping.kind {
|
||||
*clip = true;
|
||||
}
|
||||
clipping.appearance.effects.clear();
|
||||
crate::reshape::reshape_leaves_within(&mut clipping, &[&copies], crate::geometry::Budget::EFFECTS, None);
|
||||
assert_eq!(clipping.children().map(Vec::len), Some(2), "the clip and the clipped member");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_groups_copies_stop_at_the_node_limit() {
|
||||
// Empty groups cost no anchors, so only the count of nodes stops their copies.
|
||||
let empties = (0..5_000).map(|i| std::sync::Arc::new(Node::group(NodeId(100 + i), vec![])));
|
||||
let mut g = two_squares(json!({}));
|
||||
if let Some(members) = g.children_mut() {
|
||||
members.extend(empties);
|
||||
}
|
||||
g.appearance.effects.clear();
|
||||
let copies = transform(json!({"moveH": 1, "copies": 1000}));
|
||||
crate::reshape::reshape_leaves_within(&mut g, &[&copies], crate::geometry::Budget::EFFECTS, None);
|
||||
let mut nodes = 0usize;
|
||||
g.walk(&mut |_| nodes += 1);
|
||||
assert!(nodes <= crate::distort::MAX_COPY_OBJECTS + 1, "{nodes}");
|
||||
assert!(g.children().unwrap().len() < 1001, "the limit stopped the copies");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_groups_copies_count_the_art_of_opacity_masks() {
|
||||
let empties = (0..5_000).map(|i| std::sync::Arc::new(Node::group(NodeId(100 + i), vec![]))).collect();
|
||||
let mut g = two_squares(json!({}));
|
||||
if let Some(members) = g.children_mut() {
|
||||
let mask = vectorcraft_doc::OpacityMask::new(Node::group(NodeId(99), empties), true);
|
||||
std::sync::Arc::make_mut(&mut members[0]).extras_mut().mask = Some(Box::new(mask));
|
||||
}
|
||||
g.appearance.effects.clear();
|
||||
let copies = transform(json!({"moveH": 1, "copies": 1000}));
|
||||
crate::reshape::reshape_leaves_within(&mut g, &[&copies], crate::geometry::Budget::EFFECTS, None);
|
||||
let nodes = g.count_with_masks();
|
||||
assert!(nodes <= crate::distort::MAX_COPY_OBJECTS + 1, "{nodes}");
|
||||
}
|
||||
|
||||
/// The anchors `n` draws with every effect inside it evaluated, group by group and member by
|
||||
/// member as drawing and baking evaluate them. The count stops once it passes `limit`.
|
||||
fn drawn_anchors(n: &Node, limit: usize) -> usize {
|
||||
fn count(n: &Node, limit: usize, total: &mut usize) {
|
||||
if *total > limit {
|
||||
return;
|
||||
}
|
||||
if n.children().is_some() && !matches!(n.kind, NodeKind::Compound { .. }) {
|
||||
let art = crate::evaluate_container(None, n).unwrap_or_else(|| n.clone());
|
||||
art.children().into_iter().flatten().for_each(|c| count(c, limit, total));
|
||||
return;
|
||||
}
|
||||
if let Some(art) = reshape(None, n, None) {
|
||||
count(&art, limit, total);
|
||||
return;
|
||||
}
|
||||
let Some((path, _)) = crate::reshape::leaf_geometry(n) else { return };
|
||||
let Some(bounds) = path.bounds() else { return };
|
||||
let own = crate::apply_geometry(&n.appearance.effects, &path, bounds);
|
||||
let items: Vec<usize> = n
|
||||
.appearance
|
||||
.items
|
||||
.iter()
|
||||
.filter(|i| crate::has_geometry(i.effects()))
|
||||
.filter_map(|i| Some(crate::apply_geometry(i.effects(), &own, own.bounds()?).anchor_count()))
|
||||
.collect();
|
||||
*total += if items.is_empty() { own.anchor_count() } else { items.iter().sum() };
|
||||
}
|
||||
let mut total = 0;
|
||||
count(n, limit, &mut total);
|
||||
total
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_groups_copies_keep_what_the_effects_inside_them_make_to_the_budget() {
|
||||
let most = crate::geometry::MAX_EFFECT_ANCHORS;
|
||||
let copies = |key: &str| transform(json!({key: 1, "copies": 1000}));
|
||||
|
||||
// Copies of a group whose member group makes copies of its own.
|
||||
let inner = two_squares(json!({"moveH": 1, "copies": 1000}));
|
||||
let mut outer = Node::group(NodeId(30), vec![std::sync::Arc::new(inner)]);
|
||||
outer.appearance.effects.push(copies("moveV"));
|
||||
let drawn = drawn_anchors(&outer, 2 * most);
|
||||
assert!(drawn <= most, "a group inside: {drawn}");
|
||||
|
||||
// Copies of a group whose member path makes copies of its own.
|
||||
let mut own = two_squares(json!({"moveH": 1, "copies": 1000}));
|
||||
if let Some(members) = own.children_mut() {
|
||||
std::sync::Arc::make_mut(&mut members[0]).appearance.effects.push(copies("moveV"));
|
||||
}
|
||||
let drawn = drawn_anchors(&own, 2 * most);
|
||||
assert!(drawn <= most, "a path's own effect: {drawn}");
|
||||
|
||||
// Copies of a group whose member's fill makes copies of its own.
|
||||
let mut item = two_squares(json!({"moveH": 1, "copies": 1000}));
|
||||
if let Some(members) = item.children_mut() {
|
||||
std::sync::Arc::make_mut(&mut members[0]).appearance.items[0].effects_mut().push(copies("moveV"));
|
||||
}
|
||||
let drawn = drawn_anchors(&item, 2 * most);
|
||||
assert!(drawn <= most, "a fill's own effect: {drawn}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copies_with_scaled_effects_keep_what_each_one_makes_to_the_budget() {
|
||||
// Each copy's Roughen is twice as large as the last's, and the rough edges take more lines.
|
||||
let roughen = Effect { id: "distort.roughen".into(), params: json!({"size": 40, "relative": false, "detail": 20}), visible: true };
|
||||
let lines = |budget: crate::geometry::Budget| {
|
||||
let mut g = two_squares(json!({}));
|
||||
if let Some(members) = g.children_mut() {
|
||||
let m = std::sync::Arc::make_mut(&mut members[0]);
|
||||
m.appearance.items.retain(AppearanceItem::is_fill);
|
||||
m.appearance.effects.push(roughen.clone());
|
||||
members.truncate(1);
|
||||
}
|
||||
g.appearance.effects.clear();
|
||||
let copies = transform(json!({"scaleH": 200, "scaleV": 200, "copies": 5, "strokes": true}));
|
||||
crate::reshape::reshape_leaves_within(&mut g, &[&copies], budget, None);
|
||||
let (mut count, mut lines) = (0, 0);
|
||||
for leaf in leaves(&g) {
|
||||
count += 1;
|
||||
let Some((path, _)) = crate::reshape::leaf_geometry(leaf) else { continue };
|
||||
let drawn = crate::apply_geometry(&leaf.appearance.effects, &path, path.bounds().unwrap());
|
||||
lines += crate::geometry::Budget::of(&drawn).lines;
|
||||
}
|
||||
(count, lines)
|
||||
};
|
||||
let (all, most) = lines(crate::geometry::Budget::EFFECTS);
|
||||
assert_eq!(all, 6, "the original and 5 copies");
|
||||
let cap = most / 2;
|
||||
let budget = crate::geometry::Budget { anchors: crate::geometry::MAX_EFFECT_ANCHORS, lines: cap };
|
||||
let (count, drawn) = lines(budget);
|
||||
assert!(count < all, "the budget stopped the copies");
|
||||
assert!(drawn <= cap, "{drawn} > {cap}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_groups_copies_are_whole_only_when_nothing_inside_makes_more_art() {
|
||||
let copies = transform(json!({"moveH": 300, "copies": 2}));
|
||||
let effect = |id: &str| Effect { id: id.into(), params: json!({}), visible: true };
|
||||
let roughen = effect("distort.roughen");
|
||||
// Two squares under a group with `change` made to the first member.
|
||||
let whole = |change: &dyn Fn(&mut Node)| {
|
||||
let mut g = two_squares(json!({}));
|
||||
if let Some(members) = g.children_mut() {
|
||||
change(std::sync::Arc::make_mut(&mut members[0]));
|
||||
}
|
||||
g.appearance.effects.clear();
|
||||
crate::reshape::reshape_leaves_within(&mut g, &[&copies], crate::geometry::Budget::EFFECTS, None);
|
||||
g.children().unwrap().len() == 3
|
||||
};
|
||||
// A group of one square with effect `e`, in place of the member.
|
||||
let in_group = |m: &mut Node, e: &Effect| {
|
||||
let square = Node::path(NodeId(40), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default());
|
||||
let mut g = Node::group(NodeId(41), vec![std::sync::Arc::new(square), std::sync::Arc::new(m.clone())]);
|
||||
g.appearance.effects.push(e.clone());
|
||||
*m = g;
|
||||
};
|
||||
assert!(whole(&|_| {}));
|
||||
// Effects that only change pixels, and effects turned off, are copied with the art.
|
||||
assert!(whole(&|m| m.appearance.effects.push(effect("stylize.dropShadow"))));
|
||||
assert!(whole(&|m| m.appearance.effects.push(Effect { visible: false, ..roughen.clone() })));
|
||||
// Effects that draw nothing are copied with the art as well.
|
||||
assert!(
|
||||
whole(&|m| {
|
||||
m.visible = false;
|
||||
m.appearance.effects.push(roughen.clone());
|
||||
}),
|
||||
"a hidden member's effect"
|
||||
);
|
||||
assert!(
|
||||
whole(&|m| {
|
||||
*m.appearance.items[0].paint_mut() = Paint::None;
|
||||
m.appearance.items[0].effects_mut().push(roughen.clone());
|
||||
}),
|
||||
"the effect of a fill that paints nothing"
|
||||
);
|
||||
assert!(whole(&|m| m.appearance.effects.push(effect("no.such.effect"))), "an unknown effect");
|
||||
// Effects that make more art than a copy holds keep each member's copies in its place.
|
||||
assert!(!whole(&|m| m.appearance.effects.push(roughen.clone())), "a member's own effect");
|
||||
assert!(!whole(&|m| m.appearance.items[0].effects_mut().push(roughen.clone())), "a fill's own effect");
|
||||
assert!(!whole(&|m| in_group(m, &roughen)), "a group's own effect");
|
||||
assert!(!whole(&|m| in_group(m, &effect("pathfinder.add"))), "a Pathfinder effect");
|
||||
assert!(
|
||||
!whole(&|m| {
|
||||
let mut art = Node::group(NodeId(43), vec![]);
|
||||
in_group(&mut art, &roughen);
|
||||
m.extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(art, true)));
|
||||
}),
|
||||
"an effect in an opacity mask"
|
||||
);
|
||||
}
|
||||
@@ -218,34 +218,66 @@ fn visible(sources: &[TrapSource]) -> Vec<PathData> {
|
||||
/// The pairs `(i, j)`, `i < j`, in ascending order, of `rects` (`None`: left out) that come within
|
||||
/// `reach_x` of each other across x and `reach_y` across y. A sweep over the rects sorted by their
|
||||
/// left edge, so shapes spread over the page cost about one check each rather than one per pair.
|
||||
fn near_pairs(rects: &[Option<Rect>], reach_x: f64, reach_y: f64) -> Vec<(usize, usize)> {
|
||||
/// `None` when there are more than [`MAX_NEAR_PAIRS`] (many shapes all within reach of each other
|
||||
/// make the pair list grow with the square of their count) or the sweep examines more than
|
||||
/// `MAX_NEAR_SCAN` pairs (many shapes sharing an x range, far apart in y).
|
||||
fn near_pairs(rects: &[Option<Rect>], reach_x: f64, reach_y: f64) -> Option<Vec<(usize, usize)>> {
|
||||
near_pairs_within(rects, reach_x, reach_y, MAX_NEAR_SCAN)
|
||||
}
|
||||
|
||||
fn near_pairs_within(rects: &[Option<Rect>], reach_x: f64, reach_y: f64, max_scan: usize) -> Option<Vec<(usize, usize)>> {
|
||||
let mut order: Vec<(usize, Rect)> = rects.iter().enumerate().filter_map(|(i, r)| r.map(|r| (i, r))).collect();
|
||||
order.sort_unstable_by(|a, b| a.1.x0.total_cmp(&b.1.x0).then(a.0.cmp(&b.0)));
|
||||
let mut pairs = vec![];
|
||||
let mut scanned = 0usize;
|
||||
for (k, &(i, a)) in order.iter().enumerate() {
|
||||
// Later rects start at or right of `a`, so only how far right they start can rule them out.
|
||||
for &(j, b) in order[k + 1..].iter().take_while(|(_, b)| b.x0 <= a.x1 + reach_x) {
|
||||
// Rects sharing an x range but far apart in y are examined without being stored.
|
||||
scanned += 1;
|
||||
if scanned > max_scan {
|
||||
return None;
|
||||
}
|
||||
if b.y0 <= a.y1 + reach_y && a.y0 <= b.y1 + reach_y {
|
||||
if pairs.len() >= MAX_NEAR_PAIRS {
|
||||
return None;
|
||||
}
|
||||
pairs.push((i.min(j), i.max(j)));
|
||||
}
|
||||
}
|
||||
}
|
||||
pairs.sort_unstable();
|
||||
pairs
|
||||
Some(pairs)
|
||||
}
|
||||
|
||||
/// Most pairs the sweep in `near_pairs` examines, stored or not (a plain bounds check each).
|
||||
const MAX_NEAR_SCAN: usize = 1 << 27;
|
||||
|
||||
/// Most candidate pairs one trap evaluation keeps; past it no traps are made (the art draws as is).
|
||||
/// Each pair costs a boolean trap band, so real art stays far below this.
|
||||
pub const MAX_NEAR_PAIRS: usize = 1 << 20;
|
||||
|
||||
/// The traps between `sources` (back to front) with options `o`: for each two sources of
|
||||
/// different solid colours whose visible parts meet, the strip of the darker one's visible part
|
||||
/// next to the lighter one's (the other way round with [`TrapOptions::reverse`]), painted per
|
||||
/// [`trap_paint`]. Empty when no visible edges of different colours meet.
|
||||
/// [`trap_paint`]. Empty when no visible edges of different colours meet, or when there are more
|
||||
/// than [`MAX_NEAR_PAIRS`] candidate pairs or the pair scan is too long (see [`try_traps`] to tell
|
||||
/// those apart).
|
||||
pub fn traps(sources: &[TrapSource], o: &TrapOptions) -> Vec<TrapPiece> {
|
||||
try_traps(sources, o).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// [`traps`], but `None` when there are more than [`MAX_NEAR_PAIRS`] candidate pairs or the scan for
|
||||
/// them is too long (`MAX_NEAR_SCAN` pairs examined), as opposed to
|
||||
/// `Some` of an empty list when no edges meet.
|
||||
pub fn try_traps(sources: &[TrapSource], o: &TrapOptions) -> Option<Vec<TrapPiece>> {
|
||||
let colors: Vec<Option<Color>> = sources.iter().map(|s| trap_color(&s.fill)).collect();
|
||||
if colors.iter().flatten().count() < 2 {
|
||||
return vec![];
|
||||
return Some(vec![]);
|
||||
}
|
||||
let (w, r) = (o.thickness, o.height_width);
|
||||
if !(w.is_finite() && w > 0.0 && r.is_finite() && r > 0.0) {
|
||||
return vec![]; // `trap_band` cuts nothing with these.
|
||||
return Some(vec![]); // `trap_band` cuts nothing with these.
|
||||
}
|
||||
let vis = visible(sources);
|
||||
// Only pairs whose visible parts come within the trap's reach can trap (`trap_band` rejects
|
||||
@@ -257,7 +289,8 @@ pub fn traps(sources: &[TrapSource], o: &TrapOptions) -> Vec<TrapPiece> {
|
||||
let extent = rects.iter().flatten().flat_map(|b| [b.x0, b.y0, b.x1, b.y1]).fold(0.0_f64, |m, v| m.max(v.abs()));
|
||||
let hair = 1e-9 * (1.0 + extent + w * r.max(1.0));
|
||||
let mut out = vec![];
|
||||
for (i, j) in near_pairs(&rects, w + hair, w * r + hair) {
|
||||
let pairs = near_pairs(&rects, w + hair, w * r + hair)?;
|
||||
for (i, j) in pairs {
|
||||
let (Some(ci), Some(cj)) = (colors[i], colors[j]) else { continue };
|
||||
if vis[i].is_empty() || vis[j].is_empty() || same_color(&sources[i].fill, &sources[j].fill, ci, cj) {
|
||||
continue;
|
||||
@@ -276,7 +309,7 @@ pub fn traps(sources: &[TrapSource], o: &TrapOptions) -> Vec<TrapPiece> {
|
||||
let Some(color) = colors[from] else { continue };
|
||||
out.push(TrapPiece { path: band, paint: trap_paint(&sources[from].fill, color, o), from, into });
|
||||
}
|
||||
out
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// A trap's appearance: one overprinting fill of `paint`, no stroke.
|
||||
@@ -379,10 +412,19 @@ mod tests {
|
||||
assert!(near(po::area(&t[0].path, FillRule::NonZero), 22.0 * 22.0 - 400.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_pair_scan_is_bounded_even_when_no_pair_is_near() {
|
||||
// Eleven rects share an x range but lie far apart in y: 55 pairs are examined, none stored.
|
||||
let rects: Vec<Option<Rect>> = (0..11).map(|k| Some(Rect::new(0.0, k as f64 * 1000.0, 10.0, k as f64 * 1000.0 + 10.0))).collect();
|
||||
assert_eq!(near_pairs_within(&rects, 1.0, 1.0, 100), Some(vec![]));
|
||||
assert_eq!(near_pairs_within(&rects, 1.0, 1.0, 54), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_trap_without_adjoining_edges_of_different_colours() {
|
||||
let apart = vec![src(Rect::new(0.0, 0.0, 10.0, 10.0), yellow()), src(Rect::new(30.0, 0.0, 40.0, 10.0), blue())];
|
||||
assert!(traps(&apart, &TrapOptions::default()).is_empty());
|
||||
assert_eq!(try_traps(&apart, &TrapOptions::default()).map(|t| t.len()), Some(0), "no edges meet: empty, not over the cap");
|
||||
let same = vec![src(Rect::new(0.0, 0.0, 10.0, 10.0), blue()), src(Rect::new(10.0, 0.0, 20.0, 10.0), blue())];
|
||||
assert!(traps(&same, &TrapOptions::default()).is_empty());
|
||||
let mut unfilled = abutting();
|
||||
@@ -431,10 +473,13 @@ mod tests {
|
||||
fn only_shapes_within_reach_are_paired() {
|
||||
let s = many_apart_and_one_abutting();
|
||||
let rects: Vec<Option<Rect>> = s.iter().map(|t| t.path.bounds()).collect();
|
||||
assert_eq!(near_pairs(&rects, 1.0, 1.0), vec![(0, 2000)], "the squares 10 pt apart are never paired");
|
||||
assert_eq!(near_pairs(&rects, 1.0, 1.0), Some(vec![(0, 2000)]), "the squares 10 pt apart are never paired");
|
||||
// Reaching 10 pt, neighbours in a row pair, in ascending order; left-out rects never do.
|
||||
let row: Vec<Option<Rect>> = [3, 1, 0, 2].iter().map(|&k| s[k].path.bounds()).chain([None]).collect();
|
||||
assert_eq!(near_pairs(&row, 10.0, 10.0), vec![(0, 3), (1, 2), (1, 3)]);
|
||||
assert_eq!(near_pairs(&row, 10.0, 10.0), Some(vec![(0, 3), (1, 2), (1, 3)]));
|
||||
// Past the allowance the pair list stops growing instead of being built.
|
||||
let packed = vec![Some(Rect::new(0.0, 0.0, 1.0, 1.0)); 1500];
|
||||
assert_eq!(near_pairs(&packed, 1.0, 1.0), None);
|
||||
let t = traps(&s, &TrapOptions { thickness: 1.0, ..Default::default() });
|
||||
assert_eq!(t.len(), 1, "only the abutting pair traps");
|
||||
assert_eq!((t[0].from, t[0].into), (2000, 0));
|
||||
|
||||
@@ -21,6 +21,7 @@ heif = ["dep:heic-rs", "dep:rayon"]
|
||||
[dependencies]
|
||||
# Which paths stored in documents may be opened.
|
||||
craft-docpath = { workspace = true }
|
||||
craft-proof = { workspace = true }
|
||||
vectorcraft-geom = { workspace = true }
|
||||
vectorcraft-color = { workspace = true }
|
||||
vectorcraft-doc = { workspace = true }
|
||||
@@ -39,7 +40,13 @@ vectorcraft-metafile = { workspace = true }
|
||||
vectorcraft-plugins = { workspace = true }
|
||||
vectorcraft-affinity = { workspace = true }
|
||||
vectorcraft-scriptcheck = { workspace = true }
|
||||
vectorcraft-js = { workspace = true }
|
||||
# The commands' parameter schemas (schemas.rs).
|
||||
vectorcraft-apischema = { workspace = true }
|
||||
image = { workspace = true }
|
||||
jxl-oxide = { workspace = true }
|
||||
jxl-color = { workspace = true }
|
||||
jxl-grid = { workspace = true }
|
||||
hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["std", "simd"] }
|
||||
# Pure Rust, no unsafe; pinned at the version photocraft-heif uses.
|
||||
heic-rs = { version = "=0.1.4", default-features = false, features = ["std"], optional = true }
|
||||
@@ -77,5 +84,10 @@ vectorcraft-pdf = { workspace = true, features = ["read-count"] }
|
||||
wat = { workspace = true }
|
||||
# Zstandard streams for the Illustrator editing data fuzz tests.
|
||||
ruzstd = { workspace = true }
|
||||
# JPEG XL files for the `.jxl` open and place tests.
|
||||
zune-jpegxl = { workspace = true }
|
||||
zune-core = { workspace = true }
|
||||
# The JavaScript parser's lexer, compared with the script checker's on the same scripts.
|
||||
vectorcraft-js = { workspace = true }
|
||||
# A bundled font's own glyph boxes, for the glyph bounds tests.
|
||||
skrifa = { workspace = true }
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
pub mod actionset;
|
||||
mod addons;
|
||||
mod apiref;
|
||||
pub(crate) mod appearance;
|
||||
mod assets;
|
||||
mod attributes;
|
||||
@@ -69,6 +70,7 @@ pub mod pdfcmds;
|
||||
pub mod perspgrid;
|
||||
pub mod place;
|
||||
pub mod plugin;
|
||||
pub(crate) mod plugin_call;
|
||||
pub mod prefscmds;
|
||||
pub mod print;
|
||||
pub(crate) mod printadvanced;
|
||||
@@ -82,6 +84,7 @@ mod select;
|
||||
pub(crate) mod shaper;
|
||||
pub mod shortcutset;
|
||||
pub(crate) mod slices;
|
||||
pub mod spelling;
|
||||
mod stroke;
|
||||
mod style;
|
||||
pub mod stylelib;
|
||||
@@ -129,6 +132,9 @@ pub struct CommandSpec {
|
||||
pub run: Run,
|
||||
/// Record in the journal (false for queries and selection-only helpers).
|
||||
pub journal: bool,
|
||||
/// The parameters' schema: [`Session::execute`] checks the params against it before the
|
||||
/// command runs, and `params` is the text it writes ([`crate::schemas`]).
|
||||
pub schema: Option<&'static vectorcraft_apischema::ParamSchema>,
|
||||
}
|
||||
|
||||
/// Serializable command metadata.
|
||||
@@ -142,11 +148,29 @@ pub struct CommandInfo {
|
||||
pub enabled: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub disabled_reason: Option<String>,
|
||||
/// The parameters' schema as a JSON Schema 2020-12 object schema, for a command that has one.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<Value>,
|
||||
/// The same schema in the form an MCP tool's `inputSchema` takes ([`input_schema`]).
|
||||
#[serde(rename = "inputSchema", skip_serializing_if = "Option::is_none")]
|
||||
pub input_schema: Option<Value>,
|
||||
}
|
||||
|
||||
/// `schema` as an MCP tool input schema: the root `type` is `"object"`, since the arguments are
|
||||
/// always an object, and every other keyword is kept. The root of [`CommandInfo::schema`] also
|
||||
/// takes null when no parameter is required.
|
||||
pub(crate) fn input_schema(schema: &Value) -> Value {
|
||||
let mut o = schema.clone();
|
||||
if let Some(map) = o.as_object_mut() {
|
||||
map.insert("type".into(), "object".into());
|
||||
}
|
||||
o
|
||||
}
|
||||
|
||||
impl CommandSpec {
|
||||
pub fn info(&self, s: &Session) -> CommandInfo {
|
||||
let e = (self.enabled)(s);
|
||||
let schema = self.schema.map(vectorcraft_apischema::ParamSchema::to_json);
|
||||
CommandInfo {
|
||||
id: self.id,
|
||||
label: self.label,
|
||||
@@ -155,6 +179,8 @@ impl CommandSpec {
|
||||
params: self.params,
|
||||
enabled: e.is_ok(),
|
||||
disabled_reason: e.err(),
|
||||
input_schema: schema.as_ref().map(input_schema),
|
||||
schema,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,10 +263,17 @@ pub fn has_clipboard(s: &Session) -> std::result::Result<(), String> {
|
||||
|
||||
macro_rules! cmd {
|
||||
($id:literal, $label:literal, [$($m:literal),*], $sc:expr, $params:literal, $en:expr, $run:expr) => {
|
||||
$crate::cmd::CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: $sc, params: $params, enabled: $en, run: $run, journal: true }
|
||||
$crate::cmd::CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: $sc, params: $params, enabled: $en, run: $run, journal: true, schema: None }
|
||||
};
|
||||
(query $id:literal, $label:literal, [$($m:literal),*], $sc:expr, $params:literal, $en:expr, $run:expr) => {
|
||||
$crate::cmd::CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: $sc, params: $params, enabled: $en, run: $run, journal: false }
|
||||
$crate::cmd::CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: $sc, params: $params, enabled: $en, run: $run, journal: false, schema: None }
|
||||
};
|
||||
// With a parameter schema: `schema: &SCHEMA` after the run function.
|
||||
($id:literal, $label:literal, [$($m:literal),*], $sc:expr, $params:literal, $en:expr, $run:expr, schema: $schema:expr) => {
|
||||
$crate::cmd::CommandSpec { schema: Some($schema), ..$crate::cmd::cmd!($id, $label, [$($m),*], $sc, $params, $en, $run) }
|
||||
};
|
||||
(query $id:literal, $label:literal, [$($m:literal),*], $sc:expr, $params:literal, $en:expr, $run:expr, schema: $schema:expr) => {
|
||||
$crate::cmd::CommandSpec { schema: Some($schema), ..$crate::cmd::cmd!(query $id, $label, [$($m),*], $sc, $params, $en, $run) }
|
||||
};
|
||||
}
|
||||
pub(crate) use cmd;
|
||||
@@ -321,6 +354,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
|
||||
v.extend(expand::specs());
|
||||
v.extend(attributes::specs());
|
||||
v.extend(interactivity::specs());
|
||||
v.extend(spelling::specs());
|
||||
v.extend(svgfilter::specs());
|
||||
v.extend(newart::specs());
|
||||
v.extend(colortheme::specs());
|
||||
@@ -352,6 +386,8 @@ pub fn command_specs() -> &'static [CommandSpec] {
|
||||
v.extend(actionset::specs());
|
||||
v.extend(importmatches::specs());
|
||||
v.extend(addons::specs());
|
||||
// Last, so the reference it writes ends with its own section.
|
||||
v.extend(apiref::specs());
|
||||
v
|
||||
})
|
||||
}
|
||||
@@ -368,6 +404,14 @@ pub(crate) fn run_spec(s: &mut Session, spec: &CommandSpec, p: &Value) -> Result
|
||||
if plugin::is_command_id(spec.id) { plugin::run_command(s, spec.id, p) } else { (spec.run)(s, p) }
|
||||
}
|
||||
|
||||
/// [`run_spec`] for a command run outside [`Session::execute`]: with params checked against the
|
||||
/// command's schema, and the defaults of omitted values filled in, as `execute` does
|
||||
/// ([`crate::schemas::check`]).
|
||||
pub(crate) fn run_checked(s: &mut Session, spec: &CommandSpec, p: &Value) -> Result<Value> {
|
||||
let checked = crate::schemas::check(spec, p, s.replaying())?;
|
||||
run_spec(s, spec, checked.as_ref().unwrap_or(p))
|
||||
}
|
||||
|
||||
// ---------- param helpers ----------
|
||||
|
||||
pub(crate) fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
|
||||
|
||||
@@ -275,6 +275,7 @@ fn same_art(a: &NodeKind, b: &NodeKind) -> bool {
|
||||
moved.xf = a.xf;
|
||||
moved.cached_bounds = a.cached_bounds;
|
||||
moved.cached_baselines.clone_from(&a.cached_baselines);
|
||||
moved.cached_ink = a.cached_ink;
|
||||
**a == moved
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,7 +544,11 @@ fn merge(chain: &[&Piece]) -> std::result::Result<TextObject, usize> {
|
||||
for (k, w) in off.windows(2).enumerate() {
|
||||
let (delta, Some(&(s, e))) = (w[1] - w[0], before.get(k)) else { continue };
|
||||
if delta.abs() > 1e-9 {
|
||||
edit::style_range(&mut m.runs, s, e, |st| st.tracking += delta / st.size.max(1e-6) * 1000.0);
|
||||
// Thousandths of the em as the Horizontal Scale draws it (`spacing_scale`).
|
||||
let vertical = m.vertical;
|
||||
edit::style_range(&mut m.runs, s, e, |st| {
|
||||
st.tracking += delta / (st.size * vectorcraft_text::spacing_scale(st, vertical)).max(1e-6) * 1000.0;
|
||||
});
|
||||
if !TRACKING.contains(&edit::style_at(&m.runs, s).tracking) {
|
||||
return Err(k + 1);
|
||||
}
|
||||
|
||||
@@ -176,6 +176,24 @@ fn combine_joins_only_pieces_on_one_line_turned_alike() {
|
||||
assert!(other(s.execute("addon.cleanup.combineText", &json!({}))).contains("combine"));
|
||||
}
|
||||
|
||||
/// Pieces in horizontally scaled type: the tracking that puts each back where it was is measured in
|
||||
/// the em as the Horizontal Scale draws it (`vectorcraft_text::spacing_scale`), as layout spaces it.
|
||||
#[test]
|
||||
fn combine_puts_horizontally_scaled_pieces_back_where_they_were() {
|
||||
let mut s = session();
|
||||
let a = text(&mut s, json!({"x": 50, "y": 100, "text": "Left", "size": 20}));
|
||||
let b = text(&mut s, json!({"x": 160, "y": 100, "text": "Right", "size": 20}));
|
||||
for id in [a, b] {
|
||||
s.execute("text.setRangeStyle", &json!({"id": id.0, "hScale": 200})).unwrap();
|
||||
}
|
||||
let was = glyphs(&s, &[a, b]);
|
||||
select(&mut s, &[a, b]);
|
||||
let r = s.execute("addon.cleanup.combineText", &json!({})).unwrap();
|
||||
assert_eq!(ids(&r), [a]);
|
||||
assert_eq!(obj(&s, a).plain_text(), "Left Right");
|
||||
assert_same_places(&was, &glyphs(&s, &[a]));
|
||||
}
|
||||
|
||||
/// Join Point Text makes one object of stacked one-line pieces: lines top to bottom as paragraphs,
|
||||
/// pieces on a line left to right, each line's baseline where it was (its leading is the distance
|
||||
/// from the line above), in one undo step. A piece turned unlike the top-left one stays.
|
||||
|
||||
@@ -12,8 +12,8 @@ use vectorcraft_doc::NodeKind;
|
||||
use vectorcraft_render::effects;
|
||||
|
||||
use super::super::edit::{copy_above, selected_roots};
|
||||
use super::super::pathops::{len_param, num_param};
|
||||
use super::super::{CommandSpec, EngineError, NodeId, Result, Session, bad, cmd, has_selection, str_param};
|
||||
use super::super::pathops::{length_given, number_given};
|
||||
use super::super::{CommandSpec, EngineError, NodeId, Result, Session, bad, cmd, has_selection, text_param};
|
||||
|
||||
const C: &str = "addon.contour.set";
|
||||
/// The live effect that does the offsetting (the one Effect › Path › Offset Path applies).
|
||||
@@ -44,12 +44,12 @@ fn joins_for(corners: Option<&str>) -> Result<&'static str> {
|
||||
}
|
||||
|
||||
fn contour(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let offset = len_param(p, "offset").ok_or_else(|| bad(C, "missing `offset` (a distance in points)"))?;
|
||||
let offset = length_given(p, "offset", C)?.ok_or_else(|| bad(C, "missing `offset` (a distance in points)"))?;
|
||||
if offset.abs() > MAX_OFFSET {
|
||||
return Err(bad(C, "offset out of range"));
|
||||
}
|
||||
let joins = joins_for(str_param(p, "corners"))?;
|
||||
let miter = num_param(p, "miterLimit").unwrap_or(4.0).clamp(1.0, 500.0);
|
||||
let joins = joins_for(text_param(p, "corners", C)?)?;
|
||||
let miter = number_given(p, "miterLimit", C)?.unwrap_or(4.0).clamp(1.0, 500.0);
|
||||
let effect = effects::new_effect(EFFECT, &json!({"offset": offset, "joins": joins, "miterLimit": miter}))
|
||||
.ok_or_else(|| EngineError::Other("Contour: the Offset Path effect is not available".into()))?;
|
||||
let roots = selected_roots(s)?;
|
||||
|
||||
@@ -158,6 +158,39 @@ fn contour_refuses_what_it_cannot_offset() {
|
||||
assert!(err.contains("select paths or shapes"), "{err}");
|
||||
}
|
||||
|
||||
/// A param of the wrong type is an error naming the param, and leaves the document, the selection
|
||||
/// and the undo history as they were. Null keeps a param's default, and `offset` still takes a
|
||||
/// string with a unit.
|
||||
#[test]
|
||||
fn contour_refuses_a_param_of_the_wrong_type() {
|
||||
let mut s = session();
|
||||
rect(&mut s, 100.0, 100.0, 50.0, 40.0);
|
||||
let state = |s: &Session| {
|
||||
let st = s.doc().unwrap();
|
||||
(st.doc.clone(), st.selection.objects.clone(), st.history.undo.len())
|
||||
};
|
||||
let before = state(&s);
|
||||
for (p, want) in [
|
||||
(json!({"offset": true}), "`offset` must be a number, not true"),
|
||||
(json!({"offset": "abc"}), "`offset` must be a number, not \"abc\""),
|
||||
(json!({"offset": [5]}), "`offset` must be a number, not [5]"),
|
||||
(json!({"offset": 5, "corners": 3}), "`corners` must be a string"),
|
||||
(json!({"offset": 5, "miterLimit": "high"}), "`miterLimit` must be a number, not \"high\""),
|
||||
(json!({"offset": 5, "miterLimit": false}), "`miterLimit` must be a number, not false"),
|
||||
] {
|
||||
let r = s.execute("addon.contour.set", &p);
|
||||
assert!(matches!(&r, Err(EngineError::BadParams { msg, .. }) if msg == want), "{p}: {r:?}");
|
||||
assert!(state(&s) == before, "{p} changed the document, the selection or the undo history");
|
||||
}
|
||||
// A null `offset` is a missing one.
|
||||
let err = s.execute("addon.contour.set", &json!({"offset": null})).unwrap_err().to_string();
|
||||
assert!(err.contains("missing `offset`"), "{err}");
|
||||
// Null corners and miter limit keep sharp corners at 4; the offset takes a unit.
|
||||
let r = s.execute("addon.contour.set", &json!({"offset": "1 in", "corners": null, "miterLimit": null})).unwrap();
|
||||
let fx = effects_of(&s, ids_of(&r, "created")[0]);
|
||||
assert_eq!((fx[0].1["offset"].clone(), fx[0].1["joins"].clone(), fx[0].1["miterLimit"].clone()), (json!(72.0), json!("miter"), json!(4.0)));
|
||||
}
|
||||
|
||||
/// The border-with-a-gap recipe: two Contours of one shape (the gap, then the border), baked, then
|
||||
/// the gap taken out of the border with Minus Front, leaves a ring between the two.
|
||||
#[test]
|
||||
|
||||
@@ -774,8 +774,9 @@ fn divide_dashes(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
continue;
|
||||
}
|
||||
let only = (segs.len() < sp.segment_count()).then_some(&segs);
|
||||
let out = pt::divide_dashes(sp, only, spec, MAX_PIECES.saturating_sub(made))
|
||||
.ok_or_else(|| bad(C, "that makes too many pieces (at most 100000): make the dashes longer"))?;
|
||||
let out = pt::divide_dashes(sp, only, spec, MAX_PIECES.saturating_sub(made)).ok_or_else(|| {
|
||||
bad(C, "that makes too many pieces (at most 100000) or dash steps: simplify the dash pattern or make the dashes longer")
|
||||
})?;
|
||||
made += out.len();
|
||||
for (piece, kind) in out.into_iter().filter(|(_, k)| !(remove_gaps && *k == pt::Piece::Gap)) {
|
||||
dashes += usize::from(kind == pt::Piece::Dash);
|
||||
@@ -825,7 +826,7 @@ fn dashed_paths(d: &Document, roots: &[NodeId]) -> Vec<NodeId> {
|
||||
|
||||
fn expand_dashes(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "addon.pathtools.expandDashes";
|
||||
let too_many = || bad(C, "that makes too many pieces (at most 100000): make the dashes longer");
|
||||
let too_many = || bad(C, "that makes too many pieces (at most 100000) or dash steps: simplify the dash pattern or make the dashes longer");
|
||||
let roots = checked_ids_or_selection(s, p, C)?;
|
||||
let d = &s.doc()?.doc;
|
||||
// Each dashed path and its dashes, as the canvas draws them.
|
||||
|
||||
@@ -0,0 +1,461 @@
|
||||
//! `engine.apiReference`: the command API reference in Markdown, written when it is asked for from
|
||||
//! the parameter schemas of the built-in commands ([`crate::schemas`]). Each command that has a
|
||||
//! schema gets a section, in registry order, with its parameters in the schema's order; a command
|
||||
//! without a schema, a plug-in's included, is left out. The text depends only on the registry, so
|
||||
//! every call in one build returns the same bytes.
|
||||
//!
|
||||
//! Text from a schema is escaped before it goes into a heading, a list item or a table cell: the
|
||||
//! Markdown punctuation characters get a backslash (CommonMark 0.31.2, backslash escapes,
|
||||
//! <https://spec.commonmark.org/0.31.2/#backslash-escapes>), so a description shows as plain text and
|
||||
//! cannot end a table cell or make a link, an image or HTML; line breaks, tabs and other control
|
||||
//! characters become spaces.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_apischema::{Bound, MAX_DEPTH, MAX_ID, Omitted, Param, ParamSchema, Type, Writes};
|
||||
|
||||
use super::{CommandSpec, Result, Session, always, cmd, command_specs};
|
||||
use crate::EngineError;
|
||||
|
||||
const ID: &str = "engine.apiReference";
|
||||
|
||||
/// The largest reference the command returns, in bytes of UTF-8.
|
||||
pub const MAX_BYTES: usize = 1 << 20;
|
||||
|
||||
static REFERENCE: ParamSchema = ParamSchema::new(
|
||||
&[],
|
||||
"the command API reference in Markdown, written from the built-in commands' parameter schemas: a section for each command that has one, in registry order",
|
||||
)
|
||||
.returns("{markdown: string}")
|
||||
.writes(Writes::Nothing)
|
||||
.since(2);
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![cmd!(
|
||||
query "engine.apiReference",
|
||||
"Command API Reference",
|
||||
[],
|
||||
None,
|
||||
"{} the command API reference in Markdown, written from the built-in commands' parameter schemas: a section for each command that has one, in registry order → {markdown: string}",
|
||||
always,
|
||||
api_reference,
|
||||
schema: &REFERENCE
|
||||
)]
|
||||
}
|
||||
|
||||
fn api_reference(_: &mut Session, _: &Value) -> Result<Value> {
|
||||
Ok(json!({ "markdown": reference(command_specs())? }))
|
||||
}
|
||||
|
||||
const HEADER: &str = "# VectorCraft command API reference\n\nThis reference lists only the commands that have a parameter schema, in registry order. `engine.commands` lists every command, with its params text and whether it can run now.\n";
|
||||
|
||||
/// The reference for the commands of `specs` that have a schema, or an error when it would be
|
||||
/// larger than [`MAX_BYTES`]; a reference that is too large is never cut short.
|
||||
pub(crate) fn reference(specs: &[CommandSpec]) -> Result<String> {
|
||||
let mut out = String::from(HEADER);
|
||||
let mut sections = 0;
|
||||
for spec in specs {
|
||||
let Some(schema) = spec.schema else { continue };
|
||||
let text = section(spec, schema);
|
||||
if out.len() + text.len() > MAX_BYTES {
|
||||
return Err(EngineError::Other(format!("`{ID}`: the reference would be larger than {MAX_BYTES} bytes")));
|
||||
}
|
||||
out.push_str(&text);
|
||||
sections += 1;
|
||||
}
|
||||
if sections == 0 {
|
||||
out.push_str("\nNo commands have parameter schemas.\n");
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// One command's section: its heading, a list of what it is, and its parameters.
|
||||
fn section(spec: &CommandSpec, schema: &ParamSchema) -> String {
|
||||
let menu = if spec.menu.is_empty() { "None".to_string() } else { spec.menu.iter().map(|m| escape(m)).collect::<Vec<_>>().join(" › ") };
|
||||
let mut s = format!("\n## {}\n\n", escape(spec.id));
|
||||
s.push_str(&format!("- Label: {}\n", escape(spec.label)));
|
||||
s.push_str(&format!("- Menu: {menu}\n"));
|
||||
s.push_str(&format!("- API level: {}\n", schema.since));
|
||||
s.push_str(&format!("- Deprecated: {}\n", level_text(schema.deprecated)));
|
||||
s.push_str(&format!("- Writes: {}\n", writes_text(schema.writes)));
|
||||
s.push_str(&format!("- Cost: {}\n", schema.cost));
|
||||
s.push_str(&format!("- Summary: {}\n", text_or_none(schema.summary)));
|
||||
s.push_str(&format!("- Returns: {}\n\n", text_or_none(schema.returns)));
|
||||
if schema.params.is_empty() {
|
||||
s.push_str("No parameters.\n");
|
||||
return s;
|
||||
}
|
||||
s.push_str("| Parameter | Type | Range | Unit | Default | Required | Since | Deprecated | Description |\n");
|
||||
s.push_str("|---|---|---|---|---|---|---|---|---|\n");
|
||||
rows("", schema.params, 0, &mut s);
|
||||
s
|
||||
}
|
||||
|
||||
/// A table row for each of `params`, and after each one the rows of the members of an object it
|
||||
/// holds: `edits[].name` for an object in a list, `options.size` for an object.
|
||||
fn rows(prefix: &str, params: &[Param], depth: usize, out: &mut String) {
|
||||
for p in params {
|
||||
let name = if prefix.is_empty() { p.name.to_string() } else { format!("{prefix}.{}", p.name) };
|
||||
let (range, unit) = range_and_unit(&p.ty);
|
||||
out.push_str(&format!(
|
||||
"| {} | {} | {range} | {unit} | {} | {} | {} | {} | {} |\n",
|
||||
escape(&name),
|
||||
type_text(&p.ty, depth),
|
||||
default_text(p.omitted),
|
||||
if p.required { "Yes" } else { "No" },
|
||||
p.since,
|
||||
level_text(p.deprecated),
|
||||
description(p),
|
||||
));
|
||||
if depth < MAX_DEPTH
|
||||
&& let Some((members, path)) = members(&p.ty, name, depth)
|
||||
{
|
||||
rows(&path, members, depth + 1, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The parameters of the object `ty` holds, directly or as the items of lists, with the path of
|
||||
/// its members' prefix (`name`, `name[]`, `name[][]`).
|
||||
fn members(ty: &Type, mut path: String, depth: usize) -> Option<(&'static [Param], String)> {
|
||||
let mut ty = ty;
|
||||
for _ in depth..MAX_DEPTH {
|
||||
match ty {
|
||||
Type::Object(params) => return Some((params, path)),
|
||||
Type::Array(item) => {
|
||||
path.push_str("[]");
|
||||
ty = item;
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn type_text(ty: &Type, depth: usize) -> String {
|
||||
match ty {
|
||||
Type::Number { .. } => "number".into(),
|
||||
Type::Integer { .. } => "integer".into(),
|
||||
Type::Boolean => "boolean".into(),
|
||||
Type::String => "string".into(),
|
||||
Type::Enum(names) => format!("one of {}", names.iter().map(|n| format!("\"{}\"", escape(n))).collect::<Vec<_>>().join(", ")),
|
||||
Type::Id => "object id (integer)".into(),
|
||||
Type::Ids => "list of object ids".into(),
|
||||
Type::Color => escape("color (a string, [r, g, b] or an object)"),
|
||||
Type::Point => escape("point ([x, y], two numbers)"),
|
||||
Type::Object(_) => "object".into(),
|
||||
Type::Array(_) if depth >= MAX_DEPTH => "list".into(),
|
||||
Type::Array(item) => format!("list of {}", type_text(item, depth + 1)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The range and unit cells of a value of type `ty`.
|
||||
fn range_and_unit(ty: &Type) -> (String, String) {
|
||||
let ends = |low: Option<String>, high: Option<String>| {
|
||||
let both: Vec<String> = low.into_iter().chain(high).collect();
|
||||
if both.is_empty() { "None".to_string() } else { both.join(", ") }
|
||||
};
|
||||
match ty {
|
||||
Type::Number { min, max, unit } => {
|
||||
let low = min.map(|b| match b {
|
||||
Bound::Inclusive(a) => format!("at least {a}"),
|
||||
Bound::Exclusive(a) => format!("more than {a}"),
|
||||
});
|
||||
let high = max.map(|b| match b {
|
||||
Bound::Inclusive(a) => format!("at most {a}"),
|
||||
Bound::Exclusive(a) => format!("less than {a}"),
|
||||
});
|
||||
(ends(low, high), unit.map_or("None".to_string(), escape))
|
||||
}
|
||||
Type::Integer { min, max } => (ends(min.map(|a| format!("at least {a}")), max.map(|a| format!("at most {a}"))), "None".into()),
|
||||
Type::Id => (format!("at least 0, at most {MAX_ID}"), "None".into()),
|
||||
_ => ("None".into(), "None".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn default_text(omitted: Omitted) -> String {
|
||||
match omitted {
|
||||
Omitted::Nothing => "None".into(),
|
||||
Omitted::Value(json) => format!("{} (JSON)", escape(json)),
|
||||
Omitted::Note(note) => format!("{} (worked out by the command)", escape(note)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The parameter's doc text, and what the schema says about how its value is checked.
|
||||
fn description(p: &Param) -> String {
|
||||
let mut parts = Vec::new();
|
||||
if !p.doc.is_empty() {
|
||||
parts.push(escape(p.doc));
|
||||
}
|
||||
if p.null_omitted {
|
||||
parts.push("null reads as omitted".to_string());
|
||||
}
|
||||
if p.lenient {
|
||||
parts.push("lenient (a value of another type or out of range is passed to the command, not refused)".to_string());
|
||||
}
|
||||
if parts.is_empty() { "None".to_string() } else { parts.join("; ") }
|
||||
}
|
||||
|
||||
fn level_text(level: Option<u32>) -> String {
|
||||
level.map_or("None".to_string(), |l| format!("API level {l}"))
|
||||
}
|
||||
|
||||
fn writes_text(writes: Writes) -> &'static str {
|
||||
match writes {
|
||||
Writes::Nothing => "nothing",
|
||||
Writes::Session => "session",
|
||||
Writes::Preferences => "preferences",
|
||||
Writes::Document => "document",
|
||||
}
|
||||
}
|
||||
|
||||
fn text_or_none(text: &str) -> String {
|
||||
if text.is_empty() { "None".to_string() } else { escape(text) }
|
||||
}
|
||||
|
||||
/// The characters [`escape`] puts a backslash before.
|
||||
const ESCAPED: &[char] = &['\\', '|', '[', ']', '<', '>', '`', '*', '_', '!', '&', '#', '~', ':', '@'];
|
||||
|
||||
/// `text` as plain Markdown text on one line: each of [`ESCAPED`] with a backslash before it, the
|
||||
/// `.` after `www` too (so no autolink starts), and each control character as a space.
|
||||
pub(crate) fn escape(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
for c in text.chars() {
|
||||
if c.is_control() {
|
||||
out.push(' ');
|
||||
continue;
|
||||
}
|
||||
let after_www = out.len().checked_sub(3).and_then(|at| out.as_bytes().get(at..)).is_some_and(|b| b.eq_ignore_ascii_case(b"www"));
|
||||
if ESCAPED.contains(&c) || (c == '.' && after_www) {
|
||||
out.push('\\');
|
||||
}
|
||||
out.push(c);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn run(_: &mut Session, _: &Value) -> Result<Value> {
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
fn spec(id: &'static str, menu: &'static [&'static str], schema: Option<&'static ParamSchema>) -> CommandSpec {
|
||||
CommandSpec { id, label: "Label", menu, shortcut: None, params: "", enabled: always, run, journal: false, schema }
|
||||
}
|
||||
|
||||
/// The cells of a table row as a GFM table reads them: split at each `|` that has no backslash
|
||||
/// before it, with the outer pipes and the spaces around each cell taken off.
|
||||
fn cells(row: &str) -> Vec<String> {
|
||||
let inner = row.strip_prefix('|').and_then(|r| r.strip_suffix('|')).unwrap_or_else(|| panic!("not a table row: {row}"));
|
||||
let (mut out, mut cell, mut chars) = (Vec::new(), String::new(), inner.chars());
|
||||
while let Some(c) = chars.next() {
|
||||
match c {
|
||||
'\\' => {
|
||||
cell.push(c);
|
||||
cell.extend(chars.next());
|
||||
}
|
||||
'|' => out.push(std::mem::take(&mut cell).trim().to_string()),
|
||||
_ => cell.push(c),
|
||||
}
|
||||
}
|
||||
out.push(cell.trim().to_string());
|
||||
out
|
||||
}
|
||||
|
||||
/// What `text` shows as: each escaped character as itself. Panics on a Markdown punctuation
|
||||
/// character without its backslash, or a backslash before a character that needs none.
|
||||
fn literal(text: &str) -> String {
|
||||
let (mut out, mut chars) = (String::new(), text.chars());
|
||||
while let Some(c) = chars.next() {
|
||||
assert!(!ESCAPED.contains(&c) || c == '\\', "`{c}` is not escaped in {text}");
|
||||
if c == '\\' {
|
||||
let next = chars.next().unwrap_or_else(|| panic!("a backslash ends {text}"));
|
||||
assert!(ESCAPED.contains(&next) || next == '.', "`\\{next}` in {text}");
|
||||
out.push(next);
|
||||
} else {
|
||||
out.push(c);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The shown cells of the row of parameter `name`.
|
||||
fn row(markdown: &str, name: &str) -> Vec<String> {
|
||||
let found = markdown.lines().filter(|l| l.starts_with('|')).find(|l| cells(l).first().map(|c| literal(c)).as_deref() == Some(name));
|
||||
let row = found.unwrap_or_else(|| panic!("no row for {name} in\n{markdown}"));
|
||||
let cells = cells(row);
|
||||
assert_eq!(cells.len(), 9, "{row}");
|
||||
cells.iter().map(|c| literal(c)).collect()
|
||||
}
|
||||
|
||||
/// The text after `- {label}: ` in the section's list.
|
||||
fn item(markdown: &str, label: &str) -> String {
|
||||
let prefix = format!("- {label}: ");
|
||||
let line = markdown.lines().find_map(|l| l.strip_prefix(prefix.as_str()));
|
||||
literal(line.unwrap_or_else(|| panic!("no {label} in\n{markdown}")))
|
||||
}
|
||||
|
||||
/// Two calls give the same bytes, with no document open, and the reference has one section
|
||||
/// for each built-in command that has a schema, in registry order, ending with this command's.
|
||||
#[test]
|
||||
fn the_reference_lists_each_schema_once_in_registry_order_and_is_the_same_each_time() {
|
||||
let mut s = Session::new();
|
||||
let first = s.execute(ID, &json!({})).unwrap();
|
||||
let second = s.execute(ID, &Value::Null).unwrap();
|
||||
assert_eq!(first, second);
|
||||
let markdown = first["markdown"].as_str().unwrap();
|
||||
assert_eq!(markdown, reference(command_specs()).unwrap());
|
||||
assert!(markdown.len() < MAX_BYTES / 10, "{} bytes", markdown.len());
|
||||
let headings: Vec<String> = markdown.lines().filter_map(|l| l.strip_prefix("## ")).map(literal).collect();
|
||||
let expected: Vec<&str> = command_specs().iter().filter(|c| c.schema.is_some()).map(|c| c.id).collect();
|
||||
assert_eq!(headings, expected);
|
||||
assert_eq!(headings.last().map(String::as_str), Some(ID));
|
||||
assert!(expected.contains(&"view.saved.new") && !headings.iter().any(|h| h == "file.new"));
|
||||
let new_view = &markdown[markdown.find("## view.saved.new").unwrap()..];
|
||||
let names: Vec<String> = new_view
|
||||
.lines()
|
||||
.skip_while(|l| !l.starts_with("|---"))
|
||||
.skip(1)
|
||||
.take_while(|l| l.starts_with('|'))
|
||||
.map(|l| literal(&cells(l)[0]))
|
||||
.collect();
|
||||
assert_eq!(names, ["name", "center", "zoom", "rotation"]);
|
||||
let edit = &markdown[markdown.find("## view.saved.edit").unwrap()..];
|
||||
assert_eq!(row(edit, "edits[].newName")[8], "null reads as omitted");
|
||||
assert_eq!(row(edit, "newName")[8], "rename; null reads as omitted");
|
||||
}
|
||||
|
||||
const INNER: &[Param] =
|
||||
&[Param::new("size", Type::Integer { min: Some(1), max: Some(10) }), Param::new("mode", Type::Enum(&["a", "b"])).defaults_to("\"a\"")];
|
||||
|
||||
static FULL: ParamSchema = ParamSchema::new(
|
||||
&[
|
||||
Param::new("zoom", Type::Number { min: Some(Bound::Exclusive(0.0)), max: Some(Bound::Inclusive(64.0)), unit: Some("pt") })
|
||||
.doc("1 = 100%"),
|
||||
Param::new("name", Type::String).default_note("the first free name").null_omitted(),
|
||||
Param::new("old", Type::Boolean).defaults_to("true").since(2).deprecated(3).lenient(),
|
||||
Param::new("edits", Type::Array(&Type::Object(INNER))).optional(),
|
||||
Param::new("ids", Type::Ids).optional(),
|
||||
Param::new("at", Type::Point).optional(),
|
||||
Param::new("color", Type::Color).optional(),
|
||||
Param::new("id", Type::Id).optional(),
|
||||
Param::new("ratio", Type::Number { min: Some(Bound::Inclusive(-1.5)), max: Some(Bound::Exclusive(2.0)), unit: None }).optional(),
|
||||
],
|
||||
"do the thing",
|
||||
)
|
||||
.returns("{done}")
|
||||
.writes(Writes::Preferences)
|
||||
.since(2)
|
||||
.deprecated(4)
|
||||
.cost(5);
|
||||
|
||||
/// Each field of a schema shows, nested members in their order under dotted names.
|
||||
#[test]
|
||||
fn every_field_of_a_schema_is_shown() {
|
||||
let markdown = reference(&[spec("test.full", &["View", "Saved"], Some(&FULL))]).unwrap();
|
||||
for (label, shown) in [
|
||||
("Label", "Label"),
|
||||
("Menu", "View › Saved"),
|
||||
("API level", "2"),
|
||||
("Deprecated", "API level 4"),
|
||||
("Writes", "preferences"),
|
||||
("Cost", "5"),
|
||||
("Summary", "do the thing"),
|
||||
("Returns", "{done}"),
|
||||
] {
|
||||
assert_eq!(item(&markdown, label), shown, "{label}");
|
||||
}
|
||||
let lenient = "lenient (a value of another type or out of range is passed to the command, not refused)";
|
||||
let expected: [[&str; 9]; 11] = [
|
||||
["zoom", "number", "more than 0, at most 64", "pt", "None", "Yes", "1", "None", "1 = 100%"],
|
||||
["name", "string", "None", "None", "the first free name (worked out by the command)", "No", "1", "None", "null reads as omitted"],
|
||||
["old", "boolean", "None", "None", "true (JSON)", "No", "2", "API level 3", lenient],
|
||||
["edits", "list of object", "None", "None", "None", "No", "1", "None", "None"],
|
||||
["edits[].size", "integer", "at least 1, at most 10", "None", "None", "Yes", "1", "None", "None"],
|
||||
["edits[].mode", "one of \"a\", \"b\"", "None", "None", "\"a\" (JSON)", "No", "1", "None", "None"],
|
||||
["ids", "list of object ids", "None", "None", "None", "No", "1", "None", "None"],
|
||||
["at", "point ([x, y], two numbers)", "None", "None", "None", "No", "1", "None", "None"],
|
||||
["color", "color (a string, [r, g, b] or an object)", "None", "None", "None", "No", "1", "None", "None"],
|
||||
["id", "object id (integer)", "at least 0, at most 9007199254740991", "None", "None", "No", "1", "None", "None"],
|
||||
["ratio", "number", "at least -1.5, less than 2", "None", "None", "No", "1", "None", "None"],
|
||||
];
|
||||
let names: Vec<String> =
|
||||
markdown.lines().filter(|l| l.starts_with("| ") && !l.starts_with("| Parameter")).map(|l| literal(&cells(l)[0])).collect();
|
||||
assert_eq!(names, expected.map(|r| r[0]));
|
||||
for want in expected {
|
||||
assert_eq!(row(&markdown, want[0]), want, "{}", want[0]);
|
||||
}
|
||||
}
|
||||
|
||||
const HOSTILE: &str = "a | b [link](https://evil.example) <script>x</script> `code` \\ *em* _u_  & ~~s~~ # h www.example.com [email protected]\r\nnext\tline";
|
||||
|
||||
static ESCAPES: ParamSchema =
|
||||
ParamSchema::new(&[Param::new("a|b", Type::String).doc(HOSTILE).defaults_to("\"<b>|</b>\"")], HOSTILE).returns(HOSTILE);
|
||||
|
||||
/// Markdown in a schema's text shows as plain text: a description stays in its own cell of a
|
||||
/// nine-cell row, every punctuation character is escaped, and line breaks and tabs are spaces.
|
||||
#[test]
|
||||
fn markdown_in_schema_text_stays_literal_in_its_cell() {
|
||||
let markdown = reference(&[spec("test.escapes", &["A|B", "[x](y)"], Some(&ESCAPES))]).unwrap();
|
||||
let shown = HOSTILE.replace(['\r', '\n', '\t'], " ");
|
||||
let cells = row(&markdown, "a|b");
|
||||
assert_eq!(cells[4], "\"<b>|</b>\" (JSON)");
|
||||
assert_eq!(cells[8], shown);
|
||||
assert_eq!(item(&markdown, "Summary"), shown);
|
||||
assert_eq!(item(&markdown, "Returns"), shown);
|
||||
assert_eq!(item(&markdown, "Menu"), "A|B › [x](y)");
|
||||
assert!(markdown.contains("www\\.example"), "{markdown}");
|
||||
let table: Vec<&str> = markdown.lines().filter(|l| l.starts_with('|')).collect();
|
||||
assert_eq!(table.len(), 3, "the header, the delimiter row and one row: {table:?}");
|
||||
assert!(!markdown.contains('\r') && !markdown.contains('\t'));
|
||||
}
|
||||
|
||||
/// A registry with no schema gives the header and a line that says so; a command without a
|
||||
/// schema adds nothing.
|
||||
#[test]
|
||||
fn a_registry_without_schemas_gives_a_document_that_says_so() {
|
||||
let empty = format!("{HEADER}\nNo commands have parameter schemas.\n");
|
||||
assert_eq!(reference(&[]).unwrap(), empty);
|
||||
assert_eq!(reference(&[spec("test.none", &["View"], None)]).unwrap(), empty);
|
||||
let one = reference(&[spec("test.none", &[], None), spec("test.full", &[], Some(&FULL)), spec("test.other", &[], None)]).unwrap();
|
||||
assert_eq!(one, reference(&[spec("test.full", &[], Some(&FULL))]).unwrap());
|
||||
}
|
||||
|
||||
/// A reference of exactly [`MAX_BYTES`] is returned; one byte more is an error naming the
|
||||
/// command, with no part of the reference.
|
||||
#[test]
|
||||
fn a_reference_past_the_limit_is_an_error() {
|
||||
let with = |summary: String| -> Result<String> {
|
||||
let schema: &'static ParamSchema = Box::leak(Box::new(ParamSchema::new(&[], Box::leak(summary.into_boxed_str()))));
|
||||
reference(&[spec("test.big", &[], Some(schema))])
|
||||
};
|
||||
let base = with("x".into()).unwrap().len();
|
||||
let fits = with("x".repeat(MAX_BYTES - base + 1)).unwrap();
|
||||
assert_eq!(fits.len(), MAX_BYTES);
|
||||
let e = with("x".repeat(MAX_BYTES - base + 2)).unwrap_err().to_string();
|
||||
assert!(e.contains(ID) && e.contains("larger than 1048576 bytes"), "{e}");
|
||||
assert!(e.len() < 200, "{e}");
|
||||
}
|
||||
|
||||
/// An unknown key is refused with a parameter error naming it, and the command needs no
|
||||
/// document.
|
||||
#[test]
|
||||
fn an_unknown_parameter_is_refused_naming_it() {
|
||||
let mut s = Session::new();
|
||||
let e = s.execute(ID, &json!({"prefix": "view."})).unwrap_err().to_string();
|
||||
assert!(e.contains(ID) && e.contains("`prefix`"), "{e}");
|
||||
assert!(s.execute(ID, &json!([])).is_err());
|
||||
assert!(s.active().is_none());
|
||||
assert!(s.execute(ID, &json!({})).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_marks_each_punctuation_character_and_flattens_control_characters() {
|
||||
assert_eq!(escape("a|b"), "a\\|b");
|
||||
assert_eq!(escape("WWW.x and w.x"), "WWW\\.x and w.x");
|
||||
assert_eq!(escape("é\u{7}\u{85}z"), "é z");
|
||||
assert_eq!(escape("1.5 > 1"), "1.5 \\> 1");
|
||||
}
|
||||
}
|
||||
@@ -126,7 +126,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Eyedropper",
|
||||
[],
|
||||
None,
|
||||
"{source: id, ids?, pickUp?, apply? (trees as eyedropper.setOptions, merged over the Eyedropper Options for this call), reverse?: bool, append?: bool, defaultsOnly?: bool} copy the attributes both picked up and applied (by default the whole appearance stack, opacity and blend mode, and from type to type the character and paragraph attributes) from `source` to ids (default: highlighted characters while editing type, after Select All in a frame of threaded text the text of the story's frames, leaving out `source`, otherwise the object selection; highlighted characters reject enabled non-neutral object transparency), the fill, stroke and weight also to the paint defaults; `defaultsOnly` updates only paint defaults and cannot be combined with ids, reverse or append; `reverse` (Alt-click) copies from the first selected object (or ids) onto `source` instead, or with nothing selected what the Eyedropper picked up last (a click without Alt); `append` (Shift+Alt-click) adds the source's fills, strokes and effects on top of each target's stack; placed gradients land at the same place relative to each target's bounds (defaults: fitted to new art); with an object of a dynamic symbol instance selected by Direct Selection and no ids, reverse, append or defaultsOnly, that object takes the source's solid fill and stroke for this instance only, as the Eyedropper Options allow, and a sampled gradient or pattern, a source that is not a path, compound path or type, or options that pick up neither color, are refused. On a path directly in a graph series, the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke and the object's opacity and blend mode (exceptions: docs/mcp.md) → {ids}",
|
||||
"{source: id, ids?, pickUp?, apply? (trees as eyedropper.setOptions, merged over the Eyedropper Options for this call), reverse?: bool, append?: bool, defaultsOnly?: bool} copy the attributes both picked up and applied (by default the whole appearance stack, opacity and blend mode, and from type to type the character and paragraph attributes) from `source` to ids (default: highlighted characters while editing type, after Select All in a frame of threaded text the text of the story's frames, leaving out `source`, otherwise the object selection; highlighted characters reject enabled non-neutral object transparency), the fill, stroke and weight also to the paint defaults; `defaultsOnly` updates only paint defaults and cannot be combined with ids, reverse or append; `reverse` (Alt-click) copies from the first selected object (or ids) onto `source` instead, or with nothing selected what the Eyedropper picked up last (a click without Alt); `append` (Shift+Alt-click) adds the source's fills, strokes and effects on top of each target's stack; placed gradients land at the same place relative to each target's bounds (defaults: fitted to new art); with an object of a dynamic symbol instance selected by Direct Selection and no ids, reverse, append or defaultsOnly, that object takes the source's fill and stroke (solid, none, gradient or pattern) for this instance only, as the Eyedropper Options allow, and a source that is not a path, compound path or type, or options that pick up neither color, are refused. On a path directly in a graph series, the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke and the object's opacity and blend mode (exceptions: docs/mcp.md) → {ids}",
|
||||
has_doc,
|
||||
copy_from
|
||||
),
|
||||
@@ -939,7 +939,10 @@ fn remove_item(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let painted = super::graph::capture_series_top_paints(d, &top_paint_changed(&before, d, &ids));
|
||||
Ok(count_changed(d, &before, &ids) + usize::from(kept || painted))
|
||||
})?;
|
||||
s.remap_appearance_item(&ids, |a| (!idx.contains(&a)).then(|| a - idx.iter().filter(|i| **i < a).count()));
|
||||
// The rows removed from the active row's object, which need not come first in `ids`.
|
||||
let active = s.active_appearance_item.as_ref().and_then(|a| a.objects.first().copied());
|
||||
let gone = active.and_then(|a| ids.iter().position(|id| *id == a)).and_then(|k| per.get(k)).unwrap_or(&idx);
|
||||
s.remap_appearance_item(&ids, |a| (!gone.contains(&a)).then(|| a - gone.iter().filter(|i| **i < a).count()));
|
||||
ok()
|
||||
}
|
||||
|
||||
@@ -1023,10 +1026,12 @@ fn duplicate_item(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Rows at or above each copy move up by one (by the block, for copies placed at `to`).
|
||||
let m = idx.len();
|
||||
let landed = active_at.or(first_at);
|
||||
// The rows copied in the active row's object, which need not come first in `ids`.
|
||||
let copied = active.and_then(|a| ids.iter().position(|id| *id == a)).and_then(|k| per.get(k)).unwrap_or(&idx);
|
||||
s.remap_appearance_item(&ids, |a| {
|
||||
Some(match to {
|
||||
Some(_) => a + if landed.is_some_and(|at| at <= a) { m } else { 0 },
|
||||
None => a + idx.iter().filter(|i| **i < a).count(),
|
||||
None => a + copied.iter().filter(|i| **i < a).count(),
|
||||
})
|
||||
});
|
||||
ok()
|
||||
@@ -1204,11 +1209,10 @@ fn copy_from(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
let attrs = s.prefs.eyedropper.attrs(p).map_err(|e| bad(C, e))?;
|
||||
// An object of a dynamic symbol instance's art selected with Direct Selection takes the clicked
|
||||
// object's solid fill and stroke, as the Eyedropper Options allow, for this instance only. A
|
||||
// gradient or pattern sampled for it, a source that is not a path, compound path or type (a
|
||||
// symbol instance's own appearance is empty), and options that pick up neither color are
|
||||
// refused, and the paint defaults stay as they are. A path of a compound path stands for the
|
||||
// compound path, which holds the paint.
|
||||
// object's fill and stroke, as the Eyedropper Options allow, for this instance only. A source
|
||||
// that is not a path, compound path or type (a symbol instance's own appearance is empty) and
|
||||
// options that pick up neither color are refused, and the paint defaults stay as they are. A
|
||||
// path of a compound path stands for the compound path, which holds the paint.
|
||||
if !reverse
|
||||
&& !append
|
||||
&& !defaults_only
|
||||
@@ -1219,11 +1223,7 @@ fn copy_from(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let src = Arc::new(d.node(super::path::compound_or_self(d, clicked)).ok_or(EngineError::NoNode(clicked))?.clone());
|
||||
let (fill, stroke) = (super::paint::proxy_paint(&src, false, None), super::paint::proxy_paint(&src, true, None));
|
||||
let a = attrs.appearance;
|
||||
if !vectorcraft_doc::symbol_overrides::takes_part_paint(&src)
|
||||
|| !(a.fill.color || a.stroke.color)
|
||||
|| (a.fill.color && !super::paint::part_takes(&fill))
|
||||
|| (a.stroke.color && !super::paint::part_takes(&stroke))
|
||||
{
|
||||
if !vectorcraft_doc::symbol_overrides::takes_part_paint(&src) || !(a.fill.color || a.stroke.color) {
|
||||
return Err(super::brushsym::part_only());
|
||||
}
|
||||
super::brushsym::edit_part(s, id, &path, "Eyedropper", |o| {
|
||||
|
||||
@@ -44,7 +44,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Assets",
|
||||
[],
|
||||
None,
|
||||
"{} → {assets: [{id, name, nodes: [id…], bounds: [x, y, width, height] | null (the crop: the bounds the renderer measures for what its art draws, effects included), format3D: usda|usdz|gltf|obj | null (a 3D asset's mesh format; null for art exported as pictures)}]} the Asset Export panel's assets, in order",
|
||||
"{} → {assets: [{id, name, nodes: [id…], bounds: [x, y, width, height] | null (the crop: the bounds the renderer measures for what its art draws, effects included, with the clips of the groups and layers that hold it), format3D: usda|usdz|gltf|obj | null (a 3D asset's mesh format; null for art exported as pictures)}]} the Asset Export panel's assets, in order",
|
||||
has_doc,
|
||||
list
|
||||
),
|
||||
@@ -213,7 +213,7 @@ fn add_3d(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
ids.dedup();
|
||||
for id in &ids {
|
||||
let n = d.node(*id).ok_or_else(|| bad(C, format!("no such object {0}", id.0)))?;
|
||||
if let Err(e) = vectorcraft_render::effects::three_d_meshes(n) {
|
||||
if let Err(e) = vectorcraft_render::effects::three_d_meshes_in(Some(d), n) {
|
||||
let what = n.name.clone().unwrap_or_else(|| format!("#{}", id.0));
|
||||
return Err(bad(C, format!("{what} can't be exported in 3D: {e}")));
|
||||
}
|
||||
@@ -281,7 +281,8 @@ fn export_3d(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
a.nodes.iter().map(|node| d.node(*node).ok_or_else(|| bad(C, format!("no such object {0}", node.0)))).collect::<Result<Vec<_>>>()?;
|
||||
// The asset's objects share one budget of triangles.
|
||||
let what = &a.name;
|
||||
let parts = vectorcraft_render::effects::three_d_meshes_of(nodes).map_err(|e| bad(C, format!("{what} can't be exported in 3D: {e}")))?;
|
||||
let parts =
|
||||
vectorcraft_render::effects::three_d_meshes_of_in(Some(d), nodes).map_err(|e| bad(C, format!("{what} can't be exported in 3D: {e}")))?;
|
||||
let base = Some(safe_file_name(&a.name)).filter(|b| !b.is_empty()).unwrap_or_else(|| "Asset".into());
|
||||
let mut stem = base.clone();
|
||||
let mut k = 1;
|
||||
|
||||
@@ -165,13 +165,13 @@ pub struct BuiltinBrushLibrary {
|
||||
make: fn() -> BrushLibrary,
|
||||
}
|
||||
|
||||
/// The built-in brush libraries, in menu order: original brushes generated in code, one library
|
||||
/// per kind of brush.
|
||||
/// The built-in brush libraries, in menu order: original brushes generated in code.
|
||||
pub const BUILTIN: &[BuiltinBrushLibrary] = &[
|
||||
BuiltinBrushLibrary { id: "art-ribbons", name: "Art Ribbons", make: art_ribbons },
|
||||
BuiltinBrushLibrary { id: "calligraphic-nibs", name: "Calligraphic Nibs", make: calligraphic_nibs },
|
||||
BuiltinBrushLibrary { id: "pattern-borders", name: "Pattern Borders", make: pattern_borders },
|
||||
BuiltinBrushLibrary { id: "scatter-shapes", name: "Scatter Shapes", make: scatter_shapes },
|
||||
BuiltinBrushLibrary { id: "game-trails", name: "Game Trails", make: game_trails },
|
||||
];
|
||||
|
||||
/// Built-in library `id`, made once.
|
||||
@@ -378,6 +378,44 @@ fn scatter_shapes() -> BrushLibrary {
|
||||
built("Scatter Shapes", doc, brushes)
|
||||
}
|
||||
|
||||
/// Game Trails: art brushes for motion lines and impact streaks (original game HUD / VFX art).
|
||||
fn game_trails() -> BrushLibrary {
|
||||
let speed = {
|
||||
let n = 32;
|
||||
let half = |x: f64| 2.2 * (1.0 - x / 100.0).powf(1.2).max(0.08);
|
||||
let xs = |i: usize| 100.0 * i as f64 / n as f64;
|
||||
let mut pts: Vec<Point> = (0..=n).map(xs).map(|x| Point::new(x, -half(x))).collect();
|
||||
pts.extend((0..n).rev().map(xs).map(|x| Point::new(x, half(x))));
|
||||
PathData::single(SubPath::polyline(&pts, true))
|
||||
};
|
||||
let dash = group(vec![
|
||||
black(shapes::rounded_rectangle(Rect::new(0.0, -1.0, 14.0, 1.0), 1.0)),
|
||||
black(shapes::rounded_rectangle(Rect::new(22.0, -1.0, 36.0, 1.0), 1.0)),
|
||||
black(shapes::rounded_rectangle(Rect::new(44.0, -1.0, 58.0, 1.0), 1.0)),
|
||||
black(shapes::rounded_rectangle(Rect::new(66.0, -1.0, 80.0, 1.0), 1.0)),
|
||||
black(shapes::rounded_rectangle(Rect::new(88.0, -1.0, 100.0, 1.0), 1.0)),
|
||||
]);
|
||||
let burst = {
|
||||
// A wedge that flares from a tip at x=0 to a wide end at x=100.
|
||||
let tip = Point::new(0.0, 0.0);
|
||||
let top = Point::new(100.0, -6.0);
|
||||
let bot = Point::new(100.0, 6.0);
|
||||
PathData::single(SubPath::polyline(&[tip, top, bot], true))
|
||||
};
|
||||
let zigzag = band(100.0, 1.8, 40, |x| {
|
||||
let t = (x / 12.5).floor();
|
||||
let local = (x / 12.5) - t;
|
||||
if (t as i32) % 2 == 0 { -3.0 + 6.0 * local } else { 3.0 - 6.0 * local }
|
||||
});
|
||||
let brushes = vec![
|
||||
art("Speed Line", black(speed), Colorization::Tints),
|
||||
art("Dash Trail", dash, Colorization::Tints),
|
||||
art("Burst Ray", black(burst), Colorization::Tints),
|
||||
art("Zigzag", black(zigzag), Colorization::Tints),
|
||||
];
|
||||
built("Game Trails", Document::new(100.0, 100.0), brushes)
|
||||
}
|
||||
|
||||
// ---------- commands ----------
|
||||
|
||||
fn builtin_info(b: &BuiltinBrushLibrary) -> LibraryInfo {
|
||||
|
||||
@@ -208,7 +208,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Break Link to Symbol",
|
||||
[],
|
||||
None,
|
||||
"{ids?|id?} expand selected instances into plain art (with their appearance changes to a dynamic symbol), placed through the symbol's 9-slice guides when it has them, with the instance's opacity mask → {ids}",
|
||||
"{ids?|id?} expand selected instances into plain art (with their appearance changes to a dynamic symbol), placed through the symbol's 9-slice guides when it has them, with the instance's opacity mask; a hidden or locked instance's art stays hidden or locked and is not selected → {ids}",
|
||||
has_selection,
|
||||
symbol_break_link
|
||||
),
|
||||
@@ -272,7 +272,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Select Symbol Instance Part",
|
||||
[],
|
||||
None,
|
||||
"{id, path: [i…] (as symbol.instance.setAppearance)} select one path, compound path or type object of a dynamic symbol instance's art, as a Direct Selection click on it does: the instance is selected alone, and until the selection changes paint.setFill and paint.setStroke with a solid color or none, paint.invert and paint.complement on a solid color (without ids, id or item; type whose characters differ in color is refused) and appearance.copyFrom with a solid fill and stroke (the Eyedropper, without ids, reverse or append; options that pick up neither color are refused) change that object's fill or stroke for this instance only; gradients and patterns are refused, and so are the commands that change the selected objects without ids (transparency.set, the gradient commands, object.group, edit.cut and edit.clear among them); edit.copy copies the instance whole, and selection exports, artboard.fitToSelection, object.liquify, object.isolate, library.add and select.save use the instance (docs/mcp.md); selectPart itself is refused for a static symbol, a path naming no object, a group, image or other kind of object, a hidden, locked or template object or one inside such a group, and a hidden or locked instance → {id, path}",
|
||||
"{id, path: [i…] (as symbol.instance.setAppearance)} select one path, compound path or type object of a dynamic symbol instance's art, as a Direct Selection click on it does: the instance is selected alone, and until the selection changes paint.setFill and paint.setStroke (solid, none, gradient or pattern), transparency.set with opacity and/or blend, paint.invert and paint.complement on a solid color (without ids, id or item; type whose characters differ in color is refused) and appearance.copyFrom (the Eyedropper, without ids, reverse or append; options that pick up neither color are refused; a gradient or pattern fill or stroke is taken) change that object for this instance only; the gradient annotator commands, isolate/knockout on transparency.set, object.group, edit.cut and edit.clear among the commands that change the selected objects without ids are refused; edit.copy copies the instance whole, and selection exports, artboard.fitToSelection, object.liquify, object.isolate, library.add and select.save use the instance (docs/mcp.md); selectPart itself is refused for a static symbol, a path naming no object, a group, image or other kind of object, a hidden, locked or template object or one inside such a group, and a hidden or locked instance → {id, path}",
|
||||
has_doc,
|
||||
symbol_instance_select_part
|
||||
),
|
||||
|
||||
@@ -510,7 +510,7 @@ pub(super) fn expand_brush(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
pub(super) fn brush_freehand(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = str_param(p, "brush").map(str::to_string).filter(|n| s.doc().is_ok_and(|st| brush::find(&st.doc, n).is_some()));
|
||||
let spec = find_command("path.freehand").ok_or_else(|| EngineError::UnknownCommand("path.freehand".into()))?;
|
||||
let r = (spec.run)(s, p)?;
|
||||
let r = crate::cmd::run_checked(s, spec, p)?;
|
||||
let (Some(name), Some(id)) = (name, id_param(&r, "id")) else { return Ok(r) };
|
||||
s.edit("Paintbrush", |d, _| {
|
||||
if let Some(st) = d.node_mut(id).and_then(|n| n.appearance.stroke_mut()) {
|
||||
|
||||
@@ -90,10 +90,11 @@ pub(crate) fn edit_part(s: &mut Session, id: NodeId, path: &[u32], label: &str,
|
||||
})
|
||||
}
|
||||
|
||||
/// The refusal of anything but a solid color fill or stroke change while an object of a symbol
|
||||
/// instance's art is selected ([`selected_part`]), and of selecting any other object of the art.
|
||||
/// The refusal of a change that is not the fill, stroke, opacity or blend of a path, compound path
|
||||
/// or type while an object of a symbol instance's art is selected ([`selected_part`]), and of
|
||||
/// selecting any other object of the art.
|
||||
pub(crate) fn part_only() -> EngineError {
|
||||
EngineError::Other("Only the solid color fill and stroke of a path, compound path or type selected in a symbol instance can change".into())
|
||||
EngineError::Other("Only the fill, stroke, opacity and blend of a path, compound path or type selected in a symbol instance can change".into())
|
||||
}
|
||||
|
||||
pub(super) fn symbol_instance_select_part(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -390,10 +390,11 @@ fn expanded_bound(d: &Document, inst: &Node, own: bool) -> u64 {
|
||||
most.saturating_add(1).saturating_add(mask)
|
||||
}
|
||||
|
||||
/// Replace instances `ids` with their art ([`expanded_instance`], `own` as there). Returns the
|
||||
/// art's ids. Fails before any edit when the instances together would make more than
|
||||
/// Replace instances `ids` with their art ([`expanded_instance`], `own` as there). A hidden or
|
||||
/// locked instance's art stays hidden or locked. Returns each replaced instance's id with its art's
|
||||
/// id. Fails before any edit when the instances together would make more than
|
||||
/// [`MAX_EXPAND_NODES`] objects.
|
||||
fn break_links(d: &mut Document, ids: &[NodeId], own: bool) -> Result<Vec<NodeId>> {
|
||||
fn break_links(d: &mut Document, ids: &[NodeId], own: bool) -> Result<Vec<(NodeId, NodeId)>> {
|
||||
let total = ids.iter().filter_map(|id| d.node(*id)).fold(0u64, |sum, n| sum.saturating_add(expanded_bound(d, n, own)));
|
||||
if total > MAX_EXPAND_NODES as u64 {
|
||||
return Err(EngineError::Other("Too many pieces to make at once".into()));
|
||||
@@ -401,12 +402,14 @@ fn break_links(d: &mut Document, ids: &[NodeId], own: bool) -> Result<Vec<NodeId
|
||||
let mut out = vec![];
|
||||
for id in ids {
|
||||
let Some(inst) = d.node(*id).cloned() else { continue };
|
||||
let Some(art) = expanded_instance(d, &inst, own) else { continue };
|
||||
let Some(mut art) = expanded_instance(d, &inst, own) else { continue };
|
||||
let (par, idx, _) = d.position(*id).ok_or(EngineError::NoNode(*id))?;
|
||||
art.visible &= inst.visible;
|
||||
art.locked |= inst.locked;
|
||||
let aid = art.id;
|
||||
d.remove(*id)?;
|
||||
d.insert(par, idx, art)?;
|
||||
out.push(aid);
|
||||
out.push((*id, aid));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
@@ -491,10 +494,22 @@ pub(super) fn symbol_break_link(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if ids.is_empty() {
|
||||
return Err(EngineError::Other("no symbol instances selected".into()));
|
||||
}
|
||||
let iso = s.doc()?.isolation;
|
||||
let out = s.edit("Break Link to Symbol", |d, sel| {
|
||||
let out = break_links(d, &ids, true)?;
|
||||
sel.set(out.iter().copied());
|
||||
Ok(out)
|
||||
let fresh = d.new_art.get();
|
||||
let made = break_links(d, &ids, true)?;
|
||||
// Art that is hidden or locked, or in a hidden or locked group or layer, is left out of the
|
||||
// selection, as is art on a locked artboard unless the instance it replaces was new art.
|
||||
let locks = d.locked_artboards();
|
||||
sel.set(made.iter().filter_map(|(inst, art)| {
|
||||
let keep = if fresh.contains(inst) {
|
||||
crate::cmd::select::is_selectable_on_any_artboard(d, iso, *art)
|
||||
} else {
|
||||
crate::cmd::select::is_selectable(d, iso, &locks, *art)
|
||||
};
|
||||
keep.then_some(*art)
|
||||
}));
|
||||
Ok(made.into_iter().map(|(_, art)| art).collect::<Vec<_>>())
|
||||
})?;
|
||||
Ok(json!({ "ids": ids_json(&out) }))
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::{
|
||||
Beside, BuilderMap, EngineError, FillRule, Node, NodeId, PathData, Point, Rect, Result, Session, SubPath, Touched, Value, b, bad, check_numbers,
|
||||
Arc, Beside, BuilderMap, EngineError, FillRule, Node, NodeId, PathData, Point, Rect, Result, Session, Touched, Value, b, bad, check_numbers,
|
||||
f64_or, fill_param, finite, gap_param, ids_json, json, marquee_param, po, points_param, remnant, replace_leaves, replace_with_pieces, root_ids,
|
||||
shape_node, switch_or,
|
||||
};
|
||||
@@ -41,6 +41,7 @@ pub(super) fn sb_merge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let gap = gap_param(p);
|
||||
// The map the Shape Builder tool already has, which a click or drag of it is made over.
|
||||
let shared = s.tool.shape_builder_map().filter(|(_, g)| *g == gap);
|
||||
let prepared = s.tool.shape_builder_merge();
|
||||
let label = if erase { "Shape Builder Delete" } else { "Shape Builder" };
|
||||
let (ids, merged, touched, split) = s.edit(label, |d, sel| {
|
||||
let (shapes, leaves) = b::shape_builder_shapes(d, &roots, open_filled_as_closed);
|
||||
@@ -89,16 +90,28 @@ pub(super) fn sb_merge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
touched.add(map.hit(q, Some(tol)));
|
||||
return Ok((ids, None, touched, Some(split)));
|
||||
}
|
||||
let mut touched = Touched { regions: marquee.map(|(a, b)| map.regions_in_marquee(a, b)).unwrap_or_default(), ..Default::default() };
|
||||
touched.extend(&map.builder_hits_along(&pts, piece_tol));
|
||||
touched.edges = map.erased_edges(&touched);
|
||||
let touched = map.merge_touched(&pts, marquee, piece_tol);
|
||||
if touched.is_empty() {
|
||||
return Err(EngineError::Other("Shape Builder: no region under the pointer".into()));
|
||||
}
|
||||
let cuts: Vec<&SubPath> = touched.edges.iter().filter_map(|&k| map.edges.get(k)?.0.path.subpaths.first()).collect();
|
||||
let inputs = b::merge_inputs(&leaves.iter().collect::<Vec<_>>(), open_filled_as_closed);
|
||||
// The tool computed this merge on another thread when it would come out the same.
|
||||
let ready = prepared.as_ref().filter(|m| {
|
||||
shared.as_ref().is_some_and(|(s, _)| Arc::ptr_eq(s, &m.map) && std::ptr::eq(s.as_ref(), map))
|
||||
&& m.gap == gap
|
||||
&& m.touched == touched
|
||||
&& m.inputs == inputs
|
||||
});
|
||||
let computed;
|
||||
let geometry = match ready {
|
||||
Some(m) => &m.geometry,
|
||||
None => {
|
||||
computed = b::merge_geometry(map, &touched, gap, &inputs);
|
||||
&computed
|
||||
}
|
||||
};
|
||||
let picked: Vec<&po::Region> = touched.regions.iter().filter_map(|&i| map.regions.get(i).map(|r| &r.0)).collect();
|
||||
let faces = po::FaceMerger::with_gap(&map.shapes, gap_param(p));
|
||||
let union = if picked.is_empty() { PathData::default() } else { faces.merge(&picked) };
|
||||
let union = &geometry.union;
|
||||
// Pick Color From Artwork: the merged shape takes the look of the front-most object under the
|
||||
// drag's first point, else under its last point, else of the front-most object. Color
|
||||
// Swatches: the current fill, with the rest of the look of the front-most object over the
|
||||
@@ -115,7 +128,7 @@ pub(super) fn sb_merge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// What goes in place of each object the map was made from (none: it stays as it is).
|
||||
let mut outs: Vec<Option<Vec<Node>>> = vec![];
|
||||
let mut merged_id = None;
|
||||
for (i, (sh, leaf)) in map.shapes.iter().zip(&leaves).enumerate() {
|
||||
for (i, leaf) in leaves.iter().take(map.shapes.len()).enumerate() {
|
||||
let mut out = None;
|
||||
let pieces: Vec<usize> = map.lines.iter().enumerate().filter(|(_, (l, _))| l.key == i as u64).map(|(k, _)| k).collect();
|
||||
if !pieces.is_empty() {
|
||||
@@ -131,41 +144,15 @@ pub(super) fn sb_merge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
out = Some(kept);
|
||||
}
|
||||
} else {
|
||||
} else if let Some(Some(rest)) = geometry.rests.get(i) {
|
||||
// A shape loses the regions deleted or merged, then the edges deleted (each one on
|
||||
// its outline, shared edges from every shape along them).
|
||||
let in_union = picked.iter().any(|r| r.sources.contains(&i));
|
||||
let mut rest = if in_union {
|
||||
// What is left of it: its faces not merged or deleted, united. They share their
|
||||
// edges with the merged faces exactly, where subtracting the merged shape left
|
||||
// slivers of the outline along them (#569). A sliver goes in only where the
|
||||
// shape covers it and the merged shape does not take it in.
|
||||
let kept: Vec<&po::Region> = map
|
||||
.regions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(k, (r, ..))| r.sources.contains(&i) && !touched.regions.contains(k))
|
||||
.map(|(_, (r, ..))| r)
|
||||
.collect();
|
||||
if kept.is_empty() { PathData::default() } else { faces.merge_rest(i, &kept, &picked) }
|
||||
// its outline, shared edges from every shape along them). What is left of its own
|
||||
// outline keeps its fill rule; united faces fill with non-zero.
|
||||
out = Some(if rest.path.is_empty() {
|
||||
vec![]
|
||||
} else {
|
||||
b::node_outline(leaf).map_or_else(|| sh.path.clone(), |o| o.0)
|
||||
};
|
||||
let fill_closes = open_filled_as_closed && !leaf.appearance.fill_paint().is_none();
|
||||
// Edges come back from the planar map within a few hundredths of the outlines they
|
||||
// follow, whatever else is selected.
|
||||
let cut_tol = b::outline_snap(&sh.path);
|
||||
let mut cut = false;
|
||||
for piece in &cuts {
|
||||
if let Some(r) = po::cut_out(&rest, piece, fill_closes, cut_tol) {
|
||||
rest = r;
|
||||
cut = true;
|
||||
}
|
||||
}
|
||||
if in_union || cut {
|
||||
// What is left of its own outline keeps its fill rule; united faces fill with non-zero.
|
||||
out = Some(if rest.is_empty() { vec![] } else { vec![remnant(d, leaf, rest, in_union.then_some(FillRule::NonZero))] });
|
||||
}
|
||||
vec![remnant(d, leaf, rest.path.clone(), rest.united.then_some(FillRule::NonZero))]
|
||||
});
|
||||
}
|
||||
// The merged shape goes in place of the object it takes its look from, so it stays in
|
||||
// that object's group (a clip group too).
|
||||
|
||||
@@ -115,7 +115,8 @@ pub(crate) fn subpath_selected_segments(
|
||||
let n = sp.anchors.len();
|
||||
let mut selected_segs = BTreeSet::new();
|
||||
|
||||
let has_explicit_segs = seg_refs.is_some_and(|segments| segments.iter().any(|&(s, _)| s == si));
|
||||
// Segments picked anywhere on the path leave anchors out of it, as Clear does.
|
||||
let has_explicit_segs = seg_refs.is_some_and(|segments| !segments.is_empty());
|
||||
if has_explicit_segs {
|
||||
if let Some(segments) = seg_refs {
|
||||
for &(s, seg) in segments {
|
||||
@@ -209,6 +210,24 @@ pub(crate) fn extract_direct_selection(node: &Node, selection: &Selection) -> Op
|
||||
if pieces.is_empty() { None } else { Some(pieces) }
|
||||
}
|
||||
|
||||
/// Does Copy take `node` unchanged under direct selection, as [`extract_direct_selection`]
|
||||
/// returning `None` means: no anchors or segments of it are picked, it is not a path, or the
|
||||
/// picked ones cover every segment?
|
||||
pub(crate) fn copied_whole(node: &Node, selection: &Selection) -> bool {
|
||||
let seg_refs = selection.segments.get(&node.id);
|
||||
let anchor_refs = selection.anchors.get(&node.id);
|
||||
if seg_refs.is_none() && anchor_refs.is_none() {
|
||||
return true;
|
||||
}
|
||||
let Some(path) = node.path_data() else { return true };
|
||||
let (mut total_segs, mut total_selected) = (0, 0);
|
||||
for (si, sp) in path.subpaths.iter().enumerate() {
|
||||
total_segs += sp.segment_count();
|
||||
total_selected += subpath_selected_segments(sp, si, seg_refs, anchor_refs).len();
|
||||
}
|
||||
total_selected == 0 || total_selected == total_segs
|
||||
}
|
||||
|
||||
impl Clipboard {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.nodes.is_empty() && self.artboards.is_empty() && self.guides.is_empty()
|
||||
@@ -233,15 +252,47 @@ impl Clipboard {
|
||||
let d = &*st.doc;
|
||||
let mut nodes = vec![];
|
||||
let mut source_layers = vec![];
|
||||
// Each compound's bounds, measured once however many of its members are copied.
|
||||
let mut compound_bounds: std::collections::HashMap<NodeId, Option<vectorcraft_geom::Rect>> = std::collections::HashMap::new();
|
||||
for id in roots {
|
||||
let (Some(n), Some(anc)) = (d.node(*id), d.ancestry(*id)) else { continue };
|
||||
let layer_name = anc.iter().rev().skip(1).find_map(|a| d.node(*a).filter(|l| l.is_layer())).and_then(|l| l.name.clone());
|
||||
let compound_parent = anc.iter().rev().skip(1).find_map(|a| d.node(*a).filter(|p| matches!(p.kind, NodeKind::Compound { .. })));
|
||||
// Gradients fitted to the compound's bounds are pinned there, as a split pins them.
|
||||
let gradient_box = compound_parent
|
||||
.filter(|p| p.appearance.has_unplaced_gradient())
|
||||
.and_then(|p| *compound_bounds.entry(p.id).or_insert_with(|| p.geometric_bounds()));
|
||||
// A compound's member or piece copied on its own takes the compound's look (its paint,
|
||||
// transparency, opacity mask, pixel alignment and fill rule), so it pastes as it looked.
|
||||
// It is linked to the compound's graphic style while it still has that style's look
|
||||
// (a gradient pinned to the compound's bounds does not), else to none.
|
||||
let inherit_appearance = |mut piece: Node| {
|
||||
if let Some(parent) = compound_parent
|
||||
&& piece.appearance.items.is_empty()
|
||||
&& let (NodeKind::Compound { rule: compound_rule, .. }, NodeKind::Path { rule, .. }) = (&parent.kind, &mut piece.kind)
|
||||
{
|
||||
*rule = *compound_rule;
|
||||
piece.appearance = parent.appearance.clone();
|
||||
if let Some(bounds) = gradient_box {
|
||||
piece.appearance.pin_gradients(bounds);
|
||||
}
|
||||
piece.opacity = parent.opacity;
|
||||
piece.blend = parent.blend;
|
||||
piece.isolate = parent.isolate;
|
||||
piece.knockout = parent.knockout;
|
||||
piece.knockout_shape = parent.knockout_shape;
|
||||
let (mask, pixel_aligned) = (parent.extras().mask.clone(), parent.extras().pixel_aligned);
|
||||
{
|
||||
let mut extras = piece.extras_mut();
|
||||
extras.mask = mask;
|
||||
extras.pixel_aligned = pixel_aligned;
|
||||
}
|
||||
let link = parent
|
||||
.extras()
|
||||
.graphic_style
|
||||
.and_then(|g| d.graphic_style_by_id(g))
|
||||
.filter(|g| super::style::in_sync(&piece, g))
|
||||
.map(|g| g.id);
|
||||
piece.extras_mut().graphic_style = link;
|
||||
}
|
||||
piece
|
||||
};
|
||||
|
||||
Loaded 100 of 710 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user