ScummVM API documentation
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-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 #ifndef NANOSVG_CPLUSPLUS
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 #endif
37 
38 // NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes.
39 //
40 // The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.
41 //
42 // NanoSVG supports a wide range of SVG features, but something may be missing, feel free to create a pull request!
43 //
44 // The shapes in the SVG images are transformed by the viewBox and converted to specified units.
45 // That is, you should get the same looking data as your designed in your favorite app.
46 //
47 // NanoSVG can return the paths in few different units. For example if you want to render an image, you may choose
48 // to get the paths in pixels, or if you are feeding the data into a CNC-cutter, you may want to use millimeters.
49 //
50 // The units passed to NanoSVG should be one of: 'px', 'pt', 'pc' 'mm', 'cm', or 'in'.
51 // DPI (dots-per-inch) controls how the unit conversion is done.
52 //
53 // If you don't know or care about the units stuff, "px" and 96 should get you going.
54 
55 
56 /* Example Usage:
57  // Load SVG
58  NSVGimage* image;
59  image = nsvgParseFromFile("test.svg", "px", 96);
60  printf("size: %f x %f\n", image->width, image->height);
61  // Use...
62  for (NSVGshape *shape = image->shapes; shape != NULL; shape = shape->next) {
63  for (NSVGpath *path = shape->paths; path != NULL; path = path->next) {
64  for (int i = 0; i < path->npts-1; i += 3) {
65  float* p = &path->pts[i*2];
66  drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]);
67  }
68  }
69  }
70  // Delete
71  nsvgDelete(image);
72 */
73 
74 enum NSVGpaintType {
75  NSVG_PAINT_NONE = 0,
76  NSVG_PAINT_COLOR = 1,
77  NSVG_PAINT_LINEAR_GRADIENT = 2,
78  NSVG_PAINT_RADIAL_GRADIENT = 3
79 };
80 
81 enum NSVGspreadType {
82  NSVG_SPREAD_PAD = 0,
83  NSVG_SPREAD_REFLECT = 1,
84  NSVG_SPREAD_REPEAT = 2
85 };
86 
87 enum NSVGlineJoin {
88  NSVG_JOIN_MITER = 0,
89  NSVG_JOIN_ROUND = 1,
90  NSVG_JOIN_BEVEL = 2
91 };
92 
93 enum NSVGlineCap {
94  NSVG_CAP_BUTT = 0,
95  NSVG_CAP_ROUND = 1,
96  NSVG_CAP_SQUARE = 2
97 };
98 
99 enum NSVGfillRule {
100  NSVG_FILLRULE_NONZERO = 0,
101  NSVG_FILLRULE_EVENODD = 1
102 };
103 
104 enum NSVGflags {
105  NSVG_FLAGS_VISIBLE = 0x01
106 };
107 
108 typedef struct NSVGgradientStop {
109  unsigned int color;
110  float offset;
112 
113 typedef struct NSVGgradient {
114  float xform[6];
115  char spread;
116  float fx, fy;
117  int nstops;
118  NSVGgradientStop stops[1];
119 } NSVGgradient;
120 
121 typedef struct NSVGpaint {
122  char type;
123  union {
124  unsigned int color;
125  NSVGgradient* gradient;
126  };
127 } NSVGpaint;
128 
129 typedef struct NSVGpath
130 {
131  float* pts; // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ...
132  int npts; // Total number of bezier points.
133  char closed; // Flag indicating if shapes should be treated as closed.
134  float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
135  struct NSVGpath* next; // Pointer to next path, or NULL if last element.
136 } NSVGpath;
137 
138 typedef struct NSVGshape
139 {
140  char id[64]; // Optional 'id' attr of the shape or its group
141  NSVGpaint fill; // Fill paint
142  NSVGpaint stroke; // Stroke paint
143  float opacity; // Opacity of the shape.
144  float strokeWidth; // Stroke width (scaled).
145  float strokeDashOffset; // Stroke dash offset (scaled).
146  float strokeDashArray[8]; // Stroke dash array (scaled).
147  char strokeDashCount; // Number of dash values in dash array.
148  char strokeLineJoin; // Stroke join type.
149  char strokeLineCap; // Stroke cap type.
150  float miterLimit; // Miter limit
151  char fillRule; // Fill rule, see NSVGfillRule.
152  unsigned char flags; // Logical or of NSVG_FLAGS_* flags
153  float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
154  NSVGpath* paths; // Linked list of paths in the image.
155  struct NSVGshape* next; // Pointer to next shape, or NULL if last element.
156 } NSVGshape;
157 
158 typedef struct NSVGimage
159 {
160  float width; // Width of the image.
161  float height; // Height of the image.
162  NSVGshape* shapes; // Linked list of shapes in the image.
163 } NSVGimage;
164 
165 // Parses SVG file from a file, returns SVG image as paths.
166 NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi);
167 
168 // Parses SVG file from a null terminated string, returns SVG image as paths.
169 // Important note: changes the string.
170 NSVGimage* nsvgParse(char* input, const char* units, float dpi);
171 
172 // Duplicates a path.
173 NSVGpath* nsvgDuplicatePath(NSVGpath* p);
174 
175 // Deletes an image.
176 void nsvgDelete(NSVGimage* image);
177 
178 #ifndef NANOSVG_CPLUSPLUS
179 #ifdef __cplusplus
180 }
181 #endif
182 #endif
183 
184 #endif // NANOSVG_H
185 
186 #ifdef NANOSVG_IMPLEMENTATION
187 
188 #include <string.h>
189 #include <stdlib.h>
190 #include <math.h>
191 
192 #define NSVG_PI (3.14159265358979323846264338327f)
193 #define NSVG_KAPPA90 (0.5522847493f) // Length proportional to radius of a cubic bezier handle for 90deg arcs.
194 
195 #define NSVG_ALIGN_MIN 0
196 #define NSVG_ALIGN_MID 1
197 #define NSVG_ALIGN_MAX 2
198 #define NSVG_ALIGN_NONE 0
199 #define NSVG_ALIGN_MEET 1
200 #define NSVG_ALIGN_SLICE 2
201 
202 //#define NSVG_NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0)
203 #define NSVG_NOTUSED(v) do { (void)(v); } while(0)
204 #define NSVG_RGB(r, g, b) (((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16))
205 
206 #ifdef _MSC_VER
207  #pragma warning (disable: 4996) // Switch off security warnings
208  #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
209  #ifdef __cplusplus
210  #define NSVG_INLINE inline
211  #else
212  #define NSVG_INLINE
213  #endif
214 #else
215  #define NSVG_INLINE inline
216 #endif
217 
218 
219 static int nsvg__isspace(char c)
220 {
221  return strchr(" \t\n\v\f\r", c) != 0;
222 }
223 
224 static int nsvg__isdigit(char c)
225 {
226  return c >= '0' && c <= '9';
227 }
228 
229 static NSVG_INLINE float nsvg__minf(float a, float b) { return a < b ? a : b; }
230 static NSVG_INLINE float nsvg__maxf(float a, float b) { return a > b ? a : b; }
231 
232 
233 // Simple XML parser
234 
235 #define NSVG_XML_TAG 1
236 #define NSVG_XML_CONTENT 2
237 #define NSVG_XML_MAX_ATTRIBS 256
238 
239 static void nsvg__parseContent(char* s,
240  void (*contentCb)(void* ud, const char* s),
241  void* ud)
242 {
243  // Trim start white spaces
244  while (*s && nsvg__isspace(*s)) s++;
245  if (!*s) return;
246 
247  if (contentCb)
248  (*contentCb)(ud, s);
249 }
250 
251 static void nsvg__parseElement(char* s,
252  void (*startelCb)(void* ud, const char* el, const char** attr),
253  void (*endelCb)(void* ud, const char* el),
254  void* ud)
255 {
256  const char* attr[NSVG_XML_MAX_ATTRIBS];
257  int nattr = 0;
258  char* name;
259  int start = 0;
260  int end = 0;
261  char quote;
262 
263  // Skip white space after the '<'
264  while (*s && nsvg__isspace(*s)) s++;
265 
266  // Check if the tag is end tag
267  if (*s == '/') {
268  s++;
269  end = 1;
270  } else {
271  start = 1;
272  }
273 
274  // Skip comments, data and preprocessor stuff.
275  if (!*s || *s == '?' || *s == '!')
276  return;
277 
278  // Get tag name
279  name = s;
280  while (*s && !nsvg__isspace(*s)) s++;
281  if (*s) { *s++ = '\0'; }
282 
283  // Get attribs
284  while (!end && *s && nattr < NSVG_XML_MAX_ATTRIBS-3) {
285  char* name_ = NULL;
286  char* value = NULL;
287 
288  // Skip white space before the attrib name
289  while (*s && nsvg__isspace(*s)) s++;
290  if (!*s) break;
291  if (*s == '/') {
292  end = 1;
293  break;
294  }
295  name_ = s;
296  // Find end of the attrib name.
297  while (*s && !nsvg__isspace(*s) && *s != '=') s++;
298  if (*s) { *s++ = '\0'; }
299  // Skip until the beginning of the value.
300  while (*s && *s != '\"' && *s != '\'') s++;
301  if (!*s) break;
302  quote = *s;
303  s++;
304  // Store value and find the end of it.
305  value = s;
306  while (*s && *s != quote) s++;
307  if (*s) { *s++ = '\0'; }
308 
309  // Store only well formed attributes
310  if (name_ && value) {
311  attr[nattr++] = name_;
312  attr[nattr++] = value;
313  }
314  }
315 
316  // List terminator
317  attr[nattr++] = 0;
318  attr[nattr++] = 0;
319 
320  // Call callbacks.
321  if (start && startelCb)
322  (*startelCb)(ud, name, attr);
323  if (end && endelCb)
324  (*endelCb)(ud, name);
325 }
326 
327 int nsvg__parseXML(char* input,
328  void (*startelCb)(void* ud, const char* el, const char** attr),
329  void (*endelCb)(void* ud, const char* el),
330  void (*contentCb)(void* ud, const char* s),
331  void* ud)
332 {
333  char* s = input;
334  char* mark = s;
335  int state = NSVG_XML_CONTENT;
336  while (*s) {
337  if (*s == '<' && state == NSVG_XML_CONTENT) {
338  // Start of a tag
339  *s++ = '\0';
340  nsvg__parseContent(mark, contentCb, ud);
341  mark = s;
342  state = NSVG_XML_TAG;
343  } else if (*s == '>' && state == NSVG_XML_TAG) {
344  // Start of a content or new tag.
345  *s++ = '\0';
346  nsvg__parseElement(mark, startelCb, endelCb, ud);
347  mark = s;
348  state = NSVG_XML_CONTENT;
349  } else {
350  s++;
351  }
352  }
353 
354  return 1;
355 }
356 
357 
358 /* Simple SVG parser. */
359 
360 #define NSVG_MAX_ATTR 128
361 
362 enum NSVGgradientUnits {
363  NSVG_USER_SPACE = 0,
364  NSVG_OBJECT_SPACE = 1
365 };
366 
367 #define NSVG_MAX_DASHES 8
368 
369 enum NSVGunits {
370  NSVG_UNITS_USER,
371  NSVG_UNITS_PX,
372  NSVG_UNITS_PT,
373  NSVG_UNITS_PC,
374  NSVG_UNITS_MM,
375  NSVG_UNITS_CM,
376  NSVG_UNITS_IN,
377  NSVG_UNITS_PERCENT,
378  NSVG_UNITS_EM,
379  NSVG_UNITS_EX
380 };
381 
382 typedef struct NSVGcoordinate {
383  float value;
384  int units;
385 } NSVGcoordinate;
386 
387 typedef struct NSVGlinearData {
388  NSVGcoordinate x1, y1, x2, y2;
389 } NSVGlinearData;
390 
391 typedef struct NSVGradialData {
392  NSVGcoordinate cx, cy, r, fx, fy;
393 } NSVGradialData;
394 
395 typedef struct NSVGgradientData
396 {
397  char id[64];
398  char ref[64];
399  char type;
400  union {
401  NSVGlinearData linear;
402  NSVGradialData radial;
403  };
404  char spread;
405  char units;
406  float xform[6];
407  int nstops;
408  NSVGgradientStop* stops;
409  struct NSVGgradientData* next;
410 } NSVGgradientData;
411 
412 typedef struct NSVGattrib
413 {
414  char id[64];
415  float xform[6];
416  unsigned int fillColor;
417  unsigned int strokeColor;
418  float opacity;
419  float fillOpacity;
420  float strokeOpacity;
421  char fillGradient[64];
422  char strokeGradient[64];
423  float strokeWidth;
424  float strokeDashOffset;
425  float strokeDashArray[NSVG_MAX_DASHES];
426  int strokeDashCount;
427  char strokeLineJoin;
428  char strokeLineCap;
429  float miterLimit;
430  char fillRule;
431  float fontSize;
432  unsigned int stopColor;
433  float stopOpacity;
434  float stopOffset;
435  char hasFill;
436  char hasStroke;
437  char visible;
438 } NSVGattrib;
439 
440 typedef struct NSVGparser
441 {
442  NSVGattrib attr[NSVG_MAX_ATTR];
443  int attrHead;
444  float* pts;
445  int npts;
446  int cpts;
447  NSVGpath* plist;
448  NSVGimage* image;
449  NSVGgradientData* gradients;
450  NSVGshape* shapesTail;
451  float viewMinx, viewMiny, viewWidth, viewHeight;
452  int alignX, alignY, alignType;
453  float dpi;
454  char pathFlag;
455  char defsFlag;
456 } NSVGparser;
457 
458 static void nsvg__xformIdentity(float* t)
459 {
460  t[0] = 1.0f; t[1] = 0.0f;
461  t[2] = 0.0f; t[3] = 1.0f;
462  t[4] = 0.0f; t[5] = 0.0f;
463 }
464 
465 static void nsvg__xformSetTranslation(float* t, float tx, float ty)
466 {
467  t[0] = 1.0f; t[1] = 0.0f;
468  t[2] = 0.0f; t[3] = 1.0f;
469  t[4] = tx; t[5] = ty;
470 }
471 
472 static void nsvg__xformSetScale(float* t, float sx, float sy)
473 {
474  t[0] = sx; t[1] = 0.0f;
475  t[2] = 0.0f; t[3] = sy;
476  t[4] = 0.0f; t[5] = 0.0f;
477 }
478 
479 static void nsvg__xformSetSkewX(float* t, float a)
480 {
481  t[0] = 1.0f; t[1] = 0.0f;
482  t[2] = tanf(a); t[3] = 1.0f;
483  t[4] = 0.0f; t[5] = 0.0f;
484 }
485 
486 static void nsvg__xformSetSkewY(float* t, float a)
487 {
488  t[0] = 1.0f; t[1] = tanf(a);
489  t[2] = 0.0f; t[3] = 1.0f;
490  t[4] = 0.0f; t[5] = 0.0f;
491 }
492 
493 static void nsvg__xformSetRotation(float* t, float a)
494 {
495  float cs = cosf(a), sn = sinf(a);
496  t[0] = cs; t[1] = sn;
497  t[2] = -sn; t[3] = cs;
498  t[4] = 0.0f; t[5] = 0.0f;
499 }
500 
501 static void nsvg__xformMultiply(float* t, float* s)
502 {
503  float t0 = t[0] * s[0] + t[1] * s[2];
504  float t2 = t[2] * s[0] + t[3] * s[2];
505  float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
506  t[1] = t[0] * s[1] + t[1] * s[3];
507  t[3] = t[2] * s[1] + t[3] * s[3];
508  t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
509  t[0] = t0;
510  t[2] = t2;
511  t[4] = t4;
512 }
513 
514 static void nsvg__xformInverse(float* inv, float* t)
515 {
516  double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1];
517  if (det > -1e-6 && det < 1e-6) {
518  nsvg__xformIdentity(t);
519  return;
520  }
521  invdet = 1.0 / det;
522  inv[0] = (float)(t[3] * invdet);
523  inv[2] = (float)(-t[2] * invdet);
524  inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
525  inv[1] = (float)(-t[1] * invdet);
526  inv[3] = (float)(t[0] * invdet);
527  inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
528 }
529 
530 static void nsvg__xformPremultiply(float* t, float* s)
531 {
532  float s2[6];
533  memcpy(s2, s, sizeof(float)*6);
534  nsvg__xformMultiply(s2, t);
535  memcpy(t, s2, sizeof(float)*6);
536 }
537 
538 static void nsvg__xformPoint(float* dx, float* dy, float x, float y, float* t)
539 {
540  *dx = x*t[0] + y*t[2] + t[4];
541  *dy = x*t[1] + y*t[3] + t[5];
542 }
543 
544 static void nsvg__xformVec(float* dx, float* dy, float x, float y, float* t)
545 {
546  *dx = x*t[0] + y*t[2];
547  *dy = x*t[1] + y*t[3];
548 }
549 
550 #define NSVG_EPSILON (1e-12)
551 
552 static int nsvg__ptInBounds(float* pt, float* bounds)
553 {
554  return pt[0] >= bounds[0] && pt[0] <= bounds[2] && pt[1] >= bounds[1] && pt[1] <= bounds[3];
555 }
556 
557 
558 static double nsvg__evalBezier(double t, double p0, double p1, double p2, double p3)
559 {
560  double it = 1.0-t;
561  return it*it*it*p0 + 3.0*it*it*t*p1 + 3.0*it*t*t*p2 + t*t*t*p3;
562 }
563 
564 static void nsvg__curveBounds(float* bounds, float* curve)
565 {
566  int i, j, count;
567  double roots[2], a, b, c, b2ac, t, v;
568  float* v0 = &curve[0];
569  float* v1 = &curve[2];
570  float* v2 = &curve[4];
571  float* v3 = &curve[6];
572 
573  // Start the bounding box by end points
574  bounds[0] = nsvg__minf(v0[0], v3[0]);
575  bounds[1] = nsvg__minf(v0[1], v3[1]);
576  bounds[2] = nsvg__maxf(v0[0], v3[0]);
577  bounds[3] = nsvg__maxf(v0[1], v3[1]);
578 
579  // Bezier curve fits inside the convex hull of it's control points.
580  // If control points are inside the bounds, we're done.
581  if (nsvg__ptInBounds(v1, bounds) && nsvg__ptInBounds(v2, bounds))
582  return;
583 
584  // Add bezier curve inflection points in X and Y.
585  for (i = 0; i < 2; i++) {
586  a = -3.0 * v0[i] + 9.0 * v1[i] - 9.0 * v2[i] + 3.0 * v3[i];
587  b = 6.0 * v0[i] - 12.0 * v1[i] + 6.0 * v2[i];
588  c = 3.0 * v1[i] - 3.0 * v0[i];
589  count = 0;
590  if (fabs(a) < NSVG_EPSILON) {
591  if (fabs(b) > NSVG_EPSILON) {
592  t = -c / b;
593  if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
594  roots[count++] = t;
595  }
596  } else {
597  b2ac = b*b - 4.0*c*a;
598  if (b2ac > NSVG_EPSILON) {
599  t = (-b + sqrt(b2ac)) / (2.0 * a);
600  if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
601  roots[count++] = t;
602  t = (-b - sqrt(b2ac)) / (2.0 * a);
603  if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
604  roots[count++] = t;
605  }
606  }
607  for (j = 0; j < count; j++) {
608  v = nsvg__evalBezier(roots[j], v0[i], v1[i], v2[i], v3[i]);
609  bounds[0+i] = nsvg__minf(bounds[0+i], (float)v);
610  bounds[2+i] = nsvg__maxf(bounds[2+i], (float)v);
611  }
612  }
613 }
614 
615 static NSVGparser* nsvg__createParser()
616 {
617  NSVGparser* p;
618  p = (NSVGparser*)malloc(sizeof(NSVGparser));
619  if (p == NULL) goto error;
620  memset(p, 0, sizeof(NSVGparser));
621 
622  p->image = (NSVGimage*)malloc(sizeof(NSVGimage));
623  if (p->image == NULL) goto error;
624  memset(p->image, 0, sizeof(NSVGimage));
625 
626  // Init style
627  nsvg__xformIdentity(p->attr[0].xform);
628  memset(p->attr[0].id, 0, sizeof p->attr[0].id);
629  p->attr[0].fillColor = NSVG_RGB(0,0,0);
630  p->attr[0].strokeColor = NSVG_RGB(0,0,0);
631  p->attr[0].opacity = 1;
632  p->attr[0].fillOpacity = 1;
633  p->attr[0].strokeOpacity = 1;
634  p->attr[0].stopOpacity = 1;
635  p->attr[0].strokeWidth = 1;
636  p->attr[0].strokeLineJoin = NSVG_JOIN_MITER;
637  p->attr[0].strokeLineCap = NSVG_CAP_BUTT;
638  p->attr[0].miterLimit = 4;
639  p->attr[0].fillRule = NSVG_FILLRULE_NONZERO;
640  p->attr[0].hasFill = 1;
641  p->attr[0].visible = 1;
642 
643  return p;
644 
645 error:
646  if (p) {
647  if (p->image) free(p->image);
648  free(p);
649  }
650  return NULL;
651 }
652 
653 static void nsvg__deletePaths(NSVGpath* path)
654 {
655  while (path) {
656  NSVGpath *next = path->next;
657  if (path->pts != NULL)
658  free(path->pts);
659  free(path);
660  path = next;
661  }
662 }
663 
664 static void nsvg__deletePaint(NSVGpaint* paint)
665 {
666  if (paint->type == NSVG_PAINT_LINEAR_GRADIENT || paint->type == NSVG_PAINT_RADIAL_GRADIENT)
667  free(paint->gradient);
668 }
669 
670 static void nsvg__deleteGradientData(NSVGgradientData* grad)
671 {
672  NSVGgradientData* next;
673  while (grad != NULL) {
674  next = grad->next;
675  free(grad->stops);
676  free(grad);
677  grad = next;
678  }
679 }
680 
681 static void nsvg__deleteParser(NSVGparser* p)
682 {
683  if (p != NULL) {
684  nsvg__deletePaths(p->plist);
685  nsvg__deleteGradientData(p->gradients);
686  nsvgDelete(p->image);
687  free(p->pts);
688  free(p);
689  }
690 }
691 
692 static void nsvg__resetPath(NSVGparser* p)
693 {
694  p->npts = 0;
695 }
696 
697 static void nsvg__addPoint(NSVGparser* p, float x, float y)
698 {
699  if (p->npts+1 > p->cpts) {
700  p->cpts = p->cpts ? p->cpts*2 : 8;
701  p->pts = (float*)realloc(p->pts, p->cpts*2*sizeof(float));
702  if (!p->pts) return;
703  }
704  p->pts[p->npts*2+0] = x;
705  p->pts[p->npts*2+1] = y;
706  p->npts++;
707 }
708 
709 static void nsvg__moveTo(NSVGparser* p, float x, float y)
710 {
711  if (p->npts > 0) {
712  p->pts[(p->npts-1)*2+0] = x;
713  p->pts[(p->npts-1)*2+1] = y;
714  } else {
715  nsvg__addPoint(p, x, y);
716  }
717 }
718 
719 static void nsvg__lineTo(NSVGparser* p, float x, float y)
720 {
721  float px,py, dx,dy;
722  if (p->npts > 0) {
723  px = p->pts[(p->npts-1)*2+0];
724  py = p->pts[(p->npts-1)*2+1];
725  dx = x - px;
726  dy = y - py;
727  nsvg__addPoint(p, px + dx/3.0f, py + dy/3.0f);
728  nsvg__addPoint(p, x - dx/3.0f, y - dy/3.0f);
729  nsvg__addPoint(p, x, y);
730  }
731 }
732 
733 static void nsvg__cubicBezTo(NSVGparser* p, float cpx1, float cpy1, float cpx2, float cpy2, float x, float y)
734 {
735  if (p->npts > 0) {
736  nsvg__addPoint(p, cpx1, cpy1);
737  nsvg__addPoint(p, cpx2, cpy2);
738  nsvg__addPoint(p, x, y);
739  }
740 }
741 
742 static NSVGattrib* nsvg__getAttr(NSVGparser* p)
743 {
744  return &p->attr[p->attrHead];
745 }
746 
747 static void nsvg__pushAttr(NSVGparser* p)
748 {
749  if (p->attrHead < NSVG_MAX_ATTR-1) {
750  p->attrHead++;
751  memcpy(&p->attr[p->attrHead], &p->attr[p->attrHead-1], sizeof(NSVGattrib));
752  }
753 }
754 
755 static void nsvg__popAttr(NSVGparser* p)
756 {
757  if (p->attrHead > 0)
758  p->attrHead--;
759 }
760 
761 static float nsvg__actualOrigX(NSVGparser* p)
762 {
763  return p->viewMinx;
764 }
765 
766 static float nsvg__actualOrigY(NSVGparser* p)
767 {
768  return p->viewMiny;
769 }
770 
771 static float nsvg__actualWidth(NSVGparser* p)
772 {
773  return p->viewWidth;
774 }
775 
776 static float nsvg__actualHeight(NSVGparser* p)
777 {
778  return p->viewHeight;
779 }
780 
781 static float nsvg__actualLength(NSVGparser* p)
782 {
783  float w = nsvg__actualWidth(p), h = nsvg__actualHeight(p);
784  return sqrtf(w*w + h*h) / sqrtf(2.0f);
785 }
786 
787 static float nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c, float orig, float length)
788 {
789  NSVGattrib* attr = nsvg__getAttr(p);
790  switch (c.units) {
791  case NSVG_UNITS_USER: return c.value;
792  case NSVG_UNITS_PX: return c.value;
793  case NSVG_UNITS_PT: return c.value / 72.0f * p->dpi;
794  case NSVG_UNITS_PC: return c.value / 6.0f * p->dpi;
795  case NSVG_UNITS_MM: return c.value / 25.4f * p->dpi;
796  case NSVG_UNITS_CM: return c.value / 2.54f * p->dpi;
797  case NSVG_UNITS_IN: return c.value * p->dpi;
798  case NSVG_UNITS_EM: return c.value * attr->fontSize;
799  case NSVG_UNITS_EX: return c.value * attr->fontSize * 0.52f; // x-height of Helvetica.
800  case NSVG_UNITS_PERCENT: return orig + c.value / 100.0f * length;
801  default: return c.value;
802  }
803  return c.value;
804 }
805 
806 static NSVGgradientData* nsvg__findGradientData(NSVGparser* p, const char* id)
807 {
808  NSVGgradientData* grad = p->gradients;
809  if (id == NULL || *id == '\0')
810  return NULL;
811  while (grad != NULL) {
812  if (strcmp(grad->id, id) == 0)
813  return grad;
814  grad = grad->next;
815  }
816  return NULL;
817 }
818 
819 static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const float* localBounds, char* paintType)
820 {
821  NSVGattrib* attr = nsvg__getAttr(p);
822  NSVGgradientData* data = NULL;
823  NSVGgradientData* ref = NULL;
824  NSVGgradientStop* stops = NULL;
825  NSVGgradient* grad;
826  float ox, oy, sw, sh, sl;
827  int nstops = 0;
828  int refIter;
829 
830  data = nsvg__findGradientData(p, id);
831  if (data == NULL) return NULL;
832 
833  // TODO: use ref to fill in all unset values too.
834  ref = data;
835  refIter = 0;
836  while (ref != NULL) {
837  NSVGgradientData* nextRef = NULL;
838  if (stops == NULL && ref->stops != NULL) {
839  stops = ref->stops;
840  nstops = ref->nstops;
841  break;
842  }
843  nextRef = nsvg__findGradientData(p, ref->ref);
844  if (nextRef == ref) break; // prevent infite loops on malformed data
845  ref = nextRef;
846  refIter++;
847  if (refIter > 32) break; // prevent infite loops on malformed data
848  }
849  if (stops == NULL) return NULL;
850 
851  grad = (NSVGgradient*)malloc(sizeof(NSVGgradient) + sizeof(NSVGgradientStop)*(nstops-1));
852  if (grad == NULL) return NULL;
853 
854  // The shape width and height.
855  if (data->units == NSVG_OBJECT_SPACE) {
856  ox = localBounds[0];
857  oy = localBounds[1];
858  sw = localBounds[2] - localBounds[0];
859  sh = localBounds[3] - localBounds[1];
860  } else {
861  ox = nsvg__actualOrigX(p);
862  oy = nsvg__actualOrigY(p);
863  sw = nsvg__actualWidth(p);
864  sh = nsvg__actualHeight(p);
865  }
866  sl = sqrtf(sw*sw + sh*sh) / sqrtf(2.0f);
867 
868  if (data->type == NSVG_PAINT_LINEAR_GRADIENT) {
869  float x1, y1, x2, y2, dx, dy;
870  x1 = nsvg__convertToPixels(p, data->linear.x1, ox, sw);
871  y1 = nsvg__convertToPixels(p, data->linear.y1, oy, sh);
872  x2 = nsvg__convertToPixels(p, data->linear.x2, ox, sw);
873  y2 = nsvg__convertToPixels(p, data->linear.y2, oy, sh);
874  // Calculate transform aligned to the line
875  dx = x2 - x1;
876  dy = y2 - y1;
877  grad->xform[0] = dy; grad->xform[1] = -dx;
878  grad->xform[2] = dx; grad->xform[3] = dy;
879  grad->xform[4] = x1; grad->xform[5] = y1;
880  } else {
881  float cx, cy, fx, fy, r;
882  cx = nsvg__convertToPixels(p, data->radial.cx, ox, sw);
883  cy = nsvg__convertToPixels(p, data->radial.cy, oy, sh);
884  fx = nsvg__convertToPixels(p, data->radial.fx, ox, sw);
885  fy = nsvg__convertToPixels(p, data->radial.fy, oy, sh);
886  r = nsvg__convertToPixels(p, data->radial.r, 0, sl);
887  // Calculate transform aligned to the circle
888  grad->xform[0] = r; grad->xform[1] = 0;
889  grad->xform[2] = 0; grad->xform[3] = r;
890  grad->xform[4] = cx; grad->xform[5] = cy;
891  grad->fx = fx / r;
892  grad->fy = fy / r;
893  }
894 
895  nsvg__xformMultiply(grad->xform, data->xform);
896  nsvg__xformMultiply(grad->xform, attr->xform);
897 
898  grad->spread = data->spread;
899  memcpy(grad->stops, stops, nstops*sizeof(NSVGgradientStop));
900  grad->nstops = nstops;
901 
902  *paintType = data->type;
903 
904  return grad;
905 }
906 
907 static float nsvg__getAverageScale(float* t)
908 {
909  float sx = sqrtf(t[0]*t[0] + t[2]*t[2]);
910  float sy = sqrtf(t[1]*t[1] + t[3]*t[3]);
911  return (sx + sy) * 0.5f;
912 }
913 
914 static void nsvg__getLocalBounds(float* bounds, NSVGshape *shape, float* xform)
915 {
916  NSVGpath* path;
917  float curve[4*2], curveBounds[4];
918  int i, first = 1;
919  for (path = shape->paths; path != NULL; path = path->next) {
920  nsvg__xformPoint(&curve[0], &curve[1], path->pts[0], path->pts[1], xform);
921  for (i = 0; i < path->npts-1; i += 3) {
922  nsvg__xformPoint(&curve[2], &curve[3], path->pts[(i+1)*2], path->pts[(i+1)*2+1], xform);
923  nsvg__xformPoint(&curve[4], &curve[5], path->pts[(i+2)*2], path->pts[(i+2)*2+1], xform);
924  nsvg__xformPoint(&curve[6], &curve[7], path->pts[(i+3)*2], path->pts[(i+3)*2+1], xform);
925  nsvg__curveBounds(curveBounds, curve);
926  if (first) {
927  bounds[0] = curveBounds[0];
928  bounds[1] = curveBounds[1];
929  bounds[2] = curveBounds[2];
930  bounds[3] = curveBounds[3];
931  first = 0;
932  } else {
933  bounds[0] = nsvg__minf(bounds[0], curveBounds[0]);
934  bounds[1] = nsvg__minf(bounds[1], curveBounds[1]);
935  bounds[2] = nsvg__maxf(bounds[2], curveBounds[2]);
936  bounds[3] = nsvg__maxf(bounds[3], curveBounds[3]);
937  }
938  curve[0] = curve[6];
939  curve[1] = curve[7];
940  }
941  }
942 }
943 
944 static void nsvg__addShape(NSVGparser* p)
945 {
946  NSVGattrib* attr = nsvg__getAttr(p);
947  float scale = 1.0f;
948  NSVGshape* shape;
949  NSVGpath* path;
950  int i;
951 
952  if (p->plist == NULL)
953  return;
954 
955  shape = (NSVGshape*)malloc(sizeof(NSVGshape));
956  if (shape == NULL) goto error;
957  memset(shape, 0, sizeof(NSVGshape));
958 
959  memcpy(shape->id, attr->id, sizeof shape->id);
960  scale = nsvg__getAverageScale(attr->xform);
961  shape->strokeWidth = attr->strokeWidth * scale;
962  shape->strokeDashOffset = attr->strokeDashOffset * scale;
963  shape->strokeDashCount = (char)attr->strokeDashCount;
964  for (i = 0; i < attr->strokeDashCount; i++)
965  shape->strokeDashArray[i] = attr->strokeDashArray[i] * scale;
966  shape->strokeLineJoin = attr->strokeLineJoin;
967  shape->strokeLineCap = attr->strokeLineCap;
968  shape->miterLimit = attr->miterLimit;
969  shape->fillRule = attr->fillRule;
970  shape->opacity = attr->opacity;
971 
972  shape->paths = p->plist;
973  p->plist = NULL;
974 
975  // Calculate shape bounds
976  shape->bounds[0] = shape->paths->bounds[0];
977  shape->bounds[1] = shape->paths->bounds[1];
978  shape->bounds[2] = shape->paths->bounds[2];
979  shape->bounds[3] = shape->paths->bounds[3];
980  for (path = shape->paths->next; path != NULL; path = path->next) {
981  shape->bounds[0] = nsvg__minf(shape->bounds[0], path->bounds[0]);
982  shape->bounds[1] = nsvg__minf(shape->bounds[1], path->bounds[1]);
983  shape->bounds[2] = nsvg__maxf(shape->bounds[2], path->bounds[2]);
984  shape->bounds[3] = nsvg__maxf(shape->bounds[3], path->bounds[3]);
985  }
986 
987  // Set fill
988  if (attr->hasFill == 0) {
989  shape->fill.type = NSVG_PAINT_NONE;
990  } else if (attr->hasFill == 1) {
991  shape->fill.type = NSVG_PAINT_COLOR;
992  shape->fill.color = attr->fillColor;
993  shape->fill.color |= (unsigned int)(attr->fillOpacity*255) << 24;
994  } else if (attr->hasFill == 2) {
995  float inv[6], localBounds[4];
996  nsvg__xformInverse(inv, attr->xform);
997  nsvg__getLocalBounds(localBounds, shape, inv);
998  shape->fill.gradient = nsvg__createGradient(p, attr->fillGradient, localBounds, &shape->fill.type);
999  if (shape->fill.gradient == NULL) {
1000  shape->fill.type = NSVG_PAINT_NONE;
1001  }
1002  }
1003 
1004  // Set stroke
1005  if (attr->hasStroke == 0) {
1006  shape->stroke.type = NSVG_PAINT_NONE;
1007  } else if (attr->hasStroke == 1) {
1008  shape->stroke.type = NSVG_PAINT_COLOR;
1009  shape->stroke.color = attr->strokeColor;
1010  shape->stroke.color |= (unsigned int)(attr->strokeOpacity*255) << 24;
1011  } else if (attr->hasStroke == 2) {
1012  float inv[6], localBounds[4];
1013  nsvg__xformInverse(inv, attr->xform);
1014  nsvg__getLocalBounds(localBounds, shape, inv);
1015  shape->stroke.gradient = nsvg__createGradient(p, attr->strokeGradient, localBounds, &shape->stroke.type);
1016  if (shape->stroke.gradient == NULL)
1017  shape->stroke.type = NSVG_PAINT_NONE;
1018  }
1019 
1020  // Set flags
1021  shape->flags = (attr->visible ? NSVG_FLAGS_VISIBLE : 0x00);
1022 
1023  // Add to tail
1024  if (p->image->shapes == NULL)
1025  p->image->shapes = shape;
1026  else
1027  p->shapesTail->next = shape;
1028  p->shapesTail = shape;
1029 
1030  return;
1031 
1032 error:
1033  if (shape) free(shape);
1034 }
1035 
1036 static void nsvg__addPath(NSVGparser* p, char closed)
1037 {
1038  NSVGattrib* attr = nsvg__getAttr(p);
1039  NSVGpath* path = NULL;
1040  float bounds[4];
1041  float* curve;
1042  int i;
1043 
1044  if (p->npts < 4)
1045  return;
1046 
1047  if (closed)
1048  nsvg__lineTo(p, p->pts[0], p->pts[1]);
1049 
1050  // Expect 1 + N*3 points (N = number of cubic bezier segments).
1051  if ((p->npts % 3) != 1)
1052  return;
1053 
1054  path = (NSVGpath*)malloc(sizeof(NSVGpath));
1055  if (path == NULL) goto error;
1056  memset(path, 0, sizeof(NSVGpath));
1057 
1058  path->pts = (float*)malloc(p->npts*2*sizeof(float));
1059  if (path->pts == NULL) goto error;
1060  path->closed = closed;
1061  path->npts = p->npts;
1062 
1063  // Transform path.
1064  for (i = 0; i < p->npts; ++i)
1065  nsvg__xformPoint(&path->pts[i*2], &path->pts[i*2+1], p->pts[i*2], p->pts[i*2+1], attr->xform);
1066 
1067  // Find bounds
1068  for (i = 0; i < path->npts-1; i += 3) {
1069  curve = &path->pts[i*2];
1070  nsvg__curveBounds(bounds, curve);
1071  if (i == 0) {
1072  path->bounds[0] = bounds[0];
1073  path->bounds[1] = bounds[1];
1074  path->bounds[2] = bounds[2];
1075  path->bounds[3] = bounds[3];
1076  } else {
1077  path->bounds[0] = nsvg__minf(path->bounds[0], bounds[0]);
1078  path->bounds[1] = nsvg__minf(path->bounds[1], bounds[1]);
1079  path->bounds[2] = nsvg__maxf(path->bounds[2], bounds[2]);
1080  path->bounds[3] = nsvg__maxf(path->bounds[3], bounds[3]);
1081  }
1082  }
1083 
1084  path->next = p->plist;
1085  p->plist = path;
1086 
1087  return;
1088 
1089 error:
1090  if (path != NULL) {
1091  if (path->pts != NULL) free(path->pts);
1092  free(path);
1093  }
1094 }
1095 
1096 // We roll our own string to float because the std library one uses locale and messes things up.
1097 static double nsvg__atof(const char* s)
1098 {
1099  const char* cur = s;
1100  char* end = NULL;
1101  double res = 0.0, sign = 1.0;
1102  long long intPart = 0, fracPart = 0;
1103  char hasIntPart = 0, hasFracPart = 0;
1104 
1105  // Parse optional sign
1106  if (*cur == '+') {
1107  cur++;
1108  } else if (*cur == '-') {
1109  sign = -1;
1110  cur++;
1111  }
1112 
1113  // Parse integer part
1114  if (nsvg__isdigit(*cur)) {
1115  // Parse digit sequence
1116  intPart = strtoll(cur, &end, 10);
1117  if (cur != end) {
1118  res = (double)intPart;
1119  hasIntPart = 1;
1120  cur = end;
1121  }
1122  }
1123 
1124  // Parse fractional part.
1125  if (*cur == '.') {
1126  cur++; // Skip '.'
1127  if (nsvg__isdigit(*cur)) {
1128  // Parse digit sequence
1129  fracPart = strtoll(cur, &end, 10);
1130  if (cur != end) {
1131  res += (double)fracPart / pow(10.0, (double)(end - cur));
1132  hasFracPart = 1;
1133  cur = end;
1134  }
1135  }
1136  }
1137 
1138  // A valid number should have integer or fractional part.
1139  if (!hasIntPart && !hasFracPart)
1140  return 0.0;
1141 
1142  // Parse optional exponent
1143  if (*cur == 'e' || *cur == 'E') {
1144  long expPart = 0;
1145  cur++; // skip 'E'
1146  expPart = strtol(cur, &end, 10); // Parse digit sequence with sign
1147  if (cur != end) {
1148  res *= pow(10.0, (double)expPart);
1149  }
1150  }
1151 
1152  return res * sign;
1153 }
1154 
1155 
1156 static const char* nsvg__parseNumber(const char* s, char* it, const int size)
1157 {
1158  const int last = size-1;
1159  int i = 0;
1160 
1161  // sign
1162  if (*s == '-' || *s == '+') {
1163  if (i < last) it[i++] = *s;
1164  s++;
1165  }
1166  // integer part
1167  while (*s && nsvg__isdigit(*s)) {
1168  if (i < last) it[i++] = *s;
1169  s++;
1170  }
1171  if (*s == '.') {
1172  // decimal point
1173  if (i < last) it[i++] = *s;
1174  s++;
1175  // fraction part
1176  while (*s && nsvg__isdigit(*s)) {
1177  if (i < last) it[i++] = *s;
1178  s++;
1179  }
1180  }
1181  // exponent
1182  if ((*s == 'e' || *s == 'E') && (s[1] != 'm' && s[1] != 'x')) {
1183  if (i < last) it[i++] = *s;
1184  s++;
1185  if (*s == '-' || *s == '+') {
1186  if (i < last) it[i++] = *s;
1187  s++;
1188  }
1189  while (*s && nsvg__isdigit(*s)) {
1190  if (i < last) it[i++] = *s;
1191  s++;
1192  }
1193  }
1194  it[i] = '\0';
1195 
1196  return s;
1197 }
1198 
1199 static const char* nsvg__getNextPathItem(const char* s, char* it)
1200 {
1201  it[0] = '\0';
1202  // Skip white spaces and commas
1203  while (*s && (nsvg__isspace(*s) || *s == ',')) s++;
1204  if (!*s) return s;
1205  if (*s == '-' || *s == '+' || *s == '.' || nsvg__isdigit(*s)) {
1206  s = nsvg__parseNumber(s, it, 64);
1207  } else {
1208  // Parse command
1209  it[0] = *s++;
1210  it[1] = '\0';
1211  return s;
1212  }
1213 
1214  return s;
1215 }
1216 
1217 static unsigned int nsvg__parseColorHex(const char* str)
1218 {
1219  unsigned int c = 0, r = 0, g = 0, b = 0;
1220  int n = 0;
1221  str++; // skip #
1222  // Calculate number of characters.
1223  while(str[n] && !nsvg__isspace(str[n]))
1224  n++;
1225  if (n == 6) {
1226  sscanf(str, "%x", &c);
1227  } else if (n == 3) {
1228  sscanf(str, "%x", &c);
1229  c = (c&0xf) | ((c&0xf0) << 4) | ((c&0xf00) << 8);
1230  c |= c<<4;
1231  }
1232  r = (c >> 16) & 0xff;
1233  g = (c >> 8) & 0xff;
1234  b = c & 0xff;
1235  return NSVG_RGB(r,g,b);
1236 }
1237 
1238 static unsigned int nsvg__parseColorRGB(const char* str)
1239 {
1240  int r = -1, g = -1, b = -1;
1241  char s1[32]="", s2[32]="";
1242  sscanf(str + 4, "%d%[%%, \t]%d%[%%, \t]%d", &r, s1, &g, s2, &b);
1243  if (strchr(s1, '%')) {
1244  return NSVG_RGB((r*255)/100,(g*255)/100,(b*255)/100);
1245  } else {
1246  return NSVG_RGB(r,g,b);
1247  }
1248 }
1249 
1250 typedef struct NSVGNamedColor {
1251  const char* name;
1252  unsigned int color;
1253 } NSVGNamedColor;
1254 
1255 static const NSVGNamedColor nsvg__colors[] = {
1256 
1257  { "red", NSVG_RGB(255, 0, 0) },
1258  { "green", NSVG_RGB( 0, 128, 0) },
1259  { "blue", NSVG_RGB( 0, 0, 255) },
1260  { "yellow", NSVG_RGB(255, 255, 0) },
1261  { "cyan", NSVG_RGB( 0, 255, 255) },
1262  { "magenta", NSVG_RGB(255, 0, 255) },
1263  { "black", NSVG_RGB( 0, 0, 0) },
1264  { "grey", NSVG_RGB(128, 128, 128) },
1265  { "gray", NSVG_RGB(128, 128, 128) },
1266  { "white", NSVG_RGB(255, 255, 255) },
1267 
1268 #ifdef NANOSVG_ALL_COLOR_KEYWORDS
1269  { "aliceblue", NSVG_RGB(240, 248, 255) },
1270  { "antiquewhite", NSVG_RGB(250, 235, 215) },
1271  { "aqua", NSVG_RGB( 0, 255, 255) },
1272  { "aquamarine", NSVG_RGB(127, 255, 212) },
1273  { "azure", NSVG_RGB(240, 255, 255) },
1274  { "beige", NSVG_RGB(245, 245, 220) },
1275  { "bisque", NSVG_RGB(255, 228, 196) },
1276  { "blanchedalmond", NSVG_RGB(255, 235, 205) },
1277  { "blueviolet", NSVG_RGB(138, 43, 226) },
1278  { "brown", NSVG_RGB(165, 42, 42) },
1279  { "burlywood", NSVG_RGB(222, 184, 135) },
1280  { "cadetblue", NSVG_RGB( 95, 158, 160) },
1281  { "chartreuse", NSVG_RGB(127, 255, 0) },
1282  { "chocolate", NSVG_RGB(210, 105, 30) },
1283  { "coral", NSVG_RGB(255, 127, 80) },
1284  { "cornflowerblue", NSVG_RGB(100, 149, 237) },
1285  { "cornsilk", NSVG_RGB(255, 248, 220) },
1286  { "crimson", NSVG_RGB(220, 20, 60) },
1287  { "darkblue", NSVG_RGB( 0, 0, 139) },
1288  { "darkcyan", NSVG_RGB( 0, 139, 139) },
1289  { "darkgoldenrod", NSVG_RGB(184, 134, 11) },
1290  { "darkgray", NSVG_RGB(169, 169, 169) },
1291  { "darkgreen", NSVG_RGB( 0, 100, 0) },
1292  { "darkgrey", NSVG_RGB(169, 169, 169) },
1293  { "darkkhaki", NSVG_RGB(189, 183, 107) },
1294  { "darkmagenta", NSVG_RGB(139, 0, 139) },
1295  { "darkolivegreen", NSVG_RGB( 85, 107, 47) },
1296  { "darkorange", NSVG_RGB(255, 140, 0) },
1297  { "darkorchid", NSVG_RGB(153, 50, 204) },
1298  { "darkred", NSVG_RGB(139, 0, 0) },
1299  { "darksalmon", NSVG_RGB(233, 150, 122) },
1300  { "darkseagreen", NSVG_RGB(143, 188, 143) },
1301  { "darkslateblue", NSVG_RGB( 72, 61, 139) },
1302  { "darkslategray", NSVG_RGB( 47, 79, 79) },
1303  { "darkslategrey", NSVG_RGB( 47, 79, 79) },
1304  { "darkturquoise", NSVG_RGB( 0, 206, 209) },
1305  { "darkviolet", NSVG_RGB(148, 0, 211) },
1306  { "deeppink", NSVG_RGB(255, 20, 147) },
1307  { "deepskyblue", NSVG_RGB( 0, 191, 255) },
1308  { "dimgray", NSVG_RGB(105, 105, 105) },
1309  { "dimgrey", NSVG_RGB(105, 105, 105) },
1310  { "dodgerblue", NSVG_RGB( 30, 144, 255) },
1311  { "firebrick", NSVG_RGB(178, 34, 34) },
1312  { "floralwhite", NSVG_RGB(255, 250, 240) },
1313  { "forestgreen", NSVG_RGB( 34, 139, 34) },
1314  { "fuchsia", NSVG_RGB(255, 0, 255) },
1315  { "gainsboro", NSVG_RGB(220, 220, 220) },
1316  { "ghostwhite", NSVG_RGB(248, 248, 255) },
1317  { "gold", NSVG_RGB(255, 215, 0) },
1318  { "goldenrod", NSVG_RGB(218, 165, 32) },
1319  { "greenyellow", NSVG_RGB(173, 255, 47) },
1320  { "honeydew", NSVG_RGB(240, 255, 240) },
1321  { "hotpink", NSVG_RGB(255, 105, 180) },
1322  { "indianred", NSVG_RGB(205, 92, 92) },
1323  { "indigo", NSVG_RGB( 75, 0, 130) },
1324  { "ivory", NSVG_RGB(255, 255, 240) },
1325  { "khaki", NSVG_RGB(240, 230, 140) },
1326  { "lavender", NSVG_RGB(230, 230, 250) },
1327  { "lavenderblush", NSVG_RGB(255, 240, 245) },
1328  { "lawngreen", NSVG_RGB(124, 252, 0) },
1329  { "lemonchiffon", NSVG_RGB(255, 250, 205) },
1330  { "lightblue", NSVG_RGB(173, 216, 230) },
1331  { "lightcoral", NSVG_RGB(240, 128, 128) },
1332  { "lightcyan", NSVG_RGB(224, 255, 255) },
1333  { "lightgoldenrodyellow", NSVG_RGB(250, 250, 210) },
1334  { "lightgray", NSVG_RGB(211, 211, 211) },
1335  { "lightgreen", NSVG_RGB(144, 238, 144) },
1336  { "lightgrey", NSVG_RGB(211, 211, 211) },
1337  { "lightpink", NSVG_RGB(255, 182, 193) },
1338  { "lightsalmon", NSVG_RGB(255, 160, 122) },
1339  { "lightseagreen", NSVG_RGB( 32, 178, 170) },
1340  { "lightskyblue", NSVG_RGB(135, 206, 250) },
1341  { "lightslategray", NSVG_RGB(119, 136, 153) },
1342  { "lightslategrey", NSVG_RGB(119, 136, 153) },
1343  { "lightsteelblue", NSVG_RGB(176, 196, 222) },
1344  { "lightyellow", NSVG_RGB(255, 255, 224) },
1345  { "lime", NSVG_RGB( 0, 255, 0) },
1346  { "limegreen", NSVG_RGB( 50, 205, 50) },
1347  { "linen", NSVG_RGB(250, 240, 230) },
1348  { "maroon", NSVG_RGB(128, 0, 0) },
1349  { "mediumaquamarine", NSVG_RGB(102, 205, 170) },
1350  { "mediumblue", NSVG_RGB( 0, 0, 205) },
1351  { "mediumorchid", NSVG_RGB(186, 85, 211) },
1352  { "mediumpurple", NSVG_RGB(147, 112, 219) },
1353  { "mediumseagreen", NSVG_RGB( 60, 179, 113) },
1354  { "mediumslateblue", NSVG_RGB(123, 104, 238) },
1355  { "mediumspringgreen", NSVG_RGB( 0, 250, 154) },
1356  { "mediumturquoise", NSVG_RGB( 72, 209, 204) },
1357  { "mediumvioletred", NSVG_RGB(199, 21, 133) },
1358  { "midnightblue", NSVG_RGB( 25, 25, 112) },
1359  { "mintcream", NSVG_RGB(245, 255, 250) },
1360  { "mistyrose", NSVG_RGB(255, 228, 225) },
1361  { "moccasin", NSVG_RGB(255, 228, 181) },
1362  { "navajowhite", NSVG_RGB(255, 222, 173) },
1363  { "navy", NSVG_RGB( 0, 0, 128) },
1364  { "oldlace", NSVG_RGB(253, 245, 230) },
1365  { "olive", NSVG_RGB(128, 128, 0) },
1366  { "olivedrab", NSVG_RGB(107, 142, 35) },
1367  { "orange", NSVG_RGB(255, 165, 0) },
1368  { "orangered", NSVG_RGB(255, 69, 0) },
1369  { "orchid", NSVG_RGB(218, 112, 214) },
1370  { "palegoldenrod", NSVG_RGB(238, 232, 170) },
1371  { "palegreen", NSVG_RGB(152, 251, 152) },
1372  { "paleturquoise", NSVG_RGB(175, 238, 238) },
1373  { "palevioletred", NSVG_RGB(219, 112, 147) },
1374  { "papayawhip", NSVG_RGB(255, 239, 213) },
1375  { "peachpuff", NSVG_RGB(255, 218, 185) },
1376  { "peru", NSVG_RGB(205, 133, 63) },
1377  { "pink", NSVG_RGB(255, 192, 203) },
1378  { "plum", NSVG_RGB(221, 160, 221) },
1379  { "powderblue", NSVG_RGB(176, 224, 230) },
1380  { "purple", NSVG_RGB(128, 0, 128) },
1381  { "rosybrown", NSVG_RGB(188, 143, 143) },
1382  { "royalblue", NSVG_RGB( 65, 105, 225) },
1383  { "saddlebrown", NSVG_RGB(139, 69, 19) },
1384  { "salmon", NSVG_RGB(250, 128, 114) },
1385  { "sandybrown", NSVG_RGB(244, 164, 96) },
1386  { "seagreen", NSVG_RGB( 46, 139, 87) },
1387  { "seashell", NSVG_RGB(255, 245, 238) },
1388  { "sienna", NSVG_RGB(160, 82, 45) },
1389  { "silver", NSVG_RGB(192, 192, 192) },
1390  { "skyblue", NSVG_RGB(135, 206, 235) },
1391  { "slateblue", NSVG_RGB(106, 90, 205) },
1392  { "slategray", NSVG_RGB(112, 128, 144) },
1393  { "slategrey", NSVG_RGB(112, 128, 144) },
1394  { "snow", NSVG_RGB(255, 250, 250) },
1395  { "springgreen", NSVG_RGB( 0, 255, 127) },
1396  { "steelblue", NSVG_RGB( 70, 130, 180) },
1397  { "tan", NSVG_RGB(210, 180, 140) },
1398  { "teal", NSVG_RGB( 0, 128, 128) },
1399  { "thistle", NSVG_RGB(216, 191, 216) },
1400  { "tomato", NSVG_RGB(255, 99, 71) },
1401  { "turquoise", NSVG_RGB( 64, 224, 208) },
1402  { "violet", NSVG_RGB(238, 130, 238) },
1403  { "wheat", NSVG_RGB(245, 222, 179) },
1404  { "whitesmoke", NSVG_RGB(245, 245, 245) },
1405  { "yellowgreen", NSVG_RGB(154, 205, 50) },
1406 #endif
1407 };
1408 
1409 static unsigned int nsvg__parseColorName(const char* str)
1410 {
1411  int i, ncolors = sizeof(nsvg__colors) / sizeof(NSVGNamedColor);
1412 
1413  for (i = 0; i < ncolors; i++) {
1414  if (strcmp(nsvg__colors[i].name, str) == 0) {
1415  return nsvg__colors[i].color;
1416  }
1417  }
1418 
1419  return NSVG_RGB(128, 128, 128);
1420 }
1421 
1422 static unsigned int nsvg__parseColor(const char* str)
1423 {
1424  size_t len = 0;
1425  while(*str == ' ') ++str;
1426  len = strlen(str);
1427  if (len >= 1 && *str == '#')
1428  return nsvg__parseColorHex(str);
1429  else if (len >= 4 && str[0] == 'r' && str[1] == 'g' && str[2] == 'b' && str[3] == '(')
1430  return nsvg__parseColorRGB(str);
1431  return nsvg__parseColorName(str);
1432 }
1433 
1434 static float nsvg__parseOpacity(const char* str)
1435 {
1436  float val = nsvg__atof(str);
1437  if (val < 0.0f) val = 0.0f;
1438  if (val > 1.0f) val = 1.0f;
1439  return val;
1440 }
1441 
1442 static float nsvg__parseMiterLimit(const char* str)
1443 {
1444  float val = nsvg__atof(str);
1445  if (val < 0.0f) val = 0.0f;
1446  return val;
1447 }
1448 
1449 static int nsvg__parseUnits(const char* units)
1450 {
1451  if (units[0] == 'p' && units[1] == 'x')
1452  return NSVG_UNITS_PX;
1453  else if (units[0] == 'p' && units[1] == 't')
1454  return NSVG_UNITS_PT;
1455  else if (units[0] == 'p' && units[1] == 'c')
1456  return NSVG_UNITS_PC;
1457  else if (units[0] == 'm' && units[1] == 'm')
1458  return NSVG_UNITS_MM;
1459  else if (units[0] == 'c' && units[1] == 'm')
1460  return NSVG_UNITS_CM;
1461  else if (units[0] == 'i' && units[1] == 'n')
1462  return NSVG_UNITS_IN;
1463  else if (units[0] == '%')
1464  return NSVG_UNITS_PERCENT;
1465  else if (units[0] == 'e' && units[1] == 'm')
1466  return NSVG_UNITS_EM;
1467  else if (units[0] == 'e' && units[1] == 'x')
1468  return NSVG_UNITS_EX;
1469  return NSVG_UNITS_USER;
1470 }
1471 
1472 static int nsvg__isCoordinate(const char* s)
1473 {
1474  // optional sign
1475  if (*s == '-' || *s == '+')
1476  s++;
1477  // must have at least one digit, or start by a dot
1478  return (nsvg__isdigit(*s) || *s == '.');
1479 }
1480 
1481 static NSVGcoordinate nsvg__parseCoordinateRaw(const char* str)
1482 {
1483  NSVGcoordinate coord = {0, NSVG_UNITS_USER};
1484  char buf[64];
1485  coord.units = nsvg__parseUnits(nsvg__parseNumber(str, buf, 64));
1486  coord.value = nsvg__atof(buf);
1487  return coord;
1488 }
1489 
1490 static NSVGcoordinate nsvg__coord(float v, int units)
1491 {
1492  NSVGcoordinate coord = {v, units};
1493  return coord;
1494 }
1495 
1496 static float nsvg__parseCoordinate(NSVGparser* p, const char* str, float orig, float length)
1497 {
1498  NSVGcoordinate coord = nsvg__parseCoordinateRaw(str);
1499  return nsvg__convertToPixels(p, coord, orig, length);
1500 }
1501 
1502 static int nsvg__parseTransformArgs(const char* str, float* args, int maxNa, int* na)
1503 {
1504  const char* end;
1505  const char* ptr;
1506  char it[64];
1507 
1508  *na = 0;
1509  ptr = str;
1510  while (*ptr && *ptr != '(') ++ptr;
1511  if (*ptr == 0)
1512  return 1;
1513  end = ptr;
1514  while (*end && *end != ')') ++end;
1515  if (*end == 0)
1516  return 1;
1517 
1518  while (ptr < end) {
1519  if (*ptr == '-' || *ptr == '+' || *ptr == '.' || nsvg__isdigit(*ptr)) {
1520  if (*na >= maxNa) return 0;
1521  ptr = nsvg__parseNumber(ptr, it, 64);
1522  args[(*na)++] = (float)nsvg__atof(it);
1523  } else {
1524  ++ptr;
1525  }
1526  }
1527  return (int)(end - str);
1528 }
1529 
1530 
1531 static int nsvg__parseMatrix(float* xform, const char* str)
1532 {
1533  float t[6];
1534  int na = 0;
1535  int len = nsvg__parseTransformArgs(str, t, 6, &na);
1536  if (na != 6) return len;
1537  memcpy(xform, t, sizeof(float)*6);
1538  return len;
1539 }
1540 
1541 static int nsvg__parseTranslate(float* xform, const char* str)
1542 {
1543  float args[2];
1544  float t[6];
1545  int na = 0;
1546  int len = nsvg__parseTransformArgs(str, args, 2, &na);
1547  if (na == 1) args[1] = 0.0;
1548 
1549  nsvg__xformSetTranslation(t, args[0], args[1]);
1550  memcpy(xform, t, sizeof(float)*6);
1551  return len;
1552 }
1553 
1554 static int nsvg__parseScale(float* xform, const char* str)
1555 {
1556  float args[2];
1557  int na = 0;
1558  float t[6];
1559  int len = nsvg__parseTransformArgs(str, args, 2, &na);
1560  if (na == 1) args[1] = args[0];
1561  nsvg__xformSetScale(t, args[0], args[1]);
1562  memcpy(xform, t, sizeof(float)*6);
1563  return len;
1564 }
1565 
1566 static int nsvg__parseSkewX(float* xform, const char* str)
1567 {
1568  float args[1];
1569  int na = 0;
1570  float t[6];
1571  int len = nsvg__parseTransformArgs(str, args, 1, &na);
1572  nsvg__xformSetSkewX(t, args[0]/180.0f*NSVG_PI);
1573  memcpy(xform, t, sizeof(float)*6);
1574  return len;
1575 }
1576 
1577 static int nsvg__parseSkewY(float* xform, const char* str)
1578 {
1579  float args[1];
1580  int na = 0;
1581  float t[6];
1582  int len = nsvg__parseTransformArgs(str, args, 1, &na);
1583  nsvg__xformSetSkewY(t, args[0]/180.0f*NSVG_PI);
1584  memcpy(xform, t, sizeof(float)*6);
1585  return len;
1586 }
1587 
1588 static int nsvg__parseRotate(float* xform, const char* str)
1589 {
1590  float args[3];
1591  int na = 0;
1592  float m[6];
1593  float t[6];
1594  int len = nsvg__parseTransformArgs(str, args, 3, &na);
1595  if (na == 1)
1596  args[1] = args[2] = 0.0f;
1597  nsvg__xformIdentity(m);
1598 
1599  if (na > 1) {
1600  nsvg__xformSetTranslation(t, -args[1], -args[2]);
1601  nsvg__xformMultiply(m, t);
1602  }
1603 
1604  nsvg__xformSetRotation(t, args[0]/180.0f*NSVG_PI);
1605  nsvg__xformMultiply(m, t);
1606 
1607  if (na > 1) {
1608  nsvg__xformSetTranslation(t, args[1], args[2]);
1609  nsvg__xformMultiply(m, t);
1610  }
1611 
1612  memcpy(xform, m, sizeof(float)*6);
1613 
1614  return len;
1615 }
1616 
1617 static void nsvg__parseTransform(float* xform, const char* str)
1618 {
1619  float t[6];
1620  int len;
1621  nsvg__xformIdentity(xform);
1622  while (*str)
1623  {
1624  if (strncmp(str, "matrix", 6) == 0)
1625  len = nsvg__parseMatrix(t, str);
1626  else if (strncmp(str, "translate", 9) == 0)
1627  len = nsvg__parseTranslate(t, str);
1628  else if (strncmp(str, "scale", 5) == 0)
1629  len = nsvg__parseScale(t, str);
1630  else if (strncmp(str, "rotate", 6) == 0)
1631  len = nsvg__parseRotate(t, str);
1632  else if (strncmp(str, "skewX", 5) == 0)
1633  len = nsvg__parseSkewX(t, str);
1634  else if (strncmp(str, "skewY", 5) == 0)
1635  len = nsvg__parseSkewY(t, str);
1636  else{
1637  ++str;
1638  continue;
1639  }
1640  if (len != 0) {
1641  str += len;
1642  } else {
1643  ++str;
1644  continue;
1645  }
1646 
1647  nsvg__xformPremultiply(xform, t);
1648  }
1649 }
1650 
1651 static void nsvg__parseUrl(char* id, const char* str)
1652 {
1653  int i = 0;
1654  str += 4; // "url(";
1655  if (*str == '#')
1656  str++;
1657  while (i < 63 && *str != ')') {
1658  id[i] = *str++;
1659  i++;
1660  }
1661  id[i] = '\0';
1662 }
1663 
1664 static char nsvg__parseLineCap(const char* str)
1665 {
1666  if (strcmp(str, "butt") == 0)
1667  return NSVG_CAP_BUTT;
1668  else if (strcmp(str, "round") == 0)
1669  return NSVG_CAP_ROUND;
1670  else if (strcmp(str, "square") == 0)
1671  return NSVG_CAP_SQUARE;
1672  // TODO: handle inherit.
1673  return NSVG_CAP_BUTT;
1674 }
1675 
1676 static char nsvg__parseLineJoin(const char* str)
1677 {
1678  if (strcmp(str, "miter") == 0)
1679  return NSVG_JOIN_MITER;
1680  else if (strcmp(str, "round") == 0)
1681  return NSVG_JOIN_ROUND;
1682  else if (strcmp(str, "bevel") == 0)
1683  return NSVG_JOIN_BEVEL;
1684  // TODO: handle inherit.
1685  return NSVG_JOIN_MITER;
1686 }
1687 
1688 static char nsvg__parseFillRule(const char* str)
1689 {
1690  if (strcmp(str, "nonzero") == 0)
1691  return NSVG_FILLRULE_NONZERO;
1692  else if (strcmp(str, "evenodd") == 0)
1693  return NSVG_FILLRULE_EVENODD;
1694  // TODO: handle inherit.
1695  return NSVG_FILLRULE_NONZERO;
1696 }
1697 
1698 static const char* nsvg__getNextDashItem(const char* s, char* it)
1699 {
1700  int n = 0;
1701  it[0] = '\0';
1702  // Skip white spaces and commas
1703  while (*s && (nsvg__isspace(*s) || *s == ',')) s++;
1704  // Advance until whitespace, comma or end.
1705  while (*s && (!nsvg__isspace(*s) && *s != ',')) {
1706  if (n < 63)
1707  it[n++] = *s;
1708  s++;
1709  }
1710  it[n++] = '\0';
1711  return s;
1712 }
1713 
1714 static int nsvg__parseStrokeDashArray(NSVGparser* p, const char* str, float* strokeDashArray)
1715 {
1716  char item[64];
1717  int count = 0, i;
1718  float sum = 0.0f;
1719 
1720  // Handle "none"
1721  if (str[0] == 'n')
1722  return 0;
1723 
1724  // Parse dashes
1725  while (*str) {
1726  str = nsvg__getNextDashItem(str, item);
1727  if (!*item) break;
1728  if (count < NSVG_MAX_DASHES)
1729  strokeDashArray[count++] = fabsf(nsvg__parseCoordinate(p, item, 0.0f, nsvg__actualLength(p)));
1730  }
1731 
1732  for (i = 0; i < count; i++)
1733  sum += strokeDashArray[i];
1734  if (sum <= 1e-6f)
1735  count = 0;
1736 
1737  return count;
1738 }
1739 
1740 static void nsvg__parseStyle(NSVGparser* p, const char* str);
1741 
1742 static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value)
1743 {
1744  float xform[6];
1745  NSVGattrib* attr = nsvg__getAttr(p);
1746  if (!attr) return 0;
1747 
1748  if (strcmp(name, "style") == 0) {
1749  nsvg__parseStyle(p, value);
1750  } else if (strcmp(name, "display") == 0) {
1751  if (strcmp(value, "none") == 0)
1752  attr->visible = 0;
1753  // Don't reset ->visible on display:inline, one display:none hides the whole subtree
1754 
1755  } else if (strcmp(name, "fill") == 0) {
1756  if (strcmp(value, "none") == 0) {
1757  attr->hasFill = 0;
1758  } else if (strncmp(value, "url(", 4) == 0) {
1759  attr->hasFill = 2;
1760  nsvg__parseUrl(attr->fillGradient, value);
1761  } else {
1762  attr->hasFill = 1;
1763  attr->fillColor = nsvg__parseColor(value);
1764  }
1765  } else if (strcmp(name, "opacity") == 0) {
1766  attr->opacity = nsvg__parseOpacity(value);
1767  } else if (strcmp(name, "fill-opacity") == 0) {
1768  attr->fillOpacity = nsvg__parseOpacity(value);
1769  } else if (strcmp(name, "stroke") == 0) {
1770  if (strcmp(value, "none") == 0) {
1771  attr->hasStroke = 0;
1772  } else if (strncmp(value, "url(", 4) == 0) {
1773  attr->hasStroke = 2;
1774  nsvg__parseUrl(attr->strokeGradient, value);
1775  } else {
1776  attr->hasStroke = 1;
1777  attr->strokeColor = nsvg__parseColor(value);
1778  }
1779  } else if (strcmp(name, "stroke-width") == 0) {
1780  attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1781  } else if (strcmp(name, "stroke-dasharray") == 0) {
1782  attr->strokeDashCount = nsvg__parseStrokeDashArray(p, value, attr->strokeDashArray);
1783  } else if (strcmp(name, "stroke-dashoffset") == 0) {
1784  attr->strokeDashOffset = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1785  } else if (strcmp(name, "stroke-opacity") == 0) {
1786  attr->strokeOpacity = nsvg__parseOpacity(value);
1787  } else if (strcmp(name, "stroke-linecap") == 0) {
1788  attr->strokeLineCap = nsvg__parseLineCap(value);
1789  } else if (strcmp(name, "stroke-linejoin") == 0) {
1790  attr->strokeLineJoin = nsvg__parseLineJoin(value);
1791  } else if (strcmp(name, "stroke-miterlimit") == 0) {
1792  attr->miterLimit = nsvg__parseMiterLimit(value);
1793  } else if (strcmp(name, "fill-rule") == 0) {
1794  attr->fillRule = nsvg__parseFillRule(value);
1795  } else if (strcmp(name, "font-size") == 0) {
1796  attr->fontSize = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1797  } else if (strcmp(name, "transform") == 0) {
1798  nsvg__parseTransform(xform, value);
1799  nsvg__xformPremultiply(attr->xform, xform);
1800  } else if (strcmp(name, "stop-color") == 0) {
1801  attr->stopColor = nsvg__parseColor(value);
1802  } else if (strcmp(name, "stop-opacity") == 0) {
1803  attr->stopOpacity = nsvg__parseOpacity(value);
1804  } else if (strcmp(name, "offset") == 0) {
1805  attr->stopOffset = nsvg__parseCoordinate(p, value, 0.0f, 1.0f);
1806  } else if (strcmp(name, "id") == 0) {
1807  strncpy(attr->id, value, 63);
1808  attr->id[63] = '\0';
1809  } else {
1810  return 0;
1811  }
1812  return 1;
1813 }
1814 
1815 static int nsvg__parseNameValue(NSVGparser* p, const char* start, const char* end)
1816 {
1817  const char* str;
1818  const char* val;
1819  char name[512];
1820  char value[512];
1821  int n;
1822 
1823  str = start;
1824  while (str < end && *str != ':') ++str;
1825 
1826  val = str;
1827 
1828  // Right Trim
1829  while (str > start && (*str == ':' || nsvg__isspace(*str))) --str;
1830  ++str;
1831 
1832  n = (int)(str - start);
1833  if (n > 511) n = 511;
1834  if (n) memcpy(name, start, n);
1835  name[n] = 0;
1836 
1837  while (val < end && (*val == ':' || nsvg__isspace(*val))) ++val;
1838 
1839  n = (int)(end - val);
1840  if (n > 511) n = 511;
1841  if (n) memcpy(value, val, n);
1842  value[n] = 0;
1843 
1844  return nsvg__parseAttr(p, name, value);
1845 }
1846 
1847 static void nsvg__parseStyle(NSVGparser* p, const char* str)
1848 {
1849  const char* start;
1850  const char* end;
1851 
1852  while (*str) {
1853  // Left Trim
1854  while(*str && nsvg__isspace(*str)) ++str;
1855  start = str;
1856  while(*str && *str != ';') ++str;
1857  end = str;
1858 
1859  // Right Trim
1860  while (end > start && (*end == ';' || nsvg__isspace(*end))) --end;
1861  ++end;
1862 
1863  nsvg__parseNameValue(p, start, end);
1864  if (*str) ++str;
1865  }
1866 }
1867 
1868 static void nsvg__parseAttribs(NSVGparser* p, const char** attr)
1869 {
1870  int i;
1871  for (i = 0; attr[i]; i += 2)
1872  {
1873  if (strcmp(attr[i], "style") == 0)
1874  nsvg__parseStyle(p, attr[i + 1]);
1875  else
1876  nsvg__parseAttr(p, attr[i], attr[i + 1]);
1877  }
1878 }
1879 
1880 static int nsvg__getArgsPerElement(char cmd)
1881 {
1882  switch (cmd) {
1883  case 'v':
1884  case 'V':
1885  case 'h':
1886  case 'H':
1887  return 1;
1888  case 'm':
1889  case 'M':
1890  case 'l':
1891  case 'L':
1892  case 't':
1893  case 'T':
1894  return 2;
1895  case 'q':
1896  case 'Q':
1897  case 's':
1898  case 'S':
1899  return 4;
1900  case 'c':
1901  case 'C':
1902  return 6;
1903  case 'a':
1904  case 'A':
1905  return 7;
1906  case 'z':
1907  case 'Z':
1908  return 0;
1909  }
1910  return -1;
1911 }
1912 
1913 static void nsvg__pathMoveTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
1914 {
1915  if (rel) {
1916  *cpx += args[0];
1917  *cpy += args[1];
1918  } else {
1919  *cpx = args[0];
1920  *cpy = args[1];
1921  }
1922  nsvg__moveTo(p, *cpx, *cpy);
1923 }
1924 
1925 static void nsvg__pathLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
1926 {
1927  if (rel) {
1928  *cpx += args[0];
1929  *cpy += args[1];
1930  } else {
1931  *cpx = args[0];
1932  *cpy = args[1];
1933  }
1934  nsvg__lineTo(p, *cpx, *cpy);
1935 }
1936 
1937 static void nsvg__pathHLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
1938 {
1939  if (rel)
1940  *cpx += args[0];
1941  else
1942  *cpx = args[0];
1943  nsvg__lineTo(p, *cpx, *cpy);
1944 }
1945 
1946 static void nsvg__pathVLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
1947 {
1948  if (rel)
1949  *cpy += args[0];
1950  else
1951  *cpy = args[0];
1952  nsvg__lineTo(p, *cpx, *cpy);
1953 }
1954 
1955 static void nsvg__pathCubicBezTo(NSVGparser* p, float* cpx, float* cpy,
1956  float* cpx2, float* cpy2, float* args, int rel)
1957 {
1958  float x2, y2, cx1, cy1, cx2, cy2;
1959 
1960  if (rel) {
1961  cx1 = *cpx + args[0];
1962  cy1 = *cpy + args[1];
1963  cx2 = *cpx + args[2];
1964  cy2 = *cpy + args[3];
1965  x2 = *cpx + args[4];
1966  y2 = *cpy + args[5];
1967  } else {
1968  cx1 = args[0];
1969  cy1 = args[1];
1970  cx2 = args[2];
1971  cy2 = args[3];
1972  x2 = args[4];
1973  y2 = args[5];
1974  }
1975 
1976  nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
1977 
1978  *cpx2 = cx2;
1979  *cpy2 = cy2;
1980  *cpx = x2;
1981  *cpy = y2;
1982 }
1983 
1984 static void nsvg__pathCubicBezShortTo(NSVGparser* p, float* cpx, float* cpy,
1985  float* cpx2, float* cpy2, float* args, int rel)
1986 {
1987  float x1, y1, x2, y2, cx1, cy1, cx2, cy2;
1988 
1989  x1 = *cpx;
1990  y1 = *cpy;
1991  if (rel) {
1992  cx2 = *cpx + args[0];
1993  cy2 = *cpy + args[1];
1994  x2 = *cpx + args[2];
1995  y2 = *cpy + args[3];
1996  } else {
1997  cx2 = args[0];
1998  cy2 = args[1];
1999  x2 = args[2];
2000  y2 = args[3];
2001  }
2002 
2003  cx1 = 2*x1 - *cpx2;
2004  cy1 = 2*y1 - *cpy2;
2005 
2006  nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2007 
2008  *cpx2 = cx2;
2009  *cpy2 = cy2;
2010  *cpx = x2;
2011  *cpy = y2;
2012 }
2013 
2014 static void nsvg__pathQuadBezTo(NSVGparser* p, float* cpx, float* cpy,
2015  float* cpx2, float* cpy2, float* args, int rel)
2016 {
2017  float x1, y1, x2, y2, cx, cy;
2018  float cx1, cy1, cx2, cy2;
2019 
2020  x1 = *cpx;
2021  y1 = *cpy;
2022  if (rel) {
2023  cx = *cpx + args[0];
2024  cy = *cpy + args[1];
2025  x2 = *cpx + args[2];
2026  y2 = *cpy + args[3];
2027  } else {
2028  cx = args[0];
2029  cy = args[1];
2030  x2 = args[2];
2031  y2 = args[3];
2032  }
2033 
2034  // Convert to cubic bezier
2035  cx1 = x1 + 2.0f/3.0f*(cx - x1);
2036  cy1 = y1 + 2.0f/3.0f*(cy - y1);
2037  cx2 = x2 + 2.0f/3.0f*(cx - x2);
2038  cy2 = y2 + 2.0f/3.0f*(cy - y2);
2039 
2040  nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2041 
2042  *cpx2 = cx;
2043  *cpy2 = cy;
2044  *cpx = x2;
2045  *cpy = y2;
2046 }
2047 
2048 static void nsvg__pathQuadBezShortTo(NSVGparser* p, float* cpx, float* cpy,
2049  float* cpx2, float* cpy2, float* args, int rel)
2050 {
2051  float x1, y1, x2, y2, cx, cy;
2052  float cx1, cy1, cx2, cy2;
2053 
2054  x1 = *cpx;
2055  y1 = *cpy;
2056  if (rel) {
2057  x2 = *cpx + args[0];
2058  y2 = *cpy + args[1];
2059  } else {
2060  x2 = args[0];
2061  y2 = args[1];
2062  }
2063 
2064  cx = 2*x1 - *cpx2;
2065  cy = 2*y1 - *cpy2;
2066 
2067  // Convert to cubix bezier
2068  cx1 = x1 + 2.0f/3.0f*(cx - x1);
2069  cy1 = y1 + 2.0f/3.0f*(cy - y1);
2070  cx2 = x2 + 2.0f/3.0f*(cx - x2);
2071  cy2 = y2 + 2.0f/3.0f*(cy - y2);
2072 
2073  nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2074 
2075  *cpx2 = cx;
2076  *cpy2 = cy;
2077  *cpx = x2;
2078  *cpy = y2;
2079 }
2080 
2081 static float nsvg__sqr(float x) { return x*x; }
2082 static float nsvg__vmag(float x, float y) { return sqrtf(x*x + y*y); }
2083 
2084 static float nsvg__vecrat(float ux, float uy, float vx, float vy)
2085 {
2086  return (ux*vx + uy*vy) / (nsvg__vmag(ux,uy) * nsvg__vmag(vx,vy));
2087 }
2088 
2089 static float nsvg__vecang(float ux, float uy, float vx, float vy)
2090 {
2091  float r = nsvg__vecrat(ux,uy, vx,vy);
2092  if (r < -1.0f) r = -1.0f;
2093  if (r > 1.0f) r = 1.0f;
2094  return ((ux*vy < uy*vx) ? -1.0f : 1.0f) * acosf(r);
2095 }
2096 
2097 static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
2098 {
2099  // Ported from canvg (https://code.google.com/p/canvg/)
2100  float rx, ry, rotx;
2101  float x1, y1, x2, y2, cx, cy, dx, dy, d;
2102  float x1p, y1p, cxp, cyp, s, sa, sb;
2103  float ux, uy, vx, vy, a1, da;
2104  float x, y, tanx, tany, a, px = 0, py = 0, ptanx = 0, ptany = 0, t[6];
2105  float sinrx, cosrx;
2106  int fa, fs;
2107  int i, ndivs;
2108  float hda, kappa;
2109 
2110  rx = fabsf(args[0]); // y radius
2111  ry = fabsf(args[1]); // x radius
2112  rotx = args[2] / 180.0f * NSVG_PI; // x rotation angle
2113  fa = fabsf(args[3]) > 1e-6 ? 1 : 0; // Large arc
2114  fs = fabsf(args[4]) > 1e-6 ? 1 : 0; // Sweep direction
2115  x1 = *cpx; // start point
2116  y1 = *cpy;
2117  if (rel) { // end point
2118  x2 = *cpx + args[5];
2119  y2 = *cpy + args[6];
2120  } else {
2121  x2 = args[5];
2122  y2 = args[6];
2123  }
2124 
2125  dx = x1 - x2;
2126  dy = y1 - y2;
2127  d = sqrtf(dx*dx + dy*dy);
2128  if (d < 1e-6f || rx < 1e-6f || ry < 1e-6f) {
2129  // The arc degenerates to a line
2130  nsvg__lineTo(p, x2, y2);
2131  *cpx = x2;
2132  *cpy = y2;
2133  return;
2134  }
2135 
2136  sinrx = sinf(rotx);
2137  cosrx = cosf(rotx);
2138 
2139  // Convert to center point parameterization.
2140  // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
2141  // 1) Compute x1', y1'
2142  x1p = cosrx * dx / 2.0f + sinrx * dy / 2.0f;
2143  y1p = -sinrx * dx / 2.0f + cosrx * dy / 2.0f;
2144  d = nsvg__sqr(x1p)/nsvg__sqr(rx) + nsvg__sqr(y1p)/nsvg__sqr(ry);
2145  if (d > 1) {
2146  d = sqrtf(d);
2147  rx *= d;
2148  ry *= d;
2149  }
2150  // 2) Compute cx', cy'
2151  s = 0.0f;
2152  sa = nsvg__sqr(rx)*nsvg__sqr(ry) - nsvg__sqr(rx)*nsvg__sqr(y1p) - nsvg__sqr(ry)*nsvg__sqr(x1p);
2153  sb = nsvg__sqr(rx)*nsvg__sqr(y1p) + nsvg__sqr(ry)*nsvg__sqr(x1p);
2154  if (sa < 0.0f) sa = 0.0f;
2155  if (sb > 0.0f)
2156  s = sqrtf(sa / sb);
2157  if (fa == fs)
2158  s = -s;
2159  cxp = s * rx * y1p / ry;
2160  cyp = s * -ry * x1p / rx;
2161 
2162  // 3) Compute cx,cy from cx',cy'
2163  cx = (x1 + x2)/2.0f + cosrx*cxp - sinrx*cyp;
2164  cy = (y1 + y2)/2.0f + sinrx*cxp + cosrx*cyp;
2165 
2166  // 4) Calculate theta1, and delta theta.
2167  ux = (x1p - cxp) / rx;
2168  uy = (y1p - cyp) / ry;
2169  vx = (-x1p - cxp) / rx;
2170  vy = (-y1p - cyp) / ry;
2171  a1 = nsvg__vecang(1.0f,0.0f, ux,uy); // Initial angle
2172  da = nsvg__vecang(ux,uy, vx,vy); // Delta angle
2173 
2174 // if (vecrat(ux,uy,vx,vy) <= -1.0f) da = NSVG_PI;
2175 // if (vecrat(ux,uy,vx,vy) >= 1.0f) da = 0;
2176 
2177  if (fs == 0 && da > 0)
2178  da -= 2 * NSVG_PI;
2179  else if (fs == 1 && da < 0)
2180  da += 2 * NSVG_PI;
2181 
2182  // Approximate the arc using cubic spline segments.
2183  t[0] = cosrx; t[1] = sinrx;
2184  t[2] = -sinrx; t[3] = cosrx;
2185  t[4] = cx; t[5] = cy;
2186 
2187  // Split arc into max 90 degree segments.
2188  // The loop assumes an iteration per end point (including start and end), this +1.
2189  ndivs = (int)(fabsf(da) / (NSVG_PI*0.5f) + 1.0f);
2190  hda = (da / (float)ndivs) / 2.0f;
2191  kappa = fabsf(4.0f / 3.0f * (1.0f - cosf(hda)) / sinf(hda));
2192  if (da < 0.0f)
2193  kappa = -kappa;
2194 
2195  for (i = 0; i <= ndivs; i++) {
2196  a = a1 + da * ((float)i/(float)ndivs);
2197  dx = cosf(a);
2198  dy = sinf(a);
2199  nsvg__xformPoint(&x, &y, dx*rx, dy*ry, t); // position
2200  nsvg__xformVec(&tanx, &tany, -dy*rx * kappa, dx*ry * kappa, t); // tangent
2201  if (i > 0)
2202  nsvg__cubicBezTo(p, px+ptanx,py+ptany, x-tanx, y-tany, x, y);
2203  px = x;
2204  py = y;
2205  ptanx = tanx;
2206  ptany = tany;
2207  }
2208 
2209  *cpx = x2;
2210  *cpy = y2;
2211 }
2212 
2213 static void nsvg__parsePath(NSVGparser* p, const char** attr)
2214 {
2215  const char* s = NULL;
2216  char cmd = '\0';
2217  float args[10];
2218  int nargs;
2219  int rargs = 0;
2220  char initPoint;
2221  float cpx, cpy, cpx2, cpy2;
2222  const char* tmp[4];
2223  char closedFlag;
2224  int i;
2225  char item[64];
2226 
2227  for (i = 0; attr[i]; i += 2) {
2228  if (strcmp(attr[i], "d") == 0) {
2229  s = attr[i + 1];
2230  } else {
2231  tmp[0] = attr[i];
2232  tmp[1] = attr[i + 1];
2233  tmp[2] = 0;
2234  tmp[3] = 0;
2235  nsvg__parseAttribs(p, tmp);
2236  }
2237  }
2238 
2239  if (s) {
2240  nsvg__resetPath(p);
2241  cpx = 0; cpy = 0;
2242  cpx2 = 0; cpy2 = 0;
2243  initPoint = 0;
2244  closedFlag = 0;
2245  nargs = 0;
2246 
2247  while (*s) {
2248  s = nsvg__getNextPathItem(s, item);
2249  if (!*item) break;
2250  if (cmd != '\0' && nsvg__isCoordinate(item)) {
2251  if (nargs < 10)
2252  args[nargs++] = (float)nsvg__atof(item);
2253  if (nargs >= rargs) {
2254  switch (cmd) {
2255  case 'm':
2256  case 'M':
2257  nsvg__pathMoveTo(p, &cpx, &cpy, args, cmd == 'm' ? 1 : 0);
2258  // Moveto can be followed by multiple coordinate pairs,
2259  // which should be treated as linetos.
2260  cmd = (cmd == 'm') ? 'l' : 'L';
2261  rargs = nsvg__getArgsPerElement(cmd);
2262  cpx2 = cpx; cpy2 = cpy;
2263  initPoint = 1;
2264  break;
2265  case 'l':
2266  case 'L':
2267  nsvg__pathLineTo(p, &cpx, &cpy, args, cmd == 'l' ? 1 : 0);
2268  cpx2 = cpx; cpy2 = cpy;
2269  break;
2270  case 'H':
2271  case 'h':
2272  nsvg__pathHLineTo(p, &cpx, &cpy, args, cmd == 'h' ? 1 : 0);
2273  cpx2 = cpx; cpy2 = cpy;
2274  break;
2275  case 'V':
2276  case 'v':
2277  nsvg__pathVLineTo(p, &cpx, &cpy, args, cmd == 'v' ? 1 : 0);
2278  cpx2 = cpx; cpy2 = cpy;
2279  break;
2280  case 'C':
2281  case 'c':
2282  nsvg__pathCubicBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 'c' ? 1 : 0);
2283  break;
2284  case 'S':
2285  case 's':
2286  nsvg__pathCubicBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 's' ? 1 : 0);
2287  break;
2288  case 'Q':
2289  case 'q':
2290  nsvg__pathQuadBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 'q' ? 1 : 0);
2291  break;
2292  case 'T':
2293  case 't':
2294  nsvg__pathQuadBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 't' ? 1 : 0);
2295  break;
2296  case 'A':
2297  case 'a':
2298  nsvg__pathArcTo(p, &cpx, &cpy, args, cmd == 'a' ? 1 : 0);
2299  cpx2 = cpx; cpy2 = cpy;
2300  break;
2301  default:
2302  if (nargs >= 2) {
2303  cpx = args[nargs-2];
2304  cpy = args[nargs-1];
2305  cpx2 = cpx; cpy2 = cpy;
2306  }
2307  break;
2308  }
2309  nargs = 0;
2310  }
2311  } else {
2312  cmd = item[0];
2313  if (cmd == 'M' || cmd == 'm') {
2314  // Commit path.
2315  if (p->npts > 0)
2316  nsvg__addPath(p, closedFlag);
2317  // Start new subpath.
2318  nsvg__resetPath(p);
2319  closedFlag = 0;
2320  nargs = 0;
2321  } else if (initPoint == 0) {
2322  // Do not allow other commands until initial point has been set (moveTo called once).
2323  cmd = '\0';
2324  }
2325  if (cmd == 'Z' || cmd == 'z') {
2326  closedFlag = 1;
2327  // Commit path.
2328  if (p->npts > 0) {
2329  // Move current point to first point
2330  cpx = p->pts[0];
2331  cpy = p->pts[1];
2332  cpx2 = cpx; cpy2 = cpy;
2333  nsvg__addPath(p, closedFlag);
2334  }
2335  // Start new subpath.
2336  nsvg__resetPath(p);
2337  nsvg__moveTo(p, cpx, cpy);
2338  closedFlag = 0;
2339  nargs = 0;
2340  }
2341  rargs = nsvg__getArgsPerElement(cmd);
2342  if (rargs == -1) {
2343  // Command not recognized
2344  cmd = '\0';
2345  rargs = 0;
2346  }
2347  }
2348  }
2349  // Commit path.
2350  if (p->npts)
2351  nsvg__addPath(p, closedFlag);
2352  }
2353 
2354  nsvg__addShape(p);
2355 }
2356 
2357 static void nsvg__parseRect(NSVGparser* p, const char** attr)
2358 {
2359  float x = 0.0f;
2360  float y = 0.0f;
2361  float w = 0.0f;
2362  float h = 0.0f;
2363  float rx = -1.0f; // marks not set
2364  float ry = -1.0f;
2365  int i;
2366 
2367  for (i = 0; attr[i]; i += 2) {
2368  if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2369  if (strcmp(attr[i], "x") == 0) x = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2370  if (strcmp(attr[i], "y") == 0) y = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2371  if (strcmp(attr[i], "width") == 0) w = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p));
2372  if (strcmp(attr[i], "height") == 0) h = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p));
2373  if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
2374  if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
2375  }
2376  }
2377 
2378  if (rx < 0.0f && ry > 0.0f) rx = ry;
2379  if (ry < 0.0f && rx > 0.0f) ry = rx;
2380  if (rx < 0.0f) rx = 0.0f;
2381  if (ry < 0.0f) ry = 0.0f;
2382  if (rx > w/2.0f) rx = w/2.0f;
2383  if (ry > h/2.0f) ry = h/2.0f;
2384 
2385  if (w != 0.0f && h != 0.0f) {
2386  nsvg__resetPath(p);
2387 
2388  if (rx < 0.00001f || ry < 0.0001f) {
2389  nsvg__moveTo(p, x, y);
2390  nsvg__lineTo(p, x+w, y);
2391  nsvg__lineTo(p, x+w, y+h);
2392  nsvg__lineTo(p, x, y+h);
2393  } else {
2394  // Rounded rectangle
2395  nsvg__moveTo(p, x+rx, y);
2396  nsvg__lineTo(p, x+w-rx, y);
2397  nsvg__cubicBezTo(p, x+w-rx*(1-NSVG_KAPPA90), y, x+w, y+ry*(1-NSVG_KAPPA90), x+w, y+ry);
2398  nsvg__lineTo(p, x+w, y+h-ry);
2399  nsvg__cubicBezTo(p, x+w, y+h-ry*(1-NSVG_KAPPA90), x+w-rx*(1-NSVG_KAPPA90), y+h, x+w-rx, y+h);
2400  nsvg__lineTo(p, x+rx, y+h);
2401  nsvg__cubicBezTo(p, x+rx*(1-NSVG_KAPPA90), y+h, x, y+h-ry*(1-NSVG_KAPPA90), x, y+h-ry);
2402  nsvg__lineTo(p, x, y+ry);
2403  nsvg__cubicBezTo(p, x, y+ry*(1-NSVG_KAPPA90), x+rx*(1-NSVG_KAPPA90), y, x+rx, y);
2404  }
2405 
2406  nsvg__addPath(p, 1);
2407 
2408  nsvg__addShape(p);
2409  }
2410 }
2411 
2412 static void nsvg__parseCircle(NSVGparser* p, const char** attr)
2413 {
2414  float cx = 0.0f;
2415  float cy = 0.0f;
2416  float r = 0.0f;
2417  int i;
2418 
2419  for (i = 0; attr[i]; i += 2) {
2420  if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2421  if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2422  if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2423  if (strcmp(attr[i], "r") == 0) r = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualLength(p)));
2424  }
2425  }
2426 
2427  if (r > 0.0f) {
2428  nsvg__resetPath(p);
2429 
2430  nsvg__moveTo(p, cx+r, cy);
2431  nsvg__cubicBezTo(p, cx+r, cy+r*NSVG_KAPPA90, cx+r*NSVG_KAPPA90, cy+r, cx, cy+r);
2432  nsvg__cubicBezTo(p, cx-r*NSVG_KAPPA90, cy+r, cx-r, cy+r*NSVG_KAPPA90, cx-r, cy);
2433  nsvg__cubicBezTo(p, cx-r, cy-r*NSVG_KAPPA90, cx-r*NSVG_KAPPA90, cy-r, cx, cy-r);
2434  nsvg__cubicBezTo(p, cx+r*NSVG_KAPPA90, cy-r, cx+r, cy-r*NSVG_KAPPA90, cx+r, cy);
2435 
2436  nsvg__addPath(p, 1);
2437 
2438  nsvg__addShape(p);
2439  }
2440 }
2441 
2442 static void nsvg__parseEllipse(NSVGparser* p, const char** attr)
2443 {
2444  float cx = 0.0f;
2445  float cy = 0.0f;
2446  float rx = 0.0f;
2447  float ry = 0.0f;
2448  int i;
2449 
2450  for (i = 0; attr[i]; i += 2) {
2451  if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2452  if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2453  if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2454  if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
2455  if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
2456  }
2457  }
2458 
2459  if (rx > 0.0f && ry > 0.0f) {
2460 
2461  nsvg__resetPath(p);
2462 
2463  nsvg__moveTo(p, cx+rx, cy);
2464  nsvg__cubicBezTo(p, cx+rx, cy+ry*NSVG_KAPPA90, cx+rx*NSVG_KAPPA90, cy+ry, cx, cy+ry);
2465  nsvg__cubicBezTo(p, cx-rx*NSVG_KAPPA90, cy+ry, cx-rx, cy+ry*NSVG_KAPPA90, cx-rx, cy);
2466  nsvg__cubicBezTo(p, cx-rx, cy-ry*NSVG_KAPPA90, cx-rx*NSVG_KAPPA90, cy-ry, cx, cy-ry);
2467  nsvg__cubicBezTo(p, cx+rx*NSVG_KAPPA90, cy-ry, cx+rx, cy-ry*NSVG_KAPPA90, cx+rx, cy);
2468 
2469  nsvg__addPath(p, 1);
2470 
2471  nsvg__addShape(p);
2472  }
2473 }
2474 
2475 static void nsvg__parseLine(NSVGparser* p, const char** attr)
2476 {
2477  float x1 = 0.0;
2478  float y1 = 0.0;
2479  float x2 = 0.0;
2480  float y2 = 0.0;
2481  int i;
2482 
2483  for (i = 0; attr[i]; i += 2) {
2484  if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2485  if (strcmp(attr[i], "x1") == 0) x1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2486  if (strcmp(attr[i], "y1") == 0) y1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2487  if (strcmp(attr[i], "x2") == 0) x2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2488  if (strcmp(attr[i], "y2") == 0) y2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2489  }
2490  }
2491 
2492  nsvg__resetPath(p);
2493 
2494  nsvg__moveTo(p, x1, y1);
2495  nsvg__lineTo(p, x2, y2);
2496 
2497  nsvg__addPath(p, 0);
2498 
2499  nsvg__addShape(p);
2500 }
2501 
2502 static void nsvg__parsePoly(NSVGparser* p, const char** attr, int closeFlag)
2503 {
2504  int i;
2505  const char* s;
2506  float args[2];
2507  int nargs, npts = 0;
2508  char item[64];
2509 
2510  nsvg__resetPath(p);
2511 
2512  for (i = 0; attr[i]; i += 2) {
2513  if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2514  if (strcmp(attr[i], "points") == 0) {
2515  s = attr[i + 1];
2516  nargs = 0;
2517  while (*s) {
2518  s = nsvg__getNextPathItem(s, item);
2519  args[nargs++] = (float)nsvg__atof(item);
2520  if (nargs >= 2) {
2521  if (npts == 0)
2522  nsvg__moveTo(p, args[0], args[1]);
2523  else
2524  nsvg__lineTo(p, args[0], args[1]);
2525  nargs = 0;
2526  npts++;
2527  }
2528  }
2529  }
2530  }
2531  }
2532 
2533  nsvg__addPath(p, (char)closeFlag);
2534 
2535  nsvg__addShape(p);
2536 }
2537 
2538 static void nsvg__parseSVG(NSVGparser* p, const char** attr)
2539 {
2540  int i;
2541  for (i = 0; attr[i]; i += 2) {
2542  if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2543  if (strcmp(attr[i], "width") == 0) {
2544  p->image->width = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
2545  } else if (strcmp(attr[i], "height") == 0) {
2546  p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
2547  } else if (strcmp(attr[i], "viewBox") == 0) {
2548  const char *s = attr[i + 1];
2549  char buf[64];
2550  s = nsvg__parseNumber(s, buf, 64);
2551  p->viewMinx = nsvg__atof(buf);
2552  while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++;
2553  if (!*s) return;
2554  s = nsvg__parseNumber(s, buf, 64);
2555  p->viewMiny = nsvg__atof(buf);
2556  while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++;
2557  if (!*s) return;
2558  s = nsvg__parseNumber(s, buf, 64);
2559  p->viewWidth = nsvg__atof(buf);
2560  while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++;
2561  if (!*s) return;
2562  s = nsvg__parseNumber(s, buf, 64);
2563  p->viewHeight = nsvg__atof(buf);
2564  } else if (strcmp(attr[i], "preserveAspectRatio") == 0) {
2565  if (strstr(attr[i + 1], "none") != 0) {
2566  // No uniform scaling
2567  p->alignType = NSVG_ALIGN_NONE;
2568  } else {
2569  // Parse X align
2570  if (strstr(attr[i + 1], "xMin") != 0)
2571  p->alignX = NSVG_ALIGN_MIN;
2572  else if (strstr(attr[i + 1], "xMid") != 0)
2573  p->alignX = NSVG_ALIGN_MID;
2574  else if (strstr(attr[i + 1], "xMax") != 0)
2575  p->alignX = NSVG_ALIGN_MAX;
2576  // Parse X align
2577  if (strstr(attr[i + 1], "yMin") != 0)
2578  p->alignY = NSVG_ALIGN_MIN;
2579  else if (strstr(attr[i + 1], "yMid") != 0)
2580  p->alignY = NSVG_ALIGN_MID;
2581  else if (strstr(attr[i + 1], "yMax") != 0)
2582  p->alignY = NSVG_ALIGN_MAX;
2583  // Parse meet/slice
2584  p->alignType = NSVG_ALIGN_MEET;
2585  if (strstr(attr[i + 1], "slice") != 0)
2586  p->alignType = NSVG_ALIGN_SLICE;
2587  }
2588  }
2589  }
2590  }
2591 }
2592 
2593 static void nsvg__parseGradient(NSVGparser* p, const char** attr, char type)
2594 {
2595  int i;
2596  NSVGgradientData* grad = (NSVGgradientData*)malloc(sizeof(NSVGgradientData));
2597  if (grad == NULL) return;
2598  memset(grad, 0, sizeof(NSVGgradientData));
2599  grad->units = NSVG_OBJECT_SPACE;
2600  grad->type = type;
2601  if (grad->type == NSVG_PAINT_LINEAR_GRADIENT) {
2602  grad->linear.x1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2603  grad->linear.y1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2604  grad->linear.x2 = nsvg__coord(100.0f, NSVG_UNITS_PERCENT);
2605  grad->linear.y2 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2606  } else if (grad->type == NSVG_PAINT_RADIAL_GRADIENT) {
2607  grad->radial.cx = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2608  grad->radial.cy = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2609  grad->radial.r = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2610  }
2611 
2612  nsvg__xformIdentity(grad->xform);
2613 
2614  for (i = 0; attr[i]; i += 2) {
2615  if (strcmp(attr[i], "id") == 0) {
2616  strncpy(grad->id, attr[i+1], 63);
2617  grad->id[63] = '\0';
2618  } else if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2619  if (strcmp(attr[i], "gradientUnits") == 0) {
2620  if (strcmp(attr[i+1], "objectBoundingBox") == 0)
2621  grad->units = NSVG_OBJECT_SPACE;
2622  else
2623  grad->units = NSVG_USER_SPACE;
2624  } else if (strcmp(attr[i], "gradientTransform") == 0) {
2625  nsvg__parseTransform(grad->xform, attr[i + 1]);
2626  } else if (strcmp(attr[i], "cx") == 0) {
2627  grad->radial.cx = nsvg__parseCoordinateRaw(attr[i + 1]);
2628  } else if (strcmp(attr[i], "cy") == 0) {
2629  grad->radial.cy = nsvg__parseCoordinateRaw(attr[i + 1]);
2630  } else if (strcmp(attr[i], "r") == 0) {
2631  grad->radial.r = nsvg__parseCoordinateRaw(attr[i + 1]);
2632  } else if (strcmp(attr[i], "fx") == 0) {
2633  grad->radial.fx = nsvg__parseCoordinateRaw(attr[i + 1]);
2634  } else if (strcmp(attr[i], "fy") == 0) {
2635  grad->radial.fy = nsvg__parseCoordinateRaw(attr[i + 1]);
2636  } else if (strcmp(attr[i], "x1") == 0) {
2637  grad->linear.x1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2638  } else if (strcmp(attr[i], "y1") == 0) {
2639  grad->linear.y1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2640  } else if (strcmp(attr[i], "x2") == 0) {
2641  grad->linear.x2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2642  } else if (strcmp(attr[i], "y2") == 0) {
2643  grad->linear.y2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2644  } else if (strcmp(attr[i], "spreadMethod") == 0) {
2645  if (strcmp(attr[i+1], "pad") == 0)
2646  grad->spread = NSVG_SPREAD_PAD;
2647  else if (strcmp(attr[i+1], "reflect") == 0)
2648  grad->spread = NSVG_SPREAD_REFLECT;
2649  else if (strcmp(attr[i+1], "repeat") == 0)
2650  grad->spread = NSVG_SPREAD_REPEAT;
2651  } else if (strcmp(attr[i], "xlink:href") == 0) {
2652  const char *href = attr[i+1];
2653  strncpy(grad->ref, href+1, 62);
2654  grad->ref[62] = '\0';
2655  }
2656  }
2657  }
2658 
2659  grad->next = p->gradients;
2660  p->gradients = grad;
2661 }
2662 
2663 static void nsvg__parseGradientStop(NSVGparser* p, const char** attr)
2664 {
2665  NSVGattrib* curAttr = nsvg__getAttr(p);
2666  NSVGgradientData* grad;
2667  NSVGgradientStop* stop;
2668  int i, idx;
2669 
2670  curAttr->stopOffset = 0;
2671  curAttr->stopColor = 0;
2672  curAttr->stopOpacity = 1.0f;
2673 
2674  for (i = 0; attr[i]; i += 2) {
2675  nsvg__parseAttr(p, attr[i], attr[i + 1]);
2676  }
2677 
2678  // Add stop to the last gradient.
2679  grad = p->gradients;
2680  if (grad == NULL) return;
2681 
2682  grad->nstops++;
2683  grad->stops = (NSVGgradientStop*)realloc(grad->stops, sizeof(NSVGgradientStop)*grad->nstops);
2684  if (grad->stops == NULL) return;
2685 
2686  // Insert
2687  idx = grad->nstops-1;
2688  for (i = 0; i < grad->nstops-1; i++) {
2689  if (curAttr->stopOffset < grad->stops[i].offset) {
2690  idx = i;
2691  break;
2692  }
2693  }
2694  if (idx != grad->nstops-1) {
2695  for (i = grad->nstops-1; i > idx; i--)
2696  grad->stops[i] = grad->stops[i-1];
2697  }
2698 
2699  stop = &grad->stops[idx];
2700  stop->color = curAttr->stopColor;
2701  stop->color |= (unsigned int)(curAttr->stopOpacity*255) << 24;
2702  stop->offset = curAttr->stopOffset;
2703 }
2704 
2705 static void nsvg__startElement(void* ud, const char* el, const char** attr)
2706 {
2707  NSVGparser* p = (NSVGparser*)ud;
2708 
2709  if (p->defsFlag) {
2710  // Skip everything but gradients in defs
2711  if (strcmp(el, "linearGradient") == 0) {
2712  nsvg__parseGradient(p, attr, NSVG_PAINT_LINEAR_GRADIENT);
2713  } else if (strcmp(el, "radialGradient") == 0) {
2714  nsvg__parseGradient(p, attr, NSVG_PAINT_RADIAL_GRADIENT);
2715  } else if (strcmp(el, "stop") == 0) {
2716  nsvg__parseGradientStop(p, attr);
2717  }
2718  return;
2719  }
2720 
2721  if (strcmp(el, "g") == 0) {
2722  nsvg__pushAttr(p);
2723  nsvg__parseAttribs(p, attr);
2724  } else if (strcmp(el, "path") == 0) {
2725  if (p->pathFlag) // Do not allow nested paths.
2726  return;
2727  nsvg__pushAttr(p);
2728  nsvg__parsePath(p, attr);
2729  nsvg__popAttr(p);
2730  } else if (strcmp(el, "rect") == 0) {
2731  nsvg__pushAttr(p);
2732  nsvg__parseRect(p, attr);
2733  nsvg__popAttr(p);
2734  } else if (strcmp(el, "circle") == 0) {
2735  nsvg__pushAttr(p);
2736  nsvg__parseCircle(p, attr);
2737  nsvg__popAttr(p);
2738  } else if (strcmp(el, "ellipse") == 0) {
2739  nsvg__pushAttr(p);
2740  nsvg__parseEllipse(p, attr);
2741  nsvg__popAttr(p);
2742  } else if (strcmp(el, "line") == 0) {
2743  nsvg__pushAttr(p);
2744  nsvg__parseLine(p, attr);
2745  nsvg__popAttr(p);
2746  } else if (strcmp(el, "polyline") == 0) {
2747  nsvg__pushAttr(p);
2748  nsvg__parsePoly(p, attr, 0);
2749  nsvg__popAttr(p);
2750  } else if (strcmp(el, "polygon") == 0) {
2751  nsvg__pushAttr(p);
2752  nsvg__parsePoly(p, attr, 1);
2753  nsvg__popAttr(p);
2754  } else if (strcmp(el, "linearGradient") == 0) {
2755  nsvg__parseGradient(p, attr, NSVG_PAINT_LINEAR_GRADIENT);
2756  } else if (strcmp(el, "radialGradient") == 0) {
2757  nsvg__parseGradient(p, attr, NSVG_PAINT_RADIAL_GRADIENT);
2758  } else if (strcmp(el, "stop") == 0) {
2759  nsvg__parseGradientStop(p, attr);
2760  } else if (strcmp(el, "defs") == 0) {
2761  p->defsFlag = 1;
2762  } else if (strcmp(el, "svg") == 0) {
2763  nsvg__parseSVG(p, attr);
2764  }
2765 }
2766 
2767 static void nsvg__endElement(void* ud, const char* el)
2768 {
2769  NSVGparser* p = (NSVGparser*)ud;
2770 
2771  if (strcmp(el, "g") == 0) {
2772  nsvg__popAttr(p);
2773  } else if (strcmp(el, "path") == 0) {
2774  p->pathFlag = 0;
2775  } else if (strcmp(el, "defs") == 0) {
2776  p->defsFlag = 0;
2777  }
2778 }
2779 
2780 static void nsvg__content(void* ud, const char* s)
2781 {
2782  NSVG_NOTUSED(ud);
2783  NSVG_NOTUSED(s);
2784  // empty
2785 }
2786 
2787 static void nsvg__imageBounds(NSVGparser* p, float* bounds)
2788 {
2789  NSVGshape* shape;
2790  shape = p->image->shapes;
2791  if (shape == NULL) {
2792  bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0;
2793  return;
2794  }
2795  bounds[0] = shape->bounds[0];
2796  bounds[1] = shape->bounds[1];
2797  bounds[2] = shape->bounds[2];
2798  bounds[3] = shape->bounds[3];
2799  for (shape = shape->next; shape != NULL; shape = shape->next) {
2800  bounds[0] = nsvg__minf(bounds[0], shape->bounds[0]);
2801  bounds[1] = nsvg__minf(bounds[1], shape->bounds[1]);
2802  bounds[2] = nsvg__maxf(bounds[2], shape->bounds[2]);
2803  bounds[3] = nsvg__maxf(bounds[3], shape->bounds[3]);
2804  }
2805 }
2806 
2807 static float nsvg__viewAlign(float content, float container, int type)
2808 {
2809  if (type == NSVG_ALIGN_MIN)
2810  return 0;
2811  else if (type == NSVG_ALIGN_MAX)
2812  return container - content;
2813  // mid
2814  return (container - content) * 0.5f;
2815 }
2816 
2817 static void nsvg__scaleGradient(NSVGgradient* grad, float tx, float ty, float sx, float sy)
2818 {
2819  float t[6];
2820  nsvg__xformSetTranslation(t, tx, ty);
2821  nsvg__xformMultiply (grad->xform, t);
2822 
2823  nsvg__xformSetScale(t, sx, sy);
2824  nsvg__xformMultiply (grad->xform, t);
2825 }
2826 
2827 static void nsvg__scaleToViewbox(NSVGparser* p, const char* units)
2828 {
2829  NSVGshape* shape;
2830  NSVGpath* path;
2831  float tx, ty, sx, sy, us, bounds[4], t[6], avgs;
2832  int i;
2833  float* pt;
2834 
2835  // Guess image size if not set completely.
2836  nsvg__imageBounds(p, bounds);
2837 
2838  if (p->viewWidth == 0) {
2839  if (p->image->width > 0) {
2840  p->viewWidth = p->image->width;
2841  } else {
2842  p->viewMinx = bounds[0];
2843  p->viewWidth = bounds[2] - bounds[0];
2844  }
2845  }
2846  if (p->viewHeight == 0) {
2847  if (p->image->height > 0) {
2848  p->viewHeight = p->image->height;
2849  } else {
2850  p->viewMiny = bounds[1];
2851  p->viewHeight = bounds[3] - bounds[1];
2852  }
2853  }
2854  if (p->image->width == 0)
2855  p->image->width = p->viewWidth;
2856  if (p->image->height == 0)
2857  p->image->height = p->viewHeight;
2858 
2859  tx = -p->viewMinx;
2860  ty = -p->viewMiny;
2861  sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0;
2862  sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0;
2863  // Unit scaling
2864  us = 1.0f / nsvg__convertToPixels(p, nsvg__coord(1.0f, nsvg__parseUnits(units)), 0.0f, 1.0f);
2865 
2866  // Fix aspect ratio
2867  if (p->alignType == NSVG_ALIGN_MEET) {
2868  // fit whole image into viewbox
2869  sx = sy = nsvg__minf(sx, sy);
2870  tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
2871  ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
2872  } else if (p->alignType == NSVG_ALIGN_SLICE) {
2873  // fill whole viewbox with image
2874  sx = sy = nsvg__maxf(sx, sy);
2875  tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
2876  ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
2877  }
2878 
2879  // Transform
2880  sx *= us;
2881  sy *= us;
2882  avgs = (sx+sy) / 2.0f;
2883  for (shape = p->image->shapes; shape != NULL; shape = shape->next) {
2884  shape->bounds[0] = (shape->bounds[0] + tx) * sx;
2885  shape->bounds[1] = (shape->bounds[1] + ty) * sy;
2886  shape->bounds[2] = (shape->bounds[2] + tx) * sx;
2887  shape->bounds[3] = (shape->bounds[3] + ty) * sy;
2888  for (path = shape->paths; path != NULL; path = path->next) {
2889  path->bounds[0] = (path->bounds[0] + tx) * sx;
2890  path->bounds[1] = (path->bounds[1] + ty) * sy;
2891  path->bounds[2] = (path->bounds[2] + tx) * sx;
2892  path->bounds[3] = (path->bounds[3] + ty) * sy;
2893  for (i =0; i < path->npts; i++) {
2894  pt = &path->pts[i*2];
2895  pt[0] = (pt[0] + tx) * sx;
2896  pt[1] = (pt[1] + ty) * sy;
2897  }
2898  }
2899 
2900  if (shape->fill.type == NSVG_PAINT_LINEAR_GRADIENT || shape->fill.type == NSVG_PAINT_RADIAL_GRADIENT) {
2901  nsvg__scaleGradient(shape->fill.gradient, tx,ty, sx,sy);
2902  memcpy(t, shape->fill.gradient->xform, sizeof(float)*6);
2903  nsvg__xformInverse(shape->fill.gradient->xform, t);
2904  }
2905  if (shape->stroke.type == NSVG_PAINT_LINEAR_GRADIENT || shape->stroke.type == NSVG_PAINT_RADIAL_GRADIENT) {
2906  nsvg__scaleGradient(shape->stroke.gradient, tx,ty, sx,sy);
2907  memcpy(t, shape->stroke.gradient->xform, sizeof(float)*6);
2908  nsvg__xformInverse(shape->stroke.gradient->xform, t);
2909  }
2910 
2911  shape->strokeWidth *= avgs;
2912  shape->strokeDashOffset *= avgs;
2913  for (i = 0; i < shape->strokeDashCount; i++)
2914  shape->strokeDashArray[i] *= avgs;
2915  }
2916 }
2917 
2918 NSVGimage* nsvgParse(char* input, const char* units, float dpi)
2919 {
2920  NSVGparser* p;
2921  NSVGimage* ret = 0;
2922 
2923  p = nsvg__createParser();
2924  if (p == NULL) {
2925  return NULL;
2926  }
2927  p->dpi = dpi;
2928 
2929  nsvg__parseXML(input, nsvg__startElement, nsvg__endElement, nsvg__content, p);
2930 
2931  // Scale to viewBox
2932  nsvg__scaleToViewbox(p, units);
2933 
2934  ret = p->image;
2935  p->image = NULL;
2936 
2937  nsvg__deleteParser(p);
2938 
2939  return ret;
2940 }
2941 
2942 #if 0
2943 NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi)
2944 {
2945  FILE* fp = NULL;
2946  size_t size;
2947  char* data = NULL;
2948  NSVGimage* image = NULL;
2949 
2950  fp = fopen(filename, "rb");
2951  if (!fp) goto error;
2952  fseek(fp, 0, SEEK_END);
2953  size = ftell(fp);
2954  fseek(fp, 0, SEEK_SET);
2955  data = (char*)malloc(size+1);
2956  if (data == NULL) goto error;
2957  if (fread(data, 1, size, fp) != size) goto error;
2958  data[size] = '\0'; // Must be null terminated.
2959  fclose(fp);
2960  image = nsvgParse(data, units, dpi);
2961  free(data);
2962 
2963  return image;
2964 
2965 error:
2966  if (fp) fclose(fp);
2967  if (data) free(data);
2968  if (image) nsvgDelete(image);
2969  return NULL;
2970 }
2971 #endif
2972 
2973 NSVGpath* nsvgDuplicatePath(NSVGpath* p)
2974 {
2975  NSVGpath* res = NULL;
2976 
2977  if (p == NULL)
2978  return NULL;
2979 
2980  res = (NSVGpath*)malloc(sizeof(NSVGpath));
2981  if (res == NULL) goto error;
2982  memset(res, 0, sizeof(NSVGpath));
2983 
2984  res->pts = (float*)malloc(p->npts*2*sizeof(float));
2985  if (res->pts == NULL) goto error;
2986  memcpy(res->pts, p->pts, p->npts * sizeof(float) * 2);
2987  res->npts = p->npts;
2988 
2989  memcpy(res->bounds, p->bounds, sizeof(p->bounds));
2990 
2991  res->closed = p->closed;
2992 
2993  return res;
2994 
2995 error:
2996  if (res != NULL) {
2997  free(res->pts);
2998  free(res);
2999  }
3000  return NULL;
3001 }
3002 
3003 void nsvgDelete(NSVGimage* image)
3004 {
3005  NSVGshape *snext, *shape;
3006  if (image == NULL) return;
3007  shape = image->shapes;
3008  while (shape != NULL) {
3009  snext = shape->next;
3010  nsvg__deletePaths(shape->paths);
3011  nsvg__deletePaint(&shape->fill);
3012  nsvg__deletePaint(&shape->stroke);
3013  free(shape);
3014  shape = snext;
3015  }
3016  free(image);
3017 }
3018 
3019 #endif
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: nanosvg.h:158
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168
Definition: nanosvg.h:129
Definition: nanosvg.h:138
Definition: nanosvg.h:113
Definition: nanosvg.h:108
Definition: nanosvg.h:121