2 * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
20 * The SVG parser is based on Anti-Grain Geometry 2.4 SVG example
21 * Copyright (C) 2002-2004 Maxim Shemanarev (McSeem) (http://www.antigrain.com/)
23 * Arc calculation code based on canvg (https://code.google.com/p/canvg/)
25 * Bounding box calculation based on http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
32 // NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes.
34 // The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.
36 // NanoSVG supports a wide range of SVG features, but something may be missing, feel free to create a pull request!
38 // The shapes in the SVG images are transformed by the viewBox and converted to specified units.
39 // That is, you should get the same looking data as your designed in your favorite app.
41 // NanoSVG can return the paths in few different units. For example if you want to render an image, you may choose
42 // to get the paths in pixels, or if you are feeding the data into a CNC-cutter, you may want to use millimeters.
44 // The units passed to NanoVG should be one of: 'px', 'pt', 'pc' 'mm', 'cm', or 'in'.
45 // DPI (dots-per-inch) controls how the unit conversion is done.
47 // If you don't know or care about the units stuff, "px" and 96 should get you going.
53 image = nsvgParseFromFile("test.svg", "px", 96);
54 printf("size: %f x %f\n", image->width, image->height);
56 for (shape = image->shapes; shape != NULL; shape = shape->next) {
57 for (path = shape->paths; path != NULL; path = path->next) {
58 for (i = 0; i < path->npts-1; i += 3) {
59 float* p = &path->pts[i*2];
60 drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]);
71 NSVG_PAINT_LINEAR_GRADIENT = 2,
72 NSVG_PAINT_RADIAL_GRADIENT = 3,
77 NSVG_SPREAD_REFLECT = 1,
78 NSVG_SPREAD_REPEAT = 2,
94 NSVG_FILLRULE_NONZERO = 0,
95 NSVG_FILLRULE_EVENODD = 1,
99 NSVG_FLAGS_VISIBLE = 0x01
102 typedef struct NSVGgradientStop {
107 typedef struct NSVGgradient {
112 NSVGgradientStop stops[1];
115 typedef struct NSVGpaint {
117 * In the original file, using char type (without signed or unsigned) can be interpreted
118 * as 'unsigned char' in some build environments, like ARM architecture.
119 * To prevent the unexpected behavior, we replace 'char type' with 'signed char type' here.
124 NSVGgradient* gradient;
128 typedef struct NSVGpath
130 float* pts; // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ...
131 int npts; // Total number of bezier points.
132 char closed; // Flag indicating if shapes should be treated as closed.
133 float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
134 struct NSVGpath* next; // Pointer to next path, or NULL if last element.
137 typedef struct NSVGshape
139 char id[64]; // Optional 'id' attr of the shape or its group
140 NSVGpaint fill; // Fill paint
141 NSVGpaint stroke; // Stroke paint
142 float opacity; // Opacity of the shape.
143 float strokeWidth; // Stroke width (scaled).
144 float strokeDashOffset; // Stroke dash offset (scaled).
145 float strokeDashArray[8]; // Stroke dash array (scaled).
146 char strokeDashCount; // Number of dash values in dash array.
147 char strokeLineJoin; // Stroke join type.
148 char strokeLineCap; // Stroke cap type.
149 float miterLimit; // Miter limit
150 char fillRule; // Fill rule, see NSVGfillRule.
151 unsigned char flags; // Logical or of NSVG_FLAGS_* flags
152 float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
153 NSVGpath* paths; // Linked list of paths in the image.
154 struct NSVGshape* next; // Pointer to next shape, or NULL if last element.
157 typedef struct NSVGimage
159 float width; // Width of the image.
160 float height; // Height of the image.
161 NSVGshape* shapes; // Linked list of shapes in the image.
164 // Parses SVG file from a file, returns SVG image as paths.
165 NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi);
167 // Parses SVG file from a null terminated string, returns SVG image as paths.
168 // Important note: changes the string.
169 NSVGimage* nsvgParse(char* input, const char* units, float dpi);
171 // Deletes list of paths.
172 void nsvgDelete(NSVGimage* image);
177 * In the original software, The nanosvg implementation was followed here.
178 * We have moved the implementation to nanosvg.cc.