Rasterizes SVG image, returns RGBA image (non-premultiplied alpha).
The shapes in the SVG images are transformed by the viewBox and converted to specified units. That is, you should get the same looking data as your designed in your favorite app.
NanoVega.SVG can return the paths in few different units. For example if you want to render an image, you may choose to get the paths in pixels, or if you are feeding the data into a CNC-cutter, you may want to use millimeters.
The units passed to NanoVega.SVG should be one of: 'px', 'pt', 'pc', 'mm', 'cm', 'in'. DPI (dots-per-inch) controls how the unit conversion is done.
If you don't know or care about the units stuff, "px" and 96 should get you going.
Example Usage:
// Load NSVG* image = nsvgParseFromFile("test.svg", "px", 96); printf("size: %f x %f\n", image.width, image.height); // Use... image.forEachShape((in ref NSVG.Shape shape) { if (!shape.visible) return; shape.forEachPath((in ref NSVG.Path path) { // this will issue final `LineTo` for closed pathes path.forEachCommand!true(delegate (NSVG.Command cmd, const(float)[] args) nothrow @trusted @nogc { final switch (cmd) { case NSVG.Command.MoveTo: nvg.moveTo(args); break; case NSVG.Command.LineTo: nvg.lineTo(args); break; case NSVG.Command.QuadTo: nvg.quadTo(args); break; case NSVG.Command.BezierTo: nvg.bezierTo(args); break; } }); }); }); NSVGrasterizer rast = nsvgCreateRasterizer(); // Allocate memory for image ubyte* img = malloc(w*h*4); // Rasterize rasterize(rast, image, 0, 0, 1, img, w, h, w*4); // Delete image.kill();
To turn a SVG into a png:
import arsd.svg; import arsd.png; void main() { // Load NSVG* image = nsvgParseFromFile("test.svg", "px", 96); int w = 200; int h = 200; NSVGrasterizer rast = nsvgCreateRasterizer(); // Allocate memory for image auto img = new TrueColorImage(w, h); // Rasterize rasterize(rast, image, 0, 0, 1, img.imageData.bytes.ptr, w, h, w*4); // Delete image.kill(); writePng("test.png", img); }
NanoVega.SVG is a simple stupid SVG parser. The output of the parser is a list of drawing commands.
The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.
NanoVega.SVG supports a wide range of SVG features, but something may be missing. Your's Captain Obvious.