KiCad PCB EDA Suite
nanosvg.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
3  *
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.
7  *
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:
11  *
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.
19  *
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/)
22  *
23  * Arc calculation code based on canvg (https://code.google.com/p/canvg/)
24  *
25  * Bounding box calculation based on http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
26  *
27  */
28 
29 #ifndef NANOSVG_H
30 #define NANOSVG_H
31 
32 
33 // NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes.
34 //
35 // The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.
36 //
37 // NanoSVG supports a wide range of SVG features, but something may be missing, feel free to create a pull request!
38 //
39 // The shapes in the SVG images are transformed by the viewBox and converted to specified units.
40 // That is, you should get the same looking data as your designed in your favorite app.
41 //
42 // NanoSVG can return the paths in few different units. For example if you want to render an image, you may choose
43 // to get the paths in pixels, or if you are feeding the data into a CNC-cutter, you may want to use millimeters.
44 //
45 // The units passed to NanoSVG should be one of: 'px', 'pt', 'pc' 'mm', 'cm', or 'in'.
46 // DPI (dots-per-inch) controls how the unit conversion is done.
47 //
48 // If you don't know or care about the units stuff, "px" and 96 should get you going.
49 
50 
51 /* Example Usage:
52  // Load SVG
53  NSVGimage* image;
54  image = nsvgParseFromFile("test.svg", "px", 96);
55  printf("size: %f x %f\n", image->width, image->height);
56  // Use...
57  for (NSVGshape *shape = image->shapes; shape != NULL; shape = shape->next) {
58  for (NSVGpath *path = shape->paths; path != NULL; path = path->next) {
59  for (int i = 0; i < path->npts-1; i += 3) {
60  float* p = &path->pts[i*2];
61  drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]);
62  }
63  }
64  }
65  // Delete
66  nsvgDelete(image);
67 */
68 
69 #include <stdio.h>
70 
76 };
77 
82 };
83 
88 };
89 
94 };
95 
99 };
100 
101 enum NSVGflags {
103 };
104 
105 typedef struct NSVGgradientStop {
106  unsigned int color;
107  float offset;
109 
110 typedef struct NSVGgradient {
111  float xform[6];
112  char spread;
113  float fx, fy;
114  int nstops;
116 } NSVGgradient;
117 
118 typedef struct NSVGpaint {
119  char type;
120  union {
121  unsigned int color;
123  };
124 } NSVGpaint;
125 
126 typedef struct NSVGpath
127 {
128  float* pts; // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ...
129  int npts; // Total number of bezier points.
130  char closed; // Flag indicating if shapes should be treated as closed.
131  float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
132  struct NSVGpath* next; // Pointer to next path, or NULL if last element.
133 } NSVGpath;
134 
135 typedef struct NSVGshape
136 {
137  char id[64]; // Optional 'id' attr of the shape or its group
138  NSVGpaint fill; // Fill paint
139  NSVGpaint stroke; // Stroke paint
140  float opacity; // Opacity of the shape.
141  float strokeWidth; // Stroke width (scaled).
142  float strokeDashOffset; // Stroke dash offset (scaled).
143  float strokeDashArray[8]; // Stroke dash array (scaled).
144  char strokeDashCount; // Number of dash values in dash array.
145  char strokeLineJoin; // Stroke join type.
146  char strokeLineCap; // Stroke cap type.
147  float miterLimit; // Miter limit
148  char fillRule; // Fill rule, see NSVGfillRule.
149  unsigned char flags; // Logical or of NSVG_FLAGS_* flags
150  float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
151  NSVGpath* paths; // Linked list of paths in the image.
152  struct NSVGshape* next; // Pointer to next shape, or NULL if last element.
153 } NSVGshape;
154 
155 typedef struct NSVGimage
156 {
157  float width; // Width of the image.
158  float height; // Height of the image.
159  NSVGshape* shapes; // Linked list of shapes in the image.
160 } NSVGimage;
161 
162 // Parses SVG file from a file, returns SVG image as paths.
163 // fp will be closed after reading the file
164 NSVGimage* nsvgParseFromFile( FILE* fp, const char* units, float dpi);
165 
166 // Parses SVG file from a null terminated string, returns SVG image as paths.
167 // Important note: changes the string.
168 NSVGimage* nsvgParse(char* input, const char* units, float dpi);
169 
170 // Duplicates a path.
172 
173 // Deletes an image.
174 void nsvgDelete(NSVGimage* image);
175 
176 #endif // NANOSVG_H
struct NSVGpath * next
Definition: nanosvg.h:132
struct NSVGgradient NSVGgradient
NSVGpath * nsvgDuplicatePath(NSVGpath *p)
Definition: nanosvg.cpp:3712
char strokeDashCount
Definition: nanosvg.h:144
NSVGgradientStop stops[1]
Definition: nanosvg.h:115
struct NSVGshape NSVGshape
struct NSVGimage NSVGimage
struct NSVGpath NSVGpath
unsigned int color
Definition: nanosvg.h:106
NSVGgradient * gradient
Definition: nanosvg.h:122
float miterLimit
Definition: nanosvg.h:147
NSVGpath * paths
Definition: nanosvg.h:151
NSVGspreadType
Definition: nanosvg.h:78
NSVGpaint fill
Definition: nanosvg.h:138
struct NSVGshape * next
Definition: nanosvg.h:152
char spread
Definition: nanosvg.h:112
float fx
Definition: nanosvg.h:113
char type
Definition: nanosvg.h:119
float strokeWidth
Definition: nanosvg.h:141
unsigned char flags
Definition: nanosvg.h:149
struct NSVGpaint NSVGpaint
NSVGlineJoin
Definition: nanosvg.h:84
float xform[6]
Definition: nanosvg.h:111
unsigned int color
Definition: nanosvg.h:121
float * pts
Definition: nanosvg.h:128
NSVGfillRule
Definition: nanosvg.h:96
NSVGlineCap
Definition: nanosvg.h:90
struct NSVGgradientStop NSVGgradientStop
NSVGpaint stroke
Definition: nanosvg.h:139
float strokeDashArray[8]
Definition: nanosvg.h:143
int npts
Definition: nanosvg.h:129
NSVGflags
Definition: nanosvg.h:101
NSVGshape * shapes
Definition: nanosvg.h:159
NSVGpaintType
Definition: nanosvg.h:71
float bounds[4]
Definition: nanosvg.h:150
float fy
Definition: nanosvg.h:113
float opacity
Definition: nanosvg.h:140
char strokeLineCap
Definition: nanosvg.h:146
float height
Definition: nanosvg.h:158
NSVGimage * nsvgParse(char *input, const char *units, float dpi)
Definition: nanosvg.cpp:3642
char fillRule
Definition: nanosvg.h:148
char strokeLineJoin
Definition: nanosvg.h:145
void nsvgDelete(NSVGimage *image)
Definition: nanosvg.cpp:3752
float bounds[4]
Definition: nanosvg.h:131
char closed
Definition: nanosvg.h:130
float width
Definition: nanosvg.h:157
int nstops
Definition: nanosvg.h:114
NSVGimage * nsvgParseFromFile(FILE *fp, const char *units, float dpi)
Definition: nanosvg.cpp:3670
float strokeDashOffset
Definition: nanosvg.h:142