int main(int argc, char **argv){ NSVGimage* image; int dpi; if(argc <= 1){ printf("Usage : filename.svg dpi=96\n"); return 0; } if(argc <= 2){ dpi = 96; } else{ dpi = atoi(argv[2]); } image = nsvgParseFromFile(argv[1], "px", dpi ); printf("size: %f x %f\n", image->width, image->height); // Use... NSVGshape *shape; NSVGpath *path; int i; for (shape = image->shapes; shape != NULL; shape = shape->next) { for (path = shape->paths; path != NULL; path = path->next) { for (i = 0; i < path->npts-1; i += 3) { float* p = &path->pts[i*2]; printf("%.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f\n", p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); //drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]); } } } // Delete nsvgDelete(image); }
ImTextureID PiGui::RenderSVG(std::string svgFilename, int width, int height) { Output("nanosvg: %s %dx%d\n", svgFilename.c_str(), width, height); NSVGimage *image = NULL; NSVGrasterizer *rast = NULL; unsigned char* img = NULL; int w, h; // size of each icon // int size = 64; // 16 columns // int W = 16*size; int W = width; // 16 rows // int H = 16*size; int H = height; img = static_cast<unsigned char*>(malloc(W*H*4)); memset(img, 0, W * H * 4); std::string filename = svgFilename; // FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "icons"), "icons.svg"); image = nsvgParseFromFile(filename.c_str(), "px", 96.0f); if (image == NULL) { Error("Could not open SVG image.\n"); } w = static_cast<int>(image->width); h = static_cast<int>(image->height); rast = nsvgCreateRasterizer(); if (rast == NULL) { Error("Could not init rasterizer.\n"); } if (img == NULL) { Error("Could not alloc image buffer.\n"); } { float scale = double(W)/w; float tx = 0; float ty = 0; nsvgRasterize(rast, image, tx, ty, scale, img, W, H, W*4); } nsvgDeleteRasterizer(rast); nsvgDelete(image); return makeTexture(img, W, H); }
int main() { GLFWwindow* window; const GLFWvidmode* mode; if (!glfwInit()) return -1; mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); window = glfwCreateWindow(mode->width - 40, mode->height - 80, "Nano SVG", NULL, NULL); if (!window) { printf("Could not open window\n"); glfwTerminate(); return -1; } glfwSetFramebufferSizeCallback(window, resizecb); glfwMakeContextCurrent(window); glEnable(GL_POINT_SMOOTH); glEnable(GL_LINE_SMOOTH); g_image = nsvgParseFromFile("../example/nano.svg", "px", 96.0f); if (g_image == NULL) { printf("Could not open SVG image.\n"); glfwTerminate(); return -1; } while (!glfwWindowShouldClose(window)) { drawframe(window); glfwPollEvents(); } nsvgDelete(g_image); glfwTerminate(); return 0; }
int main() { struct NSVGimage *image = NULL; struct NSVGrasterizer *rast = NULL; unsigned char* img = NULL; int w, h; const char* filename = "../example/23.svg"; printf("parsing %s\n", filename); image = nsvgParseFromFile(filename, "px", 96.0f); if (image == NULL) { printf("Could not open SVG image.\n"); goto error; } w = image->width; h = image->height; rast = nsvgCreateRasterizer(); if (rast == NULL) { printf("Could not init rasterizer.\n"); goto error; } img = malloc(w*h*4); if (img == NULL) { printf("Could not alloc image buffer.\n"); goto error; } printf("rasterizing image %d x %d\n", w, h); nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4); printf("writing svg.png\n"); stbi_write_png("svg.png", w, h, 4, img, w*4); error: nsvgDeleteRasterizer(rast); nsvgDelete(image); return 0; }
VectorTexture::VectorTexture(std::string filepath, Filtering filtering, Wrap wrap) : Texture() { // Check file format if (!checkFileNameExtension(filepath, "svg")) { throwError(OperationNotifier::Operation::IMAGE_LOADING, "Graphics file not found or wrong format", filepath); } // Parse file NSVGimage* svg = nsvgParseFromFile(buildPath(filepath).c_str(), "px", SVG_DPI); // Check whether file found and parsed if (svg == NULL) { throwError(OperationNotifier::Operation::IMAGE_LOADING, "Graphics file not found or error while parsing", filepath); } // Rasterize it and create OpenGL texture rasterizeGraphics(svg, filtering, wrap, filepath); // Delete graphics nsvgDelete(svg); }
plx::ComPtr<ID2D1PathGeometry> RealizeSVG( const char* file, const plx::DPI& dpi, plx::ComPtr<ID2D1Factory2> dd_factory) { auto image = nsvgParseFromFile(file, "px", static_cast<float>(dpi.get_dpi_x())); if (!image) return nullptr; plx::ComPtr<ID2D1PathGeometry> geom; auto hr = dd_factory->CreatePathGeometry(geom.GetAddressOf()); if (hr != S_OK) throw plx::ComException(__LINE__, hr); plx::ComPtr<ID2D1GeometrySink> sink; hr = geom->Open(sink.GetAddressOf()); if (hr != S_OK) throw plx::ComException(__LINE__, hr); for (auto shape = image->shapes; shape != nullptr; shape = shape->next) { for (auto path = shape->paths; path != nullptr; path = path->next) { sink->BeginFigure( D2D1::Point2F(path->pts[0], path->pts[1]), D2D1_FIGURE_BEGIN_FILLED); for (auto i = 0; i < path->npts - 1; i += 3) { float* p = &path->pts[i*2]; sink->AddBezier( D2D1::BezierSegment(D2D1::Point2F(p[2], p[3]), D2D1::Point2F(p[4], p[5]), D2D1::Point2F(p[6], p[7]))); } sink->EndFigure( (path->closed == 1) ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN); } } sink->Close(); nsvgDelete(image); return geom; }