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