diff --git a/crates/doc/src/text.rs b/crates/doc/src/text.rs index 9de39458b0..6863960230 100644 --- a/crates/doc/src/text.rs +++ b/crates/doc/src/text.rs @@ -35,7 +35,7 @@ pub use tabs_lists::{ Bullet, DEFAULT_TAB_INTERVAL, LIST_HANGING_INDENT, LIST_LEVEL_INDENT, LIST_LEVELS, ListAlign, ListCounts, NumberStyle, ParaList, TabAlign, TabStop, }; -pub use wrap::{resolve_wrap, wrap_sources, wrap_work}; +pub use wrap::{resolve_wrap, wrap_outline, wrap_sources, wrap_work}; /// A text object. `runs` split into paragraphs at `\n`. /// diff --git a/crates/doc/src/text/wrap.rs b/crates/doc/src/text/wrap.rs index 29313ccb22..15a0fbbd40 100644 --- a/crates/doc/src/text/wrap.rs +++ b/crates/doc/src/text/wrap.rs @@ -32,6 +32,16 @@ fn outline(n: &Node, out: &mut PathData) { } } +/// 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), diff --git a/crates/eps/src/ai_editing/tests.rs b/crates/eps/src/ai_editing/tests.rs index 598e66bc3d..ffa6397805 100644 --- a/crates/eps/src/ai_editing/tests.rs +++ b/crates/eps/src/ai_editing/tests.rs @@ -12338,8 +12338,8 @@ fn an_envelope_of_area_type_in_written_art_leaves_the_wrap_on_the_bounds() { } /// A clipping path inside art cut to a clipping object is written as its fills and its strokes, -/// two objects: its text wrap options are written once, whether it has both or only strokes, and -/// with its strokes when the region outside leaves nothing of its fills. +/// two objects: its text wrap options are written once, whether it has both, only strokes, or +/// fills the region outside leaves nothing of. #[test] fn a_clipping_path_written_as_its_paint_keeps_one_text_wrap() { for (fill, left, width) in [(solid(Color::gray(0.5)), 20.0, 2.0), (Paint::None, 20.0, 2.0), (solid(Color::gray(0.5)), 101.0, 4.0)] { @@ -12450,3 +12450,151 @@ fn a_graph_with_art_left_out_is_written_without_records() { assert!(!text.contains("%_Gs") && !text.contains(" Go\n") && !text.contains("%_GS"), "{max}: {text}"); } } + +/// The wrap shapes [`vectorcraft_doc::text::resolve_wrap`] gives the area type in `doc`. +fn area_wrap(doc: &Document) -> Vec { + let mut d = doc.clone(); + vectorcraft_doc::text::resolve_wrap(None, &mut d); + let mut out = vec![]; + d.layers[0].walk(&mut |n| { + if let NodeKind::Text(t) = &n.kind + && matches!(t.kind, vectorcraft_doc::TextKind::Area { .. }) + { + out.clone_from(&t.wrap); + } + }); + out +} + +/// `got` and `want` are the same wrap shapes: the same options, offsets within `1e-6`, and the +/// same subpaths with anchors within `1e-3`. +fn same_shapes(got: &[vectorcraft_doc::WrapShape], want: &[vectorcraft_doc::WrapShape], case: &str) { + let same = got.len() == want.len() + && got.iter().zip(want).all(|(g, w)| { + g.wrap.invert == w.wrap.invert + && (g.wrap.offset - w.wrap.offset).abs() < 1e-6 + && g.path.subpaths.len() == w.path.subpaths.len() + && g.path.subpaths.iter().zip(&w.path.subpaths).all(|(a, b)| { + a.closed == b.closed + && a.anchors.len() == b.anchors.len() + && a.anchors.iter().zip(&b.anchors).all(|(p, q)| (p.p - q.p).hypot() < 1e-3) + }) + }); + assert!(same, "{case}\n{got:?}\n{want:?}"); +} + +/// A clipping object that isn't written as an object of its own (a group, an image, a symbol +/// instance, a path or compound path without paint in art cut to a clipping object) or is written +/// as its outlines or cut (type, a path the outer clipping object cuts) keeps its text wrap options on one unpainted object +/// of its wrap outline: area type below it wraps alike after a round trip, and the object stays +/// locked or hidden as it was. A clipping object without options gets no such object. +#[test] +fn a_clipping_object_not_written_whole_keeps_its_text_wrap() { + use vectorcraft_doc::{TextKind, TextObject, TextRun}; + let cases = ["group", "hidden", "locked", "image", "instance", "type", "unpainted path", "unpainted compound", "cut path", "no options"]; + for case in cases { + let mut doc = Document::new(400.0, 200.0); + let layer = doc.layers[0].id; + let st = style(10.0, Color::BLACK); + let mut area = TextObject::point(vectorcraft_geom::Point::ZERO, "", st.clone()); + area.runs = vec![TextRun::new("One two three four five six", st)]; + area.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, 400.0, 200.0)) }; + area.xf = Affine::IDENTITY; + let area = text_node(&mut doc, area); + doc.insert(Some(layer), usize::MAX, area).unwrap(); + let art = square(&mut doc, Rect::new(0.0, 0.0, 10.0, 10.0), LEAF); + doc.symbols.push(Symbol { dynamic: false, name: "Leaf".into(), art: Arc::new(art) }); + let mut cutter = match case { + "group" | "hidden" | "locked" | "no options" => { + let mut off = square(&mut doc, Rect::new(200.0, 20.0, 230.0, 60.0), Color::gray(0.5)); + off.visible = false; + let members = vec![ + square(&mut doc, Rect::new(20.0, 20.0, 50.0, 60.0), Color::gray(0.5)), + square(&mut doc, Rect::new(60.0, 30.0, 90.0, 70.0), Color::gray(0.5)), + off, + ]; + group(&mut doc, members, false) + } + "image" => image_node(&mut doc, ImageBlob::png(rgb_png()), (3, 2), Affine::new([20.0, 0.0, 0.0, 20.0, 30.0, 40.0]), None), + "instance" => instance(&mut doc, "Leaf", Affine::translate((40.0, 30.0))), + "type" => { + let label = TextObject::point(vectorcraft_geom::Point::new(30.0, 50.0), "Clip", style(24.0, Color::BLACK)); + text_node(&mut doc, label) + } + "unpainted path" => clipping(&mut doc, shapes::rectangle(Rect::new(20.0, 20.0, 80.0, 80.0))), + "unpainted compound" => { + let outer = Node::path(doc.alloc_id(), shapes::rectangle(Rect::new(20.0, 20.0, 80.0, 80.0)), Appearance::default()); + let inner = Node::path(doc.alloc_id(), shapes::rectangle(Rect::new(30.0, 30.0, 70.0, 70.0)), Appearance::default()); + Node::new(doc.alloc_id(), NodeKind::Compound { children: vec![Arc::new(outer), Arc::new(inner)], rule: FillRule::EvenOdd }) + } + _ => { + let mut c = clipping(&mut doc, shapes::rectangle(Rect::new(50.0, 20.0, 150.0, 80.0))); + c.appearance = Appearance::basic(solid(Color::gray(0.5)), Paint::None, 0.0).into(); + c + } + }; + cutter.visible = case != "hidden"; + cutter.locked = case == "locked"; + let cutter = if case == "no options" { cutter } else { wrapped(cutter, -4.5, true) }; + let member = square(&mut doc, Rect::new(30.0, 30.0, 70.0, 50.0), Color::rgb(1.0, 0.0, 0.0)); + let mut top = group(&mut doc, vec![cutter, member], true); + if matches!(case, "unpainted path" | "unpainted compound" | "cut path") { + let frame = square(&mut doc, Rect::new(0.0, 0.0, 100.0, 100.0), Color::gray(0.9)); + let frame = group(&mut doc, vec![frame], false); + top = group(&mut doc, vec![frame, top], true); + } + doc.insert(Some(layer), usize::MAX, top).unwrap(); + let text = program(&doc); + let back = read_back(&doc); + if case == "no options" { + assert!(!text.contains("(AI11TextWrapOffset)"), "{text}"); + assert!(wraps(back.layers[0].children().unwrap()).is_empty()); + continue; + } + assert_eq!(text.matches("(AI11TextWrapOffset)").count(), 1, "{case}\n{text}"); + same_wraps(&wraps(back.layers[0].children().unwrap()), &[(None, -4.5, true)]); + let mut carrier = None; + back.layers[0].walk(&mut |n| { + if n.extras().wrap.is_some() { + carrier = Some((n.locked, n.visible)); + } + }); + assert_eq!(carrier, Some((case == "locked", case != "hidden")), "{case}"); + let want = area_wrap(&doc); + assert_eq!(want.is_empty(), case == "hidden", "{case}"); + same_shapes(&area_wrap(&back), &want, case); + } +} + +/// With too little room for the unpainted object that carries a clipping object's text wrap +/// options, the options are left out, never written on another object, and the data stays readable. +#[test] +fn a_clipping_objects_text_wrap_is_left_out_without_room() { + let mut doc = Document::new(200.0, 100.0); + let layer = doc.layers[0].id; + let members = vec![ + square(&mut doc, Rect::new(20.0, 20.0, 50.0, 60.0), Color::gray(0.5)), + square(&mut doc, Rect::new(60.0, 30.0, 90.0, 70.0), Color::gray(0.5)), + ]; + let cutter = wrapped(group(&mut doc, members, false), 2.0, false); + let member = square(&mut doc, Rect::new(30.0, 30.0, 70.0, 50.0), Color::rgb(1.0, 0.0, 0.0)); + let top = group(&mut doc, vec![cutter, member], true); + doc.insert(Some(layer), usize::MAX, top).unwrap(); + let all = crate::import::objects_read(&export_ai_editing(&doc, &opts()).unwrap().program).unwrap(); + for room in 0..=all { + let out = export_within(&doc, &opts(), room).unwrap(); + assert!(crate::import::objects_read(&out.program).unwrap() <= room); + let text = String::from_utf8_lossy(&out.program).replace("\r\n", "\n"); + // The group, then the compound path of the two squares (three objects): with room for all four + // the options are written, on that compound path. + assert_eq!(text.matches("(AI11TextWrapOffset)").count(), usize::from(room >= 4), "{room}\n{text}"); + let (back, _) = crate::ai_alone(&out.program).unwrap(); + let mut carriers = vec![]; + back.layers[0].walk(&mut |n| { + if n.extras().wrap.is_some() { + carriers.push(matches!(n.kind, NodeKind::Compound { .. }) && n.appearance.items.is_empty()); + } + }); + assert_eq!(carriers, if room >= 4 { vec![true] } else { vec![] }, "{room}\n{text}"); + } +} diff --git a/crates/eps/src/ai_editing/writer.rs b/crates/eps/src/ai_editing/writer.rs index 7c211add4d..e32118fe5f 100644 --- a/crates/eps/src/ai_editing/writer.rs +++ b/crates/eps/src/ai_editing/writer.rs @@ -1232,25 +1232,22 @@ impl<'a> Writer<'a> { /// A container's members, the first one's outline cutting the others: its fills painted /// behind them (cut to the region outside too), its strokes over them, not cut by it. Its text - /// wrap options are written once: with its fills, or with its strokes when no fill object is - /// written (it has no fills, or the region outside leaves nothing of them). + /// wrap options are written once, under them all ([`Self::wrap_carrier`]), and not with its fills + /// or strokes, which may be cut or left out. fn clip_contents(&mut self, children: &[Arc]) { let Some((first, rest)) = children.split_first() else { return }; + self.wrap_carrier(first); // Nothing to clip by hides the clipped art. let Some((outline, rule)) = vectorcraft_effects::clip_outline(first) else { return }; let mut paint = first.clip_paint(); + for part in [&mut paint.fill, &mut paint.stroke].into_iter().flatten() { + part.extras_mut().wrap = None; + } let outer = self.clip.clone(); let region = self.cut(&PathData::from_bezpath(&outline), rule); self.clip = Some(region); if let Some(fill) = &paint.fill { - let objects = self.objects; self.node(fill); - if self.objects > objects - && let Some(stroke) = &mut paint.stroke - && stroke.extras().wrap.is_some() - { - stroke.extras_mut().wrap = None; - } } for c in rest { self.node(c); @@ -1261,6 +1258,28 @@ impl<'a> Writer<'a> { } } + /// The text wrap options of clipping object `first`, when it has them, on an unpainted path + /// (a compound path for several subpaths) of the outline area type wraps around + /// ([`vectorcraft_doc::text::wrap_outline`]), uncut, locked and hidden as `first` is. Nothing is + /// written for an empty outline or without room for the path ([`Self::take_objects`]). + fn wrap_carrier(&mut self, first: &Node) { + let Some(wrap) = first.extras().wrap.filter(|_| self.drawn == 0 && !self.in_look) else { return }; + let outline = vectorcraft_doc::text::wrap_outline(first); + if outline.is_empty() { + return; + } + self.state(first.locked, !first.visible); + let start = self.body.len(); + self.plain_object(&outline, FillRule::NonZero, None, None, Xy::of(first).object()); + if self.body.len() == start { + return; + } + self.block_end = usize::MAX; + self.body.push_str("%_/ArtDictionary :\n"); + self.text_wrap(wrap); + self.body.push_str("%_;\n%_\n"); + } + /// `path` (filled by `rule`) cut to the region the art is cut to, as a non-zero path. fn cut(&self, path: &PathData, rule: FillRule) -> PathData { match &self.clip { diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 58496c0477..62e1f51a9f 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1839,6 +1839,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur A PDF-compatible `.ai` saved with Include Editing Data for Other Apps writes point type, area type and type on a path as type in its editing copy, and objects next to it keep their own transparency (`crates/eps/src/ai_editing/tests.rs`, `cmd::fileio::tests_pdfedit::type_comes_back_from_the_editing_copy`; [file-format-parity.md](file-format-parity.md) lists what is written as outlines). A PDF-compatible `.ai` saved with Include Editing Data for Other Apps keeps the document's variables, data sets and the bindings of objects to variables in its editing copy, in the form the variable library reader above reads, so they come back when the file is opened. Bindings to layers, to type written as outlines, to graphs and to linked images written as other objects, data set values of graphs, and variables, data sets, bindings and values past the writer's limits are left out with a warning (#6024; `crates/eps/src/ai_editing/tests.rs` `variables_data_sets_and_bindings_are_read_back`; `crates/eps/src/ai_editing/writer/variables.rs` lists what is left out). A PDF-compatible `.ai` saved with Include Editing Data for Other Apps keeps each wrap object's Text Wrap Options in its editing copy: the Offset, positive or negative, and Invert Wrap ([the help page](https://helpx.adobe.com/illustrator/desktop/design-with-text/add-manage-text/wrap-or-unwrap-text-around-objects.html)), in the form the `.ai` reader reads them (`text_wrap` in `crates/eps/src/import/ai/read/document.rs`), so they come back when the file is opened. An offset past the -1000 to 1000 pt the reader keeps is written as the nearer end of that range. On a large canvas the offset is written at the art's scale and comes back at its size. Art the writer draws for an object, such as a blend's steps or a symbol instance written as its art, has only that object's options, and a clipping path written as its fills and its strokes has its options once (`crates/eps/src/ai_editing/tests.rs` `text_wrap_options_are_written_and_read_back`, `drawn_art_keeps_only_its_objects_text_wrap`, `text_wrap_offsets_on_a_large_canvas_come_back_at_their_size`, `text_wrap_options_of_type_and_envelopes_are_read_back`, `a_clipping_path_written_as_its_paint_keeps_one_text_wrap`). + A clipping object whose clipped art is written cut to it (a group, an image, a symbol instance, type, or a path or compound path inside art cut to another clipping object) keeps its Text Wrap Options in the editing copy on an unpainted path, or compound path, of the outline area type wraps around, under the art it clips and locked or hidden as the object is. The `.ai` reader reads an unpainted path (`paint` in `crates/eps/src/import/ai/read/art.rs`) and gives it the options of the dictionary after it (`text_wrap` in `crates/eps/src/import/ai/read/document.rs`), so area type below the object wraps the same way when the file is opened. Without room for that path under the object limit, the options are left out (`crates/eps/src/ai_editing/tests.rs` `a_clipping_object_not_written_whole_keeps_its_text_wrap`, `a_clipping_objects_text_wrap_is_left_out_without_room`). Area type wrapped only by plain saved paths, compound paths or groups remains editable in the `.ai` editing copy when each frame's wrap recomputes unchanged; other wrapped area type remains outlines. The `.ai` reader works the wrap out again from the objects above the type (`resolve_text_wrap` in `crates/eps/src/import/native/build.rs`), so the writer keeps type only where each wrap object is written as itself (one path with no paint, a solid fill or a plain solid stroke, a compound path of visible members, or a group of such objects, without effects, an opacity mask or clipping), its offset is within -1000 to 1000 pt, and the shapes of every area type in the file stay within the 2,000,000 anchors the reader copies; when art is left out at the object limit, such type is written as outlines. This is VectorCraft's own round-trip rule (`crates/eps/src/ai_editing/tests.rs` `area_type_wrapped_by_plain_objects_stays_type`, `area_type_wrapped_by_other_objects_stays_outlines`, `threaded_area_type_wrapped_by_plain_objects_stays_type`, `area_type_wrapped_on_a_large_canvas_stays_type`, `area_type_wrapped_past_the_reader_limit_stays_outlines`, `area_type_wrapped_past_the_object_limit_is_written_as_outlines`). Type written as its outlines in the editing copy still wraps area type by its bounds, not by its glyphs, when the file is opened: the outlines' group ends with an unpainted rectangle at the type's bounds that carries its Text Wrap Options, in the form the `.ai` reader reads a path's options (`text_wrap` in `crates/eps/src/import/ai/read/document.rs`), and the group keeps the type's name (`crates/eps/src/ai_editing/tests.rs` `type_written_as_outlines_keeps_wrapping_by_its_bounds`). A symbol instance written as its art, and a blend, mesh, repeat, placed document, intertwine or envelope written as a group of its art, likewise still wraps area type by its bounds when the file is opened, as it did before saving, not by the members of that art: the group starts with the same unpainted rectangle, which takes the object's lock and visibility, and the group keeps the object's name and transparency without options of its own. Area type in that art is above the rectangle, so the rectangle does not wrap it. The rectangle takes its place in the object limit before the art; without room for it, the options are left out with the limit's warning. An object without finite bounds keeps the options on its group. This is VectorCraft's own round-trip rule (`crates/eps/src/ai_editing/tests.rs` `live_art_written_as_a_group_keeps_wrapping_by_its_bounds`, `wrap_bounds_of_live_art_fit_the_object_limit_and_leave_area_type_in_the_art_unwrapped`, `live_art_without_bounds_keeps_its_text_wrap_on_the_group`).