pub struct Image {
pub data: Vec<u8>,
pub format: ImageFormat,
pub width_emu: i64,
pub height_emu: i64,
pub description: String,
pub shape_properties: Option<ShapeProperties>,
}Expand description
An inline image (<w:drawing><wp:inline>..), embedded in a run.
Structure is a minimal wp:inline/pic:pic skeleton (no rotation, cropping or other DrawingML
effects yet).
Fields§
§data: Vec<u8>The raw, encoded image bytes (e.g. the bytes of a .png file).
format: ImageFormatThe image’s encoding, also used to pick the media part’s extension and content type.
width_emu: i64The width the image is displayed at, in EMUs (English Metric Units; 914,400 EMU per inch —
CT_PositiveSize2D’s cx). Display size, not necessarily the image’s native pixel size.
height_emu: i64The height the image is displayed at, in EMUs (cy).
description: StringAlt text / description (wp:docPr’s descr attribute). May be empty.
shape_properties: Option<ShapeProperties>Additional shape formatting (fill/line/rotation) for the picture’s own pic:spPr. None
(the only value this crate produced before this point existed) preserves the exact
fixed-rectangle geometry writer.rs::write_drawing always wrote; Some enriches that
spPr with the given content (reusing
drawing::write_shape_properties/drawing::read_shape_properties) without changing the
fixed a:xfrm/size handling, which stays governed by width_emu/height_emu as before.
Purely additive: no existing round-trip test is affected since none of them ever set this
field.
Implementations§
Source§impl Image
impl Image
Sourcepub fn new(
data: impl Into<Vec<u8>>,
format: ImageFormat,
width_emu: i64,
height_emu: i64,
) -> Image
pub fn new( data: impl Into<Vec<u8>>, format: ImageFormat, width_emu: i64, height_emu: i64, ) -> Image
Creates an image with an explicit display size in EMUs.
Examples found in repository?
18fn main() -> office_toolkit::Result<()> {
19 let path = output_path("docx_images_and_charts.docx");
20 let image_bytes = std::fs::read(image_fixture_path())?;
21
22 // 2 inches wide, 1.5 inches tall (914,400 EMU per inch).
23 let image = Image::new(image_bytes, ImageFormat::Png, 1_828_800, 1_371_600)
24 .with_description("Project test image");
25 let chart = EmbeddedChart::new(sample_chart_space(), 4_572_000, 2_743_200)
26 .with_name("Quarterly revenue");
27
28 let document = Document::new()
29 .with_paragraph(heading("An inline image", false))
30 .with_paragraph(Paragraph::new().with_run(Run::with_image(image)))
31 .with_paragraph(heading("An embedded bar chart", true))
32 .with_paragraph(Paragraph::new().with_run(Run::with_chart(chart)));
33
34 document.save_to_file(&path)?;
35 println!("Wrote {}", path.display());
36 Ok(())
37}Sourcepub fn from_pixels(
data: impl Into<Vec<u8>>,
format: ImageFormat,
width_px: u32,
height_px: u32,
) -> Image
pub fn from_pixels( data: impl Into<Vec<u8>>, format: ImageFormat, width_px: u32, height_px: u32, ) -> Image
Creates an image with its display size given in pixels, converted to EMUs assuming 96 DPI (a common screen resolution default, and Word’s own default when it can’t otherwise determine an image’s intended size).
Sourcepub fn with_description(self, description: impl Into<String>) -> Image
pub fn with_description(self, description: impl Into<String>) -> Image
Sets the image’s alt text / description and returns it for chaining.
Examples found in repository?
18fn main() -> office_toolkit::Result<()> {
19 let path = output_path("docx_images_and_charts.docx");
20 let image_bytes = std::fs::read(image_fixture_path())?;
21
22 // 2 inches wide, 1.5 inches tall (914,400 EMU per inch).
23 let image = Image::new(image_bytes, ImageFormat::Png, 1_828_800, 1_371_600)
24 .with_description("Project test image");
25 let chart = EmbeddedChart::new(sample_chart_space(), 4_572_000, 2_743_200)
26 .with_name("Quarterly revenue");
27
28 let document = Document::new()
29 .with_paragraph(heading("An inline image", false))
30 .with_paragraph(Paragraph::new().with_run(Run::with_image(image)))
31 .with_paragraph(heading("An embedded bar chart", true))
32 .with_paragraph(Paragraph::new().with_run(Run::with_chart(chart)));
33
34 document.save_to_file(&path)?;
35 println!("Wrote {}", path.display());
36 Ok(())
37}Sourcepub fn with_shape_properties(self, shape_properties: ShapeProperties) -> Image
pub fn with_shape_properties(self, shape_properties: ShapeProperties) -> Image
Sets additional shape formatting (fill/line/rotation) for this picture’s own pic:spPr and
returns it for chaining. See Image::shape_properties’s doc comment.