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.

1958 lines
50KB

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