mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
.ai export: dynamic symbol changes stay on the part they name
A dynamic symbol instance keeps its appearance changes in the editing data under each changed part's child index. The copy leaves out paths and compound paths without anchors, so a change could reopen on a different part or on none, and changes over 1 MiB were dropped with no warning. Such an instance is now written as its art, with the symbol-art warning, when its changes take more than 1 MiB, when a left-out child comes at or before a changed part's index in its group, or when the symbol's root group is left with one kept child. Other instances stay live with their changes.
This commit is contained in:
1 parent
68dbab8b90
commit
4136de92c1
4 files changed
+366
-14
No files matched your search
@@ -16261,6 +16261,281 @@ fn dynamic_symbols_and_their_instance_changes_survive_a_round_trip() {
|
||||
assert!(got[2].is_empty());
|
||||
}
|
||||
|
||||
/// A document with one instance of dynamic symbol "Leaf", whose art is `art`, with changes `overrides`.
|
||||
fn dynamic_instance_doc(art: impl FnOnce(&mut Document) -> Node, overrides: Vec<vectorcraft_doc::SymbolOverride>) -> Document {
|
||||
let mut doc = Document::new(200.0, 100.0);
|
||||
let art = art(&mut doc);
|
||||
doc.symbols.push(Symbol { dynamic: true, name: "Leaf".into(), art: Arc::new(art) });
|
||||
let layer = doc.layers[0].id;
|
||||
let mut i = instance(&mut doc, "Leaf", Affine::translate((10.0, 10.0)));
|
||||
i.extras_mut().symbol_overrides = overrides;
|
||||
doc.insert(Some(layer), usize::MAX, i).unwrap();
|
||||
doc
|
||||
}
|
||||
|
||||
/// Exports `doc`, checks that its one instance is written as its art with [`writer::SYMBOL_ART`], and
|
||||
/// returns the fills of the paths it reads back as and the opacities of all its objects.
|
||||
fn dynamic_instance_as_art(doc: &Document) -> (Vec<Paint>, Vec<f32>) {
|
||||
let out = export_ai_editing(doc, &opts()).unwrap();
|
||||
assert!(out.warnings.iter().any(|w| w == writer::SYMBOL_ART), "{:?}", out.warnings);
|
||||
assert!(!out.warnings.iter().any(|w| w == writer::SYMBOL_CHANGES), "{:?}", out.warnings);
|
||||
let back = read_back(doc);
|
||||
let kids = back.layers[0].children().unwrap();
|
||||
assert!(!kids.iter().any(|n| matches!(n.kind, NodeKind::SymbolInstance { .. })), "the instance stays live");
|
||||
let (mut fills, mut opacities) = (vec![], vec![]);
|
||||
for k in kids {
|
||||
vectorcraft_doc::swatches::walk_with_masks(k, &mut |n| {
|
||||
opacities.push(n.opacity);
|
||||
if matches!(n.kind, NodeKind::Path { .. }) {
|
||||
fills.extend(n.appearance.items.iter().filter_map(|i| match i {
|
||||
AppearanceItem::Fill(f) => Some(f.paint.clone()),
|
||||
_ => None,
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
(fills, opacities)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_whose_changed_part_follows_an_empty_path_is_written_as_its_art() {
|
||||
let red = Color::rgb(1.0, 0.0, 0.0);
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| {
|
||||
let empty = Node::path(doc.alloc_id(), PathData::default(), Appearance::basic(solid(LEAF), Paint::None, 1.0));
|
||||
let sq = square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF);
|
||||
Node::group(doc.alloc_id(), vec![Arc::new(empty), Arc::new(sq)])
|
||||
},
|
||||
vec![vectorcraft_doc::SymbolOverride { path: vec![1], fill: Some(solid(red)), ..Default::default() }],
|
||||
);
|
||||
let (fills, _) = dynamic_instance_as_art(&doc);
|
||||
assert_eq!(fills.len(), 1, "{fills:?}");
|
||||
assert_ne!(fills[0], solid(LEAF), "the square keeps its changed fill");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_whose_art_loses_an_empty_path_after_the_changed_part_is_written_as_its_art() {
|
||||
let red = Color::rgb(1.0, 0.0, 0.0);
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| {
|
||||
let sq = square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF);
|
||||
let empty = Node::path(doc.alloc_id(), PathData::default(), Appearance::basic(solid(LEAF), Paint::None, 1.0));
|
||||
Node::group(doc.alloc_id(), vec![Arc::new(sq), Arc::new(empty)])
|
||||
},
|
||||
vec![vectorcraft_doc::SymbolOverride { path: vec![0], fill: Some(solid(red)), ..Default::default() }],
|
||||
);
|
||||
let (fills, _) = dynamic_instance_as_art(&doc);
|
||||
assert_eq!(fills.len(), 1, "{fills:?}");
|
||||
assert_ne!(fills[0], solid(LEAF), "the square keeps its changed fill");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_stays_live_when_an_empty_path_after_its_changed_part_moves_nothing() {
|
||||
let red = Color::rgb(1.0, 0.0, 0.0);
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| {
|
||||
let a = square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF);
|
||||
let b = square(doc, Rect::new(20.0, 0.0, 30.0, 10.0), LEAF);
|
||||
let empty = Node::path(doc.alloc_id(), PathData::default(), Appearance::basic(solid(LEAF), Paint::None, 1.0));
|
||||
Node::group(doc.alloc_id(), vec![Arc::new(a), Arc::new(b), Arc::new(empty)])
|
||||
},
|
||||
vec![vectorcraft_doc::SymbolOverride { path: vec![0], fill: Some(solid(red)), ..Default::default() }],
|
||||
);
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
assert_eq!(out.warnings, [writer::SYMBOL_CHANGES]);
|
||||
let back = read_back(&doc);
|
||||
let kids = back.layers[0].children().unwrap();
|
||||
assert!(matches!(kids[0].kind, NodeKind::SymbolInstance { .. }));
|
||||
assert_eq!(kids[0].extras().symbol_overrides, doc.layers[0].children().unwrap()[0].extras().symbol_overrides);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_with_a_named_or_transparent_root_and_one_kept_child_is_written_as_its_art() {
|
||||
for named in [true, false] {
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| {
|
||||
let sq = square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF);
|
||||
let empty = Node::path(doc.alloc_id(), PathData::default(), Appearance::basic(solid(LEAF), Paint::None, 1.0));
|
||||
let mut root = Node::group(doc.alloc_id(), vec![Arc::new(sq), Arc::new(empty)]);
|
||||
if named {
|
||||
root.name = Some("Branch".into());
|
||||
} else {
|
||||
root.opacity = 0.5;
|
||||
}
|
||||
root
|
||||
},
|
||||
vec![vectorcraft_doc::SymbolOverride { path: vec![0], fill: Some(solid(Color::rgb(1.0, 0.0, 0.0))), ..Default::default() }],
|
||||
);
|
||||
let (fills, opacities) = dynamic_instance_as_art(&doc);
|
||||
assert_eq!(fills.len(), 1, "{fills:?}");
|
||||
assert_ne!(fills[0], solid(LEAF), "the square keeps its changed fill");
|
||||
if !named {
|
||||
assert!(opacities.iter().any(|o| (o - 0.5).abs() < 1e-3), "{opacities:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_with_a_root_override_and_one_kept_child_is_written_as_its_art() {
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| {
|
||||
let empty = Node::path(doc.alloc_id(), PathData::default(), Appearance::default());
|
||||
let mut sq = square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF);
|
||||
sq.opacity = 0.5;
|
||||
Node::group(doc.alloc_id(), vec![Arc::new(empty), Arc::new(sq)])
|
||||
},
|
||||
vec![vectorcraft_doc::SymbolOverride { path: vec![], opacity: Some(0.5), ..Default::default() }],
|
||||
);
|
||||
dynamic_instance_as_art(&doc);
|
||||
let back = read_back(&doc);
|
||||
fn path_opacities(n: &Node, parent: f32, out: &mut Vec<f32>) {
|
||||
let opacity = parent * n.opacity;
|
||||
if matches!(n.kind, NodeKind::Path { .. }) {
|
||||
out.push(opacity);
|
||||
}
|
||||
for child in n.children().into_iter().flatten() {
|
||||
path_opacities(child, opacity, out);
|
||||
}
|
||||
}
|
||||
let mut opacities = vec![];
|
||||
for child in back.layers[0].children().unwrap() {
|
||||
path_opacities(child, 1.0, &mut opacities);
|
||||
}
|
||||
assert_eq!(opacities.len(), 1, "{opacities:?}");
|
||||
assert!((opacities[0] - 0.25).abs() < 1e-3, "{opacities:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_whose_override_targets_an_omitted_child_is_written_as_its_art() {
|
||||
for nested in [false, true] {
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| {
|
||||
let empty = Node::path(doc.alloc_id(), PathData::default(), Appearance::default());
|
||||
let a = square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF);
|
||||
let b = square(doc, Rect::new(20.0, 0.0, 30.0, 10.0), LEAF);
|
||||
let root = Node::group(doc.alloc_id(), vec![Arc::new(empty), Arc::new(a), Arc::new(b)]);
|
||||
if nested { Node::group(doc.alloc_id(), vec![Arc::new(root)]) } else { root }
|
||||
},
|
||||
vec![vectorcraft_doc::SymbolOverride {
|
||||
path: if nested { vec![0, 0] } else { vec![0] },
|
||||
fill: Some(solid(Color::rgb(1.0, 0.0, 0.0))),
|
||||
..Default::default()
|
||||
}],
|
||||
);
|
||||
let (fills, _) = dynamic_instance_as_art(&doc);
|
||||
assert_eq!(fills.len(), 2, "{fills:?}");
|
||||
assert!(fills.iter().all(|f| *f == solid(LEAF)), "the squares keep their original fills: {fills:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_with_a_root_note_and_one_kept_child_is_written_as_its_art() {
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| {
|
||||
let sq = square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF);
|
||||
let empty = Node::path(doc.alloc_id(), PathData::default(), Appearance::default());
|
||||
let mut root = Node::group(doc.alloc_id(), vec![Arc::new(sq), Arc::new(empty)]);
|
||||
root.extras_mut().attrs = Some(Box::new(vectorcraft_doc::ObjectAttributes { note: "Branch note".into(), ..Default::default() }));
|
||||
root
|
||||
},
|
||||
vec![vectorcraft_doc::SymbolOverride { path: vec![0], fill: Some(solid(Color::rgb(1.0, 0.0, 0.0))), ..Default::default() }],
|
||||
);
|
||||
let (fills, _) = dynamic_instance_as_art(&doc);
|
||||
assert_eq!(fills.len(), 1, "{fills:?}");
|
||||
assert_ne!(fills[0], solid(LEAF), "the square keeps its changed fill");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dynamic_instances_on_a_large_canvas_or_in_clipped_art_do_not_serialize_overrides() {
|
||||
for clipped in [false, true] {
|
||||
let o = vectorcraft_doc::SymbolOverride { fill: Some(solid(Color::rgb(1.0, 0.0, 0.0))), ..Default::default() };
|
||||
let mut doc = dynamic_instance_doc(|doc| square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF), vec![o; 40_000]);
|
||||
if clipped {
|
||||
let layer = doc.layers[0].id;
|
||||
let id = doc.layers[0].children().unwrap()[0].id;
|
||||
let art = doc.remove(id).unwrap();
|
||||
let outline = square(&mut doc, Rect::new(0.0, 0.0, 15.0, 15.0), LEAF);
|
||||
let clip = group(&mut doc, vec![outline], false);
|
||||
let g = group(&mut doc, vec![clip, (*art).clone()], true);
|
||||
doc.insert(Some(layer), usize::MAX, g).unwrap();
|
||||
} else {
|
||||
doc = on_large_canvas(doc);
|
||||
}
|
||||
writer::OVERRIDE_SERIALIZATIONS.with(|count| count.set(0));
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
writer::OVERRIDE_SERIALIZATIONS.with(|count| assert_eq!(count.get(), 0));
|
||||
assert!(out.warnings.iter().any(|w| w == writer::SYMBOL_ART), "{:?}", out.warnings);
|
||||
assert!(!out.warnings.iter().any(|w| w == writer::SYMBOL_CHANGES), "{:?}", out.warnings);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_live_dynamic_instance_serializes_its_overrides_once() {
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF),
|
||||
vec![vectorcraft_doc::SymbolOverride { opacity: Some(0.5), ..Default::default() }],
|
||||
);
|
||||
writer::OVERRIDE_SERIALIZATIONS.with(|count| count.set(0));
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
writer::OVERRIDE_SERIALIZATIONS.with(|count| assert_eq!(count.get(), 1));
|
||||
assert_eq!(out.warnings, [writer::SYMBOL_CHANGES]);
|
||||
let back = read_back(&doc);
|
||||
let got = &back.layers[0].children().unwrap()[0];
|
||||
assert!(matches!(got.kind, NodeKind::SymbolInstance { .. }));
|
||||
assert_eq!(got.extras().symbol_overrides, doc.layers[0].children().unwrap()[0].extras().symbol_overrides);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_stays_live_with_an_out_of_range_override_after_an_empty_path() {
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| {
|
||||
let a = square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF);
|
||||
let b = square(doc, Rect::new(20.0, 0.0, 30.0, 10.0), LEAF);
|
||||
let empty = Node::path(doc.alloc_id(), PathData::default(), Appearance::basic(solid(LEAF), Paint::None, 1.0));
|
||||
Node::group(doc.alloc_id(), vec![Arc::new(a), Arc::new(b), Arc::new(empty)])
|
||||
},
|
||||
vec![vectorcraft_doc::SymbolOverride { path: vec![5], fill: Some(solid(Color::rgb(1.0, 0.0, 0.0))), ..Default::default() }],
|
||||
);
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
assert_eq!(out.warnings, [writer::SYMBOL_CHANGES]);
|
||||
let back = read_back(&doc);
|
||||
let got = &back.layers[0].children().unwrap()[0];
|
||||
assert!(matches!(got.kind, NodeKind::SymbolInstance { .. }));
|
||||
assert_eq!(got.extras().symbol_overrides, doc.layers[0].children().unwrap()[0].extras().symbol_overrides);
|
||||
assert!(vectorcraft_doc::symbol_overrides::part(&doc.symbols[0].art, &[5]).is_none());
|
||||
assert!(vectorcraft_doc::symbol_overrides::part(&back.symbols[0].art, &[5]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_whose_changed_part_follows_an_empty_compound_path_is_written_as_its_art() {
|
||||
let doc = dynamic_instance_doc(
|
||||
|doc| {
|
||||
let empty = Node::path(doc.alloc_id(), PathData::default(), Appearance::basic(solid(LEAF), Paint::None, 1.0));
|
||||
let compound = Node::new(doc.alloc_id(), NodeKind::Compound { children: vec![Arc::new(empty)], rule: FillRule::EvenOdd });
|
||||
let sq = square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF);
|
||||
let mut root = Node::group(doc.alloc_id(), vec![Arc::new(compound), Arc::new(sq)]);
|
||||
root.name = Some("Branch".into());
|
||||
root
|
||||
},
|
||||
vec![vectorcraft_doc::SymbolOverride { path: vec![1], fill: Some(solid(Color::rgb(1.0, 0.0, 0.0))), ..Default::default() }],
|
||||
);
|
||||
let (fills, _) = dynamic_instance_as_art(&doc);
|
||||
assert_eq!(fills.len(), 1, "{fills:?}");
|
||||
assert_ne!(fills[0], solid(LEAF), "the square keeps its changed fill");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dynamic_instance_whose_changes_pass_the_size_limit_is_written_as_its_art() {
|
||||
let red = Color::rgb(1.0, 0.0, 0.0);
|
||||
let o = vectorcraft_doc::SymbolOverride { fill: Some(solid(red)), opacity: Some(0.5), ..Default::default() };
|
||||
let doc = dynamic_instance_doc(|doc| square(doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF), vec![o; 40_000]);
|
||||
assert!(serde_json::to_vec(&doc.layers[0].children().unwrap()[0].extras().symbol_overrides).unwrap().len() > 1 << 20);
|
||||
let (fills, opacities) = dynamic_instance_as_art(&doc);
|
||||
assert_eq!(fills.len(), 1, "{fills:?}");
|
||||
assert_ne!(fills[0], solid(LEAF), "the square keeps its changed fill");
|
||||
assert!(opacities.iter().any(|o| (o - 0.5).abs() < 1e-3), "{opacities:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn static_symbols_write_no_dynamic_entries() {
|
||||
let mut doc = dynamic_doc();
|
||||
|
||||
@@ -50,7 +50,7 @@ pub(super) use paints::{
|
||||
};
|
||||
pub(crate) use symbols::{DYNAMIC_KEY, OVERRIDES_KEY, hex_decode};
|
||||
#[cfg(test)]
|
||||
pub(super) use symbols::{MISSING_SYMBOL, SYMBOL_ART, SYMBOL_CHANGES, SYMBOL_COUNT, SYMBOL_OPTIONS};
|
||||
pub(super) use symbols::{MISSING_SYMBOL, OVERRIDE_SERIALIZATIONS, SYMBOL_ART, SYMBOL_CHANGES, SYMBOL_COUNT, SYMBOL_OPTIONS};
|
||||
#[cfg(test)]
|
||||
pub(super) use texts::{FONT_RESOLVES, RESOLVE_EACH_RUN, TYPE_ATTRIBUTES};
|
||||
#[cfg(test)]
|
||||
@@ -269,6 +269,7 @@ pub(super) struct Writer<'a> {
|
||||
/// Whether each symbol's art paints with a pattern, nested symbols included
|
||||
/// ([`Self::symbol_paints_pattern`]).
|
||||
patterned_symbols: HashMap<String, bool>,
|
||||
symbol_omissions: HashMap<*const Node, symbols::OmissionFacts>,
|
||||
/// Pattern tiles were written in the art being written ([`Self::define_symbol`] starts it over
|
||||
/// for each symbol's art).
|
||||
wrote_pattern: bool,
|
||||
@@ -289,6 +290,7 @@ pub(super) struct Writer<'a> {
|
||||
symbol_scales: HashMap<String, f64>,
|
||||
/// The entries a symbol's dictionary adds to its art's dictionary, and the art's id.
|
||||
root_entries: Option<(vectorcraft_doc::NodeId, String)>,
|
||||
/// The override dictionary entries prepared by [`Self::live_instance`], and the instance's id.
|
||||
instance_entries: Option<(vectorcraft_doc::NodeId, String)>,
|
||||
/// The objects that may be written with an art style, as they were before baking, by id
|
||||
/// ([`styles::styled_objects`]).
|
||||
@@ -402,6 +404,7 @@ impl<'a> Writer<'a> {
|
||||
defining_symbols: vec![],
|
||||
pattern_symbols: HashMap::new(),
|
||||
patterned_symbols: HashMap::new(),
|
||||
symbol_omissions: HashMap::new(),
|
||||
wrote_pattern: false,
|
||||
pattern_written: HashSet::new(),
|
||||
wrote_fixed: false,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Symbols: their definitions, the symbol list and their instances (see [`crate::ai_editing`] for
|
||||
//! the form).
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fmt::Write;
|
||||
|
||||
use vectorcraft_color::Paint;
|
||||
@@ -21,6 +21,11 @@ pub(crate) const DYNAMIC_KEY: &str = "VectorCraftDynamicSymbol";
|
||||
pub(crate) const OVERRIDES_KEY: &str = "VectorCraftSymbolOverrides";
|
||||
const MAX_OVERRIDES_BYTES: usize = 1 << 20;
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
pub(in crate::ai_editing) static OVERRIDE_SERIALIZATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
pub(crate) fn hex_decode(s: &str) -> Option<Vec<u8>> {
|
||||
if !s.len().is_multiple_of(2) || s.len() / 2 > MAX_OVERRIDES_BYTES {
|
||||
return None;
|
||||
@@ -64,10 +69,73 @@ pub(super) fn redrawn(n: &Node) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// What leaving out a group's paths and compound paths without anchors ([`Writer::shape`]) does to
|
||||
/// it in the editing data, worked out once for each group of a symbol's art.
|
||||
pub(super) struct OmissionFacts {
|
||||
/// The first omitted child's index. Overrides targeting this child or later children are unsafe.
|
||||
first: Option<usize>,
|
||||
/// A symbol root group with an omitted child and one kept child is treated as collapsing,
|
||||
/// regardless of its state.
|
||||
root_collapses: bool,
|
||||
}
|
||||
|
||||
impl OmissionFacts {
|
||||
fn of(n: &Node) -> Self {
|
||||
fn omitted(c: &Node) -> bool {
|
||||
match &c.kind {
|
||||
NodeKind::Path { path, .. } => path.is_empty(),
|
||||
NodeKind::Compound { .. } => c.children().is_none_or(|k| k.iter().filter_map(|m| m.path_data()).all(|p| p.is_empty())),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
let mut first = None;
|
||||
let mut kept_count = 0;
|
||||
for (i, c) in n.children().into_iter().flatten().enumerate() {
|
||||
if omitted(c) {
|
||||
first.get_or_insert(i);
|
||||
} else {
|
||||
kept_count += 1;
|
||||
}
|
||||
}
|
||||
let root_collapses = first.is_some() && kept_count == 1 && matches!(n.kind, NodeKind::Group { .. });
|
||||
Self { first, root_collapses }
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns serialized changes within the reader's limit when their targets remain safe after import.
|
||||
fn changes_kept(art: &Node, overrides: &[vectorcraft_doc::SymbolOverride], omissions: &mut HashMap<*const Node, OmissionFacts>) -> Option<Vec<u8>> {
|
||||
fn group_kids(n: &Node) -> Option<&Vec<std::sync::Arc<Node>>> {
|
||||
n.children().filter(|_| matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }))
|
||||
}
|
||||
#[cfg(test)]
|
||||
OVERRIDE_SERIALIZATIONS.with(|count| count.set(count.get() + 1));
|
||||
let json = serde_json::to_vec(overrides).ok()?;
|
||||
if json.len() > MAX_OVERRIDES_BYTES {
|
||||
return None;
|
||||
}
|
||||
for o in overrides {
|
||||
if omissions.entry(std::ptr::from_ref(art)).or_insert_with(|| OmissionFacts::of(art)).root_collapses {
|
||||
return None;
|
||||
}
|
||||
let mut n = art;
|
||||
for &i in o.path.iter().take(vectorcraft_doc::symbol_overrides::MAX_PATH_DEPTH) {
|
||||
let Some(kids) = group_kids(n) else { break };
|
||||
let Some(c) = kids.get(i as usize) else { break };
|
||||
let facts = omissions.entry(std::ptr::from_ref(n)).or_insert_with(|| OmissionFacts::of(n));
|
||||
if facts.first.is_some_and(|f| f <= i as usize) {
|
||||
return None;
|
||||
}
|
||||
n = c;
|
||||
}
|
||||
}
|
||||
Some(json)
|
||||
}
|
||||
|
||||
impl Writer<'_> {
|
||||
/// Whether instance `n` of symbol `symbol`, placed by `xf`, is written as an instance: not on the
|
||||
/// layers of a large canvas, in a pattern's tile or in art cut to a clipping object, without a
|
||||
/// fill or appearance changes of its own (which stain or change the art it draws) and without
|
||||
/// fill or appearance changes of its own (which stain or change the art it draws; a dynamic
|
||||
/// symbol's instance keeps its changes only when [`changes_kept`] returns their bytes) and without
|
||||
/// isolation, knockout or knockout shape of its own (which an instance's transparency doesn't
|
||||
/// hold), of a symbol without 9-slice guides and without pattern paint in its art (the canvas
|
||||
/// keeps an instance's pattern tiles where the document puts them, and a definition's art moves
|
||||
@@ -75,8 +143,11 @@ impl Writer<'_> {
|
||||
/// that keeps the art's area as it is written and scales it as the definition allows
|
||||
/// ([`Self::keeps_scale`]).
|
||||
pub(super) fn live_instance(&mut self, n: &Node, symbol: &str, xf: Affine) -> bool {
|
||||
let dynamic = self.symbol_doc.symbols.iter().any(|s| s.name == symbol && s.dynamic);
|
||||
let own = vectorcraft_brush::stain(n).is_some() || (!dynamic && !n.extras().symbol_overrides.is_empty());
|
||||
let doc = self.symbol_doc;
|
||||
let sym = doc.symbols.iter().find(|s| s.name == symbol);
|
||||
let dynamic = sym.is_some_and(|s| s.dynamic);
|
||||
let overrides = &n.extras().symbol_overrides;
|
||||
let own = vectorcraft_brush::stain(n).is_some() || (!dynamic && !overrides.is_empty());
|
||||
let group_state = n.isolate || n.knockout != Knockout::Neutral || n.knockout_shape;
|
||||
// The matrix as written (to ten decimals) must still keep the art's area.
|
||||
let m = Affine::new((self.instance_space * xf).as_coeffs().map(|v| (v * 1e10).round() / 1e10));
|
||||
@@ -91,6 +162,13 @@ impl Writer<'_> {
|
||||
&& self.define_symbol(symbol)
|
||||
&& !self.symbol_paints_pattern(symbol)
|
||||
&& self.keeps_scale(symbol, xf)
|
||||
&& (overrides.is_empty()
|
||||
|| sym.is_some_and(|s| {
|
||||
let Some(json) = changes_kept(&s.art, overrides, &mut self.symbol_omissions) else { return false };
|
||||
let e = format!("%_{} /String ({OVERRIDES_KEY}) ,\n", ps::bytes(hex_encode(&json).as_bytes()));
|
||||
self.instance_entries = Some((n.id, e));
|
||||
true
|
||||
}))
|
||||
}
|
||||
|
||||
/// Whether instance matrix `xf` places symbol `symbol`'s definition as the canvas draws the
|
||||
@@ -175,13 +253,7 @@ impl Writer<'_> {
|
||||
/// /RTransformMatrix` (to ten decimals), `;`, then its state block as a group's: `Xy`, `0 0 Xd`
|
||||
/// and `6 () XW` when it has transparency, else `9 () XW`. Its dictionary goes between the two.
|
||||
pub(super) fn symbol_instance(&mut self, n: &Node, symbol: &str, xf: Affine, xy: Xy) {
|
||||
let o = &n.extras().symbol_overrides;
|
||||
if !o.is_empty()
|
||||
&& let Ok(json) = serde_json::to_vec(o)
|
||||
&& json.len() <= MAX_OVERRIDES_BYTES
|
||||
{
|
||||
let e = format!("%_{} /String ({OVERRIDES_KEY}) ,\n", ps::bytes(hex_encode(&json).as_bytes()));
|
||||
self.instance_entries = Some((n.id, e));
|
||||
if self.instance_entries.as_ref().is_some_and(|(id, _)| *id == n.id) {
|
||||
self.warn(SYMBOL_CHANGES);
|
||||
}
|
||||
let m = self.instance_space * xf;
|
||||
|
||||
@@ -133,8 +133,10 @@ private editing data. VectorCraft:
|
||||
masks, pattern tiles and symbols as embedded images, each with a warning. Every symbol is written as a symbol
|
||||
definition (its art, its registration point and its place in the Symbols panel's order, nested symbols included)
|
||||
and its instances as symbol instances with their matrix, name, hide, lock, opacity, blending mode and opacity
|
||||
mask; instances with a fill, appearance changes, isolation, knockout or knockout shape of their own, instances of
|
||||
symbols with 9-slice guides or pattern paint, instances in pattern tiles or in art cut to a clipping object,
|
||||
mask; instances with a fill, appearance changes (a dynamic symbol's instance only when its changes take more than
|
||||
1 MiB, or omitting paths or compound paths without anchors removes a changed part, shifts its index or leaves a
|
||||
root group with one kept child, regardless of its state or the override path), isolation, knockout or knockout
|
||||
shape of their own, instances of symbols with 9-slice guides or pattern paint, instances in pattern tiles or in art cut to a clipping object,
|
||||
instances scaled almost flat, instances that turn, mirror or scale symbols holding type, live objects,
|
||||
instances written as their art, shapes with rounded live corners or strokes written as outlines or brush art,
|
||||
instances that scale their symbol more than 100 times and instances on the layers of a large canvas are written as their art, pattern fills in a
|
||||
|
||||
Reference in new issue
Block a user