Files
vectorcraft/crates/effects/src/preview.rs
T
dotCooCoo 6a493e0fee Use Preview Bounds: clicks on a layer with a 3D effect select its members
The art that clicks and marquees test leaves 3D effects unprojected again.
The faces of a layer's 3D effect carry the layer's id, which a click
cannot select, so a click on a member of such a layer selected nothing.
Use Preview Bounds still measures the projected art.
2026-11-16 00:24:24 -08:00

464 lines
22 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Use Preview Bounds: objects measured as they are drawn, with geometry and projected 3D effects.
//!
//! [`drawn_art`] gives an object whose geometry effects change what it draws as plain art. A path
//! or compound path is given as its effected geometry, and a fill or stroke with geometry effects
//! of its own paints its own geometry. Type, images, live objects and symbol instances are given as
//! their reshaped art ([`reshape`]), and a symbol instance is also given as its reshaped art when
//! its symbol's art has geometry effects. Groups and layers with geometry or Pathfinder effects of
//! their own, or with a fill or stroke of their own that has geometry effects or paints members
//! with geometry effects, are given as their evaluated art ([`evaluate_container`]). Blends,
//! envelopes and repeats whose objects have geometry effects are given as their evaluated art
//! ([`expanded_live_group`]), with each of those objects given as its own art. A clip group or
//! layer whose clipping path has strokes with geometry effects is given as that path's geometry
//! before its effects, which the renderer clips by, and those strokes as drawn. Fills and
//! strokes that paint nothing (hidden, or painted None) are left out. Extrude & Bevel, Revolve,
//! Inflate and Rotate are measured by the same projected vector art the renderer draws, with
//! any geometry effects after projection applied to that art; [`drawn_art`], which clicks and
//! marquees use, leaves 3D effects unprojected. [`bounds_of`],
//! [`oriented_bounds`] and [`anchor_bounds`] measure the visual bounds of that art, strokes
//! included, where [`Document::bounds_of`], [`Document::oriented_bounds`] and
//! [`Selection::anchor_bounds`] measure the objects themselves. Raster effects (shadows, glows,
//! blurs) are not measured. In one object, the art of at most 4,096 symbol instances is measured
//! with its geometry and 3D effects, and the other instances in it are measured without them.
//! [`bounds_of_with`], [`oriented_bounds_with`] and [`anchor_bounds_with`] measure as these do and,
//! with [`Measure::glyphs`], take point type by the ink of its glyphs (Measure Type by Glyph Bounds).
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::sync::Arc;
use vectorcraft_doc::color::Paint;
use vectorcraft_doc::{Appearance, AppearanceItem, Document, Node, NodeId, NodeKind, OrientedBox, Selection, SymbolBoxes};
use vectorcraft_geom::{Affine, PathData, Rect, Shape, union_opt};
use crate::group::has_own_paint;
use crate::reshape::{geometry_effects, leaf_geometry};
use crate::{
GeomContext, apply_geometry_with, evaluate_container, expanded_live_group, has_geometry, has_item_geometry, has_pathfinder, has_revolve,
has_solid, has_transform_art, needs_outline, outline_art, paints, reshape, revolve_art_in, solid_art_in,
};
/// Most symbol instances in one object whose art [`Drawn`] measures with geometry or 3D effects:
/// past it, the other instances in the object are measured without those effects.
const MAX_INSTANCES: usize = 4096;
/// The bounds of `ids`: with `preview`, their visual bounds as drawn ([`visual_bounds`]), else
/// their geometric bounds ([`Document::bounds_of`]).
pub fn bounds_of(doc: &Document, ids: &[NodeId], preview: bool) -> Option<Rect> {
if !preview {
return doc.bounds_of(ids, false);
}
let symbols = doc.symbol_boxes();
let drawn = Drawn::new(&symbols);
ids.iter().filter_map(|id| doc.node(*id)).fold(None, |acc, n| union_opt(acc, drawn.next().visual_bounds(n)))
}
/// The bounding box of `ids` square to their shared angle ([`Document::bbox_angle`]), measured as
/// [`bounds_of`] measures them: [`Document::oriented_bounds`] with `preview` false.
pub fn oriented_bounds(doc: &Document, ids: &[NodeId], preview: bool) -> Option<OrientedBox> {
if !preview {
return doc.oriented_bounds(ids, false);
}
let angle = doc.bbox_angle(ids);
if angle == 0.0 {
return bounds_of(doc, ids, true).map(OrientedBox::aligned);
}
let to_local = OrientedBox { angle, rect: Rect::ZERO }.to_doc().inverse();
let symbols = doc.symbol_boxes();
let drawn = Drawn::new(&symbols);
let rect = ids.iter().filter_map(|id| doc.node(*id)).fold(None, |acc, n| union_opt(acc, drawn.next().bounds_in(n, to_local)))?;
Some(OrientedBox { angle, rect })
}
/// With anchors selected with Direct Selection, the box around those anchors and the objects
/// selected whole, measured by [`bounds_of`]. `None` without any ([`Selection::anchor_bounds`]).
pub fn anchor_bounds(selection: &Selection, doc: &Document, preview: bool) -> Option<Rect> {
if selection.anchors.is_empty() {
return None;
}
union_opt(bounds_of(doc, &selection.whole(), preview), selection.anchors_box(doc))
}
/// How [`bounds_of_with`], [`oriented_bounds_with`] and [`anchor_bounds_with`] measure objects:
/// `preview` as in [`bounds_of`], and with `glyphs` (Preferences › Type › Measure Type by Glyph
/// Bounds), point type, horizontal or vertical, by the ink of its glyphs instead of its em box
/// ([`text_ink_in`]).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Measure {
pub preview: bool,
pub glyphs: bool,
}
impl Measure {
/// See [`by_glyphs`].
fn by_glyphs(self, drawn: &Drawn, n: &Node) -> bool {
self.glyphs
&& matches!(&n.kind, NodeKind::Text(t) if matches!(t.kind, vectorcraft_doc::TextKind::Point))
&& n.projection().is_none()
&& !(self.preview && drawn.draws_geometry(n))
}
}
/// Whether `m` measures `n` by the ink of its glyphs: point type, not in perspective, and with
/// `preview` only when no geometry effect changes what it draws.
pub fn by_glyphs(symbols: &SymbolBoxes, n: &Node, m: Measure) -> bool {
m.by_glyphs(&Drawn::new(symbols), n)
}
/// [`bounds_of`] measured by `m`.
pub fn bounds_of_with(doc: &Document, ids: &[NodeId], m: Measure) -> Option<Rect> {
if !m.glyphs {
return bounds_of(doc, ids, m.preview);
}
let symbols = doc.symbol_boxes();
let drawn = Drawn::new(&symbols);
if !ids.iter().filter_map(|id| doc.node(*id)).any(|n| m.by_glyphs(&drawn, n)) {
return bounds_of(doc, ids, m.preview);
}
ids.iter().filter_map(|id| doc.node(*id)).fold(None, |acc, n| union_opt(acc, measured_in(&drawn, n, Affine::IDENTITY, m)))
}
/// [`oriented_bounds`] measured by `m`.
pub fn oriented_bounds_with(doc: &Document, ids: &[NodeId], m: Measure) -> Option<OrientedBox> {
if !m.glyphs {
return oriented_bounds(doc, ids, m.preview);
}
let symbols = doc.symbol_boxes();
let drawn = Drawn::new(&symbols);
if !ids.iter().filter_map(|id| doc.node(*id)).any(|n| m.by_glyphs(&drawn, n)) {
return oriented_bounds(doc, ids, m.preview);
}
let angle = doc.bbox_angle(ids);
let to_local = if angle == 0.0 { Affine::IDENTITY } else { OrientedBox { angle, rect: Rect::ZERO }.to_doc().inverse() };
let rect = ids.iter().filter_map(|id| doc.node(*id)).fold(None, |acc, n| union_opt(acc, measured_in(&drawn, n, to_local, m)))?;
Some(OrientedBox { angle, rect })
}
/// [`anchor_bounds`] with the objects selected whole measured by `m`.
pub fn anchor_bounds_with(selection: &Selection, doc: &Document, m: Measure) -> Option<Rect> {
if selection.anchors.is_empty() {
return None;
}
union_opt(bounds_of_with(doc, &selection.whole(), m), selection.anchors_box(doc))
}
/// The box of `n` as `a` would place it, without changing it, measured by `m`: [`bounds_in`] with
/// `preview`, else [`Node::bounds_in`] of its geometric bounds, and point type by its glyphs as
/// [`Measure`] says.
pub fn bounds_in_with(symbols: &SymbolBoxes, n: &Node, a: Affine, m: Measure) -> Option<Rect> {
measured_in(&Drawn::new(symbols), n, a, m)
}
/// See [`bounds_in_with`].
fn measured_in(drawn: &Drawn, n: &Node, a: Affine, m: Measure) -> Option<Rect> {
if m.by_glyphs(drawn, n)
&& let NodeKind::Text(t) = &n.kind
&& let Some(r) = text_ink_in(t, a)
{
// The fills' and characters' strokes paint around the glyphs, as in `Node::visual_bounds`.
let o = if m.preview { n.appearance.outset().max(t.char_stroke_outset()) } else { 0.0 };
return Some(r.inflate(o, o));
}
match (m.preview, a == Affine::IDENTITY) {
(true, _) => drawn.next().bounds_in(n, a),
(false, true) => n.geometric_bounds_with(drawn.symbols),
(false, false) => n.bounds_in(drawn.symbols, a, false),
}
}
/// The box of the ink of type `t`'s glyphs with its text space mapped by `a * t.xf`: the cached
/// ink ([`vectorcraft_doc::TextObject::ink`]) when that map keeps a rectangle square, else the
/// glyph outlines and the underline and strikethrough bars mapped point by point
/// ([`vectorcraft_text::envelope_ink`]). `None` when nothing draws (no text, only spaces without
/// underline or strikethrough, or glyphs without outlines).
pub fn text_ink_in(t: &vectorcraft_doc::TextObject, a: Affine) -> Option<Rect> {
let m = a * t.xf;
let [p, q, r, s, _, _] = m.as_coeffs();
let square = (q.abs() <= 1e-12 && r.abs() <= 1e-12) || (p.abs() <= 1e-12 && s.abs() <= 1e-12);
if square {
let ink = match t.ink() {
Some(ink) => ink,
None => vectorcraft_text::envelope_ink(t, Affine::IDENTITY, None),
};
return ink.map(|i| m.transform_rect_bbox(i));
}
vectorcraft_text::envelope_ink(t, m, None)
}
/// The ink of type `t` laid out as `layout`, in text space: its glyphs
/// ([`vectorcraft_text::TextLayout::ink_bounds`]) and its underline and strikethrough bars
/// ([`vectorcraft_text::decorations`]), what [`text_ink_in`] measures. `None` when nothing draws.
pub fn laid_out_ink(layout: &vectorcraft_text::TextLayout, t: &vectorcraft_doc::TextObject) -> Option<Rect> {
vectorcraft_text::decorations(layout, vectorcraft_text::FontDb::global(), t)
.into_iter()
.fold(layout.ink_bounds(), |acc, (_, bar)| union_opt(acc, Some(bar.bounding_box())))
}
/// The visual bounds of `n` as drawn: those of its [`drawn_art`] with its 3D effects projected,
/// else [`Node::visual_bounds_with`].
pub fn visual_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
Drawn::new(symbols).visual_bounds(n)
}
/// [`visual_bounds`] of `n` as `a` would place it, without changing it ([`Node::bounds_in`]):
/// the art is drawn where `n` is and then placed by `a`.
pub fn bounds_in(symbols: &SymbolBoxes, n: &Node, a: Affine) -> Option<Rect> {
Drawn::new(symbols).bounds_in(n, a)
}
/// `n` as plain art with the geometry effects in it applied, at every depth, symbol instances
/// measured by `symbols`, for clicks and marquees. Raster effects stay on the art. A group, layer or
/// intertwine without a clip is rebuilt with the drawn art of each member that has one. A clip group
/// or layer becomes a group of its clipping path without strokes and the drawn art of the path's
/// strokes, when they have one. 3D effects stay on the art unprojected, so each object in it keeps
/// its own id. `None` when no geometry effect in `n` changes what it draws.
pub fn drawn_art(symbols: &SymbolBoxes, n: &Node) -> Option<Node> {
Drawn::without_3d(symbols).drawn_art(n)
}
/// Whether `n` is measured by art of its own with Use Preview Bounds, or an object in it is: a
/// geometry or 3D effect in it changes what it draws. A symbol instance in `n` counts when its
/// symbol's art has geometry or 3D effects.
pub fn draws_geometry(symbols: &SymbolBoxes, n: &Node) -> bool {
Drawn::new(symbols).draws_geometry(n)
}
/// One measurement: whether each symbol checked so far draws geometry or 3D art, by symbol name,
/// and how many more instances it measures with those effects in the current object ([`MAX_INSTANCES`]).
/// With `three_d` false, 3D effects are left on the art unprojected ([`drawn_art`]).
struct Drawn<'s, 'd> {
symbols: &'s SymbolBoxes<'d>,
draws: RefCell<HashMap<String, bool>>,
left: Cell<usize>,
three_d: bool,
}
impl<'s, 'd> Drawn<'s, 'd> {
fn new(symbols: &'s SymbolBoxes<'d>) -> Self {
Drawn { symbols, draws: RefCell::default(), left: Cell::new(MAX_INSTANCES), three_d: true }
}
/// A measurement that projects no 3D effect, for clicks and marquees ([`drawn_art`]).
fn without_3d(symbols: &'s SymbolBoxes<'d>) -> Self {
Drawn { three_d: false, ..Self::new(symbols) }
}
/// Whether `n` has a 3D effect of its own that this measurement projects.
fn projects(&self, n: &Node) -> bool {
self.three_d && (has_revolve(n) || has_solid(n))
}
/// Sets the count of instances left back to [`MAX_INSTANCES`], for the next object.
fn next(&self) -> &Self {
self.left.set(MAX_INSTANCES);
self
}
/// See [`visual_bounds`].
fn visual_bounds(&self, n: &Node) -> Option<Rect> {
match self.drawn_art(n) {
Some(art) => art.visual_bounds_with(self.symbols),
None => n.visual_bounds_with(self.symbols),
}
}
/// See [`bounds_in`].
fn bounds_in(&self, n: &Node, a: Affine) -> Option<Rect> {
if a == Affine::IDENTITY {
return self.visual_bounds(n);
}
match self.drawn_art(n) {
Some(art) => art.bounds_in(self.symbols, a, true),
None => n.bounds_in(self.symbols, a, true),
}
}
/// See [`drawn_art`].
fn drawn_art(&self, n: &Node) -> Option<Node> {
let Some(mut art) = self.own_art(n) else { return self.clip_art(n).or_else(|| self.with_drawn_members(n)) };
// The art's members can carry geometry effects of their own: type's fills and strokes, the
// members of an evaluated group, the clipping path of an evaluated clip group and the
// objects in a symbol's art.
if let Some(m) = self.clip_art(&art).or_else(|| self.with_drawn_members(&art)) {
art = m;
}
Some(art)
}
/// See [`draws_geometry`].
fn draws_geometry(&self, n: &Node) -> bool {
let mut found = false;
n.walk(&mut |c| {
found = found
|| self.projects(c)
|| match &c.kind {
NodeKind::SymbolInstance { symbol, .. } => !geometry_effects(c).is_empty() || self.symbol_draws(symbol),
_ => reshapes(c),
};
});
found
}
/// Whether the art of the symbol named `name` has effects and [`Self::draws_geometry`], worked
/// out once for each name.
fn symbol_draws(&self, name: &str) -> bool {
if let Some(d) = self.draws.borrow().get(name) {
return *d;
}
let d = self.symbols.has_effects(name) && self.symbols.measure_art(name, |art| self.draws_geometry(art)).unwrap_or(false);
self.draws.borrow_mut().insert(name.to_string(), d);
d
}
/// The art the geometry or 3D effects of `n` itself give it, or that a symbol instance gets from
/// those effects in its symbol's art, with the instance's appearance overrides applied.
/// A group or layer with fills or strokes of its own that
/// paint, around members with geometry effects, is evaluated too: its fills and strokes paint
/// the members' geometry before their effects. A blend, envelope or repeat whose objects have
/// geometry effects is evaluated as the renderer evaluates it ([`expanded_live_group`]), and
/// the objects in its evaluated art keep their geometry effects. `None` when it has none, and
/// for a symbol instance past [`MAX_INSTANCES`].
fn own_art(&self, n: &Node) -> Option<Node> {
let doc = Some(self.symbols.document());
// A symbol instance with a 3D effect of its own counts toward MAX_INSTANCES before its art is built.
let counted = matches!(n.kind, NodeKind::SymbolInstance { .. }) && self.projects(n);
if counted {
let left = self.left.get().checked_sub(1)?;
self.left.set(left);
}
if self.three_d
&& let Some(art) = revolve_art_in(doc, n).or_else(|| solid_art_in(doc, n))
{
// The builders retain later geometry on the projected group. Measure it after that
// geometry too; item-level effects are handled by the ordinary member traversal.
return Some(self.own_art(&art).unwrap_or(art));
}
let inner = || n.children().is_some_and(|ch| ch.iter().any(|c| self.draws_geometry(c)));
let painted = || has_own_paint(n) && inner();
match &n.kind {
NodeKind::Group { .. } | NodeKind::Layer { .. } if reshapes(n) || painted() => evaluate_container(Some(self.symbols.document()), n),
// A Transform effect that copies with a gradient, scales strokes or moves patterns makes
// objects of their own, as the renderer draws them.
NodeKind::Path { .. } | NodeKind::Compound { .. } if has_transform_art(n) => reshape(Some(self.symbols.document()), n, None),
NodeKind::Path { .. } | NodeKind::Compound { .. } if reshapes(n) => path_art(n),
NodeKind::SymbolInstance { symbol, .. } => {
if geometry_effects(n).is_empty() && !self.symbol_draws(symbol) {
return None;
}
if !counted {
let left = self.left.get().checked_sub(1)?;
self.left.set(left);
}
let art = self.symbols.measure_art(symbol, Arc::clone)?;
// Paint overrides can remove a projected surface or change its stroke outline.
let art =
if n.extras().symbol_overrides.is_empty() { art } else { Arc::new(vectorcraft_doc::symbol_overrides::dynamic_art(&art, n)) };
// An instance in perspective is projected by `reshape`.
let doc = Some(self.symbols.document());
reshape(doc, n, Some(&*art)).or_else(|| outline_art(doc, n, Some(&*art)))
}
_ if reshapes(n) => reshape(Some(self.symbols.document()), n, None),
NodeKind::Blend { .. } | NodeKind::Envelope { .. } | NodeKind::Repeat(_) if inner() => {
Some(expanded_live_group(Some(self.symbols.document()), n))
}
_ => None,
}
}
/// `n`, a group, layer or intertwine without a clip, with each member that has a
/// [`Self::drawn_art`] replaced by it. `None` for any other object and when no member has one.
fn with_drawn_members(&self, n: &Node) -> Option<Node> {
if n.clips() || !matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. } | NodeKind::Intertwine { .. }) {
return None;
}
let drawn: Vec<(usize, Node)> = n.children()?.iter().enumerate().filter_map(|(i, c)| Some((i, self.drawn_art(c)?))).collect();
if drawn.is_empty() {
return None;
}
let mut m = n.clone();
let children = m.children_mut()?;
for (i, c) in drawn {
if let Some(slot) = children.get_mut(i) {
*slot = Arc::new(c);
}
}
Some(m)
}
/// Clip group or layer `n` as its clipping path's strokes draw it: a group of the clipping path
/// without its strokes, measured as the region it clips to, and its strokes ([`Node::clip_paint`])
/// as their [`Self::drawn_art`]. The renderer clips by the clipping path's geometry before its
/// geometry effects ([`crate::clip_outline`]) and paints the path's fills inside that region and
/// its strokes over the clipped art, unclipped. `None` for any other object, when the clipping
/// path is hidden and when its strokes have no drawn art.
fn clip_art(&self, n: &Node) -> Option<Node> {
if !n.clips() {
return None;
}
let clip = n.children()?.first().filter(|c| c.visible)?;
let strokes = self.drawn_art(&clip.clip_paint().stroke?)?;
Some(Node::group(n.id, vec![Arc::new(unstroked(clip)), Arc::new(strokes)]))
}
}
/// Clipping path `clip` without its strokes or its characters' strokes.
fn unstroked(clip: &Node) -> Node {
let mut n = clip.clone();
n.appearance.items.retain(AppearanceItem::is_fill);
if let NodeKind::Text(t) = &mut n.kind {
for r in &mut t.runs {
r.style.stroke = Paint::None;
}
}
n
}
/// Whether `n` itself has a geometry effect that [`Drawn::own_art`] applies, its own or one of its
/// fill's or stroke's (a group or layer: also a Pathfinder effect; a symbol instance: only its own).
fn reshapes(n: &Node) -> bool {
match &n.kind {
NodeKind::Group { .. } | NodeKind::Layer { .. } => {
has_pathfinder(n) || !geometry_effects(n).is_empty() || n.appearance.items.iter().any(|i| paints(i) && has_geometry(i.effects()))
}
NodeKind::Path { guide: false, .. } | NodeKind::Compound { .. } => {
has_geometry(&n.appearance.effects) || n.appearance.items.iter().any(|i| paints(i) && has_geometry(i.effects()))
}
_ => needs_outline(n) && (!geometry_effects(n).is_empty() || has_item_geometry(n)),
}
}
/// Path or compound path `n` as a group of paths: its geometry after its own geometry effects
/// painted by its fills and strokes without geometry effects, and that geometry after the
/// geometry effects of each other fill or stroke that paints, painted by that fill or stroke, as
/// the renderer draws them. The first path is left out when only fills or strokes with geometry
/// effects paint. The paths keep `n`'s id and fill rule and carry no effects.
fn path_art(n: &Node) -> Option<Node> {
let (path, rule) = leaf_geometry(n)?;
let ctx = GeomContext::of(n);
let g = apply_geometry_with(&n.appearance.effects, &path, path.to_bezpath().bounding_box(), &ctx);
let piece = |geometry: PathData, items: Vec<AppearanceItem>| {
let mut ap: Appearance = (*n.appearance).clone();
ap.items = items;
ap.effects.clear();
for i in &mut ap.items {
i.effects_mut().clear();
}
let mut p = Node::path(n.id, geometry, ap);
if let NodeKind::Path { rule: r, .. } = &mut p.kind {
*r = rule;
}
Arc::new(p)
};
let (own, plain): (Vec<&AppearanceItem>, Vec<&AppearanceItem>) = n.appearance.items.iter().partition(|i| paints(i) && has_geometry(i.effects()));
let mut pieces = vec![];
if own.is_empty() || plain.iter().any(|i| paints(i)) {
pieces.push(piece(g.clone(), plain.into_iter().cloned().collect()));
}
let bounds = g.to_bezpath().bounding_box();
for item in own {
let geometry = apply_geometry_with(item.effects(), &g, bounds, &ctx.item(item));
pieces.push(piece(geometry, vec![item.clone()]));
}
Some(Node::group(n.id, pieces))
}