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.

2477 lines
64KB

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