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.

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