mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 04:38:29 +00:00
Merge main into feat/ai-editing-data
This commit is contained in:
@@ -247,7 +247,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Replace Symbol",
|
||||
[],
|
||||
None,
|
||||
"{name, ids?|id?} swap the symbol of the selected instances (they keep their appearance changes to the objects at paths a dynamic new symbol's art has) → {ids}",
|
||||
"{name, ids?|id?} swap the symbol of the selected instances (an instance given a different symbol drops its appearance changes; one given its own symbol keeps them) → {ids}",
|
||||
has_selection,
|
||||
symbol_replace
|
||||
),
|
||||
|
||||
@@ -590,14 +590,19 @@ pub(super) fn symbol_replace(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
s.edit("Replace Symbol", |d, _| {
|
||||
let new = natural_size(d, &name);
|
||||
for id in &ids {
|
||||
let old = match d.node(*id).map(|n| &n.kind) {
|
||||
Some(NodeKind::SymbolInstance { symbol, .. }) => natural_size(d, symbol),
|
||||
let (old, other) = match d.node(*id).map(|n| &n.kind) {
|
||||
Some(NodeKind::SymbolInstance { symbol, .. }) => (natural_size(d, symbol), *symbol != name),
|
||||
_ => continue,
|
||||
};
|
||||
if let Some(NodeKind::SymbolInstance { symbol, xf }) = d.node_mut(*id).map(|n| &mut n.kind) {
|
||||
let Some(n) = d.node_mut(*id) else { continue };
|
||||
if let NodeKind::SymbolInstance { symbol, xf } = &mut n.kind {
|
||||
*symbol = name.clone();
|
||||
*xf *= Affine::scale_non_uniform(new.0 / old.0, new.1 / old.1);
|
||||
}
|
||||
// An override's path names an object of the old symbol's art.
|
||||
if other && !n.extras().symbol_overrides.is_empty() {
|
||||
n.extras_mut().symbol_overrides.clear();
|
||||
}
|
||||
}
|
||||
fit_overrides(d, &ids);
|
||||
Ok(())
|
||||
|
||||
@@ -12,7 +12,7 @@ use serde_json::json;
|
||||
use vectorcraft_affinity::model::{self as af, Align as AfAlign, Blend, Kind, Pixels};
|
||||
use vectorcraft_affinity::paint::{self as ap, Cap, GradientKind as AfKind, Join};
|
||||
use vectorcraft_color::{BlendMode, Color, Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop, Paint};
|
||||
use vectorcraft_doc::appearance::{Appearance, AppearanceItem, Dash, Effect, FillLayer, LineCap, LineJoin, StrokeAlign, StrokeLayer};
|
||||
use vectorcraft_doc::appearance::{Appearance, AppearanceItem, Dash, Effect, FillLayer, LineCap, LineJoin, StrokeAlign, StrokeLayer, WidthProfile};
|
||||
use vectorcraft_doc::geom::{FillRule, PathData, shapes};
|
||||
use vectorcraft_doc::text::{CharStyle, FirstBaseline, Justify, TextKind, TextObject, TextRun, VerticalAlign};
|
||||
use vectorcraft_doc::{Artboard, Document, ImageObject, LayerColor, Node, NodeKind, OpacityMask};
|
||||
|
||||
@@ -4,7 +4,7 @@ use super::text::Placement;
|
||||
use super::{
|
||||
AfKind, Affine, Appearance, AppearanceItem, Arc, BezPath, Builder, Cap, Color, Dash, Effect, FillLayer, Gradient, GradientGeom, GradientKind,
|
||||
GradientPaint, GradientStop, ImageObject, Join, LineCap, LineJoin, NodeKind, Paint, PathData, Pixels, Point, StrokeAlign, StrokeLayer,
|
||||
TextObject, Vec2, af, ap, json,
|
||||
TextObject, Vec2, WidthProfile, af, ap, json,
|
||||
};
|
||||
|
||||
impl Builder {
|
||||
@@ -232,6 +232,7 @@ impl Builder {
|
||||
offset: phase * self.k,
|
||||
align_corners: false,
|
||||
});
|
||||
l.profile = s.profile.as_ref().map(|p| WidthProfile { points: p.iter().map(|&(t, w)| (t, w, w)).collect() });
|
||||
Some(l)
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
//! layer's hidden objects go to a hidden layer of their own just above it, with `omit` they are
|
||||
//! left out, with `visible` they show where they are. Type is written as text the format's reader
|
||||
//! lays out again, in the fonts it names; `outlineText` writes its outlines instead (as type that
|
||||
//! 8-bit text can't hold always is).
|
||||
//! 8-bit text can't hold and type with rotated characters always are).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
|
||||
@@ -127,7 +127,7 @@ pub const AI_OPTIONS: &[FormatOption] = &[
|
||||
name: "outlineText",
|
||||
ty: "boolean",
|
||||
default: "false",
|
||||
description: "with aiVersion 7, type as its outlines instead of text (which apps reading the file set in the fonts it names: they need them installed); type 8-bit text can't hold (characters outside Windows Latin-1, vertical type, ruby, inline graphics, gradient or pattern fills) is always outlines",
|
||||
description: "with aiVersion 7, type as its outlines instead of text (which apps reading the file set in the fonts it names: they need them installed); type 8-bit text can't hold (characters outside Windows Latin-1, vertical type, ruby, inline graphics, gradient or pattern fills) and type with rotated characters are always outlines",
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
@@ -300,6 +300,176 @@ mod native {
|
||||
}
|
||||
}
|
||||
|
||||
/// A version-12 document holding an open curve with an 8 px stroke whose pressure curve (`LDeP`)
|
||||
/// is made of `records`.
|
||||
fn pressure_document(records: Vec<Vec<u8>>) -> Vec<u8> {
|
||||
pressure_documents(records, 1)
|
||||
}
|
||||
|
||||
/// [`pressure_document`] with `copies` curves, all linking the first one's fill and stroke.
|
||||
fn pressure_documents(records: Vec<Vec<u8>>, copies: u32) -> Vec<u8> {
|
||||
let mut style = Some((
|
||||
solid(24, rgba(0.0, 0.0, 0.0, 1.0)),
|
||||
F::Def(
|
||||
27,
|
||||
vec![tag(b"LDsc")],
|
||||
vec![
|
||||
(tag(b"LDeL"), F::Def(28, vec![tag(b"LSty")], vec![(tag(b"Wght"), F::F64(8.0))])),
|
||||
(tag(b"LDSc"), F::Bool(false)),
|
||||
(tag(b"LDeP"), F::Obj(tag(b"CrvD"), vec![(tag(b"Data"), F::Pos(vec![F::U32(0), F::Bool(false), F::Records(18, records)]))])),
|
||||
],
|
||||
),
|
||||
));
|
||||
let lines = (0..copies)
|
||||
.map(|i| {
|
||||
let (fill, line) = style.take().unwrap_or((F::Ref(24), F::Ref(27)));
|
||||
F::Def(
|
||||
if i == 0 { 20 } else { 1000 + i },
|
||||
vec![tag(b"PCrv"), tag(b"VNod"), tag(b"Node")],
|
||||
vec![
|
||||
(
|
||||
tag(b"Crvs"),
|
||||
F::Obj(
|
||||
tag(b"PCvD"),
|
||||
vec![(
|
||||
tag(b"Data"),
|
||||
F::Pos(vec![
|
||||
F::U8(0),
|
||||
F::U32(1),
|
||||
F::Bool(false),
|
||||
F::Records(18, vec![node(0.0, 0.0, 1, 0), node(100.0, 0.0, 1, 0)]),
|
||||
]),
|
||||
)],
|
||||
),
|
||||
),
|
||||
(tag(b"LIFl"), F::Shared(vec![fill])),
|
||||
(tag(b"LILn"), F::Shared(vec![line])),
|
||||
],
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let spread = F::Def(2, vec![tag(b"Sprd")], vec![(tag(b"SprB"), F::F64s(vec![0.0, 0.0, 200.0, 100.0])), (tag(b"Chld"), F::Shared(lines))]);
|
||||
let doc = synth::stream(&[
|
||||
(tag(b"UVCn"), F::Obj(tag(b"UVCn"), vec![(tag(b"UPPI"), F::F64(72.0))])),
|
||||
(tag(b"DocR"), F::Def(1, vec![tag(b"DocN")], vec![(tag(b"Chld"), F::Shared(vec![spread]))])),
|
||||
]);
|
||||
synth::container(&[("doc.dat", &doc, Method::Zstd)], Some(&png()))
|
||||
}
|
||||
|
||||
/// The width profile of the only stroked path in `d`.
|
||||
fn stroke_of(d: &vectorcraft_doc::Document) -> vectorcraft_doc::appearance::StrokeLayer {
|
||||
let mut found = None;
|
||||
d.walk(|n| {
|
||||
if let Some(AppearanceItem::Stroke(k)) = n.appearance.items.iter().find(|i| matches!(i, AppearanceItem::Stroke(_))) {
|
||||
found = Some(k.clone());
|
||||
}
|
||||
});
|
||||
found.unwrap_or_else(|| panic!("no stroke in {:?}", d.layers))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pressure_curve_becomes_the_strokes_width_profile() {
|
||||
// Thin at the start, full width from half way: anchor (0, 0), handles (0.25, 0.5) and
|
||||
// (0.5, 1), anchor (0.5, 1), then straight on to anchor (1, 1).
|
||||
let curve = vec![node(0.0, 0.0, 4, 0), node(0.25, 0.5, 0, 1), node(0.5, 1.0, 0, 2), node(0.5, 1.0, 4, 0), node(1.0, 1.0, 4, 0)];
|
||||
let r = load("pressure.af", &pressure_document(curve)).unwrap();
|
||||
assert!(!r.preview_only);
|
||||
assert!(r.warnings.iter().all(|w| !w.contains("pressure")), "{:?}", r.warnings);
|
||||
let k = stroke_of(&r.doc);
|
||||
assert_eq!(k.width, 8.0);
|
||||
let profile = k.profile.unwrap_or_else(|| panic!("the pressure curve is read: {:?}", r.warnings));
|
||||
assert_eq!(profile.points.first(), Some(&(0.0, 0.0, 0.0)));
|
||||
assert_eq!(profile.points.last(), Some(&(1.0, 1.0, 1.0)));
|
||||
assert!(profile.points.windows(2).all(|w| w[0].0 <= w[1].0), "positions go forward: {:?}", profile.points);
|
||||
let (left, right) = profile.at(0.25);
|
||||
assert!(left > 0.25 && left < 1.0 && left == right, "{left} {right}");
|
||||
assert_eq!(profile.at(0.75), (1.0, 1.0));
|
||||
|
||||
// Widths past the full weight are held at it.
|
||||
let huge = vec![node(0.0, 0.0, 4, 0), node(0.5, 1e300, 4, 0), node(1.0, 0.0, 4, 0)];
|
||||
let profile = stroke_of(&load("huge.af", &pressure_document(huge)).unwrap().doc).profile.unwrap();
|
||||
assert!(profile.points.iter().all(|p| (0.0..=1.0).contains(&p.1)), "{:?}", profile.points);
|
||||
assert_eq!(profile.at(0.5), (1.0, 1.0));
|
||||
|
||||
// Each segment is sampled inside too: anchors at full width whose handles dip to nothing
|
||||
// still narrow the stroke, with 256 segments.
|
||||
let mut dips = vec![node(0.0, 1.0, 4, 0)];
|
||||
for i in 1..=256 {
|
||||
let (a, b) = (f64::from(i - 1) / 256.0, f64::from(i) / 256.0);
|
||||
dips.extend([node(a + (b - a) / 3.0, 0.0, 0, 1), node(b - (b - a) / 3.0, 0.0, 0, 2), node(b, 1.0, 4, 0)]);
|
||||
}
|
||||
let profile = stroke_of(&load("dips.af", &pressure_document(dips)).unwrap().doc).profile.unwrap();
|
||||
assert!(profile.points.iter().any(|p| p.1 < 0.5), "{:?}", profile.points);
|
||||
|
||||
// A curve of more than 256 segments is left out, with a warning.
|
||||
let long: Vec<Vec<u8>> = (0..=300).map(|i| node(f64::from(i) / 300.0, f64::from(i % 2), 4, 0)).collect();
|
||||
let r = load("long.af", &pressure_document(long)).unwrap();
|
||||
assert!(stroke_of(&r.doc).profile.is_none());
|
||||
assert!(r.warnings.iter().any(|w| w.contains("pressure curve can't be read")), "{:?}", r.warnings);
|
||||
|
||||
// A pressure curve at full width all along leaves the stroke plain.
|
||||
let flat = vec![node(0.0, 1.0, 4, 0), node(1.0, 1.0, 4, 0)];
|
||||
let r = load("flat.af", &pressure_document(flat)).unwrap();
|
||||
assert!(stroke_of(&r.doc).profile.is_none());
|
||||
assert!(r.warnings.iter().all(|w| !w.contains("pressure")), "{:?}", r.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dip_between_the_samples_of_a_long_pressure_curve_is_kept() {
|
||||
// 256 segments at full width, except one whose handles sit at widths 0 and 2: its width is
|
||||
// 1 at its middle and end but 0.71875 a quarter of the way along.
|
||||
let mut curve = vec![node(0.0, 1.0, 4, 0)];
|
||||
for i in 1..=256 {
|
||||
let (a, b) = (f64::from(i - 1) / 256.0, f64::from(i) / 256.0);
|
||||
let (h1, h2) = if i == 100 { (0.0, 2.0) } else { (1.0, 1.0) };
|
||||
curve.extend([node(a + (b - a) / 3.0, h1, 0, 1), node(b - (b - a) / 3.0, h2, 0, 2), node(b, 1.0, 4, 0)]);
|
||||
}
|
||||
let profile = stroke_of(&load("dip.af", &pressure_document(curve)).unwrap().doc).profile.unwrap_or_else(|| panic!("the dip is kept"));
|
||||
let narrowest = profile.points.iter().map(|p| p.1).fold(1.0, f64::min);
|
||||
assert!((narrowest - 0.71).abs() < 0.02, "{narrowest}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pressure_curve_just_under_full_width_is_kept() {
|
||||
let curve = vec![node(0.0, 0.9995, 4, 0), node(1.0, 0.9995, 4, 0)];
|
||||
let profile = stroke_of(&load("near.af", &pressure_document(curve)).unwrap().doc).profile.unwrap_or_else(|| panic!("the curve is kept"));
|
||||
assert_eq!(profile.at(0.5), (0.9995, 0.9995));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pressure_curves_stay_within_the_points_of_one_document() {
|
||||
// One 256-segment curve linked from 2100 strokes: more than a million points in all.
|
||||
let mut curve = vec![node(0.0, 1.0, 4, 0)];
|
||||
for i in 1..=256 {
|
||||
let (a, b) = (f64::from(i - 1) / 256.0, f64::from(i) / 256.0);
|
||||
curve.extend([node(a + (b - a) / 3.0, 0.0, 0, 1), node(b - (b - a) / 3.0, 0.0, 0, 2), node(b, 1.0, 4, 0)]);
|
||||
}
|
||||
let r = load("many.af", &pressure_documents(curve, 2100)).unwrap();
|
||||
let mut profiles = Vec::new();
|
||||
r.doc.walk(|n| {
|
||||
if let Some(AppearanceItem::Stroke(k)) = n.appearance.items.iter().find(|i| matches!(i, AppearanceItem::Stroke(_))) {
|
||||
profiles.push(k.profile.as_ref().map_or(0, |p| p.points.len()));
|
||||
}
|
||||
});
|
||||
assert_eq!(profiles.len(), 2100);
|
||||
assert!(profiles.iter().sum::<usize>() <= 1 << 20, "{}", profiles.iter().sum::<usize>());
|
||||
assert!(profiles.iter().filter(|n| **n > 0).count() > 1000);
|
||||
assert!(profiles.contains(&0));
|
||||
assert!(r.warnings.iter().any(|w| w.contains("pressure points one document may hold")), "{:?}", r.warnings);
|
||||
|
||||
// Curves at full width count too, so linking one from many strokes can't repeat its work
|
||||
// without bound.
|
||||
let flat: Vec<Vec<u8>> = (0..=768).map(|i| node(f64::from(i) / 768.0, 1.0, if i % 3 == 0 { 4 } else { 0 }, (i % 3) as u8)).collect();
|
||||
let r = load("flat-many.af", &pressure_documents(flat, 2100)).unwrap();
|
||||
assert!(r.warnings.iter().any(|w| w.contains("pressure points one document may hold")), "{:?}", r.warnings);
|
||||
|
||||
// So do curves that can't be read: 768 segments, more than a curve may have.
|
||||
let long: Vec<Vec<u8>> = (0..=768).map(|i| node(f64::from(i) / 768.0, f64::from(i % 2), 4, 0)).collect();
|
||||
let r = load("long-many.af", &pressure_documents(long, 2100)).unwrap();
|
||||
assert!(r.warnings.iter().any(|w| w.contains("pressure curve can't be read")), "{:?}", r.warnings);
|
||||
assert!(r.warnings.iter().any(|w| w.contains("pressure points one document may hold")), "{:?}", r.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_documents_save_reopen_export_place_and_template() {
|
||||
let bytes = document(Method::Zstd);
|
||||
|
||||
@@ -111,6 +111,28 @@ fn redefining_the_symbol_keeps_the_changes_whose_objects_remain() {
|
||||
assert_eq!(overrides(&s, inst), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replace_symbol_drops_the_changes_made_for_the_old_symbol() {
|
||||
let mut s = session();
|
||||
// "Other": two blue squares in the same places, the instance taken off the canvas.
|
||||
let (a, b) = (square(&mut s, 10.0), square(&mut s, 60.0));
|
||||
let first = id(&s.execute("symbol.new", &json!({"name": "Other", "ids": [a.0, b.0], "type": "dynamic"})).unwrap());
|
||||
s.execute("edit.clear", &json!({"ids": [first.0]})).unwrap();
|
||||
let inst = pair(&mut s, "dynamic");
|
||||
s.execute("symbol.instance.setAppearance", &json!({"id": inst.0, "path": [1], "fill": "#ff0000"})).unwrap();
|
||||
// The same symbol: nothing changes.
|
||||
s.execute("symbol.replace", &json!({"name": "Pair", "ids": [inst.0]})).unwrap();
|
||||
assert_eq!(overrides(&s, inst), 1);
|
||||
assert_eq!(pixel(&s, 80, 30), RED);
|
||||
s.execute("symbol.replace", &json!({"name": "Other", "ids": [inst.0]})).unwrap();
|
||||
assert_eq!(overrides(&s, inst), 0, "the change named an object of Pair's art");
|
||||
assert_eq!(pixel(&s, 80, 30), BLUE, "Other's art draws as the symbol has it");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(matches!(&node(&s, inst).kind, NodeKind::SymbolInstance { symbol, .. } if symbol == "Pair"));
|
||||
assert_eq!(overrides(&s, inst), 1);
|
||||
assert_eq!(pixel(&s, 80, 30), RED);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_and_static_drop_the_changes_and_undo_brings_them_back() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -140,6 +140,35 @@ fn spot_gradients_export_as_separation_shadings() {
|
||||
assert!(warnings.iter().any(|w| w.contains("spot")), "{warnings:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn white_stops_linked_to_process_swatches_are_paper_in_spot_gradients() {
|
||||
let (mut s, _) = setup();
|
||||
run(&mut s, "swatch.new", json!({"name": "Paper", "color": "#ffffff", "global": true}));
|
||||
let ink_to_paper = json!({"stops": [{"offset": 0, "swatch": "Ink", "tint": 80}, {"offset": 1, "swatch": "Paper"}]});
|
||||
// As Ink → unlinked white: a Separation shading from 80 % to 0 %.
|
||||
run(&mut s, "paint.editGradient", ink_to_paper.clone());
|
||||
let (pdf, warnings) = pdf_of(&s);
|
||||
assert!(pdf.contains("/ShadingType 2") && pdf.contains("/Separation/Ink/DeviceCMYK"), "a Separation shading");
|
||||
assert!(!pdf.contains("/DeviceN") && pdf.contains("/C0[0.8]/C1[0]"), "the shading's tints");
|
||||
assert!(!warnings.iter().any(|w| w.contains("spot")), "{warnings:?}");
|
||||
// A Perceptual one too.
|
||||
run(&mut s, "paint.editGradient", with(ink_to_paper, json!({"method": "perceptual"})));
|
||||
let (pdf, _) = pdf_of(&s);
|
||||
assert!(!pdf.contains("/Subtype/Image") && !pdf.contains("/DeviceN") && pdf.contains("/Separation/Ink/DeviceCMYK"), "Perceptual");
|
||||
// With process yellow, the linked white stop adds no ink: Yellow and Ink only.
|
||||
let yellow = json!({"c": 0, "m": 0, "y": 0.6, "k": 0});
|
||||
let three =
|
||||
json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 0.5, "swatch": "Paper"}, {"offset": 1, "color": yellow}], "method": "classic"});
|
||||
run(&mut s, "paint.editGradient", three);
|
||||
let (pdf, _) = pdf_of(&s);
|
||||
assert!(pdf.contains("/DeviceN[/Yellow/Ink]/DeviceCMYK") && pdf.contains("/C0[0 1]/C1[0 0]"), "Yellow and Ink");
|
||||
// A white spot ink is an ink, not paper: a DeviceN shading of both spot inks.
|
||||
run(&mut s, "swatch.new", json!({"name": "Opaque", "color": "#ffffff", "spot": true}));
|
||||
run(&mut s, "paint.editGradient", json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "swatch": "Opaque"}]}));
|
||||
let (pdf, _) = pdf_of(&s);
|
||||
assert!(pdf.contains("/DeviceN[/Ink/Opaque]/DeviceCMYK"), "a white spot ink keeps its plate");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spot_gradients_mixed_with_other_inks_export_as_device_n_shadings() {
|
||||
let (mut s, _) = setup();
|
||||
|
||||
Reference in New Issue
Block a user