From 10134b6f29c3ca3d0c69d4f25ec02bf9f12f12b1 Mon Sep 17 00:00:00 2001 From: moppeeer Date: Tue, 20 Oct 2026 16:49:15 +0200 Subject: [PATCH] PR-1084: trace outlines for graphic and nested wraps, and again after a relink Co-Authored-By: Claude Opus 5.5 --- crates/doc/src/lib.rs | 22 +++++++- crates/engine/src/cmd/alpha_wrap.rs | 84 ++++++++++++++++++++++++----- 2 files changed, 92 insertions(+), 14 deletions(-) diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index 2c4ecb52c..a5fec91c3 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -604,7 +604,7 @@ impl PdfCrop { /// The outline of a raster image's visible pixels (Text Wrap › Contour › Alpha Channel), in the /// image's pixel space (0..width, 0..height). Derived from the image, so not saved: the engine /// traces it again when a document opens. -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug)] pub struct AlphaOutline { pub asset: AssetId, pub threshold: u8, @@ -614,6 +614,26 @@ pub struct AlphaOutline { pub outer: designcraft_geom::PathData, /// The boundaries of transparent holes those areas enclose (wound the other way). pub holes: designcraft_geom::PathData, + /// The image data it was traced from: a relink or link update replaces the asset's data + /// under the same id, and the outline is traced again. (Weak, so it doesn't keep old image + /// data alive, and its address can't be reused by new data while it's held.) + pub source: std::sync::Weak>, +} + +impl AlphaOutline { + /// Traced from `data`, the asset's current image data. + pub fn traced_from(&self, data: &Arc>) -> bool { + std::ptr::eq(self.source.as_ptr(), Arc::as_ptr(data)) + } +} + +impl PartialEq for AlphaOutline { + fn eq(&self, other: &Self) -> bool { + (self.asset, self.threshold, self.width, self.height) == (other.asset, other.threshold, other.width, other.height) + && self.outer == other.outer + && self.holes == other.holes + && self.source.ptr_eq(&other.source) + } } impl Asset { diff --git a/crates/engine/src/cmd/alpha_wrap.rs b/crates/engine/src/cmd/alpha_wrap.rs index ea94aaf9f..717ae8ac5 100644 --- a/crates/engine/src/cmd/alpha_wrap.rs +++ b/crates/engine/src/cmd/alpha_wrap.rs @@ -20,20 +20,25 @@ const MAX_EDGES: usize = 1 << 16; pub(crate) fn sync(doc: &mut Document) { let mut wanted: Vec<(AssetId, u8)> = Vec::new(); for sp in doc.spreads.iter().chain(doc.parents.iter()) { - for it in &sp.items { - if it.wrap.mode == WrapMode::Contour - && it.wrap.contour == ContourType::AlphaChannel - && let Some(g) = it.graphic() - && !wanted.contains(&(g.asset, it.wrap.alpha_threshold)) - { - wanted.push((g.asset, it.wrap.alpha_threshold)); - } + // Every frame, in groups and Paste Into containers too, with the wrap set on the frame or + // on its placed graphic (Direct Selection sets the graphic's). + for top in &sp.items { + top.walk(&mut |it| { + let Some(g) = it.graphic() else { return }; + for w in [&it.wrap, &g.wrap] { + if w.mode == WrapMode::Contour && w.contour == ContourType::AlphaChannel && !wanted.contains(&(g.asset, w.alpha_threshold)) { + wanted.push((g.asset, w.alpha_threshold)); + } + } + }); } } if wanted.is_empty() && doc.alpha_outlines.is_empty() { return; } - doc.alpha_outlines.retain(|o| wanted.contains(&(o.asset, o.threshold)) && doc.assets.contains_key(&o.asset)); + // An outline traced from data the asset no longer has (relinked or updated) is traced again. + let assets = &doc.assets; + doc.alpha_outlines.retain(|o| wanted.contains(&(o.asset, o.threshold)) && assets.get(&o.asset).is_some_and(|a| o.traced_from(&a.data))); for (asset, threshold) in wanted { if doc.alpha_outline(asset, threshold).is_some() { continue; @@ -47,7 +52,7 @@ pub(crate) fn sync(doc: &mut Document) { /// The outline of the pixels of `data` more opaque than `threshold`. None for an image that /// can't be read, is too large, or has no transparency (its contour is its rectangle). -pub(crate) fn trace(data: &[u8], asset: AssetId, threshold: u8) -> Option { +pub(crate) fn trace(data: &Arc>, asset: AssetId, threshold: u8) -> Option { let (w, h) = designcraft_images::pixel_size(data)?; if w == 0 || h == 0 || u64::from(w) * u64::from(h) > MAX_PIXELS { return None; @@ -78,7 +83,8 @@ pub(crate) fn trace(data: &[u8], asset: AssetId, threshold: u8) -> Option 1, "the frame is nested"); + assert_eq!(d.alpha_outlines.len(), 1, "a nested frame keeps its outline"); + // A frame placed and grouped before its wrap is set: traced too. + let second = place(&mut s, 400); + let other2 = s.execute("frame.create", &json!({"rect": [600, 100, 650, 150]})).unwrap()["id"].as_u64().unwrap(); + s.execute("object.group", &json!({"ids": [second, other2]})).unwrap(); + s.execute("object.textWrap", &json!({"ids": [second], "mode": "contour", "contour": "alphaChannel", "threshold": 200})).unwrap(); + assert_eq!(s.doc().unwrap().doc.alpha_outlines.len(), 2); + // The image data replaced under the same asset, as a relink does: traced again. + let Some(designcraft_doc::Content::Graphic(g)) = s.doc().unwrap().doc.item(designcraft_doc::ItemId(image)).map(|it| it.content.clone()) + else { + panic!("a graphic") + }; + let narrower = Arc::new(png(|x, _| (24..40).contains(&x))); + s.edit(|d, _| { + let a = d.assets.get(&g.asset).unwrap(); + let mut a = (**a).clone(); + a.data = narrower.clone(); + d.assets.insert(g.asset, Arc::new(a)); + Ok(()) + }) + .unwrap(); + let d = &s.doc().unwrap().doc; + let o = d.alpha_outline(g.asset, designcraft_doc::DEFAULT_ALPHA_THRESHOLD).unwrap(); + assert!(o.traced_from(&narrower)); + let b = o.outer.bounds().unwrap(); + assert_eq!((b.x0, b.x1), (24.0, 40.0), "the new image's outline"); + } + /// An image placed 128 pt wide at (100, 100) (2 pt per pixel), its wrap a contour with no /// offset, and a caption frame at `caption` with 8 pt "Hi". fn page(data: &[u8], caption: [f64; 4]) -> (Session, Value, u64) {