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.

1695 lines
42KB

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