DPF OpenGL examples
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.

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