From bf3d27e2b91f358c47088095e34c36cf02b7626d Mon Sep 17 00:00:00 2001 From: Juan Manuel Date: Thu, 19 Nov 2026 06:53:38 +0100 Subject: [PATCH] .ai import: blank area type keeps its frame, blank point type is left out quietly (#8348) * .ai import: blank area type keeps its frame, blank point type quiet A text object whose story is blank (nothing but spaces and line ends) was never made (`Story::node`), so its slot was left empty and counted in the note "text objects on hidden layers or hidden objects couldn't be read", shown or not. A slot of a blank story that neither the text document nor the page fills is now settled after the page is matched, so the page's type still takes it where the page has some for it alone: - Blank area type and type on a path come in as a text object that keeps its frame or path and draws nothing (`Story::blank_frame_node`), as VectorCraft's Type tool keeps an empty frame (#85, #154). That covers a frame the page's spare type went past (`Merged`) too. It is kept without being weighed against the page, and so are the later frames of its thread. - Blank point type draws nothing and has nothing to keep, so it is left out without a note, as the Type tool discards empty point type. - A blank slot takes the page's type left over only when no other open slot can (`readable_first`), so it no longer takes another object's type by painting order. - A blank story's thread isn't held against an unread object that takes one of its frames, and a thread lists only the frames still there. Its frames are still threaded type to warps and envelopes, which never take the place of such a frame. The note for text objects still left out no longer says they are hidden: "text objects whose type couldn't be read from the file's text document or its page are left out". Co-Authored-By: Claude Opus 5.5 * .ai import: a blank frame an unread object leaves alone isn't threaded type When an unread object takes one frame of a blank story's two-frame thread with it, the other frame is no longer threaded in the document, but warps and envelopes still treated it as threaded type and so never weighed it against its look. They now get the frames of the threads actually kept. Co-Authored-By: Claude Opus 5.5 * Test: the lone blank frame in a warp comes in as its look, no text left Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/eps/src/import/ate.rs | 28 ++- crates/eps/src/import/native/build.rs | 113 +++++++++-- crates/eps/src/import/native/plan.rs | 70 +++++-- crates/eps/src/import/native/slots.rs | 5 +- crates/eps/src/import/tests_native.rs | 270 +++++++++++++++++++++++++- 5 files changed, 453 insertions(+), 33 deletions(-) diff --git a/crates/eps/src/import/ate.rs b/crates/eps/src/import/ate.rs index 96a408ae7d..ef765c5909 100644 --- a/crates/eps/src/import/ate.rs +++ b/crates/eps/src/import/ate.rs @@ -1189,6 +1189,12 @@ impl Story { self.text.len() } + /// Is frame `frame` of the story point type with nothing but spaces and line ends (its last + /// `\r` left out), which [`Self::node`] doesn't make: it draws nothing. + pub(super) fn blank_point(&self, frame: usize) -> bool { + frame == 0 && matches!(self.frames.first(), Some(FrameKind::Point(_))) && self.is_blank() + } + /// The point frame's matrix (the first frame's, when it is one), else the identity. fn matrix(&self) -> [f64; 6] { match self.frames.first() { @@ -1260,6 +1266,24 @@ impl Story { /// document), or `None` for type that is mirrored, has no size, or a frame past the first of /// threaded type (the first frame holds the whole story). pub(super) fn node(&self, id: NodeId, frame: usize, template: (f64, f64), to_doc: Affine) -> Option { + self.made_node(id, frame, template, to_doc, false) + } + + /// For a blank story (nothing but spaces and line ends) in an area frame or on a path, an empty + /// text object with that frame or path, as [`Self::node`] would make it, as VectorCraft's Type + /// tool keeps such an empty frame (#85, #154). Its text is the story's, spaces kept. `None` for + /// any other story or frame. + pub(super) fn blank_frame_node(&self, id: NodeId, frame: usize, template: (f64, f64), to_doc: Affine) -> Option { + self.is_blank().then(|| self.made_node(id, frame, template, to_doc, true)).flatten() + } + + /// Is the story's text nothing but spaces and line ends (`\r`, and `\x03`, a line break)? + pub(super) fn is_blank(&self) -> bool { + self.text.chars().all(|c| c.is_whitespace() || c == '\x03') + } + + /// [`Self::node`]; with `blank_frame`, a blank story in an area frame or on a path is made too. + fn made_node(&self, id: NodeId, frame: usize, template: (f64, f64), to_doc: Affine, blank_frame: bool) -> Option { let kind = self.frames.get(frame)?; let threaded = frame > 0; let (scale, xf, kind) = match kind { @@ -1301,7 +1325,9 @@ impl Story { }) .collect() }; - if text.trim().is_empty() && !threaded { + // A blank story isn't made here: the page may still have type for its object. What is left + // of one, an area frame or a path, is made by [`Self::blank_frame_node`]. + if text.trim().is_empty() && !threaded && !(blank_frame && !matches!(kind, TextKind::Point)) { return None; } // The text document measures in the art's units too: the document's scale carries its diff --git a/crates/eps/src/import/native/build.rs b/crates/eps/src/import/native/build.rs index 5a2282f807..2cba146548 100644 --- a/crates/eps/src/import/native/build.rs +++ b/crates/eps/src/import/native/build.rs @@ -531,12 +531,12 @@ pub(super) fn build_weighing( clip_by_type(&mut layers, &type_clips, &filled); doc.layers = layers; made.sort(); - let threaded_frames: BTreeSet = - made.chunk_by(|a, b| a.0 == b.0).filter(|thread| thread.len() > 1).flatten().map(|(_, _, id)| *id).collect(); + let threads = threads_of(&made); + let threaded_frames: BTreeSet = threads.iter().flat_map(|(_, ids)| ids.iter().copied()).collect(); if !unread.is_empty() { let Some(frame) = frame.as_ref() else { return Err(unread_error(&unread)) }; let mut copied = BTreeSet::new(); - let kinds = replace_unread(&mut doc, frame, &unread, &unread_elsewhere, &threaded_frames, &mut copied)?; + let kinds = replace_unread(&mut doc, frame, &unread, &unread_elsewhere, &held_frames(&infos, &stories, &threads), &mut copied)?; // With the text objects filled, the page art chosen can differ from the first try's: the // page's type set aside for it, and no other, must be in it. let all_copied = |o: &Vec>| !o.is_empty() && o.iter().all(|n| copied.contains(&n.id)); @@ -545,12 +545,8 @@ pub(super) fn build_weighing( } notes.push(format!("{}: they came in as the page's art", kinds.join(", "))); } - // A story in several frames is threaded type: its frames in order (the first holds the story). - for thread in made.chunk_by(|a, b| a.0 == b.0) { - if thread.len() > 1 { - doc.text_threads.push(thread.iter().map(|(_, _, id)| *id).collect()); - } - } + // The frames still threaded: a blank story's frame an unread object left alone isn't. + let threaded_frames = add_threads(&mut doc, threads, &threaded_frames, !unread.is_empty()); // Area type flows around the wrap objects above it before it is compared with the page. resolve_text_wrap(&mut doc)?; let bound_text = bound_slots(&doc, &bindings, &xml_ids, VariableKind::Text); @@ -1485,7 +1481,9 @@ fn slot_content( ) -> Result<(Vec, bool), String> { let made = make_slots(infos, stories, template, to_doc, notes); if let Some(own) = own.filter(|_| readable_all(infos, &made)) { - return Ok((made_only(infos, made, [own, pages[1]], notes), true)); + let mut content = made_only(infos, stories, made, [own, pages[1]], notes); + settle_blank_stories(infos, stories, template, to_doc, &mut content, notes); + return Ok((content, true)); } // The page's type is matched with every text object whose type isn't made, and weighed against // where every made one draws: that has its limits (the slots of type that looks draw are within @@ -1494,7 +1492,100 @@ fn slot_content( if open > MAX_SLOTS || infos.len().saturating_mul(pages[0].len()) > MAX_PAIRS { return Err("it has too many text objects".into()); } - Ok((plan(infos, stories, made, template, to_doc, pages, notes), false)) + let mut content = plan(infos, stories, made, template, to_doc, pages, notes); + settle_blank_stories(infos, stories, template, to_doc, &mut content, notes); + Ok((content, false)) +} + +/// The threads of the text objects `made` (story, frame and id of each, sorted): each story in +/// several frames, with its frames in order (the first holds the story). +fn threads_of(made: &[(u32, usize, NodeId)]) -> Vec<(u32, Vec)> { + made.chunk_by(|a, b| a.0 == b.0) + .filter(|thread| thread.len() > 1) + .map(|thread| (thread[0].0, thread.iter().map(|(_, _, id)| *id).collect())) + .collect() +} + +/// The frames of `threads` that an unread object can't take with it: those of a blank story's thread +/// hold no type, so they may go. +fn held_frames(infos: &[SlotInfo], stories: &[Option>], threads: &[(u32, Vec)]) -> BTreeSet { + let blank: BTreeSet = + infos.iter().zip(stories).filter(|(_, s)| s.as_ref().is_some_and(|s| s.is_blank())).filter_map(|(i, _)| i.story).collect(); + threads.iter().filter(|(story, _)| !blank.contains(story)).flat_map(|(_, ids)| ids.iter().copied()).collect() +} + +/// `threads` as `doc`'s text threads, and the frames of the threads added. With `replaced` (unread +/// objects were replaced by page art), the frames they took with them (`frames` that `doc` no longer +/// has, only a blank story's) are left out, and so is a thread left with one frame. +fn add_threads(doc: &mut Document, threads: Vec<(u32, Vec)>, frames: &BTreeSet, replaced: bool) -> BTreeSet { + let present = replaced.then(|| doc.index_paths(frames.iter().copied())); + let mut threaded = BTreeSet::new(); + for (_, mut ids) in threads { + if let Some(present) = &present { + ids.retain(|id| present.contains_key(id)); + } + if ids.len() > 1 { + threaded.extend(ids.iter().copied()); + doc.text_threads.push(ids); + } + } + threaded +} + +/// The slots of blank stories (nothing but spaces and line ends) that neither the text document nor +/// the page fills: blank point type draws nothing and has nothing to keep, so it is left out without +/// a note, as VectorCraft's Type tool discards empty point type (#85, #154); blank area type and +/// type on a path come in as an empty text object with their frame or path, as the Type tool keeps +/// such an empty frame, also where the page's type went to another text object (`Merged`). An empty +/// frame draws nothing, so the page can't show it: it is kept without being weighed against the +/// page (`prune_unseen` would take it out), and so are the later frames of its thread, which the +/// text document makes empty. Type that a look draws is left as it is. +fn settle_blank_stories( + infos: &[SlotInfo], + stories: &[Option>], + template: Option<(f64, f64)>, + to_doc: Affine, + content: &mut [Content], + notes: &mut Vec, +) { + // The stories whose first frame came in as an empty frame: their later frames stay with it. + let mut kept = BTreeSet::new(); + for ((story, info), c) in stories.iter().zip(infos).zip(content.iter_mut()) { + let Some(story) = story.as_ref().filter(|s| s.is_blank() && info.look.is_none()) else { continue }; + let unfilled = match c { + Content::Empty => true, + Content::Page(parts) => parts.is_empty(), + _ => false, + }; + if story.blank_point(info.frame) { + if unfilled { + *c = Content::Merged; + } + } else if (unfilled || matches!(c, Content::Merged)) + && let Some((node, region)) = template.and_then(|t| super::plan::blank_frame(info, story, t, to_doc)) + { + *c = Content::Made(node, region.filter(|_| info.shown), true); + kept.extend(info.story); + // Its paragraphs' settings are read as made type's are, with the same notes. + for (left_out, note) in [ + (story.tabs_left_out(), crate::import::ate::TABS_LEFT_OUT), + (story.kinsoku_sets_left_out(), crate::import::ate::KINSOKU_SETS_LEFT_OUT), + ] { + if left_out && !notes.iter().any(|n| n == note) { + notes.push(note.into()); + } + } + } + } + for (info, c) in infos.iter().zip(content.iter_mut()) { + if let Content::Made(_, _, seen) = c + && info.frame > 0 + && info.look.is_none() + && info.story.is_some_and(|s| kept.contains(&s)) + { + *seen = true; + } + } } /// The layers `doc` don't draw as the page (`why`), `unseen` text objects of them taken out: the diff --git a/crates/eps/src/import/native/plan.rs b/crates/eps/src/import/native/plan.rs index 005b89397e..17107dded9 100644 --- a/crates/eps/src/import/native/plan.rs +++ b/crates/eps/src/import/native/plan.rs @@ -66,17 +66,10 @@ pub(super) fn make_slots( .map(|(k, info)| { let t = template?; let story = stories.get(k)?.as_ref()?; - let mut node = story.node(NodeId(0), info.frame, t, to_doc)?; + let node = story.node(NodeId(0), info.frame, t, to_doc)?; tabs_left_out |= story.tabs_left_out(); kinsoku_left_out |= story.kinsoku_sets_left_out(); - let mut region = story.region(t, to_doc); - // The first line where the file has it, in the text document's frame (that of a - // copied object is its original's, as its lines are). - story.place_first_line(&mut node, info.frame, t, to_doc); - // Area type and type on a path in the object's own frame: the text document's frame - // of a copied object keeps its original's path. - frame_text_from_art(info, &mut node, &mut region); - story.flip_on_path(&mut node, info.frame); + let (mut node, region) = placed(info, story, node, t, to_doc); if let NodeKind::Text(text) = &mut node.kind && story.fit_first_line(text) { @@ -109,6 +102,28 @@ pub(super) fn make_slots( made } +/// The empty text object a slot of a blank story in an area frame or on a path comes in as, placed +/// as [`make_slots`] places made type: its frame or path, the object's own where it has one +/// ([`Story::blank_frame_node`]). `None` for any other slot. +pub(super) fn blank_frame(info: &SlotInfo, story: &Story, template: (f64, f64), to_doc: Affine) -> Option { + let node = story.blank_frame_node(NodeId(0), info.frame, template, to_doc)?; + let (node, region) = placed(info, story, node, template, to_doc); + Some((Box::new(node), region)) +} + +/// `node`, made from `story` for slot `info`, placed where the file has it, with the region it +/// draws in: the first line where the file has it, in the text document's frame (that of a copied +/// object is its original's, as its lines are); area type and type on a path in the object's own +/// frame (the text document's frame of a copied object keeps its original's path); type on a path +/// flipped as its frame keeps it. +fn placed(info: &SlotInfo, story: &Story, mut node: Node, template: (f64, f64), to_doc: Affine) -> (Node, Option) { + let mut region = story.region(template, to_doc); + story.place_first_line(&mut node, info.frame, template, to_doc); + frame_text_from_art(info, &mut node, &mut region); + story.flip_on_path(&mut node, info.frame); + (node, region) +} + /// Does the file's text document make the type of every slot that shows (at least one does)? Type /// that a look draws doesn't count: only the page has it. pub(super) fn readable_all(infos: &[SlotInfo], made: &[Option]) -> bool { @@ -122,7 +137,13 @@ pub(super) fn readable_all(infos: &[SlotInfo], made: &[Option]) -> bool { /// that no made type takes, shown or not, is left out with a note in `warn`. Slots that don't show /// and whose type isn't made take the page's type that doesn't show (`pages`: the page's text /// objects that show and that don't), by painting order. -pub(super) fn made_only(infos: &[SlotInfo], made: Vec>, pages: [&[Vec>]; 2], warn: &mut Vec) -> Vec { +pub(super) fn made_only( + infos: &[SlotInfo], + stories: &[Option>], + made: Vec>, + pages: [&[Vec>]; 2], + warn: &mut Vec, +) -> Vec { let [shown, hidden] = pages; let mut content = made_content(infos, made); // The page's type where no made type draws, in the copies that looks draw, is theirs (past @@ -154,7 +175,7 @@ pub(super) fn made_only(infos: &[SlotInfo], made: Vec>, pages: [&[V None => warn.push(PAGE_TYPE_UNCHECKED.into()), } drop_look_slots(infos, &mut content); - fill_hidden(infos, &mut content, hidden); + fill_hidden(infos, stories, &mut content, hidden); content } @@ -421,8 +442,10 @@ pub(super) fn plan( } fill_look_slots(infos, &mut content, &mut spare); // What is left of the page goes by painting order into the shown slots the file can't say. - let loose: Vec = - infos.iter().enumerate().filter(|(k, i)| i.shown && matches!(content.get(*k), Some(Content::Empty))).map(|(k, _)| k).collect(); + let loose = readable_first( + stories, + infos.iter().enumerate().filter(|(k, i)| i.shown && matches!(content.get(*k), Some(Content::Empty))).map(|(k, _)| k).collect(), + ); if loose.is_empty() { // Type of the page that no text object says it owns goes with the nearest text object that has // type of the page (never into made type); with none, it is left out. @@ -466,17 +489,28 @@ pub(super) fn plan( warn.push("the text is on the layers in the order the page paints it, so a text object may be in another group than it was".into()); } } - fill_hidden(infos, &mut content, hidden); + fill_hidden(infos, stories, &mut content, hidden); content } +/// The slots `open` that are to take the page's type left over, by painting order: those of blank +/// stories (nothing but spaces and line ends) only when no other slot is open, as such a story has +/// no type of its own; the blank ones left out come in empty or not at all (see +/// `settle_blank_stories`). +fn readable_first(stories: &[Option>], open: Vec) -> Vec { + let blank = |k: &usize| stories.get(*k).and_then(Option::as_ref).is_some_and(|s| s.is_blank()); + if open.iter().all(blank) { open } else { open.into_iter().filter(|k| !blank(k)).collect() } +} + /// The page's type that doesn't show (`hidden`) goes by painting order into the slots that don't -/// show and have nothing yet, but for what is the type of made type that doesn't show: a page object +/// show and have nothing yet ([`readable_first`]), but for what is the type of made type that doesn't show: a page object /// with words and at least half of it where such type draws, while the type has all its words left /// (each word an object takes is the type's no longer). Past `MAX_PAIRS` comparisons all of it goes. -fn fill_hidden(infos: &[SlotInfo], content: &mut [Content], hidden: &[Vec>]) { - let unread: Vec = - infos.iter().enumerate().filter(|(k, i)| !i.shown && matches!(content.get(*k), Some(Content::Empty))).map(|(k, _)| k).collect(); +fn fill_hidden(infos: &[SlotInfo], stories: &[Option>], content: &mut [Content], hidden: &[Vec>]) { + let unread = readable_first( + stories, + infos.iter().enumerate().filter(|(k, i)| !i.shown && matches!(content.get(*k), Some(Content::Empty))).map(|(k, _)| k).collect(), + ); if unread.is_empty() { return; } diff --git a/crates/eps/src/import/native/slots.rs b/crates/eps/src/import/native/slots.rs index bce43140d1..ebd6deef88 100644 --- a/crates/eps/src/import/native/slots.rs +++ b/crates/eps/src/import/native/slots.rs @@ -2,7 +2,7 @@ use super::build::MADE; use super::{AppearanceItem, Arc, BTreeSet, BlendMode, Document, Node, NodeId, NodeKind, PathData, Point, Rect, ai, slot_of}; /// The notes that say the import left out something the file has (see [`is_loss`]). -pub(super) const TEXT_LEFT_OUT: &str = "text objects on hidden layers or hidden objects couldn't be read, so they are left out"; +pub(super) const TEXT_LEFT_OUT: &str = "text objects whose type couldn't be read from the file's text document or its page are left out"; /// The same for a file without a page to take shown type from. pub(super) const TYPE_LEFT_OUT: &str = "text objects of kinds VectorCraft doesn't read from the file's text document yet are left out"; @@ -230,7 +230,8 @@ pub(super) enum Content { /// Text objects (with the strokes round them) of the page. Page(Vec>), /// A text object made from the file's text document, where it draws on the document (when it - /// shows), and whether the page has type where it is. + /// shows), and whether it is kept without being weighed against the page: the page has type + /// where it is, or it draws nothing (an empty frame of a blank story). Made(Box, Option, bool), /// Nothing the file lets this read. Empty, diff --git a/crates/eps/src/import/tests_native.rs b/crates/eps/src/import/tests_native.rs index dc0f225872..f809141202 100644 --- a/crates/eps/src/import/tests_native.rs +++ b/crates/eps/src/import/tests_native.rs @@ -3830,7 +3830,7 @@ fn type_with_a_frame_it_cant_read_is_left_out_with_a_note() { let art = native_with_text(&(layer("Back", true, &square(10, 10)) + &layer("Spare", false, &text_object(0))), &doc); let r = import(&eps(&page_square(10, 10), &art)).unwrap(); assert!(text_of(&r.document.layers[1]).is_empty()); - assert!(r.warnings.iter().any(|w| w.contains("1 text objects on hidden layers")), "{:?}", r.warnings); + assert!(r.warnings.iter().any(|w| w.contains("1 text objects whose type couldn't be read")), "{:?}", r.warnings); assert!(crate::is_loss(r.warnings.iter().find(|w| w.contains("couldn't be read")).unwrap())); } @@ -5030,6 +5030,274 @@ fn area_type_keeps_its_area_type_options() { assert_eq!(texts[2].area, vectorcraft_doc::AreaOptions::default()); } +/// A blank story in an area frame, hidden or shown, comes in as a text object that keeps its frame +/// and draws nothing (its spaces kept), as VectorCraft's Type tool keeps an empty frame (#85, #154). +/// As point type, hidden or shown, it draws nothing and has nothing to keep: it is left out, without +/// a note that something wasn't read. +#[test] +fn blank_stories_keep_their_area_frame_and_leave_point_type_out_quietly() { + let frames = [area_frame(10.0, 50.0, 90.0, 90.0), point_frame(20.0, 20.0), point_frame(30.0, 30.0), area_frame(60.0, 10.0, 90.0, 40.0)]; + let stories = [ + story("\\r", &[0], "/0 0", "/1 6.0"), + story("\\r", &[1], "/0 0", "/1 6.0"), + story(" \\r", &[2], "/0 0", "/1 6.0"), + story(" \\r", &[3], "/0 0", "/1 6.0"), + ]; + let doc = document_of(&frames, &stories); + // A shown blank area frame too: it draws nothing, so the page can't show it, and it stays. + let shown = square(10, 10) + &text_object(2) + &text_object(3); + let art = native_with_text(&(layer("Back", true, &shown) + &layer("Spare", false, &(text_object(0) + &text_object(1)))), &doc); + let r = import(&eps(&page_square(10, 10), &art)).unwrap(); + let texts = text_of(&r.document.layers[1]); + assert_eq!(texts.len(), 1, "{:?}", r.warnings); + assert_eq!(texts[0].plain_text(), ""); + let vectorcraft_doc::TextKind::Area { frame } = &texts[0].kind else { panic!("area type: {:?}", texts[0].kind) }; + // Art y 50..90 is 10..50 down the 100 pt page. + let b = frame.bounds().unwrap(); + assert!((b.x0 - 10.0).abs() < 1e-6 && (b.y0 - 10.0).abs() < 1e-6 && (b.x1 - 90.0).abs() < 1e-6 && (b.y1 - 50.0).abs() < 1e-6, "{b:?}"); + let shown = text_of(&r.document.layers[0]); + assert_eq!(shown.len(), 1, "{:?}", r.warnings); + assert_eq!(shown[0].plain_text(), " "); + let vectorcraft_doc::TextKind::Area { frame } = &shown[0].kind else { panic!("area type: {:?}", shown[0].kind) }; + // Art y 10..40 is 60..90 down the page. + let b = frame.bounds().unwrap(); + assert!((b.x0 - 60.0).abs() < 1e-6 && (b.y0 - 60.0).abs() < 1e-6 && (b.x1 - 90.0).abs() < 1e-6 && (b.y1 - 90.0).abs() < 1e-6, "{b:?}"); + assert!(!r.warnings.iter().any(|w| w.contains("couldn't be read")), "{:?}", r.warnings); +} + +/// A blank story whose object the page still draws type for takes the page's type, as before: the +/// empty frame is only for a slot nothing else fills. +#[test] +fn a_blank_area_story_the_page_draws_type_for_takes_the_pages_type() { + let doc = document_of(&[area_frame(10.0, 50.0, 90.0, 90.0)], &[story("\\r", &[0], "/0 0", "/1 6.0")]); + let art = native_with_text(&layer("Art", true, &text_object(0)), &doc); + let page = "0 0 0 1 setcmykcolor /Helvetica findfont 14 scalefont setfont 20 75 moveto (Hi there) show"; + let r = import(&eps(page, &art)).unwrap(); + let mut all = vec![]; + r.document.walk(|n| { + if let NodeKind::Text(t) = &n.kind { + all.push(t.plain_text()); + } + }); + assert_eq!(all, ["Hi there".to_string()], "{:?}", r.warnings); +} + +/// The text objects of `d`: each one's text and kind, in the layers' order. +fn text_kinds(d: &Document) -> Vec<(String, &'static str)> { + let mut out = vec![]; + d.walk(|n| { + if let NodeKind::Text(t) = &n.kind { + let kind = match t.kind { + vectorcraft_doc::TextKind::Area { .. } => "area", + vectorcraft_doc::TextKind::OnPath { .. } => "path", + _ => "point", + }; + out.push((t.plain_text(), kind)); + } + }); + out +} + +/// A blank story threaded through two shown area frames comes in as both frames, empty and still +/// one thread: the later frame the text document makes empty stays with the first. +#[test] +fn a_blank_story_threaded_through_two_shown_frames_keeps_both() { + let frames = [area_frame(10.0, 60.0, 40.0, 80.0), area_frame(50.0, 60.0, 80.0, 80.0)]; + let doc = document_of(&frames, &[story("\\r", &[0, 1], "/0 0", "/1 10.0")]); + let art = native_with_text(&layer("Art", true, &(square(10, 10) + &frame_object(0, 0) + &frame_object(0, 1))), &doc); + let r = import(&eps(&page_square(10, 10), &art)).unwrap(); + // The thread names only text objects the document has. + assert!(r.document.text_threads.iter().flatten().all(|id| r.document.node(*id).is_some()), "{:?} {:?}", r.document.text_threads, r.warnings); + assert_eq!(text_kinds(&r.document), [("".to_string(), "area"), ("".to_string(), "area")], "{:?}", r.warnings); + let ids: Vec<_> = r.document.layers[0].children().unwrap().iter().filter(|n| matches!(n.kind, NodeKind::Text(_))).map(|n| n.id).collect(); + assert_eq!(r.document.text_threads, [ids]); +} + +/// A blank story threaded from a hidden frame into a shown one: both frames come in, one thread. +#[test] +fn a_blank_story_threaded_from_a_hidden_frame_into_a_shown_one_keeps_both() { + let frames = [area_frame(10.0, 60.0, 40.0, 80.0), area_frame(50.0, 60.0, 80.0, 80.0)]; + let doc = document_of(&frames, &[story("\\r", &[0, 1], "/0 0", "/1 10.0")]); + let art = native_with_text(&(layer("Back", true, &(square(10, 10) + &frame_object(0, 1))) + &layer("Spare", false, &frame_object(0, 0))), &doc); + let r = import(&eps(&page_square(10, 10), &art)).unwrap(); + assert!(r.document.text_threads.iter().flatten().all(|id| r.document.node(*id).is_some()), "{:?} {:?}", r.document.text_threads, r.warnings); + assert_eq!(text_kinds(&r.document), [("".to_string(), "area"), ("".to_string(), "area")], "{:?}", r.warnings); + assert_eq!(r.document.text_threads.len(), 1, "{:?}", r.warnings); + assert_eq!(r.document.text_threads[0].len(), 2, "{:?}", r.warnings); +} + +/// An unread group holding a frame of a blank story's thread is replaced by its page art as for +/// any other text object (the frame holds no type): the layers are read, and the thread, left with +/// one frame, isn't one any more. +#[test] +fn unread_group_holding_a_blank_threaded_frame_is_replaced() { + let frames = [ + format!("<< /0 << /0 [ 0 0 ] /1 << /0 [ {} ] >> /2 << /0 1 >> >> >>", canvas_rect(50.0, 10.0, 54.0, 14.0)), + format!("<< /0 << /0 [ 0 0 ] /1 << /0 [ {} ] >> /2 << /0 1 >> >> >>", canvas_rect(70.0, 10.0, 74.0, 14.0)), + ]; + let text = document_of(&frames, &[story("\\r", &[0, 1], "/0 0", "/1 10.0")]); + for removed in [0, 1] { + let grouped = os1_group(&format!("{}\n{}\nZZ", square(if removed == 0 { 50 } else { 70 }, 10), frame_object(0, removed))); + let other = frame_object(0, 1 - removed); + let art = native_with_text(&(layer("Back", true, &square(10, 10)) + &layer("Mixed", true, &format!("{grouped}\n{other}"))), &text); + let (doc, notes, read) = crate::layered_ai(&private(&art), pdf_three_squares(), vec![], false, true, 1.0); + assert!(read, "removed frame {removed}: {notes:?}"); + assert!(doc.text_threads.is_empty(), "removed frame {removed}: {:?} {notes:?}", doc.text_threads); + assert_eq!(text_kinds(&doc), [("".to_string(), "area")], "removed frame {removed}: {notes:?}"); + } +} + +/// A blank story on a path comes in as empty type on that path. +#[test] +fn a_blank_story_on_a_path_keeps_its_path() { + let (a, b, c) = (canvas(10.0, 70.0), canvas(30.0, 70.0), canvas(50.0, 70.0)); + let path = format!( + "<< /0 << /0 [ 0 0 ] /1 << /0 [ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} ] >> /2 << /0 2 /6 [ 0.5 2.0 ] >> >> >>", + a.0, a.1, a.0, a.1, b.0, b.1, b.0, b.1, b.0, b.1, b.0, b.1, c.0, c.1, c.0, c.1 + ); + let doc = document_of(&[path], &[story("\\r", &[0], "/0 0", "/1 8.0")]); + let art = native_with_text(&layer("Art", true, &(square(10, 10) + &text_object(0))), &doc); + let r = import(&eps(&page_square(10, 10), &art)).unwrap(); + assert_eq!(text_kinds(&r.document), [("".to_string(), "path")], "{:?}", r.warnings); + let mut on_path = vec![]; + r.document.walk(|n| { + if let NodeKind::Text(t) = &n.kind + && let vectorcraft_doc::TextKind::OnPath { path, start, end } = &t.kind + { + on_path.push((path.bounds(), *start, *end)); + } + }); + // The path as `area_type_and_type_on_a_path_come_from_the_text_document` reads it: 40 pt long, + // the type from a quarter of the way along. + let [(Some(b), start, end)] = on_path.as_slice() else { panic!("{on_path:?}") }; + assert!((b.width() - 40.0).abs() < 1e-6 && (start - 0.25).abs() < 1e-3 && end.is_none(), "{b:?} {start} {end:?}"); +} + +/// Where every shown text object's type comes from the text document (the page isn't matched with +/// them), a hidden blank area story still comes in as its empty frame, and blank point type is left +/// out without a note. +#[test] +fn blank_stories_beside_type_the_text_document_makes() { + let frames = [point_frame(20.0, 20.0), area_frame(10.0, 50.0, 90.0, 90.0), point_frame(30.0, 30.0)]; + let stories = [story("Hi there\\r", &[0], "/0 0", "/1 6.0"), story("\\r", &[1], "/0 0", "/1 6.0"), story("\\r", &[2], "/0 0", "/1 6.0")]; + let doc = document_of(&frames, &stories); + let art = native_with_text( + &(layer("Back", true, &(square(10, 10) + &text_object(0))) + &layer("Spare", false, &(text_object(1) + &text_object(2)))), + &doc, + ); + // The page draws the square and "Hi there" where the text document sets it. + let page = format!("{}\n0 0 0 1 setcmykcolor /Helvetica findfont 6 scalefont setfont 20 20 moveto (Hi there) show", page_square(10, 10)); + let r = import(&eps(&page, &art)).unwrap(); + assert_eq!(text_kinds(&r.document), [("Hi there".to_string(), "point"), ("".to_string(), "area")], "{:?}", r.warnings); + assert!(!r.warnings.iter().any(|w| w.contains("couldn't be read")), "{:?}", r.warnings); +} + +/// Without a page (the editing data alone), a blank area story comes in as its empty frame and +/// blank point type is left out, without the note for type of kinds that aren't read. +#[test] +fn blank_stories_without_a_page() { + let frames = [area_frame(10.0, 50.0, 90.0, 90.0), point_frame(30.0, 30.0)]; + let doc = document_of(&frames, &[story("\\r", &[0], "/0 0", "/1 6.0"), story(" \\r", &[1], "/0 0", "/1 6.0")]); + let art = native_with_text(&layer("Art", true, &(text_object(0) + &text_object(1))), &doc); + let (d, notes) = crate::ai_alone(&private(&art)).unwrap(); + assert_eq!(text_kinds(&d), [("".to_string(), "area")], "{notes:?}"); + assert!(!notes.iter().any(|w| w.contains("left out")), "{notes:?}"); +} + +/// The page's spare type goes to a shown text object the text document can't make, not to a blank +/// story beside it, whichever the page paints first: the blank area story comes in as its empty +/// frame. A blank slot takes the page's type only where no other slot is open. +#[test] +fn spare_page_type_goes_to_the_unread_object_not_the_blank_one() { + // Story 0's frame is of a kind this doesn't read, so neither shown object is made: the page's + // one piece of type is spare. + let frames = [point_frame(20.0, 20.0).replace("/2 << /2 [", "/2 << /0 9 /2 ["), area_frame(10.0, 50.0, 90.0, 90.0)]; + let doc = document_of(&frames, &[story("Hi there\\r", &[0], "/0 0", "/1 6.0"), story("\\r", &[1], "/0 0", "/1 6.0")]); + let page = "0 0 0 1 setcmykcolor /Helvetica findfont 6 scalefont setfont 20 20 moveto (Hi there) show"; + for (order, body) in [("blank first", text_object(1) + &text_object(0)), ("blank last", text_object(0) + &text_object(1))] { + let art = native_with_text(&layer("Art", true, &body), &doc); + let r = import(&eps(page, &art)).unwrap(); + let mut texts = text_kinds(&r.document); + texts.sort(); + assert_eq!(texts, [("".to_string(), "area"), ("Hi there".to_string(), "point")], "{order}: {:?}", r.warnings); + } +} + +/// Two shown blank area stories and one piece of the page's type that no text object owns: with no +/// other slot open, the blank ones take it by painting order; the one left without type comes in as +/// its empty frame, not dropped as merged type. +#[test] +fn a_blank_area_story_the_spare_page_type_skips_keeps_its_frame() { + let frames = [area_frame(10.0, 50.0, 90.0, 90.0), area_frame(10.0, 5.0, 40.0, 30.0)]; + let doc = document_of(&frames, &[story("\\r", &[0], "/0 0", "/1 6.0"), story("\\r", &[1], "/0 0", "/1 6.0")]); + let art = native_with_text(&layer("Art", true, &(text_object(0) + &text_object(1))), &doc); + // Away from both frames, so neither owns it. + let page = "0 0 0 1 setcmykcolor /Helvetica findfont 6 scalefont setfont 60 40 moveto (Hi there) show"; + let r = import(&eps(page, &art)).unwrap(); + let mut texts = text_kinds(&r.document); + texts.sort(); + assert_eq!(texts, [("".to_string(), "area"), ("Hi there".to_string(), "point")], "{:?}", r.warnings); +} + +/// A warp or an envelope holding the second frame of a blank story threaded from a frame outside +/// it: a thread names only text objects the document has. The warped frame stays type, and the +/// thread stays whole, as for other threaded type, whatever the warp's look draws; the frame in the +/// envelope comes in as the envelope's drawn art, and no thread is left. +#[test] +fn a_warp_or_an_envelope_holding_a_frame_of_a_blank_thread_keeps_its_thread_of_text() { + let frames = [area_frame(10.0, 5.0, 40.0, 30.0), area_frame(20.0, 40.0, 80.0, 70.0)]; + let doc = document_of(&frames, &[story("\\r", &[0, 1], "/0 0", "/1 10.0")]); + let envelope = type_envelope_of(0, &arc_keys(0.3), &format!("{}\n", square(30, 50)), "").replace("%_0 /FrameIndex ,", "%_1 /FrameIndex ,"); + assert!(envelope.contains("%_1 /FrameIndex ,")); + let warp: String = frame_object(0, 1).lines().map(|l| format!("%_{l}\n")).collect(); + let warped = format!("u\n0 1 1 0 1 0 0 Xa\n2 2 m\n9 2 L\n9 9 L\n2 9 L\nf\nU\n9 () XW\n{warp}1 (Warped) XW\n"); + let check = |what: &str, d: &Document, notes: &[String]| { + assert!( + d.text_threads.iter().flatten().all(|id| d.node(*id).is_some_and(|n| matches!(n.kind, NodeKind::Text(_)))), + "{what}: {:?} {notes:?}", + d.text_threads + ); + }; + let art = native_with_text(&layer("Art", true, &format!("{}\n{}\n{envelope}", square(10, 10), frame_object(0, 0))), &doc); + // The page draws the envelope's drawn art. + let r = import(&eps(&format!("{}\n{}", page_square(10, 10), page_square(30, 50)), &art)).unwrap(); + check("envelope, with the page", &r.document, &r.warnings); + assert!(r.document.text_threads.is_empty(), "{:?} {:?}", r.document.text_threads, r.warnings); + let (d, notes) = crate::ai_alone(&private(&art)).unwrap(); + check("envelope, editing data alone", &d, ¬es); + assert!(d.text_threads.is_empty(), "{:?} {notes:?}", d.text_threads); + let art = native_with_text(&(warped_style(40.0, WarpOn::Object) + &layer("Art", true, &format!("{}\n{warped}", frame_object(0, 0)))), &doc); + let (d, notes) = crate::ai_alone(&private(&art)).unwrap(); + check("warp", &d, ¬es); + assert_eq!(d.text_threads.len(), 1, "{notes:?}"); + assert!(!notes.iter().any(|w| w.starts_with(crate::import::ai::WARPED_TYPE_DRAWN)), "{notes:?}"); +} + +/// A blank story's thread whose first frame an unread group takes with it leaves its other frame +/// alone, no longer threaded: in a warp, that frame is weighed against its look as any warped text +/// object is (the look draws a square the empty frame doesn't, so the look takes its place). +#[test] +fn a_blank_frame_an_unread_group_leaves_alone_in_a_warp_is_weighed_as_lone_type() { + let frames = [ + format!("<< /0 << /0 [ 0 0 ] /1 << /0 [ {} ] >> /2 << /0 1 >> >> >>", canvas_rect(50.0, 10.0, 54.0, 14.0)), + format!("<< /0 << /0 [ 0 0 ] /1 << /0 [ {} ] >> /2 << /0 1 >> >> >>", canvas_rect(70.0, 10.0, 74.0, 14.0)), + ]; + let text = document_of(&frames, &[story("\\r", &[0, 1], "/0 0", "/1 10.0")]); + let grouped = os1_group(&format!("{}\n{}\nZZ", square(50, 10), frame_object(0, 0))); + let own: String = frame_object(0, 1).lines().map(|l| format!("%_{l}\n")).collect(); + let warped = format!("u\n0 1 1 0 1 0 0 Xa\n70 10 m\n74 10 L\n74 14 L\n70 14 L\nf\nU\n9 () XW\n{own}1 (Warped) XW\n"); + let art = native_with_text( + &(warped_style(40.0, WarpOn::Object) + &layer("Back", true, &square(10, 10)) + &layer("Mixed", true, &format!("{grouped}\n{warped}"))), + &text, + ); + let (doc, notes, read) = crate::layered_ai(&private(&art), pdf_three_squares(), vec![], false, true, 1.0); + assert!(read, "{notes:?}"); + assert!(doc.text_threads.is_empty(), "{:?} {notes:?}", doc.text_threads); + assert!(notes.iter().any(|w| w.starts_with(crate::import::ai::WARPED_TYPE_DRAWN)), "{notes:?}"); + // The look took the frame's place: no text object is left. + assert!(text_kinds(&doc).is_empty(), "{:?} {notes:?}", text_kinds(&doc)); +} + /// A point type frame at the point (`x`, `y`) of the art. fn point_frame(x: f64, y: f64) -> String { let (cx, cy) = canvas(x, y);