PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/SDK/gfx/nanovg.c

https://gitlab.com/_Infinity_/infinity
C | 2746 lines | 2249 code | 383 blank | 114 comment | 373 complexity | 399bc91b498f18c51a049fa5d961f2df MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. //
  2. // Copyright (c) 2013 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. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <gfx/nanovg.h>
  21. #define FONTSTASH_IMPLEMENTATION
  22. #include "fontstash.h"
  23. #include "stb_image.h"
  24. #ifdef _MSC_VER
  25. #pragma warning(disable: 4100) // unreferenced formal parameter
  26. #pragma warning(disable: 4127) // conditional expression is constant
  27. #pragma warning(disable: 4204) // nonstandard extension used : non-constant aggregate initializer
  28. #pragma warning(disable: 4706) // assignment within conditional expression
  29. #endif
  30. #define NVG_INIT_FONTIMAGE_SIZE 512
  31. #define NVG_MAX_FONTIMAGE_SIZE 2048
  32. #define NVG_MAX_FONTIMAGES 4
  33. #define NVG_INIT_COMMANDS_SIZE 256
  34. #define NVG_INIT_POINTS_SIZE 128
  35. #define NVG_INIT_PATHS_SIZE 16
  36. #define NVG_INIT_VERTS_SIZE 256
  37. #define NVG_MAX_STATES 32
  38. #define NVG_KAPPA90 0.5522847493f // Lenght proportional to radius of a cubic bezier handle for 90deg arcs.
  39. #define NVG_COUNTOF(arr) (sizeof(arr) / sizeof(0[arr]))
  40. enum NVGcommands {
  41. NVG_MOVETO = 0,
  42. NVG_LINETO = 1,
  43. NVG_BEZIERTO = 2,
  44. NVG_CLOSE = 3,
  45. NVG_WINDING = 4,
  46. };
  47. enum NVGpointFlags
  48. {
  49. NVG_PT_CORNER = 0x01,
  50. NVG_PT_LEFT = 0x02,
  51. NVG_PT_BEVEL = 0x04,
  52. NVG_PR_INNERBEVEL = 0x08,
  53. };
  54. struct NVGstate {
  55. NVGpaint fill;
  56. NVGpaint stroke;
  57. float strokeWidth;
  58. float miterLimit;
  59. int lineJoin;
  60. int lineCap;
  61. float alpha;
  62. float xform[6];
  63. NVGscissor scissor;
  64. float fontSize;
  65. float letterSpacing;
  66. float lineHeight;
  67. float fontBlur;
  68. int textAlign;
  69. int fontId;
  70. };
  71. typedef struct NVGstate NVGstate;
  72. struct NVGpoint {
  73. float x,y;
  74. float dx, dy;
  75. float len;
  76. float dmx, dmy;
  77. unsigned char flags;
  78. };
  79. typedef struct NVGpoint NVGpoint;
  80. struct NVGpathCache {
  81. NVGpoint* points;
  82. int npoints;
  83. int cpoints;
  84. NVGpath* paths;
  85. int npaths;
  86. int cpaths;
  87. NVGvertex* verts;
  88. int nverts;
  89. int cverts;
  90. float bounds[4];
  91. };
  92. typedef struct NVGpathCache NVGpathCache;
  93. struct NVGcontext {
  94. NVGparams params;
  95. float* commands;
  96. int ccommands;
  97. int ncommands;
  98. float commandx, commandy;
  99. NVGstate states[NVG_MAX_STATES];
  100. int nstates;
  101. NVGpathCache* cache;
  102. float tessTol;
  103. float distTol;
  104. float fringeWidth;
  105. float devicePxRatio;
  106. struct FONScontext* fs;
  107. int fontImages[NVG_MAX_FONTIMAGES];
  108. int fontImageIdx;
  109. int drawCallCount;
  110. int fillTriCount;
  111. int strokeTriCount;
  112. int textTriCount;
  113. };
  114. static float nvg__sqrtf(float a) { return sqrtf(a); }
  115. static float nvg__modf(float a, float b) { return fmodf(a, b); }
  116. static float nvg__sinf(float a) { return sinf(a); }
  117. static float nvg__cosf(float a) { return cosf(a); }
  118. static float nvg__tanf(float a) { return tanf(a); }
  119. static float nvg__atan2f(float a,float b) { return atan2f(a, b); }
  120. static float nvg__acosf(float a) { return acosf(a); }
  121. static int nvg__mini(int a, int b) { return a < b ? a : b; }
  122. static int nvg__maxi(int a, int b) { return a > b ? a : b; }
  123. static int nvg__clampi(int a, int mn, int mx) { return a < mn ? mn : (a > mx ? mx : a); }
  124. static float nvg__minf(float a, float b) { return a < b ? a : b; }
  125. static float nvg__maxf(float a, float b) { return a > b ? a : b; }
  126. static float nvg__absf(float a) { return a >= 0.0f ? a : -a; }
  127. static float nvg__signf(float a) { return a >= 0.0f ? 1.0f : -1.0f; }
  128. static float nvg__clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
  129. static float nvg__cross(float dx0, float dy0, float dx1, float dy1) { return dx1*dy0 - dx0*dy1; }
  130. static float nvg__normalize(float *x, float* y)
  131. {
  132. float d = nvg__sqrtf((*x)*(*x) + (*y)*(*y));
  133. if (d > 1e-6f) {
  134. float id = 1.0f / d;
  135. *x *= id;
  136. *y *= id;
  137. }
  138. return d;
  139. }
  140. static void nvg__deletePathCache(NVGpathCache* c)
  141. {
  142. if (c == NULL) return;
  143. if (c->points != NULL) free(c->points);
  144. if (c->paths != NULL) free(c->paths);
  145. if (c->verts != NULL) free(c->verts);
  146. free(c);
  147. }
  148. static NVGpathCache* nvg__allocPathCache(void)
  149. {
  150. NVGpathCache* c = (NVGpathCache*)malloc(sizeof(NVGpathCache));
  151. if (c == NULL) goto error;
  152. memset(c, 0, sizeof(NVGpathCache));
  153. c->points = (NVGpoint*)malloc(sizeof(NVGpoint)*NVG_INIT_POINTS_SIZE);
  154. if (!c->points) goto error;
  155. c->npoints = 0;
  156. c->cpoints = NVG_INIT_POINTS_SIZE;
  157. c->paths = (NVGpath*)malloc(sizeof(NVGpath)*NVG_INIT_PATHS_SIZE);
  158. if (!c->paths) goto error;
  159. c->npaths = 0;
  160. c->cpaths = NVG_INIT_PATHS_SIZE;
  161. c->verts = (NVGvertex*)malloc(sizeof(NVGvertex)*NVG_INIT_VERTS_SIZE);
  162. if (!c->verts) goto error;
  163. c->nverts = 0;
  164. c->cverts = NVG_INIT_VERTS_SIZE;
  165. return c;
  166. error:
  167. nvg__deletePathCache(c);
  168. return NULL;
  169. }
  170. static void nvg__setDevicePixelRatio(NVGcontext* ctx, float ratio)
  171. {
  172. ctx->tessTol = 0.25f / ratio;
  173. ctx->distTol = 0.01f / ratio;
  174. ctx->fringeWidth = 1.0f / ratio;
  175. ctx->devicePxRatio = ratio;
  176. }
  177. NVGcontext* nvgCreateInternal(NVGparams* params)
  178. {
  179. FONSparams fontParams;
  180. NVGcontext* ctx = (NVGcontext*)malloc(sizeof(NVGcontext));
  181. int i;
  182. if (ctx == NULL) goto error;
  183. memset(ctx, 0, sizeof(NVGcontext));
  184. ctx->params = *params;
  185. for (i = 0; i < NVG_MAX_FONTIMAGES; i++)
  186. ctx->fontImages[i] = 0;
  187. ctx->commands = (float*)malloc(sizeof(float)*NVG_INIT_COMMANDS_SIZE);
  188. if (!ctx->commands) goto error;
  189. ctx->ncommands = 0;
  190. ctx->ccommands = NVG_INIT_COMMANDS_SIZE;
  191. ctx->cache = nvg__allocPathCache();
  192. if (ctx->cache == NULL) goto error;
  193. nvgSave(ctx);
  194. nvgReset(ctx);
  195. nvg__setDevicePixelRatio(ctx, 1.0f);
  196. if (ctx->params.renderCreate(ctx->params.userPtr) == 0) goto error;
  197. // Init font rendering
  198. memset(&fontParams, 0, sizeof(fontParams));
  199. fontParams.width = NVG_INIT_FONTIMAGE_SIZE;
  200. fontParams.height = NVG_INIT_FONTIMAGE_SIZE;
  201. fontParams.flags = FONS_ZERO_TOPLEFT;
  202. fontParams.renderCreate = NULL;
  203. fontParams.renderUpdate = NULL;
  204. fontParams.renderDraw = NULL;
  205. fontParams.renderDelete = NULL;
  206. fontParams.userPtr = NULL;
  207. ctx->fs = fonsCreateInternal(&fontParams);
  208. if (ctx->fs == NULL) goto error;
  209. // Create font texture
  210. ctx->fontImages[0] = ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_ALPHA, fontParams.width, fontParams.height, 0, NULL);
  211. if (ctx->fontImages[0] == 0) goto error;
  212. ctx->fontImageIdx = 0;
  213. return ctx;
  214. error:
  215. nvgDeleteInternal(ctx);
  216. return 0;
  217. }
  218. NVGparams* nvgInternalParams(NVGcontext* ctx)
  219. {
  220. return &ctx->params;
  221. }
  222. void nvgDeleteInternal(NVGcontext* ctx)
  223. {
  224. int i;
  225. if (ctx == NULL) return;
  226. if (ctx->commands != NULL) free(ctx->commands);
  227. if (ctx->cache != NULL) nvg__deletePathCache(ctx->cache);
  228. if (ctx->fs)
  229. fonsDeleteInternal(ctx->fs);
  230. for (i = 0; i < NVG_MAX_FONTIMAGES; i++) {
  231. if (ctx->fontImages[i] != 0) {
  232. nvgDeleteImage(ctx, ctx->fontImages[i]);
  233. ctx->fontImages[i] = 0;
  234. }
  235. }
  236. if (ctx->params.renderDelete != NULL)
  237. ctx->params.renderDelete(ctx->params.userPtr);
  238. free(ctx);
  239. }
  240. void nvgBeginFrame(NVGcontext* ctx, int windowWidth, int windowHeight, float devicePixelRatio)
  241. {
  242. /* printf("Tris: draws:%d fill:%d stroke:%d text:%d TOT:%d\n",
  243. ctx->drawCallCount, ctx->fillTriCount, ctx->strokeTriCount, ctx->textTriCount,
  244. ctx->fillTriCount+ctx->strokeTriCount+ctx->textTriCount);*/
  245. ctx->nstates = 0;
  246. nvgSave(ctx);
  247. nvgReset(ctx);
  248. nvg__setDevicePixelRatio(ctx, devicePixelRatio);
  249. ctx->params.renderViewport(ctx->params.userPtr, windowWidth, windowHeight);
  250. ctx->drawCallCount = 0;
  251. ctx->fillTriCount = 0;
  252. ctx->strokeTriCount = 0;
  253. ctx->textTriCount = 0;
  254. }
  255. void nvgEndFrame(NVGcontext* ctx)
  256. {
  257. ctx->params.renderFlush(ctx->params.userPtr);
  258. if (ctx->fontImageIdx != 0) {
  259. int fontImage = ctx->fontImages[ctx->fontImageIdx];
  260. int i, j, iw, ih;
  261. // delete images that smaller than current one
  262. if (fontImage == 0)
  263. return;
  264. nvgImageSize(ctx, fontImage, &iw, &ih);
  265. for (i = j = 0; i < ctx->fontImageIdx; i++) {
  266. if (ctx->fontImages[i] != 0) {
  267. int nw, nh;
  268. nvgImageSize(ctx, ctx->fontImages[i], &nw, &nh);
  269. if (nw < iw || nh < ih)
  270. nvgDeleteImage(ctx, ctx->fontImages[i]);
  271. else
  272. ctx->fontImages[j++] = ctx->fontImages[i];
  273. }
  274. }
  275. // make current font image to first
  276. ctx->fontImages[j++] = ctx->fontImages[0];
  277. ctx->fontImages[0] = fontImage;
  278. ctx->fontImageIdx = 0;
  279. // clear all images after j
  280. for (i = j; i < NVG_MAX_FONTIMAGES; i++)
  281. ctx->fontImages[i] = 0;
  282. }
  283. }
  284. NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b)
  285. {
  286. return nvgRGBA(r,g,b,255);
  287. }
  288. NVGcolor nvgRGBf(float r, float g, float b)
  289. {
  290. return nvgRGBAf(r,g,b,1.0f);
  291. }
  292. NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
  293. {
  294. NVGcolor color;
  295. // Use longer initialization to suppress warning.
  296. color.r = r / 255.0f;
  297. color.g = g / 255.0f;
  298. color.b = b / 255.0f;
  299. color.a = a / 255.0f;
  300. return color;
  301. }
  302. NVGcolor nvgRGBAf(float r, float g, float b, float a)
  303. {
  304. NVGcolor color;
  305. // Use longer initialization to suppress warning.
  306. color.r = r;
  307. color.g = g;
  308. color.b = b;
  309. color.a = a;
  310. return color;
  311. }
  312. NVGcolor nvgTransRGBA(NVGcolor c, unsigned char a)
  313. {
  314. c.a = a / 255.0f;
  315. return c;
  316. }
  317. NVGcolor nvgTransRGBAf(NVGcolor c, float a)
  318. {
  319. c.a = a;
  320. return c;
  321. }
  322. NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u)
  323. {
  324. int i;
  325. float oneminu;
  326. NVGcolor cint;
  327. u = nvg__clampf(u, 0.0f, 1.0f);
  328. oneminu = 1.0f - u;
  329. for( i = 0; i <4; i++ )
  330. {
  331. cint.rgba[i] = c0.rgba[i] * oneminu + c1.rgba[i] * u;
  332. }
  333. return cint;
  334. }
  335. NVGcolor nvgHSL(float h, float s, float l)
  336. {
  337. return nvgHSLA(h,s,l,255);
  338. }
  339. static float nvg__hue(float h, float m1, float m2)
  340. {
  341. if (h < 0) h += 1;
  342. if (h > 1) h -= 1;
  343. if (h < 1.0f/6.0f)
  344. return m1 + (m2 - m1) * h * 6.0f;
  345. else if (h < 3.0f/6.0f)
  346. return m2;
  347. else if (h < 4.0f/6.0f)
  348. return m1 + (m2 - m1) * (2.0f/3.0f - h) * 6.0f;
  349. return m1;
  350. }
  351. NVGcolor nvgHSLA(float h, float s, float l, unsigned char a)
  352. {
  353. float m1, m2;
  354. NVGcolor col;
  355. h = nvg__modf(h, 1.0f);
  356. if (h < 0.0f) h += 1.0f;
  357. s = nvg__clampf(s, 0.0f, 1.0f);
  358. l = nvg__clampf(l, 0.0f, 1.0f);
  359. m2 = l <= 0.5f ? (l * (1 + s)) : (l + s - l * s);
  360. m1 = 2 * l - m2;
  361. col.r = nvg__clampf(nvg__hue(h + 1.0f/3.0f, m1, m2), 0.0f, 1.0f);
  362. col.g = nvg__clampf(nvg__hue(h, m1, m2), 0.0f, 1.0f);
  363. col.b = nvg__clampf(nvg__hue(h - 1.0f/3.0f, m1, m2), 0.0f, 1.0f);
  364. col.a = a/255.0f;
  365. return col;
  366. }
  367. static NVGstate* nvg__getState(NVGcontext* ctx)
  368. {
  369. return &ctx->states[ctx->nstates-1];
  370. }
  371. void nvgTransformIdentity(float* t)
  372. {
  373. t[0] = 1.0f; t[1] = 0.0f;
  374. t[2] = 0.0f; t[3] = 1.0f;
  375. t[4] = 0.0f; t[5] = 0.0f;
  376. }
  377. void nvgTransformTranslate(float* t, float tx, float ty)
  378. {
  379. t[0] = 1.0f; t[1] = 0.0f;
  380. t[2] = 0.0f; t[3] = 1.0f;
  381. t[4] = tx; t[5] = ty;
  382. }
  383. void nvgTransformScale(float* t, float sx, float sy)
  384. {
  385. t[0] = sx; t[1] = 0.0f;
  386. t[2] = 0.0f; t[3] = sy;
  387. t[4] = 0.0f; t[5] = 0.0f;
  388. }
  389. void nvgTransformRotate(float* t, float a)
  390. {
  391. float cs = nvg__cosf(a), sn = nvg__sinf(a);
  392. t[0] = cs; t[1] = sn;
  393. t[2] = -sn; t[3] = cs;
  394. t[4] = 0.0f; t[5] = 0.0f;
  395. }
  396. void nvgTransformSkewX(float* t, float a)
  397. {
  398. t[0] = 1.0f; t[1] = 0.0f;
  399. t[2] = nvg__tanf(a); t[3] = 1.0f;
  400. t[4] = 0.0f; t[5] = 0.0f;
  401. }
  402. void nvgTransformSkewY(float* t, float a)
  403. {
  404. t[0] = 1.0f; t[1] = nvg__tanf(a);
  405. t[2] = 0.0f; t[3] = 1.0f;
  406. t[4] = 0.0f; t[5] = 0.0f;
  407. }
  408. void nvgTransformMultiply(float* t, const float* s)
  409. {
  410. float t0 = t[0] * s[0] + t[1] * s[2];
  411. float t2 = t[2] * s[0] + t[3] * s[2];
  412. float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
  413. t[1] = t[0] * s[1] + t[1] * s[3];
  414. t[3] = t[2] * s[1] + t[3] * s[3];
  415. t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
  416. t[0] = t0;
  417. t[2] = t2;
  418. t[4] = t4;
  419. }
  420. void nvgTransformPremultiply(float* t, const float* s)
  421. {
  422. float s2[6];
  423. memcpy(s2, s, sizeof(float)*6);
  424. nvgTransformMultiply(s2, t);
  425. memcpy(t, s2, sizeof(float)*6);
  426. }
  427. int nvgTransformInverse(float* inv, const float* t)
  428. {
  429. double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1];
  430. if (det > -1e-6 && det < 1e-6) {
  431. nvgTransformIdentity(inv);
  432. return 0;
  433. }
  434. invdet = 1.0 / det;
  435. inv[0] = (float)(t[3] * invdet);
  436. inv[2] = (float)(-t[2] * invdet);
  437. inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
  438. inv[1] = (float)(-t[1] * invdet);
  439. inv[3] = (float)(t[0] * invdet);
  440. inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
  441. return 1;
  442. }
  443. void nvgTransformPoint(float* dx, float* dy, const float* t, float sx, float sy)
  444. {
  445. *dx = sx*t[0] + sy*t[2] + t[4];
  446. *dy = sx*t[1] + sy*t[3] + t[5];
  447. }
  448. float nvgDegToRad(float deg)
  449. {
  450. return deg / 180.0f * NVG_PI;
  451. }
  452. float nvgRadToDeg(float rad)
  453. {
  454. return rad / NVG_PI * 180.0f;
  455. }
  456. static void nvg__setPaintColor(NVGpaint* p, NVGcolor color)
  457. {
  458. memset(p, 0, sizeof(*p));
  459. nvgTransformIdentity(p->xform);
  460. p->radius = 0.0f;
  461. p->feather = 1.0f;
  462. p->innerColor = color;
  463. p->outerColor = color;
  464. }
  465. // State handling
  466. void nvgSave(NVGcontext* ctx)
  467. {
  468. if (ctx->nstates >= NVG_MAX_STATES)
  469. return;
  470. if (ctx->nstates > 0)
  471. memcpy(&ctx->states[ctx->nstates], &ctx->states[ctx->nstates-1], sizeof(NVGstate));
  472. ctx->nstates++;
  473. }
  474. void nvgRestore(NVGcontext* ctx)
  475. {
  476. if (ctx->nstates <= 1)
  477. return;
  478. ctx->nstates--;
  479. }
  480. void nvgReset(NVGcontext* ctx)
  481. {
  482. NVGstate* state = nvg__getState(ctx);
  483. memset(state, 0, sizeof(*state));
  484. nvg__setPaintColor(&state->fill, nvgRGBA(255,255,255,255));
  485. nvg__setPaintColor(&state->stroke, nvgRGBA(0,0,0,255));
  486. state->strokeWidth = 1.0f;
  487. state->miterLimit = 10.0f;
  488. state->lineCap = NVG_BUTT;
  489. state->lineJoin = NVG_MITER;
  490. state->alpha = 1.0f;
  491. nvgTransformIdentity(state->xform);
  492. state->scissor.extent[0] = -1.0f;
  493. state->scissor.extent[1] = -1.0f;
  494. state->fontSize = 16.0f;
  495. state->letterSpacing = 0.0f;
  496. state->lineHeight = 1.0f;
  497. state->fontBlur = 0.0f;
  498. state->textAlign = NVG_ALIGN_LEFT | NVG_ALIGN_BASELINE;
  499. state->fontId = 0;
  500. }
  501. // State setting
  502. void nvgStrokeWidth(NVGcontext* ctx, float width)
  503. {
  504. NVGstate* state = nvg__getState(ctx);
  505. state->strokeWidth = width;
  506. }
  507. void nvgMiterLimit(NVGcontext* ctx, float limit)
  508. {
  509. NVGstate* state = nvg__getState(ctx);
  510. state->miterLimit = limit;
  511. }
  512. void nvgLineCap(NVGcontext* ctx, int cap)
  513. {
  514. NVGstate* state = nvg__getState(ctx);
  515. state->lineCap = cap;
  516. }
  517. void nvgLineJoin(NVGcontext* ctx, int join)
  518. {
  519. NVGstate* state = nvg__getState(ctx);
  520. state->lineJoin = join;
  521. }
  522. void nvgGlobalAlpha(NVGcontext* ctx, float alpha)
  523. {
  524. NVGstate* state = nvg__getState(ctx);
  525. state->alpha = alpha;
  526. }
  527. void nvgTransform(NVGcontext* ctx, float a, float b, float c, float d, float e, float f)
  528. {
  529. NVGstate* state = nvg__getState(ctx);
  530. float t[6] = { a, b, c, d, e, f };
  531. nvgTransformPremultiply(state->xform, t);
  532. }
  533. void nvgResetTransform(NVGcontext* ctx)
  534. {
  535. NVGstate* state = nvg__getState(ctx);
  536. nvgTransformIdentity(state->xform);
  537. }
  538. void nvgTranslate(NVGcontext* ctx, float x, float y)
  539. {
  540. NVGstate* state = nvg__getState(ctx);
  541. float t[6];
  542. nvgTransformTranslate(t, x,y);
  543. nvgTransformPremultiply(state->xform, t);
  544. }
  545. void nvgRotate(NVGcontext* ctx, float angle)
  546. {
  547. NVGstate* state = nvg__getState(ctx);
  548. float t[6];
  549. nvgTransformRotate(t, angle);
  550. nvgTransformPremultiply(state->xform, t);
  551. }
  552. void nvgSkewX(NVGcontext* ctx, float angle)
  553. {
  554. NVGstate* state = nvg__getState(ctx);
  555. float t[6];
  556. nvgTransformSkewX(t, angle);
  557. nvgTransformPremultiply(state->xform, t);
  558. }
  559. void nvgSkewY(NVGcontext* ctx, float angle)
  560. {
  561. NVGstate* state = nvg__getState(ctx);
  562. float t[6];
  563. nvgTransformSkewY(t, angle);
  564. nvgTransformPremultiply(state->xform, t);
  565. }
  566. void nvgScale(NVGcontext* ctx, float x, float y)
  567. {
  568. NVGstate* state = nvg__getState(ctx);
  569. float t[6];
  570. nvgTransformScale(t, x,y);
  571. nvgTransformPremultiply(state->xform, t);
  572. }
  573. void nvgCurrentTransform(NVGcontext* ctx, float* xform)
  574. {
  575. NVGstate* state = nvg__getState(ctx);
  576. if (xform == NULL) return;
  577. memcpy(xform, state->xform, sizeof(float)*6);
  578. }
  579. void nvgStrokeColor(NVGcontext* ctx, NVGcolor color)
  580. {
  581. NVGstate* state = nvg__getState(ctx);
  582. nvg__setPaintColor(&state->stroke, color);
  583. }
  584. void nvgStrokePaint(NVGcontext* ctx, NVGpaint paint)
  585. {
  586. NVGstate* state = nvg__getState(ctx);
  587. state->stroke = paint;
  588. nvgTransformMultiply(state->stroke.xform, state->xform);
  589. }
  590. void nvgFillColor(NVGcontext* ctx, NVGcolor color)
  591. {
  592. NVGstate* state = nvg__getState(ctx);
  593. nvg__setPaintColor(&state->fill, color);
  594. }
  595. void nvgFillPaint(NVGcontext* ctx, NVGpaint paint)
  596. {
  597. NVGstate* state = nvg__getState(ctx);
  598. state->fill = paint;
  599. nvgTransformMultiply(state->fill.xform, state->xform);
  600. }
  601. int nvgCreateImage(NVGcontext* ctx, const char* filename, int imageFlags)
  602. {
  603. int w, h, n, image;
  604. unsigned char* img;
  605. stbi_set_unpremultiply_on_load(1);
  606. stbi_convert_iphone_png_to_rgb(1);
  607. img = stbi_load(filename, &w, &h, &n, 4);
  608. if (img == NULL) {
  609. // printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
  610. return 0;
  611. }
  612. image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
  613. stbi_image_free(img);
  614. return image;
  615. }
  616. int nvgCreateImageMem(NVGcontext* ctx, int imageFlags, unsigned char* data, int ndata)
  617. {
  618. int w, h, n, image;
  619. unsigned char* img = stbi_load_from_memory(data, ndata, &w, &h, &n, 4);
  620. if (img == NULL) {
  621. // printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
  622. return 0;
  623. }
  624. image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
  625. stbi_image_free(img);
  626. return image;
  627. }
  628. int nvgCreateImageRGBA(NVGcontext* ctx, int w, int h, int imageFlags, const unsigned char* data)
  629. {
  630. return ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_RGBA, w, h, imageFlags, data);
  631. }
  632. void nvgUpdateImage(NVGcontext* ctx, int image, const unsigned char* data)
  633. {
  634. int w, h;
  635. ctx->params.renderGetTextureSize(ctx->params.userPtr, image, &w, &h);
  636. ctx->params.renderUpdateTexture(ctx->params.userPtr, image, 0,0, w,h, data);
  637. }
  638. void nvgImageSize(NVGcontext* ctx, int image, int* w, int* h)
  639. {
  640. ctx->params.renderGetTextureSize(ctx->params.userPtr, image, w, h);
  641. }
  642. void nvgDeleteImage(NVGcontext* ctx, int image)
  643. {
  644. ctx->params.renderDeleteTexture(ctx->params.userPtr, image);
  645. }
  646. NVGpaint nvgLinearGradient(NVGcontext* ctx,
  647. float sx, float sy, float ex, float ey,
  648. NVGcolor icol, NVGcolor ocol)
  649. {
  650. NVGpaint p;
  651. float dx, dy, d;
  652. const float large = 1e5;
  653. NVG_NOTUSED(ctx);
  654. memset(&p, 0, sizeof(p));
  655. // Calculate transform aligned to the line
  656. dx = ex - sx;
  657. dy = ey - sy;
  658. d = sqrtf(dx*dx + dy*dy);
  659. if (d > 0.0001f) {
  660. dx /= d;
  661. dy /= d;
  662. } else {
  663. dx = 0;
  664. dy = 1;
  665. }
  666. p.xform[0] = dy; p.xform[1] = -dx;
  667. p.xform[2] = dx; p.xform[3] = dy;
  668. p.xform[4] = sx - dx*large; p.xform[5] = sy - dy*large;
  669. p.extent[0] = large;
  670. p.extent[1] = large + d*0.5f;
  671. p.radius = 0.0f;
  672. p.feather = nvg__maxf(1.0f, d);
  673. p.innerColor = icol;
  674. p.outerColor = ocol;
  675. return p;
  676. }
  677. NVGpaint nvgRadialGradient(NVGcontext* ctx,
  678. float cx, float cy, float inr, float outr,
  679. NVGcolor icol, NVGcolor ocol)
  680. {
  681. NVGpaint p;
  682. float r = (inr+outr)*0.5f;
  683. float f = (outr-inr);
  684. NVG_NOTUSED(ctx);
  685. memset(&p, 0, sizeof(p));
  686. nvgTransformIdentity(p.xform);
  687. p.xform[4] = cx;
  688. p.xform[5] = cy;
  689. p.extent[0] = r;
  690. p.extent[1] = r;
  691. p.radius = r;
  692. p.feather = nvg__maxf(1.0f, f);
  693. p.innerColor = icol;
  694. p.outerColor = ocol;
  695. return p;
  696. }
  697. NVGpaint nvgBoxGradient(NVGcontext* ctx,
  698. float x, float y, float w, float h, float r, float f,
  699. NVGcolor icol, NVGcolor ocol)
  700. {
  701. NVGpaint p;
  702. NVG_NOTUSED(ctx);
  703. memset(&p, 0, sizeof(p));
  704. nvgTransformIdentity(p.xform);
  705. p.xform[4] = x+w*0.5f;
  706. p.xform[5] = y+h*0.5f;
  707. p.extent[0] = w*0.5f;
  708. p.extent[1] = h*0.5f;
  709. p.radius = r;
  710. p.feather = nvg__maxf(1.0f, f);
  711. p.innerColor = icol;
  712. p.outerColor = ocol;
  713. return p;
  714. }
  715. NVGpaint nvgImagePattern(NVGcontext* ctx,
  716. float cx, float cy, float w, float h, float angle,
  717. int image, int repeat, float alpha)
  718. {
  719. NVGpaint p;
  720. NVG_NOTUSED(ctx);
  721. memset(&p, 0, sizeof(p));
  722. nvgTransformRotate(p.xform, angle);
  723. p.xform[4] = cx;
  724. p.xform[5] = cy;
  725. p.extent[0] = w;
  726. p.extent[1] = h;
  727. p.image = image;
  728. p.repeat = repeat;
  729. p.innerColor = p.outerColor = nvgRGBAf(1,1,1,alpha);
  730. return p;
  731. }
  732. // Scissoring
  733. void nvgScissor(NVGcontext* ctx, float x, float y, float w, float h)
  734. {
  735. NVGstate* state = nvg__getState(ctx);
  736. w = nvg__maxf(0.0f, w);
  737. h = nvg__maxf(0.0f, h);
  738. nvgTransformIdentity(state->scissor.xform);
  739. state->scissor.xform[4] = x+w*0.5f;
  740. state->scissor.xform[5] = y+h*0.5f;
  741. nvgTransformMultiply(state->scissor.xform, state->xform);
  742. state->scissor.extent[0] = w*0.5f;
  743. state->scissor.extent[1] = h*0.5f;
  744. }
  745. static void nvg__isectRects(float* dst,
  746. float ax, float ay, float aw, float ah,
  747. float bx, float by, float bw, float bh)
  748. {
  749. float minx = nvg__maxf(ax, bx);
  750. float miny = nvg__maxf(ay, by);
  751. float maxx = nvg__minf(ax+aw, bx+bw);
  752. float maxy = nvg__minf(ay+ah, by+bh);
  753. dst[0] = minx;
  754. dst[1] = miny;
  755. dst[2] = nvg__maxf(0.0f, maxx - minx);
  756. dst[3] = nvg__maxf(0.0f, maxy - miny);
  757. }
  758. void nvgIntersectScissor(NVGcontext* ctx, float x, float y, float w, float h)
  759. {
  760. NVGstate* state = nvg__getState(ctx);
  761. float pxform[6], invxorm[6];
  762. float rect[4];
  763. float ex, ey, tex, tey;
  764. // If no previous scissor has been set, set the scissor as current scissor.
  765. if (state->scissor.extent[0] < 0) {
  766. nvgScissor(ctx, x, y, w, h);
  767. return;
  768. }
  769. // Transform the current scissor rect into current transform space.
  770. // If there is difference in rotation, this will be approximation.
  771. memcpy(pxform, state->scissor.xform, sizeof(float)*6);
  772. ex = state->scissor.extent[0];
  773. ey = state->scissor.extent[1];
  774. nvgTransformInverse(invxorm, state->xform);
  775. nvgTransformMultiply(pxform, invxorm);
  776. tex = ex*nvg__absf(pxform[0]) + ey*nvg__absf(pxform[2]);
  777. tey = ex*nvg__absf(pxform[1]) + ey*nvg__absf(pxform[3]);
  778. // Intersect rects.
  779. nvg__isectRects(rect, pxform[4]-tex,pxform[5]-tey,tex*2,tey*2, x,y,w,h);
  780. nvgScissor(ctx, rect[0], rect[1], rect[2], rect[3]);
  781. }
  782. void nvgResetScissor(NVGcontext* ctx)
  783. {
  784. NVGstate* state = nvg__getState(ctx);
  785. memset(state->scissor.xform, 0, sizeof(state->scissor.xform));
  786. state->scissor.extent[0] = -1.0f;
  787. state->scissor.extent[1] = -1.0f;
  788. }
  789. static int nvg__ptEquals(float x1, float y1, float x2, float y2, float tol)
  790. {
  791. float dx = x2 - x1;
  792. float dy = y2 - y1;
  793. return dx*dx + dy*dy < tol*tol;
  794. }
  795. static float nvg__distPtSeg(float x, float y, float px, float py, float qx, float qy)
  796. {
  797. float pqx, pqy, dx, dy, d, t;
  798. pqx = qx-px;
  799. pqy = qy-py;
  800. dx = x-px;
  801. dy = y-py;
  802. d = pqx*pqx + pqy*pqy;
  803. t = pqx*dx + pqy*dy;
  804. if (d > 0) t /= d;
  805. if (t < 0) t = 0;
  806. else if (t > 1) t = 1;
  807. dx = px + t*pqx - x;
  808. dy = py + t*pqy - y;
  809. return dx*dx + dy*dy;
  810. }
  811. static void nvg__appendCommands(NVGcontext* ctx, float* vals, int nvals)
  812. {
  813. NVGstate* state = nvg__getState(ctx);
  814. int i;
  815. if (ctx->ncommands+nvals > ctx->ccommands) {
  816. float* commands;
  817. int ccommands = ctx->ncommands+nvals + ctx->ccommands/2;
  818. commands = (float*)realloc(ctx->commands, sizeof(float)*ccommands);
  819. if (commands == NULL) return;
  820. ctx->commands = commands;
  821. ctx->ccommands = ccommands;
  822. }
  823. if ((int)vals[0] != NVG_CLOSE && (int)vals[0] != NVG_WINDING) {
  824. ctx->commandx = vals[nvals-2];
  825. ctx->commandy = vals[nvals-1];
  826. }
  827. // transform commands
  828. i = 0;
  829. while (i < nvals) {
  830. int cmd = (int)vals[i];
  831. switch (cmd) {
  832. case NVG_MOVETO:
  833. nvgTransformPoint(&vals[i+1],&vals[i+2], state->xform, vals[i+1],vals[i+2]);
  834. i += 3;
  835. break;
  836. case NVG_LINETO:
  837. nvgTransformPoint(&vals[i+1],&vals[i+2], state->xform, vals[i+1],vals[i+2]);
  838. i += 3;
  839. break;
  840. case NVG_BEZIERTO:
  841. nvgTransformPoint(&vals[i+1],&vals[i+2], state->xform, vals[i+1],vals[i+2]);
  842. nvgTransformPoint(&vals[i+3],&vals[i+4], state->xform, vals[i+3],vals[i+4]);
  843. nvgTransformPoint(&vals[i+5],&vals[i+6], state->xform, vals[i+5],vals[i+6]);
  844. i += 7;
  845. break;
  846. case NVG_CLOSE:
  847. i++;
  848. break;
  849. case NVG_WINDING:
  850. i += 2;
  851. break;
  852. default:
  853. i++;
  854. }
  855. }
  856. memcpy(&ctx->commands[ctx->ncommands], vals, nvals*sizeof(float));
  857. ctx->ncommands += nvals;
  858. }
  859. static void nvg__clearPathCache(NVGcontext* ctx)
  860. {
  861. ctx->cache->npoints = 0;
  862. ctx->cache->npaths = 0;
  863. }
  864. static NVGpath* nvg__lastPath(NVGcontext* ctx)
  865. {
  866. if (ctx->cache->npaths > 0)
  867. return &ctx->cache->paths[ctx->cache->npaths-1];
  868. return NULL;
  869. }
  870. static void nvg__addPath(NVGcontext* ctx)
  871. {
  872. NVGpath* path;
  873. if (ctx->cache->npaths+1 > ctx->cache->cpaths) {
  874. NVGpath* paths;
  875. int cpaths = ctx->cache->npaths+1 + ctx->cache->cpaths/2;
  876. paths = (NVGpath*)realloc(ctx->cache->paths, sizeof(NVGpath)*cpaths);
  877. if (paths == NULL) return;
  878. ctx->cache->paths = paths;
  879. ctx->cache->cpaths = cpaths;
  880. }
  881. path = &ctx->cache->paths[ctx->cache->npaths];
  882. memset(path, 0, sizeof(*path));
  883. path->first = ctx->cache->npoints;
  884. path->winding = NVG_CCW;
  885. ctx->cache->npaths++;
  886. }
  887. static NVGpoint* nvg__lastPoint(NVGcontext* ctx)
  888. {
  889. if (ctx->cache->npoints > 0)
  890. return &ctx->cache->points[ctx->cache->npoints-1];
  891. return NULL;
  892. }
  893. static void nvg__addPoint(NVGcontext* ctx, float x, float y, int flags)
  894. {
  895. NVGpath* path = nvg__lastPath(ctx);
  896. NVGpoint* pt;
  897. if (path == NULL) return;
  898. if (ctx->cache->npoints > 0) {
  899. pt = nvg__lastPoint(ctx);
  900. if (nvg__ptEquals(pt->x,pt->y, x,y, ctx->distTol)) {
  901. pt->flags |= flags;
  902. return;
  903. }
  904. }
  905. if (ctx->cache->npoints+1 > ctx->cache->cpoints) {
  906. NVGpoint* points;
  907. int cpoints = ctx->cache->npoints+1 + ctx->cache->cpoints/2;
  908. points = (NVGpoint*)realloc(ctx->cache->points, sizeof(NVGpoint)*cpoints);
  909. if (points == NULL) return;
  910. ctx->cache->points = points;
  911. ctx->cache->cpoints = cpoints;
  912. }
  913. pt = &ctx->cache->points[ctx->cache->npoints];
  914. memset(pt, 0, sizeof(*pt));
  915. pt->x = x;
  916. pt->y = y;
  917. pt->flags = (unsigned char)flags;
  918. ctx->cache->npoints++;
  919. path->count++;
  920. }
  921. static void nvg__closePath(NVGcontext* ctx)
  922. {
  923. NVGpath* path = nvg__lastPath(ctx);
  924. if (path == NULL) return;
  925. path->closed = 1;
  926. }
  927. static void nvg__pathWinding(NVGcontext* ctx, int winding)
  928. {
  929. NVGpath* path = nvg__lastPath(ctx);
  930. if (path == NULL) return;
  931. path->winding = winding;
  932. }
  933. static float nvg__getAverageScale(float *t)
  934. {
  935. float sx = sqrtf(t[0]*t[0] + t[2]*t[2]);
  936. float sy = sqrtf(t[1]*t[1] + t[3]*t[3]);
  937. return (sx + sy) * 0.5f;
  938. }
  939. static NVGvertex* nvg__allocTempVerts(NVGcontext* ctx, int nverts)
  940. {
  941. if (nverts > ctx->cache->cverts) {
  942. NVGvertex* verts;
  943. int cverts = (nverts + 0xff) & ~0xff; // Round up to prevent allocations when things change just slightly.
  944. verts = (NVGvertex*)realloc(ctx->cache->verts, sizeof(NVGvertex)*cverts);
  945. if (verts == NULL) return NULL;
  946. ctx->cache->verts = verts;
  947. ctx->cache->cverts = cverts;
  948. }
  949. return ctx->cache->verts;
  950. }
  951. static float nvg__triarea2(float ax, float ay, float bx, float by, float cx, float cy)
  952. {
  953. float abx = bx - ax;
  954. float aby = by - ay;
  955. float acx = cx - ax;
  956. float acy = cy - ay;
  957. return acx*aby - abx*acy;
  958. }
  959. static float nvg__polyArea(NVGpoint* pts, int npts)
  960. {
  961. int i;
  962. float area = 0;
  963. for (i = 2; i < npts; i++) {
  964. NVGpoint* a = &pts[0];
  965. NVGpoint* b = &pts[i-1];
  966. NVGpoint* c = &pts[i];
  967. area += nvg__triarea2(a->x,a->y, b->x,b->y, c->x,c->y);
  968. }
  969. return area * 0.5f;
  970. }
  971. static void nvg__polyReverse(NVGpoint* pts, int npts)
  972. {
  973. NVGpoint tmp;
  974. int i = 0, j = npts-1;
  975. while (i < j) {
  976. tmp = pts[i];
  977. pts[i] = pts[j];
  978. pts[j] = tmp;
  979. i++;
  980. j--;
  981. }
  982. }
  983. static void nvg__vset(NVGvertex* vtx, float x, float y, float u, float v)
  984. {
  985. vtx->x = x;
  986. vtx->y = y;
  987. vtx->u = u;
  988. vtx->v = v;
  989. }
  990. static void nvg__tesselateBezier(NVGcontext* ctx,
  991. float x1, float y1, float x2, float y2,
  992. float x3, float y3, float x4, float y4,
  993. int level, int type)
  994. {
  995. float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234;
  996. float dx,dy,d2,d3;
  997. if (level > 10) return;
  998. x12 = (x1+x2)*0.5f;
  999. y12 = (y1+y2)*0.5f;
  1000. x23 = (x2+x3)*0.5f;
  1001. y23 = (y2+y3)*0.5f;
  1002. x34 = (x3+x4)*0.5f;
  1003. y34 = (y3+y4)*0.5f;
  1004. x123 = (x12+x23)*0.5f;
  1005. y123 = (y12+y23)*0.5f;
  1006. dx = x4 - x1;
  1007. dy = y4 - y1;
  1008. d2 = nvg__absf(((x2 - x4) * dy - (y2 - y4) * dx));
  1009. d3 = nvg__absf(((x3 - x4) * dy - (y3 - y4) * dx));
  1010. if ((d2 + d3)*(d2 + d3) < ctx->tessTol * (dx*dx + dy*dy)) {
  1011. nvg__addPoint(ctx, x4, y4, type);
  1012. return;
  1013. }
  1014. /* if (nvg__absf(x1+x3-x2-x2) + nvg__absf(y1+y3-y2-y2) + nvg__absf(x2+x4-x3-x3) + nvg__absf(y2+y4-y3-y3) < ctx->tessTol) {
  1015. nvg__addPoint(ctx, x4, y4, type);
  1016. return;
  1017. }*/
  1018. x234 = (x23+x34)*0.5f;
  1019. y234 = (y23+y34)*0.5f;
  1020. x1234 = (x123+x234)*0.5f;
  1021. y1234 = (y123+y234)*0.5f;
  1022. nvg__tesselateBezier(ctx, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1, 0);
  1023. nvg__tesselateBezier(ctx, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1, type);
  1024. }
  1025. static void nvg__flattenPaths(NVGcontext* ctx)
  1026. {
  1027. NVGpathCache* cache = ctx->cache;
  1028. // NVGstate* state = nvg__getState(ctx);
  1029. NVGpoint* last;
  1030. NVGpoint* p0;
  1031. NVGpoint* p1;
  1032. NVGpoint* pts;
  1033. NVGpath* path;
  1034. int i, j;
  1035. float* cp1;
  1036. float* cp2;
  1037. float* p;
  1038. float area;
  1039. if (cache->npaths > 0)
  1040. return;
  1041. // Flatten
  1042. i = 0;
  1043. while (i < ctx->ncommands) {
  1044. int cmd = (int)ctx->commands[i];
  1045. switch (cmd) {
  1046. case NVG_MOVETO:
  1047. nvg__addPath(ctx);
  1048. p = &ctx->commands[i+1];
  1049. nvg__addPoint(ctx, p[0], p[1], NVG_PT_CORNER);
  1050. i += 3;
  1051. break;
  1052. case NVG_LINETO:
  1053. p = &ctx->commands[i+1];
  1054. nvg__addPoint(ctx, p[0], p[1], NVG_PT_CORNER);
  1055. i += 3;
  1056. break;
  1057. case NVG_BEZIERTO:
  1058. last = nvg__lastPoint(ctx);
  1059. if (last != NULL) {
  1060. cp1 = &ctx->commands[i+1];
  1061. cp2 = &ctx->commands[i+3];
  1062. p = &ctx->commands[i+5];
  1063. nvg__tesselateBezier(ctx, last->x,last->y, cp1[0],cp1[1], cp2[0],cp2[1], p[0],p[1], 0, NVG_PT_CORNER);
  1064. }
  1065. i += 7;
  1066. break;
  1067. case NVG_CLOSE:
  1068. nvg__closePath(ctx);
  1069. i++;
  1070. break;
  1071. case NVG_WINDING:
  1072. nvg__pathWinding(ctx, (int)ctx->commands[i+1]);
  1073. i += 2;
  1074. break;
  1075. default:
  1076. i++;
  1077. }
  1078. }
  1079. cache->bounds[0] = cache->bounds[1] = 1e6f;
  1080. cache->bounds[2] = cache->bounds[3] = -1e6f;
  1081. // Calculate the direction and length of line segments.
  1082. for (j = 0; j < cache->npaths; j++) {
  1083. path = &cache->paths[j];
  1084. pts = &cache->points[path->first];
  1085. // If the first and last points are the same, remove the last, mark as closed path.
  1086. p0 = &pts[path->count-1];
  1087. p1 = &pts[0];
  1088. if (nvg__ptEquals(p0->x,p0->y, p1->x,p1->y, ctx->distTol)) {
  1089. path->count--;
  1090. p0 = &pts[path->count-1];
  1091. path->closed = 1;
  1092. }
  1093. // Enforce winding.
  1094. if (path->count > 2) {
  1095. area = nvg__polyArea(pts, path->count);
  1096. if (path->winding == NVG_CCW && area < 0.0f)
  1097. nvg__polyReverse(pts, path->count);
  1098. if (path->winding == NVG_CW && area > 0.0f)
  1099. nvg__polyReverse(pts, path->count);
  1100. }
  1101. for(i = 0; i < path->count; i++) {
  1102. // Calculate segment direction and length
  1103. p0->dx = p1->x - p0->x;
  1104. p0->dy = p1->y - p0->y;
  1105. p0->len = nvg__normalize(&p0->dx, &p0->dy);
  1106. // Update bounds
  1107. cache->bounds[0] = nvg__minf(cache->bounds[0], p0->x);
  1108. cache->bounds[1] = nvg__minf(cache->bounds[1], p0->y);
  1109. cache->bounds[2] = nvg__maxf(cache->bounds[2], p0->x);
  1110. cache->bounds[3] = nvg__maxf(cache->bounds[3], p0->y);
  1111. // Advance
  1112. p0 = p1++;
  1113. }
  1114. }
  1115. }
  1116. static int nvg__curveDivs(float r, float arc, float tol)
  1117. {
  1118. float da = acosf(r / (r + tol)) * 2.0f;
  1119. return nvg__maxi(2, (int)ceilf(arc / da));
  1120. }
  1121. static void nvg__chooseBevel(int bevel, NVGpoint* p0, NVGpoint* p1, float w,
  1122. float* x0, float* y0, float* x1, float* y1)
  1123. {
  1124. if (bevel) {
  1125. *x0 = p1->x + p0->dy * w;
  1126. *y0 = p1->y - p0->dx * w;
  1127. *x1 = p1->x + p1->dy * w;
  1128. *y1 = p1->y - p1->dx * w;
  1129. } else {
  1130. *x0 = p1->x + p1->dmx * w;
  1131. *y0 = p1->y + p1->dmy * w;
  1132. *x1 = p1->x + p1->dmx * w;
  1133. *y1 = p1->y + p1->dmy * w;
  1134. }
  1135. }
  1136. static NVGvertex* nvg__roundJoin(NVGvertex* dst, NVGpoint* p0, NVGpoint* p1,
  1137. float lw, float rw, float lu, float ru, int ncap, float fringe)
  1138. {
  1139. int i, n;
  1140. float dlx0 = p0->dy;
  1141. float dly0 = -p0->dx;
  1142. float dlx1 = p1->dy;
  1143. float dly1 = -p1->dx;
  1144. NVG_NOTUSED(fringe);
  1145. if (p1->flags & NVG_PT_LEFT) {
  1146. float lx0,ly0,lx1,ly1,a0,a1;
  1147. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, lw, &lx0,&ly0, &lx1,&ly1);
  1148. a0 = atan2f(-dly0, -dlx0);
  1149. a1 = atan2f(-dly1, -dlx1);
  1150. if (a1 > a0) a1 -= NVG_PI*2;
  1151. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1152. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1153. n = nvg__clampi((int)ceilf(((a0 - a1) / NVG_PI) * ncap), 2, ncap);
  1154. for (i = 0; i < n; i++) {
  1155. float u = i/(float)(n-1);
  1156. float a = a0 + u*(a1-a0);
  1157. float rx = p1->x + cosf(a) * rw;
  1158. float ry = p1->y + sinf(a) * rw;
  1159. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1160. nvg__vset(dst, rx, ry, ru,1); dst++;
  1161. }
  1162. nvg__vset(dst, lx1, ly1, lu,1); dst++;
  1163. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1164. } else {
  1165. float rx0,ry0,rx1,ry1,a0,a1;
  1166. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, -rw, &rx0,&ry0, &rx1,&ry1);
  1167. a0 = atan2f(dly0, dlx0);
  1168. a1 = atan2f(dly1, dlx1);
  1169. if (a1 < a0) a1 += NVG_PI*2;
  1170. nvg__vset(dst, p1->x + dlx0*rw, p1->y + dly0*rw, lu,1); dst++;
  1171. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1172. n = nvg__clampi((int)ceilf(((a1 - a0) / NVG_PI) * ncap), 2, ncap);
  1173. for (i = 0; i < n; i++) {
  1174. float u = i/(float)(n-1);
  1175. float a = a0 + u*(a1-a0);
  1176. float lx = p1->x + cosf(a) * lw;
  1177. float ly = p1->y + sinf(a) * lw;
  1178. nvg__vset(dst, lx, ly, lu,1); dst++;
  1179. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1180. }
  1181. nvg__vset(dst, p1->x + dlx1*rw, p1->y + dly1*rw, lu,1); dst++;
  1182. nvg__vset(dst, rx1, ry1, ru,1); dst++;
  1183. }
  1184. return dst;
  1185. }
  1186. static NVGvertex* nvg__bevelJoin(NVGvertex* dst, NVGpoint* p0, NVGpoint* p1,
  1187. float lw, float rw, float lu, float ru, float fringe)
  1188. {
  1189. float rx0,ry0,rx1,ry1;
  1190. float lx0,ly0,lx1,ly1;
  1191. float dlx0 = p0->dy;
  1192. float dly0 = -p0->dx;
  1193. float dlx1 = p1->dy;
  1194. float dly1 = -p1->dx;
  1195. NVG_NOTUSED(fringe);
  1196. if (p1->flags & NVG_PT_LEFT) {
  1197. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, lw, &lx0,&ly0, &lx1,&ly1);
  1198. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1199. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1200. if (p1->flags & NVG_PT_BEVEL) {
  1201. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1202. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1203. nvg__vset(dst, lx1, ly1, lu,1); dst++;
  1204. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1205. } else {
  1206. rx0 = p1->x - p1->dmx * rw;
  1207. ry0 = p1->y - p1->dmy * rw;
  1208. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1209. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1210. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1211. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1212. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1213. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1214. }
  1215. nvg__vset(dst, lx1, ly1, lu,1); dst++;
  1216. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1217. } else {
  1218. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, -rw, &rx0,&ry0, &rx1,&ry1);
  1219. nvg__vset(dst, p1->x + dlx0*lw, p1->y + dly0*lw, lu,1); dst++;
  1220. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1221. if (p1->flags & NVG_PT_BEVEL) {
  1222. nvg__vset(dst, p1->x + dlx0*lw, p1->y + dly0*lw, lu,1); dst++;
  1223. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1224. nvg__vset(dst, p1->x + dlx1*lw, p1->y + dly1*lw, lu,1); dst++;
  1225. nvg__vset(dst, rx1, ry1, ru,1); dst++;
  1226. } else {
  1227. lx0 = p1->x + p1->dmx * lw;
  1228. ly0 = p1->y + p1->dmy * lw;
  1229. nvg__vset(dst, p1->x + dlx0*lw, p1->y + dly0*lw, lu,1); dst++;
  1230. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1231. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1232. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1233. nvg__vset(dst, p1->x + dlx1*lw, p1->y + dly1*lw, lu,1); dst++;
  1234. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1235. }
  1236. nvg__vset(dst, p1->x + dlx1*lw, p1->y + dly1*lw, lu,1); dst++;
  1237. nvg__vset(dst, rx1, ry1, ru,1); dst++;
  1238. }
  1239. return dst;
  1240. }
  1241. static NVGvertex* nvg__buttCapStart(NVGvertex* dst, NVGpoint* p,
  1242. float dx, float dy, float w, float d, float aa)
  1243. {
  1244. float px = p->x - dx*d;
  1245. float py = p->y - dy*d;
  1246. float dlx = dy;
  1247. float dly = -dx;
  1248. nvg__vset(dst, px + dlx*w - dx*aa, py + dly*w - dy*aa, 0,0); dst++;
  1249. nvg__vset(dst, px - dlx*w - dx*aa, py - dly*w - dy*aa, 1,0); dst++;
  1250. nvg__vset(dst, px + dlx*w, py + dly*w, 0,1); dst++;
  1251. nvg__vset(dst, px - dlx*w, py - dly*w, 1,1); dst++;
  1252. return dst;
  1253. }
  1254. static NVGvertex* nvg__buttCapEnd(NVGvertex* dst, NVGpoint* p,
  1255. float dx, float dy, float w, float d, float aa)
  1256. {
  1257. float px = p->x + dx*d;
  1258. float py = p->y + dy*d;
  1259. float dlx = dy;
  1260. float dly = -dx;
  1261. nvg__vset(dst, px + dlx*w, py + dly*w, 0,1); dst++;
  1262. nvg__vset(dst, px - dlx*w, py - dly*w, 1,1); dst++;
  1263. nvg__vset(dst, px + dlx*w + dx*aa, py + dly*w + dy*aa, 0,0); dst++;
  1264. nvg__vset(dst, px - dlx*w + dx*aa, py - dly*w + dy*aa, 1,0); dst++;
  1265. return dst;
  1266. }
  1267. static NVGvertex* nvg__roundCapStart(NVGvertex* dst, NVGpoint* p,
  1268. float dx, float dy, float w, int ncap, float aa)
  1269. {
  1270. int i;
  1271. float px = p->x;
  1272. float py = p->y;
  1273. float dlx = dy;
  1274. float dly = -dx;
  1275. NVG_NOTUSED(aa);
  1276. for (i = 0; i < ncap; i++) {
  1277. float a = i/(float)(ncap-1)*NVG_PI;
  1278. float ax = cosf(a) * w, ay = sinf(a) * w;
  1279. nvg__vset(dst, px - dlx*ax - dx*ay, py - dly*ax - dy*ay, 0,1); dst++;
  1280. nvg__vset(dst, px, py, 0.5f,1); dst++;
  1281. }
  1282. nvg__vset(dst, px + dlx*w, py + dly*w, 0,1); dst++;
  1283. nvg__vset(dst, px - dlx*w, py - dly*w, 1,1); dst++;
  1284. return dst;
  1285. }
  1286. static NVGvertex* nvg__roundCapEnd(NVGvertex* dst, NVGpoint* p,
  1287. float dx, float dy, float w, int ncap, float aa)
  1288. {
  1289. int i;
  1290. float px = p->x;
  1291. float py = p->y;
  1292. float dlx = dy;
  1293. float dly = -dx;
  1294. NVG_NOTUSED(aa);
  1295. nvg__vset(dst, px + dlx*w, py + dly*w, 0,1); dst++;
  1296. nvg__vset(dst, px - dlx*w, py - dly*w, 1,1); dst++;
  1297. for (i = 0; i < ncap; i++) {
  1298. float a = i/(float)(ncap-1)*NVG_PI;
  1299. float ax = cosf(a) * w, ay = sinf(a) * w;
  1300. nvg__vset(dst, px, py, 0.5f,1); dst++;
  1301. nvg__vset(dst, px - dlx*ax + dx*ay, py - dly*ax + dy*ay, 0,1); dst++;
  1302. }
  1303. return dst;
  1304. }
  1305. static void nvg__calculateJoins(NVGcontext* ctx, float w, int lineJoin, float miterLimit)
  1306. {
  1307. NVGpathCache* cache = ctx->cache;
  1308. int i, j;
  1309. float iw = 0.0f;
  1310. if (w > 0.0f) iw = 1.0f / w;
  1311. // Calculate which joins needs extra vertices to append, and gather vertex count.
  1312. for (i = 0; i < cache->npaths; i++) {
  1313. NVGpath* path = &cache->paths[i];
  1314. NVGpoint* pts = &cache->points[path->first];
  1315. NVGpoint* p0 = &pts[path->count-1];
  1316. NVGpoint* p1 = &pts[0];
  1317. int nleft = 0;
  1318. path->nbevel = 0;
  1319. for (j = 0; j < path->count; j++) {
  1320. float dlx0, dly0, dlx1, dly1, dmr2, cross, limit;
  1321. dlx0 = p0->dy;
  1322. dly0 = -p0->dx;
  1323. dlx1 = p1->dy;
  1324. dly1 = -p1->dx;
  1325. // Calculate extrusions
  1326. p1->dmx = (dlx0 + dlx1) * 0.5f;
  1327. p1->dmy = (dly0 + dly1) * 0.5f;
  1328. dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy;
  1329. if (dmr2 > 0.000001f) {
  1330. float scale = 1.0f / dmr2;
  1331. if (scale > 600.0f) {
  1332. scale = 600.0f;
  1333. }
  1334. p1->dmx *= scale;
  1335. p1->dmy *= scale;
  1336. }
  1337. // Clear flags, but keep the corner.
  1338. p1->flags = (p1->flags & NVG_PT_CORNER) ? NVG_PT_CORNER : 0;
  1339. // Keep track of left turns.
  1340. cross = p1->dx * p0->dy - p0->dx * p1->dy;
  1341. if (cross > 0.0f) {
  1342. nleft++;
  1343. p1->flags |= NVG_PT_LEFT;
  1344. }
  1345. // Calculate if we should use bevel or miter for inner join.
  1346. limit = nvg__maxf(1.01f, nvg__minf(p0->len, p1->len) * iw);
  1347. if ((dmr2 * limit*limit) < 1.0f)
  1348. p1->flags |= NVG_PR_INNERBEVEL;
  1349. // Check to see if the corner needs to be beveled.
  1350. if (p1->flags & NVG_PT_CORNER) {
  1351. if ((dmr2 * miterLimit*miterLimit) < 1.0f || lineJoin == NVG_BEVEL || lineJoin == NVG_ROUND) {
  1352. p1->flags |= NVG_PT_BEVEL;
  1353. }
  1354. }
  1355. if ((p1->flags & (NVG_PT_BEVEL | NVG_PR_INNERBEVEL)) != 0)
  1356. path->nbevel++;
  1357. p0 = p1++;
  1358. }
  1359. path->convex = (nleft == path->count) ? 1 : 0;
  1360. }
  1361. }
  1362. static int nvg__expandStroke(NVGcontext* ctx, float w, int lineCap, int lineJoin, float miterLimit)
  1363. {
  1364. NVGpathCache* cache = ctx->cache;
  1365. NVGvertex* verts;
  1366. NVGvertex* dst;
  1367. int cverts, i, j;
  1368. float aa = ctx->fringeWidth;
  1369. int ncap = nvg__curveDivs(w, NVG_PI, ctx->tessTol); // Calculate divisions per half circle.
  1370. nvg__calculateJoins(ctx, w, lineJoin, miterLimit);
  1371. // Calculate max vertex usage.
  1372. cverts = 0;
  1373. for (i = 0; i < cache->npaths; i++) {
  1374. NVGpath* path = &cache->paths[i];
  1375. int loop = (path->closed == 0) ? 0 : 1;
  1376. if (lineCap == NVG_ROUND)
  1377. cverts += (path->count + path->nbevel*(ncap+2) + 1) * 2; // plus one for loop
  1378. else
  1379. cverts += (path->count + path->nbevel*5 + 1) * 2; // plus one for loop
  1380. if (loop == 0) {
  1381. // space for caps
  1382. if (lineCap == NVG_ROUND) {
  1383. cverts += (ncap*2 + 2)*2;
  1384. } else {
  1385. cverts += (3+3)*2;
  1386. }
  1387. }
  1388. }
  1389. verts = nvg__allocTempVerts(ctx, cverts);
  1390. if (verts == NULL) return 0;
  1391. for (i = 0; i < cache->npaths; i++) {
  1392. NVGpath* path = &cache->paths[i];
  1393. NVGpoint* pts = &cache->points[path->first];
  1394. NVGpoint* p0;
  1395. NVGpoint* p1;
  1396. int s, e, loop;
  1397. float dx, dy;
  1398. path->fill = 0;
  1399. path->nfill = 0;
  1400. // Calculate fringe or stroke
  1401. loop = (path->closed == 0) ? 0 : 1;
  1402. dst = verts;
  1403. path->stroke = dst;
  1404. if (loop) {
  1405. // Looping
  1406. p0 = &pts[path->count-1];
  1407. p1 = &pts[0];
  1408. s = 0;
  1409. e = path->count;
  1410. } else {
  1411. // Add cap
  1412. p0 = &pts[0];
  1413. p1 = &pts[1];
  1414. s = 1;
  1415. e = path->count-1;
  1416. }
  1417. if (loop == 0) {
  1418. // Add cap
  1419. dx = p1->x - p0->x;
  1420. dy = p1->y - p0->y;
  1421. nvg__normalize(&dx, &dy);
  1422. if (lineCap == NVG_BUTT)
  1423. dst = nvg__buttCapStart(dst, p0, dx, dy, w, -aa*0.5f, aa);
  1424. else if (lineCap == NVG_BUTT || lineCap == NVG_SQUARE)
  1425. dst = nvg__buttCapStart(dst, p0, dx, dy, w, w-aa, aa);
  1426. else if (lineCap == NVG_ROUND)
  1427. dst = nvg__roundCapStart(dst, p0, dx, dy, w, ncap, aa);
  1428. }
  1429. for (j = s; j < e; ++j) {
  1430. if ((p1->flags & (NVG_PT_BEVEL | NVG_PR_INNERBEVEL)) != 0) {
  1431. if (lineJoin == NVG_ROUND) {
  1432. dst = nvg__roundJoin(dst, p0, p1, w, w, 0, 1, ncap, aa);
  1433. } else {
  1434. dst = nvg__bevelJoin(dst, p0, p1, w, w, 0, 1, aa);
  1435. }
  1436. } else {
  1437. nvg__vset(dst, p1->x + (p1->dmx * w), p1->y + (p1->dmy * w), 0,1); dst++;
  1438. nvg__vset(dst, p1->x - (p1->dmx * w), p1->y - (p1->dmy * w), 1,1); dst++;
  1439. }
  1440. p0 = p1++;
  1441. }
  1442. if (loop) {
  1443. // Loop it
  1444. nvg__vset(dst, verts[0].x, verts[0].y, 0,1); dst++;
  1445. nvg__vset(dst, verts[1].x, verts[1].y, 1,1); dst++;
  1446. } else {
  1447. // Add cap
  1448. dx = p1->x - p0->x;
  1449. dy = p1->y - p0->y;
  1450. nvg__normalize(&dx, &dy);
  1451. if (lineCap == NVG_BUTT)
  1452. dst = nvg__buttCapEnd(dst, p1, dx, dy, w, -aa*0.5f, aa);
  1453. else if (lineCap == NVG_BUTT || lineCap == NVG_SQUARE)
  1454. dst = nvg__buttCapEnd(dst, p1, dx, dy, w, w-aa, aa);
  1455. else if (lineCap == NVG_ROUND)
  1456. dst = nvg__roundCapEnd(dst, p1, dx, dy, w, ncap, aa);
  1457. }
  1458. path->nstroke = (int)(dst - verts);
  1459. verts = dst;
  1460. }
  1461. return 1;
  1462. }
  1463. static int nvg__expandFill(NVGcontext* ctx, float w, int lineJoin, float miterLimit)
  1464. {
  1465. NVGpathCache* cache = ctx->cache;
  1466. NVGvertex* verts;
  1467. NVGvertex* dst;
  1468. int cverts, convex, i, j;
  1469. float aa = ctx->fringeWidth;
  1470. int fringe = w > 0.0f;
  1471. nvg__calculateJoins(ctx, w, lineJoin, miterLimit);
  1472. // Calculate max vertex usage.
  1473. cverts = 0;
  1474. for (i = 0; i < cache->npaths; i++) {
  1475. NVGpath* path = &cache->paths[i];
  1476. cverts += path->count + path->nbevel + 1;
  1477. if (fringe)
  1478. cverts += (path->count + path->nbevel*5 + 1) * 2; // plus one for loop
  1479. }
  1480. verts = nvg__allocTempVerts(ctx, cverts);
  1481. if (verts == NULL) return 0;
  1482. convex = cache->npaths == 1 && cache->paths[0].convex;
  1483. for (i = 0; i < cache->npaths; i++) {
  1484. NVGpath* path = &cache->paths[i];
  1485. NVGpoint* pts = &cache->points[path->first];
  1486. NVGpoint* p0;
  1487. NVGpoint* p1;
  1488. float rw, lw, woff;
  1489. float ru, lu;
  1490. // Calculate shape vertices.
  1491. woff = 0.5f*aa;
  1492. dst = verts;
  1493. path->fill = dst;
  1494. if (fringe) {
  1495. // Looping
  1496. p0 = &pts[path->count-1];
  1497. p1 = &pts[0];
  1498. for (j = 0; j < path->count; ++j) {
  1499. if (p1->flags & NVG_PT_BEVEL) {
  1500. float dlx0 = p0->dy;
  1501. float dly0 = -p0->dx;
  1502. float dlx1 = p1->dy;
  1503. float dly1 = -p1->dx;
  1504. if (p1->flags & NVG_PT_LEFT) {
  1505. float lx = p1->x + p1->dmx * woff;
  1506. float ly = p1->y + p1->dmy * woff;
  1507. nvg__vset(dst, lx, ly, 0.5f,1); dst++;
  1508. } else {
  1509. float lx0 = p1->x + dlx0 * woff;
  1510. float ly0 = p1->y + dly0 * woff;
  1511. float lx1 = p1->x + dlx1 * woff;
  1512. float ly1 = p1->y + dly1 * woff;
  1513. nvg__vset(dst, lx0, ly0, 0.5f,1); dst++;
  1514. nvg__vset(dst, lx1, ly1, 0.5f,1); dst++;
  1515. }
  1516. } else {
  1517. nvg__vset(dst, p1->x + (p1->dmx * woff), p1->y + (p1->dmy * woff), 0.5f,1); dst++;
  1518. }
  1519. p0 = p1++;
  1520. }
  1521. } else {
  1522. for (j = 0; j < path->count; ++j) {
  1523. nvg__vset(dst, pts[j].x, pts[j].y, 0.5f,1);
  1524. dst++;
  1525. }
  1526. }
  1527. path->nfill = (int)(dst - verts);
  1528. verts = dst;
  1529. // Calculate fringe
  1530. if (fringe) {
  1531. lw = w + woff;
  1532. rw = w - woff;
  1533. lu = 0;
  1534. ru = 1;
  1535. dst = verts;
  1536. path->stroke = dst;
  1537. // Create only half a fringe for convex shapes so that
  1538. // the shape can be rendered without stenciling.
  1539. if (convex) {
  1540. lw = woff; // This should generate the same vertex as fill inset above.
  1541. lu = 0.5f; // Set outline fade at middle.
  1542. }
  1543. // Looping
  1544. p0 = &pts[path->count-1];
  1545. p1 = &pts[0];
  1546. for (j = 0; j < path->count; ++j) {
  1547. if ((p1->flags & (NVG_PT_BEVEL | NVG_PR_INNERBEVEL)) != 0) {
  1548. dst = nvg__bevelJoin(dst, p0, p1, lw, rw, lu, ru, ctx->fringeWidth);
  1549. } else {
  1550. nvg__vset(dst, p1->x + (p1->dmx * lw), p1->y + (p1->dmy * lw), lu,1); dst++;
  1551. nvg__vset(dst, p1->x - (p1->dmx * rw), p1->y - (p1->dmy * rw), ru,1); dst++;
  1552. }
  1553. p0 = p1++;
  1554. }
  1555. // Loop it
  1556. nvg__vset(dst, verts[0].x, verts[0].y, lu,1); dst++;
  1557. nvg__vset(dst, verts[1].x, verts[1].y, ru,1); dst++;
  1558. path->nstroke = (int)(dst - verts);
  1559. verts = dst;
  1560. } else {
  1561. path->stroke = NULL;
  1562. path->nstroke = 0;
  1563. }
  1564. }
  1565. return 1;
  1566. }
  1567. // Draw
  1568. void nvgBeginPath(NVGcontext* ctx)
  1569. {
  1570. ctx->ncommands = 0;
  1571. nvg__clearPathCache(ctx);
  1572. }
  1573. void nvgMoveTo(NVGcontext* ctx, float x, float y)
  1574. {
  1575. float vals[] = { NVG_MOVETO, x, y };
  1576. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1577. }
  1578. void nvgLineTo(NVGcontext* ctx, float x, float y)
  1579. {
  1580. float vals[] = { NVG_LINETO, x, y };
  1581. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1582. }
  1583. void nvgBezierTo(NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y)
  1584. {
  1585. float vals[] = { NVG_BEZIERTO, c1x, c1y, c2x, c2y, x, y };
  1586. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1587. }
  1588. void nvgQuadTo(NVGcontext* ctx, float cx, float cy, float x, float y)
  1589. {
  1590. float x0 = ctx->commandx;
  1591. float y0 = ctx->commandy;
  1592. float vals[] = { NVG_BEZIERTO,
  1593. x0 + 2.0f/3.0f*(cx - x0), y0 + 2.0f/3.0f*(cy - y0),
  1594. x + 2.0f/3.0f*(cx - x), y + 2.0f/3.0f*(cy - y),
  1595. x, y };
  1596. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1597. }
  1598. void nvgArcTo(NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius)
  1599. {
  1600. float x0 = ctx->commandx;
  1601. float y0 = ctx->commandy;
  1602. float dx0,dy0, dx1,dy1, a, d, cx,cy, a0,a1;
  1603. int dir;
  1604. if (ctx->ncommands == 0) {
  1605. return;
  1606. }
  1607. // Handle degenerate cases.
  1608. if (nvg__ptEquals(x0,y0, x1,y1, ctx->distTol) ||
  1609. nvg__ptEquals(x1,y1, x2,y2, ctx->distTol) ||
  1610. nvg__distPtSeg(x1,y1, x0,y0, x2,y2) < ctx->distTol*ctx->distTol ||
  1611. radius < ctx->distTol) {
  1612. nvgLineTo(ctx, x1,y1);
  1613. return;
  1614. }
  1615. // Calculate tangential circle to lines (x0,y0)-(x1,y1) and (x1,y1)-(x2,y2).
  1616. dx0 = x0-x1;
  1617. dy0 = y0-y1;
  1618. dx1 = x2-x1;
  1619. dy1 = y2-y1;
  1620. nvg__normalize(&dx0,&dy0);
  1621. nvg__normalize(&dx1,&dy1);
  1622. a = nvg__acosf(dx0*dx1 + dy0*dy1);
  1623. d = radius / nvg__tanf(a/2.0f);
  1624. // printf("a=%f° d=%f\n", a/NVG_PI*180.0f, d);
  1625. if (d > 10000.0f) {
  1626. nvgLineTo(ctx, x1,y1);
  1627. return;
  1628. }
  1629. if (nvg__cross(dx0,dy0, dx1,dy1) > 0.0f) {
  1630. cx = x1 + dx0*d + dy0*radius;
  1631. cy = y1 + dy0*d + -dx0*radius;
  1632. a0 = nvg__atan2f(dx0, -dy0);
  1633. a1 = nvg__atan2f(-dx1, dy1);
  1634. dir = NVG_CW;
  1635. // printf("CW c=(%f, %f) a0=%f° a1=%f°\n", cx, cy, a0/NVG_PI*180.0f, a1/NVG_PI*180.0f);
  1636. } else {
  1637. cx = x1 + dx0*d + -dy0*radius;
  1638. cy = y1 + dy0*d + dx0*radius;
  1639. a0 = nvg__atan2f(-dx0, dy0);
  1640. a1 = nvg__atan2f(dx1, -dy1);
  1641. dir = NVG_CCW;
  1642. // printf("CCW c=(%f, %f) a0=%f° a1=%f°\n", cx, cy, a0/NVG_PI*180.0f, a1/NVG_PI*180.0f);
  1643. }
  1644. nvgArc(ctx, cx, cy, radius, a0, a1, dir);
  1645. }
  1646. void nvgClosePath(NVGcontext* ctx)
  1647. {
  1648. float vals[] = { NVG_CLOSE };
  1649. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1650. }
  1651. void nvgPathWinding(NVGcontext* ctx, int dir)
  1652. {
  1653. float vals[] = { NVG_WINDING, (float)dir };
  1654. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1655. }
  1656. void nvgArc(NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir)
  1657. {
  1658. float a = 0, da = 0, hda = 0, kappa = 0;
  1659. float dx = 0, dy = 0, x = 0, y = 0, tanx = 0, tany = 0;
  1660. float px = 0, py = 0, ptanx = 0, ptany = 0;
  1661. float vals[3 + 5*7 + 100];
  1662. int i, ndivs, nvals;
  1663. int move = ctx->ncommands > 0 ? NVG_LINETO : NVG_MOVETO;
  1664. // Clamp angles
  1665. da = a1 - a0;
  1666. if (dir == NVG_CW) {
  1667. if (nvg__absf(da) >= NVG_PI*2) {
  1668. da = NVG_PI*2;
  1669. } else {
  1670. while (da < 0.0f) da += NVG_PI*2;
  1671. }
  1672. } else {
  1673. if (nvg__absf(da) >= NVG_PI*2) {
  1674. da = -NVG_PI*2;
  1675. } else {
  1676. while (da > 0.0f) da -= NVG_PI*2;
  1677. }
  1678. }
  1679. // Split arc into max 90 degree segments.
  1680. ndivs = nvg__maxi(1, nvg__mini((int)(nvg__absf(da) / (NVG_PI*0.5f) + 0.5f), 5));
  1681. hda = (da / (float)ndivs) / 2.0f;
  1682. kappa = nvg__absf(4.0f / 3.0f * (1.0f - nvg__cosf(hda)) / nvg__sinf(hda));
  1683. if (dir == NVG_CCW)
  1684. kappa = -kappa;
  1685. nvals = 0;
  1686. for (i = 0; i <= ndivs; i++) {
  1687. a = a0 + da * (i/(float)ndivs);
  1688. dx = nvg__cosf(a);
  1689. dy = nvg__sinf(a);
  1690. x = cx + dx*r;
  1691. y = cy + dy*r;
  1692. tanx = -dy*r*kappa;
  1693. tany = dx*r*kappa;
  1694. if (i == 0) {
  1695. vals[nvals++] = (float)move;
  1696. vals[nvals++] = x;
  1697. vals[nvals++] = y;
  1698. } else {
  1699. vals[nvals++] = NVG_BEZIERTO;
  1700. vals[nvals++] = px+ptanx;
  1701. vals[nvals++] = py+ptany;
  1702. vals[nvals++] = x-tanx;
  1703. vals[nvals++] = y-tany;
  1704. vals[nvals++] = x;
  1705. vals[nvals++] = y;
  1706. }
  1707. px = x;
  1708. py = y;
  1709. ptanx = tanx;
  1710. ptany = tany;
  1711. }
  1712. nvg__appendCommands(ctx, vals, nvals);
  1713. }
  1714. void nvgRect(NVGcontext* ctx, float x, floa

Large files files are truncated, but you can click here to view the full file