diff --git a/crates/doc/src/text.rs b/crates/doc/src/text.rs index 073a0f2efd..7ba5cb6199 100644 --- a/crates/doc/src/text.rs +++ b/crates/doc/src/text.rs @@ -44,7 +44,7 @@ pub use tabs_lists::{ Bullet, DEFAULT_TAB_INTERVAL, LIST_HANGING_INDENT, LIST_LEVEL_INDENT, LIST_LEVELS, ListAlign, ListCounts, MAX_LIST_START, NumberStyle, ParaList, TabAlign, TabStop, }; -pub use wrap::{resolve_wrap, wrap_copy_units, wrap_outline, wrap_sources, wrap_work}; +pub use wrap::{resolve_wrap, wrap_copy_units, wrap_outline, wrap_source_objects, wrap_sources, wrap_work}; /// A text object. `runs` split into paragraphs at `\n`. /// diff --git a/crates/doc/src/text/tests.rs b/crates/doc/src/text/tests.rs index 9371947565..8a17fd880b 100644 --- a/crates/doc/src/text/tests.rs +++ b/crates/doc/src/text/tests.rs @@ -695,6 +695,15 @@ fn a_wrap_object_below_every_area_type_object_makes_no_outline() { }); assert_eq!(sources, 1); assert_eq!(wrap::OUTLINES_MADE.with(|c| c.get()), 1, "only the wrap object above the type is outlined"); + // The objects alone come without a copy of any shape into text space. + let mut found = vec![]; + wrap_source_objects(&d, |n, objects| found.push((n.id, objects.iter().map(|o| o.id).collect::>()))); + let mut expected = vec![]; + wrap_sources(&d, |n, objects, _| expected.push((n.id, objects.iter().map(|o| o.id).collect::>()))); + assert_eq!(found, expected); + wrap::SHAPES_COPIED.with(|c| c.set(0)); + wrap_source_objects(&d, |_, _| {}); + assert_eq!(wrap::SHAPES_COPIED.with(|c| c.get()), 0, "no wrap shape is copied"); } #[test] diff --git a/crates/doc/src/text/wrap.rs b/crates/doc/src/text/wrap.rs index 33fdcb4687..2061de7092 100644 --- a/crates/doc/src/text/wrap.rs +++ b/crates/doc/src/text/wrap.rs @@ -11,6 +11,8 @@ use crate::{Document, Node, NodeId, NodeKind}; thread_local! { /// How many wrap outlines [`each_text`] has made on this thread (for tests). pub(super) static OUTLINES_MADE: std::cell::Cell = 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 = const { std::cell::Cell::new(0) }; } /// Outline of a wrap object in document coordinates: its paths, or its bounds for other art. @@ -167,6 +169,8 @@ fn shapes_of(t: &TextObject, above: &[(&Node, WrapShape)]) -> Vec { .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 } @@ -189,6 +193,18 @@ pub fn wrap_sources<'a>(doc: &'a Document, mut f: impl FnMut(&'a Node, Vec<&'a N } } +/// [`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 diff --git a/crates/engine/src/cmd/textwrap.rs b/crates/engine/src/cmd/textwrap.rs index ade875b4c9..6daa8cdd26 100644 --- a/crates/engine/src/cmd/textwrap.rs +++ b/crates/engine/src/cmd/textwrap.rs @@ -115,7 +115,7 @@ fn make(s: &mut Session, p: &Value) -> Result { } let mut out = json!({ "ids": objects.iter().map(|i| i.0).collect::>() }); let mut wraps = false; - vectorcraft_doc::text::wrap_sources(&s.doc()?.doc, |_, sources, _| wraps |= sources.iter().any(|n| objects.contains(&n.id))); + vectorcraft_doc::text::wrap_source_objects(&s.doc()?.doc, |_, sources| wraps |= sources.iter().any(|n| objects.contains(&n.id))); if !wraps { out["warning"] = json!(NO_AREA_TYPE_BELOW); }