Skip to content

Instantly share code, notes, and snippets.

@satoruhiga
Created February 16, 2015 16:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoruhiga/b75c3b29826dfe6eb575 to your computer and use it in GitHub Desktop.
Save satoruhiga/b75c3b29826dfe6eb575 to your computer and use it in GitHub Desktop.
ofxNanoSVG.h
#pragma once
#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"
class ofxNanoSVG
{
public:
bool load(const string& filepath)
{
NSVGimage* image;
image = nsvgParseFromFile(ofToDataPath(filepath).c_str(), "px", 72.0f);
if (image == NULL) return false;
width = image->width;
height = image->height;
NSVGshape* shape;
NSVGpath* path;
for (shape = image->shapes; shape != NULL; shape = shape->next) {
for (path = shape->paths; path != NULL; path = path->next) {
ofPtr<ofPath> p = ofPtr<ofPath>(new ofPath);
p->moveTo(path->pts[0], path->pts[1]);
for (int i = 0; i < path->npts - 1; i += 3)
{
float* pts = &path->pts[i * 2] + 2;
p->bezierTo(pts[0], pts[1],
pts[2], pts[3],
pts[4], pts[5]);
}
if (path->closed)
p->close();
if (shape->fill.type == NSVG_PAINT_COLOR)
{
p->setFilled(true);
ofColor c;
c.r = shape->fill.color & 0x000000FF;
c.g = (shape->fill.color >> 8) & 0x000000FF;
c.b = (shape->fill.color >> 16) & 0x000000FF;
p->setFillColor(c);
}
if (shape->stroke.type == NSVG_PAINT_COLOR)
{
p->setStrokeWidth(shape->strokeWidth);
ofColor c;
c.r = shape->fill.color & 0x000000FF;
c.g = (shape->fill.color >> 8) & 0x000000FF;
c.b = (shape->fill.color >> 16) & 0x000000FF;
p->setStrokeColor(c);
}
paths.push_back(p);
}
}
nsvgDelete(image);
return true;
}
float getWidth() const
{
return width;
}
float getHeight() const
{
return height;
}
int getNumPath() const
{
return paths.size();
}
const ofPath& getPathAt(int index) const
{
return *paths[index];
}
void draw() const
{
for (int i = 0; i < paths.size(); i++)
paths[i]->draw();
}
private:
float width, height;
typedef ofPtr<ofPath> ofPathRef;
vector <ofPathRef> paths;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment