emfsdk 0.1.0

EMF, EMF+, and WMF SDK for Rust
Documentation

EMF SDK for Rust

crates.io docs.rs

emfsdk is a pure-Rust SDK for reading, inspecting, editing, and writing Enhanced Metafile (EMF), Enhanced Metafile Format Plus (EMF+), and Windows Metafile (WMF) data.

Known records and fields are exposed as Rust types. Unknown records, reserved extensions, padding, and opaque binary payloads are preserved so an unmodified metafile can be written without losing data.

Quick Start

cargo add emfsdk
use emfsdk::Metafile;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input = std::fs::read("input.emf")?;
    let metafile = Metafile::from_bytes(&input)?;

    println!("format: {:?}", metafile.format());

    let output = metafile.to_bytes()?;
    std::fs::write("output.emf", output)?;
    Ok(())
}

Use EmfMetafile, WmfMetafile, and each record's parse_data method when you need format-specific typed access.

Compatible and Strict Parsing

Metafile::from_bytes is compatibility-first. It preserves bounded records that are structurally readable even when a producer violates a field rule. Compatibility is explicit and inspectable:

use emfsdk::Metafile;

# fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = std::fs::read("input.wmf")?;
let metafile = Metafile::from_bytes(&input)?;
for diagnostic in metafile.compatibility_diagnostics() {
    eprintln!("{diagnostic:?}");
}
# Ok(())
# }

Use Metafile::from_bytes_strict or validate_strict when every known record must satisfy the Microsoft specification and reproduce exactly through its typed parser and writer. Unknown record types remain an intentional raw fallback; malformed known records are reported instead of silently treated as typed.

Features

  • default: parsing and writing only, with no rendering dependencies
  • render: enables raster rendering with image, fontdb, and ttf-parser
cargo add emfsdk --features render

The minimum supported Rust version is 1.88. The workspace uses the Rust 2024 edition.

Format Coverage

The implementation follows the Microsoft Open Specifications:

LibreOffice and Apache POI are used as behavioral references. Corpus and round-trip tests live in ooxmlsdk-test-suite.

The current standalone candidate corpus covers 950,508 EMF records, 22,824 WMF records, and 14,231 EMF+ records. Of those, 31,922 known records require compatibility preservation. The legacy Office lane covers 134 embedded EMF and 156 embedded WMF images containing another 107,578 records; 3,955 require compatibility preservation. Tests ratchet these counts and require exact whole-file bytes plus exact typed parse/write bytes for conforming records.

Project Status

Version 0.1 remains an early SDK. Byte-preserving parsing and typed record coverage are the primary focus, but compatibility fallbacks above still need to be reduced. The public API can change before 1.0.

The optional renderer is suitable for compatibility and preview workflows, but does not promise pixel-identical output with Windows GDI, GDI+, or LibreOffice.

Changelog

See CHANGELOG.md.

License

Licensed under either of

at your option.