fix: floating image (#710)

* fix: floating image

* fix

* fix
main
bokuweb 2024-04-26 18:29:29 +09:00 committed by GitHub
parent dcc524e6c9
commit 776f76e8ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 1671 additions and 1675 deletions

View File

@ -15,6 +15,7 @@ pub fn main() -> Result<(), DocxError> {
.floating() .floating()
.offset_x(300 * 9525) .offset_x(300 * 9525)
.offset_y(400 * 9525); .offset_y(400 * 9525);
Docx::new() Docx::new()
.add_paragraph(Paragraph::new().add_run(Run::new().add_image(pic))) .add_paragraph(Paragraph::new().add_run(Run::new().add_image(pic)))
.build() .build()

View File

@ -104,17 +104,18 @@ impl BuildXML for Drawing {
b = b.align(&x.to_string()).close(); 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)); b = b.open_position_v(&format!("{}", p.relative_from_v));
if let DrawingPosition::Offset(y) = p.position_v { match p.position_v {
DrawingPosition::Offset(y) => {
let y = format!("{}", y as u32); let y = format!("{}", y as u32);
b = b.pos_offset(&y).close(); b = b.pos_offset(&y).close();
} }
DrawingPosition::Align(a) => {
b = b.align(&a.to_string()).close();
}
}
} }
let w = format!("{}", p.size.0); let w = format!("{}", p.size.0);
@ -316,6 +317,69 @@ mod tests {
</pic:pic></a:graphicData> </pic:pic></a:graphicData>
</a:graphic> </a:graphic>
</wp:anchor> </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>"# </w:drawing>"#
); );
} }

View File

@ -24,5 +24,31 @@
}, },
"resolutions": { "resolutions": {
"**/serialize-javascript": "6.0.1" "**/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": {}
} }

View File

@ -10,7 +10,7 @@
"noEmitOnError": true, "noEmitOnError": true,
"strictNullChecks": true, "strictNullChecks": true,
"target": "es6", "target": "es6",
"sourceMap": true, "lib": ["ES2017", "DOM"],
"esModuleInterop": true, "esModuleInterop": true,
"outDir": "dist/web", "outDir": "dist/web",
"declaration": true "declaration": true

View File

@ -10,6 +10,7 @@
"noEmitOnError": true, "noEmitOnError": true,
"strictNullChecks": true, "strictNullChecks": true,
"target": "es6", "target": "es6",
"lib": ["ES2017", "DOM"],
"sourceMap": true, "sourceMap": true,
"esModuleInterop": true, "esModuleInterop": true,
"outDir": "dist/node", "outDir": "dist/node",

View File

@ -10,6 +10,7 @@
"noEmitOnError": true, "noEmitOnError": true,
"strictNullChecks": true, "strictNullChecks": true,
"target": "es6", "target": "es6",
"lib": ["ES2017", "DOM"],
"sourceMap": true, "sourceMap": true,
"esModuleInterop": true, "esModuleInterop": true,
"outDir": "dist/web", "outDir": "dist/web",

File diff suppressed because it is too large Load Diff