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++;
284 if (!*s || *s ==
'?' || *s ==
'!')
289 while (*s && !nsvg__isspace(*s)) s++;
290 if (*s) { *s++ =
'\0'; }
293 while (!
end && *s && nattr < NSVG_XML_MAX_ATTRIBS-3) {
298 while (*s && nsvg__isspace(*s)) s++;
306 while (*s && !nsvg__isspace(*s) && *s !=
'=') s++;
307 if (*s) { *s++ =
'\0'; }
309 while (*s && *s !=
'\"' && *s !=
'\'') s++;
315 while (*s && *s != quote) s++;
316 if (*s) { *s++ =
'\0'; }
320 attr[nattr++] =
name;
321 attr[nattr++] = value;
330 if (start && startelCb)
331 (*startelCb)(ud,
name, attr);
333 (*endelCb)(ud,
name);
336int nsvg__parseXML(
char*
input,
337 void (*startelCb)(
void* ud,
const char* el,
const char** attr),
338 void (*endelCb)(
void* ud,
const char* el),
339 void (*contentCb)(
void* ud,
const char* s),
344 int state = NSVG_XML_CONTENT;
346 if (*s ==
'<' &&
state == NSVG_XML_CONTENT) {
349 nsvg__parseContent(mark, contentCb, ud);
351 state = NSVG_XML_TAG;
352 }
else if (*s ==
'>' &&
state == NSVG_XML_TAG) {
355 nsvg__parseElement(mark, startelCb, endelCb, ud);
357 state = NSVG_XML_CONTENT;
369#define NSVG_MAX_ATTR 128
371enum NSVGgradientUnits {
373 NSVG_OBJECT_SPACE = 1
376#define NSVG_MAX_DASHES 8
377#define NSVG_MAX_CLASSES 32
392typedef struct NSVGcoordinate {
397typedef struct NSVGlinearData {
398 NSVGcoordinate x1, y1, x2, y2;
401typedef struct NSVGradialData {
402 NSVGcoordinate
cx,
cy, r, fx, fy;
405typedef struct NSVGgradientData
411 NSVGlinearData linear;
412 NSVGradialData radial;
419 struct NSVGgradientData* next;
422typedef struct NSVGattrib
426 unsigned int fillColor;
427 unsigned int strokeColor;
431 char fillGradient[64];
432 char strokeGradient[64];
434 float strokeDashOffset;
435 float strokeDashArray[NSVG_MAX_DASHES];
442 unsigned int stopColor;
448 unsigned char paintOrder;
451typedef struct NSVGstyleDeclaration
454 char* propertiesText;
455 struct NSVGstyleDeclaration* next;
456} NSVGstyleDeclaration;
458typedef struct NSVGparser
460 NSVGattrib attr[NSVG_MAX_ATTR];
467 NSVGstyleDeclaration* styles;
468 NSVGgradientData* gradients;
470 float viewMinx, viewMiny, viewWidth, viewHeight;
471 int alignX, alignY, alignType;
478static void nsvg__xformIdentity(
float*
t)
480 t[0] = 1.0f;
t[1] = 0.0f;
481 t[2] = 0.0f;
t[3] = 1.0f;
482 t[4] = 0.0f;
t[5] = 0.0f;
485static void nsvg__xformSetTranslation(
float*
t,
float tx,
float ty)
487 t[0] = 1.0f;
t[1] = 0.0f;
488 t[2] = 0.0f;
t[3] = 1.0f;
489 t[4] = tx;
t[5] = ty;
492static void nsvg__xformSetScale(
float*
t,
float sx,
float sy)
494 t[0] = sx;
t[1] = 0.0f;
495 t[2] = 0.0f;
t[3] = sy;
496 t[4] = 0.0f;
t[5] = 0.0f;
499static void nsvg__xformSetSkewX(
float*
t,
float a)
501 t[0] = 1.0f;
t[1] = 0.0f;
503 t[4] = 0.0f;
t[5] = 0.0f;
506static void nsvg__xformSetSkewY(
float*
t,
float a)
509 t[2] = 0.0f;
t[3] = 1.0f;
510 t[4] = 0.0f;
t[5] = 0.0f;
513static void nsvg__xformSetRotation(
float*
t,
float a)
516 t[0] = cs;
t[1] = sn;
517 t[2] = -sn;
t[3] = cs;
518 t[4] = 0.0f;
t[5] = 0.0f;
521static void nsvg__xformMultiply(
float*
t,
float* s)
523 float t0 =
t[0] * s[0] +
t[1] * s[2];
524 float t2 =
t[2] * s[0] +
t[3] * s[2];
525 float t4 =
t[4] * s[0] +
t[5] * s[2] + s[4];
526 t[1] =
t[0] * s[1] +
t[1] * s[3];
527 t[3] =
t[2] * s[1] +
t[3] * s[3];
528 t[5] =
t[4] * s[1] +
t[5] * s[3] + s[5];
534static void nsvg__xformInverse(
float* inv,
float*
t)
536 double invdet, det = (double)
t[0] *
t[3] - (
double)
t[2] *
t[1];
537 if (det > -1e-6 && det < 1e-6) {
538 nsvg__xformIdentity(
t);
542 inv[0] = (float)(
t[3] * invdet);
543 inv[2] = (float)(-
t[2] * invdet);
544 inv[4] = (float)(((
double)
t[2] *
t[5] - (double)
t[3] *
t[4]) * invdet);
545 inv[1] = (float)(-
t[1] * invdet);
546 inv[3] = (float)(
t[0] * invdet);
547 inv[5] = (float)(((
double)
t[1] *
t[4] - (double)
t[0] *
t[5]) * invdet);
550static void nsvg__xformPremultiply(
float*
t,
float* s)
553 memcpy(s2, s,
sizeof(
float)*6);
554 nsvg__xformMultiply(s2,
t);
555 memcpy(
t, s2,
sizeof(
float)*6);
558static void nsvg__xformPoint(
float* dx,
float* dy,
float x,
float y,
float*
t)
560 *dx =
x*
t[0] +
y*
t[2] +
t[4];
561 *dy =
x*
t[1] +
y*
t[3] +
t[5];
564static void nsvg__xformVec(
float* dx,
float* dy,
float x,
float y,
float*
t)
566 *dx =
x*
t[0] +
y*
t[2];
567 *dy =
x*
t[1] +
y*
t[3];
570#define NSVG_EPSILON (1e-12)
572static int nsvg__ptInBounds(
float*
pt,
float* bounds)
574 return pt[0] >= bounds[0] &&
pt[0] <= bounds[2] &&
pt[1] >= bounds[1] &&
pt[1] <= bounds[3];
578static double nsvg__evalBezier(
double t,
double p0,
double p1,
double p2,
double p3)
581 return it*it*it*p0 + 3.0*it*it*
t*p1 + 3.0*it*
t*
t*p2 +
t*
t*
t*p3;
584static void nsvg__curveBounds(
float* bounds,
float* curve)
587 double roots[2],
a,
b, c, b2ac,
t, v;
588 float* v0 = &curve[0];
589 float* v1 = &curve[2];
590 float* v2 = &curve[4];
591 float* v3 = &curve[6];
594 bounds[0] = nsvg__minf(v0[0], v3[0]);
595 bounds[1] = nsvg__minf(v0[1], v3[1]);
596 bounds[2] = nsvg__maxf(v0[0], v3[0]);
597 bounds[3] = nsvg__maxf(v0[1], v3[1]);
601 if (nsvg__ptInBounds(v1, bounds) && nsvg__ptInBounds(v2, bounds))
605 for (i = 0; i < 2; i++) {
606 a = -3.0 * v0[i] + 9.0 * v1[i] - 9.0 * v2[i] + 3.0 * v3[i];
607 b = 6.0 * v0[i] - 12.0 * v1[i] + 6.0 * v2[i];
608 c = 3.0 * v1[i] - 3.0 * v0[i];
610 if (
fabs(
a) < NSVG_EPSILON) {
611 if (
fabs(
b) > NSVG_EPSILON) {
613 if (
t > NSVG_EPSILON &&
t < 1.0-NSVG_EPSILON)
617 b2ac =
b*
b - 4.0*c*
a;
618 if (b2ac > NSVG_EPSILON) {
619 t = (-
b +
sqrt(b2ac)) / (2.0 *
a);
620 if (
t > NSVG_EPSILON &&
t < 1.0-NSVG_EPSILON)
622 t = (-
b -
sqrt(b2ac)) / (2.0 *
a);
623 if (
t > NSVG_EPSILON &&
t < 1.0-NSVG_EPSILON)
627 for (j = 0; j < count; j++) {
628 v = nsvg__evalBezier(roots[j], v0[i], v1[i], v2[i], v3[i]);
629 bounds[0+i] = nsvg__minf(bounds[0+i], (
float)v);
630 bounds[2+i] = nsvg__maxf(bounds[2+i], (
float)v);
636 return (
a & 0x03) | ((
b & 0x03) << 2) | ((c & 0x03) << 4);
639static NSVGparser* nsvg__createParser(
void)
642 p = (NSVGparser*)
malloc(
sizeof(NSVGparser));
643 if (p ==
NULL)
goto error;
644 memset(p, 0,
sizeof(NSVGparser));
647 if (p->image ==
NULL)
goto error;
651 nsvg__xformIdentity(p->attr[0].xform);
652 memset(p->attr[0].id, 0,
sizeof p->attr[0].id);
653 p->attr[0].fillColor = NSVG_RGB(0,0,0);
654 p->attr[0].strokeColor = NSVG_RGB(0,0,0);
655 p->attr[0].opacity = 1;
656 p->attr[0].fillOpacity = 1;
657 p->attr[0].strokeOpacity = 1;
658 p->attr[0].stopOpacity = 1;
659 p->attr[0].strokeWidth = 1;
662 p->attr[0].miterLimit = 4;
664 p->attr[0].hasFill = 1;
665 p->attr[0].visible = 1;
672 if (p->image)
free(p->image);
677static void nsvg__deleteStyles(NSVGstyleDeclaration* style) {
679 NSVGstyleDeclaration* next = style->next;
680 free(style->className);
681 free(style->propertiesText);
698static void nsvg__deletePaint(
NSVGpaint* paint)
704static void nsvg__deleteGradientData(NSVGgradientData* grad)
706 NSVGgradientData* next;
707 while (grad !=
NULL) {
715static void nsvg__deleteParser(NSVGparser* p)
718 nsvg__deleteStyles(p->styles);
719 nsvg__deletePaths(p->plist);
720 nsvg__deleteGradientData(p->gradients);
727static void nsvg__resetPath(NSVGparser* p)
732static void nsvg__addPoint(NSVGparser* p,
float x,
float y)
734 if (p->npts+1 > p->cpts) {
735 p->cpts = p->cpts ? p->cpts*2 : 8;
736 p->pts = (
float*)
realloc(p->pts, p->cpts*2*
sizeof(
float));
739 p->pts[p->npts*2+0] =
x;
740 p->pts[p->npts*2+1] =
y;
744static void nsvg__moveTo(NSVGparser* p,
float x,
float y)
747 p->pts[(p->npts-1)*2+0] = x;
748 p->pts[(p->npts-1)*2+1] = y;
750 nsvg__addPoint(p, x, y);
754static void nsvg__lineTo(NSVGparser* p,
float x,
float y)
758 px = p->pts[(p->npts-1)*2+0];
759 py = p->pts[(p->npts-1)*2+1];
762 nsvg__addPoint(p, px + dx/3.0f, py + dy/3.0f);
763 nsvg__addPoint(p, x - dx/3.0f, y - dy/3.0f);
764 nsvg__addPoint(p, x, y);
768static void nsvg__cubicBezTo(NSVGparser* p,
float cpx1,
float cpy1,
float cpx2,
float cpy2,
float x,
float y)
771 nsvg__addPoint(p, cpx1, cpy1);
772 nsvg__addPoint(p, cpx2, cpy2);
773 nsvg__addPoint(p, x, y);
777static NSVGattrib* nsvg__getAttr(NSVGparser* p)
779 return &p->attr[p->attrHead];
782static void nsvg__pushAttr(NSVGparser* p)
784 if (p->attrHead < NSVG_MAX_ATTR-1) {
786 memcpy(&p->attr[p->attrHead], &p->attr[p->attrHead-1],
sizeof(NSVGattrib));
790static void nsvg__popAttr(NSVGparser* p)
796static float nsvg__actualOrigX(NSVGparser* p)
801static float nsvg__actualOrigY(NSVGparser* p)
806static float nsvg__actualWidth(NSVGparser* p)
811static float nsvg__actualHeight(NSVGparser* p)
813 return p->viewHeight;
816static float nsvg__actualLength(NSVGparser* p)
818 float w = nsvg__actualWidth(p),
h = nsvg__actualHeight(p);
822static float nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c,
float orig,
float length)
824 NSVGattrib* attr = nsvg__getAttr(p);
826 case NSVG_UNITS_USER:
return c.value;
827 case NSVG_UNITS_PX:
return c.value;
828 case NSVG_UNITS_PT:
return c.value / 72.0f * p->dpi;
829 case NSVG_UNITS_PC:
return c.value / 6.0f * p->dpi;
830 case NSVG_UNITS_MM:
return c.value / 25.4f * p->dpi;
831 case NSVG_UNITS_CM:
return c.value / 2.54f * p->dpi;
832 case NSVG_UNITS_IN:
return c.value * p->dpi;
833 case NSVG_UNITS_EM:
return c.value * attr->fontSize;
834 case NSVG_UNITS_EX:
return c.value * attr->fontSize * 0.52f;
835 case NSVG_UNITS_PERCENT:
return orig + c.value / 100.0f * length;
836 default:
return c.value;
841static NSVGgradientData* nsvg__findGradientData(NSVGparser* p,
const char*
id)
843 NSVGgradientData* grad = p->gradients;
844 if (
id ==
NULL || *
id ==
'\0')
846 while (grad !=
NULL) {
847 if (
strcmp(grad->id,
id) == 0)
854static NSVGgradient* nsvg__createGradient(NSVGparser* p,
const char*
id,
const float* localBounds,
float *xform,
signed char* paintType)
857 NSVGgradientData* ref =
NULL;
860 float ox, oy, sw, sh, sl;
864 data = nsvg__findGradientData(p,
id);
870 while (ref !=
NULL) {
871 NSVGgradientData* nextRef =
NULL;
872 if (stops ==
NULL && ref->stops !=
NULL) {
874 nstops = ref->nstops;
877 nextRef = nsvg__findGradientData(p, ref->ref);
878 if (nextRef == ref)
break;
881 if (refIter > 32)
break;
889 if (
data->units == NSVG_OBJECT_SPACE) {
892 sw = localBounds[2] - localBounds[0];
893 sh = localBounds[3] - localBounds[1];
895 ox = nsvg__actualOrigX(p);
896 oy = nsvg__actualOrigY(p);
897 sw = nsvg__actualWidth(p);
898 sh = nsvg__actualHeight(p);
903 float x1, y1, x2, y2, dx, dy;
904 x1 = nsvg__convertToPixels(p,
data->linear.x1, ox, sw);
905 y1 = nsvg__convertToPixels(p,
data->linear.y1, oy, sh);
906 x2 = nsvg__convertToPixels(p,
data->linear.x2, ox, sw);
907 y2 = nsvg__convertToPixels(p,
data->linear.y2, oy, sh);
915 float cx,
cy, fx, fy, r;
916 cx = nsvg__convertToPixels(p,
data->radial.cx, ox, sw);
917 cy = nsvg__convertToPixels(p,
data->radial.cy, oy, sh);
918 fx = nsvg__convertToPixels(p,
data->radial.fx, ox, sw);
919 fy = nsvg__convertToPixels(p,
data->radial.fy, oy, sh);
920 r = nsvg__convertToPixels(p,
data->radial.r, 0, sl);
929 nsvg__xformMultiply(grad->
xform,
data->xform);
930 nsvg__xformMultiply(grad->
xform, xform);
936 *paintType =
data->type;
941static float nsvg__getAverageScale(
float*
t)
945 return (sx + sy) * 0.5f;
948static void nsvg__getLocalBounds(
float* bounds,
NSVGshape *
shape,
float* xform)
951 float curve[4*2], curveBounds[4];
954 nsvg__xformPoint(&curve[0], &curve[1],
path->pts[0],
path->pts[1], xform);
955 for (i = 0; i <
path->npts-1; i += 3) {
956 nsvg__xformPoint(&curve[2], &curve[3],
path->pts[(i+1)*2],
path->pts[(i+1)*2+1], xform);
957 nsvg__xformPoint(&curve[4], &curve[5],
path->pts[(i+2)*2],
path->pts[(i+2)*2+1], xform);
958 nsvg__xformPoint(&curve[6], &curve[7],
path->pts[(i+3)*2],
path->pts[(i+3)*2+1], xform);
959 nsvg__curveBounds(curveBounds, curve);
961 bounds[0] = curveBounds[0];
962 bounds[1] = curveBounds[1];
963 bounds[2] = curveBounds[2];
964 bounds[3] = curveBounds[3];
967 bounds[0] = nsvg__minf(bounds[0], curveBounds[0]);
968 bounds[1] = nsvg__minf(bounds[1], curveBounds[1]);
969 bounds[2] = nsvg__maxf(bounds[2], curveBounds[2]);
970 bounds[3] = nsvg__maxf(bounds[3], curveBounds[3]);
978static void nsvg__addShape(NSVGparser* p)
980 NSVGattrib* attr = nsvg__getAttr(p);
986 if (p->plist ==
NULL)
994 memcpy(
shape->fillGradient, attr->fillGradient,
sizeof shape->fillGradient);
995 memcpy(
shape->strokeGradient, attr->strokeGradient,
sizeof shape->strokeGradient);
997 scale = nsvg__getAverageScale(attr->xform);
998 shape->strokeWidth = attr->strokeWidth * scale;
999 shape->strokeDashOffset = attr->strokeDashOffset * scale;
1000 shape->strokeDashCount = (char)attr->strokeDashCount;
1001 for (i = 0; i < attr->strokeDashCount; i++)
1002 shape->strokeDashArray[i] = attr->strokeDashArray[i] * scale;
1003 shape->strokeLineJoin = attr->strokeLineJoin;
1004 shape->strokeLineCap = attr->strokeLineCap;
1005 shape->miterLimit = attr->miterLimit;
1006 shape->fillRule = attr->fillRule;
1007 shape->opacity = attr->opacity;
1008 shape->paintOrder = attr->paintOrder;
1010 shape->paths = p->plist;
1019 shape->bounds[0] = nsvg__minf(
shape->bounds[0],
path->bounds[0]);
1020 shape->bounds[1] = nsvg__minf(
shape->bounds[1],
path->bounds[1]);
1021 shape->bounds[2] = nsvg__maxf(
shape->bounds[2],
path->bounds[2]);
1022 shape->bounds[3] = nsvg__maxf(
shape->bounds[3],
path->bounds[3]);
1026 if (attr->hasFill == 0) {
1028 }
else if (attr->hasFill == 1) {
1030 shape->fill.color = attr->fillColor;
1031 shape->fill.color |= (
unsigned int)(attr->fillOpacity*255) << 24;
1032 }
else if (attr->hasFill == 2) {
1037 if (attr->hasStroke == 0) {
1039 }
else if (attr->hasStroke == 1) {
1041 shape->stroke.color = attr->strokeColor;
1042 shape->stroke.color |= (
unsigned int)(attr->strokeOpacity*255) << 24;
1043 }
else if (attr->hasStroke == 2) {
1051 if (p->image->shapes ==
NULL)
1052 p->image->shapes =
shape;
1054 p->shapesTail->next =
shape;
1055 p->shapesTail =
shape;
1063static void nsvg__addPath(NSVGparser* p,
char closed)
1065 NSVGattrib* attr = nsvg__getAttr(p);
1075 nsvg__lineTo(p, p->pts[0], p->pts[1]);
1078 if ((p->npts % 3) != 1)
1085 path->pts = (
float*)
malloc(p->npts*2*
sizeof(
float));
1087 path->closed = closed;
1088 path->npts = p->npts;
1091 for (i = 0; i < p->npts; ++i)
1092 nsvg__xformPoint(&
path->pts[i*2], &
path->pts[i*2+1], p->pts[i*2], p->pts[i*2+1], attr->xform);
1095 for (i = 0; i <
path->npts-1; i += 3) {
1096 curve = &
path->pts[i*2];
1097 nsvg__curveBounds(bounds, curve);
1099 path->bounds[0] = bounds[0];
1100 path->bounds[1] = bounds[1];
1101 path->bounds[2] = bounds[2];
1102 path->bounds[3] = bounds[3];
1104 path->bounds[0] = nsvg__minf(
path->bounds[0], bounds[0]);
1105 path->bounds[1] = nsvg__minf(
path->bounds[1], bounds[1]);
1106 path->bounds[2] = nsvg__maxf(
path->bounds[2], bounds[2]);
1107 path->bounds[3] = nsvg__maxf(
path->bounds[3], bounds[3]);
1111 path->next = p->plist;
1124static double nsvg__atof(
const char* s)
1126 char* cur = (
char*)s;
1128 double res = 0.0,
sign = 1.0;
1129 long long intPart = 0, fracPart = 0;
1130 char hasIntPart = 0, hasFracPart = 0;
1135 }
else if (*cur ==
'-') {
1141 if (nsvg__isdigit(*cur)) {
1145 res = (double)intPart;
1154 if (nsvg__isdigit(*cur)) {
1158 res += (double)fracPart /
pow(10.0, (
double)(
end - cur));
1166 if (!hasIntPart && !hasFracPart)
1170 if (*cur ==
'e' || *cur ==
'E') {
1175 res *=
pow(10.0, (
double)expPart);
1183static const char* nsvg__parseNumber(
const char* s,
char* it,
const int size)
1189 if (*s ==
'-' || *s ==
'+') {
1190 if (i <
last) it[i++] = *s;
1194 while (*s && nsvg__isdigit(*s)) {
1195 if (i <
last) it[i++] = *s;
1200 if (i <
last) it[i++] = *s;
1203 while (*s && nsvg__isdigit(*s)) {
1204 if (i <
last) it[i++] = *s;
1209 if ((*s ==
'e' || *s ==
'E') && (s[1] !=
'm' && s[1] !=
'x')) {
1210 if (i <
last) it[i++] = *s;
1212 if (*s ==
'-' || *s ==
'+') {
1213 if (i <
last) it[i++] = *s;
1216 while (*s && nsvg__isdigit(*s)) {
1217 if (i <
last) it[i++] = *s;
1226static const char* nsvg__getNextPathItemWhenArcFlag(
const char* s,
char* it)
1229 while (*s && (nsvg__isspace(*s) || *s ==
',')) s++;
1231 if (*s ==
'0' || *s ==
'1') {
1239static const char* nsvg__getNextPathItem(
const char* s,
char* it)
1243 while (*s && (nsvg__isspace(*s) || *s ==
',')) s++;
1245 if (*s ==
'-' || *s ==
'+' || *s ==
'.' || nsvg__isdigit(*s)) {
1246 s = nsvg__parseNumber(s, it, 64);
1257static int nsvg__hexDigit(
char c)
1259 if (c >=
'0' && c <=
'9')
return c -
'0';
1260 if (c >=
'a' && c <=
'f')
return c -
'a' + 10;
1261 if (c >=
'A' && c <=
'F')
return c -
'A' + 10;
1265static unsigned int nsvg__parseColorHex(
const char* str)
1272 if (!str || *str !=
'#')
1273 return NSVG_RGB(128, 128, 128);
1276 int v = nsvg__hexDigit(str[n]);
1283 return NSVG_RGB(
d[0] * 16 +
d[1],
d[2] * 16 +
d[3],
d[4] * 16 +
d[5]);
1285 return NSVG_RGB(
d[0] * 17,
d[1] * 17,
d[2] * 17);
1286 return NSVG_RGB(128, 128, 128);
1293static unsigned int nsvg__parseColorRGB(
const char* str)
1296 unsigned int rgbi[3];
1299 if (
sscanf(str,
"rgb(%u, %u, %u)", &rgbi[0], &rgbi[1], &rgbi[2]) != 3) {
1301 const char delimiter[3] = {
',',
',',
')'};
1303 for (i = 0; i < 3; i++) {
1304 while (*str && (nsvg__isspace(*str))) str++;
1305 if (*str ==
'+') str++;
1307 rgbf[i] = nsvg__atof(str);
1317 while (*str && nsvg__isdigit(*str)) str++;
1320 if (!nsvg__isdigit(*str))
break;
1321 while (*str && nsvg__isdigit(*str)) str++;
1323 if (*str ==
'%') str++;
else break;
1324 while (*str && nsvg__isspace(*str)) str++;
1325 if (*str == delimiter[i]) str++;
1329 rgbi[0] =
roundf(rgbf[0] * 2.55f);
1330 rgbi[1] =
roundf(rgbf[1] * 2.55f);
1331 rgbi[2] =
roundf(rgbf[2] * 2.55f);
1333 rgbi[0] = rgbi[1] = rgbi[2] = 128;
1337 for (i = 0; i < 3; i++) {
1338 if (rgbi[i] > 255) rgbi[i] = 255;
1340 return NSVG_RGB(rgbi[0], rgbi[1], rgbi[2]);
1343typedef struct NSVGNamedColor {
1348NSVGNamedColor nsvg__colors[] = {
1350 {
"red", NSVG_RGB(255, 0, 0) },
1351 {
"green", NSVG_RGB( 0, 128, 0) },
1352 {
"blue", NSVG_RGB( 0, 0, 255) },
1353 {
"yellow", NSVG_RGB(255, 255, 0) },
1354 {
"cyan", NSVG_RGB( 0, 255, 255) },
1355 {
"magenta", NSVG_RGB(255, 0, 255) },
1356 {
"black", NSVG_RGB( 0, 0, 0) },
1357 {
"grey", NSVG_RGB(128, 128, 128) },
1358 {
"gray", NSVG_RGB(128, 128, 128) },
1359 {
"white", NSVG_RGB(255, 255, 255) },
1361#ifdef NANOSVG_ALL_COLOR_KEYWORDS
1362 {
"aliceblue", NSVG_RGB(240, 248, 255) },
1363 {
"antiquewhite", NSVG_RGB(250, 235, 215) },
1364 {
"aqua", NSVG_RGB( 0, 255, 255) },
1365 {
"aquamarine", NSVG_RGB(127, 255, 212) },
1366 {
"azure", NSVG_RGB(240, 255, 255) },
1367 {
"beige", NSVG_RGB(245, 245, 220) },
1368 {
"bisque", NSVG_RGB(255, 228, 196) },
1369 {
"blanchedalmond", NSVG_RGB(255, 235, 205) },
1370 {
"blueviolet", NSVG_RGB(138, 43, 226) },
1371 {
"brown", NSVG_RGB(165, 42, 42) },
1372 {
"burlywood", NSVG_RGB(222, 184, 135) },
1373 {
"cadetblue", NSVG_RGB( 95, 158, 160) },
1374 {
"chartreuse", NSVG_RGB(127, 255, 0) },
1375 {
"chocolate", NSVG_RGB(210, 105, 30) },
1376 {
"coral", NSVG_RGB(255, 127, 80) },
1377 {
"cornflowerblue", NSVG_RGB(100, 149, 237) },
1378 {
"cornsilk", NSVG_RGB(255, 248, 220) },
1379 {
"crimson", NSVG_RGB(220, 20, 60) },
1380 {
"darkblue", NSVG_RGB( 0, 0, 139) },
1381 {
"darkcyan", NSVG_RGB( 0, 139, 139) },
1382 {
"darkgoldenrod", NSVG_RGB(184, 134, 11) },
1383 {
"darkgray", NSVG_RGB(169, 169, 169) },
1384 {
"darkgreen", NSVG_RGB( 0, 100, 0) },
1385 {
"darkgrey", NSVG_RGB(169, 169, 169) },
1386 {
"darkkhaki", NSVG_RGB(189, 183, 107) },
1387 {
"darkmagenta", NSVG_RGB(139, 0, 139) },
1388 {
"darkolivegreen", NSVG_RGB( 85, 107, 47) },
1389 {
"darkorange", NSVG_RGB(255, 140, 0) },
1390 {
"darkorchid", NSVG_RGB(153, 50, 204) },
1391 {
"darkred", NSVG_RGB(139, 0, 0) },
1392 {
"darksalmon", NSVG_RGB(233, 150, 122) },
1393 {
"darkseagreen", NSVG_RGB(143, 188, 143) },
1394 {
"darkslateblue", NSVG_RGB( 72, 61, 139) },
1395 {
"darkslategray", NSVG_RGB( 47, 79, 79) },
1396 {
"darkslategrey", NSVG_RGB( 47, 79, 79) },
1397 {
"darkturquoise", NSVG_RGB( 0, 206, 209) },
1398 {
"darkviolet", NSVG_RGB(148, 0, 211) },
1399 {
"deeppink", NSVG_RGB(255, 20, 147) },
1400 {
"deepskyblue", NSVG_RGB( 0, 191, 255) },
1401 {
"dimgray", NSVG_RGB(105, 105, 105) },
1402 {
"dimgrey", NSVG_RGB(105, 105, 105) },
1403 {
"dodgerblue", NSVG_RGB( 30, 144, 255) },
1404 {
"firebrick", NSVG_RGB(178, 34, 34) },
1405 {
"floralwhite", NSVG_RGB(255, 250, 240) },
1406 {
"forestgreen", NSVG_RGB( 34, 139, 34) },
1407 {
"fuchsia", NSVG_RGB(255, 0, 255) },
1408 {
"gainsboro", NSVG_RGB(220, 220, 220) },
1409 {
"ghostwhite", NSVG_RGB(248, 248, 255) },
1410 {
"gold", NSVG_RGB(255, 215, 0) },
1411 {
"goldenrod", NSVG_RGB(218, 165, 32) },
1412 {
"greenyellow", NSVG_RGB(173, 255, 47) },
1413 {
"honeydew", NSVG_RGB(240, 255, 240) },
1414 {
"hotpink", NSVG_RGB(255, 105, 180) },
1415 {
"indianred", NSVG_RGB(205, 92, 92) },
1416 {
"indigo", NSVG_RGB( 75, 0, 130) },
1417 {
"ivory", NSVG_RGB(255, 255, 240) },
1418 {
"khaki", NSVG_RGB(240, 230, 140) },
1419 {
"lavender", NSVG_RGB(230, 230, 250) },
1420 {
"lavenderblush", NSVG_RGB(255, 240, 245) },
1421 {
"lawngreen", NSVG_RGB(124, 252, 0) },
1422 {
"lemonchiffon", NSVG_RGB(255, 250, 205) },
1423 {
"lightblue", NSVG_RGB(173, 216, 230) },
1424 {
"lightcoral", NSVG_RGB(240, 128, 128) },
1425 {
"lightcyan", NSVG_RGB(224, 255, 255) },
1426 {
"lightgoldenrodyellow", NSVG_RGB(250, 250, 210) },
1427 {
"lightgray", NSVG_RGB(211, 211, 211) },
1428 {
"lightgreen", NSVG_RGB(144, 238, 144) },
1429 {
"lightgrey", NSVG_RGB(211, 211, 211) },
1430 {
"lightpink", NSVG_RGB(255, 182, 193) },
1431 {
"lightsalmon", NSVG_RGB(255, 160, 122) },
1432 {
"lightseagreen", NSVG_RGB( 32, 178, 170) },
1433 {
"lightskyblue", NSVG_RGB(135, 206, 250) },
1434 {
"lightslategray", NSVG_RGB(119, 136, 153) },
1435 {
"lightslategrey", NSVG_RGB(119, 136, 153) },
1436 {
"lightsteelblue", NSVG_RGB(176, 196, 222) },
1437 {
"lightyellow", NSVG_RGB(255, 255, 224) },
1438 {
"lime", NSVG_RGB( 0, 255, 0) },
1439 {
"limegreen", NSVG_RGB( 50, 205, 50) },
1440 {
"linen", NSVG_RGB(250, 240, 230) },
1441 {
"maroon", NSVG_RGB(128, 0, 0) },
1442 {
"mediumaquamarine", NSVG_RGB(102, 205, 170) },
1443 {
"mediumblue", NSVG_RGB( 0, 0, 205) },
1444 {
"mediumorchid", NSVG_RGB(186, 85, 211) },
1445 {
"mediumpurple", NSVG_RGB(147, 112, 219) },
1446 {
"mediumseagreen", NSVG_RGB( 60, 179, 113) },
1447 {
"mediumslateblue", NSVG_RGB(123, 104, 238) },
1448 {
"mediumspringgreen", NSVG_RGB( 0, 250, 154) },
1449 {
"mediumturquoise", NSVG_RGB( 72, 209, 204) },
1450 {
"mediumvioletred", NSVG_RGB(199, 21, 133) },
1451 {
"midnightblue", NSVG_RGB( 25, 25, 112) },
1452 {
"mintcream", NSVG_RGB(245, 255, 250) },
1453 {
"mistyrose", NSVG_RGB(255, 228, 225) },
1454 {
"moccasin", NSVG_RGB(255, 228, 181) },
1455 {
"navajowhite", NSVG_RGB(255, 222, 173) },
1456 {
"navy", NSVG_RGB( 0, 0, 128) },
1457 {
"oldlace", NSVG_RGB(253, 245, 230) },
1458 {
"olive", NSVG_RGB(128, 128, 0) },
1459 {
"olivedrab", NSVG_RGB(107, 142, 35) },
1460 {
"orange", NSVG_RGB(255, 165, 0) },
1461 {
"orangered", NSVG_RGB(255, 69, 0) },
1462 {
"orchid", NSVG_RGB(218, 112, 214) },
1463 {
"palegoldenrod", NSVG_RGB(238, 232, 170) },
1464 {
"palegreen", NSVG_RGB(152, 251, 152) },
1465 {
"paleturquoise", NSVG_RGB(175, 238, 238) },
1466 {
"palevioletred", NSVG_RGB(219, 112, 147) },
1467 {
"papayawhip", NSVG_RGB(255, 239, 213) },
1468 {
"peachpuff", NSVG_RGB(255, 218, 185) },
1469 {
"peru", NSVG_RGB(205, 133, 63) },
1470 {
"pink", NSVG_RGB(255, 192, 203) },
1471 {
"plum", NSVG_RGB(221, 160, 221) },
1472 {
"powderblue", NSVG_RGB(176, 224, 230) },
1473 {
"purple", NSVG_RGB(128, 0, 128) },
1474 {
"rosybrown", NSVG_RGB(188, 143, 143) },
1475 {
"royalblue", NSVG_RGB( 65, 105, 225) },
1476 {
"saddlebrown", NSVG_RGB(139, 69, 19) },
1477 {
"salmon", NSVG_RGB(250, 128, 114) },
1478 {
"sandybrown", NSVG_RGB(244, 164, 96) },
1479 {
"seagreen", NSVG_RGB( 46, 139, 87) },
1480 {
"seashell", NSVG_RGB(255, 245, 238) },
1481 {
"sienna", NSVG_RGB(160, 82, 45) },
1482 {
"silver", NSVG_RGB(192, 192, 192) },
1483 {
"skyblue", NSVG_RGB(135, 206, 235) },
1484 {
"slateblue", NSVG_RGB(106, 90, 205) },
1485 {
"slategray", NSVG_RGB(112, 128, 144) },
1486 {
"slategrey", NSVG_RGB(112, 128, 144) },
1487 {
"snow", NSVG_RGB(255, 250, 250) },
1488 {
"springgreen", NSVG_RGB( 0, 255, 127) },
1489 {
"steelblue", NSVG_RGB( 70, 130, 180) },
1490 {
"tan", NSVG_RGB(210, 180, 140) },
1491 {
"teal", NSVG_RGB( 0, 128, 128) },
1492 {
"thistle", NSVG_RGB(216, 191, 216) },
1493 {
"tomato", NSVG_RGB(255, 99, 71) },
1494 {
"turquoise", NSVG_RGB( 64, 224, 208) },
1495 {
"violet", NSVG_RGB(238, 130, 238) },
1496 {
"wheat", NSVG_RGB(245, 222, 179) },
1497 {
"whitesmoke", NSVG_RGB(245, 245, 245) },
1498 {
"yellowgreen", NSVG_RGB(154, 205, 50) },
1502static unsigned int nsvg__parseColorName(
const char* str)
1504 int i, ncolors =
sizeof(nsvg__colors) /
sizeof(NSVGNamedColor);
1506 for (i = 0; i < ncolors; i++) {
1507 if (
strcmp(nsvg__colors[i].
name, str) == 0) {
1508 return nsvg__colors[i].color;
1512 return NSVG_RGB(128, 128, 128);
1515static unsigned int nsvg__parseColor(
const char* str)
1518 while(*str ==
' ') ++str;
1520 if (
len >= 1 && *str ==
'#')
1521 return nsvg__parseColorHex(str);
1522 else if (
len >= 4 && str[0] ==
'r' && str[1] ==
'g' && str[2] ==
'b' && str[3] ==
'(')
1523 return nsvg__parseColorRGB(str);
1524 return nsvg__parseColorName(str);
1527static float nsvg__parseOpacity(
const char* str)
1529 float val = nsvg__atof(str);
1530 if (val < 0.0f) val = 0.0f;
1531 if (val > 1.0f) val = 1.0f;
1535static float nsvg__parseMiterLimit(
const char* str)
1537 float val = nsvg__atof(str);
1538 if (val < 0.0f) val = 0.0f;
1542static int nsvg__parseUnits(
const char* units)
1544 if (units[0] ==
'p' && units[1] ==
'x')
1545 return NSVG_UNITS_PX;
1546 else if (units[0] ==
'p' && units[1] ==
't')
1547 return NSVG_UNITS_PT;
1548 else if (units[0] ==
'p' && units[1] ==
'c')
1549 return NSVG_UNITS_PC;
1550 else if (units[0] ==
'm' && units[1] ==
'm')
1551 return NSVG_UNITS_MM;
1552 else if (units[0] ==
'c' && units[1] ==
'm')
1553 return NSVG_UNITS_CM;
1554 else if (units[0] ==
'i' && units[1] ==
'n')
1555 return NSVG_UNITS_IN;
1556 else if (units[0] ==
'%')
1557 return NSVG_UNITS_PERCENT;
1558 else if (units[0] ==
'e' && units[1] ==
'm')
1559 return NSVG_UNITS_EM;
1560 else if (units[0] ==
'e' && units[1] ==
'x')
1561 return NSVG_UNITS_EX;
1562 return NSVG_UNITS_USER;
1565static int nsvg__isCoordinate(
const char* s)
1568 if (*s ==
'-' || *s ==
'+')
1571 return (nsvg__isdigit(*s) || *s ==
'.');
1574static NSVGcoordinate nsvg__parseCoordinateRaw(
const char* str)
1576 NSVGcoordinate coord = {0, NSVG_UNITS_USER};
1578 coord.units = nsvg__parseUnits(nsvg__parseNumber(str, buf, 64));
1579 coord.value = nsvg__atof(buf);
1583static NSVGcoordinate nsvg__coord(
float v,
int units)
1585 NSVGcoordinate coord = {v, units};
1589static float nsvg__parseCoordinate(NSVGparser* p,
const char* str,
float orig,
float length)
1591 NSVGcoordinate coord = nsvg__parseCoordinateRaw(str);
1592 return nsvg__convertToPixels(p, coord, orig, length);
1595static int nsvg__parseTransformArgs(
const char* str,
float*
args,
int maxNa,
int* na)
1603 while (*ptr && *ptr !=
'(') ++ptr;
1612 if (*ptr ==
'-' || *ptr ==
'+' || *ptr ==
'.' || nsvg__isdigit(*ptr)) {
1613 if (*na >= maxNa)
return 0;
1614 ptr = nsvg__parseNumber(ptr, it, 64);
1615 args[(*na)++] = (float)nsvg__atof(it);
1620 return (
int)(
end - str);
1624static int nsvg__parseMatrix(
float* xform,
const char* str)
1628 int len = nsvg__parseTransformArgs(str,
t, 6, &na);
1629 if (na != 6)
return len;
1630 memcpy(xform,
t,
sizeof(
float)*6);
1634static int nsvg__parseTranslate(
float* xform,
const char* str)
1639 int len = nsvg__parseTransformArgs(str,
args, 2, &na);
1640 if (na == 1)
args[1] = 0.0;
1642 nsvg__xformSetTranslation(
t,
args[0],
args[1]);
1643 memcpy(xform,
t,
sizeof(
float)*6);
1647static int nsvg__parseScale(
float* xform,
const char* str)
1652 int len = nsvg__parseTransformArgs(str,
args, 2, &na);
1654 nsvg__xformSetScale(
t,
args[0],
args[1]);
1655 memcpy(xform,
t,
sizeof(
float)*6);
1659static int nsvg__parseSkewX(
float* xform,
const char* str)
1664 int len = nsvg__parseTransformArgs(str,
args, 1, &na);
1665 nsvg__xformSetSkewX(
t,
args[0]/180.0f*NSVG_PI);
1666 memcpy(xform,
t,
sizeof(
float)*6);
1670static int nsvg__parseSkewY(
float* xform,
const char* str)
1675 int len = nsvg__parseTransformArgs(str,
args, 1, &na);
1676 nsvg__xformSetSkewY(
t,
args[0]/180.0f*NSVG_PI);
1677 memcpy(xform,
t,
sizeof(
float)*6);
1681static int nsvg__parseRotate(
float* xform,
const char* str)
1687 int len = nsvg__parseTransformArgs(str,
args, 3, &na);
1690 nsvg__xformIdentity(m);
1693 nsvg__xformSetTranslation(
t, -
args[1], -
args[2]);
1694 nsvg__xformMultiply(m,
t);
1697 nsvg__xformSetRotation(
t,
args[0]/180.0f*NSVG_PI);
1698 nsvg__xformMultiply(m,
t);
1701 nsvg__xformSetTranslation(
t,
args[1],
args[2]);
1702 nsvg__xformMultiply(m,
t);
1705 memcpy(xform, m,
sizeof(
float)*6);
1710static void nsvg__parseTransform(
float* xform,
const char* str)
1714 nsvg__xformIdentity(xform);
1717 if (
strncmp(str,
"matrix", 6) == 0)
1718 len = nsvg__parseMatrix(
t, str);
1719 else if (
strncmp(str,
"translate", 9) == 0)
1720 len = nsvg__parseTranslate(
t, str);
1721 else if (
strncmp(str,
"scale", 5) == 0)
1722 len = nsvg__parseScale(
t, str);
1723 else if (
strncmp(str,
"rotate", 6) == 0)
1724 len = nsvg__parseRotate(
t, str);
1725 else if (
strncmp(str,
"skewX", 5) == 0)
1726 len = nsvg__parseSkewX(
t, str);
1727 else if (
strncmp(str,
"skewY", 5) == 0)
1728 len = nsvg__parseSkewY(
t, str);
1740 nsvg__xformPremultiply(xform,
t);
1744static void nsvg__parseUrl(
char*
id,
const char* str)
1748 if (*str && *str ==
'#')
1750 while (i < 63 && *str && *str !=
')') {
1757static char nsvg__parseLineCap(
const char* str)
1759 if (
strcmp(str,
"butt") == 0)
1761 else if (
strcmp(str,
"round") == 0)
1763 else if (
strcmp(str,
"square") == 0)
1769static char nsvg__parseLineJoin(
const char* str)
1771 if (
strcmp(str,
"miter") == 0)
1773 else if (
strcmp(str,
"round") == 0)
1775 else if (
strcmp(str,
"bevel") == 0)
1781static char nsvg__parseFillRule(
const char* str)
1783 if (
strcmp(str,
"nonzero") == 0)
1785 else if (
strcmp(str,
"evenodd") == 0)
1791static unsigned char nsvg__parsePaintOrder(
const char* str)
1793 if (
strcmp(str,
"normal") == 0 ||
strcmp(str,
"fill stroke markers") == 0)
1795 else if (
strcmp(str,
"fill markers stroke") == 0)
1797 else if (
strcmp(str,
"markers fill stroke") == 0)
1799 else if (
strcmp(str,
"markers stroke fill") == 0)
1801 else if (
strcmp(str,
"stroke fill markers") == 0)
1803 else if (
strcmp(str,
"stroke markers fill") == 0)
1809static const char* nsvg__getNextDashItem(
const char* s,
char* it)
1814 while (*s && (nsvg__isspace(*s) || *s ==
',')) s++;
1816 while (*s && (!nsvg__isspace(*s) && *s !=
',')) {
1825static int nsvg__parseStrokeDashArray(NSVGparser* p,
const char* str,
float* strokeDashArray)
1837 str = nsvg__getNextDashItem(str, item);
1839 if (count < NSVG_MAX_DASHES)
1840 strokeDashArray[count++] =
fabsf(nsvg__parseCoordinate(p, item, 0.0f, nsvg__actualLength(p)));
1843 for (i = 0; i < count; i++)
1844 sum += strokeDashArray[i];
1851static void nsvg__parseStyle(NSVGparser* p,
const char* str);
1855static void nsvg__applyClassStyles(NSVGparser* p,
const char* value)
1857 const char* cur = value;
1859 const char* classStart;
1861 NSVGstyleDeclaration* style;
1863 while (*cur && nsvg__isspace(*cur))
1869 while (*cur && !nsvg__isspace(*cur))
1871 classLen = (
size_t)(cur - classStart);
1873 for (style = p->styles; style !=
NULL; style = style->next) {
1874 if (
strncmp(style->className, classStart, classLen) == 0 && style->className[classLen] ==
'\0') {
1875 nsvg__parseStyle(p, style->propertiesText);
1881static int nsvg__parseAttr(NSVGparser* p,
const char*
name,
const char* value)
1884 NSVGattrib* attr = nsvg__getAttr(p);
1885 if (!attr)
return 0;
1888 nsvg__parseStyle(p, value);
1890 if (
strcmp(value,
"none") == 0)
1895 if (
strcmp(value,
"none") == 0) {
1897 }
else if (
strncmp(value,
"url(", 4) == 0) {
1899 nsvg__parseUrl(attr->fillGradient, value);
1902 attr->fillColor = nsvg__parseColor(value);
1905 attr->opacity = nsvg__parseOpacity(value);
1906 }
else if (
strcmp(
name,
"fill-opacity") == 0) {
1907 attr->fillOpacity = nsvg__parseOpacity(value);
1909 if (
strcmp(value,
"none") == 0) {
1910 attr->hasStroke = 0;
1911 }
else if (
strncmp(value,
"url(", 4) == 0) {
1912 attr->hasStroke = 2;
1913 nsvg__parseUrl(attr->strokeGradient, value);
1915 attr->hasStroke = 1;
1916 attr->strokeColor = nsvg__parseColor(value);
1918 }
else if (
strcmp(
name,
"stroke-width") == 0) {
1919 attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1920 }
else if (
strcmp(
name,
"stroke-dasharray") == 0) {
1921 attr->strokeDashCount = nsvg__parseStrokeDashArray(p, value, attr->strokeDashArray);
1922 }
else if (
strcmp(
name,
"stroke-dashoffset") == 0) {
1923 attr->strokeDashOffset = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1924 }
else if (
strcmp(
name,
"stroke-opacity") == 0) {
1925 attr->strokeOpacity = nsvg__parseOpacity(value);
1926 }
else if (
strcmp(
name,
"stroke-linecap") == 0) {
1927 attr->strokeLineCap = nsvg__parseLineCap(value);
1928 }
else if (
strcmp(
name,
"stroke-linejoin") == 0) {
1929 attr->strokeLineJoin = nsvg__parseLineJoin(value);
1930 }
else if (
strcmp(
name,
"stroke-miterlimit") == 0) {
1931 attr->miterLimit = nsvg__parseMiterLimit(value);
1933 attr->fillRule = nsvg__parseFillRule(value);
1935 attr->fontSize = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
1937 nsvg__parseTransform(xform, value);
1938 nsvg__xformPremultiply(attr->xform, xform);
1939 }
else if (
strcmp(
name,
"stop-color") == 0) {
1940 attr->stopColor = nsvg__parseColor(value);
1941 }
else if (
strcmp(
name,
"stop-opacity") == 0) {
1942 attr->stopOpacity = nsvg__parseOpacity(value);
1944 attr->stopOffset = nsvg__parseCoordinate(p, value, 0.0f, 1.0f);
1945 }
else if (
strcmp(
name,
"paint-order") == 0) {
1946 attr->paintOrder = nsvg__parsePaintOrder(value);
1949 attr->id[63] =
'\0';
1951 nsvg__applyClassStyles(p, value);
1959static int nsvg__parseNameValue(NSVGparser* p,
const char* start,
const char*
end)
1968 while (str <
end && *str !=
':') ++str;
1973 while (str > start && (*str ==
':' || nsvg__isspace(*str))) --str;
1976 n = (int)(str - start);
1977 if (n > 511) n = 511;
1981 while (val <
end && (*val ==
':' || nsvg__isspace(*val))) ++val;
1983 n = (int)(
end - val);
1984 if (n > 511) n = 511;
1985 if (n)
memcpy(value, val, n);
1988 return nsvg__parseAttr(p,
name, value);
1991static void nsvg__parseStyle(NSVGparser* p,
const char* str)
1998 while(*str && nsvg__isspace(*str)) ++str;
2000 while(*str && *str !=
';') ++str;
2004 while (
end > start && (*
end ==
';' || nsvg__isspace(*
end))) --
end;
2007 nsvg__parseNameValue(p, start,
end);
2012static void nsvg__parseAttribs(NSVGparser* p,
const char** attr)
2015 for (i = 0; attr[i]; i += 2)
2017 if (
strcmp(attr[i],
"style") == 0)
2018 nsvg__parseStyle(p, attr[i + 1]);
2020 nsvg__parseAttr(p, attr[i], attr[i + 1]);
2024static int nsvg__getArgsPerElement(
char cmd)
2057static void nsvg__pathMoveTo(NSVGparser* p,
float* cpx,
float* cpy,
float*
args,
int rel)
2066 nsvg__moveTo(p, *cpx, *cpy);
2069static void nsvg__pathLineTo(NSVGparser* p,
float* cpx,
float* cpy,
float*
args,
int rel)
2078 nsvg__lineTo(p, *cpx, *cpy);
2081static void nsvg__pathHLineTo(NSVGparser* p,
float* cpx,
float* cpy,
float*
args,
int rel)
2087 nsvg__lineTo(p, *cpx, *cpy);
2090static void nsvg__pathVLineTo(NSVGparser* p,
float* cpx,
float* cpy,
float*
args,
int rel)
2096 nsvg__lineTo(p, *cpx, *cpy);
2099static void nsvg__pathCubicBezTo(NSVGparser* p,
float* cpx,
float* cpy,
2100 float* cpx2,
float* cpy2,
float*
args,
int rel)
2102 float x2, y2, cx1, cy1, cx2, cy2;
2105 cx1 = *cpx +
args[0];
2106 cy1 = *cpy +
args[1];
2107 cx2 = *cpx +
args[2];
2108 cy2 = *cpy +
args[3];
2109 x2 = *cpx +
args[4];
2110 y2 = *cpy +
args[5];
2120 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2128static void nsvg__pathCubicBezShortTo(NSVGparser* p,
float* cpx,
float* cpy,
2129 float* cpx2,
float* cpy2,
float*
args,
int rel)
2131 float x1, y1, x2, y2, cx1, cy1, cx2, cy2;
2136 cx2 = *cpx +
args[0];
2137 cy2 = *cpy +
args[1];
2138 x2 = *cpx +
args[2];
2139 y2 = *cpy +
args[3];
2150 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2158static void nsvg__pathQuadBezTo(NSVGparser* p,
float* cpx,
float* cpy,
2159 float* cpx2,
float* cpy2,
float*
args,
int rel)
2161 float x1, y1, x2, y2,
cx,
cy;
2162 float cx1, cy1, cx2, cy2;
2169 x2 = *cpx +
args[2];
2170 y2 = *cpy +
args[3];
2179 cx1 = x1 + 2.0f/3.0f*(
cx - x1);
2180 cy1 = y1 + 2.0f/3.0f*(
cy - y1);
2181 cx2 = x2 + 2.0f/3.0f*(
cx - x2);
2182 cy2 = y2 + 2.0f/3.0f*(
cy - y2);
2184 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2192static void nsvg__pathQuadBezShortTo(NSVGparser* p,
float* cpx,
float* cpy,
2193 float* cpx2,
float* cpy2,
float*
args,
int rel)
2195 float x1, y1, x2, y2,
cx,
cy;
2196 float cx1, cy1, cx2, cy2;
2201 x2 = *cpx +
args[0];
2202 y2 = *cpy +
args[1];
2212 cx1 = x1 + 2.0f/3.0f*(
cx - x1);
2213 cy1 = y1 + 2.0f/3.0f*(
cy - y1);
2214 cx2 = x2 + 2.0f/3.0f*(
cx - x2);
2215 cy2 = y2 + 2.0f/3.0f*(
cy - y2);
2217 nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
2225static float nsvg__sqr(
float x) {
return x*
x; }
2226static float nsvg__vmag(
float x,
float y) {
return sqrtf(x*x + y*y); }
2228static float nsvg__vecrat(
float ux,
float uy,
float vx,
float vy)
2230 return (ux*vx + uy*vy) / (nsvg__vmag(ux,uy) * nsvg__vmag(vx,vy));
2233static float nsvg__vecang(
float ux,
float uy,
float vx,
float vy)
2235 float r = nsvg__vecrat(ux,uy, vx,vy);
2236 if (r < -1.0f) r = -1.0f;
2237 if (r > 1.0f) r = 1.0f;
2238 return ((ux*vy < uy*vx) ? -1.0f : 1.0f) *
acosf(r);
2241static void nsvg__pathArcTo(NSVGparser* p,
float* cpx,
float* cpy,
float*
args,
int rel)
2245 float x1, y1, x2, y2,
cx,
cy, dx, dy,
d;
2246 float x1p, y1p, cxp, cyp, s, sa, sb;
2247 float ux, uy, vx, vy, a1, da;
2248 float x,
y, tanx, tany,
a, px = 0, py = 0, ptanx = 0, ptany = 0,
t[6];
2256 rotx =
args[2] / 180.0f * NSVG_PI;
2262 x2 = *cpx +
args[5];
2263 y2 = *cpy +
args[6];
2271 d =
sqrtf(dx*dx + dy*dy);
2272 if (
d < 1e-6f || rx < 1e-6f || ry < 1e-6f) {
2274 nsvg__lineTo(p, x2, y2);
2286 x1p = cosrx * dx / 2.0f + sinrx * dy / 2.0f;
2287 y1p = -sinrx * dx / 2.0f + cosrx * dy / 2.0f;
2288 d = nsvg__sqr(x1p)/nsvg__sqr(rx) + nsvg__sqr(y1p)/nsvg__sqr(ry);
2296 sa = nsvg__sqr(rx)*nsvg__sqr(ry) - nsvg__sqr(rx)*nsvg__sqr(y1p) - nsvg__sqr(ry)*nsvg__sqr(x1p);
2297 sb = nsvg__sqr(rx)*nsvg__sqr(y1p) + nsvg__sqr(ry)*nsvg__sqr(x1p);
2298 if (sa < 0.0f) sa = 0.0f;
2303 cxp = s * rx * y1p / ry;
2304 cyp = s * -ry * x1p / rx;
2307 cx = (x1 + x2)/2.0f + cosrx*cxp - sinrx*cyp;
2308 cy = (y1 + y2)/2.0f + sinrx*cxp + cosrx*cyp;
2311 ux = (x1p - cxp) / rx;
2312 uy = (y1p - cyp) / ry;
2313 vx = (-x1p - cxp) / rx;
2314 vy = (-y1p - cyp) / ry;
2315 a1 = nsvg__vecang(1.0f,0.0f, ux,uy);
2316 da = nsvg__vecang(ux,uy, vx,vy);
2321 if (fs == 0 && da > 0)
2323 else if (fs == 1 && da < 0)
2327 t[0] = cosrx;
t[1] = sinrx;
2328 t[2] = -sinrx;
t[3] = cosrx;
2333 ndivs = (int)(
fabsf(da) / (NSVG_PI*0.5f) + 1.0f);
2334 hda = (da / (float)ndivs) / 2.0f;
2336 if ((hda < 1e-3f) && (hda > -1e-3f))
2339 hda = (1.0f -
cosf(hda)) /
sinf(hda);
2340 kappa =
fabsf(4.0f / 3.0f * hda);
2344 for (i = 0; i <= ndivs; i++) {
2345 a = a1 + da * ((float)i/(
float)ndivs);
2348 nsvg__xformPoint(&x, &y, dx*rx, dy*ry,
t);
2349 nsvg__xformVec(&tanx, &tany, -dy*rx * kappa, dx*ry * kappa,
t);
2351 nsvg__cubicBezTo(p, px+ptanx,py+ptany, x-tanx, y-tany, x, y);
2362static void nsvg__parsePath(NSVGparser* p,
const char** attr)
2364 const char* s =
NULL;
2370 float cpx, cpy, cpx2, cpy2;
2376 for (i = 0; attr[i]; i += 2) {
2377 if (
strcmp(attr[i],
"d") == 0) {
2381 tmp[1] = attr[i + 1];
2384 nsvg__parseAttribs(p, tmp);
2398 if ((cmd ==
'A' || cmd ==
'a') && (nargs == 3 || nargs == 4))
2399 s = nsvg__getNextPathItemWhenArcFlag(s, item);
2401 s = nsvg__getNextPathItem(s, item);
2403 if (cmd !=
'\0' && nsvg__isCoordinate(item)) {
2405 args[nargs++] = (float)nsvg__atof(item);
2406 if (nargs >= rargs) {
2410 nsvg__pathMoveTo(p, &cpx, &cpy,
args, cmd ==
'm' ? 1 : 0);
2413 cmd = (cmd ==
'm') ?
'l' :
'L';
2414 rargs = nsvg__getArgsPerElement(cmd);
2415 cpx2 = cpx; cpy2 = cpy;
2420 nsvg__pathLineTo(p, &cpx, &cpy,
args, cmd ==
'l' ? 1 : 0);
2421 cpx2 = cpx; cpy2 = cpy;
2425 nsvg__pathHLineTo(p, &cpx, &cpy,
args, cmd ==
'h' ? 1 : 0);
2426 cpx2 = cpx; cpy2 = cpy;
2430 nsvg__pathVLineTo(p, &cpx, &cpy,
args, cmd ==
'v' ? 1 : 0);
2431 cpx2 = cpx; cpy2 = cpy;
2435 nsvg__pathCubicBezTo(p, &cpx, &cpy, &cpx2, &cpy2,
args, cmd ==
'c' ? 1 : 0);
2439 nsvg__pathCubicBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2,
args, cmd ==
's' ? 1 : 0);
2443 nsvg__pathQuadBezTo(p, &cpx, &cpy, &cpx2, &cpy2,
args, cmd ==
'q' ? 1 : 0);
2447 nsvg__pathQuadBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2,
args, cmd ==
't' ? 1 : 0);
2451 nsvg__pathArcTo(p, &cpx, &cpy,
args, cmd ==
'a' ? 1 : 0);
2452 cpx2 = cpx; cpy2 = cpy;
2456 cpx =
args[nargs-2];
2457 cpy =
args[nargs-1];
2458 cpx2 = cpx; cpy2 = cpy;
2466 if (cmd ==
'M' || cmd ==
'm') {
2469 nsvg__addPath(p, closedFlag);
2474 }
else if (initPoint == 0) {
2478 if (cmd ==
'Z' || cmd ==
'z') {
2485 cpx2 = cpx; cpy2 = cpy;
2486 nsvg__addPath(p, closedFlag);
2490 nsvg__moveTo(p, cpx, cpy);
2494 rargs = nsvg__getArgsPerElement(cmd);
2504 nsvg__addPath(p, closedFlag);
2510static void nsvg__parseRect(NSVGparser* p,
const char** attr)
2520 for (i = 0; attr[i]; i += 2) {
2521 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2522 if (
strcmp(attr[i],
"x") == 0)
x = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2523 if (
strcmp(attr[i],
"y") == 0)
y = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2524 if (
strcmp(attr[i],
"width") == 0)
w = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p));
2525 if (
strcmp(attr[i],
"height") == 0)
h = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p));
2526 if (
strcmp(attr[i],
"rx") == 0) rx =
fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
2527 if (
strcmp(attr[i],
"ry") == 0) ry =
fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
2531 if (rx < 0.0f && ry > 0.0f) rx = ry;
2532 if (ry < 0.0f && rx > 0.0f) ry = rx;
2533 if (rx < 0.0f) rx = 0.0f;
2534 if (ry < 0.0f) ry = 0.0f;
2535 if (rx > w/2.0f) rx =
w/2.0f;
2536 if (ry >
h/2.0f) ry =
h/2.0f;
2538 if (w != 0.0f &&
h != 0.0f) {
2541 if (rx < 0.00001f || ry < 0.0001f) {
2542 nsvg__moveTo(p, x, y);
2543 nsvg__lineTo(p, x+w, y);
2544 nsvg__lineTo(p, x+w, y+
h);
2545 nsvg__lineTo(p, x, y+
h);
2548 nsvg__moveTo(p, x+rx, y);
2549 nsvg__lineTo(p, x+w-rx, y);
2550 nsvg__cubicBezTo(p, x+w-rx*(1-NSVG_KAPPA90), y, x+w, y+ry*(1-NSVG_KAPPA90), x+w, y+ry);
2551 nsvg__lineTo(p, x+w, y+
h-ry);
2552 nsvg__cubicBezTo(p, x+w, y+
h-ry*(1-NSVG_KAPPA90), x+w-rx*(1-NSVG_KAPPA90), y+
h, x+w-rx, y+
h);
2553 nsvg__lineTo(p, x+rx, y+
h);
2554 nsvg__cubicBezTo(p, x+rx*(1-NSVG_KAPPA90), y+
h, x, y+
h-ry*(1-NSVG_KAPPA90), x, y+
h-ry);
2555 nsvg__lineTo(p, x, y+ry);
2556 nsvg__cubicBezTo(p, x, y+ry*(1-NSVG_KAPPA90), x+rx*(1-NSVG_KAPPA90), y, x+rx, y);
2559 nsvg__addPath(p, 1);
2565static void nsvg__parseCircle(NSVGparser* p,
const char** attr)
2572 for (i = 0; attr[i]; i += 2) {
2573 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2574 if (
strcmp(attr[i],
"cx") == 0)
cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2575 if (
strcmp(attr[i],
"cy") == 0)
cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2576 if (
strcmp(attr[i],
"r") == 0) r =
fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualLength(p)));
2583 nsvg__moveTo(p,
cx+r,
cy);
2584 nsvg__cubicBezTo(p,
cx+r,
cy+r*NSVG_KAPPA90,
cx+r*NSVG_KAPPA90,
cy+r,
cx,
cy+r);
2585 nsvg__cubicBezTo(p,
cx-r*NSVG_KAPPA90,
cy+r,
cx-r,
cy+r*NSVG_KAPPA90,
cx-r,
cy);
2586 nsvg__cubicBezTo(p,
cx-r,
cy-r*NSVG_KAPPA90,
cx-r*NSVG_KAPPA90,
cy-r,
cx,
cy-r);
2587 nsvg__cubicBezTo(p,
cx+r*NSVG_KAPPA90,
cy-r,
cx+r,
cy-r*NSVG_KAPPA90,
cx+r,
cy);
2589 nsvg__addPath(p, 1);
2595static void nsvg__parseEllipse(NSVGparser* p,
const char** attr)
2603 for (i = 0; attr[i]; i += 2) {
2604 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2605 if (
strcmp(attr[i],
"cx") == 0)
cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2606 if (
strcmp(attr[i],
"cy") == 0)
cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2607 if (
strcmp(attr[i],
"rx") == 0) rx =
fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
2608 if (
strcmp(attr[i],
"ry") == 0) ry =
fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
2612 if (rx > 0.0f && ry > 0.0f) {
2616 nsvg__moveTo(p,
cx+rx,
cy);
2617 nsvg__cubicBezTo(p,
cx+rx,
cy+ry*NSVG_KAPPA90,
cx+rx*NSVG_KAPPA90,
cy+ry,
cx,
cy+ry);
2618 nsvg__cubicBezTo(p,
cx-rx*NSVG_KAPPA90,
cy+ry,
cx-rx,
cy+ry*NSVG_KAPPA90,
cx-rx,
cy);
2619 nsvg__cubicBezTo(p,
cx-rx,
cy-ry*NSVG_KAPPA90,
cx-rx*NSVG_KAPPA90,
cy-ry,
cx,
cy-ry);
2620 nsvg__cubicBezTo(p,
cx+rx*NSVG_KAPPA90,
cy-ry,
cx+rx,
cy-ry*NSVG_KAPPA90,
cx+rx,
cy);
2622 nsvg__addPath(p, 1);
2628static void nsvg__parseLine(NSVGparser* p,
const char** attr)
2636 for (i = 0; attr[i]; i += 2) {
2637 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2638 if (
strcmp(attr[i],
"x1") == 0) x1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2639 if (
strcmp(attr[i],
"y1") == 0) y1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2640 if (
strcmp(attr[i],
"x2") == 0) x2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
2641 if (
strcmp(attr[i],
"y2") == 0) y2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
2647 nsvg__moveTo(p, x1, y1);
2648 nsvg__lineTo(p, x2, y2);
2650 nsvg__addPath(p, 0);
2655static void nsvg__parsePoly(NSVGparser* p,
const char** attr,
int closeFlag)
2660 int nargs, npts = 0;
2665 for (i = 0; attr[i]; i += 2) {
2666 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2667 if (
strcmp(attr[i],
"points") == 0) {
2671 s = nsvg__getNextPathItem(s, item);
2672 args[nargs++] = (float)nsvg__atof(item);
2686 nsvg__addPath(p, (
char)closeFlag);
2691static void nsvg__parseSVG(NSVGparser* p,
const char** attr)
2694 for (i = 0; attr[i]; i += 2) {
2695 if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2696 if (
strcmp(attr[i],
"width") == 0) {
2697 p->image->width = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
2698 }
else if (
strcmp(attr[i],
"height") == 0) {
2699 p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
2700 }
else if (
strcmp(attr[i],
"viewBox") == 0) {
2701 const char *s = attr[i + 1];
2703 s = nsvg__parseNumber(s, buf, 64);
2704 p->viewMinx = nsvg__atof(buf);
2705 while (*s && (nsvg__isspace(*s) || *s ==
'%' || *s ==
',')) s++;
2707 s = nsvg__parseNumber(s, buf, 64);
2708 p->viewMiny = nsvg__atof(buf);
2709 while (*s && (nsvg__isspace(*s) || *s ==
'%' || *s ==
',')) s++;
2711 s = nsvg__parseNumber(s, buf, 64);
2712 p->viewWidth = nsvg__atof(buf);
2713 while (*s && (nsvg__isspace(*s) || *s ==
'%' || *s ==
',')) s++;
2715 s = nsvg__parseNumber(s, buf, 64);
2716 p->viewHeight = nsvg__atof(buf);
2717 }
else if (
strcmp(attr[i],
"preserveAspectRatio") == 0) {
2718 if (
strstr(attr[i + 1],
"none") != 0) {
2720 p->alignType = NSVG_ALIGN_NONE;
2723 if (
strstr(attr[i + 1],
"xMin") != 0)
2724 p->alignX = NSVG_ALIGN_MIN;
2725 else if (
strstr(attr[i + 1],
"xMid") != 0)
2726 p->alignX = NSVG_ALIGN_MID;
2727 else if (
strstr(attr[i + 1],
"xMax") != 0)
2728 p->alignX = NSVG_ALIGN_MAX;
2730 if (
strstr(attr[i + 1],
"yMin") != 0)
2731 p->alignY = NSVG_ALIGN_MIN;
2732 else if (
strstr(attr[i + 1],
"yMid") != 0)
2733 p->alignY = NSVG_ALIGN_MID;
2734 else if (
strstr(attr[i + 1],
"yMax") != 0)
2735 p->alignY = NSVG_ALIGN_MAX;
2737 p->alignType = NSVG_ALIGN_MEET;
2738 if (
strstr(attr[i + 1],
"slice") != 0)
2739 p->alignType = NSVG_ALIGN_SLICE;
2746static void nsvg__parseGradient(NSVGparser* p,
const char** attr,
signed char type)
2749 NSVGgradientData* grad = (NSVGgradientData*)
malloc(
sizeof(NSVGgradientData));
2750 if (grad ==
NULL)
return;
2751 memset(grad, 0,
sizeof(NSVGgradientData));
2752 grad->units = NSVG_OBJECT_SPACE;
2755 grad->linear.x1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2756 grad->linear.y1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2757 grad->linear.x2 = nsvg__coord(100.0f, NSVG_UNITS_PERCENT);
2758 grad->linear.y2 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
2760 grad->radial.cx = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2761 grad->radial.cy = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2762 grad->radial.r = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
2765 nsvg__xformIdentity(grad->xform);
2767 for (i = 0; attr[i]; i += 2) {
2768 if (
strcmp(attr[i],
"id") == 0) {
2769 strncpy(grad->id, attr[i+1], 63);
2770 grad->id[63] =
'\0';
2771 }
else if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
2772 if (
strcmp(attr[i],
"gradientUnits") == 0) {
2773 if (
strcmp(attr[i+1],
"objectBoundingBox") == 0)
2774 grad->units = NSVG_OBJECT_SPACE;
2776 grad->units = NSVG_USER_SPACE;
2777 }
else if (
strcmp(attr[i],
"gradientTransform") == 0) {
2778 nsvg__parseTransform(grad->xform, attr[i + 1]);
2779 }
else if (
strcmp(attr[i],
"cx") == 0) {
2780 grad->radial.cx = nsvg__parseCoordinateRaw(attr[i + 1]);
2781 }
else if (
strcmp(attr[i],
"cy") == 0) {
2782 grad->radial.cy = nsvg__parseCoordinateRaw(attr[i + 1]);
2783 }
else if (
strcmp(attr[i],
"r") == 0) {
2784 grad->radial.r = nsvg__parseCoordinateRaw(attr[i + 1]);
2785 }
else if (
strcmp(attr[i],
"fx") == 0) {
2786 grad->radial.fx = nsvg__parseCoordinateRaw(attr[i + 1]);
2787 }
else if (
strcmp(attr[i],
"fy") == 0) {
2788 grad->radial.fy = nsvg__parseCoordinateRaw(attr[i + 1]);
2789 }
else if (
strcmp(attr[i],
"x1") == 0) {
2790 grad->linear.x1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2791 }
else if (
strcmp(attr[i],
"y1") == 0) {
2792 grad->linear.y1 = nsvg__parseCoordinateRaw(attr[i + 1]);
2793 }
else if (
strcmp(attr[i],
"x2") == 0) {
2794 grad->linear.x2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2795 }
else if (
strcmp(attr[i],
"y2") == 0) {
2796 grad->linear.y2 = nsvg__parseCoordinateRaw(attr[i + 1]);
2797 }
else if (
strcmp(attr[i],
"spreadMethod") == 0) {
2798 if (
strcmp(attr[i+1],
"pad") == 0)
2800 else if (
strcmp(attr[i+1],
"reflect") == 0)
2802 else if (
strcmp(attr[i+1],
"repeat") == 0)
2804 }
else if (
strcmp(attr[i],
"xlink:href") == 0) {
2805 const char *
href = attr[i+1];
2807 grad->ref[62] =
'\0';
2812 grad->next = p->gradients;
2813 p->gradients = grad;
2816static void nsvg__parseGradientStop(NSVGparser* p,
const char** attr)
2818 NSVGattrib* curAttr = nsvg__getAttr(p);
2819 NSVGgradientData* grad;
2823 curAttr->stopOffset = 0;
2824 curAttr->stopColor = 0;
2825 curAttr->stopOpacity = 1.0f;
2827 for (i = 0; attr[i]; i += 2) {
2828 nsvg__parseAttr(p, attr[i], attr[i + 1]);
2832 grad = p->gradients;
2833 if (grad ==
NULL)
return;
2837 if (grad->stops ==
NULL)
return;
2840 idx = grad->nstops-1;
2841 for (i = 0; i < grad->nstops-1; i++) {
2842 if (curAttr->stopOffset < grad->stops[i].offset) {
2847 if (idx != grad->nstops-1) {
2848 for (i = grad->nstops-1; i > idx; i--)
2849 grad->stops[i] = grad->stops[i-1];
2852 stop = &grad->stops[idx];
2853 stop->
color = curAttr->stopColor;
2854 stop->
color |= (
unsigned int)(curAttr->stopOpacity*255) << 24;
2855 stop->
offset = curAttr->stopOffset;
2858static void nsvg__startElement(
void* ud,
const char* el,
const char** attr)
2860 NSVGparser* p = (NSVGparser*)ud;
2864 if (
strcmp(el,
"linearGradient") == 0) {
2866 }
else if (
strcmp(el,
"radialGradient") == 0) {
2868 }
else if (
strcmp(el,
"stop") == 0) {
2869 nsvg__parseGradientStop(p, attr);
2870 }
else if (
strcmp(el,
"style") == 0) {
2876 if (
strcmp(el,
"g") == 0) {
2878 nsvg__parseAttribs(p, attr);
2879 }
else if (
strcmp(el,
"path") == 0) {
2883 nsvg__parsePath(p, attr);
2885 }
else if (
strcmp(el,
"rect") == 0) {
2887 nsvg__parseRect(p, attr);
2889 }
else if (
strcmp(el,
"circle") == 0) {
2891 nsvg__parseCircle(p, attr);
2893 }
else if (
strcmp(el,
"ellipse") == 0) {
2895 nsvg__parseEllipse(p, attr);
2897 }
else if (
strcmp(el,
"line") == 0) {
2899 nsvg__parseLine(p, attr);
2901 }
else if (
strcmp(el,
"polyline") == 0) {
2903 nsvg__parsePoly(p, attr, 0);
2905 }
else if (
strcmp(el,
"polygon") == 0) {
2907 nsvg__parsePoly(p, attr, 1);
2909 }
else if (
strcmp(el,
"linearGradient") == 0) {
2911 }
else if (
strcmp(el,
"radialGradient") == 0) {
2913 }
else if (
strcmp(el,
"stop") == 0) {
2914 nsvg__parseGradientStop(p, attr);
2915 }
else if (
strcmp(el,
"defs") == 0) {
2917 }
else if (
strcmp(el,
"svg") == 0) {
2918 nsvg__parseSVG(p, attr);
2919 }
else if (
strcmp(el,
"style") == 0) {
2924static void nsvg__endElement(
void* ud,
const char* el)
2926 NSVGparser* p = (NSVGparser*)ud;
2928 if (
strcmp(el,
"g") == 0) {
2930 }
else if (
strcmp(el,
"path") == 0) {
2932 }
else if (
strcmp(el,
"defs") == 0) {
2934 }
else if (
strcmp(el,
"style") == 0) {
2939static char *nsvg__strndup(
const char *s,
size_t n)
2941 char *result = (
char *)
malloc(n + 1);
2950static void nsvg__content(
void* ud,
const char* s)
2952 NSVGparser* p = (NSVGparser*)ud;
2959 NSVGstyleDeclaration* styles[NSVG_MAX_CLASSES];
2961 const char* propsStart;
2962 const char* propsEnd;
2969 while (*s && *s !=
'{') {
2970 const char* selStart;
2972 NSVGstyleDeclaration* style;
2974 while (*s && (nsvg__isspace(*s) || *s ==
','))
2976 if (!*s || *s ==
'{')
2980 while (*s && !nsvg__isspace(*s) && *s !=
',' && *s !=
'{')
2984 if (*selStart !=
'.' || nstyles >= NSVG_MAX_CLASSES)
2988 style = (NSVGstyleDeclaration*)
malloc(
sizeof(NSVGstyleDeclaration));
2991 style->className = nsvg__strndup(selStart, (
size_t)(selEnd - selStart));
2992 if (style->className ==
NULL) {
2996 style->propertiesText =
NULL;
2998 styles[nstyles++] = style;
3002 for (i = 0; i < nstyles; i++) {
3003 free(styles[i]->className);
3012 while (*s && *s !=
'}')
3018 for (i = 0; i < nstyles; i++) {
3019 styles[i]->propertiesText = nsvg__strndup(propsStart, (
size_t)(propsEnd - propsStart));
3020 if (styles[i]->propertiesText ==
NULL) {
3021 free(styles[i]->className);
3024 styles[i]->next = p->styles;
3025 p->styles = styles[i];
3034static void nsvg__imageBounds(NSVGparser* p,
float* bounds)
3037 shape = p->image->shapes;
3039 bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0;
3042 bounds[0] =
shape->bounds[0];
3043 bounds[1] =
shape->bounds[1];
3044 bounds[2] =
shape->bounds[2];
3045 bounds[3] =
shape->bounds[3];
3047 bounds[0] = nsvg__minf(bounds[0],
shape->bounds[0]);
3048 bounds[1] = nsvg__minf(bounds[1],
shape->bounds[1]);
3049 bounds[2] = nsvg__maxf(bounds[2],
shape->bounds[2]);
3050 bounds[3] = nsvg__maxf(bounds[3],
shape->bounds[3]);
3054static float nsvg__viewAlign(
float content,
float container,
int type)
3056 if (type == NSVG_ALIGN_MIN)
3058 else if (type == NSVG_ALIGN_MAX)
3059 return container - content;
3061 return (container - content) * 0.5f;
3064static void nsvg__scaleGradient(
NSVGgradient* grad,
float tx,
float ty,
float sx,
float sy)
3067 nsvg__xformSetTranslation(
t, tx, ty);
3068 nsvg__xformMultiply (grad->
xform,
t);
3070 nsvg__xformSetScale(
t, sx, sy);
3071 nsvg__xformMultiply (grad->
xform,
t);
3074static void nsvg__scaleToViewbox(NSVGparser* p,
const char* units)
3078 float tx, ty, sx, sy, us, bounds[4],
t[6], avgs;
3083 nsvg__imageBounds(p, bounds);
3085 if (p->viewWidth == 0) {
3086 if (p->image->width > 0) {
3087 p->viewWidth = p->image->width;
3089 p->viewMinx = bounds[0];
3090 p->viewWidth = bounds[2] - bounds[0];
3093 if (p->viewHeight == 0) {
3094 if (p->image->height > 0) {
3095 p->viewHeight = p->image->height;
3097 p->viewMiny = bounds[1];
3098 p->viewHeight = bounds[3] - bounds[1];
3101 if (p->image->width == 0)
3102 p->image->width = p->viewWidth;
3103 if (p->image->height == 0)
3104 p->image->height = p->viewHeight;
3108 sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0;
3109 sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0;
3111 us = 1.0f / nsvg__convertToPixels(p, nsvg__coord(1.0f, nsvg__parseUnits(units)), 0.0f, 1.0f);
3114 if (p->alignType == NSVG_ALIGN_MEET) {
3116 sx = sy = nsvg__minf(sx, sy);
3117 tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
3118 ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
3119 }
else if (p->alignType == NSVG_ALIGN_SLICE) {
3121 sx = sy = nsvg__maxf(sx, sy);
3122 tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
3123 ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
3129 avgs = (sx+sy) / 2.0f;
3131 shape->bounds[0] = (
shape->bounds[0] + tx) * sx;
3132 shape->bounds[1] = (
shape->bounds[1] + ty) * sy;
3133 shape->bounds[2] = (
shape->bounds[2] + tx) * sx;
3134 shape->bounds[3] = (
shape->bounds[3] + ty) * sy;
3136 path->bounds[0] = (
path->bounds[0] + tx) * sx;
3137 path->bounds[1] = (
path->bounds[1] + ty) * sy;
3138 path->bounds[2] = (
path->bounds[2] + tx) * sx;
3139 path->bounds[3] = (
path->bounds[3] + ty) * sy;
3140 for (i =0; i <
path->npts; i++) {
3142 pt[0] = (
pt[0] + tx) * sx;
3143 pt[1] = (
pt[1] + ty) * sy;
3148 nsvg__scaleGradient(
shape->fill.gradient, tx,ty, sx,sy);
3149 memcpy(
t,
shape->fill.gradient->xform,
sizeof(
float)*6);
3150 nsvg__xformInverse(
shape->fill.gradient->xform,
t);
3153 nsvg__scaleGradient(
shape->stroke.gradient, tx,ty, sx,sy);
3154 memcpy(
t,
shape->stroke.gradient->xform,
sizeof(
float)*6);
3155 nsvg__xformInverse(
shape->stroke.gradient->xform,
t);
3158 shape->strokeWidth *= avgs;
3159 shape->strokeDashOffset *= avgs;
3160 for (i = 0; i <
shape->strokeDashCount; i++)
3161 shape->strokeDashArray[i] *= avgs;
3165static void nsvg__createGradients(NSVGparser* p)
3171 if (
shape->fillGradient[0] !=
'\0') {
3172 float inv[6], localBounds[4];
3173 nsvg__xformInverse(inv,
shape->xform);
3174 nsvg__getLocalBounds(localBounds,
shape, inv);
3175 shape->fill.gradient = nsvg__createGradient(p,
shape->fillGradient, localBounds,
shape->xform, &
shape->fill.type);
3182 if (
shape->strokeGradient[0] !=
'\0') {
3183 float inv[6], localBounds[4];
3184 nsvg__xformInverse(inv,
shape->xform);
3185 nsvg__getLocalBounds(localBounds,
shape, inv);
3186 shape->stroke.gradient = nsvg__createGradient(p,
shape->strokeGradient, localBounds,
shape->xform, &
shape->stroke.type);
3200 p = nsvg__createParser();
3206 nsvg__parseXML(
input, nsvg__startElement, nsvg__endElement, nsvg__content, p);
3209 nsvg__createGradients(p);
3212 nsvg__scaleToViewbox(p, units);
3217 nsvg__deleteParser(p);
3229 fp =
fopen(filename,
"rb");
3230 if (!fp)
goto error;
3233 if (
size == -1l)
goto error;
3260 if (res ==
NULL)
goto error;
3264 if (res->
pts ==
NULL)
goto error;
3289 nsvg__deletePaths(
shape->paths);
3290 nsvg__deletePaint(&
shape->fill);
3291 nsvg__deletePaint(&
shape->stroke);
uint64_t size_t
Definition stdint.h:104
NSVGimage * nsvgParseFromFile(const char *filename, const char *units, float dpi)
struct NSVGgradient NSVGgradient
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
struct NSVGgradientStop NSVGgradientStop
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)
struct NSVGshape NSVGshape
struct NSVGpaint NSVGpaint
struct NSVGimage NSVGimage
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 double pow(double, double)
Definition math.cpp:173
XE_LIB double sqrt(double)
Definition math.cpp:267
XE_LIB double fabs(double)
Definition math.cpp:126
XE_LIB float fabsf(float)
Definition math.cpp:133
XE_LIB float sqrtf(float x)
Definition math.cpp:293
XE_LIB float tanf(float)
Definition math.cpp:289
XE_LIB float roundf(float x)
Definition roundf.cpp:15
XE_LIB float sinf(float)
Definition math.cpp:222
XE_LIB float cosf(float)
Definition math.cpp:94
XE_LIB long ftell(FILE *fp)
Definition stdio.cpp:170
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:44
XE_LIB int fclose(FILE *fp)
Definition stdio.cpp:226
XE_LIB int fseek(FILE *fp, long int offset, int pos)
Definition stdio.cpp:183
XE_LIB size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition stdio.cpp:96
XE_LIB long long int strtoll(const char *string, char **endString, int base)
Definition string.cpp:708
XETime t
Definition main.cpp:53
size_t a
Definition main.cpp:156
size_t b
Definition main.cpp:156
char * strstr(char *String1, char *String2)
Definition utclib.c:763
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
float acosf(float x)
Definition acosf.cpp:45
#define NULL
Definition actypes.h:561
shape
Definition bytecode.h:83
float cx
Definition compose3d.cpp:140
float cy
Definition compose3d.cpp:141
#define sign(x)
Definition draw.cpp:36
size_t size
Definition drm_bridge.h:1299
struct drm_crtc struct drm_plane struct drm_plane const struct drm_crtc_funcs const char * name
Definition drm_crtc.h:1242
__u64 image
Definition i915_drm.h:14
size_t len
Definition initial.c:23
void * data
Definition kthread.h:30
int y
Definition layout.c:118
int h
Definition layout.c:118
int x
Definition layout.c:118
const char * href
Definition layout.c:119
int w
Definition layout.c:118
#define d
Definition libunicode.c:1833
#define SEEK_SET
Definition mm.h:108
#define SEEK_END
Definition mm.h:110
#define realloc(p, s)
Definition quickjs.c:2292
#define malloc(s)
Definition quickjs.c:2290
#define free(p)
Definition quickjs.c:2291
const char const char va_list args
Definition seq_buf.h:182
XE_LIB long strtol(const char *nptr, char **endptr, int base)
converts a string to a long
Definition stdlib.cpp:268
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
NSVGgradientStop stops[1]
Definition nanosvg.h:125
int nstops
Definition nanosvg.h:124
float fx
Definition nanosvg.h:123
char spread
Definition nanosvg.h:122
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
float bounds[4]
Definition nanosvg.h:141
char closed
Definition nanosvg.h:140
int npts
Definition nanosvg.h:139
struct NSVGpath * next
Definition nanosvg.h:142
float xform[6]
Definition nanosvg.h:164
struct NSVGshape * next
Definition nanosvg.h:166
char fillGradient[64]
Definition nanosvg.h:162
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
char strokeLineJoin
Definition nanosvg.h:155
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 strokeGradient[64]
Definition nanosvg.h:163
char fillRule
Definition nanosvg.h:158
NSVGpath * paths
Definition nanosvg.h:165
float strokeDashArray[8]
Definition nanosvg.h:153
float bounds[4]
Definition nanosvg.h:161
unsigned char paintOrder
Definition nanosvg.h:159
TTY * last
Definition tty.cpp:48
struct VirtioInputEvent * input
Definition virtiokbd.c:46