Skip to main content

Image

Struct Image 

Source
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: ImageFormat

The image’s encoding, also used to pick the media part’s extension and content type.

§width_emu: i64

The 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: i64

The height the image is displayed at, in EMUs (cy).

§description: String

Alt 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

Source

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?
examples/docx_images_and_charts.rs (line 23)
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}
Source

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).

Source

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?
examples/docx_images_and_charts.rs (line 24)
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}
Source

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.

Trait Implementations§

Source§

impl Clone for Image

Source§

fn clone(&self) -> Image

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Image

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Image

Source§

fn eq(&self, other: &Image) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Image

Auto Trait Implementations§

§

impl Freeze for Image

§

impl RefUnwindSafe for Image

§

impl Send for Image

§

impl Sync for Image

§

impl Unpin for Image

§

impl UnsafeUnpin for Image

§

impl UnwindSafe for Image

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.