#svg #svg-graphics #image-metadata #graphics #image

svg_metadata

Extracts metadata (like the viewBox, width, and height) from SVG graphics

12 releases

Uses new Rust 2024

0.6.0 Aug 12, 2025
0.5.1 May 28, 2024
0.5.0 Sep 8, 2023
0.4.4 Feb 6, 2023
0.3.0 Aug 24, 2019

#2119 in Parser implementations

Download history 1258/week @ 2026-01-11 919/week @ 2026-01-18 428/week @ 2026-01-25 481/week @ 2026-02-01 330/week @ 2026-02-08 379/week @ 2026-02-15 461/week @ 2026-02-22 455/week @ 2026-03-01 572/week @ 2026-03-08 362/week @ 2026-03-15 304/week @ 2026-03-22 316/week @ 2026-03-29 384/week @ 2026-04-05 529/week @ 2026-04-12 439/week @ 2026-04-19 491/week @ 2026-04-26

1,868 downloads per month
Used in 7 crates (via vexy-vsvg)

Apache-2.0/MIT

495KB
355 lines

svg-metadata

CI Documentation

What is it?

This crate extracts metadata from SVG files. Currently it reads the following attributes:

  • viewBox
  • width
  • height

You can add more!

Usage Examples

use svg_metadata::{Metadata, ViewBox};

fn main() {
    let svg = r#"
        <svg viewBox="0 1 99 100" xmlns="http://www.w3.org/2000/svg">
            <rect x="0" y="0" width="100%" height="100%"/>
        </svg>
    "#;

    let meta = Metadata::parse(svg).unwrap();
    assert_eq!(
        meta.view_box,
        Some(ViewBox {
            min_x: 0.0,
            min_y: 1.0,
            width: 99.0,
            height: 100.0
        })
    );
}

Width and height elements contain the units (if available):

use svg_metadata::{Metadata, Height, Width, Unit};

fn main() {
    let svg = r#"
        <svg width="100cm" height="50cm" xmlns="http://www.w3.org/2000/svg">
            <rect x="0" y="0" width="100%" height="100%"/>
        </svg>
    "#;

    let meta = Metadata::parse(svg).unwrap();

    assert_eq!(
        meta.width,
        Some(Width {
            width: 100.0,
            unit: Unit::Cm
        })
    );

    assert_eq!(
        meta.height,
        Some(Height {
            height: 50.0,
            unit: Unit::Cm
        })
    );
}

(You can also parse files directly with parse_file().)

Credits

The SVG fixtures used for testing are provided by

under their respective licenses.

Dependencies

~2–3MB
~55K SLoC