Repository navigation
Expand file tree
/
Copy pathnanosvgrast.cpp
More file actions
1810 lines (1552 loc) · 50.6 KB
/
Copy pathnanosvgrast.cpp
File metadata and controls
1810 lines (1552 loc) · 50.6 KB
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-14 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.
*
* The polygon rasterization is heavily based on stb_truetype rasterizer
* by Sean Barrett - http://nothings.org/
*
*/
/* Example Usage:
// Load SVG
struct SNVGImage* image = nsvgParseFromFile("test.svg.");
// Create rasterizer (can be used to render multiple images).
struct NSVGrasterizer* rast = nsvgCreateRasterizer();
// Allocate memory for image
UINT8* img = malloc(w*h*4);
// Rasterize
scaleX = width_to_see / design_width
nsvgRasterize(rast, image, 0,0, scaleX, scaleY, img, w, h, w*4);
*/
#include "nanosvg.h"
#include "SvgDebugLog.h"
#include "svgeditor.h"
#ifndef DEBUG_ALL
#define DEBUG_RAST 0
#else
#define DEBUG_RAST DEBUG_ALL
#endif
#if DEBUG_RAST == 0
#define DBG(...)
#else
#define DBG(...) SvgDebugLog(__VA_ARGS__)
#endif
#define pow(x,n) PowF(x,n)
#define sqrtf(x) SqrtF(x)
#define sinf(x) SinF(x)
#define cosf(x) CosF(x)
#define tanf(x) TanF(x)
#define ceilf(x) CeilF(x)
#define floorf(x) FloorF(x)
#define fmodf(x,y) ModF(x,y)
#define acosf(x) AcosF(x)
#define atan2f(y,x) Atan2F(y,x)
#define fabsf(x) FabsF(x)
static void renderShape(NSVGrasterizer* r,
NSVGshape* shape, float *xform, float min_scale);
static int compare_edges(const void* a, const void* b) {
const NSVGedge* ea = (const NSVGedge*)a;
const NSVGedge* eb = (const NSVGedge*)b;
// Сначала по Y0
if (ea->y0 < eb->y0) return -1;
if (ea->y0 > eb->y0) return 1;
// При равных Y0 — по X0 (это делает сортировку стабильной для горизонтальных серпов)
if (ea->x0 < eb->x0) return -1;
if (ea->x0 > eb->x0) return 1;
// При равных X0 — по Y1
if (ea->y1 < eb->y1) return -1;
if (ea->y1 > eb->y1) return 1;
return 0;
}
//caller is responsible for free memory
NSVGrasterizer* nsvg__createRasterizer()
{
NSVGrasterizer* r = (NSVGrasterizer*)AllocateZeroPool(sizeof(NSVGrasterizer));
if (r == NULL) return NULL;
r->tessTol = 0.25f;
r->distTol = 0.01f;
return r;
}
void nsvg__deleteRasterizer(NSVGrasterizer* r)
{
NSVGmemPage* p;
if (r == NULL) return;
p = r->pages;
while (p != NULL) {
NSVGmemPage* next = p->next;
FreePool(p);
p = next;
}
if (r->edges) FreePool(r->edges);
if (r->points) FreePool(r->points);
if (r->points2) FreePool(r->points2);
if (r->scanline) FreePool(r->scanline);
// Очищаем stencil-буферы
if (r->stencilArray != NULL) {
for (int i = 0; i < r->stencilCount; i++) {
if (r->stencilArray[i] != NULL) {
FreePool(r->stencilArray[i]);
}
}
FreePool(r->stencilArray);
r->stencilArray = NULL;
}
FreePool(r);
}
static NSVGmemPage* nsvg__nextPage(NSVGrasterizer* r, NSVGmemPage* cur)
{
NSVGmemPage *newp;
// If using existing chain, return the next page in chain
if (cur != NULL && cur->next != NULL) {
return cur->next;
}
// Alloc new page
newp = (NSVGmemPage*)AllocateZeroPool(sizeof(NSVGmemPage));
if (newp == NULL) return NULL;
// Add to linked list
if (cur != NULL)
cur->next = newp;
else
r->pages = newp;
return newp;
}
static void nsvg__resetPool(NSVGrasterizer* r)
{
NSVGmemPage* p = r->pages;
while (p != NULL) {
p->size = 0;
p = p->next;
}
r->curpage = r->pages;
}
static UINT8* nsvgrast__alloc(NSVGrasterizer* r, int size)
{
UINT8* buf;
if (size > NSVG__MEMPAGE_SIZE) return NULL;
if (r->curpage == NULL || r->curpage->size+size > NSVG__MEMPAGE_SIZE) {
r->curpage = nsvg__nextPage(r, r->curpage);
}
buf = &r->curpage->mem[r->curpage->size];
r->curpage->size += size;
SetMem(buf, size, 0); // ← ОБНУЛЯЕМ ВСЮ ВЫДЕЛЕННУЮ ПАМЯТЬ!
return buf;
}
static int nsvg__ptEquals(NSVGpoint* pt1, NSVGpoint* pt2, float tol)
{
float dx = pt2->x - pt1->x;
float dy = pt2->y - pt1->y;
return SqrF(dx) + SqrF(dy) < SqrF(tol);
}
// t is a matrix xform
static void nsvg__addPathPoint(NSVGrasterizer* r, NSVGpoint* pt, float* t, int flags)
{
NSVGpoint* pt1;
NSVGpoint pt2;
if (!t) {
pt2 = *pt;
} else {
pt2.x = pt->x*t[0] + pt->y*t[2] + t[4];
pt2.y = pt->x*t[1] + pt->y*t[3] + t[5];
}
if (r->npoints > 0) {
pt1 = &r->points[r->npoints-1];
if (nsvg__ptEquals(pt1, &pt2, r->distTol)) {
r->points[r->npoints-1].flags |= (UINT8)flags;
return;
}
}
if (r->npoints+1 > r->cpoints) {
int OldSize = r->cpoints * sizeof(NSVGpoint);
r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64;
if (OldSize == 0) {
r->points = (NSVGpoint*)AllocatePool(64 * sizeof(NSVGpoint));
} else {
r->points = (NSVGpoint*)ReallocatePool(OldSize, sizeof(NSVGpoint) * r->cpoints, r->points);
}
if (r->points == NULL) return;
}
pt1 = &r->points[r->npoints];
pt1->x = pt2.x;
pt1->y = pt2.y;
pt1->flags = (UINT8)flags;
r->npoints++;
}
static void nsvg__appendPathPoint(NSVGrasterizer* r, NSVGpoint* pt)
{
if (r->npoints+1 > r->cpoints) {
int OldSize = r->cpoints * sizeof(NSVGpoint);
r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64;
if (OldSize == 0) {
r->points = (NSVGpoint*)AllocatePool(64 * sizeof(NSVGpoint));
} else
r->points = (NSVGpoint*)ReallocatePool(OldSize, sizeof(NSVGpoint) * r->cpoints, r->points);
if (r->points == NULL) return;
}
r->points[r->npoints] = *pt;
r->npoints++;
}
static void nsvg__duplicatePoints(NSVGrasterizer* r)
{
if (r->npoints > r->cpoints2) {
int OldSize = r->cpoints2 * sizeof(NSVGpoint);
r->cpoints2 = r->npoints;
if (OldSize == 0) {
r->points2 = (NSVGpoint*)AllocatePool(r->npoints * sizeof(NSVGpoint));
} else
r->points2 = (NSVGpoint*)ReallocatePool(OldSize, sizeof(NSVGpoint) * r->cpoints2, r->points2);
if (r->points2 == NULL) return;
}
if (r->npoints) {
memcpy(r->points2, r->points, sizeof(NSVGpoint) * r->npoints);
}
r->npoints2 = r->npoints;
}
static void nsvg__addEdge(NSVGrasterizer* r, float x0, float y0, float x1, float y1)
{
NSVGedge* e;
// Skip horizontal edges
if (y0 == y1)
return;
if (r->nedges+1 > r->cedges) {
int OldSize = r->cedges * sizeof(NSVGedge);
r->cedges = r->cedges > 0 ? r->cedges * 2 : 64;
if (OldSize == 0) {
r->edges = (NSVGedge*)AllocatePool(64 * sizeof(NSVGedge));
} else
r->edges = (NSVGedge*)ReallocatePool(OldSize, sizeof(NSVGedge) * r->cedges, r->edges);
if (r->edges == NULL) return;
}
e = &r->edges[r->nedges];
r->nedges++;
if (y0 < y1) {
e->x0 = x0;
e->y0 = y0;
e->x1 = x1;
e->y1 = y1;
e->dir = 1;
} else {
e->x0 = x1;
e->y0 = y1;
e->x1 = x0;
e->y1 = y0;
e->dir = -1;
}
}
static float nsvg__normalize(float *x, float* y)
{
float d = hypot(*x, *y);
if (d > 1e-6f) {
float id = 1.0f / d;
*x *= id;
*y *= id;
}
return d;
}
#define nsvg__absf(x) FabsF(x)
// 0 1 2 3 4 5 6 7
static float nsvg__controlPathLength(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
{
float l1, l2, l3;
l1 = hypot(x2 - x1, y2 - y1);
l2 = hypot(x3 - x2, y3 - y2);
l3 = hypot(x4 - x3, y4 - y3);
return l1 + l2 + l3;
}
static void nsvg__flattenCubicBez2(NSVGrasterizer* r, float* x, float* t, int type)
{
float ax, ay, bx, by, cx, cy, dx, dy;
float x1, y1, x2, y2, x3, y3, x4, y4;
NSVGpoint p;
float firstFDX, firstFDY, secondFDX, secondFDY, thirdFDX, thirdFDY;
float h, h2, h3;
x1 = x[0]*t[0] + x[1]*t[2] + t[4];
y1 = x[0]*t[1] + x[1]*t[3] + t[5];
x2 = x[2]*t[0] + x[3]*t[2] + t[4];
y2 = x[2]*t[1] + x[3]*t[3] + t[5];
x3 = x[4]*t[0] + x[5]*t[2] + t[4];
y3 = x[4]*t[1] + x[5]*t[3] + t[5];
x4 = x[6]*t[0] + x[7]*t[2] + t[4];
y4 = x[6]*t[1] + x[7]*t[3] + t[5];
float control_path_len = nsvg__controlPathLength(x1, y1, x2, y2, x3, y3, x4, y4);
// Безопасное вычисление N с защитой от переполнения
// int N = (int)(control_path_len / 8.0f) + 4; // было /1.0f + 16
// if (N < 4) N = 4;
// if (N > 256) N = 256; // было 1024
/* This is going to need tweaking, gives approximate same number of divisons
as old code on the test image */
int N = (int)(control_path_len / ( 32 * r->tessTol)) + 2;
/* Compute polynomial coefficients from Bezier points */
ax = -x1 + 3.f * x2 + -3.f * x3 + x4;
ay = -y1 + 3.f * y2 + -3.f * y3 + y4;
bx = 3.f * x1 - 6.f * x2 + 3.f * x3;
by = 3.f * y1 - 6.f * y2 + 3.f * y3;
cx = 3.0f * (x2 - x1); //-3 * x1 + 3 * x2;
cy = 3.0f * (y2 - y1); //-3 * y1 + 3 * y2;
dx = x1;
dy = y1;
/* Set up step size */
h = 1.0f / (N-1);
h2 = h * h;
h3 = h2 * h;
/* Compute forward differences from Bezier points and "h" */
p.x = dx;
p.y = dy;
firstFDX = ((ax * h + bx) * h + cx) * h;
firstFDY = ((ay * h + by) * h + cy) * h;
secondFDX = (6.0f * ax * h + 2.0f * bx) * h2;
secondFDY = (6.0f * ay * h + 2.0f * by) * h2;
thirdFDX = 6.0f * ax * h3;
thirdFDY = 6.0f * ay * h3;
/* Compute points at each step */
for (int i = 0; i < N-1; i++) {
nsvg__addPathPoint(r, &p, NULL, 0);
p.x += firstFDX;
p.y += firstFDY;
firstFDX += secondFDX;
firstFDY += secondFDY;
secondFDX += thirdFDX;
secondFDY += thirdFDY;
}
nsvg__addPathPoint(r, &p, NULL, type);
return;
}
static void nsvg__flattenShape(NSVGrasterizer* r, NSVGshape* shape, float* xform)
{
NSVGpath* path;
NSVGpoint pt;
//DBG("nsvg__flattenShape\n");
DBG("xform: %f %f %f %f %f %f\n", xform[0], xform[1], xform[2], xform[3], xform[4], xform[5]);
for (path = shape->paths; path != NULL; path = path->next) {
r->npoints = 0;
// Flatten path
pt.x = path->pts[0];
pt.y = path->pts[1];
nsvg__addPathPoint(r, &pt, xform, 0);
for (int i = 0; i < path->npts-1; i += 3) {
float* p = &path->pts[i*2];
nsvg__flattenCubicBez2(r, p, xform, 0);
}
// Close path
nsvg__addPathPoint(r, &pt, xform, 0);
// Build edges
for (int i = 0, j = r->npoints-1; i < r->npoints; j = i++)
nsvg__addEdge(r, r->points[j].x, r->points[j].y, r->points[i].x, r->points[i].y);
}
// DBG("nsvg__flattenShape: created %d edges for shape %s\n", r->nedges, shape->id);
}
enum NSVGpointFlags
{
NSVG_PT_CORNER = 0x01,
NSVG_PT_BEVEL = 0x02,
NSVG_PT_LEFT = 0x04
};
static void nsvg__initClosed(NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth)
{
float w = lineWidth * 0.5f;
float dx = p1->x - p0->x;
float dy = p1->y - p0->y;
float len = nsvg__normalize(&dx, &dy);
float px = p0->x + dx*len*0.5f, py = p0->y + dy*len*0.5f;
float dlx = dy, dly = -dx;
float lx = px - dlx*w, ly = py - dly*w;
float rx = px + dlx*w, ry = py + dly*w;
left->x = lx; left->y = ly;
right->x = rx; right->y = ry;
}
static void nsvg__buttCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect)
{
float w = lineWidth * 0.5f;
float px = p->x, py = p->y;
float dlx = dy, dly = -dx;
float lx = px - dlx*w, ly = py - dly*w;
float rx = px + dlx*w, ry = py + dly*w;
nsvg__addEdge(r, lx, ly, rx, ry);
if (connect) {
nsvg__addEdge(r, left->x, left->y, lx, ly);
nsvg__addEdge(r, rx, ry, right->x, right->y);
}
left->x = lx; left->y = ly;
right->x = rx; right->y = ry;
}
static void nsvg__squareCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect)
{
float w = lineWidth * 0.5f;
float px = p->x - dx*w, py = p->y - dy*w;
float dlx = dy, dly = -dx;
float lx = px - dlx*w, ly = py - dly*w;
float rx = px + dlx*w, ry = py + dly*w;
nsvg__addEdge(r, lx, ly, rx, ry);
if (connect) {
nsvg__addEdge(r, left->x, left->y, lx, ly);
nsvg__addEdge(r, rx, ry, right->x, right->y);
}
left->x = lx; left->y = ly;
right->x = rx; right->y = ry;
}
#ifndef NSVG_PI
const float NSVG_PI = 3.141592653589793f;
#endif
static void nsvg__roundCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int ncap, int connect)
{
float w = lineWidth * 0.5f;
float px = p->x, py = p->y;
float dlx = dy, dly = -dx;
float lx = 0, ly = 0, rx = 0, ry = 0, prevx = 0, prevy = 0;
for (int i = 0; i < ncap; i++) {
float a = (float)i/(float)(ncap-1)*NSVG_PI;
float ax = cosf(a) * w, ay = sinf(a) * w;
float x = px - dlx*ax - dx*ay;
float y = py - dly*ax - dy*ay;
if (i > 0)
nsvg__addEdge(r, prevx, prevy, x, y);
prevx = x;
prevy = y;
if (i == 0) {
lx = x; ly = y;
} else if (i == ncap-1) {
rx = x; ry = y;
}
}
if (connect) {
nsvg__addEdge(r, left->x, left->y, lx, ly);
nsvg__addEdge(r, rx, ry, right->x, right->y);
}
left->x = lx; left->y = ly;
right->x = rx; right->y = ry;
}
static void nsvg__bevelJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth)
{
float w = lineWidth * 0.5f;
float dlx0 = p0->dy, dly0 = -p0->dx;
float dlx1 = p1->dy, dly1 = -p1->dx;
float lx0 = p1->x - (dlx0 * w), ly0 = p1->y - (dly0 * w);
float rx0 = p1->x + (dlx0 * w), ry0 = p1->y + (dly0 * w);
float lx1 = p1->x - (dlx1 * w), ly1 = p1->y - (dly1 * w);
float rx1 = p1->x + (dlx1 * w), ry1 = p1->y + (dly1 * w);
nsvg__addEdge(r, lx0, ly0, left->x, left->y);
nsvg__addEdge(r, lx1, ly1, lx0, ly0);
nsvg__addEdge(r, right->x, right->y, rx0, ry0);
nsvg__addEdge(r, rx0, ry0, rx1, ry1);
left->x = lx1; left->y = ly1;
right->x = rx1; right->y = ry1;
}
static void nsvg__miterJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth)
{
float w = lineWidth * 0.5f;
float dlx0 = p0->dy, dly0 = -p0->dx;
float dlx1 = p1->dy, dly1 = -p1->dx;
float lx0, rx0, lx1, rx1;
float ly0, ry0, ly1, ry1;
if (p1->flags & NSVG_PT_LEFT) {
lx0 = lx1 = p1->x - p1->dmx * w;
ly0 = ly1 = p1->y - p1->dmy * w;
nsvg__addEdge(r, lx1, ly1, left->x, left->y);
rx0 = p1->x + (dlx0 * w);
ry0 = p1->y + (dly0 * w);
rx1 = p1->x + (dlx1 * w);
ry1 = p1->y + (dly1 * w);
nsvg__addEdge(r, right->x, right->y, rx0, ry0);
nsvg__addEdge(r, rx0, ry0, rx1, ry1);
} else {
lx0 = p1->x - (dlx0 * w);
ly0 = p1->y - (dly0 * w);
lx1 = p1->x - (dlx1 * w);
ly1 = p1->y - (dly1 * w);
nsvg__addEdge(r, lx0, ly0, left->x, left->y);
nsvg__addEdge(r, lx1, ly1, lx0, ly0);
rx0 = rx1 = p1->x + p1->dmx * w;
ry0 = ry1 = p1->y + p1->dmy * w;
nsvg__addEdge(r, right->x, right->y, rx1, ry1);
}
left->x = lx1; left->y = ly1;
right->x = rx1; right->y = ry1;
}
static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth, int ncap)
{
int n;
float w = lineWidth * 0.5f;
float dlx0 = p0->dy, dly0 = -p0->dx;
float dlx1 = p1->dy, dly1 = -p1->dx;
float a0 = atan2f(dly0, dlx0);
float a1 = atan2f(dly1, dlx1);
float da = a1 - a0;
float lx, ly, rx, ry;
if (da < -NSVG_PI) da += PI2; //NSVG_PI*2;
if (da > NSVG_PI) da -= PI2; //NSVG_PI*2;
n = (int)ceilf((nsvg__absf(da) / NSVG_PI) * (float)ncap);
if (n < 2) n = 2;
if (n > ncap) n = ncap;
lx = left->x;
ly = left->y;
rx = right->x;
ry = right->y;
for (int i = 0; i < n; i++) {
float u = (float)i/(float)(n-1);
float a = a0 + u*da;
float ax = cosf(a) * w, ay = sinf(a) * w;
float lx1 = p1->x - ax, ly1 = p1->y - ay;
float rx1 = p1->x + ax, ry1 = p1->y + ay;
nsvg__addEdge(r, lx1, ly1, lx, ly);
nsvg__addEdge(r, rx, ry, rx1, ry1);
lx = lx1; ly = ly1;
rx = rx1; ry = ry1;
}
left->x = lx; left->y = ly;
right->x = rx; right->y = ry;
}
static void nsvg__straightJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p1, float lineWidth)
{
float w = lineWidth * 0.5f;
float lx = p1->x - (p1->dmx * w), ly = p1->y - (p1->dmy * w);
float rx = p1->x + (p1->dmx * w), ry = p1->y + (p1->dmy * w);
nsvg__addEdge(r, lx, ly, left->x, left->y);
nsvg__addEdge(r, right->x, right->y, rx, ry);
left->x = lx; left->y = ly;
right->x = rx; right->y = ry;
}
static int nsvg__curveDivs(float r, float arc, float tol)
{
float da = acosf(r / (r + tol)) * 2.0f;
int divs = (int)ceilf(arc / da);
if (divs < 2) divs = 2;
return divs;
}
static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points, int npoints, int closed, int lineJoin, int lineCap, float lineWidth)
{
int ncap = nsvg__curveDivs(lineWidth*0.5f, NSVG_PI, r->tessTol); // Calculate divisions per half circle.
NSVGpoint left = {0,0,0,0,0,0,0,0,{0,0,0}}, right = {0,0,0,0,0,0,0,0,{0,0,0}}, firstLeft = {0,0,0,0,0,0,0,0,{0,0,0}}, firstRight = {0,0,0,0,0,0,0,0,{0,0,0}};
NSVGpoint* p0, *p1;
int s, e;
// Build stroke edges
if (closed) {
// Looping
p0 = &points[npoints-1];
p1 = &points[0];
s = 0;
e = npoints;
} else {
// Add cap
p0 = &points[0];
p1 = &points[1];
s = 1;
e = npoints-1;
}
if (closed) {
nsvg__initClosed(&left, &right, p0, p1, lineWidth);
firstLeft = left;
firstRight = right;
} else {
// Add cap
float dx = p1->x - p0->x;
float dy = p1->y - p0->y;
nsvg__normalize(&dx, &dy);
if (lineCap == NSVG_CAP_BUTT)
nsvg__buttCap(r, &left, &right, p0, dx, dy, lineWidth, 0);
else if (lineCap == NSVG_CAP_SQUARE)
nsvg__squareCap(r, &left, &right, p0, dx, dy, lineWidth, 0);
else if (lineCap == NSVG_CAP_ROUND)
nsvg__roundCap(r, &left, &right, p0, dx, dy, lineWidth, ncap, 0);
}
for (int j = s; j < e; ++j) {
if (p1->flags & NSVG_PT_CORNER) {
if (lineJoin == NSVG_JOIN_ROUND)
nsvg__roundJoin(r, &left, &right, p0, p1, lineWidth, ncap);
else if (lineJoin == NSVG_JOIN_BEVEL || (p1->flags & NSVG_PT_BEVEL))
nsvg__bevelJoin(r, &left, &right, p0, p1, lineWidth);
else
nsvg__miterJoin(r, &left, &right, p0, p1, lineWidth);
} else {
nsvg__straightJoin(r, &left, &right, p1, lineWidth);
}
p0 = p1++;
}
if (closed) {
// Loop it
nsvg__addEdge(r, firstLeft.x, firstLeft.y, left.x, left.y);
nsvg__addEdge(r, right.x, right.y, firstRight.x, firstRight.y);
} else {
// Add cap
float dx = p1->x - p0->x;
float dy = p1->y - p0->y;
nsvg__normalize(&dx, &dy);
if (lineCap == NSVG_CAP_BUTT)
nsvg__buttCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1);
else if (lineCap == NSVG_CAP_SQUARE)
nsvg__squareCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1);
else if (lineCap == NSVG_CAP_ROUND)
nsvg__roundCap(r, &right, &left, p1, -dx, -dy, lineWidth, ncap, 1);
}
}
static void nsvg__prepareStroke(NSVGrasterizer* r, float miterLimit, int lineJoin)
{
NSVGpoint *p0, *p1;
p0 = &r->points[r->npoints-1];
p1 = &r->points[0];
for (int i = 0; i < r->npoints; i++) {
// Calculate segment direction and length
p0->dx = p1->x - p0->x;
p0->dy = p1->y - p0->y;
p0->len = nsvg__normalize(&p0->dx, &p0->dy);
// Advance
p0 = p1++;
}
// calculate joins
p0 = &r->points[r->npoints-1];
p1 = &r->points[0];
for (int j = 0; j < r->npoints; j++) {
float dlx0, dly0, dlx1, dly1, dmr2, cross;
dlx0 = p0->dy;
dly0 = -p0->dx;
dlx1 = p1->dy;
dly1 = -p1->dx;
// Calculate extrusions
p1->dmx = (dlx0 + dlx1) * 0.5f;
p1->dmy = (dly0 + dly1) * 0.5f;
dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy;
if (dmr2 > 0.000001f) {
float s2 = 1.0f / dmr2;
if (s2 > 600.0f) {
s2 = 600.0f;
}
p1->dmx *= s2;
p1->dmy *= s2;
}
// Clear flags, but keep the corner.
p1->flags = (p1->flags & NSVG_PT_CORNER) ? NSVG_PT_CORNER : 0;
// Keep track of left turns.
cross = p1->dx * p0->dy - p0->dx * p1->dy;
if (cross > 0.0f)
p1->flags |= NSVG_PT_LEFT;
// Check to see if the corner needs to be beveled.
if (p1->flags & NSVG_PT_CORNER) {
if ((dmr2 * miterLimit*miterLimit) < 1.0f || lineJoin == NSVG_JOIN_BEVEL || lineJoin == NSVG_JOIN_ROUND) {
p1->flags |= NSVG_PT_BEVEL;
}
}
p0 = p1++;
}
}
static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float* xform)
{
int closed;
NSVGpath* path;
NSVGpoint* p0, *p1;
NSVGpoint p;
float lineWidth = 0.5;
float scalex1 = fabsf(xform[0]);
float scalex2 = fabsf(xform[2]);
float scaley1 = fabsf(xform[1]);
float scaley2 = fabsf(xform[3]);
float scale = (hypot(scalex1, scalex2) + hypot(scaley1, scaley2)) * 0.5f;
float miterLimit = shape->miterLimit;
int lineJoin = shape->strokeLineJoin;
int lineCap = shape->strokeLineCap;
lineWidth = shape->strokeWidth * scale;
for (path = shape->paths; path != NULL; path = path->next) {
// Flatten path
r->npoints = 0;
p.x = path->pts[0];
p.y = path->pts[1];
nsvg__addPathPoint(r, &p, xform, NSVG_PT_CORNER);
for (int i = 0; i < path->npts-1; i += 3) {
float* pt = &path->pts[i*2];
nsvg__flattenCubicBez2(r, pt, xform, NSVG_PT_CORNER);
}
if (r->npoints < 2)
continue;
closed = path->closed;
// If the first and last points are the same, remove the last, mark as closed path.
p0 = &r->points[r->npoints-1];
p1 = &r->points[0];
if (nsvg__ptEquals(p0, p1, r->distTol)) {
r->npoints--;
p0 = &r->points[r->npoints-1];
closed = 1;
}
if (shape->strokeDashCount > 0) {
int idash = 0, dashState = 1;
float totalDist = 0, dashLen, allDashLen, dashOffset;
NSVGpoint* cur;
if (closed)
nsvg__appendPathPoint(r, p1);
// Duplicate points -> points2.
nsvg__duplicatePoints(r);
r->npoints = 0;
cur = &r->points2[0];
nsvg__appendPathPoint(r, cur);
// Figure out dash offset.
allDashLen = 0;
for (int j = 0; j < shape->strokeDashCount; j++)
allDashLen += shape->strokeDashArray[j];
if (shape->strokeDashCount & 1)
allDashLen *= 2.0f;
// Find location inside pattern
dashOffset = fmodf(shape->strokeDashOffset, allDashLen);
if (dashOffset < 0.0f)
dashOffset += allDashLen;
while (dashOffset > shape->strokeDashArray[idash]) {
dashOffset -= shape->strokeDashArray[idash];
idash = (idash + 1) % shape->strokeDashCount;
}
dashLen = (shape->strokeDashArray[idash] - dashOffset) * scale;
for (int j = 1; j < r->npoints2; ) {
float dx = r->points2[j].x - cur->x;
float dy = r->points2[j].y - cur->y;
float dist = hypot(dx, dy);
if ((totalDist + dist) > dashLen) {
// Calculate intermediate point
float d = (dashLen - totalDist) / dist;
NSVGpoint pc;
pc.x = cur->x + dx * d;
pc.y = cur->y + dy * d;
nsvg__addPathPoint(r, &pc, NULL, NSVG_PT_CORNER);
// Stroke
if (r->npoints > 1 && dashState) {
nsvg__prepareStroke(r, miterLimit, lineJoin);
nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth);
}
// Advance dash pattern
dashState = !dashState;
idash = (idash+1) % shape->strokeDashCount;
dashLen = shape->strokeDashArray[idash] * scale;
// Restart
cur->x = pc.x;
cur->y = pc.y;
cur->flags = NSVG_PT_CORNER;
totalDist = 0.0f;
r->npoints = 0;
nsvg__appendPathPoint(r, cur);
} else {
totalDist += dist;
cur = &r->points2[j];
nsvg__appendPathPoint(r, cur);
j++;
}
}
// Stroke any leftover path
if (r->npoints > 1 && dashState)
nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth);
} else {
nsvg__prepareStroke(r, miterLimit, lineJoin);
nsvg__expandStroke(r, r->points, r->npoints, closed, lineJoin, lineCap, lineWidth);
}
}
}
static NSVGactiveEdge* nsvg__addActive(NSVGrasterizer* r, NSVGedge* e, float startPoint)
{
NSVGactiveEdge* z;
if (r->freelist != NULL) {
// Restore from freelist.
z = r->freelist;
r->freelist = z->next;
} else {
// Alloc new edge.
z = (NSVGactiveEdge*)nsvgrast__alloc(r, sizeof(NSVGactiveEdge));
if (z == NULL) return NULL;
}
float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
// STBTT_assert(e->y0 <= start_point);
// round dx down to avoid going too far
if (dxdy < 0)
z->dx = (int)(-floorf(NSVG__FIX * -dxdy));
else
z->dx = (int)floorf(NSVG__FIX * dxdy);
z->x = (int)floorf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)));
// z->x -= off_x * FIX;
z->ey = e->y1;
z->next = 0;
z->dir = e->dir;
return z;
}
static void nsvg__freeActive(NSVGrasterizer* r, NSVGactiveEdge* z)
{
z->next = r->freelist;
r->freelist = z;
}
// Доступ к stencil-буферу - O(1)!
static UINT8* getStencil(NSVGrasterizer* r, NSVGclipPathIndex index, int y)
{
// Проверяем, что индекс в пределах массива
if (index >= r->stencilCount) {
DBG("WARNING: clip index %d out of range (max %d)\n", index, r->stencilCount);
return NULL;
}
// Проверяем, что stencil-буфер существует
if (r->stencilArray[index] == NULL) {
DBG("WARNING: stencil buffer for index %d is NULL\n", index);
return NULL;
}
// Возвращаем указатель на строку y в stencil-буфере
return &r->stencilArray[index][y * r->stencilStride];
}
static void nsvg__fillScanline(UINT8* scanline, int len, int x0, int x1, int maxWeight, int* xmin, int* xmax)
{
// DBG("nsvg__fillScanline\n");
int i = x0 >> NSVG__FIXSHIFT;
int j = x1 >> NSVG__FIXSHIFT;
if (i < *xmin) *xmin = i;
if (j > *xmax) *xmax = j;
if (i < len && j >= 0) {
if (i == j) {
// x0,x1 are the same pixel, so compute combined coverage
scanline[i] = (UINT8)(scanline[i] + ((x1 - x0) * maxWeight >> NSVG__FIXSHIFT));
} else {
if (i >= 0) // add antialiasing for x0
scanline[i] = (UINT8)(scanline[i] + (((NSVG__FIX - (x0 & NSVG__FIXMASK)) * maxWeight) >> NSVG__FIXSHIFT));
else
i = -1; // clip
if (j < len) // add antialiasing for x1
scanline[j] = (UINT8)(scanline[j] + (((x1 & NSVG__FIXMASK) * maxWeight) >> NSVG__FIXSHIFT));
else
j = len; // clip
for (++i; i < j; ++i) // fill pixels between x0 and x1
scanline[i] = (UINT8)(scanline[i] + maxWeight);
}
}
}
// note: this routine clips fills that extend off the edges... ideally this
// wouldn't happen, but it could happen if the truetype glyph bounding boxes
// are wrong, or if the user supplies a too-small bitmap
static void nsvg__fillActiveEdges(UINT8 *scanline, int len, NSVGactiveEdge *e,
int maxWeight, int *xmin, int *xmax, char fillRule)
{
// non-zero winding fill
// DBG("nsvg__fillActiveEdges\n");
int x0 = 0, w = 0;
// int iteration = 0;
// const int MAX_ITERATIONS = 5000;
if (fillRule == NSVG_FILLRULE_NONZERO) {
// Non-zero
while (e != NULL) {
if (w == 0) {
// if we're currently at zero, we need to record the edge start point
x0 = e->x;
w += e->dir;
}
else {
int x1 = e->x;