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