mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Merge main into fix/shape-builder-merge-off-ui-thread
This commit is contained in:
commit
05ac9ce040
795 files changed
+98381
-6734
No files matched your search
+22
-22
@@ -13,27 +13,27 @@ on:
|
||||
workflow_dispatch:
|
||||
permissions:
|
||||
contents: read
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
CARGO_INCREMENTAL: 0
|
||||
# A newer push to a PR branch cancels its older run.
|
||||
concurrency:
|
||||
group: print-${{ github.ref }}
|
||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||
jobs:
|
||||
# Desktop printing on Windows, through the suite's shared gate. Pull requests restore the cache
|
||||
# but never save one; a manual run on main saves the cache they restore.
|
||||
windows:
|
||||
runs-on: windows-2022
|
||||
timeout-minutes: 90
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: dtolnay/rust-toolchain@e2a55d2ffb04f378e9626c28d38b36d230d1e12f # master
|
||||
with:
|
||||
toolchain: stable
|
||||
components: clippy
|
||||
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
|
||||
with:
|
||||
key: desktop-print
|
||||
- run: cargo clippy --locked -p vectorcraft --all-targets -- -D warnings
|
||||
- run: cargo test --locked -p vectorcraft
|
||||
- name: Check pending submission and queue refusal feedback
|
||||
shell: bash
|
||||
# A filter that matches nothing still passes, so fail unless some test ran.
|
||||
run: |
|
||||
cargo test --locked -p vectorcraft-ui-egui --lib -- tests::printps | tee printps.log
|
||||
grep -Eq 'test result: ok\. [1-9][0-9]* passed' printps.log || { echo "::error::tests::printps matched no tests"; exit 1; }
|
||||
# Tracks @main so suite-wide CI fixes reach every app without a PR in each repository.
|
||||
uses: storytold/craft-common-actions/.github/workflows/gate.yml@main
|
||||
with:
|
||||
os: '["windows-2022"]'
|
||||
timeout-minutes: 90
|
||||
# As before: printing builds and tests without craft-fonts, nextest or the wasm target.
|
||||
craft-fonts-ref: ""
|
||||
tools: ""
|
||||
targets: ""
|
||||
# A filter that matches nothing still passes, so the printps run fails unless some test ran.
|
||||
command: |
|
||||
cargo clippy --locked -p vectorcraft --all-targets -- -D warnings
|
||||
cargo test --locked -p vectorcraft
|
||||
log="$RUNNER_TEMP/printps.log"
|
||||
cargo test --locked -p vectorcraft-ui-egui --lib -- tests::printps | tee "$log"
|
||||
grep -Eq 'test result: ok\. [1-9][0-9]* passed' "$log" || { echo "::error::tests::printps matched no tests"; exit 1; }
|
||||
@@ -63,6 +63,7 @@ jobs:
|
||||
needs: git-sources
|
||||
permissions:
|
||||
contents: write
|
||||
packages: write
|
||||
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
|
||||
uses: storytold/craft-common-actions/.github/workflows/release.yml@main
|
||||
with:
|
||||
|
||||
@@ -27,8 +27,8 @@ permissions:
|
||||
|
||||
jobs:
|
||||
windows-arm64:
|
||||
# Immutable supporting workflow revision until the new install contract lands upstream.
|
||||
uses: storytold/craft-common-actions/.github/workflows/windows-arm64.yml@8bb4e5ab95443171104ad88113fe1ef9b0e5ce3f
|
||||
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
|
||||
uses: storytold/craft-common-actions/.github/workflows/windows-arm64.yml@main
|
||||
with:
|
||||
app-name: vectorcraft
|
||||
app-dir: VectorCraft
|
||||
|
||||
@@ -28,7 +28,7 @@ Formerly **DrawCraft** (renamed 2026-10-01): old `.drawcraft` files and `"format
|
||||
- Creative Commons with redistribution allowed.
|
||||
- **Every asset file must have a row in [`ASSETS.md`](ASSETS.md)** (path, author, source URL, licence, notes). Put licence texts next to the assets (e.g. `assets/fonts/OFL-*.txt`) and summarize them in `NOTICE`.
|
||||
- `cargo xtask assets` (run by `cargo xtask ci`) fails on any unattributed asset.
|
||||
- **Fonts live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts), never in this repo** (rules: craftrules [`standards/fonts.md`](https://github.com/storytold/craftrules/blob/main/standards/fonts.md)). Never commit a font file here; add new fonts to craft-fonts. The app uses it only through the optional `CRAFT_FONTS_DIR` build option (`CRAFT_FONTS_DIR="$PWD/../craft-fonts" cargo xtask ci`; use an absolute path), read by `crates/text/build.rs` into `vectorcraft_text::CRAFT_FONTS`; never add it to a `Cargo.toml`. Everything must build, test and run with `CRAFT_FONTS` empty, and tests of its glyphs skip when it is. Details: `docs/development.md` › Fonts.
|
||||
- **Fonts live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts), never in this repo**. Never commit a font file here; add new fonts to craft-fonts. The app uses it only through the optional `CRAFT_FONTS_DIR` build option (`CRAFT_FONTS_DIR="$PWD/../craft-fonts" cargo xtask ci`; use an absolute path), read by `crates/text/build.rs` into `vectorcraft_text::CRAFT_FONTS`; never add it to a `Cargo.toml`. Everything must build, test and run with `CRAFT_FONTS` empty, and tests of its glyphs skip when it is. Details: `docs/development.md` › Fonts.
|
||||
- Code and original assets are MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`, `NOTICE`). The ArtCraft logos in `docs/brand/` are trademarks, not open source, under `docs/brand/LICENSE-brand.txt`: never modify them or use them outside VectorCraft.
|
||||
- Prefer art generated in code for defaults (swatches, brushes, symbols, patterns, cursors). If the provenance of an asset is unclear, don't add it.
|
||||
- Reference screenshots of Illustrator stay local under the gitignored `plan/` and are never committed, published or used as assets.
|
||||
@@ -46,7 +46,7 @@ Formerly **DrawCraft** (renamed 2026-10-01): old `.drawcraft` files and `"format
|
||||
- **The UI is thin**: panels read engine state and act through `app.run(id, params)`. Colours come from `theme::Tokens`.
|
||||
- **Rust only** (no handwritten JS/TS). **Never break wasm** (`cargo xtask wasm`).
|
||||
- **Time goes through `chrono`** (workspace dependency). Never hand-roll UTC/calendar conversion (days-to-date algorithms, leap-year math); a crate owns only its output format string. The one exception is range extension: past the years chrono can represent, shift the date by whole 400-year Gregorian cycles (146,097 days, after which the calendar repeats), let chrono compute the month and day, and add the cycles back to the year (splitting seconds into hours, minutes and seconds is plain arithmetic).
|
||||
- **Shared test corpora.** Real-file test oracles (Photoshop-authored PSDs, etc.) live in [`storytold/photocraft-corpus`](https://github.com/storytold/photocraft-corpus), explained in [craftrules `standards/test-corpora.md`](https://github.com/storytold/craftrules/blob/main/standards/test-corpora.md). Never commit large binary fixtures to this repo; fetch them pinned by commit and sha256-verified, as PhotoCraft does with `cargo xtask corpus`.
|
||||
- **Shared test corpora.** Real-file test oracles (Photoshop-authored PSDs, etc.) live in [`storytold/photocraft-corpus`](https://github.com/storytold/photocraft-corpus). Never commit large binary fixtures to this repo; fetch them pinned by commit and sha256-verified, as PhotoCraft does with `cargo xtask corpus`.
|
||||
- **Quality gates** before every commit: `cargo xtask ci` (fmt, clippy -D warnings including the no-panic lints, tests, layers, wasm). One task id per commit (`M2.1: pen tool`).
|
||||
|
||||
## Running and looking at the app
|
||||
@@ -59,7 +59,7 @@ Formerly **DrawCraft** (renamed 2026-10-01): old `.drawcraft` files and `"format
|
||||
- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own; write manifests atomically. Each target dir grows to ~30 GB: delete yours when you finish (a full disk fails links with `errno=28`).
|
||||
|
||||
## Roadmap and progress docs
|
||||
The progress docs follow craftrules' [`standards/progress-docs.md`](https://github.com/storytold/craftrules/blob/main/standards/progress-docs.md):
|
||||
The progress docs:
|
||||
- [`ROADMAP.md`](ROADMAP.md): stage, headline numbers, dimensions, features, languages, upcoming, progress log.
|
||||
- [`docs/target-app-parity.md`](docs/target-app-parity.md): the authoritative parity assessment (area table, methodology, calibration, evidence of what ships).
|
||||
- [`docs/gaps.md`](docs/gaps.md): every known shortfall, ranked; the work list. [`docs/roadmap.md`](docs/roadmap.md): milestones and current focus.
|
||||
@@ -72,17 +72,19 @@ When a task lands, update in the same PR: the area row in `docs/target-app-parit
|
||||
## Contributor credits (About window)
|
||||
|
||||
- About ▸ Contributors/Models are compiled into the binary from `contributors/contributors.json`
|
||||
(commit stats; generated, never hand-edit) and `contributors/people.toml` (names people chose for
|
||||
(commit stats; never hand-edit) and `contributors/people.toml` (names people chose for
|
||||
themselves). See `docs/contributors.md`.
|
||||
- **Agents working for a contributor:** when you prepare a PR, check whether your human's GitHub
|
||||
username has a `[people.<username>]` entry in `contributors/people.toml`. If not, ask them once
|
||||
whether they want to be credited by more than their username: a real name, a display name, and/or
|
||||
their public GitHub profile name (`sync_github_name = true`). If yes, add **only their own** entry
|
||||
(copy the template at the top of the file, or run
|
||||
`python3 ../../craftrules/scripts/contributors.py --add-me . --real-name "…" --sync-github-name`)
|
||||
(copy the template at the top of the file)
|
||||
and include it in their PR, committed as them. If no, change nothing: they are credited as
|
||||
`@username` anyway.
|
||||
- Never add, edit, guess or copy anyone else's entry or name (not from git config, commit authors or
|
||||
GitHub profiles). Never hand-edit `contributors.json`.
|
||||
- Maintainers refresh the stats with `python3 ../../craftrules/scripts/contributors.py .` (it also
|
||||
re-verifies who wrote each `people.toml` entry; `--check` only verifies).
|
||||
|
||||
## CI workflows
|
||||
- Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here.
|
||||
- Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time.
|
||||
- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it.
|
||||
@@ -6,7 +6,7 @@ Every non-code asset in this repository (icons, images, fonts, example art, colo
|
||||
|
||||
Generated-in-code art is original and has no file to list. This covers the default swatches, brushes, symbols, patterns, graphic styles, image-trace presets and the vector tool cursors (`crates/ui-egui/src/cursors.rs`).
|
||||
|
||||
**Font files are never committed here.** Fonts shared by the Crafting Apps live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts) (rules: craftrules [`standards/fonts.md`](https://github.com/storytold/craftrules/blob/main/standards/fonts.md)). The small Latin UI/document fonts below predate that rule and stay.
|
||||
**Font files are never committed here.** Fonts shared by the Crafting Apps live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts). The small Latin UI/document fonts below predate that rule and stay.
|
||||
|
||||
**Optional build input: craft-fonts.** Builds made with `CRAFT_FONTS_DIR=<craft-fonts checkout>` (all official releases) embed the Japanese fonts listed in its [`ATTRIBUTION.md`](https://github.com/storytold/craft-fonts/blob/main/ATTRIBUTION.md): BIZ UDPGothic Regular and Bold, Shippori Mincho Regular and BIZ UDMincho Regular (web builds: BIZ UDPGothic Regular only), all under the SIL Open Font License 1.1. They are not files in this repository; release packages carry each one's `OFL-<family>.txt`. Builds without `CRAFT_FONTS_DIR` embed none of them.
|
||||
|
||||
@@ -345,6 +345,9 @@ Generated-in-code art is original and has no file to list. This covers the defau
|
||||
| `examples/ribbons.vectorcraft` | VectorCraft contributors | Original artwork built through the VectorCraft command API | MIT OR Apache-2.0 | |
|
||||
| `examples/feature-sheet.vectorcraft` | VectorCraft contributors | Original artwork built through the VectorCraft command API | MIT OR Apache-2.0 | |
|
||||
| `examples/perspective-city.vectorcraft` | VectorCraft contributors | Original artwork built through the VectorCraft command API | MIT OR Apache-2.0 | |
|
||||
| `crates/engine/tests/fixtures/jpeg2000/checker.jp2` | VectorCraft contributors | Original test image authored for VectorCraft's tests | MIT OR Apache-2.0 | a 2 × 2 checker encoded with FFmpeg's JPEG 2000 encoder; SHA-256 `3de9c2bb5b8aaec241d939b795002a68eb3d50e41d51c43368000804ad3de057` |
|
||||
| `crates/engine/tests/fixtures/jpeg2000/checker-raw.j2k` | VectorCraft contributors | Original test image authored for VectorCraft's tests | MIT OR Apache-2.0 | a 2 × 2 checker encoded with FFmpeg's JPEG 2000 encoder; SHA-256 `76fa51ab8ccbc2534098b26b8e7fe16dd886e2b6298ba52d06a6b1bde9b61d9e` |
|
||||
| `crates/engine/tests/fixtures/jpeg2000/checker-300ppi.jp2` | VectorCraft contributors | Original test image authored for VectorCraft's tests | MIT OR Apache-2.0 | a 2 × 2 checker encoded with FFmpeg's JPEG 2000 encoder; SHA-256 `1b1aeed8975d3cdc832da6ecf5855852d07d0c490105121e61a8aefb31af009b` |
|
||||
|
||||
## Assets embedded by the PDF backend
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ Formerly **DrawCraft** (renamed 2026-10-01): old `.drawcraft` files and `"format
|
||||
- Creative Commons with redistribution allowed.
|
||||
- **Every asset file must have a row in [`ASSETS.md`](ASSETS.md)** (path, author, source URL, licence, notes). Put licence texts next to the assets (e.g. `assets/fonts/OFL-*.txt`) and summarize them in `NOTICE`.
|
||||
- `cargo xtask assets` (run by `cargo xtask ci`) fails on any unattributed asset.
|
||||
- **Fonts live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts), never in this repo** (rules: craftrules [`standards/fonts.md`](https://github.com/storytold/craftrules/blob/main/standards/fonts.md)). Never commit a font file here; add new fonts to craft-fonts. The app uses it only through the optional `CRAFT_FONTS_DIR` build option (`CRAFT_FONTS_DIR="$PWD/../craft-fonts" cargo xtask ci`; use an absolute path), read by `crates/text/build.rs` into `vectorcraft_text::CRAFT_FONTS`; never add it to a `Cargo.toml`. Everything must build, test and run with `CRAFT_FONTS` empty, and tests of its glyphs skip when it is. Details: `docs/development.md` › Fonts.
|
||||
- **Fonts live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts), never in this repo**. Never commit a font file here; add new fonts to craft-fonts. The app uses it only through the optional `CRAFT_FONTS_DIR` build option (`CRAFT_FONTS_DIR="$PWD/../craft-fonts" cargo xtask ci`; use an absolute path), read by `crates/text/build.rs` into `vectorcraft_text::CRAFT_FONTS`; never add it to a `Cargo.toml`. Everything must build, test and run with `CRAFT_FONTS` empty, and tests of its glyphs skip when it is. Details: `docs/development.md` › Fonts.
|
||||
- Code and original assets are MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`, `NOTICE`). The ArtCraft logos in `docs/brand/` are trademarks, not open source, under `docs/brand/LICENSE-brand.txt`: never modify them or use them outside VectorCraft.
|
||||
- Prefer art generated in code for defaults (swatches, brushes, symbols, patterns, cursors). If the provenance of an asset is unclear, don't add it.
|
||||
- Reference screenshots of Illustrator stay local under the gitignored `plan/` and are never committed, published or used as assets.
|
||||
@@ -47,7 +47,7 @@ Formerly **DrawCraft** (renamed 2026-10-01): old `.drawcraft` files and `"format
|
||||
- **The UI is thin**: panels read engine state and act through `app.run(id, params)`. Colours come from `theme::Tokens`.
|
||||
- **Rust only** (no handwritten JS/TS). **Never break wasm** (`cargo xtask wasm`).
|
||||
- **Time goes through `chrono`** (workspace dependency). Never hand-roll UTC/calendar conversion (days-to-date algorithms, leap-year math); a crate owns only its output format string. The one exception is range extension: past the years chrono can represent, shift the date by whole 400-year Gregorian cycles (146,097 days, after which the calendar repeats), let chrono compute the month and day, and add the cycles back to the year (splitting seconds into hours, minutes and seconds is plain arithmetic).
|
||||
- **Shared test corpora.** Real-file test oracles (Photoshop-authored PSDs, etc.) live in [`storytold/photocraft-corpus`](https://github.com/storytold/photocraft-corpus), explained in [craftrules `standards/test-corpora.md`](https://github.com/storytold/craftrules/blob/main/standards/test-corpora.md). Never commit large binary fixtures to this repo; fetch them pinned by commit and sha256-verified, as PhotoCraft does with `cargo xtask corpus`.
|
||||
- **Shared test corpora.** Real-file test oracles (Photoshop-authored PSDs, etc.) live in [`storytold/photocraft-corpus`](https://github.com/storytold/photocraft-corpus). Never commit large binary fixtures to this repo; fetch them pinned by commit and sha256-verified, as PhotoCraft does with `cargo xtask corpus`.
|
||||
- **Quality gates** before every commit: `cargo xtask ci` (fmt, clippy -D warnings including the no-panic lints, tests, layers, wasm). One task id per commit (`M2.1: pen tool`).
|
||||
|
||||
## Running and looking at the app
|
||||
@@ -60,7 +60,7 @@ Formerly **DrawCraft** (renamed 2026-10-01): old `.drawcraft` files and `"format
|
||||
- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own; write manifests atomically. Each target dir grows to ~30 GB: delete yours when you finish (a full disk fails links with `errno=28`).
|
||||
|
||||
## Roadmap and progress docs
|
||||
The progress docs follow craftrules' [`standards/progress-docs.md`](https://github.com/storytold/craftrules/blob/main/standards/progress-docs.md):
|
||||
The progress docs:
|
||||
- [`ROADMAP.md`](ROADMAP.md): stage, headline numbers, dimensions, features, languages, upcoming, progress log.
|
||||
- [`docs/target-app-parity.md`](docs/target-app-parity.md): the authoritative parity assessment (area table, methodology, calibration, evidence of what ships).
|
||||
- [`docs/gaps.md`](docs/gaps.md): every known shortfall, ranked; the work list. [`docs/roadmap.md`](docs/roadmap.md): milestones and current focus.
|
||||
@@ -69,3 +69,8 @@ The progress docs follow craftrules' [`standards/progress-docs.md`](https://gith
|
||||
When a task lands, update in the same PR: the area row in `docs/target-app-parity.md` (score, missing items, hours) and its evidence list, the gap entry in `docs/gaps.md` if it closed or shrank, the checklist it belongs to, the milestone row in `docs/roadmap.md`, and the ROADMAP's tables and progress log when a headline number moves. Update each touched doc's status line and revision history.
|
||||
- Grade by behaviour against the public documentation (`plan/illustrator/` notes), not by whether a menu item exists. Scores are self-assessed, so err low; mark each number measured or estimated.
|
||||
- Keep the README's Status section in step with the ROADMAP headline.
|
||||
|
||||
## CI workflows
|
||||
- Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here.
|
||||
- Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time.
|
||||
- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it.
|
||||
Generated
+130
-72
@@ -303,7 +303,7 @@ dependencies = [
|
||||
"futures-lite",
|
||||
"parking",
|
||||
"polling",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"slab",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
@@ -334,7 +334,7 @@ dependencies = [
|
||||
"cfg-if",
|
||||
"event-listener",
|
||||
"futures-lite",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -360,7 +360,7 @@ dependencies = [
|
||||
"cfg-if",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"signal-hook-registry",
|
||||
"slab",
|
||||
"windows-sys 0.61.2",
|
||||
@@ -665,7 +665,7 @@ checksum = "43b32313e937c8721c9d17ee9bf60699a6402b4b7c2cb6ac1bee0ffcaa4b4e5e"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"polling",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
@@ -688,7 +688,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "138efcf0940a02ebf0cc8d1eff41a1682a46b431630f4c52450d6265876021fa"
|
||||
dependencies = [
|
||||
"calloop 0.14.5",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
]
|
||||
@@ -705,6 +705,17 @@ dependencies = [
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfb"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f13298d9178622b7916ec5d01d470425c04273c8ed07e87b1c38b34c4f4612b"
|
||||
dependencies = [
|
||||
"fnv",
|
||||
"uuid",
|
||||
"web-time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.5"
|
||||
@@ -763,9 +774,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "clipboard-win"
|
||||
version = "5.4.1"
|
||||
version = "5.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bde03770d3df201d4fb868f2c9c59e66a3e4e2bd06692a0fe701e7103c7e84d4"
|
||||
checksum = "052af7878466feccda12ba7a6a67d019d712f296809559b47c12c8cc196d3a9a"
|
||||
dependencies = [
|
||||
"error-code",
|
||||
]
|
||||
@@ -1017,7 +1028,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13af
|
||||
dependencies = [
|
||||
"craft-winprint-gdi",
|
||||
"miniz_oxide 0.9.1",
|
||||
"roxmltree 0.21.1",
|
||||
"roxmltree 0.21.2",
|
||||
"tempfile",
|
||||
"winprint",
|
||||
"winsafe",
|
||||
@@ -1047,13 +1058,14 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
"egui",
|
||||
"flate2",
|
||||
"getrandom 0.3.4",
|
||||
"image",
|
||||
"log",
|
||||
"plist",
|
||||
"semver",
|
||||
@@ -1069,6 +1081,17 @@ dependencies = [
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-vectorcraft-format"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=14d5dbb50c317b8747a9cf9eb35ab8a22515f137#14d5dbb50c317b8747a9cf9eb35ab8a22515f137"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-window"
|
||||
version = "0.1.0"
|
||||
@@ -1887,7 +1910,7 @@ version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8"
|
||||
dependencies = [
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
@@ -1939,6 +1962,16 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gif"
|
||||
version = "0.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4ae047235e33e2829703574b54fdec96bfbad892062d97fed2f76022287de61b"
|
||||
dependencies = [
|
||||
"color_quant",
|
||||
"weezl 0.1.12",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gif"
|
||||
version = "0.14.2"
|
||||
@@ -2232,7 +2265,7 @@ dependencies = [
|
||||
"rustc-hash 2.1.3",
|
||||
"smallvec",
|
||||
"stringprep",
|
||||
"zune-jpeg",
|
||||
"zune-jpeg 0.5.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2608,14 +2641,14 @@ dependencies = [
|
||||
"bytemuck",
|
||||
"byteorder-lite",
|
||||
"color_quant",
|
||||
"gif",
|
||||
"gif 0.14.2",
|
||||
"image-webp",
|
||||
"moxcms",
|
||||
"num-traits",
|
||||
"png 0.18.1",
|
||||
"tiff",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
"zune-core 0.5.3",
|
||||
"zune-jpeg 0.5.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2836,7 +2869,7 @@ dependencies = [
|
||||
"bumpalo",
|
||||
"flate2",
|
||||
"float-cmp",
|
||||
"gif",
|
||||
"gif 0.14.2",
|
||||
"image-webp",
|
||||
"imagesize 0.14.0",
|
||||
"indexmap",
|
||||
@@ -2851,7 +2884,7 @@ dependencies = [
|
||||
"tiny-skia-path 0.12.0",
|
||||
"xmp-writer",
|
||||
"yoke",
|
||||
"zune-jpeg",
|
||||
"zune-jpeg 0.5.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3899,7 +3932,7 @@ dependencies = [
|
||||
"concurrent-queue",
|
||||
"hermit-abi",
|
||||
"pin-project-lite",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
@@ -4379,12 +4412,15 @@ version = "0.45.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8928798c0a55e03c9ca6c4c6846f76377427d2c1e1f7e6de3c06ae57942df43"
|
||||
dependencies = [
|
||||
"gif 0.13.3",
|
||||
"image-webp",
|
||||
"log",
|
||||
"pico-args",
|
||||
"rgb",
|
||||
"svgtypes 0.15.3",
|
||||
"tiny-skia",
|
||||
"usvg 0.45.1",
|
||||
"zune-jpeg 0.4.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4445,9 +4481,9 @@ checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97"
|
||||
|
||||
[[package]]
|
||||
name = "roxmltree"
|
||||
version = "0.21.1"
|
||||
version = "0.21.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1964b10c76125c36f8afe190065a4bf9a87bf324842c05701330bba9f1cacbb"
|
||||
checksum = "9da7f59328b09405b181fee1018925fad50f157afb43c0c0f5c522c256dd5907"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
@@ -4488,9 +4524,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.1.5"
|
||||
version = "1.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d"
|
||||
checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"errno",
|
||||
@@ -5096,9 +5132,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.16.3"
|
||||
version = "1.16.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b"
|
||||
checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
@@ -5141,7 +5177,7 @@ dependencies = [
|
||||
"libc",
|
||||
"log",
|
||||
"memmap2",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"thiserror 2.0.21",
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
@@ -5416,7 +5452,7 @@ dependencies = [
|
||||
"fastrand",
|
||||
"getrandom 0.4.3",
|
||||
"once_cell",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
@@ -5489,7 +5525,7 @@ dependencies = [
|
||||
"half",
|
||||
"quick-error 2.0.1",
|
||||
"weezl 0.1.12",
|
||||
"zune-jpeg",
|
||||
"zune-jpeg 0.5.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5992,7 +6028,7 @@ dependencies = [
|
||||
"kurbo 0.13.1",
|
||||
"log",
|
||||
"pico-args",
|
||||
"roxmltree 0.21.1",
|
||||
"roxmltree 0.21.2",
|
||||
"simplecss",
|
||||
"siphasher",
|
||||
"skrifa 0.44.0",
|
||||
@@ -6036,7 +6072,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"arboard",
|
||||
"block2 0.6.2",
|
||||
@@ -6061,8 +6097,10 @@ dependencies = [
|
||||
"raw-window-handle",
|
||||
"rfd",
|
||||
"serde_json",
|
||||
"smithay-clipboard",
|
||||
"socket2",
|
||||
"vectorcraft-engine",
|
||||
"vectorcraft-render",
|
||||
"vectorcraft-text",
|
||||
"vectorcraft-ui-egui",
|
||||
"wayland-backend",
|
||||
@@ -6079,7 +6117,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-affinity"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
@@ -6090,7 +6128,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-brush"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"kurbo 0.13.1",
|
||||
"serde",
|
||||
@@ -6102,7 +6140,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-cad"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
@@ -6118,7 +6156,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-cli"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"log",
|
||||
"objc2-core-foundation",
|
||||
@@ -6140,7 +6178,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-color"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"kurbo 0.13.1",
|
||||
"moxcms",
|
||||
@@ -6152,7 +6190,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-doc"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"hayro-ccitt",
|
||||
@@ -6166,12 +6204,12 @@ dependencies = [
|
||||
"tiff",
|
||||
"vectorcraft-color",
|
||||
"vectorcraft-geom",
|
||||
"zune-jpeg",
|
||||
"zune-jpeg 0.5.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-effects"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
@@ -6187,17 +6225,19 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-engine"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"cfb",
|
||||
"craft-docpath",
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
"hayro-jpeg2000",
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
"log",
|
||||
"proptest",
|
||||
"roxmltree 0.21.1",
|
||||
"rustix 1.1.5",
|
||||
"roxmltree 0.21.2",
|
||||
"rustix 1.1.6",
|
||||
"ruzstd",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -6226,7 +6266,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-eps"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"fax",
|
||||
"flate2",
|
||||
@@ -6250,11 +6290,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-format"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"craft-vectorcraft-format",
|
||||
"flate2",
|
||||
"proptest",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
@@ -6265,7 +6306,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-geom"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"kurbo 0.13.1",
|
||||
"proptest",
|
||||
@@ -6277,7 +6318,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-mcp"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"log",
|
||||
@@ -6296,7 +6337,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-metafile"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
@@ -6310,7 +6351,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-pathops"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"kurbo 0.13.1",
|
||||
"linesweeper",
|
||||
@@ -6322,7 +6363,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-pdf"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"chrono",
|
||||
@@ -6335,7 +6376,7 @@ dependencies = [
|
||||
"kurbo 0.13.1",
|
||||
"log",
|
||||
"md-5",
|
||||
"roxmltree 0.21.1",
|
||||
"roxmltree 0.21.2",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
@@ -6354,7 +6395,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-plugins"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -6368,10 +6409,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-render"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"gif",
|
||||
"gif 0.14.2",
|
||||
"image",
|
||||
"jpeg-encoder",
|
||||
"log",
|
||||
@@ -6380,6 +6421,7 @@ dependencies = [
|
||||
"psd",
|
||||
"serde_json",
|
||||
"tiff",
|
||||
"unicode-general-category",
|
||||
"vectorcraft-brush",
|
||||
"vectorcraft-color",
|
||||
"vectorcraft-doc",
|
||||
@@ -6397,14 +6439,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-scriptcheck"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-svg"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"craft-docpath",
|
||||
"flate2",
|
||||
@@ -6429,7 +6471,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-testkit"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"fax",
|
||||
"image",
|
||||
@@ -6447,7 +6489,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-text"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"harfrust 0.13.3",
|
||||
"icu_segmenter",
|
||||
@@ -6468,7 +6510,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-three-d"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"vectorcraft-geom",
|
||||
"vectorcraft-pathops",
|
||||
@@ -6476,12 +6518,13 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-tools"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"vectorcraft-brush",
|
||||
"vectorcraft-color",
|
||||
"vectorcraft-doc",
|
||||
"vectorcraft-effects",
|
||||
@@ -6492,7 +6535,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-trace"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"kurbo 0.13.1",
|
||||
@@ -6504,7 +6547,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-ui-egui"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"craft-keymap",
|
||||
@@ -6515,7 +6558,7 @@ dependencies = [
|
||||
"egui_kittest",
|
||||
"image",
|
||||
"log",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"toml",
|
||||
@@ -6542,7 +6585,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vectorcraft-web"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"eframe",
|
||||
"egui",
|
||||
@@ -6808,7 +6851,7 @@ checksum = "38a91b4eaddff87b1cd1074985e3713da4af2c49742d1b356b2c01670a67a078"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"downcast-rs",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"scoped-tls",
|
||||
"smallvec",
|
||||
"wayland-sys",
|
||||
@@ -6821,7 +6864,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e3c36a0f861ad76d0901f2800b46321410d9f73f2ea88aac0650d86c32688073"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"wayland-backend",
|
||||
"wayland-scanner",
|
||||
]
|
||||
@@ -6843,7 +6886,7 @@ version = "0.31.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4a52d18780be9b1314328a3de5f930b73d2200112e3849ca6cb11822793fb34d"
|
||||
dependencies = [
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"wayland-client",
|
||||
"xcursor",
|
||||
]
|
||||
@@ -7518,7 +7561,7 @@ dependencies = [
|
||||
"libc",
|
||||
"log",
|
||||
"os_pipe",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"thiserror 2.0.21",
|
||||
"tree_magic_mini",
|
||||
"wayland-backend",
|
||||
@@ -7577,7 +7620,7 @@ dependencies = [
|
||||
"libc",
|
||||
"libloading",
|
||||
"once_cell",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"x11rb-protocol",
|
||||
]
|
||||
|
||||
@@ -7619,7 +7662,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -7673,7 +7716,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
@@ -7723,7 +7766,7 @@ dependencies = [
|
||||
"hex",
|
||||
"libc",
|
||||
"ordered-stream",
|
||||
"rustix 1.1.5",
|
||||
"rustix 1.1.6",
|
||||
"serde",
|
||||
"serde_repr",
|
||||
"tracing",
|
||||
@@ -7907,9 +7950,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "zip"
|
||||
version = "9.1.0"
|
||||
version = "9.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2886b00003a72c4ccee3f42cfc34908d33b2508a74d827dc2a4244706475c704"
|
||||
checksum = "1156b39d142a34b999c2310a50455e0cb3760bf7efad0122f796d813bee26ccb"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
@@ -7946,19 +7989,34 @@ dependencies = [
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zune-core"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f423a2c17029964870cfaabb1f13dfab7d092a62a29a89264f4d36990ca414a"
|
||||
|
||||
[[package]]
|
||||
name = "zune-core"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b"
|
||||
|
||||
[[package]]
|
||||
name = "zune-jpeg"
|
||||
version = "0.4.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29ce2c8a9384ad323cf564b67da86e21d3cfdff87908bc1223ed5c99bc792713"
|
||||
dependencies = [
|
||||
"zune-core 0.4.12",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zune-jpeg"
|
||||
version = "0.5.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "27bc9d5b815bc103f142aa054f561d9187d191692ec7c2d1e2b4737f8dbd7296"
|
||||
dependencies = [
|
||||
"zune-core",
|
||||
"zune-core 0.5.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
+5
-2
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
|
||||
default-members = ["crates/*", "apps/vectorcraft", "apps/vectorcraft-cli", "xtask"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.18.0"
|
||||
version = "0.20.0"
|
||||
edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
rust-version = "1.95"
|
||||
@@ -19,7 +19,10 @@ craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392c
|
||||
# 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 = "92d392cff132eaf53e13afff77f38b5c11b73aa0", features = ["egui"] }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", 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 = "14d5dbb50c317b8747a9cf9eb35ab8a22515f137" }
|
||||
# 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"] }
|
||||
# internal
|
||||
vectorcraft-geom = { path = "crates/geom" }
|
||||
vectorcraft-color = { path = "crates/color" }
|
||||
|
||||
@@ -205,10 +205,10 @@ drag-and-drop, set **Preferences › Performance › Linux Display Server** to *
|
||||
the app with `WAYLAND_DISPLAY= vectorcraft`: the window then opens through XWayland, where drops work
|
||||
(see [`docs/development.md`](docs/development.md#linux-wayland-and-x11)).
|
||||
|
||||
On Linux with Wayland, under KDE Plasma 6.3 or later and under GNOME, VectorCraft doesn't respond to a drawing tablet's
|
||||
pen yet (#491, #2028). Open the window through XWayland instead, where the pen works as a mouse and the app reads its
|
||||
pressure: set the same preference to **X11 (XWayland)**, or to **X11 (XWayland) When a Pen Is Attached** to use
|
||||
XWayland only when a pen is attached as VectorCraft starts, and restart; or start the app with
|
||||
On Linux with Wayland, under KDE Plasma 6.3 or later and under GNOME, VectorCraft can't get a drawing tablet's pen
|
||||
input on native Wayland yet (#491, #2028). When a pen is attached as VectorCraft starts, the window opens through
|
||||
XWayland, where the pen works as a mouse and the app reads its pressure (#4967; `VECTORCRAFT_NATIVE_WAYLAND=1` keeps
|
||||
Wayland). For a pen attached later, set the same preference to **X11 (XWayland)** and restart, or start the app with
|
||||
`WAYLAND_DISPLAY= vectorcraft` (see [`docs/development.md`](docs/development.md#linux-wayland-and-x11)).
|
||||
|
||||
### Use it from Claude Code and other agents
|
||||
@@ -328,12 +328,21 @@ stand on its own.
|
||||
| | App | What it's for | Code | Learn more |
|
||||
|:-:|---|---|---|---|
|
||||
| <img src="https://raw.githubusercontent.com/storytold/photocraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.photocraft.png" alt="" width="32" height="32"> | **PhotoCraft** | Image editing: layers, masks, type and real PSD files | [GitHub](https://github.com/storytold/photocraft) | [Website](https://getartcraft.com/apps/photocraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/vectorcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.vectorcraft.png" alt="" width="32" height="32"> | **VectorCraft** | **Vector illustration · you are here** | [GitHub](https://github.com/storytold/vectorcraft) | [Website](https://getartcraft.com/apps/vectorcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/vectorcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.vectorcraft.png" alt="" width="32" height="32"> | **VectorCraft** | Vector illustration | [GitHub](https://github.com/storytold/vectorcraft) | [Website](https://getartcraft.com/apps/vectorcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/filmcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.filmcraft.png" alt="" width="32" height="32"> | **FilmCraft** | Video editing, color and sound | [GitHub](https://github.com/storytold/filmcraft) | [Website](https://getartcraft.com/apps/filmcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/lightcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.lightcraft.png" alt="" width="32" height="32"> | **LightCraft** | Photo library and raw development | [GitHub](https://github.com/storytold/lightcraft) | [Website](https://getartcraft.com/apps/lightcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/pdfcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.pdfcraft.png" alt="" width="32" height="32"> | **PdfCraft** | Reading, organizing and protecting PDFs | [GitHub](https://github.com/storytold/pdfcraft) | [Website](https://getartcraft.com/apps/pdfcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/effectcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.effectcraft.png" alt="" width="32" height="32"> | **EffectCraft** | Motion graphics and visual effects | [GitHub](https://github.com/storytold/effectcraft) | [Website](https://getartcraft.com/apps/effectcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/designcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.designcraft.png" alt="" width="32" height="32"> | **DesignCraft** | Page layout and publishing | [GitHub](https://github.com/storytold/designcraft) | [Website](https://getartcraft.com/apps/designcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/figcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.figcraft.png" alt="" width="32" height="32"> | **FigCraft** | Interface design and prototyping | [GitHub](https://github.com/storytold/figcraft) | [Website](https://getartcraft.com/apps/figcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/wordcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.wordcraft.png" alt="" width="32" height="32"> | **WordCraft** | Word processing and documents | [GitHub](https://github.com/storytold/wordcraft) | [Website](https://getartcraft.com/apps/wordcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/soundcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.soundcraft.png" alt="" width="32" height="32"> | **SoundCraft** | Audio recording and editing | [GitHub](https://github.com/storytold/soundcraft) | [Website](https://getartcraft.com/apps/soundcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/deckcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.deckcraft.png" alt="" width="32" height="32"> | **DeckCraft** | Presentations and slide decks | [GitHub](https://github.com/storytold/deckcraft) | [Website](https://getartcraft.com/apps/deckcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/cadcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.cadcraft.png" alt="" width="32" height="32"> | **CADCraft** | Computer-aided design | [GitHub](https://github.com/storytold/cadcraft) | [Website](https://getartcraft.com/apps/cadcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/gridcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.gridcraft.png" alt="" width="32" height="32"> | **GridCraft** | Spreadsheets and data | [GitHub](https://github.com/storytold/gridcraft) | [Website](https://getartcraft.com/apps/gridcraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/texturecraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.texturecraft.png" alt="" width="32" height="32"> | **TextureCraft** | 3D texture painting | [GitHub](https://github.com/storytold/texturecraft) | [Website](https://getartcraft.com/apps/texturecraft) |
|
||||
| <img src="https://raw.githubusercontent.com/storytold/craft-anything/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.craft-anything.png" alt="" width="32" height="32"> | **Craft Anything** | See how any software works, then build your own | [GitHub](https://github.com/storytold/craft-anything) | [Website](https://getartcraft.com/apps/craft-anything) |
|
||||
| <img src="https://raw.githubusercontent.com/tangency-labs/solidcraft/main/assets/icons/app/linux/exports/hicolor/64x64/apps/io.tangencylabs.solidcraft.png" alt="" width="32" height="32"> | **SolidCraft** | Parametric 3D CAD by Tangency Labs (community app) | [GitHub](https://github.com/tangency-labs/solidcraft) | |
|
||||
|
||||
And [**ArtCraft**](https://getartcraft.com/) itself, our AI image and video studio for artists who want real control.
|
||||
|
||||
|
||||
+30
-4
@@ -9,7 +9,7 @@
|
||||
|
||||
**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-10-23 · **Change:** minor (OpenType SVG colour fonts draw and export as vector art; previously: COLR colour fonts (v0 and v1) draw and export as vector art; previously: G2: the Effect Gallery shipped (#2883): Effect › Effect Gallery… browses the Artistic, Brush Strokes, Distort, Sketch, Stylize and Texture filters, edits each filter's settings, builds an ordered stack (add, reorder, hide, remove) with a live canvas preview, and OK applies it as separate appearance effects in one undo step; remaining: filter thumbnails and a preview area inside the dialog; G2 estimate 4–6 → 1–2 h; Raster effects 82% → 85% (power-user view 49% → 52%), estimated, a modest move because the named missing dialog landed without thumbnails or an in-dialog preview; previously: colour emoji fonts with bitmap glyphs (`sbix`, `CBDT`) draw and export; headline numbers unchanged; previously:; Affinity import AF.5: master pages, tables, paragraph formats, shadows, glows, outlines, overlays and embedded documents; ruby (ふりがな) for live text: `type.ruby` sets or removes a reading over a byte range, above or below (right of the column in vertical type), aligned, sized and overhanging or squeezed; saved natively, laid out with line growth, drawn on the canvas and in SVG/PDF (outlines where ruby is present); headline numbers unchanged; previously:; 3D: original Standard, Diffuse, Top Left and Right lighting presets; command access, effective-setting recognition and preview/history; scores and estimates unchanged; Print: Japanese paper sizes in the Print dialog's Media list — JIS B4 257 × 364 mm, JIS B5 182 × 257 mm and JIS B6 128 × 182 mm beside the ISO B4/B5 (which keep their ISO 216 sizes), はがき, 往復はがき, 封筒 長形3号/長形4号/角形2号/角形3号 and 四六判; about 90 community PRs of 2026-10-19: Objects on Path, the Simplify slider, Pencil Edit Selected Paths in the middle of a path, Georgian, and more; Background Export progress dialog with Cancel, #2564; about 85 community PRs of 2026-10-18: Justification…, Word .docx placed as area type, CCITT Group 3 TIFF, Roman Hanging Punctuation, blend spines edited with the Anchor Point tool, shadows and glows in stack order, coloured 3D lights, a Dutch catalog completed; 3D lighting: one to ten colored lights, smooth colored Inflate, legacy white-light appearance preserved; surface batching and restored seam minimum; scores and estimates unchanged; about 145 community PRs of 2026-10-17: image-decode overflow guards, opaque floating panels, MCP export cancel and progress, editable Illustrator EPS import, scoped `doc_inspect`, Dutch, Danish and Indonesian, Simplified Chinese messages, screen-reader names; headline numbers unchanged; about 160 community PRs of 2026-10-15/16: PDF import crash fixes, Dimension tool, legacy `.ai` save, Intertwine, web pen input, Image Trace fixes, `.ai`/EPS/PDF import fidelity, Bulgarian, Greek, Swedish and Finnish catalogs; headline numbers unchanged) · **Target:** Adobe Illustrator 2026 (30.x)
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-01 · **Change:** minor (Progress log row for the Color Effects add-on's Find Nearest Swatch; headline numbers 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
|
||||
@@ -110,6 +110,7 @@ Weights, evidence and the missing items per area: [target-app-parity.md](docs/ta
|
||||
| Japanese | `ja` | full | 100% |
|
||||
| German | `de` | full | 98% |
|
||||
| Korean | `ko` | full | 100% |
|
||||
| Bosnian | `bs` | full (AI-assisted, native review pending) | 100% |
|
||||
| Vietnamese | `vi` | full | 100% |
|
||||
|
||||
Also shipped: Traditional Chinese (partial, 80%), Bulgarian, Finnish, Greek, Hungarian, Italian, Polish, Russian, Swedish, Ukrainian and Georgian (full), Catalan (full; AI-drafted, native review welcome), Czech (catalog complete; render-site gaps reduced; native review pending).
|
||||
@@ -128,6 +129,17 @@ Ranked; detail in [docs/roadmap.md](docs/roadmap.md) and [docs/gaps.md](docs/gap
|
||||
|
||||
## Progress log
|
||||
|
||||
- 2026-11-01: Find Nearest Swatch joins the Color Effects add-on (Object › Plug-ins › Color Effects): for each colour of the selection, the closest swatches of the document, a built-in library or a library or colour book the user loaded, with their CIEDE2000 difference; Apply gives each colour its nearest swatch as one undo step, linking global and spot swatches and adding a library's to the document first, so a process colour becomes the closest spot colour (`addon.color.nearestSwatch`, `addon.color.applyNearestSwatch`, #4923).
|
||||
- 2026-11-01: Expand Dashes into Paths joins the Path Precision Tools add-on (Object › Plug-ins › Path Precision Tools): each selected path's dashed stroke becomes separate open paths, one per dash, laid out exactly as the stroke draws them (pattern, offset, Align Dashes to Corners and Path Ends), each keeping the stroke with the dashes off, so the gaps are real gaps for cutters and plotters, as one undo step (`addon.pathtools.expandDashes`, #4922).
|
||||
- 2026-11-01: Randomize Opacity joins the Arrange Tools add-on (Object › Plug-ins › Arrange Tools): each selected object gets its own opacity at random between a minimum and a maximum percent, with a seed that repeats the result, as one undo step (`addon.arrange.randomizeOpacity`, #4925).
|
||||
- 2026-11-01: Sort Objects joins the Arrange Tools add-on (Object › Plug-ins › Arrange Tools): the selected objects are sorted by width, height, area, position, opacity or stacking order (small to large, large to small or at random) and take another of those in that order, trading their values or stepping them evenly, as one undo step (`addon.arrange.sort`, #4917).
|
||||
- 2026-11-01: New Symbols from Layers joins the Document Tools add-on (Object › Plug-ins › Document Tools): one symbol of each top-level layer's art (the layers highlighted in the Layers panel when two or more are, else every layer), named after the layer, made as New Symbol makes one; Convert Layers to Symbols also puts an instance of each in place of its layer's art. Empty layers are skipped and reported; one undo step. A VectorCraft addition: headline numbers unchanged.
|
||||
- 2026-11-01: Document Tools, a built-in add-on (Object › Plug-ins › Document Tools), starts with Save a Timestamped Copy: a copy of the document named `<name>_YYYY-MM-DD_HH-MM` (UTC; `-2`, `-3`… when that name is taken) goes next to it, or into a folder picked for a document never saved, written as File › Save a Copy writes it, so the document keeps its path, title and modified state. Off on the web, which has no clock. A VectorCraft addition: headline numbers unchanged.
|
||||
- 2026-11-01: Divide into Dashes joins the Path Precision Tools add-on (Object › Plug-ins › Path Precision Tools): the selected paths, or only their direct-selected segments, are cut into separate open paths of a dash length measured along the curve, with an optional gap (left empty, or kept as pieces), each keeping the path's appearance; Fit to Corners and Ends stretches or squeezes the lengths so every run starts and ends on a dash. A dialog with Preview, one undo step, at most 100000 pieces. A VectorCraft addition: headline numbers unchanged.
|
||||
- 2026-11-01: Group Overlapping Objects joins the Arrange Tools add-on (Object › Plug-ins › Arrange Tools): the selected objects whose visual bounds overlap or touch, chains included, go into one group each, within their own layer or group, at the stacking place of their frontmost member; objects that overlap nothing stay as they are. One undo step, a sweep along x so thousands of loose pieces group quickly (at most 20000 objects). A VectorCraft addition: headline numbers unchanged.
|
||||
- 2026-10-31: Export for Screens works as the reference app's dialog: Artboards and Assets tabs with a grid or list of thumbnails, Clear Selection and Prefix, Create Sub-folders by Scale or Format (on by default), Export PDFs As Single File (the chosen artboards as one PDF's pages, `pdfs: "single"`) or Multiple Files, iOS and Android rows, format presets in a ☰ menu, and a count of the files the export writes (#4895). About 55 community PRs and fixes landed the same day: Find Font, Replace Font, Outline Stroke and Expand leave hidden and locked art alone (with a warning); DXF export writes LINE and ELLIPSE entities and outlines only the strokes a CAD line can't draw; Blob Brush Angle and Roundness; Shift-constrained Perspective Selection; Grid Repeat Options Preview; a gradient mesh from a file budgeted at about a million quads; width-profile strokes keep their shape zoomed out; `.ai`/EPS import fidelity (area type, envelopes, transparency in dictionaries, character styles); PDF import finds hyphenated and CMap-suffixed font names; Windows renders follow the process's core affinity; Enter or Escape in a floating panel's field no longer reaches the canvas (#4803); Properties shows Blend Options for any selected blend (#4817); Artwork Cleanup finds doubled type (#4835) and joins stacked lines into one object (Join Point Text, #4826). Headline numbers unchanged.
|
||||
- 2026-10-31: Arrange Tools, a built-in add-on (Object › Plug-ins › Arrange Tools): Arrange in Grid lays the selected objects out in rows and columns, a row or a column with gaps, from the selection's top-left or centred on the artboard; Swap Objects swaps two objects' places (and, with Swap Objects and Sizes, their sizes); Replace with Top Object puts a copy of the topmost selected object in place of each of the others, kept, stretched or fitted in proportion; Set Size of Each gives every selected object an exact width and/or height about a reference point; Zoom to Selection fits the view to the selection. Each is one undo step with a previewed dialog where it has options. A VectorCraft addition: headline numbers unchanged.
|
||||
- 2026-10-30: Gradient panel presets: a Presets menu applies one of the built-in gradient libraries' presets (grouped by theme) or a saved one to the gradient behind the active proxy, keeping its placement, as one undo step; the panel menu saves the current gradient as a preset, kept with the preferences, and deletes saved ones (`gradient.presets.list/apply/save/delete`).
|
||||
- 2026-10-23: Pathfinder › Trap: the Pathfinder panel's Trap… and Effect › Pathfinder › Trap… add a thin overprinting strip of the lighter colour inside the darker one where two solid colours meet, so misregistered plates leave no gap ([trapping](https://helpx.adobe.com/illustrator/using/trapping.html)), with Thickness, Height/Width, Tint Reduction, Traps with Process Color and Reverse Traps. Illustrator effects 49/50 (SVG Filters remains); Live vector effects hours 6–10 → 4–8; headline numbers unchanged.
|
||||
- 2026-10-23: OpenType SVG colour fonts draw as vector art: each glyph's SVG document is read by the SVG importer, in the text's colours where it asks for them, and draws, exports and outlines as COLR glyphs do; all colour font formats are now drawn.
|
||||
- 2026-10-23: COLR colour fonts (v0 and v1, as in Noto Color Emoji's vector build, Segoe UI Emoji, Nabla) draw as vector art: their layers' colours, gradients, clips and blend modes on the canvas and in SVG, EPS, legacy `.ai` and PDF, and as editable paths with Create Outlines; glyphs using sweep gradients or compositing modes are drawn as images of the glyph.
|
||||
@@ -144,6 +156,8 @@ Ranked; detail in [docs/roadmap.md](docs/roadmap.md) and [docs/gaps.md](docs/gap
|
||||
|
||||
| Date | What landed |
|
||||
|---|---|
|
||||
| 2026-10-29 | Linux: the Flatpak on GNOME copies and pastes text with other apps through the compositor's core Wayland protocol (smithay-clipboard on the window's connection), where arboard has neither data-control nor X11. Scores and estimates unchanged. |
|
||||
| 2026-10-29 | Linux: a clipboard that can't be opened (the Flatpak on GNOME, where arboard has neither data-control nor X11) is retried after 1 s, doubling to a minute, and logged once, where the background Paste check used to reconnect and log a warning every 250 ms (267k warnings in 18 h). Scores and estimates unchanged. |
|
||||
| 2026-10-21 | 3D lighting: four original lighting presets shared by Revolve, Extrude & Bevel, Inflate and Rotate, with command access and manual Custom settings; scores and estimates unchanged. |
|
||||
| 2026-10-21 | Print: the Print dialog's Media list has the Japanese papers — JIS B4 (257 × 364 mm), JIS B5 (182 × 257 mm) and JIS B6 next to the ISO B4/B5 (which keep their ISO 216 dimensions for settings saved before them), はがき 100 × 148 mm, 往復はがき 148 × 200 mm, 封筒 長形3号 120 × 235 mm / 長形4号 90 × 225 mm / 角形2号 240 × 332 mm / 角形3号 216 × 277 mm and 四六判 127 × 188 mm. A paper is named by its JSON id or its dialog label, any case and spacing (`B5 (JIS)` or `b5jis`), and every added size prints as its own PDF page size. Headline numbers unchanged. |
|
||||
| 2026-10-18 | Inch rulers divide the inch in half, not in ten. Zooming in stepped an inch ruler through 0.2, 0.1 and 0.05 of an inch, marks an inch ruler does not carry; below an inch the steps are binary fractions now, 1/2 down to 1/16, with the unlabelled ticks between them binary too (down to 1/128, so the 32nds, 64ths and 128ths appear as the report expects), while the metric units, points, pixels and picas keep their tenths. A label carries the decimals its step needs, so a sixteenth reads 0.0625 rather than the rounded 0.06, with trailing zeros dropped (#2827). Headline numbers unchanged. |
|
||||
@@ -182,7 +196,7 @@ Ranked; detail in [docs/roadmap.md](docs/roadmap.md) and [docs/gaps.md](docs/gap
|
||||
| 2026-10-12 | Graphs: Object › Graph › Design… and Marker… (designs with a preview, New Design, Rename, Delete Design, Paste Design, Select Unused, and markers drawn with a design or None) (#1092); Graph Data opens as a worksheet with Import Data…, column width and decimals (#1138), and takes Transpose, Switch x/y, `|` line breaks in labels and the pie Legend, Position and Sort options (#1115); Graph Type adds Add Drop Shadow and Add Legend Across Top (#1119), widths past 100%, First Row and First Column in Front, and Draw Filled Lines (#1136); graphs keep their label formatting when drawn again (#1137). Align: a Horizontal & Vertical Align Center button (#1073); Align to Key Object makes the topmost selected object the key (#1221), and the palette's key entry reads Cancel Key Object (#1224). Pen: the nearest end wins over snapping, Alt on the last anchor edits it, and Enter, Escape or a tool switch finish a handle preview as one undo step (#821). Direct Selection: a click on an anchor of an already selected path selects that anchor alone (#1237). Make Compound Path keeps the backmost object's paint, and a selected member reads and changes the compound's fill rule (#1225). Shape Builder and Live Paint no longer turn a line's loose end past a crossing into a piece that runs out and back (#1230). Art moved or duplicated with an artboard follows Transform Pattern Tiles (#1219). A click with the Rectangular Grid tool opens its options dialog (#1169). Commands that change nothing (Lock, Hide, Transform Each, Rearrange Artboards and others) add no undo step (#1233). Saving with missing placed files keeps their relink data, and .ai export honours the chosen artboards (#718). SVG export keeps Unicode data-attribute names, and SVG Options refuses an invalid artboard range (#989). CMYK colours given in percent with a component under 1 keep their scale (#1249). Colour Image Trace keeps close dark tones apart (#1247). macOS: the Save sheet has a Format menu (#1235). The Hand and Zoom tools show drawn cursors on every platform (#1238). Czech: the remaining strings are translated, and Pathfinder's Add reads "Sjednotit" (#1226). MCP: a lost reply reports that the edit may have happened instead of replaying it (#1246); `select.key` checks its id and agents can read the key object (#1222). Tests: the command sweeps and import fuzz tests fail on a panic caught by the engine's guard (#1218), and the envelope export test ignores PDF dates and file IDs (#1220). Effect › Artistic › Film Grain (Grain, Highlight Area, Intensity; Photoshop-style filters 29/57) (#1282), and Craquelure, Mosaic Tiles and Stained Glass redraw 25–30% faster with identical pixels (#1265). Direct Selection: clicking a segment selects only that segment, and Delete removes just it (#1300); Shift-click deselects one anchor of a path selected whole (#1263). Pen + Alt converts anchors of paths inside a selected group or compound path (#1306). The Curvature tool shows circular point markers without direction handles (#1284). Snap to Point: pressing near an anchor drags by that anchor, with Smart Guides on or off (#1313). Transform Again repeats the options of the transform it repeats (#1303). Aligning anchors that are already aligned adds no undo step (#1276), and type commands that change nothing record none either (#1316). Simplify keeps the path selected with its new anchors, and its dialog previews and shows the anchor count (#1305). Adding a stroke colour to an unpainted zero-width stroke gives it a visible 1 pt width (#1256). The Eyedropper samples locked objects, template layers and art outside isolation mode (#1260), and shows a pipette cursor (#1288). Fonts named by their full name resolve to the right face (#1267), and Proportional Metrics brings the matching kerning (`apkn`, `vkrn`, `vapk`) (#1291). Artboards: Escape leaves Artboard editing (#1296), Document Setup's Edit Artboards switches to the Artboard tool (#1294), and the Artboard tool starts on the active artboard (#1309). New and imported documents use the Gridline every and Subdivisions preferences (#1314). Send to Current Layer refuses a hidden or locked current layer (#1301), and layer row commands check explicit ids (#1255). Links: Find in Folder… finds missing linked files in a folder and its subfolders and can copy them into the document's folder (#1287). Image Trace traces images over 64 megapixels scaled down instead of refusing them (#1315). DXF export marks closed curves as closed SPLINE entities (#1320). EPS import: closepath respects the path size limit (#1311). The interface is available in Hungarian (#1302), and more Czech labels are translated (#1259). Linux RPMs declare shared-library capabilities (#1290). CI pins every third-party action to a verified commit (#1321). Select menu commands skip hidden and locked art and stay inside the isolated group in isolation mode (#1325). Fill, stroke, gradient and Appearance panel commands that change nothing add no undLine truncated
|
||||
| 2026-10-12 | Georgian interface (`ka`, ქართული): the whole catalog, plurals and messages, tested complete; Georgian letters come from an installed font (SF Georgian, Sylfaen, Noto Sans Georgian, DejaVu Sans) from the first frame; Type › Change Case › Title Case and Sentence case leave Georgian lower case instead of turning it into Mtavruli. |
|
||||
| 2026-10-11 | Effect › Brush Strokes: Accented Edges, Angled Strokes, Crosshatch, Dark Strokes, Ink Outlines, Spatter, Sprayed Strokes, Sumi-e (G2). Command parameters from agents and files checked before anything changes (paint, place, cuts, dashes, crop, asset ids, anchors, paths, distort; #984–#999); command dialogs keep whole numbers whole, so Create Object Mosaic takes the tiles typed (#1000); stepper arrows take Shift (×10) and Ctrl/Cmd (÷10) like the arrow keys (#991). Exports and Rasterize supersample Art Optimized edges, so shapes meeting edge to edge leave no seam (#983). Character and paragraph styles take leading, kerning, mojikumi and the other attributes left out at their defaults (#1001). Drawing, typing and pasting with no layer shown and unlocked is refused instead of adding art to a hidden or locked layer (#1002). MCP `add_text` gives type on or in a path the colour asked for (#1003). MCP `screenshot` of a connected app renders the artboard asked for (#1004). `vectorcraft-cli run` ends quietly when its reader closes early (#1005). Select › All on Active Artboard selects on the artboard made active, not always the first (#1006). Reverse Gradient keeps off-centre midpoints, mirrored (#1007). A polar grid with no radial dividers draws none (#1008). Use Precise Cursors also covers the Pen's join pointer (#1009). SVGs with one absolute and one unitless root side import at their size, unstretched (#1010). DXF US survey inches, yards and miles open at their size and name (#1011). A file placed into a document with no other links follows its first change even while the app sits idle (#1031). PDF import keeps the style a font name says when the file doesn't embed the font (#1023). Shift selects ruler guides and art together, and Delete removes both (#1022). Open filled paths are hit by their fill on every side, the rotate cursor bows around the corner it is at, the flip icons match their flips, the Color Picker previews on the selection, and Properties › Transform reaches Scale Corners and Scale Strokes & Effects (#1036). Offset Path and the other booleans finish on outlines where the sweep used to loop until memory ran out (linesweeper 0.5, #1028). Floating panel groups stack into sets that move together, collapse to their tabs and dock as extra dock columns that scroll (#1020). Ruler guides live on layers: listed in the Layers panel, shown, hidden, locked, duplicated, merged and deleted with their layer, and moved between layers (#1021). A `.ai` whose editing data uses global process colours (`Xk`/`XK`) or pattern fills keeps its sublayers: the colours come in as global swatches at their tint, the patterns as pattern swatches (#1025). A `.ai` in PostScript form opens through its layers, read from the program itself, also when it names the prolog defining its operators without including it, as Rhino and other CAD apps write it (#1027). A fill or stroke chosen while the Type tool has characters selected colours just those characters (#1063). Scaled type shows its scaled font size, leading, baseline shift and horizontal scale in the Character panel, and a size typed there is the size you get (#1034). Character panel menu › Proportional Metrics sets Japanese and Chinese full-width glyphs on the font's proportional widths in horizontal type (#966). Align, Distribute, Move, Arrange, Lock, Hide, Ungroup and other Object commands that would change nothing no longer record an undo step or mark the document changed (#1060). Spot colours defined in Lab open with their Lab values from `.ai` and PDF files, instead of as wrong RGB colours or their CMYK equivalents (#1032). Build scripts leave a generated source they wouldn't change untouched, so the next build doesn't compile most of the workspace again where file times lose their sub-second part (#1051). A fill colour recolours the gradient mesh point last clicked with the Mesh tool or Direct Selection, and a Mesh tool click on a mesh's point selects that mesh (#1052). The All Tools drawer stays two tools wide instead of filling a wide window (#1054). Text Wrap's Make, Release and Options refuse a list of object ids with a malformed or unknown id instead of skipping it (#1055). A freeform gradient line with a malformed point index is refused instead of being shortened to the indices that are valid (#1056). The Corners dialog (`ui.corners`) refuses a malformed object id or a listed anchor that isn't a corner instead of editing the selection or the other corners (#1057). New View refuses a centre that isn't exactly [x, y] and a rotation that isn't a number, instead of trimming the one and saving the other as 0° (#1058). The command sweeps try junk scale, spacing and artboard values too, which they had skipped since before those bugs were fixed (#1059). EPS and PostScript `arc` and `arcn` with huge angles no longer hang the import, and an arc whose angles are whole turns apart is empty, as the PostScript reference defines it (#1062). Align and DisLine truncated
|
||||
| 2026-10-10 | Initial live Revolve (Effect › 3D and Materials, #846, #605), in its own `three-d` crate. Progress docs re-measured and restructured to the craftrules standard (this page, `docs/target-app-parity.md`, `gaps.md`, `ui-parity.md`, the format, hardware, localization, effects and type checklists). Same day: v0.8.0; Effect › Distort, Pixelate and Texture filters (13); German interface; Variables (data merge); Graph Type value axes and tick marks; restart when the first frame never reaches the screen (#964) |
|
||||
| 2026-10-10 | Initial live Revolve (Effect › 3D and Materials, #846, #605), in its own `three-d` crate. Progress docs re-measured and restructured (this page, `docs/target-app-parity.md`, `gaps.md`, `ui-parity.md`, the format, hardware, localization, effects and type checklists). Same day: v0.8.0; Effect › Distort, Pixelate and Texture filters (13); German interface; Variables (data merge); Graph Type value axes and tick marks; restart when the first frame never reaches the screen (#964) |
|
||||
| 2026-10-09 | Radial Blur, Smart Blur, Unsharp Mask (gap 2 begins); 340 commits, the busiest day |
|
||||
| 2026-10-08 | v0.5.0–v0.7.0; community contributions: Pen and shape-tool modifiers, Layers, Artboards, Japanese composition, Hebrew/Arabic type, interface languages, saved selections |
|
||||
| 2026-10-07 | v0.4.0; issue fixes; Layers panel rework |
|
||||
@@ -192,12 +206,24 @@ Ranked; detail in [docs/roadmap.md](docs/roadmap.md) and [docs/gaps.md](docs/gap
|
||||
| 2026-10-01 | Renamed from DrawCraft to VectorCraft |
|
||||
| 2026-09-30 | Repository history begins |
|
||||
|
||||
The full pre-standard record of what landed is kept in [docs/roadmap.md](docs/roadmap.md#progress-log-before-the-progress-docs-standard).
|
||||
The full earlier record of what landed is kept in [docs/roadmap.md](docs/roadmap.md#earlier-progress-log).
|
||||
|
||||
## Revision history
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-11-01 | minor | Progress log row for the Color Effects add-on's Find Nearest Swatch (#4923); headline numbers unchanged. |
|
||||
| 2026-11-01 | minor | Progress log row for the Path Precision Tools add-on's Expand Dashes into Paths (#4922); headline numbers unchanged. |
|
||||
| 2026-11-01 | minor | Progress log row for the Arrange Tools add-on's Randomize Opacity (#4925); headline numbers unchanged. |
|
||||
| 2026-11-01 | minor | Progress log row for the Arrange Tools add-on's Sort Objects (#4917); headline numbers unchanged. |
|
||||
| 2026-11-01 | minor | Progress log row for the Document Tools add-on's New Symbols from Layers (#4927); headline numbers unchanged. |
|
||||
| 2026-11-01 | minor | Progress log row for the Document Tools add-on's Save a Timestamped Copy (#4926); headline numbers unchanged. |
|
||||
| 2026-11-01 | minor | Progress log row for Divide into Dashes (Path Precision Tools add-on, #4950); headline numbers unchanged. |
|
||||
| 2026-11-01 | minor | Progress log row for Group Overlapping Objects (Arrange Tools add-on, #4928); headline numbers unchanged. |
|
||||
| 2026-10-31 | minor | Progress log row for Export for Screens laid out as the reference app's (#4895) and the 2026-10-31 community landings; headline numbers unchanged. |
|
||||
| 2026-10-31 | minor | Progress log row for the Arrange Tools add-on; headline numbers unchanged. |
|
||||
| 2026-10-29 | minor | Progress log row for the Linux Flatpak clipboard's core Wayland protocol text fallback; headline numbers unchanged. |
|
||||
| 2026-10-29 | minor | Progress log row for the Linux clipboard retry backoff; headline numbers unchanged. |
|
||||
| 2026-10-23 | minor | Progress log row for Pathfinder › Trap; Live vector effects hours 6–10 → 4–8; headline numbers unchanged. |
|
||||
| 2026-10-23 | minor | Progress log row for OpenType SVG colour fonts; headline numbers unchanged. |
|
||||
| 2026-10-23 | minor | Progress log row for COLR colour fonts; headline numbers unchanged. |
|
||||
@@ -217,7 +243,7 @@ The full pre-standard record of what landed is kept in [docs/roadmap.md](docs/ro
|
||||
| 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-10 | minor | Method aligned with the standard, 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 | 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 |
|
||||
|
||||
@@ -34,8 +34,9 @@ winsafe = { version = "0.0.29", default-features = false, features = ["kernel",
|
||||
|
||||
# DirectWrite's system font collection and GDI's font list (the desktop app's src/system_fonts.rs,
|
||||
# shared): fonts that font services such as Adobe Fonts load in place (#579, #1258). MIT OR
|
||||
# Apache-2.0.
|
||||
windows = { version = "0.62", features = ["Win32_Graphics_DirectWrite", "Win32_Graphics_Gdi"] }
|
||||
# Apache-2.0. Win32_System_Threading has `GetProcessAffinityMask`, which the desktop app's
|
||||
# src/windows_cores.rs, shared, uses to count the cores the process may run on.
|
||||
windows = { version = "0.62", features = ["Win32_Graphics_DirectWrite", "Win32_Graphics_Gdi", "Win32_System_Threading"] }
|
||||
|
||||
# CoreText's font manager (the desktop app's src/mac_fonts.rs, shared): fonts that apps and font
|
||||
# managers register from their own folders (#579). Zlib OR Apache-2.0 OR MIT.
|
||||
|
||||
@@ -49,6 +49,10 @@ mod prefs_dir;
|
||||
#[cfg(windows)]
|
||||
#[path = "../../vectorcraft/src/system_fonts.rs"]
|
||||
mod system_fonts;
|
||||
/// The desktop app's affinity-mask reader, shared: renders start workers only for usable cores.
|
||||
#[cfg(windows)]
|
||||
#[path = "../../vectorcraft/src/windows_cores.rs"]
|
||||
mod windows_cores;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_engine::file_access::{self, AutomationRoots};
|
||||
@@ -161,6 +165,9 @@ fn help() -> Result<(), String> {
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
// Before the first render: the cores the process may run on.
|
||||
#[cfg(windows)]
|
||||
windows_cores::install();
|
||||
// Before any font lookup: the fonts font services load (#579).
|
||||
#[cfg(windows)]
|
||||
system_fonts::install();
|
||||
|
||||
@@ -116,7 +116,7 @@ fn edit_drags(n: usize) -> Result<(f64, f64), String> {
|
||||
let view = vectorcraft_engine::ViewInfo { zoom: 8.0, ..Default::default() };
|
||||
let err = |e: vectorcraft_engine::EngineError| e.to_string();
|
||||
let mut drag = |down: Point| -> Result<f64, String> {
|
||||
let ev = |kind, y: f64| PointerEvent { kind, pos: down + Vec2::new(0.0, y), mods: Mods::default(), pressure: 1.0 };
|
||||
let ev = |kind, y: f64| PointerEvent { kind, pos: down + Vec2::new(0.0, y), mods: Mods::default(), backquote: false, pressure: 1.0 };
|
||||
s.execute("select.anchors", &json!({"id": id.0, "anchors": [[0, 400]]})).map_err(err)?;
|
||||
s.select_tool("directSelection", view).map_err(err)?;
|
||||
let undo = s.doc().map_err(err)?.history.undo.len();
|
||||
@@ -215,7 +215,7 @@ fn deep(n: usize) -> Result<(), String> {
|
||||
let first = s.doc().map_err(err)?.selection.objects.first().copied();
|
||||
let at = first.and_then(|id| s.doc().ok()?.doc.node(id)?.geometric_bounds()).map(|b| b.center());
|
||||
if let Some(down) = at {
|
||||
let ev = |kind, dx: f64| PointerEvent { kind, pos: down + Vec2::new(dx, 0.0), mods: Mods::default(), pressure: 1.0 };
|
||||
let ev = |kind, dx: f64| PointerEvent { kind, pos: down + Vec2::new(dx, 0.0), mods: Mods::default(), backquote: false, pressure: 1.0 };
|
||||
s.select_tool("selection", view).map_err(err)?;
|
||||
s.pointer(&ev(PointerKind::Down, 0.0), view).map_err(err)?;
|
||||
let mut dx = 0.0;
|
||||
|
||||
@@ -54,7 +54,12 @@ pollster = "1"
|
||||
# Win32_UI_WindowsAndMessaging has `MSG` and `WM_POINTERDOWN`, which src/windows_stylus.rs uses to
|
||||
# read a pen's barrel button from its pointer messages. Win32_UI_Input_Pointer has
|
||||
# `GetPointerPenInfo`, which it uses to read the pen's pressure and which end of the pen is down.
|
||||
windows = { version = "0.62", features = ["Win32_Graphics_DirectWrite", "Win32_Graphics_Gdi", "Win32_UI_Input_Pointer", "Win32_UI_WindowsAndMessaging"] }
|
||||
# Win32_System_Threading has `GetProcessAffinityMask`, which src/windows_cores.rs uses to count the
|
||||
# cores the process may run on. Win32_UI_Input_KeyboardAndMouse has `GetKeyState`, which
|
||||
# src/caps_lock.rs uses to read whether Caps Lock is on.
|
||||
windows = { version = "0.62", features = ["Win32_Graphics_DirectWrite", "Win32_Graphics_Gdi", "Win32_System_Threading", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_Input_Pointer", "Win32_UI_WindowsAndMessaging"] }
|
||||
# The renderer's worker count follows those cores (src/windows_cores.rs).
|
||||
vectorcraft-render = { workspace = true }
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
# The macOS menu bar (src/mac_menu.rs): Tauri's menu crate, AppKit menus behind a safe API (no GTK
|
||||
@@ -86,12 +91,16 @@ pollster = "1"
|
||||
winit = { version = "0.30", default-features = false, features = ["wayland-csd-adwaita-notitle"] }
|
||||
# A drawing tablet's pen pressure under X11 and XWayland (src/tablet.rs): XInput2 raw events on a
|
||||
# second X connection, x11rb's pure-Rust `RustConnection`. MIT OR Apache-2.0, already in the build
|
||||
# through winit with the `xinput` feature.
|
||||
x11rb = { version = "0.13", default-features = false, features = ["xinput"] }
|
||||
# through winit with the `xinput` feature. XKB reads Caps Lock's locked modifier state (src/caps_lock.rs).
|
||||
x11rb = { version = "0.13", default-features = false, features = ["xinput", "xkb"] }
|
||||
|
||||
# Asks the Wayland compositor which GPU it draws with (linux-dmabuf feedback, src/compositor_gpu.rs),
|
||||
# so Automatic draws on that one. Both are already in the build through winit.
|
||||
wayland-client = "0.31"
|
||||
# Copy and Paste of text through the compositor's core data-device protocol on the window's
|
||||
# connection (src/clipboard.rs), where arboard has neither data-control (GNOME) nor X11 (a Flatpak
|
||||
# on a Wayland session). MIT, already in the build through egui-winit.
|
||||
smithay-clipboard = "0.7"
|
||||
wayland-protocols = { version = "0.32", features = ["client", "unstable"] }
|
||||
# The save dialog's parent window on Wayland (src/portal_save.rs): an xdg-foreign handle (in
|
||||
# wayland-protocols' `unstable` set) made on the window's own connection. MIT, already in the build
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#[cfg(all(target_os = "macos", feature = "accessibility", feature = "wgpu"))]
|
||||
use super::gpu;
|
||||
use super::prefs::save_prefs;
|
||||
use super::prefs::{save_prefs, save_prefs_soon};
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
use super::single_instance;
|
||||
#[cfg(all(windows, not(target_vendor = "win7")))]
|
||||
use super::windows_stylus;
|
||||
use super::{App, VectorcraftApp, file_access, window};
|
||||
@@ -66,11 +68,17 @@ impl eframe::App for App {
|
||||
// macOS converts the frame from sRGB to the display's profile (#2249).
|
||||
#[cfg(all(target_os = "macos", feature = "wgpu"))]
|
||||
super::mac_color_space::tag(ui);
|
||||
if let Some(on) = self.host_caps_lock.read_transition() {
|
||||
self.app.set_caps_lock(on);
|
||||
}
|
||||
file_access::unconfined(|| self.app.ui(ui));
|
||||
// Preferences are written when confirmed, not only at exit: a session ended any other way
|
||||
// (the system shutting down, the process ended or crashing) used to lose them (#1747).
|
||||
if self.app.take_prefs_changed() {
|
||||
file_access::unconfined(|| save_prefs(&self.app));
|
||||
} else {
|
||||
// The workspace, panels and other UI settings change without the dialog (#4625).
|
||||
file_access::unconfined(|| save_prefs_soon(&self.app));
|
||||
}
|
||||
// Not while a file dialog's sheet has the keyboard: its name field's composition would go.
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -102,11 +110,13 @@ impl App {
|
||||
vectorcraft_ui_egui::background::wait_all(&mut self.app);
|
||||
return;
|
||||
}
|
||||
// Files from the Finder and the Dock wait while a file dialog is open: what asked for it runs
|
||||
// again on its document when it answers.
|
||||
#[cfg(target_os = "macos")]
|
||||
// Files from the Finder and the Dock (macOS) or from later launches (#4892) wait while a file
|
||||
// dialog is open: what asked for it runs again on its document when it answers.
|
||||
if !self.app.file_dialog_open() {
|
||||
#[cfg(target_os = "macos")]
|
||||
let files = open_documents::take();
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
let files = single_instance::take();
|
||||
if !files.is_empty() {
|
||||
bring_window_forward(ctx);
|
||||
}
|
||||
@@ -144,15 +154,15 @@ fn end_before_teardown(frames: u64) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Show the window when files are opened from the Finder or the Dock: a minimized window comes back
|
||||
/// out of the Dock and the app takes focus, as a document-based Mac app does (#3769).
|
||||
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
|
||||
/// Show the window when files are opened from the Finder or the Dock, or handed over by a later
|
||||
/// launch (#4892): a minimized window comes back and the app takes focus, as a document-based app
|
||||
/// does (#3769).
|
||||
pub(crate) fn bring_window_forward(ctx: &egui::Context) {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Minimized(false));
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Focus);
|
||||
}
|
||||
|
||||
/// Open files handed to the app (command line, macOS Finder and Dock) as documents. A file that
|
||||
/// Open files handed to the app (command line, macOS Finder and Dock, later launches) as documents. A file that
|
||||
/// can't be opened is reported to the user and on stderr; the others still open.
|
||||
pub(crate) fn open_files(app: &mut VectorcraftApp, files: Vec<String>) {
|
||||
for f in files {
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
//! Read the host's Caps Lock state, which gives the drawing tools the precise crosshair while it is on.
|
||||
|
||||
/// Only host transitions replace a value set by a script through `view.capsLock`.
|
||||
#[derive(Default)]
|
||||
pub struct HostCapsLock {
|
||||
previous: Option<bool>,
|
||||
#[cfg(target_os = "linux")]
|
||||
x11: Option<X11CapsLock>,
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
struct X11CapsLock {
|
||||
connection: x11rb::rust_connection::RustConnection,
|
||||
locked: bool,
|
||||
initial_sequence: u16,
|
||||
}
|
||||
|
||||
impl HostCapsLock {
|
||||
pub fn new(window: Option<&winit::window::Window>) -> Self {
|
||||
#[cfg(target_os = "linux")]
|
||||
let x11 = window.and_then(x11_connection);
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
let _ = window;
|
||||
Self {
|
||||
previous: None,
|
||||
#[cfg(target_os = "linux")]
|
||||
x11,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_transition(&mut self) -> Option<bool> {
|
||||
#[cfg(target_os = "linux")]
|
||||
let current = match self.x11.as_mut() {
|
||||
Some(state) => match state.poll() {
|
||||
Some(locked) => Some(locked),
|
||||
None => {
|
||||
self.x11 = None;
|
||||
None
|
||||
}
|
||||
},
|
||||
None => {
|
||||
// Native Wayland does not expose the seat's XKB lock bit through winit or egui.
|
||||
None
|
||||
}
|
||||
};
|
||||
#[cfg(any(target_os = "macos", windows))]
|
||||
let current = Some(read());
|
||||
// This platform has no lock-state reader.
|
||||
#[cfg(not(any(target_os = "macos", windows, target_os = "linux")))]
|
||||
let current = None;
|
||||
self.transition(current)
|
||||
}
|
||||
|
||||
fn transition(&mut self, reading: Option<bool>) -> Option<bool> {
|
||||
reading.and_then(|current| self.observe(current))
|
||||
}
|
||||
|
||||
fn observe(&mut self, current: bool) -> Option<bool> {
|
||||
if self.previous.replace(current) == Some(current) { None } else { Some(current) }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn x11_connection(window: &winit::window::Window) -> Option<X11CapsLock> {
|
||||
use winit::raw_window_handle::{HasWindowHandle as _, RawWindowHandle};
|
||||
use x11rb::protocol::xkb;
|
||||
if !matches!(window.window_handle().ok()?.as_raw(), RawWindowHandle::Xlib(_) | RawWindowHandle::Xcb(_)) {
|
||||
return None;
|
||||
}
|
||||
let (connection, _) = x11rb::connect(None).ok()?;
|
||||
let supported = x11rb::protocol::xkb::use_extension(&connection, 1, 0).ok()?.reply().ok()?.supported;
|
||||
if !supported {
|
||||
return None;
|
||||
}
|
||||
// Subscribe before the initial read so changes during setup remain queued.
|
||||
xkb::select_events(
|
||||
&connection,
|
||||
xkb::ID::USE_CORE_KBD.into(),
|
||||
xkb::EventType::default(),
|
||||
xkb::EventType::default(),
|
||||
xkb::MapPart::default(),
|
||||
xkb::MapPart::default(),
|
||||
&xkb::SelectEventsAux {
|
||||
state_notify: Some(xkb::SelectEventsAuxStateNotify {
|
||||
affect_state: xkb::StatePart::MODIFIER_LOCK,
|
||||
state_details: xkb::StatePart::MODIFIER_LOCK,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.ok()?
|
||||
.check()
|
||||
.ok()?;
|
||||
let (locked, initial_sequence) = read_x11(&connection)?;
|
||||
Some(X11CapsLock { connection, locked, initial_sequence })
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
impl X11CapsLock {
|
||||
fn poll(&mut self) -> Option<bool> {
|
||||
use x11rb::connection::Connection as _;
|
||||
use x11rb::protocol::Event;
|
||||
while let Some(event) = self.connection.poll_for_event().ok()? {
|
||||
if let Event::XkbStateNotify(state) = event
|
||||
// Events queued before the initial GetState reply must not replace that newer snapshot.
|
||||
&& state.sequence.wrapping_sub(self.initial_sequence) < 0x8000
|
||||
{
|
||||
self.locked = lock_modifier_is_set(state.locked_mods);
|
||||
}
|
||||
}
|
||||
Some(self.locked)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn read_x11(connection: &x11rb::rust_connection::RustConnection) -> Option<(bool, u16)> {
|
||||
use x11rb::protocol::xkb;
|
||||
let state = xkb::get_state(connection, xkb::ID::USE_CORE_KBD.into()).ok()?.reply().ok()?;
|
||||
Some((lock_modifier_is_set(state.locked_mods), state.sequence))
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn lock_modifier_is_set(modifiers: x11rb::protocol::xproto::ModMask) -> bool {
|
||||
use x11rb::protocol::xproto::ModMask;
|
||||
u16::from(modifiers) & u16::from(ModMask::LOCK) != 0
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
const MAC_CAPS_LOCK: usize = 1 << 16;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn mac_flags_have_caps_lock(flags: usize) -> bool {
|
||||
flags & MAC_CAPS_LOCK != 0
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub fn read() -> bool {
|
||||
mac_flags_have_caps_lock(objc2_app_kit::NSEvent::modifierFlags_class().bits())
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn windows_state_has_caps_lock(state: i16) -> bool {
|
||||
state & 1 != 0
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[allow(unsafe_code)]
|
||||
pub fn read() -> bool {
|
||||
// GetKeyState reports the toggle bit for the calling UI thread's keyboard state.
|
||||
use windows::Win32::UI::Input::KeyboardAndMouse::{GetKeyState, VK_CAPITAL};
|
||||
// SAFETY: `GetKeyState` takes a virtual-key code by value and reads no caller memory.
|
||||
windows_state_has_caps_lock(unsafe { GetKeyState(i32::from(VK_CAPITAL.0)) })
|
||||
}
|
||||
|
||||
// Querying XWayland for a native Wayland window would read a different keyboard.
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn no_host_reader_preserves_scripted_value_and_next_reading_transitions() {
|
||||
let mut host = super::HostCapsLock::default();
|
||||
let mut session = vectorcraft_engine::Session::new();
|
||||
session.execute("view.capsLock", &serde_json::json!({"on": true})).unwrap();
|
||||
if let Some(on) = host.transition(None) {
|
||||
session.execute("view.capsLock", &serde_json::json!({"on": on})).unwrap();
|
||||
}
|
||||
assert!(session.caps_lock);
|
||||
assert_eq!(host.transition(Some(false)), Some(false));
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn no_x11_reader_has_no_transition() {
|
||||
assert_eq!(super::HostCapsLock::default().read_transition(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scripted_value_survives_unchanged_host_until_transition() {
|
||||
let mut host = super::HostCapsLock::default();
|
||||
let mut session = vectorcraft_engine::Session::new();
|
||||
if let Some(on) = host.observe(false) {
|
||||
session.execute("view.capsLock", &serde_json::json!({"on": on})).unwrap();
|
||||
}
|
||||
session.execute("view.capsLock", &serde_json::json!({"on": true})).unwrap();
|
||||
if let Some(on) = host.observe(false) {
|
||||
session.execute("view.capsLock", &serde_json::json!({"on": on})).unwrap();
|
||||
}
|
||||
assert!(session.caps_lock);
|
||||
session.execute("view.capsLock", &serde_json::json!({"on": false})).unwrap();
|
||||
if let Some(on) = host.observe(true) {
|
||||
session.execute("view.capsLock", &serde_json::json!({"on": on})).unwrap();
|
||||
}
|
||||
assert!(session.caps_lock);
|
||||
if let Some(on) = host.observe(false) {
|
||||
session.execute("view.capsLock", &serde_json::json!({"on": on})).unwrap();
|
||||
}
|
||||
assert!(!session.caps_lock);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn mac_modifier_flags_map_caps_lock() {
|
||||
assert!(!super::mac_flags_have_caps_lock(0));
|
||||
assert!(!super::mac_flags_have_caps_lock(1 << 17));
|
||||
assert!(super::mac_flags_have_caps_lock(1 << 16));
|
||||
assert!(super::mac_flags_have_caps_lock((1 << 16) | (1 << 17)));
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn windows_key_state_maps_toggle_bit() {
|
||||
assert!(!super::windows_state_has_caps_lock(0));
|
||||
assert!(!super::windows_state_has_caps_lock(i16::MIN));
|
||||
assert!(super::windows_state_has_caps_lock(1));
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,10 @@
|
||||
//! and EPS as vector art (#1499) and its PNG or TIFF as they are, as screenshots copy them (on
|
||||
//! Linux arboard reads no other formats). On Wayland, arboard's data-control
|
||||
//! backend reads the compositor's clipboard (X11 through XWayland only sees what X11 apps
|
||||
//! copied), falling back to X11 where the compositor lacks the protocol.
|
||||
//! copied), falling back to X11 where the compositor lacks the protocol, and where there is no
|
||||
//! X11 either (a Flatpak on GNOME) to the compositor's core protocol on the window's connection,
|
||||
//! text only. A clipboard that can't be opened is tried again after a wait that grows to a
|
||||
//! minute, not each time Paste asks.
|
||||
//! - Everywhere: files copied in a file manager paste as the first one that is art (SVG, PDF, EPS,
|
||||
//! a metafile or a bitmap), before the clipboard's other formats (the files' paths as text).
|
||||
|
||||
@@ -19,12 +22,16 @@ use std::path::PathBuf;
|
||||
use vectorcraft_engine::cmd::clipboard::{BITMAP, EPS, FILE_HEAD, Flavour, PDF, PNG, SVG, TEXT, file_flavour};
|
||||
use vectorcraft_ui_egui::SystemClipboard;
|
||||
|
||||
/// This platform's system clipboard.
|
||||
pub fn system_clipboard() -> Box<dyn SystemClipboard> {
|
||||
/// This platform's system clipboard. On Linux, `window`'s Wayland connection carries text where
|
||||
/// arboard can't open (GNOME has no data-control protocol, a Flatpak there no X11).
|
||||
pub fn system_clipboard(window: Option<std::sync::Arc<winit::window::Window>>) -> Box<dyn SystemClipboard> {
|
||||
#[cfg(windows)]
|
||||
let cb = Box::<win::Clipboard>::default();
|
||||
let cb = {
|
||||
let _ = window;
|
||||
Box::<win::Clipboard>::default()
|
||||
};
|
||||
#[cfg(not(windows))]
|
||||
let cb = Box::<portable::Clipboard>::default();
|
||||
let cb = Box::new(portable::Clipboard::new(window));
|
||||
cb
|
||||
}
|
||||
|
||||
@@ -306,10 +313,14 @@ mod win {
|
||||
}
|
||||
}
|
||||
|
||||
/// arboard: one format at a time. Built everywhere so it is checked everywhere.
|
||||
/// arboard, or on a Wayland compositor without its data-control protocol the core protocol's text
|
||||
/// clipboard: one format at a time. Built everywhere so it is checked everywhere.
|
||||
#[cfg_attr(windows, allow(dead_code))]
|
||||
mod portable {
|
||||
use std::borrow::Cow;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use super::{BITMAP, Flavour, PNG, SystemClipboard, TEXT, copied_file, decode_png, png_flavour, text_of};
|
||||
|
||||
@@ -319,19 +330,216 @@ mod portable {
|
||||
Image(usize, usize),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
/// An open clipboard connection.
|
||||
enum Backend {
|
||||
Arboard(arboard::Clipboard),
|
||||
/// The compositor's core data-device protocol on the window's connection (text only).
|
||||
#[cfg(target_os = "linux")]
|
||||
Wayland(super::wayland::TextClipboard),
|
||||
}
|
||||
|
||||
/// What a write of a picture says where the clipboard carries text only.
|
||||
#[cfg(target_os = "linux")]
|
||||
const TEXT_ONLY: &str = "this clipboard carries text only";
|
||||
|
||||
impl Backend {
|
||||
/// arboard (Wayland data-control, else X11), else the core protocol on `window`'s
|
||||
/// Wayland connection. Err: why each failed.
|
||||
fn open(window: Option<&Arc<winit::window::Window>>) -> Result<Self, String> {
|
||||
first_open(
|
||||
|| arboard::Clipboard::new().map(Self::Arboard).map_err(|e| e.to_string()),
|
||||
|| {
|
||||
#[cfg(target_os = "linux")]
|
||||
return window.and_then(|w| super::wayland::TextClipboard::new(w.clone())).map(Self::Wayland).ok_or("not a Wayland window");
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
{
|
||||
let _ = window;
|
||||
Err("not on Linux")
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Arboard(_) => "arboard",
|
||||
#[cfg(target_os = "linux")]
|
||||
Self::Wayland(_) => "the Wayland core protocol (text only)",
|
||||
}
|
||||
}
|
||||
|
||||
fn get_text(&mut self) -> Result<String, String> {
|
||||
match self {
|
||||
Self::Arboard(cb) => cb.get_text().map_err(|e| e.to_string()),
|
||||
#[cfg(target_os = "linux")]
|
||||
Self::Wayland(cb) => cb.get_text(),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_text(&mut self, text: &str) -> Result<(), String> {
|
||||
match self {
|
||||
Self::Arboard(cb) => cb.set_text(text).map_err(|e| e.to_string()),
|
||||
#[cfg(target_os = "linux")]
|
||||
Self::Wayland(cb) => {
|
||||
cb.set_text(text);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_image(&mut self) -> Result<arboard::ImageData<'static>, String> {
|
||||
match self {
|
||||
Self::Arboard(cb) => cb.get_image().map_err(|e| e.to_string()),
|
||||
#[cfg(target_os = "linux")]
|
||||
Self::Wayland(_) => Err(TEXT_ONLY.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_image(&mut self, img: arboard::ImageData<'_>) -> Result<(), String> {
|
||||
match self {
|
||||
Self::Arboard(cb) => cb.set_image(img).map_err(|e| e.to_string()),
|
||||
#[cfg(target_os = "linux")]
|
||||
Self::Wayland(_) => Err(TEXT_ONLY.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn clear(&mut self) -> Result<(), String> {
|
||||
match self {
|
||||
Self::Arboard(cb) => cb.clear().map_err(|e| e.to_string()),
|
||||
#[cfg(target_os = "linux")]
|
||||
Self::Wayland(cb) => {
|
||||
cb.set_text("");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Files copied in a file manager (none where the backend can't read a file list).
|
||||
fn file_list(&mut self) -> Vec<std::path::PathBuf> {
|
||||
match self {
|
||||
Self::Arboard(cb) => cb.get().file_list().unwrap_or_default(),
|
||||
#[cfg(target_os = "linux")]
|
||||
Self::Wayland(_) => vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The first of `arboard` and `wayland` that opens, tried in that order. Err: why each failed.
|
||||
fn first_open<B>(arboard: impl FnOnce() -> Result<B, String>, wayland: impl FnOnce() -> Result<B, &'static str>) -> Result<B, String> {
|
||||
let arboard = match arboard() {
|
||||
Ok(cb) => return Ok(cb),
|
||||
Err(e) => e,
|
||||
};
|
||||
wayland().map_err(|wayland| format!("arboard: {arboard}; Wayland core protocol: {wayland}"))
|
||||
}
|
||||
|
||||
/// Who asks for the clipboard: the background paste check waits out the whole wait after a
|
||||
/// failure, a Copy or Paste of the user's [`USER_RETRY`] at most.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub(super) enum Asker {
|
||||
Probe,
|
||||
User,
|
||||
}
|
||||
|
||||
/// The wait after the first failure to open the clipboard; it doubles with each failed retry.
|
||||
const FIRST_WAIT: Duration = Duration::from_secs(1);
|
||||
/// The longest wait between retries.
|
||||
const MAX_WAIT: Duration = Duration::from_secs(60);
|
||||
/// A Copy or Paste of the user's retries at most this often.
|
||||
const USER_RETRY: Duration = Duration::from_secs(1);
|
||||
|
||||
/// Whether "unavailable" has been logged and not yet answered by "available again": the
|
||||
/// clipboard and its background check hold a handle each, and their failures are one outage.
|
||||
static UNAVAILABLE_LOGGED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// A failure to open, and when to try again.
|
||||
struct Failed {
|
||||
at: Instant,
|
||||
wait: Duration,
|
||||
}
|
||||
|
||||
/// A handle opened on first use and kept, or after a failure opened again only once its wait is
|
||||
/// over: where neither backend opens, asking a few times a second must not reconnect each time.
|
||||
pub(super) struct Reopen<B> {
|
||||
handle: Option<B>,
|
||||
failed: Option<Failed>,
|
||||
/// The outage flag shared with the other handles ([`UNAVAILABLE_LOGGED`]).
|
||||
logged: &'static AtomicBool,
|
||||
}
|
||||
|
||||
impl<B> Reopen<B> {
|
||||
pub(super) fn new(logged: &'static AtomicBool) -> Self {
|
||||
Self { handle: None, failed: None, logged }
|
||||
}
|
||||
|
||||
/// The open handle, opened with `open` when there is none and `asker` may try at `now`.
|
||||
/// `name` names an opened handle in the log.
|
||||
pub(super) fn get(
|
||||
&mut self,
|
||||
asker: Asker,
|
||||
now: Instant,
|
||||
open: impl FnOnce() -> Result<B, String>,
|
||||
name: impl Fn(&B) -> &'static str,
|
||||
) -> Option<&mut B> {
|
||||
if self.handle.is_none() {
|
||||
if let Some(f) = &self.failed {
|
||||
let wait = match asker {
|
||||
Asker::Probe => f.wait,
|
||||
Asker::User => f.wait.min(USER_RETRY),
|
||||
};
|
||||
if now.saturating_duration_since(f.at) < wait {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
match open() {
|
||||
Ok(handle) => {
|
||||
if self.logged.swap(false, Ordering::Relaxed) {
|
||||
log::info!("the system clipboard is available again, through {}", name(&handle));
|
||||
} else {
|
||||
log::debug!("system clipboard opened through {}", name(&handle));
|
||||
}
|
||||
self.failed = None;
|
||||
self.handle = Some(handle);
|
||||
}
|
||||
Err(why) => {
|
||||
let wait = match &self.failed {
|
||||
Some(f) => (f.wait * 2).min(MAX_WAIT),
|
||||
None => FIRST_WAIT,
|
||||
};
|
||||
if self.logged.swap(true, Ordering::Relaxed) {
|
||||
log::debug!("the system clipboard is still unavailable ({why}); next try in {wait:?}");
|
||||
} else {
|
||||
log::warn!("the system clipboard is unavailable ({why}): Copy and Paste stay inside VectorCraft");
|
||||
}
|
||||
self.failed = Some(Failed { at: now, wait });
|
||||
}
|
||||
}
|
||||
}
|
||||
self.handle.as_mut()
|
||||
}
|
||||
|
||||
/// The handle if one is open, without trying to open one.
|
||||
pub(super) fn opened(&mut self) -> Option<&mut B> {
|
||||
self.handle.as_mut()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Clipboard {
|
||||
/// Kept open: on Linux the copied data lives as long as the handle does.
|
||||
cb: Option<arboard::Clipboard>,
|
||||
cb: Reopen<Backend>,
|
||||
/// The app window, for the Wayland core protocol (on its connection).
|
||||
window: Option<Arc<winit::window::Window>>,
|
||||
ours: Option<Ours>,
|
||||
}
|
||||
|
||||
impl Clipboard {
|
||||
fn cb(&mut self) -> Result<&mut arboard::Clipboard, String> {
|
||||
if self.cb.is_none() {
|
||||
self.cb = Some(arboard::Clipboard::new().map_err(|e| e.to_string())?);
|
||||
}
|
||||
self.cb.as_mut().ok_or_else(|| "no system clipboard".to_string())
|
||||
pub fn new(window: Option<Arc<winit::window::Window>>) -> Self {
|
||||
Self { cb: Reopen::new(&UNAVAILABLE_LOGGED), window, ours: None }
|
||||
}
|
||||
|
||||
fn cb(&mut self, asker: Asker) -> Result<&mut Backend, String> {
|
||||
let window = self.window.as_ref();
|
||||
self.cb.get(asker, Instant::now(), || Backend::open(window), Backend::name).ok_or_else(|| "no system clipboard".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,19 +550,19 @@ mod portable {
|
||||
Some(_) => None,
|
||||
None => flavours.iter().find(|f| f.mime == PNG).and_then(|f| decode_png(&f.data)),
|
||||
};
|
||||
let cb = self.cb()?;
|
||||
let cb = self.cb(Asker::User)?;
|
||||
let ours = match (text, image) {
|
||||
(Some(t), _) => {
|
||||
cb.set_text(t.as_str()).map_err(|e| e.to_string())?;
|
||||
cb.set_text(t.as_str())?;
|
||||
Some(Ours::Text(t))
|
||||
}
|
||||
(None, Some(img)) => {
|
||||
let (width, height) = (img.width() as usize, img.height() as usize);
|
||||
cb.set_image(arboard::ImageData { width, height, bytes: Cow::Owned(img.into_raw()) }).map_err(|e| e.to_string())?;
|
||||
cb.set_image(arboard::ImageData { width, height, bytes: Cow::Owned(img.into_raw()) })?;
|
||||
Some(Ours::Image(width, height))
|
||||
}
|
||||
(None, None) => {
|
||||
cb.clear().map_err(|e| e.to_string())?;
|
||||
cb.clear()?;
|
||||
None
|
||||
}
|
||||
};
|
||||
@@ -363,7 +571,7 @@ mod portable {
|
||||
}
|
||||
|
||||
fn holds_ours(&mut self) -> bool {
|
||||
let Some(cb) = self.cb.as_mut() else { return false };
|
||||
let Some(cb) = self.cb.opened() else { return false };
|
||||
match &self.ours {
|
||||
Some(Ours::Text(t)) => cb.get_text().is_ok_and(|now| now == *t),
|
||||
Some(Ours::Image(w, h)) => cb.get_image().is_ok_and(|now| (now.width, now.height) == (*w, *h)),
|
||||
@@ -372,8 +580,8 @@ mod portable {
|
||||
}
|
||||
|
||||
fn read(&mut self, mimes: &[&'static str]) -> Option<Flavour> {
|
||||
let cb = self.cb().ok()?;
|
||||
if let Some(f) = cb.get().file_list().ok().and_then(|paths| copied_file(paths, mimes)) {
|
||||
let cb = self.cb(Asker::User).ok()?;
|
||||
if let Some(f) = copied_file(cb.file_list(), mimes) {
|
||||
return Some(f);
|
||||
}
|
||||
mimes.iter().find_map(|m| {
|
||||
@@ -402,7 +610,309 @@ mod portable {
|
||||
if super::mac::has(mimes) {
|
||||
return true;
|
||||
}
|
||||
mimes.contains(&TEXT) && self.cb().is_ok_and(|cb| cb.get_text().is_ok_and(|t| !t.is_empty()))
|
||||
mimes.contains(&TEXT) && self.cb(Asker::Probe).is_ok_and(|cb| cb.get_text().is_ok_and(|t| !t.is_empty()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::cell::Cell;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A clock that starts now and moves only when told.
|
||||
struct Clock(Cell<Instant>);
|
||||
|
||||
impl Clock {
|
||||
fn new() -> Self {
|
||||
Self(Cell::new(Instant::now()))
|
||||
}
|
||||
fn now(&self) -> Instant {
|
||||
self.0.get()
|
||||
}
|
||||
fn advance(&self, by: Duration) {
|
||||
self.0.set(self.0.get() + by);
|
||||
}
|
||||
}
|
||||
|
||||
fn fail() -> Result<u8, String> {
|
||||
Err("no compositor".into())
|
||||
}
|
||||
|
||||
fn name(_: &u8) -> &'static str {
|
||||
"fake"
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_paste_check_retries_with_a_growing_wait() {
|
||||
static LOGGED: AtomicBool = AtomicBool::new(false);
|
||||
let (clock, attempts) = (Clock::new(), Cell::new(0));
|
||||
let mut cb = Reopen::new(&LOGGED);
|
||||
// Every 250 ms for a minute, as `clipboard_probe` asks.
|
||||
for _ in 0..240 {
|
||||
let open = || {
|
||||
attempts.set(attempts.get() + 1);
|
||||
fail()
|
||||
};
|
||||
assert!(cb.get(Asker::Probe, clock.now(), open, name).is_none());
|
||||
clock.advance(Duration::from_millis(250));
|
||||
}
|
||||
// 0, 1, 3, 7, 15 and 31 s: not one connection a check.
|
||||
assert_eq!(attempts.get(), 6);
|
||||
assert!(LOGGED.load(Ordering::Relaxed), "the outage is logged");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_wait_stops_growing_at_a_minute() {
|
||||
static LOGGED: AtomicBool = AtomicBool::new(false);
|
||||
let (clock, attempts) = (Clock::new(), Cell::new(0));
|
||||
let mut cb = Reopen::new(&LOGGED);
|
||||
for _ in 0..(20 * 60) {
|
||||
let open = || {
|
||||
attempts.set(attempts.get() + 1);
|
||||
fail()
|
||||
};
|
||||
let _ = cb.get(Asker::Probe, clock.now(), open, name);
|
||||
clock.advance(Duration::from_secs(1));
|
||||
}
|
||||
// 0, 1, 3, 7, 15, 31, 63 s, then a minute apart until 20 minutes are up.
|
||||
assert_eq!(attempts.get(), 7 + (20 * 60 - 63 - 1) / 60);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_copy_or_paste_retries_at_most_once_a_second() {
|
||||
static LOGGED: AtomicBool = AtomicBool::new(false);
|
||||
let (clock, attempts) = (Clock::new(), Cell::new(0));
|
||||
let mut cb = Reopen::new(&LOGGED);
|
||||
let mut ask = |asker| {
|
||||
let open = || {
|
||||
attempts.set(attempts.get() + 1);
|
||||
fail()
|
||||
};
|
||||
let _ = cb.get(asker, clock.now(), open, name);
|
||||
};
|
||||
// Long after the paste check gave up for a minute, a held Ctrl+V for 3 s at 30 a second.
|
||||
for _ in 0..8 {
|
||||
ask(Asker::Probe);
|
||||
clock.advance(Duration::from_secs(70));
|
||||
}
|
||||
let before = attempts.get();
|
||||
for _ in 0..90 {
|
||||
ask(Asker::User);
|
||||
clock.advance(Duration::from_millis(33));
|
||||
}
|
||||
assert_eq!(attempts.get() - before, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_success_is_kept_and_ends_the_outage() {
|
||||
static LOGGED: AtomicBool = AtomicBool::new(false);
|
||||
let clock = Clock::new();
|
||||
let mut cb = Reopen::new(&LOGGED);
|
||||
assert!(cb.get(Asker::Probe, clock.now(), fail, name).is_none());
|
||||
assert!(LOGGED.load(Ordering::Relaxed));
|
||||
clock.advance(FIRST_WAIT);
|
||||
assert_eq!(cb.get(Asker::Probe, clock.now(), || Ok(7), name).copied(), Some(7));
|
||||
assert!(!LOGGED.load(Ordering::Relaxed), "the recovery is logged");
|
||||
// Kept: not opened again, even where opening would now fail.
|
||||
assert_eq!(cb.get(Asker::Probe, clock.now(), fail, name).copied(), Some(7));
|
||||
assert_eq!(cb.opened().copied(), Some(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arboard_is_tried_before_the_wayland_core_protocol() {
|
||||
let wayland_tried = Cell::new(false);
|
||||
let wayland = |ok| {
|
||||
wayland_tried.set(true);
|
||||
if ok { Ok("wayland") } else { Err("not a Wayland window") }
|
||||
};
|
||||
// Where arboard opens (data-control, X11) the core protocol isn't tried.
|
||||
assert_eq!(first_open(|| Ok("arboard"), || wayland(true)), Ok("arboard"));
|
||||
assert!(!wayland_tried.get());
|
||||
// The Flatpak on GNOME: no data-control, no X11.
|
||||
assert_eq!(first_open(|| Err("no X11".into()), || wayland(true)), Ok("wayland"));
|
||||
assert!(wayland_tried.get());
|
||||
// Neither: every reason is kept for the log.
|
||||
assert_eq!(
|
||||
first_open(|| Err("no X11".into()), || wayland(false)),
|
||||
Err::<&str, _>("arboard: no X11; Wayland core protocol: not a Wayland window".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_outage_is_logged_once_across_handles() {
|
||||
static LOGGED: AtomicBool = AtomicBool::new(false);
|
||||
let clock = Clock::new();
|
||||
let (mut ui, mut probe) = (Reopen::new(&LOGGED), Reopen::new(&LOGGED));
|
||||
assert!(probe.get(Asker::Probe, clock.now(), fail, name).is_none());
|
||||
assert!(LOGGED.load(Ordering::Relaxed));
|
||||
// The other handle's failure is the same outage: `swap` reports it already logged.
|
||||
assert!(ui.get(Asker::User, clock.now(), fail, name).is_none());
|
||||
assert!(LOGGED.load(Ordering::Relaxed));
|
||||
assert!(probe.opened().is_none() && ui.opened().is_none());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The compositor's clipboard through the core data-device protocol (`wl_data_device`), on the
|
||||
/// window's own Wayland connection: what Copy and Paste use where the compositor lacks the
|
||||
/// data-control protocol arboard needs (GNOME) and there is no X11 to fall back on (the Flatpak on
|
||||
/// a Wayland session). Text only (smithay-clipboard), and only while the window has focus, as the
|
||||
/// protocol has it. A read waits [`LOAD_WAIT`] at most: smithay-clipboard waits for the owner's
|
||||
/// data with no limit, so reads run on a worker of their own.
|
||||
#[cfg(target_os = "linux")]
|
||||
mod wayland {
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
|
||||
/// The longest a read waits for the selection's owner to send its text.
|
||||
const LOAD_WAIT: Duration = Duration::from_secs(2);
|
||||
/// What a read says once its worker has stopped.
|
||||
const GONE: &str = "the clipboard's reader stopped";
|
||||
|
||||
pub struct TextClipboard {
|
||||
/// Writes: a store only hands the text to smithay-clipboard's thread.
|
||||
cb: smithay_clipboard::Clipboard,
|
||||
/// Reads, on a worker with a second clipboard on the same connection.
|
||||
loads: Loads,
|
||||
/// Held as long as the clipboards: their connection is the window's.
|
||||
_window: Arc<winit::window::Window>,
|
||||
}
|
||||
|
||||
/// The read worker's clipboard and the window whose connection it uses, dropped in that order.
|
||||
struct Loader {
|
||||
cb: smithay_clipboard::Clipboard,
|
||||
_window: Arc<winit::window::Window>,
|
||||
}
|
||||
|
||||
impl Loader {
|
||||
fn load(&self) -> Result<String, String> {
|
||||
self.cb.load().map_err(|e| e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl TextClipboard {
|
||||
/// None when `window` isn't a Wayland window or the read worker can't start.
|
||||
#[allow(unsafe_code)]
|
||||
pub fn new(window: Arc<winit::window::Window>) -> Option<Self> {
|
||||
use winit::raw_window_handle::{HasDisplayHandle as _, RawDisplayHandle};
|
||||
let RawDisplayHandle::Wayland(d) = window.display_handle().ok()?.as_raw() else { return None };
|
||||
let display = d.display.as_ptr();
|
||||
// SAFETY: the display is winit's open `wl_display`, which outlives `window`; each
|
||||
// clipboard is held beside a clone of `window` and dropped before it. smithay-clipboard
|
||||
// makes its own event queue on the display and doesn't close it.
|
||||
let (cb, reader) = unsafe { (smithay_clipboard::Clipboard::new(display), smithay_clipboard::Clipboard::new(display)) };
|
||||
let loader = Loader { cb: reader, _window: window.clone() };
|
||||
// `loader.load()`, not `loader.cb.load()`: a closure moves only the fields it names,
|
||||
// and the worker must hold the window as long as its clipboard.
|
||||
let loads = Loads::spawn(move || loader.load())?;
|
||||
Some(Self { cb, loads, _window: window })
|
||||
}
|
||||
|
||||
pub fn get_text(&mut self) -> Result<String, String> {
|
||||
self.loads.get(LOAD_WAIT)
|
||||
}
|
||||
|
||||
pub fn set_text(&self, text: &str) {
|
||||
self.cb.store(text);
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads made on a worker thread and waited for a bounded time: a selection owner that never
|
||||
/// sends its data holds the worker, not the caller.
|
||||
struct Loads {
|
||||
requests: mpsc::Sender<()>,
|
||||
answers: mpsc::Receiver<Result<String, String>>,
|
||||
/// A read that timed out and hasn't answered yet: until it does, reads don't queue behind it.
|
||||
pending: bool,
|
||||
}
|
||||
|
||||
impl Loads {
|
||||
fn spawn(mut load: impl FnMut() -> Result<String, String> + Send + 'static) -> Option<Self> {
|
||||
let (requests, asked) = mpsc::channel::<()>();
|
||||
let (answer, answers) = mpsc::channel();
|
||||
let work = move || {
|
||||
while asked.recv().is_ok() {
|
||||
if answer.send(load()).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
std::thread::Builder::new().name("wayland-clipboard-read".into()).spawn(work).ok()?;
|
||||
Some(Self { requests, answers, pending: false })
|
||||
}
|
||||
|
||||
/// The text, or why there is none, after `wait` at most.
|
||||
fn get(&mut self, wait: Duration) -> Result<String, String> {
|
||||
if self.pending {
|
||||
match self.answers.try_recv() {
|
||||
// The late answer of the read that timed out: stale by now.
|
||||
Ok(_) => self.pending = false,
|
||||
Err(mpsc::TryRecvError::Empty) => return Err("the clipboard's owner hasn't answered an earlier read".into()),
|
||||
Err(mpsc::TryRecvError::Disconnected) => return Err(GONE.into()),
|
||||
}
|
||||
}
|
||||
self.requests.send(()).map_err(|_| GONE.to_string())?;
|
||||
match self.answers.recv_timeout(wait) {
|
||||
Ok(text) => text,
|
||||
Err(mpsc::RecvTimeoutError::Timeout) => {
|
||||
self.pending = true;
|
||||
Err(format!("the clipboard's owner didn't send its text within {wait:?}"))
|
||||
}
|
||||
Err(mpsc::RecvTimeoutError::Disconnected) => Err(GONE.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::Instant;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_read_that_answers_in_time_gives_the_text() {
|
||||
let mut loads = Loads::spawn(|| Ok("<svg/>".into())).unwrap();
|
||||
assert_eq!(loads.get(Duration::from_secs(5)), Ok("<svg/>".to_string()));
|
||||
assert_eq!(loads.get(Duration::from_secs(5)), Ok("<svg/>".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_owner_that_never_answers_holds_the_worker_not_the_caller() {
|
||||
let (release, gate) = mpsc::channel::<()>();
|
||||
let mut calls = 0;
|
||||
let mut loads = Loads::spawn(move || {
|
||||
calls += 1;
|
||||
if calls == 1 {
|
||||
// The first owner sends nothing until released.
|
||||
let _ = gate.recv();
|
||||
return Ok("stale".into());
|
||||
}
|
||||
Ok("fresh".into())
|
||||
})
|
||||
.unwrap();
|
||||
let start = Instant::now();
|
||||
assert!(loads.get(Duration::from_millis(50)).is_err(), "the first read times out");
|
||||
// While it is unanswered, a read doesn't queue behind it or wait at all.
|
||||
assert!(loads.get(Duration::from_secs(60)).is_err());
|
||||
assert!(start.elapsed() < Duration::from_secs(30), "no read waited past its bound");
|
||||
release.send(()).unwrap();
|
||||
// Once the late answer is in, it is dropped and a new read is made.
|
||||
let text = (0..200).find_map(|_| {
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
loads.get(Duration::from_secs(5)).ok()
|
||||
});
|
||||
assert_eq!(text.as_deref(), Some("fresh"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_read_after_the_worker_stops_says_so() {
|
||||
let mut loads = Loads::spawn(|| Ok(String::new())).unwrap();
|
||||
// The worker ends when its answers can't be sent: drop them by hand.
|
||||
let (_, dead) = mpsc::channel();
|
||||
drop(std::mem::replace(&mut loads.answers, dead));
|
||||
assert_eq!(loads.get(Duration::from_millis(50)), Err(GONE.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+37
-19
@@ -140,38 +140,56 @@ pub fn backends(default: Backends, env_set: bool, opengl: bool) -> Backends {
|
||||
}
|
||||
|
||||
/// Whether a Windows start lists OpenGL's adapters beside Direct3D 12's: only when it leaves an
|
||||
/// adapter out (an earlier start crashed on it, or a restart failed on it) or DX12 lists none
|
||||
/// (Windows 7). wgpu's OpenGL backend keeps a hidden window on a thread that pumps no messages,
|
||||
/// and Windows waits on it when the input language changes, so the whole app hung on a switch to
|
||||
/// another keyboard layout (#4150, gfx-rs/wgpu#10504), even drawing with DX12.
|
||||
/// adapter out (an earlier start crashed on it, or a restart failed on it), when
|
||||
/// `WGPU_ADAPTER_NAME` names one (it may be an OpenGL adapter), or when DX12 lists no hardware
|
||||
/// adapter (Windows 7, or a GPU without a DX12 driver, which then draws through OpenGL as before
|
||||
/// rather than on the CPU with WARP). wgpu's OpenGL backend keeps a hidden window on a thread that
|
||||
/// pumps no messages, and Windows waits on it when the input language changes, so the whole app
|
||||
/// hung on a switch to another keyboard layout (#4150, gfx-rs/wgpu#10504), even drawing with DX12.
|
||||
pub fn windows_opengl(left_out: &LeftOut) -> bool {
|
||||
left_out.opengl_only || !left_out.adapters.is_empty() || !skipped().is_empty() || !dx12_lists_adapters()
|
||||
opengl_needed(left_out, !skipped().is_empty(), std::env::var_os(NAME_ENV).is_some(), dx12_lists_hardware)
|
||||
}
|
||||
|
||||
/// Whether Direct3D 12 lists any adapter, WARP's software one included. A driver that crashes here
|
||||
/// crashes a start while it lists the adapters, which the next start answers with OpenGL alone.
|
||||
/// [`windows_opengl`], with a restart leaving adapters out (`skipping`) and an adapter named
|
||||
/// (`named`): `dx12_hardware` is asked only when nothing else decides.
|
||||
fn opengl_needed(left_out: &LeftOut, skipping: bool, named: bool, dx12_hardware: impl FnOnce() -> bool) -> bool {
|
||||
left_out.opengl_only || !left_out.adapters.is_empty() || skipping || named || !dx12_hardware()
|
||||
}
|
||||
|
||||
/// Whether Direct3D 12 lists a hardware adapter; WARP, its software one, doesn't count. A driver
|
||||
/// that crashes here crashes a start while it lists the adapters, which the next start answers with
|
||||
/// OpenGL alone.
|
||||
#[cfg(windows)]
|
||||
fn dx12_lists_adapters() -> bool {
|
||||
fn dx12_lists_hardware() -> bool {
|
||||
let mut descriptor = wgpu::InstanceDescriptor::new_without_display_handle();
|
||||
descriptor.backends = Backends::DX12;
|
||||
// FXC is part of Windows, as for the window's own instance (#1194).
|
||||
descriptor.backend_options.dx12.shader_compiler = wgpu::Dx12Compiler::Fxc;
|
||||
// The shader compiler the window's own instance takes (#1194).
|
||||
descriptor.backend_options.dx12.shader_compiler = wgpu::Dx12Compiler::from_env().unwrap_or(wgpu::Dx12Compiler::Fxc);
|
||||
let instance = wgpu::Instance::new(descriptor);
|
||||
let found = pollster::block_on(instance.enumerate_adapters(Backends::DX12)).len();
|
||||
log::info!("Direct3D 12 lists {found} graphics adapters");
|
||||
found > 0
|
||||
let kinds: Vec<DeviceType> = pollster::block_on(instance.enumerate_adapters(Backends::DX12)).iter().map(|a| a.get_info().device_type).collect();
|
||||
log::info!("Direct3D 12 lists these graphics adapters: {kinds:?}");
|
||||
any_hardware(&kinds)
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn dx12_lists_adapters() -> bool {
|
||||
fn dx12_lists_hardware() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// The largest texture side to ask the device for, which bounds the window's surface: the
|
||||
/// adapter's own limit when it is above eframe's `default` (8192 px), so a window larger than
|
||||
/// that keeps its surface (#1459). Never below `default`, which is what was asked for before.
|
||||
pub fn surface_limit(default: u32, adapter: u32) -> u32 {
|
||||
default.max(adapter)
|
||||
/// Is one of the adapters of these kinds hardware, not software such as WARP?
|
||||
#[cfg(any(windows, test))]
|
||||
fn any_hardware(kinds: &[DeviceType]) -> bool {
|
||||
kinds.iter().any(|k| *k != DeviceType::Cpu)
|
||||
}
|
||||
|
||||
/// The limits to ask the device for: what eframe `asked` for, lowered to what the `adapter` offers
|
||||
/// wherever it offers less, so a GPU short of one of them still gets a device (#2105). The texture
|
||||
/// side, which bounds the window's surface, is the adapter's own, above eframe's 8192 px when the
|
||||
/// adapter allows more, so a window larger than that keeps its surface (#1459).
|
||||
pub fn device_limits(asked: wgpu::Limits, adapter: &wgpu::Limits) -> wgpu::Limits {
|
||||
let mut limits = asked.or_worse_values_from(adapter);
|
||||
limits.max_texture_dimension_2d = adapter.max_texture_dimension_2d;
|
||||
limits
|
||||
}
|
||||
|
||||
/// One adapter, as [`adapter_order`] sees it.
|
||||
|
||||
@@ -5,11 +5,34 @@ fn gpu(name: &str, backend: Backend, device_type: DeviceType) -> Candidate {
|
||||
}
|
||||
|
||||
/// #1459: a 9048 px wide window (two display scales' ratio past 8192) fits an adapter that
|
||||
/// takes 16384 px textures; an adapter with less than eframe asks for changes nothing.
|
||||
/// takes 16384 px textures.
|
||||
#[test]
|
||||
fn the_surface_may_be_as_large_as_the_adapter_allows() {
|
||||
assert_eq!(surface_limit(8192, 16384), 16384);
|
||||
assert_eq!(surface_limit(8192, 4096), 8192);
|
||||
let adapter = wgpu::Limits { max_texture_dimension_2d: 16384, ..wgpu::Limits::default() };
|
||||
let asked = wgpu::Limits { max_texture_dimension_2d: 8192, ..wgpu::Limits::default() };
|
||||
assert_eq!(device_limits(asked, &adapter).max_texture_dimension_2d, 16384);
|
||||
}
|
||||
|
||||
/// #2105: a GPU that offers less than eframe asks for on any limit (here a Raspberry Pi 5's V3D 7.1:
|
||||
/// 15 inter-stage variables on Vulkan, 4096 px textures on OpenGL) gets a device with what it
|
||||
/// offers, instead of failing and leaving the window to the software rasterizer. A GPU that offers
|
||||
/// more is asked for no more than before, except for the texture side.
|
||||
#[test]
|
||||
fn the_device_asks_for_no_more_than_the_adapter_offers() {
|
||||
let asked = wgpu::Limits { max_texture_dimension_2d: 8192, ..wgpu::Limits::default() };
|
||||
let vulkan = wgpu::Limits { max_inter_stage_shader_variables: 15, max_texture_dimension_2d: 7680, ..wgpu::Limits::default() };
|
||||
let limits = device_limits(asked.clone(), &vulkan);
|
||||
assert_eq!((limits.max_inter_stage_shader_variables, limits.max_texture_dimension_2d), (15, 7680));
|
||||
assert!(limits.check_limits(&vulkan), "{limits:?}");
|
||||
let gl = wgpu::Limits { max_texture_dimension_2d: 4096, ..wgpu::Limits::downlevel_webgl2_defaults() };
|
||||
let webgl2 = wgpu::Limits { max_texture_dimension_2d: 8192, ..wgpu::Limits::downlevel_webgl2_defaults() };
|
||||
let limits = device_limits(webgl2.clone(), &gl);
|
||||
assert_eq!(limits.max_texture_dimension_2d, 4096);
|
||||
assert!(limits.check_limits(&gl), "{limits:?}");
|
||||
let ample = wgpu::Limits { max_texture_dimension_2d: 8192, ..wgpu::Limits::default() };
|
||||
assert_eq!(device_limits(asked.clone(), &le), asked);
|
||||
let better = wgpu::Limits { max_inter_stage_shader_variables: 31, max_texture_dimension_2d: 8192, ..wgpu::Limits::default() };
|
||||
assert_eq!(device_limits(asked.clone(), &better), asked, "never more than eframe asked for");
|
||||
}
|
||||
|
||||
fn names(c: &[Candidate], order: &[usize]) -> Vec<String> {
|
||||
@@ -472,6 +495,31 @@ fn windows_lists_opengl_once_a_start_leaves_an_adapter_out() {
|
||||
assert_eq!(listing.backends(backends(Backends::all(), false, true)), Backends::GL);
|
||||
}
|
||||
|
||||
/// OpenGL stays where it drew before #4150's fix: a GPU without a Direct3D 12 driver (DX12 lists
|
||||
/// WARP alone) and an adapter `WGPU_ADAPTER_NAME` names; a start with a DX12 GPU lists DX12 alone.
|
||||
#[test]
|
||||
fn windows_lists_opengl_without_a_dx12_gpu_or_with_an_adapter_named() {
|
||||
let start = LeftOut::default();
|
||||
let asked = std::cell::Cell::new(false);
|
||||
let hardware = |found: bool| {
|
||||
asked.set(true);
|
||||
found
|
||||
};
|
||||
assert!(!opengl_needed(&start, false, false, || hardware(true)), "a DX12 GPU: DX12 alone (#4150)");
|
||||
assert!(asked.replace(false));
|
||||
assert!(opengl_needed(&start, false, false, || hardware(false)), "WARP alone: the GPU draws through OpenGL, not the CPU");
|
||||
assert!(opengl_needed(&start, false, true, || hardware(true)), "a named adapter may be an OpenGL one");
|
||||
assert!(opengl_needed(&start, true, false, || hardware(true)), "a restart left an adapter out");
|
||||
let listing = LeftOut { opengl_only: true, ..LeftOut::default() };
|
||||
asked.set(false);
|
||||
assert!(opengl_needed(&listing, false, false, || hardware(true)));
|
||||
assert!(!asked.get(), "DX12 isn't listed once OpenGL must be");
|
||||
assert!(any_hardware(&[DeviceType::Cpu, DeviceType::IntegratedGpu]));
|
||||
assert!(any_hardware(&[DeviceType::VirtualGpu]));
|
||||
assert!(!any_hardware(&[DeviceType::Cpu]), "WARP");
|
||||
assert!(!any_hardware(&[]), "Windows 7");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn power_preference_follows_the_environment_then_the_preference() {
|
||||
assert_eq!(power_preference(None, None), AUTOMATIC);
|
||||
|
||||
@@ -255,6 +255,7 @@ pub(crate) fn services(parent: Parent, ui: egui::Context) -> Services {
|
||||
.as_ref()
|
||||
.and_then(|w| w.window_handle().ok())
|
||||
.is_some_and(|h| matches!(h.as_raw(), RawWindowHandle::Xlib(_) | RawWindowHandle::Xcb(_)));
|
||||
let (clip, clip_probe) = (parent.clone(), parent.clone());
|
||||
let (p1, p2, p3, p4, p5) = (parent.clone(), parent.clone(), parent.clone(), parent.clone(), parent);
|
||||
#[cfg(target_os = "macos")]
|
||||
let sampled_ui = ui.clone();
|
||||
@@ -286,11 +287,12 @@ pub(crate) fn services(parent: Parent, ui: egui::Context) -> Services {
|
||||
fileio::write_file_with(p, fill).map_err(|e| e.to_string())
|
||||
})),
|
||||
// Every format Copy offers and Paste reads (menu-bar Paste never sees egui's Paste event).
|
||||
system_clipboard: Some(clipboard::system_clipboard()),
|
||||
system_clipboard: Some(clipboard::system_clipboard(clip)),
|
||||
// Linux checks whether Paste has something to take on a background thread: an X11 clipboard
|
||||
// owner that never answers holds a read for up to 4 s. Windows only asks which formats the
|
||||
// clipboard holds and macOS asks the pasteboard server, so they check in line.
|
||||
clipboard_probe: cfg!(target_os = "linux").then(|| Box::new(clipboard::system_clipboard) as ClipboardProbeFactory),
|
||||
// The probe's handle is made on its own thread, from the window (`Send`), not its raw display.
|
||||
clipboard_probe: cfg!(target_os = "linux").then(|| Box::new(move || clipboard::system_clipboard(clip_probe)) as ClipboardProbeFactory),
|
||||
// Help → Discord / website / GitHub, the Discord button, About and Home links.
|
||||
open_url: Some(Box::new(|url: &str| {
|
||||
let _ = webbrowser::open(url);
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
//! VectorCraft desktop app.
|
||||
//!
|
||||
//! Usage: `vectorcraft [--control <port> [--automation-read-root <dir>]... [--automation-write-root <dir>]...]
|
||||
//! [--in-window-menus] [files…]`, `vectorcraft --version`, `vectorcraft --help` (or `-h`)
|
||||
//! [--in-window-menus] [--new-instance] [files…]`, `vectorcraft --version`, `vectorcraft --help` (or `-h`)
|
||||
//!
|
||||
//! Files given while VectorCraft already runs open in that window, unless `--new-instance` is given
|
||||
//! (Windows and Linux; `single_instance`).
|
||||
//!
|
||||
//! `--in-window-menus` (or `VECTORCRAFT_IN_WINDOW_MENUS=1`) keeps the menus inside the window on
|
||||
//! macOS instead of the macOS menu bar (`mac_menu`).
|
||||
@@ -16,6 +19,7 @@
|
||||
#![cfg_attr(all(target_os = "windows", not(debug_assertions)), windows_subsystem = "windows")]
|
||||
|
||||
mod automation_args;
|
||||
mod caps_lock;
|
||||
mod clipboard;
|
||||
#[cfg(feature = "wgpu")]
|
||||
mod compositor_gpu;
|
||||
@@ -45,11 +49,15 @@ mod open_documents;
|
||||
mod portal_save;
|
||||
mod prefs_dir;
|
||||
mod printing;
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
mod single_instance;
|
||||
#[cfg(windows)]
|
||||
mod system_fonts;
|
||||
mod tablet;
|
||||
mod window;
|
||||
mod window_frame;
|
||||
#[cfg(windows)]
|
||||
mod windows_cores;
|
||||
mod windows_stylus;
|
||||
mod xwayland;
|
||||
|
||||
@@ -87,6 +95,7 @@ use titlebar::{custom_titlebar, native_options};
|
||||
|
||||
struct App {
|
||||
app: VectorcraftApp,
|
||||
host_caps_lock: caps_lock::HostCapsLock,
|
||||
/// Reported by wgpu when the window's graphics device is lost (a driver reset).
|
||||
graphics_loss: GraphicsLoss,
|
||||
/// The graphics device was lost and the unsaved changes are kept for Data Recovery.
|
||||
@@ -118,7 +127,9 @@ Options:
|
||||
(may be given more than once)
|
||||
--in-window-menus Keep the menus inside the window on macOS
|
||||
(or VECTORCRAFT_IN_WINDOW_MENUS=1)
|
||||
--version Print the version and exit
|
||||
--new-instance Open the files in a new window even when VectorCraft
|
||||
already runs (Windows and Linux)
|
||||
--version Print the version and exit
|
||||
-h, --help Print this help and exit
|
||||
|
||||
Without a window (converting, scripting, the MCP server), use vectorcraft-cli:
|
||||
@@ -128,7 +139,7 @@ Without a window (converting, scripting, the MCP server), use vectorcraft-cli:
|
||||
fn main() -> std::process::ExitCode {
|
||||
// Windows: the update helper is a copy of this program started with `--finish-update`.
|
||||
#[cfg(windows)]
|
||||
if let Some(result) = craft_update::self_update::finish(&craft_update::suite::VECTORCRAFT, &std::env::args_os().skip(1).collect::<Vec<_>>()) {
|
||||
if let Some(result) = craft_update::self_update::finish(&vectorcraft_ui_egui::UPDATE_APP, &std::env::args_os().skip(1).collect::<Vec<_>>()) {
|
||||
std::process::exit(i32::from(result.is_err()));
|
||||
}
|
||||
// Before any thread starts: the pointer must find a cursor theme on Wayland (#2450).
|
||||
@@ -142,6 +153,7 @@ fn main() -> std::process::ExitCode {
|
||||
automation_args::AutomationArgs::new(root_env("VECTORCRAFT_AUTOMATION_READ_ROOT"), root_env("VECTORCRAFT_AUTOMATION_WRITE_ROOT"));
|
||||
let mut files = Vec::new();
|
||||
let mut in_window_menus = std::env::var_os("VECTORCRAFT_IN_WINDOW_MENUS").is_some_and(|v| !v.is_empty() && v != "0");
|
||||
let mut new_instance = false;
|
||||
let mut args = std::env::args().skip(1);
|
||||
while let Some(a) = args.next() {
|
||||
match roots_args.consume(&a, &mut args).and_then(|root| if root { Ok(true) } else { control.consume(&a, &mut args) }) {
|
||||
@@ -154,6 +166,7 @@ fn main() -> std::process::ExitCode {
|
||||
}
|
||||
match a.as_str() {
|
||||
"--in-window-menus" => in_window_menus = true,
|
||||
"--new-instance" => new_instance = true,
|
||||
"--version" => {
|
||||
println!("vectorcraft {}", env!("CARGO_PKG_VERSION"));
|
||||
return std::process::ExitCode::SUCCESS;
|
||||
@@ -183,7 +196,19 @@ fn main() -> std::process::ExitCode {
|
||||
};
|
||||
// Before desktop services, so every start-up warning is recorded (`logging`).
|
||||
let logger = logging::install();
|
||||
// Files opened from the file manager while VectorCraft runs open in its window (#4892). Before
|
||||
// the log file is attached: a launch that hands its files over leaves the running app's log be.
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
let instance = match single_instance::start(control_port.is_some(), new_instance, &files) {
|
||||
single_instance::Start::Forwarded => return std::process::ExitCode::SUCCESS,
|
||||
single_instance::Start::Running(primary) => primary,
|
||||
};
|
||||
#[cfg(target_os = "macos")]
|
||||
let _ = new_instance;
|
||||
vectorcraft_ui_egui::i18n::detect_system_lang_in_background();
|
||||
// Before the first render: the cores the process may run on.
|
||||
#[cfg(windows)]
|
||||
windows_cores::install();
|
||||
// Before the first font scan (the app's start): the fonts font services load (#579).
|
||||
#[cfg(windows)]
|
||||
system_fonts::install();
|
||||
@@ -265,15 +290,15 @@ fn main() -> std::process::ExitCode {
|
||||
let options = native_options(custom_titlebar);
|
||||
// winit picks Wayland whenever `WAYLAND_DISPLAY` is set; Preferences › Performance › Linux
|
||||
// Display Server › X11 opens the window through XWayland instead, where file drops work (#1151),
|
||||
// and X11 (XWayland) When a Pen Is Attached does so when a pen is attached, where the pen draws (#2028).
|
||||
// and the other choices do so when a pen is attached, where the pen draws (#2028, #4967).
|
||||
#[cfg(target_os = "linux")]
|
||||
let options = {
|
||||
let mut options = options;
|
||||
let pref = saved.as_ref().and_then(|ui| ui.engine_prefs.get("linuxDisplayServer")).and_then(serde_json::Value::as_str);
|
||||
if xwayland::wanted(pref, |name| std::env::var_os(name), xwayland::pen_attached) {
|
||||
if xwayland::x11_libraries_found() {
|
||||
if pref == Some("x11WithPen") {
|
||||
log::info!("pen found: opening the window through XWayland (Preferences › Performance › Linux Display Server)");
|
||||
if pref != Some("x11") {
|
||||
log::info!("pen found: opening the window through XWayland, where the pen works (VECTORCRAFT_NATIVE_WAYLAND=1 keeps Wayland)");
|
||||
} else {
|
||||
log::info!("opening the window through XWayland (Preferences › Performance › Linux Display Server)");
|
||||
}
|
||||
@@ -321,14 +346,15 @@ fn main() -> std::process::ExitCode {
|
||||
// losing the device before the first frame (#1194). `WGPU_DX12_COMPILER` picks another.
|
||||
create.instance_descriptor.backend_options.dx12.shader_compiler =
|
||||
eframe::wgpu::Dx12Compiler::from_env().unwrap_or(eframe::wgpu::Dx12Compiler::Fxc);
|
||||
// The window's surface may be as large as the adapter allows, not eframe's fixed
|
||||
// 8192 px: a window stretched over several monitors, or grown by a display scale
|
||||
// change, otherwise fails to configure its surface and the app closes (#1459).
|
||||
// The device asks for no more than the adapter offers, so a GPU short of one of
|
||||
// eframe's limits still gets a device (#2105). The window's surface may be as large as
|
||||
// the adapter allows, not eframe's fixed 8192 px: a window stretched over several
|
||||
// monitors, or grown by a display scale change, otherwise fails to configure its
|
||||
// surface and the app closes (#1459).
|
||||
let device = create.device_descriptor.clone();
|
||||
create.device_descriptor = std::sync::Arc::new(move |adapter: &eframe::wgpu::Adapter| {
|
||||
let mut descriptor = device(adapter);
|
||||
let limit = &mut descriptor.required_limits.max_texture_dimension_2d;
|
||||
*limit = gpu::surface_limit(*limit, adapter.limits().max_texture_dimension_2d);
|
||||
descriptor.required_limits = gpu::device_limits(descriptor.required_limits, &adapter.limits());
|
||||
descriptor
|
||||
});
|
||||
}
|
||||
@@ -452,7 +478,10 @@ fn main() -> std::process::ExitCode {
|
||||
}
|
||||
}
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
let _ = in_window_menus;
|
||||
{
|
||||
let _ = in_window_menus;
|
||||
single_instance::set_ui(&cc.egui_ctx);
|
||||
}
|
||||
app.services.native_input_hook = native_input_hook(&app);
|
||||
file_access::unconfined(|| open_files(&mut app, files));
|
||||
// The first frame is due from now (`gpu::watch_first_frame`).
|
||||
@@ -461,6 +490,7 @@ fn main() -> std::process::ExitCode {
|
||||
// The content and the pointer stay aligned on Windows (#1758).
|
||||
let window_frame = window_frame::service(cc, custom_titlebar);
|
||||
Ok(Box::new(App {
|
||||
host_caps_lock: caps_lock::HostCapsLock::new(winit_window.as_deref()),
|
||||
app,
|
||||
graphics_loss,
|
||||
graphics_lost: false,
|
||||
@@ -473,6 +503,10 @@ fn main() -> std::process::ExitCode {
|
||||
}),
|
||||
)
|
||||
});
|
||||
// The window is gone: later launches start their own app, including the one a graphics
|
||||
// failure starts again below.
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
drop(instance);
|
||||
#[cfg(feature = "wgpu")]
|
||||
let outcome = gpu::finish(outcome, &startup);
|
||||
#[cfg(not(feature = "wgpu"))]
|
||||
|
||||
@@ -84,24 +84,200 @@ pub(crate) fn load_prefs(app: &mut VectorcraftApp, saved: Option<vectorcraft_ui_
|
||||
app.ui = ui.sanitized();
|
||||
}
|
||||
vectorcraft_ui_egui::prefs_dialog::restore(app);
|
||||
// What this window started from: only what changes after it is this window's to save.
|
||||
set_saved_base(prefs_value(app).ok());
|
||||
}
|
||||
|
||||
/// Save what this window changed since it last read or saved the preferences, merged into the
|
||||
/// file as it is now ([`merge_changes`]): another VectorCraft window open at the same time, which
|
||||
/// read the file before this one saved, then keeps this window's changes when it saves or quits
|
||||
/// (#4625). Nothing is written when nothing changed.
|
||||
pub(crate) fn save_prefs(app: &VectorcraftApp) {
|
||||
if !prefs_enabled() || KEEP_UI_PREFS.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
return;
|
||||
}
|
||||
if let Some(p) = prefs_path() {
|
||||
let _ = std::fs::create_dir_all(p.parent().unwrap_or(std::path::Path::new(".")));
|
||||
if let Ok(bytes) = prefs_bytes(app) {
|
||||
// Preferences are best effort: a failed write keeps the previous file.
|
||||
let _ = fileio::write_atomic(&p, &bytes);
|
||||
}
|
||||
let Some(p) = prefs_path() else { return };
|
||||
let Ok(mine) = prefs_value(app) else { return };
|
||||
let base = saved_base();
|
||||
if base.as_ref().is_some_and(|b| same_lasting(b, &mine)) {
|
||||
return;
|
||||
}
|
||||
// The file as another window may have saved it since (none, or unreadable: this one's own).
|
||||
let on_disk = std::fs::read(&p).ok().and_then(|b| serde_json::from_slice::<serde_json::Value>(&b).ok());
|
||||
let merged = match (base.as_ref(), on_disk) {
|
||||
(Some(base), Some(on_disk)) => merge_changes(base, &mine, on_disk),
|
||||
_ => mine.clone(),
|
||||
};
|
||||
let _ = std::fs::create_dir_all(p.parent().unwrap_or(std::path::Path::new(".")));
|
||||
let Ok(bytes) = serde_json::to_vec_pretty(&merged) else { return };
|
||||
// Preferences are best effort: a failed write keeps the previous file, and the changes are
|
||||
// tried again on the next save.
|
||||
if fileio::write_atomic(&p, &bytes).is_ok() {
|
||||
set_saved_base(Some(mine));
|
||||
}
|
||||
}
|
||||
|
||||
/// How often [`save_prefs_soon`] looks for changes to save.
|
||||
const SAVE_EVERY: std::time::Duration = std::time::Duration::from_secs(2);
|
||||
|
||||
/// Save the preferences at most every [`SAVE_EVERY`] while the app runs, when something changed:
|
||||
/// the workspace, panels, toolbars and the other UI settings kept only in the UI state were saved
|
||||
/// only on quitting, so a session ended any other way (the window closed by the system, the
|
||||
/// process ended or crashing) started the next one from the settings before it (#4625).
|
||||
pub(crate) fn save_prefs_soon(app: &VectorcraftApp) {
|
||||
static LAST: std::sync::Mutex<Option<std::time::Instant>> = std::sync::Mutex::new(None);
|
||||
let Ok(mut last) = LAST.lock() else { return };
|
||||
if last.is_some_and(|t| t.elapsed() < SAVE_EVERY) {
|
||||
return;
|
||||
}
|
||||
*last = Some(std::time::Instant::now());
|
||||
drop(last);
|
||||
save_prefs(app);
|
||||
}
|
||||
|
||||
/// The preferences as this window last read or saved them (`None`: not read yet).
|
||||
static SAVED_BASE: std::sync::Mutex<Option<serde_json::Value>> = std::sync::Mutex::new(None);
|
||||
|
||||
fn saved_base() -> Option<serde_json::Value> {
|
||||
SAVED_BASE.lock().ok().and_then(|b| b.clone())
|
||||
}
|
||||
|
||||
fn set_saved_base(value: Option<serde_json::Value>) {
|
||||
if let Ok(mut b) = SAVED_BASE.lock() {
|
||||
*b = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// Fields of the UI state that a start resets ([`vectorcraft_ui_egui::UiState::sanitized`]): a
|
||||
/// change to them alone isn't saved, and they are never merged over another window's.
|
||||
const PASSING: &[&str] = &["status", "dialog", "flyout", "palette_open", "palette_query", "about", "open_panel"];
|
||||
|
||||
/// Do `a` and `b` hold the same preferences, leaving out what a start resets ([`PASSING`])?
|
||||
fn same_lasting(a: &serde_json::Value, b: &serde_json::Value) -> bool {
|
||||
match (a.as_object(), b.as_object()) {
|
||||
(Some(a), Some(b)) => {
|
||||
let keys: std::collections::BTreeSet<&String> = a.keys().chain(b.keys()).filter(|k| !PASSING.contains(&k.as_str())).collect();
|
||||
keys.into_iter().all(|k| a.get(k) == b.get(k))
|
||||
}
|
||||
_ => a == b,
|
||||
}
|
||||
}
|
||||
|
||||
/// `on_disk` with what changed from `base` to `mine` put over it: each top-level field of the UI
|
||||
/// state, and each engine preference (`engine_prefs`) on its own, so two windows that change
|
||||
/// different settings keep both. A field both changed takes this window's value (it saves last).
|
||||
fn merge_changes(base: &serde_json::Value, mine: &serde_json::Value, on_disk: serde_json::Value) -> serde_json::Value {
|
||||
use serde_json::Value;
|
||||
let (Some(base), Some(mine), Value::Object(mut out)) = (base.as_object(), mine.as_object(), on_disk) else {
|
||||
return mine.clone();
|
||||
};
|
||||
let keys: std::collections::BTreeSet<&String> = base.keys().chain(mine.keys()).collect();
|
||||
for key in keys {
|
||||
if PASSING.contains(&key.as_str()) {
|
||||
if let Some(v) = mine.get(key) {
|
||||
out.entry(key.clone()).or_insert_with(|| v.clone());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let (was, now) = (base.get(key), mine.get(key));
|
||||
if was == now {
|
||||
continue;
|
||||
}
|
||||
if key == "engine_prefs"
|
||||
&& let (Some(Value::Object(was)), Some(Value::Object(now)), Some(Value::Object(theirs))) = (was, now, out.get_mut(key))
|
||||
{
|
||||
let names: std::collections::BTreeSet<&String> = was.keys().chain(now.keys()).collect();
|
||||
for name in names {
|
||||
match (was.get(name), now.get(name)) {
|
||||
(a, b) if a == b => {}
|
||||
(_, Some(v)) => {
|
||||
theirs.insert(name.clone(), v.clone());
|
||||
}
|
||||
(_, None) => {
|
||||
theirs.remove(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
match now {
|
||||
Some(v) => {
|
||||
out.insert(key.clone(), v.clone());
|
||||
}
|
||||
None => {
|
||||
out.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Value::Object(out)
|
||||
}
|
||||
|
||||
/// The preferences file's contents: the UI state with the engine preferences.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn prefs_bytes(app: &VectorcraftApp) -> serde_json::Result<Vec<u8>> {
|
||||
serde_json::to_vec_pretty(&prefs_value(app)?)
|
||||
}
|
||||
|
||||
/// [`prefs_bytes`] as JSON.
|
||||
fn prefs_value(app: &VectorcraftApp) -> serde_json::Result<serde_json::Value> {
|
||||
let mut ui = app.ui.clone();
|
||||
ui.engine_prefs = app.session.prefs.to_json();
|
||||
serde_json::to_vec_pretty(&ui)
|
||||
serde_json::to_value(&ui)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::{merge_changes, same_lasting};
|
||||
|
||||
/// Two windows open at once (#4625): the one that saves last keeps the other's changes and
|
||||
/// adds its own, instead of putting back the preferences it started from.
|
||||
#[test]
|
||||
fn a_window_saves_only_what_it_changed_over_another_windows_save() {
|
||||
let base = json!({"workspace": "Essentials", "toolbar": true, "engine_prefs": {"uiAccent": "blue", "uiScaling": 1.0}});
|
||||
// The other window switched the workspace and the accent, and saved.
|
||||
let on_disk = json!({"workspace": "Painting", "toolbar": true, "engine_prefs": {"uiAccent": "violet", "uiScaling": 1.0}});
|
||||
// This one changed only the UI scaling.
|
||||
let mine = json!({"workspace": "Essentials", "toolbar": true, "engine_prefs": {"uiAccent": "blue", "uiScaling": 1.25}});
|
||||
let merged = merge_changes(&base, &mine, on_disk);
|
||||
assert_eq!(merged, json!({"workspace": "Painting", "toolbar": true, "engine_prefs": {"uiAccent": "violet", "uiScaling": 1.25}}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_setting_both_windows_changed_takes_the_last_saved() {
|
||||
let base = json!({"workspace": "Essentials", "engine_prefs": {"uiAccent": "blue"}});
|
||||
let on_disk = json!({"workspace": "Painting", "engine_prefs": {"uiAccent": "violet"}});
|
||||
let mine = json!({"workspace": "Typography", "engine_prefs": {"uiAccent": "green"}});
|
||||
assert_eq!(merge_changes(&base, &mine, on_disk), json!({"workspace": "Typography", "engine_prefs": {"uiAccent": "green"}}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fields_added_or_removed_by_this_window_are_saved() {
|
||||
let base = json!({"docking": {"root": 1}, "engine_prefs": {"old": 1}});
|
||||
let on_disk = json!({"docking": {"root": 1}, "recent_files": ["a.svg"], "engine_prefs": {"old": 1}});
|
||||
let mine = json!({"dock_width": 300.0, "engine_prefs": {"new": 2}});
|
||||
assert_eq!(merge_changes(&base, &mine, on_disk), json!({"dock_width": 300.0, "recent_files": ["a.svg"], "engine_prefs": {"new": 2}}));
|
||||
}
|
||||
|
||||
/// The status line and open dialogs reset at start: they don't make a save, and they don't
|
||||
/// replace another window's.
|
||||
#[test]
|
||||
fn what_a_start_resets_is_left_out() {
|
||||
let base = json!({"status": "", "dialog": null, "workspace": "Essentials"});
|
||||
let mine = json!({"status": "Saved", "dialog": {"kind": "preferences"}, "workspace": "Essentials"});
|
||||
assert!(same_lasting(&base, &mine));
|
||||
let on_disk = json!({"status": "Opened", "dialog": null, "workspace": "Painting"});
|
||||
let mine = json!({"status": "Saved", "dialog": {"kind": "preferences"}, "workspace": "Essentials", "toolbar": false});
|
||||
let merged = merge_changes(&base, &mine, on_disk);
|
||||
assert_eq!(merged, json!({"status": "Opened", "dialog": null, "workspace": "Painting", "toolbar": false}));
|
||||
assert!(!same_lasting(&base, &mine));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_that_is_not_an_object_is_replaced() {
|
||||
let base = json!({"workspace": "Essentials"});
|
||||
let mine = json!({"workspace": "Painting"});
|
||||
assert_eq!(merge_changes(&base, &mine, json!([1, 2])), mine);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
//! One app per user (#4892): a file opened from the file manager (double-click, Open With, a drop
|
||||
//! on the executable) while VectorCraft runs opens as a new document tab in the running app, which
|
||||
//! comes to the front, instead of in a second app.
|
||||
//!
|
||||
//! The running app (the primary) listens on a loopback port and records the port and two random
|
||||
//! tokens in `instance.json` next to the preferences (owner-only on Unix; on Windows the settings
|
||||
//! folder is the user's own). A later launch with files connects; the primary greets it with
|
||||
//! `hello`, which only a process that could read the file (or talk to the primary) knows; only then
|
||||
//! does the launch send `token` and the absolute paths of its files, one JSON line. The primary
|
||||
//! checks the token, keeps the paths that are existing files, acknowledges and opens them; the
|
||||
//! launch exits. Anything else (no file, no listener, a wrong greeting, no answer) starts the launch
|
||||
//! as its own app, which replaces a stale file. Nothing received is ever run.
|
||||
//!
|
||||
//! Runs that start many apps side by side (`--control`, `VECTORCRAFT_NO_PREFS`) neither forward
|
||||
//! nor listen, and `--new-instance` opens a launch's files in an app of its own. macOS hands files
|
||||
//! opened from Finder to the running app itself (`open_documents`), so this is off there.
|
||||
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::hash::{BuildHasher as _, RandomState};
|
||||
use std::io::{self, BufRead as _, BufReader, Read as _, Write as _};
|
||||
use std::net::{Ipv4Addr, TcpListener, TcpStream};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Mutex, OnceLock, PoisonError};
|
||||
use std::time::Duration;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::prefs::{prefs_enabled, prefs_path};
|
||||
|
||||
/// The endpoint file, next to the preferences.
|
||||
const FILE_NAME: &str = "instance.json";
|
||||
/// The longest message either side reads, and the longest endpoint file.
|
||||
const MAX_MESSAGE: u64 = 64 * 1024;
|
||||
/// The most files one launch hands over.
|
||||
const MAX_FILES: usize = 256;
|
||||
/// A live primary's kernel accepts at once, even while its window is busy; a stale port on Windows
|
||||
/// would otherwise retry for about two seconds.
|
||||
const CONNECT_TIMEOUT: Duration = Duration::from_millis(500);
|
||||
/// How long the primary waits on one launch; it serves launches one at a time.
|
||||
const SERVER_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
/// How long a launch waits on the primary: longer than a launch served before it can take.
|
||||
const CLIENT_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
/// Paths handed over by later launches that the UI hasn't opened yet.
|
||||
static PENDING: Mutex<Vec<String>> = Mutex::new(Vec::new());
|
||||
/// The UI to wake when files arrive (unset until the window exists; the first frame opens them).
|
||||
static UI: OnceLock<egui::Context> = OnceLock::new();
|
||||
|
||||
/// What this launch does about an app already running.
|
||||
pub enum Start {
|
||||
/// The running app took the files: this launch exits.
|
||||
Forwarded,
|
||||
/// This launch runs; `Some` while it is the app later launches hand their files to.
|
||||
Running(Option<Primary>),
|
||||
}
|
||||
|
||||
/// Hand `files` to the app already running, or become the app later launches hand theirs to.
|
||||
/// `automation`: started with `--control`.
|
||||
pub fn start(automation: bool, new_instance: bool, files: &[String]) -> Start {
|
||||
if automation || !prefs_enabled() {
|
||||
return Start::Running(None);
|
||||
}
|
||||
let Some(path) = prefs_path().and_then(|p| Some(p.parent()?.join(FILE_NAME))) else { return Start::Running(None) };
|
||||
// With nothing to hand over, the same exchange only asks whether an app is running.
|
||||
let send: &[String] = if new_instance { &[] } else { files };
|
||||
match forward(&path, send) {
|
||||
Ok(()) if send.is_empty() => Start::Running(None),
|
||||
Ok(()) => Start::Forwarded,
|
||||
Err(e) => {
|
||||
log::debug!("no running VectorCraft to hand files to ({e})");
|
||||
Start::Running(listen(&path, queue))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Wake `ctx` when files arrive from later launches.
|
||||
pub fn set_ui(ctx: &egui::Context) {
|
||||
// Set once; a second call would hand over the same context.
|
||||
let _ = UI.set(ctx.clone());
|
||||
}
|
||||
|
||||
/// The paths later launches handed over since the last call.
|
||||
pub fn take() -> Vec<String> {
|
||||
std::mem::take(&mut *PENDING.lock().unwrap_or_else(PoisonError::into_inner))
|
||||
}
|
||||
|
||||
fn queue(files: Vec<String>) {
|
||||
PENDING.lock().unwrap_or_else(PoisonError::into_inner).extend(files);
|
||||
if let Some(ctx) = UI.get() {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
/// This app is the one recorded in the endpoint file; dropping it removes the file, unless a later
|
||||
/// app has replaced it since.
|
||||
pub struct Primary {
|
||||
path: PathBuf,
|
||||
token: String,
|
||||
}
|
||||
|
||||
impl Drop for Primary {
|
||||
fn drop(&mut self) {
|
||||
if read_endpoint(&self.path).is_some_and(|e| e.token == self.token) {
|
||||
// Best effort: a file left behind is stale, and the next launch replaces it.
|
||||
let _ = fs::remove_file(&self.path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Endpoint {
|
||||
port: u16,
|
||||
token: String,
|
||||
hello: String,
|
||||
}
|
||||
|
||||
fn invalid(why: &str) -> io::Error {
|
||||
io::Error::new(io::ErrorKind::InvalidData, why)
|
||||
}
|
||||
|
||||
fn read_endpoint(path: &Path) -> Option<Endpoint> {
|
||||
let mut text = String::new();
|
||||
fs::File::open(path).ok()?.take(MAX_MESSAGE).read_to_string(&mut text).ok()?;
|
||||
let v: Value = serde_json::from_str(&text).ok()?;
|
||||
let port = u16::try_from(v.get("port")?.as_u64()?).ok().filter(|p| *p != 0)?;
|
||||
Some(Endpoint { port, token: v.get("token")?.as_str()?.to_owned(), hello: v.get("hello")?.as_str()?.to_owned() })
|
||||
}
|
||||
|
||||
/// Hand `files` (made absolute against this launch's folder) to the app recorded at `path`; `Ok`
|
||||
/// once it acknowledged them.
|
||||
fn forward(path: &Path, files: &[String]) -> io::Result<()> {
|
||||
let endpoint = read_endpoint(path).ok_or_else(|| invalid("no endpoint file"))?;
|
||||
let files = files.iter().map(|f| std::path::absolute(f).map(|p| p.to_string_lossy().into_owned())).collect::<io::Result<Vec<_>>>()?;
|
||||
let stream = TcpStream::connect_timeout(&(Ipv4Addr::LOCALHOST, endpoint.port).into(), CONNECT_TIMEOUT)?;
|
||||
stream.set_read_timeout(Some(CLIENT_TIMEOUT))?;
|
||||
stream.set_write_timeout(Some(CLIENT_TIMEOUT))?;
|
||||
// The token and the paths go only to a listener that knows the greeting.
|
||||
if !same(read_message(&stream)?.get("hello"), &endpoint.hello) {
|
||||
return Err(invalid("the listener isn't VectorCraft"));
|
||||
}
|
||||
write_message(&stream, &json!({ "token": endpoint.token, "files": files }))?;
|
||||
match read_message(&stream)?.get("ok") {
|
||||
Some(Value::Bool(true)) => Ok(()),
|
||||
_ => Err(invalid("refused")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Listen for later launches and record this app at `path`; each launch's files go to `deliver`.
|
||||
/// `None` when that fails (logged): the app then runs on its own, as before.
|
||||
fn listen(path: &Path, deliver: impl Fn(Vec<String>) + Send + 'static) -> Option<Primary> {
|
||||
try_listen(path, deliver).inspect_err(|e| log::warn!("files opened while VectorCraft runs will open in another window: {e}")).ok()
|
||||
}
|
||||
|
||||
fn try_listen(path: &Path, deliver: impl Fn(Vec<String>) + Send + 'static) -> io::Result<Primary> {
|
||||
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0))?;
|
||||
let port = listener.local_addr()?.port();
|
||||
let (token, hello) = random_tokens();
|
||||
write_endpoint(path, &json!({ "port": port, "token": token, "hello": hello }))?;
|
||||
// From here on a failure removes the file again.
|
||||
let primary = Primary { path: path.to_path_buf(), token: token.clone() };
|
||||
std::thread::Builder::new().name("open-files-listener".into()).spawn(move || {
|
||||
for stream in listener.incoming() {
|
||||
match stream.and_then(|s| receive(&s, &hello, &token)) {
|
||||
Ok(files) if files.is_empty() => {}
|
||||
Ok(files) => deliver(files),
|
||||
Err(e) => {
|
||||
log::warn!("refused files from another launch: {e}");
|
||||
// An accept that keeps failing (out of handles) doesn't spin.
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
}
|
||||
})?;
|
||||
Ok(primary)
|
||||
}
|
||||
|
||||
/// Serve one launch: greet it, check its token, keep the paths that are existing files.
|
||||
fn receive(stream: &TcpStream, hello: &str, token: &str) -> io::Result<Vec<String>> {
|
||||
stream.set_read_timeout(Some(SERVER_TIMEOUT))?;
|
||||
stream.set_write_timeout(Some(SERVER_TIMEOUT))?;
|
||||
write_message(stream, &json!({ "hello": hello }))?;
|
||||
let message = read_message(stream)?;
|
||||
if !same(message.get("token"), token) {
|
||||
return Err(invalid("wrong token"));
|
||||
}
|
||||
let files = message.get("files").and_then(Value::as_array).ok_or_else(|| invalid("no file list"))?;
|
||||
if files.len() > MAX_FILES {
|
||||
return Err(invalid("too many files"));
|
||||
}
|
||||
let files = files
|
||||
.iter()
|
||||
.filter_map(Value::as_str)
|
||||
.filter(|f| {
|
||||
let p = Path::new(f);
|
||||
let ok = p.is_absolute() && p.is_file();
|
||||
if !ok {
|
||||
log::warn!("another launch named {f}, which isn't a file");
|
||||
}
|
||||
ok
|
||||
})
|
||||
.map(str::to_owned)
|
||||
.collect();
|
||||
write_message(stream, &json!({ "ok": true }))?;
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
/// One JSON line of at most [`MAX_MESSAGE`] bytes. Each side sends one line and waits for the
|
||||
/// other's, so nothing read past it is lost.
|
||||
fn read_message(stream: &TcpStream) -> io::Result<Value> {
|
||||
let mut line = Vec::new();
|
||||
BufReader::new(stream.take(MAX_MESSAGE)).read_until(b'\n', &mut line)?;
|
||||
if line.last() != Some(&b'\n') {
|
||||
return Err(invalid("message too long or cut short"));
|
||||
}
|
||||
serde_json::from_slice(&line).map_err(|e| invalid(&e.to_string()))
|
||||
}
|
||||
|
||||
fn write_message(mut stream: &TcpStream, message: &Value) -> io::Result<()> {
|
||||
let mut line = serde_json::to_vec(message).map_err(io::Error::other)?;
|
||||
line.push(b'\n');
|
||||
stream.write_all(&line)
|
||||
}
|
||||
|
||||
/// `given` is the string `expected`, compared in constant time.
|
||||
fn same(given: Option<&Value>, expected: &str) -> bool {
|
||||
given
|
||||
.and_then(Value::as_str)
|
||||
.is_some_and(|g| g.len() == expected.len() && g.bytes().zip(expected.bytes()).fold(0, |acc, (a, b)| acc | (a ^ b)) == 0)
|
||||
}
|
||||
|
||||
/// Two unrelated 128-bit hex tokens. `RandomState` is keyed from the system's random numbers, and
|
||||
/// its keyed hash of distinct values gives unpredictable, independent output (no crate needed).
|
||||
fn random_tokens() -> (String, String) {
|
||||
let keyed = RandomState::new();
|
||||
let token = |a: u8, b: u8| format!("{:016x}{:016x}", keyed.hash_one(a), keyed.hash_one(b));
|
||||
(token(1, 2), token(3, 4))
|
||||
}
|
||||
|
||||
/// Replace `path` with `endpoint` at once (a temporary file renamed over it), readable only by the
|
||||
/// user on Unix.
|
||||
fn write_endpoint(path: &Path, endpoint: &Value) -> io::Result<()> {
|
||||
if let Some(dir) = path.parent() {
|
||||
fs::create_dir_all(dir)?;
|
||||
}
|
||||
let tmp = path.with_extension(format!("{}.tmp", std::process::id()));
|
||||
// A leftover of an earlier app with the same process id; `create_new` sets the mode below.
|
||||
let _ = fs::remove_file(&tmp);
|
||||
let mut options = OpenOptions::new();
|
||||
options.write(true).create_new(true);
|
||||
#[cfg(unix)]
|
||||
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
|
||||
let bytes = serde_json::to_vec(endpoint).map_err(io::Error::other)?;
|
||||
let written = options.open(&tmp).and_then(|mut f| f.write_all(&bytes)).and_then(|()| fs::rename(&tmp, path));
|
||||
if written.is_err() {
|
||||
// Best effort: the temporary file is all there is to clean up.
|
||||
let _ = fs::remove_file(&tmp);
|
||||
}
|
||||
written
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::mpsc;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A fresh folder for one test, with an existing file in it.
|
||||
fn folder(name: &str) -> (PathBuf, PathBuf, String) {
|
||||
let dir = std::env::temp_dir().join(format!("vc-single-instance-{}-{name}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let file = dir.join("a b.svg");
|
||||
fs::write(&file, "<svg/>").unwrap();
|
||||
let file = file.to_string_lossy().into_owned();
|
||||
(dir.join(FILE_NAME), dir, file)
|
||||
}
|
||||
|
||||
fn primary(path: &Path) -> (Primary, mpsc::Receiver<Vec<String>>) {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let primary = listen(path, move |files| tx.send(files).unwrap()).unwrap();
|
||||
(primary, rx)
|
||||
}
|
||||
|
||||
/// Connects as a launch would and sends `message` after the greeting; what came back.
|
||||
fn send_raw(path: &Path, message: &[u8]) -> io::Result<Value> {
|
||||
let endpoint = read_endpoint(path).unwrap();
|
||||
let stream = TcpStream::connect((Ipv4Addr::LOCALHOST, endpoint.port)).unwrap();
|
||||
stream.set_read_timeout(Some(CLIENT_TIMEOUT)).unwrap();
|
||||
assert!(same(read_message(&stream).unwrap().get("hello"), &endpoint.hello));
|
||||
(&stream).write_all(message)?;
|
||||
read_message(&stream)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_launch_hands_its_files_to_the_running_app() {
|
||||
let (path, dir, file) = folder("forward");
|
||||
let (primary, rx) = primary(&path);
|
||||
let missing = dir.join("missing.svg").to_string_lossy().into_owned();
|
||||
forward(&path, &[file.clone(), missing]).unwrap();
|
||||
assert_eq!(rx.recv_timeout(CLIENT_TIMEOUT).unwrap(), std::slice::from_ref(&file), "the missing file is left out");
|
||||
// An empty list only asks whether the app runs: nothing is delivered.
|
||||
forward(&path, &[]).unwrap();
|
||||
forward(&path, std::slice::from_ref(&file)).unwrap();
|
||||
assert_eq!(rx.recv_timeout(CLIENT_TIMEOUT).unwrap(), [file]);
|
||||
drop(primary);
|
||||
assert!(!path.exists(), "the endpoint file goes with the app");
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wrong_token_garbage_and_oversized_messages_are_refused() {
|
||||
let (path, dir, file) = folder("refused");
|
||||
let (_primary, rx) = primary(&path);
|
||||
let token = read_endpoint(&path).unwrap().token;
|
||||
let wrong = format!("{}\n", json!({ "token": "0".repeat(token.len()), "files": [file] }));
|
||||
assert!(send_raw(&path, wrong.as_bytes()).is_err());
|
||||
assert!(send_raw(&path, b"not json\n").is_err());
|
||||
assert!(send_raw(&path, &vec![b'a'; MAX_MESSAGE as usize + 10]).is_err());
|
||||
let many = format!("{}\n", json!({ "token": token, "files": vec![file.clone(); MAX_FILES + 1] }));
|
||||
assert!(send_raw(&path, many.as_bytes()).is_err());
|
||||
let relative = format!("{}\n", json!({ "token": token, "files": ["a b.svg", 7] }));
|
||||
assert_eq!(send_raw(&path, relative.as_bytes()).unwrap(), json!({ "ok": true }));
|
||||
assert!(rx.try_recv().is_err(), "nothing was delivered");
|
||||
// The listener still serves a launch with the right token.
|
||||
forward(&path, std::slice::from_ref(&file)).unwrap();
|
||||
assert_eq!(rx.recv_timeout(CLIENT_TIMEOUT).unwrap(), [file]);
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stale_endpoint_file_is_ignored_and_replaced() {
|
||||
let (path, dir, file) = folder("stale");
|
||||
// No listener on the recorded port any more.
|
||||
let port = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap().local_addr().unwrap().port();
|
||||
write_endpoint(&path, &json!({ "port": port, "token": "t", "hello": "h" })).unwrap();
|
||||
assert!(forward(&path, std::slice::from_ref(&file)).is_err());
|
||||
// A listener that doesn't know the greeting gets neither the token nor the paths.
|
||||
let impostor = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
|
||||
let port = impostor.local_addr().unwrap().port();
|
||||
write_endpoint(&path, &json!({ "port": port, "token": "t", "hello": "h" })).unwrap();
|
||||
let heard = std::thread::spawn(move || {
|
||||
let (stream, _) = impostor.accept().unwrap();
|
||||
write_message(&stream, &json!({ "hello": "x" })).unwrap();
|
||||
stream.set_read_timeout(Some(CLIENT_TIMEOUT)).unwrap();
|
||||
read_message(&stream).ok()
|
||||
});
|
||||
assert!(forward(&path, std::slice::from_ref(&file)).is_err());
|
||||
assert!(heard.join().unwrap().is_none());
|
||||
// Garbage in the file is no endpoint either.
|
||||
fs::write(&path, b"\xff not json").unwrap();
|
||||
assert!(forward(&path, std::slice::from_ref(&file)).is_err());
|
||||
// A new primary replaces the stale file.
|
||||
let (_primary, rx) = primary(&path);
|
||||
forward(&path, std::slice::from_ref(&file)).unwrap();
|
||||
assert_eq!(rx.recv_timeout(CLIENT_TIMEOUT).unwrap(), [file]);
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_replaced_endpoint_file_stays_when_the_earlier_app_ends() {
|
||||
let (path, dir, _) = folder("replaced");
|
||||
let (first, _rx1) = primary(&path);
|
||||
let (_second, _rx2) = primary(&path);
|
||||
let second_token = read_endpoint(&path).unwrap().token;
|
||||
drop(first);
|
||||
assert_eq!(read_endpoint(&path).map(|e| e.token), Some(second_token));
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tokens_are_random_and_distinct() {
|
||||
let (a, b) = random_tokens();
|
||||
let (c, _) = random_tokens();
|
||||
assert_eq!(a.len(), 32);
|
||||
assert!(a != b && a != c);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn the_endpoint_file_is_the_users_alone() {
|
||||
use std::os::unix::fs::PermissionsExt as _;
|
||||
let (path, dir, _) = folder("mode");
|
||||
let _primary = primary(&path);
|
||||
assert_eq!(fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
//! Windows: the cores the process may run on, from its affinity mask, for the renderer's worker
|
||||
//! count (`vectorcraft_render::set_usable_cores`). `available_parallelism` counts every processor
|
||||
//! in the machine, also when the process may run on fewer (started with `start /affinity`, or
|
||||
//! pinned by a launcher). The desktop app and `vectorcraft-cli` (exports, MCP) both install it; the
|
||||
//! CLI shares this file.
|
||||
//!
|
||||
//! `GetProcessAffinityMask` is a C API with no safe binding, hence the scoped `unsafe_code`
|
||||
//! allowance.
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use windows::Win32::System::Threading::{GetCurrentProcess, GetProcessAffinityMask};
|
||||
|
||||
/// Tells the renderer how many cores the process may run on.
|
||||
pub fn install() {
|
||||
vectorcraft_render::set_usable_cores(usable_cores());
|
||||
}
|
||||
|
||||
/// The number of cores in the process's affinity mask, or `None` when Windows reports none (a
|
||||
/// process with threads in more than one processor group).
|
||||
fn usable_cores() -> Option<usize> {
|
||||
let (mut process, mut system) = (0usize, 0usize);
|
||||
// SAFETY: `GetCurrentProcess` returns a pseudo-handle that needs no closing, and both pointers
|
||||
// point at live `usize`s for the length of the call.
|
||||
unsafe { GetProcessAffinityMask(GetCurrentProcess(), &mut process, &mut system) }.ok()?;
|
||||
(process != 0).then(|| process.count_ones() as usize)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
/// The mask Windows reports for this process gives at least one core and no more than the
|
||||
/// machine has. (A child process started with `start /affinity` would test a one-core mask
|
||||
/// end to end, but a process spawned while other tests hold sockets inherits them and resets
|
||||
/// their connections; `automatic_threads` covers the one-core count.)
|
||||
#[test]
|
||||
fn the_affinity_mask_gives_the_cores_the_process_may_use() {
|
||||
let cores = super::usable_cores().unwrap();
|
||||
let all = std::thread::available_parallelism().unwrap().get();
|
||||
assert!((1..=all).contains(&cores), "{cores} of {all}");
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
//! Linux: Preferences › Performance › Linux Display Server › X11 opens the window through XWayland
|
||||
//! on a Wayland session, where dropped files arrive (winit 0.30 has no Wayland drag and drop,
|
||||
//! #1151). X11 (XWayland) When a Pen Is Attached does so only when a pen is attached as the app
|
||||
//! starts: winit 0.30 doesn't bind the Wayland tablet protocol, and XWayland hands the pen to X11
|
||||
//! windows as a pointer (#2028). Only when XWayland can run the window: an X display is set and
|
||||
//! #1151). Automatic and X11 (XWayland) When a Pen Is Attached do so only when a pen is attached
|
||||
//! as the app starts, as PhotoCraft does: winit 0.30 doesn't bind the Wayland tablet protocol, so
|
||||
//! on native Wayland the pen moves only the compositor's cursor, and XWayland hands the pen to X11
|
||||
//! windows as a pointer (#2028, #4967). `VECTORCRAFT_NATIVE_WAYLAND=1` keeps a pen's window on
|
||||
//! Wayland. Only when XWayland can run the window: an X display is set and
|
||||
//! the X11 libraries winit loads at runtime are installed. Otherwise the window opens on Wayland,
|
||||
//! so a missing library never keeps the app from starting.
|
||||
//!
|
||||
@@ -21,16 +23,17 @@ const X11_LIBS: &[&str] =
|
||||
&["libX11.so.6", "libX11-xcb.so.1", "libxcb.so.1", "libXcursor.so.1", "libXi.so.6", "libxkbcommon.so.0", "libxkbcommon-x11.so.0"];
|
||||
|
||||
/// Whether the saved `linuxDisplayServer` and the environment ask for XWayland: `x11` chosen, or
|
||||
/// `x11WithPen` chosen and `pen` finding a pen; a Wayland session (winit picks Wayland whenever
|
||||
/// `WAYLAND_DISPLAY` or `WAYLAND_SOCKET` is set); and an X display to open. `pen` runs only for
|
||||
/// `x11WithPen`, after the session checks.
|
||||
/// any other choice (`automatic`, `x11WithPen`, none saved) with `pen` finding a pen and
|
||||
/// `VECTORCRAFT_NATIVE_WAYLAND` unset (or `0`); a Wayland session (winit picks Wayland whenever
|
||||
/// `WAYLAND_DISPLAY` or `WAYLAND_SOCKET` is set); and an X display to open. `pen` runs only after
|
||||
/// the other checks.
|
||||
pub fn wanted(pref: Option<&str>, var: impl Fn(&str) -> Option<OsString>, pen: impl FnOnce() -> bool) -> bool {
|
||||
let set = |name: &str| var(name).is_some_and(|v| !v.is_empty());
|
||||
let session = (set("WAYLAND_DISPLAY") || set("WAYLAND_SOCKET")) && set("DISPLAY");
|
||||
let native_wayland = var("VECTORCRAFT_NATIVE_WAYLAND").is_some_and(|v| !v.is_empty() && v != "0");
|
||||
match pref {
|
||||
Some("x11") => session,
|
||||
Some("x11WithPen") => session && pen(),
|
||||
_ => false,
|
||||
_ => session && !native_wayland && pen(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,27 +152,35 @@ mod tests {
|
||||
let wayland = env(&[("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", ":0")]);
|
||||
assert!(wanted(Some("x11"), &wayland, no_probe));
|
||||
assert!(wanted(Some("x11"), env(&[("WAYLAND_SOCKET", "3"), ("DISPLAY", ":0")]), no_probe));
|
||||
assert!(!wanted(Some("automatic"), &wayland, no_probe));
|
||||
assert!(!wanted(None, &wayland, no_probe));
|
||||
assert!(!wanted(Some("wayland"), &wayland, no_probe));
|
||||
assert!(!wanted(Some("automatic"), &wayland, || false));
|
||||
assert!(!wanted(None, &wayland, || false));
|
||||
// An X11 session already opens on X11; without an X display there is no XWayland to use.
|
||||
assert!(!wanted(Some("x11"), env(&[("DISPLAY", ":0")]), no_probe));
|
||||
assert!(!wanted(Some("x11"), env(&[("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", "")]), no_probe));
|
||||
assert!(!wanted(Some("x11"), env(&[("WAYLAND_DISPLAY", "wayland-0")]), no_probe));
|
||||
}
|
||||
|
||||
/// X11 (XWayland) When a Pen Is Attached (#2028): XWayland on a Wayland session with an X
|
||||
/// display when a pen is found, the session's own display server otherwise. The pen check
|
||||
/// runs only on such a session.
|
||||
/// Automatic and X11 (XWayland) When a Pen Is Attached (#2028, #4967): XWayland on a Wayland
|
||||
/// session with an X display when a pen is found, where the pen reaches the window, the
|
||||
/// session's own display server otherwise. The pen check runs only on such a session, and
|
||||
/// `VECTORCRAFT_NATIVE_WAYLAND` keeps Wayland.
|
||||
#[test]
|
||||
fn the_pen_choice_opens_xwayland_only_with_a_pen() {
|
||||
fn a_pen_opens_xwayland_unless_native_wayland_is_asked_for() {
|
||||
let wayland = env(&[("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", ":0")]);
|
||||
assert!(wanted(Some("x11WithPen"), &wayland, || true));
|
||||
assert!(wanted(Some("x11WithPen"), env(&[("WAYLAND_SOCKET", "3"), ("DISPLAY", ":0")]), || true));
|
||||
assert!(!wanted(Some("x11WithPen"), &wayland, || false));
|
||||
assert!(!wanted(Some("x11WithPen"), env(&[("DISPLAY", ":0")]), no_probe));
|
||||
assert!(!wanted(Some("x11WithPen"), env(&[("WAYLAND_DISPLAY", "wayland-0")]), no_probe));
|
||||
assert!(!wanted(Some("x11WithPen"), env(&[("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", "")]), no_probe));
|
||||
let native = env(&[("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", ":0"), ("VECTORCRAFT_NATIVE_WAYLAND", "1")]);
|
||||
let not_native = env(&[("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", ":0"), ("VECTORCRAFT_NATIVE_WAYLAND", "0")]);
|
||||
for pref in [Some("automatic"), None, Some("x11WithPen")] {
|
||||
assert!(wanted(pref, &wayland, || true), "{pref:?}");
|
||||
assert!(wanted(pref, env(&[("WAYLAND_SOCKET", "3"), ("DISPLAY", ":0")]), || true), "{pref:?}");
|
||||
assert!(!wanted(pref, &wayland, || false), "{pref:?}");
|
||||
assert!(!wanted(pref, env(&[("DISPLAY", ":0")]), no_probe), "{pref:?}");
|
||||
assert!(!wanted(pref, env(&[("WAYLAND_DISPLAY", "wayland-0")]), no_probe), "{pref:?}");
|
||||
assert!(!wanted(pref, env(&[("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", "")]), no_probe), "{pref:?}");
|
||||
assert!(!wanted(pref, &native, no_probe), "{pref:?}");
|
||||
assert!(wanted(pref, ¬_native, || true), "{pref:?}");
|
||||
}
|
||||
// The X11 choice opens XWayland with or without a pen; the opt-out is for the pen only.
|
||||
assert!(wanted(Some("x11"), &native, no_probe));
|
||||
}
|
||||
|
||||
/// `/proc/bus/input/devices` of a laptop with a Wacom Intuos Pro attached (64-bit longs: the
|
||||
|
||||
@@ -16,9 +16,7 @@ head-and-shoulders portrait on a full-bleed field, no frame or roundel, with the
|
||||
**Tile:** `viewBox="0 0 512 512"`, a rounded square with `rx=112`, clipped. The macOS files add Apple's
|
||||
transparent margin (tile = 824/1024 of the canvas); Windows and Linux use the tile edge to edge.
|
||||
|
||||
**Provenance:** the owner's original ArtCraft drawing (2880 px, keyed to the palette), vectorised with
|
||||
craftrules `assets/logo-options/_tools/vectorize_tile.py`. The drawing is kept in craftrules at
|
||||
`craftrules/assets/app-icons/vectorcraft/source.png`, not here. `vectorcraft.svg` is the canonical
|
||||
**Provenance:** the owner's original ArtCraft drawing, vectorised. `vectorcraft.svg` is the canonical
|
||||
artwork. Licence: see `LICENSE.txt`.
|
||||
|
||||
## Files
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
# Contributor names for the About window (craftrules standards/contributors.md).
|
||||
# Contributor names for the About window.
|
||||
#
|
||||
# Everyone who contributes is credited by GitHub username automatically. Anything more is up to
|
||||
# you: add YOUR OWN entry below, in your own PR (commit it yourself). Never add someone else; a
|
||||
# maintainer may add a person only when that person asked, recording where in consent_source.
|
||||
#
|
||||
# Easiest: python3 ../../craftrules/scripts/contributors.py --add-me . \
|
||||
# [--real-name "Ada Lovelace"] [--display-name "Ada"] [--sync-github-name]
|
||||
#
|
||||
# Or copy this block, replacing your-github-username (every field is optional):
|
||||
# Copy this block, replacing your-github-username (every field is optional):
|
||||
#
|
||||
# [people.your-github-username]
|
||||
# real_name = "Your Name" # shown when the viewer picks "Real name"
|
||||
@@ -17,6 +14,11 @@
|
||||
# To remove your names, delete your entry (or set fields empty) in a PR. The next
|
||||
# contributors.py run, and the next build, drop them.
|
||||
|
||||
[people.el-j]
|
||||
real_name = "Fabian Althaus"
|
||||
display_name = "faff (el-j)"
|
||||
sync_github_name = false
|
||||
|
||||
[people.lollinoo]
|
||||
sync_github_name = true
|
||||
|
||||
@@ -172,3 +174,18 @@ sync_github_name = false
|
||||
|
||||
[people.krosk93]
|
||||
sync_github_name = true
|
||||
|
||||
[people.ashokram-si]
|
||||
real_name = "Ashok Ram"
|
||||
|
||||
[people.dylanmtaylor]
|
||||
sync_github_name = true
|
||||
|
||||
[people.marshiyar]
|
||||
display_name = "Arshiya"
|
||||
real_name = "Arshiya Rahgozar"
|
||||
sync_github_name = false
|
||||
|
||||
[people.WadahBadawi]
|
||||
display_name = "Wadah Badawi"
|
||||
sync_github_name = true
|
||||
@@ -17,12 +17,13 @@ Affinity files.
|
||||
| Curves (`PCrv`): cubic subpaths, closed flags, live corners (`CnrD`) | paths (multi-subpath curves fill even-odd) |
|
||||
| Rectangles with corner radii (relative or absolute, locked or per corner) and round or straight corners, ellipses, polygons (smooth too), stars, square stars, pies, triangles, trapezoids | paths |
|
||||
| Compound shapes (`Comp`, add and subtract) | one even-odd path |
|
||||
| A shape or curve with children clips them | clip group whose clipping path keeps its fill (behind) and stroke (over) |
|
||||
| A shape, curve or text with children clips them | clip group whose clipping path keeps its fill (behind) and stroke (over) |
|
||||
| Vector masks (`AdCh` curves and shapes) | clip group |
|
||||
| Pixel masks (`MRst`, attached or as a layer in a group) | opacity mask |
|
||||
| Solid fills: RGBA, HSLA, CMYK, grey, Lab (D50) | fill appearance (alpha as fill opacity) |
|
||||
| Linear, elliptical and radial gradients with Affinity's midpoint bias | gradients (the bias as the midpoint where the blend is half way) |
|
||||
| Strokes: weight, scale with object, dash pattern and phase, alignment, miter limit, behind the fill; a stroke whose style is None (every shape stores one, black and 1 px) is left out | stroke appearance |
|
||||
| A stroke's pressure curve (`LDeP`: position along the path against width) | the stroke's width profile, the curve sampled at 2 to 8 points per segment (256 segments at most), widths up to the full weight |
|
||||
| Fill layers (`FRst`) | filled rectangles |
|
||||
| Artistic and frame text: Unicode scalar-indexed runs, stored fallback font, family/weight/style, boolean OpenType features, all caps, underline, strikethrough, size, tracking, horizontal scale, fixed leading, colour, outlined characters, first baseline; each paragraph's alignment, indents, space before and after, fixed and automatic leading; frame text sized by its text scale (`FTxS`), not its frame's transform | point type and area type, one paragraph style per paragraph; artistic text whose paragraphs align differently keeps them aligned within its longest line (by indents), as Affinity draws it; a frame's first line hangs from its capitals' tops, and a frame whose last lines' descenders hang out of it is made just tall enough to show them; line breaks inside a paragraph become paragraphs without spacing between them; table cells place their text as Affinity does (centred from the capitals' tops to the last baseline, reaching out of a cell too short for it) |
|
||||
| Tables (`TxtT`): grid, cell text, merged cells, cell insets and vertical alignment, the line on every cell edge | a group of area type per cell and a stroked line per edge (VectorCraft has no tables) |
|
||||
@@ -33,7 +34,7 @@ Affinity files.
|
||||
| Opacity, visibility, lock, names, the blend modes VectorCraft has | the same |
|
||||
|
||||
Everything else is reported in the import warnings, one line per kind with a count, never
|
||||
dropped silently: bevel, blur and other layer effects, adjustment layers and live filters, brush and pressure strokes,
|
||||
dropped silently: bevel, blur and other layer effects, adjustment layers and live filters, brush strokes, pressure curves that can't be read or that go past the million points one document may hold,
|
||||
transparency gradients, fill opacity, bitmap fills, master pages based on other master pages, conical gradients (as radial),
|
||||
special shapes (cloud, heart, cog, callouts, arrows…, imported as their bounding ellipse), corner
|
||||
types other than round or straight, stars with rounded points, several fills or strokes on one object (the
|
||||
|
||||
@@ -169,6 +169,88 @@ fn curves(r: &mut Reader, crvs: ObjId, budget: &mut OutlineBudget) -> Option<Pat
|
||||
Some(path)
|
||||
}
|
||||
|
||||
/// Most segments a stroke's pressure curve may have; the curves in real documents have a handful.
|
||||
const MAX_PRESSURE_SEGMENTS: usize = 256;
|
||||
|
||||
/// Most regular samples of a pressure curve: each segment gets an equal share, at least 2 (its
|
||||
/// middle and its end) and 8 at most. The points where a segment's width stops rising or falling
|
||||
/// come on top (at most two per segment), so a curve has at most 1 + 4 × [`MAX_PRESSURE_SEGMENTS`]
|
||||
/// points; the document's limit counts all of them.
|
||||
const PRESSURE_POINTS: usize = 512;
|
||||
|
||||
/// Most points one pressure curve can have (see [`PRESSURE_POINTS`]). A curve that can't be read
|
||||
/// counts this many against the document's limit.
|
||||
pub(crate) const MAX_PRESSURE_CURVE_POINTS: usize = 1 + 4 * MAX_PRESSURE_SEGMENTS;
|
||||
|
||||
/// A stroke's pressure curve (`LDsc.LDeP`, a `CrvD` whose positional `Data` holds an array of
|
||||
/// 18-byte records as in [`curves`]): x is the position along the path (0 to 1) and y the width as
|
||||
/// a fraction of the stroke's weight. Each segment is sampled at 2 to 8 points, at most
|
||||
/// [`PRESSURE_POINTS`] in all, and also where its width stops rising or falling (at most two more
|
||||
/// points), so a dip between samples is kept → (position, width) in order along the path,
|
||||
/// positions clamped into 0..1 and never going back, widths clamped into 0..1. `None` when the
|
||||
/// curve can't be read or has more than [`MAX_PRESSURE_SEGMENTS`] segments.
|
||||
pub(crate) fn pressure(r: &Reader, crvd: ObjId) -> Option<Vec<(f64, f64)>> {
|
||||
let s = r.s;
|
||||
let data = s.obj(crvd, b"Data")?;
|
||||
let records = s.positional(data).find_map(|v| if let Value::Array(a) = v { Some(a) } else { None })?;
|
||||
// Every segment takes at least an anchor record.
|
||||
if records.len() > MAX_PRESSURE_SEGMENTS * 3 + 1 {
|
||||
return None;
|
||||
}
|
||||
let curve = subpath(&anchors(records)?, false, None)?;
|
||||
if curve.segments.len() > MAX_PRESSURE_SEGMENTS {
|
||||
return None;
|
||||
}
|
||||
let steps = u32::try_from((PRESSURE_POINTS / curve.segments.len().max(1)).clamp(2, 8)).unwrap_or(2);
|
||||
let mut samples = vec![(curve.start.x, curve.start.y)];
|
||||
let mut from = curve.start;
|
||||
for [c1, c2, to] in &curve.segments {
|
||||
let mut ts: Vec<f64> = (1..=steps).map(|i| f64::from(i) / f64::from(steps)).collect();
|
||||
ts.extend(turning_points(from.y, c1.y, c2.y, to.y).into_iter().flatten());
|
||||
ts.sort_by(f64::total_cmp);
|
||||
ts.dedup_by(|a, b| (*a - *b).abs() < 1e-9);
|
||||
for t in ts {
|
||||
let u = 1.0 - t;
|
||||
let at = |a: f64, b: f64, c: f64, d: f64| u * u * u * a + 3.0 * u * u * t * b + 3.0 * u * t * t * c + t * t * t * d;
|
||||
samples.push((at(from.x, c1.x, c2.x, to.x), at(from.y, c1.y, c2.y, to.y)));
|
||||
}
|
||||
from = *to;
|
||||
}
|
||||
let mut last = 0.0f64;
|
||||
Some(
|
||||
samples
|
||||
.into_iter()
|
||||
.map(|(x, y)| {
|
||||
last = x.clamp(last, 1.0);
|
||||
(last, y.clamp(0.0, 1.0))
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// The parameters strictly between 0 and 1 where the cubic Bézier with control values `a`, `b`,
|
||||
/// `c` and `d` stops rising or falling (where its derivative is 0). The values are scaled into
|
||||
/// -1..1 first, so huge ones can't overflow the discriminant.
|
||||
fn turning_points(a: f64, b: f64, c: f64, d: f64) -> [Option<f64>; 2] {
|
||||
let scale = a.abs().max(b.abs()).max(c.abs()).max(d.abs());
|
||||
if !(scale.is_finite() && scale > 0.0) {
|
||||
return [None, None];
|
||||
}
|
||||
let (a, b, c, d) = (a / scale, b / scale, c / scale, d / scale);
|
||||
let (d0, d1, d2) = (b - a, c - b, d - c);
|
||||
let (qa, qb, qc) = (d0 - 2.0 * d1 + d2, 2.0 * (d1 - d0), d0);
|
||||
let inside = |t: f64| (t.is_finite() && t > 0.0 && t < 1.0).then_some(t);
|
||||
if qa.abs() < 1e-12 {
|
||||
return [if qb.abs() < 1e-12 { None } else { inside(-qc / qb) }, None];
|
||||
}
|
||||
let disc = qb * qb - 4.0 * qa * qc;
|
||||
if disc < 0.0 {
|
||||
return [None, None];
|
||||
}
|
||||
let root = disc.sqrt();
|
||||
[inside((-qb - root) / (2.0 * qa)), inside((-qb + root) / (2.0 * qa))]
|
||||
}
|
||||
|
||||
struct Anchor {
|
||||
p: Point,
|
||||
inh: Option<Point>,
|
||||
@@ -260,3 +342,19 @@ fn corner_radii(fields: Vec<&Value>) -> Option<Vec<(usize, f64)>> {
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn turning_points_of_huge_control_values_are_found() {
|
||||
let small = turning_points(1.0, -3.0, 3.0, 1.0);
|
||||
let huge = turning_points(1e200, -3e200, 3e200, 1e200);
|
||||
assert!(small.iter().all(Option::is_some), "{small:?}");
|
||||
for (s, h) in small.iter().zip(&huge) {
|
||||
let (Some(s), Some(h)) = (s, h) else { panic!("{huge:?}") };
|
||||
assert!((s - h).abs() < 1e-12, "{small:?} {huge:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,9 @@ const MAX_DOCUMENT_CHARS: usize = 16 << 20;
|
||||
/// Table cells of all tables together (each becomes a text object with its edge lines).
|
||||
const MAX_DOCUMENT_CELLS: usize = 1 << 18;
|
||||
|
||||
/// Points of all strokes' pressure curves together (a stroke style may be shared by many layers).
|
||||
const MAX_DOCUMENT_PRESSURE_POINTS: usize = 1 << 20;
|
||||
|
||||
/// What a whole read may still use. Shared objects can be linked from many layers, and embedded
|
||||
/// documents are read with what their embedding document left, so linking or embedding the
|
||||
/// same content many times can't multiply the work.
|
||||
@@ -39,10 +42,18 @@ pub(crate) struct Budget {
|
||||
pub(crate) chars: usize,
|
||||
/// Table cells ([`MAX_DOCUMENT_CELLS`] in all).
|
||||
pub(crate) cells: usize,
|
||||
/// Points of pressure curves ([`MAX_DOCUMENT_PRESSURE_POINTS`] in all).
|
||||
pub(crate) pressure: usize,
|
||||
}
|
||||
|
||||
impl Budget {
|
||||
pub(crate) const FULL: Budget = Budget { nodes: MAX_NODES, pixels: MAX_LAYER_PIXELS, chars: MAX_DOCUMENT_CHARS, cells: MAX_DOCUMENT_CELLS };
|
||||
pub(crate) const FULL: Budget = Budget {
|
||||
nodes: MAX_NODES,
|
||||
pixels: MAX_LAYER_PIXELS,
|
||||
chars: MAX_DOCUMENT_CHARS,
|
||||
cells: MAX_DOCUMENT_CELLS,
|
||||
pressure: MAX_DOCUMENT_PRESSURE_POINTS,
|
||||
};
|
||||
}
|
||||
|
||||
/// Read a document: archive, `doc.dat` and the model.
|
||||
@@ -441,8 +452,34 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> {
|
||||
_ => self.warn("layer effects of other kinds (left out)"),
|
||||
}
|
||||
}
|
||||
for a in s.objs(id, b"AdCh") {
|
||||
if let Some(effect) = self.attached_recolor(a) {
|
||||
out.push(effect);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
fn attached_recolor(&mut self, a: ObjId) -> Option<Effect> {
|
||||
let s = self.s;
|
||||
if !s.is(a, b"RcRA") {
|
||||
return None;
|
||||
}
|
||||
if s.bool(a, b"Visi") == Some(false) {
|
||||
return None;
|
||||
}
|
||||
let adjp = s.obj(a, b"AdjP")?;
|
||||
if !s.is(adjp, b"RecP") {
|
||||
return None;
|
||||
}
|
||||
let h = s.f64(adjp, b"RecH").unwrap_or(0.0);
|
||||
let sat = s.f64(adjp, b"RecS").unwrap_or(1.0);
|
||||
let l = s.f64(adjp, b"RecL").unwrap_or(0.5);
|
||||
let (r, g, b) = crate::paint::hsl(h, sat, l);
|
||||
let color = crate::paint::Color::Rgb { r, g, b, a: 1.0 };
|
||||
let opacity = s.f64(a, b"Opac").filter(|v| v.is_finite()).unwrap_or(1.0).clamp(0.0, 1.0);
|
||||
let blend = self.effect_blend(a);
|
||||
Some(Effect::ColorOverlay { color, opacity, blend })
|
||||
}
|
||||
|
||||
/// A layer effect's blend mode (`BlnM`); unknown modes are Normal (warned).
|
||||
fn effect_blend(&mut self, e: ObjId) -> Blend {
|
||||
@@ -479,6 +516,8 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> {
|
||||
Some(_) => self.warn("several pixel masks on one layer (only the first is used)"),
|
||||
None => {}
|
||||
}
|
||||
} else if class == Tag::of(b"RcRA") {
|
||||
// Handled in `effects` as a colour overlay.
|
||||
} else {
|
||||
self.warn(format!("{} layers attached to other layers (imported without them)", describe(class)));
|
||||
}
|
||||
|
||||
@@ -121,6 +121,9 @@ pub struct Stroke {
|
||||
pub dash: Option<(Vec<f64>, f64)>,
|
||||
/// Painted below the fill instead of above it.
|
||||
pub behind: bool,
|
||||
/// The pressure curve: (position 0..1 along the path, width as a fraction of `width`) in
|
||||
/// order along the path, `None` for a stroke of the same width all along.
|
||||
pub profile: Option<Vec<(f64, f64)>>,
|
||||
}
|
||||
|
||||
/// Gradient stops per gradient.
|
||||
@@ -194,7 +197,7 @@ fn color_within(r: &mut Reader, id: ObjId, depth: u32) -> Option<Color> {
|
||||
})
|
||||
}
|
||||
|
||||
fn hsl(h: f64, s: f64, l: f64) -> (f64, f64, f64) {
|
||||
pub(crate) fn hsl(h: f64, s: f64, l: f64) -> (f64, f64, f64) {
|
||||
let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
|
||||
let hp = (h.rem_euclid(1.0)) * 6.0;
|
||||
let x = c * (1.0 - (hp % 2.0 - 1.0).abs());
|
||||
@@ -350,8 +353,50 @@ pub(crate) fn stroke(r: &mut Reader, pen: ObjId, ldsc: ObjId, local: Affine, beh
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
if s.obj(lsty, b"Brus").is_some() || s.obj(ldsc, b"LDeP").is_some() {
|
||||
r.warn("brush and pressure strokes (imported as plain strokes)");
|
||||
if s.obj(lsty, b"Brus").is_some() {
|
||||
r.warn("brush strokes (imported as plain strokes)");
|
||||
}
|
||||
// The pressure curve sets the width along the path; one at full width all along is a plain
|
||||
// stroke. Every curve read counts against the document's points: one at full width counts its
|
||||
// points, and one that can't be read counts the most a curve may have. Once the points are
|
||||
// used up the rest are not read.
|
||||
const BEYOND: &str = "pressure strokes beyond the pressure points one document may hold (imported as plain strokes)";
|
||||
let ldep = s.obj(ldsc, b"LDeP");
|
||||
if ldep.is_some() && r.budget.pressure == 0 {
|
||||
r.warn(BEYOND);
|
||||
}
|
||||
let profile = match ldep.filter(|_| r.budget.pressure > 0).map(|p| crate::geometry::pressure(r, p)) {
|
||||
Some(Some(points)) => match r.budget.pressure.checked_sub(points.len()) {
|
||||
Some(left) => {
|
||||
r.budget.pressure = left;
|
||||
(!points.iter().all(|(_, w)| (w - 1.0).abs() < 1e-9)).then_some(points)
|
||||
}
|
||||
None => {
|
||||
r.budget.pressure = 0;
|
||||
r.warn(BEYOND);
|
||||
None
|
||||
}
|
||||
},
|
||||
Some(None) => {
|
||||
r.budget.pressure = r.budget.pressure.saturating_sub(crate::geometry::MAX_PRESSURE_CURVE_POINTS);
|
||||
r.warn("pressure strokes whose pressure curve can't be read (imported as plain strokes)");
|
||||
None
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
Some(Stroke { paint, width, cap, join, miter_limit, align, dash, behind, profile })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn hsl_lightness_1_is_white_and_0_is_black() {
|
||||
assert_eq!(hsl(0.0, 1.0, 1.0), (1.0, 1.0, 1.0));
|
||||
assert_eq!(hsl(0.5, 1.0, 1.0), (1.0, 1.0, 1.0));
|
||||
assert_eq!(hsl(0.0, 1.0, 0.0), (0.0, 0.0, 0.0));
|
||||
let (r, g, b) = hsl(0.0, 1.0, 0.5);
|
||||
assert!((r - 1.0).abs() < 1e-6 && g.abs() < 1e-6 && b.abs() < 1e-6);
|
||||
}
|
||||
Some(Stroke { paint, width, cap, join, miter_limit, align, dash, behind })
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
affinity3-mexican-guy.af dpi=300;spreads=1;model_crc32=d23a6900;counts={"depth": 8, "fills": 169, "gradients": 0, "groups": 23, "nodes": 283, "opacity": 1, "segments": 1491, "shapes": 260, "strokes": 114, "subpaths": 264};boards=[];fonts={};warnings=["blur effects (left out) (156×)", "brush and pressure strokes (imported as plain strokes) (114×)"]
|
||||
affinity3-mexican-man.af dpi=300;spreads=1;model_crc32=b8cecb1c;counts={"depth": 7, "fills": 277, "gradients": 2, "groups": 29, "nodes": 903, "segments": 3225, "shapes": 874, "strokes": 540, "subpaths": 881};boards=[];fonts={};warnings=["blur effects (left out) (244×)", "brush and pressure strokes (imported as plain strokes) (534×)"]
|
||||
affinity3-mexican-woman.af dpi=300;spreads=1;model_crc32=23d1e39c;counts={"depth": 9, "fills": 527, "gradients": 0, "groups": 28, "nodes": 595, "segments": 3562, "shapes": 567, "strokes": 60, "subpaths": 570};boards=[];fonts={};warnings=["blur effects (left out) (278×)", "brush and pressure strokes (imported as plain strokes) (54×)"]
|
||||
affinity3-playing-cards.af dpi=300;spreads=1;model_crc32=cafb7572;counts={"depth": 5, "fills": 363, "gradients": 0, "groups": 758, "nodes": 1350, "segments": 4864, "shapes": 592, "strokes": 17, "subpaths": 656};boards=[];fonts={};warnings=["blur effects (left out) (309×)", "brush and pressure strokes (imported as plain strokes)"]
|
||||
affinity3-mexican-guy.af dpi=300;spreads=1;model_crc32=32b7cf3f;counts={"depth": 8, "fills": 169, "gradients": 0, "groups": 23, "nodes": 283, "opacity": 1, "segments": 1491, "shapes": 260, "strokes": 114, "subpaths": 264};boards=[];fonts={};warnings=["blur effects (left out) (156×)"]
|
||||
affinity3-mexican-man.af dpi=300;spreads=1;model_crc32=c10daaea;counts={"depth": 7, "fills": 277, "gradients": 2, "groups": 29, "nodes": 903, "segments": 3225, "shapes": 874, "strokes": 540, "subpaths": 881};boards=[];fonts={};warnings=["blur effects (left out) (244×)"]
|
||||
affinity3-mexican-woman.af dpi=300;spreads=1;model_crc32=3bc13374;counts={"depth": 9, "fills": 527, "gradients": 0, "groups": 28, "nodes": 595, "segments": 3562, "shapes": 567, "strokes": 60, "subpaths": 570};boards=[];fonts={};warnings=["blur effects (left out) (278×)"]
|
||||
affinity3-playing-cards.af dpi=300;spreads=1;model_crc32=bc0c838d;counts={"depth": 5, "fills": 363, "gradients": 0, "groups": 758, "nodes": 1350, "segments": 4864, "shapes": 592, "strokes": 17, "subpaths": 656};boards=[];fonts={};warnings=["blur effects (left out) (309×)", "brush strokes (imported as plain strokes)"]
|
||||
patchy-adjust-curves.af dpi=72;spreads=1;model_crc32=36ab962f;counts={"depth": 1, "images": 1, "nodes": 2, "unsupported": 1};boards=[];fonts={};warnings=["adjustment layers"]
|
||||
patchy-adjust-hsl.af dpi=72;spreads=1;model_crc32=e2eac05e;counts={"depth": 1, "images": 1, "nodes": 2, "unsupported": 1};boards=[];fonts={};warnings=["adjustment layers"]
|
||||
patchy-artboards.af dpi=72;spreads=1;model_crc32=0037a28e;counts={"artboards": 2, "depth": 2, "fills": 2, "gradients": 0, "nodes": 4, "segments": 7, "shapes": 2, "strokes": 0, "subpaths": 2};boards=["board-a:0.0000,0.0000,80.0000,60.0000", "board-b:100.0000,10.0000,160.0000,70.0000"];fonts={};warnings=[]
|
||||
@@ -30,4 +30,4 @@ patchy-text-rotated.af dpi=72;spreads=1;model_crc32=3d1de559;counts={"depth": 1,
|
||||
patchy-text-runs.af dpi=72;spreads=1;model_crc32=7820b881;counts={"depth": 1, "nodes": 1, "text_chars": 7, "text_runs": 5, "texts": 1};boards=[];fonts={"Arial:ArialNarrow:400:false", "Courier New:CourierNewPSMT:400:false", "Segoe UI Emoji:SegoeUIEmoji:400:false", "Times New Roman:TimesNewRomanPSMT:400:false"};warnings=["font width variants (exact face depends on the installed PostScript font)", "text uses a stored fallback font (imported with that font)"]
|
||||
patchy-transform.af dpi=72;spreads=1;model_crc32=8d56df84;counts={"depth": 1, "images": 1, "nodes": 1};boards=[];fonts={};warnings=[]
|
||||
patchy-vector-mask.af dpi=72;spreads=1;model_crc32=80ca946a;counts={"depth": 1, "fills": 2, "gradients": 0, "nodes": 2, "segments": 7, "shapes": 2, "strokes": 0, "subpaths": 2, "vector_masks": 1};boards=[];fonts={};warnings=["unknown layers attached to other layers (imported without them)"]
|
||||
patchy-vector.af dpi=72;spreads=1;model_crc32=b36a9767;counts={"depth": 1, "fills": 3, "gradients": 0, "nodes": 4, "segments": 17, "shapes": 4, "strokes": 2, "subpaths": 5};boards=[];fonts={};warnings=[]
|
||||
patchy-vector.af dpi=72;spreads=1;model_crc32=3b16c7ed;counts={"depth": 1, "fills": 3, "gradients": 0, "nodes": 4, "segments": 17, "shapes": 4, "strokes": 2, "subpaths": 5};boards=[];fonts={};warnings=[]
|
||||
@@ -98,20 +98,26 @@ pub fn utf8(text: &str) -> F {
|
||||
}
|
||||
|
||||
pub fn document(blocks: Vec<F>, frame: bool, dpi: f64, transform: [f64; 6]) -> Vec<u8> {
|
||||
let text = F::Obj(
|
||||
tag(if frame { b"TxtF" } else { b"TxtA" }),
|
||||
vec![
|
||||
(tag(b"StSt"), F::Obj(tag(b"Stry"), vec![(tag(b"Blok"), F::Shared(blocks))])),
|
||||
(
|
||||
tag(b"TxtH"),
|
||||
F::Obj(
|
||||
tag(if frame { b"FrFr" } else { b"ArFr" }),
|
||||
vec![(tag(b"FrmB"), F::F64s(vec![10.0, 20.0, 210.0, 120.0])), (tag(b"ArtV"), F::F64(18.0))],
|
||||
),
|
||||
document_with_children(blocks, frame, dpi, transform, vec![])
|
||||
}
|
||||
|
||||
/// [`document`] whose text holds `children` (objects nested inside the text).
|
||||
pub fn document_with_children(blocks: Vec<F>, frame: bool, dpi: f64, transform: [f64; 6], children: Vec<F>) -> Vec<u8> {
|
||||
let mut fields = vec![
|
||||
(tag(b"StSt"), F::Obj(tag(b"Stry"), vec![(tag(b"Blok"), F::Shared(blocks))])),
|
||||
(
|
||||
tag(b"TxtH"),
|
||||
F::Obj(
|
||||
tag(if frame { b"FrFr" } else { b"ArFr" }),
|
||||
vec![(tag(b"FrmB"), F::F64s(vec![10.0, 20.0, 210.0, 120.0])), (tag(b"ArtV"), F::F64(18.0))],
|
||||
),
|
||||
(tag(b"Xfrm"), F::F64s(transform.to_vec())),
|
||||
],
|
||||
);
|
||||
),
|
||||
(tag(b"Xfrm"), F::F64s(transform.to_vec())),
|
||||
];
|
||||
if !children.is_empty() {
|
||||
fields.push((tag(b"Chld"), F::Shared(children)));
|
||||
}
|
||||
let text = F::Obj(tag(if frame { b"TxtF" } else { b"TxtA" }), fields);
|
||||
let mut root = vec![
|
||||
(tag(b"UVCn"), F::Obj(tag(b"UVCn"), vec![(tag(b"UPPI"), F::F64(dpi))])),
|
||||
(
|
||||
|
||||
@@ -138,9 +138,10 @@ pub fn stain(inst: &Node) -> Option<(Color, f32)> {
|
||||
Some((f.paint.color()?, f.opacity))
|
||||
}
|
||||
|
||||
/// A symbol's art for instance `inst`, stained by the instance's fill ([`stain`]).
|
||||
/// A symbol's art for instance `inst`: with the instance's own appearance changes to it (a dynamic
|
||||
/// symbol's, [`vectorcraft_doc::symbol_overrides`]), stained by the instance's fill ([`stain`]).
|
||||
pub fn instance_art(art: &Node, inst: &Node) -> Node {
|
||||
let mut a = art.clone();
|
||||
let mut a = vectorcraft_doc::symbol_overrides::dynamic_art(art, inst);
|
||||
if let Some((c, amount)) = stain(inst) {
|
||||
tint_node(&mut a, &c, amount);
|
||||
}
|
||||
|
||||
@@ -386,10 +386,22 @@ impl CornerSource {
|
||||
#[serde(default)]
|
||||
pub struct PatternBrush {
|
||||
pub side: Node,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub side_frame: Option<Rect>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub side_swatch: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub outer_corner: Option<Node>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub outer_corner_frame: Option<Rect>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub outer_corner_swatch: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub inner_corner: Option<Node>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub inner_corner_frame: Option<Rect>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub inner_corner_swatch: Option<String>,
|
||||
/// What the outer corners take when there is no `outer_corner` art. Without art or a source,
|
||||
/// they take the `inner_corner` art.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -400,8 +412,16 @@ pub struct PatternBrush {
|
||||
pub inner_source: Option<CornerSource>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub start: Option<Node>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub start_frame: Option<Rect>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub start_swatch: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub end: Option<Node>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub end_frame: Option<Rect>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub end_swatch: Option<String>,
|
||||
/// Scale in percent.
|
||||
pub scale: f64,
|
||||
/// Space between tiles in percent of the tile width.
|
||||
@@ -422,12 +442,22 @@ impl Default for PatternBrush {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
side: defaults::stitch_tile(),
|
||||
side_frame: None,
|
||||
side_swatch: None,
|
||||
outer_corner: None,
|
||||
outer_corner_frame: None,
|
||||
outer_corner_swatch: None,
|
||||
inner_corner: None,
|
||||
inner_corner_frame: None,
|
||||
inner_corner_swatch: None,
|
||||
outer_source: None,
|
||||
inner_source: None,
|
||||
start: None,
|
||||
start_frame: None,
|
||||
start_swatch: None,
|
||||
end: None,
|
||||
end_frame: None,
|
||||
end_swatch: None,
|
||||
scale: 100.0,
|
||||
spacing: 0.0,
|
||||
fit: PatternFit::Stretch,
|
||||
@@ -500,6 +530,14 @@ fn within_pair((a, b): (f64, f64), range: (f64, f64), fallback: f64) -> (f64, f6
|
||||
(within(a, range, fallback), within(b, range, fallback))
|
||||
}
|
||||
|
||||
fn valid_tile_frame(frame: Rect) -> bool {
|
||||
[frame.x0, frame.y0, frame.x1, frame.y1].into_iter().all(f64::is_finite)
|
||||
&& frame.width().is_finite()
|
||||
&& frame.height().is_finite()
|
||||
&& frame.width() > 1e-9
|
||||
&& frame.height() > 1e-9
|
||||
}
|
||||
|
||||
impl Colorization {
|
||||
/// A Hue Shift key colour that isn't a colour (components out of reach) becomes black.
|
||||
fn sanitize(&mut self) {
|
||||
@@ -600,6 +638,11 @@ impl Brush {
|
||||
BrushKind::Pattern(p) => {
|
||||
p.scale = within(p.scale, PatternBrush::SCALE_RANGE, 100.0);
|
||||
p.spacing = within(p.spacing, PatternBrush::SPACING_RANGE, 0.0);
|
||||
for frame in [&mut p.side_frame, &mut p.outer_corner_frame, &mut p.inner_corner_frame, &mut p.start_frame, &mut p.end_frame] {
|
||||
if frame.is_some_and(|f| !valid_tile_frame(f)) {
|
||||
*frame = None;
|
||||
}
|
||||
}
|
||||
p.colorization.sanitize();
|
||||
}
|
||||
BrushKind::Bristle(b) => {
|
||||
|
||||
+72
-11
@@ -27,10 +27,16 @@ fn near(a: f64, b: f64, tol: f64) -> bool {
|
||||
(a - b).abs() <= tol
|
||||
}
|
||||
|
||||
/// Points along every outline under `n`, its anchors among them. Bent art keeps its curves and
|
||||
/// long lines since #3926, so its anchors alone no longer trace it.
|
||||
fn all_points(n: &Node, out: &mut Vec<Point>) {
|
||||
n.walk(&mut |c| {
|
||||
if let Some(p) = c.path_data() {
|
||||
out.extend(p.anchors().map(|(_, _, a)| a.p));
|
||||
for sp in c.path_data().into_iter().flat_map(|p| &p.subpaths) {
|
||||
out.extend(sp.anchors.iter().map(|a| a.p));
|
||||
for i in 0..sp.segment_count() {
|
||||
let seg = sp.segment(i);
|
||||
out.extend((1..32).map(|j| kurbo::ParamCurve::eval(&seg, j as f64 / 32.0)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -185,9 +191,12 @@ fn art_brush_follows_curves() {
|
||||
}
|
||||
}
|
||||
let out = stroke_pieces(&Document::new(1.0, 1.0), &art_brush(rect_art(10.0, 4.0), ArtScale::Stretch), &bp_open, &stroke(1.0));
|
||||
// The long edges bend as curves (#3926), where they used to be cut into some 400 chords. The
|
||||
// path is run along in 0.05 pt chords, which the curves follow, so a few dozen remain.
|
||||
let (anchors, curved) = anchors_and_curved(&out[0]);
|
||||
assert!(anchors < 100 && curved > anchors / 2, "long edges bend as curves: {curved} of {anchors} anchors with handles");
|
||||
let mut pts = vec![];
|
||||
all_points(&out[0], &mut pts);
|
||||
assert!(pts.len() > 40, "long edges are subdivided so they bend: {}", pts.len());
|
||||
for p in pts {
|
||||
let r = p.distance(Point::new(100.0, 100.0));
|
||||
assert!((r - 100.0).abs() <= 2.0 + 0.3, "point {p:?} is {r} from the centre");
|
||||
@@ -552,6 +561,43 @@ fn pattern_corner_source_none_leaves_that_kind_of_corner_empty() {
|
||||
assert!((outer == plain && inner == saved) || (inner == plain && outer == saved), "only the corners set to None are empty");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pattern_without_start_tile_runs_side_to_path_start() {
|
||||
let path = line(0.0, 0.0, 100.0, 0.0);
|
||||
let doc = Document::new(1.0, 1.0);
|
||||
let mut b = pattern_brush(PatternFit::Stretch, false);
|
||||
if let BrushKind::Pattern(p) = &mut b.kind {
|
||||
p.start = Some(rect_art(20.0, 4.0));
|
||||
}
|
||||
let with_start = stroke_pieces(&doc, &b, &path, &stroke(1.0));
|
||||
assert_eq!(with_start[0].geometric_bounds().unwrap().width(), 20.0);
|
||||
if let BrushKind::Pattern(p) = &mut b.kind {
|
||||
p.start = None;
|
||||
}
|
||||
let without_start = stroke_pieces(&doc, &b, &path, &stroke(1.0));
|
||||
assert_eq!(without_start.len(), 10);
|
||||
assert!(near(without_start[0].geometric_bounds().unwrap().x0, 0.0, 1e-6));
|
||||
assert!(near(without_start[0].geometric_bounds().unwrap().width(), 10.0, 1e-6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pattern_without_end_tile_runs_side_to_path_end() {
|
||||
let path = line(0.0, 0.0, 100.0, 0.0);
|
||||
let doc = Document::new(1.0, 1.0);
|
||||
let mut b = pattern_brush(PatternFit::Stretch, false);
|
||||
if let BrushKind::Pattern(p) = &mut b.kind {
|
||||
p.end = Some(rect_art(20.0, 4.0));
|
||||
}
|
||||
let with_end = stroke_pieces(&doc, &b, &path, &stroke(1.0));
|
||||
assert!(with_end.iter().any(|n| near(n.geometric_bounds().unwrap().width(), 20.0, 1e-6)));
|
||||
if let BrushKind::Pattern(p) = &mut b.kind {
|
||||
p.end = None;
|
||||
}
|
||||
let without_end = stroke_pieces(&doc, &b, &path, &stroke(1.0));
|
||||
assert_eq!(without_end.len(), 10);
|
||||
assert!(near(without_end.last().unwrap().geometric_bounds().unwrap().x1, 100.0, 1e-6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pattern_corner_sources_round_trip_and_stay_out_of_brushes_without_them() {
|
||||
let saved = serde_json::to_value(pattern_brush(PatternFit::Stretch, true)).unwrap();
|
||||
@@ -1088,7 +1134,7 @@ fn pieces_leave_out_what_draws_a_brushed_stroke_by_name() {
|
||||
let mut brushed = rect_art(10.0, 4.0);
|
||||
brushed.appearance.items.push(vectorcraft_doc::AppearanceItem::Stroke(StrokeLayer { brush: Some("s".into()), ..stroke(1.0) }));
|
||||
for (name, art) in [("plain", rect_art(10.0, 4.0)), ("brushed", brushed)] {
|
||||
doc.symbols.push(Symbol { name: name.into(), art: std::sync::Arc::new(art.clone()) });
|
||||
doc.symbols.push(Symbol { dynamic: false, name: name.into(), art: std::sync::Arc::new(art.clone()) });
|
||||
doc.patterns.push(PatternDef::new(name, vec![std::sync::Arc::new(art)]));
|
||||
}
|
||||
let instance = |symbol: &str| Node::new(NodeId(0), NodeKind::SymbolInstance { symbol: symbol.into(), xf: vectorcraft_geom::Affine::IDENTITY });
|
||||
@@ -1226,11 +1272,12 @@ fn a_pattern_tile_keeps_its_curves_on_a_straight_path() {
|
||||
assert!(near(b.height(), 40.0, 1.0), "and still stand the tile's height: {}", b.height());
|
||||
}
|
||||
|
||||
/// The other half of the same behaviour: where the path really does bend, the tile is still
|
||||
/// subdivided, because an affine cannot reproduce a bend. The fit is checked before it is used,
|
||||
/// so a bent run falls back to exactly what it did before.
|
||||
/// The other half of the same behaviour: where the path really does bend, an affine cannot
|
||||
/// reproduce the bend, so the tile is bent, and it still comes out as curves: cubics fitted to the
|
||||
/// bent art rather than the chords it used to be flattened into (#3926). Here a 4-anchor circle
|
||||
/// tile runs round a circle of radius 200, so its outline stays in the band 20 either side of it.
|
||||
#[test]
|
||||
fn a_pattern_tile_on_a_bent_path_is_still_subdivided() {
|
||||
fn a_pattern_tile_on_a_bent_path_keeps_its_curves() {
|
||||
let circle =
|
||||
Node::path(NodeId(0), shapes::ellipse(Rect::new(0.0, -20.0, 40.0, 20.0)), Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0));
|
||||
let brush = Brush {
|
||||
@@ -1240,9 +1287,23 @@ fn a_pattern_tile_on_a_bent_path_is_still_subdivided() {
|
||||
// A circle to run along: every tile sits on a curving section.
|
||||
let round = shapes::ellipse(Rect::new(0.0, 0.0, 400.0, 400.0)).to_bezpath();
|
||||
let out = stroke_pieces(&Document::new(1.0, 1.0), &brush, &round, &stroke(1.0));
|
||||
assert!(!out.is_empty());
|
||||
let bent = out.iter().filter(|n| anchors_and_curved(n).0 > 8).count();
|
||||
assert!(bent > out.len() / 2, "tiles on a bend are still densified: {bent} of {}", out.len());
|
||||
assert!(out.len() > 20, "tiles all the way round: {}", out.len());
|
||||
let (mut lo, mut hi) = (f64::MAX, f64::MIN);
|
||||
for n in &out {
|
||||
let (anchors, curved) = anchors_and_curved(n);
|
||||
assert!((4..=16).contains(&anchors), "a bent circle tile is a few curves, not a polygon: {anchors} anchors");
|
||||
assert!(curved == anchors, "every anchor keeps a handle: {curved} of {anchors}");
|
||||
let mut pts = vec![];
|
||||
all_points(n, &mut pts);
|
||||
for q in pts {
|
||||
let r = q.distance(Point::new(200.0, 200.0));
|
||||
(lo, hi) = (lo.min(r), hi.max(r));
|
||||
}
|
||||
}
|
||||
// The path is a cubic circle (0.06 out at most) run along in chords of 0.05, and the fit holds
|
||||
// a quarter of the 0.25 flattening tolerance, so the band holds to 0.2.
|
||||
assert!(lo > 179.8 && hi < 220.2, "the curves stay in the tile's band round the path: {lo:.3}..{hi:.3}");
|
||||
assert!(lo < 180.2 && hi > 219.8, "and reach both edges of it: {lo:.3}..{hi:.3}");
|
||||
}
|
||||
|
||||
/// The kind of every node in a piece of brushed art, in paint order.
|
||||
|
||||
+352
-63
@@ -1,13 +1,15 @@
|
||||
//! Art, scatter and pattern brushes: art bent along the path by arc length.
|
||||
//!
|
||||
//! Art coordinates `(x, y)` map to the path point at arc length `s(x)` offset by
|
||||
//! `(y - centre) × cross` along the path's left normal. Art outlines are flattened and
|
||||
//! subdivided first so long straight edges bend with the path instead of cutting chords.
|
||||
//! `(y - centre) × cross` along the path's left normal. Art outlines come out as cubics fitted
|
||||
//! to the bent art, so curves stay curves and long straight edges bend with the path (#3926);
|
||||
//! where that would take more anchors than chords, they are flattened and subdivided instead.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use kurbo::ParamCurveNearest;
|
||||
use vectorcraft_doc::{Appearance, Node, NodeId, NodeKind, PressureProfile};
|
||||
use vectorcraft_geom::{Affine, BezPath, PathData, PathEl, Point, Rect, SubPath, Vec2};
|
||||
use vectorcraft_geom::{Affine, BezPath, CubicBez, ParamCurve, PathData, PathEl, Point, Rect, SubPath, Vec2};
|
||||
|
||||
use crate::track::{Rng, Track, normal, seed_of, tracks};
|
||||
use crate::{ArtBrush, ArtScale, CornerSource, Direction, PatternBrush, PatternFit, Scatter, Variation, tolerance};
|
||||
@@ -34,22 +36,27 @@ const MAX_WALK: usize = 10_000;
|
||||
/// Points on each side of the box an Auto-Sliced pattern brush corner masks its tiles with.
|
||||
const MASK_STEPS: usize = 32;
|
||||
|
||||
/// Bend every path under `n` through `f`. Paths are flattened with `flat_tol` (art space) and
|
||||
/// segments subdivided so none is longer than `max_du` along x. Stroke widths scale by
|
||||
/// `width_scale`. Text, images and symbol instances can't bend and are dropped.
|
||||
pub(crate) fn warp_node(n: &Node, f: &dyn Fn(Point) -> Point, flat_tol: f64, max_du: f64, width_scale: f64) -> Option<Node> {
|
||||
/// Bend every path under `n` through `f`. Each art segment comes out as cubics that follow it
|
||||
/// through the bend to within [`fit_tol`] of `doc_tol`, the flattening tolerance in document
|
||||
/// space, so the art keeps its curves (#3926). Where that cannot be done in no more anchors than
|
||||
/// chords would take, the path is flattened with `flat_tol` (art space) and subdivided so no chord
|
||||
/// is longer than `max_du` along x, as it always was. Stroke widths scale by `width_scale`.
|
||||
/// Text, images and symbol instances can't bend and are dropped. The art of a linked opacity mask
|
||||
/// bends with the object it masks.
|
||||
pub(crate) fn warp_node(n: &Node, f: &dyn Fn(Point) -> Point, flat_tol: f64, max_du: f64, doc_tol: f64, width_scale: f64) -> Option<Node> {
|
||||
let mut out = n.clone();
|
||||
out.appearance.scale_strokes(width_scale);
|
||||
match &mut out.kind {
|
||||
NodeKind::Path { path, live, .. } => {
|
||||
*live = None;
|
||||
*path = warp_path(path, f, flat_tol, max_du);
|
||||
*path = warp_path(path, f, flat_tol, max_du, fit_tol(doc_tol));
|
||||
if path.is_empty() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } | NodeKind::Compound { children, .. } => {
|
||||
let kids: Vec<Arc<Node>> = children.iter().filter_map(|c| warp_node(c, f, flat_tol, max_du, width_scale)).map(Arc::new).collect();
|
||||
let kids: Vec<Arc<Node>> =
|
||||
children.iter().filter_map(|c| warp_node(c, f, flat_tol, max_du, doc_tol, width_scale)).map(Arc::new).collect();
|
||||
if kids.is_empty() {
|
||||
return None;
|
||||
}
|
||||
@@ -57,57 +64,171 @@ pub(crate) fn warp_node(n: &Node, f: &dyn Fn(Point) -> Point, flat_tol: f64, max
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
map_mask(&mut out, |art| warp_node(art, f, flat_tol, max_du, doc_tol, width_scale));
|
||||
Some(out)
|
||||
}
|
||||
|
||||
fn warp_path(p: &PathData, f: &dyn Fn(Point) -> Point, flat_tol: f64, max_du: f64) -> PathData {
|
||||
let bp = p.to_bezpath();
|
||||
/// Replace the art of `n`'s linked opacity mask, if it has one, with what `map` makes of it. Where
|
||||
/// `map` draws nothing, the mask's art becomes an empty group.
|
||||
fn map_mask(n: &mut Node, map: impl FnOnce(&Node) -> Option<Node>) {
|
||||
if let Some(m) = n.extras_if_any().and_then(|e| e.mask.as_mut())
|
||||
&& m.linked
|
||||
{
|
||||
let id = m.art.id;
|
||||
m.art = Arc::new(map(&m.art).unwrap_or_else(|| Node::group(id, vec![])));
|
||||
}
|
||||
}
|
||||
|
||||
/// Each subpath of `p` through `f`: fitted cubics when they take no more anchors than the chords
|
||||
/// would, else the chords. Points `f` cannot place (not finite) are left out of the chords.
|
||||
fn warp_path(p: &PathData, f: &dyn Fn(Point) -> Point, flat_tol: f64, max_du: f64, fit_tol: f64) -> PathData {
|
||||
let mut subs = vec![];
|
||||
let mut cur: Vec<Point> = vec![];
|
||||
let mut els = vec![];
|
||||
kurbo::flatten(bp.iter(), flat_tol.max(1e-4), |e| els.push(e));
|
||||
let finish = |cur: &mut Vec<Point>, closed: bool, subs: &mut Vec<SubPath>| {
|
||||
let pts = std::mem::take(cur);
|
||||
if pts.len() < 2 {
|
||||
return;
|
||||
for sp in &p.subpaths {
|
||||
let Some(dense) = densified(sp, flat_tol, max_du) else { continue };
|
||||
if let Some(fit) = fitted(sp, f, fit_tol, dense.len()) {
|
||||
subs.push(fit);
|
||||
continue;
|
||||
}
|
||||
let mut dense = vec![pts[0]];
|
||||
let n = pts.len();
|
||||
let segs = if closed { n } else { n - 1 };
|
||||
for i in 0..segs {
|
||||
let (a, b) = (pts[i], pts[(i + 1) % n]);
|
||||
let k = (((b.x - a.x).abs() / max_du).ceil() as usize).clamp(1, MAX_SUB);
|
||||
for j in 1..=k {
|
||||
let q = a.lerp(b, j as f64 / k as f64);
|
||||
if closed && i == segs - 1 && j == k {
|
||||
break;
|
||||
}
|
||||
dense.push(q);
|
||||
}
|
||||
}
|
||||
let mapped: Vec<Point> = dense.into_iter().map(f).collect();
|
||||
subs.push(SubPath::polyline(&mapped, closed));
|
||||
};
|
||||
for e in els {
|
||||
match e {
|
||||
PathEl::MoveTo(q) => {
|
||||
finish(&mut cur, false, &mut subs);
|
||||
cur.push(q);
|
||||
}
|
||||
PathEl::LineTo(q) => cur.push(q),
|
||||
PathEl::ClosePath => {
|
||||
if cur.len() > 1 && cur[0].distance(cur[cur.len() - 1]) < 1e-9 {
|
||||
cur.pop();
|
||||
}
|
||||
finish(&mut cur, true, &mut subs);
|
||||
}
|
||||
_ => {}
|
||||
let mapped: Vec<Point> = dense.into_iter().map(f).filter(|q| q.is_finite()).collect();
|
||||
if mapped.len() >= 2 {
|
||||
subs.push(SubPath::polyline(&mapped, sp.closed));
|
||||
}
|
||||
}
|
||||
finish(&mut cur, false, &mut subs);
|
||||
PathData::new(subs)
|
||||
}
|
||||
|
||||
/// `sp` flattened with `flat_tol` and its chords subdivided so none is longer than `max_du` along
|
||||
/// x, in art space: the points the chord route maps. `None` below two points.
|
||||
fn densified(sp: &SubPath, flat_tol: f64, max_du: f64) -> Option<Vec<Point>> {
|
||||
let mut bp = BezPath::new();
|
||||
sp.to_bezpath_into(&mut bp);
|
||||
let mut pts = vec![];
|
||||
kurbo::flatten(bp.iter(), flat_tol.max(1e-4), |e| {
|
||||
if let PathEl::MoveTo(q) | PathEl::LineTo(q) = e {
|
||||
pts.push(q);
|
||||
}
|
||||
});
|
||||
if sp.closed && pts.len() > 1 && pts.first()?.distance(*pts.last()?) < 1e-9 {
|
||||
pts.pop();
|
||||
}
|
||||
if pts.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
let segs = if sp.closed { pts.len() } else { pts.len() - 1 };
|
||||
let mut dense = vec![*pts.first()?];
|
||||
for (i, (&a, &b)) in pts.iter().zip(pts.iter().cycle().skip(1)).take(segs).enumerate() {
|
||||
let k = (((b.x - a.x).abs() / max_du).ceil() as usize).clamp(1, MAX_SUB);
|
||||
for j in 1..=k {
|
||||
if sp.closed && i == segs - 1 && j == k {
|
||||
break;
|
||||
}
|
||||
dense.push(a.lerp(b, j as f64 / k as f64));
|
||||
}
|
||||
}
|
||||
Some(dense)
|
||||
}
|
||||
|
||||
/// Times an art segment is halved at most while looking for cubics that fit it.
|
||||
const FIT_DEPTH: u32 = 12;
|
||||
/// Parameter step of the finite differences that stand in for the bend's derivative.
|
||||
const FIT_DT: f64 = 1e-6;
|
||||
/// Sections a fitted piece is checked at the ends of (the ends themselves are exact).
|
||||
const FIT_CHECKS: usize = 8;
|
||||
/// Share of the flattening tolerance the fitted curves hold to. The chords were exact at their
|
||||
/// points and a few hundredths off between them, so a straight edge bends about as closely.
|
||||
const FIT_SHARE: f64 = 0.25;
|
||||
/// Tolerance paths are flattened to before art is laid along them.
|
||||
const TRACK_TOL: f64 = 0.05;
|
||||
|
||||
/// The distance (document space) fitted curves keep to the bent art, for flattening tolerance
|
||||
/// `doc_tol`: never below [`TRACK_TOL`], since following the flattened path more closely than it
|
||||
/// follows the real one only traces its chords.
|
||||
fn fit_tol(doc_tol: f64) -> f64 {
|
||||
(doc_tol * FIT_SHARE).max(TRACK_TOL)
|
||||
}
|
||||
|
||||
/// `sp` through `f` as cubics within `tol` (document space), one or more per art segment so its
|
||||
/// anchors, corners included, stay anchors. `None` when that takes more than `most` anchors, when
|
||||
/// a piece still does not fit after [`FIT_DEPTH`] halvings, or when `f` cannot place a point.
|
||||
///
|
||||
/// Each piece is the cubic with the bent piece's ends and their derivatives, which is the piece
|
||||
/// itself where the bend is affine and converges fast where it is not. It is checked against the
|
||||
/// bend at the same parameters, so the distance measured is never less than the true one. The
|
||||
/// halving is our own rather than kurbo's `fit_to_bezpath`, which halves until the floats run out
|
||||
/// on a map that never fits; the depth and the anchor count bound this one.
|
||||
fn fitted(sp: &SubPath, f: &dyn Fn(Point) -> Point, tol: f64, most: usize) -> Option<SubPath> {
|
||||
if !(tol.is_finite() && tol > 0.0) {
|
||||
return None;
|
||||
}
|
||||
let start = f(sp.anchors.first()?.p);
|
||||
if !start.is_finite() {
|
||||
return None;
|
||||
}
|
||||
let mut bp = BezPath::new();
|
||||
bp.move_to(start);
|
||||
// Pieces allowed: an open subpath's first anchor is one more, a closed one's last piece ends on it.
|
||||
let mut room = if sp.closed { most } else { most.checked_sub(1)? };
|
||||
for i in 0..sp.segment_count() {
|
||||
let c = sp.segment(i);
|
||||
let line = sp.segment_is_line(i);
|
||||
// A line's handles sit on its ends, which stalls its parameter there; thirds keep it even.
|
||||
let c = if line { CubicBez::new(c.p0, c.p0.lerp(c.p3, 1.0 / 3.0), c.p0.lerp(c.p3, 2.0 / 3.0), c.p3) } else { c };
|
||||
let at = |t: f64| Some(f(c.eval(t))).filter(|q| q.is_finite());
|
||||
fit_into(&at, (0.0, 1.0), line, tol, FIT_DEPTH, &mut bp, &mut room)?;
|
||||
}
|
||||
if sp.closed {
|
||||
bp.close_path();
|
||||
}
|
||||
// The last piece ends on the first anchor's image, so a closed subpath merges it back in; the
|
||||
// count is checked again for a map that jumps right there and keeps the two apart.
|
||||
PathData::from_bezpath(&bp).subpaths.into_iter().next().filter(|s| s.anchors.len() <= most)
|
||||
}
|
||||
|
||||
/// Pieces for parameters `t0..t1` of the bent segment `at` onto `bp`, which ends at `at(t0)`:
|
||||
/// halved until each is within `tol`, at most `depth` times and `room` pieces. A `line` that is
|
||||
/// still straight within `tol` stays a line.
|
||||
fn fit_into(
|
||||
at: &dyn Fn(f64) -> Option<Point>,
|
||||
(t0, t1): (f64, f64),
|
||||
line: bool,
|
||||
tol: f64,
|
||||
depth: u32,
|
||||
bp: &mut BezPath,
|
||||
room: &mut usize,
|
||||
) -> Option<()> {
|
||||
let span = t1 - t0;
|
||||
let (p0, p3) = (at(t0)?, at(t1)?);
|
||||
// Derivatives read from inside the piece, so a kink at either end (a corner, a turn of the
|
||||
// track) stays one.
|
||||
let d0 = (at(t0 + FIT_DT)? - p0) * (span / FIT_DT);
|
||||
let d1 = (p3 - at(t1 - FIT_DT)?) * (span / FIT_DT);
|
||||
let c = CubicBez::new(p0, p0 + d0 / 3.0, p3 - d1 / 3.0, p3);
|
||||
let chord = kurbo::Line::new(p0, p3);
|
||||
let (mut curve_fits, mut line_fits) = (c.is_finite(), line);
|
||||
for k in 1..FIT_CHECKS {
|
||||
if !(curve_fits || line_fits) {
|
||||
break;
|
||||
}
|
||||
let u = k as f64 / FIT_CHECKS as f64;
|
||||
let q = at(t0 + span * u)?;
|
||||
curve_fits &= q.distance(c.eval(u)) <= tol;
|
||||
line_fits &= chord.nearest(q, 1e-9).distance_sq <= tol * tol;
|
||||
}
|
||||
if line_fits || curve_fits {
|
||||
*room = room.checked_sub(1)?;
|
||||
if line_fits {
|
||||
bp.line_to(p3);
|
||||
} else {
|
||||
bp.curve_to(c.p1, c.p2, p3);
|
||||
}
|
||||
return Some(());
|
||||
}
|
||||
let depth = depth.checked_sub(1)?;
|
||||
let mid = t0 + span / 2.0;
|
||||
fit_into(at, (t0, mid), line, tol, depth, bp, room)?;
|
||||
fit_into(at, (mid, t1), line, tol, depth, bp, room)
|
||||
}
|
||||
|
||||
/// Art turned so it runs left → right, flipped as requested; returns it with its bounds.
|
||||
fn oriented(art: &Node, dir: Direction, flip_along: bool, flip_across: bool) -> Option<(Node, Rect)> {
|
||||
let mut a = art.clone();
|
||||
@@ -126,6 +247,16 @@ fn oriented(art: &Node, dir: Direction, flip_along: bool, flip_across: bool) ->
|
||||
(b.width() > 1e-9).then_some((a, b))
|
||||
}
|
||||
|
||||
/// Orient a tile's repeat frame with its art. The frame can be smaller than the art.
|
||||
fn oriented_tile(art: &Node, frame: Option<Rect>, flip_along: bool, flip_across: bool) -> Option<(Node, Rect)> {
|
||||
let (node, bounds) = oriented(art, Direction::LeftToRight, flip_along, flip_across)?;
|
||||
let Some(frame) = frame else { return Some((node, bounds)) };
|
||||
let x = if flip_along { (-frame.x1, -frame.x0) } else { (frame.x0, frame.x1) };
|
||||
let y = if flip_across { (-frame.y1, -frame.y0) } else { (frame.y0, frame.y1) };
|
||||
let frame = Rect::new(x.0, y.0, x.1, y.1);
|
||||
(frame.width() > 1e-9 && frame.height() > 1e-9).then_some((node, frame))
|
||||
}
|
||||
|
||||
/// Piecewise-linear map from art x (offset from the art's left edge) to arc length.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct AlongMap {
|
||||
@@ -230,7 +361,8 @@ fn affine_fit(f: &dyn Fn(Point) -> Point, b: Rect, tol: f64) -> Option<Affine> {
|
||||
/// Everything else matches [`warp_node`] deliberately, so curves are the only difference between
|
||||
/// the two routes: the same node kinds are kept and the rest dropped, `live` is cleared on every
|
||||
/// path, an empty path or a group left with no children is dropped, and strokes are scaled by
|
||||
/// `width_scale` at every level rather than by the transform's own factor.
|
||||
/// `width_scale` at every level rather than by the transform's own factor, the art of linked
|
||||
/// opacity masks included.
|
||||
fn affine_node(n: &Node, a: Affine, width_scale: f64) -> Option<Node> {
|
||||
let mut out = prepared(n, width_scale)?;
|
||||
out.transform(a, false);
|
||||
@@ -257,6 +389,7 @@ fn prepared(n: &Node, width_scale: f64) -> Option<Node> {
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
map_mask(&mut out, |art| prepared(art, width_scale));
|
||||
Some(out)
|
||||
}
|
||||
|
||||
@@ -281,7 +414,7 @@ fn bend(t: &Track, art: &Node, b: Rect, along: AlongMap, cross: f64) -> Option<N
|
||||
{
|
||||
return affine_node(art, a, cross.abs());
|
||||
}
|
||||
warp_node(art, &f, flat_tol, MAX_SEG / along.max_scale(), cross.abs())
|
||||
warp_node(art, &f, flat_tol, MAX_SEG / along.max_scale(), flat_tol * scale, cross.abs())
|
||||
}
|
||||
|
||||
/// [`bend`], with the cross scale at arc length `s` multiplied by `widen(s)` (0..=1). The strokes
|
||||
@@ -297,7 +430,7 @@ fn bend_widened(t: &Track, art: &Node, b: Rect, along: AlongMap, cross: f64, wid
|
||||
let art = widen_strokes(art, &factor);
|
||||
let scale = along.max_scale().max(cross.abs()).max(1e-6);
|
||||
let flat_tol = tolerance(b.height().max(b.width()) * scale) / scale;
|
||||
warp_node(&art, &f, flat_tol, MAX_SEG / along.max_scale(), cross.abs())
|
||||
warp_node(&art, &f, flat_tol, MAX_SEG / along.max_scale(), flat_tol * scale, cross.abs())
|
||||
}
|
||||
|
||||
/// `n` with the strokes of it and of everything inside it scaled by `factor` of the object they
|
||||
@@ -308,6 +441,7 @@ fn widen_strokes(n: &Node, factor: &dyn Fn(&Node) -> f64) -> Node {
|
||||
if let NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } | NodeKind::Compound { children, .. } = &mut out.kind {
|
||||
*children = children.iter().map(|c| Arc::new(widen_strokes(c, factor))).collect();
|
||||
}
|
||||
map_mask(&mut out, |art| Some(widen_strokes(art, factor)));
|
||||
out
|
||||
}
|
||||
|
||||
@@ -377,7 +511,7 @@ pub(crate) fn art(a: &ArtBrush, bp: &BezPath, weight: f64, pressure: Option<&Pre
|
||||
let width = light.max(heavy);
|
||||
let cross = weight * width / 100.0;
|
||||
let mut out = vec![];
|
||||
for t in tracks(bp, 0.05) {
|
||||
for t in tracks(bp, TRACK_TOL) {
|
||||
let l = t.len();
|
||||
if l <= 1e-9 {
|
||||
continue;
|
||||
@@ -419,7 +553,7 @@ pub(crate) fn scatter(sc: &Scatter, bp: &BezPath, weight: f64, pressure: Option<
|
||||
let mut rng = Rng::new(seed_of(bp, name));
|
||||
let [size_mode, spacing_mode, scatter_mode, rotation_mode] = sc.modes();
|
||||
let mut out = Budget::default();
|
||||
for t in tracks(bp, 0.05) {
|
||||
for t in tracks(bp, TRACK_TOL) {
|
||||
if out.full() {
|
||||
break;
|
||||
}
|
||||
@@ -482,6 +616,9 @@ fn anchors_of(n: &Node) -> usize {
|
||||
if let Some(c) = n.children() {
|
||||
stack.extend(c.iter().map(Arc::as_ref));
|
||||
}
|
||||
if let Some(m) = &n.extras().mask {
|
||||
stack.push(&m.art);
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
@@ -588,7 +725,7 @@ fn auto_corner(t: &Track, i: usize, source: CornerSource, side: &(Node, Rect), k
|
||||
}
|
||||
let s = if tw > 1e-9 { len / tw } else { 1.0 };
|
||||
let max_du = MAX_SEG / (2.0 * k * s.max(1.0));
|
||||
let warp = |f: &dyn Fn(Point) -> Point| warp_node(art, f, flat_tol, max_du, k);
|
||||
let warp = |f: &dyn Fn(Point) -> Point| warp_node(art, f, flat_tol, max_du, flat_tol * k, k);
|
||||
// Art point → (distance along from the left edge, stretched to `len`, and offset across).
|
||||
let along = move |q: Point| {
|
||||
let (x, y) = local(q);
|
||||
@@ -673,20 +810,20 @@ fn masked(n: Node, quad: &[Point], p: Point, towards: Vec2) -> Option<Node> {
|
||||
}
|
||||
|
||||
pub(crate) fn pattern(pb: &PatternBrush, bp: &BezPath, weight: f64) -> Vec<Node> {
|
||||
let orient = |n: &Node| oriented(n, Direction::LeftToRight, pb.flip_along, pb.flip_across);
|
||||
let Some(side) = orient(&pb.side) else { return vec![] };
|
||||
let orient = |n: &Node, frame| oriented_tile(n, frame, pb.flip_along, pb.flip_across);
|
||||
let Some(side) = orient(&pb.side, pb.side_frame) else { return vec![] };
|
||||
let k = weight * pb.scale.max(1.0) / 100.0;
|
||||
let outer = pb.outer_corner.as_ref().and_then(orient);
|
||||
let inner = pb.inner_corner.as_ref().and_then(orient);
|
||||
let start = pb.start.as_ref().and_then(orient);
|
||||
let end = pb.end.as_ref().and_then(orient);
|
||||
let outer = pb.outer_corner.as_ref().and_then(|n| orient(n, pb.outer_corner_frame));
|
||||
let inner = pb.inner_corner.as_ref().and_then(|n| orient(n, pb.inner_corner_frame));
|
||||
let start = pb.start.as_ref().and_then(|n| orient(n, pb.start_frame));
|
||||
let end = pb.end.as_ref().and_then(|n| orient(n, pb.end_frame));
|
||||
let tw = side.1.width() * k;
|
||||
let gap = tw * pb.spacing.max(0.0) / 100.0;
|
||||
// Each kind of corner takes its own art, else its source, else the other kind's art.
|
||||
let outer_tile = outer.as_ref().map(Corner::Art).or(pb.outer_source.map(Corner::Source));
|
||||
let inner_tile = inner.as_ref().map(Corner::Art).or(pb.inner_source.map(Corner::Source));
|
||||
let mut out = Budget::default();
|
||||
for t in tracks(bp, 0.05) {
|
||||
for t in tracks(bp, TRACK_TOL) {
|
||||
if out.full() {
|
||||
break;
|
||||
}
|
||||
@@ -921,4 +1058,156 @@ mod tests {
|
||||
let moved = bend(&straight, &circle, b, along, cross).expect("the straight run still draws something");
|
||||
assert_eq!(anchors(&moved), 4, "the straight run keeps the tile's curves");
|
||||
}
|
||||
|
||||
/// A 4-anchor circle tile filling `b`.
|
||||
fn circle_tile(b: Rect) -> Node {
|
||||
Node::path(
|
||||
NodeId(0),
|
||||
vectorcraft_geom::shapes::ellipse(b),
|
||||
Appearance::basic(vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK), vectorcraft_color::Paint::None, 0.0),
|
||||
)
|
||||
}
|
||||
|
||||
/// `per` points along each segment of `sp`, through `f`.
|
||||
fn traced(sp: &SubPath, f: &dyn Fn(Point) -> Point, per: usize) -> Vec<Point> {
|
||||
(0..sp.segment_count())
|
||||
.flat_map(|i| {
|
||||
let c = sp.segment(i);
|
||||
(0..per).map(move |j| f(c.eval(j as f64 / per as f64)))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// How far apart two closed outlines get (Hausdorff distance, each taken as a polygon).
|
||||
fn apart(a: &[Point], b: &[Point]) -> f64 {
|
||||
let to = |q: Point, pts: &[Point]| {
|
||||
pts.iter()
|
||||
.zip(pts.iter().cycle().skip(1))
|
||||
.map(|(&a, &b)| {
|
||||
let ab = b - a;
|
||||
let t = ((q - a).dot(ab) / ab.hypot2().max(1e-18)).clamp(0.0, 1.0);
|
||||
q.distance(a + ab * t)
|
||||
})
|
||||
.fold(f64::MAX, f64::min)
|
||||
};
|
||||
let one_way = |a: &[Point], b: &[Point]| a.iter().map(|&q| to(q, b)).fold(0.0, f64::max);
|
||||
one_way(a, b).max(one_way(b, a))
|
||||
}
|
||||
|
||||
/// #3926: on a bend the tile is fitted with cubics instead of flattened into chords. A circle
|
||||
/// tile bent round a circle stays a handful of smooth curves, closer to the bent art than the
|
||||
/// chords were, in a small fraction of their anchors.
|
||||
#[test]
|
||||
fn a_circle_tile_bent_round_a_circle_stays_a_few_curves() {
|
||||
let round = vectorcraft_geom::shapes::ellipse(Rect::new(0.0, 0.0, 400.0, 400.0)).to_bezpath();
|
||||
let t = tracks(&round, 0.05).remove(0);
|
||||
let b = Rect::new(0.0, -20.0, 40.0, 20.0);
|
||||
let along = AlongMap::linear(b.width(), 100.0, 140.0);
|
||||
let tile = circle_tile(b);
|
||||
let NodeKind::Path { path: art, .. } = &tile.kind else { panic!("the tile is a path") };
|
||||
let art = &art.subpaths[0];
|
||||
|
||||
let bent = bend(&t, &tile, b, along, 1.0).expect("the tile is drawn");
|
||||
let NodeKind::Path { path, .. } = &bent.kind else { panic!("a bent path is a path") };
|
||||
assert_eq!(path.subpaths.len(), 1);
|
||||
let sp = &path.subpaths[0];
|
||||
assert!(sp.closed, "still a closed outline");
|
||||
let n = sp.anchors.len();
|
||||
assert!((4..=16).contains(&n), "a few anchors, the tile's own four among them: {n}");
|
||||
assert!(sp.anchors.iter().all(|a| a.has_in() && a.has_out()), "every anchor keeps both handles");
|
||||
assert!(sp.anchors.first().map(|a| a.p) != sp.anchors.last().map(|a| a.p), "closed without a duplicate anchor");
|
||||
|
||||
// The same map `bend` uses, at scale 1, and the tolerance it fits to.
|
||||
let (cy, x0) = (b.center().y, b.x0);
|
||||
let f = |p: Point| t.map(along.at(p.x - x0), p.y - cy);
|
||||
let tol = tolerance(b.height().max(b.width()));
|
||||
let exact = traced(art, &f, 1000);
|
||||
let chords: Vec<Point> = densified(art, tol, MAX_SEG).expect("the tile flattens").into_iter().map(f).collect();
|
||||
let curves = traced(sp, &|p| p, 200);
|
||||
let (e_curves, e_chords) = (apart(&curves, &exact), apart(&chords, &exact));
|
||||
// Checked at eight points a piece, so a little room between them.
|
||||
assert!(e_curves <= fit_tol(tol) * 1.5, "the curves follow the bent art within {}: {e_curves:.4}", fit_tol(tol));
|
||||
assert!(e_curves < e_chords, "closer than the chords were: {e_curves:.4} against {e_chords:.4}");
|
||||
assert!(chords.len() >= 4 * n, "far fewer anchors than the {} chords took: {n}", chords.len());
|
||||
}
|
||||
|
||||
/// Fitting is bounded however badly the map behaves: where it gives points that are not finite
|
||||
/// the tile falls back to chords with those points left out (or to nothing), and where it never
|
||||
/// fits (a map that jumps everywhere) the halving stops at its depth and anchor count. Either
|
||||
/// way the call comes back promptly with finite geometry.
|
||||
#[test]
|
||||
fn a_map_that_misbehaves_still_returns_promptly_and_finite() {
|
||||
let b = Rect::new(0.0, -20.0, 40.0, 20.0);
|
||||
let tile = circle_tile(b);
|
||||
let finite = |n: &Node| match &n.kind {
|
||||
NodeKind::Path { path, .. } => {
|
||||
path.subpaths.iter().flat_map(|s| &s.anchors).all(|a| a.p.is_finite() && a.h_in.is_finite() && a.h_out.is_finite())
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
let maps: [&dyn Fn(Point) -> Point; 4] = [
|
||||
&|p| if p.x > 25.0 { Point::new(f64::NAN, p.y) } else { p },
|
||||
&|p| if p.y > 10.0 { Point::new(f64::INFINITY, p.y) } else { p + Vec2::new(p.x.sin(), 0.0) },
|
||||
&|p| Point::new(p.x, p.y + if (p.x * 1e3).sin() > 0.0 { 5.0 } else { 0.0 }),
|
||||
&|_| Point::new(f64::NAN, f64::NAN),
|
||||
];
|
||||
for (i, f) in maps.iter().enumerate() {
|
||||
let t0 = std::time::Instant::now();
|
||||
let out = warp_node(&tile, f, 0.25, 1.5, 0.25, 1.0);
|
||||
assert!(t0.elapsed() < std::time::Duration::from_secs(2), "map {i} took {:?}", t0.elapsed());
|
||||
match (i, &out) {
|
||||
(3, out) => assert!(out.is_none(), "a map that places nothing draws nothing"),
|
||||
(_, Some(n)) => assert!(finite(n), "map {i} drew non-finite geometry"),
|
||||
(_, None) => panic!("map {i} places most points, so something is drawn"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A linked opacity mask on brush art bends and widens with the art, on a bent run and on a
|
||||
/// straight one, so a mask drawn over the art still covers it. An unlinked mask stays put.
|
||||
#[test]
|
||||
fn a_linked_opacity_mask_bends_with_the_art() {
|
||||
let b = Rect::new(0.0, -20.0, 40.0, 20.0);
|
||||
let stroked = |id: u64| {
|
||||
Node::path(
|
||||
NodeId(id),
|
||||
vectorcraft_geom::shapes::ellipse(b),
|
||||
Appearance::basic(
|
||||
vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK),
|
||||
vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK),
|
||||
2.0,
|
||||
),
|
||||
)
|
||||
};
|
||||
let masked = |linked: bool| {
|
||||
let mut n = stroked(0);
|
||||
let mut mask = vectorcraft_doc::OpacityMask::new(stroked(1), true);
|
||||
mask.linked = linked;
|
||||
n.extras_mut().mask = Some(Box::new(mask));
|
||||
n
|
||||
};
|
||||
let round = vectorcraft_geom::shapes::ellipse(Rect::new(0.0, 0.0, 400.0, 400.0)).to_bezpath();
|
||||
let bent_track = tracks(&round, 0.05).remove(0);
|
||||
let mut line = BezPath::new();
|
||||
line.move_to((0.0, 0.0));
|
||||
line.line_to((320.0, 0.0));
|
||||
let straight_track = tracks(&line, 0.01).remove(0);
|
||||
let along = AlongMap::linear(b.width(), 100.0, 140.0);
|
||||
for (name, t) in [("bent", &bent_track), ("straight", &straight_track)] {
|
||||
let out = bend(t, &masked(true), b, along, 2.0).expect("the art is drawn");
|
||||
let mask = out.extras().mask.as_ref().expect("the mask is kept");
|
||||
assert_eq!(mask.art.kind, out.kind, "{name} run: the mask's art bends with the art it was drawn over");
|
||||
assert_eq!(mask.art.appearance, out.appearance, "{name} run: the mask's strokes widen with the art's");
|
||||
|
||||
let out = bend(t, &masked(false), b, along, 2.0).expect("the art is drawn");
|
||||
let mask = out.extras().mask.as_ref().expect("the mask is kept");
|
||||
assert_eq!(*mask.art, stroked(1), "{name} run: an unlinked mask stays where it was");
|
||||
|
||||
let out = bend_widened(t, &masked(true), b, along, 2.0, &|_| 0.5).expect("the art is drawn");
|
||||
let mask = out.extras().mask.as_ref().expect("the mask is kept");
|
||||
assert_eq!(mask.art.kind, out.kind, "{name} run, widened: the mask's art bends with the art");
|
||||
assert_eq!(mask.art.appearance, out.appearance, "{name} run, widened: the mask's strokes narrow with the art's");
|
||||
}
|
||||
assert_eq!(anchors_of(&masked(true)), 8, "the anchor budget counts the mask's art");
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,6 @@ image = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_json = { workspace = true }
|
||||
# The tests lay type out in the bundled fonts (the workspace leaves turning them on to the engine).
|
||||
vectorcraft-text = { workspace = true, features = ["bundled-fonts"] }
|
||||
# The tests lay type out in the bundled fonts (the workspace leaves turning them on to the engine)
|
||||
# and in fonts built in code (`vectorcraft_text::test_fonts`).
|
||||
vectorcraft-text = { workspace = true, features = ["bundled-fonts", "test-fonts"] }
|
||||
@@ -622,7 +622,7 @@ impl<'d, 'a> Build<'d, 'a> {
|
||||
let label = self.d.blocks.get(name).map_or(name, |b| b.name.as_str());
|
||||
let symbol = unique(label, &mut self.next_suffix, &self.symbol_names);
|
||||
self.symbol_names.insert(symbol.clone());
|
||||
self.doc.symbols.push(Symbol { name: symbol.clone(), art: Arc::new(art) });
|
||||
self.doc.symbols.push(Symbol { dynamic: false, name: symbol.clone(), art: Arc::new(art) });
|
||||
symbol
|
||||
});
|
||||
self.symbols.insert(key, made.clone());
|
||||
|
||||
@@ -1049,6 +1049,34 @@ fn drawings_the_export_writes_read_back() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lone_segment_reads_back_from_the_line_the_export_writes() {
|
||||
let mut doc = Document::new(200.0, 100.0);
|
||||
let layer = doc.layers[0].id;
|
||||
let mut b = kurbo::BezPath::new();
|
||||
b.move_to((20.0, 30.0));
|
||||
b.line_to((120.0, 70.0));
|
||||
let id = doc.alloc_id();
|
||||
let black = Appearance::basic(Paint::None, Paint::solid(Color::rgb8(0, 0, 0)), 1.0);
|
||||
doc.insert(Some(layer), 0, Node::path(id, vectorcraft_geom::PathData::from_bezpath(&b), black)).unwrap();
|
||||
for version in crate::DxfVersion::ALL.into_iter().filter(|v| v.r2000()) {
|
||||
let opts = crate::DxfOptions { version, unit: Unit::Points, region: doc.artboards[0].rect, ..crate::DxfOptions::default() };
|
||||
let out = crate::export(&doc, &opts).unwrap();
|
||||
assert!(std::str::from_utf8(&out.bytes).unwrap().contains("\nLINE\n"), "{version:?}");
|
||||
let back = open(&out.bytes, &points());
|
||||
let art = leaves(&back.document);
|
||||
assert_eq!(art.len(), 1, "{version:?}");
|
||||
let path = art[0].path_data().unwrap();
|
||||
assert_eq!(path.subpaths.len(), 1, "{version:?}");
|
||||
assert!(!path.subpaths[0].closed, "{version:?}");
|
||||
let shift = Affine::translate((0.0, 100.0 - height(&back.document)));
|
||||
let points = path_points(&art[0]);
|
||||
assert_eq!(points.len(), 17, "{version:?}: one segment");
|
||||
let ends = [shift * points[0], shift * points[16]];
|
||||
assert!(ends[0].distance(Point::new(20.0, 30.0)) < 1e-6 && ends[1].distance(Point::new(120.0, 70.0)) < 1e-6, "{version:?}: {ends:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closed_control_point_splines_stay_closed_through_export_and_import() {
|
||||
// (explicitly closed, ends where it starts): coincident endpoints alone do not close a path.
|
||||
|
||||
@@ -233,7 +233,8 @@ impl RasterFormat {
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum Preserve {
|
||||
/// Type becomes glyph outlines; strokes a plain CAD line can't draw (aligned inside or
|
||||
/// outside, width profiles, arrowheads, fitted dashes) become their filled outlines; brush
|
||||
/// outside, width profiles, arrowheads, fitted dashes, dashes offset into their pattern)
|
||||
/// become their filled outlines; brush
|
||||
/// strokes become their brush art.
|
||||
#[default]
|
||||
Appearance,
|
||||
@@ -277,7 +278,9 @@ pub struct DxfOptions {
|
||||
pub colors: ColorDepth,
|
||||
pub raster: RasterFormat,
|
||||
pub preserve: Preserve,
|
||||
/// Alter Paths for Appearance: every stroke is written as the filled area it paints.
|
||||
/// Alter Paths for Appearance: a stroke a CAD line can't draw (one [`Preserve::Appearance`]
|
||||
/// outlines, one wider than the widest lineweight, or any stroke in R13 and R14, which have
|
||||
/// no lineweights) is written as the filled area it paints.
|
||||
pub alter_paths: bool,
|
||||
/// Type is written as glyph outlines (always with [`Preserve::Appearance`]).
|
||||
pub outline_text: bool,
|
||||
|
||||
+147
-11
@@ -10,7 +10,7 @@ use kurbo::PathEl;
|
||||
use vectorcraft_color::Paint;
|
||||
use vectorcraft_doc::{AppearanceItem, Dash, Document, ImageObject, Node, NodeKind, StrokeLayer, SymbolBoxes, TextKind, TextObject, Unit};
|
||||
use vectorcraft_effects::stroke;
|
||||
use vectorcraft_geom::{Affine, BezPath, FillRule, PathData, Point, Rect};
|
||||
use vectorcraft_geom::{Affine, BezPath, FillRule, PathData, Point, Rect, Vec2};
|
||||
|
||||
use crate::aci::nearest_aci;
|
||||
use crate::writer::{Codes, Drawing, EntityStyle, Handle, ImageDef, Layer, LineType, fixed};
|
||||
@@ -184,6 +184,46 @@ fn circular_subpath(sub: &Sub) -> Option<CircularSub> {
|
||||
Some(CircularSub { centre, radius, samples })
|
||||
}
|
||||
|
||||
/// A closed subpath that is the four-cubic circle under an affine map: an ellipse, or a circle
|
||||
/// scaled out of proportion. `u` and `v` are the conjugate semi-diameters through its first two
|
||||
/// anchors.
|
||||
struct EllipticalSub {
|
||||
centre: Point,
|
||||
u: Vec2,
|
||||
v: Vec2,
|
||||
}
|
||||
|
||||
/// Fit a closed subpath of four cubics to one ellipse. Its anchors give the center and two
|
||||
/// conjugate semi-diameters; the subpath mapped by their inverse must pass [`circular_subpath`] as
|
||||
/// the unit circle, so the same tolerance and turn checks apply.
|
||||
fn elliptical_subpath(sub: &Sub) -> Option<EllipticalSub> {
|
||||
if !sub.closed || sub.segs.len() != 4 {
|
||||
return None;
|
||||
}
|
||||
let (a0, a1, a2) = (sub.start, sub.segs.first()?.end(), sub.segs.get(1)?.end());
|
||||
let centre = a0.midpoint(a2);
|
||||
let (u, v) = (a0 - centre, a1 - centre);
|
||||
let det = u.cross(v);
|
||||
let scale2 = u.hypot2().max(v.hypot2());
|
||||
if !det.is_finite() || !scale2.is_finite() || det.abs() <= scale2 * 1e-9 {
|
||||
return None;
|
||||
}
|
||||
let unit = |p: Point| {
|
||||
let d = p - centre;
|
||||
Point::new(d.cross(v) / det, u.cross(d) / det)
|
||||
};
|
||||
let mut segs = Vec::with_capacity(4);
|
||||
for seg in &sub.segs {
|
||||
let Seg::Cubic(a, b, to) = *seg else { return None };
|
||||
segs.push(Seg::Cubic(unit(a), unit(b), unit(to)));
|
||||
}
|
||||
let circle = circular_subpath(&Sub { start: unit(sub.start), segs, closed: true })?;
|
||||
if circle.centre.to_vec2().hypot() > 5e-4 || (circle.radius - 1.0).abs() > 5e-4 {
|
||||
return None;
|
||||
}
|
||||
Some(EllipticalSub { centre, u, v })
|
||||
}
|
||||
|
||||
fn angle_deg(p: Point, centre: Point) -> f64 {
|
||||
(p.y - centre.y).atan2(p.x - centre.x).to_degrees().rem_euclid(360.0)
|
||||
}
|
||||
@@ -233,6 +273,17 @@ const MAX_ENTITIES: u32 = 10_000_000;
|
||||
const MAX_COORD: f64 = 1e9;
|
||||
const MAX_POINTS: usize = 4_000_000;
|
||||
|
||||
/// Do the dashes of `d` start at the start of their pattern, as a linetype's do? A pattern of
|
||||
/// odd length repeats twice in its linetype.
|
||||
fn starts_with_pattern(d: &Dash) -> bool {
|
||||
if !d.is_dashed() || !d.offset.is_finite() {
|
||||
return true;
|
||||
}
|
||||
let period = d.pattern.iter().sum::<f64>() * if d.pattern.len() % 2 == 1 { 2.0 } else { 1.0 };
|
||||
let phase = d.offset.rem_euclid(period);
|
||||
phase.min(period - phase) <= period * 1e-9
|
||||
}
|
||||
|
||||
/// `bp` flattened into polygons (each closing implicitly), consecutive repeats dropped.
|
||||
fn polygons(bp: &BezPath) -> Vec<Vec<Point>> {
|
||||
// Far outside any drawing, a curve flattens to millions of points at this tolerance.
|
||||
@@ -494,7 +545,7 @@ impl<'a> Scene<'a> {
|
||||
if let Some(sym) = self.doc.symbols.iter().find(|s| &s.name == symbol)
|
||||
&& self.nest < MAX_NEST
|
||||
{
|
||||
let mut art = (*sym.art).clone();
|
||||
let mut art = vectorcraft_doc::symbol_overrides::dynamic_art(&sym.art, n);
|
||||
art.transform(*xf, false);
|
||||
self.nest += 1;
|
||||
self.node(&art, true);
|
||||
@@ -612,8 +663,7 @@ impl<'a> Scene<'a> {
|
||||
|
||||
/// The DXF lineweight nearest to `width` points (scaled with the drawing when asked).
|
||||
fn lineweight(&mut self, width: f64) -> i32 {
|
||||
let scale = if self.o.scale_lineweights { self.o.scale } else { 1.0 };
|
||||
let hundredths = width * 2540.0 / 72.0 * scale;
|
||||
let hundredths = self.hundredths(width);
|
||||
let max = LINEWEIGHTS[LINEWEIGHTS.len() - 1];
|
||||
if hundredths > f64::from(max) + 5.0 {
|
||||
self.warn(WIDE_LINES);
|
||||
@@ -621,6 +671,21 @@ impl<'a> Scene<'a> {
|
||||
LINEWEIGHTS.into_iter().min_by(|a, b| (f64::from(*a) - hundredths).abs().total_cmp(&(f64::from(*b) - hundredths).abs())).unwrap_or(max)
|
||||
}
|
||||
|
||||
/// `width` points in hundredths of a millimetre, the unit of DXF lineweights (scaled with
|
||||
/// the drawing when asked).
|
||||
fn hundredths(&self, width: f64) -> f64 {
|
||||
let scale = if self.o.scale_lineweights { self.o.scale } else { 1.0 };
|
||||
width * 2540.0 / 72.0 * scale
|
||||
}
|
||||
|
||||
/// Can a CAD line draw a plain stroke `width` points wide in this version? R12 polylines
|
||||
/// carry their width, R13 and R14 have no lineweights, and from 2000 a lineweight holds up
|
||||
/// to 2.11 mm, to the hundredth.
|
||||
fn line_keeps_width(&self, width: f64) -> bool {
|
||||
let v = self.o.version;
|
||||
!v.handles() || (v.r2000() && self.hundredths(width).round() <= f64::from(LINEWEIGHTS[LINEWEIGHTS.len() - 1]))
|
||||
}
|
||||
|
||||
/// A linetype for dash pattern `d` (one per distinct pattern).
|
||||
fn linetype(&mut self, d: &Dash) -> String {
|
||||
let mut lengths: Vec<f64> = d.pattern.iter().map(|v| v * self.k).collect();
|
||||
@@ -673,7 +738,11 @@ impl<'a> Scene<'a> {
|
||||
}
|
||||
let Some(rgb) = self.paint_rgb(&st.paint) else { return };
|
||||
let plain = stroke::is_plain(st);
|
||||
if self.o.alter_paths || (appearance && !plain) {
|
||||
// A linetype has no phase: dashes that start part way into their pattern need outlines.
|
||||
let lined = plain && st.dash.as_ref().is_none_or(starts_with_pattern);
|
||||
// Alter Paths for Appearance outlines a stroke only when a line can't draw it.
|
||||
let altered = self.o.alter_paths && !(lined && self.line_keeps_width(st.width));
|
||||
if altered || (appearance && !lined) {
|
||||
let region = stroke::outline_region(&PathData::from_bezpath(bp), rule, st);
|
||||
let style = self.style(rgb, st.opacity, None, None);
|
||||
self.area(®ion.to_bezpath(), FillRule::NonZero, &style, true);
|
||||
@@ -740,9 +809,9 @@ impl<'a> Scene<'a> {
|
||||
self.e.int(98, 0);
|
||||
}
|
||||
|
||||
/// Path `bp` as lines: polylines for straight subpaths (and for curves in R12, flattened),
|
||||
/// cubic splines for curved ones. `width`: the stroke weight, which DXF before 2000 gives
|
||||
/// polylines as a width.
|
||||
/// Path `bp` as lines: a line for an open subpath of one straight segment, polylines for
|
||||
/// other straight subpaths (and for curves in R12, flattened), cubic splines for curved ones.
|
||||
/// `width`: the stroke weight, which DXF before 2000 gives polylines as a width.
|
||||
fn lines(&mut self, bp: &BezPath, style: &EntityStyle, width: f64) {
|
||||
let v = self.o.version;
|
||||
let width = (!v.r2000() && width > 0.0).then_some(width * self.k);
|
||||
@@ -755,11 +824,17 @@ impl<'a> Scene<'a> {
|
||||
if sub.closed && pts.len() > 2 && pts.last().is_some_and(|p| p.distance(sub.start) <= 1e-9) {
|
||||
pts.pop();
|
||||
}
|
||||
self.polyline(&pts, sub.closed, style, width);
|
||||
// A LINE has no width, so a stroke before 2000 keeps its polyline width.
|
||||
match (sub.closed, width, pts.as_slice()) {
|
||||
(false, None, &[a, b]) => self.line(a, b, style),
|
||||
_ => self.polyline(&pts, sub.closed, style, width),
|
||||
}
|
||||
} else if (v.handles() || width.is_none()) && self.circular(&sub, style) {
|
||||
// Native circles/arcs are preferred by CAD/CAM software. In R12, however, a
|
||||
// stroked curve must stay on the flattened POLYLINE path so groups 40/41 retain
|
||||
// its width. Non-circular curves continue through the existing fallback.
|
||||
} else if v.handles() && self.elliptical(&sub, style) {
|
||||
// R13 and later write an ellipse as a native ELLIPSE.
|
||||
} else if v.handles() {
|
||||
self.spline(&sub, style);
|
||||
} else {
|
||||
@@ -778,6 +853,16 @@ impl<'a> Scene<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A line from `a` to `b` (document space).
|
||||
fn line(&mut self, a: Point, b: Point, style: &EntityStyle) {
|
||||
let (a, b) = (self.pt(a), self.pt(b));
|
||||
let layer = self.layer.clone();
|
||||
let h = self.handle();
|
||||
self.e.entity("LINE", h, fixed::MODEL_RECORD, &layer, style, "AcDbLine");
|
||||
self.e.xyz(10, a.x, a.y);
|
||||
self.e.xyz(11, b.x, b.y);
|
||||
}
|
||||
|
||||
/// A polyline through `pts` (document space): lightweight from R14, else a polyline with a
|
||||
/// vertex entity per point.
|
||||
fn polyline(&mut self, pts: &[Point], closed: bool, style: &EntityStyle, width: Option<f64>) {
|
||||
@@ -787,7 +872,8 @@ impl<'a> Scene<'a> {
|
||||
let pts: Vec<Point> = pts.iter().map(|p| self.pt(*p)).collect();
|
||||
let layer = self.layer.clone();
|
||||
let h = self.handle();
|
||||
let flags = i64::from(closed);
|
||||
// Bit 128: a linetype runs on through the vertices instead of restarting at each one.
|
||||
let flags = i64::from(closed) | if style.linetype.is_some() { 128 } else { 0 };
|
||||
if self.o.version.lwpolyline() {
|
||||
self.e.entity("LWPOLYLINE", h, fixed::MODEL_RECORD, &layer, style, "AcDbPolyline");
|
||||
self.e.int(90, pts.len() as i64);
|
||||
@@ -887,6 +973,53 @@ impl<'a> Scene<'a> {
|
||||
true
|
||||
}
|
||||
|
||||
/// Write a recognized elliptical subpath as a native DXF ELLIPSE (R13 and later): its center,
|
||||
/// the end of its major axis relative to the center, and the minor to major ratio. Returns
|
||||
/// false when the curve is not an ellipse, leaving the caller to write it as a spline.
|
||||
fn elliptical(&mut self, sub: &Sub, style: &EntityStyle) -> bool {
|
||||
let Some(last) = sub.segs.last().map(|s| s.end()) else { return false };
|
||||
if !sub.closed || last.distance(sub.start) > 1e-9 {
|
||||
return false;
|
||||
}
|
||||
let Some(ellipse) = elliptical_subpath(sub) else { return false };
|
||||
let centre = self.to_dxf * ellipse.centre;
|
||||
let u = self.to_dxf * (ellipse.centre + ellipse.u) - centre;
|
||||
let v = self.to_dxf * (ellipse.centre + ellipse.v) - centre;
|
||||
// The semi-axes are the square roots of the eigenvalues of [[a, b], [b, d]] = u u^T + v v^T.
|
||||
let (a, b, d) = (u.x * u.x + v.x * v.x, u.x * u.y + v.x * v.y, u.y * u.y + v.y * v.y);
|
||||
let mean = (a + d) / 2.0;
|
||||
let spread = ((a - d) / 2.0).hypot(b);
|
||||
let (major, minor) = ((mean + spread).sqrt(), (mean - spread).max(0.0).sqrt());
|
||||
let angle = 0.5 * (2.0 * b).atan2(a - d);
|
||||
let axis = Vec2::new(major * angle.cos(), major * angle.sin());
|
||||
let ratio = (minor / major).min(1.0);
|
||||
// A thinner ellipse stays a spline: DXF readers such as ezdxf need a ratio of 1e-6 or more.
|
||||
if !centre.x.is_finite() || !centre.y.is_finite() || !axis.x.is_finite() || !axis.y.is_finite() || !ratio.is_finite() || ratio < 1e-6 {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The drawing extents take the ellipse's bounding box: half-widths hypot(u.x, v.x) and
|
||||
// hypot(u.y, v.y) about the center.
|
||||
let (c, eu, ev) = (ellipse.centre, ellipse.u, ellipse.v);
|
||||
let (hx, hy) = (eu.x.hypot(ev.x), eu.y.hypot(ev.y));
|
||||
for p in [Point::new(c.x - hx, c.y), Point::new(c.x + hx, c.y), Point::new(c.x, c.y - hy), Point::new(c.x, c.y + hy)] {
|
||||
self.pt(p);
|
||||
}
|
||||
|
||||
let layer = self.layer.clone();
|
||||
let h = self.handle();
|
||||
self.e.entity("ELLIPSE", h, fixed::MODEL_RECORD, &layer, style, "AcDbEllipse");
|
||||
self.e.xyz(10, centre.x, centre.y);
|
||||
self.e.xyz(11, axis.x, axis.y);
|
||||
self.e.normal();
|
||||
// The ratio and the end parameter are written in full: rounded to six decimals, a thin
|
||||
// ellipse's minor axis would change size, and a whole turn would read back as an open curve.
|
||||
self.e.raw(40, &ratio.to_string());
|
||||
self.e.num(41, 0.0);
|
||||
self.e.raw(42, &std::f64::consts::TAU.to_string());
|
||||
true
|
||||
}
|
||||
|
||||
/// A curved subpath as one cubic B-spline whose clamped knots repeat at every joint, so it
|
||||
/// passes through every anchor (straight segments become straight cubics).
|
||||
fn spline(&mut self, sub: &Sub, style: &EntityStyle) {
|
||||
@@ -929,7 +1062,8 @@ impl<'a> Scene<'a> {
|
||||
}
|
||||
|
||||
/// Type: glyph outlines (Preserve Appearance, Outline Text, type on a path), else one text
|
||||
/// entity per run of each line.
|
||||
/// entity per run of each line, whose height (group 40) is [`CAP_HEIGHT`] times the em size its
|
||||
/// run is drawn at ([`vectorcraft_text::FontDb::font_heights_resolved`]).
|
||||
fn text(&mut self, n: &Node, t: &TextObject) {
|
||||
let outline = self.o.preserve == Preserve::Appearance || self.o.outline_text || matches!(t.kind, TextKind::OnPath { .. });
|
||||
if outline {
|
||||
@@ -941,6 +1075,8 @@ impl<'a> Scene<'a> {
|
||||
if !n.appearance.items.is_empty() {
|
||||
self.warn(TYPE_ITEMS);
|
||||
}
|
||||
let resolved = vectorcraft_text::FontDb::global().font_heights_resolved(t);
|
||||
let t = &*resolved;
|
||||
let layout = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t);
|
||||
let plain = t.plain_text();
|
||||
let xf = self.to_dxf * t.xf;
|
||||
|
||||
+337
-8
@@ -4,7 +4,7 @@ use std::sync::Arc;
|
||||
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
use vectorcraft_doc::{Appearance, CharStyle, Dash, Document, ImageBlob, ImageObject, Node, NodeKind, TextObject, Unit};
|
||||
use vectorcraft_geom::{Affine, PathData, Point, Rect, shapes};
|
||||
use vectorcraft_geom::{Affine, ParamCurve, PathData, Point, Rect, Shape, shapes};
|
||||
|
||||
use crate::*;
|
||||
|
||||
@@ -125,6 +125,71 @@ fn every_version_declares_its_acadver_and_is_well_formed() {
|
||||
assert_eq!(DxfVersion::from_id("2019"), None);
|
||||
}
|
||||
|
||||
/// The structure strict readers (Autodesk Fusion, #3085) need: the required sections and table
|
||||
/// entries, handles that every reference resolves to, and LAYOUT objects with only the group codes
|
||||
/// the DXF Reference defines for PLOTSETTINGS and LAYOUT (AutoCAD DXF Reference, OBJECTS section:
|
||||
/// https://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-1113675E-AB07-4567-BD47-B7FBE7290D1C).
|
||||
#[test]
|
||||
fn r2000_and_later_files_have_the_structure_cad_apps_require() {
|
||||
use std::collections::HashSet;
|
||||
// Common object codes, then AcDbPlotSettings, then AcDbLayout (76 is the shade plot mode in
|
||||
// the first and the UCS type in the second).
|
||||
let layout_codes: HashSet<i32> = [0, 5, 102, 330, 360, 100]
|
||||
.into_iter()
|
||||
.chain([1, 2, 4, 6, 7, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 140, 141, 142, 143, 70, 72, 73, 74, 75, 76, 77, 78, 147, 148, 149, 333])
|
||||
.chain([71, 10, 20, 11, 21, 12, 22, 32, 14, 24, 34, 15, 25, 35, 146, 13, 23, 33, 16, 26, 36, 17, 27, 37, 331, 345, 346])
|
||||
.collect();
|
||||
let d = shapes_doc();
|
||||
for v in DxfVersion::ALL.into_iter().filter(|v| *v >= DxfVersion::R2000) {
|
||||
let out = export(&d, &DxfOptions { version: v, ..opts(&d) }).unwrap();
|
||||
let p = pairs(&out.bytes);
|
||||
for name in ["HEADER", "CLASSES", "TABLES", "BLOCKS", "ENTITIES", "OBJECTS"] {
|
||||
assert!(p.windows(2).any(|w| w[0] == (0, "SECTION".into()) && w[1] == (2, name.into())), "{v:?}: {name} section");
|
||||
}
|
||||
let tables = section(&p, "TABLES");
|
||||
let names = |k: &str| -> Vec<String> { tables.iter().filter(|o| kind(o) == k).filter_map(|o| get(o, 2).map(str::to_string)).collect() };
|
||||
for (k, want) in [
|
||||
("LTYPE", "ByBlock"),
|
||||
("LTYPE", "ByLayer"),
|
||||
("LTYPE", "Continuous"),
|
||||
("LAYER", "0"),
|
||||
("STYLE", "Standard"),
|
||||
("APPID", "ACAD"),
|
||||
("DIMSTYLE", "Standard"),
|
||||
("BLOCK_RECORD", "*Model_Space"),
|
||||
("BLOCK_RECORD", "*Paper_Space"),
|
||||
] {
|
||||
assert!(names(k).iter().any(|n| n == want), "{v:?}: {k} {want}");
|
||||
}
|
||||
// Every object after the header has a handle below $HANDSEED, and every reference to
|
||||
// another object (owner 330, soft/hard pointers 340/350/390) names one of them.
|
||||
let body = &p[p.iter().position(|x| *x == (0, "ENDSEC".into())).unwrap()..];
|
||||
let handles: HashSet<u32> = body.iter().filter(|x| x.0 == 5 || x.0 == 105).map(|x| u32::from_str_radix(&x.1, 16).unwrap()).collect();
|
||||
let seed = u32::from_str_radix(&header(&p, "$HANDSEED").unwrap(), 16).unwrap();
|
||||
assert!(handles.iter().all(|h| *h < seed), "{v:?}: handles below the seed");
|
||||
for (code, value) in body.iter().filter(|x| matches!(x.0, 330 | 340 | 350 | 390)) {
|
||||
let h = u32::from_str_radix(value, 16).unwrap();
|
||||
assert!(h == 0 || handles.contains(&h), "{v:?}: group {code} points at missing handle {value}");
|
||||
}
|
||||
let objects = section(&p, "OBJECTS");
|
||||
let layouts: Vec<&Pairs> = objects.iter().filter(|o| kind(o) == "LAYOUT").collect();
|
||||
assert_eq!(layouts.len(), 2, "{v:?}: Model and Layout1");
|
||||
for l in &layouts {
|
||||
let bad: Vec<i32> = l.iter().map(|x| x.0).filter(|c| !layout_codes.contains(c)).collect();
|
||||
assert!(bad.is_empty(), "{v:?}: LAYOUT has group codes the DXF Reference doesn't define: {bad:?}");
|
||||
// The paper image origin is 148 (x) and 149 (y).
|
||||
assert!(get(l, 148).is_some() && get(l, 149).is_some(), "{v:?}: paper image origin");
|
||||
// Flag 1024 marks the model space layout, and only it.
|
||||
let name = l.iter().skip_while(|x| x.1 != "AcDbLayout").find(|x| x.0 == 1).map(|x| x.1.as_str());
|
||||
let flags: i64 = get(l, 70).unwrap().parse().unwrap();
|
||||
assert_eq!(flags & 1024 != 0, name == Some("Model"), "{v:?}: model type flag of {name:?}");
|
||||
}
|
||||
// The plot style placeholder carries only the common object codes.
|
||||
let holder = objects.iter().find(|o| kind(o) == "ACDBPLACEHOLDER").expect("placeholder");
|
||||
assert!(holder.iter().all(|x| matches!(x.0, 0 | 5 | 102 | 330)), "{v:?}: placeholder {holder:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn r12_viewport_has_no_duplicate_group_codes() {
|
||||
let d = shapes_doc();
|
||||
@@ -208,10 +273,168 @@ fn circles_and_circular_arcs_are_native_dxf_entities() {
|
||||
assert_eq!((all(&e[1], 10), all(&e[1], 20), all(&e[1], 40)), (vec![120.0], vec![50.0], vec![30.0]), "{version:?}");
|
||||
assert_eq!((all(&e[1], 50), all(&e[1], 51)), (vec![0.0], vec![90.0]), "{version:?}");
|
||||
}
|
||||
}
|
||||
|
||||
// An actual ellipse is not close enough to a circle and remains a spline.
|
||||
let ellipse = doc_with(|d| vec![path(d, shapes::ellipse(Rect::new(20.0, 20.0, 100.0, 60.0)), Paint::None, Paint::solid(Color::BLACK), 1.0)]);
|
||||
assert_eq!(kinds(&export(&ellipse, &opts(&ellipse)).unwrap()), ["SPLINE"]);
|
||||
/// The path `p` with `xf` applied.
|
||||
fn transformed(p: &PathData, xf: Affine) -> PathData {
|
||||
PathData::from_bezpath(&(xf * p.to_bezpath()))
|
||||
}
|
||||
|
||||
fn close_to(a: &[f64], b: &[f64], tol: f64) -> bool {
|
||||
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() <= tol)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ellipses_and_unevenly_scaled_circles_are_native_ellipses() {
|
||||
let circle = shapes::ellipse(Rect::new(20.0, 20.0, 80.0, 80.0));
|
||||
let d = doc_with(|d| {
|
||||
let stroke = || Paint::solid(Color::BLACK);
|
||||
vec![
|
||||
// 80 × 40 pt about (60, 40): DXF center (60, 60), major axis (40, 0), ratio 0.5.
|
||||
path(d, shapes::ellipse(Rect::new(20.0, 20.0, 100.0, 60.0)), Paint::None, stroke(), 1.0),
|
||||
// A 60 pt circle scaled 150% across about (50, 50): 90 × 60 pt, ratio 2/3.
|
||||
path(
|
||||
d,
|
||||
transformed(&circle, Affine::translate((50.0, 50.0)) * Affine::scale_non_uniform(1.5, 1.0) * Affine::translate((-50.0, -50.0))),
|
||||
Paint::None,
|
||||
stroke(),
|
||||
1.0,
|
||||
),
|
||||
// The first ellipse turned 30° about its center, filled: the outline is the ellipse too.
|
||||
path(
|
||||
d,
|
||||
transformed(&shapes::ellipse(Rect::new(20.0, 20.0, 100.0, 60.0)), Affine::rotate_about(30f64.to_radians(), Point::new(60.0, 40.0))),
|
||||
Paint::solid(Color::BLACK),
|
||||
Paint::None,
|
||||
0.0,
|
||||
),
|
||||
]
|
||||
});
|
||||
for version in DxfVersion::ALL.into_iter().filter(|version| *version >= DxfVersion::R13) {
|
||||
let out = export(&d, &DxfOptions { version, ..opts(&d) }).unwrap();
|
||||
let e = entities(&out);
|
||||
assert_eq!(e.iter().map(|x| kind(x)).collect::<Vec<_>>(), ["ELLIPSE", "ELLIPSE", "ELLIPSE", "HATCH"], "{version:?}");
|
||||
let ellipse = |x: &Pairs| (all(x, 10), all(x, 20), all(x, 11), all(x, 21), all(x, 40), all(x, 41), all(x, 42));
|
||||
let (cx, cy, mx, my, ratio, start, end) = ellipse(&e[0]);
|
||||
assert_eq!((cx, cy, mx, my, ratio), (vec![60.0], vec![60.0], vec![40.0], vec![0.0], vec![0.5]), "{version:?}");
|
||||
assert!(close_to(&start, &[0.0], 1e-9) && close_to(&end, &[std::f64::consts::TAU], 1e-12), "{version:?}: a whole turn");
|
||||
let (cx, cy, mx, my, ratio, ..) = ellipse(&e[1]);
|
||||
assert!(close_to(&[cx, cy, mx, my, ratio].concat(), &[50.0, 50.0, 45.0, 0.0, 2.0 / 3.0], 1e-6), "{version:?}: {:?}", e[1]);
|
||||
// Turned 30° counterclockwise on screen, so clockwise with DXF's y up.
|
||||
let (cx, cy, mx, my, ratio, ..) = ellipse(&e[2]);
|
||||
let (cos, sin) = (30f64.to_radians().cos(), 30f64.to_radians().sin());
|
||||
assert!(close_to(&[cx, cy, mx, my, ratio].concat(), &[60.0, 60.0, 40.0 * cos, -40.0 * sin, 0.5], 1e-6), "{version:?}: {:?}", e[2]);
|
||||
assert!(e[0].contains(&(100, "AcDbEllipse".into())), "{version:?}");
|
||||
}
|
||||
// R12 has no ELLIPSE: the curves are flattened polylines.
|
||||
let r12 = kinds(&export(&d, &DxfOptions { version: DxfVersion::R12, ..opts(&d) }).unwrap());
|
||||
assert!(!r12.iter().any(|k| k == "ELLIPSE") && r12.iter().any(|k| k == "POLYLINE"), "{r12:?}");
|
||||
// A thin ellipse keeps its ratio in full (six decimals would write 0.000002).
|
||||
let thin = doc_with(|d| vec![path(d, shapes::ellipse(Rect::new(0.0, 50.0, 200.0, 50.0003)), Paint::None, Paint::solid(Color::BLACK), 1.0)]);
|
||||
let e = entities(&export(&thin, &opts(&thin)).unwrap());
|
||||
assert_eq!(kind(&e[0]), "ELLIPSE");
|
||||
assert!(close_to(&all(&e[0], 40), &[1.5e-6], 1e-10), "{:?}", e[0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exported_ellipses_read_back_as_the_same_ellipse() {
|
||||
let xf = Affine::rotate_about(30f64.to_radians(), Point::new(60.0, 40.0));
|
||||
let d = doc_with(|d| {
|
||||
vec![path(d, transformed(&shapes::ellipse(Rect::new(20.0, 20.0, 100.0, 60.0)), xf), Paint::None, Paint::solid(Color::BLACK), 1.0)]
|
||||
});
|
||||
let out = export(&d, &opts(&d)).unwrap();
|
||||
assert_eq!(kinds(&out), ["ELLIPSE"]);
|
||||
let o = ImportOptions { unit: Some(Unit::Points), scale: Some(1.0), center: false, ..ImportOptions::default() };
|
||||
let back = import(&out.bytes, &o).unwrap().document;
|
||||
let mut paths = vec![];
|
||||
back.walk(|n| {
|
||||
if let NodeKind::Path { path, .. } = &n.kind {
|
||||
paths.push(path.to_bezpath());
|
||||
}
|
||||
});
|
||||
assert_eq!(paths.len(), 1);
|
||||
assert!(paths[0].elements().last().is_some_and(|el| matches!(el, kurbo::PathEl::ClosePath)), "the ellipse reads back closed");
|
||||
// Every point of the curve read back lies on the 80 × 40 pt ellipse turned 30°, once both are centered.
|
||||
let bp = &paths[0];
|
||||
let found = bp.bounding_box().center();
|
||||
let inverse = xf.inverse();
|
||||
let original = Point::new(60.0, 40.0);
|
||||
for seg in bp.segments() {
|
||||
for i in 0..=16 {
|
||||
let p = seg.eval(f64::from(i) / 16.0) - found.to_vec2() + original.to_vec2();
|
||||
let q = inverse * p;
|
||||
let r = ((q.x - 60.0) / 40.0).hypot((q.y - 40.0) / 20.0);
|
||||
assert!((r - 1.0).abs() < 1e-3, "{p:?} is off the ellipse ({r})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closed_four_cubic_shapes_that_are_not_writable_ellipses_stay_splines() {
|
||||
// A rounded square: four cubics through the ellipse's anchors with longer handles.
|
||||
let (c, r, k) = (Point::new(50.0, 50.0), 30.0, 30.0 * 0.8);
|
||||
let mut squircle = vectorcraft_geom::BezPath::new();
|
||||
squircle.move_to(Point::new(c.x + r, c.y));
|
||||
squircle.curve_to(Point::new(c.x + r, c.y - k), Point::new(c.x + k, c.y - r), Point::new(c.x, c.y - r));
|
||||
squircle.curve_to(Point::new(c.x - k, c.y - r), Point::new(c.x - r, c.y - k), Point::new(c.x - r, c.y));
|
||||
squircle.curve_to(Point::new(c.x - r, c.y + k), Point::new(c.x - k, c.y + r), Point::new(c.x, c.y + r));
|
||||
squircle.curve_to(Point::new(c.x + k, c.y + r), Point::new(c.x + r, c.y + k), Point::new(c.x + r, c.y));
|
||||
squircle.close_path();
|
||||
// An egg: one half wider than the other.
|
||||
let egg = shapes::ellipse(Rect::new(110.0, 20.0, 190.0, 60.0)).to_bezpath();
|
||||
let mut els: Vec<kurbo::PathEl> = egg.elements().to_vec();
|
||||
if let Some(kurbo::PathEl::CurveTo(a, b, _)) = els.get_mut(1) {
|
||||
a.x -= 10.0;
|
||||
b.x -= 10.0;
|
||||
}
|
||||
let d = doc_with(|d| {
|
||||
vec![
|
||||
path(d, PathData::from_bezpath(&squircle), Paint::None, Paint::solid(Color::BLACK), 1.0),
|
||||
path(d, PathData::from_bezpath(&kurbo::BezPath::from_vec(els)), Paint::None, Paint::solid(Color::BLACK), 1.0),
|
||||
// An ellipse thinner than the smallest ratio DXF readers take (5e-7 against 1e-6).
|
||||
path(d, shapes::ellipse(Rect::new(0.0, 90.0, 200.0, 90.0001)), Paint::None, Paint::solid(Color::BLACK), 1.0),
|
||||
]
|
||||
});
|
||||
assert_eq!(kinds(&export(&d, &opts(&d)).unwrap()), ["SPLINE", "SPLINE", "SPLINE"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lone_straight_segment_is_a_line() {
|
||||
let segment = |closed: bool, extra: Option<Point>| {
|
||||
let mut b = vectorcraft_geom::BezPath::new();
|
||||
b.move_to(Point::new(20.0, 30.0));
|
||||
b.line_to(Point::new(120.0, 70.0));
|
||||
if let Some(p) = extra {
|
||||
b.line_to(p);
|
||||
}
|
||||
if closed {
|
||||
b.close_path();
|
||||
}
|
||||
PathData::from_bezpath(&b)
|
||||
};
|
||||
let d = doc_with(|d| vec![path(d, segment(false, None), Paint::None, Paint::solid(Color::BLACK), 1.0)]);
|
||||
for version in DxfVersion::ALL.into_iter().filter(|v| v.r2000()) {
|
||||
let e = entities(&export(&d, &DxfOptions { version, ..opts(&d) }).unwrap());
|
||||
assert_eq!(e.iter().map(|x| kind(x)).collect::<Vec<_>>(), ["LINE"], "{version:?}");
|
||||
assert_eq!(e[0].iter().rfind(|p| p.0 == 100).map(|p| p.1.as_str()), Some("AcDbLine"), "{version:?}");
|
||||
assert_eq!((all(&e[0], 10), all(&e[0], 20)), (vec![20.0], vec![70.0]), "{version:?}: y up from the artboard's bottom");
|
||||
assert_eq!((all(&e[0], 11), all(&e[0], 21)), (vec![120.0], vec![30.0]), "{version:?}");
|
||||
}
|
||||
|
||||
// Without a width (R12 writes a fill as its outline), R12 writes a LINE too; a stroke weight
|
||||
// before 2000 keeps its polyline width.
|
||||
let filled = doc_with(|d| vec![path(d, segment(false, None), Paint::solid(Color::BLACK), Paint::None, 0.0)]);
|
||||
let r12 = |d: &Document| kinds(&export(d, &DxfOptions { version: DxfVersion::R12, ..opts(d) }).unwrap());
|
||||
assert_eq!(r12(&filled), ["LINE"]);
|
||||
assert_eq!(r12(&d), ["POLYLINE", "VERTEX", "VERTEX", "SEQEND"]);
|
||||
let r14 = entities(&export(&d, &DxfOptions { version: DxfVersion::R14, ..opts(&d) }).unwrap());
|
||||
assert_eq!((kind(&r14[0]), get(&r14[0], 43)), ("LWPOLYLINE", Some("1")));
|
||||
|
||||
// A closed segment and two segments stay polylines.
|
||||
for p in [segment(true, None), segment(false, Some(Point::new(150.0, 20.0)))] {
|
||||
let d = doc_with(|d| vec![path(d, p, Paint::None, Paint::solid(Color::BLACK), 1.0)]);
|
||||
assert_eq!(kinds(&export(&d, &opts(&d)).unwrap()), ["LWPOLYLINE"]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -453,14 +676,44 @@ fn dashes_become_linetypes_and_opacity_transparency() {
|
||||
|
||||
#[test]
|
||||
fn alter_paths_writes_strokes_as_filled_outlines() {
|
||||
let d = shapes_doc();
|
||||
let out = export(&d, &DxfOptions { alter_paths: true, ..opts(&d) }).unwrap();
|
||||
// Each fill and stroke: its outlines, then its hatch.
|
||||
let fill = ["LWPOLYLINE", "HATCH"];
|
||||
let rect_stroke = ["LWPOLYLINE", "LWPOLYLINE", "HATCH"];
|
||||
// Strokes a lineweight draws stay lines: the rectangle, the circle and an open arc.
|
||||
let mut d = shapes_doc();
|
||||
let c = Point::new(150.0, 95.0);
|
||||
let k = 20.0 * shapes::KAPPA;
|
||||
let mut arc = vectorcraft_geom::BezPath::new();
|
||||
arc.move_to(Point::new(c.x + 20.0, c.y));
|
||||
arc.curve_to(Point::new(c.x + 20.0, c.y - k), Point::new(c.x + k, c.y - 20.0), Point::new(c.x, c.y - 20.0));
|
||||
let arc = path(&mut d, PathData::from_bezpath(&arc), Paint::None, Paint::solid(Color::BLACK), 1.0);
|
||||
let layer = d.layers[0].id;
|
||||
d.insert(Some(layer), usize::MAX, arc).unwrap();
|
||||
let out = export(&d, &DxfOptions { alter_paths: true, ..opts(&d) }).unwrap();
|
||||
assert_eq!(kinds(&out), [&fill[..], &["LWPOLYLINE", "CIRCLE", "ARC"]].concat());
|
||||
assert!(out.warnings.is_empty(), "{:?}", out.warnings);
|
||||
// R12 polylines carry any width, so its strokes stay lines too.
|
||||
let r12 = export(&d, &DxfOptions { alter_paths: true, version: DxfVersion::R12, ..opts(&d) }).unwrap();
|
||||
assert!(!kinds(&r12).iter().any(|k| k == "HATCH"));
|
||||
assert!(entities(&r12).iter().any(|e| kind(e) == "POLYLINE" && get(e, 40) == Some("2")));
|
||||
// R13 and R14 have no lineweights: every stroke becomes its outline, then its hatch.
|
||||
let d = shapes_doc();
|
||||
let out = export(&d, &DxfOptions { alter_paths: true, version: DxfVersion::R14, ..opts(&d) }).unwrap();
|
||||
assert_eq!(kinds(&out), [&fill[..], &rect_stroke, &["CIRCLE", "CIRCLE", "HATCH"]].concat());
|
||||
// The rectangle's stroke: an outer and an inner boundary.
|
||||
assert_eq!(get(&entities(&out)[4], 91), Some("2"));
|
||||
// A stroke wider than the widest lineweight (2.11 mm) is outlined at its true width.
|
||||
let wide = doc_with(|d| vec![path(d, shapes::rectangle(Rect::new(10.0, 10.0, 110.0, 60.0)), Paint::None, Paint::solid(Color::BLACK), 8.0)]);
|
||||
let out = export(&wide, &DxfOptions { alter_paths: true, ..opts(&wide) }).unwrap();
|
||||
assert_eq!(kinds(&out), rect_stroke);
|
||||
assert!(out.warnings.is_empty(), "{:?}", out.warnings);
|
||||
let lines = export(&wide, &opts(&wide)).unwrap();
|
||||
assert_eq!(kinds(&lines), ["LWPOLYLINE"]);
|
||||
assert!(lines.warnings.iter().any(|w| w.contains("2.11 mm")));
|
||||
// To the hundredth of a millimetre: 6 pt (2.117 mm) is outlined, 5.98 pt (2.110 mm) is a line.
|
||||
for (width, expected) in [(6.0, &rect_stroke[..]), (5.98, &["LWPOLYLINE"][..])] {
|
||||
let d = doc_with(|d| vec![path(d, shapes::rectangle(Rect::new(10.0, 10.0, 110.0, 60.0)), Paint::None, Paint::solid(Color::BLACK), width)]);
|
||||
assert_eq!(kinds(&export(&d, &DxfOptions { alter_paths: true, ..opts(&d) }).unwrap()), expected, "{width} pt");
|
||||
}
|
||||
// Preserve Appearance outlines only the strokes a line can't draw.
|
||||
let mut d = shapes_doc();
|
||||
let rect = d.layers[0].children().and_then(|c| c.first()).map(|n| n.id).unwrap();
|
||||
@@ -471,6 +724,65 @@ fn alter_paths_writes_strokes_as_filled_outlines() {
|
||||
let editable = export(&d, &DxfOptions { preserve: Preserve::Editability, ..opts(&d) }).unwrap();
|
||||
assert_eq!(kinds(&editable), ["LWPOLYLINE", "HATCH", "LWPOLYLINE", "CIRCLE"]);
|
||||
assert!(editable.warnings.iter().any(|w| w.contains("stroke alignment")));
|
||||
// With Maximize Editability, Alter Paths outlines that stroke and keeps the circle a line.
|
||||
let altered = export(&d, &DxfOptions { preserve: Preserve::Editability, alter_paths: true, ..opts(&d) }).unwrap();
|
||||
assert_eq!(kinds(&altered), [&fill[..], &rect_stroke, &["CIRCLE"]].concat());
|
||||
assert!(altered.warnings.is_empty(), "{:?}", altered.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dashes_that_start_part_way_into_their_pattern_are_outlined() {
|
||||
let dashed = |offset: f64| {
|
||||
doc_with(|d| {
|
||||
let mut n = path(d, shapes::rectangle(Rect::new(10.0, 10.0, 110.0, 60.0)), Paint::None, Paint::solid(Color::BLACK), 1.0);
|
||||
if let Some(st) = n.appearance.stroke_mut() {
|
||||
st.dash = Some(Dash { pattern: vec![6.0, 3.0, 2.0], offset, align_corners: false });
|
||||
}
|
||||
vec![n]
|
||||
})
|
||||
};
|
||||
// A linetype starts at the start of its pattern: an offset of a whole period (the odd
|
||||
// pattern twice) keeps the line, any other offset needs the dashes' outlines and a hatch.
|
||||
for (offset, outlined) in [(0.0, false), (22.0, false), (4.0, true), (11.0, true)] {
|
||||
let d = dashed(offset);
|
||||
let k = kinds(&export(&d, &opts(&d)).unwrap());
|
||||
if outlined {
|
||||
assert!(k.len() > 2 && k.last().is_some_and(|k| k == "HATCH"), "offset {offset}: {k:?}");
|
||||
} else {
|
||||
assert_eq!(k, ["LWPOLYLINE"], "offset {offset}");
|
||||
}
|
||||
let altered = kinds(&export(&d, &DxfOptions { alter_paths: true, ..opts(&d) }).unwrap());
|
||||
assert_eq!(altered, k, "offset {offset}, Alter Paths");
|
||||
}
|
||||
// Maximize Editability keeps the line and its linetype.
|
||||
let d = dashed(4.0);
|
||||
let editable = export(&d, &DxfOptions { preserve: Preserve::Editability, ..opts(&d) }).unwrap();
|
||||
assert_eq!(kinds(&editable), ["LWPOLYLINE"]);
|
||||
assert_eq!(get(&entities(&editable)[0], 6), Some("DASHED1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dashed_polylines_continue_their_pattern_around_corners() {
|
||||
let rect = |dash: Option<Dash>| {
|
||||
doc_with(|d| {
|
||||
let mut n = path(d, shapes::rectangle(Rect::new(10.0, 10.0, 110.0, 60.0)), Paint::None, Paint::solid(Color::BLACK), 1.0);
|
||||
if let Some(st) = n.appearance.stroke_mut() {
|
||||
st.dash = dash.clone();
|
||||
}
|
||||
vec![n]
|
||||
})
|
||||
};
|
||||
let flags = |out: &DxfOutput| entities(out).first().and_then(|e| get(e, 70).map(str::to_string));
|
||||
// Group 70 bit 128 makes the linetype run on through the vertices instead of restarting
|
||||
// at each one; a solid polyline keeps only its closed bit.
|
||||
let dashed = rect(Some(Dash { pattern: vec![6.0, 3.0], offset: 0.0, align_corners: false }));
|
||||
for o in [opts(&dashed), DxfOptions { alter_paths: true, ..opts(&dashed) }, DxfOptions { preserve: Preserve::Editability, ..opts(&dashed) }] {
|
||||
assert_eq!(flags(&export(&dashed, &o).unwrap()).as_deref(), Some("129"));
|
||||
}
|
||||
let r12 = export(&dashed, &DxfOptions { version: DxfVersion::R12, ..opts(&dashed) }).unwrap();
|
||||
assert_eq!((kinds(&r12).first().map(String::as_str), flags(&r12).as_deref()), (Some("POLYLINE"), Some("129")));
|
||||
let solid = rect(None);
|
||||
assert_eq!(flags(&export(&solid, &opts(&solid)).unwrap()).as_deref(), Some("1"));
|
||||
}
|
||||
|
||||
fn text_doc(text: &str, size: f64) -> Document {
|
||||
@@ -594,6 +906,23 @@ fn text_outside_ascii_is_escaped_before_2007() {
|
||||
assert_eq!(text(DxfVersion::R2018), "Größe");
|
||||
}
|
||||
|
||||
/// Type whose font size measures the cap height is written at the em size it is drawn at: 14 pt
|
||||
/// measuring a 0.7 em cap height is 20 pt type, whose TEXT height is 14.
|
||||
#[test]
|
||||
fn a_font_height_reference_is_written_as_the_size_drawn() {
|
||||
use vectorcraft_text::test_fonts::{HEIGHT_FAMILY, height_font};
|
||||
vectorcraft_text::FontDb::global().add_font(height_font(true).unwrap());
|
||||
let mut d = text_doc("Plan A", 14.0);
|
||||
let id = d.layers[0].children().and_then(|c| c.first()).map(|n| n.id).unwrap();
|
||||
if let Some(NodeKind::Text(t)) = d.node_mut(id).map(|n| &mut n.kind) {
|
||||
t.runs[0].style.font_family = HEIGHT_FAMILY.into();
|
||||
t.runs[0].style.font_height_reference = vectorcraft_doc::FontHeightReference::CapHeight;
|
||||
}
|
||||
let editable = export(&d, &DxfOptions { preserve: Preserve::Editability, ..opts(&d) }).unwrap();
|
||||
let e = entities(&editable);
|
||||
assert!((all(&e[0], 40)[0] - 14.0).abs() < 1e-6, "{:?}", all(&e[0], 40));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn images_link_png_or_jpeg_files() {
|
||||
let mut png = vec![];
|
||||
@@ -674,7 +1003,7 @@ fn crop_keeps_an_instance_whose_art_reaches_the_region() {
|
||||
// while the 20 × 20 square around the origin, moved to (-60, -60) to (-40, -40), is off it.
|
||||
let mut d = doc_with(|d| vec![Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Far".into(), xf: Affine::translate((-50.0, -50.0)) })]);
|
||||
let art = path(&mut d, shapes::rectangle(Rect::new(100.0, 50.0, 140.0, 90.0)), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Far".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Far".into(), art: Arc::new(art) });
|
||||
let o = DxfOptions { crop: true, ..opts(&d) };
|
||||
assert!(!kinds(&export(&d, &o).unwrap()).is_empty());
|
||||
}
|
||||
@@ -564,7 +564,10 @@ impl Drawing {
|
||||
c.handle(350, fixed::PLOT_NORMAL);
|
||||
c.raw(100, "AcDbDictionaryWithDefault");
|
||||
c.handle(340, fixed::PLOT_NORMAL);
|
||||
c.object("ACDBPLACEHOLDER", fixed::PLOT_NORMAL, fixed::PLOT_STYLES, "AcDbPlaceHolder");
|
||||
// A placeholder has no data and no subclass marker of its own.
|
||||
c.raw(0, "ACDBPLACEHOLDER");
|
||||
c.handle(5, fixed::PLOT_NORMAL);
|
||||
c.handle(330, fixed::PLOT_STYLES);
|
||||
self.layout(c, fixed::MODEL_LAYOUT, "Model", 0, fixed::MODEL_RECORD);
|
||||
self.layout(c, fixed::PAPER_LAYOUT, "Layout1", 1, fixed::PAPER_RECORD);
|
||||
}
|
||||
@@ -618,14 +621,23 @@ impl Drawing {
|
||||
c.num(141, 0.0);
|
||||
c.num(142, 1.0);
|
||||
c.num(143, 1.0);
|
||||
c.int(70, 688);
|
||||
// Plot flags; 1024 marks the model space layout.
|
||||
c.int(70, if record == fixed::MODEL_RECORD { 688 | 1024 } else { 688 });
|
||||
c.int(72, 1);
|
||||
c.int(73, 0);
|
||||
c.int(74, 5);
|
||||
c.raw(7, "");
|
||||
c.int(75, 16);
|
||||
if self.version.true_color() {
|
||||
// Shade plot mode (as displayed), resolution level (normal) and custom DPI.
|
||||
c.int(76, 0);
|
||||
c.int(77, 2);
|
||||
c.int(78, 300);
|
||||
}
|
||||
c.num(147, 1.0);
|
||||
c.xy(148, 0.0, 0.0);
|
||||
// The paper image origin is 148 (x) and 149 (y), not a point at 148 and 158.
|
||||
c.num(148, 0.0);
|
||||
c.num(149, 0.0);
|
||||
c.raw(100, "AcDbLayout");
|
||||
c.raw(1, name);
|
||||
c.int(70, 1);
|
||||
|
||||
@@ -149,11 +149,20 @@ impl IccProfile {
|
||||
|
||||
/// Device values (RGB / CMYK / Gray, 0..1) → sRGB.
|
||||
pub fn to_srgb(&self, v: &[f32], intent: Intent) -> Option<[f32; 3]> {
|
||||
let mut src = [0.0f32; 4];
|
||||
let channels = self.channels();
|
||||
src.get_mut(..channels)?.iter_mut().zip(v.iter()).for_each(|(dst, value)| *dst = *value);
|
||||
self.to_srgb_into(src.get(..channels)?, intent)
|
||||
}
|
||||
|
||||
/// Convert an exact-width device pixel using the cached transform and stack storage.
|
||||
pub fn to_srgb_into(&self, v: &[f32], intent: Intent) -> Option<[f32; 3]> {
|
||||
if v.len() != self.channels() {
|
||||
return None;
|
||||
}
|
||||
let xf = self.to_xf(intent)?;
|
||||
let mut src = v.to_vec();
|
||||
src.resize(self.channels(), 0.0);
|
||||
let mut dst = [0.0f32; 3];
|
||||
xf.transform(&src, &mut dst).ok()?;
|
||||
xf.transform(v, &mut dst).ok()?;
|
||||
Some(dst.map(|x| x.clamp(0.0, 1.0)))
|
||||
}
|
||||
|
||||
|
||||
+112
-14
@@ -78,6 +78,14 @@ const PERCEPTUAL_TOLERANCE: f32 = 0.4 / 255.0;
|
||||
/// Most times each half of a Perceptual gradient's segment (either side of its midpoint) is
|
||||
/// halved to keep within [`PERCEPTUAL_TOLERANCE`].
|
||||
const PERCEPTUAL_DEPTH: u32 = 10;
|
||||
/// How many times [`Gradient::ink_tints`] halves the range of tints it searches: to within
|
||||
/// 1/65536 of a tint, finer than the 1/255 steps PDF writes.
|
||||
const TINT_STEPS: u32 = 16;
|
||||
/// The least OKLab lightness by which an ink at 100% must differ from paper white for
|
||||
/// [`Gradient::ink_tints`] to tell its tints apart by lightness.
|
||||
const MIN_INK_LIGHTNESS_SPAN: f64 = 1.0 / 255.0;
|
||||
/// Most stops on a gradient's curve that [`Gradient::ink_tints`] finds tints for.
|
||||
const MAX_INK_TINTS: usize = 4096;
|
||||
|
||||
/// The mix positions (0..1, after `v0`, before `v1`) between which display RGB mixed linearly
|
||||
/// stays within [`PERCEPTUAL_TOLERANCE`] of the perceptual mix of `a` and `b`: the span halved
|
||||
@@ -100,6 +108,30 @@ fn perceptual_steps(a: &Color, b: &Color, (v0, v1): (f32, f32), depth: u32, out:
|
||||
}
|
||||
}
|
||||
|
||||
/// The mix positions (`v`, 0..1) of the stops a Perceptual gradient's curve has between stops `a`
|
||||
/// and `b`, in order: [`perceptual_steps`] on either side of the midpoint, and the midpoint (½).
|
||||
fn perceptual_mixes(a: &GradientStop, b: &GradientStop) -> Vec<f32> {
|
||||
let mut vs = Vec::new();
|
||||
perceptual_steps(&a.color, &b.color, (0.0, 0.5), 1, &mut vs);
|
||||
vs.push(0.5);
|
||||
perceptual_steps(&a.color, &b.color, (0.5, 1.0), 1, &mut vs);
|
||||
vs
|
||||
}
|
||||
|
||||
/// The offset of mix position `v` between stops `a` and `b`, placed through `a`'s midpoint
|
||||
/// (`v` = ½ at the midpoint) as [`Gradient::sample`] maps it.
|
||||
fn curve_offset(a: &GradientStop, b: &GradientStop, v: f32) -> f32 {
|
||||
let m = a.midpoint.clamp(0.01, 0.99);
|
||||
let u = if v == 0.5 {
|
||||
a.midpoint
|
||||
} else if v < 0.5 {
|
||||
2.0 * v * m
|
||||
} else {
|
||||
m + (v - 0.5) * 2.0 * (1.0 - m)
|
||||
};
|
||||
a.offset + (b.offset - a.offset) * u
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct GradientStop {
|
||||
/// 0..=1 along the gradient.
|
||||
@@ -262,21 +294,9 @@ impl Gradient {
|
||||
// Perceptual: stops along the mix (`v`) where a straight line would stray from it, the
|
||||
// midpoint (v = ½) among them, placed through the midpoint as [`Self::sample`] maps them.
|
||||
let perceptual = next.filter(|_| !self.method.is_classic()).map(|n| {
|
||||
let mut vs = Vec::new();
|
||||
perceptual_steps(&s.color, &n.color, (0.0, 0.5), 1, &mut vs);
|
||||
vs.push(0.5);
|
||||
perceptual_steps(&s.color, &n.color, (0.5, 1.0), 1, &mut vs);
|
||||
let m = s.midpoint.clamp(0.01, 0.99);
|
||||
vs.into_iter().map(move |v| {
|
||||
perceptual_mixes(s, n).into_iter().map(move |v| {
|
||||
let mid = v == 0.5;
|
||||
let u = if mid {
|
||||
s.midpoint
|
||||
} else if v < 0.5 {
|
||||
2.0 * v * m
|
||||
} else {
|
||||
m + (v - 0.5) * 2.0 * (1.0 - m)
|
||||
};
|
||||
let t = s.offset + (n.offset - s.offset) * u;
|
||||
let t = curve_offset(s, n, v);
|
||||
let o = s.opacity + (n.opacity - s.opacity) * v;
|
||||
(t, self.method.mix(&s.color, &n.color, v), o, (mid && off_centre).then_some(s.midpoint))
|
||||
})
|
||||
@@ -284,6 +304,84 @@ impl Gradient {
|
||||
std::iter::once((s.offset, s.color, s.opacity, None)).chain(classic).chain(perceptual.into_iter().flatten())
|
||||
})
|
||||
}
|
||||
/// A gradient of tints of one ink `ink` (its color at 100%; paper white is 0% of it) as
|
||||
/// (offset, tint, opacity) at each of [`Self::expanded_stops`]: the tint (0..1) whose OKLab
|
||||
/// lightness is the lightness of the color there, found to [`TINT_STEPS`] halvings. Mixed
|
||||
/// linearly between them, the tints follow a Perceptual gradient's curve. `None` when the
|
||||
/// ink's lightness is within [`MIN_INK_LIGHTNESS_SPAN`] of paper white's (a white or nearly
|
||||
/// white ink), so lightness does not tell its tints apart, and when the curve has more than
|
||||
/// [`MAX_INK_TINTS`] stops ([`Self::fits_ink_tints`]).
|
||||
pub fn ink_tints(&self, ink: &Color) -> Option<Vec<(f32, f32, f32)>> {
|
||||
use crate::libraries::srgb_to_oklab;
|
||||
let lightness = |c: &Color| srgb_to_oklab(c.to_rgb())[0];
|
||||
if lightness(&ink.tinted(0.0)) - lightness(ink) < MIN_INK_LIGHTNESS_SPAN {
|
||||
return None;
|
||||
}
|
||||
let curve: Vec<_> = self.expanded().take(MAX_INK_TINTS + 1).collect();
|
||||
if curve.len() > MAX_INK_TINTS {
|
||||
return None;
|
||||
}
|
||||
let tint = |c: &Color| {
|
||||
let want = lightness(c);
|
||||
let (mut lo, mut hi) = (0.0f32, 1.0f32);
|
||||
for _ in 0..TINT_STEPS {
|
||||
let mid = (lo + hi) / 2.0;
|
||||
if lightness(&ink.tinted(mid)) > want {
|
||||
lo = mid;
|
||||
} else {
|
||||
hi = mid;
|
||||
}
|
||||
}
|
||||
(lo + hi) / 2.0
|
||||
};
|
||||
Some(curve.into_iter().map(|(t, c, o, _)| (t, tint(&c), o)).collect())
|
||||
}
|
||||
/// Does the gradient's curve ([`Self::expanded`]) have at most [`MAX_INK_TINTS`] stops, so
|
||||
/// [`Self::ink_tints`] finds tints for it?
|
||||
pub fn fits_ink_tints(&self) -> bool {
|
||||
self.expanded().nth(MAX_INK_TINTS).is_none()
|
||||
}
|
||||
/// A Perceptual gradient's curve ([`Self::expanded_stops`]) as mixes of its stops' inks:
|
||||
/// (offset, `i`, weight, opacity) at each stop on it, where the inks there are those of stop
|
||||
/// `i` and stop `i + 1` mixed linearly at the weight (0..1; 0 on the gradient's own stops).
|
||||
/// The weight is the one at which the two stops' colors mixed in display RGB have the OKLab
|
||||
/// lightness of the color on the curve there, found to [`TINT_STEPS`] halvings, or the mix
|
||||
/// position itself when the two colors' lightness differs by less than
|
||||
/// [`MIN_INK_LIGHTNESS_SPAN`]. `None` for a Classic gradient, and when the curve has more than
|
||||
/// [`MAX_INK_TINTS`] stops ([`Self::fits_ink_tints`]).
|
||||
pub fn ink_mixes(&self) -> Option<Vec<(f32, usize, f32, f32)>> {
|
||||
use crate::libraries::srgb_to_oklab;
|
||||
if self.method.is_classic() || !self.fits_ink_tints() {
|
||||
return None;
|
||||
}
|
||||
let lightness = |c: &Color| srgb_to_oklab(c.to_rgb())[0];
|
||||
let mut out = Vec::new();
|
||||
for (i, s) in self.stops.iter().enumerate() {
|
||||
out.push((s.offset, i, 0.0, s.opacity));
|
||||
let Some(n) = self.stops.get(i + 1) else { continue };
|
||||
let (l0, l1) = (lightness(&s.color), lightness(&n.color));
|
||||
for v in perceptual_mixes(s, n) {
|
||||
let weight = if (l1 - l0).abs() < MIN_INK_LIGHTNESS_SPAN {
|
||||
v
|
||||
} else {
|
||||
let want = lightness(&self.method.mix(&s.color, &n.color, v));
|
||||
let (mut lo, mut hi) = (0.0f32, 1.0f32);
|
||||
for _ in 0..TINT_STEPS {
|
||||
let mid = (lo + hi) / 2.0;
|
||||
// The mix at `mid` has not yet gone from stop `i`'s lightness to the wanted one.
|
||||
if (lightness(&GradientMethod::Classic.mix(&s.color, &n.color, mid)) - want) * (l1 - l0) < 0.0 {
|
||||
lo = mid;
|
||||
} else {
|
||||
hi = mid;
|
||||
}
|
||||
}
|
||||
(lo + hi) / 2.0
|
||||
};
|
||||
out.push((curve_offset(s, n, v), i, weight, s.opacity + (n.opacity - s.opacity) * v));
|
||||
}
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
/// 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) {
|
||||
|
||||
@@ -455,3 +455,71 @@ fn perceptual_gradients_draw_as_pixels() {
|
||||
assert!(GradientPaint::new(radial).draws_as_pixels(fill));
|
||||
}
|
||||
}
|
||||
|
||||
/// A Perceptual gradient of an ink's tints as tints: its stops keep their tints, and each tint on
|
||||
/// the curve has the lightness of the color mixed there, so the tints between the stops are not
|
||||
/// a straight line.
|
||||
#[test]
|
||||
fn ink_tints_follow_the_perceptual_curve() {
|
||||
use crate::libraries::srgb_to_oklab;
|
||||
let ink = Color::cmyk(0.0, 1.0, 0.0, 0.0);
|
||||
let mut g = Gradient::new(GradientKind::Linear, vec![GradientStop::new(0.0, ink.tinted(0.8)), GradientStop::new(1.0, Color::WHITE)]);
|
||||
g.method = GradientMethod::Perceptual;
|
||||
assert!(g.fits_ink_tints());
|
||||
let tints = g.ink_tints(&ink).expect("a magenta ink's tints differ in lightness");
|
||||
assert!(tints.len() > 2, "the curve's stops: {tints:?}");
|
||||
let (first, last) = (tints[0], tints[tints.len() - 1]);
|
||||
assert!((first.0, last.0) == (0.0, 1.0) && (first.1 - 0.8).abs() < 1e-4 && last.1 < 1e-4, "{tints:?}");
|
||||
assert!(tints.windows(2).all(|w| w[0].0 <= w[1].0 && w[0].1 >= w[1].1), "in order, lighter along the way: {tints:?}");
|
||||
let lightness = |c: &Color| srgb_to_oklab(c.to_rgb())[0];
|
||||
for (t, tint, _) in &tints {
|
||||
let want = lightness(&g.sample(*t).0);
|
||||
assert!((lightness(&ink.tinted(*tint)) - want).abs() < 1e-3, "at {t}: tint {tint}");
|
||||
}
|
||||
let mid = tints.iter().find(|s| (s.0 - 0.5).abs() < 1e-6).expect("the midpoint");
|
||||
assert!((mid.1 - 0.4).abs() > 0.01, "not halfway in tint: {mid:?}");
|
||||
// A white ink's tints all have paper white's lightness: no tints by lightness.
|
||||
assert_eq!(g.ink_tints(&Color::WHITE), None);
|
||||
// More stops on the curve than MAX_INK_TINTS: no tints.
|
||||
let n = MAX_INK_TINTS + 1;
|
||||
g.stops = (0..n).map(|i| GradientStop::new(i as f32 / (n - 1) as f32, if i % 2 == 0 { ink } else { Color::WHITE })).collect();
|
||||
assert_eq!(g.ink_tints(&ink), None);
|
||||
assert!(!g.fits_ink_tints());
|
||||
}
|
||||
|
||||
/// A Perceptual gradient's curve as mixes of its stops' inks: the stops on the curve at their
|
||||
/// offsets, each weighted so the two stops' colors mixed in display RGB have the lightness of
|
||||
/// the color on the curve there.
|
||||
#[test]
|
||||
fn ink_mixes_follow_the_perceptual_curve() {
|
||||
use crate::libraries::srgb_to_oklab;
|
||||
let (magenta, yellow) = (Color::cmyk(0.0, 1.0, 0.0, 0.0), Color::cmyk(0.0, 0.0, 0.6, 0.0));
|
||||
let mut g = Gradient::new(GradientKind::Linear, vec![GradientStop::new(0.0, magenta), GradientStop::new(1.0, yellow)]);
|
||||
g.stops[0].midpoint = 0.3;
|
||||
assert_eq!(g.ink_mixes(), None, "a Classic gradient");
|
||||
g.method = GradientMethod::Perceptual;
|
||||
let mixes = g.ink_mixes().expect("a Perceptual gradient's curve");
|
||||
let curve = g.expanded_stops();
|
||||
assert_eq!(mixes.len(), curve.len(), "a mix for each stop on the curve");
|
||||
assert_eq!((mixes[0], mixes[mixes.len() - 1]), ((0.0, 0, 0.0, 1.0), (1.0, 1, 0.0, 1.0)), "the gradient's own stops");
|
||||
let lightness = |c: &Color| srgb_to_oklab(c.to_rgb())[0];
|
||||
for ((t, i, w, _), (ct, c, _)) in mixes.iter().zip(&curve) {
|
||||
assert!((t - ct).abs() < 1e-6, "{t} on the curve at {ct}");
|
||||
if *i == 0 && *t > 0.0 {
|
||||
let mixed = GradientMethod::Classic.mix(&magenta, &yellow, *w);
|
||||
assert!((lightness(&mixed) - lightness(c)).abs() < 1e-3, "at {t}: weight {w}");
|
||||
}
|
||||
}
|
||||
let mid = mixes.iter().find(|m| (m.0 - 0.3).abs() < 1e-6).expect("the midpoint");
|
||||
assert!((mid.2 - 0.5).abs() > 0.01, "not halfway at the midpoint: {mid:?}");
|
||||
assert!(mixes.windows(2).all(|w| w[0].0 <= w[1].0), "in order: {mixes:?}");
|
||||
// Two colors of one lightness: the weights are the mix positions.
|
||||
g.stops[1].color = magenta;
|
||||
let mixes = g.ink_mixes().expect("the curve");
|
||||
let mid = mixes.iter().find(|m| (m.0 - 0.3).abs() < 1e-6).expect("the midpoint");
|
||||
assert!((mid.2 - 0.5).abs() < 1e-6, "{mid:?}");
|
||||
// More stops on the curve than MAX_INK_TINTS: no mixes.
|
||||
let n = MAX_INK_TINTS + 1;
|
||||
g.stops = (0..n).map(|i| GradientStop::new(i as f32 / (n - 1) as f32, if i % 2 == 0 { magenta } else { Color::WHITE })).collect();
|
||||
assert_eq!(g.ink_mixes(), None);
|
||||
}
|
||||
@@ -53,6 +53,15 @@ impl WidthProfile {
|
||||
// Two swells between narrow necks.
|
||||
ProfilePreset { id: "wave", label: "Wave", points: &[(0.0, 0.3, 0.3), (0.25, 1.0, 1.0), (0.5, 0.3, 0.3), (0.75, 1.0, 1.0), (1.0, 0.3, 0.3)] },
|
||||
];
|
||||
/// The widest the stroke gets, as a multiple of its weight: the largest total of the left and
|
||||
/// right factors at any of the points (between them the profile is straight), 1 for a profile
|
||||
/// without points.
|
||||
pub fn widest(&self) -> f64 {
|
||||
if self.points.is_empty() {
|
||||
return 1.0;
|
||||
}
|
||||
self.points.iter().map(|&(_, l, r)| (l.max(0.0) + r.max(0.0)) / 2.0).fold(0.0, f64::max)
|
||||
}
|
||||
/// The built-in profile with this id.
|
||||
pub fn preset(id: &str) -> Option<Self> {
|
||||
Self::PRESETS.iter().find(|p| p.id == id).map(|p| Self { points: p.points.to_vec() })
|
||||
|
||||
@@ -35,8 +35,8 @@ pub struct Artboard {
|
||||
pub video_ruler_pixel_aspect_ratio: f64,
|
||||
/// Background color (Properties › Artboards › Fill, Artboard Options). The canvas draws the
|
||||
/// artboard's paper in it, multiplied with the Simulate Colored Paper color while that is on;
|
||||
/// without one the paper is white, or the Simulate Colored Paper color. Exports and print leave
|
||||
/// it out.
|
||||
/// without one the paper is white, or the Simulate Colored Paper color. Exports and print draw
|
||||
/// it behind the art ([`super::Document::with_artboard_fills`]).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub color: Option<vectorcraft_color::Color>,
|
||||
}
|
||||
@@ -77,6 +77,37 @@ impl Artboard {
|
||||
}
|
||||
}
|
||||
|
||||
impl super::Document {
|
||||
/// A copy for output with the background colors ([`Artboard::color`]) of the artboards at
|
||||
/// `indices` drawn behind all the art: each a rectangle of its artboard filled with its color
|
||||
/// (in the color's own model), on a layer of their own (Artboard Fill) below the
|
||||
/// others. `None` when none of them has a color.
|
||||
pub fn with_artboard_fills(&self, indices: impl IntoIterator<Item = usize>) -> Option<Self> {
|
||||
let fills: Vec<(Rect, vectorcraft_color::Color)> = indices
|
||||
.into_iter()
|
||||
.filter_map(|i| {
|
||||
let a = self.artboards.get(i)?;
|
||||
Some((a.rect, a.color?))
|
||||
})
|
||||
.collect();
|
||||
if fills.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut d = self.clone();
|
||||
let mut layer = Node::layer(d.alloc_id(), "Artboard Fill", super::LayerColor::Preset(0));
|
||||
let paths: Vec<Arc<Node>> = fills
|
||||
.into_iter()
|
||||
.map(|(r, c)| {
|
||||
let look = super::Appearance::basic(vectorcraft_color::Paint::solid(c), vectorcraft_color::Paint::None, 0.0);
|
||||
Arc::new(Node::path(d.alloc_id(), vectorcraft_geom::shapes::rectangle(r), look))
|
||||
})
|
||||
.collect();
|
||||
*layer.children_mut()? = paths;
|
||||
d.layers.insert(0, Arc::new(layer));
|
||||
Some(d)
|
||||
}
|
||||
}
|
||||
|
||||
/// A document's locked artboards ([`Document::locked_artboards`]).
|
||||
pub struct LockedArtboards<'a> {
|
||||
pub(crate) rects: Vec<Rect>,
|
||||
|
||||
+33
-2
@@ -20,7 +20,7 @@ use std::sync::Arc;
|
||||
|
||||
use vectorcraft_color::{Color, Gradient, GradientPaint, GradientStop, Paint};
|
||||
use vectorcraft_geom::kurbo::ParamCurveArclen;
|
||||
use vectorcraft_geom::{Affine, Anchor, BezPath, FillRule, PathData, Point, SubPath, Vec2};
|
||||
use vectorcraft_geom::{Affine, Anchor, BezPath, FillRule, PathData, Point, Rect, SubPath, Vec2};
|
||||
|
||||
use crate::appearance::{Appearance, AppearanceItem, Dash, StrokeLayer, WidthProfile};
|
||||
use crate::live::{BlendOrientation, BlendSpacing, BlendSpec};
|
||||
@@ -196,7 +196,11 @@ pub fn lerp_paint(a: &Paint, b: &Paint, t: f32) -> Paint {
|
||||
/// so dashes open out of a solid line; patterns of different lengths repeat to a common one.
|
||||
fn lerp_dash(x: Option<&Dash>, y: Option<&Dash>, t: f64) -> Option<Dash> {
|
||||
let solid_like = |d: &Dash| Dash {
|
||||
pattern: d.pattern.chunks(2).flat_map(|c| [c.iter().sum::<f64>(), 0.0]).take(d.pattern.len().max(2)).collect(),
|
||||
// An odd pattern repeats twice per period, so pair up the doubled one to keep every gap closed.
|
||||
pattern: if d.pattern.len() % 2 == 1 { d.pattern.repeat(2) } else { d.pattern.clone() }
|
||||
.chunks(2)
|
||||
.flat_map(|c| [c.iter().sum::<f64>(), 0.0])
|
||||
.collect(),
|
||||
offset: d.offset,
|
||||
align_corners: d.align_corners,
|
||||
};
|
||||
@@ -1025,6 +1029,27 @@ impl Rail {
|
||||
}
|
||||
}
|
||||
|
||||
/// A box round everything a blend of `keys` can paint once [`Rail::place`] puts its keys and steps
|
||||
/// on its spine, turned to it with Align to Path: the spine's box grown by three times the farthest
|
||||
/// any key paints from its centre. A step's shape lies within that distance of the point on the
|
||||
/// spine it is moved to, and so does its own centre, which it turns about. `None` when nothing
|
||||
/// moves.
|
||||
pub(crate) fn rail_reach(keys: &[Arc<Node>], spec: &BlendSpec, symbols: Option<&crate::SymbolBoxes>) -> Option<Rect> {
|
||||
if spec.spine.is_none() && spec.orientation != BlendOrientation::AlignToPath {
|
||||
return None;
|
||||
}
|
||||
let spine = blend_spine(keys, spec)?.0.bounds()?;
|
||||
let radius = keys
|
||||
.iter()
|
||||
.filter_map(|k| {
|
||||
let (c, b) = (center_of(k), k.visual(symbols)?);
|
||||
let corners = [Point::new(b.x0, b.y0), Point::new(b.x1, b.y0), Point::new(b.x0, b.y1), Point::new(b.x1, b.y1)];
|
||||
Some(corners.into_iter().map(|q| (q - c).hypot()).fold(0.0, f64::max))
|
||||
})
|
||||
.fold(0.0, f64::max);
|
||||
Some(spine.inflate(3.0 * radius, 3.0 * radius))
|
||||
}
|
||||
|
||||
// =====================================================================================
|
||||
// Blend evaluation
|
||||
// =====================================================================================
|
||||
@@ -1423,6 +1448,12 @@ mod tests {
|
||||
assert_eq!(p.at(0.5), (1.0, 1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn odd_dash_blends_to_solid_with_closed_gaps() {
|
||||
let d = lerp_dash(Some(&Dash { pattern: vec![2.0, 3.0, 4.0], ..Default::default() }), None, 1.0).unwrap();
|
||||
assert!(d.pattern.iter().skip(1).step_by(2).all(|g| *g == 0.0), "{:?}", d.pattern);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copied_steps_get_new_members() {
|
||||
let rect = |x: f64| Arc::new(Node::path(NodeId(7), shapes::rectangle(Rect::new(x, 0.0, x + 10.0, 10.0)), Appearance::default()));
|
||||
|
||||
@@ -125,7 +125,7 @@ pub fn oversized_tiff(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
}
|
||||
|
||||
/// The color type of TIFF `bytes` and its decoder; `None` for [`oversized_tiff`] images too.
|
||||
fn tiff(bytes: &[u8]) -> Option<(tiff::ColorType, tiff::decoder::Decoder<Cursor<&[u8]>>)> {
|
||||
pub(crate) fn tiff(bytes: &[u8]) -> Option<(tiff::ColorType, tiff::decoder::Decoder<Cursor<&[u8]>>)> {
|
||||
if !is_tiff(bytes) || oversized_tiff(bytes).is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
//! Compound shapes (Pathfinder panel › Make Compound Shape): a group whose members each have a
|
||||
//! shape mode. It draws as the one area the modes make, from the backmost member up, painted with
|
||||
//! the group's own appearance; the members stay editable. Evaluated in `vectorcraft-effects`.
|
||||
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
|
||||
use crate::{Node, NodeKind};
|
||||
|
||||
/// How a compound shape's member combines with the area of the members below it. The backmost
|
||||
/// member's mode does not matter: it starts the area.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum ShapeMode {
|
||||
/// Adds the member's area.
|
||||
#[default]
|
||||
Add,
|
||||
/// Takes the member's area away.
|
||||
Subtract,
|
||||
/// Keeps only what the member's area overlaps.
|
||||
Intersect,
|
||||
/// Keeps where exactly one of the area and the member's area is.
|
||||
Exclude,
|
||||
}
|
||||
|
||||
impl ShapeMode {
|
||||
/// Every mode, in the Pathfinder panel's order.
|
||||
pub const ALL: [ShapeMode; 4] = [ShapeMode::Add, ShapeMode::Subtract, ShapeMode::Intersect, ShapeMode::Exclude];
|
||||
|
||||
/// The mode's name in commands and files.
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
ShapeMode::Add => "add",
|
||||
ShapeMode::Subtract => "subtract",
|
||||
ShapeMode::Intersect => "intersect",
|
||||
ShapeMode::Exclude => "exclude",
|
||||
}
|
||||
}
|
||||
|
||||
/// The mode named `s` ([`ShapeMode::name`]).
|
||||
pub fn parse(s: &str) -> Option<Self> {
|
||||
Self::ALL.into_iter().find(|m| m.name() == s)
|
||||
}
|
||||
}
|
||||
|
||||
/// Files are untrusted: a mode that isn't one of the names reads as Add.
|
||||
impl<'de> Deserialize<'de> for ShapeMode {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
let v = serde_json::Value::deserialize(d)?;
|
||||
Ok(v.as_str().and_then(ShapeMode::parse).unwrap_or_default())
|
||||
}
|
||||
}
|
||||
|
||||
impl Node {
|
||||
/// Is this a compound shape? Only a group that doesn't clip can be one: the mark on anything
|
||||
/// else (from a file) is ignored.
|
||||
pub fn is_compound_shape(&self) -> bool {
|
||||
matches!(self.kind, NodeKind::Group { clip: false, .. }) && self.extras().compound_shape
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn modes_read_back_and_unknown_ones_are_add() {
|
||||
for m in ShapeMode::ALL {
|
||||
let v = serde_json::to_value(m).unwrap();
|
||||
assert_eq!(v, serde_json::json!(m.name()));
|
||||
assert_eq!(serde_json::from_value::<ShapeMode>(v).unwrap(), m);
|
||||
}
|
||||
for v in [serde_json::json!("divide"), serde_json::json!(3), serde_json::json!(null), serde_json::json!({"a": [1]})] {
|
||||
assert_eq!(serde_json::from_value::<ShapeMode>(v).unwrap(), ShapeMode::Add);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -485,7 +485,7 @@ mod tests {
|
||||
#[test]
|
||||
fn symbols_and_patterns_are_limited_too() {
|
||||
let mut d = Document::new(10.0, 10.0);
|
||||
d.symbols.push(crate::Symbol { name: "s".into(), art: nested(MAX_DEPTH + 3) });
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "s".into(), art: nested(MAX_DEPTH + 3) });
|
||||
assert!(d.too_deep(None));
|
||||
assert!(d.limit_depth());
|
||||
assert!(!d.too_deep(None));
|
||||
@@ -504,7 +504,7 @@ mod tests {
|
||||
for _ in 0..levels {
|
||||
art = Arc::new(Node::group(NodeId(3), vec![art]));
|
||||
}
|
||||
d.symbols.push(crate::Symbol { name: format!("s{i}"), art });
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: format!("s{i}"), art });
|
||||
}
|
||||
d
|
||||
}
|
||||
@@ -573,7 +573,7 @@ mod tests {
|
||||
};
|
||||
let through_a_symbol = |d: &mut Document| {
|
||||
d.patterns.push(crate::PatternDef::new("p", vec![instance(2, "s")]));
|
||||
d.symbols.push(crate::Symbol { name: "s".into(), art: painted(3, "p") });
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "s".into(), art: painted(3, "p") });
|
||||
};
|
||||
for make in [&itself as &dyn Fn(&mut Document), &each_other, &through_a_symbol] {
|
||||
let mut d = doc(painted(1, "p"));
|
||||
|
||||
@@ -47,6 +47,7 @@ impl Document {
|
||||
patterns,
|
||||
pattern_edit,
|
||||
mask_edit,
|
||||
symbol_edit,
|
||||
next_id: _,
|
||||
unknown,
|
||||
page_isolate,
|
||||
@@ -113,6 +114,7 @@ impl Document {
|
||||
(a, b) => a.is_none() && b.is_none(),
|
||||
}
|
||||
&& *mask_edit == other.mask_edit
|
||||
&& *symbol_edit == other.symbol_edit
|
||||
&& *unknown == other.unknown
|
||||
&& *page_isolate == other.page_isolate
|
||||
&& *page_knockout == other.page_knockout
|
||||
@@ -221,6 +223,7 @@ impl Document {
|
||||
patterns: vec![],
|
||||
pattern_edit: None,
|
||||
mask_edit: None,
|
||||
symbol_edit: None,
|
||||
next_id: 1,
|
||||
unknown: Default::default(),
|
||||
page_isolate: false,
|
||||
|
||||
@@ -98,18 +98,22 @@ impl Document {
|
||||
self.views = kept;
|
||||
}
|
||||
|
||||
/// Leave opacity-mask editing: drop the temporary editing layer, a working copy of art the
|
||||
/// mask already holds (the engine syncs it after every edit).
|
||||
/// Leave opacity-mask editing and symbol editing: drop the temporary editing layer, a working
|
||||
/// copy of art the mask or the symbol already holds (the engine syncs it after every edit).
|
||||
pub fn drop_edit_modes(&mut self) {
|
||||
if let Some(me) = self.mask_edit.take() {
|
||||
self.layers.retain(|l| l.id != me.layer);
|
||||
}
|
||||
if let Some(se) = self.symbol_edit.take() {
|
||||
self.layers.retain(|l| l.id != se.layer);
|
||||
}
|
||||
}
|
||||
|
||||
/// The document as saved and exported: without the opacity-mask editing layer. Borrowed when
|
||||
/// no mask is being edited. (Pattern editing is saved: its tile layer holds unapplied edits.)
|
||||
/// The document as saved and exported: without the opacity-mask or symbol editing layer.
|
||||
/// Borrowed when neither mode is on. (Pattern editing is saved: its tile layer holds unapplied
|
||||
/// edits.)
|
||||
pub fn without_edit_modes(&self) -> std::borrow::Cow<'_, Document> {
|
||||
if self.mask_edit.is_none() {
|
||||
if self.mask_edit.is_none() && self.symbol_edit.is_none() {
|
||||
return std::borrow::Cow::Borrowed(self);
|
||||
}
|
||||
let mut d = self.clone();
|
||||
|
||||
+175
-16
@@ -1,7 +1,9 @@
|
||||
//! Hit testing over the document tree.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::OnceLock;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use vectorcraft_geom::hit::{fill_contains, stroke_contains};
|
||||
use vectorcraft_geom::{BezPath, FillRule, Point, Rect};
|
||||
@@ -99,6 +101,128 @@ pub(crate) fn image_paint(n: &Node) -> Vec<(BezPath, FillRule, AppearanceItem)>
|
||||
IMAGE_PAINT.get().map_or_else(Vec::new, |f| f(n))
|
||||
}
|
||||
|
||||
/// One level of evaluation of a live object in a document, as the canvas draws it: type in an
|
||||
/// envelope as its distorted glyph outlines, symbol instances as their art. Effects are evaluated
|
||||
/// above this crate, so the engine registers one with [`set_live_art`]. It reads the object, the
|
||||
/// document's symbols and patterns, and what [`LiveInputs`] stands for, and nothing else.
|
||||
pub type LiveArt = fn(Option<&Document>, &Node) -> Vec<Node>;
|
||||
|
||||
/// A number that changes whenever something outside the document that [`LiveArt`] reads changes,
|
||||
/// such as the installed fonts or the plug-ins.
|
||||
pub type LiveInputs = fn() -> u64;
|
||||
|
||||
static LIVE_ART: OnceLock<(LiveArt, LiveInputs)> = OnceLock::new();
|
||||
|
||||
/// Register how a live object's art is evaluated for clicks, and the number that changes with what
|
||||
/// else it reads (the first registration stays).
|
||||
pub fn set_live_art(f: LiveArt, inputs: LiveInputs) {
|
||||
// Already set: the same engine, registered by another session.
|
||||
let _ = LIVE_ART.set((f, inputs));
|
||||
}
|
||||
|
||||
/// The registered [`LiveInputs`] now, 0 while nothing is registered.
|
||||
fn live_inputs() -> u64 {
|
||||
LIVE_ART.get().map_or(0, |(_, inputs)| inputs())
|
||||
}
|
||||
|
||||
/// 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`].
|
||||
struct EvaluatedLive {
|
||||
node: Node,
|
||||
symbols: Vec<(String, Arc<Node>)>,
|
||||
patterns: Vec<crate::PatternDef>,
|
||||
inputs: u64,
|
||||
art: Rc<Vec<Node>>,
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl EvaluatedLive {
|
||||
/// Is this the evaluation of `n` in `doc`, with inputs `inputs`, as they are now?
|
||||
fn is_of(&self, doc: &Document, n: &Node, inputs: u64) -> bool {
|
||||
self.inputs == inputs
|
||||
&& self.symbols.len() == doc.symbols.len()
|
||||
&& 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
|
||||
}
|
||||
}
|
||||
|
||||
/// How many evaluations [`live_art`] keeps on each thread: the pointer moving in a blend's or an
|
||||
/// envelope's stroke band hits it again and again, and an envelope can hold a blend.
|
||||
const LIVE_KEPT: usize = 4;
|
||||
|
||||
/// Most [`art_size`] the evaluations [`live_art`] keeps on one thread add up to. Art larger than
|
||||
/// this is not kept.
|
||||
const LIVE_KEPT_SIZE: usize = 250_000;
|
||||
|
||||
/// The size of `art` for [`LIVE_KEPT_SIZE`]: its objects and the anchors of their paths.
|
||||
fn art_size(art: &[Node]) -> usize {
|
||||
let mut size = 0usize;
|
||||
for n in art {
|
||||
n.walk(&mut |c| size = size.saturating_add(1 + c.path_data().map_or(0, |p| p.anchor_count())));
|
||||
}
|
||||
size
|
||||
}
|
||||
|
||||
/// Does `n`, or the art of a symbol or a pattern of `doc`, hold a placed document? Its art changes
|
||||
/// when the file is read, so [`live_art`] does not keep an evaluation of `n`.
|
||||
fn holds_placed(doc: &Document, n: &Node) -> bool {
|
||||
let mut placed = false;
|
||||
let mut find = |c: &Node| placed |= matches!(c.kind, NodeKind::PlacedDocument(_));
|
||||
n.walk(&mut find);
|
||||
doc.symbols.iter().for_each(|s| s.art.walk(&mut find));
|
||||
doc.patterns.iter().flat_map(|p| &p.art).for_each(|a| a.walk(&mut find));
|
||||
placed
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// The evaluations [`live_art`] keeps, the latest first.
|
||||
static EVALUATED: RefCell<Vec<EvaluatedLive>> = const { RefCell::new(Vec::new()) };
|
||||
}
|
||||
|
||||
/// 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`]).
|
||||
fn live_art(doc: &Document, n: &Node) -> Rc<Vec<Node>> {
|
||||
let inputs = live_inputs();
|
||||
let kept = EVALUATED.with_borrow_mut(|kept| {
|
||||
let i = kept.iter().position(|e| e.is_of(doc, n, inputs))?;
|
||||
let e = kept.remove(i);
|
||||
let art = e.art.clone();
|
||||
kept.insert(0, e);
|
||||
Some(art)
|
||||
});
|
||||
if let Some(art) = kept {
|
||||
return art;
|
||||
}
|
||||
#[cfg(test)]
|
||||
LIVE_EVALUATED.with(|c| c.set(c.get() + 1));
|
||||
let art = Rc::new(LIVE_ART.get().map_or_else(|| crate::live::expand_live(n), |(f, _)| f(Some(doc), n)));
|
||||
let size = art_size(&art);
|
||||
if size > LIVE_KEPT_SIZE || holds_placed(doc, n) {
|
||||
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 };
|
||||
EVALUATED.with_borrow_mut(|kept| {
|
||||
kept.insert(0, e);
|
||||
kept.truncate(LIVE_KEPT);
|
||||
let mut total = 0usize;
|
||||
kept.retain(|e| {
|
||||
total = total.saturating_add(e.size);
|
||||
total <= LIVE_KEPT_SIZE
|
||||
});
|
||||
});
|
||||
art
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many live objects [`live_art`] evaluated on this thread.
|
||||
static LIVE_EVALUATED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// Topmost editable object under `p`.
|
||||
pub fn hit_test(doc: &Document, p: Point, opt: HitOptions) -> Option<Hit> {
|
||||
hit_test_skipping(doc, p, opt, &|_| false, true)
|
||||
@@ -110,6 +234,12 @@ pub fn hit_test_visible(doc: &Document, p: Point, opt: HitOptions) -> Option<Hit
|
||||
hit_test_skipping(doc, p, opt, &|_| false, false)
|
||||
}
|
||||
|
||||
/// [`hit_test_visible`] passing over the nodes `skip` names (and their contents): what lies below
|
||||
/// them at `p`. A skipped clipping path still clips.
|
||||
pub fn hit_test_visible_skipping(doc: &Document, p: Point, opt: HitOptions, skip: &dyn Fn(NodeId) -> bool) -> Option<Hit> {
|
||||
hit_test_skipping(doc, p, opt, skip, false)
|
||||
}
|
||||
|
||||
/// The object of `selected` (topmost first) whose own art lies under `p` where a clipping mask
|
||||
/// hides it: a clip group (or a layer with a clipping mask) holding it doesn't clip to `p`. A
|
||||
/// click there hits nothing ([`hit_test`]), so the selection tools grab the selection by that
|
||||
@@ -538,20 +668,15 @@ fn hit_children(
|
||||
}
|
||||
|
||||
/// The order a click at `p` tries the children of `parent` in, front first: their stacking order
|
||||
/// from the top, except that in an Intertwine group the members drawn over the others at `p`
|
||||
/// come first (from the top among them too).
|
||||
/// from the top, except that in an Intertwine group the members are tried in the order they are
|
||||
/// drawn at `p`, from the top ([`crate::intertwine::tier_at`]).
|
||||
fn hit_order<'a>(parent: &Node, children: &'a [std::sync::Arc<Node>], p: Point) -> impl Iterator<Item = usize> + 'a {
|
||||
let over: Vec<usize> = if matches!(parent.kind, NodeKind::Intertwine { .. }) {
|
||||
let contains = |a: &vectorcraft_geom::PathData| fill_contains(&a.to_bezpath(), vectorcraft_geom::FillRule::NonZero, p);
|
||||
(0..children.len()).rev().filter(|i| children.get(*i).and_then(|c| crate::intertwine::over_area(c)).is_some_and(contains)).collect()
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
let rest = (0..children.len()).rev().filter({
|
||||
let over = over.clone();
|
||||
move |i| !over.contains(i)
|
||||
});
|
||||
over.into_iter().chain(rest)
|
||||
let mut order: Vec<usize> = (0..children.len()).rev().collect();
|
||||
if matches!(parent.kind, NodeKind::Intertwine { .. }) {
|
||||
let tier = |i: usize| children.get(i).map_or(0, |c| crate::intertwine::tier_at(c, p));
|
||||
order.sort_by_cached_key(|i| std::cmp::Reverse((tier(*i), *i)));
|
||||
}
|
||||
order.into_iter()
|
||||
}
|
||||
|
||||
/// A click on the strokes of a path or compound path `n` along `bp`: what any visible stroke
|
||||
@@ -564,7 +689,15 @@ fn hit_stroke(n: &Node, bp: &vectorcraft_geom::BezPath, rule: vectorcraft_geom::
|
||||
stroke_contains(bp, 0.0, opt.tol, p).then_some(HitKind::Outline)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many objects [`hit_leaf`] tested on this thread.
|
||||
static LEAVES_TESTED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
fn hit_leaf(n: &Node, p: Point, opt: HitOptions, symbols: &SymbolBoxes) -> Option<HitKind> {
|
||||
#[cfg(test)]
|
||||
LEAVES_TESTED.with(|c| c.set(c.get() + 1));
|
||||
match &n.kind {
|
||||
NodeKind::Path { path, rule, .. } => {
|
||||
let bp = path.to_bezpath();
|
||||
@@ -611,9 +744,18 @@ fn hit_leaf(n: &Node, p: Point, opt: HitOptions, symbols: &SymbolBoxes) -> Optio
|
||||
AppearanceItem::Fill(_) => (!opt.path_only && fill_contains(bp, *rule, p)).then_some(HitKind::Fill),
|
||||
})
|
||||
}
|
||||
NodeKind::Text(_) | NodeKind::PlacedDocument(_) | NodeKind::SymbolInstance { .. } | NodeKind::Blend { .. } | NodeKind::Envelope { .. } => {
|
||||
NodeKind::Text(_) | NodeKind::PlacedDocument(_) | NodeKind::SymbolInstance { .. } => {
|
||||
n.geometric_bounds_with(symbols).filter(|b| b.inflate(opt.tol, opt.tol).contains(p)).map(|_| HitKind::Bounds)
|
||||
}
|
||||
// A blend or an envelope is hit within its bounds, and outside them where its evaluated
|
||||
// art paints: a blend's keys and steps on a spine can reach past the keys and the spine,
|
||||
// and the strokes of an envelope's distorted contents past its bounds.
|
||||
NodeKind::Blend { .. } | NodeKind::Envelope { .. } => {
|
||||
if n.geometric_bounds_with(symbols).is_some_and(|b| b.inflate(opt.tol, opt.tol).contains(p)) {
|
||||
return Some(HitKind::Bounds);
|
||||
}
|
||||
live_art(symbols.document(), n).iter().find_map(|g| hit_any(g, p, opt, symbols))
|
||||
}
|
||||
NodeKind::Repeat(r) => r.expand(Some(symbols.document())).iter().find_map(|g| hit_any(g, p, opt, symbols)),
|
||||
NodeKind::Mesh(m) => {
|
||||
let bp = m.outline().to_bezpath();
|
||||
@@ -650,8 +792,25 @@ fn on_type_path(n: &Node, t: &crate::TextObject, p: Point, tol: f64) -> bool {
|
||||
|
||||
/// Hit anywhere in an evaluated subtree (groups recurse; leaves use [`hit_leaf`]).
|
||||
fn hit_any(n: &Node, p: Point, opt: HitOptions, symbols: &SymbolBoxes) -> Option<HitKind> {
|
||||
// Hidden art does not hit.
|
||||
if !n.visible {
|
||||
return None;
|
||||
}
|
||||
// Nor art whose reach does not hold `p`, as in [`may_hit`]: a blend's steps far from `p` are
|
||||
// passed by without being measured.
|
||||
if !edits_contents(n) && n.extras().shaper.is_none() && n.reach_bounds_with(symbols).is_some_and(|b| !b.inflate(opt.tol, opt.tol).contains(p)) {
|
||||
return None;
|
||||
}
|
||||
match &n.kind {
|
||||
NodeKind::Group { children, .. } => children.iter().rev().find_map(|c| hit_any(c, p, opt, symbols)),
|
||||
// A clip group only hits inside its clipping path.
|
||||
NodeKind::Group { children, clip } => {
|
||||
if *clip && children.first().is_some_and(|c| !clip_contains(c, p)) {
|
||||
return None;
|
||||
}
|
||||
children.iter().rev().find_map(|c| hit_any(c, p, opt, symbols))
|
||||
}
|
||||
// An intertwine group hits by its members, as on the canvas.
|
||||
NodeKind::Intertwine { children } => children.iter().rev().find_map(|c| hit_any(c, p, opt, symbols)),
|
||||
_ => hit_leaf(n, p, opt, symbols),
|
||||
}
|
||||
}
|
||||
|
||||
+146
-1
@@ -429,6 +429,151 @@ fn a_marquee_meets_blends_and_envelopes_by_their_box() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A click just outside a blend's bounds, in the reach of its keys' strokes, tests only the steps
|
||||
/// whose own reach holds the point, not every step of the evaluated blend.
|
||||
#[test]
|
||||
fn a_click_in_a_blends_stroke_band_tests_only_the_steps_that_reach_it() {
|
||||
let mut d = Document::new(1000.0, 1000.0);
|
||||
let l = d.layers[0].id;
|
||||
// 10 pt circles with 4 pt strokes, y 295 to 305, from x 10 to x 890: 200 steps about 4.3 pt apart.
|
||||
let stroked = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), 4.0);
|
||||
let circle = |id, x: f64| Arc::new(Node::path(id, shapes::ellipse(Rect::new(x, 295.0, x + 10.0, 305.0)), stroked.clone()));
|
||||
let blend = d.alloc_id();
|
||||
let keys = vec![circle(d.alloc_id(), 10.0), circle(d.alloc_id(), 880.0)];
|
||||
let spec = crate::live::BlendSpec { spacing: crate::live::BlendSpacing::Steps(200), ..Default::default() };
|
||||
d.insert(Some(l), 0, Node::new(blend, NodeKind::Blend { children: keys, spec: Box::new(spec) })).unwrap();
|
||||
let count = |p: Point| {
|
||||
LEAVES_TESTED.with(|c| c.set(0));
|
||||
let h = hit_test(&d, p, HitOptions::default()).map(|h| h.leaf);
|
||||
(h, LEAVES_TESTED.with(std::cell::Cell::get))
|
||||
};
|
||||
// On the steps' strokes halfway between the keys, 4 pt above the bounds: a few steps reach it.
|
||||
let (h, tested) = count(Point::new(450.0, 291.0));
|
||||
assert_eq!(h, Some(blend));
|
||||
assert!(tested < 20, "{tested} objects tested");
|
||||
// In the corner of the reach, outside every stroke: nothing hits.
|
||||
let (h, tested) = count(Point::new(6.0, 291.0));
|
||||
assert_eq!(h, None);
|
||||
assert!(tested < 20, "{tested} objects tested");
|
||||
}
|
||||
|
||||
/// An intertwine group in a live object's evaluated art (a blend key a spine moves) hits by its
|
||||
/// members.
|
||||
#[test]
|
||||
fn evaluated_art_hits_an_intertwine_group_by_its_members() {
|
||||
let d = Document::new(200.0, 200.0);
|
||||
let square = Arc::new(Node::path(NodeId(7), shapes::rectangle(Rect::new(0.0, 0.0, 20.0, 20.0)), Appearance::default_art()));
|
||||
let group = Node::new(NodeId(8), NodeKind::Intertwine { children: vec![square] });
|
||||
let symbols = d.symbol_boxes();
|
||||
assert_eq!(hit_any(&group, Point::new(10.0, 10.0), HitOptions::default(), &symbols), Some(HitKind::Fill));
|
||||
assert_eq!(hit_any(&group, Point::new(50.0, 50.0), HitOptions::default(), &symbols), None);
|
||||
}
|
||||
|
||||
/// The pointer moving in a blend's stroke band evaluates the blend once, and again after the blend
|
||||
/// or a symbol of the document changes.
|
||||
#[test]
|
||||
fn clicks_in_a_blends_stroke_band_evaluate_it_once_until_it_changes() {
|
||||
let mut d = Document::new(1000.0, 1000.0);
|
||||
let l = d.layers[0].id;
|
||||
let stroked = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), 4.0);
|
||||
let circle = |id, x: f64| Arc::new(Node::path(id, shapes::ellipse(Rect::new(x, 295.0, x + 10.0, 305.0)), stroked.clone()));
|
||||
let blend = d.alloc_id();
|
||||
let keys = vec![circle(d.alloc_id(), 10.0), circle(d.alloc_id(), 880.0)];
|
||||
let spec = crate::live::BlendSpec { spacing: crate::live::BlendSpacing::Steps(200), ..Default::default() };
|
||||
d.insert(Some(l), 0, Node::new(blend, NodeKind::Blend { children: keys, spec: Box::new(spec) })).unwrap();
|
||||
let evaluated = || LIVE_EVALUATED.with(std::cell::Cell::get);
|
||||
let before = evaluated();
|
||||
for x in [448.0, 450.0, 452.0] {
|
||||
assert_eq!(hit_test(&d, Point::new(x, 291.0), HitOptions::default()).map(|h| h.leaf), Some(blend));
|
||||
}
|
||||
assert_eq!(evaluated(), before + 1);
|
||||
// 100 steps 8.7 pt apart: a step still reaches (450, 291).
|
||||
if let Some(NodeKind::Blend { spec, .. }) = d.node_mut(blend).map(|n| &mut n.kind) {
|
||||
spec.spacing = crate::live::BlendSpacing::Steps(100);
|
||||
}
|
||||
assert_eq!(hit_test(&d, Point::new(450.0, 291.0), HitOptions::default()).map(|h| h.leaf), Some(blend));
|
||||
assert_eq!(evaluated(), before + 2);
|
||||
let art = circle(d.alloc_id(), 0.0);
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "dot".into(), art });
|
||||
hit_test(&d, Point::new(450.0, 291.0), HitOptions::default());
|
||||
assert_eq!(evaluated(), before + 3);
|
||||
hit_test(&d, Point::new(450.0, 291.0), HitOptions::default());
|
||||
assert_eq!(evaluated(), before + 3);
|
||||
}
|
||||
|
||||
/// Evaluated art larger than [`LIVE_KEPT_SIZE`] is not kept: each click in the blend's stroke band
|
||||
/// evaluates it again.
|
||||
#[test]
|
||||
fn evaluated_art_past_the_kept_size_is_not_kept() {
|
||||
let mut d = Document::new(1000.0, 1000.0);
|
||||
let l = d.layers[0].id;
|
||||
let stroked = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), 4.0);
|
||||
// A 10 pt circle drawn with 2,000 anchors: 202 objects of 2,000 anchors each with 200 steps.
|
||||
let circle = |id, x: f64| {
|
||||
let points: Vec<Point> = (0..2000)
|
||||
.map(|i| {
|
||||
let a = f64::from(i) * std::f64::consts::TAU / 2000.0;
|
||||
Point::new(x + 5.0 + 5.0 * a.cos(), 300.0 + 5.0 * a.sin())
|
||||
})
|
||||
.collect();
|
||||
let path = vectorcraft_geom::PathData::single(vectorcraft_geom::SubPath::polyline(&points, true));
|
||||
Arc::new(Node::path(id, path, stroked.clone()))
|
||||
};
|
||||
let blend = d.alloc_id();
|
||||
let keys = vec![circle(d.alloc_id(), 10.0), circle(d.alloc_id(), 880.0)];
|
||||
let spec = crate::live::BlendSpec { spacing: crate::live::BlendSpacing::Steps(200), ..Default::default() };
|
||||
d.insert(Some(l), 0, Node::new(blend, NodeKind::Blend { children: keys, spec: Box::new(spec) })).unwrap();
|
||||
let evaluated = || LIVE_EVALUATED.with(std::cell::Cell::get);
|
||||
let before = evaluated();
|
||||
for x in [448.0, 450.0] {
|
||||
assert_eq!(hit_test(&d, Point::new(x, 291.0), HitOptions::default()).map(|h| h.leaf), Some(blend));
|
||||
}
|
||||
assert_eq!(evaluated(), before + 2);
|
||||
}
|
||||
|
||||
/// While the document's symbols hold a placed document, whose art changes when its file is read,
|
||||
/// each click in a blend's stroke band evaluates the blend again.
|
||||
#[test]
|
||||
fn evaluated_art_is_not_kept_while_a_placed_document_may_show_in_it() {
|
||||
let mut d = Document::new(1000.0, 1000.0);
|
||||
let l = d.layers[0].id;
|
||||
let stroked = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), 4.0);
|
||||
let circle = |id, x: f64| Arc::new(Node::path(id, shapes::ellipse(Rect::new(x, 295.0, x + 10.0, 305.0)), stroked.clone()));
|
||||
let blend = d.alloc_id();
|
||||
let keys = vec![circle(d.alloc_id(), 10.0), circle(d.alloc_id(), 880.0)];
|
||||
let spec = crate::live::BlendSpec { spacing: crate::live::BlendSpacing::Steps(200), ..Default::default() };
|
||||
d.insert(Some(l), 0, Node::new(blend, NodeKind::Blend { children: keys, spec: Box::new(spec) })).unwrap();
|
||||
let placed = crate::PlacedDocument {
|
||||
link: crate::LinkInfo::new("/art/logo.vectorcraft"),
|
||||
key: "logo".into(),
|
||||
bounding: false,
|
||||
page_box: crate::PageBox::Crop,
|
||||
width: 80.0,
|
||||
height: 40.0,
|
||||
xf: vectorcraft_geom::Affine::IDENTITY,
|
||||
placement: Default::default(),
|
||||
};
|
||||
let art = Arc::new(Node::new(d.alloc_id(), NodeKind::PlacedDocument(Box::new(placed))));
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "logo".into(), art });
|
||||
let evaluated = || LIVE_EVALUATED.with(std::cell::Cell::get);
|
||||
let before = evaluated();
|
||||
for x in [448.0, 450.0] {
|
||||
assert_eq!(hit_test(&d, Point::new(x, 291.0), HitOptions::default()).map(|h| h.leaf), Some(blend));
|
||||
}
|
||||
assert_eq!(evaluated(), before + 2);
|
||||
}
|
||||
|
||||
/// A kept evaluation is the evaluation of the object only while the fonts and plug-ins
|
||||
/// ([`LiveInputs`]) are as they were.
|
||||
#[test]
|
||||
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 };
|
||||
assert!(e.is_of(&d, &n, 7));
|
||||
assert!(!e.is_of(&d, &n, 8));
|
||||
}
|
||||
|
||||
/// 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.
|
||||
@@ -841,7 +986,7 @@ fn objects_at_matches_repeated_hits_on_random_documents() {
|
||||
fn objects_at_finds_symbol_instances_by_their_art() {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let art = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(100.0, 60.0, 140.0, 80.0)), Appearance::default_art());
|
||||
d.symbols.push(crate::Symbol { name: "Far".into(), art: Arc::new(art) });
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "Far".into(), art: Arc::new(art) });
|
||||
let (layer, top, inner, group) = (d.layers[0].id, d.alloc_id(), d.alloc_id(), d.alloc_id());
|
||||
let instance = |id, dy: f64| Node::new(id, NodeKind::SymbolInstance { symbol: "Far".into(), xf: vectorcraft_geom::Affine::translate((0.0, dy)) });
|
||||
d.insert(Some(layer), 0, instance(top, 0.0)).unwrap();
|
||||
|
||||
@@ -109,9 +109,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, not
|
||||
/// opacity masks) with its swatch link and tint. Mesh points have no link: changes to it are
|
||||
/// dropped.
|
||||
/// 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.
|
||||
pub fn visit_node_colors(n: &mut Node, f: &mut ColorVisitor) {
|
||||
for it in &mut n.appearance.items {
|
||||
match it {
|
||||
@@ -119,6 +119,9 @@ 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);
|
||||
}
|
||||
match &mut n.kind {
|
||||
NodeKind::Text(t) => {
|
||||
for r in &mut t.runs {
|
||||
|
||||
@@ -1,36 +1,51 @@
|
||||
//! Object › Intertwine: objects that pass over and under each other.
|
||||
//!
|
||||
//! An Intertwine group ([`NodeKind::Intertwine`]) keeps its members as they are, in their own
|
||||
//! stacking order. A member can be drawn over the other members in part of the group: that area
|
||||
//! is the member's [`crate::node::NodeExtras::intertwine`], in document coordinates. The group
|
||||
//! draws (and exports, and is hit) as its live expansion ([`expand`]): the members, then, for
|
||||
//! each member with an area, a copy of it clipped to that area. Where several members are drawn
|
||||
//! over the others in one place, the upper one in the stacking order ends on top.
|
||||
//! stacking order. A member can be drawn over other members in part of the group, at a tier: the
|
||||
//! area of tier 1 is the member's [`crate::node::NodeExtras::intertwine`], and the areas of tiers 2
|
||||
//! and up are its [`crate::node::NodeExtras::intertwine_tiers`], in document coordinates. In each
|
||||
//! place the members are drawn by their tier there (0 outside their areas), and members of one
|
||||
//! tier in their stacking order, so any order of the members can be set for each overlap area.
|
||||
//! The group draws (and exports, and is hit) as its live expansion ([`expand`]): the members,
|
||||
//! then, tier by tier, a copy of each member with an area of that tier clipped to it.
|
||||
//!
|
||||
//! The members stay editable, and Release gives them back as they were (the areas let go).
|
||||
//! Behaviour: <https://helpx.adobe.com/illustrator/using/intertwine-objects.html>.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use vectorcraft_geom::{FillRule, PathData};
|
||||
use vectorcraft_geom::hit::fill_contains;
|
||||
use vectorcraft_geom::{FillRule, PathData, Point};
|
||||
|
||||
use crate::{Node, NodeId, NodeKind};
|
||||
|
||||
/// The area where `n` is drawn over the other members of its Intertwine group (empty: nowhere).
|
||||
pub fn over_area(n: &Node) -> Option<&PathData> {
|
||||
n.extras().intertwine.as_deref().filter(|a| !a.is_empty())
|
||||
/// The areas where `n` is drawn over other members of its Intertwine group, each with its tier
|
||||
/// (1 and up); empty ones are left out.
|
||||
pub fn tier_areas(n: &Node) -> impl Iterator<Item = (usize, &PathData)> {
|
||||
let e = n.extras();
|
||||
e.intertwine
|
||||
.as_deref()
|
||||
.map(|a| (1, a))
|
||||
.into_iter()
|
||||
.chain(e.intertwine_tiers.iter().enumerate().map(|(k, a)| (k + 2, a)))
|
||||
.filter(|(_, a)| !a.is_empty())
|
||||
}
|
||||
|
||||
/// The members of an Intertwine group evaluated as plain art: the members (ids kept), then each
|
||||
/// member with an area drawn over the others, as a clip group of that area around a copy of it
|
||||
/// (ids 0: generated art).
|
||||
/// The tier `n` is drawn at at `p`: the highest tier whose area holds `p` (0: none does).
|
||||
pub fn tier_at(n: &Node, p: Point) -> usize {
|
||||
tier_areas(n).filter(|(_, a)| fill_contains(&a.to_bezpath(), FillRule::NonZero, p)).map(|(t, _)| t).max().unwrap_or(0)
|
||||
}
|
||||
|
||||
/// The members of an Intertwine group evaluated as plain art: the members (ids kept), then, for
|
||||
/// each tier from 1 up, each member with an area of that tier, as a clip group of that area
|
||||
/// around a copy of it (ids 0: generated art).
|
||||
pub fn expand(children: &[Arc<Node>]) -> Vec<Node> {
|
||||
let mut out: Vec<Node> = children.iter().map(|c| plain(c)).collect();
|
||||
for c in children {
|
||||
let Some(area) = over_area(c) else { continue };
|
||||
if !c.visible {
|
||||
continue;
|
||||
}
|
||||
let mut over: Vec<(usize, usize, &PathData)> =
|
||||
children.iter().enumerate().filter(|(_, c)| c.visible).flat_map(|(i, c)| tier_areas(c).map(move |(t, a)| (t, i, a))).collect();
|
||||
over.sort_by_key(|(t, i, _)| (*t, *i));
|
||||
for (_, i, area) in over {
|
||||
let Some(c) = children.get(i) else { continue };
|
||||
let mut clip = Node::new(NodeId(0), NodeKind::Path { path: area.clone(), rule: FillRule::NonZero, live: None, clipping: true, guide: false });
|
||||
clip.appearance = crate::Appearance::default().into();
|
||||
let mut copy = plain(c);
|
||||
@@ -40,11 +55,13 @@ pub fn expand(children: &[Arc<Node>]) -> Vec<Node> {
|
||||
out
|
||||
}
|
||||
|
||||
/// `n` without its Intertwine area (the expansion is plain art).
|
||||
/// `n` without its Intertwine areas (the expansion is plain art).
|
||||
fn plain(n: &Node) -> Node {
|
||||
let mut n = n.clone();
|
||||
if n.extras().intertwine.is_some() {
|
||||
n.extras_mut().intertwine = None;
|
||||
if n.extras().intertwine.is_some() || !n.extras().intertwine_tiers.is_empty() {
|
||||
let mut e = n.extras_mut();
|
||||
e.intertwine = None;
|
||||
e.intertwine_tiers = Vec::new();
|
||||
}
|
||||
n
|
||||
}
|
||||
@@ -88,6 +105,28 @@ mod tests {
|
||||
assert_eq!((out[2].id, children[1].id), (NodeId(0), NodeId(0)), "generated art has no ids of its own");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copies_are_drawn_tier_by_tier() {
|
||||
let over = || shapes::rectangle(Rect::new(5.0, 0.0, 10.0, 10.0));
|
||||
// The lower member at tier 2, the upper one at tier 1: the lower one ends on top.
|
||||
let mut a = (*square(1, 0.0)).clone();
|
||||
a.extras_mut().intertwine_tiers = vec![over()];
|
||||
let mut b = (*square(2, 5.0)).clone();
|
||||
b.extras_mut().intertwine = Some(Box::new(over()));
|
||||
let kids = vec![Arc::new(a), Arc::new(b)];
|
||||
assert_eq!((tier_at(&kids[0], Point::new(7.0, 5.0)), tier_at(&kids[1], Point::new(7.0, 5.0))), (2, 1));
|
||||
assert_eq!(tier_at(&kids[0], Point::new(2.0, 5.0)), 0);
|
||||
let out = expand(&kids);
|
||||
assert_eq!(out.len(), 4, "both members, then the copy of tier 1, then the one of tier 2");
|
||||
assert!(out.iter().all(|n| n.extras().intertwine.is_none() && n.extras().intertwine_tiers.is_empty()), "the expansion is plain art");
|
||||
let copied = |n: &Node| match &n.kind {
|
||||
NodeKind::Group { children, clip: true } => children[1].path_data().cloned(),
|
||||
k => panic!("a clip group: {k:?}"),
|
||||
};
|
||||
assert_eq!(copied(&out[2]).as_ref(), kids[1].path_data());
|
||||
assert_eq!(copied(&out[3]).as_ref(), kids[0].path_data());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hidden_member_is_not_drawn_over_the_others() {
|
||||
let mut a = (*square(1, 0.0)).clone();
|
||||
|
||||
+13
-3
@@ -11,6 +11,7 @@ pub mod blend;
|
||||
pub mod cancel;
|
||||
pub mod clipnest;
|
||||
pub mod cmyk;
|
||||
pub mod compound_shape;
|
||||
pub mod corners;
|
||||
pub mod depth;
|
||||
pub mod dimension;
|
||||
@@ -23,11 +24,13 @@ pub mod intertwine;
|
||||
pub mod shared;
|
||||
mod symbol_export_type;
|
||||
mod symbol_nine_slice;
|
||||
pub mod symbol_overrides;
|
||||
mod symbol_registration;
|
||||
pub use inline::{SYMBOL_HALF, SYMBOL_SIZES};
|
||||
pub use shared::Shared;
|
||||
pub use symbol_export_type::{SYMBOL_EXPORT_TYPE, SymbolExportType};
|
||||
pub use symbol_nine_slice::{SYMBOL_NINE_SLICE, SymbolNineSlice};
|
||||
pub use symbol_overrides::SymbolOverride;
|
||||
pub use symbol_registration::{SYMBOL_REGISTRATION, SymbolRegistration};
|
||||
pub mod links;
|
||||
pub mod live;
|
||||
@@ -42,7 +45,7 @@ pub mod overprint;
|
||||
pub mod pattern;
|
||||
pub mod perspective;
|
||||
mod pixels;
|
||||
pub use pixels::MAX_RASTER_ALLOC;
|
||||
pub use pixels::{GraySource, MAX_RASTER_ALLOC};
|
||||
pub mod placed_document;
|
||||
pub mod plugin_data;
|
||||
pub mod profiles;
|
||||
@@ -76,6 +79,7 @@ pub use appearance::{
|
||||
StrokeAlign, StrokeLayer, WidthProfile,
|
||||
};
|
||||
pub use assets::ExportAsset;
|
||||
pub use compound_shape::ShapeMode;
|
||||
pub use corners::LiveCorners;
|
||||
pub use depth::{MAX_DEPTH, MAX_SYMBOL_NODES};
|
||||
pub use graph::{
|
||||
@@ -102,10 +106,12 @@ pub use selection::{AnchorRef, SegmentRef, Selection};
|
||||
pub use setup::{Background, DocSetup, ExportText, GridSize, Quotes};
|
||||
pub use slices::{CellAlign, CellVAlign, Slice, SliceArea, SliceKind, SliceOptions, SliceSource};
|
||||
pub use style_libs::StyleLibrary;
|
||||
pub use text::FontHeightReference;
|
||||
pub use text::{
|
||||
AreaFit, AreaOptions, Burasagari, CharAlign, CharPosition, CharStyle, Composer, Digits, FirstBaseline, HyphenationOptions, InlineArt,
|
||||
JustificationOptions, Justify, Kinsoku, LeadingModel, Mojikumi, ParaDirection, ParaStyle, PathAlign, PathEffect, RubyAlign, RubyPosition,
|
||||
RubySpan, ScriptMetrics, SingleWordJustify, TabAlign, TabStop, TextKind, TextObject, TextRun, TextStyleDef, TextWrap, VerticalAlign, WrapShape,
|
||||
RubySpan, ScriptMetrics, SingleWordJustify, TabAlign, TabStop, TextFlow, TextKind, TextObject, TextRun, TextStyleDef, TextWrap, VerticalAlign,
|
||||
WrapShape,
|
||||
};
|
||||
pub use trace::TraceView;
|
||||
pub use variables::{DataSet, DataValue, Variable, VariableKind, Variables};
|
||||
@@ -136,7 +142,7 @@ pub use guides::{GridPrefs, Guide, RulerMode};
|
||||
#[cfg(test)]
|
||||
use limits::MAX_NEXT_ID;
|
||||
pub use limits::{DocError, FORMAT_VERSION, MAX_COORD};
|
||||
pub use records::{ImageBlob, MaskEdit, NewArtIds, SavedSelection, SavedView, Symbol};
|
||||
pub use records::{ImageBlob, MaskEdit, NewArtIds, SavedSelection, SavedView, Symbol, SymbolEdit};
|
||||
pub use units::{ColorMode, Unit, parse_number};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
@@ -216,6 +222,10 @@ pub struct Document {
|
||||
/// still read so files saved mid-edit by older versions load without the editing layer.
|
||||
#[serde(default, skip_serializing)]
|
||||
pub mask_edit: Option<MaskEdit>,
|
||||
/// Symbol editing mode, while active. Never saved (see [`Document::without_edit_modes`]): the
|
||||
/// symbol already holds the art of its editing layer.
|
||||
#[serde(default, skip_serializing)]
|
||||
pub symbol_edit: Option<SymbolEdit>,
|
||||
next_id: u64,
|
||||
/// Foreign data preserved on round-trip.
|
||||
#[serde(default, skip_serializing_if = "skip::is_empty_map")]
|
||||
|
||||
+128
-2
@@ -63,7 +63,7 @@ pub use expand::{
|
||||
};
|
||||
#[cfg(test)]
|
||||
use mesh::grid_eval;
|
||||
pub use mesh::{Coons, GradientMesh, MeshAppearance, MeshPoint, MeshQuad, default_fidelity, grid_points, nodes_bounds};
|
||||
pub use mesh::{Coons, GradientMesh, MeshAppearance, MeshPiece, MeshPoint, MeshQuad, default_fidelity, grid_points, nodes_bounds};
|
||||
pub use warp::{WarpStyle, fit_piece, map_nonlinear, map_nonlinear_within, pieces, warp_point};
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -181,6 +181,44 @@ mod tests {
|
||||
assert!((p.x - 37.0).abs() < 1e-6 && (p.y - 81.0).abs() < 1e-6, "{p:?}");
|
||||
}
|
||||
|
||||
/// #3592: a mesh read from a file can have any number of patches, and the view any zoom, so the
|
||||
/// quads one tessellation makes are budgeted: the subdivision per patch gives way before memory
|
||||
/// does, and each patch stays at least one quad.
|
||||
#[test]
|
||||
fn a_large_mesh_tessellates_within_the_quad_budget() {
|
||||
let mesh = |n: u32| GradientMesh::from_surface(n, n, &|u, v| Point::new(u * 300.0, v * 300.0), &|u, v| Color::rgb(u as f32, v as f32, 0.5));
|
||||
// 3,600 patches at 32 x 32 would be 3.7 million quads.
|
||||
let m = mesh(60);
|
||||
let quads = m.quads(32);
|
||||
assert!(quads.len() <= GradientMesh::MAX_QUADS, "{} quads", quads.len());
|
||||
assert!(quads.len() >= 3600 * 4, "still finer than a quad per patch: {}", quads.len());
|
||||
// A small mesh keeps the subdivision asked for.
|
||||
assert_eq!(mesh(3).quads(8).len(), 9 * 64);
|
||||
// The finest the view asks for, on a mesh of 22,500 patches, is a few quads per patch.
|
||||
let big = mesh(150).quads(64);
|
||||
assert!(big.len() <= GradientMesh::MAX_QUADS && big.len() >= 22_500, "{} quads", big.len());
|
||||
// More patches than the budget has quads is malformed, as a point count that doesn't fit is.
|
||||
let point = MeshPoint { p: Point::ZERO, color: Color::rgb(0.0, 0.0, 0.0), opacity: 1.0, handles: [Vec2::ZERO; 4] };
|
||||
let huge = GradientMesh { rows: 2000, cols: 2000, points: vec![point; 2001 * 2001] };
|
||||
assert!(!huge.is_valid());
|
||||
assert!(huge.quads(1).is_empty());
|
||||
}
|
||||
|
||||
/// Flat pieces of a mesh over the quad budget follow the coarser subdivision [`GradientMesh::quads`]
|
||||
/// gives way to: each patch's pieces are its own quads', so together they cover the mesh once.
|
||||
#[test]
|
||||
fn a_large_meshs_pieces_follow_the_quad_budget() {
|
||||
let m = GradientMesh::from_surface(60, 60, &|u, v| Point::new(u * 300.0, v * 300.0), &|u, v| Color::rgb(u as f32, v as f32, 0.5));
|
||||
let area = |pts: &[Point]| {
|
||||
let n = pts.len();
|
||||
(0..n).map(|i| pts[i].x * pts[(i + 1) % n].y - pts[(i + 1) % n].x * pts[i].y).sum::<f64>().abs() / 2.0
|
||||
};
|
||||
let pieces = m.pieces(32);
|
||||
assert!(pieces.len() >= 3600, "{} pieces", pieces.len());
|
||||
let total: f64 = pieces.iter().map(|p| area(&p.pts)).sum();
|
||||
assert!((total - 90_000.0).abs() < 90.0, "covered {total}");
|
||||
}
|
||||
|
||||
/// A file's rows and columns can be anything: a point count too large to compute is no match
|
||||
/// for the points (u32::MAX × u32::MAX wrapped to 0 and passed for a grid without points).
|
||||
#[test]
|
||||
@@ -195,17 +233,86 @@ mod tests {
|
||||
assert_eq!(envelope_mapper(&kind, Rect::new(0.0, 0.0, 100.0, 100.0))(p), p);
|
||||
}
|
||||
|
||||
/// How far the border of `m` strays from the ellipse in `r`, in points (measured across the
|
||||
/// ellipse's short radius), checked at 20 places along every border edge.
|
||||
fn mesh_border_off_ellipse(m: &GradientMesh, r: Rect) -> f64 {
|
||||
let (rows, cols) = (m.rows as usize, m.cols as usize);
|
||||
let edges = (0..cols).flat_map(|c| [m.edge_u(0, c), m.edge_u(rows, c)]).chain((0..rows).flat_map(|k| [m.edge_v(k, 0), m.edge_v(k, cols)]));
|
||||
let (c, a, b) = (r.center(), r.width() / 2.0, r.height() / 2.0);
|
||||
edges
|
||||
.flat_map(|e| (0..=20).map(move |i| e.eval(f64::from(i) / 20.0)))
|
||||
.map(|p| ((p.x - c.x) / a).hypot((p.y - c.y) / b) - 1.0)
|
||||
.fold(0.0f64, |w, d| w.max(d.abs() * a.min(b)))
|
||||
}
|
||||
|
||||
/// #3187: a gradient mesh made from an ellipse keeps the ellipse's shape: a Mesh tool click
|
||||
/// (a 1×1 mesh with lines through the point) and Create Gradient Mesh's 4×4 grid both lay
|
||||
/// their border on the outline, within about twice the 0.35 pt the outline is flattened to.
|
||||
/// The 1×1 mesh was up to 7 pt off this 400 × 300 pt ellipse.
|
||||
#[test]
|
||||
fn a_mesh_from_an_ellipse_keeps_its_outline() {
|
||||
const OFF: f64 = 0.75;
|
||||
let r = Rect::new(100.0, 100.0, 500.0, 400.0);
|
||||
let e = shapes::ellipse(r);
|
||||
for (rows, cols) in [(1, 1), (4, 4), (2, 5)] {
|
||||
let m = GradientMesh::for_path(&e, rows, cols, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
let off = mesh_border_off_ellipse(&m, r);
|
||||
assert!(off < OFF, "{rows}×{cols} mesh: border {off} pt off the ellipse");
|
||||
}
|
||||
let made = GradientMesh::for_path(&e, 1, 1, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
let mut m = made.clone();
|
||||
// Off centre, so each line splits its edges unevenly.
|
||||
let i = m.add_lines_at(Point::new(200.0, 330.0), 0.0).unwrap();
|
||||
let off = mesh_border_off_ellipse(&m, r);
|
||||
assert!(off < OFF, "after a Mesh tool click: {off} pt");
|
||||
// Deleting the lines again (Alt-click) gives the border back as it was.
|
||||
assert!(m.remove_point_lines(i));
|
||||
assert_eq!((m.rows, m.cols), (1, 1));
|
||||
for (k, (now, was)) in m.points.iter().zip(&made.points).enumerate() {
|
||||
let moved = now.handles.iter().zip(&was.handles).map(|(a, b)| (*a - *b).hypot()).fold(0.0, f64::max);
|
||||
assert!(now.p.distance(was.p) < 1e-6 && moved < 1e-3, "point {k}: {now:?}, made as {was:?}");
|
||||
}
|
||||
// With a handle at the new border point shortened since, the two edges no longer make one
|
||||
// curve: the merged edge is straight rather than grown past its ends.
|
||||
let mut m = made.clone();
|
||||
let i = m.add_lines_at(Point::new(200.0, 330.0), 0.0).unwrap();
|
||||
let top = m.idx(0, 1);
|
||||
m.points[top].handles[H_LEFT] *= 1e-4;
|
||||
assert!(m.remove_point_lines(i));
|
||||
let (a, b) = (m.points[0].p, m.points[1].p);
|
||||
assert!((m.points[0].handles[H_RIGHT] - (b - a) / 3.0).hypot() < 1e-9, "{:?}", m.points[0].handles);
|
||||
// A rectangle's mesh keeps straight edges with handles a third of the way along.
|
||||
let m = GradientMesh::for_path(&shapes::rectangle(Rect::new(0.0, 0.0, 90.0, 60.0)), 1, 1, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
let e = m.edge_u(0, 0);
|
||||
assert!(e.p1.distance(Point::new(30.0, 0.0)) < 1e-6 && e.p2.distance(Point::new(60.0, 0.0)) < 1e-6, "{e:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mesh_insert_and_remove_lines() {
|
||||
let mut m =
|
||||
GradientMesh::for_path(&shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), 1, 1, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
let i = m.add_lines_at(Point::new(30.0, 60.0)).unwrap();
|
||||
let i = m.add_lines_at(Point::new(30.0, 60.0), 0.0).unwrap();
|
||||
assert_eq!((m.rows, m.cols), (2, 2));
|
||||
assert!(m.points[i].p.distance(Point::new(30.0, 60.0)) < 0.5, "{:?}", m.points[i].p);
|
||||
assert!(m.remove_point_lines(i));
|
||||
assert_eq!((m.rows, m.cols), (1, 1));
|
||||
}
|
||||
|
||||
/// A point outside the mesh within the tolerance locates the nearest point of its edge.
|
||||
#[test]
|
||||
fn mesh_locate_within_reaches_the_edge() {
|
||||
let m = GradientMesh::for_path(&shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), 2, 2, Color::BLACK, MeshAppearance::Flat, 0.0).unwrap();
|
||||
let p = Point::new(102.0, 30.0);
|
||||
assert_eq!(m.locate(p), None);
|
||||
assert_eq!(m.locate_within(p, 1.0), None);
|
||||
let (r, c, u, v) = m.locate_within(p, 3.0).unwrap();
|
||||
assert_eq!((r, c), (0, 1));
|
||||
assert_eq!(u, 1.0, "on the right edge");
|
||||
let q = m.eval(r, c, u, v);
|
||||
assert!(q.distance(Point::new(100.0, 30.0)) < 1e-6, "{q:?}");
|
||||
assert_eq!(m.locate_within(Point::new(30.0, 60.0), 3.0), m.locate(Point::new(30.0, 60.0)));
|
||||
}
|
||||
|
||||
/// Corners all in CMYK mix in CMYK (the inks halfway), not in RGB.
|
||||
#[test]
|
||||
fn a_cmyk_mesh_mixes_its_inks() {
|
||||
@@ -221,6 +328,25 @@ mod tests {
|
||||
assert!(matches!(m.color_at(0, 0, 0.5, 0.5).0, Color::Rgb { .. }));
|
||||
}
|
||||
|
||||
/// #3232: the Appearance highlight lightens each point towards white in its own model, so a
|
||||
/// CMYK mesh stays CMYK (K shrinks with the other inks) and a grey one stays grey.
|
||||
#[test]
|
||||
fn mesh_highlight_keeps_each_points_model() {
|
||||
let square = shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0));
|
||||
let ink = Color::Cmyk { c: 0.0, m: 0.4, y: 0.6, k: 0.1 };
|
||||
let m = GradientMesh::for_path(&square, 2, 2, ink, MeshAppearance::ToCenter, 50.0).unwrap();
|
||||
assert_eq!(m.points[0].color, ink, "corners stay");
|
||||
let centre = m.points[m.idx(1, 1)].color;
|
||||
let Color::Cmyk { c, m: mg, y, k } = centre else { panic!("{centre:?}") };
|
||||
assert!(c.abs() < 1e-6 && (mg - 0.2).abs() < 1e-6 && (y - 0.3).abs() < 1e-6 && (k - 0.05).abs() < 1e-6, "{c} {mg} {y} {k}");
|
||||
assert!(matches!(m.color_at(0, 0, 0.5, 0.5).0, Color::Cmyk { .. }), "patches mix in CMYK");
|
||||
let m = GradientMesh::for_path(&square, 2, 2, Color::gray(0.8), MeshAppearance::ToEdge, 100.0).unwrap();
|
||||
assert_eq!((m.points[0].color, m.points[m.idx(1, 1)].color), (Color::gray(0.0), Color::gray(0.8)));
|
||||
// RGB lightens in RGB as before.
|
||||
let m = GradientMesh::for_path(&square, 2, 2, Color::rgb(1.0, 0.0, 0.0), MeshAppearance::ToCenter, 100.0).unwrap();
|
||||
assert_eq!(m.points[m.idx(1, 1)].color.to_hex(), "#ffffff");
|
||||
}
|
||||
|
||||
/// Alt-clicking a point of a mesh from a file with fewer points than its rows and columns need
|
||||
/// deletes nothing (it indexed past the points, a panic).
|
||||
#[test]
|
||||
|
||||
@@ -293,9 +293,10 @@ pub fn is_live(n: &Node) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
/// Mesh tessellation as filled quad paths (no stroke), `n`×`n` per patch.
|
||||
/// Mesh tessellation as flat-filled paths (no stroke): each patch cut `n`×`n`, its quads joined
|
||||
/// where the colour hardly changes ([`GradientMesh::pieces`]).
|
||||
pub fn mesh_quad_nodes(m: &GradientMesh, n: usize) -> Vec<Node> {
|
||||
m.quads(n)
|
||||
m.pieces(n)
|
||||
.into_iter()
|
||||
.map(|q| {
|
||||
let mut ap = Appearance::default();
|
||||
|
||||
+295
-18
@@ -35,6 +35,15 @@ pub struct MeshQuad {
|
||||
pub corners: [(Color, f32); 4],
|
||||
}
|
||||
|
||||
/// One flat-coloured piece of a mesh ([`GradientMesh::pieces`]): a closed outline through
|
||||
/// tessellation points, its colour and opacity.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MeshPiece {
|
||||
pub pts: Vec<Point>,
|
||||
pub color: Color,
|
||||
pub opacity: f32,
|
||||
}
|
||||
|
||||
/// Create Gradient Mesh → Appearance.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum MeshAppearance {
|
||||
@@ -157,6 +166,76 @@ impl Coons {
|
||||
// Gradient meshes
|
||||
// =====================================================================================
|
||||
|
||||
/// The handles, as offsets from `ends`, of the cubic from `ends.0` to `ends.1` that comes closest
|
||||
/// (least squares) to `side` between `span.0` and `span.1`; `None` when they can't be computed.
|
||||
/// Each point of the side is matched to the curve's nearest point, refined over a few rounds.
|
||||
fn fit_edge(side: &[Point], span: (f64, f64), ends: (Point, Point)) -> Option<(Vec2, Vec2)> {
|
||||
const N: usize = 32;
|
||||
const ROUNDS: usize = 6;
|
||||
let (p0, p3) = (ends.0, ends.1);
|
||||
let qs: Vec<Point> = (1..N).map(|i| Coons::sample(side, span.0 + (span.1 - span.0) * i as f64 / N as f64)).collect();
|
||||
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) = (0.0, 0.0, 0.0);
|
||||
let (mut x1, mut x2) = (Vec2::ZERO, Vec2::ZERO);
|
||||
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 r = q.to_vec2() - b0 * p0.to_vec2() - b3 * p3.to_vec2();
|
||||
c11 += b1 * b1;
|
||||
c12 += b1 * b2;
|
||||
c22 += b2 * b2;
|
||||
x1 += b1 * r;
|
||||
x2 += b2 * r;
|
||||
}
|
||||
let det = c11 * c22 - c12 * c12;
|
||||
if det.abs() < 1e-12 {
|
||||
break;
|
||||
}
|
||||
let (p1, p2) = (((c22 * x1 - c12 * x2) / det).to_point(), ((c11 * x2 - c12 * x1) / det).to_point());
|
||||
if !(p1.is_finite() && p2.is_finite()) {
|
||||
break;
|
||||
}
|
||||
fit = Some((p1 - p0, p2 - p3));
|
||||
// One Newton step towards the nearest point of the curve for each point of the side.
|
||||
let c = CubicBez::new(p0, p1, p2, p3);
|
||||
let (a, b, k) = (p1 - p0, p2 - p1, p3 - 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
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// split [`GradientMesh::insert_row`] and [`GradientMesh::insert_col`] make, so deleting a mesh
|
||||
/// line keeps the curve of the border it crossed (#3187). The share is the split parameter, read
|
||||
/// from the lengths of the middle handles. When the edges no longer fit one cubic (a point or a
|
||||
/// handle was moved since the split), the cubic grown back would not pass through the middle
|
||||
/// point, and the merged edge is straight.
|
||||
fn merged_handles(a: (Point, Vec2), mid: (Point, Vec2, Vec2), b: (Point, Vec2)) -> (Vec2, Vec2) {
|
||||
let straight = ((b.0 - a.0) / 3.0, (a.0 - b.0) / 3.0);
|
||||
let (la, lb) = (mid.1.hypot(), mid.2.hypot());
|
||||
let t = la / (la + lb);
|
||||
if !(0.01..=0.99).contains(&t) {
|
||||
return straight;
|
||||
}
|
||||
let (ha, hb) = (a.1 / t, b.1 / (1.0 - t));
|
||||
let through = CubicBez::new(a.0, a.0 + ha, b.0 + hb, b.0).eval(t);
|
||||
let tol = 1e-3 * (a.0.distance(mid.0) + mid.0.distance(b.0)).max(1e-9);
|
||||
if ha.is_finite() && hb.is_finite() && through.distance(mid.0) <= tol { (ha, hb) } else { straight }
|
||||
}
|
||||
|
||||
/// `0, 1/n, …, 1` for `n` (clamped to 1..=200) even steps.
|
||||
fn even_steps(n: u32) -> Vec<f64> {
|
||||
let n = n.clamp(1, 200);
|
||||
@@ -170,11 +249,22 @@ pub(super) fn grid_len(rows: u32, cols: u32) -> Option<usize> {
|
||||
}
|
||||
|
||||
impl GradientMesh {
|
||||
/// The most quads one tessellation ([`Self::quads`]) makes, and so the most patches a mesh can
|
||||
/// have: about 170 bytes a quad, so about 170 MB. A file's mesh can have any number of patches
|
||||
/// and the view any zoom; the subdivision per patch gives way before memory does, and a mesh
|
||||
/// with more patches than that is malformed, like a point count that doesn't fit (#3592).
|
||||
pub const MAX_QUADS: usize = 1 << 20;
|
||||
|
||||
pub fn idx(&self, r: usize, c: usize) -> usize {
|
||||
r * (self.cols as usize + 1) + c
|
||||
}
|
||||
pub fn is_valid(&self) -> bool {
|
||||
self.rows >= 1 && self.cols >= 1 && grid_len(self.rows, self.cols) == Some(self.points.len())
|
||||
self.rows >= 1 && self.cols >= 1 && grid_len(self.rows, self.cols) == Some(self.points.len()) && self.patches() <= Self::MAX_QUADS
|
||||
}
|
||||
|
||||
/// The number of patches (rows times columns), saturating.
|
||||
fn patches(&self) -> usize {
|
||||
(self.rows as usize).saturating_mul(self.cols as usize)
|
||||
}
|
||||
|
||||
/// A 1×1 mesh from its four corners (bottom-left, top-left, top-right, bottom-right in u/v
|
||||
@@ -263,11 +353,54 @@ impl GradientMesh {
|
||||
/// 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)?;
|
||||
Some(Self::from_grid(&even_steps(cols), &even_steps(rows), &|u, v| coons.eval(u, v), &|_, _, p| color_at(p)))
|
||||
let (us, vs) = (even_steps(cols), even_steps(rows));
|
||||
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)
|
||||
}
|
||||
|
||||
/// Give each edge on the mesh's border the handles that make it follow the outline it stands
|
||||
/// for, in place of the handles [`Self::from_grid`] estimates from the surface's derivatives
|
||||
/// (#3187). Where two border edges meet along a side, their handles are made collinear, so the
|
||||
/// border stays smooth there; the four corners keep the handles their own edges were fitted with.
|
||||
fn fit_border(&mut self, coons: &Coons, us: &[f64], vs: &[f64]) {
|
||||
let (rows, cols) = (self.rows as usize, self.cols as usize);
|
||||
if !self.is_valid() || us.len() != cols + 1 || vs.len() != rows + 1 {
|
||||
return;
|
||||
}
|
||||
let edges = [(0, &coons.top), (rows, &coons.bottom)]
|
||||
.map(|(r, side)| (r, side, true))
|
||||
.into_iter()
|
||||
.chain([(0, &coons.left), (cols, &coons.right)].map(|(c, side)| (c, side, false)));
|
||||
for (line, side, along_u) in edges {
|
||||
let (n, steps) = if along_u { (cols, us) } else { (rows, vs) };
|
||||
for k in 0..n {
|
||||
let (a, b, out, back) = if along_u {
|
||||
(self.idx(line, k), self.idx(line, k + 1), H_RIGHT, H_LEFT)
|
||||
} else {
|
||||
(self.idx(k, line), self.idx(k + 1, line), H_DOWN, H_UP)
|
||||
};
|
||||
if let Some((h1, h2)) = fit_edge(side, (steps[k], steps[k + 1]), (self.points[a].p, self.points[b].p)) {
|
||||
self.points[a].handles[out] = h1;
|
||||
self.points[b].handles[back] = h2;
|
||||
}
|
||||
}
|
||||
for k in 1..n {
|
||||
let (i, out, back) = if along_u { (self.idx(line, k), H_RIGHT, H_LEFT) } else { (self.idx(k, line), H_DOWN, H_UP) };
|
||||
let h = &mut self.points[i].handles;
|
||||
let (fwd, bwd) = (h[out], h[back]);
|
||||
let dir = (fwd.normalize() - bwd.normalize()).normalize();
|
||||
if fwd.hypot2() > 0.0 && bwd.hypot2() > 0.0 && dir.is_finite() {
|
||||
h[out] = dir * fwd.hypot();
|
||||
h[back] = -dir * bwd.hypot();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Create Gradient Mesh → Appearance: lighten the points towards white, to the centre or to
|
||||
/// the edges, by at most `highlight` %.
|
||||
/// 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 h = (highlight / 100.0).clamp(0.0, 1.0) as f32;
|
||||
let (rows, cols) = (self.rows as usize, self.cols as usize);
|
||||
@@ -282,7 +415,8 @@ impl GradientMesh {
|
||||
};
|
||||
if w > 0.0 {
|
||||
let i = self.idx(r, c);
|
||||
self.points[i].color = lerp_color(&self.points[i].color, &Color::WHITE, w);
|
||||
let color = self.points[i].color;
|
||||
self.points[i].color = lerp_color(&color, &Color::WHITE.in_model(color.model()), w);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -345,13 +479,20 @@ impl GradientMesh {
|
||||
(Color::rgb(rgb[0], rgb[1], rgb[2]), op)
|
||||
}
|
||||
|
||||
/// Tessellate into `n`×`n` quads per patch, each coloured at its centre and its corners.
|
||||
/// The quads per patch side [`Self::quads`] cuts at when `n` is asked: 1 to 64, and coarser
|
||||
/// where the patches at `n` would pass [`Self::MAX_QUADS`] (a quad per patch is the least).
|
||||
fn subdivision(&self, n: usize) -> usize {
|
||||
n.clamp(1, 64).min(((Self::MAX_QUADS / self.patches().max(1)) as f64).sqrt() as usize).max(1)
|
||||
}
|
||||
|
||||
/// Tessellate into `n`×`n` quads per patch ([`Self::subdivision`]), each coloured at its centre
|
||||
/// and its corners.
|
||||
pub fn quads(&self, n: usize) -> Vec<MeshQuad> {
|
||||
if !self.is_valid() {
|
||||
return vec![];
|
||||
}
|
||||
let n = n.clamp(1, 64);
|
||||
let mut out = Vec::with_capacity(self.rows as usize * self.cols as usize * n * n);
|
||||
let n = self.subdivision(n);
|
||||
let mut out = Vec::with_capacity(self.patches() * n * n);
|
||||
let mut grid = vec![Point::ZERO; (n + 1) * (n + 1)];
|
||||
let mut colors = vec![(Color::default(), 0.0f32); (n + 1) * (n + 1)];
|
||||
for r in 0..self.rows as usize {
|
||||
@@ -381,6 +522,105 @@ impl GradientMesh {
|
||||
out
|
||||
}
|
||||
|
||||
/// Flat-coloured pieces covering the mesh, for outputs that can't shade a mesh smoothly (SVG,
|
||||
/// Expand): each patch cut into `n`×`n` quads as by [`Self::quads`], then neighbouring quads
|
||||
/// joined into square blocks as large as keeps the colour across each block within about one
|
||||
/// 8-bit step. A patch of one colour is one piece; a patch whose colour changes a lot keeps
|
||||
/// all its quads. A piece's outline runs through its quads' outer corners, so it covers what
|
||||
/// they covered, and it takes the colour at its centre.
|
||||
pub fn pieces(&self, n: usize) -> Vec<MeshPiece> {
|
||||
let n = self.subdivision(n);
|
||||
let quads = self.quads(n);
|
||||
let cols = (self.cols as usize).max(1);
|
||||
let mut out = Vec::with_capacity(quads.len());
|
||||
for (k, patch) in quads.chunks_exact(n * n).enumerate() {
|
||||
let (r, c) = (k / cols, k % cols);
|
||||
let side = self.block_side(r, c, n, patch);
|
||||
if side == 1 {
|
||||
out.extend(patch.iter().map(|q| MeshPiece { pts: q.pts.to_vec(), color: q.color, opacity: q.opacity }));
|
||||
continue;
|
||||
}
|
||||
// Tessellation point (i, j) of the patch (0..=n each), from a quad it is a corner of.
|
||||
let at = |i: usize, j: usize| -> Option<Point> {
|
||||
let (qi, qj) = (i.min(n - 1), j.min(n - 1));
|
||||
let q = patch.get(qj * n + qi)?;
|
||||
q.pts
|
||||
.get(match (i > qi, j > qj) {
|
||||
(false, false) => 0,
|
||||
(true, false) => 1,
|
||||
(true, true) => 2,
|
||||
(false, true) => 3,
|
||||
})
|
||||
.copied()
|
||||
};
|
||||
for j0 in (0..n).step_by(side) {
|
||||
for i0 in (0..n).step_by(side) {
|
||||
let (i1, j1) = (i0 + side, j0 + side);
|
||||
// Around the block, in the quads' order: along its top, down its right side,
|
||||
// back along its bottom and up its left side.
|
||||
let ring = (i0..i1)
|
||||
.map(|i| (i, j0))
|
||||
.chain((j0..j1).map(|j| (i1, j)))
|
||||
.chain((i0 + 1..=i1).rev().map(|i| (i, j1)))
|
||||
.chain((j0 + 1..=j1).rev().map(|j| (i0, j)));
|
||||
let Some(pts) = ring.map(|(i, j)| at(i, j)).collect::<Option<Vec<Point>>>() else { continue };
|
||||
let half = side as f64 / 2.0;
|
||||
let (color, opacity) = self.color_at(r, c, (i0 as f64 + half) / n as f64, (j0 as f64 + half) / n as f64);
|
||||
out.push(MeshPiece { pts, color, opacity });
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The side, in quads, of the blocks [`Self::pieces`] joins patch (r, c) into, cut into
|
||||
/// `n`×`n` `quads`: the largest divisor of `n` that keeps the colour changing by at most one
|
||||
/// 8-bit step across a block. 1 (no joining) where the patch folds over itself, as the outline
|
||||
/// of a block across the fold wouldn't fill it as its quads do.
|
||||
fn block_side(&self, r: usize, c: usize, n: usize, quads: &[MeshQuad]) -> usize {
|
||||
let corner = |dr: usize, dc: usize| self.points.get(self.idx(r + dr, c + dc));
|
||||
let (Some(p00), Some(p01), Some(p10), Some(p11)) = (corner(0, 0), corner(0, 1), corner(1, 0), corner(1, 1)) else {
|
||||
return 1;
|
||||
};
|
||||
// Corners all in CMYK mix in their inks ([`Self::color_at`]); the RGB they show is checked too.
|
||||
let inks = [p00, p01, p10, p11].iter().all(|p| matches!(p.color, Color::Cmyk { .. }));
|
||||
let channels = |p: &MeshPoint| -> Vec<f32> {
|
||||
let mut v = p.color.to_rgb().to_vec();
|
||||
if inks && let Color::Cmyk { c, m, y, k } = p.color {
|
||||
v.extend([c, m, y, k]);
|
||||
}
|
||||
v.push(p.opacity);
|
||||
v
|
||||
};
|
||||
let [a, b, cc, d] = [p00, p01, p10, p11].map(channels);
|
||||
// A bilinear patch changes across a block of side h (0..1 of the patch) by at most
|
||||
// h × (du + dv) in each channel, du and dv its largest change along each parameter.
|
||||
let mut steps = 0.0f32;
|
||||
for k in 0..a.len() {
|
||||
let ch = |v: &[f32]| v.get(k).copied().unwrap_or_default();
|
||||
let du = (ch(&a) - ch(&b)).abs().max((ch(&cc) - ch(&d)).abs());
|
||||
let dv = (ch(&a) - ch(&cc)).abs().max((ch(&b) - ch(&d)).abs());
|
||||
steps = steps.max((du + dv) * 255.0);
|
||||
}
|
||||
if !steps.is_finite() {
|
||||
return 1;
|
||||
}
|
||||
let side = (1..=n).rev().find(|&s| n.is_multiple_of(s) && (n / s) as f32 >= steps).unwrap_or(1);
|
||||
if side > 1 {
|
||||
// Every quad turning the same way: the patch doesn't fold, so a block's outline
|
||||
// encloses just what its quads cover.
|
||||
let turn = |q: &MeshQuad| {
|
||||
let [a, b, c, d] = q.pts;
|
||||
(b - a).cross(c - a) + (c - a).cross(d - a)
|
||||
};
|
||||
let first = quads.first().map(turn).unwrap_or_default();
|
||||
if !quads.iter().all(|q| turn(q) * first > 0.0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
side
|
||||
}
|
||||
|
||||
/// The mesh's outer boundary as a closed path.
|
||||
pub fn outline(&self) -> PathData {
|
||||
if !self.is_valid() {
|
||||
@@ -464,12 +704,21 @@ impl GradientMesh {
|
||||
|
||||
/// Which patch and local (u, v) contains `p` (sampled search; `None` outside the mesh).
|
||||
pub fn locate(&self, p: Point) -> Option<(usize, usize, f64, f64)> {
|
||||
self.locate_within(p, 0.0)
|
||||
}
|
||||
|
||||
/// As [`Self::locate`], and for a point outside the mesh within `tol` of its outline, the
|
||||
/// patch and local (u, v) of the nearest point of the mesh's edge.
|
||||
pub fn locate_within(&self, p: Point, tol: f64) -> Option<(usize, usize, f64, f64)> {
|
||||
if !self.is_valid() {
|
||||
return None;
|
||||
}
|
||||
let bp = self.outline().to_bezpath();
|
||||
if bp.winding(p) == 0 {
|
||||
return None;
|
||||
if tol.is_nan() || tol <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
return self.nearest_edge(p).filter(|e| e.0 <= tol * tol).map(|(_, r, c, u, v)| (r, c, u, v));
|
||||
}
|
||||
const N: usize = 16;
|
||||
let mut best = (f64::INFINITY, 0, 0, 0.0, 0.0);
|
||||
@@ -506,6 +755,30 @@ impl GradientMesh {
|
||||
Some((r, c, u, v))
|
||||
}
|
||||
|
||||
/// The point of the mesh's outer edge nearest `p`: its squared distance from `p`, and its
|
||||
/// boundary patch and local (u, v).
|
||||
fn nearest_edge(&self, p: Point) -> Option<(f64, usize, usize, f64, f64)> {
|
||||
use vectorcraft_geom::kurbo::ParamCurveNearest;
|
||||
let (rows, cols) = (self.rows as usize, self.cols as usize);
|
||||
let mut best: Option<(f64, usize, usize, f64, f64)> = None;
|
||||
let mut take = |e: CubicBez, at: (usize, usize, Option<f64>, Option<f64>)| {
|
||||
let n = e.nearest(p, 1e-9);
|
||||
if best.is_none_or(|b| n.distance_sq < b.0) {
|
||||
let (r, c, u, v) = at;
|
||||
best = Some((n.distance_sq, r, c, u.unwrap_or(n.t), v.unwrap_or(n.t)));
|
||||
}
|
||||
};
|
||||
for c in 0..cols {
|
||||
take(self.edge_u(0, c), (0, c, None, Some(0.0)));
|
||||
take(self.edge_u(rows, c), (rows - 1, c, None, Some(1.0)));
|
||||
}
|
||||
for r in 0..rows {
|
||||
take(self.edge_v(r, 0), (r, 0, Some(0.0), None));
|
||||
take(self.edge_v(r, cols), (r, cols - 1, Some(1.0), None));
|
||||
}
|
||||
best
|
||||
}
|
||||
|
||||
/// Index of the mesh point within `tol` of `p`.
|
||||
pub fn point_near(&self, p: Point, tol: f64) -> Option<usize> {
|
||||
self.points
|
||||
@@ -593,9 +866,10 @@ impl GradientMesh {
|
||||
self.cols += 1;
|
||||
}
|
||||
|
||||
/// Mesh tool click: add a row and a column through `p` (inside the mesh). Returns the new point index.
|
||||
pub fn add_lines_at(&mut self, p: Point) -> Option<usize> {
|
||||
let (r, c, u, v) = self.locate(p)?;
|
||||
/// Mesh tool click: add a row and a column through `p` (inside the mesh, or outside within
|
||||
/// `tol` of its edge, through the nearest point of the edge). Returns the new point index.
|
||||
pub fn add_lines_at(&mut self, p: Point, tol: f64) -> Option<usize> {
|
||||
let (r, c, u, v) = self.locate_within(p, tol)?;
|
||||
let mut rr = r;
|
||||
let mut cc = c;
|
||||
let row_new = v > 0.02 && v < 0.98;
|
||||
@@ -630,10 +904,11 @@ impl GradientMesh {
|
||||
let above = self.idx(r - 1, cc);
|
||||
let below = self.idx(r + 1, cc);
|
||||
let mid = self.idx(r, cc);
|
||||
let (pa, pm, pb) = (self.points[above].p, self.points[mid].p, self.points[below].p);
|
||||
let _ = pm;
|
||||
self.points[above].handles[H_DOWN] = (pb - pa) / 3.0;
|
||||
self.points[below].handles[H_UP] = (pa - pb) / 3.0;
|
||||
let (pa, pb, m) = (self.points[above].p, self.points[below].p, &self.points[mid]);
|
||||
let hm = (m.p, m.handles[H_UP], m.handles[H_DOWN]);
|
||||
let (down, up) = merged_handles((pa, self.points[above].handles[H_DOWN]), hm, (pb, self.points[below].handles[H_UP]));
|
||||
self.points[above].handles[H_DOWN] = down;
|
||||
self.points[below].handles[H_UP] = up;
|
||||
}
|
||||
self.points.drain(r * w..(r + 1) * w);
|
||||
self.rows -= 1;
|
||||
@@ -644,9 +919,11 @@ impl GradientMesh {
|
||||
let rows = self.rows as usize;
|
||||
for rr in 0..=rows {
|
||||
let (l, rt) = (rr * w + c - 1, rr * w + c + 1);
|
||||
let (pl, pr) = (self.points[l].p, self.points[rt].p);
|
||||
self.points[l].handles[H_RIGHT] = (pr - pl) / 3.0;
|
||||
self.points[rt].handles[H_LEFT] = (pl - pr) / 3.0;
|
||||
let (pl, pr, m) = (self.points[l].p, self.points[rt].p, &self.points[rr * w + c]);
|
||||
let hm = (m.p, m.handles[H_LEFT], m.handles[H_RIGHT]);
|
||||
let (right, left) = merged_handles((pl, self.points[l].handles[H_RIGHT]), hm, (pr, self.points[rt].handles[H_LEFT]));
|
||||
self.points[l].handles[H_RIGHT] = right;
|
||||
self.points[rt].handles[H_LEFT] = left;
|
||||
}
|
||||
let mut k = 0;
|
||||
self.points.retain(|_| {
|
||||
|
||||
@@ -250,9 +250,13 @@ fn node_bytes(n: &Node, other: Option<&Node>) -> usize {
|
||||
if let Some(e) = n.extras_box() {
|
||||
b += ALLOC + size_of::<NodeExtras>();
|
||||
b += boxed(&e.mask) + boxed(&e.graph) + boxed(&e.shaper) + boxed(&e.attrs) + boxed(&e.slice) + boxed(&e.perspective);
|
||||
b += boxed(&e.dimension) + boxed(&e.live_paint_gaps);
|
||||
b += boxed(&e.dimension) + boxed(&e.live_paint_gaps) + boxed(&e.colorize);
|
||||
b += e.trace.as_ref().map_or(0, |t| ALLOC + size_of::<Value>() + json_bytes(t));
|
||||
b += e.intertwine.as_deref().map_or(0, |a| ALLOC + size_of::<PathData>() + path_bytes(a));
|
||||
if e.intertwine_tiers.capacity() > 0 {
|
||||
b += ALLOC + e.intertwine_tiers.capacity() * size_of::<PathData>() + e.intertwine_tiers.iter().map(path_bytes).sum::<usize>();
|
||||
}
|
||||
b += e.symbol_overrides.capacity() * size_of::<crate::SymbolOverride>();
|
||||
b += e.objects_on_path.as_deref().map_or(0, |o| {
|
||||
ALLOC + size_of::<crate::objects_on_path::ObjectsOnPath>() + o.members.capacity() * size_of::<crate::objects_on_path::Member>()
|
||||
});
|
||||
|
||||
@@ -44,11 +44,28 @@ pub struct NodeExtras {
|
||||
/// Object › Intertwine: as a member of an Intertwine group, the area (document coordinates,
|
||||
/// filled non-zero) where this object is drawn over the group's other members.
|
||||
pub intertwine: Option<Box<PathData>>,
|
||||
/// Object › Intertwine: as a member of an Intertwine group, the areas (document coordinates,
|
||||
/// filled non-zero) where this object is drawn over members that are themselves drawn over
|
||||
/// others there. Item `k` is the area of tier `k + 2`; [`NodeExtras::intertwine`] is tier 1
|
||||
/// (see [`crate::intertwine`]).
|
||||
pub intertwine_tiers: Vec<PathData>,
|
||||
/// Live Paint group: its Gap Options (Object › Live Paint › Gap Options; `None`: the defaults).
|
||||
pub live_paint_gaps: Option<Box<crate::live_paint::GapOptions>>,
|
||||
/// Object › Objects on Path: the group's path and the objects attached to it (see
|
||||
/// [`crate::objects_on_path`]).
|
||||
pub objects_on_path: Option<Box<crate::objects_on_path::ObjectsOnPath>>,
|
||||
/// A grayscale or 1-bit image's fill color (`paint.setFill` on the image): a solid paint that
|
||||
/// colorizes it. Each pixel is drawn in this color, as opaque as the pixel is dark, so white
|
||||
/// pixels are transparent. `None`: the image shows its own pixels.
|
||||
pub colorize: Option<Box<vectorcraft_color::Paint>>,
|
||||
/// Pathfinder panel › Make Compound Shape: this group is a compound shape
|
||||
/// ([`Node::is_compound_shape`], [`crate::compound_shape`]).
|
||||
pub compound_shape: bool,
|
||||
/// As a member of a compound shape, how this object combines with the members below it.
|
||||
pub shape_mode: crate::ShapeMode,
|
||||
/// As an instance of a dynamic symbol, its own appearance changes to objects of the symbol's
|
||||
/// art (see [`crate::symbol_overrides`]); empty for none.
|
||||
pub symbol_overrides: Vec<crate::SymbolOverride>,
|
||||
}
|
||||
|
||||
/// No extras: what [`Node::extras`] gives for a node without any.
|
||||
@@ -67,8 +84,13 @@ pub(super) static NO_EXTRAS: NodeExtras = NodeExtras {
|
||||
perspective: None,
|
||||
dimension: None,
|
||||
intertwine: None,
|
||||
intertwine_tiers: Vec::new(),
|
||||
live_paint_gaps: None,
|
||||
objects_on_path: None,
|
||||
colorize: None,
|
||||
compound_shape: false,
|
||||
shape_mode: crate::ShapeMode::Add,
|
||||
symbol_overrides: Vec::new(),
|
||||
};
|
||||
|
||||
/// A node's [`NodeExtras`], if it has any ([`Node::extras`]). Extras that are all unset equal
|
||||
|
||||
@@ -127,6 +127,7 @@ impl Node {
|
||||
NodeKind::Layer { .. } => "Layer",
|
||||
NodeKind::Group { clip: true, .. } => "Clip Group",
|
||||
NodeKind::Group { .. } if self.extras().objects_on_path.is_some() => "Objects on Path",
|
||||
NodeKind::Group { .. } if self.extras().compound_shape => "Compound Shape",
|
||||
NodeKind::Group { .. } => "Group",
|
||||
NodeKind::Path { live: Some(l), .. } => l.label(),
|
||||
NodeKind::Path { guide: true, .. } => "Guide",
|
||||
@@ -197,7 +198,8 @@ impl Node {
|
||||
/// ([`Appearance::outset`]) instead of measuring along their shape. An image's box takes in
|
||||
/// the shapes its own fills and strokes paint ([`crate::hit::set_image_paint`]). Groups, layers
|
||||
/// without a clipping mask and Intertwine groups take in their members' boxes, grown by their
|
||||
/// own strokes. Symbol instances are measured as in [`Node::visual_bounds`].
|
||||
/// own strokes. Symbol instances are measured as in [`Node::visual_bounds`]. A blend's box
|
||||
/// takes in where its spine can put its keys and steps.
|
||||
pub fn reach_bounds(&self) -> Option<Rect> {
|
||||
self.reach(None)
|
||||
}
|
||||
@@ -233,6 +235,11 @@ impl Node {
|
||||
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 } => {
|
||||
vectorcraft_geom::union_opt(self.visual(symbols), crate::blend::rail_reach(children, spec, symbols))
|
||||
}
|
||||
_ => self.visual(symbols),
|
||||
}
|
||||
}
|
||||
@@ -495,8 +502,13 @@ impl Node {
|
||||
Arc::make_mut(&mut m.art).transform_scaled(a, sc);
|
||||
}
|
||||
// The area where an Intertwine member is drawn over the others moves with it.
|
||||
if let Some(area) = self.extras_if_any().and_then(|e| e.intertwine.as_mut()) {
|
||||
area.transform(a);
|
||||
if let Some(e) = self.extras_if_any() {
|
||||
if let Some(area) = e.intertwine.as_mut() {
|
||||
area.transform(a);
|
||||
}
|
||||
for area in &mut e.intertwine_tiers {
|
||||
area.transform(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -545,9 +557,16 @@ impl Node {
|
||||
/// item) stands in for the topmost fill or stroke when it is of the proxy's kind. Otherwise
|
||||
/// type objects paint their runs (the first run speaks for all) in text space, fitted to the
|
||||
/// layout bounds; everything else paints its fill or stroke in the document, fitted to the
|
||||
/// geometric bounds. Strokes fit the box grown by half their weight.
|
||||
/// geometric bounds. Strokes fit the box grown by half their weight. A colorized image shows
|
||||
/// its colorize paint as its fill.
|
||||
pub fn proxy_paint(&self, stroke: bool, item: Option<usize>) -> Option<(&vectorcraft_color::Paint, Affine, Rect)> {
|
||||
let item = self.appearance.item_of_kind(item, !stroke);
|
||||
if item.is_none()
|
||||
&& !stroke
|
||||
&& let Some(c) = self.extras().colorize.as_deref()
|
||||
{
|
||||
return Some((c, Affine::IDENTITY, self.geometric_bounds().unwrap_or(Rect::ZERO)));
|
||||
}
|
||||
if item.is_none()
|
||||
&& let NodeKind::Text(t) = &self.kind
|
||||
{
|
||||
@@ -675,9 +694,15 @@ impl Node {
|
||||
|
||||
impl Node {
|
||||
/// Whether this object shows any transparency, itself or anything inside it: object, fill or
|
||||
/// stroke opacity below 100%, a blend mode, an opacity mask, effects or transparent gradient
|
||||
/// stops. Hidden objects show none.
|
||||
/// stroke opacity below 100%, a blend mode, an opacity mask, effects, transparent gradient
|
||||
/// stops or a colorized image. Hidden objects show none.
|
||||
pub fn shows_transparency(&self) -> bool {
|
||||
self.shows_transparency_with(&|_| true)
|
||||
}
|
||||
|
||||
/// [`Self::shows_transparency`], counting a colorized image by itself only when `colorize`
|
||||
/// accepts it, for files that write some colorized images in their colorize paint (EPS).
|
||||
pub fn shows_transparency_with(&self, colorize: &dyn Fn(&Node) -> bool) -> bool {
|
||||
use vectorcraft_color::Paint;
|
||||
let see_through = |p: &Paint| matches!(p, Paint::Gradient(g) if g.gradient.stops.iter().any(|s| s.opacity < 1.0));
|
||||
let effects = |fx: &[crate::Effect]| fx.iter().any(|e| e.visible);
|
||||
@@ -685,13 +710,14 @@ impl Node {
|
||||
&& (self.opacity < 1.0
|
||||
|| self.blend != BlendMode::Normal
|
||||
|| self.extras().mask.as_ref().is_some_and(|m| !m.disabled)
|
||||
|| (matches!(self.kind, NodeKind::Image(_)) && self.extras().colorize.is_some() && colorize(self))
|
||||
|| effects(&self.appearance.effects)
|
||||
|| self
|
||||
.appearance
|
||||
.items
|
||||
.iter()
|
||||
.any(|i| i.visible() && (i.opacity() < 1.0 || i.blend() != BlendMode::Normal || see_through(i.paint()) || effects(i.effects())))
|
||||
|| self.children().is_some_and(|ch| ch.iter().any(|c| c.shows_transparency())))
|
||||
|| self.children().is_some_and(|ch| ch.iter().any(|c| c.shows_transparency_with(colorize))))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ fn extras_are_written_flat_in_order() {
|
||||
e.slice = Some(Box::default());
|
||||
e.perspective = Some(Box::new(crate::PerspectiveAttachment::new("left", 1.0)));
|
||||
e.live_paint_gaps = Some(Box::default());
|
||||
e.compound_shape = true;
|
||||
e.shape_mode = crate::ShapeMode::Exclude;
|
||||
}
|
||||
/// The top-level keys of a JSON object, in the order written.
|
||||
struct Keys(Vec<String>);
|
||||
@@ -61,8 +63,11 @@ fn extras_are_written_flat_in_order() {
|
||||
"bbox_angle",
|
||||
"perspective",
|
||||
"live_paint_gaps",
|
||||
"compound_shape",
|
||||
"shape_mode",
|
||||
];
|
||||
assert_eq!(keys, order);
|
||||
assert_eq!(v["shape_mode"], "exclude");
|
||||
assert_eq!(serde_json::from_value::<Node>(v).unwrap(), n);
|
||||
let plain = Node::path(NodeId(9), shapes::rectangle(Rect::new(0.0, 0.0, 4.0, 2.0)), Appearance::default_art());
|
||||
let back: Node = serde_json::from_value(serde_json::to_value(&plain).unwrap()).unwrap();
|
||||
@@ -334,6 +339,35 @@ fn transform_group_recurses() {
|
||||
assert_eq!(child.appearance.stroke_width(), 2.0);
|
||||
}
|
||||
|
||||
/// A symbol instance in perspective keeps its picture moved by a transform, and loses its
|
||||
/// projection when no invertible map of finite numbers draws the moved picture.
|
||||
#[test]
|
||||
fn a_projection_follows_the_transform_or_is_removed() {
|
||||
use vectorcraft_geom::{Homography, Point};
|
||||
let instance = |h: Homography| {
|
||||
let mut n = Node::new(NodeId(1), NodeKind::SymbolInstance { symbol: "S".into(), xf: Affine::IDENTITY });
|
||||
n.extras_mut().perspective =
|
||||
Some(Box::new(crate::PerspectiveAttachment { projection: Some(h.to_array()), ..crate::PerspectiveAttachment::new("left", 0.0) }));
|
||||
n
|
||||
};
|
||||
let projection = |n: &Node| n.extras().perspective.as_deref().and_then(|p| p.projection);
|
||||
// A move by 10 scaled by 1e307: moved by a scale by 10, the product overflows unless scaled down.
|
||||
let big = Homography([[1e307, 0.0, 1e308], [0.0, 1e307, 0.0], [0.0, 0.0, 1e307]]);
|
||||
let mut n = instance(big);
|
||||
n.transform(Affine::scale(10.0), false);
|
||||
let p = Point::new(3.0, 7.0);
|
||||
assert!(n.projection().unwrap().apply(p).unwrap().distance(Point::new(103.0, 7.0)) < 1e-9);
|
||||
// A move by 1e200 moved by a scale by 1e150 sends every point to about (1e350, 0).
|
||||
let far = Homography([[1.0, 0.0, 1e200], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]);
|
||||
let mut n = instance(far);
|
||||
n.transform(Affine::scale(1e150), false);
|
||||
assert_eq!(projection(&n), None);
|
||||
// A transform that is not invertible leaves the projection as it is.
|
||||
let mut n = instance(far);
|
||||
n.transform(Affine::scale(0.0), false);
|
||||
assert_eq!(projection(&n), Some(far.to_array()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unplaced_gradients_pin_only_when_a_refit_would_differ() {
|
||||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, Paint};
|
||||
@@ -450,6 +484,23 @@ fn objects_are_read_within_the_node_budget() {
|
||||
assert!(with_node_budget(3, read).is_ok());
|
||||
}
|
||||
|
||||
/// An Intertwine member's areas of tier 2 and up are written and read back, and each one counts
|
||||
/// against [`with_node_budget`] as an object does, so a file of many empty areas is refused.
|
||||
#[test]
|
||||
fn intertwine_tiers_are_read_within_the_node_budget() {
|
||||
let mut n = Node::group(NodeId(1), vec![]);
|
||||
let area = vectorcraft_geom::shapes::rectangle(vectorcraft_geom::Rect::new(0.0, 0.0, 10.0, 10.0));
|
||||
n.extras_mut().intertwine_tiers = vec![PathData::default(), area];
|
||||
let json = serde_json::to_string(&n).unwrap();
|
||||
assert!(json.contains("\"intertwine_tiers\""), "{json}");
|
||||
let read = || serde_json::from_str::<Node>(&json);
|
||||
assert_eq!(with_node_budget(3, read).unwrap(), n, "the object and its two areas fit a budget of three");
|
||||
let why = with_node_budget(2, read).unwrap_err().to_string();
|
||||
assert!(why.contains("more than 2 objects"), "{why}");
|
||||
let plain = serde_json::to_string(&Node::group(NodeId(1), vec![])).unwrap();
|
||||
assert!(!plain.contains("intertwine_tiers"), "an object without tiers writes none: {plain}");
|
||||
}
|
||||
|
||||
/// #3587: fields read before an object's `type` are buffered value by value within
|
||||
/// [`with_buffered_value_budget`], each counted once: a group read back from the buffer buffers
|
||||
/// its own fields again without counting them twice. VectorCraft writes `type` first, which
|
||||
|
||||
@@ -5,6 +5,12 @@ use super::kind::NodeKind;
|
||||
use super::opacity::{Knockout, OpacityMask};
|
||||
use super::{Appearance, BlendMode, Deserialize, Node, PathData, Serialize, one, yes};
|
||||
|
||||
/// Field `name` of a [`Node`] written as `v`, or skipped. A function of its own, so the long field
|
||||
/// list stays one straight run of calls.
|
||||
fn write_field<S: serde::ser::SerializeStruct, T: Serialize + ?Sized>(st: &mut S, skip: bool, name: &'static str, v: &T) -> Result<(), S::Error> {
|
||||
if skip { st.skip_field(name) } else { st.serialize_field(name, v) }
|
||||
}
|
||||
|
||||
/// How a [`Node`] is written: the flat field list of the file format, in its order, borrowed from
|
||||
/// the node and its extras (nothing is copied). The node reads back through `NodeWire`.
|
||||
impl Serialize for Node {
|
||||
@@ -40,18 +46,19 @@ impl Serialize for Node {
|
||||
e.perspective.is_none(),
|
||||
e.dimension.is_none(),
|
||||
e.intertwine.is_none(),
|
||||
e.intertwine_tiers.is_empty(),
|
||||
e.live_paint_gaps.is_none(),
|
||||
e.objects_on_path.is_none(),
|
||||
e.colorize.is_none(),
|
||||
is_default(&e.compound_shape),
|
||||
is_default(&e.shape_mode),
|
||||
e.symbol_overrides.is_empty(),
|
||||
];
|
||||
let mut st = s.serialize_struct("Node", skips.iter().filter(|s| !**s).count())?;
|
||||
let mut skip = skips.into_iter();
|
||||
macro_rules! field {
|
||||
($name:literal, $v:expr) => {
|
||||
if skip.next().unwrap_or(false) {
|
||||
st.skip_field($name)?;
|
||||
} else {
|
||||
st.serialize_field($name, $v)?;
|
||||
}
|
||||
write_field(&mut st, skip.next().unwrap_or(false), $name, $v)?;
|
||||
};
|
||||
}
|
||||
field!("id", &self.id);
|
||||
@@ -80,8 +87,13 @@ impl Serialize for Node {
|
||||
field!("perspective", &e.perspective);
|
||||
field!("dimension", &e.dimension);
|
||||
field!("intertwine", &e.intertwine);
|
||||
field!("intertwine_tiers", &e.intertwine_tiers);
|
||||
field!("live_paint_gaps", &e.live_paint_gaps);
|
||||
field!("objects_on_path", &e.objects_on_path);
|
||||
field!("colorize", &e.colorize);
|
||||
field!("compound_shape", &e.compound_shape);
|
||||
field!("shape_mode", &e.shape_mode);
|
||||
field!("symbol_overrides", &e.symbol_overrides);
|
||||
st.end()
|
||||
}
|
||||
}
|
||||
@@ -115,11 +127,39 @@ pub fn with_node_budget<T>(max: usize, f: impl FnOnce() -> T) -> T {
|
||||
/// each counts once, and the id usually comes first, before the node's children are read.
|
||||
fn counted_id<'de, D: serde::Deserializer<'de>>(d: D) -> Result<NodeId, D::Error> {
|
||||
let id = NodeId::deserialize(d)?;
|
||||
count_one()?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// Count one object against [`with_node_budget`].
|
||||
fn count_one<E: serde::de::Error>() -> Result<(), E> {
|
||||
if let Some((left, max)) = NODES_LEFT.get() {
|
||||
let left = left.checked_sub(1).ok_or_else(|| serde::de::Error::custom(format!("the document has more than {max} objects")))?;
|
||||
let left = left.checked_sub(1).ok_or_else(|| E::custom(format!("the document has more than {max} objects")))?;
|
||||
NODES_LEFT.set(Some((left, max)));
|
||||
}
|
||||
Ok(id)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A list read from a file (a member's Intertwine areas of tier 2 and up, an instance's symbol
|
||||
/// overrides), each item counting against [`with_node_budget`] as an object does: a compressed
|
||||
/// file of repeated empty items stays small while they take memory once read.
|
||||
fn counted_list<'de, D: serde::Deserializer<'de>, T: Deserialize<'de>>(d: D) -> Result<Vec<T>, D::Error> {
|
||||
struct Items<T>(std::marker::PhantomData<T>);
|
||||
impl<'de, T: Deserialize<'de>> serde::de::Visitor<'de> for Items<T> {
|
||||
type Value = Vec<T>;
|
||||
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.write_str("a list")
|
||||
}
|
||||
fn visit_seq<A: serde::de::SeqAccess<'de>>(self, mut seq: A) -> Result<Vec<T>, A::Error> {
|
||||
let mut out = Vec::new();
|
||||
while let Some(item) = seq.next_element::<T>()? {
|
||||
count_one()?;
|
||||
out.push(item);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
d.deserialize_seq(Items(std::marker::PhantomData))
|
||||
}
|
||||
|
||||
/// How a [`Node`] is read: the flat field list of the file format. Fields move into the node
|
||||
@@ -178,10 +218,20 @@ pub(super) struct NodeWire {
|
||||
dimension: Option<Box<crate::dimension::Dimension>>,
|
||||
#[serde(default)]
|
||||
intertwine: Option<Box<PathData>>,
|
||||
#[serde(default, deserialize_with = "counted_list")]
|
||||
intertwine_tiers: Vec<PathData>,
|
||||
#[serde(default)]
|
||||
live_paint_gaps: Option<Box<crate::live_paint::GapOptions>>,
|
||||
#[serde(default)]
|
||||
objects_on_path: Option<Box<crate::objects_on_path::ObjectsOnPath>>,
|
||||
#[serde(default)]
|
||||
colorize: Option<Box<vectorcraft_color::Paint>>,
|
||||
#[serde(default)]
|
||||
compound_shape: bool,
|
||||
#[serde(default)]
|
||||
shape_mode: crate::ShapeMode,
|
||||
#[serde(default, deserialize_with = "counted_list")]
|
||||
symbol_overrides: Vec<crate::SymbolOverride>,
|
||||
}
|
||||
|
||||
impl From<NodeWire> for Node {
|
||||
@@ -201,8 +251,13 @@ impl From<NodeWire> for Node {
|
||||
perspective: w.perspective,
|
||||
dimension: w.dimension,
|
||||
intertwine: w.intertwine,
|
||||
intertwine_tiers: w.intertwine_tiers,
|
||||
live_paint_gaps: w.live_paint_gaps,
|
||||
objects_on_path: w.objects_on_path,
|
||||
colorize: w.colorize,
|
||||
compound_shape: w.compound_shape,
|
||||
shape_mode: w.shape_mode,
|
||||
symbol_overrides: w.symbol_overrides,
|
||||
};
|
||||
Node {
|
||||
id: w.id,
|
||||
|
||||
@@ -337,7 +337,7 @@ mod tests {
|
||||
// The art is a 40 × 10 box to the right of and below the origin, not the 20 × 20 square
|
||||
// around it that instances are measured by without the symbols.
|
||||
let mut d = crate::Document::new(400.0, 400.0);
|
||||
d.symbols.push(crate::Symbol { name: "Bar".into(), art: Arc::new(rect(9, Rect::new(0.0, 0.0, 40.0, 10.0))) });
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "Bar".into(), art: Arc::new(rect(9, Rect::new(0.0, 0.0, 40.0, 10.0))) });
|
||||
let line = Node::path(NodeId(1), shapes::line(Point::new(100.0, 300.0), Point::new(300.0, 300.0)), Default::default());
|
||||
let instance = Node::new(NodeId(2), NodeKind::SymbolInstance { symbol: "Bar".into(), xf: Affine::IDENTITY });
|
||||
let other = rect(3, Rect::new(0.0, 0.0, 10.0, 10.0));
|
||||
|
||||
@@ -200,7 +200,7 @@ mod tests {
|
||||
fn oriented_bounds_hug_a_rotated_symbol_instances_art() {
|
||||
let mut d = Document::new(500.0, 500.0);
|
||||
let bar = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(0.0, 0.0, 40.0, 10.0)), Appearance::default_art());
|
||||
d.symbols.push(crate::Symbol { name: "Bar".into(), art: std::sync::Arc::new(bar) });
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "Bar".into(), art: std::sync::Arc::new(bar) });
|
||||
let (l, id) = (d.layers[0].id, d.alloc_id());
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Bar".into(), xf: Affine::translate((100.0, 100.0)) })).unwrap();
|
||||
let c = Point::new(120.0, 105.0);
|
||||
|
||||
@@ -58,12 +58,14 @@ impl Node {
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep a projection drawing the same picture moved by `a` (the flat art moves by `a` too).
|
||||
/// Keep a projection drawing the same picture moved by `a` (the flat art moves by `a` too). An
|
||||
/// invertible `a` that the projection cannot follow removes it, and the art is drawn flat.
|
||||
pub(crate) fn transform_projection(&mut self, a: vectorcraft_geom::Affine) {
|
||||
if let Some(p) = self.extras_if_any().and_then(|e| e.perspective.as_deref_mut())
|
||||
&& let Some(h) = p.homography().and_then(|h| h.conjugated(a))
|
||||
&& let Some(h) = p.homography()
|
||||
&& a.determinant().abs() >= 1e-15
|
||||
{
|
||||
p.projection = Some(h.to_array());
|
||||
p.projection = h.conjugated(a).map(|h| h.to_array());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,61 @@ impl ImageBlob {
|
||||
}
|
||||
}
|
||||
|
||||
/// How a grayscale image's file stores its pixels ([`ImageBlob::gray_source`]).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct GraySource {
|
||||
/// One bit a pixel: black and white only.
|
||||
pub one_bit: bool,
|
||||
/// The file has an alpha channel.
|
||||
pub alpha: bool,
|
||||
}
|
||||
|
||||
impl ImageBlob {
|
||||
/// Whether the image's file stores grayscale or 1-bit pixels (the images a fill color
|
||||
/// colorizes), and how; `None` for color images and data that isn't an image. Reads only its
|
||||
/// header.
|
||||
pub fn gray_source(&self) -> Option<GraySource> {
|
||||
let gray = |one_bit: bool, alpha: bool| Some(GraySource { one_bit, alpha });
|
||||
if let Some((kind, _)) = crate::cmyk::tiff(&self.bytes) {
|
||||
return match kind {
|
||||
tiff::ColorType::Gray(bits) => gray(bits == 1, false),
|
||||
tiff::ColorType::GrayA(bits) => gray(bits == 1, true),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
if crate::cmyk::jpeg_components(&self.bytes) == Some(1) {
|
||||
return gray(false, false);
|
||||
}
|
||||
use image::{ExtendedColorType as E, ImageDecoder as _};
|
||||
let reader = image::ImageReader::new(Cursor::new(self.bytes.as_slice())).with_guessed_format().ok()?;
|
||||
match reader.into_decoder().ok()?.original_color_type() {
|
||||
E::L1 => gray(true, false),
|
||||
E::La1 => gray(true, true),
|
||||
E::L2 | E::L4 | E::L8 | E::L16 => gray(false, false),
|
||||
E::La2 | E::La4 | E::La8 | E::La16 => gray(false, true),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// How much of a colorize paint each pixel shows, row by row from the top: how dark the pixel
|
||||
/// is (255 less its Rec. 709 luma) scaled by its alpha, so white and transparent pixels are 0.
|
||||
/// Returns the width, the height and one byte a pixel; `None` when the image can't be decoded
|
||||
/// within [`MAX_RASTER_ALLOC`].
|
||||
pub fn coverage(&self) -> Option<(u32, u32, Vec<u8>)> {
|
||||
let img = rgba_within(&self.bytes, MAX_RASTER_ALLOC)?;
|
||||
let (w, h) = img.dimensions();
|
||||
let cover = img
|
||||
.pixels()
|
||||
.map(|p| {
|
||||
let [r, g, b, a] = p.0;
|
||||
let luma = 0.2126 * r as f32 + 0.7152 * g as f32 + 0.0722 * b as f32;
|
||||
((255.0 - luma).clamp(0.0, 255.0) * a as f32 / 255.0).round() as u8
|
||||
})
|
||||
.collect();
|
||||
Some((w, h, cover))
|
||||
}
|
||||
}
|
||||
|
||||
impl ImageBlob {
|
||||
/// The colour around pixel (`x`, `y`) (pixel space): the alpha-weighted average of the `size` ×
|
||||
/// `size` pixels centred on it, clipped to the image, as straight RGBA. `None` outside the
|
||||
@@ -298,4 +353,66 @@ mod tests {
|
||||
assert!(b.map_rgb(|c| c).is_none(), "nothing changed");
|
||||
assert!(ImageBlob::png(vec![1, 2, 3]).map_rgb(|c| c).is_none(), "undecodable");
|
||||
}
|
||||
|
||||
/// An uncompressed 1-bit TIFF, BlackIsZero, of `w` × `h` pixels: `rows` holds each row's bits,
|
||||
/// padded to a byte, most significant bit first.
|
||||
fn bilevel_tiff(w: u16, h: u16, rows: &[u8]) -> Vec<u8> {
|
||||
// Tag, type (3 short, 4 long) and value of each entry, in tag order.
|
||||
let data_at = 8 + 2 + 9 * 12 + 4;
|
||||
let entries: [(u16, u16, u32); 9] = [
|
||||
(256, 3, w.into()),
|
||||
(257, 3, h.into()),
|
||||
(258, 3, 1),
|
||||
(259, 3, 1),
|
||||
(262, 3, 1),
|
||||
(273, 4, data_at),
|
||||
(277, 3, 1),
|
||||
(278, 3, h.into()),
|
||||
(279, 4, rows.len() as u32),
|
||||
];
|
||||
let mut out = b"II*\0".to_vec();
|
||||
out.extend_from_slice(&8u32.to_le_bytes());
|
||||
out.extend_from_slice(&(entries.len() as u16).to_le_bytes());
|
||||
for (tag, kind, value) in entries {
|
||||
out.extend_from_slice(&tag.to_le_bytes());
|
||||
out.extend_from_slice(&kind.to_le_bytes());
|
||||
out.extend_from_slice(&1u32.to_le_bytes());
|
||||
out.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
out.extend_from_slice(&0u32.to_le_bytes());
|
||||
out.extend_from_slice(rows);
|
||||
out
|
||||
}
|
||||
|
||||
fn png_of(img: image::DynamicImage) -> ImageBlob {
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
ImageBlob::png(png)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grayscale_and_1_bit_files_are_told_from_color_ones() {
|
||||
let gray = |one_bit, alpha| Some(GraySource { one_bit, alpha });
|
||||
let l8 = png_of(image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(2, 1, vec![0, 255]).unwrap()));
|
||||
assert_eq!(l8.gray_source(), gray(false, false));
|
||||
let la8 = png_of(image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(1, 1, vec![0, 128]).unwrap()));
|
||||
assert_eq!(la8.gray_source(), gray(false, true));
|
||||
let mut jpeg = Vec::new();
|
||||
jpeg_encoder::Encoder::new(&mut jpeg, 90).encode(&[0, 255, 0, 255], 2, 2, jpeg_encoder::ColorType::Luma).unwrap();
|
||||
assert_eq!(ImageBlob::new("image/jpeg", jpeg).gray_source(), gray(false, false));
|
||||
// Black, white; white, black.
|
||||
let tif = ImageBlob::new("image/tiff", bilevel_tiff(2, 2, &[0b0100_0000, 0b1000_0000]));
|
||||
assert_eq!(tif.gray_source(), gray(true, false));
|
||||
assert_eq!(tif.coverage(), Some((2, 2, vec![255, 0, 0, 255])), "black covers, white doesn't");
|
||||
// Color images and data that isn't an image.
|
||||
assert_eq!(blob(&[[0, 0, 0, 255], [9, 9, 9, 255]], 2, 1).gray_source(), None);
|
||||
assert_eq!(ImageBlob::png(vec![1, 2, 3]).gray_source(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coverage_is_darkness_times_alpha() {
|
||||
let la8 = png_of(image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(4, 1, vec![0, 255, 255, 255, 51, 255, 0, 128]).unwrap()));
|
||||
assert_eq!(la8.coverage(), Some((4, 1, vec![255, 0, 204, 128])));
|
||||
assert_eq!(ImageBlob::png(vec![1, 2, 3]).coverage(), None);
|
||||
}
|
||||
}
|
||||
@@ -667,7 +667,7 @@ mod tests {
|
||||
pixels: None,
|
||||
})),
|
||||
);
|
||||
d.symbols.push(Symbol { name: "Star".into(), art: Arc::new(art) });
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Star".into(), art: Arc::new(art) });
|
||||
d.images.insert("pic".into(), ImageBlob::new("image/png", vec![1, 2, 3]));
|
||||
let mut children = vec![Arc::new(Node::new(NodeId(51), NodeKind::SymbolInstance { symbol: "Star".into(), xf: Affine::IDENTITY }))];
|
||||
if bytes.starts_with(b"nest") {
|
||||
@@ -702,7 +702,11 @@ mod tests {
|
||||
fn the_art_and_its_resources_join_only_a_prepared_copy_under_their_own_names() {
|
||||
set_loader(stub_loader);
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
d.symbols.push(Symbol { name: "Star".into(), art: Arc::new(Node::new(NodeId(60), NodeKind::Group { children: vec![], clip: false })) });
|
||||
d.symbols.push(Symbol {
|
||||
dynamic: false,
|
||||
name: "Star".into(),
|
||||
art: Arc::new(Node::new(NodeId(60), NodeKind::Group { children: vec![], clip: false })),
|
||||
});
|
||||
d.images.insert("doc1-1".into(), ImageBlob::new("application/json", b"nest".to_vec()));
|
||||
let p = placed("doc1-1");
|
||||
let layer = d.layers[0].id;
|
||||
|
||||
@@ -18,8 +18,8 @@ pub trait ColorVisitor {
|
||||
fn image(&mut self, key: &str) -> Option<String>;
|
||||
}
|
||||
|
||||
/// What a recolouring pass reaches: fills (with gradient-mesh points), strokes and the pixels of
|
||||
/// embedded images.
|
||||
/// What a recolouring pass reaches: fills (including colorized image fills and gradient-mesh
|
||||
/// points), strokes and the pixels of embedded images without a colorize paint.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Reach {
|
||||
pub fill: bool,
|
||||
@@ -33,11 +33,18 @@ impl Reach {
|
||||
}
|
||||
|
||||
/// Recolour `n` and everything inside it through `v`: the paints of its fills and strokes and of
|
||||
/// its text runs, its gradient-mesh points and an embedded image's pixels (linked images stay
|
||||
/// as they are). Members that change are replaced by copies; the others stay shared. Returns the
|
||||
/// number of paints, meshes and images that changed.
|
||||
/// its text runs, its gradient-mesh points and an embedded image's pixels (linked and colorized
|
||||
/// images keep their pixels). Members that change are replaced by copies; the others stay shared.
|
||||
/// Returns the number of paints, meshes and images that changed.
|
||||
pub fn recolor_node(n: &mut Node, reach: Reach, v: &mut dyn ColorVisitor) -> usize {
|
||||
let mut changed = 0;
|
||||
let colorized = matches!(&n.kind, NodeKind::Image(_)) && n.extras().colorize.is_some();
|
||||
if colorized
|
||||
&& reach.fill
|
||||
&& let Some(paint) = n.extras_mut().colorize.as_deref_mut()
|
||||
{
|
||||
changed += v.paint(paint) as usize;
|
||||
}
|
||||
for it in &mut n.appearance.items {
|
||||
match it {
|
||||
AppearanceItem::Fill(l) if reach.fill => changed += v.paint(&mut l.paint) as usize,
|
||||
@@ -67,7 +74,7 @@ pub fn recolor_node(n: &mut Node, reach: Reach, v: &mut dyn ColorVisitor) -> usi
|
||||
}
|
||||
// An image holding its own pixels is a glyph's (emoji in outlined type): it keeps its
|
||||
// colours, as the glyph does in type.
|
||||
NodeKind::Image(im) if reach.images && im.link.is_none() && im.pixels.is_none() => {
|
||||
NodeKind::Image(im) if reach.images && !colorized && im.link.is_none() && im.pixels.is_none() => {
|
||||
if let Some(k) = v.image(&im.key) {
|
||||
im.key = k;
|
||||
changed += 1;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{Arc, Deserialize, Node, NodeId, Point, Serialize};
|
||||
use super::{Affine, Arc, Deserialize, Node, NodeId, Point, Serialize};
|
||||
|
||||
/// New art on a locked artboard ([`Document::new_art`]). Setting it changes the document in
|
||||
/// place, so a selection change that sets it doesn't replace the document.
|
||||
@@ -44,6 +44,29 @@ pub struct MaskEdit {
|
||||
pub layer: NodeId,
|
||||
}
|
||||
|
||||
/// Symbol editing mode: the art of symbol `symbol` lives on the temporary `layer` while editing,
|
||||
/// placed by `xf` (from the symbol's coordinates to the document's), and goes back into the symbol
|
||||
/// after each edit, so every instance follows it.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SymbolEdit {
|
||||
pub symbol: String,
|
||||
pub layer: NodeId,
|
||||
/// The instance editing was entered from (a double-click on it), selected again on leaving after an edit;
|
||||
/// `None` when it was entered from the Symbols panel.
|
||||
pub instance: Option<NodeId>,
|
||||
pub xf: Affine,
|
||||
/// The symbol's art is a group whose members are on the layer: the group takes them back.
|
||||
pub group: bool,
|
||||
/// An edit changed the art since editing was entered.
|
||||
pub changed: bool,
|
||||
/// The isolated object from before editing was entered, isolated again on leaving.
|
||||
#[serde(default)]
|
||||
pub back_isolation: Option<NodeId>,
|
||||
/// The current layer from before editing was entered, current again on leaving.
|
||||
#[serde(default)]
|
||||
pub back_layer: Option<NodeId>,
|
||||
}
|
||||
|
||||
/// A saved view (View → New View…): zoom, centre and rotation, listed at the bottom of the View menu.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SavedView {
|
||||
@@ -108,6 +131,10 @@ impl SavedSelection {
|
||||
pub struct Symbol {
|
||||
pub name: String,
|
||||
pub art: Arc<Node>,
|
||||
/// Symbol Options › Symbol Type › Dynamic Symbol: each instance may change the appearance of
|
||||
/// objects in the art and stay linked (see [`crate::symbol_overrides`]). Static by default.
|
||||
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
|
||||
pub dynamic: bool,
|
||||
}
|
||||
|
||||
/// Encoded image bytes (PNG/JPEG/WebP…) shared by image objects.
|
||||
|
||||
+148
-32
@@ -107,14 +107,17 @@ impl Quotes {
|
||||
/// Single-quote pairs offered by Document Setup.
|
||||
pub const SINGLE_CHOICES: &[[char; 2]] = &[['‘', '’'], ['‚', '‘'], ['‚', '’'], ['’', '’'], ['‹', '›'], ['›', '‹'], ['『', '』'], ['\'', '\'']];
|
||||
|
||||
/// Is `c` one of these opening quotes?
|
||||
/// Is `c` one of these opening quotes? A typographic glyph that is also a closing quote does not
|
||||
/// count; a straight quote has no direction, so it still opens.
|
||||
fn is_open(&self, c: char) -> bool {
|
||||
c == self.double[0] || c == self.single[0]
|
||||
(c == self.double[0] || c == self.single[0]) && (c.is_ascii() || (c != self.double[1] && c != self.single[1]))
|
||||
}
|
||||
|
||||
/// A quote opens after nothing, whitespace, an opening bracket, a dash or another opening quote.
|
||||
fn opens(&self, prev: Option<char>) -> bool {
|
||||
prev.is_none_or(|p| p.is_whitespace() || matches!(p, '(' | '[' | '{' | '—' | '–') || self.is_open(p))
|
||||
/// `quote_opened` says whether `prev` is a quote this conversion just produced, and if so whether
|
||||
/// it opened; that settles glyphs which serve as both opening and closing quote.
|
||||
fn opens(&self, prev: Option<char>, quote_opened: Option<bool>) -> bool {
|
||||
prev.is_none_or(|p| p.is_whitespace() || matches!(p, '(' | '[' | '{' | '—' | '–') || quote_opened.unwrap_or_else(|| self.is_open(p)))
|
||||
}
|
||||
|
||||
/// `text` with straight quotes (`"` and `'`) replaced by these quotes: opening or closing by
|
||||
@@ -122,11 +125,16 @@ impl Quotes {
|
||||
/// and is updated to its last character, so text can be converted in pieces.
|
||||
pub fn apply(&self, text: &str, prev: &mut Option<char>) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
let mut quote_opened = None;
|
||||
for c in text.chars() {
|
||||
let r = match c {
|
||||
'"' => self.double[usize::from(!self.opens(*prev))],
|
||||
'\'' => self.single[usize::from(!self.opens(*prev))],
|
||||
c => c,
|
||||
quote_opened = match c {
|
||||
'"' | '\'' => Some(self.opens(*prev, quote_opened)),
|
||||
_ => None,
|
||||
};
|
||||
let r = match (c, quote_opened) {
|
||||
('"', Some(open)) => self.double[usize::from(!open)],
|
||||
('\'', Some(open)) => self.single[usize::from(!open)],
|
||||
(c, _) => c,
|
||||
};
|
||||
out.push(r);
|
||||
*prev = Some(r);
|
||||
@@ -135,34 +143,74 @@ impl Quotes {
|
||||
}
|
||||
}
|
||||
|
||||
/// Document languages and the quote characters each uses (choosing a language in Document Setup
|
||||
/// also picks its quotes).
|
||||
pub const LANGUAGES: &[(&str, Quotes)] = &[
|
||||
("English: USA", Quotes::ENGLISH),
|
||||
("English: UK", Quotes { double: ['“', '”'], single: ['‘', '’'] }),
|
||||
("German", Quotes { double: ['„', '“'], single: ['‚', '‘'] }),
|
||||
("German: Swiss", Quotes { double: ['«', '»'], single: ['‹', '›'] }),
|
||||
("French", Quotes { double: ['«', '»'], single: ['‹', '›'] }),
|
||||
("Italian", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Spanish", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Catalan", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Portuguese", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Dutch", Quotes { double: ['“', '”'], single: ['‘', '’'] }),
|
||||
("Danish", Quotes { double: ['»', '«'], single: ['›', '‹'] }),
|
||||
("Swedish", Quotes { double: ['”', '”'], single: ['’', '’'] }),
|
||||
("Finnish", Quotes { double: ['”', '”'], single: ['’', '’'] }),
|
||||
("Norwegian", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Polish", Quotes { double: ['„', '”'], single: ['‚', '’'] }),
|
||||
("Czech", Quotes { double: ['„', '“'], single: ['‚', '‘'] }),
|
||||
("Russian", Quotes { double: ['«', '»'], single: ['„', '“'] }),
|
||||
("Turkish", Quotes { double: ['“', '”'], single: ['‘', '’'] }),
|
||||
("Japanese", Quotes { double: ['「', '」'], single: ['『', '』'] }),
|
||||
("Chinese", Quotes { double: ['“', '”'], single: ['‘', '’'] }),
|
||||
/// Document and character languages: the name, the BCP 47 tag text is shaped in (so that fonts use
|
||||
/// their language's forms, OpenType `locl`) and the quote characters each uses (choosing a
|
||||
/// language in Document Setup also picks its quotes). The first is the default.
|
||||
pub const LANGUAGES: &[(&str, &str, Quotes)] = &[
|
||||
("English: USA", "en-US", Quotes::ENGLISH),
|
||||
("English: UK", "en-GB", Quotes { double: ['“', '”'], single: ['‘', '’'] }),
|
||||
("German", "de", Quotes { double: ['„', '“'], single: ['‚', '‘'] }),
|
||||
("German: Swiss", "de-CH", Quotes { double: ['«', '»'], single: ['‹', '›'] }),
|
||||
("French", "fr", Quotes { double: ['«', '»'], single: ['‹', '›'] }),
|
||||
("Italian", "it", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Spanish", "es", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Catalan", "ca", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Portuguese", "pt", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Dutch", "nl", Quotes { double: ['“', '”'], single: ['‘', '’'] }),
|
||||
("Danish", "da", Quotes { double: ['»', '«'], single: ['›', '‹'] }),
|
||||
("Swedish", "sv", Quotes { double: ['”', '”'], single: ['’', '’'] }),
|
||||
("Finnish", "fi", Quotes { double: ['”', '”'], single: ['’', '’'] }),
|
||||
("Norwegian", "nb", Quotes { double: ['«', '»'], single: ['‘', '’'] }),
|
||||
("Polish", "pl", Quotes { double: ['„', '”'], single: ['‚', '’'] }),
|
||||
("Czech", "cs", Quotes { double: ['„', '“'], single: ['‚', '‘'] }),
|
||||
("Russian", "ru", Quotes { double: ['«', '»'], single: ['„', '“'] }),
|
||||
// „…” outside, ’…’ inside: Pravopis srpskoga jezika (Matica srpska, 2010, ISBN
|
||||
// 978-86-7946-079-0, т. 168, pp. 133–135) and CLDR
|
||||
// (https://github.com/unicode-org/cldr/blob/main/common/main/sr.xml, `delimiters`).
|
||||
("Serbian: Cyrillic", "sr-Cyrl", Quotes { double: ['„', '”'], single: ['’', '’'] }),
|
||||
("Serbian: Latin", "sr-Latn", Quotes { double: ['„', '”'], single: ['’', '’'] }),
|
||||
// „…“ outside, ‚…‘ inside: CLDR (https://github.com/unicode-org/cldr/blob/main/common/main/hr.xml,
|
||||
// `delimiters`).
|
||||
("Croatian", "hr", Quotes { double: ['„', '“'], single: ['‚', '‘'] }),
|
||||
("Turkish", "tr", Quotes { double: ['“', '”'], single: ['‘', '’'] }),
|
||||
("Japanese", "ja", Quotes { double: ['「', '」'], single: ['『', '』'] }),
|
||||
("Chinese", "zh", Quotes { double: ['“', '”'], single: ['‘', '’'] }),
|
||||
("Hindi", "hi", Quotes::ENGLISH),
|
||||
("Marathi", "mr", Quotes::ENGLISH),
|
||||
("Gujarati", "gu", Quotes::ENGLISH),
|
||||
("Bengali", "bn", Quotes::ENGLISH),
|
||||
("Punjabi", "pa", Quotes::ENGLISH),
|
||||
("Tamil", "ta", Quotes::ENGLISH),
|
||||
("Telugu", "te", Quotes::ENGLISH),
|
||||
("Kannada", "kn", Quotes::ENGLISH),
|
||||
("Malayalam", "ml", Quotes::ENGLISH),
|
||||
("Odia", "or", Quotes::ENGLISH),
|
||||
("Urdu", "ur", Quotes::ENGLISH),
|
||||
];
|
||||
|
||||
/// The quotes of a [`LANGUAGES`] entry (case-insensitive).
|
||||
pub fn language_quotes(language: &str) -> Option<Quotes> {
|
||||
LANGUAGES.iter().find(|(l, _)| l.eq_ignore_ascii_case(language)).map(|(_, q)| *q)
|
||||
LANGUAGES.iter().find(|(l, _, _)| l.eq_ignore_ascii_case(language)).map(|(_, _, q)| *q)
|
||||
}
|
||||
|
||||
/// The [`LANGUAGES`] entry named or tagged `language` (case-insensitive) as a character's
|
||||
/// language ([`crate::CharStyle::language`]) stores it: its tag, or `None` for the default (the
|
||||
/// first entry); `Err` for a language not in the list.
|
||||
pub fn char_language(language: &str) -> Result<Option<&'static str>, String> {
|
||||
let i = LANGUAGES
|
||||
.iter()
|
||||
.position(|(n, t, _)| n.eq_ignore_ascii_case(language) || t.eq_ignore_ascii_case(language))
|
||||
.ok_or_else(|| format!("unknown language `{language}` (one of {})", LANGUAGES.iter().map(|(n, _, _)| *n).collect::<Vec<_>>().join(", ")))?;
|
||||
Ok(LANGUAGES.get(i).filter(|_| i > 0).map(|(_, t, _)| *t))
|
||||
}
|
||||
|
||||
/// The name of the language a character's stored `language` tag stands for: the default's for
|
||||
/// none, `None` for a tag not in [`LANGUAGES`] (from a file).
|
||||
pub fn char_language_name(language: Option<&str>) -> Option<&'static str> {
|
||||
match language {
|
||||
None => LANGUAGES.first().map(|(n, _, _)| *n),
|
||||
Some(tag) => LANGUAGES.iter().find(|(_, t, _)| t.eq_ignore_ascii_case(tag)).map(|(n, _, _)| *n),
|
||||
}
|
||||
}
|
||||
|
||||
/// The transparency flattener preset Document Setup uses unless another is chosen.
|
||||
@@ -300,9 +348,19 @@ impl DocSetup {
|
||||
pub fn keep_in_range(&mut self) {
|
||||
self.bleed = bleed_in_range(self.bleed);
|
||||
for m in [&mut self.superscript, &mut self.subscript] {
|
||||
// `clamp` passes NaN through, so a non-finite value falls back to the default first.
|
||||
if !m.size.is_finite() {
|
||||
m.size = ScriptMetrics::DEFAULT.size;
|
||||
}
|
||||
if !m.position.is_finite() {
|
||||
m.position = ScriptMetrics::DEFAULT.position;
|
||||
}
|
||||
m.size = m.size.clamp(1.0, MAX_SCRIPT);
|
||||
m.position = m.position.clamp(-MAX_SCRIPT, MAX_SCRIPT);
|
||||
}
|
||||
if !self.small_caps_size.is_finite() {
|
||||
self.small_caps_size = DocSetup::default().small_caps_size;
|
||||
}
|
||||
self.small_caps_size = self.small_caps_size.clamp(1.0, MAX_SMALL_CAPS);
|
||||
}
|
||||
}
|
||||
@@ -311,6 +369,31 @@ impl DocSetup {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Characters store a language of the list by its tag, the default as none.
|
||||
#[test]
|
||||
fn char_languages_are_stored_by_tag() {
|
||||
assert_eq!(char_language("Turkish"), Ok(Some("tr")));
|
||||
assert_eq!(char_language("MR"), Ok(Some("mr")));
|
||||
assert_eq!(char_language("english: usa"), Ok(None));
|
||||
assert_eq!(char_language("en-US"), Ok(None));
|
||||
assert!(char_language("Klingon").is_err() && char_language("").is_err());
|
||||
assert_eq!(
|
||||
(char_language_name(None), char_language_name(Some("gu")), char_language_name(Some("x-unknown"))),
|
||||
(Some("English: USA"), Some("Gujarati"), None)
|
||||
);
|
||||
// Serbian in either script, and Croatian.
|
||||
assert_eq!(char_language("Serbian: Cyrillic"), Ok(Some("sr-Cyrl")));
|
||||
assert_eq!(char_language("sr-latn"), Ok(Some("sr-Latn")));
|
||||
assert_eq!(char_language("Croatian"), Ok(Some("hr")));
|
||||
assert_eq!(char_language_name(Some("sr-Cyrl")), Some("Serbian: Cyrillic"));
|
||||
// Every entry has a tag of its own, and a name of its own.
|
||||
for (i, (n, t, _)) in LANGUAGES.iter().enumerate() {
|
||||
assert!(LANGUAGES.iter().skip(i + 1).all(|(n2, t2, _)| n2 != n && t2 != t), "{n} {t}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Straight quotes become the language's: opening or closing by what comes before, inner
|
||||
/// quotes as single ones, and the context carried across pieces converted apart.
|
||||
#[test]
|
||||
fn quotes_open_and_close_by_context() {
|
||||
let mut prev = None;
|
||||
@@ -319,6 +402,13 @@ mod tests {
|
||||
let mut prev = Some('(');
|
||||
assert_eq!(german.apply("\"a\"", &mut prev), "„a“");
|
||||
assert_eq!(prev, Some('“'));
|
||||
// Serbian closes high (”), with ’ on both sides of an inner quote; Croatian as German.
|
||||
for name in ["Serbian: Cyrillic", "Serbian: Latin"] {
|
||||
let mut prev = None;
|
||||
assert_eq!(language_quotes(name).unwrap().apply("\"a 'b' c\"", &mut prev), "„a ’b’ c”", "{name}");
|
||||
}
|
||||
let mut prev = None;
|
||||
assert_eq!(language_quotes("croatian").unwrap().apply("\"a 'b' c\"", &mut prev), "„a ‚b‘ c“");
|
||||
// Converting in pieces keeps the context.
|
||||
let mut prev = None;
|
||||
let a = Quotes::ENGLISH.apply("\"", &mut prev);
|
||||
@@ -326,6 +416,18 @@ mod tests {
|
||||
assert_eq!((a.as_str(), b.as_str()), ("“", "“"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_quote_equal_to_other_kinds_opening_quote_still_closes() {
|
||||
let q = Quotes { double: ['”', '”'], single: ['‘', '’'] };
|
||||
let mut prev = None;
|
||||
assert_eq!(q.apply("'\"a\"'", &mut prev), "‘”a”’");
|
||||
let q = Quotes { double: ['"', '"'], single: ['‘', '’'] };
|
||||
let mut prev = None;
|
||||
assert_eq!(q.apply("'\"a\"'", &mut prev), "‘\"a\"’");
|
||||
let mut prev = None;
|
||||
assert_eq!(q.apply("\"'a'\"", &mut prev), "\"‘a’\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bleed_grows_the_artboard() {
|
||||
let s = DocSetup { bleed: [9.0, 9.0, 4.0, 2.0], ..Default::default() };
|
||||
@@ -333,6 +435,20 @@ mod tests {
|
||||
assert!(s.has_bleed() && !DocSetup::default().has_bleed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keep_in_range_replaces_non_finite_type_values() {
|
||||
let mut s = DocSetup::default();
|
||||
s.superscript.size = f64::NAN;
|
||||
s.superscript.position = f64::INFINITY;
|
||||
s.subscript.size = f64::NEG_INFINITY;
|
||||
s.subscript.position = f64::NAN;
|
||||
s.small_caps_size = f64::NAN;
|
||||
s.keep_in_range();
|
||||
for v in [s.superscript.size, s.superscript.position, s.subscript.size, s.subscript.position, s.small_caps_size] {
|
||||
assert!(v.is_finite(), "{v}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presets_and_defaults() {
|
||||
let d = DocSetup::default();
|
||||
|
||||
@@ -229,13 +229,14 @@ fn item_paint_mut(it: &mut AppearanceItem) -> &mut Paint {
|
||||
}
|
||||
}
|
||||
|
||||
/// The paints a node holds itself (not its children): appearance items, then text runs.
|
||||
/// The paints a node holds itself (not its children): appearance items, then text runs, then an
|
||||
/// image's colorize paint.
|
||||
pub fn node_paints(n: &Node) -> impl Iterator<Item = &Paint> {
|
||||
let runs = match &n.kind {
|
||||
NodeKind::Text(t) => t.runs.as_slice(),
|
||||
_ => &[],
|
||||
};
|
||||
n.appearance.items.iter().map(item_paint).chain(runs.iter().flat_map(|r| [&r.style.fill, &r.style.stroke]))
|
||||
n.appearance.items.iter().map(item_paint).chain(runs.iter().flat_map(|r| [&r.style.fill, &r.style.stroke])).chain(n.extras().colorize.as_deref())
|
||||
}
|
||||
|
||||
/// Mutable [`node_paints`], in the same order.
|
||||
@@ -244,7 +245,8 @@ pub fn node_paints_mut(n: &mut Node) -> impl Iterator<Item = &mut Paint> {
|
||||
NodeKind::Text(t) => t.runs.as_mut_slice(),
|
||||
_ => &mut [],
|
||||
};
|
||||
n.appearance.items.iter_mut().map(item_paint_mut).chain(runs.iter_mut().flat_map(|r| [&mut r.style.fill, &mut r.style.stroke]))
|
||||
let colorize = n.extras.0.as_deref_mut().and_then(|e| e.colorize.as_deref_mut());
|
||||
n.appearance.items.iter_mut().map(item_paint_mut).chain(runs.iter_mut().flat_map(|r| [&mut r.style.fill, &mut r.style.stroke])).chain(colorize)
|
||||
}
|
||||
|
||||
/// `n` with `f` applied to its subtree's linkable colours, or `None` when nothing changed (then
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
//! Dynamic symbols (Symbol Options › Symbol Type › Dynamic Symbol): an instance of a dynamic
|
||||
//! symbol keeps appearance changes of its own to objects in the symbol's art, while the art stays
|
||||
//! the symbol's. Editing the symbol reshapes every instance and each keeps its changes; Reset drops
|
||||
//! them. The changes are stored on the instance ([`crate::node::NodeExtras::symbol_overrides`]) and
|
||||
//! applied wherever an instance is drawn or exported ([`dynamic_art`]).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use vectorcraft_color::{BlendMode, Paint};
|
||||
|
||||
use crate::{Node, NodeKind};
|
||||
|
||||
/// Deepest object an override may name: groups nested this deep in the symbol's art.
|
||||
pub const MAX_PATH_DEPTH: usize = 64;
|
||||
|
||||
/// One instance's appearance changes to one object of its dynamic symbol's art.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SymbolOverride {
|
||||
/// The object: child indices (paint order) from the art's root down through its groups; empty
|
||||
/// for the root itself.
|
||||
#[serde(deserialize_with = "short_path")]
|
||||
pub path: Vec<u32>,
|
||||
/// The object's (top) fill paint; type takes it as its characters' fill.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub fill: Option<Paint>,
|
||||
/// The object's (top) stroke paint; type takes it as its characters' stroke.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub stroke: Option<Paint>,
|
||||
/// The object's opacity, 0..1.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub opacity: Option<f32>,
|
||||
/// The object's blending mode.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub blend: Option<BlendMode>,
|
||||
}
|
||||
|
||||
impl SymbolOverride {
|
||||
/// Does this override change nothing?
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.fill.is_none() && self.stroke.is_none() && self.opacity.is_none() && self.blend.is_none()
|
||||
}
|
||||
|
||||
/// Put this override's changes on object `n`.
|
||||
fn apply(&self, n: &mut Node) {
|
||||
if let NodeKind::Text(t) = &mut n.kind {
|
||||
for r in &mut t.runs {
|
||||
if let Some(p) = &self.fill {
|
||||
r.style.fill = p.clone();
|
||||
}
|
||||
if let Some(p) = &self.stroke {
|
||||
r.style.stroke = p.clone();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if let Some(p) = &self.fill {
|
||||
n.appearance.set_fill(p.clone());
|
||||
}
|
||||
if let Some(p) = &self.stroke {
|
||||
n.appearance.set_stroke(p.clone());
|
||||
}
|
||||
}
|
||||
if let Some(o) = self.opacity.filter(|o| o.is_finite()) {
|
||||
n.opacity = o.clamp(0.0, 1.0);
|
||||
}
|
||||
if let Some(b) = self.blend {
|
||||
n.blend = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An override's path read from a file: at most [`MAX_PATH_DEPTH`] indices, so a compressed file
|
||||
/// of one endless path stays small once read.
|
||||
fn short_path<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Vec<u32>, D::Error> {
|
||||
struct Path;
|
||||
impl<'de> serde::de::Visitor<'de> for Path {
|
||||
type Value = Vec<u32>;
|
||||
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.write_str("a list of child indices")
|
||||
}
|
||||
fn visit_seq<A: serde::de::SeqAccess<'de>>(self, mut seq: A) -> Result<Vec<u32>, A::Error> {
|
||||
let mut out = Vec::new();
|
||||
while let Some(i) = seq.next_element::<u32>()? {
|
||||
if out.len() == MAX_PATH_DEPTH {
|
||||
return Err(serde::de::Error::custom(format!("a symbol override path is deeper than {MAX_PATH_DEPTH}")));
|
||||
}
|
||||
out.push(i);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
d.deserialize_seq(Path)
|
||||
}
|
||||
|
||||
/// The children a path steps through: a group's (clip groups included) or a layer's.
|
||||
fn groups_children(n: &Node) -> Option<&Vec<Arc<Node>>> {
|
||||
matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }).then(|| n.children()).flatten()
|
||||
}
|
||||
|
||||
/// The object `path` names in symbol art `art`, if there is one.
|
||||
pub fn part<'a>(art: &'a Node, path: &[u32]) -> Option<&'a Node> {
|
||||
if path.len() > MAX_PATH_DEPTH {
|
||||
return None;
|
||||
}
|
||||
path.iter().try_fold(art, |n, &i| groups_children(n)?.get(i as usize).map(|c| &**c))
|
||||
}
|
||||
|
||||
/// [`part`], to change (the nodes on the way are copied when shared).
|
||||
fn part_mut<'a>(art: &'a mut Node, path: &[u32]) -> Option<&'a mut Node> {
|
||||
let Some((&i, rest)) = path.split_first() else { return Some(art) };
|
||||
if !matches!(art.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }) {
|
||||
return None;
|
||||
}
|
||||
let c = art.children_mut()?.get_mut(i as usize)?;
|
||||
part_mut(Arc::make_mut(c), rest)
|
||||
}
|
||||
|
||||
/// Put `overrides` on symbol art `art`; an override naming no object of it is left out.
|
||||
pub fn apply(art: &mut Node, overrides: &[SymbolOverride]) {
|
||||
for o in overrides.iter().filter(|o| o.path.len() <= MAX_PATH_DEPTH) {
|
||||
if let Some(n) = part_mut(art, &o.path) {
|
||||
o.apply(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Symbol art `art` as instance `inst` shows it: a copy with the instance's overrides applied.
|
||||
pub fn dynamic_art(art: &Node, inst: &Node) -> Node {
|
||||
let mut a = art.clone();
|
||||
apply(&mut a, &inst.extras().symbol_overrides);
|
||||
a
|
||||
}
|
||||
|
||||
/// Keep the overrides that name an object of `art` (a symbol's new art), dropping the rest.
|
||||
pub fn retain_parts(overrides: &mut Vec<SymbolOverride>, art: &Node) {
|
||||
overrides.retain(|o| part(art, &o.path).is_some());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Appearance, NodeId};
|
||||
use vectorcraft_color::Color;
|
||||
use vectorcraft_geom::{Rect, shapes};
|
||||
|
||||
fn square(id: u64) -> Arc<Node> {
|
||||
Arc::new(Node::path(NodeId(id), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default_art()))
|
||||
}
|
||||
|
||||
fn art() -> Node {
|
||||
let inner = Node::group(NodeId(3), vec![square(4), square(5)]);
|
||||
Node::group(NodeId(1), vec![square(2), Arc::new(inner)])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paths_name_objects_through_groups_only() {
|
||||
let a = art();
|
||||
assert_eq!(part(&a, &[]).map(|n| n.id), Some(NodeId(1)));
|
||||
assert_eq!(part(&a, &[1, 0]).map(|n| n.id), Some(NodeId(4)));
|
||||
assert!(part(&a, &[2]).is_none());
|
||||
assert!(part(&a, &[0, 0]).is_none(), "a path has no children");
|
||||
assert!(part(&a, &[0; MAX_PATH_DEPTH + 1]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overrides_paint_their_object_and_skip_missing_ones() {
|
||||
let red = Paint::solid(Color::rgb(1.0, 0.0, 0.0));
|
||||
let o = vec![
|
||||
SymbolOverride { path: vec![1, 1], fill: Some(red.clone()), opacity: Some(0.5), ..Default::default() },
|
||||
SymbolOverride { path: vec![9], fill: Some(red.clone()), ..Default::default() },
|
||||
];
|
||||
let mut a = art();
|
||||
apply(&mut a, &o);
|
||||
let n = part(&a, &[1, 1]).unwrap();
|
||||
assert_eq!(n.appearance.fill_paint(), red);
|
||||
assert_eq!(n.opacity, 0.5);
|
||||
assert_eq!(part(&a, &[1, 0]).unwrap().appearance.fill_paint(), Paint::solid(Color::WHITE), "its sibling keeps the symbol's look");
|
||||
let mut kept = o;
|
||||
retain_parts(&mut kept, &a);
|
||||
assert_eq!(kept.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_path_deeper_than_the_cap_does_not_read() {
|
||||
let deep = format!("{{\"path\":{:?}}}", vec![0; MAX_PATH_DEPTH + 1]);
|
||||
assert!(serde_json::from_str::<SymbolOverride>(&deep).is_err());
|
||||
let ok: SymbolOverride = serde_json::from_str("{\"path\":[1,0],\"opacity\":0.25}").unwrap();
|
||||
assert_eq!(ok, SymbolOverride { path: vec![1, 0], opacity: Some(0.25), ..Default::default() });
|
||||
}
|
||||
}
|
||||
@@ -410,7 +410,7 @@ mod tests {
|
||||
/// Symbol `name` holding `art` in a group.
|
||||
fn symbol(d: &mut Document, name: &str, art: Vec<Node>) {
|
||||
let group = Node::group(d.alloc_id(), art.into_iter().map(Arc::new).collect());
|
||||
d.symbols.push(Symbol { name: name.into(), art: Arc::new(group) });
|
||||
d.symbols.push(Symbol { dynamic: false, name: name.into(), art: Arc::new(group) });
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+36
-1
@@ -55,6 +55,29 @@ fn doc_with_rects() -> (Document, NodeId, NodeId) {
|
||||
(d, a, b)
|
||||
}
|
||||
|
||||
/// Output draws the background colors of the artboards asked for, and only theirs, as rectangles
|
||||
/// of those artboards on a layer below the art; without a color there is nothing to draw.
|
||||
#[test]
|
||||
fn artboard_fills_go_below_the_art() {
|
||||
let (mut d, a, _) = doc_with_rects();
|
||||
assert!(d.with_artboard_fills([0]).is_none(), "no color, no fill");
|
||||
let second = Rect::new(700.0, 0.0, 800.0, 100.0);
|
||||
d.artboards.push(Artboard { id: 2, rect: second, ..d.artboards[0].clone() });
|
||||
let blue = vectorcraft_color::Color::cmyk(1.0, 0.5, 0.0, 0.0);
|
||||
d.artboards[1].color = Some(blue);
|
||||
assert!(d.with_artboard_fills([0]).is_none(), "only the artboards asked for");
|
||||
let out = d.with_artboard_fills([0, 1]).unwrap();
|
||||
assert_eq!(out.layers.len(), d.layers.len() + 1);
|
||||
assert_eq!(out.layers[1..], d.layers[..], "the art stays as it was, above the fills");
|
||||
let fills = out.layers[0].children().unwrap();
|
||||
assert_eq!(out.layers[0].display_name(), "Artboard Fill");
|
||||
assert_eq!(fills.len(), 1);
|
||||
assert_eq!(fills[0].geometric_bounds(), Some(second));
|
||||
assert_eq!(fills[0].appearance.fill_paint(), vectorcraft_color::Paint::solid(blue), "in the color's own model");
|
||||
assert!(out.node(a).is_some() && out.node(fills[0].id).is_some() && fills[0].id != a);
|
||||
assert_eq!(d.artboards, out.artboards);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_document_has_layer_and_artboard() {
|
||||
let d = Document::new(612.0, 792.0);
|
||||
@@ -202,7 +225,7 @@ fn art_on_a_locked_artboard_cannot_be_selected_or_edited() {
|
||||
fn art_on_artboard_measures_a_symbol_instance_by_its_art() {
|
||||
let mut d = Document::new(612.0, 792.0);
|
||||
let art = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default_art());
|
||||
d.symbols.push(Symbol { name: "Box".into(), art: Arc::new(art) });
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Box".into(), art: Arc::new(art) });
|
||||
let (layer, id) = (d.layers[0].id, d.alloc_id());
|
||||
d.insert(Some(layer), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Box".into(), xf: Affine::translate((2.0, 2.0)) })).unwrap();
|
||||
// The art, (2, 2) to (12, 12), lies inside; the 20 × 20 square, (-8, -8) to (12, 12), doesn't.
|
||||
@@ -388,6 +411,18 @@ fn char_position_round_trips() {
|
||||
assert!((scale - 0.583).abs() < 1e-9 && (shift + 3.33).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optical_kerning_round_trips_and_old_style_defaults_to_auto() {
|
||||
let st = CharStyle { optical_kerning: true, kerning: Some(120.0), ..CharStyle::default() };
|
||||
let value = serde_json::to_value(&st).unwrap();
|
||||
assert_eq!(value["opticalKerning"], true);
|
||||
assert_eq!(serde_json::from_value::<CharStyle>(value.clone()).unwrap(), st);
|
||||
let mut old = value;
|
||||
old.as_object_mut().unwrap().remove("opticalKerning");
|
||||
assert!(!serde_json::from_value::<CharStyle>(old).unwrap().optical_kerning);
|
||||
assert!(serde_json::to_value(CharStyle::default()).unwrap().get("opticalKerning").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn units() {
|
||||
assert_eq!(Unit::Inches.parse("1"), Some(72.0));
|
||||
|
||||
@@ -144,3 +144,10 @@ fn mixed_mul_div() {
|
||||
assert_eq!(Unit::Points.parse("10*2/5"), Some(4.0));
|
||||
assert_eq!(Unit::Points.parse("10*2/5 px"), Some(Unit::Pixels.to_pt(4.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn readout_has_no_negative_zero() {
|
||||
assert_eq!(Unit::Points.readout(-0.001), "0.00 pt");
|
||||
assert_eq!(Unit::Points.readout(-0.0), "0.00 pt");
|
||||
assert_eq!(Unit::Points.readout(-12.5), "-12.50 pt");
|
||||
}
|
||||
@@ -13,9 +13,9 @@ mod para_style;
|
||||
mod runs;
|
||||
mod tabs_lists;
|
||||
|
||||
pub use area::{AreaFit, AreaOptions, PathAlign, PathEffect, TextWrap, VerticalAlign, WrapShape};
|
||||
pub use area::{AreaFit, AreaOptions, PathAlign, PathEffect, TextFlow, TextWrap, VerticalAlign, WrapShape};
|
||||
use char_style::yes;
|
||||
pub use char_style::{CharAlign, CharPosition, CharStyle, Digits, ScriptMetrics};
|
||||
pub use char_style::{CharAlign, CharPosition, CharStyle, Digits, FontHeightReference, ScriptMetrics};
|
||||
pub use para_style::{
|
||||
Burasagari, Composer, FirstBaseline, HyphenationOptions, JustificationOptions, Justify, Kashida, Kinsoku, LeadingModel, Mojikumi, ParaDirection,
|
||||
ParaStyle, SingleWordJustify,
|
||||
|
||||
@@ -29,6 +29,19 @@ impl VerticalAlign {
|
||||
}
|
||||
}
|
||||
|
||||
/// Area Type Options › Text Flow: the order text fills the cells of area type with several rows
|
||||
/// and columns.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum TextFlow {
|
||||
/// Down each column's rows, then on to the next column (the default, and what a document
|
||||
/// from before the option reads as).
|
||||
#[default]
|
||||
Columns,
|
||||
/// Across each row's columns, then on to the next row.
|
||||
Rows,
|
||||
}
|
||||
|
||||
/// Text Wrap Options of a wrap object (Object → Text Wrap).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
@@ -135,6 +148,9 @@ pub struct AreaOptions {
|
||||
pub first_baseline_min: f64,
|
||||
/// Vertical alignment of the lines in each row/column.
|
||||
pub vertical_align: VerticalAlign,
|
||||
/// Text Flow: by columns (the default, left out when saved) or by rows.
|
||||
#[serde(skip_serializing_if = "crate::skip::is_default")]
|
||||
pub flow: TextFlow,
|
||||
/// How the frame and its text fit each other (Auto Size, Shrink Text to Fit).
|
||||
#[serde(skip_serializing_if = "crate::skip::is_default")]
|
||||
pub fit: AreaFit,
|
||||
@@ -150,6 +166,7 @@ impl Default for AreaOptions {
|
||||
first_baseline: FirstBaseline::Ascent,
|
||||
first_baseline_min: 0.0,
|
||||
vertical_align: VerticalAlign::Top,
|
||||
flow: TextFlow::Columns,
|
||||
fit: AreaFit::None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,12 @@ pub struct CharStyle {
|
||||
/// Tracking in 1/1000 em.
|
||||
#[serde(default)]
|
||||
pub tracking: f64,
|
||||
/// Kerning: None = Auto (metrics), Some(v) = manual in 1/1000 em.
|
||||
/// Manual pair spacing in 1/1000 em; 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.
|
||||
#[serde(default, rename = "opticalKerning", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub optical_kerning: bool,
|
||||
#[serde(default)]
|
||||
pub baseline_shift: f64,
|
||||
#[serde(default = "hundred")]
|
||||
@@ -101,21 +104,55 @@ pub struct CharStyle {
|
||||
/// <https://helpx.adobe.com/illustrator/using/formatting-asian-characters.html>
|
||||
#[serde(default, rename = "tateChuYoko", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub tate_chu_yoko: bool,
|
||||
/// Standard Vertical Roman Alignment (Character panel menu, East Asian options): in vertical
|
||||
/// type the half-width characters among these (Latin letters, digits and other characters that
|
||||
/// would lie on their side) stand upright one below the other instead of being rotated
|
||||
/// individually. Spaces and tate-chu-yoko blocks do not change, and horizontal type keeps the
|
||||
/// mark and does not change.
|
||||
/// <https://helpx.adobe.com/illustrator/using/formatting-asian-characters.html>
|
||||
#[serde(default, rename = "standardVerticalRoman", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub standard_vertical_roman: bool,
|
||||
/// Digits (Character panel menu, Middle Eastern options): the numeral system the digits of
|
||||
/// these characters are drawn in, whatever digits were typed.
|
||||
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
|
||||
pub digits: Digits,
|
||||
/// Font height reference (Character panel menu › Show Font Height Options): the height of the
|
||||
/// font that [`Self::size`] measures, the em box by default.
|
||||
#[serde(default, rename = "fontHeightReference", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub font_height_reference: FontHeightReference,
|
||||
/// No Break (Character panel menu): these characters are not broken across lines, including
|
||||
/// at a space or a hyphenation point inside the run. A run longer than the line stays together
|
||||
/// and can overflow it.
|
||||
/// <https://helpx.adobe.com/illustrator/using/hyphenation-line-breaks.html>
|
||||
#[serde(default, rename = "noBreak", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub no_break: bool,
|
||||
/// Language (Character panel): the BCP 47 tag of one of [`crate::setup::LANGUAGES`] these
|
||||
/// characters are shaped in, so that the font uses that language's forms (OpenType `locl`).
|
||||
/// `None`: the default language, English: USA, shaped without a language. New type takes the
|
||||
/// document's language (Document Setup). Saved only when set.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub language: Option<String>,
|
||||
}
|
||||
|
||||
/// What a character's font size measures ([`CharStyle::font_height_reference`]): the em square
|
||||
/// of the font, the height of its capitals or of its lowercase letters (the cap height and x
|
||||
/// height of its OS/2 table), or its ideographic character face (ICF, OpenType's `icfb` and
|
||||
/// `icft` baselines). Layout sets the characters at the em size at which the chosen height equals
|
||||
/// the font size; with a font that has no such value, the size measures the em box.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum FontHeightReference {
|
||||
#[default]
|
||||
EmBox,
|
||||
CapHeight,
|
||||
XHeight,
|
||||
IcfBox,
|
||||
}
|
||||
|
||||
/// The numeral system digits are drawn in ([`CharStyle::digits`]): as typed, or every digit of
|
||||
/// the Western, Arabic-Indic and Eastern Arabic-Indic sets drawn in one of them. The text keeps
|
||||
/// the digits that were typed; only the glyphs change (like All Caps).
|
||||
/// the Western, Arabic-Indic and Eastern Arabic-Indic sets drawn in one of them or in the digits
|
||||
/// of an Indic script. The text keeps the digits that were typed; only the glyphs change (like
|
||||
/// All Caps).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum Digits {
|
||||
@@ -128,9 +165,68 @@ pub enum Digits {
|
||||
Hindi,
|
||||
/// Eastern Arabic-Indic digits ۰–۹ (U+06F0–U+06F9), used in Persian and Urdu.
|
||||
Farsi,
|
||||
/// Devanagari digits ०–९ (U+0966–U+096F), used in Hindi, Marathi, Nepali and Sanskrit.
|
||||
Devanagari,
|
||||
/// Bengali digits ০–৯ (U+09E6–U+09EF), used in Bengali and Assamese.
|
||||
Bengali,
|
||||
/// Gurmukhi digits ੦–੯ (U+0A66–U+0A6F), used in Punjabi.
|
||||
Gurmukhi,
|
||||
/// Gujarati digits ૦–૯ (U+0AE6–U+0AEF).
|
||||
Gujarati,
|
||||
/// Odia digits ୦–୯ (U+0B66–U+0B6F).
|
||||
Odia,
|
||||
/// Tamil digits ௦–௯ (U+0BE6–U+0BEF).
|
||||
Tamil,
|
||||
/// Telugu digits ౦–౯ (U+0C66–U+0C6F).
|
||||
Telugu,
|
||||
/// Kannada digits ೦–೯ (U+0CE6–U+0CEF).
|
||||
Kannada,
|
||||
/// Malayalam digits ൦–൯ (U+0D66–U+0D6F).
|
||||
Malayalam,
|
||||
}
|
||||
|
||||
impl Digits {
|
||||
/// Every numeral system, in menu order.
|
||||
pub const ALL: [Digits; 13] = [
|
||||
Digits::Default,
|
||||
Digits::Arabic,
|
||||
Digits::Hindi,
|
||||
Digits::Farsi,
|
||||
Digits::Devanagari,
|
||||
Digits::Bengali,
|
||||
Digits::Gurmukhi,
|
||||
Digits::Gujarati,
|
||||
Digits::Odia,
|
||||
Digits::Tamil,
|
||||
Digits::Telugu,
|
||||
Digits::Kannada,
|
||||
Digits::Malayalam,
|
||||
];
|
||||
|
||||
/// The name files and commands give this numeral system (as it is saved).
|
||||
pub fn id(self) -> &'static str {
|
||||
match self {
|
||||
Digits::Default => "default",
|
||||
Digits::Arabic => "arabic",
|
||||
Digits::Hindi => "hindi",
|
||||
Digits::Farsi => "farsi",
|
||||
Digits::Devanagari => "devanagari",
|
||||
Digits::Bengali => "bengali",
|
||||
Digits::Gurmukhi => "gurmukhi",
|
||||
Digits::Gujarati => "gujarati",
|
||||
Digits::Odia => "odia",
|
||||
Digits::Tamil => "tamil",
|
||||
Digits::Telugu => "telugu",
|
||||
Digits::Kannada => "kannada",
|
||||
Digits::Malayalam => "malayalam",
|
||||
}
|
||||
}
|
||||
|
||||
/// The numeral system [`Digits::id`] names; `None` for any other name.
|
||||
pub fn from_id(id: &str) -> Option<Digits> {
|
||||
Digits::ALL.into_iter().find(|d| d.id() == id)
|
||||
}
|
||||
|
||||
/// The digit `c` is drawn as in this numeral system: the same digit in the chosen set, or `c`
|
||||
/// itself when `c` isn't a digit of the Western, Arabic-Indic or Eastern Arabic-Indic set.
|
||||
pub fn map(self, c: char) -> char {
|
||||
@@ -145,6 +241,15 @@ impl Digits {
|
||||
Digits::Arabic => '0' as u32,
|
||||
Digits::Hindi => 0x660,
|
||||
Digits::Farsi => 0x6F0,
|
||||
Digits::Devanagari => 0x966,
|
||||
Digits::Bengali => 0x9E6,
|
||||
Digits::Gurmukhi => 0xA66,
|
||||
Digits::Gujarati => 0xAE6,
|
||||
Digits::Odia => 0xB66,
|
||||
Digits::Tamil => 0xBE6,
|
||||
Digits::Telugu => 0xC66,
|
||||
Digits::Kannada => 0xCE6,
|
||||
Digits::Malayalam => 0xD66,
|
||||
};
|
||||
char::from_u32(base + value).unwrap_or(c)
|
||||
}
|
||||
@@ -266,6 +371,7 @@ impl Default for CharStyle {
|
||||
leading: None,
|
||||
tracking: 0.0,
|
||||
kerning: None,
|
||||
optical_kerning: false,
|
||||
baseline_shift: 0.0,
|
||||
h_scale: 100.0,
|
||||
v_scale: 100.0,
|
||||
@@ -290,9 +396,12 @@ impl Default for CharStyle {
|
||||
char_direction: None,
|
||||
proportional_metrics: false,
|
||||
tate_chu_yoko: false,
|
||||
standard_vertical_roman: false,
|
||||
small_caps: None,
|
||||
digits: Digits::Default,
|
||||
font_height_reference: FontHeightReference::EmBox,
|
||||
no_break: false,
|
||||
language: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,6 +223,7 @@ impl ParaList {
|
||||
position: CharPosition::Normal,
|
||||
tracking: 0.0,
|
||||
kerning: None,
|
||||
optical_kerning: first.optical_kerning,
|
||||
tate_chu_yoko: false,
|
||||
underline: false,
|
||||
strikethrough: false,
|
||||
|
||||
@@ -60,6 +60,69 @@ fn digits_map_between_the_numeral_systems() {
|
||||
assert_eq!(serde_json::from_value::<CharStyle>(serde_json::to_value(&farsi).unwrap()).unwrap(), farsi);
|
||||
}
|
||||
|
||||
/// The font height reference is the em box by default and is saved only when it is another.
|
||||
#[test]
|
||||
fn font_height_reference_is_saved_only_when_not_the_em_box() {
|
||||
let st = CharStyle::default();
|
||||
assert_eq!(st.font_height_reference, FontHeightReference::EmBox);
|
||||
assert!(serde_json::to_value(&st).unwrap().get("fontHeightReference").is_none());
|
||||
for (r, key) in
|
||||
[(FontHeightReference::CapHeight, "capHeight"), (FontHeightReference::XHeight, "xHeight"), (FontHeightReference::IcfBox, "icfBox")]
|
||||
{
|
||||
let set = CharStyle { font_height_reference: r, ..CharStyle::default() };
|
||||
assert_eq!(serde_json::to_value(&set).unwrap()["fontHeightReference"], key);
|
||||
assert_eq!(serde_json::from_value::<CharStyle>(serde_json::to_value(&set).unwrap()).unwrap(), set);
|
||||
}
|
||||
// Styles written before the attribute existed read as the em box.
|
||||
let old = serde_json::to_value(&st).unwrap();
|
||||
assert_eq!(serde_json::from_value::<CharStyle>(old).unwrap().font_height_reference, FontHeightReference::EmBox);
|
||||
}
|
||||
|
||||
/// Each Indic numeral system draws 0–9 (and the Arabic-Indic sets) as the ten digits of its
|
||||
/// script's block; every system saves under its id, which reads back, and other names are refused.
|
||||
#[test]
|
||||
fn indic_digits_map_to_their_scripts_block() {
|
||||
let map = |d: Digits, s: &str| -> String { s.chars().map(|c| d.map(c)).collect() };
|
||||
for (d, zero) in [
|
||||
(Digits::Devanagari, 0x966),
|
||||
(Digits::Bengali, 0x9E6),
|
||||
(Digits::Gurmukhi, 0xA66),
|
||||
(Digits::Gujarati, 0xAE6),
|
||||
(Digits::Odia, 0xB66),
|
||||
(Digits::Tamil, 0xBE6),
|
||||
(Digits::Telugu, 0xC66),
|
||||
(Digits::Kannada, 0xCE6),
|
||||
(Digits::Malayalam, 0xD66),
|
||||
] {
|
||||
let block: String = (zero..zero + 10).map(|u| char::from_u32(u).unwrap()).collect();
|
||||
assert!(block.chars().all(char::is_numeric), "{d:?}: {block}");
|
||||
for s in ["0123456789", "٠١٢٣٤٥٦٧٨٩", "۰۱۲۳۴۵۶۷۸۹"] {
|
||||
assert_eq!(map(d, s), block, "{d:?}");
|
||||
}
|
||||
assert_eq!(map(d, "a-Z ٪"), "a-Z ٪");
|
||||
}
|
||||
assert_eq!(map(Digits::Devanagari, "page 12"), "page १२");
|
||||
for d in Digits::ALL {
|
||||
assert_eq!(serde_json::to_value(d).unwrap(), d.id());
|
||||
assert_eq!(serde_json::from_value::<Digits>(d.id().into()).unwrap(), d);
|
||||
assert_eq!(Digits::from_id(d.id()), Some(d));
|
||||
}
|
||||
assert_eq!(Digits::from_id("roman"), None);
|
||||
assert!(serde_json::from_value::<Digits>("roman".into()).is_err());
|
||||
}
|
||||
|
||||
/// Language is saved only when set; styles written before it existed read as the default.
|
||||
#[test]
|
||||
fn language_is_saved_only_when_set() {
|
||||
let st = CharStyle::default();
|
||||
let old = serde_json::to_value(&st).unwrap();
|
||||
assert!(old.get("language").is_none());
|
||||
assert_eq!(serde_json::from_value::<CharStyle>(old).unwrap().language, None);
|
||||
let turkish = CharStyle { language: Some("tr".into()), ..CharStyle::default() };
|
||||
assert_eq!(serde_json::to_value(&turkish).unwrap()["language"], "tr");
|
||||
assert_eq!(serde_json::from_value::<CharStyle>(serde_json::to_value(&turkish).unwrap()).unwrap(), turkish);
|
||||
}
|
||||
|
||||
/// Auto leading follows the paragraph's Auto Leading; a set leading doesn't.
|
||||
#[test]
|
||||
fn paragraph_effective_leading_follows_auto_leading() {
|
||||
@@ -248,6 +311,19 @@ fn paragraph_styles_split_merge_and_normalize() {
|
||||
assert_eq!(back.para_at(3).space_before, 4.0);
|
||||
}
|
||||
|
||||
/// Text Flow is saved only when by rows: area type by columns (the default) saves as it did before
|
||||
/// the option, and files from before it read by columns; other values don't read.
|
||||
#[test]
|
||||
fn text_flow_is_saved_only_when_by_rows() {
|
||||
let by_columns = serde_json::to_value(AreaOptions::default()).unwrap();
|
||||
assert!(by_columns.get("flow").is_none());
|
||||
assert_eq!(serde_json::from_value::<AreaOptions>(by_columns).unwrap().flow, TextFlow::Columns);
|
||||
let by_rows = serde_json::to_value(AreaOptions { flow: TextFlow::Rows, ..AreaOptions::default() }).unwrap();
|
||||
assert_eq!(by_rows["flow"], "rows");
|
||||
assert_eq!(serde_json::from_value::<AreaOptions>(by_rows).unwrap().flow, TextFlow::Rows);
|
||||
assert!(serde_json::from_value::<AreaOptions>(serde_json::json!({"flow": "diagonal"})).is_err());
|
||||
}
|
||||
|
||||
/// 120 × 40 area type at (40, 40), its text drawn at twice its size.
|
||||
fn area_type() -> TextObject {
|
||||
let mut t = TextObject::point(Point::ZERO, "Some text", CharStyle::default());
|
||||
|
||||
@@ -161,7 +161,10 @@ impl Unit {
|
||||
/// A point value as the canvas measurement labels show it: two decimals and the suffix
|
||||
/// (`12.50 mm`).
|
||||
pub fn readout(self, pt: f64) -> String {
|
||||
format!("{:.2} {}", self.from_pt(pt), self.suffix())
|
||||
let s = format!("{:.2}", self.from_pt(pt));
|
||||
// A small negative value rounds to `-0.00`; show it as `0.00`.
|
||||
let s = if s == "-0.00" { "0.00" } else { &s };
|
||||
format!("{} {}", s, self.suffix())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{Document, NodeId, NodeKind};
|
||||
use super::{Document, NodeId, NodeKind, TextKind};
|
||||
|
||||
/// What a variable drives.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
@@ -117,7 +117,8 @@ pub struct Variables {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub active_dataset: Option<String>,
|
||||
/// Lock Variables: while set, variables can't be defined, deleted or renamed. Objects can
|
||||
/// still be bound to them and unbound, and datasets still change and apply.
|
||||
/// still be bound to them and unbound, and datasets still change and apply, except that an
|
||||
/// import whose header would define a variable is refused.
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub locked: bool,
|
||||
}
|
||||
@@ -202,17 +203,46 @@ impl Document {
|
||||
self.ancestry(id).is_some_and(|a| a.iter().all(|i| self.node(*i).is_some_and(|n| !n.locked)))
|
||||
}
|
||||
|
||||
/// What the object `id` holds for the variable of `kind`: its characters, whether it shows,
|
||||
/// or a linked image's path. `None` when the object is gone, is not of that kind, has no
|
||||
/// link path yet (an embedded image), or is locked or in a locked layer or group (the
|
||||
/// document's value is whatever it was left at).
|
||||
/// The type objects of the story that the type object `id` belongs to, in thread order: the
|
||||
/// frames of its threaded text (area type and type on a path), else `id` alone.
|
||||
pub fn story_of(&self, id: NodeId) -> Vec<NodeId> {
|
||||
let frame = |f: &NodeId| matches!(self.node(*f).map(|n| &n.kind), Some(NodeKind::Text(t)) if !matches!(t.kind, TextKind::Point));
|
||||
match self.text_threads.iter().find(|thread| thread.contains(&id)) {
|
||||
Some(thread) if frame(&id) => thread.iter().filter(|f| frame(f)).copied().collect(),
|
||||
_ => vec![id],
|
||||
}
|
||||
}
|
||||
|
||||
/// The characters of the story that the type object `id` belongs to ([`Document::story_of`]):
|
||||
/// every frame's text joined in thread order.
|
||||
pub fn story_text(&self, id: NodeId) -> String {
|
||||
self.story_of(id)
|
||||
.into_iter()
|
||||
.filter_map(|f| match self.node(f).map(|n| &n.kind) {
|
||||
Some(NodeKind::Text(t)) => Some(t.plain_text()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Whether a data set reads and rewrites the text of the type object `id`: every frame of its
|
||||
/// story ([`Document::story_of`]) is a [`Document::dataset_target`].
|
||||
pub fn story_target(&self, id: NodeId) -> bool {
|
||||
self.story_of(id).into_iter().all(|f| self.dataset_target(f))
|
||||
}
|
||||
|
||||
/// What the object `id` holds for the variable of `kind`: its characters (the whole story
|
||||
/// for a frame of threaded text), whether it shows, or a linked image's path. `None` when the
|
||||
/// object is gone, is not of that kind, has no link path yet (an embedded image), or is locked
|
||||
/// or in a locked layer or group, or for text when a frame of its story is (the document's
|
||||
/// value is whatever it was left at).
|
||||
pub fn variable_value(&self, id: NodeId, kind: VariableKind) -> Option<DataValue> {
|
||||
if !self.dataset_target(id) {
|
||||
return None;
|
||||
}
|
||||
let node = self.node(id)?;
|
||||
match (kind, &node.kind) {
|
||||
(VariableKind::Text, NodeKind::Text(t)) => Some(DataValue::Text(t.plain_text())),
|
||||
(VariableKind::Text, NodeKind::Text(_)) if self.story_target(id) => Some(DataValue::Text(self.story_text(id))),
|
||||
(VariableKind::Visibility, _) => Some(DataValue::Visible(node.visible)),
|
||||
(VariableKind::Image, NodeKind::Image(im)) => im.link.as_ref().map(|l| DataValue::Image(l.path.clone())),
|
||||
(VariableKind::Graph, _) => node.extras().graph.as_ref().map(|g| DataValue::Graph(g.to_csv())),
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
//! Effects of VectorCraft's built-in add-ons, one module per add-on.
|
||||
//!
|
||||
//! - [`color`]: Color Effects' live Exposure, Desaturate and Invert Colors, colour adjustments
|
||||
//! evaluated with the others ([`crate::adjust`]).
|
||||
//! - [`duotone`]: Color Effects' live Duotone, a colour adjustment that prints each colour's tone
|
||||
//! with one to four inks.
|
||||
//! - [`halftone`]: Color Effects' live Vector Halftone, and the screens Object › Vector Halftone
|
||||
//! builds its dots with.
|
||||
//! - [`path`]: Path Precision Tools' live Path Effects, geometry effects built on the add-on's path
|
||||
//! kernels.
|
||||
|
||||
pub mod color;
|
||||
pub mod duotone;
|
||||
pub mod halftone;
|
||||
pub mod path;
|
||||
@@ -0,0 +1,138 @@
|
||||
//! Color Effects (a built-in add-on): live Exposure, Desaturate, Duotone and Invert Colors, in
|
||||
//! Effect › Color Effects. They are colour adjustments ([`crate::is_adjustment`]): they recolour what
|
||||
//! an object paints as the others do, in stack order with them.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::catalog::{EffectInfo, Lengths};
|
||||
use crate::util::num;
|
||||
|
||||
/// Exposure: brighten or darken in stops of light, with an offset and a gamma.
|
||||
pub const EXPOSURE: &str = "addon.color.exposure";
|
||||
/// Desaturate: every colour to the grey of its lightness.
|
||||
pub const DESATURATE: &str = "addon.color.desaturate";
|
||||
/// Invert Colors: every colour to its RGB inverse.
|
||||
pub const INVERT: &str = "addon.color.invert";
|
||||
pub use super::duotone::DUOTONE;
|
||||
|
||||
/// The add-on's effects, in menu order.
|
||||
pub const EFFECTS: [&str; 4] = [DESATURATE, DUOTONE, EXPOSURE, INVERT];
|
||||
|
||||
/// Is `id` one of the add-on's effects?
|
||||
pub fn is_color_effect(id: &str) -> bool {
|
||||
EFFECTS.contains(&id)
|
||||
}
|
||||
|
||||
/// The add-on's catalogue entries.
|
||||
pub(crate) fn catalog() -> Vec<EffectInfo> {
|
||||
const MENU: &[&str] = &["Effect", "Color Effects"];
|
||||
let e = |id, label, params, defaults| EffectInfo { id, label, menu: MENU, params, defaults, raster: false, lengths: Lengths::default() };
|
||||
vec![
|
||||
e(DESATURATE, "Desaturate", "{} turns every colour the object paints to the grey of its lightness (each colour keeps its model)", json!({})),
|
||||
e(DUOTONE, "Duotone…", super::duotone::PARAMS, super::duotone::defaults()),
|
||||
e(
|
||||
EXPOSURE,
|
||||
"Exposure…",
|
||||
"{exposure: stops -20..20 (0; mostly the highlights: each stop doubles or halves the light), offset: -0.5..0.5 (0; lifts or sinks the shadows and midtones), gamma: 0.1..10 (1; above 1 lightens the midtones)} recolours as Brightness/Contrast does, in linear light",
|
||||
json!({"exposure": 0.0, "offset": 0.0, "gamma": 1.0}),
|
||||
),
|
||||
e(INVERT, "Invert Colors", "{} turns every colour the object paints to its RGB inverse (each colour keeps its model)", json!({})),
|
||||
]
|
||||
}
|
||||
|
||||
/// One of the add-on's adjustments, its parameters normalised.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) enum Adjust {
|
||||
/// Linear light times `gain`, plus `offset`, to the power 1 / `gamma`.
|
||||
Exposure {
|
||||
gain: f32,
|
||||
offset: f32,
|
||||
gamma: f32,
|
||||
},
|
||||
Desaturate,
|
||||
Invert,
|
||||
/// Duotone ([`super::duotone`]).
|
||||
Duotone(super::duotone::Duotone),
|
||||
}
|
||||
|
||||
impl Adjust {
|
||||
pub(crate) fn parse(id: &str, p: &Value) -> Option<Self> {
|
||||
Some(match id {
|
||||
EXPOSURE => Adjust::Exposure {
|
||||
gain: 2f64.powf(num(p, "exposure", 0.0).clamp(-20.0, 20.0)) as f32,
|
||||
offset: num(p, "offset", 0.0).clamp(-0.5, 0.5) as f32,
|
||||
gamma: num(p, "gamma", 1.0).clamp(0.1, 10.0) as f32,
|
||||
},
|
||||
DESATURATE => Adjust::Desaturate,
|
||||
INVERT => Adjust::Invert,
|
||||
DUOTONE => Adjust::Duotone(super::duotone::Duotone::parse(p)?),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn apply(&self, rgb: [f32; 3]) -> [f32; 3] {
|
||||
match self {
|
||||
Adjust::Exposure { gain, offset, gamma } => rgb.map(|v| to_srgb((to_linear(v) * gain + offset).max(0.0).powf(1.0 / gamma))),
|
||||
Adjust::Desaturate => {
|
||||
let [r, g, b] = rgb;
|
||||
[(r.max(g).max(b) + r.min(g).min(b)) / 2.0; 3]
|
||||
}
|
||||
Adjust::Invert => rgb.map(|v| 1.0 - v),
|
||||
Adjust::Duotone(d) => d.apply_rgb(rgb),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn to_linear(v: f32) -> f32 {
|
||||
let v = v.clamp(0.0, 1.0);
|
||||
if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
|
||||
}
|
||||
|
||||
fn to_srgb(v: f32) -> f32 {
|
||||
let v = v.clamp(0.0, 1.0);
|
||||
if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::color_map;
|
||||
use vectorcraft_doc::Effect;
|
||||
|
||||
fn map(id: &str, params: Value) -> crate::ColorMap {
|
||||
color_map(&[Effect { id: id.into(), params, visible: true }]).unwrap()
|
||||
}
|
||||
|
||||
fn close(a: [f32; 3], b: [f32; 3]) -> bool {
|
||||
a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.01)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_effects_are_colour_adjustments_in_their_own_menu() {
|
||||
for id in EFFECTS {
|
||||
assert!(crate::is_adjustment(id), "{id}");
|
||||
assert_eq!(crate::effect_info(id).unwrap().menu, ["Effect", "Color Effects"]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exposure_works_in_stops_of_linear_light() {
|
||||
let grey = [0.5; 3];
|
||||
assert!(close(map(EXPOSURE, json!({})).apply_rgb(grey), grey), "neutral by default");
|
||||
// One stop doubles the light: sRGB 0.5 (linear 0.214) becomes linear 0.428, sRGB 0.686.
|
||||
assert!(close(map(EXPOSURE, json!({"exposure": 1})).apply_rgb(grey), [0.686; 3]));
|
||||
assert!(close(map(EXPOSURE, json!({"exposure": -20})).apply_rgb([1.0; 3]), [0.0; 3]));
|
||||
assert!(map(EXPOSURE, json!({"offset": 0.1})).apply_rgb([0.0; 3])[0] > 0.3, "the offset lifts the shadows");
|
||||
assert!(map(EXPOSURE, json!({"gamma": 2})).apply_rgb(grey)[0] > 0.6);
|
||||
assert!(close(map(EXPOSURE, json!({"exposure": 1e300, "gamma": 0})).apply_rgb(grey), [1.0; 3]), "out of range values are clamped");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn desaturate_and_invert() {
|
||||
assert!(close(map(DESATURATE, json!({})).apply_rgb([1.0, 0.0, 0.0]), [0.5; 3]));
|
||||
assert!(close(map(DESATURATE, json!({})).apply_rgb([1.0, 1.0, 0.0]), [0.5; 3]), "lightness is kept, not luminance");
|
||||
assert!(close(map(INVERT, json!({})).apply_rgb([1.0, 0.25, 0.0]), [0.0, 0.75, 1.0]));
|
||||
let both = map(INVERT, json!({})).then(&map(INVERT, json!({})));
|
||||
assert!(close(both.apply_rgb([0.2, 0.4, 0.6]), [0.2, 0.4, 0.6]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
//! Color Effects' Duotone (Effect › Color Effects › Duotone…), a colour adjustment: the
|
||||
//! grey-equivalent tone of each colour an object paints is printed with one to four inks (a
|
||||
//! monotone, duotone, tritone or quadtone). Each ink, a tone, is a colour, typically a spot swatch,
|
||||
//! with its own curve from the tone's tint to the ink's. The inks overprint, so their tints
|
||||
//! multiply.
|
||||
//!
|
||||
//! With one tone of a swatch, a colour becomes a tint of that swatch, linked to it, so a spot tone
|
||||
//! separates onto its plate. With several tones it becomes the CMYK mix of their tints: a colour
|
||||
//! links to one swatch at one tint, and a mix of several spot inks has no swatch to link to.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::Document;
|
||||
use vectorcraft_doc::color::{Color, cms::Model};
|
||||
|
||||
use crate::adjust::{LUT, curve_at, curve_points_on, lookup, sample};
|
||||
use crate::raster::color_param;
|
||||
use crate::util::flag;
|
||||
|
||||
/// Duotone (Effect › Color Effects › Duotone…).
|
||||
pub const DUOTONE: &str = "addon.color.duotone";
|
||||
|
||||
/// Most tones Duotone reads (a quadtone): any past them are left out.
|
||||
pub const MAX_TONES: usize = 4;
|
||||
|
||||
/// Most points a tone's curve reads: any past them are left out.
|
||||
pub const MAX_CURVE_POINTS: usize = 64;
|
||||
|
||||
/// The parameters' description (`effect.apply`).
|
||||
pub(crate) const PARAMS: &str = "{tones: [{color: \"#rrggbb\" or a colour object, swatch?: a global or spot swatch's name (with the swatch's colour as `color`), curve?: \"tint,ink tint,ink …\" or [[tint, ink], …] (0..100, at most 64 points; \"0,0 100,100\"), active?: true}] (up to four inks, overprinting; any past four are left out), skipSpot?: false (colours linked to a spot swatch keep their colour)} prints the grey-equivalent tone of each colour the object paints with the inks, each at the tint its curve gives for the tone's: with one tone of a swatch, as a tint of that swatch (a spot tone prints on its own plate); with several, as the CMYK mix of their tints";
|
||||
|
||||
/// The dialog's defaults: one black tone, its curve linear.
|
||||
pub(crate) fn defaults() -> Value {
|
||||
json!({"tones": [{"color": "#000000", "curve": "0,0 100,100", "active": true}], "skipSpot": false})
|
||||
}
|
||||
|
||||
/// Whether swatch `name` of the document is a spot colour: `Some(false)` for a global process
|
||||
/// swatch, `None` when the document has no global swatch of that name.
|
||||
pub type SwatchKinds<'a> = dyn Fn(&str) -> Option<bool> + 'a;
|
||||
|
||||
/// [`SwatchKinds`] of `d`'s swatch `name`.
|
||||
pub fn swatch_kind(d: &Document, name: &str) -> Option<bool> {
|
||||
d.swatches_iter().find(|w| w.global && w.name == name).map(|w| w.spot)
|
||||
}
|
||||
|
||||
/// [`swatch_kind`] of each of `d`'s global swatches.
|
||||
pub fn swatch_kinds(d: &Document) -> HashMap<String, bool> {
|
||||
d.swatches_iter().filter(|w| w.global).map(|w| (w.name.clone(), w.spot)).collect()
|
||||
}
|
||||
|
||||
/// A tone's curve points (0..1 each) from its `curve` (0..100).
|
||||
pub fn tone_curve_points(v: Option<&Value>) -> Vec<(f32, f32)> {
|
||||
curve_points_on(v, 100.0, MAX_CURVE_POINTS)
|
||||
}
|
||||
|
||||
/// One ink.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct Tone {
|
||||
color: Color,
|
||||
/// `color` as CMYK inks, which several tones mix in.
|
||||
cmyk: [f32; 4],
|
||||
/// The swatch the colour is of, if any.
|
||||
swatch: Option<String>,
|
||||
/// Its curve, sampled: the tone's tint → the ink's.
|
||||
curve: Arc<[f32]>,
|
||||
}
|
||||
|
||||
/// Duotone's tones and options, its parameters read.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Duotone {
|
||||
tones: Vec<Tone>,
|
||||
skip_spot: bool,
|
||||
/// The display colour at each sampled tint, for pixels.
|
||||
rgb: Arc<[[f32; 3]]>,
|
||||
}
|
||||
|
||||
impl Duotone {
|
||||
/// `None` without an active tone (the effect then leaves the colours as they are).
|
||||
pub(crate) fn parse(p: &Value) -> Option<Self> {
|
||||
let tones: Vec<Tone> = p
|
||||
.get("tones")
|
||||
.and_then(Value::as_array)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.take(MAX_TONES)
|
||||
.filter(|t| t.is_object() && flag(t, "active", true))
|
||||
.map(|t| {
|
||||
let color = finite(color_param(t, Color::BLACK));
|
||||
let cmyk = match color.in_model(Model::Cmyk) {
|
||||
Color::Cmyk { c, m, y, k } => [c, m, y, k].map(|v| if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 }),
|
||||
_ => [0.0, 0.0, 0.0, 1.0],
|
||||
};
|
||||
let pts = tone_curve_points(t.get("curve"));
|
||||
let swatch = t.get("swatch").and_then(Value::as_str).filter(|s| !s.is_empty()).map(str::to_string);
|
||||
Tone { color, cmyk, swatch, curve: sample(|x| curve_at(&pts, x)) }
|
||||
})
|
||||
.collect();
|
||||
if tones.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut d = Duotone { tones, skip_spot: flag(p, "skipSpot", false), rgb: Arc::new([]) };
|
||||
d.rgb = (0..LUT).map(|i| d.mix(i as f32 / (LUT - 1) as f32).to_rgb()).collect();
|
||||
Some(d)
|
||||
}
|
||||
|
||||
/// The inks' colour for tone `t` (0 = white, 1 = black): one tone's colour at its tint, or
|
||||
/// several tones' CMYK tints overprinted (each ink lets through what the others leave).
|
||||
fn mix(&self, t: f32) -> Color {
|
||||
if let [one] = self.tones.as_slice() {
|
||||
return one.color.tinted(lookup(&one.curve, t));
|
||||
}
|
||||
let mut paper = [1.0f32; 4];
|
||||
for tone in &self.tones {
|
||||
let k = lookup(&tone.curve, t);
|
||||
for (p, ink) in paper.iter_mut().zip(tone.cmyk) {
|
||||
*p *= 1.0 - ink * k;
|
||||
}
|
||||
}
|
||||
let [c, m, y, k] = paper.map(|p| 1.0 - p);
|
||||
Color::cmyk(c, m, y, k)
|
||||
}
|
||||
|
||||
/// Display components 0..1 through the effect.
|
||||
pub(crate) fn apply_rgb(&self, rgb: [f32; 3]) -> [f32; 3] {
|
||||
let x = tone(rgb) * (LUT - 1) as f32;
|
||||
let i = (x.floor() as usize).min(LUT - 2);
|
||||
let f = x - i as f32;
|
||||
match (self.rgb.get(i), self.rgb.get(i + 1)) {
|
||||
(Some(a), Some(b)) => std::array::from_fn(|k| a[k] + (b[k] - a[k]) * f),
|
||||
_ => rgb,
|
||||
}
|
||||
}
|
||||
|
||||
/// A colour with its swatch link and tint through the effect ([`crate::ColorMap::apply_link`]):
|
||||
/// true when it changed. With `skipSpot`, a colour linked to a spot swatch stays.
|
||||
pub(crate) fn map_link(&self, c: &mut Color, link: &mut Option<String>, tint: &mut f32, swatches: &SwatchKinds) -> bool {
|
||||
if self.skip_spot && link.as_deref().and_then(swatches) == Some(true) {
|
||||
return false;
|
||||
}
|
||||
let t = tone(c.to_rgb());
|
||||
let new = self.mix(t);
|
||||
let (new_link, new_tint) = match self.tones.as_slice() {
|
||||
[one] => match one.swatch.as_ref().filter(|n| swatches(n).is_some()) {
|
||||
Some(name) => (Some(name.clone()), lookup(&one.curve, t)),
|
||||
None => (None, 1.0),
|
||||
},
|
||||
_ => (None, 1.0),
|
||||
};
|
||||
if new == *c && new_link == *link && new_tint == *tint {
|
||||
return false;
|
||||
}
|
||||
(*c, *link, *tint) = (new, new_link, new_tint);
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// The grey-equivalent tone of display colour `rgb`: 0 for white, 1 for black (its luma, as
|
||||
/// Convert to Grayscale reads it, so a grey's tone is its ink).
|
||||
fn tone([r, g, b]: [f32; 3]) -> f32 {
|
||||
let t = 1.0 - (0.299 * r + 0.587 * g + 0.114 * b);
|
||||
if t.is_finite() { t.clamp(0.0, 1.0) } else { 0.0 }
|
||||
}
|
||||
|
||||
/// `c` with its components finite and in range.
|
||||
fn finite(c: Color) -> Color {
|
||||
let v = |x: f32, lo: f32, hi: f32| if x.is_finite() { x.clamp(lo, hi) } else { lo };
|
||||
match c {
|
||||
Color::Rgb { r, g, b } => Color::rgb(v(r, 0.0, 1.0), v(g, 0.0, 1.0), v(b, 0.0, 1.0)),
|
||||
Color::Cmyk { c, m, y, k } => Color::cmyk(v(c, 0.0, 1.0), v(m, 0.0, 1.0), v(y, 0.0, 1.0), v(k, 0.0, 1.0)),
|
||||
Color::Gray { k } => Color::gray(v(k, 0.0, 1.0)),
|
||||
Color::Lab { l, a, b } => Color::lab(v(l, 0.0, 100.0), v(a, -128.0, 127.0), v(b, -128.0, 127.0)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,128 @@
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::Effect;
|
||||
use vectorcraft_doc::color::Color;
|
||||
|
||||
use super::*;
|
||||
use crate::{ColorMap, color_map};
|
||||
|
||||
fn map(params: Value) -> ColorMap {
|
||||
color_map(&[Effect { id: DUOTONE.into(), params, visible: true }]).unwrap()
|
||||
}
|
||||
|
||||
/// `c` with link `link` through `m`, the document having spot swatch "Ink" and process swatch
|
||||
/// "Blue": (colour, link, tint, changed).
|
||||
fn linked(m: &ColorMap, c: Color, link: Option<&str>) -> (Color, Option<String>, f32, bool) {
|
||||
let (mut c, mut link, mut tint) = (c, link.map(str::to_string), 1.0);
|
||||
let kinds = |n: &str| match n {
|
||||
"Ink" => Some(true),
|
||||
"Blue" => Some(false),
|
||||
_ => None,
|
||||
};
|
||||
let changed = m.apply_link(&mut c, &mut link, &mut tint, &kinds);
|
||||
(c, link, tint, changed)
|
||||
}
|
||||
|
||||
fn close(a: f32, b: f32) -> bool {
|
||||
(a - b).abs() < 0.01
|
||||
}
|
||||
|
||||
fn looks(c: Color, rgb: [f32; 3]) -> bool {
|
||||
c.to_rgb().iter().zip(rgb).all(|(a, b)| close(*a, b))
|
||||
}
|
||||
|
||||
const INK: Color = Color::Cmyk { c: 0.0, m: 0.5, y: 1.0, k: 0.0 };
|
||||
|
||||
fn ink_tone(curve: &str) -> Value {
|
||||
json!({"tones": [{"color": INK, "swatch": "Ink", "curve": curve}]})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_spot_tone_turns_greys_into_tints_of_its_swatch() {
|
||||
let m = map(ink_tone("0,0 100,100"));
|
||||
for k in [0.0, 0.25, 0.6, 1.0] {
|
||||
let (c, link, tint, _) = linked(&m, Color::gray(k), None);
|
||||
assert_eq!(link.as_deref(), Some("Ink"), "linked at {k}");
|
||||
assert!(close(tint, k), "the tint is the grey's ink: {k} → {tint}");
|
||||
assert_eq!(c, INK.tinted(tint), "its colour is the swatch's at that tint");
|
||||
}
|
||||
// A colour reads as its grey equivalent: white is no ink.
|
||||
let (_, _, tint, _) = linked(&m, Color::rgb(1.0, 1.0, 1.0), None);
|
||||
assert!(close(tint, 0.0));
|
||||
// A swatch the document hasn't: an unlinked tint of the tone's colour.
|
||||
let (c, link, ..) = linked(&map(json!({"tones": [{"color": INK, "swatch": "Gone"}]})), Color::gray(0.5), None);
|
||||
assert!(link.is_none() && looks(c, INK.tinted(0.5).to_rgb()));
|
||||
// Without the links, colours (mesh points) take the tone's colour too.
|
||||
assert!(looks(m.apply(Color::gray(0.5)), INK.tinted(0.5).to_rgb()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_curve_changes_the_mapping() {
|
||||
// Inverse: dark greys get little ink.
|
||||
let (_, _, tint, _) = linked(&map(ink_tone("0,100 100,0")), Color::gray(0.8), None);
|
||||
assert!(close(tint, 0.2), "{tint}");
|
||||
// Through its points exactly, flat past its ends.
|
||||
let lifted = map(ink_tone("0,0 50,80 100,100"));
|
||||
assert!(close(linked(&lifted, Color::gray(0.5), None).2, 0.8));
|
||||
assert!(close(linked(&map(ink_tone("20,0 80,100")), Color::gray(0.1), None).2, 0.0));
|
||||
// Unreadable or missing curves are linear; points past the cap are left out.
|
||||
for curve in [json!("junk"), json!(null), json!([[0, 0]])] {
|
||||
let p = json!({"tones": [{"color": INK, "swatch": "Ink", "curve": curve}]});
|
||||
assert!(close(linked(&map(p), Color::gray(0.3), None).2, 0.3), "{curve}");
|
||||
}
|
||||
let many: Vec<[f64; 2]> = (0..200).map(|i| [f64::from(i) / 2.0, 100.0 - f64::from(i) / 2.0]).collect();
|
||||
assert_eq!(tone_curve_points(Some(&json!(many))).len(), MAX_CURVE_POINTS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn several_tones_multiply_as_cmyk_inks() {
|
||||
let cyan = json!({"color": {"model": "cmyk", "c": 1.0, "m": 0.0, "y": 0.0, "k": 0.0}, "swatch": "Ink"});
|
||||
let half_black = json!({"color": {"model": "cmyk", "c": 0.6, "m": 0.0, "y": 0.0, "k": 1.0}, "curve": "0,0 100,50"});
|
||||
let m = map(json!({"tones": [cyan, half_black]}));
|
||||
let (c, link, tint, changed) = linked(&m, Color::gray(1.0), Some("Ink"));
|
||||
assert!(changed && link.is_none() && tint == 1.0, "a process mix, unlinked");
|
||||
// Cyan 100% over the second ink at 50% (60% cyan, 100% black at half): cyan 1-(1-1)(1-0.3).
|
||||
let Color::Cmyk { c, m: mg, y, k } = c else { panic!("CMYK: {c:?}") };
|
||||
assert!(close(c, 1.0) && close(mg, 0.0) && close(y, 0.0) && close(k, 0.5), "{c} {mg} {y} {k}");
|
||||
let (mid, ..) = linked(&m, Color::gray(0.5), None);
|
||||
let Color::Cmyk { c, k, .. } = mid else { panic!("CMYK") };
|
||||
// Cyan 50% over the second ink at 25% (15% cyan, 25% black): 1 - 0.5 * 0.85 = 0.575.
|
||||
assert!(close(c, 0.575) && close(k, 0.25), "{c} {k}");
|
||||
// Inactive tones and tones past the fourth don't count; none active leaves the colours.
|
||||
let one = json!({"color": "#000000"});
|
||||
let off = json!({"color": "#ff0000", "active": false});
|
||||
let only_black = map(json!({"tones": [one, off, off, off, {"color": "#ff0000"}]}));
|
||||
assert!(looks(only_black.apply(Color::gray(0.4)), [0.6; 3]));
|
||||
assert!(color_map(&[Effect { id: DUOTONE.into(), params: json!({"tones": [off]}), visible: true }]).is_none());
|
||||
assert!(color_map(&[Effect { id: DUOTONE.into(), params: json!({"tones": "x"}), visible: true }]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skip_spot_keeps_spot_colours() {
|
||||
let mut p = json!({"tones": [{"color": "#000000"}], "skipSpot": true});
|
||||
let m = map(p.clone());
|
||||
let spot = Color::cmyk(0.0, 1.0, 0.0, 0.0);
|
||||
assert_eq!(linked(&m, spot, Some("Ink")), (spot, Some("Ink".to_string()), 1.0, false));
|
||||
// A process swatch's colour, and an unlinked one, still change.
|
||||
assert!(linked(&m, spot, Some("Blue")).3);
|
||||
assert!(linked(&m, spot, None).3);
|
||||
p["skipSpot"] = json!(false);
|
||||
assert!(linked(&map(p), spot, Some("Ink")).3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pixels_and_other_adjustments_compose() {
|
||||
let m = map(json!({"tones": [{"color": "#ff0000"}]}));
|
||||
// A grey pixel half way: half way to red from white.
|
||||
let px = m.apply_rgb([0.5, 0.5, 0.5]);
|
||||
assert!(close(px[0], 1.0) && close(px[1], 0.5) && close(px[2], 0.5), "{px:?}");
|
||||
// Invert, then Duotone: white becomes full red.
|
||||
let both = color_map(&[
|
||||
Effect { id: crate::addons::color::INVERT.into(), params: json!({}), visible: true },
|
||||
Effect { id: DUOTONE.into(), params: json!({"tones": [{"color": "#ff0000"}]}), visible: true },
|
||||
])
|
||||
.unwrap();
|
||||
assert!(looks(both.apply(Color::WHITE), [1.0, 0.0, 0.0]));
|
||||
// Out of range colours are brought into range.
|
||||
let wild = map(json!({"tones": [{"color": [1e30, -5, 0.5]}]}));
|
||||
assert!(looks(wild.apply(Color::gray(1.0)), [1.0, 0.0, 0.5]));
|
||||
}
|
||||
@@ -0,0 +1,489 @@
|
||||
//! Vector Halftone (Color Effects): the art as vector dots, lines or shapes sized by its tone, as a
|
||||
//! live effect (Effect › Color Effects › Vector Halftone…, [`HALFTONE`]) and for Object › Vector
|
||||
//! Halftone, which builds its paths with the same screens ([`Halftone::layers`]).
|
||||
//!
|
||||
//! The art is sampled from pixels ([`Samples`], drawn by the renderer) once per halftone cell on a
|
||||
//! grid turned by the screen angle; each cell gets a shape whose area follows the tone there. Mono
|
||||
//! screens follow lightness in one colour (or, with `fromArt`, each dot takes the art's colour under
|
||||
//! it); CMYK screens follow the art's ink amounts (as CMYK output separates it), one screen per ink at
|
||||
//! the classic angles, multiplied over each other. The dots can be clipped to the art's outline as
|
||||
//! real geometry.
|
||||
//!
|
||||
//! The renderer, the exporters ([`crate::bake_document`]) and Expand Appearance draw an object with
|
||||
//! the effect as [`halftone_art`]: the renderer samples the art, so it registers how ([`set_art`]).
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::color::{BlendMode, Color, Paint};
|
||||
use vectorcraft_doc::{Appearance, Document, Node, NodeId};
|
||||
use vectorcraft_geom::hit::fill_contains;
|
||||
use vectorcraft_geom::{Affine, BezPath, FillRule, PathData, Point, Rect, shapes};
|
||||
use vectorcraft_pathops::{self as po, BoolOp};
|
||||
|
||||
use crate::catalog::{EffectInfo, Lengths};
|
||||
use crate::util::{flag, num, text};
|
||||
|
||||
/// Vector Halftone, the live effect.
|
||||
pub const HALFTONE: &str = "addon.color.halftone";
|
||||
|
||||
/// Most halftone cells per screen.
|
||||
pub const MAX_CELLS: f64 = 100_000.0;
|
||||
/// Most pixels the tone is sampled from.
|
||||
pub const MAX_SAMPLE_PIXELS: f64 = 16.0e6;
|
||||
/// Cells lighter than this get no dot.
|
||||
const MIN_TONE: f64 = 0.01;
|
||||
/// Levels per channel the art's colours are rounded to with `fromArt` (one path per colour).
|
||||
const COLOR_LEVELS: f64 = 15.0;
|
||||
|
||||
/// The effect's catalogue entry.
|
||||
pub(crate) fn catalog() -> Vec<EffectInfo> {
|
||||
vec![EffectInfo {
|
||||
id: HALFTONE,
|
||||
label: "Vector Halftone…",
|
||||
menu: &["Effect", "Color Effects"],
|
||||
params: "{shape: \"circle\"|\"ellipse\"|\"square\"|\"diamond\"|\"line\" (circle), frequency: lines per inch 1..300 (20), angle: deg (45; CMYK: the black screen's, cyan −30°, magenta +30°, yellow −45° from it), mode: \"mono\"|\"cmyk\" (mono), color: mono dot colour (\"#000000\"), fromArt: bool (false; mono: each dot takes the art's colour under it), invert: bool (false; dots for light tones), clip: bool (true; the dots clipped to the art's outline)} draws the object as vector halftone dots sized by its tone, as object.vectorHalftone makes them (CMYK screens multiply); the object is sampled as drawn with its other effects; at most 100000 cells per screen, past that the object draws as it is",
|
||||
defaults: json!({"shape": "circle", "frequency": 20.0, "angle": 45.0, "mode": "mono", "color": "#000000", "fromArt": false, "invert": false, "clip": true}),
|
||||
raster: false,
|
||||
lengths: Lengths::default(),
|
||||
}]
|
||||
}
|
||||
|
||||
/// Does `n` carry a visible Vector Halftone effect?
|
||||
pub fn has_halftone(n: &Node) -> bool {
|
||||
n.appearance.effects.iter().any(|e| e.visible && e.id == HALFTONE)
|
||||
}
|
||||
|
||||
/// The shape of a halftone dot.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum DotShape {
|
||||
Circle,
|
||||
Ellipse,
|
||||
Square,
|
||||
Diamond,
|
||||
Line,
|
||||
}
|
||||
|
||||
impl DotShape {
|
||||
pub fn parse(s: &str) -> Option<Self> {
|
||||
Some(match s.to_ascii_lowercase().as_str() {
|
||||
"circle" | "round" | "dot" => DotShape::Circle,
|
||||
"ellipse" => DotShape::Ellipse,
|
||||
"square" => DotShape::Square,
|
||||
"diamond" => DotShape::Diamond,
|
||||
"line" | "lines" => DotShape::Line,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// The shape for tone `t` (0..1) in a cell of side `s` centred on the origin, the screen
|
||||
/// running along x.
|
||||
fn path(self, t: f64, s: f64) -> PathData {
|
||||
let square = |side: f64| shapes::rectangle(Rect::from_center_size(Point::ZERO, (side, side)));
|
||||
match self {
|
||||
DotShape::Circle => {
|
||||
let r = s * (t / std::f64::consts::PI).sqrt();
|
||||
shapes::ellipse(Rect::from_center_size(Point::ZERO, (2.0 * r, 2.0 * r)))
|
||||
}
|
||||
DotShape::Ellipse => {
|
||||
let r = s * (t / std::f64::consts::PI).sqrt();
|
||||
shapes::ellipse(Rect::from_center_size(Point::ZERO, (2.6 * r, 2.0 * r / 1.3)))
|
||||
}
|
||||
DotShape::Square => square(s * t.sqrt()),
|
||||
DotShape::Diamond => square(s * t.sqrt()).transformed(Affine::rotate(std::f64::consts::FRAC_PI_4)),
|
||||
// Cells along a row join into a line as thick as the tone.
|
||||
DotShape::Line => shapes::rectangle(Rect::from_center_size(Point::ZERO, (s * 1.001, s * t))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One screen: its ink and angle, and how to read its tone from a sampled pixel.
|
||||
struct Screen {
|
||||
paint: Color,
|
||||
angle: f64,
|
||||
/// Index into the sampled pixel (unused for mono, CMYK inks for cmyk).
|
||||
channel: usize,
|
||||
}
|
||||
|
||||
/// The sampled art: `w`×`h` pixels of 4 bytes over `region` at `scale` px/pt.
|
||||
pub struct Samples {
|
||||
px: Vec<u8>,
|
||||
w: usize,
|
||||
h: usize,
|
||||
region: Rect,
|
||||
scale: f64,
|
||||
/// Mono: tones are 1 − lightness of RGBA pixels drawn on white; CMYK: ink amounts.
|
||||
mono: bool,
|
||||
invert: bool,
|
||||
}
|
||||
|
||||
impl Samples {
|
||||
/// Pixels `px` (`w`×`h`, 4 bytes each) over `region` at `scale` px/pt; `None` when there are
|
||||
/// fewer bytes than that.
|
||||
pub fn new(px: Vec<u8>, (w, h): (usize, usize), region: Rect, scale: f64, mono: bool, invert: bool) -> Option<Self> {
|
||||
(px.len() >= w.checked_mul(h)?.checked_mul(4)?).then_some(Self { px, w, h, region, scale, mono, invert })
|
||||
}
|
||||
|
||||
/// The index of the pixel at document point `p`, `None` outside the samples.
|
||||
fn index(&self, p: Point) -> Option<usize> {
|
||||
let x = ((p.x - self.region.x0) * self.scale).floor();
|
||||
let y = ((p.y - self.region.y0) * self.scale).floor();
|
||||
(x >= 0.0 && y >= 0.0 && (x as usize) < self.w && (y as usize) < self.h).then(|| (y as usize * self.w + x as usize) * 4)
|
||||
}
|
||||
|
||||
fn byte(&self, i: usize, k: usize) -> f64 {
|
||||
self.px.get(i + k).copied().unwrap_or(0) as f64 / 255.0
|
||||
}
|
||||
|
||||
/// The tone (0..1) of `channel` at document point `p`.
|
||||
fn tone(&self, p: Point, channel: usize) -> f64 {
|
||||
let Some(i) = self.index(p) else { return if self.invert { 1.0 } else { 0.0 } };
|
||||
let t =
|
||||
if self.mono { 1.0 - (0.2126 * self.byte(i, 0) + 0.7152 * self.byte(i, 1) + 0.0722 * self.byte(i, 2)) } else { self.byte(i, channel) };
|
||||
if self.invert { 1.0 - t } else { t }
|
||||
}
|
||||
|
||||
/// The 3 × 3 sample points of a cell of side `s` at `c` turned by `rot`.
|
||||
fn cell_points(c: Point, s: f64, rot: Affine) -> impl Iterator<Item = Point> {
|
||||
(0..9).map(move |k| c + (rot * Point::new((f64::from(k / 3) - 1.0) * s / 3.0, (f64::from(k % 3) - 1.0) * s / 3.0)).to_vec2())
|
||||
}
|
||||
|
||||
/// The mean tone over a cell of side `s` at `c` turned by `rot` (3 × 3 samples).
|
||||
fn cell_tone(&self, c: Point, s: f64, rot: Affine, channel: usize) -> f64 {
|
||||
Self::cell_points(c, s, rot).map(|p| self.tone(p, channel)).sum::<f64>() / 9.0
|
||||
}
|
||||
|
||||
/// The mean colour over a cell (as [`Self::cell_tone`]; white outside the samples), each channel
|
||||
/// rounded to [`COLOR_LEVELS`] steps.
|
||||
fn cell_color(&self, c: Point, s: f64, rot: Affine) -> [u8; 3] {
|
||||
let mut sum = [0.0; 3];
|
||||
for p in Self::cell_points(c, s, rot) {
|
||||
for (k, v) in sum.iter_mut().enumerate() {
|
||||
*v += self.index(p).map_or(1.0, |i| self.byte(i, k));
|
||||
}
|
||||
}
|
||||
sum.map(|v| (v / 9.0 * COLOR_LEVELS).round().clamp(0.0, COLOR_LEVELS) as u8)
|
||||
}
|
||||
}
|
||||
|
||||
/// A halftone's settings.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Halftone {
|
||||
pub shape: DotShape,
|
||||
/// Lines per inch.
|
||||
pub frequency: f64,
|
||||
/// Degrees.
|
||||
pub angle: f64,
|
||||
/// One screen in `color` (else four CMYK screens).
|
||||
pub mono: bool,
|
||||
pub color: Color,
|
||||
/// Mono: each dot in the art's colour under it instead of `color`.
|
||||
pub from_art: bool,
|
||||
/// Dots for light tones.
|
||||
pub invert: bool,
|
||||
/// The dots clipped to the art's outline.
|
||||
pub clip: bool,
|
||||
}
|
||||
|
||||
impl Halftone {
|
||||
/// The settings of `n`'s last visible Vector Halftone effect, its values brought into range.
|
||||
pub fn of(n: &Node) -> Option<Self> {
|
||||
let e = n.appearance.effects.iter().rev().find(|e| e.visible && e.id == HALFTONE)?;
|
||||
Some(Self::from_params(&e.params))
|
||||
}
|
||||
|
||||
fn from_params(p: &Value) -> Self {
|
||||
Self {
|
||||
shape: DotShape::parse(text(p, "shape", "circle")).unwrap_or(DotShape::Circle),
|
||||
frequency: num(p, "frequency", 20.0).clamp(1.0, 300.0),
|
||||
angle: num(p, "angle", 45.0),
|
||||
mono: !text(p, "mode", "mono").eq_ignore_ascii_case("cmyk"),
|
||||
color: crate::raster::color_param(p, Color::BLACK),
|
||||
from_art: flag(p, "fromArt", false),
|
||||
invert: flag(p, "invert", false),
|
||||
clip: flag(p, "clip", true),
|
||||
}
|
||||
}
|
||||
|
||||
/// The side of a cell in points.
|
||||
pub fn cell(&self) -> f64 {
|
||||
72.0 / self.frequency
|
||||
}
|
||||
|
||||
/// Where the art over `bounds` is sampled and at how many pixels per point: about four pixels per
|
||||
/// cell, at most [`MAX_SAMPLE_PIXELS`]. `None` when `bounds` have no area or the grid turned by
|
||||
/// the screen angle would have more than [`MAX_CELLS`] cells.
|
||||
pub fn plan(&self, bounds: Rect) -> Option<(Rect, f64)> {
|
||||
let cell = self.cell();
|
||||
if !(bounds.width() > 0.0 && bounds.height() > 0.0 && bounds.area().is_finite()) {
|
||||
return None;
|
||||
}
|
||||
// The grid turned by the screen angle covers at most (w + h)² / 2.
|
||||
if (bounds.width() + bounds.height()).powi(2) / 2.0 / (cell * cell) > MAX_CELLS {
|
||||
return None;
|
||||
}
|
||||
let scale = (4.0 / cell).min((MAX_SAMPLE_PIXELS / bounds.area()).sqrt()).clamp(0.01, 16.0);
|
||||
Some((bounds.inflate(cell, cell), scale))
|
||||
}
|
||||
|
||||
/// The screens, bottom to top.
|
||||
fn screens(&self) -> Vec<Screen> {
|
||||
let angle = self.angle;
|
||||
if self.mono {
|
||||
return vec![Screen { paint: self.color, angle, channel: 0 }];
|
||||
}
|
||||
// Bottom to top: yellow, magenta, cyan, black.
|
||||
vec![
|
||||
Screen { paint: Color::cmyk(0.0, 0.0, 1.0, 0.0), angle: angle - 45.0, channel: 2 },
|
||||
Screen { paint: Color::cmyk(0.0, 1.0, 0.0, 0.0), angle: angle + 30.0, channel: 1 },
|
||||
Screen { paint: Color::cmyk(1.0, 0.0, 0.0, 0.0), angle: angle - 30.0, channel: 0 },
|
||||
Screen { paint: Color::cmyk(0.0, 0.0, 0.0, 1.0), angle, channel: 3 },
|
||||
]
|
||||
}
|
||||
|
||||
/// The dots of the art `samples` show over `bounds`, one path per ink (per colour with
|
||||
/// `fromArt`) in paint order, each clipped to `outline` when given, and how many dots there are.
|
||||
pub fn layers(&self, samples: &Samples, bounds: Rect, outline: Option<&(BezPath, FillRule)>) -> (Vec<(PathData, Color)>, usize) {
|
||||
let cell = self.cell();
|
||||
let mut layers = vec![];
|
||||
let mut count = 0;
|
||||
for sc in self.screens() {
|
||||
let dots = screen_dots(samples, bounds, cell, &sc, self.shape);
|
||||
count += dots.len();
|
||||
if self.mono && self.from_art {
|
||||
let rot = Affine::rotate(sc.angle.to_radians());
|
||||
let mut by_color: BTreeMap<[u8; 3], Vec<PathData>> = BTreeMap::new();
|
||||
for (dot, c) in dots {
|
||||
by_color.entry(samples.cell_color(c, cell, rot)).or_default().push(dot);
|
||||
}
|
||||
for (rgb, dots) in by_color {
|
||||
let [r, g, b] = rgb.map(|v| (f64::from(v) / COLOR_LEVELS) as f32);
|
||||
layers.push((merged(dots, outline), Color::rgb(r, g, b)));
|
||||
}
|
||||
} else {
|
||||
layers.push((merged(dots.into_iter().map(|d| d.0).collect(), outline), sc.paint));
|
||||
}
|
||||
}
|
||||
layers.retain(|(path, _)| !path.is_empty());
|
||||
(layers, count)
|
||||
}
|
||||
}
|
||||
|
||||
/// The dots of one screen over `bounds`, each with its cell's centre.
|
||||
fn screen_dots(samples: &Samples, bounds: Rect, cell: f64, sc: &Screen, shape: DotShape) -> Vec<(PathData, Point)> {
|
||||
let rot = Affine::rotate(sc.angle.to_radians());
|
||||
let inv = rot.inverse();
|
||||
// The grid covers the bounds in screen space.
|
||||
let corners =
|
||||
[bounds.origin(), Point::new(bounds.x1, bounds.y0), Point::new(bounds.x1, bounds.y1), Point::new(bounds.x0, bounds.y1)].map(|q| inv * q);
|
||||
let (mut lo, mut hi) = (corners[0], corners[0]);
|
||||
for q in &corners[1..] {
|
||||
lo = Point::new(lo.x.min(q.x), lo.y.min(q.y));
|
||||
hi = Point::new(hi.x.max(q.x), hi.y.max(q.y));
|
||||
}
|
||||
let (i0, i1) = ((lo.x / cell).floor() as i64 - 1, (hi.x / cell).ceil() as i64 + 1);
|
||||
let (j0, j1) = ((lo.y / cell).floor() as i64 - 1, (hi.y / cell).ceil() as i64 + 1);
|
||||
let near = bounds.inflate(cell, cell);
|
||||
let mut dots = vec![];
|
||||
for j in j0..=j1 {
|
||||
for i in i0..=i1 {
|
||||
let c = rot * Point::new((i as f64 + 0.5) * cell, (j as f64 + 0.5) * cell);
|
||||
if !near.contains(c) {
|
||||
continue;
|
||||
}
|
||||
let t = samples.cell_tone(c, cell, rot, sc.channel).clamp(0.0, 1.0);
|
||||
if t < MIN_TONE {
|
||||
continue;
|
||||
}
|
||||
let place = Affine::translate(c.to_vec2()) * rot;
|
||||
dots.push((shape.path(t, cell).transformed(place), c));
|
||||
}
|
||||
}
|
||||
dots
|
||||
}
|
||||
|
||||
/// The outline Clip to Art clips the dots of `nodes` to: their clip outline taken together.
|
||||
pub fn art_outline(nodes: &[Node]) -> Option<(BezPath, FillRule)> {
|
||||
crate::clip_outline(&Node::group(NodeId(u64::MAX), nodes.iter().cloned().map(Arc::new).collect()))
|
||||
}
|
||||
|
||||
/// `dots` as one path, clipped to `outline` when given (dots wholly inside it need no clipping).
|
||||
fn merged(dots: Vec<PathData>, outline: Option<&(BezPath, FillRule)>) -> PathData {
|
||||
let (mut inside, mut edge) = (vec![], vec![]);
|
||||
for dot in dots {
|
||||
let whole = match (outline, dot.control_bounds()) {
|
||||
(Some((bp, rule)), Some(db)) => [db.origin(), Point::new(db.x1, db.y0), Point::new(db.x1, db.y1), Point::new(db.x0, db.y1)]
|
||||
.iter()
|
||||
.all(|q| fill_contains(bp, *rule, *q)),
|
||||
_ => true,
|
||||
};
|
||||
if whole { inside.push(dot) } else { edge.push(dot) }
|
||||
}
|
||||
let mut subs: Vec<_> = inside.into_iter().flat_map(|p| p.subpaths).collect();
|
||||
if let Some((bp, rule)) = outline
|
||||
&& !edge.is_empty()
|
||||
{
|
||||
let edge = PathData::new(edge.into_iter().flat_map(|p| p.subpaths).collect());
|
||||
let clipped = po::boolean(&edge, FillRule::NonZero, &PathData::from_bezpath(bp), *rule, BoolOp::Intersect);
|
||||
subs.extend(clipped.subpaths);
|
||||
} else {
|
||||
subs.extend(edge.into_iter().flat_map(|p| p.subpaths));
|
||||
}
|
||||
PathData::new(subs)
|
||||
}
|
||||
|
||||
/// The paths of `layers` ([`Halftone::layers`]), each filled with its ink and ids from `id`; CMYK
|
||||
/// screens (`mono` false) multiply over each other.
|
||||
pub fn dot_nodes(layers: Vec<(PathData, Color)>, mono: bool, id: &mut dyn FnMut() -> NodeId) -> Vec<Arc<Node>> {
|
||||
layers
|
||||
.into_iter()
|
||||
.map(|(path, ink)| {
|
||||
let mut n = Node::path(id(), path, Appearance::basic(Paint::solid(ink), Paint::None, 0.0));
|
||||
if !mono {
|
||||
n.blend = BlendMode::Multiply;
|
||||
}
|
||||
Arc::new(n)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `n` split for its halftone: the object the effect samples (`n` visible, without its Vector
|
||||
/// Halftone effects, transparency, mask and link) and an empty group that takes over its identity,
|
||||
/// transparency, mask and link, for the dots.
|
||||
pub fn split(n: &Node) -> (Node, Node) {
|
||||
let mut object = n.clone();
|
||||
object.appearance.effects.retain(|e| e.id != HALFTONE);
|
||||
object.visible = true;
|
||||
let mut g = Node::group(n.id, vec![]);
|
||||
(g.visible, g.locked) = (n.visible, n.locked);
|
||||
g.name = object.name.take();
|
||||
g.opacity = std::mem::replace(&mut object.opacity, 1.0);
|
||||
g.blend = std::mem::take(&mut object.blend);
|
||||
g.isolate = std::mem::take(&mut object.isolate);
|
||||
g.extras_mut().mask = object.extras_mut().mask.take();
|
||||
g.extras_mut().attrs = object.extras_mut().attrs.take();
|
||||
(object, g)
|
||||
}
|
||||
|
||||
/// How far the dots of `n`'s halftone reach past the bounds of the object it samples: nothing when
|
||||
/// they are clipped to its outline, else up to two cells (a dot's centre lies within a cell of the
|
||||
/// bounds, and the dot within another).
|
||||
pub fn reach(n: &Node) -> f64 {
|
||||
Halftone::of(n).filter(|h| !h.clip).map_or(0.0, |h| 2.0 * h.cell())
|
||||
}
|
||||
|
||||
/// Makes the art of an object with a visible Vector Halftone effect ([`halftone_art`]).
|
||||
pub type ArtHook = fn(&Document, &Node) -> Option<Node>;
|
||||
|
||||
static ART: OnceLock<ArtHook> = OnceLock::new();
|
||||
|
||||
/// Register how the art of an object with Vector Halftone is made (the renderer samples it, above
|
||||
/// this crate; the first registration stays).
|
||||
pub fn set_art(f: ArtHook) {
|
||||
// Already set: the same renderer, registered by another session.
|
||||
let _ = ART.set(f);
|
||||
}
|
||||
|
||||
/// The art of `n` with a visible Vector Halftone effect in `d`: the group [`split`] makes, holding
|
||||
/// the dot paths ([`dot_nodes`], ids `n`'s). `None` without the effect, while nothing is registered
|
||||
/// ([`set_art`]), and when the art has no area or would take too many cells ([`Halftone::plan`]).
|
||||
pub fn halftone_art(d: &Document, n: &Node) -> Option<Node> {
|
||||
if !has_halftone(n) {
|
||||
return None;
|
||||
}
|
||||
ART.get()?(d, n)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use vectorcraft_doc::Effect;
|
||||
|
||||
/// A black square's samples: `side` points of black from (10, 10), white around it.
|
||||
fn black_square(side: f64, h: &Halftone) -> (Samples, Rect) {
|
||||
let b = Rect::new(10.0, 10.0, 10.0 + side, 10.0 + side);
|
||||
let (region, scale) = h.plan(b).unwrap();
|
||||
let (w, hh) = ((region.width() * scale).round() as usize, (region.height() * scale).round() as usize);
|
||||
let mut px = vec![255u8; w * hh * 4];
|
||||
for y in 0..hh {
|
||||
for x in 0..w {
|
||||
let p = Point::new(region.x0 + (x as f64 + 0.5) / scale, region.y0 + (y as f64 + 0.5) / scale);
|
||||
if b.contains(p) {
|
||||
px[(y * w + x) * 4..][..3].copy_from_slice(&[200, 0, 0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
(Samples::new(px, (w, hh), region, scale, true, h.invert).unwrap(), b)
|
||||
}
|
||||
|
||||
fn effect(params: Value) -> Node {
|
||||
let mut n = Node::path(NodeId(7), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default());
|
||||
n.appearance.effects.push(Effect { id: HALFTONE.into(), params, visible: true });
|
||||
n
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_effect_is_in_color_effects_with_its_defaults() {
|
||||
let info = crate::effect_info(HALFTONE).unwrap();
|
||||
assert_eq!(info.menu, ["Effect", "Color Effects"]);
|
||||
assert!(!crate::is_geometry(HALFTONE) && !crate::is_raster(HALFTONE) && !crate::is_adjustment(HALFTONE));
|
||||
let h = Halftone::of(&effect(info.defaults)).unwrap();
|
||||
assert_eq!((h.shape, h.frequency, h.angle, h.mono, h.from_art, h.invert, h.clip), (DotShape::Circle, 20.0, 45.0, true, false, false, true));
|
||||
assert_eq!(h.color, Color::BLACK);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values_out_of_range_are_brought_in() {
|
||||
let h = Halftone::of(&effect(json!({"frequency": 1e308, "angle": "NaN", "shape": 7, "mode": "CMYK", "color": "nope"}))).unwrap();
|
||||
assert_eq!((h.frequency, h.angle, h.shape, h.mono, h.color), (300.0, 45.0, DotShape::Circle, false, Color::BLACK));
|
||||
assert_eq!(Halftone::of(&effect(json!({"frequency": -5}))).unwrap().frequency, 1.0);
|
||||
let mut hidden = effect(json!({}));
|
||||
hidden.appearance.effects[0].visible = false;
|
||||
assert!(Halftone::of(&hidden).is_none() && !has_halftone(&hidden));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dots_follow_the_tone_and_the_settings() {
|
||||
let h = Halftone::of(&effect(json!({"frequency": 18}))).unwrap();
|
||||
let (s, b) = black_square(60.0, &h);
|
||||
let (layers, dots) = h.layers(&s, b, None);
|
||||
assert_eq!(layers.len(), 1);
|
||||
assert!((150..=320).contains(&dots), "{dots}");
|
||||
// Inverted, the dark square gets small dots and the white around it full ones.
|
||||
let inv = Halftone { invert: true, ..h.clone() };
|
||||
let (s2, _) = black_square(60.0, &inv);
|
||||
assert!(inv.layers(&s2, b, None).1 > dots);
|
||||
// The colour of the art under each dot.
|
||||
let art = Halftone { from_art: true, ..h.clone() };
|
||||
let (layers, _) = art.layers(&s, b, None);
|
||||
assert!(layers.iter().any(|(_, c)| c.to_hex() == "#cc0000"), "{:?}", layers.iter().map(|l| l.1.to_hex()).collect::<Vec<_>>());
|
||||
// CMYK screens: one layer per ink with dots (red: magenta and yellow).
|
||||
let cmyk = Halftone { mono: false, ..h };
|
||||
let one = Samples::new([0u8, 255, 255, 0].repeat(400), (20, 20), Rect::new(0.0, 0.0, 20.0, 20.0), 1.0, false, false).unwrap();
|
||||
assert_eq!(cmyk.layers(&one, Rect::new(2.0, 2.0, 18.0, 18.0), None).0.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_caps_refuse_too_many_cells() {
|
||||
let h = Halftone::of(&effect(json!({"frequency": 300}))).unwrap();
|
||||
assert!(h.plan(Rect::new(0.0, 0.0, 10_000.0, 10_000.0)).is_none());
|
||||
assert!(h.plan(Rect::new(0.0, 0.0, f64::INFINITY, 10.0)).is_none());
|
||||
assert!(h.plan(Rect::new(0.0, 0.0, 0.0, 10.0)).is_none());
|
||||
assert!(h.plan(Rect::new(0.0, 0.0, 40.0, 40.0)).is_some());
|
||||
assert!(Samples::new(vec![0; 3], (1, 1), Rect::new(0.0, 0.0, 1.0, 1.0), 1.0, true, false).is_none());
|
||||
assert!(Samples::new(vec![], (usize::MAX, 2), Rect::new(0.0, 0.0, 1.0, 1.0), 1.0, true, false).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_group_takes_the_objects_identity() {
|
||||
let mut n = effect(json!({"clip": false, "frequency": 36}));
|
||||
n.opacity = 0.5;
|
||||
n.name = Some("art".into());
|
||||
let (object, g) = split(&n);
|
||||
assert!(!has_halftone(&object));
|
||||
assert_eq!((object.opacity, g.opacity, g.id, g.name.as_deref()), (1.0, 0.5, n.id, Some("art")));
|
||||
assert_eq!(reach(&n), 4.0);
|
||||
assert_eq!(reach(&effect(json!({}))), 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
//! Path Effects (Path Precision Tools): live geometry effects that rewrite an object's path with the
|
||||
//! add-on's path kernels (Effect › Path Effects). Each is evaluated with the other geometry effects
|
||||
//! ([`crate::apply_geometry`]), so it draws, measures, exports and expands as they do; the geometry
|
||||
//! is written only by Expand Appearance.
|
||||
//!
|
||||
//! Parameters come from documents and scripts, so each is clamped to its documented range, and a
|
||||
//! path with a coordinate past [`MAX_COORDINATE`] or not finite is left as it is.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::Dash;
|
||||
use vectorcraft_geom::{PathData, Point, SubPath};
|
||||
use vectorcraft_pathops::addons::pathtools::{self as tools, AnchorSpacing, CloseMode, ExtendMode, SMOOTH_RATIO, SegmentKind};
|
||||
use vectorcraft_pathops::addons::{PT_PER_TOLERANCE, retract_tiny_handles, simplify_keep_shape};
|
||||
|
||||
use crate::catalog::{EffectInfo, Lengths};
|
||||
use crate::stroke;
|
||||
use crate::util::{flag, num, text};
|
||||
|
||||
/// What every Path Effect's id starts with.
|
||||
pub const PREFIX: &str = "addon.path.";
|
||||
pub const DASHES: &str = "addon.path.dashes";
|
||||
pub const ADD_ANCHORS: &str = "addon.path.addAnchors";
|
||||
pub const EXTEND: &str = "addon.path.extend";
|
||||
pub const CLOSE: &str = "addon.path.close";
|
||||
pub const SMOOTH: &str = "addon.path.smooth";
|
||||
pub const RETRACT: &str = "addon.path.retract";
|
||||
pub const REVERSE: &str = "addon.path.reverse";
|
||||
pub const SIMPLIFY: &str = "addon.path.simplify";
|
||||
pub const SPLIT: &str = "addon.path.split";
|
||||
|
||||
const MENU: &[&str] = &["Effect", "Path Effects"];
|
||||
|
||||
/// The dash and gap parameters of [`DASHES`], pair by pair.
|
||||
const DASH_PAIRS: [(&str, &str); 5] = [("dash1", "gap1"), ("dash2", "gap2"), ("dash3", "gap3"), ("dash4", "gap4"), ("dash5", "gap5")];
|
||||
/// Longest dash or gap, and the farthest offset (pt).
|
||||
const MAX_DASH: f64 = 10_000.0;
|
||||
/// Longest stretch [`EXTEND`] adds or takes off an end, in points and in percent of the path.
|
||||
const MAX_EXTEND: f64 = 100_000.0;
|
||||
const MAX_EXTEND_PERCENT: f64 = 1000.0;
|
||||
/// Most anchors [`ADD_ANCHORS`] adds to one segment.
|
||||
const MAX_PER_SEGMENT: f64 = 1000.0;
|
||||
/// Closest and farthest apart [`ADD_ANCHORS`]' anchors are (pt).
|
||||
const DISTANCE_RANGE: (f64, f64) = (0.01, 100_000.0);
|
||||
/// The range of [`SMOOTH`]'s ratio.
|
||||
const RATIO_RANGE: (f64, f64) = (0.01, 0.5);
|
||||
/// Longest handle length [`RETRACT`] takes (pt).
|
||||
const MAX_HANDLE: f64 = 100_000.0;
|
||||
/// Highest tolerance of [`SIMPLIFY`].
|
||||
const MAX_TOLERANCE: f64 = 200.0;
|
||||
// shortcut: simplifying measures every anchor on every draw, so a larger path draws as it is; lift
|
||||
// this if the kernel gets an incremental pass.
|
||||
/// Most anchors [`SIMPLIFY`] takes in one path.
|
||||
const MAX_SIMPLIFY_ANCHORS: usize = 50_000;
|
||||
/// A path with a coordinate past this, or one that isn't finite, is left as it is: lengths measured
|
||||
/// on it would overflow.
|
||||
const MAX_COORDINATE: f64 = 1e12;
|
||||
|
||||
/// The effects' catalogue entries.
|
||||
pub(crate) fn catalog() -> Vec<EffectInfo> {
|
||||
let entry = |id, label, params, defaults, always| EffectInfo {
|
||||
id,
|
||||
label,
|
||||
menu: MENU,
|
||||
params,
|
||||
defaults,
|
||||
raster: false,
|
||||
lengths: Lengths { always, absolute: &[] },
|
||||
};
|
||||
vec![
|
||||
entry(
|
||||
DASHES,
|
||||
"Dashes to Geometry…",
|
||||
"{dash1: pt 0..10000 (12), gap1: pt 0..10000 (6), dash2: pt 0..10000 (0), gap2: pt 0..10000 (0), dash3: pt 0..10000 (0), gap3: pt 0..10000 (0), dash4: pt 0..10000 (0), gap4: pt 0..10000 (0), dash5: pt 0..10000 (0), gap5: pt 0..10000 (0), offset: pt -10000..10000 (0; how far into the pattern the path starts; not with alignCorners), alignCorners: bool (false; every run between corners and path ends holds whole periods, stretched or squeezed to fit, with a dash centred on each corner and end)} cuts the path into its dashes, open subpaths along it that the stroke paints as it paints any path. A dash of 0 is a point its stroke's round or projecting cap paints; a pair of two zeros is left out, and the pattern repeats. A pattern of nothing, or of more than 200000 dashes, leaves the path as it is; a dash longer than a closed subpath keeps it closed",
|
||||
json!({"dash1": 12.0, "gap1": 6.0, "dash2": 0.0, "gap2": 0.0, "dash3": 0.0, "gap3": 0.0, "dash4": 0.0, "gap4": 0.0, "dash5": 0.0, "gap5": 0.0, "offset": 0.0, "alignCorners": false}),
|
||||
&["dash1", "gap1", "dash2", "gap2", "dash3", "gap3", "dash4", "gap4", "dash5", "gap5", "offset"],
|
||||
),
|
||||
entry(
|
||||
ADD_ANCHORS,
|
||||
"Add Anchors…",
|
||||
"{target: \"all\"|\"curved\"|\"straight\" (all; the segments that get anchors), spacing: \"count\"|\"distance\" (count), count: 1..1000 (1; anchors on each segment, evenly spaced by length), distance: pt 0.01..100000 (20; an anchor every so far along each segment from its start), maxCount: 1..1000 (100; most anchors added to one segment)} adds anchors without changing the shape; past 2 million anchors in all the path stays as it is",
|
||||
json!({"target": "all", "spacing": "count", "count": 1, "distance": 20.0, "maxCount": 100}),
|
||||
&["distance"],
|
||||
),
|
||||
entry(
|
||||
EXTEND,
|
||||
"Extend Path…",
|
||||
"{startLength: pt -100000..100000 (10), startPercent: % of the path's length -1000..1000 (0), startShape: \"straight\"|\"arc\" (straight), endLength: pt -100000..100000 (10), endPercent: % of the path's length -1000..1000 (0), endShape: \"straight\"|\"arc\" (straight)} lengthens each end of an open subpath by its length and percent together, or shortens it back along itself when they come to less than 0. Straight runs on along the end's direction; arc follows the circle of the end's curvature, at most half way round. Closed subpaths stay as they are, and so does an end that would be shortened to nothing",
|
||||
json!({"startLength": 10.0, "startPercent": 0.0, "startShape": "straight", "endLength": 10.0, "endPercent": 0.0, "endShape": "straight"}),
|
||||
&["startLength", "endLength"],
|
||||
),
|
||||
entry(
|
||||
CLOSE,
|
||||
"Close Path…",
|
||||
"{mode: \"straight\"|\"smooth\" (straight; smooth: the closing segment leaves both ends smoothly)} closes every open subpath of two anchors or more",
|
||||
json!({"mode": "straight"}),
|
||||
&[],
|
||||
),
|
||||
entry(
|
||||
SMOOTH,
|
||||
"Smooth Anchors…",
|
||||
"{ratio: 0.01..0.5 (0.3905; a handle's length as a share of the distance to the nearer neighbour; 0.3905 makes a square's corners a circle)} makes every anchor smooth: a lone handle is mirrored, two are brought into line keeping their lengths, and an anchor without handles gets them at right angles to the bisector of its neighbours",
|
||||
json!({"ratio": SMOOTH_RATIO}),
|
||||
&[],
|
||||
),
|
||||
entry(
|
||||
RETRACT,
|
||||
"Retract Handles…",
|
||||
"{handles: \"all\"|\"shorter\" (all; shorter: only those shorter than length), length: pt 0..100000 (5)} takes the handles back to their anchors; an anchor that loses one becomes a corner",
|
||||
json!({"handles": "all", "length": 5.0}),
|
||||
&["length"],
|
||||
),
|
||||
entry(
|
||||
REVERSE,
|
||||
"Reverse Direction",
|
||||
"{} runs every subpath the other way: the shape stays, and arrowheads, dashes and the effects after it swap ends",
|
||||
json!({}),
|
||||
&[],
|
||||
),
|
||||
entry(
|
||||
SIMPLIFY,
|
||||
"Simplify Anchors…",
|
||||
"{tolerance: 0..200 (10; higher removes more anchors and lets the outline move further, up to tolerance × 0.02 pt), protectCorners: bool (true; anchors where the path turns by more than 30° stay)} removes the anchors a path can do without, refitting the handles beside each; open ends stay; a path of more than 50000 anchors stays as it is",
|
||||
json!({"tolerance": 10.0, "protectCorners": true}),
|
||||
&[],
|
||||
),
|
||||
entry(
|
||||
SPLIT,
|
||||
"Split at Anchors…",
|
||||
"{anchors: \"corner\"|\"smooth\"|\"both\" (both; smooth: where the handles stand in line, corner: the others)} cuts subpaths into open pieces at those anchors; the shape stays, and an open path's ends aren't cuts",
|
||||
json!({"anchors": "both"}),
|
||||
&[],
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/// Path Effect `id` with parameters `p` on `path`, which keeps to `max` anchors where anchors are
|
||||
/// added one by one. `path` as it is when `id` is no such effect or the path can't be measured.
|
||||
pub(crate) fn apply(id: &str, p: &Value, path: &PathData, max: usize) -> PathData {
|
||||
if !measurable(path) {
|
||||
return path.clone();
|
||||
}
|
||||
let edit = |change: &dyn Fn(&mut SubPath)| {
|
||||
let mut out = path.clone();
|
||||
out.subpaths.iter_mut().for_each(change);
|
||||
out
|
||||
};
|
||||
match id {
|
||||
DASHES => dashes(path, p),
|
||||
ADD_ANCHORS => add_anchors(path, p, max).unwrap_or_else(|| path.clone()),
|
||||
EXTEND => PathData::new(path.subpaths.iter().map(|sp| extend(sp, p)).collect()),
|
||||
CLOSE => {
|
||||
let mode = if text(p, "mode", "straight") == "smooth" { CloseMode::Smooth } else { CloseMode::Straight };
|
||||
edit(&|sp| {
|
||||
tools::close(sp, mode, SMOOTH_RATIO);
|
||||
})
|
||||
}
|
||||
SMOOTH => {
|
||||
let ratio = num(p, "ratio", SMOOTH_RATIO).clamp(RATIO_RANGE.0, RATIO_RANGE.1);
|
||||
edit(&|sp| tools::smooth_anchors(sp, ratio))
|
||||
}
|
||||
RETRACT if text(p, "handles", "all") == "shorter" => {
|
||||
let length = num(p, "length", 5.0).clamp(0.0, MAX_HANDLE);
|
||||
edit(&|sp| {
|
||||
retract_tiny_handles(sp, length);
|
||||
})
|
||||
}
|
||||
RETRACT => edit(&|sp| sp.anchors.iter_mut().for_each(|a| a.retract())),
|
||||
REVERSE => edit(&SubPath::reverse),
|
||||
SIMPLIFY if path.anchor_count() <= MAX_SIMPLIFY_ANCHORS => {
|
||||
let tolerance = num(p, "tolerance", 10.0).clamp(0.0, MAX_TOLERANCE) * PT_PER_TOLERANCE;
|
||||
simplify_keep_shape(path, tolerance, flag(p, "protectCorners", true))
|
||||
}
|
||||
SPLIT => {
|
||||
let at = text(p, "anchors", "both");
|
||||
let (corners, smooth) = (at != "smooth", at != "corner");
|
||||
PathData::new(path.subpaths.iter().flat_map(|sp| tools::split_at_anchors(sp, corners, smooth)).collect())
|
||||
}
|
||||
_ => path.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Is every coordinate of `path` within [`MAX_COORDINATE`]?
|
||||
fn measurable(path: &PathData) -> bool {
|
||||
let near = |q: Point| q.x.abs() <= MAX_COORDINATE && q.y.abs() <= MAX_COORDINATE;
|
||||
path.anchors().all(|(_, _, a)| near(a.p) && near(a.h_in) && near(a.h_out))
|
||||
}
|
||||
|
||||
/// [`DASHES`]: the dashes of `path` as open subpaths, those of no length as subpaths that stay on
|
||||
/// their point; `path` as it is when the pattern draws no dashes.
|
||||
fn dashes(path: &PathData, p: &Value) -> PathData {
|
||||
let length = |key| num(p, key, 0.0).clamp(0.0, MAX_DASH);
|
||||
let pairs = DASH_PAIRS.iter().map(|(dash, gap)| (length(dash), length(gap)));
|
||||
let pattern = pairs.filter(|(d, g)| *d > 0.0 || *g > 0.0).flat_map(|(d, g)| [d, g]).collect();
|
||||
let dash = Dash { pattern, offset: num(p, "offset", 0.0).clamp(-MAX_DASH, MAX_DASH), align_corners: flag(p, "alignCorners", false) };
|
||||
let Some(dashed) = stroke::dash(&path.to_bezpath(), &dash) else { return path.clone() };
|
||||
let mut out = PathData::from_bezpath(&dashed.path);
|
||||
out.subpaths.extend(dashed.dots.iter().map(|d| SubPath::polyline(&[d.at, d.at], false)));
|
||||
out
|
||||
}
|
||||
|
||||
/// [`ADD_ANCHORS`] within `max` anchors in all; `None` past them.
|
||||
fn add_anchors(path: &PathData, p: &Value, max: usize) -> Option<PathData> {
|
||||
let which = match text(p, "target", "all") {
|
||||
"curved" => SegmentKind::Curved,
|
||||
"straight" => SegmentKind::Straight,
|
||||
_ => SegmentKind::All,
|
||||
};
|
||||
let spacing = match text(p, "spacing", "count") {
|
||||
"distance" => AnchorSpacing::Distance(num(p, "distance", 20.0).clamp(DISTANCE_RANGE.0, DISTANCE_RANGE.1)),
|
||||
_ => AnchorSpacing::Count(num(p, "count", 1.0).clamp(1.0, MAX_PER_SEGMENT) as usize),
|
||||
};
|
||||
let cap = num(p, "maxCount", 100.0).clamp(1.0, MAX_PER_SEGMENT) as usize;
|
||||
let mut room = max.saturating_sub(path.anchor_count());
|
||||
let mut out = Vec::with_capacity(path.subpaths.len());
|
||||
for sp in &path.subpaths {
|
||||
let added = tools::add_anchors(sp, which, spacing, cap, room)?;
|
||||
room = room.saturating_sub(added.anchors.len().saturating_sub(sp.anchors.len()));
|
||||
out.push(added);
|
||||
}
|
||||
Some(PathData::new(out))
|
||||
}
|
||||
|
||||
/// [`EXTEND`] on one subpath, the start then the end; what can't be done stays as it is.
|
||||
fn extend(sp: &SubPath, p: &Value) -> SubPath {
|
||||
let total = sp.anchor_arc_positions().last().copied().unwrap_or(0.0);
|
||||
let by = |length, percent| {
|
||||
num(p, length, 0.0).clamp(-MAX_EXTEND, MAX_EXTEND) + num(p, percent, 0.0).clamp(-MAX_EXTEND_PERCENT, MAX_EXTEND_PERCENT) / 100.0 * total
|
||||
};
|
||||
let shape = |key| if text(p, key, "straight") == "arc" { ExtendMode::Arc } else { ExtendMode::Straight };
|
||||
let ends = [(true, by("startLength", "startPercent"), shape("startShape")), (false, by("endLength", "endPercent"), shape("endShape"))];
|
||||
let mut out = sp.clone();
|
||||
for (at_start, by, shape) in ends {
|
||||
if by != 0.0
|
||||
&& let Some(changed) = tools::extend_open(&out, at_start, by, shape)
|
||||
{
|
||||
out = changed;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,278 @@
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::Effect;
|
||||
use vectorcraft_geom::{Anchor, PathData, Point, Rect, SubPath};
|
||||
|
||||
use super::*;
|
||||
use crate::{apply_geometry, effect_info, is_geometry, is_length, is_raster, length_range};
|
||||
|
||||
fn pt(x: f64, y: f64) -> Point {
|
||||
Point::new(x, y)
|
||||
}
|
||||
|
||||
fn line(pts: &[(f64, f64)], closed: bool) -> PathData {
|
||||
PathData::single(SubPath::polyline(&pts.iter().map(|&(x, y)| pt(x, y)).collect::<Vec<_>>(), closed))
|
||||
}
|
||||
|
||||
fn square() -> PathData {
|
||||
line(&[(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)], true)
|
||||
}
|
||||
|
||||
/// A curve: a smooth anchor between two ends.
|
||||
fn curve() -> PathData {
|
||||
let mid = Anchor::smooth(pt(50.0, -30.0), pt(70.0, -30.0));
|
||||
PathData::single(SubPath::new(
|
||||
vec![
|
||||
Anchor { h_out: pt(10.0, -20.0), ..Anchor::corner(pt(0.0, 0.0)) },
|
||||
mid,
|
||||
Anchor { h_in: pt(90.0, -20.0), ..Anchor::corner(pt(100.0, 0.0)) },
|
||||
],
|
||||
false,
|
||||
))
|
||||
}
|
||||
|
||||
fn run(id: &str, params: Value, path: &PathData) -> PathData {
|
||||
let e = Effect { id: id.into(), params, visible: true };
|
||||
apply_geometry(&[e], path, path.bounds().unwrap_or(Rect::ZERO))
|
||||
}
|
||||
|
||||
fn near(a: f64, b: f64) -> bool {
|
||||
(a - b).abs() < 1e-3
|
||||
}
|
||||
|
||||
const ALL: [&str; 9] = [DASHES, ADD_ANCHORS, EXTEND, CLOSE, SMOOTH, RETRACT, REVERSE, SIMPLIFY, SPLIT];
|
||||
|
||||
#[test]
|
||||
fn the_effects_are_geometry_effects_of_the_path_effects_menu() {
|
||||
for id in ALL {
|
||||
let info = effect_info(id).unwrap();
|
||||
assert!(is_geometry(id) && !is_raster(id), "{id}");
|
||||
assert_eq!(info.menu, ["Effect", "Path Effects"], "{id}");
|
||||
assert!(id.starts_with(PREFIX));
|
||||
}
|
||||
assert!(is_length(DASHES, "gap5", false) && is_length(EXTEND, "endLength", false) && !is_length(EXTEND, "endPercent", false));
|
||||
assert_eq!(length_range(DASHES, "offset"), Some(-10_000.0..=10_000.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dashes_become_open_subpaths_along_the_path() {
|
||||
let out = run(DASHES, json!({"dash1": 10, "gap1": 5}), &line(&[(0.0, 0.0), (100.0, 0.0)], false));
|
||||
assert_eq!(out.subpaths.len(), 7, "periods of 15 on 100");
|
||||
assert!(out.subpaths.iter().all(|s| !s.closed));
|
||||
assert!(near(out.subpaths[1].anchors[0].p.x, 15.0) && near(out.subpaths[1].anchors[1].p.x, 25.0));
|
||||
// Two pairs, and a pair of zeros between them left out.
|
||||
let out = run(DASHES, json!({"dash1": 10, "gap1": 5, "dash3": 20, "gap3": 5}), &line(&[(0.0, 0.0), (100.0, 0.0)], false));
|
||||
assert!(near(out.subpaths[1].anchors[0].p.x, 15.0) && near(out.subpaths[1].anchors[1].p.x, 35.0));
|
||||
assert!(near(out.subpaths[2].anchors[0].p.x, 40.0));
|
||||
// The offset starts part way into the pattern.
|
||||
let out = run(DASHES, json!({"dash1": 10, "gap1": 5, "offset": 5}), &line(&[(0.0, 0.0), (100.0, 0.0)], false));
|
||||
assert!(near(out.subpaths[0].anchors[1].p.x, 5.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aligned_dashes_centre_on_corners_and_ends() {
|
||||
let open = line(&[(0.0, 0.0), (100.0, 0.0), (100.0, 100.0)], false);
|
||||
let out = run(DASHES, json!({"dash1": 10, "gap1": 10, "alignCorners": true}), &open);
|
||||
let on = |p: Point| out.subpaths.iter().any(|s| (0..s.segment_count()).any(|i| s.anchors[i].p == p || s.anchors[i + 1].p == p));
|
||||
assert!(on(pt(0.0, 0.0)) && on(pt(100.0, 100.0)), "a dash on each end");
|
||||
let corner = out.subpaths.iter().find(|s| s.anchors.iter().any(|a| a.p == pt(100.0, 0.0))).expect("a dash over the corner");
|
||||
assert!(near(corner.anchors[0].p.distance(pt(100.0, 0.0)), corner.anchors[2].p.distance(pt(100.0, 0.0))), "centred: {corner:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_dashes_are_points_and_patterns_of_nothing_leave_the_path() {
|
||||
let path = line(&[(0.0, 0.0), (100.0, 0.0)], false);
|
||||
let dots = run(DASHES, json!({"dash1": 0, "gap1": 25}), &path);
|
||||
assert_eq!(dots.subpaths.len(), 5);
|
||||
assert!(dots.subpaths.iter().all(|s| s.anchors.len() == 2 && s.anchors[0].p == s.anchors[1].p));
|
||||
for params in [
|
||||
json!({"dash1": 0, "gap1": 0}),
|
||||
json!({"dash1": -5, "gap1": -5}),
|
||||
json!({"dash1": 0.0001, "gap1": 0.0001}),
|
||||
json!({"dash1": "x", "gap1": null}),
|
||||
] {
|
||||
assert_eq!(run(DASHES, params.clone(), &path), path, "{params}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_anchors_spaces_them_and_keeps_to_the_caps() {
|
||||
let square = square();
|
||||
assert_eq!(run(ADD_ANCHORS, json!({}), &square).anchor_count(), 8, "one on each segment");
|
||||
assert_eq!(run(ADD_ANCHORS, json!({"count": 3}), &square).anchor_count(), 16);
|
||||
assert_eq!(run(ADD_ANCHORS, json!({"count": 3, "maxCount": 2}), &square).anchor_count(), 12);
|
||||
let out = run(ADD_ANCHORS, json!({"spacing": "distance", "distance": 30}), &square);
|
||||
assert_eq!(out.anchor_count(), 16, "at 30, 60 and 90 on each side");
|
||||
assert!(near(out.subpaths[0].anchors[2].p.x, 60.0));
|
||||
assert_eq!(run(ADD_ANCHORS, json!({"spacing": "distance", "distance": 25}), &square).anchor_count(), 16, "none on the segment's end");
|
||||
assert_eq!(run(ADD_ANCHORS, json!({"spacing": "distance", "distance": 1e-9, "maxCount": 2}), &square).anchor_count(), 12);
|
||||
// Only the segments asked for.
|
||||
let mixed = PathData::new(vec![curve().subpaths[0].clone(), square.subpaths[0].clone()]);
|
||||
assert_eq!(run(ADD_ANCHORS, json!({"target": "curved"}), &mixed).anchor_count(), 7 + 2);
|
||||
assert_eq!(run(ADD_ANCHORS, json!({"target": "straight"}), &mixed).anchor_count(), 7 + 4);
|
||||
// The shape stays.
|
||||
let out = run(ADD_ANCHORS, json!({"count": 4}), &curve());
|
||||
assert_eq!(out.anchor_count(), 3 + 8);
|
||||
assert!(near(out.length(), curve().length()));
|
||||
assert_eq!(out.bounds().map(|b| (b.y0 * 100.0).round()), curve().bounds().map(|b| (b.y0 * 100.0).round()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_anchors_past_the_budget_leaves_the_path() {
|
||||
let path = square();
|
||||
let most = json!({"count": 1000, "maxCount": 1000});
|
||||
assert_eq!(super::apply(ADD_ANCHORS, &most, &path, 4003), path);
|
||||
assert_eq!(super::apply(ADD_ANCHORS, &most, &path, 4004).anchor_count(), 4004);
|
||||
// Many segments, each with the most anchors: refused before they are built.
|
||||
let long = line(&(0..4000).map(|i| (i as f64, 0.0)).collect::<Vec<_>>(), false);
|
||||
assert_eq!(run(ADD_ANCHORS, json!({"count": 1000, "maxCount": 1000}), &long).anchor_count(), 4000, "4 million would pass the budget");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extend_lengthens_and_shortens_each_end() {
|
||||
let path = line(&[(0.0, 0.0), (100.0, 0.0)], false);
|
||||
let out = run(EXTEND, json!({"startLength": 20, "endLength": 10}), &path);
|
||||
let xs: Vec<f64> = out.subpaths[0].anchors.iter().map(|a| a.p.x).collect();
|
||||
assert_eq!(xs, [-20.0, 0.0, 100.0, 110.0]);
|
||||
// Percent of the path's length, adding to the length; negative shortens.
|
||||
let out = run(EXTEND, json!({"startLength": 0, "endLength": 5, "endPercent": 10}), &path);
|
||||
assert!(out.subpaths[0].anchors.last().is_some_and(|a| near(a.p.x, 115.0)));
|
||||
let out = run(EXTEND, json!({"startLength": -10, "endLength": 0, "endPercent": -50}), &path);
|
||||
let xs: Vec<f64> = out.subpaths[0].anchors.iter().map(|a| a.p.x).collect();
|
||||
assert!(near(xs[0], 10.0) && near(xs[1], 50.0), "{xs:?}");
|
||||
// An arc carries on round the end's curve.
|
||||
let arc = run(EXTEND, json!({"startLength": 0, "endLength": 40, "endShape": "arc"}), &curve());
|
||||
let straight = run(EXTEND, json!({"startLength": 0, "endLength": 40}), &curve());
|
||||
assert!(arc.anchor_count() >= straight.anchor_count());
|
||||
assert!((arc.length() - straight.length()).abs() < 0.1);
|
||||
assert_ne!(arc.subpaths[0].anchors.last().map(|a| a.p), straight.subpaths[0].anchors.last().map(|a| a.p));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extend_leaves_what_it_cannot_change() {
|
||||
let closed = square();
|
||||
assert_eq!(run(EXTEND, json!({"endLength": 50}), &closed), closed);
|
||||
let path = line(&[(0.0, 0.0), (100.0, 0.0)], false);
|
||||
assert_eq!(run(EXTEND, json!({"startLength": 0, "endLength": -500}), &path), path, "nothing would be left");
|
||||
assert_eq!(run(EXTEND, json!({"startLength": 0, "endLength": 0, "endPercent": -100}), &path), path);
|
||||
let one = PathData::single(SubPath::polyline(&[pt(1.0, 1.0)], false));
|
||||
assert_eq!(run(EXTEND, json!({}), &one), one);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_joins_open_subpaths_straight_or_smooth() {
|
||||
let open = line(&[(0.0, 0.0), (100.0, 0.0), (100.0, 100.0)], false);
|
||||
let out = run(CLOSE, json!({}), &open);
|
||||
assert!(out.subpaths[0].closed && out.subpaths[0].segment_is_line(2));
|
||||
let out = run(CLOSE, json!({"mode": "smooth"}), &open);
|
||||
assert!(out.subpaths[0].closed && !out.subpaths[0].segment_is_line(2));
|
||||
assert_eq!(run(CLOSE, json!({}), &square()), square());
|
||||
let one = PathData::single(SubPath::polyline(&[pt(1.0, 1.0)], false));
|
||||
assert_eq!(run(CLOSE, json!({}), &one), one);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smooth_makes_a_square_round_and_clamps_its_ratio() {
|
||||
let out = run(SMOOTH, json!({}), &square());
|
||||
assert!(out.subpaths[0].anchors.iter().all(|a| near(a.h_out.distance(a.p), 39.05) && a.has_in()));
|
||||
let at = |ratio: f64| run(SMOOTH, json!({ "ratio": ratio }), &square()).subpaths[0].anchors[0];
|
||||
assert!(near(at(9.0).h_out.distance(pt(0.0, 0.0)), 50.0));
|
||||
assert!(near(at(-9.0).h_out.distance(pt(0.0, 0.0)), 1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retract_takes_all_handles_or_the_short_ones() {
|
||||
let out = run(RETRACT, json!({}), &curve());
|
||||
assert!(out.subpaths[0].anchors.iter().all(|a| !a.has_in() && !a.has_out()));
|
||||
// The ends' handles are about 22 pt long and the middle anchor's 20.
|
||||
let out = run(RETRACT, json!({"handles": "shorter", "length": 21}), &curve());
|
||||
let a = &out.subpaths[0].anchors;
|
||||
assert!(!a[1].has_in() && !a[1].has_out() && a[0].has_out() && a[2].has_in());
|
||||
assert_eq!(run(RETRACT, json!({"handles": "shorter", "length": 1}), &curve()), curve());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverse_runs_the_other_way_and_twice_is_as_it_was() {
|
||||
let out = run(REVERSE, json!({}), &curve());
|
||||
assert_eq!(out.subpaths[0].anchors[0].p, pt(100.0, 0.0));
|
||||
assert_eq!(run(REVERSE, json!({}), &out), curve());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simplify_takes_anchors_within_the_tolerance_and_stops_at_its_cap() {
|
||||
let pts: Vec<(f64, f64)> = (0..=40).map(|i| (i as f64, (i as f64 / 40.0 * std::f64::consts::PI).sin() * 3.0)).collect();
|
||||
let path = line(&pts, false);
|
||||
let out = run(SIMPLIFY, json!({"tolerance": 200}), &path);
|
||||
assert!(out.anchor_count() < 20, "{}", out.anchor_count());
|
||||
assert_eq!(run(SIMPLIFY, json!({"tolerance": 0}), &path), path);
|
||||
// Corners stay unless told otherwise.
|
||||
let corner = line(&[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0)], false);
|
||||
assert_eq!(run(SIMPLIFY, json!({"tolerance": 200}), &corner), corner);
|
||||
assert_eq!(run(SIMPLIFY, json!({"tolerance": 200, "protectCorners": false}), &corner).anchor_count(), 2);
|
||||
let long = line(&(0..=MAX_SIMPLIFY_ANCHORS).map(|i| (i as f64, 0.0)).collect::<Vec<_>>(), false);
|
||||
assert_eq!(run(SIMPLIFY, json!({}), &long).anchor_count(), long.anchor_count());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_cuts_at_the_anchors_asked_for() {
|
||||
let mixed = PathData::single(SubPath::new(
|
||||
vec![
|
||||
Anchor::corner(pt(0.0, 0.0)),
|
||||
Anchor::smooth(pt(50.0, 0.0), pt(60.0, 0.0)),
|
||||
Anchor::corner(pt(100.0, 0.0)),
|
||||
Anchor::corner(pt(100.0, 50.0)),
|
||||
],
|
||||
false,
|
||||
));
|
||||
assert_eq!(run(SPLIT, json!({}), &mixed).subpaths.len(), 3);
|
||||
assert_eq!(run(SPLIT, json!({"anchors": "smooth"}), &mixed).subpaths.len(), 2);
|
||||
assert_eq!(run(SPLIT, json!({"anchors": "corner"}), &mixed).subpaths.len(), 2);
|
||||
let out = run(SPLIT, json!({}), &square());
|
||||
assert_eq!(out.subpaths.len(), 4);
|
||||
assert!(out.subpaths.iter().all(|s| !s.closed && s.anchors.len() == 2));
|
||||
assert!(near(out.length(), 400.0));
|
||||
assert_eq!(run(SPLIT, json!({"anchors": "smooth"}), &square()), square(), "no smooth anchors");
|
||||
}
|
||||
|
||||
/// Whatever a file holds: every effect takes wrong types, huge numbers and paths of nothing, of one
|
||||
/// anchor or past measuring, and keeps to the budget.
|
||||
#[test]
|
||||
fn junk_params_and_paths_are_harmless() {
|
||||
let junk = [
|
||||
json!({}),
|
||||
json!(null),
|
||||
json!([1, 2]),
|
||||
json!("x"),
|
||||
json!({"dash1": 1e308, "gap1": 1e308, "dash2": -1e308, "gap2": "x", "offset": 1e308, "alignCorners": "yes"}),
|
||||
json!({"dash1": 1e-300, "gap1": 1e-300, "offset": -1e308, "alignCorners": true}),
|
||||
json!({"target": 3, "spacing": ["distance"], "count": 1e308, "distance": 1e-308, "maxCount": -1e308}),
|
||||
json!({"spacing": "distance", "distance": -1e308, "maxCount": 1e308}),
|
||||
json!({"startLength": 1e308, "startPercent": 1e308, "startShape": 7, "endLength": -1e308, "endPercent": -1e308, "endShape": "arc"}),
|
||||
json!({"mode": {}, "ratio": 1e308, "handles": "shorter", "length": 1e308, "tolerance": 1e308, "protectCorners": "no", "anchors": 9}),
|
||||
json!({"ratio": -1e308, "length": -1e308, "tolerance": -1e308, "anchors": "none"}),
|
||||
];
|
||||
let far = line(&[(1e308, -1e308), (-1e308, 1e308), (1e300, 0.0)], false);
|
||||
let nan = line(&[(f64::NAN, 0.0), (f64::INFINITY, 1.0)], false);
|
||||
let tiny = line(&[(0.0, 0.0), (1e-300, 0.0), (1e-300, 1e-300)], true);
|
||||
let same = line(&[(5.0, 5.0), (5.0, 5.0), (5.0, 5.0)], true);
|
||||
let empty = PathData::single(SubPath::default());
|
||||
let one = PathData::single(SubPath::polyline(&[pt(1.0, 1.0)], true));
|
||||
let paths = [square(), curve(), far.clone(), nan.clone(), tiny, same, empty, one, PathData::default()];
|
||||
for id in ALL {
|
||||
for params in &junk {
|
||||
for path in &paths {
|
||||
let out = super::apply(id, &crate::merged_params(id, params), path, 100_000);
|
||||
assert!(out.anchor_count() <= 100_000.max(path.anchor_count()) + 8 * path.subpaths.len(), "{id} {params}");
|
||||
}
|
||||
}
|
||||
// Compared as text: NaN never equals itself.
|
||||
assert_eq!(format!("{:?}", super::apply(id, &json!({}), &nan, 100)), format!("{nan:?}"), "{id}");
|
||||
assert_eq!(super::apply(id, &json!({}), &far, 100), far, "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The dash generator's limit holds: a pattern too fine for the path leaves it solid.
|
||||
#[test]
|
||||
fn too_many_dashes_leave_the_path() {
|
||||
let long = line(&[(0.0, 0.0), (1e9, 0.0)], false);
|
||||
assert_eq!(run(DASHES, json!({"dash1": 1, "gap1": 1}), &long), long);
|
||||
}
|
||||
@@ -14,9 +14,10 @@ use std::sync::Arc;
|
||||
|
||||
use serde_json::Value;
|
||||
use vectorcraft_doc::color::{Color, Paint, keep_model};
|
||||
use vectorcraft_doc::recolor::{ColorVisitor, Reach, map_paint, recolor_node};
|
||||
use vectorcraft_doc::recolor::{ColorVisitor, Reach, map_links, recolor_node};
|
||||
use vectorcraft_doc::{Document, Effect, Node, NodeKind};
|
||||
|
||||
use crate::addons::duotone::{Duotone, SwatchKinds};
|
||||
use crate::merged_params;
|
||||
use crate::raster::color_param;
|
||||
use crate::util::{flag, num, text};
|
||||
@@ -27,7 +28,7 @@ pub const ADJUSTMENTS: [&str; 6] =
|
||||
|
||||
/// Is `id` a colour adjustment effect?
|
||||
pub fn is_adjustment(id: &str) -> bool {
|
||||
ADJUSTMENTS.contains(&id)
|
||||
ADJUSTMENTS.contains(&id) || crate::addons::color::is_color_effect(id)
|
||||
}
|
||||
|
||||
fn visible_adjustment(e: &Effect) -> bool {
|
||||
@@ -72,7 +73,7 @@ pub fn is_channel(s: &str) -> bool {
|
||||
}
|
||||
|
||||
/// Entries of a sampled tone curve.
|
||||
const LUT: usize = 256;
|
||||
pub(crate) const LUT: usize = 256;
|
||||
|
||||
/// One adjustment, its parameters normalised.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -88,6 +89,18 @@ enum Adjust {
|
||||
ShiftToColor { target: [f32; 3], amount: f32, preserve: bool },
|
||||
/// Warm/cool and magenta/green shifts, -1..1.
|
||||
TemperatureTint { temperature: f32, tint: f32 },
|
||||
/// One of the Color Effects add-on's.
|
||||
Addon(crate::addons::color::Adjust),
|
||||
}
|
||||
|
||||
impl Adjust {
|
||||
/// Duotone, which maps a colour with its swatch link ([`ColorMap::apply_link`]).
|
||||
fn duotone(&self) -> Option<&Duotone> {
|
||||
match self {
|
||||
Adjust::Addon(crate::addons::color::Adjust::Duotone(d)) => Some(d),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The colour adjustments of an effect list, composed in order.
|
||||
@@ -102,17 +115,44 @@ impl ColorMap {
|
||||
|
||||
/// Adjust display (sRGB) components 0..1.
|
||||
pub fn apply_rgb(&self, rgb: [f32; 3]) -> [f32; 3] {
|
||||
self.0.iter().fold(rgb, |c, a| a.apply(c).map(|v| if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 }))
|
||||
apply_rgb(&self.0, rgb)
|
||||
}
|
||||
|
||||
/// Adjust a colour, keeping its colour model (unchanged when the adjustments leave it as is).
|
||||
/// Duotone gives its inks' colour instead ([`Duotone`]).
|
||||
pub fn apply(&self, c: Color) -> Color {
|
||||
let rgb = c.to_rgb();
|
||||
let out = self.apply_rgb(rgb);
|
||||
if out.iter().zip(rgb).all(|(a, b)| (a - b).abs() < 1e-6) {
|
||||
return c;
|
||||
let (mut c, mut link, mut tint) = (c, None, 1.0);
|
||||
self.apply_link(&mut c, &mut link, &mut tint, &|_| None);
|
||||
c
|
||||
}
|
||||
|
||||
/// Adjust a colour with its swatch link and tint (`link`, `tint`), as a paint holds them;
|
||||
/// true when it changed. A colour that changes loses its link, except that Duotone with one
|
||||
/// tone of a swatch the document has (`swatches`, [`SwatchKinds`]) links it to that swatch at
|
||||
/// the tone's tint. The adjustments between Duotones work in display RGB, each run keeping
|
||||
/// the colour's model.
|
||||
pub fn apply_link(&self, c: &mut Color, link: &mut Option<String>, tint: &mut f32, swatches: &SwatchKinds) -> bool {
|
||||
let mut changed = false;
|
||||
let mut rest: &[Adjust] = &self.0;
|
||||
while !rest.is_empty() {
|
||||
let (run, after) = rest.split_at(rest.iter().position(|a| a.duotone().is_some()).unwrap_or(rest.len()));
|
||||
if !run.is_empty() {
|
||||
let rgb = c.to_rgb();
|
||||
let out = apply_rgb(run, rgb);
|
||||
if !out.iter().zip(rgb).all(|(a, b)| (a - b).abs() < 1e-6) {
|
||||
*c = keep_model(*c, Color::rgb(out[0], out[1], out[2]));
|
||||
*link = None;
|
||||
*tint = 1.0;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
let Some((duotone, tail)) = after.split_first() else { break };
|
||||
if let Some(d) = duotone.duotone() {
|
||||
changed |= d.map_link(c, link, tint, swatches);
|
||||
}
|
||||
rest = tail;
|
||||
}
|
||||
keep_model(c, Color::rgb(out[0], out[1], out[2]))
|
||||
changed
|
||||
}
|
||||
|
||||
/// Adjust an 8-bit sRGB pixel.
|
||||
@@ -122,6 +162,11 @@ impl ColorMap {
|
||||
}
|
||||
}
|
||||
|
||||
/// Display (sRGB) components 0..1 through `adjustments`, in order.
|
||||
fn apply_rgb(adjustments: &[Adjust], rgb: [f32; 3]) -> [f32; 3] {
|
||||
adjustments.iter().fold(rgb, |c, a| a.apply(c).map(|v| if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 }))
|
||||
}
|
||||
|
||||
/// The visible colour adjustments of `effects` as one map, `None` when there are none.
|
||||
pub fn color_map(effects: &[Effect]) -> Option<ColorMap> {
|
||||
let v: Vec<Adjust> =
|
||||
@@ -175,7 +220,7 @@ impl Adjust {
|
||||
preserve: flag(p, "preserveLightness", true),
|
||||
},
|
||||
"adjust.temperatureTint" => Adjust::TemperatureTint { temperature: amount(p, "temperature"), tint: amount(p, "tint") },
|
||||
_ => return None,
|
||||
_ => Adjust::Addon(crate::addons::color::Adjust::parse(id, p)?),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -225,17 +270,18 @@ impl Adjust {
|
||||
let (t, m) = (temperature * 0.3, tint * 0.3);
|
||||
[r * (1.0 + t), g * (1.0 - m), b * (1.0 - t)]
|
||||
}
|
||||
Adjust::Addon(a) => a.apply([r, g, b]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `f` sampled at [`LUT`] inputs 0..1.
|
||||
fn sample(f: impl Fn(f32) -> f32) -> Arc<[f32]> {
|
||||
pub(crate) fn sample(f: impl Fn(f32) -> f32) -> Arc<[f32]> {
|
||||
(0..LUT).map(|i| f(i as f32 / (LUT - 1) as f32).clamp(0.0, 1.0)).collect()
|
||||
}
|
||||
|
||||
/// The tone curve `lut` at `v` (linear between samples).
|
||||
fn lookup(lut: &[f32], v: f32) -> f32 {
|
||||
pub(crate) fn lookup(lut: &[f32], v: f32) -> f32 {
|
||||
let x = v.clamp(0.0, 1.0) * (LUT - 1) as f32;
|
||||
let i = (x.floor() as usize).min(LUT - 2);
|
||||
let (a, b) = (lut.get(i).copied().unwrap_or(v), lut.get(i + 1).copied().unwrap_or(v));
|
||||
@@ -245,6 +291,12 @@ fn lookup(lut: &[f32], v: f32) -> f32 {
|
||||
/// Curves' points (0..1 each, sorted by input, one per input) from its `points` parameter:
|
||||
/// `"x,y x,y …"` or `[[x, y], …]` in 0..255. Missing or unreadable points give the identity line.
|
||||
pub fn curve_points(v: Option<&Value>) -> Vec<(f32, f32)> {
|
||||
// At most a point per level is meaningful.
|
||||
curve_points_on(v, 255.0, 256)
|
||||
}
|
||||
|
||||
/// [`curve_points`] on a scale of 0..`max`, reading at most `cap` points.
|
||||
pub fn curve_points_on(v: Option<&Value>, max: f64, cap: usize) -> Vec<(f32, f32)> {
|
||||
let mut pts: Vec<(f64, f64)> = match v {
|
||||
Some(Value::String(s)) => s
|
||||
.split(|c: char| c.is_whitespace() || c == ';')
|
||||
@@ -257,10 +309,9 @@ pub fn curve_points(v: Option<&Value>) -> Vec<(f32, f32)> {
|
||||
_ => vec![],
|
||||
};
|
||||
pts.retain(|(x, y)| x.is_finite() && y.is_finite());
|
||||
// At most a point per level is meaningful.
|
||||
pts.truncate(256);
|
||||
let mut pts: Vec<(f32, f32)> =
|
||||
pts.into_iter().map(|(x, y)| ((x.clamp(0.0, 255.0) / 255.0) as f32, (y.clamp(0.0, 255.0) / 255.0) as f32)).collect();
|
||||
pts.truncate(cap);
|
||||
let unit = |v: f64| (v.clamp(0.0, max) / max) as f32;
|
||||
let mut pts: Vec<(f32, f32)> = pts.into_iter().map(|(x, y)| (unit(x), unit(y))).collect();
|
||||
pts.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
pts.dedup_by(|a, b| (a.0 - b.0).abs() < 1e-6);
|
||||
if pts.len() < 2 {
|
||||
@@ -354,15 +405,22 @@ fn from_hsl([h, s, l]: [f32; 3]) -> [f32; 3] {
|
||||
/// Gives the key of an embedded image recoloured by a map (`None`: leave it as it is).
|
||||
pub type ImageHook<'a> = dyn FnMut(&str, &ColorMap) -> Option<String> + 'a;
|
||||
|
||||
/// The live visitor: every colour through `map`, embedded images through the hook.
|
||||
/// The live visitor: every colour through `map` (paints with their swatch links, the document's
|
||||
/// swatches being `swatches`), embedded images through the hook.
|
||||
struct Live<'a, 'b> {
|
||||
map: &'a ColorMap,
|
||||
swatches: &'a SwatchKinds<'b>,
|
||||
images: &'a mut ImageHook<'b>,
|
||||
}
|
||||
|
||||
/// `p`'s colours through `map` with their swatch links ([`ColorMap::apply_link`]).
|
||||
fn map_paint(p: &mut Paint, map: &ColorMap, swatches: &SwatchKinds) -> bool {
|
||||
map_links(p, &|c, link, tint| map.apply_link(c, link, tint, swatches))
|
||||
}
|
||||
|
||||
impl ColorVisitor for Live<'_, '_> {
|
||||
fn paint(&mut self, p: &mut Paint) -> bool {
|
||||
map_paint(p, &|c| self.map.apply(c))
|
||||
map_paint(p, self.map, self.swatches)
|
||||
}
|
||||
fn color(&mut self, c: Color) -> Color {
|
||||
self.map.apply(c)
|
||||
@@ -375,12 +433,13 @@ impl ColorVisitor for Live<'_, '_> {
|
||||
/// `n` with the colour adjustments in it applied, `None` when it has none: its members' first,
|
||||
/// then those of its fills and strokes on their paints, then its own followed by `outer` (the
|
||||
/// adjustments of an object `n` is the art of) on everything it paints. The adjustment effects
|
||||
/// are gone from the result; embedded images take the keys `images` gives.
|
||||
pub fn adjust(n: &Node, outer: Option<&ColorMap>, images: &mut ImageHook<'_>) -> Option<Node> {
|
||||
/// are gone from the result; embedded images take the keys `images` gives. `swatches` tells the
|
||||
/// document's global and spot swatches apart ([`crate::addons::duotone::swatch_kind`]).
|
||||
pub fn adjust(n: &Node, outer: Option<&ColorMap>, swatches: &SwatchKinds, images: &mut ImageHook<'_>) -> Option<Node> {
|
||||
let mut m: Option<Node> = None;
|
||||
if let Some(ch) = n.children() {
|
||||
for (i, c) in ch.iter().enumerate() {
|
||||
if let Some(new) = adjust(c, None, images)
|
||||
if let Some(new) = adjust(c, None, swatches, images)
|
||||
&& let Some(slot) = m.get_or_insert_with(|| n.clone()).children_mut().and_then(|ch| ch.get_mut(i))
|
||||
{
|
||||
*slot = Arc::new(new);
|
||||
@@ -391,7 +450,7 @@ pub fn adjust(n: &Node, outer: Option<&ColorMap>, images: &mut ImageHook<'_>) ->
|
||||
if let Some(map) = color_map(item.effects())
|
||||
&& let Some(it) = m.get_or_insert_with(|| n.clone()).appearance.items.get_mut(i)
|
||||
{
|
||||
map_paint(it.paint_mut(), &|c| map.apply(c));
|
||||
map_paint(it.paint_mut(), &map, swatches);
|
||||
it.effects_mut().retain(|e| !is_adjustment(&e.id));
|
||||
}
|
||||
}
|
||||
@@ -403,7 +462,7 @@ pub fn adjust(n: &Node, outer: Option<&ColorMap>, images: &mut ImageHook<'_>) ->
|
||||
if let Some(map) = map {
|
||||
let mm = m.get_or_insert_with(|| n.clone());
|
||||
mm.appearance.effects.retain(|e| !is_adjustment(&e.id));
|
||||
recolor_node(mm, Reach::ALL, &mut Live { map: &map, images });
|
||||
recolor_node(mm, Reach::ALL, &mut Live { map: &map, swatches, images });
|
||||
}
|
||||
m
|
||||
}
|
||||
@@ -414,19 +473,20 @@ pub fn adjust_in_document(d: &mut Document, n: &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| s.art.clone());
|
||||
art = crate::reshape::as_art(Some(d), n, symbol.as_deref())?;
|
||||
let symbol = 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
|
||||
}
|
||||
_ => n,
|
||||
};
|
||||
let kinds = crate::addons::duotone::swatch_kinds(d);
|
||||
let mut hook = |key: &str, map: &ColorMap| -> Option<String> {
|
||||
let blob = d.images.get(key)?.map_rgb(|rgb| map.apply_rgb8(rgb))?;
|
||||
let k = blob.content_key();
|
||||
d.images.entry(k.clone()).or_insert(blob);
|
||||
Some(k)
|
||||
};
|
||||
adjust(src, None, &mut hook)
|
||||
adjust(src, None, &|name| kinds.get(name).copied(), &mut hook)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -489,17 +549,17 @@ mod tests {
|
||||
let mut g = Node::group(NodeId(1), vec![leaf(2), leaf(3)]);
|
||||
g.appearance.effects.push(Effect { id: "adjust.hueSaturation".into(), params: json!({"hue": 120}), visible: true });
|
||||
assert!(has_adjustment(&g));
|
||||
let out = adjust(&g, None, &mut |_, _| None).unwrap();
|
||||
let out = adjust(&g, None, &|_| None, &mut |_, _| None).unwrap();
|
||||
assert!(out.appearance.effects.is_empty());
|
||||
for c in out.children().unwrap() {
|
||||
assert_eq!(c.appearance.fill_paint().color().unwrap().to_hex(), "#00ff00");
|
||||
}
|
||||
// A hidden effect does nothing; a stroke's own effect recolours just that stroke.
|
||||
g.appearance.effects[0].visible = false;
|
||||
assert!(!has_adjustment(&g) && adjust(&g, None, &mut |_, _| None).is_none());
|
||||
assert!(!has_adjustment(&g) && adjust(&g, None, &|_| None, &mut |_, _| None).is_none());
|
||||
let mut p = Node::path(NodeId(5), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default_art());
|
||||
p.appearance.items[1].effects_mut().push(Effect { id: "adjust.levels".into(), params: json!({"outputBlack": 255}), visible: true });
|
||||
let out = adjust(&p, None, &mut |_, _| None).unwrap();
|
||||
let out = adjust(&p, None, &|_| None, &mut |_, _| None).unwrap();
|
||||
assert_eq!(out.appearance.items[1].paint().color().unwrap().to_hex(), "#ffffff");
|
||||
assert_eq!(out.appearance.items[0].paint(), p.appearance.items[0].paint());
|
||||
assert!(out.appearance.items[1].effects().is_empty());
|
||||
@@ -514,7 +574,7 @@ mod tests {
|
||||
let fill = Appearance::basic(Paint::Gradient(Box::new(gp)), Paint::None, 0.0);
|
||||
let mut p = Node::path(NodeId(1), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), fill);
|
||||
p.appearance.effects.push(Effect { id: "adjust.hueSaturation".into(), params: json!({"hue": 120}), visible: true });
|
||||
let out = adjust(&p, None, &mut |_, _| None).unwrap();
|
||||
let out = adjust(&p, None, &|_| None, &mut |_, _| None).unwrap();
|
||||
let Paint::Gradient(g) = out.appearance.fill_paint() else { panic!("a gradient fill") };
|
||||
// The points are what a freeform gradient draws from, not its stops.
|
||||
let hexes: Vec<String> = g.freeform.as_ref().unwrap().points.iter().map(|p| p.color.to_hex()).collect();
|
||||
|
||||
@@ -52,6 +52,7 @@ pub fn needs_bake(n: &Node) -> bool {
|
||||
|| crate::has_solid(n)
|
||||
|| has_adjustment(n)
|
||||
|| has_crop_marks(n)
|
||||
|| crate::addons::halftone::has_halftone(n)
|
||||
|| has_pathfinder(n)
|
||||
|| has_own_paint(n)
|
||||
|| has_geometry(&n.appearance.effects)
|
||||
@@ -104,6 +105,10 @@ fn bake_node(d: &mut Document, n: &Node) -> Option<Node> {
|
||||
if let Some(m) = crop_marks_art(n) {
|
||||
return Some(bake_pieces(d, m));
|
||||
}
|
||||
// Vector Halftone: the object as its dots.
|
||||
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)) {
|
||||
let m = evaluate_container(Some(d), &m).unwrap_or(m);
|
||||
return Some(bake_pieces(d, m));
|
||||
@@ -117,10 +122,12 @@ fn bake_node(d: &mut Document, n: &Node) -> Option<Node> {
|
||||
return Some(bake_pieces(d, m));
|
||||
}
|
||||
let symbol = match &n.kind {
|
||||
NodeKind::SymbolInstance { symbol, .. } => d.symbols.iter().find(|s| s.name == *symbol).map(|s| s.art.clone()),
|
||||
NodeKind::SymbolInstance { symbol, .. } => {
|
||||
d.symbols.iter().find(|s| s.name == *symbol).map(|s| vectorcraft_doc::symbol_overrides::dynamic_art(&s.art, n))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let m = reshape(Some(d), n, symbol.as_deref())?;
|
||||
let m = reshape(Some(d), n, symbol.as_ref())?;
|
||||
return Some(bake_node(d, &m).unwrap_or(m));
|
||||
}
|
||||
// A path whose Transform effect copies it with a gradient, or scales its strokes or moves its
|
||||
|
||||
@@ -42,6 +42,20 @@ pub fn is_length(id: &str, key: &str, relative: bool) -> bool {
|
||||
catalog_index().get(id).is_some_and(|info| info.lengths.keys(relative).any(|k| k == key))
|
||||
}
|
||||
|
||||
/// The range in points that distance parameter `key` of effect `id` is kept within, as its
|
||||
/// parameter documentation gives it (`{maxRadius: pt 4..127 (8), …}`); `None` when it gives none.
|
||||
pub fn length_range(id: &str, key: &str) -> Option<std::ops::RangeInclusive<f64>> {
|
||||
let doc = catalog_index().get(id)?.params;
|
||||
let starts = |at: usize| doc.get(..at).and_then(|s| s.chars().last()).is_some_and(|c| c == '{' || c == ' ');
|
||||
let rest = doc.match_indices(key).map(|(at, _)| at).filter(|at| starts(*at)).find_map(|at| {
|
||||
let rest = doc.get(at + key.len()..)?;
|
||||
rest.strip_prefix(": pt ").or_else(|| rest.strip_prefix("?: pt "))
|
||||
})?;
|
||||
let (lo, hi) = rest.split_whitespace().next()?.split_once("..")?;
|
||||
let (lo, hi) = (lo.parse::<f64>().ok()?, hi.trim_end_matches(',').parse::<f64>().ok()?);
|
||||
(lo.is_finite() && hi.is_finite() && lo <= hi).then_some(lo..=hi)
|
||||
}
|
||||
|
||||
/// The distance parameters of effect `id` (none for unknown effects).
|
||||
fn lengths_of(id: &str) -> Lengths {
|
||||
let always = |always| Lengths { always, absolute: &[] };
|
||||
@@ -777,6 +791,9 @@ pub fn effect_catalog() -> Vec<EffectInfo> {
|
||||
v.push(g(id, label, PATHFINDER, "{} (groups and layers: live Pathfinder over the members)", json!({})));
|
||||
}
|
||||
}
|
||||
v.extend(crate::addons::color::catalog());
|
||||
v.extend(crate::addons::halftone::catalog());
|
||||
v.extend(crate::addons::path::catalog());
|
||||
v.push(g(TRAP, "Trap…", PATHFINDER, trap::EFFECT_PARAMS_DOC, trap::effect_defaults()));
|
||||
v.push(g(
|
||||
CROP_MARKS,
|
||||
|
||||
@@ -114,16 +114,67 @@ pub fn apply_geometry_with(effects: &[Effect], path: &PathData, bounds: Rect, ct
|
||||
apply_geometry_within(effects, path, bounds, ctx, Budget::EFFECTS)
|
||||
}
|
||||
|
||||
/// Does effect `e` safely commute with pure translation when it is terminal?
|
||||
/// Only positive offsets with round joins commute exactly with pure translations for non-zero fills (#4145).
|
||||
fn is_terminal_offset_eligible(e: &Effect) -> bool {
|
||||
if e.id != "path.offsetPath" {
|
||||
return false;
|
||||
}
|
||||
let p = merged_params(&e.id, &e.params);
|
||||
util::join(&p, "joins") == vectorcraft_pathops::Join::Round && util::num(&p, "offset", 10.0) >= 0.0
|
||||
}
|
||||
|
||||
/// Optimize a list of visible geometry effects: move pure-translation Transform effects with
|
||||
/// copies after a terminal Offset Path effect when the effective style is safe (NonZero fill and
|
||||
/// no stroke), so that the offset path is evaluated once on the base path rather than on dozens
|
||||
/// of overlapping copies, avoiding combinatorial explosions in the planar sweep (#4145).
|
||||
pub(crate) fn optimize_geometry_effects<'a>(effects: &[&'a Effect], style_safe: bool) -> Vec<&'a Effect> {
|
||||
let mut out: Vec<&'a Effect> = effects.to_vec();
|
||||
if !style_safe || out.len() < 2 {
|
||||
return out;
|
||||
}
|
||||
let mut changed = true;
|
||||
while changed {
|
||||
changed = false;
|
||||
for i in 0..out.len().saturating_sub(1) {
|
||||
let e1 = out[i];
|
||||
if e1.id == "distort.transform" {
|
||||
let p1 = merged_params(&e1.id, &e1.params);
|
||||
if let Some((_, copies)) = crate::distort::transform_move(&p1)
|
||||
&& copies > 0
|
||||
&& is_terminal_offset_eligible(out[i + 1])
|
||||
&& out.get(i + 2..).is_some_and(|tail| tail.iter().all(|e| e.id == "distort.transform"))
|
||||
{
|
||||
out.swap(i, i + 1);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// [`apply_geometry_with`] whose effects keep to `budget`, raised to what `path` costs where that
|
||||
/// is more.
|
||||
pub(crate) fn apply_geometry_within(effects: &[Effect], path: &PathData, bounds: Rect, ctx: &GeomContext, budget: Budget) -> PathData {
|
||||
// `budget` raised to what `path` costs, measured once, and only for a path that has geometry
|
||||
// effects.
|
||||
let has_stroke = match (ctx.item, ctx.appearance) {
|
||||
(Some(idx), Some(app)) => match app.items.get(idx) {
|
||||
Some(AppearanceItem::Stroke(s)) => !s.paint.is_none() && s.width > 0.0,
|
||||
Some(AppearanceItem::Fill(_)) => false,
|
||||
None => ctx.stroke.is_some_and(|s| !s.paint.is_none() && s.width > 0.0),
|
||||
},
|
||||
_ => ctx.stroke.is_some_and(|s| !s.paint.is_none() && s.width > 0.0),
|
||||
};
|
||||
let style_safe = ctx.rule == FillRule::NonZero && !has_stroke;
|
||||
let visible_fx: Vec<&Effect> = effects.iter().filter(|e| e.visible && is_geometry(&e.id)).collect();
|
||||
let fx = optimize_geometry_effects(&visible_fx, style_safe);
|
||||
let mut limit = None;
|
||||
let mut cur = path.clone();
|
||||
let mut b = bounds;
|
||||
let mut first = true;
|
||||
for e in effects.iter().filter(|e| e.visible && is_geometry(&e.id)) {
|
||||
for e in fx {
|
||||
if !first {
|
||||
b = cur.bounds().unwrap_or(b);
|
||||
}
|
||||
@@ -214,6 +265,7 @@ fn apply_unchecked(id: &str, p: &Value, path: &PathData, b: Rect, ctx: &GeomCont
|
||||
"convertToShape.rectangle" | "convertToShape.roundedRectangle" | "convertToShape.ellipse" => stylize::convert_to_shape(id, b, p),
|
||||
"stylize.roundCorners" => stylize::round_corners(path, num(p, "radius", 10.0)),
|
||||
"stylize.scribble" => stylize::scribble(path, b, p),
|
||||
_ if id.starts_with(crate::addons::path::PREFIX) => crate::addons::path::apply(id, p, path, max),
|
||||
_ => match id.strip_prefix("warp.").and_then(WarpStyle::from_id) {
|
||||
Some(style) => warp::warp(path, b, style, p, max),
|
||||
None => path.clone(),
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
//! result keeps the appearance of the object whose paint it takes. Trap ([`crate::trap`]) keeps the
|
||||
//! children and adds its traps above them.
|
||||
//!
|
||||
//! **Compound shapes** ([`compound_shape_area`]): a compound shape draws as the one area its
|
||||
//! members' shape modes make, painted by its own appearance as a group's own fills and strokes
|
||||
//! paint its members.
|
||||
//!
|
||||
//! **Container appearance** ([`evaluate_container`]): a group's or layer's own fills and strokes
|
||||
//! paint every member's geometry, below or above the members as its contents slot says
|
||||
//! ([`Appearance::contents_at`]); its geometry effects reshape the members as one piece; its raster
|
||||
@@ -16,7 +20,7 @@ use std::sync::Arc;
|
||||
|
||||
use serde_json::Value;
|
||||
use vectorcraft_doc::color::{Color, Paint};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Document, Node, NodeId, NodeKind, StrokeLayer};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Document, Node, NodeId, NodeKind, ShapeMode, StrokeLayer};
|
||||
use vectorcraft_geom::{FillRule, PathData, Rect};
|
||||
use vectorcraft_pathops::{self as po, PathfinderOp};
|
||||
|
||||
@@ -45,10 +49,11 @@ fn op_of(id: &str) -> Option<PathfinderOp> {
|
||||
PATHFINDER_EFFECTS.iter().find(|e| e.0 == id).map(|e| e.2)
|
||||
}
|
||||
|
||||
/// Does `n` (a group or layer) carry a visible Pathfinder effect?
|
||||
/// Does `n` (a group or layer) carry a visible Pathfinder effect, or is it a compound shape?
|
||||
pub fn has_pathfinder(n: &Node) -> bool {
|
||||
matches!(n.kind, NodeKind::Group { clip: false, .. } | NodeKind::Layer { .. })
|
||||
&& n.appearance.effects.iter().any(|e| e.visible && is_pathfinder(&e.id))
|
||||
n.is_compound_shape()
|
||||
|| (matches!(n.kind, NodeKind::Group { clip: false, .. } | NodeKind::Layer { .. })
|
||||
&& n.appearance.effects.iter().any(|e| e.visible && is_pathfinder(&e.id)))
|
||||
}
|
||||
|
||||
/// Converts objects that aren't paths (e.g. text) to outline art, if possible. The paths and
|
||||
@@ -62,11 +67,15 @@ fn leaves(n: &Node, hook: Option<OutlineHook>, out: &mut Vec<Node>) {
|
||||
}
|
||||
match &n.kind {
|
||||
NodeKind::Path { guide: false, .. } | NodeKind::Compound { .. } => out.push(n.clone()),
|
||||
NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } => {
|
||||
for c in children {
|
||||
leaves(c, hook, out);
|
||||
// A compound shape takes part as its area, styled like it.
|
||||
NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } => match compound_shape_art(n, hook) {
|
||||
Some(art) => out.push(art),
|
||||
None => {
|
||||
for c in children {
|
||||
leaves(c, hook, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
// Every path of the outline art, at any depth, takes the object's opacity and blend
|
||||
// mode.
|
||||
@@ -91,6 +100,63 @@ fn geometry(n: &Node) -> Option<(PathData, FillRule)> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The operation that combines a compound shape's member in `mode` with the area below it.
|
||||
fn shape_op(mode: ShapeMode) -> po::BoolOp {
|
||||
match mode {
|
||||
ShapeMode::Add => po::BoolOp::Union,
|
||||
ShapeMode::Subtract => po::BoolOp::Difference,
|
||||
ShapeMode::Intersect => po::BoolOp::Intersect,
|
||||
ShapeMode::Exclude => po::BoolOp::Xor,
|
||||
}
|
||||
}
|
||||
|
||||
/// The area compound shape `n` draws: each visible member's shape (a group's leaves together, type
|
||||
/// as its outlines through `hook`, open paths closed) combined with the area of the members below
|
||||
/// 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`]).
|
||||
pub fn compound_shape_area(n: &Node, hook: Option<OutlineHook>) -> Option<PathData> {
|
||||
if !n.is_compound_shape() {
|
||||
return None;
|
||||
}
|
||||
let mut all: Vec<Node> = vec![];
|
||||
// Each member that has a shape: its operation and how many of the leaves are its.
|
||||
let mut members: Vec<(po::BoolOp, usize)> = vec![];
|
||||
for c in n.children()? {
|
||||
let before = all.len();
|
||||
leaves(c, hook, &mut all);
|
||||
if all.len() > before {
|
||||
members.push((shape_op(c.extras().shape_mode), all.len() - before));
|
||||
}
|
||||
}
|
||||
if !within_budget(&all) {
|
||||
return None;
|
||||
}
|
||||
if all.is_empty() {
|
||||
return Some(PathData::default());
|
||||
}
|
||||
let parts: Vec<(PathData, FillRule)> = all.iter().map(|l| geometry(l).unwrap_or_default()).collect();
|
||||
let refs: Vec<(&PathData, FillRule)> = parts.iter().map(|(p, r)| (p, *r)).collect();
|
||||
Some(po::boolean_n(&refs, |inside| {
|
||||
let (mut start, mut area) = (0, false);
|
||||
for (k, (op, count)) in members.iter().enumerate() {
|
||||
let here = inside.get(start..start + count).is_some_and(|flags| flags.contains(&true));
|
||||
start += count;
|
||||
area = if k == 0 { here } else { op.apply(area, here) };
|
||||
}
|
||||
area
|
||||
}))
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
let area = compound_shape_area(n, hook)?;
|
||||
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)
|
||||
}
|
||||
|
||||
/// A result path styled like `src` (its appearance, opacity and blend mode).
|
||||
fn styled(src: &Node, path: PathData, outline: bool) -> Node {
|
||||
let mut appearance = Appearance { items: src.appearance.items.clone(), ..Default::default() };
|
||||
@@ -196,7 +262,16 @@ pub fn pathfinder_children(group: &Node, hook: Option<OutlineHook>) -> Option<Ve
|
||||
return None;
|
||||
}
|
||||
let source: &[Arc<Node>] = group.children()?;
|
||||
let mut stack: Vec<Arc<Node>> = source.to_vec();
|
||||
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) {
|
||||
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()),
|
||||
}
|
||||
} else {
|
||||
source.to_vec()
|
||||
};
|
||||
for e in group.appearance.effects.iter().filter(|e| e.visible) {
|
||||
if e.id == crate::trap::TRAP {
|
||||
let mut lv = vec![];
|
||||
@@ -375,6 +450,13 @@ pub fn member_shapes(doc: Option<&Document>, n: &Node) -> Vec<(PathData, FillRul
|
||||
}
|
||||
match &n.kind {
|
||||
NodeKind::Path { guide: false, .. } | NodeKind::Compound { .. } => out.extend(crate::reshape::leaf_geometry(n)),
|
||||
NodeKind::Group { .. } if n.is_compound_shape() => {
|
||||
let hook: &dyn Fn(&Node) -> Option<Node> = &crate::outline_text;
|
||||
match compound_shape_area(n, Some(hook)) {
|
||||
Some(area) => out.push((area, FillRule::NonZero)),
|
||||
None => members(doc, n, out),
|
||||
}
|
||||
}
|
||||
NodeKind::Text(t) => {
|
||||
let (_, all) = crate::reshape::glyph_outlines(t);
|
||||
if !all.elements().is_empty() {
|
||||
@@ -454,6 +536,8 @@ pub fn evaluate_container(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
if let Some(ch) = m.children_mut() {
|
||||
*ch = result;
|
||||
}
|
||||
// The area is art now: drawn again, it is no compound shape of its own.
|
||||
m.extras_mut().compound_shape = false;
|
||||
}
|
||||
if !fx.is_empty() {
|
||||
m = crate::reshape::outlined_members(doc, &m);
|
||||
|
||||
Loaded 100 of 795 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user