memononen / nanosvg

Simple stupid SVG parser
zlib License
1.65k stars 353 forks source link

How to implement new renderer? #25

Open trogmaniac opened 9 years ago

trogmaniac commented 9 years ago

Where would i have to "hook in" if i wanted to write a new renderer, e.g. using GDI+? Somehow i expected to find methods to draw primitives in the renderer that i could easily replace with GDI+ calls. But i guess that was a little too optimistic :)

Thanks.

memononen commented 9 years ago

The render loop is quite simple, take a look at: https://github.com/memononen/nanosvg/blob/master/src/nanosvgrast.h#L1244

To draw the shapes, something like this could work:

NSVGpath* path;
for (path = shape->paths; path != NULL; path = path->next) {
    for (i = 0; i < path->npts-1; i += 3) {
        float* p = &path->pts[i*2];
        drawBezier(p[0]*scale,p[1]*scale, p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, 0);
    }
}

or if your API is like html canvas:

NSVGpath* path;
for (path = shape->paths; path != NULL; path = path->next) {
    moveTo(path->pts[0]*scale, path->pts[1]*scale);
    for (i = 0; i < path->npts-1; i += 3) {
        float* p = &path->pts[i*2];
        drawBezierTo(p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, 0);
    }
}

That should get you started, mapping the color and gradients is a little more effort, but not too bad.

trogmaniac commented 9 years ago

I saw that loop, but didn't know how to collect the other informations about colors, gradients etc. I'll try to study the rasterizer code some more. Thanks.

memononen commented 9 years ago

NanoSVG header should give you some idea how the paints are structured, and color helper functions should give you idea how to "decode" an RGBA color from the uint color of the paint: https://github.com/memononen/nanosvg/blob/master/src/nanosvgrast.h#L818

Using the uint for color is a bit silly here, it has historical significance. I'm going to change that to a struct as I get time to do so.