nanosvgrast.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /*
  2. * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. *
  20. * The polygon rasterization is heavily based on stb_truetype rasterizer
  21. * by Sean Barrett - http://nothings.org/
  22. *
  23. */
  24. #ifndef NANOSVGRAST_H
  25. #define NANOSVGRAST_H
  26. #ifndef NANOSVGRAST_CPLUSPLUS
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #endif
  31. typedef struct NSVGrasterizer NSVGrasterizer;
  32. /* Example Usage:
  33. // Load SVG
  34. NSVGimage* image;
  35. image = nsvgParseFromFile("test.svg", "px", 96);
  36. // Create rasterizer (can be used to render multiple images).
  37. struct NSVGrasterizer* rast = nsvgCreateRasterizer();
  38. // Allocate memory for image
  39. unsigned char* img = malloc(w*h*4);
  40. // Rasterize
  41. nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4);
  42. */
  43. // Allocated rasterizer context.
  44. NSVGrasterizer* nsvgCreateRasterizer();
  45. // Rasterizes SVG image, returns RGBA image (non-premultiplied alpha)
  46. // r - pointer to rasterizer context
  47. // image - pointer to image to rasterize
  48. // tx,ty - image offset (applied after scaling)
  49. // scale - image scale
  50. // dst - pointer to destination image data, 4 bytes per pixel (RGBA)
  51. // w - width of the image to render
  52. // h - height of the image to render
  53. // stride - number of bytes per scaleline in the destination buffer
  54. void nsvgRasterize(NSVGrasterizer* r,
  55. NSVGimage* image, float tx, float ty, float scale,
  56. unsigned char* dst, int w, int h, int stride);
  57. // Deletes rasterizer context.
  58. void nsvgDeleteRasterizer(NSVGrasterizer*);
  59. #ifndef NANOSVGRAST_CPLUSPLUS
  60. #ifdef __cplusplus
  61. }
  62. #endif
  63. #endif
  64. #endif // NANOSVGRAST_H
  65. #ifdef NANOSVGRAST_IMPLEMENTATION
  66. #include <math.h>
  67. #define NSVG__SUBSAMPLES 5
  68. #define NSVG__FIXSHIFT 10
  69. #define NSVG__FIX (1 << NSVG__FIXSHIFT)
  70. #define NSVG__FIXMASK (NSVG__FIX-1)
  71. #define NSVG__MEMPAGE_SIZE 1024
  72. typedef struct NSVGedge {
  73. float x0,y0, x1,y1;
  74. int dir;
  75. struct NSVGedge* next;
  76. } NSVGedge;
  77. typedef struct NSVGpoint {
  78. float x, y;
  79. float dx, dy;
  80. float len;
  81. float dmx, dmy;
  82. unsigned char flags;
  83. } NSVGpoint;
  84. typedef struct NSVGactiveEdge {
  85. int x,dx;
  86. float ey;
  87. int dir;
  88. struct NSVGactiveEdge *next;
  89. } NSVGactiveEdge;
  90. typedef struct NSVGmemPage {
  91. unsigned char mem[NSVG__MEMPAGE_SIZE];
  92. int size;
  93. struct NSVGmemPage* next;
  94. } NSVGmemPage;
  95. typedef struct NSVGcachedPaint {
  96. char type;
  97. char spread;
  98. float xform[6];
  99. unsigned int colors[256];
  100. } NSVGcachedPaint;
  101. struct NSVGrasterizer
  102. {
  103. float px, py;
  104. float tessTol;
  105. float distTol;
  106. NSVGedge* edges;
  107. int nedges;
  108. int cedges;
  109. NSVGpoint* points;
  110. int npoints;
  111. int cpoints;
  112. NSVGpoint* points2;
  113. int npoints2;
  114. int cpoints2;
  115. NSVGactiveEdge* freelist;
  116. NSVGmemPage* pages;
  117. NSVGmemPage* curpage;
  118. unsigned char* scanline;
  119. int cscanline;
  120. unsigned char* bitmap;
  121. int width, height, stride;
  122. };
  123. NSVGrasterizer* nsvgCreateRasterizer()
  124. {
  125. NSVGrasterizer* r = (NSVGrasterizer*)malloc(sizeof(NSVGrasterizer));
  126. if (r == NULL) goto error;
  127. memset(r, 0, sizeof(NSVGrasterizer));
  128. r->tessTol = 0.25f;
  129. r->distTol = 0.01f;
  130. return r;
  131. error:
  132. nsvgDeleteRasterizer(r);
  133. return NULL;
  134. }
  135. void nsvgDeleteRasterizer(NSVGrasterizer* r)
  136. {
  137. NSVGmemPage* p;
  138. if (r == NULL) return;
  139. p = r->pages;
  140. while (p != NULL) {
  141. NSVGmemPage* next = p->next;
  142. free(p);
  143. p = next;
  144. }
  145. if (r->edges) free(r->edges);
  146. if (r->points) free(r->points);
  147. if (r->points2) free(r->points2);
  148. if (r->scanline) free(r->scanline);
  149. free(r);
  150. }
  151. static NSVGmemPage* nsvg__nextPage(NSVGrasterizer* r, NSVGmemPage* cur)
  152. {
  153. NSVGmemPage *newp;
  154. // If using existing chain, return the next page in chain
  155. if (cur != NULL && cur->next != NULL) {
  156. return cur->next;
  157. }
  158. // Alloc new page
  159. newp = (NSVGmemPage*)malloc(sizeof(NSVGmemPage));
  160. if (newp == NULL) return NULL;
  161. memset(newp, 0, sizeof(NSVGmemPage));
  162. // Add to linked list
  163. if (cur != NULL)
  164. cur->next = newp;
  165. else
  166. r->pages = newp;
  167. return newp;
  168. }
  169. static void nsvg__resetPool(NSVGrasterizer* r)
  170. {
  171. NSVGmemPage* p = r->pages;
  172. while (p != NULL) {
  173. p->size = 0;
  174. p = p->next;
  175. }
  176. r->curpage = r->pages;
  177. }
  178. static unsigned char* nsvg__alloc(NSVGrasterizer* r, int size)
  179. {
  180. unsigned char* buf;
  181. if (size > NSVG__MEMPAGE_SIZE) return NULL;
  182. if (r->curpage == NULL || r->curpage->size+size > NSVG__MEMPAGE_SIZE) {
  183. r->curpage = nsvg__nextPage(r, r->curpage);
  184. }
  185. buf = &r->curpage->mem[r->curpage->size];
  186. r->curpage->size += size;
  187. return buf;
  188. }
  189. static int nsvg__ptEquals(float x1, float y1, float x2, float y2, float tol)
  190. {
  191. float dx = x2 - x1;
  192. float dy = y2 - y1;
  193. return dx*dx + dy*dy < tol*tol;
  194. }
  195. static void nsvg__addPathPoint(NSVGrasterizer* r, float x, float y, int flags)
  196. {
  197. NSVGpoint* pt;
  198. if (r->npoints > 0) {
  199. pt = &r->points[r->npoints-1];
  200. if (nsvg__ptEquals(pt->x,pt->y, x,y, r->distTol)) {
  201. pt->flags = (unsigned char)(pt->flags | flags);
  202. return;
  203. }
  204. }
  205. if (r->npoints+1 > r->cpoints) {
  206. r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64;
  207. r->points = (NSVGpoint*)realloc(r->points, sizeof(NSVGpoint) * r->cpoints);
  208. if (r->points == NULL) return;
  209. }
  210. pt = &r->points[r->npoints];
  211. pt->x = x;
  212. pt->y = y;
  213. pt->flags = (unsigned char)flags;
  214. r->npoints++;
  215. }
  216. static void nsvg__appendPathPoint(NSVGrasterizer* r, NSVGpoint pt)
  217. {
  218. if (r->npoints+1 > r->cpoints) {
  219. r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64;
  220. r->points = (NSVGpoint*)realloc(r->points, sizeof(NSVGpoint) * r->cpoints);
  221. if (r->points == NULL) return;
  222. }
  223. r->points[r->npoints] = pt;
  224. r->npoints++;
  225. }
  226. static void nsvg__duplicatePoints(NSVGrasterizer* r)
  227. {
  228. if (r->npoints > r->cpoints2) {
  229. r->cpoints2 = r->npoints;
  230. r->points2 = (NSVGpoint*)realloc(r->points2, sizeof(NSVGpoint) * r->cpoints2);
  231. if (r->points2 == NULL) return;
  232. }
  233. memcpy(r->points2, r->points, sizeof(NSVGpoint) * r->npoints);
  234. r->npoints2 = r->npoints;
  235. }
  236. static void nsvg__addEdge(NSVGrasterizer* r, float x0, float y0, float x1, float y1)
  237. {
  238. NSVGedge* e;
  239. // Skip horizontal edges
  240. if (y0 == y1)
  241. return;
  242. if (r->nedges+1 > r->cedges) {
  243. r->cedges = r->cedges > 0 ? r->cedges * 2 : 64;
  244. r->edges = (NSVGedge*)realloc(r->edges, sizeof(NSVGedge) * r->cedges);
  245. if (r->edges == NULL) return;
  246. }
  247. e = &r->edges[r->nedges];
  248. r->nedges++;
  249. if (y0 < y1) {
  250. e->x0 = x0;
  251. e->y0 = y0;
  252. e->x1 = x1;
  253. e->y1 = y1;
  254. e->dir = 1;
  255. } else {
  256. e->x0 = x1;
  257. e->y0 = y1;
  258. e->x1 = x0;
  259. e->y1 = y0;
  260. e->dir = -1;
  261. }
  262. }
  263. static float nsvg__normalize(float *x, float* y)
  264. {
  265. float d = sqrtf((*x)*(*x) + (*y)*(*y));
  266. if (d > 1e-6f) {
  267. float id = 1.0f / d;
  268. *x *= id;
  269. *y *= id;
  270. }
  271. return d;
  272. }
  273. static float nsvg__absf(float x) { return x < 0 ? -x : x; }
  274. static void nsvg__flattenCubicBez(NSVGrasterizer* r,
  275. float x1, float y1, float x2, float y2,
  276. float x3, float y3, float x4, float y4,
  277. int level, int type)
  278. {
  279. float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234;
  280. float dx,dy,d2,d3;
  281. if (level > 10) return;
  282. x12 = (x1+x2)*0.5f;
  283. y12 = (y1+y2)*0.5f;
  284. x23 = (x2+x3)*0.5f;
  285. y23 = (y2+y3)*0.5f;
  286. x34 = (x3+x4)*0.5f;
  287. y34 = (y3+y4)*0.5f;
  288. x123 = (x12+x23)*0.5f;
  289. y123 = (y12+y23)*0.5f;
  290. dx = x4 - x1;
  291. dy = y4 - y1;
  292. d2 = nsvg__absf(((x2 - x4) * dy - (y2 - y4) * dx));
  293. d3 = nsvg__absf(((x3 - x4) * dy - (y3 - y4) * dx));
  294. if ((d2 + d3)*(d2 + d3) < r->tessTol * (dx*dx + dy*dy)) {
  295. nsvg__addPathPoint(r, x4, y4, type);
  296. return;
  297. }
  298. x234 = (x23+x34)*0.5f;
  299. y234 = (y23+y34)*0.5f;
  300. x1234 = (x123+x234)*0.5f;
  301. y1234 = (y123+y234)*0.5f;
  302. nsvg__flattenCubicBez(r, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1, 0);
  303. nsvg__flattenCubicBez(r, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1, type);
  304. }
  305. static void nsvg__flattenShape(NSVGrasterizer* r, NSVGshape* shape, float scale)
  306. {
  307. int i, j;
  308. NSVGpath* path;
  309. for (path = shape->paths; path != NULL; path = path->next) {
  310. r->npoints = 0;
  311. // Flatten path
  312. nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, 0);
  313. for (i = 0; i < path->npts-1; i += 3) {
  314. float* p = &path->pts[i*2];
  315. nsvg__flattenCubicBez(r, p[0]*scale,p[1]*scale, p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, 0);
  316. }
  317. // Close path
  318. nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, 0);
  319. // Build edges
  320. for (i = 0, j = r->npoints-1; i < r->npoints; j = i++)
  321. nsvg__addEdge(r, r->points[j].x, r->points[j].y, r->points[i].x, r->points[i].y);
  322. }
  323. }
  324. enum NSVGpointFlags
  325. {
  326. NSVG_PT_CORNER = 0x01,
  327. NSVG_PT_BEVEL = 0x02,
  328. NSVG_PT_LEFT = 0x04
  329. };
  330. static void nsvg__initClosed(NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth)
  331. {
  332. float w = lineWidth * 0.5f;
  333. float dx = p1->x - p0->x;
  334. float dy = p1->y - p0->y;
  335. float len = nsvg__normalize(&dx, &dy);
  336. float px = p0->x + dx*len*0.5f, py = p0->y + dy*len*0.5f;
  337. float dlx = dy, dly = -dx;
  338. float lx = px - dlx*w, ly = py - dly*w;
  339. float rx = px + dlx*w, ry = py + dly*w;
  340. left->x = lx; left->y = ly;
  341. right->x = rx; right->y = ry;
  342. }
  343. static void nsvg__buttCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect)
  344. {
  345. float w = lineWidth * 0.5f;
  346. float px = p->x, py = p->y;
  347. float dlx = dy, dly = -dx;
  348. float lx = px - dlx*w, ly = py - dly*w;
  349. float rx = px + dlx*w, ry = py + dly*w;
  350. nsvg__addEdge(r, lx, ly, rx, ry);
  351. if (connect) {
  352. nsvg__addEdge(r, left->x, left->y, lx, ly);
  353. nsvg__addEdge(r, rx, ry, right->x, right->y);
  354. }
  355. left->x = lx; left->y = ly;
  356. right->x = rx; right->y = ry;
  357. }
  358. static void nsvg__squareCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect)
  359. {
  360. float w = lineWidth * 0.5f;
  361. float px = p->x - dx*w, py = p->y - dy*w;
  362. float dlx = dy, dly = -dx;
  363. float lx = px - dlx*w, ly = py - dly*w;
  364. float rx = px + dlx*w, ry = py + dly*w;
  365. nsvg__addEdge(r, lx, ly, rx, ry);
  366. if (connect) {
  367. nsvg__addEdge(r, left->x, left->y, lx, ly);
  368. nsvg__addEdge(r, rx, ry, right->x, right->y);
  369. }
  370. left->x = lx; left->y = ly;
  371. right->x = rx; right->y = ry;
  372. }
  373. #ifndef NSVG_PI
  374. #define NSVG_PI (3.14159265358979323846264338327f)
  375. #endif
  376. static void nsvg__roundCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int ncap, int connect)
  377. {
  378. int i;
  379. float w = lineWidth * 0.5f;
  380. float px = p->x, py = p->y;
  381. float dlx = dy, dly = -dx;
  382. float lx = 0, ly = 0, rx = 0, ry = 0, prevx = 0, prevy = 0;
  383. for (i = 0; i < ncap; i++) {
  384. float a = (float)i/(float)(ncap-1)*NSVG_PI;
  385. float ax = cosf(a) * w, ay = sinf(a) * w;
  386. float x = px - dlx*ax - dx*ay;
  387. float y = py - dly*ax - dy*ay;
  388. if (i > 0)
  389. nsvg__addEdge(r, prevx, prevy, x, y);
  390. prevx = x;
  391. prevy = y;
  392. if (i == 0) {
  393. lx = x; ly = y;
  394. } else if (i == ncap-1) {
  395. rx = x; ry = y;
  396. }
  397. }
  398. if (connect) {
  399. nsvg__addEdge(r, left->x, left->y, lx, ly);
  400. nsvg__addEdge(r, rx, ry, right->x, right->y);
  401. }
  402. left->x = lx; left->y = ly;
  403. right->x = rx; right->y = ry;
  404. }
  405. static void nsvg__bevelJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth)
  406. {
  407. float w = lineWidth * 0.5f;
  408. float dlx0 = p0->dy, dly0 = -p0->dx;
  409. float dlx1 = p1->dy, dly1 = -p1->dx;
  410. float lx0 = p1->x - (dlx0 * w), ly0 = p1->y - (dly0 * w);
  411. float rx0 = p1->x + (dlx0 * w), ry0 = p1->y + (dly0 * w);
  412. float lx1 = p1->x - (dlx1 * w), ly1 = p1->y - (dly1 * w);
  413. float rx1 = p1->x + (dlx1 * w), ry1 = p1->y + (dly1 * w);
  414. nsvg__addEdge(r, lx0, ly0, left->x, left->y);
  415. nsvg__addEdge(r, lx1, ly1, lx0, ly0);
  416. nsvg__addEdge(r, right->x, right->y, rx0, ry0);
  417. nsvg__addEdge(r, rx0, ry0, rx1, ry1);
  418. left->x = lx1; left->y = ly1;
  419. right->x = rx1; right->y = ry1;
  420. }
  421. static void nsvg__miterJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth)
  422. {
  423. float w = lineWidth * 0.5f;
  424. float dlx0 = p0->dy, dly0 = -p0->dx;
  425. float dlx1 = p1->dy, dly1 = -p1->dx;
  426. float lx0, rx0, lx1, rx1;
  427. float ly0, ry0, ly1, ry1;
  428. if (p1->flags & NSVG_PT_LEFT) {
  429. lx0 = lx1 = p1->x - p1->dmx * w;
  430. ly0 = ly1 = p1->y - p1->dmy * w;
  431. nsvg__addEdge(r, lx1, ly1, left->x, left->y);
  432. rx0 = p1->x + (dlx0 * w);
  433. ry0 = p1->y + (dly0 * w);
  434. rx1 = p1->x + (dlx1 * w);
  435. ry1 = p1->y + (dly1 * w);
  436. nsvg__addEdge(r, right->x, right->y, rx0, ry0);
  437. nsvg__addEdge(r, rx0, ry0, rx1, ry1);
  438. } else {
  439. lx0 = p1->x - (dlx0 * w);
  440. ly0 = p1->y - (dly0 * w);
  441. lx1 = p1->x - (dlx1 * w);
  442. ly1 = p1->y - (dly1 * w);
  443. nsvg__addEdge(r, lx0, ly0, left->x, left->y);
  444. nsvg__addEdge(r, lx1, ly1, lx0, ly0);
  445. rx0 = rx1 = p1->x + p1->dmx * w;
  446. ry0 = ry1 = p1->y + p1->dmy * w;
  447. nsvg__addEdge(r, right->x, right->y, rx1, ry1);
  448. }
  449. left->x = lx1; left->y = ly1;
  450. right->x = rx1; right->y = ry1;
  451. }
  452. static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth, int ncap)
  453. {
  454. int i, n;
  455. float w = lineWidth * 0.5f;
  456. float dlx0 = p0->dy, dly0 = -p0->dx;
  457. float dlx1 = p1->dy, dly1 = -p1->dx;
  458. float a0 = atan2f(dly0, dlx0);
  459. float a1 = atan2f(dly1, dlx1);
  460. float da = a1 - a0;
  461. float lx, ly, rx, ry;
  462. if (da < NSVG_PI) da += NSVG_PI*2;
  463. if (da > NSVG_PI) da -= NSVG_PI*2;
  464. n = (int)ceilf((nsvg__absf(da) / NSVG_PI) * (float)ncap);
  465. if (n < 2) n = 2;
  466. if (n > ncap) n = ncap;
  467. lx = left->x;
  468. ly = left->y;
  469. rx = right->x;
  470. ry = right->y;
  471. for (i = 0; i < n; i++) {
  472. float u = (float)i/(float)(n-1);
  473. float a = a0 + u*da;
  474. float ax = cosf(a) * w, ay = sinf(a) * w;
  475. float lx1 = p1->x - ax, ly1 = p1->y - ay;
  476. float rx1 = p1->x + ax, ry1 = p1->y + ay;
  477. nsvg__addEdge(r, lx1, ly1, lx, ly);
  478. nsvg__addEdge(r, rx, ry, rx1, ry1);
  479. lx = lx1; ly = ly1;
  480. rx = rx1; ry = ry1;
  481. }
  482. left->x = lx; left->y = ly;
  483. right->x = rx; right->y = ry;
  484. }
  485. static void nsvg__straightJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p1, float lineWidth)
  486. {
  487. float w = lineWidth * 0.5f;
  488. float lx = p1->x - (p1->dmx * w), ly = p1->y - (p1->dmy * w);
  489. float rx = p1->x + (p1->dmx * w), ry = p1->y + (p1->dmy * w);
  490. nsvg__addEdge(r, lx, ly, left->x, left->y);
  491. nsvg__addEdge(r, right->x, right->y, rx, ry);
  492. left->x = lx; left->y = ly;
  493. right->x = rx; right->y = ry;
  494. }
  495. static int nsvg__curveDivs(float r, float arc, float tol)
  496. {
  497. float da = acosf(r / (r + tol)) * 2.0f;
  498. int divs = (int)ceilf(arc / da);
  499. if (divs < 2) divs = 2;
  500. return divs;
  501. }
  502. static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points, int npoints, int closed, int lineJoin, int lineCap, float lineWidth)
  503. {
  504. int ncap = nsvg__curveDivs(lineWidth*0.5f, NSVG_PI, r->tessTol); // Calculate divisions per half circle.
  505. 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};
  506. NSVGpoint* p0, *p1;
  507. int j, s, e;
  508. // Build stroke edges
  509. if (closed) {
  510. // Looping
  511. p0 = &points[npoints-1];
  512. p1 = &points[0];
  513. s = 0;
  514. e = npoints;
  515. } else {
  516. // Add cap
  517. p0 = &points[0];
  518. p1 = &points[1];
  519. s = 1;
  520. e = npoints-1;
  521. }
  522. if (closed) {
  523. nsvg__initClosed(&left, &right, p0, p1, lineWidth);
  524. firstLeft = left;
  525. firstRight = right;
  526. } else {
  527. // Add cap
  528. float dx = p1->x - p0->x;
  529. float dy = p1->y - p0->y;
  530. nsvg__normalize(&dx, &dy);
  531. if (lineCap == NSVG_CAP_BUTT)
  532. nsvg__buttCap(r, &left, &right, p0, dx, dy, lineWidth, 0);
  533. else if (lineCap == NSVG_CAP_SQUARE)
  534. nsvg__squareCap(r, &left, &right, p0, dx, dy, lineWidth, 0);
  535. else if (lineCap == NSVG_CAP_ROUND)
  536. nsvg__roundCap(r, &left, &right, p0, dx, dy, lineWidth, ncap, 0);
  537. }
  538. for (j = s; j < e; ++j) {
  539. if (p1->flags & NSVG_PT_CORNER) {
  540. if (lineJoin == NSVG_JOIN_ROUND)
  541. nsvg__roundJoin(r, &left, &right, p0, p1, lineWidth, ncap);
  542. else if (lineJoin == NSVG_JOIN_BEVEL || (p1->flags & NSVG_PT_BEVEL))
  543. nsvg__bevelJoin(r, &left, &right, p0, p1, lineWidth);
  544. else
  545. nsvg__miterJoin(r, &left, &right, p0, p1, lineWidth);
  546. } else {
  547. nsvg__straightJoin(r, &left, &right, p1, lineWidth);
  548. }
  549. p0 = p1++;
  550. }
  551. if (closed) {
  552. // Loop it
  553. nsvg__addEdge(r, firstLeft.x, firstLeft.y, left.x, left.y);
  554. nsvg__addEdge(r, right.x, right.y, firstRight.x, firstRight.y);
  555. } else {
  556. // Add cap
  557. float dx = p1->x - p0->x;
  558. float dy = p1->y - p0->y;
  559. nsvg__normalize(&dx, &dy);
  560. if (lineCap == NSVG_CAP_BUTT)
  561. nsvg__buttCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1);
  562. else if (lineCap == NSVG_CAP_SQUARE)
  563. nsvg__squareCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1);
  564. else if (lineCap == NSVG_CAP_ROUND)
  565. nsvg__roundCap(r, &right, &left, p1, -dx, -dy, lineWidth, ncap, 1);
  566. }
  567. }
  568. static void nsvg__prepareStroke(NSVGrasterizer* r, float miterLimit, int lineJoin)
  569. {
  570. int i, j;
  571. NSVGpoint* p0, *p1;
  572. p0 = &r->points[r->npoints-1];
  573. p1 = &r->points[0];
  574. for (i = 0; i < r->npoints; i++) {
  575. // Calculate segment direction and length
  576. p0->dx = p1->x - p0->x;
  577. p0->dy = p1->y - p0->y;
  578. p0->len = nsvg__normalize(&p0->dx, &p0->dy);
  579. // Advance
  580. p0 = p1++;
  581. }
  582. // calculate joins
  583. p0 = &r->points[r->npoints-1];
  584. p1 = &r->points[0];
  585. for (j = 0; j < r->npoints; j++) {
  586. float dlx0, dly0, dlx1, dly1, dmr2, cross;
  587. dlx0 = p0->dy;
  588. dly0 = -p0->dx;
  589. dlx1 = p1->dy;
  590. dly1 = -p1->dx;
  591. // Calculate extrusions
  592. p1->dmx = (dlx0 + dlx1) * 0.5f;
  593. p1->dmy = (dly0 + dly1) * 0.5f;
  594. dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy;
  595. if (dmr2 > 0.000001f) {
  596. float s2 = 1.0f / dmr2;
  597. if (s2 > 600.0f) {
  598. s2 = 600.0f;
  599. }
  600. p1->dmx *= s2;
  601. p1->dmy *= s2;
  602. }
  603. // Clear flags, but keep the corner.
  604. p1->flags = (p1->flags & NSVG_PT_CORNER) ? NSVG_PT_CORNER : 0;
  605. // Keep track of left turns.
  606. cross = p1->dx * p0->dy - p0->dx * p1->dy;
  607. if (cross > 0.0f)
  608. p1->flags |= NSVG_PT_LEFT;
  609. // Check to see if the corner needs to be beveled.
  610. if (p1->flags & NSVG_PT_CORNER) {
  611. if ((dmr2 * miterLimit*miterLimit) < 1.0f || lineJoin == NSVG_JOIN_BEVEL || lineJoin == NSVG_JOIN_ROUND) {
  612. p1->flags |= NSVG_PT_BEVEL;
  613. }
  614. }
  615. p0 = p1++;
  616. }
  617. }
  618. static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float scale)
  619. {
  620. int i, j, closed;
  621. NSVGpath* path;
  622. NSVGpoint* p0, *p1;
  623. float miterLimit = shape->miterLimit;
  624. int lineJoin = shape->strokeLineJoin;
  625. int lineCap = shape->strokeLineCap;
  626. float lineWidth = shape->strokeWidth * scale;
  627. for (path = shape->paths; path != NULL; path = path->next) {
  628. // Flatten path
  629. r->npoints = 0;
  630. nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, NSVG_PT_CORNER);
  631. for (i = 0; i < path->npts-1; i += 3) {
  632. float* p = &path->pts[i*2];
  633. nsvg__flattenCubicBez(r, p[0]*scale,p[1]*scale, p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, NSVG_PT_CORNER);
  634. }
  635. if (r->npoints < 2)
  636. continue;
  637. closed = path->closed;
  638. // If the first and last points are the same, remove the last, mark as closed path.
  639. p0 = &r->points[r->npoints-1];
  640. p1 = &r->points[0];
  641. if (nsvg__ptEquals(p0->x,p0->y, p1->x,p1->y, r->distTol)) {
  642. r->npoints--;
  643. p0 = &r->points[r->npoints-1];
  644. closed = 1;
  645. }
  646. if (shape->strokeDashCount > 0) {
  647. int idash = 0, dashState = 1;
  648. float totalDist = 0, dashLen, allDashLen, dashOffset;
  649. NSVGpoint cur;
  650. if (closed)
  651. nsvg__appendPathPoint(r, r->points[0]);
  652. // Duplicate points -> points2.
  653. nsvg__duplicatePoints(r);
  654. r->npoints = 0;
  655. cur = r->points2[0];
  656. nsvg__appendPathPoint(r, cur);
  657. // Figure out dash offset.
  658. allDashLen = 0;
  659. for (j = 0; j < shape->strokeDashCount; j++)
  660. allDashLen += shape->strokeDashArray[j];
  661. if (shape->strokeDashCount & 1)
  662. allDashLen *= 2.0f;
  663. // Find location inside pattern
  664. dashOffset = fmodf(shape->strokeDashOffset, allDashLen);
  665. if (dashOffset < 0.0f)
  666. dashOffset += allDashLen;
  667. while (dashOffset > shape->strokeDashArray[idash]) {
  668. dashOffset -= shape->strokeDashArray[idash];
  669. idash = (idash + 1) % shape->strokeDashCount;
  670. }
  671. dashLen = (shape->strokeDashArray[idash] - dashOffset) * scale;
  672. for (j = 1; j < r->npoints2; ) {
  673. float dx = r->points2[j].x - cur.x;
  674. float dy = r->points2[j].y - cur.y;
  675. float dist = sqrtf(dx*dx + dy*dy);
  676. if ((totalDist + dist) > dashLen) {
  677. // Calculate intermediate point
  678. float d = (dashLen - totalDist) / dist;
  679. float x = cur.x + dx * d;
  680. float y = cur.y + dy * d;
  681. nsvg__addPathPoint(r, x, y, NSVG_PT_CORNER);
  682. // Stroke
  683. if (r->npoints > 1 && dashState) {
  684. nsvg__prepareStroke(r, miterLimit, lineJoin);
  685. nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth);
  686. }
  687. // Advance dash pattern
  688. dashState = !dashState;
  689. idash = (idash+1) % shape->strokeDashCount;
  690. dashLen = shape->strokeDashArray[idash] * scale;
  691. // Restart
  692. cur.x = x;
  693. cur.y = y;
  694. cur.flags = NSVG_PT_CORNER;
  695. totalDist = 0.0f;
  696. r->npoints = 0;
  697. nsvg__appendPathPoint(r, cur);
  698. } else {
  699. totalDist += dist;
  700. cur = r->points2[j];
  701. nsvg__appendPathPoint(r, cur);
  702. j++;
  703. }
  704. }
  705. // Stroke any leftover path
  706. if (r->npoints > 1 && dashState)
  707. nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth);
  708. } else {
  709. nsvg__prepareStroke(r, miterLimit, lineJoin);
  710. nsvg__expandStroke(r, r->points, r->npoints, closed, lineJoin, lineCap, lineWidth);
  711. }
  712. }
  713. }
  714. static int nsvg__cmpEdge(const void *p, const void *q)
  715. {
  716. const NSVGedge* a = (const NSVGedge*)p;
  717. const NSVGedge* b = (const NSVGedge*)q;
  718. if (a->y0 < b->y0) return -1;
  719. if (a->y0 > b->y0) return 1;
  720. return 0;
  721. }
  722. static NSVGactiveEdge* nsvg__addActive(NSVGrasterizer* r, NSVGedge* e, float startPoint)
  723. {
  724. NSVGactiveEdge* z;
  725. if (r->freelist != NULL) {
  726. // Restore from freelist.
  727. z = r->freelist;
  728. r->freelist = z->next;
  729. } else {
  730. // Alloc new edge.
  731. z = (NSVGactiveEdge*)nsvg__alloc(r, sizeof(NSVGactiveEdge));
  732. if (z == NULL) return NULL;
  733. }
  734. float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
  735. // STBTT_assert(e->y0 <= start_point);
  736. // round dx down to avoid going too far
  737. if (dxdy < 0)
  738. z->dx = (int)(-floorf(NSVG__FIX * -dxdy));
  739. else
  740. z->dx = (int)floorf(NSVG__FIX * dxdy);
  741. z->x = (int)floorf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)));
  742. // z->x -= off_x * FIX;
  743. z->ey = e->y1;
  744. z->next = 0;
  745. z->dir = e->dir;
  746. return z;
  747. }
  748. static void nsvg__freeActive(NSVGrasterizer* r, NSVGactiveEdge* z)
  749. {
  750. z->next = r->freelist;
  751. r->freelist = z;
  752. }
  753. static void nsvg__fillScanline(unsigned char* scanline, int len, int x0, int x1, int maxWeight, int* xmin, int* xmax)
  754. {
  755. int i = x0 >> NSVG__FIXSHIFT;
  756. int j = x1 >> NSVG__FIXSHIFT;
  757. if (i < *xmin) *xmin = i;
  758. if (j > *xmax) *xmax = j;
  759. if (i < len && j >= 0) {
  760. if (i == j) {
  761. // x0,x1 are the same pixel, so compute combined coverage
  762. scanline[i] = (unsigned char)(scanline[i] + ((x1 - x0) * maxWeight >> NSVG__FIXSHIFT));
  763. } else {
  764. if (i >= 0) // add antialiasing for x0
  765. scanline[i] = (unsigned char)(scanline[i] + (((NSVG__FIX - (x0 & NSVG__FIXMASK)) * maxWeight) >> NSVG__FIXSHIFT));
  766. else
  767. i = -1; // clip
  768. if (j < len) // add antialiasing for x1
  769. scanline[j] = (unsigned char)(scanline[j] + (((x1 & NSVG__FIXMASK) * maxWeight) >> NSVG__FIXSHIFT));
  770. else
  771. j = len; // clip
  772. for (++i; i < j; ++i) // fill pixels between x0 and x1
  773. scanline[i] = (unsigned char)(scanline[i] + maxWeight);
  774. }
  775. }
  776. }
  777. // note: this routine clips fills that extend off the edges... ideally this
  778. // wouldn't happen, but it could happen if the truetype glyph bounding boxes
  779. // are wrong, or if the user supplies a too-small bitmap
  780. static void nsvg__fillActiveEdges(unsigned char* scanline, int len, NSVGactiveEdge* e, int maxWeight, int* xmin, int* xmax, char fillRule)
  781. {
  782. // non-zero winding fill
  783. int x0 = 0, w = 0;
  784. if (fillRule == NSVG_FILLRULE_NONZERO) {
  785. // Non-zero
  786. while (e != NULL) {
  787. if (w == 0) {
  788. // if we're currently at zero, we need to record the edge start point
  789. x0 = e->x; w += e->dir;
  790. } else {
  791. int x1 = e->x; w += e->dir;
  792. // if we went to zero, we need to draw
  793. if (w == 0)
  794. nsvg__fillScanline(scanline, len, x0, x1, maxWeight, xmin, xmax);
  795. }
  796. e = e->next;
  797. }
  798. } else if (fillRule == NSVG_FILLRULE_EVENODD) {
  799. // Even-odd
  800. while (e != NULL) {
  801. if (w == 0) {
  802. // if we're currently at zero, we need to record the edge start point
  803. x0 = e->x; w = 1;
  804. } else {
  805. int x1 = e->x; w = 0;
  806. nsvg__fillScanline(scanline, len, x0, x1, maxWeight, xmin, xmax);
  807. }
  808. e = e->next;
  809. }
  810. }
  811. }
  812. static float nsvg__clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
  813. static unsigned int nsvg__RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
  814. {
  815. return (r) | (g << 8) | (b << 16) | (a << 24);
  816. }
  817. static unsigned int nsvg__lerpRGBA(unsigned int c0, unsigned int c1, float u)
  818. {
  819. int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f);
  820. int r = (((c0) & 0xff)*(256-iu) + (((c1) & 0xff)*iu)) >> 8;
  821. int g = (((c0>>8) & 0xff)*(256-iu) + (((c1>>8) & 0xff)*iu)) >> 8;
  822. int b = (((c0>>16) & 0xff)*(256-iu) + (((c1>>16) & 0xff)*iu)) >> 8;
  823. int a = (((c0>>24) & 0xff)*(256-iu) + (((c1>>24) & 0xff)*iu)) >> 8;
  824. return nsvg__RGBA((unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a);
  825. }
  826. static unsigned int nsvg__applyOpacity(unsigned int c, float u)
  827. {
  828. int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f);
  829. int r = (c) & 0xff;
  830. int g = (c>>8) & 0xff;
  831. int b = (c>>16) & 0xff;
  832. int a = (((c>>24) & 0xff)*iu) >> 8;
  833. return nsvg__RGBA((unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a);
  834. }
  835. static inline int nsvg__div255(int x)
  836. {
  837. return ((x+1) * 257) >> 16;
  838. }
  839. static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* cover, int x, int y,
  840. float tx, float ty, float scale, NSVGcachedPaint* cache)
  841. {
  842. if (cache->type == NSVG_PAINT_COLOR) {
  843. int i, cr, cg, cb, ca;
  844. cr = cache->colors[0] & 0xff;
  845. cg = (cache->colors[0] >> 8) & 0xff;
  846. cb = (cache->colors[0] >> 16) & 0xff;
  847. ca = (cache->colors[0] >> 24) & 0xff;
  848. for (i = 0; i < count; i++) {
  849. int r,g,b;
  850. int a = nsvg__div255((int)cover[0] * ca);
  851. int ia = 255 - a;
  852. // Premultiply
  853. r = nsvg__div255(cr * a);
  854. g = nsvg__div255(cg * a);
  855. b = nsvg__div255(cb * a);
  856. // Blend over
  857. r += nsvg__div255(ia * (int)dst[0]);
  858. g += nsvg__div255(ia * (int)dst[1]);
  859. b += nsvg__div255(ia * (int)dst[2]);
  860. a += nsvg__div255(ia * (int)dst[3]);
  861. dst[0] = (unsigned char)r;
  862. dst[1] = (unsigned char)g;
  863. dst[2] = (unsigned char)b;
  864. dst[3] = (unsigned char)a;
  865. cover++;
  866. dst += 4;
  867. }
  868. } else if (cache->type == NSVG_PAINT_LINEAR_GRADIENT) {
  869. // TODO: spread modes.
  870. // TODO: plenty of opportunities to optimize.
  871. float fx, fy, dx, gy;
  872. float* t = cache->xform;
  873. int i, cr, cg, cb, ca;
  874. unsigned int c;
  875. fx = ((float)x - tx) / scale;
  876. fy = ((float)y - ty) / scale;
  877. dx = 1.0f / scale;
  878. for (i = 0; i < count; i++) {
  879. int r,g,b,a,ia;
  880. gy = fx*t[1] + fy*t[3] + t[5];
  881. c = cache->colors[(int)nsvg__clampf(gy*255.0f, 0, 255.0f)];
  882. cr = (c) & 0xff;
  883. cg = (c >> 8) & 0xff;
  884. cb = (c >> 16) & 0xff;
  885. ca = (c >> 24) & 0xff;
  886. a = nsvg__div255((int)cover[0] * ca);
  887. ia = 255 - a;
  888. // Premultiply
  889. r = nsvg__div255(cr * a);
  890. g = nsvg__div255(cg * a);
  891. b = nsvg__div255(cb * a);
  892. // Blend over
  893. r += nsvg__div255(ia * (int)dst[0]);
  894. g += nsvg__div255(ia * (int)dst[1]);
  895. b += nsvg__div255(ia * (int)dst[2]);
  896. a += nsvg__div255(ia * (int)dst[3]);
  897. dst[0] = (unsigned char)r;
  898. dst[1] = (unsigned char)g;
  899. dst[2] = (unsigned char)b;
  900. dst[3] = (unsigned char)a;
  901. cover++;
  902. dst += 4;
  903. fx += dx;
  904. }
  905. } else if (cache->type == NSVG_PAINT_RADIAL_GRADIENT) {
  906. // TODO: spread modes.
  907. // TODO: plenty of opportunities to optimize.
  908. // TODO: focus (fx,fy)
  909. float fx, fy, dx, gx, gy, gd;
  910. float* t = cache->xform;
  911. int i, cr, cg, cb, ca;
  912. unsigned int c;
  913. fx = ((float)x - tx) / scale;
  914. fy = ((float)y - ty) / scale;
  915. dx = 1.0f / scale;
  916. for (i = 0; i < count; i++) {
  917. int r,g,b,a,ia;
  918. gx = fx*t[0] + fy*t[2] + t[4];
  919. gy = fx*t[1] + fy*t[3] + t[5];
  920. gd = sqrtf(gx*gx + gy*gy);
  921. c = cache->colors[(int)nsvg__clampf(gd*255.0f, 0, 255.0f)];
  922. cr = (c) & 0xff;
  923. cg = (c >> 8) & 0xff;
  924. cb = (c >> 16) & 0xff;
  925. ca = (c >> 24) & 0xff;
  926. a = nsvg__div255((int)cover[0] * ca);
  927. ia = 255 - a;
  928. // Premultiply
  929. r = nsvg__div255(cr * a);
  930. g = nsvg__div255(cg * a);
  931. b = nsvg__div255(cb * a);
  932. // Blend over
  933. r += nsvg__div255(ia * (int)dst[0]);
  934. g += nsvg__div255(ia * (int)dst[1]);
  935. b += nsvg__div255(ia * (int)dst[2]);
  936. a += nsvg__div255(ia * (int)dst[3]);
  937. dst[0] = (unsigned char)r;
  938. dst[1] = (unsigned char)g;
  939. dst[2] = (unsigned char)b;
  940. dst[3] = (unsigned char)a;
  941. cover++;
  942. dst += 4;
  943. fx += dx;
  944. }
  945. }
  946. }
  947. static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, float scale, NSVGcachedPaint* cache, char fillRule)
  948. {
  949. NSVGactiveEdge *active = NULL;
  950. int y, s;
  951. int e = 0;
  952. int maxWeight = (255 / NSVG__SUBSAMPLES); // weight per vertical scanline
  953. int xmin, xmax;
  954. for (y = 0; y < r->height; y++) {
  955. memset(r->scanline, 0, r->width);
  956. xmin = r->width;
  957. xmax = 0;
  958. for (s = 0; s < NSVG__SUBSAMPLES; ++s) {
  959. // find center of pixel for this scanline
  960. float scany = (float)(y*NSVG__SUBSAMPLES + s) + 0.5f;
  961. NSVGactiveEdge **step = &active;
  962. // update all active edges;
  963. // remove all active edges that terminate before the center of this scanline
  964. while (*step) {
  965. NSVGactiveEdge *z = *step;
  966. if (z->ey <= scany) {
  967. *step = z->next; // delete from list
  968. // NSVG__assert(z->valid);
  969. nsvg__freeActive(r, z);
  970. } else {
  971. z->x += z->dx; // advance to position for current scanline
  972. step = &((*step)->next); // advance through list
  973. }
  974. }
  975. // resort the list if needed
  976. for (;;) {
  977. int changed = 0;
  978. step = &active;
  979. while (*step && (*step)->next) {
  980. if ((*step)->x > (*step)->next->x) {
  981. NSVGactiveEdge* t = *step;
  982. NSVGactiveEdge* q = t->next;
  983. t->next = q->next;
  984. q->next = t;
  985. *step = q;
  986. changed = 1;
  987. }
  988. step = &(*step)->next;
  989. }
  990. if (!changed) break;
  991. }
  992. // insert all edges that start before the center of this scanline -- omit ones that also end on this scanline
  993. while (e < r->nedges && r->edges[e].y0 <= scany) {
  994. if (r->edges[e].y1 > scany) {
  995. NSVGactiveEdge* z = nsvg__addActive(r, &r->edges[e], scany);
  996. if (z == NULL) break;
  997. // find insertion point
  998. if (active == NULL) {
  999. active = z;
  1000. } else if (z->x < active->x) {
  1001. // insert at front
  1002. z->next = active;
  1003. active = z;
  1004. } else {
  1005. // find thing to insert AFTER
  1006. NSVGactiveEdge* p = active;
  1007. while (p->next && p->next->x < z->x)
  1008. p = p->next;
  1009. // at this point, p->next->x is NOT < z->x
  1010. z->next = p->next;
  1011. p->next = z;
  1012. }
  1013. }
  1014. e++;
  1015. }
  1016. // now process all active edges in non-zero fashion
  1017. if (active != NULL)
  1018. nsvg__fillActiveEdges(r->scanline, r->width, active, maxWeight, &xmin, &xmax, fillRule);
  1019. }
  1020. // Blit
  1021. if (xmin < 0) xmin = 0;
  1022. if (xmax > r->width-1) xmax = r->width-1;
  1023. if (xmin <= xmax) {
  1024. nsvg__scanlineSolid(&r->bitmap[y * r->stride] + xmin*4, xmax-xmin+1, &r->scanline[xmin], xmin, y, tx,ty, scale, cache);
  1025. }
  1026. }
  1027. }
  1028. static void nsvg__unpremultiplyAlpha(unsigned char* image, int w, int h, int stride)
  1029. {
  1030. int x,y;
  1031. // Unpremultiply
  1032. for (y = 0; y < h; y++) {
  1033. unsigned char *row = &image[y*stride];
  1034. for (x = 0; x < w; x++) {
  1035. int r = row[0], g = row[1], b = row[2], a = row[3];
  1036. if (a != 0) {
  1037. row[0] = (unsigned char)(r*255/a);
  1038. row[1] = (unsigned char)(g*255/a);
  1039. row[2] = (unsigned char)(b*255/a);
  1040. }
  1041. row += 4;
  1042. }
  1043. }
  1044. // Defringe
  1045. for (y = 0; y < h; y++) {
  1046. unsigned char *row = &image[y*stride];
  1047. for (x = 0; x < w; x++) {
  1048. int r = 0, g = 0, b = 0, a = row[3], n = 0;
  1049. if (a == 0) {
  1050. if (x-1 > 0 && row[-1] != 0) {
  1051. r += row[-4];
  1052. g += row[-3];
  1053. b += row[-2];
  1054. n++;
  1055. }
  1056. if (x+1 < w && row[7] != 0) {
  1057. r += row[4];
  1058. g += row[5];
  1059. b += row[6];
  1060. n++;
  1061. }
  1062. if (y-1 > 0 && row[-stride+3] != 0) {
  1063. r += row[-stride];
  1064. g += row[-stride+1];
  1065. b += row[-stride+2];
  1066. n++;
  1067. }
  1068. if (y+1 < h && row[stride+3] != 0) {
  1069. r += row[stride];
  1070. g += row[stride+1];
  1071. b += row[stride+2];
  1072. n++;
  1073. }
  1074. if (n > 0) {
  1075. row[0] = (unsigned char)(r/n);
  1076. row[1] = (unsigned char)(g/n);
  1077. row[2] = (unsigned char)(b/n);
  1078. }
  1079. }
  1080. row += 4;
  1081. }
  1082. }
  1083. }
  1084. static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opacity)
  1085. {
  1086. int i, j;
  1087. NSVGgradient* grad;
  1088. cache->type = paint->type;
  1089. if (paint->type == NSVG_PAINT_COLOR) {
  1090. cache->colors[0] = nsvg__applyOpacity(paint->color, opacity);
  1091. return;
  1092. }
  1093. grad = paint->gradient;
  1094. cache->spread = grad->spread;
  1095. memcpy(cache->xform, grad->xform, sizeof(float)*6);
  1096. if (grad->nstops == 0) {
  1097. for (i = 0; i < 256; i++)
  1098. cache->colors[i] = 0;
  1099. } if (grad->nstops == 1) {
  1100. for (i = 0; i < 256; i++)
  1101. cache->colors[i] = nsvg__applyOpacity(grad->stops[i].color, opacity);
  1102. } else {
  1103. unsigned int ca, cb = 0;
  1104. float ua, ub, du, u;
  1105. int ia, ib, count;
  1106. ca = nsvg__applyOpacity(grad->stops[0].color, opacity);
  1107. ua = nsvg__clampf(grad->stops[0].offset, 0, 1);
  1108. ub = nsvg__clampf(grad->stops[grad->nstops-1].offset, ua, 1);
  1109. ia = (int)(ua * 255.0f);
  1110. ib = (int)(ub * 255.0f);
  1111. for (i = 0; i < ia; i++) {
  1112. cache->colors[i] = ca;
  1113. }
  1114. for (i = 0; i < grad->nstops-1; i++) {
  1115. ca = nsvg__applyOpacity(grad->stops[i].color, opacity);
  1116. cb = nsvg__applyOpacity(grad->stops[i+1].color, opacity);
  1117. ua = nsvg__clampf(grad->stops[i].offset, 0, 1);
  1118. ub = nsvg__clampf(grad->stops[i+1].offset, 0, 1);
  1119. ia = (int)(ua * 255.0f);
  1120. ib = (int)(ub * 255.0f);
  1121. count = ib - ia;
  1122. if (count <= 0) continue;
  1123. u = 0;
  1124. du = 1.0f / (float)count;
  1125. for (j = 0; j < count; j++) {
  1126. cache->colors[ia+j] = nsvg__lerpRGBA(ca,cb,u);
  1127. u += du;
  1128. }
  1129. }
  1130. for (i = ib; i < 256; i++)
  1131. cache->colors[i] = cb;
  1132. }
  1133. }
  1134. /*
  1135. static void dumpEdges(NSVGrasterizer* r, const char* name)
  1136. {
  1137. float xmin = 0, xmax = 0, ymin = 0, ymax = 0;
  1138. NSVGedge *e = NULL;
  1139. int i;
  1140. if (r->nedges == 0) return;
  1141. FILE* fp = fopen(name, "w");
  1142. if (fp == NULL) return;
  1143. xmin = xmax = r->edges[0].x0;
  1144. ymin = ymax = r->edges[0].y0;
  1145. for (i = 0; i < r->nedges; i++) {
  1146. e = &r->edges[i];
  1147. xmin = nsvg__minf(xmin, e->x0);
  1148. xmin = nsvg__minf(xmin, e->x1);
  1149. xmax = nsvg__maxf(xmax, e->x0);
  1150. xmax = nsvg__maxf(xmax, e->x1);
  1151. ymin = nsvg__minf(ymin, e->y0);
  1152. ymin = nsvg__minf(ymin, e->y1);
  1153. ymax = nsvg__maxf(ymax, e->y0);
  1154. ymax = nsvg__maxf(ymax, e->y1);
  1155. }
  1156. fprintf(fp, "<svg viewBox=\"%f %f %f %f\" xmlns=\"http://www.w3.org/2000/svg\">", xmin, ymin, (xmax - xmin), (ymax - ymin));
  1157. for (i = 0; i < r->nedges; i++) {
  1158. e = &r->edges[i];
  1159. fprintf(fp ,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke:#000;\" />", e->x0,e->y0, e->x1,e->y1);
  1160. }
  1161. for (i = 0; i < r->npoints; i++) {
  1162. if (i+1 < r->npoints)
  1163. fprintf(fp ,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke:#f00;\" />", r->points[i].x, r->points[i].y, r->points[i+1].x, r->points[i+1].y);
  1164. fprintf(fp ,"<circle cx=\"%f\" cy=\"%f\" r=\"1\" style=\"fill:%s;\" />", r->points[i].x, r->points[i].y, r->points[i].flags == 0 ? "#f00" : "#0f0");
  1165. }
  1166. fprintf(fp, "</svg>");
  1167. fclose(fp);
  1168. }
  1169. */
  1170. void nsvgRasterize(NSVGrasterizer* r,
  1171. NSVGimage* image, float tx, float ty, float scale,
  1172. unsigned char* dst, int w, int h, int stride)
  1173. {
  1174. NSVGshape *shape = NULL;
  1175. NSVGedge *e = NULL;
  1176. NSVGcachedPaint cache;
  1177. int i;
  1178. r->bitmap = dst;
  1179. r->width = w;
  1180. r->height = h;
  1181. r->stride = stride;
  1182. if (w > r->cscanline) {
  1183. r->cscanline = w;
  1184. r->scanline = (unsigned char*)realloc(r->scanline, w);
  1185. if (r->scanline == NULL) return;
  1186. }
  1187. for (i = 0; i < h; i++)
  1188. memset(&dst[i*stride], 0, w*4);
  1189. for (shape = image->shapes; shape != NULL; shape = shape->next) {
  1190. if (!(shape->flags & NSVG_FLAGS_VISIBLE))
  1191. continue;
  1192. if (shape->fill.type != NSVG_PAINT_NONE) {
  1193. nsvg__resetPool(r);
  1194. r->freelist = NULL;
  1195. r->nedges = 0;
  1196. nsvg__flattenShape(r, shape, scale);
  1197. // Scale and translate edges
  1198. for (i = 0; i < r->nedges; i++) {
  1199. e = &r->edges[i];
  1200. e->x0 = tx + e->x0;
  1201. e->y0 = (ty + e->y0) * NSVG__SUBSAMPLES;
  1202. e->x1 = tx + e->x1;
  1203. e->y1 = (ty + e->y1) * NSVG__SUBSAMPLES;
  1204. }
  1205. // Rasterize edges
  1206. qsort(r->edges, r->nedges, sizeof(NSVGedge), nsvg__cmpEdge);
  1207. // now, traverse the scanlines and find the intersections on each scanline, use non-zero rule
  1208. nsvg__initPaint(&cache, &shape->fill, shape->opacity);
  1209. nsvg__rasterizeSortedEdges(r, tx,ty,scale, &cache, shape->fillRule);
  1210. }
  1211. if (shape->stroke.type != NSVG_PAINT_NONE && (shape->strokeWidth * scale) > 0.01f) {
  1212. nsvg__resetPool(r);
  1213. r->freelist = NULL;
  1214. r->nedges = 0;
  1215. nsvg__flattenShapeStroke(r, shape, scale);
  1216. // dumpEdges(r, "edge.svg");
  1217. // Scale and translate edges
  1218. for (i = 0; i < r->nedges; i++) {
  1219. e = &r->edges[i];
  1220. e->x0 = tx + e->x0;
  1221. e->y0 = (ty + e->y0) * NSVG__SUBSAMPLES;
  1222. e->x1 = tx + e->x1;
  1223. e->y1 = (ty + e->y1) * NSVG__SUBSAMPLES;
  1224. }
  1225. // Rasterize edges
  1226. qsort(r->edges, r->nedges, sizeof(NSVGedge), nsvg__cmpEdge);
  1227. // now, traverse the scanlines and find the intersections on each scanline, use non-zero rule
  1228. nsvg__initPaint(&cache, &shape->stroke, shape->opacity);
  1229. nsvg__rasterizeSortedEdges(r, tx,ty,scale, &cache, NSVG_FILLRULE_NONZERO);
  1230. }
  1231. }
  1232. nsvg__unpremultiplyAlpha(dst, w, h, stride);
  1233. r->bitmap = NULL;
  1234. r->width = 0;
  1235. r->height = 0;
  1236. r->stride = 0;
  1237. }
  1238. #endif