Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

154 Commits

Folders and files

Repository files navigation

Standalone linked library version of NanoSVG with additional feature to suit the needs of the game Vintage Story.

Nano SVG

Parser

screenshot of some splines rendered with the sample program

NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes.

The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.

NanoSVG supports a wide range of SVG features, but something may be missing, feel free to create a pull request!

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.

NanoSVG 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 NanoSVG should be one of: 'px', 'pt', 'pc' 'mm', 'cm', or '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.

Rasterizer

screenshot of tiger.svg rendered with NanoSVG rasterizer

The parser library is accompanied with really simpler SVG rasterizer. Currently it only renders flat filled shapes.

The intended usage for the rasterizer is to for example bake icons of different size into a texture. The rasterizer is not particular fast or accurate, but it's small and packed in one header file.

Example Usage

// Load
struct NSVGimage* image;
image = nsvgParseFromFile("test.svg", "px", 96);
printf("size: %f x %f\n", image->width, image->height);
// Use...
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];
			drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]);
		}
	}
}
// Delete
nsvgDelete(image);

Using NanoSVG in your project

In order to use NanoSVG in your own project, just copy nanosvg.h to your project. In one C/C++ define NANOSVG_IMPLEMENTATION before including the library to expand the NanoSVG implementation in that file. NanoSVG depends on stdio.h ,string.h and math.h, they should be included where the implementation is expanded before including NanoSVG.

#include <stdio.h>
#include <string.h>
#include <math.h>
#define NANOSVG_IMPLEMENTATION	// Expands implementation
#include "nanosvg.h"

By default, NanoSVG parses only the most common colors. In order to get support for full list of SVG color keywords, define NANOSVG_ALL_COLOR_KEYWORDS before expanding the implementation.

#include <stdio.h>
#include <string.h>
#include <math.h>
#define NANOSVG_ALL_COLOR_KEYWORDS	// Include full list of color keywords.
#define NANOSVG_IMPLEMENTATION		// Expands implementation
#include "nanosvg.h"

Compiling Example Project

In order to compile the demo project, your will need to install GLFW to compile.

NanoSVG demo project uses premake to build platform specific projects, now is good time to install it if you don't have it already. To build the example, navigate into the root folder in your favorite terminal, then:

  • OS X: premake5 xcode4
  • Windows: premake5 vs2022
  • Linux: premake5 gmake2

See premake4 documentation for full list of supported build file types.

An example of building OS X / linux:

$ premake4 gmake2
$ make

Windows:

$ premake4 vs2022
$ msbuild nanosvg.sln /p:Configuration=Release # using a vs2022 dev powershell, or open it in vs 2022 and build it there

Compiling the lib for VS

Zig build can be used to cross compile the library for all the systems supported by vs in a convenient fashion:

zig build --release -Dvs-mode=true -p path/to/your/vs/game/repo -Dtarget=x86_64-linux-gnu.2.34 
zig build --release -Dvs-mode=true -p path/to/your/vs/game/repo -Dtarget=aarch64-linux-gnu.2.34
zig build --release -Dvs-mode=true -p path/to/your/vs/game/repo -Dtarget=x86_64-macos
zig build --release -Dvs-mode=true -p path/to/your/vs/game/repo -Dtarget=aarch64-macos
zig build --release -Dvs-mode=true -p path/to/your/vs/game/repo -Dtarget=x86_64-windows-gnu

For this you simply need to have downloaded and put the zig executable in your path. This requires version 0.14 as of now.

License

The library is licensed under zlib license

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages