memononen / nanosvg

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

Programmatic Rendering of a Line - no output in resulting image #109

Open BusFactor1Inc opened 6 years ago

BusFactor1Inc commented 6 years ago

I'm trying to programmatically render some images using the lower level functions, but I'm to seeing anything in the resulting image. I can't seem to find explicit 'stroke' or 'fill' functions; is there something I'm missing to get something to actually show on the image?

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <graphics>
#define WIDTH 2000
#define HEIGHT 2000
int 
main(int argc, char (**argv))
{
        NSVGparser     *(parser) = (nsvg__createParser());
        NSVGrasterizer *(raster) = (nsvgCreateRasterizer());
        NSVGimage      *(image) = (parser->image);
        nsvg__resetPath(parser);
        nsvg__moveTo(parser, 0, 0);
        nsvg__lineTo(parser, 100, 100);
        nsvg__addPath(parser, 0);
        nsvg__addShape(parser);
        unsigned char  *(img) = (malloc(((WIDTH) * (HEIGHT) * (4))));
        nsvgRasterize(raster, image, 0, 0, 1, img, WIDTH, HEIGHT, ((WIDTH) * (4)));
        stbi_write_png("logo.png", WIDTH, HEIGHT, 4, img, ((WIDTH) * (4)));
        puts("[c]");

}

The included 'graphics' file is:

#include <stdio.h>
#include <string.h>
#include <math.h>
#define NANOSVG_IMPLEMENTATION
#include <src/nanosvg/src/nanosvg.h>
#define NANOSVGRAST_IMPLEMENTATION
#include <src/nanosvg/src/nanosvgrast.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <src/nanosvg/example/stb_image_write.h>

This is the resulting image:

logo

BusFactor1Inc commented 6 years ago

Do you have any questions about what I'm trying to accomplish with this program?

memononen commented 6 years ago

You need to somehow hack in the fill and style. They are stored in the current attribute. This might work, you need to call it before addShape():

NSVGattrib* attr = nsvg__getAttr(parser);
attr->strokeColor = NSVG_RGB(255,0,0);
attr->strokeOpacity = 1.0f;
attr-> strokeWidth = 1.0f;
attr->hasStroke = 1;

Take a look at what gets set when a style is parsed: https://github.com/memononen/nanosvg/blob/master/src/nanosvg.h#L1710

and default values are set here (i.e. default render style is black fill): https://github.com/memononen/nanosvg/blob/master/src/nanosvg.h#L624

BusFactor1Inc commented 6 years ago

:+1: thanks. I'll give it a try.

BusFactor1Inc commented 6 years ago

Still can't get anything in the image after filling those and a few more attrs (visibility, opacity).

Nothing seems to be working.

memononen commented 6 years ago

I cannot think of anything else on top of my head without spending some time in debugger and test app (sorry, don't have that time to spare right now). A kludge would be to make your program output an SVG in a string (yeah, I know!) and parse that and pass the results to the rasterizer.

I have thought about adding html canvas like API to the rendering backend, but I just have not had the time to do that.

BusFactor1Inc commented 6 years ago

:+1: If anything a simple test program that just drew an X on the image would suffice. I know I"m just missing something simple. Thanks for your help so far.

SergeySlice commented 6 years ago

Because in nsvg__appPath

if (p->npts < 4)
        return;

You have two points.