mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-22 02:15:24 +01:00
Merge main into feat/kinsoku-sets
This commit is contained in:
commit
f8b85383b4
181 files changed
+17981
-799
No files matched your search
Generated
+1
@@ -6386,6 +6386,7 @@ dependencies = [
|
||||
"pollster 1.0.1",
|
||||
"raw-window-handle",
|
||||
"rfd",
|
||||
"rustix 1.1.6",
|
||||
"serde_json",
|
||||
"socket2",
|
||||
"tempfile",
|
||||
|
||||
@@ -18,6 +18,8 @@ gate: all six core workflows (path drawing and editing, paint and style, type, l
|
||||
production, file exchange) work end to end on macOS with save and reopen, and only the last is partial, without
|
||||
blocking it ([Alpha gate](docs/roadmap.md#alpha-gate)).
|
||||
|
||||
> **Gujarati status (2026-11-14, #6613):** 4,673 catalog rows: the original 4,286 remain unchanged; all 387 additions are contributor-reviewed and approved, with 11 wording corrections. Status/error messages remain partial, and the pinned CI/release fonts and web startup list contain no Gujarati face; web selection stays unavailable until a Gujarati interface face is bundled. Scores and estimates unchanged.
|
||||
|
||||
## Headline numbers
|
||||
|
||||
| | Value | Kind |
|
||||
@@ -96,6 +98,7 @@ Weights, evidence and the missing items per area: [target-app-parity.md](docs/ta
|
||||
| Simplified Chinese | `zh-hans` | partial (messages in English) | 85% |
|
||||
| Spanish | `es` | full | 100% |
|
||||
| Hindi | `hi` | catalog complete; machine-assisted draft, native review pending | complete under the scanner |
|
||||
| Gujarati | `gu` | 4,673 contributor-reviewed catalog rows; 11 corrections applied to the 387 additions | menus and dialogs; status/error messages partial |
|
||||
| Arabic | `ar` | none (no right-to-left interface) | 0% |
|
||||
| French | `fr` | full | 100% |
|
||||
| Portuguese (Brazil) | `pt-br` | full | 100% |
|
||||
@@ -218,6 +221,7 @@ The full earlier record of what landed is kept in [docs/roadmap.md](docs/roadmap
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-11-14 | minor | Gujarati (#6613): contributor proofreading completed for all 387 additions, with 11 wording corrections; 4,673 reviewed catalog rows. Status/error messages remain partial; web selection requires a bundled Gujarati face. Scores and estimates unchanged. |
|
||||
| 2026-11-10 | minor | Tidy-up, no content change: the unheaded change bullets at the top of the file and a revision row inside the headline-numbers table move to this table under their commit dates. Scores and estimates unchanged. |
|
||||
| 2026-11-10 | minor | Raster effects: Effect Gallery thumbnails and an in-dialog preview (an original drawing through each filter). Features-table Raster effects 85% → 88%, hours 1–2 → none. Headline numbers unchanged, so the progress log is unchanged. |
|
||||
| 2026-11-05 | minor | Compound Shapes add-on (beyond Illustrator): a menu for Make, Release and Expand Compound Shape and a setting, off by default, that makes the Pathfinder panel's Shape Mode buttons create compound shapes on a click. Scores and estimates unchanged. |
|
||||
@@ -226,6 +230,8 @@ The full earlier record of what landed is kept in [docs/roadmap.md](docs/roadmap
|
||||
| 2026-11-03 | minor | UI: Window → Help Bar toggles the tool-help strip separately from the status bar and saves visibility with workspaces. Scores and estimates unchanged. |
|
||||
| 2026-11-03 | minor | Docked Tools stays between left-side panels and the canvas; redocking accepts canvas and window edges. Requested layout choice; scores and estimates unchanged. |
|
||||
| 2026-11-02 | minor | Selection QoL: saved marquee modes, screen drag direction with a 3 px dead zone, and translated mode/E feedback (#5313); VectorCraft addition, scores and estimates unchanged. |
|
||||
| 2026-11-12 | minor | Gujarati (`gu`): reviewed by a native speaker; zero/one singular plurals (CLDR); Gujarati craft-fonts faces join the UI fonts; strings added on main since branching translated (4,286 rows) |
|
||||
| 2026-11-11 | minor | Gujarati (`gu`): 4,235-row machine-assisted interface catalog, one/other plurals, desktop Gujarati font fallback; messages not yet translated; native review pending |
|
||||
| 2026-11-06 | minor | Hindi (`hi`): 7,877-row machine-assisted catalog, zero/one singular plurals, desktop font fallback and optional Devanagari craft-fonts fallback; all 55 translation tests pass; native review and a web font remain; scores and estimates unchanged |
|
||||
| 2026-11-05 | minor | Progress log row for the Type Tools add-on's Release Type on a Path (#5532); headline numbers unchanged. |
|
||||
| 2026-11-03 | minor | Progress log row for the Type Tools add-on's Text Tweaker (#4924); headline numbers unchanged. |
|
||||
|
||||
@@ -110,6 +110,10 @@ wayland-protocols = { version = "0.32", features = ["client", "unstable"] }
|
||||
# wayland-protocols' `unstable` set) made on the window's own connection. MIT, already in the build
|
||||
# through winit and rfd.
|
||||
wayland-backend = { version = "0.3", features = ["client_system"] }
|
||||
# The Wayland clipboard's pipes (src/clipboard.rs): `poll` and the nonblocking flag give up a read
|
||||
# or send when the other app moves no data for a while. Apache-2.0 WITH LLVM-exception OR
|
||||
# Apache-2.0 OR MIT, already in the build through the workspace.
|
||||
rustix = { workspace = true, features = ["std", "event", "fs"] }
|
||||
|
||||
# The save dialog's file type (src/portal_save.rs, #2679): the desktop portal over D-Bus, on the
|
||||
# systems where rfd shows the portal's dialog. MIT, already in the build through accesskit. Pinned
|
||||
|
||||
@@ -1790,15 +1790,17 @@ mod portable {
|
||||
/// starts a thread for each paste of a Copy's data by another app ([`MAX_SENDS`] at once). Reads
|
||||
/// run on another worker and take [`MAX_BYTES`] at most; text waits [`LOAD_WAIT`] at most, SVG and
|
||||
/// PNG as long as a paste's bitmap from arboard. A read that gets no answer in its wait is kept for
|
||||
/// a paste to wait for off the UI thread, as arboard's are.
|
||||
/// a paste to wait for off the UI thread, as arboard's are. A read or send whose pipe moves no data
|
||||
/// for [`IDLE_WAIT`] fails and closes the pipe, which frees its worker or thread.
|
||||
#[cfg(target_os = "linux")]
|
||||
mod wayland {
|
||||
use std::io::{Read, Write};
|
||||
use std::os::fd::AsFd as _;
|
||||
use std::io::{ErrorKind, Read, Write};
|
||||
use std::os::fd::{AsFd, BorrowedFd};
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex, MutexGuard, PoisonError, mpsc};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use rustix::event::{PollFd, PollFlags, Timespec};
|
||||
use vectorcraft_engine::cmd::clipboard::names_a_picture;
|
||||
use vectorcraft_ui_egui::ClipboardRead;
|
||||
use wayland_client::backend::ObjectData;
|
||||
@@ -1827,6 +1829,11 @@ mod wayland {
|
||||
const MAX_SENDS: usize = 8;
|
||||
/// The most types of an offer that are kept (untrusted data).
|
||||
const MAX_TYPES: usize = 256;
|
||||
/// How long a read or send waits for the other app to move a byte through the pipe before it
|
||||
/// fails, VectorCraft's own limit; the wait starts again after each byte moved.
|
||||
const IDLE_WAIT: Duration = LOAD_WAIT;
|
||||
/// The most bytes a read takes from the pipe at a time.
|
||||
const CHUNK: usize = 64 << 10;
|
||||
|
||||
/// What a read gives: the bytes, or why there are none.
|
||||
type Got = Result<Vec<u8>, String>;
|
||||
@@ -1898,37 +1905,117 @@ mod wayland {
|
||||
Some(first)
|
||||
}
|
||||
|
||||
/// `from` read to its end, `cap` bytes at most: more is an error, as is a failed read.
|
||||
fn read_capped(from: impl Read, cap: usize) -> Got {
|
||||
let mut data = vec![];
|
||||
from.take(cap as u64 + 1).read_to_end(&mut data).map_err(|e| format!("couldn't read the clipboard's data: {e}"))?;
|
||||
if data.len() > cap {
|
||||
return Err(format!("the clipboard's data is larger than {} MiB", cap >> 20));
|
||||
/// The time `idle` from now; None when that is past the clock's range (no limit).
|
||||
fn idle_until(idle: Duration) -> Option<Instant> {
|
||||
Instant::now().checked_add(idle)
|
||||
}
|
||||
|
||||
/// Makes `fd` nonblocking, its other flags kept.
|
||||
fn nonblocking(fd: BorrowedFd<'_>) -> std::io::Result<()> {
|
||||
let flags = rustix::fs::fcntl_getfl(fd)?;
|
||||
rustix::fs::fcntl_setfl(fd, flags | rustix::fs::OFlags::NONBLOCK)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Waits until `fd` is ready for `events`, closed or failed (true), or `until` passes (false);
|
||||
/// None waits without a limit. A wait a signal cuts short goes on until `until`.
|
||||
fn ready(fd: BorrowedFd<'_>, events: PollFlags, until: Option<Instant>) -> std::io::Result<bool> {
|
||||
loop {
|
||||
let left = until.map(|t| t.saturating_duration_since(Instant::now()));
|
||||
let timeout = left.and_then(|d| Timespec::try_from(d).ok());
|
||||
let mut fds = [PollFd::new(&fd, events)];
|
||||
match rustix::event::poll(&mut fds, timeout.as_ref()) {
|
||||
Ok(n) => return Ok(n > 0),
|
||||
Err(rustix::io::Errno::INTR) => {}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
}
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
/// `from` read to its end, `cap` bytes at most: more is an error, as is a failed read and a
|
||||
/// pipe that is left open with no byte coming for `idle`. A read that fails gives none of the
|
||||
/// bytes it took, and closes `from`.
|
||||
fn read_capped(mut from: impl Read + AsFd, cap: usize, idle: Duration) -> Got {
|
||||
let failed = |e: std::io::Error| format!("couldn't read the clipboard's data: {e}");
|
||||
nonblocking(from.as_fd()).map_err(failed)?;
|
||||
let (mut data, mut chunk) = (vec![], vec![0u8; CHUNK]);
|
||||
let mut until = idle_until(idle);
|
||||
loop {
|
||||
match from.read(&mut chunk) {
|
||||
Ok(0) => return Ok(data),
|
||||
Ok(n) => {
|
||||
let got = chunk.get(..n).ok_or_else(|| failed(ErrorKind::InvalidData.into()))?;
|
||||
if data.len() + got.len() > cap {
|
||||
return Err(format!("the clipboard's data is larger than {} MiB", cap >> 20));
|
||||
}
|
||||
data.extend_from_slice(got);
|
||||
until = idle_until(idle);
|
||||
}
|
||||
Err(e) if e.kind() == ErrorKind::Interrupted => {}
|
||||
Err(e) if e.kind() == ErrorKind::WouldBlock => {
|
||||
if !ready(from.as_fd(), PollFlags::IN, until).map_err(failed)? {
|
||||
return Err("the app that copied stopped sending the clipboard's data".to_string());
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(failed(e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `data` written to `to`: an error when the app that pastes closes its end (the process
|
||||
/// ignores `SIGPIPE`) or takes no byte for `idle`.
|
||||
fn send(mut to: impl Write + AsFd, data: &[u8], idle: Duration) -> std::io::Result<()> {
|
||||
nonblocking(to.as_fd())?;
|
||||
let (mut sent, mut until) = (0, idle_until(idle));
|
||||
while let Some(rest) = data.get(sent..).filter(|rest| !rest.is_empty()) {
|
||||
match to.write(rest) {
|
||||
Ok(0) => return Err(ErrorKind::WriteZero.into()),
|
||||
Ok(n) => {
|
||||
sent += n;
|
||||
until = idle_until(idle);
|
||||
}
|
||||
Err(e) if e.kind() == ErrorKind::Interrupted => {}
|
||||
Err(e) if e.kind() == ErrorKind::WouldBlock => {
|
||||
if !ready(to.as_fd(), PollFlags::OUT, until)? {
|
||||
return Err(ErrorKind::TimedOut.into());
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The threads that send a Copy's data to the apps that paste it, counted.
|
||||
#[derive(Clone, Default)]
|
||||
struct Sends(Arc<AtomicUsize>);
|
||||
#[derive(Clone)]
|
||||
struct Sends {
|
||||
running: Arc<AtomicUsize>,
|
||||
/// How long a send waits for the app that pastes to take a byte.
|
||||
idle: Duration,
|
||||
}
|
||||
|
||||
impl Default for Sends {
|
||||
fn default() -> Self {
|
||||
Self { running: Arc::default(), idle: IDLE_WAIT }
|
||||
}
|
||||
}
|
||||
|
||||
impl Sends {
|
||||
/// `data` written to `to` on a thread of its own, which then closes `to`; an app that
|
||||
/// closes its end early ends the write (the process ignores `SIGPIPE`). While
|
||||
/// [`MAX_SENDS`] are being written `to` is closed at once with nothing written → false.
|
||||
fn start(&self, mut to: impl Write + Send + 'static, data: Arc<[u8]>) -> bool {
|
||||
if self.0.fetch_add(1, Ordering::SeqCst) >= MAX_SENDS {
|
||||
self.0.fetch_sub(1, Ordering::SeqCst);
|
||||
/// `data` written to `to` on a thread of its own ([`send`]), which then closes `to`; an app
|
||||
/// that closes its end early or takes no byte for the send's idle wait ends the write.
|
||||
/// While [`MAX_SENDS`] are being written `to` is closed at once with nothing written → false.
|
||||
fn start(&self, to: impl Write + AsFd + Send + 'static, data: Arc<[u8]>) -> bool {
|
||||
if self.running.fetch_add(1, Ordering::SeqCst) >= MAX_SENDS {
|
||||
self.running.fetch_sub(1, Ordering::SeqCst);
|
||||
return false;
|
||||
}
|
||||
let count = self.0.clone();
|
||||
let send = move || {
|
||||
let _ = to.write_all(&data);
|
||||
drop(to);
|
||||
let (count, idle) = (self.running.clone(), self.idle);
|
||||
let work = move || {
|
||||
let _ = send(to, &data, idle);
|
||||
count.fetch_sub(1, Ordering::SeqCst);
|
||||
};
|
||||
if std::thread::Builder::new().name("wayland-clipboard-send".into()).spawn(send).is_err() {
|
||||
self.0.fetch_sub(1, Ordering::SeqCst);
|
||||
if std::thread::Builder::new().name("wayland-clipboard-send".into()).spawn(work).is_err() {
|
||||
self.running.fetch_sub(1, Ordering::SeqCst);
|
||||
return false;
|
||||
}
|
||||
true
|
||||
@@ -1937,7 +2024,7 @@ mod wayland {
|
||||
/// How many are being written.
|
||||
#[cfg(test)]
|
||||
fn running(&self) -> usize {
|
||||
self.0.load(Ordering::SeqCst)
|
||||
self.running.load(Ordering::SeqCst)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2298,7 +2385,7 @@ mod wayland {
|
||||
|
||||
impl Receiver {
|
||||
/// The clipboard's data of type `wire`, read until the app that copied closes the pipe,
|
||||
/// [`MAX_BYTES`] at most.
|
||||
/// [`MAX_BYTES`] at most; an error when that app sends no byte for [`IDLE_WAIT`].
|
||||
fn receive(&self, wire: String) -> Got {
|
||||
let offer = lock(&self.objects).selection.clone().ok_or("the clipboard is empty")?;
|
||||
let (from, to) = std::io::pipe().map_err(|e| format!("couldn't open a pipe for the clipboard's data: {e}"))?;
|
||||
@@ -2307,7 +2394,7 @@ mod wayland {
|
||||
// holds the only one, and the read ends when that app closes it.
|
||||
drop(to);
|
||||
self.conn.flush().map_err(|e| format!("couldn't reach the compositor: {e}"))?;
|
||||
read_capped(from, MAX_BYTES)
|
||||
read_capped(from, MAX_BYTES, IDLE_WAIT)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2557,31 +2644,86 @@ mod wayland {
|
||||
assert_eq!(keyboard_serial(&modifiers(9), None), None);
|
||||
}
|
||||
|
||||
/// A wait no test reaches: the other end of each pipe below either moves data or closes.
|
||||
const LONG: Duration = Duration::from_secs(600);
|
||||
|
||||
/// A pipe holding `data`, its write end closed when `close`.
|
||||
fn pipe_with(data: &[u8], close: bool) -> (std::io::PipeReader, Option<std::io::PipeWriter>) {
|
||||
let (from, mut to) = std::io::pipe().unwrap();
|
||||
to.write_all(data).unwrap();
|
||||
(from, (!close).then_some(to))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_read_stops_at_its_limit() {
|
||||
assert_eq!(MAX_BYTES, 64 * 1024 * 1024);
|
||||
assert_eq!(read_capped(&b"12345678"[..], 8), Ok(b"12345678".to_vec()));
|
||||
assert_eq!(read_capped(&b""[..], 8), Ok(vec![]));
|
||||
assert_eq!(read_capped(&[0u8; 9][..], 8), Err("the clipboard's data is larger than 0 MiB".to_string()));
|
||||
assert_eq!(read_capped(&vec![0u8; (2 << 20) + 1][..], 2 << 20), Err("the clipboard's data is larger than 2 MiB".to_string()));
|
||||
/// A pipe that breaks.
|
||||
struct Broken;
|
||||
impl Read for Broken {
|
||||
fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> {
|
||||
Err(std::io::ErrorKind::BrokenPipe.into())
|
||||
}
|
||||
assert_eq!(IDLE_WAIT, LOAD_WAIT);
|
||||
assert_eq!(read_capped(pipe_with(b"12345678", true).0, 8, Duration::ZERO), Ok(b"12345678".to_vec()));
|
||||
assert_eq!(read_capped(pipe_with(b"", true).0, 8, Duration::ZERO), Ok(vec![]));
|
||||
assert_eq!(read_capped(pipe_with(&[0u8; 9], true).0, 8, Duration::ZERO), Err("the clipboard's data is larger than 0 MiB".to_string()));
|
||||
// More than a pipe holds, from an app that is still sending.
|
||||
let (from, to) = std::io::pipe().unwrap();
|
||||
assert!(Sends::default().start(to, vec![0u8; (2 << 20) + 1].into()));
|
||||
assert_eq!(read_capped(from, 2 << 20, LONG), Err("the clipboard's data is larger than 2 MiB".to_string()));
|
||||
// A pipe that can't be read: the write end.
|
||||
let (_, to) = std::io::pipe().unwrap();
|
||||
let unreadable = std::fs::File::from(std::os::fd::OwnedFd::from(to));
|
||||
assert!(read_capped(unreadable, 8, LONG).is_err_and(|e| e.starts_with("couldn't read the clipboard's data")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_read_from_an_app_that_stops_sending_fails_and_closes_the_pipe() {
|
||||
let stopped = Err("the app that copied stopped sending the clipboard's data".to_string());
|
||||
// Some bytes and then nothing, the pipe left open: none of them are pasted.
|
||||
let (from, to) = pipe_with(b"part", false);
|
||||
assert_eq!(read_capped(from, MAX_BYTES, Duration::ZERO), stopped);
|
||||
// The read end is closed, so that app's next write fails.
|
||||
let mut to = to.unwrap();
|
||||
assert_eq!(to.write(b"x").map_err(|e| e.kind()), Err(ErrorKind::BrokenPipe));
|
||||
// Nothing at all, and data that fills the limit exactly with more never coming.
|
||||
let (from, _held) = pipe_with(b"", false);
|
||||
assert_eq!(read_capped(from, MAX_BYTES, Duration::ZERO), stopped);
|
||||
let (from, _held) = pipe_with(b"12345678", false);
|
||||
assert_eq!(read_capped(from, 8, Duration::ZERO), stopped);
|
||||
// The next read gets its data.
|
||||
assert_eq!(read_capped(pipe_with(b"words", true).0, MAX_BYTES, Duration::ZERO), Ok(b"words".to_vec()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_send_to_an_app_that_stops_reading_fails_and_frees_its_thread() {
|
||||
let data: Arc<[u8]> = vec![3u8; 300_000].into();
|
||||
// More than a pipe holds, to an app that never reads.
|
||||
let (_held, to) = std::io::pipe().unwrap();
|
||||
assert_eq!(send(to, &data, Duration::ZERO).map_err(|e| e.kind()), Err(ErrorKind::TimedOut));
|
||||
// An app that closes its end before reading.
|
||||
let (from, to) = std::io::pipe().unwrap();
|
||||
drop(from);
|
||||
assert_eq!(send(to, &data, LONG).map_err(|e| e.kind()), Err(ErrorKind::BrokenPipe));
|
||||
// A send thread to an app that does not read until the send gives up, its end still open.
|
||||
let sends = Sends { idle: Duration::ZERO, ..Sends::default() };
|
||||
let (from, to) = std::io::pipe().unwrap();
|
||||
assert!(sends.start(to, data.clone()));
|
||||
while sends.running() > 0 {
|
||||
std::thread::yield_now();
|
||||
}
|
||||
assert!(read_capped(Broken, 8).is_err_and(|e| e.starts_with("couldn't read the clipboard's data")));
|
||||
// What the pipe held when the send gave up, then the end of the data: the send closed it.
|
||||
let got = read_capped(from, MAX_BYTES, Duration::ZERO).map(|d| d.len());
|
||||
assert!(got.as_ref().is_ok_and(|&n| n > 0 && n < data.len()), "{got:?}");
|
||||
// The thread is free for the next paste.
|
||||
let (from, to) = std::io::pipe().unwrap();
|
||||
assert!(sends.start(to, b"words"[..].into()));
|
||||
assert_eq!(read_capped(from, MAX_BYTES, LONG), Ok(b"words".to_vec()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_copys_data_is_sent_through_the_pipe_and_closed() {
|
||||
let sends = Sends::default();
|
||||
assert_eq!(sends.idle, IDLE_WAIT);
|
||||
let (from, to) = std::io::pipe().unwrap();
|
||||
let data: Arc<[u8]> = vec![7u8; 300_000].into();
|
||||
assert!(sends.start(to, data.clone()));
|
||||
// More than a pipe holds: the write ends once all of it is read, then the pipe closes.
|
||||
assert_eq!(read_capped(from, MAX_BYTES).as_deref(), Ok(&data[..]));
|
||||
assert_eq!(read_capped(from, MAX_BYTES, LONG).as_deref(), Ok(&data[..]));
|
||||
// An app that closes its end before reading ends the send.
|
||||
let (from, to) = std::io::pipe().unwrap();
|
||||
drop(from);
|
||||
@@ -2593,9 +2735,9 @@ mod wayland {
|
||||
|
||||
#[test]
|
||||
fn pastes_that_read_nothing_hold_a_few_sends_at_most() {
|
||||
let sends = Sends::default();
|
||||
let sends = Sends { idle: LONG, ..Sends::default() };
|
||||
let data: Arc<[u8]> = vec![1u8; 300_000].into();
|
||||
// Apps that ask for the data and never read it.
|
||||
// Apps that ask for the data and never read it, within the sends' idle wait.
|
||||
let stalled: Vec<_> = (0..MAX_SENDS)
|
||||
.map(|_| {
|
||||
let (from, to) = std::io::pipe().unwrap();
|
||||
@@ -2606,7 +2748,7 @@ mod wayland {
|
||||
// The next paste gets its pipe closed at once, with nothing written.
|
||||
let (from, to) = std::io::pipe().unwrap();
|
||||
assert!(!sends.start(to, data.clone()));
|
||||
assert_eq!(read_capped(from, MAX_BYTES), Ok(vec![]));
|
||||
assert_eq!(read_capped(from, MAX_BYTES, Duration::ZERO), Ok(vec![]));
|
||||
// Once those apps close their pipes, the sends end and others are sent again.
|
||||
drop(stalled);
|
||||
while sends.running() > 0 {
|
||||
@@ -2614,7 +2756,7 @@ mod wayland {
|
||||
}
|
||||
let (from, to) = std::io::pipe().unwrap();
|
||||
assert!(sends.start(to, data.clone()));
|
||||
assert_eq!(read_capped(from, MAX_BYTES).map(|d| d.len()), Ok(data.len()));
|
||||
assert_eq!(read_capped(from, MAX_BYTES, LONG).map(|d| d.len()), Ok(data.len()));
|
||||
}
|
||||
|
||||
/// What a clipboard holds, by type.
|
||||
|
||||
@@ -373,11 +373,14 @@ fn main() -> std::process::ExitCode {
|
||||
// Display Server › X11 opens the window through XWayland instead, where file drops work (#1151),
|
||||
// and the other choices do so when a pen is attached, where the pen draws (#2028, #4967).
|
||||
#[cfg(target_os = "linux")]
|
||||
let mut forced_to_x11 = false;
|
||||
#[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() {
|
||||
forced_to_x11 = true;
|
||||
if pref != Some("x11") {
|
||||
log::info!("pen found: opening the window through XWayland, where the pen works (VECTORCRAFT_NATIVE_WAYLAND=1 keeps Wayland)");
|
||||
} else {
|
||||
@@ -393,6 +396,11 @@ fn main() -> std::process::ExitCode {
|
||||
}
|
||||
options
|
||||
};
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
let forced_to_x11 = false;
|
||||
// Preferences › User Interface notes under System Title Bar when the desktop draws the frame
|
||||
// with it on or off.
|
||||
let desktop_keeps_frame = titlebar::desktop_keeps_frame(forced_to_x11);
|
||||
// With Windows Ink, winit reports a pen as a touch and drops its barrel button and which end
|
||||
// is down; the message hook notes each pointer contact and whether it starts with the barrel
|
||||
// button held, and writes the pen's end into the pen feed (`windows_stylus`). It also notes the
|
||||
@@ -595,6 +603,7 @@ fn main() -> std::process::ExitCode {
|
||||
app.custom_titlebar = custom_titlebar;
|
||||
app.caption_layout = caption_layout;
|
||||
app.desktop_frame = !custom_titlebar;
|
||||
app.desktop_keeps_frame = desktop_keeps_frame;
|
||||
if let Some(port) = control_port {
|
||||
let rx = control_server::start(port, cc.egui_ctx.clone());
|
||||
app = app.with_control(rx).with_automation_roots(roots.clone());
|
||||
|
||||
@@ -346,6 +346,34 @@ fn the_titlebar_variable_decides_the_frame_before_the_window_opens() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Preferences says under System Title Bar that the desktop draws the frame with it on or off
|
||||
/// exactly where the start frame without VectorCraft Title Bar on Every Desktop is the desktop's
|
||||
/// with System Title Bar off as well as on and `VECTORCRAFT_TITLEBAR` is unset: Linux and the BSDs
|
||||
/// only.
|
||||
#[test]
|
||||
fn the_desktop_keeps_the_frame_where_system_title_bar_does_not_change_it() {
|
||||
use crate::titlebar::desktop_keeps_frame_from;
|
||||
use craft_window::{Frame, Layout};
|
||||
let linux = cfg!(not(any(target_os = "macos", target_os = "windows")));
|
||||
for probed in [Frame::Desktop, Frame::App(Layout::default())] {
|
||||
for forced_to_x11 in [false, true] {
|
||||
let frame = super::start_frame(probed.clone(), None, forced_to_x11, false);
|
||||
let either_way = !super::custom_titlebar_from(&frame, false) && !super::custom_titlebar_from(&frame, true);
|
||||
assert_eq!(desktop_keeps_frame_from(&probed, None, forced_to_x11), linux && either_way, "{probed:?} {forced_to_x11}");
|
||||
}
|
||||
}
|
||||
// KDE, Sway and X11, and GNOME through XWayland, keep the desktop's frame; GNOME on Wayland does not.
|
||||
assert_eq!(desktop_keeps_frame_from(&Frame::Desktop, None, false), linux);
|
||||
assert_eq!(desktop_keeps_frame_from(&Frame::App(Layout::default()), None, true), linux);
|
||||
assert!(!desktop_keeps_frame_from(&Frame::App(Layout::default()), None, false));
|
||||
// While `VECTORCRAFT_TITLEBAR` chooses the frame, System Title Bar does not apply and there is no note.
|
||||
for (frame, overridden) in [(Frame::App(Layout::default()), Some(true)), (Frame::Desktop, Some(false))] {
|
||||
for forced_to_x11 in [false, true] {
|
||||
assert!(!desktop_keeps_frame_from(&frame, overridden, forced_to_x11), "{overridden:?} {forced_to_x11}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The file extensions the macOS bundle declares: its document types and its own exported type.
|
||||
fn plist_extensions(plist: &str) -> Vec<&str> {
|
||||
["<key>CFBundleTypeExtensions</key>", "<key>public.filename-extension</key>"]
|
||||
|
||||
@@ -191,6 +191,25 @@ pub(crate) fn custom_titlebar(saved: Option<&vectorcraft_ui_egui::UiState>) -> (
|
||||
(custom_titlebar_from(&frame, opted_into_system), caption_layout_from(&frame))
|
||||
}
|
||||
|
||||
/// Whether the desktop would draw this window's frame with System Title Bar on or off while
|
||||
/// VectorCraft Title Bar on Every Desktop is off: `VECTORCRAFT_TITLEBAR` is not set to `app`,
|
||||
/// `system` or `desktop` (`overridden` is `None`), and the probe chose
|
||||
/// [`craft_window::Frame::Desktop`] or the window opens through XWayland (`forced_to_x11`)
|
||||
/// ([`app_draws_frame`]). The saved VectorCraft Title Bar on Every Desktop is left out, so the value
|
||||
/// stays `true` on such a desktop while that preference gives the app the frame. Always `false` on
|
||||
/// macOS and Windows, where System Title Bar decides the frame by itself.
|
||||
pub(crate) fn desktop_keeps_frame_from(probed: &craft_window::Frame, overridden: Option<bool>, forced_to_x11: bool) -> bool {
|
||||
let probed_app = matches!(probed, craft_window::Frame::App(_));
|
||||
cfg!(not(any(target_os = "macos", target_os = "windows"))) && overridden.is_none() && !app_draws_frame(probed_app, None, forced_to_x11, false)
|
||||
}
|
||||
|
||||
/// [`desktop_keeps_frame_from`] for this start's probe, given whether the launch path opens the
|
||||
/// window through XWayland.
|
||||
pub(crate) fn desktop_keeps_frame(forced_to_x11: bool) -> bool {
|
||||
let choice = probed_choice();
|
||||
desktop_keeps_frame_from(choice.frame(), choice.overridden(), forced_to_x11)
|
||||
}
|
||||
|
||||
pub(crate) fn native_options(custom_titlebar: bool) -> eframe::NativeOptions {
|
||||
eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default()
|
||||
|
||||
@@ -114,51 +114,9 @@ fn strip_brushes(n: &mut Node) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Mix every solid paint under `n` towards `color` by `amount` (0..1) — the Symbol Stainer.
|
||||
pub fn tint_node(n: &mut Node, color: &Color, amount: f32) {
|
||||
let amount = amount.clamp(0.0, 1.0);
|
||||
for it in &mut n.appearance.items {
|
||||
let p = match it {
|
||||
AppearanceItem::Fill(f) => &mut f.paint,
|
||||
AppearanceItem::Stroke(s) => &mut s.paint,
|
||||
};
|
||||
if let Paint::Solid { color: c, .. } = p {
|
||||
*p = Paint::solid(c.lerp(color, amount));
|
||||
}
|
||||
}
|
||||
if let vectorcraft_doc::NodeKind::Text(t) = &mut n.kind {
|
||||
for r in &mut t.runs {
|
||||
for p in [&mut r.style.fill, &mut r.style.stroke] {
|
||||
if let Paint::Solid { color: c, .. } = p {
|
||||
*p = Paint::solid(c.lerp(color, amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(ch) = n.children_mut() {
|
||||
for c in ch.iter_mut() {
|
||||
tint_node(Arc::make_mut(c), color, amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The stain instance `inst` puts on its symbol's art: its visible fill colour and that fill's
|
||||
/// opacity (how far the art mixes towards it). `None` for an unstained instance, including one
|
||||
/// whose fill has an opacity of 0, so its art keeps its paints and their swatch links.
|
||||
pub fn stain(inst: &Node) -> Option<(Color, f32)> {
|
||||
let f = inst.appearance.fill().filter(|f| f.visible && f.opacity > 0.0)?;
|
||||
Some((f.paint.color()?, f.opacity))
|
||||
}
|
||||
|
||||
/// 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 = vectorcraft_doc::symbol_overrides::dynamic_art(art, inst);
|
||||
if let Some((c, amount)) = stain(inst) {
|
||||
tint_node(&mut a, &c, amount);
|
||||
}
|
||||
a
|
||||
}
|
||||
/// The Symbol Stainer's mix, an instance's stain and its stained art, from
|
||||
/// [`vectorcraft_doc::symbol_overrides`], which the effects that reshape an instance also read.
|
||||
pub use vectorcraft_doc::symbol_overrides::{instance_art, stain, tint_node};
|
||||
|
||||
/// The solid colours of the fills and strokes under `nodes`, most used first (then in the order
|
||||
/// they first appear): the colours Brush Options offers as Hue Shift's key colour, the first being
|
||||
|
||||
@@ -683,7 +683,7 @@ impl<'a> Scene<'a> {
|
||||
{
|
||||
// Symbols holding instances of symbols are written in full for each instance,
|
||||
// empty or not: every instance's art counts against one budget for the export.
|
||||
let art = vectorcraft_doc::symbol_overrides::dynamic_art(&sym.art, n);
|
||||
let art = vectorcraft_doc::symbol_overrides::instance_art(&sym.art, n);
|
||||
let count = art.count_with_masks() as u64;
|
||||
// Counted as `place_symbol_art_unclipped` places it: no clip groups.
|
||||
let most = doc.symbol_slice_map(symbol, *xf).map_or(count, |m| m.most_placed_unclipped(&art, doc));
|
||||
|
||||
@@ -1395,6 +1395,21 @@ fn crop_keeps_an_instance_whose_art_reaches_the_region() {
|
||||
assert!(!kinds(&export(&d, &o).unwrap()).is_empty());
|
||||
}
|
||||
|
||||
/// An instance stained blue by its fill writes its symbol's red art in blue, as the canvas draws it.
|
||||
#[test]
|
||||
fn a_stained_instance_is_written_in_its_stained_color() {
|
||||
let mut d = doc_with(|d| {
|
||||
let mut n = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Mark".into(), xf: Affine::translate((30.0, 30.0)) });
|
||||
n.appearance = Appearance::basic(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0).into();
|
||||
vec![n]
|
||||
});
|
||||
let art = path(&mut d, shapes::rectangle(Rect::new(0.0, 0.0, 40.0, 40.0)), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Mark".into(), art: Arc::new(art) });
|
||||
let out = export(&d, &opts(&d)).unwrap();
|
||||
let colors: Vec<Option<String>> = entities(&out).iter().map(|e| get(e, 420).map(str::to_string)).collect();
|
||||
assert!(!colors.is_empty() && colors.iter().all(|c| c.as_deref() == Some("255")), "{colors:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn symbols_fanning_out_into_empty_symbols_stop_at_the_symbol_budget() {
|
||||
// Five levels of 100 instances of the level below, ending in an empty symbol: 10^10 empty
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
//! square to it: scaling by a side handle stretches it along its own width or height. Path
|
||||
//! geometry is still baked; Object ▸ Transform ▸ Reset Bounding Box sets the angle back to 0.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use vectorcraft_geom::{Affine, Point, Rect, Vec2, normalize_deg, union_opt};
|
||||
|
||||
use crate::{Document, Node, NodeId, NodeKind, SymbolBoxes};
|
||||
@@ -84,7 +86,35 @@ pub fn transformed_angle(angle: f64, a: Affine) -> f64 {
|
||||
}
|
||||
}
|
||||
|
||||
/// The turn (counter-clockwise degrees) from the boxes of art placed by a symbol instance's
|
||||
/// transform `xf` to the instance's own box at `angle`: 0 while the box turns with the transform,
|
||||
/// otherwise the change Reset Bounding Box made to it. Under a mirroring `xf` the instance's box can
|
||||
/// stand 180° from the one `xf` gives in one step ([`transformed_angle`] keeps the angle nearer the
|
||||
/// one before at each step); the turn is then 180°, so the art's boxes match the instance's.
|
||||
pub fn placed_turn(angle: f64, xf: Affine) -> f64 {
|
||||
let turn = normalize_deg(angle - transformed_angle(0.0, xf));
|
||||
if turn.is_finite() && turn.abs() > EPS_DEG { turn } else { 0.0 }
|
||||
}
|
||||
|
||||
impl Node {
|
||||
/// Turns the box of this object, and of every object that turns with it ([`Node::transform`]:
|
||||
/// its children and the art of a linked opacity mask), by `by` degrees counter-clockwise.
|
||||
/// Layers have no box.
|
||||
pub fn turn_boxes(&mut self, by: f64) {
|
||||
if !self.is_layer() {
|
||||
let a = normalize_deg(self.bbox_angle + by);
|
||||
self.bbox_angle = if a.abs() <= EPS_DEG { 0.0 } else { a };
|
||||
}
|
||||
for c in self.children_mut().into_iter().flatten() {
|
||||
Arc::make_mut(c).turn_boxes(by);
|
||||
}
|
||||
if let Some(m) = self.extras_if_any().and_then(|e| e.mask.as_mut())
|
||||
&& m.linked
|
||||
{
|
||||
Arc::make_mut(&mut m.art).turn_boxes(by);
|
||||
}
|
||||
}
|
||||
|
||||
/// Bounds of this object as `a` would place it (geometric, or `visual` with its strokes),
|
||||
/// without changing it, symbol instances measured by their art from `symbols`
|
||||
/// ([`Node::geometric_bounds_with`]).
|
||||
@@ -185,6 +215,44 @@ mod tests {
|
||||
assert_eq!(transformed_angle(30.0, Affine::scale(0.0)), 30.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placed_turn_is_what_reset_bounding_box_took_off_the_instance() {
|
||||
let xf = Affine::translate((40.0, 10.0)) * rot(20.0) * Affine::scale_non_uniform(2.0, 0.5);
|
||||
// The box turns with the transform: no turn.
|
||||
assert_eq!(placed_turn(transformed_angle(0.0, xf), xf), 0.0);
|
||||
assert_eq!(placed_turn(0.0, Affine::scale(3.0)), 0.0);
|
||||
// Reset to 0: the placed art turns back by the 20°.
|
||||
assert!(close(placed_turn(0.0, xf), -20.0));
|
||||
// Mirrored: turned 170° and then flipped, the instance's box stands 180° from the box the
|
||||
// whole transform gives in one step, and the art turns the half turn to match it.
|
||||
let flip = Affine::scale_non_uniform(1.0, -1.0);
|
||||
let flipped = flip * rot(170.0);
|
||||
let stepwise = transformed_angle(transformed_angle(0.0, rot(170.0)), flip);
|
||||
assert!(close(stepwise, -170.0) && close(transformed_angle(0.0, flipped), 10.0));
|
||||
assert!(close(placed_turn(stepwise, flipped).abs(), 180.0));
|
||||
assert!(close(normalize_deg(placed_turn(0.0, flipped) + transformed_angle(0.0, flipped)), 0.0));
|
||||
// Degenerate and non-finite transforms turn nothing.
|
||||
assert_eq!(placed_turn(0.0, Affine::scale(0.0)), 0.0);
|
||||
assert_eq!(placed_turn(f64::NAN, xf), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn turn_boxes_turns_children_and_linked_mask_art_but_not_layers() {
|
||||
let mut d = Document::new(500.0, 500.0);
|
||||
let a = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default_art());
|
||||
let mut g = Node::group(d.alloc_id(), vec![Arc::new(a.clone())]);
|
||||
g.bbox_angle = 170.0;
|
||||
g.extras_mut().mask = Some(Box::new(crate::OpacityMask::new(a.clone(), true)));
|
||||
let mut layer = (*d.layers[0]).clone();
|
||||
layer.children_mut().unwrap().push(Arc::new(g));
|
||||
layer.turn_boxes(20.0);
|
||||
assert_eq!(layer.bbox_angle, 0.0);
|
||||
let g = &layer.children().unwrap()[0];
|
||||
assert!(close(g.bbox_angle, -170.0), "wraps to (-180, 180]");
|
||||
assert!(close(g.children().unwrap()[0].bbox_angle, 20.0));
|
||||
assert!(close(g.extras().mask.as_ref().unwrap().art.bbox_angle, 20.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oriented_bounds_hug_a_rotated_rectangle() {
|
||||
let mut d = Document::new(500.0, 500.0);
|
||||
|
||||
@@ -614,6 +614,8 @@ impl NineSliceMap {
|
||||
/// `n`, whose box at the natural size is `b`, drawn in each cell it reaches: a clip group of
|
||||
/// the cell around a copy placed by the cell's map, all in a group with `n`'s id. `n` placed
|
||||
/// by `center` when it draws symbol art ([`draws_symbol_art`]), and in [`Self::place_unclipped`].
|
||||
/// The clip groups and clip paths take the angle of the cell map that places them. The group
|
||||
/// and each copy keep `n`'s box angle, turned by the map that places them.
|
||||
///
|
||||
/// The drop shadows, outer glows and blurs ([`PAINTED_EFFECTS`]) at the end of `n`'s own
|
||||
/// effects, after its last other visible effect, go on that group with its opacity, blending
|
||||
@@ -647,11 +649,15 @@ impl NineSliceMap {
|
||||
area.transform(local);
|
||||
let mut clip = Node::new(NodeId(0), NodeKind::Path { path: area, rule: FillRule::NonZero, live: None, clipping: true, guide: false });
|
||||
clip.appearance = Appearance::default().into();
|
||||
clip.bbox_angle = crate::orient::transformed_angle(0.0, local);
|
||||
let copy = transformed(n, local * self.natural);
|
||||
cells.push(Arc::new(Node::new(NodeId(0), NodeKind::Group { children: vec![Arc::new(clip), Arc::new(copy)], clip: true })));
|
||||
let mut cell = Node::new(NodeId(0), NodeKind::Group { children: vec![Arc::new(clip), Arc::new(copy)], clip: true });
|
||||
cell.bbox_angle = crate::orient::transformed_angle(0.0, local);
|
||||
cells.push(Arc::new(cell));
|
||||
}
|
||||
}
|
||||
let mut out = Node::group(n.id, cells);
|
||||
out.bbox_angle = crate::orient::transformed_angle(n.bbox_angle, center);
|
||||
out.visible = n.visible;
|
||||
out.locked = n.locked;
|
||||
out.name = n.name.clone();
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
//! 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`]).
|
||||
//! applied wherever an instance is drawn or exported ([`dynamic_art`]), and an instance's fill
|
||||
//! stains the art after them ([`instance_art`]).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use vectorcraft_color::{BlendMode, Paint};
|
||||
use vectorcraft_color::{BlendMode, Color, Paint};
|
||||
|
||||
use crate::{Node, NodeKind};
|
||||
use crate::{AppearanceItem, Node, NodeKind};
|
||||
|
||||
/// Deepest object an override may name: groups nested this deep in the symbol's art.
|
||||
pub const MAX_PATH_DEPTH: usize = 64;
|
||||
@@ -150,6 +151,52 @@ pub fn dynamic_art(art: &Node, inst: &Node) -> Node {
|
||||
a
|
||||
}
|
||||
|
||||
/// Mix every solid paint under `n` towards `color` by `amount` (0..1): the Symbol Stainer.
|
||||
pub fn tint_node(n: &mut Node, color: &Color, amount: f32) {
|
||||
let amount = amount.clamp(0.0, 1.0);
|
||||
for it in &mut n.appearance.items {
|
||||
let p = match it {
|
||||
AppearanceItem::Fill(f) => &mut f.paint,
|
||||
AppearanceItem::Stroke(s) => &mut s.paint,
|
||||
};
|
||||
if let Paint::Solid { color: c, .. } = p {
|
||||
*p = Paint::solid(c.lerp(color, amount));
|
||||
}
|
||||
}
|
||||
if let NodeKind::Text(t) = &mut n.kind {
|
||||
for r in &mut t.runs {
|
||||
for p in [&mut r.style.fill, &mut r.style.stroke] {
|
||||
if let Paint::Solid { color: c, .. } = p {
|
||||
*p = Paint::solid(c.lerp(color, amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(ch) = n.children_mut() {
|
||||
for c in ch.iter_mut() {
|
||||
tint_node(Arc::make_mut(c), color, amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The stain instance `inst` puts on its symbol's art: its visible fill color and that fill's
|
||||
/// opacity (how far the art mixes towards it). `None` for an unstained instance, including one
|
||||
/// whose fill has an opacity of 0, so its art keeps its paints and their swatch links.
|
||||
pub fn stain(inst: &Node) -> Option<(Color, f32)> {
|
||||
let f = inst.appearance.fill().filter(|f| f.visible && f.opacity > 0.0)?;
|
||||
Some((f.paint.color()?, f.opacity))
|
||||
}
|
||||
|
||||
/// A symbol's art for instance `inst`, as the canvas draws it: with the instance's own appearance
|
||||
/// changes to it ([`dynamic_art`]), stained by the instance's fill ([`stain`]).
|
||||
pub fn instance_art(art: &Node, inst: &Node) -> Node {
|
||||
let mut a = dynamic_art(art, inst);
|
||||
if let Some((c, amount)) = stain(inst) {
|
||||
tint_node(&mut a, &c, amount);
|
||||
}
|
||||
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());
|
||||
@@ -159,7 +206,6 @@ pub fn retain_parts(overrides: &mut Vec<SymbolOverride>, art: &Node) {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Appearance, NodeId};
|
||||
use vectorcraft_color::Color;
|
||||
use vectorcraft_geom::{Rect, shapes};
|
||||
|
||||
fn square(id: u64) -> Arc<Node> {
|
||||
|
||||
@@ -523,13 +523,14 @@ fn color_mask_keys<'a>(n: &'a Node, out: &mut Vec<&'a str>) {
|
||||
|
||||
/// [`adjust`] for export and Expand Appearance: recoloured embedded images become new images in
|
||||
/// `d` (named by their content), and a symbol instance stands for its symbol's art: `symbol_art`
|
||||
/// when given, otherwise the symbol's art with the instance's own appearance changes.
|
||||
/// when given, otherwise the symbol's art with the instance's own appearance changes, stained by
|
||||
/// the instance's fill ([`vectorcraft_doc::symbol_overrides::instance_art`]).
|
||||
pub fn adjust_in_document(d: &mut Document, n: &Node, symbol_art: Option<Node>) -> Option<Node> {
|
||||
let art;
|
||||
let src = match &n.kind {
|
||||
NodeKind::SymbolInstance { symbol, .. } if has_adjustment(n) => {
|
||||
let symbol = symbol_art
|
||||
.or_else(|| d.symbols.iter().find(|s| s.name == *symbol).map(|s| vectorcraft_doc::symbol_overrides::dynamic_art(&s.art, n)));
|
||||
.or_else(|| d.symbols.iter().find(|s| s.name == *symbol).map(|s| vectorcraft_doc::symbol_overrides::instance_art(&s.art, n)));
|
||||
art = crate::reshape::as_art(Some(d), n, symbol.as_ref())?;
|
||||
&art
|
||||
}
|
||||
|
||||
@@ -148,9 +148,11 @@ fn bake_node(d: &mut Document, n: &Node) -> Option<Node> {
|
||||
if let Some(m) = moved_type_group(n) {
|
||||
return Some(bake_pieces(d, m));
|
||||
}
|
||||
// A symbol instance's art as the canvas draws it: with its own appearance changes, stained
|
||||
// by its fill.
|
||||
let symbol = match &n.kind {
|
||||
NodeKind::SymbolInstance { symbol, .. } => {
|
||||
d.symbols.iter().find(|s| s.name == *symbol).map(|s| vectorcraft_doc::symbol_overrides::dynamic_art(&s.art, n))
|
||||
d.symbols.iter().find(|s| s.name == *symbol).map(|s| vectorcraft_doc::symbol_overrides::instance_art(&s.art, n))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
@@ -59,6 +59,46 @@ fn object_effects_become_geometry_and_raster_effects_stay() {
|
||||
assert_eq!(m.appearance.effects[0].id, "stylize.dropShadow");
|
||||
}
|
||||
|
||||
/// The fill colors of the paths under the layers of `d`, in paint order.
|
||||
fn path_fills(d: &Document) -> Vec<Color> {
|
||||
let mut out = vec![];
|
||||
d.walk(|n| {
|
||||
if matches!(n.kind, NodeKind::Path { .. })
|
||||
&& let Some(c) = n.appearance.fill_paint().color()
|
||||
{
|
||||
out.push(c);
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// A symbol instance half stained red is baked in its stained colors, as the canvas draws it:
|
||||
/// moved by a Transform effect, and recolored by a color adjustment (Invert Colors) after the
|
||||
/// stain.
|
||||
#[test]
|
||||
fn a_stained_instance_is_baked_in_its_stained_colors() {
|
||||
let blue = Color::rgb8(30, 90, 220);
|
||||
let art = Node::path(NodeId(2), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::basic(Paint::solid(blue), Paint::None, 0.0));
|
||||
let mut inst = Node::new(NodeId(50), NodeKind::SymbolInstance { symbol: "Mark".into(), xf: vectorcraft_geom::Affine::translate((20.0, 20.0)) });
|
||||
inst.appearance = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0).into();
|
||||
inst.appearance.items.retain(|i| i.is_fill());
|
||||
if let vectorcraft_doc::AppearanceItem::Fill(f) = &mut inst.appearance.items[0] {
|
||||
f.opacity = 0.5;
|
||||
}
|
||||
let stained = vectorcraft_doc::symbol_overrides::instance_art(&art, &inst).appearance.fill_paint().color().unwrap();
|
||||
assert_ne!(stained.to_hex(), blue.to_hex());
|
||||
let [r, g, b] = stained.to_rgb();
|
||||
let inverted = Color::rgb(1.0 - r, 1.0 - g, 1.0 - b);
|
||||
for (id, params, want) in [("distort.transform", json!({"moveH": 30.0}), stained), (crate::addons::color::INVERT, json!({}), inverted)] {
|
||||
let mut n = inst.clone();
|
||||
n.appearance.effects.push(fx(id, params));
|
||||
let mut d = doc_with(n);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Mark".into(), art: Arc::new(art.clone()) });
|
||||
let fills = path_fills(&bake_document(&d).unwrap());
|
||||
assert_eq!(fills.iter().map(Color::to_hex).collect::<Vec<_>>(), [want.to_hex()], "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_baked_transform_moves_a_placed_gradient_with_the_path() {
|
||||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||||
|
||||
@@ -23,15 +23,22 @@ pub fn text_outliner() -> Outliner<'static> {
|
||||
/// Type outlined, or a symbol instance as its symbol's art (from `doc`) placed by the instance,
|
||||
/// through the symbol's 9-slice guides when it has them ([`Document::place_symbol_art`], `None`
|
||||
/// past its object limit). As an [`OutlineHook`](crate::OutlineHook), it gives compound shapes and
|
||||
/// Pathfinder operations the symbol instances among their members.
|
||||
/// Pathfinder operations the symbol instances among their members. The group around the art takes
|
||||
/// the instance's box angle, and the art's boxes turn with it, as Break Link turns them.
|
||||
pub fn outline_object(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
match &n.kind {
|
||||
NodeKind::SymbolInstance { symbol, xf } => {
|
||||
let doc = doc?;
|
||||
let sym = doc.symbols.iter().find(|s| s.name == *symbol)?;
|
||||
let art = vectorcraft_doc::symbol_overrides::dynamic_art(&sym.art, n);
|
||||
let art = doc.place_symbol_art(symbol, &art, *xf)?;
|
||||
Some(Node::group(n.id, vec![Arc::new(art)]))
|
||||
let mut art = doc.place_symbol_art(symbol, &art, *xf)?;
|
||||
let turn = vectorcraft_doc::orient::placed_turn(n.bbox_angle, *xf);
|
||||
if turn != 0.0 {
|
||||
art.turn_boxes(turn);
|
||||
}
|
||||
let mut out = Node::group(n.id, vec![Arc::new(art)]);
|
||||
out.bbox_angle = n.bbox_angle;
|
||||
Some(out)
|
||||
}
|
||||
_ => outline_text(n),
|
||||
}
|
||||
|
||||
@@ -1570,6 +1570,27 @@ fn outline_object_is_a_geometry_effect_that_keeps_the_path() {
|
||||
assert_eq!(apply_geometry(&[fx("path.outlineObject", json!({})), offset.clone()], &square(), b), apply_geometry(&[offset], &square(), b));
|
||||
}
|
||||
|
||||
/// A symbol instance given to Pathfinder and compound shapes as its art: the group takes the
|
||||
/// instance's box angle and the art turns with it, turned with the instance or reset to 0.
|
||||
#[test]
|
||||
fn outline_object_turns_an_instances_art_with_its_box() {
|
||||
use std::sync::Arc;
|
||||
|
||||
use vectorcraft_doc::orient::transformed_angle;
|
||||
use vectorcraft_doc::{Appearance, Document, NodeId, Symbol};
|
||||
let mut d = Document::new(500.0, 500.0);
|
||||
d.symbols.push(Symbol { dynamic: false, name: "s".into(), art: Arc::new(Node::path(NodeId(0), square(), Appearance::default())) });
|
||||
let xf = Affine::translate((200.0, 100.0)) * Affine::rotate(0.35);
|
||||
for box_angle in [transformed_angle(0.0, xf), 0.0] {
|
||||
let mut inst = Node::new(NodeId(3), NodeKind::SymbolInstance { symbol: "s".into(), xf });
|
||||
inst.bbox_angle = box_angle;
|
||||
let out = outline_object(Some(&d), &inst).unwrap();
|
||||
let mut angles = vec![];
|
||||
out.walk(&mut |c| angles.push(c.bbox_angle));
|
||||
assert!(angles.len() == 2 && angles.iter().all(|a| close(*a, box_angle, 1e-9)), "{box_angle}: {angles:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_to_shape_sizes() {
|
||||
let r = run("convertToShape.rectangle", json!({"extraW": 20, "extraH": 10}), &square()).bounds().unwrap();
|
||||
|
||||
@@ -442,6 +442,15 @@ pub(crate) fn f64_req(p: &Value, key: &str, cmd: &str) -> Result<f64> {
|
||||
Some(v) => v.as_f64().ok_or_else(|| bad(cmd, format!("`{key}` must be a number, not {v}"))),
|
||||
}
|
||||
}
|
||||
/// The whole number of 0 or more that `p` gives for `key`. A missing key or null is "missing integer
|
||||
/// `{key}`"; any other value, such as `"3"`, `2.5`, `-1` or `true`, is "`{key}` must be a whole
|
||||
/// number", as [`object_param`] words it.
|
||||
pub(crate) fn u64_req(p: &Value, key: &str, cmd: &str) -> Result<u64> {
|
||||
match p.get(key) {
|
||||
None | Some(Value::Null) => Err(bad(cmd, format!("missing integer `{key}`"))),
|
||||
Some(v) => v.as_u64().ok_or_else(|| bad(cmd, format!("`{key}` must be a whole number"))),
|
||||
}
|
||||
}
|
||||
/// The number `p` gives for `key`, or `default` when `p` leaves it out or gives null. Any other
|
||||
/// value, such as `"40"`, is an error: "`{key}` must be a number, not {v}".
|
||||
pub(crate) fn number_or(p: &Value, key: &str, default: f64, cmd: &str) -> Result<f64> {
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::{LiveShape, NodeId, NodeKind};
|
||||
use vectorcraft_geom::{Affine, FillRule, Point, Rect, shapes};
|
||||
@@ -16,9 +18,25 @@ use vectorcraft_tools::addons::box3d::CustomShape;
|
||||
|
||||
use super::super::create::insert_look;
|
||||
use super::super::{
|
||||
CommandSpec, Result, Session, bool_or, check_numbers, check_switches, cmd, f64_or, f64_req, has_doc, has_selection, str_param, targets,
|
||||
CommandSpec, Result, Session, bad, bool_or, check_numbers, check_switches, cmd, f64_or, f64_req, has_doc, has_selection, number_or, str_param,
|
||||
targets,
|
||||
};
|
||||
|
||||
/// The sides of a polygon and the points of a star: whole numbers in this range.
|
||||
const COUNTS: RangeInclusive<f64> = 3.0..=1000.0;
|
||||
/// `rounded` and `randomized` of a polygon or star.
|
||||
const ROUNDING: RangeInclusive<f64> = -10.0..=10.0;
|
||||
/// A star's inner radius as a fraction of its outer radius.
|
||||
const SPOKE_RATIO: RangeInclusive<f64> = 0.01..=1.0;
|
||||
/// A spiral's `turns`.
|
||||
const TURNS: RangeInclusive<f64> = 0.01..=1024.0;
|
||||
/// A spiral's `divergence`.
|
||||
const DIVERGENCE: RangeInclusive<f64> = 0.0..=1000.0;
|
||||
/// A spiral's `innerRadius`, as a fraction of its radius.
|
||||
const INNER_RADIUS: RangeInclusive<f64> = 0.0..=0.999;
|
||||
/// The `arcMode` names; `slice` and `open` are also read as `pie` and `arc`.
|
||||
const ARC_MODES: [&str; 4] = ["whole", "pie", "chord", "arc"];
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
@@ -26,7 +44,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Rectangle",
|
||||
[],
|
||||
None,
|
||||
"{x, y, width, height, radius?: pt, radii?: [tl,tr,br,bl], startAngle?, endAngle?: degrees (the part of the outline kept, clockwise from its first anchor, the top-left corner; each side an equal share of 360; default 0 and 360), arcMode?: whole|pie|chord|arc, rotation?: deg clockwise about the centre (0: upright)} → {id}",
|
||||
"{x, y, width, height: pt (0 or more), radius?: pt (0 or more), radii?: [tl,tr,br,bl] (each 0 or more), startAngle?, endAngle?: degrees (the part of the outline kept, clockwise from its first anchor, the top-left corner; each side an equal share of 360; default 0 and 360), arcMode?: whole|pie|chord|arc, rotation?: deg clockwise about the centre (0: upright)} → {id}",
|
||||
has_doc,
|
||||
rectangle
|
||||
),
|
||||
@@ -35,7 +53,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Ellipse",
|
||||
[],
|
||||
None,
|
||||
"{x, y, width, height, startAngle?, endAngle?: degrees (counter-clockwise from 3 o'clock, as object.setLiveShape's pieStart and pieEnd; default 0 and 360), arcMode?: whole|pie|chord|arc, rotation?: deg clockwise about the centre (0: upright)} → {id}",
|
||||
"{x, y, width, height: pt (0 or more), startAngle?, endAngle?: degrees (counter-clockwise from 3 o'clock, as object.setLiveShape's pieStart and pieEnd; default 0 and 360), arcMode?: whole|pie|chord|arc, rotation?: deg clockwise about the centre (0: upright)} → {id}",
|
||||
has_doc,
|
||||
ellipse
|
||||
),
|
||||
@@ -44,7 +62,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Polygon",
|
||||
[],
|
||||
None,
|
||||
"{cx, cy, radius, sides=6, rounded=0, randomized=0, startAngle?, endAngle?: degrees (the part of the outline kept, clockwise from its first anchor, the top corner; each side an equal share of 360; default 0 and 360), arcMode?: whole|pie|chord|arc, rotation?: deg clockwise (0: a corner straight up)} → {id}",
|
||||
"{cx, cy, radius: pt (0 or more), sides=6: a whole number 3 to 1000, rounded=0, randomized=0: -10 to 10, startAngle?, endAngle?: degrees (the part of the outline kept, clockwise from its first anchor, the top corner; each side an equal share of 360; default 0 and 360), arcMode?: whole|pie|chord|arc, rotation?: deg clockwise (0: a corner straight up)} → {id}",
|
||||
has_doc,
|
||||
polygon
|
||||
),
|
||||
@@ -53,7 +71,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Star",
|
||||
[],
|
||||
None,
|
||||
"{cx, cy, radius1, radius2?, points=5, rounded=0, randomized=0, startAngle?, endAngle?: degrees (the part of the outline kept, clockwise from its first anchor, the top point; each side an equal share of 360; default 0 and 360), arcMode?: whole|pie|chord|arc, rotation?: deg clockwise (0: a point straight up)} → {id}",
|
||||
"{cx, cy, radius1: pt (0 or more), radius2?: pt (0.01 to 1 times radius1; default half of it), points=5: a whole number 3 to 1000, rounded=0, randomized=0: -10 to 10, startAngle?, endAngle?: degrees (the part of the outline kept, clockwise from its first anchor, the top point; each side an equal share of 360; default 0 and 360), arcMode?: whole|pie|chord|arc, rotation?: deg clockwise (0: a point straight up)} → {id}",
|
||||
has_doc,
|
||||
star
|
||||
),
|
||||
@@ -62,7 +80,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Spiral",
|
||||
[],
|
||||
None,
|
||||
"{cx, cy, radius, turns=3, divergence=1, innerRadius=0, clockwise=true} → {id}",
|
||||
"{cx, cy, radius: pt (0 or more), turns=3: 0.01 to 1024, divergence=1: 0 to 1000, innerRadius=0: 0 to 0.999 of radius, clockwise=true} → {id}",
|
||||
has_doc,
|
||||
spiral
|
||||
),
|
||||
@@ -71,7 +89,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"3D Box",
|
||||
[],
|
||||
None,
|
||||
"{x, y, width, height, angleX=30 (degrees about the level axis: positive tips the top away from you), angleY=30 (degrees about the upright axis: positive turns the right side away from you), angleZ=0 (degrees clockwise in the page), parallelX=false, parallelY=true, parallelZ=false} → {id}",
|
||||
"{x, y, width, height: pt (0 or more), angleX=30 (degrees about the level axis: positive tips the top away from you), angleY=30 (degrees about the upright axis: positive turns the right side away from you), angleZ=0 (degrees clockwise in the page), parallelX=false, parallelY=true, parallelZ=false} → {id}",
|
||||
has_doc,
|
||||
box3d
|
||||
),
|
||||
@@ -80,33 +98,96 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Configure Shape",
|
||||
[],
|
||||
None,
|
||||
"{ids?, radius?, radii?: [tl,tr,br,bl], startAngle?, endAngle?: degrees (as addon.shapes.rectangle, ellipse, polygon and star take them: an ellipse's counter-clockwise from 3 o'clock, the others' clockwise along the outline), arcMode?: whole|pie|chord|arc, sides?, isStar?, spokeRatio?, rounded?, randomized?, turns?, divergence?, innerRadius?, angleX?, angleY?: degrees (as addon.shapes.box3d: positive tips the top, or turns the right side, away from you), angleZ?: degrees clockwise in the page, parallelX?, parallelY?, parallelZ?, reset?: bool} → {updated}",
|
||||
"{ids?, radius?: pt (0 or more; fitted to half each rectangle's shorter side), radii?: [tl,tr,br,bl] (each 0 or more), startAngle?, endAngle?: degrees (as addon.shapes.rectangle, ellipse, polygon and star take them: an ellipse's counter-clockwise from 3 o'clock, the others' clockwise along the outline), arcMode?: whole|pie|chord|arc, sides?: a whole number 3 to 1000, isStar?, spokeRatio?: 0.01 to 1, rounded?, randomized?: -10 to 10, turns?: 0.01 to 1024, divergence?: 0 to 1000, innerRadius?: 0 to 0.999, angleX?, angleY?: degrees (as addon.shapes.box3d: positive tips the top, or turns the right side, away from you), angleZ?: degrees clockwise in the page, parallelX?, parallelY?, parallelZ?, reset?: bool} an omitted or null value keeps each shape's setting → {updated}",
|
||||
has_selection,
|
||||
configure
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
fn parse_arc_params(p: &Value) -> ((f64, f64), shapes::EllipseMode) {
|
||||
/// The number `p` gives for `key`, or `default` when `p` leaves it out or gives null. A number
|
||||
/// outside `range` is an error that names `key` and the range.
|
||||
fn ranged(p: &Value, key: &str, default: f64, range: &RangeInclusive<f64>, cmd: &str) -> Result<f64> {
|
||||
let x = number_or(p, key, default, cmd)?;
|
||||
if !range.contains(&x) {
|
||||
let (min, max) = (range.start(), range.end());
|
||||
return Err(bad(cmd, format!("`{key}` must be between {min} and {max} (got {x})")));
|
||||
}
|
||||
Ok(x)
|
||||
}
|
||||
|
||||
/// The number of sides or points `p` gives for `key`, or `default` when `p` leaves it out or gives
|
||||
/// null. A fraction or a number outside [`COUNTS`] is an error.
|
||||
fn count(p: &Value, key: &str, default: u32, cmd: &str) -> Result<u32> {
|
||||
let x = number_or(p, key, f64::from(default), cmd)?;
|
||||
if x.fract() != 0.0 || !COUNTS.contains(&x) {
|
||||
let (min, max) = (COUNTS.start(), COUNTS.end());
|
||||
return Err(bad(cmd, format!("`{key}` must be a whole number between {min} and {max} (got {x})")));
|
||||
}
|
||||
Ok(x as u32)
|
||||
}
|
||||
|
||||
/// The size `p` gives for `key`: a number that is not negative.
|
||||
fn size(p: &Value, key: &str, cmd: &str) -> Result<f64> {
|
||||
not_negative(f64_req(p, key, cmd)?, key, cmd)
|
||||
}
|
||||
|
||||
/// The size `p` gives for `key`, or `default` when `p` leaves it out or gives null.
|
||||
fn size_or(p: &Value, key: &str, default: f64, cmd: &str) -> Result<f64> {
|
||||
not_negative(number_or(p, key, default, cmd)?, key, cmd)
|
||||
}
|
||||
|
||||
fn not_negative(x: f64, key: &str, cmd: &str) -> Result<f64> {
|
||||
if x < 0.0 {
|
||||
return Err(bad(cmd, format!("`{key}` must be a number not below 0")));
|
||||
}
|
||||
Ok(x)
|
||||
}
|
||||
|
||||
/// The four corner radii `p` gives as `radii` ([tl, tr, br, bl]), or `None` when `p` leaves it out
|
||||
/// or gives null. Anything but four numbers that are not negative is an error.
|
||||
fn radii_param(p: &Value, cmd: &str) -> Result<Option<[f64; 4]>> {
|
||||
let key = "radii";
|
||||
match p.get(key) {
|
||||
None | Some(Value::Null) => Ok(None),
|
||||
Some(v) => {
|
||||
let four = v.as_array().filter(|a| a.len() == 4).and_then(|a| {
|
||||
let mut out = [0.0; 4];
|
||||
for (slot, item) in out.iter_mut().zip(a) {
|
||||
*slot = item.as_f64().filter(|r| *r >= 0.0)?;
|
||||
}
|
||||
Some(out)
|
||||
});
|
||||
four.map(Some).ok_or_else(|| bad(cmd, format!("`{key}` must be four numbers not below 0: [tl, tr, br, bl]")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The mode `p` gives as `arcMode`, or `None` when `p` leaves it out or gives null. Any other
|
||||
/// value than one of [`ARC_MODES`] (or `slice` or `open`) is an error.
|
||||
fn arc_mode(p: &Value, cmd: &str) -> Result<Option<shapes::EllipseMode>> {
|
||||
let key = "arcMode";
|
||||
match p.get(key) {
|
||||
None | Some(Value::Null) => Ok(None),
|
||||
Some(v) => v.as_str().and_then(parse_mode).map(Some).ok_or_else(|| bad(cmd, format!("`{key}` must be one of {}", ARC_MODES.join(", ")))),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_arc_params(p: &Value, cmd: &str) -> Result<((f64, f64), shapes::EllipseMode)> {
|
||||
let start = f64_or(p, "startAngle", 0.0);
|
||||
let end = f64_or(p, "endAngle", 360.0);
|
||||
let mode = match str_param(p, "arcMode") {
|
||||
Some("pie" | "slice") => shapes::EllipseMode::Slice,
|
||||
Some("chord") => shapes::EllipseMode::Chord,
|
||||
Some("arc" | "open") => shapes::EllipseMode::Arc,
|
||||
_ => shapes::EllipseMode::Whole,
|
||||
};
|
||||
let mode = arc_mode(p, cmd)?.unwrap_or(shapes::EllipseMode::Whole);
|
||||
let span = (end - start).rem_euclid(360.0);
|
||||
let pie = if mode == shapes::EllipseMode::Whole && (span < 1e-6 || (span - 360.0).abs() < 1e-6) { (0.0, 360.0) } else { (start, end) };
|
||||
(pie, mode)
|
||||
Ok((pie, mode))
|
||||
}
|
||||
|
||||
fn rect_of(p: &Value, c: &str) -> Result<Rect> {
|
||||
let x = f64_req(p, "x", c)?;
|
||||
let y = f64_req(p, "y", c)?;
|
||||
let w = f64_req(p, "width", c)?;
|
||||
let h = f64_req(p, "height", c)?;
|
||||
Ok(Rect::new(x, y, x + w.abs(), y + h.abs()).abs())
|
||||
let w = size(p, "width", c)?;
|
||||
let h = size(p, "height", c)?;
|
||||
Ok(Rect::new(x, y, x + w, y + h))
|
||||
}
|
||||
|
||||
fn rect_xf(r: Rect, rot: f64) -> Affine {
|
||||
@@ -141,18 +222,9 @@ fn rectangle(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let c = "addon.shapes.rectangle";
|
||||
check_numbers(p, &["radius", "startAngle", "endAngle", "rotation"], c)?;
|
||||
let r = rect_of(p, c)?;
|
||||
let radius = f64_or(p, "radius", 0.0).max(0.0);
|
||||
let mut radii = [radius; 4];
|
||||
if let Some(arr) = p.get("radii").and_then(Value::as_array)
|
||||
&& arr.len() == 4
|
||||
{
|
||||
for (i, item) in arr.iter().enumerate() {
|
||||
if let Some(val) = item.as_f64().filter(|n| n.is_finite()) {
|
||||
radii[i] = val.max(0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
let (pie, arc_mode) = parse_arc_params(p);
|
||||
let radius = size_or(p, "radius", 0.0, c)?;
|
||||
let radii = radii_param(p, c)?.unwrap_or([radius; 4]);
|
||||
let (pie, arc_mode) = parse_arc_params(p, c)?;
|
||||
let xf = rect_xf(r, f64_or(p, "rotation", 0.0));
|
||||
let live = LiveShape::Rectangle { w: r.width(), h: r.height(), radii, kinds: Default::default(), radii_relative: false, xf };
|
||||
let cs = CustomShape::Rectangle { w: r.width(), h: r.height(), radii, kinds: Default::default(), radii_relative: false, xf, pie, arc_mode };
|
||||
@@ -164,7 +236,7 @@ fn ellipse(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let c = "addon.shapes.ellipse";
|
||||
check_numbers(p, &["startAngle", "endAngle", "rotation"], c)?;
|
||||
let r = rect_of(p, c)?;
|
||||
let (pie, arc_mode) = parse_arc_params(p);
|
||||
let (pie, arc_mode) = parse_arc_params(p, c)?;
|
||||
let xf = rect_xf(r, f64_or(p, "rotation", 0.0));
|
||||
let live = LiveShape::Ellipse { w: r.width(), h: r.height(), pie, xf };
|
||||
let cs = CustomShape::Ellipse { w: r.width(), h: r.height(), pie, arc_mode, xf };
|
||||
@@ -174,13 +246,13 @@ fn ellipse(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn polygon(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let c = "addon.shapes.polygon";
|
||||
let center = Point::new(f64_req(p, "cx", c)?, f64_req(p, "cy", c)?);
|
||||
let r = f64_req(p, "radius", c)?.abs();
|
||||
let r = size(p, "radius", c)?;
|
||||
check_numbers(p, &["sides", "rotation", "rounded", "randomized", "startAngle", "endAngle"], c)?;
|
||||
let sides = (f64_or(p, "sides", 6.0).round() as i64).clamp(3, 1000) as u32;
|
||||
let sides = count(p, "sides", 6, c)?;
|
||||
let rot = f64_or(p, "rotation", 0.0);
|
||||
let rounded = f64_or(p, "rounded", 0.0).clamp(-10.0, 10.0);
|
||||
let randomized = f64_or(p, "randomized", 0.0).clamp(-10.0, 10.0);
|
||||
let (pie, arc_mode) = parse_arc_params(p);
|
||||
let rounded = ranged(p, "rounded", 0.0, &ROUNDING, c)?;
|
||||
let randomized = ranged(p, "randomized", 0.0, &ROUNDING, c)?;
|
||||
let (pie, arc_mode) = parse_arc_params(p, c)?;
|
||||
let xf = Affine::translate(center.to_vec2()) * vectorcraft_geom::rotate_deg(rot);
|
||||
let live = LiveShape::Polygon { radius: r, sides, xf, radii: vec![], kinds: vec![], radii_relative: false };
|
||||
let cs = CustomShape::Polygon {
|
||||
@@ -202,16 +274,17 @@ fn polygon(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn star(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let c = "addon.shapes.star";
|
||||
let center = Point::new(f64_req(p, "cx", c)?, f64_req(p, "cy", c)?);
|
||||
let r1 = f64_req(p, "radius1", c)?.abs();
|
||||
let r1 = size(p, "radius1", c)?;
|
||||
check_numbers(p, &["radius2", "points", "rotation", "rounded", "randomized", "startAngle", "endAngle"], c)?;
|
||||
let r2 = f64_or(p, "radius2", r1 / 2.0).abs();
|
||||
let points = (f64_or(p, "points", 5.0).round() as i64).clamp(3, 1000) as u32;
|
||||
// The inner radius is a fraction of the outer one, in `SPOKE_RATIO`.
|
||||
let r2 = ranged(p, "radius2", r1 / 2.0, &(r1 * SPOKE_RATIO.start()..=r1 * SPOKE_RATIO.end()), c)?;
|
||||
let points = count(p, "points", 5, c)?;
|
||||
let rot = f64_or(p, "rotation", 0.0);
|
||||
let rounded = f64_or(p, "rounded", 0.0).clamp(-10.0, 10.0);
|
||||
let randomized = f64_or(p, "randomized", 0.0).clamp(-10.0, 10.0);
|
||||
let (pie, arc_mode) = parse_arc_params(p);
|
||||
let rounded = ranged(p, "rounded", 0.0, &ROUNDING, c)?;
|
||||
let randomized = ranged(p, "randomized", 0.0, &ROUNDING, c)?;
|
||||
let (pie, arc_mode) = parse_arc_params(p, c)?;
|
||||
let xf = Affine::translate(center.to_vec2()) * vectorcraft_geom::rotate_deg(rot);
|
||||
let ratio = if r1 > 1e-9 { (r2 / r1).clamp(0.01, 1.0) } else { 0.5 };
|
||||
let ratio = if r1 > 1e-9 { (r2 / r1).clamp(*SPOKE_RATIO.start(), *SPOKE_RATIO.end()) } else { 0.5 };
|
||||
let cs = CustomShape::Polygon {
|
||||
radius: r1,
|
||||
sides: points,
|
||||
@@ -231,12 +304,12 @@ fn star(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn spiral(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let c = "addon.shapes.spiral";
|
||||
let center = Point::new(f64_req(p, "cx", c)?, f64_req(p, "cy", c)?);
|
||||
let r = f64_req(p, "radius", c)?.abs();
|
||||
let r = size(p, "radius", c)?;
|
||||
check_numbers(p, &["turns", "divergence", "innerRadius"], c)?;
|
||||
check_switches(p, &["clockwise"], c)?;
|
||||
let turns = f64_or(p, "turns", 3.0).clamp(0.01, 1024.0);
|
||||
let divergence = f64_or(p, "divergence", 1.0).clamp(0.0, 1000.0);
|
||||
let inner_radius = f64_or(p, "innerRadius", 0.0).clamp(0.0, 0.999);
|
||||
let turns = ranged(p, "turns", 3.0, &TURNS, c)?;
|
||||
let divergence = ranged(p, "divergence", 1.0, &DIVERGENCE, c)?;
|
||||
let inner_radius = ranged(p, "innerRadius", 0.0, &INNER_RADIUS, c)?;
|
||||
let clockwise = bool_or(p, "clockwise", true);
|
||||
let cs = CustomShape::Spiral { center, radius: r, turns, divergence, inner_radius, clockwise };
|
||||
insert_custom(s, "Spiral", None, cs)
|
||||
@@ -299,24 +372,54 @@ fn configure(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
c,
|
||||
)?;
|
||||
check_switches(p, &["isStar", "parallelX", "parallelY", "parallelZ", "reset"], c)?;
|
||||
check_configure(p, c)?;
|
||||
let ids = targets(s, p)?;
|
||||
let reset = bool_or(p, "reset", false);
|
||||
let spiral_hint = p.get("turns").is_some() || p.get("divergence").is_some() || p.get("innerRadius").is_some();
|
||||
// The shapes the params change, worked out first so that a call that changes nothing records
|
||||
// no undo step.
|
||||
let changes: Vec<(NodeId, CustomShape)> = {
|
||||
let doc = &s.doc()?.doc;
|
||||
ids.into_iter()
|
||||
.filter_map(|id| {
|
||||
let n = doc.node(id)?;
|
||||
let before = CustomShape::read_for_update(n, spiral_hint)?;
|
||||
let mut cs = before.clone();
|
||||
let stored = CustomShape::read(n).is_some();
|
||||
(apply_shape_params(&mut cs, p, reset) && (cs != before || !stored)).then_some((id, cs))
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
if changes.is_empty() {
|
||||
return Ok(json!({ "updated": 0 }));
|
||||
}
|
||||
let updated = s.edit("Live Shape", |d, _| {
|
||||
let mut count = 0usize;
|
||||
for id in ids {
|
||||
let Some(n) = d.node_mut(id) else { continue };
|
||||
let Some(mut cs) = CustomShape::read_for_update(n, spiral_hint) else { continue };
|
||||
if apply_shape_params(&mut cs, p, reset) {
|
||||
cs.write_to_node(n);
|
||||
count += 1;
|
||||
}
|
||||
for (id, cs) in &changes {
|
||||
let Some(n) = d.node_mut(*id) else { continue };
|
||||
cs.write_to_node(n);
|
||||
count += 1;
|
||||
}
|
||||
Ok(count)
|
||||
})?;
|
||||
Ok(json!({ "updated": updated }))
|
||||
}
|
||||
|
||||
/// Error unless every bounded value `p` gives is in its range, before any shape changes.
|
||||
fn check_configure(p: &Value, c: &str) -> Result<()> {
|
||||
size_or(p, "radius", 0.0, c)?;
|
||||
radii_param(p, c)?;
|
||||
arc_mode(p, c)?;
|
||||
count(p, "sides", 6, c)?;
|
||||
ranged(p, "spokeRatio", 0.5, &SPOKE_RATIO, c)?;
|
||||
ranged(p, "rounded", 0.0, &ROUNDING, c)?;
|
||||
ranged(p, "randomized", 0.0, &ROUNDING, c)?;
|
||||
ranged(p, "turns", 3.0, &TURNS, c)?;
|
||||
ranged(p, "divergence", 1.0, &DIVERGENCE, c)?;
|
||||
ranged(p, "innerRadius", 0.0, &INNER_RADIUS, c)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn num_param(p: &Value, k: &str) -> Option<f64> {
|
||||
p.get(k).and_then(Value::as_f64).filter(|v| v.is_finite())
|
||||
}
|
||||
@@ -331,8 +434,8 @@ fn apply_arc_params(p: &Value, pie: &mut (f64, f64), arc_mode: &mut shapes::Elli
|
||||
pie.1 = a1.rem_euclid(360.0);
|
||||
changed = true;
|
||||
}
|
||||
if let Some(m) = str_param(p, "arcMode") {
|
||||
*arc_mode = parse_mode(m);
|
||||
if let Some(m) = str_param(p, "arcMode").and_then(parse_mode) {
|
||||
*arc_mode = m;
|
||||
if *arc_mode == shapes::EllipseMode::Whole {
|
||||
*pie = (0.0, 360.0);
|
||||
} else if (pie.1 - pie.0).abs() < 1e-6 || ((pie.1 - pie.0).abs() - 360.0).abs() < 1e-6 {
|
||||
@@ -351,19 +454,14 @@ fn apply_rect_params(p: &Value, reset: bool, max_r: f64, radii: &mut [f64; 4], p
|
||||
*arc_mode = shapes::EllipseMode::Whole;
|
||||
changed = true;
|
||||
}
|
||||
// A radius larger than half the rectangle's shorter side is fitted to that size.
|
||||
if let Some(r) = num_param(p, "radius") {
|
||||
*radii = [r.clamp(0.0, max_r); 4];
|
||||
changed = true;
|
||||
}
|
||||
if let Some(arr) = p.get("radii").and_then(Value::as_array)
|
||||
&& arr.len() == 4
|
||||
{
|
||||
for (i, v) in arr.iter().enumerate() {
|
||||
if let Some(r) = v.as_f64().filter(|x| x.is_finite()) {
|
||||
radii[i] = r.clamp(0.0, max_r);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if let Ok(Some(four)) = radii_param(p, "addon.shapes.configure") {
|
||||
*radii = four.map(|r| r.clamp(0.0, max_r));
|
||||
changed = true;
|
||||
}
|
||||
changed | apply_arc_params(p, pie, arc_mode)
|
||||
}
|
||||
@@ -378,8 +476,9 @@ fn apply_poly_params(p: &Value, reset: bool, sides: &mut u32, spoke_ratio: &mut
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
if let Some(n_sides) = p.get("sides").and_then(Value::as_u64) {
|
||||
*sides = (n_sides as u32).clamp(3, 1000);
|
||||
// `check_configure` has refused values outside their ranges.
|
||||
if let Some(n_sides) = num_param(p, "sides") {
|
||||
*sides = n_sides as u32;
|
||||
changed = true;
|
||||
}
|
||||
if let Some(star) = p.get("isStar").and_then(Value::as_bool) {
|
||||
@@ -387,15 +486,15 @@ fn apply_poly_params(p: &Value, reset: bool, sides: &mut u32, spoke_ratio: &mut
|
||||
changed = true;
|
||||
}
|
||||
if let Some(sr) = num_param(p, "spokeRatio") {
|
||||
*spoke_ratio = Some(sr.clamp(0.01, 1.0));
|
||||
*spoke_ratio = Some(sr);
|
||||
changed = true;
|
||||
}
|
||||
if let Some(rnd) = num_param(p, "rounded") {
|
||||
*rounded = rnd.clamp(-10.0, 10.0);
|
||||
*rounded = rnd;
|
||||
changed = true;
|
||||
}
|
||||
if let Some(rand) = num_param(p, "randomized") {
|
||||
*randomized = rand.clamp(-10.0, 10.0);
|
||||
*randomized = rand;
|
||||
changed = true;
|
||||
}
|
||||
changed
|
||||
@@ -429,15 +528,15 @@ fn apply_shape_params(cs: &mut CustomShape, p: &Value, reset: bool) -> bool {
|
||||
changed = true;
|
||||
}
|
||||
if let Some(t) = num_param(p, "turns") {
|
||||
*turns = t.clamp(0.01, 1024.0);
|
||||
*turns = t;
|
||||
changed = true;
|
||||
}
|
||||
if let Some(div) = num_param(p, "divergence") {
|
||||
*divergence = div.clamp(0.0, 1000.0);
|
||||
*divergence = div;
|
||||
changed = true;
|
||||
}
|
||||
if let Some(ir) = num_param(p, "innerRadius") {
|
||||
*inner_radius = ir.clamp(0.0, 0.999);
|
||||
*inner_radius = ir;
|
||||
changed = true;
|
||||
}
|
||||
changed
|
||||
@@ -470,11 +569,12 @@ fn apply_shape_params(cs: &mut CustomShape, p: &Value, reset: bool) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_mode(m: &str) -> shapes::EllipseMode {
|
||||
fn parse_mode(m: &str) -> Option<shapes::EllipseMode> {
|
||||
match m {
|
||||
"pie" | "slice" => shapes::EllipseMode::Slice,
|
||||
"chord" => shapes::EllipseMode::Chord,
|
||||
"arc" | "open" => shapes::EllipseMode::Arc,
|
||||
_ => shapes::EllipseMode::Whole,
|
||||
"whole" => Some(shapes::EllipseMode::Whole),
|
||||
"pie" | "slice" => Some(shapes::EllipseMode::Slice),
|
||||
"chord" => Some(shapes::EllipseMode::Chord),
|
||||
"arc" | "open" => Some(shapes::EllipseMode::Arc),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -135,3 +135,169 @@ fn addon_shapes_commands_and_format_roundtrip_preserve_paths() {
|
||||
assert_eq!(CustomShape::read(a), CustomShape::read(b));
|
||||
}
|
||||
}
|
||||
|
||||
fn id_of(s: &mut Session, cmd: &str, p: serde_json::Value) -> NodeId {
|
||||
NodeId(s.execute(cmd, &p).unwrap_or_else(|e| panic!("{cmd} {p}: {e}"))["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn shape_of(s: &Session, id: NodeId) -> CustomShape {
|
||||
CustomShape::read(s.doc().unwrap().doc.node(id).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
/// Runs `cmd` with `p`, which must fail with `msg` and leave the document and its history as they
|
||||
/// were.
|
||||
fn refused(s: &mut Session, cmd: &str, p: serde_json::Value, msg: &str) {
|
||||
let saved = vectorcraft_format::save(&s.doc().unwrap().doc, false);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let e = s.execute(cmd, &p).map(|v| v.to_string()).unwrap_err().to_string();
|
||||
assert!(e.contains(msg), "{cmd} {p}: {e}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "{cmd} {p}");
|
||||
assert_eq!(vectorcraft_format::save(&s.doc().unwrap().doc, false), saved, "{cmd} {p}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn polygon_and_star_refuse_counts_that_are_not_whole_numbers_from_3_to_1000() {
|
||||
let mut s = new_session();
|
||||
for n in [json!(2.5), json!(2), json!(0), json!(-6), json!(1000.5), json!(5000), json!(4_294_967_299u64)] {
|
||||
let x = n.as_f64().unwrap();
|
||||
let p = json!({"cx": 0.0, "cy": 0.0, "radius": 10.0, "sides": n});
|
||||
refused(&mut s, "addon.shapes.polygon", p, &format!("`sides` must be a whole number between 3 and 1000 (got {x})"));
|
||||
let p = json!({"cx": 0.0, "cy": 0.0, "radius1": 10.0, "points": n});
|
||||
refused(&mut s, "addon.shapes.star", p, &format!("`points` must be a whole number between 3 and 1000 (got {x})"));
|
||||
}
|
||||
refused(&mut s, "addon.shapes.polygon", json!({"cx": 0.0, "cy": 0.0, "radius": 10.0, "sides": "6"}), "`sides` must be a number");
|
||||
refused(&mut s, "addon.shapes.star", json!({"cx": 0.0, "cy": 0.0, "radius1": 10.0, "points": [5]}), "`points` must be a number");
|
||||
|
||||
// The ends of the range are taken as given, and an omitted or null count is the default.
|
||||
for (n, want) in [(json!(3), 3), (json!(1000), 1000), (json!(7.0), 7), (json!(null), 6)] {
|
||||
let id = id_of(&mut s, "addon.shapes.polygon", json!({"cx": 0.0, "cy": 0.0, "radius": 10.0, "sides": n}));
|
||||
assert!(matches!(shape_of(&s, id), CustomShape::Polygon { sides, spoke_ratio: None, .. } if sides == want), "{n}");
|
||||
}
|
||||
let id = id_of(&mut s, "addon.shapes.polygon", json!({"cx": 0.0, "cy": 0.0, "radius": 10.0}));
|
||||
assert!(matches!(shape_of(&s, id), CustomShape::Polygon { sides: 6, .. }));
|
||||
for (n, want) in [(json!(3), 3), (json!(1000), 1000), (json!(null), 5)] {
|
||||
let id = id_of(&mut s, "addon.shapes.star", json!({"cx": 0.0, "cy": 0.0, "radius1": 10.0, "points": n}));
|
||||
assert!(matches!(shape_of(&s, id), CustomShape::Polygon { sides, spoke_ratio: Some(_), .. } if sides == want), "{n}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shapes_refuse_numbers_outside_their_ranges() {
|
||||
let mut s = new_session();
|
||||
let poly = json!({"cx": 0.0, "cy": 0.0, "radius": 10.0});
|
||||
let star = json!({"cx": 0.0, "cy": 0.0, "radius1": 10.0});
|
||||
let spiral = json!({"cx": 0.0, "cy": 0.0, "radius": 10.0});
|
||||
let with = |base: &serde_json::Value, key: &str, v: f64| {
|
||||
let mut p = base.clone();
|
||||
p[key] = json!(v);
|
||||
p
|
||||
};
|
||||
for (cmd, base, key, bad, good) in [
|
||||
("addon.shapes.polygon", &poly, "rounded", [-10.5, 10.5], [-10.0, 10.0]),
|
||||
("addon.shapes.polygon", &poly, "randomized", [-11.0, 10.01], [-10.0, 10.0]),
|
||||
("addon.shapes.star", &star, "rounded", [-10.5, 10.5], [-10.0, 10.0]),
|
||||
("addon.shapes.star", &star, "randomized", [-11.0, 10.01], [-10.0, 10.0]),
|
||||
("addon.shapes.star", &star, "radius2", [0.05, 11.0], [0.1, 10.0]),
|
||||
("addon.shapes.spiral", &spiral, "turns", [0.0, 1024.5], [0.01, 1024.0]),
|
||||
("addon.shapes.spiral", &spiral, "divergence", [-0.1, 1000.5], [0.0, 1000.0]),
|
||||
("addon.shapes.spiral", &spiral, "innerRadius", [-0.1, 1.0], [0.0, 0.999]),
|
||||
] {
|
||||
let (min, max) = (good[0], good[1]);
|
||||
for x in bad {
|
||||
refused(&mut s, cmd, with(base, key, x), &format!("`{key}` must be between {min} and {max} (got {x})"));
|
||||
}
|
||||
for x in good {
|
||||
s.execute(cmd, &with(base, key, x)).unwrap_or_else(|e| panic!("{cmd} {key} {x}: {e}"));
|
||||
}
|
||||
}
|
||||
|
||||
// Sizes are not negative.
|
||||
for (cmd, p, key) in [
|
||||
("addon.shapes.polygon", json!({"cx": 0.0, "cy": 0.0, "radius": -10.0}), "radius"),
|
||||
("addon.shapes.star", json!({"cx": 0.0, "cy": 0.0, "radius1": -10.0}), "radius1"),
|
||||
("addon.shapes.spiral", json!({"cx": 0.0, "cy": 0.0, "radius": -10.0}), "radius"),
|
||||
("addon.shapes.rectangle", json!({"x": 0.0, "y": 0.0, "width": -10.0, "height": 10.0}), "width"),
|
||||
("addon.shapes.rectangle", json!({"x": 0.0, "y": 0.0, "width": 10.0, "height": 10.0, "radius": -1.0}), "radius"),
|
||||
("addon.shapes.ellipse", json!({"x": 0.0, "y": 0.0, "width": 10.0, "height": -10.0}), "height"),
|
||||
("addon.shapes.box3d", json!({"x": 0.0, "y": 0.0, "width": -10.0, "height": 10.0}), "width"),
|
||||
] {
|
||||
refused(&mut s, cmd, p, &format!("`{key}` must be a number not below 0"));
|
||||
}
|
||||
|
||||
// Corner radii are four numbers that are not negative.
|
||||
for radii in [json!([1.0, 2.0, 3.0]), json!([1.0, 2.0, 3.0, -4.0]), json!([1.0, 2.0, 3.0, "4"]), json!(4.0)] {
|
||||
let p = json!({"x": 0.0, "y": 0.0, "width": 100.0, "height": 80.0, "radii": radii});
|
||||
refused(&mut s, "addon.shapes.rectangle", p, "`radii` must be four numbers not below 0: [tl, tr, br, bl]");
|
||||
}
|
||||
|
||||
// An arc mode is one of the names the commands take.
|
||||
for mode in [json!("slices"), json!(""), json!(3)] {
|
||||
let p = json!({"x": 0.0, "y": 0.0, "width": 100.0, "height": 80.0, "arcMode": mode});
|
||||
refused(&mut s, "addon.shapes.rectangle", p.clone(), "`arcMode` must be one of whole, pie, chord, arc");
|
||||
refused(&mut s, "addon.shapes.ellipse", p, "`arcMode` must be one of whole, pie, chord, arc");
|
||||
}
|
||||
for mode in ["whole", "pie", "slice", "chord", "arc", "open"] {
|
||||
s.execute("addon.shapes.ellipse", &json!({"x": 0.0, "y": 0.0, "width": 100.0, "height": 80.0, "arcMode": mode})).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn configure_refuses_values_outside_their_ranges_before_changing_any_shape() {
|
||||
let mut s = new_session();
|
||||
let rect = id_of(&mut s, "addon.shapes.rectangle", json!({"x": 0.0, "y": 0.0, "width": 100.0, "height": 80.0, "radius": 4.0}));
|
||||
let poly = id_of(&mut s, "addon.shapes.polygon", json!({"cx": 50.0, "cy": 50.0, "radius": 40.0, "sides": 5}));
|
||||
let spiral = id_of(&mut s, "addon.shapes.spiral", json!({"cx": 60.0, "cy": 60.0, "radius": 45.0}));
|
||||
let ids = json!([rect.0, poly.0, spiral.0]);
|
||||
let cfg = |key: &str, v: serde_json::Value| json!({"ids": ids.clone(), key: v});
|
||||
let c = "addon.shapes.configure";
|
||||
for n in [json!(2.5), json!(2), json!(-1), json!(5000), json!(4_294_967_299u64)] {
|
||||
let x = n.as_f64().unwrap();
|
||||
refused(&mut s, c, cfg("sides", n), &format!("`sides` must be a whole number between 3 and 1000 (got {x})"));
|
||||
}
|
||||
for (key, bad, min, max) in [
|
||||
("spokeRatio", [0.0, 1.5], 0.01, 1.0),
|
||||
("rounded", [-10.5, 11.0], -10.0, 10.0),
|
||||
("randomized", [-11.0, 10.5], -10.0, 10.0),
|
||||
("turns", [0.0, 2000.0], 0.01, 1024.0),
|
||||
("divergence", [-1.0, 1000.5], 0.0, 1000.0),
|
||||
("innerRadius", [-0.5, 1.0], 0.0, 0.999),
|
||||
] {
|
||||
for x in bad {
|
||||
refused(&mut s, c, cfg(key, json!(x)), &format!("`{key}` must be between {min} and {max} (got {x})"));
|
||||
}
|
||||
}
|
||||
refused(&mut s, c, cfg("radius", json!(-1.0)), "`radius` must be a number not below 0");
|
||||
for radii in [json!([1.0, 2.0]), json!([1.0, 2.0, 3.0, -1.0]), json!("4")] {
|
||||
refused(&mut s, c, cfg("radii", radii), "`radii` must be four numbers not below 0: [tl, tr, br, bl]");
|
||||
}
|
||||
refused(&mut s, c, cfg("arcMode", json!("bogus")), "`arcMode` must be one of whole, pie, chord, arc");
|
||||
// A valid value next to a refused one changes no shape either.
|
||||
refused(&mut s, c, json!({"ids": ids.clone(), "sides": 8, "turns": 0.0}), "`turns` must be between 0.01 and 1024 (got 0)");
|
||||
|
||||
// In range, the values are taken as given; a radius larger than the rectangle takes is fitted to it.
|
||||
s.execute(c, &json!({"ids": [poly.0], "sides": 1000, "rounded": -10.0, "randomized": 10.0})).unwrap();
|
||||
assert!(matches!(shape_of(&s, poly), CustomShape::Polygon { sides: 1000, rounded, randomized, .. } if rounded == -10.0 && randomized == 10.0));
|
||||
s.execute(c, &json!({"ids": [poly.0], "sides": 3})).unwrap();
|
||||
assert!(matches!(shape_of(&s, poly), CustomShape::Polygon { sides: 3, .. }));
|
||||
s.execute(c, &json!({"ids": [spiral.0], "turns": 1024.0, "divergence": 0.0, "innerRadius": 0.999})).unwrap();
|
||||
assert!(
|
||||
matches!(shape_of(&s, spiral), CustomShape::Spiral { turns, divergence, inner_radius, .. } if turns == 1024.0 && divergence == 0.0 && inner_radius == 0.999)
|
||||
);
|
||||
s.execute(c, &json!({"ids": [rect.0], "radius": 1000.0})).unwrap();
|
||||
assert!(matches!(shape_of(&s, rect), CustomShape::Rectangle { radii, .. } if radii == [40.0; 4]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn configure_records_no_undo_step_when_nothing_changes() {
|
||||
let mut s = new_session();
|
||||
let poly = id_of(&mut s, "addon.shapes.polygon", json!({"cx": 50.0, "cy": 50.0, "radius": 40.0, "sides": 5, "rounded": 0.5}));
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let r = s.execute("addon.shapes.configure", &json!({"ids": [poly.0], "sides": 5, "rounded": 0.5})).unwrap();
|
||||
assert_eq!(r["updated"], 0);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
|
||||
let r = s.execute("addon.shapes.configure", &json!({"ids": [poly.0], "sides": 6})).unwrap();
|
||||
assert_eq!(r["updated"], 1);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);
|
||||
assert!(matches!(shape_of(&s, poly), CustomShape::Polygon { sides: 6, .. }));
|
||||
}
|
||||
@@ -36,7 +36,7 @@ use super::paint::paint_from;
|
||||
use super::{AppearanceAttrs, EyedropperAttrs, FillAttrs, StrokeAttrs};
|
||||
use super::{
|
||||
CommandSpec, NodeId, Result, Session, bad, check_numbers, check_switches, cmd, has_doc, has_selection, ids_param, index, leaf_targets,
|
||||
object_param, ok, paint_targets, str_param, switch_or, targets,
|
||||
object_param, ok, paint_targets, str_param, switch_or, targets, u64_req,
|
||||
};
|
||||
use crate::EngineError;
|
||||
|
||||
@@ -604,9 +604,9 @@ pub(crate) fn edits_stroke(s: &Session, p: &Value, item: ItemTarget, default: bo
|
||||
Ok(ids.get(item.anchor(s, &ids)).and_then(|id| d.node(*id)?.appearance.items.get(index)).map_or(default, |it| !it.is_fill()))
|
||||
}
|
||||
|
||||
/// The whole number of 0 or more that `key` holds, read with [`index`].
|
||||
/// The whole number of 0 or more that `key` holds, read with [`u64_req`] and [`index`].
|
||||
pub(crate) fn index_param(p: &Value, key: &str, cmd: &str) -> Result<usize> {
|
||||
p.get(key).and_then(Value::as_u64).map(index).ok_or_else(|| bad(cmd, format!("missing integer `{key}`")))
|
||||
u64_req(p, key, cmd).map(index)
|
||||
}
|
||||
|
||||
fn set_active_item(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -53,7 +53,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Export Assets",
|
||||
[],
|
||||
None,
|
||||
"{assets?: [asset id…] (default all but the 3D assets, which assets.export3D writes), folder?, zip?, openLocation?, formats?, preset?, settings?, prefix?, subfolders?, antiAlias?} each asset's art alone, cropped to it, in every format row: document.exportForScreens {assets} at the document's export settings (document.exportSettings) under the ones given; with subfolders false every file goes into the folder itself, mobile and density rows too → what document.exportForScreens returns (no folder: the files, or one zip, as dataBase64). Leaves the remembered settings as they are (assets.settings.set changes them)",
|
||||
"{assets?: [asset id…] (default all but the 3D assets, which assets.export3D writes), folder?, zip?, openLocation?, formats?, preset?, settings?, prefix?, subfolders?, antiAlias?} each asset's art alone, cropped to it, in every format row: document.exportForScreens {assets} at the document's export settings (document.exportSettings) under the ones given; with subfolders false every file goes into the folder itself, mobile and density rows too → what document.exportForScreens returns (no folder: the files, or one zip, as dataBase64; warnings name a key in the rows or settings given that only another format takes, not one in the remembered settings). Leaves the remembered settings as they are (assets.settings.set changes them)",
|
||||
has_doc,
|
||||
export
|
||||
),
|
||||
@@ -429,7 +429,7 @@ fn export(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
q.insert("assets".into(), json!(all));
|
||||
}
|
||||
export_screens(s, &Value::Object(q)).map_err(|e| of_cmd(C, e))
|
||||
export_screens(s, &Value::Object(q), p).map_err(|e| of_cmd(C, e))
|
||||
}
|
||||
|
||||
fn settings_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -226,7 +226,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Break Link to Symbol",
|
||||
[],
|
||||
None,
|
||||
"{ids?|id?} expand selected instances into plain art (with their appearance changes to a dynamic symbol), placed through the symbol's 9-slice guides when it has them, with the instance's opacity mask; a hidden or locked instance's art stays hidden or locked and is not selected → {ids}",
|
||||
"{ids?|id?} expand selected instances into plain art (with their appearance changes to a dynamic symbol), placed through the symbol's 9-slice guides when it has them, with the instance's opacity mask and its own bounding box (square after Reset Bounding Box); a hidden or locked instance's art stays hidden or locked and is not selected → {ids}",
|
||||
has_selection,
|
||||
symbol_break_link
|
||||
),
|
||||
|
||||
@@ -393,8 +393,10 @@ pub(super) fn symbol_place(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
/// The plain art of instance `inst` in document space (fresh ids), with the instance's own
|
||||
/// appearance changes to a dynamic symbol's art when `own`, placed through its symbol's 9-slice
|
||||
/// guides when it has them. The instance's opacity mask goes on the art, or on a group around it
|
||||
/// when the art has a mask of its own.
|
||||
/// guides when it has them. The art's bounding boxes turn with the instance's own box, so after
|
||||
/// Reset Bounding Box they stand as the instance's box does. The instance's opacity mask goes on
|
||||
/// the art, or on a group around it when the art has a mask of its own; a group added around the
|
||||
/// art takes the instance's box angle.
|
||||
fn expanded_instance(d: &mut Document, inst: &Node, own: bool) -> Option<Node> {
|
||||
let NodeKind::SymbolInstance { symbol, xf } = &inst.kind else { return None };
|
||||
let sym = d.symbols.iter().find(|s| &s.name == symbol)?;
|
||||
@@ -402,12 +404,17 @@ fn expanded_instance(d: &mut Document, inst: &Node, own: bool) -> Option<Node> {
|
||||
if let Some((color, amount)) = brush::stain(inst) {
|
||||
brush::tint_node(&mut art, &color, amount);
|
||||
}
|
||||
let art = d.place_symbol_art(symbol, &art, *xf)?;
|
||||
let mut art = d.place_symbol_art(symbol, &art, *xf)?;
|
||||
let turn = vectorcraft_doc::orient::placed_turn(inst.bbox_angle, *xf);
|
||||
if turn != 0.0 {
|
||||
art.turn_boxes(turn);
|
||||
}
|
||||
let mut art = d.reid(&art);
|
||||
if inst.opacity < 1.0 || inst.blend != vectorcraft_color::BlendMode::Normal {
|
||||
if !matches!(art.kind, NodeKind::Group { .. }) {
|
||||
let id = d.alloc_id();
|
||||
art = Node::group(id, vec![Arc::new(art)]);
|
||||
art.bbox_angle = inst.bbox_angle;
|
||||
}
|
||||
art.opacity *= inst.opacity;
|
||||
art.blend = inst.blend;
|
||||
@@ -416,6 +423,7 @@ fn expanded_instance(d: &mut Document, inst: &Node, own: bool) -> Option<Node> {
|
||||
if art.extras().mask.is_some() {
|
||||
let id = d.alloc_id();
|
||||
art = Node::group(id, vec![Arc::new(art)]);
|
||||
art.bbox_angle = inst.bbox_angle;
|
||||
}
|
||||
art.extras_mut().mask = Some(mask.clone());
|
||||
}
|
||||
|
||||
@@ -231,9 +231,9 @@ pub(super) fn within_input_bound(q: Point) -> bool {
|
||||
q.x.is_finite() && q.y.is_finite() && q.x.abs() < MAX_INPUT && q.y.abs() < MAX_INPUT
|
||||
}
|
||||
|
||||
/// The whole number of 0 or more that `key` holds, read with [`index`].
|
||||
/// The whole number of 0 or more that `key` holds, read with [`u64_req`](crate::cmd::u64_req) and [`index`].
|
||||
pub(super) fn usize_req(p: &Value, key: &str, cmd: &str) -> Result<usize> {
|
||||
p.get(key).and_then(Value::as_u64).map(index).ok_or_else(|| bad(cmd, format!("missing integer `{key}`")))
|
||||
crate::cmd::u64_req(p, key, cmd).map(index)
|
||||
}
|
||||
|
||||
/// The whole number of 0 or more that `key` holds, or `None` when `key` is absent or null. Any other
|
||||
|
||||
@@ -11,7 +11,9 @@ use super::{
|
||||
// ---------- Pencil / Paintbrush ----------
|
||||
|
||||
/// Fit a freehand polyline: drop jitter, build a spline through the samples (sharp turns stay
|
||||
/// corners) and simplify it to `tol`.
|
||||
/// corners) and simplify it to `tol`. A closed stroke is fitted as the open one is and closed by a
|
||||
/// straight segment from its end back to its start: fitting round the gap would curve that segment
|
||||
/// on from both ends, past them and back.
|
||||
fn fit_freehand(pts: &[Point], tol: f64, closed: bool) -> SubPath {
|
||||
let min = (tol * 0.3).max(vectorcraft_tools::draw2::MIN_FIT_TOLERANCE * 0.3);
|
||||
let (Some(&p0), Some(&pn)) = (pts.first(), pts.last()) else { return SubPath::new(vec![], closed) };
|
||||
@@ -41,15 +43,26 @@ fn fit_freehand(pts: &[Point], tol: f64, closed: bool) -> SubPath {
|
||||
(kept[i], corner)
|
||||
})
|
||||
.collect();
|
||||
let sp = vectorcraft_tools::draw2::catmull_rom(&marked, closed);
|
||||
let sp = vectorcraft_tools::draw2::catmull_rom(&marked, false);
|
||||
if sp.anchors.len() < 3 {
|
||||
return sp;
|
||||
// Two anchors closed would run back over the line itself.
|
||||
return SubPath::new(sp.anchors, closed && n > 2);
|
||||
}
|
||||
let fitted = po::simplify_with(
|
||||
&PathData::single(sp.clone()),
|
||||
&SimplifyOptions { tolerance: tol, corner_angle_deg: 60.0, straight_lines: false, ..Default::default() },
|
||||
);
|
||||
fitted.subpaths.into_iter().next().filter(|s| s.anchors.len() >= 2).unwrap_or(sp)
|
||||
let mut anchors = fitted.subpaths.into_iter().next().filter(|s| s.anchors.len() >= 2).unwrap_or(sp).anchors;
|
||||
if closed {
|
||||
// No handle on either side of the closing segment.
|
||||
if let Some(l) = anchors.last_mut() {
|
||||
l.h_out = l.p;
|
||||
}
|
||||
if let Some(f) = anchors.first_mut() {
|
||||
f.h_in = f.p;
|
||||
}
|
||||
}
|
||||
SubPath::new(anchors, closed)
|
||||
}
|
||||
|
||||
/// The path of a freehand stroke: fitted at `tol` ([`fit_freehand`]), or with no `tol` the points
|
||||
|
||||
@@ -11,7 +11,7 @@ use super::clipboard::SwatchChoices;
|
||||
use super::{
|
||||
CommandSpec, Result, Session, always, bad, can_clear_history, can_redo, can_undo, check_numbers, checked_ids_param, clock_date, cmd, date_param,
|
||||
f64_req, finite_numbers, has_clipboard, has_doc, has_selection, has_selection_guides_or_artboard_tool, has_selection_or_guides, index, number_or,
|
||||
object_param, ok, point_param, str_param, switch_or,
|
||||
object_param, ok, point_param, str_param, switch_or, u64_req,
|
||||
};
|
||||
use crate::{Clipboard, EngineError, SourceLayer, inspect};
|
||||
|
||||
@@ -249,7 +249,10 @@ fn file_close(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
|
||||
fn doc_activate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let i = p.get("index").and_then(Value::as_u64).map(index).ok_or_else(|| bad("document.activate", "missing index"))?;
|
||||
let i = match p.get("index") {
|
||||
None | Some(Value::Null) => return Err(bad("document.activate", "missing index")),
|
||||
Some(v) => v.as_u64().map(index).ok_or_else(|| bad("document.activate", "`index` must be a whole number"))?,
|
||||
};
|
||||
if s.set_active(i) { ok() } else { Err(bad("document.activate", "no such document")) }
|
||||
}
|
||||
|
||||
@@ -262,7 +265,10 @@ fn doc_move(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if from >= n {
|
||||
return Err(bad("document.move", "no such document"));
|
||||
}
|
||||
let to = p.get("to").and_then(Value::as_u64).map(index).ok_or_else(|| bad("document.move", "missing `to`: a non-negative integer"))?;
|
||||
let to = match p.get("to") {
|
||||
None | Some(Value::Null) => return Err(bad("document.move", "missing `to`: a non-negative integer")),
|
||||
Some(v) => v.as_u64().map(index).ok_or_else(|| bad("document.move", "`to` must be a whole number"))?,
|
||||
};
|
||||
let to = to.min(n.saturating_sub(1));
|
||||
if !s.move_document(from, to) {
|
||||
return Err(bad("document.move", "no such document"));
|
||||
@@ -1190,8 +1196,7 @@ fn clear(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// Document ids scope node ids: two tabs may contain objects with the same node id.
|
||||
fn drop_selection(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const CMD: &str = "edit.dropSelection";
|
||||
let key = "sourceDocument";
|
||||
let uid = p[key].as_u64().ok_or_else(|| bad(CMD, format!("missing integer `{key}`")))?;
|
||||
let uid = u64_req(p, "sourceDocument", CMD)?;
|
||||
let point = |key: &str| finite_numbers::<2>(&p[key]).ok_or_else(|| bad(CMD, format!("{key} must be [x, y] with finite coordinates")));
|
||||
let grab = point("grab")?;
|
||||
let at = point("at")?;
|
||||
|
||||
@@ -92,7 +92,7 @@ pub use pdfimport::{LoadOptions, page_document};
|
||||
pub(crate) use save::job_for;
|
||||
pub(crate) use save::save as save_document;
|
||||
pub use save::{
|
||||
SAVE_FORMATS, SaveJob, SaveMode, SavePlan, StreamWrite, TEMPLATE_FORMATS, dialog_folder, documents_dialog_folder, export_folder,
|
||||
SAVE_FORMATS, SaveJob, SaveMode, SavePlan, StreamWrite, TEMPLATE_FORMATS, dialog_folder, documents_dialog_folder, export_folder, file_reads,
|
||||
leave_out_unreadable_save_paths, reads_option as save_reads_option, remembered_save_options, save_filters, save_format, save_job, save_plan,
|
||||
save_with, stamp_save_dates, template_filters, templates_dialog_folder, templates_folder,
|
||||
};
|
||||
|
||||
@@ -54,7 +54,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Export for Screens",
|
||||
["File", "Export"],
|
||||
None,
|
||||
"{folder?, zip?: false (one .zip of every file, each deflated or stored, whichever is smaller: no folder → {name, dataBase64, bytes, files: [name…]}; with a folder it is written there → {path, bytes, files}), artboard?: index | artboards?: [index…] | range?: \"1-3\" (default all), assets?: [asset id…] (instead of artboards: each asset's art alone, cropped to its bounds and named after the asset; see assets.list), layers?: [layer id…] (instead of artboards: each layer's art alone, sublayers included, named after the layer), selection?: one|each (instead of artboards: the selected art as one file, or a file per selected object, named after it), fullDocument?: false (one file per format instead: a PDF of every artboard, other formats the bounds of all visible art, named after the document), includeBleed?: false (artboards grown by the document's bleed), includeArtboardFill?: true (Include Artboard Fill: each artboard's background color behind its art; false leaves it out), pdfs?: multiple|single (Export PDFs As: with several artboards, single writes them as the pages of one PDF per PDF row, named after the document), exportPreset?: (a preset of document.exportPresets.list: its settings, under these params), subfolders?: false (each row's files in a sub-folder: its folder, else its size for raster (1x, 2x, 100w…) or its format (SVG, PDF)), folders?: none|rows (as subfolders)|format (a folder per extension: png, svg, pdf…)|item (a folder per artboard, asset, layer or object)|itemFormat (item/extension) (default: rows with subfolders or a preset, else none), name?: (a file name pattern instead of the item's name, with <filename> for the document, <name> for the artboard, asset, layer or object, <index> from 1, #### for the counter zero-padded to the run of #, <format>, <scale>, <suffix> (else the row's suffix ends the name), YYYYMMDD, YYYY-MM-DD, YYYY, <date>, <time>, <YYYY>, <MM>, <DD>, <hh>, <mm> and <ss>; a trailing .ext is dropped, as in `COMPANY-Logo-<filename>-####-YYYYMMDD`; different files it names alike get -2, -3…), counterStart?: 1, counterPer?: item (every file of an item has its number)|file, date?: YYYY-MM-DD (the date the pattern uses; default today, UTC), preset?: mobile (PNG 1x, 2x, 3x) | density (PNG 0.75x–4x in ldpi…xxxhdpi sub-folders) instead of formats (turns subfolders on), formats?: [{format: png|png8|jpg|webp|gif|tiff|bmp|tga|psd|svg|svgz|pdf|eps|ai|vectorcraft, scale?: 1 | \"2x\" | \"100w\" | \"100h\" | \"72ppi\", width?: px | height?: px | ppi? (raster only; width, then height, then ppi win over scale), suffix?: (raster default: @2x, @100w, @100h, none at 1x; vector formats drop size suffixes), folder?: (its sub-folder), quality?: (jpg 0–100), …the format's options (antiAlias, background, preset (pdf)…)}], settings?: {png|png8|jpg|webp|gif|tiff|bmp|tga|psd|svg|svgz|pdf|eps|ai|vectorcraft: {…options for every row of that format}} (rows' own options win), prefix?, antiAlias?: none|art|type (raster rows without their own), openLocation?: (remembered; the app shows the files after exporting)} one file per artboard and format (a PDF holds its artboard alone; artboards with the same name, in any case, get -2, -3…; an unnamed one is Artboard-N; a row whose file would take a name another row's file has, in any case, gets -2, -3… too, and identical rows write one file; row folders that differ only in case are one folder, spelled as the first row spells it) → {files: [path…]} (into the folder of exportPreset, outside the automation read roots: no ZIP path, and files relative to that folder); no folder → {files: [{name, dataBase64}]} (names include sub-folders: 2x/Icon@2x.png); every reply has warnings: [the encoders' warnings for the files, each once: an image that couldn't be decoded, type written in the fallback font…]. eps, ai and vectorcraft rows write a document of the item alone (its one artboard). The params (but zip and assets) are remembered in the document (document.exportSettings; not an undo step). A key it doesn't take, a key no format takes in a format row or in settings, settings for a format it doesn't write, and an unknown key inside a PDF, EPS or SVG option's object, are refused. Under automation read roots, an svg or svgz row is refused, before the folder is made or any file is written, while the document links a JavaScript file outside them, as document.export refuses it",
|
||||
"{folder?, zip?: false (one .zip of every file, each deflated or stored, whichever is smaller: no folder → {name, dataBase64, bytes, files: [name…]}; with a folder it is written there → {path, bytes, files}), artboard?: index | artboards?: [index…] | range?: \"1-3\" (default all), assets?: [asset id…] (instead of artboards: each asset's art alone, cropped to its bounds and named after the asset; see assets.list), layers?: [layer id…] (instead of artboards: each layer's art alone, sublayers included, named after the layer), selection?: one|each (instead of artboards: the selected art as one file, or a file per selected object, named after it), fullDocument?: false (one file per format instead: a PDF of every artboard, other formats the bounds of all visible art, named after the document), includeBleed?: false (artboards grown by the document's bleed), includeArtboardFill?: true (Include Artboard Fill: each artboard's background color behind its art; false leaves it out), pdfs?: multiple|single (Export PDFs As: with several artboards, single writes them as the pages of one PDF per PDF row, named after the document), exportPreset?: (a preset of document.exportPresets.list: its settings, under these params), subfolders?: false (each row's files in a sub-folder: its folder, else its size for raster (1x, 2x, 100w…) or its format (SVG, PDF)), folders?: none|rows (as subfolders)|format (a folder per extension: png, svg, pdf…)|item (a folder per artboard, asset, layer or object)|itemFormat (item/extension) (default: rows with subfolders or a preset, else none), name?: (a file name pattern instead of the item's name, with <filename> for the document, <name> for the artboard, asset, layer or object, <index> from 1, #### for the counter zero-padded to the run of #, <format>, <scale>, <suffix> (else the row's suffix ends the name), YYYYMMDD, YYYY-MM-DD, YYYY, <date>, <time>, <YYYY>, <MM>, <DD>, <hh>, <mm> and <ss>; a trailing .ext is dropped, as in `COMPANY-Logo-<filename>-####-YYYYMMDD`; different files it names alike get -2, -3…), counterStart?: 1, counterPer?: item (every file of an item has its number)|file, date?: YYYY-MM-DD (the date the pattern uses; default today, UTC), preset?: mobile (PNG 1x, 2x, 3x) | density (PNG 0.75x–4x in ldpi…xxxhdpi sub-folders) instead of formats (turns subfolders on), formats?: [{format: png|png8|jpg|webp|gif|tiff|bmp|tga|psd|svg|svgz|pdf|eps|ai|vectorcraft, scale?: 1 | \"2x\" | \"100w\" | \"100h\" | \"72ppi\", width?: px | height?: px | ppi? (raster only; width, then height, then ppi win over scale), suffix?: (raster default: @2x, @100w, @100h, none at 1x; vector formats drop size suffixes), folder?: (its sub-folder), quality?: (jpg 0–100), …the format's options (antiAlias, background, preset (pdf)…)}], settings?: {png|png8|jpg|webp|gif|tiff|bmp|tga|psd|svg|svgz|pdf|eps|ai|vectorcraft: {…options for every row of that format}} (rows' own options win), prefix?, antiAlias?: none|art|type (raster rows without their own), openLocation?: (remembered; the app shows the files after exporting)} one file per artboard and format (a PDF holds its artboard alone; artboards with the same name, in any case, get -2, -3…; an unnamed one is Artboard-N; a row whose file would take a name another row's file has, in any case, gets -2, -3… too, and identical rows write one file; row folders that differ only in case are one folder, spelled as the first row spells it) → {files: [path…]} (into the folder of exportPreset, outside the automation read roots: no ZIP path, and files relative to that folder); no folder → {files: [{name, dataBase64}]} (names include sub-folders: 2x/Icon@2x.png); every reply has warnings: [the encoders' warnings for the files, each once: an image that couldn't be decoded, type written in the fallback font…]. eps, ai and vectorcraft rows write a document of the item alone (its one artboard). The params (but zip and assets) are remembered in the document (document.exportSettings; not an undo step). A key it doesn't take, a key no format takes in a format row or in settings, settings for a format it doesn't write, and an unknown key inside a PDF, EPS or SVG option's object, are refused; a key only another format takes in a row or in settings is not used, and warnings name it as given (formats[0].quality; a key in settings only when no row of that format reads it; not a key of an exportPreset). Under automation read roots, an svg or svgz row is refused, before the folder is made or any file is written, while the document links a JavaScript file outside them, as document.export refuses it",
|
||||
has_doc,
|
||||
screens::export_for_screens
|
||||
),
|
||||
|
||||
@@ -32,26 +32,25 @@ pub fn unused_option_notes(cmd: &str, f: &Format, p: &Value) -> Vec<String> {
|
||||
}
|
||||
let listed = |k: &str| super::FORMATS.iter().filter(|f| f.write).flat_map(|f| f.options).any(|o| o.name == k);
|
||||
let written = super::save::written_as(cmd, f, p);
|
||||
let legacy = f.id == "ai" && super::legacy_ai::wanted(cmd, f, p).unwrap_or(false);
|
||||
let reads = |k: &str| match (f.id, k) {
|
||||
// A legacy `.ai` file is not a PDF: it reads its own options only.
|
||||
("ai", _) if legacy => super::legacy_ai::reads(k),
|
||||
// The other `.ai` files don't read the legacy file's options, and `preserveEditing` is
|
||||
// on for them whatever is given.
|
||||
("ai", "preserveEditing") => false,
|
||||
("ai", _) if super::legacy_ai::LEGACY_ONLY.contains(&k) => false,
|
||||
// An export writes one file: only a save writes each artboard to a file of its own, and
|
||||
// a native file's `range` names those files.
|
||||
("vectorcraft" | "template" | "ai", "separateArtboards") | ("vectorcraft" | "template", "range") => false,
|
||||
_ => super::save::reads_option(f, k),
|
||||
};
|
||||
let legacy = super::save::legacy_file(cmd, f, p);
|
||||
given
|
||||
.iter()
|
||||
.filter(|(k, v)| !v.is_null() && !EXPORT_PARAMS.contains(&k.as_str()) && listed(k) && !reads(k))
|
||||
.filter(|(k, v)| !v.is_null() && !EXPORT_PARAMS.contains(&k.as_str()) && listed(k) && !export_reads(f, legacy, k))
|
||||
.map(|(k, _)| super::save::unused_option_note(k, &written))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Does the file an export writes in `f` read option `key`, `legacy` when it is a legacy `.ai`
|
||||
/// file? As a save's file does ([`super::save::file_reads`]), except that an export writes one
|
||||
/// file: only a save writes each artboard to a file of its own, and a native file's `range` names
|
||||
/// those files.
|
||||
pub(super) fn export_reads(f: &Format, legacy: bool, key: &str) -> bool {
|
||||
match (f.id, key) {
|
||||
("vectorcraft" | "template" | "ai", "separateArtboards") | ("vectorcraft" | "template", "range") => false,
|
||||
_ => super::save::file_reads(f, legacy, key),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn serialize(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let f = writable("document.serialize", Some(text_param(p, "format", "document.serialize")?.unwrap_or("vectorcraft")), None)?;
|
||||
check_linked_scripts("document.serialize", f.id, s.doc()?)?;
|
||||
|
||||
@@ -95,14 +95,18 @@ pub struct SavePlan {
|
||||
/// The `modified` param (as `date_param` reads it): the File Info date a Save or Save As to a file
|
||||
/// stamps; `None`: the time it is written.
|
||||
pub modified: Option<Option<i64>>,
|
||||
/// Warnings about the params themselves: each native option (`compress`, `version`,
|
||||
/// `preview`, `precision`) given beside the path, and each key of `options`, that the format
|
||||
/// doesn't read, naming the file the save writes.
|
||||
/// Warnings about the params themselves: each option a save format takes given beside the
|
||||
/// path, and each key of `options`, that the file the save writes doesn't read
|
||||
/// ([`file_reads`]), naming that file.
|
||||
pub notes: Vec<String>,
|
||||
/// The file the document was read from and what reading it left out, when the save needs
|
||||
/// `acknowledgeLoss` to write over it and was not given it: every file the save writes is
|
||||
/// checked against it before the first is written.
|
||||
pub lossy_source: Option<(String, Vec<String>)>,
|
||||
/// The `.ai` options the file doesn't read, with the values the document remembers from its
|
||||
/// last `.ai` save: not in `options`, they go back into the remembered options once the file
|
||||
/// is the document's own.
|
||||
pub kept: Map<String, Value>,
|
||||
}
|
||||
|
||||
impl SavePlan {
|
||||
@@ -279,7 +283,8 @@ pub fn save_plan(s: &Session, mode: SaveMode, p: &Value) -> Result<SavePlan> {
|
||||
}
|
||||
}
|
||||
// None given: the ones the document was last saved with in this format.
|
||||
let mut options = if given.is_empty() && format.id == st.format { remembered_save_options(st) } else { given };
|
||||
let from_memory = given.is_empty() && format.id == st.format;
|
||||
let mut options = if from_memory { remembered_save_options(st) } else { given };
|
||||
// Remember the effective choice, including the encoder's default, so another document's
|
||||
// later save cannot change what this one writes. Invalid non-null values still reach validation.
|
||||
if matches!(format.id, "vectorcraft" | "template") {
|
||||
@@ -311,21 +316,54 @@ pub fn save_plan(s: &Session, mode: SaveMode, p: &Value) -> Result<SavePlan> {
|
||||
let given = str_param(p, "path").is_some_and(|g| !g.is_empty());
|
||||
super::check_not_lossy_overwrite(st, path, p, cmd).map_err(|e| if given { e } else { super::access::naming_in_error(e, path) })?;
|
||||
}
|
||||
let written = written_as(cmd, format, &Value::Object(options.clone()));
|
||||
let top = NATIVE_OPTIONS.iter().filter(|k| p.get(**k).is_some_and(|v| !v.is_null())).map(|k| k.to_string());
|
||||
let chosen = Value::Object(options.clone());
|
||||
let written = written_as(cmd, format, &chosen);
|
||||
let legacy = legacy_file(cmd, format, &chosen);
|
||||
// Beside the path, a key some save format takes; in `options`, any key.
|
||||
let top = p.as_object().into_iter().flatten().filter(|(k, v)| !v.is_null() && save_listed(k)).map(|(k, _)| k.to_string());
|
||||
let nested = p.get("options").and_then(Value::as_object).into_iter().flatten().filter(|(_, v)| !v.is_null()).map(|(k, _)| k);
|
||||
let notes = top
|
||||
.filter(|k| !reads_option(format, k))
|
||||
.chain(nested.filter(|k| !reads_option(format, k)).map(|k| format!("options.{k}")))
|
||||
.filter(|k| !file_reads(format, legacy, k))
|
||||
.chain(nested.filter(|k| !file_reads(format, legacy, k)).map(|k| format!("options.{k}")))
|
||||
.map(|k| unused_option_note(&k, &written))
|
||||
.collect();
|
||||
// When the document's own file is a `.ai`, the `.ai` options this file doesn't read and the
|
||||
// request doesn't give keep their remembered values ([`SavePlan::kept`]), so that Save Options
|
||||
// still shows them (it sends only the ones the file reads). They are neither written nor sent
|
||||
// again.
|
||||
let mut kept = Map::new();
|
||||
if format.id == "ai" && format.id == st.format {
|
||||
let last = remembered_save_options(st);
|
||||
for o in format.options.iter().filter(|o| !file_reads(format, legacy, o.name)) {
|
||||
let value = if from_memory {
|
||||
options.remove(o.name)
|
||||
} else if options.contains_key(o.name) {
|
||||
None
|
||||
} else {
|
||||
last.get(o.name).cloned()
|
||||
};
|
||||
if let Some(v) = value {
|
||||
kept.insert(o.name.to_string(), v);
|
||||
}
|
||||
}
|
||||
}
|
||||
let lossy_source = super::lossy_source(st, p);
|
||||
Ok(SavePlan { mode, path, format, options, name, folder, modified, notes, lossy_source })
|
||||
Ok(SavePlan { mode, path, format, options, name, folder, modified, notes, lossy_source, kept })
|
||||
}
|
||||
|
||||
/// The native save options, which a save also takes beside the path.
|
||||
const NATIVE_OPTIONS: [&str; 4] = ["compress", "version", "preview", "precision"];
|
||||
|
||||
/// Does some save format (or template format) take option `key`?
|
||||
fn save_listed(key: &str) -> bool {
|
||||
NATIVE_OPTIONS.contains(&key)
|
||||
|| super::FORMATS
|
||||
.iter()
|
||||
.filter(|f| SAVE_FORMATS.contains(&f.id) || TEMPLATE_FORMATS.contains(&f.id))
|
||||
.flat_map(|f| f.options)
|
||||
.any(|o| o.name == key)
|
||||
}
|
||||
|
||||
/// The first option `p` gives both beside the path and in `options` with different values, as
|
||||
/// `(key, top-level value, options value)`. For an SVG save (`svg`), an SVG option's value on
|
||||
/// either side is the one its `svg: {…}` object gives, else the flat one.
|
||||
@@ -467,6 +505,26 @@ pub fn reads_option(f: &Format, key: &str) -> bool {
|
||||
f.options.iter().any(|o| o.name == key) || (is_pdf(f) && (super::pdf::OPTIONS.iter().any(|o| o.name == key) || super::pdf::is_setting(key)))
|
||||
}
|
||||
|
||||
/// Does the file a save or an export writes in `f` read option `key`, `legacy` when it is a legacy
|
||||
/// `.ai` file ([`super::legacy_ai::wanted`])? As [`reads_option`], except for `.ai` and `.ait`
|
||||
/// files: a legacy file reads its own options only ([`super::legacy_ai::reads`]), and the others
|
||||
/// don't read the legacy file's options or `preserveEditing`, which is on for them whatever is
|
||||
/// given.
|
||||
pub fn file_reads(f: &Format, legacy: bool, key: &str) -> bool {
|
||||
match (f.id, key) {
|
||||
("ai", _) if legacy => super::legacy_ai::reads(key),
|
||||
("ai" | "ait", "preserveEditing") => false,
|
||||
("ai" | "ait", _) if super::legacy_ai::LEGACY_ONLY.contains(&key) => false,
|
||||
_ => reads_option(f, key),
|
||||
}
|
||||
}
|
||||
|
||||
/// Is a save or an export of `cmd` in `f` with options `p` to write a legacy `.ai` file? An
|
||||
/// unknown version is not one (the save or export refuses it).
|
||||
pub(super) fn legacy_file(cmd: &str, f: &Format, p: &Value) -> bool {
|
||||
f.id == "ai" && super::legacy_ai::wanted(cmd, f, p).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// The note a save or an export gives for option `key`, as the request names it, which the file it
|
||||
/// writes (`written`, see [`written_as`]) doesn't read.
|
||||
pub(super) fn unused_option_note(key: &str, written: &Format) -> String {
|
||||
@@ -1037,6 +1095,7 @@ impl SaveJob {
|
||||
st.path = Some(path);
|
||||
st.format = self.plan.format.id;
|
||||
st.save_options = self.plan.options;
|
||||
st.save_options.extend(self.plan.kept);
|
||||
// A later save rounds only when it gives `precision` too.
|
||||
st.save_options.remove("precision");
|
||||
st.save_artboards = self.snapshot.artboards.iter().map(|a| a.id).collect();
|
||||
@@ -1239,7 +1298,7 @@ pub(super) fn specs() -> Vec<CommandSpec> {
|
||||
"Save Document",
|
||||
[],
|
||||
None,
|
||||
"{path?, format?: vectorcraft|template|pdf|svg|svgz|ai|eps (default: the path's extension, else the document's own format), options?: {…the format's options, see file.formatOptions; default: as last saved}, svg?: {…SVG options} (an SVG save keeps hidden layers, display:none, unless hiddenLayers is false), …the SVG and PDF options given flat instead (an option given flat or in svg and in options with different values is an error), compress?: bool (native, also in options: gzip; default: the useCompression preference; warnings say when the format doesn't read compress, version, preview or precision given flat, or a key given in options), version?: 4 (3, 2 or 1: for older VectorCraft versions; 1 and 2 are never compressed), preview?: false (embed a PNG of the first artboard, at most 256 px), precision?: 0..9 (native: round path anchors and handles to this many decimal places, a smaller file; colors, transforms and type stay exact; default: exact, and a later save without it writes exactly again), separateArtboards?: false (native and .ai: also save each artboard of `range` to <name>-<artboard>.<ext> beside the file, holding that artboard and the art touching it; the result's files lists every file written), range?: \"1-3, 5\"|\"all\" (default; with separateArtboards), includeLinked?: false (keep linked files' own pixels, not just their previews; they stay linked), embedProfiles?: true (carry the ICC profiles loaded from files that the document is tagged with; opening the file installs them where missing), pdfCompatible?: true (native: also carry a PDF of every artboard; false omits it) | true for .ai (false: blank pages, only the native document; a .ai file's compress, default true, is its compression.compressText), modified?: Unix seconds|null (the File Info modified date, and created date when there is none, a save to a file stamps; default now, recorded in the journal so a replay matches; null: leave the dates)} → {path, format, bytes, warnings, files?: [path…] (the file and its artboards' files), linked?: [path…] (images an SVG links to)}. Save writes one artboard, except a .ai file and EPS (below): a .ai file is a PDF-compatible file of every artboard carrying the native document (preserveEditing always on; PDF options flat or in options), which document.open restores exactly, on the active artboard (the app's, else the one the document opened on; with editingData: false, the first); separateArtboards writes more files, one per artboard. EPS takes the options of document.exportEps: by default one file whose bounding box fits the visible art (the first artboard when no art shows), carrying the whole editable document; useArtboards: true (also when artboard, artboards or range names artboards; file.formatOptions then reports it true) makes the chosen artboard the page, drawn with its background color (without Use Artboards, only the first artboard's page when no art shows has its color), and with two or more chosen the file at the path is the central file of the visible art (again the first artboard when none shows) and each artboard is also written to <name>_<artboard>.eps beside it, holding that artboard; selectedOnly is refused; its PostScript appearance flattens transparency, as EPS requires. Without a path it writes the document's own file in its own format: a document opened from or saved as SVG/PDF/EPS saves as that again (warnings name what the format loses). No path known (never saved, converted from an older version, restored by Data Recovery, or another format) → {dataBase64, format, name, folder?, warnings} and the document stays modified. Under automation read roots, a reply leaves out the document's own file outside them when no path is given (files and linked then name the files by their file names), and a suggested folder outside them; an svg or svgz save (a document whose own format is SVG or SVGZ included) is refused, before any file is written, while the document links a JavaScript file outside them (svgInteractivity; a URL is not checked), also by file.saveAs and file.saveCopy. A key that no save format takes, beside the path or in options, and an unknown key inside a PDF option's object or svg, are refused. A terminal filesystem permission/read-only refusal returns READ_ONLY_LOCATION: followed by the reason; recovery-copy failures keep their original error. Non-UI callers never open a save panel",
|
||||
"{path?, format?: vectorcraft|template|pdf|svg|svgz|ai|eps (default: the path's extension, else the document's own format), options?: {…the format's options, see file.formatOptions; default: as last saved}, svg?: {…SVG options} (an SVG save keeps hidden layers, display:none, unless hiddenLayers is false), …the SVG and PDF options given flat instead (an option given flat or in svg and in options with different values is an error), compress?: bool (native, also in options: gzip; default: the useCompression preference; warnings name an option given flat or in options that the file doesn't read: a legacy .ai reads only its own options, and other .ai files don't read outlineText, hiddenObjects, transparentImages or preserveEditing), version?: 4 (3, 2 or 1: for older VectorCraft versions; 1 and 2 are never compressed), preview?: false (embed a PNG of the first artboard, at most 256 px), precision?: 0..9 (native: round path anchors and handles to this many decimal places, a smaller file; colors, transforms and type stay exact; default: exact, and a later save without it writes exactly again), separateArtboards?: false (native and .ai: also save each artboard of `range` to <name>-<artboard>.<ext> beside the file, holding that artboard and the art touching it; the result's files lists every file written), range?: \"1-3, 5\"|\"all\" (default; with separateArtboards), includeLinked?: false (keep linked files' own pixels, not just their previews; they stay linked), embedProfiles?: true (carry the ICC profiles loaded from files that the document is tagged with; opening the file installs them where missing), pdfCompatible?: true (native: also carry a PDF of every artboard; false omits it) | true for .ai (false: blank pages, only the native document; a .ai file's compress, default true, is its compression.compressText), modified?: Unix seconds|null (the File Info modified date, and created date when there is none, a save to a file stamps; default now, recorded in the journal so a replay matches; null: leave the dates)} → {path, format, bytes, warnings, files?: [path…] (the file and its artboards' files), linked?: [path…] (images an SVG links to)}. Save writes one artboard, except a .ai file and EPS (below): a .ai file is a PDF-compatible file of every artboard carrying the native document (preserveEditing always on; PDF options flat or in options), which document.open restores exactly, on the active artboard (the app's, else the one the document opened on; with editingData: false, the first); separateArtboards writes more files, one per artboard. EPS takes the options of document.exportEps: by default one file whose bounding box fits the visible art (the first artboard when no art shows), carrying the whole editable document; useArtboards: true (also when artboard, artboards or range names artboards; file.formatOptions then reports it true) makes the chosen artboard the page, drawn with its background color (without Use Artboards, only the first artboard's page when no art shows has its color), and with two or more chosen the file at the path is the central file of the visible art (again the first artboard when none shows) and each artboard is also written to <name>_<artboard>.eps beside it, holding that artboard; selectedOnly is refused; its PostScript appearance flattens transparency, as EPS requires. Without a path it writes the document's own file in its own format: a document opened from or saved as SVG/PDF/EPS saves as that again (warnings name what the format loses). No path known (never saved, converted from an older version, restored by Data Recovery, or another format) → {dataBase64, format, name, folder?, warnings} and the document stays modified. Under automation read roots, a reply leaves out the document's own file outside them when no path is given (files and linked then name the files by their file names), and a suggested folder outside them; an svg or svgz save (a document whose own format is SVG or SVGZ included) is refused, before any file is written, while the document links a JavaScript file outside them (svgInteractivity; a URL is not checked), also by file.saveAs and file.saveCopy. A key that no save format takes, beside the path or in options, and an unknown key inside a PDF option's object or svg, are refused. A terminal filesystem permission/read-only refusal returns READ_ONLY_LOCATION: followed by the reason; recovery-copy failures keep their original error. Non-UI callers never open a save panel",
|
||||
has_doc,
|
||||
|s, p| save(s, SaveMode::Save, p)
|
||||
),
|
||||
|
||||
@@ -18,7 +18,7 @@ use vectorcraft_geom::Rect;
|
||||
|
||||
use super::super::{EngineError, NodeId, Result, Session, bad, bool_or, str_param};
|
||||
use super::encode::{add_note, single_artboard};
|
||||
use super::export::{export_crop, isolated, selected_artwork, without_artboards};
|
||||
use super::export::{export_crop, export_reads, isolated, selected_artwork, without_artboards};
|
||||
use super::naming::{self, NameParts};
|
||||
use super::{
|
||||
ARTBOARD_PARAMS, ArtboardPick, Format, Note, artboard_file_names_keeping_dots, create_dir, distinct_names_by, encode_with_notes, merge,
|
||||
@@ -524,6 +524,77 @@ fn rows_of(s: &Session, p: &Value, preset: Option<&str>) -> Result<Vec<Row>> {
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
/// A note for each option of a format row (`formats`) or of a format's `settings` in the request
|
||||
/// `asked` that some format takes but the file the row writes, or every row of that format writes,
|
||||
/// doesn't read (`{format: png, quality: 80}`), in the wording of a save's note and named as the
|
||||
/// request names it (`formats[0].quality`, `settings.png.quality`). The rows and settings the
|
||||
/// export writes with are those of `p` (`asked` with a preset or the remembered settings laid
|
||||
/// under it). A row's own keys (`scale`, `suffix`…), the keys the export reads for every format
|
||||
/// (`includeArtboardFill`, `richBlack`, `params`) and an artboard choice (the export picks the
|
||||
/// artboards) get none, and so does an exporter plug-in's format.
|
||||
pub(crate) fn unused_option_notes(asked: &Value, p: &Value) -> Vec<String> {
|
||||
let listed = |k: &str| super::FORMATS.iter().filter(|f| f.write).flat_map(|f| f.options).any(|o| o.name == k);
|
||||
let own = |k: &str| {
|
||||
ROW_KEYS.contains(&k) || ARTBOARD_PARAMS.contains(&k) || matches!(k, "useArtboards" | "includeArtboardFill" | "richBlack" | "params")
|
||||
};
|
||||
// The keys of `given` that `f`, writing with `options` (the format's settings and the row's
|
||||
// own), doesn't read.
|
||||
let unread = |f: &Format, given: &Map<String, Value>, options: &Value| -> Vec<String> {
|
||||
if vectorcraft_plugins::export::by_format(f.id).is_some() {
|
||||
return Vec::new();
|
||||
}
|
||||
let legacy = super::save::legacy_file(C, f, options);
|
||||
given.iter().filter(|(k, v)| !v.is_null() && !own(k) && listed(k) && !export_reads(f, legacy, k)).map(|(k, _)| k.clone()).collect()
|
||||
};
|
||||
let note = |key: String, f: &Format, options: &Value| super::save::unused_option_note(&key, &super::save::written_as(C, f, options));
|
||||
let settings = p.get("settings").and_then(Value::as_object);
|
||||
let of_format = |id: &str| settings.and_then(|s| s.get(id)).and_then(Value::as_object).cloned().unwrap_or_default();
|
||||
// A row's format and the options it writes with.
|
||||
let row_of = |row: &Value| -> Option<(&'static Format, Value)> {
|
||||
let (Some(row), Ok(f)) = (row.as_object(), writable(C, Some(str_param(row, "format").unwrap_or("png")), None)) else { return None };
|
||||
let mut options = of_format(f.id);
|
||||
options.extend(row.iter().map(|(k, v)| (k.clone(), v.clone())));
|
||||
Some((f, Value::Object(options)))
|
||||
};
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
if let Some(Value::Array(rows)) = asked.get("formats") {
|
||||
for (i, row) in rows.iter().enumerate() {
|
||||
let (Some((f, options)), Some(given)) = (row_of(row), row.as_object()) else { continue };
|
||||
out.extend(unread(f, given, &options).into_iter().map(|k| note(format!("formats[{i}].{k}"), f, &options)));
|
||||
}
|
||||
}
|
||||
// The rows written, each with its own keys: a format's settings are read as each of its rows
|
||||
// writes them (a row may name another `.ai` version), else as they are.
|
||||
let written: Vec<(&Format, Value, Map<String, Value>)> = match p.get("formats") {
|
||||
Some(Value::Array(rows)) => {
|
||||
rows.iter().filter_map(|row| row_of(row).map(|(f, options)| (f, options, row.as_object().cloned().unwrap_or_default()))).collect()
|
||||
}
|
||||
_ => Vec::new(),
|
||||
};
|
||||
for (id, given) in asked.get("settings").and_then(Value::as_object).into_iter().flatten() {
|
||||
let (Some(given), Ok(f)) = (given.as_object(), writable(C, Some(id), None)) else { continue };
|
||||
let mut rows: Vec<(&Value, Option<&Map<String, Value>>)> =
|
||||
written.iter().filter(|(g, ..)| g.id == f.id).map(|(_, o, row)| (o, Some(row))).collect();
|
||||
let alone = Value::Object(of_format(f.id));
|
||||
if rows.is_empty() {
|
||||
rows.push((&alone, None));
|
||||
}
|
||||
let unread_by: Vec<Vec<String>> = rows.iter().map(|(o, _)| unread(f, given, o)).collect();
|
||||
// A setting a row reads and doesn't give itself is used. One no row uses is noted when a
|
||||
// row doesn't read it, naming the first such row's file.
|
||||
for (k, v) in given {
|
||||
// A row's own key, null too, replaces the setting.
|
||||
let overridden = |row: Option<&Map<String, Value>>| row.is_some_and(|r| r.contains_key(k));
|
||||
let used = rows.iter().zip(&unread_by).any(|((_, row), u)| !u.contains(k) && !overridden(*row));
|
||||
let unread_row = rows.iter().zip(&unread_by).find(|(_, u)| u.contains(k));
|
||||
if let (false, false, Some(((options, _), _))) = (v.is_null(), used, unread_row) {
|
||||
out.push(note(format!("settings.{id}.{k}"), f, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Check the export settings in `p` (formats, preset, settings…) as an export would read them.
|
||||
pub(crate) fn check_settings(s: &Session, p: &Value) -> Result<()> {
|
||||
rows_of(s, p, ScreenParams::of(p)?.preset.as_deref()).map(|_| ())
|
||||
@@ -646,9 +717,10 @@ impl Output {
|
||||
pub(super) fn export_for_screens(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// A folder the client gave, as against the one its `exportPreset` keeps.
|
||||
let given = p.get("folder").is_some();
|
||||
let asked = p;
|
||||
let p = with_preset(s, p)?;
|
||||
let hidden = if given { None } else { str_param(&p, "folder").filter(|f| !f.is_empty() && !super::access::shown(f)) };
|
||||
let mut r = export(s, &p).map_err(|e| match hidden {
|
||||
let mut r = export(s, &p, asked).map_err(|e| match hidden {
|
||||
Some(folder) => super::access::naming_in_error(e, folder),
|
||||
None => e,
|
||||
})?;
|
||||
@@ -1022,9 +1094,12 @@ pub(crate) fn with_export_preset(s: &Session, cmd: &str, settings: &mut Map<Stri
|
||||
/// else the bounds of all art). Items that share a name get `-2`, `-3`… instead of overwriting, and
|
||||
/// so do different files that would share a name, with or without the `name` pattern. The reply's
|
||||
/// `warnings` lists every warning the encoders give for the files, each once, what the files lose
|
||||
/// first ([`super::note_texts`]).
|
||||
pub(crate) fn export(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// first ([`super::note_texts`]), and a note for each option of the request `asked` (`p` before a
|
||||
/// preset or the remembered settings were laid under it) that the files the rows write don't read
|
||||
/// ([`unused_option_notes`]).
|
||||
pub(crate) fn export(s: &mut Session, p: &Value, asked: &Value) -> Result<Value> {
|
||||
check_passwords(p)?;
|
||||
let unused = unused_option_notes(asked, p);
|
||||
let o = ScreenParams::of(p)?;
|
||||
let rows = rows_of(s, p, o.preset.as_deref())?;
|
||||
o.one_source()?;
|
||||
@@ -1054,7 +1129,7 @@ pub(crate) fn export(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Include Artboard Fill off: the encoders leave the background colors out.
|
||||
let fill = if o.include_artboard_fill == Some(false) { json!({ "includeArtboardFill": false }) } else { json!({}) };
|
||||
let rows = rows.into_iter().map(|r| Row { options: merge(with_black_output(s, &r.options), fill.clone()), ..r }).collect();
|
||||
let export = ScreenExport { cmd: C, o, rows, naming, items, hidden: None };
|
||||
let export = ScreenExport { cmd: C, o, rows, naming, items, unused, hidden: None };
|
||||
match &mut s.screens_hand_off {
|
||||
Some(slot) => {
|
||||
*slot = Some(export);
|
||||
@@ -1075,6 +1150,8 @@ pub struct ScreenExport {
|
||||
rows: Vec<Row>,
|
||||
naming: Naming,
|
||||
items: Vec<Item>,
|
||||
/// The notes on options the files don't read ([`unused_option_notes`]).
|
||||
unused: Vec<String>,
|
||||
/// The export folder and the name the request that prepared the export may see it by, when the
|
||||
/// folder came from the `exportPreset` and is outside that request's read roots: the reply and
|
||||
/// the errors of [`ScreenExport::write`] name it as [`export_for_screens`] does.
|
||||
@@ -1139,14 +1216,17 @@ impl ScreenExport {
|
||||
|
||||
/// [`ScreenExport::write`].
|
||||
fn encode_and_write(export: ScreenExport, writer: &mut dyn FnMut(&str, &[u8]) -> Result<()>) -> Result<Value> {
|
||||
let ScreenExport { o, rows, naming, items, .. } = export;
|
||||
let ScreenExport { o, rows, naming, items, unused, .. } = export;
|
||||
let folder = o.folder.as_deref().filter(|f| !f.is_empty());
|
||||
if let Some(dir) = folder {
|
||||
create_dir(dir)?;
|
||||
}
|
||||
let mut out = Output::default();
|
||||
// The encoders' notes for every file, each once.
|
||||
// The notes on the options, then the encoders' notes for every file, each once.
|
||||
let mut notes: Vec<Note> = Vec::new();
|
||||
for text in unused {
|
||||
add_note(&mut notes, Note::setting(text));
|
||||
}
|
||||
// Include Bleed: a grown artboard already holds the bleed. Assets, layers and a selection are
|
||||
// written on a page of their own bounds, which it doesn't grow.
|
||||
let bleed_on_page = o.include_bleed && o.assets.is_none() && o.layers.is_none() && o.selection.is_none();
|
||||
|
||||
@@ -748,8 +748,8 @@ fn a_native_save_streams_the_bytes_the_encoder_makes() {
|
||||
}
|
||||
|
||||
/// An option given beside the path (an SVG option flat or in `svg`) and in `options` with different
|
||||
/// values is refused before anything is written; `compress`, `version`, `preview` or `precision` beside the path of a format
|
||||
/// that doesn't read it is named in the warnings.
|
||||
/// values is refused before anything is written; a native option beside the path of a format that doesn't read it is named
|
||||
/// in the warnings.
|
||||
#[test]
|
||||
fn a_save_refuses_disagreeing_options_and_names_unread_native_ones() {
|
||||
let d = tmp_dir("save-options");
|
||||
|
||||
@@ -280,3 +280,102 @@ fn note_texts_give_the_losses_first_each_kind_in_order() {
|
||||
assert_eq!(note_texts(&[Note::setting("s1"), Note::setting("s2")]), ["s1", "s2"]);
|
||||
assert!(note_texts(&[]).is_empty());
|
||||
}
|
||||
|
||||
/// A save notes the `.ai` options its file doesn't read as an export does: a PDF-compatible file
|
||||
/// doesn't read the legacy file's options or `preserveEditing`, and a legacy file reads its own
|
||||
/// options only. The notes cover the options beside the path and in `options`.
|
||||
#[test]
|
||||
fn a_save_notes_the_ai_options_its_file_does_not_read() {
|
||||
let (mut s, notes) = (with_art(), unused);
|
||||
let r = s
|
||||
.execute("file.saveCopy", &json!({"format": "ai", "outlineText": true, "preserveEditing": false, "options": {"hiddenObjects": "omit"}}))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
notes(&r),
|
||||
[
|
||||
"`outlineText` was not used: PDF-compatible .ai files don't read it",
|
||||
"`preserveEditing` was not used: PDF-compatible .ai files don't read it",
|
||||
"`options.hiddenObjects` was not used: PDF-compatible .ai files don't read it"
|
||||
],
|
||||
"{r}"
|
||||
);
|
||||
let p = json!({"format": "ai", "aiVersion": "7", "compress": true, "marks": {"trim": true}, "options": {"pdfCompatible": false}});
|
||||
let r = s.execute("file.saveCopy", &p).unwrap();
|
||||
assert_eq!(
|
||||
notes(&r),
|
||||
[
|
||||
"`compress` was not used: legacy .ai (version 7) files don't read it",
|
||||
"`marks` was not used: legacy .ai (version 7) files don't read it",
|
||||
"`options.pdfCompatible` was not used: legacy .ai (version 7) files don't read it"
|
||||
],
|
||||
"{r}"
|
||||
);
|
||||
// The file's own options get none.
|
||||
for p in [
|
||||
json!({"format": "ai", "aiVersion": "7", "outlineText": true, "options": {"hiddenObjects": "omit", "range": "1"}}),
|
||||
json!({"format": "ai", "compress": true, "marks": {"trim": true}, "options": {"editingData": false}}),
|
||||
json!({"format": "pdf", "marks": {"trim": true}, "preserveEditing": false}),
|
||||
] {
|
||||
let r = s.execute("file.saveCopy", &p).unwrap();
|
||||
assert!(notes(&r).is_empty(), "{p}: {r}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A save notes an option beside the path that only another save format takes, as it notes one in
|
||||
/// `options`.
|
||||
#[test]
|
||||
fn a_save_notes_an_option_beside_the_path_that_only_another_save_format_takes() {
|
||||
let (mut s, notes) = (with_art(), unused);
|
||||
let r = s.execute("file.saveCopy", &json!({"format": "vectorcraft", "marks": {"trim": true}, "svg": {"minify": true}})).unwrap();
|
||||
assert_eq!(notes(&r), ["`marks` was not used: VectorCraft files don't read it", "`svg` was not used: VectorCraft files don't read it"], "{r}");
|
||||
}
|
||||
|
||||
/// The `.ai` options a save's file doesn't read keep the values the document's last save in that
|
||||
/// format gave them, so a save from Save Options, which sends only the options the file reads,
|
||||
/// leaves the other version's choices as they were.
|
||||
#[test]
|
||||
fn a_save_keeps_the_ai_options_its_file_does_not_read_from_the_last_save() {
|
||||
let mut s = with_art();
|
||||
let dir = tmp_dir("ai-kept");
|
||||
let path = dir.join("kept.ai").to_string_lossy().to_string();
|
||||
let value = |s: &mut Session, key: &str| s.execute("file.formatOptions", &json!({"format": "ai"})).unwrap()["options"][key]["value"].clone();
|
||||
s.execute("file.saveAs", &json!({"path": path, "format": "ai", "options": {"aiVersion": "7", "outlineText": true}})).unwrap();
|
||||
s.execute("file.saveAs", &json!({"path": path, "format": "ai", "options": {"aiVersion": "pdf", "editingData": false}})).unwrap();
|
||||
assert_eq!(value(&mut s, "outlineText"), json!(true));
|
||||
assert_eq!(value(&mut s, "editingData"), json!(false));
|
||||
s.execute("file.saveAs", &json!({"path": path, "format": "ai", "options": {"aiVersion": "7", "hiddenObjects": "omit"}})).unwrap();
|
||||
assert_eq!(value(&mut s, "editingData"), json!(false));
|
||||
// An option the request gives is the one saved.
|
||||
s.execute("file.saveAs", &json!({"path": path, "format": "ai", "options": {"aiVersion": "7", "outlineText": false}})).unwrap();
|
||||
assert_eq!(value(&mut s, "outlineText"), json!(false));
|
||||
// The kept options are neither written nor sent again: a plain save's options (which a retry
|
||||
// in another folder sends to Save As) hold only what the legacy file reads.
|
||||
for p in [json!({}), json!({"options": {"aiVersion": "7"}})] {
|
||||
let plan = save_plan(&s, SaveMode::Save, &p).unwrap();
|
||||
assert!(!plan.options.contains_key("editingData") && !plan.options.contains_key("compress"), "{:?}", plan.options);
|
||||
assert_eq!(plan.kept.get("editingData"), Some(&json!(false)));
|
||||
assert!(plan.notes.is_empty(), "{:?}", plan.notes);
|
||||
let retry = json!({"path": path, "format": "ai", "options": plan.options});
|
||||
let r = s.execute("file.saveAs", &retry).unwrap();
|
||||
assert!(!r["warnings"].to_string().contains("was not used"), "{r}");
|
||||
assert_eq!(value(&mut s, "editingData"), json!(false));
|
||||
}
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// The first `.ai` save of a document whose own file is another format remembers only the options
|
||||
/// it was given: nothing from that format's options is kept.
|
||||
#[test]
|
||||
fn a_first_ai_save_keeps_only_the_options_it_is_given() {
|
||||
let mut s = with_art();
|
||||
let dir = tmp_dir("ai-first");
|
||||
let native = dir.join("first.vectorcraft").to_string_lossy().to_string();
|
||||
let path = dir.join("first.ai").to_string_lossy().to_string();
|
||||
s.execute("file.saveAs", &json!({"path": native, "options": {"compress": false}})).unwrap();
|
||||
let plan = save_plan(&s, SaveMode::SaveAs, &json!({"path": path, "format": "ai", "options": {"aiVersion": "7"}})).unwrap();
|
||||
assert!(plan.kept.is_empty(), "{:?}", plan.kept);
|
||||
s.execute("file.saveAs", &json!({"path": path, "format": "ai", "options": {"aiVersion": "7"}})).unwrap();
|
||||
let o = s.execute("file.formatOptions", &json!({"format": "ai"})).unwrap();
|
||||
assert_eq!(o["options"]["compress"]["value"], json!(true), "{o}");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
@@ -1163,3 +1163,93 @@ fn single_file_pdfs_hold_every_chosen_artboard() {
|
||||
assert_eq!(names(&r), ["Artboard-1.pdf", "Artboard-2.pdf"]);
|
||||
assert!(s.execute("document.exportForScreens", &json!({"pdfs": "both", "formats": [{"format": "pdf"}]})).is_err());
|
||||
}
|
||||
|
||||
/// The warnings of reply `r` that name an option the file doesn't read.
|
||||
fn unused(r: &Value) -> Vec<String> {
|
||||
r["warnings"].as_array().unwrap().iter().filter_map(Value::as_str).filter(|w| w.contains("was not used")).map(str::to_string).collect()
|
||||
}
|
||||
|
||||
/// An option of a format row, or of a format's settings, that only another format reads is
|
||||
/// accepted, and the reply's warnings name it as the request does, in the words of a save's note.
|
||||
#[test]
|
||||
fn a_row_option_only_another_format_reads_gets_a_note() {
|
||||
let mut s = session(1);
|
||||
let p = json!({
|
||||
"formats": [{"format": "png", "quality": 80}, {"format": "jpg", "quality": 80}, {"format": "ai", "aiVersion": "7", "compress": true}],
|
||||
"settings": {"png": {"quality": 50, "interlaced": true}, "svg": {"interlaced": true}},
|
||||
});
|
||||
let r = export(&mut s, p);
|
||||
assert_eq!(
|
||||
unused(&r),
|
||||
[
|
||||
"`formats[0].quality` was not used: PNG files don't read it",
|
||||
"`formats[2].compress` was not used: legacy .ai (version 7) files don't read it",
|
||||
"`settings.png.quality` was not used: PNG files don't read it",
|
||||
"`settings.svg.interlaced` was not used: SVG files don't read it"
|
||||
],
|
||||
"{r}"
|
||||
);
|
||||
// A row's own keys, the keys every row takes and the artboard choices get none.
|
||||
let p = json!({"formats": [
|
||||
{"format": "png", "scale": "2x", "suffix": "@2x", "folder": "x", "includeArtboardFill": false, "artboard": 0, "useArtboards": true},
|
||||
{"format": "pdf", "marks": {"trim": true}, "preset": "High Quality Print"},
|
||||
{"format": "vectorcraft", "compress": true, "range": "1"},
|
||||
]});
|
||||
let r = export(&mut s, p);
|
||||
assert!(unused(&r).is_empty(), "{r}");
|
||||
// A format's settings are read as each row of that format writes them: a row can name another
|
||||
// `.ai` version than the settings do.
|
||||
let r = export(&mut s, json!({"formats": [{"format": "ai", "aiVersion": "7"}], "settings": {"ai": {"marks": {"trim": true}}}}));
|
||||
assert_eq!(unused(&r), ["`settings.ai.marks` was not used: legacy .ai (version 7) files don't read it"], "{r}");
|
||||
let r = export(&mut s, json!({"formats": [{"format": "ai", "aiVersion": "pdf"}], "settings": {"ai": {"aiVersion": "7", "compress": true}}}));
|
||||
assert!(unused(&r).is_empty(), "{r}");
|
||||
// A setting another row of the format reads gets none; one no row reads names the first row's
|
||||
// file.
|
||||
let rows = json!([{"format": "ai", "aiVersion": "7"}, {"format": "ai"}]);
|
||||
let r = export(&mut s, json!({"formats": rows, "settings": {"ai": {"editingData": false}}}));
|
||||
assert!(unused(&r).is_empty(), "{r}");
|
||||
let r = export(&mut s, json!({"formats": rows, "settings": {"ai": {"quality": 80}}}));
|
||||
assert_eq!(unused(&r), ["`settings.ai.quality` was not used: legacy .ai (version 7) files don't read it"], "{r}");
|
||||
// A row that gives the key itself doesn't use the setting.
|
||||
let rows = json!([{"format": "ai", "aiVersion": "7"}, {"format": "ai", "editingData": true}]);
|
||||
let r = export(&mut s, json!({"formats": rows, "settings": {"ai": {"editingData": false}}}));
|
||||
assert_eq!(unused(&r), ["`settings.ai.editingData` was not used: legacy .ai (version 7) files don't read it"], "{r}");
|
||||
let rows = json!([{"format": "ai", "aiVersion": "7"}, {"format": "ai", "editingData": null}]);
|
||||
let r = export(&mut s, json!({"formats": rows, "settings": {"ai": {"editingData": false}}}));
|
||||
assert_eq!(unused(&r), ["`settings.ai.editingData` was not used: legacy .ai (version 7) files don't read it"], "{r}");
|
||||
// An export writes one file per row and item: a native row doesn't read `separateArtboards`.
|
||||
let r = export(&mut s, json!({"formats": [{"format": "vectorcraft", "separateArtboards": true}]}));
|
||||
assert_eq!(unused(&r), ["`formats[0].separateArtboards` was not used: VectorCraft files don't read it"], "{r}");
|
||||
}
|
||||
|
||||
/// The Asset Export panel's export notes the options of its request, not those of the settings
|
||||
/// the document remembers.
|
||||
#[test]
|
||||
fn an_asset_export_notes_the_options_it_is_given() {
|
||||
let mut s = session(1);
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
s.execute("assets.add", &json!({"multiple": false})).unwrap();
|
||||
s.execute("assets.settings.set", &json!({"formats": [{"format": "png", "quality": 80}]})).unwrap();
|
||||
let r = s.execute("assets.export", &json!({})).unwrap();
|
||||
assert!(unused(&r).is_empty(), "{r}");
|
||||
let r = s.execute("assets.export", &json!({"formats": [{"format": "svg", "quality": 80}]})).unwrap();
|
||||
assert_eq!(unused(&r), ["`formats[0].quality` was not used: SVG files don't read it"], "{r}");
|
||||
// Settings given are read as the remembered rows write them.
|
||||
s.execute("assets.settings.set", &json!({"formats": [{"format": "ai", "aiVersion": "7"}]})).unwrap();
|
||||
let r = s.execute("assets.export", &json!({"settings": {"ai": {"editingData": false}}})).unwrap();
|
||||
assert_eq!(unused(&r), ["`settings.ai.editingData` was not used: legacy .ai (version 7) files don't read it"], "{r}");
|
||||
s.execute("assets.settings.set", &json!({"formats": [{"format": "png", "quality": 80}]})).unwrap();
|
||||
// So does the export a client prepares.
|
||||
let p = json!({"formats": [{"format": "svg", "quality": 80}]});
|
||||
let r = s.prepare_screen_export("assets.export", &p).unwrap().write(&mut |_: &str, _: &[u8]| Ok(())).unwrap();
|
||||
assert_eq!(unused(&r), ["`formats[0].quality` was not used: SVG files don't read it"], "{r}");
|
||||
}
|
||||
|
||||
/// A preset's options add no notes: Export for Screens notes the options of its request.
|
||||
#[test]
|
||||
fn an_export_preset_adds_no_notes() {
|
||||
let mut s = session(1);
|
||||
s.execute("document.exportPresets.save", &json!({"name": "Mixed", "settings": {"formats": [{"format": "png", "quality": 80}]}})).unwrap();
|
||||
let r = export(&mut s, json!({"exportPreset": "Mixed"}));
|
||||
assert!(unused(&r).is_empty(), "{r}");
|
||||
}
|
||||
@@ -1724,7 +1724,7 @@ fn apply(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut out = json!({ "changed": changed, "swatches": added });
|
||||
not_changed.report(&mut out);
|
||||
if let Some(e) = edit {
|
||||
e.defaults(s);
|
||||
e.defaults(s)?;
|
||||
out["relinked"] = json!(e.relinked());
|
||||
}
|
||||
if converted {
|
||||
|
||||
@@ -1176,6 +1176,7 @@ pub fn jobs(s: &mut Session, store: &Arc<dyn RecoveryStore>) -> Result<(Vec<Reco
|
||||
modified: None,
|
||||
notes: Vec::new(),
|
||||
lossy_source: None,
|
||||
kept: serde_json::Map::new(),
|
||||
};
|
||||
let save = fileio::job_for(s, st, plan)?;
|
||||
let meta = Meta {
|
||||
|
||||
@@ -322,7 +322,7 @@ fn swatch_delete(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
super::paint::check_colorized_kept(colorized_ok, d).map_err(|e| bad(C, e))?;
|
||||
Ok(unlinked)
|
||||
})?;
|
||||
map_default_paints(s, &mut unlink);
|
||||
map_default_paints(s, &mut unlink)?;
|
||||
Ok(json!({"deleted": names, "unlinked": unlinked}))
|
||||
}
|
||||
|
||||
@@ -660,20 +660,41 @@ impl Relink {
|
||||
renamed || recolored
|
||||
}
|
||||
|
||||
fn defaults(&self, s: &mut Session) {
|
||||
map_default_paints(s, &mut |c, l, t| self.apply(c, l, t));
|
||||
fn defaults(&self, s: &mut Session) -> Result<()> {
|
||||
map_default_paints(s, &mut |c, l, t| self.apply(c, l, t))
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply a swatch-link rewrite to the default fill and stroke for new art, except during a live
|
||||
/// preview (Cancel rolls the document back, not the session).
|
||||
pub(super) fn map_default_paints(s: &mut Session, f: &mut LinkFn) {
|
||||
if s.in_interaction() {
|
||||
return;
|
||||
/// Apply a swatch-link rewrite to the fill and stroke new art takes and to the paints of its
|
||||
/// template appearance, except during a live preview (Cancel rolls the document back, not the
|
||||
/// session). A `command.batch` step rewrites them as the command run on its own does: a failed
|
||||
/// batch, and a canceled batch preview, put back the paints the batch started with. A change is
|
||||
/// recorded with the step just made, so Undo and Redo put these paints back with the document.
|
||||
pub(super) fn map_default_paints(s: &mut Session, f: &mut LinkFn) -> Result<()> {
|
||||
if s.in_interaction() && s.batch_stash.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
for p in [&mut s.paint.fill, &mut s.paint.stroke] {
|
||||
p.map_links(f);
|
||||
let start = super::colormgmt::new_art_colors_before(s)?;
|
||||
let pd = &mut s.paint;
|
||||
let template = pd.appearance.iter_mut().flat_map(|a| a.items.iter_mut()).map(|it| match it {
|
||||
vectorcraft_doc::AppearanceItem::Fill(f) => &mut f.paint,
|
||||
vectorcraft_doc::AppearanceItem::Stroke(s) => &mut s.paint,
|
||||
});
|
||||
let mut changed = false;
|
||||
for p in [&mut pd.fill, &mut pd.stroke].into_iter().chain(template) {
|
||||
changed |= p.map_links(f);
|
||||
}
|
||||
if changed {
|
||||
super::colormgmt::record_new_art_colors(s, start)?;
|
||||
// The step takes the paints the command finally leaves, after the selection's appearance
|
||||
// is inherited.
|
||||
let st = s.doc()?;
|
||||
if st.interaction.is_none() {
|
||||
let uid = st.uid;
|
||||
s.batch_paint_docs.push(uid);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Apply a swatch-link rewrite to the document ([`Document::map_solid_paints`]) and to the art of
|
||||
@@ -888,7 +909,7 @@ fn swatch_edit(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
super::paint::check_colorized_kept(colorized_ok, d).map_err(|e| bad(C, e))?;
|
||||
Ok(relinked)
|
||||
})?;
|
||||
relink.defaults(s);
|
||||
relink.defaults(s)?;
|
||||
Ok(json!({"name": to, "relinked": relinked}))
|
||||
}
|
||||
|
||||
@@ -1084,7 +1105,7 @@ fn swatch_merge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
super::paint::check_colorized_kept(colorized_ok, d).map_err(|e| bad(C, e))?;
|
||||
Ok(relinked)
|
||||
})?;
|
||||
map_default_paints(s, &mut relink);
|
||||
map_default_paints(s, &mut relink)?;
|
||||
Ok(json!({"name": keep, "merged": merged, "relinked": relinked}))
|
||||
}
|
||||
|
||||
@@ -1141,7 +1162,7 @@ fn swatch_spot_options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let relinked = map_swatch_links(d, &mut follow_bases(&bases));
|
||||
Ok((relinked, bases))
|
||||
})?;
|
||||
map_default_paints(s, &mut follow_bases(&bases));
|
||||
map_default_paints(s, &mut follow_bases(&bases))?;
|
||||
Ok(json!({"useLab": use_lab, "relinked": relinked}))
|
||||
}
|
||||
|
||||
@@ -1165,8 +1186,11 @@ pub(super) struct GroupEdit {
|
||||
|
||||
impl GroupEdit {
|
||||
/// Apply the link changes to the default fill and stroke for new art.
|
||||
pub(super) fn defaults(&self, s: &mut Session) {
|
||||
self.relinks.iter().for_each(|r| r.defaults(s));
|
||||
pub(super) fn defaults(&self, s: &mut Session) -> Result<()> {
|
||||
if self.relinks.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
map_default_paints(s, &mut |c, l, t| self.relinks.iter().any(|r| r.apply(c, l, t)))
|
||||
}
|
||||
|
||||
/// How many paints linked to the group's swatches the rewrite changed.
|
||||
@@ -1242,6 +1266,6 @@ fn swatch_edit_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
colors.into_iter().enumerate().map(|(i, c)| if old.get(i) == Some(&Some(c)) { c } else { super::paint::new_color(s, p, c) }).collect();
|
||||
let rename = str_param(p, "rename");
|
||||
let e = s.edit("Edit Color Group", |d, _| rewrite_group(d, group, &colors, rename, C))?;
|
||||
e.defaults(s);
|
||||
e.defaults(s)?;
|
||||
Ok(json!({"name": e.name, "swatches": e.swatches, "relinked": e.relinked}))
|
||||
}
|
||||
@@ -980,7 +980,7 @@ fn reset_defaults(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(added)
|
||||
})?;
|
||||
if replace {
|
||||
map_default_paints(s, &mut |_, l, _| unlink_dead(&live, l));
|
||||
map_default_paints(s, &mut |_, l, _| unlink_dead(&live, l))?;
|
||||
}
|
||||
Ok(json!({ "added": added }))
|
||||
}
|
||||
|
||||
@@ -1688,10 +1688,9 @@ fn warichu_is_a_character_attribute_set_saved_and_exported() {
|
||||
assert!(["warichu", "warichuLines", "warichuGap", "warichuScale", "warichuAlign"].iter().all(|k| saved.get(*k).is_none()), "{saved}");
|
||||
let plain_svg = vectorcraft_svg::export(&s.doc().unwrap().doc, &Default::default());
|
||||
let plain_pdf = vectorcraft_pdf::export(&s.doc().unwrap().doc, &vectorcraft_pdf::PdfOptions::uncompressed()).unwrap();
|
||||
let text_ops = |pdf: &[u8]| {
|
||||
let s = String::from_utf8_lossy(pdf);
|
||||
s.contains("Tj") || s.contains("TJ")
|
||||
};
|
||||
// Only text objects count: the file ID in the trailer and the XMP is base64 that changes each
|
||||
// second and now and then spells `Tj` or `TJ`.
|
||||
let text_ops = |pdf: &[u8]| String::from_utf8_lossy(pdf).lines().any(|l| l.starts_with("BT ") && (l.contains("Tj") || l.contains("TJ")));
|
||||
assert!(plain_svg.contains("<text") && text_ops(&plain_pdf));
|
||||
|
||||
// text.setFormat with the values the characters already have changes nothing and records no
|
||||
|
||||
@@ -369,12 +369,13 @@ impl DocState {
|
||||
/// menu asks on every frame it is open, and measuring builds the solids of 3D effects, so the
|
||||
/// answer is kept for this version of the document and those artboards, and a change of the
|
||||
/// selection or the view keeps it. It is measured again when what the measure reads outside the
|
||||
/// document changes: the installed plug-ins, whose effects draw art, and the installed fonts,
|
||||
/// which set the bounds of all type.
|
||||
/// document changes: the installed plug-ins, whose effects draw art, and the installed fonts and
|
||||
/// the hyphenation exceptions, which set the bounds of all type.
|
||||
pub fn any_art_to_fit(&self, indices: &[usize]) -> bool {
|
||||
self.any_art_to_fit_with(indices, (vectorcraft_plugins::registry::revision(), vectorcraft_text::FontDb::global().generation()))
|
||||
self.any_art_to_fit_with(indices, (vectorcraft_plugins::registry::revision(), vectorcraft_text::layout_inputs()))
|
||||
}
|
||||
/// [`Self::any_art_to_fit`], with `outside` the generations of the plug-ins and the fonts.
|
||||
/// [`Self::any_art_to_fit`], with `outside` the generations of the plug-ins and the layout inputs
|
||||
/// ([`vectorcraft_text::layout_inputs`]).
|
||||
pub(crate) fn any_art_to_fit_with(&self, indices: &[usize], outside: (u64, u64)) -> bool {
|
||||
// Measuring runs renderer and plug-in code; should it ever ask again while measuring, it
|
||||
// is measured without being kept rather than borrowed twice.
|
||||
|
||||
@@ -25,9 +25,11 @@ impl Session {
|
||||
// Clicks find an image's own fills and strokes where their effects draw them.
|
||||
vectorcraft_doc::hit::set_image_paint(vectorcraft_render::effects::image_paint);
|
||||
// Clicks find a blend's steps and an envelope's distorted art where the canvas draws them,
|
||||
// evaluated again when the fonts or the plug-ins change.
|
||||
// evaluated again when the fonts, the hyphenation exceptions or the plug-ins change.
|
||||
vectorcraft_doc::hit::set_live_art(vectorcraft_render::effects::expand_live, || {
|
||||
vectorcraft_text::FontDb::global().generation().rotate_left(32) ^ vectorcraft_render::effects::plugin_revision()
|
||||
let mut h = std::hash::DefaultHasher::new();
|
||||
std::hash::Hash::hash(&(vectorcraft_text::layout_inputs(), vectorcraft_render::effects::plugin_revision()), &mut h);
|
||||
std::hash::Hasher::finish(&h)
|
||||
});
|
||||
// Envelopes map type from the bounds of its glyph outlines, underlines and strikethroughs.
|
||||
vectorcraft_text::register_envelope_ink();
|
||||
|
||||
@@ -560,6 +560,125 @@ fn reset_bounding_box_then_release_intertwine() {
|
||||
reset_then_release("object.intertwine.make", "object.intertwine.release");
|
||||
}
|
||||
|
||||
/// A symbol of two rectangles, the first turned `inner` degrees before the symbol is made, placed
|
||||
/// as an instance that is then turned 20°.
|
||||
fn turned_instance(inner: f64) -> (Session, NodeId) {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 100.0, 100.0, 100.0, 50.0);
|
||||
let b = rect(&mut s, 150.0, 220.0, 100.0, 50.0);
|
||||
if inner != 0.0 {
|
||||
sel(&mut s, &[a]);
|
||||
s.execute("object.rotate", &json!({"angle": inner})).unwrap();
|
||||
}
|
||||
sel(&mut s, &[a, b]);
|
||||
let inst = id_of(&s.execute("symbol.new", &json!({"name": "Pair"})).unwrap());
|
||||
sel(&mut s, &[inst]);
|
||||
s.execute("object.rotate", &json!({"angle": 20})).unwrap();
|
||||
assert!(near(angle(&s, inst), 20.0));
|
||||
(s, inst)
|
||||
}
|
||||
|
||||
/// Break Link and Expand with the parameters that turn instance `inst` into its art.
|
||||
fn expand_instance(s: &mut Session, command: &str, inst: NodeId) {
|
||||
sel(s, &[inst]);
|
||||
let params = if command == "symbol.breakLink" { json!({"ids": [inst.0]}) } else { json!({"object": true, "fill": false, "stroke": false}) };
|
||||
s.execute(command, ¶ms).unwrap();
|
||||
assert!(s.doc().unwrap().doc.node(inst).is_none_or(|n| !matches!(n.kind, vectorcraft_doc::NodeKind::SymbolInstance { .. })), "{command}");
|
||||
}
|
||||
|
||||
/// The angles of the objects inside the document's only top-level object, that object included.
|
||||
fn art_angles(s: &Session) -> Vec<f64> {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let layer = d.layers.first().unwrap();
|
||||
let top = layer.children().unwrap();
|
||||
assert_eq!(top.len(), 1);
|
||||
let mut out = vec![];
|
||||
subtree_angles(&top[0], &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_bounding_box_then_expand_symbol_instance() {
|
||||
for command in ["symbol.breakLink", "object.expand"] {
|
||||
let (mut s, inst) = turned_instance(0.0);
|
||||
assert_eq!(s.execute("object.resetBoundingBox", &json!({})).unwrap()["changed"], 1);
|
||||
assert_eq!(angle(&s, inst), 0.0);
|
||||
expand_instance(&mut s, command, inst);
|
||||
let angles = art_angles(&s);
|
||||
assert!(angles.len() >= 2 && angles.iter().all(|a| near(*a, 0.0)), "{command}: {angles:?}");
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
assert_eq!(selection_box(&s).angle, 0.0, "{command}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expanding_a_turned_symbol_instance_keeps_its_angle() {
|
||||
for command in ["symbol.breakLink", "object.expand"] {
|
||||
let (mut s, inst) = turned_instance(0.0);
|
||||
expand_instance(&mut s, command, inst);
|
||||
let angles = art_angles(&s);
|
||||
assert!(angles.len() >= 2 && angles.iter().all(|a| near(*a, 20.0)), "{command}: {angles:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_bounding_box_then_expand_keeps_the_angles_inside_the_symbol() {
|
||||
for command in ["symbol.breakLink", "object.expand"] {
|
||||
let (mut s, inst) = turned_instance(30.0);
|
||||
s.execute("object.resetBoundingBox", &json!({})).unwrap();
|
||||
expand_instance(&mut s, command, inst);
|
||||
// The turned rectangle keeps the 30° it had in the symbol; the rest stand square.
|
||||
let mut angles = art_angles(&s);
|
||||
angles.sort_by(f64::total_cmp);
|
||||
assert!(near(*angles.last().unwrap(), 30.0) && angles[..angles.len() - 1].iter().all(|a| near(*a, 0.0)), "{command}: {angles:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The group that an instance's opacity adds around the art of a one-object symbol stands as the
|
||||
/// instance's box does, turned or reset.
|
||||
#[test]
|
||||
fn expanding_a_faded_symbol_instance_gives_its_group_the_instance_box() {
|
||||
for command in ["symbol.breakLink", "object.expand"] {
|
||||
for reset in [false, true] {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 100.0, 100.0, 100.0, 50.0);
|
||||
sel(&mut s, &[a]);
|
||||
let inst = id_of(&s.execute("symbol.new", &json!({"name": "One"})).unwrap());
|
||||
sel(&mut s, &[inst]);
|
||||
s.execute("object.rotate", &json!({"angle": 20})).unwrap();
|
||||
s.execute("object.setProps", &json!({"ids": [inst.0], "opacity": 50})).unwrap();
|
||||
if reset {
|
||||
s.execute("object.resetBoundingBox", &json!({})).unwrap();
|
||||
}
|
||||
expand_instance(&mut s, command, inst);
|
||||
let want = if reset { 0.0 } else { 20.0 };
|
||||
let angles = art_angles(&s);
|
||||
assert!(angles.len() == 2 && angles.iter().all(|a| near(*a, want)), "{command}, reset {reset}: {angles:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A mirrored instance: turned 170° and then reflected, its box stands 180° from the box its
|
||||
/// transform gives in one step. The art takes the instance's box, and stands square after a reset.
|
||||
#[test]
|
||||
fn expanding_a_reflected_symbol_instance_keeps_its_box() {
|
||||
for command in ["symbol.breakLink", "object.expand"] {
|
||||
for reset in [false, true] {
|
||||
let (mut s, inst) = turned_instance(0.0);
|
||||
s.execute("object.rotate", &json!({"angle": 150})).unwrap();
|
||||
s.execute("object.reflect", &json!({"axis": "horizontal"})).unwrap();
|
||||
assert!(near(angle(&s, inst), -170.0), "{}", angle(&s, inst));
|
||||
if reset {
|
||||
s.execute("object.resetBoundingBox", &json!({})).unwrap();
|
||||
}
|
||||
let want = angle(&s, inst);
|
||||
expand_instance(&mut s, command, inst);
|
||||
let angles = art_angles(&s);
|
||||
assert!(angles.len() >= 2 && angles.iter().all(|a| near(*a, want)), "{command}, reset {reset}: {angles:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_bounding_box_then_release_linked_opacity_mask() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -184,6 +184,25 @@ fn pencil_alt_closes_path() {
|
||||
assert!(ps[0].1.is_closed());
|
||||
}
|
||||
|
||||
/// A Pencil stroke drawn as a dome and closed with Alt at the release closes with a straight segment
|
||||
/// across the bottom, from where the stroke ends to where it starts: the path stays within where the
|
||||
/// pen went, rather than curving on past both ends and back like a hat's brim.
|
||||
#[test]
|
||||
fn pencil_alt_closes_a_stroke_straight_back_to_its_start() {
|
||||
let mut s = session();
|
||||
let pts: Vec<(f64, f64)> =
|
||||
(0..=60).map(|i| f64::from(i) * std::f64::consts::PI / 60.0).map(|a| (300.0 - 150.0 * a.cos(), 400.0 - 200.0 * a.sin())).collect();
|
||||
drag(&mut s, "pencil", &pts, Mods { alt: true, ..Default::default() });
|
||||
let ps = paths(&s);
|
||||
let sp = &ps[0].1.subpaths[0];
|
||||
assert!(sp.closed);
|
||||
let b = ps[0].1.bounds().unwrap();
|
||||
assert!(b.x0 > 149.5 && b.x1 < 450.5 && b.y1 < 400.5, "within the stroke: {b:?}");
|
||||
let (first, last) = (sp.anchors.first().unwrap(), sp.anchors.last().unwrap());
|
||||
assert!(first.p.distance(Point::new(150.0, 400.0)) < 1e-6 && last.p.distance(Point::new(450.0, 400.0)) < 1e-6, "{first:?} {last:?}");
|
||||
assert_eq!((last.h_out, first.h_in), (last.p, first.p), "a straight closing segment");
|
||||
}
|
||||
|
||||
/// #3799: with Shift held, a wavy Pencil drag draws one straight segment at 45° steps from the
|
||||
/// Constrain Angle (here 30°: a drag at about 60° comes out at 75°).
|
||||
#[test]
|
||||
|
||||
@@ -572,6 +572,64 @@ fn a_malformed_required_number_is_not_reported_as_missing() {
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);
|
||||
}
|
||||
|
||||
/// A required whole number given as something other than a whole number of 0 or more (`"3"`, `2.5`,
|
||||
/// `-1`, `true`, `[]`, `{}`) is refused as the wrong type, naming its key, not as a missing integer; one
|
||||
/// left out or given as null is still missing. An optional one left out or null keeps its default.
|
||||
/// Neither changes the document or records an undo step.
|
||||
#[test]
|
||||
fn a_malformed_required_integer_is_not_reported_as_missing() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 40})).unwrap();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let with = |base: &serde_json::Value, key: &str, v: Option<serde_json::Value>| {
|
||||
let mut p = base.clone();
|
||||
let o = p.as_object_mut().unwrap();
|
||||
match v {
|
||||
Some(v) => o.insert(key.into(), v),
|
||||
None => o.remove(key),
|
||||
};
|
||||
p
|
||||
};
|
||||
let refused = |s: &mut Session, cmd: &str, p: &serde_json::Value, want: &str| match s.execute(cmd, p) {
|
||||
Err(EngineError::BadParams { cmd: c, msg }) => assert_eq!((c.as_str(), msg.as_str()), (cmd, want), "{p}"),
|
||||
other => panic!("{cmd} {p}: {other:?}"),
|
||||
};
|
||||
let ends = json!({"sourceDocument": 1, "grab": [0, 0], "at": [0, 0], "ids": [1]});
|
||||
let cases = [
|
||||
("appearance.setItem", json!({"index": 0}), "index", "missing integer `index`"),
|
||||
("path.removeAnchor", json!({"id": 1, "anchor": 0}), "anchor", "missing integer `anchor`"),
|
||||
("path.split", json!({"id": 1, "anchor": 0}), "anchor", "need `anchor` or `segment` + `t`"),
|
||||
("addon.pen.removePoint", json!({"anchor": 0}), "anchor", "missing integer `anchor`"),
|
||||
("edit.dropSelection", ends, "sourceDocument", "missing integer `sourceDocument`"),
|
||||
("document.activate", json!({"index": 0}), "index", "missing index"),
|
||||
("document.move", json!({"to": 0}), "to", "missing `to`: a non-negative integer"),
|
||||
];
|
||||
let malformed = [json!("3"), json!(2.5), json!(-1), json!(true), json!([]), json!({})];
|
||||
for (cmd, base, key, missing) in &cases {
|
||||
for v in &malformed {
|
||||
refused(&mut s, cmd, &with(base, key, Some(v.clone())), &format!("`{key}` must be a whole number"));
|
||||
}
|
||||
for p in [with(base, key, None), with(base, key, Some(json!(null)))] {
|
||||
refused(&mut s, cmd, &p, missing);
|
||||
}
|
||||
}
|
||||
// An optional `subpath` given as anything else is refused the same way; left out or null, it is
|
||||
// subpath 0 and the command goes on to read `anchor`.
|
||||
let base = json!({"id": 1, "anchor": "3"});
|
||||
for v in &malformed {
|
||||
refused(&mut s, "path.removeAnchor", &with(&base, "subpath", Some(v.clone())), "`subpath` must be a whole number");
|
||||
}
|
||||
for p in [with(&base, "subpath", None), with(&base, "subpath", Some(json!(null)))] {
|
||||
refused(&mut s, "path.removeAnchor", &p, "`anchor` must be a whole number");
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().doc, doc);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
// A whole JSON number is a whole number.
|
||||
s.execute("document.activate", &json!({"index": 0})).unwrap();
|
||||
}
|
||||
|
||||
/// A command that takes the keys of one that isn't checked yet isn't checked either.
|
||||
#[test]
|
||||
fn a_reference_to_an_unchecked_command_is_unchecked() {
|
||||
|
||||
@@ -693,7 +693,6 @@ fn use_precise_cursors_swaps_the_tools_own_pointers_for_a_crosshair() {
|
||||
("blobBrush", Cursor::BlobBrush),
|
||||
("shaper", Cursor::Shaper),
|
||||
("pencil", Cursor::PencilNew),
|
||||
("smooth", Cursor::Smooth),
|
||||
("pathEraser", Cursor::PathEraser),
|
||||
("join", Cursor::Join),
|
||||
("eraser", Cursor::Eraser),
|
||||
@@ -738,6 +737,27 @@ fn use_precise_cursors_swaps_the_tools_own_pointers_for_a_crosshair() {
|
||||
}
|
||||
}
|
||||
|
||||
/// With no selected path, the Smooth tool shows Not Allowed, and Use Precise Cursors leaves it so; with a
|
||||
/// selected path, it shows its own pointer, or a crosshair with the preference on.
|
||||
#[test]
|
||||
fn use_precise_cursors_keeps_the_smooth_tools_not_allowed_pointer() {
|
||||
use vectorcraft_tools::{Cursor, Mods};
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
|
||||
let at = vectorcraft_geom::Point::new(300.0, 300.0);
|
||||
s.select_tool("smooth", ViewInfo::default()).unwrap();
|
||||
for precise in [false, true] {
|
||||
s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": precise})).unwrap();
|
||||
assert_eq!(s.cursor(at, Mods::default(), ViewInfo::default()), Cursor::NotAllowed, "precise {precise}");
|
||||
}
|
||||
let id = s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("select.set", &json!({"ids": [id]})).unwrap();
|
||||
for (precise, c) in [(false, Cursor::Smooth), (true, Cursor::Crosshair)] {
|
||||
s.execute("prefs.set", &json!({"key": "usePreciseCursors", "value": precise})).unwrap();
|
||||
assert_eq!(s.cursor(at, Mods::default(), ViewInfo::default()), c, "precise {precise}");
|
||||
}
|
||||
}
|
||||
|
||||
/// With Alt held while drawing, the Pencil and the Paintbrush show their closing cursors, and Use Precise Cursors
|
||||
/// turns those into a crosshair too.
|
||||
#[test]
|
||||
|
||||
@@ -1075,6 +1075,146 @@ fn deleting_a_global_swatch_unlinks_art_that_keeps_its_colour() {
|
||||
assert_eq!(fill_of(&s, a), linked("#2a6fb0", "Brand"));
|
||||
}
|
||||
|
||||
/// A session whose default fill and stroke for new art are linked to the global swatch `Brand`
|
||||
/// (`#2a6fb0`), in color group `G`, beside the global swatch `Other` (`#ff8800`).
|
||||
fn brand_defaults_session() -> Session {
|
||||
let mut s = session();
|
||||
run(&mut s, "swatch.new", json!({"name": "Brand", "color": "#2a6fb0", "global": true}));
|
||||
run(&mut s, "swatch.newGroup", json!({"name": "G", "swatches": ["Brand"]}));
|
||||
run(&mut s, "swatch.new", json!({"name": "Other", "color": "#ff8800", "global": true}));
|
||||
run(&mut s, "select.set", json!({"ids": []}));
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "Brand"}));
|
||||
run(&mut s, "paint.setStroke", json!({"swatch": "Brand"}));
|
||||
assert_eq!(s.paint.fill, linked("#2a6fb0", "Brand"));
|
||||
s
|
||||
}
|
||||
|
||||
fn fill_and_stroke(s: &Session, id: NodeId) -> (Paint, Paint) {
|
||||
let a = &doc(s).node(id).unwrap().appearance;
|
||||
(a.fill_paint(), a.stroke_paint())
|
||||
}
|
||||
|
||||
/// Run `steps` and then draw a rectangle, once one command at a time and once as one
|
||||
/// `command.batch`, and check that both leave the same default paints and give the rectangle drawn
|
||||
/// in the batch, and one drawn after it, the same fill and stroke. Returns the batch's rectangle's
|
||||
/// fill and stroke.
|
||||
fn batch_matches_steps(steps: &[(&str, Value)]) -> (Paint, Paint) {
|
||||
let shape = ("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10}));
|
||||
let mut one_by_one = brand_defaults_session();
|
||||
for (id, p) in steps {
|
||||
run(&mut one_by_one, id, p.clone());
|
||||
}
|
||||
let single = rect(&mut one_by_one);
|
||||
let mut batched = brand_defaults_session();
|
||||
let commands: Vec<Value> = steps.iter().chain([&shape]).map(|(id, p)| json!({"command": id, "params": p})).collect();
|
||||
let r = run(&mut batched, "command.batch", json!({ "commands": commands }));
|
||||
let in_batch = NodeId(r["results"][steps.len()]["id"].as_u64().unwrap());
|
||||
assert_eq!(batched.paint.fill, one_by_one.paint.fill, "the default fill after {steps:?}");
|
||||
assert_eq!(batched.paint.stroke, one_by_one.paint.stroke, "the default stroke after {steps:?}");
|
||||
let expected = fill_and_stroke(&one_by_one, single);
|
||||
assert_eq!(fill_and_stroke(&batched, in_batch), expected, "art drawn in the batch after {steps:?}");
|
||||
let later = rect(&mut batched);
|
||||
assert_eq!(fill_and_stroke(&batched, later), expected, "art drawn after the batch after {steps:?}");
|
||||
expected
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swatch_steps_in_a_batch_change_the_new_art_paints_as_commands_do() {
|
||||
let blue = Paint::solid(Color::from_hex("#2a6fb0").unwrap());
|
||||
assert_eq!(batch_matches_steps(&[("swatch.delete", json!({"name": "Brand"}))]), (blue.clone(), blue));
|
||||
let link = linked("#2a6fb0", "Brand");
|
||||
assert_eq!(batch_matches_steps(&[("swatch.delete", json!({"name": "Brand", "unlink": false}))]), (link.clone(), link));
|
||||
let signal = linked("#00ff00", "Signal");
|
||||
let edit = ("swatch.edit", json!({"name": "Brand", "newName": "Signal", "color": "#00ff00"}));
|
||||
assert_eq!(batch_matches_steps(&[edit]), (signal.clone(), signal));
|
||||
let other = linked("#ff8800", "Other");
|
||||
assert_eq!(batch_matches_steps(&[("swatch.merge", json!({"names": ["Other", "Brand"]}))]), (other.clone(), other));
|
||||
let gray = linked("#ababab", "Brand");
|
||||
assert_eq!(batch_matches_steps(&[("swatch.editGroup", json!({"group": "G", "colors": ["#ababab"]}))]), (gray.clone(), gray));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undo_and_redo_of_a_swatch_edit_put_back_the_new_art_paints() {
|
||||
let edit = json!({"name": "Brand", "newName": "Signal", "color": "#00ff00"});
|
||||
let delete = json!({"name": "Brand"});
|
||||
for (id, p, fill) in
|
||||
[("swatch.edit", &edit, linked("#00ff00", "Signal")), ("swatch.delete", &delete, Paint::solid(Color::from_hex("#2a6fb0").unwrap()))]
|
||||
{
|
||||
for batch in [false, true] {
|
||||
let mut s = brand_defaults_session();
|
||||
let before = s.paint.clone();
|
||||
if batch {
|
||||
run(&mut s, "command.batch", json!({"commands": [{"command": id, "params": p}]}));
|
||||
} else {
|
||||
run(&mut s, id, p.clone());
|
||||
}
|
||||
assert_eq!(s.paint.fill, fill, "{id}, batch: {batch}");
|
||||
let after = s.paint.clone();
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(s.paint, before, "Undo of {id}, batch: {batch}");
|
||||
run(&mut s, "edit.redo", json!({}));
|
||||
assert_eq!(s.paint, after, "Redo of {id}, batch: {batch}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swatch_edits_reach_the_paints_of_the_new_art_template() {
|
||||
let mut s = brand_defaults_session();
|
||||
s.paint.appearance = Some(vectorcraft_doc::Appearance::basic(linked("#2a6fb0", "Brand"), linked("#2a6fb0", "Brand"), 1.0));
|
||||
run(&mut s, "swatch.edit", json!({"name": "Brand", "newName": "Signal", "color": "#00ff00"}));
|
||||
let template = |s: &Session| -> Vec<Paint> {
|
||||
s.paint
|
||||
.appearance
|
||||
.iter()
|
||||
.flat_map(|a| a.items.iter())
|
||||
.map(|it| match it {
|
||||
vectorcraft_doc::AppearanceItem::Fill(f) => f.paint.clone(),
|
||||
vectorcraft_doc::AppearanceItem::Stroke(k) => k.paint.clone(),
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
assert_eq!(template(&s), [linked("#00ff00", "Signal"), linked("#00ff00", "Signal")]);
|
||||
run(&mut s, "swatch.delete", json!({"name": "Signal"}));
|
||||
let green = Paint::solid(Color::from_hex("#00ff00").unwrap());
|
||||
assert_eq!(template(&s), [green.clone(), green]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_batch_puts_back_the_new_art_paints_a_swatch_step_changed() {
|
||||
let mut s = brand_defaults_session();
|
||||
let before = s.paint.clone();
|
||||
let steps = json!([
|
||||
{"command": "swatch.edit", "params": {"name": "Brand", "newName": "Signal", "color": "#00ff00"}},
|
||||
{"command": "no.such.command", "params": {}},
|
||||
]);
|
||||
assert!(s.execute("command.batch", &json!({ "commands": steps })).is_err());
|
||||
assert_eq!(s.paint, before);
|
||||
assert_eq!(doc(&s).swatch("Brand").unwrap().paint.color(), Some(Color::from_hex("#2a6fb0").unwrap()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canceling_a_swatch_options_preview_keeps_the_new_art_paints() {
|
||||
let mut s = brand_defaults_session();
|
||||
let before = s.paint.clone();
|
||||
s.begin_interaction("Swatch Options").unwrap();
|
||||
s.preview("swatch.edit", &json!({"name": "Brand", "newName": "Signal", "color": "#00ff00"})).unwrap();
|
||||
s.cancel_interaction().unwrap();
|
||||
assert_eq!(s.paint, before);
|
||||
assert!(doc(&s).swatch("Signal").is_none());
|
||||
// A previewed batch that is canceled puts them back too, and one that is accepted keeps them.
|
||||
let steps = json!([{"command": "swatch.edit", "params": {"name": "Brand", "newName": "Signal", "color": "#00ff00"}}]);
|
||||
s.begin_interaction("Batch Preview").unwrap();
|
||||
s.preview("command.batch", &json!({ "commands": steps })).unwrap();
|
||||
s.cancel_interaction().unwrap();
|
||||
assert_eq!(s.paint, before);
|
||||
s.begin_interaction("Batch Preview").unwrap();
|
||||
s.preview("command.batch", &json!({ "commands": steps })).unwrap();
|
||||
s.commit_interaction().unwrap();
|
||||
assert_eq!(s.paint.fill, linked("#00ff00", "Signal"));
|
||||
assert_eq!(s.paint.stroke, linked("#00ff00", "Signal"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_several_swatches_and_groups() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -20,9 +20,18 @@ fn close(a: Rect, b: Rect) -> bool {
|
||||
/// A 100 × 100 frame with a 10 × 10 square in its top-left corner and one in its center, made
|
||||
/// into symbol "Panel" with 9-slice guides on, its instance selected at its natural size.
|
||||
fn panel_symbol() -> (Session, NodeId) {
|
||||
panel_symbol_filled(None)
|
||||
}
|
||||
|
||||
/// [`panel_symbol`] with the frame filled with `fill` when there is one.
|
||||
fn panel_symbol_filled(fill: Option<vectorcraft_color::Paint>) -> (Session, NodeId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
let ids = [rect(&mut s, 50.0, 50.0, 100.0, 100.0), rect(&mut s, 50.0, 50.0, 10.0, 10.0), rect(&mut s, 95.0, 95.0, 10.0, 10.0)];
|
||||
if let Some(fill) = fill {
|
||||
let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
doc.node_mut(ids[0]).unwrap().appearance = vectorcraft_doc::Appearance::basic(fill, vectorcraft_color::Paint::None, 0.0).into();
|
||||
}
|
||||
s.select(|_, sel| sel.set(ids)).unwrap();
|
||||
let r = s.execute("symbol.new", &json!({"name": "Panel", "nineSlice": true})).unwrap();
|
||||
assert_eq!(r["nineSlice"], json!([0.25, 0.25, 0.25, 0.25]));
|
||||
@@ -350,3 +359,39 @@ fn guides_outside_the_art_survive_saving_and_reopening_a_native_file() {
|
||||
assert!(back.symbol_slice_map(symbol, *xf).is_none(), "{bad}: scaled as a whole");
|
||||
}
|
||||
}
|
||||
|
||||
/// [`panel_symbol`] with a gradient on the frame, so that the frame is drawn in a clip group for
|
||||
/// each cell, its instance resized to 200 × 150 and turned 20°.
|
||||
fn turned_gradient_panel() -> (Session, NodeId) {
|
||||
let gradient = vectorcraft_color::Paint::Gradient(Box::new(vectorcraft_color::GradientPaint::new(vectorcraft_color::Gradient::default())));
|
||||
let (mut s, id) = panel_symbol_filled(Some(gradient));
|
||||
s.execute("object.setBounds", &json!({"x": 50, "y": 50, "width": 200, "height": 150, "reference": 0})).unwrap();
|
||||
s.execute("object.rotate", &json!({"angle": 20})).unwrap();
|
||||
(s, id)
|
||||
}
|
||||
|
||||
/// The bounding box angles of the objects under the art that replaced an instance, and whether
|
||||
/// that art holds a clip group.
|
||||
fn expanded_angles(s: &Session, art: NodeId) -> (Vec<f64>, bool) {
|
||||
let (mut angles, mut clips) = (vec![], false);
|
||||
s.doc().unwrap().doc.node(art).unwrap().walk(&mut |c| {
|
||||
angles.push(c.bbox_angle);
|
||||
clips |= matches!(c.kind, NodeKind::Group { clip: true, .. });
|
||||
});
|
||||
(angles, clips)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn break_link_turns_the_cells_of_a_sliced_instance_with_its_box() {
|
||||
for reset in [false, true] {
|
||||
let (mut s, _) = turned_gradient_panel();
|
||||
if reset {
|
||||
assert_eq!(s.execute("object.resetBoundingBox", &json!({})).unwrap()["changed"], 1);
|
||||
}
|
||||
let r = s.execute("symbol.breakLink", &json!({})).unwrap();
|
||||
let (angles, clips) = expanded_angles(&s, NodeId(r["ids"][0].as_u64().unwrap()));
|
||||
assert!(clips, "the frame is drawn in clip groups");
|
||||
let want = if reset { 0.0 } else { 20.0 };
|
||||
assert!(angles.iter().all(|a| (a - want).abs() < 1e-6), "reset {reset}: {angles:?}");
|
||||
}
|
||||
}
|
||||
@@ -3255,7 +3255,8 @@ fn images_are_written_again_as_the_same_bytes() {
|
||||
|
||||
/// The program of `doc` with LF line ends, and each image's samples (from after `XI` and its line
|
||||
/// end to `%%EndData`, a `%_` before it kept) and each preview's rows (from after `32 17 ` to the
|
||||
/// line end before `%AI26_EndPlacedObjectPreview`) shown as `<n bytes>`.
|
||||
/// line end before `%AI26_EndPlacedObjectPreview` or `%_%AI26_EndPlacedObjectPreview`) shown as
|
||||
/// `<n bytes>`.
|
||||
fn program_shown(doc: &Document) -> String {
|
||||
shown(&export_ai_editing(doc, &opts()).unwrap().program)
|
||||
}
|
||||
@@ -3267,10 +3268,13 @@ fn shown(p: &[u8]) -> String {
|
||||
let mut at = 0;
|
||||
loop {
|
||||
let xi = find(at, b"\r\nXI\r\n").map(|i| (i + 6, b"%%EndData".as_slice()));
|
||||
let pv = find(at, b" 32 17 ").map(|i| (i + 7, b"\r\n%AI26_EndPlacedObjectPreview".as_slice()));
|
||||
let pv = find(at, b" 32 17 ").map(|i| (i + 7, b"%AI26_EndPlacedObjectPreview".as_slice()));
|
||||
let Some((start, end)) = [xi, pv].into_iter().flatten().min_by_key(|(s, _)| *s) else { break };
|
||||
let mut stop = find(start, end).unwrap();
|
||||
if end.starts_with(b"%%") && p.get(stop - 2..stop) == Some(b"%_") {
|
||||
if p.get(stop - 2..stop) == Some(b"%_") {
|
||||
stop -= 2;
|
||||
}
|
||||
if end.starts_with(b"%AI26") && p.get(stop - 2..stop) == Some(b"\r\n") {
|
||||
stop -= 2;
|
||||
}
|
||||
out.extend_from_slice(&p[at..start]);
|
||||
@@ -7354,18 +7358,147 @@ fn an_image_on_hidden_lines_does_not_come_in_beside_its_look() {
|
||||
assert_eq!(art[0].name.as_deref(), Some("Photo"));
|
||||
}
|
||||
|
||||
/// A linked image, an image with an opacity mask, and an image with a Warp and another effect are
|
||||
/// written as their drawn art, with no art style.
|
||||
/// [`warped_photo`] linked to `/Users/someone/Job/art/photo.png`, in folder `art` next to the
|
||||
/// document's.
|
||||
fn linked_warped_photo(doc: &mut Document, effects: Vec<vectorcraft_doc::Effect>) -> Node {
|
||||
let mut n = warped_photo(doc, effects);
|
||||
if let NodeKind::Image(im) = &mut n.kind {
|
||||
let mut link = vectorcraft_doc::LinkInfo::new("/Users/someone/Job/art/photo.png");
|
||||
link.relative = Some("art/photo.png".into());
|
||||
im.link = Some(link);
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
/// A linked image with one Warp is written with an art style of that Warp, its bent pieces as its
|
||||
/// drawn look and the image itself as a placed file on `%_` lines, with its path, folder and
|
||||
/// preview: it reads back as the linked image, its pixels, placement, name, opacity and link as
|
||||
/// they were, with its Warp live and its settings kept.
|
||||
#[test]
|
||||
fn a_linked_image_with_a_warp_reads_back_linked_with_its_warp_live() {
|
||||
// GUARDS: a linked image with a Warp was written as its drawn pieces alone, and read back
|
||||
// without its Warp or its link.
|
||||
let params = serde_json::json!({"bend": 40.0});
|
||||
let doc = photo_doc(|d| linked_warped_photo(d, vec![fx("warp.arc", params.clone())]));
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
assert!(out.warnings.is_empty(), "{:?}", out.warnings);
|
||||
let text = program_shown(&doc);
|
||||
assert!(text.contains("\n%_%AI5_BeginPlace\n"), "{text}");
|
||||
assert!(text.contains("\n%_(/Users/someone/Job/art/photo.png)`\n"), "{text}");
|
||||
assert!(text.contains("\n%_(art/)\n"), "{text}");
|
||||
assert!(text.contains("\n%_8 8 32 32 17 <256 bytes>\n"), "{text}");
|
||||
assert!(text.contains("\n%_%AI5_EndPlace\n") && text.contains("\n1 (Anon 1) XW\n"), "{text}");
|
||||
assert!(!text.contains("BeginRaster\n%_"), "no embedded copy: {text}");
|
||||
let back = read_back(&doc);
|
||||
let art = back.layers[0].children().unwrap();
|
||||
assert_eq!(art.len(), 1, "{:#?}", lines(&back));
|
||||
let (mut want, mut got) = (vec![], vec![]);
|
||||
pictures(&doc, doc.layers[0].children().unwrap(), "", &mut want);
|
||||
pictures(&back, art, "", &mut got);
|
||||
assert_eq!(got, want);
|
||||
let [warp] = art[0].appearance.effects.as_slice() else { panic!("{:?}", art[0].appearance) };
|
||||
assert_eq!(warp.id, "warp.arc");
|
||||
let (w, g) = (vectorcraft_effects::merged_params("warp.arc", ¶ms), vectorcraft_effects::merged_params(&warp.id, &warp.params));
|
||||
for key in ["bend", "horizontal", "vertical"] {
|
||||
assert!((g[key].as_f64().unwrap() - w[key].as_f64().unwrap()).abs() < 6e-3, "{key}: {g} for {w}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A linked image with one Warp larger than a placed file's preview: its copy's preview and its drawn
|
||||
/// look's pieces (placed files of the same image) hold the same smaller preview, so fine detail
|
||||
/// reads back with the Warp live, the link kept and the preview's pixels, as a linked image without
|
||||
/// a Warp does.
|
||||
#[test]
|
||||
fn a_large_linked_image_with_a_warp_reads_back_from_its_preview() {
|
||||
// GUARDS: a linked image with a Warp was written as its drawn pieces alone.
|
||||
let mut doc = Document::new(200.0, 100.0);
|
||||
let layer = doc.layers[0].id;
|
||||
let checks =
|
||||
image::RgbaImage::from_fn(600, 300, |x, y| if (x + y) % 2 == 0 { image::Rgba([255, 0, 0, 255]) } else { image::Rgba([0, 0, 255, 255]) });
|
||||
let mut link = vectorcraft_doc::LinkInfo::new("/Users/someone/Job/art/checks.png");
|
||||
link.relative = Some("art/checks.png".into());
|
||||
let xf = Affine::new([0.25, 0.0, 0.0, 0.25, 20.0, 10.0]);
|
||||
let mut n = image_node(&mut doc, ImageBlob::png(png_of(checks.into())), (600, 300), xf, Some(link));
|
||||
n.appearance = Appearance { effects: vec![fx("warp.arc", serde_json::json!({"bend": 40.0}))], ..Appearance::default() }.into();
|
||||
doc.insert(Some(layer), usize::MAX, n).unwrap();
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
assert!(out.warnings.is_empty(), "{:?}", out.warnings);
|
||||
let text = program_shown(&doc);
|
||||
assert!(text.contains("\n%_256 128 1024 32 17 <131072 bytes>\n"), "the copy's preview");
|
||||
assert!(text.contains("\n256 128 1024 32 17 <131072 bytes>\n"), "the look's pieces' previews");
|
||||
let back = read_back(&doc);
|
||||
let art = back.layers[0].children().unwrap();
|
||||
assert_eq!(art.len(), 1, "{:#?}", lines(&back));
|
||||
let NodeKind::Image(im) = &art[0].kind else { panic!("{:?}", art[0].kind) };
|
||||
assert_eq!((im.width, im.height), (256, 128));
|
||||
assert_eq!(
|
||||
im.link.as_ref().map(|l| (l.path.as_str(), l.relative.as_deref())),
|
||||
Some(("/Users/someone/Job/art/checks.png", Some("art/checks.png")))
|
||||
);
|
||||
assert_eq!(art[0].appearance.effects.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(), ["warp.arc"]);
|
||||
}
|
||||
|
||||
/// A hidden or locked image with one Warp, embedded or linked, keeps its state, its link and its
|
||||
/// live Warp.
|
||||
#[test]
|
||||
fn a_hidden_or_locked_image_with_a_warp_keeps_its_state() {
|
||||
// GUARDS: a linked image with a Warp was written as its drawn pieces alone, and a hidden image's
|
||||
// Warp was compared with a look that drew nothing, so it came in as its drawn pieces.
|
||||
for (what, make) in [("embedded", warped_photo as fn(&mut Document, Vec<vectorcraft_doc::Effect>) -> Node), ("linked", linked_warped_photo)] {
|
||||
for (shown, locked) in [(true, true), (false, false), (false, true)] {
|
||||
let doc = photo_doc(|d| {
|
||||
let mut n = make(d, vec![fx("warp.arc", serde_json::json!({"bend": 40.0}))]);
|
||||
(n.visible, n.locked) = (shown, locked);
|
||||
n
|
||||
});
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
assert!(out.warnings.is_empty(), "{what} {shown} {locked}: {:?}", out.warnings);
|
||||
let back = read_back(&doc);
|
||||
let art = back.layers[0].children().unwrap();
|
||||
assert_eq!(art.len(), 1, "{what} {shown} {locked}: {:#?}", lines(&back));
|
||||
assert_eq!(images_of(&back).len(), 1, "{what} {shown} {locked}");
|
||||
assert_eq!(images_of(&back), images_of(&doc), "{what} {shown} {locked}");
|
||||
assert_eq!(art[0].appearance.effects.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(), ["warp.arc"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A linked image with one Warp in an opacity mask's art keeps its live Warp; its copy on `%_` lines
|
||||
/// is embedded, as linked images in masks are, with that warning.
|
||||
#[test]
|
||||
fn a_linked_image_with_a_warp_in_a_mask_is_embedded_with_its_warp_live() {
|
||||
// GUARDS: the placed file of a styled image written in an opacity mask's art.
|
||||
let doc = photo_doc(|d| {
|
||||
let art = linked_warped_photo(d, vec![fx("warp.arc", serde_json::json!({"bend": 40.0}))]);
|
||||
let mut square =
|
||||
Node::path(d.alloc_id(), shapes::rectangle(Rect::new(20.0, 10.0, 100.0, 90.0)), Appearance::basic(solid(Color::BLACK), Paint::None, 0.0));
|
||||
square.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
|
||||
square
|
||||
});
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
assert_eq!(out.warnings, [writer::LINKS_EMBEDDED]);
|
||||
let text = program_shown(&doc);
|
||||
assert!(!text.contains("BeginPlace") && text.contains("1 (Anon 1) XW"), "{text}");
|
||||
let back = read_back(&doc);
|
||||
let mask = back.layers[0].children().unwrap()[0].extras().mask.as_ref().map(|m| m.art.clone()).expect("the mask");
|
||||
let NodeKind::Image(im) = &mask.kind else { panic!("{:?}", mask.kind) };
|
||||
assert!(im.link.is_none());
|
||||
assert_eq!(mask.appearance.effects.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(), ["warp.arc"]);
|
||||
}
|
||||
|
||||
/// An image with an opacity mask and an image with a Warp and another effect are written as their
|
||||
/// drawn art, with no art style, linked or embedded.
|
||||
#[test]
|
||||
fn other_warped_images_are_written_as_their_drawn_art() {
|
||||
let arc = || fx("warp.arc", serde_json::json!({"bend": 40.0}));
|
||||
let linked = photo_doc(|d| {
|
||||
let mut n = warped_photo(d, vec![arc()]);
|
||||
if let NodeKind::Image(im) = &mut n.kind {
|
||||
im.link = Some(vectorcraft_doc::LinkInfo::new("/art/photo.png"));
|
||||
}
|
||||
let linked_masked = photo_doc(|d| {
|
||||
let mut n = linked_warped_photo(d, vec![arc()]);
|
||||
let white =
|
||||
Node::path(d.alloc_id(), shapes::rectangle(Rect::new(20.0, 10.0, 100.0, 90.0)), Appearance::basic(solid(Color::WHITE), Paint::None, 0.0));
|
||||
n.extras_mut().mask = Some(Box::new(OpacityMask::new(white, false)));
|
||||
n
|
||||
});
|
||||
let linked_stacked = photo_doc(|d| linked_warped_photo(d, vec![arc(), fx("stylize.feather", serde_json::json!({"radius": 2.0}))]));
|
||||
let masked = photo_doc(|d| {
|
||||
let mut n = warped_photo(d, vec![arc()]);
|
||||
let white =
|
||||
@@ -7374,7 +7507,7 @@ fn other_warped_images_are_written_as_their_drawn_art() {
|
||||
n
|
||||
});
|
||||
let stacked = photo_doc(|d| warped_photo(d, vec![arc(), fx("stylize.feather", serde_json::json!({"radius": 2.0}))]));
|
||||
for (what, doc) in [("linked", linked), ("masked", masked), ("stacked", stacked)] {
|
||||
for (what, doc) in [("linked masked", linked_masked), ("linked stacked", linked_stacked), ("masked", masked), ("stacked", stacked)] {
|
||||
assert!(!program_shown(&doc).contains("1 (Anon"), "{what}");
|
||||
let back = read_back(&doc);
|
||||
let art = back.layers[0].children().unwrap();
|
||||
|
||||
@@ -206,6 +206,9 @@ pub(super) struct Writer<'a> {
|
||||
mask_level: Option<(u32, u32)>,
|
||||
/// The image just written was written as a placed file.
|
||||
placed: bool,
|
||||
/// The image being written is the copy of a styled object on the layers, on `%_` lines: linked,
|
||||
/// it is written as a placed file there ([`Self::styled_object`]).
|
||||
styled_copy: bool,
|
||||
/// The type just written was written as type, not as its outlines.
|
||||
typed: bool,
|
||||
/// The objects bound to variables so far, in the order they are written: each one's id, its XML
|
||||
@@ -299,6 +302,7 @@ impl<'a> Writer<'a> {
|
||||
hidden: 0,
|
||||
mask_level: None,
|
||||
placed: false,
|
||||
styled_copy: false,
|
||||
typed: false,
|
||||
bound: vec![],
|
||||
texts: texts::TextDoc::default(),
|
||||
|
||||
@@ -94,7 +94,8 @@ impl Samples {
|
||||
impl Writer<'_> {
|
||||
/// Image object `im` of transparency `xy`: embedded, a raster ([`Self::raster`]); linked, a
|
||||
/// placed file with a preview ([`Self::placed_file`]), except in a part of the data written on
|
||||
/// `%_` lines and in a symbol's art. One whose pixels can't be read is its outline filled mid gray. Where the art is
|
||||
/// `%_` lines (other than a styled object's copy on the layers, [`Self::styled_object`]) and in
|
||||
/// a symbol's art. One whose pixels can't be read is its outline filled mid gray. Where the art is
|
||||
/// cut to a clipping path, the image is in a clipping group of that path.
|
||||
pub(super) fn image(&mut self, im: &ImageObject, xy: Xy) {
|
||||
let doc = self.doc;
|
||||
@@ -125,7 +126,7 @@ impl Writer<'_> {
|
||||
let fit =
|
||||
Affine::scale_non_uniform(f64::from(im.width.max(1)) / f64::from(samples.width), f64::from(im.height.max(1)) / f64::from(samples.height));
|
||||
match &im.link {
|
||||
Some(link) if self.hidden == 0 && !self.in_symbol => {
|
||||
Some(link) if (self.hidden == 0 || self.styled_copy) && !self.in_symbol => {
|
||||
// Cut to a region, the placed file is in a clipping group.
|
||||
self.placed = cut.is_none();
|
||||
self.placed_file(link, &samples, (im.width.max(1), im.height.max(1)), im.xf);
|
||||
|
||||
@@ -42,7 +42,7 @@ pub(in crate::ai_editing) fn style_links(doc: &Document) -> usize {
|
||||
/// The objects of `doc`'s layers and of their opacity masks' art that may be written with an art
|
||||
/// style ([`Writer::styled_object`]), by id (a mask's art root shown, as [`Writer::mask`] writes
|
||||
/// it): paths and compound paths with points and groups, with live effects, linked to a graphic
|
||||
/// style or (paths and compound paths) with a brush on a stroke, and embedded images with one Warp
|
||||
/// style or (paths and compound paths) with a brush on a stroke, and images with one Warp
|
||||
/// ([`warped_image`]), without Opacity & Mask Define
|
||||
/// Knockout Shape (the reader takes an object's transparency
|
||||
/// from its look, which doesn't carry it). A group's members are paths, compound paths or such
|
||||
@@ -100,12 +100,11 @@ fn candidate(n: &Node) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Is `n` an embedded image without an opacity mask whose appearance is one Warp, with no fills or
|
||||
/// strokes that paint and no effects on them: an image the reader reads with its Warp live
|
||||
/// ([`crate::import::read_art_style`]'s `image_warp`)?
|
||||
/// Is `n` an image, embedded or linked, without an opacity mask whose appearance is one Warp, with
|
||||
/// no fills or strokes that paint and no effects on them: an image the reader reads with its Warp
|
||||
/// live ([`crate::import::read_art_style`]'s `image_warp`)?
|
||||
fn warped_image(n: &Node) -> bool {
|
||||
let NodeKind::Image(im) = &n.kind else { return false };
|
||||
im.link.is_none()
|
||||
matches!(n.kind, NodeKind::Image(_))
|
||||
&& n.extras().mask.is_none()
|
||||
&& matches!(n.appearance.effects.as_slice(), [e] if e.id.starts_with("warp."))
|
||||
&& n.appearance.items.iter().all(|i| i.paint().is_none() && i.effects().is_empty())
|
||||
@@ -725,7 +724,8 @@ impl Writer<'_> {
|
||||
/// glows or feathers, the object's transparency
|
||||
/// after the group's end, `0 0 Xd`, `6 () XW`); the object itself on `%_` lines (a path's or
|
||||
/// compound path's points painted with nothing, or with its gradient fill when its style has
|
||||
/// one; a group with its members; an image with its pixels), its name and opacity mask; then
|
||||
/// one; a group with its members; an image with its pixels, or on the layers a linked image as
|
||||
/// a placed file with its path, folder and preview), its name and opacity mask; then
|
||||
/// `1 (name) XW`.
|
||||
/// The look's group and the object itself are counted first ([`Self::take_objects`]): without
|
||||
/// room for them, nothing is written, and the object counts neither as written with its graphic
|
||||
@@ -772,6 +772,9 @@ impl Writer<'_> {
|
||||
// The object, on `%_` lines, in its own state (the look's members may have changed it).
|
||||
self.state(orig.locked, !orig.visible);
|
||||
let saved = std::mem::take(&mut self.body);
|
||||
// On the layers, a linked image's copy is a placed file; in an opacity mask's art, it is
|
||||
// embedded, as other linked images there are.
|
||||
let on_layers = self.hidden == 0;
|
||||
self.hidden += 1;
|
||||
let fit = drawn_outline(n, orig).map(|p| p.bounding_box());
|
||||
// A path's or an image's objects were kept for it above; it counts them itself.
|
||||
@@ -781,7 +784,11 @@ impl Writer<'_> {
|
||||
match &orig.kind {
|
||||
NodeKind::Path { path, rule, .. } => self.styled_outline(orig, path, *rule, fit, xy),
|
||||
NodeKind::Compound { rule, .. } => self.styled_outline(orig, &compound_path(orig), *rule, fit, xy),
|
||||
NodeKind::Image(im) => self.image(&self.doc.shown_image(orig, im), xy),
|
||||
NodeKind::Image(im) => {
|
||||
let was = std::mem::replace(&mut self.styled_copy, on_layers);
|
||||
self.image(&self.doc.shown_image(orig, im), xy);
|
||||
self.styled_copy = was;
|
||||
}
|
||||
NodeKind::Group { children, .. } => {
|
||||
self.line("u");
|
||||
for c in children {
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
//! next layers: a clipping group in each layer, read by [`masks`].
|
||||
//! - Ruler guides: a straight `(N) *` guide path, directly on a layer, that runs across the whole
|
||||
//! canvas (16,383 points around the template box's centre) or from edge to edge of one artboard
|
||||
//! is a ruler guide on that layer ([`Document::guides`]); other guide paths stay paths.
|
||||
//! is a ruler guide on that layer ([`Document::guides`]), shown or hidden, with the path's name,
|
||||
//! lock and visibility; other guide paths stay paths.
|
||||
//! - Groups `u` … `U`, compound paths `*u` … `*U`, clipping groups `q` … `Q` (the path painted
|
||||
//! with `W` clips); a group's transparency and name follow its end. A wraparound group `*w` …
|
||||
//! `*W` (version 7 specification §15.4.2) is a group whose objects other than its type wrap
|
||||
|
||||
@@ -234,11 +234,10 @@ impl<'a> Reader<'a> {
|
||||
}
|
||||
let kind = NodeKind::Path { path, rule: gs.rule, live: None, clipping: clip, guide };
|
||||
let mut node = self.new_node(kind, &gs);
|
||||
// A ruler guide is written as a straight `(N) *` guide path on its layer.
|
||||
// A ruler guide is written as a straight `(N) *` guide path on its layer, shown or hidden.
|
||||
if guide
|
||||
&& op == "N"
|
||||
&& !hidden
|
||||
&& !gs.hidden
|
||||
&& !clip
|
||||
&& self.pattern.is_none()
|
||||
&& matches!(self.frames.last(), Some(Frame { kind: Kind::Layer(_), .. }))
|
||||
|
||||
@@ -1445,7 +1445,8 @@ fn ruler_guide(a: Point, b: Point, centre: Option<(f64, f64)>, boards: &[Rect])
|
||||
}
|
||||
|
||||
/// Makes the children of the layers and sublayers of `nodes` that `pick` makes a guide of into
|
||||
/// ruler guide nodes with the same ids, moved to the top of their layer in their order.
|
||||
/// ruler guide nodes with the same ids, names, locks and visibility, moved to the top of their layer
|
||||
/// in their order.
|
||||
fn take_ruler_guides(nodes: &mut [Arc<Node>], pick: &dyn Fn(NodeId) -> Option<Guide>, depth: usize) {
|
||||
if depth > MAX_DEPTH {
|
||||
return;
|
||||
@@ -1458,7 +1459,11 @@ fn take_ruler_guides(nodes: &mut [Arc<Node>], pick: &dyn Fn(NodeId) -> Option<Gu
|
||||
let mut guides = vec![];
|
||||
kids.retain(|k| match pick(k.id) {
|
||||
Some(g) => {
|
||||
guides.push(Arc::new(Node::new(k.id, NodeKind::RulerGuide { vertical: g.vertical, pos: g.pos, artboard: g.artboard })));
|
||||
let mut n = Node::new(k.id, NodeKind::RulerGuide { vertical: g.vertical, pos: g.pos, artboard: g.artboard });
|
||||
n.name = k.name.clone();
|
||||
n.locked = k.locked;
|
||||
n.visible = k.visible;
|
||||
guides.push(Arc::new(n));
|
||||
false
|
||||
}
|
||||
None => true,
|
||||
|
||||
@@ -1141,7 +1141,8 @@ fn image_warps(object: &Node, st: &crate::import::ai::style::ArtStyle) -> bool {
|
||||
|
||||
/// Does image `object` with `appearance` (its style's Warp) draw as `look`, its drawn look, compared
|
||||
/// over all that the warp draws ([`crate::import::ai::envelope::warped_alike`]) on `layer`, a layer
|
||||
/// id no other node has? Both are drawn shown, opaque and in the Normal mode. Unless the look is on
|
||||
/// id no other node has? Both are drawn shown, opaque and in the Normal mode; a hidden look is drawn
|
||||
/// with every object in it shown, as the hidden object's look holds them. Unless the look is on
|
||||
/// `%_` lines itself (`hidden`), the copies in it that are on `%_` lines (`hidden_ids`) are left out.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn image_warp_alike(
|
||||
@@ -1161,12 +1162,24 @@ fn image_warp_alike(
|
||||
if !hidden {
|
||||
strip(&mut plain, hidden_ids);
|
||||
}
|
||||
if !plain.visible {
|
||||
show_members(&mut plain);
|
||||
}
|
||||
for n in [&mut live, &mut plain] {
|
||||
(n.visible, n.opacity, n.blend) = (true, 1.0, BlendMode::Normal);
|
||||
}
|
||||
crate::import::ai::envelope::warped_alike(&live, &plain, layer, shared, page, spent)
|
||||
}
|
||||
|
||||
/// Show every object in container `n`, at any depth.
|
||||
fn show_members(n: &mut Node) {
|
||||
for c in n.children_mut().into_iter().flatten() {
|
||||
let c = Arc::make_mut(c);
|
||||
c.visible = true;
|
||||
show_members(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// What [`placed_paints`] keys a pattern's name with, apart from the gradients' names.
|
||||
const PATTERN_KEY: &str = "\u{1}pattern ";
|
||||
|
||||
|
||||
@@ -8832,7 +8832,8 @@ fn other_guide_paths_stay_guide_paths() {
|
||||
guide_line(0.0, 30.0, 200.0, 30.0, "S"),
|
||||
guide_line(-7885.0, -7796.0, 8498.0, 8587.0, "N"),
|
||||
format!("u\n{}U\n", guide_line(-7885.0, 40.0, 8498.0, 40.0, "N")),
|
||||
format!("1 Xw\n{}0 Xw\n", guide_line(-7885.0, 40.0, 8498.0, 40.0, "N")),
|
||||
format!("1 Xw\n{}0 Xw\n", guide_line(20.0, 40.0, 80.0, 40.0, "N")),
|
||||
format!("1 A\n{}0 A\n", guide_line(-7885.0, 40.0, 8498.0, 40.0, "S")),
|
||||
"-7885 40 m\n306 40 L\n8498 40 L\n(N) *\n".to_string(),
|
||||
];
|
||||
for body in cases {
|
||||
@@ -8842,6 +8843,35 @@ fn other_guide_paths_stay_guide_paths() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A hidden (`1 Xw`), locked (`1 A`) or named ruler guide path is a ruler guide that keeps its own
|
||||
/// visibility, lock and name, across the canvas or an artboard, on a shown layer.
|
||||
#[test]
|
||||
fn hidden_locked_and_named_guide_paths_are_ruler_guides() {
|
||||
let body = format!(
|
||||
"1 Xw\n{}0 Xw\n1 A\n{}0 A\n{}%_/ArtDictionary :\n%_(Mark) /String (AIArtName) ,\n%_;\n%_\n1 Xw\n{}0 Xw\n",
|
||||
guide_line(-7885.0, 40.0, 8498.0, 40.0, "N"),
|
||||
guide_line(25.0, 8587.0, 25.0, -7796.0, "N"),
|
||||
guide_line(-7885.0, 90.0, 8498.0, 90.0, "N"),
|
||||
guide_line(0.0, 30.0, 200.0, 30.0, "N"),
|
||||
);
|
||||
let doc = read(canvas_stream(&layer(&body)).as_bytes()).unwrap().doc;
|
||||
let top = &doc.layers[0];
|
||||
assert!(top.visible);
|
||||
let kids = top.children().unwrap();
|
||||
assert_eq!(kids.len(), 4);
|
||||
assert!(kids.iter().all(|c| c.is_ruler_guide()), "no guide path is left");
|
||||
let got: Vec<_> = doc.ruler_guides().into_iter().map(|g| (g.vertical, g.pos, g.artboard, g.layer, g.hidden, g.locked, g.name)).collect();
|
||||
let want = [
|
||||
(false, 60.0, None, Some(top.id), true, false, None),
|
||||
(true, 25.0, None, Some(top.id), false, true, None),
|
||||
(false, 10.0, None, Some(top.id), false, false, Some("Mark".to_string())),
|
||||
(false, 70.0, Some(1), Some(top.id), true, false, None),
|
||||
];
|
||||
assert_eq!(got, want);
|
||||
// The nodes themselves carry the state, so the Layers rows show it.
|
||||
assert_eq!(kids.iter().map(|k| (k.visible, k.locked)).collect::<Vec<_>>(), [(false, false), (true, true), (true, false), (false, false)]);
|
||||
}
|
||||
|
||||
/// The layer whose `Lb` has `ops` as its options (the first one in the file).
|
||||
fn layer_with(ops: &str) -> Node {
|
||||
let doc = read_ok(&format!("%AI5_BeginLayer\n{ops} Lb\n(L) Ln\nLB\n%AI5_EndLayer--\n")).doc;
|
||||
|
||||
@@ -2067,3 +2067,17 @@ fn colr_glyphs_of_outlined_type_are_vector_art() {
|
||||
assert!(rasters(&text).is_empty(), "no images: {text}");
|
||||
assert!(text.contains("1 0 0 Xa"), "the red layer: {text}");
|
||||
}
|
||||
|
||||
/// An instance stained blue by its fill writes its symbol's red art in blue, as the canvas draws it.
|
||||
#[test]
|
||||
fn a_stained_instance_is_written_in_its_stained_color() {
|
||||
let mut doc = Document::new(200.0, 100.0);
|
||||
let art = shape(&mut doc, shapes::rectangle(Rect::new(0.0, 0.0, 40.0, 40.0)), red(), Paint::None);
|
||||
doc.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Mark".into(), art: Arc::new(art) });
|
||||
let mut n = Node::new(doc.alloc_id(), NodeKind::SymbolInstance { symbol: "Mark".into(), xf: Affine::translate((50.0, 30.0)) });
|
||||
n.appearance = Appearance::basic(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0).into();
|
||||
let l = doc.layers[0].id;
|
||||
doc.insert(Some(l), 0, n).unwrap();
|
||||
let text = String::from_utf8(write(&doc).bytes).unwrap();
|
||||
assert!(text.contains("0 0 1 Xa") && !text.contains("1 0 0 Xa"), "{text}");
|
||||
}
|
||||
@@ -297,7 +297,7 @@ impl<'a> Writer<'a> {
|
||||
self.warn(LIVE);
|
||||
if let Some(sym) = self.doc.symbols.iter().find(|s| &s.name == symbol)
|
||||
&& self.nest < MAX_NEST
|
||||
&& let Some(mut art) = self.doc.place_symbol_art(symbol, &vectorcraft_doc::symbol_overrides::dynamic_art(&sym.art, n), *xf)
|
||||
&& let Some(mut art) = self.doc.place_symbol_art(symbol, &vectorcraft_doc::symbol_overrides::instance_art(&sym.art, n), *xf)
|
||||
{
|
||||
art.visible = true;
|
||||
self.nest += 1;
|
||||
|
||||
@@ -511,7 +511,7 @@ impl<'a> Scene<'a> {
|
||||
NodeKind::SymbolInstance { symbol, xf } => {
|
||||
if let Some(sym) = self.doc.symbols.iter().find(|s| &s.name == symbol)
|
||||
&& self.nest < MAX_NEST
|
||||
&& let Some(art) = self.doc.place_symbol_art(symbol, &vectorcraft_doc::symbol_overrides::dynamic_art(&sym.art, n), *xf)
|
||||
&& let Some(art) = self.doc.place_symbol_art(symbol, &vectorcraft_doc::symbol_overrides::instance_art(&sym.art, n), *xf)
|
||||
{
|
||||
self.nest += 1;
|
||||
self.node(&art, true);
|
||||
|
||||
@@ -1390,6 +1390,20 @@ fn an_instance_whose_art_reaches_the_region_is_written() {
|
||||
assert!(page.contains("1 0 0 rg"), "{page}");
|
||||
}
|
||||
|
||||
/// An instance stained blue by its fill writes its symbol's red art in blue, as the canvas draws it.
|
||||
#[test]
|
||||
fn a_stained_instance_is_written_in_its_stained_color() {
|
||||
let mut d = doc_with(|d| {
|
||||
let mut n = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Mark".into(), xf: Affine::translate((50.0, 50.0)) });
|
||||
n.appearance = Appearance::basic(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0).into();
|
||||
vec![n]
|
||||
});
|
||||
let art = path(&mut d, shapes::rectangle(Rect::new(0.0, 0.0, 40.0, 40.0)), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Mark".into(), art: Arc::new(art) });
|
||||
let page = page_of(&d, &opts(&d));
|
||||
assert!(page.contains("0 0 1 rg") && !page.contains("1 0 0 rg"), "{page}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_instance_whose_brush_art_reaches_the_region_is_written() {
|
||||
// The symbol's art: a line from (0, 40) to (0, 60) with a 1 pt red stroke painted by a 20 pt
|
||||
|
||||
@@ -284,7 +284,7 @@ fn save_file_warns_about_options_it_does_not_use() {
|
||||
}
|
||||
|
||||
/// The notes on `options` name the `.ai` file the save writes, as the note on a top-level option
|
||||
/// does: a legacy `.ai` for `aiVersion: "7"` (beside `pdfCompatible: false` too), a
|
||||
/// does: a legacy `.ai` for `aiVersion: "7"` (beside `pdfCompatible: false` too, which it notes), a
|
||||
/// non-PDF-compatible `.ai` for `pdfCompatible: false`, and a PDF-compatible `.ai` otherwise, also
|
||||
/// when the save reuses the options the document was last saved with.
|
||||
#[test]
|
||||
@@ -301,8 +301,12 @@ fn save_file_notes_name_the_ai_file_it_writes() {
|
||||
("pdf.ai", json!({"pdfCompatible": true, "preview": true}), "PDF-compatible .ai"),
|
||||
] {
|
||||
let r = text(&call_tool(&mut h, "save_file", &json!({"path": tmp(&dir, name), "version": 3, "options": options})));
|
||||
let expected =
|
||||
[format!("`version` was not used: {label} files don't read it"), format!("`options.preview` was not used: {label} files don't read it")];
|
||||
// A legacy `.ai` doesn't read `pdfCompatible` either.
|
||||
let unread = |k: &str| options.get(k).is_some() && (k != "pdfCompatible" || label.starts_with("legacy"));
|
||||
let expected: Vec<String> = std::iter::once("version".to_string())
|
||||
.chain(["pdfCompatible", "preview"].into_iter().filter(|k| unread(k)).map(|k| format!("options.{k}")))
|
||||
.map(|k| format!("`{k}` was not used: {label} files don't read it"))
|
||||
.collect();
|
||||
assert_eq!(notes(&r), expected, "{name}: {r}");
|
||||
}
|
||||
// A save given no option the file reads writes it as last saved: a legacy `.ai` again.
|
||||
|
||||
@@ -1465,7 +1465,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.action.new",
|
||||
"New Action",
|
||||
"",
|
||||
"{set: index, name, key?: 0-15, cmd?, shift?, color?: red|yellow|green|blue|violet} add an action named name (spaces trimmed from both ends) after the last action of the Actions panel's set at set (0 is the top set) and start recording into it; Stop Playing/Recording (ui.action.stop) ends the recording. key 1 to 15 is the function key F1 to F15 that plays the action (0 or null: none), with cmd (Ctrl, Command on macOS) and shift; color is its Button Mode color (null or \"\": none), which the panel does not show. Action function keys take priority over command shortcuts and preserve the command's binding. On Windows F1, Ctrl+F4 and Ctrl+F6 cannot be used. The set becomes current and the new action is selected. The panel's Create New Action button and menu's New Action… (dialog `actionNew` with set, name, key, cmd, shift and color) run this on Record. While an action is recording, and for an index past the last set, an empty name, a key or color that is not one of these, or an action that would give the set more than 1,000 actions or an action set file larger than 8 MiB, it is an error, nothing is added and no recording starts → {set, action}",
|
||||
"{set: index, name, key?: 0-15, cmd?, shift?, color?: red|yellow|green|blue|violet} add an action named name (spaces trimmed from both ends) after the last action of the Actions panel's set at set (0 is the top set) and start recording into it; Stop Playing/Recording (ui.action.stop) ends the recording. key 1 to 15 is the function key F1 to F15 that plays the action (0 or null: none), with cmd (Ctrl, Command on macOS) and shift; color is its Button Mode color (null or \"\": none), the color of its button in Button Mode (ui.action.buttonMode). Action function keys take priority over command shortcuts and preserve the command's binding. On Windows F1, Ctrl+F4 and Ctrl+F6 cannot be used. The set becomes current and the new action is selected. The panel's Create New Action button and menu's New Action… (dialog `actionNew` with set, name, key, cmd, shift and color) run this on Record. While an action is recording, and for an index past the last set, an empty name, a key or color that is not one of these, or an action that would give the set more than 1,000 actions or an action set file larger than 8 MiB, it is an error, nothing is added and no recording starts → {set, action}",
|
||||
),
|
||||
(
|
||||
"ui.action.options",
|
||||
@@ -1479,6 +1479,18 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"",
|
||||
"{action: name, set?: name} play the Actions panel's action named action at the speed ui.action.playbackOptions sets; set names the set to look in (default: every set). Accelerated plays it as one undoable batch → what command.batch returns. Step by Step and Pause For play its first step now and the others one at a time, each its own undo step, until the last, ui.action.stop, or a step that comes due while a different document is active than after the step before → {results: [the first step's result], pending: the steps left}. An action with a stop, at any speed, plays up to the stop and waits until ui.action.continue or ui.action.stop; at Accelerated the steps between stops are one undoable batch each → {results, pending, atStop?: true when it already waits}. A name no action has, or one that more than one action in the sets looked in has, or an action played while another is still playing step by step or with pauses, is an error and nothing plays",
|
||||
),
|
||||
(
|
||||
"ui.action.playAt",
|
||||
"Play Action",
|
||||
"",
|
||||
"{set: index, action: index} play the Actions panel's action at action in set (0 is the top set) as ui.action.play plays it, and return what ui.action.play returns. A click on an action's button in Button Mode runs this. A missing index, or one that is not a whole number or is past the last set or action, is an error and nothing plays",
|
||||
),
|
||||
(
|
||||
"ui.action.buttonMode",
|
||||
"Button Mode",
|
||||
"",
|
||||
"{enabled?: bool} with true, the Actions panel shows every action of every set, in set order and then action order, as a button with its name in its Action Options color (no color: an ordinary button), without set names, steps or checkmarks, and a click on a button plays its action (ui.action.playAt); with false, it shows its list again; left out or null, it switches to the other view. The panel menu's Button Mode runs this and shows a check mark while it is on. The view is kept with the UI preferences. Turning it on while an action records, or an enabled that is not true, false or null, is an error and the view stays → {enabled}",
|
||||
),
|
||||
(
|
||||
"ui.action.playbackOptions",
|
||||
"Playback Options",
|
||||
|
||||
@@ -217,7 +217,7 @@ impl<'a> Scene<'a> {
|
||||
NodeKind::SymbolInstance { symbol, xf } => {
|
||||
if let Some(sym) = self.doc.symbols.iter().find(|s| &s.name == symbol)
|
||||
&& self.nest < MAX_NEST
|
||||
&& let art = vectorcraft_doc::symbol_overrides::dynamic_art(&sym.art, n)
|
||||
&& let art = vectorcraft_doc::symbol_overrides::instance_art(&sym.art, n)
|
||||
&& let Some(art) =
|
||||
if self.clips { self.doc.place_symbol_art(symbol, &art, *xf) } else { self.doc.place_symbol_art_unclipped(symbol, &art, *xf) }
|
||||
{
|
||||
|
||||
@@ -399,6 +399,22 @@ fn an_instance_whose_art_reaches_the_artboard_is_written() {
|
||||
assert!(k.contains(&39), "a brush fills the art: {k:?}");
|
||||
}
|
||||
|
||||
/// An instance stained blue by its fill writes its symbol's red art in blue, as the canvas draws it,
|
||||
/// in EMF (clipped) and in WMF (unclipped).
|
||||
#[test]
|
||||
fn a_stained_instance_is_written_in_its_stained_color() {
|
||||
let mut inst = Node::new(NodeId(0), NodeKind::SymbolInstance { symbol: "Mark".into(), xf: Affine::translate((30.0, 30.0)) });
|
||||
inst.appearance = Appearance::basic(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0).into();
|
||||
let mut d = doc_with(100.0, 100.0, vec![inst]);
|
||||
let art = path(shapes::rectangle(Rect::new(0.0, 0.0, 40.0, 40.0)), red(), Paint::None, 0.0);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Mark".into(), art: Arc::new(art) });
|
||||
for clips in [true, false] {
|
||||
let (ops, _) = crate::scene::Scene::new(&d, d.artboards[0].rect, clips).run();
|
||||
let colors: Vec<[u8; 3]> = fills(&ops).into_iter().map(|(_, rgb)| rgb).collect();
|
||||
assert_eq!(colors, [[0, 0, 255]], "clips: {clips}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A pattern `name` repeating `art` every point.
|
||||
fn point_tiles(name: &str, art: Node) -> PatternDef {
|
||||
let mut def = PatternDef::new(name, vec![Arc::new(art)]);
|
||||
|
||||
@@ -259,8 +259,9 @@ mod tests {
|
||||
#[test]
|
||||
fn offset_path_sweeps_a_long_low_path_turned_with_the_result_of_the_path_on_end() {
|
||||
let mut rng = Rng(17);
|
||||
// Grown past the zigzag's bends, where the stroker leaves loops whose faces are dropped by
|
||||
// the distance test; grown a little; and inset.
|
||||
// An open zigzag grown by 1.5, a closed one grown by 0.5 and a closed one inset by 0.3. Each
|
||||
// is swept turned when low and as it is when stood on end, and the two results cover the
|
||||
// same region. None of the three leaves a face for the distance test to drop.
|
||||
for (closed, delta) in [(false, 1.5), (true, 0.5), (true, -0.3)] {
|
||||
let what = format!("closed {closed}, offset {delta}");
|
||||
let low = zigzag(3, 300, 0.4, &mut rng, closed);
|
||||
@@ -274,7 +275,8 @@ mod tests {
|
||||
assert_same_region(&turned, &upright(&on_end), &what);
|
||||
}
|
||||
// A long plate with a row of round holes: growing it by 1 closes the narrow holes, which
|
||||
// the distance test drops, and keeps the wide ones.
|
||||
// the distance test drops when it measures each hole's point turned back into the plate's
|
||||
// own frame, and keeps the wide ones.
|
||||
use kurbo::Shape;
|
||||
let mut plate = kurbo::Rect::new(0.0, 0.0, 4000.0, 20.0).to_path(0.1);
|
||||
for i in 0..200u32 {
|
||||
|
||||
@@ -336,14 +336,15 @@ impl Exporter<'_> {
|
||||
// instances multiply.
|
||||
// Placing through 9-slice guides needs room for the most objects it can make
|
||||
// (`NineSliceMap::most_placed`), and the objects it makes count.
|
||||
// A dynamic symbol's instance shows its own appearance changes.
|
||||
// A dynamic symbol's instance shows its own appearance changes, and the instance's
|
||||
// fill stains its art, as on the canvas.
|
||||
let doc = self.doc;
|
||||
if let Some(sym) = doc.symbols.iter().find(|x| &x.name == symbol) {
|
||||
let changed;
|
||||
let art: &Node = if n.extras().symbol_overrides.is_empty() {
|
||||
let art: &Node = if n.extras().symbol_overrides.is_empty() && vectorcraft_doc::symbol_overrides::stain(n).is_none() {
|
||||
&sym.art
|
||||
} else {
|
||||
changed = vectorcraft_doc::symbol_overrides::dynamic_art(&sym.art, n);
|
||||
changed = vectorcraft_doc::symbol_overrides::instance_art(&sym.art, n);
|
||||
&changed
|
||||
};
|
||||
let count = art.count() as u64;
|
||||
|
||||
@@ -271,6 +271,71 @@ fn a_scaled_nine_slice_instance_keeps_its_corners() {
|
||||
assert!(close(boxes[2], Rect::new(105.0, 85.0, 135.0, 105.0), 0.01), "{boxes:?}");
|
||||
}
|
||||
|
||||
/// An instance of symbol `name` placed by `xf`, its fill red at `opacity` (shown when `visible`).
|
||||
fn stained_instance(name: &str, xf: Affine, opacity: f32, visible: bool) -> Node {
|
||||
let mut n = Node::new(NodeId(0), NodeKind::SymbolInstance { symbol: name.into(), xf });
|
||||
n.appearance = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0).into();
|
||||
n.appearance.items.retain(AppearanceItem::is_fill);
|
||||
if let AppearanceItem::Fill(f) = &mut n.appearance.items[0] {
|
||||
(f.opacity, f.visible) = (opacity, visible);
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
/// The fill colors of `d`'s leaves, in paint order.
|
||||
fn leaf_fills(d: &Document) -> Vec<String> {
|
||||
leaves(d).iter().filter_map(|n| n.appearance.fill_paint().color()).map(|c| c.to_hex()).collect()
|
||||
}
|
||||
|
||||
/// A stained instance is written in the colors the canvas draws it in
|
||||
/// ([`vectorcraft_brush::instance_art`]): on its own, with a Transform effect that moves it, inside
|
||||
/// a group, after a dynamic symbol's own fill change, and placed through 9-slice guides. A hidden
|
||||
/// fill or one at 0% opacity leaves the art's colors as they are.
|
||||
#[test]
|
||||
fn a_stained_instance_keeps_its_stain() {
|
||||
let blue = Color::rgb8(30, 90, 220);
|
||||
let art = || Node::group(NodeId(0), vec![Arc::new(rect_node(Rect::new(0.0, 0.0, 100.0, 100.0), blue))]);
|
||||
let canvas = |n: &Node| leaf_fills_of(&vectorcraft_brush::instance_art(&art(), n));
|
||||
fn leaf_fills_of(n: &Node) -> Vec<String> {
|
||||
let mut d = doc(10.0, 10.0);
|
||||
add(&mut d, n.clone());
|
||||
leaf_fills(&d)
|
||||
}
|
||||
let write = |n: Node, nine_slice: bool| {
|
||||
let mut d = doc(300.0, 200.0);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: true, name: "Panel".into(), art: Arc::new(art()) });
|
||||
if nine_slice {
|
||||
d.set_symbol_nine_slice("Panel", Some(vectorcraft_doc::SymbolNineSlice::default()));
|
||||
}
|
||||
add(&mut d, n);
|
||||
leaf_fills(&roundtrip(&d))
|
||||
};
|
||||
let half = stained_instance("Panel", Affine::translate((20.0, 20.0)), 0.5, true);
|
||||
let want = canvas(&half);
|
||||
assert_ne!(want, [blue.to_hex()], "the stain changes the color");
|
||||
assert_eq!(write(half.clone(), false), want, "on its own");
|
||||
let mut moved = half.clone();
|
||||
moved.appearance.effects.push(vectorcraft_doc::Effect {
|
||||
id: "distort.transform".into(),
|
||||
params: serde_json::json!({"moveH": 30.0}),
|
||||
visible: true,
|
||||
});
|
||||
assert_eq!(write(moved, false), want, "with a geometry effect");
|
||||
assert_eq!(write(Node::group(NodeId(0), vec![Arc::new(half.clone())]), false), want, "in a group");
|
||||
let mut scaled = half.clone();
|
||||
scaled.kind = NodeKind::SymbolInstance { symbol: "Panel".into(), xf: Affine::translate((20.0, 20.0)) * Affine::scale_non_uniform(2.0, 1.5) };
|
||||
assert_eq!(write(scaled, true), want, "through 9-slice guides");
|
||||
let mut changed = half.clone();
|
||||
changed.extras_mut().symbol_overrides =
|
||||
vec![vectorcraft_doc::SymbolOverride { path: vec![0], fill: Some(Paint::solid(Color::rgb(0.0, 1.0, 0.0))), ..Default::default() }];
|
||||
let changed_want = canvas(&changed);
|
||||
assert_ne!(changed_want, want, "the part's own fill is stained");
|
||||
assert_eq!(write(changed, false), changed_want, "after a dynamic symbol's change");
|
||||
for (opacity, visible) in [(0.0, true), (0.5, false)] {
|
||||
assert_eq!(write(stained_instance("Panel", Affine::IDENTITY, opacity, visible), false), [blue.to_hex()], "unstained");
|
||||
}
|
||||
}
|
||||
|
||||
/// The placed art of all the 9-slice instances in one export holds at most `MAX_SYMBOL_NODES`
|
||||
/// objects, opacity-mask art included, as on the canvas: of two instances whose art placed through
|
||||
/// the guides holds about 9,009,000 objects each (a gradient square in all nine cells, each copy
|
||||
|
||||
@@ -110,8 +110,8 @@ pub(crate) struct PlacedEntry {
|
||||
pub(crate) art: std::sync::Weak<Node>,
|
||||
/// The art placed, or `None` for art placed again in every frame ([`depends_on_document`]).
|
||||
pub(crate) placed: Option<Arc<Node>>,
|
||||
/// The font generation ([`vectorcraft_text::FontDb::generation`]) the art was placed in, when
|
||||
/// it holds type: the art is placed again when the fonts change.
|
||||
/// The layout inputs ([`vectorcraft_text::layout_inputs`]) the art was placed with, when it
|
||||
/// holds type: the art is placed again when the fonts or the hyphenation exceptions change.
|
||||
pub(crate) fonts: Option<u64>,
|
||||
/// The 9-slice map the art was placed through ([`Renderer::sliced_art`]), or `None` for art
|
||||
/// placed by the transform alone ([`Renderer::placed_art`]). An entry is used only by the
|
||||
|
||||
@@ -412,7 +412,7 @@ impl Renderer {
|
||||
/// brush library it reads; for images and placed documents their pixels and files, the
|
||||
/// layer's Dim Images and the options they are drawn with (with
|
||||
/// [`crate::RenderOptions::kept_images_only`], also which pixels and undecodable notes the
|
||||
/// image store holds); for type the fonts, whether overprinting shows
|
||||
/// image store holds); for type the fonts and the hyphenation exceptions, whether overprinting shows
|
||||
/// ([`crate::proof::overprints`]) and Document Setup's substitution highlights; and the
|
||||
/// plug-ins installed, with the names of the document's swatches and patterns and its pattern
|
||||
/// definitions (a plug-in's painted pieces can name them), when an object in it has a plug-in
|
||||
@@ -457,7 +457,7 @@ impl Renderer {
|
||||
f.doc.setup.highlight_substituted_glyphs,
|
||||
o.highlight_alternate_glyphs,
|
||||
));
|
||||
(vectorcraft_text::FontDb::global().generation(), crate::proof::overprints(f.opts), highlights).hash(&mut h);
|
||||
(vectorcraft_text::layout_inputs(), crate::proof::overprints(f.opts), highlights).hash(&mut h);
|
||||
}
|
||||
if reads.plugins || (doc.plugins && (reads.symbols || reads.patterns)) {
|
||||
(effects::plugin_revision(), doc.names, doc.patterns).hash(&mut h);
|
||||
|
||||
@@ -65,8 +65,8 @@ pub struct Renderer {
|
||||
/// The latest cached glyph geometry for each text object id. A move changes the node's Arc
|
||||
/// but not its glyphs in text space, so the old geometry can still be used after comparison.
|
||||
text_ids: PtrMap<NodeId, usize>,
|
||||
/// The [`vectorcraft_text::FontDb::generation`] `texts` was laid out with: type lays out
|
||||
/// again when fonts are added or rescanned.
|
||||
/// The [`vectorcraft_text::layout_inputs`] `texts` was laid out with: type lays out again
|
||||
/// when fonts are added or rescanned and when the hyphenation exceptions change.
|
||||
text_fonts: u64,
|
||||
/// Clip regions per clipping path (see [`Self::clip_of`]).
|
||||
clips: FrameCache<usize, ClipEntry>,
|
||||
|
||||
@@ -127,8 +127,9 @@ struct Walked {
|
||||
}
|
||||
|
||||
impl Walked {
|
||||
/// The [`resources_key`] of the node walked: the walk's hash, the font generation when it found
|
||||
/// type or a placed file, the plug-in revision and the hash of the document's swatch and pattern
|
||||
/// The [`resources_key`] of the node walked: the walk's hash, the layout inputs (fonts and
|
||||
/// hyphenation exceptions, [`vectorcraft_text::layout_inputs`]) when it found type or a placed
|
||||
/// file, the plug-in revision and the hash of the document's swatch and pattern
|
||||
/// names (`names`) when it found a plug-in effect, and the color settings `cms`. Whether a
|
||||
/// plug-in paints its pieces depends on the plug-ins installed, so the names count for every
|
||||
/// plug-in effect.
|
||||
@@ -136,7 +137,7 @@ impl Walked {
|
||||
let mut h = DefaultHasher::new();
|
||||
self.hash.hash(&mut h);
|
||||
if self.text {
|
||||
vectorcraft_text::FontDb::global().generation().hash(&mut h);
|
||||
vectorcraft_text::layout_inputs().hash(&mut h);
|
||||
}
|
||||
if self.plugins {
|
||||
crate::effects::plugin_revision().hash(&mut h);
|
||||
|
||||
@@ -39,7 +39,8 @@ type TileKey = (String, u64, Ink, bool);
|
||||
|
||||
/// What a tile was drawn from besides its pattern's own art, for art that shows more than itself:
|
||||
/// the art of every pattern and symbol and the pixels of every image in the document, the
|
||||
/// document's other data (which holds its brushes), and the fonts. Editing a symbol leaves the art
|
||||
/// document's other data (which holds its brushes), and the fonts and hyphenation exceptions
|
||||
/// ([`vectorcraft_text::layout_inputs`]). Editing a symbol leaves the art
|
||||
/// of a pattern that shows it as it was, so the art alone kept the tile from before (#1723). Held
|
||||
/// weakly: a weak reference keeps the allocation, so no other value gets one of these addresses
|
||||
/// while the tile is kept.
|
||||
@@ -66,13 +67,13 @@ impl Deps {
|
||||
art: Self::art(doc).map(Arc::downgrade).collect(),
|
||||
pixels: Self::pixels(doc).map(Arc::downgrade).collect(),
|
||||
unknown: vectorcraft_doc::Shared::downgrade(&doc.unknown),
|
||||
fonts: vectorcraft_text::FontDb::global().generation(),
|
||||
fonts: vectorcraft_text::layout_inputs(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Does `doc` hold what the tile was drawn from?
|
||||
fn current(&self, doc: &Document) -> bool {
|
||||
self.fonts == vectorcraft_text::FontDb::global().generation()
|
||||
self.fonts == vectorcraft_text::layout_inputs()
|
||||
&& std::ptr::eq(self.unknown.as_ptr(), &*doc.unknown)
|
||||
&& self.art.iter().map(Weak::as_ptr).eq(Self::art(doc).map(Arc::as_ptr))
|
||||
&& self.pixels.iter().map(Weak::as_ptr).eq(Self::pixels(doc).map(Arc::as_ptr))
|
||||
|
||||
@@ -55,7 +55,7 @@ impl Renderer {
|
||||
/// objects) hit for them. Other art is placed again in every frame, and so is art that holds
|
||||
/// symbol instances, type with inline graphics, images, placed documents or raster effects
|
||||
/// ([`depends_on_document`]), and art that the cache has no room for. Art that holds type is
|
||||
/// placed again when the fonts change.
|
||||
/// placed again when the fonts or the hyphenation exceptions change.
|
||||
pub(super) fn placed_art(&mut self, art: &Arc<Node>, xf: Affine, scale_strokes: bool, inst: Option<&Node>) -> (Arc<Node>, bool) {
|
||||
let kept_kind = matches!(art.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }) || vectorcraft_doc::live::is_live(art);
|
||||
// An instance with appearance changes of its own isn't kept: the key doesn't tell them apart.
|
||||
@@ -67,7 +67,7 @@ impl Renderer {
|
||||
let key = (Arc::as_ptr(art) as usize, xf.as_coeffs().map(f64::to_bits), scale_strokes, stain);
|
||||
if let Some(e) = self.placed_arts.get(&key, self.stamp)
|
||||
&& std::ptr::eq(e.art.as_ptr(), Arc::as_ptr(art))
|
||||
&& e.fonts.is_none_or(|g| g == vectorcraft_text::FontDb::global().generation())
|
||||
&& e.fonts.is_none_or(|g| g == vectorcraft_text::layout_inputs())
|
||||
&& e.slice.is_none()
|
||||
{
|
||||
return match &e.placed {
|
||||
@@ -106,7 +106,7 @@ impl Renderer {
|
||||
if keeps
|
||||
&& let Some(e) = self.placed_arts.get(&key, self.stamp)
|
||||
&& std::ptr::eq(e.art.as_ptr(), Arc::as_ptr(art))
|
||||
&& e.fonts.is_none_or(|g| g == vectorcraft_text::FontDb::global().generation())
|
||||
&& e.fonts.is_none_or(|g| g == vectorcraft_text::layout_inputs())
|
||||
&& e.slice.as_ref() == Some(map)
|
||||
{
|
||||
match e.placed.clone() {
|
||||
@@ -134,7 +134,7 @@ impl Renderer {
|
||||
fn keep_placed(&mut self, key: PlacedKey, art: &Arc<Node>, placed: &Arc<Node>, slice: Option<vectorcraft_doc::NineSliceMap>) -> bool {
|
||||
let kept = (!depends_on_document(art)).then(|| placed.clone());
|
||||
let keeps = kept.is_some();
|
||||
let fonts = (keeps && holds_type(art)).then(|| vectorcraft_text::FontDb::global().generation());
|
||||
let fonts = (keeps && holds_type(art)).then(vectorcraft_text::layout_inputs);
|
||||
// The bytes count the key and the entry, the art's allocation that the weak reference
|
||||
// keeps, and the kept art.
|
||||
let bytes = size_of::<(PlacedKey, PlacedEntry, Node)>() + kept.as_deref().map_or(0, lru::node_bytes);
|
||||
@@ -150,7 +150,7 @@ impl Renderer {
|
||||
if !matches!(&a.kind, NodeKind::Text(own) if std::ptr::eq(&**own, t)) {
|
||||
return (Arc::new(text_geom(t)), false);
|
||||
}
|
||||
let fonts = vectorcraft_text::FontDb::global().generation();
|
||||
let fonts = vectorcraft_text::layout_inputs();
|
||||
if fonts != self.text_fonts {
|
||||
self.texts.clear();
|
||||
self.text_ids.clear();
|
||||
|
||||
@@ -3,16 +3,16 @@
|
||||
|
||||
use std::sync::{PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
use vectorcraft_doc::text::{CharStyle, TextObject};
|
||||
use vectorcraft_doc::text::{CharStyle, TextKind, TextObject};
|
||||
use vectorcraft_doc::{Node, NodeKind};
|
||||
use vectorcraft_geom::Point;
|
||||
use vectorcraft_geom::{PathData, Point};
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// Taken to add fonts, or to look up an installed one by name (`FontDb::face` loads it, which
|
||||
/// changes the fonts too). Renderers lay type out again when the fonts change, so tests of what
|
||||
/// they keep between frames hold it shared ([`fonts_steady`]) and the tests that change the fonts
|
||||
/// hold it alone ([`adding_fonts`]).
|
||||
/// Taken to add fonts, to look up an installed one by name (`FontDb::face` loads it, which
|
||||
/// changes the fonts too) or to change the hyphenation exceptions. Renderers lay type out again
|
||||
/// when the fonts or the exceptions change, so tests of what they keep between frames hold it
|
||||
/// shared ([`fonts_steady`]) and the tests that change them hold it alone ([`adding_fonts`]).
|
||||
static FONTS: RwLock<()> = RwLock::new(());
|
||||
|
||||
/// No fonts are added by these tests until the guard is dropped.
|
||||
@@ -29,14 +29,12 @@ pub(crate) fn adding_fonts() -> RwLockWriteGuard<'static, ()> {
|
||||
/// What `step` returns from a run in which the fonts stayed the same. The fonts can change while
|
||||
/// [`fonts_steady`] is held: the first lookup by family name catalogs the installed fonts, and
|
||||
/// resolving a cataloged family, or a character no loaded font has, loads a face. `step` runs up
|
||||
/// to 100 times, until [`vectorcraft_text::FontDb::generation`] is the same after a run as before
|
||||
/// it.
|
||||
/// to 100 times, until [`vectorcraft_text::layout_inputs`] is the same after a run as before it.
|
||||
pub(crate) fn with_fonts_unchanged<T>(mut step: impl FnMut() -> T) -> T {
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
for _ in 0..100 {
|
||||
let before = db.generation();
|
||||
let before = vectorcraft_text::layout_inputs();
|
||||
let out = step();
|
||||
if db.generation() == before {
|
||||
if vectorcraft_text::layout_inputs() == before {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -466,6 +464,75 @@ fn enveloped_type_under_effects_redraws_when_its_font_becomes_available() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Hyphenated area type redraws with its new line breaks when Preferences › Hyphenation ›
|
||||
/// Exceptions change: its glyph geometry, the pixels of its raster effects, the art drawn for
|
||||
/// geometry effects, the art kept for a symbol instance and an envelope's evaluation are made
|
||||
/// again.
|
||||
#[test]
|
||||
fn hyphenated_type_redraws_when_the_exceptions_change() {
|
||||
let _adding = adding_fonts();
|
||||
struct Clear;
|
||||
impl Drop for Clear {
|
||||
fn drop(&mut self) {
|
||||
vectorcraft_text::set_hyphenation_exceptions("");
|
||||
}
|
||||
}
|
||||
let _clear = Clear;
|
||||
use vectorcraft_doc::Effect;
|
||||
let moved = || Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 0.0}), visible: true };
|
||||
const WORDS: &str = "Extraordinarily comprehensive typographical considerations notwithstanding everything.";
|
||||
const NEVER: &str = "extraordinarily, comprehensive, typographical, considerations, notwithstanding";
|
||||
let cases = ["the area type", "the type with a drop shadow", "the group with a Transform effect", "the symbol instance", "the envelope"];
|
||||
for (i, what) in cases.into_iter().enumerate() {
|
||||
vectorcraft_text::set_hyphenation_exceptions("");
|
||||
let mut d = Document::new(300.0, 150.0);
|
||||
let mut t = TextObject::point(Point::ZERO, WORDS, CharStyle { size: 14.0, ..Default::default() });
|
||||
t.kind = TextKind::Area { frame: PathData::from_bezpath(&Rect::new(10.0, 10.0, 130.0, 145.0).to_path(0.1)) };
|
||||
t.xf = Affine::IDENTITY;
|
||||
t.para.hyphenate = true;
|
||||
let mut object = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
|
||||
match i {
|
||||
0 => {}
|
||||
1 => {
|
||||
let params = serde_json::json!({"x": 4, "y": 4, "blur": 1, "opacity": 100});
|
||||
object.appearance.effects.push(Effect { id: "stylize.dropShadow".into(), params, visible: true });
|
||||
}
|
||||
2 => {
|
||||
object = Node::group(d.alloc_id(), vec![Arc::new(object)]);
|
||||
object.appearance.effects.push(moved());
|
||||
}
|
||||
3 => {
|
||||
object.appearance.effects.push(moved());
|
||||
let art = Node::group(d.alloc_id(), vec![Arc::new(object)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Words".into(), art: Arc::new(art) });
|
||||
object = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Words".into(), xf: Affine::IDENTITY });
|
||||
}
|
||||
_ => {
|
||||
let kind = vectorcraft_doc::EnvelopeKind::Warp { style: "arc".into(), bend: 30.0, h: 0.0, v: 0.0, horizontal: true, version: 0 };
|
||||
let envelope = NodeKind::Envelope {
|
||||
content: vec![Arc::new(object)],
|
||||
kind: Box::new(kind),
|
||||
fidelity: 50.0,
|
||||
editing: false,
|
||||
options: Default::default(),
|
||||
frame: Box::new(Affine::IDENTITY),
|
||||
};
|
||||
object = Node::new(d.alloc_id(), envelope);
|
||||
}
|
||||
}
|
||||
let l = d.layers[0].id;
|
||||
d.insert(Some(l), 0, object).unwrap();
|
||||
let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
let mut r = Renderer::new();
|
||||
let before = r.render(&d, 300, 150, Affine::IDENTITY, &opts).pixels;
|
||||
vectorcraft_text::set_hyphenation_exceptions(NEVER);
|
||||
let after = r.render(&d, 300, 150, Affine::IDENTITY, &opts).pixels;
|
||||
let fresh = Renderer::new().render(&d, 300, 150, Affine::IDENTITY, &opts).pixels;
|
||||
assert_ne!(before, fresh, "{what}: the words in the list are not broken");
|
||||
assert!(after == fresh, "{what}: drawn as a new renderer draws it");
|
||||
}
|
||||
}
|
||||
|
||||
/// Without an engine session, a renderer maps an envelope of type from its glyph outline bounds,
|
||||
/// and measures the type again when its font becomes available.
|
||||
#[test]
|
||||
|
||||
@@ -320,13 +320,13 @@ fn kept_glyphs(r: &mut Renderer, d: &Document) -> Vec<Option<Arc<TextGeom>>> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `f`'s result from a run during which the fonts did not change: other tests add fonts, and the
|
||||
/// renderer lays all type out again when the fonts change.
|
||||
/// `f`'s result from a run during which the fonts and the hyphenation exceptions did not change:
|
||||
/// other tests change them, and the renderer lays all type out again when they change.
|
||||
fn fonts_unchanged<T>(mut f: impl FnMut() -> T) -> T {
|
||||
loop {
|
||||
let fonts = vectorcraft_text::FontDb::global().generation();
|
||||
let inputs = vectorcraft_text::layout_inputs();
|
||||
let t = f();
|
||||
if vectorcraft_text::FontDb::global().generation() == fonts {
|
||||
if vectorcraft_text::layout_inputs() == inputs {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -859,7 +859,7 @@ fn type_outside_its_frame_is_drawn() {
|
||||
fn a_pan_draws_the_type_from_the_kept_pixels() {
|
||||
let _fonts = crate::tests_fontchange::fonts_steady();
|
||||
let opts = RenderOptions::default();
|
||||
let fonts = || vectorcraft_text::FontDb::global().generation();
|
||||
let fonts = vectorcraft_text::layout_inputs;
|
||||
// How many type objects a renderer drew offscreen for `b` in `second` after drawing `a` in
|
||||
// `first`. The second frame's pixels are a new renderer's, each channel within `near`. The
|
||||
// frames are drawn again while the fonts change between them (other tests load fonts, and the
|
||||
|
||||
+168
-15
@@ -110,8 +110,25 @@ struct ElemStyle {
|
||||
word_spacing: f64,
|
||||
/// `paint-order` paints the stroke before the fill.
|
||||
stroke_first: bool,
|
||||
/// What its characters' Character Direction ([`Ctx::elem`]) leaves out of the file's
|
||||
/// `unicode-bidi`, for the import's warnings.
|
||||
bidi_notes: Vec<&'static str>,
|
||||
}
|
||||
|
||||
/// An override (`unicode-bidi="bidi-override"`) read as an embedding.
|
||||
const BIDI_OVERRIDE: &str = "unicode-bidi=\"bidi-override\" imported as an embedding; directional override is not supported";
|
||||
/// An isolate (`unicode-bidi="isolate"`) read as an embedding.
|
||||
const BIDI_ISOLATE: &str = "unicode-bidi=\"isolate\" imported as an embedding; isolation is not supported";
|
||||
/// `unicode-bidi="isolate-override"` read as an embedding.
|
||||
const BIDI_ISOLATE_OVERRIDE: &str = "unicode-bidi=\"isolate-override\" imported as an embedding; isolation and override are not supported";
|
||||
/// `unicode-bidi="plaintext"` read as an embedding in the direction of its first strong character.
|
||||
const BIDI_PLAINTEXT: &str =
|
||||
"unicode-bidi=\"plaintext\" imported as an embedding in the direction of its first strong character; isolation is not supported";
|
||||
/// An embedding inside another, kept as an embedding of its own.
|
||||
const BIDI_NESTED: &str = "a unicode-bidi embedding inside another imported one level deep; its characters may be ordered differently";
|
||||
/// An override on the `<text>` itself, which keeps only its paragraph direction.
|
||||
const BIDI_TEXT_OVERRIDE: &str = "unicode-bidi override on the text imported as its paragraph direction; directional override is not supported";
|
||||
|
||||
/// One character of a text and the adjustments its position needs.
|
||||
struct Cell {
|
||||
ch: char,
|
||||
@@ -694,6 +711,9 @@ struct Ctx {
|
||||
sizes: RefCell<HashMap<roxmltree::NodeId, f64>>,
|
||||
weights: RefCell<HashMap<roxmltree::NodeId, u16>>,
|
||||
elems: RefCell<HashMap<roxmltree::NodeId, Rc<ElemStyle>>>,
|
||||
/// Whether the first strong character of each element read so far is right to left
|
||||
/// ([`Self::first_strong_rtl`]); `None` where it holds none.
|
||||
strong: RefCell<HashMap<roxmltree::NodeId, Option<bool>>>,
|
||||
/// The path each `<textPath>` read so far follows ([`Self::text_path`]); `None` where it has
|
||||
/// no usable one. The styles of a `<use>` instance share it, so each path is found and parsed
|
||||
/// once however many `<use>` elements show its text.
|
||||
@@ -716,6 +736,7 @@ impl Ctx {
|
||||
sizes: RefCell::default(),
|
||||
weights: RefCell::default(),
|
||||
elems: RefCell::default(),
|
||||
strong: RefCell::default(),
|
||||
paths: Rc::default(),
|
||||
};
|
||||
let root = xml.root_element();
|
||||
@@ -737,6 +758,7 @@ impl Ctx {
|
||||
sizes: RefCell::default(),
|
||||
weights: RefCell::default(),
|
||||
elems: RefCell::default(),
|
||||
strong: RefCell::default(),
|
||||
paths: self.paths.clone(),
|
||||
}
|
||||
}
|
||||
@@ -915,6 +937,8 @@ impl Ctx {
|
||||
};
|
||||
let parent = if n.tag_name().name() == "text" { 0.0 } else { n.parent_element().map_or(0.0, |p| self.elem(p).style.baseline_shift) };
|
||||
st.baseline_shift = parent + own;
|
||||
let (char_direction, bidi_notes) = self.char_direction(n);
|
||||
st.char_direction = char_direction;
|
||||
if self.prop(n, "visibility").is_some_and(|v| v == "hidden" || v == "collapse") {
|
||||
(st.fill, st.stroke, fill_url, stroke_url) = (Paint::None, Paint::None, None, None);
|
||||
}
|
||||
@@ -922,11 +946,93 @@ impl Ctx {
|
||||
let order = self.prop(n, "paint-order").and_then(|v| svgtypes::PaintOrder::from_str(&v).ok()).unwrap_or_default().order;
|
||||
let at = |k| order.iter().position(|o| *o == k);
|
||||
let stroke_first = at(svgtypes::PaintOrderKind::Stroke) < at(svgtypes::PaintOrderKind::Fill);
|
||||
let e = Rc::new(ElemStyle { style: st, fill: fill_url, stroke: stroke_url, word_spacing, stroke_first });
|
||||
let e = Rc::new(ElemStyle { style: st, fill: fill_url, stroke: stroke_url, word_spacing, stroke_first, bidi_notes });
|
||||
self.elems.borrow_mut().insert(n.id(), e.clone());
|
||||
e
|
||||
}
|
||||
|
||||
/// The computed `unicode-bidi` of element `n`, in lowercase. It is not inherited: `normal`
|
||||
/// unless `n` sets it, and its parent's value for `inherit`.
|
||||
fn unicode_bidi(&self, n: XNode) -> String {
|
||||
let mut at = Some(n);
|
||||
while let Some(a) = at {
|
||||
match self.css.own(a, "unicode-bidi").map(|v| v.trim().to_ascii_lowercase()) {
|
||||
Some(v) if v == "inherit" => at = self.parent(a),
|
||||
Some(v) => return v,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
"normal".into()
|
||||
}
|
||||
|
||||
/// The Character Direction of the characters of element `n` inside a `<text>`, and what it
|
||||
/// leaves out (SVG 1.1 §10.7.4,
|
||||
/// https://www.w3.org/TR/SVG11/text.html#RelationshipWithBi-directionality, and CSS Writing
|
||||
/// Modes 3 §2.2, https://www.w3.org/TR/css-writing-modes-3/#unicode-bidi). A `<tspan>`,
|
||||
/// `<textPath>` or `<a>` whose `unicode-bidi` opens an embedding gives its characters an
|
||||
/// embedding in its `direction`; one that opens none (`normal`) leaves them in its parent's.
|
||||
/// The text model holds one level of embedding and no override or isolation: overrides and
|
||||
/// isolates become embeddings, `plaintext` an embedding in the direction of its first strong
|
||||
/// character, and an embedding inside another an embedding of its own, each with a note. The
|
||||
/// `<text>` itself sets the paragraph direction ([`pin_direction`]) and no Character Direction.
|
||||
fn char_direction(&self, n: XNode) -> (Option<ParaDirection>, Vec<&'static str>) {
|
||||
let name = n.tag_name().name();
|
||||
let bidi = self.unicode_bidi(n);
|
||||
if !matches!(name, "tspan" | "textPath" | "a") {
|
||||
let notes =
|
||||
if name == "text" && matches!(bidi.as_str(), "bidi-override" | "isolate-override") { vec![BIDI_TEXT_OVERRIDE] } else { vec![] };
|
||||
return (None, notes);
|
||||
}
|
||||
let parent = n.parent_element().map(|p| self.elem(p));
|
||||
let inherited = parent.as_ref().and_then(|p| p.style.char_direction);
|
||||
let mut notes = parent.map_or_else(Vec::new, |p| p.bidi_notes.clone());
|
||||
let rtl = |rtl: bool| Some(if rtl { ParaDirection::RightToLeft } else { ParaDirection::LeftToRight });
|
||||
let own = || rtl(self.prop(n, "direction").is_some_and(|d| d.trim() == "rtl"));
|
||||
let (dir, note) = match bidi.as_str() {
|
||||
"embed" => (own(), None),
|
||||
"bidi-override" => (own(), Some(BIDI_OVERRIDE)),
|
||||
"isolate" => (own(), Some(BIDI_ISOLATE)),
|
||||
"isolate-override" => (own(), Some(BIDI_ISOLATE_OVERRIDE)),
|
||||
"plaintext" => (rtl(self.first_strong_rtl(n) == Some(true)), Some(BIDI_PLAINTEXT)),
|
||||
// `normal`, and a value CSS ignores.
|
||||
_ => return (inherited, notes),
|
||||
};
|
||||
for note in note.into_iter().chain(inherited.map(|_| BIDI_NESTED)) {
|
||||
if !notes.contains(¬e) {
|
||||
notes.push(note);
|
||||
}
|
||||
}
|
||||
(dir, notes)
|
||||
}
|
||||
|
||||
/// Whether the first strong character (UAX #9 P2) of the characters [`Self::collect`] reads
|
||||
/// from element `n` is right to left; `None` when they hold none. Each element is read once.
|
||||
fn first_strong_rtl(&self, n: XNode) -> Option<bool> {
|
||||
if let Some(s) = self.strong.borrow().get(&n.id()) {
|
||||
return *s;
|
||||
}
|
||||
let s = n.children().find_map(|c| {
|
||||
if c.is_text() {
|
||||
text_first_strong_rtl(c.text().unwrap_or(""))
|
||||
} else if self.is_inline_child(c) {
|
||||
self.first_strong_rtl(c)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
self.strong.borrow_mut().insert(n.id(), s);
|
||||
s
|
||||
}
|
||||
|
||||
/// Whether child `c` of a `<text>` content element holds characters of the text: a shown
|
||||
/// `<tspan>`, `<textPath>` or `<a>`.
|
||||
fn is_inline_child(&self, c: XNode) -> bool {
|
||||
c.is_element()
|
||||
&& c.tag_name().namespace() == Some(SVG_NS)
|
||||
&& matches!(c.tag_name().name(), "tspan" | "textPath" | "a")
|
||||
&& self.css.own(c, "display").as_deref() != Some("none")
|
||||
}
|
||||
|
||||
/// Characters of `el` and its `<tspan>`, `<textPath>` and `<a>` children, with white space
|
||||
/// handled like `xml:space` (newlines and tabs become spaces; runs of spaces collapse unless
|
||||
/// preserved).
|
||||
@@ -943,11 +1049,7 @@ impl Ctx {
|
||||
}
|
||||
out.chars.push((ch, el));
|
||||
}
|
||||
} else if c.is_element()
|
||||
&& c.tag_name().namespace() == Some(SVG_NS)
|
||||
&& matches!(c.tag_name().name(), "tspan" | "textPath" | "a")
|
||||
&& self.css.own(c, "display").as_deref() != Some("none")
|
||||
{
|
||||
} else if self.is_inline_child(c) {
|
||||
if c.tag_name().name() == "textPath" {
|
||||
out.path.get_or_insert(c);
|
||||
}
|
||||
@@ -1234,6 +1336,16 @@ impl Ctx {
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
// What the Character Direction leaves out of the file's `unicode-bidi`, once for the text.
|
||||
let mut bidi_notes: Vec<&str> = vec![];
|
||||
for (el, _) in cs.spans.iter().filter(|(_, r)| !r.is_empty() && r.start < n) {
|
||||
for note in &self.elem(*el).bidi_notes {
|
||||
if !bidi_notes.contains(note) {
|
||||
bidi_notes.push(note);
|
||||
}
|
||||
}
|
||||
}
|
||||
warnings.extend(bidi_notes.into_iter().map(|note| format!("{label}: {note}")));
|
||||
let under = stroke_under(&mut cells, label, warnings);
|
||||
|
||||
let path = cs.path.and_then(|tp| match self.text_path(xml, tp) {
|
||||
@@ -1244,7 +1356,16 @@ impl Ctx {
|
||||
}
|
||||
});
|
||||
let vertical = self.prop(t, "writing-mode").is_some_and(|m| m.starts_with("tb") || m.starts_with("vertical"));
|
||||
let rtl = self.prop(t, "direction").is_some_and(|d| d.trim() == "rtl");
|
||||
// With `unicode-bidi: plaintext`, the text runs in the direction of its first strong
|
||||
// character, and each paragraph in that of its own ([`pin_first_strong`]).
|
||||
let plaintext = self.unicode_bidi(t) == "plaintext";
|
||||
let rtl = if plaintext { self.first_strong_rtl(t) == Some(true) } else { self.prop(t, "direction").is_some_and(|d| d.trim() == "rtl") };
|
||||
let pin = |obj: &mut TextObject| {
|
||||
pin_direction(obj, rtl);
|
||||
if plaintext {
|
||||
pin_first_strong(obj);
|
||||
}
|
||||
};
|
||||
// Where the chunk starting at character `i` sits on its anchor: 0 its left (or top) edge,
|
||||
// 0.5 its middle, 1 its right (or bottom) edge; on a path, its edge toward the path's
|
||||
// start, its middle or its edge toward the path's end. In horizontal type `start` and
|
||||
@@ -1258,7 +1379,15 @@ impl Ctx {
|
||||
Some("end") => 1.0,
|
||||
_ => 0.0,
|
||||
};
|
||||
if !vertical && self.prop(el, "direction").is_some_and(|d| d.trim() == "rtl") { 1.0 - a } else { a }
|
||||
// With `unicode-bidi: plaintext` on the `<text>`, a chunk runs in the direction of its
|
||||
// first strong character, left to right without one ([`pin_first_strong`]).
|
||||
let chunk_rtl = || {
|
||||
let end = (i + 1..n).find(|&j| x[j].is_some() || y[j].is_some()).unwrap_or(n);
|
||||
let chunk: String = cs.chars[i..end].iter().map(|c| c.0).collect();
|
||||
text_first_strong_rtl(&chunk) == Some(true)
|
||||
};
|
||||
let rtl = if plaintext { chunk_rtl() } else { self.prop(el, "direction").is_some_and(|d| d.trim() == "rtl") };
|
||||
if !vertical && rtl { 1.0 - a } else { a }
|
||||
};
|
||||
let anchor = anchor_of(0);
|
||||
self.fit_lengths(&cs.spans, &mut cells, vertical, label, warnings);
|
||||
@@ -1306,7 +1435,7 @@ impl Ctx {
|
||||
obj.xf = Affine::IDENTITY;
|
||||
} else {
|
||||
let origin = Point::new(x[0].unwrap_or(0.0) + dx[0], y[0].unwrap_or(0.0) + dy[0]);
|
||||
let (justify, left, starts) = lines(&mut cells, &x, &y, &dx, &dy, &anchor_of, rtl, origin.x, &mut breaks);
|
||||
let (justify, left, starts) = lines(&mut cells, &x, &y, &dx, &dy, &anchor_of, &pin, origin.x, &mut breaks);
|
||||
obj.para.justify = justify;
|
||||
obj.xf = Affine::translate((finite(left), finite(origin.y)));
|
||||
// Lines anchored differently or at different x (paragraphs aligned or indented
|
||||
@@ -1332,7 +1461,7 @@ impl Ctx {
|
||||
if !para_styles.is_empty() {
|
||||
obj.set_paragraph_styles(para_styles);
|
||||
}
|
||||
pin_direction(&mut obj, rtl);
|
||||
pin(&mut obj);
|
||||
keep_reader_anchors(&mut obj);
|
||||
// The placeholder covers the text (laid out when a paint server needs its bounding box).
|
||||
let b = if paints.is_empty() { obj.bounds() } else { Some(obj.xf.transform_rect_bbox(measure(&obj).bounds)) }.unwrap_or_default();
|
||||
@@ -1437,9 +1566,9 @@ fn leading_tolerance(spacing: f64) -> f64 {
|
||||
/// Point type: line breaks, baseline shifts and kerning from the positioning lists. Returns the
|
||||
/// justification, the x of the first line's left (or anchor) edge and each line's first
|
||||
/// character, alignment (as for [`justify_of`]) and anchor x. `anchor_of` gives where the chunk
|
||||
/// starting at a character sits on its anchor, and `rtl` whether the text's `direction` is `rtl`
|
||||
/// ([`pin_direction`]). When characters are placed on their own, every line is left-aligned and
|
||||
/// its anchor x is its left edge.
|
||||
/// starting at a character sits on its anchor, and `pin` sets the paragraph directions of the
|
||||
/// text ([`pin_direction`]). When characters are placed on their own, every line is left-aligned
|
||||
/// and its anchor x is its left edge.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn lines(
|
||||
cells: &mut [Cell],
|
||||
@@ -1448,7 +1577,7 @@ fn lines(
|
||||
dx: &[f64],
|
||||
dy: &[f64],
|
||||
anchor_of: &dyn Fn(usize) -> f64,
|
||||
rtl: bool,
|
||||
pin: &dyn Fn(&mut TextObject),
|
||||
x0: f64,
|
||||
breaks: &mut Vec<Break>,
|
||||
) -> (Justify, f64, Vec<(usize, f64, f64)>) {
|
||||
@@ -1519,7 +1648,7 @@ fn lines(
|
||||
for round in 0..4 {
|
||||
let (runs, bytes) = assemble(cells, breaks);
|
||||
let mut text = with_runs(runs.0);
|
||||
pin_direction(&mut text, rtl);
|
||||
pin(&mut text);
|
||||
let (pens, ends) = pens(&measure(&text), &bytes);
|
||||
// The edges of each line's first chunk, found once a round.
|
||||
let first: Vec<Option<(f64, f64)>> = lines.iter().map(|&(s, _)| edges(s, &pens, &ends)).collect();
|
||||
@@ -1621,6 +1750,30 @@ fn pin_direction(obj: &mut TextObject, rtl: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
/// `unicode-bidi: plaintext` on a `<text>` (CSS Writing Modes 3 §2.2,
|
||||
/// https://www.w3.org/TR/css-writing-modes-3/#unicode-bidi): each paragraph of `obj` is pinned to
|
||||
/// the direction of its first strong character, and left to right without one (UAX #9 P2, P3).
|
||||
fn pin_first_strong(obj: &mut TextObject) {
|
||||
let text = obj.plain_text();
|
||||
let mut styles = obj.paragraph_styles();
|
||||
for (para, style) in text.split('\n').zip(styles.iter_mut()) {
|
||||
let rtl = text_first_strong_rtl(para) == Some(true);
|
||||
style.direction = Some(if rtl { ParaDirection::RightToLeft } else { ParaDirection::LeftToRight });
|
||||
}
|
||||
obj.set_paragraph_styles(styles);
|
||||
}
|
||||
|
||||
/// Whether the first strong character (UAX #9 P2) of `text` is right to left; `None` when it holds
|
||||
/// none. Paragraph separators count as spaces, so all of `text` is searched.
|
||||
fn text_first_strong_rtl(text: &str) -> Option<bool> {
|
||||
let line: String = text.chars().map(|c| if matches!(c, '\n' | '\r' | '\u{1C}'..='\u{1E}' | '\u{85}' | '\u{2029}') { ' ' } else { c }).collect();
|
||||
if vectorcraft_text::paragraph_is_rtl(&line, None) {
|
||||
return Some(true);
|
||||
}
|
||||
// Without a strong character of its own, a right-to-left letter after it is the first one.
|
||||
(!vectorcraft_text::paragraph_is_rtl(&format!("{line}\u{5D0}"), None)).then_some(false)
|
||||
}
|
||||
|
||||
/// A left-aligned point text of `runs` at the origin (for measuring).
|
||||
fn with_runs(runs: Vec<TextRun>) -> TextObject {
|
||||
let mut t = TextObject::point(Point::ZERO, "", CharStyle::default());
|
||||
|
||||
@@ -391,6 +391,11 @@ fn rtl_point_text_start_and_end_use_directional_edges() {
|
||||
(r#"<text x="200" y="60" direction="rtl" font-size="20"><tspan direction="ltr">Wave</tspan></text>"#, 0.0),
|
||||
(r#"<text x="200" y="60" text-anchor="start" font-size="20">Wave</text>"#, 0.0),
|
||||
(r#"<text x="200" y="60" direction="ltr" text-anchor="end" font-size="20">Wave</text>"#, 1.0),
|
||||
// With `unicode-bidi: plaintext`, the direction of the chunk's first strong character.
|
||||
(r#"<text x="200" y="60" unicode-bidi="plaintext" font-size="20">אבג</text>"#, 1.0),
|
||||
(r#"<text x="200" y="60" unicode-bidi="plaintext" text-anchor="end" font-size="20">12 אב</text>"#, 0.0),
|
||||
(r#"<text x="200" y="60" direction="rtl" unicode-bidi="plaintext" font-size="20">Wave</text>"#, 0.0),
|
||||
(r#"<text x="200" y="60" direction="rtl" unicode-bidi="plaintext" text-anchor="end" font-size="20">12</text>"#, 1.0),
|
||||
] {
|
||||
let d = open(body);
|
||||
let (left, right) = line_edges(only_text(&d))[0];
|
||||
@@ -464,6 +469,169 @@ fn rtl_lines_use_each_chunk_first_character_direction_and_anchor() {
|
||||
assert!(close((e[3].0 + e[3].1) / 2.0, 200.0, 1e-6), "a middle line is centered on 200: {e:?}");
|
||||
}
|
||||
|
||||
/// The Character Direction of the run holding `part`.
|
||||
fn char_direction_of(t: &TextObject, part: &str) -> Option<vectorcraft_doc::ParaDirection> {
|
||||
t.runs.iter().find(|r| r.text.contains(part)).unwrap_or_else(|| panic!("no run holds {part:?}: {:?}", t.runs)).style.char_direction
|
||||
}
|
||||
|
||||
/// SVG 1.1 §10.7.4: a `<tspan>` whose `unicode-bidi` is `embed` or `bidi-override` opens a bidi
|
||||
/// embedding in its `direction`; with `normal`, its `direction` leaves the ordering alone. The
|
||||
/// embedding becomes the Character Direction of its characters, and the `<text>` keeps its own
|
||||
/// paragraph direction. https://www.w3.org/TR/SVG11/text.html#RelationshipWithBi-directionality
|
||||
#[test]
|
||||
fn a_tspan_bidi_embedding_becomes_its_character_direction() {
|
||||
use vectorcraft_doc::ParaDirection::{LeftToRight as Ltr, RightToLeft as Rtl};
|
||||
let read = |body: &str| {
|
||||
let (d, w) = import_with_report(&svg(body)).unwrap_or_else(|e| panic!("{e}"));
|
||||
(only_text(&d).clone(), w)
|
||||
};
|
||||
// (outer direction, inner direction, unicode-bidi, the inner run's Character Direction, a warning)
|
||||
for (outer, inner, bidi, want, warns) in [
|
||||
("ltr", "rtl", "embed", Some(Rtl), false),
|
||||
("rtl", "ltr", "embed", Some(Ltr), false),
|
||||
("ltr", "rtl", "bidi-override", Some(Rtl), true),
|
||||
("rtl", "ltr", "bidi-override", Some(Ltr), true),
|
||||
("ltr", "rtl", "normal", None, false),
|
||||
("rtl", "ltr", "normal", None, false),
|
||||
("ltr", "rtl", "EMBED", Some(Rtl), false),
|
||||
("ltr", "rtl", "sideways", None, false),
|
||||
] {
|
||||
let (t, w) = read(&format!(
|
||||
r#"<text x="20" y="40" font-size="20" direction="{outer}">ab <tspan direction="{inner}" unicode-bidi="{bidi}">1 2</tspan> cd</text>"#
|
||||
));
|
||||
let case = format!("{outer} text, {inner} {bidi} tspan");
|
||||
assert_eq!(t.plain_text(), "ab 1 2 cd", "{case}: the characters stay in logical order");
|
||||
assert_eq!(char_direction_of(&t, "1 2"), want, "{case}: {:?}", t.runs);
|
||||
assert_eq!(char_direction_of(&t, "ab"), None, "{case}");
|
||||
assert_eq!(t.para_at(0).direction == Some(Rtl), outer == "rtl", "{case}: the paragraph keeps the text's direction");
|
||||
assert_eq!(w.iter().any(|w| w.contains("bidi-override") && w.contains("not supported")), warns, "{case}: {w:?}");
|
||||
if !warns {
|
||||
assert!(w.is_empty(), "{case}: {w:?}");
|
||||
}
|
||||
}
|
||||
// In left-to-right text, digits in a right-to-left embedding are drawn right to left.
|
||||
let order = |bidi: &str| {
|
||||
let (t, _) = read(&format!(r#"<text x="20" y="40" font-size="20">ab <tspan direction="rtl" unicode-bidi="{bidi}">1 2</tspan></text>"#));
|
||||
let lay = layout(FontDb::global(), &t);
|
||||
let x = |byte: usize| lay.glyphs.iter().find(|g| g.byte == byte).map(|g| g.origin.x).unwrap_or_else(|| panic!("no glyph at {byte}"));
|
||||
x(3) < x(5)
|
||||
};
|
||||
assert!(!order("embed"), "an embedding draws 2 before 1");
|
||||
assert!(order("normal"), "without one, 1 comes first");
|
||||
}
|
||||
|
||||
/// `unicode-bidi` is not inherited: a `<tspan>` without one stays in its parent's embedding, and
|
||||
/// `inherit` takes the parent's value. CSS rules and `style` set it as they set any property.
|
||||
#[test]
|
||||
fn unicode_bidi_is_not_inherited_and_reads_from_the_cascade() {
|
||||
use vectorcraft_doc::ParaDirection::{LeftToRight as Ltr, RightToLeft as Rtl};
|
||||
let read = |body: &str| {
|
||||
let (d, w) = import_with_report(&svg(body)).unwrap_or_else(|e| panic!("{e}"));
|
||||
(only_text(&d).clone(), w)
|
||||
};
|
||||
// An inner `<tspan>` with only a `direction` stays in the outer embedding.
|
||||
let (t, w) = read(r#"<text y="40">ab <tspan direction="rtl" unicode-bidi="embed">12 <tspan direction="ltr">34</tspan></tspan> cd</text>"#);
|
||||
assert_eq!((char_direction_of(&t, "12"), char_direction_of(&t, "34"), char_direction_of(&t, "cd")), (Some(Rtl), Some(Rtl), None), "{:?}", t.runs);
|
||||
assert!(w.is_empty(), "{w:?}");
|
||||
// `inherit` opens an embedding of its own, inside the outer one: kept one level deep, with a warning.
|
||||
let (t, w) = read(
|
||||
r#"<text y="40">ab <tspan direction="rtl" unicode-bidi="embed">12 <tspan direction="ltr" unicode-bidi="inherit">34</tspan></tspan></text>"#,
|
||||
);
|
||||
assert_eq!((char_direction_of(&t, "12"), char_direction_of(&t, "34")), (Some(Rtl), Some(Ltr)), "{:?}", t.runs);
|
||||
assert!(w.len() == 1 && w[0].contains("inside another"), "{w:?}");
|
||||
// A `unicode-bidi` on the `<text>` or a group is not passed on to the `<tspan>`.
|
||||
let (t, _) = read(r#"<g unicode-bidi="embed"><text y="40" unicode-bidi="embed">ab <tspan direction="rtl">12</tspan></text></g>"#);
|
||||
assert!(t.runs.iter().all(|r| r.style.char_direction.is_none()), "{:?}", t.runs);
|
||||
// From a CSS rule and from `style`; `direction` inherits from the `<text>`.
|
||||
for body in [
|
||||
r#"<style>.e{unicode-bidi:embed}</style><text y="40" direction="rtl">ab <tspan class="e">12</tspan></text>"#,
|
||||
r#"<text y="40" direction="rtl">ab <tspan style="unicode-bidi: embed">12</tspan></text>"#,
|
||||
] {
|
||||
let (t, w) = read(body);
|
||||
assert_eq!(char_direction_of(&t, "12"), Some(Rtl), "{body}: {:?}", t.runs);
|
||||
assert_eq!(char_direction_of(&t, "ab"), None, "{body}: {:?}", t.runs);
|
||||
assert!(w.is_empty(), "{body}: {w:?}");
|
||||
}
|
||||
// On a path and in a link too.
|
||||
let (t, _) = read(
|
||||
r##"<defs><path id="p" d="M20 100 H380"/></defs><text><textPath href="#p">ab <tspan direction="ltr" unicode-bidi="embed">12</tspan> <a href="https://example.com/"><tspan direction="rtl" unicode-bidi="embed">34</tspan></a></textPath></text>"##,
|
||||
);
|
||||
assert!(matches!(t.kind, TextKind::OnPath { .. }));
|
||||
assert_eq!((char_direction_of(&t, "12"), char_direction_of(&t, "34")), (Some(Ltr), Some(Rtl)), "{:?}", t.runs);
|
||||
}
|
||||
|
||||
/// The text model holds embeddings only: isolates and overrides become embeddings in their
|
||||
/// `direction`, and `plaintext` one in the direction of its first strong character, each with a
|
||||
/// warning once a text (CSS Writing Modes 3 §2.2,
|
||||
/// https://www.w3.org/TR/css-writing-modes-3/#unicode-bidi).
|
||||
#[test]
|
||||
fn unicode_bidi_values_the_text_model_cannot_hold_are_reported() {
|
||||
use vectorcraft_doc::ParaDirection::{LeftToRight as Ltr, RightToLeft as Rtl};
|
||||
for (bidi, dir, text, want, says) in [
|
||||
("isolate", "rtl", "12", Some(Rtl), "isolation is not supported"),
|
||||
("isolate-override", "rtl", "12", Some(Rtl), "isolation and override are not supported"),
|
||||
("plaintext", "ltr", "\u{5D0}\u{5D1}", Some(Rtl), "first strong character"),
|
||||
("plaintext", "rtl", "12 ab", Some(Ltr), "first strong character"),
|
||||
] {
|
||||
let body = format!(
|
||||
r#"<text id="t" y="40">ab <tspan direction="{dir}" unicode-bidi="{bidi}">{text}</tspan> <tspan direction="{dir}" unicode-bidi="{bidi}">{text}</tspan></text>"#
|
||||
);
|
||||
let (d, w) = import_with_report(&svg(&body)).unwrap_or_else(|e| panic!("{e}"));
|
||||
let t = only_text(&d);
|
||||
assert_eq!(char_direction_of(t, text), want, "{bidi}: {:?}", t.runs);
|
||||
assert!(w.len() == 1 && w[0].starts_with("text 't': ") && w[0].contains(&format!("\"{bidi}\"")) && w[0].contains(says), "{bidi}: {w:?}");
|
||||
}
|
||||
// `plaintext` looks for the first strong character among the characters imported, however far in.
|
||||
let far = format!("{}\u{5D0}\u{5D1}", "1 ".repeat(1500));
|
||||
for (body, part, want) in [
|
||||
(r#"<tspan display="none">אב</tspan>ab"#.to_string(), "ab", Ltr),
|
||||
(format!("<tspan>{far}</tspan>"), "\u{5D0}", Rtl),
|
||||
(format!("<tspan>\n</tspan>12 <tspan>{far}</tspan>"), "\u{5D0}", Rtl),
|
||||
] {
|
||||
let (d, _) =
|
||||
import_with_report(&svg(&format!(r#"<text y="40">x <tspan direction="ltr" unicode-bidi="plaintext">{body}</tspan></text>"#))).unwrap();
|
||||
let t = only_text(&d);
|
||||
assert_eq!(char_direction_of(t, part), Some(want), "{body}: {:?}", t.runs);
|
||||
}
|
||||
// `plaintext` on the `<text>`: each paragraph runs in the direction of its first strong character.
|
||||
for (body, want) in [
|
||||
("<text y=\"40\" unicode-bidi=\"plaintext\">12 \u{5D0}\u{5D1} cd</text>", vec![Some(Rtl)]),
|
||||
(r#"<text y="40" direction="rtl" unicode-bidi="plaintext">12 ab</text>"#, vec![Some(Ltr)]),
|
||||
(
|
||||
"<text unicode-bidi=\"plaintext\"><tspan x=\"10\" y=\"20\">ab</tspan><tspan x=\"10\" dy=\"40\">\u{5D0}\u{5D1} cd</tspan></text>",
|
||||
vec![Some(Ltr), Some(Rtl)],
|
||||
),
|
||||
// A paragraph without a strong character runs left to right.
|
||||
(
|
||||
"<text unicode-bidi=\"plaintext\"><tspan x=\"10\" y=\"20\">\u{5D0}\u{5D1}</tspan><tspan x=\"10\" dy=\"40\">1-2</tspan></text>",
|
||||
vec![Some(Rtl), Some(Ltr)],
|
||||
),
|
||||
] {
|
||||
let (d, w) = import_with_report(&svg(body)).unwrap();
|
||||
let t = only_text(&d);
|
||||
let got: Vec<_> = (0..t.paragraph_styles().len()).map(|i| t.para_at(i).direction).collect();
|
||||
assert_eq!(got, want, "{body}");
|
||||
assert!(w.is_empty(), "{body}: {w:?}");
|
||||
}
|
||||
// Absolutely placed characters sit on their x in the direction of their own paragraph.
|
||||
let (d, _) = import_with_report(&svg(
|
||||
r#"<text unicode-bidi="plaintext" font-size="20"><tspan x="200" y="40">אב</tspan><tspan x="100" y="80">ab אב cd</tspan><tspan x="300">ef</tspan></text>"#,
|
||||
))
|
||||
.unwrap();
|
||||
let t = only_text(&d);
|
||||
assert_eq!((t.para_at(0).direction, t.para_at(1).direction), (Some(Rtl), Some(Ltr)));
|
||||
let lay = layout(FontDb::global(), t);
|
||||
for (byte, x) in [(5, 100.0), (15, 300.0)] {
|
||||
let g = lay.glyphs.iter().find(|g| g.byte == byte).unwrap_or_else(|| panic!("no glyph at {byte}"));
|
||||
let left = (t.xf * g.origin).x;
|
||||
assert!(close(left, x, 0.05), "the character at {byte} starts at {left}, not {x}");
|
||||
}
|
||||
// An override on the `<text>` keeps its paragraph direction.
|
||||
let (d, w) = import_with_report(&svg(r#"<text y="40" direction="rtl" unicode-bidi="bidi-override">ab</text>"#)).unwrap();
|
||||
assert_eq!(only_text(&d).para_at(0).direction, Some(Rtl));
|
||||
assert!(w.len() == 1 && w[0].contains("override on the text"), "{w:?}");
|
||||
}
|
||||
|
||||
/// A line starts at its own x, also when it is anchored as the line before it is and when another
|
||||
/// line holds absolutely placed characters.
|
||||
#[test]
|
||||
|
||||
@@ -272,6 +272,33 @@ fn instances_the_def_cant_stand_for_get_their_own_art() {
|
||||
assert_similar(&render_artboard(&d), &resvg_render(&svg, 200, 120), 24.0, 0.002);
|
||||
}
|
||||
|
||||
/// An instance half stained red whose Transform effect moves it 60 pt right is written in its
|
||||
/// stained colors where the canvas draws it, on its own and inside a group.
|
||||
#[test]
|
||||
fn a_stained_instance_with_a_geometry_effect_keeps_its_stain() {
|
||||
for grouped in [false, true] {
|
||||
let mut d = symbol_doc(0.0, &[]);
|
||||
let mut stained = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "My Mark".into(), xf: Affine::translate((40.0, 60.0)) });
|
||||
stained.appearance = Appearance::basic(Paint::solid(Color::rgb8(255, 0, 0)), Paint::None, 0.0).into();
|
||||
stained.appearance.items.retain(|i| i.is_fill());
|
||||
if let vectorcraft_doc::AppearanceItem::Fill(f) = &mut stained.appearance.items[0] {
|
||||
f.opacity = 0.5;
|
||||
}
|
||||
stained.appearance.effects.push(vectorcraft_doc::Effect {
|
||||
id: "distort.transform".into(),
|
||||
params: serde_json::json!({"moveH": 60.0}),
|
||||
visible: true,
|
||||
});
|
||||
let n = if grouped { Node::group(d.alloc_id(), vec![Arc::new(stained)]) } else { stained };
|
||||
let l = d.layers[0].id;
|
||||
d.insert(Some(l), usize::MAX, n).unwrap();
|
||||
let svg = export(&d, &ExportOptions::default());
|
||||
let blue = Color::rgb8(30, 90, 220).to_hex().to_ascii_lowercase();
|
||||
assert!(!svg.to_ascii_lowercase().contains(&blue), "the symbol's own blue is stained: {svg}");
|
||||
assert_similar(&render_artboard(&d), &resvg_render(&svg, 200, 120), 24.0, 0.003);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pattern_paints_in_symbols_stay_on_the_page() {
|
||||
let mut d = Document::new(200.0, 120.0);
|
||||
|
||||
@@ -260,6 +260,40 @@ fn character_direction_is_written_on_its_runs() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A run set Left to Right keeps its Character Direction when its SVG is opened again, on a line
|
||||
/// and on a path.
|
||||
#[test]
|
||||
fn character_direction_reads_back() {
|
||||
use vectorcraft_doc::ParaDirection;
|
||||
let ltr = CharStyle { char_direction: Some(ParaDirection::LeftToRight), ..style(12.0) };
|
||||
let mut on_path = TextObject::point(Point::ZERO, "", style(12.0));
|
||||
on_path.runs = vec![run("Due ", style(12.0)), run("21 10 2026", ltr.clone())];
|
||||
let mut line = BezPath::new();
|
||||
line.move_to((20.0, 100.0));
|
||||
line.line_to((280.0, 100.0));
|
||||
on_path.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
|
||||
let mut path_doc = Document::new(300.0, 200.0);
|
||||
let n = Node::new(path_doc.alloc_id(), NodeKind::Text(Box::new(on_path)));
|
||||
let l = path_doc.layers[0].id;
|
||||
path_doc.insert(Some(l), 0, n).unwrap();
|
||||
let area = area_doc(vec![run("Due ", style(12.0)), run("21 10 2026", ltr), run(" ok", style(12.0))], 280.0, Justify::Left);
|
||||
for (name, d) in [("area", area), ("path", path_doc)] {
|
||||
let svg = export(&d, &ExportOptions::default());
|
||||
let back = vectorcraft_svg::import(&svg).unwrap_or_else(|e| panic!("{name}: {e}\n{svg}"));
|
||||
let mut texts = vec![];
|
||||
back.walk(|n| {
|
||||
if let NodeKind::Text(t) = &n.kind {
|
||||
texts.push((**t).clone());
|
||||
}
|
||||
});
|
||||
assert_eq!(texts.len(), 1, "{name}: {svg}");
|
||||
let t = &texts[0];
|
||||
let dir = |part: &str| t.runs.iter().find(|r| r.text.contains(part)).map(|r| r.style.char_direction);
|
||||
assert_eq!(dir("2026"), Some(Some(ParaDirection::LeftToRight)), "{name}: {:?}\n{svg}", t.runs);
|
||||
assert_eq!(dir("Due"), Some(None), "{name}: {:?}", t.runs);
|
||||
}
|
||||
}
|
||||
|
||||
/// Digits are written as drawn: the style's numeral system, not the digits that were typed (as
|
||||
/// All Caps writes capitals).
|
||||
#[test]
|
||||
|
||||
+39
-13
@@ -12,7 +12,7 @@ use skrifa::outline::DrawSettings;
|
||||
use skrifa::raw::TableProvider;
|
||||
use skrifa::raw::types::BoundingBox;
|
||||
use skrifa::{GlyphId, MetadataProvider};
|
||||
use vectorcraft_doc::color::{BlendMode, Color, Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop, Paint};
|
||||
use vectorcraft_doc::color::{BlendMode, Color, Gradient, GradientGeom, GradientKind, GradientPaint, GradientSpread, GradientStop, Paint};
|
||||
use vectorcraft_doc::{Appearance, FillLayer, ImageBlob, Node, NodeId, NodeKind};
|
||||
use vectorcraft_geom::PathData;
|
||||
use vello_cpu::peniko;
|
||||
@@ -279,8 +279,10 @@ impl<'a> Tree<'a> {
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
let gradient = |kind, stops: Vec<GradientStop>, geom| {
|
||||
let mut paint = GradientPaint::new(Gradient { kind, stops, ..Gradient::default() });
|
||||
let gradient = |kind, stops: Vec<GradientStop>, spread, geom| {
|
||||
// COLR repeats the interval from the first stop to the last, and one run of the
|
||||
// stops spans the gradient's vector.
|
||||
let mut paint = GradientPaint::new(Gradient { kind, stops, spread, ..Gradient::default() });
|
||||
paint.geom = Some(geom);
|
||||
Paint::Gradient(Box::new(paint))
|
||||
};
|
||||
@@ -290,22 +292,22 @@ impl<'a> Tree<'a> {
|
||||
Some((Paint::solid(c), a))
|
||||
}
|
||||
Brush::LinearGradient { p0, p1, color_stops, extend } => {
|
||||
if *extend != Extend::Pad {
|
||||
self.unsupported.get_or_insert("a repeating or reflecting gradient");
|
||||
let spread = spread(*extend);
|
||||
let (p0, p1) = (point(*p0), point(*p1));
|
||||
let stops = stops(color_stops, self);
|
||||
// Without a stop, the others would repeat over a different interval.
|
||||
if !spread.is_pad() && stops.len() < color_stops.len() {
|
||||
self.unsupported.get_or_insert("a repeating or reflecting gradient with a stop missing from the palette");
|
||||
return None;
|
||||
}
|
||||
let (p0, p1) = (point(*p0), point(*p1));
|
||||
let (stops, first, last) = normalized(stops(color_stops, self))?;
|
||||
let (stops, first, last) = normalized(stops)?;
|
||||
// Stops past 0 and 1 move the ends instead.
|
||||
let mut geom = GradientGeom { start: p0.lerp(p1, f64::from(first)), end: p0.lerp(p1, f64::from(last)), aspect: 1.0, focal: None };
|
||||
geom.transform(xf, GradientKind::Linear);
|
||||
Some((gradient(GradientKind::Linear, stops, geom), 1.0))
|
||||
Some((gradient(GradientKind::Linear, stops, spread, geom), 1.0))
|
||||
}
|
||||
Brush::RadialGradient { c0, r0, c1, r1, color_stops, extend } => {
|
||||
if *extend != Extend::Pad {
|
||||
self.unsupported.get_or_insert("a repeating or reflecting gradient");
|
||||
return None;
|
||||
}
|
||||
let spread = spread(*extend);
|
||||
let (c0, c1, r0, r1) = (point(*c0), point(*c1), f64::from(*r0), f64::from(*r1));
|
||||
let concentric = (c0 - c1).hypot() < 1e-6;
|
||||
if (r0 > 1e-6 && !concentric) || r1 <= r0 || (c0 - c1).hypot() >= r1 {
|
||||
@@ -317,13 +319,27 @@ impl<'a> Tree<'a> {
|
||||
self.unsupported.get_or_insert("a radial gradient with stops past its circles");
|
||||
return None;
|
||||
}
|
||||
if !spread.is_pad() {
|
||||
if stops.len() < color_stops.len() {
|
||||
self.unsupported.get_or_insert("a repeating or reflecting gradient with a stop missing from the palette");
|
||||
return None;
|
||||
}
|
||||
// A run of the stops is the whole way from the center to the circle: one that
|
||||
// starts at a circle (a first stop past the center comes as one) repeats over
|
||||
// less of it. The normalized ends can be a rounding error off 0 and 1.
|
||||
let (lo, hi) = stops.iter().fold((f32::INFINITY, f32::NEG_INFINITY), |(lo, hi), s| (lo.min(s.offset), hi.max(s.offset)));
|
||||
if r0.abs() > 1e-6 || lo.abs() > 1e-4 || (hi - 1.0).abs() > 1e-4 {
|
||||
self.unsupported.get_or_insert("a repeating or reflecting radial gradient that doesn't start at its center");
|
||||
return None;
|
||||
}
|
||||
}
|
||||
// A start circle around the centre starts the stops further out.
|
||||
for s in &mut stops {
|
||||
s.offset = ((r0 + f64::from(s.offset) * (r1 - r0)) / r1) as f32;
|
||||
}
|
||||
let mut geom = GradientGeom { start: c1, end: c1 + Vec2::new(r1, 0.0), aspect: 1.0, focal: (!concentric).then_some(c0) };
|
||||
geom.transform(xf, GradientKind::Radial);
|
||||
Some((gradient(GradientKind::Radial, stops, geom), 1.0))
|
||||
Some((gradient(GradientKind::Radial, stops, spread, geom), 1.0))
|
||||
}
|
||||
Brush::SweepGradient { .. } => {
|
||||
self.unsupported.get_or_insert("a sweep gradient");
|
||||
@@ -345,6 +361,16 @@ impl<'a> Tree<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A COLR extend mode as the gradient's spread; an unknown one pads, as the COLR specification
|
||||
/// says.
|
||||
fn spread(extend: Extend) -> GradientSpread {
|
||||
match extend {
|
||||
Extend::Repeat => GradientSpread::Repeat,
|
||||
Extend::Reflect => GradientSpread::Reflect,
|
||||
_ => GradientSpread::Pad,
|
||||
}
|
||||
}
|
||||
|
||||
/// `stops` sorted, from 0 to 1 (as the gradient's ends then move), and where the first and last
|
||||
/// were; `None` without stops.
|
||||
fn normalized(mut stops: Vec<GradientStop>) -> Option<(Vec<GradientStop>, f32, f32)> {
|
||||
|
||||
@@ -53,6 +53,7 @@ pub(super) fn script_families(c: char) -> &'static [&'static str] {
|
||||
"Lohit Devanagari",
|
||||
"Mangal",
|
||||
],
|
||||
Script::Gujarati => &["Gujarati Sangam MN", "GujaratiMT", "Nirmala UI", "Shruti", "Noto Sans Gujarati", "Noto Sans Gujarati UI"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
@@ -164,6 +165,13 @@ mod tests {
|
||||
assert!(script_families('a').is_empty());
|
||||
}
|
||||
|
||||
/// Gujarati takes a font that draws it (macOS Gujarati Sangam MN, Windows Nirmala UI or Shruti).
|
||||
#[test]
|
||||
fn gujarati_prefers_fonts_made_for_it() {
|
||||
assert_eq!(script_families('ગ').first(), Some(&"Gujarati Sangam MN"));
|
||||
assert!(script_families('ગ').contains(&"Nirmala UI"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_name_table_of_many_records_is_cut_short() {
|
||||
let many: Vec<(u16, &str)> = (0..2000).map(|_| (1, "Short")).collect();
|
||||
|
||||
@@ -502,16 +502,16 @@ mod tests {
|
||||
}
|
||||
|
||||
/// Process-wide store used by `prefs.set` / restore. Each change makes envelopes measure type
|
||||
/// again.
|
||||
/// again, and caches of layouts lay type out again.
|
||||
#[test]
|
||||
fn set_hyphenation_exceptions_round_trips_the_source_string() {
|
||||
let inputs = crate::layout::envelope_ink_inputs();
|
||||
let inputs = crate::layout_inputs();
|
||||
set_hyphenation_exceptions("typography, hap-pen");
|
||||
assert_eq!(hyphenation_exceptions(), "typography, hap-pen");
|
||||
let changed = crate::layout::envelope_ink_inputs();
|
||||
assert_ne!(changed, inputs, "the envelope inputs change with the exceptions");
|
||||
let changed = crate::layout_inputs();
|
||||
assert_ne!(changed, inputs, "the layout inputs change with the exceptions");
|
||||
set_hyphenation_exceptions("");
|
||||
assert!(hyphenation_exceptions().is_empty());
|
||||
assert_ne!(crate::layout::envelope_ink_inputs(), changed, "and again when they are cleared");
|
||||
assert_ne!(crate::layout_inputs(), changed, "and again when they are cleared");
|
||||
}
|
||||
}
|
||||
@@ -47,9 +47,7 @@ pub(crate) use driver::{fit_passes, layout_faces_per_call};
|
||||
pub(crate) use on_path::{layout_trying_each_start, path_arrangements};
|
||||
use vertical::{TcyStyle, stands_upright, tate_chu_yoko, upright_cell, upright_centre, upright_extent, whole_character_widths};
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use driver::envelope_ink_inputs;
|
||||
pub use driver::{envelope_ink, register_envelope_ink};
|
||||
pub use driver::{envelope_ink, layout_inputs, register_envelope_ink};
|
||||
|
||||
const EPS: f64 = 1e-6;
|
||||
|
||||
|
||||
@@ -191,12 +191,13 @@ pub fn envelope_ink(t: &TextObject, m: Affine, projection: Option<(vectorcraft_g
|
||||
/// Make envelopes map type from [`envelope_ink`], measured again when the fonts or the hyphenation
|
||||
/// exceptions change (the first registration stays, see `vectorcraft_doc::live::set_type_ink`).
|
||||
pub fn register_envelope_ink() {
|
||||
vectorcraft_doc::live::set_type_ink(envelope_ink, envelope_ink_inputs);
|
||||
vectorcraft_doc::live::set_type_ink(envelope_ink, layout_inputs);
|
||||
}
|
||||
|
||||
/// A number that changes whenever what [`envelope_ink`] reads besides the type changes: the
|
||||
/// installed fonts and the hyphenation exceptions.
|
||||
pub(crate) fn envelope_ink_inputs() -> u64 {
|
||||
/// A number that changes when the fonts ([`FontDb::generation`]) or the hyphenation exceptions
|
||||
/// (Preferences › Hyphenation › Exceptions) change. Caches of layouts made with the global font
|
||||
/// database, and of art made from them, key on it.
|
||||
pub fn layout_inputs() -> u64 {
|
||||
FontDb::global().generation().rotate_left(32) ^ crate::hyphen::exceptions_generation()
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ pub use fontdb::{
|
||||
user_font_dirs,
|
||||
};
|
||||
pub use hyphen::{hyphenation_exceptions, set_hyphenation_exceptions};
|
||||
pub use layout::{envelope_ink, register_envelope_ink};
|
||||
pub use layout::{envelope_ink, layout_inputs, register_envelope_ink};
|
||||
use std::sync::Arc;
|
||||
pub use svg_glyph::{SvgGlyphReader, set_svg_glyph_reader};
|
||||
|
||||
|
||||
@@ -326,12 +326,8 @@ fn paints_documents_lack_make_the_glyph_an_image() {
|
||||
let line = |extend| ColorLine::new(extend, 2, vec![ColorStop::new(F2Dot14::from_f32(0.0), 0, one), ColorStop::new(one, 1, one)]);
|
||||
let w = FWord::new;
|
||||
type GlyphPaint<'a> = Box<dyn Fn(write_fonts::types::GlyphId16) -> ColrPaint + 'a>;
|
||||
let cases: [(&str, GlyphPaint<'_>); 3] = [
|
||||
let cases: [(&str, GlyphPaint<'_>); 2] = [
|
||||
("Colrtest Xor", Box::new(|o| ColrPaint::composite(ColrPaint::glyph(solid(2), o), CompositeMode::Xor, ColrPaint::glyph(solid(0), o)))),
|
||||
(
|
||||
"Colrtest Repeat",
|
||||
Box::new(move |o| ColrPaint::glyph(ColrPaint::linear_gradient(line(Extend::Repeat), w(0), w(0), w(100), w(0), w(0), w(100)), o)),
|
||||
),
|
||||
(
|
||||
"Colrtest Circles",
|
||||
Box::new(move |o| {
|
||||
@@ -362,6 +358,129 @@ fn a_radial_gradient_from_a_circle_around_its_centre_is_vector_art() {
|
||||
assert_eq!(offsets, [0.25, 1.0], "the stops start a quarter of the way out");
|
||||
}
|
||||
|
||||
/// A COLRv1 color line through `stops` (offset, palette index) that extends by `extend`.
|
||||
fn color_line(extend: write_fonts::tables::colr::Extend, stops: &[(f32, u16)]) -> write_fonts::tables::colr::ColorLine {
|
||||
use write_fonts::tables::colr::{ColorLine, ColorStop};
|
||||
use write_fonts::types::F2Dot14;
|
||||
let one = F2Dot14::from_f32(1.0);
|
||||
let stops: Vec<ColorStop> = stops.iter().map(|&(at, index)| ColorStop::new(F2Dot14::from_f32(at), index, one)).collect();
|
||||
ColorLine::new(extend, u16::try_from(stops.len()).unwrap(), stops)
|
||||
}
|
||||
|
||||
/// The gradient filling the glyph's one path.
|
||||
fn glyph_gradient(font: &[u8]) -> vectorcraft_doc::color::GradientPaint {
|
||||
let root = art(font, COLOUR_CHAR);
|
||||
let [only] = children(&root) else { panic!("one path") };
|
||||
let Paint::Gradient(g) = fill(only) else { panic!("a gradient") };
|
||||
*g
|
||||
}
|
||||
|
||||
/// A linear gradient that repeats or reflects is vector art. The gradient spreads the same way,
|
||||
/// and its stops run once from the first stop's place to the last stop's place.
|
||||
#[test]
|
||||
fn a_repeating_or_reflecting_linear_gradient_is_vector_art() {
|
||||
use vectorcraft_doc::color::GradientSpread;
|
||||
use write_fonts::tables::colr::Extend;
|
||||
use write_fonts::types::FWord;
|
||||
let w = FWord::new;
|
||||
for (family, extend, spread) in
|
||||
[("Colrtest Linear Repeat", Extend::Repeat, GradientSpread::Repeat), ("Colrtest Linear Reflect", Extend::Reflect, GradientSpread::Reflect)]
|
||||
{
|
||||
let line = color_line(extend, &[(0.2, 0), (0.6, 1)]);
|
||||
let font =
|
||||
v1_font(family, COLOUR_CHAR, |o| ColrPaint::glyph(ColrPaint::linear_gradient(line.clone(), w(0), w(0), w(100), w(0), w(0), w(100)), o));
|
||||
let g = glyph_gradient(&font);
|
||||
assert_eq!(g.gradient.kind, GradientKind::Linear, "{family}");
|
||||
assert_eq!(g.gradient.painted_spread(), (spread, 1), "{family}");
|
||||
let offsets: Vec<f32> = g.gradient.stops.iter().map(|s| s.offset).collect();
|
||||
assert_eq!(offsets, [0.0, 1.0], "{family}");
|
||||
let geom = g.geom.unwrap();
|
||||
assert!((geom.start.x - 20.0).abs() < 0.01 && (geom.end.x - 60.0).abs() < 0.01, "{family}: {:?} → {:?}", geom.start, geom.end);
|
||||
}
|
||||
}
|
||||
|
||||
/// A radial gradient from its center that repeats or reflects is vector art that spreads the same
|
||||
/// way, also with a focal point. Its circle is at the last stop, and its stops run once from the
|
||||
/// center out to that circle.
|
||||
#[test]
|
||||
fn a_repeating_or_reflecting_radial_gradient_from_its_center_is_vector_art() {
|
||||
use vectorcraft_doc::color::GradientSpread;
|
||||
use write_fonts::tables::colr::Extend;
|
||||
use write_fonts::types::{FWord, UfWord};
|
||||
let w = FWord::new;
|
||||
let cases = [
|
||||
("Colrtest Radial Repeat", Extend::Repeat, GradientSpread::Repeat, 250, 1.0, 400.0),
|
||||
("Colrtest Radial Reflect", Extend::Reflect, GradientSpread::Reflect, 300, 1.0, 400.0),
|
||||
("Colrtest Radial Half", Extend::Repeat, GradientSpread::Repeat, 300, 0.5, 200.0),
|
||||
// Normalized, the last stop is a rounding error short of 1.
|
||||
("Colrtest Radial Fine", Extend::Repeat, GradientSpread::Repeat, 250, 41.0 / 16384.0, 400.0 * 41.0 / 16384.0),
|
||||
];
|
||||
for (family, extend, spread, x0, last, radius) in cases {
|
||||
let line = color_line(extend, &[(0.0, 0), (last / 2.0, 2), (last, 1)]);
|
||||
let font = v1_font(family, COLOUR_CHAR, |o| {
|
||||
ColrPaint::glyph(ColrPaint::radial_gradient(line.clone(), w(x0), w(300), UfWord::new(0), w(250), w(300), UfWord::new(400)), o)
|
||||
});
|
||||
let g = glyph_gradient(&font);
|
||||
assert_eq!(g.gradient.kind, GradientKind::Radial, "{family}");
|
||||
assert_eq!(g.gradient.painted_spread(), (spread, 1), "{family}");
|
||||
let offsets: Vec<f32> = g.gradient.stops.iter().map(|s| s.offset).collect();
|
||||
assert!(offsets.len() == 3 && offsets.iter().zip([0.0, 0.5, 1.0]).all(|(a, b)| (a - b).abs() < 0.05), "{family}: {offsets:?}");
|
||||
let geom = g.geom.unwrap();
|
||||
assert!(((geom.end - geom.start).hypot() - radius).abs() < 0.1, "{family}: {geom:?}");
|
||||
assert_eq!(geom.focal.is_some(), x0 != 250, "{family}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A radial gradient that repeats from a circle stays an image. The circle is either a start circle
|
||||
/// around the center or the place a first stop past the center puts the start.
|
||||
#[test]
|
||||
fn a_repeating_radial_gradient_from_a_circle_makes_the_glyph_an_image() {
|
||||
use write_fonts::tables::colr::Extend;
|
||||
use write_fonts::types::{FWord, UfWord};
|
||||
let w = FWord::new;
|
||||
for (family, r0, first) in [("Colrtest Ring Repeat", 100, 0.0), ("Colrtest Short Repeat", 0, 0.25)] {
|
||||
let line = color_line(Extend::Repeat, &[(first, 0), (1.0, 1)]);
|
||||
let font = v1_font(family, COLOUR_CHAR, |o| {
|
||||
ColrPaint::glyph(ColrPaint::radial_gradient(line.clone(), w(250), w(300), UfWord::new(r0), w(250), w(300), UfWord::new(400)), o)
|
||||
});
|
||||
assert!(matches!(content(&font, COLOUR_CHAR), Some(ColourContent::Image(_))), "{family}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A gradient that repeats or reflects with a stop whose palette index is outside the palette stays
|
||||
/// an image. Without that stop, the other stops would repeat over a different interval.
|
||||
#[test]
|
||||
fn a_repeating_gradient_with_a_stop_missing_from_the_palette_makes_the_glyph_an_image() {
|
||||
use write_fonts::tables::colr::Extend;
|
||||
use write_fonts::types::{FWord, UfWord};
|
||||
let w = FWord::new;
|
||||
let line = color_line(Extend::Repeat, &[(0.0, 0), (0.5, 1), (1.0, 200)]);
|
||||
let linear = v1_font("Colrtest Linear Missing", COLOUR_CHAR, |o| {
|
||||
ColrPaint::glyph(ColrPaint::linear_gradient(line.clone(), w(0), w(0), w(100), w(0), w(0), w(100)), o)
|
||||
});
|
||||
let radial = v1_font("Colrtest Radial Missing", COLOUR_CHAR, |o| {
|
||||
ColrPaint::glyph(ColrPaint::radial_gradient(line.clone(), w(250), w(300), UfWord::new(0), w(250), w(300), UfWord::new(400)), o)
|
||||
});
|
||||
for (family, font) in [("linear", linear), ("radial", radial)] {
|
||||
assert!(matches!(content(&font, COLOUR_CHAR), Some(ColourContent::Image(_))), "{family}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A color line with an extend value the COLR specification doesn't define pads.
|
||||
#[test]
|
||||
fn an_unknown_extend_value_pads() {
|
||||
use vectorcraft_doc::color::GradientSpread;
|
||||
use write_fonts::tables::colr::Extend;
|
||||
use write_fonts::types::FWord;
|
||||
let w = FWord::new;
|
||||
let line = color_line(Extend::Unknown, &[(0.0, 0), (1.0, 1)]);
|
||||
let font = v1_font("Colrtest Unknown Extend", COLOUR_CHAR, |o| {
|
||||
ColrPaint::glyph(ColrPaint::linear_gradient(line.clone(), w(0), w(0), w(100), w(0), w(0), w(100)), o)
|
||||
});
|
||||
let g = glyph_gradient(&font);
|
||||
assert_eq!(g.gradient.painted_spread(), (GradientSpread::Pad, 1));
|
||||
}
|
||||
|
||||
/// A glyph whose layers don't use the foreground colour is read once, whatever the type's fill.
|
||||
#[test]
|
||||
fn a_glyph_without_the_foreground_colour_is_read_once_for_every_fill() {
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
//! dragging, ←/→ its second value or corner radius. A click selects a parametric shape and shows
|
||||
//! its handles (centre, size, rotation, corner radius and one of the kind's own), whose drags run
|
||||
//! `addon.pathtools.shape.set`; a double-click on the canvas places one with the last settings.
|
||||
//! Each gesture is one undo step.
|
||||
//! Each gesture is one undo step. A new shape's center (from a press or a double-click) and the
|
||||
//! point dragged out from it snap as a drawing tool's points do ([`DrawSnap`]), hovering before
|
||||
//! the press too.
|
||||
//!
|
||||
//! A parametric shape is an ordinary path that keeps its numbers in one entry of its object data
|
||||
//! ([`KEY`]) with a fingerprint of the outline they drew. Moved, turned or scaled by any tool it
|
||||
@@ -17,6 +19,7 @@ use vectorcraft_geom::{Affine, BezPath, PathData, Point, Vec2};
|
||||
use vectorcraft_pathops::addons::pathtools::shapes::fingerprint;
|
||||
pub use vectorcraft_pathops::addons::pathtools::shapes::{MAX_COUNT, MAX_SIZE, MIN_COUNT, MIN_SIZE, ParametricShape, ShapeKind};
|
||||
|
||||
use crate::guides::DrawSnap;
|
||||
use crate::{Action, Cursor, Mods, Overlay, PointerEvent, PointerKind, Tool, ToolContext, ToolKey};
|
||||
|
||||
/// The tool's id.
|
||||
@@ -209,7 +212,7 @@ fn angle(c: Point, p: Point, shift: bool) -> f64 {
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
enum Drag {
|
||||
/// Drawing a shape out from its centre.
|
||||
Create { center: Point },
|
||||
Create { center: Point, from: Point },
|
||||
/// Dragging a handle of shape `id`, which was `shape` when the drag began at `from`.
|
||||
Edit { id: NodeId, shape: ParametricShape, grip: Grip, from: Point },
|
||||
}
|
||||
@@ -221,11 +224,18 @@ pub struct ParametricShapeTool {
|
||||
drag: Option<Drag>,
|
||||
/// Where the pointer last was during the drag, and the keys held.
|
||||
last: (Point, Mods),
|
||||
/// Smart Guides for a new shape's center and the point dragged out from it.
|
||||
snap: DrawSnap,
|
||||
}
|
||||
|
||||
impl Default for ParametricShapeTool {
|
||||
fn default() -> Self {
|
||||
Self { next: ParametricShape::new(ShapeKind::Polygon, Point::ZERO, 100.0, 100.0), drag: None, last: (Point::ZERO, Mods::default()) }
|
||||
Self {
|
||||
next: ParametricShape::new(ShapeKind::Polygon, Point::ZERO, 100.0, 100.0),
|
||||
drag: None,
|
||||
last: (Point::ZERO, Mods::default()),
|
||||
snap: DrawSnap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +257,7 @@ impl ParametricShapeTool {
|
||||
fn shaped(&self, drag: &Drag, p: Point, mods: Mods) -> (&'static str, ParametricShape, Option<NodeId>) {
|
||||
let fit = |v: f64| v.clamp(MIN_SIZE, MAX_SIZE);
|
||||
match *drag {
|
||||
Drag::Create { center } => {
|
||||
Drag::Create { center, .. } => {
|
||||
let r = center.distance(p);
|
||||
let aspect = self.next.width / self.next.height;
|
||||
let s = ParametricShape {
|
||||
@@ -288,6 +298,18 @@ impl ParametricShapeTool {
|
||||
}
|
||||
Some(Action::Preview(cmd.into(), p))
|
||||
}
|
||||
|
||||
/// Show the Smart Guides of a new shape's center at `p`, the pointer hovering; none over a
|
||||
/// handle of the selected shape or over a parametric shape, where a press does not draw.
|
||||
fn hover(&mut self, cx: &ToolContext, p: Point) {
|
||||
let tol = cx.point_tol();
|
||||
let on_grip = selected(cx).is_some_and(|(_, s)| grips(cx, &s).iter().any(|(_, at)| at.distance(p) <= tol));
|
||||
if on_grip || shape_at(cx, p).is_some() {
|
||||
self.snap.clear();
|
||||
} else {
|
||||
self.snap.hover(cx, p, &[], None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Tool for ParametricShapeTool {
|
||||
@@ -300,6 +322,9 @@ impl Tool for ParametricShapeTool {
|
||||
}
|
||||
|
||||
fn pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {
|
||||
if ev.kind == PointerKind::Move && self.drag.is_none() {
|
||||
self.hover(cx, ev.pos);
|
||||
}
|
||||
match ev.kind {
|
||||
PointerKind::Move => vec![],
|
||||
PointerKind::Down => {
|
||||
@@ -309,15 +334,17 @@ impl Tool for ParametricShapeTool {
|
||||
near.min_by(|a, b| a.1.distance(ev.pos).total_cmp(&b.1.distance(ev.pos))).map(|(g, _)| (id, s, g))
|
||||
});
|
||||
if let Some((id, shape, grip)) = grip {
|
||||
self.snap.clear();
|
||||
self.drag = Some(Drag::Edit { id, shape, grip, from: ev.pos });
|
||||
self.last = (ev.pos, ev.mods);
|
||||
return vec![Action::Begin(EDIT.into())];
|
||||
}
|
||||
if let Some(id) = shape_at(cx, ev.pos) {
|
||||
self.snap.clear();
|
||||
return if cx.selection.objects == [id] { vec![] } else { vec![Action::Exec("select.set".into(), json!({ "ids": [id.0] }))] };
|
||||
}
|
||||
let (center, _) = crate::guides::snap_draw(cx, ev.pos, &[]);
|
||||
self.drag = Some(Drag::Create { center });
|
||||
let center = self.snap.press(cx, ev.pos, &[], None);
|
||||
self.drag = Some(Drag::Create { center, from: ev.pos });
|
||||
self.last = (center, ev.mods);
|
||||
vec![Action::Begin(CREATE.into())]
|
||||
}
|
||||
@@ -325,15 +352,19 @@ impl Tool for ParametricShapeTool {
|
||||
if self.drag.is_none() {
|
||||
return vec![];
|
||||
}
|
||||
self.last = (ev.pos, ev.mods);
|
||||
// The point a new shape is dragged out to snaps; a handle's drag does not.
|
||||
let pos = if matches!(self.drag, Some(Drag::Create { .. })) { self.snap.drag(cx, ev.pos, None) } else { ev.pos };
|
||||
self.last = (pos, ev.mods);
|
||||
let Some(preview) = self.preview() else { return vec![] };
|
||||
if ev.kind == PointerKind::Drag {
|
||||
return vec![preview];
|
||||
}
|
||||
self.snap.clear();
|
||||
let Some(drag) = self.drag.take() else { return vec![] };
|
||||
match drag {
|
||||
// A click draws nothing (a double-click places a shape).
|
||||
Drag::Create { center } if center.distance(ev.pos) <= cx.tol(2.0) => vec![Action::Cancel],
|
||||
// A click draws nothing (a double-click places a shape), even when its center
|
||||
// snapped a few pixels from where it was pressed.
|
||||
Drag::Create { from, .. } if from.distance(ev.pos) <= cx.tol(2.0) => vec![Action::Cancel],
|
||||
Drag::Create { .. } => {
|
||||
if let Action::Preview(_, p) = &preview
|
||||
&& let Ok(s) = apply(self.next, p)
|
||||
@@ -350,7 +381,8 @@ impl Tool for ParametricShapeTool {
|
||||
if self.drag.is_some() || shape_at(cx, ev.pos).is_some() {
|
||||
return vec![];
|
||||
}
|
||||
let s = ParametricShape { center: ev.pos, ..self.next };
|
||||
let s = ParametricShape { center: self.snap.press(cx, ev.pos, &[], None), ..self.next };
|
||||
self.snap.clear();
|
||||
vec![Action::Exec("addon.pathtools.shape.create".into(), to_json(&s))]
|
||||
}
|
||||
}
|
||||
@@ -358,6 +390,7 @@ impl Tool for ParametricShapeTool {
|
||||
|
||||
fn key(&mut self, cx: &ToolContext, key: ToolKey, _mods: Mods) -> Vec<Action> {
|
||||
if key == ToolKey::Escape && self.drag.take().is_some() {
|
||||
self.snap.clear();
|
||||
return vec![Action::Cancel];
|
||||
}
|
||||
if !matches!(self.drag, Some(Drag::Create { .. })) {
|
||||
@@ -385,13 +418,16 @@ impl Tool for ParametricShapeTool {
|
||||
}
|
||||
|
||||
fn overlays(&self, cx: &ToolContext) -> Vec<Overlay> {
|
||||
if let Some(Drag::Create { center }) = &self.drag {
|
||||
let (_, s, _) = self.shaped(&Drag::Create { center: *center }, self.last.0, self.last.1);
|
||||
if let Some(drag @ Drag::Create { .. }) = &self.drag {
|
||||
let (_, s, _) = self.shaped(drag, self.last.0, self.last.1);
|
||||
let text = format!("{}\nAngle: {:.1}°", cx.size_label(s.width, s.height), s.rotation);
|
||||
return vec![Overlay::Measure { p: self.last.0 + Vec2::new(cx.tol(12.0), cx.tol(12.0)), text }];
|
||||
let mut out = self.snap.guides().to_vec();
|
||||
out.push(Overlay::Measure { p: self.last.0 + Vec2::new(cx.tol(12.0), cx.tol(12.0)), text });
|
||||
return out;
|
||||
}
|
||||
let Some((_, s)) = selected(cx) else { return vec![] };
|
||||
// The shape's box, then its handles.
|
||||
// The hovering pointer's Smart Guides, then the selected shape's box and handles.
|
||||
let mut out = self.snap.guides().to_vec();
|
||||
let Some((_, s)) = selected(cx) else { return out };
|
||||
let (rx, ry) = (s.width / 2.0, s.height / 2.0);
|
||||
let mut bp = BezPath::new();
|
||||
let f: Affine = s.frame();
|
||||
@@ -400,7 +436,7 @@ impl Tool for ParametricShapeTool {
|
||||
if i == 0 { bp.move_to(*p) } else { bp.line_to(*p) }
|
||||
}
|
||||
bp.close_path();
|
||||
let mut out = vec![Overlay::Path { path: bp, color: BLUE, width: 1.0, dashed: true }];
|
||||
out.push(Overlay::Path { path: bp, color: BLUE, width: 1.0, dashed: true });
|
||||
for (grip, p) in grips(cx, &s) {
|
||||
if grip == Grip::Rotate {
|
||||
out.push(Overlay::Line { a: f * Point::new(0.0, -ry), b: p, color: BLUE, dashed: false });
|
||||
@@ -418,6 +454,11 @@ impl Tool for ParametricShapeTool {
|
||||
Cursor::ParametricShape
|
||||
}
|
||||
|
||||
fn deactivate(&mut self, _cx: &ToolContext) -> Vec<Action> {
|
||||
self.snap.clear();
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// The next shape's numbers (its size and rotation are a double-click's).
|
||||
fn options(&self) -> Value {
|
||||
let mut v = to_json(&self.next);
|
||||
|
||||
@@ -695,6 +695,14 @@ impl Tool for ShapeBuilderTool {
|
||||
self.finish_edit(cx)
|
||||
}
|
||||
|
||||
/// The pointer hovers nothing for the tool: its highlight, the stroke a click would split and
|
||||
/// the cursor swatch preview go, and a map that arrives afterwards highlights nothing.
|
||||
fn hover_ended(&mut self, _cx: &ToolContext) {
|
||||
self.hover = None;
|
||||
self.stroke = None;
|
||||
self.at = None;
|
||||
}
|
||||
|
||||
fn poll(&mut self, cx: &ToolContext, wait: bool) -> Vec<Action> {
|
||||
let mut acts = self.poll_merge(cx, wait);
|
||||
if !acts.is_empty() || self.dragging {
|
||||
|
||||
@@ -769,6 +769,11 @@ impl Tool for LivePaintSelectionTool {
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// The pointer hovers nothing for the tool: its highlight goes.
|
||||
fn hover_ended(&mut self, _cx: &ToolContext) {
|
||||
self.hover = LpHover::None;
|
||||
}
|
||||
|
||||
fn overlays(&self, cx: &ToolContext) -> Vec<Overlay> {
|
||||
if let Some(r) = self.drag.and_then(|d| d.marquee(cx)) {
|
||||
return vec![Overlay::Marquee(r)];
|
||||
|
||||
@@ -920,6 +920,43 @@ fn shape_builder_shift_drag_shows_no_scissors_or_stroke_highlight() {
|
||||
assert_eq!(t.cursor(&cx, Point::new(75.0, 75.0), shift), Cursor::ShapeBuilder);
|
||||
}
|
||||
|
||||
/// When the pointer leaves the canvas, the Shape Builder's region or stroke highlight and its
|
||||
/// cursor swatch preview go.
|
||||
#[test]
|
||||
fn shape_builder_hover_ended_drops_the_highlight_and_the_swatch_preview() {
|
||||
let (d, s) = corner_rects();
|
||||
let p = paint();
|
||||
let cx = cx(&d, &s, &p);
|
||||
let mut t = ShapeBuilderTool::default();
|
||||
for (key, v) in [("pickColorFrom", json!("swatches")), ("cursorSwatchPreview", json!(true)), ("clickStrokeSplitsPath", json!(true))] {
|
||||
t.set_option(key, &v);
|
||||
}
|
||||
// Over a region, then over a stroke a click would split.
|
||||
for (x, y) in [(25.0, 25.0), (-1.0, 25.0)] {
|
||||
t.pointer(&cx, &ev(PointerKind::Move, x, y));
|
||||
let ov = t.overlays(&cx);
|
||||
assert!(ov.iter().any(|o| matches!(o, Overlay::Swatch { .. })) && ov.iter().any(|o| matches!(o, Overlay::Path { .. })), "{ov:?}");
|
||||
t.hover_ended(&cx);
|
||||
assert!(t.overlays(&cx).is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
/// A map of a large selection that arrives after the pointer leaves the canvas highlights nothing
|
||||
/// under the point where the pointer was last seen.
|
||||
#[test]
|
||||
fn shape_builder_map_arriving_after_hover_ended_highlights_nothing() {
|
||||
let (d, s) = square_row(40);
|
||||
let p = paint();
|
||||
let cx = ToolContext { revision: (1, 1), ..cx(&d, &s, &p) };
|
||||
let mut t = ShapeBuilderTool::default();
|
||||
t.pointer(&cx, &ev(PointerKind::Move, 15.0, 10.0));
|
||||
assert!(t.waiting() && t.hover.is_none(), "the map is built on another thread");
|
||||
t.hover_ended(&cx);
|
||||
t.poll(&cx, true);
|
||||
assert!(!t.waiting());
|
||||
assert!(t.hover.is_none() && t.overlays(&cx).is_empty(), "{:?}", t.hover);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_builder_stroke_options_round_trip_and_pick_the_side_under_the_pointer() {
|
||||
let (d, s) = corner_rects();
|
||||
@@ -1593,6 +1630,21 @@ fn live_paint_selection_hover_builds_no_map_of_the_selection() {
|
||||
assert!(!t.waiting());
|
||||
}
|
||||
|
||||
/// When the pointer leaves the canvas, the Live Paint Selection tool's highlight goes.
|
||||
#[test]
|
||||
fn live_paint_selection_hover_ended_drops_the_highlight() {
|
||||
let (d, ..) = live_group_doc();
|
||||
let s = Selection::default();
|
||||
let p = paint();
|
||||
let cx = cx(&d, &s, &p);
|
||||
let mut t = LivePaintSelectionTool::default();
|
||||
t.pointer(&cx, &ev(PointerKind::Move, 75.0, 25.0));
|
||||
let ov = t.overlays(&cx);
|
||||
assert!(ov.iter().any(|o| matches!(o, Overlay::Path { .. })), "{ov:?}");
|
||||
t.hover_ended(&cx);
|
||||
assert!(t.overlays(&cx).is_empty());
|
||||
}
|
||||
|
||||
/// A Live Paint Selection click pressed with `down`: the press does nothing, and the release does
|
||||
/// what is returned.
|
||||
fn lp_click(t: &mut LivePaintSelectionTool, cx: &ToolContext, down: PointerEvent) -> Vec<Action> {
|
||||
|
||||
@@ -453,6 +453,7 @@ group = "Navigation"
|
||||
shown_when = [
|
||||
"The Rotate, Reflect, Scale and Shear tools with nothing selected.",
|
||||
"Puppet Warp off the mesh, where no pin can go.",
|
||||
"The Smooth tool, and the Pencil or Blob Brush where Alt gives the Smooth tool, when no path is selected that the Smooth tool can act on: a drag changes nothing.",
|
||||
]
|
||||
hotspot = "The centre."
|
||||
designer = false
|
||||
@@ -690,10 +691,10 @@ look = "Only a small dot at the centre, as the Blob Brush's: the tip outline dra
|
||||
id = "Smooth"
|
||||
group = "Drawing and editing tools"
|
||||
shown_when = [
|
||||
"The Smooth tool: a drag along a selected path smooths it.",
|
||||
"The Pencil tool with Alt key toggles to Smooth Tool on: with Alt held before a stroke begins to draw, and through a drag that smooths.",
|
||||
"The Paintbrush tool with Alt held before a press while a path is selected that the Smooth tool can act on (an editable path, not a guide, with three or more anchors on one of its subpaths), and through a drag that smooths.",
|
||||
"The Blob Brush tool with Alt held before a press, or before the drag has moved from the press point, and through a drag that smooths.",
|
||||
"The Smooth tool while a path is selected that it can act on (an editable path, not a guide, with three or more anchors on one of its subpaths), wherever the pointer is: a drag along the path smooths it. With no such path selected, Not Allowed shows.",
|
||||
"The Pencil tool with Alt key toggles to Smooth Tool on and such a path selected: with Alt held before a stroke begins to draw, and through a drag that smooths.",
|
||||
"The Paintbrush tool with Alt held before a press while such a path is selected, and through a drag that smooths.",
|
||||
"The Blob Brush tool with such a path selected: with Alt held before a press, or before the drag has moved from the press point, and through a drag that smooths.",
|
||||
]
|
||||
hotspot = "The tip."
|
||||
|
||||
|
||||
@@ -344,10 +344,19 @@ impl DimensionTool {
|
||||
_ => Orientation::Aligned,
|
||||
};
|
||||
}
|
||||
d.place(p);
|
||||
// A dimension dragged out from art starts with placeholder points. Read the art's points
|
||||
// before placing the dimension at the pointer. Clear the offset first: an out-of-range
|
||||
// offset from the last pointer position fails the read.
|
||||
if !matches!(d.mode, DimensionMode::Radius | DimensionMode::Diameter) {
|
||||
d.offset = 0.0;
|
||||
}
|
||||
if d.resolve(cx.doc).is_err() {
|
||||
return vec![];
|
||||
}
|
||||
d.place(p);
|
||||
if d.validate().is_err() {
|
||||
return vec![];
|
||||
}
|
||||
let mut actions = vec![];
|
||||
if !self.began {
|
||||
actions.push(Action::Begin("Create Dimension".into()));
|
||||
@@ -675,6 +684,53 @@ mod tests {
|
||||
assert!(matches!(placed.last(), Some(Action::Commit)), "{placed:?}");
|
||||
}
|
||||
|
||||
/// A pointer position too far away to place the dimension shows no preview, and the preview
|
||||
/// comes back where the pointer returns.
|
||||
#[test]
|
||||
fn placement_recovers_after_a_position_out_of_range() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let c = cx(&d, &s, &p);
|
||||
let mut t = DimensionTool::default();
|
||||
for (x, y) in [(400.0, 400.0), (450.0, 400.0)] {
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, x, y));
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Up, x, y));
|
||||
}
|
||||
assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Move, 420.0, 3.0e6)).is_empty());
|
||||
let acts = t.pointer(&c, &PointerEvent::new(PointerKind::Move, 420.0, 450.0));
|
||||
let Some(Action::Preview(_, params)) = acts.last() else { panic!("{acts:?}") };
|
||||
assert_eq!(params["offset"], json!(50.0));
|
||||
}
|
||||
|
||||
/// Dragging out from a segment previews the dimension at the pointer from the first drag on.
|
||||
#[test]
|
||||
fn the_first_preview_of_a_segment_drag_is_at_the_pointer() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let c = cx(&d, &s, &p);
|
||||
let mut t = DimensionTool::default();
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, 150.0, 100.0));
|
||||
let acts = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, 150.0, 60.0));
|
||||
let Some(Action::Preview(_, params)) = acts.last() else { panic!("{acts:?}") };
|
||||
assert_eq!(params["offset"], json!(-40.0), "40 pt above the top edge");
|
||||
}
|
||||
|
||||
/// Dragging out from a corner previews the angle at the pointer's distance from the corner
|
||||
/// from the first drag on.
|
||||
#[test]
|
||||
fn the_first_preview_of_a_corner_drag_is_at_the_pointer() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let c = cx(&d, &s, &p);
|
||||
let mut t = DimensionTool::default();
|
||||
t.set_option("mode", &json!("angular"));
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, 100.0, 100.0));
|
||||
let acts = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, 130.0, 140.0));
|
||||
let Some(Action::Preview(_, params)) = acts.last() else { panic!("{acts:?}") };
|
||||
assert_eq!(params["vertex"], json!([100.0, 100.0]));
|
||||
assert_eq!(params["offset"], json!(50.0), "50 pt from the corner");
|
||||
}
|
||||
|
||||
/// A Linear press on an anchor picks that anchor even when the pointer moves a few pixels
|
||||
/// before the release: it does not drag out the dimension of a segment that ends there. Both
|
||||
/// picks keep their links to the path, so the dimension measures between the two anchors.
|
||||
|
||||
@@ -777,7 +777,8 @@ fn has_selected_smooth_path(cx: &ToolContext) -> bool {
|
||||
cx.selection.objects.iter().filter_map(|id| cx.doc.node(*id)).any(|n| {
|
||||
let mut found = false;
|
||||
n.walk(&mut |child| {
|
||||
if matches!(&child.kind, NodeKind::Path { guide: false, path, .. } if path.control_bounds().is_some() && path.subpaths.iter().any(smoothable))
|
||||
if !found
|
||||
&& matches!(&child.kind, NodeKind::Path { guide: false, path, .. } if path.control_bounds().is_some() && path.subpaths.iter().any(smoothable))
|
||||
&& editable(child.id)
|
||||
{
|
||||
found = true;
|
||||
@@ -787,6 +788,13 @@ fn has_selected_smooth_path(cx: &ToolContext) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
/// The Smooth tool's cursor: Not Allowed when no selected path can be smoothed, since a drag then
|
||||
/// changes nothing. A selected path anywhere on the canvas is enough, so the cursor does not depend
|
||||
/// on where the pointer is.
|
||||
fn smooth_cursor(cx: &ToolContext) -> Cursor {
|
||||
if has_selected_smooth_path(cx) { Cursor::Smooth } else { Cursor::NotAllowed }
|
||||
}
|
||||
|
||||
impl GestureTool {
|
||||
pub fn new(id: &str) -> Self {
|
||||
let id: &'static str = match id {
|
||||
@@ -1173,7 +1181,7 @@ impl GestureTool {
|
||||
let tol = cx.tol(self.edit_within);
|
||||
let radius = cx.tol(SMOOTH_RADIUS);
|
||||
if self.smoothing {
|
||||
if has_sel && pts.len() >= 2 {
|
||||
if pts.len() >= 2 && has_selected_smooth_path(cx) {
|
||||
return vec![Action::Exec(
|
||||
"path.smoothRegion".into(),
|
||||
json!({"points": points_json(pts), "radius": radius, "fidelity": cx.tol(self.smooth_fidelity)}),
|
||||
@@ -1225,7 +1233,7 @@ impl GestureTool {
|
||||
}),
|
||||
)],
|
||||
"knife" if pts.len() >= 2 => vec![Action::Exec("path.knife".into(), json!({"points": points_json(pts)}))],
|
||||
"smooth" if has_sel && pts.len() >= 2 => {
|
||||
"smooth" if pts.len() >= 2 && has_selected_smooth_path(cx) => {
|
||||
vec![Action::Exec(
|
||||
"path.smoothRegion".into(),
|
||||
json!({"points": points_json(pts), "radius": radius, "fidelity": cx.tol(self.fidelity)}),
|
||||
@@ -1534,13 +1542,15 @@ impl Tool for GestureTool {
|
||||
}
|
||||
fn cursor(&self, cx: &ToolContext, p: Point, m: Mods) -> Cursor {
|
||||
// An active smoothing drag, or Alt before a press that would switch the Pencil, Paintbrush
|
||||
// or Blob Brush to Smooth. A Paintbrush stroke already drawing keeps drawing with Alt.
|
||||
// or Blob Brush to Smooth. A Paintbrush stroke already drawing keeps drawing with Alt. With
|
||||
// no selected path to smooth, the Pencil and the Blob Brush show Not Allowed, as the Smooth
|
||||
// tool does.
|
||||
if self.smoothing
|
||||
|| (self.id == "pencil" && self.alt_smooth && m.alt && !self.began)
|
||||
|| (!self.active && self.id == "paintbrush" && m.alt && has_selected_smooth_path(cx))
|
||||
|| self.blob_alt_smooths(m)
|
||||
{
|
||||
return Cursor::Smooth;
|
||||
return smooth_cursor(cx);
|
||||
}
|
||||
// Before a press that would draw a straight line: over an end of a selected open path,
|
||||
// the line continues that path from the end (`PointerKind::Down`).
|
||||
@@ -1563,7 +1573,7 @@ impl Tool for GestureTool {
|
||||
"blobBrush" => Cursor::BlobBrush,
|
||||
"eraser" => Cursor::Eraser,
|
||||
"knife" => Cursor::Knife,
|
||||
"smooth" => Cursor::Smooth,
|
||||
"smooth" => smooth_cursor(cx),
|
||||
"pathEraser" => Cursor::PathEraser,
|
||||
"join" => Cursor::Join,
|
||||
// Before a press that draws a new path the Pencil shows its star; over a selected path
|
||||
|
||||
@@ -369,7 +369,7 @@ fn the_freehand_tools_want_every_sample() {
|
||||
|
||||
#[test]
|
||||
fn alt_shows_the_closing_cursor_and_a_release_with_alt_closes() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (d, rect) = doc_with_rect();
|
||||
let s = Selection::default();
|
||||
let p = paint();
|
||||
let cx = cx(&d, &s, &p);
|
||||
@@ -396,18 +396,26 @@ fn alt_shows_the_closing_cursor_and_a_release_with_alt_closes() {
|
||||
}
|
||||
// With Alt key toggles to Smooth Tool on, Alt lends the Pencil the Smooth tool instead, until
|
||||
// a stroke has begun to draw: Alt then neither smooths nor closes, and the cursor stays the Pencil's.
|
||||
// With nothing selected to smooth, the Smooth tool it lends shows Not Allowed.
|
||||
let mut t = GestureTool::new("pencil");
|
||||
t.set_option("altSmooth", &json!(true));
|
||||
assert_eq!(t.cursor(&cx, at, alt), Cursor::Smooth);
|
||||
assert_eq!(t.cursor(&cx, at, alt), Cursor::NotAllowed);
|
||||
t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 0.0, 0.0));
|
||||
t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, 10.0, 5.0));
|
||||
assert_eq!(t.cursor(&cx, at, alt), Cursor::Pencil, "a stroke that has begun");
|
||||
let acts = t.pointer(&cx, &PointerEvent::new(PointerKind::Up, 20.0, 0.0).with_mods(alt));
|
||||
assert!(matches!(&acts[0], Action::Preview(_, v) if v["closed"] == false), "{acts:?}");
|
||||
// A smoothing drag keeps the Smooth cursor when Alt is let go.
|
||||
t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 0.0, 0.0).with_mods(alt));
|
||||
t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, 10.0, 5.0).with_mods(alt));
|
||||
assert_eq!(t.cursor(&cx, at, Mods::default()), Cursor::Smooth, "a smoothing drag");
|
||||
// A smoothing drag keeps the lent cursor when Alt is let go: Smooth with a selected path, Not
|
||||
// Allowed without one.
|
||||
let mut selected = Selection::default();
|
||||
selected.set([rect]);
|
||||
for (sel, c) in [(&s, Cursor::NotAllowed), (&selected, Cursor::Smooth)] {
|
||||
let cx = crate::testutil::cx(&d, sel, &p);
|
||||
t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 0.0, 0.0).with_mods(alt));
|
||||
t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, 10.0, 5.0).with_mods(alt));
|
||||
assert_eq!(t.cursor(&cx, at, Mods::default()), c, "a smoothing drag");
|
||||
t.pointer(&cx, &PointerEvent::new(PointerKind::Up, 20.0, 0.0).with_mods(alt));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2531,6 +2539,117 @@ fn smooth_needs_selection_and_brackets_resize() {
|
||||
assert_eq!(b.options()["size"], 11.0);
|
||||
}
|
||||
|
||||
/// The Smooth tool shows Not Allowed while no selected path can be smoothed: nothing selected, only
|
||||
/// type, a path of two anchors or a locked path. A selected path anywhere, alone or in a selected
|
||||
/// group, gives the Smooth cursor wherever the pointer is, and the cursor follows the selection while
|
||||
/// the tool stays chosen.
|
||||
#[test]
|
||||
fn smooth_shows_not_allowed_without_a_selected_path_to_smooth() {
|
||||
use std::sync::Arc;
|
||||
let (mut d, rect) = doc_with_rect();
|
||||
let layer = d.layers[0].id;
|
||||
let line_id = d.alloc_id();
|
||||
let line = vectorcraft_geom::PathData::from_bezpath(&vectorcraft_geom::BezPath::from_vec(vec![
|
||||
vectorcraft_geom::PathEl::MoveTo(Point::new(300.0, 300.0)),
|
||||
vectorcraft_geom::PathEl::LineTo(Point::new(400.0, 300.0)),
|
||||
]));
|
||||
d.insert(Some(layer), 1, vectorcraft_doc::Node::path(line_id, line, vectorcraft_doc::Appearance::default_art())).unwrap();
|
||||
let square = vectorcraft_geom::shapes::rectangle(vectorcraft_geom::Rect::new(300.0, 350.0, 350.0, 400.0));
|
||||
let inner = vectorcraft_doc::Node::path(d.alloc_id(), square, vectorcraft_doc::Appearance::default_art());
|
||||
let group = vectorcraft_doc::Node::group(d.alloc_id(), vec![Arc::new(inner)]);
|
||||
let group_id = group.id;
|
||||
d.insert(Some(layer), 2, group).unwrap();
|
||||
let (text_doc, text) = doc_with_area_type();
|
||||
let mut locked = d.clone();
|
||||
locked.node_mut(rect).unwrap().locked = true;
|
||||
let p = paint();
|
||||
let sel = |ids: &[NodeId]| {
|
||||
let mut s = Selection::default();
|
||||
s.set(ids.iter().copied());
|
||||
s
|
||||
};
|
||||
let t = GestureTool::new("smooth");
|
||||
let far = Point::new(480.0, 20.0);
|
||||
let cases: [(&vectorcraft_doc::Document, Selection, Cursor, &str); 7] = [
|
||||
(&d, Selection::default(), Cursor::NotAllowed, "nothing selected"),
|
||||
(&text_doc, sel(&[text]), Cursor::NotAllowed, "type"),
|
||||
(&d, sel(&[line_id]), Cursor::NotAllowed, "a two-anchor path"),
|
||||
(&locked, sel(&[rect]), Cursor::NotAllowed, "a locked path"),
|
||||
(&d, sel(&[rect]), Cursor::Smooth, "a path away from the pointer"),
|
||||
(&d, sel(&[line_id, rect]), Cursor::Smooth, "a path beside one it cannot smooth"),
|
||||
(&d, sel(&[group_id]), Cursor::Smooth, "a group holding a path"),
|
||||
];
|
||||
for (doc, s, c, what) in &cases {
|
||||
assert_eq!(t.cursor(&cx(doc, s, &p), far, Mods::default()), *c, "{what}");
|
||||
}
|
||||
// The cursor changes as the selection does, with the same tool.
|
||||
let (none, one) = (Selection::default(), sel(&[rect]));
|
||||
for (s, c) in [(&none, Cursor::NotAllowed), (&one, Cursor::Smooth), (&none, Cursor::NotAllowed)] {
|
||||
assert_eq!(t.cursor(&cx(&d, s, &p), Point::new(150.0, 150.0), Mods::default()), c);
|
||||
}
|
||||
// A drag with Not Allowed showing sends nothing.
|
||||
let mut t = GestureTool::new("smooth");
|
||||
let acts = drag(&mut t, &cx(&d, &none, &p), &[(50.0, 150.0), (150.0, 150.0), (250.0, 150.0)], Mods::default());
|
||||
assert!(acts.iter().all(|a| a.is_empty()), "{acts:?}");
|
||||
}
|
||||
|
||||
/// The Blob Brush's Alt shows Not Allowed with no selected path to smooth, and the Smooth cursor with
|
||||
/// one; the Paintbrush's Alt without one keeps the closing cursor, since its release closes the stroke.
|
||||
#[test]
|
||||
fn borrowed_smooth_shows_not_allowed_without_a_selected_path_to_smooth() {
|
||||
let (d, rect) = doc_with_rect();
|
||||
let none = Selection::default();
|
||||
let mut one = Selection::default();
|
||||
one.set([rect]);
|
||||
let p = paint();
|
||||
let alt = Mods { alt: true, ..Default::default() };
|
||||
let at = Point::new(400.0, 400.0);
|
||||
let blob = GestureTool::new("blobBrush");
|
||||
assert_eq!(blob.cursor(&cx(&d, &none, &p), at, alt), Cursor::NotAllowed);
|
||||
assert_eq!(blob.cursor(&cx(&d, &one, &p), at, alt), Cursor::Smooth);
|
||||
assert_eq!(blob.cursor(&cx(&d, &none, &p), at, Mods::default()), Cursor::BlobBrush);
|
||||
let mut pencil = GestureTool::new("pencil");
|
||||
pencil.set_option("altSmooth", &json!(true));
|
||||
assert_eq!(pencil.cursor(&cx(&d, &none, &p), at, alt), Cursor::NotAllowed);
|
||||
assert_eq!(pencil.cursor(&cx(&d, &one, &p), at, alt), Cursor::Smooth);
|
||||
let brush = GestureTool::new("paintbrush");
|
||||
assert_eq!(brush.cursor(&cx(&d, &none, &p), at, alt), Cursor::PaintbrushClose);
|
||||
assert_eq!(brush.cursor(&cx(&d, &one, &p), at, alt), Cursor::Smooth);
|
||||
}
|
||||
|
||||
/// A Smooth release sends no command while the selection holds no path it can smooth, from the
|
||||
/// Smooth tool and from the Blob Brush and Pencil with Alt; with a path selected it smooths.
|
||||
#[test]
|
||||
fn smoothing_release_sends_nothing_without_a_selected_path_to_smooth() {
|
||||
let (text_doc, text) = doc_with_area_type();
|
||||
let (d, rect) = doc_with_rect();
|
||||
let mut type_only = Selection::default();
|
||||
type_only.set([text]);
|
||||
let mut one = Selection::default();
|
||||
one.set([rect]);
|
||||
let p = paint();
|
||||
let alt = Mods { alt: true, ..Default::default() };
|
||||
let stroke = [(50.0, 150.0), (150.0, 150.0), (250.0, 160.0)];
|
||||
let smooths = |acts: &[Vec<Action>]| acts.iter().flatten().any(|a| matches!(a, Action::Exec(c, _) if c == "path.smoothRegion"));
|
||||
for (id, mods) in [("smooth", Mods::default()), ("blobBrush", alt), ("pencil", alt)] {
|
||||
let tool = || {
|
||||
let mut t = GestureTool::new(id);
|
||||
if id == "pencil" {
|
||||
t.set_option("altSmooth", &json!(true));
|
||||
}
|
||||
t
|
||||
};
|
||||
let mut c = cx(&text_doc, &type_only, &p);
|
||||
c.smart_guides = false;
|
||||
let acts = drag_held(&mut tool(), &c, &stroke, mods);
|
||||
assert!(!smooths(&acts), "{id} with only type selected: {acts:?}");
|
||||
let mut c = cx(&d, &one, &p);
|
||||
c.smart_guides = false;
|
||||
let acts = drag_held(&mut tool(), &c, &stroke, mods);
|
||||
assert!(smooths(&acts), "{id} with a path selected: {acts:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pencil_alt_toggles_to_smooth_when_enabled_and_closes_when_disabled() {
|
||||
let (d, id) = doc_with_rect();
|
||||
|
||||
@@ -714,6 +714,81 @@ fn pen_click_near_a_glyph_anchor_lands_on_it() {
|
||||
assert!(q.distance(anchor) > 1e-3, "{q:?} landed on the glyph anchor with Smart Guides off");
|
||||
}
|
||||
|
||||
/// Snap to Glyph: the Parametric Shape tool's center (a drag and a double-click) and the point
|
||||
/// dragged out from it, and the Slice tool's corners, land on a glyph's anchor point as a Pen
|
||||
/// click does, with the guides shown while hovering, and not with Snap to Glyph off.
|
||||
#[test]
|
||||
fn parametric_shape_and_slice_points_land_on_a_glyph_anchor() {
|
||||
use crate::addons::parametric_shape::ParametricShapeTool;
|
||||
use crate::slice::SliceTool;
|
||||
use crate::{Action, PointerEvent, PointerKind, Tool};
|
||||
let (d, t) = glyph_doc("H", 0.0);
|
||||
let lay = layout_text(FontDb::global(), &t);
|
||||
let g = lay.glyphs.iter().find(|g| !g.outline.elements().is_empty()).expect("a glyph with an outline");
|
||||
// An anchor inside the glyph's bounds, which no glyph bounds line passes through.
|
||||
let bb = g.outline.bounding_box();
|
||||
let inside = |p: Point| p.x > bb.x0 + 1.0 && p.x < bb.x1 - 1.0 && p.y > bb.y0 + 1.0 && p.y < bb.y1 - 1.0;
|
||||
let anchor = t.xf * g.outline.segments().map(|s| s.start()).find(|p| inside(*p)).expect("an anchor inside the glyph");
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let click = anchor + Vec2::new(1.5, -1.0);
|
||||
let ev = |kind, q: Point| PointerEvent::new(kind, q.x, q.y);
|
||||
let center = |v: &serde_json::Value| Point::new(v["center"][0].as_f64().unwrap(), v["center"][1].as_f64().unwrap());
|
||||
// A press away from the type, for drags that end at the click.
|
||||
let away = anchor + Vec2::new(-83.3, -61.7);
|
||||
for glyphs in [true, false] {
|
||||
let c = ToolContext { snap_to_glyph: glyphs, ..cx(&d, &s, &p) };
|
||||
let lands = |q: Point| q.distance(anchor) < 1e-9;
|
||||
// A Parametric Shape drawn out from the click.
|
||||
let mut tool = ParametricShapeTool::default();
|
||||
tool.pointer(&c, &ev(PointerKind::Down, click));
|
||||
let acts = tool.pointer(&c, &ev(PointerKind::Drag, click + Vec2::new(80.0, 60.0)));
|
||||
let Some(Action::Preview(_, v)) = acts.first() else { panic!("{acts:?}") };
|
||||
assert_eq!(lands(center(v)), glyphs, "drag, Snap to Glyph {glyphs}: {:?} vs {anchor:?}", center(v));
|
||||
// A Parametric Shape dragged out to the click is sized and turned as one dragged out to
|
||||
// the anchor itself.
|
||||
let drawn_to = |q: Point| {
|
||||
let mut tool = ParametricShapeTool::default();
|
||||
tool.pointer(&c, &ev(PointerKind::Down, away));
|
||||
tool.pointer(&c, &ev(PointerKind::Drag, q))
|
||||
};
|
||||
assert_eq!(drawn_to(click) == drawn_to(anchor), glyphs, "dragged point, Snap to Glyph {glyphs}");
|
||||
// A click 3 px from the anchor draws nothing, though its center snaps onto the anchor.
|
||||
let near = anchor + Vec2::new(2.5, -1.5);
|
||||
let mut tool = ParametricShapeTool::default();
|
||||
tool.pointer(&c, &ev(PointerKind::Down, near));
|
||||
let acts = tool.pointer(&c, &ev(PointerKind::Drag, near));
|
||||
let Some(Action::Preview(_, v)) = acts.first() else { panic!("{acts:?}") };
|
||||
assert_eq!(lands(center(v)), glyphs, "click, Snap to Glyph {glyphs}: {:?} vs {anchor:?}", center(v));
|
||||
assert_eq!(tool.pointer(&c, &ev(PointerKind::Up, near)), vec![Action::Cancel], "click, Snap to Glyph {glyphs}");
|
||||
// One placed with a double-click.
|
||||
let mut tool = ParametricShapeTool::default();
|
||||
let acts = tool.pointer(&c, &ev(PointerKind::DoubleClick, click));
|
||||
let Some(Action::Exec(_, v)) = acts.first() else { panic!("{acts:?}") };
|
||||
assert_eq!(lands(center(v)), glyphs, "double-click, Snap to Glyph {glyphs}: {:?} vs {anchor:?}", center(v));
|
||||
// The pointer hovering at the click shows the guides of the anchor it would land on.
|
||||
let mut tool = ParametricShapeTool::default();
|
||||
tool.pointer(&c, &ev(PointerKind::Move, click));
|
||||
assert_eq!(!tool.overlays(&c).is_empty(), glyphs, "hover, Snap to Glyph {glyphs}");
|
||||
// A slice dragged from the click down and to the right starts at its top-left corner.
|
||||
let mut tool = SliceTool::default();
|
||||
tool.pointer(&c, &ev(PointerKind::Move, click));
|
||||
assert_eq!(!tool.overlays(&c).is_empty(), glyphs, "slice hover, Snap to Glyph {glyphs}");
|
||||
tool.pointer(&c, &ev(PointerKind::Down, click));
|
||||
let acts = tool.pointer(&c, &ev(PointerKind::Drag, click + Vec2::new(80.0, 60.0)));
|
||||
let Some(Action::Preview(_, v)) = acts.get(1) else { panic!("{acts:?}") };
|
||||
let corner = Point::new(v["x"].as_f64().unwrap(), v["y"].as_f64().unwrap());
|
||||
assert_eq!(lands(corner), glyphs, "slice, Snap to Glyph {glyphs}: {corner:?} vs {anchor:?}");
|
||||
// One dragged from up and to the left ends at its bottom-right corner.
|
||||
let mut tool = SliceTool::default();
|
||||
tool.pointer(&c, &ev(PointerKind::Down, away));
|
||||
let acts = tool.pointer(&c, &ev(PointerKind::Drag, click));
|
||||
let Some(Action::Preview(_, v)) = acts.get(1) else { panic!("{acts:?}") };
|
||||
let f = |k: &str| v[k].as_f64().unwrap();
|
||||
let end = Point::new(f("x") + f("width"), f("y") + f("height"));
|
||||
assert_eq!(end.distance(anchor) < 1e-6, glyphs, "slice end, Snap to Glyph {glyphs}: {end:?} vs {anchor:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Snap to Glyph: a single line of type has an x-height guide; a paragraph shows glyph bounds only.
|
||||
#[test]
|
||||
fn drawn_points_line_up_with_the_x_height_of_a_single_line() {
|
||||
@@ -953,6 +1028,124 @@ fn a_drawn_point_lands_on_a_corner_of_a_glyphs_bounds() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Snap to Glyph: both ends of a gradient vector drawn on the art with the Gradient tool, a click
|
||||
/// that applies one, and its dragged start and end handles, land on a glyph's anchor point as a
|
||||
/// Pen click does, with the guides shown while hovering, and not with Snap to Glyph off. A drawn
|
||||
/// end also lands on a slanted path's segment, which no bounds line passes along, and on a
|
||||
/// construction guide through its start, and with Shift on an anchor along its 45° step. With nothing
|
||||
/// selected, hovering off the art shows no guides. A press that snaps still starts a drag only
|
||||
/// after the pointer moves 3 px from where it was pressed.
|
||||
#[test]
|
||||
fn gradient_vector_ends_land_on_a_glyph_anchor() {
|
||||
use crate::xform::GradientTool;
|
||||
use crate::{Action, PointerEvent, PointerKind, Tool};
|
||||
use vectorcraft_color::{Gradient, GradientGeom, GradientPaint, Paint};
|
||||
use vectorcraft_doc::Appearance;
|
||||
use vectorcraft_geom::shapes;
|
||||
let (mut d, t) = glyph_doc("H", 0.0);
|
||||
let lay = layout_text(FontDb::global(), &t);
|
||||
let g = lay.glyphs.iter().find(|g| !g.outline.elements().is_empty()).expect("a glyph with an outline");
|
||||
// An anchor inside the glyph's bounds, which no glyph bounds line passes through.
|
||||
let bb = g.outline.bounding_box();
|
||||
let inside = |p: Point| p.x > bb.x0 + 1.0 && p.x < bb.x1 - 1.0 && p.y > bb.y0 + 1.0 && p.y < bb.y1 - 1.0;
|
||||
let anchor = t.xf * g.outline.segments().map(|s| s.start()).find(|p| inside(*p)).expect("an anchor inside the glyph");
|
||||
let type_id = d.children(Some(d.layers[0].id)).unwrap()[0].id;
|
||||
// A rectangle away from the type, with a gradient from (300, 100) to (400, 100).
|
||||
let (l, rect) = (d.layers[0].id, d.alloc_id());
|
||||
d.insert(Some(l), 0, Node::path(rect, shapes::rectangle(Rect::new(300.0, 50.0, 400.0, 150.0)), Appearance::default_art())).unwrap();
|
||||
let mut gp = GradientPaint::new(Gradient::default());
|
||||
gp.geom = Some(GradientGeom { start: Point::new(300.0, 100.0), end: Point::new(400.0, 100.0), aspect: 1.0, focal: None });
|
||||
d.node_mut(rect).unwrap().appearance.set_fill(Paint::Gradient(Box::new(gp)));
|
||||
// A slanted line, its middle far from its bounds' edges.
|
||||
let (a0, a1) = (Point::new(200.0, 420.0), Point::new(280.0, 480.0));
|
||||
let line = d.alloc_id();
|
||||
d.insert(Some(l), 0, Node::path(line, shapes::line(a0, a1), Appearance::default_art())).unwrap();
|
||||
let mut on_type = Selection::default();
|
||||
on_type.add(type_id);
|
||||
let mut on_rect = Selection::default();
|
||||
on_rect.add(rect);
|
||||
let p = paint();
|
||||
let click = anchor + Vec2::new(1.5, -1.0);
|
||||
let away = anchor + Vec2::new(-83.3, -61.7);
|
||||
let ev = |kind, q: Point| PointerEvent::new(kind, q.x, q.y);
|
||||
let preview = |acts: &[Action]| {
|
||||
acts.iter().find_map(|a| if let Action::Preview(_, v) = a { Some(v.clone()) } else { None }).unwrap_or_else(|| panic!("{acts:?}"))
|
||||
};
|
||||
let at = |v: &serde_json::Value| Point::new(v[0].as_f64().unwrap(), v[1].as_f64().unwrap());
|
||||
for glyphs in [true, false] {
|
||||
let c = ToolContext { snap_to_glyph: glyphs, ..cx(&d, &on_type, &p) };
|
||||
let lands = |q: Point| q.distance(anchor) < 1e-9;
|
||||
// The pointer hovering at the click shows the guides of the anchor a vector would start on.
|
||||
let mut tool = GradientTool::default();
|
||||
tool.pointer(&c, &ev(PointerKind::Move, click));
|
||||
assert_eq!(!tool.overlays(&c).is_empty(), glyphs, "hover, Snap to Glyph {glyphs}");
|
||||
// A vector drawn from the click starts on the anchor.
|
||||
tool.pointer(&c, &ev(PointerKind::Down, click));
|
||||
let v = preview(&tool.pointer(&c, &ev(PointerKind::Drag, click + Vec2::new(80.0, 60.0))));
|
||||
assert_eq!(lands(at(&v["start"])), glyphs, "start, Snap to Glyph {glyphs}: {v}");
|
||||
tool.pointer(&c, &ev(PointerKind::Up, click + Vec2::new(80.0, 60.0)));
|
||||
// One drawn to beside the slanted line, a quarter of the way along (15 pt or more from its
|
||||
// bounds' edges, centre and anchors), ends on the line.
|
||||
let dir = (a1 - a0) / (a1 - a0).hypot();
|
||||
let quarter = a0 + (a1 - a0) * 0.25;
|
||||
tool.pointer(&c, &ev(PointerKind::Down, away));
|
||||
let v = preview(&tool.pointer(&c, &ev(PointerKind::Drag, quarter + Vec2::new(-dir.y, dir.x) * 2.0)));
|
||||
assert!((at(&v["end"]) - a0).cross(dir).abs() < 1e-6, "end on the line: {v}");
|
||||
tool.pointer(&c, &ev(PointerKind::Up, quarter));
|
||||
// One drawn to the click ends on it.
|
||||
tool.pointer(&c, &ev(PointerKind::Down, away));
|
||||
let v = preview(&tool.pointer(&c, &ev(PointerKind::Drag, click)));
|
||||
assert_eq!(lands(at(&v["end"])), glyphs, "end, Snap to Glyph {glyphs}: {v}");
|
||||
tool.pointer(&c, &ev(PointerKind::Up, click));
|
||||
// A click at the click applies a gradient that starts on the anchor.
|
||||
tool.pointer(&c, &ev(PointerKind::Down, click));
|
||||
let acts = tool.pointer(&c, &ev(PointerKind::Up, click));
|
||||
let v = acts.iter().find_map(|a| if let Action::Exec(_, v) = a { Some(v.clone()) } else { None }).unwrap_or_else(|| panic!("{acts:?}"));
|
||||
assert_eq!(lands(at(&v["start"])), glyphs, "click, Snap to Glyph {glyphs}: {v}");
|
||||
// One drawn to 2 px below the horizontal through its start, far from any target, ends on
|
||||
// that construction guide, as a Line Segment's end does.
|
||||
tool.pointer(&c, &ev(PointerKind::Down, away));
|
||||
let v = preview(&tool.pointer(&c, &ev(PointerKind::Drag, away + Vec2::new(150.0, 2.0))));
|
||||
assert!((at(&v["end"]).y - away.y).abs() < 1e-9, "end on the construction guide: {v}");
|
||||
tool.pointer(&c, &ev(PointerKind::Up, away + Vec2::new(150.0, 2.0)));
|
||||
// With Shift, one drawn at 45° toward the line's first anchor slides along that step onto it.
|
||||
let shift = |kind, q: Point| PointerEvent { mods: crate::Mods { shift: true, ..Default::default() }, ..ev(kind, q) };
|
||||
tool.pointer(&c, &shift(PointerKind::Down, a0 + Vec2::new(60.0, -60.0)));
|
||||
let v = preview(&tool.pointer(&c, &shift(PointerKind::Drag, a0 + Vec2::new(-1.5, 0.5))));
|
||||
assert!(at(&v["end"]).distance(a0) < 1e-9, "Shift end on the anchor: {v}");
|
||||
tool.pointer(&c, &shift(PointerKind::Up, a0));
|
||||
// A press 3 px from the anchor, then a move of 2 px away from it: no drag yet.
|
||||
let near = anchor + Vec2::new(2.5, -1.5);
|
||||
tool.pointer(&c, &ev(PointerKind::Down, near));
|
||||
let acts = tool.pointer(&c, &ev(PointerKind::Drag, near + Vec2::new(1.5, -1.0)));
|
||||
assert!(acts.is_empty(), "drag start, Snap to Glyph {glyphs}: {acts:?}");
|
||||
tool.pointer(&c, &ev(PointerKind::Up, near + Vec2::new(1.5, -1.0)));
|
||||
// The end handle of the rectangle's gradient dragged to the click lands on the anchor.
|
||||
let c = ToolContext { snap_to_glyph: glyphs, ..cx(&d, &on_rect, &p) };
|
||||
let mut tool = GradientTool::default();
|
||||
// Hovering over the handle (on the rectangle's edge) shows no guides.
|
||||
tool.pointer(&c, &ev(PointerKind::Move, Point::new(400.0, 100.0)));
|
||||
assert!(!tool.overlays(&c).iter().any(|o| matches!(o, Overlay::Label { .. })), "hover over the handle");
|
||||
tool.pointer(&c, &ev(PointerKind::Down, Point::new(400.0, 100.0)));
|
||||
let v = preview(&tool.pointer(&c, &ev(PointerKind::Drag, click)));
|
||||
assert_eq!(lands(at(&v["end"])), glyphs, "end handle, Snap to Glyph {glyphs}: {v}");
|
||||
tool.pointer(&c, &ev(PointerKind::Up, click));
|
||||
// The start handle dragged to the click lands on the anchor.
|
||||
tool.pointer(&c, &ev(PointerKind::Down, Point::new(300.0, 100.0)));
|
||||
let v = preview(&tool.pointer(&c, &ev(PointerKind::Drag, click)));
|
||||
assert_eq!(lands(at(&v["start"])), glyphs, "start handle, Snap to Glyph {glyphs}: {v}");
|
||||
tool.pointer(&c, &ev(PointerKind::Up, click));
|
||||
// With nothing selected, hovering beside the rectangle (off the art and its 1 pt stroke,
|
||||
// where a press draws nothing, but within the snapping tolerance of its edge) shows no
|
||||
// guides.
|
||||
let none = Selection::default();
|
||||
let c = ToolContext { snap_to_glyph: glyphs, ..cx(&d, &none, &p) };
|
||||
let mut tool = GradientTool::default();
|
||||
tool.pointer(&c, &ev(PointerKind::Move, Point::new(296.2, 75.0)));
|
||||
assert!(tool.overlays(&c).is_empty(), "hover off the art: {:?}", tool.overlays(&c));
|
||||
}
|
||||
}
|
||||
|
||||
/// With Smart Guides off, View → Snap to Point still pulls a drawn point onto an anchor, but
|
||||
/// draws no Smart Guides: no "anchor" label, no lines.
|
||||
#[test]
|
||||
|
||||
+10
-2
@@ -624,8 +624,16 @@ mod cursor_tests {
|
||||
/// Use Precise Cursors turns it into the crosshair.
|
||||
#[test]
|
||||
fn drawing_tools_show_their_own_cursor() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (d, rect) = doc_with_rect();
|
||||
let away = Point::new(400.0, 400.0);
|
||||
// The Smooth tool shows its own cursor only with a selected path (Not Allowed without one).
|
||||
let mut selected = Selection::default();
|
||||
selected.set([rect]);
|
||||
let paint = paint();
|
||||
let cursor_of = |id: &str| match id {
|
||||
"smooth" => create(id).cursor(&cx(&d, &selected, &paint), away, Mods::default()),
|
||||
_ => cursor_at(id, &d, away),
|
||||
};
|
||||
for (id, c) in [
|
||||
("lasso", Cursor::Lasso),
|
||||
(addons::enclosing_lasso::ID, Cursor::Lasso),
|
||||
@@ -651,7 +659,7 @@ mod cursor_tests {
|
||||
("symbolStyler", Cursor::SymbolStyler),
|
||||
("mesh", Cursor::MeshAdd),
|
||||
] {
|
||||
assert_eq!(cursor_at(id, &d, away), c, "{id}");
|
||||
assert_eq!(cursor_of(id), c, "{id}");
|
||||
assert_eq!(c.precise(), Cursor::Crosshair, "{id}");
|
||||
// The Blob Brush's and the Eraser's dot is drawn: their tip outline goes round it. The
|
||||
// Live Paint Bucket's bucket is drawn.
|
||||
|
||||
+44
-13
@@ -14,7 +14,7 @@ use vectorcraft_doc::{NodeId, OrientedBox, SliceSource};
|
||||
use vectorcraft_geom::{Affine, Point, Rect, Vec2};
|
||||
|
||||
use crate::bbox::{Handle, hit_handle, move_delta, scale_for_drag};
|
||||
use crate::guides::{PointSnap, Targets};
|
||||
use crate::guides::{DrawSnap, PointSnap, Targets};
|
||||
use crate::shape::drag_rect;
|
||||
use crate::xform::{BLUE, polygon, rect_corners};
|
||||
use crate::{Action, Cursor, Mods, Overlay, PointerEvent, PointerKind, Tool, ToolContext, ToolKey, json_ids};
|
||||
@@ -37,9 +37,26 @@ fn has_area(r: Rect) -> bool {
|
||||
r.width() > 1e-6 && r.height() > 1e-6
|
||||
}
|
||||
|
||||
/// Snap `p` as the drawing tools do, then to the nearest ruler guide within reach (guides win).
|
||||
fn snap(cx: &ToolContext, p: Point) -> (Point, Vec<Overlay>) {
|
||||
let (mut q, ov) = crate::guides::snap_draw(cx, p, &[]);
|
||||
/// When the Slice tool snaps a point.
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Phase {
|
||||
/// The pointer hovers before a press.
|
||||
Hover,
|
||||
/// The first corner is pressed: the targets are gathered afresh.
|
||||
Press,
|
||||
/// The other corner is dragged: the press's targets are kept.
|
||||
Drag,
|
||||
}
|
||||
|
||||
/// Snap `p` as the drawing tools do ([`DrawSnap`]), then to the nearest ruler guide within reach
|
||||
/// (guides win, and the Smart Guides are then not shown).
|
||||
fn snap(draw: &mut DrawSnap, cx: &ToolContext, p: Point, phase: Phase) -> (Point, Vec<Overlay>) {
|
||||
let mut q = match phase {
|
||||
Phase::Hover => draw.hover(cx, p, &[], None),
|
||||
Phase::Press => draw.press(cx, p, &[], None),
|
||||
Phase::Drag => draw.drag(cx, p, None),
|
||||
};
|
||||
let mut ov = draw.guides().to_vec();
|
||||
let tol = cx.tol(5.0);
|
||||
let guides = cx.doc.ruler_guides();
|
||||
let nearest = |vertical: bool, v: f64| {
|
||||
@@ -50,9 +67,11 @@ fn snap(cx: &ToolContext, p: Point) -> (Point, Vec<Overlay>) {
|
||||
};
|
||||
if let Some(g) = nearest(true, p.x) {
|
||||
q.x = g.pos;
|
||||
ov.clear();
|
||||
}
|
||||
if let Some(g) = nearest(false, p.y) {
|
||||
q.y = g.pos;
|
||||
ov.clear();
|
||||
}
|
||||
(q, ov)
|
||||
}
|
||||
@@ -64,6 +83,8 @@ pub struct SliceTool {
|
||||
last: Point,
|
||||
began: bool,
|
||||
guides: Vec<Overlay>,
|
||||
/// Smart Guides for the slice's corners, and for the pointer hovering before a press.
|
||||
snap: DrawSnap,
|
||||
}
|
||||
|
||||
impl Tool for SliceTool {
|
||||
@@ -77,14 +98,20 @@ impl Tool for SliceTool {
|
||||
|
||||
fn pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {
|
||||
match ev.kind {
|
||||
PointerKind::Move => {
|
||||
if self.start.is_none() {
|
||||
self.guides = snap(&mut self.snap, cx, ev.pos, Phase::Hover).1;
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
PointerKind::Down => {
|
||||
let (p, _) = snap(cx, ev.pos);
|
||||
(self.start, self.last, self.began) = (Some(p), p, false);
|
||||
let (p, guides) = snap(&mut self.snap, cx, ev.pos, Phase::Press);
|
||||
(self.start, self.last, self.began, self.guides) = (Some(p), p, false, guides);
|
||||
vec![]
|
||||
}
|
||||
PointerKind::Drag => {
|
||||
let Some(start) = self.start else { return vec![] };
|
||||
let (p, guides) = snap(cx, ev.pos);
|
||||
let (p, guides) = snap(&mut self.snap, cx, ev.pos, Phase::Drag);
|
||||
(self.last, self.guides) = (p, guides);
|
||||
let mut out = vec![];
|
||||
if !self.began {
|
||||
@@ -102,6 +129,7 @@ impl Tool for SliceTool {
|
||||
}
|
||||
PointerKind::Up => {
|
||||
self.guides.clear();
|
||||
self.snap.clear();
|
||||
let began = std::mem::take(&mut self.began);
|
||||
if self.start.take().is_some() && began { vec![Action::Commit] } else { vec![] }
|
||||
}
|
||||
@@ -123,6 +151,8 @@ impl Tool for SliceTool {
|
||||
}
|
||||
o
|
||||
}
|
||||
// The hovering pointer's Smart Guides.
|
||||
None => self.guides.clone(),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
@@ -134,6 +164,7 @@ impl Tool for SliceTool {
|
||||
fn deactivate(&mut self, _cx: &ToolContext) -> Vec<Action> {
|
||||
self.start = None;
|
||||
self.guides.clear();
|
||||
self.snap.clear();
|
||||
if std::mem::take(&mut self.began) { vec![Action::Cancel] } else { vec![] }
|
||||
}
|
||||
}
|
||||
@@ -152,8 +183,8 @@ impl Drag {
|
||||
}
|
||||
}
|
||||
|
||||
/// What a user slice's handle snaps to, gathered when it is pressed: what the Slice tool's drag
|
||||
/// snaps to ([`snap`]), the art through [`PointSnap`] and the ruler guides on their own.
|
||||
/// What a user slice's handle snaps to, gathered when it is pressed: the art's bounds through
|
||||
/// [`PointSnap`], as a bounding-box handle snaps, and the ruler guides on their own.
|
||||
struct ResizeSnap {
|
||||
art: PointSnap,
|
||||
rulers: Targets,
|
||||
@@ -164,10 +195,10 @@ impl ResizeSnap {
|
||||
Self { art: PointSnap::scaling(cx, || Targets::collect(cx.doc, &[], None)), rulers: Targets::rulers(cx) }
|
||||
}
|
||||
|
||||
/// Snap the resize `a` ([`scale_for_drag`]) of the slice `rect` by `handle` as [`snap`] snaps
|
||||
/// a drawn slice's corner: the handle snaps as a bounding-box handle does
|
||||
/// ([`PointSnap::snap_scale`]), then a ruler guide within 5 px of where that leaves it takes
|
||||
/// what the handle moves on its axis. Returns the scale and the guides showing why.
|
||||
/// Snap the resize `a` ([`scale_for_drag`]) of the slice `rect` by `handle`: the handle snaps
|
||||
/// as a bounding-box handle does ([`PointSnap::snap_scale`]), then a ruler guide within 5 px of
|
||||
/// where that leaves it takes what the handle moves on its axis, as [`snap`] does for a drawn
|
||||
/// slice's corner. Returns the scale and the guides showing why.
|
||||
fn scale(&self, cx: &ToolContext, rect: Rect, handle: Handle, a: Affine, m: Mods) -> (Affine, Vec<Overlay>) {
|
||||
let bx = OrientedBox::aligned(rect);
|
||||
let (a, ov) = self.art.snap_scale(cx, &bx, handle, a, m.shift, m.alt);
|
||||
|
||||
Loaded 100 of 181 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user