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