GitCorner

Code

ossartcraftvectorcraft๐ŸŒ
2bc41aeb8602
//! Symbol instances measured by their symbol's art: [`SymbolBoxes`] looks each symbol's art up in
//! the document and measures it once, and an instance's bounds are its art's box placed by the
//! instance's transform ([`Node::geometric_bounds_with`]).

use std::cell::{Cell, OnceCell, RefCell};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use vectorcraft_geom::{Affine, Rect};

use crate::symbol_nine_slice::transformed;
use crate::{AppearanceItem, Document, MAX_SYMBOL_NODES, NineSliceMap, Node, NodeKind, SYMBOL_HALF, Symbol};

/// The box a symbol instance is measured by when its symbol's art has no box
/// ([`SymbolBoxes::art`]), and in [`Node::geometric_bounds`]: the 20 ร— 20 square around the origin
/// that symbols made in the app keep their art in.
pub(crate) const SYMBOL_SQUARE: Rect = Rect::new(-SYMBOL_HALF, -SYMBOL_HALF, SYMBOL_HALF, SYMBOL_HALF);

/// Instances of a symbol whose art holds symbols nested this many deep are measured by
/// [`SYMBOL_SQUARE`] instead of their art.
const MAX_DEPTH: usize = 32;

/// Most effect reaches one set of boxes measures for transforms that place the art of symbols
/// holding instances: past it, an estimate stands in ([`SymbolBoxes::effects_reach`]).
const MAX_REACHES: usize = 4096;

/// A symbol's name and the bits of the linear part of a transform that places its art.
type Placing = (String, [u64; 4]);

/// Each symbol name of a document โ†’ the index of the first symbol of that name.
pub(crate) type SymbolNames = HashMap<String, usize>;

/// A symbol's art measured in symbol space.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct ArtBox {
    /// The art's geometric bounds.
    pub(crate) geometric: Rect,
    /// The art's visual bounds (its strokes included), never smaller than `geometric`.
    pub(crate) visual: Rect,
    /// The farthest any stroke in the art can paint from its path ([`crate::Appearance::outset`]).
    pub(crate) reach: f64,
    /// Whether the art, or the art of a symbol an instance in it shows, has a visible effect.
    pub(crate) effects: bool,
    /// The widest stroke of the objects with a visible brushed stroke in the art, or in the art
    /// of a symbol an instance in it shows: `None` when no object there has one.
    pub(crate) brush: Option<f64>,
    /// Whether the art holds an instance of a symbol with 9-slice guides, or of a symbol whose art
    /// holds one at any depth.
    pub(crate) sliced: bool,
    /// The box of the regions that the live geometry effects of the clipping paths in the art give
    /// them ([`crate::hit::set_clip_region`]), and of the instances in the art whose art has such
    /// regions, each measured by [`Node::reach_bounds_with`]. `None` when nothing in the art has
    /// one.
    pub(crate) reshaped: Option<Rect>,
}

/// The art boxes of a document's symbols, which [`Node::geometric_bounds_with`],
/// [`Node::visual_bounds_with`] and [`Node::reach_bounds_with`] measure symbol instances by. Each
/// symbol's art is measured the first time an instance of it is measured; make a new one after the
/// symbols change.
pub struct SymbolBoxes<'a> {
    doc: &'a Document,
    /// Each symbol name of the document โ†’ the index of the first symbol of that name (the one a
    /// name finds), made the first time a name is looked up ([`Self::symbol_art`]) or given by
    /// [`Self::with_names`].
    names: OnceCell<Arc<SymbolNames>>,
    /// The art boxes measured so far, by symbol name (`None` for a symbol whose instances are
    /// measured by [`SYMBOL_SQUARE`]).
    measured: RefCell<HashMap<String, Option<ArtBox>>>,
    /// How deep symbols nest in the art of each symbol checked so far, by symbol name
    /// ([`Self::depth`]).
    depths: RefCell<HashMap<String, Option<usize>>>,
    /// Set while symbol art or a click's box is measured, and always in boxes made by
    /// [`Self::quick`]: instances are then measured by their art boxes alone
    /// ([`Self::placed_visual`] returns `None`).
    quick: Cell<bool>,
    /// Set in boxes made by [`Self::with_brush_art`], and cleared while symbol art is measured
    /// ([`Self::brush_pad`]).
    brush_art: Cell<bool>,
    /// Cleared in boxes made by [`Self::without_art_strokes`], and set while symbol art is
    /// measured ([`Self::art_strokes`]).
    art_strokes: Cell<bool>,
    /// The effect reaches measured so far, by symbol name and the bits of the linear part of the
    /// transform that places the art ([`Self::effects_reach`]).
    reaches: RefCell<HashMap<(String, [u64; 4]), f64>>,
    /// How many of them are of symbols whose art holds instances.
    holders: Cell<usize>,
    /// The farthest effect reach measured so far for each symbol.
    farthest: RefCell<HashMap<String, f64>>,
    /// The effect reach of each symbol's art with no transform, measured past [`MAX_REACHES`]
    /// ([`Self::unplaced_reach`]).
    unplaced: RefCell<HashMap<String, f64>>,
    /// How many times [`Self::measure_art`] has run a measure on symbol art ([`Self::art_measured`]).
    runs: Cell<usize>,
    /// The boxes [`Self::sliced_visual`] has measured, by symbol name and the bits of the linear
    /// part of the transform that placed the art.
    sliced: RefCell<HashMap<Placing, Option<Rect>>>,
    /// How many objects of symbol art [`Self::sliced_visual`] has placed, the art of opacity masks
    /// included.
    sliced_placed: Cell<u64>,
    /// The most objects of symbol art [`Self::sliced_visual`] places: [`MAX_SYMBOL_NODES`].
    sliced_most: u64,
    /// The widths [`Self::measured_width`] has measured, by kind and symbol name.
    widths: RefCell<HashMap<(&'static str, String), Option<f64>>>,
    /// While [`Self::consulted`] runs: the symbols whose art boxes [`Self::art`] has given outside
    /// the measuring of symbol art.
    consulted: RefCell<Option<HashSet<String>>>,
}

/// What the art boxes and nesting depths of a document's symbols depend on: each symbol's name and
/// art, the symbols' 9-slice guides (which symbols' art holds instances placed through guides), and
/// the inputs of the live geometry effects of clipping paths ([`crate::hit::set_clip_region`]).
/// Symbol sizes only move art placed through guides, which is measured on each call. Each symbol's
/// art is held by a weak pointer. While a weak pointer to an art's allocation is held, nothing else
/// can be at that address, and the art can't be changed in place (`Arc::make_mut` moves it), so art
/// changed in any way no longer matches, as in the cache of `Document::inline_cycles`.
struct KeptKey {
    symbols: Vec<(String, std::sync::Weak<Node>)>,
    guides: Option<serde_json::Value>,
    inputs: (u64, bool),
}

impl KeptKey {
    /// The key of the document's 9-slice guides.
    const GUIDES: &str = crate::symbol_nine_slice::SYMBOL_NINE_SLICE;

    fn of(doc: &Document, inputs: (u64, bool)) -> Self {
        KeptKey {
            symbols: doc.symbols.iter().map(|s| (s.name.clone(), Arc::downgrade(&s.art))).collect(),
            guides: doc.unknown.get(Self::GUIDES).cloned(),
            inputs,
        }
    }

    /// Does this describe `doc`'s symbols as they are now, with inputs `inputs`?
    fn is_of(&self, doc: &Document, inputs: (u64, bool)) -> bool {
        self.inputs == inputs
            && self.symbols.len() == doc.symbols.len()
            && self.symbols.iter().zip(&doc.symbols).all(|((name, art), s)| std::ptr::eq(art.as_ptr(), Arc::as_ptr(&s.art)) && *name == s.name)
            && self.guides.as_ref() == doc.unknown.get(Self::GUIDES)
    }

    fn same(&self, other: &KeptKey) -> bool {
        self.inputs == other.inputs
            && self.symbols.len() == other.symbols.len()
            && self.symbols.iter().zip(&other.symbols).all(|((n, a), (m, b))| a.ptr_eq(b) && n == m)
            && self.guides == other.guides
    }
}

/// Art boxes, nesting depths and the name index measured by boxes made by
/// [`Document::kept_symbol_boxes`], kept for the next ones made for the same symbols.
struct Kept {
    key: KeptKey,
    names: Option<Arc<SymbolNames>>,
    measured: HashMap<String, Option<ArtBox>>,
    depths: HashMap<String, Option<usize>>,
}

/// How many sets of symbols [`Document::kept_symbol_boxes`] keeps measurements for on each thread:
/// clicks can alternate between two open documents.
const KEPT_SETS: usize = 2;

thread_local! {
    /// The measurements [`Document::kept_symbol_boxes`] keeps, the latest first.
    static KEPT: RefCell<Vec<Kept>> = const { RefCell::new(Vec::new()) };
}

