sf-svg
nanosvg.hpp
Go to the documentation of this file.
1 /* ================================================ *
2  * nanosvg++ *
3  * This software is a fork of nanosvg (note below). *
4  * No license-related aspects are affected. *
5  * Kamil Koczurek | koczurekk@gmail.com *
6  * ================================================ */
7 /* ================================================================================= *
8  * Copyright (c) 2013-14 Mikko Mononen memon@inside.org *
9  * *
10  * This software is provided 'as-is', without any express or implied *
11  * warranty. In no event will the authors be held liable for any damages *
12  * arising from the use of this software. *
13  * *
14  * Permission is granted to anyone to use this software for any purpose, *
15  * including commercial applications, and to alter it and redistribute it *
16  * freely, subject to the following restrictions: *
17  * *
18  * 1. The origin of this software must not be misrepresented; you must not *
19  * claim that you wrote the original software. If you use this software *
20  * in a product, an acknowledgment in the product documentation would be *
21  * appreciated but is not required. *
22  * 2. Altered source versions must be plainly marked as such, and must not be *
23  * misrepresented as being the original software. *
24  * 3. This notice may not be removed or altered from any source distribution. *
25  * *
26  * The SVG parser is based on Anti-Grain Geometry 2.4 SVG example *
27  * Copyright (C) 2002-2004 Maxim Shemanarev (McSeem) (http://www.antigrain.com/) *
28  * *
29  * Arc calculation code based on canvg (https://code.google.com/p/canvg/) *
30  * *
31  * Bounding box calculation based on *
32  * http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html *
33  * ================================================================================= */
34 
39 
40 #ifndef NANOSVG_H
41 #define NANOSVG_H
42 
43 #include <string.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <math.h>
47 
48 #include "enums.hpp"
49 
53 namespace nsvg {
54 
61 namespace cstyle {
62  /*
63  * Definitions
64  */
65  #define MAX_ATTR 128
66  #define NSVG_MAX_DASHES 8
67 
68  /*
69  * Structures
70  */
71  struct GradientStop {
72  unsigned int color;
73  float offset;
74  };
75 
76  struct Gradient {
77  float xform[6];
78  SpreadType spread;
79  float fx, fy;
80  int nstops;
81  GradientStop stops[1];
82  };
83 
84  struct Paint {
85  PaintType type;
86  union {
87  unsigned int color;
88  Gradient* gradient;
89  };
90  };
91 
92  struct PathStruct
93  {
94  float* pts; // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ...
95  int npts; // Total number of bezier points.
96  char closed; // Flag indicating if shapes should be treated as closed.
97  float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
98 
99  PathStruct* next; // Pointer to next path, or NULL if last element.
100  };
101 
102  struct ShapeStruct
103  {
104  char id[64]; // Optional 'id' attr of the shape or its group
105  Paint fill; // Fill paint
106  Paint stroke; // Stroke paint
107  float opacity; // Opacity of the shape.
108  float strokeWidth; // Stroke width (scaled).
109  float strokeDashOffset; // Stroke dash offset (scaled).
110  float strokeDashArray[8]; // Stroke dash array (scaled).
111  char strokeDashCount; // Number of dash values in dash array.
112  LineJoin strokeLineJoin; // Stroke join type.
113  LineCap strokeLineCap; // Stroke cap type.
114  FillRule fillRule; // Fill rule, see NSVGfillRule.
115  Flags flags; // Logical or of NSVG_FLAGS_* flags
116  float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
117 
118  PathStruct* paths; // Linked list of paths in the image.
119  ShapeStruct* next; // Pointer to next shape, or NULL if last element.
120  };
121 
122  struct ImageStruct
123  {
124  float width; // Width of the image.
125  float height; // Height of the image.
126  ShapeStruct* shapes; // Linked list of shapes in the image.
127  };
128 
129  struct Coordinate {
130  float value;
131  Units units;
132  };
133 
134  struct LinearData {
135  Coordinate x1, y1, x2, y2;
136  };
137 
138  struct RadialData {
139  Coordinate cx, cy, r, fx, fy;
140  };
141 
143  {
144  char id[64];
145  char ref[64];
146  PaintType type;
147  union {
148  LinearData linear;
149  RadialData radial;
150  };
151  SpreadType spread;
152  GradientUnits units;
153  float xform[6];
154  int nstops;
155  GradientStop* stops;
156  struct GradientData* next;
157  };
158 
159  struct Attrib
160  {
161  char id[64];
162  float xform[6];
163  unsigned int fillColor;
164  unsigned int strokeColor;
165  float opacity;
166  float fillOpacity;
167  float strokeOpacity;
168  char fillGradient[64];
169  char strokeGradient[64];
170  float strokeWidth;
171  float strokeDashOffset;
172  float strokeDashArray[NSVG_MAX_DASHES];
173  int strokeDashCount;
174  LineJoin strokeLineJoin;
175  LineCap strokeLineCap;
176  FillRule fillRule;
177  float fontSize;
178  unsigned int stopColor;
179  float stopOpacity;
180  float stopOffset;
181  char hasFill;
182  char hasStroke;
183  char visible;
184  };
185 
186  struct Parser
187  {
188  Attrib attr[MAX_ATTR];
189  int attrHead;
190  float* pts;
191  int npts;
192  int cpts;
193  PathStruct* plist;
194  ImageStruct* image;
195  GradientData* gradients;
196  float viewMinx, viewMiny, viewWidth, viewHeight;
197  int alignX, alignY, alignType;
198  float dpi;
199  char pathFlag;
200  char defsFlag;
201  };
202 
203  /*
204  * Functions
205  */
206  // Parses SVG file from a file, returns SVG image as paths.
207  ImageStruct* parseFromFile(const char* filename, const char* units, float dpi);
208 
209  // Parses SVG file from a null terminated string, returns SVG image as paths.
210  // Important note: changes the string.
211  ImageStruct* parse(char* input, const char* units, float dpi);
212 
213  // Deletes list of paths.
214  void deleteImage(ImageStruct* image);
215 }
216 }
217 #endif // NANOSVG_H
Definition: nanosvg.hpp:142
Definition: nanosvg.hpp:129
Definition: nanosvg.hpp:138
Nanosvg++ namespace.
Definition: enums.hpp:15
Definition: nanosvg.hpp:159
Definition: nanosvg.hpp:186
Definition: nanosvg.hpp:92
Definition: nanosvg.hpp:84
Definition: nanosvg.hpp:134
Definition: nanosvg.hpp:76
Definition: nanosvg.hpp:102
Enums definitions.
Units
Available units.
Definition: enums.hpp:60
Definition: nanosvg.hpp:122
Definition: nanosvg.hpp:71