33#ifndef NANOSVG_CPLUSPLUS
185#ifndef NANOSVG_CPLUSPLUS
191#ifdef NANOSVG_IMPLEMENTATION
198#define NSVG_PI (3.14159265358979323846264338327f)
199#define NSVG_KAPPA90 (0.5522847493f)
201#define NSVG_ALIGN_MIN 0
202#define NSVG_ALIGN_MID 1
203#define NSVG_ALIGN_MAX 2
204#define NSVG_ALIGN_NONE 0
205#define NSVG_ALIGN_MEET 1
206#define NSVG_ALIGN_SLICE 2
208#define NSVG_NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0)
209#define NSVG_RGB(r, g, b) (((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16))
212 #pragma warning (disable: 4996)
213 #pragma warning (disable: 4100)
215 #define NSVG_INLINE inline
220 #define NSVG_INLINE inline
224static int nsvg__isspace(
char c)
226 return strchr(
" \t\n\v\f\r", c) != 0;
229static int nsvg__isdigit(
char c)
231 return c >=
'0' && c <=
'9';
234static NSVG_INLINE
float nsvg__minf(
float a,
float b) {
return a < b ? a : b; }
235static NSVG_INLINE
float nsvg__maxf(
float a,
float b) {
return a > b ? a : b; }
240#define NSVG_XML_TAG 1
241#define NSVG_XML_CONTENT 2
242#define NSVG_XML_MAX_ATTRIBS 256
244static void nsvg__parseContent(
char* s,
245 void (*contentCb)(
void* ud,
const char* s),
249 while (*s && nsvg__isspace(*s)) s++;
256static void nsvg__parseElement(
char* s,
257 void (*startelCb)(
void* ud,
const char* el,
const char** attr),
258 void (*endelCb)(
void* ud,
const char* el),
261 const char* attr[NSVG_XML_MAX_ATTRIBS];
269 while (*s && nsvg__isspace(*s)) s++;
280 if (!*s || *s ==
'?' || *s ==
'!')
285 while (*s && !nsvg__isspace(*s)) s++;
286 if (*s) { *s++ =
'\0'; }
289 while (!end && *s && nattr < NSVG_XML_MAX_ATTRIBS-3) {
294 while (*s && nsvg__isspace(*s)) s++;
302 while (*s && !nsvg__isspace(*s) && *s !=
'=') s++;
303 if (*s) { *s++ =
'\0'; }
305 while (*s && *s !=
'\"' && *s !=
'\'') s++;
311 while (*s && *s != quote) s++;
312 if (*s) { *s++ =
'\0'; }
316 attr[nattr++] = name;
317 attr[nattr++] = value;
326 if (start && startelCb)
327 (*startelCb)(ud, name, attr);
329 (*endelCb)(ud, name);
332int nsvg__parseXML(
char* input,
333 void (*startelCb)(
void* ud,
const char* el,
const char** attr),
334 void (*endelCb)(
void* ud,
const char* el),
335 void (*contentCb)(
void* ud,
const char* s),
340 int state = NSVG_XML_CONTENT;
342 if (*s ==
'<' && state == NSVG_XML_CONTENT) {
345 nsvg__parseContent(mark, contentCb, ud);
347 state = NSVG_XML_TAG;
348 }
else if (*s ==
'>' && state == NSVG_XML_TAG) {
351 nsvg__parseElement(mark, startelCb, endelCb, ud);
353 state = NSVG_XML_CONTENT;
365#define NSVG_MAX_ATTR 128
367enum NSVGgradientUnits {
369 NSVG_OBJECT_SPACE = 1
372#define NSVG_MAX_DASHES 8
387typedef struct NSVGcoordinate {
392typedef struct NSVGlinearData {
393 NSVGcoordinate x1, y1, x2, y2;
396typedef struct NSVGradialData {
397 NSVGcoordinate cx, cy, r, fx, fy;
400typedef struct NSVGgradientData
406 NSVGlinearData linear;
407 NSVGradialData radial;
414 struct NSVGgradientData* next;
417typedef struct NSVGattrib
421 unsigned int fillColor;
422 unsigned int strokeColor;
426 char fillGradient[64];
427 char strokeGradient[64];
429 float strokeDashOffset;
430 float strokeDashArray[NSVG_MAX_DASHES];
437 unsigned int stopColor;
445typedef struct NSVGparser
447 NSVGattrib attr[NSVG_MAX_ATTR];
454 NSVGgradientData* gradients;
456 float viewMinx, viewMiny, viewWidth, viewHeight;
457 int alignX, alignY, alignType;
463static void nsvg__xformIdentity(
float* t)
465 t[0] = 1.0f; t[1] = 0.0f;
466 t[2] = 0.0f; t[3] = 1.0f;
467 t[4] = 0.0f; t[5] = 0.0f;
470static void nsvg__xformSetTranslation(
float* t,
float tx,
float ty)
472 t[0] = 1.0f; t[1] = 0.0f;
473 t[2] = 0.0f; t[3] = 1.0f;
474 t[4] = tx; t[5] = ty;
477static void nsvg__xformSetScale(
float* t,
float sx,
float sy)
479 t[0] = sx; t[1] = 0.0f;
480 t[2] = 0.0f; t[3] = sy;
481 t[4] = 0.0f; t[5] = 0.0f;
484static void nsvg__xformSetSkewX(
float* t,
float a)
486 t[0] = 1.0f; t[1] = 0.0f;
487 t[2] = tanf(a); t[3] = 1.0f;
488 t[4] = 0.0f; t[5] = 0.0f;
491static void nsvg__xformSetSkewY(
float* t,
float a)
493 t[0] = 1.0f; t[1] = tanf(a);
494 t[2] = 0.0f; t[3] = 1.0f;
495 t[4] = 0.0f; t[5] = 0.0f;
498static void nsvg__xformSetRotation(
float* t,
float a)
500 float cs = cosf(a), sn = sinf(a);
501 t[0] = cs; t[1] = sn;
502 t[2] = -sn; t[3] = cs;
503 t[4] = 0.0f; t[5] = 0.0f;
506static void nsvg__xformMultiply(
float* t,
float* s)
508 float t0 = t[0] * s[0] + t[1] * s[2];
509 float t2 = t[2] * s[0] + t[3] * s[2];
510 float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
511 t[1] = t[0] * s[1] + t[1] * s[3];
512 t[3] = t[2] * s[1] + t[3] * s[3];
513 t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
519static void nsvg__xformInverse(
float* inv,
float* t)
521 double invdet, det = (double)t[0] * t[3] - (
double)t[2] * t[1];
522 if (det > -1e-6 && det < 1e-6) {
523 nsvg__xformIdentity(t);
527 inv[0] = (float)(t[3] * invdet);
528 inv[2] = (float)(-t[2] * invdet);
529 inv[4] = (float)(((
double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
530 inv[1] = (float)(-t[1] * invdet);
531 inv[3] = (float)(t[0] * invdet);
532 inv[5] = (float)(((
double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
535static void nsvg__xformPremultiply(
float* t,
float* s)
538 memcpy(s2, s,
sizeof(
float)*6);
539 nsvg__xformMultiply(s2, t);
540 memcpy(t, s2,
sizeof(
float)*6);
543static void nsvg__xformPoint(
float* dx,
float* dy,
float x,
float y,
float* t)
545 *dx = x*t[0] + y*t[2] + t[4];
546 *dy = x*t[1] + y*t[3] + t[5];
549static void nsvg__xformVec(
float* dx,
float* dy,
float x,
float y,
float* t)
551 *dx = x*t[0] + y*t[2];
552 *dy = x*t[1] + y*t[3];
555#define NSVG_EPSILON (1e-12)
557static int nsvg__ptInBounds(
float* pt,
float* bounds)
559 return pt[0] >= bounds[0] && pt[0] <= bounds[2] && pt[1] >= bounds[1] && pt[1] <= bounds[3];
563static double nsvg__evalBezier(
double t,
double p0,
double p1,
double p2,
double p3)
566 return it*it*it*p0 + 3.0*it*it*t*p1 + 3.0*it*t*t*p2 + t*t*t*p3;
569static void nsvg__curveBounds(
float* bounds,
float* curve)
572 double roots[2], a, b, c, b2ac, t, v;
573 float* v0 = &curve[0];
574 float* v1 = &curve[2];
575 float* v2 = &curve[4];
576 float* v3 = &curve[6];
579 bounds[0] = nsvg__minf(v0[0], v3[0]);
580 bounds[1] = nsvg__minf(v0[1], v3[1]);
581 bounds[2] = nsvg__maxf(v0[0], v3[0]);
582 bounds[3] = nsvg__maxf(v0[1], v3[1]);
586 if (nsvg__ptInBounds(v1, bounds) && nsvg__ptInBounds(v2, bounds))
590 for (i = 0; i < 2; i++) {
591 a = -3.0 * v0[i] + 9.0 * v1[i] - 9.0 * v2[i] + 3.0 * v3[i];
592 b = 6.0 * v0[i] - 12.0 * v1[i] + 6.0 * v2[i];
593 c = 3.0 * v1[i] - 3.0 * v0[i];
595 if (fabs(a) < NSVG_EPSILON) {
596 if (fabs(b) > NSVG_EPSILON) {
598 if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
602 b2ac = b*b - 4.0*c*a;
603 if (b2ac > NSVG_EPSILON) {
604 t = (-b + sqrt(b2ac)) / (2.0 * a);
605 if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
607 t = (-b - sqrt(b2ac)) / (2.0 * a);
608 if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
612 for (j = 0; j < count; j++) {
613 v = nsvg__evalBezier(roots[j], v0[i], v1[i], v2[i], v3[i]);
614 bounds[0+i] = nsvg__minf(bounds[0+i], (
float)v);
615 bounds[2+i] = nsvg__maxf(bounds[2+i], (
float)v);
620static NSVGparser* nsvg__createParser(
unsigned current_color)
623 p = (NSVGparser*)malloc(
sizeof(NSVGparser));
624 if (p == NULL)
goto error;
625 memset(p, 0,
sizeof(NSVGparser));
628 if (p->image == NULL)
goto error;
632 nsvg__xformIdentity(p->attr[0].xform);
633 memset(p->attr[0].id, 0,
sizeof p->attr[0].id);
634 p->attr[0].fillColor = NSVG_RGB(0,0,0);
635 p->attr[0].strokeColor = NSVG_RGB(0,0,0);
636 p->attr[0].opacity = 1;
637 p->attr[0].fillOpacity = 1;
638 p->attr[0].strokeOpacity = 1;
639 p->attr[0].stopOpacity = 1;
640 p->attr[0].strokeWidth = 1;
643 p->attr[0].miterLimit = 4;
645 p->attr[0].hasFill = 1;
646 p->attr[0].visible = 1;
648 p->attr[0].fontSize = 40.0f;
650 p->image->current_color = current_color;
656 if (p->image) free(p->image);
666 if (
path->pts != NULL)
673static void nsvg__deletePaint(
NSVGpaint* paint)
679static void nsvg__deleteGradientData(NSVGgradientData* grad)
681 NSVGgradientData* next;
682 while (grad != NULL) {
690static void nsvg__deleteParser(NSVGparser* p)
693 nsvg__deletePaths(p->plist);
694 nsvg__deleteGradientData(p->gradients);
701static void nsvg__resetPath(NSVGparser* p)
706static void nsvg__addPoint(NSVGparser* p,
float x,
float y)
708 if (p->npts+1 > p->cpts) {
709 p->cpts = p->cpts ? p->cpts*2 : 8;
710 p->pts = (
float*)realloc(p->pts, p->cpts*2*
sizeof(
float));
713 p->pts[p->npts*2+0] = x;
714 p->pts[p->npts*2+1] = y;
718static void nsvg__moveTo(NSVGparser* p,
float x,
float y)
721 p->pts[(p->npts-1)*2+0] = x;
722 p->pts[(p->npts-1)*2+1] = y;
724 nsvg__addPoint(p, x, y);
728static void nsvg__lineTo(NSVGparser* p,
float x,
float y)
732 px = p->pts[(p->npts-1)*2+0];
733 py = p->pts[(p->npts-1)*2+1];
736 nsvg__addPoint(p, px + dx/3.0f, py + dy/3.0f);
737 nsvg__addPoint(p, x - dx/3.0f, y - dy/3.0f);
738 nsvg__addPoint(p, x, y);
742static void nsvg__cubicBezTo(NSVGparser* p,
float cpx1,
float cpy1,
float cpx2,
float cpy2,
float x,
float y)
745 nsvg__addPoint(p, cpx1, cpy1);
746 nsvg__addPoint(p, cpx2, cpy2);
747 nsvg__addPoint(p, x, y);
751static NSVGattrib* nsvg__getAttr(NSVGparser* p)
753 return &p->attr[p->attrHead];
756static void nsvg__pushAttr(NSVGparser* p)
758 if (p->attrHead < NSVG_MAX_ATTR-1) {
760 memcpy(&p->attr[p->attrHead], &p->attr[p->attrHead-1],
sizeof(NSVGattrib));
764static void nsvg__popAttr(NSVGparser* p)
770static float nsvg__actualOrigX(NSVGparser* p)
775static float nsvg__actualOrigY(NSVGparser* p)
780static float nsvg__actualWidth(NSVGparser* p)
785static float nsvg__actualHeight(NSVGparser* p)
787 return p->viewHeight;
790static float nsvg__actualLength(NSVGparser* p)
792 float w = nsvg__actualWidth(p), h = nsvg__actualHeight(p);
793 return sqrtf(w*w + h*h) / sqrtf(2.0f);
796static float nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c,
float orig,
float length)
798 NSVGattrib* attr = nsvg__getAttr(p);
800 case NSVG_UNITS_USER:
return c.value;
801 case NSVG_UNITS_PX:
return c.value;
802 case NSVG_UNITS_PT:
return c.value / 72.0f * p->dpi;
803 case NSVG_UNITS_PC:
return c.value / 6.0f * p->dpi;
804 case NSVG_UNITS_MM:
return c.value / 25.4f * p->dpi;
805 case NSVG_UNITS_CM:
return c.value / 2.54f * p->dpi;
806 case NSVG_UNITS_IN:
return c.value * p->dpi;
807 case NSVG_UNITS_EM:
return c.value * attr->fontSize;
808 case NSVG_UNITS_EX:
return c.value * attr->fontSize * 0.52f;
809 case NSVG_UNITS_PERCENT:
return orig + c.value / 100.0f * length;
810 default:
return c.value;
815static NSVGgradientData* nsvg__findGradientData(NSVGparser* p,
const char*
id)
817 NSVGgradientData* grad = p->gradients;
818 if (
id == NULL || *
id ==
'\0')
820 while (grad != NULL) {
821 if (strcmp(grad->id,
id) == 0)
828static NSVGgradient* nsvg__createGradient(NSVGparser* p,
const char*
id,
const float* localBounds,
float *xform,
signed char* paintType)
830 NSVGgradientData* data = NULL;
831 NSVGgradientData* ref = NULL;
834 float ox, oy, sw,
sh, sl;
838 data = nsvg__findGradientData(p,
id);
839 if (data == NULL)
return NULL;
844 while (ref != NULL) {
845 NSVGgradientData* nextRef = NULL;
846 if (stops == NULL && ref->stops != NULL) {
848 nstops = ref->nstops;
851 nextRef = nsvg__findGradientData(p, ref->ref);
852 if (nextRef == ref)
break;
855 if (refIter > 32)
break;
857 if (stops == NULL)
return NULL;
860 if (grad == NULL)
return NULL;
863 if (data->units == NSVG_OBJECT_SPACE) {
866 sw = localBounds[2] - localBounds[0];
867 sh = localBounds[3] - localBounds[1];
869 ox = nsvg__actualOrigX(p);
870 oy = nsvg__actualOrigY(p);
871 sw = nsvg__actualWidth(p);
872 sh = nsvg__actualHeight(p);
874 sl = sqrtf(sw*sw +
sh*
sh) / sqrtf(2.0f);
877 float x1, y1, x2, y2, dx, dy;
878 x1 = nsvg__convertToPixels(p, data->grad.linear.x1, ox, sw);
879 y1 = nsvg__convertToPixels(p, data->grad.linear.y1, oy,
sh);
880 x2 = nsvg__convertToPixels(p, data->grad.linear.x2, ox, sw);
881 y2 = nsvg__convertToPixels(p, data->grad.linear.y2, oy,
sh);
889 float cx, cy, fx, fy, r;
890 cx = nsvg__convertToPixels(p, data->grad.radial.cx, ox, sw);
891 cy = nsvg__convertToPixels(p, data->grad.radial.cy, oy,
sh);
892 fx = nsvg__convertToPixels(p, data->grad.radial.fx, ox, sw);
893 fy = nsvg__convertToPixels(p, data->grad.radial.fy, oy,
sh);
894 r = nsvg__convertToPixels(p, data->grad.radial.r, 0, sl);
903 nsvg__xformMultiply(grad->
xform, data->xform);
904 nsvg__xformMultiply(grad->
xform, xform);
906 grad->
spread = data->spread;
910 *paintType = data->type;
915static float nsvg__getAverageScale(
float* t)
917 float sx = sqrtf(t[0]*t[0] + t[2]*t[2]);
918 float sy = sqrtf(t[1]*t[1] + t[3]*t[3]);
919 return (sx + sy) * 0.5f;
922static void nsvg__getLocalBounds(
float* bounds,
NSVGshape *shape,
float* xform)
925 float curve[4*2], curveBounds[4];
928 nsvg__xformPoint(&curve[0], &curve[1],
path->pts[0],
path->pts[1], xform);
929 for (i = 0; i <
path->npts-1; i += 3) {
930 nsvg__xformPoint(&curve[2], &curve[3],
path->pts[(i+1)*2],
path->pts[(i+1)*2+1], xform);
931 nsvg__xformPoint(&curve[4], &curve[5],
path->pts[(i+2)*2],
path->pts[(i+2)*2+1], xform);
932 nsvg__xformPoint(&curve[6], &curve[7],
path->pts[(i+3)*2],
path->pts[(i+3)*2+1], xform);
933 nsvg__curveBounds(curveBounds, curve);
935 bounds[0] = curveBounds[0];
936 bounds[1] = curveBounds[1];
937 bounds[2] = curveBounds[2];
938 bounds[3] = curveBounds[3];
941 bounds[0] = nsvg__minf(bounds[0], curveBounds[0]);
942 bounds[1] = nsvg__minf(bounds[1], curveBounds[1]);
943 bounds[2] = nsvg__maxf(bounds[2], curveBounds[2]);
944 bounds[3] = nsvg__maxf(bounds[3], curveBounds[3]);
952static void nsvg__addShape(NSVGparser* p)
954 NSVGattrib* attr = nsvg__getAttr(p);
960 if (p->plist == NULL)
964 if (shape == NULL)
goto error;
967 memcpy(shape->
id, attr->id,
sizeof shape->
id);
970 memcpy(shape->
xform, attr->xform,
sizeof shape->
xform);
971 scale = nsvg__getAverageScale(attr->xform);
975 for (i = 0; i < attr->strokeDashCount; i++)
981 shape->
opacity = attr->opacity;
984 shape->
paths = p->plist;
1000 if (attr->hasFill == 0) {
1002 }
else if (attr->hasFill == 1) {
1005 shape->
fill.
v.
color |= (
unsigned int)(attr->fillOpacity*255) << 24;
1006 }
else if (attr->hasFill == 2) {
1011 if (attr->hasStroke == 0) {
1013 }
else if (attr->hasStroke == 1) {
1016 shape->
stroke.
v.
color |= (
unsigned int)(attr->strokeOpacity*255) << 24;
1017 }
else if (attr->hasStroke == 2) {
1025 if (p->image->shapes == NULL)
1026 p->image->shapes = shape;
1028 p->shapesTail->next = shape;
1029 p->shapesTail = shape;
1034 if (shape) free(shape);
1037static void nsvg__addPath(NSVGparser* p,
char closed)
1039 NSVGattrib* attr = nsvg__getAttr(p);
1049 nsvg__lineTo(p, p->pts[0], p->pts[1]);
1052 if ((p->npts % 3) != 1)
1059 path->pts = (
float*)malloc(p->npts*2*
sizeof(
float));
1061 path->closed = closed;
1062 path->npts = p->npts;
1065 for (i = 0; i < p->npts; ++i)
1066 nsvg__xformPoint(&
path->pts[i*2], &
path->pts[i*2+1], p->pts[i*2], p->pts[i*2+1], attr->xform);
1069 for (i = 0; i <
path->npts-1; i += 3) {
1070 curve = &
path->pts[i*2];
1071 nsvg__curveBounds(bounds, curve);
1073 path->bounds[0] = bounds[0];
1074 path->bounds[1] = bounds[1];
1075 path->bounds[2] = bounds[2];
1076 path->bounds[3] = bounds[3];
1078 path->bounds[0] = nsvg__minf(
path->bounds[0], bounds[0]);
1079 path->bounds[1] = nsvg__minf(
path->bounds[1], bounds[1]);
1080 path->bounds[2] = nsvg__maxf(
path->bounds[2], bounds[2]);
1081 path->bounds[3] = nsvg__maxf(
path->bounds[3], bounds[3]);
1085 path->next = p->plist;
1092 if (
path->pts != NULL) free(
path->pts);
1098static double nsvg__atof(
const char* s)
1100 char* cur = (
char*)s;
1102 double res = 0.0, sign = 1.0;
1103 double intPart = 0.0, fracPart = 0.0;
1104 char hasIntPart = 0, hasFracPart = 0;
1109 }
else if (*cur ==
'-') {
1115 if (nsvg__isdigit(*cur)) {
1118 intPart = (double)_strtoi64(cur, &end, 10);
1120 intPart = (double)strtoll(cur, &end, 10);
1132 if (nsvg__isdigit(*cur)) {
1135 fracPart = (double)_strtoi64(cur, &end, 10);
1137 fracPart = (double)strtoll(cur, &end, 10);
1140 res += fracPart / pow(10.0, (
double)(end - cur));
1148 if (!hasIntPart && !hasFracPart)
1152 if (*cur ==
'e' || *cur ==
'E') {
1153 double expPart = 0.0;
1155 expPart = (double)strtol(cur, &end, 10);
1157 res *= pow(10.0, expPart);
1165static const char* nsvg__parseNumber(
const char* s,
char* it,
const int size)
1167 const int last = size-1;
1171 if (*s ==
'-' || *s ==
'+') {
1172 if (i < last) it[i++] = *s;
1176 while (*s && nsvg__isdigit(*s)) {
1177 if (i < last) it[i++] = *s;
1182 if (i < last) it[i++] = *s;
1185 while (*s && nsvg__isdigit(*s)) {
1186 if (i < last) it[i++] = *s;
1191 if ((*s ==
'e' || *s ==
'E') && (s[1] !=
'm' && s[1] !=
'x')) {
1192 if (i < last) it[i++] = *s;
1194 if (*s ==
'-' || *s ==
'+') {
1195 if (i < last) it[i++] = *s;
1198 while (*s && nsvg__isdigit(*s)) {
1199 if (i < last) it[i++] = *s;
1208static const char* nsvg__getNextPathItemWhenArcFlag(
const char* s,
char* it)
1211 while (*s && (nsvg__isspace(*s) || *s ==
',')) s++;
1213 if (*s ==
'0' || *s ==
'1') {
1221static const char* nsvg__getNextPathItem(
const char* s,
char* it)
1225 while (*s && (nsvg__isspace(*s) || *s ==
',')) s++;
1227 if (*s ==
'-' || *s ==
'+' || *s ==
'.' || nsvg__isdigit(*s)) {
1228 s = nsvg__parseNumber(s, it, 64);
1239static unsigned int nsvg__parseColorHex(
const char* str)
1241 unsigned int r=0, g=0, b=0;
1242 if (sscanf(str,
"#%2x%2x%2x", &r, &g, &b) == 3 )
1243 return NSVG_RGB(r, g, b);
1244 if (sscanf(str,
"#%1x%1x%1x", &r, &g, &b) == 3 )
1245 return NSVG_RGB(r*17, g*17, b*17);
1246 return NSVG_RGB(128, 128, 128);
1253static unsigned int nsvg__parseColorRGB(
const char* str)
1256 unsigned int rgbi[3];
1259 if (sscanf(str,
"rgb(%u, %u, %u)", &rgbi[0], &rgbi[1], &rgbi[2]) != 3) {
1261 const char delimiter[3] = {
',',
',',
')'};
1263 for (i = 0; i < 3; i++) {
1264 while (*str && (nsvg__isspace(*str))) str++;
1265 if (*str ==
'+') str++;
1267 rgbf[i] = nsvg__atof(str);
1277 while (*str && nsvg__isdigit(*str)) str++;
1280 if (!nsvg__isdigit(*str))
break;
1281 while (*str && nsvg__isdigit(*str)) str++;
1283 if (*str ==
'%') str++;
else break;
1284 while (nsvg__isspace(*str)) str++;
1285 if (*str == delimiter[i]) str++;
1289 rgbi[0] = roundf(rgbf[0] * 2.55f);
1290 rgbi[1] = roundf(rgbf[1] * 2.55f);
1291 rgbi[2] = roundf(rgbf[2] * 2.55f);
1293 rgbi[0] = rgbi[1] = rgbi[2] = 128;
1297 for (i = 0; i < 3; i++) {
1298 if (rgbi[i] > 255) rgbi[i] = 255;
1300 return NSVG_RGB(rgbi[0], rgbi[1], rgbi[2]);
1303typedef struct NSVGNamedColor {
1308NSVGNamedColor nsvg__colors[] = {
1310 {
"red", NSVG_RGB(255, 0, 0) },
1311 {
"green", NSVG_RGB( 0, 128, 0) },
1312 {
"blue", NSVG_RGB( 0, 0, 255) },
1313 {
"yellow", NSVG_RGB(255, 255, 0) },
1314 {
"cyan", NSVG_RGB( 0, 255, 255) },
1315 {
"magenta", NSVG_RGB(255, 0, 255) },
1316 {
"black", NSVG_RGB( 0, 0, 0) },
1317 {
"grey", NSVG_RGB(128, 128, 128) },
1318 {
"gray", NSVG_RGB(128, 128, 128) },
1319 {
"white", NSVG_RGB(255, 255, 255) },
1321#ifdef NANOSVG_ALL_COLOR_KEYWORDS
1322 {
"aliceblue", NSVG_RGB(240, 248, 255) },
1323 {
"antiquewhite", NSVG_RGB(250, 235, 215) },
1324 {
"aqua", NSVG_RGB( 0, 255, 255) },
1325 {
"aquamarine", NSVG_RGB(127, 255, 212) },
1326 {
"azure", NSVG_RGB(240, 255, 255) },
1327 {
"beige", NSVG_RGB(245, 245, 220) },
1328 {
"bisque", NSVG_RGB(255, 228, 196) },
1329 {
"blanchedalmond", NSVG_RGB(255, 235, 205) },
1330 {
"blueviolet", NSVG_RGB(138, 43, 226) },
1331 {
"brown", NSVG_RGB(165, 42, 42) },
1332 {
"burlywood", NSVG_RGB(222, 184, 135) },
1333 {
"cadetblue", NSVG_RGB( 95, 158, 160) },
1334 {
"chartreuse", NSVG_RGB(127, 255, 0) },
1335 {
"chocolate", NSVG_RGB(210, 105, 30) },
1336 {
"coral", NSVG_RGB(255, 127, 80) },
1337 {
"cornflowerblue", NSVG_RGB(100, 149, 237) },
1338 {
"cornsilk", NSVG_RGB(255, 248, 220) },
1339 {
"crimson", NSVG_RGB(220, 20, 60) },
1340 {
"darkblue", NSVG_RGB( 0, 0, 139) },
1341 {
"darkcyan", NSVG_RGB( 0, 139, 139) },
1342 {
"darkgoldenrod", NSVG_RGB(184, 134, 11) },
1343 {
"darkgray", NSVG_RGB(169, 169, 169) },
1344 {
"darkgreen", NSVG_RGB( 0, 100, 0) },
1345 {
"darkgrey", NSVG_RGB(169, 169, 169) },
1346 {
"darkkhaki", NSVG_RGB(189, 183, 107) },
1347 {
"darkmagenta", NSVG_RGB(139, 0, 139) },
1348 {
"darkolivegreen", NSVG_RGB( 85, 107, 47) },
1349 {
"darkorange", NSVG_RGB(255, 140, 0) },
1350 {
"darkorchid", NSVG_RGB(153, 50, 204) },
1351 {
"darkred", NSVG_RGB(139, 0, 0) },
1352 {
"darksalmon", NSVG_RGB(233, 150, 122) },
1353 {
"darkseagreen", NSVG_RGB(143, 188, 143) },
1354 {
"darkslateblue", NSVG_RGB( 72, 61, 139) },
1355 {
"darkslategray", NSVG_RGB( 47, 79, 79) },
1356 {
"darkslategrey", NSVG_RGB( 47, 79, 79) },
1357 {
"darkturquoise", NSVG_RGB( 0, 206, 209) },
1358 {
"darkviolet", NSVG_RGB(148, 0, 211) },
1359 {
"deeppink", NSVG_RGB(255, 20, 147) },
1360 {
"deepskyblue", NSVG_RGB( 0, 191, 255) },
1361 {
"dimgray", NSVG_RGB(105, 105, 105) },
1362 {
"dimgrey", NSVG_RGB(105, 105, 105) },
1363 {
"dodgerblue", NSVG_RGB( 30, 144, 255) },
1364 {
"firebrick", NSVG_RGB(178, 34, 34) },
1365 {
"floralwhite", NSVG_RGB(255, 250, 240) },
1366 {
"forestgreen", NSVG_RGB( 34, 139, 34) },
1367 {
"fuchsia", NSVG_RGB(255, 0, 255) },
1368 {
"gainsboro", NSVG_RGB(220, 220, 220) },
1369 {
"ghostwhite", NSVG_RGB(248, 248, 255) },
1370 {
"gold", NSVG_RGB(255, 215, 0) },
1371 {
"goldenrod", NSVG_RGB(218, 165, 32) },
1372 {
"greenyellow", NSVG_RGB(173, 255, 47) },
1373 {
"honeydew", NSVG_RGB(240, 255, 240) },
1374 {
"hotpink", NSVG_RGB(255, 105, 180) },
1375 {
"indianred", NSVG_RGB(205, 92, 92) },
1376 {
"indigo", NSVG_RGB( 75, 0, 130) },
1377 {
"ivory", NSVG_RGB(255, 255, 240) },
1378 {
"khaki", NSVG_RGB(240, 230, 140) },
1379 {
"lavender", NSVG_RGB(230, 230, 250) },
1380 {
"lavenderblush", NSVG_RGB(255, 240, 245) },
1381 {
"lawngreen", NSVG_RGB(124, 252, 0) },
1382 {
"lemonchiffon", NSVG_RGB(255, 250, 205) },
1383 {
"lightblue", NSVG_RGB(173, 216, 230) },
1384 {
"lightcoral", NSVG_RGB(240, 128, 128) },
1385 {
"lightcyan", NSVG_RGB(224, 255, 255) },
1386 {
"lightgoldenrodyellow", NSVG_RGB(250, 250, 210) },
1387 {
"lightgray", NSVG_RGB(211, 211, 211) },
1388 {
"lightgreen", NSVG_RGB(144, 238, 144) },
1389 {
"lightgrey", NSVG_RGB(211, 211, 211) },
1390 {
"lightpink", NSVG_RGB(255, 182, 193) },
1391 {
"lightsalmon", NSVG_RGB(255, 160, 122) },
1392 {
"lightseagreen", NSVG_RGB( 32, 178, 170) },
1393 {
"lightskyblue", NSVG_RGB(135, 206, 250) },
1394 {
"lightslategray", NSVG_RGB(119, 136, 153) },
1395 {
"lightslategrey", NSVG_RGB(119, 136, 153) },
1396 {
"lightsteelblue", NSVG_RGB(176, 196, 222) },
1397 {
"lightyellow", NSVG_RGB(255, 255, 224) },
1398 {
"lime", NSVG_RGB( 0, 255, 0) },
1399 {
"limegreen", NSVG_RGB( 50, 205, 50) },
1400 {
"linen", NSVG_RGB(250, 240, 230) },
1401 {
"maroon", NSVG_RGB(128, 0, 0) },
1402 {
"mediumaquamarine", NSVG_RGB(102, 205, 170) },
1403 {
"mediumblue", NSVG_RGB( 0, 0, 205) },
1404 {
"mediumorchid", NSVG_RGB(186, 85, 211) },
1405 {
"mediumpurple", NSVG_RGB(147, 112, 219) },
1406 {
"mediumseagreen", NSVG_RGB( 60, 179, 113) },
1407 {
"mediumslateblue", NSVG_RGB(123, 104, 238) },
1408 {
"mediumspringgreen", NSVG_RGB( 0, 250, 154) },
1409 {
"mediumturquoise", NSVG_RGB( 72, 209, 204) },
1410 {
"mediumvioletred", NSVG_RGB(199, 21, 133) },
1411 {
"midnightblue", NSVG_RGB( 25, 25, 112) },
1412 {
"mintcream", NSVG_RGB(245, 255, 250) },
1413 {
"mistyrose", NSVG_RGB(255, 228, 225) },
1414 {
"moccasin", NSVG_RGB(255, 228, 181) },
1415 {
"navajowhite", NSVG_RGB(255, 222, 173) },
1416 {
"navy", NSVG_RGB( 0, 0, 128) },
1417 {
"oldlace", NSVG_RGB(253, 245, 230) },
1418 {
"olive", NSVG_RGB(128, 128, 0) },
1419 {
"olivedrab", NSVG_RGB(107, 142, 35) },
1420 {
"orange", NSVG_RGB(255, 165, 0) },
1421 {
"orangered", NSVG_RGB(255, 69, 0) },
1422 {
"orchid", NSVG_RGB(218, 112, 214) },
1423 {
"palegoldenrod", NSVG_RGB(238, 232, 170) },
1424 {
"palegreen", NSVG_RGB(152, 251, 152) },
1425 {
"paleturquoise", NSVG_RGB(175, 238, 238) },
1426 {
"palevioletred", NSVG_RGB(219, 112, 147) },
1427 {
"papayawhip", NSVG_RGB(255, 239, 213) },
1428 {
"peachpuff", NSVG_RGB(255, 218, 185) },
1429 {
"peru", NSVG_RGB(205, 133, 63) },
1430 {
"pink", NSVG_RGB(255, 192, 203) },
1431 {
"plum", NSVG_RGB(221, 160, 221) },
1432 {
"powderblue", NSVG_RGB(176, 224, 230) },
1433 {
"purple", NSVG_RGB(128, 0, 128) },
1434 {
"rosybrown", NSVG_RGB(188, 143, 143) },
1435 {
"royalblue", NSVG_RGB( 65, 105, 225) },
1436 {
"saddlebrown", NSVG_RGB(139, 69, 19) },
1437 {
"salmon", NSVG_RGB(250, 128, 114) },
1438 {
"sandybrown", NSVG_RGB(244, 164, 96) },
1439 {
"seagreen", NSVG_RGB( 46, 139, 87) },
1440 {
"seashell", NSVG_RGB(255, 245, 238) },
1441 {
"sienna", NSVG_RGB(160, 82, 45) },
1442 {
"silver", NSVG_RGB(192, 192, 192) },
1443 {
"skyblue", NSVG_RGB(135, 206, 235) },
1444 {
"slateblue", NSVG_RGB(106, 90, 205) },
1445 {
"slategray", NSVG_RGB(112, 128, 144) },
1446 {
"slategrey", NSVG_RGB(112, 128, 144) },
1447 {
"snow", NSVG_RGB(255, 250, 250) },
1448 {
"springgreen", NSVG_RGB( 0, 255, 127) },
1449 {
"steelblue", NSVG_RGB( 70, 130, 180) },
1450 {
"tan", NSVG_RGB(210, 180, 140) },
1451 {
"teal", NSVG_RGB( 0, 128, 128) },
1452 {
"thistle", NSVG_RGB(216, 191, 216) },
1453 {
"tomato", NSVG_RGB(255, 99, 71) },
1454 {
"turquoise", NSVG_RGB( 64, 224, 208) },
1455 {
"violet", NSVG_RGB(238, 130, 238) },
1456 {
"wheat", NSVG_RGB(245, 222, 179) },
1457 {
"whitesmoke", NSVG_RGB(245, 245, 245) },
1458 {
"yellowgreen", NSVG_RGB(154, 205, 50) },
1462static unsigned int nsvg__parseColorName(
const char* str)
1464 int i, ncolors =
sizeof(nsvg__colors) /
sizeof(NSVGNamedColor);
1466 for (i = 0; i < ncolors; i++) {
1467 if (strcmp(nsvg__colors[i].name, str) == 0) {
1468 return nsvg__colors[i].color;
1472 return NSVG_RGB(128, 128, 128);
1475static unsigned int nsvg__parseColor(
const char* str)
1478 while(*str ==
' ') ++str;
1480 if (len >= 1 && *str ==
'#')
1481 return nsvg__parseColorHex(str);
1482 else if (len >= 4 && str[0] ==
'r' && str[1] ==
'g' && str[2] ==
'b' && str[3] ==
'(')
1483 return nsvg__parseColorRGB(str);
1484 return nsvg__parseColorName(str);
1487static float nsvg__parseOpacity(
const char* str)
1489 float val = nsvg__atof(str);
1490 if (val < 0.0f) val = 0.0f;
1491 if (val > 1.0f) val = 1.0f;
1495static float nsvg__parseMiterLimit(
const char* str)
1497 float val = nsvg__atof(str);
1498 if (val < 0.0f) val = 0.0f;
1502static int nsvg__parseUnits(
const char* units)
1504 if (units[0] ==
'p' && units[1] ==
'x')
1505 return NSVG_UNITS_PX;
1506 else if (units[0] ==
'p' && units[1] ==
't')
1507 return NSVG_UNITS_PT;
1508 else if (units[0] ==
'p' && units[1] ==
'c')
1509 return NSVG_UNITS_PC;
1510 else if (units[0] ==
'm' && units[1] ==
'm')
1511 return NSVG_UNITS_MM;
1512 else if (units[0] ==
'c' && units[1] ==
'm')
1513 return NSVG_UNITS_CM;
1514 else if (units[0] ==
'i' && units[1] ==
'n')
1515 return NSVG_UNITS_IN;
1516 else if (units[0] ==
'%')
1517 return NSVG_UNITS_PERCENT;
1518 else if (units[0] ==
'e' && units[1] ==
'm')
1519 return NSVG_UNITS_EM;
1520 else if (units[0] ==
'e' && units[1] ==
'x')
1521 return NSVG_UNITS_EX;
1522 return NSVG_UNITS_USER;
1525static int nsvg__isCoordinate(
const char* s)
1528 if (*s ==
'-' || *s ==
'+')
1531 return (nsvg__isdigit(*s) || *s ==
'.');
1534static NSVGcoordinate nsvg__parseCoordinateRaw(
const char* str)
1536 NSVGcoordinate coord = {0, NSVG_UNITS_USER};
1538 coord.units = nsvg__parseUnits(nsvg__parseNumber(str, buf, 64));
1539 coord.value = nsvg__atof(buf);
1543static NSVGcoordinate nsvg__coord(
float v,
int units)
1545 NSVGcoordinate coord = {v, units};
1549static float nsvg__parseCoordinate(NSVGparser* p,
const char* str,
float orig,
float length)
1551 NSVGcoordinate coord = nsvg__parseCoordinateRaw(str);
1552 return nsvg__convertToPixels(p, coord, orig, length);
1555static int nsvg__parseTransformArgs(
const char* str,
float* args,
int maxNa,
int* na)
1563 while (*ptr && *ptr !=
'(') ++ptr;
1567 while (*end && *end !=
')') ++end;
1572 if (*ptr ==
'-' || *ptr ==
'+' || *ptr ==
'.' || nsvg__isdigit(*ptr)) {
1573 if (*na >= maxNa)
return 0;
1574 ptr = nsvg__parseNumber(ptr, it, 64);
1575 args[(*na)++] = (float)nsvg__atof(it);
1580 return (
int)(end - str);
1584static int nsvg__parseMatrix(
float* xform,
const char* str)
1588 int len = nsvg__parseTransformArgs(str, t, 6, &na);
1589 if (na != 6)
return len;
1590 memcpy(xform, t,
sizeof(
float)*6);
1594static int nsvg__parseTranslate(
float* xform,
const char* str)
1599 int len = nsvg__parseTransformArgs(str, args, 2, &na);
1600 if (na == 1) args[1] = 0.0;
1602 nsvg__xformSetTranslation(t, args[0], args[1]);
1603 memcpy(xform, t,
sizeof(
float)*6);
1607static int nsvg__parseScale(
float* xform,
const char* str)
1612 int len = nsvg__parseTransformArgs(str, args, 2, &na);
1613 if (na == 1) args[1] = args[0];
1614 nsvg__xformSetScale(t, args[0], args[1]);
1615 memcpy(xform, t,
sizeof(
float)*6);
1619static int nsvg__parseSkewX(
float* xform,
const char* str)
1624 int len = nsvg__parseTransformArgs(str, args, 1, &na);
1625 nsvg__xformSetSkewX(t, args[0]/180.0f*NSVG_PI);
1626 memcpy(xform, t,
sizeof(
float)*6);
1630static int nsvg__parseSkewY(
float* xform,
const char* str)
1635 int len = nsvg__parseTransformArgs(str, args, 1, &na);
1636 nsvg__xformSetSkewY(t, args[0]/180.0f*NSVG_PI);
1637 memcpy(xform, t,
sizeof(
float)*6);
1641static int nsvg__parseRotate(
float* xform,
const char* str)
1647 int len = nsvg__parseTransformArgs(str, args, 3, &na);
1649 args[1] = args[2] = 0.0f;
1650 nsvg__xformIdentity(m);
1653 nsvg__xformSetTranslation(t, -args[1], -args[2]);
1654 nsvg__xformMultiply(m, t);
1657 nsvg__xformSetRotation(t, args[0]/180.0f*NSVG_PI);
1658 nsvg__xformMultiply(m, t);
1661 nsvg__xformSetTranslation(t, args[1], args[2]);
1662 nsvg__xformMultiply(m, t);
1665 memcpy(xform, m,
sizeof(
float)*6);
1670static void nsvg__parseTransform(
float* xform,
const char* str)
1674 nsvg__xformIdentity(xform);
1677 if (strncmp(str,
"matrix", 6) == 0)
1678 len = nsvg__parseMatrix(t, str);
1679 else if (strncmp(str,
"translate", 9) == 0)
1680 len = nsvg__parseTranslate(t, str);
1681 else if (strncmp(str,
"scale", 5) == 0)
1682 len = nsvg__parseScale(t, str);
1683 else if (strncmp(str,
"rotate", 6) == 0)
1684 len = nsvg__parseRotate(t, str);
1685 else if (strncmp(str,
"skewX", 5) == 0)
1686 len = nsvg__parseSkewX(t, str);
1687 else if (strncmp(str,
"skewY", 5) == 0)
1688 len = nsvg__parseSkewY(t, str);
1700 nsvg__xformPremultiply(xform, t);
1704static void nsvg__parseUrl(
char*
id,
const char* str)
1708 if (*str && *str ==
'#')
1710 while (i < 63 && *str && *str !=
')') {
1717static char nsvg__parseLineCap(
const char* str)
1719 if (strcmp(str,
"butt") == 0)
1721 else if (strcmp(str,
"round") == 0)
1723 else if (strcmp(str,
"square") == 0)
1729static char nsvg__parseLineJoin(
const char* str)
1731 if (strcmp(str,
"miter") == 0)
1733 else if (strcmp(str,
"round") == 0)
1735 else if (strcmp(str,
"bevel") == 0)
1741static char nsvg__parseFillRule(
const char* str)
1743 if (strcmp(str,
"nonzero") == 0)
1745 else if (strcmp(str,
"evenodd") == 0)
1751static const char* nsvg__getNextDashItem(
const char* s,
char* it)
1756 while (*s && (nsvg__isspace(*s) || *s ==
',')) s++;
1758 while (*s && (!nsvg__isspace(*s) && *s !=
',')) {
1767static int nsvg__parseStrokeDashArray(NSVGparser* p,
const char* str,
float* strokeDashArray)
1779 str = nsvg__getNextDashItem(str, item);
1781 if (count < NSVG_MAX_DASHES)
1782 strokeDashArray[count++] = fabsf(nsvg__parseCoordinate(p, item, 0.0f, nsvg__actualLength(p)));
1785 for (i = 0; i < count; i++)
1786 sum += strokeDashArray[i];
1793static void nsvg__parseStyle(NSVGparser* p,
const char* str);
1795static int nsvg__parseAttr(NSVGparser* p,
const char* name,
const char* value)
1798 NSVGattrib* attr = nsvg__getAttr(p);
1799 if (!attr)
return 0;
1801 if (strcmp(name,
"style") == 0) {
1802 nsvg__parseStyle(p, value);
1803 }
else if (strcmp(name,
"display") == 0) {
1804 if (strcmp(value,
"none") == 0)
1808 }
else if (strcmp(name,
"fill") == 0) {
1809 if (strcmp(value,
"none") == 0) {
1811 }
else if (strncmp(value,
"url(", 4) == 0) {
1813 nsvg__parseUrl(attr->fillGradient, value);
1814 }
else if (strncmp(value,
"currentColor", 12) == 0) {
1816 attr->fillColor = p->image->current_color;
1819 attr->fillColor = nsvg__parseColor(value);
1821 }
else if (strcmp(name,
"opacity") == 0) {
1822 attr->opacity = nsvg__parseOpacity(value);
1823 }
else if (strcmp(name,
"fill-opacity") == 0) {
1824 attr->fillOpacity = nsvg__parseOpacity(value);
1825 }
else if (strcmp(name,
"stroke") == 0) {
1826 if (strcmp(value,
"none") == 0) {
1827 attr->hasStroke = 0;
1828 }
else if (strncmp(value,
"url(", 4) == 0) {
1829 attr->hasStroke = 2;
1830 nsvg__parseUrl(attr->strokeGradient, value);
1831 }
else if (strncmp(value,
"currentColor", 12) == 0) {
1832 attr->hasStroke = 1;
1833 attr->strokeColor = p->image->current_color;
1835 attr->hasStroke = 1;
1836 attr->strokeColor = nsvg__parseColor(value);
1838 }
else if (strcmp(name,
"stroke-width") == 0) {
1839 attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1840 }
else if (strcmp(name,
"stroke-dasharray") == 0) {
1841 attr->strokeDashCount = nsvg__parseStrokeDashArray(p, value, attr->strokeDashArray);
1842 }
else if (strcmp(name,
"stroke-dashoffset") == 0) {
1843 attr->strokeDashOffset = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1844 }
else if (strcmp(name,
"stroke-opacity") == 0) {
1845 attr->strokeOpacity = nsvg__parseOpacity(value);
1846 }
else if (strcmp(name,
"stroke-linecap") == 0) {
1847 attr->strokeLineCap = nsvg__parseLineCap(value);
1848 }
else if (strcmp(name,
"stroke-linejoin") == 0) {
1849 attr->strokeLineJoin = nsvg__parseLineJoin(value);
1850 }
else if (strcmp(name,
"stroke-miterlimit") == 0) {
1851 attr->miterLimit = nsvg__parseMiterLimit(value);
1852 }
else if (strcmp(name,
"fill-rule") == 0) {
1853 attr->fillRule = nsvg__parseFillRule(value);
1854 }
else if (strcmp(name,
"font-size") == 0) {
1855 attr->fontSize = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1856 }
else if (strcmp(name,
"transform") == 0) {
1857 nsvg__parseTransform(xform, value);
1858 nsvg__xformPremultiply(attr->xform, xform);
1859 }
else if (strcmp(name,
"stop-color") == 0) {
1860 attr->stopColor = nsvg__parseColor(value);
1861 }
else if (strcmp(name,
"stop-opacity") == 0) {
1862 attr->stopOpacity = nsvg__parseOpacity(value);
1863 }
else if (strcmp(name,
"offset") == 0) {
1864 attr->stopOffset = nsvg__parseCoordinate(p, value, 0.0f, 1.0f);
1865 }
else if (strcmp(name,
"id") == 0) {
1866 strncpy(attr->id, value, 63);
1867 attr->id[63] =
'\0';
1874static int nsvg__parseNameValue(NSVGparser* p,
const char* start,
const char* end)
1883 while (str < end && *str !=
':') ++str;
1888 while (str > start && (*str ==
':' || nsvg__isspace(*str))) --str;
1891 n = (int)(str - start);
1892 if (n > 511) n = 511;
1893 if (n) memcpy(name, start, n);
1896 while (val < end && (*val ==
':' || nsvg__isspace(*val))) ++val;
1898 n = (int)(end - val);
1899 if (n > 511) n = 511;
1900 if (n) memcpy(value, val, n);
1903 return nsvg__parseAttr(p, name, value);
1906static void nsvg__parseStyle(NSVGparser* p,
const char* str)
1913 while(*str && nsvg__isspace(*str)) ++str;
1915 while(*str && *str !=
';') ++str;
1919 while (end > start && (*end ==
';' || nsvg__isspace(*end))) --end;
1922 nsvg__parseNameValue(p, start, end);
1927static void nsvg__parseAttribs(NSVGparser* p,
const char** attr)
1930 for (i = 0; attr[i]; i += 2)
1932 if (strcmp(attr[i],
"style") == 0)
1933 nsvg__parseStyle(p, attr[i + 1]);
1935 nsvg__parseAttr(p, attr[i], attr[i + 1]);
1939static int nsvg__getArgsPerElement(
char cmd)
1972static void nsvg__pathMoveTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
1981 nsvg__moveTo(p, *cpx, *cpy);
1984static void nsvg__pathLineTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
1993 nsvg__lineTo(p, *cpx, *cpy);
1996static void nsvg__pathHLineTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
2002 nsvg__lineTo(p, *cpx, *cpy);
2005static void nsvg__pathVLineTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
2011 nsvg__lineTo(p, *cpx, *cpy);
2014static void nsvg__pathCubicBezTo(NSVGparser* p,
float* cpx,
float* cpy,
2015 float* cpx2,
float* cpy2,
float* args,
int rel)
2017 float x2, y2, cx1, cy1, cx2, cy2;
2020 cx1 = *cpx + args[0];
2021 cy1 = *cpy + args[1];
2022 cx2 = *cpx + args[2];
2023 cy2 = *cpy + args[3];
2024 x2 = *cpx + args[4];
2025 y2 = *cpy + args[5];
2035 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2043static void nsvg__pathCubicBezShortTo(NSVGparser* p,
float* cpx,
float* cpy,
2044 float* cpx2,
float* cpy2,
float* args,
int rel)
2046 float x1, y1, x2, y2, cx1, cy1, cx2, cy2;
2051 cx2 = *cpx + args[0];
2052 cy2 = *cpy + args[1];
2053 x2 = *cpx + args[2];
2054 y2 = *cpy + args[3];
2065 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2073static void nsvg__pathQuadBezTo(NSVGparser* p,
float* cpx,
float* cpy,
2074 float* cpx2,
float* cpy2,
float* args,
int rel)
2076 float x1, y1, x2, y2, cx, cy;
2077 float cx1, cy1, cx2, cy2;
2082 cx = *cpx + args[0];
2083 cy = *cpy + args[1];
2084 x2 = *cpx + args[2];
2085 y2 = *cpy + args[3];
2094 cx1 = x1 + 2.0f/3.0f*(cx - x1);
2095 cy1 = y1 + 2.0f/3.0f*(cy - y1);
2096 cx2 = x2 + 2.0f/3.0f*(cx - x2);
2097 cy2 = y2 + 2.0f/3.0f*(cy - y2);
2099 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2107static void nsvg__pathQuadBezShortTo(NSVGparser* p,
float* cpx,
float* cpy,
2108 float* cpx2,
float* cpy2,
float* args,
int rel)
2110 float x1, y1, x2, y2, cx, cy;
2111 float cx1, cy1, cx2, cy2;
2116 x2 = *cpx + args[0];
2117 y2 = *cpy + args[1];
2127 cx1 = x1 + 2.0f/3.0f*(cx - x1);
2128 cy1 = y1 + 2.0f/3.0f*(cy - y1);
2129 cx2 = x2 + 2.0f/3.0f*(cx - x2);
2130 cy2 = y2 + 2.0f/3.0f*(cy - y2);
2132 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2140static float nsvg__sqr(
float x) {
return x*x; }
2141static float nsvg__vmag(
float x,
float y) {
return sqrtf(x*x + y*y); }
2143static float nsvg__vecrat(
float ux,
float uy,
float vx,
float vy)
2145 return (ux*vx + uy*vy) / (nsvg__vmag(ux,uy) * nsvg__vmag(vx,vy));
2148static float nsvg__vecang(
float ux,
float uy,
float vx,
float vy)
2150 float r = nsvg__vecrat(ux,uy, vx,vy);
2151 if (r < -1.0f) r = -1.0f;
2152 if (r > 1.0f) r = 1.0f;
2153 return ((ux*vy < uy*vx) ? -1.0f : 1.0f) * acosf(r);
2156static void nsvg__pathArcTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
2160 float x1, y1, x2, y2, cx, cy, dx, dy, d;
2161 float x1p, y1p, cxp, cyp, s, sa, sb;
2162 float ux, uy, vx, vy, a1, da;
2163 float x, y, tanx, tany, a, px = 0, py = 0, ptanx = 0, ptany = 0, t[6];
2169 rx = fabsf(args[0]);
2170 ry = fabsf(args[1]);
2171 rotx = args[2] / 180.0f * NSVG_PI;
2172 fa = fabsf(args[3]) > 1e-6 ? 1 : 0;
2173 fs = fabsf(args[4]) > 1e-6 ? 1 : 0;
2177 x2 = *cpx + args[5];
2178 y2 = *cpy + args[6];
2186 d = sqrtf(dx*dx + dy*dy);
2187 if (d < 1e-6f || rx < 1e-6f || ry < 1e-6f) {
2189 nsvg__lineTo(p, x2, y2);
2201 x1p = cosrx * dx / 2.0f + sinrx * dy / 2.0f;
2202 y1p = -sinrx * dx / 2.0f + cosrx * dy / 2.0f;
2203 d = nsvg__sqr(x1p)/nsvg__sqr(rx) + nsvg__sqr(y1p)/nsvg__sqr(ry);
2211 sa = nsvg__sqr(rx)*nsvg__sqr(ry) - nsvg__sqr(rx)*nsvg__sqr(y1p) - nsvg__sqr(ry)*nsvg__sqr(x1p);
2212 sb = nsvg__sqr(rx)*nsvg__sqr(y1p) + nsvg__sqr(ry)*nsvg__sqr(x1p);
2213 if (sa < 0.0f) sa = 0.0f;
2218 cxp = s * rx * y1p / ry;
2219 cyp = s * -ry * x1p / rx;
2222 cx = (x1 + x2)/2.0f + cosrx*cxp - sinrx*cyp;
2223 cy = (y1 + y2)/2.0f + sinrx*cxp + cosrx*cyp;
2226 ux = (x1p - cxp) / rx;
2227 uy = (y1p - cyp) / ry;
2228 vx = (-x1p - cxp) / rx;
2229 vy = (-y1p - cyp) / ry;
2230 a1 = nsvg__vecang(1.0f,0.0f, ux,uy);
2231 da = nsvg__vecang(ux,uy, vx,vy);
2236 if (fs == 0 && da > 0)
2238 else if (fs == 1 && da < 0)
2242 t[0] = cosrx; t[1] = sinrx;
2243 t[2] = -sinrx; t[3] = cosrx;
2244 t[4] = cx; t[5] = cy;
2248 ndivs = (int)(fabsf(da) / (NSVG_PI*0.5f) + 1.0f);
2249 hda = (da / (float)ndivs) / 2.0f;
2251 if ((hda < 1e-3f) && (hda > -1e-3f))
2254 hda = (1.0f - cosf(hda)) / sinf(hda);
2255 kappa = fabsf(4.0f / 3.0f * hda);
2259 for (i = 0; i <= ndivs; i++) {
2260 a = a1 + da * ((float)i/(
float)ndivs);
2263 nsvg__xformPoint(&x, &y, dx*rx, dy*ry, t);
2264 nsvg__xformVec(&tanx, &tany, -dy*rx * kappa, dx*ry * kappa, t);
2266 nsvg__cubicBezTo(p, px+ptanx,py+ptany, x-tanx, y-tany, x, y);
2277static void nsvg__parsePath(NSVGparser* p,
const char** attr)
2279 const char* s = NULL;
2285 float cpx, cpy, cpx2, cpy2;
2291 for (i = 0; attr[i]; i += 2) {
2292 if (strcmp(attr[i],
"d") == 0) {
2296 tmp[1] = attr[i + 1];
2299 nsvg__parseAttribs(p, tmp);
2313 if ((cmd ==
'A' || cmd ==
'a') && (nargs == 3 || nargs == 4))
2314 s = nsvg__getNextPathItemWhenArcFlag(s, item);
2316 s = nsvg__getNextPathItem(s, item);
2318 if (cmd !=
'\0' && nsvg__isCoordinate(item)) {
2320 args[nargs++] = (float)nsvg__atof(item);
2321 if (nargs >= rargs) {
2325 nsvg__pathMoveTo(p, &cpx, &cpy, args, cmd ==
'm' ? 1 : 0);
2328 cmd = (cmd ==
'm') ?
'l' :
'L';
2329 rargs = nsvg__getArgsPerElement(cmd);
2330 cpx2 = cpx; cpy2 = cpy;
2335 nsvg__pathLineTo(p, &cpx, &cpy, args, cmd ==
'l' ? 1 : 0);
2336 cpx2 = cpx; cpy2 = cpy;
2340 nsvg__pathHLineTo(p, &cpx, &cpy, args, cmd ==
'h' ? 1 : 0);
2341 cpx2 = cpx; cpy2 = cpy;
2345 nsvg__pathVLineTo(p, &cpx, &cpy, args, cmd ==
'v' ? 1 : 0);
2346 cpx2 = cpx; cpy2 = cpy;
2350 nsvg__pathCubicBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd ==
'c' ? 1 : 0);
2354 nsvg__pathCubicBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd ==
's' ? 1 : 0);
2358 nsvg__pathQuadBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd ==
'q' ? 1 : 0);
2362 nsvg__pathQuadBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd ==
't' ? 1 : 0);
2366 nsvg__pathArcTo(p, &cpx, &cpy, args, cmd ==
'a' ? 1 : 0);
2367 cpx2 = cpx; cpy2 = cpy;
2371 cpx = args[nargs-2];
2372 cpy = args[nargs-1];
2373 cpx2 = cpx; cpy2 = cpy;
2381 if (cmd ==
'M' || cmd ==
'm') {
2384 nsvg__addPath(p, closedFlag);
2389 }
else if (initPoint == 0) {
2393 if (cmd ==
'Z' || cmd ==
'z') {
2400 cpx2 = cpx; cpy2 = cpy;
2401 nsvg__addPath(p, closedFlag);
2405 nsvg__moveTo(p, cpx, cpy);
2409 rargs = nsvg__getArgsPerElement(cmd);
2419 nsvg__addPath(p, closedFlag);
2425static void nsvg__parseRect(NSVGparser* p,
const char** attr)
2435 for (i = 0; attr[i]; i += 2) {
2436 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2437 if (strcmp(attr[i],
"x") == 0) x = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2438 if (strcmp(attr[i],
"y") == 0) y = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2439 if (strcmp(attr[i],
"width") == 0) w = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p));
2440 if (strcmp(attr[i],
"height") == 0) h = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p));
2441 if (strcmp(attr[i],
"rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
2442 if (strcmp(attr[i],
"ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
2446 if (rx < 0.0f && ry > 0.0f) rx = ry;
2447 if (ry < 0.0f && rx > 0.0f) ry = rx;
2448 if (rx < 0.0f) rx = 0.0f;
2449 if (ry < 0.0f) ry = 0.0f;
2450 if (rx > w/2.0f) rx = w/2.0f;
2451 if (ry > h/2.0f) ry = h/2.0f;
2453 if (w != 0.0f && h != 0.0f) {
2456 if (rx < 0.00001f || ry < 0.0001f) {
2457 nsvg__moveTo(p, x, y);
2458 nsvg__lineTo(p, x+w, y);
2459 nsvg__lineTo(p, x+w, y+h);
2460 nsvg__lineTo(p, x, y+h);
2463 nsvg__moveTo(p, x+rx, y);
2464 nsvg__lineTo(p, x+w-rx, y);
2465 nsvg__cubicBezTo(p, x+w-rx*(1-NSVG_KAPPA90), y, x+w, y+ry*(1-NSVG_KAPPA90), x+w, y+ry);
2466 nsvg__lineTo(p, x+w, y+h-ry);
2467 nsvg__cubicBezTo(p, x+w, y+h-ry*(1-NSVG_KAPPA90), x+w-rx*(1-NSVG_KAPPA90), y+h, x+w-rx, y+h);
2468 nsvg__lineTo(p, x+rx, y+h);
2469 nsvg__cubicBezTo(p, x+rx*(1-NSVG_KAPPA90), y+h, x, y+h-ry*(1-NSVG_KAPPA90), x, y+h-ry);
2470 nsvg__lineTo(p, x, y+ry);
2471 nsvg__cubicBezTo(p, x, y+ry*(1-NSVG_KAPPA90), x+rx*(1-NSVG_KAPPA90), y, x+rx, y);
2474 nsvg__addPath(p, 1);
2480static void nsvg__parseUse(NSVGparser* p,
const char** attr)
2482 const char *href = NULL;
2487 for (i = 0; attr[i]; i += 2) {
2488 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2489 if (strcmp(attr[i],
"xlink:href") == 0)
2491 else if (strcmp(attr[i],
"x") == 0)
2492 x0 = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2493 else if (strcmp(attr[i],
"y") == 0)
2494 y0 = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2499 if (!href || href[0] !=
'#')
2507 for (shape = p->image->shapes; shape != NULL; shape = shape->
next) {
2508 if (strcmp(shape->
id, href) == 0)
2519 for (
int i = 0; i <
path->npts; i++) {
2520 float x =
path->pts[i*2+0];
2521 float y =
path->pts[i*2+1];
2522 nsvg__addPoint(p, x + x0, y + y0);
2524 nsvg__addPath(p,
path->closed);
2530static void nsvg__parseCircle(NSVGparser* p,
const char** attr)
2537 for (i = 0; attr[i]; i += 2) {
2538 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2539 if (strcmp(attr[i],
"cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2540 if (strcmp(attr[i],
"cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2541 if (strcmp(attr[i],
"r") == 0) r = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualLength(p)));
2548 nsvg__moveTo(p, cx+r, cy);
2549 nsvg__cubicBezTo(p, cx+r, cy+r*NSVG_KAPPA90, cx+r*NSVG_KAPPA90, cy+r, cx, cy+r);
2550 nsvg__cubicBezTo(p, cx-r*NSVG_KAPPA90, cy+r, cx-r, cy+r*NSVG_KAPPA90, cx-r, cy);
2551 nsvg__cubicBezTo(p, cx-r, cy-r*NSVG_KAPPA90, cx-r*NSVG_KAPPA90, cy-r, cx, cy-r);
2552 nsvg__cubicBezTo(p, cx+r*NSVG_KAPPA90, cy-r, cx+r, cy-r*NSVG_KAPPA90, cx+r, cy);
2554 nsvg__addPath(p, 1);
2560static void nsvg__parseEllipse(NSVGparser* p,
const char** attr)
2568 for (i = 0; attr[i]; i += 2) {
2569 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2570 if (strcmp(attr[i],
"cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2571 if (strcmp(attr[i],
"cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2572 if (strcmp(attr[i],
"rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
2573 if (strcmp(attr[i],
"ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
2577 if (rx > 0.0f && ry > 0.0f) {
2581 nsvg__moveTo(p, cx+rx, cy);
2582 nsvg__cubicBezTo(p, cx+rx, cy+ry*NSVG_KAPPA90, cx+rx*NSVG_KAPPA90, cy+ry, cx, cy+ry);
2583 nsvg__cubicBezTo(p, cx-rx*NSVG_KAPPA90, cy+ry, cx-rx, cy+ry*NSVG_KAPPA90, cx-rx, cy);
2584 nsvg__cubicBezTo(p, cx-rx, cy-ry*NSVG_KAPPA90, cx-rx*NSVG_KAPPA90, cy-ry, cx, cy-ry);
2585 nsvg__cubicBezTo(p, cx+rx*NSVG_KAPPA90, cy-ry, cx+rx, cy-ry*NSVG_KAPPA90, cx+rx, cy);
2587 nsvg__addPath(p, 1);
2593static void nsvg__parseLine(NSVGparser* p,
const char** attr)
2601 for (i = 0; attr[i]; i += 2) {
2602 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2603 if (strcmp(attr[i],
"x1") == 0) x1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2604 if (strcmp(attr[i],
"y1") == 0) y1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2605 if (strcmp(attr[i],
"x2") == 0) x2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2606 if (strcmp(attr[i],
"y2") == 0) y2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2612 nsvg__moveTo(p, x1, y1);
2613 nsvg__lineTo(p, x2, y2);
2615 nsvg__addPath(p, 0);
2620static void nsvg__parsePoly(NSVGparser* p,
const char** attr,
int closeFlag)
2625 int nargs, npts = 0;
2630 for (i = 0; attr[i]; i += 2) {
2631 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2632 if (strcmp(attr[i],
"points") == 0) {
2636 s = nsvg__getNextPathItem(s, item);
2637 args[nargs++] = (float)nsvg__atof(item);
2640 nsvg__moveTo(p, args[0], args[1]);
2642 nsvg__lineTo(p, args[0], args[1]);
2651 nsvg__addPath(p, (
char)closeFlag);
2656static void nsvg__parseSVG(NSVGparser* p,
const char** attr)
2659 for (i = 0; attr[i]; i += 2) {
2660 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2661 if (strcmp(attr[i],
"width") == 0) {
2662 p->image->width = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
2663 }
else if (strcmp(attr[i],
"height") == 0) {
2664 p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
2665 }
else if (strcmp(attr[i],
"viewBox") == 0) {
2666 const char *s = attr[i + 1];
2668 s = nsvg__parseNumber(s, buf, 64);
2669 p->viewMinx = nsvg__atof(buf);
2670 while (*s && (nsvg__isspace(*s) || *s ==
'%' || *s ==
',')) s++;
2672 s = nsvg__parseNumber(s, buf, 64);
2673 p->viewMiny = nsvg__atof(buf);
2674 while (*s && (nsvg__isspace(*s) || *s ==
'%' || *s ==
',')) s++;
2676 s = nsvg__parseNumber(s, buf, 64);
2677 p->viewWidth = nsvg__atof(buf);
2678 while (*s && (nsvg__isspace(*s) || *s ==
'%' || *s ==
',')) s++;
2680 s = nsvg__parseNumber(s, buf, 64);
2681 p->viewHeight = nsvg__atof(buf);
2682 }
else if (strcmp(attr[i],
"preserveAspectRatio") == 0) {
2683 if (strstr(attr[i + 1],
"none") != 0) {
2685 p->alignType = NSVG_ALIGN_NONE;
2688 if (strstr(attr[i + 1],
"xMin") != 0)
2689 p->alignX = NSVG_ALIGN_MIN;
2690 else if (strstr(attr[i + 1],
"xMid") != 0)
2691 p->alignX = NSVG_ALIGN_MID;
2692 else if (strstr(attr[i + 1],
"xMax") != 0)
2693 p->alignX = NSVG_ALIGN_MAX;
2695 if (strstr(attr[i + 1],
"yMin") != 0)
2696 p->alignY = NSVG_ALIGN_MIN;
2697 else if (strstr(attr[i + 1],
"yMid") != 0)
2698 p->alignY = NSVG_ALIGN_MID;
2699 else if (strstr(attr[i + 1],
"yMax") != 0)
2700 p->alignY = NSVG_ALIGN_MAX;
2702 p->alignType = NSVG_ALIGN_MEET;
2703 if (strstr(attr[i + 1],
"slice") != 0)
2704 p->alignType = NSVG_ALIGN_SLICE;
2711static void nsvg__parseGradient(NSVGparser* p,
const char** attr,
signed char type)
2714 NSVGgradientData* grad = (NSVGgradientData*)malloc(
sizeof(NSVGgradientData));
2715 if (grad == NULL)
return;
2716 memset(grad, 0,
sizeof(NSVGgradientData));
2717 grad->units = NSVG_OBJECT_SPACE;
2720 grad->grad.linear.x1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2721 grad->grad.linear.y1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2722 grad->grad.linear.x2 = nsvg__coord(100.0f, NSVG_UNITS_PERCENT);
2723 grad->grad.linear.y2 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2725 grad->grad.radial.cx = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2726 grad->grad.radial.cy = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2727 grad->grad.radial.r = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2730 nsvg__xformIdentity(grad->xform);
2732 for (i = 0; attr[i]; i += 2) {
2733 if (strcmp(attr[i],
"id") == 0) {
2734 strncpy(grad->id, attr[i+1], 63);
2735 grad->id[63] =
'\0';
2736 }
else if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2737 if (strcmp(attr[i],
"gradientUnits") == 0) {
2738 if (strcmp(attr[i+1],
"objectBoundingBox") == 0)
2739 grad->units = NSVG_OBJECT_SPACE;
2741 grad->units = NSVG_USER_SPACE;
2742 }
else if (strcmp(attr[i],
"gradientTransform") == 0) {
2743 nsvg__parseTransform(grad->xform, attr[i + 1]);
2744 }
else if (strcmp(attr[i],
"cx") == 0) {
2745 grad->grad.radial.cx = nsvg__parseCoordinateRaw(attr[i + 1]);
2746 }
else if (strcmp(attr[i],
"cy") == 0) {
2747 grad->grad.radial.cy = nsvg__parseCoordinateRaw(attr[i + 1]);
2748 }
else if (strcmp(attr[i],
"r") == 0) {
2749 grad->grad.radial.r = nsvg__parseCoordinateRaw(attr[i + 1]);
2750 }
else if (strcmp(attr[i],
"fx") == 0) {
2751 grad->grad.radial.fx = nsvg__parseCoordinateRaw(attr[i + 1]);
2752 }
else if (strcmp(attr[i],
"fy") == 0) {
2753 grad->grad.radial.fy = nsvg__parseCoordinateRaw(attr[i + 1]);
2754 }
else if (strcmp(attr[i],
"x1") == 0) {
2755 grad->grad.linear.x1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2756 }
else if (strcmp(attr[i],
"y1") == 0) {
2757 grad->grad.linear.y1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2758 }
else if (strcmp(attr[i],
"x2") == 0) {
2759 grad->grad.linear.x2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2760 }
else if (strcmp(attr[i],
"y2") == 0) {
2761 grad->grad.linear.y2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2762 }
else if (strcmp(attr[i],
"spreadMethod") == 0) {
2763 if (strcmp(attr[i+1],
"pad") == 0)
2765 else if (strcmp(attr[i+1],
"reflect") == 0)
2767 else if (strcmp(attr[i+1],
"repeat") == 0)
2769 }
else if (strcmp(attr[i],
"xlink:href") == 0) {
2770 const char *href = attr[i+1];
2771 strncpy(grad->ref, href+1, 62);
2772 grad->ref[62] =
'\0';
2777 grad->next = p->gradients;
2778 p->gradients = grad;
2781static void nsvg__parseGradientStop(NSVGparser* p,
const char** attr)
2783 NSVGattrib* curAttr = nsvg__getAttr(p);
2784 NSVGgradientData* grad;
2788 curAttr->stopOffset = 0;
2789 curAttr->stopColor = 0;
2790 curAttr->stopOpacity = 1.0f;
2792 for (i = 0; attr[i]; i += 2) {
2793 nsvg__parseAttr(p, attr[i], attr[i + 1]);
2797 grad = p->gradients;
2798 if (grad == NULL)
return;
2802 if (grad->stops == NULL)
return;
2805 idx = grad->nstops-1;
2806 for (i = 0; i < grad->nstops-1; i++) {
2807 if (curAttr->stopOffset < grad->stops[i].offset) {
2812 if (idx != grad->nstops-1) {
2813 for (i = grad->nstops-1; i > idx; i--)
2814 grad->stops[i] = grad->stops[i-1];
2817 stop = &grad->stops[idx];
2818 stop->
color = curAttr->stopColor;
2819 stop->
color |= (
unsigned int)(curAttr->stopOpacity*255) << 24;
2820 stop->
offset = curAttr->stopOffset;
2823static void nsvg__startElement(
void* ud,
const char* el,
const char** attr)
2825 NSVGparser* p = (NSVGparser*)ud;
2829 if (strcmp(el,
"linearGradient") == 0) {
2831 }
else if (strcmp(el,
"radialGradient") == 0) {
2833 }
else if (strcmp(el,
"stop") == 0) {
2834 nsvg__parseGradientStop(p, attr);
2835 }
else if (strcmp(el,
"path") == 0) {
2837 nsvg__parsePath(p, attr);
2839 }
else if (strcmp(el,
"g") == 0) {
2841 nsvg__parseAttribs(p, attr);
2843 }
else if (strcmp(el,
"symbol") == 0) {
2845 nsvg__parseAttribs(p, attr);
2854 if (strcmp(el,
"g") == 0) {
2856 nsvg__parseAttribs(p, attr);
2858 }
else if (strcmp(el,
"use") == 0) {
2860 nsvg__parseUse(p, attr);
2862 }
else if (strcmp(el,
"path") == 0) {
2866 nsvg__parsePath(p, attr);
2868 }
else if (strcmp(el,
"rect") == 0) {
2870 nsvg__parseRect(p, attr);
2872 }
else if (strcmp(el,
"circle") == 0) {
2874 nsvg__parseCircle(p, attr);
2876 }
else if (strcmp(el,
"ellipse") == 0) {
2878 nsvg__parseEllipse(p, attr);
2880 }
else if (strcmp(el,
"line") == 0) {
2882 nsvg__parseLine(p, attr);
2884 }
else if (strcmp(el,
"polyline") == 0) {
2886 nsvg__parsePoly(p, attr, 0);
2888 }
else if (strcmp(el,
"polygon") == 0) {
2890 nsvg__parsePoly(p, attr, 1);
2892 }
else if (strcmp(el,
"linearGradient") == 0) {
2894 }
else if (strcmp(el,
"radialGradient") == 0) {
2896 }
else if (strcmp(el,
"stop") == 0) {
2897 nsvg__parseGradientStop(p, attr);
2898 }
else if (strcmp(el,
"defs") == 0) {
2900 }
else if (strcmp(el,
"svg") == 0) {
2901 nsvg__parseSVG(p, attr);
2908static void nsvg__endElement(
void* ud,
const char* el)
2910 NSVGparser* p = (NSVGparser*)ud;
2913 if (strcmp(el,
"g") == 0)
2915 else if (strcmp(el,
"symbol") == 0)
2917 else if (strcmp(el,
"defs") == 0)
2922 if (strcmp(el,
"g") == 0) {
2924 }
else if (strcmp(el,
"path") == 0) {
2929static void nsvg__content(
void* ud,
const char* s)
2936static void nsvg__imageBounds(NSVGparser* p,
float* bounds)
2939 shape = p->image->shapes;
2940 if (shape == NULL) {
2941 bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0;
2944 bounds[0] = shape->
bounds[0];
2945 bounds[1] = shape->
bounds[1];
2946 bounds[2] = shape->
bounds[2];
2947 bounds[3] = shape->
bounds[3];
2948 for (shape = shape->
next; shape != NULL; shape = shape->
next) {
2949 bounds[0] = nsvg__minf(bounds[0], shape->
bounds[0]);
2950 bounds[1] = nsvg__minf(bounds[1], shape->
bounds[1]);
2951 bounds[2] = nsvg__maxf(bounds[2], shape->
bounds[2]);
2952 bounds[3] = nsvg__maxf(bounds[3], shape->
bounds[3]);
2956static float nsvg__viewAlign(
float content,
float container,
int type)
2958 if (type == NSVG_ALIGN_MIN)
2960 else if (type == NSVG_ALIGN_MAX)
2961 return container - content;
2963 return (container - content) * 0.5f;
2966static void nsvg__scaleGradient(
NSVGgradient* grad,
float tx,
float ty,
float sx,
float sy)
2969 nsvg__xformSetTranslation(t, tx, ty);
2970 nsvg__xformMultiply (grad->
xform, t);
2972 nsvg__xformSetScale(t, sx, sy);
2973 nsvg__xformMultiply (grad->
xform, t);
2976static void nsvg__scaleToViewbox(NSVGparser* p,
const char* units)
2980 float tx, ty, sx, sy, us, bounds[4], t[6], avgs;
2985 nsvg__imageBounds(p, bounds);
2987 if (p->viewWidth == 0) {
2988 if (p->image->width > 0) {
2989 p->viewWidth = p->image->width;
2991 p->viewMinx = bounds[0];
2992 p->viewWidth = bounds[2] - bounds[0];
2995 if (p->viewHeight == 0) {
2996 if (p->image->height > 0) {
2997 p->viewHeight = p->image->height;
2999 p->viewMiny = bounds[1];
3000 p->viewHeight = bounds[3] - bounds[1];
3003 if (p->image->width == 0)
3004 p->image->width = p->viewWidth;
3005 if (p->image->height == 0)
3006 p->image->height = p->viewHeight;
3010 sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0;
3011 sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0;
3013 us = 1.0f / nsvg__convertToPixels(p, nsvg__coord(1.0f, nsvg__parseUnits(units)), 0.0f, 1.0f);
3016 if (p->alignType == NSVG_ALIGN_MEET) {
3018 sx = sy = nsvg__minf(sx, sy);
3019 tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
3020 ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
3021 }
else if (p->alignType == NSVG_ALIGN_SLICE) {
3023 sx = sy = nsvg__maxf(sx, sy);
3024 tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
3025 ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
3031 avgs = (sx+sy) / 2.0f;
3032 for (shape = p->image->shapes; shape != NULL; shape = shape->
next) {
3041 path->bounds[0] = (
path->bounds[0] + tx) * sx;
3042 path->bounds[1] = (
path->bounds[1] + ty) * sy;
3043 path->bounds[2] = (
path->bounds[2] + tx) * sx;
3044 path->bounds[3] = (
path->bounds[3] + ty) * sy;
3045 for (i =0; i <
path->npts; i++) {
3046 pt = &
path->pts[i*2];
3047 pt[0] = (pt[0] + tx) * sx;
3048 pt[1] = (pt[1] + ty) * sy;
3070static void nsvg__createGradients(NSVGparser* p)
3074 for (shape = p->image->shapes; shape != NULL; shape = shape->
next) {
3080 float inv[6], localBounds[4];
3081 nsvg__xformInverse(inv, shape->
xform);
3082 nsvg__getLocalBounds(localBounds, shape, inv);
3091 float inv[6], localBounds[4];
3092 nsvg__xformInverse(inv, shape->
xform);
3093 nsvg__getLocalBounds(localBounds, shape, inv);
3103NSVGimage*
nsvgParse(
char* input,
const char* units,
float dpi,
unsigned current_color)
3108 p = nsvg__createParser(current_color);
3114 nsvg__parseXML(input, nsvg__startElement, nsvg__endElement, nsvg__content, p);
3117 nsvg__createGradients(p);
3120 nsvg__scaleToViewbox(p, units);
3125 nsvg__deleteParser(p);
3137 fp = fopen(filename,
"rb");
3138 if (!fp)
goto error;
3139 fseek(fp, 0, SEEK_END);
3141 fseek(fp, 0, SEEK_SET);
3142 data = (
char*)malloc(size+1);
3143 if (data == NULL)
goto error;
3144 if (fread(data, 1, size, fp) != size)
goto error;
3154 if (data) free(data);
3167 if (res == NULL)
goto error;
3170 res->
pts = (
float*)malloc(p->
npts*2*
sizeof(
float));
3172 memcpy(res->
pts, p->
pts, p->
npts *
sizeof(
float) * 2);
3192 if (
image == NULL)
return;
3193 shape =
image->shapes;
3194 while (shape != NULL) {
3195 snext = shape->
next;
3196 nsvg__deletePaths(shape->
paths);
3197 nsvg__deletePaint(&shape->
fill);
3198 nsvg__deletePaint(&shape->
stroke);
static void error(char *msg)
NSVGpath * nsvgDuplicatePath(NSVGpath *p)
@ NSVG_PAINT_RADIAL_GRADIENT
@ NSVG_PAINT_LINEAR_GRADIENT
NSVGimage * nsvgParseFromFile(const char *filename, const char *units, float dpi, unsigned current_color)
NSVGimage * nsvgParse(char *input, const char *units, float dpi, unsigned current_color)
void nsvgDelete(NSVGimage *image)
NSVGgradientStop stops[1]