/// [`SymbolBoxes`] made by [`Document::kept_symbol_boxes`], which keep their measurements for the
/// next ones when dropped.
pub(crate) struct KeptBoxes<'a> {
    boxes: SymbolBoxes<'a>,
    /// What the measurements depend on, under which they are kept.
    key: Option<KeptKey>,
    /// Other boxes of the same document whose measurements are kept with these
    /// ([`Self::keeping`]).
    also: Option<&'a SymbolBoxes<'a>>,
}

impl<'a> std::ops::Deref for KeptBoxes<'a> {
    type Target = SymbolBoxes<'a>;

    fn deref(&self) -> &SymbolBoxes<'a> {
        &self.boxes
    }
}

impl<'a> KeptBoxes<'a> {
    /// These boxes, which also keep the art boxes and nesting depths `other` has measured when
    /// dropped. `other` measures the same document, as the boxes of
    /// [`Document::locked_artboards_kept`] do.
    pub(crate) fn keeping(mut self, other: &'a SymbolBoxes<'a>) -> Self {
        self.also = Some(other);
        self
    }
}

impl Drop for KeptBoxes<'_> {
    /// Keeps the measurements, adding them to those kept for the same symbols by other boxes alive
    /// at the same time. Art boxes of symbols whose art holds instances of symbols with 9-slice
    /// guides are left out: measuring them places art within a budget the boxes share
    /// ([`SymbolBoxes::sliced_visual`]).
    fn drop(&mut self) {
        let Some(key) = self.key.take() else { return };
        let b = &self.boxes;
        let mut measured = b.measured.take();
        let mut depths = b.depths.take();
        if let Some(other) = self.also {
            for (name, a) in other.measured.borrow().iter() {
                measured.entry(name.clone()).or_insert(*a);
            }
            for (name, d) in other.depths.borrow().iter() {
                depths.entry(name.clone()).or_insert(*d);
            }
        }
        measured.retain(|_, a| a.is_none_or(|a| !a.sliced));
        let mut kept = Kept { key, names: b.names.get().cloned(), measured, depths };
        // Nothing is kept while the thread ends or the list is in use.
        let _ = KEPT.try_with(|k| {
            if let Ok(mut k) = k.try_borrow_mut() {
                if let Some(i) = k.iter().position(|e| e.key.same(&kept.key)) {
                    let other = k.remove(i);
                    kept.names = kept.names.or(other.names);
                    for (name, a) in other.measured {
                        kept.measured.entry(name).or_insert(a);
                    }
                    for (name, d) in other.depths {
                        kept.depths.entry(name).or_insert(d);
                    }
                }
                k.insert(0, kept);
                k.truncate(KEPT_SETS);
            }
        });
    }
}

impl Document {
    /// [`SymbolBoxes`] for this document's symbols, none measured yet.
    pub fn symbol_boxes(&self) -> SymbolBoxes<'_> {
        SymbolBoxes {
            doc: self,
            names: OnceCell::new(),
            measured: RefCell::default(),
            depths: RefCell::default(),
            quick: Cell::new(false),
            brush_art: Cell::new(false),
            art_strokes: Cell::new(true),
            reaches: RefCell::default(),
            holders: Cell::new(0),
            farthest: RefCell::default(),
            unplaced: RefCell::default(),
            runs: Cell::new(0),
            sliced: RefCell::default(),
            sliced_placed: Cell::new(0),
            sliced_most: MAX_SYMBOL_NODES,
            widths: RefCell::default(),
            consulted: RefCell::default(),
        }
    }

    /// [`Self::symbol_boxes`] that start with the art boxes, nesting depths and name index measured
    /// by the last such boxes made on this thread for the same symbols, and keep theirs for the next
    /// ones when dropped. The measurements are made again when a symbol's name or art, the symbols'
    /// 9-slice guides, or the inputs of the live geometry effects of clipping paths have
    /// changed since. Measurements for the last [`KEPT_SETS`] sets of symbols are kept.
    pub(crate) fn kept_symbol_boxes(&self) -> KeptBoxes<'_> {
        self.kept_boxes(false)
    }

    /// [`Self::symbol_boxes`] that start with a copy of the measurements
    /// [`Self::kept_symbol_boxes`] would start with, and keep nothing themselves.
    pub(crate) fn symbol_boxes_from_kept(&self) -> SymbolBoxes<'_> {
        let mut kept = self.kept_boxes(true);
        kept.key = None;
        std::mem::replace(&mut kept.boxes, self.symbol_boxes())
    }

    /// [`Self::kept_symbol_boxes`], taking the kept measurements, or with `copy` copying them.
    fn kept_boxes(&self, copy: bool) -> KeptBoxes<'_> {
        let mut boxes = self.symbol_boxes();
        let inputs = crate::hit::clip_region_inputs();
        let found = KEPT
            .try_with(|k| {
                let mut k = k.try_borrow_mut().ok()?;
                let i = k.iter().position(|e| e.key.is_of(self, inputs))?;
                if copy {
                    let e = k.get(i)?;
                    let key = KeptKey { symbols: e.key.symbols.clone(), guides: e.key.guides.clone(), inputs };
                    Some(Kept { key, names: e.names.clone(), measured: e.measured.clone(), depths: e.depths.clone() })
                } else {
                    Some(k.remove(i))
                }
            })
            .ok()
            .flatten();
        let key = match found {
            Some(e) => {
                if let Some(names) = e.names {
                    // The boxes were just made, so no index is set yet.
                    let _ = boxes.names.set(names);
                }
                *boxes.measured.get_mut() = e.measured;
                *boxes.depths.get_mut() = e.depths;
                e.key
            }
            None => KeptKey::of(self, inputs),
        };
        KeptBoxes { boxes, key: Some(key), also: None }
    }

    /// Each symbol name of this document โ†’ the index of the first symbol of that name (the one a
    /// name finds).
    pub(crate) fn symbol_names(&self) -> SymbolNames {
        #[cfg(any(test, feature = "symbol-read-count"))]
        SYMBOLS_READ.with(|c| c.set(c.get() + self.symbols.len()));
        let mut names = HashMap::with_capacity(self.symbols.len());
        for (i, s) in self.symbols.iter().enumerate() {
            names.entry(s.name.clone()).or_insert(i);
        }
        names
    }
}

#[cfg(any(test, feature = "symbol-read-count"))]
thread_local! {
    /// How many symbols [`Document::symbol_names`] has read on this thread.
    pub(crate) static SYMBOLS_READ: Cell<usize> = const { Cell::new(0) };
    /// How many art boxes [`SymbolBoxes::art`] has measured on this thread.
    static ART_BOXES_MEASURED: Cell<usize> = const { Cell::new(0) };
}

/// How many symbols [`Document::symbol_names`] has read on this thread, for other crates' tests
/// that count how often the symbol list is read to find symbols by name (the `symbol-read-count`
/// feature, dev-dependencies only).
#[cfg(any(test, feature = "symbol-read-count"))]
#[doc(hidden)]
pub fn symbols_read() -> usize {
    SYMBOLS_READ.with(Cell::get)
}

