diff --git a/crates/docx/src/read/story/vml.rs b/crates/docx/src/read/story/vml.rs
index 2460be72b2..4fd74b2c92 100644
--- a/crates/docx/src/read/story/vml.rs
+++ b/crates/docx/src/read/story/vml.rs
@@ -144,7 +144,9 @@ impl Reader<'_> {
// Brightness and contrast (Word's picture watermark "Washout" sets them, #1711).
let frac = |k: &str| img.attr(k).and_then(crate::units::vml_number);
let fx = craft_wordmodel::picfx::PicFx::from_vml(frac("gain").unwrap_or(1.0), frac("blacklevel").unwrap_or(0.0));
- return Some(InlineObject::Image { media, w, h, alt, float, crop: [0.0; 4], fx, ole: None });
+ let crop =
+ ["cropleft", "croptop", "cropright", "cropbottom"].map(|key| img.attr(key).and_then(vml_fraction).unwrap_or(0.0).clamp(0.0, 1.0));
+ return Some(InlineObject::Image { media, w, h, alt, float, crop, fx, ole: None });
}
// The shapetype the shape is drawn from (its `type`, ECMA-376 Part 4 VML), when written in
// the same picture: what the shape leaves out, it takes from there.
diff --git a/crates/docx/tests/it/picture_links.rs b/crates/docx/tests/it/picture_links.rs
index dc5f53e19c..34cf625e1b 100644
--- a/crates/docx/tests/it/picture_links.rs
+++ b/crates/docx/tests/it/picture_links.rs
@@ -125,3 +125,91 @@ fn a_linked_picture_is_saved_with_its_link_on_the_drawing_too() {
let edited = out.finish().unwrap().into_inner();
assert_eq!(picture_links(&craft_docx::read(&edited).unwrap())[0], Some("https://example.com/saved".to_string()));
}
+
+// Original synthetic VML package for #2912, reported by denstu473; no reference-app asset.
+#[test]
+fn a_vml_picture_keeps_its_crop_when_saved() {
+ fn image_crop(doc: &Document, original_png: &[u8]) -> [f32; 4] {
+ let paragraph = doc.body[0].as_para().expect("the synthetic picture's paragraph");
+ assert_eq!(paragraph.objects.len(), 1, "the VML picture must be decoded before testing crop");
+ let InlineObject::Image { media, w, h, alt, crop, fx, .. } = ¶graph.objects[0] else {
+ panic!("the synthetic object must be an image");
+ };
+ assert!((*w - 24.0).abs() < 0.001 && (*h - 12.0).abs() < 0.001, "picture size changed: {w} x {h}");
+ assert_eq!(alt, "cropped square");
+ assert_eq!(doc.media.get(media).expect("the picture's embedded media").as_slice(), original_png);
+ assert_eq!(*fx, craft_wordmodel::picfx::PicFx::new(40_000, 0, 0));
+ *crop
+ }
+ fn crops_near(actual: [f32; 4], expected: [f32; 4], tolerance: f32) -> bool {
+ actual.into_iter().zip(expected).all(|(a, b)| (a - b).abs() <= tolerance)
+ }
+
+ let original_png = png();
+ let mut lost = Vec::new();
+ // Crop slots are left, top, right, bottom. These distinct fractions are exactly binary.
+ let positive = [0.125, 0.0625, 0.25, 0.5];
+ for (label, attributes, expected, saved_rect) in [
+ ("default", "", [0.0; 4], None),
+ (
+ "report fixed fractions",
+ r#"cropleft="8192f" croptop="3277f" cropright="16384f" cropbottom="6554f""#,
+ [8192.0f32 / 65_536.0, 3277.0 / 65_536.0, 16384.0 / 65_536.0, 6554.0 / 65_536.0],
+ Some(r#""#),
+ ),
+ (
+ "fixed fractions",
+ r#"cropleft="8192f" croptop="4096f" cropright="16384f" cropbottom="32768f""#,
+ positive,
+ Some(r#""#),
+ ),
+ (
+ "decimal fractions",
+ r#"cropleft="0.125" croptop="0.0625" cropright="0.25" cropbottom="0.5""#,
+ positive,
+ Some(r#""#),
+ ),
+ (
+ "percentage fractions",
+ r#"cropleft="12.5%" croptop="6.25%" cropright="25%" cropbottom="50%""#,
+ positive,
+ Some(r#""#),
+ ),
+ (
+ "bounded fractions",
+ r#"cropleft="-25%" croptop="125%" cropright="not-a-fraction" cropbottom="NaN""#,
+ [0.0, 1.0, 0.0, 0.0],
+ Some(r#""#),
+ ),
+ ] {
+ // The local namespace keeps the existing package helper and every old fixture intact.
+ let body = format!(
+ r#""#
+ );
+ let doc = craft_docx::read(&package(&body)).unwrap();
+ let read_crop = image_crop(&doc, &original_png);
+ let saved = craft_docx::write(&doc).unwrap();
+ let xml = part(&saved, "word/document.xml");
+ let reopened = craft_docx::read(&saved).unwrap();
+ let reopened_crop = image_crop(&reopened, &original_png);
+
+ if let Some(saved_rect) = saved_rect {
+ if !crops_near(read_crop, expected, 0.000_001) {
+ lost.push(format!("{label}: read crop {read_crop:?}"));
+ }
+ if !xml.contains(saved_rect) {
+ lost.push(format!("{label}: saved srcRect"));
+ }
+ // DrawingML uses 100000 units per fraction; allow its rounding on a real re-read.
+ if !crops_near(reopened_crop, expected, 0.000_01) {
+ lost.push(format!("{label}: reopened crop {reopened_crop:?}"));
+ }
+ } else {
+ // The zero/default control must work before any positive-crop regression is reported.
+ assert_eq!(read_crop, [0.0; 4]);
+ assert!(!xml.contains("