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.

2946 lines
76KB

  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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include <memory.h>
  22. #include "nanovg.h"
  23. #define FONTSTASH_IMPLEMENTATION
  24. #include "fontstash.h"
  25. #ifndef NVG_NO_STB
  26. #define STB_IMAGE_IMPLEMENTATION
  27. #include "stb_image.h"
  28. #endif
  29. #ifdef _MSC_VER
  30. #pragma warning(disable: 4100) // unreferenced formal parameter
  31. #pragma warning(disable: 4127) // conditional expression is constant
  32. #pragma warning(disable: 4204) // nonstandard extension used : non-constant aggregate initializer
  33. #pragma warning(disable: 4706) // assignment within conditional expression
  34. #endif
  35. #define NVG_INIT_FONTIMAGE_SIZE 512
  36. #define NVG_MAX_FONTIMAGE_SIZE 2048
  37. #define NVG_MAX_FONTIMAGES 4
  38. #define NVG_INIT_COMMANDS_SIZE 256
  39. #define NVG_INIT_POINTS_SIZE 128
  40. #define NVG_INIT_PATHS_SIZE 16
  41. #define NVG_INIT_VERTS_SIZE 256
  42. #define NVG_MAX_STATES 32
  43. #define NVG_KAPPA90 0.5522847493f // Length proportional to radius of a cubic bezier handle for 90deg arcs.
  44. #define NVG_COUNTOF(arr) (sizeof(arr) / sizeof(0[arr]))
  45. enum NVGcommands {
  46. NVG_MOVETO = 0,
  47. NVG_LINETO = 1,
  48. NVG_BEZIERTO = 2,
  49. NVG_CLOSE = 3,
  50. NVG_WINDING = 4,
  51. };
  52. enum NVGpointFlags
  53. {
  54. NVG_PT_CORNER = 0x01,
  55. NVG_PT_LEFT = 0x02,
  56. NVG_PT_BEVEL = 0x04,
  57. NVG_PR_INNERBEVEL = 0x08,
  58. };
  59. struct NVGstate {
  60. NVGcompositeOperationState compositeOperation;
  61. int shapeAntiAlias;
  62. NVGpaint fill;
  63. NVGpaint stroke;
  64. float strokeWidth;
  65. float miterLimit;
  66. int lineJoin;
  67. int lineCap;
  68. float alpha;
  69. float xform[6];
  70. NVGscissor scissor;
  71. float fontSize;
  72. float letterSpacing;
  73. float lineHeight;
  74. float fontBlur;
  75. int textAlign;
  76. int fontId;
  77. };
  78. typedef struct NVGstate NVGstate;
  79. struct NVGpoint {
  80. float x,y;
  81. float dx, dy;
  82. float len;
  83. float dmx, dmy;
  84. unsigned char flags;
  85. };
  86. typedef struct NVGpoint NVGpoint;
  87. struct NVGpathCache {
  88. NVGpoint* points;
  89. int npoints;
  90. int cpoints;
  91. NVGpath* paths;
  92. int npaths;
  93. int cpaths;
  94. NVGvertex* verts;
  95. int nverts;
  96. int cverts;
  97. float bounds[4];
  98. };
  99. typedef struct NVGpathCache NVGpathCache;
  100. struct NVGcontext {
  101. NVGparams params;
  102. float* commands;
  103. int ccommands;
  104. int ncommands;
  105. float commandx, commandy;
  106. NVGstate states[NVG_MAX_STATES];
  107. int nstates;
  108. NVGpathCache* cache;
  109. float tessTol;
  110. float distTol;
  111. float fringeWidth;
  112. float devicePxRatio;
  113. struct FONScontext* fs;
  114. int fontImages[NVG_MAX_FONTIMAGES];
  115. int fontImageIdx;
  116. int drawCallCount;
  117. int fillTriCount;
  118. int strokeTriCount;
  119. int textTriCount;
  120. };
  121. static float nvg__sqrtf(float a) { return sqrtf(a); }
  122. static float nvg__modf(float a, float b) { return fmodf(a, b); }
  123. static float nvg__sinf(float a) { return sinf(a); }
  124. static float nvg__cosf(float a) { return cosf(a); }
  125. static float nvg__tanf(float a) { return tanf(a); }
  126. static float nvg__atan2f(float a,float b) { return atan2f(a, b); }
  127. static float nvg__acosf(float a) { return acosf(a); }
  128. static int nvg__mini(int a, int b) { return a < b ? a : b; }
  129. static int nvg__maxi(int a, int b) { return a > b ? a : b; }
  130. static int nvg__clampi(int a, int mn, int mx) { return a < mn ? mn : (a > mx ? mx : a); }
  131. static float nvg__minf(float a, float b) { return a < b ? a : b; }
  132. static float nvg__maxf(float a, float b) { return a > b ? a : b; }
  133. static float nvg__absf(float a) { return a >= 0.0f ? a : -a; }
  134. static float nvg__signf(float a) { return a >= 0.0f ? 1.0f : -1.0f; }
  135. static float nvg__clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
  136. static float nvg__cross(float dx0, float dy0, float dx1, float dy1) { return dx1*dy0 - dx0*dy1; }
  137. static float nvg__normalize(float *x, float* y)
  138. {
  139. float d = nvg__sqrtf((*x)*(*x) + (*y)*(*y));
  140. if (d > 1e-6f) {
  141. float id = 1.0f / d;
  142. *x *= id;
  143. *y *= id;
  144. }
  145. return d;
  146. }
  147. static void nvg__deletePathCache(NVGpathCache* c)
  148. {
  149. if (c == NULL) return;
  150. if (c->points != NULL) free(c->points);
  151. if (c->paths != NULL) free(c->paths);
  152. if (c->verts != NULL) free(c->verts);
  153. free(c);
  154. }
  155. static NVGpathCache* nvg__allocPathCache(void)
  156. {
  157. NVGpathCache* c = (NVGpathCache*)malloc(sizeof(NVGpathCache));
  158. if (c == NULL) goto error;
  159. memset(c, 0, sizeof(NVGpathCache));
  160. c->points = (NVGpoint*)malloc(sizeof(NVGpoint)*NVG_INIT_POINTS_SIZE);
  161. if (!c->points) goto error;
  162. c->npoints = 0;
  163. c->cpoints = NVG_INIT_POINTS_SIZE;
  164. c->paths = (NVGpath*)malloc(sizeof(NVGpath)*NVG_INIT_PATHS_SIZE);
  165. if (!c->paths) goto error;
  166. c->npaths = 0;
  167. c->cpaths = NVG_INIT_PATHS_SIZE;
  168. c->verts = (NVGvertex*)malloc(sizeof(NVGvertex)*NVG_INIT_VERTS_SIZE);
  169. if (!c->verts) goto error;
  170. c->nverts = 0;
  171. c->cverts = NVG_INIT_VERTS_SIZE;
  172. return c;
  173. error:
  174. nvg__deletePathCache(c);
  175. return NULL;
  176. }
  177. static void nvg__setDevicePixelRatio(NVGcontext* ctx, float ratio)
  178. {
  179. ctx->tessTol = 0.25f / ratio;
  180. ctx->distTol = 0.01f / ratio;
  181. ctx->fringeWidth = 1.0f / ratio;
  182. ctx->devicePxRatio = ratio;
  183. }
  184. static NVGcompositeOperationState nvg__compositeOperationState(int op)
  185. {
  186. int sfactor, dfactor;
  187. if (op == NVG_SOURCE_OVER)
  188. {
  189. sfactor = NVG_ONE;
  190. dfactor = NVG_ONE_MINUS_SRC_ALPHA;
  191. }
  192. else if (op == NVG_SOURCE_IN)
  193. {
  194. sfactor = NVG_DST_ALPHA;
  195. dfactor = NVG_ZERO;
  196. }
  197. else if (op == NVG_SOURCE_OUT)
  198. {
  199. sfactor = NVG_ONE_MINUS_DST_ALPHA;
  200. dfactor = NVG_ZERO;
  201. }
  202. else if (op == NVG_ATOP)
  203. {
  204. sfactor = NVG_DST_ALPHA;
  205. dfactor = NVG_ONE_MINUS_SRC_ALPHA;
  206. }
  207. else if (op == NVG_DESTINATION_OVER)
  208. {
  209. sfactor = NVG_ONE_MINUS_DST_ALPHA;
  210. dfactor = NVG_ONE;
  211. }
  212. else if (op == NVG_DESTINATION_IN)
  213. {
  214. sfactor = NVG_ZERO;
  215. dfactor = NVG_SRC_ALPHA;
  216. }
  217. else if (op == NVG_DESTINATION_OUT)
  218. {
  219. sfactor = NVG_ZERO;
  220. dfactor = NVG_ONE_MINUS_SRC_ALPHA;
  221. }
  222. else if (op == NVG_DESTINATION_ATOP)
  223. {
  224. sfactor = NVG_ONE_MINUS_DST_ALPHA;
  225. dfactor = NVG_SRC_ALPHA;
  226. }
  227. else if (op == NVG_LIGHTER)
  228. {
  229. sfactor = NVG_ONE;
  230. dfactor = NVG_ONE;
  231. }
  232. else if (op == NVG_COPY)
  233. {
  234. sfactor = NVG_ONE;
  235. dfactor = NVG_ZERO;
  236. }
  237. else if (op == NVG_XOR)
  238. {
  239. sfactor = NVG_ONE_MINUS_DST_ALPHA;
  240. dfactor = NVG_ONE_MINUS_SRC_ALPHA;
  241. }
  242. else
  243. {
  244. sfactor = NVG_ONE;
  245. dfactor = NVG_ZERO;
  246. }
  247. NVGcompositeOperationState state;
  248. state.srcRGB = sfactor;
  249. state.dstRGB = dfactor;
  250. state.srcAlpha = sfactor;
  251. state.dstAlpha = dfactor;
  252. return state;
  253. }
  254. static NVGstate* nvg__getState(NVGcontext* ctx)
  255. {
  256. return &ctx->states[ctx->nstates-1];
  257. }
  258. NVGcontext* nvgCreateInternal(NVGparams* params)
  259. {
  260. FONSparams fontParams;
  261. NVGcontext* ctx = (NVGcontext*)malloc(sizeof(NVGcontext));
  262. int i;
  263. if (ctx == NULL) goto error;
  264. memset(ctx, 0, sizeof(NVGcontext));
  265. ctx->params = *params;
  266. for (i = 0; i < NVG_MAX_FONTIMAGES; i++)
  267. ctx->fontImages[i] = 0;
  268. ctx->commands = (float*)malloc(sizeof(float)*NVG_INIT_COMMANDS_SIZE);
  269. if (!ctx->commands) goto error;
  270. ctx->ncommands = 0;
  271. ctx->ccommands = NVG_INIT_COMMANDS_SIZE;
  272. ctx->cache = nvg__allocPathCache();
  273. if (ctx->cache == NULL) goto error;
  274. nvgSave(ctx);
  275. nvgReset(ctx);
  276. nvg__setDevicePixelRatio(ctx, 1.0f);
  277. if (ctx->params.renderCreate(ctx->params.userPtr) == 0) goto error;
  278. // Init font rendering
  279. memset(&fontParams, 0, sizeof(fontParams));
  280. fontParams.width = NVG_INIT_FONTIMAGE_SIZE;
  281. fontParams.height = NVG_INIT_FONTIMAGE_SIZE;
  282. fontParams.flags = FONS_ZERO_TOPLEFT;
  283. fontParams.renderCreate = NULL;
  284. fontParams.renderUpdate = NULL;
  285. fontParams.renderDraw = NULL;
  286. fontParams.renderDelete = NULL;
  287. fontParams.userPtr = NULL;
  288. ctx->fs = fonsCreateInternal(&fontParams);
  289. if (ctx->fs == NULL) goto error;
  290. // Create font texture
  291. ctx->fontImages[0] = ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_ALPHA, fontParams.width, fontParams.height, 0, NULL);
  292. if (ctx->fontImages[0] == 0) goto error;
  293. ctx->fontImageIdx = 0;
  294. return ctx;
  295. error:
  296. nvgDeleteInternal(ctx);
  297. return 0;
  298. }
  299. NVGparams* nvgInternalParams(NVGcontext* ctx)
  300. {
  301. return &ctx->params;
  302. }
  303. void nvgDeleteInternal(NVGcontext* ctx)
  304. {
  305. int i;
  306. if (ctx == NULL) return;
  307. if (ctx->commands != NULL) free(ctx->commands);
  308. if (ctx->cache != NULL) nvg__deletePathCache(ctx->cache);
  309. if (ctx->fs)
  310. fonsDeleteInternal(ctx->fs);
  311. for (i = 0; i < NVG_MAX_FONTIMAGES; i++) {
  312. if (ctx->fontImages[i] != 0) {
  313. nvgDeleteImage(ctx, ctx->fontImages[i]);
  314. ctx->fontImages[i] = 0;
  315. }
  316. }
  317. if (ctx->params.renderDelete != NULL)
  318. ctx->params.renderDelete(ctx->params.userPtr);
  319. free(ctx);
  320. }
  321. void nvgBeginFrame(NVGcontext* ctx, float windowWidth, float windowHeight, float devicePixelRatio)
  322. {
  323. /* printf("Tris: draws:%d fill:%d stroke:%d text:%d TOT:%d\n",
  324. ctx->drawCallCount, ctx->fillTriCount, ctx->strokeTriCount, ctx->textTriCount,
  325. ctx->fillTriCount+ctx->strokeTriCount+ctx->textTriCount);*/
  326. ctx->nstates = 0;
  327. nvgSave(ctx);
  328. nvgReset(ctx);
  329. nvg__setDevicePixelRatio(ctx, devicePixelRatio);
  330. ctx->params.renderViewport(ctx->params.userPtr, windowWidth, windowHeight, devicePixelRatio);
  331. ctx->drawCallCount = 0;
  332. ctx->fillTriCount = 0;
  333. ctx->strokeTriCount = 0;
  334. ctx->textTriCount = 0;
  335. }
  336. void nvgCancelFrame(NVGcontext* ctx)
  337. {
  338. ctx->params.renderCancel(ctx->params.userPtr);
  339. }
  340. void nvgEndFrame(NVGcontext* ctx)
  341. {
  342. ctx->params.renderFlush(ctx->params.userPtr);
  343. if (ctx->fontImageIdx != 0) {
  344. int fontImage = ctx->fontImages[ctx->fontImageIdx];
  345. int i, j, iw, ih;
  346. // delete images that smaller than current one
  347. if (fontImage == 0)
  348. return;
  349. nvgImageSize(ctx, fontImage, &iw, &ih);
  350. for (i = j = 0; i < ctx->fontImageIdx; i++) {
  351. if (ctx->fontImages[i] != 0) {
  352. int nw, nh;
  353. nvgImageSize(ctx, ctx->fontImages[i], &nw, &nh);
  354. if (nw < iw || nh < ih)
  355. nvgDeleteImage(ctx, ctx->fontImages[i]);
  356. else
  357. ctx->fontImages[j++] = ctx->fontImages[i];
  358. }
  359. }
  360. // make current font image to first
  361. ctx->fontImages[j++] = ctx->fontImages[0];
  362. ctx->fontImages[0] = fontImage;
  363. ctx->fontImageIdx = 0;
  364. // clear all images after j
  365. for (i = j; i < NVG_MAX_FONTIMAGES; i++)
  366. ctx->fontImages[i] = 0;
  367. }
  368. }
  369. NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b)
  370. {
  371. return nvgRGBA(r,g,b,255);
  372. }
  373. NVGcolor nvgRGBf(float r, float g, float b)
  374. {
  375. return nvgRGBAf(r,g,b,1.0f);
  376. }
  377. NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
  378. {
  379. NVGcolor color;
  380. // Use longer initialization to suppress warning.
  381. color.r = r / 255.0f;
  382. color.g = g / 255.0f;
  383. color.b = b / 255.0f;
  384. color.a = a / 255.0f;
  385. return color;
  386. }
  387. NVGcolor nvgRGBAf(float r, float g, float b, float a)
  388. {
  389. NVGcolor color;
  390. // Use longer initialization to suppress warning.
  391. color.r = r;
  392. color.g = g;
  393. color.b = b;
  394. color.a = a;
  395. return color;
  396. }
  397. NVGcolor nvgTransRGBA(NVGcolor c, unsigned char a)
  398. {
  399. c.a = a / 255.0f;
  400. return c;
  401. }
  402. NVGcolor nvgTransRGBAf(NVGcolor c, float a)
  403. {
  404. c.a = a;
  405. return c;
  406. }
  407. NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u)
  408. {
  409. int i;
  410. float oneminu;
  411. NVGcolor cint = {{{0}}};
  412. u = nvg__clampf(u, 0.0f, 1.0f);
  413. oneminu = 1.0f - u;
  414. for( i = 0; i <4; i++ )
  415. {
  416. cint.rgba[i] = c0.rgba[i] * oneminu + c1.rgba[i] * u;
  417. }
  418. return cint;
  419. }
  420. NVGcolor nvgHSL(float h, float s, float l)
  421. {
  422. return nvgHSLA(h,s,l,255);
  423. }
  424. static float nvg__hue(float h, float m1, float m2)
  425. {
  426. if (h < 0) h += 1;
  427. if (h > 1) h -= 1;
  428. if (h < 1.0f/6.0f)
  429. return m1 + (m2 - m1) * h * 6.0f;
  430. else if (h < 3.0f/6.0f)
  431. return m2;
  432. else if (h < 4.0f/6.0f)
  433. return m1 + (m2 - m1) * (2.0f/3.0f - h) * 6.0f;
  434. return m1;
  435. }
  436. NVGcolor nvgHSLA(float h, float s, float l, unsigned char a)
  437. {
  438. float m1, m2;
  439. NVGcolor col;
  440. h = nvg__modf(h, 1.0f);
  441. if (h < 0.0f) h += 1.0f;
  442. s = nvg__clampf(s, 0.0f, 1.0f);
  443. l = nvg__clampf(l, 0.0f, 1.0f);
  444. m2 = l <= 0.5f ? (l * (1 + s)) : (l + s - l * s);
  445. m1 = 2 * l - m2;
  446. col.r = nvg__clampf(nvg__hue(h + 1.0f/3.0f, m1, m2), 0.0f, 1.0f);
  447. col.g = nvg__clampf(nvg__hue(h, m1, m2), 0.0f, 1.0f);
  448. col.b = nvg__clampf(nvg__hue(h - 1.0f/3.0f, m1, m2), 0.0f, 1.0f);
  449. col.a = a/255.0f;
  450. return col;
  451. }
  452. void nvgTransformIdentity(float* t)
  453. {
  454. t[0] = 1.0f; t[1] = 0.0f;
  455. t[2] = 0.0f; t[3] = 1.0f;
  456. t[4] = 0.0f; t[5] = 0.0f;
  457. }
  458. void nvgTransformTranslate(float* t, float tx, float ty)
  459. {
  460. t[0] = 1.0f; t[1] = 0.0f;
  461. t[2] = 0.0f; t[3] = 1.0f;
  462. t[4] = tx; t[5] = ty;
  463. }
  464. void nvgTransformScale(float* t, float sx, float sy)
  465. {
  466. t[0] = sx; t[1] = 0.0f;
  467. t[2] = 0.0f; t[3] = sy;
  468. t[4] = 0.0f; t[5] = 0.0f;
  469. }
  470. void nvgTransformRotate(float* t, float a)
  471. {
  472. float cs = nvg__cosf(a), sn = nvg__sinf(a);
  473. t[0] = cs; t[1] = sn;
  474. t[2] = -sn; t[3] = cs;
  475. t[4] = 0.0f; t[5] = 0.0f;
  476. }
  477. void nvgTransformSkewX(float* t, float a)
  478. {
  479. t[0] = 1.0f; t[1] = 0.0f;
  480. t[2] = nvg__tanf(a); t[3] = 1.0f;
  481. t[4] = 0.0f; t[5] = 0.0f;
  482. }
  483. void nvgTransformSkewY(float* t, float a)
  484. {
  485. t[0] = 1.0f; t[1] = nvg__tanf(a);
  486. t[2] = 0.0f; t[3] = 1.0f;
  487. t[4] = 0.0f; t[5] = 0.0f;
  488. }
  489. void nvgTransformMultiply(float* t, const float* s)
  490. {
  491. float t0 = t[0] * s[0] + t[1] * s[2];
  492. float t2 = t[2] * s[0] + t[3] * s[2];
  493. float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
  494. t[1] = t[0] * s[1] + t[1] * s[3];
  495. t[3] = t[2] * s[1] + t[3] * s[3];
  496. t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
  497. t[0] = t0;
  498. t[2] = t2;
  499. t[4] = t4;
  500. }
  501. void nvgTransformPremultiply(float* t, const float* s)
  502. {
  503. float s2[6];
  504. memcpy(s2, s, sizeof(float)*6);
  505. nvgTransformMultiply(s2, t);
  506. memcpy(t, s2, sizeof(float)*6);
  507. }
  508. int nvgTransformInverse(float* inv, const float* t)
  509. {
  510. double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1];
  511. if (det > -1e-6 && det < 1e-6) {
  512. nvgTransformIdentity(inv);
  513. return 0;
  514. }
  515. invdet = 1.0 / det;
  516. inv[0] = (float)(t[3] * invdet);
  517. inv[2] = (float)(-t[2] * invdet);
  518. inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
  519. inv[1] = (float)(-t[1] * invdet);
  520. inv[3] = (float)(t[0] * invdet);
  521. inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
  522. return 1;
  523. }
  524. void nvgTransformPoint(float* dx, float* dy, const float* t, float sx, float sy)
  525. {
  526. *dx = sx*t[0] + sy*t[2] + t[4];
  527. *dy = sx*t[1] + sy*t[3] + t[5];
  528. }
  529. float nvgDegToRad(float deg)
  530. {
  531. return deg / 180.0f * NVG_PI;
  532. }
  533. float nvgRadToDeg(float rad)
  534. {
  535. return rad / NVG_PI * 180.0f;
  536. }
  537. static void nvg__setPaintColor(NVGpaint* p, NVGcolor color)
  538. {
  539. memset(p, 0, sizeof(*p));
  540. nvgTransformIdentity(p->xform);
  541. p->radius = 0.0f;
  542. p->feather = 1.0f;
  543. p->innerColor = color;
  544. p->outerColor = color;
  545. }
  546. // State handling
  547. void nvgSave(NVGcontext* ctx)
  548. {
  549. if (ctx->nstates >= NVG_MAX_STATES)
  550. return;
  551. if (ctx->nstates > 0)
  552. memcpy(&ctx->states[ctx->nstates], &ctx->states[ctx->nstates-1], sizeof(NVGstate));
  553. ctx->nstates++;
  554. }
  555. void nvgRestore(NVGcontext* ctx)
  556. {
  557. if (ctx->nstates <= 1)
  558. return;
  559. ctx->nstates--;
  560. }
  561. void nvgReset(NVGcontext* ctx)
  562. {
  563. NVGstate* state = nvg__getState(ctx);
  564. memset(state, 0, sizeof(*state));
  565. nvg__setPaintColor(&state->fill, nvgRGBA(255,255,255,255));
  566. nvg__setPaintColor(&state->stroke, nvgRGBA(0,0,0,255));
  567. state->compositeOperation = nvg__compositeOperationState(NVG_SOURCE_OVER);
  568. state->shapeAntiAlias = 1;
  569. state->strokeWidth = 1.0f;
  570. state->miterLimit = 10.0f;
  571. state->lineCap = NVG_BUTT;
  572. state->lineJoin = NVG_MITER;
  573. state->alpha = 1.0f;
  574. nvgTransformIdentity(state->xform);
  575. state->scissor.extent[0] = -1.0f;
  576. state->scissor.extent[1] = -1.0f;
  577. state->fontSize = 16.0f;
  578. state->letterSpacing = 0.0f;
  579. state->lineHeight = 1.0f;
  580. state->fontBlur = 0.0f;
  581. state->textAlign = NVG_ALIGN_LEFT | NVG_ALIGN_BASELINE;
  582. state->fontId = 0;
  583. }
  584. // State setting
  585. void nvgShapeAntiAlias(NVGcontext* ctx, int enabled)
  586. {
  587. NVGstate* state = nvg__getState(ctx);
  588. state->shapeAntiAlias = enabled;
  589. }
  590. void nvgStrokeWidth(NVGcontext* ctx, float width)
  591. {
  592. NVGstate* state = nvg__getState(ctx);
  593. state->strokeWidth = width;
  594. }
  595. void nvgMiterLimit(NVGcontext* ctx, float limit)
  596. {
  597. NVGstate* state = nvg__getState(ctx);
  598. state->miterLimit = limit;
  599. }
  600. void nvgLineCap(NVGcontext* ctx, int cap)
  601. {
  602. NVGstate* state = nvg__getState(ctx);
  603. state->lineCap = cap;
  604. }
  605. void nvgLineJoin(NVGcontext* ctx, int join)
  606. {
  607. NVGstate* state = nvg__getState(ctx);
  608. state->lineJoin = join;
  609. }
  610. void nvgGlobalAlpha(NVGcontext* ctx, float alpha)
  611. {
  612. NVGstate* state = nvg__getState(ctx);
  613. state->alpha = alpha;
  614. }
  615. void nvgTransform(NVGcontext* ctx, float a, float b, float c, float d, float e, float f)
  616. {
  617. NVGstate* state = nvg__getState(ctx);
  618. float t[6] = { a, b, c, d, e, f };
  619. nvgTransformPremultiply(state->xform, t);
  620. }
  621. void nvgResetTransform(NVGcontext* ctx)
  622. {
  623. NVGstate* state = nvg__getState(ctx);
  624. nvgTransformIdentity(state->xform);
  625. }
  626. void nvgTranslate(NVGcontext* ctx, float x, float y)
  627. {
  628. NVGstate* state = nvg__getState(ctx);
  629. float t[6];
  630. nvgTransformTranslate(t, x,y);
  631. nvgTransformPremultiply(state->xform, t);
  632. }
  633. void nvgRotate(NVGcontext* ctx, float angle)
  634. {
  635. NVGstate* state = nvg__getState(ctx);
  636. float t[6];
  637. nvgTransformRotate(t, angle);
  638. nvgTransformPremultiply(state->xform, t);
  639. }
  640. void nvgSkewX(NVGcontext* ctx, float angle)
  641. {
  642. NVGstate* state = nvg__getState(ctx);
  643. float t[6];
  644. nvgTransformSkewX(t, angle);
  645. nvgTransformPremultiply(state->xform, t);
  646. }
  647. void nvgSkewY(NVGcontext* ctx, float angle)
  648. {
  649. NVGstate* state = nvg__getState(ctx);
  650. float t[6];
  651. nvgTransformSkewY(t, angle);
  652. nvgTransformPremultiply(state->xform, t);
  653. }
  654. void nvgScale(NVGcontext* ctx, float x, float y)
  655. {
  656. NVGstate* state = nvg__getState(ctx);
  657. float t[6];
  658. nvgTransformScale(t, x,y);
  659. nvgTransformPremultiply(state->xform, t);
  660. }
  661. void nvgCurrentTransform(NVGcontext* ctx, float* xform)
  662. {
  663. NVGstate* state = nvg__getState(ctx);
  664. if (xform == NULL) return;
  665. memcpy(xform, state->xform, sizeof(float)*6);
  666. }
  667. void nvgStrokeColor(NVGcontext* ctx, NVGcolor color)
  668. {
  669. NVGstate* state = nvg__getState(ctx);
  670. nvg__setPaintColor(&state->stroke, color);
  671. }
  672. void nvgStrokePaint(NVGcontext* ctx, NVGpaint paint)
  673. {
  674. NVGstate* state = nvg__getState(ctx);
  675. state->stroke = paint;
  676. nvgTransformMultiply(state->stroke.xform, state->xform);
  677. }
  678. void nvgFillColor(NVGcontext* ctx, NVGcolor color)
  679. {
  680. NVGstate* state = nvg__getState(ctx);
  681. nvg__setPaintColor(&state->fill, color);
  682. }
  683. void nvgFillPaint(NVGcontext* ctx, NVGpaint paint)
  684. {
  685. NVGstate* state = nvg__getState(ctx);
  686. state->fill = paint;
  687. nvgTransformMultiply(state->fill.xform, state->xform);
  688. }
  689. #ifndef NVG_NO_STB
  690. int nvgCreateImage(NVGcontext* ctx, const char* filename, int imageFlags)
  691. {
  692. int w, h, n, image;
  693. unsigned char* img;
  694. stbi_set_unpremultiply_on_load(1);
  695. stbi_convert_iphone_png_to_rgb(1);
  696. img = stbi_load(filename, &w, &h, &n, 4);
  697. if (img == NULL) {
  698. // printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
  699. return 0;
  700. }
  701. image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
  702. stbi_image_free(img);
  703. return image;
  704. }
  705. int nvgCreateImageMem(NVGcontext* ctx, int imageFlags, unsigned char* data, int ndata)
  706. {
  707. int w, h, n, image;
  708. unsigned char* img = stbi_load_from_memory(data, ndata, &w, &h, &n, 4);
  709. if (img == NULL) {
  710. // printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
  711. return 0;
  712. }
  713. image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
  714. stbi_image_free(img);
  715. return image;
  716. }
  717. #endif
  718. int nvgCreateImageRGBA(NVGcontext* ctx, int w, int h, int imageFlags, const unsigned char* data)
  719. {
  720. return ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_RGBA, w, h, imageFlags, data);
  721. }
  722. void nvgUpdateImage(NVGcontext* ctx, int image, const unsigned char* data)
  723. {
  724. int w, h;
  725. ctx->params.renderGetTextureSize(ctx->params.userPtr, image, &w, &h);
  726. ctx->params.renderUpdateTexture(ctx->params.userPtr, image, 0,0, w,h, data);
  727. }
  728. void nvgImageSize(NVGcontext* ctx, int image, int* w, int* h)
  729. {
  730. ctx->params.renderGetTextureSize(ctx->params.userPtr, image, w, h);
  731. }
  732. void nvgDeleteImage(NVGcontext* ctx, int image)
  733. {
  734. ctx->params.renderDeleteTexture(ctx->params.userPtr, image);
  735. }
  736. NVGpaint nvgLinearGradient(NVGcontext* ctx,
  737. float sx, float sy, float ex, float ey,
  738. NVGcolor icol, NVGcolor ocol)
  739. {
  740. NVGpaint p;
  741. float dx, dy, d;
  742. const float large = 1e5;
  743. NVG_NOTUSED(ctx);
  744. memset(&p, 0, sizeof(p));
  745. // Calculate transform aligned to the line
  746. dx = ex - sx;
  747. dy = ey - sy;
  748. d = sqrtf(dx*dx + dy*dy);
  749. if (d > 0.0001f) {
  750. dx /= d;
  751. dy /= d;
  752. } else {
  753. dx = 0;
  754. dy = 1;
  755. }
  756. p.xform[0] = dy; p.xform[1] = -dx;
  757. p.xform[2] = dx; p.xform[3] = dy;
  758. p.xform[4] = sx - dx*large; p.xform[5] = sy - dy*large;
  759. p.extent[0] = large;
  760. p.extent[1] = large + d*0.5f;
  761. p.radius = 0.0f;
  762. p.feather = nvg__maxf(1.0f, d);
  763. p.innerColor = icol;
  764. p.outerColor = ocol;
  765. return p;
  766. }
  767. NVGpaint nvgRadialGradient(NVGcontext* ctx,
  768. float cx, float cy, float inr, float outr,
  769. NVGcolor icol, NVGcolor ocol)
  770. {
  771. NVGpaint p;
  772. float r = (inr+outr)*0.5f;
  773. float f = (outr-inr);
  774. NVG_NOTUSED(ctx);
  775. memset(&p, 0, sizeof(p));
  776. nvgTransformIdentity(p.xform);
  777. p.xform[4] = cx;
  778. p.xform[5] = cy;
  779. p.extent[0] = r;
  780. p.extent[1] = r;
  781. p.radius = r;
  782. p.feather = nvg__maxf(1.0f, f);
  783. p.innerColor = icol;
  784. p.outerColor = ocol;
  785. return p;
  786. }
  787. NVGpaint nvgBoxGradient(NVGcontext* ctx,
  788. float x, float y, float w, float h, float r, float f,
  789. NVGcolor icol, NVGcolor ocol)
  790. {
  791. NVGpaint p;
  792. NVG_NOTUSED(ctx);
  793. memset(&p, 0, sizeof(p));
  794. nvgTransformIdentity(p.xform);
  795. p.xform[4] = x+w*0.5f;
  796. p.xform[5] = y+h*0.5f;
  797. p.extent[0] = w*0.5f;
  798. p.extent[1] = h*0.5f;
  799. p.radius = r;
  800. p.feather = nvg__maxf(1.0f, f);
  801. p.innerColor = icol;
  802. p.outerColor = ocol;
  803. return p;
  804. }
  805. NVGpaint nvgImagePattern(NVGcontext* ctx,
  806. float cx, float cy, float w, float h, float angle,
  807. int image, float alpha)
  808. {
  809. NVGpaint p;
  810. NVG_NOTUSED(ctx);
  811. memset(&p, 0, sizeof(p));
  812. nvgTransformRotate(p.xform, angle);
  813. p.xform[4] = cx;
  814. p.xform[5] = cy;
  815. p.extent[0] = w;
  816. p.extent[1] = h;
  817. p.image = image;
  818. p.innerColor = p.outerColor = nvgRGBAf(1,1,1,alpha);
  819. return p;
  820. }
  821. // Scissoring
  822. void nvgScissor(NVGcontext* ctx, float x, float y, float w, float h)
  823. {
  824. NVGstate* state = nvg__getState(ctx);
  825. w = nvg__maxf(0.0f, w);
  826. h = nvg__maxf(0.0f, h);
  827. nvgTransformIdentity(state->scissor.xform);
  828. state->scissor.xform[4] = x+w*0.5f;
  829. state->scissor.xform[5] = y+h*0.5f;
  830. nvgTransformMultiply(state->scissor.xform, state->xform);
  831. state->scissor.extent[0] = w*0.5f;
  832. state->scissor.extent[1] = h*0.5f;
  833. }
  834. static void nvg__isectRects(float* dst,
  835. float ax, float ay, float aw, float ah,
  836. float bx, float by, float bw, float bh)
  837. {
  838. float minx = nvg__maxf(ax, bx);
  839. float miny = nvg__maxf(ay, by);
  840. float maxx = nvg__minf(ax+aw, bx+bw);
  841. float maxy = nvg__minf(ay+ah, by+bh);
  842. dst[0] = minx;
  843. dst[1] = miny;
  844. dst[2] = nvg__maxf(0.0f, maxx - minx);
  845. dst[3] = nvg__maxf(0.0f, maxy - miny);
  846. }
  847. void nvgIntersectScissor(NVGcontext* ctx, float x, float y, float w, float h)
  848. {
  849. NVGstate* state = nvg__getState(ctx);
  850. float pxform[6], invxorm[6];
  851. float rect[4];
  852. float ex, ey, tex, tey;
  853. // If no previous scissor has been set, set the scissor as current scissor.
  854. if (state->scissor.extent[0] < 0) {
  855. nvgScissor(ctx, x, y, w, h);
  856. return;
  857. }
  858. // Transform the current scissor rect into current transform space.
  859. // If there is difference in rotation, this will be approximation.
  860. memcpy(pxform, state->scissor.xform, sizeof(float)*6);
  861. ex = state->scissor.extent[0];
  862. ey = state->scissor.extent[1];
  863. nvgTransformInverse(invxorm, state->xform);
  864. nvgTransformMultiply(pxform, invxorm);
  865. tex = ex*nvg__absf(pxform[0]) + ey*nvg__absf(pxform[2]);
  866. tey = ex*nvg__absf(pxform[1]) + ey*nvg__absf(pxform[3]);
  867. // Intersect rects.
  868. nvg__isectRects(rect, pxform[4]-tex,pxform[5]-tey,tex*2,tey*2, x,y,w,h);
  869. nvgScissor(ctx, rect[0], rect[1], rect[2], rect[3]);
  870. }
  871. void nvgResetScissor(NVGcontext* ctx)
  872. {
  873. NVGstate* state = nvg__getState(ctx);
  874. memset(state->scissor.xform, 0, sizeof(state->scissor.xform));
  875. state->scissor.extent[0] = -1.0f;
  876. state->scissor.extent[1] = -1.0f;
  877. }
  878. // Global composite operation.
  879. void nvgGlobalCompositeOperation(NVGcontext* ctx, int op)
  880. {
  881. NVGstate* state = nvg__getState(ctx);
  882. state->compositeOperation = nvg__compositeOperationState(op);
  883. }
  884. void nvgGlobalCompositeBlendFunc(NVGcontext* ctx, int sfactor, int dfactor)
  885. {
  886. nvgGlobalCompositeBlendFuncSeparate(ctx, sfactor, dfactor, sfactor, dfactor);
  887. }
  888. void nvgGlobalCompositeBlendFuncSeparate(NVGcontext* ctx, int srcRGB, int dstRGB, int srcAlpha, int dstAlpha)
  889. {
  890. NVGcompositeOperationState op;
  891. op.srcRGB = srcRGB;
  892. op.dstRGB = dstRGB;
  893. op.srcAlpha = srcAlpha;
  894. op.dstAlpha = dstAlpha;
  895. NVGstate* state = nvg__getState(ctx);
  896. state->compositeOperation = op;
  897. }
  898. static int nvg__ptEquals(float x1, float y1, float x2, float y2, float tol)
  899. {
  900. float dx = x2 - x1;
  901. float dy = y2 - y1;
  902. return dx*dx + dy*dy < tol*tol;
  903. }
  904. static float nvg__distPtSeg(float x, float y, float px, float py, float qx, float qy)
  905. {
  906. float pqx, pqy, dx, dy, d, t;
  907. pqx = qx-px;
  908. pqy = qy-py;
  909. dx = x-px;
  910. dy = y-py;
  911. d = pqx*pqx + pqy*pqy;
  912. t = pqx*dx + pqy*dy;
  913. if (d > 0) t /= d;
  914. if (t < 0) t = 0;
  915. else if (t > 1) t = 1;
  916. dx = px + t*pqx - x;
  917. dy = py + t*pqy - y;
  918. return dx*dx + dy*dy;
  919. }
  920. static void nvg__appendCommands(NVGcontext* ctx, float* vals, int nvals)
  921. {
  922. NVGstate* state = nvg__getState(ctx);
  923. int i;
  924. if (ctx->ncommands+nvals > ctx->ccommands) {
  925. float* commands;
  926. int ccommands = ctx->ncommands+nvals + ctx->ccommands/2;
  927. commands = (float*)realloc(ctx->commands, sizeof(float)*ccommands);
  928. if (commands == NULL) return;
  929. ctx->commands = commands;
  930. ctx->ccommands = ccommands;
  931. }
  932. if ((int)vals[0] != NVG_CLOSE && (int)vals[0] != NVG_WINDING) {
  933. ctx->commandx = vals[nvals-2];
  934. ctx->commandy = vals[nvals-1];
  935. }
  936. // transform commands
  937. i = 0;
  938. while (i < nvals) {
  939. int cmd = (int)vals[i];
  940. switch (cmd) {
  941. case NVG_MOVETO:
  942. nvgTransformPoint(&vals[i+1],&vals[i+2], state->xform, vals[i+1],vals[i+2]);
  943. i += 3;
  944. break;
  945. case NVG_LINETO:
  946. nvgTransformPoint(&vals[i+1],&vals[i+2], state->xform, vals[i+1],vals[i+2]);
  947. i += 3;
  948. break;
  949. case NVG_BEZIERTO:
  950. nvgTransformPoint(&vals[i+1],&vals[i+2], state->xform, vals[i+1],vals[i+2]);
  951. nvgTransformPoint(&vals[i+3],&vals[i+4], state->xform, vals[i+3],vals[i+4]);
  952. nvgTransformPoint(&vals[i+5],&vals[i+6], state->xform, vals[i+5],vals[i+6]);
  953. i += 7;
  954. break;
  955. case NVG_CLOSE:
  956. i++;
  957. break;
  958. case NVG_WINDING:
  959. i += 2;
  960. break;
  961. default:
  962. i++;
  963. }
  964. }
  965. memcpy(&ctx->commands[ctx->ncommands], vals, nvals*sizeof(float));
  966. ctx->ncommands += nvals;
  967. }
  968. static void nvg__clearPathCache(NVGcontext* ctx)
  969. {
  970. ctx->cache->npoints = 0;
  971. ctx->cache->npaths = 0;
  972. }
  973. static NVGpath* nvg__lastPath(NVGcontext* ctx)
  974. {
  975. if (ctx->cache->npaths > 0)
  976. return &ctx->cache->paths[ctx->cache->npaths-1];
  977. return NULL;
  978. }
  979. static void nvg__addPath(NVGcontext* ctx)
  980. {
  981. NVGpath* path;
  982. if (ctx->cache->npaths+1 > ctx->cache->cpaths) {
  983. NVGpath* paths;
  984. int cpaths = ctx->cache->npaths+1 + ctx->cache->cpaths/2;
  985. paths = (NVGpath*)realloc(ctx->cache->paths, sizeof(NVGpath)*cpaths);
  986. if (paths == NULL) return;
  987. ctx->cache->paths = paths;
  988. ctx->cache->cpaths = cpaths;
  989. }
  990. path = &ctx->cache->paths[ctx->cache->npaths];
  991. memset(path, 0, sizeof(*path));
  992. path->first = ctx->cache->npoints;
  993. path->winding = NVG_CCW;
  994. ctx->cache->npaths++;
  995. }
  996. static NVGpoint* nvg__lastPoint(NVGcontext* ctx)
  997. {
  998. if (ctx->cache->npoints > 0)
  999. return &ctx->cache->points[ctx->cache->npoints-1];
  1000. return NULL;
  1001. }
  1002. static void nvg__addPoint(NVGcontext* ctx, float x, float y, int flags)
  1003. {
  1004. NVGpath* path = nvg__lastPath(ctx);
  1005. NVGpoint* pt;
  1006. if (path == NULL) return;
  1007. if (path->count > 0 && ctx->cache->npoints > 0) {
  1008. pt = nvg__lastPoint(ctx);
  1009. if (nvg__ptEquals(pt->x,pt->y, x,y, ctx->distTol)) {
  1010. pt->flags |= flags;
  1011. return;
  1012. }
  1013. }
  1014. if (ctx->cache->npoints+1 > ctx->cache->cpoints) {
  1015. NVGpoint* points;
  1016. int cpoints = ctx->cache->npoints+1 + ctx->cache->cpoints/2;
  1017. points = (NVGpoint*)realloc(ctx->cache->points, sizeof(NVGpoint)*cpoints);
  1018. if (points == NULL) return;
  1019. ctx->cache->points = points;
  1020. ctx->cache->cpoints = cpoints;
  1021. }
  1022. pt = &ctx->cache->points[ctx->cache->npoints];
  1023. memset(pt, 0, sizeof(*pt));
  1024. pt->x = x;
  1025. pt->y = y;
  1026. pt->flags = (unsigned char)flags;
  1027. ctx->cache->npoints++;
  1028. path->count++;
  1029. }
  1030. static void nvg__closePath(NVGcontext* ctx)
  1031. {
  1032. NVGpath* path = nvg__lastPath(ctx);
  1033. if (path == NULL) return;
  1034. path->closed = 1;
  1035. }
  1036. static void nvg__pathWinding(NVGcontext* ctx, int winding)
  1037. {
  1038. NVGpath* path = nvg__lastPath(ctx);
  1039. if (path == NULL) return;
  1040. path->winding = winding;
  1041. }
  1042. static float nvg__getAverageScale(float *t)
  1043. {
  1044. float sx = sqrtf(t[0]*t[0] + t[2]*t[2]);
  1045. float sy = sqrtf(t[1]*t[1] + t[3]*t[3]);
  1046. return (sx + sy) * 0.5f;
  1047. }
  1048. static NVGvertex* nvg__allocTempVerts(NVGcontext* ctx, int nverts)
  1049. {
  1050. if (nverts > ctx->cache->cverts) {
  1051. NVGvertex* verts;
  1052. int cverts = (nverts + 0xff) & ~0xff; // Round up to prevent allocations when things change just slightly.
  1053. verts = (NVGvertex*)realloc(ctx->cache->verts, sizeof(NVGvertex)*cverts);
  1054. if (verts == NULL) return NULL;
  1055. ctx->cache->verts = verts;
  1056. ctx->cache->cverts = cverts;
  1057. }
  1058. return ctx->cache->verts;
  1059. }
  1060. static float nvg__triarea2(float ax, float ay, float bx, float by, float cx, float cy)
  1061. {
  1062. float abx = bx - ax;
  1063. float aby = by - ay;
  1064. float acx = cx - ax;
  1065. float acy = cy - ay;
  1066. return acx*aby - abx*acy;
  1067. }
  1068. static float nvg__polyArea(NVGpoint* pts, int npts)
  1069. {
  1070. int i;
  1071. float area = 0;
  1072. for (i = 2; i < npts; i++) {
  1073. NVGpoint* a = &pts[0];
  1074. NVGpoint* b = &pts[i-1];
  1075. NVGpoint* c = &pts[i];
  1076. area += nvg__triarea2(a->x,a->y, b->x,b->y, c->x,c->y);
  1077. }
  1078. return area * 0.5f;
  1079. }
  1080. static void nvg__polyReverse(NVGpoint* pts, int npts)
  1081. {
  1082. NVGpoint tmp;
  1083. int i = 0, j = npts-1;
  1084. while (i < j) {
  1085. tmp = pts[i];
  1086. pts[i] = pts[j];
  1087. pts[j] = tmp;
  1088. i++;
  1089. j--;
  1090. }
  1091. }
  1092. static void nvg__vset(NVGvertex* vtx, float x, float y, float u, float v)
  1093. {
  1094. vtx->x = x;
  1095. vtx->y = y;
  1096. vtx->u = u;
  1097. vtx->v = v;
  1098. }
  1099. static void nvg__tesselateBezier(NVGcontext* ctx,
  1100. float x1, float y1, float x2, float y2,
  1101. float x3, float y3, float x4, float y4,
  1102. int level, int type)
  1103. {
  1104. float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234;
  1105. float dx,dy,d2,d3;
  1106. if (level > 10) return;
  1107. x12 = (x1+x2)*0.5f;
  1108. y12 = (y1+y2)*0.5f;
  1109. x23 = (x2+x3)*0.5f;
  1110. y23 = (y2+y3)*0.5f;
  1111. x34 = (x3+x4)*0.5f;
  1112. y34 = (y3+y4)*0.5f;
  1113. x123 = (x12+x23)*0.5f;
  1114. y123 = (y12+y23)*0.5f;
  1115. dx = x4 - x1;
  1116. dy = y4 - y1;
  1117. d2 = nvg__absf(((x2 - x4) * dy - (y2 - y4) * dx));
  1118. d3 = nvg__absf(((x3 - x4) * dy - (y3 - y4) * dx));
  1119. if ((d2 + d3)*(d2 + d3) < ctx->tessTol * (dx*dx + dy*dy)) {
  1120. nvg__addPoint(ctx, x4, y4, type);
  1121. return;
  1122. }
  1123. /* 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) {
  1124. nvg__addPoint(ctx, x4, y4, type);
  1125. return;
  1126. }*/
  1127. x234 = (x23+x34)*0.5f;
  1128. y234 = (y23+y34)*0.5f;
  1129. x1234 = (x123+x234)*0.5f;
  1130. y1234 = (y123+y234)*0.5f;
  1131. nvg__tesselateBezier(ctx, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1, 0);
  1132. nvg__tesselateBezier(ctx, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1, type);
  1133. }
  1134. static void nvg__flattenPaths(NVGcontext* ctx)
  1135. {
  1136. NVGpathCache* cache = ctx->cache;
  1137. // NVGstate* state = nvg__getState(ctx);
  1138. NVGpoint* last;
  1139. NVGpoint* p0;
  1140. NVGpoint* p1;
  1141. NVGpoint* pts;
  1142. NVGpath* path;
  1143. int i, j;
  1144. float* cp1;
  1145. float* cp2;
  1146. float* p;
  1147. float area;
  1148. if (cache->npaths > 0)
  1149. return;
  1150. // Flatten
  1151. i = 0;
  1152. while (i < ctx->ncommands) {
  1153. int cmd = (int)ctx->commands[i];
  1154. switch (cmd) {
  1155. case NVG_MOVETO:
  1156. nvg__addPath(ctx);
  1157. p = &ctx->commands[i+1];
  1158. nvg__addPoint(ctx, p[0], p[1], NVG_PT_CORNER);
  1159. i += 3;
  1160. break;
  1161. case NVG_LINETO:
  1162. p = &ctx->commands[i+1];
  1163. nvg__addPoint(ctx, p[0], p[1], NVG_PT_CORNER);
  1164. i += 3;
  1165. break;
  1166. case NVG_BEZIERTO:
  1167. last = nvg__lastPoint(ctx);
  1168. if (last != NULL) {
  1169. cp1 = &ctx->commands[i+1];
  1170. cp2 = &ctx->commands[i+3];
  1171. p = &ctx->commands[i+5];
  1172. nvg__tesselateBezier(ctx, last->x,last->y, cp1[0],cp1[1], cp2[0],cp2[1], p[0],p[1], 0, NVG_PT_CORNER);
  1173. }
  1174. i += 7;
  1175. break;
  1176. case NVG_CLOSE:
  1177. nvg__closePath(ctx);
  1178. i++;
  1179. break;
  1180. case NVG_WINDING:
  1181. nvg__pathWinding(ctx, (int)ctx->commands[i+1]);
  1182. i += 2;
  1183. break;
  1184. default:
  1185. i++;
  1186. }
  1187. }
  1188. cache->bounds[0] = cache->bounds[1] = 1e6f;
  1189. cache->bounds[2] = cache->bounds[3] = -1e6f;
  1190. // Calculate the direction and length of line segments.
  1191. for (j = 0; j < cache->npaths; j++) {
  1192. path = &cache->paths[j];
  1193. pts = &cache->points[path->first];
  1194. // If the first and last points are the same, remove the last, mark as closed path.
  1195. p0 = &pts[path->count-1];
  1196. p1 = &pts[0];
  1197. if (nvg__ptEquals(p0->x,p0->y, p1->x,p1->y, ctx->distTol)) {
  1198. path->count--;
  1199. p0 = &pts[path->count-1];
  1200. path->closed = 1;
  1201. }
  1202. // Enforce winding.
  1203. if (path->count > 2) {
  1204. area = nvg__polyArea(pts, path->count);
  1205. if (path->winding == NVG_CCW && area < 0.0f)
  1206. nvg__polyReverse(pts, path->count);
  1207. if (path->winding == NVG_CW && area > 0.0f)
  1208. nvg__polyReverse(pts, path->count);
  1209. }
  1210. for(i = 0; i < path->count; i++) {
  1211. // Calculate segment direction and length
  1212. p0->dx = p1->x - p0->x;
  1213. p0->dy = p1->y - p0->y;
  1214. p0->len = nvg__normalize(&p0->dx, &p0->dy);
  1215. // Update bounds
  1216. cache->bounds[0] = nvg__minf(cache->bounds[0], p0->x);
  1217. cache->bounds[1] = nvg__minf(cache->bounds[1], p0->y);
  1218. cache->bounds[2] = nvg__maxf(cache->bounds[2], p0->x);
  1219. cache->bounds[3] = nvg__maxf(cache->bounds[3], p0->y);
  1220. // Advance
  1221. p0 = p1++;
  1222. }
  1223. }
  1224. }
  1225. static int nvg__curveDivs(float r, float arc, float tol)
  1226. {
  1227. float da = acosf(r / (r + tol)) * 2.0f;
  1228. return nvg__maxi(2, (int)ceilf(arc / da));
  1229. }
  1230. static void nvg__chooseBevel(int bevel, NVGpoint* p0, NVGpoint* p1, float w,
  1231. float* x0, float* y0, float* x1, float* y1)
  1232. {
  1233. if (bevel) {
  1234. *x0 = p1->x + p0->dy * w;
  1235. *y0 = p1->y - p0->dx * w;
  1236. *x1 = p1->x + p1->dy * w;
  1237. *y1 = p1->y - p1->dx * w;
  1238. } else {
  1239. *x0 = p1->x + p1->dmx * w;
  1240. *y0 = p1->y + p1->dmy * w;
  1241. *x1 = p1->x + p1->dmx * w;
  1242. *y1 = p1->y + p1->dmy * w;
  1243. }
  1244. }
  1245. static NVGvertex* nvg__roundJoin(NVGvertex* dst, NVGpoint* p0, NVGpoint* p1,
  1246. float lw, float rw, float lu, float ru, int ncap,
  1247. float fringe)
  1248. {
  1249. int i, n;
  1250. float dlx0 = p0->dy;
  1251. float dly0 = -p0->dx;
  1252. float dlx1 = p1->dy;
  1253. float dly1 = -p1->dx;
  1254. NVG_NOTUSED(fringe);
  1255. if (p1->flags & NVG_PT_LEFT) {
  1256. float lx0,ly0,lx1,ly1,a0,a1;
  1257. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, lw, &lx0,&ly0, &lx1,&ly1);
  1258. a0 = atan2f(-dly0, -dlx0);
  1259. a1 = atan2f(-dly1, -dlx1);
  1260. if (a1 > a0) a1 -= NVG_PI*2;
  1261. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1262. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1263. n = nvg__clampi((int)ceilf(((a0 - a1) / NVG_PI) * ncap), 2, ncap);
  1264. for (i = 0; i < n; i++) {
  1265. float u = i/(float)(n-1);
  1266. float a = a0 + u*(a1-a0);
  1267. float rx = p1->x + cosf(a) * rw;
  1268. float ry = p1->y + sinf(a) * rw;
  1269. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1270. nvg__vset(dst, rx, ry, ru,1); dst++;
  1271. }
  1272. nvg__vset(dst, lx1, ly1, lu,1); dst++;
  1273. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1274. } else {
  1275. float rx0,ry0,rx1,ry1,a0,a1;
  1276. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, -rw, &rx0,&ry0, &rx1,&ry1);
  1277. a0 = atan2f(dly0, dlx0);
  1278. a1 = atan2f(dly1, dlx1);
  1279. if (a1 < a0) a1 += NVG_PI*2;
  1280. nvg__vset(dst, p1->x + dlx0*rw, p1->y + dly0*rw, lu,1); dst++;
  1281. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1282. n = nvg__clampi((int)ceilf(((a1 - a0) / NVG_PI) * ncap), 2, ncap);
  1283. for (i = 0; i < n; i++) {
  1284. float u = i/(float)(n-1);
  1285. float a = a0 + u*(a1-a0);
  1286. float lx = p1->x + cosf(a) * lw;
  1287. float ly = p1->y + sinf(a) * lw;
  1288. nvg__vset(dst, lx, ly, lu,1); dst++;
  1289. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1290. }
  1291. nvg__vset(dst, p1->x + dlx1*rw, p1->y + dly1*rw, lu,1); dst++;
  1292. nvg__vset(dst, rx1, ry1, ru,1); dst++;
  1293. }
  1294. return dst;
  1295. }
  1296. static NVGvertex* nvg__bevelJoin(NVGvertex* dst, NVGpoint* p0, NVGpoint* p1,
  1297. float lw, float rw, float lu, float ru, float fringe)
  1298. {
  1299. float rx0,ry0,rx1,ry1;
  1300. float lx0,ly0,lx1,ly1;
  1301. float dlx0 = p0->dy;
  1302. float dly0 = -p0->dx;
  1303. float dlx1 = p1->dy;
  1304. float dly1 = -p1->dx;
  1305. NVG_NOTUSED(fringe);
  1306. if (p1->flags & NVG_PT_LEFT) {
  1307. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, lw, &lx0,&ly0, &lx1,&ly1);
  1308. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1309. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1310. if (p1->flags & NVG_PT_BEVEL) {
  1311. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1312. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1313. nvg__vset(dst, lx1, ly1, lu,1); dst++;
  1314. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1315. } else {
  1316. rx0 = p1->x - p1->dmx * rw;
  1317. ry0 = p1->y - p1->dmy * rw;
  1318. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1319. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1320. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1321. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1322. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1323. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1324. }
  1325. nvg__vset(dst, lx1, ly1, lu,1); dst++;
  1326. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1327. } else {
  1328. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, -rw, &rx0,&ry0, &rx1,&ry1);
  1329. nvg__vset(dst, p1->x + dlx0*lw, p1->y + dly0*lw, lu,1); dst++;
  1330. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1331. if (p1->flags & NVG_PT_BEVEL) {
  1332. nvg__vset(dst, p1->x + dlx0*lw, p1->y + dly0*lw, lu,1); dst++;
  1333. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1334. nvg__vset(dst, p1->x + dlx1*lw, p1->y + dly1*lw, lu,1); dst++;
  1335. nvg__vset(dst, rx1, ry1, ru,1); dst++;
  1336. } else {
  1337. lx0 = p1->x + p1->dmx * lw;
  1338. ly0 = p1->y + p1->dmy * lw;
  1339. nvg__vset(dst, p1->x + dlx0*lw, p1->y + dly0*lw, lu,1); dst++;
  1340. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1341. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1342. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1343. nvg__vset(dst, p1->x + dlx1*lw, p1->y + dly1*lw, lu,1); dst++;
  1344. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1345. }
  1346. nvg__vset(dst, p1->x + dlx1*lw, p1->y + dly1*lw, lu,1); dst++;
  1347. nvg__vset(dst, rx1, ry1, ru,1); dst++;
  1348. }
  1349. return dst;
  1350. }
  1351. static NVGvertex* nvg__buttCapStart(NVGvertex* dst, NVGpoint* p,
  1352. float dx, float dy, float w, float d,
  1353. float aa, float u0, float u1)
  1354. {
  1355. float px = p->x - dx*d;
  1356. float py = p->y - dy*d;
  1357. float dlx = dy;
  1358. float dly = -dx;
  1359. nvg__vset(dst, px + dlx*w - dx*aa, py + dly*w - dy*aa, u0,0); dst++;
  1360. nvg__vset(dst, px - dlx*w - dx*aa, py - dly*w - dy*aa, u1,0); dst++;
  1361. nvg__vset(dst, px + dlx*w, py + dly*w, u0,1); dst++;
  1362. nvg__vset(dst, px - dlx*w, py - dly*w, u1,1); dst++;
  1363. return dst;
  1364. }
  1365. static NVGvertex* nvg__buttCapEnd(NVGvertex* dst, NVGpoint* p,
  1366. float dx, float dy, float w, float d,
  1367. float aa, float u0, float u1)
  1368. {
  1369. float px = p->x + dx*d;
  1370. float py = p->y + dy*d;
  1371. float dlx = dy;
  1372. float dly = -dx;
  1373. nvg__vset(dst, px + dlx*w, py + dly*w, u0,1); dst++;
  1374. nvg__vset(dst, px - dlx*w, py - dly*w, u1,1); dst++;
  1375. nvg__vset(dst, px + dlx*w + dx*aa, py + dly*w + dy*aa, u0,0); dst++;
  1376. nvg__vset(dst, px - dlx*w + dx*aa, py - dly*w + dy*aa, u1,0); dst++;
  1377. return dst;
  1378. }
  1379. static NVGvertex* nvg__roundCapStart(NVGvertex* dst, NVGpoint* p,
  1380. float dx, float dy, float w, int ncap,
  1381. float aa, float u0, float u1)
  1382. {
  1383. int i;
  1384. float px = p->x;
  1385. float py = p->y;
  1386. float dlx = dy;
  1387. float dly = -dx;
  1388. NVG_NOTUSED(aa);
  1389. for (i = 0; i < ncap; i++) {
  1390. float a = i/(float)(ncap-1)*NVG_PI;
  1391. float ax = cosf(a) * w, ay = sinf(a) * w;
  1392. nvg__vset(dst, px - dlx*ax - dx*ay, py - dly*ax - dy*ay, u0,1); dst++;
  1393. nvg__vset(dst, px, py, 0.5f,1); dst++;
  1394. }
  1395. nvg__vset(dst, px + dlx*w, py + dly*w, u0,1); dst++;
  1396. nvg__vset(dst, px - dlx*w, py - dly*w, u1,1); dst++;
  1397. return dst;
  1398. }
  1399. static NVGvertex* nvg__roundCapEnd(NVGvertex* dst, NVGpoint* p,
  1400. float dx, float dy, float w, int ncap,
  1401. float aa, float u0, float u1)
  1402. {
  1403. int i;
  1404. float px = p->x;
  1405. float py = p->y;
  1406. float dlx = dy;
  1407. float dly = -dx;
  1408. NVG_NOTUSED(aa);
  1409. nvg__vset(dst, px + dlx*w, py + dly*w, u0,1); dst++;
  1410. nvg__vset(dst, px - dlx*w, py - dly*w, u1,1); dst++;
  1411. for (i = 0; i < ncap; i++) {
  1412. float a = i/(float)(ncap-1)*NVG_PI;
  1413. float ax = cosf(a) * w, ay = sinf(a) * w;
  1414. nvg__vset(dst, px, py, 0.5f,1); dst++;
  1415. nvg__vset(dst, px - dlx*ax + dx*ay, py - dly*ax + dy*ay, u0,1); dst++;
  1416. }
  1417. return dst;
  1418. }
  1419. static void nvg__calculateJoins(NVGcontext* ctx, float w, int lineJoin, float miterLimit)
  1420. {
  1421. NVGpathCache* cache = ctx->cache;
  1422. int i, j;
  1423. float iw = 0.0f;
  1424. if (w > 0.0f) iw = 1.0f / w;
  1425. // Calculate which joins needs extra vertices to append, and gather vertex count.
  1426. for (i = 0; i < cache->npaths; i++) {
  1427. NVGpath* path = &cache->paths[i];
  1428. NVGpoint* pts = &cache->points[path->first];
  1429. NVGpoint* p0 = &pts[path->count-1];
  1430. NVGpoint* p1 = &pts[0];
  1431. int nleft = 0;
  1432. path->nbevel = 0;
  1433. for (j = 0; j < path->count; j++) {
  1434. float dlx0, dly0, dlx1, dly1, dmr2, cross, limit;
  1435. dlx0 = p0->dy;
  1436. dly0 = -p0->dx;
  1437. dlx1 = p1->dy;
  1438. dly1 = -p1->dx;
  1439. // Calculate extrusions
  1440. p1->dmx = (dlx0 + dlx1) * 0.5f;
  1441. p1->dmy = (dly0 + dly1) * 0.5f;
  1442. dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy;
  1443. if (dmr2 > 0.000001f) {
  1444. float scale = 1.0f / dmr2;
  1445. if (scale > 600.0f) {
  1446. scale = 600.0f;
  1447. }
  1448. p1->dmx *= scale;
  1449. p1->dmy *= scale;
  1450. }
  1451. // Clear flags, but keep the corner.
  1452. p1->flags = (p1->flags & NVG_PT_CORNER) ? NVG_PT_CORNER : 0;
  1453. // Keep track of left turns.
  1454. cross = p1->dx * p0->dy - p0->dx * p1->dy;
  1455. if (cross > 0.0f) {
  1456. nleft++;
  1457. p1->flags |= NVG_PT_LEFT;
  1458. }
  1459. // Calculate if we should use bevel or miter for inner join.
  1460. limit = nvg__maxf(1.01f, nvg__minf(p0->len, p1->len) * iw);
  1461. if ((dmr2 * limit*limit) < 1.0f)
  1462. p1->flags |= NVG_PR_INNERBEVEL;
  1463. // Check to see if the corner needs to be beveled.
  1464. if (p1->flags & NVG_PT_CORNER) {
  1465. if ((dmr2 * miterLimit*miterLimit) < 1.0f || lineJoin == NVG_BEVEL || lineJoin == NVG_ROUND) {
  1466. p1->flags |= NVG_PT_BEVEL;
  1467. }
  1468. }
  1469. if ((p1->flags & (NVG_PT_BEVEL | NVG_PR_INNERBEVEL)) != 0)
  1470. path->nbevel++;
  1471. p0 = p1++;
  1472. }
  1473. path->convex = (nleft == path->count) ? 1 : 0;
  1474. }
  1475. }
  1476. static int nvg__expandStroke(NVGcontext* ctx, float w, float fringe, int lineCap, int lineJoin, float miterLimit)
  1477. {
  1478. NVGpathCache* cache = ctx->cache;
  1479. NVGvertex* verts;
  1480. NVGvertex* dst;
  1481. int cverts, i, j;
  1482. float aa = fringe;//ctx->fringeWidth;
  1483. float u0 = 0.0f, u1 = 1.0f;
  1484. int ncap = nvg__curveDivs(w, NVG_PI, ctx->tessTol); // Calculate divisions per half circle.
  1485. w += aa * 0.5f;
  1486. // Disable the gradient used for antialiasing when antialiasing is not used.
  1487. if (aa == 0.0f) {
  1488. u0 = 0.5f;
  1489. u1 = 0.5f;
  1490. }
  1491. nvg__calculateJoins(ctx, w, lineJoin, miterLimit);
  1492. // Calculate max vertex usage.
  1493. cverts = 0;
  1494. for (i = 0; i < cache->npaths; i++) {
  1495. NVGpath* path = &cache->paths[i];
  1496. int loop = (path->closed == 0) ? 0 : 1;
  1497. if (lineJoin == NVG_ROUND)
  1498. cverts += (path->count + path->nbevel*(ncap+2) + 1) * 2; // plus one for loop
  1499. else
  1500. cverts += (path->count + path->nbevel*5 + 1) * 2; // plus one for loop
  1501. if (loop == 0) {
  1502. // space for caps
  1503. if (lineCap == NVG_ROUND) {
  1504. cverts += (ncap*2 + 2)*2;
  1505. } else {
  1506. cverts += (3+3)*2;
  1507. }
  1508. }
  1509. }
  1510. verts = nvg__allocTempVerts(ctx, cverts);
  1511. if (verts == NULL) return 0;
  1512. for (i = 0; i < cache->npaths; i++) {
  1513. NVGpath* path = &cache->paths[i];
  1514. NVGpoint* pts = &cache->points[path->first];
  1515. NVGpoint* p0;
  1516. NVGpoint* p1;
  1517. int s, e, loop;
  1518. float dx, dy;
  1519. path->fill = 0;
  1520. path->nfill = 0;
  1521. // Calculate fringe or stroke
  1522. loop = (path->closed == 0) ? 0 : 1;
  1523. dst = verts;
  1524. path->stroke = dst;
  1525. if (loop) {
  1526. // Looping
  1527. p0 = &pts[path->count-1];
  1528. p1 = &pts[0];
  1529. s = 0;
  1530. e = path->count;
  1531. } else {
  1532. // Add cap
  1533. p0 = &pts[0];
  1534. p1 = &pts[1];
  1535. s = 1;
  1536. e = path->count-1;
  1537. }
  1538. if (loop == 0) {
  1539. // Add cap
  1540. dx = p1->x - p0->x;
  1541. dy = p1->y - p0->y;
  1542. nvg__normalize(&dx, &dy);
  1543. if (lineCap == NVG_BUTT)
  1544. dst = nvg__buttCapStart(dst, p0, dx, dy, w, -aa*0.5f, aa, u0, u1);
  1545. else if (lineCap == NVG_BUTT || lineCap == NVG_SQUARE)
  1546. dst = nvg__buttCapStart(dst, p0, dx, dy, w, w-aa, aa, u0, u1);
  1547. else if (lineCap == NVG_ROUND)
  1548. dst = nvg__roundCapStart(dst, p0, dx, dy, w, ncap, aa, u0, u1);
  1549. }
  1550. for (j = s; j < e; ++j) {
  1551. if ((p1->flags & (NVG_PT_BEVEL | NVG_PR_INNERBEVEL)) != 0) {
  1552. if (lineJoin == NVG_ROUND) {
  1553. dst = nvg__roundJoin(dst, p0, p1, w, w, u0, u1, ncap, aa);
  1554. } else {
  1555. dst = nvg__bevelJoin(dst, p0, p1, w, w, u0, u1, aa);
  1556. }
  1557. } else {
  1558. nvg__vset(dst, p1->x + (p1->dmx * w), p1->y + (p1->dmy * w), u0,1); dst++;
  1559. nvg__vset(dst, p1->x - (p1->dmx * w), p1->y - (p1->dmy * w), u1,1); dst++;
  1560. }
  1561. p0 = p1++;
  1562. }
  1563. if (loop) {
  1564. // Loop it
  1565. nvg__vset(dst, verts[0].x, verts[0].y, u0,1); dst++;
  1566. nvg__vset(dst, verts[1].x, verts[1].y, u1,1); dst++;
  1567. } else {
  1568. // Add cap
  1569. dx = p1->x - p0->x;
  1570. dy = p1->y - p0->y;
  1571. nvg__normalize(&dx, &dy);
  1572. if (lineCap == NVG_BUTT)
  1573. dst = nvg__buttCapEnd(dst, p1, dx, dy, w, -aa*0.5f, aa, u0, u1);
  1574. else if (lineCap == NVG_BUTT || lineCap == NVG_SQUARE)
  1575. dst = nvg__buttCapEnd(dst, p1, dx, dy, w, w-aa, aa, u0, u1);
  1576. else if (lineCap == NVG_ROUND)
  1577. dst = nvg__roundCapEnd(dst, p1, dx, dy, w, ncap, aa, u0, u1);
  1578. }
  1579. path->nstroke = (int)(dst - verts);
  1580. verts = dst;
  1581. }
  1582. return 1;
  1583. }
  1584. static int nvg__expandFill(NVGcontext* ctx, float w, int lineJoin, float miterLimit)
  1585. {
  1586. NVGpathCache* cache = ctx->cache;
  1587. NVGvertex* verts;
  1588. NVGvertex* dst;
  1589. int cverts, convex, i, j;
  1590. float aa = ctx->fringeWidth;
  1591. int fringe = w > 0.0f;
  1592. nvg__calculateJoins(ctx, w, lineJoin, miterLimit);
  1593. // Calculate max vertex usage.
  1594. cverts = 0;
  1595. for (i = 0; i < cache->npaths; i++) {
  1596. NVGpath* path = &cache->paths[i];
  1597. cverts += path->count + path->nbevel + 1;
  1598. if (fringe)
  1599. cverts += (path->count + path->nbevel*5 + 1) * 2; // plus one for loop
  1600. }
  1601. verts = nvg__allocTempVerts(ctx, cverts);
  1602. if (verts == NULL) return 0;
  1603. convex = cache->npaths == 1 && cache->paths[0].convex;
  1604. for (i = 0; i < cache->npaths; i++) {
  1605. NVGpath* path = &cache->paths[i];
  1606. NVGpoint* pts = &cache->points[path->first];
  1607. NVGpoint* p0;
  1608. NVGpoint* p1;
  1609. float rw, lw, woff;
  1610. float ru, lu;
  1611. // Calculate shape vertices.
  1612. woff = 0.5f*aa;
  1613. dst = verts;
  1614. path->fill = dst;
  1615. if (fringe) {
  1616. // Looping
  1617. p0 = &pts[path->count-1];
  1618. p1 = &pts[0];
  1619. for (j = 0; j < path->count; ++j) {
  1620. if (p1->flags & NVG_PT_BEVEL) {
  1621. float dlx0 = p0->dy;
  1622. float dly0 = -p0->dx;
  1623. float dlx1 = p1->dy;
  1624. float dly1 = -p1->dx;
  1625. if (p1->flags & NVG_PT_LEFT) {
  1626. float lx = p1->x + p1->dmx * woff;
  1627. float ly = p1->y + p1->dmy * woff;
  1628. nvg__vset(dst, lx, ly, 0.5f,1); dst++;
  1629. } else {
  1630. float lx0 = p1->x + dlx0 * woff;
  1631. float ly0 = p1->y + dly0 * woff;
  1632. float lx1 = p1->x + dlx1 * woff;
  1633. float ly1 = p1->y + dly1 * woff;
  1634. nvg__vset(dst, lx0, ly0, 0.5f,1); dst++;
  1635. nvg__vset(dst, lx1, ly1, 0.5f,1); dst++;
  1636. }
  1637. } else {
  1638. nvg__vset(dst, p1->x + (p1->dmx * woff), p1->y + (p1->dmy * woff), 0.5f,1); dst++;
  1639. }
  1640. p0 = p1++;
  1641. }
  1642. } else {
  1643. for (j = 0; j < path->count; ++j) {
  1644. nvg__vset(dst, pts[j].x, pts[j].y, 0.5f,1);
  1645. dst++;
  1646. }
  1647. }
  1648. path->nfill = (int)(dst - verts);
  1649. verts = dst;
  1650. // Calculate fringe
  1651. if (fringe) {
  1652. lw = w + woff;
  1653. rw = w - woff;
  1654. lu = 0;
  1655. ru = 1;
  1656. dst = verts;
  1657. path->stroke = dst;
  1658. // Create only half a fringe for convex shapes so that
  1659. // the shape can be rendered without stenciling.
  1660. if (convex) {
  1661. lw = woff; // This should generate the same vertex as fill inset above.
  1662. lu = 0.5f; // Set outline fade at middle.
  1663. }
  1664. // Looping
  1665. p0 = &pts[path->count-1];
  1666. p1 = &pts[0];
  1667. for (j = 0; j < path->count; ++j) {
  1668. if ((p1->flags & (NVG_PT_BEVEL | NVG_PR_INNERBEVEL)) != 0) {
  1669. dst = nvg__bevelJoin(dst, p0, p1, lw, rw, lu, ru, ctx->fringeWidth);
  1670. } else {
  1671. nvg__vset(dst, p1->x + (p1->dmx * lw), p1->y + (p1->dmy * lw), lu,1); dst++;
  1672. nvg__vset(dst, p1->x - (p1->dmx * rw), p1->y - (p1->dmy * rw), ru,1); dst++;
  1673. }
  1674. p0 = p1++;
  1675. }
  1676. // Loop it
  1677. nvg__vset(dst, verts[0].x, verts[0].y, lu,1); dst++;
  1678. nvg__vset(dst, verts[1].x, verts[1].y, ru,1); dst++;
  1679. path->nstroke = (int)(dst - verts);
  1680. verts = dst;
  1681. } else {
  1682. path->stroke = NULL;
  1683. path->nstroke = 0;
  1684. }
  1685. }
  1686. return 1;
  1687. }
  1688. // Draw
  1689. void nvgBeginPath(NVGcontext* ctx)
  1690. {
  1691. ctx->ncommands = 0;
  1692. nvg__clearPathCache(ctx);
  1693. }
  1694. void nvgMoveTo(NVGcontext* ctx, float x, float y)
  1695. {
  1696. float vals[] = { NVG_MOVETO, x, y };
  1697. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1698. }
  1699. void nvgLineTo(NVGcontext* ctx, float x, float y)
  1700. {
  1701. float vals[] = { NVG_LINETO, x, y };
  1702. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1703. }
  1704. void nvgBezierTo(NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y)
  1705. {
  1706. float vals[] = { NVG_BEZIERTO, c1x, c1y, c2x, c2y, x, y };
  1707. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1708. }
  1709. void nvgQuadTo(NVGcontext* ctx, float cx, float cy, float x, float y)
  1710. {
  1711. float x0 = ctx->commandx;
  1712. float y0 = ctx->commandy;
  1713. float vals[] = { NVG_BEZIERTO,
  1714. x0 + 2.0f/3.0f*(cx - x0), y0 + 2.0f/3.0f*(cy - y0),
  1715. x + 2.0f/3.0f*(cx - x), y + 2.0f/3.0f*(cy - y),
  1716. x, y };
  1717. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1718. }
  1719. void nvgArcTo(NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius)
  1720. {
  1721. float x0 = ctx->commandx;
  1722. float y0 = ctx->commandy;
  1723. float dx0,dy0, dx1,dy1, a, d, cx,cy, a0,a1;
  1724. int dir;
  1725. if (ctx->ncommands == 0) {
  1726. return;
  1727. }
  1728. // Handle degenerate cases.
  1729. if (nvg__ptEquals(x0,y0, x1,y1, ctx->distTol) ||
  1730. nvg__ptEquals(x1,y1, x2,y2, ctx->distTol) ||
  1731. nvg__distPtSeg(x1,y1, x0,y0, x2,y2) < ctx->distTol*ctx->distTol ||
  1732. radius < ctx->distTol) {
  1733. nvgLineTo(ctx, x1,y1);
  1734. return;
  1735. }
  1736. // Calculate tangential circle to lines (x0,y0)-(x1,y1) and (x1,y1)-(x2,y2).
  1737. dx0 = x0-x1;
  1738. dy0 = y0-y1;
  1739. dx1 = x2-x1;
  1740. dy1 = y2-y1;
  1741. nvg__normalize(&dx0,&dy0);
  1742. nvg__normalize(&dx1,&dy1);
  1743. a = nvg__acosf(dx0*dx1 + dy0*dy1);
  1744. d = radius / nvg__tanf(a/2.0f);
  1745. // printf("a=%f° d=%f\n", a/NVG_PI*180.0f, d);
  1746. if (d > 10000.0f) {
  1747. nvgLineTo(ctx, x1,y1);
  1748. return;
  1749. }
  1750. if (nvg__cross(dx0,dy0, dx1,dy1) > 0.0f) {
  1751. cx = x1 + dx0*d + dy0*radius;
  1752. cy = y1 + dy0*d + -dx0*radius;
  1753. a0 = nvg__atan2f(dx0, -dy0);
  1754. a1 = nvg__atan2f(-dx1, dy1);
  1755. dir = NVG_CW;
  1756. // printf("CW c=(%f, %f) a0=%f° a1=%f°\n", cx, cy, a0/NVG_PI*180.0f, a1/NVG_PI*180.0f);
  1757. } else {
  1758. cx = x1 + dx0*d + -dy0*radius;
  1759. cy = y1 + dy0*d + dx0*radius;
  1760. a0 = nvg__atan2f(-dx0, dy0);
  1761. a1 = nvg__atan2f(dx1, -dy1);
  1762. dir = NVG_CCW;
  1763. // printf("CCW c=(%f, %f) a0=%f° a1=%f°\n", cx, cy, a0/NVG_PI*180.0f, a1/NVG_PI*180.0f);
  1764. }
  1765. nvgArc(ctx, cx, cy, radius, a0, a1, dir);
  1766. }
  1767. void nvgClosePath(NVGcontext* ctx)
  1768. {
  1769. float vals[] = { NVG_CLOSE };
  1770. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1771. }
  1772. void nvgPathWinding(NVGcontext* ctx, int dir)
  1773. {
  1774. float vals[] = { NVG_WINDING, (float)dir };
  1775. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1776. }
  1777. void nvgArc(NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir)
  1778. {
  1779. float a = 0, da = 0, hda = 0, kappa = 0;
  1780. float dx = 0, dy = 0, x = 0, y = 0, tanx = 0, tany = 0;
  1781. float px = 0, py = 0, ptanx = 0, ptany = 0;
  1782. float vals[3 + 5*7 + 100];
  1783. int i, ndivs, nvals;
  1784. int move = ctx->ncommands > 0 ? NVG_LINETO : NVG_MOVETO;
  1785. // Clamp angles
  1786. da = a1 - a0;
  1787. if (dir == NVG_CW) {
  1788. if (nvg__absf(da) >= NVG_PI*2) {
  1789. da = NVG_PI*2;
  1790. } else {
  1791. while (da < 0.0f) da += NVG_PI*2;
  1792. }
  1793. } else {
  1794. if (nvg__absf(da) >= NVG_PI*2) {
  1795. da = -NVG_PI*2;
  1796. } else {
  1797. while (da > 0.0f) da -= NVG_PI*2;
  1798. }
  1799. }
  1800. // Split arc into max 90 degree segments.
  1801. ndivs = nvg__maxi(1, nvg__mini((int)(nvg__absf(da) / (NVG_PI*0.5f) + 0.5f), 5));
  1802. hda = (da / (float)ndivs) / 2.0f;
  1803. kappa = nvg__absf(4.0f / 3.0f * (1.0f - nvg__cosf(hda)) / nvg__sinf(hda));
  1804. if (dir == NVG_CCW)
  1805. kappa = -kappa;
  1806. nvals = 0;
  1807. for (i = 0; i <= ndivs; i++) {
  1808. a = a0 + da * (i/(float)ndivs);
  1809. dx = nvg__cosf(a);
  1810. dy = nvg__sinf(a);
  1811. x = cx + dx*r;
  1812. y = cy + dy*r;
  1813. tanx = -dy*r*kappa;
  1814. tany = dx*r*kappa;
  1815. if (i == 0) {
  1816. vals[nvals++] = (float)move;
  1817. vals[nvals++] = x;
  1818. vals[nvals++] = y;
  1819. } else {
  1820. vals[nvals++] = NVG_BEZIERTO;
  1821. vals[nvals++] = px+ptanx;
  1822. vals[nvals++] = py+ptany;
  1823. vals[nvals++] = x-tanx;
  1824. vals[nvals++] = y-tany;
  1825. vals[nvals++] = x;
  1826. vals[nvals++] = y;
  1827. }
  1828. px = x;
  1829. py = y;
  1830. ptanx = tanx;
  1831. ptany = tany;
  1832. }
  1833. nvg__appendCommands(ctx, vals, nvals);
  1834. }
  1835. void nvgRect(NVGcontext* ctx, float x, float y, float w, float h)
  1836. {
  1837. float vals[] = {
  1838. NVG_MOVETO, x,y,
  1839. NVG_LINETO, x,y+h,
  1840. NVG_LINETO, x+w,y+h,
  1841. NVG_LINETO, x+w,y,
  1842. NVG_CLOSE
  1843. };
  1844. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1845. }
  1846. void nvgRoundedRect(NVGcontext* ctx, float x, float y, float w, float h, float r)
  1847. {
  1848. nvgRoundedRectVarying(ctx, x, y, w, h, r, r, r, r);
  1849. }
  1850. void nvgRoundedRectVarying(NVGcontext* ctx, float x, float y, float w, float h, float radTopLeft, float radTopRight, float radBottomRight, float radBottomLeft)
  1851. {
  1852. if(radTopLeft < 0.1f && radTopRight < 0.1f && radBottomRight < 0.1f && radBottomLeft < 0.1f) {
  1853. nvgRect(ctx, x, y, w, h);
  1854. return;
  1855. } else {
  1856. float halfw = nvg__absf(w)*0.5f;
  1857. float halfh = nvg__absf(h)*0.5f;
  1858. float rxBL = nvg__minf(radBottomLeft, halfw) * nvg__signf(w), ryBL = nvg__minf(radBottomLeft, halfh) * nvg__signf(h);
  1859. float rxBR = nvg__minf(radBottomRight, halfw) * nvg__signf(w), ryBR = nvg__minf(radBottomRight, halfh) * nvg__signf(h);
  1860. float rxTR = nvg__minf(radTopRight, halfw) * nvg__signf(w), ryTR = nvg__minf(radTopRight, halfh) * nvg__signf(h);
  1861. float rxTL = nvg__minf(radTopLeft, halfw) * nvg__signf(w), ryTL = nvg__minf(radTopLeft, halfh) * nvg__signf(h);
  1862. float vals[] = {
  1863. NVG_MOVETO, x, y + ryTL,
  1864. NVG_LINETO, x, y + h - ryBL,
  1865. NVG_BEZIERTO, x, y + h - ryBL*(1 - NVG_KAPPA90), x + rxBL*(1 - NVG_KAPPA90), y + h, x + rxBL, y + h,
  1866. NVG_LINETO, x + w - rxBR, y + h,
  1867. NVG_BEZIERTO, x + w - rxBR*(1 - NVG_KAPPA90), y + h, x + w, y + h - ryBR*(1 - NVG_KAPPA90), x + w, y + h - ryBR,
  1868. NVG_LINETO, x + w, y + ryTR,
  1869. NVG_BEZIERTO, x + w, y + ryTR*(1 - NVG_KAPPA90), x + w - rxTR*(1 - NVG_KAPPA90), y, x + w - rxTR, y,
  1870. NVG_LINETO, x + rxTL, y,
  1871. NVG_BEZIERTO, x + rxTL*(1 - NVG_KAPPA90), y, x, y + ryTL*(1 - NVG_KAPPA90), x, y + ryTL,
  1872. NVG_CLOSE
  1873. };
  1874. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1875. }
  1876. }
  1877. void nvgEllipse(NVGcontext* ctx, float cx, float cy, float rx, float ry)
  1878. {
  1879. float vals[] = {
  1880. NVG_MOVETO, cx-rx, cy,
  1881. NVG_BEZIERTO, cx-rx, cy+ry*NVG_KAPPA90, cx-rx*NVG_KAPPA90, cy+ry, cx, cy+ry,
  1882. NVG_BEZIERTO, cx+rx*NVG_KAPPA90, cy+ry, cx+rx, cy+ry*NVG_KAPPA90, cx+rx, cy,
  1883. NVG_BEZIERTO, cx+rx, cy-ry*NVG_KAPPA90, cx+rx*NVG_KAPPA90, cy-ry, cx, cy-ry,
  1884. NVG_BEZIERTO, cx-rx*NVG_KAPPA90, cy-ry, cx-rx, cy-ry*NVG_KAPPA90, cx-rx, cy,
  1885. NVG_CLOSE
  1886. };
  1887. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1888. }
  1889. void nvgCircle(NVGcontext* ctx, float cx, float cy, float r)
  1890. {
  1891. nvgEllipse(ctx, cx,cy, r,r);
  1892. }
  1893. void nvgDebugDumpPathCache(NVGcontext* ctx)
  1894. {
  1895. const NVGpath* path;
  1896. int i, j;
  1897. printf("Dumping %d cached paths\n", ctx->cache->npaths);
  1898. for (i = 0; i < ctx->cache->npaths; i++) {
  1899. path = &ctx->cache->paths[i];
  1900. printf(" - Path %d\n", i);
  1901. if (path->nfill) {
  1902. printf(" - fill: %d\n", path->nfill);
  1903. for (j = 0; j < path->nfill; j++)
  1904. printf("%f\t%f\n", path->fill[j].x, path->fill[j].y);
  1905. }
  1906. if (path->nstroke) {
  1907. printf(" - stroke: %d\n", path->nstroke);
  1908. for (j = 0; j < path->nstroke; j++)
  1909. printf("%f\t%f\n", path->stroke[j].x, path->stroke[j].y);
  1910. }
  1911. }
  1912. }
  1913. void nvgFill(NVGcontext* ctx)
  1914. {
  1915. NVGstate* state = nvg__getState(ctx);
  1916. const NVGpath* path;
  1917. NVGpaint fillPaint = state->fill;
  1918. int i;
  1919. nvg__flattenPaths(ctx);
  1920. if (ctx->params.edgeAntiAlias && state->shapeAntiAlias)
  1921. nvg__expandFill(ctx, ctx->fringeWidth, NVG_MITER, 2.4f);
  1922. else
  1923. nvg__expandFill(ctx, 0.0f, NVG_MITER, 2.4f);
  1924. // Apply global alpha
  1925. fillPaint.innerColor.a *= state->alpha;
  1926. fillPaint.outerColor.a *= state->alpha;
  1927. ctx->params.renderFill(ctx->params.userPtr, &fillPaint, state->compositeOperation, &state->scissor, ctx->fringeWidth,
  1928. ctx->cache->bounds, ctx->cache->paths, ctx->cache->npaths);
  1929. // Count triangles
  1930. for (i = 0; i < ctx->cache->npaths; i++) {
  1931. path = &ctx->cache->paths[i];
  1932. ctx->fillTriCount += path->nfill-2;
  1933. ctx->fillTriCount += path->nstroke-2;
  1934. ctx->drawCallCount += 2;
  1935. }
  1936. }
  1937. void nvgStroke(NVGcontext* ctx)
  1938. {
  1939. NVGstate* state = nvg__getState(ctx);
  1940. float scale = nvg__getAverageScale(state->xform);
  1941. float strokeWidth = nvg__clampf(state->strokeWidth * scale, 0.0f, 200.0f);
  1942. NVGpaint strokePaint = state->stroke;
  1943. const NVGpath* path;
  1944. int i;
  1945. if (strokeWidth < ctx->fringeWidth) {
  1946. // If the stroke width is less than pixel size, use alpha to emulate coverage.
  1947. // Since coverage is area, scale by alpha*alpha.
  1948. float alpha = nvg__clampf(strokeWidth / ctx->fringeWidth, 0.0f, 1.0f);
  1949. strokePaint.innerColor.a *= alpha*alpha;
  1950. strokePaint.outerColor.a *= alpha*alpha;
  1951. strokeWidth = ctx->fringeWidth;
  1952. }
  1953. // Apply global alpha
  1954. strokePaint.innerColor.a *= state->alpha;
  1955. strokePaint.outerColor.a *= state->alpha;
  1956. nvg__flattenPaths(ctx);
  1957. if (ctx->params.edgeAntiAlias && state->shapeAntiAlias)
  1958. nvg__expandStroke(ctx, strokeWidth*0.5f, ctx->fringeWidth, state->lineCap, state->lineJoin, state->miterLimit);
  1959. else
  1960. nvg__expandStroke(ctx, strokeWidth*0.5f, 0.0f, state->lineCap, state->lineJoin, state->miterLimit);
  1961. ctx->params.renderStroke(ctx->params.userPtr, &strokePaint, state->compositeOperation, &state->scissor, ctx->fringeWidth,
  1962. strokeWidth, ctx->cache->paths, ctx->cache->npaths);
  1963. // Count triangles
  1964. for (i = 0; i < ctx->cache->npaths; i++) {
  1965. path = &ctx->cache->paths[i];
  1966. ctx->strokeTriCount += path->nstroke-2;
  1967. ctx->drawCallCount++;
  1968. }
  1969. }
  1970. // Add fonts
  1971. int nvgCreateFont(NVGcontext* ctx, const char* name, const char* filename)
  1972. {
  1973. return fonsAddFont(ctx->fs, name, filename, 0);
  1974. }
  1975. int nvgCreateFontAtIndex(NVGcontext* ctx, const char* name, const char* filename, const int fontIndex)
  1976. {
  1977. return fonsAddFont(ctx->fs, name, filename, fontIndex);
  1978. }
  1979. int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData)
  1980. {
  1981. return fonsAddFontMem(ctx->fs, name, data, ndata, freeData, 0);
  1982. }
  1983. int nvgCreateFontMemAtIndex(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData, const int fontIndex)
  1984. {
  1985. return fonsAddFontMem(ctx->fs, name, data, ndata, freeData, fontIndex);
  1986. }
  1987. int nvgFindFont(NVGcontext* ctx, const char* name)
  1988. {
  1989. if (name == NULL) return -1;
  1990. return fonsGetFontByName(ctx->fs, name);
  1991. }
  1992. int nvgAddFallbackFontId(NVGcontext* ctx, int baseFont, int fallbackFont)
  1993. {
  1994. if(baseFont == -1 || fallbackFont == -1) return 0;
  1995. return fonsAddFallbackFont(ctx->fs, baseFont, fallbackFont);
  1996. }
  1997. int nvgAddFallbackFont(NVGcontext* ctx, const char* baseFont, const char* fallbackFont)
  1998. {
  1999. return nvgAddFallbackFontId(ctx, nvgFindFont(ctx, baseFont), nvgFindFont(ctx, fallbackFont));
  2000. }
  2001. void nvgResetFallbackFontsId(NVGcontext* ctx, int baseFont)
  2002. {
  2003. fonsResetFallbackFont(ctx->fs, baseFont);
  2004. }
  2005. void nvgResetFallbackFonts(NVGcontext* ctx, const char* baseFont)
  2006. {
  2007. nvgResetFallbackFontsId(ctx, nvgFindFont(ctx, baseFont));
  2008. }
  2009. // State setting
  2010. void nvgFontSize(NVGcontext* ctx, float size)
  2011. {
  2012. NVGstate* state = nvg__getState(ctx);
  2013. state->fontSize = size;
  2014. }
  2015. void nvgFontBlur(NVGcontext* ctx, float blur)
  2016. {
  2017. NVGstate* state = nvg__getState(ctx);
  2018. state->fontBlur = blur;
  2019. }
  2020. void nvgTextLetterSpacing(NVGcontext* ctx, float spacing)
  2021. {
  2022. NVGstate* state = nvg__getState(ctx);
  2023. state->letterSpacing = spacing;
  2024. }
  2025. void nvgTextLineHeight(NVGcontext* ctx, float lineHeight)
  2026. {
  2027. NVGstate* state = nvg__getState(ctx);
  2028. state->lineHeight = lineHeight;
  2029. }
  2030. void nvgTextAlign(NVGcontext* ctx, int align)
  2031. {
  2032. NVGstate* state = nvg__getState(ctx);
  2033. state->textAlign = align;
  2034. }
  2035. void nvgFontFaceId(NVGcontext* ctx, int font)
  2036. {
  2037. NVGstate* state = nvg__getState(ctx);
  2038. state->fontId = font;
  2039. }
  2040. void nvgFontFace(NVGcontext* ctx, const char* font)
  2041. {
  2042. NVGstate* state = nvg__getState(ctx);
  2043. state->fontId = fonsGetFontByName(ctx->fs, font);
  2044. }
  2045. static float nvg__quantize(float a, float d)
  2046. {
  2047. return ((int)(a / d + 0.5f)) * d;
  2048. }
  2049. static float nvg__getFontScale(NVGstate* state)
  2050. {
  2051. return nvg__minf(nvg__quantize(nvg__getAverageScale(state->xform), 0.01f), 4.0f);
  2052. }
  2053. static void nvg__flushTextTexture(NVGcontext* ctx)
  2054. {
  2055. int dirty[4];
  2056. if (fonsValidateTexture(ctx->fs, dirty)) {
  2057. int fontImage = ctx->fontImages[ctx->fontImageIdx];
  2058. // Update texture
  2059. if (fontImage != 0) {
  2060. int iw, ih;
  2061. const unsigned char* data = fonsGetTextureData(ctx->fs, &iw, &ih);
  2062. int x = dirty[0];
  2063. int y = dirty[1];
  2064. int w = dirty[2] - dirty[0];
  2065. int h = dirty[3] - dirty[1];
  2066. ctx->params.renderUpdateTexture(ctx->params.userPtr, fontImage, x,y, w,h, data);
  2067. }
  2068. }
  2069. }
  2070. static int nvg__allocTextAtlas(NVGcontext* ctx)
  2071. {
  2072. int iw, ih;
  2073. nvg__flushTextTexture(ctx);
  2074. if (ctx->fontImageIdx >= NVG_MAX_FONTIMAGES-1)
  2075. return 0;
  2076. // if next fontImage already have a texture
  2077. if (ctx->fontImages[ctx->fontImageIdx+1] != 0)
  2078. nvgImageSize(ctx, ctx->fontImages[ctx->fontImageIdx+1], &iw, &ih);
  2079. else { // calculate the new font image size and create it.
  2080. nvgImageSize(ctx, ctx->fontImages[ctx->fontImageIdx], &iw, &ih);
  2081. if (iw > ih)
  2082. ih *= 2;
  2083. else
  2084. iw *= 2;
  2085. if (iw > NVG_MAX_FONTIMAGE_SIZE || ih > NVG_MAX_FONTIMAGE_SIZE)
  2086. iw = ih = NVG_MAX_FONTIMAGE_SIZE;
  2087. ctx->fontImages[ctx->fontImageIdx+1] = ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_ALPHA, iw, ih, 0, NULL);
  2088. }
  2089. ++ctx->fontImageIdx;
  2090. fonsResetAtlas(ctx->fs, iw, ih);
  2091. return 1;
  2092. }
  2093. static void nvg__renderText(NVGcontext* ctx, NVGvertex* verts, int nverts)
  2094. {
  2095. NVGstate* state = nvg__getState(ctx);
  2096. NVGpaint paint = state->fill;
  2097. // Render triangles.
  2098. paint.image = ctx->fontImages[ctx->fontImageIdx];
  2099. // Apply global alpha
  2100. paint.innerColor.a *= state->alpha;
  2101. paint.outerColor.a *= state->alpha;
  2102. ctx->params.renderTriangles(ctx->params.userPtr, &paint, state->compositeOperation, &state->scissor, verts, nverts, ctx->fringeWidth);
  2103. ctx->drawCallCount++;
  2104. ctx->textTriCount += nverts/3;
  2105. }
  2106. static int nvg__isTransformFlipped( const float *xform)
  2107. {
  2108. float det = xform[0] * xform[3] - xform[2] * xform[1];
  2109. return( det < 0);
  2110. }
  2111. float nvgText(NVGcontext* ctx, float x, float y, const char* string, const char* end)
  2112. {
  2113. NVGstate* state = nvg__getState(ctx);
  2114. FONStextIter iter, prevIter;
  2115. FONSquad q;
  2116. NVGvertex* verts;
  2117. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2118. float invscale = 1.0f / scale;
  2119. int cverts = 0;
  2120. int nverts = 0;
  2121. int isFlipped;
  2122. float tmp;
  2123. if (end == NULL)
  2124. end = string + strlen(string);
  2125. if (state->fontId == FONS_INVALID) return x;
  2126. fonsSetSize(ctx->fs, state->fontSize*scale);
  2127. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2128. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2129. fonsSetAlign(ctx->fs, state->textAlign);
  2130. fonsSetFont(ctx->fs, state->fontId);
  2131. cverts = nvg__maxi(2, (int)(end - string)) * 6; // conservative estimate.
  2132. verts = nvg__allocTempVerts(ctx, cverts);
  2133. if (verts == NULL) return x;
  2134. isFlipped = nvg__isTransformFlipped( state->xform);
  2135. fonsTextIterInit(ctx->fs, &iter, x*scale, y*scale, string, end, FONS_GLYPH_BITMAP_REQUIRED);
  2136. prevIter = iter;
  2137. while (fonsTextIterNext(ctx->fs, &iter, &q)) {
  2138. float c[4*2];
  2139. if (iter.prevGlyphIndex == -1) { // can not retrieve glyph?
  2140. if (nverts != 0) {
  2141. nvg__renderText(ctx, verts, nverts);
  2142. nverts = 0;
  2143. }
  2144. if (!nvg__allocTextAtlas(ctx))
  2145. break; // no memory :(
  2146. iter = prevIter;
  2147. fonsTextIterNext(ctx->fs, &iter, &q); // try again
  2148. if (iter.prevGlyphIndex == -1) // still can not find glyph?
  2149. break;
  2150. }
  2151. prevIter = iter;
  2152. if( isFlipped)
  2153. {
  2154. tmp = q.y0; q.y0 = q.y1; q.y1 = tmp;
  2155. tmp = q.t0; q.t0 = q.t1; q.t1 = tmp;
  2156. }
  2157. // Transform corners.
  2158. nvgTransformPoint(&c[0],&c[1], state->xform, q.x0*invscale, q.y0*invscale);
  2159. nvgTransformPoint(&c[2],&c[3], state->xform, q.x1*invscale, q.y0*invscale);
  2160. nvgTransformPoint(&c[4],&c[5], state->xform, q.x1*invscale, q.y1*invscale);
  2161. nvgTransformPoint(&c[6],&c[7], state->xform, q.x0*invscale, q.y1*invscale);
  2162. // Create triangles
  2163. if (nverts+6 <= cverts) {
  2164. nvg__vset(&verts[nverts], c[0], c[1], q.s0, q.t0); nverts++;
  2165. nvg__vset(&verts[nverts], c[4], c[5], q.s1, q.t1); nverts++;
  2166. nvg__vset(&verts[nverts], c[2], c[3], q.s1, q.t0); nverts++;
  2167. nvg__vset(&verts[nverts], c[0], c[1], q.s0, q.t0); nverts++;
  2168. nvg__vset(&verts[nverts], c[6], c[7], q.s0, q.t1); nverts++;
  2169. nvg__vset(&verts[nverts], c[4], c[5], q.s1, q.t1); nverts++;
  2170. }
  2171. }
  2172. // TODO: add back-end bit to do this just once per frame.
  2173. nvg__flushTextTexture(ctx);
  2174. nvg__renderText(ctx, verts, nverts);
  2175. return iter.nextx / scale;
  2176. }
  2177. void nvgTextBox(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end)
  2178. {
  2179. NVGstate* state = nvg__getState(ctx);
  2180. NVGtextRow rows[2];
  2181. int nrows = 0, i;
  2182. int oldAlign = state->textAlign;
  2183. int haling = state->textAlign & (NVG_ALIGN_LEFT | NVG_ALIGN_CENTER | NVG_ALIGN_RIGHT);
  2184. int valign = state->textAlign & (NVG_ALIGN_TOP | NVG_ALIGN_MIDDLE | NVG_ALIGN_BOTTOM | NVG_ALIGN_BASELINE);
  2185. float lineh = 0;
  2186. if (state->fontId == FONS_INVALID) return;
  2187. nvgTextMetrics(ctx, NULL, NULL, &lineh);
  2188. state->textAlign = NVG_ALIGN_LEFT | valign;
  2189. while ((nrows = nvgTextBreakLines(ctx, string, end, breakRowWidth, rows, 2))) {
  2190. for (i = 0; i < nrows; i++) {
  2191. NVGtextRow* row = &rows[i];
  2192. if (haling & NVG_ALIGN_LEFT)
  2193. nvgText(ctx, x, y, row->start, row->end);
  2194. else if (haling & NVG_ALIGN_CENTER)
  2195. nvgText(ctx, x + breakRowWidth*0.5f - row->width*0.5f, y, row->start, row->end);
  2196. else if (haling & NVG_ALIGN_RIGHT)
  2197. nvgText(ctx, x + breakRowWidth - row->width, y, row->start, row->end);
  2198. y += lineh * state->lineHeight;
  2199. }
  2200. string = rows[nrows-1].next;
  2201. }
  2202. state->textAlign = oldAlign;
  2203. }
  2204. int nvgTextGlyphPositions(NVGcontext* ctx, float x, float y, const char* string, const char* end, NVGglyphPosition* positions, int maxPositions)
  2205. {
  2206. NVGstate* state = nvg__getState(ctx);
  2207. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2208. float invscale = 1.0f / scale;
  2209. FONStextIter iter, prevIter;
  2210. FONSquad q;
  2211. int npos = 0;
  2212. if (state->fontId == FONS_INVALID) return 0;
  2213. if (end == NULL)
  2214. end = string + strlen(string);
  2215. if (string == end)
  2216. return 0;
  2217. fonsSetSize(ctx->fs, state->fontSize*scale);
  2218. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2219. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2220. fonsSetAlign(ctx->fs, state->textAlign);
  2221. fonsSetFont(ctx->fs, state->fontId);
  2222. fonsTextIterInit(ctx->fs, &iter, x*scale, y*scale, string, end, FONS_GLYPH_BITMAP_OPTIONAL);
  2223. prevIter = iter;
  2224. while (fonsTextIterNext(ctx->fs, &iter, &q)) {
  2225. if (iter.prevGlyphIndex < 0 && nvg__allocTextAtlas(ctx)) { // can not retrieve glyph?
  2226. iter = prevIter;
  2227. fonsTextIterNext(ctx->fs, &iter, &q); // try again
  2228. }
  2229. prevIter = iter;
  2230. positions[npos].str = iter.str;
  2231. positions[npos].x = iter.x * invscale;
  2232. positions[npos].minx = nvg__minf(iter.x, q.x0) * invscale;
  2233. positions[npos].maxx = nvg__maxf(iter.nextx, q.x1) * invscale;
  2234. npos++;
  2235. if (npos >= maxPositions)
  2236. break;
  2237. }
  2238. return npos;
  2239. }
  2240. enum NVGcodepointType {
  2241. NVG_SPACE,
  2242. NVG_NEWLINE,
  2243. NVG_CHAR,
  2244. NVG_CJK_CHAR,
  2245. };
  2246. int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows)
  2247. {
  2248. NVGstate* state = nvg__getState(ctx);
  2249. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2250. float invscale = 1.0f / scale;
  2251. FONStextIter iter, prevIter;
  2252. FONSquad q;
  2253. int nrows = 0;
  2254. float rowStartX = 0;
  2255. float rowWidth = 0;
  2256. float rowMinX = 0;
  2257. float rowMaxX = 0;
  2258. const char* rowStart = NULL;
  2259. const char* rowEnd = NULL;
  2260. const char* wordStart = NULL;
  2261. float wordStartX = 0;
  2262. float wordMinX = 0;
  2263. const char* breakEnd = NULL;
  2264. float breakWidth = 0;
  2265. float breakMaxX = 0;
  2266. int type = NVG_SPACE, ptype = NVG_SPACE;
  2267. unsigned int pcodepoint = 0;
  2268. if (maxRows == 0) return 0;
  2269. if (state->fontId == FONS_INVALID) return 0;
  2270. if (end == NULL)
  2271. end = string + strlen(string);
  2272. if (string == end) return 0;
  2273. fonsSetSize(ctx->fs, state->fontSize*scale);
  2274. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2275. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2276. fonsSetAlign(ctx->fs, state->textAlign);
  2277. fonsSetFont(ctx->fs, state->fontId);
  2278. breakRowWidth *= scale;
  2279. fonsTextIterInit(ctx->fs, &iter, 0, 0, string, end, FONS_GLYPH_BITMAP_OPTIONAL);
  2280. prevIter = iter;
  2281. while (fonsTextIterNext(ctx->fs, &iter, &q)) {
  2282. if (iter.prevGlyphIndex < 0 && nvg__allocTextAtlas(ctx)) { // can not retrieve glyph?
  2283. iter = prevIter;
  2284. fonsTextIterNext(ctx->fs, &iter, &q); // try again
  2285. }
  2286. prevIter = iter;
  2287. switch (iter.codepoint) {
  2288. case 9: // \t
  2289. case 11: // \v
  2290. case 12: // \f
  2291. case 32: // space
  2292. case 0x00a0: // NBSP
  2293. type = NVG_SPACE;
  2294. break;
  2295. case 10: // \n
  2296. type = pcodepoint == 13 ? NVG_SPACE : NVG_NEWLINE;
  2297. break;
  2298. case 13: // \r
  2299. type = pcodepoint == 10 ? NVG_SPACE : NVG_NEWLINE;
  2300. break;
  2301. case 0x0085: // NEL
  2302. type = NVG_NEWLINE;
  2303. break;
  2304. default:
  2305. if ((iter.codepoint >= 0x4E00 && iter.codepoint <= 0x9FFF) ||
  2306. (iter.codepoint >= 0x3000 && iter.codepoint <= 0x30FF) ||
  2307. (iter.codepoint >= 0xFF00 && iter.codepoint <= 0xFFEF) ||
  2308. (iter.codepoint >= 0x1100 && iter.codepoint <= 0x11FF) ||
  2309. (iter.codepoint >= 0x3130 && iter.codepoint <= 0x318F) ||
  2310. (iter.codepoint >= 0xAC00 && iter.codepoint <= 0xD7AF))
  2311. type = NVG_CJK_CHAR;
  2312. else
  2313. type = NVG_CHAR;
  2314. break;
  2315. }
  2316. if (type == NVG_NEWLINE) {
  2317. // Always handle new lines.
  2318. rows[nrows].start = rowStart != NULL ? rowStart : iter.str;
  2319. rows[nrows].end = rowEnd != NULL ? rowEnd : iter.str;
  2320. rows[nrows].width = rowWidth * invscale;
  2321. rows[nrows].minx = rowMinX * invscale;
  2322. rows[nrows].maxx = rowMaxX * invscale;
  2323. rows[nrows].next = iter.next;
  2324. nrows++;
  2325. if (nrows >= maxRows)
  2326. return nrows;
  2327. // Set null break point
  2328. breakEnd = rowStart;
  2329. breakWidth = 0.0;
  2330. breakMaxX = 0.0;
  2331. // Indicate to skip the white space at the beginning of the row.
  2332. rowStart = NULL;
  2333. rowEnd = NULL;
  2334. rowWidth = 0;
  2335. rowMinX = rowMaxX = 0;
  2336. } else {
  2337. if (rowStart == NULL) {
  2338. // Skip white space until the beginning of the line
  2339. if (type == NVG_CHAR || type == NVG_CJK_CHAR) {
  2340. // The current char is the row so far
  2341. rowStartX = iter.x;
  2342. rowStart = iter.str;
  2343. rowEnd = iter.next;
  2344. rowWidth = iter.nextx - rowStartX;
  2345. rowMinX = q.x0 - rowStartX;
  2346. rowMaxX = q.x1 - rowStartX;
  2347. wordStart = iter.str;
  2348. wordStartX = iter.x;
  2349. wordMinX = q.x0 - rowStartX;
  2350. // Set null break point
  2351. breakEnd = rowStart;
  2352. breakWidth = 0.0;
  2353. breakMaxX = 0.0;
  2354. }
  2355. } else {
  2356. float nextWidth = iter.nextx - rowStartX;
  2357. // track last non-white space character
  2358. if (type == NVG_CHAR || type == NVG_CJK_CHAR) {
  2359. rowEnd = iter.next;
  2360. rowWidth = iter.nextx - rowStartX;
  2361. rowMaxX = q.x1 - rowStartX;
  2362. }
  2363. // track last end of a word
  2364. if (((ptype == NVG_CHAR || ptype == NVG_CJK_CHAR) && type == NVG_SPACE) || type == NVG_CJK_CHAR) {
  2365. breakEnd = iter.str;
  2366. breakWidth = rowWidth;
  2367. breakMaxX = rowMaxX;
  2368. }
  2369. // track last beginning of a word
  2370. if ((ptype == NVG_SPACE && (type == NVG_CHAR || type == NVG_CJK_CHAR)) || type == NVG_CJK_CHAR) {
  2371. wordStart = iter.str;
  2372. wordStartX = iter.x;
  2373. wordMinX = q.x0;
  2374. }
  2375. // Break to new line when a character is beyond break width.
  2376. if ((type == NVG_CHAR || type == NVG_CJK_CHAR) && nextWidth > breakRowWidth) {
  2377. // The run length is too long, need to break to new line.
  2378. if (breakEnd == rowStart) {
  2379. // The current word is longer than the row length, just break it from here.
  2380. rows[nrows].start = rowStart;
  2381. rows[nrows].end = iter.str;
  2382. rows[nrows].width = rowWidth * invscale;
  2383. rows[nrows].minx = rowMinX * invscale;
  2384. rows[nrows].maxx = rowMaxX * invscale;
  2385. rows[nrows].next = iter.str;
  2386. nrows++;
  2387. if (nrows >= maxRows)
  2388. return nrows;
  2389. rowStartX = iter.x;
  2390. rowStart = iter.str;
  2391. rowEnd = iter.next;
  2392. rowWidth = iter.nextx - rowStartX;
  2393. rowMinX = q.x0 - rowStartX;
  2394. rowMaxX = q.x1 - rowStartX;
  2395. wordStart = iter.str;
  2396. wordStartX = iter.x;
  2397. wordMinX = q.x0 - rowStartX;
  2398. } else {
  2399. // Break the line from the end of the last word, and start new line from the beginning of the new.
  2400. rows[nrows].start = rowStart;
  2401. rows[nrows].end = breakEnd;
  2402. rows[nrows].width = breakWidth * invscale;
  2403. rows[nrows].minx = rowMinX * invscale;
  2404. rows[nrows].maxx = breakMaxX * invscale;
  2405. rows[nrows].next = wordStart;
  2406. nrows++;
  2407. if (nrows >= maxRows)
  2408. return nrows;
  2409. // Update row
  2410. rowStartX = wordStartX;
  2411. rowStart = wordStart;
  2412. rowEnd = iter.next;
  2413. rowWidth = iter.nextx - rowStartX;
  2414. rowMinX = wordMinX - rowStartX;
  2415. rowMaxX = q.x1 - rowStartX;
  2416. }
  2417. // Set null break point
  2418. breakEnd = rowStart;
  2419. breakWidth = 0.0;
  2420. breakMaxX = 0.0;
  2421. }
  2422. }
  2423. }
  2424. pcodepoint = iter.codepoint;
  2425. ptype = type;
  2426. }
  2427. // Break the line from the end of the last word, and start new line from the beginning of the new.
  2428. if (rowStart != NULL) {
  2429. rows[nrows].start = rowStart;
  2430. rows[nrows].end = rowEnd;
  2431. rows[nrows].width = rowWidth * invscale;
  2432. rows[nrows].minx = rowMinX * invscale;
  2433. rows[nrows].maxx = rowMaxX * invscale;
  2434. rows[nrows].next = end;
  2435. nrows++;
  2436. }
  2437. return nrows;
  2438. }
  2439. float nvgTextBounds(NVGcontext* ctx, float x, float y, const char* string, const char* end, float* bounds)
  2440. {
  2441. NVGstate* state = nvg__getState(ctx);
  2442. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2443. float invscale = 1.0f / scale;
  2444. float width;
  2445. if (state->fontId == FONS_INVALID) return 0;
  2446. fonsSetSize(ctx->fs, state->fontSize*scale);
  2447. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2448. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2449. fonsSetAlign(ctx->fs, state->textAlign);
  2450. fonsSetFont(ctx->fs, state->fontId);
  2451. width = fonsTextBounds(ctx->fs, x*scale, y*scale, string, end, bounds);
  2452. if (bounds != NULL) {
  2453. // Use line bounds for height.
  2454. fonsLineBounds(ctx->fs, y*scale, &bounds[1], &bounds[3]);
  2455. bounds[0] *= invscale;
  2456. bounds[1] *= invscale;
  2457. bounds[2] *= invscale;
  2458. bounds[3] *= invscale;
  2459. }
  2460. return width * invscale;
  2461. }
  2462. void nvgTextBoxBounds(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end, float* bounds)
  2463. {
  2464. NVGstate* state = nvg__getState(ctx);
  2465. NVGtextRow rows[2];
  2466. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2467. float invscale = 1.0f / scale;
  2468. int nrows = 0, i;
  2469. int oldAlign = state->textAlign;
  2470. int haling = state->textAlign & (NVG_ALIGN_LEFT | NVG_ALIGN_CENTER | NVG_ALIGN_RIGHT);
  2471. int valign = state->textAlign & (NVG_ALIGN_TOP | NVG_ALIGN_MIDDLE | NVG_ALIGN_BOTTOM | NVG_ALIGN_BASELINE);
  2472. float lineh = 0, rminy = 0, rmaxy = 0;
  2473. float minx, miny, maxx, maxy;
  2474. if (state->fontId == FONS_INVALID) {
  2475. if (bounds != NULL)
  2476. bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0f;
  2477. return;
  2478. }
  2479. nvgTextMetrics(ctx, NULL, NULL, &lineh);
  2480. state->textAlign = NVG_ALIGN_LEFT | valign;
  2481. minx = maxx = x;
  2482. miny = maxy = y;
  2483. fonsSetSize(ctx->fs, state->fontSize*scale);
  2484. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2485. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2486. fonsSetAlign(ctx->fs, state->textAlign);
  2487. fonsSetFont(ctx->fs, state->fontId);
  2488. fonsLineBounds(ctx->fs, 0, &rminy, &rmaxy);
  2489. rminy *= invscale;
  2490. rmaxy *= invscale;
  2491. while ((nrows = nvgTextBreakLines(ctx, string, end, breakRowWidth, rows, 2))) {
  2492. for (i = 0; i < nrows; i++) {
  2493. NVGtextRow* row = &rows[i];
  2494. float rminx, rmaxx, dx = 0;
  2495. // Horizontal bounds
  2496. if (haling & NVG_ALIGN_LEFT)
  2497. dx = 0;
  2498. else if (haling & NVG_ALIGN_CENTER)
  2499. dx = breakRowWidth*0.5f - row->width*0.5f;
  2500. else if (haling & NVG_ALIGN_RIGHT)
  2501. dx = breakRowWidth - row->width;
  2502. rminx = x + row->minx + dx;
  2503. rmaxx = x + row->maxx + dx;
  2504. minx = nvg__minf(minx, rminx);
  2505. maxx = nvg__maxf(maxx, rmaxx);
  2506. // Vertical bounds.
  2507. miny = nvg__minf(miny, y + rminy);
  2508. maxy = nvg__maxf(maxy, y + rmaxy);
  2509. y += lineh * state->lineHeight;
  2510. }
  2511. string = rows[nrows-1].next;
  2512. }
  2513. state->textAlign = oldAlign;
  2514. if (bounds != NULL) {
  2515. bounds[0] = minx;
  2516. bounds[1] = miny;
  2517. bounds[2] = maxx;
  2518. bounds[3] = maxy;
  2519. }
  2520. }
  2521. void nvgTextMetrics(NVGcontext* ctx, float* ascender, float* descender, float* lineh)
  2522. {
  2523. NVGstate* state = nvg__getState(ctx);
  2524. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2525. float invscale = 1.0f / scale;
  2526. if (state->fontId == FONS_INVALID) return;
  2527. fonsSetSize(ctx->fs, state->fontSize*scale);
  2528. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2529. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2530. fonsSetAlign(ctx->fs, state->textAlign);
  2531. fonsSetFont(ctx->fs, state->fontId);
  2532. fonsVertMetrics(ctx->fs, ascender, descender, lineh);
  2533. if (ascender != NULL)
  2534. *ascender *= invscale;
  2535. if (descender != NULL)
  2536. *descender *= invscale;
  2537. if (lineh != NULL)
  2538. *lineh *= invscale;
  2539. }
  2540. // vim: ft=c nu noet ts=4