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