openEmbroider  0.1
an open source embroidery software
 All Classes Functions Enumerations
nanosvg.h
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-Graim 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 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 // NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes.
37 //
38 // The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.
39 //
40 // NanoSVG supports a wide range of SVG features, but something may be missing, feel free to create a pull request!
41 //
42 // The shapes in the SVG images are transformed by the viewBox and converted to specified units.
43 // That is, you should get the same looking data as your designed in your favorite app.
44 //
45 // NanoSVG can return the paths in few different units. For example if you want to render an image, you may choose
46 // to get the paths in pixels, or if you are feeding the data into a CNC-cutter, you may want to use millimeters.
47 //
48 // The units passed to NanoVG should be one of: 'px', 'pt', 'pc' 'mm', 'cm', or 'in'.
49 // DPI (dots-per-inch) controls how the unit conversion is done.
50 //
51 // If you don't know or care about the units stuff, "px" and 96 should get you going.
52 
53 
54 /* Example Usage:
55  // Load
56  SNVGImage* image;
57  image = nsvgParseFromFile("test.svg", "px", 96);
58  printf("size: %f x %f\n", image->width, image->height);
59  // Use...
60  for (shape = image->shapes; shape != NULL; shape = shape->next) {
61  for (path = shape->paths; path != NULL; path = path->next) {
62  for (i = 0; i < path->npts-1; i += 3) {
63  float* p = &path->pts[i*2];
64  drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]);
65  }
66  }
67  }
68  // Delete
69  nsvgDelete(image);
70 */
71 
72 enum NSVGpaintType {
73  NSVG_PAINT_NONE = 0,
74  NSVG_PAINT_COLOR = 1,
75  NSVG_PAINT_LINEAR_GRADIENT = 2,
76  NSVG_PAINT_RADIAL_GRADIENT = 3,
77 };
78 
79 enum NSVGspreadType {
80  NSVG_SPREAD_PAD = 0,
81  NSVG_SPREAD_REFLECT = 1,
82  NSVG_SPREAD_REPEAT = 2,
83 };
84 
85 enum NSVGlineJoin {
86  NSVG_JOIN_MITER = 0,
87  NSVG_JOIN_ROUND = 1,
88  NSVG_JOIN_BEVEL = 2,
89 };
90 
91 enum NSVGlineCap {
92  NSVG_CAP_BUTT = 0,
93  NSVG_CAP_ROUND = 1,
94  NSVG_CAP_SQUARE = 2,
95 };
96 
97 typedef struct NSVGgradientStop {
98  unsigned int color;
99  float offset;
101 
102 typedef struct NSVGgradient {
103  float xform[6];
104  char spread;
105  float fx, fy;
106  int nstops;
107  NSVGgradientStop stops[1];
108 } NSVGgradient;
109 
110 typedef struct NSVGpaint {
111  char type;
112  union {
113  unsigned int color;
114  NSVGgradient* gradient;
115  };
116 } NSVGpaint;
117 
118 typedef struct NSVGpath
119 {
120  float* pts; // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ...
121  int npts; // Total number of bezier points.
122  char closed; // Flag indicating if shapes should be treated as closed.
123  float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
124  struct NSVGpath* next; // Pointer to next path, or NULL if last element.
125 } NSVGpath;
126 
127 typedef struct NSVGshape
128 {
129  char id[64]; // Optional 'id' attr of the shape or its group
130  NSVGpaint fill; // Fill paint
131  NSVGpaint stroke; // Stroke paint
132  float opacity; // Opacity of the shape.
133  float strokeWidth; // Stroke width (scaled).
134  char strokeLineJoin; // Stroke join type.
135  char strokeLineCap; // Stroke cap type.
136  float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
137  NSVGpath* paths; // Linked list of paths in the image.
138  struct NSVGshape* next; // Pointer to next shape, or NULL if last element.
139 } NSVGshape;
140 
141 typedef struct NSVGimage
142 {
143  float width; // Width of the image.
144  float height; // Height of the image.
145  NSVGshape* shapes; // Linked list of shapes in the image.
146 } NSVGimage;
147 
148 // Parses SVG file from a file, returns SVG image as paths.
149 NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi);
150 
151 // Parses SVG file from a null terminated string, returns SVG image as paths.
152 NSVGimage* nsvgParse(char* input, const char* units, float dpi);
153 
154 // Deletes list of paths.
155 void nsvgDelete(NSVGimage* image);
156 
157 #ifdef __cplusplus
158 };
159 #endif
160 
161 #endif // NANOSVG_H
162 
163 #ifdef NANOSVG_IMPLEMENTATION
164 #endif
Definition: nanosvg.h:141
Definition: nanosvg.h:118
Definition: nanosvg.h:127
Definition: nanosvg.h:102
Definition: nanosvg.h:97
Definition: nanosvg.h:110