mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 10:14:46 +01:00
Merge main into feat/brushes-bristle-library
# Conflicts: # docs/target-app-parity.md
This commit is contained in:
commit
d9a47bcb46
150 files changed
+10880
-614
No files matched your search
@@ -3,7 +3,7 @@ name = "vectorcraft-cad"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
description = "VectorCraft CAD interchange: a hand-written ASCII DXF writer (R12 to 2018)"
|
||||
description = "VectorCraft CAD interchange: a hand-written ASCII DXF writer (R12 to 2018) and an HP-GL/2 plot file reader"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
//! HP-GL import: a plotter file (HP-GL/2, `.plt`, `.hpgl`, `.hgl`, `.hpg`) as a document. Older
|
||||
//! HP-GL files are read by the HP-GL/2 rules for the instructions the two share.
|
||||
//!
|
||||
//! The pen's moves become paths: each run drawn with the pen down is one stroked path in the
|
||||
//! selected pen's color and width (0.35 mm unless PW sets another), with the line type as a dash
|
||||
//! pattern and the line attributes as caps and joins. Circles, arcs, wedges, rectangles and Bézier
|
||||
//! curves are drawn as curves (the chord angle isn't used). Filled rectangles, wedges and polygons
|
||||
//! (polygon mode, FP) become filled paths, edged ones stroked paths. Plotter units (1016 to the
|
||||
//! inch, y up) become points, through user units (SC, IP, IR) and the rotation (RO). The artboard
|
||||
//! is the bounds of what the art paints, its strokes included.
|
||||
//!
|
||||
//! VectorCraft's own rules where the device decides: before the first SP the pen is pen 1 (a
|
||||
//! device draws nothing until SP), and the hard-clip limits are 11 × 8.5 in from the origin, so
|
||||
//! P2 lies there when the file sets no scaling points. SP0 draws nothing. Labels, symbol mode's symbols and raster
|
||||
//! images (HP RTL) aren't drawn, the input window doesn't clip, hatched fills are filled solid,
|
||||
//! and the art of every page goes on one artboard; each comes back as a warning. Printer escape
|
||||
//! sequences and PJL lines around the instructions are skipped.
|
||||
//!
|
||||
//! - `lex`: the bytes as instructions, numbers, labels and PE data.
|
||||
//! - `plot`: the instructions played on the pen, as shapes.
|
||||
|
||||
mod lex;
|
||||
mod plot;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Dash, Document, FillLayer, LayerColor, Node, NodeId, NodeKind, StrokeLayer};
|
||||
use vectorcraft_geom::{Affine, PathData, Rect};
|
||||
|
||||
use crate::Imported;
|
||||
use lex::Lexer;
|
||||
use plot::Plot;
|
||||
|
||||
/// Points per plotter unit.
|
||||
const PT_PER_PLU: f64 = 72.0 / 1016.0;
|
||||
/// The weight of a zero-width pen (one device dot), in points.
|
||||
const THINNEST: f64 = 0.25;
|
||||
|
||||
/// Instructions the reader knows and draws nothing for (their parameters are read and dropped).
|
||||
const IGNORED: &[[u8; 2]] = &[
|
||||
*b"AC", *b"AD", *b"AF", *b"AH", *b"AP", *b"AS", *b"CA", *b"CF", *b"CM", *b"CP", *b"CS", *b"CT", *b"CV", *b"DC", *b"DI", *b"DL", *b"DP", *b"DR",
|
||||
*b"DS", *b"DV", *b"EC", *b"ES", *b"FI", *b"FN", *b"FS", *b"GM", *b"IM", *b"IV", *b"KY", *b"LM", *b"LO", *b"MC", *b"MG", *b"MT", *b"NR", *b"OA",
|
||||
*b"OC", *b"OD", *b"OE", *b"OF", *b"OG", *b"OH", *b"OI", *b"OL", *b"OP", *b"OS", *b"OT", *b"OW", *b"PB", *b"PP", *b"PS", *b"PT", *b"QL", *b"RF",
|
||||
*b"RP", *b"SA", *b"SB", *b"SD", *b"SG", *b"SI", *b"SL", *b"SR", *b"SS", *b"ST", *b"SV", *b"TD", *b"TL", *b"TR", *b"UC", *b"VA", *b"VN", *b"VS",
|
||||
*b"XT", *b"YT",
|
||||
];
|
||||
|
||||
/// Instructions the reader plays.
|
||||
const PLAYED: &[[u8; 2]] = &[
|
||||
*b"AA", *b"AR", *b"AT", *b"BL", *b"BP", *b"BR", *b"BZ", *b"CI", *b"CO", *b"CR", *b"DF", *b"DT", *b"EA", *b"EP", *b"ER", *b"EW", *b"FP", *b"FR",
|
||||
*b"FT", *b"IN", *b"IP", *b"IR", *b"IW", *b"LA", *b"LB", *b"LT", *b"NP", *b"PA", *b"PC", *b"PD", *b"PE", *b"PG", *b"PM", *b"PR", *b"PU", *b"PW",
|
||||
*b"RA", *b"RO", *b"RR", *b"RT", *b"SC", *b"SM", *b"SP", *b"UL", *b"WD", *b"WG", *b"WU",
|
||||
];
|
||||
|
||||
/// Instructions a plot file starts with.
|
||||
const FIRST: &[[u8; 2]] = &[
|
||||
*b"IN", *b"DF", *b"BP", *b"CO", *b"PS", *b"SP", *b"PU", *b"PD", *b"PA", *b"PR", *b"IP", *b"IR", *b"SC", *b"NP", *b"PC", *b"PW", *b"WU", *b"RO",
|
||||
*b"QL", *b"MC", *b"PP", *b"TR", *b"CR", *b"LT", *b"LA", *b"FT", *b"VS", *b"FS", *b"AC", *b"PG", *b"PE", *b"DT",
|
||||
];
|
||||
|
||||
/// Does `bytes` start like an HP-GL file? After any printer escapes and PJL lines (however long),
|
||||
/// it must hold nothing but instructions the reader knows, the first one of those a plot file
|
||||
/// starts with, up to the eighth instruction or 4 KB past the first. Their parameters must be
|
||||
/// numbers, but for a comment's quoted string and the text, characters and encoded data of LB, BL,
|
||||
/// WD, DT, SM and PE. An instruction the reader doesn't know counts when its numbers end with a
|
||||
/// semicolon.
|
||||
pub fn is_hpgl(bytes: &[u8]) -> bool {
|
||||
holds_instructions(bytes, true)
|
||||
}
|
||||
|
||||
/// Does `bytes` hold HP-GL instructions as [`is_hpgl`] checks? When `strict` is false, any of them
|
||||
/// may come first: a file picked by its extension may start with one a plot file seldom starts
|
||||
/// with (`SI`, `DI`). Either way, at least one of the instructions checked must be one the reader
|
||||
/// knows.
|
||||
fn holds_instructions(bytes: &[u8], strict: bool) -> bool {
|
||||
let mut lex = Lexer::new(bytes);
|
||||
let mut terminator = lex::ETX;
|
||||
let mut first = 0;
|
||||
let mut any_known = false;
|
||||
for n in 0..8 {
|
||||
if n > 0 && lex.pos() > first + 4096 {
|
||||
return any_known;
|
||||
}
|
||||
let Some(op) = lex.op(true) else { return n > 0 && any_known && lex.done() };
|
||||
if n == 0 {
|
||||
first = lex.pos();
|
||||
}
|
||||
if strict && n == 0 && !FIRST.contains(&op) {
|
||||
return false;
|
||||
}
|
||||
let known = PLAYED.contains(&op) || IGNORED.contains(&op);
|
||||
any_known |= known;
|
||||
match &op {
|
||||
b"CO" => {
|
||||
if !lex.comment() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
b"LB" | b"BL" | b"WD" => lex.label(terminator),
|
||||
b"DT" => {
|
||||
terminator = lex.char().unwrap_or(lex::ETX);
|
||||
lex.params();
|
||||
}
|
||||
b"SM" => {
|
||||
// The symbol character is read so it isn't taken for an instruction.
|
||||
let _ = lex.char();
|
||||
}
|
||||
b"PE" => lex.skip_pe(),
|
||||
_ if known => lex.params(),
|
||||
// An instruction the reader doesn't know (of an older or a device's own set) counts
|
||||
// when numbers and a semicolon follow it.
|
||||
_ => {
|
||||
if !lex.closed_params() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
any_known
|
||||
}
|
||||
|
||||
/// Read an HP-GL file. It may start with any instruction the reader knows, or one of another set
|
||||
/// that a semicolon ends; the rest of [`is_hpgl`]'s check applies, and at least one instruction the
|
||||
/// reader knows must be among those it checks.
|
||||
pub fn import(bytes: &[u8]) -> Result<Imported, String> {
|
||||
if !holds_instructions(bytes, false) {
|
||||
return Err("not an HP-GL file".into());
|
||||
}
|
||||
let mut lex = Lexer::new(bytes);
|
||||
let mut plot = Plot::new();
|
||||
while !plot.full
|
||||
&& let Some(op) = lex.op(false)
|
||||
{
|
||||
plot.op(op, &mut lex);
|
||||
}
|
||||
Ok(build(plot.finish(lex.raster)))
|
||||
}
|
||||
|
||||
fn rgb(c: [u8; 3]) -> Paint {
|
||||
Paint::solid(Color::rgb8(c[0], c[1], c[2]))
|
||||
}
|
||||
|
||||
/// The plot's shapes as paths on one layer, the artboard round what they paint.
|
||||
fn build(plot: Plot) -> Imported {
|
||||
// Plotter units, y up, to points, y down.
|
||||
let flip = Affine::scale_non_uniform(PT_PER_PLU, -PT_PER_PLU);
|
||||
let mut nodes: Vec<Node> = plot.shapes.iter().map(|s| node(s, flip)).collect();
|
||||
// The strokes' caps and miter joins included.
|
||||
let finite = |r: &Rect| [r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite());
|
||||
let frame = nodes.iter().filter_map(Node::visual_bounds).filter(finite).reduce(|a, b| a.union(b)).unwrap_or(Rect::new(0.0, 0.0, 612.0, 792.0));
|
||||
let mut doc = Document::new(frame.width().max(1.0), frame.height().max(1.0));
|
||||
doc.layers.clear();
|
||||
let shift = Affine::translate(-frame.origin().to_vec2());
|
||||
for n in &mut nodes {
|
||||
n.id = doc.alloc_id();
|
||||
if let NodeKind::Path { path, .. } = &mut n.kind {
|
||||
path.transform(shift);
|
||||
}
|
||||
}
|
||||
let mut layer = Node::layer(doc.alloc_id(), "Layer 1", LayerColor::Preset(0));
|
||||
if let Some(c) = layer.children_mut() {
|
||||
*c = nodes.into_iter().map(Arc::new).collect();
|
||||
}
|
||||
doc.layers = vec![Arc::new(layer)];
|
||||
Imported { document: doc, warnings: plot.warnings }
|
||||
}
|
||||
|
||||
/// A shape as a path, its plotter units through `to_points`.
|
||||
fn node(s: &plot::Shape, to_points: Affine) -> Node {
|
||||
let mut path = s.path.clone();
|
||||
path.apply_affine(to_points);
|
||||
let mut items = vec![];
|
||||
if let Some((c, _)) = s.fill {
|
||||
items.push(AppearanceItem::Fill(FillLayer::new(rgb(c))));
|
||||
}
|
||||
if let Some(st) = &s.stroke {
|
||||
let mut layer = StrokeLayer::new(rgb(st.color), (st.width * PT_PER_PLU).max(THINNEST));
|
||||
(layer.cap, layer.join, layer.miter_limit) = (st.cap, st.join, st.miter);
|
||||
layer.dash = st.dash.as_ref().map(|d| Dash {
|
||||
pattern: d.pattern.iter().map(|v| v * PT_PER_PLU).collect(),
|
||||
offset: d.offset * PT_PER_PLU,
|
||||
align_corners: d.adaptive,
|
||||
});
|
||||
items.push(AppearanceItem::Stroke(layer));
|
||||
}
|
||||
let mut n = Node::path(NodeId(0), PathData::from_bezpath(&path), Appearance { items, ..Appearance::default() });
|
||||
if let (NodeKind::Path { rule, .. }, Some((_, r))) = (&mut n.kind, s.fill) {
|
||||
*rule = r;
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,428 @@
|
||||
//! The bytes of an HP-GL file as instructions: two-letter mnemonics and the parameters that follow
|
||||
//! them, read one at a time as the plot asks for them. Printer escape sequences (PCL, HP RTL, the
|
||||
//! HP-GL device-control escapes) and PJL lines around and between the instructions are skipped,
|
||||
//! with the data their escapes carry.
|
||||
|
||||
/// The end-of-text character, the default label terminator.
|
||||
pub(super) const ETX: u8 = 3;
|
||||
const ESC: u8 = 0x1b;
|
||||
|
||||
/// The most digits of a number that count: longer ones are read as their first digits.
|
||||
const MAX_DIGITS: usize = 18;
|
||||
|
||||
/// What the bytes being read hold.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Mode {
|
||||
/// HP-GL instructions.
|
||||
Hpgl,
|
||||
/// PCL or HP RTL: escape sequences and their data, nothing to draw.
|
||||
Pcl,
|
||||
/// After a Universal Exit Language: PJL lines until a language starts.
|
||||
Pjl,
|
||||
}
|
||||
|
||||
/// One item of a PE (Polyline Encoded) instruction.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(super) enum PeItem {
|
||||
/// Select pen.
|
||||
Pen(i64),
|
||||
/// Rectangle mode: the following coordinates come in fours.
|
||||
Rect,
|
||||
/// A coordinate pair, after a pen-up flag (`up`) and an absolute flag (`absolute`).
|
||||
Point { x: f64, y: f64, up: bool, absolute: bool },
|
||||
}
|
||||
|
||||
/// The state of one PE instruction being read.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub(super) struct Pe {
|
||||
seven_bit: bool,
|
||||
fraction_bits: u32,
|
||||
up: bool,
|
||||
absolute: bool,
|
||||
}
|
||||
|
||||
pub(super) struct Lexer<'a> {
|
||||
b: &'a [u8],
|
||||
at: usize,
|
||||
mode: Mode,
|
||||
/// The file holds HP RTL or PCL raster image data.
|
||||
pub raster: bool,
|
||||
}
|
||||
|
||||
/// Is `c` a byte that separates numeric parameters (or white space between instructions)?
|
||||
fn separator(c: u8) -> bool {
|
||||
matches!(c, b' ' | b',' | b'\t' | b'\r' | b'\n' | 0)
|
||||
}
|
||||
|
||||
impl<'a> Lexer<'a> {
|
||||
pub fn new(b: &'a [u8]) -> Self {
|
||||
Self { b, at: 0, mode: Mode::Hpgl, raster: false }
|
||||
}
|
||||
|
||||
fn peek(&self) -> Option<u8> {
|
||||
self.b.get(self.at).copied()
|
||||
}
|
||||
|
||||
/// Whether everything has been read.
|
||||
pub fn done(&self) -> bool {
|
||||
self.at >= self.b.len()
|
||||
}
|
||||
|
||||
/// How many bytes have been read.
|
||||
pub fn pos(&self) -> usize {
|
||||
self.at
|
||||
}
|
||||
|
||||
/// The next instruction's mnemonic, in upper case; `strict`: only white space, semicolons and
|
||||
/// escape sequences may come before it (anything else gives `None`, as the end does).
|
||||
pub fn op(&mut self, strict: bool) -> Option<[u8; 2]> {
|
||||
loop {
|
||||
let c = self.peek()?;
|
||||
if c == ESC {
|
||||
self.escape();
|
||||
continue;
|
||||
}
|
||||
match self.mode {
|
||||
Mode::Pcl => {
|
||||
self.at += 1;
|
||||
continue;
|
||||
}
|
||||
Mode::Pjl => {
|
||||
if separator(c) {
|
||||
self.at += 1;
|
||||
} else if c == b'@' {
|
||||
self.pjl_line();
|
||||
} else {
|
||||
self.mode = Mode::Hpgl;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Mode::Hpgl => {}
|
||||
}
|
||||
let next = self.b.get(self.at + 1).copied();
|
||||
if c.is_ascii_alphabetic()
|
||||
&& let Some(n) = next.filter(u8::is_ascii_alphabetic)
|
||||
{
|
||||
self.at += 2;
|
||||
return Some([c.to_ascii_uppercase(), n.to_ascii_uppercase()]);
|
||||
}
|
||||
if strict && !(separator(c) || c == b';' || c < 0x20) {
|
||||
return None;
|
||||
}
|
||||
self.at += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// One `@PJL` line: an `ENTER LANGUAGE` command picks what follows.
|
||||
fn pjl_line(&mut self) {
|
||||
let rest = &self.b[self.at..];
|
||||
let end = rest.iter().position(|&c| c == b'\n').map_or(rest.len(), |n| n + 1);
|
||||
// A command names its language within its first 256 bytes; longer lines aren't copied.
|
||||
let line = String::from_utf8_lossy(&rest[..end.min(256)]).to_ascii_uppercase();
|
||||
if let Some(lang) = line.split_once("LANGUAGE").map(|(_, l)| l.trim_start_matches([' ', '=']).trim()) {
|
||||
self.mode = if lang.starts_with("HPGL") { Mode::Hpgl } else { Mode::Pcl };
|
||||
}
|
||||
self.at += end;
|
||||
}
|
||||
|
||||
/// An escape sequence at the current byte, with the data it carries.
|
||||
fn escape(&mut self) {
|
||||
self.at += 1;
|
||||
let Some(c) = self.peek() else { return };
|
||||
self.at += 1;
|
||||
match c {
|
||||
// Universal Exit Language (`ESC%-12345X`) and the language switches (`ESC%#B` to HP-GL,
|
||||
// `ESC%#A` to PCL or HP RTL).
|
||||
b'%' => {
|
||||
let (_, end) = self.value();
|
||||
match end {
|
||||
Some(b'X') => self.mode = Mode::Pjl,
|
||||
Some(b'B') => self.mode = Mode::Hpgl,
|
||||
Some(b'A') => self.mode = Mode::Pcl,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// Reset: PCL follows, until a language switch.
|
||||
b'E' => self.mode = Mode::Pcl,
|
||||
// HP-GL device control: `ESC.` and a letter, with parameters up to a colon.
|
||||
b'.' => {
|
||||
let Some(kind) = self.peek() else { return };
|
||||
self.at += 1;
|
||||
if !b"()YZBEJKLORUX".contains(&kind) {
|
||||
let rest = &self.b[self.at..self.b.len().min(self.at + 80)];
|
||||
if let Some(n) = rest.iter().position(|&c| c == b':')
|
||||
&& rest[..n].iter().all(|c| c.is_ascii_digit() || matches!(c, b';' | b' ' | b','))
|
||||
{
|
||||
self.at += n + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Parameterized PCL: a group character, then values each ending in a letter (upper case
|
||||
// ends the sequence); `W`, `*b…V` (raster data by plane) and `&p…X` (transparent print
|
||||
// data) are followed by that many bytes of data. Without a group character (`ESC(8U`,
|
||||
// `ESC)0N`), one value and the letter ending it.
|
||||
0x21..=0x2f => {
|
||||
let Some(group) = self.peek().filter(|g| (0x60..=0x7e).contains(g)) else {
|
||||
self.value();
|
||||
return;
|
||||
};
|
||||
self.at += 1;
|
||||
loop {
|
||||
let (v, end) = self.value();
|
||||
let Some(end) = end else { return };
|
||||
let upper = end.to_ascii_uppercase();
|
||||
if upper == b'W' || (c, group, upper) == (b'*', b'b', b'V') || (c, group, upper) == (b'&', b'p', b'X') {
|
||||
let n = v.max(0.0).min((self.b.len() - self.at) as f64) as usize;
|
||||
if (c, group) == (b'*', b'b') && n > 0 {
|
||||
self.raster = true;
|
||||
}
|
||||
self.at += n;
|
||||
}
|
||||
if (c, group, end) == (b'*', b'r', b'A') {
|
||||
self.raster = true;
|
||||
}
|
||||
if !end.is_ascii_lowercase() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// A two-character escape.
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// A PCL value (`[+-]digits[.digits]`, any part missing) and the character that ends it; `None`
|
||||
/// when that is not a letter or `@` (which is then left unread).
|
||||
fn value(&mut self) -> (f64, Option<u8>) {
|
||||
let v = self.number_here().unwrap_or(0.0);
|
||||
match self.peek() {
|
||||
Some(c) if c.is_ascii_alphabetic() || c == b'@' => {
|
||||
self.at += 1;
|
||||
(v, Some(c))
|
||||
}
|
||||
_ => (v, None),
|
||||
}
|
||||
}
|
||||
|
||||
/// A number starting at the current byte (`[+-]digits[.digits]`), `None` when it has no digit
|
||||
/// (its sign and point are read all the same).
|
||||
fn number_here(&mut self) -> Option<f64> {
|
||||
let mut neg = false;
|
||||
if let Some(c @ (b'+' | b'-')) = self.peek() {
|
||||
neg = c == b'-';
|
||||
self.at += 1;
|
||||
}
|
||||
let (mut v, mut digits, mut scale) = (0.0f64, 0usize, 1.0f64);
|
||||
let mut point = false;
|
||||
while let Some(c) = self.peek() {
|
||||
if c.is_ascii_digit() {
|
||||
if digits < MAX_DIGITS {
|
||||
if point {
|
||||
scale /= 10.0;
|
||||
v += f64::from(c - b'0') * scale;
|
||||
} else {
|
||||
v = v * 10.0 + f64::from(c - b'0');
|
||||
}
|
||||
} else if !point {
|
||||
// Past the digits that count, an integer part keeps its size.
|
||||
v *= 10.0;
|
||||
}
|
||||
digits += 1;
|
||||
} else if c == b'.' && !point {
|
||||
point = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
self.at += 1;
|
||||
}
|
||||
let v = v.min(f64::MAX);
|
||||
(digits > 0).then_some(if neg { -v } else { v })
|
||||
}
|
||||
|
||||
/// The next numeric parameter of the instruction being read, or `None` at its end (a semicolon
|
||||
/// ending it is read).
|
||||
pub fn number(&mut self) -> Option<f64> {
|
||||
loop {
|
||||
let c = self.peek()?;
|
||||
if separator(c) {
|
||||
self.at += 1;
|
||||
} else if c.is_ascii_digit() || matches!(c, b'+' | b'-' | b'.') {
|
||||
if let Some(v) = self.number_here() {
|
||||
return Some(v);
|
||||
}
|
||||
} else {
|
||||
if c == b';' {
|
||||
self.at += 1;
|
||||
}
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Up to `out.len()` numeric parameters into `out` (the rest are read and dropped) → how many.
|
||||
pub fn numbers(&mut self, out: &mut [f64]) -> usize {
|
||||
let mut n = 0;
|
||||
while let Some(v) = self.number() {
|
||||
if let Some(slot) = out.get_mut(n) {
|
||||
*slot = v;
|
||||
}
|
||||
n += 1;
|
||||
}
|
||||
n.min(out.len())
|
||||
}
|
||||
|
||||
/// Read and drop the parameters of the instruction being read: numbers and quoted strings.
|
||||
pub fn params(&mut self) {
|
||||
loop {
|
||||
while self.number().is_some() {}
|
||||
if self.peek() != Some(b'"') {
|
||||
return;
|
||||
}
|
||||
self.quoted();
|
||||
}
|
||||
}
|
||||
|
||||
/// Read and drop numeric parameters → whether a semicolon ended them (an instruction the reader
|
||||
/// doesn't know, written out in full).
|
||||
pub fn closed_params(&mut self) -> bool {
|
||||
while self.number().is_some() {}
|
||||
self.at > 0 && self.b.get(self.at - 1) == Some(&b';')
|
||||
}
|
||||
|
||||
/// A comment (CO): its quoted string or nothing, then the terminator or the end → whether it
|
||||
/// is one.
|
||||
pub fn comment(&mut self) -> bool {
|
||||
while self.peek().is_some_and(separator) {
|
||||
self.at += 1;
|
||||
}
|
||||
match self.peek() {
|
||||
Some(b'"') => self.quoted(),
|
||||
Some(b';') => {
|
||||
self.at += 1;
|
||||
true
|
||||
}
|
||||
None => true,
|
||||
Some(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// The text of a label, up to and including `terminator` (or the end).
|
||||
pub fn label(&mut self, terminator: u8) {
|
||||
let rest = &self.b[self.at..];
|
||||
self.at += rest.iter().position(|&c| c == terminator).map_or(rest.len(), |n| n + 1);
|
||||
}
|
||||
|
||||
/// A quoted string (CO): the parameters up to its closing quote, then the terminator → whether
|
||||
/// there was one.
|
||||
pub fn quoted(&mut self) -> bool {
|
||||
while self.peek().is_some_and(separator) {
|
||||
self.at += 1;
|
||||
}
|
||||
let found = self.peek() == Some(b'"');
|
||||
if found {
|
||||
self.at += 1;
|
||||
let rest = &self.b[self.at..];
|
||||
self.at += rest.iter().position(|&c| c == b'"').map_or(rest.len(), |n| n + 1);
|
||||
}
|
||||
if self.peek() == Some(b';') {
|
||||
self.at += 1;
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
/// The character right after the mnemonic (DT, SM), `None` when it is a semicolon or the end
|
||||
/// (the semicolon is read).
|
||||
pub fn char(&mut self) -> Option<u8> {
|
||||
let c = self.peek()?;
|
||||
self.at += 1;
|
||||
(c != b';').then_some(c)
|
||||
}
|
||||
|
||||
/// The next item of a PE instruction, `None` at its end (its semicolon is read).
|
||||
pub fn pe(&mut self, st: &mut Pe) -> Option<PeItem> {
|
||||
loop {
|
||||
let raw = self.peek()?;
|
||||
let c = raw & 0x7f;
|
||||
// Spaces, deletes, control characters and bytes 128 to 160 and 255 are ignored.
|
||||
if raw <= 32 || raw == 127 || (128..=160).contains(&raw) || raw == 255 {
|
||||
self.at += 1;
|
||||
continue;
|
||||
}
|
||||
match c {
|
||||
b';' => {
|
||||
self.at += 1;
|
||||
return None;
|
||||
}
|
||||
// Flags keep their meaning with the eighth bit set; digits are 63 and up (191 and up).
|
||||
b':' | b'<' | b'>' | b'=' | b'7' | b'9' if raw < 63 || (0xb7..191).contains(&raw) => {
|
||||
self.at += 1;
|
||||
match c {
|
||||
b':' => return self.pe_value(st).map(PeItem::Pen).or_else(|| self.end_pe()),
|
||||
b'<' => st.up = true,
|
||||
b'>' => match self.pe_value(st) {
|
||||
Some(n) => st.fraction_bits = n.clamp(0, 52) as u32,
|
||||
None => return self.end_pe(),
|
||||
},
|
||||
b'=' => st.absolute = true,
|
||||
b'7' => st.seven_bit = true,
|
||||
_ => return Some(PeItem::Rect),
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let scale = 0.5f64.powi(st.fraction_bits as i32);
|
||||
let (Some(x), Some(y)) = (self.pe_value(st), self.pe_value(st)) else { return self.end_pe() };
|
||||
let item = PeItem::Point { x: x as f64 * scale, y: y as f64 * scale, up: st.up, absolute: st.absolute };
|
||||
(st.up, st.absolute) = (false, false);
|
||||
return Some(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The rest of a PE instruction that can't be decoded: see [`Lexer::skip_pe`].
|
||||
fn end_pe(&mut self) -> Option<PeItem> {
|
||||
self.skip_pe();
|
||||
None
|
||||
}
|
||||
|
||||
/// The data of a PE instruction, up to and including its semicolon, unread: its data is never
|
||||
/// read as instructions.
|
||||
pub fn skip_pe(&mut self) {
|
||||
let rest = &self.b[self.at..];
|
||||
self.at += rest.iter().position(|&c| c == b';').map_or(rest.len(), |n| n + 1);
|
||||
}
|
||||
|
||||
/// One encoded PE number, `None` when the instruction ends inside it (or a byte that is no
|
||||
/// digit comes first, which is read).
|
||||
fn pe_value(&mut self, st: &Pe) -> Option<i64> {
|
||||
let (base, low, high): (u64, std::ops::RangeInclusive<u8>, std::ops::RangeInclusive<u8>) =
|
||||
if st.seven_bit { (32, 63..=94, 95..=126) } else { (64, 63..=126, 191..=254) };
|
||||
let (mut v, mut place) = (0u64, 1u64);
|
||||
loop {
|
||||
let raw = self.peek()?;
|
||||
let c = if st.seven_bit { raw & 0x7f } else { raw };
|
||||
if raw <= 32 || raw == 127 || (128..=160).contains(&raw) || raw == 255 {
|
||||
self.at += 1;
|
||||
continue;
|
||||
}
|
||||
if c == b';' {
|
||||
return None;
|
||||
}
|
||||
self.at += 1;
|
||||
let (digit, last) = if low.contains(&c) {
|
||||
(u64::from(c - 63), false)
|
||||
} else if high.contains(&c) {
|
||||
(u64::from(c - high.start()), true)
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
v = v.saturating_add(digit.saturating_mul(place));
|
||||
place = place.saturating_mul(base);
|
||||
if last {
|
||||
// The low bit is the sign.
|
||||
let n = i64::try_from(v >> 1).unwrap_or(i64::MAX);
|
||||
return Some(if v & 1 == 1 { -n } else { n });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,630 @@
|
||||
//! HP-GL import: units and the y axis, pens, runs, curves, filled and edged areas, polygon mode,
|
||||
//! line types, scaling, rotation, encoded polylines, escapes around the instructions, what is
|
||||
//! skipped with a warning, which files are taken to be HP-GL, and hostile files.
|
||||
|
||||
use kurbo::{BezPath, PathEl, Point, Shape as _};
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
use vectorcraft_doc::{AppearanceItem, Document, LineCap, LineJoin, Node, NodeKind, StrokeLayer};
|
||||
use vectorcraft_geom::FillRule;
|
||||
|
||||
use super::plot::{FAR_AWAY, HATCHES, LABELS, PAGES, RASTER, SYMBOLS, TOO_MANY_POINTS, WINDOW};
|
||||
use super::*;
|
||||
|
||||
/// Points per millimeter.
|
||||
const MM: f64 = 72.0 / 25.4;
|
||||
|
||||
fn plot(s: &str) -> Imported {
|
||||
plot_bytes(s.as_bytes())
|
||||
}
|
||||
|
||||
fn plot_bytes(b: &[u8]) -> Imported {
|
||||
match import(b) {
|
||||
Ok(i) => i,
|
||||
Err(e) => panic!("{e}: {:?}", String::from_utf8_lossy(b)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Every object, in paint order.
|
||||
fn leaves(doc: &Document) -> Vec<Node> {
|
||||
let mut out = vec![];
|
||||
doc.walk(|n| {
|
||||
if n.children().is_none() {
|
||||
out.push(n.clone());
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
fn bez(n: &Node) -> BezPath {
|
||||
match &n.kind {
|
||||
NodeKind::Path { path, .. } => path.to_bezpath(),
|
||||
k => panic!("a path, got {k:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn stroke(n: &Node) -> StrokeLayer {
|
||||
n.appearance.items.iter().find_map(|i| if let AppearanceItem::Stroke(s) = i { Some(s.clone()) } else { None }).expect("a stroke")
|
||||
}
|
||||
|
||||
fn fill(n: &Node) -> Option<Paint> {
|
||||
n.appearance.items.iter().find_map(|i| if let AppearanceItem::Fill(f) = i { Some(f.paint.clone()) } else { None })
|
||||
}
|
||||
|
||||
fn rule(n: &Node) -> FillRule {
|
||||
match &n.kind {
|
||||
NodeKind::Path { rule, .. } => *rule,
|
||||
k => panic!("a path, got {k:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn rgb(r: u8, g: u8, b: u8) -> Paint {
|
||||
Paint::solid(Color::rgb8(r, g, b))
|
||||
}
|
||||
|
||||
/// The end points of `bp`'s elements.
|
||||
fn ends(bp: &BezPath) -> Vec<Point> {
|
||||
bp.elements().iter().filter_map(|e| e.end_point()).collect()
|
||||
}
|
||||
|
||||
fn near(a: Point, b: Point) -> bool {
|
||||
(a - b).hypot() < 1e-6
|
||||
}
|
||||
|
||||
fn assert_points(got: &[Point], want: &[(f64, f64)]) {
|
||||
assert_eq!(got.len(), want.len(), "{got:?}");
|
||||
for (g, w) in got.iter().zip(want) {
|
||||
assert!(near(*g, Point::new(w.0, w.1)), "{got:?} != {want:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A PE number as the reference guide's encoding writes it (base 64, or base 32 for 7-bit data).
|
||||
fn pe_number(v: i64, seven_bit: bool) -> Vec<u8> {
|
||||
let mut n = if v >= 0 { 2 * v as u64 } else { 2 * v.unsigned_abs() + 1 };
|
||||
let (base, high) = if seven_bit { (32, 95) } else { (64, 191) };
|
||||
let mut out = vec![];
|
||||
while n >= base {
|
||||
out.push(63 + (n % base) as u8);
|
||||
n /= base;
|
||||
}
|
||||
out.push(high + n as u8);
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plotter_units_become_points_with_y_down() {
|
||||
// An inch right and an inch up, with the thinnest pen (0.25 pt): the artboard reaches 0.125 pt
|
||||
// past the line at the corner's miter, and stops at the line's butt ends.
|
||||
let i = plot("IN;SP1;PW0;PU0,0;PD1016,0,1016,1016;PU;");
|
||||
let doc = &i.document;
|
||||
let r = doc.artboards[0].rect;
|
||||
assert!((r.width() - 72.125).abs() < 1e-9 && (r.height() - 72.125).abs() < 1e-9, "{r:?}");
|
||||
let shapes = leaves(doc);
|
||||
assert_eq!(shapes.len(), 1);
|
||||
assert_points(&ends(&bez(&shapes[0])), &[(0.0, 72.0), (72.0, 72.0), (72.0, 0.0)]);
|
||||
let s = stroke(&shapes[0]);
|
||||
assert_eq!((s.width, s.paint.clone(), s.cap, s.join, s.miter_limit), (0.25, rgb(0, 0, 0), LineCap::Butt, LineJoin::Miter, 5.0));
|
||||
assert!(fill(&shapes[0]).is_none() && i.warnings.is_empty(), "{:?}", i.warnings);
|
||||
|
||||
// The artboard holds what the strokes paint, the tip of a sharp miter join included.
|
||||
let i = plot("IN;SP1;PW2;PD2032,0,0,1000;");
|
||||
let r = i.document.artboards[0].rect;
|
||||
let painted = leaves(&i.document)[0].visual_bounds().unwrap();
|
||||
let path = bez(&leaves(&i.document)[0]).bounding_box();
|
||||
assert!(painted.x1 - path.x1 > 2.0 * MM, "the miter reaches past half the width: {painted:?} {path:?}");
|
||||
let inside = r.x0 <= painted.x0 + 1e-9 && r.y0 <= painted.y0 + 1e-9 && r.x1 >= painted.x1 - 1e-9 && r.y1 >= painted.y1 - 1e-9;
|
||||
assert!(inside, "{r:?} {painted:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pens_pick_colors_and_widths() {
|
||||
// Before any SP the pen is pen 1; SP0 draws nothing; pens past the palette wrap round it.
|
||||
let i = plot("IN;PD0,0,100,0;PU;SP2;PD0,100,100,100;PU;SP0;PD0,200,100,200;PU;SP9;PD0,300,100,300;PU;SP3;PD0,400,100,400;PU;");
|
||||
let colors: Vec<Paint> = leaves(&i.document).iter().map(|n| stroke(n).paint).collect();
|
||||
assert_eq!(colors, [rgb(0, 0, 0), rgb(255, 0, 0), rgb(255, 0, 0), rgb(0, 255, 0)]);
|
||||
|
||||
// PC gives a pen a color in the color range; PC with the pen alone gives back its default.
|
||||
let i = plot("IN;CR0,1,0,1,0,1;PC3,0,0,1;SP3;PD0,0,10,0;PU;PC3;PD0,10,10,10;PU;");
|
||||
let colors: Vec<Paint> = leaves(&i.document).iter().map(|n| stroke(n).paint).collect();
|
||||
assert_eq!(colors, [rgb(0, 0, 255), rgb(0, 255, 0)]);
|
||||
|
||||
// PW in millimeters for every pen, or for one; IN gives back 0.35 mm.
|
||||
let i = plot("IN;SP1;PW1;PD0,0,10,0;PU;PW2,2;SP2;PD0,10,10,10;PU;SP1;PD0,20,10,20;PU;IN;SP1;PD0,30,10,30;");
|
||||
let widths: Vec<f64> = leaves(&i.document).iter().map(|n| stroke(n).width).collect();
|
||||
let want = [MM, 2.0 * MM, MM, 0.35 * MM];
|
||||
assert!(widths.iter().zip(want).all(|(a, b)| (a - b).abs() < 1e-9), "{widths:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_run_with_the_pen_down_is_one_path() {
|
||||
// Relative moves; a run that comes back to its start is closed; a pen-up move elsewhere starts
|
||||
// another path, and one back to where the line ended continues it.
|
||||
let i = plot("IN;SP1;PW0;PA0,0;PR;PD1016,0,0,1016,-1016,0,0,-1016;PU2032,0;PD0,1016;PU0,1016;PU0,-1016;PD508,0;PU0,500;PD0,500;");
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!(shapes.len(), 3, "{shapes:#?}");
|
||||
let NodeKind::Path { path: square, .. } = &shapes[0].kind else { panic!("a path") };
|
||||
assert_eq!((square.subpaths.len(), square.subpaths[0].anchors.len(), square.subpaths[0].closed), (1, 4, true));
|
||||
assert_eq!(ends(&bez(&shapes[1])).len(), 3, "the run went on from where it ended");
|
||||
assert_eq!(ends(&bez(&shapes[2])).len(), 2);
|
||||
|
||||
// A pen-down move of no length draws a dot.
|
||||
let i = plot("IN;SP1;PA500,500;PD;PA500,500;PU;");
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!(shapes.len(), 1);
|
||||
let b = bez(&shapes[0]).bounding_box();
|
||||
assert!((b.width() - 72.0 / 1016.0).abs() < 1e-9, "{b:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn circles_arcs_and_curves_are_drawn_as_curves() {
|
||||
// A circle of an inch round the pen, which stays at its center (the next line starts there).
|
||||
let i = plot("IN;SP1;PW0;PA2032,2032;CI1016;PD3048,2032;");
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!(shapes.len(), 2);
|
||||
let c = bez(&shapes[0]);
|
||||
assert!(c.elements().iter().any(|e| matches!(e, PathEl::CurveTo(..))) && matches!(c.elements().last(), Some(PathEl::ClosePath)));
|
||||
// The curves stay within a thousandth of a point of the circle.
|
||||
let b = c.bounding_box();
|
||||
assert!((b.width() - 144.0).abs() < 1e-3 && (b.height() - 144.0).abs() < 1e-3, "{b:?}");
|
||||
let line = ends(&bez(&shapes[1]));
|
||||
assert!((line[0] - b.center()).hypot() < 1e-3, "{line:?} {b:?}");
|
||||
|
||||
// AA: a quarter turn counterclockwise round (0, 0) from (1016, 0) ends at (0, 1016), and the
|
||||
// pen goes on from there. AR takes its center relative to the pen.
|
||||
for s in ["IN;SP1;PW0;PA1016,0;PD;AA0,0,90;PA0,2032;", "IN;SP1;PW0;PA1016,0;PD;AR-1016,0,90;PA0,2032;"] {
|
||||
let shapes = leaves(&plot(s).document);
|
||||
assert_eq!(shapes.len(), 1, "{s}");
|
||||
let p = bez(&shapes[0]);
|
||||
let pts = ends(&p);
|
||||
// y down: (1016, 0) is at the bottom right, the arc's end on the left a quarter turn up.
|
||||
let (start, end) = (pts[0], pts[pts.len() - 2]);
|
||||
assert!(near(start, Point::new(72.125, 144.0)) && near(end, Point::new(0.125, 72.0)), "{s}: {pts:?}");
|
||||
assert!(near(*pts.last().unwrap(), Point::new(0.125, 0.0)), "{s}: {pts:?}");
|
||||
}
|
||||
|
||||
// AT: the arc from the pen through a point to an end; points in a line give a line.
|
||||
let shapes = leaves(&plot("IN;SP1;PW0;PA1016,0;PD;AT0,1016,-1016,0;").document);
|
||||
let pts = ends(&bez(&shapes[0]));
|
||||
let b = bez(&shapes[0]).bounding_box();
|
||||
assert!((b.width() - 144.0).abs() < 1e-6 && (b.height() - 72.0).abs() < 1e-6, "a half circle: {b:?}");
|
||||
assert!(near(pts[0], Point::new(144.125, 72.125)) && near(*pts.last().unwrap(), Point::new(0.125, 72.125)), "{pts:?}");
|
||||
let shapes = leaves(&plot("IN;SP1;PW0;PA0,0;PD;AT508,0,1016,0;").document);
|
||||
assert_eq!(ends(&bez(&shapes[0])).len(), 2);
|
||||
|
||||
// BZ and BR: cubic Béziers, BR's points relative to each curve's start.
|
||||
for s in [
|
||||
"IN;SP1;PW0;PA0,0;PD;BZ0,1016,1016,1016,1016,0,1016,-1016,2032,-1016,2032,0;",
|
||||
"IN;SP1;PW0;PA0,0;PD;BR0,1016,1016,1016,1016,0,0,-1016,1016,-1016,1016,0;",
|
||||
] {
|
||||
let shapes = leaves(&plot(s).document);
|
||||
let els = bez(&shapes[0]).elements().to_vec();
|
||||
assert_eq!(els.iter().filter(|e| matches!(e, PathEl::CurveTo(..))).count(), 2, "{s}");
|
||||
// The curves reach 762 plotter units (three quarters of their control points) up and down.
|
||||
assert!(near(els.last().and_then(PathEl::end_point).unwrap(), Point::new(144.125, 54.125)), "{s}: {els:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rectangles_wedges_and_polygons_fill_and_edge() {
|
||||
// RA fills, EA edges; neither moves the pen.
|
||||
let i = plot("IN;SP2;PW0;PA0,0;RA1016,1016;EA1016,1016;PD508,0;");
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!(shapes.len(), 3);
|
||||
assert_eq!((fill(&shapes[0]), shapes[0].appearance.items.len()), (Some(rgb(255, 0, 0)), 1));
|
||||
assert!(fill(&shapes[1]).is_none() && stroke(&shapes[1]).paint == rgb(255, 0, 0));
|
||||
assert!(near(ends(&bez(&shapes[2]))[0], Point::new(0.125, 72.125)), "the line starts where the pen was");
|
||||
|
||||
// WG fills a wedge, EW edges one; a whole turn is a circle.
|
||||
let i = plot("IN;SP1;PA0,0;WG1016,0,90;EW1016,0,360;");
|
||||
let shapes = leaves(&i.document);
|
||||
let wedge = bez(&shapes[0]);
|
||||
assert!(fill(&shapes[0]).is_some() && ends(&wedge).len() >= 3);
|
||||
let ring = bez(&shapes[1]).bounding_box();
|
||||
assert!((ring.width() - 144.0).abs() < 1e-6);
|
||||
|
||||
// Polygon mode: two squares, one inside the other; FP fills even-odd by default, FP1
|
||||
// non-zero; EP edges only what was drawn with the pen down.
|
||||
let poly = "IN;SP1;PW0;PA0,0;PM0;PD3048,0,3048,3048,0,3048,0,0;PM1;PU1016,1016;PD2032,1016;PU2032,2032;PD1016,2032,1016,1016;PM2;";
|
||||
let i = plot(&format!("{poly}FP;FP1;EP;"));
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!(shapes.len(), 3);
|
||||
assert_eq!((rule(&shapes[0]), rule(&shapes[1])), (FillRule::EvenOdd, FillRule::NonZero));
|
||||
let filled = bez(&shapes[0]);
|
||||
assert_eq!(filled.elements().iter().filter(|e| matches!(e, PathEl::ClosePath)).count(), 2);
|
||||
let edged = bez(&shapes[2]);
|
||||
assert_eq!(edged.elements().iter().filter(|e| matches!(e, PathEl::MoveTo(..))).count(), 3, "the pen-up side isn't edged: {edged:?}");
|
||||
// Nothing is drawn in polygon mode itself.
|
||||
assert!(leaves(&plot(poly).document).is_empty());
|
||||
|
||||
// A circle in polygon mode is a subpolygon of its own; CI and RA after PM2 work as before.
|
||||
let i = plot("IN;SP1;PA2032,2032;PM0;CI1016;PM2;FP;");
|
||||
assert_eq!(leaves(&i.document).len(), 1);
|
||||
|
||||
// Fill types: shading mixes the pen with white; hatching fills solid with a warning.
|
||||
let i = plot("IN;SP5;PA0,0;FT10,50;RA100,100;FT3,10,45;RA200,200;");
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!((fill(&shapes[0]), fill(&shapes[1])), (Some(rgb(128, 128, 255)), Some(rgb(0, 0, 255))));
|
||||
assert_eq!(i.warnings, [HATCHES]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_types_become_dash_patterns() {
|
||||
// A 10 mm pattern of line type 2 is 5 mm on, 5 mm off.
|
||||
let i = plot("IN;SP1;LT2,10,1;PD0,0,4000,0;");
|
||||
let d = stroke(&leaves(&i.document)[0]).dash.expect("dashed");
|
||||
assert!(d.pattern.iter().all(|v| (v - 5.0 * MM).abs() < 1e-9) && d.pattern.len() == 2 && !d.align_corners, "{d:?}");
|
||||
// The default length is 4% of the P1-P2 diagonal; IP sets P1 and P2.
|
||||
let i = plot("IN;IP0,0,3000,4000;SP1;LT3;PD0,0,4000,0;");
|
||||
let d = stroke(&leaves(&i.document)[0]).dash.expect("dashed");
|
||||
let total = 200.0 * 72.0 / 1016.0;
|
||||
assert!((d.pattern[0] - 0.7 * total).abs() < 1e-9 && (d.pattern[1] - 0.3 * total).abs() < 1e-9, "{d:?}");
|
||||
// Adaptive types stretch to each line; a zero length is a dot one plotter unit long.
|
||||
let i = plot("IN;SP1;LT-4,10,1;PD0,0,4000,0;");
|
||||
let d = stroke(&leaves(&i.document)[0]).dash.expect("dashed");
|
||||
assert!(d.align_corners && (d.pattern[2] - 72.0 / 1016.0).abs() < 1e-12, "{d:?}");
|
||||
// UL redefines a type; LT; draws solid and LT99 gives the type back.
|
||||
let i = plot("IN;SP1;UL2,1,3;LT2,10,1;PD0,0,4000,0;PU;LT;PD0,100,4000,100;PU;LT99;PD0,200,4000,200;");
|
||||
let shapes = leaves(&i.document);
|
||||
let d = stroke(&shapes[0]).dash.expect("dashed");
|
||||
assert!((d.pattern[0] - 2.5 * MM).abs() < 1e-9 && (d.pattern[1] - 7.5 * MM).abs() < 1e-9, "{d:?}");
|
||||
assert!(stroke(&shapes[1]).dash.is_none() && stroke(&shapes[2]).dash.is_some());
|
||||
// Line type 0 draws a dot at each point.
|
||||
let i = plot("IN;SP1;LT0;PD0,0,1000,0,2000,0;");
|
||||
assert_eq!(leaves(&i.document).len(), 3);
|
||||
// LA sets the caps, joins and miter limit.
|
||||
let s = stroke(&leaves(&plot("IN;SP1;LA1,4,2,4,3,9;PD0,0,100,0;").document)[0]);
|
||||
assert_eq!((s.cap, s.join, s.miter_limit), (LineCap::Round, LineJoin::Round, 9.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scaling_maps_user_units_onto_p1_and_p2() {
|
||||
// Anisotropic: 100 user units across an inch.
|
||||
let size = |s: &str| {
|
||||
let shapes = leaves(&plot(s).document);
|
||||
bez(&shapes[0]).bounding_box().size()
|
||||
};
|
||||
let s = size("IN;IP0,0,1016,2032;SC0,100,0,100;SP1;PD0,0,100,100;");
|
||||
assert!((s.width - 72.0).abs() < 1e-9 && (s.height - 144.0).abs() < 1e-9, "{s:?}");
|
||||
// Isotropic: the smaller of the two scales on both axes.
|
||||
let s = size("IN;IP0,0,1016,2032;SC0,100,0,100,1;SP1;PU0,0;PD100,100;");
|
||||
assert!((s.width - 72.0).abs() < 1e-9 && (s.height - 72.0).abs() < 1e-9, "{s:?}");
|
||||
// Point factor: 10 plotter units per user unit.
|
||||
let s = size("IN;SC0,10,0,10,2;SP1;PD0,0,101.6,50.8;");
|
||||
assert!((s.width - 72.0).abs() < 1e-9 && (s.height - 36.0).abs() < 1e-9, "{s:?}");
|
||||
// Without IP, P2 lies 11 × 8.5 in from the origin.
|
||||
let s = size("IN;SC0,11,0,8.5;SP1;PD0,0,1,1;");
|
||||
assert!((s.width - 72.0).abs() < 1e-9 && (s.height - 72.0).abs() < 1e-9, "{s:?}");
|
||||
// SC; goes back to plotter units; scaling that makes no area is ignored.
|
||||
let s = size("IN;SC0,1,0,1;SC;SC0,0,0,1;SP1;PD0,0,1016,1016;");
|
||||
assert!((s.width - 72.0).abs() < 1e-9, "{s:?}");
|
||||
// RO turns the coordinate system: x runs up.
|
||||
let shapes = leaves(&plot("IN;RO90;SP1;PW0;PA0,0;PD1016,0;").document);
|
||||
let b = bez(&shapes[0]).bounding_box();
|
||||
assert!(b.width() < 1e-9 && (b.height() - 72.0).abs() < 1e-9, "{b:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn polyline_encoded_data_is_read() {
|
||||
let mut f = b"IN;SP1;PW0;PE".to_vec();
|
||||
// Pen 2, a pen-up move to the absolute (1016, 0), then a relative line an inch up.
|
||||
f.push(b':');
|
||||
f.extend(pe_number(2, false));
|
||||
f.extend(b"<=");
|
||||
f.extend(pe_number(1016, false));
|
||||
f.extend(pe_number(0, false));
|
||||
f.extend(pe_number(0, false));
|
||||
f.extend(pe_number(1016, false));
|
||||
f.push(b';');
|
||||
let i = plot_bytes(&f);
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!(shapes.len(), 1);
|
||||
assert_eq!(stroke(&shapes[0]).paint, rgb(255, 0, 0));
|
||||
assert_points(&ends(&bez(&shapes[0])), &[(0.125, 72.0), (0.125, 0.0)]);
|
||||
|
||||
// 7-bit data with two fractional bits: 4064 / 4 = 1016.
|
||||
let mut f = b"IN;SP1;PW0;PE7>".to_vec();
|
||||
f.extend(pe_number(2, true));
|
||||
f.extend(pe_number(-4064, true));
|
||||
f.extend(pe_number(0, true));
|
||||
f.push(b';');
|
||||
let b = bez(&leaves(&plot_bytes(&f).document)[0]).bounding_box();
|
||||
assert!((b.width() - 72.0).abs() < 1e-9, "{b:?}");
|
||||
|
||||
// Data that can't be decoded ends the instruction at its semicolon: none of it is read as
|
||||
// instructions.
|
||||
let shapes = leaves(&plot("IN;SP1;PE#PD5000,5000;PD1016,0;").document);
|
||||
assert_eq!(shapes.len(), 1);
|
||||
assert!((bez(&shapes[0]).bounding_box().width() - 72.0).abs() < 1e-9);
|
||||
|
||||
// Rectangle mode: a pen-up move, then a filled rectangle relative to it.
|
||||
let mut f = b"IN;SP3;PE9".to_vec();
|
||||
for v in [100, 100, 1016, 508] {
|
||||
f.extend(pe_number(v, false));
|
||||
}
|
||||
f.push(b';');
|
||||
let shapes = leaves(&plot_bytes(&f).document);
|
||||
assert_eq!(fill(&shapes[0]), Some(rgb(0, 255, 0)));
|
||||
let b = bez(&shapes[0]).bounding_box();
|
||||
assert!((b.width() - 72.0).abs() < 1e-9 && (b.height() - 36.0).abs() < 1e-9, "{b:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn printer_escapes_and_job_lines_are_skipped() {
|
||||
let mut f = b"\x1b%-12345X@PJL JOB\r\n@PJL ENTER LANGUAGE=PCL\r\n\x1bE\x1b%0BIN;SP1;PW0;PD0,0,1016,0;PU;\x1b%0A".to_vec();
|
||||
// An HP RTL raster image whose data spells out instructions.
|
||||
f.extend(b"\x1b*r1A\x1b*b11WPD9999,9999\x1b*rC");
|
||||
f.extend(b"\x1b%1BPD1016,1016;\x1b%-12345X@PJL EOJ\r\n");
|
||||
assert!(is_hpgl(&f));
|
||||
let i = plot_bytes(&f);
|
||||
let shapes = leaves(&i.document);
|
||||
// The pen went up and came down where the line ended, so the line goes on.
|
||||
assert_eq!(shapes.len(), 1, "{shapes:#?}");
|
||||
assert_eq!(ends(&bez(&shapes[0])).len(), 3);
|
||||
assert_eq!(i.warnings, [RASTER], "{:?}", i.warnings);
|
||||
let r = i.document.artboards[0].rect;
|
||||
assert!((r.width() - 72.125).abs() < 1e-9 && (r.height() - 72.125).abs() < 1e-9, "{r:?}");
|
||||
|
||||
// The device-control escapes of older plotters.
|
||||
let f = b"\x1b.(\x1b.I81;;17:\x1b.N;19:IN;SP1;PD0,0,1016,0;\x1b.)";
|
||||
assert!(is_hpgl(f));
|
||||
assert_eq!(leaves(&plot_bytes(f).document).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn labels_comments_and_unknown_instructions_are_skipped() {
|
||||
// A label's text, up to its terminator (ETX, or the one DT sets), isn't read as instructions.
|
||||
let i = plot("IN;SP1;LBPD9999,9999\x03DT*,1;LBPU0,0;PD5000,5000*CO\"PD8000,8000\";PD1016,0;PU;ZZ1,2;QQ;");
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!(shapes.len(), 1);
|
||||
assert_eq!(i.warnings, [LABELS.to_string(), "2 instructions of kinds VectorCraft doesn't read were skipped".to_string()]);
|
||||
// Symbol mode's symbols aren't drawn, with a warning; its lines are.
|
||||
let i = plot("IN;SP1;SM*;PD100,0;SM;");
|
||||
assert_eq!((i.warnings.clone(), leaves(&i.document).len()), (vec![SYMBOLS.to_string()], 1));
|
||||
// Later pages and the input window are warned about.
|
||||
let i = plot("IN;IW0,0,10,10;SP1;PD100,0;PU;PG;PD0,100;PG;");
|
||||
assert_eq!(i.warnings, [WINDOW, PAGES]);
|
||||
// A page advance raises the pen at the origin: nothing joins the pages.
|
||||
let i = plot("IN;SP1;PD100,0;PG;PA200,0;PD300,0;");
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!(shapes.len(), 2);
|
||||
assert_eq!(ends(&bez(&shapes[1])).len(), 2, "the second page's line starts at (200, 0)");
|
||||
// A page advance at the end changes nothing.
|
||||
assert!(plot("IN;SP1;PD100,0;PG;").warnings.is_empty());
|
||||
// A file that draws nothing opens as a letter-size page.
|
||||
let i = plot("IN;SP1;PU100,100;");
|
||||
assert!(leaves(&i.document).is_empty() && i.document.artboards[0].rect.size() == kurbo::Size::new(612.0, 792.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_plot_files_are_taken_for_hpgl() {
|
||||
for f in [
|
||||
&b"IN;SP1;PU0,0;PD100,100;"[..],
|
||||
b"in;sp1;pu0,0;pd100,100;",
|
||||
b";IN;\r\nSP1;\r\nPA100,100;",
|
||||
b"\x03IN;",
|
||||
b"SP1PD0,0,10,10PU",
|
||||
b"IN",
|
||||
b"CO\"made by hand\";IN;",
|
||||
b"IN;SP1;DT*;LBhello world*PA0,0;SMx;PA1,1;SM;",
|
||||
b"IN;SP1;PE<=\xbf\xbf;PU;",
|
||||
b"\x1b%-12345X@PJL ENTER LANGUAGE=HPGL2\nIN;SP1;",
|
||||
] {
|
||||
assert!(is_hpgl(f), "{:?}", String::from_utf8_lossy(f));
|
||||
}
|
||||
for f in [
|
||||
&b""[..],
|
||||
b"IN THE BEGINNING",
|
||||
b"set terminal png\nplot sin(x)\n",
|
||||
b"Paris, 10 to 12",
|
||||
b"SPDX-License-Identifier: MIT",
|
||||
b"CONTENTS",
|
||||
b"CO \"a comment\";hello world",
|
||||
b"DT*;LBtext*nope",
|
||||
b"\x89PNG\r\n\x1a\n",
|
||||
b"IN;SP1;PD0,0;hello world",
|
||||
b"\x1b%-12345X@PJL ENTER LANGUAGE=PCL\n\x1bE\x1b*r1A",
|
||||
] {
|
||||
assert!(!is_hpgl(f), "{:?}", String::from_utf8_lossy(f));
|
||||
assert_eq!(import(f).err().as_deref(), Some("not an HP-GL file"), "{:?}", String::from_utf8_lossy(f));
|
||||
}
|
||||
// A comment needs no string; instructions of an older or a device's own set count when a
|
||||
// semicolon ends them.
|
||||
for f in [&b"CO;IN;SP1;PD0,0,10,10;"[..], b"IN;CO;SP1;PD0,0,10,10;", b"IN;SP1;VA;VS10;PU0,0;PD1016,0;", b"IN;GC5;SP1;PD0,0,10,0;"] {
|
||||
assert!(is_hpgl(f), "{:?}", String::from_utf8_lossy(f));
|
||||
}
|
||||
assert!(!is_hpgl(b"IN;GC5") && !is_hpgl(b"IN;Hello, world;"));
|
||||
// A printer prolog of any length comes before the first instruction.
|
||||
let long = [&b"\x1b%-12345X"[..], &b"@PJL COMMENT a job line of some length\r\n".repeat(400), b"\x1bE\x1b%0BIN;SP1;PD100,0;"].concat();
|
||||
assert!(long.len() > 16_000 && is_hpgl(&long));
|
||||
assert_eq!(leaves(&plot_bytes(&long).document).len(), 1);
|
||||
// Instructions of any kind may follow the first eight, garbage included.
|
||||
assert!(is_hpgl(b"IN;SP1;PU;PD;PU;PD;PU;PD;hello world"));
|
||||
}
|
||||
|
||||
/// A file read as HP-GL (picked by its extension) may start with any instruction the reader knows,
|
||||
/// or one of another set that a semicolon ends; its content alone doesn't make it HP-GL.
|
||||
#[test]
|
||||
fn a_plot_file_may_start_with_any_instruction() {
|
||||
for f in [&b"SI0.2,0.3;SP1;PA0,0;PD1016,0;"[..], b"DI1,0;SP1;PA0,0;PD1016,0;", b"GC5;SP1;PA0,0;PD1016,0;"] {
|
||||
assert!(!is_hpgl(f), "{:?}", String::from_utf8_lossy(f));
|
||||
assert_eq!(leaves(&plot_bytes(f).document).len(), 1, "{:?}", String::from_utf8_lossy(f));
|
||||
}
|
||||
for f in [&b"GC5"[..], b"SIhello world", b"set terminal png\n"] {
|
||||
assert_eq!(import(f).err().as_deref(), Some("not an HP-GL file"), "{:?}", String::from_utf8_lossy(f));
|
||||
}
|
||||
// Instructions of another set alone, however many or long, are not taken for HP-GL.
|
||||
let long = [&b"GC"[..], "1".repeat(5000).as_bytes(), b";ZZ1;"].concat();
|
||||
for f in [&b"GC5;"[..], b"GC5;ZZ1;", b"GC5;ZZ1;YY2;XX3;WW4;VV5;UU6;TT7;", &long] {
|
||||
assert_eq!(import(f).err().as_deref(), Some("not an HP-GL file"), "{:?}", String::from_utf8_lossy(f));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escapes_that_carry_values_or_data_are_skipped_whole() {
|
||||
// A reset leaves HP-GL until a language switch; symbol-set escapes carry a value and a letter.
|
||||
for f in [
|
||||
&b"\x1bE\x1b(8U\x1b%1BIN;SP1;PD0,0,1016,0;"[..],
|
||||
b"\x1bE\x1b(10U\x1b)0N\x1b%1BIN;SP1;PD0,0,1016,0;",
|
||||
b"\x1bEHello\x1b%1BIN;SP1;PD0,0,1016,0;",
|
||||
] {
|
||||
assert!(is_hpgl(f), "{:?}", String::from_utf8_lossy(f));
|
||||
assert_eq!(leaves(&plot_bytes(f).document).len(), 1, "{:?}", String::from_utf8_lossy(f));
|
||||
}
|
||||
// A letter left over from an escape doesn't join the next mnemonic.
|
||||
let i = plot_bytes(b"IN;SP1;\x1b(8UPD0,0,1016,0;");
|
||||
assert!(i.warnings.is_empty() && leaves(&i.document).len() == 1, "{:?}", i.warnings);
|
||||
// Raster data by plane and transparent print data are skipped by their byte counts, however
|
||||
// they read.
|
||||
let line = |data_escape: &[u8]| {
|
||||
let data = b"\x1b%1BPD0,5000;";
|
||||
let mut f = b"IN;SP1;PW0;PD0,0,0,1016;\x1b%0A".to_vec();
|
||||
f.extend(data_escape);
|
||||
f.extend(data.len().to_string().as_bytes());
|
||||
f.extend(if data_escape.ends_with(b"b") { &b"V"[..] } else { b"X" });
|
||||
f.extend(data);
|
||||
f.extend(b"\x1b*rC\x1b%1BPD0,0;");
|
||||
bez(&leaves(&plot_bytes(&f).document)[0]).bounding_box()
|
||||
};
|
||||
for escape in [&b"\x1b*r1A\x1b*b"[..], b"\x1b&p"] {
|
||||
let b = line(escape);
|
||||
assert!((b.height() - 72.0).abs() < 1e-9, "{:?}: {b:?}", String::from_utf8_lossy(escape));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn polygon_mode_follows_its_own_rules() {
|
||||
// The point after a circle starts a new subpolygon: the triangle after it doesn't reach back
|
||||
// to the circle's center, filled or edged.
|
||||
let i = plot("IN;SP1;PW0;PA1000,1000;PM0;CI200;PD2000,1000,2000,2000,1000,2000;PM2;FP;EP;");
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!(shapes.len(), 2);
|
||||
for s in &shapes {
|
||||
let p = bez(s);
|
||||
let first = p.elements().iter().position(|e| matches!(e, PathEl::ClosePath)).unwrap_or(p.elements().len());
|
||||
let circle = BezPath::from_vec(p.elements()[..first].to_vec()).bounding_box();
|
||||
let center = circle.center();
|
||||
assert!(ends(&p).iter().all(|e| !near(*e, center)), "{p:?}");
|
||||
}
|
||||
// PM1 closes with the pen up: EP draws two sides of the triangle, not the third; PM2 closes
|
||||
// with the pen as it is.
|
||||
let sides = |s: &str| {
|
||||
let p = bez(&leaves(&plot(s).document)[0]);
|
||||
p.elements().iter().filter(|e| matches!(e, PathEl::LineTo(..))).count()
|
||||
};
|
||||
assert_eq!(sides("IN;SP1;PA0,0;PM0;PD1000,0,1000,1000;PM1;PM2;EP;"), 2);
|
||||
assert_eq!(sides("IN;SP1;PA0,0;PM0;PD1000,0,1000,1000;PM2;EP;"), 3);
|
||||
// The closing point moves the pen: a relative move after PM1 starts from the start.
|
||||
let i = plot("IN;SP1;PA0,0;PM0;PD1000,0,1000,1000;PM1;PU;PR0,2000;PD1000,0,0,1000;PM2;FP;");
|
||||
let b = bez(&leaves(&i.document)[0]).bounding_box();
|
||||
assert!((b.height() - 3000.0 * PT_PER_PLU).abs() < 1e-9, "{b:?}");
|
||||
// A subpolygon of fewer than three points (the closing point counted) is neither filled nor
|
||||
// edged.
|
||||
assert!(leaves(&plot("IN;SP1;PA0,0;PM0;PD0,0;PM2;FP;EP;").document).is_empty());
|
||||
let i = plot("IN;SP1;PA0,0;PM0;PR0,0;PM1;PA;PD1000,0,0,1000,0,0;PM2;FP;");
|
||||
let filled = bez(&leaves(&i.document)[0]);
|
||||
assert_eq!(filled.elements().iter().filter(|e| matches!(e, PathEl::ClosePath)).count(), 1, "{filled:?}");
|
||||
// Polygon mode ignores the instructions it doesn't allow: pens, scaling and labels.
|
||||
let i = plot("IN;SP1;PA0,0;PM0;SP2;SC0,1,0,1;LBPD9999,9999\x03PD1016,0,1016,1016;PM2;FP;");
|
||||
let shapes = leaves(&i.document);
|
||||
assert_eq!((shapes.len(), fill(&shapes[0])), (1, Some(rgb(0, 0, 0))));
|
||||
assert!((bez(&shapes[0]).bounding_box().width() - 72.0).abs() < 1e-9 && i.warnings.is_empty(), "{:?}", i.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotation_keeps_the_pen_and_turns_the_defaults() {
|
||||
// After RO90, IP's default P2 is the rotated hard-clip corner: the user square spans 8.5 in
|
||||
// across its rotated x axis and 11 in up it, which lie up and across the page.
|
||||
let i = plot("IN;RO90;IP;SC0,100,0,100;SP1;PW0;PA0,0;PD100,0,100,100,0,100,0,0;");
|
||||
let b = bez(&leaves(&i.document)[0]).bounding_box();
|
||||
assert!((b.width() - 792.0).abs() < 1e-9 && (b.height() - 612.0).abs() < 1e-9, "{b:?}");
|
||||
// IR's percentages are of the rotated limits too.
|
||||
let i = plot("IN;RO90;IR0,0,100,100;SC0,1,0,1;SP1;PW0;PA0,0;PD1,1;");
|
||||
let b = bez(&leaves(&i.document)[0]).bounding_box();
|
||||
assert!((b.width() - 792.0).abs() < 1e-9 && (b.height() - 612.0).abs() < 1e-9, "{b:?}");
|
||||
// The pen stays where it was on the page: a line after RO starts where the one before ended.
|
||||
let i = plot("IN;SP1;PW0;PD0,0,1016,0;PU;RO90;PD;PR1016,0;");
|
||||
let all = leaves(&i.document).iter().map(|n| bez(n).bounding_box()).reduce(|a, b| a.union(b)).unwrap();
|
||||
assert!((all.width() - 72.0).abs() < 1e-9 && (all.height() - 72.0).abs() < 1e-9, "{all:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_type_zero_and_odd_patterns_follow_the_reference() {
|
||||
// Line type 0: one dot for an arc (at its end) and one for a circle (at its center).
|
||||
for s in ["IN;SP1;LT0;PA0,0;PD;AA1016,0,180;", "IN;SP1;LT0;PA0,0;CI500;"] {
|
||||
let shapes = leaves(&plot(s).document);
|
||||
assert_eq!(shapes.len(), 1, "{s}");
|
||||
assert!(bez(&shapes[0]).bounding_box().width() < 0.1, "{s}");
|
||||
}
|
||||
// An odd number of gaps repeats with the last pen-down stretch running into the first.
|
||||
let i = plot("IN;SP1;UL3,50,25,25;LT3,10,1;PD0,0,4000,0;");
|
||||
let d = stroke(&leaves(&i.document)[0]).dash.expect("dashed");
|
||||
assert!((d.pattern[0] - 7.5 * MM).abs() < 1e-9 && (d.pattern[1] - 2.5 * MM).abs() < 1e-9 && d.pattern.len() == 2, "{d:?}");
|
||||
assert!((d.offset - 2.5 * MM).abs() < 1e-9, "{d:?}");
|
||||
// One pen-down stretch is a solid line; a dot keeps the pattern's length.
|
||||
let d = stroke(&leaves(&plot("IN;SP1;UL3,100;LT3,10,1;PD0,0,4000,0;").document)[0]).dash.expect("dashed");
|
||||
assert_eq!(d.pattern[1], 0.0);
|
||||
let d = stroke(&leaves(&plot("IN;SP1;LT4,10,1;PD0,0,4000,0;").document)[0]).dash.expect("dashed");
|
||||
assert!((d.pattern.iter().sum::<f64>() - 10.0 * MM).abs() < 1e-9, "{d:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pen_colors_ignore_two_or_three_parameters() {
|
||||
let i = plot("IN;PC3,0,0,255;PC3,255;PC3,255,0;SP3;PD0,0,10,0;");
|
||||
assert_eq!(stroke(&leaves(&i.document)[0]).paint, rgb(0, 0, 255));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_files_import_without_panics() {
|
||||
let mut cases: Vec<Vec<u8>> = vec![
|
||||
// Coordinates past any plotter's range, and numbers with too many digits.
|
||||
b"IN;SP1;PD99999999999999999999999999999,0;PU0,0;PD1016,1016;".to_vec(),
|
||||
b"IN;SP1;PD0.000000000000000000000000001,1e999,--,++,..;".to_vec(),
|
||||
// Unterminated labels, comments and encoded polylines.
|
||||
b"IN;SP1;LBno end".to_vec(),
|
||||
b"IN;CO\"no end".to_vec(),
|
||||
b"IN;SP1;PE<=\xff\xff\xff\xfe\xfe".to_vec(),
|
||||
b"IN;SP1;PE>\x7e\x7e\x7e\x7e\x7e\x7e\x7e\x7e\x7e\x7e\x7e\x7e\xfe\x40\xc0;".to_vec(),
|
||||
// Arcs and wedges of every degenerate kind, with huge sweeps.
|
||||
b"IN;SP1;PD;AA0,0,32767;AR0,0,90;AT0,0,0,0;RT1,1,2,2;CI0;CI-5;WG0,0,0;EW5,99999,-99999;WG-5,0,360;CI99999999999999;".to_vec(),
|
||||
// Scaling onto one point, rotations the format doesn't define, huge palettes and pens.
|
||||
b"IN;IP5,5,5,5;SC0,1,0,1,1;SP1;PD1,1;SC0,0,0,0,2;RO45;NP99999999;SP99999999999;PC99999999,1,2,3;PW99999,99999;PD2,2;".to_vec(),
|
||||
// Polygon mode left open, PM1 and PM2 without PM0, FP and EP with nothing buffered.
|
||||
b"IN;SP1;FP;EP;PM1;PM2;PM0;PD10,10;PM1;PM1;PM1;CI5;PE<\xc0\xc0;".to_vec(),
|
||||
// Escapes cut short, and data lengths past the end.
|
||||
b"\x1b%-12345X@PJL".to_vec(),
|
||||
b"IN;SP1;PD0,0;\x1b*b99999999999W".to_vec(),
|
||||
b"IN;\x1b".to_vec(),
|
||||
b"IN;\x1b.".to_vec(),
|
||||
];
|
||||
// A job line with no end.
|
||||
cases.push([&b"\x1b%-12345X@PJL "[..], &b"x".repeat(1 << 20)].concat());
|
||||
// Bytes of every value after a valid start.
|
||||
cases.push([&b"IN;SP1;PD"[..], &(0..=255u8).collect::<Vec<_>>()].concat());
|
||||
for f in &cases {
|
||||
let r = std::panic::catch_unwind(|| import(f));
|
||||
assert!(r.is_ok(), "{:?}", String::from_utf8_lossy(f));
|
||||
}
|
||||
let far = import(&cases[0]).unwrap();
|
||||
assert!(far.warnings.iter().any(|w| w == FAR_AWAY), "{:?}", far.warnings);
|
||||
assert_eq!(leaves(&far.document).len(), 1, "the line in range is kept");
|
||||
|
||||
// Past the budget for path points, the rest is left out with a warning; pen-up moves, which
|
||||
// make no path, don't count.
|
||||
let many = format!("IN;SP1;PR;PD{}", "1,0,".repeat(300_000));
|
||||
let i = plot(&many);
|
||||
assert!(i.warnings.iter().any(|w| w == TOO_MANY_POINTS), "{:?}", i.warnings);
|
||||
// Scaling points past any range give relative pen widths no wider than the widest pen.
|
||||
let i = plot("IN;IP-99999999999999999999,0,99999999999999999999,0;WU1;PW32767;SP1;PD100,0;");
|
||||
let r = i.document.artboards[0].rect;
|
||||
assert!(r.width().is_finite() && r.height() <= 16384.0 * 72.0 / 1016.0 + 1e-6, "{r:?}");
|
||||
assert!((stroke(&leaves(&i.document)[0]).width - 16384.0 * 72.0 / 1016.0).abs() < 1e-9);
|
||||
|
||||
// Each fill of the polygon buffer is a copy of it, and counts.
|
||||
let big = format!("IN;SP1;PM0;PR;PD{}PM2;{}", "1,1,".repeat(1000), "FP;EP;".repeat(1000));
|
||||
let i = plot(&big);
|
||||
assert!(i.warnings.iter().any(|w| w == TOO_MANY_POINTS) && leaves(&i.document).len() < 400, "{:?}", i.warnings);
|
||||
for pen in ["SP1;PU", "SP0;PD"] {
|
||||
let moves = format!("IN;{pen}{};SP1;PU0,0;PD1016,0;", "1,0,".repeat(300_000));
|
||||
let i = plot(&moves);
|
||||
assert!(i.warnings.is_empty() && leaves(&i.document).len() == 1, "{pen}: {:?}", i.warnings);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
//! CAD interchange: a hand-written ASCII DXF writer (versions R12 to 2018) and reader.
|
||||
//! CAD interchange: a hand-written ASCII DXF writer (versions R12 to 2018) and reader, and an
|
||||
//! HP-GL/2 plot file reader.
|
||||
//!
|
||||
//! [`export`] writes the visible art of a document into one DXF drawing: each layer becomes a DXF
|
||||
//! layer (hidden ones switched off, locked ones locked, non-printing ones not plotted), paths
|
||||
@@ -11,13 +12,16 @@
|
||||
//! clipping) is approximated or left out, and each such loss comes back as a warning.
|
||||
//!
|
||||
//! [`import()`] reads an ASCII DXF drawing (any version) into a document: see [`import`].
|
||||
//! [`hpgl::import`] reads an HP-GL plotter file: see [`hpgl`].
|
||||
//!
|
||||
//! - `aci`: the 256-colour index palette and the nearest index of a colour.
|
||||
//! - `writer`: group codes, handles and the file's sections, tables and objects.
|
||||
//! - `scene`: the document walk that turns objects into entities.
|
||||
//! - `import`: the reader, entities to art, blocks to symbols.
|
||||
//! - `hpgl`: the HP-GL reader, the pen's moves to paths.
|
||||
|
||||
mod aci;
|
||||
pub mod hpgl;
|
||||
pub mod import;
|
||||
mod scene;
|
||||
mod writer;
|
||||
|
||||
@@ -26,11 +26,16 @@ impl WidthProfile {
|
||||
pub fn at(&self, t: f64) -> (f64, f64) {
|
||||
Self::at_points(&self.points, t)
|
||||
}
|
||||
/// [`Self::at`] of a profile's points (a preset's or a saved profile's). Each side runs through
|
||||
/// its points on a monotone cubic curve (Fritsch–Carlson): it flows through a width point
|
||||
/// without a kink, turns flat at a point wider or narrower than both neighbours, and never
|
||||
/// goes past either point of the span it is in (#5714). Points on one straight line stay on
|
||||
/// it. A step (two points at one position, [`Self::around`]) ends the curve on each side of it.
|
||||
/// [`Self::at`] of a profile's points (a preset's or a saved profile's).
|
||||
///
|
||||
/// Each side runs through its points on a cubic Hermite curve, its slope at each one the
|
||||
/// weighted harmonic mean of the secants either side (Fritsch–Carlson). That is C1 continuous
|
||||
/// and monotone between points, so it never overshoots them, a point eases into its neighbours
|
||||
/// instead of making a corner, and moving one reshapes the curve as far as its neighbours'
|
||||
/// neighbours (their slopes depend on it). The ends of a run use the one-sided PCHIP slope
|
||||
/// from its first two secants, or the secant when it has only two points, so a two-point
|
||||
/// profile stays a straight taper and the lens is a parabola. Two points at one `t` are still
|
||||
/// a step: each run either side ends independently at it.
|
||||
pub fn at_points(p: &[(f64, f64, f64)], t: f64) -> (f64, f64) {
|
||||
if p.is_empty() {
|
||||
return (1.0, 1.0);
|
||||
@@ -71,9 +76,10 @@ impl WidthProfile {
|
||||
// Two swells between narrow necks.
|
||||
ProfilePreset { id: "wave", label: "Wave", points: &[(0.0, 0.3, 0.3), (0.25, 1.0, 1.0), (0.5, 0.3, 0.3), (0.75, 1.0, 1.0), (1.0, 0.3, 0.3)] },
|
||||
];
|
||||
/// The widest the stroke gets, as a multiple of its weight, 1 for a profile without points. Each
|
||||
/// side stays between the widths of the two points of each span ([`Self::at_points`]), but the
|
||||
/// two sides needn't peak together, so a span counts its wider left and its wider right.
|
||||
/// The widest the stroke gets, as a multiple of its weight, 1 for a profile without points.
|
||||
/// Each side's smooth curve stays between the widths of the two points of each span
|
||||
/// ([`Self::at_points`]), but the two sides needn't peak together, so a span counts its wider
|
||||
/// left and its wider right.
|
||||
pub fn widest(&self) -> f64 {
|
||||
let p = &self.points;
|
||||
if p.is_empty() {
|
||||
@@ -249,11 +255,27 @@ impl AngleProfile {
|
||||
/// How close two width points' positions are to make a step ([`WidthProfile::around`]).
|
||||
const STEP: f64 = 1e-7;
|
||||
|
||||
/// The one-sided three-point PCHIP slope at the start of a run, from the first two secants.
|
||||
/// A sign change caps the slope, and a slope that turns back on the first secant is flattened.
|
||||
/// Runs of only two points use their secant instead (see [`tangent`]).
|
||||
fn end_slope(h0: f64, h1: f64, d0: f64, d1: f64) -> f64 {
|
||||
let m = ((2.0 * h0 + h1) * d0 - h0 * d1) / (h0 + h1);
|
||||
if !m.is_finite() || m * d0 <= 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
if d0 * d1 < 0.0 {
|
||||
return if d0 > 0.0 { m.min(3.0 * d0) } else { m.max(3.0 * d0) };
|
||||
}
|
||||
m
|
||||
}
|
||||
|
||||
/// The slope of one side (`v` reads it from a point) of width profile `p` at its point `j`, in
|
||||
/// width per unit of position: Fritsch–Carlson's choice for a monotone cubic curve. It is 0 where
|
||||
/// the point is wider or narrower than both neighbours, the weighted harmonic mean of the slopes
|
||||
/// of the spans on either side elsewhere (never more than three times either, so no span
|
||||
/// overshoots its points), and the span's own slope at an end of the profile or beside a step.
|
||||
/// the point is wider or narrower than both neighbours, and the weighted harmonic mean of the
|
||||
/// slopes of the spans on either side elsewhere (never more than three times either, so no span
|
||||
/// overshoots its points). The ends of a run take the one-sided three-point PCHIP slope, or their
|
||||
/// secant when the run has only two points; a step ends the run on each side, so the points either
|
||||
/// side of it do not see across it.
|
||||
fn tangent(p: &[(f64, f64, f64)], j: usize, v: fn(&(f64, f64, f64)) -> f64) -> f64 {
|
||||
let Some(q) = p.get(j) else { return 0.0 };
|
||||
let before = j.checked_sub(1).and_then(|k| p.get(k)).filter(|a| q.0 - a.0 > STEP);
|
||||
@@ -268,8 +290,18 @@ fn tangent(p: &[(f64, f64, f64)], j: usize, v: fn(&(f64, f64, f64)) -> f64) -> f
|
||||
let (h0, h1) = (q.0 - a.0, b.0 - q.0);
|
||||
3.0 * (h0 + h1) / ((2.0 * h1 + h0) / d0 + (h1 + 2.0 * h0) / d1)
|
||||
}
|
||||
(Some(a), None) => slope(a, q),
|
||||
(None, Some(b)) => slope(q, b),
|
||||
// The end of a run: its previous two points set a one-sided three-point slope, or its
|
||||
// secant when the run has only two points (the point before `a` is on the other side of a
|
||||
// step, or does not exist).
|
||||
(Some(a), None) => {
|
||||
let Some(prev) = j.checked_sub(2).and_then(|k| p.get(k)).filter(|z| a.0 - z.0 > STEP) else { return slope(a, q) };
|
||||
end_slope(q.0 - a.0, a.0 - prev.0, slope(a, q), slope(prev, a))
|
||||
}
|
||||
// The start of a run, symmetrically.
|
||||
(None, Some(b)) => {
|
||||
let Some(next) = p.get(j + 2).filter(|z| z.0 - b.0 > STEP) else { return slope(q, b) };
|
||||
end_slope(b.0 - q.0, next.0 - b.0, slope(q, b), slope(b, next))
|
||||
}
|
||||
(None, None) => 0.0,
|
||||
}
|
||||
}
|
||||
@@ -424,12 +456,12 @@ mod tests {
|
||||
#[test]
|
||||
fn widths_run_smoothly_through_width_points() {
|
||||
// A swell in the middle: flat at its widest, and a quarter of the way along above the
|
||||
// straight ramp from 1 to 3 (2).
|
||||
// straight ramp from 1 to 3 (2) because the end eases out of the first point.
|
||||
let swell = WidthProfile { points: vec![(0.0, 1.0, 1.0), (0.5, 3.0, 2.0), (1.0, 1.0, 1.0)] };
|
||||
let (before, after) = slopes(&swell, 0.5);
|
||||
assert!(before.abs() < 1e-4 && after.abs() < 1e-4, "{before} {after}");
|
||||
let (l, r) = swell.at(0.25);
|
||||
assert!((l - 2.25).abs() < 1e-12 && (r - 1.625).abs() < 1e-12, "{l} {r}");
|
||||
assert!((l - 2.5).abs() < 1e-12 && (r - 1.75).abs() < 1e-12, "{l} {r}");
|
||||
// Rising on both sides of a point: the slope is the same either side of it.
|
||||
let rise = WidthProfile { points: vec![(0.0, 0.0, 0.0), (0.3, 1.0, 1.0), (1.0, 4.0, 4.0)] };
|
||||
let (before, after) = slopes(&rise, 0.3);
|
||||
|
||||
@@ -197,12 +197,85 @@ fn strokes_without_pressure_round_trip_unchanged() {
|
||||
fn profile_interp() {
|
||||
let p = WidthProfile::lens();
|
||||
assert_eq!(p.at(0.5), (1.0, 1.0));
|
||||
// Half way up the lens the width curves above the straight ramp (0.5), flattening into its
|
||||
// widest point (#5714).
|
||||
assert_eq!(p.at(0.25), (0.625, 0.625));
|
||||
// The lens is a parabola between its ends, so a quarter of the way along is 0.75: above the
|
||||
// 0.5 a straight taper would give.
|
||||
assert_eq!(p.at(0.25), (0.75, 0.75));
|
||||
assert_eq!(p.at(2.0), (0.0, 0.0));
|
||||
}
|
||||
|
||||
/// The width runs smoothly between points: it passes through them, has one slope either side
|
||||
/// of one, and never overshoots the widths its points set.
|
||||
#[test]
|
||||
fn profile_is_smooth_between_points() {
|
||||
let p = [(0.0, 1.0, 2.0), (0.5, 0.2, 2.0), (1.0, 1.0, 2.0)];
|
||||
assert!((WidthProfile::at_points(&p, 0.5).0 - 0.2).abs() < 1e-12);
|
||||
let slope = |t: f64| (WidthProfile::at_points(&p, t + 1e-6).0 - WidthProfile::at_points(&p, t - 1e-6).0) / 2e-6;
|
||||
// A local minimum at the middle point is flat there, so the two sides agree.
|
||||
assert!(slope(0.5).abs() < 1e-3, "{}", slope(0.5));
|
||||
for k in 0..=100 {
|
||||
let w = WidthProfile::at_points(&p, k as f64 / 100.0).0;
|
||||
assert!((0.2..=1.0).contains(&w), "{w} at {k}");
|
||||
}
|
||||
// The side with no points of its own stays flat.
|
||||
for k in 0..=100 {
|
||||
assert!((WidthProfile::at_points(&p, k as f64 / 100.0).1 - 2.0).abs() < 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
/// A run with only two points takes its secant as both end slopes, so it stays a straight
|
||||
/// taper on each side instead of becoming a smoothstep.
|
||||
#[test]
|
||||
fn profile_two_point_runs_are_linear() {
|
||||
let p = [(0.0, 0.0, 1.0), (1.0, 1.0, 0.0)];
|
||||
for k in 0..=100 {
|
||||
let t = k as f64 / 100.0;
|
||||
let (l, r) = WidthProfile::at_points(&p, t);
|
||||
assert!((l - t).abs() < 1e-12, "left {l} at {t}");
|
||||
assert!((r - (1.0 - t)).abs() < 1e-12, "right {r} at {t}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The lens's one-sided end slopes make it a parabola: at a quarter of the way it is 0.75,
|
||||
/// above the 0.5 a straight line between its points would give.
|
||||
#[test]
|
||||
fn profile_lens_is_convex() {
|
||||
let p = WidthProfile::lens();
|
||||
assert!((p.at(0.25).0 - 0.75).abs() < 1e-12);
|
||||
assert!((p.at(0.75).0 - 0.75).abs() < 1e-12);
|
||||
for k in 1..25 {
|
||||
let t = k as f64 / 100.0;
|
||||
assert!(p.at(t).0 > 2.0 * t, "{} at {t}", p.at(t).0);
|
||||
assert!(p.at(1.0 - t).0 > 2.0 * t, "{} at {}", p.at(1.0 - t).0, 1.0 - t);
|
||||
}
|
||||
}
|
||||
|
||||
/// A step still steps, and the smooth runs either side meet it flat.
|
||||
#[test]
|
||||
fn profile_steps_stay_steps() {
|
||||
let p = [(0.0, 1.0, 1.0), (0.5, 1.0, 1.0), (0.5, 2.0, 2.0), (1.0, 2.0, 2.0)];
|
||||
assert_eq!(WidthProfile::at_points(&p, 0.5), (1.0, 1.0));
|
||||
assert_eq!(WidthProfile::at_points(&p, 0.5 + 1e-6), (2.0, 2.0));
|
||||
assert_eq!(WidthProfile::at_points(&p, 0.5 - 1e-6), (1.0, 1.0));
|
||||
assert_eq!(WidthProfile::at_points(&p, 0.25), (1.0, 1.0));
|
||||
assert_eq!(WidthProfile::at_points(&p, 0.75), (2.0, 2.0));
|
||||
}
|
||||
|
||||
/// Moving a point only reshapes the segments beside it (local influence), not the whole path.
|
||||
#[test]
|
||||
fn profile_points_only_affect_the_curve_around_them() {
|
||||
// A monotone run, so no neighbouring slope is held flat by a local extremum.
|
||||
let a = [(0.0, 0.0, 0.0), (0.25, 0.3, 0.3), (0.5, 0.5, 0.5), (0.75, 0.7, 0.7), (1.0, 1.0, 1.0)];
|
||||
let mut b = a;
|
||||
b[3] = (0.75, 0.9, 0.9);
|
||||
// Moving p[3] changes the slopes at p[2] and p[3], so p[1] and p[4] also shape the
|
||||
// curve: nothing before p[1] may move.
|
||||
for k in 0..=24 {
|
||||
let t = k as f64 / 100.0;
|
||||
assert_eq!(WidthProfile::at_points(&a, t), WidthProfile::at_points(&b, t), "at {t}");
|
||||
}
|
||||
assert_ne!(WidthProfile::at_points(&a, 0.3), WidthProfile::at_points(&b, 0.3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profile_presets_round_trip_their_ids() {
|
||||
for p in WidthProfile::PRESETS {
|
||||
|
||||
+36
-8
@@ -46,7 +46,7 @@ impl Document {
|
||||
return (0, 0);
|
||||
}
|
||||
let own = size_of::<Document>() + ALLOC + ARC;
|
||||
let mut w = Walk { docs: [self, other], seen: [None, None], bytes: [own, own] };
|
||||
let mut w = Walk { docs: [self, other], seen: [None, None], bytes: [own, own], appearances: Default::default() };
|
||||
w.list(&self.layers, &other.layers);
|
||||
for i in 0..self.symbols.len().max(other.symbols.len()) {
|
||||
match (self.symbols.get(i), other.symbols.get(i)) {
|
||||
@@ -134,6 +134,9 @@ struct Walk<'a> {
|
||||
seen: [Option<HashSet<usize>>; 2],
|
||||
/// For each side, what it keeps that the other doesn't.
|
||||
bytes: [usize; 2],
|
||||
/// For each side, the appearances counted so far (by address): objects that share one (a
|
||||
/// graphic style applied to them all) count it once.
|
||||
appearances: [HashSet<usize>; 2],
|
||||
}
|
||||
|
||||
impl Walk<'_> {
|
||||
@@ -174,8 +177,9 @@ impl Walk<'_> {
|
||||
self.alone(1, b);
|
||||
return;
|
||||
}
|
||||
self.bytes[0] += node_bytes(a, Some(b));
|
||||
self.bytes[1] += node_bytes(b, Some(a));
|
||||
let [ca, cb] = &mut self.appearances;
|
||||
self.bytes[0] += node_bytes(a, Some(b), ca);
|
||||
self.bytes[1] += node_bytes(b, Some(a), cb);
|
||||
match (a.children(), b.children()) {
|
||||
(Some(x), Some(y)) => self.list(x, y),
|
||||
(x, y) => {
|
||||
@@ -198,8 +202,8 @@ impl Walk<'_> {
|
||||
if self.other_holds(side, n) {
|
||||
return;
|
||||
}
|
||||
if let Some(b) = self.bytes.get_mut(side) {
|
||||
*b += node_bytes(n, None);
|
||||
if let (Some(b), Some(counted)) = (self.bytes.get_mut(side), self.appearances.get_mut(side)) {
|
||||
*b += node_bytes(n, None, counted);
|
||||
}
|
||||
for c in n.children().into_iter().flatten() {
|
||||
self.alone(side, c);
|
||||
@@ -242,14 +246,15 @@ impl Walk<'_> {
|
||||
}
|
||||
|
||||
/// One node's own heap: the node, and what it owns besides its children and mask art (counted
|
||||
/// as nodes of their own). Its appearance counts unless `other`, its other version, shares it.
|
||||
fn node_bytes(n: &Node, other: Option<&Node>) -> usize {
|
||||
/// as nodes of their own). Its appearance counts unless `other`, its other version, shares it, or
|
||||
/// another node of the same side shares it and was counted already (its address is in `counted`).
|
||||
fn node_bytes(n: &Node, other: Option<&Node>, counted: &mut HashSet<usize>) -> usize {
|
||||
fn boxed<T>(b: &Option<Box<T>>) -> usize {
|
||||
b.as_ref().map_or(0, |_| ALLOC + size_of::<T>())
|
||||
}
|
||||
let mut b = ALLOC + ARC + size_of::<Node>();
|
||||
b += n.name.as_ref().map_or(0, String::capacity);
|
||||
if !other.is_some_and(|m| Shared::ptr_eq(&m.appearance, &n.appearance)) {
|
||||
if !other.is_some_and(|m| Shared::ptr_eq(&m.appearance, &n.appearance)) && counted.insert(&*n.appearance as *const Appearance as usize) {
|
||||
b += ALLOC + ARC + appearance_heap_bytes(&n.appearance);
|
||||
}
|
||||
if let Some(e) = n.extras_box() {
|
||||
@@ -447,6 +452,29 @@ mod tests {
|
||||
assert!(added > kept + 3 * 300, "{added} for three new rectangles");
|
||||
}
|
||||
|
||||
/// Objects given one shared appearance (a graphic style applied to them all) count it once.
|
||||
#[test]
|
||||
fn a_shared_appearance_counts_once() {
|
||||
let n = 100;
|
||||
let before = doc_with_rects(n);
|
||||
let ids: Vec<_> = before.layers[0].children().unwrap().iter().map(|c| c.id).collect();
|
||||
let ap = Appearance::basic(Paint::None, Paint::solid(vectorcraft_color::Color::BLACK), 2.0);
|
||||
let restyled = |each: bool| {
|
||||
let mut d = before.clone();
|
||||
let one = Shared::new(ap.clone());
|
||||
for id in &ids {
|
||||
d.node_mut(*id).unwrap().appearance = if each { Shared::new(ap.clone()) } else { one.clone() };
|
||||
}
|
||||
d
|
||||
};
|
||||
let (shared, copies) = (restyled(false), restyled(true));
|
||||
let (kept, added) = before.retained_bytes_each(&shared);
|
||||
assert_eq!(shared.retained_bytes_each(&before), (added, kept));
|
||||
let (kept_copies, added_copies) = before.retained_bytes_each(&copies);
|
||||
assert_eq!(kept, kept_copies, "the old version keeps the same either way");
|
||||
assert_eq!(added_copies - added, (n - 1) * (ALLOC + ARC + appearance_heap_bytes(&ap)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_resource_counts_only_when_changed() {
|
||||
let before = doc_with_rects(1);
|
||||
|
||||
+19
-10
@@ -80,10 +80,8 @@ fn head_ends(st: &StrokeLayer, bp: &BezPath) -> Vec<(Point, Vec2, bool)> {
|
||||
/// at `t`): the profile at that fraction of the subpath's length, or wider where an end of the
|
||||
/// segment is a corner. The stroke draws the sides there straight to the corner's join and inner
|
||||
/// meeting point, which can lie outside the exact width all along the segment.
|
||||
fn profile_at(profile: &WidthProfile, segs: &[PathSeg], closed: bool, i: usize, t: f64) -> (f64, f64) {
|
||||
fn profile_at(profile: &WidthProfile, segs: &[PathSeg], lens: &[f64], total: f64, closed: bool, i: usize, t: f64) -> (f64, f64) {
|
||||
let n = segs.len();
|
||||
let lens: Vec<f64> = segs.iter().map(|s| s.arclen(ARCLEN_ACCURACY)).collect();
|
||||
let total: f64 = lens.iter().sum::<f64>().max(1e-12);
|
||||
let before = lens[..i].iter().sum::<f64>();
|
||||
let mut w = profile.at((before + segs[i].subsegment(0.0..t).arclen(ARCLEN_ACCURACY)) / total);
|
||||
let corner = |a: usize, b: usize| (closed || a < b) && kink(&segs[a], &segs[b]);
|
||||
@@ -306,7 +304,8 @@ impl StrokeLayer {
|
||||
|
||||
/// Does this stroke along `bp` (filled with `rule`) paint within `tol` of `p`? Inside and
|
||||
/// outside strokes of closed paths count on their own side only (and within `tol` of the
|
||||
/// path); a width profile counts at its width where `p` is nearest.
|
||||
/// path); a width profile counts at the width of every segment that can reach `p`, as the
|
||||
/// wider side of a farther one can paint past the nearest.
|
||||
pub fn hits(&self, bp: &BezPath, rule: FillRule, p: Point, tol: f64) -> bool {
|
||||
let reach = self.reach() + tol;
|
||||
if self.width.is_nan() || self.width <= 0.0 || !bp.bounding_box().inflate(reach, reach).contains(p) {
|
||||
@@ -330,17 +329,27 @@ impl StrokeLayer {
|
||||
let segs = drawn(&els[r]);
|
||||
let (half, region) = self.body(closed);
|
||||
let full = half * self.profile_max() * dash_caps.unwrap_or(1.0);
|
||||
// The nearest point on this subpath.
|
||||
let near = segs.iter().enumerate().map(|(i, s)| (i, s.nearest(p, 1e-9))).min_by(|a, b| a.1.distance_sq.total_cmp(&b.1.distance_sq));
|
||||
if let Some((i, nr)) = near
|
||||
&& nr.distance_sq.sqrt() <= full + tol
|
||||
{
|
||||
// Every segment near enough to reach `p`: with a width profile the wider side of a
|
||||
// farther segment can paint past the nearest one, so the nearest alone is not enough.
|
||||
let near = segs.iter().enumerate().filter_map(|(i, s)| {
|
||||
let nr = s.nearest(p, 1e-9);
|
||||
(nr.distance_sq.sqrt() <= full + tol).then_some((i, nr))
|
||||
});
|
||||
// The lengths are only needed when a profiled candidate reaches `p`; compute them
|
||||
// once for the whole subpath rather than once per candidate.
|
||||
let mut lengths: Option<(Vec<f64>, f64)> = None;
|
||||
for (i, nr) in near {
|
||||
let d = nr.distance_sq.sqrt();
|
||||
let factor = if dash_caps.is_some() {
|
||||
full / half.max(1e-12)
|
||||
} else {
|
||||
self.profile.as_ref().map_or(1.0, |pr| {
|
||||
let (l, rt) = profile_at(pr, &segs, closed, i, nr.t);
|
||||
let (lens, total) = lengths.get_or_insert_with(|| {
|
||||
let lens: Vec<f64> = segs.iter().map(|s| s.arclen(ARCLEN_ACCURACY)).collect();
|
||||
let total = lens.iter().sum::<f64>().max(1e-12);
|
||||
(lens, total)
|
||||
});
|
||||
let (l, rt) = profile_at(pr, &segs, lens, *total, closed, i, nr.t);
|
||||
// Nearest to a segment's end, `p` is beside a join or past a cap, which
|
||||
// spans the widths of both sides.
|
||||
let at_end = nr.t <= 1e-9 || nr.t >= 1.0 - 1e-9;
|
||||
|
||||
@@ -176,29 +176,29 @@ pub fn effect_catalog() -> Vec<EffectInfo> {
|
||||
REVOLVE,
|
||||
"Revolve…",
|
||||
&["Effect", "3D and Materials"],
|
||||
"{angle: degrees (0..360, 360), offset: pt (0..100000, 0), edge: left|right, rotationX: degrees (0), rotationY: degrees (0), rotationZ: degrees (0), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), segments: integer (8..128, 64), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth), cap: bool (true, close circular ends of open profiles; partial-sweep cut sides stay open), expandVisibleOnly: bool (true, trim covered opaque solid surfaces only on Expand Appearance)}. Shaded surfaces use smooth vector gradients automatically; intersecting profiles use approximate painter visibility.",
|
||||
json!({"angle":360.0,"offset":0.0,"edge":"left","rotationX":0.0,"rotationY":0.0,"rotationZ":0.0,"perspectiveAngle":0.0,"segments":64,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0,"cap":true,"expandVisibleOnly":true}),
|
||||
"{angle: degrees (0..360, 360), offset: pt (0..100000, 0), edge: left|right, rotationX: degrees (0), rotationY: degrees (0), rotationZ: degrees (0), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), segments: integer (8..128, 64), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth), shadow: bool (false; the first light casts a hard-edged shadow, black at 50% times the art's opacity), shadowPosition: behind|below (behind; behind: on a plane square to the view, below: on the floor under the object, turned with it), shadowDistance: % (0..100, 0; how far the plane is from the object, as a share of its largest size), shadowBounds: % (10..400, 100; how far the plane reaches: at 100 it is four times the object's largest size across), cap: bool (true, close circular ends of open profiles; partial-sweep cut sides stay open), expandVisibleOnly: bool (true, trim covered opaque solid surfaces only on Expand Appearance)}. Shaded surfaces use smooth vector gradients automatically; intersecting profiles use approximate painter visibility.",
|
||||
json!({"angle":360.0,"offset":0.0,"edge":"left","rotationX":0.0,"rotationY":0.0,"rotationZ":0.0,"perspectiveAngle":0.0,"segments":64,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0,"shadow":false,"shadowPosition":"behind","shadowDistance":0.0,"shadowBounds":100.0,"cap":true,"expandVisibleOnly":true}),
|
||||
),
|
||||
g(
|
||||
EXTRUDE,
|
||||
"Extrude & Bevel…",
|
||||
&["Effect", "3D and Materials"],
|
||||
"{depth: pt (0..2000, 50), twist: degrees (0..360, 0; the back turned against the front), taper: % (1..100, 100; the back's size), cap: bool (true: solid; false: hollow), bevel: none|classic|rounded|cove|step|ogee (none), bevelWidth: pt (0..1000, 4), bevelHeight: pt (0..1000, 4), bevelRepeat: integer (1..32, 1), bevelSpace: % (0..90, 0; flat between repeats), bevelInside: bool (false: added around the shape; true: carved into it), bevelBothSides: bool (false), rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth)}. The front face is the art, extruded backwards about the centre of its bounds; painted with the top fill (else the top stroke).",
|
||||
json!({"depth":50.0,"twist":0.0,"taper":100.0,"cap":true,"bevel":"none","bevelWidth":4.0,"bevelHeight":4.0,"bevelRepeat":1,"bevelSpace":0.0,"bevelInside":false,"bevelBothSides":false,"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0}),
|
||||
"{depth: pt (0..2000, 50), twist: degrees (0..360, 0; the back turned against the front), taper: % (1..100, 100; the back's size), cap: bool (true: solid; false: hollow), bevel: none|classic|rounded|cove|step|ogee (none), bevelWidth: pt (0..1000, 4), bevelHeight: pt (0..1000, 4), bevelRepeat: integer (1..32, 1), bevelSpace: % (0..90, 0; flat between repeats), bevelInside: bool (false: added around the shape; true: carved into it), bevelBothSides: bool (false), rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth), shadow: bool (false; the first light casts a hard-edged shadow, black at 50% times the art's opacity), shadowPosition: behind|below (behind; behind: on a plane square to the view, below: on the floor under the object, turned with it), shadowDistance: % (0..100, 0; how far the plane is from the object, as a share of its largest size), shadowBounds: % (10..400, 100; how far the plane reaches: at 100 it is four times the object's largest size across)}. The front face is the art, extruded backwards about the centre of its bounds; painted with the top fill (else the top stroke).",
|
||||
json!({"depth":50.0,"twist":0.0,"taper":100.0,"cap":true,"bevel":"none","bevelWidth":4.0,"bevelHeight":4.0,"bevelRepeat":1,"bevelSpace":0.0,"bevelInside":false,"bevelBothSides":false,"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0,"shadow":false,"shadowPosition":"behind","shadowDistance":0.0,"shadowBounds":100.0}),
|
||||
),
|
||||
g(
|
||||
INFLATE,
|
||||
"Inflate…",
|
||||
&["Effect", "3D and Materials"],
|
||||
"{depth: pt (0..2000, 0; flat depth between front and back), twist: degrees (0..360, 0; the back turned against the front), taper: % (1..100, 100; the back's size), cap: bool (true: solid; false: no back, a shell open behind), volume: % (0..100, 50; 50 rounds the edge like a half sphere), bothSides: bool (false: flat back), rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth)}. Painted with the top fill (else the top stroke).",
|
||||
json!({"depth":0.0,"twist":0.0,"taper":100.0,"cap":true,"volume":50.0,"bothSides":false,"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0}),
|
||||
"{depth: pt (0..2000, 0; flat depth between front and back), twist: degrees (0..360, 0; the back turned against the front), taper: % (1..100, 100; the back's size), cap: bool (true: solid; false: no back, a shell open behind), volume: % (0..100, 50; 50 rounds the edge like a half sphere), bothSides: bool (false: flat back), rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth), shadow: bool (false; the first light casts a hard-edged shadow, black at 50% times the art's opacity), shadowPosition: behind|below (behind; behind: on a plane square to the view, below: on the floor under the object, turned with it), shadowDistance: % (0..100, 0; how far the plane is from the object, as a share of its largest size), shadowBounds: % (10..400, 100; how far the plane reaches: at 100 it is four times the object's largest size across)}. Painted with the top fill (else the top stroke).",
|
||||
json!({"depth":0.0,"twist":0.0,"taper":100.0,"cap":true,"volume":50.0,"bothSides":false,"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0,"shadow":false,"shadowPosition":"behind","shadowDistance":0.0,"shadowBounds":100.0}),
|
||||
),
|
||||
g(
|
||||
ROTATE,
|
||||
"Rotate…",
|
||||
&["Effect", "3D and Materials"],
|
||||
"{rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth)}. Turns the flat art in 3D about the centre of its bounds; every fill and stroke keeps its paint, shaded as one plane.",
|
||||
json!({"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0}),
|
||||
"{rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth), shadow: bool (false; the first light casts a hard-edged shadow, black at 50% times the art's opacity), shadowPosition: behind|below (behind; behind: on a plane square to the view, below: on the floor under the object, turned with it), shadowDistance: % (0..100, 0; how far the plane is from the object, as a share of its largest size), shadowBounds: % (10..400, 100; how far the plane reaches: at 100 it is four times the object's largest size across)}. Turns the flat art in 3D about the centre of its bounds; every fill and stroke keeps its paint, shaded as one plane.",
|
||||
json!({"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0,"shadow":false,"shadowPosition":"behind","shadowDistance":0.0,"shadowBounds":100.0}),
|
||||
),
|
||||
g(
|
||||
CLASSIC_EXTRUDE,
|
||||
|
||||
@@ -112,7 +112,9 @@ pub fn validate_revolve_in(doc: Option<&Document>, n: &Node, p: &Value) -> Resul
|
||||
let b = union_bounds(&shapes).ok_or(EMPTY)?;
|
||||
let options = shared_options(&shapes, revolve_options(p))?;
|
||||
let mut copies = 0;
|
||||
return shapes.iter().try_for_each(|s| check_surface(&s.path, b, options, surface_item(&s.items), &mut copies));
|
||||
shapes.iter().try_for_each(|s| check_surface(&s.path, b, options, surface_item(&s.items), &mut copies))?;
|
||||
let casting = shapes.iter().filter(|s| surface_item(&s.items).is_some_and(|i| paint_of(i).1 * s.opacity > 0.0));
|
||||
return check_shadow(p, casting.map(|s| &s.path), b, options);
|
||||
}
|
||||
let (path, _) = object_profile(n, "Revolve")?;
|
||||
if !has_two_anchors(&path) {
|
||||
@@ -121,7 +123,18 @@ pub fn validate_revolve_in(doc: Option<&Document>, n: &Node, p: &Value) -> Resul
|
||||
let path = geometry(&path, before, &GeomContext::of(n));
|
||||
let b = path.bounds().ok_or("Revolve requires a shape with at least two anchors")?;
|
||||
let paints = solid_paints(n);
|
||||
check_surface(&path, b, revolve_options(p), surface_item(&paints), &mut 0)
|
||||
check_surface(&path, b, revolve_options(p), surface_item(&paints), &mut 0)?;
|
||||
check_shadow(p, [&path], b, revolve_options(p))
|
||||
}
|
||||
|
||||
/// With the shadow of Revolve parameters `p` on, an error when the surfaces of `paths` it is cast
|
||||
/// from, about the axis of `b`, are past the limits together.
|
||||
fn check_shadow<'a>(p: &Value, paths: impl IntoIterator<Item = &'a PathData>, b: Rect, options: Revolve) -> Result<(), String> {
|
||||
if crate::solids::shadow::options(p).is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
let mut caster = vectorcraft_three_d::ShadowCaster::new();
|
||||
paths.into_iter().try_for_each(|path| caster.revolve(path, vectorcraft_geom::FillRule::NonZero, b, options).map(|_| ()))
|
||||
}
|
||||
|
||||
/// [`validate_revolve`] for a Revolve with parameters `p` at index `index` of the effects of
|
||||
@@ -144,14 +157,16 @@ pub(crate) fn validate_item_revolve(n: &Node, p: &Value, target: usize, index: u
|
||||
for (i, item) in n.appearance.items.iter().enumerate() {
|
||||
let effects = item.effects();
|
||||
let at = if i == target { Some(index) } else { effects.iter().position(|e| e.visible && e.id == REVOLVE) };
|
||||
let options = match at.and_then(|at| effects.get(at)) {
|
||||
_ if i == target => revolve_options(p),
|
||||
Some(fx) if crate::paints(item) => revolve_options(&merged_params(REVOLVE, &fx.params)),
|
||||
let params = match at.and_then(|at| effects.get(at)) {
|
||||
_ if i == target => p.clone(),
|
||||
Some(fx) if crate::paints(item) => merged_params(REVOLVE, &fx.params),
|
||||
_ => continue,
|
||||
};
|
||||
let options = revolve_options(¶ms);
|
||||
let path = geometry(&path, at.and_then(|at| effects.get(..at)).unwrap_or(effects), &ctx.item(item));
|
||||
let b = path.bounds().ok_or(EMPTY)?;
|
||||
check_surface(&path, b, options, crate::paints(item).then_some(item), &mut copies)?;
|
||||
check_shadow(¶ms, [&path], b, options)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -189,9 +204,10 @@ fn surface_item(items: &[AppearanceItem]) -> Option<&AppearanceItem> {
|
||||
items.iter().rev().find(|i| matches!(i, AppearanceItem::Fill(_))).or_else(|| items.last())
|
||||
}
|
||||
|
||||
/// Check Revolve's parameter values `p` alone: its lights, `edge` and `cap`.
|
||||
/// Check Revolve's parameter values `p` alone: its lights, shadow, `edge` and `cap`.
|
||||
pub(crate) fn validate_revolve_params(p: &Value) -> Result<(), String> {
|
||||
crate::lighting::validate(p)?;
|
||||
crate::solids::shadow::validate(p)?;
|
||||
if p.get("edge").is_some_and(|v| v.as_str() != Some("left") && v.as_str() != Some("right")) {
|
||||
return Err("Revolve edge must be left or right".into());
|
||||
}
|
||||
@@ -205,6 +221,43 @@ fn geometry(path: &PathData, fx: &[Effect], ctx: &GeomContext) -> PathData {
|
||||
apply_geometry_with(fx, path, path.bounds().unwrap_or(Rect::ZERO), ctx)
|
||||
}
|
||||
|
||||
/// The shadow of Revolve `fx` cast by the surface of `path` in `item`'s paint about the axis of
|
||||
/// `b`, revolved with `options`, as dark as `item` is opaque and in its blending mode; `None` with
|
||||
/// the shadow off, when `item` is fully transparent or when it casts nothing, and an error when
|
||||
/// the surface it is cast from is past the limits.
|
||||
fn shadow_of(id: NodeId, fx: &Effect, path: &PathData, item: &AppearanceItem, b: Rect, options: Revolve) -> Result<Option<Node>, String> {
|
||||
let Some(settings) = crate::solids::shadow::options(&merged_params(REVOLVE, &fx.params)) else { return Ok(None) };
|
||||
let (_, opacity, blend) = paint_of(item);
|
||||
if opacity <= 0.0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut caster = vectorcraft_three_d::ShadowCaster::new();
|
||||
caster.revolve(path, vectorcraft_geom::FillRule::NonZero, b, options)?;
|
||||
Ok(crate::solids::shadow::art(id, &caster, settings, crate::solids::shadow::Ink { opacity, blend }))
|
||||
}
|
||||
|
||||
/// The shadow of group Revolve `fx` cast by the surfaces of the members `shapes` that have one,
|
||||
/// about the axis of `b`, on one plane; each member's part is as dark as its surface is opaque,
|
||||
/// in its paint's blending mode, and its opacity mask and blending mode apply to that part
|
||||
/// ([`crate::solids::shadow::scoped_art`]). An error when the surfaces it is cast from are past the
|
||||
/// limits.
|
||||
fn group_shadow(id: NodeId, fx: &Effect, shapes: &[Profile], b: Rect, options: Revolve) -> Result<Vec<Arc<Node>>, String> {
|
||||
let Some(settings) = crate::solids::shadow::options(&merged_params(REVOLVE, &fx.params)) else { return Ok(vec![]) };
|
||||
let mut caster = vectorcraft_three_d::ShadowCaster::new();
|
||||
let mut cast = vec![];
|
||||
for s in shapes {
|
||||
let Some((_, opacity, blend)) = surface_item(&s.items).map(paint_of) else { continue };
|
||||
// A member whose surface is fully transparent casts nothing.
|
||||
let ink = crate::solids::shadow::Ink { opacity: opacity * s.opacity, blend };
|
||||
if ink.opacity <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let faces = caster.revolve(&s.path, vectorcraft_geom::FillRule::NonZero, b, options)?;
|
||||
cast.push((s.scopes.as_slice(), ink, faces));
|
||||
}
|
||||
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
|
||||
/// the bytes of the paint copies of every surface of the object.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
@@ -445,7 +498,13 @@ fn art(doc: Option<&Document>, n: &Node, expand: bool) -> Option<Node> {
|
||||
let paints = solid_paints(n);
|
||||
let item = surface_item(&paints)?;
|
||||
let expand = expand && !n.appearance.effects.iter().skip(index + 1).any(|e| e.visible && is_geometry(&e.id));
|
||||
children.push(Arc::new(object_surface(n, &path, fx, item, expand, &mut copies)?));
|
||||
let surface = object_surface(n, &path, fx, item, expand, &mut copies)?;
|
||||
if let Some(b) = path.bounds()
|
||||
&& let Some(shadow) = shadow_of(n.id, fx, &path, item, b, revolve_options(&merged_params(REVOLVE, &fx.params))).ok()?
|
||||
{
|
||||
children.push(Arc::new(shadow));
|
||||
}
|
||||
children.push(Arc::new(surface));
|
||||
// Later geometry acts on the projected art as a whole through container evaluation.
|
||||
remaining = n
|
||||
.appearance
|
||||
@@ -458,6 +517,8 @@ fn art(doc: Option<&Document>, n: &Node, expand: bool) -> Option<Node> {
|
||||
} else {
|
||||
let path = geometry(&path, &n.appearance.effects, &ctx);
|
||||
remaining.retain(|e| !is_geometry(&e.id));
|
||||
// Every item's shadow goes behind every item's art.
|
||||
let mut shadows = vec![];
|
||||
for item in n.appearance.items.iter().filter(|i| crate::paints(i)) {
|
||||
if let Some(index) = item.effects().iter().position(|e| e.visible && e.id == REVOLVE) {
|
||||
let fx = item.effects().get(index)?;
|
||||
@@ -465,11 +526,18 @@ fn art(doc: Option<&Document>, n: &Node, expand: bool) -> Option<Node> {
|
||||
let expand = expand && !item.effects().iter().skip(index + 1).any(|e| e.visible && is_geometry(&e.id));
|
||||
let mut art = object_surface(n, &p, fx, item, expand, &mut copies)?;
|
||||
art.appearance.effects = item.effects().iter().skip(index + 1).filter(|e| e.id != REVOLVE).cloned().collect();
|
||||
if let Some(b) = p.bounds()
|
||||
&& let Some(mut shadow) = shadow_of(n.id, fx, &p, item, b, revolve_options(&merged_params(REVOLVE, &fx.params))).ok()?
|
||||
{
|
||||
shadow.appearance.effects = art.appearance.effects.clone();
|
||||
shadows.push(Arc::new(shadow));
|
||||
}
|
||||
children.push(Arc::new(art));
|
||||
} else {
|
||||
children.push(Arc::new(Node::path(n.id, path.clone(), Appearance { items: vec![item.clone()], ..Default::default() })));
|
||||
}
|
||||
}
|
||||
children.splice(0..0, shadows);
|
||||
}
|
||||
let mut art = n.clone();
|
||||
art.kind = NodeKind::Group { children, clip: false };
|
||||
@@ -704,6 +772,9 @@ fn group_art(n: &Node, expand: bool) -> Option<Node> {
|
||||
scoped(n.id, runs.collect(), 0)
|
||||
}
|
||||
};
|
||||
// One shadow for every member's surface, behind them all.
|
||||
let mut children = children;
|
||||
children.splice(0..0, group_shadow(n.id, fx, &shapes, b, options).ok()?);
|
||||
let remaining =
|
||||
n.appearance.effects.iter().enumerate().filter(|(i, e)| *i != index && (!is_geometry(&e.id) || *i > index)).map(|(_, e)| e.clone()).collect();
|
||||
let mut art = n.clone();
|
||||
|
||||
+265
-35
@@ -20,6 +20,8 @@ pub use classic::{CLASSIC_EXTRUDE, SHADING_COLORS, classic_extrude_options, clas
|
||||
mod source;
|
||||
pub(crate) use source::{resolve, resolve_for};
|
||||
|
||||
pub(crate) mod shadow;
|
||||
|
||||
pub const EXTRUDE: &str = "threeD.extrude";
|
||||
pub const INFLATE: &str = "threeD.inflate";
|
||||
pub const ROTATE: &str = "threeD.rotate";
|
||||
@@ -192,6 +194,7 @@ fn validate_solid_params(id: &str, p: &Value) -> Result<(), String> {
|
||||
classic::validate(p)?;
|
||||
}
|
||||
crate::lighting::validate(p)?;
|
||||
shadow::validate(p)?;
|
||||
if let Some(v) = p.get("bevel")
|
||||
&& !v.as_str().is_some_and(|s| s == "none" || BevelShape::parse(s).is_some())
|
||||
{
|
||||
@@ -214,17 +217,31 @@ pub fn validate_solid(n: &Node, id: &str, p: &Value) -> Result<(), String> {
|
||||
/// [`validate_solid`] with the symbols of `doc`: a symbol instance, a blend and a clipping group
|
||||
/// build from the art they show ([`resolve`]).
|
||||
pub fn validate_solid_in(doc: Option<&Document>, n: &Node, id: &str, p: &Value) -> Result<(), String> {
|
||||
validate_solid_for(doc, n, id, p, None)
|
||||
}
|
||||
|
||||
/// [`validate_solid_in`] for the effect on the object (`item` `None`) or on its appearance item
|
||||
/// `item`, whose shadow is cast by that item's solid alone.
|
||||
fn validate_solid_for(doc: Option<&Document>, n: &Node, id: &str, p: &Value, item: Option<usize>) -> Result<(), String> {
|
||||
validate_solid_params(id, p)?;
|
||||
let label = label_of(id);
|
||||
let n = &*source::resolve_for(doc, n, label, id == ROTATE)?;
|
||||
if is_container(n) {
|
||||
let shapes = members(n, label)?;
|
||||
let b = union_bounds(&shapes).ok_or_else(|| format!("{label} requires a group holding a shape with at least two anchors"))?;
|
||||
return shapes.iter().try_for_each(|s| facets(id, &s.path, s.rule, b, p).map(|_| ()));
|
||||
shapes.iter().try_for_each(|s| facets(id, &s.path, s.rule, b, p).map(|_| ()))?;
|
||||
return cast_shadow(id, p, &shapes).map(|_| ());
|
||||
}
|
||||
let (path, rule) = object_profile(n, label)?;
|
||||
let b = path.bounds().ok_or_else(|| format!("{label} requires a shape with at least two anchors"))?;
|
||||
facets(id, &path, rule, b, p).map(|_| ())
|
||||
facets(id, &path, rule, b, p)?;
|
||||
// With its shadow on, the shadow of the object's solids (or of the item's) must fit too.
|
||||
let items = match item.and_then(|i| n.appearance.items.get(i)) {
|
||||
Some(item) => vec![item.clone()],
|
||||
None => solid_paints(n),
|
||||
};
|
||||
let shapes = [Profile { path, rule, items, opacity: 1.0, scopes: vec![] }];
|
||||
cast_shadow(id, p, &shapes).map(|_| ())
|
||||
}
|
||||
|
||||
/// [`profile`], or the error a command reports for object `n`: type with no outlines to build from
|
||||
@@ -297,7 +314,7 @@ fn masked(id: NodeId, surfaces: Vec<(&[Scope], Arc<Node>)>) -> Vec<Arc<Node>> {
|
||||
}
|
||||
|
||||
/// Does any of `shapes` sit in a scope with an opacity mask or a blending mode other than Normal?
|
||||
fn has_masks(shapes: &[Profile]) -> bool {
|
||||
pub(crate) fn has_masks(shapes: &[Profile]) -> bool {
|
||||
shapes.iter().any(|s| s.scopes.iter().any(composites))
|
||||
}
|
||||
|
||||
@@ -420,7 +437,8 @@ pub fn validate_three_d_at_in(doc: Option<&Document>, n: &Node, id: &str, p: &Va
|
||||
Err("A 3D effect on a group or layer goes on the group itself, not on its fill or stroke".into())
|
||||
}
|
||||
Some((item, index)) if id == crate::REVOLVE => crate::revolve::validate_item_revolve(n, p, item, index),
|
||||
_ => validate_three_d_in(doc, n, id, p),
|
||||
Some((item, _)) => validate_solid_for(doc, n, id, p, Some(item)),
|
||||
None => validate_three_d_in(doc, n, id, p),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -516,8 +534,8 @@ fn region(path: &PathData, rule: FillRule, item: &AppearanceItem) -> (PathData,
|
||||
}
|
||||
|
||||
/// One part of [`solid_parts`]: a fill or stroke, its shape's opacity, the region it paints and its
|
||||
/// fill rule, and its shape's scopes.
|
||||
type Part<'a> = (&'a AppearanceItem, f32, PathData, FillRule, &'a [Scope]);
|
||||
/// fill rule, its shape's scopes and the index of its shape.
|
||||
type Part<'a> = (&'a AppearanceItem, f32, PathData, FillRule, &'a [Scope], usize);
|
||||
|
||||
/// The parts Extrude & Bevel and Inflate build solids from: each shape's top fill and its strokes,
|
||||
/// a group's members from the back up, each with its shape's opacity, the region it paints and its
|
||||
@@ -526,18 +544,128 @@ type Part<'a> = (&'a AppearanceItem, f32, PathData, FillRule, &'a [Scope]);
|
||||
fn solid_parts(shapes: &[Profile], fallback: Rect) -> (Vec<Part<'_>>, Rect) {
|
||||
let parts: Vec<Part> = shapes
|
||||
.iter()
|
||||
.flat_map(|s| {
|
||||
.enumerate()
|
||||
.flat_map(|(member, s)| {
|
||||
let top_fill = s.items.iter().rposition(|i| matches!(i, AppearanceItem::Fill(_)));
|
||||
s.items.iter().enumerate().filter(move |(k, i)| Some(*k) == top_fill || matches!(i, AppearanceItem::Stroke(_))).map(|(_, i)| {
|
||||
s.items.iter().enumerate().filter(move |(k, i)| Some(*k) == top_fill || matches!(i, AppearanceItem::Stroke(_))).map(move |(_, i)| {
|
||||
let (area, rule) = region(&s.path, s.rule, i);
|
||||
(i, s.opacity, area, rule, s.scopes.as_slice())
|
||||
(i, s.opacity, area, rule, s.scopes.as_slice(), member)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let bounds = parts.iter().filter_map(|(_, _, area, _, _)| area.bounds()).reduce(|a, c| a.union(c)).unwrap_or(fallback);
|
||||
let bounds = parts.iter().filter_map(|(_, _, area, _, _, _)| area.bounds()).reduce(|a, c| a.union(c)).unwrap_or(fallback);
|
||||
(parts, bounds)
|
||||
}
|
||||
|
||||
/// What a face of one of the solids [`art_for`] draws in one order was built from: the facet's
|
||||
/// depth, whether it is a side face and whether it is turned away from the viewer, the bounds of
|
||||
/// its polygon, the index of its shape, and whether its solid is that shape's fill or one of its
|
||||
/// strokes that covers the fill's whole edge.
|
||||
struct Placed {
|
||||
depth: f64,
|
||||
side: bool,
|
||||
away: bool,
|
||||
bounds: Rect,
|
||||
member: usize,
|
||||
paints: Paints,
|
||||
}
|
||||
|
||||
/// The part of its shape a face's solid is built from ([`Placed`]).
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Paints {
|
||||
Fill,
|
||||
/// A stroke whose solid holds the fill's whole edge and hides it: one with a width, without
|
||||
/// dashes, a width profile or a brush, painted with a solid color or a gradient without
|
||||
/// transparent stops, on a shape whose subpaths are all closed.
|
||||
Stroke,
|
||||
Other,
|
||||
}
|
||||
|
||||
impl Paints {
|
||||
/// What `item` of a shape is, the shape's subpaths all closed when `closed`.
|
||||
fn of(item: &AppearanceItem, closed: bool) -> Self {
|
||||
match item {
|
||||
AppearanceItem::Fill(_) => Paints::Fill,
|
||||
AppearanceItem::Stroke(s)
|
||||
if closed
|
||||
&& s.width > 0.0
|
||||
&& s.width.is_finite()
|
||||
&& s.dash.is_none()
|
||||
&& s.profile.is_none()
|
||||
&& s.brush.is_none()
|
||||
&& match &s.paint {
|
||||
Paint::Solid { .. } => true,
|
||||
Paint::Gradient(_) => crate::lighting::opaque_paint(&s.paint).1.is_none(),
|
||||
_ => false,
|
||||
} =>
|
||||
{
|
||||
Paints::Stroke
|
||||
}
|
||||
AppearanceItem::Stroke(_) => Paints::Other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Placed {
|
||||
/// The face of `f`, of shape `member`'s part `paints`.
|
||||
fn of(f: &Facet, member: usize, paints: Paints) -> Self {
|
||||
let mut points = f.contours.iter().flatten();
|
||||
let bounds = points.next().map_or(Rect::ZERO, |p| points.fold(Rect::from_points(*p, *p), |r, q| r.union_pt(*q)));
|
||||
Placed { depth: f.depth, side: f.side, away: f.away, bounds, member, paints }
|
||||
}
|
||||
}
|
||||
|
||||
/// The most bounds comparisons [`draw_order`] makes; the fill walls it has not reached by then keep
|
||||
/// their depths.
|
||||
const MAX_OVERLAP_CHECKS: usize = 4_000_000;
|
||||
|
||||
/// The order faces `faces` are drawn in, back to front, as indices into `faces`: by depth (faces
|
||||
/// at the same depth in the order given), except that with `sealed` (solids with caps) a side face
|
||||
/// of a shape's fill turned toward the viewer is drawn before each face of the same shape's
|
||||
/// covering strokes ([`Paints::Stroke`]), turned toward the viewer, whose bounds overlap its own.
|
||||
/// Such a stroke's closed solid holds the fill's walls, so they never show and can be drawn under
|
||||
/// any other face.
|
||||
fn draw_order(faces: &[Placed], sealed: bool) -> Vec<usize> {
|
||||
// Each face's depth to sort by, and whether it was moved before a stroke face at that depth.
|
||||
let mut keys: Vec<(f64, bool)> = faces.iter().map(|f| (f.depth, false)).collect();
|
||||
if sealed {
|
||||
// The stroke faces turned toward the viewer of each shape, nearest last.
|
||||
let mut strokes: Vec<Vec<&Placed>> = vec![];
|
||||
for f in faces.iter().filter(|f| f.paints == Paints::Stroke && !f.away) {
|
||||
if strokes.len() <= f.member {
|
||||
strokes.resize_with(f.member + 1, Vec::new);
|
||||
}
|
||||
strokes[f.member].push(f);
|
||||
}
|
||||
for list in &mut strokes {
|
||||
list.sort_by(|a, b| a.depth.total_cmp(&b.depth));
|
||||
}
|
||||
let overlap = |a: &Rect, b: &Rect| a.x0 < b.x1 && b.x0 < a.x1 && a.y0 < b.y1 && b.y0 < a.y1;
|
||||
let mut checks = 0usize;
|
||||
for (f, key) in faces.iter().zip(&mut keys) {
|
||||
if f.paints != Paints::Fill || !f.side || f.away {
|
||||
continue;
|
||||
}
|
||||
let Some(list) = strokes.get(f.member).filter(|l| !l.is_empty()) else { continue };
|
||||
for s in list.iter().take_while(|s| s.depth <= f.depth) {
|
||||
if checks >= MAX_OVERLAP_CHECKS {
|
||||
break;
|
||||
}
|
||||
checks += 1;
|
||||
if overlap(&s.bounds, &f.bounds) {
|
||||
*key = (s.depth, true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut order: Vec<usize> = (0..faces.len()).collect();
|
||||
// A stable sort: faces at the same depth keep the order of the appearance, and a fill's side
|
||||
// face moved to a stroke face's depth goes before it.
|
||||
order.sort_by(|&a, &b| keys[a].0.total_cmp(&keys[b].0).then(keys[b].1.cmp(&keys[a].1)));
|
||||
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]) {
|
||||
@@ -551,16 +679,100 @@ pub(crate) fn reshape_members(n: &Node, before: &[Effect], shapes: &mut [Profile
|
||||
}
|
||||
}
|
||||
|
||||
/// A 3D effect's shadow and its surfaces ([`art_for`]).
|
||||
type ShadowAndSurfaces = (Vec<Arc<Node>>, Vec<Arc<Node>>);
|
||||
|
||||
/// The 3D art of one effect `fx` on `path` (already through the effects before it).
|
||||
/// Rotate turns every fill and stroke, each in its own paint. Extrude & Bevel and Inflate build a
|
||||
/// solid from the path for the top visible fill and one from the outline of each visible stroke,
|
||||
/// each in its own paint. When every one is opaque with normal blending and no colored light makes
|
||||
/// a surface one composite or masks it, their faces are drawn in one back-to-front order (where
|
||||
/// depths tie, as for the caps turned toward the viewer, in the order of the appearance);
|
||||
/// depths tie, as for the caps turned toward the viewer, in the order of the appearance, and with
|
||||
/// caps, a shape's fill walls before the faces of its covering strokes that overlap them:
|
||||
/// [`draw_order`]);
|
||||
/// otherwise each solid is a surface of its own, in that order. A member's opacity mask and
|
||||
/// blending mode, and those of a group it is in, apply to the surfaces of the members it holds
|
||||
/// ([`masked`]).
|
||||
fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>> {
|
||||
/// ([`masked`]). The art comes in two parts: the effect's shadow ([`cast_shadow`], none with it
|
||||
/// off), to be drawn behind every surface of the object, and the surfaces.
|
||||
fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<ShadowAndSurfaces> {
|
||||
let art = surfaces_for(n, fx, shapes)?;
|
||||
// A shadow past the limits draws the source path, as a solid past them does; commands report
|
||||
// the error ([`validate_solid_in`]).
|
||||
let shadow = match cast_shadow(&fx.id, &crate::merged_params(&fx.id, &fx.params), shapes).ok()? {
|
||||
Some((caster, settings, cast)) => shadow::scoped_art(n.id, &caster, settings, cast, has_masks(shapes)),
|
||||
None => vec![],
|
||||
};
|
||||
Some((shadow, art))
|
||||
}
|
||||
|
||||
/// What the shadow of the solids [`surfaces_for`] builds for effect `id` with parameters `p` on
|
||||
/// `shapes` is cast from, on one plane: the faces of every solid, the shadow's settings and the
|
||||
/// faces of each solid of visible art with its member's scopes and ink. `None` with the shadow
|
||||
/// off, for Extrude & Bevel (Classic) or with no shapes. A stroke's solid that is not built (past
|
||||
/// the limits) casts nothing, as it draws nothing; any other solid that is not built is an error.
|
||||
/// When a member's opacity mask or blending mode applies to its solids ([`masked`]), it applies to
|
||||
/// the part of the shadow they cast too ([`shadow::scoped_art`]).
|
||||
fn cast_shadow<'a>(id: &str, p: &Value, shapes: &'a [Profile]) -> Result<Option<Cast<'a>>, String> {
|
||||
if id == CLASSIC_EXTRUDE {
|
||||
return Ok(None);
|
||||
}
|
||||
let (Some(settings), Some(b)) = (shadow::options(p), union_bounds(shapes)) else { return Ok(None) };
|
||||
let view = view_options(p);
|
||||
let mut caster = vectorcraft_three_d::ShadowCaster::new();
|
||||
// The faces each solid of visible art added, with its member's scopes and its ink (opacity
|
||||
// and blending mode), in order. A fully transparent fill or stroke (or member) casts nothing
|
||||
// and does not place the plane.
|
||||
let mut cast: Vec<shadow::Cast> = vec![];
|
||||
let ink = |item: &AppearanceItem, member: f32| {
|
||||
let (_, opacity, blend) = paint_of(item, member);
|
||||
shadow::Ink { opacity, blend }
|
||||
};
|
||||
if id == ROTATE {
|
||||
for s in shapes {
|
||||
for item in s.items.iter().filter(|i| ink(i, s.opacity).opacity > 0.0) {
|
||||
let (area, rule) = region(&s.path, s.rule, item);
|
||||
let faces = caster.plane(&area, rule, b, view)?;
|
||||
cast.push((s.scopes.as_slice(), ink(item, s.opacity), faces));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let (parts, bounds) = solid_parts(shapes, b);
|
||||
let inks: Vec<shadow::Ink> = parts.iter().map(|(item, member, ..)| ink(item, *member)).collect();
|
||||
// Inflate's eye is placed for every shape together, as the solids are.
|
||||
let added: Vec<Result<std::ops::Range<usize>, String>> = if id == INFLATE {
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule, _, _)| (area, *rule)).collect();
|
||||
let casts: Vec<bool> = inks.iter().map(|i| i.opacity > 0.0).collect();
|
||||
caster.inflate(&shapes, &casts, bounds, inflate_options(p), view)
|
||||
} else {
|
||||
let mut added = vec![];
|
||||
for ((_, _, area, rule, _, _), ink) in parts.iter().zip(&inks) {
|
||||
added.push(if ink.opacity > 0.0 { caster.extrude(area, *rule, bounds, extrude_options(p), view) } else { Ok(0..0) });
|
||||
}
|
||||
added
|
||||
};
|
||||
for (((item, _, _, _, scopes, _), ink), faces) in parts.iter().zip(inks).zip(added) {
|
||||
if ink.opacity <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
// A stroke whose own solid is past the limits is left out; past the shadow's limit for
|
||||
// all the solids together, the shadow is refused.
|
||||
let faces = match faces {
|
||||
Ok(faces) => faces,
|
||||
Err(e) if matches!(item, AppearanceItem::Stroke(_)) && e != vectorcraft_three_d::TOO_COMPLEX_SHADOW => continue,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
cast.push((*scopes, ink, faces));
|
||||
}
|
||||
}
|
||||
Ok(Some((caster, settings, cast)))
|
||||
}
|
||||
|
||||
/// The faces a shadow is cast from, its settings, and the faces of each solid of visible art with
|
||||
/// its member's scopes and ink ([`cast_shadow`]).
|
||||
type Cast<'a> = (vectorcraft_three_d::ShadowCaster, vectorcraft_three_d::Shadow, Vec<shadow::Cast<'a>>);
|
||||
|
||||
/// [`art_for`] without the shadow: the solids' surfaces.
|
||||
fn surfaces_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>> {
|
||||
let b = union_bounds(shapes)?;
|
||||
let p = crate::merged_params(&fx.id, &fx.params);
|
||||
let classic = fx.id == CLASSIC_EXTRUDE;
|
||||
@@ -588,17 +800,17 @@ fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>>
|
||||
let (parts, bounds) = solid_parts(shapes, b);
|
||||
let built = if fx.id == INFLATE {
|
||||
// One eye for every puff, as far away as the highest needs.
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule, _)| (area, *rule)).collect();
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule, _, _)| (area, *rule)).collect();
|
||||
vectorcraft_three_d::inflate_together(&shapes, bounds, inflate_options(&p), view_options(&p))
|
||||
} else {
|
||||
parts.iter().map(|(_, _, area, rule, _)| facets(&fx.id, area, *rule, bounds, &p)).collect()
|
||||
parts.iter().map(|(_, _, area, rule, _, _)| facets(&fx.id, area, *rule, bounds, &p)).collect()
|
||||
};
|
||||
let mut solids = vec![];
|
||||
for ((item, opacity, _, _, scopes), f) in parts.iter().zip(built) {
|
||||
for ((item, opacity, _, _, scopes, member), f) in parts.iter().zip(built) {
|
||||
let item: &AppearanceItem = item;
|
||||
match f {
|
||||
Ok(f) if !crate::revolve::paint_copies_add(&mut paint_bytes, facet_copies(&f, paint_ref(item), colored), paint_ref(item)) => return None,
|
||||
Ok(f) => solids.push((item, *opacity, *scopes, f)),
|
||||
Ok(f) => solids.push((item, *opacity, *scopes, f, *member)),
|
||||
// A stroke's outline past the limits is left out; the fill's solid still draws.
|
||||
Err(_) if matches!(item, AppearanceItem::Stroke(_)) => {}
|
||||
// A loaded file or a shape edited past the limits keeps drawing its source path
|
||||
@@ -610,14 +822,16 @@ fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>>
|
||||
if solids.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let opaque = solids.iter().all(|(item, member, _, _)| {
|
||||
let opaque = solids.iter().all(|(item, member, _, _, _)| {
|
||||
let (_, opacity, blend) = paint_of(item, *member);
|
||||
opacity >= 1.0 && blend == BlendMode::Normal
|
||||
});
|
||||
let surfaces: Vec<(Vec<f64>, Node, &[Scope])> = solids
|
||||
let surfaces: Vec<(Vec<Placed>, Node, &[Scope])> = solids
|
||||
.into_iter()
|
||||
.map(|(item, member, scopes, f)| {
|
||||
let depths: Vec<f64> = f.iter().map(|f| f.depth).collect();
|
||||
.map(|(item, member, scopes, f, shape)| {
|
||||
let closed = shapes.get(shape).is_some_and(|s| s.path.subpaths.iter().all(|p| p.closed));
|
||||
let paints = Paints::of(item, closed);
|
||||
let depths: Vec<Placed> = f.iter().map(|f| Placed::of(f, shape, paints)).collect();
|
||||
let (paint, opacity, blend) = paint_of(item, member);
|
||||
let art = if classic { classic::surface(n, f, &paint, opacity, blend, &p) } else { surface(n, f, &paint, opacity, blend, colored) };
|
||||
(depths, art, scopes)
|
||||
@@ -635,15 +849,20 @@ fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>>
|
||||
if !opaque || !plain {
|
||||
return Some(surfaces.into_iter().map(|(_, s, _)| Arc::new(s)).collect());
|
||||
}
|
||||
let mut faces = vec![];
|
||||
let (mut placed, mut faces) = (vec![], vec![]);
|
||||
for (depths, s, _) in surfaces {
|
||||
if let NodeKind::Group { children, .. } = s.kind {
|
||||
faces.extend(depths.into_iter().zip(children));
|
||||
placed.extend(depths);
|
||||
faces.extend(children.into_iter().map(Some));
|
||||
}
|
||||
}
|
||||
// A stable sort: faces at the same depth keep the order of the appearance.
|
||||
faces.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
Some(vec![Arc::new(Node::group(n.id, faces.into_iter().map(|(_, face)| face).collect()))])
|
||||
let sealed = match fx.id.as_str() {
|
||||
INFLATE => inflate_options(&p).cap,
|
||||
CLASSIC_EXTRUDE => classic_extrude_options(&p).cap,
|
||||
_ => extrude_options(&p).cap,
|
||||
};
|
||||
let order = draw_order(&placed, sealed);
|
||||
Some(vec![Arc::new(Node::group(n.id, order.into_iter().filter_map(|k| faces.get_mut(k).and_then(Option::take)).collect()))])
|
||||
}
|
||||
|
||||
fn geometry(path: &PathData, fx: &[Effect], ctx: &GeomContext) -> PathData {
|
||||
@@ -676,7 +895,8 @@ pub fn solid_art_in(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
let remaining: Vec<Effect>;
|
||||
if let Some((index, fx)) = solid(&n.appearance.effects) {
|
||||
let path = geometry(&path, n.appearance.effects.get(..index)?, &ctx);
|
||||
children.extend(art_for(n, fx, &[Profile { path, rule, items: paints, opacity: 1.0, scopes: vec![] }])?);
|
||||
let (shadow, surfaces) = art_for(n, fx, &[Profile { path, rule, items: paints, opacity: 1.0, scopes: vec![] }])?;
|
||||
children.extend(shadow.into_iter().chain(surfaces));
|
||||
// Later geometry effects act on the projected art as a whole.
|
||||
remaining = n
|
||||
.appearance
|
||||
@@ -689,18 +909,24 @@ pub fn solid_art_in(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
} else {
|
||||
let path = geometry(&path, &n.appearance.effects, &ctx);
|
||||
remaining = n.appearance.effects.iter().filter(|e| !is_geometry(&e.id)).cloned().collect();
|
||||
// Every item's shadow goes behind every item's art.
|
||||
let mut shadows = vec![];
|
||||
for item in &paints {
|
||||
if let Some((index, fx)) = solid(item.effects()) {
|
||||
let p = geometry(&path, item.effects().get(..index)?, &ctx.item(item));
|
||||
for mut art in art_for(n, fx, &[Profile { path: p, rule, items: vec![item.clone()], opacity: 1.0, scopes: vec![] }])? {
|
||||
Arc::make_mut(&mut art).appearance.effects =
|
||||
item.effects().iter().skip(index + 1).filter(|e| !is_three_d(&e.id)).cloned().collect();
|
||||
children.push(art);
|
||||
}
|
||||
let (shadow, surfaces) = art_for(n, fx, &[Profile { path: p, rule, items: vec![item.clone()], opacity: 1.0, scopes: vec![] }])?;
|
||||
let later: Vec<Effect> = item.effects().iter().skip(index + 1).filter(|e| !is_three_d(&e.id)).cloned().collect();
|
||||
let with_later = |mut art: Arc<Node>| {
|
||||
Arc::make_mut(&mut art).appearance.effects = later.clone();
|
||||
art
|
||||
};
|
||||
shadows.extend(shadow.into_iter().map(with_later));
|
||||
children.extend(surfaces.into_iter().map(with_later));
|
||||
} else {
|
||||
children.push(Arc::new(Node::path(n.id, path.clone(), Appearance { items: vec![item.clone()], ..Default::default() })));
|
||||
}
|
||||
}
|
||||
children.splice(0..0, shadows);
|
||||
}
|
||||
let mut art = n.clone();
|
||||
art.kind = NodeKind::Group { children, clip: false };
|
||||
@@ -716,7 +942,8 @@ fn group_solid_art(n: &Node) -> Option<Node> {
|
||||
let (index, fx) = solid(&n.appearance.effects)?;
|
||||
let mut shapes = members(n, "").ok()?;
|
||||
reshape_members(n, n.appearance.effects.get(..index)?, &mut shapes);
|
||||
let children = art_for(n, fx, &shapes)?;
|
||||
let (shadow, surfaces) = art_for(n, fx, &shapes)?;
|
||||
let children = shadow.into_iter().chain(surfaces).collect();
|
||||
let remaining =
|
||||
n.appearance.effects.iter().enumerate().filter(|(i, e)| *i != index && (!is_geometry(&e.id) || *i > index)).map(|(_, e)| e.clone()).collect();
|
||||
let mut art = n.clone();
|
||||
@@ -788,11 +1015,11 @@ fn meshes_for(fx: &Effect, shapes: &[Profile], total: &mut usize, limit: usize)
|
||||
// Inflate lays out every puff at once (they share one eye); each Inflate and Extrude & Bevel
|
||||
// solid and its mesh are built as the loop reaches them.
|
||||
let mut inflated = (fx.id == INFLATE).then(|| {
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule, _)| (area, *rule)).collect();
|
||||
let shapes: Vec<(&PathData, FillRule)> = parts.iter().map(|(_, _, area, rule, _, _)| (area, *rule)).collect();
|
||||
vectorcraft_three_d::inflate_meshes(&shapes, bounds, inflate_options(&p), view)
|
||||
});
|
||||
let extrude = extrude_options(&p);
|
||||
for (item, member, area, rule, _) in &parts {
|
||||
for (item, member, area, rule, _, _) in &parts {
|
||||
let item: &AppearanceItem = item;
|
||||
let mesh = match inflated.as_mut() {
|
||||
Some(built) => built.next().unwrap_or_else(|| Ok(Mesh::default())),
|
||||
@@ -906,3 +1133,6 @@ mod tests_mesh;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_sources;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_shadow;
|
||||
@@ -0,0 +1,101 @@
|
||||
//! The shadow of the 3D and Materials effects (Extrude & Bevel, Inflate, Rotate and Revolve): the
|
||||
//! `shadow` switch with `shadowPosition`, `shadowDistance` and `shadowBounds`, and the shape it
|
||||
//! draws behind the solids.
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::Value;
|
||||
use vectorcraft_doc::color::{BlendMode, Color, Paint};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, FillLayer, Node, NodeId};
|
||||
use vectorcraft_three_d::{Shadow, ShadowCaster, ShadowPlane, ShadowPosition};
|
||||
|
||||
use super::Scope;
|
||||
use crate::util::{flag, num, text};
|
||||
|
||||
/// The shadow's paint: black at this opacity. VectorCraft's own values.
|
||||
const SHADOW_OPACITY: f32 = 0.5;
|
||||
|
||||
/// The shadow `p` asks for, or `None` with the switch off or when the first light lights nothing
|
||||
/// (Shading is off or its intensity is 0), so that no shadow is built.
|
||||
pub(crate) fn options(p: &Value) -> Option<Shadow> {
|
||||
let intensity = crate::lighting::lights(p).and_then(|l| l.iter().next().map(|l| l.intensity)).unwrap_or_else(|| num(p, "lightIntensity", 80.0));
|
||||
let lit = flag(p, "shade", true) && intensity > 0.0;
|
||||
(lit && flag(p, "shadow", false)).then(|| Shadow {
|
||||
position: ShadowPosition::parse(text(p, "shadowPosition", "behind")).unwrap_or_default(),
|
||||
distance: num(p, "shadowDistance", 0.0) / 100.0,
|
||||
bounds: num(p, "shadowBounds", 100.0) / 100.0,
|
||||
})
|
||||
}
|
||||
|
||||
/// Check the shadow settings in `p`: an error for a value out of its range or of the wrong kind.
|
||||
pub(crate) fn validate(p: &Value) -> Result<(), String> {
|
||||
if p.get("shadow").is_some_and(|v| !v.is_boolean()) {
|
||||
return Err("shadow must be true or false".into());
|
||||
}
|
||||
if p.get("shadowPosition").is_some_and(|v| v.as_str().and_then(ShadowPosition::parse).is_none()) {
|
||||
return Err("shadowPosition must be behind or below".into());
|
||||
}
|
||||
for (key, min, max) in [("shadowDistance", 0.0, 100.0), ("shadowBounds", 10.0, 400.0)] {
|
||||
if p.get(key).is_some_and(|v| !v.as_f64().is_some_and(|n| n.is_finite() && (min..=max).contains(&n))) {
|
||||
return Err(format!("{key} must be a number from {min} to {max}"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The opacity and blending mode of the fill or stroke (and member) that casts part of a shadow.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(crate) struct Ink {
|
||||
pub(crate) opacity: f32,
|
||||
pub(crate) blend: BlendMode,
|
||||
}
|
||||
|
||||
/// The shadow of the faces in `caster` as a path of object `id`, cast by art in `ink`: filled
|
||||
/// black at [`SHADOW_OPACITY`] times its opacity, in its blending mode, with the nonzero rule;
|
||||
/// `None` when it casts nothing.
|
||||
pub(crate) fn art(id: NodeId, caster: &ShadowCaster, shadow: Shadow, ink: Ink) -> Option<Node> {
|
||||
let all = 0..caster.len();
|
||||
let all = std::slice::from_ref(&all);
|
||||
piece(id, caster, &caster.shadow_plane(shadow, all)?, all, ink)
|
||||
}
|
||||
|
||||
/// [`art`] of the faces in the ranges `faces` of `caster` only, on `plane`.
|
||||
fn piece(id: NodeId, caster: &ShadowCaster, plane: &ShadowPlane, faces: &[Range<usize>], ink: Ink) -> Option<Node> {
|
||||
let path = caster.path_on(plane, faces)?;
|
||||
let mut fill = FillLayer::new(Paint::solid(Color::BLACK));
|
||||
fill.opacity = SHADOW_OPACITY * ink.opacity.clamp(0.0, 1.0);
|
||||
fill.blend = ink.blend;
|
||||
Some(Node::path(id, path, Appearance { items: vec![AppearanceItem::Fill(fill)], ..Default::default() }))
|
||||
}
|
||||
|
||||
/// Whether `a` and `b` are the same objects' scopes (each member holds its own copies).
|
||||
fn same_scopes(a: &[Scope], b: &[Scope]) -> bool {
|
||||
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| a.key == b.key)
|
||||
}
|
||||
|
||||
/// The faces one solid added to a caster, with the scopes of its member and the ink of its art.
|
||||
pub(crate) type Cast<'a> = (&'a [Scope], Ink, Range<usize>);
|
||||
|
||||
/// The faces of the solids that cast one piece of a shadow, with their scopes and ink.
|
||||
type Run<'a> = (&'a [Scope], Ink, Vec<Range<usize>>);
|
||||
|
||||
/// The shadow `cast` casts on one plane: the faces of `caster` that the solids of visible art
|
||||
/// added, each range with the scopes of the member that cast it and the ink of the art, in order.
|
||||
/// Each run of solids in the same ink casts a piece in that ink ([`art`]); with `masks` (a member's
|
||||
/// opacity mask or blending mode applies to its solids) the solids of a run also share their scopes,
|
||||
/// and each piece is drawn in their opacity masks and blending modes ([`super::masked`]).
|
||||
pub(crate) fn scoped_art(id: NodeId, caster: &ShadowCaster, shadow: Shadow, cast: Vec<Cast<'_>>, masks: bool) -> Vec<Arc<Node>> {
|
||||
let placing: Vec<Range<usize>> = cast.iter().map(|(.., faces)| faces.clone()).collect();
|
||||
let Some(plane) = caster.shadow_plane(shadow, &placing) else { return vec![] };
|
||||
// Consecutive solids in the same ink (and with masks, the same scopes) share a piece; the pieces
|
||||
// keep the solids' order.
|
||||
let mut runs: Vec<Run> = vec![];
|
||||
for (scopes, ink, faces) in cast {
|
||||
match runs.last_mut() {
|
||||
Some(run) if run.1 == ink && (!masks || same_scopes(run.0, scopes)) => run.2.push(faces),
|
||||
_ => runs.push((scopes, ink, vec![faces])),
|
||||
}
|
||||
}
|
||||
let pieces = runs.into_iter().filter_map(|(scopes, ink, faces)| Some((scopes, Arc::new(piece(id, caster, &plane, &faces, ink)?))));
|
||||
if masks { super::masked(id, pieces.collect()) } else { pieces.map(|(_, piece)| piece).collect() }
|
||||
}
|
||||
@@ -180,6 +180,137 @@ fn the_front_caps_are_drawn_last_in_the_order_of_the_appearance() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A C, open to the right: two concave corners where its walls meet the walls of the notch.
|
||||
fn letter_c() -> BezPath {
|
||||
let mut b = BezPath::new();
|
||||
b.move_to((100.0, 100.0));
|
||||
b.line_to((200.0, 100.0));
|
||||
b.line_to((200.0, 130.0));
|
||||
b.line_to((130.0, 130.0));
|
||||
b.line_to((130.0, 170.0));
|
||||
b.line_to((200.0, 170.0));
|
||||
b.line_to((200.0, 200.0));
|
||||
b.line_to((100.0, 200.0));
|
||||
b.close_path();
|
||||
b
|
||||
}
|
||||
|
||||
/// The pairs (blue, red) of `walls`, by index, where the blue face is drawn first and the two
|
||||
/// faces' bounds overlap.
|
||||
fn red_after_blue(walls: &[&Node]) -> Vec<(usize, usize)> {
|
||||
let bounds: Vec<Rect> = walls.iter().map(|f| f.path_data().and_then(PathData::bounds).unwrap()).collect();
|
||||
let overlap = |a: &Rect, b: &Rect| a.x0 < b.x1 && b.x0 < a.x1 && a.y0 < b.y1 && b.y0 < a.y1;
|
||||
let mut out = vec![];
|
||||
for (i, a) in walls.iter().enumerate() {
|
||||
for (j, b) in walls.iter().enumerate().skip(i + 1) {
|
||||
if blue(a) && !blue(b) && overlap(&bounds[i], &bounds[j]) {
|
||||
out.push((i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// An opaque stroke's solid holds the fill's edge, so the fill's walls are drawn before every
|
||||
/// face of the stroke they overlap, and the stroke's walls cover them at the concave corners of a
|
||||
/// turned C, in Extrude & Bevel and Extrude & Bevel (Classic). The front caps stay last, the
|
||||
/// fill's below the stroke's.
|
||||
#[test]
|
||||
fn a_fills_walls_are_drawn_before_the_stroke_faces_over_them() {
|
||||
let views = [json!({}), json!({"rotationX": -20, "rotationY": -40, "rotationZ": 0}), json!({"rotationX": 25, "rotationY": -55, "rotationZ": 10})];
|
||||
for id in [EXTRUDE, CLASSIC_EXTRUDE] {
|
||||
for view in &views {
|
||||
let mut params = view.clone();
|
||||
params["depth"] = json!(60);
|
||||
let mut n = stroked(&letter_c());
|
||||
n.appearance.effects.push(fx(id, params));
|
||||
let art = solid_art(&n).unwrap();
|
||||
let faces = faces(&art);
|
||||
let [walls @ .., fill_cap, stroke_cap] = faces.as_slice() else { panic!("{id} {view}: the caps") };
|
||||
assert!(!blue(fill_cap) && blue(stroke_cap), "{id} {view}: the caps last");
|
||||
assert!(walls.iter().any(|f| !blue(f)) && walls.iter().any(|f| blue(f)), "{id} {view}: walls of both");
|
||||
let wrong = red_after_blue(walls);
|
||||
assert!(wrong.is_empty(), "{id} {view}: fill walls after the stroke faces over them: {wrong:?} of {}", walls.len());
|
||||
}
|
||||
}
|
||||
// Strokes aligned inside or outside keep the fill's walls, in that order. A dashed stroke
|
||||
// leaves the fill's walls in the gaps between its dashes, and they keep their depths.
|
||||
use vectorcraft_doc::StrokeAlign;
|
||||
for (dash, align) in [(true, StrokeAlign::Center), (false, StrokeAlign::Inside), (false, StrokeAlign::Outside)] {
|
||||
let mut n = stroked(&letter_c());
|
||||
if let Some(AppearanceItem::Stroke(s)) = n.appearance.items.iter_mut().find(|i| matches!(i, AppearanceItem::Stroke(_))) {
|
||||
s.dash = dash.then(|| vectorcraft_doc::Dash { pattern: vec![12.0, 12.0], offset: 0.0, align_corners: false });
|
||||
s.align = align;
|
||||
}
|
||||
n.appearance.effects.push(fx(EXTRUDE, views[1].clone()));
|
||||
let art = solid_art(&n).unwrap();
|
||||
let faces = faces(&art);
|
||||
let [walls @ .., _, _] = faces.as_slice() else { panic!("{align:?}: the caps") };
|
||||
assert!(walls.iter().any(|f| !blue(f)), "{align:?}: the fill's walls");
|
||||
if !dash {
|
||||
assert!(red_after_blue(walls).is_empty(), "{align:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The order keeps depths except where a shape's fill wall turned toward the viewer overlaps a
|
||||
/// face of the same shape's stroke drawn before it: the wall then goes just before that face. A
|
||||
/// cap, a face turned away, another shape's stroke, a stroke face the wall does not overlap and an
|
||||
/// effect without caps keep the order of the depths.
|
||||
#[test]
|
||||
fn the_draw_order_moves_a_fill_wall_before_the_stroke_faces_it_overlaps() {
|
||||
let at = |depth: f64, x: f64, member: usize, paints: Paints, side: bool, away: bool| Placed {
|
||||
depth,
|
||||
side,
|
||||
away,
|
||||
bounds: Rect::new(x, 0.0, x + 10.0, 10.0),
|
||||
member,
|
||||
paints,
|
||||
};
|
||||
// 0 the fill's wall; 1 the stroke's wall over it; 2 another shape's stroke over it; 3 a stroke
|
||||
// wall elsewhere; 4 a stroke face turned away; 5 a fill cap; 6 the stroke's cap, nearer; 7 a
|
||||
// fill wall at the stroke wall's depth.
|
||||
let faces = [
|
||||
at(5.0, 0.0, 0, Paints::Fill, true, false),
|
||||
at(3.0, 5.0, 0, Paints::Stroke, true, false),
|
||||
at(2.0, 5.0, 1, Paints::Stroke, true, false),
|
||||
at(1.0, 50.0, 0, Paints::Stroke, true, false),
|
||||
at(0.0, 0.0, 0, Paints::Stroke, true, true),
|
||||
at(4.0, 0.0, 0, Paints::Fill, false, false),
|
||||
at(6.0, 0.0, 0, Paints::Stroke, false, false),
|
||||
at(3.0, 0.0, 0, Paints::Fill, true, false),
|
||||
];
|
||||
assert_eq!(draw_order(&faces, true), [4, 3, 2, 0, 7, 1, 5, 6]);
|
||||
assert_eq!(draw_order(&faces, false), [4, 3, 2, 1, 7, 5, 0, 6]);
|
||||
// A shape with no stroke, and a stroke without a width, move nothing.
|
||||
// Only a stroke whose solid hides the fill's whole edge counts: an opaque, plain stroke with a
|
||||
// width on a closed shape.
|
||||
let stroke = |f: &dyn Fn(&mut StrokeLayer)| {
|
||||
let mut s = StrokeLayer::new(Paint::solid(BLUE), 6.0);
|
||||
f(&mut s);
|
||||
AppearanceItem::Stroke(s)
|
||||
};
|
||||
assert!(Paints::of(&stroke(&|_| {}), true) == Paints::Stroke);
|
||||
assert!(Paints::of(&stroke(&|_| {}), false) == Paints::Other, "an open shape");
|
||||
assert!(Paints::of(&stroke(&|s| s.width = 0.0), true) == Paints::Other, "no width");
|
||||
let dash = vectorcraft_doc::Dash { pattern: vec![4.0, 4.0], offset: 0.0, align_corners: false };
|
||||
assert!(Paints::of(&stroke(&|s| s.dash = Some(dash.clone())), true) == Paints::Other, "dashes");
|
||||
assert!(Paints::of(&stroke(&|s| s.brush = Some("brush".into())), true) == Paints::Other);
|
||||
assert!(Paints::of(&stroke(&|s| s.paint = Paint::None), true) == Paints::Other);
|
||||
assert!(Paints::of(&stroke(&|s| s.paint = Paint::Pattern { pattern: "p".into(), xf: Affine::IDENTITY }), true) == Paints::Other, "a pattern");
|
||||
use vectorcraft_doc::color::{Gradient, GradientKind, GradientPaint, GradientStop};
|
||||
let gradient = |opacity: f32| {
|
||||
let mut end = GradientStop::new(1.0, RED);
|
||||
end.opacity = opacity;
|
||||
Paint::Gradient(Box::new(GradientPaint::new(Gradient::new(GradientKind::Linear, vec![GradientStop::new(0.0, BLUE), end]))))
|
||||
};
|
||||
assert!(Paints::of(&stroke(&|s| s.paint = gradient(1.0)), true) == Paints::Stroke, "an opaque gradient");
|
||||
assert!(Paints::of(&stroke(&|s| s.paint = gradient(0.5)), true) == Paints::Other, "a gradient with a transparent stop");
|
||||
let other = |p: Paints| if p == Paints::Stroke { Paints::Other } else { p };
|
||||
let unstroked: Vec<Placed> = faces.iter().map(|f| at(f.depth, f.bounds.x0, f.member, other(f.paints), f.side, f.away)).collect();
|
||||
assert_eq!(draw_order(&unstroked, true), draw_order(&faces, false));
|
||||
}
|
||||
|
||||
/// A stroked open path builds a ribbon, the stroke's outline, not the area the path would enclose
|
||||
/// if it were closed: for an L of two 100 pt legs, a 6 pt band with butt ends and a mitered corner.
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::color::Color;
|
||||
use vectorcraft_doc::{Document, NodeId};
|
||||
use vectorcraft_geom::{Affine, BezPath};
|
||||
|
||||
use super::*;
|
||||
|
||||
const RED: Color = Color::Rgb { r: 0.8, g: 0.2, b: 0.1 };
|
||||
|
||||
/// The 3D and Materials effects that cast shadows.
|
||||
const CASTING: [&str; 4] = [EXTRUDE, INFLATE, ROTATE, crate::REVOLVE];
|
||||
|
||||
fn square_node(id: u64, dx: f64) -> Node {
|
||||
let mut b = BezPath::new();
|
||||
b.move_to((100.0 + dx, 100.0));
|
||||
b.line_to((200.0 + dx, 100.0));
|
||||
b.line_to((200.0 + dx, 200.0));
|
||||
b.line_to((100.0 + dx, 200.0));
|
||||
b.close_path();
|
||||
Node::path(NodeId(id), PathData::from_bezpath(&b), Appearance::basic(Paint::solid(RED), Paint::None, 0.0))
|
||||
}
|
||||
|
||||
fn art(n: &Node, id: &str) -> Option<Node> {
|
||||
if id == crate::REVOLVE { crate::revolve_art(n) } else { solid_art(n) }
|
||||
}
|
||||
|
||||
/// The opacity of shadow `n`: a path filled black and nothing else; `None` for any other node.
|
||||
fn shadow_opacity(n: &Node) -> Option<f32> {
|
||||
let [AppearanceItem::Fill(fill)] = n.appearance.items.as_slice() else { return None };
|
||||
(matches!(n.kind, NodeKind::Path { .. }) && fill.paint.color() == Some(Color::BLACK)).then_some(fill.opacity)
|
||||
}
|
||||
|
||||
/// Whether `n` is the shadow of opaque art: a path filled black at 50 %, nothing else.
|
||||
fn is_shadow(n: &Node) -> bool {
|
||||
shadow_opacity(n) == Some(0.5)
|
||||
}
|
||||
|
||||
fn shadows(n: &Node) -> usize {
|
||||
let mut count = 0;
|
||||
n.walk(&mut |c| count += usize::from(is_shadow(c)));
|
||||
count
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_3d_effect_draws_its_shadow_behind_its_solid_when_shadows_are_on() {
|
||||
for id in CASTING {
|
||||
let mut n = square_node(1, 0.0);
|
||||
n.appearance.effects.push(crate::new_effect(id, &json!({})).unwrap());
|
||||
let plain = art(&n, id).unwrap_or_else(|| panic!("{id}"));
|
||||
assert_eq!(shadows(&plain), 0, "{id}: off by default");
|
||||
n.appearance.effects[0].params = json!({"shadow": true, "shadowDistance": 50});
|
||||
let lit = art(&n, id).unwrap_or_else(|| panic!("{id}"));
|
||||
let children = lit.children().unwrap();
|
||||
assert!(is_shadow(&children[0]), "{id}: the shadow is drawn first");
|
||||
assert_eq!(shadows(&lit), 1, "{id}");
|
||||
// The default light is up and to the left: the shadow on a plane away from the object
|
||||
// reaches past the solid down and right.
|
||||
let shadow = children[0].visual_bounds().unwrap();
|
||||
let solid = children[1..].iter().filter_map(|c| c.visual_bounds()).reduce(|a, b| a.union(b)).unwrap();
|
||||
assert!(shadow.x1 > solid.x1 && shadow.y1 > solid.y1, "{id}: {shadow:?} {solid:?}");
|
||||
// The art's bounds take the shadow in.
|
||||
assert!(lit.visual_bounds().unwrap().contains_rect(shadow), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_shadow_moves_with_its_settings() {
|
||||
for id in CASTING {
|
||||
let bounds = |params: Value| {
|
||||
let mut n = square_node(1, 0.0);
|
||||
n.appearance.effects.push(crate::new_effect(id, ¶ms).unwrap());
|
||||
let lit = art(&n, id).unwrap();
|
||||
let shadow = &lit.children().unwrap()[0];
|
||||
assert!(is_shadow(shadow), "{id} {params}");
|
||||
shadow.visual_bounds().unwrap()
|
||||
};
|
||||
let near = bounds(json!({"shadow": true}));
|
||||
let far = bounds(json!({"shadow": true, "shadowDistance": 100}));
|
||||
assert!(far.x1 > near.x1 + 1.0, "{id}: {near:?} {far:?}");
|
||||
// With the light straight in front, the outline falls about the plane's center; at 10 %
|
||||
// the plane cuts it to a square 0.4 times the object's size across.
|
||||
let tight = bounds(json!({"shadow": true, "shadowBounds": 10, "lightHeight": 90}));
|
||||
let wide = bounds(json!({"shadow": true, "shadowBounds": 400, "lightHeight": 90}));
|
||||
assert!(wide.width() > tight.width() * 2.0, "{id}: {tight:?} {wide:?}");
|
||||
let below = bounds(json!({"shadow": true, "shadowPosition": "below", "rotationX": -30}));
|
||||
assert_ne!(below, near, "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shadow_settings_out_of_range_or_of_the_wrong_kind_are_refused() {
|
||||
for id in CASTING {
|
||||
for (params, error) in [
|
||||
(json!({"shadow": "yes"}), "shadow must be true or false"),
|
||||
(json!({"shadowPosition": "under"}), "shadowPosition must be behind or below"),
|
||||
(json!({"shadowPosition": 1}), "shadowPosition must be behind or below"),
|
||||
(json!({"shadowDistance": 101}), "shadowDistance must be a number from 0 to 100"),
|
||||
(json!({"shadowDistance": -1}), "shadowDistance must be a number from 0 to 100"),
|
||||
(json!({"shadowBounds": 5}), "shadowBounds must be a number from 10 to 400"),
|
||||
(json!({"shadowBounds": "wide"}), "shadowBounds must be a number from 10 to 400"),
|
||||
] {
|
||||
let err = validate_three_d_params(id, ¶ms).unwrap_err();
|
||||
assert!(err.contains(error), "{id} {params}: {err}");
|
||||
}
|
||||
let ok = json!({"shadow": true, "shadowPosition": "below", "shadowDistance": 100, "shadowBounds": 10});
|
||||
assert!(validate_three_d_params(id, &ok).is_ok(), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_group_casts_one_shadow_for_all_its_members() {
|
||||
for id in CASTING {
|
||||
let mut g = Node::group(NodeId(9), vec![Arc::new(square_node(1, 0.0)), Arc::new(square_node(2, 200.0))]);
|
||||
g.appearance.effects.push(crate::new_effect(id, &json!({"shadow": true, "lightHeight": 90})).unwrap());
|
||||
let lit = art(&g, id).unwrap_or_else(|| panic!("{id}"));
|
||||
assert_eq!(shadows(&lit), 1, "{id}");
|
||||
let shadow = lit.children().unwrap()[0].clone();
|
||||
assert!(is_shadow(&shadow), "{id}");
|
||||
// With the light straight in front, the shadow behind spans both members.
|
||||
let b = shadow.visual_bounds().unwrap();
|
||||
assert!(b.x0 < 200.0 && b.x1 > 300.0, "{id}: {b:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A member's opacity mask applies to the part of the shadow it casts, as to its solid: that part
|
||||
/// is drawn in the mask, the other member's outside it, both on one plane.
|
||||
#[test]
|
||||
fn a_masked_member_casts_its_part_of_the_shadow_in_its_mask() {
|
||||
for id in CASTING {
|
||||
let mut masked = square_node(2, 200.0);
|
||||
let mut cover = square_node(3, 200.0);
|
||||
cover.appearance.items = Appearance::basic(Paint::solid(Color::WHITE), Paint::None, 0.0).items;
|
||||
masked.extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(cover, true)));
|
||||
let mut g = Node::group(NodeId(9), vec![Arc::new(square_node(1, 0.0)), Arc::new(masked)]);
|
||||
g.appearance.effects.push(crate::new_effect(id, &json!({"shadow": true})).unwrap());
|
||||
let lit = art(&g, id).unwrap_or_else(|| panic!("{id}"));
|
||||
assert_eq!(shadows(&lit), 2, "{id}: one piece for each member");
|
||||
let mut in_mask = 0;
|
||||
lit.walk(&mut |c| {
|
||||
if c.extras().mask.is_some() {
|
||||
c.walk(&mut |s| in_mask += usize::from(is_shadow(s)));
|
||||
}
|
||||
});
|
||||
assert_eq!(in_mask, 1, "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transparent_art_and_a_light_that_lights_nothing_cast_no_shadow() {
|
||||
for id in CASTING {
|
||||
let mut n = square_node(1, 0.0);
|
||||
n.appearance.effects.push(crate::new_effect(id, &json!({"shadow": true, "shade": false})).unwrap());
|
||||
assert_eq!(shadows(&art(&n, id).unwrap()), 0, "{id}: Shading off");
|
||||
n.appearance.effects[0].params = json!({"shadow": true, "lightIntensity": 0});
|
||||
assert_eq!(shadows(&art(&n, id).unwrap()), 0, "{id}: no intensity");
|
||||
// A member at 0 % opacity casts nothing; the other member still casts its shadow.
|
||||
let mut clear = square_node(2, 200.0);
|
||||
clear.opacity = 0.0;
|
||||
let mut g = Node::group(NodeId(9), vec![Arc::new(square_node(1, 0.0)), Arc::new(clear)]);
|
||||
g.appearance.effects.push(crate::new_effect(id, &json!({"shadow": true, "lightHeight": 90})).unwrap());
|
||||
let lit = art(&g, id).unwrap_or_else(|| panic!("{id}"));
|
||||
let shadow = lit.children().unwrap()[0].clone();
|
||||
assert!(is_shadow(&shadow), "{id}");
|
||||
let b = shadow.visual_bounds().unwrap();
|
||||
assert!(b.x1 < 300.0, "{id}: only the visible member casts: {b:?}");
|
||||
// Art at 50 % casts a shadow half as dark.
|
||||
let mut half = square_node(1, 0.0);
|
||||
for item in &mut half.appearance.items {
|
||||
if let AppearanceItem::Fill(fill) = item {
|
||||
fill.opacity = 0.5;
|
||||
}
|
||||
}
|
||||
half.appearance.effects.push(crate::new_effect(id, &json!({"shadow": true})).unwrap());
|
||||
let lit = art(&half, id).unwrap();
|
||||
assert_eq!(shadow_opacity(&lit.children().unwrap()[0]), Some(0.25), "{id}");
|
||||
// Art in a blending mode casts its shadow in that mode.
|
||||
for item in &mut half.appearance.items {
|
||||
if let AppearanceItem::Fill(fill) = item {
|
||||
fill.blend = BlendMode::Screen;
|
||||
}
|
||||
}
|
||||
let lit = art(&half, id).unwrap();
|
||||
let AppearanceItem::Fill(fill) = &lit.children().unwrap()[0].appearance.items[0] else { panic!("{id}: a fill") };
|
||||
assert_eq!(fill.blend, BlendMode::Screen, "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
/// With a 3D effect on each of two fills, both fills' shadows are drawn behind both fills' solids.
|
||||
#[test]
|
||||
fn the_shadows_of_every_fill_go_behind_every_fill_s_solid() {
|
||||
for id in CASTING {
|
||||
let mut n = square_node(1, 0.0);
|
||||
let fill = n.appearance.items.iter().find(|i| matches!(i, AppearanceItem::Fill(_))).unwrap().clone();
|
||||
n.appearance.items = vec![fill.clone(), fill];
|
||||
for item in &mut n.appearance.items {
|
||||
item.effects_mut().push(crate::new_effect(id, &json!({"shadow": true})).unwrap());
|
||||
}
|
||||
let lit = art(&n, id).unwrap_or_else(|| panic!("{id}"));
|
||||
let children = lit.children().unwrap();
|
||||
assert!(children.len() >= 4, "{id}");
|
||||
assert!(is_shadow(&children[0]) && is_shadow(&children[1]), "{id}: both shadows first");
|
||||
assert!(children[2..].iter().all(|c| !is_shadow(c)), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expand_appearance_keeps_the_shadow_as_a_path() {
|
||||
for id in CASTING {
|
||||
let mut d = Document::new(400.0, 400.0);
|
||||
let mut n = square_node(1, 0.0);
|
||||
n.id = d.alloc_id();
|
||||
n.appearance.effects.push(crate::new_effect(id, &json!({"shadow": true})).unwrap());
|
||||
let mut stroke = |_: &mut Document, _: &PathData, _: FillRule, _: &StrokeLayer| None;
|
||||
let expanded = crate::expand_leaf(&mut d, &n, &mut stroke).unwrap_or_else(|| panic!("{id}"));
|
||||
assert_eq!(shadows(&expanded), 1, "{id}");
|
||||
let mut effects = 0;
|
||||
expanded.walk(&mut |c| effects += c.appearance.effects.len());
|
||||
assert_eq!(effects, 0, "{id}");
|
||||
// It moves with the object, as the solid does.
|
||||
let before = art(&n, id).unwrap().children().unwrap()[0].visual_bounds().unwrap();
|
||||
n.transform(Affine::translate((40.0, 0.0)), false);
|
||||
let after = art(&n, id).unwrap().children().unwrap()[0].visual_bounds().unwrap();
|
||||
assert!((after.x0 - before.x0 - 40.0).abs() < 1e-6, "{id}: {before:?} {after:?}");
|
||||
}
|
||||
}
|
||||
@@ -173,6 +173,18 @@ fn bar(hw: f64) -> f64 {
|
||||
/// Where the notch of the (open) arrow sits, as a fraction of the length.
|
||||
const NOTCH: f64 = 0.7;
|
||||
|
||||
/// How far [`super::stroke_pieces`] trims the line under each end's head: the head's inset from
|
||||
/// the tip, along the path. Both are 0 without a head at that end, and in [`ArrowAlign::Extend`],
|
||||
/// where the stroke keeps its full length.
|
||||
pub(super) fn trims(st: &StrokeLayer) -> (f64, f64) {
|
||||
if st.arrow_align != ArrowAlign::Tip {
|
||||
return (0.0, 0.0);
|
||||
}
|
||||
let cap = cap_extent(st.cap, st.width);
|
||||
let inset_of = |kind: Option<Arrowhead>, end: bool| kind.map_or(0.0, |k| inset(k, st.arrow_weight(end), cap));
|
||||
(inset_of(st.start_arrow, false), inset_of(st.end_arrow, true))
|
||||
}
|
||||
|
||||
/// Distance from the tip where the stroke ends, for a stroke whose cap reaches `cap` past its end.
|
||||
fn inset(kind: Arrowhead, hw: f64, cap: f64) -> f64 {
|
||||
let l = len(hw);
|
||||
|
||||
@@ -18,11 +18,11 @@ use std::collections::VecDeque;
|
||||
use kurbo::{BezPath, Point, Shape, Vec2};
|
||||
use vectorcraft_doc::color::{Gradient, GradientGeom, GradientPaint, Paint};
|
||||
use vectorcraft_doc::live::{H_DOWN, H_LEFT, H_RIGHT, H_UP};
|
||||
use vectorcraft_doc::{GradientMesh, LineJoin, MeshPoint, StrokeAlign, StrokeGradientMode, StrokeLayer};
|
||||
use vectorcraft_doc::{GradientMesh, LineCap, LineJoin, MeshPoint, StrokeAlign, StrokeGradientMode, StrokeLayer};
|
||||
use vectorcraft_geom::FillRule;
|
||||
|
||||
use super::width::{flatten, left, length, split_at};
|
||||
use super::{aligned_width, cap_extent, is_closed, segments, subpaths, unit};
|
||||
use super::{aligned_width, arrow, cap_extent, is_closed, segments, subpaths, unit};
|
||||
|
||||
/// Largest stretch of a rib on a turn's bisector (1 / cos of half the turn, about 74°) before the
|
||||
/// corner gets a fan of ribs round its outside.
|
||||
@@ -97,7 +97,10 @@ impl Lay<'_> {
|
||||
|
||||
/// How far a rib at fraction `t` reaches either side of the path to cover the stroke there.
|
||||
fn cover(&self, t: f64) -> f64 {
|
||||
self.half(t) + self.margin
|
||||
// A square cap's corners stand √2 of the half-width out from where the dash ends, and a
|
||||
// dash can end just past a corner: reach that far, or they paint outside every slice.
|
||||
let diag = if self.st.dash.is_some() && self.st.cap == LineCap::Square { std::f64::consts::SQRT_2 } else { 1.0 };
|
||||
self.half(t) * diag + self.margin
|
||||
}
|
||||
|
||||
/// The across band at fraction `t` of a subpath whose visible side is `side` (`Some(true)`:
|
||||
@@ -162,6 +165,9 @@ fn strips(bp: &BezPath, rule: FillRule, st: &StrokeLayer, tol: f64, margin: f64,
|
||||
return vec![];
|
||||
}
|
||||
let heads = (st.head_reach(false), st.head_reach(true));
|
||||
// With a tip-aligned arrowhead the stroke's line, and with it the width profile, stops at the
|
||||
// head's inset from the tip (`stroke_pieces`).
|
||||
let trims = arrow::trims(st);
|
||||
let mut lay = Lay { st, hw, margin, ext: (0.0, 0.0) };
|
||||
// Caps reach as far past the ends as the stroke is wide there.
|
||||
let cap = |t: f64| cap_extent(st.cap, 2.0 * lay.half(t));
|
||||
@@ -170,27 +176,34 @@ fn strips(bp: &BezPath, rule: FillRule, st: &StrokeLayer, tol: f64, margin: f64,
|
||||
let count = subs.len();
|
||||
let mut out = vec![];
|
||||
for (k, (range, closed)) in subs.into_iter().enumerate() {
|
||||
let (pts, corner) = flatten(&segments(&bp.elements()[range]), closed, tol.max(1e-4));
|
||||
let side =
|
||||
(st.align != StrokeAlign::Center && closed_path && closed).then(|| band_on_left(bp, rule, &pts, st.align, (4.0 * tol).min(hw / 2.0)));
|
||||
let segs = segments(&bp.elements()[range]);
|
||||
let flat = flatten(&segs, closed, tol.max(1e-4));
|
||||
let side = (st.align != StrokeAlign::Center && closed_path && closed)
|
||||
.then(|| band_on_left(bp, rule, &flat.pts, st.align, (4.0 * tol).min(hw / 2.0)));
|
||||
// Arrowheads sit on the start of the first subpath and the end of the last one.
|
||||
let heads = (if k == 0 { heads.0 } else { 0.0 }, if k + 1 == count { heads.1 } else { 0.0 });
|
||||
let ribs = match pts.len() {
|
||||
let trims = (if k == 0 { trims.0 } else { 0.0 }, if k + 1 == count { trims.1 } else { 0.0 });
|
||||
let ribs = match flat.pts.len() {
|
||||
0 => continue,
|
||||
// A subpath of no length: a dot (round and projecting caps paint it).
|
||||
1 => {
|
||||
let (d, b, e) = (Vec2::new(1.0, 0.0), lay.band(0.0, side), lay.ext.0.max(lay.ext.1));
|
||||
let r = lay.cover(0.0);
|
||||
vec![Rib::square(pts[0] - d * e, d, r, 0.0, b), Rib::square(pts[0] + d * e, d, r, 0.0, b)]
|
||||
vec![Rib::square(flat.pts[0] - d * e, d, r, 0.0, b), Rib::square(flat.pts[0] + d * e, d, r, 0.0, b)]
|
||||
}
|
||||
_ => {
|
||||
let total = length(&pts, closed);
|
||||
let total = length(&flat.pts, closed);
|
||||
if total <= 1e-12 {
|
||||
continue;
|
||||
}
|
||||
let steps = if max_dt < 1.0 { (1.0 / max_dt).ceil() as usize } else { 1 };
|
||||
let profile = st.profile.iter().flat_map(|p| p.points.iter().map(|q| q.0));
|
||||
let heads = if closed { (0.0, 0.0) } else { heads };
|
||||
// A tip-aligned arrowhead shortens the line it sits on, and the stroke's outline
|
||||
// reads the width profile along the shortened line (`stroke_pieces`); the ribs
|
||||
// must read it there too, or on a profile that changes fast they cover a narrower
|
||||
// band than the stroke paints.
|
||||
let span = if closed { (0.0, total) } else { (trims.0.min(total), (total - trims.1).max(trims.0)) };
|
||||
// Where an arrowhead's reach ends gets a rib of its own, so the ribs that reach
|
||||
// across a head run all the way there instead of narrowing towards the next corner
|
||||
// from the head's tip, which leaves the corners of its base outside (#5964).
|
||||
@@ -200,11 +213,11 @@ fn strips(bp: &BezPath, rule: FillRule, st: &StrokeLayer, tol: f64, margin: f64,
|
||||
.copied()
|
||||
.chain(profile)
|
||||
.chain((1..steps).map(|i| i as f64 / steps as f64))
|
||||
.map(|t| t * total)
|
||||
.map(|t| span.0 + (span.1 - span.0) * t)
|
||||
.chain(head_ends)
|
||||
.collect();
|
||||
let (pts, _) = split_at(&pts, &corner, closed, &at);
|
||||
strip(&lay, &pts, closed, total, side, heads)
|
||||
let (pts, _, _) = split_at(&flat, closed, &at, &segs);
|
||||
strip(&lay, &pts, closed, total, side, heads, span)
|
||||
}
|
||||
};
|
||||
out.push(ribs);
|
||||
@@ -226,8 +239,9 @@ fn band_on_left(bp: &BezPath, rule: FillRule, pts: &[Point], align: StrokeAlign,
|
||||
}
|
||||
|
||||
/// The ribs along one flattened subpath `pts` of length `total` (`heads`: how far arrowheads
|
||||
/// reach from its start and end).
|
||||
fn strip(lay: &Lay, pts: &[Point], closed: bool, total: f64, side: Option<bool>, heads: (f64, f64)) -> Vec<Rib> {
|
||||
/// reach from its start and end). `span` is the stretch of the subpath the width profile is laid
|
||||
/// along: the whole of it, or what is left between tip-aligned arrowheads.
|
||||
fn strip(lay: &Lay, pts: &[Point], closed: bool, total: f64, side: Option<bool>, heads: (f64, f64), span: (f64, f64)) -> Vec<Rib> {
|
||||
let n = pts.len();
|
||||
let seg_count = if closed { n } else { n - 1 };
|
||||
let dir: Vec<Vec2> = (0..seg_count).map(|i| unit(pts[(i + 1) % n] - pts[i])).collect();
|
||||
@@ -249,7 +263,9 @@ fn strip(lay: &Lay, pts: &[Point], closed: bool, total: f64, side: Option<bool>,
|
||||
// stroke is wide anywhere: where a stroke crosses or runs close by itself, a narrow part
|
||||
// reaching that far would paint over a wide part beside it (#5960). The start of a closed
|
||||
// subpath is also its end. Ribs near an arrowhead reach across it.
|
||||
let cover: Vec<f64> = (0..n).map(|i| if closed && i == 0 { lay.cover(0.0).max(lay.cover(1.0)) } else { lay.cover(along[i] / total) }).collect();
|
||||
// The fraction of the profile at how far `s` is along the subpath: the profile spans `span`.
|
||||
let t_at = |s: f64| if span.1 > span.0 { ((s - span.0) / (span.1 - span.0)).clamp(0.0, 1.0) } else { 0.0 };
|
||||
let cover: Vec<f64> = (0..n).map(|i| if closed && i == 0 { lay.cover(0.0).max(lay.cover(1.0)) } else { lay.cover(t_at(along[i])) }).collect();
|
||||
let window = match lay.st.dash {
|
||||
Some(_) if lay.st.profile.is_some() => cap_extent(lay.st.cap, 2.0 * lay.hw * lay.st.profile_max()),
|
||||
_ => 0.0,
|
||||
@@ -279,7 +295,7 @@ fn strip(lay: &Lay, pts: &[Point], closed: bool, total: f64, side: Option<bool>,
|
||||
let (first, last) = if closed { (0, n) } else { (1, n - 1) };
|
||||
for i in first..last {
|
||||
let v = i % n;
|
||||
let t = along[v] / total;
|
||||
let t = t_at(along[v]);
|
||||
let ribs = lay.turn(pts[v], dir[(v + seg_count - 1) % seg_count], dir[v % seg_count], reach(v), t, lay.band(t, side));
|
||||
// A closed subpath starts after the fan of its first corner and ends with it.
|
||||
let skip = if closed && i == 0 { ribs.len() - 1 } else { 0 };
|
||||
|
||||
@@ -232,16 +232,14 @@ fn line_outline_uses_the_profile_dashes_or_a_plain_stroke() {
|
||||
let plain = line_outline(&l, &stroke(10.0, |_| {}), 10.0, 1e-3);
|
||||
assert!(near(plain.area().abs(), 1000.0, 1e-6));
|
||||
let lens = line_outline(&l, &stroke(10.0, |s| s.profile = Some(WidthProfile::lens())), 10.0, 1e-3);
|
||||
// The lens swells on a cubic, flat at the middle: 7/12 of the full-width band (#5714).
|
||||
assert!(near(lens.area().abs(), 1000.0 * 7.0 / 12.0, 0.05));
|
||||
// Each dash takes its widths from where it sits along the lens (0 → 1 → 0). Half of the lens
|
||||
// rises as w(u) = u + u² − u³ (u from 0 to 1 across that half): the dash over 0..25 covers
|
||||
// its first half, ∫₀^½ w = 29/192, the one over 50..75 the wider other half, 83/192.
|
||||
// The lens is a parabola between its ends: 2/3 of the full-width band.
|
||||
assert!(near(lens.area().abs(), 2000.0 / 3.0, 0.1));
|
||||
// Each dash takes its widths from where it sits along the lens (0 → 1 → 0).
|
||||
let st = stroke(10.0, |s| {
|
||||
s.profile = Some(WidthProfile::lens());
|
||||
s.dash = Some(dashed(&[25.0, 25.0], 0.0));
|
||||
});
|
||||
assert!(near(line_outline(&l, &st, 10.0, 1e-3).area().abs(), 500.0 * (29.0 + 83.0) / 192.0, 0.05));
|
||||
assert!(near(line_outline(&l, &st, 10.0, 1e-3).area().abs(), 1000.0 / 3.0, 0.1));
|
||||
assert!(line_outline(&BezPath::new(), &st, 10.0, 1e-3).elements().is_empty());
|
||||
}
|
||||
|
||||
|
||||
@@ -155,9 +155,10 @@ fn each_dash_takes_its_width_from_the_profile_along_the_whole_path() {
|
||||
s.dash = Some(Dash { pattern: vec![10.0, 10.0], offset: 0.0, align_corners: false });
|
||||
});
|
||||
let o = line_outline(&line(100.0), &st, 10.0, 1e-3);
|
||||
// The dash over 40..50 is near the lens's widest point; the one over 0..10 near its tip.
|
||||
// The dash over 40..50 is near the lens's widest point; the one over 0..10 near its tip,
|
||||
// where the smooth profile is thinner than a straight ramp (a parabola's first tenth).
|
||||
assert!(o.contains(Point::new(45.0, 4.0)) && o.contains(Point::new(45.0, -4.0)), "middle dash is wide");
|
||||
assert!(!o.contains(Point::new(5.0, 2.0)) && o.contains(Point::new(5.0, 0.3)), "first dash is thin");
|
||||
assert!(o.contains(Point::new(5.0, 0.8)) && !o.contains(Point::new(5.0, 1.2)), "first dash is thin");
|
||||
assert!(!o.contains(Point::new(15.0, 0.0)), "gaps stay empty");
|
||||
// Dots too: a round dot near the middle is bigger than one near an end.
|
||||
let st = stroke(10.0, |s| {
|
||||
@@ -166,7 +167,7 @@ fn each_dash_takes_its_width_from_the_profile_along_the_whole_path() {
|
||||
s.dash = Some(Dash { pattern: vec![0.0, 10.0], offset: 0.0, align_corners: false });
|
||||
});
|
||||
let o = line_outline(&line(100.0), &st, 10.0, 1e-3);
|
||||
assert!(o.contains(Point::new(50.0, 4.5)) && !o.contains(Point::new(10.0, 1.5)) && o.contains(Point::new(10.0, 0.5)));
|
||||
assert!(o.contains(Point::new(50.0, 4.5)) && o.contains(Point::new(10.0, 1.4)) && !o.contains(Point::new(10.0, 2.0)));
|
||||
}
|
||||
|
||||
/// An L whose corner turns 90° at (100, 0), or a sharp V turning back at (100, 0).
|
||||
|
||||
@@ -138,9 +138,8 @@ fn across_runs_from_the_left_edge_to_the_right() {
|
||||
let mut lens = st.clone();
|
||||
lens.profile = Some(WidthProfile::lens());
|
||||
let sl = slices(&line, &lens);
|
||||
// A quarter of the way along, the lens is 0.625 as wide (its curve, #5714): its top edge is
|
||||
// 3.125 up.
|
||||
assert!(param(&sl, Point::new(25.0, -3.0)).unwrap() < 0.05);
|
||||
// A quarter of the way along, the lens is three quarters as wide: its top edge is 3.75 up.
|
||||
assert!(param(&sl, Point::new(25.0, -3.6)).unwrap() < 0.05);
|
||||
assert!((param(&sl, Point::new(50.0, -2.5)).unwrap() - 0.25).abs() < 0.02);
|
||||
}
|
||||
|
||||
|
||||
+789
-187
File diff suppressed because it is too large.
Load diff
@@ -749,6 +749,7 @@ pub(crate) fn unique_name_from(base: &str, next: &mut std::collections::HashMap<
|
||||
/// The most presets one presets file imports: each is looked up among the saved ones.
|
||||
pub(crate) const MAX_IMPORTED_PRESETS: usize = 1000;
|
||||
|
||||
pub(crate) use addons::DecodedImages;
|
||||
pub use docmenu::guide_indexes;
|
||||
pub(crate) use edit::{settle_interaction, typing_in_progress};
|
||||
pub(crate) use object::bounds_rewrite;
|
||||
|
||||
@@ -6,12 +6,15 @@ mod cleanup;
|
||||
mod color;
|
||||
mod contour;
|
||||
mod document;
|
||||
mod image;
|
||||
mod pathtools;
|
||||
mod pen;
|
||||
mod select;
|
||||
mod shapes;
|
||||
mod typetools;
|
||||
|
||||
pub(crate) use image::DecodedImages;
|
||||
|
||||
use super::CommandSpec;
|
||||
|
||||
/// The commands of every built-in add-on.
|
||||
@@ -21,6 +24,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
v.extend(color::specs());
|
||||
v.extend(contour::specs());
|
||||
v.extend(document::specs());
|
||||
v.extend(image::specs());
|
||||
v.extend(pathtools::specs());
|
||||
v.extend(pen::specs());
|
||||
v.extend(select::specs());
|
||||
|
||||
@@ -0,0 +1,598 @@
|
||||
//! Image Tools (`addon.image.*`), a built-in add-on. Remove Background makes the background of the
|
||||
//! selected images transparent in their own pixels, as one undo step. The background is the most
|
||||
//! common color along an image's edges, the average of its four corners, or colors the caller
|
||||
//! picks. It is cleared where it reaches in from an edge, or wherever it is. The work is done here,
|
||||
//! on the pixels; nothing is sent anywhere. It is a VectorCraft addition. Its submenu (Object ›
|
||||
//! Plug-ins › Image Tools) and dialog are the UI's.
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::io::Cursor;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
use image::{DynamicImage, ImageBuffer, LumaA, Rgba};
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::Color;
|
||||
use vectorcraft_doc::{ImageBlob, NodeId, NodeKind};
|
||||
use vectorcraft_geom::Affine;
|
||||
|
||||
use super::super::fileio::{MAX_RASTER_ALLOC, ppi};
|
||||
use super::super::menucmds::has_image;
|
||||
use super::super::{CommandSpec, Result, Session, bad, checked_ids_param, cmd, hex_with_alpha, number_or, switch_or, text_param};
|
||||
|
||||
/// Remove Background's command.
|
||||
pub const REMOVE_BACKGROUND: &str = "addon.image.removeBackground";
|
||||
const C: &str = REMOVE_BACKGROUND;
|
||||
|
||||
/// The most colors `colors` lists.
|
||||
const MAX_COLORS: usize = 16;
|
||||
|
||||
/// Bytes a pixel takes while it is worked on, beside its pixels: its level, whether the edge search
|
||||
/// reached it, and its place in the search's stack (4 bytes, twice that while the stack grows).
|
||||
const WORK_BYTES: u64 = 10;
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![cmd!(
|
||||
"addon.image.removeBackground",
|
||||
"Remove Background…",
|
||||
["Object", "Plug-ins", "Image Tools"],
|
||||
None,
|
||||
"{background?: \"edges\" (default: the most common color along the image's edges; nothing is cleared when the edges hold at least as many clear pixels as pixels of that color) | \"corners\" (the average of the image's four corner pixels that are not clear; nothing is cleared when all four are clear) | \"colors\" (the colors in `colors`), colors?: [color…] or one string of them split by commas or spaces (1 to 16 colors, each a hex color \"#rrggbb\" or \"#rgb\" with or without the # or one of black, white, red, green and blue; needed with background: colors, and checked but not used with edges or corners), contiguous?: bool (true: only background pixels that reach in from an edge are cleared; false: they are cleared wherever they are; true), match?: \"distance\" (default: a color's RGB distance from a background color) | \"channels\" (its largest difference on one of red, green and blue), tolerance?: 0..100 (with distance, % of the largest RGB distance; with channels, % of 255 steps on one channel; a pixel this near a background color is cleared; 10), softness?: 0..100 (% past the tolerance over which pixels fade from cleared to kept; 10), ids?: [id…] (default: the selected images)} make the background of each image transparent in its own pixels, as one undo step: the image stays an image, embedded as a PNG with an alpha channel at the depth of its pixels (16-bit images keep 16 bits and grayscale images stay gray) with the source's color profile and resolution, and a linked image is embedded; a CMYK image, a linked image that shows its preview (embed or relink it first), an image too large to change, and a result with no pixel left are refused, and an image whose pixels do not change is left as it is (no undo step when none changes) → {changed: the images changed, ids, cleared: the pixels made fully clear in them}",
|
||||
has_image,
|
||||
remove_background
|
||||
)]
|
||||
}
|
||||
|
||||
/// Where the background's colors come from.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) enum Background {
|
||||
/// The most common color along the image's edges.
|
||||
Edges,
|
||||
/// The average of the image's four corner pixels that are not clear.
|
||||
Corners,
|
||||
/// These colors.
|
||||
Colors(Vec<[u8; 3]>),
|
||||
}
|
||||
|
||||
/// How near a color is to a background color.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(crate) enum Match {
|
||||
/// The RGB distance, as a percentage of the largest one (black to white).
|
||||
Distance,
|
||||
/// The largest difference on one of red, green and blue, as a percentage of a channel's range.
|
||||
Channels,
|
||||
}
|
||||
|
||||
/// What Remove Background clears: the background, whether only where it reaches in from an edge,
|
||||
/// and how near a color must be to it (a percentage, measured as `measure` says) to be cleared, and
|
||||
/// to fade out past that.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) struct Settings {
|
||||
pub background: Background,
|
||||
pub contiguous: bool,
|
||||
pub measure: Match,
|
||||
pub tolerance: f64,
|
||||
pub softness: f64,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
/// The settings `p` gives, each checked.
|
||||
fn read(p: &Value) -> Result<Self> {
|
||||
let percent = |key: &str| -> Result<f64> {
|
||||
let v = number_or(p, key, 10.0, C)?;
|
||||
if v.is_finite() && (0.0..=100.0).contains(&v) { Ok(v) } else { Err(bad(C, format!("`{key}` must be from 0 to 100"))) }
|
||||
};
|
||||
let (tolerance, softness) = (percent("tolerance")?, percent("softness")?);
|
||||
let colors = match p.get("colors") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(v) => Some(colors(v)?),
|
||||
};
|
||||
let background = match text_param(p, "background", C)? {
|
||||
None | Some("edges") => Background::Edges,
|
||||
Some("corners") => Background::Corners,
|
||||
Some("colors") => Background::Colors(colors.ok_or_else(colors_error)?),
|
||||
Some(_) => return Err(bad(C, "`background` must be edges, corners or colors")),
|
||||
};
|
||||
let measure = match text_param(p, "match", C)? {
|
||||
None | Some("distance") => Match::Distance,
|
||||
Some("channels") => Match::Channels,
|
||||
Some(_) => return Err(bad(C, "`match` must be distance or channels")),
|
||||
};
|
||||
let contiguous = switch_or(p, "contiguous", true, C)?;
|
||||
Ok(Self { background, contiguous, measure, tolerance, softness })
|
||||
}
|
||||
}
|
||||
|
||||
fn colors_error() -> crate::EngineError {
|
||||
bad(C, "`colors` must list 1 to 16 colors, each hex (#rrggbb or #rgb) or black, white, red, green or blue")
|
||||
}
|
||||
|
||||
/// The colors `v` lists: an array of strings, or one string of them split by commas or spaces.
|
||||
/// Each is read as [`Color::from_hex`] reads it; one with an alpha part is refused.
|
||||
fn colors(v: &Value) -> Result<Vec<[u8; 3]>> {
|
||||
// One past the most is enough to tell a list that is too long.
|
||||
let texts: Vec<&str> = match v {
|
||||
Value::String(s) => s.split([',', ' ', '\t', '\n']).filter(|t| !t.is_empty()).take(MAX_COLORS + 1).collect(),
|
||||
Value::Array(a) => a.iter().take(MAX_COLORS + 1).map(Value::as_str).collect::<Option<_>>().ok_or_else(colors_error)?,
|
||||
_ => return Err(colors_error()),
|
||||
};
|
||||
if texts.is_empty() || texts.len() > MAX_COLORS {
|
||||
return Err(colors_error());
|
||||
}
|
||||
texts.into_iter().map(|t| rgb8(t).ok_or_else(colors_error)).collect()
|
||||
}
|
||||
|
||||
/// The 8-bit RGB of the color `t` writes.
|
||||
fn rgb8(t: &str) -> Option<[u8; 3]> {
|
||||
if hex_with_alpha(t) {
|
||||
return None;
|
||||
}
|
||||
let [r, g, b] = Color::from_hex(t)?.to_rgb_uncalibrated();
|
||||
Some([r, g, b].map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8))
|
||||
}
|
||||
|
||||
fn remove_background(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let settings = Settings::read(p)?;
|
||||
// A preview step keeps the pixels it decodes for the next step; other runs keep none.
|
||||
let mut kept = std::mem::take(&mut s.decoded_images);
|
||||
let previewing = s.previewing;
|
||||
let found = changes(s, p, &settings, previewing.then_some(&mut kept));
|
||||
s.decoded_images = kept;
|
||||
let changes = found?;
|
||||
let ids: Vec<u64> = changes.iter().map(|(id, ..)| id.0).collect();
|
||||
let made_clear: u64 = changes.iter().map(|(_, out, _)| out.cleared).sum();
|
||||
if !changes.is_empty() {
|
||||
s.edit("Remove Background", |d, _| {
|
||||
for (id, out, xf) in changes {
|
||||
let key = out.blob.content_key();
|
||||
d.images.entry(key.clone()).or_insert(out.blob);
|
||||
if let Some(NodeKind::Image(im)) = d.node_mut(id).map(|n| &mut n.kind) {
|
||||
im.key = key;
|
||||
(im.width, im.height, im.xf) = (out.width, out.height, xf);
|
||||
im.link = None;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
Ok(json!({ "changed": ids.len(), "ids": ids, "cleared": made_clear }))
|
||||
}
|
||||
|
||||
/// The images `p` names (`ids`, or the selected images) whose pixels change, each with its pixels
|
||||
/// cleared as `settings` say and the transform that keeps its place and size. Decoded pixels are
|
||||
/// taken from and kept in `kept` when it is given.
|
||||
fn changes(s: &Session, p: &Value, settings: &Settings, mut kept: Option<&mut DecodedImages>) -> Result<Vec<(NodeId, Cleared, Affine)>> {
|
||||
let st = s.doc()?;
|
||||
let ids: Vec<NodeId> = if p.get("ids").is_some_and(|v| !v.is_null()) {
|
||||
// Each image once, however often `ids` names it.
|
||||
let mut given = HashSet::new();
|
||||
let ids: Vec<NodeId> = checked_ids_param(s, p, "ids", C)?.into_iter().filter(|id| given.insert(*id)).collect();
|
||||
if let Some(id) = ids.iter().find(|id| !matches!(st.doc.node(**id).map(|n| &n.kind), Some(NodeKind::Image(_)))) {
|
||||
return Err(bad(C, format!("object {} is not an image", id.0)));
|
||||
}
|
||||
ids
|
||||
} else {
|
||||
st.selection.objects.iter().copied().filter(|id| matches!(st.doc.node(*id).map(|n| &n.kind), Some(NodeKind::Image(_)))).collect()
|
||||
};
|
||||
if ids.is_empty() {
|
||||
return Err(bad(C, "select an image"));
|
||||
}
|
||||
// Every image is checked before any is decoded.
|
||||
let mut images = vec![];
|
||||
for id in ids {
|
||||
let Some(NodeKind::Image(im)) = st.doc.node(id).map(|n| &n.kind) else { continue };
|
||||
let blob = st.doc.images.get(&im.key).ok_or_else(|| bad(C, "the image's pixels are missing"))?;
|
||||
// The preview stands in for a file that hasn't been read, or that is kept at its preview.
|
||||
if im.link.is_some() && blob.is_proxy() {
|
||||
return Err(bad(C, "the linked image shows its preview: embed or relink it first"));
|
||||
}
|
||||
if blob.is_cmyk() {
|
||||
return Err(bad(C, "a CMYK image has no transparency to set"));
|
||||
}
|
||||
images.push((id, im, blob));
|
||||
}
|
||||
// Each image's pixels once, however many objects show them.
|
||||
let mut cleared: HashMap<&str, Option<Cleared>> = HashMap::new();
|
||||
let mut changes = vec![];
|
||||
for (id, im, blob) in images {
|
||||
let out = match cleared.get(im.key.as_str()) {
|
||||
Some(out) => out.clone(),
|
||||
None => {
|
||||
let out = clear(blob, settings, kept.as_deref_mut())?;
|
||||
cleared.insert(&im.key, out.clone());
|
||||
out
|
||||
}
|
||||
};
|
||||
if let Some(out) = out {
|
||||
// A file read at another size than the object's keeps the object's place and size.
|
||||
let xf = if (out.width, out.height) == (im.width, im.height) {
|
||||
im.xf
|
||||
} else {
|
||||
im.xf * Affine::scale_non_uniform(f64::from(im.width) / f64::from(out.width), f64::from(im.height) / f64::from(out.height))
|
||||
};
|
||||
changes.push((id, out, xf));
|
||||
}
|
||||
}
|
||||
Ok(changes)
|
||||
}
|
||||
|
||||
/// An image with its background cleared: its PNG, its size in pixels, and how many of its pixels
|
||||
/// were made fully clear.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Cleared {
|
||||
pub blob: ImageBlob,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub cleared: u64,
|
||||
}
|
||||
|
||||
/// An image's pixels as straight RGBA, at 8 or 16 bits a sample.
|
||||
#[derive(Clone)]
|
||||
enum Pixels {
|
||||
Eight(image::RgbaImage),
|
||||
Sixteen(ImageBuffer<Rgba<u16>, Vec<u16>>),
|
||||
}
|
||||
|
||||
/// The most bytes of decoded pixels and ICC profiles [`DecodedImages`] keeps (128 MiB, or 32 MiB in
|
||||
/// the web app).
|
||||
const KEEP_BYTES: u64 = MAX_RASTER_ALLOC / 16;
|
||||
|
||||
/// The images Remove Background decoded during an interaction's preview steps, the most recently
|
||||
/// used first, at most [`KEEP_BYTES`] in all. The next preview step runs the command again on the
|
||||
/// same bytes and takes their pixels from here instead of decoding them again. The session holds
|
||||
/// it and empties it when the interaction ends (OK or Cancel). A run that is not a preview step
|
||||
/// does not read or fill it.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct DecodedImages(Vec<Decoded>);
|
||||
|
||||
/// An image decoded by [`clear`]: the bytes it was decoded from (held weakly, so they are not kept
|
||||
/// alive for it, and an entry whose bytes are gone is dropped), its pixels, whether it was gray,
|
||||
/// and its ICC profile.
|
||||
struct Decoded {
|
||||
source: Weak<Vec<u8>>,
|
||||
pixels: Arc<Pixels>,
|
||||
gray: bool,
|
||||
icc: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many times this thread decoded an image in [`clear`].
|
||||
pub(crate) static DECODES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
impl DecodedImages {
|
||||
/// Drops every image kept.
|
||||
pub(crate) fn empty(&mut self) {
|
||||
self.0.clear();
|
||||
}
|
||||
|
||||
/// How many images are kept.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
/// The pixels, gray flag and ICC profile kept for `bytes`, if they are kept. Images whose bytes
|
||||
/// are gone are dropped first.
|
||||
fn get(&mut self, bytes: &Arc<Vec<u8>>) -> Option<(Arc<Pixels>, bool, Option<Vec<u8>>)> {
|
||||
self.0.retain(|d| d.source.strong_count() > 0);
|
||||
let at = self.0.iter().position(|d| std::ptr::eq(d.source.as_ptr(), Arc::as_ptr(bytes)))?;
|
||||
let d = self.0.remove(at);
|
||||
let found = (Arc::clone(&d.pixels), d.gray, d.icc.clone());
|
||||
self.0.insert(0, d);
|
||||
Some(found)
|
||||
}
|
||||
|
||||
/// Keeps `pixels` decoded from `bytes` first, in place of anything kept for the same bytes, and
|
||||
/// drops images whose bytes are gone and the least recently used past [`KEEP_BYTES`].
|
||||
fn keep(&mut self, bytes: &Arc<Vec<u8>>, pixels: Arc<Pixels>, gray: bool, icc: Option<Vec<u8>>) {
|
||||
self.0.retain(|d| d.source.strong_count() > 0 && !std::ptr::eq(d.source.as_ptr(), Arc::as_ptr(bytes)));
|
||||
self.0.insert(0, Decoded { source: Arc::downgrade(bytes), pixels, gray, icc });
|
||||
let mut total = 0u64;
|
||||
self.0.retain(|d| {
|
||||
total = total.saturating_add(d.bytes());
|
||||
total <= KEEP_BYTES
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Decoded {
|
||||
/// How many bytes the entry keeps: its pixels and its ICC profile.
|
||||
fn bytes(&self) -> u64 {
|
||||
self.pixels.bytes().saturating_add(self.icc.as_ref().map_or(0, |icc| icc.len() as u64))
|
||||
}
|
||||
}
|
||||
|
||||
impl Pixels {
|
||||
/// How many bytes the pixels take.
|
||||
fn bytes(&self) -> u64 {
|
||||
match self {
|
||||
Self::Eight(img) => img.as_raw().len() as u64,
|
||||
Self::Sixteen(img) => img.as_raw().len() as u64 * 2,
|
||||
}
|
||||
}
|
||||
|
||||
/// The width and height in pixels.
|
||||
fn dimensions(&self) -> (u32, u32) {
|
||||
match self {
|
||||
Self::Eight(img) => img.dimensions(),
|
||||
Self::Sixteen(img) => img.dimensions(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Pixel `i` (row by row from the top left) at 16 bits a sample (an 8-bit sample `v` is
|
||||
/// `v * 257`).
|
||||
fn rgba16(&self, i: usize) -> [u16; 4] {
|
||||
let mut out = [0u16; 4];
|
||||
match self {
|
||||
Self::Eight(img) => {
|
||||
for (o, v) in out.iter_mut().zip(img.as_raw().get(i * 4..i * 4 + 4).unwrap_or_default()) {
|
||||
*o = u16::from(*v) * 257;
|
||||
}
|
||||
}
|
||||
Self::Sixteen(img) => {
|
||||
for (o, v) in out.iter_mut().zip(img.as_raw().get(i * 4..i * 4 + 4).unwrap_or_default()) {
|
||||
*o = *v;
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// `blob` with its background cleared as `settings` say, or `None` when no pixel changes. A CMYK
|
||||
/// image, one that can't be decoded or is too large to change, and a result with no pixel left are
|
||||
/// errors. The PNG keeps the source's ICC profile and resolution. The decoded pixels are taken
|
||||
/// from and kept in `kept` when it is given.
|
||||
pub(crate) fn clear(blob: &ImageBlob, settings: &Settings, mut kept: Option<&mut DecodedImages>) -> Result<Option<Cleared>> {
|
||||
if blob.is_cmyk() {
|
||||
return Err(bad(C, "a CMYK image has no transparency to set"));
|
||||
}
|
||||
let found = kept.as_deref_mut().and_then(|k| k.get(&blob.bytes));
|
||||
let (source, gray, icc) = match found {
|
||||
Some(found) => found,
|
||||
None => decode(blob, kept)?,
|
||||
};
|
||||
// The pixels kept for the next run stay as they are; ones not kept are worked on in place.
|
||||
let mut px = Arc::try_unwrap(source).unwrap_or_else(|kept| Pixels::clone(&kept));
|
||||
let (w, h) = px.dimensions();
|
||||
let levels = levels(w as usize, h as usize, &|i| px.rgba16(i), settings);
|
||||
let Some(cleared) = apply(&mut px, &levels) else { return Ok(None) };
|
||||
let left = match &px {
|
||||
Pixels::Eight(img) => img.pixels().any(|p| p.0[3] > 0),
|
||||
Pixels::Sixteen(img) => img.pixels().any(|p| p.0[3] > 0),
|
||||
};
|
||||
if !left {
|
||||
return Err(bad(C, "every pixel matches the background: no image would be left"));
|
||||
}
|
||||
let out = match (px, gray) {
|
||||
(Pixels::Eight(img), false) => DynamicImage::ImageRgba8(img),
|
||||
(Pixels::Sixteen(img), false) => DynamicImage::ImageRgba16(img),
|
||||
(Pixels::Eight(img), true) => {
|
||||
DynamicImage::ImageLumaA8(ImageBuffer::from_fn(w, h, |x, y| LumaA([img.get_pixel(x, y).0[0], img.get_pixel(x, y).0[3]])))
|
||||
}
|
||||
(Pixels::Sixteen(img), true) => {
|
||||
DynamicImage::ImageLumaA16(ImageBuffer::from_fn(w, h, |x, y| LumaA([img.get_pixel(x, y).0[0], img.get_pixel(x, y).0[3]])))
|
||||
}
|
||||
};
|
||||
let mut png = Vec::new();
|
||||
let mut encoder = image::codecs::png::PngEncoder::new(&mut png);
|
||||
if let Some(icc) = icc {
|
||||
use image::ImageEncoder as _;
|
||||
encoder.set_icc_profile(icc).map_err(|e| bad(C, e.to_string()))?;
|
||||
}
|
||||
out.write_with_encoder(encoder).map_err(|e| bad(C, e.to_string()))?;
|
||||
// The resolution the file declares, which sizes the image at 100% when it is replaced or relinked.
|
||||
if let Some(r) = ppi::resolution(&blob.bytes) {
|
||||
png = ppi::with_png_resolution(&png, r);
|
||||
}
|
||||
Ok(Some(Cleared { blob: ImageBlob::png(png), width: w, height: h, cleared }))
|
||||
}
|
||||
|
||||
/// `blob`'s pixels as straight RGBA, whether they were gray, and its ICC profile. An image that
|
||||
/// can't be decoded or is too large to change is an error. The pixels are kept in `kept`, when it
|
||||
/// is given, if a copy of them fits the decode allowance beside the work.
|
||||
fn decode(blob: &ImageBlob, kept: Option<&mut DecodedImages>) -> Result<(Arc<Pixels>, bool, Option<Vec<u8>>)> {
|
||||
let reader =
|
||||
|| image::ImageReader::new(Cursor::new(blob.bytes.as_slice())).with_guessed_format().map_err(|_| bad(C, "the image's pixels can't be read"));
|
||||
let mut decoder = reader()?.into_decoder().map_err(|e| bad(C, format!("the image's pixels can't be read: {e}")))?;
|
||||
// The ICC profile the colors are in goes with them into the PNG.
|
||||
let (w, h, kind, icc) = {
|
||||
use image::ImageDecoder as _;
|
||||
let (w, h) = decoder.dimensions();
|
||||
(w, h, decoder.color_type(), decoder.icc_profile().ok().flatten())
|
||||
};
|
||||
drop(decoder);
|
||||
// The file's pixels, those worked on (as many again for the PNG made of them) and the work,
|
||||
// checked from the header before anything is decoded.
|
||||
let deep = kind.bytes_per_pixel() > kind.channel_count();
|
||||
let per_pixel = u64::from(kind.bytes_per_pixel()) + 2 * if deep { 8 } else { 4 } + WORK_BYTES;
|
||||
if u64::from(w).checked_mul(u64::from(h)).and_then(|n| n.checked_mul(per_pixel)).is_none_or(|n| n > MAX_RASTER_ALLOC) {
|
||||
return Err(bad(C, format!("an image of {w} × {h} pixels is too large to change")));
|
||||
}
|
||||
let mut reader = reader()?;
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(MAX_RASTER_ALLOC);
|
||||
reader.limits(limits);
|
||||
let img = reader.decode().map_err(|e| bad(C, format!("the image's pixels can't be read: {e}")))?;
|
||||
#[cfg(test)]
|
||||
DECODES.with(|c| c.set(c.get() + 1));
|
||||
let gray = matches!(img.color(), image::ColorType::L8 | image::ColorType::La8 | image::ColorType::L16 | image::ColorType::La16);
|
||||
let px = Arc::new(if deep { Pixels::Sixteen(img.into_rgba16()) } else { Pixels::Eight(img.into_rgba8()) });
|
||||
// A kept copy is worked on as a copy: one more set of the pixels worked on.
|
||||
let with_copy = per_pixel + if deep { 8 } else { 4 };
|
||||
if let Some(kept) = kept
|
||||
&& u64::from(w).checked_mul(u64::from(h)).and_then(|n| n.checked_mul(with_copy)).is_some_and(|n| n <= MAX_RASTER_ALLOC)
|
||||
&& px.bytes().saturating_add(icc.as_ref().map_or(0, |icc| icc.len() as u64)) <= KEEP_BYTES
|
||||
{
|
||||
kept.keep(&blob.bytes, Arc::clone(&px), gray, icc.clone());
|
||||
}
|
||||
Ok((px, gray, icc))
|
||||
}
|
||||
|
||||
/// Each pixel's alpha scaled by its level (255 keeps it). Returns how many pixels were made fully
|
||||
/// clear, or `None` when no pixel changed.
|
||||
fn apply(px: &mut Pixels, levels: &[u8]) -> Option<u64> {
|
||||
let (mut changed, mut cleared) = (false, 0u64);
|
||||
match px {
|
||||
Pixels::Eight(img) => {
|
||||
for (p, &l) in img.pixels_mut().zip(levels) {
|
||||
let a = ((u32::from(p.0[3]) * u32::from(l) + 127) / 255) as u8;
|
||||
changed |= a != p.0[3];
|
||||
cleared += u64::from(a == 0 && p.0[3] != 0);
|
||||
p.0[3] = a;
|
||||
}
|
||||
}
|
||||
Pixels::Sixteen(img) => {
|
||||
for (p, &l) in img.pixels_mut().zip(levels) {
|
||||
let a = ((u32::from(p.0[3]) * u32::from(l) + 127) / 255) as u16;
|
||||
changed |= a != p.0[3];
|
||||
cleared += u64::from(a == 0 && p.0[3] != 0);
|
||||
p.0[3] = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
changed.then_some(cleared)
|
||||
}
|
||||
|
||||
/// How much of each of the `w` × `h` pixels `px` gives (row by row, straight RGBA at 16 bits a
|
||||
/// sample) stays, from 0 (cleared) to 255 (kept), as `settings` say. Each pixel's distance to the
|
||||
/// background is worked out once. A pixel past the first `u32::MAX` is left kept by the edge search.
|
||||
pub(crate) fn levels(w: usize, h: usize, px: &dyn Fn(usize) -> [u16; 4], settings: &Settings) -> Vec<u8> {
|
||||
let n = w.saturating_mul(h);
|
||||
let refs: Vec<[u16; 3]> = match &settings.background {
|
||||
Background::Colors(refs) => refs.iter().map(|c| c.map(|v| u16::from(v) * 257)).collect(),
|
||||
Background::Edges => match edge_color(w, h, px) {
|
||||
Some(edge) => vec![edge],
|
||||
None => return vec![255; n],
|
||||
},
|
||||
Background::Corners => match corner_color(w, h, px) {
|
||||
Some(corner) => vec![corner],
|
||||
None => return vec![255; n],
|
||||
},
|
||||
};
|
||||
let mut out: Vec<u8> = (0..n)
|
||||
.map(|i| {
|
||||
let [r, g, b, _] = px(i);
|
||||
level([r, g, b], &refs, settings)
|
||||
})
|
||||
.collect();
|
||||
if settings.contiguous {
|
||||
// Out from the edges, through clear pixels and pixels that lose some alpha; the pixels it
|
||||
// doesn't reach are kept.
|
||||
let reaches = |out: &[u8], i: usize| out.get(i).is_some_and(|l| *l < 255) || px(i)[3] == 0;
|
||||
let mut seen = vec![false; n];
|
||||
let mut stack: Vec<u32> = vec![];
|
||||
for i in border(w, h).filter(|&i| reaches(&out, i)) {
|
||||
if let (Some(s), Ok(at)) = (seen.get_mut(i), u32::try_from(i)) {
|
||||
*s = true;
|
||||
stack.push(at);
|
||||
}
|
||||
}
|
||||
while let Some(at) = stack.pop() {
|
||||
let i = at as usize;
|
||||
let (x, y) = (i % w, i / w);
|
||||
let next = [(x > 0).then(|| i - 1), (x + 1 < w).then(|| i + 1), (y > 0).then(|| i - w), (y + 1 < h).then(|| i + w)];
|
||||
for j in next.into_iter().flatten() {
|
||||
if let Some(s) = seen.get_mut(j)
|
||||
&& !*s
|
||||
&& reaches(&out, j)
|
||||
&& let Ok(at) = u32::try_from(j)
|
||||
{
|
||||
*s = true;
|
||||
stack.push(at);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (l, s) in out.iter_mut().zip(&seen) {
|
||||
if !*s {
|
||||
*l = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The level of a pixel of color `c`: 0 within the tolerance of the nearest of `refs`, 255 from
|
||||
/// the tolerance plus the softness on, and in proportion between them.
|
||||
fn level(c: [u16; 3], refs: &[[u16; 3]], settings: &Settings) -> u8 {
|
||||
let Settings { tolerance, softness, measure, .. } = *settings;
|
||||
let d = refs.iter().map(|r| apart(c, *r, measure)).fold(f64::INFINITY, f64::min);
|
||||
if d <= tolerance {
|
||||
0
|
||||
} else if d >= tolerance + softness {
|
||||
255
|
||||
} else {
|
||||
((d - tolerance) / softness * 255.0).round().clamp(0.0, 255.0) as u8
|
||||
}
|
||||
}
|
||||
|
||||
/// How far apart `a` and `b` are, as a percentage, measured as `measure` says: the RGB distance of
|
||||
/// the largest one (black to white), or the largest difference on one channel of the channel's range.
|
||||
/// A difference of `k` 8-bit steps on one channel is exactly `k / 255 × 100` with channels.
|
||||
fn apart(a: [u16; 3], b: [u16; 3], measure: Match) -> f64 {
|
||||
match measure {
|
||||
Match::Distance => {
|
||||
let sq: f64 = a.iter().zip(b).map(|(x, y)| (f64::from(*x) - f64::from(y)).powi(2)).sum();
|
||||
sq.sqrt() / (65_535.0 * 3f64.sqrt()) * 100.0
|
||||
}
|
||||
Match::Channels => f64::from(a.iter().zip(b).map(|(x, y)| x.abs_diff(y)).max().unwrap_or(0)) / 65_535.0 * 100.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// The pixels on the edges of a `w` × `h` image, each once.
|
||||
fn border(w: usize, h: usize) -> impl Iterator<Item = usize> {
|
||||
let top = (0..w).filter(move |_| h > 0);
|
||||
let bottom = (0..w).filter(move |_| h > 1).map(move |x| (h - 1) * w + x);
|
||||
let sides = (1..h.saturating_sub(1)).flat_map(move |y| [Some(y * w), (w > 1).then(|| y * w + w - 1)].into_iter().flatten());
|
||||
top.chain(bottom).chain(sides)
|
||||
}
|
||||
|
||||
/// The most common color along an image's edges: its edge pixels grouped by 16 levels a channel,
|
||||
/// with the clear ones as a group of their own, as the average of the largest group. `None` when
|
||||
/// the clear edge pixels are at least as many as that group: the background is already cleared.
|
||||
fn edge_color(w: usize, h: usize, px: &dyn Fn(usize) -> [u16; 4]) -> Option<[u16; 3]> {
|
||||
let mut groups: HashMap<[u16; 3], (u64, [u64; 3])> = HashMap::new();
|
||||
let mut clear = 0u64;
|
||||
for i in border(w, h) {
|
||||
let [r, g, b, a] = px(i);
|
||||
if a == 0 {
|
||||
clear += 1;
|
||||
continue;
|
||||
}
|
||||
let e = groups.entry([r >> 12, g >> 12, b >> 12]).or_default();
|
||||
e.0 += 1;
|
||||
for (s, v) in e.1.iter_mut().zip([r, g, b]) {
|
||||
*s += u64::from(v);
|
||||
}
|
||||
}
|
||||
// Equal groups go by color, so the result doesn't depend on the map's order.
|
||||
let (_, (n, sums)) = groups.into_iter().max_by(|a, b| a.1.0.cmp(&b.1.0).then(b.0.cmp(&a.0)))?;
|
||||
if clear >= n {
|
||||
return None;
|
||||
}
|
||||
Some(sums.map(|s| ((s + n / 2) / n.max(1)) as u16))
|
||||
}
|
||||
|
||||
/// The average of the four corner pixels of a `w` × `h` image that are not clear (a corner of a
|
||||
/// one-pixel-wide image counts twice). `None` when all four are clear: the background is already
|
||||
/// cleared.
|
||||
fn corner_color(w: usize, h: usize, px: &dyn Fn(usize) -> [u16; 4]) -> Option<[u16; 3]> {
|
||||
if w == 0 || h == 0 {
|
||||
return None;
|
||||
}
|
||||
let corners = [0, w - 1, (h - 1) * w, (h - 1) * w + w - 1];
|
||||
let (mut n, mut sums) = (0u64, [0u64; 3]);
|
||||
for [r, g, b, a] in corners.map(px) {
|
||||
if a > 0 {
|
||||
n += 1;
|
||||
for (s, v) in sums.iter_mut().zip([r, g, b]) {
|
||||
*s += u64::from(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
(n > 0).then(|| sums.map(|s| ((s + n / 2) / n) as u16))
|
||||
}
|
||||
@@ -0,0 +1,480 @@
|
||||
use std::io::Cursor;
|
||||
use std::sync::Arc;
|
||||
|
||||
use image::{DynamicImage, ImageBuffer, Luma, Rgb};
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::{ImageBlob, ImageObject, LinkInfo, Node, NodeId, NodeKind};
|
||||
use vectorcraft_geom::Affine;
|
||||
|
||||
use super::{Background, Match, REMOVE_BACKGROUND as C, Settings, level, levels};
|
||||
use crate::Session;
|
||||
use crate::cmd::fileio::ppi;
|
||||
|
||||
const WHITE: [u8; 4] = [255, 255, 255, 255];
|
||||
const RED: [u8; 4] = [200, 0, 0, 255];
|
||||
|
||||
/// The command's defaults with `background`.
|
||||
fn settings(background: Background) -> Settings {
|
||||
Settings { background, contiguous: true, measure: Match::Distance, tolerance: 10.0, softness: 10.0 }
|
||||
}
|
||||
|
||||
fn png(img: DynamicImage) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
/// 12 × 10 pixels: white, with a red block from (3, 3) to (9, 7) that holds one white pixel at
|
||||
/// (6, 5), which no white edge pixel reaches.
|
||||
fn framed() -> image::RgbaImage {
|
||||
image::RgbaImage::from_fn(12, 10, |x, y| {
|
||||
let block = (3..9).contains(&x) && (3..7).contains(&y);
|
||||
image::Rgba(if block && (x, y) != (6, 5) { RED } else { WHITE })
|
||||
})
|
||||
}
|
||||
|
||||
/// A session with `bytes` opened as a document, its image selected.
|
||||
fn open(bytes: &[u8]) -> (Session, NodeId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("document.open", &json!({"name": "art.png", "dataBase64": vectorcraft_format::base64_encode(bytes)})).unwrap();
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
let id = s.doc().unwrap().selection.objects[0];
|
||||
(s, id)
|
||||
}
|
||||
|
||||
fn image_of(s: &Session, id: NodeId) -> ImageObject {
|
||||
let NodeKind::Image(im) = &s.doc().unwrap().doc.node(id).unwrap().kind else { panic!("an image") };
|
||||
(**im).clone()
|
||||
}
|
||||
|
||||
fn bytes_of(s: &Session, id: NodeId) -> Arc<Vec<u8>> {
|
||||
s.doc().unwrap().doc.images[&image_of(s, id).key].bytes.clone()
|
||||
}
|
||||
|
||||
fn pixels(s: &Session, id: NodeId) -> DynamicImage {
|
||||
image::load_from_memory(&bytes_of(s, id)).unwrap()
|
||||
}
|
||||
|
||||
fn alpha(img: &DynamicImage, x: u32, y: u32) -> u8 {
|
||||
img.to_rgba8().get_pixel(x, y).0[3]
|
||||
}
|
||||
|
||||
fn undo_len(s: &Session) -> usize {
|
||||
s.doc().unwrap().history.undo.len()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_edge_colors_are_cleared_where_they_reach_in_from_an_edge() {
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
let before = (image_of(&s, id), undo_len(&s));
|
||||
let r = s.execute(C, &json!({})).unwrap();
|
||||
assert_eq!(r, json!({"changed": 1, "ids": [id.0], "cleared": 12 * 10 - 6 * 4}));
|
||||
let out = pixels(&s, id);
|
||||
assert_eq!(out.color(), image::ColorType::Rgba8);
|
||||
assert_eq!((alpha(&out, 0, 0), alpha(&out, 11, 9), alpha(&out, 2, 4)), (0, 0, 0), "the white around the block");
|
||||
assert_eq!(out.to_rgba8().get_pixel(4, 4).0, RED, "the block");
|
||||
assert_eq!(alpha(&out, 6, 5), 255, "white inside the block, which no edge reaches");
|
||||
let after = image_of(&s, id);
|
||||
assert_eq!((after.width, after.height, after.xf), (12, 10, before.0.xf), "the same place and size");
|
||||
assert_eq!(undo_len(&s), before.1 + 1, "one undo step");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(image_of(&s, id), before.0);
|
||||
}
|
||||
|
||||
/// Picked colors, like the other backgrounds, are cleared where they reach in from an edge, and with
|
||||
/// `contiguous: false` wherever they are.
|
||||
#[test]
|
||||
fn picked_colors_are_cleared_from_the_edges_or_wherever_they_are() {
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
s.execute(C, &json!({"background": "colors", "colors": ["#fff"], "tolerance": 0, "softness": 0})).unwrap();
|
||||
let out = pixels(&s, id);
|
||||
assert_eq!((alpha(&out, 0, 0), alpha(&out, 6, 5), alpha(&out, 4, 4)), (0, 255, 255), "not inside the block");
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
s.execute(C, &json!({"background": "colors", "colors": ["#fff"], "contiguous": false, "tolerance": 0, "softness": 0})).unwrap();
|
||||
let out = pixels(&s, id);
|
||||
assert_eq!((alpha(&out, 0, 0), alpha(&out, 6, 5), alpha(&out, 4, 4)), (0, 0, 255), "inside the block too; the red stays");
|
||||
}
|
||||
|
||||
/// With `contiguous: false` the edge color is cleared inside the subject too.
|
||||
#[test]
|
||||
fn the_edge_color_can_be_cleared_wherever_it_is() {
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
let r = s.execute(C, &json!({"contiguous": false})).unwrap();
|
||||
assert_eq!(r["cleared"], 12 * 10 - 6 * 4 + 1);
|
||||
let out = pixels(&s, id);
|
||||
assert_eq!((alpha(&out, 0, 0), alpha(&out, 6, 5), alpha(&out, 4, 4)), (0, 0, 255));
|
||||
}
|
||||
|
||||
/// `colors` as one string split by commas and spaces, written as other color params take them.
|
||||
#[test]
|
||||
fn picked_colors_can_be_one_string_of_colors() {
|
||||
for colors in ["#00ff00, ffffff", "green white", "#0f0,#FFF"] {
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
s.execute(C, &json!({"background": "colors", "colors": colors, "contiguous": false, "tolerance": 0, "softness": 0})).unwrap();
|
||||
let out = pixels(&s, id);
|
||||
assert_eq!((alpha(&out, 0, 0), alpha(&out, 6, 5), alpha(&out, 4, 4)), (0, 0, 255), "{colors}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A subject along a whole edge stays: only the most common edge color is the background.
|
||||
#[test]
|
||||
fn a_subject_along_one_edge_is_not_the_background() {
|
||||
let img = image::RgbaImage::from_fn(10, 10, |_, y| image::Rgba(if y >= 7 { RED } else { WHITE }));
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(img)));
|
||||
s.execute(C, &json!({})).unwrap();
|
||||
let out = pixels(&s, id);
|
||||
assert_eq!((alpha(&out, 5, 0), alpha(&out, 5, 9), alpha(&out, 0, 8)), (0, 255, 255));
|
||||
}
|
||||
|
||||
/// The preview steps of one interaction, as the dialog's are, decode an image once and give what a
|
||||
/// run on freshly decoded pixels gives. The pixels are kept only until the interaction ends (OK or
|
||||
/// Cancel), and a run that is not a preview step keeps none.
|
||||
#[test]
|
||||
fn preview_steps_on_the_same_pixels_decode_them_once() {
|
||||
let decodes = || super::DECODES.with(std::cell::Cell::get);
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
let start = decodes();
|
||||
let p = json!({"background": "colors", "colors": "#c80000", "contiguous": false, "tolerance": 0, "softness": 0});
|
||||
s.begin_interaction("Remove Background").unwrap();
|
||||
s.preview(C, &json!({"tolerance": 0, "softness": 0})).unwrap();
|
||||
s.preview(C, &json!({"tolerance": 20, "softness": 0})).unwrap();
|
||||
s.preview(C, &p).unwrap();
|
||||
assert_eq!((decodes() - start, s.decoded_images.len()), (1, 1), "decoded once for three preview steps");
|
||||
s.commit_interaction().unwrap();
|
||||
assert_eq!(s.decoded_images.len(), 0, "OK lets the pixels go");
|
||||
let (mut fresh, fresh_id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
fresh.execute(C, &p).unwrap();
|
||||
assert_eq!((decodes() - start, fresh.decoded_images.len()), (2, 0), "a run that is not a preview step keeps nothing");
|
||||
assert_eq!(bytes_of(&s, id), bytes_of(&fresh, fresh_id), "the same result");
|
||||
assert_eq!((alpha(&pixels(&s, id), 4, 4), alpha(&pixels(&s, id), 0, 0)), (0, 255), "the red cleared, the white kept");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.begin_interaction("Remove Background").unwrap();
|
||||
s.preview(C, &json!({})).unwrap();
|
||||
assert_eq!(s.decoded_images.len(), 1);
|
||||
s.cancel_interaction().unwrap();
|
||||
assert_eq!(s.decoded_images.len(), 0, "Cancel lets the pixels go");
|
||||
s.execute(C, &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute(C, &json!({})).unwrap();
|
||||
assert_eq!(decodes() - start, 5, "each run that is not a preview step decodes");
|
||||
}
|
||||
|
||||
/// Pixels a preview step takes from the step before keep what they were decoded with: a 16-bit gray
|
||||
/// image with an ICC profile comes out as a run on freshly decoded pixels gives it.
|
||||
#[test]
|
||||
fn reused_pixels_keep_their_depth_gray_and_profile() {
|
||||
use image::{ImageDecoder as _, ImageEncoder as _};
|
||||
let icc = b"a profile's bytes".to_vec();
|
||||
let gray16 = ImageBuffer::from_fn(8, 8, |x, y| Luma([if (2..6).contains(&x) && (2..6).contains(&y) { 7_000u16 } else { 61_000 }]));
|
||||
let mut bytes = Vec::new();
|
||||
let mut encoder = image::codecs::png::PngEncoder::new(&mut bytes);
|
||||
encoder.set_icc_profile(icc.clone()).unwrap();
|
||||
DynamicImage::ImageLuma16(gray16).write_with_encoder(encoder).unwrap();
|
||||
let decodes = || super::DECODES.with(std::cell::Cell::get);
|
||||
let (mut s, id) = open(&bytes);
|
||||
let start = decodes();
|
||||
s.begin_interaction("Remove Background").unwrap();
|
||||
s.preview(C, &json!({"tolerance": 0, "softness": 0})).unwrap();
|
||||
s.preview(C, &json!({})).unwrap();
|
||||
assert_eq!(decodes() - start, 1, "the second step took the pixels of the first");
|
||||
let (mut fresh, fresh_id) = open(&bytes);
|
||||
fresh.execute(C, &json!({})).unwrap();
|
||||
assert_eq!(bytes_of(&s, id), bytes_of(&fresh, fresh_id), "the same result");
|
||||
let DynamicImage::ImageLumaA16(out) = pixels(&s, id) else { panic!("16-bit gray with alpha") };
|
||||
assert_eq!((out.get_pixel(0, 0).0, out.get_pixel(3, 3).0), ([61_000, 0], [7_000, 65_535]));
|
||||
let result = bytes_of(&s, id);
|
||||
let mut decoder = image::codecs::png::PngDecoder::new(Cursor::new(result.as_slice())).unwrap();
|
||||
assert_eq!(decoder.icc_profile().unwrap(), Some(icc));
|
||||
}
|
||||
|
||||
/// Once the background is cleared, most edge pixels are clear and a second run clears nothing: the
|
||||
/// subject's color along an edge is not taken for the background.
|
||||
#[test]
|
||||
fn a_second_run_leaves_the_subject() {
|
||||
let img = image::RgbaImage::from_fn(10, 10, |x, y| {
|
||||
image::Rgba(if y >= 7 {
|
||||
RED
|
||||
} else if (4..6).contains(&x) && (3..5).contains(&y) {
|
||||
[0, 0, 200, 255]
|
||||
} else {
|
||||
WHITE
|
||||
})
|
||||
});
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(img)));
|
||||
s.execute(C, &json!({})).unwrap();
|
||||
let (first, steps) = (image_of(&s, id), undo_len(&s));
|
||||
let r = s.execute(C, &json!({})).unwrap();
|
||||
assert_eq!(r, json!({"changed": 0, "ids": [], "cleared": 0}));
|
||||
assert_eq!((image_of(&s, id), undo_len(&s)), (first, steps), "no change and no undo step");
|
||||
let out = pixels(&s, id);
|
||||
assert_eq!((alpha(&out, 5, 0), alpha(&out, 5, 9), alpha(&out, 4, 3)), (0, 255, 255), "the red and the blue stay");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tolerance_and_softness_set_how_near_a_color_is_cleared() {
|
||||
// White; 1.96 % from white; 21.6 % from white.
|
||||
let px = [WHITE, [250, 250, 250, 255], [200, 200, 200, 255]];
|
||||
let at = |i: usize| px[i].map(|v| u16::from(v) * 257);
|
||||
let run = |tolerance, softness| levels(3, 1, &at, &Settings { tolerance, softness, ..settings(Background::Colors(vec![[255; 3]])) });
|
||||
assert_eq!(run(0.0, 0.0), [0, 255, 255], "only white itself at 0");
|
||||
assert_eq!(run(2.0, 0.0), [0, 0, 255]);
|
||||
assert_eq!(run(0.0, 10.0), [0, 50, 255], "1.96 % into a 10 % fade");
|
||||
assert_eq!(run(100.0, 0.0), [0, 0, 0], "everything at 100");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_result_with_no_pixel_left_is_refused_and_changes_nothing() {
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
let before = (image_of(&s, id), undo_len(&s));
|
||||
let e = s.execute(C, &json!({"background": "colors", "colors": "#ffffff", "tolerance": 100})).unwrap_err();
|
||||
assert!(e.to_string().contains("every pixel matches the background"), "{e}");
|
||||
assert_eq!((image_of(&s, id), undo_len(&s)), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_image_whose_pixels_do_not_change_is_left_with_no_undo_step() {
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
let before = (image_of(&s, id), undo_len(&s));
|
||||
let r = s.execute(C, &json!({"background": "colors", "colors": "#00ff00", "tolerance": 0, "softness": 0})).unwrap();
|
||||
assert_eq!(r, json!({"changed": 0, "ids": [], "cleared": 0}));
|
||||
assert_eq!((image_of(&s, id), undo_len(&s)), before);
|
||||
// An image whose edges and corners are already clear has no edge or corner color to clear,
|
||||
// whatever color the clear pixels hold.
|
||||
let ring =
|
||||
image::RgbaImage::from_fn(6, 6, |x, y| image::Rgba(if (1..5).contains(&x) && (1..5).contains(&y) { WHITE } else { [255, 255, 255, 0] }));
|
||||
for p in [json!({}), json!({"background": "corners", "contiguous": false})] {
|
||||
let (mut s, _) = open(&png(DynamicImage::ImageRgba8(ring.clone())));
|
||||
assert_eq!(s.execute(C, &p).unwrap()["changed"], 0, "{p}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The corners' color: 40 × 30 pixels of light gray around a red block, cleared within 5 steps on
|
||||
/// each channel (`match: channels`, a tolerance of `5 / 255 × 100`, no softness), from the edges
|
||||
/// or wherever it is.
|
||||
#[test]
|
||||
fn the_corners_color_is_cleared() {
|
||||
let img = image::RgbaImage::from_fn(40, 30, |x, y| {
|
||||
image::Rgba(if (10..30).contains(&x) && (8..22).contains(&y) { [200, 40, 40, 255] } else { [240, 240, 240, 255] })
|
||||
});
|
||||
for contiguous in [false, true] {
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(img.clone())));
|
||||
let p = json!({"background": "corners", "contiguous": contiguous, "match": "channels", "tolerance": 5.0 / 255.0 * 100.0, "softness": 0});
|
||||
let r = s.execute(C, &p).unwrap();
|
||||
assert_eq!(r["cleared"], 40 * 30 - 14 * 20, "{p}");
|
||||
let out = pixels(&s, id).to_rgba8();
|
||||
assert_eq!((out.get_pixel(0, 0).0[3], out.get_pixel(15, 15).0), (0, [200, 40, 40, 255]), "{p}");
|
||||
}
|
||||
// A clear corner is left out of the average: the other three are white, and white is cleared.
|
||||
let mut img = image::RgbaImage::from_pixel(8, 8, image::Rgba(WHITE));
|
||||
img.put_pixel(0, 0, image::Rgba([0; 4]));
|
||||
img.put_pixel(4, 4, image::Rgba(RED));
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(img)));
|
||||
let r = s.execute(C, &json!({"background": "corners", "tolerance": 0, "softness": 0})).unwrap();
|
||||
let out = pixels(&s, id);
|
||||
assert_eq!((alpha(&out, 7, 7), alpha(&out, 3, 3), alpha(&out, 4, 4)), (0, 0, 255));
|
||||
assert_eq!(r["cleared"], 8 * 8 - 2, "the pixels made clear, not the corner that was clear already");
|
||||
}
|
||||
|
||||
/// One picked color matched by channel at a tolerance of 0: cleared wherever it is, and a pixel a
|
||||
/// long way from it kept.
|
||||
#[test]
|
||||
fn one_picked_color_is_cleared_by_channel() {
|
||||
let mut img = image::RgbaImage::from_pixel(4, 4, image::Rgba([10, 20, 30, 255]));
|
||||
img.put_pixel(2, 2, image::Rgba([250, 250, 250, 255]));
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(img)));
|
||||
let p = json!({"background": "colors", "colors": "#0a141e", "contiguous": false, "match": "channels", "tolerance": 0, "softness": 0});
|
||||
assert_eq!(s.execute(C, &p).unwrap()["cleared"], 15);
|
||||
let out = pixels(&s, id);
|
||||
assert_eq!((alpha(&out, 0, 0), alpha(&out, 2, 2)), (0, 255));
|
||||
}
|
||||
|
||||
/// With `match: channels`, a tolerance of `t / 255 × 100` clears a color `k` 8-bit steps from the
|
||||
/// background on one channel exactly when `k ≤ t`, for every `t` and `k` from 0 to 255; with
|
||||
/// `distance` the same color is nearer when the others match.
|
||||
#[test]
|
||||
fn channels_match_the_largest_difference_on_one_channel() {
|
||||
for t in 0..=255u16 {
|
||||
let s = Settings { measure: Match::Channels, tolerance: f64::from(t) / 255.0 * 100.0, softness: 0.0, ..settings(Background::Edges) };
|
||||
for k in 0..=255u16 {
|
||||
for c in [[k * 257, 0, 0], [0, k * 257, 0], [0, 0, k * 257]] {
|
||||
assert_eq!(level(c, &[[0; 3]], &s) == 0, k <= t, "t {t}, k {k}");
|
||||
}
|
||||
}
|
||||
}
|
||||
let at = |measure| {
|
||||
level([0, 40 * 257, 0], &[[0; 3]], &Settings { measure, tolerance: 32.0 / 255.0 * 100.0, softness: 0.0, ..settings(Background::Edges) })
|
||||
};
|
||||
assert_eq!((at(Match::Channels), at(Match::Distance)), (255, 0), "40 steps on green: past 32 on one channel; 9 % of the RGB distance");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sixteen_bit_and_gray_images_keep_their_depth_and_channels() {
|
||||
let deep = ImageBuffer::from_fn(8, 8, |x, y| Rgb(if (2..6).contains(&x) && (2..6).contains(&y) { [51_400, 0, 1] } else { [65_535; 3] }));
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgb16(deep)));
|
||||
s.execute(C, &json!({})).unwrap();
|
||||
let DynamicImage::ImageRgba16(out) = pixels(&s, id) else { panic!("16-bit RGBA") };
|
||||
assert_eq!(out.get_pixel(0, 0).0[3], 0);
|
||||
assert_eq!(out.get_pixel(3, 3).0, [51_400, 0, 1, 65_535], "16-bit samples kept as they were");
|
||||
// Samples one 8-bit step apart are told apart: only the background is cleared at 0.
|
||||
let near = ImageBuffer::from_fn(4, 4, |x, y| Rgb(if (1..3).contains(&x) && (1..3).contains(&y) { [65_280; 3] } else { [65_535; 3] }));
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgb16(near)));
|
||||
s.execute(C, &json!({"tolerance": 0, "softness": 0})).unwrap();
|
||||
let DynamicImage::ImageRgba16(out) = pixels(&s, id) else { panic!("16-bit RGBA") };
|
||||
assert_eq!((out.get_pixel(0, 0).0[3], out.get_pixel(1, 1).0[3]), (0, 65_535));
|
||||
let gray = ImageBuffer::from_fn(8, 8, |x, y| Luma([if (2..6).contains(&x) && (2..6).contains(&y) { 30u8 } else { 240 }]));
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageLuma8(gray)));
|
||||
s.execute(C, &json!({})).unwrap();
|
||||
let DynamicImage::ImageLumaA8(out) = pixels(&s, id) else { panic!("gray with alpha") };
|
||||
assert_eq!((out.get_pixel(0, 0).0, out.get_pixel(3, 3).0), ([240, 0], [30, 255]));
|
||||
let gray16 = ImageBuffer::from_fn(8, 8, |x, y| Luma([if (2..6).contains(&x) && (2..6).contains(&y) { 7_000u16 } else { 61_000 }]));
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageLuma16(gray16)));
|
||||
s.execute(C, &json!({})).unwrap();
|
||||
let DynamicImage::ImageLumaA16(out) = pixels(&s, id) else { panic!("16-bit gray with alpha") };
|
||||
assert_eq!((out.get_pixel(0, 0).0, out.get_pixel(3, 3).0), ([61_000, 0], [7_000, 65_535]));
|
||||
}
|
||||
|
||||
/// The ICC profile the colors are in stays with them.
|
||||
#[test]
|
||||
fn the_color_profile_is_kept() {
|
||||
use image::{ImageDecoder as _, ImageEncoder as _};
|
||||
let icc = b"a profile's bytes".to_vec();
|
||||
let mut bytes = Vec::new();
|
||||
let mut encoder = image::codecs::png::PngEncoder::new(&mut bytes);
|
||||
encoder.set_icc_profile(icc.clone()).unwrap();
|
||||
DynamicImage::ImageRgba8(framed()).write_with_encoder(encoder).unwrap();
|
||||
let blob = ImageBlob::png(bytes);
|
||||
let out = super::clear(&blob, &settings(Background::Edges), None).unwrap().unwrap();
|
||||
let mut decoder = image::codecs::png::PngDecoder::new(Cursor::new(out.blob.bytes.as_slice())).unwrap();
|
||||
assert_eq!(decoder.icc_profile().unwrap(), Some(icc));
|
||||
}
|
||||
|
||||
/// The resolution the file declares stays with the pixels, so a later Replace or relink sizes the
|
||||
/// image as before.
|
||||
#[test]
|
||||
fn the_resolution_is_kept() {
|
||||
let bytes = ppi::with_png_resolution(&png(DynamicImage::ImageRgba8(framed())), (300.0, 150.0));
|
||||
let (mut s, id) = open(&bytes);
|
||||
s.execute(C, &json!({})).unwrap();
|
||||
let (x, y) = ppi::resolution(&bytes_of(&s, id)).expect("a resolution");
|
||||
assert_eq!((x.round(), y.round()), (300.0, 150.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cmyk_image_is_refused() {
|
||||
let inks = vectorcraft_doc::cmyk::Inks::new(4, 2, [0, 0, 0, 0].repeat(8)).unwrap();
|
||||
let tiff = ImageBlob::cmyk_tiff(&inks).unwrap();
|
||||
let (mut s, id) = open(&tiff.bytes);
|
||||
let before = (image_of(&s, id), undo_len(&s));
|
||||
let e = s.execute(C, &json!({})).unwrap_err();
|
||||
assert!(e.to_string().contains("a CMYK image has no transparency to set"), "{e}");
|
||||
assert_eq!((image_of(&s, id), undo_len(&s)), before);
|
||||
}
|
||||
|
||||
/// A linked image of `blob`, `width` × `height` pixels as the object records it, beside the open
|
||||
/// document's image.
|
||||
fn place_linked(s: &mut Session, key: &str, blob: ImageBlob, (width, height): (u32, u32)) -> NodeId {
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert(key.into(), blob);
|
||||
let id = d.alloc_id();
|
||||
let im = ImageObject {
|
||||
key: key.into(),
|
||||
width,
|
||||
height,
|
||||
xf: Default::default(),
|
||||
link: Some(LinkInfo::new("/nowhere/art.png")),
|
||||
placement: Default::default(),
|
||||
pixels: None,
|
||||
};
|
||||
d.insert(d.default_layer(), 0, Node::new(id, NodeKind::Image(Box::new(im))))?;
|
||||
sel.set([id]);
|
||||
Ok(id)
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_linked_image_is_embedded_and_one_that_shows_its_preview_is_refused() {
|
||||
let bytes = png(DynamicImage::ImageRgba8(framed()));
|
||||
let (mut s, _) = open(&bytes);
|
||||
let linked = place_linked(&mut s, "linked", ImageBlob::png(bytes.clone()), (12, 10));
|
||||
let clip = vectorcraft_doc::links::PlacementOptions { clip: true, ..Default::default() };
|
||||
s.edit("Placement", |d, _| {
|
||||
if let Some(NodeKind::Image(im)) = d.node_mut(linked).map(|n| &mut n.kind) {
|
||||
im.placement = clip;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let r = s.execute(C, &json!({"ids": [linked.0, linked.0]})).unwrap();
|
||||
assert_eq!(r, json!({"changed": 1, "ids": [linked.0], "cleared": 12 * 10 - 6 * 4}), "an image named twice changes once");
|
||||
let im = image_of(&s, linked);
|
||||
assert_eq!((im.link, alpha(&pixels(&s, linked), 0, 0)), (None, 0), "embedded, with its background cleared");
|
||||
assert_eq!(im.placement, clip, "its Placement Options kept, as Embed keeps them");
|
||||
// Pixels the file wasn't read for: the preview, as an image whose file is missing shows it.
|
||||
let preview = Arc::new(bytes);
|
||||
let missing = place_linked(&mut s, "missing", ImageBlob { mime: "image/png".into(), bytes: preview.clone(), proxy: Some(preview) }, (12, 10));
|
||||
let before = (image_of(&s, missing), undo_len(&s));
|
||||
let e = s.execute(C, &json!({})).unwrap_err();
|
||||
assert!(e.to_string().contains("the linked image shows its preview: embed or relink it first"), "{e}");
|
||||
assert_eq!((image_of(&s, missing), undo_len(&s)), before);
|
||||
}
|
||||
|
||||
/// An object whose pixels were read at another size than it records keeps its place and size.
|
||||
#[test]
|
||||
fn an_image_read_at_another_size_keeps_its_place_and_size() {
|
||||
let bytes = png(DynamicImage::ImageRgba8(framed()));
|
||||
let (mut s, _) = open(&bytes);
|
||||
let id = place_linked(&mut s, "linked", ImageBlob::png(bytes), (24, 20));
|
||||
s.execute(C, &json!({"ids": [id.0]})).unwrap();
|
||||
let im = image_of(&s, id);
|
||||
assert_eq!((im.width, im.height, im.xf), (12, 10, Affine::scale_non_uniform(2.0, 2.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_params_are_refused_and_change_nothing() {
|
||||
let (mut s, id) = open(&png(DynamicImage::ImageRgba8(framed())));
|
||||
let rect = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 5, "height": 5})).unwrap()["id"].clone();
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
let before = (image_of(&s, id), undo_len(&s));
|
||||
// With `ids`, the command is available when one of them is an image.
|
||||
let not_image = format!("object {rect} is not an image");
|
||||
let cases: [(Value, &str); 18] = [
|
||||
(json!({"background": "sky"}), "`background` must be edges, corners or colors"),
|
||||
(json!({"background": 5}), "`background` must be a string"),
|
||||
(json!({"match": "rgb"}), "`match` must be distance or channels"),
|
||||
(json!({"match": true}), "`match` must be a string"),
|
||||
(json!({"contiguous": "yes"}), "`contiguous` must be true or false"),
|
||||
(json!({"contiguous": 1}), "`contiguous` must be true or false"),
|
||||
(json!({"tolerance": 101}), "`tolerance` must be from 0 to 100"),
|
||||
(json!({"softness": -1}), "`softness` must be from 0 to 100"),
|
||||
(json!({"tolerance": "5"}), "`tolerance` must be a number"),
|
||||
(json!({"background": "colors"}), "`colors` must list 1 to 16 colors, each hex (#rrggbb or #rgb) or black, white, red, green or blue"),
|
||||
(json!({"background": "colors", "colors": []}), "`colors` must list"),
|
||||
(json!({"background": "colors", "colors": "#12"}), "`colors` must list"),
|
||||
(json!({"background": "colors", "colors": "#ffffff80"}), "`colors` must list"),
|
||||
(json!({"colors": ["#ffffff", 7]}), "`colors` must list"),
|
||||
(json!({"background": "colors", "colors": vec!["#000"; 17]}), "`colors` must list"),
|
||||
(json!({"background": "colors", "colors": "#000 ".repeat(100_000)}), "`colors` must list"),
|
||||
(json!({"ids": [id.0, rect]}), ¬_image),
|
||||
(json!({"ids": [rect]}), "select an image"),
|
||||
];
|
||||
for (p, msg) in cases {
|
||||
let e = s.execute(C, &p).unwrap_err().to_string();
|
||||
assert!(e.contains(msg), "{p}: {e}");
|
||||
}
|
||||
assert_eq!((image_of(&s, id), undo_len(&s)), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_image_too_large_to_change_is_refused_before_it_is_decoded() {
|
||||
let run = |w: u32, h: u32| {
|
||||
let (mut s, _) = open(&crate::tests_linkspanel::png_header(w, h));
|
||||
s.execute(C, &json!({})).unwrap_err().to_string()
|
||||
};
|
||||
// As gray samples, RGBA worked on and kept for the PNG, and the work: 19 bytes a pixel.
|
||||
let over = run(10_632, 10_631);
|
||||
assert!(over.contains("an image of 10632 × 10631 pixels is too large to change"), "{over}");
|
||||
// Within the allowance it is decoded, and its missing rows can't be read.
|
||||
let within = run(10_631, 10_631);
|
||||
assert!(within.contains("the image's pixels can't be read"), "{within}");
|
||||
}
|
||||
@@ -19,12 +19,12 @@
|
||||
//! shape that a Convert to Shape or Outline Object effect on them gives them, below or above the
|
||||
//! image as its Contents row says; new ones go right below that row.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::{BlendMode, Paint};
|
||||
use vectorcraft_doc::appearance::SharedStack;
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, CharStyle, FillLayer, Node, NodeKind, ParaStyle, StrokeLayer};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, CharStyle, Document, FillLayer, Node, NodeKind, ParaStyle, StrokeLayer};
|
||||
use vectorcraft_geom::Rect;
|
||||
|
||||
use super::edit::{outermost, roots_of, selected_roots};
|
||||
@@ -126,7 +126,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Eyedropper",
|
||||
[],
|
||||
None,
|
||||
"{source: id, ids?, pickUp?, apply? (trees as eyedropper.setOptions, merged over the Eyedropper Options for this call), reverse?: bool, append?: bool, defaultsOnly?: bool} copy the attributes both picked up and applied (by default the whole appearance stack, opacity and blend mode, and from type to type the character and paragraph attributes) from `source` to ids (default: highlighted characters while editing type, after Select All in a frame of threaded text the text of the story's frames, leaving out `source`, otherwise the object selection; highlighted characters reject enabled non-neutral object transparency), the fill, stroke and weight also to the paint defaults; `defaultsOnly` updates only paint defaults and cannot be combined with ids, reverse or append; `reverse` (Alt-click) copies from the first selected object (or ids) onto `source` instead, or with nothing selected what the Eyedropper picked up last (a click without Alt); `append` (Shift+Alt-click) adds the source's fills, strokes and effects on top of each target's stack; placed gradients land at the same place relative to each target's bounds (defaults: fitted to new art); with an object of a dynamic symbol instance selected by Direct Selection and no ids, reverse, append or defaultsOnly, that object takes the source's fill and stroke (solid, none, gradient or pattern) for this instance only, as the Eyedropper Options allow, and a source that is not a path, compound path or type, or options that pick up neither color, are refused. On a path directly in a graph series, the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke and the object's opacity and blend mode (exceptions: docs/mcp.md) → {ids}",
|
||||
"{source: id, ids?, pickUp?, apply? (trees as eyedropper.setOptions, merged over the Eyedropper Options for this call), reverse?: bool, append?: bool, defaultsOnly?: bool} copy the attributes both picked up and applied (by default the whole appearance stack, opacity and blend mode, and from type to type the character and paragraph attributes using effective character dimensions while preserving target transforms) from `source` to ids (default: highlighted characters while editing type, after Select All in a frame of threaded text the text of the story's frames, leaving out `source`, otherwise the object selection; highlighted characters reject enabled non-neutral object transparency), the fill, stroke and weight also to the paint defaults; `defaultsOnly` updates only paint defaults and cannot be combined with ids, reverse or append; `reverse` (Alt-click) copies from the first selected object (or ids) onto `source` instead, or with nothing selected what the Eyedropper picked up last (a click without Alt); `append` (Shift+Alt-click) adds the source's fills, strokes and effects on top of each target's stack; placed gradients land at the same place relative to each target's bounds (defaults: fitted to new art); with an object of a dynamic symbol instance selected by Direct Selection and no ids, reverse, append or defaultsOnly, that object takes the source's fill and stroke (solid, none, gradient or pattern) for this instance only, as the Eyedropper Options allow, and a source that is not a path, compound path or type, or options that pick up neither color, are refused. On a path directly in a graph series, the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke and the object's opacity and blend mode (exceptions: docs/mcp.md) → {ids}",
|
||||
has_doc,
|
||||
copy_from
|
||||
),
|
||||
@@ -1198,7 +1198,7 @@ fn target_contents(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({ "ids": ids.iter().map(|i| i.0).collect::<Vec<_>>() }))
|
||||
}
|
||||
|
||||
/// Sample enabled attributes, refusing object transparency that a text range cannot store.
|
||||
/// Sample enabled attributes with transform-aware character dimensions and atomic validation.
|
||||
fn copy_from(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "appearance.copyFrom";
|
||||
let clicked = id_param(p, "source").ok_or_else(|| bad(C, "missing `source` id"))?;
|
||||
@@ -1270,9 +1270,6 @@ fn copy_from(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
None => s.eyedropper_sample.clone().ok_or_else(|| bad(C, "select the object to copy from"))?,
|
||||
};
|
||||
let picked = Picked::of(&node);
|
||||
if !reverse && !defaults_only {
|
||||
s.eyedropper_sample = Some(node);
|
||||
}
|
||||
// The highlighted characters' type objects and ranges: after Select All in threaded text, all
|
||||
// the text of every frame of the story, except a frame the attributes come from, in place of
|
||||
// the object selection.
|
||||
@@ -1285,6 +1282,15 @@ fn copy_from(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some((id, start, end)) if targets.contains(&id) => vec![(id, Some((start, end)))],
|
||||
_ => vec![],
|
||||
};
|
||||
let target_ids = || targets.iter().copied().filter(|_| ranges.is_empty()).chain(ranges.iter().map(|(id, _)| *id));
|
||||
// Prepare every dimensional copy before changing defaults or the remembered sample. A bad
|
||||
// source or target transform must refuse the entire pick, including a multi-frame range.
|
||||
let characters = if attrs.character && !append {
|
||||
let d = &s.doc()?.doc;
|
||||
copied_character_styles(&node, target_ids().filter_map(|id| d.node(id)))?
|
||||
} else {
|
||||
HashMap::new()
|
||||
};
|
||||
// Character styles cannot represent object transparency. Refuse before even changing
|
||||
// paint defaults; applying it to the object would also alter the unselected characters.
|
||||
if attrs.appearance.transparency {
|
||||
@@ -1298,11 +1304,33 @@ fn copy_from(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if !reverse {
|
||||
picked.to_defaults(s, &attrs.appearance);
|
||||
}
|
||||
// The session's bounds check follows the selection. Check text targets reached through
|
||||
// explicit IDs, a reverse pick, or a highlighted story as well. Paragraph changes can
|
||||
// move point text even when character copying is disabled.
|
||||
let text_ids: Vec<_> = if attrs.paragraph && !append && picked.text.is_some() {
|
||||
let d = &s.doc()?.doc;
|
||||
target_ids().filter(|id| d.node(*id).is_some_and(|n| matches!(n.kind, NodeKind::Text(_)))).collect()
|
||||
} else {
|
||||
characters.keys().copied().collect()
|
||||
};
|
||||
let check_text_bounds = |d: &Document| {
|
||||
if !text_ids.is_empty() && !crate::sanity::objects_in_range(d, &text_ids) {
|
||||
return Err(bad(C, "sampled text would exceed the canvas (coordinates out of range)"));
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
// Remember the pick only after the document edit has passed its own validation.
|
||||
let finish = |s: &mut Session, ids: &[NodeId]| {
|
||||
if !reverse {
|
||||
if !defaults_only {
|
||||
s.eyedropper_sample = Some(node);
|
||||
}
|
||||
picked.to_defaults(s, &attrs.appearance);
|
||||
}
|
||||
json!({ "ids": ids.iter().map(|i| i.0).collect::<Vec<_>>() })
|
||||
};
|
||||
if (targets.is_empty() && ranges.is_empty()) || attrs.is_empty() {
|
||||
return Ok(json!({ "ids": [] }));
|
||||
return Ok(finish(s, &[]));
|
||||
}
|
||||
if !ranges.is_empty() {
|
||||
let ids: Vec<NodeId> = ranges.iter().map(|(f, _)| *f).collect();
|
||||
@@ -1312,24 +1340,26 @@ fn copy_from(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let Some(NodeKind::Text(t)) = d.node_mut(id).map(|n| &mut n.kind) else { return Err(EngineError::NoNode(id)) };
|
||||
let (start, end) = range.unwrap_or((0, vectorcraft_text::edit::runs_len(&t.runs)));
|
||||
let placed = picked.placed(Some(t.local_bounds()));
|
||||
vectorcraft_text::edit::style_range(&mut t.runs, start, end, |st| picked.run(st, &placed, &attrs));
|
||||
vectorcraft_text::edit::style_range(&mut t.runs, start, end, |st| Picked::run(st, &placed, &attrs, characters.get(&id)));
|
||||
if let Some((_, para)) = picked.text.as_ref().filter(|_| attrs.paragraph) {
|
||||
t.edit_paras(Some(t.paragraphs_in(start, end)), |p| *p = para.clone());
|
||||
}
|
||||
super::typecmd::refresh_bounds(t);
|
||||
}
|
||||
check_text_bounds(d)?;
|
||||
Ok(count_changed(d, &before, &ids))
|
||||
})?;
|
||||
return Ok(json!({ "ids": ids.iter().map(|i| i.0).collect::<Vec<_>>() }));
|
||||
return Ok(finish(s, &ids));
|
||||
}
|
||||
// When every target comes out as it was, the document and its undo history stay as they are.
|
||||
let before = s.doc()?.doc.clone();
|
||||
super::overprint::edit_counted(s, "Eyedropper", |d| {
|
||||
for id in &targets {
|
||||
if let Some(n) = d.node_mut(*id) {
|
||||
picked.apply(n, &attrs, append);
|
||||
picked.apply(n, &attrs, append, characters.get(id));
|
||||
}
|
||||
}
|
||||
check_text_bounds(d)?;
|
||||
// The paints the pick wrote, with a weight going with the paint of its stroke, as in `set_item`. `append` adds
|
||||
// only the colors of the visible rows the source has.
|
||||
let (fill, stroke) = (attrs.appearance.fill, attrs.appearance.stroke);
|
||||
@@ -1369,7 +1399,59 @@ fn copy_from(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let object = super::graph::capture_series_object_opacity(d, &objects);
|
||||
Ok(count_changed(d, &before, &targets) + usize::from(kept || painted || object))
|
||||
})?;
|
||||
Ok(json!({ "ids": targets.iter().map(|i| i.0).collect::<Vec<_>>() }))
|
||||
Ok(finish(s, &targets))
|
||||
}
|
||||
|
||||
/// Read source typography in document space, then compensate for each target's transform.
|
||||
/// Paint stays in its existing sampling path; character scale never changes object placement.
|
||||
fn copied_character_styles<'a>(source: &Node, targets: impl Iterator<Item = &'a Node>) -> Result<HashMap<NodeId, CharStyle>> {
|
||||
const C: &str = "appearance.copyFrom";
|
||||
let mut styles = HashMap::new();
|
||||
let NodeKind::Text(source) = &source.kind else { return Ok(styles) };
|
||||
let Some(run) = source.runs.first() else { return Ok(styles) };
|
||||
let mut targets = targets
|
||||
.filter_map(|n| match &n.kind {
|
||||
NodeKind::Text(t) => Some((n.id, t)),
|
||||
_ => None,
|
||||
})
|
||||
.peekable();
|
||||
if targets.peek().is_none() {
|
||||
return Ok(styles);
|
||||
}
|
||||
let effective = source.effective_char_style(&run.style).ok_or_else(|| bad(C, "source text transform is collapsed or unrepresentable"))?;
|
||||
if !copied_dimensions_fit(&run.style, &effective) {
|
||||
return Err(bad(C, "character dimensions cannot be represented with the source text transform"));
|
||||
}
|
||||
for (id, text) in targets {
|
||||
let scale = text.style_scale().ok_or_else(|| bad(C, "target text transform is collapsed or unrepresentable"))?;
|
||||
let mut local = effective.clone();
|
||||
local.size /= scale.points;
|
||||
local.leading = local.leading.map(|v| v / scale.points);
|
||||
local.baseline_shift /= scale.points;
|
||||
local.tcy_up_down /= scale.points;
|
||||
local.tcy_left_right /= scale.points;
|
||||
local.h_scale /= scale.horizontal;
|
||||
// As with dimensional type commands, reject overflow and underflow before layout.
|
||||
if !copied_dimensions_fit(&effective, &local) {
|
||||
return Err(bad(C, "character dimensions cannot be represented with the target text transform"));
|
||||
}
|
||||
styles.insert(id, local);
|
||||
}
|
||||
Ok(styles)
|
||||
}
|
||||
|
||||
/// Bound each converted type dimension and refuse loss to zero before layout reads it.
|
||||
fn copied_dimensions_fit(from: &CharStyle, to: &CharStyle) -> bool {
|
||||
[
|
||||
(from.size, to.size),
|
||||
(from.leading.unwrap_or(0.0), to.leading.unwrap_or(0.0)),
|
||||
(from.baseline_shift, to.baseline_shift),
|
||||
(from.tcy_up_down, to.tcy_up_down),
|
||||
(from.tcy_left_right, to.tcy_left_right),
|
||||
(from.h_scale, to.h_scale),
|
||||
]
|
||||
.iter()
|
||||
.all(|(from, to)| to.is_finite() && to.abs() <= crate::MAX_COORD && (*from == 0.0 || *to != 0.0))
|
||||
}
|
||||
|
||||
/// What the Eyedropper picks up from an object.
|
||||
@@ -1422,8 +1504,8 @@ impl Picked {
|
||||
|
||||
/// Copy character attributes and the enabled paint attributes through the same path for
|
||||
/// whole text objects and highlighted character ranges.
|
||||
fn run(&self, st: &mut CharStyle, placed: &Appearance, a: &EyedropperAttrs) {
|
||||
if let Some((cs, _)) = self.text.as_ref().filter(|_| a.character) {
|
||||
fn run(st: &mut CharStyle, placed: &Appearance, a: &EyedropperAttrs, character: Option<&CharStyle>) {
|
||||
if let Some(cs) = character {
|
||||
*st = CharStyle {
|
||||
fill: std::mem::take(&mut st.fill),
|
||||
stroke: std::mem::take(&mut st.stroke),
|
||||
@@ -1439,7 +1521,7 @@ impl Picked {
|
||||
/// Apply attributes `a` to `n`. `append` adds the fills and strokes (and effects) on top of its
|
||||
/// stack instead. Type takes the paints on its characters, and character and paragraph
|
||||
/// attributes from type.
|
||||
fn apply(&self, n: &mut Node, a: &EyedropperAttrs, append: bool) {
|
||||
fn apply(&self, n: &mut Node, a: &EyedropperAttrs, append: bool, character: Option<&CharStyle>) {
|
||||
let ap = &a.appearance;
|
||||
if ap.transparency {
|
||||
n.opacity = self.opacity;
|
||||
@@ -1456,7 +1538,7 @@ impl Picked {
|
||||
let placed = self.placed(Some(t.local_bounds()));
|
||||
let text = self.text.as_ref();
|
||||
for r in &mut t.runs {
|
||||
self.run(&mut r.style, &placed, a);
|
||||
Self::run(&mut r.style, &placed, a, character);
|
||||
}
|
||||
if let Some((_, para)) = text.filter(|_| a.paragraph) {
|
||||
// The source's first paragraph's attributes, on every paragraph.
|
||||
|
||||
@@ -673,10 +673,14 @@ fn undo_group_end(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// the step before. As any new edit it ends the steps to redo, so Redo never swaps the document
|
||||
/// under an interaction that would then preview on, or cancel to, the document from before (#1719).
|
||||
/// A typing session also ends in the Type tool, whose caret and marked text (IME) must follow the
|
||||
/// document.
|
||||
/// document. A preview that Real-time Drawing off left unapplied is applied first, as the tool's own
|
||||
/// commit applies it; when it fails, the interaction is canceled.
|
||||
pub(crate) fn settle_interaction(s: &mut Session) -> Result<()> {
|
||||
let typing = typing_in_progress(s);
|
||||
s.commit_interaction()?;
|
||||
match s.unapplied_interaction_preview().filter(|_| !s.previewing) {
|
||||
Some((cmd, p)) if s.preview(&cmd, &p).is_err() => s.cancel_interaction()?,
|
||||
_ => s.commit_interaction()?,
|
||||
}
|
||||
if typing {
|
||||
s.set_tool_option("endTyping", &Value::Bool(true));
|
||||
}
|
||||
|
||||
@@ -881,3 +881,56 @@ fn move_takes_several_effects_that_land_together_in_order() {
|
||||
assert!(s.execute("effect.move", &json!({"from": [0, 9], "to": 0, "fromItem": null, "ids": [id.0]})).is_err());
|
||||
assert_eq!(node(&s, id).appearance, before);
|
||||
}
|
||||
|
||||
/// A 3D effect's shadow is set with `effect.setParams` in one undo step, saved in the native file,
|
||||
/// drawn in exports and kept as a path by Expand Appearance; settings out of range are refused
|
||||
/// without an undo step.
|
||||
#[test]
|
||||
fn a_3d_shadow_is_one_undo_step_saved_exported_and_expanded() {
|
||||
let (mut s, id) = session_with_rect();
|
||||
s.execute("effect.apply", &json!({"effect": effects::EXTRUDE, "ids": [id.0], "item": null})).unwrap();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
for (params, why) in [
|
||||
(json!({"shadowBounds": 401}), "shadowBounds must be a number from 10 to 400"),
|
||||
(json!({"shadowPosition": "left"}), "shadowPosition must be behind or below"),
|
||||
] {
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let e = s.execute("effect.setParams", &json!({"ids": [id.0], "index": 0, "item": null, "params": params})).unwrap_err().to_string();
|
||||
assert!(e.contains(why), "{params}: {e}");
|
||||
assert_eq!(s.doc().unwrap().doc, before);
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
let params = json!({"shadow": true, "shadowPosition": "below", "shadowDistance": 20, "shadowBounds": 150});
|
||||
s.execute("effect.setParams", &json!({"ids": [id.0], "index": 0, "item": null, "params": params})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);
|
||||
let art = effects::solid_art(&node(&s, id)).unwrap();
|
||||
let shadow = art.children().unwrap()[0].clone();
|
||||
assert!(matches!(shadow.kind, NodeKind::Path { .. }));
|
||||
// Saved and opened again, the effect keeps its shadow settings and draws the same shadow.
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
let restored = vectorcraft_format::load(&vectorcraft_format::save(doc, false)).unwrap();
|
||||
let fx = &restored.node(id).unwrap().appearance.effects[0];
|
||||
for (key, value) in params.as_object().unwrap() {
|
||||
assert_eq!(fx.params.get(key), Some(value), "{key}");
|
||||
}
|
||||
assert_eq!(effects::solid_art(restored.node(id).unwrap()).unwrap(), art);
|
||||
// SVG export draws it: one more shape than without the shadow.
|
||||
let shapes = |s: &mut Session| {
|
||||
let svg = s.execute("document.serialize", &json!({"format": "svg"})).unwrap();
|
||||
let svg = svg["data"].as_str().or_else(|| svg["text"].as_str()).map(str::to_string).unwrap_or_else(|| svg.to_string());
|
||||
vectorcraft_testkit::svg_shapes(&svg)
|
||||
};
|
||||
let with = shapes(&mut s);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
let without = shapes(&mut s);
|
||||
assert_eq!(with, without + 1);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
s.execute("effect.expandAppearance", &json!({"ids": [id.0]})).unwrap();
|
||||
let mut shadows = 0;
|
||||
s.doc().unwrap().doc.walk(|n| {
|
||||
if n.path_data().is_some() && n.path_data() == shadow.path_data() && n.appearance.items == shadow.appearance.items {
|
||||
shadows += 1;
|
||||
}
|
||||
});
|
||||
assert_eq!(shadows, 1);
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
//! save, export and serialize by path or bytes (base64), the format table, `command.batch` and
|
||||
//! `action.batch` (the Actions panel menu's Batch…).
|
||||
//!
|
||||
//! - `load`: `document.open` (native, legacy, SVG/SVGZ, PDF/.ai/.ait, DXF, raster images).
|
||||
//! - `load`: `document.open` (native, legacy, SVG/SVGZ, PDF/.ai/.ait, DXF, HP-GL, raster images).
|
||||
//! - `action_batch`: play an action on every file in a folder or every data set.
|
||||
//! - `encode`: one encoder per writable format, with typed options parsed from the params.
|
||||
//! - `svg`: the SVG Options (styling, fonts, images, object ids, artboards…).
|
||||
@@ -205,6 +205,8 @@ mod tests_eps;
|
||||
#[cfg(test)]
|
||||
mod tests_dxfimport;
|
||||
#[cfg(test)]
|
||||
mod tests_hpgl;
|
||||
#[cfg(test)]
|
||||
mod tests_metafile;
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -9,7 +9,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Open Document",
|
||||
[],
|
||||
None,
|
||||
"{path | name | dataBase64 (a file's path, or its name with its bytes), pages?: \"2-3, 5\" (PDF/.ai: 1-based, default all; one artboard each) | page?: n, cropTo?: bounding|art|crop (default)|trim|bleed|media (the box each artboard gets; bounding: the art's bounds; trim: the bleed box round it becomes the document's bleed; a PDF-compatible .ai or a PDF saved with its editing data opens on its trim boxes for crop), password? (encrypted PDFs; see document.pdfInfo), textAs?: text (default: point type per line, lines wrapped in a frame as area type where they lay out the same, in the file's fonts by name; missing ones are listed in warnings) | outlines (glyph paths), layers?: true (default: optional content groups become layers with their visibility, print state and lock — art that is off comes in as a hidden layer; other art goes to a layer per page; a PSD file's layers become layers holding their pixels, with their names, visibility, opacity and blend modes, groups as layers of sublayers, layer masks as opacity masks, clipped layers and layers in blend modes VectorCraft doesn't have merged with the layers below) | false (one layer per page, without the art that is off; Save then asks for a name; a PSD file opens as its merged image), hiddenLayers?: false (a PSD file's hidden layers come in hidden instead of being left out), editingData?: true (default: an EPS, .ai or PDF that carries its editor's own copy of the art opens from it, with its layers, hidden objects, guides, artboards and the art outside them; a .ai or PDF whose type shows opens from its PDF part with textAs: outlines) | \"always\" (as true, and a .ai's editing copy is used even where it draws differently from its PDF part, which can be older; the warnings say so) | false (only what its page or PDF part prints, for print pipelines; a .ai saved without PDF compatibility then can't be opened), legacyNameEncoding?: windows-1252 (default) | macintosh | windows-949 (also euc-kr) | shift_jis | gbk | big5 (the code page names in the editing data of a .ai or an EPS are read in when they aren't UTF-8: layers, objects, artboards, colors, gradients, patterns, graphic styles, swatches, brushes, symbols, variables and data sets, and data sets' text values; warnings list the first three such names as read, and count the rest), colorMode?: rgb|cmyk (the mode the document opens in, its colours converted as file.documentColorMode does, grays? as there; default: the file's — a PDF keeps CMYK, Gray and spot inks (spot swatches at a tint) and opens in CMYK when painted mostly in CMYK), grays?: as file.documentColorMode, dxf?: {layout?: \"Model\" (default) | a paper layout's name (document.dxfInfo lists them), unit?, scale? (the ratio: 1 unit of the art = scale drawing units; default: the drawing at 1:1 in its own unit), fit?: false (scale to fit an artboard of fitTo?: [612, 792], turned to the art's orientation), scaleLineweights?: false (lineweights scale with the art), center?: true (false: the drawing's origin on the artboard's bottom-left corner; fitted art's bottom-left corner), mergeLayers?: false (all art on one layer)} (DXF), crop?: as cropTo (its older name), importer?: \"plugin.<id>\" (an importer plug-in's format id, see document.formats: it opens the file, which must have an extension it lists, also one a built-in format reads), params?: {…} (the importer's own parameters, see plugin.info)} → {index, title, format, warnings, restored, missingLinks, modifiedLinks, updatedLinks: [{name, path, ids}]}; any readable format (see document.formats): .vectorcraft/.drawcraft, .svg/.svgz, .pdf/.ai, .ait, ASCII .dxf (each DXF layer with art a layer; blocks symbols; what can't come in is listed in warnings), PNG/JPEG/GIF/WebP/TIFF/BMP (an image opens as a document of its size at the resolution it declares, as file.place sizes it; 72 ppi, 1 px = 1 pt, when it declares none), .emf/.wmf (one artboard, the picture's frame; records VectorCraft doesn't read are skipped with one warning), .eps and PostScript .ai (one artboard, the bounding box: an EPS file VectorCraft wrote restores the document it carries, restored: true; other files are read by a PostScript interpreter — paths, colours and spot inks, clips, gradients, images, type in the fonts named — and come in as their TIFF preview, with a warning, when their PostScript can't be read). A PDF/.ai/.ait or SVG saved with Preserve Editing restores the native document it carries (restored: true; a PDF only when no pages are picked), unless the file was changed elsewhere since or the data can't be read: then its artwork is imported and the first warning says why. Templates (native templates, .ait) open as a new untitled document; a restored .ai keeps its path (Save writes .ai again) and opens on the artboard its editing copy names as active. A file that no built-in format reads, by its content or its extension, opens with the installed importer plug-in that lists its extension (the one whose id sorts
Line truncated
|
||||
"{path | name | dataBase64 (a file's path, or its name with its bytes), pages?: \"2-3, 5\" (PDF/.ai: 1-based, default all; one artboard each) | page?: n, cropTo?: bounding|art|crop (default)|trim|bleed|media (the box each artboard gets; bounding: the art's bounds; trim: the bleed box round it becomes the document's bleed; a PDF-compatible .ai or a PDF saved with its editing data opens on its trim boxes for crop), password? (encrypted PDFs; see document.pdfInfo), textAs?: text (default: point type per line, lines wrapped in a frame as area type where they lay out the same, in the file's fonts by name; missing ones are listed in warnings) | outlines (glyph paths), layers?: true (default: optional content groups become layers with their visibility, print state and lock — art that is off comes in as a hidden layer; other art goes to a layer per page; a PSD file's layers become layers holding their pixels, with their names, visibility, opacity and blend modes, groups as layers of sublayers, layer masks as opacity masks, clipped layers and layers in blend modes VectorCraft doesn't have merged with the layers below) | false (one layer per page, without the art that is off; Save then asks for a name; a PSD file opens as its merged image), hiddenLayers?: false (a PSD file's hidden layers come in hidden instead of being left out), editingData?: true (default: an EPS, .ai or PDF that carries its editor's own copy of the art opens from it, with its layers, hidden objects, guides, artboards and the art outside them; a .ai or PDF whose type shows opens from its PDF part with textAs: outlines) | \"always\" (as true, and a .ai's editing copy is used even where it draws differently from its PDF part, which can be older; the warnings say so) | false (only what its page or PDF part prints, for print pipelines; a .ai saved without PDF compatibility then can't be opened), legacyNameEncoding?: windows-1252 (default) | macintosh | windows-949 (also euc-kr) | shift_jis | gbk | big5 (the code page names in the editing data of a .ai or an EPS are read in when they aren't UTF-8: layers, objects, artboards, colors, gradients, patterns, graphic styles, swatches, brushes, symbols, variables and data sets, and data sets' text values; warnings list the first three such names as read, and count the rest), colorMode?: rgb|cmyk (the mode the document opens in, its colours converted as file.documentColorMode does, grays? as there; default: the file's — a PDF keeps CMYK, Gray and spot inks (spot swatches at a tint) and opens in CMYK when painted mostly in CMYK), grays?: as file.documentColorMode, dxf?: {layout?: \"Model\" (default) | a paper layout's name (document.dxfInfo lists them), unit?, scale? (the ratio: 1 unit of the art = scale drawing units; default: the drawing at 1:1 in its own unit), fit?: false (scale to fit an artboard of fitTo?: [612, 792], turned to the art's orientation), scaleLineweights?: false (lineweights scale with the art), center?: true (false: the drawing's origin on the artboard's bottom-left corner; fitted art's bottom-left corner), mergeLayers?: false (all art on one layer)} (DXF), crop?: as cropTo (its older name), importer?: \"plugin.<id>\" (an importer plug-in's format id, see document.formats: it opens the file, which must have an extension it lists, also one a built-in format reads), params?: {…} (the importer's own parameters, see plugin.info)} → {index, title, format, warnings, restored, missingLinks, modifiedLinks, updatedLinks: [{name, path, ids}]}; any readable format (see document.formats): .vectorcraft/.drawcraft, .svg/.svgz, .pdf/.ai, .ait, ASCII .dxf (each DXF layer with art a layer; blocks symbols; what can't come in is listed in warnings), PNG/JPEG/GIF/WebP/TIFF/BMP (an image opens as a document of its size at the resolution it declares, as file.place sizes it; 72 ppi, 1 px = 1 pt, when it declares none), .emf/.wmf (one artboard, the picture's frame; records VectorCraft doesn't read are skipped with one warning), HP-GL plot files .plt/.hpgl/.hgl/.hpg (one artboard round the art; what isn't drawn is listed in warnings), .eps and PostScript .ai (one artboard, the bounding box: an EPS file VectorCraft wrote restores the document it carries, restored: true; other files are read by a PostScript interpreter — paths, colours and spot inks, clips, gradients, images, type in the fonts named — and come in as their TIFF preview, with a warning, when their PostScript can't be read). A PDF/.ai/.ait or SVG saved with Preserve Editing restores the native document it carries (restored: true; a PDF only when no pages are picked), unless the file was changed elsewhere since or the data can't be read: then its artwork is imported and the first warning says why. Templates (native templates, .ait) open as a new untitled document; a restored .ai keeps its path (Save writes .ai again) and opens on the artboard its editing copy names as active. A file that no built-in format reads, by its content o
Line truncated
|
||||
always,
|
||||
load::open
|
||||
),
|
||||
|
||||
@@ -479,6 +479,8 @@ pub const FORMATS: &[Format] = &[
|
||||
},
|
||||
Format { id: "emf", label: "EMF", extensions: &["emf"], mime: "image/emf", read: true, write: true, raster: false, options: metafile::OPTIONS },
|
||||
Format { id: "wmf", label: "WMF", extensions: &["wmf"], mime: "image/wmf", read: true, write: true, raster: false, options: metafile::OPTIONS },
|
||||
// HP-GL/2 plot files, read by `vectorcraft_cad::hpgl` into paths; exports don't write them.
|
||||
reader("hpgl", "HP-GL", &["plt", "hpgl", "hgl", "hpg"], "application/vnd.hp-hpgl", false),
|
||||
Format {
|
||||
id: "tga",
|
||||
label: "Targa",
|
||||
@@ -552,6 +554,10 @@ pub const OPEN_EXTS: &[&str] = &[
|
||||
"dxf",
|
||||
"emf",
|
||||
"wmf",
|
||||
"plt",
|
||||
"hpgl",
|
||||
"hgl",
|
||||
"hpg",
|
||||
"eps",
|
||||
"epsf",
|
||||
"ps",
|
||||
@@ -565,9 +571,10 @@ pub const OPEN_EXTS: &[&str] = &[
|
||||
/// `.epsf`, `.ps`, Targa, PCX, Pixar, JPEG 2000, HEIC/HEIF or JPEG XL type, nor one for the `.pns`, `.dib` and `.rle` names of
|
||||
/// PNG and BMP files (File › Open and Place still read them all). On Linux the desktop entry's EPS
|
||||
/// type (shared-mime-info's) also covers `.epsf`, so file managers offer VectorCraft for it.
|
||||
/// The packaging declares no type for HP-GL plot files either.
|
||||
pub const UNASSOCIATED_EXTS: &[&str] = &[
|
||||
"psd", "pdd", "psb", "epsf", "ps", "pns", "dib", "rle", "tga", "vda", "icb", "vst", "pcx", "pxr", "jpf", "jpx", "jp2", "j2k", "j2c", "jpc",
|
||||
"heic", "heif", "jxl",
|
||||
"heic", "heif", "jxl", "plt", "hpgl", "hgl", "hpg",
|
||||
];
|
||||
|
||||
/// The extension that picks each writable format when exporting (the format's first; PNG-8 shares
|
||||
@@ -641,6 +648,10 @@ pub const PLACE_EXTS: &[&str] = &[
|
||||
"dxf",
|
||||
"emf",
|
||||
"wmf",
|
||||
"plt",
|
||||
"hpgl",
|
||||
"hgl",
|
||||
"hpg",
|
||||
"eps",
|
||||
"epsf",
|
||||
"ps",
|
||||
|
||||
@@ -134,6 +134,17 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option<&'static Format> {
|
||||
if clipboard::looks_like_svg(&String::from_utf8_lossy(&bytes[..bytes.len().min(4096)])) {
|
||||
return format("svg");
|
||||
}
|
||||
// HP-GL has no signature: a file that starts with plotter instructions, named as one or with an
|
||||
// extension no other format (text, the formats VectorCraft names as unsupported, and the files
|
||||
// File › Open takes in other ways included) reads.
|
||||
let ext = super::extension(name);
|
||||
let other = by_name.is_some_and(|f| f.id != "hpgl")
|
||||
|| super::TEXT_EXTS.contains(&ext.as_str())
|
||||
|| super::unsupported(&ext).is_some()
|
||||
|| super::opens_as_other(&ext);
|
||||
if !other && vectorcraft_cad::hpgl::is_hpgl(bytes) {
|
||||
return format("hpgl");
|
||||
}
|
||||
by_name
|
||||
}
|
||||
|
||||
@@ -487,6 +498,10 @@ pub fn load_with(name: &str, bytes: &[u8], opts: &LoadOptions) -> Result<Loaded>
|
||||
let (doc, warnings) = super::metafile::import(bytes)?;
|
||||
(doc, warnings, false)
|
||||
}
|
||||
"hpgl" => {
|
||||
let i = vectorcraft_cad::hpgl::import(bytes).map_err(err)?;
|
||||
(i.document, i.warnings, false)
|
||||
}
|
||||
"affinity" => {
|
||||
let i = super::affinity::import(&title, bytes)?;
|
||||
preview_only = i.preview_only;
|
||||
|
||||
@@ -42,7 +42,7 @@ pub(crate) const OPTIONS: &[FormatOption] = &[
|
||||
name: "compatibility",
|
||||
ty: "string",
|
||||
default: "\"1.7\"",
|
||||
description: "PDF version: 1.3 (transparency flattened) | 1.4 | 1.5 | 1.6 | 1.7 | 2.0",
|
||||
description: "PDF version: 1.3 (transparency flattened) | 1.4 | 1.5 | 1.6 | 1.7 | 2.0 (a pdfA2b file at 1.4 with a page longer than 14,400 pt is written as 1.6, with a UserUnit as at 1.6, and a warning; see document.exportPdf)",
|
||||
},
|
||||
FormatOption {
|
||||
name: "preserveEditing",
|
||||
@@ -99,7 +99,7 @@ pub(crate) const OPTIONS: &[FormatOption] = &[
|
||||
name: "longPagesInPoints",
|
||||
ty: "boolean",
|
||||
default: "false",
|
||||
description: "a page with a side longer than 14,400 pt (200 in) is written at its size in points, without a UserUnit, at every PDF version, in files without a standard (PDF/A files from 1.5 on keep the UserUnit)",
|
||||
description: "a page with a side longer than 14,400 pt (200 in) is written at its size in points, without a UserUnit, at every PDF version, in files without a standard (PDF/A files keep the UserUnit)",
|
||||
},
|
||||
FormatOption {
|
||||
name: "createLayers",
|
||||
@@ -128,7 +128,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Export PDF",
|
||||
[],
|
||||
None,
|
||||
"{path?, preset?: \"VectorCraft Default\" (built-in or saved: pdf.preset.list), artboard? | artboards?: [i…] | range?: \"1-3, 5\" (1-based; default all, one page each), standard?: none|pdfA2b|pdfX1a (CMYK, grey and spot colours only: colours and images converted to output.destination or the CMYK output intent, untagged; transparency flattened with flattenerPreset; PDF 1.3)|pdfX3 (colours ICC-tagged; transparency flattened; PDF 1.3)|pdfX4 (transparency and layers kept; colours ICC-tagged; PDF 1.6 at most) (PDF/X: a /GTS_PDFX output intent, by default the CMYK profile in effect, embedded; TrimBox, Trapped, GTS_PDFXVersion, title and dates; fonts embedded or outlined), compatibility?: 1.3 (no transparency: it is flattened with flattenerPreset, without the layers the file leaves out; images keep no see-through pixels)|1.4|1.5|1.6|1.7 (default)|2.0 (choosing a standard sets its version: 1.7 for pdfA2b, 1.6 for pdfX4, 1.3 for pdfX1a and pdfX3, which write 1.4 as 1.3 too; pdfA2b refuses 1.3; a page with a side longer than 14,400 pt keeps its size with a UserUnit from 1.6 on: 1.5 is then written as 1.6, with 1.5's encryption; PDF/X files, 1.3 and 1.4 write it in points, and 1.3 and 1.4 (pdfX1a and pdfX3 too) refuse a page past 32,767 pt whose size or art needs a real number past 32,767; a page whose short side is 3 pt or longer but would be under 3 units at that UserUnit gets a smaller one that keeps the short side at 3 units; a warning gives each such page's size), longPagesInPoints?: false (true: such a page is written at its size in points, without a UserUnit, at every version, and 1.5 stays 1.5, in files without a standard (PDF/A files from 1.5 on keep the UserUnit); apps that keep to the limit cut it to 200 in), preserveEditing? (on in VectorCraft Default: the native document as an embedded file, which document.open restores; choosing a standard turns it off, a standard refuses it), thumbnails? (each page drawn as a /Thumb image, 106 px on its long side), fastWebView? (a linearised file: the first page shows while the rest downloads; it stays linearised when encrypted), viewAfterSaving? (the app opens the written file), createLayers? (each layer and sublayer but template layers as a PDF layer, an optional content group listed under its layer's: hidden layers off, non-printing ones /PrintState /OFF, locked ones locked; needs 1.5 or later; they reopen as layers; choosing pdfX1a or pdfX3 turns it off; they refuse it), includeNonPrinting? (keep layers whose Print option is off; left out by default unless createLayers), flattenerPreset?: \"\" (High Resolution) | high | medium | low | a saved preset (flattener.presets.list): what PDF 1.3 files flatten transparency with, flattener?: {object.flattenTransparency options over it}, compression?: {color?, gray?: {downsample: none|average|subsample|bicubic, ppi: 300, abovePpi: 450, compression: none|zip|jpeg|jpeg2000|auto, quality: minimum|low|medium|high|maximum}, mono?: {downsample, ppi: 1200, abovePpi: 1800, compression: none|ccittG3|ccittG4|zip|runLength} (black-and-white images), compressText?: true} (images above abovePpi are resampled to ppi; auto keeps JPEGs JPEG, the others lossless; JPEG needs opaque images; none, jpeg2000, CCITT and runLength are written as ZIP with a warning), marks?: {trim, registration, colorBars, pageInfo, kind: roman|japanese, weight: 0.25 (trim marks and targets), offset: 6 (pt from the artboard, at least the bleed)} (printer's marks in [Registration], every plate: trim marks, registration targets mid-side, CMYK/spot/black-tint colour bars on top, page information (title, artboard, date UTC) below), bleed?: {useDocument (the document's bleed, document.setup), top, bottom, left, right} (pt; art in the bleed is kept; a PDF/X file with no bleed on a side where the document's bleed has one comes back with a warning). Each page: TrimBox = artboard, BleedBox = artboard + bleed, MediaBox = that + the marks' room (art clipped to the BleedBox), output?: {conversion: none (default)|destination (every colour into the destination profile's model, CMYK or RGB, also colours of that model in another profile)|preserveNumbers (only colours of the other model and Lab; those in the destination's model keep their numbers) (grey stays grey; images are converted too, printer's marks are not), destination: profile name (edit.colorSettings lists them; default: the document's profile for its colour mode), profiles: none (device colours)|all|destination|taggedSource (documents assigned a profile: edit.assignProfile) (ICC-based colours: CMYK with the destination or document CMYK profile, RGB as sRGB, grey with the sRGB tone curve; PDF/A, pdfX3 and pdfX4 always, whatever profiles says), outputIntent: profile name (embedded as the catalog's /GTS_PDFX output intent; a name that isn't a profile is only named, which pdfX1a and pdfX3 allow only with a registry and pdfX4 refuses; pdfX1a needs a CMYK one), outputCondition, outputConditionId (default: outputIn
Line truncated
|
||||
"{path?, preset?: \"VectorCraft Default\" (built-in or saved: pdf.preset.list), artboard? | artboards?: [i…] | range?: \"1-3, 5\" (1-based; default all, one page each), standard?: none|pdfA2b|pdfX1a (CMYK, grey and spot colours only: colours and images converted to output.destination or the CMYK output intent, untagged; transparency flattened with flattenerPreset; PDF 1.3)|pdfX3 (colours ICC-tagged; transparency flattened; PDF 1.3)|pdfX4 (transparency and layers kept; colours ICC-tagged; PDF 1.6 at most) (PDF/X: a /GTS_PDFX output intent, by default the CMYK profile in effect, embedded; TrimBox, Trapped, GTS_PDFXVersion, title and dates; fonts embedded or outlined), compatibility?: 1.3 (no transparency: it is flattened with flattenerPreset, without the layers the file leaves out; images keep no see-through pixels)|1.4|1.5|1.6|1.7 (default)|2.0 (choosing a standard sets its version: 1.7 for pdfA2b, 1.6 for pdfX4, 1.3 for pdfX1a and pdfX3, which write 1.4 as 1.3 too; pdfA2b refuses 1.3; a page with a side longer than 14,400 pt keeps its size with a UserUnit from 1.6 on: 1.5 is then written as 1.6, with 1.5's encryption, and so is 1.4 in pdfA2b files, with a warning; PDF/X files and other 1.3 and 1.4 files write it in points, and 1.3 and 1.4 (pdfX1a and pdfX3 too) refuse a page past 32,767 pt whose size or art needs a real number past 32,767; a page whose short side is 3 pt or longer but would be under 3 units at that UserUnit gets a smaller one that keeps the short side at 3 units; a warning gives each such page's size), longPagesInPoints?: false (true: such a page is written at its size in points, without a UserUnit, at every version, and 1.5 stays 1.5, in files without a standard (PDF/A files keep the UserUnit); apps that keep to the limit cut it to 200 in), preserveEditing? (on in VectorCraft Default: the native document as an embedded file, which document.open restores; choosing a standard turns it off, a standard refuses it), thumbnails? (each page drawn as a /Thumb image, 106 px on its long side), fastWebView? (a linearised file: the first page shows while the rest downloads; it stays linearised when encrypted), viewAfterSaving? (the app opens the written file), createLayers? (each layer and sublayer but template layers as a PDF layer, an optional content group listed under its layer's: hidden layers off, non-printing ones /PrintState /OFF, locked ones locked; needs 1.5 or later; they reopen as layers; choosing pdfX1a or pdfX3 turns it off; they refuse it), includeNonPrinting? (keep layers whose Print option is off; left out by default unless createLayers), flattenerPreset?: \"\" (High Resolution) | high | medium | low | a saved preset (flattener.presets.list): what PDF 1.3 files flatten transparency with, flattener?: {object.flattenTransparency options over it}, compression?: {color?, gray?: {downsample: none|average|subsample|bicubic, ppi: 300, abovePpi: 450, compression: none|zip|jpeg|jpeg2000|auto, quality: minimum|low|medium|high|maximum}, mono?: {downsample, ppi: 1200, abovePpi: 1800, compression: none|ccittG3|ccittG4|zip|runLength} (black-and-white images), compressText?: true} (images above abovePpi are resampled to ppi; auto keeps JPEGs JPEG, the others lossless; JPEG needs opaque images; none, jpeg2000, CCITT and runLength are written as ZIP with a warning), marks?: {trim, registration, colorBars, pageInfo, kind: roman|japanese, weight: 0.25 (trim marks and targets), offset: 6 (pt from the artboard, at least the bleed)} (printer's marks in [Registration], every plate: trim marks, registration targets mid-side, CMYK/spot/black-tint colour bars on top, page information (title, artboard, date UTC) below), bleed?: {useDocument (the document's bleed, document.setup), top, bottom, left, right} (pt; art in the bleed is kept; a PDF/X file with no bleed on a side where the document's bleed has one comes back with a warning). Each page: TrimBox = artboard, BleedBox = artboard + bleed, MediaBox = that + the marks' room (art clipped to the BleedBox), output?: {conversion: none (default)|destination (every colour into the destination profile's model, CMYK or RGB, also colours of that model in another profile)|preserveNumbers (only colours of the other model and Lab; those in the destination's model keep their numbers) (grey stays grey; images are converted too, printer's marks are not), destination: profile name (edit.colorSettings lists them; default: the document's profile for its colour mode), profiles: none (device colours)|all|destination|taggedSource (documents assigned a profile: edit.assignProfile) (ICC-based colours: CMYK with the destination or document CMYK profile, RGB as sRGB, grey with the sRGB tone curve; PDF/A, pdfX3 and pdfX4 always, whatever profiles says), outputIntent: profile name (embedded as the catalog's /GTS_PDFX output intent; a name that isn't a profile is only named, which pdfX1a and pdfX3 allow only with a registry and pdfX4 refuses; pdfX1a needs a CMYK one), out
Line truncated
|
||||
has_doc,
|
||||
export_pdf
|
||||
),
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
//! HP-GL plotter files through the commands: `document.open` by extension or by content, File ›
|
||||
//! Place as one group of paths, the dialogs' filters and the format list, and files that aren't
|
||||
//! HP-GL refused with a message.
|
||||
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::NodeKind;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn b64(bytes: &[u8]) -> String {
|
||||
vectorcraft_format::base64_encode(bytes)
|
||||
}
|
||||
|
||||
/// An inch-wide red square and a black diagonal, with the thinnest pen.
|
||||
const PLOT: &[u8] = b"IN;SP2;PW0;PA0,0;PD1016,0,1016,1016,0,1016,0,0;PU;SP1;PD1016,1016;PU;";
|
||||
|
||||
#[test]
|
||||
fn document_open_reads_a_plot_file() {
|
||||
let mut s = Session::new();
|
||||
let r = s.execute("document.open", &json!({"name": "part.plt", "dataBase64": b64(PLOT)})).unwrap();
|
||||
assert_eq!((r["format"].as_str(), r["title"].as_str()), (Some("hpgl"), Some("part.plt")), "{r}");
|
||||
let st = s.doc().unwrap();
|
||||
let board = st.doc.artboards[0].rect;
|
||||
assert!((board.width() - 72.25).abs() < 1e-9 && (board.height() - 72.25).abs() < 1e-9, "{board:?}");
|
||||
let mut paths = vec![];
|
||||
st.doc.walk(|n| {
|
||||
if let NodeKind::Path { .. } = n.kind {
|
||||
paths.push(n.clone());
|
||||
}
|
||||
});
|
||||
assert_eq!(paths.len(), 2);
|
||||
// Opened as artwork: Save asks for a name.
|
||||
assert!(st.path.is_none());
|
||||
|
||||
// A plot file opens by its content in any of its names, and under a name no format reads.
|
||||
for name in ["drawing.prn", "PART.HPGL", "part.hgl", "part.hpg"] {
|
||||
let r = Session::new().execute("document.open", &json!({"name": name, "dataBase64": b64(PLOT)})).unwrap();
|
||||
assert_eq!(r["format"].as_str(), Some("hpgl"), "{name}: {r}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_place_places_a_plot_file_as_one_group() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 300, "height": 200})).unwrap();
|
||||
let p = json!({"name": "part.plt", "dataBase64": b64(PLOT), "thumbnail": 16});
|
||||
let info = s.execute("file.place.info", &p).unwrap();
|
||||
assert_eq!(info["format"], "hpgl", "{info}");
|
||||
assert!((info["width"].as_f64().unwrap() - 72.25).abs() < 1e-9, "{info}");
|
||||
let r = s.execute("file.place", &p).unwrap();
|
||||
assert_eq!((&r["format"], r["ids"].as_array().map(Vec::len)), (&json!("hpgl"), Some(1)), "{r}");
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
let group = doc.node(vectorcraft_doc::NodeId(r["ids"][0].as_u64().unwrap())).unwrap();
|
||||
let NodeKind::Group { children, clip, .. } = &group.kind else { panic!("a group, got {:?}", group.kind) };
|
||||
assert_eq!((children.len(), *clip), (2, false), "the art's bounds, not clipped");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn files_that_are_not_hpgl_are_refused() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 100, "height": 50})).unwrap();
|
||||
let before = s.doc().unwrap().doc.artboards[0].rect.size();
|
||||
let script = b"set terminal png\nplot sin(x)\n";
|
||||
let e = s.execute("document.open", &json!({"name": "graph.plt", "dataBase64": b64(script)})).expect_err("refused");
|
||||
assert!(e.to_string().contains("not an HP-GL file"), "{e}");
|
||||
assert_eq!(s.doc().unwrap().doc.artboards[0].rect.size(), before);
|
||||
assert!(s.execute("file.place", &json!({"name": "graph.plt", "dataBase64": b64(script)})).is_err());
|
||||
// Text that isn't a plot opens by its name, not as HP-GL.
|
||||
assert!(detect("notes.txt", b"IN THE BEGINNING").is_none_or(|f| f.id != "hpgl"));
|
||||
// Only a plot file's own names, and names no other format reads, are read by their content:
|
||||
// text, other formats' files and the formats named as unsupported (a DWG drawing starts with
|
||||
// `AC` and digits) aren't, even when they start like a plot.
|
||||
for name in ["notes.txt", "notes.svg", "picture.tga", "drawing.dxf", "site.dwg", "old.pct"] {
|
||||
for bytes in [&b"IN"[..], b"in 5;", b"AC1032\0\0", PLOT] {
|
||||
assert!(detect(name, bytes).is_none_or(|f| f.id != "hpgl"), "{name}: {:?}", String::from_utf8_lossy(bytes));
|
||||
}
|
||||
}
|
||||
let text = s.execute("file.place", &json!({"name": "notes.txt", "dataBase64": b64(b"IN 5; PA 10;")})).unwrap();
|
||||
assert_eq!(text["format"], "txt", "{text}");
|
||||
}
|
||||
|
||||
/// A plot file named as one opens when its first instruction is one a plot file seldom starts with;
|
||||
/// under another name it isn't taken for HP-GL.
|
||||
#[test]
|
||||
fn a_plot_file_by_its_name_may_start_with_any_instruction() {
|
||||
let plot = b"SI0.2,0.3;SP1;PA0,0;PD1016,0;";
|
||||
let r = Session::new().execute("document.open", &json!({"name": "part.plt", "dataBase64": b64(plot)})).unwrap();
|
||||
assert_eq!(r["format"].as_str(), Some("hpgl"), "{r}");
|
||||
assert!(detect("drawing.prn", plot).is_none_or(|f| f.id != "hpgl"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_dialogs_and_the_format_list_name_hpgl() {
|
||||
let exts = ["plt", "hpgl", "hgl", "hpg"];
|
||||
for e in exts {
|
||||
assert!(OPEN_EXTS.contains(&e) && PLACE_EXTS.contains(&e) && UNASSOCIATED_EXTS.contains(&e), "{e}");
|
||||
assert_eq!(format_for_name(&format!("a.{e}")).map(|f| f.id), Some("hpgl"), "{e}");
|
||||
}
|
||||
let filter = ("HP-GL", &exts[..]);
|
||||
assert!(open_filters().any(|f| f == filter) && place_filters().any(|f| f == filter));
|
||||
let r = Session::new().execute("document.formats", &json!({})).unwrap();
|
||||
let f = r["formats"].as_array().unwrap().iter().find(|f| f["id"] == "hpgl").unwrap().clone();
|
||||
assert_eq!((f["read"].as_bool(), f["write"].as_bool(), f["raster"].as_bool()), (Some(true), Some(false), Some(false)));
|
||||
}
|
||||
@@ -41,7 +41,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Check Links",
|
||||
[],
|
||||
None,
|
||||
"{ids?: [image or placed document ids] (default: every linked image and placed document)} where each linked file is and whether it changed since it was read → {links: [{name, path, status: ok|modified|missing, ids, found?: path (found away from its path: relative to the document, or by name in its folder), preview: true (showing the low-resolution preview, not the file), refused?: why the path is not followed (another computer, a device or a URL: it counts as missing)}], missing, modified}",
|
||||
"{ids?: [image or placed document ids] (default: every linked image and placed document)} where each linked file is and whether it changed since it was read → {links: [{name, path?, status: ok|modified|missing, ids, found?: path (found away from its path: relative to the document, or by name in its folder), preview: true (showing the low-resolution preview, not the file), refused?: why the path is not followed (another computer, a device or a URL: it counts as missing)}], missing, modified}; under automation read roots, path and found are left out when the file they name is outside the roots",
|
||||
has_doc,
|
||||
check
|
||||
),
|
||||
@@ -77,7 +77,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Links",
|
||||
[],
|
||||
None,
|
||||
"{show?: all (default)|missing|modified|embedded, sort?: name|kind (file format)|status (missing, modified, ok, embedded); default: stacking order; top first} every image object and placed document in the layers, as the Links panel lists them → {links: [{id, name, linked, status: ok|modified|missing|embedded, format, pixelWidth, pixelHeight, path?, found?: path (found away from its path), page?, preview?: true (showing the saved preview), note?: said of an image past the decode allowance (shown at reduced size, or too large to draw), document?: true (a placed document: pageWidth and pageHeight in pt instead of pixels)}], missing, modified, embedded}",
|
||||
"{show?: all (default)|missing|modified|embedded, sort?: name|kind (file format)|status (missing, modified, ok, embedded); default: stacking order; top first} every image object and placed document in the layers, as the Links panel lists them → {links: [{id, name, linked, status: ok|modified|missing|embedded, format, pixelWidth, pixelHeight, path?, found?: path (found away from its path), page?, preview?: true (showing the saved preview), note?: said of an image past the decode allowance (shown at reduced size, or too large to draw), document?: true (a placed document: pageWidth and pageHeight in pt instead of pixels)}], missing, modified, embedded}; under automation read roots, path and found are left out when the file they name is outside the roots",
|
||||
has_doc,
|
||||
list
|
||||
),
|
||||
@@ -113,7 +113,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Link Info",
|
||||
[],
|
||||
None,
|
||||
"{id?} an image's or placed document's Link Info (default: the one selected) → image.info's fields (id, name, linked, link, colorMode, pixelWidth, pixelHeight, width, height) and status: ok|modified|missing|embedded, format, ppi: [x, y] (the file's), effectivePpi: [x, y] (at its placed size), scale: [x%, y%] (of its 100% size), rotation (degrees, counter-clockwise), placement: {preserve, align, clip}; linked images also fileName, location (its folder), page?, fileSize? (bytes), modified?, created? (ms since the Unix epoch), refused? (why the path is not followed); a placed document has document: true, pageWidth and pageHeight (pt) instead of pixels, ppi and colorMode, and its scale is of its artboard's or page's 100% size",
|
||||
"{id?} an image's or placed document's Link Info (default: the one selected) → image.info's fields (id, name, linked, link, colorMode, pixelWidth, pixelHeight, width, height) and status: ok|modified|missing|embedded, format, ppi: [x, y] (the file's), effectivePpi: [x, y] (at its placed size), scale: [x%, y%] (of its 100% size), rotation (degrees, counter-clockwise), placement: {preserve, align, clip}; linked images also fileName, location (its folder), page?, fileSize? (bytes), modified?, created? (ms since the Unix epoch), refused? (why the path is not followed); a placed document has document: true, pageWidth and pageHeight (pt) instead of pixels, ppi and colorMode, and its scale is of its artboard's or page's 100% size; under automation read roots, a link whose file is outside the roots has no link, path or location, and fileSize and modified come only from the file",
|
||||
has_doc,
|
||||
info
|
||||
),
|
||||
@@ -882,17 +882,35 @@ fn install(d: &mut Document, ids: &[NodeId], (link, img): FileRead, force: Optio
|
||||
}
|
||||
|
||||
/// Groups (of the document saved at `doc_path`) as `{name, path, ids}` rows (those with objects in
|
||||
/// the layers), with `refused`: why the path isn't followed, when it isn't.
|
||||
/// the layers), with `refused`: why the path isn't followed, when it isn't. `path` is left out
|
||||
/// where [`path_shown`] says so.
|
||||
fn rows<'a>(gs: impl Iterator<Item = &'a Group>, doc_path: Option<&str>) -> Vec<Value> {
|
||||
gs.filter(|g| !g.ids.is_empty())
|
||||
.map(|g| {
|
||||
let mut row = json!({ "name": g.link.name(), "path": g.link.path, "ids": ids_json(&g.ids) });
|
||||
let mut row = json!({ "name": g.link.name(), "ids": ids_json(&g.ids) });
|
||||
note_path(&mut row, "path", &g.link.path, doc_path);
|
||||
note_refused(&mut row, &g.link, doc_path);
|
||||
row
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// May a reply to the caller asking now carry the link path `path` (of the document saved at
|
||||
/// `doc_path`)? Always without read roots in force; with them, only when the file it names (a
|
||||
/// relative path taken from the document's folder) lies inside them.
|
||||
fn path_shown(path: &str, doc_path: Option<&str>) -> bool {
|
||||
crate::file_access::current().is_none() || fileio::document_path(path, doc_path).is_ok_and(|p| crate::file_access::check_read(&p).is_ok())
|
||||
}
|
||||
|
||||
/// `row[key] = path` when [`path_shown`] says so; otherwise `row` has no `key`.
|
||||
fn note_path(row: &mut Value, key: &str, path: &str, doc_path: Option<&str>) {
|
||||
if path_shown(path, doc_path) {
|
||||
row[key] = json!(path);
|
||||
} else if let Some(o) = row.as_object_mut() {
|
||||
o.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// `row["refused"]`: why the path of `link` isn't followed, when it isn't ([`refused`]).
|
||||
fn note_refused(row: &mut Value, link: &LinkInfo, doc_path: Option<&str>) {
|
||||
if let Some(r) = refused(link, doc_path) {
|
||||
@@ -1188,10 +1206,12 @@ fn check(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (status, found) = probe(g, LinkBase::of(st));
|
||||
missing += usize::from(status == Status::Missing);
|
||||
modified += usize::from(status == Status::Modified);
|
||||
let mut row = json!({ "name": g.link.name(), "path": g.link.path, "status": status.as_str(), "ids": ids_json(&g.ids) });
|
||||
note_refused(&mut row, &g.link, LinkBase::of(st).doc);
|
||||
let doc_path = LinkBase::of(st).doc;
|
||||
let mut row = json!({ "name": g.link.name(), "status": status.as_str(), "ids": ids_json(&g.ids) });
|
||||
note_path(&mut row, "path", &g.link.path, doc_path);
|
||||
note_refused(&mut row, &g.link, doc_path);
|
||||
if let Some(f) = found.filter(|f| f.path != g.link.path) {
|
||||
row["found"] = json!(f.path);
|
||||
note_path(&mut row, "found", &f.path, doc_path);
|
||||
}
|
||||
if stale(&st.doc, g) {
|
||||
row["preview"] = json!(true);
|
||||
@@ -1663,14 +1683,15 @@ fn list(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
o.insert("linked".into(), json!(link.is_some()));
|
||||
o.insert("status".into(), json!(status));
|
||||
}
|
||||
let doc_path = LinkBase::of(st).doc;
|
||||
if let Some(l) = link {
|
||||
row["path"] = json!(l.path);
|
||||
note_path(&mut row, "path", &l.path, doc_path);
|
||||
if let Some(page) = l.page {
|
||||
row["page"] = json!(page);
|
||||
}
|
||||
}
|
||||
if let Some(f) = found {
|
||||
row["found"] = json!(f);
|
||||
note_path(&mut row, "found", f, doc_path);
|
||||
}
|
||||
if preview {
|
||||
row["preview"] = json!(true);
|
||||
@@ -1811,13 +1832,17 @@ fn placement_json(o: PlacementOptions) -> Value {
|
||||
|
||||
/// Link Info's fields of the linked file `l` (of object `id` of `st`) into `out`.
|
||||
fn file_fields(st: &crate::DocState, id: NodeId, l: &LinkInfo, out: &mut Value) {
|
||||
// Whether the path is shown is judged for the caller asking, before the opener's roots apply.
|
||||
let shown = path_shown(&l.path, LinkBase::of(st).doc);
|
||||
// A confined caller reads within its own roots; anyone else within what the caller that
|
||||
// opened the document may read (never wider than the caller asking now).
|
||||
let roots = crate::file_access::current().or_else(|| st.opened_by.clone());
|
||||
crate::file_access::confine(roots.as_ref(), || file_fields_here(st, id, l, out));
|
||||
crate::file_access::confine(roots.as_ref(), || file_fields_here(st, id, l, shown, out));
|
||||
}
|
||||
|
||||
fn file_fields_here(st: &crate::DocState, id: NodeId, l: &LinkInfo, out: &mut Value) {
|
||||
/// [`file_fields`]; without `shown`, the link's path (`path`, `link`) and folder (`location`)
|
||||
/// and the size and date the document keeps of its file are left out.
|
||||
fn file_fields_here(st: &crate::DocState, id: NodeId, l: &LinkInfo, shown: bool, out: &mut Value) {
|
||||
let base = LinkBase::of(st);
|
||||
let status = groups(&st.doc, Some(&[id])).first().map_or(Status::Missing, |g| probe(g, base).0);
|
||||
let doc_path = base.doc;
|
||||
@@ -1825,13 +1850,20 @@ fn file_fields_here(st: &crate::DocState, id: NodeId, l: &LinkInfo, out: &mut Va
|
||||
let stamp = path.as_deref().and_then(file_stamp);
|
||||
out["status"] = json!(status.as_str());
|
||||
note_refused(out, l, doc_path);
|
||||
out["path"] = json!(l.path);
|
||||
if shown {
|
||||
out["path"] = json!(l.path);
|
||||
out["location"] = json!(l.folder());
|
||||
} else if let Some(o) = out.as_object_mut() {
|
||||
for k in ["path", "link", "location"] {
|
||||
o.remove(k);
|
||||
}
|
||||
}
|
||||
out["fileName"] = json!(l.name());
|
||||
out["location"] = json!(l.folder());
|
||||
let kept = |v: Option<u64>| v.filter(|_| shown);
|
||||
for (k, v) in [
|
||||
("page", l.page.map(u64::from)),
|
||||
("fileSize", stamp.map(|s| s.0).or(l.size)),
|
||||
("modified", stamp.and_then(|s| s.1).or(l.modified)),
|
||||
("fileSize", stamp.map(|s| s.0).or(kept(l.size))),
|
||||
("modified", stamp.and_then(|s| s.1).or(kept(l.modified))),
|
||||
("created", path.as_deref().and_then(file_created)),
|
||||
] {
|
||||
if let Some(v) = v {
|
||||
|
||||
@@ -871,7 +871,7 @@ fn rasterize(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn has_image(s: &Session) -> std::result::Result<(), String> {
|
||||
pub(crate) fn has_image(s: &Session) -> std::result::Result<(), String> {
|
||||
has_selection(s)?;
|
||||
let st = s.active().ok_or("no document open")?;
|
||||
if st.selection.objects.iter().any(|id| matches!(st.doc.node(*id).map(|n| &n.kind), Some(NodeKind::Image(_)))) {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
//! the file declares ([`fileio::ppi`]; 72 ppi when it declares none), linked to its file with
|
||||
//! `link` ([`super::links`]).
|
||||
//! - An SVG becomes one group of its art; so does a DXF drawing, read with the DXF options
|
||||
//! (`dxf`, [`fileio::dxfimport`]).
|
||||
//! (`dxf`, [`fileio::dxfimport`]), and an HP-GL plot file (`vectorcraft_cad::hpgl`).
|
||||
//! - An EMF or WMF picture becomes one group, clipped to its frame (`crop: "crop"`) or bounded by
|
||||
//! its art (`crop: "bounding"`).
|
||||
//! - An EPS file (or a PostScript .ai) becomes one group, clipped to its bounding box or bounded by
|
||||
@@ -69,7 +69,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Place…",
|
||||
["File"],
|
||||
Some("Cmd+Shift+P"),
|
||||
"{path | name | dataBase64 (a file's path, or its name with its bytes), link?: true (a raster image keeps its file's path; a VectorCraft document at path becomes one locked placed document showing artboard `page`, linked to the file and read again when it changes (links.update; saving it in this app updates it at once), drawn as vectors in every output; so does a PDF/.ai page at path, showing its `crop` box, unless password or textAs: \"outlines\" is given; false: an editable copy of its art, as from dataBase64; other files are embedded), text?: {characterSet?: \"unicode\" (UTF-8, or UTF-16 with a byte-order mark; other bytes as the platform's 8-bit set) | \"ansi\" (the platform's 8-bit set), platform?: \"windows\" (Windows-1252) | \"mac\" (Mac Roman), removeLineReturns?: false (each block of lines becomes one paragraph; blank lines end paragraphs), removeParagraphReturns?: false (drop blank lines), replaceSpaces?: n (runs of n ≥ 2 spaces become a tab)} (a .txt file, placed as area type filling rect, the replaced object's bounds, or else the artboard less a 36 pt margin), template?: false (onto a new locked template layer below the current layer), replace?: false (swap the one selected object, keeping its stacking place and transform; no at/rect; refused while an object of a symbol instance's art is selected), at?: [x, y] centre (default: the first artboard's centre), rect?: [x, y, width, height] fit inside; aspect kept (wins over at), page?: 1 (PDF/.ai page, or a native document's artboard), crop?: \"crop\" (clipped to that page or artboard, default; an SVG, only when given: framed and clipped by its viewBox, else by its art's bounds) | \"bounding\" (the bounds of the art that shows, with its strokes and effects, as an export measures them: hidden objects and guides left out) | \"art\" | \"trim\" | \"bleed\" | \"media\" (a PDF page's boxes), password? (an encrypted PDF), dxf?: {…the DXF options of document.open} (a .dxf drawing: fit fills the artboard under at, else the first; fitted or uncentred art lands with its drawing's artboard on that artboard, else it is centred on at)} → {ids, name, format, linked, width, height, warnings}. Raster images come in at 100% of their physical size (the file's ppi, else 72); SVG, DXF, PDF/.ai, EMF/WMF (clipped to the picture's frame), EPS (clipped to its bounding box; as document.open reads it), a .ai saved without PDF compatibility (from its editing data, as document.open reads it: the art of the layers that show on artboard `page`, clipped to that artboard unless crop is \"bounding\") and native documents as one group, with the images, symbols, patterns, swatches and brushes they use. One undo step; selects what it placed (unless on a template layer); never touches the clipboard. A PNG, JPEG, GIF or WebP image whose pixels can't be read (a file cut short) is an error, and nothing is placed",
|
||||
"{path | name | dataBase64 (a file's path, or its name with its bytes), link?: true (a raster image keeps its file's path; a VectorCraft document at path becomes one locked placed document showing artboard `page`, linked to the file and read again when it changes (links.update; saving it in this app updates it at once), drawn as vectors in every output; so does a PDF/.ai page at path, showing its `crop` box, unless password or textAs: \"outlines\" is given; false: an editable copy of its art, as from dataBase64; other files are embedded), text?: {characterSet?: \"unicode\" (UTF-8, or UTF-16 with a byte-order mark; other bytes as the platform's 8-bit set) | \"ansi\" (the platform's 8-bit set), platform?: \"windows\" (Windows-1252) | \"mac\" (Mac Roman), removeLineReturns?: false (each block of lines becomes one paragraph; blank lines end paragraphs), removeParagraphReturns?: false (drop blank lines), replaceSpaces?: n (runs of n ≥ 2 spaces become a tab)} (a .txt file, placed as area type filling rect, the replaced object's bounds, or else the artboard less a 36 pt margin), template?: false (onto a new locked template layer below the current layer), replace?: false (swap the one selected object, keeping its stacking place and transform; no at/rect; refused while an object of a symbol instance's art is selected), at?: [x, y] centre (default: the first artboard's centre), rect?: [x, y, width, height] fit inside; aspect kept (wins over at), page?: 1 (PDF/.ai page, or a native document's artboard), crop?: \"crop\" (clipped to that page or artboard, default; an SVG, only when given: framed and clipped by its viewBox, else by its art's bounds) | \"bounding\" (the bounds of the art that shows, with its strokes and effects, as an export measures them: hidden objects and guides left out) | \"art\" | \"trim\" | \"bleed\" | \"media\" (a PDF page's boxes), password? (an encrypted PDF), dxf?: {…the DXF options of document.open} (a .dxf drawing: fit fills the artboard under at, else the first; fitted or uncentred art lands with its drawing's artboard on that artboard, else it is centred on at)} → {ids, name, format, linked, width, height, warnings}. Raster images come in at 100% of their physical size (the file's ppi, else 72); SVG, DXF, HP-GL (.plt, .hpgl, .hgl, .hpg), PDF/.ai, EMF/WMF (clipped to the picture's frame), EPS (clipped to its bounding box; as document.open reads it), a .ai saved without PDF compatibility (from its editing data, as document.open reads it: the art of the layers that show on artboard `page`, clipped to that artboard unless crop is \"bounding\") and native documents as one group, with the images, symbols, patterns, swatches and brushes they use. One undo step; selects what it placed (unless on a template layer); never touches the clipboard. A PNG, JPEG, GIF or WebP image whose pixels can't be read (a file cut short) is an error, and nothing is placed",
|
||||
has_doc,
|
||||
place
|
||||
),
|
||||
@@ -264,7 +264,7 @@ fn load(p: &Value, cmd: &str, board: Option<Rect>, link_pdf: bool) -> Result<Loa
|
||||
let board = doc.artboards.first().map(|a| a.rect).filter(|_| anchored);
|
||||
let layers = std::mem::take(&mut doc.layers);
|
||||
let symbols = doc.symbol_boxes();
|
||||
let (nodes, clip) = if matches!(format.id, "svg" | "svgz" | "dxf") {
|
||||
let (nodes, clip) = if matches!(format.id, "svg" | "svgz" | "dxf" | "hpgl") {
|
||||
// An SVG with `crop: "crop"` asked for is framed and clipped by its viewBox (its artboard),
|
||||
// as a PDF by its page: art drawn on a shared canvas keeps its place and scale (#788). By
|
||||
// default, as Illustrator places one, it is its art's bounds.
|
||||
|
||||
@@ -741,17 +741,18 @@ fn install(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
// Installed plug-ins stay installed after a restart (#2541). The module is written first and its
|
||||
// grant only once it is there; when the grant can't be written, the store gets the module it
|
||||
// held back. A failed write of that module is only logged, and the grant then doesn't cover
|
||||
// the new module. A stored module that can't be read back isn't replaced by one that needs a
|
||||
// grant.
|
||||
// held back. A failed write of that module is only logged: the store then holds the new module,
|
||||
// which the grant doesn't cover. A write that reports a failure may still have put the old
|
||||
// module back, so the store is read again to tell which module it holds. A stored module that
|
||||
// can't be read back isn't replaced by one that needs a grant.
|
||||
let previous = s.stored_plugin_bytes(plugin.id());
|
||||
let mut stored = (previous.is_ok() || !m.asks_permissions()) && s.store_plugin(plugin.id(), &bytes);
|
||||
let holds_new = |s: &Session| s.stored_plugin_bytes(plugin.id()).is_ok_and(|held| held.as_deref() == Some(&bytes[..]));
|
||||
if m.asks_permissions() {
|
||||
let g = Grant { id: m.id.clone(), hash: plugin.hash().to_string(), permissions: wanted };
|
||||
registry::grant(g.clone());
|
||||
if stored && !s.store_grant(&m.id, Some(g)) {
|
||||
s.put_back_plugin(plugin.id(), previous.ok().flatten().as_deref());
|
||||
stored = false;
|
||||
stored = !s.put_back_plugin(plugin.id(), previous.ok().flatten().as_deref()) && holds_new(s);
|
||||
}
|
||||
} else {
|
||||
// This module takes the id over, so nothing the user approved for the id applies any more.
|
||||
@@ -764,7 +765,7 @@ fn install(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// stays; one that can't be opened or read may approve the old bytes once it reads again.
|
||||
if stored && !s.store_grant(&m.id, None) && s.holds_plugin_grant(&m.id) {
|
||||
let back = previous.as_ref().is_ok_and(|held| s.put_back_plugin(plugin.id(), held.as_deref()));
|
||||
stored = !back;
|
||||
stored = !back && holds_new(s);
|
||||
if !back || matches!(previous, Ok(None)) {
|
||||
s.ignore_stored_grant(&m.id);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ pub(super) const ALLOWED: &[&str] = &[
|
||||
"addon.color.strokeToFill",
|
||||
"addon.color.swatchLegend",
|
||||
"addon.contour.set",
|
||||
"addon.image.removeBackground",
|
||||
"addon.pathtools.addPoints",
|
||||
"addon.pathtools.close",
|
||||
"addon.pathtools.corner",
|
||||
|
||||
@@ -13,11 +13,12 @@
|
||||
//! to loses its characters' own fill (stroke) when the style has fills (strokes), so the style's
|
||||
//! paint shows instead of the character colour under it.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::Paint;
|
||||
use vectorcraft_doc::{Appearance, DEFAULT_GRAPHIC_STYLE, Document, GraphicStyle, Node, NodeId, NodeKind};
|
||||
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};
|
||||
@@ -260,9 +261,14 @@ fn override_chars(n: &mut Node, ap: &Appearance) {
|
||||
}
|
||||
|
||||
/// Give `n` style `g`: its appearance (a group or layer too: its own, above its contents; type its
|
||||
/// characters' colour too with `chars`, Override Character Color) and transparency.
|
||||
fn style_node(n: &mut Node, g: &GraphicStyle, chars: bool) {
|
||||
n.appearance = g.appearance_on(n).into_owned().into();
|
||||
/// characters' colour too with `chars`, Override Character Color) and transparency. Objects that
|
||||
/// take `g`'s appearance as it is share one copy of it, made in `shared` the first time; an object
|
||||
/// whose placed gradients land on its own bounds gets its own.
|
||||
fn style_node(n: &mut Node, g: &GraphicStyle, shared: &mut Option<Shared<Appearance>>, chars: bool) {
|
||||
n.appearance = match g.appearance_on(n) {
|
||||
Cow::Borrowed(ap) => shared.get_or_insert_with(|| Shared::new(ap.clone())).clone(),
|
||||
Cow::Owned(ap) => ap.into(),
|
||||
};
|
||||
if chars {
|
||||
override_chars(n, &g.appearance);
|
||||
}
|
||||
@@ -270,9 +276,9 @@ fn style_node(n: &mut Node, g: &GraphicStyle, chars: bool) {
|
||||
}
|
||||
|
||||
/// Give `id` style `g` ([`style_node`]) and link it.
|
||||
fn apply_style(d: &mut Document, id: NodeId, g: &GraphicStyle, chars: bool) {
|
||||
fn apply_style(d: &mut Document, id: NodeId, g: &GraphicStyle, shared: &mut Option<Shared<Appearance>>, chars: bool) {
|
||||
if let Some(n) = d.node_mut(id) {
|
||||
style_node(n, g, chars);
|
||||
style_node(n, g, shared, chars);
|
||||
n.extras_mut().graphic_style = Some(g.id);
|
||||
super::object::snap_if_aligned(n);
|
||||
}
|
||||
@@ -313,7 +319,7 @@ impl Session {
|
||||
/// Color as set): the Graphic Styles panel's previews.
|
||||
pub fn styled(&self, n: &Node, g: &GraphicStyle) -> Node {
|
||||
let mut n = n.clone();
|
||||
style_node(&mut n, g, self.prefs.override_char_color);
|
||||
style_node(&mut n, g, &mut None, self.prefs.override_char_color);
|
||||
n
|
||||
}
|
||||
}
|
||||
@@ -354,8 +360,9 @@ fn style_apply(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
} else {
|
||||
d.graphic_style_id(i);
|
||||
let g = d.graphic_styles[i].clone();
|
||||
let mut shared = None;
|
||||
for id in &ids {
|
||||
apply_style(d, *id, &g, chars);
|
||||
apply_style(d, *id, &g, &mut shared, chars);
|
||||
}
|
||||
super::appearance::capture_series_writes(&before, d, &ids, (true, true), (true, true), false, true);
|
||||
}
|
||||
@@ -474,8 +481,9 @@ fn style_redefine(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (follow, edited): (Vec<NodeId>, Vec<NodeId>) = linked.into_iter().partition(|nid| d.node(*nid).is_some_and(|n| in_sync(n, &old)));
|
||||
set_link(d, &edited, None);
|
||||
let g = d.graphic_styles[i].clone();
|
||||
let mut shared = None;
|
||||
for nid in &follow {
|
||||
apply_style(d, *nid, &g, chars);
|
||||
apply_style(d, *nid, &g, &mut shared, chars);
|
||||
}
|
||||
super::appearance::capture_series_writes(&before, d, &follow, (true, true), (true, true), false, true);
|
||||
set_link(d, &[src], Some(id));
|
||||
|
||||
@@ -282,6 +282,9 @@ pub struct Session {
|
||||
pub(crate) in_tool_actions: bool,
|
||||
/// While [`Session::preview`] runs its command: its edits are the interaction's own.
|
||||
previewing: bool,
|
||||
/// The images Remove Background decoded during the interaction's preview steps, emptied when
|
||||
/// it ends ([`cmd::DecodedImages`]).
|
||||
pub(crate) decoded_images: cmd::DecodedImages,
|
||||
/// Envelope Options with no envelope selected: the options and fidelity new envelopes get
|
||||
/// (`None`: the reference app's defaults, fidelity 50); not saved.
|
||||
pub(crate) envelope_defaults: Option<(vectorcraft_doc::live::EnvelopeOptions, f64)>,
|
||||
|
||||
@@ -189,11 +189,15 @@ fn home_folder() -> String {
|
||||
}
|
||||
|
||||
/// The folders `Folder.temp`, `Folder.desktop` and the others name, `null` where there is none.
|
||||
/// On the web every one is `null`, and no platform folder is looked up.
|
||||
fn locations(home: &str) -> Data {
|
||||
if cfg!(target_arch = "wasm32") {
|
||||
return Data::Object(PLACES.iter().map(|k| (k.to_string(), Data::Null)).collect());
|
||||
}
|
||||
let env = |k: &str| std::env::var(k).ok().filter(|v| !v.is_empty());
|
||||
let join = |a: &str, b: &str| Path::new(a).join(b).to_string_lossy().into_owned();
|
||||
let exe = std::env::current_exe().ok().and_then(|p| p.parent().map(|d| d.to_string_lossy().into_owned()));
|
||||
let temp = std::env::temp_dir().to_string_lossy().into_owned();
|
||||
let temp = temp_folder().to_string_lossy().into_owned();
|
||||
let (user_data, app_data, common, system, trash) = if WINDOWS {
|
||||
(env("APPDATA"), env("PROGRAMDATA"), env("CommonProgramFiles"), env("SystemRoot"), None)
|
||||
} else if cfg!(target_os = "macos") {
|
||||
@@ -209,7 +213,6 @@ fn locations(home: &str) -> Data {
|
||||
(Some(data.clone()), Some("/usr/share".to_string()), Some("/usr/share".to_string()), Some("/".to_string()), Some(join(&data, "Trash")))
|
||||
};
|
||||
let s = |v: Option<String>| v.map_or(Data::Null, Data::String);
|
||||
let web = cfg!(target_arch = "wasm32");
|
||||
let places = vec![
|
||||
("appData", app_data),
|
||||
("appPackage", exe.clone()),
|
||||
@@ -222,7 +225,15 @@ fn locations(home: &str) -> Data {
|
||||
("trash", trash),
|
||||
("userData", user_data),
|
||||
];
|
||||
Data::Object(places.into_iter().map(|(k, v)| (k.to_string(), if web { Data::Null } else { s(v) })).collect())
|
||||
Data::Object(places.into_iter().map(|(k, v)| (k.to_string(), s(v))).collect())
|
||||
}
|
||||
|
||||
/// The names [`locations`] gives.
|
||||
const PLACES: [&str; 10] = ["appData", "appPackage", "commonFiles", "desktop", "myDocuments", "startup", "system", "temp", "trash", "userData"];
|
||||
|
||||
/// The folder for temporary files: the platform's, or on the web the root, where nothing is read.
|
||||
fn temp_folder() -> std::path::PathBuf {
|
||||
if cfg!(target_arch = "wasm32") { std::path::PathBuf::from("/") } else { std::env::temp_dir() }
|
||||
}
|
||||
|
||||
/// The start of an absolute path.
|
||||
@@ -349,12 +360,12 @@ impl Pathname {
|
||||
}
|
||||
}
|
||||
|
||||
/// `s` with every character but letters, digits and `-_.!~*'()` written as `%xx` escapes of its
|
||||
/// `s` with every character but letters, digits and `/-_.!~*'()` written as `%xx` escapes of its
|
||||
/// UTF-8 bytes (RFC 2396).
|
||||
fn encode(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len());
|
||||
for b in s.bytes() {
|
||||
if b.is_ascii_alphanumeric() || b"-_.!~*'()".contains(&b) {
|
||||
if b.is_ascii_alphanumeric() || b"/-_.!~*'()".contains(&b) {
|
||||
out.push(b as char);
|
||||
} else {
|
||||
out.push_str(&format!("%{b:02X}"));
|
||||
@@ -842,7 +853,23 @@ fn temp_name(host: &Host, _: &[Data]) -> Result<Data, HostError> {
|
||||
let mut disk = host.disk.borrow_mut();
|
||||
disk.temps += 1;
|
||||
let name = format!("vc{:05}.tmp", disk.temps);
|
||||
Ok(Data::String(std::env::temp_dir().join(name).to_string_lossy().into_owned()))
|
||||
Ok(Data::String(temp_folder().join(name).to_string_lossy().into_owned()))
|
||||
}
|
||||
|
||||
fn locations_of(host: &Host, _: &[Data]) -> Result<Data, HostError> {
|
||||
Ok(locations(&host.disk.borrow().home))
|
||||
}
|
||||
|
||||
fn encode_text(_: &Host, a: &[Data]) -> Result<Data, HostError> {
|
||||
Ok(Data::String(encode(&arg_text(a, 0))))
|
||||
}
|
||||
|
||||
fn decode_text(_: &Host, a: &[Data]) -> Result<Data, HostError> {
|
||||
Ok(Data::String(decode(&arg_text(a, 0))))
|
||||
}
|
||||
|
||||
fn encoding_available(_: &Host, a: &[Data]) -> Result<Data, HostError> {
|
||||
Ok(Data::Bool(Encoding::parse(&arg_text(a, 0)).is_some()))
|
||||
}
|
||||
|
||||
/// The folder `Folder.current` starts at in the run `host` serves: the script's own folder.
|
||||
@@ -851,10 +878,6 @@ fn start_folder(host: &Host, _: &[Data]) -> Result<Data, HostError> {
|
||||
Ok(Data::String(disk.pathname(&disk.start, &disk.start, false).fs()))
|
||||
}
|
||||
|
||||
fn locations_of(host: &Host, _: &[Data]) -> Result<Data, HostError> {
|
||||
Ok(locations(&host.disk.borrow().home))
|
||||
}
|
||||
|
||||
/// Adds `File` and `Folder` to the realm, their host functions acting for the run `link` holds.
|
||||
pub(super) fn install(realm: &mut Realm, link: &super::Link) -> Result<(), RunError> {
|
||||
let unchecked = |f: fn(&Host, &[Data]) -> Result<Data, HostError>| {
|
||||
@@ -871,7 +894,6 @@ pub(super) fn install(realm: &mut Realm, link: &super::Link) -> Result<(), RunEr
|
||||
("noPicker".into(), value(NO_PICKER)),
|
||||
("badMode".into(), value(BAD_MODE)),
|
||||
]);
|
||||
let pure = |f: fn(&str) -> Data| HostItem::Function(Rc::new(move |a| Ok(f(&arg_text(&a, 0)))));
|
||||
let system = if WINDOWS {
|
||||
"Windows"
|
||||
} else if cfg!(target_os = "macos") {
|
||||
@@ -884,10 +906,10 @@ pub(super) fn install(realm: &mut Realm, link: &super::Link) -> Result<(), RunEr
|
||||
("system".into(), value(system)),
|
||||
("lineFeed".into(), value(if WINDOWS { "Windows" } else { "Unix" })),
|
||||
("current".into(), unchecked(start_folder)),
|
||||
("locations".into(), unchecked(locations_of)),
|
||||
("encode".into(), pure(|s| Data::String(encode(s)))),
|
||||
("decode".into(), pure(|s| Data::String(decode(s)))),
|
||||
("encodingAvailable".into(), pure(|s| Data::Bool(Encoding::parse(s).is_some()))),
|
||||
("locations".into(), call(link, locations_of)),
|
||||
("encode".into(), call(link, encode_text)),
|
||||
("decode".into(), call(link, decode_text)),
|
||||
("encodingAvailable".into(), call(link, encoding_available)),
|
||||
("parse".into(), call(link, parse)),
|
||||
("relative".into(), call(link, relative)),
|
||||
("tempName".into(), call(link, temp_name)),
|
||||
@@ -955,7 +977,7 @@ mod tests {
|
||||
#[test]
|
||||
fn uri_escapes_are_utf8_both_ways() {
|
||||
assert_eq!(encode("caf\u{e9} 1"), "caf%C3%A9%201");
|
||||
assert_eq!(encode("a-_.!~*'()/b"), "a-_.!~*'()%2Fb");
|
||||
assert_eq!(encode("a-_.!~*'()/b"), "a-_.!~*'()/b");
|
||||
assert_eq!(decode("caf%C3%A9%20x"), "caf\u{e9} x");
|
||||
assert_eq!(decode("100%"), "100%");
|
||||
assert_eq!(decode("%E9"), "%E9", "a lone Latin-1 escape is not UTF-8");
|
||||
|
||||
@@ -115,6 +115,7 @@ impl Session {
|
||||
recovery: Default::default(),
|
||||
in_tool_actions: false,
|
||||
previewing: false,
|
||||
decoded_images: Default::default(),
|
||||
envelope_defaults: None,
|
||||
liquify_stroke: None,
|
||||
plane_widget_press: false,
|
||||
|
||||
@@ -335,6 +335,7 @@ impl Session {
|
||||
|
||||
pub fn commit_interaction(&mut self) -> Result<()> {
|
||||
self.clear_deferred_overlay_cache();
|
||||
self.decoded_images.empty();
|
||||
if let Some(paint) = self.active().and_then(|st| st.interaction.as_ref()).and_then(|it| it.preview_paint.clone()) {
|
||||
self.paint = paint;
|
||||
}
|
||||
@@ -414,6 +415,7 @@ impl Session {
|
||||
|
||||
pub fn cancel_interaction(&mut self) -> Result<()> {
|
||||
self.clear_deferred_overlay_cache();
|
||||
self.decoded_images.empty();
|
||||
self.pending_paint = None;
|
||||
if let Some(paint) = self
|
||||
.active()
|
||||
|
||||
@@ -190,6 +190,9 @@ fn lock_disabled(_: &Path) -> Option<std::fs::File> {
|
||||
thread_local! {
|
||||
/// Tests: [`Session::put_back_plugin`] fails on this thread, as on a full volume.
|
||||
pub(crate) static PUT_BACK_FAILS: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
||||
/// Tests: [`Session::put_back_plugin`] writes the old module back on this thread and then
|
||||
/// reports a failure, as when the last flush of a copy fails.
|
||||
pub(crate) static PUT_BACK_FAILS_AFTER_WRITE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
||||
/// Tests: moving a plug-in's settings and cache back from `<folder>.removed` ([`move_back`])
|
||||
/// fails on this thread, as when another program holds a file in the folder.
|
||||
pub(crate) static MOVE_BACK_FAILS: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
||||
@@ -504,16 +507,21 @@ impl Session {
|
||||
|
||||
/// Return the store's module for the plug-in `id` to `previous`, the bytes it held before a
|
||||
/// write ([`Session::stored_plugin_bytes`]): with `None`, the file is removed. → whether the
|
||||
/// store now holds `previous`.
|
||||
/// write reported success; a write that reports a failure may still have put `previous` back.
|
||||
pub(crate) fn put_back_plugin(&self, id: &str, previous: Option<&[u8]>) -> bool {
|
||||
#[cfg(test)]
|
||||
if PUT_BACK_FAILS.get() {
|
||||
return false;
|
||||
}
|
||||
match previous {
|
||||
let back = match previous {
|
||||
Some(bytes) => self.store_plugin(id, bytes),
|
||||
None => self.unstore_plugin(id),
|
||||
};
|
||||
#[cfg(test)]
|
||||
if PUT_BACK_FAILS_AFTER_WRITE.get() {
|
||||
return false;
|
||||
}
|
||||
back
|
||||
}
|
||||
|
||||
/// The file the plug-in `id` is stored in, spelled as [`Session::load_stored_plugins`] names it
|
||||
|
||||
@@ -115,6 +115,9 @@ fn constrain_angle_draws_boxes_along_the_turned_axes() {
|
||||
let mut s = new_doc();
|
||||
set_pref(&mut s, "constrainAngle", json!(90));
|
||||
for tool in ["rectangle", "ellipse"] {
|
||||
// The shape drawn before stays selected, and a press on its box's corner handle would
|
||||
// scale it instead of drawing.
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.select_tool(tool, ViewInfo::default()).unwrap();
|
||||
// 60 right and 20 down on the page: along axes turned 90°, 20 wide and 60 high.
|
||||
gesture(&mut s, Mods::default(), &[(Down, 100.0, 100.0), (Drag, 130.0, 110.0), (Drag, 160.0, 120.0), (Up, 160.0, 120.0)]);
|
||||
|
||||
@@ -215,6 +215,291 @@ fn type_picks_up_font_and_size() {
|
||||
assert_eq!((st.size, justify, st.fill.color().unwrap().to_hex()), (12.0, preserved_alignment, "#00ff00".into()));
|
||||
}
|
||||
|
||||
/// Sample document-space typography between text objects with different local transforms.
|
||||
#[test]
|
||||
fn scaled_type_pick_matches_effective_character_dimensions() {
|
||||
for source_area in [false, true] {
|
||||
for target_area in [false, true] {
|
||||
let mut s = session();
|
||||
let create = |s: &mut Session, area: bool, x: f64| {
|
||||
let mut p = json!({"x": x, "y": 100, "text": "Sample", "size": 12});
|
||||
if area {
|
||||
p["area"] = json!({"width": 160, "height": 80});
|
||||
}
|
||||
id_of(s.execute("text.create", &p).unwrap())
|
||||
};
|
||||
let src = create(&mut s, source_area, 100.0);
|
||||
s.execute("object.transform", &json!({"matrix": [3, 0, 0, 2, 0, 0]})).unwrap();
|
||||
let dst = create(&mut s, target_area, 300.0);
|
||||
s.execute("object.transform", &json!({"matrix": [0.5, 0, 0, 1.5, 0, 0]})).unwrap();
|
||||
let before = node(&s, dst);
|
||||
let source = node(&s, src);
|
||||
let history = undo_len(&s);
|
||||
s.execute("appearance.copyFrom", &json!({"source": src.0})).unwrap();
|
||||
let after = node(&s, dst);
|
||||
let NodeKind::Text(t) = &after.kind else { panic!("not text") };
|
||||
let NodeKind::Text(old) = &before.kind else { panic!("not text") };
|
||||
let effective = t.effective_char_style(&t.first_style()).unwrap();
|
||||
assert!((effective.size - 24.0).abs() < 1e-8, "sampled font size: {}", effective.size);
|
||||
assert!((effective.h_scale - 150.0).abs() < 1e-8, "sampled horizontal scale: {}", effective.h_scale);
|
||||
assert_eq!(t.xf, old.xf, "sampling keeps the target's transform");
|
||||
assert_eq!(node(&s, src), source);
|
||||
assert_eq!(undo_len(&s), history + 1);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(node(&s, dst), before);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(node(&s, dst), after);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A text object snapshot, including the local attributes and retained affine transform.
|
||||
fn sampled_type(s: &Session, id: NodeId) -> vectorcraft_doc::TextObject {
|
||||
let NodeKind::Text(t) = node(s, id).kind else { panic!("not text") };
|
||||
*t
|
||||
}
|
||||
|
||||
/// Compare the dimensional character attributes that text controls display in document units.
|
||||
fn assert_sampled_dimensions(actual: &vectorcraft_doc::CharStyle, expected: &vectorcraft_doc::CharStyle) {
|
||||
assert_eq!(actual.leading.is_some(), expected.leading.is_some());
|
||||
for (a, e) in [
|
||||
(actual.size, expected.size),
|
||||
(actual.h_scale, expected.h_scale),
|
||||
(actual.v_scale, expected.v_scale),
|
||||
(actual.leading.unwrap_or(0.0), expected.leading.unwrap_or(0.0)),
|
||||
(actual.baseline_shift, expected.baseline_shift),
|
||||
(actual.tcy_up_down, expected.tcy_up_down),
|
||||
(actual.tcy_left_right, expected.tcy_left_right),
|
||||
] {
|
||||
assert!((a - e).abs() < 1e-8, "sampled dimension: {a} != {e}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Rotation, shear, reflection and grouping stay on the objects while typography is copied.
|
||||
#[test]
|
||||
fn scaled_type_pick_preserves_affines_and_document_dimensions() {
|
||||
for grouped in [false, true] {
|
||||
let mut s = session();
|
||||
let src = id_of(s.execute("text.create", &json!({"x": 100, "y": 100, "text": "Source", "size": 12})).unwrap());
|
||||
s.execute("text.setStyle", &json!({"leading": 18})).unwrap();
|
||||
s.execute("text.setFormat", &json!({"hScale": 80, "vScale": 125, "baselineShift": 3, "tcyUpDown": 2, "tcyLeftRight": -1})).unwrap();
|
||||
if grouped {
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
}
|
||||
s.execute("object.transform", &json!({"matrix": [0, -3, -2, 1, 0, 0]})).unwrap();
|
||||
let src_text = sampled_type(&s, src);
|
||||
let expected = src_text.effective_char_style(&src_text.first_style()).unwrap();
|
||||
let dst = id_of(s.execute("text.create", &json!({"x": 300, "y": 100, "text": "Target", "size": 20})).unwrap());
|
||||
if grouped {
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
}
|
||||
s.execute("object.transform", &json!({"matrix": [-1, 0.5, 1, 2, 0, 0]})).unwrap();
|
||||
let before = sampled_type(&s, dst);
|
||||
s.execute("appearance.copyFrom", &json!({"source": src.0})).unwrap();
|
||||
let after = sampled_type(&s, dst);
|
||||
assert_sampled_dimensions(&after.effective_char_style(&after.first_style()).unwrap(), &expected);
|
||||
assert_eq!(after.xf, before.xf);
|
||||
assert_eq!(sampled_type(&s, src), src_text);
|
||||
let bytes = vectorcraft_format::save_file(&s.doc().unwrap().doc);
|
||||
let reopened = vectorcraft_format::load(&bytes).unwrap();
|
||||
let NodeKind::Text(t) = &reopened.node(dst).unwrap().kind else { panic!("not text") };
|
||||
assert_sampled_dimensions(&t.effective_char_style(&t.first_style()).unwrap(), &expected);
|
||||
}
|
||||
}
|
||||
|
||||
/// A transformed target compensates only the highlighted characters, retaining the others.
|
||||
#[test]
|
||||
fn scaled_type_pick_changes_only_the_highlighted_range() {
|
||||
let mut s = session();
|
||||
let src = id_of(s.execute("text.create", &json!({"x": 100, "y": 100, "text": "Source", "size": 12})).unwrap());
|
||||
s.execute("object.transform", &json!({"matrix": [3, 0, 0, 2, 0, 0]})).unwrap();
|
||||
let source = sampled_type(&s, src);
|
||||
let expected = source.effective_char_style(&source.first_style()).unwrap();
|
||||
let dst = id_of(s.execute("text.create", &json!({"x": 300, "y": 100, "text": "Sample", "size": 10})).unwrap());
|
||||
s.execute("object.transform", &json!({"matrix": [0.5, 0, 0, 3, 0, 0]})).unwrap();
|
||||
let before = sampled_type(&s, dst);
|
||||
s.select_tool("type", ViewInfo::default()).unwrap();
|
||||
s.set_tool_option("textSelection", &json!({"id": dst.0, "start": 1, "end": 4}));
|
||||
s.select_tool("eyedropper", ViewInfo::default()).unwrap();
|
||||
let history = undo_len(&s);
|
||||
s.execute("appearance.copyFrom", &json!({"source": src.0})).unwrap();
|
||||
let after = sampled_type(&s, dst);
|
||||
assert_eq!(after.runs.len(), 3);
|
||||
assert_eq!((&*after.runs[0].text, &*after.runs[1].text, &*after.runs[2].text), ("S", "amp", "le"));
|
||||
assert_eq!(after.runs[0].style, before.first_style());
|
||||
assert_eq!(after.runs[2].style, before.first_style());
|
||||
assert_sampled_dimensions(&after.effective_char_style(&after.runs[1].style).unwrap(), &expected);
|
||||
assert_eq!(after.xf, before.xf);
|
||||
assert_eq!(undo_len(&s), history + 1);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(sampled_type(&s, dst), before);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(sampled_type(&s, dst), after);
|
||||
}
|
||||
|
||||
/// Reject unusable source or target transforms before any part of a multi-object pick changes.
|
||||
#[test]
|
||||
fn scaled_type_pick_rejects_unrepresentable_dimensions_atomically() {
|
||||
for (bad_source, factor) in [(true, 0.0), (false, 0.0), (false, 1e-306), (true, f64::from_bits(1))] {
|
||||
let mut s = session();
|
||||
let size = if bad_source && factor > 0.0 { 0.1 } else { 24.0 };
|
||||
let src = id_of(s.execute("text.create", &json!({"x": 100, "y": 100, "text": "Source", "size": size, "color": "#00ff00"})).unwrap());
|
||||
if bad_source {
|
||||
s.execute("object.transform", &json!({"matrix": [factor, 0, 0, factor, 0, 0]})).unwrap();
|
||||
}
|
||||
let good = id_of(s.execute("text.create", &json!({"x": 300, "y": 100, "text": "Good", "size": 10})).unwrap());
|
||||
let other = id_of(s.execute("text.create", &json!({"x": 300, "y": 200, "text": "Other", "size": 12})).unwrap());
|
||||
if !bad_source {
|
||||
s.execute("object.transform", &json!({"matrix": [factor, 0, 0, factor, 0, 0]})).unwrap();
|
||||
}
|
||||
s.execute("select.set", &json!({"ids": [good.0, other.0]})).unwrap();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
let paint = s.paint.clone();
|
||||
let sample = s.eyedropper_sample.clone();
|
||||
let history = undo_len(&s);
|
||||
let error = s.execute("appearance.copyFrom", &json!({"source": src.0})).unwrap_err();
|
||||
assert!(error.to_string().contains("transform"), "{error}");
|
||||
assert_eq!(s.doc().unwrap().doc, doc);
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
assert_eq!(s.paint, paint);
|
||||
assert_eq!(s.eyedropper_sample, sample);
|
||||
assert_eq!(undo_len(&s), history);
|
||||
// A paint-only pick does not need a usable text transform.
|
||||
s.execute("appearance.copyFrom", &json!({"source": src.0, "apply": {"character": false, "paragraph": false}})).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// A failed dimensional pick retains highlighted characters and the previously remembered sample.
|
||||
#[test]
|
||||
fn scaled_type_pick_rejection_preserves_the_highlighted_range_and_sample() {
|
||||
let mut s = session();
|
||||
let saved = id_of(s.execute("text.create", &json!({"x": 100, "y": 100, "text": "Saved", "size": 18})).unwrap());
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.execute("appearance.copyFrom", &json!({"source": saved.0})).unwrap();
|
||||
let src = id_of(s.execute("text.create", &json!({"x": 100, "y": 200, "text": "Source", "size": 24, "color": "#00ff00"})).unwrap());
|
||||
s.execute("object.transform", &json!({"matrix": [0, 0, 0, 0, 0, 0]})).unwrap();
|
||||
let dst = id_of(s.execute("text.create", &json!({"x": 300, "y": 100, "text": "Sample", "size": 10})).unwrap());
|
||||
s.select_tool("type", ViewInfo::default()).unwrap();
|
||||
s.set_tool_option("textSelection", &json!({"id": dst.0, "start": 1, "end": 4}));
|
||||
s.select_tool("eyedropper", ViewInfo::default()).unwrap();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
let range = s.tool_options();
|
||||
let paint = s.paint.clone();
|
||||
let sample = s.eyedropper_sample.clone();
|
||||
let history = undo_len(&s);
|
||||
assert!(s.execute("appearance.copyFrom", &json!({"source": src.0})).is_err());
|
||||
assert_eq!(s.doc().unwrap().doc, doc);
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
assert_eq!(s.tool_options(), range);
|
||||
assert_eq!(s.paint, paint);
|
||||
assert_eq!(s.eyedropper_sample, sample);
|
||||
assert_eq!(undo_len(&s), history);
|
||||
// Alt-click can still apply the last successful sample after the rejected pick.
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.execute("appearance.copyFrom", &json!({"source": dst.0, "reverse": true})).unwrap();
|
||||
assert_eq!(sampled_type(&s, dst).first_style().size, 18.0);
|
||||
}
|
||||
|
||||
/// A pick that would extend text beyond the canvas keeps the previous sample and defaults.
|
||||
#[test]
|
||||
fn scaled_type_pick_canvas_rejection_preserves_sample_and_defaults() {
|
||||
for mode in ["selection", "ids", "reverse", "range"] {
|
||||
let mut s = session();
|
||||
let saved = id_of(s.execute("text.create", &json!({"x": 40, "y": 40, "text": "Saved", "size": 18, "color": "#0000ff"})).unwrap());
|
||||
let src = id_of(s.execute("text.create", &json!({"x": 100, "y": 100, "text": "Source", "size": 96, "color": "#ff0000"})).unwrap());
|
||||
let dst = id_of(s.execute("text.create", &json!({"x": MAX_COORD - 100.0, "y": 200, "text": "WWWW", "size": 10})).unwrap());
|
||||
s.execute("appearance.copyFrom", &json!({"source": saved.0, "ids": []})).unwrap();
|
||||
let pick = match mode {
|
||||
"ids" | "reverse" => {
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
if mode == "ids" { json!({"source": src.0, "ids": [dst.0]}) } else { json!({"source": dst.0, "ids": [src.0], "reverse": true}) }
|
||||
}
|
||||
"range" => {
|
||||
s.select_tool("type", ViewInfo::default()).unwrap();
|
||||
s.set_tool_option("textSelection", &json!({"id": dst.0, "start": 0, "end": 3}));
|
||||
s.select_tool("eyedropper", ViewInfo::default()).unwrap();
|
||||
json!({"source": src.0})
|
||||
}
|
||||
_ => json!({"source": src.0}),
|
||||
};
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
let range = s.tool_options();
|
||||
let paint = s.paint.clone();
|
||||
let sample = s.eyedropper_sample.clone();
|
||||
let history = undo_len(&s);
|
||||
|
||||
let error = s.execute("appearance.copyFrom", &pick).unwrap_err();
|
||||
assert!(error.to_string().contains("coordinates out of range"), "{mode}: {error}");
|
||||
assert_eq!(s.doc().unwrap().doc, doc, "{mode}");
|
||||
assert_eq!(s.doc().unwrap().selection, selection, "{mode}");
|
||||
assert_eq!(s.tool_options(), range, "{mode}");
|
||||
assert_eq!(s.paint, paint, "{mode}");
|
||||
assert_eq!(s.eyedropper_sample, sample, "{mode}");
|
||||
assert_eq!(undo_len(&s), history, "{mode}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Changing justification can move explicitly targeted point text outside the canvas even
|
||||
/// when character copying is disabled.
|
||||
#[test]
|
||||
fn paragraph_only_pick_rejects_out_of_bounds_explicit_target() {
|
||||
let mut s = session();
|
||||
let saved = id_of(s.execute("text.create", &json!({"x": 40, "y": 40, "text": "Saved", "color": "#0000ff"})).unwrap());
|
||||
let src = id_of(s.execute("text.create", &json!({"x": 100, "y": 100, "text": "Source", "color": "#ff0000"})).unwrap());
|
||||
s.execute("text.setStyle", &json!({"justify": "left"})).unwrap();
|
||||
let dst = id_of(s.execute("text.create", &json!({"x": 100, "y": 200, "text": "WWWW", "size": 16})).unwrap());
|
||||
s.execute("text.setStyle", &json!({"justify": "right"})).unwrap();
|
||||
s.execute("object.transform", &json!({"matrix": [1, 0, 0, 1, MAX_COORD - 101.0, 0]})).unwrap();
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.execute("appearance.copyFrom", &json!({"source": saved.0, "ids": []})).unwrap();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
let paint = s.paint.clone();
|
||||
let sample = s.eyedropper_sample.clone();
|
||||
let history = undo_len(&s);
|
||||
|
||||
let pick = json!({"source": src.0, "ids": [dst.0], "apply": {"character": false}});
|
||||
let error = s.execute("appearance.copyFrom", &pick).unwrap_err();
|
||||
assert!(error.to_string().contains("coordinates out of range"), "{error}");
|
||||
assert_eq!(s.doc().unwrap().doc, doc);
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
assert_eq!(s.paint, paint);
|
||||
assert_eq!(s.eyedropper_sample, sample);
|
||||
assert_eq!(undo_len(&s), history);
|
||||
}
|
||||
|
||||
/// Disabled character copying and append preserve typography; reverse and cached picks convert it.
|
||||
#[test]
|
||||
fn scaled_type_pick_respects_options_and_reverse_sampling() {
|
||||
let mut s = session();
|
||||
let src = id_of(s.execute("text.create", &json!({"x": 100, "y": 100, "text": "Source", "size": 12})).unwrap());
|
||||
s.execute("object.scale", &json!({"sx": 300, "sy": 200})).unwrap();
|
||||
let source = sampled_type(&s, src);
|
||||
let expected = source.effective_char_style(&source.first_style()).unwrap();
|
||||
let dst = id_of(s.execute("text.create", &json!({"x": 300, "y": 100, "text": "Target", "size": 20})).unwrap());
|
||||
s.execute("object.scale", &json!({"sx": 50, "sy": 150})).unwrap();
|
||||
let before = sampled_type(&s, dst);
|
||||
for options in [json!({"pickUp": {"character": false}}), json!({"apply": {"character": false}}), json!({"append": true})] {
|
||||
let mut p = json!({"source": src.0, "ids": [dst.0]});
|
||||
p.as_object_mut().unwrap().extend(options.as_object().unwrap().clone());
|
||||
s.execute("appearance.copyFrom", &p).unwrap();
|
||||
assert_sampled_dimensions(&sampled_type(&s, dst).first_style(), &before.first_style());
|
||||
}
|
||||
s.execute("select.set", &json!({"ids": [src.0]})).unwrap();
|
||||
s.execute("appearance.copyFrom", &json!({"source": dst.0, "reverse": true})).unwrap();
|
||||
let after = sampled_type(&s, dst);
|
||||
assert_sampled_dimensions(&after.effective_char_style(&after.first_style()).unwrap(), &expected);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.execute("appearance.copyFrom", &json!({"source": src.0})).unwrap();
|
||||
s.execute("appearance.copyFrom", &json!({"source": dst.0, "reverse": true})).unwrap();
|
||||
let cached = sampled_type(&s, dst);
|
||||
assert_sampled_dimensions(&cached.effective_char_style(&cached.first_style()).unwrap(), &expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alt_click_applies_the_selection_to_the_clicked_object() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -132,6 +132,44 @@ fn graphic_styles_keep_gradients_relative_to_the_bounds() {
|
||||
assert!(close(fill_geom(&s, dst).unwrap().start, Point::new(0.2, 0.5)), "legacy styles are applied as stored");
|
||||
}
|
||||
|
||||
/// Applying a style with a placed gradient to objects of different sizes gives each its own
|
||||
/// appearance with the gradient on its own bounds, through undo and redo; a style from an older
|
||||
/// file applies the same stored gradient to each, as one shared appearance.
|
||||
#[test]
|
||||
fn a_style_places_its_gradient_on_each_object() {
|
||||
let (mut s, src, dst) = source_and_target();
|
||||
let other = rect(&mut s, 100.0, 300.0, 50.0, 200.0);
|
||||
s.execute("select.set", &json!({"ids": [src.0]})).unwrap();
|
||||
s.execute("graphicStyle.new", &json!({"name": "Glow"})).unwrap();
|
||||
s.execute("graphicStyle.apply", &json!({"name": "Glow", "ids": [dst.0, other.0]})).unwrap();
|
||||
let placed = |s: &Session| {
|
||||
let (g, h) = (fill_geom(s, dst).unwrap(), fill_geom(s, other).unwrap());
|
||||
close(g.start, Point::new(540.0, 60.0))
|
||||
&& close(g.end, Point::new(660.0, 60.0))
|
||||
&& close(h.start, Point::new(110.0, 400.0))
|
||||
&& close(h.end, Point::new(140.0, 400.0))
|
||||
};
|
||||
let shared = |s: &Session| {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
vectorcraft_doc::Shared::ptr_eq(&d.node(dst).unwrap().appearance, &d.node(other).unwrap().appearance)
|
||||
};
|
||||
assert!(placed(&s) && !shared(&s));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert!(placed(&s) && !shared(&s));
|
||||
|
||||
let glow = s.doc().unwrap().doc.graphic_styles.iter().find(|g| g.name == "Glow").cloned().unwrap();
|
||||
let legacy = vectorcraft_doc::GraphicStyle { name: "Old".into(), id: 0, unit_box: false, ..glow };
|
||||
s.edit("Add", |d, _| {
|
||||
d.graphic_styles.push(legacy);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("graphicStyle.apply", &json!({"name": "Old", "ids": [dst.0, other.0]})).unwrap();
|
||||
assert!(shared(&s));
|
||||
assert!(close(fill_geom(&s, other).unwrap().start, Point::new(0.2, 0.5)), "legacy styles are applied as stored");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eyedropper_options_choose_what_copy_from_takes() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -296,6 +296,59 @@ fn link_info_stays_within_the_asking_callers_roots() {
|
||||
assert_ne!(i["status"], "ok", "{i}");
|
||||
}
|
||||
|
||||
/// A confined caller's `links.list`, `links.check` and `links.info` replies, and its
|
||||
/// `document.open` reply, leave out the path and folder of a linked file outside its read roots
|
||||
/// and keep the row with the file's name. A file found by name inside the roots shows its path.
|
||||
#[test]
|
||||
fn link_paths_outside_the_read_roots_are_left_out() {
|
||||
let away = Folder::new("lrr-away");
|
||||
let home = Folder::new("lrr-home");
|
||||
let pic = away.file("photo.png");
|
||||
let bytes = png(60, 30, RED);
|
||||
write(&pic, &bytes);
|
||||
let mut s = session();
|
||||
let id = place(&mut s, &pic);
|
||||
let roots = crate::file_access::AutomationRoots::new(&[&home.0], &[]).unwrap().unwrap();
|
||||
let ask = |s: &mut Session, cmd: &str, p: Value| crate::file_access::confine(Some(&roots), || run(s, cmd, p));
|
||||
let hidden = |r: &Value| assert!(!r.to_string().contains("lrr-away"), "{r}");
|
||||
let list = ask(&mut s, "links.list", json!({}));
|
||||
hidden(&list);
|
||||
let row = &list["links"][0];
|
||||
assert_eq!((row["id"].as_u64(), row["name"].as_str(), row["status"].as_str()), (Some(id.0), Some("photo.png"), Some("missing")), "{list}");
|
||||
let check = ask(&mut s, "links.check", json!({}));
|
||||
hidden(&check);
|
||||
assert_eq!((check["links"][0]["name"].as_str(), check["missing"].as_u64()), (Some("photo.png"), Some(1)), "{check}");
|
||||
let info = ask(&mut s, "links.info", json!({"id": id.0}));
|
||||
hidden(&info);
|
||||
assert_eq!(
|
||||
(info["fileName"].as_str(), info["status"].as_str(), info["linked"].as_bool()),
|
||||
(Some("photo.png"), Some("missing"), Some(true)),
|
||||
"{info}"
|
||||
);
|
||||
for k in ["path", "link", "location", "fileSize", "modified", "created"] {
|
||||
assert!(info.get(k).is_none(), "{k}: {info}");
|
||||
}
|
||||
// Unconfined, the same replies carry the paths.
|
||||
assert_eq!(run(&mut s, "links.list", json!({}))["links"][0]["path"], json!(pic));
|
||||
let i = run(&mut s, "links.info", json!({"id": id.0}));
|
||||
assert_eq!((&i["path"], &i["link"], i["fileSize"].as_u64()), (&json!(pic), &json!(pic), Some(bytes.len() as u64)), "{i}");
|
||||
// Saved inside the roots next to a copy of the file: the copy is found by name and shown.
|
||||
save(&mut s, &home.file("poster.vectorcraft"));
|
||||
let copy = home.file("photo.png");
|
||||
write(©, &bytes);
|
||||
let check = ask(&mut s, "links.check", json!({}));
|
||||
hidden(&check);
|
||||
assert_eq!((check["links"][0]["status"].as_str(), &check["links"][0]["found"]), (Some("ok"), &json!(copy)), "{check}");
|
||||
let list = ask(&mut s, "links.list", json!({}));
|
||||
hidden(&list);
|
||||
assert_eq!(list["links"][0]["found"], json!(copy), "{list}");
|
||||
// Opened again without the copy: the missing link's row has no path.
|
||||
std::fs::remove_file(©).unwrap();
|
||||
let opened = ask(&mut Session::new(), "document.open", json!({"path": home.file("poster.vectorcraft")}));
|
||||
hidden(&opened);
|
||||
assert_eq!(opened["missingLinks"][0]["name"], "photo.png", "{opened}");
|
||||
}
|
||||
|
||||
/// A 600×300 pt image at the centre of the artboard, its placement options set to `options`,
|
||||
/// relinked to a 300×100 px file → the image's id and session.
|
||||
fn relinked(name: &str, options: Value) -> (Session, NodeId, Folder) {
|
||||
|
||||
@@ -73,14 +73,13 @@ fn arrowheads_are_part_of_the_outline() {
|
||||
assert_eq!(path_of(&n).subpaths.len(), 1, "line and head are one shape");
|
||||
}
|
||||
|
||||
/// Outline Stroke of a Lens-profile line covers 7/12 of the full-width rectangle.
|
||||
/// Outline Stroke of a Lens-profile line covers 2/3 of the full-width rectangle.
|
||||
#[test]
|
||||
fn a_lens_profile_outlines_to_seven_twelfths_of_the_rectangle() {
|
||||
fn a_lens_profile_outlines_to_two_thirds_of_the_rectangle() {
|
||||
let mut s = session();
|
||||
line(&mut s, json!({"weight": 10, "profile": "lens"}));
|
||||
let n = outline(&mut s);
|
||||
// Each half of the lens swells on a cubic, flat at the middle (#5714).
|
||||
assert!(near(area(&n), 7.0 / 12.0 * 10.0 * 100.0, 2.0), "{}", area(&n));
|
||||
assert!(near(area(&n), 2.0 / 3.0 * 10.0 * 100.0, 2.0), "{}", area(&n));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1060,6 +1060,30 @@ fn link_info_gives_the_artboards_size_not_pixels() {
|
||||
);
|
||||
}
|
||||
|
||||
/// A confined caller's Link Info of a placed document whose file is outside its read roots has no
|
||||
/// `link`, `path` or `location`, and keeps the file's name.
|
||||
#[test]
|
||||
fn link_info_leaves_out_a_placed_documents_path_outside_the_read_roots() {
|
||||
let away = Folder::new("placed-lrr-away");
|
||||
let home = Folder::new("placed-lrr-home");
|
||||
let src = away.file("logo.vectorcraft");
|
||||
source(&src, 100.0, 50.0, RED, 0);
|
||||
let mut s = session();
|
||||
let id = place(&mut s, &src);
|
||||
let roots = crate::file_access::AutomationRoots::new(&[&home.0], &[]).unwrap().unwrap();
|
||||
let i = crate::file_access::confine(Some(&roots), || s.execute("links.info", &json!({"id": id.0}))).unwrap();
|
||||
assert!(!i.to_string().contains("placed-lrr-away"), "{i}");
|
||||
assert_eq!(
|
||||
(i["fileName"].as_str(), i["document"].as_bool(), i["status"].as_str()),
|
||||
(Some("logo.vectorcraft"), Some(true), Some("missing")),
|
||||
"{i}"
|
||||
);
|
||||
let l = crate::file_access::confine(Some(&roots), || s.execute("links.list", &json!({}))).unwrap();
|
||||
assert!(!l.to_string().contains("placed-lrr-away") && l["links"][0]["name"] == "logo.vectorcraft", "{l}");
|
||||
let i = s.execute("links.info", &json!({"id": id.0})).unwrap();
|
||||
assert_eq!((&i["link"], &i["path"]), (&json!(src), &json!(src)), "unconfined: {i}");
|
||||
}
|
||||
|
||||
/// Save As reads its defaults from the catalog; using them must retain current editable links.
|
||||
#[test]
|
||||
fn native_save_catalog_defaults_keep_placed_documents_editable() {
|
||||
|
||||
@@ -2297,6 +2297,194 @@ fn a_failed_store_write_keeps_the_stored_module_with_its_grant() {
|
||||
vectorcraft_plugins::registry::revoke(id);
|
||||
}
|
||||
|
||||
/// Runs the test `name` of this module again in a process of its own → whether this is that
|
||||
/// process. The list of disabled plug-ins is kept for the whole process, and the other tests here
|
||||
/// read it again each time they attach a plug-in store.
|
||||
fn in_own_process(name: &str) -> bool {
|
||||
const CHILD: &str = "VECTORCRAFT_TEST_PLUGIN_DISABLED_LIST";
|
||||
if std::env::var_os(CHILD).is_some() {
|
||||
return true;
|
||||
}
|
||||
let output = std::process::Command::new(std::env::current_exe().unwrap())
|
||||
.args(["--exact", &format!("tests::plugins::{name}"), "--nocapture"])
|
||||
.env(CHILD, "1")
|
||||
.output()
|
||||
.unwrap();
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(
|
||||
output.status.success() && stdout.contains("test result: ok. 1 passed; 0 failed;"),
|
||||
"{name} failed in its own process:\n{stdout}\n{}",
|
||||
String::from_utf8_lossy(&output.stderr),
|
||||
);
|
||||
false
|
||||
}
|
||||
|
||||
/// An update whose grant can't be written, and whose old module can't be put back either, stays in
|
||||
/// the store: a disabled plug-in is turned on, as for any update the store holds. The old grant
|
||||
/// doesn't cover the new module, so the next start skips it until it is approved.
|
||||
#[test]
|
||||
fn an_update_left_in_the_store_by_a_failed_put_back_turns_a_disabled_plugin_on() {
|
||||
if !in_own_process("an_update_left_in_the_store_by_a_failed_put_back_turns_a_disabled_plugin_on") {
|
||||
return;
|
||||
}
|
||||
use vectorcraft_plugins::{permissions::sha256_hex, registry, safe_mode};
|
||||
let folder = crate::tests_links::Folder::new("plugin-store-failed-put-back-update");
|
||||
let id = "test.engine.permissions.putbackupdate";
|
||||
let first = asking(id, r#"["doc.read"]"#, &json!({"objects": []}));
|
||||
let changed = asking(id, r#"["doc.read","doc.write"]"#, &json!({"objects": []}));
|
||||
let mut s = session();
|
||||
s.attach_plugin_store(folder.0.clone());
|
||||
run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&first), "grant": ["doc.read"]}));
|
||||
run(&mut s, "plugin.setEnabled", json!({"id": id, "enabled": false}));
|
||||
let grants = folder.0.join("grants.json");
|
||||
let recorded = std::fs::read(&grants).unwrap();
|
||||
std::fs::remove_file(&grants).unwrap();
|
||||
std::fs::create_dir_all(grants.join("block")).unwrap();
|
||||
|
||||
crate::store::PUT_BACK_FAILS.set(true);
|
||||
let update = json!({"dataBase64": vectorcraft_format::base64_encode(&changed), "grant": ["doc.read", "doc.write"], "replace": true});
|
||||
let installed = s.execute("plugin.install", &update);
|
||||
crate::store::PUT_BACK_FAILS.set(false);
|
||||
assert_eq!(installed.unwrap()["hash"], sha256_hex(&changed), "the new module runs in this session");
|
||||
assert_eq!(registry::get(id).map(|p| p.hash().to_string()), Some(sha256_hex(&changed)));
|
||||
assert_eq!(std::fs::read(s.stored_plugin_file(id).unwrap()).unwrap(), changed, "the store holds the new module");
|
||||
assert!(safe_mode::disabled().is_empty(), "the module the store holds is turned on");
|
||||
let saved = std::fs::read(folder.0.join(crate::store::DISABLED_FILE)).unwrap();
|
||||
assert!(safe_mode::parse_disabled_file(&saved).is_empty(), "nothing disabled is saved");
|
||||
|
||||
std::fs::remove_dir_all(&grants).unwrap();
|
||||
std::fs::write(&grants, &recorded).unwrap();
|
||||
registry::remove(id);
|
||||
registry::revoke(id);
|
||||
let mut next = session();
|
||||
next.attach_plugin_store(folder.0.clone());
|
||||
assert!(registry::get(id).is_none(), "the old grant doesn't cover the new module");
|
||||
safe_mode::set_disabled(vec![]);
|
||||
}
|
||||
|
||||
/// An update whose grant can't be written, and whose put-back writes the old module and then
|
||||
/// reports a failure, leaves the old module in the store: a disabled plug-in stays disabled, and the
|
||||
/// next start doesn't load it.
|
||||
#[test]
|
||||
fn an_update_put_back_that_fails_after_writing_keeps_a_disabled_plugin_off() {
|
||||
if !in_own_process("an_update_put_back_that_fails_after_writing_keeps_a_disabled_plugin_off") {
|
||||
return;
|
||||
}
|
||||
use vectorcraft_plugins::{registry, safe_mode};
|
||||
let folder = crate::tests_links::Folder::new("plugin-store-put-back-fails-after-write");
|
||||
let id = "test.engine.permissions.putbacklate";
|
||||
let first = asking(id, r#"["doc.read"]"#, &json!({"objects": []}));
|
||||
let changed = asking(id, r#"["doc.read","doc.write"]"#, &json!({"objects": []}));
|
||||
let mut s = session();
|
||||
s.attach_plugin_store(folder.0.clone());
|
||||
run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&first), "grant": ["doc.read"]}));
|
||||
run(&mut s, "plugin.setEnabled", json!({"id": id, "enabled": false}));
|
||||
let disabled = safe_mode::disabled();
|
||||
assert!(!disabled.is_empty());
|
||||
let grants = folder.0.join("grants.json");
|
||||
let recorded = std::fs::read(&grants).unwrap();
|
||||
std::fs::remove_file(&grants).unwrap();
|
||||
std::fs::create_dir_all(grants.join("block")).unwrap();
|
||||
|
||||
crate::store::PUT_BACK_FAILS_AFTER_WRITE.set(true);
|
||||
let update = json!({"dataBase64": vectorcraft_format::base64_encode(&changed), "grant": ["doc.read", "doc.write"], "replace": true});
|
||||
let installed = s.execute("plugin.install", &update);
|
||||
crate::store::PUT_BACK_FAILS_AFTER_WRITE.set(false);
|
||||
installed.unwrap();
|
||||
assert_eq!(std::fs::read(s.stored_plugin_file(id).unwrap()).unwrap(), first, "the store holds the old module");
|
||||
assert_eq!(safe_mode::disabled(), disabled, "the plug-in stays disabled");
|
||||
let saved = std::fs::read(folder.0.join(crate::store::DISABLED_FILE)).unwrap();
|
||||
assert!(!safe_mode::parse_disabled_file(&saved).is_empty(), "the disabled list on disk is kept");
|
||||
|
||||
std::fs::remove_dir_all(&grants).unwrap();
|
||||
std::fs::write(&grants, &recorded).unwrap();
|
||||
registry::remove(id);
|
||||
registry::revoke(id);
|
||||
let mut next = session();
|
||||
next.attach_plugin_store(folder.0.clone());
|
||||
assert!(registry::get(id).is_none(), "the disabled old module doesn't load");
|
||||
safe_mode::set_disabled(vec![]);
|
||||
}
|
||||
|
||||
/// A module without permissions whose install can't remove the old grant, and whose put-back writes
|
||||
/// the old module and then reports a failure, leaves the old module in the store: a disabled plug-in
|
||||
/// stays disabled.
|
||||
#[test]
|
||||
fn a_plain_update_put_back_that_fails_after_writing_keeps_a_disabled_plugin_off() {
|
||||
if !in_own_process("a_plain_update_put_back_that_fails_after_writing_keeps_a_disabled_plugin_off") {
|
||||
return;
|
||||
}
|
||||
use vectorcraft_plugins::safe_mode;
|
||||
let folder = crate::tests_links::Folder::new("plugin-store-plain-put-back-fails-after-write");
|
||||
let id = "test.engine.permissions.plainputbacklate";
|
||||
let empty = json!({"objects": []});
|
||||
let approved = asking(id, r#"["doc.read"]"#, &empty);
|
||||
let plain = asking(id, "null", &empty);
|
||||
let grants = folder.0.join("grants.json");
|
||||
let mut s = session();
|
||||
s.attach_plugin_store(folder.0.clone());
|
||||
run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&approved), "grant": ["doc.read"]}));
|
||||
run(&mut s, "plugin.setEnabled", json!({"id": id, "enabled": false}));
|
||||
let disabled = safe_mode::disabled();
|
||||
assert!(!disabled.is_empty());
|
||||
let file = s.stored_plugin_file(id).unwrap();
|
||||
// `1e9` is written back as `1000000000.0`, which makes the rewritten file larger than the 1 MiB that is read.
|
||||
let entry = serde_json::from_slice::<Vec<Value>>(&std::fs::read(&grants).unwrap()).unwrap().remove(0);
|
||||
let full = format!(r#"[{entry},{{"n":[{}]}}]"#, vec!["1e9"; 200_000].join(",")).into_bytes();
|
||||
std::fs::write(&grants, &full).unwrap();
|
||||
|
||||
crate::store::PUT_BACK_FAILS_AFTER_WRITE.set(true);
|
||||
let installed = s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&plain), "replace": true}));
|
||||
crate::store::PUT_BACK_FAILS_AFTER_WRITE.set(false);
|
||||
installed.unwrap();
|
||||
assert_eq!(std::fs::read(&file).unwrap(), approved, "the store holds the old module");
|
||||
assert_eq!(safe_mode::disabled(), disabled, "the plug-in stays disabled");
|
||||
let saved = std::fs::read(folder.0.join(crate::store::DISABLED_FILE)).unwrap();
|
||||
assert!(!safe_mode::parse_disabled_file(&saved).is_empty(), "the disabled list on disk is kept");
|
||||
|
||||
std::fs::remove_file(&grants).unwrap();
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
safe_mode::set_disabled(vec![]);
|
||||
}
|
||||
|
||||
/// A first install whose grant can't be written, and whose module can't be taken out of the store
|
||||
/// again, stays in the store: the plug-in's file is turned on. No grant covers the module, so the
|
||||
/// next start skips it until it is approved.
|
||||
#[test]
|
||||
fn a_first_install_left_in_the_store_by_a_failed_put_back_turns_its_file_on() {
|
||||
if !in_own_process("a_first_install_left_in_the_store_by_a_failed_put_back_turns_its_file_on") {
|
||||
return;
|
||||
}
|
||||
use vectorcraft_plugins::{registry, safe_mode};
|
||||
let folder = crate::tests_links::Folder::new("plugin-store-failed-put-back-first");
|
||||
let id = "test.engine.permissions.putbackfirst";
|
||||
let first = asking(id, r#"["doc.read"]"#, &json!({"objects": []}));
|
||||
let mut s = session();
|
||||
s.attach_plugin_store(folder.0.clone());
|
||||
let module = s.stored_plugin_file(id).unwrap();
|
||||
run(&mut s, "plugin.setEnabled", json!({"file": module, "enabled": false}));
|
||||
let grants = folder.0.join("grants.json");
|
||||
std::fs::create_dir_all(grants.join("block")).unwrap();
|
||||
|
||||
crate::store::PUT_BACK_FAILS.set(true);
|
||||
let installed = s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&first), "grant": ["doc.read"]}));
|
||||
crate::store::PUT_BACK_FAILS.set(false);
|
||||
installed.unwrap();
|
||||
assert!(registry::get(id).is_some(), "the module runs in this session");
|
||||
assert_eq!(std::fs::read(&module).unwrap(), first, "the store holds the module");
|
||||
assert!(safe_mode::disabled().is_empty(), "the file the store holds is turned on");
|
||||
let saved = std::fs::read(folder.0.join(crate::store::DISABLED_FILE)).unwrap();
|
||||
assert!(safe_mode::parse_disabled_file(&saved).is_empty(), "nothing disabled is saved");
|
||||
|
||||
std::fs::remove_dir_all(&grants).unwrap();
|
||||
registry::remove(id);
|
||||
registry::revoke(id);
|
||||
let mut next = session();
|
||||
next.attach_plugin_store(folder.0.clone());
|
||||
assert!(registry::get(id).is_none(), "no grant covers the module");
|
||||
safe_mode::set_disabled(vec![]);
|
||||
}
|
||||
|
||||
/// A plug-in folder reports a changed module as needing approval and still loads the other files;
|
||||
/// granted on its own, the changed module loads from the folder. A session without a store uses the
|
||||
/// grants held by the process.
|
||||
@@ -3784,3 +3972,60 @@ fn a_filter_that_calls_commands_is_given_the_edit_in_progress() {
|
||||
assert_eq!(x, json!(17.0), "undoing the filter brings back the moved rectangle");
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
}
|
||||
|
||||
/// Installs a filter `id` that calls commands and hands back what it is given, with `commands` (a
|
||||
/// JSON list of command specs) in its manifest.
|
||||
fn install_echo_calling(s: &mut Session, id: &str, commands: &str) {
|
||||
let grant = json!(["doc.read", "doc.write", "commands"]);
|
||||
let manifest = format!(
|
||||
r#"{{"id":"{id}","name":"Test {id}","kind":"filter","api":2,"permissions":{grant},"commandAllowlist":["select.all"],"commands":{commands}}}"#
|
||||
);
|
||||
let wasm = wat::parse_str(tpl::module(&manifest, tpl::ECHO, "", 1)).unwrap();
|
||||
run(s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&wasm), "grant": grant, "replace": true}));
|
||||
}
|
||||
|
||||
/// With Real-time Drawing off, a 50 × 50 rectangle at (10, 10) whose move by 7 pt is still being
|
||||
/// dragged: the tool has stored the move, which the document does not hold yet.
|
||||
fn deferred_move(s: &mut Session) -> NodeId {
|
||||
let r = NodeId(run(s, "shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50}))["id"].as_u64().unwrap());
|
||||
run(s, "prefs.set", json!({"key": "realTimeDrawing", "value": false}));
|
||||
let move_by = vectorcraft_tools::Action::Preview("object.move".into(), json!({"dx": 7, "dy": 0}));
|
||||
s.apply_actions(vec![vectorcraft_tools::Action::Begin("Move".into()), move_by]).unwrap();
|
||||
assert_eq!(node(s, r).geometric_bounds().unwrap().x0, 10.0, "the move is not applied while it is dragged");
|
||||
r
|
||||
}
|
||||
|
||||
/// The rectangle a run named `label` during [`deferred_move`] left: moved, with the interaction
|
||||
/// ended and the move an undo step of its own before the run's.
|
||||
fn assert_moved_once(s: &Session, r: NodeId, label: &str) {
|
||||
assert!(!s.in_interaction());
|
||||
assert_eq!(node(s, r).geometric_bounds().unwrap().x0, 17.0, "the filter was given the moved rectangle");
|
||||
let labels: Vec<&str> = s.doc().unwrap().history.undo.iter().rev().take(2).map(|e| e.label.as_str()).collect();
|
||||
assert_eq!(labels, [label, "Move"]);
|
||||
}
|
||||
|
||||
/// With Real-time Drawing off, `plugin.run` of a filter that calls commands during a drag applies
|
||||
/// the dragged move first and gives the filter the moved rectangle, which it hands back as it was.
|
||||
#[test]
|
||||
fn a_filter_that_calls_commands_is_given_a_deferred_move() {
|
||||
let id = "test.engine.callsdeferred";
|
||||
let mut s = session();
|
||||
install_echo_calling(&mut s, id, "[]");
|
||||
let r = deferred_move(&mut s);
|
||||
run(&mut s, "plugin.run", json!({"id": id}));
|
||||
assert_moved_once(&s, r, &format!("Test {id}"));
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
}
|
||||
|
||||
/// A plug-in command that calls commands, run with Real-time Drawing off during a drag, is given
|
||||
/// the moved rectangle as `plugin.run` is.
|
||||
#[test]
|
||||
fn a_plugin_command_that_calls_commands_is_given_a_deferred_move() {
|
||||
let id = "test.engine.callsdeferredcmd";
|
||||
let mut s = session();
|
||||
install_echo_calling(&mut s, id, r#"[{"id":"same","label":"Same"}]"#);
|
||||
let r = deferred_move(&mut s);
|
||||
run(&mut s, &format!("plugin.{id}.same"), json!({}));
|
||||
assert_moved_once(&s, r, "Same");
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
}
|
||||
@@ -2047,6 +2047,42 @@ fn real_time_drawing_turned_on_mid_drag_still_commits_deferred_preview() {
|
||||
assert_eq!(paths(&s), 1, "the deferred rectangle is committed");
|
||||
}
|
||||
|
||||
/// With Real-time Drawing off, Undo during a drag keeps the dragged move as its own step and takes
|
||||
/// back only that: the rectangle drawn before stays, and Redo brings the move back.
|
||||
#[test]
|
||||
fn undo_during_a_deferred_drag_takes_back_only_the_drag() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
|
||||
let r = NodeId(s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap()["id"].as_u64().unwrap());
|
||||
set_pref(&mut s, "realTimeDrawing", json!(false));
|
||||
let x = |s: &Session| s.doc().unwrap().doc.node(r).and_then(vectorcraft_doc::Node::geometric_bounds).map(|b| b.x0);
|
||||
let move_by = vectorcraft_tools::Action::Preview("object.move".into(), json!({"dx": 7, "dy": 0}));
|
||||
s.apply_actions(vec![vectorcraft_tools::Action::Begin("Move".into()), move_by]).unwrap();
|
||||
assert_eq!(x(&s), Some(10.0));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(!s.in_interaction());
|
||||
assert_eq!(x(&s), Some(10.0), "the rectangle stays");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(x(&s), Some(17.0), "Redo brings the move back");
|
||||
}
|
||||
|
||||
/// Undo during a drag whose stored preview fails cancels the drag, as its release would, and then
|
||||
/// takes back the step before it.
|
||||
#[test]
|
||||
fn undo_during_a_deferred_drag_that_fails_cancels_it() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
|
||||
set_pref(&mut s, "realTimeDrawing", json!(false));
|
||||
let bad = vectorcraft_tools::Action::Preview("no.such.command".into(), json!({}));
|
||||
s.apply_actions(vec![vectorcraft_tools::Action::Begin("Bad".into()), bad]).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(!s.in_interaction());
|
||||
let d = s.doc().unwrap();
|
||||
assert_eq!(d.doc.layers.iter().map(|l| l.children().map_or(0, |c| c.len())).sum::<usize>(), 0, "the rectangle is undone");
|
||||
assert!(d.history.undo.iter().all(|e| e.label != "Bad"));
|
||||
}
|
||||
|
||||
/// Repeated overlay redraws with Real-time Drawing off reuse the cached deferred feedback.
|
||||
#[test]
|
||||
fn deferred_preview_overlays_are_cached_across_redraws() {
|
||||
|
||||
@@ -55,6 +55,42 @@ fn file_constructor_identity_and_folder_path() {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// `exists` is true only when the path names an entry of the object's own kind.
|
||||
#[test]
|
||||
fn script_file_exists_checks_the_kind() {
|
||||
let base = folder("kind");
|
||||
write(&base.join("main/data.txt"), b"x");
|
||||
std::fs::create_dir_all(base.join("main/sub")).unwrap();
|
||||
let log =
|
||||
logged(&base, "vc.log([new File('sub').exists, new Folder('data.txt').exists, new File('data.txt').exists, new Folder('sub').exists]);");
|
||||
assert_eq!(log, json!([[false, false, true, true]]));
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A property a script puts on `Object.prototype` is not read as part of what the app returns.
|
||||
#[test]
|
||||
fn script_file_ignores_inherited_result_fields() {
|
||||
let base = folder("inherited");
|
||||
write(&base.join("main/data.txt"), b"data");
|
||||
let log = logged(
|
||||
&base,
|
||||
"Object.prototype.error = 'inherited'; Object.prototype.exists = true;\n\
|
||||
var f = new File('data.txt'); var opened = f.open('r');\n\
|
||||
vc.log([f.exists, new File('nope').exists, opened, f.error, f.read()]);",
|
||||
);
|
||||
assert_eq!(log, json!([[true, false, true, "", "data"]]));
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// `File.encode`, `File.decode`, `File.isEncodingAvailable` and the `Folder` locations are counted as
|
||||
/// calls to the app.
|
||||
#[test]
|
||||
fn script_file_class_functions_are_counted() {
|
||||
let source = "File.encode('x'); File.decode('y'); Folder.isEncodingAvailable('UTF-8'); Folder.temp;";
|
||||
let r = Session::new().execute("script.run", &json!({"source": source})).unwrap();
|
||||
assert_eq!(r["hostCallCounts"]["fileCalls"], 4, "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_names_are_uri_encoded_and_current_relative() {
|
||||
let base = folder("names");
|
||||
@@ -74,7 +110,7 @@ fn file_names_are_uri_encoded_and_current_relative() {
|
||||
assert_eq!(
|
||||
log,
|
||||
json!([
|
||||
["caf%C3%A9%20x.txt", "caf\u{e9} x.txt", "caf%C3%A9%20x.txt", true, "%C3%A9%20%2F", "\u{e9}", "sub/y.txt", true, true],
|
||||
["caf%C3%A9%20x.txt", "caf\u{e9} x.txt", "caf%C3%A9%20x.txt", true, "%C3%A9%20/", "\u{e9}", "sub/y.txt", true, true],
|
||||
["sub", "w", "w"],
|
||||
["literal%20name.txt", "literal%2520name.txt", "literal%2520name.txt", true, "p%41", "p%41"]
|
||||
])
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::{BlendMode, Color, Paint};
|
||||
use vectorcraft_doc::{GraphicStyle, Knockout, Node};
|
||||
use vectorcraft_doc::{GraphicStyle, Knockout, Node, Shared};
|
||||
|
||||
use crate::*;
|
||||
|
||||
@@ -202,6 +202,77 @@ fn redefine_leaves_unlinked_and_edited_objects_alone() {
|
||||
assert!(s.execute("graphicStyle.redefine", &json!({"id": c.0})).is_err());
|
||||
}
|
||||
|
||||
/// Applying a style to several objects gives them one shared copy of its appearance; editing one
|
||||
/// object gives that one its own, and the saved file is the same as with a copy for each.
|
||||
#[test]
|
||||
fn applying_a_style_shares_one_appearance() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 10.0);
|
||||
run(&mut s, "appearance.clear", json!({}));
|
||||
run(&mut s, "paint.setFill", json!({"color": "#00ff00"}));
|
||||
run(&mut s, "paint.setStroke", json!({"color": "#0000ff"}));
|
||||
run(&mut s, "effect.apply", json!({"effect": "distort.roughen"}));
|
||||
run(&mut s, "graphicStyle.new", json!({"name": "Rough"}));
|
||||
let rough = style(&s, "Rough");
|
||||
let ids = [rect(&mut s, 100.0), rect(&mut s, 200.0), rect(&mut s, 300.0)];
|
||||
let [b, c, d] = ids;
|
||||
run(&mut s, "graphicStyle.apply", json!({"name": "Rough", "ids": [b.0, c.0, d.0]}));
|
||||
let shared = |s: &Session, x: NodeId, y: NodeId| Shared::ptr_eq(&node(s, x).appearance, &node(s, y).appearance);
|
||||
assert!(shared(&s, b, c) && shared(&s, c, d), "one appearance for every object");
|
||||
for id in ids {
|
||||
assert_eq!(node(&s, id).appearance, rough.appearance);
|
||||
assert_eq!(node(&s, id).extras().graphic_style, Some(rough.id));
|
||||
}
|
||||
// The saved file is the same as one where each object has its own copy.
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let mut copies = (*doc).clone();
|
||||
for id in ids {
|
||||
let n = copies.node_mut(id).unwrap();
|
||||
n.appearance = Shared::new((*n.appearance).clone());
|
||||
}
|
||||
assert!(!Shared::ptr_eq(&copies.node(b).unwrap().appearance, &copies.node(c).unwrap().appearance));
|
||||
for pretty in [false, true] {
|
||||
assert_eq!(vectorcraft_format::save(&doc, pretty), vectorcraft_format::save(&copies, pretty));
|
||||
}
|
||||
|
||||
// Editing one object leaves the others and the style as they were.
|
||||
set_fill(&mut s, b, "#ff0000");
|
||||
assert!(!shared(&s, b, c) && shared(&s, c, d));
|
||||
assert_eq!(fill(&s, b), Paint::solid(Color::from_hex("#ff0000").unwrap()));
|
||||
assert_eq!(node(&s, c).appearance, rough.appearance);
|
||||
assert_eq!(style(&s, "Rough"), rough);
|
||||
assert_eq!(linked(&mut s, "Rough"), vec![a.0, c.0, d.0]);
|
||||
|
||||
// Undo and redo restore the values, still shared.
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert!(shared(&s, b, c) && node(&s, b).appearance == rough.appearance);
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
for id in ids {
|
||||
assert_ne!(node(&s, id).appearance, rough.appearance);
|
||||
assert_eq!(node(&s, id).extras().graphic_style, None);
|
||||
}
|
||||
run(&mut s, "edit.redo", json!({}));
|
||||
assert!(shared(&s, b, c) && shared(&s, c, d) && node(&s, d).appearance == rough.appearance);
|
||||
}
|
||||
|
||||
/// Redefine Graphic Style gives the objects still linked one shared copy of the new appearance.
|
||||
#[test]
|
||||
fn redefine_shares_one_appearance() {
|
||||
let mut s = session();
|
||||
let (a, b, c) = linked_three(&mut s);
|
||||
let d = rect(&mut s, 300.0);
|
||||
run(&mut s, "graphicStyle.apply", json!({"name": "S"}));
|
||||
set_fill(&mut s, d, "#00ff00");
|
||||
set_fill(&mut s, a, "#0000ff");
|
||||
run(&mut s, "graphicStyle.redefine", json!({"name": "S", "id": a.0}));
|
||||
let (nb, nc) = (node(&s, b), node(&s, c));
|
||||
assert!(Shared::ptr_eq(&nb.appearance, &nc.appearance));
|
||||
assert_eq!(nb.appearance, style(&s, "S").appearance);
|
||||
// The edited object keeps its own look.
|
||||
assert_eq!(fill(&s, d), Paint::solid(Color::from_hex("#00ff00").unwrap()));
|
||||
assert!(!Shared::ptr_eq(&node(&s, d).appearance, &nb.appearance));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_a_style_unlinks_and_renaming_keeps_links() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -221,9 +221,9 @@ fn new_width_point_adjust_adjoining_uses_sampled_widths() {
|
||||
let (mut s, id) = line_session();
|
||||
let original = vec![(0.0, 9.0, 9.0), (0.25, 1.0, 0.0), (0.75, 3.0, 0.0), (1.0, 7.0, 7.0)];
|
||||
s.execute("stroke.widthProfile.set", &json!({"ids": [id.0], "points": original})).unwrap();
|
||||
// At t=0.5 the old left side is where the profile's curve is there (#5714) and the right
|
||||
// side is 0pt. Indexed editing of that sampled point is the existing convention: scale the
|
||||
// nonzero side, add from zero.
|
||||
// At t=0.5 the old left side is where the profile's curve is there and the right side is
|
||||
// 0pt. Indexed editing of that sampled point is the existing convention: scale the nonzero
|
||||
// side, add from zero.
|
||||
let sampled = vectorcraft_doc::WidthProfile { points: original }.at(0.5).0;
|
||||
let index = if indexed { Some(set(&mut s, json!({"id": id.0, "t": 0.5, "left": sampled * 5.0, "right": 0}))) } else { None };
|
||||
set(&mut s, json!({"id": id.0, "index": index, "t": 0.5, "left": 20, "right": 5, "adjustAdjoining": true}));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Untrusted files never crash the app: garbage, truncated, mutated and hostile SVG, PDF and DXF input,
|
||||
//! Untrusted files never crash the app: garbage, truncated, mutated and hostile SVG, PDF, DXF and HP-GL input,
|
||||
//! mutated raster images (Photoshop documents and JPEG XL images among them) placed with File → Place, and swatch (`.vcswatches`, `.gpl`, `.ase`), graphic style (`.vcstyles`), flattener preset
|
||||
//! (`.vcflattener`) and export preset (`.vcexportpresets`) libraries, shortcut sets and action sets other apps save as text (`.kys`, `.aia`), and native files (compressed, damaged, saved for older versions),
|
||||
//! must load as an error or as a document that then renders and exports, without a panic; nor may
|
||||
@@ -2935,6 +2935,73 @@ proptest! {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- HP-GL plot files: opened and placed ----------
|
||||
|
||||
/// A plot file with every kind of instruction the reader plays, printer escapes around it.
|
||||
const HPGL_SAMPLE: &[u8] = b"\x1b%-12345X@PJL ENTER LANGUAGE=HPGL2\r\n\x1bE\x1b%0BIN;BP1,\"job\";CO\"sample\";NP16;PC9,10,20,30;CR0,100,0,100,0,100;\
|
||||
SP1;PW0.5;WU0;LA1,4,2,4,3,7;LT2,5,1;PA0,0;PD1000,0,1000,1000;PU;LT;PR;PD-500,0,0,-500;PA2000,2000;CI300,5;AA2000,2500,120;AR0,100,-90;\
|
||||
AT2500,2500,3000,2000;RT100,100,200,0;BZ0,100,100,100,100,0;BR10,10,20,10,30,0;UL3,1,2,1;LT-3,2;EA3000,3000;ER100,-100;FT10,40;RA3500,3500;\
|
||||
RR200,200;WG400,30,120;EW-400,0,360;PM0;PD4000,4000,4500,4000,4500,4500;PM1;PU4100,4100;PD4200,4100,4200,4200;PM2;FP1;EP;FT3,10,45;FP;\
|
||||
IP0,0,10160,7620;SC0,100,0,100,1,20,80;PU10,10;PD20,20;SC0,10,0,10,2;PD5,5;SC;RO90;PD100,100;RO;IR10,10,90,90;IW0,0,5000,5000;\
|
||||
DT*,1;LBlabel*SMx;PA0,0;SM;PE:\xc1<=\xbf\xc0\xc2\xc2;LB\x03PG;\x1b%0A\x1b*r1A\x1b*b4W\x01\x02\x03\x04\x1b*rC\x1b%1BPD1,1;\x1b%-12345X";
|
||||
|
||||
/// The sample the mutations start from opens as a plot with art.
|
||||
#[test]
|
||||
fn the_hpgl_sample_opens() {
|
||||
let l = vectorcraft_engine::cmd::fileio::load("x.plt", HPGL_SAMPLE).unwrap();
|
||||
assert_eq!(l.format.id, "hpgl");
|
||||
let mut paths = 0;
|
||||
l.doc.walk(|n| paths += usize::from(matches!(n.kind, vectorcraft_doc::NodeKind::Path { .. })));
|
||||
assert!(paths > 15, "{paths} paths, warnings {:?}", l.warnings);
|
||||
}
|
||||
|
||||
/// A random instruction: a mnemonic the reader plays (or one it doesn't) with hostile numbers.
|
||||
fn arb_hpgl_instruction() -> impl Strategy<Value = String> {
|
||||
let op = prop::sample::select(vec![
|
||||
"IN", "DF", "PU", "PD", "PA", "PR", "SP", "PW", "WU", "NP", "PC", "CR", "LT", "UL", "LA", "FT", "SC", "IP", "IR", "RO", "CI", "AA", "AR",
|
||||
"AT", "RT", "BZ", "BR", "EA", "ER", "RA", "RR", "EW", "WG", "PM", "FP", "EP", "LB", "DT", "SM", "CO", "PG", "IW", "PE", "ZZ",
|
||||
]);
|
||||
let number = prop_oneof![
|
||||
Just("0".to_string()),
|
||||
Just("-1".to_string()),
|
||||
(-3000i32..3000).prop_map(|v| v.to_string()),
|
||||
Just("99999999999999999999999".to_string()),
|
||||
Just("0.000001".to_string()),
|
||||
Just("32767".to_string()),
|
||||
Just("-32768".to_string()),
|
||||
Just("360".to_string()),
|
||||
];
|
||||
(op, prop::collection::vec(number, 0..8)).prop_map(|(o, n)| format!("{o}{};", n.join(",")))
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(config())]
|
||||
|
||||
#[test]
|
||||
fn random_hpgl_instructions_never_panic(instructions in prop::collection::vec(arb_hpgl_instruction(), 0..40)) {
|
||||
let bytes = format!("IN;SP1;{}", instructions.concat()).into_bytes();
|
||||
survive_metafile("HP-GL instructions", "x.plt", &bytes)?;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mutated_hpgl_files_never_panic(cut in 0usize..1200, edits in prop::collection::vec((0usize..1200, any::<u8>()), 0..16)) {
|
||||
let mut bytes = HPGL_SAMPLE.to_vec();
|
||||
let n = bytes.len();
|
||||
for &(at, b) in &edits {
|
||||
bytes[at % n] = b;
|
||||
}
|
||||
bytes.truncate(cut.max(4));
|
||||
survive_metafile("mutated HP-GL", "x.plt", &bytes)?;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hpgl_garbage_never_panics(bytes in prop::collection::vec(any::<u8>(), 0..400)) {
|
||||
let mut b = b"IN;SP1;PD".to_vec();
|
||||
b.extend(bytes);
|
||||
survive_metafile("HP-GL garbage", "x.hpgl", &b)?;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- print presets files ----------
|
||||
|
||||
/// Import print presets from `data`, then lay out (and print, when short) the rich document with
|
||||
|
||||
@@ -9,14 +9,18 @@
|
||||
//! a script's own method: a getter, a function, a date, a regular expression, an error, a boxed
|
||||
//! primitive, an array with properties besides its elements, an object whose prototype is not
|
||||
//! `Object.prototype`, a property that is not enumerable or any other object that is not plain
|
||||
//! data throws a `TypeError`, and so does an object that contains itself. A value nested deeper, with more entries or longer text than the
|
||||
//! data throws a `TypeError`, and so does an object that contains itself. A literal makes an
|
||||
//! extensible object whose properties are writable and configurable and an array whose `length`
|
||||
//! is writable, so a property that is not writable or not configurable, an object or array that is
|
||||
//! not extensible (after `Object.preventExtensions`, `Object.seal` or `Object.freeze`) and an
|
||||
//! array with a read-only `length` throw a `TypeError` too. A value nested deeper, with more entries or longer text than the
|
||||
//! realm's [`crate::DataLimits`] allow throws a `RangeError`. Every value written takes fuel.
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::{Builder, arg, method};
|
||||
use crate::heap::{Class, NativeFn, ObjId, Prop, key};
|
||||
use crate::heap::{CONFIGURABLE, Class, NativeFn, ObjId, Object, Prop, WRITABLE, key};
|
||||
use crate::interp::{ErrorType, Interp, JsResult};
|
||||
use crate::value::{Value, array_index, index_key, number_to_str};
|
||||
use crate::{ScriptItem, bytecode::Code};
|
||||
@@ -289,10 +293,14 @@ impl Source {
|
||||
Class::Ordinary => return Err(interp.type_error(NOT_PLAIN_OBJECT)),
|
||||
_ => None,
|
||||
};
|
||||
let fixed = shape.as_ref().and(fixed_state(o));
|
||||
let Some(shape) = shape else {
|
||||
let what = format!("toSource: a {} cannot be written as source", interp.object(id)?.class.name());
|
||||
return Err(interp.type_error(&what));
|
||||
};
|
||||
if let Some(what) = fixed {
|
||||
return Err(interp.type_error(what));
|
||||
}
|
||||
self.ancestors.push(id);
|
||||
let r = match shape {
|
||||
Err(length) => self.array(interp, id, length),
|
||||
@@ -347,6 +355,23 @@ const NOT_PLAIN_ARRAY: &str =
|
||||
"toSource: an array with properties besides its elements, a property that is not enumerable or another prototype cannot be written as source";
|
||||
const NOT_PLAIN_OBJECT: &str =
|
||||
"toSource: an object with a property that is not enumerable, or whose prototype is not Object.prototype, cannot be written as source";
|
||||
const NOT_EXTENSIBLE: &str = "toSource: a non-extensible object cannot be written as source";
|
||||
const READ_ONLY_LENGTH: &str = "toSource: an array with a read-only length cannot be written as source";
|
||||
const FIXED_PROPERTY: &str = "toSource: a property that is not writable or configurable cannot be written as source";
|
||||
|
||||
/// Why the literal for the array or plain object `o` would not make it again: `o` is not
|
||||
/// extensible, its `length` is read-only, or one of its data properties is not writable or not
|
||||
/// configurable. A literal makes each of these open.
|
||||
fn fixed_state(o: &Object) -> Option<&'static str> {
|
||||
if !o.extensible {
|
||||
return Some(NOT_EXTENSIBLE);
|
||||
}
|
||||
if matches!(o.class, Class::Array { length_writable: false, .. }) {
|
||||
return Some(READ_ONLY_LENGTH);
|
||||
}
|
||||
let open = WRITABLE | CONFIGURABLE;
|
||||
o.props.iter().any(|(_, p)| matches!(p, Prop::Data { flags, .. } if flags & open != open)).then_some(FIXED_PROPERTY)
|
||||
}
|
||||
|
||||
/// The getter behind `item`; [`ScriptItem::Global`] is a value and has none.
|
||||
pub(crate) fn getter(item: ScriptItem) -> Option<(&'static str, NativeFn)> {
|
||||
|
||||
@@ -126,6 +126,45 @@ fn to_source_refuses_what_is_not_plain_data() {
|
||||
assert!(thrown(&mut r, "toSource({get g() { throw 'ran'; }})").starts_with("TypeError"), "the getter does not run");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_source_refuses_flags_and_extensibility_a_literal_does_not_make() {
|
||||
let mut r = realm();
|
||||
let property = "TypeError: toSource: a property that is not writable or configurable cannot be written as source";
|
||||
let extensible = "TypeError: toSource: a non-extensible object cannot be written as source";
|
||||
let length = "TypeError: toSource: an array with a read-only length cannot be written as source";
|
||||
for (source, want) in [
|
||||
("var o = {}; Object.defineProperty(o, 'a', {value: 1, enumerable: true, configurable: true}); toSource(o)", property),
|
||||
("var o = {}; Object.defineProperty(o, 'a', {value: 1, enumerable: true, writable: true}); toSource(o)", property),
|
||||
("var a = [1, 2]; Object.defineProperty(a, '1', {writable: false}); toSource(a)", property),
|
||||
("var a = [1, 2]; Object.defineProperty(a, '0', {configurable: false}); a.toSource()", property),
|
||||
("var o = {}; Object.defineProperty(o, 'a', {value: 1, enumerable: true}); toSource({x: [{b: o}]})", property),
|
||||
("toSource(Object.preventExtensions({}))", extensible),
|
||||
("toSource(Object.preventExtensions([]))", extensible),
|
||||
("toSource(Object.seal({a: 1}))", extensible),
|
||||
("toSource(Object.freeze({a: 1}))", extensible),
|
||||
("toSource(Object.seal([1]))", extensible),
|
||||
("toSource(Object.freeze([1]))", extensible),
|
||||
("toSource([1, {b: Object.freeze({})}])", extensible),
|
||||
("var a = []; Object.defineProperty(a, 'length', {writable: false}); toSource(a)", length),
|
||||
("var a = [1, , 3]; Object.defineProperty(a, 'length', {writable: false}); toSource({a: a})", length),
|
||||
] {
|
||||
assert_eq!(thrown(&mut r, source), want, "{source}");
|
||||
}
|
||||
// The refused states are ones the literal would lose: evaluated, it is open again.
|
||||
let lost = "var o = Object.freeze({a: 1}); var w = eval('({\"a\":1})');
|
||||
[Object.isExtensible(o), Object.isExtensible(w), Object.getOwnPropertyDescriptor(w, 'a').writable].join()";
|
||||
assert_eq!(text(&mut r, lost), "false,true,true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_source_writes_properties_defined_with_every_flag_on() {
|
||||
let mut r = realm();
|
||||
let source = "var o = {}; Object.defineProperty(o, 'a', {value: 1, writable: true, enumerable: true, configurable: true});
|
||||
var a = [0]; Object.defineProperty(a, '0', {value: 2, writable: true, enumerable: true, configurable: true});
|
||||
toSource({o: o, a: a, e: {}, f: [], s: [1, , 3]})";
|
||||
assert_eq!(text(&mut r, source), r#"({"o":({"a":1}), "a":[2], "e":({}), "f":[], "s":[1, , 3]})"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_source_of_a_string_with_many_escapes_throws_a_range_error_before_the_heap_limit() {
|
||||
let mut r = realm();
|
||||
|
||||
@@ -70,8 +70,13 @@ fn message(i: &mut Interp, name: &str) -> JsResult<Value> {
|
||||
i.get_value(&text, &key(name))
|
||||
}
|
||||
|
||||
/// The field `name` of a record the host gave: its own data property, `undefined` when it has none,
|
||||
/// so that a property a script put on `Object.prototype` is not read as part of it.
|
||||
fn field(i: &mut Interp, v: &Value, name: &str) -> JsResult<Value> {
|
||||
i.get_value(v, &key(name))
|
||||
match v.as_object() {
|
||||
Some(o) => slot(i, o, name),
|
||||
None => Ok(Value::Undefined),
|
||||
}
|
||||
}
|
||||
|
||||
/// The value of the own data property `name` of `o`, `undefined` when it has none.
|
||||
@@ -310,8 +315,19 @@ fn parent(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value>
|
||||
instance(i, proto, up)
|
||||
}
|
||||
|
||||
/// `exists`: whether the path names something of the object's own kind, a folder for a `Folder`
|
||||
/// and anything else for a `File`.
|
||||
fn exists(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
||||
Ok(Value::Bool(stat_field(i, &this, "exists", "exists", Value::Bool(false))?.truthy()))
|
||||
let folder = classes(i)?.folder_proto;
|
||||
let want_dir = match this.as_object() {
|
||||
Some(o) => i.object(o)?.proto == Some(folder),
|
||||
None => false,
|
||||
};
|
||||
let Some(r) = stat(i, &this, "exists")? else { return Ok(Value::Bool(false)) };
|
||||
i.rooted(std::slice::from_ref(&r), |i| {
|
||||
let dir = field(i, &r, "dir")?.truthy();
|
||||
Ok(Value::Bool(field(i, &r, "exists")?.truthy() && dir == want_dir))
|
||||
})
|
||||
}
|
||||
|
||||
fn created(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
||||
@@ -607,7 +623,7 @@ fn get_files(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<V
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
let entries = field(i, &r, "entries")?;
|
||||
let length = field(i, &entries, "length")?;
|
||||
let length = i.get_value(&entries, &key("length"))?;
|
||||
let n = i.to_number(&length)?;
|
||||
i.reserve_heap(OBJECT_BYTES)?;
|
||||
let proto = i.intrinsics.array_proto;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! The E4X built-ins (ECMA-357 sections 13.1 to 13.5): `XML`, `XMLList`, `Namespace`, `QName`,
|
||||
//! `isXMLName` and `setDefaultXMLNamespace`. A realm has them only with [`crate::Options::e4x`].
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::builtins::{arg, constructor, data, method, native};
|
||||
@@ -662,19 +663,25 @@ fn has_simple_content(interp: &mut Interp, this: Value, _: &[Value], _: bool) ->
|
||||
}
|
||||
}
|
||||
|
||||
/// The namespaces in scope at `x`, nearest declarations first, one for each prefix.
|
||||
/// The namespaces in scope at `x`, nearest declarations first, one for each prefix. Takes one unit
|
||||
/// of fuel for each node it climbs through and for each declaration it reads.
|
||||
fn scope_of(interp: &mut Interp, x: Option<ObjId>) -> JsResult<Vec<XmlNs>> {
|
||||
let mut out: Vec<XmlNs> = Vec::new();
|
||||
let mut seen: HashSet<Option<Key>> = HashSet::new();
|
||||
let mut cur = x;
|
||||
while let Some(y) = cur {
|
||||
let node = interp.xml_node(y)?.clone();
|
||||
for ns in &node.ns {
|
||||
interp.use_fuel()?;
|
||||
let (list, parent) = {
|
||||
let node = interp.xml_node(y)?;
|
||||
(node.ns.clone(), node.parent)
|
||||
};
|
||||
for ns in list {
|
||||
interp.use_fuel()?;
|
||||
if !out.iter().any(|n| n.prefix == ns.prefix) {
|
||||
out.push(ns.clone());
|
||||
if seen.insert(ns.prefix.clone()) {
|
||||
out.push(ns);
|
||||
}
|
||||
}
|
||||
cur = node.parent;
|
||||
cur = parent;
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
@@ -702,9 +709,8 @@ fn namespace_declarations(interp: &mut Interp, this: Value, _: &[Value], _: bool
|
||||
return namespaces_array(interp, Vec::new());
|
||||
}
|
||||
let parent = interp.xml_node(x)?.parent;
|
||||
let ancestors = scope_of(interp, parent)?;
|
||||
let declared: Vec<XmlNs> =
|
||||
interp.xml_node(x)?.ns.iter().filter(|ns| !ancestors.iter().any(|a| a.prefix == ns.prefix && a.uri == ns.uri)).cloned().collect();
|
||||
let ancestors: HashMap<Option<Key>, Key> = scope_of(interp, parent)?.into_iter().map(|a| (a.prefix, a.uri)).collect();
|
||||
let declared: Vec<XmlNs> = interp.xml_node(x)?.ns.iter().filter(|ns| ancestors.get(&ns.prefix) != Some(&ns.uri)).cloned().collect();
|
||||
namespaces_array(interp, declared)
|
||||
}
|
||||
|
||||
|
||||
@@ -740,6 +740,16 @@ fn insert_child_before_and_after_take_fuel_for_each_child_they_scan() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn namespace_lookups_take_fuel_for_each_ancestor_they_climb() {
|
||||
let shallow = "var x = <a><a/></a>; var leaf = x.children()[0];";
|
||||
let deep = "var x = <a/>; var leaf = x; for (var i = 0; i < 150; i++) { var c = <a/>; leaf.appendChild(c); leaf = c; }";
|
||||
for action in ["leaf.inScopeNamespaces()", "leaf.namespaceDeclarations()", "leaf.namespace()", "leaf.namespace('p')"] {
|
||||
let (s, d) = (fuel_of(shallow, action), fuel_of(deep, action));
|
||||
assert!(d >= s + 149, "{action}: {s} one level down, {d} 150 levels down");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn limits_cannot_be_caught_by_the_script() {
|
||||
assert_eq!(kind_of(TIGHT, "try { for (;;) (<a/>).toXMLString(); } catch (e) { 1 }"), RunErrorKind::Limit(LimitKind::Fuel));
|
||||
|
||||
@@ -1966,6 +1966,10 @@ fn drawing_gestures_snap_to_smart_guides() {
|
||||
let b = &made["bounds"];
|
||||
[&b["x"], &b["y"], &b["width"], &b["height"]].map(|v| v.as_f64().unwrap())
|
||||
};
|
||||
// The rectangle drawn first stays selected, and the press lands on its box's corner handle,
|
||||
// which would scale it instead of drawing.
|
||||
let r = call(&mut s, 30, "run_command", json!({"command": "select.none", "params": {}}));
|
||||
assert_eq!(r["isError"], false, "{r}");
|
||||
let events = json!([{"kind": "move", "x": 202, "y": 203}, {"kind": "down", "x": 202, "y": 203}, {"kind": "drag", "x": 330, "y": 341}, {"kind": "up", "x": 330, "y": 341}]);
|
||||
assert_eq!(bounds(&mut s, 3, events, "rectangle"), [200.0, 200.0, 130.0, 141.0]);
|
||||
let r = call(&mut s, 5, "run_command", json!({"command": "select.none", "params": {}}));
|
||||
|
||||
@@ -1212,7 +1212,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.colorPanel",
|
||||
"Color Panel Options",
|
||||
"",
|
||||
"{mode?: grayscale|rgb|hsb|cmyk|websafe, options?: bool} pick the Color panel's mode as its panel menu does (the active color converts to the mode's model as one undo step), and Show Options (true) or Hide Options (false); both are kept with the preferences; RGB, HSB, Web Safe RGB and hex values use the working RGB space (ui.inspect: ui.color_mode, ui.color_hide_options) → {mode (null: none picked yet), options, shown: the mode the panel shows the color in, or tint for a tint of a global or spot swatch}",
|
||||
"{mode?: grayscale|rgb|hsb|cmyk|websafe, options?: bool, recentColors?: bool} pick the Color panel's mode as its panel menu does (the active color converts to the mode's model as one undo step), Show Options (true) or Hide Options (false), and check (true) or uncheck (false) Show Recent Colors, which shows or hides the panel's Recent Colors row (applied colors still join the list while it is hidden; the Swatches panel keeps its row); all three are kept with the preferences; RGB, HSB, Web Safe RGB and hex values use the working RGB space (ui.inspect: ui.color_mode, ui.color_hide_options, ui.color_show_recent_colors) → {mode (null: none picked yet), options, recentColors, shown: the mode the panel shows the color in, or tint for a tint of a global or spot swatch}",
|
||||
),
|
||||
(
|
||||
"ui.savePdfDialog",
|
||||
@@ -1395,7 +1395,13 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.action.insertStop",
|
||||
"Insert Stop",
|
||||
"",
|
||||
"{set?: index, action?: index, step?: index, message?, allowContinue?: bool} insert a stop, at which a playing action waits and shows message. With set and action (0 is the top set or action), the stop goes into that action, after step, else after its last step; without them it goes into the recording in progress, after the steps recorded so far. message defaults to none, and allowContinue, which offers Continue in the message box, defaults to false. A stop in an action becomes the panel's selected step, and its action and set become the selected ones. An index past the last set, action or step, a param of another type, params that are not an object, or no action and no recording is an error and changes nothing. The panel menu's Insert Stop… (dialog `actionStopRecord` with message, allowContinue, set, action and step) runs this on OK → {set, action, step}, or {recording: true}",
|
||||
"{set?: index, action?: index, step?: index, message?, allowContinue?: bool} insert a stop, at which a playing action waits and shows message. With set and action (0 is the top set or action), the stop goes into that action, after step, else after its last step; without them it goes into the recording in progress, after the steps recorded so far. message defaults to none, and allowContinue, which offers Continue in the message box, defaults to false. A stop in an action becomes the panel's selected step, and its action and set become the selected ones. An index past the last set, action or step, a param of another type, params that are not an object, no action and no recording, or a stop that would give the action more than 10,000 steps or its set's file more than 8 MiB, counting the steps that the recording in progress adds to the set, is an error and changes nothing. The panel menu's Insert Stop… (dialog `actionStopRecord` with message, allowContinue, set, action and step) runs this on OK → {set, action, step}, or {recording: true}",
|
||||
),
|
||||
(
|
||||
"ui.action.insertMenuItem",
|
||||
"Insert Menu Item",
|
||||
"",
|
||||
"{set?: index, action?: index, step?: index, command, params?: object} insert the command of a menu item as a step, without running it; the step plays as choosing the item runs it. command and params (default {}) must be those of a menu item that can be a step: one that runs an engine command, whose label does not end with \"…\", outside the VectorCraft, File, Window and Help menus, and that is not Undo, Redo, a Paste, a batch (command.batch), a preference (prefs.*) or action (action.*) command, an Object › Align item, Image Trace's Make or Make and Expand, Pattern's Make or Edit Pattern, a saved selection, saved view or recent font item, or a plug-in's item. The place is as ui.action.insertStop takes it: with set and action, after step, else after the action's last step; without them, the recording in progress. The new step becomes the panel's selected step, and its action and set become the selected ones. A command and params that no such menu item has, a param of another type, params that are not an object, no action and no recording, or a step that would give the action more than 10,000 steps or its set's file more than 8 MiB, counting the steps that the recording in progress adds to the set, is an error and changes nothing. The panel menu's Insert Menu Item… (dialog `actionInsertMenuItem` with find, command, params, set, action and step) runs this on OK → {set, action, step}, or {recording: true}",
|
||||
),
|
||||
(
|
||||
"ui.action.continue",
|
||||
|
||||
@@ -17,6 +17,9 @@ const MAX_BULGE_SAMPLES: f64 = 1024.0;
|
||||
/// About how many points of a long piece its first fit uses.
|
||||
const COARSE_POINTS: usize = 16;
|
||||
|
||||
/// Most times [`fit_through_rec`] splits a run of points, which bounds its recursion.
|
||||
const MAX_SPLITS: usize = 40;
|
||||
|
||||
/// Unit vector or `None` if (nearly) zero.
|
||||
pub(crate) fn unit(v: Vec2) -> Option<Vec2> {
|
||||
let l = v.hypot();
|
||||
@@ -37,7 +40,7 @@ pub(crate) fn end_tangent(c: &CubicBez) -> Vec2 {
|
||||
///
|
||||
/// `t0` is the unit tangent leaving `pts[0]`; `t1` is the unit tangent arriving at the last point
|
||||
/// (forward direction). Consecutive output cubics are G1 continuous.
|
||||
pub(crate) fn fit_cubics(pts: &[Point], t0: Vec2, t1: Vec2, tol: f64) -> Vec<CubicBez> {
|
||||
pub fn fit_cubics(pts: &[Point], t0: Vec2, t1: Vec2, tol: f64) -> Vec<CubicBez> {
|
||||
let pts = dedup(pts);
|
||||
if pts.len() < 2 {
|
||||
return Vec::new();
|
||||
@@ -48,6 +51,43 @@ pub(crate) fn fit_cubics(pts: &[Point], t0: Vec2, t1: Vec2, tol: f64) -> Vec<Cub
|
||||
join_pieces(&pts, &pieces, t0, tol)
|
||||
}
|
||||
|
||||
/// Fit a chain of cubics through `pts`, sampled from a smooth curve, with maximum error `tol` at
|
||||
/// the points.
|
||||
///
|
||||
/// Unlike [`fit_cubics`], a piece may leave the chords between the points: the points lie on the
|
||||
/// curve being fitted, so the piece bows with them (each end keeps its tangent, and a two-point
|
||||
/// piece gets handles a third of its chord long). Use this for dense samples of a known curve, and
|
||||
/// [`fit_cubics`] for the samples of a polyline. `t0` and `t1` are as in [`fit_cubics`].
|
||||
pub fn fit_cubics_through(pts: &[Point], t0: Vec2, t1: Vec2, tol: f64) -> Vec<CubicBez> {
|
||||
let pts = dedup(pts);
|
||||
let mut out = Vec::new();
|
||||
if pts.len() < 2 {
|
||||
return out;
|
||||
}
|
||||
fit_through_rec(&pts, t0, t1, tol.max(1e-9), &mut out, 0);
|
||||
out
|
||||
}
|
||||
|
||||
/// Fits all of `pts` with one cubic, splitting at its worst point until each piece is within `tol`
|
||||
/// of its points (see [`fit_cubics_through`]). `depth` bounds the recursion on hostile points.
|
||||
fn fit_through_rec(pts: &[Point], t0: Vec2, t1: Vec2, tol: f64, out: &mut Vec<CubicBez>, depth: usize) {
|
||||
let n = pts.len();
|
||||
if n == 2 {
|
||||
let d = pts[0].distance(pts[1]) / 3.0;
|
||||
out.push(CubicBez::new(pts[0], pts[0] + t0 * d, pts[1] - t1 * d, pts[1]));
|
||||
return;
|
||||
}
|
||||
let (curve, err, split) = fit_single_tol(pts, t0, t1, tol);
|
||||
if err <= tol || depth >= MAX_SPLITS {
|
||||
out.push(curve);
|
||||
return;
|
||||
}
|
||||
let split = split.clamp(1, n - 2);
|
||||
let tc = unit(pts[split + 1] - pts[split - 1]).unwrap_or(t0);
|
||||
fit_through_rec(&pts[..=split], t0, tc, tol, out, depth + 1);
|
||||
fit_through_rec(&pts[split..], tc, t1, tol, out, depth + 1);
|
||||
}
|
||||
|
||||
/// The curves of `pieces`, which follow each other along `pts` from its first point, leaving it
|
||||
/// along `t0`. Each curve takes up to [`MAX_JOINS`] of the pieces after its first one, as long as
|
||||
/// the fit of them together is within `tol`.
|
||||
@@ -152,6 +192,32 @@ pub(crate) fn fit_single(pts: &[Point], t0: Vec2, t1: Vec2) -> (CubicBez, f64, u
|
||||
fit_single_from(pts, chord_params(pts), t0, t1)
|
||||
}
|
||||
|
||||
/// [`fit_single`] that stops reparameterising once the curve is within `tol` of its points: a
|
||||
/// curve already that close needs no better fit, and each Newton round reads every point.
|
||||
fn fit_single_tol(pts: &[Point], t0: Vec2, t1: Vec2, tol: f64) -> (CubicBez, f64, usize) {
|
||||
let mut u = chord_params(pts);
|
||||
let mut best = generate(pts, &u, t0, t1);
|
||||
let (mut best_err, mut best_split) = max_error(pts, &best, &u);
|
||||
for _ in 0..MAX_REPARAM {
|
||||
if best_err <= tol {
|
||||
break;
|
||||
}
|
||||
for (i, ui) in u.iter_mut().enumerate() {
|
||||
*ui = newton(&best, pts[i], *ui);
|
||||
}
|
||||
let c = generate(pts, &u, t0, t1);
|
||||
let (e, s) = max_error(pts, &c, &u);
|
||||
if e < best_err {
|
||||
best = c;
|
||||
best_err = e;
|
||||
best_split = s;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(best, best_err, best_split)
|
||||
}
|
||||
|
||||
/// [`fit_single`] starting from the parameters `u` (one per point, 0 to 1) instead of the
|
||||
/// chord-length ones.
|
||||
pub(crate) fn fit_single_from(pts: &[Point], u: Vec<f64>, t0: Vec2, t1: Vec2) -> (CubicBez, f64, usize) {
|
||||
@@ -334,6 +400,25 @@ mod tests {
|
||||
assert!(out[0].p1.distance(c.p1) < 1.0, "{:?}", out[0]);
|
||||
}
|
||||
|
||||
/// A curve's samples are followed between the points, not chopped to the chords through
|
||||
/// them: a smooth outline fitted from coarse samples must keep its bow (an outline's area
|
||||
/// depends on it), unlike a polyline's samples.
|
||||
#[test]
|
||||
fn through_fit_bows_between_coarse_points() {
|
||||
// The parabola y = 4t(1 - t) over x = 100t, sampled at 8 points.
|
||||
let node = |t: f64| Point::new(100.0 * t, 4.0 * t * (1.0 - t));
|
||||
let pts: Vec<Point> = (0..=8).map(|i| node(i as f64 / 8.0)).collect();
|
||||
let (t0, t1) = (Vec2::new(1.0, 0.04).normalize(), Vec2::new(1.0, -0.04).normalize());
|
||||
let out = fit_cubics_through(&pts, t0, t1, 0.001);
|
||||
for c in &out {
|
||||
for k in 0..=100 {
|
||||
let q = c.eval(k as f64 / 100.0);
|
||||
let want = 4.0 * (q.x / 100.0) * (1.0 - q.x / 100.0);
|
||||
assert!((q.y - want).abs() < 0.01, "{:?} at {q:?} wants {want}", c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn splits_when_needed() {
|
||||
// A zig-zag cannot be fitted with one cubic.
|
||||
|
||||
@@ -26,6 +26,7 @@ pub use edit::{
|
||||
remove_anchor, remove_redundant_points, simplify, simplify_level_tolerance, simplify_part, simplify_with, smooth, smooth_level, smooth_options,
|
||||
split_into_grid,
|
||||
};
|
||||
pub use fit::{fit_cubics, fit_cubics_through};
|
||||
pub use offset::{Cap, Join, MAX_OFFSET_SEGMENT_PAIRS, offset_path, outline_stroke, stroke_region, try_offset_path};
|
||||
pub use pathfinder::{FaceMerger, PathfinderOp, Region, Shape, merge_regions, pathfinder, pathfinder_with, region_at, regions, regions_within};
|
||||
pub use planar::{
|
||||
|
||||
@@ -63,9 +63,11 @@ fn export_with_report_inner(doc: &Document, opts: &PdfOptions, left_out: &mut Ha
|
||||
// Preserve Editing: the native document as an embedded file (`check` refused PDF/A with it).
|
||||
let mut warnings = set.warnings();
|
||||
if raised.is_some() {
|
||||
warnings.push(
|
||||
"PDF 1.5 has no UserUnit, which a page longer than 14,400 pt (200 in) needs to keep its size: the file is written as PDF 1.6".into(),
|
||||
);
|
||||
warnings.push(if opts.settings.compatibility == Compatibility::Pdf14 {
|
||||
"PDF 1.4 has no UserUnit, which a page longer than 14,400 pt (200 in) needs to stay within PDF/A-2's page limit of 14,400 units: the file is written as PDF 1.6".into()
|
||||
} else {
|
||||
"PDF 1.5 has no UserUnit, which a page longer than 14,400 pt (200 in) needs to keep its size: the file is written as PDF 1.6".into()
|
||||
});
|
||||
}
|
||||
let native = opts.native.as_deref().filter(|_| set.preserve_editing);
|
||||
if let Some(native) = native {
|
||||
@@ -199,9 +201,9 @@ fn writes_user_units(set: &PdfSettings) -> bool {
|
||||
}
|
||||
|
||||
/// `long_pages_in_points` is on in a file without a standard. PDF/X files write long pages in
|
||||
/// points anyway, and PDF/A files from PDF 1.5 on keep the `UserUnit`, which keeps their page
|
||||
/// boundaries within 14,400 units (ISO 19005-2, 6.1.13). A PDF/A file at PDF 1.4 has no
|
||||
/// `UserUnit` and writes long pages in points either way.
|
||||
/// points anyway, and PDF/A files keep the `UserUnit`, which keeps their page boundaries within
|
||||
/// 14,400 units (ISO 19005-2, 6.1.13). A PDF/A file at PDF 1.4 is written as PDF 1.6 to have one
|
||||
/// ([`for_long_pages`]).
|
||||
fn long_pages_in_points(set: &PdfSettings) -> bool {
|
||||
set.long_pages_in_points && set.standard == Standard::None
|
||||
}
|
||||
@@ -239,10 +241,20 @@ fn for_long_pages(set: &PdfSettings, pages: &[(usize, PageBoxes)]) -> Result<Opt
|
||||
}
|
||||
let raised = (set.compatibility == Compatibility::Pdf15 && !set.standard.is_pdfx() && !long_pages_in_points(set))
|
||||
.then(|| PdfSettings { compatibility: Compatibility::Pdf16, ..set.clone() });
|
||||
// PDF/A-2 limits page boundaries to 14,400 units (ISO 19005-2, 6.1.13), and PDF 1.4 has no
|
||||
// `UserUnit`: a PDF/A file at PDF 1.4 is written as PDF 1.6, with the `UserUnit` whether or
|
||||
// not `long_pages_in_points` is on.
|
||||
let raised = raised.or_else(|| {
|
||||
(set.standard == Standard::PdfA2b && set.compatibility == Compatibility::Pdf14).then(|| PdfSettings {
|
||||
compatibility: Compatibility::Pdf16,
|
||||
long_pages_in_points: false,
|
||||
..set.clone()
|
||||
})
|
||||
});
|
||||
// The writer's test for a whole number, on the `f32` it writes.
|
||||
let real = |side: f64| side > MAX_PDF14_REAL && (side as f32) as i32 as f32 != side as f32;
|
||||
if let Some((i, b)) = pages.iter().find(|(_, b)| real(b.media.width()) || real(b.media.height())) {
|
||||
page_too_long(set, *i, b.media.width().max(b.media.height()))?;
|
||||
page_too_long(raised.as_ref().unwrap_or(set), *i, b.media.width().max(b.media.height()))?;
|
||||
}
|
||||
Ok(raised)
|
||||
}
|
||||
|
||||
@@ -256,14 +256,48 @@ fn every_preset_and_version_keeps_a_20_m_page_or_says_why_not() {
|
||||
}
|
||||
}
|
||||
}
|
||||
// PDF/X-4 and PDF/A-2b files at PDF 1.4 have no `UserUnit` either.
|
||||
// PDF/X-4 files at PDF 1.4 have no `UserUnit` either; PDF/A-2b files at PDF 1.4 are written as
|
||||
// PDF 1.6 to have one.
|
||||
let at_14 = |standard| report(&d, PdfSettings { standard, compatibility: Compatibility::Pdf14, ..PdfSettings::default() });
|
||||
refused(at_14(Standard::PdfX4).err().unwrap(), "PDF 1.4");
|
||||
refused(at_14(Standard::PdfA2b).err().unwrap(), "PDF 1.4");
|
||||
let r = at_14(Standard::PdfA2b).unwrap();
|
||||
assert!(r.bytes.starts_with(b"%PDF-1.6"));
|
||||
assert_eq!(raw_numbers(&r.bytes, "/UserUnit", 1), [3.938]);
|
||||
}
|
||||
|
||||
/// PDF/A-2 limits page boundaries to 14,400 units (ISO 19005-2, 6.1.13), and PDF 1.4 has no
|
||||
/// `UserUnit`: a PDF/A-2b file at PDF 1.4 with a page longer than 14,400 pt is written as PDF 1.6,
|
||||
/// with a `UserUnit` that keeps the page within 14,400 units and a warning, with
|
||||
/// `long_pages_in_points` on or off. Pages past 32,767 pt that aren't whole numbers of points are
|
||||
/// written too. A file whose pages are all 14,400 pt or shorter stays PDF 1.4.
|
||||
#[test]
|
||||
fn pdfa2b_at_pdf_1_4_writes_a_long_page_as_pdf_1_6_within_14400_units() {
|
||||
for in_points in [false, true] {
|
||||
let set = PdfSettings {
|
||||
standard: Standard::PdfA2b,
|
||||
compatibility: Compatibility::Pdf14,
|
||||
long_pages_in_points: in_points,
|
||||
..PdfSettings::default()
|
||||
};
|
||||
for side in [17_000.0, 32_767.0, 32_767.5, 40_000.0, TWENTY_M] {
|
||||
let r = report(&sized(side, 300.0), set.clone()).unwrap_or_else(|e| panic!("{side} {in_points}: {e}"));
|
||||
assert!(r.bytes.starts_with(b"%PDF-1.6"), "{side} {in_points}");
|
||||
let unit = raw_numbers(&r.bytes, "/UserUnit", 1);
|
||||
assert_eq!(unit.len(), 1, "{side} {in_points}");
|
||||
let media = raw_numbers(&r.bytes, "/MediaBox", 4);
|
||||
assert!(media[2] - media[0] <= 14_400.0 && media[3] - media[1] <= 14_400.0, "{side} {in_points}: {media:?}");
|
||||
assert!(((media[2] - media[0]) * unit[0] - side).abs() < side * 0.001, "{side} {in_points}: {media:?} {unit:?}");
|
||||
let raised = "PDF 1.4 has no UserUnit, which a page longer than 14,400 pt (200 in) needs to stay within PDF/A-2's page limit of 14,400 units: the file is written as PDF 1.6";
|
||||
assert!(r.warnings.iter().any(|w| w == raised), "{side} {in_points}: {:?}", r.warnings);
|
||||
}
|
||||
let r = report(&sized(14_400.0, 300.0), set.clone()).unwrap();
|
||||
assert!(r.bytes.starts_with(b"%PDF-1.4"), "{in_points}");
|
||||
assert!(!r.warnings.iter().any(|w| w.contains("has no UserUnit")), "{in_points}: {:?}", r.warnings);
|
||||
}
|
||||
}
|
||||
|
||||
/// A page from 14,400 to 32,767 pt long is written in points, with the warning, in PDF 1.3 and 1.4
|
||||
/// files, PDF/X-1a, PDF/X-3, PDF/X-4 and PDF/A-2b files among them, as before the warning. Past
|
||||
/// files, PDF/X-1a, PDF/X-3 and PDF/X-4 files among them, as before the warning. Past
|
||||
/// 32,767 pt a page of whole points is written too, as the writer puts whole numbers as integers;
|
||||
/// a side that isn't a whole number, or art that needs a real number past 32,767, gets the error.
|
||||
#[test]
|
||||
@@ -276,7 +310,6 @@ fn pdf_1_4_writes_a_page_up_to_32767_pt_in_points() {
|
||||
("PDF/X-1a:2001", preset("PDF/X-1a:2001")),
|
||||
("PDF/X-3:2002", preset("PDF/X-3:2002")),
|
||||
("PDF/X-4:2010", PdfSettings { standard: Standard::PdfX4, compatibility: Compatibility::Pdf14, ..PdfSettings::default() }),
|
||||
("PDF/A-2b", PdfSettings { standard: Standard::PdfA2b, compatibility: Compatibility::Pdf14, ..PdfSettings::default() }),
|
||||
];
|
||||
for (label, set) in &sets {
|
||||
for side in [17_000.0, 32_767.0, 40_000.0] {
|
||||
@@ -287,8 +320,8 @@ fn pdf_1_4_writes_a_page_up_to_32767_pt_in_points() {
|
||||
let points = format!("{label} pages are written without a UserUnit, in points, and apps that keep to the limit cut the page to 200 in");
|
||||
assert!(w.len() == 1 && w[0].ends_with(&points), "{label}: {w:?}");
|
||||
}
|
||||
// For PDF/X-4 and PDF/A-2b files the error names the PDF version.
|
||||
let label = if matches!(*label, "PDF/X-4:2010" | "PDF/A-2b") { "PDF 1.4" } else { label };
|
||||
// For PDF/X-4 files the error names the PDF version.
|
||||
let label = if *label == "PDF/X-4:2010" { "PDF 1.4" } else { label };
|
||||
let refused = |e: PdfError, side: &str| {
|
||||
let refusal = format!("{label} files hold real numbers up to 32,767, and the page of artboard 1, {side} pt long, needs larger ones");
|
||||
assert!(e.to_string().contains(&refusal), "{e}");
|
||||
|
||||
@@ -92,6 +92,11 @@ fn a_long_profile_stroke_keeps_its_widths_when_outlined_near_the_view() {
|
||||
let near = render(&mut renderer, &d, at);
|
||||
assert_eq!(renderer.strokes.values().any(|e| e.crop.is_some()), cropped, "{name}: crop choice");
|
||||
let full = whole(&d, at);
|
||||
// The cropped outline fits cubics through the same samples, but its fit starts at the
|
||||
// crop rather than at the subpath's end, so a cubic may put its control points
|
||||
// differently (the same curve). The rasteriser then flattens it into a slightly
|
||||
// different polygon, which shows as one level on an anti-aliased edge pixel. Anything
|
||||
// more would be a wrong width.
|
||||
let mismatched: Vec<_> = near
|
||||
.pixels
|
||||
.as_chunks::<4>()
|
||||
@@ -99,7 +104,7 @@ fn a_long_profile_stroke_keeps_its_widths_when_outlined_near_the_view() {
|
||||
.iter()
|
||||
.zip(full.pixels.as_chunks::<4>().0.iter())
|
||||
.enumerate()
|
||||
.filter(|(_, (a, b))| a != b)
|
||||
.filter(|(_, (a, b))| a.iter().zip(b.iter()).any(|(a, b)| a.abs_diff(*b) > 1))
|
||||
.take(8)
|
||||
.map(|(i, (a, b))| (i % SIZE as usize, i / SIZE as usize, a.to_vec(), b.to_vec()))
|
||||
.collect();
|
||||
|
||||
@@ -17,6 +17,8 @@ pub use lights::{Light, Lights, MAX_LIGHTS};
|
||||
mod caps;
|
||||
mod mesh;
|
||||
pub use mesh::{Mesh, extrude_mesh, inflate_meshes, plane_mesh, revolve_mesh};
|
||||
mod shadow;
|
||||
pub use shadow::{MAX_SHADOW_FACES, Shadow, ShadowCaster, ShadowPlane, ShadowPosition, TOO_COMPLEX_SHADOW};
|
||||
|
||||
use vectorcraft_geom::{BezPath, FillRule, ParamCurve, PathData, Point, Rect, Shape, Vec2, kurbo::ParamCurveDeriv};
|
||||
|
||||
@@ -672,6 +674,19 @@ impl HoleSearch {
|
||||
}
|
||||
}
|
||||
|
||||
/// How sanitized Revolve options `o` project a profile with `bounds`: about the axis at the
|
||||
/// profile's middle height, with the eye as far as the swept radius or the height needs.
|
||||
fn revolve_projection(bounds: Rect, o: Revolve) -> shadow::Projection {
|
||||
let axis = match o.edge {
|
||||
Edge::Left => bounds.x0 - o.offset,
|
||||
Edge::Right => bounds.x1 + o.offset,
|
||||
};
|
||||
let center_y = (bounds.y0 + bounds.y1) * 0.5;
|
||||
let radius = (bounds.x0 - axis).abs().max((bounds.x1 - axis).abs());
|
||||
let eye = eye_distance(o.perspective, radius.max(bounds.height().abs()).max(1.0));
|
||||
shadow::Projection { view: o, origin: Point::new(axis, center_y), eye }
|
||||
}
|
||||
|
||||
/// [`revolve`], or with `mesh` the surface's faces before projection, kept there instead of drawn,
|
||||
/// each turned out of the region `rule` fills.
|
||||
fn revolve_faces(
|
||||
@@ -697,13 +712,7 @@ fn revolve_faces(
|
||||
if ![bounds.x0, bounds.y0, bounds.x1, bounds.y1].iter().all(|v| v.is_finite() && v.abs() <= 1e8) {
|
||||
return Err("Revolve requires finite profile coordinates within 100 million points".into());
|
||||
}
|
||||
let axis = match o.edge {
|
||||
Edge::Left => bounds.x0 - o.offset,
|
||||
Edge::Right => bounds.x1 + o.offset,
|
||||
};
|
||||
let center_y = (bounds.y0 + bounds.y1) * 0.5;
|
||||
let radius = (bounds.x0 - axis).abs().max((bounds.x1 - axis).abs());
|
||||
let eye = eye_distance(o.perspective, radius.max(bounds.height().abs()).max(1.0));
|
||||
let shadow::Projection { origin: Point { x: axis, y: center_y }, eye, .. } = revolve_projection(bounds, o);
|
||||
let steps = o.steps();
|
||||
let sweep = o.angle.to_radians();
|
||||
let smooth = o.shade && o.smooth_shading;
|
||||
|
||||
@@ -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, true).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, true).and_then(|(_, faces, _)| mesh(&faces))
|
||||
}
|
||||
|
||||
/// Inflate's solids for several shapes of one object ([`crate::inflate_together`]) as meshes, one
|
||||
@@ -72,7 +72,7 @@ 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, true).map(|r| r.and_then(|(_, faces, _)| mesh(&faces)))
|
||||
}
|
||||
|
||||
/// Revolve's surface ([`crate::revolve`]) as a mesh. As the effect draws it, a sweep of less than
|
||||
|
||||
@@ -0,0 +1,343 @@
|
||||
//! The shadow a 3D effect's solid casts: its faces projected along the first light onto a plane
|
||||
//! behind the object (square to the view) or below it (under the object's own vertical axis, turned
|
||||
//! with it), as one hard-edged vector shape.
|
||||
//!
|
||||
//! Every face is projected whole, so the shape is the union of the projected faces: each contour is
|
||||
//! turned so that its face winds the same way, and the shape is filled with the nonzero rule. The
|
||||
//! plane ends at a square about the foot of the object's center, so a light that grazes the plane
|
||||
//! does not cast a shadow without end.
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
use vectorcraft_geom::{BezPath, FillRule, PathData, Point, Rect};
|
||||
|
||||
use crate::solid::{Face3, extrude_faces, inflate_faces, plane_faces};
|
||||
use crate::{Extrude, Inflate, MAX_SOLID_FACES, Revolve, V3, View, finite, perspective_scale};
|
||||
|
||||
/// Where the shadow falls.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum ShadowPosition {
|
||||
/// On a plane behind the object, square to the view.
|
||||
#[default]
|
||||
Behind,
|
||||
/// On a plane below the object, square to its vertical axis and turned with it.
|
||||
Below,
|
||||
}
|
||||
|
||||
impl ShadowPosition {
|
||||
pub const ALL: [ShadowPosition; 2] = [Self::Behind, Self::Below];
|
||||
|
||||
pub fn id(self) -> &'static str {
|
||||
match self {
|
||||
Self::Behind => "behind",
|
||||
Self::Below => "below",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(s: &str) -> Option<Self> {
|
||||
Self::ALL.into_iter().find(|p| p.id() == s)
|
||||
}
|
||||
}
|
||||
|
||||
/// A shadow's settings.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Shadow {
|
||||
pub position: ShadowPosition,
|
||||
/// How far the plane is from the object, as a share (0..1) of the object's largest size.
|
||||
pub distance: f64,
|
||||
/// How far the plane reaches, as a share (0.1..4): at 1 the plane is a square four times the
|
||||
/// object's largest size across.
|
||||
pub bounds: f64,
|
||||
}
|
||||
|
||||
impl Default for Shadow {
|
||||
fn default() -> Self {
|
||||
Self { position: ShadowPosition::Behind, distance: 0.0, bounds: 1.0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Shadow {
|
||||
fn sanitized(self) -> Self {
|
||||
Self { distance: finite(self.distance, 0.0, 0.0, 1.0), bounds: finite(self.bounds, 1.0, 0.1, 4.0), ..self }
|
||||
}
|
||||
}
|
||||
|
||||
/// The rotation and projection a solid is drawn with.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(crate) struct Projection {
|
||||
/// The sanitized view: its rotation, perspective and lights.
|
||||
pub(crate) view: Revolve,
|
||||
/// Where the view's origin is on the page.
|
||||
pub(crate) origin: Point,
|
||||
/// The eye's distance in front of the origin; `None` without perspective.
|
||||
pub(crate) eye: Option<f64>,
|
||||
}
|
||||
|
||||
impl Projection {
|
||||
fn project(&self, v: V3) -> Point {
|
||||
let f = perspective_scale(self.eye, v.2);
|
||||
Point::new(self.origin.x + v.0 * f, self.origin.y + v.1 * f)
|
||||
}
|
||||
|
||||
/// The unit direction toward the first light, in view space; `None` when it lights nothing:
|
||||
/// Shading is off or its intensity is 0.
|
||||
fn light(&self) -> Option<V3> {
|
||||
let first = self.view.lights.and_then(|l| l.iter().next().copied());
|
||||
let intensity = first.map_or(self.view.light_intensity, |l| l.intensity);
|
||||
if !self.view.shade || intensity <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
let [x, y, z] = first.map_or_else(|| self.view.light_direction(), |l| l.direction());
|
||||
Some(V3(x, y, z).unit())
|
||||
}
|
||||
}
|
||||
|
||||
/// The light must reach the plane at least this steeply (its cosine to the plane's normal).
|
||||
const MIN_LIGHT: f64 = 1e-3;
|
||||
|
||||
/// Most faces one object's shadow is cast from, its solids together: as many as two of the largest
|
||||
/// solids.
|
||||
pub const MAX_SHADOW_FACES: usize = 2 * MAX_SOLID_FACES;
|
||||
|
||||
/// The error a solid added past [`MAX_SHADOW_FACES`] returns.
|
||||
pub const TOO_COMPLEX_SHADOW: &str = "The 3D shadow is too complex; turn Shadows off or simplify the art";
|
||||
|
||||
/// The plane a shadow falls on ([`ShadowCaster::shadow_plane`]), placed once for the pieces of one
|
||||
/// object's shadow ([`ShadowCaster::path_on`]).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct ShadowPlane {
|
||||
projection: Projection,
|
||||
/// The unit direction toward the light, and its cosine to the plane's normal.
|
||||
light: V3,
|
||||
reach: f64,
|
||||
/// The plane's unit normal toward the object, and two unit axes along it.
|
||||
n: V3,
|
||||
u: V3,
|
||||
w: V3,
|
||||
/// Where the plane is along its normal, and the foot of the object's center on it.
|
||||
level: f64,
|
||||
foot: V3,
|
||||
/// Half the side of the square the plane ends at.
|
||||
half: f64,
|
||||
}
|
||||
|
||||
/// The faces of one object's solids, gathered to cast one shadow. The solids of one object share
|
||||
/// their bounds and view, so they share a projection: the first solid's is kept. Each solid added
|
||||
/// returns the range of its faces, so that the shadow of some of them can be drawn on the plane
|
||||
/// of others ([`ShadowCaster::shadow_plane`], [`ShadowCaster::path_on`]).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct ShadowCaster {
|
||||
faces: Vec<Face3>,
|
||||
projection: Option<Projection>,
|
||||
}
|
||||
|
||||
impl ShadowCaster {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// How many faces have been gathered.
|
||||
pub fn len(&self) -> usize {
|
||||
self.faces.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.faces.is_empty()
|
||||
}
|
||||
|
||||
/// Keep `faces`, or an error when they would take the faces gathered past [`MAX_SHADOW_FACES`].
|
||||
fn add(&mut self, faces: Vec<Face3>, projection: Option<Projection>) -> Result<Range<usize>, String> {
|
||||
let start = self.faces.len();
|
||||
if start.saturating_add(faces.len()) > MAX_SHADOW_FACES {
|
||||
return Err(TOO_COMPLEX_SHADOW.into());
|
||||
}
|
||||
if !faces.is_empty() && self.projection.is_none() {
|
||||
self.projection = projection;
|
||||
}
|
||||
self.faces.extend(faces);
|
||||
Ok(start..self.faces.len())
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// Add Inflate's solids for several shapes of one object ([`crate::inflate_together`]), each
|
||||
/// shape whose entry in `casts` is true: the eye is placed for every shape, as for the solids
|
||||
/// drawn, but the faces of the others are not kept (their range is empty). A shape that does
|
||||
/// not inflate is left out, as its solid is; the result says which, in order.
|
||||
pub fn inflate(
|
||||
&mut self,
|
||||
shapes: &[(&PathData, FillRule)],
|
||||
casts: &[bool],
|
||||
bounds: Rect,
|
||||
options: Inflate,
|
||||
view: View,
|
||||
) -> Vec<Result<Range<usize>, String>> {
|
||||
inflate_faces(shapes, bounds, options, view, true)
|
||||
.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()) }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Add Revolve's surface ([`crate::revolve`]).
|
||||
pub fn revolve(&mut self, path: &PathData, rule: FillRule, bounds: Rect, options: Revolve) -> Result<Range<usize>, String> {
|
||||
let mut faces = vec![];
|
||||
crate::revolve_faces(path, rule, bounds, options, Some(&mut faces))?;
|
||||
let o = options.sanitized();
|
||||
self.add(faces, Some(crate::revolve_projection(bounds, o)))
|
||||
}
|
||||
|
||||
/// The shadow of the faces gathered, on the page, filled with the nonzero rule: `None` when
|
||||
/// there are none, when the first light lights nothing (Shading is off or its intensity is 0)
|
||||
/// or does not reach the plane (it is behind the object for a shadow behind it, or below the
|
||||
/// plane for one below), or when nothing of the shadow falls within the plane.
|
||||
pub fn path(&self, shadow: Shadow) -> Option<PathData> {
|
||||
let all = 0..self.faces.len();
|
||||
let all = std::slice::from_ref(&all);
|
||||
self.path_on(&self.shadow_plane(shadow, all)?, all)
|
||||
}
|
||||
|
||||
/// The faces in the ranges `faces`, from the faces gathered.
|
||||
fn within(&self, faces: &[Range<usize>]) -> impl Iterator<Item = &Face3> {
|
||||
faces.iter().flat_map(|r| self.faces.get(r.clone()).unwrap_or_default())
|
||||
}
|
||||
|
||||
/// The plane `shadow` falls on for the faces in the ranges `faces` (its distance, size and
|
||||
/// center follow from them), for [`ShadowCaster::path_on`]: `None` when there are none or when
|
||||
/// the first light lights nothing or does not reach the plane.
|
||||
pub fn shadow_plane(&self, shadow: Shadow, faces: &[Range<usize>]) -> Option<ShadowPlane> {
|
||||
let p = self.projection?;
|
||||
let light = p.light()?;
|
||||
let s = shadow.sanitized();
|
||||
let corners = || self.within(faces).flat_map(|(contours, _)| contours.iter().flatten());
|
||||
let first = *corners().next()?;
|
||||
let (lo, hi) = corners()
|
||||
.fold((first, first), |(lo, hi), v| (V3(lo.0.min(v.0), lo.1.min(v.1), lo.2.min(v.2)), V3(hi.0.max(v.0), hi.1.max(v.1), hi.2.max(v.2))));
|
||||
let size = (hi.0 - lo.0).max(hi.1 - lo.1).max(hi.2 - lo.2).max(1e-9);
|
||||
// The plane's normal toward the object, and two axes along it.
|
||||
let (n, u, w) = match s.position {
|
||||
ShadowPosition::Behind => (V3(0.0, 0.0, 1.0), V3(1.0, 0.0, 0.0), V3(0.0, 1.0, 0.0)),
|
||||
ShadowPosition::Below => {
|
||||
let r = |v: V3| p.view.rotate(v).unit();
|
||||
(r(V3(0.0, -1.0, 0.0)), r(V3(1.0, 0.0, 0.0)), r(V3(0.0, 0.0, 1.0)))
|
||||
}
|
||||
};
|
||||
let reach = n.dot(light);
|
||||
if !reach.is_finite() || reach < MIN_LIGHT {
|
||||
return None;
|
||||
}
|
||||
// The plane: every corner at least `distance` × size in front of it.
|
||||
let level = corners().map(|v| n.dot(*v)).fold(f64::INFINITY, f64::min) - s.distance * size;
|
||||
let centre = V3((lo.0 + hi.0) / 2.0, (lo.1 + hi.1) / 2.0, (lo.2 + hi.2) / 2.0);
|
||||
let foot = add(centre, scale(n, level - n.dot(centre)));
|
||||
Some(ShadowPlane { projection: p, light, reach, n, u, w, level, foot, half: 2.0 * s.bounds * size })
|
||||
}
|
||||
|
||||
/// The shadow the faces in the ranges `faces` cast on `plane`, on the page, filled with the
|
||||
/// nonzero rule; `None` when nothing of it falls within the plane.
|
||||
pub fn path_on(&self, plane: &ShadowPlane, faces: &[Range<usize>]) -> Option<PathData> {
|
||||
let &ShadowPlane { projection: p, light, reach, n, u, w, level, foot, half } = plane;
|
||||
// A corner's shadow, in the plane's axes about the foot.
|
||||
let cast = |v: V3| {
|
||||
let q = add(v, scale(light, -(n.dot(v) - level) / reach));
|
||||
let d = q.sub(foot);
|
||||
Point::new(u.dot(d), w.dot(d))
|
||||
};
|
||||
let to_page = |q: Point| p.project(add(foot, add(scale(u, q.x), scale(w, q.y))));
|
||||
let mut bp = BezPath::new();
|
||||
for (contours, _) in self.within(faces) {
|
||||
let flat: Vec<Vec<Point>> = contours.iter().map(|c| c.iter().map(|v| cast(*v)).collect()).collect();
|
||||
let turn: f64 = flat.iter().map(|c| signed_area(c)).sum();
|
||||
// A face edge-on to the light casts nothing.
|
||||
if flat_area(turn, flat.iter().flatten()) {
|
||||
continue;
|
||||
}
|
||||
for c in &flat {
|
||||
let mut clipped = clip(c, half);
|
||||
if turn < 0.0 {
|
||||
clipped.reverse();
|
||||
}
|
||||
// A plane seen edge-on draws nothing.
|
||||
let page: Vec<Point> = clipped.into_iter().map(to_page).collect();
|
||||
if flat_area(signed_area(&page), &page) {
|
||||
continue;
|
||||
}
|
||||
let Some((start, rest)) = page.split_first() else { continue };
|
||||
bp.move_to(*start);
|
||||
for q in rest {
|
||||
bp.line_to(*q);
|
||||
}
|
||||
bp.close_path();
|
||||
}
|
||||
}
|
||||
let path = PathData::from_bezpath(&bp);
|
||||
(!path.subpaths.is_empty()).then_some(path)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `area`, the signed area of a polygon with corners `points`, is within rounding of
|
||||
/// nothing for its size (the square of its bounds' diagonal), or not a number, so the polygon draws
|
||||
/// nothing. A long, narrow polygon keeps an area well above that.
|
||||
fn flat_area<'a>(area: f64, points: impl IntoIterator<Item = &'a Point>) -> bool {
|
||||
let mut points = points.into_iter();
|
||||
let Some(first) = points.next() else { return true };
|
||||
let bounds = points.fold(Rect::from_points(*first, *first), |b, p| b.union_pt(*p));
|
||||
let size = bounds.width().powi(2) + bounds.height().powi(2);
|
||||
!area.is_finite() || !size.is_finite() || area.abs() <= 1e-14 * size
|
||||
}
|
||||
|
||||
fn add(a: V3, b: V3) -> V3 {
|
||||
V3(a.0 + b.0, a.1 + b.1, a.2 + b.2)
|
||||
}
|
||||
|
||||
fn scale(a: V3, k: f64) -> V3 {
|
||||
V3(a.0 * k, a.1 * k, a.2 * k)
|
||||
}
|
||||
|
||||
/// Twice the signed area of polygon `c` (positive counterclockwise with y up).
|
||||
fn signed_area(c: &[Point]) -> f64 {
|
||||
c.iter().zip(c.iter().cycle().skip(1)).take(c.len()).map(|(a, b)| a.x * b.y - b.x * a.y).sum::<f64>() / 2.0
|
||||
}
|
||||
|
||||
/// Polygon `c` cut to the square from -`half` to `half` on both axes, edge by edge (the square is
|
||||
/// convex, so each contour's winding inside it is kept).
|
||||
fn clip(c: &[Point], half: f64) -> Vec<Point> {
|
||||
let mut out: Vec<Point> = c.to_vec();
|
||||
// Each side: the axis it cuts across (x or y) and whether it is the upper one.
|
||||
for (across_x, upper) in [(true, false), (true, true), (false, false), (false, true)] {
|
||||
let coord = |p: Point| if across_x { p.x } else { p.y };
|
||||
let limit = if upper { half } else { -half };
|
||||
let inside = |p: Point| if upper { coord(p) <= limit } else { coord(p) >= limit };
|
||||
let input = std::mem::take(&mut out);
|
||||
let Some(&last) = input.last() else { break };
|
||||
let mut prev = last;
|
||||
for &cur in &input {
|
||||
let (a, b) = (inside(prev), inside(cur));
|
||||
if a != b {
|
||||
let (pc, cc) = (coord(prev), coord(cur));
|
||||
let t = (limit - pc) / (cc - pc);
|
||||
out.push(prev.lerp(cur, t));
|
||||
}
|
||||
if b {
|
||||
out.push(cur);
|
||||
}
|
||||
prev = cur;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,187 @@
|
||||
use vectorcraft_geom::{BezPath, FillRule, PathData, Point, Rect, Shape};
|
||||
|
||||
use super::*;
|
||||
use crate::{Extrude, Inflate, Light, Lights, Revolve, View};
|
||||
|
||||
fn square(x: f64, y: f64, s: f64) -> PathData {
|
||||
let mut b = BezPath::new();
|
||||
b.move_to((x, y));
|
||||
b.line_to((x + s, y));
|
||||
b.line_to((x + s, y + s));
|
||||
b.line_to((x, y + s));
|
||||
b.close_path();
|
||||
PathData::from_bezpath(&b)
|
||||
}
|
||||
|
||||
/// The view straight on: no rotation, the default light.
|
||||
fn front() -> View {
|
||||
View { rotation_x: 0.0, rotation_y: 0.0, rotation_z: 0.0, ..View::default() }
|
||||
}
|
||||
|
||||
fn extruded(view: View, shadow: Shadow) -> Option<PathData> {
|
||||
let path = square(100.0, 100.0, 100.0);
|
||||
let mut caster = ShadowCaster::new();
|
||||
caster.extrude(&path, FillRule::NonZero, Rect::new(100.0, 100.0, 200.0, 200.0), Extrude::default(), view).unwrap();
|
||||
caster.path(shadow)
|
||||
}
|
||||
|
||||
/// The area the shadow covers, sampled on a grid over `r` with the nonzero rule.
|
||||
fn covered(path: &PathData, r: Rect, steps: usize) -> f64 {
|
||||
let bp = path.to_bezpath();
|
||||
let (dx, dy) = (r.width() / steps as f64, r.height() / steps as f64);
|
||||
let mut hits = 0;
|
||||
for i in 0..steps {
|
||||
for j in 0..steps {
|
||||
let p = Point::new(r.x0 + (i as f64 + 0.5) * dx, r.y0 + (j as f64 + 0.5) * dy);
|
||||
if bp.winding(p) != 0 {
|
||||
hits += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
hits as f64 * dx * dy
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_light_straight_in_front_casts_the_silhouette_behind_the_object() {
|
||||
let view = View { light_height: 90.0, ..front() };
|
||||
let path = extruded(view, Shadow::default()).expect("a shadow");
|
||||
let b = path.bounds().unwrap();
|
||||
for (got, want) in [(b.x0, 100.0), (b.y0, 100.0), (b.x1, 200.0), (b.y1, 200.0)] {
|
||||
assert!((got - want).abs() < 1e-6, "{b:?}");
|
||||
}
|
||||
// Every face of the box projects into the square, and the nonzero union fills it whole.
|
||||
let area = covered(&path, Rect::new(90.0, 90.0, 210.0, 210.0), 120);
|
||||
assert!((area - 10_000.0).abs() < 300.0, "{area}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_shadow_behind_falls_away_from_the_light_and_moves_with_the_distance() {
|
||||
// The default light is up and to the left, in front: the shadow falls down and to the right.
|
||||
let near = extruded(front(), Shadow::default()).unwrap().bounds().unwrap();
|
||||
assert!(near.x1 > 200.0 + 1.0 && near.y1 > 200.0 + 1.0, "{near:?}");
|
||||
assert!(near.x0 >= 100.0 - 1e-6 && near.y0 >= 100.0 - 1e-6, "{near:?}");
|
||||
let far = extruded(front(), Shadow { distance: 1.0, ..Shadow::default() }).unwrap().bounds().unwrap();
|
||||
assert!(far.x1 > near.x1 + 10.0 && far.y1 > near.y1 + 10.0, "{near:?} {far:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_light_behind_the_object_casts_no_shadow_behind_it() {
|
||||
assert!(extruded(View { light_behind: true, ..front() }, Shadow::default()).is_none());
|
||||
let behind = Light { behind: true, ..Light::default() };
|
||||
let lights = Lights::new(&[behind, Light::default()]).unwrap();
|
||||
// The first light casts the shadow.
|
||||
assert!(extruded(View { lights: Some(lights), ..front() }, Shadow::default()).is_none());
|
||||
let lights = Lights::new(&[Light::default(), behind]).unwrap();
|
||||
assert!(extruded(View { lights: Some(lights), ..front() }, Shadow::default()).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_light_that_lights_nothing_casts_no_shadow() {
|
||||
assert!(extruded(View { light_intensity: 0.0, ..front() }, Shadow::default()).is_none());
|
||||
assert!(extruded(View { shade: false, ..front() }, Shadow::default()).is_none());
|
||||
let dark = Light { intensity: 0.0, ..Light::default() };
|
||||
let lights = Lights::new(&[dark, Light::default()]).unwrap();
|
||||
assert!(extruded(View { lights: Some(lights), ..front() }, Shadow::default()).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_shadow_below_lies_under_the_object_on_its_floor() {
|
||||
let below = Shadow { position: ShadowPosition::Below, ..Shadow::default() };
|
||||
// Seen level, the floor is edge-on: nothing to draw.
|
||||
assert!(extruded(front(), below).is_none());
|
||||
// Seen from above (the effects' default Off-Axis Front view), the floor shows.
|
||||
let off_axis = View { rotation_x: -21.88, rotation_y: -23.4, rotation_z: 9.06, ..View::default() };
|
||||
assert!(extruded(off_axis, below).is_some());
|
||||
// Seen from straight above, the floor is square to the view, under the object: with the light
|
||||
// straight in front too, the shadow is the object's outline, as the one behind it is.
|
||||
let top = View { rotation_x: -90.0, light_height: 90.0, ..front() };
|
||||
let under = extruded(top, below).unwrap().bounds().unwrap();
|
||||
let back = extruded(top, Shadow::default()).unwrap().bounds().unwrap();
|
||||
for (a, b) in [(under.x0, back.x0), (under.y0, back.y0), (under.x1, back.x1), (under.y1, back.y1)] {
|
||||
assert!((a - b).abs() < 1e-6, "{under:?} {back:?}");
|
||||
}
|
||||
// A light from below the floor casts nothing on it.
|
||||
let from_below = View { light_rotation: -90.0, light_height: 0.0, ..off_axis };
|
||||
assert!(extruded(from_below, below).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shadow_bounds_limit_the_plane_the_shadow_falls_on() {
|
||||
// A low light casts a long shadow; the plane cuts it.
|
||||
let low = View { light_height: 5.0, ..front() };
|
||||
let small = extruded(low, Shadow { bounds: 0.1, ..Shadow::default() }).unwrap().bounds().unwrap();
|
||||
let large = extruded(low, Shadow { bounds: 4.0, ..Shadow::default() }).unwrap().bounds().unwrap();
|
||||
// At 10 % the square reaches 0.2 × the size (100) from the foot of the center (150, 150).
|
||||
for v in [small.x0, small.y0, small.x1, small.y1] {
|
||||
assert!((130.0 - 1e-6..=170.0 + 1e-6).contains(&v), "{small:?}");
|
||||
}
|
||||
assert!(large.width() > small.width() * 5.0, "{small:?} {large:?}");
|
||||
for v in [large.x0, large.y0, large.x1, large.y1] {
|
||||
assert!((-650.0 - 1e-6..=950.0 + 1e-6).contains(&v), "{large:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_settings_are_sanitized_and_an_empty_caster_casts_nothing() {
|
||||
assert!(ShadowCaster::new().path(Shadow::default()).is_none());
|
||||
let wild = Shadow { distance: f64::NAN, bounds: f64::INFINITY, ..Shadow::default() };
|
||||
assert_eq!(wild.sanitized(), Shadow { bounds: 1.0, ..Shadow::default() });
|
||||
assert_eq!(Shadow { distance: -3.0, bounds: 99.0, ..Shadow::default() }.sanitized(), Shadow { bounds: 4.0, ..Shadow::default() });
|
||||
assert_eq!(ShadowPosition::parse("below"), Some(ShadowPosition::Below));
|
||||
assert_eq!(ShadowPosition::parse("under"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_faces_one_shadow_is_cast_from_are_bounded() {
|
||||
// An open profile of 500 points revolves into about 64,000 faces at 128 segments: five fit,
|
||||
// the sixth does not.
|
||||
let mut b = BezPath::new();
|
||||
b.move_to((110.0, 0.0));
|
||||
for i in 1..500 {
|
||||
b.line_to((110.0 + f64::from(i % 2) * 5.0, f64::from(i)));
|
||||
}
|
||||
let path = PathData::from_bezpath(&b);
|
||||
let bounds = path.bounds().unwrap();
|
||||
let options = Revolve { segments: 128, offset: 10.0, ..Revolve::default() };
|
||||
let mut caster = ShadowCaster::new();
|
||||
for _ in 0..5 {
|
||||
let faces = caster.revolve(&path, FillRule::NonZero, bounds, options).unwrap();
|
||||
assert!(faces.len() > 60_000, "{faces:?}");
|
||||
}
|
||||
let err = caster.revolve(&path, FillRule::NonZero, bounds, options).unwrap_err();
|
||||
assert!(err.contains("too complex"), "{err}");
|
||||
assert!(caster.len() <= MAX_SHADOW_FACES);
|
||||
assert!(caster.path(Shadow::default()).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_small_solid_beside_a_huge_one_keeps_its_shadow() {
|
||||
let bounds = Rect::new(0.0, 0.0, 1_000_010.0, 10.0);
|
||||
let view = View { light_height: 90.0, ..front() };
|
||||
let mut caster = ShadowCaster::new();
|
||||
let small = caster.extrude(&square(0.0, 0.0, 10.0), FillRule::NonZero, bounds, Extrude::default(), view).unwrap();
|
||||
let huge = caster.extrude(&square(1_000_000.0, 0.0, 10.0), FillRule::NonZero, bounds, Extrude::default(), view).unwrap();
|
||||
let all = [small.clone(), huge];
|
||||
let plane = caster.shadow_plane(Shadow { bounds: 4.0, ..Shadow::default() }, &all).unwrap();
|
||||
let path = caster.path_on(&plane, std::slice::from_ref(&small)).expect("the small solid's shadow");
|
||||
let b = path.bounds().unwrap();
|
||||
assert!((b.width() - 10.0).abs() < 1e-3 && (b.height() - 10.0).abs() < 1e-3, "{b:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_effect_casts_a_shadow() {
|
||||
let path = square(100.0, 100.0, 100.0);
|
||||
let bounds = Rect::new(100.0, 100.0, 200.0, 200.0);
|
||||
let mut plane = ShadowCaster::new();
|
||||
plane.plane(&path, FillRule::NonZero, bounds, View::default()).unwrap();
|
||||
assert!(plane.path(Shadow::default()).is_some());
|
||||
let mut inflate = ShadowCaster::new();
|
||||
let results = inflate.inflate(&[(&path, FillRule::NonZero)], &[true], bounds, Inflate::default(), View::default());
|
||||
assert!(results.iter().all(Result::is_ok));
|
||||
assert!(inflate.path(Shadow::default()).is_some());
|
||||
let mut revolve = ShadowCaster::new();
|
||||
revolve.revolve(&path, FillRule::NonZero, bounds, Revolve::default()).unwrap();
|
||||
let b = revolve.path(Shadow::default()).unwrap().bounds().unwrap();
|
||||
// The surface turns about the square's left edge, reaching 100 to either side of it.
|
||||
assert!(b.x1 > 100.0 && b.x0 < 100.0 + 1e-6, "{b:?}");
|
||||
}
|
||||
@@ -257,6 +257,12 @@ pub struct Facet {
|
||||
/// The facet's outline with the profile's curves kept as cubics, for [`Facet::path`]. The
|
||||
/// contours stay the polygon that visibility and depth use.
|
||||
pub outline: Option<PathData>,
|
||||
/// The facet is a piece of the solid's sides (Extrude & Bevel's walls and bevels, Inflate's
|
||||
/// sides) rather than of its ends (the caps, a plane, Inflate's front and back).
|
||||
pub side: bool,
|
||||
/// The facet is turned away from the viewer: a closed solid keeps it (seen edge-on, or with
|
||||
/// Draw Hidden Faces) and draws it behind the facets turned toward the viewer.
|
||||
pub away: bool,
|
||||
/// The contours in view space, before projection, for tests that measure depths.
|
||||
#[cfg(test)]
|
||||
view: Vec<Vec<V3>>,
|
||||
|
||||
@@ -9,6 +9,7 @@ use super::{
|
||||
Bevel, Extrude, Facet, FillRule, MAX_FACES, MAX_SOLID_FACES, ParamCurve, PathData, Point, Rect, Revolve, V3, View, eye_distance,
|
||||
perspective_scale,
|
||||
};
|
||||
use crate::shadow::Projection;
|
||||
|
||||
/// Turns 3D points into document points and shades faces for one view.
|
||||
pub(super) struct Camera {
|
||||
@@ -48,14 +49,19 @@ impl Camera {
|
||||
pub(super) fn brightness(&self, n: V3) -> f64 {
|
||||
crate::brightness(&self.view, n, self.light)
|
||||
}
|
||||
/// The rotation and projection this camera draws with, for a shadow cast from its faces.
|
||||
pub(super) fn projection(&self) -> Projection {
|
||||
Projection { view: self.view, origin: self.origin, eye: self.eye }
|
||||
}
|
||||
}
|
||||
|
||||
/// A face of a solid before projection: its contours in view space (holes running the other way)
|
||||
/// 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.
|
||||
pub(crate) type Built = (Vec<Facet>, Vec<Face3>);
|
||||
/// 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>);
|
||||
|
||||
/// Facets being collected for one solid.
|
||||
pub(super) struct Builder<'a> {
|
||||
@@ -142,6 +148,8 @@ impl Builder<'_> {
|
||||
normal: [n.0, n.1, n.2],
|
||||
rgb_gradient: None,
|
||||
outline: None,
|
||||
side: false,
|
||||
away: behind,
|
||||
#[cfg(test)]
|
||||
view,
|
||||
});
|
||||
@@ -221,6 +229,11 @@ impl Builder<'_> {
|
||||
}
|
||||
let before = self.facets.len();
|
||||
self.face(vec![quad], normal, order)?;
|
||||
if self.facets.len() > before
|
||||
&& let Some(f) = self.facets.last_mut()
|
||||
{
|
||||
f.side = true;
|
||||
}
|
||||
if let Some(w) = curves.as_mut().filter(|_| pieces == 1)
|
||||
&& let Some(span) = w.spans.get(i).copied().flatten()
|
||||
{
|
||||
@@ -250,7 +263,7 @@ impl Builder<'_> {
|
||||
/// 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())
|
||||
(self.facets, self.mesh.unwrap_or_default(), Some(self.camera.projection()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,7 +505,7 @@ 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, false).map(|(facets, ..)| facets)
|
||||
}
|
||||
|
||||
/// [`extrude`], or with `mesh` the solid's faces before projection instead of its facets.
|
||||
@@ -602,7 +615,7 @@ 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, false).map(|(facets, ..)| facets)
|
||||
}
|
||||
|
||||
/// [`plane`], or with `mesh` its face before projection instead of its facet.
|
||||
|
||||
@@ -62,7 +62,7 @@ pub fn inflate(path: &PathData, rule: FillRule, bounds: Rect, options: Inflate,
|
||||
/// on its own over the grid of `bounds`, and one eye sees them all, as far away as the highest
|
||||
/// puff needs with Perspective. One result for each shape, in order.
|
||||
pub fn inflate_together(shapes: &[(&PathData, FillRule)], bounds: Rect, options: Inflate, view: View) -> Vec<Result<Vec<Facet>, String>> {
|
||||
inflate_faces(shapes, bounds, options, view, false).map(|r| r.map(|(facets, _)| facets)).collect()
|
||||
inflate_faces(shapes, bounds, options, view, false).map(|r| r.map(|(facets, ..)| facets)).collect()
|
||||
}
|
||||
|
||||
/// [`inflate_together`], or with `mesh` each solid's faces before projection instead of its facets.
|
||||
@@ -81,7 +81,7 @@ pub(crate) fn inflate_faces(
|
||||
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![])),
|
||||
Ok(None) => Ok((vec![], vec![], None)),
|
||||
Err(e) => Err(e),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -98,6 +98,33 @@ fn hidden_faces_draw_every_face_of_the_solid_behind_the_visible_ones() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Each facet says whether it is a piece of the solid's sides and whether it is turned away from
|
||||
/// the viewer: a box's walls and bevels are sides and its two caps are not; Inflate's sides are
|
||||
/// sides and its front is not.
|
||||
#[test]
|
||||
fn facets_tell_the_sides_from_the_ends_and_the_faces_turned_away() {
|
||||
let (p, b) = shape(&square(0.0, 0.0, 100.0));
|
||||
let v = View { rotation_x: 20.0, rotation_y: 30.0, ..front() };
|
||||
let bevel = Bevel { width: 10.0, height: 10.0, ..Bevel::default() };
|
||||
for o in [Extrude { hidden: true, ..Extrude::default() }, Extrude { hidden: true, bevel: Some(bevel), ..Extrude::default() }] {
|
||||
let all = extrude(&p, FillRule::EvenOdd, b, o, v).unwrap();
|
||||
let ends: Vec<&Facet> = all.iter().filter(|f| !f.side).collect();
|
||||
let [a, c] = ends[..] else { panic!("two caps among {} facets", all.len()) };
|
||||
let along = a.normal[0] * c.normal[0] + a.normal[1] * c.normal[1] + a.normal[2] * c.normal[2];
|
||||
assert!(close(along, -1.0, 1e-9), "the caps face opposite ways: {along}");
|
||||
assert!(all.len() > 6 || o.bevel.is_none(), "the bevel adds sides");
|
||||
// The faces turned away are drawn first, the others after them.
|
||||
let away = all.iter().take_while(|f| f.away).count();
|
||||
assert!(away > 0 && all[away..].iter().all(|f| !f.away), "{away} of {}", all.len());
|
||||
}
|
||||
let shown = extrude(&p, FillRule::EvenOdd, b, Extrude::default(), v).unwrap();
|
||||
assert!(shown.iter().all(|f| !f.away));
|
||||
let puff = inflate(&p, FillRule::EvenOdd, b, Inflate { depth: 20.0, ..Inflate::default() }, v).unwrap();
|
||||
assert!(puff.iter().any(|f| f.side) && puff.iter().any(|f| !f.side));
|
||||
let flat = inflate(&p, FillRule::EvenOdd, b, Inflate::default(), v).unwrap();
|
||||
assert!(!flat.is_empty() && flat.iter().all(|f| !f.side));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn taper_shrinks_the_back_and_twist_turns_it() {
|
||||
let (p, b) = shape(&square(0.0, 0.0, 100.0));
|
||||
|
||||
+130
-24
@@ -11,19 +11,20 @@
|
||||
//! The selection keeps its bounding box and its Live Shape widgets on show with the Rectangle,
|
||||
//! Rounded Rectangle, Ellipse, Polygon and Star tools, so a shape just drawn can be adjusted at
|
||||
//! once: a drag on a live rectangle's or polygon's corner widget rounds its corners and a drag on
|
||||
//! a live ellipse's pie widget sets its pie angle, as with the Selection tool; a press on the
|
||||
//! selection's centre point (Show Center) moves it, and one just outside a corner of its box turns
|
||||
//! it about that centre, as the Selection tool's rotate zone does; a press anywhere else draws a
|
||||
//! new shape (#5477).
|
||||
//! a live ellipse's pie widget sets its pie angle, as with the Selection tool; a drag on one of the
|
||||
//! box's eight handles scales the selection, a press on the selection's centre point (Show Center)
|
||||
//! moves it, and one just outside a corner of its box turns it about that centre, as the Selection
|
||||
//! tool's handles, centre point and rotate zone do; a press anywhere else draws a new shape (#5477).
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::OrientedBox;
|
||||
use vectorcraft_geom::{Affine, Point, Rect, Vec2};
|
||||
|
||||
use crate::bbox::{move_delta, rotate_for_drag};
|
||||
use crate::bbox::{Handle, move_delta, rotate_for_drag, scale_for_drag};
|
||||
use crate::corners::{self, CornerDrag};
|
||||
use crate::guides::{DrawSnap, Leave, square};
|
||||
use crate::guides::{DrawSnap, Leave, PointSnap, Targets, square};
|
||||
use crate::pie::{self, PieDrag};
|
||||
use crate::select::{MoveSnap, hit_center, matrix_json, rotate_zone, shown_box};
|
||||
use crate::select::{MoveSnap, handle_cursor, hit_center, is_area_type, matrix_json, rotate_zone, shown_box};
|
||||
use crate::{Action, Cursor, Mods, Overlay, PointerEvent, PointerKind, Tool, ToolContext, ToolKey};
|
||||
|
||||
/// The largest corner radius the Rounded Rectangle tool draws, points: the range of General ›
|
||||
@@ -31,6 +32,22 @@ use crate::{Action, Cursor, Mods, Overlay, PointerEvent, PointerKind, Tool, Tool
|
||||
/// value stop there too.
|
||||
pub const MAX_CORNER_RADIUS: f64 = 1296.0;
|
||||
|
||||
/// How far (screen points, along each axis) from a bounding-box handle's centre a shape tool's
|
||||
/// press scales the selection: half the drawn handle square, which is 6 points wide at the default
|
||||
/// Selection & Anchor Display › Size 3 and a point wider or narrower per step. The Selection tool
|
||||
/// takes 5 points.
|
||||
fn handle_reach(cx: &ToolContext) -> f64 {
|
||||
(6.0 + f64::from(cx.anchor_size.clamp(1, 7)) - 3.0) / 2.0
|
||||
}
|
||||
|
||||
/// A document offset `d` in screen points: across and down the window.
|
||||
fn screen_offset(cx: &ToolContext, d: Vec2) -> (f64, f64) {
|
||||
match cx.screen {
|
||||
Some(s) if s.right.hypot2() > 0.0 && s.down.hypot2() > 0.0 => (d.dot(s.right) / s.right.hypot2(), d.dot(s.down) / s.down.hypot2()),
|
||||
_ => (d.x * cx.zoom, d.y * cx.zoom),
|
||||
}
|
||||
}
|
||||
|
||||
/// Does tool `id` keep the selection's bounding box and Live Shape widgets on show and let its
|
||||
/// corner and pie widgets be dragged (#5477)? The shape tools that draw a box: not Line Segment.
|
||||
pub fn shows_shape_widgets(id: &str) -> bool {
|
||||
@@ -43,9 +60,15 @@ enum WidgetDrag {
|
||||
Pie(PieDrag),
|
||||
}
|
||||
|
||||
/// A drag of the selection itself, taken by a shape tool: the centre point moves it and the zone
|
||||
/// just outside a corner of its box turns it, as the Selection tool's drags do (#5477).
|
||||
/// A drag of the selection itself, taken by a shape tool: a handle of its box scales it, the centre
|
||||
/// point moves it and the zone just outside a corner of its box turns it, as the Selection tool's
|
||||
/// drags do (#5477).
|
||||
enum SelectionDrag {
|
||||
/// Scaling the selection by `handle` of its box `bx`, as it was when the press came. `areas`:
|
||||
/// area type is selected, and its frame resizes while its type keeps its size. `snap` holds
|
||||
/// what the moved handle snaps to. `last`: where the last drag was, and whether Backtick was
|
||||
/// down then.
|
||||
Scale { handle: Handle, bx: OrientedBox, areas: bool, snap: Box<PointSnap>, last: Option<(Point, bool)> },
|
||||
/// Moving the selection by the centre point `hold`: `start` is where the press was, and the
|
||||
/// move has begun once the pointer left the drag threshold.
|
||||
Move { start: Point, hold: Point, began: bool, snap: Box<MoveSnap> },
|
||||
@@ -178,6 +201,22 @@ impl ShapeTool {
|
||||
rotate_zone(cx, &self.box_of(cx)?, p)
|
||||
}
|
||||
|
||||
/// The handle of the selection's box under `p`, with the box: a press there scales the
|
||||
/// selection, as with the Selection tool. A shape tool takes only the drawn handle square
|
||||
/// ([`handle_reach`]), which stays square to the screen when the box or the view is turned, so
|
||||
/// a press just beside a handle still draws a new shape.
|
||||
fn over_handle(&self, cx: &ToolContext, p: Point) -> Option<(Handle, OrientedBox)> {
|
||||
let bx = self.box_of(cx)?;
|
||||
let reach = handle_reach(cx);
|
||||
Handle::ALL
|
||||
.into_iter()
|
||||
.find(|h| {
|
||||
let (x, y) = screen_offset(cx, p - bx.to_doc() * h.pos(bx.rect));
|
||||
x.abs() <= reach && y.abs() <= reach
|
||||
})
|
||||
.map(|h| (h, bx))
|
||||
}
|
||||
|
||||
/// Is `p` over the centre point of a selected object (Attributes › Show Center), where a press
|
||||
/// moves the selection? The point shows whatever View › Show Bounding Box says, so the press
|
||||
/// takes it too.
|
||||
@@ -185,22 +224,35 @@ impl ShapeTool {
|
||||
shows_shape_widgets(self.id) && hit_center(cx, p, cx.point_tol()).is_some()
|
||||
}
|
||||
|
||||
/// A press on the selection's centre point or just outside a corner of its box: the drag moves
|
||||
/// or turns the selection instead of drawing a new shape (#5477). A turn begins with the press,
|
||||
/// as the Selection tool's does; a move begins once the pointer leaves the drag threshold.
|
||||
/// A press on a handle of the selection's box, on its centre point or just outside a corner of
|
||||
/// its box: the drag scales, moves or turns the selection instead of drawing a new shape
|
||||
/// (#5477). The handles come first, as with the Selection tool. A scale or a turn begins with
|
||||
/// the press, as the Selection tool's do; a move begins once the pointer leaves the drag
|
||||
/// threshold.
|
||||
fn press_selection(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Option<Vec<Action>> {
|
||||
if !shows_shape_widgets(self.id) {
|
||||
return None;
|
||||
}
|
||||
self.selection = match hit_center(cx, ev.pos, cx.point_tol()) {
|
||||
Some((_, hold)) => Some(SelectionDrag::Move { start: ev.pos, hold, began: false, snap: Box::new(MoveSnap::new(cx)) }),
|
||||
None => self.over_rotate(cx, ev.pos).map(|center| SelectionDrag::Rotate { center, start: ev.pos }),
|
||||
self.selection = if let Some((handle, bx)) = self.over_handle(cx, ev.pos) {
|
||||
let is_area = |id: &vectorcraft_doc::NodeId| cx.doc.node(*id).is_some_and(is_area_type);
|
||||
let areas = cx.selection.objects.iter().any(is_area);
|
||||
Some(SelectionDrag::Scale { handle, bx, areas, snap: Box::new(PointSnap::new(cx, || Targets::for_scale(cx))), last: None })
|
||||
} else {
|
||||
match hit_center(cx, ev.pos, cx.point_tol()) {
|
||||
Some((_, hold)) => Some(SelectionDrag::Move { start: ev.pos, hold, began: false, snap: Box::new(MoveSnap::new(cx)) }),
|
||||
None => self.over_rotate(cx, ev.pos).map(|center| SelectionDrag::Rotate { center, start: ev.pos }),
|
||||
}
|
||||
};
|
||||
self.selection.as_ref()?;
|
||||
self.snap.clear();
|
||||
self.guides.clear();
|
||||
self.measure = None;
|
||||
Some(match self.selection {
|
||||
// Only area type: the step resizes type areas; anything else scales.
|
||||
Some(SelectionDrag::Scale { areas, .. }) => {
|
||||
let only_areas = areas && cx.selection.objects.iter().all(|id| cx.doc.node(*id).is_some_and(is_area_type));
|
||||
vec![Action::Begin(if only_areas { "Resize Type Area" } else { "Scale" }.into())]
|
||||
}
|
||||
Some(SelectionDrag::Rotate { .. }) => vec![Action::Begin("Rotate".into())],
|
||||
_ => vec![],
|
||||
})
|
||||
@@ -235,6 +287,32 @@ impl ShapeTool {
|
||||
self.measure = cx.transform_tools_guides.then(|| (ev.pos, format!("{:.1}°", -deg)));
|
||||
vec![Action::Preview("object.transform".into(), json!({"matrix": matrix_json(a), "copy": false}))]
|
||||
}
|
||||
(PointerKind::Drag, SelectionDrag::Scale { handle, bx, areas, snap, last }) => {
|
||||
// As the Selection tool scales: in the box's own frame, the moved handle snapped,
|
||||
// and with Use Preview Bounds by the visual box's size unless the drag flips it.
|
||||
*last = Some((ev.pos, ev.backquote));
|
||||
let m = ev.mods;
|
||||
let a = scale_for_drag(bx.rect, *handle, bx.to_local(ev.pos), m.shift, m.alt);
|
||||
let (a, guides) = snap.snap_scale(cx, bx, *handle, a, m.shift, m.alt);
|
||||
self.guides = guides;
|
||||
let nr = a.transform_rect_bbox(bx.rect);
|
||||
self.measure = cx.transform_tools_guides.then(|| (ev.pos, cx.size_label(nr.width(), nr.height())));
|
||||
let [sx, _, _, sy, _, _] = a.as_coeffs();
|
||||
let (cmd, mut params) = if !ev.backquote && cx.preview_bounds && sx > 0.0 && sy > 0.0 {
|
||||
let reference = if m.alt { 4 } else { handle.opposite().reference() };
|
||||
("object.setBounds", json!({ "width": nr.width(), "height": nr.height(), "reference": reference, "copy": false }))
|
||||
} else {
|
||||
("object.transform", json!({ "matrix": matrix_json(bx.conjugate(a)), "copy": false }))
|
||||
};
|
||||
if *areas {
|
||||
params["typeAreas"] = json!(true);
|
||||
}
|
||||
if ev.backquote {
|
||||
params["objects"] = json!(false);
|
||||
params["patterns"] = json!(true);
|
||||
}
|
||||
vec![Action::Preview(cmd.into(), params)]
|
||||
}
|
||||
(PointerKind::Up, SelectionDrag::Move { began, .. }) => {
|
||||
let began = *began;
|
||||
self.selection = None;
|
||||
@@ -243,8 +321,22 @@ impl ShapeTool {
|
||||
self.snapped = false;
|
||||
if began { vec![Action::Commit] } else { vec![] }
|
||||
}
|
||||
(PointerKind::Up, SelectionDrag::Scale { last, .. }) => {
|
||||
// Backtick pressed or let go after the last drag: the scale is previewed again at
|
||||
// that drag's position with the key as it is at the release, as the Selection tool
|
||||
// does.
|
||||
let again = last.filter(|(_, backquote)| *backquote != ev.backquote).map(|(pos, _)| pos);
|
||||
let mut out =
|
||||
again.and_then(|pos| self.selection_pointer(cx, &PointerEvent { kind: PointerKind::Drag, pos, ..*ev })).unwrap_or_default();
|
||||
self.selection = None;
|
||||
self.guides.clear();
|
||||
self.measure = None;
|
||||
out.push(Action::Commit);
|
||||
out
|
||||
}
|
||||
(PointerKind::Up, SelectionDrag::Rotate { .. }) => {
|
||||
self.selection = None;
|
||||
self.guides.clear();
|
||||
self.measure = None;
|
||||
vec![Action::Commit]
|
||||
}
|
||||
@@ -422,8 +514,9 @@ impl Tool for ShapeTool {
|
||||
fn busy(&self) -> bool {
|
||||
self.start.is_some() || self.widget.is_some() || self.selection.is_some()
|
||||
}
|
||||
/// A widget or selection drag takes the pointer; otherwise a press draws, unless it lands on
|
||||
/// the selection's centre point or in the rotate zone just outside its box's corner.
|
||||
/// A widget or selection drag takes the pointer; otherwise a press draws, unless it lands on a
|
||||
/// handle of the selection's box, its centre point or the rotate zone just outside its box's
|
||||
/// corner.
|
||||
fn pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {
|
||||
if let Some(out) = self.widget_pointer(cx, ev) {
|
||||
return out;
|
||||
@@ -434,7 +527,11 @@ impl Tool for ShapeTool {
|
||||
match ev.kind {
|
||||
PointerKind::Move => {
|
||||
// Over a widget a press would drag, no Smart Guide for a new shape's start.
|
||||
if self.over_widget(cx, ev.pos) || self.over_center(cx, ev.pos) || self.over_rotate(cx, ev.pos).is_some() {
|
||||
if self.over_widget(cx, ev.pos)
|
||||
|| self.over_handle(cx, ev.pos).is_some()
|
||||
|| self.over_center(cx, ev.pos)
|
||||
|| self.over_rotate(cx, ev.pos).is_some()
|
||||
{
|
||||
self.snap.clear();
|
||||
} else {
|
||||
self.snap.hover(cx, ev.pos, &[], None);
|
||||
@@ -566,6 +663,11 @@ impl Tool for ShapeTool {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
/// A drag of a handle of the selection's box: the canvas keeps drawing the box at the changing
|
||||
/// bounds, as for the Selection tool.
|
||||
fn resizing(&self) -> bool {
|
||||
matches!(self.selection, Some(SelectionDrag::Scale { .. }))
|
||||
}
|
||||
/// Adjusting a widget (the canvas hides the bounding box meanwhile, as for the Selection tool).
|
||||
fn transforming(&self) -> bool {
|
||||
match &self.widget {
|
||||
@@ -573,7 +675,7 @@ impl Tool for ShapeTool {
|
||||
Some(WidgetDrag::Pie(d)) => d.adjusting(),
|
||||
None => match &self.selection {
|
||||
Some(SelectionDrag::Move { began, .. }) => *began,
|
||||
Some(SelectionDrag::Rotate { .. }) => true,
|
||||
Some(SelectionDrag::Rotate { .. } | SelectionDrag::Scale { .. }) => true,
|
||||
None => false,
|
||||
},
|
||||
}
|
||||
@@ -630,9 +732,9 @@ impl Tool for ShapeTool {
|
||||
}
|
||||
o
|
||||
}
|
||||
/// The Selection tool's pointer over a Live Shape widget, the selection's centre point or the
|
||||
/// rotate zone just outside its box's corner; the crosshair everywhere else, where a press
|
||||
/// draws.
|
||||
/// The Selection tool's pointer over a Live Shape widget, a handle of the selection's box, its
|
||||
/// centre point or the rotate zone just outside its box's corner; the crosshair everywhere
|
||||
/// else, where a press draws.
|
||||
fn cursor(&self, cx: &ToolContext, p: Point, m: Mods) -> Cursor {
|
||||
// Over a widget a press would drag (or dragging one), the Selection tool's pointer for it.
|
||||
let idle = self.start.is_none();
|
||||
@@ -645,13 +747,14 @@ impl Tool for ShapeTool {
|
||||
// move snaps, as the Selection tool's.
|
||||
match &self.selection {
|
||||
Some(SelectionDrag::Rotate { center, .. }) => return Cursor::rotate_about_in_view(*center, p, cx.view_angle()),
|
||||
Some(SelectionDrag::Scale { handle, bx, .. }) => return handle_cursor(*handle, bx.angle - cx.view_angle()),
|
||||
Some(SelectionDrag::Move { began: true, .. }) if m.alt => return Cursor::SelectionCopy,
|
||||
Some(SelectionDrag::Move { began: true, .. }) => return if self.snapped { Cursor::ArrowHollow } else { Cursor::Arrow },
|
||||
Some(_) => return Cursor::Arrow,
|
||||
None => {}
|
||||
}
|
||||
// A press takes the corner and pie widgets first, then the centre point, then the zone
|
||||
// just outside a corner; everywhere else it draws a new shape.
|
||||
// A press takes the corner and pie widgets first, then the box's handles, then the centre
|
||||
// point, then the zone just outside a corner; everywhere else it draws a new shape.
|
||||
if idle {
|
||||
if shows_shape_widgets(self.id) && corners::over_widget(cx, p, false) {
|
||||
return Cursor::CornerRadius;
|
||||
@@ -659,6 +762,9 @@ impl Tool for ShapeTool {
|
||||
if self.over_widget(cx, p) {
|
||||
return Cursor::ArrowHollow;
|
||||
}
|
||||
if let Some((handle, bx)) = self.over_handle(cx, p) {
|
||||
return handle_cursor(handle, bx.angle - cx.view_angle());
|
||||
}
|
||||
if self.over_center(cx, p) {
|
||||
return Cursor::Move;
|
||||
}
|
||||
|
||||
@@ -583,6 +583,96 @@ fn a_shape_tool_moves_the_selection_by_its_centre_point_and_turns_it_from_a_corn
|
||||
assert_eq!(a.first(), Some(&Action::Begin("Rectangle".into())), "{a:?}");
|
||||
}
|
||||
|
||||
/// With a shape tool, a drag on one of the eight handles of the selection's bounding box scales the
|
||||
/// selection, as with the Selection tool, instead of drawing a new shape from the handle.
|
||||
#[test]
|
||||
fn a_shape_tool_scales_the_selection_by_its_bounding_box_handles() {
|
||||
let (d, s) = unfilled_live_rect();
|
||||
let p = paint();
|
||||
let c = cx(&d, &s, &p);
|
||||
let corner = Point::new(200.0, 200.0);
|
||||
for id in ["rectangle", "roundedRectangle", "polygon", "star", "ellipse"] {
|
||||
let mut t = ShapeTool::new(id);
|
||||
assert_eq!(t.cursor(&c, corner, Mods::default()), handle_cursor(Handle::BottomRight, 0.0), "{id}");
|
||||
// No Smart Guide for a new shape's start over a handle.
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Move, corner.x, corner.y));
|
||||
assert!(t.overlays(&c).is_empty(), "{id}");
|
||||
assert_eq!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, corner.x, corner.y)), vec![Action::Begin("Scale".into())], "{id}");
|
||||
// A resize: the canvas keeps the box on show at the changing bounds.
|
||||
assert!(t.busy() && t.transforming() && t.resizing(), "{id}");
|
||||
let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, 220.0, 230.0));
|
||||
let want = scale_for_drag(Rect::new(100.0, 100.0, 200.0, 200.0), Handle::BottomRight, Point::new(220.0, 230.0), false, false);
|
||||
assert_eq!(a, vec![Action::Preview("object.transform".into(), json!({"matrix": matrix_json(want), "copy": false}))], "{id}");
|
||||
assert_eq!(t.cursor(&c, Point::new(220.0, 230.0), Mods::default()), handle_cursor(Handle::BottomRight, 0.0), "{id}");
|
||||
assert_eq!(t.pointer(&c, &PointerEvent::new(PointerKind::Up, 220.0, 230.0)), vec![Action::Commit], "{id}");
|
||||
assert!(!t.busy() && !t.transforming() && !t.resizing(), "{id}");
|
||||
}
|
||||
// The side handles scale along one axis; Escape turns the scale back.
|
||||
let mut t = ShapeTool::new("rectangle");
|
||||
assert_eq!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, 150.0, 100.0)), vec![Action::Begin("Scale".into())]);
|
||||
let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, 150.0, 80.0));
|
||||
let want = scale_for_drag(Rect::new(100.0, 100.0, 200.0, 200.0), Handle::Top, Point::new(150.0, 80.0), false, false);
|
||||
assert_eq!(a, vec![Action::Preview("object.transform".into(), json!({"matrix": matrix_json(want), "copy": false}))]);
|
||||
assert_eq!(t.key(&c, ToolKey::Escape, Mods::default()), vec![Action::Cancel]);
|
||||
assert!(!t.busy());
|
||||
// Backtick pressed after the last drag: the release previews the scale again for the patterns
|
||||
// only, then commits.
|
||||
let mut t = ShapeTool::new("rectangle");
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, corner.x, corner.y));
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Drag, 220.0, 230.0));
|
||||
let a = t.pointer(&c, &PointerEvent { backquote: true, ..PointerEvent::new(PointerKind::Up, 220.0, 230.0) });
|
||||
let want = scale_for_drag(Rect::new(100.0, 100.0, 200.0, 200.0), Handle::BottomRight, Point::new(220.0, 230.0), false, false);
|
||||
let patterns = json!({"matrix": matrix_json(want), "copy": false, "objects": false, "patterns": true});
|
||||
assert_eq!(a, vec![Action::Preview("object.transform".into(), patterns), Action::Commit]);
|
||||
assert!(!t.busy());
|
||||
// View › Hide Bounding Box hides the handles, and a press there draws a new shape.
|
||||
let hidden = ToolContext { show_bbox: false, ..cx(&d, &s, &p) };
|
||||
let mut t = ShapeTool::new("rectangle");
|
||||
assert_eq!(t.cursor(&hidden, corner, Mods::default()), Cursor::Crosshair);
|
||||
t.pointer(&hidden, &PointerEvent::new(PointerKind::Down, corner.x, corner.y));
|
||||
let a = t.pointer(&hidden, &PointerEvent::new(PointerKind::Drag, 240.0, 240.0));
|
||||
assert_eq!(a.first(), Some(&Action::Begin("Rectangle".into())), "{a:?}");
|
||||
// The Line Segment tool shows no box, so its press draws.
|
||||
let mut t = ShapeTool::new("lineSegment");
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, corner.x, corner.y));
|
||||
let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, 240.0, 240.0));
|
||||
assert_eq!(a.first(), Some(&Action::Begin("Line".into())), "{a:?}");
|
||||
}
|
||||
|
||||
/// A shape tool's press scales the selection only on the drawn handle square: 3 screen points from
|
||||
/// the handle's centre along each axis at the default Anchor Size, at any zoom. A press 4 points
|
||||
/// from it draws a new shape; at Anchor Size 7 the square is 10 points wide and that press scales.
|
||||
#[test]
|
||||
fn a_shape_tool_draws_from_a_press_just_beside_a_handle() {
|
||||
let (d, s) = unfilled_live_rect();
|
||||
let p = paint();
|
||||
for zoom in [1.0, 4.0] {
|
||||
let c = ToolContext { zoom, ..cx(&d, &s, &p) };
|
||||
let on = Point::new(200.0 + 3.0 / zoom, 200.0 - 3.0 / zoom);
|
||||
let mut t = ShapeTool::new("rectangle");
|
||||
assert_eq!(t.cursor(&c, on, Mods::default()), handle_cursor(Handle::BottomRight, 0.0), "{zoom}");
|
||||
assert_eq!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, on.x, on.y)), vec![Action::Begin("Scale".into())], "{zoom}");
|
||||
let beside = Point::new(200.0 + 2.0 / zoom, 200.0 + 4.0 / zoom);
|
||||
let mut t = ShapeTool::new("rectangle");
|
||||
assert_eq!(t.cursor(&c, beside, Mods::default()), Cursor::Crosshair, "{zoom}");
|
||||
t.pointer(&c, &PointerEvent::new(PointerKind::Down, beside.x, beside.y));
|
||||
let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, 260.0, 260.0));
|
||||
assert_eq!(a.first(), Some(&Action::Begin("Rectangle".into())), "{zoom}: {a:?}");
|
||||
let big = ToolContext { anchor_size: 7, ..c };
|
||||
let mut t = ShapeTool::new("rectangle");
|
||||
assert_eq!(t.cursor(&big, beside, Mods::default()), handle_cursor(Handle::BottomRight, 0.0), "{zoom}");
|
||||
assert_eq!(t.pointer(&big, &PointerEvent::new(PointerKind::Down, beside.x, beside.y)), vec![Action::Begin("Scale".into())], "{zoom}");
|
||||
}
|
||||
// In a view turned 45°, the drawn square stays square to the screen: a press 3 screen points
|
||||
// right of and below the handle's centre, 4.24 pt along the box's x axis, scales.
|
||||
let screen = turned_view(45.0);
|
||||
let c = ToolContext { screen, ..cx(&d, &s, &p) };
|
||||
let f = screen.unwrap_or_else(|| panic!("a turned view"));
|
||||
let corner = Point::new(200.0, 200.0) + f.right * 3.0 + f.down * 3.0;
|
||||
let mut t = ShapeTool::new("rectangle");
|
||||
assert_eq!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, corner.x, corner.y)), vec![Action::Begin("Scale".into())]);
|
||||
}
|
||||
|
||||
/// Two selected live rectangles, the second placed so that its top-left Live Corners widget (10 pt
|
||||
/// in along the corner's bisector) lands on the first one's centre point.
|
||||
fn two_live_rects() -> (vectorcraft_doc::Document, Selection) {
|
||||
|
||||
@@ -7,6 +7,7 @@ pub mod compounds;
|
||||
pub mod contour;
|
||||
pub mod document;
|
||||
pub mod enclosing_lasso;
|
||||
pub mod image;
|
||||
pub mod pathtools;
|
||||
pub mod pen;
|
||||
pub mod select;
|
||||
@@ -26,6 +27,7 @@ pub fn plugin_menu() -> Vec<Item> {
|
||||
Item::Sub("Contour", contour::menu()),
|
||||
Item::Sub("Document Tools", document::menu()),
|
||||
Item::Sub("Enclosing Lasso", enclosing_lasso::menu()),
|
||||
Item::Sub("Image Tools", image::menu()),
|
||||
Item::Sub("Path Precision Tools", pathtools::menu()),
|
||||
Item::Sub("Selection Tools", select::menu()),
|
||||
Item::Sub("Smart Pen", pen::menu()),
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
//! Image Tools in the interface: its submenu (Object › Plug-ins › Image Tools). Remove Background's
|
||||
//! menu item opens the generic command dialog (`ui.paramDialog`) on its options, with a Preview
|
||||
//! (`dialogs::command`).
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
use crate::menus::Item;
|
||||
|
||||
/// Remove Background's command.
|
||||
pub const REMOVE_BACKGROUND: &str = "addon.image.removeBackground";
|
||||
/// Remove Background › Background: what the command's `background` takes, by name.
|
||||
pub const BACKGROUNDS: &[(&str, &str)] = &[("Edge Color", "edges"), ("Corner Color", "corners"), ("Picked Colors", "colors")];
|
||||
/// Remove Background › Match: what the command's `match` takes, by name.
|
||||
pub const MATCHES: &[(&str, &str)] = &[("Distance", "distance"), ("Each Channel", "channels")];
|
||||
|
||||
/// Object › Plug-ins › Image Tools.
|
||||
pub fn menu() -> Vec<Item> {
|
||||
vec![Item::Cmd(
|
||||
"Remove Background…",
|
||||
REMOVE_BACKGROUND,
|
||||
json!({"background": "edges", "colors": "#ffffff", "contiguous": true, "match": "distance", "tolerance": 10, "softness": 10}),
|
||||
)]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,111 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::NodeKind;
|
||||
use vectorcraft_engine::Session;
|
||||
|
||||
use crate::tests_labels::painted_text;
|
||||
use crate::{VectorcraftApp, menus};
|
||||
|
||||
/// An app with one image open and selected: 12 × 10 pixels, white, a near-white ring one pixel in
|
||||
/// (13.7 % from white) and a red block inside it.
|
||||
fn app() -> VectorcraftApp {
|
||||
let img = image::RgbaImage::from_fn(12, 10, |x, y| {
|
||||
image::Rgba(if (3..9).contains(&x) && (3..7).contains(&y) {
|
||||
[200, 0, 0, 255]
|
||||
} else if (1..11).contains(&x) && (1..9).contains(&y) && (x == 1 || x == 10 || y == 1 || y == 8) {
|
||||
[220, 220, 220, 255]
|
||||
} else {
|
||||
[255; 4]
|
||||
})
|
||||
});
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("document.open", json!({"name": "art.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
|
||||
app.run("select.all", json!({})).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
/// The selected image's pixel key and bytes.
|
||||
fn image(app: &VectorcraftApp) -> (String, Arc<Vec<u8>>) {
|
||||
let st = app.session.doc().unwrap();
|
||||
let Some(NodeKind::Image(im)) = st.selection.objects.first().and_then(|id| st.doc.node(*id)).map(|n| &n.kind) else { panic!("an image") };
|
||||
(im.key.clone(), st.doc.images[&im.key].bytes.clone())
|
||||
}
|
||||
|
||||
/// The alpha of the selected image's pixels at the corner, on the ring and in the block.
|
||||
fn alphas(app: &VectorcraftApp) -> [u8; 3] {
|
||||
let px = image::load_from_memory(&image(app).1).unwrap().to_rgba8();
|
||||
[(0, 0), (1, 1), (4, 4)].map(|(x, y)| px.get_pixel(x, y).0[3])
|
||||
}
|
||||
|
||||
fn open(app: &mut VectorcraftApp) {
|
||||
let entries = menus::menu_entries(app);
|
||||
let item = entries.iter().find(|e| e.path == ["Object", "Plug-ins", "Image Tools"] && e.label == "Remove Background…").unwrap();
|
||||
assert!(item.enabled);
|
||||
let (id, p) = menus::click_target(&item.label, item.command.as_deref().unwrap(), &item.params);
|
||||
menus::invoke(app, &id, p);
|
||||
}
|
||||
|
||||
fn show(app: &mut VectorcraftApp) -> String {
|
||||
painted_text(app, |app, ui| crate::dialogs::show(app, ui.ctx()))
|
||||
}
|
||||
|
||||
fn set(app: &mut VectorcraftApp, key: &str, v: serde_json::Value) {
|
||||
crate::dialogs::set_field(app.ui.dialog.as_mut().unwrap(), key.into(), v);
|
||||
}
|
||||
|
||||
/// Object › Plug-ins › Image Tools › Remove Background… opens the command dialog on Edge Color
|
||||
/// with Preview off. With Preview on the canvas shows the result for the values in the dialog,
|
||||
/// each change worked out again from the image as it was; Cancel puts the image back with no step
|
||||
/// of history, and OK keeps the result for the values in the dialog as one step.
|
||||
#[test]
|
||||
fn remove_background_previews_cancels_and_runs_on_ok() {
|
||||
let mut app = app();
|
||||
let steps = |app: &VectorcraftApp| app.session.doc().unwrap().history.undo.len();
|
||||
let (start, n) = (image(&app), steps(&app));
|
||||
|
||||
open(&mut app);
|
||||
let d = app.ui.dialog.as_ref().expect("a dialog opens");
|
||||
assert_eq!((d.str("__command"), d.str("__label")), (super::REMOVE_BACKGROUND.to_string(), "Remove Background".to_string()));
|
||||
assert_eq!((d.str("background"), d.bool("preview")), ("edges".to_string(), false));
|
||||
assert!(crate::dialogs::accepts_field(d, "preview"));
|
||||
assert_eq!((d.bool("contiguous"), d.str("match")), (true, "distance".to_string()));
|
||||
let shown = show(&mut app);
|
||||
for s in ["Remove Background", "Edge Color", "Contiguous", "Match", "Distance", "Tolerance", "Softness", "Preview"] {
|
||||
assert!(shown.contains(s), "{s} in {shown}");
|
||||
}
|
||||
assert_eq!(image(&app), start, "no preview without the checkbox");
|
||||
|
||||
// Preview on: at tolerance 10 the ring fades; at 20 it is cleared; at 0, with no softness,
|
||||
// only the white is cleared and the ring is whole again.
|
||||
set(&mut app, "preview", json!(true));
|
||||
show(&mut app);
|
||||
let [corner, ring, block] = alphas(&app);
|
||||
assert!(corner == 0 && ring > 0 && ring < 255 && block == 255, "{:?}", alphas(&app));
|
||||
set(&mut app, "tolerance", json!(20));
|
||||
show(&mut app);
|
||||
assert_eq!(alphas(&app), [0, 0, 255]);
|
||||
set(&mut app, "tolerance", json!(0));
|
||||
set(&mut app, "softness", json!(0));
|
||||
show(&mut app);
|
||||
assert_eq!(alphas(&app), [0, 255, 255], "worked out again from the image as it was");
|
||||
|
||||
// Cancel: as it was, with no step of history.
|
||||
crate::dialogs::cancel(&mut app);
|
||||
assert_eq!((image(&app), steps(&app)), (start.clone(), n), "Cancel restores the image and adds no undo step");
|
||||
assert!(!app.session.in_interaction());
|
||||
|
||||
// OK: the values in the dialog, as one step.
|
||||
open(&mut app);
|
||||
set(&mut app, "preview", json!(true));
|
||||
set(&mut app, "tolerance", json!(20));
|
||||
show(&mut app);
|
||||
crate::dialogs::confirm(&mut app).unwrap();
|
||||
assert!(app.ui.dialog.is_none());
|
||||
assert_eq!(steps(&app), n + 1, "one undo step");
|
||||
assert_eq!(alphas(&app), [0, 0, 255]);
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(image(&app), start);
|
||||
}
|
||||
@@ -123,7 +123,7 @@ pub(super) fn selection_overlay(app: &mut VectorcraftApp, p: &egui::Painter, xf:
|
||||
// shows while the key is down, where its handles take that press.
|
||||
let held = super::drag::mods(p.ctx().input(|i| i.modifiers), false);
|
||||
let lends_selection = !app.session.tool_busy() && app.session.lent_tool_id(&held) == Some("selection");
|
||||
let show_box = (app.session.tool_id() == "selection" || lends_selection || (shape_tool && !app.session.tool_busy()))
|
||||
let show_box = (app.session.tool_id() == "selection" || lends_selection || (shape_tool && (!app.session.tool_busy() || resizing)))
|
||||
&& (resizing || !app.session.tool_transforming() || moved.is_some())
|
||||
&& app.ui.view.bounding_box
|
||||
&& app.session.active().is_some_and(|st| !st.selection.is_empty() && st.selection.anchors.is_empty());
|
||||
|
||||
@@ -1173,6 +1173,56 @@ fn a_click_just_outside_the_canvas_does_not_reach_the_tool() {
|
||||
assert_eq!(selected(&app), vec![vectorcraft_doc::NodeId(id)]);
|
||||
}
|
||||
|
||||
/// The box-drawing shape tools show the bounding box of the shape just drawn (#5477). A drag of
|
||||
/// its bottom-right handle with the tool still active scales that shape, as with the Selection
|
||||
/// tool, rather than drawing a second shape from the handle.
|
||||
#[test]
|
||||
fn a_shape_tool_scales_the_shape_just_drawn_by_its_bounding_box_handle() {
|
||||
let button = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
for tool in ["rectangle", "roundedRectangle", "ellipse", "polygon", "star"] {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
app.select_tool(tool);
|
||||
let ctx = egui::Context::default();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
let drag = |app: &mut VectorcraftApp, a: Pos2, b: Pos2| {
|
||||
for ev in [
|
||||
egui::Event::PointerMoved(a),
|
||||
button(a, true),
|
||||
egui::Event::PointerMoved(a.lerp(b, 0.5)),
|
||||
egui::Event::PointerMoved(b),
|
||||
button(b, false),
|
||||
] {
|
||||
frame(app, &ctx, vec![ev]);
|
||||
}
|
||||
frame(app, &ctx, vec![]);
|
||||
};
|
||||
let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap());
|
||||
drag(&mut app, xf.to_screen(Point::new(150.0, 120.0)), xf.to_screen(Point::new(210.0, 170.0)));
|
||||
let objects = |app: &VectorcraftApp| {
|
||||
let mut n = 0;
|
||||
app.session.active().unwrap().doc.walk(|node| n += usize::from(node.path_data().is_some()));
|
||||
n
|
||||
};
|
||||
assert_eq!(objects(&app), 1, "{tool}: the drag draws one shape");
|
||||
let before = app.selection_box().unwrap_or_else(|| panic!("{tool}: the shape just drawn shows its box"));
|
||||
// The bottom-right handle, dragged 40 pt along the box's width and 30 pt along its height
|
||||
// (a polygon's or star's box turns with the shape).
|
||||
let (corner, to) = (Point::new(before.rect.x1, before.rect.y1), Point::new(before.rect.x1 + 40.0, before.rect.y1 + 30.0));
|
||||
drag(&mut app, xf.to_screen(before.to_doc() * corner), xf.to_screen(before.to_doc() * to));
|
||||
let after = app.selection_box().unwrap_or_else(|| panic!("{tool}: the shape stays selected"));
|
||||
let label = app.session.active().and_then(|st| st.history.undo.last().map(|e| e.label.clone()));
|
||||
assert_eq!(objects(&app), 1, "{tool}: no second shape, last step {label:?}");
|
||||
assert!(
|
||||
(after.rect.width() - before.rect.width() - 40.0).abs() < 0.5 && (after.rect.height() - before.rect.height() - 30.0).abs() < 0.5,
|
||||
"{tool}: {:?} -> {:?}, last step {label:?}",
|
||||
before.rect,
|
||||
after.rect
|
||||
);
|
||||
assert_eq!(label.as_deref(), Some("Scale"), "{tool}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drag_from_a_ruler_onto_the_canvas_makes_a_guide() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
|
||||
@@ -180,7 +180,8 @@ fn inline_transform_width(ui: &Ui, t: &Tokens) -> f32 {
|
||||
labels + fields + crate::panels::transform::CONSTRAIN_LINK_SIZE + ui.spacing().item_spacing.x * items as f32
|
||||
}
|
||||
|
||||
/// The size of the Control bar's Align, Distribute and area type vertical alignment buttons.
|
||||
/// The size of the Control bar's Align, Distribute and area type vertical alignment buttons, and
|
||||
/// the height of its Align To menu.
|
||||
const BUTTON: f32 = 24.0;
|
||||
|
||||
/// The space ahead of the separator that ends a group of the Control bar's controls.
|
||||
@@ -219,9 +220,9 @@ fn separator_width(ui: &Ui) -> f32 {
|
||||
|
||||
/// The room the controls at the end of the Control bar need: the Transform link, or Document Setup
|
||||
/// and Preferences with nothing selected, or none while the panel menu has that group off. The
|
||||
/// groups ahead of them (the Paragraph section, area type's vertical alignment, the Align and
|
||||
/// Distribute buttons) show only while it is left, so the bar trims them from its right end
|
||||
/// (#5372), after the inline X/Y/W/H fields.
|
||||
/// groups ahead of them (the Paragraph section, area type's vertical alignment, the Align buttons,
|
||||
/// the Align To menu and the Distribute buttons) show only while it is left, so the bar trims them
|
||||
/// from its right end (#5372), after the inline X/Y/W/H fields.
|
||||
fn end_width(app: &VectorcraftApp, ui: &Ui, nothing_selected: bool) -> f32 {
|
||||
if nothing_selected {
|
||||
if group_on(app, "documentOptions") { DOCUMENT_SETUP_WIDTH + ui.spacing().item_spacing.x + PREFERENCES_WIDTH } else { 0.0 }
|
||||
@@ -235,10 +236,10 @@ fn end_width(app: &VectorcraftApp, ui: &Ui, nothing_selected: bool) -> f32 {
|
||||
/// Draws the Control bar's groups between its Character controls and the Transform link, and
|
||||
/// returns whether the Transform link and X/Y/W/H follow (false: nothing is selected, and Document
|
||||
/// Setup and Preferences, drawn here, end the bar, or the Transform group is off). The groups are
|
||||
/// the Paragraph section and area type's vertical alignment (#3759), then the Align buttons and, for
|
||||
/// two or more objects, the
|
||||
/// Distribute buttons (a single object keeps the bar as it was); each shows only while the end of
|
||||
/// the bar keeps its room. Groups the panel menu has off are left out, and take no room.
|
||||
/// the Paragraph section and area type's vertical alignment (#3759), then the Align buttons, the
|
||||
/// Align To menu and, for two or more objects, the Distribute buttons (a single object keeps the bar
|
||||
/// as it was); each shows only while the end of the bar keeps its room. Groups the panel menu has
|
||||
/// off are left out, and take no room.
|
||||
fn groups_before_transform(app: &mut VectorcraftApp, ui: &mut Ui, objects: usize) -> bool {
|
||||
let nothing = objects == 0;
|
||||
let end = end_width(app, ui, nothing);
|
||||
@@ -267,9 +268,12 @@ fn groups_before_transform(app: &mut VectorcraftApp, ui: &mut Ui, objects: usize
|
||||
let end = if transform { separator_width(ui) + end } else { 0.0 };
|
||||
if group_on(app, "alignDistribute") && ui.available_width() >= buttons_width(ui, 7) + end {
|
||||
crate::panels::align::align_buttons(app, ui, BUTTON);
|
||||
if objects > 1 && ui.available_width() >= GROUP_GAP + buttons_width(ui, 6) + end {
|
||||
ui.add_space(GROUP_GAP);
|
||||
crate::panels::align::distribute_buttons(app, ui, BUTTON);
|
||||
if ui.available_width() >= crate::panels::align::align_to_menu_width(BUTTON) + ui.spacing().item_spacing.x + end {
|
||||
crate::panels::align::align_to_menu(app, ui, BUTTON);
|
||||
if objects > 1 && ui.available_width() >= GROUP_GAP + buttons_width(ui, 6) + end {
|
||||
ui.add_space(GROUP_GAP);
|
||||
crate::panels::align::distribute_buttons(app, ui, BUTTON);
|
||||
}
|
||||
}
|
||||
if transform {
|
||||
separator(ui);
|
||||
|
||||
@@ -230,24 +230,73 @@ pub(super) fn status_readouts(app: &VectorcraftApp, ui: &mut Ui, t: &Tokens) {
|
||||
.collect();
|
||||
// Reserve the spinner's space while idle too: starting a background job changes the text,
|
||||
// not its origin. Messages take priority over optional readouts in a narrow window.
|
||||
let message = if let Some(job) = busy.first() {
|
||||
let (message, notes) = if let Some(job) = busy.first() {
|
||||
let spinner = egui::Rect::from_center_size(egui::pos2(area.left() + 6.0, area.center().y), vec2(12.0, 12.0));
|
||||
ui.put(spinner, egui::Spinner::new().size(12.0).color(t.accent));
|
||||
let label = crate::i18n::msg(job);
|
||||
let more = busy.len() - 1;
|
||||
if more > 0 { format!("{label}… (+{more})") } else { format!("{label}…") }
|
||||
(if more > 0 { format!("{label}… (+{more})") } else { format!("{label}…") }, &[][..])
|
||||
} else {
|
||||
crate::i18n::msg(&app.ui.status)
|
||||
(crate::i18n::msg(&app.ui.status), status_notes(&app.ui))
|
||||
};
|
||||
if !message.is_empty() {
|
||||
let rect = egui::Rect::from_min_max(egui::pos2((area.left() + 18.0).min(right), area.top()), egui::pos2(right, area.bottom()));
|
||||
status_field(ui, "message", rect, message, egui::FontId::proportional(11.0), t.text);
|
||||
status_field_with_notes(ui, "message", rect, message, egui::FontId::proportional(11.0), t.text, notes);
|
||||
}
|
||||
}
|
||||
|
||||
/// Most notes the status message's hover and access information list.
|
||||
const MAX_LISTED_NOTES: usize = 30;
|
||||
|
||||
/// The notes behind the status message (the open's, [`crate::state::UiState::status_notes`]) when
|
||||
/// there are several and the status bar still shows the message they came with, else none.
|
||||
fn status_notes(ui: &crate::state::UiState) -> &[String] {
|
||||
let notes = ui.status_notes.as_ref().filter(|(status, notes)| *status == ui.status && notes.len() > 1);
|
||||
notes.map(|(_, notes)| notes.as_slice()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// `notes` in the interface language, one to a line, the first `most` of them (at most
|
||||
/// [`MAX_LISTED_NOTES`]) and how many more.
|
||||
pub(super) fn listed_notes(notes: &[String], most: usize) -> String {
|
||||
let lang = crate::i18n::current();
|
||||
let most = most.min(MAX_LISTED_NOTES);
|
||||
let mut lines: Vec<String> = notes.iter().take(most).map(|n| format!("• {}", crate::i18n::message(lang, n))).collect();
|
||||
if notes.len() > most {
|
||||
lines.push(format!("… (+{})", notes.len() - most));
|
||||
}
|
||||
lines.join("\n")
|
||||
}
|
||||
|
||||
/// The hover of the status message `text` with `notes`: the message, then as many of the notes
|
||||
/// ([`listed_notes`]) as fit in the window above or below the field `field`, wrapped within the
|
||||
/// window's width.
|
||||
fn notes_tooltip(response: egui::Response, field: egui::Rect, text: &str, notes: &[String]) -> egui::Response {
|
||||
response.on_hover_ui(|ui| {
|
||||
let screen = ui.ctx().content_rect();
|
||||
let margins = ui.spacing().menu_margin.sum() + 2.0 * ui.spacing().item_spacing;
|
||||
let width = ui.spacing().tooltip_width.min(screen.width() - margins.x).max(0.0);
|
||||
ui.set_max_width(width);
|
||||
let room = (field.top() - screen.top()).max(screen.bottom() - field.bottom()) - 2.0 * margins.y;
|
||||
let tip = |most: usize| format!("{text}\n{}", listed_notes(notes, most));
|
||||
let height =
|
||||
|s: &str| egui::WidgetText::from(s).into_galley(ui, Some(egui::TextWrapMode::Wrap), width, egui::FontSelection::Default).size().y;
|
||||
let mut most = notes.len().min(MAX_LISTED_NOTES);
|
||||
while most > 0 && height(&tip(most)) > room {
|
||||
most -= 1;
|
||||
}
|
||||
ui.add(egui::Label::new(tip(most)).wrap());
|
||||
})
|
||||
}
|
||||
|
||||
/// A field never grows with its text. Clipping also bounds over-wide glyphs; hover and access
|
||||
/// information retain the complete value when its on-screen label is truncated.
|
||||
fn status_field(ui: &mut Ui, id: &str, rect: egui::Rect, text: String, font: egui::FontId, color: egui::Color32) {
|
||||
status_field_with_notes(ui, id, rect, text, font, color, &[]);
|
||||
}
|
||||
|
||||
/// [`status_field`], whose hover, when there are `notes`, lists them ([`notes_tooltip`]) and whose
|
||||
/// access information has them after the text.
|
||||
fn status_field_with_notes(ui: &mut Ui, id: &str, rect: egui::Rect, text: String, font: egui::FontId, color: egui::Color32, notes: &[String]) {
|
||||
if rect.width() <= 0.0 || rect.height() <= 0.0 {
|
||||
return;
|
||||
}
|
||||
@@ -261,9 +310,17 @@ fn status_field(ui: &mut Ui, id: &str, rect: egui::Rect, text: String, font: egu
|
||||
let elided = galley.elided;
|
||||
ui.painter().with_clip_rect(ui.clip_rect().intersect(rect)).galley(at, galley, color);
|
||||
let response = ui.interact(rect, ui.scope_id().with(("status-field", id)), Sense::hover());
|
||||
response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Label, true, &text));
|
||||
if elided {
|
||||
full_text_tooltip(response, text);
|
||||
if notes.is_empty() {
|
||||
response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Label, true, &text));
|
||||
if elided {
|
||||
full_text_tooltip(response, text);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// The list is made only for a hover or a screen reader.
|
||||
response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Label, true, format!("{text}\n{}", listed_notes(notes, MAX_LISTED_NOTES))));
|
||||
if response.hovered() {
|
||||
notes_tooltip(response, rect, &text, notes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -461,6 +461,65 @@ fn a_clipped_status_message_shows_in_full_on_hover() {
|
||||
assert_eq!(hovered.iter().filter(|t| t.text.starts_with("Saved")).count(), 1, "a message that fits has no tooltip: {hovered:?}");
|
||||
}
|
||||
|
||||
/// The status message of an open with several notes lists them all on hover, and screen readers
|
||||
/// receive them after the message, while the status bar shows that message. A later message
|
||||
/// leaves the list out, and so does an open with one note.
|
||||
#[test]
|
||||
fn the_message_of_an_open_with_several_notes_lists_them_on_hover() {
|
||||
let (mut app, ctx) = status_readout_app();
|
||||
ctx.enable_accesskit();
|
||||
let notes = vec!["the file's layers weren't read".to_string(), "a brush wasn't read".to_string()];
|
||||
app.ui.status = crate::io::opened_with_notes(2, ¬es[0]);
|
||||
app.ui.status_notes = Some((app.ui.status.clone(), notes));
|
||||
let listed = "• the file's layers weren't read\n• a brush wasn't read";
|
||||
let full = format!("{}\n{listed}", app.ui.status);
|
||||
let (texts, nodes, hovered) = bar_hover(&mut app, &ctx, 1400.0, "Opened", super::status_bar);
|
||||
assert!(!status_text(&texts, "Opened").elided);
|
||||
assert!(nodes.iter().any(|n| n.value() == Some(full.as_str())), "screen readers receive the notes: {nodes:?}");
|
||||
assert!(hovered.iter().any(|t| t.text == full), "the tooltip lists the notes: {hovered:?}");
|
||||
|
||||
app.ui.status = "Saved recovery data for Untitled-1".to_owned();
|
||||
let (_, nodes, hovered) = bar_hover(&mut app, &ctx, 1400.0, "Saved", super::status_bar);
|
||||
assert!(!nodes.iter().any(|n| n.value().is_some_and(|v| v.contains("• "))), "{nodes:?}");
|
||||
assert_eq!(hovered.iter().filter(|t| t.text.starts_with("Saved")).count(), 1, "no tooltip: {hovered:?}");
|
||||
|
||||
// One note is the message itself: no list.
|
||||
app.ui.status = crate::io::opened_with_notes(1, "a brush wasn't read");
|
||||
app.ui.status_notes = Some((app.ui.status.clone(), vec!["a brush wasn't read".to_string()]));
|
||||
let (_, nodes, hovered) = bar_hover(&mut app, &ctx, 1400.0, "Opened", super::status_bar);
|
||||
assert!(!nodes.iter().any(|n| n.value().is_some_and(|v| v.contains("• "))), "{nodes:?}");
|
||||
assert_eq!(hovered.iter().filter(|t| t.text.starts_with("Opened")).count(), 1, "no tooltip: {hovered:?}");
|
||||
}
|
||||
|
||||
/// The list has at most 30 notes, then how many more there are.
|
||||
#[test]
|
||||
fn the_hover_lists_thirty_notes_and_counts_the_rest() {
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
let notes: Vec<String> = (1..=32).map(|i| format!("note {i}")).collect();
|
||||
let listed = super::status_bar::listed_notes(¬es, usize::MAX);
|
||||
let lines: Vec<&str> = listed.lines().collect();
|
||||
assert_eq!((lines.len(), lines[0], lines[29], lines[30]), (31, "• note 1", "• note 30", "… (+2)"));
|
||||
assert_eq!(super::status_bar::listed_notes(¬es[..2], usize::MAX), "• note 1\n• note 2");
|
||||
assert_eq!(super::status_bar::listed_notes(¬es[..3], 1), "• note 1\n… (+2)");
|
||||
}
|
||||
|
||||
/// A hover with more long notes than the window can show lists the ones that fit and how many
|
||||
/// more there are, inside the window. A window 300 points tall is shorter than the main window's
|
||||
/// smallest size (500 points) at UI Scaling 1.5.
|
||||
#[test]
|
||||
fn the_hover_of_many_notes_stays_inside_a_small_window() {
|
||||
let (mut app, ctx) = status_readout_app();
|
||||
let notes: Vec<String> = (1..=32).map(|i| format!("note {i}: {}", "this file's PostScript couldn't be read ".repeat(3))).collect();
|
||||
app.ui.status = crate::io::opened_with_notes(notes.len(), ¬es[0]);
|
||||
app.ui.status_notes = Some((app.ui.status.clone(), notes));
|
||||
let (_, _, hovered) = bar_hover(&mut app, &ctx, 700.0, "Opened", super::status_bar);
|
||||
let tip: Vec<_> = hovered.iter().filter(|t| t.text.contains("\n• note 1:")).collect();
|
||||
let [tip] = &tip[..] else { panic!("one tooltip lists the notes: {hovered:?}") };
|
||||
assert!(tip.bounds.top() >= 0.0 && tip.bounds.bottom() <= 300.0 && tip.bounds.right() <= 700.0, "{tip:?}");
|
||||
let last = tip.text.lines().last().unwrap_or_default();
|
||||
assert!(last.starts_with("… (+") && !tip.text.contains("• note 30:"), "{last:?}");
|
||||
}
|
||||
|
||||
/// The Help Bar's hint ends in an ellipsis when it is wider than the window and shows in full on
|
||||
/// hover, in a tooltip that wraps within the window (#6622). The Dimension tool's hint is wider
|
||||
/// than the smallest main window (800 points); 400 points is that window at UI Scaling 2.0.
|
||||
@@ -2663,6 +2722,109 @@ fn the_control_bar_shows_the_distribute_buttons_for_two_or_more_objects() {
|
||||
assert_eq!((left(&app, &a), left(&app, &b), left(&app, &c)), (10.0, 105.0, 200.0), "{}", app.ui.status);
|
||||
}
|
||||
|
||||
/// The Control bar's Align To menu follows its Align buttons. It shows the Align panel's Align To
|
||||
/// choice (Artboard for one object, which the buttons align to the artboard) and screen readers
|
||||
/// get it as the value of a combo box named "Align To:". Its menu checks that choice, has Align to
|
||||
/// Key Object off while no key object can be made, and a pick chooses as the Align panel does:
|
||||
/// Artboard and Selection are stored with the UI state and let go of the key object, and Key
|
||||
/// Object makes the topmost selected object the key, keeping the stored choice. The Align
|
||||
/// buttons then align to the choice.
|
||||
#[test]
|
||||
fn the_control_bar_align_to_menu_chooses_what_align_aligns_to() {
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
let mut app = crate::VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 400, "height": 200})).unwrap();
|
||||
let rect = |app: &mut crate::VectorcraftApp, x: f64| {
|
||||
app.run("shape.rectangle", json!({"x": x, "y": 40, "width": 30, "height": 20})).unwrap()["id"].clone()
|
||||
};
|
||||
let (a, b) = (rect(&mut app, 50.0), rect(&mut app, 150.0));
|
||||
let ctx = egui::Context::default();
|
||||
ctx.enable_accesskit();
|
||||
crate::theme::install_fonts(&ctx);
|
||||
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1600.0, 300.0));
|
||||
let frame = |app: &mut crate::VectorcraftApp, events: Vec<egui::Event>| {
|
||||
let mut out = ctx.run_ui(egui::RawInput { events, screen_rect: Some(screen), ..Default::default() }, |ui| super::control_bar(app, ui));
|
||||
out.textures_delta.clear();
|
||||
out.platform_output.accesskit_update.map(|u| u.nodes).unwrap_or_default()
|
||||
};
|
||||
type Nodes = Vec<(egui::accesskit::NodeId, egui::accesskit::Node)>;
|
||||
// A control by its name; a menu row's name follows its check column.
|
||||
let find = |nodes: &Nodes, name: &str| {
|
||||
nodes.iter().find(|(_, n)| n.label().is_some_and(|l| l.trim_start_matches(['✓', ' ']) == name)).map(|(_, n)| n.clone())
|
||||
};
|
||||
let center = |n: &egui::accesskit::Node| {
|
||||
let b = n.bounds().unwrap_or_else(|| panic!("{n:?}"));
|
||||
egui::pos2(((b.x0 + b.x1) / 2.0) as f32, ((b.y0 + b.y1) / 2.0) as f32)
|
||||
};
|
||||
let click = |app: &mut crate::VectorcraftApp, at: egui::Pos2| {
|
||||
let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
frame(app, vec![egui::Event::PointerMoved(at), press(true)]);
|
||||
frame(app, vec![press(false)]);
|
||||
frame(app, vec![]);
|
||||
frame(app, vec![])
|
||||
};
|
||||
let menu = |app: &mut crate::VectorcraftApp| {
|
||||
frame(app, vec![]);
|
||||
let nodes = frame(app, vec![]);
|
||||
find(&nodes, "Align To:").unwrap_or_else(|| panic!("the Control bar has the Align To menu"))
|
||||
};
|
||||
// Opens the menu, checks its rows and picks `pick`, or closes it with Escape.
|
||||
let choose = |app: &mut crate::VectorcraftApp, checked: &str, key_on: bool, pick: Option<&str>| {
|
||||
let at = center(&menu(app));
|
||||
let nodes = click(app, at);
|
||||
for name in ["Align to Selection", "Align to Key Object", "Align to Artboard"] {
|
||||
let row = find(&nodes, name).unwrap_or_else(|| panic!("the menu lists {name}"));
|
||||
assert_eq!(row.label().is_some_and(|l| l.starts_with('✓')), name == checked, "{name} checked");
|
||||
assert_eq!(row.is_disabled(), name == "Align to Key Object" && !key_on, "{name} off");
|
||||
}
|
||||
let nodes = match pick {
|
||||
Some(pick) => click(app, center(&find(&nodes, pick).unwrap_or_else(|| panic!("{pick}")))),
|
||||
None => {
|
||||
let escape =
|
||||
|pressed| egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
|
||||
frame(app, vec![escape(true)]);
|
||||
frame(app, vec![escape(false)]);
|
||||
frame(app, vec![])
|
||||
}
|
||||
};
|
||||
assert!(find(&nodes, "Align to Artboard").is_none(), "the menu closes");
|
||||
};
|
||||
let key = |app: &crate::VectorcraftApp| app.session.active().unwrap().selection.key;
|
||||
let x0 = |app: &crate::VectorcraftApp, id: &serde_json::Value| {
|
||||
app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id.as_u64().unwrap())).unwrap().geometric_bounds().unwrap().x0
|
||||
};
|
||||
|
||||
// One object: the buttons align it to the artboard, and Align to Key Object is off.
|
||||
app.run("select.set", json!({"ids": [a]})).unwrap();
|
||||
let m = menu(&mut app);
|
||||
assert_eq!(m.role(), egui::accesskit::Role::ComboBox);
|
||||
assert_eq!(m.value(), Some("Align to Artboard"), "one object shows Artboard");
|
||||
choose(&mut app, "Align to Artboard", false, None);
|
||||
assert_eq!(key(&app), None);
|
||||
|
||||
// Two objects: the stored choice, Selection.
|
||||
app.run("select.set", json!({"ids": [a, b]})).unwrap();
|
||||
assert_eq!(menu(&mut app).value(), Some("Align to Selection"));
|
||||
choose(&mut app, "Align to Selection", true, Some("Align to Artboard"));
|
||||
assert_eq!(app.ui.align_to, crate::panels::align::AlignTo::Artboard, "the choice the Align panel shows");
|
||||
assert_eq!(menu(&mut app).value(), Some("Align to Artboard"));
|
||||
let left = find(&frame(&mut app, vec![]), "Horizontal Align Left").unwrap_or_else(|| panic!("Horizontal Align Left"));
|
||||
click(&mut app, center(&left));
|
||||
assert_eq!((x0(&app, &a), x0(&app, &b)), (0.0, 0.0), "aligned to the artboard: {}", app.ui.status);
|
||||
|
||||
// Key Object makes the topmost the key and keeps the stored choice.
|
||||
choose(&mut app, "Align to Artboard", true, Some("Align to Key Object"));
|
||||
assert_eq!(key(&app), Some(vectorcraft_doc::NodeId(b.as_u64().unwrap())), "the topmost object is the key");
|
||||
assert_eq!(app.ui.align_to, crate::panels::align::AlignTo::Artboard, "the stored choice stays");
|
||||
assert_eq!(menu(&mut app).value(), Some("Align to Key Object"));
|
||||
|
||||
// Selection lets go of the key and is stored.
|
||||
choose(&mut app, "Align to Key Object", true, Some("Align to Selection"));
|
||||
assert_eq!(key(&app), None, "the key object is let go");
|
||||
assert_eq!(app.ui.align_to, crate::panels::align::AlignTo::Selection);
|
||||
assert_eq!(menu(&mut app).value(), Some("Align to Selection"));
|
||||
}
|
||||
|
||||
/// The Control bar shows the Global Edit Options dropdown while Start Global Edit can run. Its
|
||||
/// options are the session's, which the Properties panel's dropdown also sets: Match Size from
|
||||
/// the Control bar narrows the Results and the objects Start Global Edit takes. The dropdown is off
|
||||
@@ -2765,7 +2927,8 @@ fn the_control_bar_shows_the_inline_transform_fields_only_when_they_fit() {
|
||||
crate::theme::install_fonts(&ctx);
|
||||
let mut seen = false;
|
||||
let mut menu_seen = false;
|
||||
// The Align group, then for two objects the Distribute group (#5372): each by its controls.
|
||||
// The Align group and the Align To menu, then for two objects the Distribute group (#5372):
|
||||
// each by its controls.
|
||||
let mut groups: Vec<(&str, Vec<&str>)> = vec![(
|
||||
"Align",
|
||||
vec![
|
||||
@@ -2778,6 +2941,7 @@ fn the_control_bar_shows_the_inline_transform_fields_only_when_they_fit() {
|
||||
"Vertical Align Bottom",
|
||||
],
|
||||
)];
|
||||
groups.push(("Align To", vec!["Align To:"]));
|
||||
if count == 2 {
|
||||
groups.push((
|
||||
"Distribute",
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
mod about;
|
||||
pub mod action_batch;
|
||||
pub mod action_insert_menu_item;
|
||||
pub mod action_new;
|
||||
pub mod action_playback;
|
||||
pub mod action_set_options;
|
||||
@@ -391,6 +392,7 @@ registry! {
|
||||
ActionPlayback: [action_playback::KIND] => action_playback::SPEC,
|
||||
ActionStopRecord: [action_stop::RECORD_KIND] => action_stop::RECORD_SPEC,
|
||||
ActionStopMessage: [action_stop::MESSAGE_KIND] => action_stop::MESSAGE_SPEC,
|
||||
ActionInsertMenuItem: [action_insert_menu_item::KIND] => action_insert_menu_item::SPEC,
|
||||
SliceOptions: [slices::OPTIONS] => slices::OPTIONS_SPEC,
|
||||
DivideSlices: [slices::DIVIDE] => slices::DIVIDE_SPEC,
|
||||
EpsOptions: [eps_options::KIND] => eps_options::SPEC,
|
||||
|
||||
@@ -0,0 +1,261 @@
|
||||
//! Actions panel menu › Insert Menu Item… (`ui.action.insertMenuItem`): the menu items that can be
|
||||
//! steps of an action, to pick one from the list or find it by name.
|
||||
//!
|
||||
//! Its fields: `find` (the words the list is narrowed to), `command` and `params` (the item picked;
|
||||
//! `command` is empty until one is), `note` (`none` or `several` when Find picked nothing), and
|
||||
//! `set`, `action` and `step`, the place of the new step (no `set` and `action`: the recording in
|
||||
//! progress). Picking an item runs nothing. OK inserts it; an invalid place is an error, and the
|
||||
//! dialog stays open.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::DialogSpec;
|
||||
use crate::state::Dialog;
|
||||
use crate::theme::Tokens;
|
||||
use crate::{VectorcraftApp, menus, widgets};
|
||||
|
||||
/// The dialog kind of Insert Menu Item.
|
||||
pub const KIND: &str = "actionInsertMenuItem";
|
||||
|
||||
/// The most rows the list shows at once; Find narrows it.
|
||||
const MAX_LISTED: usize = 200;
|
||||
|
||||
/// The width of the Menu Item label.
|
||||
const LABEL_W: f32 = 80.0;
|
||||
|
||||
pub(super) const SPEC: DialogSpec = DialogSpec {
|
||||
heading: |_| tl!("Insert Menu Item").into(),
|
||||
body,
|
||||
confirm,
|
||||
ok_enabled: Some(|_, d| !d.str("command").is_empty()),
|
||||
min_width: 460.0,
|
||||
..DialogSpec::FORM
|
||||
};
|
||||
|
||||
/// Opens Insert Menu Item for action `target` as (set, action), after `step`, else for the recording
|
||||
/// in progress.
|
||||
pub fn open(app: &mut VectorcraftApp, target: Option<(usize, usize)>, step: Option<usize>) {
|
||||
let mut fields = json!({"find": "", "command": "", "params": {}, "note": ""});
|
||||
if let Some((set, action)) = target {
|
||||
fields["set"] = json!(set);
|
||||
fields["action"] = json!(action);
|
||||
if let Some(step) = step {
|
||||
fields["step"] = json!(step);
|
||||
}
|
||||
}
|
||||
app.ui.dialog = Some(Dialog::new(KIND, fields));
|
||||
}
|
||||
|
||||
/// A row of the list: an item of [`menus::insertable_items`], with what it shows and what Find
|
||||
/// matches worked out once.
|
||||
pub(crate) struct Row {
|
||||
command: &'static str,
|
||||
params: Value,
|
||||
/// The menu path and label in the UI language (`Object › Group`).
|
||||
shown: String,
|
||||
/// The English path and label, the shown one and the command id, lowercased, one per line.
|
||||
haystack: String,
|
||||
}
|
||||
|
||||
/// The rows in `lang`, for menus that name fonts in English when `english_names` is set.
|
||||
fn build(lang: crate::i18n::Lang, english_names: bool) -> Vec<Row> {
|
||||
menus::insertable_items(english_names)
|
||||
.into_iter()
|
||||
.map(|item| {
|
||||
let parts = || item.path.iter().chain(std::iter::once(&item.label)).copied();
|
||||
let english = parts().collect::<Vec<_>>().join(" › ");
|
||||
let shown = parts().map(|s| crate::i18n::tr(lang, s)).collect::<Vec<_>>().join(" › ");
|
||||
let haystack = format!("{}\n{}\n{}", english.to_lowercase(), shown.to_lowercase(), item.command.to_lowercase());
|
||||
Row { command: item.command, params: item.params, shown, haystack }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The rows, built again only when the language, the installed plug-ins, the font name setting or
|
||||
/// the loaded fonts change, not on every frame.
|
||||
fn rows(app: &VectorcraftApp, ctx: &egui::Context) -> Arc<Vec<Row>> {
|
||||
type Key = (&'static str, u64, bool, u64, bool);
|
||||
let lang = crate::i18n::current();
|
||||
let english_names = app.session.prefs.font_names_in_english;
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
// Read before building, as the Font menu does: fonts that load meanwhile build the rows again.
|
||||
let (cataloged, generation) = (db.installed_fonts_ready(), db.generation());
|
||||
let key: Key = (lang.code(), menus::plugin_revision(), english_names, generation, cataloged);
|
||||
let id = egui::Id::unique("action-insert-menu-item-rows");
|
||||
if let Some((k, v)) = ctx.data(|d| d.get_temp::<(Key, Arc<Vec<Row>>)>(id))
|
||||
&& k == key
|
||||
{
|
||||
return v;
|
||||
}
|
||||
let built = Arc::new(build(lang, english_names));
|
||||
ctx.data_mut(|d| d.insert_temp::<(Key, Arc<Vec<Row>>)>(id, (key, built.clone())));
|
||||
built
|
||||
}
|
||||
|
||||
/// The rows that every word of `find` matches, in menu order.
|
||||
fn matches<'a>(rows: &'a [Row], find: &str) -> impl Iterator<Item = &'a Row> {
|
||||
let words: Vec<String> = find.to_lowercase().split_whitespace().map(str::to_string).collect();
|
||||
rows.iter().filter(move |r| words.iter().all(|w| r.haystack.contains(w.as_str())))
|
||||
}
|
||||
|
||||
/// Picks `row` in `d`.
|
||||
fn pick(d: &mut Dialog, row: &Row) {
|
||||
d.fields.insert("command".into(), json!(row.command));
|
||||
d.fields.insert("params".into(), row.params.clone());
|
||||
d.fields.insert("note".into(), json!(""));
|
||||
}
|
||||
|
||||
/// Find: picks the one row that `find` matches; with none or several, picks nothing and notes it.
|
||||
fn find(rows: &[Row], d: &mut Dialog) {
|
||||
let found: Vec<&Row> = matches(rows, &d.str("find")).take(2).collect();
|
||||
match found[..] {
|
||||
[row] => pick(d, row),
|
||||
[] => {
|
||||
d.fields.insert("note".into(), json!("none"));
|
||||
}
|
||||
_ => {
|
||||
d.fields.insert("note".into(), json!("several"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `row` is the item picked in `d`.
|
||||
fn picked(d: &Dialog, row: &Row) -> bool {
|
||||
d.fields.get("command").and_then(Value::as_str) == Some(row.command) && d.fields.get("params") == Some(&row.params)
|
||||
}
|
||||
|
||||
fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||||
let rows = rows(app, ui.ctx());
|
||||
let dim = Tokens::get(ui.ctx()).text_dim;
|
||||
let mut find_now = false;
|
||||
ui.horizontal(|ui| {
|
||||
if super::form::text_named(ui, d, "find", tl!("Find"), 300.0) {
|
||||
d.fields.insert("note".into(), json!(""));
|
||||
}
|
||||
find_now = widgets::secondary_button(ui, tl!("Find")).clicked();
|
||||
});
|
||||
if find_now {
|
||||
find(&rows, d);
|
||||
}
|
||||
match d.str("note").as_str() {
|
||||
"none" => {
|
||||
ui.label(egui::RichText::new(tl!("No menu item matches.")).color(dim));
|
||||
}
|
||||
"several" => {
|
||||
ui.label(egui::RichText::new(tl!("Several menu items match. Choose one from the list.")).color(dim));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
ui.add_space(4.0);
|
||||
let mut chosen: Option<usize> = None;
|
||||
let query = d.str("find");
|
||||
egui::ScrollArea::vertical().max_height(260.0).auto_shrink([false, true]).show(ui, |ui| {
|
||||
for (i, row) in matches(&rows, &query).enumerate().take(MAX_LISTED) {
|
||||
if ui.selectable_label(picked(d, row), row.shown.as_str()).clicked() {
|
||||
chosen = Some(i);
|
||||
}
|
||||
}
|
||||
});
|
||||
if let Some(row) = chosen.and_then(|i| matches(&rows, &query).nth(i)) {
|
||||
pick(d, row);
|
||||
}
|
||||
ui.add_space(4.0);
|
||||
let shown = rows.iter().find(|r| picked(d, r)).map_or_else(
|
||||
|| {
|
||||
let command = d.str("command");
|
||||
if command.is_empty() { tl!("None").to_string() } else { command }
|
||||
},
|
||||
|r| r.shown.clone(),
|
||||
);
|
||||
widgets::label_row(ui, "Menu Item:", LABEL_W, |ui| {
|
||||
ui.label(shown);
|
||||
});
|
||||
ui.label(egui::RichText::new(tl!("Items that open a dialog, and File, Window and Help menu items, are not listed.")).color(dim));
|
||||
false
|
||||
}
|
||||
|
||||
fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
let mut params = json!({"command": d.str("command"), "params": d.fields.get("params").cloned().unwrap_or_else(|| json!({}))});
|
||||
for key in ["set", "action", "step"] {
|
||||
if let Some(value) = d.fields.get(key) {
|
||||
params[key] = value.clone();
|
||||
}
|
||||
}
|
||||
let r = app.run(crate::panels::actions::INSERT_MENU_ITEM, params)?;
|
||||
app.ui.dialog = None;
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::i18n::Lang;
|
||||
|
||||
#[test]
|
||||
fn find_picks_the_one_match_and_notes_none_or_several() {
|
||||
let rows = build(Lang::EN, true);
|
||||
let mut d = Dialog::new(KIND, json!({"find": "object group", "command": "", "params": {}, "note": ""}));
|
||||
find(&rows, &mut d);
|
||||
assert_eq!((d.str("note"), d.str("command")), ("several".to_string(), String::new()), "Group and Ungroup both match");
|
||||
d.fields.insert("find".into(), json!("OBJECT.UNGROUP"));
|
||||
find(&rows, &mut d);
|
||||
assert_eq!((d.str("command"), d.fields["params"].clone(), d.str("note")), ("object.ungroup".to_string(), json!({}), String::new()));
|
||||
d.fields.insert("find".into(), json!("no such menu item here"));
|
||||
find(&rows, &mut d);
|
||||
assert_eq!((d.str("note"), d.str("command")), ("none".to_string(), "object.ungroup".to_string()), "a failed Find keeps the pick");
|
||||
}
|
||||
|
||||
/// The list holds the menu items that run an engine command without asking for values first,
|
||||
/// outside the application, File, Window and Help menus, and leaves out Undo, Redo, the Pastes,
|
||||
/// Object › Align, Image Trace's Make and Make and Expand, Pattern's Make and Edit Pattern and the
|
||||
/// saved-selection, saved-view and recent-font items.
|
||||
#[test]
|
||||
fn the_list_holds_only_menu_items_that_can_be_steps() {
|
||||
let items = menus::insertable_items(true);
|
||||
assert!(items.len() > 100, "only {} items", items.len());
|
||||
for item in &items {
|
||||
assert!(vectorcraft_engine::find_command(item.command).is_some(), "{item:?}");
|
||||
assert!(!item.label.ends_with('…'), "{item:?}");
|
||||
assert!(!["VectorCraft", "File", "Window", "Help"].contains(&item.path[0]), "{item:?}");
|
||||
assert!(item.params.is_object(), "{item:?}");
|
||||
}
|
||||
let has = |command: &str| items.iter().any(|i| i.command == command);
|
||||
for command in ["object.group", "object.ungroup", "object.arrange.bringToFront"] {
|
||||
assert!(has(command), "{command}");
|
||||
}
|
||||
for command in [
|
||||
"edit.undo",
|
||||
"edit.redo",
|
||||
"edit.paste",
|
||||
"edit.pasteInFront",
|
||||
"object.move",
|
||||
"object.align",
|
||||
"imageTrace.make",
|
||||
"imageTrace.makeAndExpand",
|
||||
"object.pattern.make",
|
||||
"object.pattern.edit",
|
||||
"file.save",
|
||||
"view.zoomIn",
|
||||
"select.recall1",
|
||||
] {
|
||||
assert!(!has(command), "{command}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Rows show their menu path in the UI language, and Find matches the English path, the shown
|
||||
/// one and the command id.
|
||||
#[test]
|
||||
fn rows_show_the_translated_path_and_match_english_shown_text_and_ids() {
|
||||
let de = Lang::from_code("de").unwrap();
|
||||
let rows = build(de, true);
|
||||
let group = rows.iter().find(|r| r.command == "object.group").unwrap();
|
||||
assert_eq!(group.shown, format!("{} › {}", crate::i18n::tr(de, "Object"), crate::i18n::tr(de, "Group")));
|
||||
for words in ["object group", group.shown.as_str(), "object.group"] {
|
||||
assert!(matches(&rows, words).any(|r| r.command == "object.group"), "{words}");
|
||||
}
|
||||
let shared = rows.iter().any(|r| rows.iter().any(|o| o.command == r.command && o.params != r.params));
|
||||
assert!(shared, "items that share a command are listed apart, by their params");
|
||||
}
|
||||
}
|
||||
@@ -26,9 +26,9 @@ const NUMBERS: &[&str] = &["size", "leading", "kerning", "tracking"];
|
||||
/// The on, off or unset checkboxes.
|
||||
const SWITCHES: &[&str] = &["underline", "strikethrough", "standardVerticalRoman"];
|
||||
/// The attributes of Advanced Character Formats, in the order the section shows them.
|
||||
const ADVANCED: &[&str] = &["h_scale", "v_scale", "baseline_shift", "rotation", "language"];
|
||||
const ADVANCED: &[&str] = &["h_scale", "v_scale", "baseline_shift", "tcyUpDown", "tcyLeftRight", "rotation", "language"];
|
||||
/// The number fields of Advanced Character Formats.
|
||||
const ADVANCED_NUMBERS: &[&str] = &["h_scale", "v_scale", "baseline_shift", "rotation"];
|
||||
const ADVANCED_NUMBERS: &[&str] = &["h_scale", "v_scale", "baseline_shift", "tcyUpDown", "tcyLeftRight", "rotation"];
|
||||
/// The attributes of Character Color.
|
||||
const COLOR: &[&str] = &["fill", "stroke", "stroke_width", "overprint_fill", "overprint_stroke"];
|
||||
/// The fields of Character Color: the fill and stroke paints, their tints in percent, the stroke
|
||||
@@ -78,6 +78,8 @@ pub(super) const SPEC: DialogSpec = DialogSpec {
|
||||
"h_scale",
|
||||
"v_scale",
|
||||
"baseline_shift",
|
||||
"tcyUpDown",
|
||||
"tcyLeftRight",
|
||||
"rotation",
|
||||
"language",
|
||||
"fill",
|
||||
@@ -250,6 +252,8 @@ fn optional_number(d: &Dialog, key: &str) -> Result<Option<Value>, String> {
|
||||
"h_scale" => tl!("Horizontal Scale"),
|
||||
"v_scale" => tl!("Vertical Scale"),
|
||||
"baseline_shift" => tl!("Baseline Shift"),
|
||||
"tcyUpDown" => tl!("Tate-chu-yoko Up/Down"),
|
||||
"tcyLeftRight" => tl!("Tate-chu-yoko Left/Right"),
|
||||
"rotation" => tl!("Character Rotation"),
|
||||
"fill_tint" => tl!("Fill Tint"),
|
||||
"stroke_tint" => tl!("Stroke Tint"),
|
||||
@@ -281,7 +285,7 @@ fn optional_number(d: &Dialog, key: &str) -> Result<Option<Value>, String> {
|
||||
if ["h_scale", "v_scale"].contains(&key) && !(1.0..=10000.0).contains(&n) {
|
||||
return Err(format!("{label} must be a number from 1 to 10000"));
|
||||
}
|
||||
if key == "baseline_shift" && !(-1296.0..=1296.0).contains(&n) {
|
||||
if ["baseline_shift", "tcyUpDown", "tcyLeftRight"].contains(&key) && !(-1296.0..=1296.0).contains(&n) {
|
||||
return Err(format!("{label} must be a number from -1296 to 1296"));
|
||||
}
|
||||
// Weight from 0 to the document limit, a tint from 0% to 100%.
|
||||
@@ -564,7 +568,7 @@ pub(crate) fn reset_basic(d: &mut Dialog) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset Panel in Advanced Character Formats: its five attributes become unset.
|
||||
/// Reset Panel in Advanced Character Formats: its seven attributes become unset.
|
||||
pub(crate) fn reset_advanced(d: &mut Dialog) {
|
||||
d.fields.insert("__resetAdvanced".into(), json!(true));
|
||||
for key in ADVANCED_NUMBERS {
|
||||
@@ -718,14 +722,16 @@ pub(crate) fn basic_body(ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Advanced Character Formats: Horizontal Scale, Vertical Scale, Baseline Shift, Character Rotation
|
||||
/// and Language, each empty (unset) or set.
|
||||
/// Advanced Character Formats: Horizontal Scale, Vertical Scale, Baseline Shift, the Tate-chu-yoko
|
||||
/// Up/Down and Left/Right, Character Rotation and Language, each empty (unset) or set.
|
||||
pub(crate) fn advanced_body(ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
egui::Grid::new("character-style-advanced").num_columns(2).spacing([18.0, 8.0]).show(ui, |ui| {
|
||||
for (key, caption, unit) in [
|
||||
("h_scale", tl!("Horizontal Scale"), "%"),
|
||||
("v_scale", tl!("Vertical Scale"), "%"),
|
||||
("baseline_shift", tl!("Baseline Shift"), "pt"),
|
||||
("tcyUpDown", tl!("Tate-chu-yoko Up/Down"), "pt"),
|
||||
("tcyLeftRight", tl!("Tate-chu-yoko Left/Right"), "pt"),
|
||||
("rotation", tl!("Character Rotation"), "°"),
|
||||
] {
|
||||
let row = ui.label(caption);
|
||||
@@ -1473,11 +1479,19 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advanced_section_shows_its_five_controls() {
|
||||
fn advanced_section_shows_its_seven_controls() {
|
||||
let mut d = dialog_for(json!({}));
|
||||
let mut app = app();
|
||||
let shown = crate::tests_labels::painted_text(&mut app, |_, ui| advanced_body(ui, &mut d));
|
||||
for label in ["Horizontal Scale", "Vertical Scale", "Baseline Shift", "Character Rotation", "Language:"] {
|
||||
for label in [
|
||||
"Horizontal Scale",
|
||||
"Vertical Scale",
|
||||
"Baseline Shift",
|
||||
"Tate-chu-yoko Up/Down",
|
||||
"Tate-chu-yoko Left/Right",
|
||||
"Character Rotation",
|
||||
"Language:",
|
||||
] {
|
||||
assert!(shown.contains(label), "{label}: {shown}");
|
||||
}
|
||||
}
|
||||
@@ -1569,6 +1583,85 @@ mod tests {
|
||||
assert!(!def.attrs.contains_key("v_scale"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tate_chu_yoko_offsets_show_set_reset_and_keep_their_limits() {
|
||||
let d = dialog_for(json!({"tcyUpDown": 2.0, "size": 12.0}));
|
||||
assert_eq!((d.fields["tcyUpDown"].clone(), d.fields["tcyLeftRight"].clone()), (json!(2.0), json!("")));
|
||||
assert_eq!(edited_attrs(&d).unwrap(), attrs(json!({"tcyUpDown": 2.0, "size": 12.0})));
|
||||
// A value a style captured from scaled text stays while its field is untouched.
|
||||
assert_eq!(edited_attrs(&dialog_for(json!({"tcyLeftRight": 3000.0}))).unwrap(), attrs(json!({"tcyLeftRight": 3000.0})));
|
||||
|
||||
let mut d = dialog_for(json!({"tcyUpDown": 2.0}));
|
||||
d.fields.insert("tcyUpDown".into(), json!(""));
|
||||
d.fields.insert("tcyLeftRight".into(), json!("-1296"));
|
||||
assert_eq!(edited_attrs(&d).unwrap(), attrs(json!({"tcyLeftRight": -1296.0})));
|
||||
d.fields.insert("tcyLeftRight".into(), json!("0"));
|
||||
assert_eq!(edited_attrs(&d).unwrap(), attrs(json!({"tcyLeftRight": 0.0})));
|
||||
for (key, value, message) in [
|
||||
("tcyUpDown", "1296.1", "Tate-chu-yoko Up/Down must be a number from -1296 to 1296"),
|
||||
("tcyLeftRight", "-1296.1", "Tate-chu-yoko Left/Right must be a number from -1296 to 1296"),
|
||||
("tcyUpDown", "high", "Tate-chu-yoko Up/Down must be a number"),
|
||||
("tcyLeftRight", "NaN", "Tate-chu-yoko Left/Right must be a number"),
|
||||
] {
|
||||
let mut d = dialog_for(json!({}));
|
||||
d.fields.insert(key.into(), json!(value));
|
||||
assert_eq!(edited_attrs(&d).unwrap_err(), message, "{key} {value}");
|
||||
}
|
||||
|
||||
let mut d = dialog_for(json!({"tcyUpDown": 2.0, "tcyLeftRight": -1.0, "size": 12.0}));
|
||||
d.fields.insert("section".into(), json!("advanced"));
|
||||
reset_section(&mut d);
|
||||
assert_eq!(edited_attrs(&d).unwrap(), attrs(json!({"size": 12.0})));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tate_chu_yoko_offsets_preview_cancel_and_commit_to_the_style_and_its_text() {
|
||||
let mut app = app();
|
||||
app.run("charStyle.new", json!({"name": "Body", "attrs": {"size": 24.0, "tcyLeftRight": 0.0}})).unwrap();
|
||||
let id = app.run("text.create", json!({"x": 10, "y": 40, "text": "Text"})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("charStyle.apply", json!({"name": "Body", "id": id})).unwrap();
|
||||
app.run("text.setRangeStyle", json!({"id": id, "start": 2, "end": 4, "tcyLeftRight": 5.0})).unwrap();
|
||||
let before = app.session.doc().unwrap().doc.clone();
|
||||
let undo = app.session.doc().unwrap().history.undo.len();
|
||||
open(&mut app, "Body").unwrap();
|
||||
{
|
||||
let d = app.ui.dialog.as_mut().unwrap();
|
||||
d.fields.insert("section".into(), json!("advanced"));
|
||||
d.fields.insert("tcyUpDown".into(), json!("4"));
|
||||
}
|
||||
frame(&mut app);
|
||||
assert_eq!(app.session.doc().unwrap().doc.char_styles[0].attrs["tcyUpDown"], json!(4.0));
|
||||
super::super::cancel(&mut app);
|
||||
assert_eq!(app.session.doc().unwrap().doc, before);
|
||||
assert_eq!(app.session.doc().unwrap().history.undo.len(), undo);
|
||||
|
||||
open(&mut app, "Body").unwrap();
|
||||
{
|
||||
let d = app.ui.dialog.as_mut().unwrap();
|
||||
d.fields.insert("tcyUpDown".into(), json!("4"));
|
||||
d.fields.insert("tcyLeftRight".into(), json!("-3"));
|
||||
}
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
let state = app.session.doc().unwrap();
|
||||
assert_eq!(state.history.undo.len(), undo + 1);
|
||||
assert_eq!(state.doc.char_styles[0].attrs, attrs(json!({"size": 24.0, "tcyUpDown": 4.0, "tcyLeftRight": -3.0})));
|
||||
let vectorcraft_doc::NodeKind::Text(t) = &state.doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind else { panic!("text") };
|
||||
assert_eq!(t.runs.iter().map(|r| (r.style.tcy_up_down, r.style.tcy_left_right)).collect::<Vec<_>>(), [(4.0, -3.0), (4.0, 5.0)]);
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(app.session.doc().unwrap().doc, before);
|
||||
|
||||
app.run("ui.newCharStyle", json!({})).unwrap();
|
||||
{
|
||||
let d = app.ui.dialog.as_mut().unwrap();
|
||||
d.fields.insert("name".into(), json!("Shifted"));
|
||||
d.fields.insert("tcyLeftRight".into(), json!("6"));
|
||||
}
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
let doc = &app.session.doc().unwrap().doc;
|
||||
let def = doc.char_styles.iter().find(|def| def.name == "Shifted").unwrap();
|
||||
assert_eq!(def.attrs["tcyLeftRight"], json!(6.0));
|
||||
}
|
||||
|
||||
/// The negated tags of the twenty stylistic sets: an explicit empty Stylistic Sets choice.
|
||||
fn no_sets() -> Vec<String> {
|
||||
vectorcraft_text::EXTRA_FEATURES[..20].iter().map(|s| format!("-{s}")).collect()
|
||||
|
||||
@@ -64,11 +64,12 @@ pub(super) const SPEC: DialogSpec = DialogSpec {
|
||||
/// Does this dialog show a Preview checkbox? Grid Repeat Options does: the public page for the
|
||||
/// grid options lists one, and the radial and mirror pages do not. Type on a Path Options does
|
||||
/// too (#5360), as public tutorials of the dialog show, and Type Tools › Release Type on a Path
|
||||
/// works the same way (#5532). (Cancel rolls a preview back whichever dialog started it.)
|
||||
/// works the same way (#5532), and so does Image Tools › Remove Background. (Cancel rolls a
|
||||
/// preview back whichever dialog started it.)
|
||||
fn previews(d: &Dialog) -> bool {
|
||||
match d.str("__command").as_str() {
|
||||
"object.repeat.options" => d.fields.contains_key("gridType"),
|
||||
"type.pathOptions" | crate::addons::typetools::RELEASE => true,
|
||||
"type.pathOptions" | crate::addons::typetools::RELEASE | crate::addons::image::REMOVE_BACKGROUND => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -117,6 +118,7 @@ fn order(command: &str) -> &'static [&'static str] {
|
||||
"object.pathfinder.trap" => &["thickness", "heightWidth", "tintReduction", "trapsWithProcessColor", "reverseTraps"],
|
||||
"addon.color.printFixes" => &["inkLimit", "excludePureBlack", "removeWhiteOverprint", "overprintBlack", "textOnly", "fill", "stroke"],
|
||||
crate::addons::typetools::RELEASE => &["mode", "keepOriginal"],
|
||||
crate::addons::image::REMOVE_BACKGROUND => &["background", "colors", "contiguous", "match", "tolerance", "softness"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
@@ -136,6 +138,8 @@ fn choices(command: &str, key: &str) -> Option<form::Choices> {
|
||||
("graph.setType", "pieLegend") => Some(PIE_LEGEND),
|
||||
("graph.setType", "piePosition") => Some(PIE_POSITION),
|
||||
("graph.setType", "pieSort") => Some(PIE_SORT),
|
||||
(crate::addons::image::REMOVE_BACKGROUND, "background") => Some(crate::addons::image::BACKGROUNDS),
|
||||
(crate::addons::image::REMOVE_BACKGROUND, "match") => Some(crate::addons::image::MATCHES),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,6 +391,12 @@ pub(super) fn own_choices(effect: &str, key: &str, cmyk: bool) -> Option<Vec<(&'
|
||||
let c3 = |s: &'static str| crate::i18n::tr_ctx(crate::i18n::current(), "3d", s);
|
||||
Some(match (effect, key) {
|
||||
(vectorcraft_effects::REVOLVE, "edge") => vec![(tl!("Left"), "left"), (tl!("Right"), "right")],
|
||||
(
|
||||
vectorcraft_effects::REVOLVE | vectorcraft_effects::EXTRUDE | vectorcraft_effects::INFLATE | vectorcraft_effects::ROTATE,
|
||||
"shadowPosition",
|
||||
) => {
|
||||
vec![(tl!("Behind Object"), "behind"), (tl!("Below Object"), "below")]
|
||||
}
|
||||
(vectorcraft_effects::CLASSIC_EXTRUDE, "surface") => {
|
||||
vec![(tl!("Wireframe"), "wireframe"), (tl!("No Shading"), "none"), (tl!("Diffuse"), "diffuse"), (tl!("Plastic"), "plastic")]
|
||||
}
|
||||
|
||||
@@ -146,10 +146,12 @@ fn start_search(app: &mut VectorcraftApp, d: &mut Dialog, folder: &str) -> Resul
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The files a search found for `row` (`{path, ids}`), best first: the answer's `links` row with
|
||||
/// the same path and ids.
|
||||
/// The files a search found for `row` (`{path?, ids}`), best first: the answer's `links` row with
|
||||
/// the same ids and, when both have one, the same path. A row has no `path` when its file is outside
|
||||
/// an automation client's read roots.
|
||||
fn files_for<'a>(found: &'a [Value], row: &Value) -> &'a [Value] {
|
||||
found.iter().find(|r| r["path"] == row["path"] && r["ids"] == row["ids"]).and_then(|r| r["files"].as_array()).map_or(&[], Vec::as_slice)
|
||||
let same_path = |r: &Value| r["path"].is_null() || row["path"].is_null() || r["path"] == row["path"];
|
||||
found.iter().find(|r| r["ids"] == row["ids"] && same_path(r)).and_then(|r| r["files"].as_array()).map_or(&[], Vec::as_slice)
|
||||
}
|
||||
|
||||
/// The file chosen at the start among `files`: the only one that is the same file, else the only
|
||||
|
||||
@@ -339,3 +339,12 @@ fn find_in_folder_asks_for_the_folder_off_the_ui_thread() {
|
||||
searched(&mut app);
|
||||
assert_eq!(pick(app.ui.dialog.as_ref().unwrap(), "here.png"), &json!(here));
|
||||
}
|
||||
|
||||
/// A row whose path `links.check` left out (its file is outside the read roots) still shows the
|
||||
/// files the search found for its objects.
|
||||
#[test]
|
||||
fn a_row_without_its_path_shows_the_files_found_for_its_objects() {
|
||||
let found = [json!({"name": "a.png", "path": "/elsewhere/a.png", "ids": [3], "files": [{"path": "/root/a.png"}]})];
|
||||
assert_eq!(files_for(&found, &json!({"name": "a.png", "ids": [3]})), [json!({"path": "/root/a.png"})]);
|
||||
assert!(files_for(&found, &json!({"name": "a.png", "ids": [4]})).is_empty());
|
||||
}
|
||||
@@ -333,6 +333,27 @@ fn lighting_controls(ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
store_selected(d, &selected);
|
||||
}
|
||||
});
|
||||
shadow_controls(ui, d);
|
||||
}
|
||||
|
||||
/// The Shadows switch, and below it while the switch is on or mixed, the shadow's Position,
|
||||
/// Distance from Object and Shadow Bounds. The first light casts the shadow, so they are
|
||||
/// unavailable while Shading is off.
|
||||
fn shadow_controls(ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
ui.add_enabled_ui(flag(d, "shade", true) != Some(false), |ui| {
|
||||
toggle(ui, d, "shadow", tl!("Shadows"), false);
|
||||
if flag(d, "shadow", false) == Some(false) {
|
||||
return;
|
||||
}
|
||||
let positions: Vec<(Value, &str)> = super::effect::own_choices(&d.str("__effect"), "shadowPosition", false)
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|(label, value)| (json!(value), label))
|
||||
.collect();
|
||||
choice(ui, d, "shadowPosition", tl!("Position"), &positions);
|
||||
number(ui, d, "shadowDistance", tl!("Distance from Object"), (0.0, 100.0), "%", 1, None);
|
||||
number(ui, d, "shadowBounds", tl!("Shadow Bounds"), (10.0, 400.0), "%", 1, None);
|
||||
});
|
||||
}
|
||||
|
||||
fn lighting_presets(ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
|
||||
@@ -148,6 +148,24 @@ fn command_driven_fields_preview_and_small_windows_keep_the_buttons_reachable()
|
||||
assert!(!h.app.session.in_interaction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_shadows_switch_shows_its_settings_and_turns_the_shadow_on_in_one_undo_step() {
|
||||
let mut h = Harness::with("M100 100 L200 100 L200 200 L100 200 Z", EXTRUDE, vec2(1280.0, 900.0));
|
||||
let before = h.app.session.doc().unwrap().doc.clone();
|
||||
h.settle();
|
||||
h.click(h.rect("Lighting").center());
|
||||
assert!(!h.texts.iter().any(|(s, _)| s == "Shadow Bounds"), "the settings show only while shadows are on");
|
||||
h.click(h.rect("Shadows").center());
|
||||
assert_eq!(h.app.ui.dialog.as_ref().unwrap().fields.get("shadow"), Some(&json!(true)));
|
||||
assert!(h.texts.iter().any(|(s, _)| s == "Distance from Object"));
|
||||
let state = h.app.session.doc().unwrap();
|
||||
let node = state.doc.node(state.selection.objects[0]).unwrap();
|
||||
assert_eq!(node.appearance.effects[0].params["shadow"], json!(true), "the preview draws the shadow");
|
||||
dialogs::confirm(&mut h.app).unwrap();
|
||||
h.app.run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(h.app.session.doc().unwrap().doc, before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lighting_tab_shows_light_fields_and_keeps_the_dialog_compact() {
|
||||
let mut h = Harness::new(vec2(1280.0, 900.0));
|
||||
@@ -158,7 +176,9 @@ fn lighting_tab_shows_light_fields_and_keeps_the_dialog_compact() {
|
||||
assert!(Rect::from_min_size(Pos2::ZERO, h.screen).contains_rect(h.rect(label)));
|
||||
}
|
||||
let panel = h.ctx.memory(|m| m.area_rect(Id::unique(("dialog", "effect")).with("revolve"))).unwrap();
|
||||
assert!(panel.width() < 420.0 && panel.height() < 570.0, "compact lighting controls with light management: {panel:?}");
|
||||
// The Shadows switch adds one 28-point row to the 570-point bound; its settings show only
|
||||
// while it is on.
|
||||
assert!(panel.width() < 420.0 && panel.height() < 600.0, "compact lighting controls with light management: {panel:?}");
|
||||
h.drag(h.rect("Height").center(), vec2(24.0, 0.0));
|
||||
assert_ne!(h.value("lightHeight"), 30.0);
|
||||
h.click(h.response(("revolve-reset", "lightHeight")).center());
|
||||
|
||||
@@ -247,6 +247,8 @@ fn new_width_point_profile_and_compound_target() {
|
||||
assert!(d.fields.get("index").is_none_or(Value::is_null));
|
||||
let actual_t = d.f64("t", -1.0);
|
||||
assert!((actual_t - 0.25).abs() < 1e-6, "compound={compound} pointer={quarter:?} actual_t={actual_t} want=0.25");
|
||||
// The two-point profile is linear: at a quarter its sides are 1.25 and 0.75 of
|
||||
// 1..2 and 0.5..1.5, on a 10pt stroke (5pt half).
|
||||
assert!((d.f64("side1", 0.0) - 6.25).abs() < 1e-6);
|
||||
assert!((d.f64("side2", 0.0) - 3.75).abs() < 1e-6);
|
||||
assert!(!d.bool("linked"));
|
||||
|
||||
@@ -686,6 +686,15 @@ font-technology Variable Променливи
|
||||
Created: Създадено:
|
||||
Crop Изрязване
|
||||
Crop Image Изрязване на изображение
|
||||
Image Tools Инструменти за изображения
|
||||
Remove Background… Премахване на фона…
|
||||
Remove Background Премахване на фона
|
||||
Background Фон
|
||||
Edge Color Цвят на ръба
|
||||
Picked Colors Избрани цветове
|
||||
Corner Color Цвят на ъглите
|
||||
Contiguous Съседни
|
||||
Each Channel Всеки канал
|
||||
Edit Path Редактиране на контура
|
||||
Mask Маска
|
||||
Crop Marks Знаци за изрязване
|
||||
@@ -3143,6 +3152,7 @@ digits Malayalam Малаялам
|
||||
Show Modified Показване на променените
|
||||
Show Note Показване на бележка
|
||||
Show Options Показване на опциите
|
||||
Show Recent Colors Показване на скорошните цветове
|
||||
Show Original Path Показване на оригиналния път
|
||||
Open this dialog with these settings next time Отваряне на този прозорец с тези настройки следващия път
|
||||
Auto-Simplify Автоматично опростяване
|
||||
@@ -3446,6 +3456,8 @@ digits Malayalam Малаялам
|
||||
Scale at Center Мащабиране от центъра
|
||||
Up/Down: Нагоре/надолу:
|
||||
Left/Right: Наляво/надясно:
|
||||
Tate-chu-yoko Up/Down Тате-чу-йоко нагоре/надолу
|
||||
Tate-chu-yoko Left/Right Тате-чу-йоко наляво/надясно
|
||||
Temperature Температура
|
||||
Temperature/Tint… Температура/Нюанс…
|
||||
Template Шаблон
|
||||
@@ -4364,6 +4376,7 @@ axis Both И двете
|
||||
@msg not a stored central record не е съхранен централен запис
|
||||
@msg not a valid plug-in module: {0} не е валиден модул за плъгин: {0}
|
||||
@msg not an EMF or WMF file не е EMF или WMF файл
|
||||
@msg not an HP-GL file не е HP-GL файл
|
||||
@msg not found не е открито
|
||||
@msg not in pattern editing mode не е в режим на редактиране на мостра
|
||||
@msg finish editing the pattern first първо завършете редактирането на мострата
|
||||
@@ -5512,6 +5525,11 @@ theme Appearance Външен вид
|
||||
Softness Мекота
|
||||
Ambient Light Околна светлина
|
||||
Light behind the object Светлина зад обекта
|
||||
Shadows Сенки
|
||||
Distance from Object Разстояние от обекта
|
||||
Shadow Bounds Граници на сянката
|
||||
Behind Object Зад обекта
|
||||
Below Object Под обекта
|
||||
3D Lighting Preset Предварителна настройка за 3D осветление
|
||||
Standard Стандартно
|
||||
Diffuse Дифузно
|
||||
@@ -5889,6 +5907,16 @@ axis Y Axis Y ос
|
||||
@msg message must be text message трябва да бъде текст
|
||||
@msg allowContinue must be true or false allowContinue трябва да бъде true или false
|
||||
@msg Batch cannot play an action with a stop Пакет не може да изпълни действие със спиране
|
||||
Insert Menu Item… Вмъкване на елемент от меню…
|
||||
Insert Menu Item Вмъкване на елемент от меню
|
||||
Menu Item: Елемент от меню:
|
||||
No menu item matches. Няма съвпадащ елемент от меню.
|
||||
Several menu items match. Choose one from the list. Съвпадат няколко елемента от меню. Изберете един от списъка.
|
||||
Items that open a dialog, and File, Window and Help menu items, are not listed. Елементите, които отварят диалогов прозорец, и елементите от менютата „Файл“, „Прозорец“ и „Помощ“ не са изброени.
|
||||
@msg command must be text command трябва да бъде текст
|
||||
@msg no menu item that can be inserted runs this command няма елемент от меню за вмъкване, който изпълнява тази команда
|
||||
@msg the action already has 10,000 steps действието вече има 10 000 стъпки
|
||||
@msg the action set would be larger than 8 MiB наборът от действия ще стане по-голям от 8 MiB
|
||||
Playback Options Опции за възпроизвеждане
|
||||
Accelerated Ускорено
|
||||
Step by Step Стъпка по стъпка
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -196,6 +196,15 @@
|
||||
Expand… Proširi…
|
||||
Expand Appearance Proširi izgled
|
||||
Crop Image Izreži sliku
|
||||
Image Tools Alati za slike
|
||||
Remove Background… Ukloni pozadinu…
|
||||
Remove Background Ukloni pozadinu
|
||||
Background Pozadina
|
||||
Edge Color Boja ruba
|
||||
Picked Colors Odabrane boje
|
||||
Corner Color Boja uglova
|
||||
Contiguous Susjedni
|
||||
Each Channel Svaki kanal
|
||||
Edit Path Uredi putanju
|
||||
Mask Maska
|
||||
Rasterize… Rasteriziraj…
|
||||
@@ -1535,6 +1544,7 @@ digits Malayalam Malajalam
|
||||
Roman Baseline Latinična osnovna linija
|
||||
Select a text object to edit its character attributes. Odaberite tekstualni objekat da uredite njegove atribute znakova.
|
||||
Show Options Prikaži opcije
|
||||
Show Recent Colors Prikaži nedavne boje
|
||||
Color Panel Options Opcije panela boja
|
||||
Small Caps Mala velika slova
|
||||
Small Caps (on the roadmap) Mala velika slova (u planu)
|
||||
@@ -1563,6 +1573,8 @@ digits Malayalam Malajalam
|
||||
Scale at Center Skaliraj od centra
|
||||
Up/Down: Gore/dolje:
|
||||
Left/Right: Lijevo/desno:
|
||||
Tate-chu-yoko Up/Down Tate-chu-yoko gore/dolje
|
||||
Tate-chu-yoko Left/Right Tate-chu-yoko lijevo/desno
|
||||
Underline Podvučeno
|
||||
Vertical Scale % Okomita skala %
|
||||
Axis Osa
|
||||
@@ -4755,6 +4767,7 @@ imageTrace Outline Obris
|
||||
@msg not a stored central record nije sačuvani centralni zapis
|
||||
@msg not a valid plug-in module: {0} nije važeći modul dodatka: {0}
|
||||
@msg not an EMF or WMF file nije EMF ni WMF datoteka
|
||||
@msg not an HP-GL file nije HP-GL datoteka
|
||||
@msg not an export presets file (format `{_1}`) nije datoteka predložaka izvoza (format `{_1}`)
|
||||
@msg not an export presets file: {e} nije datoteka predložaka izvoza: {e}
|
||||
@msg not found nije pronađeno
|
||||
@@ -5596,6 +5609,11 @@ mark-style Japanese Japanski
|
||||
Softness Mekoća
|
||||
Ambient Light Ambijentalno svjetlo
|
||||
Light behind the object Svjetlo iza objekta
|
||||
Shadows Sjene
|
||||
Distance from Object Udaljenost od objekta
|
||||
Shadow Bounds Granice sjene
|
||||
Behind Object Iza objekta
|
||||
Below Object Ispod objekta
|
||||
3D Lighting Preset Predložak 3D osvjetljenja
|
||||
Standard Standardno
|
||||
Diffuse Difuzno
|
||||
@@ -5928,6 +5946,16 @@ axis Y Axis Y osa
|
||||
@msg message must be text message mora biti tekst
|
||||
@msg allowContinue must be true or false allowContinue mora biti true ili false
|
||||
@msg Batch cannot play an action with a stop Paketna obrada ne može reprodukovati akciju sa zaustavljanjem
|
||||
Insert Menu Item… Umetni stavku menija…
|
||||
Insert Menu Item Umetni stavku menija
|
||||
Menu Item: Stavka menija:
|
||||
No menu item matches. Nijedna stavka menija ne odgovara.
|
||||
Several menu items match. Choose one from the list. Odgovara više stavki menija. Izaberite jednu sa liste.
|
||||
Items that open a dialog, and File, Window and Help menu items, are not listed. Stavke koje otvaraju dijalog i stavke menija Datoteka, Prozor i Pomoć nisu navedene.
|
||||
@msg command must be text command mora biti tekst
|
||||
@msg no menu item that can be inserted runs this command nijedna stavka menija koja se može umetnuti ne pokreće ovu naredbu
|
||||
@msg the action already has 10,000 steps akcija već ima 10.000 koraka
|
||||
@msg the action set would be larger than 8 MiB skup akcija bio bi veći od 8 MiB
|
||||
Playback Options Opcije reprodukcije
|
||||
Accelerated Ubrzano
|
||||
Step by Step Korak po korak
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
Loaded 100 of 150 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user