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); + } +}