mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-23 10:53:35 +01:00
Merge main into perf/flatten-sweep-staggered-boxes
This commit is contained in:
commit
0296e9e12f
171 files changed
+11513
-1205
No files matched your search
Generated
+2
@@ -6386,6 +6386,7 @@ dependencies = [
|
||||
"pollster 1.0.1",
|
||||
"raw-window-handle",
|
||||
"rfd",
|
||||
"rustix 1.1.6",
|
||||
"serde_json",
|
||||
"socket2",
|
||||
"tempfile",
|
||||
@@ -6822,6 +6823,7 @@ dependencies = [
|
||||
"skrifa 0.47.0",
|
||||
"svgtypes 0.16.1",
|
||||
"thiserror 2.0.21",
|
||||
"unicode-bidi",
|
||||
"usvg 0.48.1",
|
||||
"vectorcraft-brush",
|
||||
"vectorcraft-color",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -108,37 +108,14 @@ impl eframe::App for App {
|
||||
self.app.start_pending_update();
|
||||
#[cfg(target_os = "macos")]
|
||||
super::mac_cursor::release();
|
||||
// Dropping the app can wait for the clipboard handles to let go: the windows leave the
|
||||
// screen first (`quit_windows`).
|
||||
#[cfg(not(any(target_os = "macos", windows)))]
|
||||
hide_before_teardown(self.winit_window.as_deref());
|
||||
super::quit_windows::before_teardown(self.winit_window.as_deref());
|
||||
end_before_teardown(self.frames);
|
||||
}
|
||||
}
|
||||
|
||||
/// The main window as [`hide_before_teardown`] sees it.
|
||||
#[cfg(not(any(target_os = "macos", windows)))]
|
||||
pub(crate) trait QuittingWindow {
|
||||
fn hide(&self);
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "macos", windows)))]
|
||||
impl QuittingWindow for winit::window::Window {
|
||||
fn hide(&self) {
|
||||
self.set_visible(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// On Linux and the BSDs, hide the main window before eframe drops the app. Dropping the app waits
|
||||
/// up to [`vectorcraft_ui_egui::CLIPBOARD_PROBE_STOP_WAIT`] for the clipboard handles to let go, and
|
||||
/// the window would stay on screen for that time. The window is hidden, not closed: the Wayland
|
||||
/// clipboard reads text on the window's connection. winit hides X11 windows (XWayland included) and
|
||||
/// cannot hide a Wayland window.
|
||||
#[cfg(not(any(target_os = "macos", windows)))]
|
||||
pub(crate) fn hide_before_teardown<W: QuittingWindow + ?Sized>(window: Option<&W>) {
|
||||
if let Some(window) = window {
|
||||
window.hide();
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
/// The frame's times for the hang report, and its last stage ([`hang_beacon::after_frame`]): a
|
||||
/// focused, shown window whose windows had input waits for the renderer and the next frame,
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -60,6 +60,8 @@ mod portal_request;
|
||||
mod portal_save;
|
||||
mod prefs_dir;
|
||||
mod printing;
|
||||
#[cfg(not(any(target_os = "macos", windows)))]
|
||||
mod quit_windows;
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
mod single_instance;
|
||||
#[cfg(windows)]
|
||||
@@ -90,8 +92,6 @@ mod titlebar;
|
||||
use app::bring_window_forward;
|
||||
use app::native_input_hook;
|
||||
use app::open_or_hold;
|
||||
#[cfg(all(test, not(any(target_os = "macos", windows))))]
|
||||
use app::{QuittingWindow, hide_before_teardown};
|
||||
use host_services::services;
|
||||
#[cfg(all(test, feature = "wgpu"))]
|
||||
use prefs::{KEEP_UI_PREFS, decode_saved_prefs, keep_prefs_copy, prefs_bytes};
|
||||
@@ -122,7 +122,7 @@ struct App {
|
||||
/// Keeps the custom title bar's window borderless (Windows, #1758).
|
||||
window_frame: Option<window_frame::WindowFrameFn>,
|
||||
/// The main window; used to read and set maximized state the UI layer does not see on Linux (#3635),
|
||||
/// and to hide the window on quit before the clipboard handles let go (`app::hide_before_teardown`).
|
||||
/// and to take the window off screen on quit before the clipboard handles let go (`quit_windows`).
|
||||
winit_window: Option<std::sync::Arc<winit::window::Window>>,
|
||||
/// A saved or fitted geometry asked for maximize once the window is mapped (Linux).
|
||||
pending_maximize: bool,
|
||||
@@ -418,6 +418,10 @@ fn main() -> std::process::ExitCode {
|
||||
}));
|
||||
options
|
||||
};
|
||||
// Quitting takes the X11 windows of floating panels and documents off screen with the main
|
||||
// window, so eframe's windows are recorded as it makes and drops them (`quit_windows`).
|
||||
#[cfg(target_os = "linux")]
|
||||
let options = eframe::NativeOptions { viewport_hooks: Some(std::sync::Arc::new(quit_windows::Hooks)), ..options };
|
||||
#[cfg(feature = "wgpu")]
|
||||
let options = {
|
||||
let mut options = options;
|
||||
|
||||
@@ -242,7 +242,7 @@ fn parent(window: &winit::window::Window) -> (String, Option<()>) {
|
||||
/// (<https://wayland.app/protocols/xdg-foreign-unstable-v2>), made as rfd's `window_identifier`
|
||||
/// makes it.
|
||||
#[cfg(target_os = "linux")]
|
||||
mod wayland {
|
||||
pub(crate) mod wayland {
|
||||
use wayland_client::globals::{GlobalListContents, registry_queue_init};
|
||||
use wayland_client::protocol::wl_registry::{self, WlRegistry};
|
||||
use wayland_client::protocol::wl_surface::WlSurface;
|
||||
@@ -308,9 +308,10 @@ mod wayland {
|
||||
}
|
||||
}
|
||||
|
||||
/// `window`'s Wayland connection and surface, from its raw handles.
|
||||
/// `window`'s Wayland connection and surface, from its raw handles. The caller holds `window`
|
||||
/// while it uses them (`quit_windows` unmaps the main window's surface through them).
|
||||
#[allow(unsafe_code)]
|
||||
fn surface(window: &winit::window::Window) -> Option<(Connection, WlSurface)> {
|
||||
pub(crate) fn surface(window: &winit::window::Window) -> Option<(Connection, WlSurface)> {
|
||||
use wayland_backend::sys::client::{Backend, ObjectId};
|
||||
use winit::raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _, RawDisplayHandle, RawWindowHandle};
|
||||
let (RawWindowHandle::Wayland(w), RawDisplayHandle::Wayland(d)) =
|
||||
@@ -319,8 +320,8 @@ mod wayland {
|
||||
return None;
|
||||
};
|
||||
// SAFETY: the display is winit's open `wl_display`, which outlives `window` (the caller
|
||||
// holds it until the export is dropped); a backend made from a foreign display does not
|
||||
// close it when dropped.
|
||||
// holds it while it uses the connection, and the export holds it until it is dropped); a
|
||||
// backend made from a foreign display does not close it when dropped.
|
||||
let backend = unsafe { Backend::from_foreign_display(d.display.as_ptr().cast()) };
|
||||
let conn = Connection::from_backend(backend);
|
||||
// SAFETY: the pointer is the window's live `wl_surface` proxy on that display.
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
//! Takes the app's windows off screen when it quits on Linux and the BSDs, before eframe drops the
|
||||
//! app. Dropping the app waits up to [`vectorcraft_ui_egui::CLIPBOARD_PROBE_STOP_WAIT`] for the
|
||||
//! clipboard handles to let go, and eframe closes the windows only after that wait.
|
||||
//!
|
||||
//! The main window is hidden on X11 (XWayland included). winit cannot hide a Wayland window, so on
|
||||
//! Linux a Wayland main window is unmapped instead: its surface gets no buffer, and the window stays
|
||||
//! open with the connection the Wayland clipboard copies and reads on. On Linux the X11 windows of
|
||||
//! floating panels and documents are recorded as eframe creates and drops them ([`Hooks`]), and
|
||||
//! quitting unmaps them through an X11 connection of its own. On native Wayland, floating panels and
|
||||
//! documents are normally drawn inside the main window.
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
use std::collections::HashMap;
|
||||
#[cfg(target_os = "linux")]
|
||||
use std::sync::{LazyLock, Mutex, MutexGuard, PoisonError};
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
use winit::raw_window_handle::{HasWindowHandle as _, RawWindowHandle};
|
||||
|
||||
/// The main window as quitting sees it.
|
||||
pub(crate) trait QuittingWindow {
|
||||
/// The window is a Wayland surface.
|
||||
fn is_wayland(&self) -> bool;
|
||||
/// Hides an X11 window.
|
||||
fn hide(&self);
|
||||
/// Takes a Wayland window off screen and keeps it open.
|
||||
fn unmap(&self);
|
||||
}
|
||||
|
||||
impl QuittingWindow for winit::window::Window {
|
||||
#[cfg(target_os = "linux")]
|
||||
fn is_wayland(&self) -> bool {
|
||||
matches!(self.window_handle().map(|h| h.as_raw()), Ok(RawWindowHandle::Wayland(_)))
|
||||
}
|
||||
|
||||
/// The BSDs have no Wayland unmap: their windows are hidden through winit.
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn is_wayland(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn hide(&self) {
|
||||
self.set_visible(false);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn unmap(&self) {
|
||||
unmap_wayland(self);
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn unmap(&self) {
|
||||
self.set_visible(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes the main window and the floating windows `x11` lists off screen: a Wayland main window is
|
||||
/// unmapped and any other main window hidden, and `unmap_x11` unmaps the floating windows.
|
||||
pub(crate) fn off_screen<W: QuittingWindow + ?Sized>(main: Option<&W>, x11: &[u32], unmap_x11: impl FnOnce(&[u32])) {
|
||||
if let Some(window) = main {
|
||||
if window.is_wayland() {
|
||||
window.unmap();
|
||||
} else {
|
||||
window.hide();
|
||||
}
|
||||
}
|
||||
if !x11.is_empty() {
|
||||
unmap_x11(x11);
|
||||
}
|
||||
}
|
||||
|
||||
/// [`off_screen`] with the running app's main window and the floating windows [`Hooks`] recorded.
|
||||
/// Runs on quit, before eframe drops the app.
|
||||
pub(crate) fn before_teardown(main: Option<&winit::window::Window>) {
|
||||
#[cfg(target_os = "linux")]
|
||||
off_screen(main, &CHILD_WINDOWS.x11(), unmap_x11);
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
off_screen(main, &[], |_| {});
|
||||
}
|
||||
|
||||
/// The X11 windows of floating panels and documents, by viewport, while eframe holds them.
|
||||
#[cfg(target_os = "linux")]
|
||||
#[derive(Default)]
|
||||
pub(crate) struct ChildWindows(Mutex<HashMap<egui::ViewportId, u32>>);
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
impl ChildWindows {
|
||||
/// eframe made `viewport`'s window, with `handle`. The main window, and windows of other window
|
||||
/// systems, are not recorded; a window made again for the same viewport replaces its record.
|
||||
pub(crate) fn created(&self, viewport: egui::ViewportId, handle: Option<RawWindowHandle>) {
|
||||
let mut windows = self.lock();
|
||||
match handle.and_then(x11_window) {
|
||||
Some(id) if viewport != egui::ViewportId::ROOT => windows.insert(viewport, id),
|
||||
_ => windows.remove(&viewport),
|
||||
};
|
||||
}
|
||||
|
||||
/// eframe dropped `viewport`'s window.
|
||||
pub(crate) fn destroyed(&self, viewport: egui::ViewportId) {
|
||||
self.lock().remove(&viewport);
|
||||
}
|
||||
|
||||
/// The recorded windows, in id order.
|
||||
pub(crate) fn x11(&self) -> Vec<u32> {
|
||||
let mut ids: Vec<u32> = self.lock().values().copied().collect();
|
||||
ids.sort_unstable();
|
||||
ids.dedup();
|
||||
ids
|
||||
}
|
||||
|
||||
fn lock(&self) -> MutexGuard<'_, HashMap<egui::ViewportId, u32>> {
|
||||
self.0.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
}
|
||||
|
||||
/// The X11 window id of `handle`, None for another window system.
|
||||
#[cfg(target_os = "linux")]
|
||||
fn x11_window(handle: RawWindowHandle) -> Option<u32> {
|
||||
match handle {
|
||||
RawWindowHandle::Xlib(h) => u32::try_from(h.window).ok(),
|
||||
RawWindowHandle::Xcb(h) => Some(h.window.get()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The running app's floating windows, which [`Hooks`] records.
|
||||
#[cfg(target_os = "linux")]
|
||||
static CHILD_WINDOWS: LazyLock<ChildWindows> = LazyLock::new(ChildWindows::default);
|
||||
|
||||
/// Records the windows eframe creates and drops in [`CHILD_WINDOWS`]; set as
|
||||
/// [`eframe::NativeOptions::viewport_hooks`].
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(crate) struct Hooks;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
impl eframe::ViewportHooks for Hooks {
|
||||
fn window_created(&self, viewport: egui::ViewportId, window: &winit::window::Window) {
|
||||
CHILD_WINDOWS.created(viewport, window.window_handle().ok().map(|h| h.as_raw()));
|
||||
}
|
||||
|
||||
fn window_destroyed(&self, viewport: egui::ViewportId) {
|
||||
CHILD_WINDOWS.destroyed(viewport);
|
||||
}
|
||||
}
|
||||
|
||||
/// Unmaps the X11 `windows` through a connection of its own, and waits until the server has done so.
|
||||
/// An id whose window is gone gets an error, which is ignored.
|
||||
#[cfg(target_os = "linux")]
|
||||
fn unmap_x11(windows: &[u32]) {
|
||||
use x11rb::protocol::xproto::ConnectionExt as _;
|
||||
let conn = match x11rb::connect(None) {
|
||||
Ok((conn, _)) => conn,
|
||||
Err(e) => {
|
||||
log::warn!("couldn't reach the X server to hide the floating windows on quit: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
for &window in windows {
|
||||
if let Ok(cookie) = conn.unmap_window(window) {
|
||||
cookie.ignore_error();
|
||||
}
|
||||
}
|
||||
// The server answers once it has handled the requests before this one.
|
||||
if let Ok(cookie) = conn.get_input_focus() {
|
||||
let _ = cookie.reply();
|
||||
}
|
||||
}
|
||||
|
||||
/// Attaches no buffer to `window`'s Wayland surface and commits it, which unmaps the window: the
|
||||
/// compositor stops showing it, and the window and its connection stay open.
|
||||
#[cfg(target_os = "linux")]
|
||||
fn unmap_wayland(window: &winit::window::Window) {
|
||||
let Some((conn, surface)) = super::portal_save::wayland::surface(window) else { return };
|
||||
surface.attach(None, 0, 0);
|
||||
surface.commit();
|
||||
if let Err(e) = conn.flush() {
|
||||
log::warn!("couldn't reach the compositor to hide the window on quit: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::cell::RefCell;
|
||||
|
||||
/// A main window that records what quitting did to it.
|
||||
struct Window<'a> {
|
||||
wayland: bool,
|
||||
done: &'a RefCell<Vec<String>>,
|
||||
}
|
||||
|
||||
impl QuittingWindow for Window<'_> {
|
||||
fn is_wayland(&self) -> bool {
|
||||
self.wayland
|
||||
}
|
||||
fn hide(&self) {
|
||||
self.done.borrow_mut().push("hide".into());
|
||||
}
|
||||
fn unmap(&self) {
|
||||
self.done.borrow_mut().push("unmap".into());
|
||||
}
|
||||
}
|
||||
|
||||
fn quit(wayland: Option<bool>, x11: &[u32]) -> Vec<String> {
|
||||
let done = RefCell::new(Vec::new());
|
||||
let window = wayland.map(|wayland| Window { wayland, done: &done });
|
||||
off_screen(window.as_ref(), x11, |ids| done.borrow_mut().push(format!("unmap x11 {ids:?}")));
|
||||
done.into_inner()
|
||||
}
|
||||
|
||||
/// winit cannot hide a Wayland window: quitting unmaps it, and never only asks winit to hide it.
|
||||
#[test]
|
||||
fn quitting_unmaps_a_wayland_main_window() {
|
||||
assert_eq!(quit(Some(true), &[]), ["unmap"]);
|
||||
}
|
||||
|
||||
/// An X11 main window (XWayland included) is hidden, and the floating panels' and documents' X11
|
||||
/// windows are unmapped with it.
|
||||
#[test]
|
||||
fn quitting_hides_an_x11_main_window_and_its_floating_windows() {
|
||||
assert_eq!(quit(Some(false), &[]), ["hide"]);
|
||||
assert_eq!(quit(Some(false), &[7, 9]), ["hide", "unmap x11 [7, 9]"]);
|
||||
}
|
||||
|
||||
/// Without a main window (none was made), the floating windows still go, and with none at all
|
||||
/// nothing is done.
|
||||
#[test]
|
||||
fn quitting_without_a_main_window() {
|
||||
assert_eq!(quit(None, &[3]), ["unmap x11 [3]"]);
|
||||
assert!(quit(None, &[]).is_empty());
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn xlib(id: std::os::raw::c_ulong) -> Option<RawWindowHandle> {
|
||||
Some(RawWindowHandle::Xlib(winit::raw_window_handle::XlibWindowHandle::new(id)))
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn viewport(name: &str) -> egui::ViewportId {
|
||||
egui::ViewportId::from_hash_of(name)
|
||||
}
|
||||
|
||||
/// The floating windows eframe made are recorded until it drops them; the main window, which
|
||||
/// quitting reaches through winit, is not.
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn floating_x11_windows_are_recorded_while_they_exist() {
|
||||
let windows = ChildWindows::default();
|
||||
windows.created(egui::ViewportId::ROOT, xlib(1));
|
||||
windows.created(viewport("Layers"), xlib(9));
|
||||
let xcb = winit::raw_window_handle::XcbWindowHandle::new(std::num::NonZeroU32::new(5).expect("not zero"));
|
||||
windows.created(viewport("Untitled-2"), Some(RawWindowHandle::Xcb(xcb)));
|
||||
windows.created(viewport("Color"), xlib(7));
|
||||
assert_eq!(windows.x11(), [5, 7, 9]);
|
||||
windows.destroyed(viewport("Color"));
|
||||
assert_eq!(windows.x11(), [5, 9]);
|
||||
// A viewport that eframe has already dropped, or never made, is no error.
|
||||
windows.destroyed(viewport("Color"));
|
||||
windows.destroyed(viewport("Swatches"));
|
||||
assert_eq!(windows.x11(), [5, 9]);
|
||||
}
|
||||
|
||||
/// A window made again for a viewport replaces its record, and one of another window system, or
|
||||
/// one whose handle could not be read, removes it.
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn a_window_made_again_replaces_its_record() {
|
||||
let windows = ChildWindows::default();
|
||||
windows.created(viewport("Layers"), xlib(9));
|
||||
windows.created(viewport("Layers"), xlib(11));
|
||||
assert_eq!(windows.x11(), [11]);
|
||||
let surface = std::ptr::NonNull::<std::ffi::c_void>::dangling();
|
||||
let wayland = winit::raw_window_handle::WaylandWindowHandle::new(surface);
|
||||
windows.created(viewport("Layers"), Some(RawWindowHandle::Wayland(wayland)));
|
||||
assert!(windows.x11().is_empty());
|
||||
windows.created(viewport("Layers"), xlib(11));
|
||||
windows.created(viewport("Layers"), None);
|
||||
assert!(windows.x11().is_empty());
|
||||
}
|
||||
}
|
||||
@@ -515,21 +515,3 @@ fn the_linux_desktop_entry_opens_every_readable_format() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// On Linux and the BSDs, quitting hides the main window before eframe drops the app, whose
|
||||
/// clipboard handles may take up to `CLIPBOARD_PROBE_STOP_WAIT` to let go.
|
||||
#[cfg(not(any(target_os = "macos", windows)))]
|
||||
#[test]
|
||||
fn quitting_hides_the_window_before_the_clipboards_let_go() {
|
||||
struct Window(std::cell::Cell<u32>);
|
||||
impl QuittingWindow for Window {
|
||||
fn hide(&self) {
|
||||
self.0.set(self.0.get() + 1);
|
||||
}
|
||||
}
|
||||
let window = Window(std::cell::Cell::new(0));
|
||||
hide_before_teardown(Some(&window));
|
||||
assert_eq!(window.0.get(), 1, "the window was not hidden");
|
||||
// Without a window (none was made), there is nothing to hide.
|
||||
hide_before_teardown::<Window>(None);
|
||||
}
|
||||
@@ -363,6 +363,14 @@ pub struct SeriesPaint {
|
||||
/// Object blend mode of each path directly in the series group, as [`Self::opacity`]. Absent: Normal.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub blend: Option<vectorcraft_color::BlendMode>,
|
||||
/// A gradient in [`Self::fill`] is placed relative to the unit box (0, 0)–(1, 1), and each mark takes it on its own
|
||||
/// geometric bounds. False: the gradient is in document coordinates, and every mark takes it unchanged.
|
||||
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
||||
pub fill_unit_box: bool,
|
||||
/// A gradient in [`Self::stroke`] is placed relative to the unit box, as [`Self::fill_unit_box`], and each mark takes
|
||||
/// it on its stroke's box (the geometric bounds grown by half the weight).
|
||||
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
||||
pub stroke_unit_box: bool,
|
||||
}
|
||||
|
||||
/// The object rows one kind of graph label keeps ([`LabelStyles::values_rows`]): the paint, weight, opacity and blend mode
|
||||
|
||||
@@ -18,7 +18,7 @@ mod wrap;
|
||||
pub use area::{AreaFit, AreaOptions, PathAlign, PathEffect, TextFlow, TextWrap, VerticalAlign, WrapShape};
|
||||
use char_style::yes;
|
||||
pub use char_style::{
|
||||
CharAlign, CharPosition, CharStyle, Digits, FontHeightReference, GlyphChoice, MAX_AKI, MAX_WARICHU_GAP, ScriptMetrics, WARICHU_LINES,
|
||||
CharAlign, CharPosition, CharStyle, Digits, FontHeightReference, GlyphChoice, MAX_AKI, MAX_TSUME, MAX_WARICHU_GAP, ScriptMetrics, WARICHU_LINES,
|
||||
WARICHU_SCALE, WarichuAlign,
|
||||
};
|
||||
pub use composite::{
|
||||
|
||||
@@ -44,6 +44,14 @@ pub struct CharStyle {
|
||||
/// Insert Aki Right: space added after the character in reading order, as [`Self::aki_left`].
|
||||
#[serde(default, rename = "akiRight", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub aki_right: f64,
|
||||
/// Tsume (Character panel, East Asian options): the percentage of the blank space on each side
|
||||
/// of the character's ink, inside its own width, that is taken away, 0 to [`MAX_TSUME`]. 50
|
||||
/// halves that space and 100 sets the ink edge to edge; the glyph itself is not scaled. A
|
||||
/// cluster takes the value of its first character. Tracking, kerning and Insert Aki are added
|
||||
/// after it. Horizontal type only.
|
||||
/// <https://helpx.adobe.com/illustrator/using/formatting-asian-characters.html>
|
||||
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
|
||||
pub tsume: f64,
|
||||
/// Use glyph shapes for automatic pair spacing; false uses font metrics.
|
||||
#[serde(default, rename = "opticalKerning", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub optical_kerning: bool,
|
||||
@@ -440,6 +448,9 @@ fn hundred() -> f64 {
|
||||
/// The largest Insert Aki Left or Right ([`CharStyle::aki_left`]), in 1/1000 em: one em.
|
||||
pub const MAX_AKI: f64 = 1000.0;
|
||||
|
||||
/// The largest Tsume ([`CharStyle::tsume`]), in percent: all the blank space beside the ink.
|
||||
pub const MAX_TSUME: f64 = 100.0;
|
||||
|
||||
/// The number of lines a warichu block may be stacked on ([`CharStyle::warichu_lines`]).
|
||||
pub const WARICHU_LINES: std::ops::RangeInclusive<u8> = 1..=6;
|
||||
|
||||
@@ -534,6 +545,7 @@ impl Default for CharStyle {
|
||||
kerning_before: 0.0,
|
||||
aki_left: 0.0,
|
||||
aki_right: 0.0,
|
||||
tsume: 0.0,
|
||||
optical_kerning: false,
|
||||
baseline_shift: 0.0,
|
||||
h_scale: 100.0,
|
||||
@@ -588,7 +600,8 @@ impl CharStyle {
|
||||
/// becomes the nearer end. A leading of 0, which legacy `.ai` import stores, stays, and a
|
||||
/// baseline shift that is not a number becomes 0. The Tate-chu-yoko Up/Down and Left/Right
|
||||
/// offsets are kept as the baseline shift is. Insert Aki Left and Right are kept from 0 to
|
||||
/// [`MAX_AKI`], and one that is not a number becomes 0. The Warichu Settings Lines and Scale
|
||||
/// [`MAX_AKI`], and one that is not a number becomes 0; Tsume is kept from 0 to [`MAX_TSUME`] the
|
||||
/// same way. The Warichu Settings Lines and Scale
|
||||
/// are kept to [`WARICHU_LINES`] and [`WARICHU_SCALE`]; a gap or scale that is not a number becomes its default. The gap is kept to 0 to
|
||||
/// [`crate::MAX_COORD`], as the baseline shift is, since a scaled type object holds it in its
|
||||
/// own units.
|
||||
@@ -606,6 +619,7 @@ impl CharStyle {
|
||||
let aki = |v: f64| if v.is_nan() { 0.0 } else { v.clamp(0.0, MAX_AKI) };
|
||||
self.aki_left = aki(self.aki_left);
|
||||
self.aki_right = aki(self.aki_right);
|
||||
self.tsume = if self.tsume.is_nan() { 0.0 } else { self.tsume.clamp(0.0, MAX_TSUME) };
|
||||
self.warichu_lines = self.warichu_lines.clamp(*WARICHU_LINES.start(), *WARICHU_LINES.end());
|
||||
self.warichu_gap = if self.warichu_gap.is_nan() { default.warichu_gap } else { self.warichu_gap.clamp(0.0, crate::MAX_COORD) };
|
||||
self.warichu_scale =
|
||||
|
||||
@@ -507,6 +507,7 @@ impl crate::Document {
|
||||
st.tcy_left_right = num("tcyLeftRight").unwrap_or(st.tcy_left_right);
|
||||
st.aki_left = num("akiLeft").unwrap_or(st.aki_left);
|
||||
st.aki_right = num("akiRight").unwrap_or(st.aki_right);
|
||||
st.tsume = num("tsume").unwrap_or(st.tsume);
|
||||
st.warichu_gap = num("warichuGap").unwrap_or(st.warichu_gap);
|
||||
st.warichu_scale = num("warichuScale").unwrap_or(st.warichu_scale);
|
||||
st.keep_dimensions_in_range();
|
||||
@@ -532,6 +533,7 @@ impl crate::Document {
|
||||
("tcyLeftRight", Some(st.tcy_left_right)),
|
||||
("akiLeft", Some(st.aki_left)),
|
||||
("akiRight", Some(st.aki_right)),
|
||||
("tsume", Some(st.tsume)),
|
||||
("leading", st.leading),
|
||||
];
|
||||
for (k, v) in kept {
|
||||
|
||||
@@ -357,7 +357,7 @@ impl ParaList {
|
||||
|
||||
/// The style the marker is set in: `first` (the style of the paragraph's first character)
|
||||
/// without its baseline shift, rotation, superscript or subscript, tracking, manual kerning,
|
||||
/// Insert Aki, tate-chu-yoko, warichu, underline and strikethrough.
|
||||
/// Insert Aki, Tsume, tate-chu-yoko, warichu, underline and strikethrough.
|
||||
pub fn marker_style(first: &CharStyle) -> CharStyle {
|
||||
CharStyle {
|
||||
baseline_shift: 0.0,
|
||||
@@ -368,6 +368,7 @@ impl ParaList {
|
||||
kerning_before: 0.0,
|
||||
aki_left: 0.0,
|
||||
aki_right: 0.0,
|
||||
tsume: 0.0,
|
||||
optical_kerning: first.optical_kerning,
|
||||
tate_chu_yoko: false,
|
||||
warichu: false,
|
||||
|
||||
@@ -129,6 +129,34 @@ fn insert_aki_is_saved_only_when_set() {
|
||||
assert_eq!((d.char_styles[0].attrs["akiLeft"].as_f64(), d.char_styles[0].attrs["akiRight"].as_f64()), (Some(MAX_AKI), Some(0.0)));
|
||||
}
|
||||
|
||||
/// Tsume is 0 by default and saved only when not 0; styles written before it read as 0, and a
|
||||
/// value out of range from a file, in a run or a character style, is kept from 0 to 100.
|
||||
#[test]
|
||||
fn tsume_is_saved_only_when_set_and_kept_in_range() {
|
||||
let st = CharStyle::default();
|
||||
assert_eq!(st.tsume, 0.0);
|
||||
let saved = serde_json::to_value(&st).unwrap();
|
||||
assert!(saved.get("tsume").is_none());
|
||||
let read = serde_json::from_value::<CharStyle>(saved).unwrap();
|
||||
assert_eq!(read.tsume, 0.0);
|
||||
let set = CharStyle { tsume: 37.5, ..CharStyle::default() };
|
||||
let saved = serde_json::to_value(&set).unwrap();
|
||||
assert_eq!(saved.get("tsume").and_then(serde_json::Value::as_f64), Some(37.5));
|
||||
assert_eq!(serde_json::from_value::<CharStyle>(saved).unwrap(), set);
|
||||
for (stored, kept) in [(-5.0, 0.0), (250.0, MAX_TSUME), (f64::NAN, 0.0), (f64::INFINITY, MAX_TSUME), (50.0, 50.0)] {
|
||||
let mut damaged = CharStyle { tsume: stored, ..CharStyle::default() };
|
||||
damaged.keep_dimensions_in_range();
|
||||
assert_eq!(damaged.tsume, kept, "{stored}");
|
||||
}
|
||||
let mut d = crate::Document::new(100.0, 100.0);
|
||||
for tsume in [500, -1] {
|
||||
let attrs = serde_json::json!({ "tsume": tsume });
|
||||
d.char_styles.push(TextStyleDef { name: format!("Tight {tsume}"), attrs: attrs.as_object().cloned().unwrap_or_default() });
|
||||
}
|
||||
d.keep_type_values_in_range();
|
||||
assert_eq!((d.char_styles[0].attrs["tsume"].as_f64(), d.char_styles[1].attrs["tsume"].as_f64()), (Some(MAX_TSUME), Some(0.0)));
|
||||
}
|
||||
|
||||
/// Each Indic numeral system draws 0–9 (and the Arabic-Indic sets) as the ten digits of its
|
||||
/// script's block; every system saves under its id, which reads back, and other names are refused.
|
||||
#[test]
|
||||
|
||||
@@ -29,7 +29,7 @@ use crate::group::{has_own_paint, paints};
|
||||
use crate::reshape::{as_art, geometry_effects, glyph_outlines, outline_nested_text, reshape_leaves};
|
||||
use crate::{
|
||||
GeomContext, adjust_in_document, apply_geometry_carrying, crop_marks_art, evaluate_container, has_adjustment, has_crop_marks, has_geometry,
|
||||
has_item_geometry, has_pathfinder, is_pathfinder, merged_params, needs_outline, outline_text, reshape,
|
||||
has_instance_row_raster, has_item_geometry, has_pathfinder, is_pathfinder, merged_params, needs_outline, outline_text, reshape,
|
||||
};
|
||||
|
||||
fn item_effects(item: &AppearanceItem) -> &[vectorcraft_doc::Effect] {
|
||||
@@ -58,6 +58,7 @@ pub fn needs_bake(n: &Node) -> bool {
|
||||
|| has_geometry(&n.appearance.effects)
|
||||
|| n.projection().is_some()
|
||||
|| n.appearance.items.iter().any(|i| has_geometry(item_effects(i)))
|
||||
|| has_instance_row_raster(n)
|
||||
|| n.children().is_some_and(|ch| ch.iter().any(|c| needs_bake(c)))
|
||||
}
|
||||
|
||||
@@ -142,9 +143,11 @@ fn bake_node(d: &mut Document, n: &Node) -> Option<Node> {
|
||||
}
|
||||
// Type, images, symbol instances and live objects: reshaped through their outlines, and so are
|
||||
// type whose own fills or strokes have geometry effects and images whose own fills or strokes
|
||||
// paint a shape ([`has_item_geometry`]). Type that Transform effects only move and copy stays
|
||||
// type, moved to each place.
|
||||
if needs_outline(n) && (has_geometry(&n.appearance.effects) || n.projection().is_some() || has_item_geometry(n)) {
|
||||
// paint a shape ([`has_item_geometry`]), and symbol instances whose active fill or stroke has a
|
||||
// raster effect, which the fills and strokes of their art then carry
|
||||
// ([`has_instance_row_raster`]). Type that Transform effects only move and copy stays type,
|
||||
// moved to each place.
|
||||
if needs_outline(n) && (has_geometry(&n.appearance.effects) || n.projection().is_some() || has_item_geometry(n) || has_instance_row_raster(n)) {
|
||||
if let Some(m) = moved_type_group(n) {
|
||||
return Some(bake_pieces(d, m));
|
||||
}
|
||||
|
||||
@@ -661,8 +661,9 @@ pub fn has_container_appearance(n: &Node) -> bool {
|
||||
/// The regions a group's or layer's own fills and strokes paint, in document space, each with its
|
||||
/// fill rule: every visible member path and compound path, type as its glyph outlines and live
|
||||
/// objects evaluated; a compound shape as its area, with the symbol instances among its members
|
||||
/// as their symbols' art from `doc`; the members of a clip group or clipping layer without its
|
||||
/// clipping path. Images, other symbol instances and guides add nothing. A Repeat counts the
|
||||
/// as their symbols' art from `doc`; a clip group or clipping layer (`n` itself or one among its
|
||||
/// members) as the region its clipping path clips to ([`crate::clip::clip_outline`]), so members
|
||||
/// it hides add nothing. Images, other symbol instances and guides add nothing. A Repeat counts the
|
||||
/// Repeats in the art of `doc`'s symbols that its instances show toward its instance budget.
|
||||
pub fn member_shapes(doc: Option<&Document>, n: &Node) -> Vec<(PathData, FillRule)> {
|
||||
fn push(doc: Option<&Document>, n: &Node, out: &mut Vec<(PathData, FillRule)>) {
|
||||
@@ -690,8 +691,14 @@ pub fn member_shapes(doc: Option<&Document>, n: &Node) -> Vec<(PathData, FillRul
|
||||
}
|
||||
}
|
||||
fn members(doc: Option<&Document>, n: &Node, out: &mut Vec<(PathData, FillRule)>) {
|
||||
let clip = usize::from(n.clips());
|
||||
for c in n.children().into_iter().flatten().skip(clip) {
|
||||
// A clip group shows its members only inside its clipping path: its outline is that
|
||||
// region, not the clipped members' own shapes, which may reach far outside it (#7289).
|
||||
if n.clips() {
|
||||
let clip = n.children().and_then(|ch| ch.first());
|
||||
out.extend(clip.and_then(|c| crate::clip::clip_outline(c)).map(|(bp, rule)| (PathData::from_bezpath(&bp), rule)));
|
||||
return;
|
||||
}
|
||||
for c in n.children().into_iter().flatten() {
|
||||
push(doc, c, out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1757,3 +1757,38 @@ fn a_sublayers_ruler_guide_in_a_hidden_or_locked_group_stays_hidden_or_locked()
|
||||
assert_eq!(guides, [(NodeId(20), !hidden, locked)], "hidden {hidden}, locked {locked}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A group's own Fill + Offset Path below its contents follows the outline of a clip group among
|
||||
/// its members, its clipping path, not the clipped art it hides outside that path (#7289).
|
||||
#[test]
|
||||
fn a_groups_own_fill_follows_a_clip_groups_clipping_path_not_its_hidden_art() {
|
||||
let mut clip = square_at_100();
|
||||
clip.id = NodeId(2);
|
||||
if let NodeKind::Path { clipping, .. } = &mut clip.kind {
|
||||
*clipping = true;
|
||||
}
|
||||
// The clipped art reaches 100 pt past the clipping path on either side.
|
||||
let hidden = Node::path(NodeId(3), shapes::rectangle(Rect::new(0.0, 100.0, 300.0, 200.0)), black());
|
||||
let clipped = Node::new(NodeId(5), NodeKind::Group { children: vec![Arc::new(clip), Arc::new(hidden)], clip: true });
|
||||
let mut g = Node::group(NodeId(10), vec![Arc::new(clipped.clone())]);
|
||||
let mut fill = vectorcraft_doc::FillLayer::new(Paint::solid(Color::rgb(1.0, 0.0, 0.0)));
|
||||
fill.effects.push(offset_10());
|
||||
g.appearance.items = vec![AppearanceItem::Fill(fill)];
|
||||
g.appearance.set_contents_at(1);
|
||||
let near = |a: Rect, b: Rect| (a.x0 - b.x0).abs() < 0.5 && (a.y0 - b.y0).abs() < 0.5 && (a.x1 - b.x1).abs() < 0.5 && (a.y1 - b.y1).abs() < 0.5;
|
||||
let expected = Rect::new(90.0, 90.0, 210.0, 210.0);
|
||||
|
||||
let art = evaluate_container(None, &g).unwrap();
|
||||
let ch = art.children().unwrap();
|
||||
assert_eq!(ch.len(), 2, "the fill's art below the clip group");
|
||||
assert_eq!(ch[0].id, NodeId(10));
|
||||
let b = drawn_bounds(&ch[0]).unwrap();
|
||||
assert!(near(b, expected), "the offset fill hugs the clipping path: {b:?}");
|
||||
|
||||
// The clip group itself with that fill: the same outline.
|
||||
let mut own = clipped;
|
||||
own.appearance = g.appearance.clone();
|
||||
assert_eq!(member_shapes(None, &own).len(), 1);
|
||||
let b = member_shapes(None, &own)[0].0.bounds().unwrap();
|
||||
assert!(near(b, Rect::new(100.0, 100.0, 200.0, 200.0)), "the clipping path's region: {b:?}");
|
||||
}
|
||||
@@ -80,8 +80,9 @@ pub use marks::{CROP_MARKS, crop_marks_art, has_crop_marks};
|
||||
pub use pixel::{PIXEL_EFFECTS, PixelFx, PixelSpace};
|
||||
pub use raster::{RASTERIZE, RasterFx, Rasterize, color_param, follow_items, item_effects_in, outset, raster_effects, without_geometry};
|
||||
pub use reshape::{
|
||||
SYMBOL_ART_MAX_NODES, expand_outlined, follows_image_pixels, gives_image_shape, has_gradient_copies, has_item_geometry, has_painted_effects,
|
||||
has_transform_art, has_transform_copies, has_transform_options, image_paint, needs_outline, outline_art, outline_text, reshape,
|
||||
SYMBOL_ART_MAX_NODES, expand_outlined, follows_image_pixels, gives_image_shape, has_gradient_copies, has_instance_row_raster, has_item_geometry,
|
||||
has_painted_effects, has_transform_art, has_transform_copies, has_transform_options, image_paint, needs_outline, outline_art, outline_text,
|
||||
reshape,
|
||||
};
|
||||
pub use revolve::{
|
||||
CLASSIC_REVOLVE, REVOLVE, has_revolve, is_revolve, revolve_art, revolve_art_in, revolve_options, validate_revolve, validate_revolve_in,
|
||||
|
||||
@@ -162,15 +162,26 @@ fn active_stroke(n: &Node) -> Option<&vectorcraft_doc::StrokeLayer> {
|
||||
n.appearance.stroke().filter(|s| s.visible && s.opacity > 0.0)
|
||||
}
|
||||
|
||||
/// The visible geometry effects in `fx`, in order.
|
||||
fn geometry_of(fx: &[Effect]) -> Vec<Effect> {
|
||||
fx.iter().filter(|e| e.visible && is_geometry(&e.id)).cloned().collect()
|
||||
/// Is `n` a symbol instance whose active fill or stroke has a visible raster effect (a shadow, glow,
|
||||
/// blur, feather or other painted effect, [`crate::is_raster`])? Its art takes that effect on each
|
||||
/// fill or stroke in it that paints ([`add_instance_row_effects`]), and draws it there.
|
||||
pub fn has_instance_row_raster(n: &Node) -> bool {
|
||||
let raster = |fx: &[Effect]| fx.iter().any(|e| e.visible && crate::is_raster(&e.id));
|
||||
matches!(n.kind, NodeKind::SymbolInstance { .. })
|
||||
&& (active_fill(n).is_some_and(|f| raster(&f.effects)) || active_stroke(n).is_some_and(|s| raster(&s.effects)))
|
||||
}
|
||||
|
||||
/// `art`, the art symbol instance `n` places, with the visible geometry effects of `n`'s active
|
||||
/// fill added after the effects of every fill in it that paints, and those of its active stroke
|
||||
/// after the effects of every stroke in it that paints ([`active_fill`], [`active_stroke`]). Each
|
||||
/// fill or stroke keeps its paint, and the others keep their shapes. Live objects in it that are
|
||||
/// The visible geometry and raster effects in `fx`, in order: those a symbol instance's active fill
|
||||
/// or stroke passes on to its art.
|
||||
fn row_effects_of(fx: &[Effect]) -> Vec<Effect> {
|
||||
fx.iter().filter(|e| e.visible && (is_geometry(&e.id) || crate::is_raster(&e.id))).cloned().collect()
|
||||
}
|
||||
|
||||
/// `art`, the art symbol instance `n` places, with the visible geometry and raster effects of
|
||||
/// `n`'s active fill added after the effects of every fill in it that paints, and those of its
|
||||
/// active stroke after the effects of every stroke in it that paints ([`active_fill`],
|
||||
/// [`active_stroke`]): a Drop Shadow on the instance's fill shadows each filled shape in the art.
|
||||
/// Each fill or stroke keeps its paint, and the others keep their shapes. Live objects in it that are
|
||||
/// reached are evaluated first (with the symbols of `doc`), so the effects reshape what they draw.
|
||||
/// Clip groups' clipping paths, guides and the art of opacity masks keep their shapes. A symbol
|
||||
/// instance in the art takes the effects on its own active fill or stroke, or on a new fill or
|
||||
@@ -180,8 +191,8 @@ fn geometry_of(fx: &[Effect]) -> Vec<Effect> {
|
||||
/// instance in the art takes them.
|
||||
fn add_instance_row_effects(doc: Option<&Document>, n: &Node, art: &mut Node) {
|
||||
let NodeKind::SymbolInstance { symbol, .. } = &n.kind else { return };
|
||||
let fill = active_fill(n).map_or(vec![], |f| geometry_of(&f.effects));
|
||||
let stroke = active_stroke(n).map_or(vec![], |s| geometry_of(&s.effects));
|
||||
let fill = active_fill(n).map_or(vec![], |f| row_effects_of(&f.effects));
|
||||
let stroke = active_stroke(n).map_or(vec![], |s| row_effects_of(&s.effects));
|
||||
if !fill.is_empty() || !stroke.is_empty() {
|
||||
add_row_effects(doc, symbol, art, &fill, &stroke);
|
||||
}
|
||||
@@ -526,8 +537,8 @@ fn with_id(art: &Node, id: NodeId) -> Node {
|
||||
/// a symbol instance as `symbol_art` (the symbol's art, stained as the caller draws it) placed by
|
||||
/// the instance's transform (through the symbol's 9-slice guides in `doc`, when it has them), as
|
||||
/// the art its members draw when the instance has geometry effects of its own
|
||||
/// ([`crate::group::members_as_drawn`]), its fills and strokes carrying the geometry effects of the
|
||||
/// instance's active fill and stroke ([`add_instance_row_effects`]).
|
||||
/// ([`crate::group::members_as_drawn`]), its fills and strokes carrying the geometry and raster
|
||||
/// effects of the instance's active fill and stroke ([`add_instance_row_effects`]).
|
||||
/// `None` for other kinds (images, paths, containers), and for an instance whose art placed
|
||||
/// through the guides could hold more than [`vectorcraft_doc::MAX_SYMBOL_NODES`] objects. A Repeat counts the Repeats in the art of
|
||||
/// `doc`'s symbols that its instances show toward its instance budget.
|
||||
@@ -709,12 +720,14 @@ pub(crate) fn outline_nested_text(doc: Option<&Document>, n: &mut Node, clipping
|
||||
/// reshaped with it, and keep their effects from their first Convert to Shape or Outline Object
|
||||
/// on. The result is a group with `n`'s id, name, visibility, transparency, opacity mask
|
||||
/// and remaining (raster) effects; `None` when `n` has no visible geometry effect, no perspective
|
||||
/// and is not type or an image with a fill or stroke of its own that [`has_item_geometry`] counts,
|
||||
/// or has no art (a symbol instance without `symbol_art`). A path with [`has_transform_art`] is
|
||||
/// always reshaped. The fills and strokes in the art whose own Transform effects reach their
|
||||
/// appearance become objects of their own ([`transform_items`]).
|
||||
/// and is not type or an image with a fill or stroke of its own that [`has_item_geometry`] counts
|
||||
/// or a symbol instance whose active fill or stroke has a raster effect
|
||||
/// ([`has_instance_row_raster`]), or has no art (a symbol instance without `symbol_art`). A path
|
||||
/// with [`has_transform_art`] is always reshaped. The fills and strokes in the art whose own
|
||||
/// Transform effects reach their appearance become objects of their own ([`transform_items`]).
|
||||
pub fn reshape(doc: Option<&Document>, n: &Node, symbol_art: Option<&Node>) -> Option<Node> {
|
||||
if !has_transform_art(n) && ((geometry_effects(n).is_empty() && n.projection().is_none() && !has_item_geometry(n)) || !needs_outline(n)) {
|
||||
let own = !geometry_effects(n).is_empty() || n.projection().is_some() || has_item_geometry(n) || has_instance_row_raster(n);
|
||||
if !has_transform_art(n) && (!own || !needs_outline(n)) {
|
||||
return None;
|
||||
}
|
||||
as_art(doc, n, symbol_art)
|
||||
|
||||
@@ -370,7 +370,9 @@ fn group_shadow(id: NodeId, fx: &Effect, shapes: &[Profile], b: Rect, options: R
|
||||
Ok(crate::solids::shadow::scoped_art(id, &caster, settings, cast, crate::solids::has_masks(shapes)))
|
||||
}
|
||||
|
||||
/// One object's surface of revolution about the axis of `b`, and each face's depth. `copies` counts
|
||||
/// One object's surface of revolution about the axis of `b`, and each face it draws ([`Drawn`]),
|
||||
/// back to front: by depth, and where two faces overlap, by their depths there
|
||||
/// ([`crate::solids::surface_order`]). `copies` counts
|
||||
/// the bytes of the paint copies of every surface and graphic of the object. Revolve (Classic) draws every face
|
||||
/// with its classic surface, as Extrude & Bevel (Classic) does. Revolve's graphics, with the
|
||||
/// symbols of `doc`, are drawn on the faces of the side ([`crate::solids::graphics::surface_parts`]):
|
||||
@@ -388,7 +390,7 @@ fn surface(
|
||||
blend: vectorcraft_doc::color::BlendMode,
|
||||
expand: bool,
|
||||
copies: &mut Copies,
|
||||
) -> Option<(Vec<f64>, Node)> {
|
||||
) -> Option<(Vec<Drawn>, Node)> {
|
||||
// A loaded file or a profile edited past the limits keeps drawing its source path instead
|
||||
// of crashing the frame. Commands validate the same evaluator and report its errors.
|
||||
if fx.id == CLASSIC_REVOLVE {
|
||||
@@ -397,9 +399,12 @@ fn surface(
|
||||
if copies.surfaces > MAX_PAINT_COPIES {
|
||||
return None;
|
||||
}
|
||||
let depths = facets.iter().map(|f| f.depth).collect();
|
||||
let order = crate::solids::surface_order(facets.iter().map(|f| (f.contours.first().map_or(&[][..], Vec::as_slice), f.plane, f.depth)));
|
||||
let facets = in_order(facets, &order);
|
||||
let drawn =
|
||||
facets.iter().map(|f| Drawn { depth: f.depth, points: f.contours.first().cloned().unwrap_or_default(), plane: f.plane }).collect();
|
||||
let p = merged_params(&fx.id, &fx.params);
|
||||
return Some((depths, crate::solids::classic::surface(n, facets, &paint, opacity, blend, &p)));
|
||||
return Some((drawn, crate::solids::classic::surface(n, facets, &paint, opacity, blend, &p)));
|
||||
}
|
||||
let colored = options.light_color != [1.0; 3] || options.lights.is_some();
|
||||
let faces = vectorcraft_three_d::revolve(path, b, options).ok()?;
|
||||
@@ -407,6 +412,8 @@ fn surface(
|
||||
if copies.surfaces > MAX_PAINT_COPIES {
|
||||
return None;
|
||||
}
|
||||
let order = crate::solids::surface_order(faces.iter().map(|f| (f.points.as_slice(), f.plane, f.depth)));
|
||||
let faces = in_order(faces, &order);
|
||||
let graphics = crate::solids::mapped_graphics(&merged_params(&fx.id, &fx.params));
|
||||
let shade = |face: &vectorcraft_three_d::Face, c: Color| graphic_paint(face, c, colored);
|
||||
// Graphics past the limits are left out, and the surface draws without them and their paints'
|
||||
@@ -437,21 +444,28 @@ fn surface(
|
||||
&& n.has_default_transparency())
|
||||
.then(|| vectorcraft_three_d::visible_faces(&faces).ok())
|
||||
.flatten();
|
||||
let faces = visible.unwrap_or_else(|| {
|
||||
faces
|
||||
.into_iter()
|
||||
.map(|f| vectorcraft_three_d::VisibleFace {
|
||||
path: f.path(),
|
||||
brightness: f.brightness,
|
||||
depth: f.depth,
|
||||
gradient: f.gradient,
|
||||
illumination: f.illumination,
|
||||
light_gradient: f.light_gradient,
|
||||
rgb_gradient: f.rgb_gradient,
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
let depths: Vec<f64> = faces.iter().map(|f: &vectorcraft_three_d::VisibleFace| f.depth).collect();
|
||||
let (faces, keys): (Vec<vectorcraft_three_d::VisibleFace>, Vec<Drawn>) = match visible {
|
||||
Some(visible) => {
|
||||
let keys = visible.iter().map(|f| Drawn { depth: f.depth, points: vec![], plane: None }).collect();
|
||||
(visible, keys)
|
||||
}
|
||||
None => {
|
||||
let keys = faces.iter().map(|f| Drawn { depth: f.depth, points: f.points.clone(), plane: f.plane }).collect();
|
||||
let faces = faces
|
||||
.into_iter()
|
||||
.map(|f| vectorcraft_three_d::VisibleFace {
|
||||
path: f.path(),
|
||||
brightness: f.brightness,
|
||||
depth: f.depth,
|
||||
gradient: f.gradient,
|
||||
illumination: f.illumination,
|
||||
light_gradient: f.light_gradient,
|
||||
rgb_gradient: f.rgb_gradient,
|
||||
})
|
||||
.collect();
|
||||
(faces, keys)
|
||||
}
|
||||
};
|
||||
let children = faces
|
||||
.into_iter()
|
||||
.map(|face| {
|
||||
@@ -517,7 +531,21 @@ fn surface(
|
||||
let mut group = Node::group(n.id, children);
|
||||
group.opacity = opacity;
|
||||
group.blend = blend;
|
||||
Some((depths, group))
|
||||
Some((keys, group))
|
||||
}
|
||||
|
||||
/// A face [`surface`] draws: its depth and, for [`crate::solids::surface_order`], its polygon on the
|
||||
/// drawing and its plane (none for a face trimmed to the part of it that shows).
|
||||
struct Drawn {
|
||||
depth: f64,
|
||||
points: Vec<Point>,
|
||||
plane: Option<vectorcraft_three_d::DepthPlane>,
|
||||
}
|
||||
|
||||
/// `items` in `order`, a permutation of their indices.
|
||||
fn in_order<T>(items: Vec<T>, order: &[usize]) -> Vec<T> {
|
||||
let mut slots: Vec<Option<T>> = items.into_iter().map(Some).collect();
|
||||
order.iter().filter_map(|&k| slots.get_mut(k).and_then(Option::take)).collect()
|
||||
}
|
||||
|
||||
/// A surface of one object about the bounds of `path`, built with options derived from `fx`.
|
||||
@@ -934,14 +962,17 @@ fn group_art(doc: Option<&Document>, n: &Node, expand: bool) -> Option<Node> {
|
||||
} else {
|
||||
let count = surfaces.len();
|
||||
let mut faces = vec![];
|
||||
for (member, (depths, s, scopes)) in surfaces.into_iter().enumerate() {
|
||||
for (member, (drawn, s, scopes)) in surfaces.into_iter().enumerate() {
|
||||
let (opacity, blend) = (s.opacity, s.blend);
|
||||
if let NodeKind::Group { children, .. } = s.kind {
|
||||
faces.extend(depths.into_iter().zip(children).map(|(depth, face)| (depth, member, opacity, blend, scopes, face)));
|
||||
faces.extend(drawn.into_iter().zip(children).map(|(drawn, face)| (drawn, member, opacity, blend, scopes, face)));
|
||||
}
|
||||
}
|
||||
// A stable sort: faces at the same depth keep the members' order.
|
||||
faces.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
// A stable sort: faces at the same depth keep the members' order. Overlapping faces, of one
|
||||
// member or of two, then go in the order of their depths where they overlap.
|
||||
faces.sort_by(|a, b| a.0.depth.total_cmp(&b.0.depth));
|
||||
let order = crate::solids::surface_order(faces.iter().map(|(d, ..)| (d.points.as_slice(), d.plane, d.depth)));
|
||||
let faces = in_order(faces, &order);
|
||||
if opaque {
|
||||
vec![Arc::new(Node::group(n.id, faces.into_iter().map(|(.., face)| face).collect()))]
|
||||
} else {
|
||||
|
||||
@@ -7,7 +7,7 @@ use vectorcraft_doc::color::{BlendMode, Paint};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Document, Effect, Node, NodeId, NodeKind, OpacityMask};
|
||||
use vectorcraft_geom::{BezPath, FillRule, PathData, Point, Rect, Vec2};
|
||||
use vectorcraft_pathops::BoolOp;
|
||||
use vectorcraft_three_d::{Bevel, BevelShape, DepthPlane, Extrude, Facet, Inflate, MAX_OBJECT_FACES, Mesh, PlanarFace, View};
|
||||
use vectorcraft_three_d::{Bevel, BevelShape, DepthPlane, Extrude, Facet, Inflate, MAX_OBJECT_FACES, Mesh, MeshedSolid, PlanarFace, View};
|
||||
|
||||
#[cfg(test)]
|
||||
use vectorcraft_doc::color::Color;
|
||||
@@ -656,7 +656,7 @@ fn solid_parts(shapes: &[Profile], fallback: Rect) -> (Vec<Part<'_>>, Rect) {
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many solids [`built_solids`] has built on this thread, for the tests.
|
||||
/// How many solids [`built_solids`] and [`meshes_for`] have built on this thread, for the tests.
|
||||
static SOLIDS_BUILT: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
@@ -1261,6 +1261,35 @@ fn draw_order(faces: &[Placed], sealed: bool) -> Vec<usize> {
|
||||
local_order(faces, order)
|
||||
}
|
||||
|
||||
/// The order, back to front, of the faces of one or more Revolve surfaces `faces`, given back to
|
||||
/// front by depth: each face's polygon on the drawing, its plane and its depth. When two
|
||||
/// overlapping faces are drawn the wrong way round for their depths where they overlap, every pair
|
||||
/// of overlapping faces with a convex polygon and a plane goes in the order of its depths there,
|
||||
/// the face nearer the viewer after the other ([`local_order`]), and each step draws the face that
|
||||
/// comes first in the given order of those whose faces before are drawn, so a face without a plane
|
||||
/// or a convex polygon can also move. Otherwise the order given.
|
||||
pub(crate) fn surface_order<'a>(faces: impl IntoIterator<Item = (&'a [Point], Option<DepthPlane>, f64)>) -> Vec<usize> {
|
||||
let placed: Vec<Placed> = faces
|
||||
.into_iter()
|
||||
.map(|(points, plane, depth)| {
|
||||
let local = plane.is_some() && convex(points).is_some();
|
||||
// Every face counts as one of a stroke's on one shape, so each overlapping pair is compared.
|
||||
Placed {
|
||||
depth,
|
||||
side: true,
|
||||
away: false,
|
||||
bounds: polygon_bounds(points),
|
||||
member: 0,
|
||||
paints: Paints::Other,
|
||||
outline: if local { points.to_vec() } else { vec![] },
|
||||
plane: plane.filter(|_| local),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let order = (0..placed.len()).collect();
|
||||
local_order(&placed, order)
|
||||
}
|
||||
|
||||
/// `shapes`, the members of group or layer `n`, reshaped by the visible geometry effects `before`
|
||||
/// its 3D effect, about the bounds of all of them.
|
||||
pub(crate) fn reshape_members(n: &Node, before: &[Effect], shapes: &mut [Profile]) {
|
||||
@@ -2039,31 +2068,35 @@ fn meshes_for(fx: &Effect, shapes: &[Profile], total: &mut usize, limit: usize)
|
||||
return Ok(out);
|
||||
}
|
||||
let (parts, bounds) = solid_parts(shapes, b);
|
||||
// The solids the art draws, past the limits of the object's solids together as it is
|
||||
// ([`built_solids`]), each built and its faces counted as the loop reaches it, then dropped.
|
||||
let drawn = built_solids(&fx.id, &p, &parts, bounds, MAX_OBJECT_FACES).map(|r| r.map(|f| f.is_some()));
|
||||
// Inflate lays out every puff at once (they share one eye); each Inflate and Extrude & Bevel
|
||||
// solid and its mesh are built as the loop reaches them.
|
||||
// Inflate lays out every puff at once (they share one eye). Each Inflate and Extrude & Bevel
|
||||
// solid is built once, as the loop reaches it: its facets decide whether the art draws it, as
|
||||
// [`built_solids`] does, and its faces before projection make its mesh.
|
||||
let mut inflated = (fx.id == INFLATE).then(|| {
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule, _, _)| (area, *rule)).collect();
|
||||
vectorcraft_three_d::inflate_meshes(&shapes, bounds, inflate_options(&p), view)
|
||||
vectorcraft_three_d::inflate_meshed(&shapes, bounds, inflate_options(&p), view)
|
||||
});
|
||||
let extrude = if fx.id == CLASSIC_EXTRUDE { classic_extrude_options(&p) } else { extrude_options(&p) };
|
||||
for ((item, member, area, rule, _, _), drawn) in parts.iter().zip(drawn) {
|
||||
let mut kept = 0usize;
|
||||
for (item, member, area, rule, _, _) in &parts {
|
||||
let item: &AppearanceItem = item;
|
||||
if !drawn? {
|
||||
// A stroke the art leaves out is left out of the meshes too; Inflate's next puff
|
||||
// still belongs to the next part.
|
||||
if let Some(built) = inflated.as_mut() {
|
||||
built.next();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let mesh = match inflated.as_mut() {
|
||||
Some(built) => built.next().unwrap_or_else(|| Ok(Mesh::default())),
|
||||
None => vectorcraft_three_d::extrude_mesh(area, *rule, bounds, extrude, view),
|
||||
#[cfg(test)]
|
||||
SOLIDS_BUILT.with(|n| n.set(n.get() + 1));
|
||||
let built = match inflated.as_mut() {
|
||||
Some(each) => each.next().unwrap_or_else(|| Ok(MeshedSolid::default())),
|
||||
None => vectorcraft_three_d::extrude_meshed(area, *rule, bounds, extrude, view),
|
||||
};
|
||||
match mesh {
|
||||
// A stroke the art leaves out is left out of the meshes too. The faces of the solids kept
|
||||
// count together against the object's limit.
|
||||
let solid = match built {
|
||||
Ok(solid) => solid,
|
||||
Err(_) if matches!(item, AppearanceItem::Stroke(_)) => continue,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
kept = kept.saturating_add(solid.facets.len());
|
||||
if kept > MAX_OBJECT_FACES {
|
||||
return Err(vectorcraft_three_d::TOO_COMPLEX_SOLID.to_string());
|
||||
}
|
||||
match solid.mesh() {
|
||||
Ok(mesh) => {
|
||||
let (paint, opacity, _) = paint_of(item, *member);
|
||||
keep(&mut out, mesh, mesh_color(&paint, opacity))?;
|
||||
@@ -2174,3 +2207,6 @@ mod tests_sources;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_shadow;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_revolve_order;
|
||||
@@ -208,3 +208,151 @@ fn a_gradient_takes_the_average_of_its_stops() {
|
||||
let pattern = Paint::Pattern { pattern: "x".into(), xf: vectorcraft_geom::Affine::IDENTITY };
|
||||
assert!(close(mesh_color(&pattern, 0.5), [0.5, 0.5, 0.5, 0.5]));
|
||||
}
|
||||
|
||||
/// The meshes of Extrude & Bevel or Inflate `fx` on `shapes`, each built apart from the facets that
|
||||
/// decide which solids are drawn ([`built_solids`], then [`vectorcraft_three_d::extrude_mesh`] or
|
||||
/// [`vectorcraft_three_d::inflate_meshes`]), for comparing with [`meshes_for`].
|
||||
fn meshes_built_apart(fx: &Effect, shapes: &[Profile], total: &mut usize, limit: usize) -> Result<Vec<SolidMesh>, String> {
|
||||
let p = crate::merged_params(&fx.id, &fx.params);
|
||||
let view = view_options(&p);
|
||||
let (parts, bounds) = solid_parts(shapes, union_bounds(shapes).unwrap());
|
||||
let drawn = built_solids(&fx.id, &p, &parts, bounds, MAX_OBJECT_FACES).map(|r| r.map(|f| f.is_some()));
|
||||
let mut inflated = (fx.id == INFLATE).then(|| {
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule, _, _)| (area, *rule)).collect();
|
||||
vectorcraft_three_d::inflate_meshes(&shapes, bounds, inflate_options(&p), view)
|
||||
});
|
||||
let extrude = if fx.id == CLASSIC_EXTRUDE { classic_extrude_options(&p) } else { extrude_options(&p) };
|
||||
let mut out = vec![];
|
||||
for ((item, opacity, area, rule, _, _), drawn) in parts.iter().zip(drawn) {
|
||||
let item: &AppearanceItem = item;
|
||||
if !drawn? {
|
||||
if let Some(built) = inflated.as_mut() {
|
||||
built.next();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let mesh = match inflated.as_mut() {
|
||||
Some(built) => built.next().unwrap_or_else(|| Ok(Mesh::default())),
|
||||
None => vectorcraft_three_d::extrude_mesh(area, *rule, bounds, extrude, view),
|
||||
};
|
||||
match mesh {
|
||||
Ok(mesh) => {
|
||||
let (paint, opacity, _) = paint_of(item, *opacity);
|
||||
keep_mesh(&mut out, total, SolidMesh { mesh, color: mesh_color(&paint, opacity) }, limit)?;
|
||||
}
|
||||
Err(_) if matches!(item, AppearanceItem::Stroke(_)) => {}
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The bits of every number in `meshes`, for comparing two exports exactly.
|
||||
type MeshBits = Vec<([u32; 4], Vec<[u64; 3]>, Vec<[u64; 3]>, Vec<[u32; 3]>)>;
|
||||
|
||||
fn mesh_bits(meshes: &[SolidMesh]) -> MeshBits {
|
||||
meshes
|
||||
.iter()
|
||||
.map(|m| {
|
||||
let (positions, normals) = (&m.mesh.positions, &m.mesh.normals);
|
||||
(
|
||||
m.color.map(f32::to_bits),
|
||||
positions.iter().map(|p| p.map(f64::to_bits)).collect(),
|
||||
normals.iter().map(|n| n.map(f64::to_bits)).collect(),
|
||||
m.mesh.triangles.clone(),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A five-pointed star about `(x, 150)` with curved edges, filled red and stroked black.
|
||||
fn star_node(id: u64, x: f64) -> Node {
|
||||
let mut b = BezPath::new();
|
||||
for k in 0..10 {
|
||||
let (s, c) = (k as f64 * std::f64::consts::PI / 5.0).sin_cos();
|
||||
let r = if k % 2 == 0 { 60.0 } else { 25.0 };
|
||||
let p = (x + r * c, 150.0 + r * s);
|
||||
if k == 0 { b.move_to(p) } else { b.curve_to((p.0 + 4.0, p.1), (p.0, p.1 + 4.0), p) }
|
||||
}
|
||||
b.close_path();
|
||||
Node::path(NodeId(id), PathData::from_bezpath(&b), Appearance::basic(Paint::solid(RED), Paint::solid(Color::BLACK), 6.0))
|
||||
}
|
||||
|
||||
/// The shapes `meshes_for` builds from: one node's, or each of several.
|
||||
fn shapes_of(nodes: &[&Node]) -> Vec<Profile> {
|
||||
nodes
|
||||
.iter()
|
||||
.map(|n| Profile { path: profile(n).unwrap().0, rule: FillRule::NonZero, items: solid_paints(n), opacity: 1.0, scopes: vec![] })
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Building each Extrude & Bevel and Inflate solid once exports the same meshes, to the bit, as
|
||||
/// building its facets and its mesh apart, within and past the triangle budget.
|
||||
#[test]
|
||||
fn one_build_exports_the_meshes_built_apart() {
|
||||
let mut second_stroke = square_node(3, 400.0);
|
||||
if let Some(stroke) = second_stroke.appearance.items.get(1).cloned() {
|
||||
second_stroke.appearance.items.push(stroke);
|
||||
}
|
||||
let (star, square, other) = (star_node(1, 100.0), square_node(2, 250.0), star_node(4, 600.0));
|
||||
let cases = [
|
||||
(EXTRUDE, json!({})),
|
||||
(EXTRUDE, json!({ "bevel": "rounded", "bevelWidth": 6.0, "bevelHeight": 5.0, "twist": 60.0, "taper": 70.0 })),
|
||||
(EXTRUDE, json!({ "bevel": "cove", "bevelInside": true, "bevelBothSides": true, "cap": false, "perspectiveAngle": 50.0 })),
|
||||
(EXTRUDE, json!({ "depth": 0.0 })),
|
||||
(CLASSIC_EXTRUDE, json!({ "bevel": "classic", "bevelHeight": 8.0 })),
|
||||
(INFLATE, json!({})),
|
||||
(INFLATE, json!({ "depth": 30.0, "bothSides": true, "volume": 80.0 })),
|
||||
(INFLATE, json!({ "depth": 40.0, "twist": 360.0, "taper": 70.0, "cap": false })),
|
||||
(INFLATE, json!({ "depth": 0.1, "twist": 0.1, "bothSides": true })),
|
||||
];
|
||||
let sets: [Vec<&Node>; 3] = [vec![&star], vec![&second_stroke], vec![&star, &square, &other]];
|
||||
for (id, params) in cases {
|
||||
let effect = fx(id, params.clone());
|
||||
for nodes in &sets {
|
||||
let shapes = shapes_of(nodes);
|
||||
let (mut once, mut apart) = (0, 0);
|
||||
let built = meshes_for(&effect, &shapes, &mut once, MAX_MESH_TRIANGLES).unwrap_or_else(|e| panic!("{id} {params}: {e}"));
|
||||
let reference = meshes_built_apart(&effect, &shapes, &mut apart, MAX_MESH_TRIANGLES).unwrap();
|
||||
assert!(!reference.is_empty());
|
||||
assert_eq!(mesh_bits(&built), mesh_bits(&reference), "{id} {params}");
|
||||
assert_eq!(once, apart);
|
||||
// Past the budget, at the same mesh, with the same count.
|
||||
let limit = once - 1;
|
||||
let (mut once, mut apart) = (0, 0);
|
||||
let built = meshes_for(&effect, &shapes, &mut once, limit);
|
||||
assert!(built.is_err(), "{id} {params}");
|
||||
assert_eq!(built.err(), meshes_built_apart(&effect, &shapes, &mut apart, limit).err(), "{id} {params}");
|
||||
assert_eq!(once, apart, "{id} {params}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A stroke whose solid is past the limits is left out of the export and the fill's mesh exports,
|
||||
/// as when the facets and the mesh are built apart.
|
||||
#[test]
|
||||
fn a_stroke_past_the_limits_is_left_out_of_the_export() {
|
||||
// 40,000 corners: within the 65,536 points of the fill's solid, but not the stroke's outline.
|
||||
let points: Vec<Point> = (0..40_000)
|
||||
.map(|k| {
|
||||
let (s, c) = (k as f64 * std::f64::consts::TAU / 40_000.0).sin_cos();
|
||||
let r = if k % 2 == 0 { 10_000.0 } else { 9_999.0 };
|
||||
Point::new(20_000.0 + r * c, 20_000.0 + r * s)
|
||||
})
|
||||
.collect();
|
||||
let path = PathData::single(vectorcraft_geom::SubPath::polyline(&points, true));
|
||||
let n = Node::path(NodeId(1), path, Appearance::basic(Paint::solid(RED), Paint::solid(Color::BLACK), 2.0));
|
||||
for id in [EXTRUDE, INFLATE] {
|
||||
// Without caps: a cap of 40,000 corners takes more work to cut into triangles than an
|
||||
// export allows.
|
||||
let effect = fx(id, json!({ "cap": false }));
|
||||
let shapes = shapes_of(&[&n]);
|
||||
// The fill's mesh alone has more triangles than an export takes.
|
||||
let (mut once, mut apart) = (0, 0);
|
||||
let built = meshes_for(&effect, &shapes, &mut once, usize::MAX).unwrap_or_else(|e| panic!("{id}: {e}"));
|
||||
assert_eq!(built.len(), 1, "{id}: the fill's mesh alone");
|
||||
assert!(close(built[0].color, [0.8, 0.2, 0.1, 1.0]), "{id}");
|
||||
let reference = meshes_built_apart(&effect, &shapes, &mut apart, usize::MAX).unwrap();
|
||||
assert_eq!(mesh_bits(&built), mesh_bits(&reference), "{id}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
use crate::revolve::{CLASSIC_REVOLVE, REVOLVE, revolve_art, revolve_options};
|
||||
|
||||
/// The first pair of faces `(polygon, plane)`, in the order given, drawn the wrong way round where
|
||||
/// their polygons overlap: the earlier one nearer the viewer at the middle of the overlap.
|
||||
fn misordered(faces: &[(Vec<Point>, Option<DepthPlane>)]) -> Option<(usize, usize)> {
|
||||
for (i, (a, pa)) in faces.iter().enumerate() {
|
||||
let Some(pa) = pa.filter(|_| convex(a).is_some()) else { continue };
|
||||
for (j, (b, pb)) in faces.iter().enumerate().skip(i + 1) {
|
||||
let Some(pb) = pb else { continue };
|
||||
let Some(turn) = convex(b) else { continue };
|
||||
if !rects_overlap(polygon_bounds(a), polygon_bounds(b)) {
|
||||
continue;
|
||||
}
|
||||
let Some(mid) = centroid(&clip_convex(a, b, turn)) else { continue };
|
||||
let (Some(da), Some(db)) = (pa.depth_at(mid), pb.depth_at(mid)) else { continue };
|
||||
if da - db > 1e-6 * (1.0 + da.abs().max(db.abs())) {
|
||||
return Some((i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// An open, vase-like profile of straight edges.
|
||||
fn vase() -> PathData {
|
||||
let mut b = BezPath::new();
|
||||
b.move_to((30.0, 20.0));
|
||||
for p in [(90.0, 20.0), (55.0, 65.0), (95.0, 110.0), (30.0, 145.0)] {
|
||||
b.line_to(p);
|
||||
}
|
||||
PathData::from_bezpath(&b)
|
||||
}
|
||||
|
||||
/// The `k`th of five closed 14 × 20 pt rectangles stepping down and away from the axis, as the
|
||||
/// dashes of a dashed line's outline do.
|
||||
fn dash(k: usize) -> PathData {
|
||||
let (x, y) = (30.0 + 12.0 * k as f64, 20.0 + 26.0 * k as f64);
|
||||
let mut b = BezPath::new();
|
||||
b.move_to((x, y));
|
||||
b.line_to((x + 14.0, y));
|
||||
b.line_to((x + 14.0, y + 20.0));
|
||||
b.line_to((x, y + 20.0));
|
||||
b.close_path();
|
||||
PathData::from_bezpath(&b)
|
||||
}
|
||||
|
||||
/// The five [`dash`]es as one compound path.
|
||||
fn dashes() -> PathData {
|
||||
PathData::new((0..5).flat_map(|k| dash(k).subpaths).collect())
|
||||
}
|
||||
|
||||
/// A tilted three-quarter turn of [`vase`], whose faces sorted by their middles' depths draw a
|
||||
/// nearer face under a farther one where the two overlap.
|
||||
fn params() -> Value {
|
||||
json!({"angle": 270, "offset": 12, "rotationX": -30, "rotationY": 0, "perspectiveAngle": 0, "segments": 24, "cap": true})
|
||||
}
|
||||
|
||||
/// A three-quarter turn of [`dashes`], steeply tilted and in wide perspective, whose classic facets
|
||||
/// sorted by their middles' depths draw a nearer facet under a farther one where the two overlap.
|
||||
fn classic_params() -> Value {
|
||||
json!({"angle": 270, "offset": 12, "rotationX": -60, "rotationY": 0, "perspectiveAngle": 80, "cap": true})
|
||||
}
|
||||
|
||||
fn node(path: PathData) -> Node {
|
||||
Node::path(NodeId(1), path, Appearance::basic(Paint::solid(Color::rgb(0.1, 0.7, 0.8)), Paint::None, 0.0))
|
||||
}
|
||||
|
||||
/// The faces of `faces` (polygon, plane and outline) in the order `art` draws their outlines.
|
||||
fn drawn_order(art: &Node, faces: &[(Vec<Point>, Option<DepthPlane>, PathData)]) -> Vec<(Vec<Point>, Option<DepthPlane>)> {
|
||||
let mut out = vec![];
|
||||
art.walk(&mut |n| {
|
||||
if let Some(path) = n.path_data()
|
||||
&& let Some((points, plane, _)) = faces.iter().find(|(.., outline)| outline == path)
|
||||
{
|
||||
out.push((points.clone(), *plane));
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// Of a tilted, revolved vase, a face sorted by the depth of its middle goes under a farther face
|
||||
/// that it is in front of where the two overlap. The surface draws the nearer face last there, on
|
||||
/// its own and as a member of a revolved group.
|
||||
#[test]
|
||||
fn a_revolved_surface_draws_the_nearer_of_two_overlapping_faces_last() {
|
||||
let path = vase();
|
||||
let faces = vectorcraft_three_d::revolve(&path, path.bounds().unwrap(), revolve_options(¶ms())).unwrap();
|
||||
let by_depth: Vec<_> = faces.iter().map(|f| (f.points.clone(), f.plane)).collect();
|
||||
assert!(misordered(&by_depth).is_some(), "the faces by depth are drawn the wrong way round somewhere");
|
||||
let keyed: Vec<_> = faces.iter().map(|f| (f.points.clone(), f.plane, f.path())).collect();
|
||||
let mut n = node(path.clone());
|
||||
n.appearance.effects.push(crate::new_effect(REVOLVE, ¶ms()).unwrap());
|
||||
let drawn = drawn_order(&revolve_art(&n).unwrap(), &keyed);
|
||||
assert_eq!(drawn.len(), faces.len());
|
||||
assert_eq!(misordered(&drawn), None, "an object");
|
||||
let mut g = Node::group(NodeId(9), vec![Arc::new(node(path))]);
|
||||
g.appearance.effects.push(crate::new_effect(REVOLVE, ¶ms()).unwrap());
|
||||
let drawn = drawn_order(&revolve_art(&g).unwrap(), &keyed);
|
||||
assert_eq!(drawn.len(), faces.len());
|
||||
assert_eq!(misordered(&drawn), None, "a group's member");
|
||||
}
|
||||
|
||||
/// The faces of a group's members, sorted together by the depths of their middles, go in the order
|
||||
/// of their depths where they overlap, those of two members as well as those of one.
|
||||
#[test]
|
||||
fn a_revolved_groups_members_draw_the_nearer_of_two_overlapping_faces_last() {
|
||||
let p = json!({"angle": 270, "offset": 12, "rotationX": -30, "rotationY": 0, "perspectiveAngle": 0, "segments": 16, "cap": true});
|
||||
let members: Vec<PathData> = (0..5).map(dash).collect();
|
||||
let b = dashes().bounds().unwrap();
|
||||
let mut faces: Vec<_> = members.iter().flat_map(|m| vectorcraft_three_d::revolve(m, b, revolve_options(&p)).unwrap()).collect();
|
||||
faces.sort_by(|a, b| a.depth.total_cmp(&b.depth));
|
||||
let by_depth: Vec<_> = faces.iter().map(|f| (f.points.clone(), f.plane)).collect();
|
||||
assert!(misordered(&by_depth).is_some(), "the faces by depth are drawn the wrong way round somewhere");
|
||||
let keyed: Vec<_> = faces.iter().map(|f| (f.points.clone(), f.plane, f.path())).collect();
|
||||
let mut g = Node::group(NodeId(9), members.into_iter().map(|m| Arc::new(node(m))).collect());
|
||||
g.appearance.effects.push(crate::new_effect(REVOLVE, &p).unwrap());
|
||||
let drawn = drawn_order(&revolve_art(&g).unwrap(), &keyed);
|
||||
assert_eq!(drawn.len(), faces.len());
|
||||
assert_eq!(misordered(&drawn), None);
|
||||
}
|
||||
|
||||
/// Revolve (Classic) draws the nearer of two overlapping facets last where they overlap, too.
|
||||
#[test]
|
||||
fn a_classic_revolved_surface_draws_the_nearer_of_two_overlapping_facets_last() {
|
||||
let path = dashes();
|
||||
// Revolve (Classic) sweeps its surface unshaded, with one facet for each cell.
|
||||
let classic = vectorcraft_three_d::Revolve {
|
||||
shade: false,
|
||||
smooth_shading: false,
|
||||
lights: None,
|
||||
light_color: [1.0; 3],
|
||||
..revolve_options(&crate::merged_params(CLASSIC_REVOLVE, &classic_params()))
|
||||
};
|
||||
let facets = vectorcraft_three_d::revolve_facets(&path, path.bounds().unwrap(), classic).unwrap();
|
||||
let by_depth: Vec<_> = facets.iter().map(|f| (f.contours[0].clone(), f.plane)).collect();
|
||||
assert!(misordered(&by_depth).is_some(), "the facets by depth are drawn the wrong way round somewhere");
|
||||
let keyed: Vec<_> = facets.iter().map(|f| (f.contours[0].clone(), f.plane, f.path())).collect();
|
||||
let mut n = node(path);
|
||||
n.appearance.effects.push(crate::new_effect(CLASSIC_REVOLVE, &classic_params()).unwrap());
|
||||
let drawn = drawn_order(&revolve_art(&n).unwrap(), &keyed);
|
||||
assert_eq!(drawn.len(), facets.len());
|
||||
assert_eq!(misordered(&drawn), None);
|
||||
}
|
||||
|
||||
/// A surface whose faces by depth are already drawn the right way round wherever they overlap keeps
|
||||
/// that order.
|
||||
#[test]
|
||||
fn a_surface_already_in_order_keeps_it() {
|
||||
let path = vase();
|
||||
let p = json!({"angle": 360, "offset": 12, "rotationX": 0, "rotationY": 0, "segments": 24, "cap": true});
|
||||
let faces = vectorcraft_three_d::revolve(&path, path.bounds().unwrap(), revolve_options(&p)).unwrap();
|
||||
let by_depth: Vec<_> = faces.iter().map(|f| (f.points.clone(), f.plane)).collect();
|
||||
assert_eq!(misordered(&by_depth), None);
|
||||
let order = surface_order(faces.iter().map(|f| (f.points.as_slice(), f.plane, f.depth)));
|
||||
assert_eq!(order, (0..faces.len()).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
/// Of two overlapping faces, the one nearer the viewer where they overlap goes after the other,
|
||||
/// whatever their depths; a face without a plane, and one that overlaps neither, keep their places.
|
||||
/// Two faces at the same depth there keep their order.
|
||||
#[test]
|
||||
fn surface_order_puts_the_nearer_face_where_they_overlap_last() {
|
||||
let square = |x: f64| vec![Point::new(x, 0.0), Point::new(x + 10.0, 0.0), Point::new(x + 10.0, 10.0), Point::new(x, 10.0)];
|
||||
// z = 10 - x: at the middle of its overlap with the next square (x 7.5) it is at z 2.5.
|
||||
let r = std::f64::consts::FRAC_1_SQRT_2;
|
||||
let sloped = Some(DepthPlane { normal: [r, 0.0, r], offset: 10.0 * r, center: Point::ZERO, eye: None });
|
||||
let flat = |z: f64| Some(DepthPlane { normal: [0.0, 0.0, 1.0], offset: z, center: Point::ZERO, eye: None });
|
||||
let (a, b, apart) = (square(0.0), square(5.0), square(40.0));
|
||||
let faces = [(&a[..], sloped, 5.0), (&apart[..], flat(0.0), 1.0), (&b[..], flat(3.0), 3.0)];
|
||||
assert_eq!(surface_order(faces), vec![0, 1, 2], "the flat face, nearer at the overlap, is already last");
|
||||
let faces = [(&b[..], flat(2.0), 2.0), (&apart[..], flat(0.0), 3.0), (&a[..], sloped, 5.0)];
|
||||
assert_eq!(surface_order(faces), vec![0, 1, 2], "the sloped face, nearer at the overlap, is already last");
|
||||
// The sloped face's middle (x 5) is at z 5, but the flat face at z 3 is nearer at the overlap.
|
||||
let faces = [(&b[..], flat(3.0), 3.0), (&apart[..], flat(0.0), 4.0), (&a[..], sloped, 5.0)];
|
||||
assert_eq!(surface_order(faces), vec![1, 2, 0], "the flat face is nearer at the overlap");
|
||||
let faces = [(&b[..], None, 3.0), (&apart[..], flat(0.0), 4.0), (&a[..], sloped, 5.0)];
|
||||
assert_eq!(surface_order(faces), vec![0, 1, 2], "no plane");
|
||||
let faces = [(&b[..], flat(2.5), 3.0), (&a[..], sloped, 5.0)];
|
||||
assert_eq!(surface_order(faces), vec![0, 1], "the same depth there");
|
||||
}
|
||||
@@ -559,6 +559,15 @@ pub(crate) fn given_object_ids(p: &Value, command: &str) -> Result<Option<Vec<No
|
||||
pub(crate) fn index(v: u64) -> usize {
|
||||
usize::try_from(v).unwrap_or(usize::MAX)
|
||||
}
|
||||
/// The whole number of 0 or more that `p` gives for `key`, read with [`index`], or `None` when `p`
|
||||
/// leaves it out or gives null. Any other value, such as `"3"`, `2.5`, `-1` or `true`, is an error
|
||||
/// under `cmd`: "`{key}` must be a whole number".
|
||||
pub(crate) fn index_opt(p: &Value, key: &str, cmd: &str) -> Result<Option<usize>> {
|
||||
match p.get(key) {
|
||||
None | Some(Value::Null) => Ok(None),
|
||||
Some(v) => v.as_u64().map(|n| Some(index(n))).ok_or_else(|| bad(cmd, format!("`{key}` must be a whole number"))),
|
||||
}
|
||||
}
|
||||
/// Validate all explicit targets before a command changes the document or selection.
|
||||
pub(crate) fn checked_ids_param(s: &Session, p: &Value, key: &str, command: &str) -> Result<Vec<NodeId>> {
|
||||
checked_nodes_param(s, p, key, command, false)
|
||||
|
||||
@@ -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, .. }));
|
||||
}
|
||||
@@ -127,6 +127,17 @@ fn node_refs(n: &Node, f: Visit) {
|
||||
if let Some(cm) = Document::color_mask_of(m) {
|
||||
f(Ref::Name(Res::Image, &cm.key));
|
||||
}
|
||||
// A graph keeps paints and style names for its series and labels, which a redraw gives them.
|
||||
if let Some(g) = m.extras().graph.as_deref() {
|
||||
vectorcraft_doc::swatches::graph_paints(m).for_each(|p| paint_refs(p, f));
|
||||
let l = &g.label_styles;
|
||||
for s in [&l.values, &l.categories, &l.legend].into_iter().flatten().filter_map(|st| st.style_name.as_ref()) {
|
||||
f(Ref::Name(Res::CharStyle, s));
|
||||
}
|
||||
for s in [&l.values_para, &l.categories_para, &l.legend_para].into_iter().flatten().filter_map(|pa| pa.style_name.as_ref()) {
|
||||
f(Ref::Name(Res::ParaStyle, s));
|
||||
}
|
||||
}
|
||||
match &m.kind {
|
||||
NodeKind::Text(t) => {
|
||||
for r in &t.runs {
|
||||
@@ -671,6 +682,27 @@ impl Relink {
|
||||
self.name(Res::Image, &mut cm.key);
|
||||
self.name(Res::Image, &mut cm.from);
|
||||
}
|
||||
if let Some(g) = n.extras_if_any().and_then(|e| e.graph.as_deref_mut()) {
|
||||
for s in &mut g.series_paints {
|
||||
s.fill.iter_mut().chain(s.stroke.iter_mut()).for_each(|p| self.paint(p));
|
||||
}
|
||||
let l = &mut g.label_styles;
|
||||
for st in [&mut l.values, &mut l.categories, &mut l.legend].into_iter().flatten() {
|
||||
self.paint(&mut st.fill);
|
||||
self.paint(&mut st.stroke);
|
||||
if let Some(s) = &mut st.style_name {
|
||||
self.name(Res::CharStyle, s);
|
||||
}
|
||||
}
|
||||
for pa in [&mut l.values_para, &mut l.categories_para, &mut l.legend_para].into_iter().flatten() {
|
||||
if let Some(s) = &mut pa.style_name {
|
||||
self.name(Res::ParaStyle, s);
|
||||
}
|
||||
}
|
||||
for r in [&mut l.values_rows, &mut l.categories_rows, &mut l.legend_rows].into_iter().flatten() {
|
||||
r.paint.fill.iter_mut().chain(r.paint.stroke.iter_mut()).for_each(|p| self.paint(p));
|
||||
}
|
||||
}
|
||||
match &mut n.kind {
|
||||
NodeKind::Text(t) => {
|
||||
for r in &mut t.runs {
|
||||
|
||||
@@ -568,12 +568,11 @@ fn switch_units(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
Ok(json!({ "units": next.label() }))
|
||||
}
|
||||
|
||||
/// The `artboard` param of the ruler origin commands: an artboard index, 0 when not given.
|
||||
/// The `artboard` param of the ruler origin commands: an artboard index, 0 when left out or null. A
|
||||
/// value that is not a whole number is "`artboard` must be a whole number"; one past the artboards
|
||||
/// is "index out of range".
|
||||
fn artboard_param(s: &Session, p: &Value, c: &str) -> Result<usize> {
|
||||
let index = match p.get("artboard") {
|
||||
None => 0,
|
||||
Some(v) => v.as_u64().and_then(|n| usize::try_from(n).ok()).ok_or_else(|| bad(c, "index out of range"))?,
|
||||
};
|
||||
let index = super::index_opt(p, "artboard", c)?.unwrap_or(0);
|
||||
if index >= s.doc()?.doc.artboards.len() {
|
||||
return Err(bad(c, "index out of range"));
|
||||
}
|
||||
@@ -583,14 +582,15 @@ fn artboard_param(s: &Session, p: &Value, c: &str) -> Result<usize> {
|
||||
fn ruler_origin(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let d = &s.doc()?.doc;
|
||||
// A document without artboards measures from its global zero point.
|
||||
let index = if d.artboards.is_empty() && p.get("artboard").is_none() { 0 } else { artboard_param(s, p, "document.rulerOrigin")? };
|
||||
let index =
|
||||
if d.artboards.is_empty() && p.get("artboard").is_none_or(Value::is_null) { 0 } else { artboard_param(s, p, "document.rulerOrigin")? };
|
||||
Ok(json!(s.doc()?.doc.ruler_origin_for(index)))
|
||||
}
|
||||
|
||||
fn set_ruler_origin(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "document.setRulerOrigin";
|
||||
let origin = Point::new(f64_req(p, "x", C)?, f64_req(p, "y", C)?);
|
||||
let index = if s.doc()?.doc.rulers == RulerMode::Global && p.get("artboard").is_none() { 0 } else { artboard_param(s, p, C)? };
|
||||
let index = if s.doc()?.doc.rulers == RulerMode::Global && p.get("artboard").is_none_or(Value::is_null) { 0 } else { artboard_param(s, p, C)? };
|
||||
change_ruler_origin(s, index, Some(origin), "Ruler Origin")
|
||||
}
|
||||
|
||||
|
||||
@@ -865,8 +865,8 @@ fn load_texture(params: &mut Value, cmd: &str) -> Result<()> {
|
||||
/// Has `n` an appearance Expand Appearance turns into objects: a path that isn't basic (several
|
||||
/// fills or strokes, effects, transparency of a fill or stroke, a brush), a group or layer with
|
||||
/// fills, strokes or effects of its own or such members, other objects with effects, type with
|
||||
/// fills or strokes of its own, or a symbol instance whose active fill or stroke has a geometry
|
||||
/// effect ([`effects::has_item_geometry`])?
|
||||
/// fills or strokes of its own, or a symbol instance whose active fill or stroke has a geometry or
|
||||
/// raster effect ([`effects::has_item_geometry`], [`effects::has_instance_row_raster`])?
|
||||
pub(crate) fn expandable(n: &Node) -> bool {
|
||||
expandable_except(n, &[])
|
||||
}
|
||||
@@ -883,7 +883,9 @@ pub(crate) fn expandable_except(n: &Node, skip: &[NodeId]) -> bool {
|
||||
!n.appearance.items.is_empty() || !n.appearance.effects.is_empty() || children.iter().any(|c| expandable_except(c, skip))
|
||||
}
|
||||
NodeKind::Text(_) => !n.appearance.items.is_empty() || !n.appearance.effects.is_empty(),
|
||||
NodeKind::Image(_) | NodeKind::SymbolInstance { .. } => effects::has_item_geometry(n) || !n.appearance.effects.is_empty(),
|
||||
NodeKind::Image(_) | NodeKind::SymbolInstance { .. } => {
|
||||
effects::has_item_geometry(n) || effects::has_instance_row_raster(n) || !n.appearance.effects.is_empty()
|
||||
}
|
||||
_ => !n.appearance.effects.is_empty(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ pub use encode::{ARTBOARD_PARAMS, ArtboardPick, Encoded, Note, NoteKind, encode,
|
||||
pub(crate) use encode::{anti_alias, background, with_single_artboard};
|
||||
pub(crate) use export::with_artboard_fills;
|
||||
pub(crate) use export::{EXPORT_PARAMS, export_crop, isolated};
|
||||
pub use export::{export_doc, export_source, unused_option_notes};
|
||||
pub use export::{export_doc, export_source, selection_doc, unused_option_notes};
|
||||
use load::err;
|
||||
pub(crate) use load::import_svg;
|
||||
pub(crate) use load::is_placeholder;
|
||||
|
||||
@@ -45,7 +45,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Export Selection…",
|
||||
["File"],
|
||||
None,
|
||||
"{path?, format?: png|jpg|webp|svg|svgz|pdf|ai|gif|png8|tiff|bmp|tga|psd|txt|dxf|eps|emf|wmf|hpgl|vectorcraft|template (default: from the extension, else png), ppi?: number (raster formats: pixels per inch, stored in the file; wins over scale; without either, the document's raster effects resolution), scale?: number (pixels per point), …the format's options} the selected objects (template layers left out), each in the layers and groups that hold it, whose clipping paths, opacity masks, effects, opacity and blend modes apply as on the canvas (an object of a blend, envelope, repeat or intertwine on its own; txt: the selected type alone), cropped to the bounds the renderer measures for that art (a clip group by its clip) → {path, format, bytes, warnings, bounds} (no path → {dataBase64, format, bytes, warnings, bounds}); warnings say what the format approximated or left out, as for document.export. A key no format takes, and an unknown key inside a PDF or EPS option's object, are refused; a key only another format takes is not used, and warnings name it. Under automation read roots, svg and svgz are refused while the document links a JavaScript file outside them, as document.export refuses them",
|
||||
"{path?, format?: png|jpg|webp|svg|svgz|pdf|ai|gif|png8|tiff|bmp|tga|psd|txt|dxf|eps|emf|wmf|hpgl|vectorcraft|template (default: from the extension, else png), ppi?: number (raster formats: pixels per inch, stored in the file; wins over scale; without either, the document's raster effects resolution), scale?: number (pixels per point), …the format's options} the selected objects (template layers left out), each in the layers and groups that hold it, whose clipping paths, opacity masks, effects, opacity and blend modes apply as on the canvas (an object of a blend, envelope, repeat or intertwine on its own; txt: the selected type alone), cropped to the bounds the renderer measures for that art (a clip group by its clip) → {path, format, bytes, warnings, bounds} (no path → {dataBase64, format, bytes, warnings, bounds}); warnings say what the format approximated or left out, as for document.export. A key no format takes, and an unknown key inside a PDF or EPS option's object, are refused; a key only another format takes is not used, and warnings name it. Under automation read roots, svg and svgz are refused while the document links a JavaScript file outside them, as document.export refuses them. In the app, with no params or {}, the save panel picks the file and its format's options dialog follows (Save PDF, SVG Options, the raster options, EPS Options, DXF Options, Text Export Options or an exporter plug-in's parameters; other formats are written at once), whose OK writes the selection: the call returns null while the dialog is open, and OK returns what file.exportAs returns, without format or bounds (Text Export Options: {path})",
|
||||
has_selection,
|
||||
export::export_selection
|
||||
),
|
||||
|
||||
@@ -295,8 +295,14 @@ fn source<'a>(s: &'a mut Session, f: &Format, p: &Value, cmd: &str) -> Result<(C
|
||||
/// ([`isolated`]), with one artboard: their crop. With `text`, the objects alone on one layer, so a
|
||||
/// text export holds the type of the selected objects and not that of a clipping path around them.
|
||||
fn selection(s: &mut Session, cmd: &str, text: bool) -> Result<(Document, vectorcraft_geom::Rect)> {
|
||||
let ids = selected_artwork(s)?;
|
||||
let doc = &s.doc()?.doc;
|
||||
selection_doc(s.doc()?, cmd, text)
|
||||
}
|
||||
|
||||
/// The document an Export Selection of `st` writes and its crop, as `document.exportSelection`
|
||||
/// writes it ([`selection`]); `cmd` names the command in the error when nothing selected draws.
|
||||
pub fn selection_doc(st: &DocState, cmd: &str, text: bool) -> Result<(Document, vectorcraft_geom::Rect)> {
|
||||
let ids = artwork_of(st);
|
||||
let doc = &st.doc;
|
||||
let (mut d, bounds) = isolated(doc, &ids, "Selection").ok_or_else(|| bad(cmd, "select something to export"))?;
|
||||
if text {
|
||||
let mut layer = Node::layer(d.alloc_id(), "Selection", vectorcraft_doc::LayerColor::Preset(0));
|
||||
@@ -312,12 +318,16 @@ fn selection(s: &mut Session, cmd: &str, text: bool) -> Result<(Document, vector
|
||||
/// layers are guides and are left out. With an object of a dynamic symbol instance's art selected,
|
||||
/// the instance is exported whole.
|
||||
pub(crate) fn selected_artwork(s: &Session) -> Result<Vec<NodeId>> {
|
||||
let st = s.doc()?;
|
||||
Ok(artwork_of(s.doc()?))
|
||||
}
|
||||
|
||||
/// [`selected_artwork`] of document `st`.
|
||||
fn artwork_of(st: &DocState) -> Vec<NodeId> {
|
||||
let ids = edit::roots_of(&st.doc, st.selection.in_paint_order(&st.doc));
|
||||
let is_template = |id| st.doc.node(id).is_some_and(|l| matches!(l.kind, NodeKind::Layer { template: true, .. }));
|
||||
// On a template layer or sublayer, at any depth.
|
||||
let on_template = |id| st.doc.ancestry(id).is_some_and(|a| a.into_iter().any(is_template));
|
||||
Ok(ids.into_iter().filter(|id| !on_template(*id)).collect())
|
||||
ids.into_iter().filter(|id| !on_template(*id)).collect()
|
||||
}
|
||||
|
||||
/// The crop of an export of objects `ids` of `doc` alone ([`isolated`]): the bounds the renderer
|
||||
|
||||
@@ -304,11 +304,22 @@ fn options_not_applied_yet_and_document_features_warn() {
|
||||
let frame = s.execute("shape.rectangle", &json!({"x": 20, "y": 20, "width": 50, "height": 40})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("paint.setStroke", &json!({"ids": [frame], "swatch": "Dots"})).unwrap();
|
||||
assert_eq!(warnings(&export(&mut s, json!({}))).len(), 0);
|
||||
// A knockout group is approximated: its warning comes back from every PDF path.
|
||||
// A knockout group is written as a PDF knockout group: nothing to report.
|
||||
s.execute("select.set", &json!({"ids": [frame]})).unwrap();
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
s.execute("transparency.set", &json!({"knockout": "on"})).unwrap();
|
||||
let knockout = |w: Vec<String>| w.iter().any(|w| w.contains("knockout groups"));
|
||||
assert!(!knockout(warnings(&export(&mut s, json!({})))));
|
||||
// One whose soft masks would draw a brushed stroke is approximated: its warning comes back
|
||||
// from every PDF path.
|
||||
let rect =
|
||||
|s: &mut Session, x: i32| s.execute("shape.rectangle", &json!({"x": x, "y": 45, "width": 40, "height": 30})).unwrap()["id"].as_u64().unwrap();
|
||||
let (lower, upper) = (rect(&mut s, 20), rect(&mut s, 40));
|
||||
s.execute("brush.apply", &json!({"ids": [upper], "name": "3 pt. Round"})).unwrap();
|
||||
s.execute("object.setProps", &json!({"ids": [lower, upper], "opacity": 50, "knockoutShape": true})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [lower, upper]})).unwrap();
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
s.execute("transparency.set", &json!({"knockout": "on"})).unwrap();
|
||||
assert!(knockout(warnings(&export(&mut s, json!({})))));
|
||||
assert!(knockout(warnings(&s.execute("document.export", &as_export(&json!({}))).unwrap())));
|
||||
// Other formats have none.
|
||||
@@ -432,6 +443,17 @@ fn pdf_settings_summarise_changes_and_warnings() {
|
||||
s.execute("effect.apply", &json!({"effect": "stylize.dropShadow", "ids": [g]})).unwrap();
|
||||
assert_eq!(s.execute("document.pdfSettings", &json!({"includeDocument": true})).unwrap()["warnings"], json!([]));
|
||||
s.execute("transparency.set", &json!({"knockout": "on"})).unwrap();
|
||||
// A knockout group is written as a PDF knockout group: nothing to report.
|
||||
assert_eq!(s.execute("document.pdfSettings", &json!({"includeDocument": true})).unwrap()["warnings"], json!([]));
|
||||
// One whose soft masks would draw a brushed stroke is approximated.
|
||||
let rect =
|
||||
|s: &mut Session, x: i32| s.execute("shape.rectangle", &json!({"x": x, "y": 45, "width": 40, "height": 30})).unwrap()["id"].as_u64().unwrap();
|
||||
let (lower, upper) = (rect(&mut s, 20), rect(&mut s, 40));
|
||||
s.execute("brush.apply", &json!({"ids": [upper], "name": "3 pt. Round"})).unwrap();
|
||||
s.execute("object.setProps", &json!({"ids": [lower, upper], "opacity": 50, "knockoutShape": true})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [lower, upper]})).unwrap();
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
s.execute("transparency.set", &json!({"knockout": "on"})).unwrap();
|
||||
assert_eq!(s.execute("document.pdfSettings", &json!({})).unwrap()["warnings"], json!([]));
|
||||
let full = s.execute("document.pdfSettings", &json!({"includeDocument": true})).unwrap();
|
||||
assert!(warnings(&full).iter().any(|w| w.contains("knockout groups")), "{}", full["warnings"]);
|
||||
|
||||
@@ -33,7 +33,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Gradient Vector",
|
||||
[],
|
||||
None,
|
||||
"{start?: [x,y], end?: [x,y] (document coordinates; both or neither: omitted, the vector stays), aspect?: % (radial, conic and diamond: the extent's height / width across the vector, a negative one mirroring a conic's sweep and an error for the other kinds; default: kept), focal?: [x,y] (document coordinates) | null (radial: the focal point, where the first stop sits, pulled inside the extent ellipse; null centres it; default: it keeps its place in the ellipse; an error on a conic or diamond gradient), ids?|id?, stroke?: bool (default: the targeted item's kind, else the active proxy), item?: fill/stroke item index|null (alias: index; omitted: the Appearance panel's active item when it is of the edited kind), target?: \"object\"|\"contents\", index?: appearance item index (as item)} set the gradient vector, aspect ratio and focal point (solid paints become the default gradient; type objects set it on their runs, in text space). On a path directly in a graph series, the graph also stores the gradient with its placement when it is on the topmost visible fill or stroke (docs/mcp.md); on a graph label's row, the graph stores the label's topmost visible fill and stroke rows for every label of its kind, with the gradient fitted to each label",
|
||||
"{start?: [x,y], end?: [x,y] (document coordinates; both or neither: omitted, the vector stays), aspect?: % (radial, conic and diamond: the extent's height / width across the vector, a negative one mirroring a conic's sweep and an error for the other kinds; default: kept), focal?: [x,y] (document coordinates) | null (radial: the focal point, where the first stop sits, pulled inside the extent ellipse; null centres it; default: it keeps its place in the ellipse; an error on a conic or diamond gradient), ids?|id?, stroke?: bool (default: the targeted item's kind, else the active proxy), item?: fill/stroke item index|null (alias: index; omitted: the Appearance panel's active item when it is of the edited kind), target?: \"object\"|\"contents\", index?: appearance item index (as item)} set the gradient vector, aspect ratio and focal point (solid paints become the default gradient; type objects set it on their runs, in text space). On a path directly in a graph series, the graph also stores the gradient with its placement relative to the path's box when it is on the topmost visible fill or stroke, and every path of the series takes it at the same place on its own box (docs/mcp.md); on a graph label's row, the graph stores the label's topmost visible fill and stroke rows for every label of its kind, with the gradient fitted to each label",
|
||||
has_doc,
|
||||
set_gradient_geom
|
||||
),
|
||||
|
||||
@@ -6,7 +6,7 @@ use super::labels::{
|
||||
recolour_text, runs, shadow_of,
|
||||
};
|
||||
use super::markers::{MARKER_BUDGET, marker_cost, marker_designs};
|
||||
use super::series::{series_marks, with_series_opacity};
|
||||
use super::series::{series_marks, with_series_gradients, with_series_opacity};
|
||||
use super::{
|
||||
Arc, BezPath, Color, Document, GraphKind, GraphSpec, Justify, Node, NodeKind, Paint, PathData, Point, Rect, TickLength, ValueAxisSide, shapes,
|
||||
};
|
||||
@@ -1006,7 +1006,7 @@ impl<'a> Plot<'a> {
|
||||
}
|
||||
}
|
||||
let name = g.series.get(s).cloned().filter(|n| !n.is_empty()).unwrap_or_else(|| format!("Series {}", s + 1));
|
||||
let grp = self.b.series(&name, s, with_series_opacity(g, s, items));
|
||||
let grp = self.b.series(&name, s, with_series_opacity(g, s, with_series_gradients(g, s, items)));
|
||||
self.b.out.push(grp);
|
||||
}
|
||||
self.b.out.append(&mut self.on_top);
|
||||
|
||||
@@ -2,7 +2,7 @@ use vectorcraft_color::BlendMode;
|
||||
|
||||
use super::axis::radar_axis;
|
||||
use super::labels::{Role, filled_lines_on, label_para, label_style};
|
||||
use super::{Arc, CharStyle, Color, Document, GraphKind, GraphSpec, HashMap, Node, NodeId, NodeKind, Paint, Result, Session, Value, bad};
|
||||
use super::{Arc, CharStyle, Color, Document, GraphKind, GraphSpec, HashMap, Node, NodeId, NodeKind, Paint, Rect, Result, Session, Value, bad};
|
||||
|
||||
// ---------- generation ----------
|
||||
|
||||
@@ -229,7 +229,8 @@ pub(crate) fn capture_series_top_paints(doc: &mut Document, edited: &[(NodeId, b
|
||||
continue;
|
||||
}
|
||||
let (paint, width) = shown_paint(node, *fill);
|
||||
captures.push((r.graph, r.index, *fill, paint, (!*fill).then_some(width)));
|
||||
let width = (!*fill).then_some(width);
|
||||
captures.push((r.graph, r.index, *fill, paint, width, paint_box(node, width)));
|
||||
}
|
||||
store_series_paints(doc, captures)
|
||||
}
|
||||
@@ -281,11 +282,129 @@ fn settle_series_paints(doc: &mut Document, before: SeriesPaintsBefore) -> bool
|
||||
changed
|
||||
}
|
||||
|
||||
/// Store each capture (graph, series index, fill or stroke, paint, stroke weight) in its series. Returns whether a
|
||||
/// The box a series keeps a gradient relative to: (0, 0)–(1, 1), as graphic styles keep theirs.
|
||||
const UNIT_BOX: Rect = vectorcraft_doc::GraphicStyle::UNIT_BOX;
|
||||
|
||||
/// The gradient `slot` keeps for its fill (`fill`) or stroke as the graph draws it on box `on`: one kept relative to the
|
||||
/// unit box placed on `on`, also when `on` has no width or no height, and one kept in document coordinates as it is kept.
|
||||
/// None when the slot keeps no gradient, or keeps one relative to the unit box and there is no box.
|
||||
fn kept_on(slot: &vectorcraft_doc::SeriesPaint, fill: bool, on: Option<Rect>) -> Option<Paint> {
|
||||
let (kept, unit) = if fill { (&slot.fill, slot.fill_unit_box) } else { (&slot.stroke, slot.stroke_unit_box) };
|
||||
let Some(Paint::Gradient(g)) = kept else { return None };
|
||||
if !unit {
|
||||
return Some(Paint::Gradient(g.clone()));
|
||||
}
|
||||
let on = on?;
|
||||
let mut g = g.clone();
|
||||
g.rebase(UNIT_BOX, on);
|
||||
Some(Paint::Gradient(g))
|
||||
}
|
||||
|
||||
/// Whether `a` and `b` are the same paint, a gradient's vector, focal point and freeform points each within 1e-6 pt (the
|
||||
/// rounding a move or a placement on another box leaves).
|
||||
fn same_paint_placed(a: &Paint, b: &Paint) -> bool {
|
||||
let (Paint::Gradient(a), Paint::Gradient(b)) = (a, b) else { return a == b };
|
||||
let near = |p: vectorcraft_geom::Point, q: vectorcraft_geom::Point| (p - q).hypot() <= 1e-6;
|
||||
let mut a = (**a).clone();
|
||||
match (&mut a.geom, &b.geom) {
|
||||
(Some(ag), Some(bg)) => {
|
||||
let focal = match (ag.focal, bg.focal) {
|
||||
(Some(p), Some(q)) => near(p, q),
|
||||
(p, q) => p.is_none() && q.is_none(),
|
||||
};
|
||||
if !(near(ag.start, bg.start) && near(ag.end, bg.end) && focal) {
|
||||
return false;
|
||||
}
|
||||
(ag.start, ag.end, ag.focal) = (bg.start, bg.end, bg.focal);
|
||||
}
|
||||
(None, None) => {}
|
||||
_ => return false,
|
||||
}
|
||||
match (&mut a.freeform, &b.freeform) {
|
||||
(Some(af), Some(bf)) => {
|
||||
if af.points.len() != bf.points.len() || !af.points.iter().zip(&bf.points).all(|(p, q)| near(p.at, q.at)) {
|
||||
return false;
|
||||
}
|
||||
for (p, q) in af.points.iter_mut().zip(&bf.points) {
|
||||
p.at = q.at;
|
||||
}
|
||||
}
|
||||
(None, None) => {}
|
||||
_ => return false,
|
||||
}
|
||||
a == **b
|
||||
}
|
||||
|
||||
/// The box a gradient on `node`'s fill (`width` None) or on its stroke of `width` is placed on: the geometric bounds, grown
|
||||
/// by half the weight for a stroke. The box can have no width or no height.
|
||||
fn paint_box(node: &Node, width: Option<f64>) -> Option<Rect> {
|
||||
let b = node.geometric_bounds()?;
|
||||
Some(width.map_or(b, |w| vectorcraft_doc::appearance::stroke_paint_bounds(b, w)))
|
||||
}
|
||||
|
||||
/// `paint` as a series keeps it: a gradient placed on `from` (the edited mark's [`paint_box`]) is moved to the same place
|
||||
/// relative to [`UNIT_BOX`], so each mark takes it on its own box. A box with no width or no height, which a gradient's
|
||||
/// place cannot be kept relative to, leaves the paint in document coordinates. Returns the paint and whether it was moved.
|
||||
fn unit_box_paint(paint: Paint, from: Option<Rect>) -> (Paint, bool) {
|
||||
match (paint, from.filter(|b| b.width() > 1e-9 && b.height() > 1e-9)) {
|
||||
(Paint::Gradient(mut g), Some(from)) => {
|
||||
g.rebase(from, UNIT_BOX);
|
||||
(Paint::Gradient(g), true)
|
||||
}
|
||||
(paint, _) => (paint, false),
|
||||
}
|
||||
}
|
||||
|
||||
/// The paths directly inside series `i`'s group (`items`) with the series' kept gradients, which are kept relative to the
|
||||
/// unit box, placed on each path's own box: a fill on its geometric bounds, a stroke on its stroke's box. Paints kept in
|
||||
/// document coordinates (from files saved before the unit box) are left as they are.
|
||||
pub(super) fn with_series_gradients(g: &GraphSpec, i: usize, mut items: Vec<Arc<Node>>) -> Vec<Arc<Node>> {
|
||||
let Some(slot) = g.series_paints.get(i) else { return items };
|
||||
fn kept(p: &Option<Paint>, unit: bool) -> Option<&Paint> {
|
||||
p.as_ref().filter(|p| unit && matches!(p, Paint::Gradient(_)))
|
||||
}
|
||||
let (fill, stroke) = (kept(&slot.fill, slot.fill_unit_box), kept(&slot.stroke, slot.stroke_unit_box));
|
||||
if fill.is_none() && stroke.is_none() {
|
||||
return items;
|
||||
}
|
||||
// A Filled Lines band takes the kept stroke as its fill when no fill is kept.
|
||||
let band_fill = if slot.fill.is_none() { stroke } else { None };
|
||||
let fill_from_store = |p: &Paint| fill == Some(p) || band_fill == Some(p);
|
||||
let stroke_from_store = |p: &Paint| stroke == Some(p);
|
||||
for item in &mut items {
|
||||
if !matches!(item.kind, NodeKind::Path { .. }) {
|
||||
continue;
|
||||
}
|
||||
let Some(b) = item.geometric_bounds() else { continue };
|
||||
let ap = &item.appearance;
|
||||
if !ap.fill().is_some_and(|f| fill_from_store(&f.paint)) && !ap.stroke().is_some_and(|st| stroke_from_store(&st.paint)) {
|
||||
continue;
|
||||
}
|
||||
let ap = &mut Arc::make_mut(item).appearance;
|
||||
if let Some(f) = ap.fill_mut().filter(|f| fill_from_store(&f.paint))
|
||||
&& let Paint::Gradient(gp) = &mut f.paint
|
||||
{
|
||||
gp.rebase(UNIT_BOX, b);
|
||||
}
|
||||
if let Some(st) = ap.stroke_mut().filter(|st| stroke_from_store(&st.paint)) {
|
||||
let to = st.paint_bounds(b);
|
||||
if let Paint::Gradient(gp) = &mut st.paint {
|
||||
gp.rebase(UNIT_BOX, to);
|
||||
}
|
||||
}
|
||||
}
|
||||
items
|
||||
}
|
||||
|
||||
/// A paint taken from a series mark: graph, series index, fill or stroke, paint, stroke weight and the mark's
|
||||
/// [`paint_box`].
|
||||
type Capture = (NodeId, usize, bool, Paint, Option<f64>, Option<Rect>);
|
||||
|
||||
/// Store each capture in its series; a gradient is kept relative to the unit box ([`unit_box_paint`]). Returns whether a
|
||||
/// series' captured fill or stroke differs from what the graph stored before the first capture.
|
||||
fn store_series_paints(doc: &mut Document, captures: Vec<(NodeId, usize, bool, Paint, Option<f64>)>) -> bool {
|
||||
fn store_series_paints(doc: &mut Document, captures: Vec<Capture>) -> bool {
|
||||
let mut before = SeriesPaintsBefore::new();
|
||||
for (graph, index, fill, paint, width) in captures {
|
||||
for (graph, index, fill, paint, width, from) in captures {
|
||||
let Some(spec) = doc.node_mut(graph).and_then(|n| n.extras_if_any().and_then(|e| e.graph.as_deref_mut())) else { continue };
|
||||
before.entry(graph).or_insert_with(|| spec.series_paints.clone());
|
||||
// `index` is below the graph's series count (`series_members`).
|
||||
@@ -293,10 +412,17 @@ fn store_series_paints(doc: &mut Document, captures: Vec<(NodeId, usize, bool, P
|
||||
spec.series_paints.resize(index + 1, vectorcraft_doc::SeriesPaint::default());
|
||||
}
|
||||
let Some(slot) = spec.series_paints.get_mut(index) else { continue };
|
||||
// A mark that shows the kept gradient as it was drawn leaves the kept gradient as it is.
|
||||
let drawn = kept_on(slot, fill, from).is_some_and(|kept| same_paint_placed(&kept, &paint));
|
||||
let (paint, unit) = unit_box_paint(paint, from);
|
||||
if fill {
|
||||
slot.fill = Some(paint);
|
||||
if !drawn {
|
||||
(slot.fill, slot.fill_unit_box) = (Some(paint), unit);
|
||||
}
|
||||
} else {
|
||||
slot.stroke = Some(paint);
|
||||
if !drawn {
|
||||
(slot.stroke, slot.stroke_unit_box) = (Some(paint), unit);
|
||||
}
|
||||
slot.stroke_width = width;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -508,6 +508,89 @@ fn paint_targets_leave_a_graph_designs_copy_out() {
|
||||
assert_eq!(super::paint_targets(d, &[copy, art, mark]), [mark]);
|
||||
}
|
||||
|
||||
/// The art of a marker design in series 1 of a line graph: the design's copy and a path inside it.
|
||||
fn marker_design_art() -> (Session, NodeId, NodeId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
save_design(&mut s, "Pill");
|
||||
let id = graph(&mut s, "line");
|
||||
s.execute("graph.marker", &json!({"id": id.0, "seriesIndexes": [1], "design": "Pill"})).unwrap();
|
||||
let marker = series(s.doc().unwrap().doc.node(id).unwrap(), 1).children().unwrap()[1].clone();
|
||||
assert!(matches!(marker.kind, NodeKind::Group { .. }), "the design's art");
|
||||
let mut art = vec![];
|
||||
marker.walk(&mut |m| {
|
||||
if matches!(m.kind, NodeKind::Path { .. }) {
|
||||
art.push(m.id);
|
||||
}
|
||||
});
|
||||
(s, marker.id, art[0])
|
||||
}
|
||||
|
||||
/// The ids `graphicStyle.list` reports as linked to style `name`.
|
||||
fn linked_to(s: &mut Session, name: &str) -> Vec<u64> {
|
||||
let list = s.execute("graphicStyle.list", &json!({})).unwrap();
|
||||
let style = list["styles"].as_array().unwrap().iter().find(|g| g["name"] == name).unwrap().clone();
|
||||
let mut ids: Vec<u64> = style["linked"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect();
|
||||
ids.sort_unstable();
|
||||
ids
|
||||
}
|
||||
|
||||
/// Graphic styles and the art of a column or marker design in a series (the design's copy and a path inside it):
|
||||
/// `graphicStyle.new` makes a style from the art without linking it; `graphicStyle.redefine` leaves the art's look as
|
||||
/// it was and unlinks it, whether the art is linked (as in a document saved before) or the source, while the other
|
||||
/// linked objects follow the new look.
|
||||
#[test]
|
||||
fn graphic_styles_leave_a_graph_designs_art_unlinked() {
|
||||
let make = |marker: bool| {
|
||||
if marker {
|
||||
marker_design_art()
|
||||
} else {
|
||||
let (s, _, copy, art, _) = design_art();
|
||||
(s, copy, art)
|
||||
}
|
||||
};
|
||||
let rows = |s: &Session, id: NodeId| s.doc().unwrap().doc.node(id).unwrap().appearance.clone();
|
||||
let link = |s: &Session, id: NodeId| s.doc().unwrap().doc.node(id).unwrap().extras().graphic_style;
|
||||
let link_directly = |s: &mut Session, ids: &[NodeId], style: u32| {
|
||||
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
for id in ids {
|
||||
d.node_mut(*id).unwrap().extras_mut().graphic_style = Some(style);
|
||||
}
|
||||
};
|
||||
let rect = |s: &mut Session| {
|
||||
NodeId(s.execute("shape.rectangle", &json!({"x": 700, "y": 700, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap())
|
||||
};
|
||||
for marker in [false, true] {
|
||||
for source in [0, 1] {
|
||||
let at = format!("marker {marker}, source {source}");
|
||||
// New: the style is made from the art, which stays unlinked.
|
||||
let (mut s, copy, art) = make(marker);
|
||||
let source = [copy, art][source];
|
||||
s.execute("graphicStyle.new", &json!({"id": source.0, "name": "Art"})).unwrap();
|
||||
assert_eq!(link(&s, source), None, "{at}");
|
||||
assert_eq!(linked_to(&mut s, "Art"), Vec::<u64>::new(), "{at}");
|
||||
// Redefine from another object: linked art keeps its look and is unlinked; a linked rectangle follows.
|
||||
let follower = rect(&mut s);
|
||||
s.execute("graphicStyle.apply", &json!({"name": "Art", "ids": [follower.0]})).unwrap();
|
||||
let style = link(&s, follower).unwrap();
|
||||
link_directly(&mut s, &[copy, art], style);
|
||||
let (copy_was, art_was) = (rows(&s, copy), rows(&s, art));
|
||||
let (blue_rect, blue) = blue_style(&mut s, "Blue", false);
|
||||
s.execute("graphicStyle.redefine", &json!({"name": "Art", "id": blue_rect.0})).unwrap();
|
||||
assert_eq!((rows(&s, copy), rows(&s, art)), (copy_was.clone(), art_was.clone()), "{at}");
|
||||
assert_eq!((link(&s, copy), link(&s, art)), (None, None), "{at}");
|
||||
assert_eq!(rows(&s, follower).fill_paint(), blue, "{at}");
|
||||
assert_eq!(linked_to(&mut s, "Art"), [follower.0.min(blue_rect.0), follower.0.max(blue_rect.0)], "{at}");
|
||||
// Redefine from the art itself: the art is unlinked and the linked rectangle takes the art's look.
|
||||
link_directly(&mut s, &[copy, art], style);
|
||||
s.execute("graphicStyle.redefine", &json!({"name": "Art", "id": source.0})).unwrap();
|
||||
assert_eq!((rows(&s, copy), rows(&s, art)), (copy_was, art_was), "{at}");
|
||||
assert_eq!((link(&s, copy), link(&s, art)), (None, None), "{at}");
|
||||
assert_eq!(linked_to(&mut s, "Art"), [follower.0.min(blue_rect.0), follower.0.max(blue_rect.0)], "{at}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The art of a marker design in a series is refused too.
|
||||
#[test]
|
||||
fn effect_commands_refuse_a_marker_designs_art() {
|
||||
@@ -2754,10 +2837,39 @@ fn a_gradient_edit_on_a_series_mark_is_kept() {
|
||||
series_0_is(&mut s, id, (&fill, 0.4, &red, 2.0), &was);
|
||||
}
|
||||
|
||||
/// A gradient placed on a series mark's fill with `paint.setGradientGeom` is stored on the graph as it is placed, and
|
||||
/// every mark of the series has that fill, placement included, after the graph is drawn again.
|
||||
/// Where gradient `paint` sits on box `b`: its vector's start and end, then each freeform point, as fractions of the box.
|
||||
fn gradient_place(paint: &Paint, b: vectorcraft_geom::Rect) -> Vec<f64> {
|
||||
let Paint::Gradient(g) = paint else { panic!("not a gradient: {paint:?}") };
|
||||
let points = g.geom.iter().flat_map(|geom| [geom.start, geom.end]).chain(g.freeform.iter().flat_map(|f| f.points.iter().map(|p| p.at)));
|
||||
points.flat_map(|p| [(p.x - b.x0) / b.width(), (p.y - b.y0) / b.height()]).collect()
|
||||
}
|
||||
|
||||
/// The box a gradient on `mark`'s fill (`stroke` false) or stroke is placed on.
|
||||
fn paint_box_of(mark: &vectorcraft_doc::Node, stroke: bool) -> vectorcraft_geom::Rect {
|
||||
let b = mark.geometric_bounds().unwrap();
|
||||
if stroke { vectorcraft_doc::appearance::stroke_paint_bounds(b, mark.appearance.stroke_width()) } else { b }
|
||||
}
|
||||
|
||||
/// The fill (`stroke` false) or stroke of every path of series 0, the legend swatch included, has a gradient at `place` on
|
||||
/// its own box, and the gradients are not all at one place in the document. Returns how many paths were checked.
|
||||
fn series_0_gradients_at(s: &Session, id: NodeId, stroke: bool, place: &[f64]) -> usize {
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
let paths: Vec<_> = series(&n, 0).children().unwrap().iter().filter(|m| matches!(m.kind, NodeKind::Path { .. })).cloned().collect();
|
||||
let paints: Vec<Paint> = paths.iter().map(|m| if stroke { m.appearance.stroke_paint() } else { m.appearance.fill_paint() }).collect();
|
||||
for (m, paint) in paths.iter().zip(&paints) {
|
||||
let at = gradient_place(paint, paint_box_of(m, stroke));
|
||||
assert_eq!(at.len(), place.len(), "{at:?}");
|
||||
assert!(at.iter().zip(place).all(|(a, b)| (a - b).abs() < 1e-9), "{at:?} is not {place:?}");
|
||||
}
|
||||
assert!(paints.windows(2).any(|w| w[0] != w[1]), "every mark has the gradient at one place: {paints:?}");
|
||||
paths.len()
|
||||
}
|
||||
|
||||
/// A gradient placed on a series mark's fill with `paint.setGradientGeom` is stored relative to the mark's box, and every
|
||||
/// mark of the series has it at the same place on its own box after the graph is drawn again, also after a data change
|
||||
/// moves and resizes the marks and after the graph is moved.
|
||||
#[test]
|
||||
fn a_gradient_placed_on_a_series_mark_is_kept_with_its_placement() {
|
||||
fn a_gradient_placed_on_a_series_mark_takes_the_same_place_on_every_mark() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
// `paint.setFill` stores the gradient before it is placed.
|
||||
s.execute("paint.setFill", &json!({"ids": [marks[0].0], "gradient": {"kind": "linear"}})).unwrap();
|
||||
@@ -2766,26 +2878,258 @@ fn a_gradient_placed_on_a_series_mark_is_kept_with_its_placement() {
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [marks[0].0], "start": [0, 0], "end": [10, 40]})).unwrap();
|
||||
let placed = fill_of(&s, marks[0]);
|
||||
assert_ne!(placed, fill, "the command places the gradient");
|
||||
assert_eq!(spec(&s, id).series_paints[0].fill, Some(placed.clone()));
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2], [3, 4]]})).unwrap();
|
||||
assert!(marks_of(&s, id, 0).iter().all(|m| m.0 == placed), "{:?}", marks_of(&s, id, 0));
|
||||
let mark = s.doc().unwrap().doc.node(marks[0]).unwrap().clone();
|
||||
let place = gradient_place(&placed, paint_box_of(&mark, false));
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
assert!(kept.fill_unit_box);
|
||||
assert_eq!(gradient_place(kept.fill.as_ref().unwrap(), vectorcraft_doc::GraphicStyle::UNIT_BOX), place);
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2], [3, 4], [6, 1]]})).unwrap();
|
||||
assert!(series_0_gradients_at(&s, id, false, &place) >= 4);
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 50, "dy": 30})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[2, 2], [5, 4], [1, 1]]})).unwrap();
|
||||
series_0_gradients_at(&s, id, false, &place);
|
||||
}
|
||||
|
||||
/// A point recolored with `paint.freeform.setPoint` in a freeform gradient on a series mark's fill is stored on the graph.
|
||||
/// A gradient placed on a series mark's stroke is kept relative to the mark's stroke box, and every mark of the series has
|
||||
/// it at the same place on its own stroke box after the graph is drawn again.
|
||||
#[test]
|
||||
fn a_gradient_placed_on_a_series_marks_stroke_takes_the_same_place_on_every_mark() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
s.execute("paint.setStroke", &json!({"ids": [marks[1].0], "gradient": {"kind": "radial"}})).unwrap();
|
||||
s.execute("stroke.set", &json!({"ids": [marks[1].0], "weight": 4})).unwrap();
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [marks[1].0], "stroke": true, "start": [150, 200], "end": [170, 260]})).unwrap();
|
||||
let mark = s.doc().unwrap().doc.node(marks[1]).unwrap().clone();
|
||||
let place = gradient_place(&mark.appearance.stroke_paint(), paint_box_of(&mark, true));
|
||||
assert!(spec(&s, id).series_paints[0].stroke_unit_box);
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2], [3, 4], [6, 1]]})).unwrap();
|
||||
series_0_gradients_at(&s, id, true, &place);
|
||||
}
|
||||
|
||||
/// A placed gradient picked up with the Eyedropper from art outside the graph is kept relative to the series mark it is
|
||||
/// dropped on, and every mark of the series has it at the same place on its own box.
|
||||
#[test]
|
||||
fn a_placed_gradient_dropped_on_a_series_mark_takes_the_same_place_on_every_mark() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
let r = NodeId(s.execute("shape.rectangle", &json!({"x": 600, "y": 600, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("paint.setFill", &json!({"ids": [r.0], "gradient": {"kind": "linear"}})).unwrap();
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [r.0], "start": [610, 600], "end": [630, 620]})).unwrap();
|
||||
s.execute("appearance.copyFrom", &json!({"source": r.0, "ids": [marks[2].0]})).unwrap();
|
||||
let mark = s.doc().unwrap().doc.node(marks[2]).unwrap().clone();
|
||||
let place = gradient_place(&mark.appearance.fill_paint(), paint_box_of(&mark, false));
|
||||
assert!(spec(&s, id).series_paints[0].fill_unit_box);
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2], [3, 4], [6, 1]]})).unwrap();
|
||||
series_0_gradients_at(&s, id, false, &place);
|
||||
}
|
||||
|
||||
/// A graphic style with a placed gradient applied to a series mark is kept relative to that mark's box, and every mark of
|
||||
/// the series has the gradient at the same place on its own box.
|
||||
#[test]
|
||||
fn a_graphic_style_gradient_applied_to_a_series_mark_takes_the_same_place_on_every_mark() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
let r = NodeId(s.execute("shape.rectangle", &json!({"x": 600, "y": 600, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("paint.setFill", &json!({"ids": [r.0], "gradient": {"kind": "linear"}})).unwrap();
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [r.0], "start": [610, 600], "end": [630, 620]})).unwrap();
|
||||
s.execute("graphicStyle.new", &json!({"id": r.0, "name": "Placed"})).unwrap();
|
||||
s.execute("graphicStyle.apply", &json!({"name": "Placed", "ids": [marks[2].0]})).unwrap();
|
||||
let mark = s.doc().unwrap().doc.node(marks[2]).unwrap().clone();
|
||||
let place = gradient_place(&mark.appearance.fill_paint(), paint_box_of(&mark, false));
|
||||
assert!(spec(&s, id).series_paints[0].fill_unit_box);
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2], [3, 4], [6, 1]]})).unwrap();
|
||||
series_0_gradients_at(&s, id, false, &place);
|
||||
}
|
||||
|
||||
/// A point recolored with `paint.freeform.setPoint` in a freeform gradient on a series mark's fill is stored on the graph,
|
||||
/// and every mark of the series has the gradient's points at the same place on its own box after the graph is drawn again.
|
||||
#[test]
|
||||
fn a_freeform_gradient_edit_on_a_series_mark_is_kept() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
let points = json!([{"at": [20, 20], "color": "#53aadf"}, {"at": [50, 50], "color": "#ff8000"}]);
|
||||
// `paint.setFill` stores the gradient before its point is recolored.
|
||||
s.execute("paint.setFill", &json!({"ids": [marks[0].0], "gradient": {"kind": "freeform", "freeform": {"points": points}}})).unwrap();
|
||||
let fill = fill_of(&s, marks[0]);
|
||||
assert_eq!(spec(&s, id).series_paints[0].fill, Some(fill.clone()));
|
||||
s.execute("paint.freeform.setPoint", &json!({"ids": [marks[0].0], "index": 1, "color": "#00ff00"})).unwrap();
|
||||
let edited = fill_of(&s, marks[0]);
|
||||
assert_ne!(edited, fill, "the command recolors the point");
|
||||
assert_eq!(spec(&s, id).series_paints[0].fill, Some(edited.clone()));
|
||||
let Paint::Gradient(g) = &edited else { panic!("{edited:?}") };
|
||||
assert_eq!(g.freeform.as_ref().unwrap().points[1].color.to_hex(), "#00ff00");
|
||||
let mark = s.doc().unwrap().doc.node(marks[0]).unwrap().clone();
|
||||
let place = gradient_place(&edited, paint_box_of(&mark, false));
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
assert!(kept.fill_unit_box);
|
||||
assert_eq!(gradient_place(kept.fill.as_ref().unwrap(), vectorcraft_doc::GraphicStyle::UNIT_BOX), place);
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2], [3, 4]]})).unwrap();
|
||||
assert!(marks_of(&s, id, 0).iter().all(|m| m.0 == edited), "{:?}", marks_of(&s, id, 0));
|
||||
series_0_gradients_at(&s, id, false, &place);
|
||||
}
|
||||
|
||||
/// A gradient a graph kept in document coordinates (a file saved before gradients were kept relative to the unit box) is
|
||||
/// drawn on every mark as it was kept.
|
||||
#[test]
|
||||
fn a_gradient_kept_in_document_coordinates_is_drawn_as_kept() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
s.execute("paint.setFill", &json!({"ids": [marks[0].0], "gradient": {"kind": "linear"}})).unwrap();
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [marks[0].0], "start": [0, 0], "end": [10, 40]})).unwrap();
|
||||
let placed = fill_of(&s, marks[0]);
|
||||
s.edit("Kept in document coordinates", |d, _| {
|
||||
let mut extras = d.node_mut(id).unwrap().extras_mut();
|
||||
let slot = &mut extras.graph.as_mut().unwrap().series_paints[0];
|
||||
(slot.fill, slot.fill_unit_box) = (Some(placed.clone()), false);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2], [3, 4]]})).unwrap();
|
||||
assert!(marks_of(&s, id, 0).iter().all(|m| m.0 == placed), "{:?}", marks_of(&s, id, 0));
|
||||
// The same gradient kept for the fill relative to the unit box leaves the stroke kept in document coordinates as it is.
|
||||
s.edit("Kept fill and stroke", |d, _| {
|
||||
let mut extras = d.node_mut(id).unwrap().extras_mut();
|
||||
let slot = &mut extras.graph.as_mut().unwrap().series_paints[0];
|
||||
(slot.fill, slot.fill_unit_box) = (Some(placed.clone()), true);
|
||||
(slot.stroke, slot.stroke_unit_box, slot.stroke_width) = (Some(placed.clone()), false, Some(2.0));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[2, 2], [3, 4]]})).unwrap();
|
||||
let drawn = marks_of(&s, id, 0);
|
||||
assert!(drawn.iter().all(|m| m.1 == placed), "{drawn:?}");
|
||||
series_0_gradients_at(&s, id, false, &gradient_place(&placed, vectorcraft_doc::GraphicStyle::UNIT_BOX));
|
||||
}
|
||||
|
||||
/// A gradient placed on a series path with no height (a flat line's fill) is kept in document coordinates, and every
|
||||
/// path of the series that takes the kept fill has it at that place in the document.
|
||||
#[test]
|
||||
fn a_gradient_placed_on_a_series_path_with_no_height_is_kept_in_document_coordinates() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "line");
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[3, 2], [3, 4], [3, 6]]})).unwrap();
|
||||
let flat = |s: &Session| {
|
||||
let parts = series_parts(s, id, 0);
|
||||
parts.into_iter().find(|m| m.appearance.fill_paint().is_none() && m.geometric_bounds().is_some_and(|b| b.height() < 1e-9)).unwrap().id
|
||||
};
|
||||
let line = flat(&s);
|
||||
s.execute("paint.setFill", &json!({"ids": [line.0], "gradient": {"kind": "linear"}})).unwrap();
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [line.0], "start": [150, 120], "end": [300, 280]})).unwrap();
|
||||
let placed = fill_of(&s, line);
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
assert!(!kept.fill_unit_box);
|
||||
assert_eq!(kept.fill, Some(placed.clone()));
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[3, 1], [3, 5], [3, 2]]})).unwrap();
|
||||
let fills: Vec<Paint> = series_parts(&s, id, 0).iter().map(|m| m.appearance.fill_paint()).filter(|p| matches!(p, Paint::Gradient(_))).collect();
|
||||
assert!(fills.len() >= 2 && fills.iter().all(|p| *p == placed), "{fills:?}");
|
||||
}
|
||||
|
||||
/// Writing a series mark's stroke weight as it is, after the graph drew the mark's stroke from a gradient the series
|
||||
/// keeps relative to the unit box, leaves the kept gradient, the document and its undo history as they are, on every mark.
|
||||
#[test]
|
||||
fn rewriting_a_series_marks_drawn_gradient_stroke_records_no_undo_step() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
s.execute("paint.setStroke", &json!({"ids": [marks[1].0], "gradient": {"kind": "radial"}})).unwrap();
|
||||
s.execute("stroke.set", &json!({"ids": [marks[1].0], "weight": 4})).unwrap();
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [marks[1].0], "stroke": true, "start": [150, 200], "end": [170, 260]})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1.3, 2], [3.7, 4], [6.1, 1]]})).unwrap();
|
||||
let was = s.doc().unwrap().doc.clone();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let drawn: Vec<_> = series_parts(&s, id, 0).into_iter().filter(|m| matches!(m.appearance.stroke_paint(), Paint::Gradient(_))).collect();
|
||||
assert!(drawn.len() >= 4, "{}", drawn.len());
|
||||
for mark in drawn {
|
||||
let row = top_row(&s, mark.id, false);
|
||||
s.execute("appearance.setItem", &json!({"ids": [mark.id.0], "index": row, "weight": mark.appearance.stroke_width()})).unwrap();
|
||||
assert!(s.doc().unwrap().doc == was, "writing mark {:?} changed the document", mark.id);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
}
|
||||
// A mark moved with its gradient shows the kept gradient on its moved box, up to rounding.
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
for (i, mark) in series_parts(&s, id, 0).into_iter().filter(|m| matches!(m.appearance.stroke_paint(), Paint::Gradient(_))).enumerate() {
|
||||
s.execute("select.set", &json!({"ids": [mark.id.0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 13.37 + i as f64 * 0.731, "dy": -7.19})).unwrap();
|
||||
let moved = s.doc().unwrap().doc.node(mark.id).unwrap().geometric_bounds().unwrap();
|
||||
assert!((moved.x0 - mark.geometric_bounds().unwrap().x0 - 13.37 - i as f64 * 0.731).abs() < 1e-9, "mark {:?} did not move", mark.id);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let row = top_row(&s, mark.id, false);
|
||||
s.execute("appearance.setItem", &json!({"ids": [mark.id.0], "index": row, "weight": mark.appearance.stroke_width()})).unwrap();
|
||||
assert_eq!(spec(&s, id).series_paints[0], kept, "writing moved mark {:?} changed the kept stroke", mark.id);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
}
|
||||
}
|
||||
|
||||
/// Writing the fill of a series mark with no height (a column for a value of 0) as it is, after the graph drew the mark's
|
||||
/// fill from a gradient the series keeps relative to the unit box, leaves the kept gradient and its unit-box flag as they
|
||||
/// are.
|
||||
#[test]
|
||||
fn rewriting_a_drawn_gradient_fill_on_a_series_mark_with_no_height_keeps_the_kept_gradient() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
s.execute("paint.setFill", &json!({"ids": [marks[0].0], "gradient": {"kind": "linear"}})).unwrap();
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [marks[0].0], "start": [0, 0], "end": [10, 40]})).unwrap();
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
assert!(kept.fill_unit_box);
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[0, 2], [3, 4], [6, 1]]})).unwrap();
|
||||
let flat = series_parts(&s, id, 0)
|
||||
.into_iter()
|
||||
.find(|m| matches!(m.appearance.fill_paint(), Paint::Gradient(_)) && m.geometric_bounds().is_some_and(|b| b.height() < 1e-9))
|
||||
.unwrap()
|
||||
.id;
|
||||
let changed = s.edit("Write the fill as it is", |d, _| Ok(super::capture_series_top_paints(d, &[(flat, true)]))).unwrap();
|
||||
assert!(!changed);
|
||||
assert_eq!(spec(&s, id).series_paints[0], kept);
|
||||
}
|
||||
|
||||
/// Writing a series mark's stroke weight as it is, after the graph drew the mark's stroke from a gradient the series keeps
|
||||
/// in document coordinates (a file saved before the unit box), leaves the kept gradient and its unit-box flag as they are
|
||||
/// and records no undo step.
|
||||
#[test]
|
||||
fn rewriting_a_series_marks_stroke_drawn_from_a_gradient_kept_in_document_coordinates_keeps_it() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
s.execute("paint.setStroke", &json!({"ids": [marks[1].0], "gradient": {"kind": "radial"}})).unwrap();
|
||||
s.execute("stroke.set", &json!({"ids": [marks[1].0], "weight": 2})).unwrap();
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [marks[1].0], "stroke": true, "start": [150, 200], "end": [170, 260]})).unwrap();
|
||||
let placed = s.doc().unwrap().doc.node(marks[1]).unwrap().appearance.stroke_paint();
|
||||
s.edit("Kept in document coordinates", |d, _| {
|
||||
let mut extras = d.node_mut(id).unwrap().extras_mut();
|
||||
let slot = &mut extras.graph.as_mut().unwrap().series_paints[0];
|
||||
(slot.stroke, slot.stroke_unit_box, slot.stroke_width) = (Some(placed.clone()), false, Some(2.0));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2], [3, 4], [6, 1]]})).unwrap();
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let drawn: Vec<_> = series_parts(&s, id, 0).into_iter().filter(|m| m.appearance.stroke_paint() == placed).collect();
|
||||
assert!(drawn.len() >= 3, "{}", drawn.len());
|
||||
for mark in drawn {
|
||||
let row = top_row(&s, mark.id, false);
|
||||
s.execute("appearance.setItem", &json!({"ids": [mark.id.0], "index": row, "weight": mark.appearance.stroke_width()})).unwrap();
|
||||
assert_eq!(spec(&s, id).series_paints[0], kept, "writing mark {:?} changed the kept stroke", mark.id);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
}
|
||||
}
|
||||
|
||||
/// A gradient placed on a line series' stroke is kept relative to the line's stroke box, and with Draw Filled Lines the
|
||||
/// band, which takes the kept stroke as its fill, has it at the same place on its own box.
|
||||
#[test]
|
||||
fn a_gradient_kept_for_a_lines_stroke_takes_the_same_place_on_its_filled_line() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "line");
|
||||
let line = series_parts(&s, id, 0).into_iter().find(|m| m.appearance.fill_paint().is_none()).unwrap().id;
|
||||
s.execute("paint.setStroke", &json!({"ids": [line.0], "gradient": {"kind": "linear"}})).unwrap();
|
||||
s.execute("paint.setGradientGeom", &json!({"ids": [line.0], "stroke": true, "start": [150, 120], "end": [300, 280]})).unwrap();
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
assert!(kept.stroke_unit_box && kept.fill.is_none());
|
||||
let place = gradient_place(kept.stroke.as_ref().unwrap(), vectorcraft_doc::GraphicStyle::UNIT_BOX);
|
||||
s.execute("graph.setType", &json!({"id": id.0, "filledLines": true})).unwrap();
|
||||
let band = series_parts(&s, id, 0).into_iter().find(|m| matches!(m.appearance.fill_paint(), Paint::Gradient(_))).unwrap();
|
||||
let at = gradient_place(&band.appearance.fill_paint(), band.geometric_bounds().unwrap());
|
||||
assert!(at.iter().zip(&place).all(|(a, b)| (a - b).abs() < 1e-9), "{at:?} is not {place:?}");
|
||||
}
|
||||
|
||||
/// A series' kept unit-box flags are read from and written to a file, and a file without them reads as document
|
||||
/// coordinates.
|
||||
#[test]
|
||||
fn a_series_paints_unit_box_flags_round_trip() {
|
||||
let kept = vectorcraft_doc::SeriesPaint { fill_unit_box: true, stroke_unit_box: true, ..Default::default() };
|
||||
let text = serde_json::to_string(&kept).unwrap();
|
||||
assert_eq!(text, r#"{"fillUnitBox":true,"strokeUnitBox":true}"#);
|
||||
assert_eq!(serde_json::from_str::<vectorcraft_doc::SeriesPaint>(&text).unwrap(), kept);
|
||||
let old: vectorcraft_doc::SeriesPaint = serde_json::from_str("{}").unwrap();
|
||||
assert!(!old.fill_unit_box && !old.stroke_unit_box);
|
||||
}
|
||||
|
||||
/// A series mark's fill recolored with Invert or Complement (the proxy, or one appearance row), an Edit Colors command,
|
||||
|
||||
@@ -21,7 +21,7 @@ use vectorcraft_color::{Color, GradientPaint, Paint};
|
||||
use vectorcraft_doc::{Appearance, Node, NodeId, TextStyleDef};
|
||||
|
||||
use super::clipboard::SwatchChoices;
|
||||
use super::{CommandSpec, EngineError, Result, Session, always, bad, cmd, color_value, has_doc, ids_param, index, ok, point_param, str_param};
|
||||
use super::{CommandSpec, EngineError, Result, Session, always, bad, cmd, color_value, has_doc, ids_param, index_opt, ok, point_param, str_param};
|
||||
use crate::Clipboard;
|
||||
|
||||
/// The extension of a library file.
|
||||
@@ -1649,11 +1649,11 @@ fn move_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "library.moveGroup";
|
||||
let id = library_id(s, p, C)?;
|
||||
let group = group_param(p, C)?;
|
||||
let to = p.get("to").and_then(Value::as_u64).ok_or_else(|| bad(C, "missing `to` (an index)"))?;
|
||||
let to = index_opt(p, "to", C)?.ok_or_else(|| bad(C, "missing `to` (an index)"))?;
|
||||
let (name, at) = s.libraries.change(&id, |lib| {
|
||||
let i = group_at(lib, group, C)?;
|
||||
let g = lib.groups.remove(i);
|
||||
let at = index(to).min(lib.groups.len());
|
||||
let at = to.min(lib.groups.len());
|
||||
let name = g.name.clone();
|
||||
lib.groups.insert(at, g);
|
||||
Ok((name, at))
|
||||
|
||||
@@ -67,7 +67,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Character / Paragraph",
|
||||
[],
|
||||
None,
|
||||
"{ids?|id?, kerning?: 1/1000 em|\"auto\"|\"optical\", baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg counter-clockwise (0: upright), underline?, strikethrough?, allCaps?, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), antiAlias?: \"none\"|\"sharp\"|\"crisp\"|\"strong\" (Character panel › Anti-aliasing Method, how the type's edges are drawn as pixels; it sets each targeted type object, with or without a range; each frame of a thread keeps its own method; graph labels keep it for every label of their kind when the graph is drawn again; new type: \"sharp\"), leftIndent?, rightIndent?, firstLineIndent?, spaceBefore?, spaceAfter?: pt (±1296), list?: \"none\"|\"bullet\"|\"number\", bullet?: \"none\"|\"dot\"|\"dash\"|\"circle\"|\"square\", number?: \"none\"|\"arabic\"|\"upperLetters\"|\"lowerLetters\"|\"upperRoman\"|\"lowerRoman\"|\"twoDigits\"|\"threeDigits\", listAlign?: \"left\"|\"right\", listLevel?: 1..9 (Paragraph panel › Bullets and Numbered List: each paragraph of a list starts with its marker, drawn in the font, size and color of the paragraph's first character, and its text starts at the next tab stop after the marker, the left indent counting as a stop; a bulleted list's marker is its bullet, and a numbered list's is the paragraph's number and a period: 1, 2, 3; A to Z, then AA, BB; a to z, then aa, bb; I, II, III; i, ii, iii; 01 to 99, then 100; 001 to 999, then 1000; letters and roman numerals past 3999 in Arabic digits; the numbers count the story's numbered paragraphs, its threaded frames included, from 1 after a paragraph in no list, and an empty or bulleted paragraph is not counted; list \"bullet\" keeps a list's bullet, else a dot, and list \"number\" keeps a numbered list's style, else arabic; without list, a number style makes a numbered list and a bullet makes a bulleted one; \"none\" in any of the three turns the list off; a paragraph that joins a list with left and first-line indents of 0 gets 18 and -18 unless the call sets an indent; a horizontal right-to-left list paragraph has its indents mirrored, measured from the right; listAlign \"right\" (Bullets and Numbering…'s Alignment) puts the marker against the text, and \"left\" (the default) puts it at the first line's start, the text starting at the same place with either; it sets the paragraphs that are in a list or that the call puts in one, and a list keeps it when it changes between bullets and numbers; listLevel (Tab and Shift+Tab at the start of a list paragraph, Bullets and Numbering…'s Level) is a whole number from 1 to 9 that sets the paragraphs in a list or that the call puts in one, a list keeping it when it changes between bullets and numbers, and a list turned on without listLevel starting at level 1; each level past the first sets the paragraph 18 pt further in than its left indent, and each level counts its own numbers: a list paragraph with text sets the counts of the levels deeper than its own back to 0, so a deeper numbered paragraph after it counts from 1, and a paragraph back at a shallower level goes on with that level's count), hyphenate?: bool, wordsLongerThan?: letters 1..25 (5), afterFirst?, beforeLast?: letters 1..15 (3), hyphenLimit?: lines 0..25 (0: no limit), hyphenationZone?: pt 0..1296 (0; Single-line Composer only), hyphenateCapitalized?: bool (true) (Paragraph panel menu › Hyphenation…: only words longer than wordsLongerThan break, keeping afterFirst letters before the hyphen and beforeLast after it; at most hyphenLimit lines in a row end with a hyphen; a line whose text already ends within hyphenationZone of its right edge breaks before the next word instead of hyphenating it; the counts are rounded, and every value is clamped to its range), wordSpacing?: % 0..1000 (80, 100, 133), letterSpacing?: % of a word space -100..500 (0, 0, 0), glyphScaling?: % 50..200 (100, 100, 100): each [min, desired, max] or {min?, desired?, max?} (Paragraph panel menu › Justification…: Desired applies to every line, Minimum and Maximum bound what justified lines may use; values are clamped to their range, and Minimum ≤ Desired ≤ Maximum must hold afterwards or nothing changes), autoLeading?: % of the size 0..500 (120; the leading of characters whose leading is Auto), singleWordJustification?: \"fullJustify\"|\"alignLeft\"|\"alignCenter\"|\"alignRight\" (a justified line holding one word), mojikumi?: \"none\"|\"yakumonoHankaku\"|\"gyomatsuYakumonoHankaku\"|\"gyomatsuYakumonoZenkaku\"|\"yakumonoZenkaku\"|\"lineEndHalf\" (Mojikumi Set: Japanese punctuation spacing), kinsoku?: \"none\"|\"hard\"|\"soft\" (Kinsoku Set: Soft lets 々, ー and small kana start a line), kinsokuType?: \"pushInFirst\"|\"pushOutFirst\"|\"pushOutOnly\" (Paragraph panel menu › Kinsoku Shori Type: how area type keeps those characters off a line's ends; pushInFirst sets them on the line when its gaps next to CJK characters can close up enough, by at most a tent
Line truncated
|
||||
"{ids?|id?, kerning?: 1/1000 em|\"auto\"|\"optical\", baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg counter-clockwise (0: upright), underline?, strikethrough?, allCaps?, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), antiAlias?: \"none\"|\"sharp\"|\"crisp\"|\"strong\" (Character panel › Anti-aliasing Method, how the type's edges are drawn as pixels; it sets each targeted type object, with or without a range; each frame of a thread keeps its own method; graph labels keep it for every label of their kind when the graph is drawn again; new type: \"sharp\"), leftIndent?, rightIndent?, firstLineIndent?, spaceBefore?, spaceAfter?: pt (±1296), list?: \"none\"|\"bullet\"|\"number\", bullet?: \"none\"|\"dot\"|\"dash\"|\"circle\"|\"square\", number?: \"none\"|\"arabic\"|\"upperLetters\"|\"lowerLetters\"|\"upperRoman\"|\"lowerRoman\"|\"twoDigits\"|\"threeDigits\", listAlign?: \"left\"|\"right\", listLevel?: 1..9 (Paragraph panel › Bullets and Numbered List: each paragraph of a list starts with its marker, drawn in the font, size and color of the paragraph's first character, and its text starts at the next tab stop after the marker, the left indent counting as a stop; a bulleted list's marker is its bullet, and a numbered list's is the paragraph's number and a period: 1, 2, 3; A to Z, then AA, BB; a to z, then aa, bb; I, II, III; i, ii, iii; 01 to 99, then 100; 001 to 999, then 1000; letters and roman numerals past 3999 in Arabic digits; the numbers count the story's numbered paragraphs, its threaded frames included, from 1 after a paragraph in no list, and an empty or bulleted paragraph is not counted; list \"bullet\" keeps a list's bullet, else a dot, and list \"number\" keeps a numbered list's style, else arabic; without list, a number style makes a numbered list and a bullet makes a bulleted one; \"none\" in any of the three turns the list off; a paragraph that joins a list with left and first-line indents of 0 gets 18 and -18 unless the call sets an indent; a horizontal right-to-left list paragraph has its indents mirrored, measured from the right; listAlign \"right\" (Bullets and Numbering…'s Alignment) puts the marker against the text, and \"left\" (the default) puts it at the first line's start, the text starting at the same place with either; it sets the paragraphs that are in a list or that the call puts in one, and a list keeps it when it changes between bullets and numbers; listLevel (Tab and Shift+Tab at the start of a list paragraph, Bullets and Numbering…'s Level) is a whole number from 1 to 9 that sets the paragraphs in a list or that the call puts in one, a list keeping it when it changes between bullets and numbers, and a list turned on without listLevel starting at level 1; each level past the first sets the paragraph 18 pt further in than its left indent, and each level counts its own numbers: a list paragraph with text sets the counts of the levels deeper than its own back to 0, so a deeper numbered paragraph after it counts from 1, and a paragraph back at a shallower level goes on with that level's count), hyphenate?: bool, wordsLongerThan?: letters 1..25 (5), afterFirst?, beforeLast?: letters 1..15 (3), hyphenLimit?: lines 0..25 (0: no limit), hyphenationZone?: pt 0..1296 (0; Single-line Composer only), hyphenateCapitalized?: bool (true) (Paragraph panel menu › Hyphenation…: only words longer than wordsLongerThan break, keeping afterFirst letters before the hyphen and beforeLast after it; at most hyphenLimit lines in a row end with a hyphen; a line whose text already ends within hyphenationZone of its right edge breaks before the next word instead of hyphenating it; the counts are rounded, and every value is clamped to its range), wordSpacing?: % 0..1000 (80, 100, 133), letterSpacing?: % of a word space -100..500 (0, 0, 0), glyphScaling?: % 50..200 (100, 100, 100): each [min, desired, max] or {min?, desired?, max?} (Paragraph panel menu › Justification…: Desired applies to every line, Minimum and Maximum bound what justified lines may use; values are clamped to their range, and Minimum ≤ Desired ≤ Maximum must hold afterwards or nothing changes), autoLeading?: % of the size 0..500 (120; the leading of characters whose leading is Auto), singleWordJustification?: \"fullJustify\"|\"alignLeft\"|\"alignCenter\"|\"alignRight\" (a justified line holding one word), mojikumi?: \"none\"|\"yakumonoHankaku\"|\"gyomatsuYakumonoHankaku\"|\"gyomatsuYakumonoZenkaku\"|\"yakumonoZenkaku\"|\"lineEndHalf\" (Mojikumi Set: Japanese punctuation spacing), kinsoku?: \"none\"|\"hard\"|\"soft\" (Kinsoku Set: Soft lets 々, ー and small kana start a line), kinsokuType?: \"pushInFirst\"|\"pushOutFirst\"|\"pushOutOnly\" (Paragraph panel menu › Kinsoku Shori Type: how area type keeps those characters off a line's ends; pushInFirst sets them on the line when its gaps next to CJK characters can close up enough, by at most a tent
Line truncated
|
||||
has_doc,
|
||||
set_format
|
||||
),
|
||||
@@ -88,7 +88,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
fn artboard_reorder(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "artboard.reorder";
|
||||
let i = artboard_index(&s.doc()?.doc, p, C)?;
|
||||
let to = p.get("to").and_then(Value::as_u64).map(index).ok_or_else(|| bad(C, "missing to"))?;
|
||||
let to = super::index_opt(p, "to", C)?.ok_or_else(|| bad(C, "missing to"))?;
|
||||
s.edit("Reorder Artboards", |d, _| {
|
||||
if i >= d.artboards.len() {
|
||||
return Err(EngineError::Other("no such artboard".into()));
|
||||
@@ -588,6 +588,7 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
"warichuAlign",
|
||||
"akiLeft",
|
||||
"akiRight",
|
||||
"tsume",
|
||||
"standardVerticalRoman",
|
||||
"noBreak",
|
||||
"digits",
|
||||
@@ -632,6 +633,7 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let tcy_left_right = super::textedit::tcy_offset_param(p, "tcyLeftRight", C)?;
|
||||
let aki_left = super::textedit::aki_param(p, "akiLeft", C)?;
|
||||
let aki_right = super::textedit::aki_param(p, "akiRight", C)?;
|
||||
let tsume = super::textedit::tsume_param(p, C)?;
|
||||
let warichu_lines = super::textedit::warichu_lines_param(p, C)?;
|
||||
let warichu_gap = super::textedit::warichu_number_param(p, "warichuGap", C)?;
|
||||
let warichu_scale = super::textedit::warichu_number_param(p, "warichuScale", C)?;
|
||||
@@ -816,6 +818,9 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(v) = aki_right {
|
||||
st.aki_right = v;
|
||||
}
|
||||
if let Some(v) = tsume {
|
||||
st.tsume = v;
|
||||
}
|
||||
if let Some(v) = flag("standardVerticalRoman") {
|
||||
st.standard_vertical_roman = v;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use super::draw2::usize_opt;
|
||||
use super::pathops::Beside;
|
||||
use super::{
|
||||
CommandSpec, Result, Session, bad, check_numbers, check_switches, cmd, f64_or, f64_req, finite_numbers, has_anchors, has_doc, has_selected_path,
|
||||
has_selection, ids_param, index, number_or, object_param, ok, str_param, switch_or, targets,
|
||||
has_selection, ids_param, index_opt, number_or, object_param, ok, str_param, switch_or, targets,
|
||||
};
|
||||
use crate::EngineError;
|
||||
|
||||
@@ -170,7 +170,7 @@ pub(super) fn path_mut(d: &mut vectorcraft_doc::Document, id: NodeId) -> Result<
|
||||
fn append_anchor(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = object_param(p, "id", "path.appendAnchor")?.ok_or_else(|| bad("path.appendAnchor", "missing id"))?;
|
||||
let a = anchor_from_json(p, "path.appendAnchor")?;
|
||||
let subpath = p.get("subpath").and_then(Value::as_u64).map(|u| u as usize);
|
||||
let subpath = index_opt(p, "subpath", "path.appendAnchor")?;
|
||||
s.edit("Pen", |d, sel| {
|
||||
let path = path_mut(d, id)?;
|
||||
let si = match subpath {
|
||||
@@ -208,7 +208,7 @@ fn append_anchor(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// `at` first moves the anchor, its handles with it (the Pen's closing drag with Space held).
|
||||
fn close(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = object_param(p, "id", "path.close")?.ok_or_else(|| bad("path.close", "missing id"))?;
|
||||
let subpath = p.get("subpath").and_then(Value::as_u64).map(|u| u as usize);
|
||||
let subpath = index_opt(p, "subpath", "path.close")?;
|
||||
let handle = |key: &str| match p.get(key).filter(|h| !h.is_null()) {
|
||||
None => Ok(None),
|
||||
Some(h) => finite_numbers(h).map(|[x, y]| Some(Point::new(x, y))).ok_or_else(|| bad("path.close", format!("`{key}` must be [x, y]"))),
|
||||
@@ -310,7 +310,7 @@ pub(crate) fn transform_anchors(s: &mut Session, label: &str, xf: vectorcraft_ge
|
||||
fn set_handle(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = object_param(p, "id", "path.setHandle")?.ok_or_else(|| bad("path.setHandle", "missing id"))?;
|
||||
let si = usize_opt(p, "subpath", "path.setHandle")?.unwrap_or(0);
|
||||
let ai = p.get("anchor").and_then(Value::as_u64).map(index).ok_or_else(|| bad("path.setHandle", "missing anchor"))?;
|
||||
let ai = index_opt(p, "anchor", "path.setHandle")?.ok_or_else(|| bad("path.setHandle", "missing anchor"))?;
|
||||
let out = str_param(p, "which") != Some("in");
|
||||
let pos = Point::new(f64_req(p, "x", "path.setHandle")?, f64_req(p, "y", "path.setHandle")?);
|
||||
let independent = switch_or(p, "independent", false, "path.setHandle")?;
|
||||
@@ -337,7 +337,7 @@ fn set_handle(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn move_anchor(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = object_param(p, "id", "path.moveAnchor")?.ok_or_else(|| bad("path.moveAnchor", "missing id"))?;
|
||||
let si = usize_opt(p, "subpath", "path.moveAnchor")?.unwrap_or(0);
|
||||
let ai = p.get("anchor").and_then(Value::as_u64).map(index).ok_or_else(|| bad("path.moveAnchor", "missing anchor"))?;
|
||||
let ai = index_opt(p, "anchor", "path.moveAnchor")?.ok_or_else(|| bad("path.moveAnchor", "missing anchor"))?;
|
||||
let pos = Point::new(f64_req(p, "x", "path.moveAnchor")?, f64_req(p, "y", "path.moveAnchor")?);
|
||||
// An anchor already at (x, y) changes nothing and records no undo step.
|
||||
if let Some(a) = anchor_of(s, id, si, ai)?
|
||||
@@ -453,7 +453,7 @@ pub(super) fn paths_in(d: &vectorcraft_doc::Document, ids: &[NodeId]) -> Vec<Nod
|
||||
fn reverse(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "path.reverse";
|
||||
let want = p.get("reversed").map(|v| v.as_bool().ok_or_else(|| bad(C, "`reversed` must be true or false"))).transpose()?;
|
||||
let subpath = p.get("subpath").and_then(Value::as_u64).map(|u| u as usize);
|
||||
let subpath = index_opt(p, "subpath", C)?;
|
||||
let ids = paths_in(&s.doc()?.doc, &targets(s, p)?);
|
||||
if ids.is_empty() {
|
||||
return Err(bad(C, "select paths"));
|
||||
@@ -1784,7 +1784,7 @@ fn reshape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn insert_anchor(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = object_param(p, "id", "path.insertAnchor")?.ok_or_else(|| bad("path.insertAnchor", "missing id"))?;
|
||||
let si = usize_opt(p, "subpath", "path.insertAnchor")?.unwrap_or(0);
|
||||
let seg = p.get("segment").and_then(Value::as_u64).map(index).ok_or_else(|| bad("path.insertAnchor", "missing segment"))?;
|
||||
let seg = index_opt(p, "segment", "path.insertAnchor")?.ok_or_else(|| bad("path.insertAnchor", "missing segment"))?;
|
||||
let t = number_or(p, "t", 0.5, "path.insertAnchor")?.clamp(0.0, 1.0);
|
||||
let idx = s.edit("Add Anchor Point", |d, sel| {
|
||||
let path = path_mut(d, id)?;
|
||||
|
||||
@@ -32,7 +32,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Run Script",
|
||||
[],
|
||||
None,
|
||||
"{path?: a .js or .jsx file | source?: the script's text (exactly one of the two), name?: the name errors and the report use (default: the script's #script line, else the path's file name, or \"script\"), args?: {…} (at most 1 MiB as JSON; vc.args in the script), answers?: [{kind: alert|confirm|prompt, value: \"ok\" (alert) | true or false (confirm) | text or null (prompt)}] (for a run with nobody to ask: the answers to its dialogs, in order; at most 10000 and 1 MiB as JSON; refused in the app, where the person answers; an empty object is an empty list), maxFuel?: whole number from 1 to 100000000000 (instructions in all; default 1000000000)} run a JavaScript file against the open documents, through the vc object: vc.run(id, params) runs a command and returns its result, a failed command throws an error the script can catch and puts back the documents and the session state it changed, while any file it wrote stays written and is named; vc.document is a snapshot of the active document or null; vc.commands lists the commands a script may run; vc.log(value) adds to the report and the Script Console (script.console); $.write(...values) writes the values to the Script Console as String(value) gives them, and $.writeln(...values) does the same and ends the line; $.evalFile(path, timeout?) runs a file with the same globals and returns its last value (timeout, in milliseconds from 0, is taken and not waited on), $.fileName is the file being run (empty for text), $.includePath the #includepath list and $.engineName the engine's name (VectorCraft without #targetengine); alert(message[, title, errorIcon]), confirm(message[, noAsDflt, title]) and prompt(message, preset[, title]) open a dialog with a Stop button (default title \"Script Alert\"; message and preset as String(value) gives them; errorIcon and noAsDflt as Boolean(value) gives them; No is the default with noAsDflt): alert returns undefined, confirm true for Yes and false for No, prompt the field's text for OK and null for Cancel; in the app the run waits at the call for the person's answer and Stop ends it as script.stop does, a dialog cannot open inside a callback of a built-in, a getter, a conversion or a file $.evalFile runs and throws there (an #include file's code is the script's own and can), and a run with nobody to ask takes the next of answers, and ends when none is left or the next is for another kind of dialog, or when the script ends with answers unused; vc.args is args. Before anything runs, the script's preprocessor lines (# or //@ at the start of a line, outside strings and comments) are read: #include \"file\" and //@include put that file's text in place of the line, looked for beside the file that includes it, then in each #includepath folder in turn (separated by ;, relative to that file's folder; a later #includepath replaces the list), then in each JSINCLUDE folder; #script names the run; #target takes any value and changes nothing; #strict on makes an assignment to a read-only or getter-only property, or a new property on an object that is not extensible (preventExtensions, seal, freeze, vc, $), throw a TypeError, without the rest of ES5 strict mode, and #strict off turns that off (the last #strict line of the script and its includes sets the run; off without one; a $.evalFile file's own line holds for its code); #targetengine name runs the script in the named engine, whose globals and functions stay for the next run that names it until script.engine.drop drops it or the session ends (script.engines lists them; a name of letters, digits and _ not starting with a digit, or a quoted one of 1 to 64 bytes without control characters, matched as written; only in the script itself, once, before its code and #include lines; main, and a script without the line, get a new engine each run; a session keeps 8 named engines, a ninth name is refused, each has 64 MiB of heap, and an uncaught error, a limit or script.stop keeps the globals the run set while the documents go back; host functions kept in a global act for the run that calls them; an error in code an earlier run compiled is at that run's file, line and column, within 8 MiB of source maps kept for each engine, the oldest going first; not on the web); #engine is refused as unsupported, any other #name as unknown, and another //@name is a comment. A script run from a file reads its includes and $.evalFile's files only in that file's folder and the folders below it, refusing a path with a .. name or a link that leads out (a JSINCLUDE folder outside is passed over); a script given as text reads none. File(path) and Folder(path) name files and folders and read them in the same folders, and inside the automation read roots when they are set: exists, length, created, modified, getFiles(mask or function; at most 2048 entries), resolve, and open('r') with read, readln, readch, seek, tell and close; a path outside, a network or device path and a link that leads out give false or null with
Line truncated
|
||||
"{path?: a .js or .jsx file | source?: the script's text (exactly one of the two), name?: the name errors and the report use (default: the script's #script line, else the path's file name, or \"script\"), args?: {…} (at most 1 MiB as JSON; vc.args in the script), answers?: [{kind: alert|confirm|prompt, value: \"ok\" (alert) | true or false (confirm) | text or null (prompt)}] (for a run with nobody to ask: the answers to its dialogs, in order; at most 10000 and 1 MiB as JSON; refused in the app, where the person answers; an empty object is an empty list), maxFuel?: whole number from 1 to 100000000000 (instructions in all; default 1000000000)} run a JavaScript file against the open documents, through the vc object: vc.run(id, params) runs a command and returns its result, a failed command throws an error the script can catch and puts back the documents and the session state it changed, while any file it wrote stays written and is named; vc.document is a snapshot of the active document or null; vc.commands lists the commands a script may run; vc.log(value) adds to the report and the Script Console (script.console); $.write(...values) writes the values to the Script Console as String(value) gives them, and $.writeln(...values) does the same and ends the line; $.evalFile(path, timeout?) runs a file with the same globals and returns its last value (timeout, in milliseconds from 0, is taken and not waited on), $.fileName is the file of the code that reads it (the file being run for code that eval or Function compiled; empty for code compiled from a script given as text), $.includePath the #includepath list and $.engineName the engine's name (VectorCraft without #targetengine); alert(message[, title, errorIcon]), confirm(message[, noAsDflt, title]) and prompt(message, preset[, title]) open a dialog with a Stop button (default title \"Script Alert\"; message and preset as String(value) gives them; errorIcon and noAsDflt as Boolean(value) gives them; No is the default with noAsDflt): alert returns undefined, confirm true for Yes and false for No, prompt the field's text for OK and null for Cancel; in the app the run waits at the call for the person's answer and Stop ends it as script.stop does, a dialog cannot open inside a callback of a built-in, a getter, a conversion or a file $.evalFile runs and throws there (an #include file's code is the script's own and can), and a run with nobody to ask takes the next of answers, and ends when none is left or the next is for another kind of dialog, or when the script ends with answers unused; vc.args is args. Before anything runs, the script's preprocessor lines (# or //@ at the start of a line, outside strings and comments) are read: #include \"file\" and //@include put that file's text in place of the line, looked for beside the file that includes it, then in each #includepath folder in turn (separated by ;, relative to that file's folder; a later #includepath replaces the list), then in each JSINCLUDE folder; #script names the run; #target takes any value and changes nothing; #strict on makes an assignment to a read-only or getter-only property, or a new property on an object that is not extensible (preventExtensions, seal, freeze, vc, $), throw a TypeError, without the rest of ES5 strict mode, and #strict off turns that off (the last #strict line of the script and its includes sets the run; off without one; a $.evalFile file's own line holds for its code); #targetengine name runs the script in the named engine, whose globals and functions stay for the next run that names it until script.engine.drop drops it or the session ends (script.engines lists them; a name of letters, digits and _ not starting with a digit, or a quoted one of 1 to 64 bytes without control characters, matched as written; only in the script itself, once, before its code and #include lines; main, and a script without the line, get a new engine each run; a session keeps 8 named engines, a ninth name is refused, each has 64 MiB of heap, and an uncaught error, a limit or script.stop keeps the globals the run set while the documents go back; host functions kept in a global act for the run that calls them; an error in code an earlier run compiled is at that run's file, line and column, within 8 MiB of source maps kept for each engine, the oldest going first; not on the web); #engine is refused as unsupported, any other #name as unknown, and another //@name is a comment. A script run from a file reads its includes and $.evalFile's files only in that file's folder and the folders below it, refusing a path with a .. name or a link that leads out (a JSINCLUDE folder outside is passed over); a script given as text reads none. File(path) and Folder(path) name files and folders and read them in the same folders, and inside the automation read roots when they are set: exists, length, created, modified, getFiles(mask or function; at most 2048 entries), resolve, and open('r') with read, readln, readch,
Line truncated
|
||||
always,
|
||||
run
|
||||
),
|
||||
|
||||
@@ -21,7 +21,7 @@ use vectorcraft_color::Paint;
|
||||
use vectorcraft_doc::{Appearance, DEFAULT_GRAPHIC_STYLE, Document, GraphicStyle, Node, NodeId, NodeKind, Shared};
|
||||
use vectorcraft_geom::Rect;
|
||||
|
||||
use super::{CommandSpec, Result, Session, always, bad, bool_or, cmd, has_doc, index, object_param, ok, str_param, unique_name};
|
||||
use super::{CommandSpec, Result, Session, always, bad, bool_or, cmd, has_doc, index_opt, object_param, ok, str_param, unique_name};
|
||||
use crate::EngineError;
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
@@ -40,7 +40,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"New Graphic Style",
|
||||
["Window", "Graphic Styles"],
|
||||
None,
|
||||
"{name?, id?} a style from object `id` or the first selected object: its appearance, opacity, blend mode, isolate and knockout (a group without fills or strokes of its own lends its topmost object's, type its characters'; a graph series group with no fill, stroke, effect, opacity, blend mode, isolation, knockout or style link of its own stands for the topmost path directly in it; placed gradients are kept relative to its bounds); links the object. Names are made unique → {name}",
|
||||
"{name?, id?} a style from object `id` or the first selected object: its appearance, opacity, blend mode, isolate and knockout (a group without fills or strokes of its own lends its topmost object's, type its characters'; a graph series group with no fill, stroke, effect, opacity, blend mode, isolation, knockout or style link of its own stands for the topmost path directly in it; placed gradients are kept relative to its bounds); links the object, unless it is the art of a graph design in a series. Names are made unique → {name}",
|
||||
has_doc,
|
||||
style_new
|
||||
),
|
||||
@@ -68,7 +68,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Redefine Graphic Style",
|
||||
["Window", "Appearance"],
|
||||
None,
|
||||
"{name?, id?} replace the style (default: the one the object was last linked to) with the look of object `id` or the first selected object (a graph series group with no fill, stroke, effect, opacity, blend mode, isolation, knockout or style link of its own: the topmost path directly in it); the objects still linked to it update, and that object becomes linked. A linked graph series group passes the style to the paths directly in it, other than the source and paths linked to the style whose look was edited, and is left cleared (as appearance.clear) and unlinked. On a linked path directly in a graph series, the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke and the object's opacity and blend mode (exceptions: docs/mcp.md); on a graph label, the graph stores the label's topmost visible fill and stroke rows and object opacity and blend mode for every label of its kind → {name}",
|
||||
"{name?, id?} replace the style (default: the one the object was last linked to) with the look of object `id` or the first selected object (a graph series group with no fill, stroke, effect, opacity, blend mode, isolation, knockout or style link of its own: the topmost path directly in it); the objects still linked to it update, and that object becomes linked. The art of a graph design in a series keeps its look and is unlinked, as the source or as a linked object. A linked graph series group passes the style to the paths directly in it, other than the source and paths linked to the style whose look was edited, and is left cleared (as appearance.clear) and unlinked. On a linked path directly in a graph series, the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke and the object's opacity and blend mode (exceptions: docs/mcp.md); on a graph label, the graph stores the label's topmost visible fill and stroke rows and object opacity and blend mode for every label of its kind → {name}",
|
||||
has_doc,
|
||||
style_redefine
|
||||
),
|
||||
@@ -392,8 +392,10 @@ fn style_new(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
None => d.new_graphic_style_name(),
|
||||
};
|
||||
let g = GraphicStyle { id: d.next_graphic_style_id(), ..GraphicStyle::of(name.clone(), captured(n), n) };
|
||||
// The art of a graph design in a series is not linked: the graph draws it again from the design.
|
||||
let link = (!super::graph::is_design_art(d, src)).then_some(g.id);
|
||||
s.edit("New Graphic Style", |d, _| {
|
||||
set_link(d, &[src], Some(g.id));
|
||||
set_link(d, &[src], link);
|
||||
d.graphic_styles.push(g);
|
||||
Ok(())
|
||||
})?;
|
||||
@@ -491,10 +493,12 @@ fn style_redefine(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
s.edit_named("Redefine Graphic Style “{name}”", &name, false, |d, _| {
|
||||
let id = d.graphic_style_id(i);
|
||||
let old = std::mem::replace(&mut d.graphic_styles[i], GraphicStyle { name: name.clone(), id, ..look });
|
||||
// Objects still linked follow the new definition; edited ones are unlinked (they keep
|
||||
// their look), and the source keeps its own.
|
||||
// Objects still linked follow the new definition; edited ones are unlinked (they keep their look), and the
|
||||
// source keeps its own. The art of a graph design in a series is unlinked and keeps its look too, as the
|
||||
// source or as a linked object: the graph draws it again from the design.
|
||||
let linked = objects_where(d, |n| n.extras().graphic_style == Some(id) && n.id != src);
|
||||
let (follow, edited): (Vec<NodeId>, Vec<NodeId>) = linked.into_iter().partition(|nid| d.node(*nid).is_some_and(|n| in_sync(n, &old)));
|
||||
let (follow, edited): (Vec<NodeId>, Vec<NodeId>) =
|
||||
linked.into_iter().partition(|nid| !super::graph::is_design_art(d, *nid) && d.node(*nid).is_some_and(|n| in_sync(n, &old)));
|
||||
set_link(d, &edited, None);
|
||||
// A linked graph series group passes the style to the paths directly inside it, so the graph stores it, and
|
||||
// goes back to the empty appearance the graph draws it with. The source and the edited paths keep their look.
|
||||
@@ -509,7 +513,8 @@ fn style_redefine(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
super::appearance::capture_series_writes(&before, d, &follow_marks, (true, true), (true, true), false, true);
|
||||
super::graph::capture_label_rows(d, &follow_marks);
|
||||
set_link(d, &[src], Some(id));
|
||||
let link = (!super::graph::is_design_art(d, src)).then_some(id);
|
||||
set_link(d, &[src], link);
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(json!({ "name": name }))
|
||||
@@ -585,10 +590,10 @@ fn style_merge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn style_move(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "graphicStyle.move";
|
||||
let name = str_param(p, "name").ok_or_else(|| bad(cmd, "missing name"))?;
|
||||
let to = p.get("to").and_then(Value::as_u64).ok_or_else(|| bad(cmd, "missing `to` (an index)"))?;
|
||||
let to = index_opt(p, "to", cmd)?.ok_or_else(|| bad(cmd, "missing `to` (an index)"))?;
|
||||
let d = &s.doc()?.doc;
|
||||
let from = style_index(d, name)?;
|
||||
let to = index(to).min(d.graphic_styles.len() - 1);
|
||||
let to = to.min(d.graphic_styles.len() - 1);
|
||||
if from == to {
|
||||
return ok();
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Character",
|
||||
[],
|
||||
None,
|
||||
"{id? (default: the one selected type object), start?: byte, end?: byte (default: all text), story?: bool = false (all the text of every frame of the threaded story `id` is a frame of, in one undo step; no `start` or `end`), font?, style?, size?: pt 0.1..1296 (outside it: an error), leading?: pt|\"auto\", tracking?, kerning?: 1/1000 em|\"auto\"|\"optical\" (the space after each character), kerningBefore?: 1/1000 em -1000..10000 (the space before a character that starts a line by itself: the first character of the text, of a paragraph or after a forced line break; no effect elsewhere; 0 by default), baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg counter-clockwise (0: upright), fill?: colour|\"none\", stroke?: colour|\"none\" (#rrggbbaa and #rgba are refused in both), keepModel?: bool (fill and stroke, as paint.setFill), strokeWidth?: pt, strokeOptions?: {weight?, cap?, join?, miterLimit?, align?, dash?, dashOffset?, alignDashes?} (as stroke.set: the character stroke), underline?, strikethrough?, allCaps?: bool, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), features?: [\"dlig\", \"-liga\", \"salt=2\", …] (swsh, ss01–ss20, salt, titl, pnum, lnum, sups, subs, numr, dnom and aalt take the font's alternate number as `tag=N`, N 0–65535: \"salt=2\" draws the second alternate of each glyph that has one, \"salt\" is \"salt=1\", \"-salt\" and \"salt=0\" are off; the range is shaped with its word, so a letter's alternate keeps it joined to its neighbours), featureChanges?: [\"tnum\", \"-onum\", \"salt=2\", …] (OpenType features turned on, or with - off, on top of each character's own, a value replacing the feature's last one; its other features are kept), charAlign?: \"romanBaseline\"|\"emBoxTop\"|\"emBoxCenter\"|\"emBoxBottom\"|\"icfTop\"|\"icfBottom\", proportionalMetrics?: bool (full-width glyphs on the font's proportional widths: `palt` in horizontal type, `vpal` in vertical type), tateChuYoko?: bool (Character panel menu › Tate-chu-yoko, with Show East Asian Options: in vertical type those characters stand side by side across one em of the column; kept on the characters and ignored in horizontal type), tcyUpDown?, tcyLeftRight?: pt -1296..1296 (Character panel menu › Tate-chu-yoko Settings: in vertical type a block of tate-chu-yoko characters moves up by a positive Up/Down and down by a negative one, right by a positive Left/Right and left by a negative one, as one piece with the values of its first character; on a vertical path a positive Up/Down moves it toward the path's start and a positive Left/Right to the path's left; 0 by default; automatic digit blocks and horizontal type do not move; neither turns tateChuYoko on), warichu?: bool (Character panel menu › Warichu, with Show East Asian Options: the characters are set at warichuScale percent of their size and stacked on warichuLines lines inside their line, one above the other in horizontal type and side by side in vertical type, the first line above or to the right; a stretch of them is one block with the settings of its first character that stays whole on one line and may run past its end; type on a path sets them at their own size), warichuLines?: 1..6 (default 2), warichuGap?: pt 0..100 (the space between the lines, default 0), warichuScale?: % 10..100 (default 50), warichuAlign?: \"auto\"|\"left\"|\"center\"|\"right\"|\"justifyLeft\"|\"justifyCenter\"|\"justifyRight\"|\"justifyAll\" (Warichu Settings: how the lines line up along the block, which is as long as its longest line; the justify values spread every line but the last, which is set left, centered or right, or spread with justifyAll; auto, the default, sets them as justifyLeft; left and right are top and bottom in vertical type; none of them turns warichu on), akiLeft?, akiRight?: 1/1000 em 0..1000 (Character panel › Insert Aki Left and Right, with Show East Asian Options: space before and after each character in reading order, on its left and right in left-to-right type, on its right and left in right-to-left type, above and below it in vertical type; a ligature or a letter with its marks takes the values of its first character; line wrapping counts it, alignment keeps it at a line's ends and justification does not stretch or narrow it; 0 by default; a value that is not a number or is outside the range is refused, naming the key), standardVerticalRoman?: bool (Character panel menu › Standard Vertical Roman Alignment, with Show East Asian Options: in vertical type those half-width characters, such as Latin letters and digits, stand upright one below the other instead of being rotated; spaces and tate-chu-yoko blocks are not changed; kept on the characters and ignored in horizontal type; off by default), noBreak?: bool (Character panel menu › No Break: these characters stay on one line, including across a space inside the run), kashidas?: bool (Character panel menu › Kashidas: selected characters all
Line truncated
|
||||
"{id? (default: the one selected type object), start?: byte, end?: byte (default: all text), story?: bool = false (all the text of every frame of the threaded story `id` is a frame of, in one undo step; no `start` or `end`), font?, style?, size?: pt 0.1..1296 (outside it: an error), leading?: pt|\"auto\", tracking?, kerning?: 1/1000 em|\"auto\"|\"optical\" (the space after each character), kerningBefore?: 1/1000 em -1000..10000 (the space before a character that starts a line by itself: the first character of the text, of a paragraph or after a forced line break; no effect elsewhere; 0 by default), baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg counter-clockwise (0: upright), fill?: colour|\"none\", stroke?: colour|\"none\" (#rrggbbaa and #rgba are refused in both), keepModel?: bool (fill and stroke, as paint.setFill), strokeWidth?: pt, strokeOptions?: {weight?, cap?, join?, miterLimit?, align?, dash?, dashOffset?, alignDashes?} (as stroke.set: the character stroke), underline?, strikethrough?, allCaps?: bool, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), features?: [\"dlig\", \"-liga\", \"salt=2\", …] (swsh, ss01–ss20, salt, titl, pnum, lnum, sups, subs, numr, dnom and aalt take the font's alternate number as `tag=N`, N 0–65535: \"salt=2\" draws the second alternate of each glyph that has one, \"salt\" is \"salt=1\", \"-salt\" and \"salt=0\" are off; the range is shaped with its word, so a letter's alternate keeps it joined to its neighbours), featureChanges?: [\"tnum\", \"-onum\", \"salt=2\", …] (OpenType features turned on, or with - off, on top of each character's own, a value replacing the feature's last one; its other features are kept), charAlign?: \"romanBaseline\"|\"emBoxTop\"|\"emBoxCenter\"|\"emBoxBottom\"|\"icfTop\"|\"icfBottom\", proportionalMetrics?: bool (full-width glyphs on the font's proportional widths: `palt` in horizontal type, `vpal` in vertical type), tateChuYoko?: bool (Character panel menu › Tate-chu-yoko, with Show East Asian Options: in vertical type those characters stand side by side across one em of the column; kept on the characters and ignored in horizontal type), tcyUpDown?, tcyLeftRight?: pt -1296..1296 (Character panel menu › Tate-chu-yoko Settings: in vertical type a block of tate-chu-yoko characters moves up by a positive Up/Down and down by a negative one, right by a positive Left/Right and left by a negative one, as one piece with the values of its first character; on a vertical path a positive Up/Down moves it toward the path's start and a positive Left/Right to the path's left; 0 by default; automatic digit blocks and horizontal type do not move; neither turns tateChuYoko on), warichu?: bool (Character panel menu › Warichu, with Show East Asian Options: the characters are set at warichuScale percent of their size and stacked on warichuLines lines inside their line, one above the other in horizontal type and side by side in vertical type, the first line above or to the right; a stretch of them is one block with the settings of its first character that stays whole on one line and may run past its end; type on a path sets them at their own size), warichuLines?: 1..6 (default 2), warichuGap?: pt 0..100 (the space between the lines, default 0), warichuScale?: % 10..100 (default 50), warichuAlign?: \"auto\"|\"left\"|\"center\"|\"right\"|\"justifyLeft\"|\"justifyCenter\"|\"justifyRight\"|\"justifyAll\" (Warichu Settings: how the lines line up along the block, which is as long as its longest line; the justify values spread every line but the last, which is set left, centered or right, or spread with justifyAll; auto, the default, sets them as justifyLeft; left and right are top and bottom in vertical type; none of them turns warichu on), akiLeft?, akiRight?: 1/1000 em 0..1000 (Character panel › Insert Aki Left and Right, with Show East Asian Options: space before and after each character in reading order, on its left and right in left-to-right type, on its right and left in right-to-left type, above and below it in vertical type; a ligature or a letter with its marks takes the values of its first character; line wrapping counts it, alignment keeps it at a line's ends and justification does not stretch or narrow it; 0 by default; a value that is not a number or is outside the range is refused, naming the key), tsume?: % 0..100 (Character panel › Tsume, with Show East Asian Options: the share of the blank space on each side of each character's ink, inside its own width, that is taken away in horizontal type, the glyph keeping its shape; 100 sets the ink edge to edge; a character without ink, such as a space, keeps its width; a ligature or a letter with its marks takes the value of its first character; tracking, kerning and Insert Aki are added after it, and justification keeps it; vertical type keeps the characters' spacing; 0 by default; a value that is not a number or is outside t
Line truncated
|
||||
has_doc,
|
||||
set_range_style
|
||||
),
|
||||
@@ -468,6 +468,8 @@ pub(crate) struct CharChange {
|
||||
/// Insert Aki Left and Right, in 1/1000 em.
|
||||
aki_left: Option<f64>,
|
||||
aki_right: Option<f64>,
|
||||
/// Tsume, in percent.
|
||||
tsume: Option<f64>,
|
||||
standard_vertical_roman: Option<bool>,
|
||||
no_break: Option<bool>,
|
||||
digits: Option<vectorcraft_doc::Digits>,
|
||||
@@ -545,6 +547,16 @@ pub(crate) fn aki_param(p: &Value, key: &str, cmd: &str) -> Result<Option<f64>>
|
||||
if (lo..=hi).contains(&v) { Ok(Some(v)) } else { Err(bad(cmd, format!("`{key}` must be from {lo} to {hi}"))) }
|
||||
}
|
||||
|
||||
/// `tsume` (Tsume): a percentage from 0 to [`vectorcraft_doc::text::MAX_TSUME`]. Any other value is
|
||||
/// an error.
|
||||
pub(crate) fn tsume_param(p: &Value, cmd: &str) -> Result<Option<f64>> {
|
||||
let key = "tsume";
|
||||
let Some(v) = p.get(key).filter(|v| !v.is_null()) else { return Ok(None) };
|
||||
let v = v.as_f64().filter(|v| v.is_finite()).ok_or_else(|| bad(cmd, format!("`{key}` must be a number")))?;
|
||||
let (lo, hi) = (0.0, vectorcraft_doc::text::MAX_TSUME);
|
||||
if (lo..=hi).contains(&v) { Ok(Some(v)) } else { Err(bad(cmd, format!("`{key}` must be from {lo} to {hi}"))) }
|
||||
}
|
||||
|
||||
/// `digits: "default"|"arabic"|"hindi"|"farsi"|"devanagari"|…` ([`Digits::id`]) → the numeral
|
||||
/// system the digits are drawn in.
|
||||
///
|
||||
@@ -824,6 +836,7 @@ impl CharChange {
|
||||
warichu_align: warichu_align_param(p, cmd)?,
|
||||
aki_left: aki_param(p, "akiLeft", cmd)?,
|
||||
aki_right: aki_param(p, "akiRight", cmd)?,
|
||||
tsume: tsume_param(p, cmd)?,
|
||||
standard_vertical_roman: flag("standardVerticalRoman"),
|
||||
no_break: flag("noBreak"),
|
||||
digits: digits_param(p, cmd)?,
|
||||
@@ -888,6 +901,7 @@ impl CharChange {
|
||||
&& self.warichu_align.is_none()
|
||||
&& self.aki_left.is_none()
|
||||
&& self.aki_right.is_none()
|
||||
&& self.tsume.is_none()
|
||||
&& self.standard_vertical_roman.is_none()
|
||||
&& self.no_break.is_none()
|
||||
&& self.digits.is_none()
|
||||
@@ -976,6 +990,9 @@ impl CharChange {
|
||||
if let Some(v) = self.aki_right {
|
||||
st.aki_right = v;
|
||||
}
|
||||
if let Some(v) = self.tsume {
|
||||
st.tsume = v;
|
||||
}
|
||||
if let Some(v) = self.standard_vertical_roman {
|
||||
st.standard_vertical_roman = v;
|
||||
}
|
||||
|
||||
@@ -222,6 +222,7 @@ pub fn char_attrs(v: &CharStyle) -> Map<String, Value> {
|
||||
("warichuAlign", json!(v.warichu_align)),
|
||||
("akiLeft", json!(v.aki_left)),
|
||||
("akiRight", json!(v.aki_right)),
|
||||
("tsume", json!(v.tsume)),
|
||||
("standardVerticalRoman", json!(v.standard_vertical_roman)),
|
||||
("noBreak", json!(v.no_break)),
|
||||
("digits", json!(v.digits)),
|
||||
@@ -578,6 +579,10 @@ fn attrs_param(d: &Document, p: &Value, kind: Kind, verb: &str, kept: Option<&Ma
|
||||
return Err(bad(&c, format!("`{key}` must be from {lo} to {hi}")));
|
||||
}
|
||||
}
|
||||
let (key, lo, hi) = ("tsume", 0.0, vectorcraft_doc::text::MAX_TSUME);
|
||||
if changed(key) && !(lo..=hi).contains(&st.tsume) {
|
||||
return Err(bad(&c, format!("`{key}` must be from {lo} to {hi}")));
|
||||
}
|
||||
let lines = vectorcraft_doc::text::WARICHU_LINES;
|
||||
if changed("warichuLines") && !lines.contains(&st.warichu_lines) {
|
||||
return Err(bad(&c, format!("`warichuLines` must be a whole number from {} to {}", lines.start(), lines.end())));
|
||||
@@ -1075,12 +1080,12 @@ fn unused(s: &mut Session, kind: Kind) -> Result<Value> {
|
||||
|
||||
/// The styles of `kind` in `src` that load, with their source names so that art in the file keeps
|
||||
/// referring to them: not the Normal style, and not one whose attributes don't read, whose sizes,
|
||||
/// scales, leading, shifts, Insert Aki, Warichu Settings or stroke width are out of the range `charStyle.new` takes, whose
|
||||
/// scales, leading, shifts, Insert Aki, Tsume, Warichu Settings or stroke width are out of the range `charStyle.new` takes, whose
|
||||
/// indents, spacing, Hyphenation or Justification options or tab stops are out of the range
|
||||
/// `paraStyle.new` takes, or whose name is empty, which are skipped. → (the styles, the names in
|
||||
/// `src` of those skipped)
|
||||
fn loadable(src: &Document, kind: Kind) -> (Vec<TextStyleDef>, Vec<String>) {
|
||||
const RANGED: [&str; 21] = [
|
||||
const RANGED: [&str; 22] = [
|
||||
"size",
|
||||
"h_scale",
|
||||
"v_scale",
|
||||
@@ -1088,6 +1093,7 @@ fn loadable(src: &Document, kind: Kind) -> (Vec<TextStyleDef>, Vec<String>) {
|
||||
"baseline_shift",
|
||||
"akiLeft",
|
||||
"akiRight",
|
||||
"tsume",
|
||||
"tcyUpDown",
|
||||
"tcyLeftRight",
|
||||
"warichuLines",
|
||||
|
||||
@@ -397,6 +397,8 @@ fn character_style_dimensions_are_checked_before_a_definition_changes() {
|
||||
json!({"tcyLeftRight": -1296.1}),
|
||||
json!({"akiLeft": -0.1}),
|
||||
json!({"akiRight": 1000.1}),
|
||||
json!({"tsume": -0.1}),
|
||||
json!({"tsume": 100.1}),
|
||||
] {
|
||||
let key = attrs.as_object().unwrap().keys().next().unwrap();
|
||||
for cmd in ["charStyle.edit", "charStyle.setAttrs", "charStyle.new"] {
|
||||
@@ -419,7 +421,8 @@ fn character_style_dimensions_are_checked_before_a_definition_changes() {
|
||||
"h_scale": 1.0, "v_scale": 10000.0,
|
||||
"baseline_shift": -1296.0,
|
||||
"tcyUpDown": 1296.0, "tcyLeftRight": -1296.0,
|
||||
"akiLeft": 0.0, "akiRight": 1000.0
|
||||
"akiLeft": 0.0, "akiRight": 1000.0,
|
||||
"tsume": 100.0
|
||||
}}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -12,8 +12,8 @@ use vectorcraft_geom::{Affine, PathData, Vec2};
|
||||
use super::edit::selected_roots;
|
||||
use super::pathops::{length_given, number_given, shape_node};
|
||||
use super::{
|
||||
CommandSpec, EngineError, Result, Session, bad, check_numbers, cmd, color_value, f64_req, has_doc, has_selection, index, number_or, object_param,
|
||||
ok, str_param, switch_or, targets, text_param,
|
||||
CommandSpec, EngineError, Result, Session, bad, check_numbers, cmd, color_value, f64_req, has_doc, has_selection, index, index_opt, number_or,
|
||||
object_param, ok, str_param, switch_or, targets, text_param,
|
||||
};
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
@@ -1376,7 +1376,7 @@ fn set_path_handle(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "text.setPathHandle";
|
||||
let id = object_param(p, "id", C)?.ok_or_else(|| bad(C, "missing id"))?;
|
||||
let si = super::draw2::usize_opt(p, "subpath", C)?.unwrap_or(0);
|
||||
let ai = p.get("anchor").and_then(Value::as_u64).map(index).ok_or_else(|| bad(C, "missing anchor"))?;
|
||||
let ai = index_opt(p, "anchor", C)?.ok_or_else(|| bad(C, "missing anchor"))?;
|
||||
let out = super::object::choice(p, C, "which", &["in", "out"])? != Some("in");
|
||||
let pos = vectorcraft_geom::Point::new(f64_req(p, "x", C)?, f64_req(p, "y", C)?);
|
||||
let independent = switch_or(p, "independent", false, C)?;
|
||||
|
||||
@@ -1821,6 +1821,61 @@ fn insert_aki_is_a_character_attribute_set_one_side_at_a_time() {
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step for a refused call");
|
||||
}
|
||||
|
||||
/// Tsume (Character panel, East Asian options): a character attribute in percent, 0 by default
|
||||
/// and saved only when not 0. Both commands set it in one undo step, a call that gives the value
|
||||
/// the text already has records none, and a value that is not a number or lies outside 0 to 100 is
|
||||
/// refused, naming the key, before anything changes; a character style captured from the
|
||||
/// characters carries it.
|
||||
#[test]
|
||||
fn tsume_is_a_character_attribute() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
|
||||
let id = s.execute("text.create", &json!({"x": 10, "y": 50, "text": "(日本)"})).unwrap()["id"].as_u64().unwrap();
|
||||
let runs = |s: &Session| match &s.doc().unwrap().doc.node(NodeId(id)).unwrap().kind {
|
||||
NodeKind::Text(t) => t.runs.clone(),
|
||||
_ => panic!("text"),
|
||||
};
|
||||
let tsume = |s: &Session| runs(s).iter().map(|r| (r.text.clone(), r.style.tsume)).collect::<Vec<_>>();
|
||||
let saved = serde_json::to_value(&runs(&s)[0].style).unwrap();
|
||||
assert!(saved.get("tsume").is_none());
|
||||
s.execute("select.set", &json!({"ids": [id]})).unwrap();
|
||||
s.execute("text.setFormat", &json!({"tsume": 50})).unwrap();
|
||||
assert_eq!(tsume(&s), [("(日本)".to_string(), 50.0)]);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
s.execute("text.setFormat", &json!({"tsume": 50})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step without a change");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(tsume(&s), [("(日本)".to_string(), 0.0)], "one undo step");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(tsume(&s), [("(日本)".to_string(), 50.0)]);
|
||||
|
||||
let paren = "(".len();
|
||||
s.execute("text.setRangeStyle", &json!({"id": id, "start": 0, "end": paren, "tsume": 100})).unwrap();
|
||||
assert_eq!(tsume(&s), [("(".to_string(), 100.0), ("日本)".to_string(), 50.0)]);
|
||||
let saved = serde_json::to_value(&runs(&s)[0].style).unwrap();
|
||||
assert_eq!(saved["tsume"], json!(100.0));
|
||||
let back = vectorcraft_format::load(&vectorcraft_format::save(&s.doc().unwrap().doc, false)).unwrap();
|
||||
let NodeKind::Text(t) = &back.node(NodeId(id)).unwrap().kind else { panic!("text") };
|
||||
let reopened: Vec<_> = t.runs.iter().map(|r| (r.text.clone(), r.style.tsume)).collect();
|
||||
assert_eq!(reopened, tsume(&s));
|
||||
let attrs = super::super::textstyles::char_attrs(&runs(&s)[0].style);
|
||||
assert_eq!(attrs["tsume"], json!(100.0));
|
||||
|
||||
let before = tsume(&s);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
for (cmd, p, want) in [
|
||||
("text.setFormat", json!({"tsume": "50"}), "`tsume` must be a number"),
|
||||
("text.setFormat", json!({"tsume": -1}), "`tsume` must be from 0 to 100"),
|
||||
("text.setRangeStyle", json!({"id": id, "tsume": true}), "`tsume` must be a number"),
|
||||
("text.setRangeStyle", json!({"id": id, "start": 0, "end": paren, "tsume": 100.5}), "`tsume` must be from 0 to 100"),
|
||||
] {
|
||||
let e = s.execute(cmd, &p).unwrap_err();
|
||||
assert!(matches!(&e, EngineError::BadParams { msg, .. } if msg == want), "{cmd} {p}: {e}");
|
||||
}
|
||||
assert_eq!(tsume(&s), before);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step for a refused call");
|
||||
}
|
||||
|
||||
/// Standard Vertical Roman Alignment (Character panel menu, East Asian options): a character
|
||||
/// attribute, off by default and saved only when on. Documents written before it read as off; a
|
||||
/// character style captured from the characters carries it.
|
||||
|
||||
@@ -138,7 +138,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Paste without Formatting",
|
||||
["Edit"],
|
||||
Some("Cmd+Alt+V"),
|
||||
"{center?, dx?, dy?, swatchConflict?} paste as edit.paste; pasted text takes the default character style (one run) and leaves its character and paragraph styles behind → {ids, added, merged, renamed}",
|
||||
"{center?, dx?, dy?, swatchConflict?} paste as edit.paste; pasted text takes the default character style (one run) and leaves its character and paragraph styles behind, as do the labels of pasted graphs → {ids, added, merged, renamed}",
|
||||
has_clipboard,
|
||||
paste_plain
|
||||
),
|
||||
@@ -1460,6 +1460,16 @@ fn strip_formatting(n: &mut vectorcraft_doc::Node) {
|
||||
t.set_all_paras(Default::default());
|
||||
refresh_bounds(t);
|
||||
}
|
||||
// A graph keeps the names of its labels' character and paragraph styles for each redraw.
|
||||
if let Some(g) = n.extras_if_any().and_then(|e| e.graph.as_deref_mut()) {
|
||||
let l = &mut g.label_styles;
|
||||
for st in [&mut l.values, &mut l.categories, &mut l.legend].into_iter().flatten() {
|
||||
st.style_name = None;
|
||||
}
|
||||
for pa in [&mut l.values_para, &mut l.categories_para, &mut l.legend_para].into_iter().flatten() {
|
||||
pa.style_name = None;
|
||||
}
|
||||
}
|
||||
if let Some(ch) = n.children_mut() {
|
||||
for c in ch.iter_mut() {
|
||||
strip_formatting(std::sync::Arc::make_mut(c));
|
||||
|
||||
@@ -26,7 +26,8 @@
|
||||
//! Before a run starts, its preprocessor lines are read ([`include`]): included files take the
|
||||
//! place of their lines, and an error in one is reported at its own file, line and column.
|
||||
//! `$.evalFile(path)` runs another file in the same global environment and gives its last value,
|
||||
//! `$.fileName` is the file being run and `$.includePath` the `#includepath` list.
|
||||
//! `$.fileName` is the file of the code that reads it (the file being run for code that `eval` or
|
||||
//! `Function` compiled) and `$.includePath` the `#includepath` list.
|
||||
//!
|
||||
//! A run writes to the Script Console with `$.write` and `$.writeln`, and `vc.log` adds a line
|
||||
//! there too. The session's console gets a start line, the run's output, its uncaught error, an end
|
||||
|
||||
@@ -468,6 +468,72 @@ fn styles_symbols_and_images_of_the_same_name_are_renamed() {
|
||||
assert_eq!(key(&s, img2), src_key);
|
||||
}
|
||||
|
||||
/// A graph whose series fill, value labels and legend rows paint with global swatch "Brand" and
|
||||
/// whose value labels are set in character style "Emphasis" and paragraph style "Body", as a
|
||||
/// later redraw would give them to its marks and labels.
|
||||
fn styled_graph(s: &mut Session) -> NodeId {
|
||||
s.execute("swatch.new", &json!({"name": "Brand", "color": "#ff0000", "global": true})).unwrap();
|
||||
s.execute("charStyle.new", &json!({"name": "Emphasis", "attrs": {"size": 24.0}})).unwrap();
|
||||
s.execute("paraStyle.new", &json!({"name": "Body", "attrs": {"space_after": 8.0}})).unwrap();
|
||||
let v = s.execute("graph.create", &json!({"type": "column", "x": 200, "y": 200, "width": 60, "height": 40, "csv": ",2024\nQ1,3\nQ2,5"}));
|
||||
let id = id_of(&v.unwrap());
|
||||
let brand = linked(hex("#ff0000"), "Brand", 1.0);
|
||||
let mut extras = doc_mut(s).node_mut(id).unwrap().extras_mut();
|
||||
let g = extras.graph.as_mut().unwrap();
|
||||
g.series_paints = vec![vectorcraft_doc::SeriesPaint { fill: Some(brand.clone()), ..Default::default() }];
|
||||
let l = &mut g.label_styles;
|
||||
l.values = Some(vectorcraft_doc::CharStyle { fill: brand.clone(), style_name: Some("Emphasis".into()), ..Default::default() });
|
||||
l.values_para = Some(vectorcraft_doc::ParaStyle { style_name: Some("Body".into()), ..Default::default() });
|
||||
let rows = vectorcraft_doc::SeriesPaint { stroke: Some(brand), ..Default::default() };
|
||||
l.legend_rows = Some(vectorcraft_doc::graph::LabelRows { paint: rows, ..Default::default() });
|
||||
id
|
||||
}
|
||||
|
||||
/// The swatch names a graph's stored paints link to, and its value labels' character and
|
||||
/// paragraph style names.
|
||||
fn graph_links(s: &Session, id: NodeId) -> (Vec<String>, Option<String>, Option<String>) {
|
||||
let n = node(s, id);
|
||||
let swatches = vectorcraft_doc::swatches::graph_paints(&n).filter_map(|p| match p {
|
||||
Paint::Solid { swatch: Some(w), .. } => Some(w.clone()),
|
||||
_ => None,
|
||||
});
|
||||
let l = &n.extras().graph.as_deref().unwrap().label_styles;
|
||||
let values = l.values.as_ref().and_then(|st| st.style_name.clone());
|
||||
(swatches.collect(), values, l.values_para.as_ref().and_then(|pa| pa.style_name.clone()))
|
||||
}
|
||||
|
||||
/// A pasted graph brings the swatches and text styles its series and labels keep, and follows
|
||||
/// them when they come in under new names.
|
||||
#[test]
|
||||
fn a_pasted_graph_brings_and_follows_its_label_styles_and_paints() {
|
||||
let mut s = session();
|
||||
let g = styled_graph(&mut s);
|
||||
copy(&mut s, &[g]);
|
||||
|
||||
new_doc(&mut s);
|
||||
let v = s.execute("edit.pasteInPlace", &json!({})).unwrap();
|
||||
let d = doc(&s);
|
||||
assert!(d.swatch("Brand").is_some(), "{v}");
|
||||
assert!(d.char_styles.iter().any(|x| x.name == "Emphasis") && d.para_styles.iter().any(|x| x.name == "Body"), "{v}");
|
||||
let brand = vec!["Brand".to_string(); 3];
|
||||
assert_eq!(graph_links(&s, ids_of(&v)[0]), (brand.clone(), Some("Emphasis".into()), Some("Body".into())));
|
||||
|
||||
// Into a document that gives each name something else: they come in renamed.
|
||||
new_doc(&mut s);
|
||||
s.execute("swatch.new", &json!({"name": "Brand", "color": "#0000ff", "global": true})).unwrap();
|
||||
s.execute("charStyle.new", &json!({"name": "Emphasis", "attrs": {"size": 8.0}})).unwrap();
|
||||
s.execute("paraStyle.new", &json!({"name": "Body", "attrs": {"space_after": 2.0}})).unwrap();
|
||||
let v = s.execute("edit.pasteInPlace", &json!({"swatchConflict": "add"})).unwrap();
|
||||
let brand2 = vec!["Brand 2".to_string(); 3];
|
||||
assert_eq!(graph_links(&s, ids_of(&v)[0]), (brand2, Some("Emphasis 2".into()), Some("Body 2".into())), "{v}");
|
||||
|
||||
// Without formatting: the labels' text styles stay behind.
|
||||
new_doc(&mut s);
|
||||
let v = s.execute("edit.pasteWithoutFormatting", &json!({})).unwrap();
|
||||
assert!(doc(&s).char_styles.is_empty() && doc(&s).para_styles.is_empty(), "{v}");
|
||||
assert_eq!(graph_links(&s, ids_of(&v)[0]), (brand, None, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pasting_again_reuses_what_an_earlier_paste_added() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -422,6 +422,122 @@ fn a_compound_clipping_path_in_the_art_keeps_its_shape() {
|
||||
assert_eq!(seen, (true, true));
|
||||
}
|
||||
|
||||
/// A Drop Shadow 20 pt to the right on row `row` of instance `id`, solid black with no blur.
|
||||
fn shadow_row(s: &mut Session, id: NodeId, row: usize) {
|
||||
let params = json!({"x": 20, "y": 0, "blur": 0, "opacity": 100, "mode": "normal"});
|
||||
run(s, "effect.apply", json!({"ids": [id.0], "item": row, "effect": "stylize.dropShadow", "params": params}));
|
||||
}
|
||||
|
||||
/// Is `p` dark: a shadow, not the white page or the red fill?
|
||||
fn dark(p: [u8; 4]) -> bool {
|
||||
p[..3].iter().all(|c| *c < 100)
|
||||
}
|
||||
|
||||
/// A raster effect on a symbol instance's fill draws on the filled shapes of its art: the shadow
|
||||
/// falls right of the square, the bounds take it in, a hidden fill draws none, and Expand
|
||||
/// Appearance keeps the look in one undo step.
|
||||
#[test]
|
||||
fn a_symbol_instance_fill_shadow_shadows_its_filled_art() {
|
||||
let mut s = session();
|
||||
let (id, row) = instance_with_row(&mut s, "Square", true);
|
||||
assert!(near(render(&s).pixel(160, 100), WHITE));
|
||||
shadow_row(&mut s, id, row);
|
||||
let before = render(&s);
|
||||
assert!(dark(before.pixel(165, 100)), "the fill's shadow falls right of the square: {:?}", before.pixel(165, 100));
|
||||
assert!(near(before.pixel(100, 100), RED), "the art draws over its shadow: {:?}", before.pixel(100, 100));
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let b = vectorcraft_render::painted_bounds(&doc, doc.node(id).unwrap()).expect("the instance has bounds");
|
||||
assert!(b.x1 >= 169.0, "the bounds take in the shadow: {b:?}");
|
||||
// A view that shows only the shadow, right of the square: the instance is still drawn.
|
||||
let opts = vectorcraft_render::RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
let right = vectorcraft_render::Renderer::new().render(&doc, 200, 200, vectorcraft_geom::Affine::translate((-155.0, 0.0)), &opts);
|
||||
assert!(dark(right.pixel(10, 100)), "the shadow past the square's box is drawn: {:?}", right.pixel(10, 100));
|
||||
|
||||
assert!(expandable(&s), "the fill's shadow can be expanded");
|
||||
run(&mut s, "effect.expandAppearance", json!({}));
|
||||
let ch = node(&s, id).children().cloned().expect("the instance becomes a group");
|
||||
assert!(matches!(ch[0].kind, NodeKind::Image(_)), "the shadow is an image under the art: {}", ch[0].kind_label());
|
||||
assert!(matches!(ch[1].kind, NodeKind::SymbolInstance { .. }), "the art stays an instance: {}", ch[1].kind_label());
|
||||
no_effects_left(&ch[1]);
|
||||
let after = render(&s);
|
||||
assert!(differing(&before, &after, 24) < 400, "expanding changed {} pixels", differing(&before, &after, 24));
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
let n = node(&s, id);
|
||||
assert!(matches!(n.kind, NodeKind::SymbolInstance { .. }), "one undo gives the instance back");
|
||||
assert!(n.appearance.items[row].effects().iter().any(|e| e.id == "stylize.dropShadow"));
|
||||
|
||||
run(&mut s, "appearance.setItem", json!({"ids": [id.0], "index": row, "visible": false}));
|
||||
assert!(near(render(&s).pixel(165, 100), WHITE), "a hidden fill draws no shadow");
|
||||
}
|
||||
|
||||
/// A raster effect on a symbol instance's stroke draws on the stroked shapes of its art only: the
|
||||
/// stroke's shadow falls right of the stroke, and none falls where only the fill would cast one.
|
||||
#[test]
|
||||
fn a_symbol_instance_stroke_shadow_shadows_only_its_stroked_art() {
|
||||
let mut s = session();
|
||||
let (id, row) = instance_with_row(&mut s, "Square", false);
|
||||
shadow_row(&mut s, id, row);
|
||||
let r = render(&s);
|
||||
assert!(dark(r.pixel(170, 100)), "the stroke's shadow falls right of the stroke: {:?}", r.pixel(170, 100));
|
||||
assert!(near(r.pixel(160, 100), WHITE), "the fill casts no shadow: {:?}", r.pixel(160, 100));
|
||||
}
|
||||
|
||||
/// Geometry and raster effects on one fill of an instance: the shadow falls from the grown fill.
|
||||
#[test]
|
||||
fn a_symbol_instance_fill_shadow_follows_the_fill_offset_before_it() {
|
||||
let mut s = session();
|
||||
let (id, row) = instance_with_row(&mut s, "Square", true);
|
||||
offset_row(&mut s, id, row, 10.0);
|
||||
shadow_row(&mut s, id, row);
|
||||
let r = render(&s);
|
||||
assert!(near(r.pixel(44, 100), RED), "the fill grows by the offset: {:?}", r.pixel(44, 100));
|
||||
assert!(dark(r.pixel(175, 100)), "the shadow falls from the grown fill: {:?}", r.pixel(175, 100));
|
||||
}
|
||||
|
||||
/// An instance inside a symbol's art passes the outer instance's fill shadow on to its own art.
|
||||
#[test]
|
||||
fn a_nested_symbol_instance_takes_the_outer_fill_shadow() {
|
||||
let mut s = session();
|
||||
square(&mut s);
|
||||
run(&mut s, "symbol.new", json!({"name": "Inner"}));
|
||||
let id = NodeId(run(&mut s, "symbol.new", json!({"name": "Outer"}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "appearance.addFill", json!({"ids": [id.0]}));
|
||||
let row = node(&s, id).appearance.items.len() - 1;
|
||||
run(&mut s, "appearance.setItem", json!({"ids": [id.0], "index": row, "none": true}));
|
||||
shadow_row(&mut s, id, row);
|
||||
assert!(dark(render(&s).pixel(165, 100)), "the inner art casts the shadow: {:?}", render(&s).pixel(165, 100));
|
||||
}
|
||||
|
||||
/// Export draws a symbol instance's fill shadow on the filled shapes of its art: baking leaves the
|
||||
/// shadow on the art's fill, not its stroke, and PDF export makes an image of it.
|
||||
#[test]
|
||||
fn export_draws_a_symbol_instance_fill_shadow() {
|
||||
let mut s = session();
|
||||
let (id, row) = instance_with_row(&mut s, "Square", true);
|
||||
shadow_row(&mut s, id, row);
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let baked = vectorcraft_render::effects::bake_document(&doc).expect("the instance is baked");
|
||||
let n = baked.node(id).expect("the baked art keeps the instance's id").clone();
|
||||
assert!(!matches!(n.kind, NodeKind::SymbolInstance { .. }));
|
||||
let shadowed = |i: &vectorcraft_doc::AppearanceItem| i.effects().iter().any(|e| e.id == "stylize.dropShadow");
|
||||
let (mut fills, mut strokes) = (0, 0);
|
||||
n.walk(&mut |part| {
|
||||
fills += part.appearance.items.iter().filter(|i| i.is_fill() && shadowed(i)).count();
|
||||
strokes += part.appearance.items.iter().filter(|i| !i.is_fill() && shadowed(i)).count();
|
||||
});
|
||||
assert_eq!((fills, strokes), (1, 0), "the art's fill carries the shadow");
|
||||
|
||||
let flat = crate::flatten_raster_effects(&doc).expect("the shadow becomes an image");
|
||||
let mut image = None;
|
||||
flat.node(id).expect("the flattened art keeps the instance's id").walk(&mut |part| {
|
||||
if matches!(part.kind, NodeKind::Image(_)) {
|
||||
image = part.geometric_bounds();
|
||||
}
|
||||
});
|
||||
let image = image.expect("the shadow is an image");
|
||||
assert!(image.x1 >= 165.0, "the image holds the shadow right of the square: {image:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_fills_and_an_offset_stroke_become_three_paths_that_look_the_same() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -138,7 +138,7 @@ fn knockout_settings_round_trip_and_old_bools_load() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pdf_and_svg_write_knockout_groups_as_masks() {
|
||||
fn pdf_writes_knockout_groups_and_svg_writes_them_as_masks() {
|
||||
let mut s = session();
|
||||
let g = knockout_group(&mut s);
|
||||
let plain = s.doc().unwrap().doc.clone();
|
||||
@@ -149,10 +149,11 @@ fn pdf_and_svg_write_knockout_groups_as_masks() {
|
||||
let r = pdf(&plain);
|
||||
assert!(!String::from_utf8_lossy(&r.bytes).contains("/SMask") && r.warnings.is_empty());
|
||||
let r = pdf(&ko);
|
||||
let text = String::from_utf8_lossy(&r.bytes);
|
||||
// The lower rectangle is drawn through a luminosity mask made of the upper one's alpha.
|
||||
assert!(text.contains("/Luminosity") && text.contains("/Alpha"), "soft masks");
|
||||
assert!(r.warnings.iter().any(|w| w.contains("knockout")), "{:?}", r.warnings);
|
||||
let text = String::from_utf8_lossy(&r.bytes).replace(' ', "");
|
||||
// The rectangles knock out the area they paint: a PDF knockout group, without soft masks.
|
||||
assert_eq!(text.matches("/Ktrue").count(), 1, "a knockout group");
|
||||
assert!(!text.contains("/Luminosity"), "no soft masks");
|
||||
assert!(r.warnings.is_empty(), "{:?}", r.warnings);
|
||||
|
||||
let svg = |d: &Document| vectorcraft_svg::export(d, &Default::default());
|
||||
assert!(!svg(&plain).contains("knockout"));
|
||||
@@ -173,5 +174,5 @@ fn pdf_and_svg_write_knockout_groups_as_masks() {
|
||||
assert!(svg(&page).contains("isolation"));
|
||||
page.page_knockout = true;
|
||||
assert_eq!(svg(&page).matches("mask=\"url(#knockout-").count(), 1, "the layer is neutral: its objects knock each other out");
|
||||
assert!(String::from_utf8_lossy(&pdf(&page).bytes).contains("/Luminosity"));
|
||||
assert!(String::from_utf8_lossy(&pdf(&page).bytes).replace(' ', "").contains("/Ktrue"));
|
||||
}
|
||||
@@ -849,3 +849,75 @@ fn journal_entries_pass_the_check() {
|
||||
assert!(check_params(id, params).is_ok(), "{id} {params}");
|
||||
}
|
||||
}
|
||||
|
||||
/// An index of a path, type, panel, library, style or ruler command 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. A required one is not reported as missing, and an optional one (`subpath` of
|
||||
/// `path.appendAnchor`, `path.close` and `path.reverse`, `artboard` of the ruler origin commands) is
|
||||
/// not read as its default. A required one left out or null is still missing, and an optional one left
|
||||
/// out or null keeps its default. Neither changes the document or records an undo step.
|
||||
#[test]
|
||||
fn a_malformed_index_is_not_missing_or_its_default() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = s.execute("path.create", &json!({"d": "M0 0 L10 0 M20 0 L30 0"})).unwrap()["id"].clone();
|
||||
s.execute("library.create", &json!({"name": "Brand"})).unwrap();
|
||||
s.execute("library.createGroup", &json!({"name": "Warm"})).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 malformed = [json!("3"), json!(2.5), json!(-1), json!(true), json!([]), json!({})];
|
||||
let handle = json!({"id": id, "x": 5, "y": 5});
|
||||
let required = [
|
||||
("path.setHandle", handle.clone(), "anchor", "missing anchor"),
|
||||
("path.moveAnchor", handle.clone(), "anchor", "missing anchor"),
|
||||
("path.insertAnchor", json!({"id": id}), "segment", "missing segment"),
|
||||
("text.setPathHandle", handle.clone(), "anchor", "missing anchor"),
|
||||
("artboard.reorder", json!({"index": 0}), "to", "missing to"),
|
||||
("library.moveGroup", json!({"group": "Warm"}), "to", "missing `to` (an index)"),
|
||||
("graphicStyle.move", json!({"name": "Default"}), "to", "missing `to` (an index)"),
|
||||
];
|
||||
for (cmd, base, key, missing) in &required {
|
||||
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);
|
||||
}
|
||||
}
|
||||
let optional = [
|
||||
("path.appendAnchor", handle.clone(), "subpath"),
|
||||
("path.close", json!({"id": id}), "subpath"),
|
||||
("path.reverse", json!({"ids": [id]}), "subpath"),
|
||||
("document.rulerOrigin", json!({}), "artboard"),
|
||||
("document.setRulerOrigin", json!({"x": 5, "y": 5}), "artboard"),
|
||||
("document.resetRulerOrigin", json!({}), "artboard"),
|
||||
];
|
||||
for (cmd, base, key) in &optional {
|
||||
for v in &malformed {
|
||||
refused(&mut s, cmd, &with(base, key, Some(v.clone())), &format!("`{key}` must be a whole number"));
|
||||
}
|
||||
}
|
||||
// A whole number that names no artboard is still out of range.
|
||||
refused(&mut s, "document.resetRulerOrigin", &json!({"artboard": 1}), "index out of range");
|
||||
assert_eq!(s.doc().unwrap().doc, doc);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
// Left out or null, an optional one keeps its default.
|
||||
for (cmd, base, key) in &optional {
|
||||
for p in [with(base, key, None), with(base, key, Some(json!(null)))] {
|
||||
s.execute(cmd, &p).unwrap_or_else(|e| panic!("{cmd} {p}: {e}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8321,7 +8321,7 @@ impl ArtBrushOptions {
|
||||
|
||||
/// A path with the Calligraphic brush, and one with an Art brush in Tints whose art bends along the
|
||||
/// path, come back as those paths with the brush on their stroke at its weight, not as their drawn
|
||||
/// look; a path with the Art brush that keeps its art's colors comes back as its drawn look.
|
||||
/// look; so does a path with the Art brush that keeps its art's colors.
|
||||
#[test]
|
||||
fn brushed_paths_are_read_back_brushed() {
|
||||
let mut doc = brushes_document();
|
||||
@@ -8341,8 +8341,63 @@ fn brushed_paths_are_read_back_brushed() {
|
||||
assert_eq!(art[1].path_data(), ours[1].path_data());
|
||||
brushed(&mut doc, "Plain ribbon", (20.0, 180.0), (280.0, 180.0), 1.0);
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
assert!(out.warnings.iter().any(|w| w == writer::STROKES), "{:?}", out.warnings);
|
||||
assert!(matches!(read_back(&doc).layers[0].children().unwrap()[2].kind, NodeKind::Group { .. }));
|
||||
assert!(!out.warnings.iter().any(|w| w == writer::STROKES), "{:?}", out.warnings);
|
||||
let back = read_back(&doc);
|
||||
let n = &back.layers[0].children().unwrap()[2];
|
||||
assert!(matches!(n.kind, NodeKind::Path { .. }), "{n:#?}");
|
||||
assert_eq!(n.appearance.stroke().and_then(|s| s.brush.as_deref()), Some("Plain ribbon"));
|
||||
}
|
||||
|
||||
/// Art brushes at another width, flipped along or across, scaled proportionately or stretched
|
||||
/// between guides (flipped along too, or at guides the written numbers round) are written with those
|
||||
/// options, and a path with each comes back as the path with the brush on its stroke, except Scale
|
||||
/// Proportionately's, which is written as its brush's art.
|
||||
#[test]
|
||||
fn art_brushes_with_options_are_read_back_with_them() {
|
||||
use vectorcraft_brush::{ArtBrush, ArtScale, Brush, BrushKind};
|
||||
let thirds = ArtScale::BetweenGuides { start: 1.0 / 3.0, end: 2.0 / 3.0 };
|
||||
let options: Vec<(&str, ArtBrush)> = vec![
|
||||
("Wide", ArtBrush { width: 250.0, ..ArtBrush::default() }),
|
||||
("Along", ArtBrush { flip_along: true, ..ArtBrush::default() }),
|
||||
("Across", ArtBrush { flip_across: true, ..ArtBrush::default() }),
|
||||
("Proportional", ArtBrush { scale: ArtScale::Proportional, width: 50.0, ..ArtBrush::default() }),
|
||||
("Guides", ArtBrush { scale: ArtScale::BetweenGuides { start: 0.25, end: 0.5 }, ..ArtBrush::default() }),
|
||||
("Guides along", ArtBrush { scale: ArtScale::BetweenGuides { start: 0.25, end: 0.5 }, flip_along: true, ..ArtBrush::default() }),
|
||||
("Thirds", ArtBrush { scale: thirds, flip_along: true, ..ArtBrush::default() }),
|
||||
];
|
||||
let lib: Vec<Brush> = options
|
||||
.iter()
|
||||
.map(|(name, a)| Brush { name: (*name).into(), kind: BrushKind::Art(ArtBrush { art: brush_art_node(), ..a.clone() }) })
|
||||
.collect();
|
||||
let mut doc = Document::new(300.0, 200.0);
|
||||
vectorcraft_brush::store(&mut doc, &lib);
|
||||
for (i, b) in lib.iter().enumerate() {
|
||||
let y = 20.0 + 25.0 * i as f64;
|
||||
brushed(&mut doc, &b.name, (20.0, y), (280.0, y), 1.0);
|
||||
}
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
assert!(!out.warnings.iter().any(|w| w == writer::BRUSHES), "{:?}", out.warnings);
|
||||
let back = read_back(&doc);
|
||||
let got = vectorcraft_brush::library(&back);
|
||||
assert_eq!(got.len(), lib.len());
|
||||
for (g, w) in got.iter().zip(&lib) {
|
||||
let (BrushKind::Art(g), BrushKind::Art(w)) = (&g.kind, &w.kind) else { panic!("art") };
|
||||
if w.scale == thirds {
|
||||
let ArtScale::BetweenGuides { start, end } = g.scale else { panic!("guides: {g:?}") };
|
||||
assert!((start - 1.0 / 3.0).abs() < 1e-6 && (end - 2.0 / 3.0).abs() < 1e-6, "{g:?}");
|
||||
assert_eq!(ArtBrushOptions::of(&ArtBrush { scale: thirds, ..g.clone() }), ArtBrushOptions::of(w));
|
||||
} else {
|
||||
assert_eq!(ArtBrushOptions::of(g), ArtBrushOptions::of(w));
|
||||
}
|
||||
}
|
||||
for (n, b) in back.layers[0].children().unwrap().iter().zip(&lib) {
|
||||
if b.name == "Proportional" {
|
||||
assert!(matches!(n.kind, NodeKind::Group { .. }), "{}: the brush's art: {n:#?}", b.name);
|
||||
continue;
|
||||
}
|
||||
assert!(matches!(n.kind, NodeKind::Path { .. }), "{}: {n:#?}", b.name);
|
||||
assert_eq!(n.appearance.stroke().and_then(|s| s.brush.as_deref()), Some(b.name.as_str()));
|
||||
}
|
||||
}
|
||||
|
||||
/// A compound path with the Calligraphic brush comes back as that compound path with the brush on its
|
||||
@@ -8646,8 +8701,8 @@ fn brushes_write_the_same_bytes_again() {
|
||||
/// beside a brush that comes back, and each changed from a brush that comes back by the one setting
|
||||
/// that leaves it out: an Art brush whose art is a group with a fill of its own, a Scatter brush whose
|
||||
/// art is a path without points, an Art brush whose art is a group without members, Pattern and Bristle
|
||||
/// brushes, Art brushes flipped along or across, overlapping, at another width, with a width that
|
||||
/// varies, in another direction or scale, Art and Scatter brushes in Tints and Shades or Hue Shift, a
|
||||
/// brushes, Art brushes overlapping, with a width that varies, in another direction or with guides
|
||||
/// that leave no art between them, Art and Scatter brushes in Tints and Shades or Hue Shift, a
|
||||
/// brush whose name holds a slash and a second brush of a name. A brush left out doesn't take its
|
||||
/// name from a later one.
|
||||
#[test]
|
||||
@@ -8671,13 +8726,10 @@ fn brushes_the_reader_cant_read_are_left_out() {
|
||||
("empty group", art_with(&|a| a.art = Node::group(vectorcraft_doc::NodeId(1), vec![]))),
|
||||
("pattern", BrushKind::Pattern(PatternBrush::default())),
|
||||
("bristle", BrushKind::Bristle(Bristle::default())),
|
||||
("flipped along", art_with(&|a| a.flip_along = true)),
|
||||
("flipped across", art_with(&|a| a.flip_across = true)),
|
||||
("overlap", art_with(&|a| a.overlap = true)),
|
||||
("width", art_with(&|a| a.width = 50.0)),
|
||||
("width varies", art_with(&|a| a.width_mode = Some(Variation::Pressure))),
|
||||
("direction", art_with(&|a| a.direction = Direction::RightToLeft)),
|
||||
("proportional", art_with(&|a| a.scale = ArtScale::Proportional)),
|
||||
("guides with no art between them", art_with(&|a| a.scale = ArtScale::BetweenGuides { start: 0.5, end: 0.5 })),
|
||||
("art tints and shades", art_with(&|a| a.colorization = Colorization::TintsAndShades)),
|
||||
("art hue shift", art_with(&|a| a.colorization = hue)),
|
||||
(
|
||||
@@ -8887,8 +8939,8 @@ fn an_empty_brushes_panel_comes_back_empty() {
|
||||
}
|
||||
|
||||
/// A path with one of a new document's brushes writes the brushes the reader reads, and keeps its
|
||||
/// brush. The new document's Art brush with other options, its Pattern brushes and its Bristle
|
||||
/// brushes are left out with a warning, so the file opens without them.
|
||||
/// brush. The new document's Pattern brushes and its Bristle brushes are left out with a warning,
|
||||
/// so the file opens without them.
|
||||
#[test]
|
||||
fn a_new_documents_brush_on_a_path_writes_the_brushes() {
|
||||
let mut doc = Document::new(300.0, 200.0);
|
||||
@@ -8898,7 +8950,7 @@ fn a_new_documents_brush_on_a_path_writes_the_brushes() {
|
||||
let back = read_back(&doc);
|
||||
let n = &back.layers[0].children().unwrap()[0];
|
||||
assert_eq!(n.appearance.stroke().and_then(|s| s.brush.as_deref()), Some("3 pt. Round"));
|
||||
let left_out = ["Arrow", "Chain", "Stitches", "Diamonds", "Bristle Round", "Bristle Flat Fan"];
|
||||
let left_out = ["Chain", "Stitches", "Diamonds", "Bristle Round", "Bristle Flat Fan"];
|
||||
let want: Vec<&str> = vectorcraft_brush::defaults().iter().map(|b| b.name.as_str()).filter(|n| !left_out.contains(n)).collect();
|
||||
let lib = vectorcraft_brush::library(&back);
|
||||
assert_eq!(lib.iter().map(|b| b.name.as_str()).collect::<Vec<_>>(), want);
|
||||
|
||||
@@ -23,7 +23,7 @@ use super::{Writer, Xy};
|
||||
use crate::ai_ops;
|
||||
use crate::ps;
|
||||
|
||||
pub(in crate::ai_editing) const BRUSHES: &str = "Pattern and Bristle brushes, Art brushes that don't stretch their art along the path from left to right at 100% unflipped without overlap, Scatter and Art brushes in Tints and Shades or Hue Shift, brushes whose art holds an empty path or group or more than paths, compound paths and groups with one solid fill and one plain stroke each, brushes whose names hold a slash or repeat an earlier brush's name, brushes that would open with other settings, brushes past the number or the amount of art one file's brushes can hold, and every brush on a large canvas are left out of the Brushes panel in the editing data for other apps";
|
||||
pub(in crate::ai_editing) const BRUSHES: &str = "Pattern and Bristle brushes, Art brushes that don't run their art from left to right, that overlap or whose width follows the pen, Scatter and Art brushes in Tints and Shades or Hue Shift, brushes whose art holds an empty path or group or more than paths, compound paths and groups with one solid fill and one plain stroke each, brushes whose names hold a slash or repeat an earlier brush's name, brushes that would open with other settings, brushes past the number or the amount of art one file's brushes can hold, and every brush on a large canvas are left out of the Brushes panel in the editing data for other apps";
|
||||
|
||||
/// Most brushes written (the reader's limit).
|
||||
const MAX_BRUSHES: usize = 4096;
|
||||
@@ -145,22 +145,71 @@ fn scatter_params(s: &Scatter, art: &str) -> Option<String> {
|
||||
Some(format!("2 /{art}/ 5 0 0 0 / {}", nums6(&numbers)))
|
||||
}
|
||||
|
||||
/// An Art brush's parameters, its art the section `art`: `None` for options the reader doesn't read
|
||||
/// (it reads an Art brush that stretches its art along the path from left to right at 100%,
|
||||
/// unflipped, without overlap, in Tints or keeping its colors).
|
||||
fn art_params(a: &ArtBrush, art: &str) -> Option<String> {
|
||||
let plain = a.direction == Direction::LeftToRight
|
||||
&& matches!(a.scale, ArtScale::Stretch)
|
||||
&& a.width == 100.0
|
||||
&& a.width_mode.is_none()
|
||||
&& !a.flip_along
|
||||
&& !a.flip_across
|
||||
&& !a.overlap;
|
||||
if !plain {
|
||||
/// An Art brush's settings as its parameters' numbers 2 to 15 give them and a brushed stroke's filter
|
||||
/// holds them ([`ART_SETTINGS`]): `None` for options the reader doesn't read (it reads an Art brush
|
||||
/// that runs its art from left to right without overlap, at a fixed width, in Tints or keeping its
|
||||
/// colors). The guides of Stretch Between Guides are the lengths of the art before the first and
|
||||
/// after the second, measured before the art is flipped along the path.
|
||||
fn art_settings(a: &ArtBrush) -> Option<[f64; 14]> {
|
||||
if a.direction != Direction::LeftToRight || a.width_mode.is_some() || a.overlap {
|
||||
return None;
|
||||
}
|
||||
let c = colorization_code(a.colorization)?;
|
||||
let numbers = [1.0, c, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0];
|
||||
let length = a.art.geometric_bounds().map_or(0.0, |b| b.width());
|
||||
let (proportional, segmented, before, after) = match a.scale {
|
||||
ArtScale::Stretch => (0.0, 0.0, 0.0, 0.0),
|
||||
ArtScale::Proportional => (1.0, 0.0, 0.0, 0.0),
|
||||
ArtScale::BetweenGuides { start, end } if start < end => {
|
||||
let (before, after) = (start * length, (1.0 - end) * length);
|
||||
let (before, after) = if a.flip_along { (after, before) } else { (before, after) };
|
||||
(0.0, 1.0, before, after)
|
||||
}
|
||||
ArtScale::BetweenGuides { .. } => return None,
|
||||
};
|
||||
let scaling = a.width / 100.0;
|
||||
let flip = |on: bool| f64::from(u8::from(on));
|
||||
Some([
|
||||
colorization_code(a.colorization)?,
|
||||
scaling,
|
||||
0.0,
|
||||
1.0,
|
||||
flip(a.flip_along),
|
||||
flip(a.flip_across),
|
||||
proportional,
|
||||
scaling,
|
||||
scaling,
|
||||
0.0,
|
||||
segmented,
|
||||
before,
|
||||
after,
|
||||
1.0,
|
||||
])
|
||||
}
|
||||
|
||||
/// The entries of a brushed stroke's filter that hold an Art brush's settings, in the order of
|
||||
/// [`art_settings`], with how each is written.
|
||||
const ART_SETTINGS: [(&str, &str); 14] = [
|
||||
("Colorization", "Int"),
|
||||
("Scaling", "Real"),
|
||||
("Spacing", "Real"),
|
||||
("Adjustment", "Int"),
|
||||
("Counterclockwise", "Int"),
|
||||
("UpsideDown", "Int"),
|
||||
("Proportional", "Bool"),
|
||||
("MinScaling", "Real"),
|
||||
("MaxScaling", "Real"),
|
||||
("ScaleYDependsOn", "Int"),
|
||||
("Segmented", "Bool"),
|
||||
("StartSegmentLength", "Real"),
|
||||
("EndSegmentLength", "Real"),
|
||||
("FoldAdjustment", "Bool"),
|
||||
];
|
||||
|
||||
/// An Art brush's parameters, its art the section `art`: `None` for options the reader doesn't read
|
||||
/// ([`art_settings`]).
|
||||
fn art_params(a: &ArtBrush, art: &str) -> Option<String> {
|
||||
let settings = art_settings(a)?;
|
||||
let mut numbers = vec![1.0];
|
||||
numbers.extend(settings);
|
||||
Some(format!("2 / {art}/ / / / / 0 0/ 5 0 0 0 / {}", nums6(&numbers)))
|
||||
}
|
||||
|
||||
@@ -458,25 +507,10 @@ impl Writer<'_> {
|
||||
d.push_str("0 /Int (AngleRelativeTo) ,\n");
|
||||
"(Calligraphic Brush) /Title ,\n"
|
||||
}
|
||||
BrushKind::Art(_) => {
|
||||
BrushKind::Art(a) => {
|
||||
d.push_str("15 /Int (Adobe Effect Expand Before Version) ,\n"); // brand-ok: the entry's name in the files
|
||||
for (key, value, kind) in [
|
||||
("StartSegmentLength", "0", "Real"),
|
||||
("EndSegmentLength", "0", "Real"),
|
||||
("Counterclockwise", "0", "Int"),
|
||||
("MinScaling", "1", "Real"),
|
||||
("MaxScaling", "1", "Real"),
|
||||
("Colorization", "1", "Int"),
|
||||
("Spacing", "0", "Real"),
|
||||
("Adjustment", "1", "Int"),
|
||||
("Proportional", "0", "Bool"),
|
||||
("Segmented", "0", "Bool"),
|
||||
("UpsideDown", "0", "Int"),
|
||||
("FoldAdjustment", "1", "Bool"),
|
||||
("ScaleYDependsOn", "0", "Int"),
|
||||
("Scaling", "1", "Real"),
|
||||
] {
|
||||
let _ = writeln!(d, "{value} /{kind} ({key}) ,");
|
||||
for ((key, kind), value) in ART_SETTINGS.iter().zip(art_settings(a)?) {
|
||||
let _ = writeln!(d, "{} /{kind} ({key}) ,", nums6(&[value]));
|
||||
}
|
||||
"(Art Brush) /Title ,\n"
|
||||
}
|
||||
|
||||
@@ -36,7 +36,13 @@
|
||||
//! - Scatter: the size (2, 3), spacing (5, 6) and scatter (8, 9) as (low, high) factors of the
|
||||
//! art, the rotation in degrees (11, 12), how each varies (4, 7, 10, 13) and whether the
|
||||
//! rotation follows the path (14), and 1 Tints (1).
|
||||
//! - Art: 1 Tints (2).
|
||||
//! - Art: numbers 2 to 15 are the settings a path's art style holds as entries ([`ArtSettings`]):
|
||||
//! the colorization (2: 0 None, 1 Tints; other codes color the art otherwise), the width as a factor
|
||||
//! (3), `Spacing` (4), `Adjustment` (5), flipped along (6) and across (7), Scale Proportionately
|
||||
//! (8), the lowest and highest width when the width follows the pen (9, 10, not drawn otherwise),
|
||||
//! how the width varies (11, 0 Fixed), Stretch Between Guides (12) and the lengths of the art
|
||||
//! before its first guide and after its second (13, 14, in the art's units, measured before
|
||||
//! the art is flipped along the path), and `FoldAdjustment` (15).
|
||||
//! - Pattern: its scale as a factor (3).
|
||||
//! - A Bristle brush's parameters start with 8 numbers: two codes, then the stiffness, bristle
|
||||
//! thickness, density, paint opacity and bristle length as fractions and the size in
|
||||
@@ -45,14 +51,15 @@
|
||||
//! [`super::read`]), usually in a group after its frame, an unpainted rectangle around it. A frame
|
||||
//! at the art's bounds isn't kept ([`without_frame`]); a wider one is.
|
||||
//!
|
||||
//! What isn't read keeps its default: the Art brush's other options, the pattern brush's tiles
|
||||
//! What isn't read keeps its default: an Art brush's settings that VectorCraft doesn't draw as the
|
||||
//! file does ([`art_options`]) apart from its colorization, the pattern brush's tiles
|
||||
//! (which slot is which tile isn't known) and the colorization of the other brushes, which is left
|
||||
//! at None so the art keeps its own colors.
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use kurbo::Affine;
|
||||
use vectorcraft_brush::{ArtBrush, Bristle, Brush, BrushKind, Calligraphic, Colorization, Scatter, Variation};
|
||||
use vectorcraft_brush::{ArtBrush, ArtScale, Bristle, Brush, BrushKind, Calligraphic, Colorization, Scatter, Variation};
|
||||
use vectorcraft_color::Paint;
|
||||
use vectorcraft_doc::{AppearanceItem, Node, NodeKind};
|
||||
|
||||
@@ -118,13 +125,13 @@ pub(super) enum DraftKind {
|
||||
brush: Scatter,
|
||||
art: String,
|
||||
},
|
||||
/// The art is the section named `art`. `plain`: the brush stretches its art along the path,
|
||||
/// unflipped, at 100% in Tints (see [`art_is_plain`]), so [`Self::Art`]'s `brush` draws as the
|
||||
/// file's brush does.
|
||||
/// The art is the section named `art`. `settings`: the brush's settings when [`art_options`]
|
||||
/// reads them, so [`Self::Art`]'s `brush` draws as the file's brush does (its guides set from
|
||||
/// its art's length by [`finish_brushes`]).
|
||||
Art {
|
||||
brush: ArtBrush,
|
||||
art: String,
|
||||
plain: bool,
|
||||
settings: Option<ArtSettings>,
|
||||
},
|
||||
/// The five art sections the brush names, in its order (which is which tile isn't known), and
|
||||
/// its scale (%).
|
||||
@@ -226,12 +233,17 @@ pub(super) fn calligraphic(values: [f64; 3], variation: [f64; 3], codes: [f64; 3
|
||||
Calligraphic { angle, roundness, size, variation, modes: Some(modes) }
|
||||
}
|
||||
|
||||
/// An Art brush's settings: its object's numbers 2 to 15, which a path's art style holds as the
|
||||
/// entries `Colorization`, `Scaling`, `Spacing`, `Adjustment`, `Counterclockwise`, `UpsideDown`,
|
||||
/// `Proportional`, `MinScaling`, `MaxScaling`, `ScaleYDependsOn`, `Segmented`,
|
||||
/// `StartSegmentLength`, `EndSegmentLength` and `FoldAdjustment`, in that order.
|
||||
pub(super) type ArtSettings = [f64; 14];
|
||||
|
||||
/// The settings a path's art style gives its stroke's brush.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(super) enum PathBrush {
|
||||
Calligraphic(Calligraphic),
|
||||
/// An Art brush whose art stretches along the path, at the brush's own width, in Tints.
|
||||
Art,
|
||||
Art(ArtSettings),
|
||||
}
|
||||
|
||||
/// Does an Art brush draw `art` as the file does? It bends paths in solid colors: other objects
|
||||
@@ -250,8 +262,9 @@ pub(super) fn art_bends(art: &Node) -> bool {
|
||||
bends
|
||||
}
|
||||
|
||||
/// The art sections of `drafts`' Art brushes that an Art brush draws as the file does.
|
||||
pub(super) fn bending_art(drafts: &[BrushDraft], art: &HashMap<String, Node>) -> HashSet<String> {
|
||||
/// The art sections of `drafts`' Art brushes that an Art brush draws as the file does, with the
|
||||
/// length of each (its width, the art running left to right).
|
||||
pub(super) fn bending_art(drafts: &[BrushDraft], art: &HashMap<String, Node>) -> HashMap<String, f64> {
|
||||
let names: HashSet<&String> = drafts
|
||||
.iter()
|
||||
.filter_map(|d| match &d.kind {
|
||||
@@ -259,13 +272,15 @@ pub(super) fn bending_art(drafts: &[BrushDraft], art: &HashMap<String, Node>) ->
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
names.into_iter().filter(|n| art.get(*n).is_some_and(art_bends)).cloned().collect()
|
||||
names.into_iter().filter_map(|n| art.get(n).filter(|a| art_bends(a)).and_then(|a| a.geometric_bounds()).map(|b| (n.clone(), b.width()))).collect()
|
||||
}
|
||||
|
||||
/// Does the brush `draft` draw as `settings`, the settings a path's art style gives it? A path whose
|
||||
/// settings differ (Options of Selected Object) keeps its drawn look: a stroke names a brush, and
|
||||
/// has no settings of its own. `bending`: [`bending_art`].
|
||||
pub(super) fn draws_as(draft: &BrushDraft, settings: &PathBrush, bending: &HashSet<String>) -> bool {
|
||||
/// has no settings of its own. An Art brush's settings that don't draw ([`drawn`]) may differ, and
|
||||
/// the path's own settings must be ones the brush would open with ([`art_options`], [`opens_live`]).
|
||||
/// `bending`: [`bending_art`].
|
||||
pub(super) fn draws_as(draft: &BrushDraft, settings: &PathBrush, bending: &HashMap<String, f64>) -> bool {
|
||||
let close = |a: f64, b: f64| (a - b).abs() <= 1e-3 * a.abs().max(b.abs()).max(1.0);
|
||||
match (&draft.kind, settings) {
|
||||
(DraftKind::Calligraphic(d), PathBrush::Calligraphic(c)) => {
|
||||
@@ -275,16 +290,90 @@ pub(super) fn draws_as(draft: &BrushDraft, settings: &PathBrush, bending: &HashS
|
||||
&& d.variation.iter().zip(&c.variation).all(|(a, b)| close(*a, *b))
|
||||
&& d.modes() == c.modes()
|
||||
}
|
||||
(DraftKind::Art { plain: true, art: name, .. }, PathBrush::Art) => bending.contains(name),
|
||||
(DraftKind::Art { settings: Some(d), art: name, .. }, PathBrush::Art(s)) => {
|
||||
let (dd, ds) = (drawn(d), drawn(s));
|
||||
art_options(s).is_some()
|
||||
&& bending.get(name).is_some_and(|length| {
|
||||
// The guides match as lengths and as fractions of the art's length, so they differ
|
||||
// neither on short art nor near the ends of long art.
|
||||
dd.iter().zip(ds).all(|(a, b)| close(*a, b))
|
||||
&& [11, 12].iter().all(|&i| close(dd[i] / length, ds[i] / length))
|
||||
&& opens_live(d, *length)
|
||||
&& opens_live(s, *length)
|
||||
})
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Are an Art brush's last 15 numbers those of a brush that stretches its art along the path,
|
||||
/// unflipped, at 100% in Tints? The numbers aren't read otherwise: any other value keeps its paths'
|
||||
/// drawn look.
|
||||
fn art_is_plain(numbers: &[f64; 15]) -> bool {
|
||||
*numbers == [1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
|
||||
/// Art brush settings `s` with those that don't draw set to 0: the lowest and highest width while the
|
||||
/// width is fixed, and the guides' lengths without Stretch Between Guides.
|
||||
fn drawn(s: &ArtSettings) -> ArtSettings {
|
||||
let mut out = *s;
|
||||
if s[9] == 0.0 {
|
||||
(out[7], out[8]) = (0.0, 0.0);
|
||||
}
|
||||
if s[10] == 0.0 {
|
||||
(out[11], out[12]) = (0.0, 0.0);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// What an Art brush's settings give VectorCraft's Art brush, its guides apart ([`guides`]): `None`
|
||||
/// when VectorCraft doesn't draw them as the file does: a colorization other than None or Tints, a
|
||||
/// width outside [`ArtBrush::WIDTH_RANGE`] or one that varies, both Scale Proportionately and Stretch
|
||||
/// Between Guides, a flip or scale that isn't 0 or 1, or a `Spacing`, `Adjustment` or
|
||||
/// `FoldAdjustment` other than 0, 1 and 1 (what other values change isn't known).
|
||||
fn art_options(s: &ArtSettings) -> Option<ArtBrush> {
|
||||
let [colorization, scaling, spacing, adjustment, along, across, proportional, _, _, varies, segmented, _, _, fold] = *s;
|
||||
let switch = |v: f64| match v {
|
||||
0.0 => Some(false),
|
||||
1.0 => Some(true),
|
||||
_ => None,
|
||||
};
|
||||
let colorization = match colorization {
|
||||
0.0 => Colorization::None,
|
||||
1.0 => Colorization::Tints,
|
||||
_ => return None,
|
||||
};
|
||||
let width = scaling * 100.0;
|
||||
let (low, high) = ArtBrush::WIDTH_RANGE;
|
||||
if !(low..=high).contains(&width) || varies != 0.0 || spacing != 0.0 || adjustment != 1.0 || fold != 1.0 {
|
||||
return None;
|
||||
}
|
||||
let scale = match (switch(proportional)?, switch(segmented)?) {
|
||||
(false, false) => ArtScale::Stretch,
|
||||
(true, false) => ArtScale::Proportional,
|
||||
(false, true) => ArtScale::BetweenGuides { start: 0.0, end: 1.0 },
|
||||
(true, true) => return None,
|
||||
};
|
||||
Some(ArtBrush { scale, width, flip_along: switch(along)?, flip_across: switch(across)?, colorization, ..ArtBrush::default() })
|
||||
}
|
||||
|
||||
/// The guides of Art brush settings `s` (with Stretch Between Guides) for art of length `length`, as
|
||||
/// [`ArtScale::BetweenGuides`] fractions of the art as it runs along the path: `None` when they leave
|
||||
/// no art between them. The settings measure the art before it is flipped along the path.
|
||||
fn guides(s: &ArtSettings, length: f64) -> Option<(f64, f64)> {
|
||||
let (before, after) = (s[11], s[12]);
|
||||
if !(before >= 0.0 && after >= 0.0 && before + after < length) {
|
||||
return None;
|
||||
}
|
||||
let (before, after) = if s[4] == 1.0 { (after, before) } else { (before, after) };
|
||||
Some((before / length, 1.0 - after / length))
|
||||
}
|
||||
|
||||
/// Do Art brush settings `s` draw art of length `length`: no Stretch Between Guides, or guides with
|
||||
/// art between them?
|
||||
fn guides_fit(s: &ArtSettings, length: f64) -> bool {
|
||||
s[10] == 0.0 || guides(s, length).is_some()
|
||||
}
|
||||
|
||||
/// Does a path with Art brush settings `s`, its art of length `length`, open with the brush on its
|
||||
/// stroke? Its guides must fit ([`guides_fit`]). Scale Proportionately keeps the drawn look:
|
||||
/// VectorCraft's Art brush sizes proportional art by the path's length and not by the stroke's
|
||||
/// weight.
|
||||
fn opens_live(s: &ArtSettings, length: f64) -> bool {
|
||||
s[6] == 0.0 && guides_fit(s, length)
|
||||
}
|
||||
|
||||
/// The brush named `name` of kind `kind` from its parameters `params`.
|
||||
@@ -342,15 +431,22 @@ pub(super) fn parse_tool_object(kind: Tool, name: &str, params: &str) -> Result<
|
||||
Tool::Art => {
|
||||
// The older form's 8 numbers aren't known: its brush keeps the defaults and its
|
||||
// paths their drawn look. The count tells the form, not the first number.
|
||||
// Any other count isn't a form this reads.
|
||||
let (colorization, plain) = if let Ok(n15) = <&[f64; 15]>::try_from(n.as_slice()) {
|
||||
(if n15[1] == 1.0 { Colorization::Tints } else { Colorization::None }, art_is_plain(n15))
|
||||
// Any other count isn't a form this reads. Settings that aren't read keep the
|
||||
// defaults but the colorization, and their paths their drawn look.
|
||||
let (brush, settings) = if let Ok(&[first, ref settings @ ..]) = <&[f64; 15]>::try_from(n.as_slice()) {
|
||||
match art_options(settings).filter(|_| first == 1.0) {
|
||||
Some(brush) => (brush, Some(*settings)),
|
||||
None => {
|
||||
let colorization = if settings[0] == 1.0 { Colorization::Tints } else { Colorization::None };
|
||||
(ArtBrush { colorization, ..ArtBrush::default() }, None)
|
||||
}
|
||||
}
|
||||
} else if n.len() == 8 {
|
||||
(ArtBrush::default().colorization, false)
|
||||
(ArtBrush::default(), None)
|
||||
} else {
|
||||
return Err(BrushError::Values);
|
||||
};
|
||||
DraftKind::Art { brush: ArtBrush { colorization, ..ArtBrush::default() }, art: art()?, plain }
|
||||
DraftKind::Art { brush, art: art()?, settings }
|
||||
}
|
||||
_ => {
|
||||
let [_, _, p3, ..] = n.as_slice() else { return Err(BrushError::Values) };
|
||||
@@ -453,10 +549,15 @@ pub(super) fn finish_brushes_within(
|
||||
_ if !kept => None,
|
||||
DraftKind::Calligraphic(c) => Some(BrushKind::Calligraphic(c)),
|
||||
DraftKind::Scatter { brush, art: name } => copy(&name).map(|a| BrushKind::Scatter(Scatter { art: a, ..brush })),
|
||||
DraftKind::Art { brush, art: name, .. } => copy(&name).map(|mut a| {
|
||||
DraftKind::Art { mut brush, art: name, settings } => copy(&name).map(|mut a| {
|
||||
// The side of the section with y up draws on the right of the path, and a brush
|
||||
// draws art's y (down here) on the left of the path.
|
||||
a.transform(Affine::scale_non_uniform(1.0, -1.0), false);
|
||||
if let ArtScale::BetweenGuides { start, end } = &mut brush.scale {
|
||||
// Guides that leave no art between them stretch the whole art.
|
||||
let length = a.geometric_bounds().map_or(0.0, |b| b.width());
|
||||
(*start, *end) = settings.and_then(|s| guides(&s, length)).unwrap_or((0.0, 1.0));
|
||||
}
|
||||
BrushKind::Art(ArtBrush { art: a, ..brush })
|
||||
}),
|
||||
DraftKind::Pattern { .. } => None,
|
||||
@@ -586,8 +687,8 @@ pub(crate) struct BrushRead {
|
||||
pub brush: Brush,
|
||||
/// A path whose art style names the brush, at the brush's own settings, opens with the brush on
|
||||
/// its stroke: a Calligraphic brush, or an Art brush whose art an Art brush draws as the file
|
||||
/// does ([`art_bends`]) and whose numbers are those of a brush that stretches its art along the
|
||||
/// path, unflipped, at 100% in Tints.
|
||||
/// does ([`art_bends`]) and whose settings VectorCraft draws as the file does ([`art_options`],
|
||||
/// with guides that leave art between them and without Scale Proportionately: [`opens_live`]).
|
||||
pub live: bool,
|
||||
}
|
||||
|
||||
@@ -604,7 +705,9 @@ pub(crate) fn read_brush_object(plugin: &str, name: &str, params: &str, art: Opt
|
||||
let live = kept(&drafts, §ions).first().copied().unwrap_or(false)
|
||||
&& match &drafts[0].kind {
|
||||
DraftKind::Calligraphic(_) => true,
|
||||
DraftKind::Art { plain: true, art: section, .. } => bending_art(&drafts, §ions).contains(section),
|
||||
DraftKind::Art { settings: Some(s), art: section, .. } => {
|
||||
bending_art(&drafts, §ions).get(section).is_some_and(|length| opens_live(s, *length))
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
let [draft] = drafts;
|
||||
@@ -643,8 +746,8 @@ pub(crate) struct BrushesRead {
|
||||
/// The brush a stroke that names a brush is drawn with, by name: the first one kept
|
||||
/// ([`kept`]), as an index in [`Self::drafts`].
|
||||
by_name: HashMap<String, usize>,
|
||||
/// The art an Art brush draws as the file does ([`bending_art`]).
|
||||
bending: HashSet<String>,
|
||||
/// The art an Art brush draws as the file does, with its length ([`bending_art`]).
|
||||
bending: HashMap<String, f64>,
|
||||
}
|
||||
|
||||
impl BrushesRead {
|
||||
@@ -681,7 +784,7 @@ impl BrushesRead {
|
||||
st.items.iter().all(|item| match item {
|
||||
StyleItem::Stroke { brush: Some((name, settings)), .. } => {
|
||||
let Some(draft) = self.by_name.get(name).and_then(|i| self.drafts.get(*i)) else { return false };
|
||||
draws_as(draft, settings, &self.bending) && !(matches!(settings, PathBrush::Art) && !st.effects.is_empty())
|
||||
draws_as(draft, settings, &self.bending) && !(matches!(settings, PathBrush::Art(_)) && !st.effects.is_empty())
|
||||
}
|
||||
_ => true,
|
||||
})
|
||||
|
||||
@@ -438,8 +438,8 @@ struct BrushesKept {
|
||||
read: (usize, usize),
|
||||
/// Which of [`Reader::brush_drafts`] are kept as brushes.
|
||||
kept: Vec<bool>,
|
||||
/// The art an Art brush draws as the file does.
|
||||
bending: HashSet<String>,
|
||||
/// The art an Art brush draws as the file does, with its length.
|
||||
bending: HashMap<String, f64>,
|
||||
}
|
||||
|
||||
struct Reader<'a> {
|
||||
|
||||
@@ -1181,7 +1181,7 @@ mod tests {
|
||||
assert!(nodes > 0);
|
||||
let draft = |i: usize| brush::BrushDraft {
|
||||
name: format!("Brush {i}"),
|
||||
kind: brush::DraftKind::Art { brush: vectorcraft_brush::ArtBrush::default(), art: "A".to_string(), plain: false },
|
||||
kind: brush::DraftKind::Art { brush: vectorcraft_brush::ArtBrush::default(), art: "A".to_string(), settings: None },
|
||||
};
|
||||
let drafts: Vec<_> = (0..5).map(draft).collect();
|
||||
let (kept, left_out) = brush::finish_brushes_within(drafts.clone(), &reader.brush_art, None, 3 * nodes);
|
||||
|
||||
@@ -992,8 +992,8 @@ struct StylePaints<'a> {
|
||||
brushes: &'a [super::super::brush::BrushDraft],
|
||||
/// Which of [`Self::brushes`] are kept as brushes ([`super::super::brush::kept`]).
|
||||
kept: &'a [bool],
|
||||
/// The art an Art brush draws as the file does ([`super::super::brush::bending_art`]).
|
||||
bending: &'a HashSet<String>,
|
||||
/// The art an Art brush draws as the file does, with its length ([`super::super::brush::bending_art`]).
|
||||
bending: &'a HashMap<String, f64>,
|
||||
space: Affine,
|
||||
}
|
||||
|
||||
@@ -1270,7 +1270,7 @@ fn styled(object: &Node, look: &Node, st: &crate::import::ai::style::ArtStyle, p
|
||||
if !super::super::brush::draws_as(draft, settings, paints.bending) {
|
||||
return None;
|
||||
}
|
||||
let art = matches!(settings, super::super::brush::PathBrush::Art);
|
||||
let art = matches!(settings, super::super::brush::PathBrush::Art(_));
|
||||
let on_a_path = matches!(object.kind, NodeKind::Path { .. } | NodeKind::Compound { .. });
|
||||
if !on_a_path && (art || !slot) {
|
||||
return None;
|
||||
@@ -1570,7 +1570,7 @@ mod tests {
|
||||
let st = &styles["Inked"];
|
||||
let brushes = [parse_tool_object(Tool::Calligraphic, "N", "1 4 8 30 50 -150 210 2 5 6 0").unwrap()];
|
||||
let (gradients, patterns, names, v7_patterns, bending) =
|
||||
(HashMap::new(), HashMap::new(), ShownNames::default(), HashSet::new(), HashSet::new());
|
||||
(HashMap::new(), HashMap::new(), ShownNames::default(), HashSet::new(), HashMap::new());
|
||||
let paints = StylePaints {
|
||||
gradients: &gradients,
|
||||
patterns: &patterns,
|
||||
@@ -1616,7 +1616,7 @@ mod tests {
|
||||
// GUARDS: an image with a Warp in its art style came in as its drawn look.
|
||||
let warp = ("Adobe Deform", "1 /Int (DeformStyle) ,\n0.4 /Real (DeformValue) ,\n"); // brand-ok: the filter's name in the files
|
||||
let (gradients, patterns, names, v7_patterns, bending) =
|
||||
(HashMap::new(), HashMap::new(), ShownNames::default(), HashSet::new(), HashSet::new());
|
||||
(HashMap::new(), HashMap::new(), ShownNames::default(), HashSet::new(), HashMap::new());
|
||||
let paints = StylePaints {
|
||||
gradients: &gradients,
|
||||
patterns: &patterns,
|
||||
|
||||
@@ -1500,11 +1500,30 @@ const ART_ENTRIES: [&str; 18] = [
|
||||
"Adobe Effect Expand Before Version", // brand-ok: the entry's name in the files
|
||||
];
|
||||
|
||||
/// An Art brush's filter `f` as the stroke it paints: `None` when it isn't a stroke in a color, names
|
||||
/// no Art brush, holds an entry that isn't read, or its settings aren't those of art that stretches
|
||||
/// along the path, unflipped, at the brush's own width (`Scaling` 1), in Tints (`Colorization` 1),
|
||||
/// with `FoldAdjustment` 1, which is [`vectorcraft_brush::ArtBrush`]'s `overlap: false`: its drawn
|
||||
/// look stays.
|
||||
/// The entries of an Art brush's filter that hold its settings, in the order of
|
||||
/// [`super::brush::ArtSettings`], with how each is written and its value when the filter leaves it
|
||||
/// out (`None`: the filter must hold it).
|
||||
const ART_SETTINGS: [(&str, Kind, Option<f64>); 14] = [
|
||||
("Colorization", Kind::Int, None),
|
||||
("Scaling", Kind::Real, None),
|
||||
("Spacing", Kind::Real, Some(0.0)),
|
||||
("Adjustment", Kind::Int, Some(1.0)),
|
||||
("Counterclockwise", Kind::Int, Some(0.0)),
|
||||
("UpsideDown", Kind::Int, Some(0.0)),
|
||||
("Proportional", Kind::Bool, Some(0.0)),
|
||||
("MinScaling", Kind::Real, Some(1.0)),
|
||||
("MaxScaling", Kind::Real, Some(1.0)),
|
||||
("ScaleYDependsOn", Kind::Int, Some(0.0)),
|
||||
("Segmented", Kind::Bool, Some(0.0)),
|
||||
("StartSegmentLength", Kind::Real, Some(0.0)),
|
||||
("EndSegmentLength", Kind::Real, Some(0.0)),
|
||||
("FoldAdjustment", Kind::Bool, Some(1.0)),
|
||||
];
|
||||
|
||||
/// An Art brush's filter `f` as the stroke it paints, with its settings: `None` when it isn't a
|
||||
/// stroke in a color, names no Art brush, holds an entry that isn't read or a setting that isn't a
|
||||
/// number of its kind, or tints its art with a swatch's color: its drawn look stays. Whether the
|
||||
/// brush draws as these settings is [`super::brush::draws_as`]'s to say.
|
||||
fn art_stroke(f: &Filter) -> Option<StyleItem> {
|
||||
if !f.paints.0
|
||||
|| f.gradient
|
||||
@@ -1519,28 +1538,13 @@ fn art_stroke(f: &Filter) -> Option<StyleItem> {
|
||||
if plugin != ART_BRUSH {
|
||||
return None;
|
||||
}
|
||||
let off = |key: &str, kind: Kind| matches!(entry(f, key, kind), Ok(None | Some(0.0)));
|
||||
let plain = off("Proportional", Kind::Bool)
|
||||
&& off("Segmented", Kind::Bool)
|
||||
&& off("UpsideDown", Kind::Int)
|
||||
&& off("Counterclockwise", Kind::Int)
|
||||
&& off("ScaleYDependsOn", Kind::Int)
|
||||
&& off("Spacing", Kind::Real)
|
||||
&& off("StartSegmentLength", Kind::Real)
|
||||
&& off("EndSegmentLength", Kind::Real);
|
||||
if !plain || entry(f, "Adjustment", Kind::Int).ok()?.unwrap_or(1.0) != 1.0 || entry(f, "FoldAdjustment", Kind::Bool).ok()?.unwrap_or(1.0) != 1.0 {
|
||||
return None;
|
||||
}
|
||||
let one = |key: &str| entry(f, key, Kind::Real).is_ok_and(|v| v.unwrap_or(1.0) == 1.0);
|
||||
if entry(f, "Scaling", Kind::Real).ok()?? != 1.0 || !one("MinScaling") || !one("MaxScaling") {
|
||||
return None;
|
||||
}
|
||||
if entry(f, "Colorization", Kind::Int).ok()?? != 1.0 {
|
||||
return None;
|
||||
let mut settings = [0.0; 14];
|
||||
for (slot, (key, kind, missing)) in settings.iter_mut().zip(ART_SETTINGS) {
|
||||
*slot = entry(f, key, kind).ok()?.or(missing)?;
|
||||
}
|
||||
let paint = f.stroke.clone()?;
|
||||
// The tinted pieces are process colors, so a swatch's ink would be lost.
|
||||
if matches!(paint, Paint::Solid { swatch: Some(_), .. }) {
|
||||
if settings[0] == 1.0 && matches!(paint, Paint::Solid { swatch: Some(_), .. }) {
|
||||
return None;
|
||||
}
|
||||
Some(StyleItem::Stroke {
|
||||
@@ -1552,7 +1556,7 @@ fn art_stroke(f: &Filter) -> Option<StyleItem> {
|
||||
dash: None,
|
||||
profile: None,
|
||||
align: StrokeAlign::Center,
|
||||
brush: Some((name.clone(), PathBrush::Art)),
|
||||
brush: Some((name.clone(), PathBrush::Art(settings))),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -9797,7 +9797,7 @@ fn a_pattern_brushs_five_slots_are_kept() {
|
||||
fn an_older_art_brush_comes_in_with_its_art() {
|
||||
const OLD_RIBBON: &str = "1 / Ribbon art/ / / / / 0 1 / 1 0 1 0 1 0 0 0";
|
||||
let d = brush::parse_tool_object(brush::Tool::Art, "Ribbon", OLD_RIBBON).unwrap();
|
||||
assert!(matches!(d.kind, brush::DraftKind::Art { ref art, plain: false, .. } if art == "Ribbon art"), "{d:?}");
|
||||
assert!(matches!(d.kind, brush::DraftKind::Art { ref art, settings: None, .. } if art == "Ribbon art"), "{d:?}");
|
||||
let brush::DraftKind::Art { ref brush, .. } = d.kind else { panic!("art") };
|
||||
assert_eq!(brush.colorization, vectorcraft_brush::ArtBrush::default().colorization, "the default colorization");
|
||||
// A count of numbers neither form has isn't read.
|
||||
@@ -9807,8 +9807,8 @@ fn an_older_art_brush_comes_in_with_its_art() {
|
||||
assert_eq!(brush::parse_tool_object(brush::Tool::Scatter, "Spray", "1 /Dot art/ 0 1 / 1 0 1 0 1 0 0 0"), Err(brush::BrushError::Values));
|
||||
// 8 numbers are the older form whatever the first number says: read with the defaults.
|
||||
let d = brush::parse_tool_object(brush::Tool::Art, "Ribbon", "0 / Ribbon art/ / / / / 0 1 / 1 0 1 0 1 0 0 0").unwrap();
|
||||
let brush::DraftKind::Art { ref brush, ref art, plain } = d.kind else { panic!("art") };
|
||||
assert_eq!((art.as_str(), plain, brush.colorization), ("Ribbon art", false, vectorcraft_brush::ArtBrush::default().colorization));
|
||||
let brush::DraftKind::Art { ref brush, ref art, settings } = d.kind else { panic!("art") };
|
||||
assert_eq!((art.as_str(), settings, brush.colorization), ("Ribbon art", None, vectorcraft_brush::ArtBrush::default().colorization));
|
||||
let objects = [plugin_object(ART_BRUSH, "Ribbon", OLD_RIBBON), plugin_object(ART_BRUSH, "Bare", "1 / / / / / / 0 1 / 1 0 1 0 1 0 0 0")].concat();
|
||||
let r = read_ok(&format!("{}{objects}{}", brush_art("Ribbon art", 4.0), layer("")));
|
||||
let lib = vectorcraft_brush::library(&r.doc);
|
||||
@@ -10471,9 +10471,109 @@ fn an_art_brush_bends_only_paths_in_solid_fills() {
|
||||
assert!(wrong.is_empty(), "{wrong:?}");
|
||||
}
|
||||
|
||||
/// The entries of an Art brush's filter that hold its settings, in the order of its object's numbers 2
|
||||
/// to 15, with how each is written.
|
||||
const ART_KEYS: [(&str, &str); 14] = [
|
||||
("Colorization", "Int"),
|
||||
("Scaling", "Real"),
|
||||
("Spacing", "Real"),
|
||||
("Adjustment", "Int"),
|
||||
("Counterclockwise", "Int"),
|
||||
("UpsideDown", "Int"),
|
||||
("Proportional", "Bool"),
|
||||
("MinScaling", "Real"),
|
||||
("MaxScaling", "Real"),
|
||||
("ScaleYDependsOn", "Int"),
|
||||
("Segmented", "Bool"),
|
||||
("StartSegmentLength", "Real"),
|
||||
("EndSegmentLength", "Real"),
|
||||
("FoldAdjustment", "Bool"),
|
||||
];
|
||||
|
||||
/// The Art brush `Ribbon` (its art 40 long: [`lopsided_art`]) whose settings, numbers 2 to 15 of its
|
||||
/// object, are those of [`RIBBON`] with `changes` (index in the 14, value), and the style `Inked` that
|
||||
/// gives a path the same settings.
|
||||
fn ribbon_with(changes: &[(usize, &str)]) -> (String, String) {
|
||||
let mut settings = ["1", "1", "0", "1", "0", "0", "0", "1", "1", "0", "0", "0", "0", "1"];
|
||||
for (i, v) in changes {
|
||||
settings[*i] = v;
|
||||
}
|
||||
let brush = format!("2 / Ribbon art/ / / / / 0 0/ 5 0 0 0 / 1 {}", settings.join(" "));
|
||||
let entries: Vec<(&str, &str, &str)> = ART_KEYS.iter().zip(settings).map(|((key, kind), value)| (*key, value, *kind)).collect();
|
||||
(format!("{}{}", lopsided_art("Ribbon art"), plugin_object(ART_BRUSH, "Ribbon", &brush)), art_style("Ribbon", 1.0, &entries, ""))
|
||||
}
|
||||
|
||||
/// A path with an Art brush whose settings are its own comes in brushed, the brush with those
|
||||
/// settings: None, another width (the lowest and highest widths aren't drawn while the width is
|
||||
/// fixed), flipped along or across, and Stretch Between Guides, whose guides come in as fractions of
|
||||
/// the art as it runs along the path (measured before it is flipped along it, so a flip along swaps
|
||||
/// them). Lengths without Stretch Between Guides aren't guides. A brush that scales its art
|
||||
/// proportionately comes into the panel with that option, and its path keeps its drawn look.
|
||||
#[test]
|
||||
fn an_art_brush_with_options_comes_in_brushed_with_them() {
|
||||
use vectorcraft_brush::{ArtBrush, ArtScale, Colorization};
|
||||
let check_file = |why: &str, (def, style): (String, String), want: &dyn Fn(&ArtBrush) -> bool| {
|
||||
let r = read_ok(&format!("{def}{style}{}", layer(&brushed_path())));
|
||||
let a = art(&r.doc);
|
||||
assert!(matches!(a[0].kind, NodeKind::Path { .. }), "{why}: the path, not its look: {a:#?}");
|
||||
assert_eq!(a[0].appearance.stroke().and_then(|s| s.brush.as_deref()), Some("Ribbon"), "{why}");
|
||||
let lib = vectorcraft_brush::library(&r.doc);
|
||||
let vectorcraft_brush::BrushKind::Art(brush) = brush_named(&lib, "Ribbon") else { panic!("{why}: art") };
|
||||
assert!(want(brush), "{why}: {brush:?}");
|
||||
r
|
||||
};
|
||||
let check = |why: &str, changes: &[(usize, &str)], want: &dyn Fn(&ArtBrush) -> bool| check_file(why, ribbon_with(changes), want);
|
||||
let near = |a: f64, b: f64| (a - b).abs() < 1e-9;
|
||||
// Settings that don't draw may differ between the brush and the path: the lowest and highest
|
||||
// width while the width is fixed, and lengths without Stretch Between Guides.
|
||||
let (def, style) = ribbon_with(&[(1, "2"), (7, "2"), (8, "2")]);
|
||||
let style = style.replace("2 /Real (MinScaling) ,\n", "").replace("2 /Real (MaxScaling) ,\n", "");
|
||||
check_file("the path leaves out the widths the pen would give", (def, style), &|b| b.width == 200.0);
|
||||
let (def, _) = ribbon_with(&[]);
|
||||
let (_, style) = ribbon_with(&[(11, "5"), (12, "10")]);
|
||||
check_file("lengths on the path alone", (def, style), &|b| b.scale == ArtScale::Stretch);
|
||||
check("its art's colors", &[(0, "0")], &|b| b.colorization == Colorization::None);
|
||||
check("twice as wide", &[(1, "2"), (7, "2"), (8, "2")], &|b| b.width == 200.0);
|
||||
check("half as wide, the pen's widths left", &[(1, "0.5")], &|b| b.width == 50.0 && b.width_mode.is_none());
|
||||
let along = check("flipped along", &[(4, "1")], &|b| b.flip_along && !b.flip_across);
|
||||
let across = check("flipped across", &[(5, "1")], &|b| b.flip_across && !b.flip_along);
|
||||
let (def, style) = ribbon_with(&[(6, "1")]);
|
||||
let r = read_ok(&format!("{def}{style}{}", layer(&brushed_path())));
|
||||
assert!(matches!(art(&r.doc)[0].kind, NodeKind::Group { .. }), "proportional: the drawn look");
|
||||
let lib = vectorcraft_brush::library(&r.doc);
|
||||
let vectorcraft_brush::BrushKind::Art(brush) = brush_named(&lib, "Ribbon") else { panic!("proportional: art") };
|
||||
assert_eq!(brush.scale, ArtScale::Proportional);
|
||||
check(
|
||||
"between guides",
|
||||
&[(10, "1"), (11, "5"), (12, "10")],
|
||||
&|b| matches!(b.scale, ArtScale::BetweenGuides { start, end } if near(start, 5.0 / 40.0) && near(end, 30.0 / 40.0)),
|
||||
);
|
||||
check("between guides, flipped along", &[(10, "1"), (11, "5"), (12, "10"), (4, "1")], &|b| {
|
||||
b.flip_along && matches!(b.scale, ArtScale::BetweenGuides { start, end } if near(start, 10.0 / 40.0) && near(end, 35.0 / 40.0))
|
||||
});
|
||||
check("lengths without guides", &[(11, "5"), (12, "10")], &|b| b.scale == ArtScale::Stretch);
|
||||
// Along a straight line, y down: unflipped, the wide block is above the line and the narrow one
|
||||
// below it, at the start.
|
||||
let line = kurbo::BezPath::from_vec(vec![kurbo::PathEl::MoveTo(Point::new(0.0, 0.0)), kurbo::PathEl::LineTo(Point::new(100.0, 0.0))]);
|
||||
let filled = |r: &Structure, x: f64, y: f64| {
|
||||
let lib = vectorcraft_brush::library(&r.doc);
|
||||
let st = art(&r.doc)[0].appearance.stroke().cloned().unwrap();
|
||||
let ribbon = lib.iter().find(|b| b.name == "Ribbon").unwrap();
|
||||
let pieces = vectorcraft_brush::stroke_pieces(&r.doc, ribbon, &line, &st);
|
||||
pieces.iter().any(|n| {
|
||||
let mut hit = false;
|
||||
n.walk(&mut |c| hit |= c.path_data().is_some_and(|p| kurbo::Shape::winding(&p.to_bezpath(), Point::new(x, y)) != 0));
|
||||
hit
|
||||
})
|
||||
};
|
||||
// At weight 1 the art is 8 across: each block is 4 across, on its side of the line.
|
||||
assert!(filled(&across, 50.0, 2.0) && filled(&across, 10.0, -2.0) && !filled(&across, 50.0, -2.0), "across: the wide block below");
|
||||
assert!(filled(&along, 50.0, -2.0) && filled(&along, 90.0, 2.0) && !filled(&along, 10.0, 2.0), "along: the narrow block at the end");
|
||||
}
|
||||
|
||||
/// An Art brushed path keeps its drawn look when the brush wouldn't draw it the way the file does:
|
||||
/// its settings aren't those of art that stretches along the path at the brush's own width and
|
||||
/// color, the brush's own aren't, its art isn't read, or the brush isn't one the panel keeps.
|
||||
/// its settings differ from the brush's, the brush's are ones VectorCraft doesn't draw as the file
|
||||
/// does, its art isn't read, or the brush isn't one the panel keeps.
|
||||
#[test]
|
||||
fn an_art_brushed_path_the_brush_cant_draw_keeps_its_drawn_look() {
|
||||
let good = format!("{}{}", lopsided_art("Ribbon art"), plugin_object(ART_BRUSH, "Ribbon", RIBBON));
|
||||
@@ -10516,21 +10616,7 @@ fn an_art_brushed_path_the_brush_cant_draw_keeps_its_drawn_look() {
|
||||
),
|
||||
plain_style.clone(),
|
||||
),
|
||||
(
|
||||
"a width other than the brush's 100%",
|
||||
format!("{}{}", lopsided_art("Ribbon art"), plugin_object(ART_BRUSH, "Ribbon", wide)),
|
||||
art_style(
|
||||
"Ribbon",
|
||||
1.0,
|
||||
&STRETCHED.map(|e| if matches!(e.0, "Scaling" | "MinScaling" | "MaxScaling") { (e.0, "2", e.2) } else { e }),
|
||||
"",
|
||||
),
|
||||
),
|
||||
(
|
||||
"a brush that keeps its art's colors",
|
||||
format!("{}{}", lopsided_art("Ribbon art"), plugin_object(ART_BRUSH, "Ribbon", own_colors)),
|
||||
art_style("Ribbon", 1.0, &STRETCHED.map(|e| if e.0 == "Colorization" { (e.0, "0", e.2) } else { e }), ""),
|
||||
),
|
||||
("a brush that keeps its art's colors, the path in Tints", format!("{}{}", lopsided_art("Ribbon art"), plugin_object(ART_BRUSH, "Ribbon", own_colors)), plain_style.clone()),
|
||||
("an entry that isn't read", good.clone(), art_style("Ribbon", 1.0, &STRETCHED, "1 /Int (SomeOtherSetting) ,\n")),
|
||||
(
|
||||
"an effect on the path",
|
||||
@@ -10543,17 +10629,16 @@ fn an_art_brushed_path_the_brush_cant_draw_keeps_its_drawn_look() {
|
||||
("proportional", good.clone(), style_with("Proportional", "1")),
|
||||
("between guides", good.clone(), style_with("Segmented", "1")),
|
||||
("its own width", good.clone(), style_with("Scaling", "2")),
|
||||
("a width that varies", good.clone(), style_with("MaxScaling", "2")),
|
||||
("a width on the pen", good.clone(), style_with("ScaleYDependsOn", "2")),
|
||||
("spacing", good.clone(), style_with("Spacing", "1")),
|
||||
("flipped", good.clone(), style_with("UpsideDown", "1")),
|
||||
("a spacing close to the brush's 0", good.clone(), style_with("Spacing", "0.0005")),
|
||||
("flipped",good.clone(), style_with("UpsideDown", "1")),
|
||||
("flipped the other way", good.clone(), style_with("Counterclockwise", "1")),
|
||||
("another adjustment", good.clone(), style_with("Adjustment", "2")),
|
||||
("no fold adjustment", good.clone(), style_with("FoldAdjustment", "0")),
|
||||
("its own colorization", good.clone(), style_with("Colorization", "0")),
|
||||
("tints and shades", good.clone(), style_with("Colorization", "2")),
|
||||
("hue shift", good.clone(), style_with("Colorization", "3")),
|
||||
("start of a guide", good.clone(), art_style("Ribbon", 1.0, &STRETCHED, "5 /Real (StartSegmentLength) ,\n")),
|
||||
("variable width", good.clone(), art_style("Ribbon", 1.0, &STRETCHED, "2 /Int (BSWidthSource) ,\n")),
|
||||
(
|
||||
"width arrays",
|
||||
@@ -10570,6 +10655,35 @@ fn an_art_brushed_path_the_brush_cant_draw_keeps_its_drawn_look() {
|
||||
("art not read", plugin_object(ART_BRUSH, "Ribbon", RIBBON), plain_style.clone()),
|
||||
("no such brush", good.clone(), art_style("Other", 1.0, &STRETCHED, "")),
|
||||
];
|
||||
// The brush's own settings, which the path shares, but VectorCraft doesn't draw as the file does.
|
||||
let own = [
|
||||
("both brush and path: tints and shades", &[(0, "2")][..]),
|
||||
("both: hue shift", &[(0, "3")]),
|
||||
("both: spacing", &[(2, "1")]),
|
||||
("both: another adjustment", &[(3, "2")]),
|
||||
("both: no fold adjustment", &[(13, "0")]),
|
||||
("both: a width on the pen", &[(9, "1"), (7, "0.5"), (8, "2")]),
|
||||
("both: a width past the range", &[(1, "20"), (7, "20"), (8, "20")]),
|
||||
("both: a flip of another value", &[(4, "2")]),
|
||||
("both: proportional", &[(6, "1")]),
|
||||
("both: proportional and between guides", &[(6, "1"), (10, "1"), (11, "5"), (12, "10")]),
|
||||
("both: guides that leave no art between them", &[(10, "1"), (11, "30"), (12, "10")]),
|
||||
("both: a guide past the start", &[(10, "1"), (11, "-5"), (12, "10")]),
|
||||
];
|
||||
let mut cases: Vec<(&str, String, String)> = cases
|
||||
.into_iter()
|
||||
.chain(own.iter().map(|(why, changes)| {
|
||||
let (def, style) = ribbon_with(changes);
|
||||
(*why, def, style)
|
||||
}))
|
||||
.collect();
|
||||
// The brush's guides leave art between them, and the path's, close to them, leave none.
|
||||
let (def, style) = ribbon_with(&[(10, "1"), (11, "20"), (12, "19.99")]);
|
||||
cases.push(("path guides close to the brush's that leave no art between them", def, style.replace("19.99 /", "20 /")));
|
||||
// On art 0.01 long, a guide at 0.0035 is 35% of the way along it, and the brush's at 0.003 is 30%.
|
||||
let (def, style) = ribbon_with(&[(10, "1"), (11, "0.003"), (12, "0.002")]);
|
||||
let short = format!("%AI8_BeginBrushPattern\n(Ribbon art)\n0 A\n0 g\n{}f\n%AI8_EndBrushPattern\n", rect_path(0.0, 0.0, 0.01, 4.0));
|
||||
cases.push(("path guides elsewhere on short art", def.replace(&lopsided_art("Ribbon art"), &short), style.replace("0.003 /", "0.0035 /")));
|
||||
let mut wrong = vec![];
|
||||
for (why, def, style) in cases {
|
||||
let r = read_ok(&format!("{def}{style}{}", layer(&brushed_path())));
|
||||
|
||||
+64
-19
@@ -92,6 +92,19 @@ struct ImageCode {
|
||||
}
|
||||
|
||||
impl ImageCode {
|
||||
/// A hash of everything that is written for the image: equal images have equal keys.
|
||||
fn content_key(&self) -> u64 {
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut h = std::collections::hash_map::DefaultHasher::new();
|
||||
(&self.prelude, &self.dict, self.filter, self.op, &self.ascii).hash(&mut h);
|
||||
h.finish()
|
||||
}
|
||||
|
||||
/// Is everything written for `other` the same as for this image?
|
||||
fn same_image(&self, other: &ImageCode) -> bool {
|
||||
self.prelude == other.prelude && self.dict == other.dict && self.filter == other.filter && self.op == other.op && self.ascii == other.ascii
|
||||
}
|
||||
|
||||
/// The operators and data that draw the image where it is placed.
|
||||
fn push_inline(&self, s: &mut String) {
|
||||
s.push_str(&self.prelude);
|
||||
@@ -253,6 +266,9 @@ pub(crate) struct Scene<'a> {
|
||||
images: HashMap<String, Option<usize>>,
|
||||
/// The source key and solid paint together identify a colorized encoding.
|
||||
colorized_images: HashMap<(String, String), Option<usize>>,
|
||||
/// Freeform gradients' images by [`ImageCode::content_key`] → their indices in `codes`: a field
|
||||
/// encoded the same as one already kept draws from it.
|
||||
fields: HashMap<u64, Vec<usize>>,
|
||||
/// The encoded images.
|
||||
codes: Vec<ImageCode>,
|
||||
/// Where in `body` each placement of an encoded image goes (in order) and the index of its code.
|
||||
@@ -281,6 +297,7 @@ impl<'a> Scene<'a> {
|
||||
brushes: None,
|
||||
images: HashMap::new(),
|
||||
colorized_images: HashMap::new(),
|
||||
fields: HashMap::new(),
|
||||
codes: vec![],
|
||||
placements: vec![],
|
||||
pending: 0,
|
||||
@@ -478,7 +495,7 @@ impl<'a> Scene<'a> {
|
||||
_ => {}
|
||||
}
|
||||
self.note_losses(n);
|
||||
let (was, placed) = (self.body.len(), self.placements.len());
|
||||
let (was, placed, codes) = (self.body.len(), self.placements.len(), self.codes.len());
|
||||
match &n.kind {
|
||||
NodeKind::Layer { children, clip: false, .. } | NodeKind::Group { children, clip: false } => {
|
||||
children.iter().for_each(|c| self.node(c, false));
|
||||
@@ -536,6 +553,7 @@ impl<'a> Scene<'a> {
|
||||
if self.size() > self.max_body {
|
||||
self.body.truncate(was);
|
||||
self.unplace(placed);
|
||||
self.forget_fields(codes);
|
||||
self.warn(TOO_LARGE);
|
||||
}
|
||||
}
|
||||
@@ -1364,6 +1382,14 @@ impl<'a> Scene<'a> {
|
||||
if !(long > 1e-9 && long.is_finite()) {
|
||||
return;
|
||||
}
|
||||
let place = Affine::translate(area.origin().to_vec2()) * Affine::scale_non_uniform(area.width(), area.height());
|
||||
if !invertible(place) {
|
||||
return;
|
||||
}
|
||||
if self.size().saturating_add(64) > self.max_body {
|
||||
self.warn(TOO_LARGE);
|
||||
return;
|
||||
}
|
||||
let k = (self.doc.raster_effects_ppi / 72.0).min(MAX_FIELD_PX / long);
|
||||
let along = |len: f64| (len * k).ceil().clamp(1.0, MAX_FIELD_PX) as u16;
|
||||
let (cols, rows) = (along(area.width()), along(area.height()));
|
||||
@@ -1383,9 +1409,43 @@ impl<'a> Scene<'a> {
|
||||
let rgba: Vec<u8> = field.display_grid(area, cols, rows).flat_map(|(c, a)| [q(c[0]), q(c[1]), q(c[2]), q(a)]).collect();
|
||||
self.encode_image(&rgba, w, h)
|
||||
};
|
||||
let place = Affine::translate(area.origin().to_vec2()) * Affine::scale_non_uniform(area.width(), area.height());
|
||||
if let Some(image) = image {
|
||||
self.place_inline(&image, place);
|
||||
let Some(image) = image else { return };
|
||||
let key = image.content_key();
|
||||
let found =
|
||||
self.fields.get(&key).and_then(|bucket| bucket.iter().copied().find(|&i| self.codes.get(i).is_some_and(|c| c.same_image(&image))));
|
||||
let i = match found {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
// A field that can't be placed isn't kept.
|
||||
if self.size().saturating_add(image.inline_len).saturating_add(64) > self.max_body {
|
||||
self.warn(TOO_LARGE);
|
||||
return;
|
||||
}
|
||||
let i = self.add_code(image);
|
||||
self.fields.entry(key).or_default().push(i);
|
||||
i
|
||||
}
|
||||
};
|
||||
self.place_image(i, place);
|
||||
}
|
||||
|
||||
/// Forget the field images after the first `n` codes that no placement draws any longer, last
|
||||
/// first, up to the first code that is drawn or isn't a field image.
|
||||
fn forget_fields(&mut self, n: usize) {
|
||||
while self.codes.len() > n {
|
||||
let i = self.codes.len() - 1;
|
||||
let Some(code) = self.codes.get(i) else { break };
|
||||
if code.uses > 0 {
|
||||
break;
|
||||
}
|
||||
let key = code.content_key();
|
||||
let Some(bucket) = self.fields.get_mut(&key) else { break };
|
||||
let Some(at) = bucket.iter().position(|&j| j == i) else { break };
|
||||
bucket.swap_remove(at);
|
||||
if bucket.is_empty() {
|
||||
self.fields.remove(&key);
|
||||
}
|
||||
self.codes.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1595,21 +1655,6 @@ impl<'a> Scene<'a> {
|
||||
self.line("Q");
|
||||
}
|
||||
|
||||
/// Draw `code`, an image drawn nowhere else, on the unit square mapped by `place`.
|
||||
fn place_inline(&mut self, code: &ImageCode, place: Affine) {
|
||||
if !invertible(place) {
|
||||
return;
|
||||
}
|
||||
if self.size().saturating_add(code.inline_len).saturating_add(64) > self.max_body {
|
||||
self.warn(TOO_LARGE);
|
||||
return;
|
||||
}
|
||||
self.line("q");
|
||||
let _ = writeln!(self.body, "{} cm", ps::matrix(place));
|
||||
code.push_inline(&mut self.body);
|
||||
self.line("Q");
|
||||
}
|
||||
|
||||
/// Straight RGBA pixels (`w` × `h`, rows from the top) as the operators and data that draw
|
||||
/// them on the unit square: in the process colour space, transparent pixels masked out at
|
||||
/// Level 3 (a key colour no opaque pixel has), white at Level 2; partly transparent ones over
|
||||
|
||||
@@ -1264,6 +1264,104 @@ fn the_page_limit_counts_a_repeated_images_samples_once() {
|
||||
assert!(page.body.len() + page.setup.len() <= inline + 1000 + 400, "{}", page.body.len());
|
||||
}
|
||||
|
||||
/// A freeform gradient of `points` (x, y and color).
|
||||
fn freeform(points: &[(f64, f64, Color)]) -> Paint {
|
||||
let mut g = GradientPaint::new(Gradient { kind: GradientKind::Freeform, ..Default::default() });
|
||||
g.freeform = Some(Freeform { points: points.iter().map(|&(x, y, c)| FreeformPoint::new(Point::new(x, y), c)).collect(), ..Default::default() });
|
||||
Paint::Gradient(Box::new(g))
|
||||
}
|
||||
|
||||
/// A square at (`x`, `y`) of side `side`, filled with a freeform gradient of `a` and `b` at a
|
||||
/// fifth and four fifths of its width.
|
||||
fn freeform_square(d: &mut Document, x: f64, y: f64, side: f64, a: Color, b: Color) -> Node {
|
||||
let mid = y + side / 2.0;
|
||||
let paint = freeform(&[(x + side / 5.0, mid, a), (x + side * 0.8, mid, b)]);
|
||||
path(d, shapes::rectangle(Rect::new(x, y, x + side, y + side)), paint, Paint::None, 0.0)
|
||||
}
|
||||
|
||||
/// A freeform gradient on a shape and on a moved copy of it is sampled the same for both: its
|
||||
/// image's samples are written once, at both levels, in RGB and in CMYK.
|
||||
#[test]
|
||||
fn a_freeform_gradients_image_drawn_twice_writes_its_samples_once() {
|
||||
let rgb = (Color::rgb(1.0, 0.0, 0.0), Color::rgb(0.0, 0.0, 1.0));
|
||||
let cmyk = (Color::cmyk(1.0, 0.0, 0.0, 0.0), Color::cmyk(0.0, 0.0, 0.0, 1.0));
|
||||
for (a, b) in [rgb, cmyk] {
|
||||
let single = doc_with(|d| vec![freeform_square(d, 0.0, 0.0, 64.0, a, b)]);
|
||||
let twice = doc_with(|d| vec![freeform_square(d, 0.0, 0.0, 64.0, a, b), freeform_square(d, 64.0, 64.0, 64.0, a, b)]);
|
||||
for level in Level::ALL {
|
||||
let one = export(&single, &EpsOptions { level, ..opts(&single) }, None, None).unwrap();
|
||||
let one_ps = text(&one);
|
||||
assert!(!one_ps.contains("VCim"), "a field drawn once is written where it is drawn: {one_ps}");
|
||||
let out = export(&twice, &EpsOptions { level, ..opts(&twice) }, None, None).unwrap();
|
||||
let ps = text(&out);
|
||||
checked_page(&ps);
|
||||
assert_eq!(reuses(&ps, 0, level), 2, "{ps}");
|
||||
assert!(!ps.contains("VCim1"), "{ps}");
|
||||
let body = &ps[ps.find("%%EndSetup").unwrap()..];
|
||||
assert!(!body.contains("currentfile") && !body.contains("~>"), "no samples in the page: {ps}");
|
||||
assert_eq!(reused_samples(&ps, level), inline_samples(&one_ps, level), "{level:?}");
|
||||
assert_eq!(out.warnings, one.warnings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Fields sampled differently are each written where they are drawn: different gradients, and one
|
||||
/// gradient on shapes of different sizes.
|
||||
#[test]
|
||||
fn freeform_gradients_sampled_differently_dont_share_an_image() {
|
||||
let (red, blue, green) = (Color::rgb(1.0, 0.0, 0.0), Color::rgb(0.0, 0.0, 1.0), Color::rgb(0.0, 1.0, 0.0));
|
||||
let colors = doc_with(|d| vec![freeform_square(d, 0.0, 0.0, 64.0, red, blue), freeform_square(d, 0.0, 0.0, 64.0, red, green)]);
|
||||
let sizes = doc_with(|d| {
|
||||
let paint = freeform(&[(20.0, 50.0, red), (80.0, 50.0, blue)]);
|
||||
vec![
|
||||
path(d, shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), paint.clone(), Paint::None, 0.0),
|
||||
path(d, shapes::rectangle(Rect::new(0.0, 0.0, 50.0, 100.0)), paint, Paint::None, 0.0),
|
||||
]
|
||||
});
|
||||
for d in [colors, sizes] {
|
||||
for level in Level::ALL {
|
||||
let ps = page_of(&d, &EpsOptions { level, ..opts(&d) });
|
||||
assert!(!ps.contains("VCim"), "{ps}");
|
||||
assert_eq!(ps.matches("} exec\n").count(), 2, "{ps}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A field's second placement that passes the page limit is left out, and the first is written
|
||||
/// where it is drawn. A field taken back with the shape that passed the limit leaves nothing in the
|
||||
/// setup.
|
||||
#[test]
|
||||
fn a_field_taken_back_at_the_page_limit_leaves_no_image_behind() {
|
||||
let (red, blue) = (Color::rgb(1.0, 0.0, 0.0), Color::rgb(0.0, 0.0, 1.0));
|
||||
let single = doc_with(|d| vec![freeform_square(d, 0.0, 0.0, 64.0, red, blue)]);
|
||||
let o = opts(&single);
|
||||
let inline = crate::scene::Scene::new(&single, &o).run().body;
|
||||
let twice = doc_with(|d| vec![freeform_square(d, 0.0, 0.0, 64.0, red, blue), freeform_square(d, 0.0, 0.0, 64.0, red, blue)]);
|
||||
let page = crate::scene::Scene::new(&twice, &o).with_max_body(inline.len() + 50).run();
|
||||
assert!(page.warnings.iter().any(|w| w.contains("too large")), "{:?}", page.warnings);
|
||||
assert!(page.body.starts_with(&inline), "{}", page.body);
|
||||
assert_eq!(page.body.matches("} exec\n").count(), 1, "{}", page.body);
|
||||
assert!(!page.setup.contains("VCim"), "{}", page.setup);
|
||||
// A many-sided shape whose stroke passes the limit after its fill's field was placed.
|
||||
let big = |d: &mut Document, stroke: Paint| {
|
||||
let paint = freeform(&[(60.0, 100.0, Color::rgb(0.0, 1.0, 0.0)), (140.0, 100.0, red)]);
|
||||
path(d, shapes::polygon(Point::new(100.0, 100.0), 90.0, 2000, 0.0), paint, stroke, 1.0)
|
||||
};
|
||||
let filled = doc_with(|d| vec![big(d, Paint::None)]);
|
||||
let stroked = doc_with(|d| vec![big(d, Paint::solid(Color::rgb(0.0, 0.0, 0.0)))]);
|
||||
let fill = crate::scene::Scene::new(&filled, &o).run().body.len();
|
||||
let both = crate::scene::Scene::new(&stroked, &o).run().body.len();
|
||||
assert!(both > fill + 20_000, "{fill} {both}");
|
||||
let d = doc_with(|d| {
|
||||
let s = big(d, Paint::solid(Color::rgb(0.0, 0.0, 0.0)));
|
||||
vec![s, freeform_square(d, 0.0, 0.0, 64.0, red, blue), freeform_square(d, 0.0, 0.0, 64.0, red, blue)]
|
||||
});
|
||||
let page = crate::scene::Scene::new(&d, &o).with_max_body(fill + (both - fill) / 2).run();
|
||||
assert!(page.warnings.iter().any(|w| w.contains("too large")), "{:?}", page.warnings);
|
||||
assert_eq!(reuses(&page.body, 0, Level::Three), 2, "{}", page.body);
|
||||
assert!(page.setup.contains("/VCim0 VCsrc") && !page.setup.contains("VCim1"), "{}", page.setup);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_length_and_ascii85_round_trip() {
|
||||
let cycle: Vec<u8> = (0..=255u8).cycle().take(1000).collect();
|
||||
|
||||
@@ -1330,7 +1330,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"file.exportAs",
|
||||
"Export As…",
|
||||
"",
|
||||
"{format?, useArtboards?, range?} opens Export As (then the format's options); with {path, …document.export options} writes the file(s) → {path, warnings, files?}; under automation read roots, an SVG or SVGZ is refused while the document links a JavaScript file outside them, as document.export refuses it",
|
||||
"{format?, useArtboards?, range?} opens Export As (then the format's options); with {path, …document.export options} writes the file(s) → {path, warnings, files?}; exportSelection: true writes the selection alone, cropped to it, as document.exportSelection writes it (artboard options are not used); under automation read roots, an SVG or SVGZ is refused while the document links a JavaScript file outside them, as document.export refuses it",
|
||||
),
|
||||
(
|
||||
"ui.fileInfoDialog",
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -46,7 +46,7 @@ use exporter::{MAX_COORD, writable, xf};
|
||||
#[cfg(test)]
|
||||
use images::{png_8_bit, png_16_bit};
|
||||
#[cfg(test)]
|
||||
pub(crate) use nodes::MEASURES;
|
||||
pub(crate) use nodes::{KNOCKOUT_BRUSHES, KNOCKOUT_SHAPES, MAX_KNOCKOUT_MASK_OBJECTS, MEASURES};
|
||||
pub(crate) use writer::{Sheet, Writer};
|
||||
|
||||
pub(crate) struct Exporter<'a> {
|
||||
@@ -127,6 +127,14 @@ pub(crate) struct Exporter<'a> {
|
||||
/// The most [`Self::symbol_nodes`] may reach: [`vectorcraft_doc::MAX_SYMBOL_NODES`], or less
|
||||
/// while one of the copies on a sheet is drawn ([`Writer::page`]).
|
||||
pub(crate) symbol_limit: u64,
|
||||
/// Objects the symbol instances in the copies drawn in knockout masks held, together: counted
|
||||
/// apart from [`Self::symbol_nodes`], within [`vectorcraft_doc::MAX_SYMBOL_NODES`] of their own.
|
||||
mask_symbol_nodes: u64,
|
||||
/// Whether a knockout mask is being drawn: [`Self::symbol_nodes`] then counts
|
||||
/// [`Self::mask_symbol_nodes`].
|
||||
in_knockout_mask: bool,
|
||||
/// Whether a knockout mask left out symbol art because the objects its copies may hold ran out.
|
||||
mask_art_left_out: bool,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -54,6 +54,9 @@ impl<'a> Exporter<'a> {
|
||||
paper: Rect::ZERO,
|
||||
symbol_nodes: 0,
|
||||
symbol_limit: vectorcraft_doc::MAX_SYMBOL_NODES,
|
||||
mask_symbol_nodes: 0,
|
||||
in_knockout_mask: false,
|
||||
mask_art_left_out: false,
|
||||
overprint: false,
|
||||
overprinted: false,
|
||||
invisible: false,
|
||||
|
||||
+545
-62
@@ -1,7 +1,9 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::exporter::{UNDECODED, address, blend, color, constant_mask, cover, in_range, norm, rects_overlap, rule, to_path, writable, xf};
|
||||
use super::{
|
||||
Affine, AppearanceItem, BezPath, BlendMode, Exporter, Fill, FillRule, GradientKind, LineCap, LineJoin, Mark, Node, NodeKind, NormalizedF32,
|
||||
Paint, Path, Rect, Shape, Stroke, StrokeAlign, StrokeDash, StrokeLayer, Surface, WrittenShape, krilla, stroke,
|
||||
Affine, AppearanceItem, BezPath, BlendMode, Document, Exporter, Fill, FillRule, GradientKind, LineCap, LineJoin, Mark, Node, NodeKind,
|
||||
NormalizedF32, Paint, Path, Rect, Shape, Stroke, StrokeAlign, StrokeDash, StrokeLayer, Surface, WrittenShape, krilla, stroke,
|
||||
};
|
||||
|
||||
/// An object's visual box for leaving out what is off the page. A plain group or layer also holds
|
||||
@@ -57,88 +59,146 @@ impl Exporter<'_> {
|
||||
|
||||
/// The children of a group, as the elements of a knockout group when it is one.
|
||||
pub(super) fn children(&mut self, s: &mut Surface, children: &[std::sync::Arc<Node>], page: Rect) {
|
||||
self.measured_children(s, children, page, &[]);
|
||||
self.measured_children(s, children, page, &[], false);
|
||||
}
|
||||
|
||||
/// [`Self::children`] with the boxes of the last members in `measured`.
|
||||
fn measured_children(&mut self, s: &mut Surface, children: &[std::sync::Arc<Node>], page: Rect, measured: &[Measured]) {
|
||||
/// [`Self::children`] with the boxes of the last members in `measured`, in an isolated group
|
||||
/// when `isolated`.
|
||||
fn measured_children(&mut self, s: &mut Surface, children: &[std::sync::Arc<Node>], page: Rect, measured: &[Measured], isolated: bool) {
|
||||
if !self.knockout {
|
||||
if isolated {
|
||||
s.push_isolated();
|
||||
}
|
||||
let unmeasured = children.len().saturating_sub(measured.len());
|
||||
for (i, c) in children.iter().enumerate() {
|
||||
self.measured_node(s, c, page, false, i.checked_sub(unmeasured).and_then(|i| measured.get(i)));
|
||||
}
|
||||
if isolated {
|
||||
s.pop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
let elements: Vec<_> = Node::knockout_elements(children)
|
||||
let elements: Vec<(&Node, Measured)> = Node::knockout_elements(children)
|
||||
.into_iter()
|
||||
.map(|c| (c, self.measure(c)))
|
||||
.map(|c| (&**c, self.measure(c)))
|
||||
.filter(|(_, m)| m.bounds.is_some_and(|b| rects_overlap(b.inflate(1.0, 1.0), page)))
|
||||
.collect();
|
||||
if !elements.is_empty() && elements.iter().all(|(c, _)| c.knockout_outline().is_some()) {
|
||||
let pieces: Vec<&Node> = elements.iter().map(|(c, _)| &***c).collect();
|
||||
if vectorcraft_effects::warp_pieces_translucent(self.doc, &pieces) {
|
||||
return self.warp_pieces(s, &elements, page);
|
||||
if elements.is_empty() {
|
||||
return;
|
||||
}
|
||||
if elements.iter().all(|(c, _)| c.knockout_outline().is_some()) {
|
||||
let pieces: Vec<&Node> = elements.iter().map(|(c, _)| *c).collect();
|
||||
if isolated {
|
||||
s.push_isolated();
|
||||
}
|
||||
for (c, m) in &elements {
|
||||
self.measured_node(s, c, page, false, Some(m));
|
||||
if vectorcraft_effects::warp_pieces_translucent(self.doc, &pieces) {
|
||||
self.warp_pieces(s, &elements, page);
|
||||
} else {
|
||||
for (c, m) in &elements {
|
||||
self.measured_node(s, c, page, false, Some(m));
|
||||
}
|
||||
}
|
||||
if isolated {
|
||||
s.pop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// The PDF writer has no knockout groups: each element is drawn through a soft mask of
|
||||
// where the elements above it don't paint (the same look for Normal blending). The masks
|
||||
// nest, so element i sits inside the masks of elements i+1…n: open them outermost first.
|
||||
self.warn("knockout groups are written as soft-masked groups (same look, but not editable as knockout groups)");
|
||||
let Some(((first, measured), rest)) = elements.split_first() else { return };
|
||||
for (c, m) in rest.iter().rev() {
|
||||
let mask = self.knockout_mask(s, c, page, m);
|
||||
s.push_mask(mask);
|
||||
let several = elements.len() > 1;
|
||||
// A PDF knockout group (`/K`) knocks out by the area each element paints. Where an element
|
||||
// above another knocks out by less (its opacity or mask defines its knockout shape, or it
|
||||
// paints translucently), each element is drawn through one soft mask of where the
|
||||
// elements above it that overlap it paint, while those masks hold no more than
|
||||
// [`MAX_KNOCKOUT_MASK_OBJECTS`] objects and no brushed stroke.
|
||||
if several && !elements.iter().skip(1).all(|(c, _)| shape_is_coverage(c)) {
|
||||
let doc = self.doc;
|
||||
let brushes = self.brushes.get_or_insert_with(|| vectorcraft_brush::library(doc));
|
||||
match knockout_masks(BrushSearch::new(doc, brushes), &elements) {
|
||||
Ok(above) => {
|
||||
// Every mask is made before anything is drawn: when the symbol objects the
|
||||
// masks may hold run out, one would leave out art its element covers, and
|
||||
// the group is written as a PDF knockout group instead.
|
||||
let outer = std::mem::take(&mut self.mask_art_left_out);
|
||||
let before = *self.mask_counter();
|
||||
let mut masks = Vec::with_capacity(above.len());
|
||||
for above in above {
|
||||
let shapes: Vec<_> = above.into_iter().map(|j| &elements[j]).collect();
|
||||
masks.push((!shapes.is_empty()).then(|| self.knockout_mask(s, &shapes, page)));
|
||||
}
|
||||
let left_out = std::mem::replace(&mut self.mask_art_left_out, outer);
|
||||
if left_out {
|
||||
*self.mask_counter() = before;
|
||||
self.warn(KNOCKOUT_SHAPES);
|
||||
} else {
|
||||
if isolated {
|
||||
s.push_isolated();
|
||||
}
|
||||
for ((c, m), mask) in elements.iter().zip(masks) {
|
||||
if let Some(mask) = mask {
|
||||
s.push_mask(mask);
|
||||
self.measured_node(s, c, page, false, Some(m));
|
||||
s.pop();
|
||||
} else {
|
||||
self.measured_node(s, c, page, false, Some(m));
|
||||
}
|
||||
}
|
||||
if isolated {
|
||||
s.pop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
Err(warning) => self.warn(warning),
|
||||
}
|
||||
}
|
||||
self.measured_node(s, first, page, false, Some(measured));
|
||||
for (c, m) in rest {
|
||||
s.pop();
|
||||
let group = several || isolated;
|
||||
if group {
|
||||
s.push_group(krilla::blend::GroupOptions { isolated, knockout: several });
|
||||
}
|
||||
for (c, m) in &elements {
|
||||
// In a PDF knockout group each painting operation knocks out the ones before it. An
|
||||
// element drawn with several is one non-isolated group (drawn through a full alpha
|
||||
// mask), so that its own fills and strokes don't knock each other out; its opacity
|
||||
// or opacity mask makes it one already.
|
||||
let unit = several && !one_paint(c) && c.opacity >= 1.0 && c.extras().mask.as_deref().is_none_or(|m| m.disabled);
|
||||
if unit {
|
||||
let all = constant_mask(s, page, 1.0);
|
||||
s.push_mask(all);
|
||||
}
|
||||
self.measured_node(s, c, page, false, Some(m));
|
||||
if unit {
|
||||
s.pop();
|
||||
}
|
||||
}
|
||||
if group {
|
||||
s.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/// [`Self::measured_children`] of a plain group or layer. When they are the pieces of a
|
||||
/// mesh-warped image, every piece, on the page or not, takes part in the one size its pixels
|
||||
/// are resampled for ([`Self::warp_image_size`]).
|
||||
fn group_children(&mut self, s: &mut Surface, children: &[std::sync::Arc<Node>], page: Rect, measured: &[Measured]) {
|
||||
fn group_children(&mut self, s: &mut Surface, children: &[std::sync::Arc<Node>], page: Rect, measured: &[Measured], isolated: bool) {
|
||||
let outer = self.warp_image_size(children).map(|w| self.warp_size.replace(w));
|
||||
self.measured_children(s, children, page, measured);
|
||||
self.measured_children(s, children, page, measured, isolated);
|
||||
if let Some(outer) = outer {
|
||||
self.warp_size = outer;
|
||||
}
|
||||
}
|
||||
|
||||
/// A luminosity mask that is 1 − the knockout shape of `c`, whose box is in `measured`: white,
|
||||
/// then black through an alpha mask of `c` (at full object opacity without its own mask, unless
|
||||
/// those define its shape).
|
||||
fn knockout_mask(&mut self, s: &mut Surface, c: &Node, page: Rect, measured: &Measured) -> krilla::mask::Mask {
|
||||
let shape = if c.knockout_shape {
|
||||
c.clone()
|
||||
} else {
|
||||
let mut s = c.clone().with_mask(None);
|
||||
s.opacity = 1.0;
|
||||
s
|
||||
};
|
||||
self.shapes_mask(s, &[(&shape, Some(measured))], page)
|
||||
}
|
||||
|
||||
/// The pieces of a partly transparent mesh-warped image (`elements`, each with its box) as the
|
||||
/// elements of a knockout group ([`vectorcraft_effects::warp_pieces_translucent`]). Each piece
|
||||
/// is drawn through one soft mask of where the pieces above it that reach its box don't cover
|
||||
/// ([`vectorcraft_effects::knockout_silhouette`]): only neighbors overlap, so each mask holds a
|
||||
/// few triangles and no mask nests in another. A piece above that overlaps it only over opaque
|
||||
/// pixels ([`vectorcraft_effects::WarpSeams`]) is left out of its mask.
|
||||
fn warp_pieces(&mut self, s: &mut Surface, elements: &[(&std::sync::Arc<Node>, Measured)], page: Rect) {
|
||||
fn warp_pieces(&mut self, s: &mut Surface, elements: &[(&Node, Measured)], page: Rect) {
|
||||
// Each piece's triangle and its box: a piece paints inside its triangle only.
|
||||
let silhouette = |c: &Node| {
|
||||
let b = c.knockout_outline()?.geometric_bounds()?;
|
||||
Some((vectorcraft_effects::knockout_silhouette(c, vectorcraft_doc::color::Color::WHITE)?, b))
|
||||
};
|
||||
let silhouettes: Vec<Option<(Node, Rect)>> = elements.iter().map(|(c, _)| silhouette(c)).collect();
|
||||
let pieces: Vec<&Node> = elements.iter().map(|(c, _)| &***c).collect();
|
||||
let pieces: Vec<&Node> = elements.iter().map(|(c, _)| *c).collect();
|
||||
let seams = vectorcraft_effects::WarpSeams::new(self.doc, &pieces);
|
||||
for (i, (c, measured)) in elements.iter().enumerate() {
|
||||
let Some((_, reach)) = &silhouettes[i] else { continue };
|
||||
@@ -191,6 +251,67 @@ impl Exporter<'_> {
|
||||
krilla::mask::Mask::new(sb.finish(), krilla::mask::MaskType::Luminosity)
|
||||
}
|
||||
|
||||
/// A luminosity mask that is 1 − the knockout shapes of `shapes` (each with its box): white,
|
||||
/// then black through an alpha mask of each (at full object opacity without its own mask,
|
||||
/// unless those define its shape). The symbol instances in the copies count toward
|
||||
/// [`Exporter::mask_symbol_nodes`], not toward the objects the visible art may draw. A
|
||||
/// knockout mask drawn inside another one counts toward the same objects as the outer one.
|
||||
fn knockout_mask(&mut self, s: &mut Surface, shapes: &[&(&Node, Measured)], page: Rect) -> krilla::mask::Mask {
|
||||
if self.in_knockout_mask {
|
||||
return self.knockout_mask_art(s, shapes, page);
|
||||
}
|
||||
self.in_knockout_mask = true;
|
||||
let visible = std::mem::replace(&mut self.symbol_nodes, self.mask_symbol_nodes);
|
||||
let limit = std::mem::replace(&mut self.symbol_limit, vectorcraft_doc::MAX_SYMBOL_NODES);
|
||||
let mask = self.knockout_mask_art(s, shapes, page);
|
||||
self.mask_symbol_nodes = std::mem::replace(&mut self.symbol_nodes, visible);
|
||||
self.symbol_limit = limit;
|
||||
self.in_knockout_mask = false;
|
||||
mask
|
||||
}
|
||||
|
||||
/// The symbol objects the copies drawn in knockout masks held, together: [`Self::symbol_nodes`]
|
||||
/// while a knockout mask is being drawn, [`Exporter::mask_symbol_nodes`] otherwise.
|
||||
fn mask_counter(&mut self) -> &mut u64 {
|
||||
if self.in_knockout_mask { &mut self.symbol_nodes } else { &mut self.mask_symbol_nodes }
|
||||
}
|
||||
|
||||
/// [`Self::knockout_mask`], drawn within the symbol objects counted at the time.
|
||||
fn knockout_mask_art(&mut self, s: &mut Surface, shapes: &[&(&Node, Measured)], page: Rect) -> krilla::mask::Mask {
|
||||
let transform = s.group_transform();
|
||||
let mut sb = s.stream_builder();
|
||||
let mut ms = Surface::stream(sb.surface());
|
||||
ms.push_transform(&transform);
|
||||
cover(&mut ms, page, 255, 1.0);
|
||||
for (c, measured) in shapes.iter().map(|e| (e.0, &e.1)) {
|
||||
let shape = if c.knockout_shape {
|
||||
c.clone()
|
||||
} else {
|
||||
let mut s = c.clone().with_mask(None);
|
||||
s.opacity = 1.0;
|
||||
s
|
||||
};
|
||||
let alpha = {
|
||||
let transform = ms.group_transform();
|
||||
let mut ab = ms.stream_builder();
|
||||
let mut als = Surface::stream(ab.surface());
|
||||
als.push_transform(&transform);
|
||||
let overprint = std::mem::take(&mut self.overprint);
|
||||
self.measured_node(&mut als, &shape, page, false, Some(measured));
|
||||
self.overprint = overprint;
|
||||
als.pop();
|
||||
als.finish();
|
||||
krilla::mask::Mask::new(ab.finish(), krilla::mask::MaskType::Alpha)
|
||||
};
|
||||
ms.push_mask(alpha);
|
||||
cover(&mut ms, page, 0, 1.0);
|
||||
ms.pop();
|
||||
}
|
||||
ms.pop();
|
||||
ms.finish();
|
||||
krilla::mask::Mask::new(sb.finish(), krilla::mask::MaskType::Luminosity)
|
||||
}
|
||||
|
||||
/// The box of `n`, and of the members of a plain group or layer.
|
||||
fn measure(&self, n: &Node) -> Measured {
|
||||
if !matches!(n.kind, NodeKind::Layer { clip: false, .. } | NodeKind::Group { clip: false, .. }) {
|
||||
@@ -242,23 +363,7 @@ impl Exporter<'_> {
|
||||
// Whether this container's children knock each other out (a knockout group is written as a group).
|
||||
let knockout = n.knocks_out(self.knockout);
|
||||
let enclosing = std::mem::replace(&mut self.knockout, knockout);
|
||||
let mut pushes = 0;
|
||||
if n.blend != BlendMode::Normal {
|
||||
s.push_blend_mode(blend(n.blend));
|
||||
pushes += 1;
|
||||
}
|
||||
if n.opacity < 1.0 || (container && (n.isolate || n.blend != BlendMode::Normal || knockout)) {
|
||||
if !n.isolate && n.blends_through() {
|
||||
// Not isolated: blending inside reaches the art below the group.
|
||||
let alpha = constant_mask(s, page, n.opacity);
|
||||
s.push_mask(alpha);
|
||||
} else if n.opacity < 1.0 {
|
||||
s.push_opacity(norm(n.opacity));
|
||||
} else {
|
||||
s.push_isolated();
|
||||
}
|
||||
pushes += 1;
|
||||
}
|
||||
let (mut pushes, isolated_children) = transparency(s, n, page, container, knockout);
|
||||
let mask = n.extras().mask.as_deref().filter(|m| !m.disabled);
|
||||
let alpha = match (&n.kind, mask) {
|
||||
(NodeKind::Image(im), Some(m)) => self.masked_image(im, m, !n.extras().pixelated),
|
||||
@@ -279,7 +384,7 @@ impl Exporter<'_> {
|
||||
}
|
||||
match &n.kind {
|
||||
NodeKind::Layer { children, clip: false, .. } | NodeKind::Group { children, clip: false } => {
|
||||
self.group_children(s, children, page, measured.map_or(&[], |m| m.members.as_slice()));
|
||||
self.group_children(s, children, page, measured.map_or(&[], |m| m.members.as_slice()), isolated_children);
|
||||
}
|
||||
NodeKind::Group { children, clip: true } | NodeKind::Layer { children, clip: true, .. } => {
|
||||
// The region every output clips to; nothing to clip by hides the clipped art. The
|
||||
@@ -366,8 +471,10 @@ impl Exporter<'_> {
|
||||
(art, count)
|
||||
})
|
||||
});
|
||||
if let Some(art) = placed.flatten() {
|
||||
self.node(s, &art, page, true);
|
||||
match placed.flatten() {
|
||||
Some(art) => self.node(s, &art, page, true),
|
||||
// A knockout mask that leaves the art out no longer matches what it masks.
|
||||
None => self.mask_art_left_out |= self.in_knockout_mask,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -741,3 +848,379 @@ impl Exporter<'_> {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the blend mode and the transparency group `n` is drawn with (`container` when it is a
|
||||
/// group or layer whose children `knockout` each other). Returns how many were opened, and
|
||||
/// whether the children are to be drawn in an isolated group of their own: the knockout group of
|
||||
/// an unclipped group's elements is its isolated group too.
|
||||
fn transparency(s: &mut Surface, n: &Node, page: Rect, container: bool, knockout: bool) -> (usize, bool) {
|
||||
let mut pushes = 0;
|
||||
if n.blend != BlendMode::Normal {
|
||||
s.push_blend_mode(blend(n.blend));
|
||||
pushes += 1;
|
||||
}
|
||||
let mut isolated_children = false;
|
||||
if n.opacity < 1.0 || (container && (n.isolate || n.blend != BlendMode::Normal || knockout)) {
|
||||
if !n.isolate && n.blends_through() {
|
||||
// Not isolated: blending inside reaches the art below the group.
|
||||
let alpha = constant_mask(s, page, n.opacity);
|
||||
s.push_mask(alpha);
|
||||
pushes += 1;
|
||||
} else if n.opacity < 1.0 {
|
||||
s.push_opacity(norm(n.opacity));
|
||||
pushes += 1;
|
||||
} else if knockout && matches!(n.kind, NodeKind::Layer { clip: false, .. } | NodeKind::Group { clip: false, .. }) {
|
||||
isolated_children = true;
|
||||
} else {
|
||||
s.push_isolated();
|
||||
pushes += 1;
|
||||
}
|
||||
}
|
||||
(pushes, isolated_children)
|
||||
}
|
||||
|
||||
/// The warning for a knockout group whose elements are too many to draw through soft masks: it is
|
||||
/// written as a PDF knockout group, where an element knocks out all of the area it paints.
|
||||
pub(crate) const KNOCKOUT_SHAPES: &str = "knockout groups with too many overlapping translucent elements are written as PDF knockout groups: \
|
||||
their translucent elements knock out what they cover fully";
|
||||
|
||||
/// The most objects the soft masks of one knockout group's elements draw between them: each mask
|
||||
/// draws the elements above its own element that overlap it.
|
||||
pub(crate) const MAX_KNOCKOUT_MASK_OBJECTS: usize = 1 << 16;
|
||||
|
||||
/// The most elements of one knockout group drawn through soft masks: finding the elements that
|
||||
/// overlap compares every pair.
|
||||
pub(crate) const MAX_MASKED_ELEMENTS: usize = 2048;
|
||||
|
||||
/// The warning for a knockout group in which a soft mask would draw an element with a brushed
|
||||
/// stroke: it is written as a PDF knockout group, where an element knocks out all of the area it
|
||||
/// paints.
|
||||
pub(crate) const KNOCKOUT_BRUSHES: &str = "knockout groups with brushed strokes over other translucent elements are written as PDF knockout groups: \
|
||||
their translucent elements knock out what they cover fully";
|
||||
|
||||
/// For each element of a knockout group (with its box), the indices of the elements above it whose
|
||||
/// boxes overlap its own. The warning to give instead ([`KNOCKOUT_SHAPES`]) when there are more than
|
||||
/// [`MAX_MASKED_ELEMENTS`] elements or the masks would draw more than
|
||||
/// [`MAX_KNOCKOUT_MASK_OBJECTS`] objects, or ([`KNOCKOUT_BRUSHES`]) when a mask would draw a
|
||||
/// brushed stroke ([`BrushSearch`]), whose brush art the object count leaves out.
|
||||
fn knockout_masks(mut search: BrushSearch, elements: &[(&Node, Measured)]) -> Result<Vec<Vec<usize>>, &'static str> {
|
||||
if elements.len() > MAX_MASKED_ELEMENTS {
|
||||
return Err(KNOCKOUT_SHAPES);
|
||||
}
|
||||
let counts: Vec<usize> = elements.iter().map(|(c, _)| c.count_with_masks()).collect();
|
||||
let mut checked: Vec<Option<bool>> = vec![None; elements.len()];
|
||||
let mut objects = 0usize;
|
||||
let mut out = Vec::with_capacity(elements.len());
|
||||
for (i, (_, below)) in elements.iter().enumerate() {
|
||||
let mut above = vec![];
|
||||
for (j, (over_node, over)) in elements.iter().enumerate().skip(i + 1) {
|
||||
if let (Some(a), Some(b)) = (below.bounds, over.bounds)
|
||||
&& rects_overlap(a.inflate(1.0, 1.0), b.inflate(1.0, 1.0))
|
||||
{
|
||||
// The mask draws a copy of the element without its own opacity mask, unless the
|
||||
// mask defines its knockout shape.
|
||||
if *checked[j].get_or_insert_with(|| search.node(over_node, over_node.knockout_shape, 0)) {
|
||||
return Err(if search.exhausted { KNOCKOUT_SHAPES } else { KNOCKOUT_BRUSHES });
|
||||
}
|
||||
objects = objects.checked_add(counts[j]).filter(|&o| o <= MAX_KNOCKOUT_MASK_OBJECTS).ok_or(KNOCKOUT_SHAPES)?;
|
||||
above.push(j);
|
||||
}
|
||||
}
|
||||
out.push(above);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The art a [`BrushSearch`] looks through once: a symbol's or a pattern's, by name, or a
|
||||
/// symbol's with one instance's appearance changes, by the symbol's name and the changes.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
enum SharedArt {
|
||||
Symbol,
|
||||
Pattern,
|
||||
Dynamic,
|
||||
}
|
||||
|
||||
/// A search for the brushed strokes drawing an object draws, whose brush art can hold many more
|
||||
/// objects than the stroked path.
|
||||
struct BrushSearch<'a> {
|
||||
doc: &'a Document,
|
||||
/// The brushes the export draws strokes with ([`Exporter::brush_art`]); a stroke whose brush
|
||||
/// is not among them is drawn as a plain stroke.
|
||||
brushes: &'a [vectorcraft_brush::Brush],
|
||||
/// The answer for each symbol's and pattern's art, and each symbol's art with an instance's
|
||||
/// appearance changes, once found: false while it is being looked through, so that art that
|
||||
/// uses itself ends the search.
|
||||
seen: HashMap<(SharedArt, String), bool>,
|
||||
/// The objects the search may still visit or copy: [`MAX_KNOCKOUT_MASK_OBJECTS`] at the start.
|
||||
left: usize,
|
||||
/// Whether the search stopped because [`Self::left`] ran out; it then answers true.
|
||||
exhausted: bool,
|
||||
}
|
||||
|
||||
impl<'a> BrushSearch<'a> {
|
||||
fn new(doc: &'a Document, brushes: &'a [vectorcraft_brush::Brush]) -> Self {
|
||||
Self { doc, brushes, seen: HashMap::new(), left: MAX_KNOCKOUT_MASK_OBJECTS, exhausted: false }
|
||||
}
|
||||
|
||||
/// Takes `n` objects from [`Self::left`]; false, and the search exhausted, when fewer are left.
|
||||
fn spend(&mut self, n: usize) -> bool {
|
||||
match self.left.checked_sub(n) {
|
||||
Some(left) if !self.exhausted => {
|
||||
self.left = left;
|
||||
true
|
||||
}
|
||||
_ => {
|
||||
(self.left, self.exhausted) = (0, true);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes the objects of `n` (its members and opacity-mask art included) from [`Self::left`],
|
||||
/// counting no further than what is left; false when they don't fit.
|
||||
fn spend_tree(&mut self, n: &Node) -> bool {
|
||||
self.spend(1)
|
||||
&& n.extras().mask.as_deref().is_none_or(|m| self.spend_tree(&m.art))
|
||||
&& n.children().into_iter().flatten().all(|c| self.spend_tree(c))
|
||||
}
|
||||
|
||||
/// Whether drawing `n` draws a brushed stroke: on `n` or its visible members (a compound
|
||||
/// path's members are drawn with its own paint), in the art of the opacity masks that are on
|
||||
/// (`n`'s own when `own_mask`), of symbol instances and inline graphics, of live objects as
|
||||
/// evaluated, and of the patterns that fills, strokes and type paint with. `depth` counts the
|
||||
/// objects and the mask, symbol and pattern art above `n`: past
|
||||
/// [`vectorcraft_doc::depth::MAX_DRAWN_DEPTH`] the search stops and counts as a brush, and so
|
||||
/// it does when the objects it may visit or copy ([`Self::left`]) run out.
|
||||
fn node(&mut self, n: &Node, own_mask: bool, depth: usize) -> bool {
|
||||
if depth > vectorcraft_doc::depth::MAX_DRAWN_DEPTH || !self.spend(1) {
|
||||
return true;
|
||||
}
|
||||
let depth = depth + 1;
|
||||
// A fill is drawn when it is visible, and a stroke when it also paints and has a width
|
||||
// (as `Exporter::shape` draws them).
|
||||
for item in n.appearance.items.iter().filter(|i| i.visible()) {
|
||||
let paint = match item {
|
||||
AppearanceItem::Fill(f) => &f.paint,
|
||||
AppearanceItem::Stroke(st) if st.paint.is_none() || st.width <= 0.0 => continue,
|
||||
AppearanceItem::Stroke(st) => {
|
||||
if st.brush.as_deref().is_some_and(|name| self.brushes.iter().any(|b| b.name == name)) {
|
||||
return true;
|
||||
}
|
||||
&st.paint
|
||||
}
|
||||
};
|
||||
if self.paint(paint, depth) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if own_mask && n.extras().mask.as_deref().is_some_and(|m| !m.disabled && self.node(&m.art, true, depth)) {
|
||||
return true;
|
||||
}
|
||||
match &n.kind {
|
||||
NodeKind::SymbolInstance { symbol, .. } => {
|
||||
// A dynamic symbol's instance draws its art with its own appearance changes: the
|
||||
// art is copied once for each set of changes, counted before it is copied.
|
||||
let doc = self.doc;
|
||||
match doc.symbols.iter().find(|s| &s.name == symbol) {
|
||||
Some(s) if !n.extras().symbol_overrides.is_empty() => {
|
||||
let key = format!("{symbol}\u{0}{:?}", n.extras().symbol_overrides);
|
||||
self.shared(SharedArt::Dynamic, &key, |search| {
|
||||
!search.spend_tree(&s.art) || search.node(&vectorcraft_doc::symbol_overrides::dynamic_art(&s.art, n), true, depth)
|
||||
})
|
||||
}
|
||||
_ => self.symbol(symbol, depth),
|
||||
}
|
||||
}
|
||||
NodeKind::Text(t) => t.runs.iter().any(|r| {
|
||||
let stroke = r.style.stroke_width > 0.0 && self.paint(&r.style.stroke, depth);
|
||||
stroke || self.paint(&r.style.fill, depth) || r.inline.as_ref().is_some_and(|ig| self.symbol(&ig.symbol, depth))
|
||||
}),
|
||||
NodeKind::Compound { .. } => false,
|
||||
// Live objects are drawn as their evaluated art, as `Exporter::measured_node` draws them.
|
||||
NodeKind::Blend { .. }
|
||||
| NodeKind::Envelope { .. }
|
||||
| NodeKind::Mesh(_)
|
||||
| NodeKind::Repeat(_)
|
||||
| NodeKind::PlacedDocument(_)
|
||||
| NodeKind::Intertwine { .. } => {
|
||||
// Each live object is reached once in the art searched (shared art is searched
|
||||
// once); its evaluated art is counted before it is searched.
|
||||
let g = vectorcraft_effects::expand_live_deep(Some(self.doc), n);
|
||||
!self.spend_tree(&g) || g.children().into_iter().flatten().filter(|c| c.visible).any(|c| self.node(c, true, depth))
|
||||
}
|
||||
_ => n.children().into_iter().flatten().filter(|c| c.visible).any(|c| self.node(c, true, depth)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the art of the symbol named `name` draws a brushed stroke.
|
||||
fn symbol(&mut self, name: &str, depth: usize) -> bool {
|
||||
let doc = self.doc;
|
||||
let Some(s) = doc.symbols.iter().find(|s| s.name == name) else { return false };
|
||||
self.shared(SharedArt::Symbol, name, |search| search.node(&s.art, true, depth))
|
||||
}
|
||||
|
||||
/// Whether `paint` is a pattern whose tile art draws a brushed stroke.
|
||||
fn paint(&mut self, paint: &Paint, depth: usize) -> bool {
|
||||
let Paint::Pattern { pattern, .. } = paint else { return false };
|
||||
let doc = self.doc;
|
||||
let Some(def) = doc.pattern(pattern) else { return false };
|
||||
self.shared(SharedArt::Pattern, pattern, |search| def.art.iter().filter(|c| c.visible).any(|c| search.node(c, true, depth)))
|
||||
}
|
||||
|
||||
/// The answer of `search` for the shared art `kind` named `name`, found once.
|
||||
fn shared(&mut self, kind: SharedArt, name: &str, search: impl FnOnce(&mut Self) -> bool) -> bool {
|
||||
let key = (kind, name.to_string());
|
||||
if let Some(&b) = self.seen.get(&key) {
|
||||
return b;
|
||||
}
|
||||
self.seen.insert(key.clone(), false);
|
||||
let b = search(self);
|
||||
self.seen.insert(key, b);
|
||||
b
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `n` is drawn with one painting operation at most: a path or compound path with at most
|
||||
/// one visible fill or stroke that paints, in a solid color, without effects, a brush or
|
||||
/// arrowheads.
|
||||
fn one_paint(n: &Node) -> bool {
|
||||
let mut items = n.appearance.items.iter().filter(|i| i.visible() && !matches!(i.paint(), Paint::None));
|
||||
let simple = items.next().is_none_or(|i| {
|
||||
let plain = match i {
|
||||
AppearanceItem::Fill(_) => true,
|
||||
AppearanceItem::Stroke(st) => st.brush.is_none() && st.start_arrow.is_none() && st.end_arrow.is_none(),
|
||||
};
|
||||
plain && i.effects().is_empty() && matches!(i.paint(), Paint::Solid { .. })
|
||||
});
|
||||
matches!(n.kind, NodeKind::Path { .. } | NodeKind::Compound { .. }) && n.appearance.effects.is_empty() && simple && items.next().is_none()
|
||||
}
|
||||
|
||||
/// Whether element `c` of a knockout group knocks out all of the area it paints, as an element of
|
||||
/// a PDF knockout group does: its opacity and opacity mask are full or don't define its knockout
|
||||
/// shape, and every part of it paints opaquely.
|
||||
fn shape_is_coverage(c: &Node) -> bool {
|
||||
let own = !c.knockout_shape || (c.opacity >= 1.0 && c.extras().mask.as_deref().is_none_or(|m| m.disabled));
|
||||
own && paints_opaque(c)
|
||||
}
|
||||
|
||||
/// Whether what `n` paints is opaque wherever it paints, at its own opacity and mask: its visible
|
||||
/// fills and strokes are solid colors or gradients with opaque stops, without effects or brushes,
|
||||
/// and so are the members of a group or compound path, at full opacity without an opacity mask.
|
||||
fn paints_opaque(n: &Node) -> bool {
|
||||
let paint = |p: &Paint| match p {
|
||||
Paint::None | Paint::Solid { .. } => true,
|
||||
Paint::Gradient(g) => g.gradient.stops.iter().all(|s| s.opacity >= 1.0),
|
||||
_ => false,
|
||||
};
|
||||
let items = n.appearance.items.iter().all(|i| {
|
||||
let brush = matches!(i, AppearanceItem::Stroke(s) if s.brush.is_some());
|
||||
!i.visible() || (i.opacity() >= 1.0 && i.effects().is_empty() && !brush && paint(i.paint()))
|
||||
});
|
||||
let member =
|
||||
|c: &std::sync::Arc<Node>| !c.visible || (c.opacity >= 1.0 && c.extras().mask.as_deref().is_none_or(|m| m.disabled) && paints_opaque(c));
|
||||
n.appearance.effects.is_empty()
|
||||
&& items
|
||||
&& match &n.kind {
|
||||
NodeKind::Path { .. } => true,
|
||||
NodeKind::Group { children, .. } | NodeKind::Layer { children, .. } | NodeKind::Compound { children, .. } => children.iter().all(member),
|
||||
NodeKind::Mesh(m) => m.points.iter().all(|p| p.opacity >= 1.0),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use vectorcraft_doc::{Appearance, NodeId, Symbol, SymbolOverride};
|
||||
use vectorcraft_geom::shapes;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// An instance of a dynamic symbol whose art holds an instance of the same symbol with the same
|
||||
/// appearance changes ends the search for brushed strokes where the art comes round again,
|
||||
/// after a few objects, instead of recursing until the stack runs out.
|
||||
#[test]
|
||||
fn a_dynamic_symbol_holding_itself_ends_the_brush_search() {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let instance = || {
|
||||
let mut n = Node::new(NodeId(0), NodeKind::SymbolInstance { symbol: "Loop".into(), xf: Affine::translate((40.0, 0.0)) });
|
||||
n.extras_mut().symbol_overrides = vec![SymbolOverride { path: vec![0], opacity: Some(0.5), ..Default::default() }];
|
||||
n
|
||||
};
|
||||
let square = Node::path(NodeId(0), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::basic(Paint::None, Paint::None, 0.0));
|
||||
let art = Node::group(NodeId(0), vec![Arc::new(square), Arc::new(instance())]);
|
||||
d.symbols.push(Symbol { dynamic: true, name: "Loop".into(), art: Arc::new(art) });
|
||||
let mut search = BrushSearch::new(&d, &[]);
|
||||
assert!(!search.node(&instance(), true, 0));
|
||||
assert!(!search.exhausted);
|
||||
assert!(MAX_KNOCKOUT_MASK_OBJECTS - search.left < 10, "{} objects", MAX_KNOCKOUT_MASK_OBJECTS - search.left);
|
||||
}
|
||||
|
||||
/// A group of 1,000 instances of a dynamic symbol whose art holds 100 squares, each instance
|
||||
/// with appearance changes of its own: copying and looking through every instance's art would
|
||||
/// take about 203,000 objects, and the search stops when [`MAX_KNOCKOUT_MASK_OBJECTS`] are
|
||||
/// used, counting as a brush. With the same changes on every instance, the art is copied
|
||||
/// once.
|
||||
#[test]
|
||||
fn the_brush_search_stops_within_its_object_limit() {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let square =
|
||||
Arc::new(Node::path(NodeId(0), shapes::rectangle(Rect::new(0.0, 0.0, 1.0, 1.0)), Appearance::basic(Paint::None, Paint::None, 0.0)));
|
||||
d.symbols.push(Symbol { dynamic: true, name: "Dots".into(), art: Arc::new(Node::group(NodeId(0), vec![square; 100])) });
|
||||
let group = |distinct: bool| {
|
||||
let instances = (0..1000u32)
|
||||
.map(|i| {
|
||||
let mut n = Node::new(NodeId(0), NodeKind::SymbolInstance { symbol: "Dots".into(), xf: Affine::IDENTITY });
|
||||
let path = if distinct { vec![0, i] } else { vec![0] };
|
||||
n.extras_mut().symbol_overrides = vec![SymbolOverride { path, opacity: Some(0.5), ..Default::default() }];
|
||||
Arc::new(n)
|
||||
})
|
||||
.collect();
|
||||
Node::group(NodeId(0), instances)
|
||||
};
|
||||
let mut search = BrushSearch::new(&d, &[]);
|
||||
assert!(search.node(&group(true), true, 0));
|
||||
assert!(search.exhausted);
|
||||
assert_eq!(search.left, 0);
|
||||
let mut search = BrushSearch::new(&d, &[]);
|
||||
assert!(!search.node(&group(false), true, 0));
|
||||
assert!(!search.exhausted);
|
||||
// The group, the 1,000 instances, and one copy of the art counted and looked through.
|
||||
assert_eq!(MAX_KNOCKOUT_MASK_OBJECTS - search.left, 1 + 1000 + 101 + 101);
|
||||
}
|
||||
|
||||
/// A placed file read as one square with a 3 pt brushed stroke.
|
||||
fn brushed_art() -> (Document, Node, Rect) {
|
||||
let mut d = Document::new(40.0, 20.0);
|
||||
d.layers.clear();
|
||||
let mut st = StrokeLayer::new(Paint::solid(vectorcraft_color::Color::BLACK), 3.0);
|
||||
st.brush = Some("3 pt. Round".into());
|
||||
let mut square = Node::path(NodeId(0), shapes::rectangle(Rect::new(0.0, 0.0, 40.0, 20.0)), Appearance::basic(Paint::None, Paint::None, 0.0));
|
||||
square.appearance.items.push(AppearanceItem::Stroke(st));
|
||||
let art = Node::group(NodeId(0), vec![Arc::new(square)]);
|
||||
(d, art, Rect::new(0.0, 0.0, 40.0, 20.0))
|
||||
}
|
||||
|
||||
/// A placed document is drawn as the art read from its file, and the brushed strokes in that
|
||||
/// art count.
|
||||
#[test]
|
||||
fn brushed_strokes_in_a_placed_documents_art_count() {
|
||||
let d = Document::new(200.0, 200.0);
|
||||
let placed = vectorcraft_doc::PlacedDocument {
|
||||
link: vectorcraft_doc::LinkInfo::new("/art/placed.vectorcraft"),
|
||||
key: "placed-1".into(),
|
||||
bounding: false,
|
||||
page_box: vectorcraft_doc::placed_document::PageBox::Crop,
|
||||
width: 40.0,
|
||||
height: 20.0,
|
||||
xf: Affine::IDENTITY,
|
||||
placement: Default::default(),
|
||||
};
|
||||
// The art the export reads for it before drawing (`Document::with_placed_art`).
|
||||
vectorcraft_doc::placed_document::prime(&placed, Arc::new(vec![]), brushed_art());
|
||||
let n = Node::new(NodeId(0), NodeKind::PlacedDocument(Box::new(placed)));
|
||||
let brushes = vectorcraft_brush::library(&d);
|
||||
assert!(BrushSearch::new(&d, &brushes).node(&n, true, 0));
|
||||
}
|
||||
}
|
||||
@@ -387,6 +387,21 @@ impl<'p> Builder<'p> {
|
||||
only = fold_stop_opacity(&only, &m).unwrap_or_else(|| only.with_mask(Some(Box::new(m))));
|
||||
}
|
||||
Some(only)
|
||||
} else if let [only] = children.as_slice()
|
||||
&& matches!(only.kind, NodeKind::Group { clip: false, .. })
|
||||
&& only.knockout == Knockout::On
|
||||
&& only.opacity >= 0.999
|
||||
&& only.blend == BlendMode::Normal
|
||||
&& !only.isolate
|
||||
&& only.extras().mask.is_none()
|
||||
&& mask.is_none()
|
||||
&& !knockout
|
||||
{
|
||||
// A knockout group drawn in a form that carries its opacity, blend mode or
|
||||
// isolation: one knockout group with them.
|
||||
let mut only = (**only).clone();
|
||||
(only.opacity, only.blend, only.isolate) = (opacity, blend, isolate);
|
||||
Some(only)
|
||||
} else {
|
||||
let mut g = Node::group(self.id(), children);
|
||||
(g.opacity, g.blend, g.isolate) = (opacity, blend, isolate);
|
||||
|
||||
@@ -81,6 +81,11 @@ impl<'a> Surface<'a> {
|
||||
self.group(|s| s.push_isolated());
|
||||
}
|
||||
|
||||
/// A transparency group with `options` (isolated, knockout), as [`Self::push_isolated`].
|
||||
pub fn push_group(&mut self, options: krilla::blend::GroupOptions) {
|
||||
self.group(|s| s.push_group(options));
|
||||
}
|
||||
|
||||
/// The mask's stream is already in [`Self::group_transform`]'s space, like the group's art.
|
||||
pub fn push_mask(&mut self, mask: Mask) {
|
||||
self.group(|s| s.push_mask(mask));
|
||||
|
||||
@@ -97,3 +97,466 @@ fn a_knockout_group_with_blending_elements_is_non_isolated_unless_isolated() {
|
||||
assert_eq!(text(&d).contains("/Itrue"), isolate, "isolate {isolate}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A knockout group of `n` red, green and blue squares at 50% opacity that all overlap, over an
|
||||
/// opaque backdrop. `knockout_shape`: their opacity defines their knockout shape.
|
||||
fn knockout_squares(n: usize, knockout_shape: bool, isolate: bool) -> Document {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
add(&mut d, rect(Rect::new(0.0, 0.0, 200.0, 200.0), (0.9, 0.8, 0.2)));
|
||||
let colors = [(1.0, 0.0, 0.0), (0.0, 0.8, 0.0), (0.0, 0.0, 1.0)];
|
||||
let squares = (0..n)
|
||||
.map(|i| {
|
||||
let x = (i % 20) as f64 * 2.0;
|
||||
let mut s = rect(Rect::new(20.0 + x, 50.0, 120.0 + x, 150.0), colors[i % 3]);
|
||||
(s.id, s.opacity, s.knockout_shape) = (d.alloc_id(), 0.5, knockout_shape);
|
||||
std::sync::Arc::new(s)
|
||||
})
|
||||
.collect();
|
||||
let mut g = Node::group(NodeId(0), squares);
|
||||
(g.knockout, g.isolate) = (vectorcraft_doc::Knockout::On, isolate);
|
||||
add(&mut d, g);
|
||||
d
|
||||
}
|
||||
|
||||
/// How many objects each knockout group of `d` holds.
|
||||
fn knockout_groups(d: &Document) -> Vec<usize> {
|
||||
let mut out = vec![];
|
||||
d.walk(|n| {
|
||||
if n.knockout == vectorcraft_doc::Knockout::On {
|
||||
out.push(n.children().map_or(0, |c| c.len()));
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// A knockout group whose elements knock out the area they paint is written as one PDF knockout
|
||||
/// group (`/K true`), whatever the number of elements, and reads back as a knockout group that
|
||||
/// draws as the original does.
|
||||
#[test]
|
||||
fn a_knockout_group_is_written_as_one_pdf_knockout_group() {
|
||||
for n in [0, 1, 2, 49, 50, 200] {
|
||||
let d = knockout_squares(n, false, false);
|
||||
let t = text(&d);
|
||||
assert_eq!(t.matches("/Ktrue").count(), usize::from(n > 1), "{n}");
|
||||
let back = import(&export(&d, &PdfOptions::default()).unwrap()).unwrap_or_else(|e| panic!("{n}: {e}"));
|
||||
assert_eq!(knockout_groups(&back), if n > 1 { vec![n] } else { vec![] }, "{n}");
|
||||
if n == 200 {
|
||||
vectorcraft_testkit::raster::assert_similar(
|
||||
&vectorcraft_testkit::raster::render_artboard(&back),
|
||||
&vectorcraft_testkit::raster::render_artboard(&d),
|
||||
4.0,
|
||||
0.01,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The knockout group of an isolated group is isolated too (`/I true` beside `/K true`), and the
|
||||
/// group reads back as isolated and knocking out. (Each translucent element is a group of its own.)
|
||||
#[test]
|
||||
fn an_isolated_knockout_group_is_one_isolated_pdf_knockout_group() {
|
||||
for isolate in [false, true] {
|
||||
let mut d = knockout_squares(3, false, isolate);
|
||||
// Blending inside, which isolation keeps from the backdrop.
|
||||
let g = d.layers[0].children().unwrap()[1].id;
|
||||
let top = d.node(g).unwrap().children().unwrap()[2].id;
|
||||
d.node_mut(top).unwrap().blend = BlendMode::Multiply;
|
||||
let t = text(&d);
|
||||
assert_eq!(t.matches("/Ktrue").count(), 1, "isolate {isolate}");
|
||||
assert_eq!(t.contains("/Itrue/Ktrue"), isolate, "isolate {isolate}");
|
||||
let back = import(&export(&d, &PdfOptions::default()).unwrap()).unwrap();
|
||||
let mut found = vec![];
|
||||
back.walk(|n| {
|
||||
if n.knockout == vectorcraft_doc::Knockout::On {
|
||||
found.push(n.isolate);
|
||||
}
|
||||
});
|
||||
assert_eq!(found, [isolate]);
|
||||
}
|
||||
}
|
||||
|
||||
/// A translucent knockout group is written as a form with its opacity around the `/K` group, and
|
||||
/// reads back as one knockout group with that opacity and its isolation.
|
||||
#[test]
|
||||
fn a_translucent_knockout_group_reads_back_as_one_group() {
|
||||
for isolate in [false, true] {
|
||||
let mut d = knockout_squares(3, false, isolate);
|
||||
let g = d.layers[0].children().unwrap()[1].id;
|
||||
d.node_mut(g).unwrap().opacity = 0.5;
|
||||
let top = d.node(g).unwrap().children().unwrap()[2].id;
|
||||
d.node_mut(top).unwrap().blend = BlendMode::Multiply;
|
||||
let back = import(&export(&d, &PdfOptions::default()).unwrap()).unwrap();
|
||||
let mut found = vec![];
|
||||
back.walk(|n| {
|
||||
if matches!(n.kind, vectorcraft_doc::NodeKind::Group { .. }) {
|
||||
found.push((
|
||||
n.knockout == vectorcraft_doc::Knockout::On,
|
||||
n.isolate,
|
||||
(n.opacity * 100.0).round(),
|
||||
n.children().map_or(0, |c| c.len()),
|
||||
));
|
||||
}
|
||||
});
|
||||
assert_eq!(found, [(true, isolate, 50.0, 3)], "isolate {isolate}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Elements whose opacity defines their knockout shape knock out less than a PDF knockout group
|
||||
/// knocks out: each is drawn through one soft mask of the elements above it, so that 200 of them
|
||||
/// still read back, every one of them.
|
||||
#[test]
|
||||
fn elements_whose_opacity_defines_their_knockout_shape_are_drawn_through_one_mask_each() {
|
||||
let d = knockout_squares(200, true, false);
|
||||
let r = export_with_report(&d, &PdfOptions::default()).unwrap();
|
||||
assert!(!r.warnings.iter().any(|w| w.contains("knockout")), "{:?}", r.warnings);
|
||||
assert!(!text(&d).contains("/Ktrue"));
|
||||
let back = import(&r.bytes).unwrap();
|
||||
let mut paths = 0;
|
||||
back.walk(|n| paths += usize::from(matches!(n.kind, vectorcraft_doc::NodeKind::Path { .. })));
|
||||
// The backdrop and the 200 squares (the masks' copies of them are opacity-mask art).
|
||||
assert_eq!(paths, 201);
|
||||
}
|
||||
|
||||
/// When the masks would draw more than [`crate::export::MAX_KNOCKOUT_MASK_OBJECTS`] objects, the
|
||||
/// group is written as a PDF knockout group, with a warning, and reads back as a knockout group.
|
||||
#[test]
|
||||
fn a_knockout_group_too_large_to_mask_is_a_pdf_knockout_group_with_a_warning() {
|
||||
// 400 elements that all overlap: 79,800 objects in their masks.
|
||||
const { assert!(400 * 399 / 2 > crate::export::MAX_KNOCKOUT_MASK_OBJECTS) };
|
||||
let d = knockout_squares(400, true, false);
|
||||
let r = export_with_report(&d, &PdfOptions::default()).unwrap();
|
||||
assert!(r.warnings.iter().any(|w| w == crate::export::KNOCKOUT_SHAPES), "{:?}", r.warnings);
|
||||
assert_eq!(text(&d).matches("/Ktrue").count(), 1);
|
||||
assert_eq!(knockout_groups(&import(&r.bytes).unwrap()), [400]);
|
||||
}
|
||||
|
||||
/// In a PDF knockout group each painting operation knocks out the ones before it: an element with
|
||||
/// two fills, the second in Multiply, is drawn as one group (through a full alpha mask), so that
|
||||
/// its second fill blends with its first. An element with one fill needs no group.
|
||||
#[test]
|
||||
fn an_element_with_several_paints_is_one_group_in_a_knockout_group() {
|
||||
for (fills, groups) in [(1, 0), (2, 1)] {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let below = rect(Rect::new(0.0, 0.0, 100.0, 100.0), (0.0, 0.0, 1.0));
|
||||
let mut top = rect(Rect::new(50.0, 50.0, 150.0, 150.0), (1.0, 0.0, 0.0));
|
||||
if fills == 2 {
|
||||
let mut multiply = vectorcraft_doc::FillLayer::new(Paint::solid(Color::rgb(0.0, 0.0, 1.0)));
|
||||
multiply.blend = BlendMode::Multiply;
|
||||
top.appearance.items.push(vectorcraft_doc::AppearanceItem::Fill(multiply));
|
||||
}
|
||||
top.id = d.alloc_id();
|
||||
let below = Node { id: d.alloc_id(), ..below };
|
||||
let mut g = Node::group(NodeId(0), vec![std::sync::Arc::new(below), std::sync::Arc::new(top)]);
|
||||
g.knockout = vectorcraft_doc::Knockout::On;
|
||||
add(&mut d, g);
|
||||
let t = text(&d);
|
||||
assert_eq!(t.matches("/Ktrue").count(), 1, "{fills}");
|
||||
let Some(start) = t.find("/Ktrue") else { panic!("no knockout group") };
|
||||
let group = &t[start..];
|
||||
let Some(end) = group.find("endstream") else { panic!("no knockout stream") };
|
||||
assert_eq!(group[..end].matches("Do").count(), groups, "{fills}: {t}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Elements that don't overlap need no masks of each other.
|
||||
#[test]
|
||||
fn knockout_masks_hold_only_the_elements_that_overlap() {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let squares = (0..3)
|
||||
.map(|i| {
|
||||
let x = f64::from(i) * 60.0;
|
||||
let mut s = rect(Rect::new(x, 0.0, x + 40.0, 40.0), (0.0, 0.0, 1.0));
|
||||
(s.id, s.opacity, s.knockout_shape) = (d.alloc_id(), 0.5, true);
|
||||
std::sync::Arc::new(s)
|
||||
})
|
||||
.collect();
|
||||
let mut g = Node::group(NodeId(0), squares);
|
||||
g.knockout = vectorcraft_doc::Knockout::On;
|
||||
add(&mut d, g);
|
||||
let t = text(&d);
|
||||
assert!(!t.contains("/Luminosity") && !t.contains("/Ktrue"), "{t}");
|
||||
}
|
||||
|
||||
/// The copies of symbol instances drawn in knockout masks don't count toward the objects the
|
||||
/// visible symbol art of an export may hold (`MAX_SYMBOL_NODES`): of two overlapping 50% instances
|
||||
/// whose art placed through 9-slice guides holds about 6,510,000 objects each (a gradient square
|
||||
/// in all nine cells, each copy with a disabled opacity mask of 723,351 objects), both are written
|
||||
/// in their nine cells, although the lower one's mask draws a copy of the upper one first.
|
||||
#[test]
|
||||
fn knockout_mask_copies_of_symbol_instances_leave_the_visible_art_its_object_limit() {
|
||||
let d = panel_instances(850, &[20.0, 40.0]);
|
||||
// Nine squares of each instance (the mask's copy of the upper one is opacity-mask art).
|
||||
assert_eq!(painted_paths(&export(&d, &PdfOptions::default()).unwrap()), 18);
|
||||
}
|
||||
|
||||
/// A knockout group of overlapping 50% instances, at `xs`, of a symbol whose art placed through
|
||||
/// 9-slice guides holds 9 × (`mask_objects`² + `mask_objects` + 3) objects (a gradient square in
|
||||
/// all nine cells, each copy with a disabled opacity mask of `mask_objects`² + `mask_objects` + 1
|
||||
/// objects).
|
||||
fn panel_instances(mask_objects: usize, xs: &[f64]) -> Document {
|
||||
use vectorcraft_color::{Gradient, GradientPaint};
|
||||
use vectorcraft_geom::Affine;
|
||||
let mut d = Document::new(300.0, 200.0);
|
||||
let leaf = std::sync::Arc::new(rect(Rect::new(0.0, 0.0, 1.0, 1.0), (0.0, 0.0, 0.0)));
|
||||
let inner = std::sync::Arc::new(Node::group(NodeId(0), vec![leaf; mask_objects]));
|
||||
let mut mask = vectorcraft_doc::OpacityMask::new(Node::group(NodeId(0), vec![inner; mask_objects]), true);
|
||||
(mask.linked, mask.disabled) = (false, true);
|
||||
let gradient = Paint::Gradient(Box::new(GradientPaint::new(Gradient::default())));
|
||||
let mut square = Node::path(NodeId(0), shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), Appearance::basic(gradient, Paint::None, 0.0));
|
||||
square.extras_mut().mask = Some(Box::new(mask));
|
||||
let art = Node::group(NodeId(0), vec![std::sync::Arc::new(square)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Panel".into(), art: std::sync::Arc::new(art) });
|
||||
d.set_symbol_nine_slice("Panel", Some(vectorcraft_doc::SymbolNineSlice::default()));
|
||||
let instances = xs
|
||||
.iter()
|
||||
.map(|&x| {
|
||||
let xf = Affine::translate((x, 20.0)) * Affine::scale_non_uniform(2.0, 1.5);
|
||||
let mut n = Node::new(NodeId(0), vectorcraft_doc::NodeKind::SymbolInstance { symbol: "Panel".into(), xf });
|
||||
(n.id, n.opacity, n.knockout_shape) = (d.alloc_id(), 0.5, true);
|
||||
std::sync::Arc::new(n)
|
||||
})
|
||||
.collect();
|
||||
let mut g = Node::group(NodeId(0), instances);
|
||||
g.knockout = vectorcraft_doc::Knockout::On;
|
||||
add(&mut d, g);
|
||||
d
|
||||
}
|
||||
|
||||
/// The paths of `pdf` read back, the art of opacity masks left out.
|
||||
fn painted_paths(pdf: &[u8]) -> usize {
|
||||
let mut paths = 0;
|
||||
import(pdf).unwrap().walk(|n| paths += usize::from(matches!(n.kind, vectorcraft_doc::NodeKind::Path { .. })));
|
||||
paths
|
||||
}
|
||||
|
||||
/// When the copies the knockout masks draw would hold more symbol objects than they may
|
||||
/// (`MAX_SYMBOL_NODES` of their own), the group is written as a PDF knockout group, with a
|
||||
/// warning, instead of masks that leave out art their elements cover: four overlapping instances
|
||||
/// of about 3,870,000 objects each fit the visible art's limit, and the six copies in their masks
|
||||
/// do not.
|
||||
#[test]
|
||||
fn knockout_masks_whose_symbol_copies_run_out_give_way_to_a_pdf_knockout_group() {
|
||||
let d = panel_instances(655, &[20.0, 40.0, 60.0, 80.0]);
|
||||
let r = export_with_report(&d, &PdfOptions::default()).unwrap();
|
||||
assert!(r.warnings.iter().any(|w| w == crate::export::KNOCKOUT_SHAPES), "{:?}", r.warnings);
|
||||
assert_eq!(painted_paths(&r.bytes), 36);
|
||||
}
|
||||
|
||||
/// A brushed stroke draws brush art that the objects counted for the knockout masks leave out:
|
||||
/// a group in which a mask would draw an element with a brushed stroke is written as a PDF
|
||||
/// knockout group, with a warning. A brushed element that no mask draws (the lowest one) leaves
|
||||
/// the masks as they are.
|
||||
#[test]
|
||||
fn a_knockout_group_whose_masks_would_draw_a_brushed_stroke_is_a_pdf_knockout_group() {
|
||||
for (brushed, expected) in [(1, true), (0, false)] {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let squares = (0..2)
|
||||
.map(|i| {
|
||||
let x = f64::from(i) * 40.0;
|
||||
let mut s = rect(Rect::new(20.0 + x, 50.0, 120.0 + x, 150.0), (0.0, 0.0, 1.0));
|
||||
(s.id, s.opacity, s.knockout_shape) = (d.alloc_id(), 0.5, true);
|
||||
if i == brushed {
|
||||
let mut st = vectorcraft_doc::StrokeLayer::new(Paint::solid(Color::BLACK), 3.0);
|
||||
st.brush = Some("3 pt. Round".into());
|
||||
s.appearance.items.push(vectorcraft_doc::AppearanceItem::Stroke(st));
|
||||
}
|
||||
std::sync::Arc::new(s)
|
||||
})
|
||||
.collect();
|
||||
let mut g = Node::group(NodeId(0), squares);
|
||||
g.knockout = vectorcraft_doc::Knockout::On;
|
||||
add(&mut d, g);
|
||||
let r = export_with_report(&d, &PdfOptions::default()).unwrap();
|
||||
assert_eq!(r.warnings.iter().any(|w| w == crate::export::KNOCKOUT_BRUSHES), expected, "{brushed}: {:?}", r.warnings);
|
||||
assert!(!r.warnings.iter().any(|w| w == crate::export::KNOCKOUT_SHAPES), "{brushed}: {:?}", r.warnings);
|
||||
assert_eq!(text(&d).matches("/Ktrue").count(), usize::from(expected), "{brushed}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The knockout group of `upper` at 50%, whose opacity defines its knockout shape when
|
||||
/// `knockout_shape`, between two 50% squares whose opacity defines theirs (so that the group is
|
||||
/// drawn through masks): whether the export gives the brushed-stroke warning.
|
||||
fn warns_of_brushes(mut d: Document, mut upper: Node, knockout_shape: bool) -> bool {
|
||||
(upper.id, upper.opacity, upper.knockout_shape) = (d.alloc_id(), 0.5, knockout_shape);
|
||||
let mut lower = rect(Rect::new(20.0, 50.0, 120.0, 150.0), (1.0, 0.0, 0.0));
|
||||
(lower.id, lower.opacity, lower.knockout_shape) = (d.alloc_id(), 0.5, true);
|
||||
let mut top = rect(Rect::new(100.0, 50.0, 180.0, 150.0), (0.0, 0.8, 0.0));
|
||||
(top.id, top.opacity, top.knockout_shape) = (d.alloc_id(), 0.5, true);
|
||||
let mut g = Node::group(NodeId(0), [lower, upper, top].map(std::sync::Arc::new).to_vec());
|
||||
g.knockout = vectorcraft_doc::Knockout::On;
|
||||
add(&mut d, g);
|
||||
let r = export_with_report(&d, &PdfOptions::default()).unwrap();
|
||||
r.warnings.iter().any(|w| w == crate::export::KNOCKOUT_BRUSHES)
|
||||
}
|
||||
|
||||
/// A square at (60, 50) with a 3 pt brushed stroke.
|
||||
fn brushed_square() -> Node {
|
||||
let mut line = rect(Rect::new(60.0, 50.0, 160.0, 150.0), (1.0, 1.0, 1.0));
|
||||
let mut st = vectorcraft_doc::StrokeLayer::new(Paint::solid(Color::BLACK), 3.0);
|
||||
st.brush = Some("3 pt. Round".into());
|
||||
line.appearance.items.push(vectorcraft_doc::AppearanceItem::Stroke(st));
|
||||
line
|
||||
}
|
||||
|
||||
/// Only brushed strokes a mask draws count: a hidden member's is not drawn, nor is one without
|
||||
/// paint or width, nor the one in the opacity mask of an element whose mask does not define its
|
||||
/// knockout shape (its copy in a mask leaves the opacity mask out).
|
||||
#[test]
|
||||
fn brushed_strokes_that_a_mask_does_not_draw_leave_the_masks_as_they_are() {
|
||||
let d = Document::new(200.0, 200.0);
|
||||
let mut hidden = brushed_square();
|
||||
hidden.visible = false;
|
||||
let plain = rect(Rect::new(60.0, 50.0, 160.0, 150.0), (0.0, 0.0, 1.0));
|
||||
let group = Node::group(NodeId(0), vec![std::sync::Arc::new(plain.clone()), std::sync::Arc::new(hidden)]);
|
||||
assert!(!warns_of_brushes(d.clone(), group, true));
|
||||
// A brushed stroke without paint or width draws nothing.
|
||||
for (paint, width) in [(Paint::None, 3.0), (Paint::solid(Color::BLACK), 0.0)] {
|
||||
let mut idle = brushed_square();
|
||||
if let Some(vectorcraft_doc::AppearanceItem::Stroke(st)) = idle.appearance.items.last_mut() {
|
||||
(st.paint, st.width) = (paint, width);
|
||||
}
|
||||
assert!(!warns_of_brushes(d.clone(), idle, true), "width {width}");
|
||||
}
|
||||
assert!(warns_of_brushes(d.clone(), brushed_square(), true));
|
||||
// A group's own brushed stroke is drawn too: the export bakes it into a path among the
|
||||
// group's members.
|
||||
let mut stroked_group = Node::group(NodeId(0), vec![std::sync::Arc::new(plain.clone())]);
|
||||
stroked_group.appearance = brushed_square().appearance;
|
||||
assert!(warns_of_brushes(d.clone(), stroked_group, true));
|
||||
for (knockout_shape, expected) in [(false, false), (true, true)] {
|
||||
let mut masked = plain.clone();
|
||||
let mut mask = vectorcraft_doc::OpacityMask::new(brushed_square(), true);
|
||||
mask.linked = false;
|
||||
masked.extras_mut().mask = Some(Box::new(mask));
|
||||
assert_eq!(warns_of_brushes(d.clone(), masked, knockout_shape), expected, "knockout shape {knockout_shape}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A compound path's members are drawn with its own paint, so a brushed stroke on a member draws
|
||||
/// nothing; one on the compound path does.
|
||||
#[test]
|
||||
fn a_brushed_stroke_counts_on_a_compound_path_and_not_on_its_members() {
|
||||
let d = Document::new(200.0, 200.0);
|
||||
let compound = |members: Vec<Node>| {
|
||||
let children = members.into_iter().map(std::sync::Arc::new).collect();
|
||||
let mut c = Node::new(NodeId(0), vectorcraft_doc::NodeKind::Compound { children, rule: Default::default() });
|
||||
c.appearance = Appearance::basic(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0).into();
|
||||
c
|
||||
};
|
||||
assert!(!warns_of_brushes(d.clone(), compound(vec![brushed_square()]), true));
|
||||
let mut stroked = compound(vec![rect(Rect::new(60.0, 50.0, 160.0, 150.0), (1.0, 1.0, 1.0))]);
|
||||
stroked.appearance = brushed_square().appearance;
|
||||
assert!(warns_of_brushes(d, stroked, true));
|
||||
}
|
||||
|
||||
/// A pattern's tiles draw the brushed strokes in its art: an element filled with such a pattern
|
||||
/// counts, and one filled with a pattern without them does not.
|
||||
#[test]
|
||||
fn a_brushed_stroke_in_a_pattern_counts() {
|
||||
for (brushed_tile, expected) in [(true, true), (false, false)] {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let tile = if brushed_tile { brushed_square() } else { rect(Rect::new(60.0, 50.0, 160.0, 150.0), (1.0, 1.0, 1.0)) };
|
||||
d.patterns.push(vectorcraft_doc::pattern::PatternDef::new("Tile", vec![std::sync::Arc::new(tile)]));
|
||||
let pattern = Paint::Pattern { pattern: "Tile".into(), xf: vectorcraft_geom::Affine::IDENTITY };
|
||||
let filled = Node::path(NodeId(0), shapes::rectangle(Rect::new(60.0, 50.0, 160.0, 150.0)), Appearance::basic(pattern, Paint::None, 0.0));
|
||||
assert_eq!(warns_of_brushes(d, filled, true), expected, "brushed tile {brushed_tile}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A stroke whose brush the export does not know is drawn as a plain stroke, and a stroke without
|
||||
/// width draws nothing, not even its pattern's brushed strokes.
|
||||
#[test]
|
||||
fn strokes_that_draw_no_brush_art_leave_the_masks_as_they_are() {
|
||||
let mut unknown = brushed_square();
|
||||
if let Some(vectorcraft_doc::AppearanceItem::Stroke(st)) = unknown.appearance.items.last_mut() {
|
||||
st.brush = Some("No Such Brush".into());
|
||||
}
|
||||
assert!(!warns_of_brushes(Document::new(200.0, 200.0), unknown, true));
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
d.patterns.push(vectorcraft_doc::pattern::PatternDef::new("Tile", vec![std::sync::Arc::new(brushed_square())]));
|
||||
let pattern = Paint::Pattern { pattern: "Tile".into(), xf: vectorcraft_geom::Affine::IDENTITY };
|
||||
let mut thin = rect(Rect::new(60.0, 50.0, 160.0, 150.0), (0.0, 0.0, 1.0));
|
||||
thin.appearance.items.push(vectorcraft_doc::AppearanceItem::Stroke(vectorcraft_doc::StrokeLayer::new(pattern, 0.0)));
|
||||
assert!(!warns_of_brushes(d, thin, true));
|
||||
}
|
||||
|
||||
/// Type draws the brushed strokes in the patterns its characters are filled with and in the art
|
||||
/// of its inline graphics.
|
||||
#[test]
|
||||
fn brushed_strokes_in_type_patterns_and_inline_graphics_count() {
|
||||
let text = |d: &mut Document, fill: Paint, inline: bool| {
|
||||
let style = vectorcraft_doc::CharStyle { size: 60.0, fill, ..Default::default() };
|
||||
let mut t = vectorcraft_doc::TextObject::point(vectorcraft_geom::Point::new(60.0, 120.0), "Type", style.clone());
|
||||
if inline {
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Brushed".into(), art: std::sync::Arc::new(brushed_square()) });
|
||||
let mut run = vectorcraft_doc::TextRun::new("\u{FFFC}", style);
|
||||
run.inline = Some(vectorcraft_doc::InlineArt::new("Brushed"));
|
||||
t.runs.push(run);
|
||||
}
|
||||
Node::new(NodeId(0), vectorcraft_doc::NodeKind::Text(Box::new(t)))
|
||||
};
|
||||
let black = Paint::solid(Color::BLACK);
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let plain = text(&mut d, black.clone(), false);
|
||||
assert!(!warns_of_brushes(d.clone(), plain, true));
|
||||
d.patterns.push(vectorcraft_doc::pattern::PatternDef::new("Tile", vec![std::sync::Arc::new(brushed_square())]));
|
||||
let patterned = text(&mut d, Paint::Pattern { pattern: "Tile".into(), xf: vectorcraft_geom::Affine::IDENTITY }, false);
|
||||
assert!(warns_of_brushes(d, patterned, true));
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let inline = text(&mut d, black, true);
|
||||
assert!(warns_of_brushes(d, inline, true));
|
||||
}
|
||||
|
||||
/// Type draws the brushed strokes of its appearance as brush art too.
|
||||
#[test]
|
||||
fn a_brushed_stroke_on_type_counts() {
|
||||
let style = vectorcraft_doc::CharStyle { size: 60.0, ..Default::default() };
|
||||
let text = vectorcraft_doc::TextObject::point(vectorcraft_geom::Point::new(60.0, 120.0), "Type", style);
|
||||
let mut n = Node::new(NodeId(0), vectorcraft_doc::NodeKind::Text(Box::new(text)));
|
||||
n.appearance = brushed_square().appearance;
|
||||
assert!(warns_of_brushes(Document::new(200.0, 200.0), n, true));
|
||||
}
|
||||
|
||||
/// The brushed strokes a mask would draw are found in the opacity-mask art of a symbol's art too,
|
||||
/// and a disabled opacity mask, which is not drawn, holds none.
|
||||
#[test]
|
||||
fn brushed_strokes_in_mask_art_count_only_where_the_mask_is_on() {
|
||||
for (in_symbol, disabled, expected) in [(true, false, true), (false, true, false), (false, false, true)] {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let mut line = rect(Rect::new(0.0, 0.0, 100.0, 100.0), (1.0, 1.0, 1.0));
|
||||
let mut st = vectorcraft_doc::StrokeLayer::new(Paint::solid(Color::BLACK), 3.0);
|
||||
st.brush = Some("3 pt. Round".into());
|
||||
line.appearance.items.push(vectorcraft_doc::AppearanceItem::Stroke(st));
|
||||
let mut mask = vectorcraft_doc::OpacityMask::new(line, true);
|
||||
(mask.linked, mask.disabled) = (false, disabled);
|
||||
let mut masked = rect(Rect::new(0.0, 0.0, 100.0, 100.0), (0.0, 0.0, 1.0));
|
||||
masked.extras_mut().mask = Some(Box::new(mask));
|
||||
let mut upper = if in_symbol {
|
||||
let art = Node::group(NodeId(0), vec![std::sync::Arc::new(masked)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Masked".into(), art: std::sync::Arc::new(art) });
|
||||
let xf = vectorcraft_geom::Affine::translate((60.0, 50.0));
|
||||
Node::new(NodeId(0), vectorcraft_doc::NodeKind::SymbolInstance { symbol: "Masked".into(), xf })
|
||||
} else {
|
||||
masked.transform(vectorcraft_geom::Affine::translate((60.0, 50.0)), false);
|
||||
masked
|
||||
};
|
||||
(upper.id, upper.opacity, upper.knockout_shape) = (d.alloc_id(), 0.5, true);
|
||||
let mut lower = rect(Rect::new(20.0, 50.0, 120.0, 150.0), (1.0, 0.0, 0.0));
|
||||
(lower.id, lower.opacity, lower.knockout_shape) = (d.alloc_id(), 0.5, true);
|
||||
let mut g = Node::group(NodeId(0), vec![std::sync::Arc::new(lower), std::sync::Arc::new(upper)]);
|
||||
g.knockout = vectorcraft_doc::Knockout::On;
|
||||
add(&mut d, g);
|
||||
let r = export_with_report(&d, &PdfOptions::default()).unwrap();
|
||||
let warned = r.warnings.iter().any(|w| w == crate::export::KNOCKOUT_BRUSHES);
|
||||
assert_eq!(warned, expected, "symbol {in_symbol}, disabled {disabled}: {:?}", r.warnings);
|
||||
}
|
||||
}
|
||||
|
||||
/// PDF/A-2 allows transparency: a knockout group is written as one there too.
|
||||
#[test]
|
||||
fn a_pdf_a_2b_file_keeps_a_knockout_group() {
|
||||
let mut o = PdfOptions::default();
|
||||
(o.settings.standard, o.settings.compatibility) = (Standard::PdfA2b, Standard::PdfA2b.version());
|
||||
let bytes = export(&knockout_squares(3, false, false), &o).unwrap();
|
||||
assert_eq!(knockout_groups(&import(&bytes).unwrap()), [3]);
|
||||
}
|
||||
@@ -91,9 +91,7 @@ fn fixture(form: Form, kind: GradientKind, mode: ColorMode, origin: Point) -> (D
|
||||
/// Round trips preserve gradient placement for the form types the importer supports.
|
||||
#[test]
|
||||
fn gradients_in_forms_keep_their_position_when_read_back() {
|
||||
// The importer has a pre-existing mismatch on the knockout's synthesized nested masks;
|
||||
// Quartz below checks its actual PDF output, along with the other forms.
|
||||
for form in FORMS.into_iter().filter(|f| !matches!(f, Form::Knockout)) {
|
||||
for form in FORMS {
|
||||
for kind in [GradientKind::Linear, GradientKind::Radial] {
|
||||
for origin in [Point::ORIGIN, Point::new(125.0, -80.0)] {
|
||||
let (d, opts) = fixture(form, kind, ColorMode::Rgb, origin);
|
||||
|
||||
@@ -356,7 +356,7 @@ fn shape_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
|
||||
};
|
||||
let reshaped = effects::reshape(Some(symbols.document()), n, symbol.as_ref());
|
||||
let b = match &reshaped {
|
||||
Some(r) if matches!(n.kind, NodeKind::SymbolInstance { .. }) && effects::has_item_geometry(n) => instance_piece_bounds(symbols, r),
|
||||
Some(r) if has_instance_row_effects(n) => instance_piece_bounds(symbols, r),
|
||||
Some(r) if matches!(n.kind, NodeKind::Text(_)) || (matches!(n.kind, NodeKind::Image(_)) && !r.clips()) => {
|
||||
r.children().into_iter().flatten().fold(None, |acc, c| vectorcraft_geom::union_opt(acc, painted_reach(symbols, c)))
|
||||
}
|
||||
@@ -379,9 +379,16 @@ fn shape_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Is `n` a symbol instance whose active fill or stroke has geometry or raster effects that the
|
||||
/// fills and strokes of its art take ([`effects::has_item_geometry`],
|
||||
/// [`effects::has_instance_row_raster`])?
|
||||
fn has_instance_row_effects(n: &Node) -> bool {
|
||||
matches!(n.kind, NodeKind::SymbolInstance { .. }) && (effects::has_item_geometry(n) || effects::has_instance_row_raster(n))
|
||||
}
|
||||
|
||||
/// The bounds of `art`, the art [`effects::reshape`] made of a symbol instance whose active fill or
|
||||
/// stroke has geometry effects ([`effects::has_item_geometry`]): its paths measured with the
|
||||
/// geometry effects of their fills and strokes ([`visual_bounds`]), the symbol instances in it that
|
||||
/// stroke has geometry or raster effects ([`has_instance_row_effects`]): its paths measured with the
|
||||
/// effects of their fills and strokes, shadows and glows included ([`visual_bounds`]), the symbol instances in it that
|
||||
/// took those effects by their own reshaped art in turn, its groups and layers with their own fills
|
||||
/// and strokes by the art those paint ([`effects::evaluate_container`]), its clip groups by their
|
||||
/// clipping paths and other objects as [`Node::visual_bounds_with`] measures them. Hidden objects are left out.
|
||||
@@ -409,7 +416,7 @@ fn pieces_within(symbols: &SymbolBoxes, art: &Node, depth: usize, budget: &mut u
|
||||
g.children().into_iter().flatten().fold(None, |acc, c| vectorcraft_geom::union_opt(acc, pieces_within(symbols, c, depth, budget)))
|
||||
}
|
||||
NodeKind::Path { .. } | NodeKind::Compound { .. } if has_fx(art) => visual_bounds(symbols, art),
|
||||
NodeKind::SymbolInstance { symbol, .. } if depth < vectorcraft_doc::depth::MAX_DEPTH && effects::has_item_geometry(art) => {
|
||||
NodeKind::SymbolInstance { symbol, .. } if depth < vectorcraft_doc::depth::MAX_DEPTH && has_instance_row_effects(art) => {
|
||||
let doc = symbols.document();
|
||||
let reshaped = doc.symbols.iter().find(|s| s.name == *symbol).and_then(|s| {
|
||||
let count = s.art.count() as u64;
|
||||
@@ -639,7 +646,7 @@ fn cull_bounds_in(symbols: &SymbolBoxes, n: &Node, after_reshape: bool, brush: b
|
||||
};
|
||||
let reshaped = effects::reshape(Some(symbols.document()), n, symbol.as_ref());
|
||||
let b = match &reshaped {
|
||||
Some(r) if matches!(n.kind, NodeKind::SymbolInstance { .. }) && effects::has_item_geometry(n) => instance_piece_bounds(symbols, r),
|
||||
Some(r) if has_instance_row_effects(n) => instance_piece_bounds(symbols, r),
|
||||
Some(r) if matches!(n.kind, NodeKind::Text(_)) || (matches!(n.kind, NodeKind::Image(_)) && !r.clips()) => {
|
||||
r.children().into_iter().flatten().fold(None, |acc, c| vectorcraft_geom::union_opt(acc, inner(c)))
|
||||
}
|
||||
|
||||
@@ -20,12 +20,14 @@ pub(crate) fn has_fx(n: &Node) -> bool {
|
||||
/// Does `n` carry visible object-level effects that only apply through its art (type, images,
|
||||
/// symbol instances, live objects; Crop Marks and colour adjustments on anything), is it type with
|
||||
/// a geometry effect on a fill or stroke of its own or an image with a fill or stroke of its own
|
||||
/// that paints a shape ([`effects::has_item_geometry`]), is it a path whose Transform effect copies
|
||||
/// it with a gradient or changes its fills and strokes ([`effects::has_transform_art`]), or is it a
|
||||
/// group or layer with an appearance of its own (see [`Renderer::draw_object_fx`])?
|
||||
/// that paints a shape ([`effects::has_item_geometry`]), a symbol instance whose active fill or
|
||||
/// stroke has a raster effect ([`effects::has_instance_row_raster`]), is it a path whose Transform
|
||||
/// effect copies it with a gradient or changes its fills and strokes ([`effects::has_transform_art`]),
|
||||
/// or is it a group or layer with an appearance of its own (see [`Renderer::draw_object_fx`])?
|
||||
pub(crate) fn has_object_fx(n: &Node) -> bool {
|
||||
(effects::needs_outline(n) && (visible(&n.appearance.effects) || n.projection().is_some()))
|
||||
|| effects::has_item_geometry(n)
|
||||
|| effects::has_instance_row_raster(n)
|
||||
|| effects::has_transform_art(n)
|
||||
|| effects::has_container_appearance(n)
|
||||
|| effects::has_crop_marks(n)
|
||||
|
||||
@@ -31,6 +31,8 @@ image = { workspace = true }
|
||||
# Colour / transform / length parsing for the <text> fallback (MIT OR Apache-2.0; already used by usvg).
|
||||
svgtypes = "0.16"
|
||||
simplecss = "0.2.2"
|
||||
# Bidi classes for the first strong character of `unicode-bidi: plaintext` text (MIT OR Apache-2.0; already used by vectorcraft-text).
|
||||
unicode-bidi = "0.3.18"
|
||||
|
||||
[dev-dependencies]
|
||||
# Reading embedded font subsets back.
|
||||
|
||||
+149
-64
@@ -163,6 +163,18 @@ fn pin_to_page(n: &mut Node, m: Affine) {
|
||||
|
||||
/// What SVG Tiny 1.2 does without masks.
|
||||
const TINY_MASKS: &str = "SVG Tiny 1.2 has no masks: opacity masks are left out and knockout groups written as plain groups";
|
||||
|
||||
/// What a knockout group too large to mask is written as.
|
||||
const KNOCKOUT_SIZE: &str = "knockout groups with too many overlapping elements to mask are written as plain groups";
|
||||
|
||||
/// The most knockout shapes the masks of one knockout group's elements use between them: each
|
||||
/// mask uses the shapes of the elements above its own element that overlap it.
|
||||
const MAX_KNOCKOUT_MASK_SHAPES: usize = 1 << 16;
|
||||
|
||||
/// The most elements of one knockout group drawn through masks: finding the elements that
|
||||
/// overlap compares every pair.
|
||||
const MAX_MASKED_ELEMENTS: usize = 2048;
|
||||
|
||||
pub(super) const UNDECODED: &str = "an image couldn't be decoded and is missing from the picture";
|
||||
|
||||
/// The region of a mask or filter over art that has no box to measure: one covering any artwork.
|
||||
@@ -450,20 +462,39 @@ impl Writer<'_> {
|
||||
region
|
||||
}
|
||||
|
||||
/// The box of what each of `nodes` paints, as [`Self::paint_box`] measures it.
|
||||
fn paint_boxes(&mut self, nodes: &[&Node]) -> Vec<Option<Option<Rect>>> {
|
||||
let symbols = self.region_boxes.take().unwrap_or_else(|| self.doc.symbol_boxes());
|
||||
let mut art_reach = std::mem::take(&mut self.art_reach);
|
||||
let boxes = nodes.iter().map(|n| self.paint_box(&symbols, &mut art_reach, n)).collect();
|
||||
self.region_boxes = Some(symbols);
|
||||
self.art_reach = art_reach;
|
||||
boxes
|
||||
}
|
||||
|
||||
/// The box of what `n` paints in the current user space, as [`Self::region_attrs`] measures
|
||||
/// it: `Some(None)` when it has no box, `None` when the box can't be told (type, or nesting
|
||||
/// deeper than [`MAX_REACH_DEPTH`]).
|
||||
fn paint_box(&self, symbols: &vectorcraft_doc::SymbolBoxes, art_reach: &mut HashMap<String, f64>, n: &Node) -> Option<Option<Rect>> {
|
||||
let Some(v) = n.visual_bounds_with(symbols) else { return Some(None) };
|
||||
let r = self.paint_reach(symbols, art_reach, n, v, 0)?;
|
||||
let g = n.geometric_bounds_with(symbols).unwrap_or(v);
|
||||
let stroke = [g.x0 - v.x0, g.y0 - v.y0, v.x1 - g.x1, v.y1 - g.y1].into_iter().fold(0.0, f64::max);
|
||||
let scaled = self.xf.transform_rect_bbox(v.inflate(r, r));
|
||||
let unscaled = self.xf.transform_rect_bbox(g).inflate(stroke + r, stroke + r);
|
||||
Some(Some(scaled.union(unscaled)))
|
||||
}
|
||||
|
||||
/// [`Self::region_attrs`] with the document's symbol boxes and the symbols' art reach so far.
|
||||
fn region_of(&self, symbols: &vectorcraft_doc::SymbolBoxes, art_reach: &mut HashMap<String, f64>, nodes: &[&Node]) -> String {
|
||||
let mut painted: Option<Rect> = None;
|
||||
for n in nodes {
|
||||
let Some(v) = n.visual_bounds_with(symbols) else { continue };
|
||||
let Some(r) = self.paint_reach(symbols, art_reach, n, v, 0) else {
|
||||
let Some(b) = self.paint_box(symbols, art_reach, n) else {
|
||||
return BIG.to_string();
|
||||
};
|
||||
let g = n.geometric_bounds_with(symbols).unwrap_or(v);
|
||||
let stroke = [g.x0 - v.x0, g.y0 - v.y0, v.x1 - g.x1, v.y1 - g.y1].into_iter().fold(0.0, f64::max);
|
||||
let scaled = self.xf.transform_rect_bbox(v.inflate(r, r));
|
||||
let unscaled = self.xf.transform_rect_bbox(g).inflate(stroke + r, stroke + r);
|
||||
let both = scaled.union(unscaled);
|
||||
painted = Some(painted.map_or(both, |p| p.union(both)));
|
||||
if let Some(both) = b {
|
||||
painted = Some(painted.map_or(both, |p| p.union(both)));
|
||||
}
|
||||
}
|
||||
let Some(b) = painted.map(|b| b.inflate(2.0, 2.0)).filter(|b| b.is_finite()) else {
|
||||
return BIG.to_string();
|
||||
@@ -572,8 +603,10 @@ impl Writer<'_> {
|
||||
}
|
||||
|
||||
/// Children of the group being written. SVG has no knockout groups: in one, each element is
|
||||
/// drawn through a mask of where the elements above it don't paint (the same look for Normal
|
||||
/// blending). The masks nest, so element i sits inside the masks of elements i+1…n.
|
||||
/// drawn through one mask of where the elements above it that overlap it don't paint (the
|
||||
/// same look for Normal blending). The masks share one copy of each element's knockout shape,
|
||||
/// and the elements stay siblings however many there are. A group whose masks would hold more
|
||||
/// than [`MAX_KNOCKOUT_MASK_SHAPES`] shapes is written as a plain group, with a warning.
|
||||
pub(super) fn children(&mut self, children: &[std::sync::Arc<Node>]) {
|
||||
if self.knockout && self.tiny() {
|
||||
self.warn(TINY_MASKS);
|
||||
@@ -603,36 +636,124 @@ impl Writer<'_> {
|
||||
}
|
||||
return;
|
||||
}
|
||||
let Some((first, rest)) = elements.split_first() else { return };
|
||||
// The elements are written first, in paint order, so that each mask copies an element
|
||||
let all: Vec<&Node> = elements.iter().map(|c| &***c).collect();
|
||||
let Some(above) = self.knockout_masks(&all) else {
|
||||
self.warn(KNOCKOUT_SIZE);
|
||||
for c in elements {
|
||||
self.node(c);
|
||||
}
|
||||
return;
|
||||
};
|
||||
// The elements are written first, in paint order, so that each shape copies an element
|
||||
// with the symbol art count the element itself was written with
|
||||
// (`Self::symbol_nodes`): an instance left out of an element is left out of its mask.
|
||||
let (depth, nest) = (self.depth, rest.len());
|
||||
let first = self.detached(depth + nest, |w| w.node(first));
|
||||
let mut written = Vec::with_capacity(nest);
|
||||
for (i, c) in rest.iter().enumerate() {
|
||||
// (`Self::symbol_nodes`): an instance left out of an element is left out of its shape.
|
||||
let depth = self.depth;
|
||||
let mut written = Vec::with_capacity(elements.len());
|
||||
for (c, above) in elements.iter().zip(&above) {
|
||||
let before = self.symbol_nodes;
|
||||
written.push((before, self.detached(depth + nest - 1 - i, |w| w.node(c))));
|
||||
written.push((before, self.detached(depth + usize::from(!above.is_empty()), |w| w.node(c))));
|
||||
}
|
||||
let after = self.symbol_nodes;
|
||||
// Each mask covers what the group's elements paint.
|
||||
let all: Vec<&Node> = elements.iter().map(|c| &***c).collect();
|
||||
let region = self.region_attrs(&all);
|
||||
for (c, (before, _)) in rest.iter().zip(&written).rev() {
|
||||
self.symbol_nodes = *before;
|
||||
let mid = self.knockout_mask(c, ®ion);
|
||||
self.line(&format!("<g mask=\"url(#{mid})\">"));
|
||||
self.depth += 1;
|
||||
let mut shapes: Vec<Option<String>> = vec![None; elements.len()];
|
||||
for &j in above.iter().flatten() {
|
||||
if shapes[j].is_none() {
|
||||
self.symbol_nodes = written[j].0;
|
||||
shapes[j] = Some(self.knockout_shape(elements[j]));
|
||||
}
|
||||
}
|
||||
self.symbol_nodes = after;
|
||||
self.body.push_str(&first);
|
||||
for (_, body) in written {
|
||||
self.depth -= 1;
|
||||
self.line("</g>");
|
||||
for (above, (_, body)) in above.iter().zip(written) {
|
||||
if above.is_empty() {
|
||||
self.body.push_str(&body);
|
||||
continue;
|
||||
}
|
||||
let ids: Vec<&str> = above.iter().filter_map(|&j| shapes[j].as_deref()).collect();
|
||||
let mid = self.knockout_mask(&ids, ®ion);
|
||||
self.line(&format!("<g mask=\"url(#{mid})\">"));
|
||||
self.body.push_str(&body);
|
||||
self.line("</g>");
|
||||
}
|
||||
}
|
||||
|
||||
/// For each element of a knockout group, the indices of the elements above it whose painted
|
||||
/// boxes overlap its own (every one above it when a box can't be told). `None` when there are
|
||||
/// more than [`MAX_MASKED_ELEMENTS`] elements or the masks would hold more than
|
||||
/// [`MAX_KNOCKOUT_MASK_SHAPES`] shapes.
|
||||
fn knockout_masks(&mut self, elements: &[&Node]) -> Option<Vec<Vec<usize>>> {
|
||||
if elements.len() > MAX_MASKED_ELEMENTS {
|
||||
return None;
|
||||
}
|
||||
let boxes = self.paint_boxes(elements);
|
||||
let overlap = |a: Option<Option<Rect>>, b: Option<Option<Rect>>| match (a, b) {
|
||||
(Some(None), _) | (_, Some(None)) => false,
|
||||
(Some(Some(a)), Some(Some(b))) => a.x0 <= b.x1 + 2.0 && b.x0 <= a.x1 + 2.0 && a.y0 <= b.y1 + 2.0 && b.y0 <= a.y1 + 2.0,
|
||||
_ => true,
|
||||
};
|
||||
let mut shapes = 0usize;
|
||||
let mut out = Vec::with_capacity(elements.len());
|
||||
for (i, &below) in boxes.iter().enumerate() {
|
||||
let mut above = vec![];
|
||||
for (j, &over) in boxes.iter().enumerate().skip(i + 1) {
|
||||
if overlap(below, over) {
|
||||
shapes = shapes.checked_add(1).filter(|&s| s <= MAX_KNOCKOUT_MASK_SHAPES)?;
|
||||
above.push(j);
|
||||
}
|
||||
}
|
||||
out.push(above);
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// One copy of the knockout shape of `c` in the definitions: `c` at full object opacity
|
||||
/// without its own mask, unless those define its shape. Returns its id.
|
||||
fn knockout_shape(&mut self, c: &Node) -> String {
|
||||
let shape = if c.knockout_shape {
|
||||
c.clone()
|
||||
} else {
|
||||
let mut s = c.clone().with_mask(None);
|
||||
s.opacity = 1.0;
|
||||
s
|
||||
};
|
||||
let anonymous = std::mem::replace(&mut self.anonymous, true);
|
||||
let art = self.detached(2, |w| w.node(&shape));
|
||||
self.anonymous = anonymous;
|
||||
let id = self.fresh_id("knockout-art");
|
||||
self.def(1, &format!("<g id=\"{id}\">"));
|
||||
self.defs.push_str(&art);
|
||||
self.def(1, "</g>");
|
||||
id
|
||||
}
|
||||
|
||||
/// A mask that is 1 − the knockout shapes `ids` ([`Self::knockout_shape`]): white, then the
|
||||
/// shapes painted black. `region` is the attributes of the mask's region
|
||||
/// ([`Self::region_attrs`]), which the filter that paints the shapes black shares. Returns
|
||||
/// the mask id.
|
||||
fn knockout_mask(&mut self, ids: &[&str], region: &str) -> String {
|
||||
let filter = match self.knockout_filters.get(region) {
|
||||
Some(f) => f.clone(),
|
||||
None => {
|
||||
let f = self.fresh_id("knockout-shape");
|
||||
self.def(1, &format!("<filter id=\"{f}\" filterUnits=\"userSpaceOnUse\" {region}>"));
|
||||
self.def(2, "<feColorMatrix type=\"matrix\" values=\"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0\"/>");
|
||||
self.def(1, "</filter>");
|
||||
self.knockout_filters.insert(region.to_string(), f.clone());
|
||||
f
|
||||
}
|
||||
};
|
||||
let mid = self.fresh_id("knockout");
|
||||
self.def(1, &format!("<mask id=\"{mid}\" {MASK} {region}>"));
|
||||
self.def(2, &format!("<rect {region} fill=\"white\"/>"));
|
||||
self.def(2, &format!("<g filter=\"url(#{filter})\">"));
|
||||
for id in ids {
|
||||
self.def(3, &format!("<use xlink:href=\"#{id}\"/>"));
|
||||
}
|
||||
self.def(2, "</g>");
|
||||
self.def(1, "</mask>");
|
||||
mid
|
||||
}
|
||||
|
||||
/// The pieces of a partly transparent mesh-warped image (`elements`) as the elements of a
|
||||
/// knockout group ([`vectorcraft_effects::warp_pieces_translucent`]). Each piece is drawn
|
||||
/// through one mask of where the pieces above it that reach it don't cover
|
||||
@@ -683,42 +804,6 @@ impl Writer<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A mask that is 1 − the knockout shape of `c`: white, then `c` painted black (at full object
|
||||
/// opacity without its own mask, unless those define its shape). `region` is the attributes of
|
||||
/// the mask's region ([`Self::region_attrs`]), which the filter that paints `c` black shares.
|
||||
/// Returns the mask id.
|
||||
fn knockout_mask(&mut self, c: &Node, region: &str) -> String {
|
||||
let filter = match self.knockout_filters.get(region) {
|
||||
Some(f) => f.clone(),
|
||||
None => {
|
||||
let f = self.fresh_id("knockout-shape");
|
||||
self.def(1, &format!("<filter id=\"{f}\" filterUnits=\"userSpaceOnUse\" {region}>"));
|
||||
self.def(2, "<feColorMatrix type=\"matrix\" values=\"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0\"/>");
|
||||
self.def(1, "</filter>");
|
||||
self.knockout_filters.insert(region.to_string(), f.clone());
|
||||
f
|
||||
}
|
||||
};
|
||||
let shape = if c.knockout_shape {
|
||||
c.clone()
|
||||
} else {
|
||||
let mut s = c.clone().with_mask(None);
|
||||
s.opacity = 1.0;
|
||||
s
|
||||
};
|
||||
let anonymous = std::mem::replace(&mut self.anonymous, true);
|
||||
let art = self.detached(3, |w| w.node(&shape));
|
||||
self.anonymous = anonymous;
|
||||
let mid = self.fresh_id("knockout");
|
||||
self.def(1, &format!("<mask id=\"{mid}\" {MASK} {region}>"));
|
||||
self.def(2, &format!("<rect {region} fill=\"white\"/>"));
|
||||
self.def(2, &format!("<g filter=\"url(#{filter})\">"));
|
||||
self.defs.push_str(&art);
|
||||
self.def(2, "</g>");
|
||||
self.def(1, "</mask>");
|
||||
mid
|
||||
}
|
||||
|
||||
/// An object, inside `<a>` when it links to a URL (Attributes panel).
|
||||
pub(super) fn node(&mut self, n: &Node) {
|
||||
// Ruler guides are not written, not even as a link.
|
||||
|
||||
@@ -283,6 +283,11 @@ impl Writer<'_> {
|
||||
self.warn("type on a path with Insert Aki is outlined to keep its look");
|
||||
return self.text_outlines(n, t);
|
||||
}
|
||||
// Tsume does as well.
|
||||
if t.runs.iter().any(|r| r.style.tsume != 0.0) {
|
||||
self.warn("type on a path with Tsume is outlined to keep its look");
|
||||
return self.text_outlines(n, t);
|
||||
}
|
||||
// A `<textPath>` holds the characters of the text, not the bullets or numbers drawn
|
||||
// before them.
|
||||
if !lay.markers.is_empty() {
|
||||
@@ -715,13 +720,13 @@ const PLACE_TOLERANCE: f64 = 1e-6;
|
||||
/// starts a new segment at each character the layout draws elsewhere than a reader continuing
|
||||
/// the segment would ([`reader_advances`]): a Justify All line's letter gaps, the gaps between
|
||||
/// the characters of a justified Japanese line, and the spacing Mojikumi changes. Text with Insert
|
||||
/// Aki does so on every line, with `fewer` too.
|
||||
/// Aki or Tsume does so on every line, with `fewer` too.
|
||||
fn text_lines(t: &TextObject, lay: &vectorcraft_text::TextLayout, fewer: bool) -> Vec<Line> {
|
||||
let plain = t.plain_text();
|
||||
let mut lines = Vec::with_capacity(lay.lines.len());
|
||||
let (mut kerned, mut unkerned) = (None, None);
|
||||
// Insert Aki moves characters from where a reader draws them, with `fewer` too.
|
||||
let aki = t.runs.iter().any(|r| r.style.aki_left != 0.0 || r.style.aki_right != 0.0);
|
||||
// Insert Aki and Tsume move characters from where a reader draws them, with `fewer` too.
|
||||
let aki = t.runs.iter().any(|r| r.style.aki_left != 0.0 || r.style.aki_right != 0.0 || r.style.tsume != 0.0);
|
||||
for line in &lay.lines {
|
||||
let para = plain.as_bytes().get(..line.start).map_or(0, |b| b.iter().filter(|&&c| c == b'\n').count());
|
||||
let style = t.para_at(para);
|
||||
@@ -885,11 +890,12 @@ fn reader_advances(t: &TextObject, lay: &vectorcraft_text::TextLayout, kerning:
|
||||
if !kerning {
|
||||
r.style.kerning.get_or_insert(0.0);
|
||||
}
|
||||
// A reader puts no space before a line's first character (`kerningBefore`) and no Insert
|
||||
// Aki: the segment starts that much further along instead.
|
||||
// A reader puts no space before a line's first character (`kerningBefore`), no Insert
|
||||
// Aki and no Tsume: the segment starts that much further along instead.
|
||||
r.style.kerning_before = 0.0;
|
||||
r.style.aki_left = 0.0;
|
||||
r.style.aki_right = 0.0;
|
||||
r.style.tsume = 0.0;
|
||||
}
|
||||
let relaid;
|
||||
let glyphs = if set == *t {
|
||||
|
||||
+235
-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>>>,
|
||||
/// The first strong characters of each `<text>` read so far, by the `<text>`
|
||||
/// ([`Self::first_strong_rtl`]).
|
||||
strong: RefCell<HashMap<roxmltree::NodeId, Rc<FirstStrong>>>,
|
||||
/// 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,110 @@ 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. The characters of the whole
|
||||
/// `<text>` are read once, as one sequence, so an isolate that opens in one text node and
|
||||
/// closes in a later one is skipped whole ([`FirstStrong`]).
|
||||
fn first_strong_rtl(&self, n: XNode) -> Option<bool> {
|
||||
// The `<text>` (or outermost element) whose characters hold those of `n`.
|
||||
let mut root = n;
|
||||
while let Some(p) = root.parent_element().filter(|_| root.tag_name().name() != "text" && self.is_inline_child(root)) {
|
||||
root = p;
|
||||
}
|
||||
let strong = self.strong.borrow().get(&root.id()).cloned();
|
||||
let strong = strong.unwrap_or_else(|| {
|
||||
let mut chars = vec![];
|
||||
let mut spans = HashMap::new();
|
||||
self.inline_chars(root, &mut chars, &mut spans);
|
||||
let s = Rc::new(FirstStrong::new(&chars, spans));
|
||||
self.strong.borrow_mut().insert(root.id(), s.clone());
|
||||
s
|
||||
});
|
||||
strong.of(n.id())
|
||||
}
|
||||
|
||||
/// Appends the characters of element `n` and of its shown inline children to `chars`, in
|
||||
/// document order, and the range each of those elements holds to `spans`.
|
||||
fn inline_chars(&self, n: XNode, chars: &mut Vec<char>, spans: &mut HashMap<roxmltree::NodeId, Range<usize>>) {
|
||||
let start = chars.len();
|
||||
for c in n.children() {
|
||||
if c.is_text() {
|
||||
chars.extend(c.text().unwrap_or("").chars());
|
||||
} else if self.is_inline_child(c) {
|
||||
self.inline_chars(c, chars, spans);
|
||||
}
|
||||
}
|
||||
spans.insert(n.id(), start..chars.len());
|
||||
}
|
||||
|
||||
/// 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 +1066,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 +1353,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 +1373,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 +1396,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 +1452,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 +1478,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 +1583,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 +1594,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 +1665,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 +1767,80 @@ 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)
|
||||
}
|
||||
|
||||
/// The first strong characters (UAX #9 P2) of the elements of one `<text>`, found for each element
|
||||
/// in constant time from one pass over the text's characters. An isolate initiator is skipped to
|
||||
/// its matching PDI (UAX #9 BD9), and one without a matching PDI to the end; paragraph separators
|
||||
/// count as spaces, as in [`text_first_strong_rtl`].
|
||||
struct FirstStrong {
|
||||
/// The characters each element holds: a range of indices into `next`.
|
||||
spans: HashMap<roxmltree::NodeId, Range<usize>>,
|
||||
/// For each character and for the end: the index of the first strong character from there on,
|
||||
/// isolates skipped, and whether it is right to left; `None` when there is none.
|
||||
next: Vec<Option<(usize, bool)>>,
|
||||
}
|
||||
|
||||
impl FirstStrong {
|
||||
fn new(chars: &[char], spans: HashMap<roxmltree::NodeId, Range<usize>>) -> Self {
|
||||
use unicode_bidi::BidiClass::{AL, FSI, L, LRI, PDI, R, RLI};
|
||||
let class = |c: char| unicode_bidi::bidi_class(c);
|
||||
// The matching PDI of each isolate initiator.
|
||||
let mut pdi = vec![None; chars.len()];
|
||||
let mut open = vec![];
|
||||
for (i, &c) in chars.iter().enumerate() {
|
||||
match class(c) {
|
||||
LRI | RLI | FSI => open.push(i),
|
||||
PDI => {
|
||||
if let Some(at) = open.pop() {
|
||||
pdi[at] = Some(i);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut next = vec![None; chars.len() + 1];
|
||||
for (i, &c) in chars.iter().enumerate().rev() {
|
||||
next[i] = match class(c) {
|
||||
L => Some((i, false)),
|
||||
R | AL => Some((i, true)),
|
||||
LRI | RLI | FSI => pdi[i].and_then(|p| next[p + 1]),
|
||||
_ => next[i + 1],
|
||||
};
|
||||
}
|
||||
Self { spans, next }
|
||||
}
|
||||
|
||||
/// Whether the first strong character of element `n` is right to left; `None` when it holds
|
||||
/// none. An isolate that opens inside `n` and closes after it hides the rest of `n`.
|
||||
fn of(&self, n: roxmltree::NodeId) -> Option<bool> {
|
||||
let span = self.spans.get(&n)?;
|
||||
self.next.get(span.start).copied().flatten().filter(|&(at, _)| at < span.end).map(|(_, rtl)| rtl)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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());
|
||||
|
||||
@@ -262,3 +262,93 @@ fn a_mask_region_covers_a_blend_as_written() {
|
||||
let [x, y, w, h] = ["x", "y", "width", "height"].map(|a| mask.attribute(a).and_then(|v| v.parse::<f64>().ok()).unwrap_or(f64::NAN));
|
||||
assert!(x <= 0.0 && y <= 0.0 && x + w >= 95.0 && y + h >= 35.0 && w < 200.0 && h < 200.0, "{x} {y} {w} {h} in {svg}");
|
||||
}
|
||||
|
||||
/// A knockout group of `n` red, green and blue squares at 50% opacity that all overlap, over an
|
||||
/// opaque backdrop. `knockout_shape`: their opacity defines their knockout shape.
|
||||
fn knockout_squares(n: usize, knockout_shape: bool) -> Document {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let backdrop = rect(&mut d, Rect::new(0.0, 0.0, 200.0, 200.0), [230, 200, 50]);
|
||||
add(&mut d, backdrop);
|
||||
let colors = [[255, 0, 0], [0, 200, 0], [0, 0, 255]];
|
||||
let squares = (0..n)
|
||||
.map(|i| {
|
||||
let x = (i % 20) as f64 * 2.0;
|
||||
let mut s = rect(&mut d, Rect::new(20.0 + x, 50.0, 120.0 + x, 150.0), colors[i % 3]);
|
||||
(s.opacity, s.knockout_shape) = (0.5, knockout_shape);
|
||||
std::sync::Arc::new(s)
|
||||
})
|
||||
.collect();
|
||||
let mut group = Node::group(d.alloc_id(), squares);
|
||||
group.knockout = vectorcraft_doc::Knockout::On;
|
||||
add(&mut d, group);
|
||||
d
|
||||
}
|
||||
|
||||
/// The deepest an element of `svg` nests, counting the root element as 1.
|
||||
fn element_depth(svg: &str) -> usize {
|
||||
let xml = roxmltree::Document::parse_with_options(svg, roxmltree::ParsingOptions { allow_dtd: true, ..Default::default() }).unwrap();
|
||||
xml.descendants().filter(|n| n.is_element()).map(|n| n.ancestors().filter(|a| a.is_element()).count()).max().unwrap_or(0)
|
||||
}
|
||||
|
||||
/// The elements of a knockout group are siblings, each in one mask of the overlapping elements
|
||||
/// above it, however many there are: 200 translucent elements stay well within the nesting the
|
||||
/// web build reads (34 levels), and the file reads back with nothing merged. Where the elements'
|
||||
/// shapes are the area they paint, the file draws as the canvas does.
|
||||
#[test]
|
||||
fn a_knockout_group_of_many_translucent_elements_stays_shallow() {
|
||||
for knockout_shape in [false, true] {
|
||||
let d = knockout_squares(200, knockout_shape);
|
||||
let (svg, warnings) = vectorcraft_svg::export_with_report(&d, &ExportOptions::default());
|
||||
assert!(warnings.is_empty(), "{warnings:?}");
|
||||
assert_eq!(svg.matches("<mask").count(), 199);
|
||||
// Each shape is written once.
|
||||
assert_eq!(svg.matches("<use ").count(), 200 * 199 / 2);
|
||||
let depth = element_depth(&svg);
|
||||
assert!(depth < 12, "{depth}");
|
||||
let (back, notes) = vectorcraft_svg::import_with_report(&svg).unwrap();
|
||||
assert!(!notes.contains(&vectorcraft_doc::depth::merged_note()), "{notes:?}");
|
||||
assert!(back.layers[0].children().is_some_and(|c| !c.is_empty()));
|
||||
if !knockout_shape {
|
||||
let (file, canvas) = (resvg_render(&svg, 200, 200), render_artboard(&d));
|
||||
for (x, y) in [(30, 100), (100, 100), (150, 100), (10, 10)] {
|
||||
assert!(
|
||||
within(file.over_white(x, y), canvas.over_white(x, y), 3),
|
||||
"{x},{y}: {:?} {:?}",
|
||||
file.over_white(x, y),
|
||||
canvas.over_white(x, y)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// When the masks of a knockout group would use more than 65,536 shapes, the group is written as
|
||||
/// a plain group, with a warning.
|
||||
#[test]
|
||||
fn a_knockout_group_too_large_to_mask_is_a_plain_group_with_a_warning() {
|
||||
// 400 elements that all overlap: 79,800 shapes in their masks.
|
||||
let d = knockout_squares(400, false);
|
||||
let (svg, warnings) = vectorcraft_svg::export_with_report(&d, &ExportOptions::default());
|
||||
assert!(warnings.iter().any(|w| w.contains("too many overlapping elements")), "{warnings:?}");
|
||||
assert!(!svg.contains("<mask"));
|
||||
assert!(element_depth(&svg) < 12);
|
||||
}
|
||||
|
||||
/// Elements that don't overlap need no masks of each other.
|
||||
#[test]
|
||||
fn knockout_masks_hold_only_the_elements_that_overlap() {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let squares = (0..3)
|
||||
.map(|i| {
|
||||
let x = f64::from(i) * 60.0;
|
||||
let mut s = rect(&mut d, Rect::new(x, 0.0, x + 40.0, 40.0), [0, 0, 255]);
|
||||
s.opacity = 0.5;
|
||||
std::sync::Arc::new(s)
|
||||
})
|
||||
.collect();
|
||||
let mut group = Node::group(d.alloc_id(), squares);
|
||||
group.knockout = vectorcraft_doc::Knockout::On;
|
||||
add(&mut d, group);
|
||||
let svg = export(&d, &ExportOptions::default());
|
||||
assert!(!svg.contains("<mask"), "{svg}");
|
||||
}
|
||||
@@ -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,175 @@ 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),
|
||||
// An isolate that opens in one text node and closes in a later one is skipped whole.
|
||||
("⁧ab<tspan>⁩א</tspan>".to_string(), "\u{5D0}", Rtl),
|
||||
("⁧ab<tspan>cd</tspan>⁩א".to_string(), "\u{5D0}", Rtl),
|
||||
// An isolate that opens in a nested `plaintext` element and closes after it hides the rest of that element.
|
||||
(r#"<tspan unicode-bidi="plaintext">⁧ab</tspan>⁩א"#.to_string(), "\u{5D0}", Rtl),
|
||||
(r#"<tspan unicode-bidi="plaintext">⁧ab</tspan>⁩א"#.to_string(), "ab", Ltr),
|
||||
] {
|
||||
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]
|
||||
|
||||
@@ -196,6 +196,35 @@ fn insert_aki_places_each_moved_character() {
|
||||
assert!(!out.svg.contains("<textPath") && out.warnings.iter().any(|w| w.contains("Insert Aki")), "{:?}", out.warnings);
|
||||
}
|
||||
|
||||
/// Tsume: each character the layout moves by its Tsume starts a `<tspan>` where the character is
|
||||
/// drawn, so a reader draws the text where the canvas does, with fewer `<tspan>`s asked for too.
|
||||
/// On a path the type is outlined.
|
||||
#[test]
|
||||
fn tsume_places_each_moved_character() {
|
||||
let tight = CharStyle { tsume: 50.0, ..style(12.0) };
|
||||
let runs =
|
||||
vec![run("H", style(12.0)), run("O", tight.clone()), run("H ", style(12.0)), run("OHO\nHO", CharStyle { tsume: 100.0, ..style(12.0) })];
|
||||
for justify in [Justify::Left, Justify::Center, Justify::Right] {
|
||||
assert_drawn_in_place(&area_doc(runs.clone(), 200.0, justify));
|
||||
}
|
||||
let d = area_doc(runs, 200.0, Justify::Left);
|
||||
let plain = tspans(&export(&area_doc(vec![run("HOH OHO\nHO", style(12.0))], 200.0, Justify::Left), &ExportOptions::default())).len();
|
||||
let fewer = tspans(&export(&d, &ExportOptions { fewer_tspans: true, ..Default::default() })).len();
|
||||
assert!(fewer > plain, "each moved character is placed with fewer tspans too: {plain} {fewer}");
|
||||
let mut t = TextObject::point(Point::ZERO, "", style(12.0));
|
||||
t.runs = vec![run("HO", tight)];
|
||||
let mut line = BezPath::new();
|
||||
line.move_to((20.0, 100.0));
|
||||
line.line_to((280.0, 100.0));
|
||||
t.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
|
||||
let mut d = Document::new(300.0, 200.0);
|
||||
let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
|
||||
let l = d.layers[0].id;
|
||||
d.insert(Some(l), 0, n).unwrap();
|
||||
let out = vectorcraft_svg::export_full(&d, &ExportOptions::default(), None);
|
||||
assert!(!out.svg.contains("<textPath") && out.warnings.iter().any(|w| w.contains("Tsume")), "{:?}", out.warnings);
|
||||
}
|
||||
|
||||
/// Proportional Metrics (#966) is written as the `palt` feature, beside the character's own.
|
||||
#[test]
|
||||
fn proportional_metrics_write_palt() {
|
||||
@@ -260,6 +289,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)> {
|
||||
|
||||
@@ -56,6 +56,7 @@ pub(crate) fn paint_only(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
kerning_before: _,
|
||||
aki_left: _,
|
||||
aki_right: _,
|
||||
tsume: _,
|
||||
optical_kerning,
|
||||
baseline_shift,
|
||||
h_scale,
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
//! line. Each line the composer tries is placed from where the lines above it left the pen, at its
|
||||
//! own tallest metrics, as the line loop in [`super::flow`] places a composed line: the span it
|
||||
//! gets is the room across its whole height.
|
||||
//!
|
||||
//! A paragraph the frame cannot hold is still composed whole: a line the frame has no room for,
|
||||
//! and every line after it, is measured as wide as the span of the last line set in the frame,
|
||||
//! less its own indents. The line loop sets only the lines that fit, and the rest overflows or
|
||||
//! goes on in the next threaded frame, which composes it again at its own measures.
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::collections::HashMap;
|
||||
@@ -63,19 +68,33 @@ struct Start<'r> {
|
||||
/// The paragraph's first line (it takes the first-line indent).
|
||||
first: bool,
|
||||
pen: Pen<'r>,
|
||||
/// The line placed at each of the metrics it was tried with: its width and the pen after it,
|
||||
/// or `None` when the frame has no room for it.
|
||||
placed: Vec<(Metrics, Option<(f64, Pen<'r>)>)>,
|
||||
/// The width of the span the line above was set in, `None` for the paragraph's first line.
|
||||
span: Option<f64>,
|
||||
/// The line above was past the frame's end: this line is too, at the width of `span`.
|
||||
past: bool,
|
||||
/// The line placed at each of the metrics it was tried with, or `None` when it can't be set.
|
||||
placed: Vec<(Metrics, Option<Placed<'r>>)>,
|
||||
/// The metrics of glyphs `pos..to`, measured so far.
|
||||
to: usize,
|
||||
m: Option<Metrics>,
|
||||
}
|
||||
|
||||
/// What a line's placement depends on: its first glyph, whether it is the paragraph's first line,
|
||||
/// and the pen's cell, baselines, pending space, narrowest word without room and further spans.
|
||||
type Key = (usize, bool, usize, Option<u64>, Option<u64>, u64, u64, Vec<[u64; 3]>);
|
||||
/// A line set from a [`Start`]: its width, the pen after it, the width of the span it was set
|
||||
/// in, and whether it is past the frame's end (measured, but not in the frame).
|
||||
#[derive(Clone)]
|
||||
struct Placed<'r> {
|
||||
width: f64,
|
||||
pen: Pen<'r>,
|
||||
span: f64,
|
||||
past: bool,
|
||||
}
|
||||
|
||||
fn key(pos: usize, first: bool, pen: &Pen<'_>) -> Key {
|
||||
/// What a line's placement depends on: its first glyph, whether it is the paragraph's first line,
|
||||
/// the pen's cell, baselines, pending space, narrowest word without room and further spans, the
|
||||
/// span of the line above and whether the line is past the frame's end.
|
||||
type Key = (usize, bool, usize, Option<u64>, Option<u64>, u64, u64, Vec<[u64; 3]>, Option<u64>, bool);
|
||||
|
||||
fn key(pos: usize, first: bool, pen: &Pen<'_>, span: Option<f64>, past: bool) -> Key {
|
||||
(
|
||||
pos,
|
||||
first,
|
||||
@@ -85,6 +104,8 @@ fn key(pos: usize, first: bool, pen: &Pen<'_>) -> Key {
|
||||
pen.pending.to_bits(),
|
||||
pen.no_room_for.to_bits(),
|
||||
pen.queued.iter().map(|&(b, x0, x1)| [b.to_bits(), x0.to_bits(), x1.to_bits()]).collect(),
|
||||
span.map(f64::to_bits),
|
||||
past,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -159,13 +180,14 @@ impl<'a, 'r> Shaped<'a, 'r> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The start of a line at glyph `pos` with `pen`: an existing one when its placement is the same.
|
||||
fn start(&mut self, pos: usize, first: bool, pen: Pen<'r>) -> usize {
|
||||
let k = key(pos, first, &pen);
|
||||
/// The start of a line at glyph `pos` with `pen`, after a line set in a span `span` wide (past
|
||||
/// the frame's end with `past`): an existing one when its placement is the same.
|
||||
fn start(&mut self, pos: usize, first: bool, pen: Pen<'r>, span: Option<f64>, past: bool) -> usize {
|
||||
let k = key(pos, first, &pen, span, past);
|
||||
if let Some(&id) = self.known.get(&k) {
|
||||
return id;
|
||||
}
|
||||
self.starts.push(Start { pos, first, pen, placed: vec![], to: pos, m: None });
|
||||
self.starts.push(Start { pos, first, pen, span, past, placed: vec![], to: pos, m: None });
|
||||
let id = self.starts.len() - 1;
|
||||
self.known.insert(k, id);
|
||||
id
|
||||
@@ -177,7 +199,7 @@ impl Measure for Shaped<'_, '_> {
|
||||
|
||||
fn first(&mut self) -> usize {
|
||||
let pen = self.pen.clone();
|
||||
self.start(0, !self.continued, pen)
|
||||
self.start(0, !self.continued, pen, None, false)
|
||||
}
|
||||
|
||||
fn line(&mut self, &id: &usize, pos: usize, end: usize) -> Result<Option<(f64, usize)>, Exhausted> {
|
||||
@@ -186,14 +208,14 @@ impl Measure for Shaped<'_, '_> {
|
||||
}
|
||||
self.probes += 1;
|
||||
let Some(start) = self.starts.get_mut(id) else { return Ok(None) };
|
||||
let Some((width, pen)) = line(&self.para, start, pos, end)? else { return Ok(None) };
|
||||
Ok(Some((width, self.start(end, false, pen))))
|
||||
let Some(placed) = line(&self.para, start, pos, end)? else { return Ok(None) };
|
||||
Ok(Some((placed.width, self.start(end, false, placed.pen, Some(placed.span), placed.past))))
|
||||
}
|
||||
|
||||
/// A longer line is placed as this one is, and gets the same room, unless a glyph after it
|
||||
/// changes the line's tallest metrics.
|
||||
/// changes the line's tallest metrics. Past the frame's end every line is as wide.
|
||||
fn may_widen(&mut self, &id: &usize, pos: usize, end: usize) -> bool {
|
||||
let Some(start) = self.starts.get(id) else { return false };
|
||||
let Some(start) = self.starts.get(id).filter(|start| !start.past) else { return false };
|
||||
let m = if start.pos == pos && start.to == end { start.m } else { self.para.sg.get(pos..end).and_then(Metrics::max) };
|
||||
match (m, self.rest.get(end).copied().flatten()) {
|
||||
(Some(m), Some(rest)) => m.union(rest) != m,
|
||||
@@ -212,12 +234,14 @@ impl Drop for Shaped<'_, '_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The width of the line of glyphs `pos..end` from `start`, and the pen after it; `None` when
|
||||
/// the frame has no room for it.
|
||||
fn line<'r>(p: &Para<'_>, start: &mut Start<'r>, pos: usize, end: usize) -> Result<Option<(f64, Pen<'r>)>, Exhausted> {
|
||||
/// The line of glyphs `pos..end` from `start`; `None` when it can't be set.
|
||||
fn line<'r>(p: &Para<'_>, start: &mut Start<'r>, pos: usize, end: usize) -> Result<Option<Placed<'r>>, Exhausted> {
|
||||
if start.pos != pos || end <= pos {
|
||||
return Ok(None);
|
||||
}
|
||||
if start.past {
|
||||
return Ok(past_end(p, start));
|
||||
}
|
||||
// The line's tallest metrics, measured on from the glyphs already measured.
|
||||
if end < start.to {
|
||||
start.m = p.sg.get(pos..end).and_then(Metrics::max);
|
||||
@@ -239,9 +263,18 @@ fn line<'r>(p: &Para<'_>, start: &mut Start<'r>, pos: usize, end: usize) -> Resu
|
||||
Ok(placed)
|
||||
}
|
||||
|
||||
/// The line from `start` placed with metrics `m`: its width and the pen after it, as the line loop
|
||||
/// places and settles it; `None` when no frame cell has room for it.
|
||||
fn place<'r>(p: &Para<'_>, start: &Start<'r>, m: Metrics) -> Result<Option<(f64, Pen<'r>)>, Exhausted> {
|
||||
/// A line from `start` past the frame's end: as wide as the span of the last line set in the frame,
|
||||
/// less its indents, and the pen left where the frame ran out. `None` when the paragraph has no line
|
||||
/// in the frame to measure it by.
|
||||
fn past_end<'r>(p: &Para<'_>, start: &Start<'r>) -> Option<Placed<'r>> {
|
||||
let span = start.span.filter(|span| span.is_finite())?;
|
||||
let (ind_l, ind_r) = line_indents(p.para, start.first, p.mirror);
|
||||
Some(Placed { width: span - ind_l - ind_r, pen: start.pen.clone(), span, past: true })
|
||||
}
|
||||
|
||||
/// The line from `start` placed with metrics `m`, as the line loop places and settles it; past the
|
||||
/// frame's end ([`past_end`]) when no frame cell has room for it.
|
||||
fn place<'r>(p: &Para<'_>, start: &Start<'r>, m: Metrics) -> Result<Option<Placed<'r>>, Exhausted> {
|
||||
let (ind_l, ind_r) = line_indents(p.para, start.first, p.mirror);
|
||||
let word = word_width(p.sg, start.pos, p.line_end, p.para.kinsoku) - (p.start_credit)(start.pos);
|
||||
let mut pen = start.pen.clone();
|
||||
@@ -262,12 +295,12 @@ fn place<'r>(p: &Para<'_>, start: &Start<'r>, m: Metrics) -> Result<Option<(f64,
|
||||
for queued in &mut pen.queued {
|
||||
queued.0 = baseline;
|
||||
}
|
||||
return Ok(Some((x1 - x0 - ind_l - ind_r, pen)));
|
||||
return Ok(Some(Placed { width: x1 - x0 - ind_l - ind_r, pen, span: x1 - x0, past: false }));
|
||||
}
|
||||
Some(_) => return Ok(None),
|
||||
// Out of steps: the frame may still have room further down.
|
||||
None if p.steps.get() == 0 => return Err(Exhausted),
|
||||
None => return Ok(None),
|
||||
None => return Ok(past_end(p, start)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ use kurbo::Shape;
|
||||
use skrifa::MetadataProvider;
|
||||
use skrifa::charmap::Charmap;
|
||||
use skrifa::instance::Size;
|
||||
use vectorcraft_doc::text::{INLINE_CHAR, MAX_AKI};
|
||||
use vectorcraft_doc::text::{INLINE_CHAR, MAX_AKI, MAX_TSUME};
|
||||
use vectorcraft_doc::{CharStyle, Digits, InlineArt};
|
||||
|
||||
use crate::features::{OtFeatures, ligatures_suppressed_by};
|
||||
@@ -969,9 +969,9 @@ fn source_run_at(runs: &[(Range<usize>, &CharStyle)], span: &Range<usize>, byte:
|
||||
/// Can one glyph draw characters in styles `a` and `b`? OpenType features already apply to their
|
||||
/// own character ranges during shaping, so feature-only differences do not split a cluster.
|
||||
/// Horizontal type does not read Standard Vertical Roman Alignment or the tate-chu-yoko offsets.
|
||||
/// The space before a line's first character ([`space_before`]) and Insert Aki ([`aki`]) come
|
||||
/// from the cluster's first character, so they do not split a ligature or a letter from its marks
|
||||
/// either. Warichu Settings count only on characters marked Warichu ([`warichu_alike`]).
|
||||
/// The space before a line's first character ([`space_before`]), Insert Aki ([`aki`]) and Tsume
|
||||
/// ([`tighten`]) come from the cluster's first character, so they do not split a ligature or a
|
||||
/// letter from its marks either. Warichu Settings count only on characters marked Warichu ([`warichu_alike`]).
|
||||
fn drawn_alike(a: &CharStyle, b: &CharStyle, vertical: bool) -> bool {
|
||||
// Exhaustive, like `shapes_alike`: compare borrowed fields without copying feature lists.
|
||||
let CharStyle {
|
||||
@@ -986,6 +986,7 @@ fn drawn_alike(a: &CharStyle, b: &CharStyle, vertical: bool) -> bool {
|
||||
kerning_before,
|
||||
aki_left,
|
||||
aki_right,
|
||||
tsume,
|
||||
optical_kerning,
|
||||
baseline_shift,
|
||||
h_scale,
|
||||
@@ -1034,6 +1035,7 @@ fn drawn_alike(a: &CharStyle, b: &CharStyle, vertical: bool) -> bool {
|
||||
kerning_before == &a.kerning_before
|
||||
&& aki_left == &a.aki_left
|
||||
&& aki_right == &a.aki_right
|
||||
&& tsume == &a.tsume
|
||||
&& offsets == other_offsets
|
||||
&& font_family == &b.font_family
|
||||
&& font_style == &b.font_style
|
||||
@@ -1094,7 +1096,7 @@ pub(crate) fn warichu_alike(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
/// character style's name, leading, baseline shift, rotation, Character Alignment, Character
|
||||
/// Direction (text is shaped apart where its bidi level changes), Kashidas, the kerning (each glyph takes its
|
||||
/// run's, see [`shape_segment`]), the space before a line's first character ([`space_before`]),
|
||||
/// Insert Aki ([`aki`]), the OpenType features (each run's apply to its own characters, as features
|
||||
/// Insert Aki ([`aki`]), Tsume ([`tighten`]), the OpenType features (each run's apply to its own characters, as features
|
||||
/// over a range of the one buffer) and the tracking, as long as the tracking keeps or drops
|
||||
/// standard ligatures alike ([`crate::features::ligatures_suppressed_by`]).
|
||||
fn shapes_alike(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
@@ -1111,6 +1113,7 @@ fn shapes_alike(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
kerning_before: _,
|
||||
aki_left: _,
|
||||
aki_right: _,
|
||||
tsume: _,
|
||||
optical_kerning: _,
|
||||
baseline_shift: _,
|
||||
h_scale,
|
||||
@@ -1332,6 +1335,9 @@ struct Shaped {
|
||||
/// from their letters and the character-level pass matches ([`crate::harakat`]); empty
|
||||
/// otherwise (glyphs keep their shaping-cluster start).
|
||||
chars: Vec<u32>,
|
||||
/// Each glyph's x advance without the font's pair kerning, the width Tsume measures, when
|
||||
/// horizontal type with Tsume sets the same glyphs without it; empty otherwise.
|
||||
unkerned: Vec<i32>,
|
||||
}
|
||||
|
||||
/// The features that turn the whole-text features `base` into `own` over the clusters `range`, put
|
||||
@@ -1443,6 +1449,8 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
}
|
||||
ranged
|
||||
};
|
||||
// Horizontal type with Tsume on any of the segment's characters.
|
||||
let tsume_here = !feats.vertical && runs.get(source_runs.clone()).unwrap_or_default().iter().any(|(_, s)| s.tsume > 0.0);
|
||||
// Shape the segment as if all of it were in `st`, with the features `more` on top.
|
||||
let shape_in = |st: &CharStyle, more: &[Feature]| {
|
||||
// A BCP 47 tag is short ASCII; anything else (a file may hold anything) is shaped without one.
|
||||
@@ -1452,7 +1460,7 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
.filter(|l| l.len() <= 35 && l.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-'))
|
||||
.and_then(|l| l.parse::<harfrust::Language>().ok());
|
||||
let mut shaped = Shaped::default();
|
||||
let Shaped { raw, proportional, vpal, chars } = &mut shaped;
|
||||
let Shaped { raw, proportional, vpal, chars, unkerned } = &mut shaped;
|
||||
let hb_shaped = face.hb().map(|hb| {
|
||||
let shaper = face.shaper.shaper(&hb).instance(face.instance.as_ref()).build();
|
||||
// The baseline and character-level pass use exactly the same resolved features.
|
||||
@@ -1531,6 +1539,16 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
*proportional = full.iter().zip(raw).map(|(a, b)| a.2 != b.2).collect();
|
||||
}
|
||||
}
|
||||
if tsume_here {
|
||||
// Tsume measures each glyph on its own width: the same glyphs set without the
|
||||
// font's pair kerning (`kern` and `apkn`) give it.
|
||||
let mut bare = along_features.clone();
|
||||
bare.extend([b"kern", b"apkn"].map(|t| Feature::new(Tag::new(t), 0, range.start..=range.end)));
|
||||
let bare = shape(&bare, along);
|
||||
if same_glyphs(&bare) {
|
||||
*unkerned = bare.iter().map(|g| g.2).collect();
|
||||
}
|
||||
}
|
||||
if feats.proportional_vertical(st) {
|
||||
// `vpal` adjusts heights, which only top-to-bottom shaping applies: the same glyphs set
|
||||
// top to bottom with it (and the vertical kerning that goes with it, `vkrn` and `vapk`)
|
||||
@@ -1577,7 +1595,7 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
shaped
|
||||
};
|
||||
let first_optical = if optical(range.start) { Some(range.start) } else { boundaries().find(|&b| optical(b)) };
|
||||
let Shaped { raw, proportional, vpal, chars } = if let Some(first) = first_optical.filter(|_| !feats.vertical) {
|
||||
let Shaped { raw, proportional, vpal, chars, unkerned } = if let Some(first) = first_optical.filter(|_| !feats.vertical) {
|
||||
// The pair entering Optical keeps the left character's font kerning. Subsequent
|
||||
// Optical pairs use outline spacing, so font pair features stop at its second character.
|
||||
let optical_off = if first == range.start {
|
||||
@@ -1711,11 +1729,16 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
let last_in_cluster = gi + 1 == n || raw[gi + 1].1 as usize != cl;
|
||||
let ch = first_char(cl);
|
||||
let mut adv = xa as f64 * k * hs;
|
||||
// The glyph's own width, without the font's pair kerning and before the tracking, manual
|
||||
// kerning and Insert Aki after it: what Tsume measures.
|
||||
let mut own = unkerned.get(gi).map_or(adv, |&u| f64::from(u) * k * hs);
|
||||
// The chosen glyph takes its own advance.
|
||||
let chosen = chosen_at(gi);
|
||||
let gid = match chosen {
|
||||
Some(c) => {
|
||||
adv += (face.advance(c.gid) - face.advance(gid)) * k * hs;
|
||||
let wider = (face.advance(c.gid) - face.advance(gid)) * k * hs;
|
||||
adv += wider;
|
||||
own += wider;
|
||||
c.gid
|
||||
}
|
||||
None => gid,
|
||||
@@ -1723,7 +1746,8 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
let mut kern_shift = 0.0;
|
||||
let mut tracked = 0.0;
|
||||
let (mut aki_shift, mut aki_in_adv) = (0.0, 0.0);
|
||||
if ch == SOFT_HYPHEN || ch == '\u{2028}' {
|
||||
let invisible = ch == SOFT_HYPHEN || ch == '\u{2028}';
|
||||
if invisible {
|
||||
adv = 0.0;
|
||||
} else {
|
||||
let manual_kern = gst.kerning.map(|v| v / 1000.0 * spacing_em).unwrap_or(0.0);
|
||||
@@ -1785,9 +1809,61 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
cursive,
|
||||
hyphen_language: crate::hyphen::Language::from_tag(gst.language.as_deref()),
|
||||
});
|
||||
// Tsume takes its share of the blank space beside the cluster's ink once the cluster is
|
||||
// complete, in horizontal type.
|
||||
if last_in_cluster && !invisible && !feats.vertical && gst.tsume > 0.0 {
|
||||
let first = out.iter().rposition(|g| g.byte != cl).map_or(0, |i| i + 1);
|
||||
tighten(out.get_mut(first..).unwrap_or_default(), own, gst.tsume);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tsume ([`CharStyle::tsume`]) on one cluster: `glyphs` are its glyphs in the order they are
|
||||
/// drawn along the line, the last one with `own` as its width before the tracking, kerning and
|
||||
/// Insert Aki its advance holds. `tsume` percent of the blank space between the cluster's own width
|
||||
/// and its ink, on each side, comes out of the cluster's advance, and its glyphs are drawn the
|
||||
/// left side's share earlier. A cluster without ink, such as a space, or with a width that is not
|
||||
/// positive, keeps its place and width.
|
||||
fn tighten(glyphs: &mut [SGlyph], own: f64, tsume: f64) {
|
||||
let Some(last) = glyphs.len().checked_sub(1) else { return };
|
||||
let share = if tsume.is_nan() { 0.0 } else { tsume.clamp(0.0, MAX_TSUME) / 100.0 };
|
||||
let (mut pen, mut ink): (f64, Option<(f64, f64)>) = (0.0, None);
|
||||
for (i, g) in glyphs.iter().enumerate() {
|
||||
if let Some((a, b)) = ink_across(&g.face, g.gid) {
|
||||
let (a, b) = (pen + g.dx + a * g.sx, pen + g.dx + b * g.sx);
|
||||
let (lo, hi) = (a.min(b), a.max(b));
|
||||
ink = Some(ink.map_or((lo, hi), |(l, h)| (l.min(lo), h.max(hi))));
|
||||
}
|
||||
pen += if i == last { own } else { g.adv };
|
||||
}
|
||||
let Some((lo, hi)) = ink.filter(|(lo, hi)| lo.is_finite() && hi.is_finite()) else { return };
|
||||
if !(pen.is_finite() && pen > 0.0) {
|
||||
return;
|
||||
}
|
||||
let left = lo.clamp(0.0, pen) * share;
|
||||
let right = (pen - hi).clamp(0.0, pen) * share;
|
||||
for g in glyphs.iter_mut() {
|
||||
g.dx -= left;
|
||||
}
|
||||
if let Some(g) = glyphs.get_mut(last) {
|
||||
g.adv -= left + right;
|
||||
}
|
||||
}
|
||||
|
||||
/// The left and right edges of glyph `gid`'s outline in `face`, in font units; `None` for a glyph
|
||||
/// without an outline (a space, or a bitmap or color glyph without one).
|
||||
fn ink_across(face: &FontFace, gid: u32) -> Option<(f64, f64)> {
|
||||
let font = face.skrifa()?;
|
||||
let glyph = font.outline_glyphs().get(skrifa::GlyphId::new(gid))?;
|
||||
let mut pen = crate::fontdb::FlipPen(kurbo::BezPath::new());
|
||||
glyph.draw(skrifa::outline::DrawSettings::unhinted(Size::unscaled(), face.location()), &mut pen).ok()?;
|
||||
if pen.0.elements().is_empty() {
|
||||
return None;
|
||||
}
|
||||
let b = pen.0.bounding_box();
|
||||
Some((b.x0, b.x1))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod optical_tests {
|
||||
use super::*;
|
||||
|
||||
@@ -30,6 +30,7 @@ mod shape_plans;
|
||||
mod snap;
|
||||
mod svg_glyphs;
|
||||
mod sysfonts;
|
||||
mod tsume;
|
||||
mod typo;
|
||||
mod variable;
|
||||
mod versions;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -465,18 +465,72 @@ fn composer_x_mixed_sizes_in_a_frame_that_widens() {
|
||||
|
||||
#[test]
|
||||
fn composer_x_mixed_sizes_overflow_a_shaped_frame() {
|
||||
// A stepped frame too short for the paragraph: the lines that fit are set, in order, each once
|
||||
// and within its room, and the frame overflows.
|
||||
// A stepped frame too short for the paragraph: the every-line composer still breaks the lines
|
||||
// that fit as one paragraph, with the lines past the frame's end measured as wide as the last
|
||||
// line the frame holds. Here that is the narrow part, so the lines that fit break as in a frame
|
||||
// tall enough for the whole paragraph, and not as the single-line composer breaks them. They
|
||||
// are set in order, each once and within its room, and the frame overflows.
|
||||
let w = x_width();
|
||||
let mut t = stepped(w, w * 0.5, 40.0, 75.0);
|
||||
t.runs = with_x(sans(36.0));
|
||||
t.para.composer = Composer::EveryLine;
|
||||
let l = layout(db(), &t);
|
||||
let set = |bottom: f64, composer: Composer| {
|
||||
let mut t = stepped(w, w * 0.5, 40.0, bottom);
|
||||
t.runs = with_x(sans(36.0));
|
||||
t.para.composer = composer;
|
||||
let l = layout(db(), &t);
|
||||
(line_texts(&t, &l), l, t)
|
||||
};
|
||||
let (every, l, t) = set(75.0, Composer::EveryLine);
|
||||
assert!(l.overflow && l.lines.len() >= 2, "{:?}", l.lines);
|
||||
assert_eq!(l.lines[0].start, 0);
|
||||
assert!(l.lines.windows(2).all(|p| p[0].end == p[1].start && p[0].start < p[0].end), "{:?}", l.lines);
|
||||
assert!(past_their_room(&t, &l).is_empty(), "{:?}", l.lines);
|
||||
assert!(l.lines.iter().all(|li| li.baseline + li.descent <= 75.0 + 0.01), "{:?}", l.lines);
|
||||
assert_eq!(every, ["Destroy all creatures with toughness", "X or less. Then", "draw a card for each"]);
|
||||
let (whole, ..) = set(300.0, Composer::EveryLine);
|
||||
assert_eq!(every, whole[..every.len()]);
|
||||
let (greedy, ..) = set(75.0, Composer::SingleLine);
|
||||
assert_ne!(every, greedy);
|
||||
// A frame with no room for a line sets none.
|
||||
for composer in [Composer::EveryLine, Composer::SingleLine] {
|
||||
let mut t = stepped(w, w * 0.5, 4.0, 8.0);
|
||||
t.runs = with_x(sans(36.0));
|
||||
t.para.composer = composer;
|
||||
let l = layout(db(), &t);
|
||||
assert!(l.lines.is_empty() && l.overflow, "{composer:?}: {:?}", l.lines);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composer_x_mixed_sizes_overflow_around_text_wrap() {
|
||||
// A rectangle that wraps around an object beside its lower lines, too short for the
|
||||
// paragraph: the every-line composer breaks the lines that fit as one paragraph, each span
|
||||
// measured across its line's whole height, and the frame overflows. No glyph runs into the
|
||||
// object.
|
||||
let w = x_width();
|
||||
let object = Rect::new(w * 0.4, 40.0, w * 0.6, 70.0);
|
||||
let set = |composer: Composer| {
|
||||
let mut runs = with_x(sans(36.0));
|
||||
runs[0] = run("Destroy all creatures with toughness\u{a0}", sans(12.0));
|
||||
let mut t = area(runs, Rect::new(0.0, 0.0, w, 80.0));
|
||||
t.wrap = vec![vectorcraft_doc::WrapShape { path: PathData::from_bezpath(&object.to_path(0.1)), wrap: Default::default() }];
|
||||
t.para.composer = composer;
|
||||
let l = layout(db(), &t);
|
||||
(line_texts(&t, &l), l, t)
|
||||
};
|
||||
let (every, l, t) = set(Composer::EveryLine);
|
||||
assert!(l.overflow && l.lines.len() >= 3, "{:?}", l.lines);
|
||||
assert!(l.lines.windows(2).all(|p| p[0].end == p[1].start && p[0].start < p[0].end), "{:?}", l.lines);
|
||||
assert!(past_their_room(&t, &l).is_empty(), "{:?}", l.lines);
|
||||
let text = t.plain_text();
|
||||
let hits = l.glyphs.iter().filter(|g| {
|
||||
let li = &l.lines[g.line];
|
||||
let across = li.baseline - li.ascent < object.y1 && li.baseline + li.descent > object.y0;
|
||||
across && !text[g.byte..].starts_with(char::is_whitespace) && g.origin.x < object.x1 && g.origin.x + g.advance > object.x0
|
||||
});
|
||||
assert_eq!(hits.count(), 0, "{:?}", l.lines);
|
||||
// The lines that fit break as in `composer_x_mixed_sizes_around_text_wrap`'s taller frame.
|
||||
assert_eq!(every, ["Destroy all creatures with", "toughness\u{a0}X", "or less. Then", "draw a card for", "each creature"]);
|
||||
let (greedy, ..) = set(Composer::SingleLine);
|
||||
assert_ne!(every, greedy);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
//! Tsume (`CharStyle::tsume`): the share of the blank space on each side of a character's ink,
|
||||
//! inside its own width, that horizontal type takes away, the glyph keeping its shape.
|
||||
use super::{area, db, point, style};
|
||||
use crate::*;
|
||||
use kurbo::Shape;
|
||||
use vectorcraft_doc::{CharStyle, Justify};
|
||||
|
||||
/// Where glyph `byte` is drawn along a horizontal line.
|
||||
fn drawn_x(l: &TextLayout, byte: usize) -> f64 {
|
||||
l.glyphs.iter().find(|g| g.byte == byte).map(|g| g.xf.as_coeffs()[4]).unwrap()
|
||||
}
|
||||
|
||||
/// The left and right edges of glyph `byte`'s ink.
|
||||
fn ink(l: &TextLayout, byte: usize) -> (f64, f64) {
|
||||
let b = l.glyphs.iter().find(|g| g.byte == byte).map(|g| g.outline.bounding_box()).unwrap();
|
||||
(b.x0, b.x1)
|
||||
}
|
||||
|
||||
/// `t` with `f` applied to the style of the character at `byte`.
|
||||
fn with(t: &TextObject, byte: usize, f: impl Fn(&mut CharStyle)) -> TextObject {
|
||||
let mut t = t.clone();
|
||||
let end = edit::next_char(&t.plain_text(), byte);
|
||||
edit::style_range(&mut t.runs, byte, end, f);
|
||||
t
|
||||
}
|
||||
|
||||
/// How far each of `bytes` moved along the line from `a` to `b`.
|
||||
fn moves(a: &TextObject, b: &TextObject, bytes: &[usize]) -> Vec<f64> {
|
||||
let (la, lb) = (layout(db(), a), layout(db(), b));
|
||||
bytes.iter().map(|&i| drawn_x(&lb, i) - drawn_x(&la, i)).collect()
|
||||
}
|
||||
|
||||
fn near(values: &[f64], expected: &[f64]) -> bool {
|
||||
values.len() == expected.len() && values.iter().zip(expected).all(|(v, e)| (v - e).abs() < 1e-6)
|
||||
}
|
||||
|
||||
/// The blank space before and after the O of "HOH" in `t`'s layout: between the start of its
|
||||
/// width and its ink, and between its ink and the next character.
|
||||
fn o_bearings(t: &TextObject) -> (f64, f64) {
|
||||
let l = layout(db(), t);
|
||||
let (x0, x1) = ink(&l, 1);
|
||||
(x0 - drawn_x(&l, 1), drawn_x(&l, 2) - x1)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tsume_takes_its_share_of_the_space_beside_the_ink() {
|
||||
let plain = point("HOH", style(100.0));
|
||||
let (before, after) = o_bearings(&plain);
|
||||
assert!(before > 1.0 && after > 1.0, "{before} {after}");
|
||||
let plain_layout = layout(db(), &plain);
|
||||
let width = ink(&plain_layout, 1).1 - ink(&plain_layout, 1).0;
|
||||
for tsume in [25.0, 50.0, 100.0] {
|
||||
let tight = with(&plain, 1, |s| s.tsume = tsume);
|
||||
let share = tsume / 100.0;
|
||||
// The O's ink moves back by its share of the space before it; the H after it also moves
|
||||
// back by the share of the space after the O.
|
||||
let l = layout(db(), &tight);
|
||||
let (x0, x1) = ink(&l, 1);
|
||||
let moved = (x0 - ink(&plain_layout, 1).0, ink(&l, 2).0 - ink(&plain_layout, 2).0);
|
||||
assert!((moved.0 + before * share).abs() < 1e-6, "{tsume}: {moved:?}");
|
||||
assert!((moved.1 + (before + after) * share).abs() < 1e-6, "{tsume}: {moved:?}");
|
||||
// The glyph keeps its shape, and the H before it stays.
|
||||
assert!((x1 - x0 - width).abs() < 1e-6, "{tsume}: {} {width}", x1 - x0);
|
||||
assert!((ink(&l, 0).0 - ink(&plain_layout, 0).0).abs() < 1e-9);
|
||||
}
|
||||
// At 100% the O's ink starts where the H before it ends its width, and the next H starts its
|
||||
// width where the O's ink ends.
|
||||
let l = layout(db(), &with(&plain, 1, |s| s.tsume = 100.0));
|
||||
let cell_start = drawn_x(&plain_layout, 1);
|
||||
assert!((ink(&l, 1).0 - cell_start).abs() < 1e-6 && (drawn_x(&l, 2) - ink(&l, 1).1).abs() < 1e-6, "{:?} {cell_start}", ink(&l, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_out_of_range_and_spaces_keep_the_layout() {
|
||||
let plain = point("H O", style(100.0));
|
||||
let base = layout(db(), &plain);
|
||||
for (tsume, at) in [(0.0, 2), (f64::NAN, 2), (-50.0, 2), (100.0, 1)] {
|
||||
// 0, a value that is not a number, a negative value, and a space at 100%.
|
||||
let l = layout(db(), &with(&plain, at, |s| s.tsume = tsume));
|
||||
let xs: Vec<f64> = l.glyphs.iter().map(|g| g.xf.as_coeffs()[4]).collect();
|
||||
let base_xs: Vec<f64> = base.glyphs.iter().map(|g| g.xf.as_coeffs()[4]).collect();
|
||||
assert_eq!(xs, base_xs, "{tsume} at {at}");
|
||||
}
|
||||
// A value above 100 takes all the space, as 100 does.
|
||||
let most = moves(&plain, &with(&plain, 2, |s| s.tsume = 500.0), &[2]);
|
||||
let full = moves(&plain, &with(&plain, 2, |s| s.tsume = 100.0), &[2]);
|
||||
assert!(near(&most, &full) && full[0] < -1.0, "{most:?} {full:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracking_and_insert_aki_are_added_after_the_tsume() {
|
||||
let plain = point("HOH", style(100.0));
|
||||
let tight = moves(&plain, &with(&plain, 1, |s| s.tsume = 50.0), &[0, 1, 2]);
|
||||
let spaced = moves(&plain, &with(&plain, 1, |s| (s.tsume, s.aki_left, s.tracking) = (50.0, 250.0, 100.0)), &[0, 1, 2]);
|
||||
// 25 pt of Aki before the O, and 10 pt of tracking after it, on top of the Tsume.
|
||||
assert!(tight[1] < -1.0 && tight[2] < tight[1], "{tight:?}");
|
||||
assert!(near(&spaced, &[0.0, tight[1] + 25.0, tight[2] + 35.0]), "{tight:?} {spaced:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_font_kerning_is_added_after_the_tsume() {
|
||||
// An L, with blank space after its ink, before a letter the font kerns it with: its Tsume is
|
||||
// the same as before an H, which the font does not kern it with.
|
||||
let lh = point("LH", style(100.0));
|
||||
let kerned = ["LT", "LV", "LY", "LW"]
|
||||
.map(|s| point(s, style(100.0)))
|
||||
.into_iter()
|
||||
.find(|t| drawn_x(&layout(db(), t), 1) < drawn_x(&layout(db(), &lh), 1) - 1.0);
|
||||
let Some(kerned) = kerned else { panic!("the test font kerns none of the pairs") };
|
||||
let before_kerned = moves(&kerned, &with(&kerned, 0, |s| s.tsume = 100.0), &[1]);
|
||||
let before_h = moves(&lh, &with(&lh, 0, |s| s.tsume = 100.0), &[1]);
|
||||
assert!(near(&before_kerned, &before_h) && before_h[0] < -10.0, "{before_kerned:?} {before_h:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_letter_with_its_mark_takes_the_tsume_of_the_letter() {
|
||||
let plain = point("H\u{301}O", style(100.0));
|
||||
let after = "H\u{301}".len();
|
||||
let letter = moves(&plain, &with(&plain, 0, |s| s.tsume = 100.0), &[after]);
|
||||
let mark = moves(&plain, &with(&plain, 1, |s| s.tsume = 100.0), &[after]);
|
||||
assert!(letter[0] < -1.0 && near(&mark, &[0.0]), "{letter:?} {mark:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn justification_keeps_the_tsume() {
|
||||
let plain = area("HOH HOH", style(20.0), Rect::new(0.0, 0.0, 400.0, 400.0), Justify::JustifyAll);
|
||||
let tight = with(&plain, 1, |s| s.tsume = 100.0);
|
||||
let (lp, lt) = (layout(db(), &plain), layout(db(), &tight));
|
||||
// The O stays nearer the H before it by the space Tsume took before its ink.
|
||||
let gap = |l: &TextLayout| ink(l, 1).0 - ink(l, 0).1;
|
||||
assert!(gap(<) < gap(&lp) - 1.0, "{} {}", gap(&lp), gap(<));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vertical_type_keeps_its_spacing() {
|
||||
for text in ["§§", "ab"] {
|
||||
let mut plain = point(text, style(20.0));
|
||||
plain.xf = Affine::IDENTITY;
|
||||
plain.vertical = true;
|
||||
let centers = |t: &TextObject| layout(db(), t).glyphs.iter().map(|g| g.outline.bounding_box().center()).collect::<Vec<_>>();
|
||||
assert_eq!(centers(&with(&plain, 0, |s| s.tsume = 100.0)), centers(&plain), "{text}");
|
||||
}
|
||||
}
|
||||
@@ -397,6 +397,80 @@ fn a_continued_paragraph_fills_the_frame_it_continues_into() {
|
||||
assert_eq!(checked, 16);
|
||||
}
|
||||
|
||||
/// Area type in a frame `w` wide down to `y` and `narrow` wide below it, down to `bottom`.
|
||||
fn stepped_frame(w: f64, narrow: f64, y: f64, bottom: f64) -> TextObject {
|
||||
let mut t = frame(0.0);
|
||||
let path = kurbo::BezPath::from_vec(vec![
|
||||
kurbo::PathEl::MoveTo((0.0, 0.0).into()),
|
||||
kurbo::PathEl::LineTo((w, 0.0).into()),
|
||||
kurbo::PathEl::LineTo((w, y).into()),
|
||||
kurbo::PathEl::LineTo((narrow, y).into()),
|
||||
kurbo::PathEl::LineTo((narrow, bottom).into()),
|
||||
kurbo::PathEl::LineTo((0.0, bottom).into()),
|
||||
kurbo::PathEl::ClosePath,
|
||||
]);
|
||||
t.kind = TextKind::Area { frame: PathData::from_bezpath(&path) };
|
||||
t
|
||||
}
|
||||
|
||||
/// A paragraph of mixed type sizes that a stepped frame cannot hold goes on in the next frame. The
|
||||
/// every-line composer breaks the lines the first frame holds as one paragraph, as in a frame tall
|
||||
/// enough for all of it and not as the single-line composer breaks them, and the first frame
|
||||
/// takes the text of those lines. The next frame sets the rest at its own width, without the
|
||||
/// first-line indent, and no character is lost or repeated. The frames take the same shares as
|
||||
/// when each is measured with the whole rest of the story.
|
||||
#[test]
|
||||
fn a_mixed_size_paragraph_continued_from_a_shaped_frame_is_composed_in_both() {
|
||||
use vectorcraft_doc::Composer;
|
||||
let db = FontDb::global();
|
||||
let small = CharStyle { size: 12.0, ..CharStyle::default() };
|
||||
let big = CharStyle { size: 36.0, ..CharStyle::default() };
|
||||
let probe = layout(db, &TextObject::point(vectorcraft_geom::Point::ZERO, "Destroy all creatures with toughness X or", small.clone()));
|
||||
let w = probe.lines[0].x1 - probe.lines[0].x0 + 2.0;
|
||||
let story = vec![
|
||||
TextRun { text: "Destroy all creatures with toughness ".into(), style: small.clone(), inline: None },
|
||||
TextRun { text: "X".into(), style: big, inline: None },
|
||||
TextRun { text: " or less. Then draw a card for each creature destroyed this way.\nSecond paragraph.".into(), style: small, inline: None },
|
||||
];
|
||||
let text: String = story.iter().map(|r| r.text.as_str()).collect();
|
||||
let (a, tall) = (stepped_frame(w, w * 0.5, 40.0, 85.0), stepped_frame(w, w * 0.5, 40.0, 300.0));
|
||||
let b = sized(200.0, w * 0.75, 300.0);
|
||||
let texts = |lay: &crate::TextLayout, shown: &str| lay.lines.iter().map(|l| shown[l.start..l.end].trim_end().to_string()).collect::<Vec<_>>();
|
||||
let shares = |composer: Composer, indent: f64| {
|
||||
let para = ParaStyle { composer, first_line_indent: indent, ..Default::default() };
|
||||
let paras = [para.clone(), para];
|
||||
let parts = distribute(db, &[&a, &b], &story, ¶s);
|
||||
assert_eq!(parts.iter().map(|p| p.continues).collect::<Vec<_>>(), [false, true], "{composer:?}");
|
||||
let joined: String = parts.iter().flat_map(|p| &p.runs).map(|r| r.text.as_str()).collect();
|
||||
assert_eq!(joined, text, "{composer:?}: no character lost or repeated");
|
||||
same_as_whole_rest(db, &[&a, &b], &story, ¶s, &format!("{composer:?}, indent {indent}"));
|
||||
let shown = |p: &Slice| p.runs.iter().map(|r| r.text.as_str()).collect::<String>();
|
||||
let lays: Vec<_> = [&a, &b].iter().zip(&parts).map(|(f, p)| laid(db, f, p)).collect();
|
||||
assert!(lays.iter().all(|l| !l.overflow), "{composer:?}: each frame holds its share");
|
||||
let whole =
|
||||
laid(db, &tall, &Slice { runs: story.clone(), paras: paras.to_vec(), list_count: 0, list_level_counts: vec![], continues: false });
|
||||
let first = texts(&lays[0], &shown(&parts[0]));
|
||||
// The first frame takes the text of the lines it sets.
|
||||
assert_eq!(shown(&parts[0]).split_whitespace().collect::<Vec<_>>(), first.join(" ").split_whitespace().collect::<Vec<_>>());
|
||||
(first, texts(&whole, &text), lays, parts)
|
||||
};
|
||||
for indent in [0.0, 20.0] {
|
||||
let (every, whole, lays, parts) = shares(Composer::EveryLine, indent);
|
||||
assert!(every.len() >= 2, "{every:?}");
|
||||
assert_eq!(every, whole[..every.len()], "indent {indent}");
|
||||
if indent == 0.0 {
|
||||
assert_eq!(every, ["Destroy all creatures with toughness", "X or less. Then", "draw a card for each"]);
|
||||
let (greedy, ..) = shares(Composer::SingleLine, indent);
|
||||
assert_eq!(greedy, ["Destroy all creatures with toughness X", "or less. Then draw a", "card for each", "creature destroyed"]);
|
||||
}
|
||||
// The next frame starts at its left edge, and its lines keep within it.
|
||||
assert!((line_start(&lays[1], 0) - 200.0).abs() < 1e-6, "{}", line_start(&lays[1], 0));
|
||||
assert!(lays[1].lines.iter().all(|l| l.x1 <= 200.0 + w * 0.75 + 1e-6), "{:?}", lays[1].lines);
|
||||
assert!((line_start(&lays[0], 0) - indent).abs() < 1e-6, "{}", line_start(&lays[0], 0));
|
||||
assert!(parts[1].continues && lays[1].lines.len() >= 2, "{:?}", lays[1].lines);
|
||||
}
|
||||
}
|
||||
|
||||
/// `frames` holding the runs of `parts`, for [`continued`].
|
||||
fn filled(frames: &[&TextObject], parts: &[Slice]) -> Vec<TextObject> {
|
||||
frames
|
||||
|
||||
@@ -19,7 +19,7 @@ mod lights;
|
||||
pub use lights::{Light, Lights, MAX_LIGHTS};
|
||||
mod caps;
|
||||
mod mesh;
|
||||
pub use mesh::{Mesh, extrude_mesh, inflate_meshes, plane_mesh, revolve_mesh};
|
||||
pub use mesh::{Mesh, MeshedSolid, extrude_mesh, extrude_meshed, inflate_meshed, inflate_meshes, plane_mesh, revolve_mesh};
|
||||
mod shadow;
|
||||
pub use shadow::{MAX_SHADOW_FACES, Shadow, ShadowCaster, ShadowPlane, ShadowPosition, TOO_COMPLEX_SHADOW};
|
||||
|
||||
@@ -371,6 +371,10 @@ pub struct Face {
|
||||
/// For a face of the swept side (not an end cap): where its corners lie on the side surface,
|
||||
/// for a graphic mapped onto it.
|
||||
pub surface: Option<SurfaceCell>,
|
||||
/// The face's plane in view space and the projection it was drawn with, for its depth at a
|
||||
/// point of the drawing ([`DepthPlane::depth_at`]). `None` when its corners lie more than
|
||||
/// 0.001 pt off one plane or a corner is at or past the eye.
|
||||
pub plane: Option<DepthPlane>,
|
||||
}
|
||||
|
||||
/// Where a face of Revolve's swept side lies on the side surface, unrolled flat: x runs around the
|
||||
@@ -638,6 +642,14 @@ fn project_face(vertices: &[V3], normals: Option<&[V3]>, o: Revolve, origin: Poi
|
||||
normal = V3(-normal.0, -normal.1, -normal.2);
|
||||
}
|
||||
let flat_brightness = brightness(&o, normal, light);
|
||||
// As for an extruded face: no plane when a corner lies off it, or when the projection holds a
|
||||
// corner just in front of the eye, so the drawing no longer matches the plane's projection.
|
||||
let centre = vertices.iter().fold(V3::default(), |s, v| V3(s.0 + v.0, s.1 + v.1, s.2 + v.2));
|
||||
let centre = V3(centre.0 / vertices.len() as f64, centre.1 / vertices.len() as f64, centre.2 / vertices.len() as f64);
|
||||
let offset = normal.dot(centre);
|
||||
let held = eye.is_some_and(|e| vertices.iter().any(|v| e - v.2 < e * 1e-3));
|
||||
let flat = !held && vertices.iter().all(|v| (normal.dot(*v) - offset).abs() <= 1e-3);
|
||||
let plane = flat.then_some(DepthPlane { normal: [normal.0, normal.1, normal.2], offset, center: origin, eye });
|
||||
let points: Vec<_> = vertices
|
||||
.iter()
|
||||
.map(|v| {
|
||||
@@ -707,6 +719,7 @@ fn project_face(vertices: &[V3], normals: Option<&[V3]>, o: Revolve, origin: Poi
|
||||
cap: false,
|
||||
visibility_pieces: None,
|
||||
surface: None,
|
||||
plane,
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
|
||||
use vectorcraft_geom::{FillRule, PathData, Rect};
|
||||
|
||||
use crate::solid::{Face3, extrude_faces, inflate_faces, plane_faces};
|
||||
use crate::{Extrude, Inflate, Revolve, V3, View};
|
||||
use crate::solid::{Face3, Keep, extrude_faces, inflate_faces, plane_faces};
|
||||
use crate::{Extrude, Facet, Inflate, Revolve, V3, View};
|
||||
|
||||
/// Most steps the triangulation of one solid may take: a face's ears and bridges each test the
|
||||
/// face's other corners.
|
||||
@@ -56,12 +56,12 @@ impl Mesh {
|
||||
|
||||
/// Extrude & Bevel's solid ([`crate::extrude`]) as a mesh.
|
||||
pub fn extrude_mesh(path: &PathData, rule: FillRule, bounds: Rect, options: Extrude, view: View) -> Result<Mesh, String> {
|
||||
extrude_faces(path, rule, bounds, options, view, true).and_then(|(_, faces, _)| mesh(&faces))
|
||||
extrude_faces(path, rule, bounds, options, view, Keep::Faces).and_then(|(_, faces, _)| mesh(&faces?))
|
||||
}
|
||||
|
||||
/// Rotate's plane ([`crate::plane`]) as a mesh.
|
||||
pub fn plane_mesh(path: &PathData, rule: FillRule, bounds: Rect, view: View) -> Result<Mesh, String> {
|
||||
plane_faces(path, rule, bounds, view, true).and_then(|(_, faces, _)| mesh(&faces))
|
||||
plane_faces(path, rule, bounds, view, Keep::Faces).and_then(|(_, faces, _)| mesh(&faces?))
|
||||
}
|
||||
|
||||
/// Inflate's solids for several shapes of one object ([`crate::inflate_together`]) as meshes, one
|
||||
@@ -72,7 +72,49 @@ pub fn inflate_meshes(
|
||||
options: Inflate,
|
||||
view: View,
|
||||
) -> impl Iterator<Item = Result<Mesh, String>> + use<> {
|
||||
inflate_faces(shapes, bounds, options, view, true).map(|r| r.and_then(|(_, faces, _)| mesh(&faces)))
|
||||
inflate_faces(shapes, bounds, options, view, Keep::Faces).map(|r| r.and_then(|(_, faces, _)| mesh(&faces?)))
|
||||
}
|
||||
|
||||
/// One solid built once for both its drawing and its mesh: the facets [`crate::extrude`] or
|
||||
/// [`crate::inflate_each`] gives, and the faces before projection that [`MeshedSolid::mesh`] cuts
|
||||
/// into the mesh [`extrude_mesh`] or [`inflate_meshes`] gives.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MeshedSolid {
|
||||
pub facets: Vec<Facet>,
|
||||
/// The faces before projection, or the error the mesh alone gives when there are too many.
|
||||
faces: Result<Vec<Face3>, String>,
|
||||
}
|
||||
|
||||
/// A solid with no facets and an empty mesh.
|
||||
impl Default for MeshedSolid {
|
||||
fn default() -> Self {
|
||||
Self { facets: vec![], faces: Ok(vec![]) }
|
||||
}
|
||||
}
|
||||
|
||||
impl MeshedSolid {
|
||||
/// The solid as a mesh. The faces are cut into triangles on each call.
|
||||
pub fn mesh(&self) -> Result<Mesh, String> {
|
||||
mesh(self.faces.as_ref().map_err(String::clone)?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extrude & Bevel's solid ([`crate::extrude`]) and its mesh ([`extrude_mesh`]) from one build. An
|
||||
/// error where [`crate::extrude`] gives one.
|
||||
pub fn extrude_meshed(path: &PathData, rule: FillRule, bounds: Rect, options: Extrude, view: View) -> Result<MeshedSolid, String> {
|
||||
extrude_faces(path, rule, bounds, options, view, Keep::Both).map(|(facets, faces, _)| MeshedSolid { facets, faces })
|
||||
}
|
||||
|
||||
/// Inflate's solids for several shapes of one object ([`crate::inflate_each`]) and their meshes
|
||||
/// ([`inflate_meshes`]), each solid built once, as the iterator reaches it. An error where
|
||||
/// [`crate::inflate_each`] gives one.
|
||||
pub fn inflate_meshed(
|
||||
shapes: &[(&PathData, FillRule)],
|
||||
bounds: Rect,
|
||||
options: Inflate,
|
||||
view: View,
|
||||
) -> impl Iterator<Item = Result<MeshedSolid, String>> + use<> {
|
||||
inflate_faces(shapes, bounds, options, view, Keep::Both).map(|r| r.map(|(facets, faces, _)| MeshedSolid { facets, faces }))
|
||||
}
|
||||
|
||||
/// Revolve's surface ([`crate::revolve`]) as a mesh. As the effect draws it, a sweep of less than
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||
use vectorcraft_geom::{BezPath, FillRule, PathData, Rect};
|
||||
|
||||
use super::*;
|
||||
use crate::{Bevel, Edge};
|
||||
use crate::{Bevel, BevelShape, Edge};
|
||||
|
||||
fn square(b: &mut BezPath, x: f64, y: f64, s: f64, clockwise: bool) {
|
||||
b.move_to((x, y));
|
||||
@@ -372,3 +372,130 @@ fn join_numbers_each_parts_corners_after_the_last() {
|
||||
assert!(both.triangles.iter().all(|t| t.iter().all(|i| (*i as usize) < both.positions.len())));
|
||||
assert_eq!(both.triangles[one.triangles.len()][0], one.triangles[0][0] + one.positions.len() as u32);
|
||||
}
|
||||
|
||||
/// A mesh's positions and normals as bits, and its triangles.
|
||||
type MeshBits = (Vec<[u64; 3]>, Vec<[u64; 3]>, Vec<[u32; 3]>);
|
||||
|
||||
/// The bits of every number in `m`, for comparing two meshes exactly.
|
||||
fn bits(m: &Mesh) -> MeshBits {
|
||||
(m.positions.iter().map(|p| p.map(f64::to_bits)).collect(), m.normals.iter().map(|n| n.map(f64::to_bits)).collect(), m.triangles.clone())
|
||||
}
|
||||
|
||||
/// A star of `points` points about (100, 100), with a square hole: curved and straight edges both.
|
||||
fn star(points: usize) -> (PathData, Rect) {
|
||||
let mut b = BezPath::new();
|
||||
for k in 0..2 * points {
|
||||
let (s, c) = (k as f64 * std::f64::consts::PI / points as f64).sin_cos();
|
||||
let r = if k % 2 == 0 { 90.0 } else { 45.0 };
|
||||
let p = (100.0 + r * c, 100.0 + r * s);
|
||||
if k == 0 { b.move_to(p) } else { b.curve_to((p.0 + 3.0, p.1), (p.0, p.1 + 3.0), p) }
|
||||
}
|
||||
b.close_path();
|
||||
square(&mut b, 90.0, 90.0, 20.0, true);
|
||||
shape(&b)
|
||||
}
|
||||
|
||||
/// The views the solids are compared in: straight on, turned, with perspective, and with colored
|
||||
/// lights (Inflate's smooth triangles).
|
||||
fn views() -> Vec<View> {
|
||||
let turned = View { rotation_x: -25.0, rotation_y: 35.0, rotation_z: 10.0, ..View::default() };
|
||||
vec![front(), turned, View { perspective: 60.0, ..turned }, View { light_color: [1.0, 0.5, 0.25], ..turned }]
|
||||
}
|
||||
|
||||
/// Extrude & Bevel built once for its facets and its mesh gives what [`crate::extrude`] and
|
||||
/// [`extrude_mesh`] give apart, to the bit, errors included.
|
||||
#[test]
|
||||
fn an_extruded_solid_and_its_mesh_match_the_two_built_apart() {
|
||||
let bevel = Bevel { shape: BevelShape::Rounded, width: 6.0, height: 5.0, ..Bevel::default() };
|
||||
let options = [
|
||||
Extrude::default(),
|
||||
Extrude { depth: 0.0, ..Extrude::default() },
|
||||
Extrude { cap: false, ..Extrude::default() },
|
||||
Extrude { hidden: true, ..Extrude::default() },
|
||||
Extrude { twist: 90.0, taper: 0.6, ..Extrude::default() },
|
||||
Extrude { bevel: Some(bevel), ..Extrude::default() },
|
||||
Extrude { bevel: Some(Bevel { both_sides: true, inside: true, ..bevel }), twist: 45.0, ..Extrude::default() },
|
||||
];
|
||||
let (p, r) = star(7);
|
||||
for view in views() {
|
||||
for o in options {
|
||||
let meshed = extrude_meshed(&p, FillRule::NonZero, r, o, view).unwrap();
|
||||
let facets = crate::extrude(&p, FillRule::NonZero, r, o, view).unwrap();
|
||||
assert_eq!(format!("{:?}", meshed.facets), format!("{facets:?}"), "{o:?} {view:?}");
|
||||
let apart = extrude_mesh(&p, FillRule::NonZero, r, o, view).unwrap();
|
||||
assert!(!apart.is_empty());
|
||||
assert_eq!(bits(&meshed.mesh().unwrap()), bits(&apart), "{o:?} {view:?}");
|
||||
}
|
||||
}
|
||||
// A solid that can't be built gives the same error.
|
||||
let flat = Rect::new(0.0, 0.0, f64::INFINITY, 10.0);
|
||||
let o = Extrude::default();
|
||||
let error = crate::extrude(&p, FillRule::NonZero, flat, o, front()).err();
|
||||
assert!(error.is_some());
|
||||
assert_eq!(extrude_meshed(&p, FillRule::NonZero, flat, o, front()).err(), error);
|
||||
}
|
||||
|
||||
/// Inflate's solids built once for their facets and meshes give what [`crate::inflate_each`] and
|
||||
/// [`inflate_meshes`] give apart, to the bit: several puffs under one eye, both sides, open
|
||||
/// backs, and twisted sides cut finer than room is left for.
|
||||
#[test]
|
||||
fn inflated_solids_and_their_meshes_match_the_two_built_apart() {
|
||||
let (star, _) = star(5);
|
||||
let mut b = BezPath::new();
|
||||
square(&mut b, 200.0, 20.0, 60.0, false);
|
||||
let (box_, _) = shape(&b);
|
||||
let ring: Vec<vectorcraft_geom::Point> = (0..600)
|
||||
.map(|k| {
|
||||
let (s, c) = (k as f64 * std::f64::consts::TAU / 600.0).sin_cos();
|
||||
vectorcraft_geom::Point::new(100.0 + 90.0 * c, 100.0 + 90.0 * s)
|
||||
})
|
||||
.collect();
|
||||
let ring = PathData::single(vectorcraft_geom::SubPath::polyline(&ring, true));
|
||||
let sets = [vec![(&star, FillRule::NonZero), (&box_, FillRule::NonZero), (&star, FillRule::EvenOdd)], vec![(&ring, FillRule::NonZero)]];
|
||||
let bounds = Rect::new(0.0, 0.0, 280.0, 200.0);
|
||||
let options = [
|
||||
Inflate::default(),
|
||||
Inflate { depth: 30.0, both_sides: true, ..Inflate::default() },
|
||||
Inflate { depth: 20.0, cap: false, volume: 80.0, ..Inflate::default() },
|
||||
Inflate { depth: 40.0, twist: 360.0, taper: 0.7, both_sides: true, ..Inflate::default() },
|
||||
// Thin and barely twisted: the star's sides want more pieces than room is left after its
|
||||
// front and back, and the mesh, which keeps the faces turned away, has less room left.
|
||||
Inflate { depth: 0.1, twist: 0.1, both_sides: true, ..Inflate::default() },
|
||||
];
|
||||
for shapes in &sets {
|
||||
for view in views() {
|
||||
for o in options {
|
||||
let meshed: Vec<_> = inflate_meshed(shapes, bounds, o, view).collect();
|
||||
let facets: Vec<_> = crate::inflate_each(shapes, bounds, o, view).collect();
|
||||
let apart: Vec<_> = inflate_meshes(shapes, bounds, o, view).collect();
|
||||
assert_eq!(meshed.len(), shapes.len());
|
||||
for ((m, f), a) in meshed.iter().zip(&facets).zip(&apart) {
|
||||
let (m, f, a) = (m.as_ref().unwrap(), f.as_ref().unwrap(), a.as_ref().unwrap());
|
||||
assert_eq!(format!("{:?}", m.facets), format!("{f:?}"), "{o:?} {view:?}");
|
||||
assert!(!a.is_empty());
|
||||
assert_eq!(bits(&m.mesh().unwrap()), bits(a), "{o:?} {view:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One build gives both the facets and the mesh: each solid is built once.
|
||||
#[test]
|
||||
fn a_meshed_solid_is_built_once() {
|
||||
let (p, r) = star(5);
|
||||
let extruded = || crate::solid::EXTRUDED.with(|n| n.get());
|
||||
let start = extruded();
|
||||
let solid = extrude_meshed(&p, FillRule::NonZero, r, Extrude::default(), front()).unwrap();
|
||||
assert!(!solid.facets.is_empty() && solid.mesh().is_ok_and(|m| !m.is_empty()));
|
||||
assert_eq!(extruded(), start + 1);
|
||||
let inflated = || crate::solid::INFLATED.with(|n| n.get());
|
||||
let start = inflated();
|
||||
let mut b = BezPath::new();
|
||||
square(&mut b, 200.0, 20.0, 60.0, false);
|
||||
let (q, _) = shape(&b);
|
||||
for solid in inflate_meshed(&[(&p, FillRule::NonZero), (&q, FillRule::NonZero)], Rect::new(0.0, 0.0, 280.0, 200.0), Inflate::default(), front()) {
|
||||
assert!(solid.is_ok_and(|s| !s.facets.is_empty() && s.mesh().is_ok_and(|m| !m.is_empty())));
|
||||
}
|
||||
assert_eq!(inflated(), start + 2);
|
||||
}
|
||||
@@ -11,7 +11,7 @@ use std::ops::Range;
|
||||
|
||||
use vectorcraft_geom::{BezPath, FillRule, PathData, Point, Rect};
|
||||
|
||||
use crate::solid::{Face3, extrude_faces, inflate_faces, plane_faces};
|
||||
use crate::solid::{Face3, Keep, extrude_faces, inflate_faces, plane_faces};
|
||||
use crate::{Extrude, Inflate, MAX_SOLID_FACES, Revolve, V3, View, finite, perspective_scale};
|
||||
|
||||
/// Where the shadow falls.
|
||||
@@ -160,14 +160,14 @@ impl ShadowCaster {
|
||||
|
||||
/// Add Extrude & Bevel's solid ([`crate::extrude`]).
|
||||
pub fn extrude(&mut self, path: &PathData, rule: FillRule, bounds: Rect, options: Extrude, view: View) -> Result<Range<usize>, String> {
|
||||
let (_, faces, projection) = extrude_faces(path, rule, bounds, options, view, true)?;
|
||||
self.add(faces, projection)
|
||||
let (_, faces, projection) = extrude_faces(path, rule, bounds, options, view, Keep::Faces)?;
|
||||
self.add(faces?, projection)
|
||||
}
|
||||
|
||||
/// Add Rotate's plane ([`crate::plane`]).
|
||||
pub fn plane(&mut self, path: &PathData, rule: FillRule, bounds: Rect, view: View) -> Result<Range<usize>, String> {
|
||||
let (_, faces, projection) = plane_faces(path, rule, bounds, view, true)?;
|
||||
self.add(faces, projection)
|
||||
let (_, faces, projection) = plane_faces(path, rule, bounds, view, Keep::Faces)?;
|
||||
self.add(faces?, projection)
|
||||
}
|
||||
|
||||
/// Add Inflate's solids for several shapes of one object ([`crate::inflate_together`]), each
|
||||
@@ -182,11 +182,11 @@ impl ShadowCaster {
|
||||
options: Inflate,
|
||||
view: View,
|
||||
) -> Vec<Result<Range<usize>, String>> {
|
||||
inflate_faces(shapes, bounds, options, view, true)
|
||||
inflate_faces(shapes, bounds, options, view, Keep::Faces)
|
||||
.enumerate()
|
||||
.map(|(i, r)| {
|
||||
let (_, faces, projection) = r?;
|
||||
if casts.get(i).copied().unwrap_or(true) { self.add(faces, projection) } else { Ok(self.faces.len()..self.faces.len()) }
|
||||
if casts.get(i).copied().unwrap_or(true) { self.add(faces?, projection) } else { Ok(self.faces.len()..self.faces.len()) }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -20,9 +20,11 @@ mod membrane;
|
||||
mod outline;
|
||||
mod rings;
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use build::EXTRUDED;
|
||||
#[cfg(test)]
|
||||
use build::{Camera, cuts, edge_distance, newell, section};
|
||||
pub(crate) use build::{Face3, extrude_faces, plane_faces};
|
||||
pub(crate) use build::{Face3, Keep, extrude_faces, plane_faces};
|
||||
pub use build::{extrude, plane};
|
||||
pub use face_map::{FaceMap, extrude_face_map, plane_face_map};
|
||||
#[cfg(test)]
|
||||
@@ -293,8 +295,8 @@ pub struct Facet {
|
||||
/// (its front). `None` for every other facet.
|
||||
pub end: Option<PlanarFace>,
|
||||
/// The face's plane in view space and the projection it was drawn with, for its depth at a
|
||||
/// point of the drawing ([`Facet::depth_at`]). `None` for Revolve's faces and for a face whose
|
||||
/// corners lie more than 0.001 pt off one plane (a piece of a twisted wall).
|
||||
/// point of the drawing ([`Facet::depth_at`]). `None` for a face whose corners lie more than
|
||||
/// 0.001 pt off one plane (a piece of a twisted wall) or reach the eye.
|
||||
pub plane: Option<DepthPlane>,
|
||||
/// The contours in view space, before projection, for tests that measure depths.
|
||||
#[cfg(test)]
|
||||
@@ -365,7 +367,7 @@ impl Facet {
|
||||
PathData::from_bezpath(&bp)
|
||||
}
|
||||
|
||||
/// Revolve's face `face` as a facet: its polygon, curved outline, depth, normal and lighting.
|
||||
/// Revolve's face `face` as a facet: its polygon, curved outline, depth, plane, normal and lighting.
|
||||
/// A face that joins the profile's ends round the axis is a side; a cap is an end.
|
||||
pub(crate) fn of_face(face: crate::Face) -> Self {
|
||||
Facet {
|
||||
@@ -380,7 +382,7 @@ impl Facet {
|
||||
// A revolved cap is not one of the planar faces graphics map onto.
|
||||
end: None,
|
||||
away: false,
|
||||
plane: None,
|
||||
plane: face.plane,
|
||||
#[cfg(test)]
|
||||
view: vec![],
|
||||
}
|
||||
|
||||
@@ -64,9 +64,25 @@ impl Camera {
|
||||
/// and its outward normal.
|
||||
pub(crate) type Face3 = (Vec<Vec<V3>>, V3);
|
||||
|
||||
/// What a solid's builder makes: its facets, or with a mesh asked for, its faces before projection,
|
||||
/// and the projection it used (`None` when nothing was built).
|
||||
pub(crate) type Built = (Vec<Facet>, Vec<Face3>, Option<Projection>);
|
||||
/// What a solid's builder makes: its facets, its faces before projection when a mesh is asked for
|
||||
/// (an error when they pass [`MAX_SOLID_FACES`] while the facets are built too), and the
|
||||
/// projection it used (`None` when nothing was built).
|
||||
pub(crate) type Built = (Vec<Facet>, Result<Vec<Face3>, String>, Option<Projection>);
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many Extrude & Bevel solids this thread has built, for the tests.
|
||||
pub(crate) static EXTRUDED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// What a solid's builder keeps: its facets, its faces before projection for a mesh, or both
|
||||
/// from one build.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum Keep {
|
||||
Facets,
|
||||
Faces,
|
||||
Both,
|
||||
}
|
||||
|
||||
/// Facets being collected for one solid.
|
||||
pub(super) struct Builder<'a> {
|
||||
@@ -80,6 +96,11 @@ pub(super) struct Builder<'a> {
|
||||
pub(super) facets: Vec<Facet>,
|
||||
/// With `Some`, every face goes here as it is, none hidden or projected, for a mesh.
|
||||
pub(super) mesh: Option<Vec<Face3>>,
|
||||
/// The faces are projected into `facets`; without it only `mesh` is kept.
|
||||
pub(super) draw: bool,
|
||||
/// The faces kept for the mesh passed [`MAX_SOLID_FACES`] while the facets were built too: the
|
||||
/// mesh is too complex, and `mesh` keeps no more faces.
|
||||
pub(super) lost: bool,
|
||||
}
|
||||
|
||||
/// Where a face goes in the order the facets are drawn, back to front.
|
||||
@@ -98,20 +119,46 @@ pub(super) enum Order {
|
||||
Last,
|
||||
}
|
||||
|
||||
impl Builder<'_> {
|
||||
impl<'a> Builder<'a> {
|
||||
/// A builder for one solid seen by `camera`, keeping what `keep` asks for.
|
||||
pub(super) fn new(camera: &'a Camera, closed: bool, hidden: bool, keep: Keep) -> Self {
|
||||
Self { camera, closed, hidden, facets: vec![], mesh: (keep != Keep::Facets).then(Vec::new), draw: keep != Keep::Faces, lost: false }
|
||||
}
|
||||
|
||||
/// Add a face of view-space contours with outward normal `n` (view space), drawn in `order`.
|
||||
pub(super) fn face(&mut self, contours: Vec<Vec<V3>>, n: V3, order: Order) -> Result<(), String> {
|
||||
let count: usize = contours.iter().map(Vec::len).sum();
|
||||
if count < 3 {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(mesh) = &mut self.mesh {
|
||||
mesh.push((contours, n.unit()));
|
||||
if mesh.len() > MAX_SOLID_FACES {
|
||||
if self.draw {
|
||||
self.facet(&contours, count, n, order)?;
|
||||
}
|
||||
let Some(mesh) = &mut self.mesh else { return Ok(()) };
|
||||
mesh.push((contours, n.unit()));
|
||||
if mesh.len() > MAX_SOLID_FACES {
|
||||
if !self.draw {
|
||||
return Err(TOO_COMPLEX_SURFACE.into());
|
||||
}
|
||||
return Ok(());
|
||||
self.lose_mesh();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Keep no more faces for the mesh, which is too complex.
|
||||
pub(super) fn lose_mesh(&mut self) {
|
||||
self.mesh = None;
|
||||
self.lost = true;
|
||||
}
|
||||
|
||||
/// How many faces the mesh holds, while the facets are built too and the mesh is kept.
|
||||
pub(super) fn faces_kept(&self) -> Option<usize> {
|
||||
self.mesh.as_ref().filter(|_| self.draw).map(Vec::len)
|
||||
}
|
||||
|
||||
/// Project a face of `count` view-space corners with outward normal `n` into a facet, drawn in
|
||||
/// `order`, unless it is hidden or has no area.
|
||||
fn facet(&mut self, contours: &[Vec<V3>], count: usize, n: V3, order: Order) -> Result<(), String> {
|
||||
let centre = contours.iter().flatten().fold(V3::default(), |s, v| V3(s.0 + v.0, s.1 + v.1, s.2 + v.2));
|
||||
let centre = V3(centre.0 / count as f64, centre.1 / count as f64, centre.2 / count as f64);
|
||||
let n = n.unit();
|
||||
@@ -138,7 +185,7 @@ impl Builder<'_> {
|
||||
let flat = !held && contours.iter().flatten().all(|v| (n.dot(*v) - offset).abs() <= 1e-3);
|
||||
let plane = flat.then_some(DepthPlane { normal: [n.0, n.1, n.2], offset, center: self.camera.origin, eye: self.camera.eye });
|
||||
#[cfg(test)]
|
||||
let view = contours.clone();
|
||||
let view = contours.to_vec();
|
||||
let contours: Vec<Vec<Point>> = contours.iter().map(|c| c.iter().map(|v| self.camera.project(*v)).collect()).collect();
|
||||
if contours.iter().flatten().any(|p| !p.x.is_finite() || !p.y.is_finite()) {
|
||||
return Err("The 3D projection is not finite".into());
|
||||
@@ -262,7 +309,7 @@ impl Builder<'_> {
|
||||
/// Give the facet that [`Builder::face`] added, when it added one after `before` facets, the
|
||||
/// outline `outline` makes from its projected contours.
|
||||
pub(super) fn outline(&mut self, before: usize, outline: impl FnOnce(&[Vec<Point>]) -> Option<PathData>) {
|
||||
if self.mesh.is_some() || self.facets.len() <= before {
|
||||
if !self.draw || self.facets.len() <= before {
|
||||
return;
|
||||
}
|
||||
if let Some(f) = self.facets.last_mut() {
|
||||
@@ -273,7 +320,7 @@ impl Builder<'_> {
|
||||
/// Mark the facet that [`Builder::face`] added, when it added one after `before` facets, as the
|
||||
/// planar end `end` of the solid.
|
||||
pub(super) fn end(&mut self, before: usize, end: PlanarFace) {
|
||||
if self.mesh.is_some() || self.facets.len() <= before {
|
||||
if !self.draw || self.facets.len() <= before {
|
||||
return;
|
||||
}
|
||||
if let Some(f) = self.facets.last_mut() {
|
||||
@@ -281,15 +328,19 @@ impl Builder<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// How many faces the builder holds: facets, or faces kept for a mesh.
|
||||
/// How many faces the builder holds: facets, or without them the faces kept for a mesh.
|
||||
pub(super) fn len(&self) -> usize {
|
||||
self.mesh.as_ref().map_or(self.facets.len(), Vec::len)
|
||||
match &self.mesh {
|
||||
Some(mesh) if !self.draw => mesh.len(),
|
||||
_ => self.facets.len(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The facets back to front, and the faces kept for a mesh (none without [`Builder::mesh`]).
|
||||
pub(super) fn finish(mut self) -> Built {
|
||||
self.facets.sort_by(|a, b| a.depth.total_cmp(&b.depth));
|
||||
(self.facets, self.mesh.unwrap_or_default(), Some(self.camera.projection()))
|
||||
let faces = if self.lost { Err(TOO_COMPLEX_SURFACE.into()) } else { Ok(self.mesh.unwrap_or_default()) };
|
||||
(self.facets, faces, Some(self.camera.projection()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,16 +587,18 @@ fn steps(o: &Extrude) -> Vec<Step> {
|
||||
/// Extrude & Bevel: the shape's outline extruded backwards, with optional twist, taper, bevels
|
||||
/// and caps.
|
||||
pub fn extrude(path: &PathData, rule: FillRule, bounds: Rect, options: Extrude, view: View) -> Result<Vec<Facet>, String> {
|
||||
extrude_faces(path, rule, bounds, options, view, false).map(|(facets, ..)| facets)
|
||||
extrude_faces(path, rule, bounds, options, view, Keep::Facets).map(|(facets, ..)| facets)
|
||||
}
|
||||
|
||||
/// [`extrude`], or with `mesh` the solid's faces before projection instead of its facets.
|
||||
pub(crate) fn extrude_faces(path: &PathData, rule: FillRule, bounds: Rect, options: Extrude, view: View, mesh: bool) -> Result<Built, String> {
|
||||
/// [`extrude`], keeping the solid's facets, its faces before projection, or both, as `keep` asks.
|
||||
pub(crate) fn extrude_faces(path: &PathData, rule: FillRule, bounds: Rect, options: Extrude, view: View, keep: Keep) -> Result<Built, String> {
|
||||
#[cfg(test)]
|
||||
EXTRUDED.with(|n| n.set(n.get() + 1));
|
||||
check_bounds(bounds)?;
|
||||
let o = options.sanitized();
|
||||
// No depth: the flat shape, seen from either side.
|
||||
if o.depth <= 0.0 {
|
||||
return plane_faces(path, rule, bounds, view, mesh);
|
||||
return plane_faces(path, rule, bounds, view, keep);
|
||||
}
|
||||
let steps = steps(&o);
|
||||
// One face for each edge of the outline in each band between two steps, and the two caps:
|
||||
@@ -561,7 +614,7 @@ pub(crate) fn extrude_faces(path: &PathData, rule: FillRule, bounds: Rect, optio
|
||||
return Err(TOO_COMPLEX_SURFACE.into());
|
||||
}
|
||||
let camera = Camera::new(view, bounds, o.depth);
|
||||
let mut b = Builder { camera: &camera, closed: o.cap, hidden: o.hidden, facets: vec![], mesh: mesh.then(Vec::new) };
|
||||
let mut b = Builder::new(&camera, o.cap, o.hidden, keep);
|
||||
let z = |t: f64| o.depth / 2.0 - o.depth * t;
|
||||
let ring_at =
|
||||
|ring: &[Point], s: &Step| -> Vec<V3> { section(&inset(ring, s.inset), bounds, &o, s.t).iter().map(|p| camera.place(*p, z(s.t))).collect() };
|
||||
@@ -647,15 +700,15 @@ pub(crate) fn extrude_faces(path: &PathData, rule: FillRule, bounds: Rect, optio
|
||||
/// Rotate: the flat art turned in 3D about the centre of `bounds` (which may be larger than the
|
||||
/// shape's own, so that every part of one object turns about the same point).
|
||||
pub fn plane(path: &PathData, rule: FillRule, bounds: Rect, view: View) -> Result<Vec<Facet>, String> {
|
||||
plane_faces(path, rule, bounds, view, false).map(|(facets, ..)| facets)
|
||||
plane_faces(path, rule, bounds, view, Keep::Facets).map(|(facets, ..)| facets)
|
||||
}
|
||||
|
||||
/// [`plane`], or with `mesh` its face before projection instead of its facet.
|
||||
pub(crate) fn plane_faces(path: &PathData, rule: FillRule, bounds: Rect, view: View, mesh: bool) -> Result<Built, String> {
|
||||
/// [`plane`], keeping its facet, its face before projection, or both, as `keep` asks.
|
||||
pub(crate) fn plane_faces(path: &PathData, rule: FillRule, bounds: Rect, view: View, keep: Keep) -> Result<Built, String> {
|
||||
check_bounds(bounds)?;
|
||||
let rings = solid_rings_traced(path, rule, bounds.width().max(bounds.height()), MAX_PLANE_POINTS, CURVE_PIECES)?;
|
||||
let camera = Camera::new(view, bounds, 0.0);
|
||||
let mut b = Builder { camera: &camera, closed: false, hidden: false, facets: vec![], mesh: mesh.then(Vec::new) };
|
||||
let mut b = Builder::new(&camera, false, false, keep);
|
||||
let contours: Vec<Vec<V3>> = rings.iter().map(|r| r.points.iter().map(|p| camera.place(*p, 0.0)).collect()).collect();
|
||||
// A plane is one flat face, seen from either side. Its normal is turned toward the viewer, so
|
||||
// it is drawn last, as a cap is, and planes drawn in one order keep the order they are given in.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::build::{Builder, Built, Camera, Order, Turn, check_bounds, cuts, fit, newell, outward};
|
||||
use super::build::{Builder, Built, Camera, Keep, Order, Turn, check_bounds, cuts, fit, newell, outward};
|
||||
use super::membrane::{Edges, MAX_MATERIAL_SEGMENTS, Membrane, cell_triangles, clip, crossings, filled, material};
|
||||
use super::rings::{MAX_SOLID_POINTS, TOO_COMPLEX_SURFACE, curve_pieces, signed_area, solid_rings};
|
||||
use super::{Facet, FillRule, Inflate, MAX_FACES, MAX_SOLID_FACES, PathData, Point, Rect, V3, Vec2, View};
|
||||
@@ -73,26 +73,26 @@ pub fn inflate_each(
|
||||
options: Inflate,
|
||||
view: View,
|
||||
) -> impl Iterator<Item = Result<Vec<Facet>, String>> + use<> {
|
||||
inflate_faces(shapes, bounds, options, view, false).map(|r| r.map(|(facets, ..)| facets))
|
||||
inflate_faces(shapes, bounds, options, view, Keep::Facets).map(|r| r.map(|(facets, ..)| facets))
|
||||
}
|
||||
|
||||
/// [`inflate_together`], or with `mesh` each solid's faces before projection instead of its facets.
|
||||
/// Every shape's membrane is solved at once (the eye depends on the highest puff); each solid is
|
||||
/// built as the iterator reaches it.
|
||||
/// [`inflate_together`], keeping each solid's facets, its faces before projection, or both, as
|
||||
/// `keep` asks. Every shape's membrane is solved at once (the eye depends on the highest puff);
|
||||
/// each solid is built as the iterator reaches it.
|
||||
pub(crate) fn inflate_faces(
|
||||
shapes: &[(&PathData, FillRule)],
|
||||
bounds: Rect,
|
||||
options: Inflate,
|
||||
view: View,
|
||||
mesh: bool,
|
||||
keep: Keep,
|
||||
) -> impl Iterator<Item = Result<Built, String>> + use<> {
|
||||
let o = options.sanitized();
|
||||
let puffs: Vec<Result<Option<Puff>, String>> =
|
||||
shapes.iter().map(|(path, rule)| check_bounds(bounds).and_then(|()| puff(path, *rule, bounds, &o))).collect();
|
||||
let reach = puffs.iter().flatten().flatten().map(|p| p.height).fold(0.0, f64::max);
|
||||
puffs.into_iter().map(move |p| match p {
|
||||
Ok(Some(p)) => inflated(p, bounds, o, view, reach, mesh),
|
||||
Ok(None) => Ok((vec![], vec![], None)),
|
||||
Ok(Some(p)) => inflated(p, bounds, o, view, reach, keep),
|
||||
Ok(None) => Ok((vec![], Ok(vec![]), None)),
|
||||
Err(e) => Err(e),
|
||||
})
|
||||
}
|
||||
@@ -106,9 +106,9 @@ thread_local! {
|
||||
}
|
||||
|
||||
/// The facets of one inflated shape, for a camera that allows for a puff `reach` high (or the
|
||||
/// shape's own, if higher): `reach` is the highest puff of the shapes drawn together. With `mesh`,
|
||||
/// its faces before projection instead.
|
||||
fn inflated(p: Puff, bounds: Rect, o: Inflate, view: View, reach: f64, mesh: bool) -> Result<Built, String> {
|
||||
/// shape's own, if higher): `reach` is the highest puff of the shapes drawn together. Its faces
|
||||
/// before projection instead, or both, as `keep` asks.
|
||||
fn inflated(p: Puff, bounds: Rect, o: Inflate, view: View, reach: f64, keep: Keep) -> Result<Built, String> {
|
||||
#[cfg(test)]
|
||||
INFLATED.with(|n| n.set(n.get() + 1));
|
||||
let Puff { rings, edges, cell, nx, ny, m, height } = p;
|
||||
@@ -149,7 +149,7 @@ fn inflated(p: Puff, bounds: Rect, o: Inflate, view: View, reach: f64, mesh: boo
|
||||
let smooth = view.shade && view.has_colored_lights();
|
||||
let camera = Camera::new(view, bounds, o.depth + 2.0 * height.max(reach));
|
||||
// Without its back cap the object is a shell open behind, its inside showing.
|
||||
let mut b = Builder { camera: &camera, closed: o.cap, hidden: false, facets: vec![], mesh: mesh.then(Vec::new) };
|
||||
let mut b = Builder::new(&camera, o.cap, false, keep);
|
||||
let half = o.depth / 2.0;
|
||||
// The back: turned and scaled by the twist and taper, its puff too.
|
||||
let back_turn = Turn::new(bounds, o.twist, o.taper, 1.0);
|
||||
@@ -375,15 +375,40 @@ fn inflated(p: Puff, bounds: Rect, o: Inflate, view: View, reach: f64, mesh: boo
|
||||
let turn = Turn::new(bounds, o.twist, o.taper, t);
|
||||
ring.iter().map(|p| camera.place(turn.apply(*p), half - o.depth * t)).collect()
|
||||
};
|
||||
let mut pieces: Vec<Vec<usize>> = rings.iter().map(|r| if o.twist != 0.0 { cuts(&at(r, 0), &at(r, 1)) } else { vec![] }).collect();
|
||||
let cut: Vec<Vec<usize>> = rings.iter().map(|r| if o.twist != 0.0 { cuts(&at(r, 0), &at(r, 1)) } else { vec![] }).collect();
|
||||
let mut pieces = cut.clone();
|
||||
fit(&mut pieces, slices, MAX_FACES.saturating_sub(b.len()));
|
||||
let edges = rings.iter().map(Vec::len).fold(0usize, usize::saturating_add);
|
||||
if b.len().saturating_add(edges.saturating_mul(slices)) > MAX_SOLID_FACES {
|
||||
return Err(TOO_COMPLEX_SURFACE.into());
|
||||
}
|
||||
for (ring, pieces) in rings.iter().zip(&pieces) {
|
||||
for k in 0..slices {
|
||||
b.band(&at(ring, k), &at(ring, k + 1), pieces, |i| outward(ring, i, 0.0))?;
|
||||
let sides = |b: &mut Builder<'_>, pieces: &[Vec<usize>]| -> Result<(), String> {
|
||||
for (ring, pieces) in rings.iter().zip(pieces) {
|
||||
for k in 0..slices {
|
||||
b.band(&at(ring, k), &at(ring, k + 1), pieces, |i| outward(ring, i, 0.0))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
// A mesh kept with the facets has the sides a mesh alone has: limited, and with a twist
|
||||
// cut, by the faces the mesh holds, which can be more than the facets.
|
||||
let mut own_sides = None;
|
||||
if let Some(kept) = b.faces_kept() {
|
||||
let mut own = cut;
|
||||
fit(&mut own, slices, MAX_FACES.saturating_sub(kept));
|
||||
if kept.saturating_add(edges.saturating_mul(slices)) > MAX_SOLID_FACES {
|
||||
b.lose_mesh();
|
||||
} else if own != pieces {
|
||||
own_sides = Some((own, b.mesh.take()));
|
||||
}
|
||||
}
|
||||
sides(&mut b, &pieces)?;
|
||||
if let Some((own, faces)) = own_sides {
|
||||
let mut m = Builder::new(&camera, o.cap, false, Keep::Faces);
|
||||
m.mesh = faces;
|
||||
match sides(&mut m, &own) {
|
||||
Ok(()) => b.mesh = m.mesh,
|
||||
Err(_) => b.lose_mesh(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,12 +225,12 @@ fn a_flat_facet_measures_its_depth_on_its_plane() {
|
||||
/// eye, has no plane: its drawing no longer matches the plane's projection there.
|
||||
#[test]
|
||||
fn a_face_reaching_past_the_eye_has_no_plane() {
|
||||
use super::build::{Builder, Order};
|
||||
use super::build::{Builder, Keep, Order};
|
||||
let (_, b) = shape(&square(0.0, 0.0, 100.0));
|
||||
let camera = Camera::new(View { perspective: 50.0, ..front() }, b, 0.0);
|
||||
let eye = camera.center_and_eye().1.unwrap();
|
||||
for (far, has_plane) in [(eye / 2.0, true), (2.0 * eye, false)] {
|
||||
let mut builder = Builder { camera: &camera, closed: false, hidden: false, facets: vec![], mesh: None };
|
||||
let mut builder = Builder::new(&camera, false, false, Keep::Facets);
|
||||
let corners = vec![V3(-10.0, -10.0, 0.0), V3(10.0, -10.0, 0.0), V3(0.0, 10.0, far)];
|
||||
builder.face(vec![corners], V3(0.0, -far, 20.0), Order::Depth).unwrap();
|
||||
assert_eq!(builder.facets.len(), 1, "{far}");
|
||||
|
||||
@@ -375,3 +375,35 @@ fn malformed_and_degenerate_inputs_are_bounded() {
|
||||
assert!(revolve(&p, Rect::new(f64::NAN, 0.0, 1.0, 1.0), Revolve::default()).is_err());
|
||||
assert!(revolve(&PathData::default(), b, Revolve::default()).unwrap().is_empty());
|
||||
}
|
||||
|
||||
/// A revolved face carries its plane: the depth it gives at each corner of its drawing is the
|
||||
/// corner's view-space depth, with and without perspective. A face whose corners lie off one plane,
|
||||
/// or with a corner at or past the eye, has none.
|
||||
#[test]
|
||||
fn a_revolved_face_measures_its_depth_on_its_plane() {
|
||||
let o = Revolve { rotation_x: -30.0, rotation_y: 25.0, ..Default::default() }.sanitized();
|
||||
let origin = Point::new(50.0, 80.0);
|
||||
let light = V3(0.0, 0.0, 1.0);
|
||||
let [a, b, c, d] = [V3(-20.0, -10.0, 5.0), V3(20.0, -12.0, -15.0), V3(25.0, 15.0, -20.0), V3(-18.0, 14.0, 4.0)];
|
||||
// The fourth corner moved onto the plane of the first three.
|
||||
let n = b.sub(a).cross(c.sub(a));
|
||||
let d = V3(d.0, d.1, a.2 - (n.0 * (d.0 - a.0) + n.1 * (d.1 - a.1)) / n.2);
|
||||
let flat = [a, b, c, d];
|
||||
for eye in [None, Some(400.0)] {
|
||||
let face = project_face(&flat, None, o, origin, eye, light).unwrap().unwrap();
|
||||
let plane = face.plane.unwrap();
|
||||
for (p, v) in face.points.iter().zip(&flat) {
|
||||
let depth = plane.depth_at(*p).unwrap();
|
||||
assert!((depth - v.2).abs() < 1e-6, "{eye:?}: {depth} against {}", v.2);
|
||||
}
|
||||
let twisted = [a, b, c, V3(d.0, d.1, d.2 + 1.0)];
|
||||
assert!(project_face(&twisted, None, o, origin, eye, light).unwrap().unwrap().plane.is_none(), "{eye:?}");
|
||||
}
|
||||
let past = [a, b, V3(c.0, c.1, 500.0)];
|
||||
assert!(project_face(&past, None, o, origin, Some(400.0), light).unwrap().unwrap().plane.is_none());
|
||||
assert!(project_face(&past, None, o, origin, Some(1000.0), light).unwrap().unwrap().plane.is_some());
|
||||
// Every face of a revolved profile of straight edges has one.
|
||||
let (p, b) = cylinder();
|
||||
let faces = revolve(&p, b, Revolve { rotation_x: -30.0, perspective: 35.0, ..Default::default() }).unwrap();
|
||||
assert!(!faces.is_empty() && faces.iter().all(|f| f.plane.is_some()));
|
||||
}
|
||||
@@ -303,6 +303,7 @@ mod tests {
|
||||
cap: false,
|
||||
visibility_pieces: None,
|
||||
surface: None,
|
||||
plane: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loaded 100 of 171 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user