impl<'a> SymbolBoxes<'a> {
    /// The document whose symbols these are.
    pub fn document(&self) -> &'a Document {
        self.doc
    }

    /// New boxes for the same document, none measured yet and with none of the options set, that
    /// find symbols by name through the same index as these boxes ([`Self::symbol`]). Measuring
    /// objects one at a time in boxes of their own made this way measures them as boxes made by
    /// [`Document::symbol_boxes`] do, without reading the symbol list again.
    pub fn unmeasured(&self) -> SymbolBoxes<'a> {
        let doc: &'a Document = self.doc;
        let names = self.names.get_or_init(|| Arc::new(doc.symbol_names()));
        doc.symbol_boxes().with_names(names)
    }

    /// Makes these boxes find symbols by `names` ([`Document::symbol_names`]), made from the
    /// document's symbols while they had the names and order they have now.
    pub(crate) fn with_names(self, names: &Arc<SymbolNames>) -> Self {
        let _ = self.names.set(names.clone());
        self
    }

    /// The first symbol of the document named `name`, found in time independent of the number of
    /// symbols after the first lookup.
    pub fn symbol(&self, name: &str) -> Option<&'a Symbol> {
        let doc: &'a Document = self.doc;
        let names = self.names.get_or_init(|| Arc::new(doc.symbol_names()));
        let i = *names.get(name)?;
        match doc.symbols.get(i) {
            Some(s) if s.name == name => Some(s),
            // Names that no longer match the symbols: the first symbol of that name.
            _ => doc.symbols.iter().find(|s| s.name == name),
        }
    }

    /// The art of the first symbol of the document named `name` ([`Self::symbol`]).
    pub(crate) fn symbol_art(&self, name: &str) -> Option<&'a Arc<Node>> {
        self.symbol(name).map(|s| &s.art)
    }

    /// Makes these boxes measure every symbol instance as [`Node::reach_bounds_with`] does, by its
    /// art box alone. An instance that scales or turns stroked art then measures a little larger
    /// than with [`Node::visual_bounds_with`], and its art is not transformed to measure it. The
    /// visual box of an instance placed through 9-slice guides holds the art box placed by each
    /// cell's map ([`crate::NineSliceMap::placement_bounds`]). An instance whose art holds an
    /// instance of a symbol with 9-slice guides at any depth is measured as its art placed by its
    /// transform ([`Self::sliced_visual`]).
    pub fn quick(self) -> Self {
        self.quick.set(true);
        self
    }

    /// Makes these boxes grow the visual box of each symbol instance whose art holds brushed
    /// strokes by as far as their brush art can paint past it ([`Self::brush_pad`]). The visual
    /// boxes of the groups, layers and live objects that hold such an instance grow with it.
    pub fn with_brush_art(self) -> Self {
        self.brush_art.set(true);
        self
    }

    /// Makes these boxes measure the visual box of each symbol instance without the strokes in
    /// its art: the art's geometric box placed by the instance's transform, grown by the
    /// instance's own strokes. The strokes in the art keep their weight when the instance scales.
    /// The visual boxes of the groups, layers and live objects that hold an instance do not include
    /// those strokes either.
    pub fn without_art_strokes(self) -> Self {
        self.art_strokes.set(false);
        self
    }

    /// Whether instances are measured with the strokes in their art: false in boxes made by
    /// [`Self::without_art_strokes`], except while symbol art is measured.
    pub(crate) fn art_strokes(&self) -> bool {
        self.art_strokes.get()
    }

    /// The art box of the symbol named `name`, with the instances in the art measured by their own
    /// art boxes. Returns `None` when [`Self::depth`] is `None` and when the art has no finite
    /// bounds: instances of the symbol are then measured by [`SYMBOL_SQUARE`].
    pub(crate) fn art(&self, name: &str) -> Option<ArtBox> {
        let cached = self.measured.borrow().get(name).copied();
        let b = cached.unwrap_or_else(|| {
            #[cfg(test)]
            ART_BOXES_MEASURED.with(|c| c.set(c.get() + 1));
            let b = self.measure_art(name, |art| self.in_art(|| art_box(art, self))).flatten();
            self.measured.borrow_mut().insert(name.to_string(), b);
            b
        });
        if b.is_some()
            && let Some(seen) = self.consulted.borrow_mut().as_mut()
            && !seen.contains(name)
        {
            seen.insert(name.to_string());
        }
        b
    }

    /// Runs `measure`, and returns the names of the symbols whose art boxes it took from these
    /// boxes, the art of symbols it measured left out: the symbols whose art the instances it
    /// measured are measured by.
    pub(crate) fn consulted(&self, measure: impl FnOnce()) -> HashSet<String> {
        let was = self.consulted.replace(Some(HashSet::new()));
        measure();
        self.consulted.replace(was).unwrap_or_default()
    }

    /// Runs `measure` on the art of the symbol named `name`. Returns `None` when [`Self::depth`]
    /// is `None`, without running it.
    pub fn measure_art<R>(&self, name: &str, measure: impl FnOnce(&Arc<Node>) -> R) -> Option<R> {
        self.depth(name)?;
        let art = self.symbol_art(name)?;
        self.runs.set(self.runs.get() + 1);
        Some(measure(art))
    }

    /// How many times [`Self::measure_art`] has run a measure on symbol art for these boxes, the
    /// measures behind the cached art boxes and placed visual boxes included.
    pub fn art_measured(&self) -> usize {
        self.runs.get()
    }

    /// The geometric box of the art of the symbol named `name` in symbol space, with the instances
    /// in the art measured by their own art boxes. `None` when [`Self::art`] is `None`: instances of
    /// the symbol are then measured by [`SYMBOL_SQUARE`].
    pub fn art_bounds(&self, name: &str) -> Option<Rect> {
        self.art(name).map(|a| a.geometric)
    }

    /// Whether the art of the symbol named `name`, or of a symbol an instance in it shows, has a
    /// visible effect (raster or geometry, the object's own or a fill's or stroke's). False when
    /// [`Self::art`] is `None`.
    pub fn has_effects(&self, name: &str) -> bool {
        self.art(name).is_some_and(|a| a.effects)
    }

    /// How far the effects in the art of the symbol named `name` paint past that art when a
    /// transform with the linear part `linear` places it, as `measure` measures it for a linear
    /// part. `measure` runs once for each symbol and linear part. For symbols whose art holds
    /// instances, it runs [`MAX_REACHES`] times at most for these boxes. After that, a linear part
    /// not measured yet takes the reach with no transform, times โˆš2 and the larger of 1 and the
    /// linear part's Frobenius norm, or the farthest reach measured for the symbol when that is
    /// farther; `measure` runs once more for each of those symbols whose reach with no transform
    /// was not measured yet. The estimate holds for effects that paint a fixed distance past the
    /// art, or a distance in proportion to its size.
    pub fn effects_reach(&self, name: &str, linear: [f64; 4], measure: &dyn Fn([f64; 4]) -> f64) -> f64 {
        let key = (name.to_string(), linear.map(f64::to_bits));
        if let Some(r) = self.reaches.borrow().get(&key) {
            return *r;
        }
        let holder = self.depth(name) != Some(0);
        if holder && self.holders.get() >= MAX_REACHES {
            let norm = linear.iter().map(|v| v * v).sum::<f64>().sqrt();
            let estimate = self.unplaced_reach(name, measure) * std::f64::consts::SQRT_2 * norm.max(1.0);
            return self.farthest.borrow().get(name).map_or(estimate, |f| f.max(estimate));
        }
        if holder {
            self.holders.set(self.holders.get() + 1);
        }
        let r = measure(linear);
        self.reaches.borrow_mut().insert(key, r);
        let mut farthest = self.farthest.borrow_mut();
        let f = farthest.entry(name.to_string()).or_insert(r);
        *f = f.max(r);
        r
    }

    /// [`Self::effects_reach`] with no transform, measured once for each symbol, also past
    /// [`MAX_REACHES`].
    fn unplaced_reach(&self, name: &str, measure: &dyn Fn([f64; 4]) -> f64) -> f64 {
        const IDENTITY: [f64; 4] = [1.0, 0.0, 0.0, 1.0];
        if let Some(r) = self.reaches.borrow().get(&(name.to_string(), IDENTITY.map(f64::to_bits))) {
            return *r;
        }
        if let Some(r) = self.unplaced.borrow().get(name) {
            return *r;
        }
        let r = measure(IDENTITY);
        self.unplaced.borrow_mut().insert(name.to_string(), r);
        r
    }

    /// The widest stroke of the objects with a visible brushed stroke in the art of the symbol
    /// named `name`, or in the art of a symbol an instance in it shows. `None` when no object there
    /// has one, and when [`Self::art`] is `None`.
    pub fn brush_width(&self, name: &str) -> Option<f64> {
        self.art(name)?.brush
    }

    /// The width `measure` gives for the symbol named `name`, measured once for these boxes for each
    /// `kind`, which names what `measure` measures. `measure` may call this again for other names.
    pub fn measured_width(&self, kind: &'static str, name: &str, measure: impl FnOnce() -> Option<f64>) -> Option<f64> {
        let key = (kind, name.to_string());
        if let Some(w) = self.widths.borrow().get(&key) {
            return *w;
        }
        let w = measure();
        self.widths.borrow_mut().insert(key, w);
        w
    }

    /// How far the visual box of an instance of the symbol named `name` grows in boxes made by
    /// [`Self::with_brush_art`]: `16 ร— max(w, 1) + 8` for the widest stroke `w` that
    /// [`Self::brush_width`] finds, as far as the renderer grows the cull bounds of a brushed path
    /// (`brush_fx::cull_bounds` in vectorcraft-render). 0 in other boxes, while symbol art is
    /// measured, and when the art has no brushed stroke.
    pub(crate) fn brush_pad(&self, name: &str) -> f64 {
        if !self.brush_art.get() {
            return 0.0;
        }
        self.brush_width(name).map_or(0.0, |w| 16.0 * w.max(1.0) + 8.0)
    }

    /// How deep symbols nest in the art of the symbol named `name`: 0 when the art holds no
    /// instance of a symbol in the document, 1 when the symbols its instances show hold none, and
    /// so on. Returns `None` when the document has no such symbol, when the symbol or a symbol
    /// nested in its art holds an instance of itself at any depth, and when symbols nest
    /// [`MAX_DEPTH`] deep: instances of the symbol are then measured by [`SYMBOL_SQUARE`]. The
    /// answer depends only on the document's symbols.
    fn depth(&self, name: &str) -> Option<usize> {
        if let Some(d) = self.depths.borrow().get(name) {
            return *d;
        }
        let mut learned = HashMap::new();
        let d = self.find_depth(name, &mut learned);
        let mut depths = self.depths.borrow_mut();
        depths.extend(learned);
        depths.insert(name.to_string(), d);
        d
    }

    /// Finds [`Self::depth`] for the symbol named `name` by walking the symbols its art's instances
    /// show, depth first, without recursing. `learned` collects what the walk finds out about the
    /// other symbols it reaches: the depth of each one it walks to the end, and `None` for those on
    /// the way down to a symbol that holds an instance of itself or nests too deep.
    fn find_depth(&self, name: &str, learned: &mut HashMap<String, Option<usize>>) -> Option<usize> {
        // The symbols on the way down from `name`: each with the symbols its art shows that are
        // still to be walked, and the deepest nesting found under it so far.
        let mut path: Vec<(String, Vec<String>, usize)> = vec![(name.to_string(), self.shown(self.symbol_art(name)?), 0)];
        loop {
            let (_, todo, _) = path.last_mut()?;
            let Some(next) = todo.pop() else {
                // Everything under it is walked.
                let (done, _, d) = path.pop()?;
                if d >= MAX_DEPTH {
                    learned.insert(done, None);
                    return give_up(&path, learned);
                }
                learned.insert(done, Some(d));
                let Some((_, _, up)) = path.last_mut() else { return Some(d) };
                *up = (*up).max(d + 1);
                continue;
            };
            let known = learned.get(&next).copied().or_else(|| self.depths.borrow().get(&next).copied());
            match known {
                Some(Some(d)) => {
                    if let Some((_, _, deepest)) = path.last_mut() {
                        *deepest = (*deepest).max(d + 1);
                    }
                }
                // It holds an instance of itself or nests too deep, and so does every symbol on
                // the way down to it.
                Some(None) => return give_up(&path, learned),
                // A symbol that holds an instance of itself.
                None if path.iter().any(|(n, ..)| *n == next) => return give_up(&path, learned),
                // Symbols nested MAX_DEPTH deep under `name`.
                None if path.len() >= MAX_DEPTH => return None,
                None => {
                    if let Some(a) = self.symbol_art(&next) {
                        let todo = self.shown(a);
                        path.push((next, todo, 0));
                    }
                }
            }
        }
    }

    /// The visual bounds of symbol `name`'s art drawn as an instance with transform `xf` draws it
    /// (through its 9-slice guides when it has them, [`Document::place_symbol_art`]), its strokes
    /// at their own weight, the instances in it measured by their art boxes. `None`
    /// when [`Self::depth`] is `None`, in boxes made by [`Self::quick`], while symbol art or a
    /// click's box is measured, and when placing the art through its guides could make more than
    /// [`MAX_SYMBOL_NODES`] objects ([`crate::NineSliceMap::most_placed`]).
    pub(crate) fn placed_visual(&self, name: &str, xf: Affine) -> Option<Rect> {
        self.placed_visual_within(name, xf, MAX_SYMBOL_NODES)
    }

    /// [`Self::placed_visual`], `None` when placing the art through its guides could make more
    /// than `most` objects.
    pub(crate) fn placed_visual_within(&self, name: &str, xf: Affine, most: u64) -> Option<Rect> {
        if self.quick.get() {
            return None;
        }
        self.place_and_measure(name, xf, most)
    }

    /// [`Self::placed_visual`] in boxes made by [`Self::quick`] and while symbol art is measured
    /// too, for a symbol whose art holds an instance of a symbol with 9-slice guides at any depth:
    /// where that instance draws its art depends on the transform of each instance above it. The
    /// art is placed once for each symbol and linear part of `xf`, and the box is moved by the rest
    /// of `xf`. `None` for other symbols, when [`Self::placed_visual`] is `None` outside quick
    /// boxes, and when placing the art would take the objects of the art these boxes have placed
    /// for it past [`MAX_SYMBOL_NODES`], the art placed to measure the instances in placed art
    /// included. The renderer draws no more objects of symbol art than that in one frame.
    pub(crate) fn sliced_visual(&self, name: &str, xf: Affine) -> Option<Rect> {
        if !self.art(name)?.sliced {
            return None;
        }
        let [a, b, c, d, e, f] = xf.as_coeffs();
        let (linear, moved) = (Affine::new([a, b, c, d, 0.0, 0.0]), Affine::translate((e, f)));
        let key = (name.to_string(), [a, b, c, d].map(f64::to_bits));
        let cached = self.sliced.borrow().get(&key).copied();
        let v = match cached {
            Some(v) => v,
            None => {
                // The most objects placing makes, the art of opacity masks included, is set aside
                // while the art is placed and measured, and what placing did not make is given back.
                let art = self.symbol_art(name)?;
                let most = match self.slice_map(name, linear) {
                    Some(m) => self.in_art(|| m.most_placed_with(art, self)),
                    None => art.count_with_masks() as u64,
                };
                let used = self.sliced_placed.get();
                if used.saturating_add(most) > self.sliced_most {
                    return None;
                }
                self.sliced_placed.set(used + most);
                let (v, placed) = self.place_and_count(name, linear, MAX_SYMBOL_NODES).unwrap_or((None, 0));
                self.sliced_placed.set(self.sliced_placed.get().saturating_sub(most.saturating_sub(placed)));
                self.sliced.borrow_mut().insert(key, v);
                v
            }
        };
        v.map(|v| moved.transform_rect_bbox(v))
    }

    /// [`Document::symbol_slice_map`] with the guides cutting the art box these boxes measure for
    /// the symbol (its geometric part, which does not depend on the mode of the boxes).
    pub(crate) fn slice_map(&self, name: &str, xf: Affine) -> Option<NineSliceMap> {
        let guides = self.doc.symbol_nine_slice(name)?;
        let art = self.art_bounds(name).or_else(|| self.symbol_art(name)?.geometric_bounds())?;
        NineSliceMap::new(guides, art, self.doc.symbol_natural_xf(name), xf)
    }

    /// The box [`Self::placed_visual_within`] returns outside quick boxes.
    fn place_and_measure(&self, name: &str, xf: Affine, most: u64) -> Option<Rect> {
        self.place_and_count(name, xf, most).and_then(|(v, _)| v)
    }

    /// [`Self::place_and_measure`], with the number of objects of the placed art, the art of
    /// opacity masks included. `None` when the art is not placed.
    fn place_and_count(&self, name: &str, xf: Affine, most: u64) -> Option<(Option<Rect>, u64)> {
        self.measure_art(name, |art| {
            let art = match self.slice_map(name, xf) {
                Some(m) if self.in_art(|| m.most_placed_with(art, self)) > most => return None,
                Some(m) => self.in_art(|| m.place_with(art, self)),
                None => transformed(art, xf),
            };
            Some((self.in_art(|| art.visual(Some(self))), art.count_with_masks() as u64))
        })
        .flatten()
    }

    /// Runs `f` with instances measured by their art boxes alone.
    pub(crate) fn quickly<R>(&self, f: impl FnOnce() -> R) -> R {
        let was = self.quick.replace(true);
        let r = f();
        self.quick.set(was);
        r
    }

    /// Runs `f` as symbol art is measured: instances measured by their art boxes alone, with the
    /// strokes in their art, and without [`Self::brush_pad`], which the instance that shows the
    /// art adds once.
    fn in_art<R>(&self, f: impl FnOnce() -> R) -> R {
        let pad = self.brush_art.replace(false);
        let strokes = self.art_strokes.replace(true);
        let consulted = self.consulted.take();
        let r = self.quickly(f);
        self.brush_art.set(pad);
        self.art_strokes.set(strokes);
        *self.consulted.borrow_mut() = consulted;
        r
    }

    /// The names of the symbols of the document that the instances in `art` show, each once.
    fn shown(&self, art: &Node) -> Vec<String> {
        let mut seen = HashSet::new();
        let mut names = vec![];
        art.walk(&mut |n| {
            if let NodeKind::SymbolInstance { symbol, .. } = &n.kind
                && seen.insert(symbol.as_str())
                && self.symbol_art(symbol).is_some()
            {
                names.push(symbol.clone());
            }
        });
        names
    }
}

