Skip to content

Commit

Permalink
fix: floating image (#710)
Browse files Browse the repository at this point in the history
* fix: floating image

* fix

* fix
  • Loading branch information
bokuweb authored Apr 26, 2024
1 parent dcc524e commit 776f76e
Show file tree
Hide file tree
Showing 7 changed files with 1,669 additions and 1,673 deletions.
1 change: 1 addition & 0 deletions docx-core/examples/image_floating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn main() -> Result<(), DocxError> {
.floating()
.offset_x(300 * 9525)
.offset_y(400 * 9525);

Docx::new()
.add_paragraph(Paragraph::new().add_run(Run::new().add_image(pic)))
.build()
Expand Down
78 changes: 71 additions & 7 deletions docx-core/src/documents/elements/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,17 @@ impl BuildXML for Drawing {
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();
}

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

if let DrawingPosition::Offset(y) = p.position_v {
let y = format!("{}", y as u32);
b = b.pos_offset(&y).close();
match p.position_v {
DrawingPosition::Offset(y) => {
let y = format!("{}", y as u32);
b = b.pos_offset(&y).close();
}
DrawingPosition::Align(a) => {
b = b.align(&a.to_string()).close();
}
}
}

Expand Down Expand Up @@ -316,6 +317,69 @@ mod tests {
</pic:pic></a:graphicData>
</a:graphic>
</wp:anchor>
</w:drawing>"#
);
}

#[test]
fn test_issue686() {
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 pic = Pic::new(&buf)
.size(320 * 9525, 240 * 9525)
.floating()
.offset_x(300 * 9525)
.offset_y(400 * 9525);

let d = Drawing::new().pic(pic).build();
assert_eq!(
str::from_utf8(&d).unwrap(),
r#"<w:drawing>
<wp:anchor distT="0" distB="0" distL="0" distR="0" simplePos="0" allowOverlap="0" behindDoc="0" locked="0" layoutInCell="0" relativeHeight="190500">
<wp:simplePos x="0" y="0" />
<wp:positionH relativeFrom="margin">
<wp:posOffset>2857500</wp:posOffset>
</wp:positionH>
<wp:positionV relativeFrom="margin">
<wp:posOffset>3810000</wp:posOffset>
</wp:positionV>
<wp:extent cx="3048000" cy="2286000" />
<wp:effectExtent b="0" l="0" r="0" t="0" />
<wp:wrapSquare wrapText="bothSides" />
<wp:docPr id="1" name="Figure" />
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" />
</wp:cNvGraphicFramePr>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="0" name="" />
<pic:cNvPicPr>
<a:picLocks noChangeAspect="1" noChangeArrowheads="1" />
</pic:cNvPicPr>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rIdImage123" />
<a:srcRect />
<a:stretch>
<a:fillRect />
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode="auto">
<a:xfrm rot="0">
<a:off x="0" y="0" />
<a:ext cx="3048000" cy="2286000" />
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst />
</a:prstGeom>
</pic:spPr>
</pic:pic></a:graphicData>
</a:graphic>
</wp:anchor>
</w:drawing>"#
);
}
Expand Down
28 changes: 27 additions & 1 deletion docx-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,31 @@
},
"resolutions": {
"**/serialize-javascript": "6.0.1"
}
},
"devDependencies": {
"@types/file-saver": "2.0.5",
"@wasm-tool/wasm-pack-plugin": "1.6.0",
"adm-zip": "0.5.10",
"cpy-cli": "4.2.0",
"file-saver": "2.0.5",
"html-webpack-plugin": "5.5.3",
"jest": "28.1.3",
"npm-run-all": "4.1.5",
"text-encoding": "0.7.0",
"ts-loader": "9.4.2",
"typescript": "4.9.3",
"webpack": "4.46.0",
"webpack-cli": "5.0.1",
"webpack-dev-server": "3.11.3",
"webpack-merge": "5.8.0"
},
"files": [
"dist",
"dist/web/pkg",
"dist/node/pkg",
"js/*"
],
"module": "dist/web/index.js",
"types": "dist/web/index.d.ts",
"dependencies": {}
}
2 changes: 1 addition & 1 deletion docx-wasm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"noEmitOnError": true,
"strictNullChecks": true,
"target": "es6",
"sourceMap": true,
"lib": ["ES2017", "DOM"],
"esModuleInterop": true,
"outDir": "dist/web",
"declaration": true
Expand Down
1 change: 1 addition & 0 deletions docx-wasm/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"noEmitOnError": true,
"strictNullChecks": true,
"target": "es6",
"lib": ["ES2017", "DOM"],
"sourceMap": true,
"esModuleInterop": true,
"outDir": "dist/node",
Expand Down
1 change: 1 addition & 0 deletions docx-wasm/tsconfig.web.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"noEmitOnError": true,
"strictNullChecks": true,
"target": "es6",
"lib": ["ES2017", "DOM"],
"sourceMap": true,
"esModuleInterop": true,
"outDir": "dist/web",
Expand Down
Loading

0 comments on commit 776f76e

Please sign in to comment.