| Crates.io | svg_metadata |
| lib.rs | svg_metadata |
| version | 0.6.0 |
| created_at | 2019-08-14 08:32:48.590819+00 |
| updated_at | 2025-08-12 11:11:31.237305+00 |
| description | Extracts metadata (like the viewBox, width, and height) from SVG graphics |
| homepage | |
| repository | https://github.com/mre/svg-metadata |
| max_upload_size | |
| id | 156704 |
| size | 1,702,233 |
This crate extracts metadata from SVG files. Currently it reads the following attributes:
viewBoxwidthheightYou can add more!
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().)
The SVG fixtures used for testing are provided by
under their respective licenses.