You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1650 lines
41KB

  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 "nanovg.h"
  21. #define FONTSTASH_IMPLEMENTATION
  22. #include "fontstash.h"
  23. #define STBI_HEADER_FILE_ONLY
  24. #include "stb_image.c"
  25. #define NVG_INIT_PATH_SIZE 256
  26. #define NVG_MAX_STATES 32
  27. #define NVG_AA 1.0f
  28. #define NVG_KAPPA90 0.5522847493f // Lenght proportional to radius of a cubic bezier handle for 90deg arcs.
  29. #define NVG_COUNTOF(arr) (sizeof(arr) / sizeof(0[arr]))
  30. enum NVGcommands {
  31. NVG_MOVETO = 0,
  32. NVG_LINETO = 1,
  33. NVG_BEZIERTO = 2,
  34. NVG_CLOSE = 3,
  35. NVG_WINDING = 4,
  36. };
  37. enum NVGpointFlags
  38. {
  39. NVG_BEVEL = 0x01,
  40. NVG_LEFT = 0x02,
  41. NVG_CUSP = 0x04,
  42. };
  43. enum NVGexpandFeatures {
  44. NVG_FILL = 0x01,
  45. NVG_STROKE = 0x02,
  46. NVG_CAPS = 0x04,
  47. };
  48. struct NVGstate {
  49. struct NVGpaint fill;
  50. struct NVGpaint stroke;
  51. float strokeWidth;
  52. float miterLimit;
  53. float xform[6];
  54. struct NVGscissor scissor;
  55. float fontSize;
  56. float letterSpacing;
  57. float fontBlur;
  58. int textAlign;
  59. int fontId;
  60. };
  61. struct NVGpoint {
  62. float x,y;
  63. float dx, dy;
  64. float len;
  65. float dmx, dmy;
  66. unsigned char flags;
  67. };
  68. struct NVGpathCache {
  69. struct NVGpoint* points;
  70. int npoints;
  71. int cpoints;
  72. struct NVGpath* paths;
  73. int npaths;
  74. int cpaths;
  75. struct NVGvertex* verts;
  76. int nverts;
  77. int cverts;
  78. float bounds[4];
  79. };
  80. struct NVGcontext {
  81. struct NVGparams params;
  82. float* commands;
  83. int ccommands;
  84. int ncommands;
  85. float commandx, commandy;
  86. struct NVGstate states[NVG_MAX_STATES];
  87. int nstates;
  88. struct NVGpathCache* cache;
  89. float tessTol;
  90. float distTol;
  91. struct FONScontext* fs;
  92. int fontImage;
  93. int drawCallCount;
  94. int fillTriCount;
  95. int strokeTriCount;
  96. int textTriCount;
  97. };
  98. static float nvg__sqrtf(float a) { return sqrtf(a); }
  99. static float nvg__modf(float a, float b) { return fmodf(a, b); }
  100. static float nvg__sinf(float a) { return sinf(a); }
  101. static float nvg__cosf(float a) { return cosf(a); }
  102. static float nvg__tanf(float a) { return tanf(a); }
  103. static float nvg__atan2f(float a,float b) { return atan2f(a, b); }
  104. static float nvg__acosf(float a) { return acosf(a); }
  105. static int nvg__mini(int a, int b) { return a < b ? a : b; }
  106. static int nvg__maxi(int a, int b) { return a > b ? a : b; }
  107. static float nvg__minf(float a, float b) { return a < b ? a : b; }
  108. static float nvg__maxf(float a, float b) { return a > b ? a : b; }
  109. static float nvg__absf(float a) { return a >= 0.0f ? a : -a; }
  110. static float nvg__clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
  111. static float nvg__cross(float dx0, float dy0, float dx1, float dy1) { return dx1*dy0 - dx0*dy1; }
  112. static float nvg__normalize(float *x, float* y)
  113. {
  114. float d = nvg__sqrtf((*x)*(*x) + (*y)*(*y));
  115. if (d > 1e-6f) {
  116. d = 1.0f / d;
  117. *x *= d;
  118. *y *= d;
  119. }
  120. return d;
  121. }
  122. static void nvg__deletePathCache(struct NVGpathCache* c)
  123. {
  124. if (c == NULL) return;
  125. if (c->points != NULL) free(c->points);
  126. if (c->paths != NULL) free(c->paths);
  127. if (c->verts != NULL) free(c->verts);
  128. free(c);
  129. }
  130. static struct NVGpathCache* nvg__allocPathCache()
  131. {
  132. struct NVGpathCache* c = (struct NVGpathCache*)malloc(sizeof(struct NVGpathCache));
  133. if (c == NULL) goto error;
  134. memset(c, 0, sizeof(struct NVGpathCache));
  135. c->points = (struct NVGpoint*)malloc(sizeof(struct NVGpoint)*4);
  136. if (!c->points) goto error;
  137. c->npoints = 0;
  138. c->cpoints = 4;
  139. c->paths = (struct NVGpath*)malloc(sizeof(struct NVGpath)*4);
  140. if (!c->paths) goto error;
  141. c->npaths = 0;
  142. c->cpaths = 4;
  143. c->verts = (struct NVGvertex*)malloc(sizeof(struct NVGvertex)*4);
  144. if (!c->verts) goto error;
  145. c->nverts = 0;
  146. c->cverts = 4;
  147. return c;
  148. error:
  149. nvg__deletePathCache(c);
  150. return NULL;
  151. }
  152. struct NVGcontext* nvgCreateInternal(struct NVGparams* params)
  153. {
  154. struct FONSparams fontParams;
  155. struct NVGcontext* ctx = (struct NVGcontext*)malloc(sizeof(struct NVGcontext));
  156. if (ctx == NULL) goto error;
  157. memset(ctx, 0, sizeof(struct NVGcontext));
  158. ctx->params = *params;
  159. ctx->commands = (float*)malloc(sizeof(float)*NVG_INIT_PATH_SIZE);
  160. if (!ctx->commands) goto error;
  161. ctx->ncommands = 0;
  162. ctx->ccommands = NVG_INIT_PATH_SIZE;
  163. ctx->cache = nvg__allocPathCache();
  164. if (ctx->cache == NULL) goto error;
  165. nvgSave(ctx);
  166. nvgReset(ctx);
  167. ctx->tessTol = 0.3f * 4.0f;
  168. ctx->distTol = 0.01f;
  169. if (ctx->params.renderCreate(ctx->params.userPtr) == 0) goto error;
  170. // Init font rendering
  171. memset(&fontParams, 0, sizeof(fontParams));
  172. fontParams.width = params->atlasWidth;
  173. fontParams.height = params->atlasHeight;
  174. fontParams.flags = FONS_ZERO_TOPLEFT;
  175. fontParams.renderCreate = NULL;
  176. fontParams.renderUpdate = NULL;
  177. fontParams.renderDraw = NULL;
  178. fontParams.renderDelete = NULL;
  179. fontParams.userPtr = NULL;
  180. ctx->fs = fonsCreateInternal(&fontParams);
  181. if (ctx->fs == NULL) goto error;
  182. // Create font texture
  183. ctx->fontImage = ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_ALPHA, fontParams.width, fontParams.height, NULL);
  184. if (ctx->fontImage == 0) goto error;
  185. return ctx;
  186. error:
  187. nvgDeleteInternal(ctx);
  188. return 0;
  189. }
  190. void nvgDeleteInternal(struct NVGcontext* ctx)
  191. {
  192. if (ctx == NULL) return;
  193. if (ctx->commands != NULL) free(ctx->commands);
  194. if (ctx->cache != NULL) nvg__deletePathCache(ctx->cache);
  195. if (ctx->fs)
  196. fonsDeleteInternal(ctx->fs);
  197. if (ctx->params.renderDelete != NULL)
  198. ctx->params.renderDelete(ctx->params.userPtr);
  199. free(ctx);
  200. }
  201. void nvgBeginFrame(struct NVGcontext* ctx)
  202. {
  203. /* printf("Tris: draws:%d fill:%d stroke:%d text:%d TOT:%d\n",
  204. ctx->drawCallCount, ctx->fillTriCount, ctx->strokeTriCount, ctx->textTriCount,
  205. ctx->fillTriCount+ctx->strokeTriCount+ctx->textTriCount);*/
  206. ctx->drawCallCount = 0;
  207. ctx->fillTriCount = 0;
  208. ctx->strokeTriCount = 0;
  209. ctx->textTriCount = 0;
  210. }
  211. unsigned int nvgRGB(unsigned char r, unsigned char g, unsigned char b)
  212. {
  213. return nvgRGBA(r,g,b,255);
  214. }
  215. unsigned int nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
  216. {
  217. return (r) | (g << 8) | (b << 16) | (a << 24);
  218. }
  219. unsigned int nvgTransRGBA(unsigned int c, unsigned char a)
  220. {
  221. int r = (c) & 0xff;
  222. int g = (c>>8) & 0xff;
  223. int b = (c>>16) & 0xff;
  224. return nvgRGBA(r,g,b,a);
  225. }
  226. unsigned int nvgLerpRGBA(unsigned int c0, unsigned int c1, float u)
  227. {
  228. int iu = (float)(nvg__clampf(u, 0.0f, 1.0f) * 256.0f);
  229. int r = (((c0) & 0xff)*(256-iu) + (((c1) & 0xff)*iu)) >> 8;
  230. int g = (((c0>>8) & 0xff)*(256-iu) + (((c1>>8) & 0xff)*iu)) >> 8;
  231. int b = (((c0>>16) & 0xff)*(256-iu) + (((c1>>16) & 0xff)*iu)) >> 8;
  232. int a = (((c0>>24) & 0xff)*(256-iu) + (((c1>>24) & 0xff)*iu)) >> 8;
  233. return nvgRGBA(r,g,b,a);
  234. }
  235. unsigned int nvgHSL(float h, float s, float l)
  236. {
  237. return nvgHSLA(h,s,l,255);
  238. }
  239. static float nvg__hue(float h, float m1, float m2)
  240. {
  241. if (h < 0) h += 1;
  242. if (h > 1) h -= 1;
  243. if (h < 1.0f/6.0f)
  244. return m1 + (m2 - m1) * h * 6.0f;
  245. else if (h < 3.0f/6.0f)
  246. return m2;
  247. else if (h < 4.0f/6.0f)
  248. return m1 + (m2 - m1) * (2.0f/3.0f - h) * 6.0f;
  249. return m1;
  250. }
  251. unsigned int nvgHSLA(float h, float s, float l, unsigned char a)
  252. {
  253. h = nvg__modf(h, 1.0f);
  254. if (h < 0.0f) h += 1.0f;
  255. s = nvg__clampf(s, 0.0f, 1.0f);
  256. l = nvg__clampf(l, 0.0f, 1.0f);
  257. float m2 = l <= 0.5f ? (l * (1 + s)) : (l + s - l * s);
  258. float m1 = 2 * l - m2;
  259. unsigned char r = (unsigned char)nvg__clampf(nvg__hue(h + 1.0f/3.0f, m1, m2) * 255.0f, 0, 255);
  260. unsigned char g = (unsigned char)nvg__clampf(nvg__hue(h, m1, m2) * 255.0f, 0, 255);
  261. unsigned char b = (unsigned char)nvg__clampf(nvg__hue(h - 1.0f/3.0f, m1, m2) * 255.0f, 0, 255);
  262. return nvgRGBA(r,g,b,a);
  263. }
  264. static struct NVGstate* nvg__getState(struct NVGcontext* ctx)
  265. {
  266. return &ctx->states[ctx->nstates-1];
  267. }
  268. static void nvg__xformIdentity(float* t)
  269. {
  270. t[0] = 1.0f; t[1] = 0.0f;
  271. t[2] = 0.0f; t[3] = 1.0f;
  272. t[4] = 0.0f; t[5] = 0.0f;
  273. }
  274. static void nvg__xformTranslate(float* t, float tx, float ty)
  275. {
  276. t[0] = 1.0f; t[1] = 0.0f;
  277. t[2] = 0.0f; t[3] = 1.0f;
  278. t[4] = tx; t[5] = ty;
  279. }
  280. static void nvg__xformScale(float* t, float sx, float sy)
  281. {
  282. t[0] = sx; t[1] = 0.0f;
  283. t[2] = 0.0f; t[3] = sy;
  284. t[4] = 0.0f; t[5] = 0.0f;
  285. }
  286. static void nvg__xformRotate(float* t, float a)
  287. {
  288. float cs = nvg__cosf(a), sn = nvg__sinf(a);
  289. t[0] = cs; t[1] = sn;
  290. t[2] = -sn; t[3] = cs;
  291. t[4] = 0.0f; t[5] = 0.0f;
  292. }
  293. static void nvg__xformMultiply(float* t, float* s)
  294. {
  295. float t0 = t[0] * s[0] + t[1] * s[2];
  296. float t2 = t[2] * s[0] + t[3] * s[2];
  297. float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
  298. t[1] = t[0] * s[1] + t[1] * s[3];
  299. t[3] = t[2] * s[1] + t[3] * s[3];
  300. t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
  301. t[0] = t0;
  302. t[2] = t2;
  303. t[4] = t4;
  304. }
  305. static void nvg__xformPremultiply(float* t, float* s)
  306. {
  307. float s2[6];
  308. memcpy(s2, s, sizeof(float)*6);
  309. nvg__xformMultiply(s2, t);
  310. memcpy(t, s2, sizeof(float)*6);
  311. }
  312. static void nvg__setPaintColor(struct NVGpaint* p, unsigned int color)
  313. {
  314. memset(p, 0, sizeof(*p));
  315. nvg__xformIdentity(p->xform);
  316. p->radius = 0.0f;
  317. p->feather = 1.0f;
  318. p->innerColor = color;
  319. p->outerColor = color;
  320. }
  321. // State handling
  322. void nvgSave(struct NVGcontext* ctx)
  323. {
  324. if (ctx->nstates >= NVG_MAX_STATES)
  325. return;
  326. if (ctx->nstates > 0)
  327. memcpy(&ctx->states[ctx->nstates], &ctx->states[ctx->nstates-1], sizeof(struct NVGstate));
  328. ctx->nstates++;
  329. }
  330. void nvgRestore(struct NVGcontext* ctx)
  331. {
  332. if (ctx->nstates <= 1)
  333. return;
  334. ctx->nstates--;
  335. }
  336. void nvgReset(struct NVGcontext* ctx)
  337. {
  338. struct NVGstate* state = nvg__getState(ctx);
  339. memset(state, 0, sizeof(*state));
  340. nvg__setPaintColor(&state->fill, nvgRGBA(255,255,255,255));
  341. nvg__setPaintColor(&state->stroke, nvgRGBA(0,0,0,255));
  342. state->strokeWidth = 1.0f;
  343. state->miterLimit = 1.2f;
  344. nvg__xformIdentity(state->xform);
  345. state->scissor.extent[0] = 0.0f;
  346. state->scissor.extent[1] = 0.0f;
  347. state->fontSize = 16.0f;
  348. state->letterSpacing = 0.0f;
  349. state->fontBlur = 0.0f;
  350. state->textAlign = NVG_ALIGN_LEFT | NVG_ALIGN_BASELINE;
  351. state->fontId = 0;
  352. }
  353. // State setting
  354. void nvgStrokeWidth(struct NVGcontext* ctx, float width)
  355. {
  356. struct NVGstate* state = nvg__getState(ctx);
  357. state->strokeWidth = width;
  358. }
  359. void nvgMiterLimit(struct NVGcontext* ctx, float limit)
  360. {
  361. struct NVGstate* state = nvg__getState(ctx);
  362. state->miterLimit = limit;
  363. }
  364. void nvgTransform(struct NVGcontext* ctx, float a, float b, float c, float d, float e, float f)
  365. {
  366. struct NVGstate* state = nvg__getState(ctx);
  367. float t[6] = { a, b, c, d, e, f };
  368. nvg__xformPremultiply(state->xform, t);
  369. }
  370. void nvgResetTransform(struct NVGcontext* ctx)
  371. {
  372. struct NVGstate* state = nvg__getState(ctx);
  373. nvg__xformIdentity(state->xform);
  374. }
  375. void nvgTranslate(struct NVGcontext* ctx, float x, float y)
  376. {
  377. struct NVGstate* state = nvg__getState(ctx);
  378. float t[6];
  379. nvg__xformTranslate(t, x,y);
  380. nvg__xformPremultiply(state->xform, t);
  381. }
  382. void nvgRotate(struct NVGcontext* ctx, float angle)
  383. {
  384. struct NVGstate* state = nvg__getState(ctx);
  385. float t[6];
  386. nvg__xformRotate(t, angle);
  387. nvg__xformPremultiply(state->xform, t);
  388. }
  389. void nvgScale(struct NVGcontext* ctx, float x, float y)
  390. {
  391. struct NVGstate* state = nvg__getState(ctx);
  392. float t[6];
  393. nvg__xformScale(t, x,y);
  394. nvg__xformPremultiply(state->xform, t);
  395. }
  396. void nvgStrokeColor(struct NVGcontext* ctx, unsigned int color)
  397. {
  398. struct NVGstate* state = nvg__getState(ctx);
  399. nvg__setPaintColor(&state->stroke, color);
  400. }
  401. void nvgStrokePaint(struct NVGcontext* ctx, struct NVGpaint paint)
  402. {
  403. struct NVGstate* state = nvg__getState(ctx);
  404. state->stroke = paint;
  405. nvg__xformMultiply(state->stroke.xform, state->xform);
  406. }
  407. void nvgFillColor(struct NVGcontext* ctx, unsigned int color)
  408. {
  409. struct NVGstate* state = nvg__getState(ctx);
  410. nvg__setPaintColor(&state->fill, color);
  411. }
  412. void nvgFillPaint(struct NVGcontext* ctx, struct NVGpaint paint)
  413. {
  414. struct NVGstate* state = nvg__getState(ctx);
  415. state->fill = paint;
  416. nvg__xformMultiply(state->fill.xform, state->xform);
  417. }
  418. int nvgCreateImage(struct NVGcontext* ctx, const char* filename)
  419. {
  420. int w, h, n, image;
  421. unsigned char* img = stbi_load(filename, &w, &h, &n, 4);
  422. if (img == NULL) {
  423. // printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
  424. return 0;
  425. }
  426. image = nvgCreateImageRGBA(ctx, w, h, img);
  427. stbi_image_free(img);
  428. return image;
  429. }
  430. int nvgCreateImageMem(struct NVGcontext* ctx, unsigned char* data, int ndata, int freeData)
  431. {
  432. int w, h, n, image;
  433. unsigned char* img = stbi_load_from_memory(data, ndata, &w, &h, &n, 4);
  434. if (img == NULL) {
  435. // printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
  436. return 0;
  437. }
  438. image = nvgCreateImageRGBA(ctx, w, h, img);
  439. stbi_image_free(img);
  440. return image;
  441. }
  442. int nvgCreateImageRGBA(struct NVGcontext* ctx, int w, int h, const unsigned char* data)
  443. {
  444. return ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_RGBA, w, h, data);
  445. }
  446. void nvgUpdateImage(struct NVGcontext* ctx, int image, const unsigned char* data)
  447. {
  448. int w, h;
  449. ctx->params.renderGetTextureSize(ctx->params.userPtr, image, &w, &h);
  450. ctx->params.renderUpdateTexture(ctx->params.userPtr, image, 0,0, w,h, data);
  451. }
  452. void nvgImageSize(struct NVGcontext* ctx, int image, int* w, int* h)
  453. {
  454. ctx->params.renderGetTextureSize(ctx->params.userPtr, image, w, h);
  455. }
  456. void nvgDeleteImage(struct NVGcontext* ctx, int image)
  457. {
  458. ctx->params.renderDeleteTexture(ctx->params.userPtr, image);
  459. }
  460. struct NVGpaint nvgLinearGradient(struct NVGcontext* ctx,
  461. float sx, float sy, float ex, float ey,
  462. unsigned int icol, unsigned int ocol)
  463. {
  464. struct NVGpaint p;
  465. float dx, dy, d;
  466. const float large = 1e5;
  467. memset(&p, 0, sizeof(p));
  468. // Calculate transform aligned to the line
  469. dx = ex - sx;
  470. dy = ey - sy;
  471. d = sqrtf(dx*dx + dy*dy);
  472. if (d > 0.0001f) {
  473. dx /= d;
  474. dy /= d;
  475. } else {
  476. dx = 0;
  477. dy = 1;
  478. }
  479. p.xform[0] = dy; p.xform[1] = -dx;
  480. p.xform[2] = dx; p.xform[3] = dy;
  481. p.xform[4] = sx - dx*large; p.xform[5] = sy - dy*large;
  482. p.extent[0] = large;
  483. p.extent[1] = large + d*0.5f;
  484. p.radius = 0.0f;
  485. p.feather = nvg__maxf(1.0f, d);
  486. p.innerColor = icol;
  487. p.outerColor = ocol;
  488. return p;
  489. }
  490. struct NVGpaint nvgRadialGradient(struct NVGcontext* ctx,
  491. float cx, float cy, float inr, float outr,
  492. unsigned int icol, unsigned int ocol)
  493. {
  494. struct NVGpaint p;
  495. float r = (inr+outr)*0.5f;
  496. float f = (outr-inr);
  497. memset(&p, 0, sizeof(p));
  498. nvg__xformIdentity(p.xform);
  499. p.xform[4] = cx;
  500. p.xform[5] = cy;
  501. p.extent[0] = r;
  502. p.extent[1] = r;
  503. p.radius = r;
  504. p.feather = nvg__maxf(1.0f, f);
  505. p.innerColor = icol;
  506. p.outerColor = ocol;
  507. return p;
  508. }
  509. struct NVGpaint nvgBoxGradient(struct NVGcontext* ctx,
  510. float x, float y, float w, float h, float r, float f,
  511. unsigned int icol, unsigned int ocol)
  512. {
  513. struct NVGpaint p;
  514. memset(&p, 0, sizeof(p));
  515. nvg__xformIdentity(p.xform);
  516. p.xform[4] = x+w*0.5f;
  517. p.xform[5] = y+h*0.5f;
  518. p.extent[0] = w*0.5f;
  519. p.extent[1] = h*0.5f;
  520. p.radius = r;
  521. p.feather = nvg__maxf(1.0f, f);
  522. p.innerColor = icol;
  523. p.outerColor = ocol;
  524. return p;
  525. }
  526. struct NVGpaint nvgImagePattern(struct NVGcontext* ctx,
  527. float cx, float cy, float w, float h, float angle,
  528. int image, int repeat)
  529. {
  530. struct NVGpaint p;
  531. memset(&p, 0, sizeof(p));
  532. nvg__xformRotate(p.xform, angle);
  533. p.xform[4] = cx;
  534. p.xform[5] = cy;
  535. p.extent[0] = w;
  536. p.extent[1] = h;
  537. p.image = image;
  538. p.repeat = repeat;
  539. return p;
  540. }
  541. // Scissoring
  542. void nvgScissor(struct NVGcontext* ctx, float x, float y, float w, float h)
  543. {
  544. struct NVGstate* state = nvg__getState(ctx);
  545. nvg__xformIdentity(state->scissor.xform);
  546. state->scissor.xform[4] = x+w*0.5f;
  547. state->scissor.xform[5] = y+h*0.5f;
  548. nvg__xformMultiply(state->scissor.xform, state->xform);
  549. state->scissor.extent[0] = w*0.5f;
  550. state->scissor.extent[1] = h*0.5f;
  551. }
  552. void nvgResetScissor(struct NVGcontext* ctx)
  553. {
  554. struct NVGstate* state = nvg__getState(ctx);
  555. memset(state->scissor.xform, 0, sizeof(state->scissor.xform));
  556. state->scissor.extent[0] = 0;
  557. state->scissor.extent[1] = 0;
  558. }
  559. static void nvg__xformPt(float* dx, float* dy, float sx, float sy, const float* t)
  560. {
  561. *dx = sx*t[0] + sy*t[2] + t[4];
  562. *dy = sx*t[1] + sy*t[3] + t[5];
  563. }
  564. static int nvg__ptEquals(float x1, float y1, float x2, float y2, float tol)
  565. {
  566. float dx = x2 - x1;
  567. float dy = y2 - y1;
  568. return dx*dx + dy*dy < tol*tol;
  569. }
  570. static float nvg__distPtSeg(float x, float y, float px, float py, float qx, float qy)
  571. {
  572. float pqx, pqy, dx, dy, d, t;
  573. pqx = qx-px;
  574. pqy = qy-py;
  575. dx = x-px;
  576. dy = y-py;
  577. d = pqx*pqx + pqy*pqy;
  578. t = pqx*dx + pqy*dy;
  579. if (d > 0) t /= d;
  580. if (t < 0) t = 0;
  581. else if (t > 1) t = 1;
  582. dx = px + t*pqx - x;
  583. dy = py + t*pqy - y;
  584. return dx*dx + dy*dy;
  585. }
  586. static void nvg__appendCommands(struct NVGcontext* ctx, float* vals, int nvals)
  587. {
  588. struct NVGstate* state = nvg__getState(ctx);
  589. int i;
  590. if (ctx->ncommands+nvals > ctx->ccommands) {
  591. if (ctx->ccommands == 0) ctx->ccommands = 8;
  592. while (ctx->ccommands < ctx->ncommands+nvals)
  593. ctx->ccommands *= 2;
  594. ctx->commands = (float*)realloc(ctx->commands, ctx->ccommands*sizeof(float));
  595. if (ctx->commands == NULL) return;
  596. }
  597. // transform commands
  598. i = 0;
  599. while (i < nvals) {
  600. int cmd = (int)vals[i];
  601. switch (cmd) {
  602. case NVG_MOVETO:
  603. nvg__xformPt(&vals[i+1],&vals[i+2], vals[i+1],vals[i+2], state->xform);
  604. i += 3;
  605. break;
  606. case NVG_LINETO:
  607. nvg__xformPt(&vals[i+1],&vals[i+2], vals[i+1],vals[i+2], state->xform);
  608. i += 3;
  609. break;
  610. case NVG_BEZIERTO:
  611. nvg__xformPt(&vals[i+1],&vals[i+2], vals[i+1],vals[i+2], state->xform);
  612. nvg__xformPt(&vals[i+3],&vals[i+4], vals[i+3],vals[i+4], state->xform);
  613. nvg__xformPt(&vals[i+5],&vals[i+6], vals[i+5],vals[i+6], state->xform);
  614. i += 7;
  615. break;
  616. case NVG_CLOSE:
  617. i++;
  618. break;
  619. case NVG_WINDING:
  620. i += 2;
  621. break;
  622. default:
  623. i++;
  624. }
  625. }
  626. memcpy(&ctx->commands[ctx->ncommands], vals, nvals*sizeof(float));
  627. ctx->ncommands += nvals;
  628. if ((int)vals[0] != NVG_CLOSE && (int)vals[0] != NVG_WINDING) {
  629. ctx->commandx = vals[nvals-2];
  630. ctx->commandy = vals[nvals-1];
  631. }
  632. }
  633. static void nvg__clearPathCache(struct NVGcontext* ctx)
  634. {
  635. ctx->cache->npoints = 0;
  636. ctx->cache->npaths = 0;
  637. }
  638. static struct NVGpath* nvg__lastPath(struct NVGcontext* ctx)
  639. {
  640. if (ctx->cache->npaths > 0)
  641. return &ctx->cache->paths[ctx->cache->npaths-1];
  642. return NULL;
  643. }
  644. static void nvg__addPath(struct NVGcontext* ctx)
  645. {
  646. struct NVGpath* path;
  647. if (ctx->cache->npaths+1 > ctx->cache->cpaths) {
  648. ctx->cache->cpaths = (ctx->cache->cpaths == 0) ? 8 : (ctx->cache->cpaths*2);
  649. ctx->cache->paths = (struct NVGpath*)realloc(ctx->cache->paths, sizeof(struct NVGpath)*ctx->cache->cpaths);
  650. if (ctx->cache->paths == NULL) return;
  651. }
  652. path = &ctx->cache->paths[ctx->cache->npaths];
  653. memset(path, 0, sizeof(*path));
  654. path->first = ctx->cache->npoints;
  655. path->winding = NVG_CCW;
  656. ctx->cache->npaths++;
  657. }
  658. static struct NVGpoint* nvg__lastPoint(struct NVGcontext* ctx)
  659. {
  660. if (ctx->cache->npoints > 0)
  661. return &ctx->cache->points[ctx->cache->npoints-1];
  662. return NULL;
  663. }
  664. static void nvg__addPoint(struct NVGcontext* ctx, float x, float y)
  665. {
  666. struct NVGpath* path = nvg__lastPath(ctx);
  667. struct NVGpoint* pt;
  668. if (path == NULL) return;
  669. if (ctx->cache->npoints > 0) {
  670. pt = nvg__lastPoint(ctx);
  671. if (nvg__ptEquals(pt->x,pt->y, x,y, ctx->distTol))
  672. return;
  673. }
  674. if (ctx->cache->npoints+1 > ctx->cache->cpoints) {
  675. ctx->cache->cpoints = (ctx->cache->cpoints == 0) ? 8 : (ctx->cache->cpoints*2);
  676. ctx->cache->points = (struct NVGpoint*)realloc(ctx->cache->points, sizeof(struct NVGpoint)*ctx->cache->cpoints);
  677. if (ctx->cache->points == NULL) return;
  678. }
  679. pt = &ctx->cache->points[ctx->cache->npoints];
  680. memset(pt, 0, sizeof(*pt));
  681. pt->x = x;
  682. pt->y = y;
  683. ctx->cache->npoints++;
  684. path->count++;
  685. }
  686. static void nvg__closePath(struct NVGcontext* ctx)
  687. {
  688. struct NVGpath* path = nvg__lastPath(ctx);
  689. if (path == NULL) return;
  690. path->closed = 1;
  691. }
  692. static void nvg__pathWinding(struct NVGcontext* ctx, int winding)
  693. {
  694. struct NVGpath* path = nvg__lastPath(ctx);
  695. if (path == NULL) return;
  696. path->winding = winding;
  697. }
  698. static float nvg__getAverageScale(float *t)
  699. {
  700. float sx = sqrtf(t[0]*t[0] + t[2]*t[2]);
  701. float sy = sqrtf(t[1]*t[1] + t[3]*t[3]);
  702. return (sx + sy) * 0.5f;
  703. }
  704. static struct NVGvertex* nvg__allocTempVerts(struct NVGcontext* ctx, int nverts)
  705. {
  706. if (nverts > ctx->cache->cverts) {
  707. if (ctx->cache->cverts == 0) ctx->cache->cverts = 8;
  708. while (ctx->cache->cverts < nverts)
  709. ctx->cache->cverts *= 2;
  710. ctx->cache->verts = (struct NVGvertex*)realloc(ctx->cache->verts, sizeof(struct NVGvertex)*ctx->cache->cverts);
  711. if (ctx->cache->verts == NULL) return NULL;
  712. }
  713. return ctx->cache->verts;
  714. }
  715. static float nvg__triarea2(float ax, float ay, float bx, float by, float cx, float cy)
  716. {
  717. float abx = bx - ax;
  718. float aby = by - ay;
  719. float acx = cx - ax;
  720. float acy = cy - ay;
  721. return acx*aby - abx*acy;
  722. }
  723. static float nvg__polyArea(struct NVGpoint* pts, int npts)
  724. {
  725. int i;
  726. float area = 0;
  727. for (i = 2; i < npts; i++) {
  728. struct NVGpoint* a = &pts[0];
  729. struct NVGpoint* b = &pts[i-1];
  730. struct NVGpoint* c = &pts[i];
  731. area += nvg__triarea2(a->x,a->y, b->x,b->y, c->x,c->y);
  732. }
  733. return area * 0.5f;
  734. }
  735. static void nvg__polyReverse(struct NVGpoint* pts, int npts)
  736. {
  737. struct NVGpoint tmp;
  738. int i = 0, j = npts-1;
  739. while (i < j) {
  740. tmp = pts[i];
  741. pts[i] = pts[j];
  742. pts[j] = tmp;
  743. i++;
  744. j--;
  745. }
  746. }
  747. static void nvg__vset(struct NVGvertex* vtx, float x, float y, float u, float v)
  748. {
  749. vtx->x = x;
  750. vtx->y = y;
  751. vtx->u = u;
  752. vtx->v = v;
  753. }
  754. static void nvg__tesselateBezier(struct NVGcontext* ctx,
  755. float x1, float y1, float x2, float y2,
  756. float x3, float y3, float x4, float y4,
  757. int level)
  758. {
  759. float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234;
  760. if (level > 10) return;
  761. 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) {
  762. nvg__addPoint(ctx, x4, y4);
  763. return;
  764. }
  765. x12 = (x1+x2)*0.5f;
  766. y12 = (y1+y2)*0.5f;
  767. x23 = (x2+x3)*0.5f;
  768. y23 = (y2+y3)*0.5f;
  769. x34 = (x3+x4)*0.5f;
  770. y34 = (y3+y4)*0.5f;
  771. x123 = (x12+x23)*0.5f;
  772. y123 = (y12+y23)*0.5f;
  773. x234 = (x23+x34)*0.5f;
  774. y234 = (y23+y34)*0.5f;
  775. x1234 = (x123+x234)*0.5f;
  776. y1234 = (y123+y234)*0.5f;
  777. nvg__tesselateBezier(ctx, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1);
  778. nvg__tesselateBezier(ctx, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1);
  779. }
  780. static void nvg__flattenPaths(struct NVGcontext* ctx, float m)
  781. {
  782. struct NVGpathCache* cache = ctx->cache;
  783. // struct NVGstate* state = nvg__getState(ctx);
  784. struct NVGpoint* last;
  785. struct NVGpoint* p0;
  786. struct NVGpoint* p1;
  787. struct NVGpoint* pts;
  788. struct NVGpath* path;
  789. int i, j;
  790. float* cp1;
  791. float* cp2;
  792. float* p;
  793. float area;
  794. if (cache->npaths > 0)
  795. return;
  796. // Flatten
  797. i = 0;
  798. while (i < ctx->ncommands) {
  799. int cmd = (int)ctx->commands[i];
  800. switch (cmd) {
  801. case NVG_MOVETO:
  802. nvg__addPath(ctx);
  803. p = &ctx->commands[i+1];
  804. nvg__addPoint(ctx, p[0], p[1]);
  805. i += 3;
  806. break;
  807. case NVG_LINETO:
  808. p = &ctx->commands[i+1];
  809. nvg__addPoint(ctx, p[0], p[1]);
  810. i += 3;
  811. break;
  812. case NVG_BEZIERTO:
  813. last = nvg__lastPoint(ctx);
  814. if (last != NULL) {
  815. cp1 = &ctx->commands[i+1];
  816. cp2 = &ctx->commands[i+3];
  817. p = &ctx->commands[i+5];
  818. nvg__tesselateBezier(ctx, last->x,last->y, cp1[0],cp1[1], cp2[0],cp2[1], p[0],p[1], 0);
  819. }
  820. i += 7;
  821. break;
  822. case NVG_CLOSE:
  823. nvg__closePath(ctx);
  824. i++;
  825. break;
  826. case NVG_WINDING:
  827. nvg__pathWinding(ctx, (int)ctx->commands[i+1]);
  828. i += 2;
  829. break;
  830. default:
  831. i++;
  832. }
  833. }
  834. cache->bounds[0] = cache->bounds[1] = 1e6f;
  835. cache->bounds[2] = cache->bounds[3] = -1e6f;
  836. // Calculate the direction and length of line segments.
  837. for (j = 0; j < cache->npaths; j++) {
  838. path = &cache->paths[j];
  839. pts = &cache->points[path->first];
  840. // If the first and last points are the same, remove the last, mark as closed path.
  841. p0 = &pts[path->count-1];
  842. p1 = &pts[0];
  843. if (nvg__ptEquals(p0->x,p0->y, p1->x,p1->y, ctx->distTol)) {
  844. path->count--;
  845. p0 = &pts[path->count-1];
  846. path->closed = 1;
  847. }
  848. // Enforce winding.
  849. if (path->count > 2) {
  850. area = nvg__polyArea(pts, path->count);
  851. if (path->winding == NVG_CCW && area > 0.0f)
  852. nvg__polyReverse(pts, path->count);
  853. if (path->winding == NVG_CW && area < 0.0f)
  854. nvg__polyReverse(pts, path->count);
  855. }
  856. for(i = 0; i < path->count; ++i) {
  857. // Calculate segment direction and length
  858. p0->dx = p1->x - p0->x;
  859. p0->dy = p1->y - p0->y;
  860. p0->len = nvg__normalize(&p0->dx, &p0->dy);
  861. // Update bounds
  862. cache->bounds[0] = nvg__minf(cache->bounds[0], p0->x);
  863. cache->bounds[1] = nvg__minf(cache->bounds[1], p0->y);
  864. cache->bounds[2] = nvg__maxf(cache->bounds[2], p0->x);
  865. cache->bounds[3] = nvg__maxf(cache->bounds[3], p0->y);
  866. // Advance
  867. p0 = p1++;
  868. }
  869. }
  870. // Calculate which joins needs extra vertices to append, and gather vertex count.
  871. for (j = 0; j < cache->npaths; j++) {
  872. path = &cache->paths[j];
  873. pts = &cache->points[path->first];
  874. path->nbevel = 0;
  875. p0 = &pts[path->count-1];
  876. p1 = &pts[0];
  877. for(i = 0; i < path->count; ++i) {
  878. float dlx0, dly0, dlx1, dly1, dmr2, scale, cross;
  879. dlx0 = p0->dy;
  880. dly0 = -p0->dx;
  881. dlx1 = p1->dy;
  882. dly1 = -p1->dx;
  883. // Calculate extrusions
  884. p1->dmx = (dlx0 + dlx1) * 0.5f;
  885. p1->dmy = (dly0 + dly1) * 0.5f;
  886. dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy;
  887. if (dmr2 > 0.000001f) {
  888. scale = 1.0f / dmr2;
  889. if (scale > 200.0f) scale = 200.0f;
  890. p1->dmx *= scale;
  891. p1->dmy *= scale;
  892. }
  893. // Check to see if the corner needs to be beveled.
  894. if ((dmr2 * m*m) < 1.0f) {
  895. cross = p1->dx * p0->dy - p0->dx * p1->dy;
  896. p1->flags |= NVG_BEVEL;
  897. if (cross < 0)
  898. p1->flags |= NVG_LEFT;
  899. path->nbevel++;
  900. }
  901. p0 = p1++;
  902. }
  903. }
  904. }
  905. static int nvg__expandStrokeAndFill(struct NVGcontext* ctx, int feats, float w)
  906. {
  907. struct NVGpathCache* cache = ctx->cache;
  908. struct NVGpath* path;
  909. struct NVGpoint* pts;
  910. struct NVGvertex* verts;
  911. struct NVGvertex* dst;
  912. struct NVGpoint* p0;
  913. struct NVGpoint* p1;
  914. int nstroke, cverts;
  915. int i, j, s, e;
  916. float wo = 0;
  917. // Calculate max vertex usage.
  918. cverts = 0;
  919. for (i = 0; i < cache->npaths; i++) {
  920. path = &cache->paths[i];
  921. if (feats & NVG_FILL)
  922. cverts += path->count + path->nbevel + 1;
  923. if (feats & NVG_STROKE) {
  924. int loop = ((feats & NVG_CAPS) && path->closed == 0) ? 0 : 1;
  925. cverts += (path->count + path->nbevel + 1) * 2; // plus one for loop
  926. if (loop == 0)
  927. cverts += (3+3)*2; // space for caps
  928. }
  929. }
  930. verts = nvg__allocTempVerts(ctx, cverts);
  931. if (verts == NULL) return 0;
  932. for (i = 0; i < cache->npaths; i++) {
  933. path = &cache->paths[i];
  934. pts = &cache->points[path->first];
  935. nstroke = (path->count + path->nbevel + 1) * 2;
  936. // Calculate shape vertices.
  937. if (feats & NVG_FILL) {
  938. wo = 0.5f;
  939. dst = verts;
  940. path->fill = dst;
  941. // Looping
  942. p0 = &pts[path->count-1];
  943. p1 = &pts[0];
  944. for (j = 0; j < path->count; ++j) {
  945. if (p1->flags & NVG_BEVEL) {
  946. float dlx0 = p0->dy;
  947. float dly0 = -p0->dx;
  948. float dlx1 = p1->dy;
  949. float dly1 = -p1->dx;
  950. if (p1->flags & NVG_LEFT) {
  951. float lx = p1->x - p1->dmx * wo;
  952. float ly = p1->y - p1->dmy * wo;
  953. nvg__vset(dst, lx, ly, 0.5f,1); dst++;
  954. } else {
  955. float lx0 = p1->x - dlx0 * wo;
  956. float ly0 = p1->y - dly0 * wo;
  957. float lx1 = p1->x - dlx1 * wo;
  958. float ly1 = p1->y - dly1 * wo;
  959. nvg__vset(dst, lx0, ly0, 0.5f,1); dst++;
  960. nvg__vset(dst, lx1, ly1, 0.5f,1); dst++;
  961. }
  962. } else {
  963. nvg__vset(dst, p1->x - (p1->dmx * wo), p1->y - (p1->dmy * wo), 0.5f,1); dst++;
  964. }
  965. p0 = p1++;
  966. }
  967. path->nfill = (int)(dst - verts);
  968. verts += path->nfill;
  969. } else {
  970. wo = 0.0f;
  971. path->fill = 0;
  972. path->nfill = 0;
  973. }
  974. // Calculate fringe
  975. if (feats & NVG_STROKE) {
  976. float lw = w + wo; float rw = w - wo;
  977. int loop = ((feats & NVG_CAPS) && path->closed == 0) ? 0 : 1;
  978. dst = verts;
  979. path->stroke = dst;
  980. if (loop) {
  981. // Looping
  982. p0 = &pts[path->count-1];
  983. p1 = &pts[0];
  984. s = 0;
  985. e = path->count;
  986. } else {
  987. // Add cap
  988. p0 = &pts[0];
  989. p1 = &pts[1];
  990. s = 1;
  991. e = path->count-1;
  992. }
  993. if (loop == 0) {
  994. // Add cap
  995. float dx, dy, dlx, dly;
  996. dx = p1->x - p0->x;
  997. dy = p1->y - p0->y;
  998. nvg__normalize(&dx, &dy);
  999. dlx = dy;
  1000. dly = -dx;
  1001. nvg__vset(dst, p0->x + dlx*rw - dx*NVG_AA, p0->y + dly*rw - dy*NVG_AA, 0,0); dst++;
  1002. nvg__vset(dst, p0->x - dlx*lw - dx*NVG_AA, p0->y - dly*lw - dy*NVG_AA, 1,0); dst++;
  1003. nvg__vset(dst, p0->x + dlx*rw, p0->y + dly * rw, 0,1); dst++;
  1004. nvg__vset(dst, p0->x - dlx*lw, p0->y - dly * lw, 1,1); dst++;
  1005. }
  1006. for (j = s; j < e; ++j) {
  1007. if (p1->flags & NVG_BEVEL) {
  1008. float dlx0 = p0->dy;
  1009. float dly0 = -p0->dx;
  1010. float dlx1 = p1->dy;
  1011. float dly1 = -p1->dx;
  1012. if (p1->flags & NVG_LEFT) {
  1013. float rx0 = p1->x + dlx0 * rw;
  1014. float ry0 = p1->y + dly0 * rw;
  1015. float rx1 = p1->x + dlx1 * rw;
  1016. float ry1 = p1->y + dly1 * rw;
  1017. float lx = p1->x - p1->dmx * lw;
  1018. float ly = p1->y - p1->dmy * lw;
  1019. nvg__vset(dst, rx0, ry0, 0,1); dst++;
  1020. nvg__vset(dst, lx, ly, 1,1); dst++;
  1021. nvg__vset(dst, rx1, ry1, 0,1); dst++;
  1022. nvg__vset(dst, lx, ly, 1,1); dst++;
  1023. } else {
  1024. float rx = p1->x + p1->dmx * rw;
  1025. float ry = p1->y + p1->dmy * rw;
  1026. float lx0 = p1->x - dlx0 * lw;
  1027. float ly0 = p1->y - dly0 * lw;
  1028. float lx1 = p1->x - dlx1 * lw;
  1029. float ly1 = p1->y - dly1 * lw;
  1030. nvg__vset(dst, rx, ry, 0,1); dst++;
  1031. nvg__vset(dst, lx0, ly0, 1,1); dst++;
  1032. nvg__vset(dst, rx, ry, 0,1); dst++;
  1033. nvg__vset(dst, lx1, ly1, 1,1); dst++;
  1034. }
  1035. } else {
  1036. nvg__vset(dst, p1->x + (p1->dmx * rw), p1->y + (p1->dmy * rw), 0,1); dst++;
  1037. nvg__vset(dst, p1->x - (p1->dmx * lw), p1->y - (p1->dmy * lw), 1,1); dst++;
  1038. }
  1039. p0 = p1++;
  1040. }
  1041. if (loop) {
  1042. // Loop it
  1043. nvg__vset(dst, verts[0].x, verts[0].y, 0,1); dst++;
  1044. nvg__vset(dst, verts[1].x, verts[1].y, 1,1); dst++;
  1045. } else {
  1046. // Add cap
  1047. float dx, dy, dlx, dly;
  1048. dx = p1->x - p0->x;
  1049. dy = p1->y - p0->y;
  1050. nvg__normalize(&dx, &dy);
  1051. dlx = dy;
  1052. dly = -dx;
  1053. nvg__vset(dst, p1->x + dlx*rw, p1->y + dly * rw, 0,1); dst++;
  1054. nvg__vset(dst, p1->x - dlx*lw, p1->y - dly * lw, 1,1); dst++;
  1055. nvg__vset(dst, p1->x + dlx*rw + dx*NVG_AA, p1->y + dly*rw + dy*NVG_AA, 0,0); dst++;
  1056. nvg__vset(dst, p1->x - dlx*lw + dx*NVG_AA, p1->y - dly*lw + dy*NVG_AA, 1,0); dst++;
  1057. }
  1058. path->nstroke = (int)(dst - verts);
  1059. verts += nstroke;
  1060. } else {
  1061. path->stroke = 0;
  1062. path->nstroke = 0;
  1063. }
  1064. }
  1065. return 1;
  1066. }
  1067. // Draw
  1068. void nvgBeginPath(struct NVGcontext* ctx)
  1069. {
  1070. ctx->ncommands = 0;
  1071. nvg__clearPathCache(ctx);
  1072. }
  1073. void nvgMoveTo(struct NVGcontext* ctx, float x, float y)
  1074. {
  1075. float vals[] = { NVG_MOVETO, x, y };
  1076. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1077. }
  1078. void nvgLineTo(struct NVGcontext* ctx, float x, float y)
  1079. {
  1080. float vals[] = { NVG_LINETO, x, y };
  1081. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1082. }
  1083. void nvgBezierTo(struct NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y)
  1084. {
  1085. float vals[] = { NVG_BEZIERTO, c1x, c1y, c2x, c2y, x, y };
  1086. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1087. }
  1088. void nvgArcTo(struct NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius)
  1089. {
  1090. float x0 = ctx->commandx;
  1091. float y0 = ctx->commandy;
  1092. float dx0,dy0, dx1,dy1, a, d, cx,cy, a0,a1;
  1093. int dir;
  1094. if (ctx->ncommands == 0) {
  1095. return;
  1096. }
  1097. // Handle degenerate cases.
  1098. if (nvg__ptEquals(x0,y0, x1,y1, ctx->distTol) ||
  1099. nvg__ptEquals(x1,y1, x2,y2, ctx->distTol) ||
  1100. nvg__distPtSeg(x1,y1, x0,y0, x2,y2) < ctx->distTol*ctx->distTol ||
  1101. radius < ctx->distTol) {
  1102. nvgLineTo(ctx, x1,y1);
  1103. return;
  1104. }
  1105. // Calculate tangential circle to lines (x0,y0)-(x1,y1) and (x1,y1)-(x2,y2).
  1106. dx0 = x0-x1;
  1107. dy0 = y0-y1;
  1108. dx1 = x2-x1;
  1109. dy1 = y2-y1;
  1110. nvg__normalize(&dx0,&dy0);
  1111. nvg__normalize(&dx1,&dy1);
  1112. a = nvg__acosf(dx0*dx1 + dy0*dy1);
  1113. d = radius / nvg__tanf(a/2.0f);
  1114. // printf("a=%f° d=%f\n", a/NVG_PI*180.0f, d);
  1115. if (d > 10000.0f) {
  1116. nvgLineTo(ctx, x1,y1);
  1117. return;
  1118. }
  1119. if (nvg__cross(dx0,dy0, dx1,dy1) > 0.0f) {
  1120. cx = x1 + dx0*d + dy0*radius;
  1121. cy = y1 + dy0*d + -dx0*radius;
  1122. a0 = nvg__atan2f(dx0, -dy0);
  1123. a1 = nvg__atan2f(-dx1, dy1);
  1124. dir = NVG_CW;
  1125. // printf("CW c=(%f, %f) a0=%f° a1=%f°\n", cx, cy, a0/NVG_PI*180.0f, a1/NVG_PI*180.0f);
  1126. } else {
  1127. cx = x1 + dx0*d + -dy0*radius;
  1128. cy = y1 + dy0*d + dx0*radius;
  1129. a0 = nvg__atan2f(-dx0, dy0);
  1130. a1 = nvg__atan2f(dx1, -dy1);
  1131. dir = NVG_CCW;
  1132. // printf("CCW c=(%f, %f) a0=%f° a1=%f°\n", cx, cy, a0/NVG_PI*180.0f, a1/NVG_PI*180.0f);
  1133. }
  1134. nvgArc(ctx, cx, cy, radius, a0, a1, dir);
  1135. }
  1136. void nvgClosePath(struct NVGcontext* ctx)
  1137. {
  1138. float vals[] = { NVG_CLOSE };
  1139. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1140. }
  1141. void nvgPathWinding(struct NVGcontext* ctx, int dir)
  1142. {
  1143. float vals[] = { NVG_WINDING, (float)dir };
  1144. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1145. }
  1146. void nvgArc(struct NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir)
  1147. {
  1148. float a, da, hda, kappa;
  1149. float dx, dy, x, y, tanx, tany;
  1150. float px, py, ptanx, ptany;
  1151. float vals[3 + 5*7 + 100];
  1152. int i, ndivs, nvals;
  1153. int move = ctx->ncommands > 0 ? NVG_LINETO : NVG_MOVETO;
  1154. // Clamp angles
  1155. da = a1 - a0;
  1156. if (dir == NVG_CW) {
  1157. if (nvg__absf(da) >= NVG_PI*2) {
  1158. da = NVG_PI*2;
  1159. } else {
  1160. while (da < 0.0f) da += NVG_PI*2;
  1161. }
  1162. } else {
  1163. if (nvg__absf(da) >= NVG_PI*2) {
  1164. da = -NVG_PI*2;
  1165. } else {
  1166. while (da > 0.0f) da -= NVG_PI*2;
  1167. }
  1168. }
  1169. // Split arc into max 90 degree segments.
  1170. ndivs = nvg__maxi(1, nvg__mini((int)(nvg__absf(da) / (NVG_PI*0.5f) + 0.5f), 5));
  1171. hda = (da / (float)ndivs) / 2.0f;
  1172. kappa = nvg__absf(4.0f / 3.0f * (1.0f - nvg__cosf(hda)) / nvg__sinf(hda));
  1173. if (dir == NVG_CCW)
  1174. kappa = -kappa;
  1175. nvals = 0;
  1176. for (i = 0; i <= ndivs; i++) {
  1177. a = a0 + da * (i/(float)ndivs);
  1178. dx = nvg__cosf(a);
  1179. dy = nvg__sinf(a);
  1180. x = cx + dx*r;
  1181. y = cy + dy*r;
  1182. tanx = -dy*r*kappa;
  1183. tany = dx*r*kappa;
  1184. if (i == 0) {
  1185. vals[nvals++] = move;
  1186. vals[nvals++] = x;
  1187. vals[nvals++] = y;
  1188. } else {
  1189. vals[nvals++] = NVG_BEZIERTO;
  1190. vals[nvals++] = px+ptanx;
  1191. vals[nvals++] = py+ptany;
  1192. vals[nvals++] = x-tanx;
  1193. vals[nvals++] = y-tany;
  1194. vals[nvals++] = x;
  1195. vals[nvals++] = y;
  1196. }
  1197. px = x;
  1198. py = y;
  1199. ptanx = tanx;
  1200. ptany = tany;
  1201. }
  1202. nvg__appendCommands(ctx, vals, nvals);
  1203. }
  1204. void nvgRect(struct NVGcontext* ctx, float x, float y, float w, float h)
  1205. {
  1206. float vals[] = {
  1207. NVG_MOVETO, x,y,
  1208. NVG_LINETO, x+w,y,
  1209. NVG_LINETO, x+w,y+h,
  1210. NVG_LINETO, x,y+h,
  1211. NVG_CLOSE
  1212. };
  1213. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1214. }
  1215. void nvgRoundedRect(struct NVGcontext* ctx, float x, float y, float w, float h, float r)
  1216. {
  1217. if (r < 0.1f) {
  1218. nvgRect(ctx, x,y,w,h);
  1219. return;
  1220. }
  1221. float vals[] = {
  1222. NVG_MOVETO, x+r, y,
  1223. NVG_LINETO, x+w-r, y,
  1224. NVG_BEZIERTO, x+w-r*(1-NVG_KAPPA90), y, x+w, y+r*(1-NVG_KAPPA90), x+w, y+r,
  1225. NVG_LINETO, x+w, y+h-r,
  1226. NVG_BEZIERTO, x+w, y+h-r*(1-NVG_KAPPA90), x+w-r*(1-NVG_KAPPA90), y+h, x+w-r, y+h,
  1227. NVG_LINETO, x+r, y+h,
  1228. NVG_BEZIERTO, x+r*(1-NVG_KAPPA90), y+h, x, y+h-r*(1-NVG_KAPPA90), x, y+h-r,
  1229. NVG_LINETO, x, y+r,
  1230. NVG_BEZIERTO, x, y+r*(1-NVG_KAPPA90), x+r*(1-NVG_KAPPA90), y, x+r, y,
  1231. NVG_CLOSE
  1232. };
  1233. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1234. }
  1235. void nvgEllipse(struct NVGcontext* ctx, float cx, float cy, float rx, float ry)
  1236. {
  1237. float vals[] = {
  1238. NVG_MOVETO, cx+rx, cy,
  1239. NVG_BEZIERTO, cx+rx, cy+ry*NVG_KAPPA90, cx+rx*NVG_KAPPA90, cy+ry, cx, cy+ry,
  1240. NVG_BEZIERTO, cx-rx*NVG_KAPPA90, cy+ry, cx-rx, cy+ry*NVG_KAPPA90, cx-rx, cy,
  1241. NVG_BEZIERTO, cx-rx, cy-ry*NVG_KAPPA90, cx-rx*NVG_KAPPA90, cy-ry, cx, cy-ry,
  1242. NVG_BEZIERTO, cx+rx*NVG_KAPPA90, cy-ry, cx+rx, cy-ry*NVG_KAPPA90, cx+rx, cy,
  1243. NVG_CLOSE
  1244. };
  1245. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1246. }
  1247. void nvgCircle(struct NVGcontext* ctx, float cx, float cy, float r)
  1248. {
  1249. nvgEllipse(ctx, cx,cy, r,r);
  1250. }
  1251. void nvgFill(struct NVGcontext* ctx)
  1252. {
  1253. struct NVGstate* state = nvg__getState(ctx);
  1254. const struct NVGpath* path;
  1255. int i;
  1256. nvg__flattenPaths(ctx, state->miterLimit);
  1257. nvg__expandStrokeAndFill(ctx, NVG_FILL|NVG_STROKE, NVG_AA);
  1258. ctx->params.renderFill(ctx->params.userPtr, &state->fill, &state->scissor, NVG_AA,
  1259. ctx->cache->bounds, ctx->cache->paths, ctx->cache->npaths);
  1260. // Count triangles
  1261. for (i = 0; i < ctx->cache->npaths; i++) {
  1262. path = &ctx->cache->paths[i];
  1263. ctx->fillTriCount += path->nfill-2;
  1264. ctx->fillTriCount += path->nstroke-2;
  1265. ctx->drawCallCount += 2;
  1266. }
  1267. }
  1268. void nvgStroke(struct NVGcontext* ctx)
  1269. {
  1270. struct NVGstate* state = nvg__getState(ctx);
  1271. float scale = nvg__getAverageScale(state->xform);
  1272. float strokeWidth = nvg__clampf(state->strokeWidth * scale, 1.0f, 20.0f);
  1273. const struct NVGpath* path;
  1274. int i;
  1275. nvg__flattenPaths(ctx, state->miterLimit);
  1276. nvg__expandStrokeAndFill(ctx, NVG_STROKE|NVG_CAPS, strokeWidth*0.5f + NVG_AA/2.0f);
  1277. ctx->params.renderStroke(ctx->params.userPtr, &state->stroke, &state->scissor, NVG_AA,
  1278. strokeWidth, ctx->cache->paths, ctx->cache->npaths);
  1279. // Count triangles
  1280. for (i = 0; i < ctx->cache->npaths; i++) {
  1281. path = &ctx->cache->paths[i];
  1282. ctx->strokeTriCount += path->nstroke-2;
  1283. ctx->drawCallCount++;
  1284. }
  1285. }
  1286. // Add fonts
  1287. int nvgCreateFont(struct NVGcontext* ctx, const char* name, const char* path)
  1288. {
  1289. return fonsAddFont(ctx->fs, name, path);
  1290. }
  1291. int nvgCreateFontMem(struct NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData)
  1292. {
  1293. return fonsAddFontMem(ctx->fs, name, data, ndata, freeData);
  1294. }
  1295. int nvgFindFont(struct NVGcontext* ctx, const char* name)
  1296. {
  1297. if (name == NULL) return -1;
  1298. return fonsGetFontByName(ctx->fs, name);
  1299. }
  1300. // State setting
  1301. void nvgFontSize(struct NVGcontext* ctx, float size)
  1302. {
  1303. struct NVGstate* state = nvg__getState(ctx);
  1304. state->fontSize = size;
  1305. }
  1306. void nvgLetterSpacing(struct NVGcontext* ctx, float spacing)
  1307. {
  1308. struct NVGstate* state = nvg__getState(ctx);
  1309. state->letterSpacing = spacing;
  1310. }
  1311. void nvgFontBlur(struct NVGcontext* ctx, float blur)
  1312. {
  1313. struct NVGstate* state = nvg__getState(ctx);
  1314. state->fontBlur = blur;
  1315. }
  1316. void nvgTextAlign(struct NVGcontext* ctx, int align)
  1317. {
  1318. struct NVGstate* state = nvg__getState(ctx);
  1319. state->textAlign = align;
  1320. }
  1321. void nvgFontFaceId(struct NVGcontext* ctx, int font)
  1322. {
  1323. struct NVGstate* state = nvg__getState(ctx);
  1324. state->fontId = font;
  1325. }
  1326. void nvgFontFace(struct NVGcontext* ctx, const char* font)
  1327. {
  1328. struct NVGstate* state = nvg__getState(ctx);
  1329. state->fontId = fonsGetFontByName(ctx->fs, font);
  1330. }
  1331. float nvgText(struct NVGcontext* ctx, float x, float y, const char* string, const char* end)
  1332. {
  1333. struct NVGstate* state = nvg__getState(ctx);
  1334. struct FONStextIter iter;
  1335. struct FONSquad q;
  1336. struct NVGvertex* verts;
  1337. float scale = nvg__minf(nvg__getAverageScale(state->xform), 4.0f);
  1338. float invscale = 1.0f / scale;
  1339. int dirty[4];
  1340. int cverts = 0;
  1341. int nverts = 0;
  1342. if (end == NULL)
  1343. end = string + strlen(string);
  1344. if (state->fontId == FONS_INVALID) return x;
  1345. fonsSetSize(ctx->fs, state->fontSize*scale);
  1346. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  1347. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  1348. fonsSetAlign(ctx->fs, state->textAlign);
  1349. fonsSetFont(ctx->fs, state->fontId);
  1350. cverts = nvg__maxi(2, (int)(end - string)) * 6; // conservative estimate.
  1351. verts = nvg__allocTempVerts(ctx, cverts);
  1352. if (verts == NULL) return x;
  1353. fonsTextIterInit(ctx->fs, &iter, x*scale, y*scale, string, end);
  1354. while (fonsTextIterNext(ctx->fs, &iter, &q)) {
  1355. // Trasnform corners.
  1356. float c[4*2];
  1357. nvg__xformPt(&c[0],&c[1], q.x0*invscale, q.y0*invscale, state->xform);
  1358. nvg__xformPt(&c[2],&c[3], q.x1*invscale, q.y0*invscale, state->xform);
  1359. nvg__xformPt(&c[4],&c[5], q.x1*invscale, q.y1*invscale, state->xform);
  1360. nvg__xformPt(&c[6],&c[7], q.x0*invscale, q.y1*invscale, state->xform);
  1361. // Create triangles
  1362. if (nverts+6 <= cverts) {
  1363. nvg__vset(&verts[nverts], c[0], c[1], q.s0, q.t0); nverts++;
  1364. nvg__vset(&verts[nverts], c[4], c[5], q.s1, q.t1); nverts++;
  1365. nvg__vset(&verts[nverts], c[2], c[3], q.s1, q.t0); nverts++;
  1366. nvg__vset(&verts[nverts], c[0], c[1], q.s0, q.t0); nverts++;
  1367. nvg__vset(&verts[nverts], c[6], c[7], q.s0, q.t1); nverts++;
  1368. nvg__vset(&verts[nverts], c[4], c[5], q.s1, q.t1); nverts++;
  1369. }
  1370. }
  1371. if (fonsValidateTexture(ctx->fs, dirty)) {
  1372. // Update texture
  1373. if (ctx->fontImage != 0) {
  1374. int iw, ih;
  1375. const unsigned char* data = fonsGetTextureData(ctx->fs, &iw, &ih);
  1376. int x = dirty[0];
  1377. int y = dirty[1];
  1378. int w = dirty[2] - dirty[0];
  1379. int h = dirty[3] - dirty[1];
  1380. ctx->params.renderUpdateTexture(ctx->params.userPtr, ctx->fontImage, x,y, w,h, data);
  1381. }
  1382. }
  1383. // Render triangles.
  1384. ctx->params.renderTriangles(ctx->params.userPtr, &state->fill, &state->scissor, ctx->fontImage, verts, nverts);
  1385. ctx->drawCallCount++;
  1386. ctx->textTriCount += nverts/3;
  1387. return iter.x;
  1388. }
  1389. float nvgTextBounds(struct NVGcontext* ctx, const char* string, const char* end, float* bounds)
  1390. {
  1391. struct NVGstate* state = nvg__getState(ctx);
  1392. if (state->fontId == FONS_INVALID) return 0;
  1393. fonsSetSize(ctx->fs, state->fontSize);
  1394. fonsSetSpacing(ctx->fs, state->letterSpacing);
  1395. fonsSetBlur(ctx->fs, state->fontBlur);
  1396. fonsSetAlign(ctx->fs, state->textAlign);
  1397. fonsSetFont(ctx->fs, state->fontId);
  1398. return fonsTextBounds(ctx->fs, string, end, bounds);
  1399. }
  1400. void nvgVertMetrics(struct NVGcontext* ctx, float* ascender, float* descender, float* lineh)
  1401. {
  1402. struct NVGstate* state = nvg__getState(ctx);
  1403. if (state->fontId == FONS_INVALID) return;
  1404. fonsSetSize(ctx->fs, state->fontSize);
  1405. fonsSetSpacing(ctx->fs, state->letterSpacing);
  1406. fonsSetBlur(ctx->fs, state->fontBlur);
  1407. fonsSetAlign(ctx->fs, state->textAlign);
  1408. fonsSetFont(ctx->fs, state->fontId);
  1409. fonsVertMetrics(ctx->fs, ascender, descender, lineh);
  1410. }