arsd.svg

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.

More...

Members

Enums

NSVGDefaults
enum NSVGDefaults

Functions

kill
void kill(NSVG* image)
kill
void kill(NSVGrasterizer r)
nsvgCreateRasterizer
NSVGrasterizer nsvgCreateRasterizer()
nsvgParse
NSVG* nsvgParse(const(char)[] input, const(char)[] units, float dpi, int canvaswdt, int canvashgt)
nsvgParseFromFile
NSVG* nsvgParseFromFile(const(char)[] filename, const(char)[] units, float dpi, int canvaswdt, int canvashgt)
nsvgParseFromFile
NSVG* nsvgParseFromFile(ST fi, const(char)[] units, float dpi, int canvaswdt, int canvashgt)
nsvg__curveBounds
void nsvg__curveBounds(float* bounds, const(float)* curve)
nsvg__evalBezier
double nsvg__evalBezier(double t, double p0, double p1, double p2, double p3)
nsvg__ptInBounds
int nsvg__ptInBounds(const(float)* pt, const(float)* bounds)
nsvg__xformIdentity
void nsvg__xformIdentity(float* t)
nsvg__xformInverse
void nsvg__xformInverse(float* inv, const(float)* t)
nsvg__xformMultiply
void nsvg__xformMultiply(float* t, const(float)* s)
nsvg__xformPoint
void nsvg__xformPoint(float* dx, float* dy, float x, float y, const(float)* t)
nsvg__xformPremultiply
void nsvg__xformPremultiply(float* t, const(float)* s)
nsvg__xformSetRotation
void nsvg__xformSetRotation(float* t, float a)
nsvg__xformSetScale
void nsvg__xformSetScale(float* t, float sx, float sy)
nsvg__xformSetSkewX
void nsvg__xformSetSkewX(float* t, float a)
nsvg__xformSetSkewY
void nsvg__xformSetSkewY(float* t, float a)
nsvg__xformSetTranslation
void nsvg__xformSetTranslation(float* t, float tx, float ty)
nsvg__xformVec
void nsvg__xformVec(float* dx, float* dy, float x, float y, const(float)* t)
rasterize
void rasterize(NSVGrasterizer r, const(NSVG)* image, float tx, float ty, float scale, ubyte* dst, int w, int h, int stride)

Rasterizes SVG image, returns RGBA image (non-premultiplied alpha).

Manifest constants

NSVG_EPSILON
enum NSVG_EPSILON;
NSVG_PI
enum NSVG_PI;

Structs

NSVG
struct NSVG

Detailed Description

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);


}
Suggestion Box / Bug Report