#svg #nanosvg


A simple SVG parser and rasterizer using NanoSVG

9 unstable releases

Uses old Rust 2015

0.5.1 Sep 3, 2019
0.5.0 Jun 18, 2018
0.4.0 Apr 8, 2018
0.3.2 Apr 3, 2018
0.1.0 Dec 5, 2017

#117 in #svg

Download history 107/week @ 2023-05-31 13/week @ 2023-06-07 61/week @ 2023-06-14 104/week @ 2023-06-21 68/week @ 2023-06-28 95/week @ 2023-07-05 42/week @ 2023-07-12 71/week @ 2023-07-19 79/week @ 2023-07-26 32/week @ 2023-08-02 63/week @ 2023-08-09 79/week @ 2023-08-16 38/week @ 2023-08-23 54/week @ 2023-08-30 89/week @ 2023-09-06 52/week @ 2023-09-13

235 downloads per month
Used in 3 crates (via purrmitive)


694 lines

nsvg Build Status Build status Crates.io

A friendly Rust wrapper around the excellent NanoSVG C library.

Offers a simple way to parse and rasterize SVG, at whichever scale you want. It was written as a convenient way to have scaled UI elements in video games that are suited to the user's selected resolution, but can also be used for simple file conversions.

NanoSVG supports a wide range of SVG features, with most of the vector elements fully supported.

The rasterizer runs entirely on the CPU and has no external dependencies. The quality will be fairly equivalent to exporting a bitmap from Inkscape. The rasterizer is based on the one in stb_truetype, all rasters will be anti-aliased. You can read more about it here.

There are faster GPU based solutions to rendering vector graphics out there, but the simplicity of NanoSVG and it's lack of dependencies is a huge benefit, and should run just fine cross platform.


Include nsvg in your Cargo.toml dependencies.

nsvg = "0.5.1"

To include nsvg without the image dependency, add it to your Cargo.toml without default dependencies.

version = "0.5.1"
default-features = false

Now you can parse and rasterize SVGs. Use the scale argument to produce larger or smaller rasterised images. The aspect ratio will always remain the same.

extern crate nsvg;

use std::path::Path;

fn main() {
  let path = Path::new("examples/example.svg");

  // Load and parse the svg
  let svg = nsvg::parse_file(path, nsvg::Units::Pixel, 96.0).unwrap();

  // Create a scaled raster
  let scale = 2.0;
  let image = svg.rasterize(scale);

Unsupported SVG elements

As it is mostly intended to be used for parsing and rasterizing vector graphics, some SVG features are not supported by nsvg:

  • Text elements are ignored, although text can simply be converted to a path and it will work just fine

  • Embedded bitmap images are ignored

  • Scripts are ignored

  • Animations are ignored

If you encounter anything that does not rasterize as you would expect, try converting it to a path first.


By default nsvg will use prebuilt FFI bindings, but you can regenerate them manually by running:

cargo build --features bindgen

Which will also copy the bindings into the src directory. If the bindings need to be updated (when upgrading NanoSVG, for example) they should be checked in to version control.

Make sure you have libclang-dev available so bindgen works.

cargo test to run tests.

There is one provided example, which you can run with the following command:

cargo run --example svg_to_png

This will convert the example.svg vector into a PNG. The PNG will be written to the working directory as example_output.png.

See also




NanoSVG is licensed under zlib license

Sutte Hakkun logo is drawn by Nico Vliek, and is a registered trademark of Nintendo. Used for demonstration purposes only.

Mr Disk is drawn by Eoin Stanley, and is a registered trademark of Nintendo. Used for demonstration purposes only.

Anything else is MIT


~83K SLoC