32#ifndef NANOSVG_CPLUSPLUS
189#ifndef NANOSVG_CPLUSPLUS
195#ifdef NANOSVG_IMPLEMENTATION
202#define NSVG_PI (3.14159265358979323846264338327f)
203#define NSVG_KAPPA90 (0.5522847493f)
205#define NSVG_ALIGN_MIN 0
206#define NSVG_ALIGN_MID 1
207#define NSVG_ALIGN_MAX 2
208#define NSVG_ALIGN_NONE 0
209#define NSVG_ALIGN_MEET 1
210#define NSVG_ALIGN_SLICE 2
212#define NSVG_NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0)
213#define NSVG_RGB(r, g, b) (((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16))
216#pragma warning (disable: 4996)
217#pragma warning (disable: 4100)
219#define NSVG_INLINE inline
224#define NSVG_INLINE inline
228static int nsvg__isspace(
char c)
230 return strchr(
" \t\n\v\f\r", c) != 0;
233static int nsvg__isdigit(
char c)
235 return c >=
'0' && c <=
'9';
238static NSVG_INLINE
float nsvg__minf(
float a,
float b) {
return a < b ? a : b; }
239static NSVG_INLINE
float nsvg__maxf(
float a,
float b) {
return a > b ? a : b; }
244#define NSVG_XML_TAG 1
245#define NSVG_XML_CONTENT 2
246#define NSVG_XML_MAX_ATTRIBS 256
248static void nsvg__parseContent(
char* s,
249 void (*contentCb)(
void* ud,
const char* s),
253 while (*s && nsvg__isspace(*s)) s++;
260static void nsvg__parseElement(
char* s,
261 void (*startelCb)(
void* ud,
const char* el,
const char** attr),
262 void (*endelCb)(
void* ud,
const char* el),
265 const char* attr[NSVG_XML_MAX_ATTRIBS];
273 while (*s && nsvg__isspace(*s)) s++;
285 if (!*s || *s ==
'?' || *s ==
'!')
290 while (*s && !nsvg__isspace(*s)) s++;
291 if (*s) { *s++ =
'\0'; }
294 while (!
end && *s && nattr < NSVG_XML_MAX_ATTRIBS - 3) {
299 while (*s && nsvg__isspace(*s)) s++;
307 while (*s && !nsvg__isspace(*s) && *s !=
'=') s++;
308 if (*s) { *s++ =
'\0'; }
310 while (*s && *s !=
'\"' && *s !=
'\'') s++;
316 while (*s && *s != quote) s++;
317 if (*s) { *s++ =
'\0'; }
321 attr[nattr++] = name;
322 attr[nattr++] = value;
331 if (start && startelCb)
332 (*startelCb)(ud, name, attr);
334 (*endelCb)(ud, name);
337int nsvg__parseXML(
char*
input,
338 void (*startelCb)(
void* ud,
const char* el,
const char** attr),
339 void (*endelCb)(
void* ud,
const char* el),
340 void (*contentCb)(
void* ud,
const char* s),
345 int state = NSVG_XML_CONTENT;
347 if (*s ==
'<' && state == NSVG_XML_CONTENT) {
350 nsvg__parseContent(mark, contentCb, ud);
352 state = NSVG_XML_TAG;
354 else if (*s ==
'>' && state == NSVG_XML_TAG) {
357 nsvg__parseElement(mark, startelCb, endelCb, ud);
359 state = NSVG_XML_CONTENT;
372#define NSVG_MAX_ATTR 128
374enum NSVGgradientUnits {
376 NSVG_OBJECT_SPACE = 1
379#define NSVG_MAX_DASHES 8
380#define NSVG_MAX_CLASSES 32
395typedef struct NSVGcoordinate {
400typedef struct NSVGlinearData {
401 NSVGcoordinate x1, y1, x2, y2;
404typedef struct NSVGradialData {
405 NSVGcoordinate cx, cy, r, fx, fy;
408typedef struct NSVGgradientData
414 NSVGlinearData linear;
415 NSVGradialData radial;
422 struct NSVGgradientData* next;
425typedef struct NSVGattrib
429 unsigned int fillColor;
430 unsigned int strokeColor;
434 char fillGradient[64];
435 char strokeGradient[64];
437 float strokeDashOffset;
438 float strokeDashArray[NSVG_MAX_DASHES];
445 unsigned int stopColor;
451 unsigned char paintOrder;
454typedef struct NSVGstyleDeclaration
457 char* propertiesText;
458 struct NSVGstyleDeclaration* next;
459} NSVGstyleDeclaration;
461typedef struct NSVGparser
463 NSVGattrib attr[NSVG_MAX_ATTR];
470 NSVGstyleDeclaration* styles;
471 NSVGgradientData* gradients;
473 float viewMinx, viewMiny, viewWidth, viewHeight;
474 int alignX, alignY, alignType;
481static void nsvg__xformIdentity(
float*
t)
483 t[0] = 1.0f;
t[1] = 0.0f;
484 t[2] = 0.0f;
t[3] = 1.0f;
485 t[4] = 0.0f;
t[5] = 0.0f;
488static void nsvg__xformSetTranslation(
float*
t,
float tx,
float ty)
490 t[0] = 1.0f;
t[1] = 0.0f;
491 t[2] = 0.0f;
t[3] = 1.0f;
492 t[4] = tx;
t[5] = ty;
495static void nsvg__xformSetScale(
float*
t,
float sx,
float sy)
497 t[0] = sx;
t[1] = 0.0f;
498 t[2] = 0.0f;
t[3] = sy;
499 t[4] = 0.0f;
t[5] = 0.0f;
502static void nsvg__xformSetSkewX(
float*
t,
float a)
504 t[0] = 1.0f;
t[1] = 0.0f;
505 t[2] =
tanf(a);
t[3] = 1.0f;
506 t[4] = 0.0f;
t[5] = 0.0f;
509static void nsvg__xformSetSkewY(
float*
t,
float a)
511 t[0] = 1.0f;
t[1] =
tanf(a);
512 t[2] = 0.0f;
t[3] = 1.0f;
513 t[4] = 0.0f;
t[5] = 0.0f;
516static void nsvg__xformSetRotation(
float*
t,
float a)
519 t[0] = cs;
t[1] = sn;
520 t[2] = -sn;
t[3] = cs;
521 t[4] = 0.0f;
t[5] = 0.0f;
524static void nsvg__xformMultiply(
float*
t,
float* s)
526 float t0 =
t[0] * s[0] +
t[1] * s[2];
527 float t2 =
t[2] * s[0] +
t[3] * s[2];
528 float t4 =
t[4] * s[0] +
t[5] * s[2] + s[4];
529 t[1] =
t[0] * s[1] +
t[1] * s[3];
530 t[3] =
t[2] * s[1] +
t[3] * s[3];
531 t[5] =
t[4] * s[1] +
t[5] * s[3] + s[5];
537static void nsvg__xformInverse(
float* inv,
float*
t)
539 double invdet, det = (double)
t[0] *
t[3] - (
double)
t[2] *
t[1];
540 if (det > -1e-6 && det < 1e-6) {
541 nsvg__xformIdentity(
t);
545 inv[0] = (float)(
t[3] * invdet);
546 inv[2] = (float)(-
t[2] * invdet);
547 inv[4] = (float)(((
double)
t[2] *
t[5] - (double)
t[3] *
t[4]) * invdet);
548 inv[1] = (float)(-
t[1] * invdet);
549 inv[3] = (float)(
t[0] * invdet);
550 inv[5] = (float)(((
double)
t[1] *
t[4] - (double)
t[0] *
t[5]) * invdet);
553static void nsvg__xformPremultiply(
float*
t,
float* s)
556 memcpy(s2, s,
sizeof(
float) * 6);
557 nsvg__xformMultiply(s2,
t);
558 memcpy(
t, s2,
sizeof(
float) * 6);
561static void nsvg__xformPoint(
float* dx,
float* dy,
float x,
float y,
float*
t)
563 *dx =
x *
t[0] + y *
t[2] +
t[4];
564 *dy =
x *
t[1] + y *
t[3] +
t[5];
567static void nsvg__xformVec(
float* dx,
float* dy,
float x,
float y,
float*
t)
569 *dx =
x *
t[0] + y *
t[2];
570 *dy =
x *
t[1] + y *
t[3];
573#define NSVG_EPSILON (1e-12)
575static int nsvg__ptInBounds(
float* pt,
float* bounds)
577 return pt[0] >= bounds[0] && pt[0] <= bounds[2] && pt[1] >= bounds[1] && pt[1] <= bounds[3];
581static double nsvg__evalBezier(
double t,
double p0,
double p1,
double p2,
double p3)
584 return it * it * it * p0 + 3.0 * it * it *
t * p1 + 3.0 * it *
t *
t * p2 +
t *
t *
t * p3;
587static void nsvg__curveBounds(
float* bounds,
float* curve)
590 double roots[2], a, b, c, b2ac,
t, v;
591 float* v0 = &curve[0];
592 float* v1 = &curve[2];
593 float* v2 = &curve[4];
594 float* v3 = &curve[6];
597 bounds[0] = nsvg__minf(v0[0], v3[0]);
598 bounds[1] = nsvg__minf(v0[1], v3[1]);
599 bounds[2] = nsvg__maxf(v0[0], v3[0]);
600 bounds[3] = nsvg__maxf(v0[1], v3[1]);
604 if (nsvg__ptInBounds(v1, bounds) && nsvg__ptInBounds(v2, bounds))
608 for (i = 0; i < 2; i++) {
609 a = -3.0 * v0[i] + 9.0 * v1[i] - 9.0 * v2[i] + 3.0 * v3[i];
610 b = 6.0 * v0[i] - 12.0 * v1[i] + 6.0 * v2[i];
611 c = 3.0 * v1[i] - 3.0 * v0[i];
613 if (
fabs(a) < NSVG_EPSILON) {
614 if (
fabs(b) > NSVG_EPSILON) {
616 if (
t > NSVG_EPSILON &&
t < 1.0 - NSVG_EPSILON)
621 b2ac = b * b - 4.0 * c * a;
622 if (b2ac > NSVG_EPSILON) {
623 t = (-b +
sqrt(b2ac)) / (2.0 * a);
624 if (
t > NSVG_EPSILON &&
t < 1.0 - NSVG_EPSILON)
626 t = (-b -
sqrt(b2ac)) / (2.0 * a);
627 if (
t > NSVG_EPSILON &&
t < 1.0 - NSVG_EPSILON)
631 for (j = 0; j < count; j++) {
632 v = nsvg__evalBezier(roots[j], v0[i], v1[i], v2[i], v3[i]);
633 bounds[0 + i] = nsvg__minf(bounds[0 + i], (
float)v);
634 bounds[2 + i] = nsvg__maxf(bounds[2 + i], (
float)v);
640 return (a & 0x03) | ((b & 0x03) << 2) | ((c & 0x03) << 4);
643static NSVGparser* nsvg__createParser(
void)
646 p = (NSVGparser*)
malloc(
sizeof(NSVGparser));
647 if (p ==
NULL)
goto error;
648 memset(p, 0,
sizeof(NSVGparser));
651 if (p->image ==
NULL)
goto error;
655 nsvg__xformIdentity(p->attr[0].xform);
656 memset(p->attr[0].id, 0,
sizeof p->attr[0].id);
657 p->attr[0].fillColor = NSVG_RGB(0, 0, 0);
658 p->attr[0].strokeColor = NSVG_RGB(0, 0, 0);
659 p->attr[0].opacity = 1;
660 p->attr[0].fillOpacity = 1;
661 p->attr[0].strokeOpacity = 1;
662 p->attr[0].stopOpacity = 1;
663 p->attr[0].strokeWidth = 1;
666 p->attr[0].miterLimit = 4;
668 p->attr[0].hasFill = 1;
669 p->attr[0].visible = 1;
676 if (p->image)
free(p->image);
681static void nsvg__deleteStyles(NSVGstyleDeclaration* style) {
683 NSVGstyleDeclaration* next = style->next;
684 free(style->className);
685 free(style->propertiesText);
702static void nsvg__deletePaint(
NSVGpaint* paint)
708static void nsvg__deleteGradientData(NSVGgradientData* grad)
710 NSVGgradientData* next;
711 while (grad !=
NULL) {
719static void nsvg__deleteParser(NSVGparser* p)
722 nsvg__deleteStyles(p->styles);
723 nsvg__deletePaths(p->plist);
724 nsvg__deleteGradientData(p->gradients);
731static void nsvg__resetPath(NSVGparser* p)
736static void nsvg__addPoint(NSVGparser* p,
float x,
float y)
738 if (p->npts + 1 > p->cpts) {
739 p->cpts = p->cpts ? p->cpts * 2 : 8;
740 p->pts = (
float*)
realloc(p->pts, p->cpts * 2 *
sizeof(
float));
743 p->pts[p->npts * 2 + 0] =
x;
744 p->pts[p->npts * 2 + 1] = y;
748static void nsvg__moveTo(NSVGparser* p,
float x,
float y)
751 p->pts[(p->npts - 1) * 2 + 0] = x;
752 p->pts[(p->npts - 1) * 2 + 1] = y;
755 nsvg__addPoint(p, x, y);
759static void nsvg__lineTo(NSVGparser* p,
float x,
float y)
761 float px, py, dx, dy;
763 px = p->pts[(p->npts - 1) * 2 + 0];
764 py = p->pts[(p->npts - 1) * 2 + 1];
767 nsvg__addPoint(p, px + dx / 3.0f, py + dy / 3.0f);
768 nsvg__addPoint(p, x - dx / 3.0f, y - dy / 3.0f);
769 nsvg__addPoint(p, x, y);
773static void nsvg__cubicBezTo(NSVGparser* p,
float cpx1,
float cpy1,
float cpx2,
float cpy2,
float x,
float y)
776 nsvg__addPoint(p, cpx1, cpy1);
777 nsvg__addPoint(p, cpx2, cpy2);
778 nsvg__addPoint(p, x, y);
782static NSVGattrib* nsvg__getAttr(NSVGparser* p)
784 return &p->attr[p->attrHead];
787static void nsvg__pushAttr(NSVGparser* p)
789 if (p->attrHead < NSVG_MAX_ATTR - 1) {
791 memcpy(&p->attr[p->attrHead], &p->attr[p->attrHead - 1],
sizeof(NSVGattrib));
795static void nsvg__popAttr(NSVGparser* p)
801static float nsvg__actualOrigX(NSVGparser* p)
806static float nsvg__actualOrigY(NSVGparser* p)
811static float nsvg__actualWidth(NSVGparser* p)
816static float nsvg__actualHeight(NSVGparser* p)
818 return p->viewHeight;
821static float nsvg__actualLength(NSVGparser* p)
823 float w = nsvg__actualWidth(p), h = nsvg__actualHeight(p);
827static float nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c,
float orig,
float length)
829 NSVGattrib* attr = nsvg__getAttr(p);
831 case NSVG_UNITS_USER:
return c.value;
832 case NSVG_UNITS_PX:
return c.value;
833 case NSVG_UNITS_PT:
return c.value / 72.0f * p->dpi;
834 case NSVG_UNITS_PC:
return c.value / 6.0f * p->dpi;
835 case NSVG_UNITS_MM:
return c.value / 25.4f * p->dpi;
836 case NSVG_UNITS_CM:
return c.value / 2.54f * p->dpi;
837 case NSVG_UNITS_IN:
return c.value * p->dpi;
838 case NSVG_UNITS_EM:
return c.value * attr->fontSize;
839 case NSVG_UNITS_EX:
return c.value * attr->fontSize * 0.52f;
840 case NSVG_UNITS_PERCENT:
return orig + c.value / 100.0f * length;
841 default:
return c.value;
846static NSVGgradientData* nsvg__findGradientData(NSVGparser* p,
const char*
id)
848 NSVGgradientData* grad = p->gradients;
849 if (
id ==
NULL || *
id ==
'\0')
851 while (grad !=
NULL) {
852 if (
strcmp(grad->id,
id) == 0)
859static NSVGgradient* nsvg__createGradient(NSVGparser* p,
const char*
id,
const float* localBounds,
float* xform,
signed char* paintType)
861 NSVGgradientData* data =
NULL;
862 NSVGgradientData* ref =
NULL;
865 float ox, oy, sw, sh, sl;
869 data = nsvg__findGradientData(p,
id);
875 while (ref !=
NULL) {
876 NSVGgradientData* nextRef =
NULL;
877 if (stops ==
NULL && ref->stops !=
NULL) {
879 nstops = ref->nstops;
882 nextRef = nsvg__findGradientData(p, ref->ref);
883 if (nextRef == ref)
break;
886 if (refIter > 32)
break;
894 if (data->units == NSVG_OBJECT_SPACE) {
897 sw = localBounds[2] - localBounds[0];
898 sh = localBounds[3] - localBounds[1];
901 ox = nsvg__actualOrigX(p);
902 oy = nsvg__actualOrigY(p);
903 sw = nsvg__actualWidth(p);
904 sh = nsvg__actualHeight(p);
909 float x1, y1, x2, y2, dx, dy;
910 x1 = nsvg__convertToPixels(p, data->linear.x1, ox, sw);
911 y1 = nsvg__convertToPixels(p, data->linear.y1, oy, sh);
912 x2 = nsvg__convertToPixels(p, data->linear.x2, ox, sw);
913 y2 = nsvg__convertToPixels(p, data->linear.y2, oy, sh);
922 float cx, cy, fx, fy, r;
923 cx = nsvg__convertToPixels(p, data->radial.cx, ox, sw);
924 cy = nsvg__convertToPixels(p, data->radial.cy, oy, sh);
925 fx = nsvg__convertToPixels(p, data->radial.fx, ox, sw);
926 fy = nsvg__convertToPixels(p, data->radial.fy, oy, sh);
927 r = nsvg__convertToPixels(p, data->radial.r, 0, sl);
936 nsvg__xformMultiply(grad->
xform, data->xform);
937 nsvg__xformMultiply(grad->
xform, xform);
939 grad->
spread = data->spread;
943 *paintType = data->type;
948static float nsvg__getAverageScale(
float*
t)
950 float sx =
sqrtf(
t[0] *
t[0] +
t[2] *
t[2]);
951 float sy =
sqrtf(
t[1] *
t[1] +
t[3] *
t[3]);
952 return (sx + sy) * 0.5f;
955static void nsvg__getLocalBounds(
float* bounds,
NSVGshape* shape,
float* xform)
958 float curve[4 * 2], curveBounds[4];
961 nsvg__xformPoint(&curve[0], &curve[1],
path->pts[0],
path->pts[1], xform);
962 for (i = 0; i <
path->npts - 1; i += 3) {
963 nsvg__xformPoint(&curve[2], &curve[3],
path->pts[(i + 1) * 2],
path->pts[(i + 1) * 2 + 1], xform);
964 nsvg__xformPoint(&curve[4], &curve[5],
path->pts[(i + 2) * 2],
path->pts[(i + 2) * 2 + 1], xform);
965 nsvg__xformPoint(&curve[6], &curve[7],
path->pts[(i + 3) * 2],
path->pts[(i + 3) * 2 + 1], xform);
966 nsvg__curveBounds(curveBounds, curve);
968 bounds[0] = curveBounds[0];
969 bounds[1] = curveBounds[1];
970 bounds[2] = curveBounds[2];
971 bounds[3] = curveBounds[3];
975 bounds[0] = nsvg__minf(bounds[0], curveBounds[0]);
976 bounds[1] = nsvg__minf(bounds[1], curveBounds[1]);
977 bounds[2] = nsvg__maxf(bounds[2], curveBounds[2]);
978 bounds[3] = nsvg__maxf(bounds[3], curveBounds[3]);
986static void nsvg__addShape(NSVGparser* p)
988 NSVGattrib* attr = nsvg__getAttr(p);
994 if (p->plist ==
NULL)
998 if (shape ==
NULL)
goto error;
1001 memcpy(shape->
id, attr->id,
sizeof shape->
id);
1005 scale = nsvg__getAverageScale(attr->xform);
1009 for (i = 0; i < attr->strokeDashCount; i++)
1015 shape->
opacity = attr->opacity;
1018 shape->
paths = p->plist;
1034 if (attr->hasFill == 0) {
1037 else if (attr->hasFill == 1) {
1040 shape->
fill.
color |= (
unsigned int)(attr->fillOpacity * 255) << 24;
1042 else if (attr->hasFill == 2) {
1047 if (attr->hasStroke == 0) {
1050 else if (attr->hasStroke == 1) {
1053 shape->
stroke.
color |= (
unsigned int)(attr->strokeOpacity * 255) << 24;
1055 else if (attr->hasStroke == 2) {
1063 if (p->image->shapes ==
NULL)
1064 p->image->shapes = shape;
1066 p->shapesTail->next = shape;
1067 p->shapesTail = shape;
1072 if (shape)
free(shape);
1075static void nsvg__addPath(NSVGparser* p,
char closed)
1077 NSVGattrib* attr = nsvg__getAttr(p);
1087 nsvg__lineTo(p, p->pts[0], p->pts[1]);
1090 if ((p->npts % 3) != 1)
1097 path->pts = (
float*)
malloc(p->npts * 2 *
sizeof(
float));
1099 path->closed = closed;
1100 path->npts = p->npts;
1103 for (i = 0; i < p->npts; ++i)
1104 nsvg__xformPoint(&
path->pts[i * 2], &
path->pts[i * 2 + 1], p->pts[i * 2], p->pts[i * 2 + 1], attr->xform);
1107 for (i = 0; i <
path->npts - 1; i += 3) {
1108 curve = &
path->pts[i * 2];
1109 nsvg__curveBounds(bounds, curve);
1111 path->bounds[0] = bounds[0];
1112 path->bounds[1] = bounds[1];
1113 path->bounds[2] = bounds[2];
1114 path->bounds[3] = bounds[3];
1117 path->bounds[0] = nsvg__minf(
path->bounds[0], bounds[0]);
1118 path->bounds[1] = nsvg__minf(
path->bounds[1], bounds[1]);
1119 path->bounds[2] = nsvg__maxf(
path->bounds[2], bounds[2]);
1120 path->bounds[3] = nsvg__maxf(
path->bounds[3], bounds[3]);
1124 path->next = p->plist;
1137static double nsvg__atof(
const char* s)
1139 char* cur = (
char*)s;
1141 double res = 0.0,
sign = 1.0;
1142 long long intPart = 0, fracPart = 0;
1143 char hasIntPart = 0, hasFracPart = 0;
1149 else if (*cur ==
'-') {
1155 if (nsvg__isdigit(*cur)) {
1159 res = (double)intPart;
1168 if (nsvg__isdigit(*cur)) {
1172 res += (double)fracPart /
pow(10.0, (
double)(
end - cur));
1180 if (!hasIntPart && !hasFracPart)
1184 if (*cur ==
'e' || *cur ==
'E') {
1189 res *=
pow(10.0, (
double)expPart);
1197static const char* nsvg__parseNumber(
const char* s,
char* it,
const int size)
1199 const int last = size - 1;
1203 if (*s ==
'-' || *s ==
'+') {
1204 if (i <
last) it[i++] = *s;
1208 while (*s && nsvg__isdigit(*s)) {
1209 if (i <
last) it[i++] = *s;
1214 if (i <
last) it[i++] = *s;
1217 while (*s && nsvg__isdigit(*s)) {
1218 if (i <
last) it[i++] = *s;
1223 if ((*s ==
'e' || *s ==
'E') && (s[1] !=
'm' && s[1] !=
'x')) {
1224 if (i <
last) it[i++] = *s;
1226 if (*s ==
'-' || *s ==
'+') {
1227 if (i <
last) it[i++] = *s;
1230 while (*s && nsvg__isdigit(*s)) {
1231 if (i <
last) it[i++] = *s;
1240static const char* nsvg__getNextPathItemWhenArcFlag(
const char* s,
char* it)
1243 while (*s && (nsvg__isspace(*s) || *s ==
',')) s++;
1245 if (*s ==
'0' || *s ==
'1') {
1253static const char* nsvg__getNextPathItem(
const char* s,
char* it)
1257 while (*s && (nsvg__isspace(*s) || *s ==
',')) s++;
1259 if (*s ==
'-' || *s ==
'+' || *s ==
'.' || nsvg__isdigit(*s)) {
1260 s = nsvg__parseNumber(s, it, 64);
1272static unsigned int nsvg__parseColorHex(
const char* str)
1274 unsigned int r = 0, g = 0, b = 0;
1275 if (
sscanf(str,
"#%2x%2x%2x", &r, &g, &b) == 3)
1276 return NSVG_RGB(r, g, b);
1277 if (
sscanf(str,
"#%1x%1x%1x", &r, &g, &b) == 3)
1278 return NSVG_RGB(r * 17, g * 17, b * 17);
1279 return NSVG_RGB(128, 128, 128);
1286static unsigned int nsvg__parseColorRGB(
const char* str)
1289 unsigned int rgbi[3];
1292 if (
sscanf(str,
"rgb(%u, %u, %u)", &rgbi[0], &rgbi[1], &rgbi[2]) != 3) {
1294 const char delimiter[3] = {
',',
',',
')' };
1296 for (i = 0; i < 3; i++) {
1297 while (*str && (nsvg__isspace(*str))) str++;
1298 if (*str ==
'+') str++;
1300 rgbf[i] = nsvg__atof(str);
1310 while (*str && nsvg__isdigit(*str)) str++;
1313 if (!nsvg__isdigit(*str))
break;
1314 while (*str && nsvg__isdigit(*str)) str++;
1316 if (*str ==
'%') str++;
else break;
1317 while (*str && nsvg__isspace(*str)) str++;
1318 if (*str == delimiter[i]) str++;
1322 rgbi[0] =
roundf(rgbf[0] * 2.55f);
1323 rgbi[1] =
roundf(rgbf[1] * 2.55f);
1324 rgbi[2] =
roundf(rgbf[2] * 2.55f);
1327 rgbi[0] = rgbi[1] = rgbi[2] = 128;
1331 for (i = 0; i < 3; i++) {
1332 if (rgbi[i] > 255) rgbi[i] = 255;
1334 return NSVG_RGB(rgbi[0], rgbi[1], rgbi[2]);
1337typedef struct NSVGNamedColor {
1342NSVGNamedColor nsvg__colors[] = {
1344 {
"red", NSVG_RGB(255, 0, 0) },
1345 {
"green", NSVG_RGB(0, 128, 0) },
1346 {
"blue", NSVG_RGB(0, 0, 255) },
1347 {
"yellow", NSVG_RGB(255, 255, 0) },
1348 {
"cyan", NSVG_RGB(0, 255, 255) },
1349 {
"magenta", NSVG_RGB(255, 0, 255) },
1350 {
"black", NSVG_RGB(0, 0, 0) },
1351 {
"grey", NSVG_RGB(128, 128, 128) },
1352 {
"gray", NSVG_RGB(128, 128, 128) },
1353 {
"white", NSVG_RGB(255, 255, 255) },
1355#ifdef NANOSVG_ALL_COLOR_KEYWORDS
1356 {
"aliceblue", NSVG_RGB(240, 248, 255) },
1357 {
"antiquewhite", NSVG_RGB(250, 235, 215) },
1358 {
"aqua", NSVG_RGB(0, 255, 255) },
1359 {
"aquamarine", NSVG_RGB(127, 255, 212) },
1360 {
"azure", NSVG_RGB(240, 255, 255) },
1361 {
"beige", NSVG_RGB(245, 245, 220) },
1362 {
"bisque", NSVG_RGB(255, 228, 196) },
1363 {
"blanchedalmond", NSVG_RGB(255, 235, 205) },
1364 {
"blueviolet", NSVG_RGB(138, 43, 226) },
1365 {
"brown", NSVG_RGB(165, 42, 42) },
1366 {
"burlywood", NSVG_RGB(222, 184, 135) },
1367 {
"cadetblue", NSVG_RGB(95, 158, 160) },
1368 {
"chartreuse", NSVG_RGB(127, 255, 0) },
1369 {
"chocolate", NSVG_RGB(210, 105, 30) },
1370 {
"coral", NSVG_RGB(255, 127, 80) },
1371 {
"cornflowerblue", NSVG_RGB(100, 149, 237) },
1372 {
"cornsilk", NSVG_RGB(255, 248, 220) },
1373 {
"crimson", NSVG_RGB(220, 20, 60) },
1374 {
"darkblue", NSVG_RGB(0, 0, 139) },
1375 {
"darkcyan", NSVG_RGB(0, 139, 139) },
1376 {
"darkgoldenrod", NSVG_RGB(184, 134, 11) },
1377 {
"darkgray", NSVG_RGB(169, 169, 169) },
1378 {
"darkgreen", NSVG_RGB(0, 100, 0) },
1379 {
"darkgrey", NSVG_RGB(169, 169, 169) },
1380 {
"darkkhaki", NSVG_RGB(189, 183, 107) },
1381 {
"darkmagenta", NSVG_RGB(139, 0, 139) },
1382 {
"darkolivegreen", NSVG_RGB(85, 107, 47) },
1383 {
"darkorange", NSVG_RGB(255, 140, 0) },
1384 {
"darkorchid", NSVG_RGB(153, 50, 204) },
1385 {
"darkred", NSVG_RGB(139, 0, 0) },
1386 {
"darksalmon", NSVG_RGB(233, 150, 122) },
1387 {
"darkseagreen", NSVG_RGB(143, 188, 143) },
1388 {
"darkslateblue", NSVG_RGB(72, 61, 139) },
1389 {
"darkslategray", NSVG_RGB(47, 79, 79) },
1390 {
"darkslategrey", NSVG_RGB(47, 79, 79) },
1391 {
"darkturquoise", NSVG_RGB(0, 206, 209) },
1392 {
"darkviolet", NSVG_RGB(148, 0, 211) },
1393 {
"deeppink", NSVG_RGB(255, 20, 147) },
1394 {
"deepskyblue", NSVG_RGB(0, 191, 255) },
1395 {
"dimgray", NSVG_RGB(105, 105, 105) },
1396 {
"dimgrey", NSVG_RGB(105, 105, 105) },
1397 {
"dodgerblue", NSVG_RGB(30, 144, 255) },
1398 {
"firebrick", NSVG_RGB(178, 34, 34) },
1399 {
"floralwhite", NSVG_RGB(255, 250, 240) },
1400 {
"forestgreen", NSVG_RGB(34, 139, 34) },
1401 {
"fuchsia", NSVG_RGB(255, 0, 255) },
1402 {
"gainsboro", NSVG_RGB(220, 220, 220) },
1403 {
"ghostwhite", NSVG_RGB(248, 248, 255) },
1404 {
"gold", NSVG_RGB(255, 215, 0) },
1405 {
"goldenrod", NSVG_RGB(218, 165, 32) },
1406 {
"greenyellow", NSVG_RGB(173, 255, 47) },
1407 {
"honeydew", NSVG_RGB(240, 255, 240) },
1408 {
"hotpink", NSVG_RGB(255, 105, 180) },
1409 {
"indianred", NSVG_RGB(205, 92, 92) },
1410 {
"indigo", NSVG_RGB(75, 0, 130) },
1411 {
"ivory", NSVG_RGB(255, 255, 240) },
1412 {
"khaki", NSVG_RGB(240, 230, 140) },
1413 {
"lavender", NSVG_RGB(230, 230, 250) },
1414 {
"lavenderblush", NSVG_RGB(255, 240, 245) },
1415 {
"lawngreen", NSVG_RGB(124, 252, 0) },
1416 {
"lemonchiffon", NSVG_RGB(255, 250, 205) },
1417 {
"lightblue", NSVG_RGB(173, 216, 230) },
1418 {
"lightcoral", NSVG_RGB(240, 128, 128) },
1419 {
"lightcyan", NSVG_RGB(224, 255, 255) },
1420 {
"lightgoldenrodyellow", NSVG_RGB(250, 250, 210) },
1421 {
"lightgray", NSVG_RGB(211, 211, 211) },
1422 {
"lightgreen", NSVG_RGB(144, 238, 144) },
1423 {
"lightgrey", NSVG_RGB(211, 211, 211) },
1424 {
"lightpink", NSVG_RGB(255, 182, 193) },
1425 {
"lightsalmon", NSVG_RGB(255, 160, 122) },
1426 {
"lightseagreen", NSVG_RGB(32, 178, 170) },
1427 {
"lightskyblue", NSVG_RGB(135, 206, 250) },
1428 {
"lightslategray", NSVG_RGB(119, 136, 153) },
1429 {
"lightslategrey", NSVG_RGB(119, 136, 153) },
1430 {
"lightsteelblue", NSVG_RGB(176, 196, 222) },
1431 {
"lightyellow", NSVG_RGB(255, 255, 224) },
1432 {
"lime", NSVG_RGB(0, 255, 0) },
1433 {
"limegreen", NSVG_RGB(50, 205, 50) },
1434 {
"linen", NSVG_RGB(250, 240, 230) },
1435 {
"maroon", NSVG_RGB(128, 0, 0) },
1436 {
"mediumaquamarine", NSVG_RGB(102, 205, 170) },
1437 {
"mediumblue", NSVG_RGB(0, 0, 205) },
1438 {
"mediumorchid", NSVG_RGB(186, 85, 211) },
1439 {
"mediumpurple", NSVG_RGB(147, 112, 219) },
1440 {
"mediumseagreen", NSVG_RGB(60, 179, 113) },
1441 {
"mediumslateblue", NSVG_RGB(123, 104, 238) },
1442 {
"mediumspringgreen", NSVG_RGB(0, 250, 154) },
1443 {
"mediumturquoise", NSVG_RGB(72, 209, 204) },
1444 {
"mediumvioletred", NSVG_RGB(199, 21, 133) },
1445 {
"midnightblue", NSVG_RGB(25, 25, 112) },
1446 {
"mintcream", NSVG_RGB(245, 255, 250) },
1447 {
"mistyrose", NSVG_RGB(255, 228, 225) },
1448 {
"moccasin", NSVG_RGB(255, 228, 181) },
1449 {
"navajowhite", NSVG_RGB(255, 222, 173) },
1450 {
"navy", NSVG_RGB(0, 0, 128) },
1451 {
"oldlace", NSVG_RGB(253, 245, 230) },
1452 {
"olive", NSVG_RGB(128, 128, 0) },
1453 {
"olivedrab", NSVG_RGB(107, 142, 35) },
1454 {
"orange", NSVG_RGB(255, 165, 0) },
1455 {
"orangered", NSVG_RGB(255, 69, 0) },
1456 {
"orchid", NSVG_RGB(218, 112, 214) },
1457 {
"palegoldenrod", NSVG_RGB(238, 232, 170) },
1458 {
"palegreen", NSVG_RGB(152, 251, 152) },
1459 {
"paleturquoise", NSVG_RGB(175, 238, 238) },
1460 {
"palevioletred", NSVG_RGB(219, 112, 147) },
1461 {
"papayawhip", NSVG_RGB(255, 239, 213) },
1462 {
"peachpuff", NSVG_RGB(255, 218, 185) },
1463 {
"peru", NSVG_RGB(205, 133, 63) },
1464 {
"pink", NSVG_RGB(255, 192, 203) },
1465 {
"plum", NSVG_RGB(221, 160, 221) },
1466 {
"powderblue", NSVG_RGB(176, 224, 230) },
1467 {
"purple", NSVG_RGB(128, 0, 128) },
1468 {
"rosybrown", NSVG_RGB(188, 143, 143) },
1469 {
"royalblue", NSVG_RGB(65, 105, 225) },
1470 {
"saddlebrown", NSVG_RGB(139, 69, 19) },
1471 {
"salmon", NSVG_RGB(250, 128, 114) },
1472 {
"sandybrown", NSVG_RGB(244, 164, 96) },
1473 {
"seagreen", NSVG_RGB(46, 139, 87) },
1474 {
"seashell", NSVG_RGB(255, 245, 238) },
1475 {
"sienna", NSVG_RGB(160, 82, 45) },
1476 {
"silver", NSVG_RGB(192, 192, 192) },
1477 {
"skyblue", NSVG_RGB(135, 206, 235) },
1478 {
"slateblue", NSVG_RGB(106, 90, 205) },
1479 {
"slategray", NSVG_RGB(112, 128, 144) },
1480 {
"slategrey", NSVG_RGB(112, 128, 144) },
1481 {
"snow", NSVG_RGB(255, 250, 250) },
1482 {
"springgreen", NSVG_RGB(0, 255, 127) },
1483 {
"steelblue", NSVG_RGB(70, 130, 180) },
1484 {
"tan", NSVG_RGB(210, 180, 140) },
1485 {
"teal", NSVG_RGB(0, 128, 128) },
1486 {
"thistle", NSVG_RGB(216, 191, 216) },
1487 {
"tomato", NSVG_RGB(255, 99, 71) },
1488 {
"turquoise", NSVG_RGB(64, 224, 208) },
1489 {
"violet", NSVG_RGB(238, 130, 238) },
1490 {
"wheat", NSVG_RGB(245, 222, 179) },
1491 {
"whitesmoke", NSVG_RGB(245, 245, 245) },
1492 {
"yellowgreen", NSVG_RGB(154, 205, 50) },
1496static unsigned int nsvg__parseColorName(
const char* str)
1498 int i, ncolors =
sizeof(nsvg__colors) /
sizeof(NSVGNamedColor);
1500 for (i = 0; i < ncolors; i++) {
1501 if (
strcmp(nsvg__colors[i].name, str) == 0) {
1502 return nsvg__colors[i].color;
1506 return NSVG_RGB(128, 128, 128);
1509static unsigned int nsvg__parseColor(
const char* str)
1512 while (*str ==
' ') ++str;
1514 if (len >= 1 && *str ==
'#')
1515 return nsvg__parseColorHex(str);
1516 else if (len >= 4 && str[0] ==
'r' && str[1] ==
'g' && str[2] ==
'b' && str[3] ==
'(')
1517 return nsvg__parseColorRGB(str);
1518 return nsvg__parseColorName(str);
1521static float nsvg__parseOpacity(
const char* str)
1523 float val = nsvg__atof(str);
1524 if (val < 0.0f) val = 0.0f;
1525 if (val > 1.0f) val = 1.0f;
1529static float nsvg__parseMiterLimit(
const char* str)
1531 float val = nsvg__atof(str);
1532 if (val < 0.0f) val = 0.0f;
1536static int nsvg__parseUnits(
const char* units)
1538 if (units[0] ==
'p' && units[1] ==
'x')
1539 return NSVG_UNITS_PX;
1540 else if (units[0] ==
'p' && units[1] ==
't')
1541 return NSVG_UNITS_PT;
1542 else if (units[0] ==
'p' && units[1] ==
'c')
1543 return NSVG_UNITS_PC;
1544 else if (units[0] ==
'm' && units[1] ==
'm')
1545 return NSVG_UNITS_MM;
1546 else if (units[0] ==
'c' && units[1] ==
'm')
1547 return NSVG_UNITS_CM;
1548 else if (units[0] ==
'i' && units[1] ==
'n')
1549 return NSVG_UNITS_IN;
1550 else if (units[0] ==
'%')
1551 return NSVG_UNITS_PERCENT;
1552 else if (units[0] ==
'e' && units[1] ==
'm')
1553 return NSVG_UNITS_EM;
1554 else if (units[0] ==
'e' && units[1] ==
'x')
1555 return NSVG_UNITS_EX;
1556 return NSVG_UNITS_USER;
1559static int nsvg__isCoordinate(
const char* s)
1562 if (*s ==
'-' || *s ==
'+')
1565 return (nsvg__isdigit(*s) || *s ==
'.');
1568static NSVGcoordinate nsvg__parseCoordinateRaw(
const char* str)
1570 NSVGcoordinate coord = { 0, NSVG_UNITS_USER };
1572 coord.units = nsvg__parseUnits(nsvg__parseNumber(str, buf, 64));
1573 coord.value = nsvg__atof(buf);
1577static NSVGcoordinate nsvg__coord(
float v,
int units)
1579 NSVGcoordinate coord = { v, units };
1583static float nsvg__parseCoordinate(NSVGparser* p,
const char* str,
float orig,
float length)
1585 NSVGcoordinate coord = nsvg__parseCoordinateRaw(str);
1586 return nsvg__convertToPixels(p, coord, orig, length);
1589static int nsvg__parseTransformArgs(
const char* str,
float* args,
int maxNa,
int* na)
1597 while (*ptr && *ptr !=
'(') ++ptr;
1606 if (*ptr ==
'-' || *ptr ==
'+' || *ptr ==
'.' || nsvg__isdigit(*ptr)) {
1607 if (*na >= maxNa)
return 0;
1608 ptr = nsvg__parseNumber(ptr, it, 64);
1609 args[(*na)++] = (float)nsvg__atof(it);
1615 return (
int)(
end - str);
1619static int nsvg__parseMatrix(
float* xform,
const char* str)
1623 int len = nsvg__parseTransformArgs(str,
t, 6, &na);
1624 if (na != 6)
return len;
1625 memcpy(xform,
t,
sizeof(
float) * 6);
1629static int nsvg__parseTranslate(
float* xform,
const char* str)
1634 int len = nsvg__parseTransformArgs(str, args, 2, &na);
1635 if (na == 1) args[1] = 0.0;
1637 nsvg__xformSetTranslation(
t, args[0], args[1]);
1638 memcpy(xform,
t,
sizeof(
float) * 6);
1642static int nsvg__parseScale(
float* xform,
const char* str)
1647 int len = nsvg__parseTransformArgs(str, args, 2, &na);
1648 if (na == 1) args[1] = args[0];
1649 nsvg__xformSetScale(
t, args[0], args[1]);
1650 memcpy(xform,
t,
sizeof(
float) * 6);
1654static int nsvg__parseSkewX(
float* xform,
const char* str)
1659 int len = nsvg__parseTransformArgs(str, args, 1, &na);
1660 nsvg__xformSetSkewX(
t, args[0] / 180.0f * NSVG_PI);
1661 memcpy(xform,
t,
sizeof(
float) * 6);
1665static int nsvg__parseSkewY(
float* xform,
const char* str)
1670 int len = nsvg__parseTransformArgs(str, args, 1, &na);
1671 nsvg__xformSetSkewY(
t, args[0] / 180.0f * NSVG_PI);
1672 memcpy(xform,
t,
sizeof(
float) * 6);
1676static int nsvg__parseRotate(
float* xform,
const char* str)
1682 int len = nsvg__parseTransformArgs(str, args, 3, &na);
1684 args[1] = args[2] = 0.0f;
1685 nsvg__xformIdentity(m);
1688 nsvg__xformSetTranslation(
t, -args[1], -args[2]);
1689 nsvg__xformMultiply(m,
t);
1692 nsvg__xformSetRotation(
t, args[0] / 180.0f * NSVG_PI);
1693 nsvg__xformMultiply(m,
t);
1696 nsvg__xformSetTranslation(
t, args[1], args[2]);
1697 nsvg__xformMultiply(m,
t);
1700 memcpy(xform, m,
sizeof(
float) * 6);
1705static void nsvg__parseTransform(
float* xform,
const char* str)
1709 nsvg__xformIdentity(xform);
1712 if (
strncmp(str,
"matrix", 6) == 0)
1713 len = nsvg__parseMatrix(
t, str);
1714 else if (
strncmp(str,
"translate", 9) == 0)
1715 len = nsvg__parseTranslate(
t, str);
1716 else if (
strncmp(str,
"scale", 5) == 0)
1717 len = nsvg__parseScale(
t, str);
1718 else if (
strncmp(str,
"rotate", 6) == 0)
1719 len = nsvg__parseRotate(
t, str);
1720 else if (
strncmp(str,
"skewX", 5) == 0)
1721 len = nsvg__parseSkewX(
t, str);
1722 else if (
strncmp(str,
"skewY", 5) == 0)
1723 len = nsvg__parseSkewY(
t, str);
1736 nsvg__xformPremultiply(xform,
t);
1740static void nsvg__parseUrl(
char*
id,
const char* str)
1744 if (*str && *str ==
'#')
1746 while (i < 63 && *str && *str !=
')') {
1753static char nsvg__parseLineCap(
const char* str)
1755 if (
strcmp(str,
"butt") == 0)
1757 else if (
strcmp(str,
"round") == 0)
1759 else if (
strcmp(str,
"square") == 0)
1765static char nsvg__parseLineJoin(
const char* str)
1767 if (
strcmp(str,
"miter") == 0)
1769 else if (
strcmp(str,
"round") == 0)
1771 else if (
strcmp(str,
"bevel") == 0)
1777static char nsvg__parseFillRule(
const char* str)
1779 if (
strcmp(str,
"nonzero") == 0)
1781 else if (
strcmp(str,
"evenodd") == 0)
1787static unsigned char nsvg__parsePaintOrder(
const char* str)
1789 if (
strcmp(str,
"normal") == 0 ||
strcmp(str,
"fill stroke markers") == 0)
1791 else if (
strcmp(str,
"fill markers stroke") == 0)
1793 else if (
strcmp(str,
"markers fill stroke") == 0)
1795 else if (
strcmp(str,
"markers stroke fill") == 0)
1797 else if (
strcmp(str,
"stroke fill markers") == 0)
1799 else if (
strcmp(str,
"stroke markers fill") == 0)
1805static const char* nsvg__getNextDashItem(
const char* s,
char* it)
1810 while (*s && (nsvg__isspace(*s) || *s ==
',')) s++;
1812 while (*s && (!nsvg__isspace(*s) && *s !=
',')) {
1821static int nsvg__parseStrokeDashArray(NSVGparser* p,
const char* str,
float* strokeDashArray)
1833 str = nsvg__getNextDashItem(str, item);
1835 if (count < NSVG_MAX_DASHES)
1836 strokeDashArray[count++] =
fabsf(nsvg__parseCoordinate(p, item, 0.0f, nsvg__actualLength(p)));
1839 for (i = 0; i < count; i++)
1840 sum += strokeDashArray[i];
1847static void nsvg__parseStyle(NSVGparser* p,
const char* str);
1851static void nsvg__applyClassStyles(NSVGparser* p,
const char* value)
1853 const char* cur = value;
1855 const char* classStart;
1857 NSVGstyleDeclaration* style;
1859 while (*cur && nsvg__isspace(*cur))
1865 while (*cur && !nsvg__isspace(*cur))
1867 classLen = (
size_t)(cur - classStart);
1869 for (style = p->styles; style !=
NULL; style = style->next) {
1870 if (
strncmp(style->className, classStart, classLen) == 0 && style->className[classLen] ==
'\0') {
1871 nsvg__parseStyle(p, style->propertiesText);
1877static int nsvg__parseAttr(NSVGparser* p,
const char* name,
const char* value)
1880 NSVGattrib* attr = nsvg__getAttr(p);
1881 if (!attr)
return 0;
1883 if (
strcmp(name,
"style") == 0) {
1884 nsvg__parseStyle(p, value);
1886 else if (
strcmp(name,
"display") == 0) {
1887 if (
strcmp(value,
"none") == 0)
1892 else if (
strcmp(name,
"fill") == 0) {
1893 if (
strcmp(value,
"none") == 0) {
1896 else if (
strncmp(value,
"url(", 4) == 0) {
1898 nsvg__parseUrl(attr->fillGradient, value);
1902 attr->fillColor = nsvg__parseColor(value);
1905 else if (
strcmp(name,
"opacity") == 0) {
1906 attr->opacity = nsvg__parseOpacity(value);
1908 else if (
strcmp(name,
"fill-opacity") == 0) {
1909 attr->fillOpacity = nsvg__parseOpacity(value);
1911 else if (
strcmp(name,
"stroke") == 0) {
1912 if (
strcmp(value,
"none") == 0) {
1913 attr->hasStroke = 0;
1915 else if (
strncmp(value,
"url(", 4) == 0) {
1916 attr->hasStroke = 2;
1917 nsvg__parseUrl(attr->strokeGradient, value);
1920 attr->hasStroke = 1;
1921 attr->strokeColor = nsvg__parseColor(value);
1924 else if (
strcmp(name,
"stroke-width") == 0) {
1925 attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1927 else if (
strcmp(name,
"stroke-dasharray") == 0) {
1928 attr->strokeDashCount = nsvg__parseStrokeDashArray(p, value, attr->strokeDashArray);
1930 else if (
strcmp(name,
"stroke-dashoffset") == 0) {
1931 attr->strokeDashOffset = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1933 else if (
strcmp(name,
"stroke-opacity") == 0) {
1934 attr->strokeOpacity = nsvg__parseOpacity(value);
1936 else if (
strcmp(name,
"stroke-linecap") == 0) {
1937 attr->strokeLineCap = nsvg__parseLineCap(value);
1939 else if (
strcmp(name,
"stroke-linejoin") == 0) {
1940 attr->strokeLineJoin = nsvg__parseLineJoin(value);
1942 else if (
strcmp(name,
"stroke-miterlimit") == 0) {
1943 attr->miterLimit = nsvg__parseMiterLimit(value);
1945 else if (
strcmp(name,
"fill-rule") == 0) {
1946 attr->fillRule = nsvg__parseFillRule(value);
1948 else if (
strcmp(name,
"font-size") == 0) {
1949 attr->fontSize = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1951 else if (
strcmp(name,
"transform") == 0) {
1952 nsvg__parseTransform(xform, value);
1953 nsvg__xformPremultiply(attr->xform, xform);
1955 else if (
strcmp(name,
"stop-color") == 0) {
1956 attr->stopColor = nsvg__parseColor(value);
1958 else if (
strcmp(name,
"stop-opacity") == 0) {
1959 attr->stopOpacity = nsvg__parseOpacity(value);
1961 else if (
strcmp(name,
"offset") == 0) {
1962 attr->stopOffset = nsvg__parseCoordinate(p, value, 0.0f, 1.0f);
1964 else if (
strcmp(name,
"paint-order") == 0) {
1965 attr->paintOrder = nsvg__parsePaintOrder(value);
1967 else if (
strcmp(name,
"id") == 0) {
1969 attr->id[63] =
'\0';
1971 else if (
strcmp(name,
"class") == 0) {
1972 nsvg__applyClassStyles(p, value);
1980static int nsvg__parseNameValue(NSVGparser* p,
const char* start,
const char*
end)
1989 while (str <
end && *str !=
':') ++str;
1994 while (str > start && (*str ==
':' || nsvg__isspace(*str))) --str;
1997 n = (int)(str - start);
1998 if (n > 511) n = 511;
1999 if (n)
memcpy(name, (
void*)start, n);
2002 while (val <
end && (*val ==
':' || nsvg__isspace(*val))) ++val;
2004 n = (int)(
end - val);
2005 if (n > 511) n = 511;
2006 if (n)
memcpy(value, (
void*)val, n);
2009 return nsvg__parseAttr(p, name, value);
2012static void nsvg__parseStyle(NSVGparser* p,
const char* str)
2019 while (*str && nsvg__isspace(*str)) ++str;
2021 while (*str && *str !=
';') ++str;
2025 while (
end > start && (*
end ==
';' || nsvg__isspace(*
end))) --
end;
2028 nsvg__parseNameValue(p, start,
end);
2033static void nsvg__parseAttribs(NSVGparser* p,
const char** attr)
2036 for (i = 0; attr[i]; i += 2)
2038 if (
strcmp(attr[i],
"style") == 0)
2039 nsvg__parseStyle(p, attr[i + 1]);
2041 nsvg__parseAttr(p, attr[i], attr[i + 1]);
2045static int nsvg__getArgsPerElement(
char cmd)
2078static void nsvg__pathMoveTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
2088 nsvg__moveTo(p, *cpx, *cpy);
2091static void nsvg__pathLineTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
2101 nsvg__lineTo(p, *cpx, *cpy);
2104static void nsvg__pathHLineTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
2110 nsvg__lineTo(p, *cpx, *cpy);
2113static void nsvg__pathVLineTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
2119 nsvg__lineTo(p, *cpx, *cpy);
2122static void nsvg__pathCubicBezTo(NSVGparser* p,
float* cpx,
float* cpy,
2123 float* cpx2,
float* cpy2,
float* args,
int rel)
2125 float x2, y2, cx1, cy1, cx2, cy2;
2128 cx1 = *cpx + args[0];
2129 cy1 = *cpy + args[1];
2130 cx2 = *cpx + args[2];
2131 cy2 = *cpy + args[3];
2132 x2 = *cpx + args[4];
2133 y2 = *cpy + args[5];
2144 nsvg__cubicBezTo(p, cx1, cy1, cx2, cy2, x2, y2);
2152static void nsvg__pathCubicBezShortTo(NSVGparser* p,
float* cpx,
float* cpy,
2153 float* cpx2,
float* cpy2,
float* args,
int rel)
2155 float x1, y1, x2, y2, cx1, cy1, cx2, cy2;
2160 cx2 = *cpx + args[0];
2161 cy2 = *cpy + args[1];
2162 x2 = *cpx + args[2];
2163 y2 = *cpy + args[3];
2172 cx1 = 2 * x1 - *cpx2;
2173 cy1 = 2 * y1 - *cpy2;
2175 nsvg__cubicBezTo(p, cx1, cy1, cx2, cy2, x2, y2);
2183static void nsvg__pathQuadBezTo(NSVGparser* p,
float* cpx,
float* cpy,
2184 float* cpx2,
float* cpy2,
float* args,
int rel)
2186 float x1, y1, x2, y2, cx, cy;
2187 float cx1, cy1, cx2, cy2;
2192 cx = *cpx + args[0];
2193 cy = *cpy + args[1];
2194 x2 = *cpx + args[2];
2195 y2 = *cpy + args[3];
2205 cx1 = x1 + 2.0f / 3.0f * (cx - x1);
2206 cy1 = y1 + 2.0f / 3.0f * (cy - y1);
2207 cx2 = x2 + 2.0f / 3.0f * (cx - x2);
2208 cy2 = y2 + 2.0f / 3.0f * (cy - y2);
2210 nsvg__cubicBezTo(p, cx1, cy1, cx2, cy2, x2, y2);
2218static void nsvg__pathQuadBezShortTo(NSVGparser* p,
float* cpx,
float* cpy,
2219 float* cpx2,
float* cpy2,
float* args,
int rel)
2221 float x1, y1, x2, y2, cx, cy;
2222 float cx1, cy1, cx2, cy2;
2227 x2 = *cpx + args[0];
2228 y2 = *cpy + args[1];
2235 cx = 2 * x1 - *cpx2;
2236 cy = 2 * y1 - *cpy2;
2239 cx1 = x1 + 2.0f / 3.0f * (cx - x1);
2240 cy1 = y1 + 2.0f / 3.0f * (cy - y1);
2241 cx2 = x2 + 2.0f / 3.0f * (cx - x2);
2242 cy2 = y2 + 2.0f / 3.0f * (cy - y2);
2244 nsvg__cubicBezTo(p, cx1, cy1, cx2, cy2, x2, y2);
2252static float nsvg__sqr(
float x) {
return x *
x; }
2253static float nsvg__vmag(
float x,
float y) {
return sqrtf(x * x + y * y); }
2255static float nsvg__vecrat(
float ux,
float uy,
float vx,
float vy)
2257 return (ux * vx + uy * vy) / (nsvg__vmag(ux, uy) * nsvg__vmag(vx, vy));
2260static float nsvg__vecang(
float ux,
float uy,
float vx,
float vy)
2262 float r = nsvg__vecrat(ux, uy, vx, vy);
2263 if (r < -1.0f) r = -1.0f;
2264 if (r > 1.0f) r = 1.0f;
2265 return ((ux * vy < uy* vx) ? -1.0f : 1.0f) *
acosf(r);
2268static void nsvg__pathArcTo(NSVGparser* p,
float* cpx,
float* cpy,
float* args,
int rel)
2272 float x1, y1, x2, y2, cx, cy, dx, dy, d;
2273 float x1p, y1p, cxp, cyp, s, sa, sb;
2274 float ux, uy, vx, vy, a1, da;
2275 float x, y, tanx, tany, a, px = 0, py = 0, ptanx = 0, ptany = 0,
t[6];
2281 rx =
fabsf(args[0]);
2282 ry =
fabsf(args[1]);
2283 rotx = args[2] / 180.0f * NSVG_PI;
2284 fa =
fabsf(args[3]) > 1e-6 ? 1 : 0;
2285 fs =
fabsf(args[4]) > 1e-6 ? 1 : 0;
2289 x2 = *cpx + args[5];
2290 y2 = *cpy + args[6];
2299 d =
sqrtf(dx * dx + dy * dy);
2300 if (d < 1e-6f || rx < 1e-6f || ry < 1e-6f) {
2302 nsvg__lineTo(p, x2, y2);
2314 x1p = cosrx * dx / 2.0f + sinrx * dy / 2.0f;
2315 y1p = -sinrx * dx / 2.0f + cosrx * dy / 2.0f;
2316 d = nsvg__sqr(x1p) / nsvg__sqr(rx) + nsvg__sqr(y1p) / nsvg__sqr(ry);
2324 sa = nsvg__sqr(rx) * nsvg__sqr(ry) - nsvg__sqr(rx) * nsvg__sqr(y1p) - nsvg__sqr(ry) * nsvg__sqr(x1p);
2325 sb = nsvg__sqr(rx) * nsvg__sqr(y1p) + nsvg__sqr(ry) * nsvg__sqr(x1p);
2326 if (sa < 0.0f) sa = 0.0f;
2331 cxp = s * rx * y1p / ry;
2332 cyp = s * -ry * x1p / rx;
2335 cx = (x1 + x2) / 2.0f + cosrx * cxp - sinrx * cyp;
2336 cy = (y1 + y2) / 2.0f + sinrx * cxp + cosrx * cyp;
2339 ux = (x1p - cxp) / rx;
2340 uy = (y1p - cyp) / ry;
2341 vx = (-x1p - cxp) / rx;
2342 vy = (-y1p - cyp) / ry;
2343 a1 = nsvg__vecang(1.0f, 0.0f, ux, uy);
2344 da = nsvg__vecang(ux, uy, vx, vy);
2349 if (fs == 0 && da > 0)
2351 else if (fs == 1 && da < 0)
2355 t[0] = cosrx;
t[1] = sinrx;
2356 t[2] = -sinrx;
t[3] = cosrx;
2357 t[4] = cx;
t[5] = cy;
2361 ndivs = (int)(
fabsf(da) / (NSVG_PI * 0.5f) + 1.0f);
2362 hda = (da / (float)ndivs) / 2.0f;
2364 if ((hda < 1e-3f) && (hda > -1e-3f))
2367 hda = (1.0f -
cosf(hda)) /
sinf(hda);
2368 kappa =
fabsf(4.0f / 3.0f * hda);
2372 for (i = 0; i <= ndivs; i++) {
2373 a = a1 + da * ((float)i / (
float)ndivs);
2376 nsvg__xformPoint(&x, &y, dx * rx, dy * ry,
t);
2377 nsvg__xformVec(&tanx, &tany, -dy * rx * kappa, dx * ry * kappa,
t);
2379 nsvg__cubicBezTo(p, px + ptanx, py + ptany, x - tanx, y - tany, x, y);
2390static void nsvg__parsePath(NSVGparser* p,
const char** attr)
2392 const char* s =
NULL;
2398 float cpx, cpy, cpx2, cpy2;
2404 for (i = 0; attr[i]; i += 2) {
2405 if (
strcmp(attr[i],
"d") == 0) {
2410 tmp[1] = attr[i + 1];
2413 nsvg__parseAttribs(p, tmp);
2427 if ((cmd ==
'A' || cmd ==
'a') && (nargs == 3 || nargs == 4))
2428 s = nsvg__getNextPathItemWhenArcFlag(s, item);
2430 s = nsvg__getNextPathItem(s, item);
2432 if (cmd !=
'\0' && nsvg__isCoordinate(item)) {
2434 args[nargs++] = (float)nsvg__atof(item);
2435 if (nargs >= rargs) {
2439 nsvg__pathMoveTo(p, &cpx, &cpy, args, cmd ==
'm' ? 1 : 0);
2442 cmd = (cmd ==
'm') ?
'l' :
'L';
2443 rargs = nsvg__getArgsPerElement(cmd);
2444 cpx2 = cpx; cpy2 = cpy;
2449 nsvg__pathLineTo(p, &cpx, &cpy, args, cmd ==
'l' ? 1 : 0);
2450 cpx2 = cpx; cpy2 = cpy;
2454 nsvg__pathHLineTo(p, &cpx, &cpy, args, cmd ==
'h' ? 1 : 0);
2455 cpx2 = cpx; cpy2 = cpy;
2459 nsvg__pathVLineTo(p, &cpx, &cpy, args, cmd ==
'v' ? 1 : 0);
2460 cpx2 = cpx; cpy2 = cpy;
2464 nsvg__pathCubicBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd ==
'c' ? 1 : 0);
2468 nsvg__pathCubicBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd ==
's' ? 1 : 0);
2472 nsvg__pathQuadBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd ==
'q' ? 1 : 0);
2476 nsvg__pathQuadBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd ==
't' ? 1 : 0);
2480 nsvg__pathArcTo(p, &cpx, &cpy, args, cmd ==
'a' ? 1 : 0);
2481 cpx2 = cpx; cpy2 = cpy;
2485 cpx = args[nargs - 2];
2486 cpy = args[nargs - 1];
2487 cpx2 = cpx; cpy2 = cpy;
2496 if (cmd ==
'M' || cmd ==
'm') {
2499 nsvg__addPath(p, closedFlag);
2505 else if (initPoint == 0) {
2509 if (cmd ==
'Z' || cmd ==
'z') {
2516 cpx2 = cpx; cpy2 = cpy;
2517 nsvg__addPath(p, closedFlag);
2521 nsvg__moveTo(p, cpx, cpy);
2525 rargs = nsvg__getArgsPerElement(cmd);
2535 nsvg__addPath(p, closedFlag);
2541static void nsvg__parseRect(NSVGparser* p,
const char** attr)
2551 for (i = 0; attr[i]; i += 2) {
2552 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2553 if (
strcmp(attr[i],
"x") == 0)
x = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2554 if (
strcmp(attr[i],
"y") == 0) y = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2555 if (
strcmp(attr[i],
"width") == 0) w = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, nsvg__actualWidth(p));
2556 if (
strcmp(attr[i],
"height") == 0) h = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, nsvg__actualHeight(p));
2557 if (
strcmp(attr[i],
"rx") == 0) rx =
fabsf(nsvg__parseCoordinate(p, attr[i + 1], 0.0f, nsvg__actualWidth(p)));
2558 if (
strcmp(attr[i],
"ry") == 0) ry =
fabsf(nsvg__parseCoordinate(p, attr[i + 1], 0.0f, nsvg__actualHeight(p)));
2562 if (rx < 0.0f && ry > 0.0f) rx = ry;
2563 if (ry < 0.0f && rx > 0.0f) ry = rx;
2564 if (rx < 0.0f) rx = 0.0f;
2565 if (ry < 0.0f) ry = 0.0f;
2566 if (rx > w / 2.0f) rx = w / 2.0f;
2567 if (ry > h / 2.0f) ry = h / 2.0f;
2569 if (w != 0.0f && h != 0.0f) {
2572 if (rx < 0.00001f || ry < 0.0001f) {
2573 nsvg__moveTo(p, x, y);
2574 nsvg__lineTo(p, x + w, y);
2575 nsvg__lineTo(p, x + w, y + h);
2576 nsvg__lineTo(p, x, y + h);
2580 nsvg__moveTo(p, x + rx, y);
2581 nsvg__lineTo(p, x + w - rx, y);
2582 nsvg__cubicBezTo(p, x + w - rx * (1 - NSVG_KAPPA90), y, x + w, y + ry * (1 - NSVG_KAPPA90), x + w, y + ry);
2583 nsvg__lineTo(p, x + w, y + h - ry);
2584 nsvg__cubicBezTo(p, x + w, y + h - ry * (1 - NSVG_KAPPA90), x + w - rx * (1 - NSVG_KAPPA90), y + h, x + w - rx, y + h);
2585 nsvg__lineTo(p, x + rx, y + h);
2586 nsvg__cubicBezTo(p, x + rx * (1 - NSVG_KAPPA90), y + h, x, y + h - ry * (1 - NSVG_KAPPA90), x, y + h - ry);
2587 nsvg__lineTo(p, x, y + ry);
2588 nsvg__cubicBezTo(p, x, y + ry * (1 - NSVG_KAPPA90), x + rx * (1 - NSVG_KAPPA90), y, x + rx, y);
2591 nsvg__addPath(p, 1);
2597static void nsvg__parseCircle(NSVGparser* p,
const char** attr)
2604 for (i = 0; attr[i]; i += 2) {
2605 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2606 if (
strcmp(attr[i],
"cx") == 0) cx = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2607 if (
strcmp(attr[i],
"cy") == 0) cy = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2608 if (
strcmp(attr[i],
"r") == 0) r =
fabsf(nsvg__parseCoordinate(p, attr[i + 1], 0.0f, nsvg__actualLength(p)));
2615 nsvg__moveTo(p, cx + r, cy);
2616 nsvg__cubicBezTo(p, cx + r, cy + r * NSVG_KAPPA90, cx + r * NSVG_KAPPA90, cy + r, cx, cy + r);
2617 nsvg__cubicBezTo(p, cx - r * NSVG_KAPPA90, cy + r, cx - r, cy + r * NSVG_KAPPA90, cx - r, cy);
2618 nsvg__cubicBezTo(p, cx - r, cy - r * NSVG_KAPPA90, cx - r * NSVG_KAPPA90, cy - r, cx, cy - r);
2619 nsvg__cubicBezTo(p, cx + r * NSVG_KAPPA90, cy - r, cx + r, cy - r * NSVG_KAPPA90, cx + r, cy);
2621 nsvg__addPath(p, 1);
2627static void nsvg__parseEllipse(NSVGparser* p,
const char** attr)
2635 for (i = 0; attr[i]; i += 2) {
2636 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2637 if (
strcmp(attr[i],
"cx") == 0) cx = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2638 if (
strcmp(attr[i],
"cy") == 0) cy = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2639 if (
strcmp(attr[i],
"rx") == 0) rx =
fabsf(nsvg__parseCoordinate(p, attr[i + 1], 0.0f, nsvg__actualWidth(p)));
2640 if (
strcmp(attr[i],
"ry") == 0) ry =
fabsf(nsvg__parseCoordinate(p, attr[i + 1], 0.0f, nsvg__actualHeight(p)));
2644 if (rx > 0.0f && ry > 0.0f) {
2648 nsvg__moveTo(p, cx + rx, cy);
2649 nsvg__cubicBezTo(p, cx + rx, cy + ry * NSVG_KAPPA90, cx + rx * NSVG_KAPPA90, cy + ry, cx, cy + ry);
2650 nsvg__cubicBezTo(p, cx - rx * NSVG_KAPPA90, cy + ry, cx - rx, cy + ry * NSVG_KAPPA90, cx - rx, cy);
2651 nsvg__cubicBezTo(p, cx - rx, cy - ry * NSVG_KAPPA90, cx - rx * NSVG_KAPPA90, cy - ry, cx, cy - ry);
2652 nsvg__cubicBezTo(p, cx + rx * NSVG_KAPPA90, cy - ry, cx + rx, cy - ry * NSVG_KAPPA90, cx + rx, cy);
2654 nsvg__addPath(p, 1);
2660static void nsvg__parseLine(NSVGparser* p,
const char** attr)
2668 for (i = 0; attr[i]; i += 2) {
2669 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2670 if (
strcmp(attr[i],
"x1") == 0) x1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2671 if (
strcmp(attr[i],
"y1") == 0) y1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2672 if (
strcmp(attr[i],
"x2") == 0) x2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2673 if (
strcmp(attr[i],
"y2") == 0) y2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2679 nsvg__moveTo(p, x1, y1);
2680 nsvg__lineTo(p, x2, y2);
2682 nsvg__addPath(p, 0);
2687static void nsvg__parsePoly(NSVGparser* p,
const char** attr,
int closeFlag)
2692 int nargs, npts = 0;
2697 for (i = 0; attr[i]; i += 2) {
2698 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2699 if (
strcmp(attr[i],
"points") == 0) {
2703 s = nsvg__getNextPathItem(s, item);
2704 args[nargs++] = (float)nsvg__atof(item);
2707 nsvg__moveTo(p, args[0], args[1]);
2709 nsvg__lineTo(p, args[0], args[1]);
2718 nsvg__addPath(p, (
char)closeFlag);
2723static void nsvg__parseSVG(NSVGparser* p,
const char** attr)
2726 for (i = 0; attr[i]; i += 2) {
2727 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2728 if (
strcmp(attr[i],
"width") == 0) {
2729 p->image->width = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
2731 else if (
strcmp(attr[i],
"height") == 0) {
2732 p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
2734 else if (
strcmp(attr[i],
"viewBox") == 0) {
2735 const char* s = attr[i + 1];
2737 s = nsvg__parseNumber(s, buf, 64);
2738 p->viewMinx = nsvg__atof(buf);
2739 while (*s && (nsvg__isspace(*s) || *s ==
'%' || *s ==
',')) s++;
2741 s = nsvg__parseNumber(s, buf, 64);
2742 p->viewMiny = nsvg__atof(buf);
2743 while (*s && (nsvg__isspace(*s) || *s ==
'%' || *s ==
',')) s++;
2745 s = nsvg__parseNumber(s, buf, 64);
2746 p->viewWidth = nsvg__atof(buf);
2747 while (*s && (nsvg__isspace(*s) || *s ==
'%' || *s ==
',')) s++;
2749 s = nsvg__parseNumber(s, buf, 64);
2750 p->viewHeight = nsvg__atof(buf);
2752 else if (
strcmp(attr[i],
"preserveAspectRatio") == 0) {
2753 if (
strstr(attr[i + 1],
"none") != 0) {
2755 p->alignType = NSVG_ALIGN_NONE;
2759 if (
strstr(attr[i + 1],
"xMin") != 0)
2760 p->alignX = NSVG_ALIGN_MIN;
2761 else if (
strstr(attr[i + 1],
"xMid") != 0)
2762 p->alignX = NSVG_ALIGN_MID;
2763 else if (
strstr(attr[i + 1],
"xMax") != 0)
2764 p->alignX = NSVG_ALIGN_MAX;
2766 if (
strstr(attr[i + 1],
"yMin") != 0)
2767 p->alignY = NSVG_ALIGN_MIN;
2768 else if (
strstr(attr[i + 1],
"yMid") != 0)
2769 p->alignY = NSVG_ALIGN_MID;
2770 else if (
strstr(attr[i + 1],
"yMax") != 0)
2771 p->alignY = NSVG_ALIGN_MAX;
2773 p->alignType = NSVG_ALIGN_MEET;
2774 if (
strstr(attr[i + 1],
"slice") != 0)
2775 p->alignType = NSVG_ALIGN_SLICE;
2782static void nsvg__parseGradient(NSVGparser* p,
const char** attr,
signed char type)
2785 NSVGgradientData* grad = (NSVGgradientData*)
malloc(
sizeof(NSVGgradientData));
2786 if (grad ==
NULL)
return;
2787 memset(grad, 0,
sizeof(NSVGgradientData));
2788 grad->units = NSVG_OBJECT_SPACE;
2791 grad->linear.x1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2792 grad->linear.y1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2793 grad->linear.x2 = nsvg__coord(100.0f, NSVG_UNITS_PERCENT);
2794 grad->linear.y2 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2797 grad->radial.cx = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2798 grad->radial.cy = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2799 grad->radial.r = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2802 nsvg__xformIdentity(grad->xform);
2804 for (i = 0; attr[i]; i += 2) {
2805 if (
strcmp(attr[i],
"id") == 0) {
2806 strncpy(grad->id, attr[i + 1], 63);
2807 grad->id[63] =
'\0';
2809 else if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2810 if (
strcmp(attr[i],
"gradientUnits") == 0) {
2811 if (
strcmp(attr[i + 1],
"objectBoundingBox") == 0)
2812 grad->units = NSVG_OBJECT_SPACE;
2814 grad->units = NSVG_USER_SPACE;
2816 else if (
strcmp(attr[i],
"gradientTransform") == 0) {
2817 nsvg__parseTransform(grad->xform, attr[i + 1]);
2819 else if (
strcmp(attr[i],
"cx") == 0) {
2820 grad->radial.cx = nsvg__parseCoordinateRaw(attr[i + 1]);
2822 else if (
strcmp(attr[i],
"cy") == 0) {
2823 grad->radial.cy = nsvg__parseCoordinateRaw(attr[i + 1]);
2825 else if (
strcmp(attr[i],
"r") == 0) {
2826 grad->radial.r = nsvg__parseCoordinateRaw(attr[i + 1]);
2828 else if (
strcmp(attr[i],
"fx") == 0) {
2829 grad->radial.fx = nsvg__parseCoordinateRaw(attr[i + 1]);
2831 else if (
strcmp(attr[i],
"fy") == 0) {
2832 grad->radial.fy = nsvg__parseCoordinateRaw(attr[i + 1]);
2834 else if (
strcmp(attr[i],
"x1") == 0) {
2835 grad->linear.x1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2837 else if (
strcmp(attr[i],
"y1") == 0) {
2838 grad->linear.y1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2840 else if (
strcmp(attr[i],
"x2") == 0) {
2841 grad->linear.x2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2843 else if (
strcmp(attr[i],
"y2") == 0) {
2844 grad->linear.y2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2846 else if (
strcmp(attr[i],
"spreadMethod") == 0) {
2847 if (
strcmp(attr[i + 1],
"pad") == 0)
2849 else if (
strcmp(attr[i + 1],
"reflect") == 0)
2851 else if (
strcmp(attr[i + 1],
"repeat") == 0)
2854 else if (
strcmp(attr[i],
"xlink:href") == 0) {
2855 const char* href = attr[i + 1];
2856 strncpy(grad->ref, href + 1, 62);
2857 grad->ref[62] =
'\0';
2862 grad->next = p->gradients;
2863 p->gradients = grad;
2866static void nsvg__parseGradientStop(NSVGparser* p,
const char** attr)
2868 NSVGattrib* curAttr = nsvg__getAttr(p);
2869 NSVGgradientData* grad;
2873 curAttr->stopOffset = 0;
2874 curAttr->stopColor = 0;
2875 curAttr->stopOpacity = 1.0f;
2877 for (i = 0; attr[i]; i += 2) {
2878 nsvg__parseAttr(p, attr[i], attr[i + 1]);
2882 grad = p->gradients;
2883 if (grad ==
NULL)
return;
2887 if (grad->stops ==
NULL)
return;
2890 idx = grad->nstops - 1;
2891 for (i = 0; i < grad->nstops - 1; i++) {
2892 if (curAttr->stopOffset < grad->stops[i].offset) {
2897 if (idx != grad->nstops - 1) {
2898 for (i = grad->nstops - 1; i > idx; i--)
2899 grad->stops[i] = grad->stops[i - 1];
2902 stop = &grad->stops[idx];
2903 stop->
color = curAttr->stopColor;
2904 stop->
color |= (
unsigned int)(curAttr->stopOpacity * 255) << 24;
2905 stop->
offset = curAttr->stopOffset;
2908static void nsvg__startElement(
void* ud,
const char* el,
const char** attr)
2910 NSVGparser* p = (NSVGparser*)ud;
2914 if (
strcmp(el,
"linearGradient") == 0) {
2917 else if (
strcmp(el,
"radialGradient") == 0) {
2920 else if (
strcmp(el,
"stop") == 0) {
2921 nsvg__parseGradientStop(p, attr);
2923 else if (
strcmp(el,
"style") == 0) {
2929 if (
strcmp(el,
"g") == 0) {
2931 nsvg__parseAttribs(p, attr);
2933 else if (
strcmp(el,
"path") == 0) {
2937 nsvg__parsePath(p, attr);
2940 else if (
strcmp(el,
"rect") == 0) {
2942 nsvg__parseRect(p, attr);
2945 else if (
strcmp(el,
"circle") == 0) {
2947 nsvg__parseCircle(p, attr);
2950 else if (
strcmp(el,
"ellipse") == 0) {
2952 nsvg__parseEllipse(p, attr);
2955 else if (
strcmp(el,
"line") == 0) {
2957 nsvg__parseLine(p, attr);
2960 else if (
strcmp(el,
"polyline") == 0) {
2962 nsvg__parsePoly(p, attr, 0);
2965 else if (
strcmp(el,
"polygon") == 0) {
2967 nsvg__parsePoly(p, attr, 1);
2970 else if (
strcmp(el,
"linearGradient") == 0) {
2973 else if (
strcmp(el,
"radialGradient") == 0) {
2976 else if (
strcmp(el,
"stop") == 0) {
2977 nsvg__parseGradientStop(p, attr);
2979 else if (
strcmp(el,
"defs") == 0) {
2982 else if (
strcmp(el,
"svg") == 0) {
2983 nsvg__parseSVG(p, attr);
2985 else if (
strcmp(el,
"style") == 0) {
2990static void nsvg__endElement(
void* ud,
const char* el)
2992 NSVGparser* p = (NSVGparser*)ud;
2994 if (
strcmp(el,
"g") == 0) {
2997 else if (
strcmp(el,
"path") == 0) {
3000 else if (
strcmp(el,
"defs") == 0) {
3003 else if (
strcmp(el,
"style") == 0) {
3008static char* nsvg__strndup(
const char* s,
size_t n)
3010 char* result = (
char*)
malloc(n + 1);
3014 memcpy(result,(
void*) s, n);
3019static void nsvg__content(
void* ud,
const char* s)
3021 NSVGparser* p = (NSVGparser*)ud;
3028 NSVGstyleDeclaration* styles[NSVG_MAX_CLASSES];
3030 const char* propsStart;
3031 const char* propsEnd;
3038 while (*s && *s !=
'{') {
3039 const char* selStart;
3041 NSVGstyleDeclaration* style;
3043 while (*s && (nsvg__isspace(*s) || *s ==
','))
3045 if (!*s || *s ==
'{')
3049 while (*s && !nsvg__isspace(*s) && *s !=
',' && *s !=
'{')
3053 if (*selStart !=
'.' || nstyles >= NSVG_MAX_CLASSES)
3057 style = (NSVGstyleDeclaration*)
malloc(
sizeof(NSVGstyleDeclaration));
3060 style->className = nsvg__strndup(selStart, (
size_t)(selEnd - selStart));
3061 if (style->className ==
NULL) {
3065 style->propertiesText =
NULL;
3067 styles[nstyles++] = style;
3071 for (i = 0; i < nstyles; i++) {
3072 free(styles[i]->className);
3081 while (*s && *s !=
'}')
3087 for (i = 0; i < nstyles; i++) {
3088 styles[i]->propertiesText = nsvg__strndup(propsStart, (
size_t)(propsEnd - propsStart));
3089 if (styles[i]->propertiesText ==
NULL) {
3090 free(styles[i]->className);
3094 styles[i]->next = p->styles;
3095 p->styles = styles[i];
3104static void nsvg__imageBounds(NSVGparser* p,
float* bounds)
3107 shape = p->image->shapes;
3108 if (shape ==
NULL) {
3109 bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0;
3112 bounds[0] = shape->
bounds[0];
3113 bounds[1] = shape->
bounds[1];
3114 bounds[2] = shape->
bounds[2];
3115 bounds[3] = shape->
bounds[3];
3116 for (shape = shape->
next; shape !=
NULL; shape = shape->
next) {
3117 bounds[0] = nsvg__minf(bounds[0], shape->
bounds[0]);
3118 bounds[1] = nsvg__minf(bounds[1], shape->
bounds[1]);
3119 bounds[2] = nsvg__maxf(bounds[2], shape->
bounds[2]);
3120 bounds[3] = nsvg__maxf(bounds[3], shape->
bounds[3]);
3124static float nsvg__viewAlign(
float content,
float container,
int type)
3126 if (type == NSVG_ALIGN_MIN)
3128 else if (type == NSVG_ALIGN_MAX)
3129 return container - content;
3131 return (container - content) * 0.5f;
3134static void nsvg__scaleGradient(
NSVGgradient* grad,
float tx,
float ty,
float sx,
float sy)
3137 nsvg__xformSetTranslation(
t, tx, ty);
3138 nsvg__xformMultiply(grad->
xform,
t);
3140 nsvg__xformSetScale(
t, sx, sy);
3141 nsvg__xformMultiply(grad->
xform,
t);
3144static void nsvg__scaleToViewbox(NSVGparser* p,
const char* units)
3148 float tx, ty, sx, sy, us, bounds[4],
t[6], avgs;
3153 nsvg__imageBounds(p, bounds);
3155 if (p->viewWidth == 0) {
3156 if (p->image->width > 0) {
3157 p->viewWidth = p->image->width;
3160 p->viewMinx = bounds[0];
3161 p->viewWidth = bounds[2] - bounds[0];
3164 if (p->viewHeight == 0) {
3165 if (p->image->height > 0) {
3166 p->viewHeight = p->image->height;
3169 p->viewMiny = bounds[1];
3170 p->viewHeight = bounds[3] - bounds[1];
3173 if (p->image->width == 0)
3174 p->image->width = p->viewWidth;
3175 if (p->image->height == 0)
3176 p->image->height = p->viewHeight;
3180 sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0;
3181 sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0;
3183 us = 1.0f / nsvg__convertToPixels(p, nsvg__coord(1.0f, nsvg__parseUnits(units)), 0.0f, 1.0f);
3186 if (p->alignType == NSVG_ALIGN_MEET) {
3188 sx = sy = nsvg__minf(sx, sy);
3189 tx += nsvg__viewAlign(p->viewWidth * sx, p->image->width, p->alignX) / sx;
3190 ty += nsvg__viewAlign(p->viewHeight * sy, p->image->height, p->alignY) / sy;
3192 else if (p->alignType == NSVG_ALIGN_SLICE) {
3194 sx = sy = nsvg__maxf(sx, sy);
3195 tx += nsvg__viewAlign(p->viewWidth * sx, p->image->width, p->alignX) / sx;
3196 ty += nsvg__viewAlign(p->viewHeight * sy, p->image->height, p->alignY) / sy;
3202 avgs = (sx + sy) / 2.0f;
3203 for (shape = p->image->shapes; shape !=
NULL; shape = shape->
next) {
3209 path->bounds[0] = (
path->bounds[0] + tx) * sx;
3210 path->bounds[1] = (
path->bounds[1] + ty) * sy;
3211 path->bounds[2] = (
path->bounds[2] + tx) * sx;
3212 path->bounds[3] = (
path->bounds[3] + ty) * sy;
3213 for (i = 0; i <
path->npts; i++) {
3214 pt = &
path->pts[i * 2];
3215 pt[0] = (pt[0] + tx) * sx;
3216 pt[1] = (pt[1] + ty) * sy;
3221 nsvg__scaleGradient(shape->
fill.
gradient, tx, ty, sx, sy);
3238static void nsvg__createGradients(NSVGparser* p)
3242 for (shape = p->image->shapes; shape !=
NULL; shape = shape->
next) {
3245 float inv[6], localBounds[4];
3246 nsvg__xformInverse(inv, shape->
xform);
3247 nsvg__getLocalBounds(localBounds, shape, inv);
3256 float inv[6], localBounds[4];
3257 nsvg__xformInverse(inv, shape->
xform);
3258 nsvg__getLocalBounds(localBounds, shape, inv);
3273 p = nsvg__createParser();
3279 nsvg__parseXML(
input, nsvg__startElement, nsvg__endElement, nsvg__content, p);
3282 nsvg__createGradients(p);
3285 nsvg__scaleToViewbox(p, units);
3290 nsvg__deleteParser(p);
3302 fp =
fopen(filename,
"rb");
3303 if (!fp)
goto error;
3307 data = (
char*)
malloc(size + 1);
3308 if (data ==
NULL)
goto error;
3309 if (
fread(data, 1, size, fp) != size)
goto error;
3319 if (data)
free(data);
3332 if (res ==
NULL)
goto error;
3336 if (res->
pts ==
NULL)
goto error;
3357 if (image ==
NULL)
return;
3359 while (shape !=
NULL) {
3360 snext = shape->
next;
3361 nsvg__deletePaths(shape->
paths);
3362 nsvg__deletePaint(&shape->
fill);
3363 nsvg__deletePaint(&shape->
stroke);
uint64_t size_t
Definition stdint.h:101
#define SEEK_SET
Definition stdio.h:46
#define SEEK_END
Definition stdio.h:48
XE_LIB long ftell(FILE *fp)
Definition stdio.cpp:172
XE_LIB int sscanf(const char *str, const char *format,...)
Definition stdio.cpp:397
XE_LIB FILE * fopen(const char *name, const char *mode)
Definition stdio.cpp:42
XE_LIB int fclose(FILE *fp)
Definition stdio.cpp:229
XE_LIB int fseek(FILE *fp, long int offset, int pos)
Definition stdio.cpp:185
XE_LIB size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition stdio.cpp:93
XE_LIB long long int strtoll(const char *string, char **endString, int base)
Definition string.cpp:568
XETime t
Definition main.cpp:53
char * path
Definition main.cpp:66
int strcmp(const char *String1, const char *String2)
Definition utclib.c:579
char * strstr(char *String1, char *String2)
Definition utclib.c:763
void * memcpy(void *Dest, const void *Src, ACPI_SIZE Count)
Definition utclib.c:310
ACPI_SIZE strlen(const char *String)
Definition utclib.c:379
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition utclib.c:644
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition utclib.c:537
char * strchr(const char *String, int ch)
locates first occurance of character in string
Definition utclib.c:611
void * memset(void *Dest, int Value, ACPI_SIZE Count)
Definition utclib.c:346
float acosf(float x)
Definition acosf.cpp:50
#define NULL
Definition actypes.h:561
#define sign(x)
Definition draw.cpp:36
XE_LIB double pow(double, double)
Definition math.cpp:173
XE_LIB double sqrt(double)
Definition math.cpp:276
XE_LIB double fabs(double)
Definition math.cpp:120
XE_LIB float fabsf(float)
Definition math.cpp:127
XE_LIB float sqrtf(float x)
Definition math.cpp:304
XE_LIB float tanf(float)
Definition math.cpp:299
XE_LIB float roundf(float x)
Definition roundf.cpp:16
XE_LIB float sinf(float)
Definition math.cpp:227
XE_LIB float cosf(float)
Definition math.cpp:85
NSVGimage * nsvgParseFromFile(const char *filename, const char *units, float dpi)
NSVGfillRule
Definition nanosvg.h:100
@ NSVG_FILLRULE_NONZERO
Definition nanosvg.h:101
@ NSVG_FILLRULE_EVENODD
Definition nanosvg.h:102
NSVGpaintOrder
Definition nanosvg.h:109
@ NSVG_PAINT_STROKE
Definition nanosvg.h:112
@ NSVG_PAINT_MARKERS
Definition nanosvg.h:111
@ NSVG_PAINT_FILL
Definition nanosvg.h:110
NSVGpath * nsvgDuplicatePath(NSVGpath *p)
NSVGlineCap
Definition nanosvg.h:94
@ NSVG_CAP_SQUARE
Definition nanosvg.h:97
@ NSVG_CAP_BUTT
Definition nanosvg.h:95
@ NSVG_CAP_ROUND
Definition nanosvg.h:96
NSVGflags
Definition nanosvg.h:105
@ NSVG_FLAGS_VISIBLE
Definition nanosvg.h:106
NSVGpaintType
Definition nanosvg.h:74
@ NSVG_PAINT_NONE
Definition nanosvg.h:76
@ NSVG_PAINT_UNDEF
Definition nanosvg.h:75
@ NSVG_PAINT_COLOR
Definition nanosvg.h:77
@ NSVG_PAINT_RADIAL_GRADIENT
Definition nanosvg.h:79
@ NSVG_PAINT_LINEAR_GRADIENT
Definition nanosvg.h:78
NSVGimage * nsvgParse(char *input, const char *units, float dpi)
void nsvgDelete(NSVGimage *image)
NSVGspreadType
Definition nanosvg.h:82
@ NSVG_SPREAD_REPEAT
Definition nanosvg.h:85
@ NSVG_SPREAD_PAD
Definition nanosvg.h:83
@ NSVG_SPREAD_REFLECT
Definition nanosvg.h:84
NSVGlineJoin
Definition nanosvg.h:88
@ NSVG_JOIN_BEVEL
Definition nanosvg.h:91
@ NSVG_JOIN_ROUND
Definition nanosvg.h:90
@ NSVG_JOIN_MITER
Definition nanosvg.h:89
XE_LIB long strtol(const char *nptr, char **endptr, int base)
converts a string to a long
Definition stdlib.cpp:282
XE_LIB void * realloc(void *address, unsigned int new_size)
Definition _heap.cpp:6420
unsigned int color
Definition nanosvg.h:116
float offset
Definition nanosvg.h:117
float fy
Definition nanosvg.h:123
float xform[6]
Definition nanosvg.h:121
int nstops
Definition nanosvg.h:124
float fx
Definition nanosvg.h:123
char spread
Definition nanosvg.h:122
NSVGgradientStop stops[1]
Definition nanosvg.h:125
float width
Definition nanosvg.h:171
float height
Definition nanosvg.h:172
NSVGshape * shapes
Definition nanosvg.h:173
unsigned int color
Definition nanosvg.h:131
NSVGgradient * gradient
Definition nanosvg.h:132
signed char type
Definition nanosvg.h:129
float * pts
Definition nanosvg.h:138
char closed
Definition nanosvg.h:140
int npts
Definition nanosvg.h:139
struct NSVGpath * next
Definition nanosvg.h:142
float bounds[4]
Definition nanosvg.h:141
NSVGpath * paths
Definition nanosvg.h:165
float miterLimit
Definition nanosvg.h:157
char strokeLineCap
Definition nanosvg.h:156
char strokeDashCount
Definition nanosvg.h:154
float opacity
Definition nanosvg.h:150
NSVGpaint fill
Definition nanosvg.h:148
struct NSVGshape * next
Definition nanosvg.h:166
char strokeLineJoin
Definition nanosvg.h:155
float xform[6]
Definition nanosvg.h:164
float strokeDashArray[8]
Definition nanosvg.h:153
float strokeWidth
Definition nanosvg.h:151
unsigned char flags
Definition nanosvg.h:160
float strokeDashOffset
Definition nanosvg.h:152
NSVGpaint stroke
Definition nanosvg.h:149
char fillRule
Definition nanosvg.h:158
char fillGradient[64]
Definition nanosvg.h:162
float bounds[4]
Definition nanosvg.h:161
unsigned char paintOrder
Definition nanosvg.h:159
char id[64]
Definition nanosvg.h:147
char strokeGradient[64]
Definition nanosvg.h:163
int x
Definition term.cpp:49
TTY * last
Definition tty.cpp:48
void * malloc(unsigned nSize)
Definition uspios.c:31
void free(void *pBlock)
Definition uspios.c:35
struct VirtioInputEvent * input
Definition virtiokbd.c:45