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.

2704 lines
71KB

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