/// Records every symbol on `path` in `learned` as one whose instances are measured by
/// [`SYMBOL_SQUARE`]. Returns `None`.
fn give_up(path: &[(String, Vec<String>, usize)], learned: &mut HashMap<String, Option<usize>>) -> Option<usize> {
    for (n, ..) in path {
        learned.insert(n.clone(), None);
    }
    None
}

/// Measures symbol art `art`, with the instances in it measured by `symbols`.
fn art_box(art: &Arc<Node>, symbols: &SymbolBoxes) -> Option<ArtBox> {
    let finite = |r: &Rect| [r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite());
    let geometric = art.geometric(Some(symbols)).filter(finite)?;
    let visual = art.visual(Some(symbols)).filter(finite).map_or(geometric, |v| v.union(geometric));
    let reach = crate::live::max_outset(std::slice::from_ref(art), Some(symbols));
    let mut effects = false;
    let mut brush: Option<f64> = None;
    let mut sliced = false;
    art.walk(&mut |n| {
        let mut own = n.appearance.effects.iter().chain(n.appearance.items.iter().flat_map(|i| i.effects().iter()));
        effects |= own.any(|e| e.visible);
        let strokes = || n.appearance.items.iter().filter_map(|i| if let AppearanceItem::Stroke(s) = i { Some(s) } else { None });
        if strokes().any(|s| s.visible && s.brush.is_some()) {
            brush = Some(strokes().fold(brush.unwrap_or(0.0), |w, s| w.max(s.width)));
        }
        if let NodeKind::SymbolInstance { symbol, .. } = &n.kind {
            effects |= symbols.has_effects(symbol);
            if let Some(w) = symbols.brush_width(symbol) {
                brush = Some(brush.map_or(w, |b| b.max(w)));
            }
            sliced |= symbols.doc.symbol_nine_slice(symbol).is_some() || symbols.art(symbol).is_some_and(|a| a.sliced);
        }
    });
    let mut reshaped = None;
    if effects {
        art.walk(&mut |n| {
            let b = match &n.kind {
                NodeKind::SymbolInstance { symbol, .. } if symbols.art(symbol).is_some_and(|a| a.reshaped.is_some()) => n.reach(Some(symbols)),
                _ if n.clips() => n.children().and_then(|c| c.first()).and_then(|c| crate::hit::reshaped_clip_box(c)),
                _ => None,
            };
            reshaped = vectorcraft_geom::union_opt(reshaped, b.filter(finite));
        });
    }
    Some(ArtBox { geometric, visual, reach, effects, brush, sliced, reshaped })
}

