From 8eae257cf336a0379029987acae5528051697d2f Mon Sep 17 00:00:00 2001 From: Rees Date: Fri, 20 Nov 2026 05:59:01 +0100 Subject: [PATCH] DOCX #2532: preserve imported picture soft edges --- crates/docx/src/read/story/drawing.rs | 12 ++++++++-- crates/docx/src/write/story/drawing.rs | 7 +++++- crates/docx/tests/it/drawing_payload.rs | 26 +++++++++++++++++++++ crates/wordmodel/src/picfx.rs | 30 +++++++++++++++++++------ 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/crates/docx/src/read/story/drawing.rs b/crates/docx/src/read/story/drawing.rs index 7e51ded2a..cbffa58fc 100644 --- a/crates/docx/src/read/story/drawing.rs +++ b/crates/docx/src/read/story/drawing.rs @@ -291,7 +291,8 @@ impl Reader<'_> { /// A picture: the first `a:blip` in `pic` and its crop. fn read_pic(&mut self, pic: &El, rels: &Rels, w: f32, h: f32, alt: String, mut float: Float) -> Option { let blip = pic.find("a:blip")?; - float.set_spin(xfrm_spin(pic.find("pic:spPr"))); + let props = pic.find("pic:spPr"); + float.set_spin(xfrm_spin(props)); let Some(embed) = blip.attr("r:embed") else { // A linked picture holds no copy: nothing to draw, and nothing is fetched. if let Some(link) = blip.attr("r:link") { @@ -311,7 +312,14 @@ impl Reader<'_> { } } } - Some(InlineObject::Image { media, w, h, alt, float, crop, fx: crate::units::blip_fx(blip), ole: None }) + let radius = props + .and_then(|e| e.child("a:effectLst")) + .and_then(|e| e.child("a:softEdge")) + .and_then(|e| e.attr("rad")) + .and_then(|value| value.parse().ok()) + .unwrap_or(0); + let fx = crate::units::blip_fx(blip).with_soft_edge_emu(radius); + Some(InlineObject::Image { media, w, h, alt, float, crop, fx, ole: None }) } /// A `wpg:wgp` group (`w` × `h`): its pictures, shapes and text boxes, nested groups diff --git a/crates/docx/src/write/story/drawing.rs b/crates/docx/src/write/story/drawing.rs index 01e2998d4..507e02b7c 100644 --- a/crates/docx/src/write/story/drawing.rs +++ b/crates/docx/src/write/story/drawing.rs @@ -39,7 +39,7 @@ impl Writer<'_> { // Brightness, contrast and transparency (#1711, #1766) as `a:blip` effects (ECMA-376 // §20.1.8.13 CT_Blip: an unbounded choice of effects, in any order). let fx = fx.clamped(); - if fx.is_identity() { + if !fx.changes_pixels() && fx.transparency == 0 { w.empty("a:blip", &[("r:embed", &rid)]); } else { w.open("a:blip", &[("r:embed", &rid)]); @@ -64,6 +64,11 @@ impl Writer<'_> { w.open("a:prstGeom", &[("prst", "rect")]); w.empty("a:avLst", &[]); w.close("a:prstGeom"); + if let Some(radius) = fx.soft_edge_emu { + w.open("a:effectLst", &[]); + w.empty("a:softEdge", &[("rad", &n(i64::from(radius)))]); + w.close("a:effectLst"); + } w.close("pic:spPr"); w.close("pic:pic"); } diff --git a/crates/docx/tests/it/drawing_payload.rs b/crates/docx/tests/it/drawing_payload.rs index 5b241b1d7..04a317147 100644 --- a/crates/docx/tests/it/drawing_payload.rs +++ b/crates/docx/tests/it/drawing_payload.rs @@ -158,3 +158,29 @@ fn own_picture_fill_keeps_image() { assert_eq!((float.wrap, float.x, float.y), (Wrap::InFrontOfText, 72.0, 100.0)); assert_eq!(doc.media[media].as_slice(), BLUE_PNG); } + +#[test] +fn nested_picture_soft_edges_survive_repeated_saves() { + use std::io::Read; + + for (input_radius, expected_radius) in [(76_200, Some(76_200)), (0, None), (-1, None), (i64::MAX, Some(1_270_000))] { + let effect = format!(r#""#); + let mut bytes = package(&INLINE_XML.replace("", &effect)); + for _ in 0..2 { + let doc = craft_docx::read(&bytes).unwrap(); + check_box(&doc); + bytes = craft_docx::write(&doc).unwrap(); + let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); + let mut xml = String::new(); + zip.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + // The text box's DrawingML and VML fallback can both carry the same picture. + let mut effects = xml.split(", } impl PicFx { /// Corrections clamped to their ranges. pub fn new(bright: i32, contrast: i32, transparency: i32) -> PicFx { - PicFx { bright: bright.clamp(-FX_MAX, FX_MAX), contrast: contrast.clamp(-FX_MAX, FX_MAX), transparency: transparency.clamp(0, FX_MAX) } + PicFx { + bright: bright.clamp(-FX_MAX, FX_MAX), + contrast: contrast.clamp(-FX_MAX, FX_MAX), + transparency: transparency.clamp(0, FX_MAX), + soft_edge_emu: None, + } } /// The same corrections, each clamped to its range (for values that came from anywhere). pub fn clamped(self) -> PicFx { - PicFx::new(self.bright, self.contrast, self.transparency) + PicFx::new(self.bright, self.contrast, self.transparency).with_soft_edge_emu(self.soft_edge_emu.map_or(0, i64::from)) + } + + /// Sets and bounds the picture's soft-edge radius without changing its other effects. + pub fn with_soft_edge_emu(mut self, radius: i64) -> Self { + self.soft_edge_emu = u32::try_from(radius.clamp(0, MAX_SOFT_EDGE_EMU)).ok().filter(|&value| value != 0); + self } /// No correction at all. @@ -174,7 +190,7 @@ mod tests { #[test] fn hostile_values_clamp() { for v in [i32::MIN, -100_001, 0, 100_001, i32::MAX] { - let fx = PicFx { bright: v, contrast: v, transparency: v }; + let fx = PicFx { bright: v, contrast: v, transparency: v, ..Default::default() }; let _ = fx.table(); assert!((0.0..=1.0).contains(&fx.opacity())); let mut px = [1u8, 2, 3, 4];