| 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 |
|
| 30 |
|
| 31 |
#ifndef NANOSVGRAST_H |
| 32 |
#define NANOSVGRAST_H |
| 33 |
|
| 34 |
#include "nanosvg.h" |
| 35 |
|
| 36 |
#ifndef NANOSVGRAST_CPLUSPLUS |
| 37 |
#ifdef __cplusplus |
| 38 |
extern "C" { |
| 39 |
#endif |
| 40 |
#endif |
| 41 |
|
| 42 |
typedef struct NSVGrasterizer NSVGrasterizer; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| •55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
NSVGrasterizer* nsvgCreateRasterizer(void); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| •67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
void nsvgRasterize(NSVGrasterizer* r, |
| 73 |
NSVGimage* image, float tx, float ty, float scale, |
| 74 |
unsigned char* dst, int w, int h, int stride); |
| 75 |
|
| •76 |
|
| 77 |
void nsvgRasterizeXY(NSVGrasterizer* r, |
| 78 |
NSVGimage* image, float tx, float ty, |
| 79 |
float sx, float sy, |
| 80 |
unsigned char* dst, int w, int h, int stride); |
| 81 |
|
| 82 |
|
| 83 |
void nsvgDeleteRasterizer(NSVGrasterizer*); |
| 84 |
|
| 85 |
|
| 86 |
#ifndef NANOSVGRAST_CPLUSPLUS |
| 87 |
#ifdef __cplusplus |
| 88 |
} |
| 89 |
#endif |
| 90 |
#endif |
| 91 |
|
| 92 |
#ifdef NANOSVGRAST_IMPLEMENTATION |
| 93 |
|
| 94 |
#include <math.h> |
| 95 |
#include <stdlib.h> |
| 96 |
#include <string.h> |
| 97 |
|
| 98 |
#define NSVG__SUBSAMPLES 5 |
| 99 |
#define NSVG__FIXSHIFT 10 |
| 100 |
#define NSVG__FIX (1 << NSVG__FIXSHIFT) |
| 101 |
#define NSVG__FIXMASK (NSVG__FIX-1) |
| 102 |
#define NSVG__MEMPAGE_SIZE 1024 |
| 103 |
|
| 104 |
typedef struct NSVGedge { |
| 105 |
float x0,y0, x1,y1; |
| 106 |
int dir; |
| 107 |
struct NSVGedge* next; |
| 108 |
} NSVGedge; |
| 109 |
|
| 110 |
typedef struct NSVGpoint { |
| 111 |
float x, y; |
| 112 |
float dx, dy; |
| 113 |
float len; |
| 114 |
float dmx, dmy; |
| 115 |
unsigned char flags; |
| 116 |
} NSVGpoint; |
| 117 |
|
| 118 |
typedef struct NSVGactiveEdge { |
| 119 |
int x,dx; |
| 120 |
float ey; |
| 121 |
int dir; |
| 122 |
struct NSVGactiveEdge *next; |
| 123 |
} NSVGactiveEdge; |
| 124 |
|
| 125 |
typedef struct NSVGmemPage { |
| 126 |
unsigned char mem[NSVG__MEMPAGE_SIZE]; |
| 127 |
int size; |
| 128 |
struct NSVGmemPage* next; |
| 129 |
} NSVGmemPage; |
| 130 |
|
| 131 |
typedef struct NSVGcachedPaint { |
| 132 |
signed char type; |
| 133 |
char spread; |
| 134 |
float xform[6]; |
| 135 |
unsigned int colors[256]; |
| 136 |
} NSVGcachedPaint; |
| 137 |
|
| 138 |
struct NSVGrasterizer |
| 139 |
{ |
| 140 |
float px, py; |
| 141 |
|
| 142 |
float tessTol; |
| 143 |
float distTol; |
| 144 |
|
| 145 |
NSVGedge* edges; |
| 146 |
int nedges; |
| 147 |
int cedges; |
| 148 |
|
| 149 |
NSVGpoint* points; |
| 150 |
int npoints; |
| 151 |
int cpoints; |
| 152 |
|
| 153 |
NSVGpoint* points2; |
| 154 |
int npoints2; |
| 155 |
int cpoints2; |
| 156 |
|
| 157 |
NSVGactiveEdge* freelist; |
| 158 |
NSVGmemPage* pages; |
| 159 |
NSVGmemPage* curpage; |
| 160 |
|
| 161 |
unsigned char* scanline; |
| 162 |
int cscanline; |
| 163 |
|
| 164 |
unsigned char* bitmap; |
| 165 |
int width, height, stride; |
| 166 |
}; |
| 167 |
|
| 168 |
NSVGrasterizer* nsvgCreateRasterizer(void) |
| 169 |
{ |
| 170 |
NSVGrasterizer* r = (NSVGrasterizer*)malloc(sizeof(NSVGrasterizer)); |
| 171 |
if (r == NULL) goto error; |
| 172 |
memset(r, 0, sizeof(NSVGrasterizer)); |
| 173 |
|
| 174 |
r->tessTol = 0.25f; |
| 175 |
r->distTol = 0.01f; |
| 176 |
|
| 177 |
return r; |
| 178 |
|
| 179 |
error: |
| 180 |
nsvgDeleteRasterizer(r); |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
void nsvgDeleteRasterizer(NSVGrasterizer* r) |
| 185 |
{ |
| 186 |
NSVGmemPage* p; |
| 187 |
|
| 188 |
if (r == NULL) return; |
| 189 |
|
| 190 |
p = r->pages; |
| 191 |
while (p != NULL) { |
| 192 |
NSVGmemPage* next = p->next; |
| 193 |
free(p); |
| 194 |
p = next; |
| 195 |
} |
| 196 |
|
| 197 |
if (r->edges) free(r->edges); |
| 198 |
if (r->points) free(r->points); |
| 199 |
if (r->points2) free(r->points2); |
| 200 |
if (r->scanline) free(r->scanline); |
| 201 |
|
| 202 |
free(r); |
| 203 |
} |
| 204 |
|
| 205 |
static NSVGmemPage* nsvg__nextPage(NSVGrasterizer* r, NSVGmemPage* cur) |
| 206 |
{ |
| 207 |
NSVGmemPage *newp; |
| 208 |
|
| 209 |
|
| 210 |
if (cur != NULL && cur->next != NULL) { |
| 211 |
return cur->next; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
newp = (NSVGmemPage*)malloc(sizeof(NSVGmemPage)); |
| 216 |
if (newp == NULL) return NULL; |
| 217 |
memset(newp, 0, sizeof(NSVGmemPage)); |
| 218 |
|
| 219 |
|
| 220 |
if (cur != NULL) |
| 221 |
cur->next = newp; |
| 222 |
else |
| 223 |
r->pages = newp; |
| 224 |
|
| 225 |
return newp; |
| 226 |
} |
| 227 |
|
| 228 |
static void nsvg__resetPool(NSVGrasterizer* r) |
| 229 |
{ |
| 230 |
NSVGmemPage* p = r->pages; |
| 231 |
while (p != NULL) { |
| 232 |
p->size = 0; |
| 233 |
p = p->next; |
| 234 |
} |
| 235 |
r->curpage = r->pages; |
| 236 |
} |
| 237 |
|
| 238 |
static unsigned char* nsvg__alloc(NSVGrasterizer* r, int size) |
| 239 |
{ |
| 240 |
unsigned char* buf; |
| 241 |
if (size > NSVG__MEMPAGE_SIZE) return NULL; |
| 242 |
if (r->curpage == NULL || r->curpage->size+size > NSVG__MEMPAGE_SIZE) { |
| 243 |
r->curpage = nsvg__nextPage(r, r->curpage); |
| 244 |
} |
| 245 |
buf = &r->curpage->mem[r->curpage->size]; |
| 246 |
r->curpage->size += size; |
| 247 |
return buf; |
| 248 |
} |
| 249 |
|
| 250 |
static int nsvg__ptEquals(float x1, float y1, float x2, float y2, float tol) |
| 251 |
{ |
| 252 |
float dx = x2 - x1; |
| 253 |
float dy = y2 - y1; |
| 254 |
return dx*dx + dy*dy < tol*tol; |
| 255 |
} |
| 256 |
|
| 257 |
static void nsvg__addPathPoint(NSVGrasterizer* r, float x, float y, int flags) |
| 258 |
{ |
| 259 |
NSVGpoint* pt; |
| 260 |
|
| 261 |
if (r->npoints > 0) { |
| 262 |
pt = &r->points[r->npoints-1]; |
| 263 |
if (nsvg__ptEquals(pt->x,pt->y, x,y, r->distTol)) { |
| 264 |
pt->flags = (unsigned char)(pt->flags | flags); |
| 265 |
return; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
if (r->npoints+1 > r->cpoints) { |
| 270 |
r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64; |
| 271 |
r->points = (NSVGpoint*)realloc(r->points, sizeof(NSVGpoint) * r->cpoints); |
| 272 |
if (r->points == NULL) return; |
| 273 |
} |
| 274 |
|
| 275 |
pt = &r->points[r->npoints]; |
| 276 |
pt->x = x; |
| 277 |
pt->y = y; |
| 278 |
pt->flags = (unsigned char)flags; |
| 279 |
r->npoints++; |
| 280 |
} |
| 281 |
|
| 282 |
static void nsvg__appendPathPoint(NSVGrasterizer* r, NSVGpoint pt) |
| 283 |
{ |
| 284 |
if (r->npoints+1 > r->cpoints) { |
| 285 |
r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64; |
| 286 |
r->points = (NSVGpoint*)realloc(r->points, sizeof(NSVGpoint) * r->cpoints); |
| 287 |
if (r->points == NULL) return; |
| 288 |
} |
| 289 |
r->points[r->npoints] = pt; |
| 290 |
r->npoints++; |
| 291 |
} |
| 292 |
|
| 293 |
static void nsvg__duplicatePoints(NSVGrasterizer* r) |
| 294 |
{ |
| 295 |
if (r->npoints > r->cpoints2) { |
| 296 |
r->cpoints2 = r->npoints; |
| 297 |
r->points2 = (NSVGpoint*)realloc(r->points2, sizeof(NSVGpoint) * r->cpoints2); |
| 298 |
if (r->points2 == NULL) return; |
| 299 |
} |
| 300 |
|
| 301 |
memcpy(r->points2, r->points, sizeof(NSVGpoint) * r->npoints); |
| 302 |
r->npoints2 = r->npoints; |
| 303 |
} |
| 304 |
|
| 305 |
static void nsvg__addEdge(NSVGrasterizer* r, float x0, float y0, float x1, float y1) |
| 306 |
{ |
| 307 |
NSVGedge* e; |
| 308 |
|
| 309 |
|
| 310 |
if (y0 == y1) |
| 311 |
return; |
| 312 |
|
| 313 |
if (r->nedges+1 > r->cedges) { |
| 314 |
r->cedges = r->cedges > 0 ? r->cedges * 2 : 64; |
| 315 |
r->edges = (NSVGedge*)realloc(r->edges, sizeof(NSVGedge) * r->cedges); |
| 316 |
if (r->edges == NULL) return; |
| 317 |
} |
| 318 |
|
| 319 |
e = &r->edges[r->nedges]; |
| 320 |
r->nedges++; |
| 321 |
|
| 322 |
if (y0 < y1) { |
| 323 |
e->x0 = x0; |
| 324 |
e->y0 = y0; |
| 325 |
e->x1 = x1; |
| 326 |
e->y1 = y1; |
| 327 |
e->dir = 1; |
| 328 |
} else { |
| 329 |
e->x0 = x1; |
| 330 |
e->y0 = y1; |
| 331 |
e->x1 = x0; |
| 332 |
e->y1 = y0; |
| 333 |
e->dir = -1; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
static float nsvg__normalize(float *x, float* y) |
| 338 |
{ |
| 339 |
float d = sqrtf((*x)*(*x) + (*y)*(*y)); |
| 340 |
if (d > 1e-6f) { |
| 341 |
float id = 1.0f / d; |
| 342 |
*x *= id; |
| 343 |
*y *= id; |
| 344 |
} |
| 345 |
return d; |
| 346 |
} |
| 347 |
|
| 348 |
static float nsvg__absf(float x) { return x < 0 ? -x : x; } |
| 349 |
static float nsvg__roundf(float x) { return (x >= 0) ? floorf(x + 0.5) : ceilf(x - 0.5); } |
| 350 |
|
| 351 |
static void nsvg__flattenCubicBez(NSVGrasterizer* r, |
| 352 |
float x1, float y1, float x2, float y2, |
| 353 |
float x3, float y3, float x4, float y4, |
| 354 |
int level, int type) |
| 355 |
{ |
| 356 |
float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234; |
| 357 |
float dx,dy,d2,d3; |
| 358 |
|
| 359 |
if (level > 10) return; |
| 360 |
|
| 361 |
x12 = (x1+x2)*0.5f; |
| 362 |
y12 = (y1+y2)*0.5f; |
| 363 |
x23 = (x2+x3)*0.5f; |
| 364 |
y23 = (y2+y3)*0.5f; |
| 365 |
x34 = (x3+x4)*0.5f; |
| 366 |
y34 = (y3+y4)*0.5f; |
| 367 |
x123 = (x12+x23)*0.5f; |
| 368 |
y123 = (y12+y23)*0.5f; |
| 369 |
|
| 370 |
dx = x4 - x1; |
| 371 |
dy = y4 - y1; |
| 372 |
d2 = nsvg__absf((x2 - x4) * dy - (y2 - y4) * dx); |
| 373 |
d3 = nsvg__absf((x3 - x4) * dy - (y3 - y4) * dx); |
| 374 |
|
| 375 |
if ((d2 + d3)*(d2 + d3) < r->tessTol * (dx*dx + dy*dy)) { |
| 376 |
nsvg__addPathPoint(r, x4, y4, type); |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
x234 = (x23+x34)*0.5f; |
| 381 |
y234 = (y23+y34)*0.5f; |
| 382 |
x1234 = (x123+x234)*0.5f; |
| 383 |
y1234 = (y123+y234)*0.5f; |
| 384 |
|
| 385 |
nsvg__flattenCubicBez(r, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1, 0); |
| 386 |
nsvg__flattenCubicBez(r, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1, type); |
| 387 |
} |
| 388 |
|
| •389 |
static void nsvg__flattenShape(NSVGrasterizer* r, NSVGshape* shape, float sx, float sy) |
| 390 |
{ |
| 391 |
int i, j; |
| 392 |
NSVGpath* path; |
| 393 |
|
| 394 |
for (path = shape->paths; path != NULL; path = path->next) { |
| 395 |
r->npoints = 0; |
| 396 |
|
| •397 |
nsvg__addPathPoint(r, path->pts[0]*sx, path->pts[1]*sy, 0); |
| 398 |
for (i = 0; i < path->npts-1; i += 3) { |
| 399 |
float* p = &path->pts[i*2]; |
| •400 |
nsvg__flattenCubicBez(r, p[0]*sx,p[1]*sy, p[2]*sx,p[3]*sy, p[4]*sx,p[5]*sy, p[6]*sx,p[7]*sy, 0, 0); |
| 401 |
} |
| 402 |
|
| •403 |
nsvg__addPathPoint(r, path->pts[0]*sx, path->pts[1]*sy, 0); |
| 404 |
|
| 405 |
for (i = 0, j = r->npoints-1; i < r->npoints; j = i++) |
| 406 |
nsvg__addEdge(r, r->points[j].x, r->points[j].y, r->points[i].x, r->points[i].y); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
enum NSVGpointFlags |
| 411 |
{ |
| 412 |
NSVG_PT_CORNER = 0x01, |
| 413 |
NSVG_PT_BEVEL = 0x02, |
| 414 |
NSVG_PT_LEFT = 0x04 |
| 415 |
}; |
| 416 |
|
| 417 |
static void nsvg__initClosed(NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth) |
| 418 |
{ |
| 419 |
float w = lineWidth * 0.5f; |
| 420 |
float dx = p1->x - p0->x; |
| 421 |
float dy = p1->y - p0->y; |
| 422 |
float len = nsvg__normalize(&dx, &dy); |
| 423 |
float px = p0->x + dx*len*0.5f, py = p0->y + dy*len*0.5f; |
| 424 |
float dlx = dy, dly = -dx; |
| 425 |
float lx = px - dlx*w, ly = py - dly*w; |
| 426 |
float rx = px + dlx*w, ry = py + dly*w; |
| 427 |
left->x = lx; left->y = ly; |
| 428 |
right->x = rx; right->y = ry; |
| 429 |
} |
| 430 |
|
| 431 |
static void nsvg__buttCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect) |
| 432 |
{ |
| 433 |
float w = lineWidth * 0.5f; |
| 434 |
float px = p->x, py = p->y; |
| 435 |
float dlx = dy, dly = -dx; |
| 436 |
float lx = px - dlx*w, ly = py - dly*w; |
| 437 |
float rx = px + dlx*w, ry = py + dly*w; |
| 438 |
|
| 439 |
nsvg__addEdge(r, lx, ly, rx, ry); |
| 440 |
|
| 441 |
if (connect) { |
| 442 |
nsvg__addEdge(r, left->x, left->y, lx, ly); |
| 443 |
nsvg__addEdge(r, rx, ry, right->x, right->y); |
| 444 |
} |
| 445 |
left->x = lx; left->y = ly; |
| 446 |
right->x = rx; right->y = ry; |
| 447 |
} |
| 448 |
|
| 449 |
static void nsvg__squareCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect) |
| 450 |
{ |
| 451 |
float w = lineWidth * 0.5f; |
| 452 |
float px = p->x - dx*w, py = p->y - dy*w; |
| 453 |
float dlx = dy, dly = -dx; |
| 454 |
float lx = px - dlx*w, ly = py - dly*w; |
| 455 |
float rx = px + dlx*w, ry = py + dly*w; |
| 456 |
|
| 457 |
nsvg__addEdge(r, lx, ly, rx, ry); |
| 458 |
|
| 459 |
if (connect) { |
| 460 |
nsvg__addEdge(r, left->x, left->y, lx, ly); |
| 461 |
nsvg__addEdge(r, rx, ry, right->x, right->y); |
| 462 |
} |
| 463 |
left->x = lx; left->y = ly; |
| 464 |
right->x = rx; right->y = ry; |
| 465 |
} |
| 466 |
|
| 467 |
#ifndef NSVG_PI |
| 468 |
#define NSVG_PI (3.14159265358979323846264338327f) |
| 469 |
#endif |
| 470 |
|
| 471 |
static void nsvg__roundCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int ncap, int connect) |
| 472 |
{ |
| 473 |
int i; |
| 474 |
float w = lineWidth * 0.5f; |
| 475 |
float px = p->x, py = p->y; |
| 476 |
float dlx = dy, dly = -dx; |
| 477 |
float lx = 0, ly = 0, rx = 0, ry = 0, prevx = 0, prevy = 0; |
| 478 |
|
| 479 |
for (i = 0; i < ncap; i++) { |
| 480 |
float a = (float)i/(float)(ncap-1)*NSVG_PI; |
| 481 |
float ax = cosf(a) * w, ay = sinf(a) * w; |
| 482 |
float x = px - dlx*ax - dx*ay; |
| 483 |
float y = py - dly*ax - dy*ay; |
| 484 |
|
| 485 |
if (i > 0) |
| 486 |
nsvg__addEdge(r, prevx, prevy, x, y); |
| 487 |
|
| 488 |
prevx = x; |
| 489 |
prevy = y; |
| 490 |
|
| 491 |
if (i == 0) { |
| 492 |
lx = x; ly = y; |
| 493 |
} else if (i == ncap-1) { |
| 494 |
rx = x; ry = y; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
if (connect) { |
| 499 |
nsvg__addEdge(r, left->x, left->y, lx, ly); |
| 500 |
nsvg__addEdge(r, rx, ry, right->x, right->y); |
| 501 |
} |
| 502 |
|
| 503 |
left->x = lx; left->y = ly; |
| 504 |
right->x = rx; right->y = ry; |
| 505 |
} |
| 506 |
|
| 507 |
static void nsvg__bevelJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth) |
| 508 |
{ |
| 509 |
float w = lineWidth * 0.5f; |
| 510 |
float dlx0 = p0->dy, dly0 = -p0->dx; |
| 511 |
float dlx1 = p1->dy, dly1 = -p1->dx; |
| 512 |
float lx0 = p1->x - (dlx0 * w), ly0 = p1->y - (dly0 * w); |
| 513 |
float rx0 = p1->x + (dlx0 * w), ry0 = p1->y + (dly0 * w); |
| 514 |
float lx1 = p1->x - (dlx1 * w), ly1 = p1->y - (dly1 * w); |
| 515 |
float rx1 = p1->x + (dlx1 * w), ry1 = p1->y + (dly1 * w); |
| 516 |
|
| 517 |
nsvg__addEdge(r, lx0, ly0, left->x, left->y); |
| 518 |
nsvg__addEdge(r, lx1, ly1, lx0, ly0); |
| 519 |
|
| 520 |
nsvg__addEdge(r, right->x, right->y, rx0, ry0); |
| 521 |
nsvg__addEdge(r, rx0, ry0, rx1, ry1); |
| 522 |
|
| 523 |
left->x = lx1; left->y = ly1; |
| 524 |
right->x = rx1; right->y = ry1; |
| 525 |
} |
| 526 |
|
| 527 |
static void nsvg__miterJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth) |
| 528 |
{ |
| 529 |
float w = lineWidth * 0.5f; |
| 530 |
float dlx0 = p0->dy, dly0 = -p0->dx; |
| 531 |
float dlx1 = p1->dy, dly1 = -p1->dx; |
| 532 |
float lx0, rx0, lx1, rx1; |
| 533 |
float ly0, ry0, ly1, ry1; |
| 534 |
|
| 535 |
if (p1->flags & NSVG_PT_LEFT) { |
| 536 |
lx0 = lx1 = p1->x - p1->dmx * w; |
| 537 |
ly0 = ly1 = p1->y - p1->dmy * w; |
| 538 |
nsvg__addEdge(r, lx1, ly1, left->x, left->y); |
| 539 |
|
| 540 |
rx0 = p1->x + (dlx0 * w); |
| 541 |
ry0 = p1->y + (dly0 * w); |
| 542 |
rx1 = p1->x + (dlx1 * w); |
| 543 |
ry1 = p1->y + (dly1 * w); |
| 544 |
nsvg__addEdge(r, right->x, right->y, rx0, ry0); |
| 545 |
nsvg__addEdge(r, rx0, ry0, rx1, ry1); |
| 546 |
} else { |
| 547 |
lx0 = p1->x - (dlx0 * w); |
| 548 |
ly0 = p1->y - (dly0 * w); |
| 549 |
lx1 = p1->x - (dlx1 * w); |
| 550 |
ly1 = p1->y - (dly1 * w); |
| 551 |
nsvg__addEdge(r, lx0, ly0, left->x, left->y); |
| 552 |
nsvg__addEdge(r, lx1, ly1, lx0, ly0); |
| 553 |
|
| 554 |
rx0 = rx1 = p1->x + p1->dmx * w; |
| 555 |
ry0 = ry1 = p1->y + p1->dmy * w; |
| 556 |
nsvg__addEdge(r, right->x, right->y, rx1, ry1); |
| 557 |
} |
| 558 |
|
| 559 |
left->x = lx1; left->y = ly1; |
| 560 |
right->x = rx1; right->y = ry1; |
| 561 |
} |
| 562 |
|
| 563 |
static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth, int ncap) |
| 564 |
{ |
| 565 |
int i, n; |
| 566 |
float w = lineWidth * 0.5f; |
| 567 |
float dlx0 = p0->dy, dly0 = -p0->dx; |
| 568 |
float dlx1 = p1->dy, dly1 = -p1->dx; |
| 569 |
float a0 = atan2f(dly0, dlx0); |
| 570 |
float a1 = atan2f(dly1, dlx1); |
| 571 |
float da = a1 - a0; |
| 572 |
float lx, ly, rx, ry; |
| 573 |
|
| 574 |
if (da < NSVG_PI) da += NSVG_PI*2; |
| 575 |
if (da > NSVG_PI) da -= NSVG_PI*2; |
| 576 |
|
| 577 |
n = (int)ceilf((nsvg__absf(da) / NSVG_PI) * (float)ncap); |
| 578 |
if (n < 2) n = 2; |
| 579 |
if (n > ncap) n = ncap; |
| 580 |
|
| 581 |
lx = left->x; |
| 582 |
ly = left->y; |
| 583 |
rx = right->x; |
| 584 |
ry = right->y; |
| 585 |
|
| 586 |
for (i = 0; i < n; i++) { |
| 587 |
float u = (float)i/(float)(n-1); |
| 588 |
float a = a0 + u*da; |
| 589 |
float ax = cosf(a) * w, ay = sinf(a) * w; |
| 590 |
float lx1 = p1->x - ax, ly1 = p1->y - ay; |
| 591 |
float rx1 = p1->x + ax, ry1 = p1->y + ay; |
| 592 |
|
| 593 |
nsvg__addEdge(r, lx1, ly1, lx, ly); |
| 594 |
nsvg__addEdge(r, rx, ry, rx1, ry1); |
| 595 |
|
| 596 |
lx = lx1; ly = ly1; |
| 597 |
rx = rx1; ry = ry1; |
| 598 |
} |
| 599 |
|
| 600 |
left->x = lx; left->y = ly; |
| 601 |
right->x = rx; right->y = ry; |
| 602 |
} |
| 603 |
|
| 604 |
static void nsvg__straightJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p1, float lineWidth) |
| 605 |
{ |
| 606 |
float w = lineWidth * 0.5f; |
| 607 |
float lx = p1->x - (p1->dmx * w), ly = p1->y - (p1->dmy * w); |
| 608 |
float rx = p1->x + (p1->dmx * w), ry = p1->y + (p1->dmy * w); |
| 609 |
|
| 610 |
nsvg__addEdge(r, lx, ly, left->x, left->y); |
| 611 |
nsvg__addEdge(r, right->x, right->y, rx, ry); |
| 612 |
|
| 613 |
left->x = lx; left->y = ly; |
| 614 |
right->x = rx; right->y = ry; |
| 615 |
} |
| 616 |
|
| 617 |
static int nsvg__curveDivs(float r, float arc, float tol) |
| 618 |
{ |
| 619 |
float da = acosf(r / (r + tol)) * 2.0f; |
| 620 |
int divs = (int)ceilf(arc / da); |
| 621 |
if (divs < 2) divs = 2; |
| 622 |
return divs; |
| 623 |
} |
| 624 |
|
| 625 |
static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points, int npoints, int closed, int lineJoin, int lineCap, float lineWidth) |
| 626 |
{ |
| 627 |
int ncap = nsvg__curveDivs(lineWidth*0.5f, NSVG_PI, r->tessTol); |
| 628 |
NSVGpoint left = {0,0,0,0,0,0,0,0}, right = {0,0,0,0,0,0,0,0}, firstLeft = {0,0,0,0,0,0,0,0}, firstRight = {0,0,0,0,0,0,0,0}; |
| 629 |
NSVGpoint* p0, *p1; |
| 630 |
int j, s, e; |
| 631 |
|
| 632 |
|
| 633 |
if (closed) { |
| 634 |
|
| 635 |
p0 = &points[npoints-1]; |
| 636 |
p1 = &points[0]; |
| 637 |
s = 0; |
| 638 |
e = npoints; |
| 639 |
} else { |
| 640 |
|
| 641 |
p0 = &points[0]; |
| 642 |
p1 = &points[1]; |
| 643 |
s = 1; |
| 644 |
e = npoints-1; |
| 645 |
} |
| 646 |
|
| 647 |
if (closed) { |
| 648 |
nsvg__initClosed(&left, &right, p0, p1, lineWidth); |
| 649 |
firstLeft = left; |
| 650 |
firstRight = right; |
| 651 |
} else { |
| 652 |
|
| 653 |
float dx = p1->x - p0->x; |
| 654 |
float dy = p1->y - p0->y; |
| 655 |
nsvg__normalize(&dx, &dy); |
| 656 |
if (lineCap == NSVG_CAP_BUTT) |
| 657 |
nsvg__buttCap(r, &left, &right, p0, dx, dy, lineWidth, 0); |
| 658 |
else if (lineCap == NSVG_CAP_SQUARE) |
| 659 |
nsvg__squareCap(r, &left, &right, p0, dx, dy, lineWidth, 0); |
| 660 |
else if (lineCap == NSVG_CAP_ROUND) |
| 661 |
nsvg__roundCap(r, &left, &right, p0, dx, dy, lineWidth, ncap, 0); |
| 662 |
} |
| 663 |
|
| 664 |
for (j = s; j < e; ++j) { |
| 665 |
if (p1->flags & NSVG_PT_CORNER) { |
| 666 |
if (lineJoin == NSVG_JOIN_ROUND) |
| 667 |
nsvg__roundJoin(r, &left, &right, p0, p1, lineWidth, ncap); |
| 668 |
else if (lineJoin == NSVG_JOIN_BEVEL || (p1->flags & NSVG_PT_BEVEL)) |
| 669 |
nsvg__bevelJoin(r, &left, &right, p0, p1, lineWidth); |
| 670 |
else |
| 671 |
nsvg__miterJoin(r, &left, &right, p0, p1, lineWidth); |
| 672 |
} else { |
| 673 |
nsvg__straightJoin(r, &left, &right, p1, lineWidth); |
| 674 |
} |
| 675 |
p0 = p1++; |
| 676 |
} |
| 677 |
|
| 678 |
if (closed) { |
| 679 |
|
| 680 |
nsvg__addEdge(r, firstLeft.x, firstLeft.y, left.x, left.y); |
| 681 |
nsvg__addEdge(r, right.x, right.y, firstRight.x, firstRight.y); |
| 682 |
} else { |
| 683 |
|
| 684 |
float dx = p1->x - p0->x; |
| 685 |
float dy = p1->y - p0->y; |
| 686 |
nsvg__normalize(&dx, &dy); |
| 687 |
if (lineCap == NSVG_CAP_BUTT) |
| 688 |
nsvg__buttCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1); |
| 689 |
else if (lineCap == NSVG_CAP_SQUARE) |
| 690 |
nsvg__squareCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1); |
| 691 |
else if (lineCap == NSVG_CAP_ROUND) |
| 692 |
nsvg__roundCap(r, &right, &left, p1, -dx, -dy, lineWidth, ncap, 1); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
static void nsvg__prepareStroke(NSVGrasterizer* r, float miterLimit, int lineJoin) |
| 697 |
{ |
| 698 |
int i, j; |
| 699 |
NSVGpoint* p0, *p1; |
| 700 |
|
| 701 |
p0 = &r->points[r->npoints-1]; |
| 702 |
p1 = &r->points[0]; |
| 703 |
for (i = 0; i < r->npoints; i++) { |
| 704 |
|
| 705 |
p0->dx = p1->x - p0->x; |
| 706 |
p0->dy = p1->y - p0->y; |
| 707 |
p0->len = nsvg__normalize(&p0->dx, &p0->dy); |
| 708 |
|
| 709 |
p0 = p1++; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
p0 = &r->points[r->npoints-1]; |
| 714 |
p1 = &r->points[0]; |
| 715 |
for (j = 0; j < r->npoints; j++) { |
| 716 |
float dlx0, dly0, dlx1, dly1, dmr2, cross; |
| 717 |
dlx0 = p0->dy; |
| 718 |
dly0 = -p0->dx; |
| 719 |
dlx1 = p1->dy; |
| 720 |
dly1 = -p1->dx; |
| 721 |
|
| 722 |
p1->dmx = (dlx0 + dlx1) * 0.5f; |
| 723 |
p1->dmy = (dly0 + dly1) * 0.5f; |
| 724 |
dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy; |
| 725 |
if (dmr2 > 0.000001f) { |
| 726 |
float s2 = 1.0f / dmr2; |
| 727 |
if (s2 > 600.0f) { |
| 728 |
s2 = 600.0f; |
| 729 |
} |
| 730 |
p1->dmx *= s2; |
| 731 |
p1->dmy *= s2; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
p1->flags = (p1->flags & NSVG_PT_CORNER) ? NSVG_PT_CORNER : 0; |
| 736 |
|
| 737 |
|
| 738 |
cross = p1->dx * p0->dy - p0->dx * p1->dy; |
| 739 |
if (cross > 0.0f) |
| 740 |
p1->flags |= NSVG_PT_LEFT; |
| 741 |
|
| 742 |
|
| 743 |
if (p1->flags & NSVG_PT_CORNER) { |
| 744 |
if ((dmr2 * miterLimit*miterLimit) < 1.0f || lineJoin == NSVG_JOIN_BEVEL || lineJoin == NSVG_JOIN_ROUND) { |
| 745 |
p1->flags |= NSVG_PT_BEVEL; |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
p0 = p1++; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| •753 |
static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float sx, float sy) |
| 754 |
{ |
| 755 |
int i, j, closed; |
| 756 |
NSVGpath* path; |
| 757 |
NSVGpoint* p0, *p1; |
| 758 |
float miterLimit = shape->miterLimit; |
| 759 |
int lineJoin = shape->strokeLineJoin; |
| 760 |
int lineCap = shape->strokeLineCap; |
| •761 |
const float sw = (sx + sy) / 2; |
| 762 |
const float lineWidth = shape->strokeWidth * sw; |
| 763 |
|
| 764 |
for (path = shape->paths; path != NULL; path = path->next) { |
| 765 |
|
| 766 |
r->npoints = 0; |
| •767 |
nsvg__addPathPoint(r, path->pts[0]*sx, path->pts[1]*sy, NSVG_PT_CORNER); |
| 768 |
for (i = 0; i < path->npts-1; i += 3) { |
| 769 |
float* p = &path->pts[i*2]; |
| •770 |
nsvg__flattenCubicBez(r, p[0]*sx,p[1]*sy, p[2]*sx,p[3]*sy, p[4]*sx,p[5]*sy, p[6]*sx,p[7]*sy, 0, NSVG_PT_CORNER); |
| 771 |
} |
| 772 |
if (r->npoints < 2) |
| 773 |
continue; |
| 774 |
|
| 775 |
closed = path->closed; |
| 776 |
|
| 777 |
|
| 778 |
p0 = &r->points[r->npoints-1]; |
| 779 |
p1 = &r->points[0]; |
| 780 |
if (nsvg__ptEquals(p0->x,p0->y, p1->x,p1->y, r->distTol)) { |
| 781 |
r->npoints--; |
| 782 |
p0 = &r->points[r->npoints-1]; |
| 783 |
closed = 1; |
| 784 |
} |
| 785 |
|
| 786 |
if (shape->strokeDashCount > 0) { |
| 787 |
int idash = 0, dashState = 1; |
| 788 |
float totalDist = 0, dashLen, allDashLen, dashOffset; |
| 789 |
NSVGpoint cur; |
| 790 |
|
| 791 |
if (closed) |
| 792 |
nsvg__appendPathPoint(r, r->points[0]); |
| 793 |
|
| 794 |
|
| 795 |
nsvg__duplicatePoints(r); |
| 796 |
|
| 797 |
r->npoints = 0; |
| 798 |
cur = r->points2[0]; |
| 799 |
nsvg__appendPathPoint(r, cur); |
| 800 |
|
| 801 |
|
| 802 |
allDashLen = 0; |
| 803 |
for (j = 0; j < shape->strokeDashCount; j++) |
| 804 |
allDashLen += shape->strokeDashArray[j]; |
| 805 |
if (shape->strokeDashCount & 1) |
| 806 |
allDashLen *= 2.0f; |
| 807 |
|
| 808 |
dashOffset = fmodf(shape->strokeDashOffset, allDashLen); |
| 809 |
if (dashOffset < 0.0f) |
| 810 |
dashOffset += allDashLen; |
| 811 |
|
| 812 |
while (dashOffset > shape->strokeDashArray[idash]) { |
| 813 |
dashOffset -= shape->strokeDashArray[idash]; |
| 814 |
idash = (idash + 1) % shape->strokeDashCount; |
| 815 |
} |
| •816 |
dashLen = (shape->strokeDashArray[idash] - dashOffset) * sw; |
| 817 |
|
| 818 |
for (j = 1; j < r->npoints2; ) { |
| 819 |
float dx = r->points2[j].x - cur.x; |
| 820 |
float dy = r->points2[j].y - cur.y; |
| 821 |
float dist = sqrtf(dx*dx + dy*dy); |
| 822 |
|
| 823 |
if ((totalDist + dist) > dashLen) { |
| 824 |
|
| 825 |
float d = (dashLen - totalDist) / dist; |
| 826 |
float x = cur.x + dx * d; |
| 827 |
float y = cur.y + dy * d; |
| 828 |
nsvg__addPathPoint(r, x, y, NSVG_PT_CORNER); |
| 829 |
|
| 830 |
|
| 831 |
if (r->npoints > 1 && dashState) { |
| 832 |
nsvg__prepareStroke(r, miterLimit, lineJoin); |
| 833 |
nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth); |
| 834 |
} |
| 835 |
|
| 836 |
dashState = !dashState; |
| 837 |
idash = (idash+1) % shape->strokeDashCount; |
| •838 |
dashLen = shape->strokeDashArray[idash] * sw; |
| 839 |
|
| 840 |
cur.x = x; |
| 841 |
cur.y = y; |
| 842 |
cur.flags = NSVG_PT_CORNER; |
| 843 |
totalDist = 0.0f; |
| 844 |
r->npoints = 0; |
| 845 |
nsvg__appendPathPoint(r, cur); |
| 846 |
} else { |
| 847 |
totalDist += dist; |
| 848 |
cur = r->points2[j]; |
| 849 |
nsvg__appendPathPoint(r, cur); |
| 850 |
j++; |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
if (r->npoints > 1 && dashState) { |
| 855 |
nsvg__prepareStroke(r, miterLimit, lineJoin); |
| 856 |
nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth); |
| 857 |
} |
| 858 |
} else { |
| 859 |
nsvg__prepareStroke(r, miterLimit, lineJoin); |
| 860 |
nsvg__expandStroke(r, r->points, r->npoints, closed, lineJoin, lineCap, lineWidth); |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
static int nsvg__cmpEdge(const void *p, const void *q) |
| 866 |
{ |
| 867 |
const NSVGedge* a = (const NSVGedge*)p; |
| 868 |
const NSVGedge* b = (const NSVGedge*)q; |
| 869 |
|
| 870 |
if (a->y0 < b->y0) return -1; |
| 871 |
if (a->y0 > b->y0) return 1; |
| 872 |
return 0; |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
static NSVGactiveEdge* nsvg__addActive(NSVGrasterizer* r, NSVGedge* e, float startPoint) |
| 877 |
{ |
| 878 |
NSVGactiveEdge* z; |
| 879 |
|
| 880 |
if (r->freelist != NULL) { |
| 881 |
|
| 882 |
z = r->freelist; |
| 883 |
r->freelist = z->next; |
| 884 |
} else { |
| 885 |
|
| 886 |
z = (NSVGactiveEdge*)nsvg__alloc(r, sizeof(NSVGactiveEdge)); |
| 887 |
if (z == NULL) return NULL; |
| 888 |
} |
| 889 |
|
| 890 |
float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); |
| 891 |
|
| 892 |
|
| 893 |
if (dxdy < 0) |
| 894 |
z->dx = (int)(-nsvg__roundf(NSVG__FIX * -dxdy)); |
| 895 |
else |
| 896 |
z->dx = (int)nsvg__roundf(NSVG__FIX * dxdy); |
| 897 |
z->x = (int)nsvg__roundf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0))); |
| 898 |
|
| 899 |
z->ey = e->y1; |
| 900 |
z->next = 0; |
| 901 |
z->dir = e->dir; |
| 902 |
|
| 903 |
return z; |
| 904 |
} |
| 905 |
|
| 906 |
static void nsvg__freeActive(NSVGrasterizer* r, NSVGactiveEdge* z) |
| 907 |
{ |
| 908 |
z->next = r->freelist; |
| 909 |
r->freelist = z; |
| 910 |
} |
| 911 |
|
| 912 |
static void nsvg__fillScanline(unsigned char* scanline, int len, int x0, int x1, int maxWeight, int* xmin, int* xmax) |
| 913 |
{ |
| 914 |
int i = x0 >> NSVG__FIXSHIFT; |
| 915 |
int j = x1 >> NSVG__FIXSHIFT; |
| 916 |
if (i < *xmin) *xmin = i; |
| 917 |
if (j > *xmax) *xmax = j; |
| 918 |
if (i < len && j >= 0) { |
| 919 |
if (i == j) { |
| 920 |
|
| 921 |
scanline[i] = (unsigned char)(scanline[i] + ((x1 - x0) * maxWeight >> NSVG__FIXSHIFT)); |
| 922 |
} else { |
| 923 |
if (i >= 0) |
| 924 |
scanline[i] = (unsigned char)(scanline[i] + (((NSVG__FIX - (x0 & NSVG__FIXMASK)) * maxWeight) >> NSVG__FIXSHIFT)); |
| 925 |
else |
| 926 |
i = -1; |
| 927 |
|
| 928 |
if (j < len) |
| 929 |
scanline[j] = (unsigned char)(scanline[j] + (((x1 & NSVG__FIXMASK) * maxWeight) >> NSVG__FIXSHIFT)); |
| 930 |
else |
| 931 |
j = len; |
| 932 |
|
| 933 |
for (++i; i < j; ++i) |
| 934 |
scanline[i] = (unsigned char)(scanline[i] + maxWeight); |
| 935 |
} |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
static void nsvg__fillActiveEdges(unsigned char* scanline, int len, NSVGactiveEdge* e, int maxWeight, int* xmin, int* xmax, char fillRule) |
| 943 |
{ |
| 944 |
|
| 945 |
int x0 = 0, w = 0; |
| 946 |
|
| 947 |
if (fillRule == NSVG_FILLRULE_NONZERO) { |
| 948 |
|
| 949 |
while (e != NULL) { |
| 950 |
if (w == 0) { |
| 951 |
|
| 952 |
x0 = e->x; w += e->dir; |
| 953 |
} else { |
| 954 |
int x1 = e->x; w += e->dir; |
| 955 |
|
| 956 |
if (w == 0) |
| 957 |
nsvg__fillScanline(scanline, len, x0, x1, maxWeight, xmin, xmax); |
| 958 |
} |
| 959 |
e = e->next; |
| 960 |
} |
| 961 |
} else if (fillRule == NSVG_FILLRULE_EVENODD) { |
| 962 |
|
| 963 |
while (e != NULL) { |
| 964 |
if (w == 0) { |
| 965 |
|
| 966 |
x0 = e->x; w = 1; |
| 967 |
} else { |
| 968 |
int x1 = e->x; w = 0; |
| 969 |
nsvg__fillScanline(scanline, len, x0, x1, maxWeight, xmin, xmax); |
| 970 |
} |
| 971 |
e = e->next; |
| 972 |
} |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
static float nsvg__clampf(float a, float mn, float mx) { |
| 977 |
if (isnan(a)) |
| 978 |
return mn; |
| 979 |
return a < mn ? mn : (a > mx ? mx : a); |
| 980 |
} |
| 981 |
|
| 982 |
static unsigned int nsvg__RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 983 |
{ |
| 984 |
return ((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16) | ((unsigned int)a << 24); |
| 985 |
} |
| 986 |
|
| 987 |
static unsigned int nsvg__lerpRGBA(unsigned int c0, unsigned int c1, float u) |
| 988 |
{ |
| 989 |
int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f); |
| 990 |
int r = (((c0) & 0xff)*(256-iu) + (((c1) & 0xff)*iu)) >> 8; |
| 991 |
int g = (((c0>>8) & 0xff)*(256-iu) + (((c1>>8) & 0xff)*iu)) >> 8; |
| 992 |
int b = (((c0>>16) & 0xff)*(256-iu) + (((c1>>16) & 0xff)*iu)) >> 8; |
| 993 |
int a = (((c0>>24) & 0xff)*(256-iu) + (((c1>>24) & 0xff)*iu)) >> 8; |
| 994 |
return nsvg__RGBA((unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a); |
| 995 |
} |
| 996 |
|
| 997 |
static unsigned int nsvg__applyOpacity(unsigned int c, float u) |
| 998 |
{ |
| 999 |
int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f); |
| 1000 |
int r = (c) & 0xff; |
| 1001 |
int g = (c>>8) & 0xff; |
| 1002 |
int b = (c>>16) & 0xff; |
| 1003 |
int a = (((c>>24) & 0xff)*iu) >> 8; |
| 1004 |
return nsvg__RGBA((unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a); |
| 1005 |
} |
| 1006 |
|
| 1007 |
static inline int nsvg__div255(int x) |
| 1008 |
{ |
| 1009 |
return ((x+1) * 257) >> 16; |
| 1010 |
} |
| 1011 |
|
| 1012 |
static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* cover, int x, int y, |
| •1013 |
float tx, float ty, float sx, float sy, NSVGcachedPaint* cache) |
| 1014 |
{ |
| 1015 |
|
| 1016 |
if (cache->type == NSVG_PAINT_COLOR) { |
| 1017 |
int i, cr, cg, cb, ca; |
| 1018 |
cr = cache->colors[0] & 0xff; |
| 1019 |
cg = (cache->colors[0] >> 8) & 0xff; |
| 1020 |
cb = (cache->colors[0] >> 16) & 0xff; |
| 1021 |
ca = (cache->colors[0] >> 24) & 0xff; |
| 1022 |
|
| 1023 |
for (i = 0; i < count; i++) { |
| 1024 |
int r,g,b; |
| 1025 |
int a = nsvg__div255((int)cover[0] * ca); |
| 1026 |
int ia = 255 - a; |
| 1027 |
|
| 1028 |
r = nsvg__div255(cr * a); |
| 1029 |
g = nsvg__div255(cg * a); |
| 1030 |
b = nsvg__div255(cb * a); |
| 1031 |
|
| 1032 |
|
| 1033 |
r += nsvg__div255(ia * (int)dst[0]); |
| 1034 |
g += nsvg__div255(ia * (int)dst[1]); |
| 1035 |
b += nsvg__div255(ia * (int)dst[2]); |
| 1036 |
a += nsvg__div255(ia * (int)dst[3]); |
| 1037 |
|
| 1038 |
dst[0] = (unsigned char)r; |
| 1039 |
dst[1] = (unsigned char)g; |
| 1040 |
dst[2] = (unsigned char)b; |
| 1041 |
dst[3] = (unsigned char)a; |
| 1042 |
|
| 1043 |
cover++; |
| 1044 |
dst += 4; |
| 1045 |
} |
| 1046 |
} else if (cache->type == NSVG_PAINT_LINEAR_GRADIENT) { |
| 1047 |
|
| 1048 |
|
| 1049 |
float fx, fy, dx, gy; |
| 1050 |
float* t = cache->xform; |
| 1051 |
int i, cr, cg, cb, ca; |
| 1052 |
unsigned int c; |
| 1053 |
|
| •1054 |
fx = ((float)x - tx) / sx; |
| 1055 |
fy = ((float)y - ty) / sy; |
| 1056 |
dx = 1.0f / sx; |
| 1057 |
|
| 1058 |
for (i = 0; i < count; i++) { |
| 1059 |
int r,g,b,a,ia; |
| 1060 |
gy = fx*t[1] + fy*t[3] + t[5]; |
| 1061 |
c = cache->colors[(int)nsvg__clampf(gy*255.0f, 0, 255.0f)]; |
| 1062 |
cr = (c) & 0xff; |
| 1063 |
cg = (c >> 8) & 0xff; |
| 1064 |
cb = (c >> 16) & 0xff; |
| 1065 |
ca = (c >> 24) & 0xff; |
| 1066 |
|
| 1067 |
a = nsvg__div255((int)cover[0] * ca); |
| 1068 |
ia = 255 - a; |
| 1069 |
|
| 1070 |
|
| 1071 |
r = nsvg__div255(cr * a); |
| 1072 |
g = nsvg__div255(cg * a); |
| 1073 |
b = nsvg__div255(cb * a); |
| 1074 |
|
| 1075 |
|
| 1076 |
r += nsvg__div255(ia * (int)dst[0]); |
| 1077 |
g += nsvg__div255(ia * (int)dst[1]); |
| 1078 |
b += nsvg__div255(ia * (int)dst[2]); |
| 1079 |
a += nsvg__div255(ia * (int)dst[3]); |
| 1080 |
|
| 1081 |
dst[0] = (unsigned char)r; |
| 1082 |
dst[1] = (unsigned char)g; |
| 1083 |
dst[2] = (unsigned char)b; |
| 1084 |
dst[3] = (unsigned char)a; |
| 1085 |
|
| 1086 |
cover++; |
| 1087 |
dst += 4; |
| 1088 |
fx += dx; |
| 1089 |
} |
| 1090 |
} else if (cache->type == NSVG_PAINT_RADIAL_GRADIENT) { |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
float fx, fy, dx, gx, gy, gd; |
| 1095 |
float* t = cache->xform; |
| 1096 |
int i, cr, cg, cb, ca; |
| 1097 |
unsigned int c; |
| 1098 |
|
| •1099 |
fx = ((float)x - tx) / sx; |
| 1100 |
fy = ((float)y - ty) / sy; |
| 1101 |
dx = 1.0f / sx; |
| 1102 |
|
| 1103 |
for (i = 0; i < count; i++) { |
| 1104 |
int r,g,b,a,ia; |
| 1105 |
gx = fx*t[0] + fy*t[2] + t[4]; |
| 1106 |
gy = fx*t[1] + fy*t[3] + t[5]; |
| 1107 |
gd = sqrtf(gx*gx + gy*gy); |
| 1108 |
c = cache->colors[(int)nsvg__clampf(gd*255.0f, 0, 255.0f)]; |
| 1109 |
cr = (c) & 0xff; |
| 1110 |
cg = (c >> 8) & 0xff; |
| 1111 |
cb = (c >> 16) & 0xff; |
| 1112 |
ca = (c >> 24) & 0xff; |
| 1113 |
|
| 1114 |
a = nsvg__div255((int)cover[0] * ca); |
| 1115 |
ia = 255 - a; |
| 1116 |
|
| 1117 |
|
| 1118 |
r = nsvg__div255(cr * a); |
| 1119 |
g = nsvg__div255(cg * a); |
| 1120 |
b = nsvg__div255(cb * a); |
| 1121 |
|
| 1122 |
|
| 1123 |
r += nsvg__div255(ia * (int)dst[0]); |
| 1124 |
g += nsvg__div255(ia * (int)dst[1]); |
| 1125 |
b += nsvg__div255(ia * (int)dst[2]); |
| 1126 |
a += nsvg__div255(ia * (int)dst[3]); |
| 1127 |
|
| 1128 |
dst[0] = (unsigned char)r; |
| 1129 |
dst[1] = (unsigned char)g; |
| 1130 |
dst[2] = (unsigned char)b; |
| 1131 |
dst[3] = (unsigned char)a; |
| 1132 |
|
| 1133 |
cover++; |
| 1134 |
dst += 4; |
| 1135 |
fx += dx; |
| 1136 |
} |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| •1140 |
static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, float sx, float sy, NSVGcachedPaint* cache, char fillRule) |
| 1141 |
{ |
| 1142 |
NSVGactiveEdge *active = NULL; |
| 1143 |
int y, s; |
| 1144 |
int e = 0; |
| 1145 |
int maxWeight = (255 / NSVG__SUBSAMPLES); |
| 1146 |
int xmin, xmax; |
| 1147 |
|
| 1148 |
for (y = 0; y < r->height; y++) { |
| 1149 |
memset(r->scanline, 0, r->width); |
| 1150 |
xmin = r->width; |
| 1151 |
xmax = 0; |
| 1152 |
for (s = 0; s < NSVG__SUBSAMPLES; ++s) { |
| 1153 |
|
| 1154 |
float scany = (float)(y*NSVG__SUBSAMPLES + s) + 0.5f; |
| 1155 |
NSVGactiveEdge **step = &active; |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
while (*step) { |
| 1160 |
NSVGactiveEdge *z = *step; |
| 1161 |
if (z->ey <= scany) { |
| 1162 |
*step = z->next; |
| 1163 |
|
| 1164 |
nsvg__freeActive(r, z); |
| 1165 |
} else { |
| 1166 |
z->x += z->dx; |
| 1167 |
step = &((*step)->next); |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
for (;;) { |
| 1173 |
int changed = 0; |
| 1174 |
step = &active; |
| 1175 |
while (*step && (*step)->next) { |
| 1176 |
if ((*step)->x > (*step)->next->x) { |
| 1177 |
NSVGactiveEdge* t = *step; |
| 1178 |
NSVGactiveEdge* q = t->next; |
| 1179 |
t->next = q->next; |
| 1180 |
q->next = t; |
| 1181 |
*step = q; |
| 1182 |
changed = 1; |
| 1183 |
} |
| 1184 |
step = &(*step)->next; |
| 1185 |
} |
| 1186 |
if (!changed) break; |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
while (e < r->nedges && r->edges[e].y0 <= scany) { |
| 1191 |
if (r->edges[e].y1 > scany) { |
| 1192 |
NSVGactiveEdge* z = nsvg__addActive(r, &r->edges[e], scany); |
| 1193 |
if (z == NULL) break; |
| 1194 |
|
| 1195 |
if (active == NULL) { |
| 1196 |
active = z; |
| 1197 |
} else if (z->x < active->x) { |
| 1198 |
|
| 1199 |
z->next = active; |
| 1200 |
active = z; |
| 1201 |
} else { |
| 1202 |
|
| 1203 |
NSVGactiveEdge* p = active; |
| 1204 |
while (p->next && p->next->x < z->x) |
| 1205 |
p = p->next; |
| 1206 |
|
| 1207 |
z->next = p->next; |
| 1208 |
p->next = z; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
e++; |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
if (active != NULL) |
| 1216 |
nsvg__fillActiveEdges(r->scanline, r->width, active, maxWeight, &xmin, &xmax, fillRule); |
| 1217 |
} |
| 1218 |
|
| 1219 |
if (xmin < 0) xmin = 0; |
| 1220 |
if (xmax > r->width-1) xmax = r->width-1; |
| 1221 |
if (xmin <= xmax) { |
| •1222 |
nsvg__scanlineSolid(&r->bitmap[y * r->stride] + xmin*4, xmax-xmin+1, &r->scanline[xmin], xmin, y, tx,ty, sx, sy, cache); |
| 1223 |
} |
| 1224 |
} |
| 1225 |
|
| 1226 |
} |
| 1227 |
|
| 1228 |
static void nsvg__unpremultiplyAlpha(unsigned char* image, int w, int h, int stride) |
| 1229 |
{ |
| 1230 |
int x,y; |
| 1231 |
|
| 1232 |
|
| 1233 |
for (y = 0; y < h; y++) { |
| 1234 |
unsigned char *row = &image[y*stride]; |
| 1235 |
for (x = 0; x < w; x++) { |
| 1236 |
int r = row[0], g = row[1], b = row[2], a = row[3]; |
| 1237 |
if (a != 0) { |
| 1238 |
row[0] = (unsigned char)(r*255/a); |
| 1239 |
row[1] = (unsigned char)(g*255/a); |
| 1240 |
row[2] = (unsigned char)(b*255/a); |
| 1241 |
} |
| 1242 |
row += 4; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
for (y = 0; y < h; y++) { |
| 1248 |
unsigned char *row = &image[y*stride]; |
| 1249 |
for (x = 0; x < w; x++) { |
| 1250 |
int r = 0, g = 0, b = 0, a = row[3], n = 0; |
| 1251 |
if (a == 0) { |
| 1252 |
if (x-1 > 0 && row[-1] != 0) { |
| 1253 |
r += row[-4]; |
| 1254 |
g += row[-3]; |
| 1255 |
b += row[-2]; |
| 1256 |
n++; |
| 1257 |
} |
| 1258 |
if (x+1 < w && row[7] != 0) { |
| 1259 |
r += row[4]; |
| 1260 |
g += row[5]; |
| 1261 |
b += row[6]; |
| 1262 |
n++; |
| 1263 |
} |
| 1264 |
if (y-1 > 0 && row[-stride+3] != 0) { |
| 1265 |
r += row[-stride]; |
| 1266 |
g += row[-stride+1]; |
| 1267 |
b += row[-stride+2]; |
| 1268 |
n++; |
| 1269 |
} |
| 1270 |
if (y+1 < h && row[stride+3] != 0) { |
| 1271 |
r += row[stride]; |
| 1272 |
g += row[stride+1]; |
| 1273 |
b += row[stride+2]; |
| 1274 |
n++; |
| 1275 |
} |
| 1276 |
if (n > 0) { |
| 1277 |
row[0] = (unsigned char)(r/n); |
| 1278 |
row[1] = (unsigned char)(g/n); |
| 1279 |
row[2] = (unsigned char)(b/n); |
| 1280 |
} |
| 1281 |
} |
| 1282 |
row += 4; |
| 1283 |
} |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opacity) |
| 1289 |
{ |
| 1290 |
int i, j; |
| 1291 |
NSVGgradient* grad; |
| 1292 |
|
| 1293 |
cache->type = paint->type; |
| 1294 |
|
| 1295 |
if (paint->type == NSVG_PAINT_COLOR) { |
| 1296 |
cache->colors[0] = nsvg__applyOpacity(paint->color, opacity); |
| 1297 |
return; |
| 1298 |
} |
| 1299 |
|
| 1300 |
grad = paint->gradient; |
| 1301 |
|
| 1302 |
cache->spread = grad->spread; |
| 1303 |
memcpy(cache->xform, grad->xform, sizeof(float)*6); |
| 1304 |
|
| 1305 |
if (grad->nstops == 0) { |
| 1306 |
for (i = 0; i < 256; i++) |
| 1307 |
cache->colors[i] = 0; |
| 1308 |
} else if (grad->nstops == 1) { |
| 1309 |
unsigned int color = nsvg__applyOpacity(grad->stops[0].color, opacity); |
| 1310 |
for (i = 0; i < 256; i++) |
| 1311 |
cache->colors[i] = color; |
| 1312 |
} else { |
| 1313 |
unsigned int ca, cb = 0; |
| 1314 |
float ua, ub, du, u; |
| 1315 |
int ia, ib, count; |
| 1316 |
|
| 1317 |
ca = nsvg__applyOpacity(grad->stops[0].color, opacity); |
| 1318 |
ua = nsvg__clampf(grad->stops[0].offset, 0, 1); |
| 1319 |
ub = nsvg__clampf(grad->stops[grad->nstops-1].offset, ua, 1); |
| 1320 |
ia = (int)(ua * 255.0f); |
| 1321 |
ib = (int)(ub * 255.0f); |
| 1322 |
for (i = 0; i < ia; i++) { |
| 1323 |
cache->colors[i] = ca; |
| 1324 |
} |
| 1325 |
|
| 1326 |
for (i = 0; i < grad->nstops-1; i++) { |
| 1327 |
ca = nsvg__applyOpacity(grad->stops[i].color, opacity); |
| 1328 |
cb = nsvg__applyOpacity(grad->stops[i+1].color, opacity); |
| 1329 |
ua = nsvg__clampf(grad->stops[i].offset, 0, 1); |
| 1330 |
ub = nsvg__clampf(grad->stops[i+1].offset, 0, 1); |
| 1331 |
ia = (int)(ua * 255.0f); |
| 1332 |
ib = (int)(ub * 255.0f); |
| 1333 |
count = ib - ia; |
| 1334 |
if (count <= 0) continue; |
| 1335 |
u = 0; |
| 1336 |
du = 1.0f / (float)count; |
| 1337 |
for (j = 0; j < count; j++) { |
| 1338 |
cache->colors[ia+j] = nsvg__lerpRGBA(ca,cb,u); |
| 1339 |
u += du; |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
for (i = ib; i < 256; i++) |
| 1344 |
cache->colors[i] = cb; |
| 1345 |
} |
| 1346 |
|
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| •1391 |
void nsvgRasterizeXY(NSVGrasterizer* r, |
| 1392 |
NSVGimage* image, float tx, float ty, |
| 1393 |
float sx, float sy, |
| 1394 |
unsigned char* dst, int w, int h, int stride) |
| 1395 |
{ |
| 1396 |
NSVGshape *shape = NULL; |
| 1397 |
NSVGedge *e = NULL; |
| 1398 |
NSVGcachedPaint cache; |
| 1399 |
int i; |
| 1400 |
int j; |
| 1401 |
unsigned char paintOrder; |
| •1402 |
const float sw = (sx + sy) / 2; |
| 1403 |
|
| 1404 |
r->bitmap = dst; |
| 1405 |
r->width = w; |
| 1406 |
r->height = h; |
| 1407 |
r->stride = stride; |
| 1408 |
|
| 1409 |
if (w > r->cscanline) { |
| 1410 |
r->cscanline = w; |
| 1411 |
r->scanline = (unsigned char*)realloc(r->scanline, w); |
| 1412 |
if (r->scanline == NULL) return; |
| 1413 |
} |
| 1414 |
|
| 1415 |
for (i = 0; i < h; i++) |
| 1416 |
memset(&dst[i*stride], 0, w*4); |
| 1417 |
|
| 1418 |
for (shape = image->shapes; shape != NULL; shape = shape->next) { |
| 1419 |
if (!(shape->flags & NSVG_FLAGS_VISIBLE)) |
| 1420 |
continue; |
| 1421 |
|
| 1422 |
for (j = 0; j < 3; j++) { |
| 1423 |
paintOrder = (shape->paintOrder >> (2 * j)) & 0x03; |
| 1424 |
|
| 1425 |
if (paintOrder == NSVG_PAINT_FILL && shape->fill.type != NSVG_PAINT_NONE) { |
| 1426 |
nsvg__resetPool(r); |
| 1427 |
r->freelist = NULL; |
| 1428 |
r->nedges = 0; |
| 1429 |
|
| •1430 |
nsvg__flattenShape(r, shape, sx, sy); |
| 1431 |
|
| 1432 |
|
| 1433 |
for (i = 0; i < r->nedges; i++) { |
| 1434 |
e = &r->edges[i]; |
| 1435 |
e->x0 = tx + e->x0; |
| 1436 |
e->y0 = (ty + e->y0) * NSVG__SUBSAMPLES; |
| 1437 |
e->x1 = tx + e->x1; |
| 1438 |
e->y1 = (ty + e->y1) * NSVG__SUBSAMPLES; |
| 1439 |
} |
| 1440 |
|
| 1441 |
|
| 1442 |
if (r->nedges != 0) |
| 1443 |
qsort(r->edges, r->nedges, sizeof(NSVGedge), nsvg__cmpEdge); |
| 1444 |
|
| 1445 |
|
| 1446 |
nsvg__initPaint(&cache, &shape->fill, shape->opacity); |
| 1447 |
|
| •1448 |
nsvg__rasterizeSortedEdges(r, tx, ty, sx, sy, &cache, shape->fillRule); |
| 1449 |
} |
| •1450 |
if (paintOrder == NSVG_PAINT_STROKE && shape->stroke.type != NSVG_PAINT_NONE && (shape->strokeWidth * sw) > 0.01f) { |
| 1451 |
nsvg__resetPool(r); |
| 1452 |
r->freelist = NULL; |
| 1453 |
r->nedges = 0; |
| 1454 |
|
| •1455 |
nsvg__flattenShapeStroke(r, shape, sx, sy); |
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
for (i = 0; i < r->nedges; i++) { |
| 1461 |
e = &r->edges[i]; |
| 1462 |
e->x0 = tx + e->x0; |
| 1463 |
e->y0 = (ty + e->y0) * NSVG__SUBSAMPLES; |
| 1464 |
e->x1 = tx + e->x1; |
| 1465 |
e->y1 = (ty + e->y1) * NSVG__SUBSAMPLES; |
| 1466 |
} |
| 1467 |
|
| 1468 |
|
| 1469 |
if (r->nedges != 0) |
| 1470 |
qsort(r->edges, r->nedges, sizeof(NSVGedge), nsvg__cmpEdge); |
| 1471 |
|
| 1472 |
|
| 1473 |
nsvg__initPaint(&cache, &shape->stroke, shape->opacity); |
| 1474 |
|
| •1475 |
nsvg__rasterizeSortedEdges(r, tx, ty, sx, sy, &cache, NSVG_FILLRULE_NONZERO); |
| 1476 |
} |
| 1477 |
} |
| 1478 |
} |
| 1479 |
|
| 1480 |
nsvg__unpremultiplyAlpha(dst, w, h, stride); |
| 1481 |
|
| 1482 |
r->bitmap = NULL; |
| 1483 |
r->width = 0; |
| 1484 |
r->height = 0; |
| 1485 |
r->stride = 0; |
| 1486 |
} |
| 1487 |
|
| •1488 |
void nsvgRasterize(NSVGrasterizer* r, |
| 1489 |
NSVGimage* image, float tx, float ty, float scale, |
| 1490 |
unsigned char* dst, int w, int h, int stride) |
| 1491 |
{ |
| 1492 |
nsvgRasterizeXY(r,image, tx, ty, scale, scale, dst, w, h, stride); |
| 1493 |
} |
| 1494 |
|
| 1495 |
#endif |
| 1496 |
|
| 1497 |
#endif |