#[cfg(test)]
mod tests {
    use vectorcraft_color::{Color, Paint};
    use vectorcraft_geom::{Point, shapes};

    use super::*;
    use crate::{Appearance, NodeId, Symbol};

    /// A rectangle filled black, with a `stroke` pt black stroke (0: none).
    fn rect(d: &mut Document, r: Rect, stroke: f64) -> Node {
        let paint = if stroke > 0.0 { Paint::solid(Color::BLACK) } else { Paint::None };
        Node::path(d.alloc_id(), shapes::rectangle(r), Appearance::basic(Paint::solid(Color::BLACK), paint, stroke))
    }

    fn instance(d: &mut Document, symbol: &str, xf: Affine) -> Node {
        Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: symbol.into(), xf })
    }

    /// Symbol `name` holding `art` in a group.
    fn symbol(d: &mut Document, name: &str, art: Vec<Node>) {
        let group = Node::group(d.alloc_id(), art.into_iter().map(Arc::new).collect());
        d.symbols.push(Symbol { dynamic: false, name: name.into(), art: Arc::new(group) });
    }

    #[test]
    fn an_instance_measures_as_its_art_placed_by_its_transform() {
        let mut d = Document::new(200.0, 200.0);
        let bar = rect(&mut d, Rect::new(0.0, 0.0, 20.0, 10.0), 2.0);
        symbol(&mut d, "Bar", vec![bar]);
        let moved = instance(&mut d, "Bar", Affine::translate((20.0, 30.0)));
        let scaled = instance(&mut d, "Bar", Affine::scale(3.0));
        let s = d.symbol_boxes();
        assert_eq!(moved.geometric_bounds_with(&s), Some(Rect::new(20.0, 30.0, 40.0, 40.0)));
        // Without the document: the 20 ร— 20 square around the origin, moved.
        assert_eq!(moved.geometric_bounds(), Some(Rect::new(10.0, 20.0, 30.0, 40.0)));
        // The 2 pt stroke paints 1 pt around the bar.
        assert_eq!(moved.visual_bounds_with(&s), Some(Rect::new(19.0, 29.0, 41.0, 41.0)));
        assert_eq!(moved.reach_bounds_with(&s), moved.visual_bounds_with(&s));
        // Scaled 3 times: the geometry scales, the stroke keeps its weight.
        assert_eq!(scaled.geometric_bounds_with(&s), Some(Rect::new(0.0, 0.0, 60.0, 30.0)));
        assert_eq!(scaled.visual_bounds_with(&s), Some(Rect::new(-1.0, -1.0, 61.0, 31.0)));
        // A click's box: the geometric box grown by the farthest the stroke can reach (a mitered
        // corner up to its miter limit).
        let reach = d.symbols[0].art.children().unwrap()[0].appearance.outset();
        assert_eq!(scaled.reach_bounds_with(&s), Some(Rect::new(0.0, 0.0, 60.0, 30.0).inflate(reach, reach)));
    }

    #[test]
    fn quick_boxes_hold_art_that_9_slice_guides_move_past_the_instance() {
        // A 100 ร— 100 frame with guides a quarter in, holding an instance of a bar from x 10 to 70.
        // Scaled 2 times across, the bar crosses the left guide and is placed by the middle
        // column's map, x โ†’ 3x โˆ’ 50: it draws from x โˆ’20 to 160, past the frame's left edge at 0.
        let mut d = Document::new(200.0, 200.0);
        let bar = rect(&mut d, Rect::new(10.0, 40.0, 70.0, 60.0), 0.0);
        symbol(&mut d, "Bar", vec![bar]);
        let frame = rect(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), 0.0);
        let inner = instance(&mut d, "Bar", Affine::IDENTITY);
        symbol(&mut d, "Panel", vec![frame, inner]);
        d.set_symbol_nine_slice("Panel", Some(crate::SymbolNineSlice::default()));
        let panel = instance(&mut d, "Panel", Affine::scale_non_uniform(2.0, 1.0));
        let exact = panel.visual_bounds_with(&d.symbol_boxes()).unwrap();
        assert_eq!(exact, Rect::new(-20.0, 0.0, 200.0, 100.0));
        let quick = panel.visual_bounds_with(&d.symbol_boxes().quick()).unwrap();
        assert_eq!(quick.union(exact), quick, "{quick:?} holds {exact:?}");
    }

    #[test]
    fn quick_boxes_hold_art_that_nested_9_slice_guides_move_past_the_instance() {
        // The panel of the test above, in a symbol without guides: once at its natural size and
        // once scaled 2 times across. Where the bar draws depends on the transform the holder's
        // instance adds to the panel's.
        let mut d = Document::new(200.0, 200.0);
        let bar = rect(&mut d, Rect::new(10.0, 40.0, 70.0, 60.0), 0.0);
        symbol(&mut d, "Bar", vec![bar]);
        let frame = rect(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), 0.0);
        let inner = instance(&mut d, "Bar", Affine::IDENTITY);
        symbol(&mut d, "Panel", vec![frame, inner]);
        d.set_symbol_nine_slice("Panel", Some(crate::SymbolNineSlice::default()));
        let natural = instance(&mut d, "Panel", Affine::IDENTITY);
        symbol(&mut d, "Holder", vec![natural]);
        let wide = instance(&mut d, "Panel", Affine::scale_non_uniform(2.0, 1.0));
        symbol(&mut d, "Wide holder", vec![wide]);
        // The holder scaled 2 times across scales the panel 2 times across: the bar draws from x
        // โˆ’20 to 160, as in the test above.
        let holder = instance(&mut d, "Holder", Affine::scale_non_uniform(2.0, 1.0));
        let quick = holder.visual_bounds_with(&d.symbol_boxes().quick()).unwrap();
        let drawn = Rect::new(-20.0, 40.0, 160.0, 60.0);
        assert_eq!(quick.union(drawn), quick, "{quick:?} holds {drawn:?}");
        let wide_holder = instance(&mut d, "Wide holder", Affine::scale(2.0));
        for n in [&holder, &wide_holder] {
            let exact = n.visual_bounds_with(&d.symbol_boxes()).unwrap();
            assert!(exact.x0 < 0.0, "{exact:?} reaches past the frame's left edge");
            let quick = n.visual_bounds_with(&d.symbol_boxes().quick()).unwrap();
            assert_eq!(quick.union(exact), quick, "{quick:?} holds {exact:?}");
            let reach = n.reach_bounds_with(&d.symbol_boxes()).unwrap();
            assert_eq!(reach.union(exact), reach, "{reach:?} holds {exact:?}");
        }
    }

    #[test]
    fn quick_boxes_place_art_that_holds_9_slice_instances_a_bounded_number_of_times() {
        // A frame with guides, and 16 levels of symbols that each hold an instance of the level
        // below a little taller and one turned a little: the transforms that place the frame differ
        // for each of the 2^16 ways down, so measuring every one would place art 2^17 times.
        let mut d = Document::new(200.0, 200.0);
        let frame = rect(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), 0.0);
        symbol(&mut d, "Level 0", vec![frame]);
        d.set_symbol_nine_slice("Level 0", Some(crate::SymbolNineSlice::default()));
        let (taller, turned) = (Affine::scale_non_uniform(1.0, 1.01), Affine::rotate(0.1));
        for i in 1..=16 {
            let below = format!("Level {}", i - 1);
            let pair = vec![instance(&mut d, &below, taller), instance(&mut d, &below, turned)];
            symbol(&mut d, &format!("Level {i}"), pair);
        }
        let top = instance(&mut d, "Level 16", taller);
        // With room for 5,000 objects: each placement places at least one.
        let s = SymbolBoxes { sliced_most: 5000, ..d.symbol_boxes().quick() };
        assert!(top.visual_bounds_with(&s).is_some());
        assert!(s.sliced_placed.get() <= 5000, "{} objects", s.sliced_placed.get());
        assert!(s.art_measured() <= 5000 + d.symbols.len(), "{} measures", s.art_measured());
        // Instances whose transforms differ only by a move place the art once.
        let level = instance(&mut d, "Level 1", taller);
        let moved = instance(&mut d, "Level 1", Affine::translate((5.0, 7.0)) * taller);
        let s = d.symbol_boxes().quick();
        let b = level.visual_bounds_with(&s).unwrap();
        let measured = s.art_measured();
        assert_eq!(moved.visual_bounds_with(&s), Some(Affine::translate((5.0, 7.0)).transform_rect_bbox(b)));
        assert_eq!(s.art_measured(), measured);
        // The art of an opacity mask counts toward the limit: a group holding the framed
        // instance, masked by a group of 10 rectangles, is 13 objects.
        let mut framed = instance(&mut d, "Level 0", Affine::IDENTITY);
        let squares = (0..10).map(|_| Arc::new(rect(&mut d, Rect::new(0.0, 0.0, 10.0, 10.0), 0.0))).collect();
        let mask = Node::group(d.alloc_id(), squares);
        framed.extras_mut().mask = Some(Box::new(crate::OpacityMask::new(mask, true)));
        symbol(&mut d, "Masked", vec![framed]);
        let masked = instance(&mut d, "Masked", taller);
        let s = d.symbol_boxes().quick();
        assert!(masked.visual_bounds_with(&s).is_some());
        assert_eq!(s.sliced_placed.get(), 13);
    }

    #[test]
    fn symbols_are_found_by_name_reading_the_list_once() {
        // 2,000 symbols, then two named "Mark": a name finds the first.
        let mut d = Document::new(200.0, 200.0);
        for i in 0..2000 {
            let r = rect(&mut d, Rect::new(0.0, 0.0, 1.0, 1.0), 0.0);
            symbol(&mut d, &format!("Filler {i}"), vec![r]);
        }
        let (first, second) = (rect(&mut d, Rect::new(0.0, 0.0, 10.0, 10.0), 0.0), rect(&mut d, Rect::new(0.0, 0.0, 20.0, 20.0), 0.0));
        symbol(&mut d, "Mark", vec![first]);
        symbol(&mut d, "Mark", vec![second]);
        let read = || SYMBOLS_READ.with(Cell::get);
        let before = read();
        let s = d.symbol_boxes();
        for _ in 0..1000 {
            assert!(s.symbol("Mark").is_some_and(|m| std::ptr::eq(m, &d.symbols[2000])));
            assert!(s.symbol("Filler 7").is_some_and(|m| std::ptr::eq(m, &d.symbols[7])));
            assert!(s.symbol("Nowhere").is_none());
        }
        assert_eq!(read() - before, d.symbols.len());
    }

    #[test]
    fn unmeasured_boxes_share_the_index_and_nothing_measured() {
        // 2,000 symbols, then a 9-slice frame held, scaled, by "Holder", whose instances spend
        // the boxes' budget of placed objects.
        let mut d = Document::new(200.0, 200.0);
        for i in 0..2000 {
            let r = rect(&mut d, Rect::new(0.0, 0.0, 1.0, 1.0), 0.0);
            symbol(&mut d, &format!("Filler {i}"), vec![r]);
        }
        let frame = rect(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), 1.0);
        symbol(&mut d, "Panel", vec![frame]);
        d.set_symbol_nine_slice("Panel", Some(crate::SymbolNineSlice::default()));
        let wide = instance(&mut d, "Panel", Affine::scale_non_uniform(2.0, 1.0));
        symbol(&mut d, "Holder", vec![wide]);
        let holder = instance(&mut d, "Holder", Affine::scale_non_uniform(1.5, 1.5));
        let fresh = holder.visual_bounds_with(&d.symbol_boxes().quick());
        let read = || SYMBOLS_READ.with(Cell::get);
        let s = d.symbol_boxes().quick();
        assert_eq!(holder.visual_bounds_with(&s), fresh);
        assert!(s.sliced_placed.get() > 0);
        let before = read();
        let u = s.unmeasured();
        assert_eq!(read(), before, "the index is shared");
        assert!(u.measured.borrow().is_empty() && u.sliced.borrow().is_empty() && u.reaches.borrow().is_empty());
        assert_eq!((u.sliced_placed.get(), u.holders.get(), u.quick.get()), (0, 0, false));
        assert_eq!(holder.visual_bounds_with(&u.quick()), fresh);
        assert_eq!(read(), before);
    }

    #[test]
    fn measuring_9_slice_instances_reads_the_symbol_list_once() {
        // 2,000 symbols, then a frame with guides and a symbol with guides holding the frame
        // scaled 2 times across, each shown by 200 instances scaled by different amounts.
        let mut d = Document::new(200.0, 200.0);
        for i in 0..2000 {
            let dot = rect(&mut d, Rect::new(0.0, 0.0, 1.0, 1.0), 0.0);
            symbol(&mut d, &format!("Dot {i}"), vec![dot]);
        }
        let frame = rect(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), 1.0);
        symbol(&mut d, "Panel", vec![frame]);
        d.set_symbol_nine_slice("Panel", Some(crate::SymbolNineSlice::default()));
        let wide = instance(&mut d, "Panel", Affine::scale_non_uniform(2.0, 1.0));
        symbol(&mut d, "Holder", vec![wide]);
        d.set_symbol_nine_slice("Holder", Some(crate::SymbolNineSlice::default()));
        let scaled = |d: &mut Document, name: &str| -> Vec<Node> {
            (1..=200).map(|i| instance(d, name, Affine::scale_non_uniform(1.0 + f64::from(i) / 100.0, 1.5))).collect()
        };
        let (panels, holders) = (scaled(&mut d, "Panel"), scaled(&mut d, "Holder"));
        // What each instance measures in boxes of its own.
        let exact: Vec<_> = panels.iter().map(|n| n.visual_bounds_with(&d.symbol_boxes())).collect();
        let quick: Vec<_> = holders.iter().map(|n| n.visual_bounds_with(&d.symbol_boxes().quick())).collect();
        let read = || SYMBOLS_READ.with(Cell::get);
        let before = read();
        let s = d.symbol_boxes();
        assert_eq!(panels.iter().map(|n| n.visual_bounds_with(&s)).collect::<Vec<_>>(), exact);
        let s = d.symbol_boxes().quick();
        assert_eq!(holders.iter().map(|n| n.visual_bounds_with(&s)).collect::<Vec<_>>(), quick);
        // Each set of boxes reads the list once to find symbols by name.
        let symbols = d.symbols.len();
        assert!(read() - before <= 2 * symbols, "{} symbols read", read() - before);
    }

    #[test]
    fn missing_symbols_and_symbols_inside_themselves_measure_as_the_square() {
        let mut d = Document::new(200.0, 200.0);
        let missing = instance(&mut d, "Nowhere", Affine::translate((50.0, 50.0)));
        assert_eq!(missing.geometric_bounds_with(&d.symbol_boxes()), Some(Rect::new(40.0, 40.0, 60.0, 60.0)));
        // A symbol whose art holds an instance of itself 100 pt to the right, and a symbol whose
        // art holds an instance of that one.
        let square = rect(&mut d, Rect::new(0.0, 0.0, 10.0, 10.0), 0.0);
        let inner = instance(&mut d, "Loop", Affine::translate((100.0, 0.0)));
        symbol(&mut d, "Loop", vec![square.clone(), inner]);
        let looped = instance(&mut d, "Loop", Affine::IDENTITY);
        symbol(&mut d, "Holder", vec![square, looped]);
        let outer = instance(&mut d, "Loop", Affine::IDENTITY);
        let doubled = instance(&mut d, "Loop", Affine::scale(2.0));
        let holder = instance(&mut d, "Holder", Affine::translate((50.0, 50.0)));
        let s = d.symbol_boxes();
        assert_eq!(outer.geometric_bounds_with(&s), Some(Rect::new(-10.0, -10.0, 10.0, 10.0)));
        assert_eq!(outer.visual_bounds_with(&s), Some(Rect::new(-10.0, -10.0, 10.0, 10.0)));
        assert_eq!(doubled.visual_bounds_with(&s), Some(Rect::new(-20.0, -20.0, 20.0, 20.0)));
        assert_eq!(holder.geometric_bounds_with(&s), Some(Rect::new(40.0, 40.0, 60.0, 60.0)));
    }

    #[test]
    fn nested_instances_measure_by_their_own_symbols_art() {
        let mut d = Document::new(200.0, 200.0);
        let dot = rect(&mut d, Rect::new(0.0, 0.0, 4.0, 4.0), 0.0);
        symbol(&mut d, "Dot", vec![dot]);
        let right = instance(&mut d, "Dot", Affine::translate((50.0, 0.0)));
        let below = instance(&mut d, "Dot", Affine::translate((0.0, 50.0)));
        symbol(&mut d, "Pair", vec![right, below]);
        let pair = instance(&mut d, "Pair", Affine::translate((100.0, 100.0)));
        assert_eq!(pair.geometric_bounds_with(&d.symbol_boxes()), Some(Rect::new(100.0, 100.0, 154.0, 154.0)));
    }

    #[test]
    fn each_symbols_art_box_is_measured_once() {
        // `Pair` holds two instances of `Dot`; three instances of `Pair` are measured twice each.
        let mut d = Document::new(200.0, 200.0);
        let dot = rect(&mut d, Rect::new(0.0, 0.0, 4.0, 4.0), 0.0);
        symbol(&mut d, "Dot", vec![dot]);
        let right = instance(&mut d, "Dot", Affine::translate((50.0, 0.0)));
        let below = instance(&mut d, "Dot", Affine::translate((0.0, 50.0)));
        symbol(&mut d, "Pair", vec![right, below]);
        let pairs: Vec<Node> = (0..3).map(|i| instance(&mut d, "Pair", Affine::translate((f64::from(i) * 10.0, 0.0)))).collect();
        let s = d.symbol_boxes().quick();
        assert_eq!(s.art_measured(), 0);
        for _ in 0..2 {
            for p in &pairs {
                p.geometric_bounds_with(&s);
            }
        }
        assert_eq!(s.art_measured(), 2, "the art of Pair and of Dot");
    }

    #[test]
    fn symbols_nested_32_deep_measure_as_the_square_whatever_was_measured_first() {
        // A chain of 40 symbols, each holding a 1 ร— 1 square and an instance of the next one 1 pt to
        // the right: symbols nest 39 deep in Link 0's art and 31 deep in Link 8's.
        let mut d = Document::new(200.0, 200.0);
        for i in 0..40 {
            let unit = rect(&mut d, Rect::new(0.0, 0.0, 1.0, 1.0), 0.0);
            let next = instance(&mut d, &format!("Link {}", i + 1), Affine::translate((1.0, 0.0)));
            symbol(&mut d, &format!("Link {i}"), if i < 39 { vec![unit, next] } else { vec![unit] });
        }
        let link0 = instance(&mut d, "Link 0", Affine::IDENTITY);
        let link7 = instance(&mut d, "Link 7", Affine::IDENTITY);
        let link8 = instance(&mut d, "Link 8", Affine::IDENTITY);
        let (square, chain) = (Some(Rect::new(-10.0, -10.0, 10.0, 10.0)), Some(Rect::new(0.0, 0.0, 32.0, 1.0)));
        let s = d.symbol_boxes();
        assert_eq!(link0.geometric_bounds_with(&s), square);
        assert_eq!(link8.geometric_bounds_with(&s), chain);
        assert_eq!(link7.geometric_bounds_with(&s), square);
        let s = d.symbol_boxes();
        assert_eq!(link8.geometric_bounds_with(&s), chain);
        assert_eq!(link0.geometric_bounds_with(&s), square);
    }

    #[test]
    fn brushed_strokes_are_found_in_the_art_and_in_nested_symbols_art() {
        let mut d = Document::new(200.0, 200.0);
        let mut line = rect(&mut d, Rect::new(0.0, 0.0, 10.0, 10.0), 3.0);
        line.appearance.stroke_mut().unwrap().brush = Some("3 pt. Round".into());
        symbol(&mut d, "Brushed", vec![line]);
        let plain = rect(&mut d, Rect::new(0.0, 0.0, 10.0, 10.0), 5.0);
        let inner = instance(&mut d, "Brushed", Affine::IDENTITY);
        symbol(&mut d, "Holder", vec![plain.clone(), inner]);
        symbol(&mut d, "Plain", vec![plain]);
        let s = d.symbol_boxes();
        assert_eq!(s.brush_width("Brushed"), Some(3.0));
        assert_eq!(s.brush_width("Holder"), Some(3.0), "the 5 pt stroke has no brush");
        assert_eq!(s.brush_width("Plain"), None);
    }

    #[test]
    fn boxes_with_brush_art_grow_instances_of_brushed_art_once() {
        let mut d = Document::new(200.0, 200.0);
        // A line from (0, 40) to (0, 60) with a 1 pt brushed stroke, and a symbol whose art holds an
        // instance of it.
        let mut stroke = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 1.0);
        stroke.stroke_mut().unwrap().brush = Some("3 pt. Round".into());
        let line = Node::path(d.alloc_id(), shapes::line((0.0, 40.0).into(), (0.0, 60.0).into()), stroke);
        symbol(&mut d, "Nib", vec![line]);
        let inner = instance(&mut d, "Nib", Affine::IDENTITY);
        symbol(&mut d, "Holder", vec![inner]);
        let nib = instance(&mut d, "Nib", Affine::translate((-5.0, 0.0)));
        let holder = instance(&mut d, "Holder", Affine::translate((-5.0, 0.0)));
        let group = Node::group(d.alloc_id(), vec![Arc::new(nib.clone())]);
        let repeat = Node::new(d.alloc_id(), NodeKind::Repeat(crate::RepeatSpec::mirror(vec![Arc::new(nib.clone())], 90.0, 10.0)));
        let (plain, brushed) = (d.symbol_boxes(), d.symbol_boxes().with_brush_art());
        // 16 ร— 1 + 8 for the 1 pt stroke.
        for n in [&nib, &holder, &group, &repeat] {
            let b = n.visual_bounds_with(&plain).unwrap();
            assert_eq!(n.visual_bounds_with(&brushed), Some(b.inflate(24.0, 24.0)), "{}", n.kind_label());
        }
        assert_eq!(nib.geometric_bounds_with(&brushed), nib.geometric_bounds_with(&plain));
    }

    #[test]
    fn effect_reaches_are_measured_once_for_each_linear_part_and_up_to_a_limit() {
        // `Leaf` holds a square; `Holder` and `Other` each hold an instance of `Leaf`.
        let mut d = Document::new(10.0, 10.0);
        let square = rect(&mut d, Rect::new(0.0, 0.0, 1.0, 1.0), 0.0);
        symbol(&mut d, "Leaf", vec![square]);
        for name in ["Holder", "Other"] {
            let leaf = instance(&mut d, "Leaf", Affine::IDENTITY);
            symbol(&mut d, name, vec![leaf]);
        }
        let s = d.symbol_boxes();
        let runs = &Cell::new(0);
        // A measure that finds `reach` for every linear part.
        let measure = |reach: f64| {
            move |_: [f64; 4]| {
                runs.set(runs.get() + 1);
                reach
            }
        };
        let unscaled = [1.0, 0.0, 0.0, 1.0];
        assert_eq!(s.effects_reach("Holder", unscaled, &measure(3.0)), 3.0);
        assert_eq!(s.effects_reach("Holder", unscaled, &measure(5.0)), 3.0, "measured once");
        assert_eq!(runs.get(), 1);
        for i in 1..MAX_REACHES {
            s.effects_reach("Holder", [1.0, 0.0, 0.0, 1.0 + i as f64], &measure(1.0));
        }
        assert_eq!(runs.get(), MAX_REACHES);
        // Past the limit: the reach with no transform, times โˆš2 and the larger of 1 and the
        // Frobenius norm (โˆš8 for a scale of 2, โˆš2 with no transform, โˆš0.5 for a scale of 0.5). That
        // reach is measured once more for a symbol not measured before.
        let near = |a: f64, b: f64| (a - b).abs() < 1e-9;
        assert!(near(s.effects_reach("Holder", [2.0, 0.0, 0.0, 2.0], &measure(9.0)), 12.0));
        assert!(near(s.effects_reach("Other", unscaled, &measure(9.0)), 18.0));
        assert!(near(s.effects_reach("Other", [0.5, 0.0, 0.0, 0.5], &measure(7.0)), 9.0 * std::f64::consts::SQRT_2));
        assert_eq!(runs.get(), MAX_REACHES + 1);
        // A symbol whose art holds no instance is measured past the limit too.
        assert_eq!(s.effects_reach("Leaf", [2.0, 0.0, 0.0, 2.0], &measure(7.0)), 7.0);
        assert_eq!(runs.get(), MAX_REACHES + 2);
    }

    /// How many art boxes `f` measures on this thread.
    fn art_boxes_measured(f: impl FnOnce()) -> usize {
        let before = ART_BOXES_MEASURED.with(Cell::get);
        f();
        ART_BOXES_MEASURED.with(Cell::get) - before
    }

    /// A document with symbols "S 0" to "S 9", each a 10 ร— 10 square around the origin, and an
    /// instance of each at (40 i + 20, 20).
    fn ten_symbols() -> Document {
        let mut d = Document::new(400.0, 100.0);
        let layer = d.layers.first().map(|l| l.id);
        for i in 0..10 {
            let square = rect(&mut d, Rect::new(-5.0, -5.0, 5.0, 5.0), 0.0);
            symbol(&mut d, &format!("S {i}"), vec![square]);
            let at = instance(&mut d, &format!("S {i}"), Affine::translate((40.0 * f64::from(i) + 20.0, 20.0)));
            d.insert(layer, usize::MAX, at).unwrap();
        }
        d
    }

    #[test]
    fn hit_tests_measure_each_symbols_art_once_while_the_symbols_stay_the_same() {
        use crate::hit::{self, HitOptions};
        let d = ten_symbols();
        let clicks = || {
            for i in 0..10 {
                let p = Point::new(40.0 * f64::from(i) + 20.0, 20.0);
                assert!(hit::hit_test(&d, p, HitOptions::default()).is_some(), "instance {i} is hit");
                assert_eq!(hit::objects_at(&d, p, HitOptions::default(), None).len(), 1);
            }
            assert_eq!(hit::marquee(&d, Rect::new(0.0, 0.0, 400.0, 100.0), None, false).len(), 10);
        };
        let first = art_boxes_measured(clicks);
        assert_eq!(first, 10, "each symbol's art box measured once");
        assert_eq!(art_boxes_measured(clicks), 0, "art boxes measured again");
    }

    #[test]
    fn hit_tests_measure_a_symbol_again_after_it_changes() {
        use crate::hit::{self, HitOptions};
        let mut d = ten_symbols();
        // Past the square of "S 1" at (60, 20).
        let past = Point::new(71.0, 20.0);
        assert!(hit::hit_test(&d, past, HitOptions::default()).is_none());
        // The art grows 3 times in place: no other copy of it is held in the document.
        Arc::make_mut(&mut d.symbols[1].art).transform(Affine::scale(3.0), false);
        let measured = art_boxes_measured(|| assert!(hit::hit_test(&d, past, HitOptions::default()).is_some(), "the grown art is hit"));
        assert!(measured > 0);
        // New art for "S 2", 40 wide.
        let wide = rect(&mut d, Rect::new(-20.0, -5.0, 20.0, 5.0), 0.0);
        d.symbols[2].art = Arc::new(wide);
        assert!(hit::hit_test(&d, Point::new(115.0, 20.0), HitOptions::default()).is_some(), "the new art is hit");
        // "S 3" renamed: its instance now shows the 20 ร— 20 square.
        d.symbols[3].name = "Renamed".into();
        assert!(hit::hit_test(&d, Point::new(148.0, 28.0), HitOptions::default()).is_some(), "the square is hit");
        assert_eq!(hits(&d), fresh_hits(&d));
    }

    /// What clicks on a grid over `d` hit, and what a marquee over it selects.
    fn hits(d: &Document) -> (Vec<Option<Vec<NodeId>>>, Vec<NodeId>) {
        use crate::hit::{self, HitOptions};
        let mut clicks = Vec::new();
        for y in (0..400).step_by(5) {
            for x in (0..400).step_by(5) {
                let p = Point::new(f64::from(x), f64::from(y));
                clicks.push(hit::hit_test(d, p, HitOptions::default()).map(|h| h.ancestry));
            }
        }
        (clicks, hit::marquee(d, Rect::new(0.0, 0.0, 400.0, 400.0), None, false))
    }

    /// [`hits`] with no measurements kept from earlier hit tests.
    fn fresh_hits(d: &Document) -> (Vec<Option<Vec<NodeId>>>, Vec<NodeId>) {
        KEPT.with_borrow_mut(Vec::clear);
        hits(d)
    }

    /// A bar in a panel with or without 9-slice guides, in a holder whose instance at (50, 50) is
    /// scaled 2 times across. With the guides, the bar draws from x 30 to 210 and y 90 to 110; without
    /// them, from x 70 to 190, inside the panel's frame from x 50 to 250 and y 50 to 150.
    fn sliced_holder(guides: bool) -> Document {
        let mut d = Document::new(400.0, 400.0);
        let bar = rect(&mut d, Rect::new(10.0, 40.0, 70.0, 60.0), 0.0);
        symbol(&mut d, "Bar", vec![bar]);
        let frame = rect(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), 0.0);
        let inner = instance(&mut d, "Bar", Affine::IDENTITY);
        symbol(&mut d, "Panel", vec![frame, inner]);
        if guides {
            d.set_symbol_nine_slice("Panel", Some(crate::SymbolNineSlice::default()));
        }
        let panel = instance(&mut d, "Panel", Affine::IDENTITY);
        symbol(&mut d, "Holder", vec![panel]);
        let layer = d.layers.first().map(|l| l.id);
        let holder = instance(&mut d, "Holder", Affine::translate((50.0, 50.0)) * Affine::scale_non_uniform(2.0, 1.0));
        d.insert(layer, usize::MAX, holder).unwrap();
        d
    }

    #[test]
    fn hit_tests_follow_new_9_slice_guides_and_symbol_sizes() {
        use crate::hit::{self, HitOptions};
        let mut d = sliced_holder(false);
        // Left of the frame, where only the bar placed through the guides draws.
        let left = Point::new(40.0, 100.0);
        assert!(hit::hit_test(&d, left, HitOptions::default()).is_none());
        d.set_symbol_nine_slice("Panel", Some(crate::SymbolNineSlice::default()));
        assert!(hit::hit_test(&d, left, HitOptions::default()).is_some(), "the bar placed through the guides is hit");
        assert_eq!(hits(&d), fresh_hits(&d));
        let before = hits(&d);
        d.unknown.insert(crate::SYMBOL_SIZES.into(), serde_json::json!({ "Panel": [200.0, 100.0] }));
        let after = hits(&d);
        assert_ne!(after, before, "the size moves the guides");
        assert_eq!(after, fresh_hits(&d));
    }

    #[test]
    fn hit_tests_measure_symbols_holding_9_slice_instances_on_each_call() {
        use crate::hit::{self, HitOptions};
        let d = sliced_holder(true);
        let left = Point::new(40.0, 100.0);
        assert!(hit::hit_test(&d, left, HitOptions::default()).is_some());
        // The holder's art holds an instance of a symbol with guides; the panel's art holds none.
        KEPT.with_borrow(|k| {
            let measured = &k[0].measured;
            assert!(measured.contains_key("Panel") && !measured.contains_key("Holder"), "{:?}", measured.keys());
        });
        let again = art_boxes_measured(|| assert!(hit::hit_test(&d, left, HitOptions::default()).is_some()));
        assert!(again > 0, "the holder's art box measured again");
    }

    #[test]
    fn kept_measurements_depend_on_the_inputs_of_clipping_path_effects() {
        let d = ten_symbols();
        let key = KeptKey::of(&d, (0, false));
        assert!(key.is_of(&d, (0, false)));
        assert!(!key.is_of(&d, (1, false)), "live inputs changed");
        assert!(!key.is_of(&d, (0, true)), "a clip region registered");
    }

    #[test]
    fn hit_tests_with_a_locked_artboard_measure_each_symbols_art_once() {
        use crate::hit::{self, HitOptions};
        let mut d = ten_symbols();
        d.artboards[0].locked = true;
        let clicks = || {
            for i in 0..10 {
                let p = Point::new(40.0 * f64::from(i) + 20.0, 20.0);
                assert!(hit::hit_test(&d, p, HitOptions::default()).is_none(), "instance {i} is on the locked artboard");
                assert!(hit::objects_at(&d, p, HitOptions::default(), None).is_empty());
            }
        };
        assert!(art_boxes_measured(clicks) >= 10, "each symbol's art box measured");
        assert_eq!(art_boxes_measured(clicks), 0, "art boxes measured again");
        // A marquee passes over the objects on a locked artboard without measuring them itself: only
        // its locks measure their art.
        let mut d = ten_symbols();
        d.artboards[0].locked = true;
        let marquee = || assert!(hit::marquee(&d, Rect::new(0.0, 0.0, 400.0, 100.0), None, false).is_empty());
        assert_eq!(art_boxes_measured(marquee), 10, "each symbol's art box measured once");
        assert_eq!(art_boxes_measured(marquee), 0, "art boxes measured again");
    }

    #[test]
    fn hit_tests_hold_no_symbol_art() {
        use crate::hit::{self, HitOptions};
        let d = ten_symbols();
        drop(hit::hit_test(&d, Point::new(20.0, 20.0), HitOptions::default()));
        assert!(KEPT.with_borrow(|k| !k.is_empty()));
        assert!(d.symbols.iter().all(|s| Arc::strong_count(&s.art) == 1), "only the document holds the symbols' art");
    }

    #[test]
    fn hit_tests_keep_the_measurements_of_two_sets_of_symbols() {
        use crate::hit::{self, HitOptions};
        let docs = [ten_symbols(), ten_symbols(), ten_symbols()];
        let p = Point::new(20.0, 20.0);
        for d in &docs {
            drop(hit::hit_test(d, p, HitOptions::default()));
        }
        assert_eq!(KEPT.with_borrow(Vec::len), KEPT_SETS);
        // The last two are kept, the first is measured again.
        let kept = art_boxes_measured(|| docs[1..].iter().for_each(|d| drop(hit::hit_test(d, p, HitOptions::default()))));
        assert_eq!(kept, 0);
        let first = art_boxes_measured(|| drop(hit::hit_test(&docs[0], p, HitOptions::default())));
        assert!(first > 0);
    }
}