mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 12:38:18 +00:00
Hit testing: keep the shapes a symbol instance's fill and stroke effects paint
Every click and hover measured the shapes that each symbol instance with a geometry effect on its fill or stroke paints, once for the cull box and again for the hit, so hovering over a document with many such instances of a complex symbol reshaped every one of them on each move. Those shapes are now kept on the thread, as live objects' art already is, and measured again only when the instance, the symbols, patterns, swatches, fonts or plug-ins change. They are not kept while the symbols hold an image, whose pixels can change when its file is read again. Kept evaluations of live objects and of instances are also measured again when a symbol's 9-slice guides or natural size change; before, a click on a live object holding an instance could follow the old placement.
This commit is contained in:
+114
-46
@@ -98,10 +98,58 @@ pub fn set_image_paint(f: ImagePaint) {
|
||||
let _ = IMAGE_PAINT.set(f);
|
||||
}
|
||||
|
||||
/// The shapes that [`ImagePaint`] gives for one object.
|
||||
type Painted = Vec<(BezPath, FillRule, AppearanceItem)>;
|
||||
|
||||
/// The shapes that the image or symbol instance `n` paints with its geometry effects
|
||||
/// ([`set_image_paint`]), none while nothing is registered.
|
||||
pub(crate) fn image_paint(doc: Option<&Document>, n: &Node) -> Vec<(BezPath, FillRule, AppearanceItem)> {
|
||||
IMAGE_PAINT.get().map_or_else(Vec::new, |f| f(doc, n))
|
||||
/// ([`set_image_paint`]), none while nothing is registered. The shapes of a symbol instance of
|
||||
/// `doc` with an effect on a fill or stroke are kept on this thread ([`PAINT_KEPT`]) and measured
|
||||
/// again when `n`, a symbol of `doc` or its 9-slice guides or natural size, a pattern of `doc`,
|
||||
/// its swatches or color groups, or the
|
||||
/// [`LiveInputs`] have changed since, and on every call while `n` or the document's symbols or
|
||||
/// patterns hold a placed document or the symbols hold an image ([`symbols_hold_images`]).
|
||||
pub(crate) fn image_paint(doc: Option<&Document>, n: &Node) -> Rc<Painted> {
|
||||
let Some(f) = IMAGE_PAINT.get() else { return Rc::default() };
|
||||
let row_effects = || n.appearance.items.iter().any(|i| !i.effects().is_empty());
|
||||
let Some(d) = doc.filter(|_| matches!(n.kind, NodeKind::SymbolInstance { .. }) && row_effects()) else {
|
||||
return Rc::new(f(doc, n));
|
||||
};
|
||||
let inputs = live_inputs();
|
||||
if let Some(painted) = kept(&PAINTED, d, n, inputs) {
|
||||
return painted;
|
||||
}
|
||||
#[cfg(test)]
|
||||
PAINT_EVALUATED.with(|c| c.set(c.get() + 1));
|
||||
let painted = Rc::new(f(doc, n));
|
||||
if !symbols_hold_images(d) {
|
||||
let size = painted.iter().fold(0usize, |s, (bp, _, _)| s.saturating_add(1 + bp.elements().len()));
|
||||
keep(&PAINTED, d, n, inputs, &painted, size, PAINT_KEPT);
|
||||
}
|
||||
painted
|
||||
}
|
||||
|
||||
/// Does the art of a symbol of `doc` hold an image? The shapes its fills and strokes paint can
|
||||
/// follow its pixels, which change when its file is read again, so [`image_paint`] does not keep
|
||||
/// the shapes of an instance then.
|
||||
fn symbols_hold_images(doc: &Document) -> bool {
|
||||
let mut image = false;
|
||||
doc.symbols.iter().for_each(|s| s.art.walk(&mut |c| image |= matches!(c.kind, NodeKind::Image(_))));
|
||||
image
|
||||
}
|
||||
|
||||
/// How many symbol instances' shapes [`image_paint`] keeps on each thread: every click and every
|
||||
/// hover measures each instance whose fill or stroke has a geometry effect.
|
||||
const PAINT_KEPT: usize = 64;
|
||||
|
||||
thread_local! {
|
||||
/// The shapes [`image_paint`] keeps, the latest first.
|
||||
static PAINTED: RefCell<Vec<Evaluated<Painted>>> = const { RefCell::new(Vec::new()) };
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many symbol instances [`image_paint`] measured on this thread.
|
||||
static PAINT_EVALUATED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// One level of evaluation of a live object in a document, as the canvas draws it: type in an
|
||||
@@ -128,21 +176,68 @@ fn live_inputs() -> u64 {
|
||||
LIVE_ART.get().map_or(0, |(_, inputs)| inputs())
|
||||
}
|
||||
|
||||
/// A live object evaluated by [`live_art`] on this thread: the object, its document's symbols (by
|
||||
/// name and art), patterns, swatches and color groups and the [`LiveInputs`] as they were, the art
|
||||
/// and its [`art_size`].
|
||||
struct EvaluatedLive {
|
||||
/// An object evaluated by [`live_art`] or [`image_paint`] on this thread: the object, its
|
||||
/// document's symbols (by name and art), patterns, swatches and color groups, the document data
|
||||
/// that symbols' 9-slice guides and natural sizes are kept in, and the [`LiveInputs`] as they
|
||||
/// were, the result and its size.
|
||||
struct Evaluated<T> {
|
||||
node: Node,
|
||||
symbols: Vec<(String, Arc<Node>)>,
|
||||
patterns: Vec<crate::PatternDef>,
|
||||
swatches: crate::Shared<Vec<vectorcraft_color::Swatch>>,
|
||||
swatch_groups: crate::Shared<Vec<vectorcraft_color::SwatchGroup>>,
|
||||
unknown: crate::Shared<serde_json::Map<String, serde_json::Value>>,
|
||||
inputs: u64,
|
||||
art: Rc<Vec<Node>>,
|
||||
art: Rc<T>,
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl EvaluatedLive {
|
||||
/// The evaluations kept on this thread in one store, the latest first.
|
||||
type Store<T> = std::thread::LocalKey<RefCell<Vec<Evaluated<T>>>>;
|
||||
|
||||
/// The evaluation of `n` in `doc` with inputs `inputs` that `store` keeps, moved to the front.
|
||||
fn kept<T>(store: &'static Store<T>, doc: &Document, n: &Node, inputs: u64) -> Option<Rc<T>> {
|
||||
store.with_borrow_mut(|kept| {
|
||||
let i = kept.iter().position(|e| e.is_of(doc, n, inputs))?;
|
||||
let e = kept.remove(i);
|
||||
let art = e.art.clone();
|
||||
kept.insert(0, e);
|
||||
Some(art)
|
||||
})
|
||||
}
|
||||
|
||||
/// Keep `art`, of size `size`, the evaluation of `n` in `doc` with inputs `inputs`, at the front
|
||||
/// of `store`, which keeps `most` evaluations whose sizes add up to [`LIVE_KEPT_SIZE`] at most.
|
||||
/// Nothing is kept when `art` alone is larger, or when `n` or the document's symbols or patterns
|
||||
/// hold a placed document ([`holds_placed`]).
|
||||
fn keep<T>(store: &'static Store<T>, doc: &Document, n: &Node, inputs: u64, art: &Rc<T>, size: usize, most: usize) {
|
||||
if size > LIVE_KEPT_SIZE || holds_placed(doc, n) {
|
||||
return;
|
||||
}
|
||||
let symbols = doc.symbols.iter().map(|s| (s.name.clone(), s.art.clone())).collect();
|
||||
let e = Evaluated {
|
||||
node: n.clone(),
|
||||
symbols,
|
||||
patterns: doc.patterns.clone(),
|
||||
swatches: doc.swatches.clone(),
|
||||
swatch_groups: doc.swatch_groups.clone(),
|
||||
unknown: doc.unknown.clone(),
|
||||
inputs,
|
||||
art: art.clone(),
|
||||
size,
|
||||
};
|
||||
store.with_borrow_mut(|kept| {
|
||||
kept.insert(0, e);
|
||||
kept.truncate(most);
|
||||
let mut total = 0usize;
|
||||
kept.retain(|e| {
|
||||
total = total.saturating_add(e.size);
|
||||
total <= LIVE_KEPT_SIZE
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
impl<T> Evaluated<T> {
|
||||
/// Is this the evaluation of `n` in `doc`, with inputs `inputs`, as they are now?
|
||||
fn is_of(&self, doc: &Document, n: &Node, inputs: u64) -> bool {
|
||||
self.inputs == inputs
|
||||
@@ -152,6 +247,7 @@ impl EvaluatedLive {
|
||||
&& self.patterns == doc.patterns
|
||||
&& self.swatches == doc.swatches
|
||||
&& self.swatch_groups == doc.swatch_groups
|
||||
&& self.unknown == doc.unknown
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,8 +255,9 @@ impl EvaluatedLive {
|
||||
/// envelope's stroke band hits it again and again, and an envelope can hold a blend.
|
||||
const LIVE_KEPT: usize = 4;
|
||||
|
||||
/// Most [`art_size`] the evaluations [`live_art`] keeps on one thread add up to. Art larger than
|
||||
/// this is not kept.
|
||||
/// Most size the evaluations [`live_art`] keeps on one thread add up to ([`art_size`]), and the
|
||||
/// shapes [`image_paint`] keeps (each shape and its path elements). Art larger than this is not
|
||||
/// kept.
|
||||
const LIVE_KEPT_SIZE: usize = 250_000;
|
||||
|
||||
/// The size of `art` for [`LIVE_KEPT_SIZE`]: its objects and the anchors of their paths.
|
||||
@@ -185,52 +282,23 @@ fn holds_placed(doc: &Document, n: &Node) -> bool {
|
||||
|
||||
thread_local! {
|
||||
/// The evaluations [`live_art`] keeps, the latest first.
|
||||
static EVALUATED: RefCell<Vec<EvaluatedLive>> = const { RefCell::new(Vec::new()) };
|
||||
static EVALUATED: RefCell<Vec<Evaluated<Vec<Node>>>> = const { RefCell::new(Vec::new()) };
|
||||
}
|
||||
|
||||
/// The art live object `n` of `doc` draws ([`set_live_art`]); [`crate::live::expand_live`] while
|
||||
/// nothing is registered. The art is evaluated again when `n`, a symbol of `doc`, a pattern of
|
||||
/// `doc`, its swatches or color groups, or the [`LiveInputs`] have changed since, and on every
|
||||
/// nothing is registered. The art is evaluated again when `n`, a symbol of `doc` or its 9-slice
|
||||
/// guides or natural size, a pattern of `doc`, its swatches or color groups, or the
|
||||
/// [`LiveInputs`] have changed since, and on every
|
||||
/// click while it or the document's symbols or patterns hold a placed document ([`holds_placed`]).
|
||||
fn live_art(doc: &Document, n: &Node) -> Rc<Vec<Node>> {
|
||||
let inputs = live_inputs();
|
||||
let kept = EVALUATED.with_borrow_mut(|kept| {
|
||||
let i = kept.iter().position(|e| e.is_of(doc, n, inputs))?;
|
||||
let e = kept.remove(i);
|
||||
let art = e.art.clone();
|
||||
kept.insert(0, e);
|
||||
Some(art)
|
||||
});
|
||||
if let Some(art) = kept {
|
||||
if let Some(art) = kept(&EVALUATED, doc, n, inputs) {
|
||||
return art;
|
||||
}
|
||||
#[cfg(test)]
|
||||
LIVE_EVALUATED.with(|c| c.set(c.get() + 1));
|
||||
let art = Rc::new(LIVE_ART.get().map_or_else(|| crate::live::expand_live(n), |(f, _)| f(Some(doc), n)));
|
||||
let size = art_size(&art);
|
||||
if size > LIVE_KEPT_SIZE || holds_placed(doc, n) {
|
||||
return art;
|
||||
}
|
||||
let symbols = doc.symbols.iter().map(|s| (s.name.clone(), s.art.clone())).collect();
|
||||
let e = EvaluatedLive {
|
||||
node: n.clone(),
|
||||
symbols,
|
||||
patterns: doc.patterns.clone(),
|
||||
swatches: doc.swatches.clone(),
|
||||
swatch_groups: doc.swatch_groups.clone(),
|
||||
inputs,
|
||||
art: art.clone(),
|
||||
size,
|
||||
};
|
||||
EVALUATED.with_borrow_mut(|kept| {
|
||||
kept.insert(0, e);
|
||||
kept.truncate(LIVE_KEPT);
|
||||
let mut total = 0usize;
|
||||
kept.retain(|e| {
|
||||
total = total.saturating_add(e.size);
|
||||
total <= LIVE_KEPT_SIZE
|
||||
});
|
||||
});
|
||||
keep(&EVALUATED, doc, n, inputs, &art, art_size(&art), LIVE_KEPT);
|
||||
art
|
||||
}
|
||||
|
||||
|
||||
@@ -599,6 +599,60 @@ fn evaluated_art_is_not_kept_while_a_placed_document_may_show_in_it() {
|
||||
assert_eq!(evaluated(), before + 2);
|
||||
}
|
||||
|
||||
/// For instances of the symbol "Painted", a fill that paints the square from (300, 0) to
|
||||
/// (320, 20); nothing for anything else.
|
||||
fn painted_far_away(_: Option<&Document>, n: &Node) -> Vec<(BezPath, FillRule, AppearanceItem)> {
|
||||
match &n.kind {
|
||||
NodeKind::SymbolInstance { symbol, .. } if symbol == "Painted" => {
|
||||
let square = shapes::rectangle(Rect::new(300.0, 0.0, 320.0, 20.0)).to_bezpath();
|
||||
vec![(square, FillRule::NonZero, AppearanceItem::Fill(crate::FillLayer::new(Paint::solid(Color::rgb(1.0, 0.0, 0.0)))))]
|
||||
}
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Clicks and hovers measure the shapes a symbol instance's fill and stroke effects paint once,
|
||||
/// and again only after the instance or the symbols change.
|
||||
#[test]
|
||||
fn clicks_measure_a_symbol_instances_painted_shapes_once_until_it_changes() {
|
||||
set_image_paint(painted_far_away);
|
||||
let mut d = Document::new(1000.0, 1000.0);
|
||||
let l = d.layers[0].id;
|
||||
let art = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(0.0, 0.0, 20.0, 20.0)), Appearance::default_art());
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "Painted".into(), art: Arc::new(art) });
|
||||
let inst = d.alloc_id();
|
||||
let mut n = Node::new(inst, NodeKind::SymbolInstance { symbol: "Painted".into(), xf: vectorcraft_geom::Affine::IDENTITY });
|
||||
n.appearance = Appearance::default_art().into();
|
||||
let offset = crate::Effect { id: "path.offsetPath".into(), params: serde_json::json!({"offset": 10}), visible: true };
|
||||
n.appearance.items[0].effects_mut().push(offset);
|
||||
d.insert(Some(l), 0, n).unwrap();
|
||||
let measured = || PAINT_EVALUATED.with(std::cell::Cell::get);
|
||||
let before = measured();
|
||||
let at = |d: &Document, x: f64| hit_test(d, Point::new(x, 10.0), HitOptions::default()).map(|h| h.leaf);
|
||||
assert_eq!(at(&d, 310.0), Some(inst), "a click where the instance's fill paints");
|
||||
assert_eq!(at(&d, 500.0), None);
|
||||
assert_eq!(at(&d, 10.0), Some(inst));
|
||||
assert_eq!(measured(), before + 1);
|
||||
d.node_mut(inst).unwrap().appearance.items[0].effects_mut()[0].params = serde_json::json!({"offset": 12});
|
||||
assert_eq!(at(&d, 310.0), Some(inst));
|
||||
assert_eq!(measured(), before + 2, "a changed instance is measured again");
|
||||
let other = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(0.0, 0.0, 5.0, 5.0)), Appearance::default_art());
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "Other".into(), art: Arc::new(other) });
|
||||
assert_eq!(at(&d, 310.0), Some(inst));
|
||||
assert_eq!(measured(), before + 3, "changed symbols measure it again");
|
||||
assert_eq!(at(&d, 310.0), Some(inst));
|
||||
assert_eq!(measured(), before + 3);
|
||||
// An image's pixels change when its file is read again, so with one in a symbol's art every
|
||||
// click measures the instance.
|
||||
let xf = vectorcraft_geom::Affine::IDENTITY;
|
||||
let im = crate::ImageObject { key: "k".into(), width: 10, height: 10, xf, link: None, placement: Default::default(), pixels: None };
|
||||
let image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(im)));
|
||||
d.symbols.push(crate::Symbol { dynamic: false, name: "Photo".into(), art: Arc::new(image) });
|
||||
assert_eq!(at(&d, 310.0), Some(inst));
|
||||
assert_eq!(at(&d, 310.0), Some(inst));
|
||||
assert!(measured() >= before + 5, "each click measures it: {}", measured() - before);
|
||||
}
|
||||
|
||||
/// A kept evaluation is the evaluation of the object only while the fonts and plug-ins
|
||||
/// ([`LiveInputs`]) are as they were.
|
||||
#[test]
|
||||
@@ -611,19 +665,32 @@ fn a_kept_evaluation_is_stale_after_the_live_inputs_change() {
|
||||
}
|
||||
|
||||
/// An evaluation of `n` in `d`, with inputs 7, holding no symbols, patterns or art.
|
||||
fn kept_evaluation(d: &Document, n: &Node) -> EvaluatedLive {
|
||||
EvaluatedLive {
|
||||
fn kept_evaluation(d: &Document, n: &Node) -> Evaluated<Vec<Node>> {
|
||||
Evaluated {
|
||||
node: n.clone(),
|
||||
symbols: vec![],
|
||||
patterns: vec![],
|
||||
swatches: d.swatches.clone(),
|
||||
swatch_groups: d.swatch_groups.clone(),
|
||||
unknown: d.unknown.clone(),
|
||||
inputs: 7,
|
||||
art: std::rc::Rc::new(vec![]),
|
||||
size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// A kept evaluation is stale once a symbol's 9-slice guides or natural size change: they place
|
||||
/// the symbol's art, and the document keeps them apart from the symbols.
|
||||
#[test]
|
||||
fn a_kept_evaluation_is_stale_after_a_symbols_placement_changes() {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let n = Node::path(NodeId(3), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default_art());
|
||||
let e = kept_evaluation(&d, &n);
|
||||
assert!(e.is_of(&d, &n, 7));
|
||||
d.unknown.insert(crate::SYMBOL_SIZES.into(), serde_json::json!({"Logo": [40.0, 20.0]}));
|
||||
assert!(!e.is_of(&d, &n, 7));
|
||||
}
|
||||
|
||||
/// A kept evaluation is stale once a swatch changes, in a color group too: a painted live effect's
|
||||
/// pieces name swatches.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user