#svg #nanosvg

nsvg

A simple SVG parser and rasterizer using NanoSVG

8 releases (4 breaking)

0.5.0 Jun 18, 2018
0.4.0 Apr 8, 2018
0.3.2 Apr 3, 2018
0.3.1 Mar 27, 2018
0.1.0 Dec 5, 2017

#91 in Images

Download history 3/week @ 2019-01-17 1/week @ 2019-01-24 3/week @ 2019-01-31 2/week @ 2019-02-07 2/week @ 2019-02-14 1/week @ 2019-02-21 27/week @ 2019-02-28 32/week @ 2019-03-07 2/week @ 2019-03-14 43/week @ 2019-03-21 5/week @ 2019-03-28 11/week @ 2019-04-04 5/week @ 2019-04-11 2/week @ 2019-04-18 13/week @ 2019-04-25

82 downloads per month
Used in 1 crate

Zlib/MIT

40KB
694 lines

nsvg Build Status Build status Crates.io

A friendly Rust wrapper around the excellent NanoSVG C library. Offering simple SVG parsing and rasterizing.

Does not provide all the functionality of NanoSVG yet. Just the bare minimum to create scaled rasters of SVGs.

Like NanoSVG, the rasteriser only renders flat filled shapes. It is not particularly fast or accurate, but it is a simple way to bake vector graphics into textures.

https://github.com/memononen/nanosvg

https://github.com/rezrov/libnanosvg

Usage

Include nsvg in your Cargo.toml dependencies.

[dependencies]
nsvg = "0.5.0"

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

[dependencies.nsvg]
version = "0.5.0"
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 remain the same.

This can be useful to create rasters to suit high resolution displays.

extern crate nsvg;

use std::path::Path;

fn main() {
  let path = Path::new("examples/spiral.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);
}

Developing

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 spiral.svg vector into a PNG. The PNG will be written to the working directory as spiral_output.png.

License

NanoSVG is licensed under zlib license

Anything else is MIT

Dependencies

~2MB
~40K SLoC