2 * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
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.
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:
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.
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/)
23 * Arc calculation code based on canvg (https://code.google.com/p/canvg/)
25 * Bounding box calculation based on http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
32 * In the original software, The nanosvg implementation was included in the header file.
33 * We have separated the implementation to source file here.
41 #define NSVG_PI (3.14159265358979323846264338327f)
42 #define NSVG_KAPPA90 (0.5522847493f) // Lenght proportional to radius of a cubic bezier handle for 90deg arcs.
44 #define NSVG_ALIGN_MIN 0
45 #define NSVG_ALIGN_MID 1
46 #define NSVG_ALIGN_MAX 2
47 #define NSVG_ALIGN_NONE 0
48 #define NSVG_ALIGN_MEET 1
49 #define NSVG_ALIGN_SLICE 2
51 #define NSVG_NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0)
52 #define NSVG_RGB(r, g, b) (((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16))
54 #define NSVG_INLINE inline
57 static int nsvg__isspace(char c)
59 return strchr(" \t\n\v\f\r", c) != 0;
62 static int nsvg__isdigit(char c)
64 return strchr("0123456789", c) != 0;
67 static int nsvg__isnum(char c)
69 return strchr("0123456789+-.eE", c) != 0;
72 static NSVG_INLINE float nsvg__minf(float a, float b) { return a < b ? a : b; }
73 static NSVG_INLINE float nsvg__maxf(float a, float b) { return a > b ? a : b; }
78 #define NSVG_XML_TAG 1
79 #define NSVG_XML_CONTENT 2
80 #define NSVG_XML_MAX_ATTRIBS 256
82 static void nsvg__parseContent(char* s,
83 void (*contentCb)(void* ud, const char* s),
86 // Trim start white spaces
87 while (*s && nsvg__isspace(*s)) s++;
94 static void nsvg__parseElement(char* s,
95 void (*startelCb)(void* ud, const char* el, const char** attr),
96 void (*endelCb)(void* ud, const char* el),
99 const char* attr[NSVG_XML_MAX_ATTRIBS];
106 // Skip white space after the '<'
107 while (*s && nsvg__isspace(*s)) s++;
109 // Check if the tag is end tag
117 // Skip comments, data and preprocessor stuff.
118 if (!*s || *s == '?' || *s == '!')
123 while (*s && !nsvg__isspace(*s)) s++;
124 if (*s) { *s++ = '\0'; }
127 while (!end && *s && nattr < NSVG_XML_MAX_ATTRIBS-3) {
128 // Skip white space before the attrib name
129 while (*s && nsvg__isspace(*s)) s++;
136 // Find end of the attrib name.
137 while (*s && !nsvg__isspace(*s) && *s != '=') s++;
138 if (*s) { *s++ = '\0'; }
139 // Skip until the beginning of the value.
140 while (*s && *s != '\"' && *s != '\'') s++;
144 // Store value and find the end of it.
146 while (*s && *s != quote) s++;
147 if (*s) { *s++ = '\0'; }
155 if (start && startelCb)
156 (*startelCb)(ud, name, attr);
158 (*endelCb)(ud, name);
161 int nsvg__parseXML(char* input,
162 void (*startelCb)(void* ud, const char* el, const char** attr),
163 void (*endelCb)(void* ud, const char* el),
164 void (*contentCb)(void* ud, const char* s),
169 int state = NSVG_XML_CONTENT;
171 if (*s == '<' && state == NSVG_XML_CONTENT) {
174 nsvg__parseContent(mark, contentCb, ud);
176 state = NSVG_XML_TAG;
177 } else if (*s == '>' && state == NSVG_XML_TAG) {
178 // Start of a content or new tag.
180 nsvg__parseElement(mark, startelCb, endelCb, ud);
182 state = NSVG_XML_CONTENT;
192 /* Simple SVG parser. */
194 #define NSVG_MAX_ATTR 128
196 enum NSVGgradientUnits {
198 NSVG_OBJECT_SPACE = 1,
201 #define NSVG_MAX_DASHES 8
216 typedef struct NSVGcoordinate {
221 typedef struct NSVGlinearData {
222 NSVGcoordinate x1, y1, x2, y2;
225 typedef struct NSVGradialData {
226 NSVGcoordinate cx, cy, r, fx, fy;
229 typedef struct NSVGgradientData
235 * In the original file, using char type (without signed or unsigned) can be interpreted
236 * as 'unsigned char' in some build environments, like ARM architecture.
237 * To prevent the unexpected behavior, we replace 'char type' with 'signed char type' here.
241 NSVGlinearData linear;
242 NSVGradialData radial;
247 * In the original file, using char type (without signed or unsigned) can be interpreted
248 * as 'unsigned char' in some build environments, like ARM architecture.
249 * To prevent the unexpected behavior, we replace 'char units' with 'signed char units' here.
254 NSVGgradientStop* stops;
255 struct NSVGgradientData* next;
258 typedef struct NSVGattrib
262 unsigned int fillColor;
263 unsigned int strokeColor;
267 char fillGradient[64];
268 char strokeGradient[64];
270 float strokeDashOffset;
271 float strokeDashArray[NSVG_MAX_DASHES];
277 unsigned int stopColor;
285 typedef struct NSVGparser
287 NSVGattrib attr[NSVG_MAX_ATTR];
294 NSVGgradientData* gradients;
295 float viewMinx, viewMiny, viewWidth, viewHeight;
296 int alignX, alignY, alignType;
302 static void nsvg__xformIdentity(float* t)
304 t[0] = 1.0f; t[1] = 0.0f;
305 t[2] = 0.0f; t[3] = 1.0f;
306 t[4] = 0.0f; t[5] = 0.0f;
309 static void nsvg__xformSetTranslation(float* t, float tx, float ty)
311 t[0] = 1.0f; t[1] = 0.0f;
312 t[2] = 0.0f; t[3] = 1.0f;
313 t[4] = tx; t[5] = ty;
316 static void nsvg__xformSetScale(float* t, float sx, float sy)
318 t[0] = sx; t[1] = 0.0f;
319 t[2] = 0.0f; t[3] = sy;
320 t[4] = 0.0f; t[5] = 0.0f;
323 static void nsvg__xformSetSkewX(float* t, float a)
325 t[0] = 1.0f; t[1] = 0.0f;
326 t[2] = tanf(a); t[3] = 1.0f;
327 t[4] = 0.0f; t[5] = 0.0f;
330 static void nsvg__xformSetSkewY(float* t, float a)
332 t[0] = 1.0f; t[1] = tanf(a);
333 t[2] = 0.0f; t[3] = 1.0f;
334 t[4] = 0.0f; t[5] = 0.0f;
337 static void nsvg__xformSetRotation(float* t, float a)
339 float cs = cosf(a), sn = sinf(a);
340 t[0] = cs; t[1] = sn;
341 t[2] = -sn; t[3] = cs;
342 t[4] = 0.0f; t[5] = 0.0f;
345 static void nsvg__xformMultiply(float* t, float* s)
347 float t0 = t[0] * s[0] + t[1] * s[2];
348 float t2 = t[2] * s[0] + t[3] * s[2];
349 float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
350 t[1] = t[0] * s[1] + t[1] * s[3];
351 t[3] = t[2] * s[1] + t[3] * s[3];
352 t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
358 static void nsvg__xformInverse(float* inv, float* t)
360 double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1];
361 if (det > -1e-6 && det < 1e-6) {
362 nsvg__xformIdentity(t);
366 inv[0] = (float)(t[3] * invdet);
367 inv[2] = (float)(-t[2] * invdet);
368 inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
369 inv[1] = (float)(-t[1] * invdet);
370 inv[3] = (float)(t[0] * invdet);
371 inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
374 static void nsvg__xformPremultiply(float* t, float* s)
377 memcpy(s2, s, sizeof(float)*6);
378 nsvg__xformMultiply(s2, t);
379 memcpy(t, s2, sizeof(float)*6);
382 static void nsvg__xformPoint(float* dx, float* dy, float x, float y, float* t)
384 *dx = x*t[0] + y*t[2] + t[4];
385 *dy = x*t[1] + y*t[3] + t[5];
388 static void nsvg__xformVec(float* dx, float* dy, float x, float y, float* t)
390 *dx = x*t[0] + y*t[2];
391 *dy = x*t[1] + y*t[3];
394 #define NSVG_EPSILON (1e-12)
396 static int nsvg__ptInBounds(float* pt, float* bounds)
398 return pt[0] >= bounds[0] && pt[0] <= bounds[2] && pt[1] >= bounds[1] && pt[1] <= bounds[3];
402 static double nsvg__evalBezier(double t, double p0, double p1, double p2, double p3)
405 return it*it*it*p0 + 3.0*it*it*t*p1 + 3.0*it*t*t*p2 + t*t*t*p3;
408 static void nsvg__curveBounds(float* bounds, float* curve)
411 double roots[2], a, b, c, b2ac, t, v;
412 float* v0 = &curve[0];
413 float* v1 = &curve[2];
414 float* v2 = &curve[4];
415 float* v3 = &curve[6];
417 // Start the bounding box by end points
418 bounds[0] = nsvg__minf(v0[0], v3[0]);
419 bounds[1] = nsvg__minf(v0[1], v3[1]);
420 bounds[2] = nsvg__maxf(v0[0], v3[0]);
421 bounds[3] = nsvg__maxf(v0[1], v3[1]);
423 // Bezier curve fits inside the convex hull of it's control points.
424 // If control points are inside the bounds, we're done.
425 if (nsvg__ptInBounds(v1, bounds) && nsvg__ptInBounds(v2, bounds))
428 // Add bezier curve inflection points in X and Y.
429 for (i = 0; i < 2; i++) {
430 a = -3.0 * v0[i] + 9.0 * v1[i] - 9.0 * v2[i] + 3.0 * v3[i];
431 b = 6.0 * v0[i] - 12.0 * v1[i] + 6.0 * v2[i];
432 c = 3.0 * v1[i] - 3.0 * v0[i];
434 if (fabs(a) < NSVG_EPSILON) {
435 if (fabs(b) > NSVG_EPSILON) {
437 if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
441 b2ac = b*b - 4.0*c*a;
442 if (b2ac > NSVG_EPSILON) {
443 t = (-b + sqrt(b2ac)) / (2.0 * a);
444 if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
446 t = (-b - sqrt(b2ac)) / (2.0 * a);
447 if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
451 for (j = 0; j < count; j++) {
452 v = nsvg__evalBezier(roots[j], v0[i], v1[i], v2[i], v3[i]);
453 bounds[0+i] = nsvg__minf(bounds[0+i], (float)v);
454 bounds[2+i] = nsvg__maxf(bounds[2+i], (float)v);
459 static NSVGparser* nsvg__createParser()
462 p = (NSVGparser*)malloc(sizeof(NSVGparser));
463 if (p == NULL) goto error;
464 memset(p, 0, sizeof(NSVGparser));
466 p->image = (NSVGimage*)malloc(sizeof(NSVGimage));
467 if (p->image == NULL) goto error;
468 memset(p->image, 0, sizeof(NSVGimage));
471 nsvg__xformIdentity(p->attr[0].xform);
472 memset(p->attr[0].id, 0, sizeof p->attr[0].id);
473 p->attr[0].fillColor = NSVG_RGB(0,0,0);
474 p->attr[0].strokeColor = NSVG_RGB(0,0,0);
475 p->attr[0].opacity = 1;
476 p->attr[0].fillOpacity = 1;
477 p->attr[0].strokeOpacity = 1;
478 p->attr[0].stopOpacity = 1;
479 p->attr[0].strokeWidth = 1;
480 p->attr[0].strokeLineJoin = NSVG_JOIN_MITER;
481 p->attr[0].strokeLineCap = NSVG_CAP_BUTT;
482 p->attr[0].fillRule = NSVG_FILLRULE_NONZERO;
483 p->attr[0].hasFill = 1;
484 p->attr[0].visible = 1;
490 if (p->image) free(p->image);
496 static void nsvg__deletePaths(NSVGpath* path)
499 NSVGpath *next = path->next;
500 if (path->pts != NULL)
507 static void nsvg__deletePaint(NSVGpaint* paint)
509 if (paint->type == NSVG_PAINT_LINEAR_GRADIENT || paint->type == NSVG_PAINT_RADIAL_GRADIENT)
510 free(paint->gradient);
513 static void nsvg__deleteGradientData(NSVGgradientData* grad)
515 NSVGgradientData* next;
516 while (grad != NULL) {
524 static void nsvg__deleteParser(NSVGparser* p)
527 nsvg__deletePaths(p->plist);
528 nsvg__deleteGradientData(p->gradients);
529 nsvgDelete(p->image);
535 static void nsvg__resetPath(NSVGparser* p)
540 static void nsvg__addPoint(NSVGparser* p, float x, float y)
542 if (p->npts+1 > p->cpts) {
543 p->cpts = p->cpts ? p->cpts*2 : 8;
544 p->pts = (float*)realloc(p->pts, p->cpts*2*sizeof(float));
547 p->pts[p->npts*2+0] = x;
548 p->pts[p->npts*2+1] = y;
552 static void nsvg__moveTo(NSVGparser* p, float x, float y)
555 p->pts[(p->npts-1)*2+0] = x;
556 p->pts[(p->npts-1)*2+1] = y;
558 nsvg__addPoint(p, x, y);
562 static void nsvg__lineTo(NSVGparser* p, float x, float y)
566 px = p->pts[(p->npts-1)*2+0];
567 py = p->pts[(p->npts-1)*2+1];
570 nsvg__addPoint(p, px + dx/3.0f, py + dy/3.0f);
571 nsvg__addPoint(p, x - dx/3.0f, y - dy/3.0f);
572 nsvg__addPoint(p, x, y);
576 static void nsvg__cubicBezTo(NSVGparser* p, float cpx1, float cpy1, float cpx2, float cpy2, float x, float y)
578 nsvg__addPoint(p, cpx1, cpy1);
579 nsvg__addPoint(p, cpx2, cpy2);
580 nsvg__addPoint(p, x, y);
583 static NSVGattrib* nsvg__getAttr(NSVGparser* p)
585 return &p->attr[p->attrHead];
588 static void nsvg__pushAttr(NSVGparser* p)
590 if (p->attrHead < NSVG_MAX_ATTR-1) {
592 memcpy(&p->attr[p->attrHead], &p->attr[p->attrHead-1], sizeof(NSVGattrib));
596 static void nsvg__popAttr(NSVGparser* p)
602 static float nsvg__actualOrigX(NSVGparser* p)
607 static float nsvg__actualOrigY(NSVGparser* p)
612 static float nsvg__actualWidth(NSVGparser* p)
617 static float nsvg__actualHeight(NSVGparser* p)
619 return p->viewHeight;
622 static float nsvg__actualLength(NSVGparser* p)
624 float w = nsvg__actualWidth(p), h = nsvg__actualHeight(p);
625 return sqrtf(w*w + h*h) / sqrtf(2.0f);
628 static float nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c, float orig, float length)
630 NSVGattrib* attr = nsvg__getAttr(p);
632 case NSVG_UNITS_USER: return c.value;
633 case NSVG_UNITS_PX: return c.value;
634 case NSVG_UNITS_PT: return c.value / 72.0f * p->dpi;
635 case NSVG_UNITS_PC: return c.value / 6.0f * p->dpi;
636 case NSVG_UNITS_MM: return c.value / 25.4f * p->dpi;
637 case NSVG_UNITS_CM: return c.value / 2.54f * p->dpi;
638 case NSVG_UNITS_IN: return c.value * p->dpi;
639 case NSVG_UNITS_EM: return c.value * attr->fontSize;
640 case NSVG_UNITS_EX: return c.value * attr->fontSize * 0.52f; // x-height of Helvetica.
641 case NSVG_UNITS_PERCENT: return orig + c.value / 100.0f * length;
642 default: return c.value;
647 static NSVGgradientData* nsvg__findGradientData(NSVGparser* p, const char* id)
649 NSVGgradientData* grad = p->gradients;
651 if (strcmp(grad->id, id) == 0)
659 * In the original file, using char type (without signed or unsigned) can be interpreted
660 * as 'unsigned char' in some build environments, like ARM architecture.
661 * To prevent the unexpected behavior, we replace 'char paintType' with 'signed char paintType' here.
663 static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const float* localBounds, signed char* paintType)
665 NSVGattrib* attr = nsvg__getAttr(p);
666 NSVGgradientData* data = NULL;
667 NSVGgradientData* ref = NULL;
668 NSVGgradientStop* stops = NULL;
670 float ox, oy, sw, sh, sl;
673 data = nsvg__findGradientData(p, id);
674 if (data == NULL) return NULL;
676 // TODO: use ref to fill in all unset values too.
678 while (ref != NULL) {
679 if (stops == NULL && ref->stops != NULL) {
681 nstops = ref->nstops;
684 ref = nsvg__findGradientData(p, ref->ref);
686 if (stops == NULL) return NULL;
688 grad = (NSVGgradient*)malloc(sizeof(NSVGgradient) + sizeof(NSVGgradientStop)*(nstops-1));
689 if (grad == NULL) return NULL;
691 // The shape width and height.
692 if (data->units == NSVG_OBJECT_SPACE) {
695 sw = localBounds[2] - localBounds[0];
696 sh = localBounds[3] - localBounds[1];
698 ox = nsvg__actualOrigX(p);
699 oy = nsvg__actualOrigY(p);
700 sw = nsvg__actualWidth(p);
701 sh = nsvg__actualHeight(p);
703 sl = sqrtf(sw*sw + sh*sh) / sqrtf(2.0f);
705 if (data->type == NSVG_PAINT_LINEAR_GRADIENT) {
706 float x1, y1, x2, y2, dx, dy;
707 x1 = nsvg__convertToPixels(p, data->linear.x1, ox, sw);
708 y1 = nsvg__convertToPixels(p, data->linear.y1, oy, sh);
709 x2 = nsvg__convertToPixels(p, data->linear.x2, ox, sw);
710 y2 = nsvg__convertToPixels(p, data->linear.y2, oy, sh);
711 // Calculate transform aligned to the line
714 grad->xform[0] = dy; grad->xform[1] = -dx;
715 grad->xform[2] = dx; grad->xform[3] = dy;
716 grad->xform[4] = x1; grad->xform[5] = y1;
718 float cx, cy, fx, fy, r;
719 cx = nsvg__convertToPixels(p, data->radial.cx, ox, sw);
720 cy = nsvg__convertToPixels(p, data->radial.cy, oy, sh);
721 fx = nsvg__convertToPixels(p, data->radial.fx, ox, sw);
722 fy = nsvg__convertToPixels(p, data->radial.fy, oy, sh);
723 r = nsvg__convertToPixels(p, data->radial.r, 0, sl);
724 // Calculate transform aligned to the circle
725 grad->xform[0] = r; grad->xform[1] = 0;
726 grad->xform[2] = 0; grad->xform[3] = r;
727 grad->xform[4] = cx; grad->xform[5] = cy;
732 if( data->units == NSVG_OBJECT_SPACE)
734 float scaling[6],t[6];
735 scaling[0] = 1.0 / ( localBounds[2] - localBounds[0] );
738 scaling[3] = 1.0 / ( localBounds[3] - localBounds[1]);
739 scaling[4] = -localBounds[0] * scaling[0];
740 scaling[5] = -localBounds[1] * scaling[3];
741 memcpy(t, scaling, sizeof(float)*6);
742 nsvg__xformInverse(scaling, t);
743 nsvg__xformMultiply(grad->xform, data->xform);
744 nsvg__xformMultiply(grad->xform, scaling);
745 nsvg__xformMultiply(grad->xform, attr->xform);
749 nsvg__xformMultiply(grad->xform, data->xform);
750 nsvg__xformMultiply(grad->xform, attr->xform);
753 grad->spread = data->spread;
754 memcpy(grad->stops, stops, nstops*sizeof(NSVGgradientStop));
755 grad->nstops = nstops;
757 *paintType = data->type;
762 static float nsvg__getAverageScale(float* t)
764 float sx = sqrtf(t[0]*t[0] + t[2]*t[2]);
765 float sy = sqrtf(t[1]*t[1] + t[3]*t[3]);
766 return (sx + sy) * 0.5f;
769 static void nsvg__getLocalBounds(float* bounds, NSVGshape *shape, float* xform)
772 float curve[4*2], curveBounds[4];
774 for (path = shape->paths; path != NULL; path = path->next) {
775 nsvg__xformPoint(&curve[0], &curve[1], path->pts[0], path->pts[1], xform);
776 for (i = 0; i < path->npts-1; i += 3) {
777 nsvg__xformPoint(&curve[2], &curve[3], path->pts[(i+1)*2], path->pts[(i+1)*2+1], xform);
778 nsvg__xformPoint(&curve[4], &curve[5], path->pts[(i+2)*2], path->pts[(i+2)*2+1], xform);
779 nsvg__xformPoint(&curve[6], &curve[7], path->pts[(i+3)*2], path->pts[(i+3)*2+1], xform);
780 nsvg__curveBounds(curveBounds, curve);
782 bounds[0] = curveBounds[0];
783 bounds[1] = curveBounds[1];
784 bounds[2] = curveBounds[2];
785 bounds[3] = curveBounds[3];
788 bounds[0] = nsvg__minf(bounds[0], curveBounds[0]);
789 bounds[1] = nsvg__minf(bounds[1], curveBounds[1]);
790 bounds[2] = nsvg__maxf(bounds[2], curveBounds[2]);
791 bounds[3] = nsvg__maxf(bounds[3], curveBounds[3]);
799 static void nsvg__addShape(NSVGparser* p)
801 NSVGattrib* attr = nsvg__getAttr(p);
803 NSVGshape *shape, *cur, *prev;
807 if (p->plist == NULL)
810 shape = (NSVGshape*)malloc(sizeof(NSVGshape));
811 if (shape == NULL) goto error;
812 memset(shape, 0, sizeof(NSVGshape));
814 memcpy(shape->id, attr->id, sizeof shape->id);
815 scale = nsvg__getAverageScale(attr->xform);
816 shape->strokeWidth = attr->strokeWidth * scale;
817 shape->strokeDashOffset = attr->strokeDashOffset * scale;
818 shape->strokeDashCount = attr->strokeDashCount;
819 for (i = 0; i < attr->strokeDashCount; i++)
820 shape->strokeDashArray[i] = attr->strokeDashArray[i] * scale;
821 shape->strokeLineJoin = attr->strokeLineJoin;
822 shape->strokeLineCap = attr->strokeLineCap;
823 shape->fillRule = attr->fillRule;
824 shape->opacity = attr->opacity;
826 shape->paths = p->plist;
829 // Calculate shape bounds
830 shape->bounds[0] = shape->paths->bounds[0];
831 shape->bounds[1] = shape->paths->bounds[1];
832 shape->bounds[2] = shape->paths->bounds[2];
833 shape->bounds[3] = shape->paths->bounds[3];
834 for (path = shape->paths->next; path != NULL; path = path->next) {
835 shape->bounds[0] = nsvg__minf(shape->bounds[0], path->bounds[0]);
836 shape->bounds[1] = nsvg__minf(shape->bounds[1], path->bounds[1]);
837 shape->bounds[2] = nsvg__maxf(shape->bounds[2], path->bounds[2]);
838 shape->bounds[3] = nsvg__maxf(shape->bounds[3], path->bounds[3]);
842 if (attr->hasFill == 0) {
843 shape->fill.type = NSVG_PAINT_NONE;
844 } else if (attr->hasFill == 1) {
845 shape->fill.type = NSVG_PAINT_COLOR;
846 shape->fill.color = attr->fillColor;
847 shape->fill.color |= (unsigned int)(attr->fillOpacity*255) << 24;
848 } else if (attr->hasFill == 2) {
849 shape->opacity *= attr->fillOpacity;
850 float inv[6], localBounds[4];
851 nsvg__xformInverse(inv, attr->xform);
852 nsvg__getLocalBounds(localBounds, shape, inv);
853 shape->fill.gradient = nsvg__createGradient(p, attr->fillGradient, localBounds, &shape->fill.type);
854 if (shape->fill.gradient == NULL) {
855 shape->fill.type = NSVG_PAINT_NONE;
860 if (attr->hasStroke == 0) {
861 shape->stroke.type = NSVG_PAINT_NONE;
862 } else if (attr->hasStroke == 1) {
863 shape->stroke.type = NSVG_PAINT_COLOR;
864 shape->stroke.color = attr->strokeColor;
865 shape->stroke.color |= (unsigned int)(attr->strokeOpacity*255) << 24;
866 } else if (attr->hasStroke == 2) {
867 float inv[6], localBounds[4];
868 nsvg__xformInverse(inv, attr->xform);
869 nsvg__getLocalBounds(localBounds, shape, inv);
870 shape->stroke.gradient = nsvg__createGradient(p, attr->strokeGradient, localBounds, &shape->stroke.type);
871 if (shape->stroke.gradient == NULL)
872 shape->stroke.type = NSVG_PAINT_NONE;
876 shape->flags = (attr->visible ? NSVG_FLAGS_VISIBLE : 0x00);
880 cur = p->image->shapes;
881 while (cur != NULL) {
886 p->image->shapes = shape;
893 if (shape) free(shape);
896 static void nsvg__addPath(NSVGparser* p, char closed)
898 NSVGattrib* attr = nsvg__getAttr(p);
899 NSVGpath* path = NULL;
908 nsvg__lineTo(p, p->pts[0], p->pts[1]);
910 path = (NSVGpath*)malloc(sizeof(NSVGpath));
911 if (path == NULL) goto error;
912 memset(path, 0, sizeof(NSVGpath));
914 path->pts = (float*)malloc(p->npts*2*sizeof(float));
915 if (path->pts == NULL) goto error;
916 path->closed = closed;
917 path->npts = p->npts;
920 for (i = 0; i < p->npts; ++i)
921 nsvg__xformPoint(&path->pts[i*2], &path->pts[i*2+1], p->pts[i*2], p->pts[i*2+1], attr->xform);
924 for (i = 0; i < path->npts-1; i += 3) {
925 curve = &path->pts[i*2];
926 nsvg__curveBounds(bounds, curve);
928 path->bounds[0] = bounds[0];
929 path->bounds[1] = bounds[1];
930 path->bounds[2] = bounds[2];
931 path->bounds[3] = bounds[3];
933 path->bounds[0] = nsvg__minf(path->bounds[0], bounds[0]);
934 path->bounds[1] = nsvg__minf(path->bounds[1], bounds[1]);
935 path->bounds[2] = nsvg__maxf(path->bounds[2], bounds[2]);
936 path->bounds[3] = nsvg__maxf(path->bounds[3], bounds[3]);
940 path->next = p->plist;
947 if (path->pts != NULL) free(path->pts);
952 static const char* nsvg__parseNumber(const char* s, char* it, const int size)
954 const int last = size-1;
958 if (*s == '-' || *s == '+') {
959 if (i < last) it[i++] = *s;
963 while (*s && nsvg__isdigit(*s)) {
964 if (i < last) it[i++] = *s;
969 if (i < last) it[i++] = *s;
972 while (*s && nsvg__isdigit(*s)) {
973 if (i < last) it[i++] = *s;
978 if (*s == 'e' || *s == 'E') {
979 if (i < last) it[i++] = *s;
981 if (*s == '-' || *s == '+') {
982 if (i < last) it[i++] = *s;
985 while (*s && nsvg__isdigit(*s)) {
986 if (i < last) it[i++] = *s;
995 static const char* nsvg__getNextPathItem(const char* s, char* it)
998 // Skip white spaces and commas
999 while (*s && (nsvg__isspace(*s) || *s == ',')) s++;
1001 if (*s == '-' || *s == '+' || *s == '.' || nsvg__isdigit(*s)) {
1002 s = nsvg__parseNumber(s, it, 64);
1013 static unsigned int nsvg__parseColorHex(const char* str)
1015 unsigned int c = 0, r = 0, g = 0, b = 0;
1018 // Calculate number of characters.
1019 while(str[n] && !nsvg__isspace(str[n]))
1022 sscanf(str, "%x", &c);
1023 } else if (n == 3) {
1024 sscanf(str, "%x", &c);
1025 c = (c&0xf) | ((c&0xf0) << 4) | ((c&0xf00) << 8);
1028 r = (c >> 16) & 0xff;
1029 g = (c >> 8) & 0xff;
1031 return NSVG_RGB(r,g,b);
1034 static unsigned int nsvg__parseColorRGB(const char* str)
1036 int r = -1, g = -1, b = -1;
1037 char s1[32]="", s2[32]="";
1040 * In the original file, the formatted data reading did not specify the string with width limitation.
1041 * To prevent the possible overflow, we replace '%s' with '%32s' here.
1043 sscanf(str + 4, "%d%32[%%, \t]%d%32[%%, \t]%d", &r, s1, &g, s2, &b);
1044 if (strchr(s1, '%')) {
1045 return NSVG_RGB((r*255)/100,(g*255)/100,(b*255)/100);
1047 return NSVG_RGB(r,g,b);
1051 typedef struct NSVGNamedColor {
1056 NSVGNamedColor nsvg__colors[] = {
1058 { "red", NSVG_RGB(255, 0, 0) },
1059 { "green", NSVG_RGB( 0, 128, 0) },
1060 { "blue", NSVG_RGB( 0, 0, 255) },
1061 { "yellow", NSVG_RGB(255, 255, 0) },
1062 { "cyan", NSVG_RGB( 0, 255, 255) },
1063 { "magenta", NSVG_RGB(255, 0, 255) },
1064 { "black", NSVG_RGB( 0, 0, 0) },
1065 { "grey", NSVG_RGB(128, 128, 128) },
1066 { "gray", NSVG_RGB(128, 128, 128) },
1067 { "white", NSVG_RGB(255, 255, 255) },
1069 { "aliceblue", NSVG_RGB(240, 248, 255) },
1070 { "antiquewhite", NSVG_RGB(250, 235, 215) },
1071 { "aqua", NSVG_RGB( 0, 255, 255) },
1072 { "aquamarine", NSVG_RGB(127, 255, 212) },
1073 { "azure", NSVG_RGB(240, 255, 255) },
1074 { "beige", NSVG_RGB(245, 245, 220) },
1075 { "bisque", NSVG_RGB(255, 228, 196) },
1076 { "blanchedalmond", NSVG_RGB(255, 235, 205) },
1077 { "blueviolet", NSVG_RGB(138, 43, 226) },
1078 { "brown", NSVG_RGB(165, 42, 42) },
1079 { "burlywood", NSVG_RGB(222, 184, 135) },
1080 { "cadetblue", NSVG_RGB( 95, 158, 160) },
1081 { "chartreuse", NSVG_RGB(127, 255, 0) },
1082 { "chocolate", NSVG_RGB(210, 105, 30) },
1083 { "coral", NSVG_RGB(255, 127, 80) },
1084 { "cornflowerblue", NSVG_RGB(100, 149, 237) },
1085 { "cornsilk", NSVG_RGB(255, 248, 220) },
1086 { "crimson", NSVG_RGB(220, 20, 60) },
1087 { "darkblue", NSVG_RGB( 0, 0, 139) },
1088 { "darkcyan", NSVG_RGB( 0, 139, 139) },
1089 { "darkgoldenrod", NSVG_RGB(184, 134, 11) },
1090 { "darkgray", NSVG_RGB(169, 169, 169) },
1091 { "darkgreen", NSVG_RGB( 0, 100, 0) },
1092 { "darkgrey", NSVG_RGB(169, 169, 169) },
1093 { "darkkhaki", NSVG_RGB(189, 183, 107) },
1094 { "darkmagenta", NSVG_RGB(139, 0, 139) },
1095 { "darkolivegreen", NSVG_RGB( 85, 107, 47) },
1096 { "darkorange", NSVG_RGB(255, 140, 0) },
1097 { "darkorchid", NSVG_RGB(153, 50, 204) },
1098 { "darkred", NSVG_RGB(139, 0, 0) },
1099 { "darksalmon", NSVG_RGB(233, 150, 122) },
1100 { "darkseagreen", NSVG_RGB(143, 188, 143) },
1101 { "darkslateblue", NSVG_RGB( 72, 61, 139) },
1102 { "darkslategray", NSVG_RGB( 47, 79, 79) },
1103 { "darkslategrey", NSVG_RGB( 47, 79, 79) },
1104 { "darkturquoise", NSVG_RGB( 0, 206, 209) },
1105 { "darkviolet", NSVG_RGB(148, 0, 211) },
1106 { "deeppink", NSVG_RGB(255, 20, 147) },
1107 { "deepskyblue", NSVG_RGB( 0, 191, 255) },
1108 { "dimgray", NSVG_RGB(105, 105, 105) },
1109 { "dimgrey", NSVG_RGB(105, 105, 105) },
1110 { "dodgerblue", NSVG_RGB( 30, 144, 255) },
1111 { "firebrick", NSVG_RGB(178, 34, 34) },
1112 { "floralwhite", NSVG_RGB(255, 250, 240) },
1113 { "forestgreen", NSVG_RGB( 34, 139, 34) },
1114 { "fuchsia", NSVG_RGB(255, 0, 255) },
1115 { "gainsboro", NSVG_RGB(220, 220, 220) },
1116 { "ghostwhite", NSVG_RGB(248, 248, 255) },
1117 { "gold", NSVG_RGB(255, 215, 0) },
1118 { "goldenrod", NSVG_RGB(218, 165, 32) },
1119 { "greenyellow", NSVG_RGB(173, 255, 47) },
1120 { "honeydew", NSVG_RGB(240, 255, 240) },
1121 { "hotpink", NSVG_RGB(255, 105, 180) },
1122 { "indianred", NSVG_RGB(205, 92, 92) },
1123 { "indigo", NSVG_RGB( 75, 0, 130) },
1124 { "ivory", NSVG_RGB(255, 255, 240) },
1125 { "khaki", NSVG_RGB(240, 230, 140) },
1126 { "lavender", NSVG_RGB(230, 230, 250) },
1127 { "lavenderblush", NSVG_RGB(255, 240, 245) },
1128 { "lawngreen", NSVG_RGB(124, 252, 0) },
1129 { "lemonchiffon", NSVG_RGB(255, 250, 205) },
1130 { "lightblue", NSVG_RGB(173, 216, 230) },
1131 { "lightcoral", NSVG_RGB(240, 128, 128) },
1132 { "lightcyan", NSVG_RGB(224, 255, 255) },
1133 { "lightgoldenrodyellow", NSVG_RGB(250, 250, 210) },
1134 { "lightgray", NSVG_RGB(211, 211, 211) },
1135 { "lightgreen", NSVG_RGB(144, 238, 144) },
1136 { "lightgrey", NSVG_RGB(211, 211, 211) },
1137 { "lightpink", NSVG_RGB(255, 182, 193) },
1138 { "lightsalmon", NSVG_RGB(255, 160, 122) },
1139 { "lightseagreen", NSVG_RGB( 32, 178, 170) },
1140 { "lightskyblue", NSVG_RGB(135, 206, 250) },
1141 { "lightslategray", NSVG_RGB(119, 136, 153) },
1142 { "lightslategrey", NSVG_RGB(119, 136, 153) },
1143 { "lightsteelblue", NSVG_RGB(176, 196, 222) },
1144 { "lightyellow", NSVG_RGB(255, 255, 224) },
1145 { "lime", NSVG_RGB( 0, 255, 0) },
1146 { "limegreen", NSVG_RGB( 50, 205, 50) },
1147 { "linen", NSVG_RGB(250, 240, 230) },
1148 { "maroon", NSVG_RGB(128, 0, 0) },
1149 { "mediumaquamarine", NSVG_RGB(102, 205, 170) },
1150 { "mediumblue", NSVG_RGB( 0, 0, 205) },
1151 { "mediumorchid", NSVG_RGB(186, 85, 211) },
1152 { "mediumpurple", NSVG_RGB(147, 112, 219) },
1153 { "mediumseagreen", NSVG_RGB( 60, 179, 113) },
1154 { "mediumslateblue", NSVG_RGB(123, 104, 238) },
1155 { "mediumspringgreen", NSVG_RGB( 0, 250, 154) },
1156 { "mediumturquoise", NSVG_RGB( 72, 209, 204) },
1157 { "mediumvioletred", NSVG_RGB(199, 21, 133) },
1158 { "midnightblue", NSVG_RGB( 25, 25, 112) },
1159 { "mintcream", NSVG_RGB(245, 255, 250) },
1160 { "mistyrose", NSVG_RGB(255, 228, 225) },
1161 { "moccasin", NSVG_RGB(255, 228, 181) },
1162 { "navajowhite", NSVG_RGB(255, 222, 173) },
1163 { "navy", NSVG_RGB( 0, 0, 128) },
1164 { "oldlace", NSVG_RGB(253, 245, 230) },
1165 { "olive", NSVG_RGB(128, 128, 0) },
1166 { "olivedrab", NSVG_RGB(107, 142, 35) },
1167 { "orange", NSVG_RGB(255, 165, 0) },
1168 { "orangered", NSVG_RGB(255, 69, 0) },
1169 { "orchid", NSVG_RGB(218, 112, 214) },
1170 { "palegoldenrod", NSVG_RGB(238, 232, 170) },
1171 { "palegreen", NSVG_RGB(152, 251, 152) },
1172 { "paleturquoise", NSVG_RGB(175, 238, 238) },
1173 { "palevioletred", NSVG_RGB(219, 112, 147) },
1174 { "papayawhip", NSVG_RGB(255, 239, 213) },
1175 { "peachpuff", NSVG_RGB(255, 218, 185) },
1176 { "peru", NSVG_RGB(205, 133, 63) },
1177 { "pink", NSVG_RGB(255, 192, 203) },
1178 { "plum", NSVG_RGB(221, 160, 221) },
1179 { "powderblue", NSVG_RGB(176, 224, 230) },
1180 { "purple", NSVG_RGB(128, 0, 128) },
1181 { "rosybrown", NSVG_RGB(188, 143, 143) },
1182 { "royalblue", NSVG_RGB( 65, 105, 225) },
1183 { "saddlebrown", NSVG_RGB(139, 69, 19) },
1184 { "salmon", NSVG_RGB(250, 128, 114) },
1185 { "sandybrown", NSVG_RGB(244, 164, 96) },
1186 { "seagreen", NSVG_RGB( 46, 139, 87) },
1187 { "seashell", NSVG_RGB(255, 245, 238) },
1188 { "sienna", NSVG_RGB(160, 82, 45) },
1189 { "silver", NSVG_RGB(192, 192, 192) },
1190 { "skyblue", NSVG_RGB(135, 206, 235) },
1191 { "slateblue", NSVG_RGB(106, 90, 205) },
1192 { "slategray", NSVG_RGB(112, 128, 144) },
1193 { "slategrey", NSVG_RGB(112, 128, 144) },
1194 { "snow", NSVG_RGB(255, 250, 250) },
1195 { "springgreen", NSVG_RGB( 0, 255, 127) },
1196 { "steelblue", NSVG_RGB( 70, 130, 180) },
1197 { "tan", NSVG_RGB(210, 180, 140) },
1198 { "teal", NSVG_RGB( 0, 128, 128) },
1199 { "thistle", NSVG_RGB(216, 191, 216) },
1200 { "tomato", NSVG_RGB(255, 99, 71) },
1201 { "turquoise", NSVG_RGB( 64, 224, 208) },
1202 { "violet", NSVG_RGB(238, 130, 238) },
1203 { "wheat", NSVG_RGB(245, 222, 179) },
1204 { "whitesmoke", NSVG_RGB(245, 245, 245) },
1205 { "yellowgreen", NSVG_RGB(154, 205, 50) },
1208 static unsigned int nsvg__parseColorName(const char* str)
1210 int i, ncolors = sizeof(nsvg__colors) / sizeof(NSVGNamedColor);
1212 for (i = 0; i < ncolors; i++) {
1213 if (strcmp(nsvg__colors[i].name, str) == 0) {
1214 return nsvg__colors[i].color;
1218 return NSVG_RGB(128, 128, 128);
1221 static unsigned int nsvg__parseColor(const char* str)
1224 while(*str == ' ') ++str;
1226 if (len >= 1 && *str == '#')
1227 return nsvg__parseColorHex(str);
1228 else if (len >= 4 && str[0] == 'r' && str[1] == 'g' && str[2] == 'b' && str[3] == '(')
1229 return nsvg__parseColorRGB(str);
1230 return nsvg__parseColorName(str);
1233 static float nsvg__parseOpacity(const char* str)
1236 sscanf(str, "%f", &val);
1237 if (val < 0.0f) val = 0.0f;
1238 if (val > 1.0f) val = 1.0f;
1242 static int nsvg__parseUnits(const char* units)
1244 if (units[0] == 'p' && units[1] == 'x')
1245 return NSVG_UNITS_PX;
1246 else if (units[0] == 'p' && units[1] == 't')
1247 return NSVG_UNITS_PT;
1248 else if (units[0] == 'p' && units[1] == 'c')
1249 return NSVG_UNITS_PC;
1250 else if (units[0] == 'm' && units[1] == 'm')
1251 return NSVG_UNITS_MM;
1252 else if (units[0] == 'c' && units[1] == 'm')
1253 return NSVG_UNITS_CM;
1254 else if (units[0] == 'i' && units[1] == 'n')
1255 return NSVG_UNITS_IN;
1256 else if (units[0] == '%')
1257 return NSVG_UNITS_PERCENT;
1258 else if (units[0] == 'e' && units[1] == 'm')
1259 return NSVG_UNITS_EM;
1260 else if (units[0] == 'e' && units[1] == 'x')
1261 return NSVG_UNITS_EX;
1262 return NSVG_UNITS_USER;
1265 static NSVGcoordinate nsvg__parseCoordinateRaw(const char* str)
1267 NSVGcoordinate coord = {0, NSVG_UNITS_USER};
1271 * In the original file, the formatted data reading did not specify the string with width limitation.
1272 * To prevent the possible overflow, we replace '%s' with '%32s' here.
1274 sscanf(str, "%f%32s", &coord.value, units);
1275 coord.units = nsvg__parseUnits(units);
1279 static NSVGcoordinate nsvg__coord(float v, int units)
1281 NSVGcoordinate coord = {v, units};
1285 static float nsvg__parseCoordinate(NSVGparser* p, const char* str, float orig, float length)
1287 NSVGcoordinate coord = nsvg__parseCoordinateRaw(str);
1288 return nsvg__convertToPixels(p, coord, orig, length);
1291 static int nsvg__parseTransformArgs(const char* str, float* args, int maxNa, int* na)
1299 while (*ptr && *ptr != '(') ++ptr;
1303 while (*end && *end != ')') ++end;
1308 if (*ptr == '-' || *ptr == '+' || *ptr == '.' || nsvg__isdigit(*ptr)) {
1309 if (*na >= maxNa) return 0;
1310 ptr = nsvg__parseNumber(ptr, it, 64);
1311 args[(*na)++] = (float)atof(it);
1316 return (int)(end - str);
1320 static int nsvg__parseMatrix(float* xform, const char* str)
1324 int len = nsvg__parseTransformArgs(str, t, 6, &na);
1325 if (na != 6) return len;
1326 memcpy(xform, t, sizeof(float)*6);
1330 static int nsvg__parseTranslate(float* xform, const char* str)
1335 int len = nsvg__parseTransformArgs(str, args, 2, &na);
1336 if (na == 1) args[1] = 0.0;
1338 nsvg__xformSetTranslation(t, args[0], args[1]);
1339 memcpy(xform, t, sizeof(float)*6);
1343 static int nsvg__parseScale(float* xform, const char* str)
1348 int len = nsvg__parseTransformArgs(str, args, 2, &na);
1349 if (na == 1) args[1] = args[0];
1350 nsvg__xformSetScale(t, args[0], args[1]);
1351 memcpy(xform, t, sizeof(float)*6);
1355 static int nsvg__parseSkewX(float* xform, const char* str)
1360 int len = nsvg__parseTransformArgs(str, args, 1, &na);
1361 nsvg__xformSetSkewX(t, args[0]/180.0f*NSVG_PI);
1362 memcpy(xform, t, sizeof(float)*6);
1366 static int nsvg__parseSkewY(float* xform, const char* str)
1371 int len = nsvg__parseTransformArgs(str, args, 1, &na);
1372 nsvg__xformSetSkewY(t, args[0]/180.0f*NSVG_PI);
1373 memcpy(xform, t, sizeof(float)*6);
1377 static int nsvg__parseRotate(float* xform, const char* str)
1383 int len = nsvg__parseTransformArgs(str, args, 3, &na);
1385 args[1] = args[2] = 0.0f;
1386 nsvg__xformIdentity(m);
1389 nsvg__xformSetTranslation(t, -args[1], -args[2]);
1390 nsvg__xformMultiply(m, t);
1393 nsvg__xformSetRotation(t, args[0]/180.0f*NSVG_PI);
1394 nsvg__xformMultiply(m, t);
1397 nsvg__xformSetTranslation(t, args[1], args[2]);
1398 nsvg__xformMultiply(m, t);
1401 memcpy(xform, m, sizeof(float)*6);
1406 static void nsvg__parseTransform(float* xform, const char* str)
1409 nsvg__xformIdentity(xform);
1412 if (strncmp(str, "matrix", 6) == 0)
1413 str += nsvg__parseMatrix(t, str);
1414 else if (strncmp(str, "translate", 9) == 0)
1415 str += nsvg__parseTranslate(t, str);
1416 else if (strncmp(str, "scale", 5) == 0)
1417 str += nsvg__parseScale(t, str);
1418 else if (strncmp(str, "rotate", 6) == 0)
1419 str += nsvg__parseRotate(t, str);
1420 else if (strncmp(str, "skewX", 5) == 0)
1421 str += nsvg__parseSkewX(t, str);
1422 else if (strncmp(str, "skewY", 5) == 0)
1423 str += nsvg__parseSkewY(t, str);
1429 nsvg__xformPremultiply(xform, t);
1433 static void nsvg__parseUrl(char* id, const char* str)
1436 str += 4; // "url(";
1439 while (i < 63 && *str != ')') {
1446 static char nsvg__parseLineCap(const char* str)
1448 if (strcmp(str, "butt") == 0)
1449 return NSVG_CAP_BUTT;
1450 else if (strcmp(str, "round") == 0)
1451 return NSVG_CAP_ROUND;
1452 else if (strcmp(str, "square") == 0)
1453 return NSVG_CAP_SQUARE;
1454 // TODO: handle inherit.
1455 return NSVG_CAP_BUTT;
1458 static char nsvg__parseLineJoin(const char* str)
1460 if (strcmp(str, "miter") == 0)
1461 return NSVG_JOIN_MITER;
1462 else if (strcmp(str, "round") == 0)
1463 return NSVG_JOIN_ROUND;
1464 else if (strcmp(str, "bevel") == 0)
1465 return NSVG_JOIN_BEVEL;
1466 // TODO: handle inherit.
1467 return NSVG_CAP_BUTT;
1470 static char nsvg__parseFillRule(const char* str)
1472 if (strcmp(str, "nonzero") == 0)
1473 return NSVG_FILLRULE_NONZERO;
1474 else if (strcmp(str, "evenodd") == 0)
1475 return NSVG_FILLRULE_EVENODD;
1476 // TODO: handle inherit.
1477 return NSVG_FILLRULE_NONZERO;
1480 static const char* nsvg__getNextDashItem(const char* s, char* it)
1484 // Skip white spaces and commas
1485 while (*s && (nsvg__isspace(*s) || *s == ',')) s++;
1486 // Advance until whitespace, comma or end.
1487 while (*s && (!nsvg__isspace(*s) && *s != ',')) {
1496 static int nsvg__parseStrokeDashArray(NSVGparser* p, const char* str, float* strokeDashArray)
1508 str = nsvg__getNextDashItem(str, item);
1510 if (count < NSVG_MAX_DASHES)
1511 strokeDashArray[count++] = fabsf(nsvg__parseCoordinate(p, item, 0.0f, nsvg__actualLength(p)));
1514 for (i = 0; i < count; i++)
1515 sum += strokeDashArray[i];
1522 static void nsvg__parseStyle(NSVGparser* p, const char* str);
1524 static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value)
1527 NSVGattrib* attr = nsvg__getAttr(p);
1528 if (!attr) return 0;
1530 if (strcmp(name, "style") == 0) {
1531 nsvg__parseStyle(p, value);
1532 } else if (strcmp(name, "display") == 0) {
1533 if (strcmp(value, "none") == 0)
1535 // Don't reset ->visible on display:inline, one display:none hides the whole subtree
1537 } else if (strcmp(name, "fill") == 0) {
1538 if (strcmp(value, "none") == 0) {
1540 } else if (strncmp(value, "url(", 4) == 0) {
1542 nsvg__parseUrl(attr->fillGradient, value);
1545 attr->fillColor = nsvg__parseColor(value);
1547 } else if (strcmp(name, "opacity") == 0) {
1548 attr->opacity = nsvg__parseOpacity(value);
1549 } else if (strcmp(name, "fill-opacity") == 0) {
1550 attr->fillOpacity = nsvg__parseOpacity(value);
1551 } else if (strcmp(name, "stroke") == 0) {
1552 if (strcmp(value, "none") == 0) {
1553 attr->hasStroke = 0;
1554 } else if (strncmp(value, "url(", 4) == 0) {
1555 attr->hasStroke = 2;
1556 nsvg__parseUrl(attr->strokeGradient, value);
1558 attr->hasStroke = 1;
1559 attr->strokeColor = nsvg__parseColor(value);
1561 } else if (strcmp(name, "stroke-width") == 0) {
1562 attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1563 } else if (strcmp(name, "stroke-dasharray") == 0) {
1564 attr->strokeDashCount = nsvg__parseStrokeDashArray(p, value, attr->strokeDashArray);
1565 } else if (strcmp(name, "stroke-dashoffset") == 0) {
1566 attr->strokeDashOffset = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1567 } else if (strcmp(name, "stroke-opacity") == 0) {
1568 attr->strokeOpacity = nsvg__parseOpacity(value);
1569 } else if (strcmp(name, "stroke-linecap") == 0) {
1570 attr->strokeLineCap = nsvg__parseLineCap(value);
1571 } else if (strcmp(name, "stroke-linejoin") == 0) {
1572 attr->strokeLineJoin = nsvg__parseLineJoin(value);
1573 } else if (strcmp(name, "fill-rule") == 0) {
1574 attr->fillRule = nsvg__parseFillRule(value);
1575 } else if (strcmp(name, "font-size") == 0) {
1576 attr->fontSize = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1577 } else if (strcmp(name, "transform") == 0) {
1578 nsvg__parseTransform(xform, value);
1579 nsvg__xformPremultiply(attr->xform, xform);
1580 } else if (strcmp(name, "stop-color") == 0) {
1581 attr->stopColor = nsvg__parseColor(value);
1582 } else if (strcmp(name, "stop-opacity") == 0) {
1583 attr->stopOpacity = nsvg__parseOpacity(value);
1584 } else if (strcmp(name, "offset") == 0) {
1585 attr->stopOffset = nsvg__parseCoordinate(p, value, 0.0f, 1.0f);
1586 } else if (strcmp(name, "id") == 0) {
1587 strncpy(attr->id, value, 63);
1588 attr->id[63] = '\0';
1595 static int nsvg__parseNameValue(NSVGparser* p, const char* start, const char* end)
1604 while (str < end && *str != ':') ++str;
1609 while (str > start && (*str == ':' || nsvg__isspace(*str))) --str;
1612 n = (int)(str - start);
1613 if (n > 511) n = 511;
1614 if (n) memcpy(name, start, n);
1617 while (val < end && (*val == ':' || nsvg__isspace(*val))) ++val;
1619 n = (int)(end - val);
1620 if (n > 511) n = 511;
1621 if (n) memcpy(value, val, n);
1624 return nsvg__parseAttr(p, name, value);
1627 static void nsvg__parseStyle(NSVGparser* p, const char* str)
1634 while(*str && nsvg__isspace(*str)) ++str;
1636 while(*str && *str != ';') ++str;
1640 while (end > start && (*end == ';' || nsvg__isspace(*end))) --end;
1643 nsvg__parseNameValue(p, start, end);
1648 static void nsvg__parseAttribs(NSVGparser* p, const char** attr)
1651 for (i = 0; attr[i]; i += 2)
1653 if (strcmp(attr[i], "style") == 0)
1654 nsvg__parseStyle(p, attr[i + 1]);
1656 nsvg__parseAttr(p, attr[i], attr[i + 1]);
1660 static int nsvg__getArgsPerElement(char cmd)
1690 static void nsvg__pathMoveTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
1699 nsvg__moveTo(p, *cpx, *cpy);
1702 static void nsvg__pathLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
1711 nsvg__lineTo(p, *cpx, *cpy);
1714 static void nsvg__pathHLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
1720 nsvg__lineTo(p, *cpx, *cpy);
1723 static void nsvg__pathVLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
1729 nsvg__lineTo(p, *cpx, *cpy);
1732 static void nsvg__pathCubicBezTo(NSVGparser* p, float* cpx, float* cpy,
1733 float* cpx2, float* cpy2, float* args, int rel)
1735 float x2, y2, cx1, cy1, cx2, cy2;
1738 cx1 = *cpx + args[0];
1739 cy1 = *cpy + args[1];
1740 cx2 = *cpx + args[2];
1741 cy2 = *cpy + args[3];
1742 x2 = *cpx + args[4];
1743 y2 = *cpy + args[5];
1753 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
1761 static void nsvg__pathCubicBezShortTo(NSVGparser* p, float* cpx, float* cpy,
1762 float* cpx2, float* cpy2, float* args, int rel)
1764 float x1, y1, x2, y2, cx1, cy1, cx2, cy2;
1769 cx2 = *cpx + args[0];
1770 cy2 = *cpy + args[1];
1771 x2 = *cpx + args[2];
1772 y2 = *cpy + args[3];
1783 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
1791 static void nsvg__pathQuadBezTo(NSVGparser* p, float* cpx, float* cpy,
1792 float* cpx2, float* cpy2, float* args, int rel)
1794 float x1, y1, x2, y2, cx, cy;
1795 float cx1, cy1, cx2, cy2;
1800 cx = *cpx + args[0];
1801 cy = *cpy + args[1];
1802 x2 = *cpx + args[2];
1803 y2 = *cpy + args[3];
1811 // Convert to cubic bezier
1812 cx1 = x1 + 2.0f/3.0f*(cx - x1);
1813 cy1 = y1 + 2.0f/3.0f*(cy - y1);
1814 cx2 = x2 + 2.0f/3.0f*(cx - x2);
1815 cy2 = y2 + 2.0f/3.0f*(cy - y2);
1817 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
1825 static void nsvg__pathQuadBezShortTo(NSVGparser* p, float* cpx, float* cpy,
1826 float* cpx2, float* cpy2, float* args, int rel)
1828 float x1, y1, x2, y2, cx, cy;
1829 float cx1, cy1, cx2, cy2;
1834 x2 = *cpx + args[0];
1835 y2 = *cpy + args[1];
1844 // Convert to cubix bezier
1845 cx1 = x1 + 2.0f/3.0f*(cx - x1);
1846 cy1 = y1 + 2.0f/3.0f*(cy - y1);
1847 cx2 = x2 + 2.0f/3.0f*(cx - x2);
1848 cy2 = y2 + 2.0f/3.0f*(cy - y2);
1850 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
1858 static float nsvg__sqr(float x) { return x*x; }
1859 static float nsvg__vmag(float x, float y) { return sqrtf(x*x + y*y); }
1861 static float nsvg__vecrat(float ux, float uy, float vx, float vy)
1863 return (ux*vx + uy*vy) / (nsvg__vmag(ux,uy) * nsvg__vmag(vx,vy));
1866 static float nsvg__vecang(float ux, float uy, float vx, float vy)
1868 float r = nsvg__vecrat(ux,uy, vx,vy);
1869 if (r < -1.0f) r = -1.0f;
1870 if (r > 1.0f) r = 1.0f;
1871 return ((ux*vy < uy*vx) ? -1.0f : 1.0f) * acosf(r);
1874 static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
1876 // Ported from canvg (https://code.google.com/p/canvg/)
1878 float x1, y1, x2, y2, cx, cy, dx, dy, d;
1879 float x1p, y1p, cxp, cyp, s, sa, sb;
1880 float ux, uy, vx, vy, a1, da;
1881 float x, y, tanx, tany, a, px = 0, py = 0, ptanx = 0, ptany = 0, t[6];
1887 rx = fabsf(args[0]); // y radius
1888 ry = fabsf(args[1]); // x radius
1889 rotx = args[2] / 180.0f * NSVG_PI; // x rotation engle
1890 fa = fabsf(args[3]) > 1e-6 ? 1 : 0; // Large arc
1891 fs = fabsf(args[4]) > 1e-6 ? 1 : 0; // Sweep direction
1892 x1 = *cpx; // start point
1894 if (rel) { // end point
1895 x2 = *cpx + args[5];
1896 y2 = *cpy + args[6];
1904 d = sqrtf(dx*dx + dy*dy);
1905 if (d < 1e-6f || rx < 1e-6f || ry < 1e-6f) {
1906 // The arc degenerates to a line
1907 nsvg__lineTo(p, x2, y2);
1916 // Convert to center point parameterization.
1917 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1918 // 1) Compute x1', y1'
1919 x1p = cosrx * dx / 2.0f + sinrx * dy / 2.0f;
1920 y1p = -sinrx * dx / 2.0f + cosrx * dy / 2.0f;
1921 d = nsvg__sqr(x1p)/nsvg__sqr(rx) + nsvg__sqr(y1p)/nsvg__sqr(ry);
1927 // 2) Compute cx', cy'
1929 sa = nsvg__sqr(rx)*nsvg__sqr(ry) - nsvg__sqr(rx)*nsvg__sqr(y1p) - nsvg__sqr(ry)*nsvg__sqr(x1p);
1930 sb = nsvg__sqr(rx)*nsvg__sqr(y1p) + nsvg__sqr(ry)*nsvg__sqr(x1p);
1931 if (sa < 0.0f) sa = 0.0f;
1936 cxp = s * rx * y1p / ry;
1937 cyp = s * -ry * x1p / rx;
1939 // 3) Compute cx,cy from cx',cy'
1940 cx = (x1 + x2)/2.0f + cosrx*cxp - sinrx*cyp;
1941 cy = (y1 + y2)/2.0f + sinrx*cxp + cosrx*cyp;
1943 // 4) Calculate theta1, and delta theta.
1944 ux = (x1p - cxp) / rx;
1945 uy = (y1p - cyp) / ry;
1946 vx = (-x1p - cxp) / rx;
1947 vy = (-y1p - cyp) / ry;
1948 a1 = nsvg__vecang(1.0f,0.0f, ux,uy); // Initial angle
1949 da = nsvg__vecang(ux,uy, vx,vy); // Delta angle
1951 // if (vecrat(ux,uy,vx,vy) <= -1.0f) da = NSVG_PI;
1952 // if (vecrat(ux,uy,vx,vy) >= 1.0f) da = 0;
1957 da = da - 2*NSVG_PI;
1959 da = 2*NSVG_PI + da;
1962 // Approximate the arc using cubic spline segments.
1963 t[0] = cosrx; t[1] = sinrx;
1964 t[2] = -sinrx; t[3] = cosrx;
1965 t[4] = cx; t[5] = cy;
1967 // Split arc into max 90 degree segments.
1968 // The loop assumes an iteration per end point (including start and end), this +1.
1969 ndivs = (int)(fabsf(da) / (NSVG_PI*0.5f) + 1.0f);
1970 hda = (da / (float)ndivs) / 2.0f;
1971 kappa = fabsf(4.0f / 3.0f * (1.0f - cosf(hda)) / sinf(hda));
1975 for (i = 0; i <= ndivs; i++) {
1976 a = a1 + da * (i/(float)ndivs);
1979 nsvg__xformPoint(&x, &y, dx*rx, dy*ry, t); // position
1980 nsvg__xformVec(&tanx, &tany, -dy*rx * kappa, dx*ry * kappa, t); // tangent
1982 nsvg__cubicBezTo(p, px+ptanx,py+ptany, x-tanx, y-tany, x, y);
1993 static void nsvg__parsePath(NSVGparser* p, const char** attr)
1995 const char* s = NULL;
2000 float cpx, cpy, cpx2, cpy2;
2006 for (i = 0; attr[i]; i += 2) {
2007 if (strcmp(attr[i], "d") == 0) {
2011 tmp[1] = attr[i + 1];
2014 nsvg__parseAttribs(p, tmp);
2026 s = nsvg__getNextPathItem(s, item);
2028 if (nsvg__isnum(item[0])) {
2030 args[nargs++] = (float)atof(item);
2031 if (nargs >= rargs) {
2035 nsvg__pathMoveTo(p, &cpx, &cpy, args, cmd == 'm' ? 1 : 0);
2036 // Moveto can be followed by multiple coordinate pairs,
2037 // which should be treated as linetos.
2038 cmd = (cmd == 'm') ? 'l' : 'L';
2039 rargs = nsvg__getArgsPerElement(cmd);
2040 cpx2 = cpx; cpy2 = cpy;
2044 nsvg__pathLineTo(p, &cpx, &cpy, args, cmd == 'l' ? 1 : 0);
2045 cpx2 = cpx; cpy2 = cpy;
2049 nsvg__pathHLineTo(p, &cpx, &cpy, args, cmd == 'h' ? 1 : 0);
2050 cpx2 = cpx; cpy2 = cpy;
2054 nsvg__pathVLineTo(p, &cpx, &cpy, args, cmd == 'v' ? 1 : 0);
2055 cpx2 = cpx; cpy2 = cpy;
2059 nsvg__pathCubicBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 'c' ? 1 : 0);
2063 nsvg__pathCubicBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 's' ? 1 : 0);
2067 nsvg__pathQuadBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 'q' ? 1 : 0);
2071 nsvg__pathQuadBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 't' ? 1 : 0);
2075 nsvg__pathArcTo(p, &cpx, &cpy, args, cmd == 'a' ? 1 : 0);
2076 cpx2 = cpx; cpy2 = cpy;
2080 cpx = args[nargs-2];
2081 cpy = args[nargs-1];
2082 cpx2 = cpx; cpy2 = cpy;
2090 rargs = nsvg__getArgsPerElement(cmd);
2091 if (cmd == 'M' || cmd == 'm') {
2094 nsvg__addPath(p, closedFlag);
2095 // Start new subpath.
2099 } else if (cmd == 'Z' || cmd == 'z') {
2103 // Move current point to first point
2106 cpx2 = cpx; cpy2 = cpy;
2107 nsvg__addPath(p, closedFlag);
2109 // Start new subpath.
2111 nsvg__moveTo(p, cpx, cpy);
2119 nsvg__addPath(p, closedFlag);
2125 static void nsvg__parseRect(NSVGparser* p, const char** attr)
2131 float rx = -1.0f; // marks not set
2135 for (i = 0; attr[i]; i += 2) {
2136 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2137 if (strcmp(attr[i], "x") == 0) x = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2138 if (strcmp(attr[i], "y") == 0) y = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2139 if (strcmp(attr[i], "width") == 0) w = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p));
2140 if (strcmp(attr[i], "height") == 0) h = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p));
2141 if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
2142 if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
2146 if (rx < 0.0f && ry > 0.0f) rx = ry;
2147 if (ry < 0.0f && rx > 0.0f) ry = rx;
2148 if (rx < 0.0f) rx = 0.0f;
2149 if (ry < 0.0f) ry = 0.0f;
2150 if (rx > w/2.0f) rx = w/2.0f;
2151 if (ry > h/2.0f) ry = h/2.0f;
2153 if (w != 0.0f && h != 0.0f) {
2156 if (rx < 0.00001f || ry < 0.0001f) {
2157 nsvg__moveTo(p, x, y);
2158 nsvg__lineTo(p, x+w, y);
2159 nsvg__lineTo(p, x+w, y+h);
2160 nsvg__lineTo(p, x, y+h);
2162 // Rounded rectangle
2163 nsvg__moveTo(p, x+rx, y);
2164 nsvg__lineTo(p, x+w-rx, y);
2165 nsvg__cubicBezTo(p, x+w-rx*(1-NSVG_KAPPA90), y, x+w, y+ry*(1-NSVG_KAPPA90), x+w, y+ry);
2166 nsvg__lineTo(p, x+w, y+h-ry);
2167 nsvg__cubicBezTo(p, x+w, y+h-ry*(1-NSVG_KAPPA90), x+w-rx*(1-NSVG_KAPPA90), y+h, x+w-rx, y+h);
2168 nsvg__lineTo(p, x+rx, y+h);
2169 nsvg__cubicBezTo(p, x+rx*(1-NSVG_KAPPA90), y+h, x, y+h-ry*(1-NSVG_KAPPA90), x, y+h-ry);
2170 nsvg__lineTo(p, x, y+ry);
2171 nsvg__cubicBezTo(p, x, y+ry*(1-NSVG_KAPPA90), x+rx*(1-NSVG_KAPPA90), y, x+rx, y);
2174 nsvg__addPath(p, 1);
2180 static void nsvg__parseCircle(NSVGparser* p, const char** attr)
2187 for (i = 0; attr[i]; i += 2) {
2188 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2189 if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2190 if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2191 if (strcmp(attr[i], "r") == 0) r = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualLength(p)));
2198 nsvg__moveTo(p, cx+r, cy);
2199 nsvg__cubicBezTo(p, cx+r, cy+r*NSVG_KAPPA90, cx+r*NSVG_KAPPA90, cy+r, cx, cy+r);
2200 nsvg__cubicBezTo(p, cx-r*NSVG_KAPPA90, cy+r, cx-r, cy+r*NSVG_KAPPA90, cx-r, cy);
2201 nsvg__cubicBezTo(p, cx-r, cy-r*NSVG_KAPPA90, cx-r*NSVG_KAPPA90, cy-r, cx, cy-r);
2202 nsvg__cubicBezTo(p, cx+r*NSVG_KAPPA90, cy-r, cx+r, cy-r*NSVG_KAPPA90, cx+r, cy);
2204 nsvg__addPath(p, 1);
2210 static void nsvg__parseEllipse(NSVGparser* p, const char** attr)
2218 for (i = 0; attr[i]; i += 2) {
2219 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2220 if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2221 if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2222 if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
2223 if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
2227 if (rx > 0.0f && ry > 0.0f) {
2231 nsvg__moveTo(p, cx+rx, cy);
2232 nsvg__cubicBezTo(p, cx+rx, cy+ry*NSVG_KAPPA90, cx+rx*NSVG_KAPPA90, cy+ry, cx, cy+ry);
2233 nsvg__cubicBezTo(p, cx-rx*NSVG_KAPPA90, cy+ry, cx-rx, cy+ry*NSVG_KAPPA90, cx-rx, cy);
2234 nsvg__cubicBezTo(p, cx-rx, cy-ry*NSVG_KAPPA90, cx-rx*NSVG_KAPPA90, cy-ry, cx, cy-ry);
2235 nsvg__cubicBezTo(p, cx+rx*NSVG_KAPPA90, cy-ry, cx+rx, cy-ry*NSVG_KAPPA90, cx+rx, cy);
2237 nsvg__addPath(p, 1);
2243 static void nsvg__parseLine(NSVGparser* p, const char** attr)
2251 for (i = 0; attr[i]; i += 2) {
2252 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2253 if (strcmp(attr[i], "x1") == 0) x1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2254 if (strcmp(attr[i], "y1") == 0) y1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2255 if (strcmp(attr[i], "x2") == 0) x2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2256 if (strcmp(attr[i], "y2") == 0) y2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2262 nsvg__moveTo(p, x1, y1);
2263 nsvg__lineTo(p, x2, y2);
2265 nsvg__addPath(p, 0);
2270 static void nsvg__parsePoly(NSVGparser* p, const char** attr, int closeFlag)
2275 int nargs, npts = 0;
2280 for (i = 0; attr[i]; i += 2) {
2281 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2282 if (strcmp(attr[i], "points") == 0) {
2286 s = nsvg__getNextPathItem(s, item);
2287 args[nargs++] = (float)atof(item);
2290 nsvg__moveTo(p, args[0], args[1]);
2292 nsvg__lineTo(p, args[0], args[1]);
2301 nsvg__addPath(p, (char)closeFlag);
2306 static void nsvg__parseSVG(NSVGparser* p, const char** attr)
2309 for (i = 0; attr[i]; i += 2) {
2310 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2311 if (strcmp(attr[i], "width") == 0) {
2312 p->image->width = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 1.0f);
2313 } else if (strcmp(attr[i], "height") == 0) {
2314 p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 1.0f);
2315 } else if (strcmp(attr[i], "viewBox") == 0) {
2316 sscanf(attr[i + 1], "%f%*[%%, \t]%f%*[%%, \t]%f%*[%%, \t]%f", &p->viewMinx, &p->viewMiny, &p->viewWidth, &p->viewHeight);
2317 } else if (strcmp(attr[i], "preserveAspectRatio") == 0) {
2318 if (strstr(attr[i + 1], "none") != 0) {
2319 // No uniform scaling
2320 p->alignType = NSVG_ALIGN_NONE;
2323 if (strstr(attr[i + 1], "xMin") != 0)
2324 p->alignX = NSVG_ALIGN_MIN;
2325 else if (strstr(attr[i + 1], "xMid") != 0)
2326 p->alignX = NSVG_ALIGN_MID;
2327 else if (strstr(attr[i + 1], "xMax") != 0)
2328 p->alignX = NSVG_ALIGN_MAX;
2330 if (strstr(attr[i + 1], "yMin") != 0)
2331 p->alignY = NSVG_ALIGN_MIN;
2332 else if (strstr(attr[i + 1], "yMid") != 0)
2333 p->alignY = NSVG_ALIGN_MID;
2334 else if (strstr(attr[i + 1], "yMax") != 0)
2335 p->alignY = NSVG_ALIGN_MAX;
2337 p->alignType = NSVG_ALIGN_MEET;
2338 if (strstr(attr[i + 1], "slice") != 0)
2339 p->alignType = NSVG_ALIGN_SLICE;
2346 static void nsvg__parseGradient(NSVGparser* p, const char** attr, char type)
2349 NSVGgradientData* grad = (NSVGgradientData*)malloc(sizeof(NSVGgradientData));
2350 if (grad == NULL) return;
2351 memset(grad, 0, sizeof(NSVGgradientData));
2352 grad->units = NSVG_OBJECT_SPACE;
2354 if (grad->type == NSVG_PAINT_LINEAR_GRADIENT) {
2355 grad->linear.x1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2356 grad->linear.y1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2357 grad->linear.x2 = nsvg__coord(100.0f, NSVG_UNITS_PERCENT);
2358 grad->linear.y2 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2359 } else if (grad->type == NSVG_PAINT_RADIAL_GRADIENT) {
2360 grad->radial.cx = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2361 grad->radial.cy = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2362 grad->radial.r = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2365 nsvg__xformIdentity(grad->xform);
2367 for (i = 0; attr[i]; i += 2) {
2368 if (strcmp(attr[i], "id") == 0) {
2369 strncpy(grad->id, attr[i+1], 63);
2370 grad->id[63] = '\0';
2371 } else if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2372 if (strcmp(attr[i], "gradientUnits") == 0) {
2373 if (strcmp(attr[i+1], "objectBoundingBox") == 0)
2374 grad->units = NSVG_OBJECT_SPACE;
2376 grad->units = NSVG_USER_SPACE;
2377 } else if (strcmp(attr[i], "gradientTransform") == 0) {
2378 nsvg__parseTransform(grad->xform, attr[i + 1]);
2379 } else if (strcmp(attr[i], "cx") == 0) {
2380 grad->radial.cx = nsvg__parseCoordinateRaw(attr[i + 1]);
2381 } else if (strcmp(attr[i], "cy") == 0) {
2382 grad->radial.cy = nsvg__parseCoordinateRaw(attr[i + 1]);
2383 } else if (strcmp(attr[i], "r") == 0) {
2384 grad->radial.r = nsvg__parseCoordinateRaw(attr[i + 1]);
2385 } else if (strcmp(attr[i], "fx") == 0) {
2386 grad->radial.fx = nsvg__parseCoordinateRaw(attr[i + 1]);
2387 } else if (strcmp(attr[i], "fy") == 0) {
2388 grad->radial.fy = nsvg__parseCoordinateRaw(attr[i + 1]);
2389 } else if (strcmp(attr[i], "x1") == 0) {
2390 grad->linear.x1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2391 } else if (strcmp(attr[i], "y1") == 0) {
2392 grad->linear.y1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2393 } else if (strcmp(attr[i], "x2") == 0) {
2394 grad->linear.x2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2395 } else if (strcmp(attr[i], "y2") == 0) {
2396 grad->linear.y2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2397 } else if (strcmp(attr[i], "spreadMethod") == 0) {
2398 if (strcmp(attr[i+1], "pad") == 0)
2399 grad->spread = NSVG_SPREAD_PAD;
2400 else if (strcmp(attr[i+1], "reflect") == 0)
2401 grad->spread = NSVG_SPREAD_REFLECT;
2402 else if (strcmp(attr[i+1], "repeat") == 0)
2403 grad->spread = NSVG_SPREAD_REPEAT;
2404 } else if (strcmp(attr[i], "xlink:href") == 0) {
2405 const char *href = attr[i+1];
2406 strncpy(grad->ref, href+1, 62);
2407 grad->ref[62] = '\0';
2412 grad->next = p->gradients;
2413 p->gradients = grad;
2416 static void nsvg__parseGradientStop(NSVGparser* p, const char** attr)
2418 NSVGattrib* curAttr = nsvg__getAttr(p);
2419 NSVGgradientData* grad;
2420 NSVGgradientStop* stop;
2423 curAttr->stopOffset = 0;
2424 curAttr->stopColor = 0;
2425 curAttr->stopOpacity = 1.0f;
2427 for (i = 0; attr[i]; i += 2) {
2428 nsvg__parseAttr(p, attr[i], attr[i + 1]);
2431 // Add stop to the last gradient.
2432 grad = p->gradients;
2433 if (grad == NULL) return;
2436 grad->stops = (NSVGgradientStop*)realloc(grad->stops, sizeof(NSVGgradientStop)*grad->nstops);
2437 if (grad->stops == NULL) return;
2440 idx = grad->nstops-1;
2441 for (i = 0; i < grad->nstops-1; i++) {
2442 if (curAttr->stopOffset < grad->stops[i].offset) {
2447 if (idx != grad->nstops-1) {
2448 for (i = grad->nstops-1; i > idx; i--)
2449 grad->stops[i] = grad->stops[i-1];
2452 stop = &grad->stops[idx];
2453 stop->color = curAttr->stopColor;
2454 stop->color |= (unsigned int)(curAttr->stopOpacity*255) << 24;
2455 stop->offset = curAttr->stopOffset;
2458 static void nsvg__startElement(void* ud, const char* el, const char** attr)
2460 NSVGparser* p = (NSVGparser*)ud;
2463 // Skip everything but gradients in defs
2464 if (strcmp(el, "linearGradient") == 0) {
2465 nsvg__parseGradient(p, attr, NSVG_PAINT_LINEAR_GRADIENT);
2466 } else if (strcmp(el, "radialGradient") == 0) {
2467 nsvg__parseGradient(p, attr, NSVG_PAINT_RADIAL_GRADIENT);
2468 } else if (strcmp(el, "stop") == 0) {
2469 nsvg__parseGradientStop(p, attr);
2474 if (strcmp(el, "g") == 0) {
2476 nsvg__parseAttribs(p, attr);
2477 } else if (strcmp(el, "path") == 0) {
2478 if (p->pathFlag) // Do not allow nested paths.
2481 nsvg__parsePath(p, attr);
2483 } else if (strcmp(el, "rect") == 0) {
2485 nsvg__parseRect(p, attr);
2487 } else if (strcmp(el, "circle") == 0) {
2489 nsvg__parseCircle(p, attr);
2491 } else if (strcmp(el, "ellipse") == 0) {
2493 nsvg__parseEllipse(p, attr);
2495 } else if (strcmp(el, "line") == 0) {
2497 nsvg__parseLine(p, attr);
2499 } else if (strcmp(el, "polyline") == 0) {
2501 nsvg__parsePoly(p, attr, 0);
2503 } else if (strcmp(el, "polygon") == 0) {
2505 nsvg__parsePoly(p, attr, 1);
2507 } else if (strcmp(el, "linearGradient") == 0) {
2508 nsvg__parseGradient(p, attr, NSVG_PAINT_LINEAR_GRADIENT);
2509 } else if (strcmp(el, "radialGradient") == 0) {
2510 nsvg__parseGradient(p, attr, NSVG_PAINT_RADIAL_GRADIENT);
2511 } else if (strcmp(el, "stop") == 0) {
2512 nsvg__parseGradientStop(p, attr);
2513 } else if (strcmp(el, "defs") == 0) {
2515 } else if (strcmp(el, "svg") == 0) {
2516 nsvg__parseSVG(p, attr);
2520 static void nsvg__endElement(void* ud, const char* el)
2522 NSVGparser* p = (NSVGparser*)ud;
2524 if (strcmp(el, "g") == 0) {
2526 } else if (strcmp(el, "path") == 0) {
2528 } else if (strcmp(el, "defs") == 0) {
2533 static void nsvg__content(void* ud, const char* s)
2540 static void nsvg__imageBounds(NSVGparser* p, float* bounds)
2543 shape = p->image->shapes;
2544 if (shape == NULL) {
2545 bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0;
2548 bounds[0] = shape->bounds[0];
2549 bounds[1] = shape->bounds[1];
2550 bounds[2] = shape->bounds[2];
2551 bounds[3] = shape->bounds[3];
2552 for (shape = shape->next; shape != NULL; shape = shape->next) {
2553 bounds[0] = nsvg__minf(bounds[0], shape->bounds[0]);
2554 bounds[1] = nsvg__minf(bounds[1], shape->bounds[1]);
2555 bounds[2] = nsvg__maxf(bounds[2], shape->bounds[2]);
2556 bounds[3] = nsvg__maxf(bounds[3], shape->bounds[3]);
2560 static float nsvg__viewAlign(float content, float container, int type)
2562 if (type == NSVG_ALIGN_MIN)
2564 else if (type == NSVG_ALIGN_MAX)
2565 return container - content;
2567 return (container - content) * 0.5f;
2570 static void nsvg__scaleGradient(NSVGgradient* grad, float tx, float ty, float sx, float sy)
2572 grad->xform[0] *= sx;
2573 grad->xform[1] *= sx;
2574 grad->xform[2] *= sy;
2575 grad->xform[3] *= sy;
2576 grad->xform[4] += tx*sx;
2577 grad->xform[5] += ty*sx;
2580 static void nsvg__scaleToViewbox(NSVGparser* p, const char* units)
2584 float tx, ty, sx, sy, us, bounds[4], t[6], avgs;
2588 // Guess image size if not set completely.
2589 nsvg__imageBounds(p, bounds);
2591 if (p->viewWidth == 0) {
2592 if (p->image->width > 0) {
2593 p->viewWidth = p->image->width;
2595 p->viewMinx = bounds[0];
2596 p->viewWidth = bounds[2] - bounds[0];
2599 if (p->viewHeight == 0) {
2600 if (p->image->height > 0) {
2601 p->viewHeight = p->image->height;
2603 p->viewMiny = bounds[1];
2604 p->viewHeight = bounds[3] - bounds[1];
2607 if (p->image->width <= 1)
2608 p->image->width = p->viewWidth;
2609 if (p->image->height <= 1)
2610 p->image->height = p->viewHeight;
2614 sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0;
2615 sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0;
2617 us = 1.0f / nsvg__convertToPixels(p, nsvg__coord(1.0f, nsvg__parseUnits(units)), 0.0f, 1.0f);
2620 if (p->alignType == NSVG_ALIGN_MEET) {
2621 // fit whole image into viewbox
2622 sx = sy = nsvg__minf(sx, sy);
2623 tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
2624 ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
2625 } else if (p->alignType == NSVG_ALIGN_SLICE) {
2626 // fill whole viewbox with image
2627 sx = sy = nsvg__maxf(sx, sy);
2628 tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
2629 ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
2635 avgs = (sx+sy) / 2.0f;
2636 for (shape = p->image->shapes; shape != NULL; shape = shape->next) {
2637 shape->bounds[0] = (shape->bounds[0] + tx) * sx;
2638 shape->bounds[1] = (shape->bounds[1] + ty) * sy;
2639 shape->bounds[2] = (shape->bounds[2] + tx) * sx;
2640 shape->bounds[3] = (shape->bounds[3] + ty) * sy;
2641 for (path = shape->paths; path != NULL; path = path->next) {
2642 path->bounds[0] = (path->bounds[0] + tx) * sx;
2643 path->bounds[1] = (path->bounds[1] + ty) * sy;
2644 path->bounds[2] = (path->bounds[2] + tx) * sx;
2645 path->bounds[3] = (path->bounds[3] + ty) * sy;
2646 for (i =0; i < path->npts; i++) {
2647 pt = &path->pts[i*2];
2648 pt[0] = (pt[0] + tx) * sx;
2649 pt[1] = (pt[1] + ty) * sy;
2653 if (shape->fill.type == NSVG_PAINT_LINEAR_GRADIENT || shape->fill.type == NSVG_PAINT_RADIAL_GRADIENT) {
2654 nsvg__scaleGradient(shape->fill.gradient, tx,ty, sx,sy);
2655 memcpy(t, shape->fill.gradient->xform, sizeof(float)*6);
2656 nsvg__xformInverse(shape->fill.gradient->xform, t);
2658 if (shape->stroke.type == NSVG_PAINT_LINEAR_GRADIENT || shape->stroke.type == NSVG_PAINT_RADIAL_GRADIENT) {
2659 nsvg__scaleGradient(shape->stroke.gradient, tx,ty, sx,sy);
2660 memcpy(t, shape->stroke.gradient->xform, sizeof(float)*6);
2661 nsvg__xformInverse(shape->stroke.gradient->xform, t);
2664 shape->strokeWidth *= avgs;
2665 shape->strokeDashOffset *= avgs;
2666 for (i = 0; i < shape->strokeDashCount; i++)
2667 shape->strokeDashArray[i] *= avgs;
2671 NSVGimage* nsvgParse(char* input, const char* units, float dpi)
2676 p = nsvg__createParser();
2682 nsvg__parseXML(input, nsvg__startElement, nsvg__endElement, nsvg__content, p);
2685 nsvg__scaleToViewbox(p, units);
2690 nsvg__deleteParser(p);
2695 NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi)
2700 NSVGimage* image = NULL;
2702 fp = fopen(filename, "rb");
2703 if (!fp) goto error;
2704 fseek(fp, 0, SEEK_END);
2706 fseek(fp, 0, SEEK_SET);
2707 data = (char*)malloc(size+1);
2708 if (data == NULL) goto error;
2709 if (fread(data, 1, size, fp) != size) goto error;
2710 data[size] = '\0'; // Must be null terminated.
2712 image = nsvgParse(data, units, dpi);
2719 if (data) free(data);
2720 if (image) nsvgDelete(image);
2724 void nsvgDelete(NSVGimage* image)
2726 if (image == NULL) return;
2728 NSVGshape *snext, *shape;
2729 shape = image->shapes;
2730 while (shape != NULL) {
2731 snext = shape->next;
2732 nsvg__deletePaths(shape->paths);
2733 nsvg__deletePaint(&shape->fill);
2734 nsvg__deletePaint(&shape->stroke);