diff --git a/crates/eps/src/import/ai/envelope.rs b/crates/eps/src/import/ai/envelope.rs index d9a14a0239..6ff37bec92 100644 --- a/crates/eps/src/import/ai/envelope.rs +++ b/crates/eps/src/import/ai/envelope.rs @@ -59,6 +59,9 @@ pub(crate) fn drawn_note(typed: bool) -> &'static str { pub(super) const MAX_PENDING: usize = 64; /// The most envelopes and warped text objects one import draws to check; more keep their drawn art. pub(crate) const MAX_CHECKED: usize = 256; +/// The most comparisons with pages those checks make in one import, one for each page an object +/// is on ([`drawn_alike`]); an object that needs more keeps its drawn art. +pub(crate) const MAX_PAGE_COMPARISONS: usize = 4 * MAX_CHECKED; /// The most strokes with dashes aligned to corners one import draws to check against their drawn /// looks (apart from [`MAX_CHECKED`]); more keep their drawn looks. pub(crate) const MAX_DASH_CHECKS: usize = 256; @@ -664,13 +667,31 @@ fn differing(live: &Document, drawn: &Document, region: Rect, scale: f64) -> Opt } /// The differences from their drawn art that the live envelopes and warped type of one import may -/// add up to, as shares of the page drawn as the page check draws it: half of what that check allows -/// ([`MAX_DIFFERENCE`], [`MAX_SOLID_DIFFERENCE`]), so they never make the file open from its PDF part -/// instead. +/// add up to on each page the art may be compared with, as shares of that page drawn as the page +/// check draws it: half of what that check allows ([`MAX_DIFFERENCE`], [`MAX_SOLID_DIFFERENCE`]), +/// so they never make the file open from its PDF part instead. Each page has its own share. #[derive(Debug, Default)] pub(crate) struct Spent { - pub(super) any: f64, - pub(super) solid: f64, + /// Each page with the shares of it spent so far: of pixels that differ at all, and of those + /// whose neighbors all differ too. + pub(super) pages: Vec<(Rect, f64, f64)>, + /// How many comparisons with pages have been made ([`MAX_PAGE_COMPARISONS`]). + pub(super) compared: usize, +} + +impl Spent { + /// The shares of `page` spent so far. + pub(super) fn on(&self, page: Rect) -> (f64, f64) { + self.pages.iter().find(|(p, ..)| *p == page).map_or((0.0, 0.0), |&(_, any, solid)| (any, solid)) + } + + /// Set the shares of `page` spent so far. + fn set(&mut self, page: Rect, any: f64, solid: f64) { + match self.pages.iter_mut().find(|(p, ..)| *p == page) { + Some(spent) => *spent = (page, any, solid), + None => self.pages.push((page, any, solid)), + } + } } /// A text object read with a Warp. @@ -738,7 +759,8 @@ pub struct WarpedType { pub(super) envelopes: Vec, pub(super) checks: usize, pub(super) spent: Spent, - pub(super) page: Option, + /// The pages the art may be compared with, each with its own share of the budget ([`Spent`]). + pub(super) pages: Vec, /// [`super::Structure::type_clips`]: type in an envelope's contents clips its clipping group too. pub(super) type_clips: HashMap, /// The envelopes of type that may open live in layers built without a page, by their place @@ -884,7 +906,7 @@ impl WarpedType { let (id, layer) = (doc.alloc_id(), doc.alloc_id()); let mut plain = art.clone(); (plain.visible, plain.opacity) = (true, 1.0); - let page = self.page.filter(|_| shown.contains(&drawn)); + let pages: Vec = if shown.contains(&drawn) { self.pages.clone() } else { vec![] }; let mut alike = None; // In Edit Contents, a grid that draws the type elsewhere than its drawn art is tried again // moved onto that art ([`moved_onto`]), after every other way. @@ -917,7 +939,7 @@ impl WarpedType { let shared = Shared { patterns: &doc.patterns, images: &doc.images }; let mut live_one = envelope.clone(); (live_one.visible, live_one.opacity) = (true, 1.0); - if drawn_alike(&live_one, &plain, layer, &shared, page, &mut self.spent) { + if drawn_alike(&live_one, &plain, layer, &shared, &pages, &mut self.spent) { alike = Some(envelope); break; } @@ -942,7 +964,8 @@ impl WarpedType { /// budget has no check left for) with that look: `made` gives the text object a slot became. A /// frame in `threaded` stays type, since the frames of a thread share one story. A replaced /// object's box goes back into the comparisons with the page (out of `restyled`), and only what - /// `doc` shows counts against the page's share of the budget. Returns how many were replaced. + /// `doc` shows counts against the share of the budget of each page it is on. Returns how many + /// were replaced. pub(crate) fn settle( self, doc: &mut Document, @@ -950,7 +973,7 @@ impl WarpedType { threaded: &BTreeSet, restyled: &mut Vec, ) -> usize { - let Self { looks, mut checks, mut spent, page, .. } = self; + let Self { looks, mut checks, mut spent, pages, .. } = self; let mut shown_ids = BTreeSet::new(); doc.layers.iter().for_each(|l| collect_shown(l, &mut shown_ids)); let mut replaced = 0; @@ -965,7 +988,8 @@ impl WarpedType { let (mut shown, mut plain) = (text.clone(), look.clone()); (shown.visible, shown.opacity) = (true, 1.0); (plain.visible, plain.opacity) = (true, 1.0); - alike = warped_alike(&shown, &plain, layer, &shared, page.filter(|_| shown_ids.contains(&id)), &mut spent); + let on: &[Rect] = if shown_ids.contains(&id) { &pages } else { &[] }; + alike = warped_alike(&shown, &plain, layer, &shared, on, &mut spent); } if alike { continue; @@ -1032,23 +1056,24 @@ pub(super) fn warps(appearance: &Appearance) -> bool { } /// Do `live` and `drawn` draw alike? Either neither has any paint ([`paints`]), or few of the -/// pixels either inks differ and fewer still where a whole patch differs, and, on `page` (when the -/// file has one), they differ little enough that with the differences in `spent` the page check -/// still takes the layers' art (it is added to `spent` when they do). Both are drawn on `layer`, a -/// layer id no other node has. -pub(super) fn drawn_alike(live: &Node, drawn: &Node, layer: NodeId, shared: &Shared<'_>, page: Option, spent: &mut Spent) -> bool { - alike_within(live, drawn, live.visual_bounds(), layer, shared, page, spent) +/// pixels either inks differ and fewer still where a whole patch differs, and, on each of `pages` +/// they are on (the pages the art may be compared with), they differ little enough that with the +/// differences `spent` holds for that page the page check still takes the layers' art (they are +/// added to `spent` when they do on every one). Both are drawn on `layer`, a layer id no other node +/// has. +pub(super) fn drawn_alike(live: &Node, drawn: &Node, layer: NodeId, shared: &Shared<'_>, pages: &[Rect], spent: &mut Spent) -> bool { + alike_within(live, drawn, live.visual_bounds(), layer, shared, pages, spent) } /// [`drawn_alike`] for type under a Warp, compared over all that its effects draw, which can reach /// past its own box. -pub(super) fn warped_alike(live: &Node, drawn: &Node, layer: NodeId, shared: &Shared<'_>, page: Option, spent: &mut Spent) -> bool { +pub(super) fn warped_alike(live: &Node, drawn: &Node, layer: NodeId, shared: &Shared<'_>, pages: &[Rect], spent: &mut Spent) -> bool { let reach = vectorcraft_render::effect_bounds(&alone(live, layer, shared).symbol_boxes(), live); - alike_within(live, drawn, vectorcraft_geom::union_opt(live.visual_bounds(), reach), layer, shared, page, spent) + alike_within(live, drawn, vectorcraft_geom::union_opt(live.visual_bounds(), reach), layer, shared, pages, spent) } /// [`drawn_alike`], with `live` measured as `reach`. -fn alike_within(live: &Node, drawn: &Node, reach: Option, layer: NodeId, shared: &Shared<'_>, page: Option, spent: &mut Spent) -> bool { +fn alike_within(live: &Node, drawn: &Node, reach: Option, layer: NodeId, shared: &Shared<'_>, pages: &[Rect], spent: &mut Spent) -> bool { // Neither has any paint (type with no fill and no stroke, its drawn art the outlines unpainted): // both draw nothing, so they draw alike and add nothing to what the page check sees. Their // shapes aren't compared. Art that paints, however faintly, is compared as before. @@ -1069,21 +1094,31 @@ fn alike_within(live: &Node, drawn: &Node, reach: Option, layer: NodeId, s if share(solid) > MAX_SOLID || share(any) > MAX_ANY { return false; } - let Some(page) = page.filter(|p| p.is_finite() && p.width() > 0.0 && p.height() > 0.0) else { return true }; - // Only what is on the page counts, drawn as the page check draws it (about 400 pixels along the - // page's longer side). - let on_page = region.intersect(page); - let scale = compare_scale(page); - if on_page.width() * scale < 1.0 || on_page.height() * scale < 1.0 { - return true; + let mut added = vec![]; + for &page in pages.iter().filter(|p| p.is_finite() && p.width() > 0.0 && p.height() > 0.0) { + // Only what is on the page counts, drawn as the page check draws it (about 400 pixels along + // the page's longer side). + let on_page = region.intersect(page); + let scale = compare_scale(page); + if on_page.width() * scale < 1.0 || on_page.height() * scale < 1.0 { + continue; + } + if spent.compared >= MAX_PAGE_COMPARISONS { + return false; + } + spent.compared += 1; + let Some((any, solid, _)) = differing(&x, &y, on_page, scale) else { return false }; + let pixels = (page.width() * scale * page.height() * scale).max(1.0); + let (was_any, was_solid) = spent.on(page); + let (any, solid) = (was_any + any as f64 / pixels, was_solid + solid as f64 / pixels); + if any > MAX_DIFFERENCE / 2.0 || solid > MAX_SOLID_DIFFERENCE / 2.0 { + return false; + } + added.push((page, any, solid)); } - let Some((any, solid, _)) = differing(&x, &y, on_page, scale) else { return false }; - let pixels = (page.width() * scale * page.height() * scale).max(1.0); - let (any, solid) = (spent.any + any as f64 / pixels, spent.solid + solid as f64 / pixels); - if any > MAX_DIFFERENCE / 2.0 || solid > MAX_SOLID_DIFFERENCE / 2.0 { - return false; + for (page, any, solid) in added { + spent.set(page, any, solid); } - *spent = Spent { any, solid }; true } @@ -1161,7 +1196,7 @@ mod tests { #[test] fn art_with_no_paint_draws_alike_and_faint_art_is_still_compared() { let shared = Shared { patterns: &[], images: &BTreeMap::new() }; - let alike = |a: &Node, b: &Node| drawn_alike(a, b, NodeId(999), &shared, None, &mut Spent::default()); + let alike = |a: &Node, b: &Node| drawn_alike(a, b, NodeId(999), &shared, &[], &mut Spent::default()); let (none, other_none) = (square_node(1, None), square_node(2, None)); assert!(alike(&none, &other_none)); let (faint, other_faint) = (square_node(1, Some(0.02)), square_node(2, Some(0.02))); @@ -1169,6 +1204,22 @@ mod tests { assert!(!alike(&none, &square_node(2, Some(1.0)))); } + /// Each page an object is on takes one of the import's comparisons with pages. With none left, + /// art on a page keeps its drawn art, and art on no page needs none. + #[test] + fn comparisons_with_pages_stop_at_their_limit() { + let shared = Shared { patterns: &[], images: &BTreeMap::new() }; + let (live, drawn) = (square_node(1, Some(1.0)), square_node(2, Some(1.0))); + let (page, other) = (Rect::new(0.0, 0.0, 100.0, 100.0), Rect::new(0.0, 0.0, 60.0, 60.0)); + let mut spent = Spent { compared: MAX_PAGE_COMPARISONS - 2, ..Default::default() }; + assert!(drawn_alike(&live, &drawn, NodeId(999), &shared, &[page, other], &mut spent)); + assert_eq!(spent.compared, MAX_PAGE_COMPARISONS); + assert!(!drawn_alike(&live, &drawn, NodeId(999), &shared, &[page], &mut spent)); + let off = Rect::new(500.0, 500.0, 600.0, 600.0); + assert!(drawn_alike(&live, &drawn, NodeId(999), &shared, &[off], &mut spent)); + assert_eq!(spent.compared, MAX_PAGE_COMPARISONS); + } + /// "AVA" at 40 points, at (10, 60). fn type_node(id: u64, style: CharStyle) -> Node { Node::new(NodeId(id), NodeKind::Text(Box::new(TextObject::point(Point::new(10.0, 60.0), "AVA", CharStyle { size: 40.0, ..style })))) diff --git a/crates/eps/src/import/ai/read.rs b/crates/eps/src/import/ai/read.rs index 66cdb82adc..3bb571df07 100644 --- a/crates/eps/src/import/ai/read.rs +++ b/crates/eps/src/import/ai/read.rs @@ -472,6 +472,10 @@ struct BrushesKept { bending: HashMap, } +/// What the pages the art may be compared with are worked out from: the move into the document, +/// the number of artboards, the first artboard and the art's bounding box. +type PageSources = (Option, usize, Option, Option); + struct Reader<'a> { lex: Lexer<'a>, stack: Vec, @@ -604,6 +608,8 @@ struct Reader<'a> { envelope_checks: usize, /// How far the live envelopes read so far draw from their drawn art ([`super::envelope::Spent`]). envelope_spent: super::envelope::Spent, + /// [`Self::pages_in_doc`] as last worked out, with what it was worked out from. + page_cache: Option<(PageSources, Arc<[Rect]>)>, /// How many more strokes with dashes aligned to corners this import may draw to check /// ([`super::MAX_DASH_CHECKS`]). dash_checks: usize, @@ -877,6 +883,7 @@ impl<'a> Reader<'a> { envelopes: Vec::new(), envelope_checks: super::envelope::MAX_CHECKED, envelope_spent: super::envelope::Spent::default(), + page_cache: None, dash_checks: super::MAX_DASH_CHECKS, defs_only: false, warped_type: Vec::new(), diff --git a/crates/eps/src/import/ai/read/containers.rs b/crates/eps/src/import/ai/read/containers.rs index 2a14ce1c24..ad97b7b47b 100644 --- a/crates/eps/src/import/ai/read/containers.rs +++ b/crates/eps/src/import/ai/read/containers.rs @@ -917,6 +917,7 @@ impl<'a> Reader<'a> { } let Some(p) = self.envelopes.pop() else { return }; let scale = self.space_scale(); + let pages = self.pages_in_doc(); let drawn = self.frames.last().filter(|f| f.children.len() == p.siblings + 1).and_then(|f| f.children.last()); let Some(drawn) = drawn else { self.warn(crate::import::ai::envelope::drawn_note(p.typed)); @@ -959,8 +960,7 @@ impl<'a> Reader<'a> { (shown.visible, shown.opacity) = (true, 1.0); let mut plain = drawn.clone(); (plain.visible, plain.opacity) = (true, 1.0); - let page = self.page_in_doc(); - let mut alike = crate::import::ai::envelope::drawn_alike(&shown, &plain, layer, &shared, page, &mut self.envelope_spent); + let mut alike = crate::import::ai::envelope::drawn_alike(&shown, &plain, layer, &shared, &pages, &mut self.envelope_spent); // In Edit Contents, a grid that draws the contents elsewhere than the drawn art is checked // again moved onto that art. if !alike @@ -969,7 +969,7 @@ impl<'a> Reader<'a> { { self.envelope_checks -= 1; (envelope.kind, shown.kind) = (kind.clone(), kind); - alike = crate::import::ai::envelope::drawn_alike(&shown, &plain, layer, &shared, page, &mut self.envelope_spent); + alike = crate::import::ai::envelope::drawn_alike(&shown, &plain, layer, &shared, &pages, &mut self.envelope_spent); } if !alike { self.warn(crate::import::ai::envelope::DRAWN); diff --git a/crates/eps/src/import/ai/read/document.rs b/crates/eps/src/import/ai/read/document.rs index 8492cca6f0..544c6725cc 100644 --- a/crates/eps/src/import/ai/read/document.rs +++ b/crates/eps/src/import/ai/read/document.rs @@ -55,6 +55,22 @@ impl AiArtboard { } } +/// The pages the art may be compared with in the document's space, art space being moved there by +/// `to_doc` (none before the art starts): each of `artboards` (else `first`, the first artboard), +/// then `art_box`, the art's bounding box. A page that is an earlier one to half a point is left out. +fn pages_of(to_doc: Option, artboards: &[AiArtboard], first: Option, art_box: Option) -> Vec { + let Some(to_doc) = to_doc else { return vec![] }; + let boards: Vec = if artboards.is_empty() { first.into_iter().collect() } else { artboards.iter().map(|b| b.rect).collect() }; + let mut pages: Vec = vec![]; + for page in boards.into_iter().chain(art_box).map(|r| to_doc.transform_rect_bbox(r)) { + let same = |p: &Rect| [p.x0 - page.x0, p.y0 - page.y0, p.x1 - page.x1, p.y1 - page.y1].iter().all(|d| d.abs() <= 0.5); + if !pages.iter().any(same) { + pages.push(page); + } + } + pages +} + /// The number of entry `key` in `o` when it is written as one finite number of the type `ty` /// (`/Int`, `/Real`, `/Bool`; a whole number unless `/Real`), else `None`. fn typed(o: &Obj, key: &str, ty: &str) -> Option { @@ -923,10 +939,21 @@ impl<'a> Reader<'a> { self.artboards.first().map(|b| b.rect).or(self.cropmarks).or_else(|| self.sized_board()) } - /// The page the art is compared with in the document's space, once the art has started. - pub(super) fn page_in_doc(&self) -> Option { - let board = self.first_board().or_else(|| self.bbox_board())?; - Some(self.to_doc?.transform_rect_bbox(board)) + /// Every page the art may be compared with, in the document's space, once the art has started: + /// each artboard (else the first artboard [`Self::first_board`] gives), since a page of a file's + /// PDF part can show any of them, and the art's bounding box ([`Self::bbox_board`]), which a page + /// cropped to the art and the page of the legacy format show ([`pages_of`]). Worked out again + /// only when the artboards, the art's box or the move into the document have changed. + pub(super) fn pages_in_doc(&mut self) -> Arc<[Rect]> { + let sources = (self.to_doc, self.artboards.len(), self.first_board(), self.bbox_board()); + if let Some((from, pages)) = &self.page_cache + && *from == sources + { + return pages.clone(); + } + let pages: Arc<[Rect]> = pages_of(sources.0, &self.artboards, sources.2, sources.3).into(); + self.page_cache = Some((sources, pages.clone())); + pages } /// The page of a file that says nothing of its artboard (the legacy format as other apps write @@ -1239,8 +1266,8 @@ impl<'a> Reader<'a> { } let to_doc = self.ensure_space(); let artboard = self.first_board(); - // Before the artboards are taken below: the page is the first artboard's. - let page = self.page_in_doc(); + // Before the artboards are taken below: the pages are the artboards' and the art's box. + let pages = self.pages_in_doc(); self.check_symbols(); self.order_symbols(); drop_commented_copies(&mut self.doc.layers, &self.hidden_ids); @@ -1447,7 +1474,7 @@ impl<'a> Reader<'a> { envelopes: self.typed_envelopes, checks: self.envelope_checks, spent: self.envelope_spent, - page, + pages: pages.to_vec(), alone: None, type_clips: self.type_clips, }, diff --git a/crates/eps/src/import/ai/read/ops.rs b/crates/eps/src/import/ai/read/ops.rs index fcdffd4580..362425ec72 100644 --- a/crates/eps/src/import/ai/read/ops.rs +++ b/crates/eps/src/import/ai/read/ops.rs @@ -787,7 +787,7 @@ impl<'a> Reader<'a> { } let space = self.space(); self.note_brushes_kept(); - let page = self.page_in_doc(); + let pages = self.pages_in_doc(); let Some(f) = self.frames.last_mut() else { return }; let n = f.children.len(); let (Some(object), Some(look)) = (n.checked_sub(1).and_then(|i| f.children.get(i)), n.checked_sub(2).and_then(|i| f.children.get(i))) else { @@ -873,7 +873,8 @@ impl<'a> Reader<'a> { let layer = self.doc.alloc_id(); let shared = crate::import::ai::envelope::Shared { patterns: &self.doc.patterns, images: &self.doc.images }; let shown = !hidden && object.visible && look.visible; - look_alike(object, &ap, look, &self.hidden_ids, hidden, layer, &shared, page.filter(|_| shown), &mut self.envelope_spent) + let on: &[Rect] = if shown { &pages } else { &[] }; + look_alike(object, &ap, look, &self.hidden_ids, hidden, layer, &shared, on, &mut self.envelope_spent) }; alike.then_some(ap) } @@ -885,7 +886,7 @@ impl<'a> Reader<'a> { let layer = self.doc.alloc_id(); let shared = crate::import::ai::envelope::Shared { patterns: &self.doc.patterns, images: &self.doc.images }; let mut spent = crate::import::ai::envelope::Spent::default(); - look_alike(object, &ap, look, &self.hidden_ids, hidden, layer, &shared, None, &mut spent) + look_alike(object, &ap, look, &self.hidden_ids, hidden, layer, &shared, &[], &mut spent) }; alike.then_some(ap) } @@ -1314,7 +1315,7 @@ fn look_alike( hidden: bool, layer: NodeId, shared: &crate::import::ai::envelope::Shared<'_>, - page: Option, + pages: &[Rect], spent: &mut crate::import::ai::envelope::Spent, ) -> bool { let mut live = object.clone(); @@ -1329,7 +1330,7 @@ fn look_alike( for n in [&mut live, &mut plain] { (n.visible, n.opacity, n.blend) = (true, 1.0, BlendMode::Normal); } - crate::import::ai::envelope::warped_alike(&live, &plain, layer, shared, page, spent) + crate::import::ai::envelope::warped_alike(&live, &plain, layer, shared, pages, spent) } /// Does `appearance` stroke with dashes aligned to corners and path ends? diff --git a/crates/eps/src/import/ai/tests.rs b/crates/eps/src/import/ai/tests.rs index 242d433b7b..9ea967b927 100644 --- a/crates/eps/src/import/ai/tests.rs +++ b/crates/eps/src/import/ai/tests.rs @@ -9395,24 +9395,35 @@ fn warped_type_gets_what_the_envelopes_left_of_the_budget() { let exact = read_ok(&layer(&envelope_of(&straight_mesh(1, 1, (120.0, 60.0)), ENVELOPE_KEYS, &envelope_content(), 2, &drawn_content(&flat)))); let beside = format!("{RED}{}f\n", rect_path(40.5, 20.0, 160.5, 80.0)); let shifted = read_ok(&layer(&envelope_of(&straight_mesh(1, 1, (120.0, 60.0)), ENVELOPE_KEYS, &envelope_content(), 2, &beside))); + let page = Rect::new(0.0, 0.0, 200.0, 100.0); for r in [&exact, &shifted] { assert_eq!(r.warped_type.checks, super::envelope::MAX_CHECKED - 1); - assert!(r.warped_type.page.is_some()); + assert_eq!(r.warped_type.pages, [page]); } live_envelope(&art(&shifted.doc)[0]); - assert!(shifted.warped_type.spent.any > exact.warped_type.spent.any); + assert!(shifted.warped_type.spent.on(page).0 > exact.warped_type.spent.on(page).0); } -/// In a file with an artboard list, warped type is held to the first artboard, not to the crop marks. +/// In a file with an artboard list, warped type is held to every artboard, not to the crop marks. #[test] -fn warped_type_is_held_to_the_first_artboard() { +fn warped_type_is_held_to_every_artboard() { let boards = "%_/Document :\n%_/Dictionary :\n%_/Array :\n\ %_/Dictionary :\n%_0 200 /RealPointRelToROrigin\n%_ (PositionPoint1) ,\n%_300 0 /RealPointRelToROrigin\n%_ (PositionPoint2) ,\n%_; ,\n\ %_/Dictionary :\n%_400 0 /RealPointRelToROrigin\n%_ (PositionPoint2) ,\n%_600 -150 /RealPointRelToROrigin\n%_ (PositionPoint1) ,\n%_; ,\n\ %_; (ArtboardArray) ,\n%_; /NotRecorded ,\n%_;\n"; let r = read_ok(&format!("{boards}{}", layer(&format!("{RED}{}f\n", rect_path(20.0, 20.0, 40.0, 40.0))))); assert_eq!(r.doc.artboards[0].rect, Rect::new(0.0, 0.0, 300.0, 200.0)); - assert_eq!(r.warped_type.page, Some(Rect::new(0.0, 0.0, 300.0, 200.0))); + assert_eq!(r.warped_type.pages, [Rect::new(0.0, 0.0, 300.0, 200.0), Rect::new(400.0, 200.0, 600.0, 350.0)]); +} + +/// Artboards that are an earlier one to half a point, and an art box that is one of them, add no +/// page of their own. +#[test] +fn pages_that_repeat_an_artboard_are_held_once() { + let boards = boards_at(&[[0.0, 0.0, 200.0, 100.0], [0.2, 0.0, 200.0, 100.3], [0.0, 0.0, 200.0, 100.0], [300.0, 0.0, 400.0, 100.0]]); + let data = format!("{boards}{}", layer(&format!("{RED}{}f\n", rect_path(20.0, 20.0, 40.0, 40.0)))); + let r = read(stream_with_header("%%HiResBoundingBox: 300 0 400 100.4\n", &data).as_bytes()).unwrap(); + assert_eq!(r.warped_type.pages, [Rect::new(0.0, 0.0, 200.0, 100.0), Rect::new(300.0, 0.0, 400.0, 100.0)]); } /// Type whose Warp is hidden draws no warp, so it stays type even with no check left. @@ -9454,17 +9465,39 @@ fn warped_type_on_the_page_spends_what_is_left_of_the_budget() { w.look = Node::group(vectorcraft_doc::NodeId(u64::MAX - 10 - i as u64), vec![Arc::new(w.look.clone()), mark.clone()]); } let copy = |w: &super::envelope::WarpedLook| super::envelope::WarpedLook { slot: w.slot, look: w.look.clone(), left_out: None }; - let page = Some(Rect::new(0.0, 60.0, 60.0, 100.0)); + let page = Rect::new(0.0, 60.0, 60.0, 100.0); let settle = |w: &super::envelope::WarpedLook, spent| { - let warped = super::envelope::WarpedType { looks: vec![copy(w)], checks: 1, spent, page, ..Default::default() }; + let warped = super::envelope::WarpedType { looks: vec![copy(w)], checks: 1, spent, pages: vec![page], ..Default::default() }; warped.settle(&mut doc.clone(), Some, &Default::default(), &mut vec![]) }; - let spent = || super::envelope::Spent { any: MAX_DIFFERENCE / 2.0, solid: MAX_SOLID_DIFFERENCE / 2.0 }; + let spent = || super::envelope::Spent { pages: vec![(page, MAX_DIFFERENCE / 2.0, MAX_SOLID_DIFFERENCE / 2.0)], ..Default::default() }; assert_eq!(settle(&looks[1], Default::default()), 0); assert_eq!(settle(&looks[1], spent()), 1); assert_eq!(settle(&looks[0], spent()), 0); } +/// Each page the art may be compared with has its own share of the budget: warped type on the +/// second page spends that page's share, whatever is left of the first page's. +#[test] +fn warped_type_on_a_later_page_spends_that_pages_share_of_the_budget() { + use crate::import::native::compare::{MAX_DIFFERENCE, MAX_SOLID_DIFFERENCE}; + let (doc, mut looks) = warped_type_doc(2); + // The second object shows. Its look draws a little more than it. + let mark = read_ok(&layer(&format!("{RED}{}f\n", rect_path(2.0, 2.0, 2.6, 2.6)))); + let mark = art(&mark.doc)[0].clone(); + let w = looks.remove(1); + let look = Node::group(vectorcraft_doc::NodeId(u64::MAX - 10), vec![Arc::new(w.look.clone()), mark]); + let (first, second) = (Rect::new(200.0, 0.0, 260.0, 40.0), Rect::new(0.0, 60.0, 60.0, 100.0)); + let settle = |spent| { + let looks = vec![super::envelope::WarpedLook { slot: w.slot, look: look.clone(), left_out: None }]; + let warped = super::envelope::WarpedType { looks, checks: 1, spent, pages: vec![first, second], ..Default::default() }; + warped.settle(&mut doc.clone(), Some, &Default::default(), &mut vec![]) + }; + let full = |page| super::envelope::Spent { pages: vec![(page, MAX_DIFFERENCE / 2.0, MAX_SOLID_DIFFERENCE / 2.0)], ..Default::default() }; + assert_eq!(settle(full(first)), 0); + assert_eq!(settle(full(second)), 1); +} + /// An envelope that comes out close to its drawn art, but with more of the page differing than the /// page check can take from the live envelopes (its drawn art 2 pt to the side on a 200 × 100 /// page), keeps the drawn art, so the file still opens from its layers. @@ -9479,6 +9512,58 @@ fn an_envelope_that_differs_from_its_drawn_art_over_the_page_keeps_it() { assert_eq!(r.warnings, [super::envelope::DRAWN]); } +/// The document data of artboards in art space, each `[x0, y0, x1, y1]`. +fn boards_at(boards: &[[f64; 4]]) -> String { + let boards: String = boards + .iter() + .map(|[x0, y0, x1, y1]| { + format!("%_/Dictionary :\n%_{x0} {y1} /RealPointRelToROrigin\n%_ (PositionPoint1) ,\n%_{x1} {y0} /RealPointRelToROrigin\n%_ (PositionPoint2) ,\n%_; ,\n") + }) + .collect(); + format!("%_/Document :\n%_/Dictionary :\n%_/Array :\n{boards}%_; (ArtboardArray) ,\n%_; /NotRecorded ,\n%_;\n") +} + +/// The envelope of [`an_envelope_that_differs_from_its_drawn_art_over_the_page_keeps_it`] on the +/// second artboard, the first a strip along the top that it doesn't reach: a page of the second +/// artboard can be compared with the layers, so it is held to that page and keeps its drawn art. +/// One that draws as its drawn art opens live there. +#[test] +fn an_envelope_on_a_later_artboard_that_differs_over_its_page_keeps_its_drawn_art() { + let boards = boards_at(&[[0.0, 98.0, 20.0, 100.0], [0.0, 0.0, 200.0, 100.0]]); + let shifted = format!("{RED}{}f\n", rect_path(42.0, 20.0, 162.0, 80.0)); + let body = envelope_of(&straight_mesh(1, 1, (120.0, 60.0)), ENVELOPE_KEYS, &envelope_content(), 2, &shifted); + let r = read_ok(&format!("{boards}{}", layer(&body))); + let a = art(&r.doc); + assert_eq!(a.len(), 1, "{a:#?}"); + assert!(matches!(a[0].kind, NodeKind::Group { .. }), "{a:#?}"); + assert_eq!(r.warnings, [super::envelope::DRAWN]); + assert_eq!(r.warped_type.pages, [Rect::new(0.0, 0.0, 20.0, 2.0), Rect::new(0.0, 0.0, 200.0, 100.0)]); + let flat = vectorcraft_doc::live::EnvelopeKind::Mesh { + rows: 1, + cols: 1, + points: vec![Point::new(40.0, 20.0), Point::new(160.0, 20.0), Point::new(40.0, 80.0), Point::new(160.0, 80.0)], + handles: vec![], + }; + let body = envelope_of(&straight_mesh(1, 1, (120.0, 60.0)), ENVELOPE_KEYS, &envelope_content(), 2, &drawn_content(&flat)); + let r = read_ok(&format!("{boards}{}", layer(&body))); + live_envelope(&art(&r.doc)[0]); +} + +/// The same envelope on no artboard, inside the art's bounding box: a page cropped to the art +/// shows it, so it is held to that page too. +#[test] +fn an_envelope_off_the_artboards_that_differs_over_the_arts_box_keeps_its_drawn_art() { + let shifted = format!("{RED}{}f\n", rect_path(42.0, 20.0, 162.0, 80.0)); + let body = envelope_of(&straight_mesh(1, 1, (120.0, 60.0)), ENVELOPE_KEYS, &envelope_content(), 2, &shifted); + let data = format!("{}{}", boards_at(&[[0.0, 98.0, 20.0, 100.0]]), layer(&body)); + let r = read(stream_with_header("%%HiResBoundingBox: 0 0 200 100\n", &data).as_bytes()).unwrap(); + let a = art(&r.doc); + assert_eq!(a.len(), 1, "{a:#?}"); + assert!(matches!(a[0].kind, NodeKind::Group { .. }), "{a:#?}"); + assert_eq!(r.warnings, [super::envelope::DRAWN]); + assert_eq!(r.warped_type.pages, [Rect::new(0.0, 0.0, 20.0, 2.0), Rect::new(0.0, 0.0, 200.0, 100.0)]); +} + /// An envelope whose drawn art holds an object the reader can't read keeps that drawn art, which /// the page's art replaces. #[test]