From cae632a2f3d82fc3cd84327cb293afc107fca628 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 27 Sep 2022 13:49:15 +0900 Subject: [PATCH] feat: support rot (#540) --- docx-core/examples/image_inline_rotate.rs | 20 +++++++++++++++++++ docx-core/src/documents/elements/drawing.rs | 2 +- docx-core/src/documents/elements/pic.rs | 14 +++++++++++-- docx-core/src/xml_builder/pic.rs | 1 + docx-wasm/js/image.ts | 6 ++++++ docx-wasm/js/index.ts | 3 +++ docx-wasm/js/json/bindings/Pic.ts | 2 +- docx-wasm/src/pic.rs | 6 ++++++ .../test/__snapshots__/index.test.js.snap | 10 ++++++---- 9 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 docx-core/examples/image_inline_rotate.rs diff --git a/docx-core/examples/image_inline_rotate.rs b/docx-core/examples/image_inline_rotate.rs new file mode 100644 index 0000000..a855271 --- /dev/null +++ b/docx-core/examples/image_inline_rotate.rs @@ -0,0 +1,20 @@ +use std::fs::*; +use std::io::Read; + +use docx_rs::*; + +pub fn main() -> Result<(), DocxError> { + let path = std::path::Path::new("./output/examples/image_inline_rotate.docx"); + let file = File::create(&path).unwrap(); + let mut img = File::open("./images/cat_min.jpg").unwrap(); + let mut buf = Vec::new(); + let _ = img.read_to_end(&mut buf).unwrap(); + + // rotate 180deg. + let pic = Pic::new(&buf).size(320 * 9525, 240 * 9525).rotate(180); + Docx::new() + .add_paragraph(Paragraph::new().add_run(Run::new().add_text("🐱").add_image(pic))) + .build() + .pack(file)?; + Ok(()) +} diff --git a/docx-core/src/documents/elements/drawing.rs b/docx-core/src/documents/elements/drawing.rs index be93745..b0eedc2 100644 --- a/docx-core/src/documents/elements/drawing.rs +++ b/docx-core/src/documents/elements/drawing.rs @@ -175,7 +175,7 @@ mod tests { - + diff --git a/docx-core/src/documents/elements/pic.rs b/docx-core/src/documents/elements/pic.rs index f31ef13..cae6f9d 100644 --- a/docx-core/src/documents/elements/pic.rs +++ b/docx-core/src/documents/elements/pic.rs @@ -44,6 +44,8 @@ pub struct Pic { pub dist_b: i32, pub dist_l: i32, pub dist_r: i32, + // deg + pub rot: u16, } impl Pic { @@ -74,6 +76,7 @@ impl Pic { dist_b: 0, dist_l: 0, dist_r: 0, + rot: 0, } } @@ -97,6 +100,7 @@ impl Pic { dist_b: 0, dist_l: 0, dist_r: 0, + rot: 0, } } @@ -111,6 +115,12 @@ impl Pic { self } + // unit is deg + pub fn rotate(mut self, deg: u16) -> Pic { + self.rot = deg; + self + } + pub fn floating(mut self) -> Pic { self.position_type = DrawingPositionType::Anchor; self @@ -197,7 +207,7 @@ impl BuildXML for Pic { .close() .close() .open_pic_sp_pr("auto") - .open_a_xfrm() + .open_a_xfrm_with_rot(&format!("{}", (self.rot as u32) * 60 * 1000)) .a_off("0", "0") .a_ext(&w, &h) .close() @@ -243,7 +253,7 @@ mod tests { - + diff --git a/docx-core/src/xml_builder/pic.rs b/docx-core/src/xml_builder/pic.rs index 2e5f8b2..3e3fbb5 100644 --- a/docx-core/src/xml_builder/pic.rs +++ b/docx-core/src/xml_builder/pic.rs @@ -21,6 +21,7 @@ impl XMLBuilder { open!(open_pic_sp_pr, "pic:spPr", "bwMode"); open!(open_a_xfrm, "a:xfrm"); + open!(open_a_xfrm_with_rot, "a:xfrm", "rot"); closed!(a_off, "a:off", "x", "y"); closed!(a_ext, "a:ext", "cx", "cy"); open!(open_a_prst_geom, "a:prstGeom", "prst"); diff --git a/docx-wasm/js/image.ts b/docx-wasm/js/image.ts index cc6d60e..351c3f5 100644 --- a/docx-wasm/js/image.ts +++ b/docx-wasm/js/image.ts @@ -5,6 +5,7 @@ export class Image { _floating: boolean = false; _offsetX = 0; _offsetY = 0; + rot = 0; constructor(data: Uint8Array) { this.data = data; @@ -16,6 +17,11 @@ export class Image { return this; }; + rotate = (deg: number) => { + this.rot = deg; + return this; + }; + floating = () => { this._floating = true; return this; diff --git a/docx-wasm/js/index.ts b/docx-wasm/js/index.ts index f0ab526..54933b5 100644 --- a/docx-wasm/js/index.ts +++ b/docx-wasm/js/index.ts @@ -264,6 +264,9 @@ export class Docx { if (child._offsetY != null) { pic = pic.offset_x(child._offsetY); } + if (child.rot != null) { + pic = pic.rotate(child.rot); + } run = run.add_image(pic); } }); diff --git a/docx-wasm/js/json/bindings/Pic.ts b/docx-wasm/js/json/bindings/Pic.ts index 3612e42..53dcd17 100644 --- a/docx-wasm/js/json/bindings/Pic.ts +++ b/docx-wasm/js/json/bindings/Pic.ts @@ -3,4 +3,4 @@ import type { DrawingPositionType } from "./DrawingPositionType"; import type { RelativeFromHType } from "./RelativeFromHType"; import type { RelativeFromVType } from "./RelativeFromVType"; -export interface Pic { id: string, image: Array, size: [number, number], positionType: DrawingPositionType, simplePos: boolean, simplePosX: number, simplePosY: number, layoutInCell: boolean, relativeHeight: number, allowOverlap: boolean, positionH: DrawingPosition, positionV: DrawingPosition, relativeFromH: RelativeFromHType, relativeFromV: RelativeFromVType, distT: number, distB: number, distL: number, distR: number, } \ No newline at end of file +export interface Pic { id: string, image: Array, size: [number, number], positionType: DrawingPositionType, simplePos: boolean, simplePosX: number, simplePosY: number, layoutInCell: boolean, relativeHeight: number, allowOverlap: boolean, positionH: DrawingPosition, positionV: DrawingPosition, relativeFromH: RelativeFromHType, relativeFromV: RelativeFromVType, distT: number, distB: number, distL: number, distR: number, rot: number, } \ No newline at end of file diff --git a/docx-wasm/src/pic.rs b/docx-wasm/src/pic.rs index 8433a2f..9460868 100644 --- a/docx-wasm/src/pic.rs +++ b/docx-wasm/src/pic.rs @@ -17,6 +17,12 @@ impl Pic { self } + // unit is deg + pub fn rotate(mut self, deg: u16) -> Self { + self.0 = self.0.rotate(deg); + self + } + pub fn floating(mut self) -> Pic { self.0 = self.0.floating(); self diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index 81be6a8..64fcb02 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -14027,6 +14027,7 @@ Object { "relativeFromH": "margin", "relativeFromV": "margin", "relativeHeight": 0, + "rot": 0, "simplePos": false, "simplePosX": 0, "simplePosY": 0, @@ -14096,6 +14097,7 @@ Object { "relativeFromH": "margin", "relativeFromV": "margin", "relativeHeight": 251658240, + "rot": 0, "simplePos": false, "simplePosX": 798195, "simplePosY": 2510155, @@ -129340,7 +129342,7 @@ exports[`writer should write inline image 2`] = ` - + @@ -129394,7 +129396,7 @@ exports[`writer should write inline jpeg image 2`] = ` - + @@ -129448,7 +129450,7 @@ exports[`writer should write jpeg image with del 2`] = ` - + @@ -129502,7 +129504,7 @@ exports[`writer should write jpeg image with ins 2`] = ` - +