Skip to content

Commit 776f76e

Browse files
authored
fix: floating image (#710)
* fix: floating image * fix * fix
1 parent dcc524e commit 776f76e

File tree

7 files changed

+1669
-1673
lines changed

7 files changed

+1669
-1673
lines changed

docx-core/examples/image_floating.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub fn main() -> Result<(), DocxError> {
1515
.floating()
1616
.offset_x(300 * 9525)
1717
.offset_y(400 * 9525);
18+
1819
Docx::new()
1920
.add_paragraph(Paragraph::new().add_run(Run::new().add_image(pic)))
2021
.build()

docx-core/src/documents/elements/drawing.rs

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,17 @@ impl BuildXML for Drawing {
104104
b = b.align(&x.to_string()).close();
105105
}
106106
}
107-
if let DrawingPosition::Offset(x) = p.position_h {
108-
let x = format!("{}", x as u32);
109-
b = b.pos_offset(&x).close();
110-
}
111107

112108
b = b.open_position_v(&format!("{}", p.relative_from_v));
113109

114-
if let DrawingPosition::Offset(y) = p.position_v {
115-
let y = format!("{}", y as u32);
116-
b = b.pos_offset(&y).close();
110+
match p.position_v {
111+
DrawingPosition::Offset(y) => {
112+
let y = format!("{}", y as u32);
113+
b = b.pos_offset(&y).close();
114+
}
115+
DrawingPosition::Align(a) => {
116+
b = b.align(&a.to_string()).close();
117+
}
117118
}
118119
}
119120

@@ -316,6 +317,69 @@ mod tests {
316317
</pic:pic></a:graphicData>
317318
</a:graphic>
318319
</wp:anchor>
320+
</w:drawing>"#
321+
);
322+
}
323+
324+
#[test]
325+
fn test_issue686() {
326+
use std::io::Read;
327+
328+
let mut img = std::fs::File::open("../images/cat_min.jpg").unwrap();
329+
let mut buf = Vec::new();
330+
let _ = img.read_to_end(&mut buf).unwrap();
331+
let pic = Pic::new(&buf)
332+
.size(320 * 9525, 240 * 9525)
333+
.floating()
334+
.offset_x(300 * 9525)
335+
.offset_y(400 * 9525);
336+
337+
let d = Drawing::new().pic(pic).build();
338+
assert_eq!(
339+
str::from_utf8(&d).unwrap(),
340+
r#"<w:drawing>
341+
<wp:anchor distT="0" distB="0" distL="0" distR="0" simplePos="0" allowOverlap="0" behindDoc="0" locked="0" layoutInCell="0" relativeHeight="190500">
342+
<wp:simplePos x="0" y="0" />
343+
<wp:positionH relativeFrom="margin">
344+
<wp:posOffset>2857500</wp:posOffset>
345+
</wp:positionH>
346+
<wp:positionV relativeFrom="margin">
347+
<wp:posOffset>3810000</wp:posOffset>
348+
</wp:positionV>
349+
<wp:extent cx="3048000" cy="2286000" />
350+
<wp:effectExtent b="0" l="0" r="0" t="0" />
351+
<wp:wrapSquare wrapText="bothSides" />
352+
<wp:docPr id="1" name="Figure" />
353+
<wp:cNvGraphicFramePr>
354+
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" />
355+
</wp:cNvGraphicFramePr>
356+
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
357+
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
358+
<pic:nvPicPr>
359+
<pic:cNvPr id="0" name="" />
360+
<pic:cNvPicPr>
361+
<a:picLocks noChangeAspect="1" noChangeArrowheads="1" />
362+
</pic:cNvPicPr>
363+
</pic:nvPicPr>
364+
<pic:blipFill>
365+
<a:blip r:embed="rIdImage123" />
366+
<a:srcRect />
367+
<a:stretch>
368+
<a:fillRect />
369+
</a:stretch>
370+
</pic:blipFill>
371+
<pic:spPr bwMode="auto">
372+
<a:xfrm rot="0">
373+
<a:off x="0" y="0" />
374+
<a:ext cx="3048000" cy="2286000" />
375+
</a:xfrm>
376+
<a:prstGeom prst="rect">
377+
<a:avLst />
378+
</a:prstGeom>
379+
</pic:spPr>
380+
</pic:pic></a:graphicData>
381+
</a:graphic>
382+
</wp:anchor>
319383
</w:drawing>"#
320384
);
321385
}

docx-wasm/package.json

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,31 @@
2424
},
2525
"resolutions": {
2626
"**/serialize-javascript": "6.0.1"
27-
}
27+
},
28+
"devDependencies": {
29+
"@types/file-saver": "2.0.5",
30+
"@wasm-tool/wasm-pack-plugin": "1.6.0",
31+
"adm-zip": "0.5.10",
32+
"cpy-cli": "4.2.0",
33+
"file-saver": "2.0.5",
34+
"html-webpack-plugin": "5.5.3",
35+
"jest": "28.1.3",
36+
"npm-run-all": "4.1.5",
37+
"text-encoding": "0.7.0",
38+
"ts-loader": "9.4.2",
39+
"typescript": "4.9.3",
40+
"webpack": "4.46.0",
41+
"webpack-cli": "5.0.1",
42+
"webpack-dev-server": "3.11.3",
43+
"webpack-merge": "5.8.0"
44+
},
45+
"files": [
46+
"dist",
47+
"dist/web/pkg",
48+
"dist/node/pkg",
49+
"js/*"
50+
],
51+
"module": "dist/web/index.js",
52+
"types": "dist/web/index.d.ts",
53+
"dependencies": {}
2854
}

docx-wasm/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"noEmitOnError": true,
1111
"strictNullChecks": true,
1212
"target": "es6",
13-
"sourceMap": true,
13+
"lib": ["ES2017", "DOM"],
1414
"esModuleInterop": true,
1515
"outDir": "dist/web",
1616
"declaration": true

docx-wasm/tsconfig.node.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"noEmitOnError": true,
1111
"strictNullChecks": true,
1212
"target": "es6",
13+
"lib": ["ES2017", "DOM"],
1314
"sourceMap": true,
1415
"esModuleInterop": true,
1516
"outDir": "dist/node",

docx-wasm/tsconfig.web.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"noEmitOnError": true,
1111
"strictNullChecks": true,
1212
"target": "es6",
13+
"lib": ["ES2017", "DOM"],
1314
"sourceMap": true,
1415
"esModuleInterop": true,
1516
"outDir": "dist/web",

0 commit comments

Comments
 (0)