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