feat: support rot (#540)

main
bokuweb 2022-09-27 13:49:15 +09:00 committed by GitHub
parent 2717ec17dc
commit cae632a2f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 56 additions and 8 deletions

View File

@ -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(())
}

View File

@ -175,7 +175,7 @@ mod tests {
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode="auto">
<a:xfrm>
<a:xfrm rot="0">
<a:off x="0" y="0" />
<a:ext cx="3048000" cy="2286000" />
</a:xfrm>

View File

@ -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 {
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode="auto">
<a:xfrm>
<a:xfrm rot="0">
<a:off x="0" y="0" />
<a:ext cx="3048000" cy="2286000" />
</a:xfrm>

View File

@ -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");

View File

@ -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;

View File

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

View File

@ -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<number>, 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, }
export interface Pic { id: string, image: Array<number>, 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, }

View File

@ -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

View File

@ -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`] = `
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode=\\"auto\\">
<a:xfrm>
<a:xfrm rot=\\"0\\">
<a:off x=\\"0\\" y=\\"0\\" />
<a:ext cx=\\"3048000\\" cy=\\"2286000\\" />
</a:xfrm>
@ -129394,7 +129396,7 @@ exports[`writer should write inline jpeg image 2`] = `
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode=\\"auto\\">
<a:xfrm>
<a:xfrm rot=\\"0\\">
<a:off x=\\"0\\" y=\\"0\\" />
<a:ext cx=\\"3048000\\" cy=\\"2286000\\" />
</a:xfrm>
@ -129448,7 +129450,7 @@ exports[`writer should write jpeg image with del 2`] = `
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode=\\"auto\\">
<a:xfrm>
<a:xfrm rot=\\"0\\">
<a:off x=\\"0\\" y=\\"0\\" />
<a:ext cx=\\"3048000\\" cy=\\"2286000\\" />
</a:xfrm>
@ -129502,7 +129504,7 @@ exports[`writer should write jpeg image with ins 2`] = `
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode=\\"auto\\">
<a:xfrm>
<a:xfrm rot=\\"0\\">
<a:off x=\\"0\\" y=\\"0\\" />
<a:ext cx=\\"3048000\\" cy=\\"2286000\\" />
</a:xfrm>