diff --git a/docx-core/src/documents/elements/drawing.rs b/docx-core/src/documents/elements/drawing.rs index b0eedc2..665b9da 100644 --- a/docx-core/src/documents/elements/drawing.rs +++ b/docx-core/src/documents/elements/drawing.rs @@ -89,6 +89,15 @@ impl BuildXML for Box { ) .open_position_h(&format!("{}", p.relative_from_h)); + match p.position_h { + DrawingPosition::Offset(x) => { + let x = format!("{}", x as u32); + b = b.pos_offset(&x).close(); + } + DrawingPosition::Align(x) => { + b = b.align(&x.to_string()).close(); + } + } if let DrawingPosition::Offset(x) = p.position_h { let x = format!("{}", x as u32); b = b.pos_offset(&x).close(); @@ -108,8 +117,13 @@ impl BuildXML for Box { // Please see 20.4.2.7 extent (Drawing Object Size) // One inch equates to 914400 EMUs and a centimeter is 360000 .wp_extent(&w, &h) - .wp_effect_extent("0", "0", "0", "0") - .wrap_none() + .wp_effect_extent("0", "0", "0", "0"); + if p.allow_overlap { + b = b.wrap_none(); + } else if p.position_type == DrawingPositionType::Anchor { + b = b.wrap_square("bothSides"); + } + b = b .wp_doc_pr("1", "Figure") .open_wp_c_nv_graphic_frame_pr() .a_graphic_frame_locks( @@ -151,6 +165,55 @@ mod tests { assert_eq!( str::from_utf8(&d).unwrap(), r#" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +"# + ); + } + + #[test] + fn test_drawing_build_with_pic_overlap() { + use std::io::Read; + + let mut img = std::fs::File::open("../images/cat_min.jpg").unwrap(); + let mut buf = Vec::new(); + let _ = img.read_to_end(&mut buf).unwrap(); + let d = Box::new(Drawing::new().pic(Pic::new(&buf).overlapping())).build(); + assert_eq!( + str::from_utf8(&d).unwrap(), + r#" @@ -186,6 +249,67 @@ mod tests { +"# + ); + } + + #[test] + fn test_drawing_build_with_pic_align_right() { + use std::io::Read; + + let mut img = std::fs::File::open("../images/cat_min.jpg").unwrap(); + let mut buf = Vec::new(); + let _ = img.read_to_end(&mut buf).unwrap(); + let mut pic = Pic::new(&buf).floating(); + pic = pic.relative_from_h(RelativeFromHType::Column); + pic = pic.relative_from_v(RelativeFromVType::Paragraph); + pic = pic.position_h(DrawingPosition::Align(PicAlign::Right)); + let d = Box::new(Drawing::new().pic(pic)).build(); + assert_eq!( + str::from_utf8(&d).unwrap(), + r#" + + + + right + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + "# ); } diff --git a/docx-core/src/documents/elements/pic.rs b/docx-core/src/documents/elements/pic.rs index cae6f9d..1397947 100644 --- a/docx-core/src/documents/elements/pic.rs +++ b/docx-core/src/documents/elements/pic.rs @@ -126,6 +126,11 @@ impl Pic { self } + pub fn overlapping(mut self) -> Pic { + self.allow_overlap = true; + self + } + pub fn offset_x(mut self, x: i32) -> Pic { self.position_h = DrawingPosition::Offset(x); self diff --git a/docx-core/src/types/drawing_position.rs b/docx-core/src/types/drawing_position.rs index 7088736..298ed6b 100644 --- a/docx-core/src/types/drawing_position.rs +++ b/docx-core/src/types/drawing_position.rs @@ -1,4 +1,5 @@ use serde::Serialize; +use std::fmt; use wasm_bindgen::prelude::*; @@ -18,10 +19,23 @@ pub enum DrawingPositionType { pub enum PicAlign { Left, Right, + Center, Bottom, Top, } +impl fmt::Display for PicAlign { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + PicAlign::Left => write!(f, "left"), + PicAlign::Right => write!(f, "right"), + PicAlign::Center => write!(f, "center"), + PicAlign::Bottom => write!(f, "bottom"), + PicAlign::Top => write!(f, "top"), + } + } +} + #[derive(Debug, Clone, Copy, Serialize, PartialEq, ts_rs::TS)] #[ts(export)] #[serde(rename_all = "camelCase")] diff --git a/docx-core/src/xml_builder/drawing.rs b/docx-core/src/xml_builder/drawing.rs index 6057f8b..e6476c0 100644 --- a/docx-core/src/xml_builder/drawing.rs +++ b/docx-core/src/xml_builder/drawing.rs @@ -43,5 +43,7 @@ impl XMLBuilder { open!(open_position_h, "wp:positionH", "relativeFrom"); open!(open_position_v, "wp:positionV", "relativeFrom"); closed_with_child!(pos_offset, "wp:posOffset"); + closed_with_child!(align, "wp:align"); closed!(wrap_none, "wp:wrapNone"); + closed!(wrap_square, "wp:wrapSquare", "wrapText"); } diff --git a/docx-wasm/js/json/bindings/PicAlign.ts b/docx-wasm/js/json/bindings/PicAlign.ts index 39b141e..c82a21b 100644 --- a/docx-wasm/js/json/bindings/PicAlign.ts +++ b/docx-wasm/js/json/bindings/PicAlign.ts @@ -1,2 +1,2 @@ -export type PicAlign = "left" | "right" | "bottom" | "top"; \ No newline at end of file +export type PicAlign = "left" | "right" | "center" | "bottom" | "top"; \ No newline at end of file diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index 753aee1..4aab990 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -152363,7 +152363,6 @@ exports[`writer should write inline image 2`] = ` - @@ -152417,7 +152416,6 @@ exports[`writer should write inline jpeg image 2`] = ` - @@ -152471,7 +152469,6 @@ exports[`writer should write jpeg image with del 2`] = ` - @@ -152525,7 +152522,6 @@ exports[`writer should write jpeg image with ins 2`] = ` -