From 95f11a4f1c3eed00b1561dac087f5d0d7adddc38 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Sun, 30 Aug 2026 14:05:00 +0700 Subject: [PATCH] fix: preserve parsed picture rotation Signed-off-by: Yonghye Kwon --- docx-core/src/reader/pic.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docx-core/src/reader/pic.rs b/docx-core/src/reader/pic.rs index c0c60a2c2..55bd123ae 100644 --- a/docx-core/src/reader/pic.rs +++ b/docx-core/src/reader/pic.rs @@ -24,6 +24,19 @@ impl ElementReader for Pic { pic = pic.id(id) } } + AXMLElement::Xfrm => { + if let Some(rotation) = read(&attributes, "rot") { + if let Ok(rotation) = i64::from_str(&rotation) { + const UNITS_PER_DEGREE: i64 = 60_000; + const FULL_TURN_UNITS: i64 = 360 * UNITS_PER_DEGREE; + let normalized = rotation.rem_euclid(FULL_TURN_UNITS); + let degrees = ((normalized + UNITS_PER_DEGREE / 2) + / UNITS_PER_DEGREE) + % 360; + pic = pic.rotate(degrees as u16); + } + } + } AXMLElement::Off => { let mut offset_x: i32 = 0; let mut offset_y: i32 = 0; @@ -70,3 +83,26 @@ impl ElementReader for Pic { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_picture_rotation_from_transform() { + let xml = r#" + + + + + + + + "#; + let mut reader = EventReader::new(xml.as_bytes()); + + let picture = Pic::read(&mut reader, &[]).expect("picture should parse"); + + assert_eq!(picture.rot, 349); + } +}