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