-
Notifications
You must be signed in to change notification settings - Fork 2
/
nanovg.c
2891 lines (2495 loc) · 74.4 KB
/
nanovg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2013 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <memory.h>
#include "nanovg.h"
#define FONTSTASH_IMPLEMENTATION
#include "fontstash.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#ifdef _MSC_VER
#pragma warning(disable: 4100) // unreferenced formal parameter
#pragma warning(disable: 4127) // conditional expression is constant
#pragma warning(disable: 4204) // nonstandard extension used : non-constant aggregate initializer
#pragma warning(disable: 4706) // assignment within conditional expression
#endif
#define NVG_INIT_FONTIMAGE_SIZE 512
#define NVG_MAX_FONTIMAGE_SIZE 2048
#define NVG_MAX_FONTIMAGES 4
#define NVG_INIT_COMMANDS_SIZE 256
#define NVG_INIT_POINTS_SIZE 128
#define NVG_INIT_PATHS_SIZE 16
#define NVG_INIT_VERTS_SIZE 256
#define NVG_MAX_STATES 32
#define NVG_KAPPA90 0.5522847493f // Length proportional to radius of a cubic bezier handle for 90deg arcs.
#define NVG_COUNTOF(arr) (sizeof(arr) / sizeof(0[arr]))
enum NVGcommands {
NVG_MOVETO = 0,
NVG_LINETO = 1,
NVG_BEZIERTO = 2,
NVG_CLOSE = 3,
NVG_WINDING = 4,
};
enum NVGpointFlags
{
NVG_PT_CORNER = 0x01,
NVG_PT_LEFT = 0x02,
NVG_PT_BEVEL = 0x04,
NVG_PR_INNERBEVEL = 0x08,
};
struct NVGstate {
NVGcompositeOperationState compositeOperation;
int shapeAntiAlias;
NVGpaint fill;
NVGpaint stroke;
float strokeWidth;
float miterLimit;
int lineJoin;
int lineCap;
float alpha;
float xform[6];
NVGscissor scissor;
float fontSize;
float letterSpacing;
float lineHeight;
float fontBlur;
int textAlign;
int fontId;
};
typedef struct NVGstate NVGstate;
struct NVGpoint {
float x,y;
float dx, dy;
float len;
float dmx, dmy;
unsigned char flags;
};
typedef struct NVGpoint NVGpoint;
struct NVGpathCache {
NVGpoint* points;
int npoints;
int cpoints;
NVGpath* paths;
int npaths;
int cpaths;
NVGvertex* verts;
int nverts;
int cverts;
float bounds[4];
};
typedef struct NVGpathCache NVGpathCache;
struct NVGcontext {
NVGparams params;
float* commands;
int ccommands;
int ncommands;
float commandx, commandy;
NVGstate states[NVG_MAX_STATES];
int nstates;
NVGpathCache* cache;
float tessTol;
float distTol;
float fringeWidth;
float devicePxRatio;
struct FONScontext* fs;
int fontImages[NVG_MAX_FONTIMAGES];
int fontImageIdx;
int drawCallCount;
int fillTriCount;
int strokeTriCount;
int textTriCount;
};
static float nvg__sqrtf(float a) { return sqrtf(a); }
static float nvg__modf(float a, float b) { return fmodf(a, b); }
static float nvg__sinf(float a) { return sinf(a); }
static float nvg__cosf(float a) { return cosf(a); }
static float nvg__tanf(float a) { return tanf(a); }
static float nvg__atan2f(float a,float b) { return atan2f(a, b); }
static float nvg__acosf(float a) { return acosf(a); }
static int nvg__mini(int a, int b) { return a < b ? a : b; }
static int nvg__maxi(int a, int b) { return a > b ? a : b; }
static int nvg__clampi(int a, int mn, int mx) { return a < mn ? mn : (a > mx ? mx : a); }
static float nvg__minf(float a, float b) { return a < b ? a : b; }
static float nvg__maxf(float a, float b) { return a > b ? a : b; }
static float nvg__absf(float a) { return a >= 0.0f ? a : -a; }
static float nvg__signf(float a) { return a >= 0.0f ? 1.0f : -1.0f; }
static float nvg__clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
static float nvg__cross(float dx0, float dy0, float dx1, float dy1) { return dx1*dy0 - dx0*dy1; }
static float nvg__normalize(float *x, float* y)
{
float d = nvg__sqrtf((*x)*(*x) + (*y)*(*y));
if (d > 1e-6f) {
float id = 1.0f / d;
*x *= id;
*y *= id;
}
return d;
}
static void nvg__deletePathCache(NVGpathCache* c)
{
if (c == NULL) return;
if (c->points != NULL) free(c->points);
if (c->paths != NULL) free(c->paths);
if (c->verts != NULL) free(c->verts);
free(c);
}
static NVGpathCache* nvg__allocPathCache(void)
{
NVGpathCache* c = (NVGpathCache*)malloc(sizeof(NVGpathCache));
if (c == NULL) goto error;
memset(c, 0, sizeof(NVGpathCache));
c->points = (NVGpoint*)malloc(sizeof(NVGpoint)*NVG_INIT_POINTS_SIZE);
if (!c->points) goto error;
c->npoints = 0;
c->cpoints = NVG_INIT_POINTS_SIZE;
c->paths = (NVGpath*)malloc(sizeof(NVGpath)*NVG_INIT_PATHS_SIZE);
if (!c->paths) goto error;
c->npaths = 0;
c->cpaths = NVG_INIT_PATHS_SIZE;
c->verts = (NVGvertex*)malloc(sizeof(NVGvertex)*NVG_INIT_VERTS_SIZE);
if (!c->verts) goto error;
c->nverts = 0;
c->cverts = NVG_INIT_VERTS_SIZE;
return c;
error:
nvg__deletePathCache(c);
return NULL;
}
static void nvg__setDevicePixelRatio(NVGcontext* ctx, float ratio)
{
ctx->tessTol = 0.25f / ratio;
ctx->distTol = 0.01f / ratio;
ctx->fringeWidth = 1.0f / ratio;
ctx->devicePxRatio = ratio;
}
static NVGcompositeOperationState nvg__compositeOperationState(int op)
{
int sfactor, dfactor;
if (op == NVG_SOURCE_OVER)
{
sfactor = NVG_ONE;
dfactor = NVG_ONE_MINUS_SRC_ALPHA;
}
else if (op == NVG_SOURCE_IN)
{
sfactor = NVG_DST_ALPHA;
dfactor = NVG_ZERO;
}
else if (op == NVG_SOURCE_OUT)
{
sfactor = NVG_ONE_MINUS_DST_ALPHA;
dfactor = NVG_ZERO;
}
else if (op == NVG_ATOP)
{
sfactor = NVG_DST_ALPHA;
dfactor = NVG_ONE_MINUS_SRC_ALPHA;
}
else if (op == NVG_DESTINATION_OVER)
{
sfactor = NVG_ONE_MINUS_DST_ALPHA;
dfactor = NVG_ONE;
}
else if (op == NVG_DESTINATION_IN)
{
sfactor = NVG_ZERO;
dfactor = NVG_SRC_ALPHA;
}
else if (op == NVG_DESTINATION_OUT)
{
sfactor = NVG_ZERO;
dfactor = NVG_ONE_MINUS_SRC_ALPHA;
}
else if (op == NVG_DESTINATION_ATOP)
{
sfactor = NVG_ONE_MINUS_DST_ALPHA;
dfactor = NVG_SRC_ALPHA;
}
else if (op == NVG_LIGHTER)
{
sfactor = NVG_ONE;
dfactor = NVG_ONE;
}
else if (op == NVG_COPY)
{
sfactor = NVG_ONE;
dfactor = NVG_ZERO;
}
else if (op == NVG_XOR)
{
sfactor = NVG_ONE_MINUS_DST_ALPHA;
dfactor = NVG_ONE_MINUS_SRC_ALPHA;
}
else
{
sfactor = NVG_ONE;
dfactor = NVG_ZERO;
}
NVGcompositeOperationState state;
state.srcRGB = sfactor;
state.dstRGB = dfactor;
state.srcAlpha = sfactor;
state.dstAlpha = dfactor;
return state;
}
static NVGstate* nvg__getState(NVGcontext* ctx)
{
return &ctx->states[ctx->nstates-1];
}
NVGcontext* nvgCreateInternal(NVGparams* params)
{
FONSparams fontParams;
NVGcontext* ctx = (NVGcontext*)malloc(sizeof(NVGcontext));
int i;
if (ctx == NULL) goto error;
memset(ctx, 0, sizeof(NVGcontext));
ctx->params = *params;
for (i = 0; i < NVG_MAX_FONTIMAGES; i++)
ctx->fontImages[i] = 0;
ctx->commands = (float*)malloc(sizeof(float)*NVG_INIT_COMMANDS_SIZE);
if (!ctx->commands) goto error;
ctx->ncommands = 0;
ctx->ccommands = NVG_INIT_COMMANDS_SIZE;
ctx->cache = nvg__allocPathCache();
if (ctx->cache == NULL) goto error;
nvgSave(ctx);
nvgReset(ctx);
nvg__setDevicePixelRatio(ctx, 1.0f);
if (ctx->params.renderCreate(ctx->params.userPtr) == 0) goto error;
// Init font rendering
memset(&fontParams, 0, sizeof(fontParams));
fontParams.width = NVG_INIT_FONTIMAGE_SIZE;
fontParams.height = NVG_INIT_FONTIMAGE_SIZE;
fontParams.flags = FONS_ZERO_TOPLEFT;
fontParams.renderCreate = NULL;
fontParams.renderUpdate = NULL;
fontParams.renderDraw = NULL;
fontParams.renderDelete = NULL;
fontParams.userPtr = NULL;
ctx->fs = fonsCreateInternal(&fontParams);
if (ctx->fs == NULL) goto error;
// Create font texture
ctx->fontImages[0] = ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_ALPHA, fontParams.width, fontParams.height, 0, NULL);
if (ctx->fontImages[0] == 0) goto error;
ctx->fontImageIdx = 0;
return ctx;
error:
nvgDeleteInternal(ctx);
return 0;
}
NVGparams* nvgInternalParams(NVGcontext* ctx)
{
return &ctx->params;
}
void nvgDeleteInternal(NVGcontext* ctx)
{
int i;
if (ctx == NULL) return;
if (ctx->commands != NULL) free(ctx->commands);
if (ctx->cache != NULL) nvg__deletePathCache(ctx->cache);
if (ctx->fs)
fonsDeleteInternal(ctx->fs);
for (i = 0; i < NVG_MAX_FONTIMAGES; i++) {
if (ctx->fontImages[i] != 0) {
nvgDeleteImage(ctx, ctx->fontImages[i]);
ctx->fontImages[i] = 0;
}
}
if (ctx->params.renderDelete != NULL)
ctx->params.renderDelete(ctx->params.userPtr);
free(ctx);
}
void nvgBeginFrame(NVGcontext* ctx, int windowWidth, int windowHeight, float devicePixelRatio)
{
/* printf("Tris: draws:%d fill:%d stroke:%d text:%d TOT:%d\n",
ctx->drawCallCount, ctx->fillTriCount, ctx->strokeTriCount, ctx->textTriCount,
ctx->fillTriCount+ctx->strokeTriCount+ctx->textTriCount);*/
ctx->nstates = 0;
nvgSave(ctx);
nvgReset(ctx);
nvg__setDevicePixelRatio(ctx, devicePixelRatio);
ctx->params.renderViewport(ctx->params.userPtr, windowWidth, windowHeight, devicePixelRatio);
ctx->drawCallCount = 0;
ctx->fillTriCount = 0;
ctx->strokeTriCount = 0;
ctx->textTriCount = 0;
}
void nvgCancelFrame(NVGcontext* ctx)
{
ctx->params.renderCancel(ctx->params.userPtr);
}
void nvgEndFrame(NVGcontext* ctx)
{
ctx->params.renderFlush(ctx->params.userPtr);
if (ctx->fontImageIdx != 0) {
int fontImage = ctx->fontImages[ctx->fontImageIdx];
int i, j, iw, ih;
// delete images that smaller than current one
if (fontImage == 0)
return;
nvgImageSize(ctx, fontImage, &iw, &ih);
for (i = j = 0; i < ctx->fontImageIdx; i++) {
if (ctx->fontImages[i] != 0) {
int nw, nh;
nvgImageSize(ctx, ctx->fontImages[i], &nw, &nh);
if (nw < iw || nh < ih)
nvgDeleteImage(ctx, ctx->fontImages[i]);
else
ctx->fontImages[j++] = ctx->fontImages[i];
}
}
// make current font image to first
ctx->fontImages[j++] = ctx->fontImages[0];
ctx->fontImages[0] = fontImage;
ctx->fontImageIdx = 0;
// clear all images after j
for (i = j; i < NVG_MAX_FONTIMAGES; i++)
ctx->fontImages[i] = 0;
}
}
NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b)
{
return nvgRGBA(r,g,b,255);
}
NVGcolor nvgRGBf(float r, float g, float b)
{
return nvgRGBAf(r,g,b,1.0f);
}
NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
NVGcolor color;
// Use longer initialization to suppress warning.
color.r = r / 255.0f;
color.g = g / 255.0f;
color.b = b / 255.0f;
color.a = a / 255.0f;
return color;
}
NVGcolor nvgRGBAf(float r, float g, float b, float a)
{
NVGcolor color;
// Use longer initialization to suppress warning.
color.r = r;
color.g = g;
color.b = b;
color.a = a;
return color;
}
NVGcolor nvgTransRGBA(NVGcolor c, unsigned char a)
{
c.a = a / 255.0f;
return c;
}
NVGcolor nvgTransRGBAf(NVGcolor c, float a)
{
c.a = a;
return c;
}
NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u)
{
int i;
float oneminu;
NVGcolor cint = {{{0}}};
u = nvg__clampf(u, 0.0f, 1.0f);
oneminu = 1.0f - u;
for( i = 0; i <4; i++ )
{
cint.rgba[i] = c0.rgba[i] * oneminu + c1.rgba[i] * u;
}
return cint;
}
NVGcolor nvgHSL(float h, float s, float l)
{
return nvgHSLA(h,s,l,255);
}
static float nvg__hue(float h, float m1, float m2)
{
if (h < 0) h += 1;
if (h > 1) h -= 1;
if (h < 1.0f/6.0f)
return m1 + (m2 - m1) * h * 6.0f;
else if (h < 3.0f/6.0f)
return m2;
else if (h < 4.0f/6.0f)
return m1 + (m2 - m1) * (2.0f/3.0f - h) * 6.0f;
return m1;
}
NVGcolor nvgHSLA(float h, float s, float l, unsigned char a)
{
float m1, m2;
NVGcolor col;
h = nvg__modf(h, 1.0f);
if (h < 0.0f) h += 1.0f;
s = nvg__clampf(s, 0.0f, 1.0f);
l = nvg__clampf(l, 0.0f, 1.0f);
m2 = l <= 0.5f ? (l * (1 + s)) : (l + s - l * s);
m1 = 2 * l - m2;
col.r = nvg__clampf(nvg__hue(h + 1.0f/3.0f, m1, m2), 0.0f, 1.0f);
col.g = nvg__clampf(nvg__hue(h, m1, m2), 0.0f, 1.0f);
col.b = nvg__clampf(nvg__hue(h - 1.0f/3.0f, m1, m2), 0.0f, 1.0f);
col.a = a/255.0f;
return col;
}
void nvgTransformIdentity(float* t)
{
t[0] = 1.0f; t[1] = 0.0f;
t[2] = 0.0f; t[3] = 1.0f;
t[4] = 0.0f; t[5] = 0.0f;
}
void nvgTransformTranslate(float* t, float tx, float ty)
{
t[0] = 1.0f; t[1] = 0.0f;
t[2] = 0.0f; t[3] = 1.0f;
t[4] = tx; t[5] = ty;
}
void nvgTransformScale(float* t, float sx, float sy)
{
t[0] = sx; t[1] = 0.0f;
t[2] = 0.0f; t[3] = sy;
t[4] = 0.0f; t[5] = 0.0f;
}
void nvgTransformRotate(float* t, float a)
{
float cs = nvg__cosf(a), sn = nvg__sinf(a);
t[0] = cs; t[1] = sn;
t[2] = -sn; t[3] = cs;
t[4] = 0.0f; t[5] = 0.0f;
}
void nvgTransformSkewX(float* t, float a)
{
t[0] = 1.0f; t[1] = 0.0f;
t[2] = nvg__tanf(a); t[3] = 1.0f;
t[4] = 0.0f; t[5] = 0.0f;
}
void nvgTransformSkewY(float* t, float a)
{
t[0] = 1.0f; t[1] = nvg__tanf(a);
t[2] = 0.0f; t[3] = 1.0f;
t[4] = 0.0f; t[5] = 0.0f;
}
void nvgTransformMultiply(float* t, const float* s)
{
float t0 = t[0] * s[0] + t[1] * s[2];
float t2 = t[2] * s[0] + t[3] * s[2];
float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
t[1] = t[0] * s[1] + t[1] * s[3];
t[3] = t[2] * s[1] + t[3] * s[3];
t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
t[0] = t0;
t[2] = t2;
t[4] = t4;
}
void nvgTransformPremultiply(float* t, const float* s)
{
float s2[6];
memcpy(s2, s, sizeof(float)*6);
nvgTransformMultiply(s2, t);
memcpy(t, s2, sizeof(float)*6);
}
int nvgTransformInverse(float* inv, const float* t)
{
double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1];
if (det > -1e-6 && det < 1e-6) {
nvgTransformIdentity(inv);
return 0;
}
invdet = 1.0 / det;
inv[0] = (float)(t[3] * invdet);
inv[2] = (float)(-t[2] * invdet);
inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
inv[1] = (float)(-t[1] * invdet);
inv[3] = (float)(t[0] * invdet);
inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
return 1;
}
void nvgTransformPoint(float* dx, float* dy, const float* t, float sx, float sy)
{
*dx = sx*t[0] + sy*t[2] + t[4];
*dy = sx*t[1] + sy*t[3] + t[5];
}
float nvgDegToRad(float deg)
{
return deg / 180.0f * NVG_PI;
}
float nvgRadToDeg(float rad)
{
return rad / NVG_PI * 180.0f;
}
static void nvg__setPaintColor(NVGpaint* p, NVGcolor color)
{
memset(p, 0, sizeof(*p));
nvgTransformIdentity(p->xform);
p->radius = 0.0f;
p->feather = 1.0f;
p->innerColor = color;
p->outerColor = color;
}
// State handling
void nvgSave(NVGcontext* ctx)
{
if (ctx->nstates >= NVG_MAX_STATES)
return;
if (ctx->nstates > 0)
memcpy(&ctx->states[ctx->nstates], &ctx->states[ctx->nstates-1], sizeof(NVGstate));
ctx->nstates++;
}
void nvgRestore(NVGcontext* ctx)
{
if (ctx->nstates <= 1)
return;
ctx->nstates--;
}
void nvgReset(NVGcontext* ctx)
{
NVGstate* state = nvg__getState(ctx);
memset(state, 0, sizeof(*state));
nvg__setPaintColor(&state->fill, nvgRGBA(255,255,255,255));
nvg__setPaintColor(&state->stroke, nvgRGBA(0,0,0,255));
state->compositeOperation = nvg__compositeOperationState(NVG_SOURCE_OVER);
state->shapeAntiAlias = 1;
state->strokeWidth = 1.0f;
state->miterLimit = 10.0f;
state->lineCap = NVG_BUTT;
state->lineJoin = NVG_MITER;
state->alpha = 1.0f;
nvgTransformIdentity(state->xform);
state->scissor.extent[0] = -1.0f;
state->scissor.extent[1] = -1.0f;
state->fontSize = 16.0f;
state->letterSpacing = 0.0f;
state->lineHeight = 1.0f;
state->fontBlur = 0.0f;
state->textAlign = NVG_ALIGN_LEFT | NVG_ALIGN_BASELINE;
state->fontId = 0;
}
// State setting
void nvgShapeAntiAlias(NVGcontext* ctx, int enabled)
{
NVGstate* state = nvg__getState(ctx);
state->shapeAntiAlias = enabled;
}
void nvgStrokeWidth(NVGcontext* ctx, float width)
{
NVGstate* state = nvg__getState(ctx);
state->strokeWidth = width;
}
void nvgMiterLimit(NVGcontext* ctx, float limit)
{
NVGstate* state = nvg__getState(ctx);
state->miterLimit = limit;
}
void nvgLineCap(NVGcontext* ctx, int cap)
{
NVGstate* state = nvg__getState(ctx);
state->lineCap = cap;
}
void nvgLineJoin(NVGcontext* ctx, int join)
{
NVGstate* state = nvg__getState(ctx);
state->lineJoin = join;
}
void nvgGlobalAlpha(NVGcontext* ctx, float alpha)
{
NVGstate* state = nvg__getState(ctx);
state->alpha = alpha;
}
void nvgTransform(NVGcontext* ctx, float a, float b, float c, float d, float e, float f)
{
NVGstate* state = nvg__getState(ctx);
float t[6] = { a, b, c, d, e, f };
nvgTransformPremultiply(state->xform, t);
}
void nvgResetTransform(NVGcontext* ctx)
{
NVGstate* state = nvg__getState(ctx);
nvgTransformIdentity(state->xform);
}
void nvgTranslate(NVGcontext* ctx, float x, float y)
{
NVGstate* state = nvg__getState(ctx);
float t[6];
nvgTransformTranslate(t, x,y);
nvgTransformPremultiply(state->xform, t);
}
void nvgRotate(NVGcontext* ctx, float angle)
{
NVGstate* state = nvg__getState(ctx);
float t[6];
nvgTransformRotate(t, angle);
nvgTransformPremultiply(state->xform, t);
}
void nvgSkewX(NVGcontext* ctx, float angle)
{
NVGstate* state = nvg__getState(ctx);
float t[6];
nvgTransformSkewX(t, angle);
nvgTransformPremultiply(state->xform, t);
}
void nvgSkewY(NVGcontext* ctx, float angle)
{
NVGstate* state = nvg__getState(ctx);
float t[6];
nvgTransformSkewY(t, angle);
nvgTransformPremultiply(state->xform, t);
}
void nvgScale(NVGcontext* ctx, float x, float y)
{
NVGstate* state = nvg__getState(ctx);
float t[6];
nvgTransformScale(t, x,y);
nvgTransformPremultiply(state->xform, t);
}
void nvgCurrentTransform(NVGcontext* ctx, float* xform)
{
NVGstate* state = nvg__getState(ctx);
if (xform == NULL) return;
memcpy(xform, state->xform, sizeof(float)*6);
}
void nvgStrokeColor(NVGcontext* ctx, NVGcolor color)
{
NVGstate* state = nvg__getState(ctx);
nvg__setPaintColor(&state->stroke, color);
}
void nvgStrokePaint(NVGcontext* ctx, NVGpaint paint)
{
NVGstate* state = nvg__getState(ctx);
state->stroke = paint;
nvgTransformMultiply(state->stroke.xform, state->xform);
}
void nvgFillColor(NVGcontext* ctx, NVGcolor color)
{
NVGstate* state = nvg__getState(ctx);
nvg__setPaintColor(&state->fill, color);
}
void nvgFillPaint(NVGcontext* ctx, NVGpaint paint)
{
NVGstate* state = nvg__getState(ctx);
state->fill = paint;
nvgTransformMultiply(state->fill.xform, state->xform);
}
int nvgCreateImage(NVGcontext* ctx, const char* filename, int imageFlags)
{
int w, h, n, image;
unsigned char* img;
stbi_set_unpremultiply_on_load(1);
stbi_convert_iphone_png_to_rgb(1);
img = stbi_load(filename, &w, &h, &n, 4);
if (img == NULL) {
// printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
return 0;
}
image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
stbi_image_free(img);
return image;
}
int nvgCreateImageMem(NVGcontext* ctx, int imageFlags, unsigned char* data, int ndata)
{
int w, h, n, image;
unsigned char* img = stbi_load_from_memory(data, ndata, &w, &h, &n, 4);
if (img == NULL) {
// printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
return 0;
}
image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
stbi_image_free(img);
return image;
}
int nvgCreateImageRGBA(NVGcontext* ctx, int w, int h, int imageFlags, const unsigned char* data)
{
return ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_RGBA, w, h, imageFlags, data);
}
void nvgUpdateImage(NVGcontext* ctx, int image, const unsigned char* data)
{
int w, h;
ctx->params.renderGetTextureSize(ctx->params.userPtr, image, &w, &h);
ctx->params.renderUpdateTexture(ctx->params.userPtr, image, 0,0, w,h, data);
}
void nvgImageSize(NVGcontext* ctx, int image, int* w, int* h)
{
ctx->params.renderGetTextureSize(ctx->params.userPtr, image, w, h);
}
void nvgDeleteImage(NVGcontext* ctx, int image)
{
ctx->params.renderDeleteTexture(ctx->params.userPtr, image);
}
NVGpaint nvgLinearGradient(NVGcontext* ctx,
float sx, float sy, float ex, float ey,
NVGcolor icol, NVGcolor ocol)
{
NVGpaint p;
float dx, dy, d;
const float large = 1e5;
NVG_NOTUSED(ctx);
memset(&p, 0, sizeof(p));
// Calculate transform aligned to the line
dx = ex - sx;
dy = ey - sy;
d = sqrtf(dx*dx + dy*dy);
if (d > 0.0001f) {
dx /= d;
dy /= d;
} else {
dx = 0;
dy = 1;
}
p.xform[0] = dy; p.xform[1] = -dx;
p.xform[2] = dx; p.xform[3] = dy;
p.xform[4] = sx - dx*large; p.xform[5] = sy - dy*large;
p.extent[0] = large;
p.extent[1] = large + d*0.5f;
p.radius = 0.0f;
p.feather = nvg__maxf(1.0f, d);
p.innerColor = icol;
p.outerColor = ocol;
return p;
}
NVGpaint nvgRadialGradient(NVGcontext* ctx,
float cx, float cy, float inr, float outr,
NVGcolor icol, NVGcolor ocol)
{
NVGpaint p;
float r = (inr+outr)*0.5f;
float f = (outr-inr);
NVG_NOTUSED(ctx);
memset(&p, 0, sizeof(p));
nvgTransformIdentity(p.xform);
p.xform[4] = cx;
p.xform[5] = cy;
p.extent[0] = r;
p.extent[1] = r;
p.radius = r;
p.feather = nvg__maxf(1.0f, f);
p.innerColor = icol;
p.outerColor = ocol;
return p;
}
NVGpaint nvgBoxGradient(NVGcontext* ctx,
float x, float y, float w, float h, float r, float f,
NVGcolor icol, NVGcolor ocol)
{
NVGpaint p;
NVG_NOTUSED(ctx);
memset(&p, 0, sizeof(p));
nvgTransformIdentity(p.xform);
p.xform[4] = x+w*0.5f;
p.xform[5] = y+h*0.5f;
p.extent[0] = w*0.5f;
p.extent[1] = h*0.5f;
p.radius = r;
p.feather = nvg__maxf(1.0f, f);
p.innerColor = icol;
p.outerColor = ocol;
return p;
}
NVGpaint nvgImagePattern(NVGcontext* ctx,
float cx, float cy, float w, float h, float angle,
int image, float alpha)
{
NVGpaint p;
NVG_NOTUSED(ctx);
memset(&p, 0, sizeof(p));
nvgTransformRotate(p.xform, angle);
p.xform[4] = cx;
p.xform[5] = cy;
p.extent[0] = w;
p.extent[1] = h;
p.image = image;
p.innerColor = p.outerColor = nvgRGBAf(1,1,1,alpha);
return p;
}
// Scissoring
void nvgScissor(NVGcontext* ctx, float x, float y, float w, float h)
{
NVGstate* state = nvg__getState(ctx);
w = nvg__maxf(0.0f, w);
h = nvg__maxf(0.0f, h);
nvgTransformIdentity(state->scissor.xform);
state->scissor.xform[4] = x+w*0.5f;
state->scissor.xform[5] = y+h*0.5f;
nvgTransformMultiply(state->scissor.xform, state->xform);
state->scissor.extent[0] = w*0.5f;
state->scissor.extent[1] = h*0.5f;
}
static void nvg__isectRects(float* dst,
float ax, float ay, float aw, float ah,
float bx, float by, float bw, float bh)
{
float minx = nvg__maxf(ax, bx);
float miny = nvg__maxf(ay, by);
float maxx = nvg__minf(ax+aw, bx+bw);
float maxy = nvg__minf(ay+ah, by+bh);
dst[0] = minx;
dst[1] = miny;
dst[2] = nvg__maxf(0.0f, maxx - minx);
dst[3] = nvg__maxf(0.0f, maxy - miny);
}
void nvgIntersectScissor(NVGcontext* ctx, float x, float y, float w, float h)
{
NVGstate* state = nvg__getState(ctx);
float pxform[6], invxorm[6];
float rect[4];
float ex, ey, tex, tey;
// If no previous scissor has been set, set the scissor as current scissor.
if (state->scissor.extent[0] < 0) {
nvgScissor(ctx, x, y, w, h);
return;
}
// Transform the current scissor rect into current transform space.