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