Files
vectorcraft/crates/doc/src/text/wrap.rs
T
dotCooCoo c683302d4e Text Wrap: Make's no-type warning checks the wrap objects without copying their shapes
Make Text Wrap called wrap_sources to learn whether a new wrap object
sits above area type, which copied every wrap shape of every area text
object in the document into text space and threw the copies away.
wrap_source_objects gives the same text objects and wrap objects without
making those copies, and Make uses it.
2026-12-01 09:00:09 -08:00

239 lines
9.0 KiB
Rust

//! Object → Text Wrap: area type flows around the wrap objects stacked above it in the same layer.
use std::sync::Arc;
use vectorcraft_geom::{PathData, shapes};
use super::{TextKind, TextObject, WrapShape};
use crate::{Document, Node, NodeId, NodeKind};
#[cfg(test)]
thread_local! {
/// How many wrap outlines [`each_text`] has made on this thread (for tests).
pub(super) static OUTLINES_MADE: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
/// How many wrap shapes [`shapes_of`] has copied into text space on this thread (for tests).
pub(super) static SHAPES_COPIED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// Outline of a wrap object in document coordinates: its paths, or its bounds for other art.
fn outline(n: &Node, out: &mut PathData) {
match &n.kind {
NodeKind::Path { path, guide: false, .. } => {
out.subpaths.extend(path.subpaths.iter().filter(|sp| sp.closed || sp.anchors.len() > 2).cloned())
}
NodeKind::Compound { children, .. } | NodeKind::Group { children, clip: false } => {
for c in children.iter().filter(|c| c.visible) {
outline(c, out);
}
}
// A clip group wraps by its clipping path.
NodeKind::Group { children, clip: true } => {
if let Some(c) = children.first() {
outline(c, out);
}
}
_ => {
if let Some(b) = n.geometric_bounds() {
out.subpaths.extend(shapes::rectangle(b).subpaths);
}
}
}
}
/// The outline area type wraps around for wrap object `n`, in document coordinates, as
/// [`resolve_wrap`] makes it: a path's subpaths that are closed or have more than two anchors, those
/// of the shown members of a group or compound path, a clip group's clipping object's, and the
/// bounds of other art. Empty when `n` has none.
pub fn wrap_outline(n: &Node) -> PathData {
let mut pd = PathData::default();
outline(n, &mut pd);
pd
}
enum Item<'a> {
Wrap(&'a Node),
Text(&'a Node),
}
fn collect<'a>(n: &'a Node, out: &mut Vec<Item<'a>>) {
if !n.visible {
return;
}
if n.extras().wrap.is_some() && !matches!(n.kind, NodeKind::Layer { .. }) {
out.push(Item::Wrap(n));
return;
}
match &n.kind {
NodeKind::Text(t) if matches!(t.kind, TextKind::Area { .. }) => out.push(Item::Text(n)),
NodeKind::Layer { children, .. } | NodeKind::Group { children, .. } => {
for c in children {
collect(c, out);
}
}
_ => {}
}
}
/// The anchors and subpaths [`outline`] would collect for wrap object `n`, counted without copying
/// them.
fn outline_size(n: &Node) -> (usize, usize) {
match &n.kind {
NodeKind::Path { path, guide: false, .. } => path
.subpaths
.iter()
.filter(|sp| sp.closed || sp.anchors.len() > 2)
.fold((0usize, 0usize), |(a, s), sp| (a.saturating_add(sp.anchors.len()), s.saturating_add(1))),
NodeKind::Compound { children, .. } | NodeKind::Group { children, clip: false } => children
.iter()
.filter(|c| c.visible)
.map(|c| outline_size(c))
.fold((0, 0), |(a, s), (ca, cs)| (a.saturating_add(ca), s.saturating_add(cs))),
NodeKind::Group { children, clip: true } => children.first().map_or((0, 0), |c| outline_size(c)),
// A rectangle of the object's bounds.
_ => {
if n.geometric_bounds().is_some() {
(4, 1)
} else {
(0, 0)
}
}
}
}
/// How much [`resolve_wrap`] would copy into the wrap shapes of every area text object in `doc`:
/// each wrap object's outline (its anchors, plus one for the shape) once for each area text object
/// below it in its layer.
pub fn wrap_work(doc: &Document) -> usize {
work(doc, false)
}
/// [`wrap_work`], with one more for each subpath of each outline, empty ones included.
pub fn wrap_copy_units(doc: &Document) -> usize {
work(doc, true)
}
fn work(doc: &Document, count_subpaths: bool) -> usize {
let mut total = 0usize;
for layer in &doc.layers {
let mut items = vec![];
collect(layer, &mut items);
let mut above = 0usize;
for it in items.iter().rev() {
match it {
Item::Wrap(n) => {
let (anchors, subpaths) = outline_size(n);
if anchors > 0 {
let subpaths = if count_subpaths { subpaths } else { 0 };
above = above.saturating_add(anchors).saturating_add(subpaths).saturating_add(1);
}
}
Item::Text(_) => total = total.saturating_add(above),
}
}
}
total
}
/// Each area text object of `layer`, from the top down, with the wrap objects above it in the
/// layer whose outlines aren't empty, topmost first, each with the shape its outline makes in
/// document coordinates. Each wrap object's outline is made once.
fn each_text<'a>(layer: &'a Node, mut f: impl FnMut(&'a Node, &[(&'a Node, WrapShape)])) {
let mut items = vec![];
collect(layer, &mut items);
let mut above: Vec<(&Node, WrapShape)> = vec![];
// A wrap object below every area text object of the layer wraps none of them: its outline is
// not made.
let first_text = items.iter().position(|it| matches!(it, Item::Text(_)));
for (i, it) in items.iter().enumerate().rev() {
match it {
Item::Wrap(_) if first_text.is_none_or(|t| i < t) => {}
Item::Wrap(n) => {
#[cfg(test)]
OUTLINES_MADE.with(|c| c.set(c.get() + 1));
let mut pd = PathData::default();
outline(n, &mut pd);
if !pd.is_empty() {
above.push((n, WrapShape { path: pd, wrap: n.extras().wrap.unwrap_or_default() }));
}
}
Item::Text(n) => f(n, &above),
}
}
}
/// The wrap shapes of text object `t` (text space) from `above`, the wrap objects above it as
/// [`each_text`] gives them: bottom first.
fn shapes_of(t: &TextObject, above: &[(&Node, WrapShape)]) -> Vec<WrapShape> {
let inv = t.xf.inverse();
above
.iter()
.rev()
.map(|(_, s)| {
#[cfg(test)]
SHAPES_COPIED.with(|c| c.set(c.get() + 1));
let mut path = s.path.clone();
path.transform(inv);
WrapShape { path, wrap: s.wrap }
})
.collect()
}
/// Each area text object on `doc`'s layers that [`resolve_wrap`] gives wrap shapes, with the wrap
/// objects whose outlines make them, bottom first, and those shapes (text space), as
/// [`resolve_wrap`] works them out. The copying is [`wrap_work`].
pub fn wrap_sources<'a>(doc: &'a Document, mut f: impl FnMut(&'a Node, Vec<&'a Node>, Vec<WrapShape>)) {
for layer in &doc.layers {
each_text(layer, |n, above| {
if let NodeKind::Text(t) = &n.kind
&& !above.is_empty()
{
f(n, above.iter().rev().map(|(s, _)| *s).collect(), shapes_of(t, above));
}
});
}
}
/// [`wrap_sources`] without the shapes: each area text object that gets wrap shapes, with the wrap
/// objects whose outlines make them, bottom first. No shape is copied into text space.
pub fn wrap_source_objects<'a>(doc: &'a Document, mut f: impl FnMut(&'a Node, Vec<&'a Node>)) {
for layer in &doc.layers {
each_text(layer, |n, above| {
if matches!(n.kind, NodeKind::Text(_)) && !above.is_empty() {
f(n, above.iter().rev().map(|(s, _)| *s).collect());
}
});
}
}
/// Resolve the wrap shapes (`TextObject::wrap`, text space) of each area text object from the
/// outlines of the wrap objects above it in its layer. With `before`, the layers it holds unchanged
/// are skipped; without it, every layer is resolved. Returns the area text objects whose shapes
/// changed, whose cached bounds are then out of date.
pub fn resolve_wrap(before: Option<&Document>, doc: &mut Document) -> Vec<NodeId> {
let mut updates: Vec<(NodeId, Vec<WrapShape>)> = vec![];
for (idx, layer) in doc.layers.iter().enumerate() {
if let Some(before) = before
&& (before.layers.get(idx).is_some_and(|b| Arc::ptr_eq(b, layer))
|| before.layers.iter().any(|b| b.id == layer.id && Arc::ptr_eq(b, layer)))
{
continue;
}
each_text(layer, |n, above| {
if let NodeKind::Text(t) = &n.kind {
let shapes = shapes_of(t, above);
if shapes != t.wrap {
updates.push((n.id, shapes));
}
}
});
}
let mut changed = Vec::with_capacity(updates.len());
for (id, shapes) in updates {
if let Some(NodeKind::Text(t)) = doc.node_mut(id).map(|n| &mut n.kind) {
t.wrap = shapes;
changed.push(id);
}
}
changed
}