-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayerExDraw.cpp
More file actions
2733 lines (2389 loc) · 94.1 KB
/
Copy pathLayerExDraw.cpp
File metadata and controls
2733 lines (2389 loc) · 94.1 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
#include "ncbind.hpp"
#include "LayerExDraw.hpp"
#include <vector>
#include <string>
#include <stdio.h>
#include <cmath>
#include <map>
#include <cstring>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// thorvg 初期化
#ifdef LAYEREXVECTOR_TVG_GW
#include "thorvg_gw_bridge.h"
static tvg::Matrix mulMatrix(const tvg::Matrix &a, const tvg::Matrix &b);
#endif
extern void RegisterLayerExVectorLicenses(); // LicensesGen.cpp (生成物)
void initThorvg()
{
// 同梱コンポーネント (ThorVG 等) のライセンス文を本体へ登録する
// (System.getLicenseList / getLicenseText で参照可能になる)
RegisterLayerExVectorLicenses();
// 利用するスレッド数を指定 (0 ならメインスレッドのみ、1 以上ならその数だけワーカースレッドを起動)
tvg::Initializer::init(4);
#ifdef LAYEREXVECTOR_TVG_GW
// gw テキストローダ使用時: 本体の glyphware ブリッジをこの DLL 内の
// thorvg コピーへ注入する (thorvg を静的リンクしているため、exe 側で
// 登録されたブリッジは DLL 側のグローバルには反映されない)
tvgGwSetBridge(static_cast<const TvgGwBridge*>(TVPGetFontTvgBridge()));
#endif
}
// thorvg 終了
void deInitThorvg()
{
tvg::Initializer::term();
}
// --------------------------------------------------------
// ユーティリティ関数
// --------------------------------------------------------
extern bool IsArray(const tTJSVariant &var);
extern PointF getPoint(const tTJSVariant &var);
void getPoints(const tTJSVariant &var, vector<PointF> &points)
{
ncbPropAccessor info(var);
int c = info.GetArrayCount();
for (int i = 0; i < c; i++) {
tTJSVariant p;
if (info.checkVariant(i, p)) {
points.push_back(getPoint(p));
}
}
}
extern RectF getRect(const tTJSVariant &var);
void getRects(const tTJSVariant &var, vector<RectF> &rects)
{
ncbPropAccessor info(var);
int c = info.GetArrayCount();
for (int i = 0; i < c; i++) {
tTJSVariant p;
if (info.checkVariant(i, p)) {
rects.push_back(getRect(p));
}
}
}
static void getReals(const tTJSVariant &var, vector<REAL> &points)
{
ncbPropAccessor info(var);
int c = info.GetArrayCount();
for (int i = 0; i < c; i++) {
points.push_back((REAL)info.getRealValue(i));
}
}
// --------------------------------------------------------
// グラデーションの GDI+ 互換 (rect / mode / angle / wrapMode / blend 系)
// --------------------------------------------------------
static tvg::Fill::ColorStop makeStop(REAL offset, ARGB c)
{
tvg::Fill::ColorStop s;
s.offset = offset;
s.a = (c >> 24) & 0xFF;
s.r = (c >> 16) & 0xFF;
s.g = (c >> 8) & 0xFF;
s.b = c & 0xFF;
return s;
}
static ARGB lerpARGB(ARGB c1, ARGB c2, REAL t)
{
if (t < 0) t = 0;
if (t > 1) t = 1;
ARGB r = 0;
for (int sh = 0; sh <= 24; sh += 8) {
int a = (c1 >> sh) & 0xFF, b = (c2 >> sh) & 0xFF;
r |= (ARGB)((int)(a + (b - a) * t + 0.5f) & 0xFF) << sh;
}
return r;
}
// GDI+ の WrapMode → ThorVG の FillSpread
static tvg::FillSpread toSpread(int wrapMode)
{
switch (wrapMode) {
case WrapModeTileFlipX:
case WrapModeTileFlipY:
case WrapModeTileFlipXY: return tvg::FillSpread::Reflect;
case WrapModeClamp: return tvg::FillSpread::Pad;
default: return tvg::FillSpread::Repeat; // WrapModeTile (GDI+ の既定)
}
}
// LinearGradientBrush(rect, c1, c2, angle, isAngleScalable) と同じ始点・終点を求める。
// 角度方向の単位ベクトルへ矩形の 4 隅を射影し、最小 → 最大を色 1 → 色 2 にする。
// isAngleScalable は角度を矩形の縦横比で補正する (tanθ' = tanθ * w / h)。
// mode 指定は 横 0° / 縦 90° / 順斜め 45° / 逆斜め 135° (斜めは縦横比で補正)
static void linearPointsFromRect(const RectF &rect, REAL angle, bool scalable,
REAL &x1, REAL &y1, REAL &x2, REAL &y2)
{
double rad = angle * M_PI / 180.0;
double dx = cos(rad), dy = sin(rad);
if (scalable) {
dx *= rect.Height;
dy *= rect.Width;
}
double len = sqrt(dx * dx + dy * dy);
if (len <= 0) { dx = 1; dy = 0; len = 1; }
dx /= len; dy /= len;
double cx[4] = { rect.X, rect.X + rect.Width, rect.X, rect.X + rect.Width };
double cy[4] = { rect.Y, rect.Y, rect.Y + rect.Height, rect.Y + rect.Height };
double tmin = 0, tmax = 0;
for (int i = 0; i < 4; i++) {
double t = cx[i] * dx + cy[i] * dy;
if (i == 0 || t < tmin) tmin = t;
if (i == 0 || t > tmax) tmax = t;
}
x1 = (REAL)(dx * tmin); y1 = (REAL)(dy * tmin);
x2 = (REAL)(dx * tmax); y2 = (REAL)(dy * tmax);
}
static void readReals(ncbPropAccessor &acc, tjs_int index, vector<REAL> &out)
{
tTJSVariant v;
if (acc.checkVariant(index, v) && v.Type() == tvtObject) getReals(v, out);
}
static void readReals(ncbPropAccessor &acc, const tjs_char *key, vector<REAL> &out)
{
tTJSVariant v;
if (acc.checkVariant(key, v) && v.Type() == tvtObject) getReals(v, out);
}
static void readColors(const tTJSVariant &var, vector<ARGB> &out)
{
ncbPropAccessor info(var);
int c = info.GetArrayCount();
for (int i = 0; i < c; i++) out.push_back((ARGB)info.getIntValue(i));
}
// GDI+ の色の配分指定 (commonBrushParameter) をカラーストップへ置き換える。
// blend … 位置ごとの係数 (0=色1 / 1=色2)
// blendBellShape … 釣鐘形 (正規分布) を 33 点で近似
// blendTriangularShape… 三角形 (focus で色 2 に達し両端は色 1)
// interpolationColors … 位置ごとの色 (多色)
// ⚠ useGammaCorrection は再現しない (受けるだけ)
static void applyBlendParams(ncbPropAccessor &info, ARGB c1, ARGB c2, vector<tvg::Fill::ColorStop> &stops)
{
tTJSVariant var;
vector<tvg::Fill::ColorStop> r;
if (info.checkVariant(TJS_W("interpolationColors"), var) && var.Type() == tvtObject) {
vector<ARGB> colors;
vector<REAL> pos;
ncbPropAccessor b(var);
if (IsArray(var)) {
tTJSVariant cv;
if (b.checkVariant((tjs_int)0, cv) && cv.Type() == tvtObject) readColors(cv, colors);
readReals(b, (tjs_int)1, pos);
} else {
tTJSVariant cv;
if (b.checkVariant(TJS_W("presetColors"), cv) && cv.Type() == tvtObject) readColors(cv, colors);
readReals(b, TJS_W("blendPositions"), pos);
}
size_t n = colors.size() < pos.size() ? colors.size() : pos.size();
for (size_t i = 0; i < n; i++) r.push_back(makeStop(pos[i], colors[i]));
} else if (info.checkVariant(TJS_W("blend"), var) && var.Type() == tvtObject) {
vector<REAL> factors, pos;
ncbPropAccessor b(var);
if (IsArray(var)) {
readReals(b, (tjs_int)0, factors);
readReals(b, (tjs_int)1, pos);
} else {
readReals(b, TJS_W("blendFactors"), factors);
readReals(b, TJS_W("blendPositions"), pos);
}
size_t n = factors.size() < pos.size() ? factors.size() : pos.size();
for (size_t i = 0; i < n; i++) r.push_back(makeStop(pos[i], lerpARGB(c1, c2, factors[i])));
} else if (info.checkVariant(TJS_W("blendTriangularShape"), var)) {
REAL focus, scale;
if (var.Type() == tvtObject && IsArray(var)) {
ncbPropAccessor s(var);
focus = (REAL)s.getRealValue(0); scale = (REAL)s.getRealValue(1, 1.0);
} else {
focus = (REAL)info.getRealValue(TJS_W("focus"), 0.5); scale = (REAL)info.getRealValue(TJS_W("scale"), 1.0);
}
if (focus > 0) r.push_back(makeStop(0, c1));
r.push_back(makeStop(focus, lerpARGB(c1, c2, scale)));
if (focus < 1) r.push_back(makeStop(1, c1));
} else if (info.checkVariant(TJS_W("blendBellShape"), var)) {
REAL focus, scale;
if (var.Type() == tvtObject && IsArray(var)) {
ncbPropAccessor s(var);
focus = (REAL)s.getRealValue(0); scale = (REAL)s.getRealValue(1, 1.0);
} else {
focus = (REAL)info.getRealValue(TJS_W("focus"), 0.5); scale = (REAL)info.getRealValue(TJS_W("scale"), 1.0);
}
// focus で scale に達する釣鐘。 両側をそれぞれ正規分布の累積で 0 → 1 に寄せる
const int N = 33;
for (int i = 0; i < N; i++) {
REAL t = (REAL)i / (N - 1);
double f;
if (t <= focus) { double u = focus > 0 ? t / focus : 1; f = 0.5 * (1 + erf((u - 0.5) * 2 * 1.5)); }
else { double u = focus < 1 ? (1 - t) / (1 - focus) : 1; f = 0.5 * (1 + erf((u - 0.5) * 2 * 1.5)); }
r.push_back(makeStop(t, lerpARGB(c1, c2, (REAL)(f * scale))));
}
}
if (r.size() >= 2) stops = r;
}
// --------------------------------------------------------
// パスグラデーション (GDI+ PathGradientBrush 互換)
// --------------------------------------------------------
// blend 系の指定 (位置ごとの係数) を表にする。 interpolationColors は色の表。
// パスグラデーションの位置は 0 = 外周 / 1 = 中心 (GDI+ と同じ)
static void readPathBlend(ncbPropAccessor &info, Appearance::DrawInfo &d)
{
tTJSVariant var;
if (info.checkVariant(TJS_W("interpolationColors"), var) && var.Type() == tvtObject) {
vector<ARGB> colors; vector<REAL> pos;
ncbPropAccessor b(var);
tTJSVariant cv;
if (IsArray(var)) {
if (b.checkVariant((tjs_int)0, cv) && cv.Type() == tvtObject) readColors(cv, colors);
readReals(b, (tjs_int)1, pos);
} else {
if (b.checkVariant(TJS_W("presetColors"), cv) && cv.Type() == tvtObject) readColors(cv, colors);
readReals(b, TJS_W("blendPositions"), pos);
}
size_t n = colors.size() < pos.size() ? colors.size() : pos.size();
for (size_t i = 0; i < n; i++) { d.pgPresetPos.push_back(pos[i]); d.pgPresetColors.push_back(colors[i]); }
} else if (info.checkVariant(TJS_W("blend"), var) && var.Type() == tvtObject) {
vector<REAL> fac, pos;
ncbPropAccessor b(var);
if (IsArray(var)) { readReals(b, (tjs_int)0, fac); readReals(b, (tjs_int)1, pos); }
else { readReals(b, TJS_W("blendFactors"), fac); readReals(b, TJS_W("blendPositions"), pos); }
size_t n = fac.size() < pos.size() ? fac.size() : pos.size();
for (size_t i = 0; i < n; i++) { d.pgBlendPos.push_back(pos[i]); d.pgBlendFac.push_back(fac[i]); }
} else if (info.checkVariant(TJS_W("blendTriangularShape"), var) || info.checkVariant(TJS_W("blendBellShape"), var)) {
bool bell = !info.HasValue(TJS_W("blendTriangularShape"));
REAL focus, scale;
if (var.Type() == tvtObject && IsArray(var)) {
ncbPropAccessor s(var);
focus = (REAL)s.getRealValue(0); scale = (REAL)s.getRealValue(1, 1.0);
} else {
focus = (REAL)info.getRealValue(TJS_W("focus"), 0.5); scale = (REAL)info.getRealValue(TJS_W("scale"), 1.0);
}
const int N = bell ? 33 : 3;
for (int i = 0; i < N; i++) {
REAL t = bell ? (REAL)i / (N - 1) : (i == 0 ? 0 : i == 1 ? focus : 1);
double f;
if (!bell) f = (i == 1) ? 1 : 0;
else if (t <= focus) { double u = focus > 0 ? t / focus : 1; f = 0.5 * (1 + erf((u - 0.5) * 2 * 1.5)); }
else { double u = focus < 1 ? (1 - t) / (1 - focus) : 1; f = 0.5 * (1 + erf((u - 0.5) * 2 * 1.5)); }
d.pgBlendPos.push_back(t); d.pgBlendFac.push_back((REAL)(f * scale));
}
}
}
// GDI+ の PathGradientBrush と同じ指定を受ける。
// points (必須) / surroundColors (既定 白、足りなければ最後を繰り返す) /
// centerColor (既定 黒) / centerPoint (既定 頂点の平均) / focusScales /
// wrapMode (既定 Tile) / blend 系 / interpolationColors
static void parsePathGradient(ncbPropAccessor &info, const tTJSVariant &pointsVar, Appearance::DrawInfo &d)
{
ncbPropAccessor pts(pointsVar);
int n = pts.GetArrayCount();
for (int i = 0; i < n; i++) {
tTJSVariant pv;
if (!pts.checkVariant((tjs_int)i, pv)) continue;
PointF p = getPoint(pv);
d.pgX.push_back(p.X); d.pgY.push_back(p.Y);
}
if (d.pgX.size() < 2) return;
d.usePathGradient = true;
tTJSVariant var;
if (info.checkVariant(TJS_W("surroundColors"), var) && var.Type() == tvtObject) readColors(var, d.pgColors);
if (d.pgColors.empty()) d.pgColors.push_back(0xFFFFFFFF);
while (d.pgColors.size() < d.pgX.size()) d.pgColors.push_back(d.pgColors.back());
d.pgCenterColor = (ARGB)info.getIntValue(TJS_W("centerColor"), (tjs_int)0xFF000000);
if (info.checkVariant(TJS_W("centerPoint"), var)) {
PointF c = getPoint(var);
d.pgCx = c.X; d.pgCy = c.Y;
} else {
double sx = 0, sy = 0;
for (size_t i = 0; i < d.pgX.size(); i++) { sx += d.pgX[i]; sy += d.pgY[i]; }
d.pgCx = (REAL)(sx / d.pgX.size()); d.pgCy = (REAL)(sy / d.pgY.size());
}
if (info.checkVariant(TJS_W("focusScales"), var)) {
if (var.Type() == tvtObject && IsArray(var)) {
ncbPropAccessor s(var);
d.pgFocusX = (REAL)s.getRealValue(0); d.pgFocusY = (REAL)s.getRealValue(1);
}
} else if (info.HasValue(TJS_W("xScale")) || info.HasValue(TJS_W("yScale"))) {
d.pgFocusX = (REAL)info.getRealValue(TJS_W("xScale")); d.pgFocusY = (REAL)info.getRealValue(TJS_W("yScale"));
}
d.pgWrap = (int)info.getIntValue(TJS_W("wrapMode"), WrapModeTile);
readPathBlend(info, d);
}
// 表の位置 p (0..1) での値を線形補間で引く
static REAL lookupTable(const vector<REAL> &pos, const vector<REAL> &val, REAL p)
{
size_t n = pos.size();
if (n == 0) return p;
if (p <= pos[0]) return val[0];
for (size_t i = 1; i < n; i++) {
if (p <= pos[i]) {
REAL span = pos[i] - pos[i-1];
REAL t = span > 0 ? (p - pos[i-1]) / span : 0;
return val[i-1] + (val[i] - val[i-1]) * t;
}
}
return val[n-1];
}
static ARGB lookupColorTable(const vector<REAL> &pos, const vector<ARGB> &col, REAL p)
{
size_t n = pos.size();
if (n == 0) return 0;
if (p <= pos[0]) return col[0];
for (size_t i = 1; i < n; i++) {
if (p <= pos[i]) {
REAL span = pos[i] - pos[i-1];
return lerpARGB(col[i-1], col[i], span > 0 ? (p - pos[i-1]) / span : 0);
}
}
return col[n-1];
}
// 範囲外の座標を外接矩形の中へ畳む (Tile は繰り返し、TileFlip* は折り返し)
static REAL wrapCoord(REAL v, REAL lo, REAL w, bool flip)
{
if (w <= 0) return v;
double r = (v - lo) / w;
double k = floor(r);
double f = (r - k) * w;
if (flip && ((long long)k & 1)) f = w - f;
return (REAL)(lo + f);
}
// 点 (qx, qy) の色。 中心と各辺 (Pi, Pi+1) の三角形で重心座標を取り、
// t = 中心 0 → 外周 1、s = 辺の上の位置。 色 = lerp(中心色, lerp(Ci, Ci+1, s), t)
// (= GDI+ の PathGradient)。 多角形の外は透明 (0)。 hint は前回当たった辺
static ARGB pathGradientColor(const Appearance::DrawInfo &d, REAL qx, REAL qy, int &hint)
{
const int n = (int)d.pgX.size();
const double cx = d.pgCx, cy = d.pgCy;
const double px = qx - cx, py = qy - cy;
for (int k = 0; k < n; k++) {
// 前回の辺から外側へ順に試す (隣の画素はたいてい同じ三角形)
int e = (k & 1) ? hint - (k + 1) / 2 : hint + k / 2;
e = ((e % n) + n) % n;
int e2 = (e + 1) % n;
double ax = d.pgX[e] - cx, ay = d.pgY[e] - cy;
double bx = d.pgX[e2] - cx, by = d.pgY[e2] - cy;
double det = ax * by - ay * bx;
if (fabs(det) < 1e-12) continue;
double a = (px * by - py * bx) / det;
double b = (ax * py - ay * px) / det;
const double eps = 1e-9;
if (a < -eps || b < -eps || a + b > 1 + eps) continue;
hint = e;
double t = a + b;
double s = t > 0 ? b / t : 0;
// focusScales: 中心側の縮小した多角形の中は中心色
if (d.pgFocusX > 0 || d.pgFocusY > 0) {
double dx = fabs(ax + (bx - ax) * s), dy = fabs(ay + (by - ay) * s);
double f = (dx + dy) > 0 ? (d.pgFocusX * dx + d.pgFocusY * dy) / (dx + dy) : 0;
if (f >= 1) t = 0;
else t = (t <= f) ? 0 : (t - f) / (1 - f);
}
REAL p = (REAL)(1 - t); // 位置: 0 = 外周 / 1 = 中心
if (!d.pgPresetPos.empty()) return lookupColorTable(d.pgPresetPos, d.pgPresetColors, p);
ARGB edge = lerpARGB(d.pgColors[e], d.pgColors[e2], (REAL)s);
REAL fac = d.pgBlendPos.empty() ? p : lookupTable(d.pgBlendPos, d.pgBlendFac, p);
return lerpARGB(edge, d.pgCenterColor, fac);
}
return 0;
}
// --------------------------------------------------------
// アピアランス情報
// --------------------------------------------------------
Appearance::Appearance() {}
Appearance::~Appearance()
{
clear();
}
void Appearance::clear()
{
drawInfos.clear();
}
// tvg::Picture を w×h の ARGB8888 バッファへネイティブ解像度でラスタライズする。
// (テクスチャブラシのソースタイルのピクセルを取り出すために使用)
static bool rasterizePicture(tvg::Picture* src, uint32_t* out, int w, int h)
{
if (!src || !out || w <= 0 || h <= 0) return false;
memset(out, 0, (size_t)w * h * sizeof(uint32_t));
tvg::SwCanvas* c = tvg::SwCanvas::gen();
if (!c) return false;
bool ok = false;
if (c->target(out, w, w, h, tvg::ColorSpace::ARGB8888) == tvg::Result::Success) {
tvg::Picture* dup = (tvg::Picture*)src->duplicate();
if (dup) {
dup->size((float)w, (float)h);
if (c->add(dup) == tvg::Result::Success) {
c->draw(true);
c->sync();
ok = true;
}
}
}
delete c; // 追加した Picture は Canvas 破棄時に解放される
return ok;
}
void Appearance::addBrush(tTJSVariant colorOrBrush, REAL ox, REAL oy)
{
DrawInfo info;
info.type = 1; // フィル
info.ox = ox;
info.oy = oy;
if (colorOrBrush.Type() != tvtObject) {
// ARGB色
ARGB color = (ARGB)(tjs_int)colorOrBrush;
info.fillA = (color >> 24) & 0xFF;
info.fillR = (color >> 16) & 0xFF;
info.fillG = (color >> 8) & 0xFF;
info.fillB = color & 0xFF;
} else {
// ブラシ情報(辞書)
ncbPropAccessor propInfo(colorOrBrush);
int type = propInfo.getIntValue(TJS_W("type"), BrushTypeSolidColor);
tTJSVariant pathPoints;
if (type == BrushTypeLinearGradient) {
info.useLinearGradient = true;
// 始点・終点。 GDI+ と同じく point1/point2、無ければ rect + (angle | mode)
tTJSVariant var;
if (propInfo.checkVariant(TJS_W("point1"), var)) {
PointF p1 = getPoint(var);
info.gradX1 = p1.X;
info.gradY1 = p1.Y;
if (propInfo.checkVariant(TJS_W("point2"), var)) {
PointF p2 = getPoint(var);
info.gradX2 = p2.X;
info.gradY2 = p2.Y;
}
} else if (propInfo.checkVariant(TJS_W("rect"), var)) {
RectF rect = getRect(var);
REAL angle = 0;
bool scalable = false;
if (propInfo.HasValue(TJS_W("angle"))) {
angle = (REAL)propInfo.getRealValue(TJS_W("angle"), 0);
scalable = propInfo.getIntValue(TJS_W("isAngleScalable"), 0) != 0;
} else {
switch (propInfo.getIntValue(TJS_W("mode"), LinearGradientModeHorizontal)) {
case LinearGradientModeVertical: angle = 90; break;
case LinearGradientModeForwardDiagonal: angle = 45; scalable = true; break;
case LinearGradientModeBackwardDiagonal: angle = 135; scalable = true; break;
default: angle = 0; break;
}
}
linearPointsFromRect(rect, angle, scalable, info.gradX1, info.gradY1, info.gradX2, info.gradY2);
}
// 範囲外の扱い。 GDI+ の線形グラデーションの既定は Tile (繰り返し)
info.gradSpread = toSpread(propInfo.getIntValue(TJS_W("wrapMode"), WrapModeTile));
ARGB color1 = (ARGB)propInfo.getIntValue(TJS_W("color1"), 0);
ARGB color2 = (ARGB)propInfo.getIntValue(TJS_W("color2"), 0);
tvg::Fill::ColorStop stop1, stop2;
stop1.offset = 0.0f;
stop1.a = (color1 >> 24) & 0xFF;
stop1.r = (color1 >> 16) & 0xFF;
stop1.g = (color1 >> 8) & 0xFF;
stop1.b = color1 & 0xFF;
stop2.offset = 1.0f;
stop2.a = (color2 >> 24) & 0xFF;
stop2.r = (color2 >> 16) & 0xFF;
stop2.g = (color2 >> 8) & 0xFF;
stop2.b = color2 & 0xFF;
info.colorStops.push_back(stop1);
info.colorStops.push_back(stop2);
applyBlendParams(propInfo, color1, color2, info.colorStops);
} else if (type == BrushTypePathGradient && propInfo.checkVariant(TJS_W("points"), pathPoints) && pathPoints.Type() == tvtObject) {
// GDI+ の PathGradientBrush と同じ計算 (描画時に色の画像を作る)
parsePathGradient(propInfo, pathPoints, info);
} else if (type == BrushTypePathGradient) { // points が無い: 放射グラデーションで近似 (旧来の radius 指定)
info.useRadialGradient = true;
tTJSVariant var;
if (propInfo.checkVariant(TJS_W("centerPoint"), var)) {
PointF cp = getPoint(var);
info.gradCx = cp.X;
info.gradCy = cp.Y;
}
info.gradR = (REAL)propInfo.getRealValue(TJS_W("radius"), 100);
ARGB centerColor = (ARGB)propInfo.getIntValue(TJS_W("centerColor"), 0xFFFFFFFF);
tvg::Fill::ColorStop stop1, stop2;
stop1.offset = 0.0f;
stop1.a = (centerColor >> 24) & 0xFF;
stop1.r = (centerColor >> 16) & 0xFF;
stop1.g = (centerColor >> 8) & 0xFF;
stop1.b = centerColor & 0xFF;
stop2.offset = 1.0f;
stop2.a = 0;
stop2.r = 0;
stop2.g = 0;
stop2.b = 0;
info.colorStops.push_back(stop1);
info.colorStops.push_back(stop2);
} else if (type == BrushTypeTextureFill) {
// 画像タイル塗り (GDI+ TextureBrush 相当)。image はストレージパス文字列。
ttstr imgPath = propInfo.getStrValue(TJS_W("image"));
if (imgPath.length() > 0) {
::Image img;
if (img.load(imgPath.c_str()) && img.getPicture()) {
int tw = (int)(img.GetWidth() + 0.5f);
int th = (int)(img.GetHeight() + 0.5f);
// dstRect でソース領域を指定 (省略時は画像全体)
int sx = 0, sy = 0, sw = tw, sh = th;
tTJSVariant rectVar;
if (propInfo.checkVariant(TJS_W("dstRect"), rectVar)) {
ncbPropAccessor ra(rectVar);
if (ra.GetArrayCount() >= 4) {
sx = (int)ra.getRealValue(0);
sy = (int)ra.getRealValue(1);
sw = (int)ra.getRealValue(2);
sh = (int)ra.getRealValue(3);
}
}
if (sw <= 0) sw = tw;
if (sh <= 0) sh = th;
// ソース画像をラスタライズして dstRect 範囲をタイル素材に切り出す
if (tw > 0 && th > 0) {
std::vector<uint32_t> full((size_t)tw * th, 0);
if (rasterizePicture(img.getPicture(), full.data(), tw, th)) {
info.texW = sw;
info.texH = sh;
info.texPixels.assign((size_t)sw * sh, 0);
for (int y = 0; y < sh; y++) {
for (int x = 0; x < sw; x++) {
int fx = sx + x, fy = sy + y;
if (fx >= 0 && fx < tw && fy >= 0 && fy < th)
info.texPixels[(size_t)y*sw + x] = full[(size_t)fy*tw + fx];
}
}
info.useTextureFill = true;
}
}
}
}
} else {
// SolidColor
ARGB color = (ARGB)propInfo.getIntValue(TJS_W("color"), 0xFFFFFFFF);
info.fillA = (color >> 24) & 0xFF;
info.fillR = (color >> 16) & 0xFF;
info.fillG = (color >> 8) & 0xFF;
info.fillB = color & 0xFF;
}
}
drawInfos.push_back(info);
}
void Appearance::addPen(tTJSVariant colorOrBrush, tTJSVariant widthOrOption, REAL ox, REAL oy)
{
DrawInfo info;
info.type = 0; // ストローク
info.ox = ox;
info.oy = oy;
// 色設定
if (colorOrBrush.Type() != tvtObject) {
ARGB color = (ARGB)(tjs_int)colorOrBrush;
info.strokeA = (color >> 24) & 0xFF;
info.strokeR = (color >> 16) & 0xFF;
info.strokeG = (color >> 8) & 0xFF;
info.strokeB = color & 0xFF;
} else {
ncbPropAccessor propInfo(colorOrBrush);
int btype = propInfo.getIntValue(TJS_W("type"), BrushTypeSolidColor);
if (btype == BrushTypeLinearGradient || btype == BrushTypePathGradient) {
// GDI+ の Pen(brush): 線をブラシで塗る。 ブラシの解釈は addBrush と同じ
Appearance tmp;
tmp.addBrush(colorOrBrush, 0, 0);
const DrawInfo &b = tmp.drawInfos.back();
info.useLinearGradient = b.useLinearGradient;
info.useRadialGradient = b.useRadialGradient;
info.gradX1 = b.gradX1; info.gradY1 = b.gradY1; info.gradX2 = b.gradX2; info.gradY2 = b.gradY2;
info.gradCx = b.gradCx; info.gradCy = b.gradCy; info.gradR = b.gradR;
info.colorStops = b.colorStops; info.gradSpread = b.gradSpread;
info.usePathGradient = b.usePathGradient;
info.pgX = b.pgX; info.pgY = b.pgY; info.pgColors = b.pgColors;
info.pgCx = b.pgCx; info.pgCy = b.pgCy; info.pgCenterColor = b.pgCenterColor;
info.pgFocusX = b.pgFocusX; info.pgFocusY = b.pgFocusY; info.pgWrap = b.pgWrap;
info.pgBlendPos = b.pgBlendPos; info.pgBlendFac = b.pgBlendFac;
info.pgPresetPos = b.pgPresetPos; info.pgPresetColors = b.pgPresetColors;
} else {
ARGB color = (ARGB)propInfo.getIntValue(TJS_W("color"), 0xFFFFFFFF);
info.strokeA = (color >> 24) & 0xFF;
info.strokeR = (color >> 16) & 0xFF;
info.strokeG = (color >> 8) & 0xFF;
info.strokeB = color & 0xFF;
}
}
// 幅とオプション設定
if (widthOrOption.Type() != tvtObject) {
info.strokeWidth = (REAL)(tjs_real)widthOrOption;
} else {
ncbPropAccessor propInfo(widthOrOption);
tTJSVariant var;
if (propInfo.checkVariant(TJS_W("width"), var)) {
info.strokeWidth = (REAL)(tjs_real)var;
}
// LineCap
if (propInfo.checkVariant(TJS_W("startCap"), var) || propInfo.checkVariant(TJS_W("endCap"), var)) {
int cap = (int)(tjs_int)var;
switch (cap) {
case LineCapFlat:
info.strokeCap = tvg::StrokeCap::Butt;
break;
case LineCapSquare:
info.strokeCap = tvg::StrokeCap::Square;
break;
case LineCapRound:
info.strokeCap = tvg::StrokeCap::Round;
break;
case LineCapTriangle:
// ThorVG doesn't have Triangle cap, use Square as fallback
info.strokeCap = tvg::StrokeCap::Square;
break;
default:
info.strokeCap = tvg::StrokeCap::Square;
break;
}
}
// LineJoin
if (propInfo.checkVariant(TJS_W("lineJoin"), var)) {
int join = (int)(tjs_int)var;
switch (join) {
case LineJoinMiter:
info.strokeJoin = tvg::StrokeJoin::Miter;
break;
case LineJoinBevel:
info.strokeJoin = tvg::StrokeJoin::Bevel;
break;
case LineJoinRound:
info.strokeJoin = tvg::StrokeJoin::Round;
break;
case LineJoinMiterClipped:
// ThorVG doesn't have MiterClipped, use Miter as fallback
info.strokeJoin = tvg::StrokeJoin::Miter;
break;
default:
info.strokeJoin = tvg::StrokeJoin::Bevel;
break;
}
}
// MiterLimit
if (propInfo.checkVariant(TJS_W("miterLimit"), var)) {
info.miterLimit = (REAL)(tjs_real)var;
}
// DashStyle
if (propInfo.checkVariant(TJS_W("dashStyle"), var)) {
if (IsArray(var)) {
getReals(var, info.dashPattern);
} else if (var.Type() == tvtInteger) {
// GDI+ の DashStyle。 パターンはペン幅の倍数
static const REAL dash[] = { 3, 1 };
static const REAL dot[] = { 1, 1 };
static const REAL dashdot[] = { 3, 1, 1, 1 };
static const REAL dashdotdot[] = { 3, 1, 1, 1, 1, 1 };
const REAL *pat = nullptr; int n = 0;
switch ((tjs_int)var) {
case DashStyleDash: pat = dash; n = 2; break;
case DashStyleDot: pat = dot; n = 2; break;
case DashStyleDashDot: pat = dashdot; n = 4; break;
case DashStyleDashDotDot: pat = dashdotdot; n = 6; break;
default: break; // Solid / Custom は実線
}
REAL w = info.strokeWidth > 0 ? info.strokeWidth : 1;
for (int i = 0; i < n; i++) info.dashPattern.push_back(pat[i] * w);
}
}
// DashCap (破線の各区切りの端)
if (propInfo.checkVariant(TJS_W("dashCap"), var)) {
info.dashCap = (int)(tjs_int)var;
}
// DashOffset
if (propInfo.checkVariant(TJS_W("dashOffset"), var)) {
info.dashOffset = (REAL)(tjs_real)var;
}
}
drawInfos.push_back(info);
}
// --------------------------------------------------------
// FontInfo クラス
// --------------------------------------------------------
FontInfo::FontInfo()
: fontSize(12.0f), italic(0), letterSpacing(1.0f), lineSpacing(1.0f)
{
}
FontInfo::FontInfo(const tjs_char *family, REAL size)
: fontSize(size), italic(0), letterSpacing(1.0f), lineSpacing(1.0f)
{
if (family) {
fontFamily = family;
}
}
FontInfo::FontInfo(const FontInfo &orig)
: fontFamily(orig.fontFamily),
fontSize(orig.fontSize),
italic(orig.italic),
letterSpacing(orig.letterSpacing),
lineSpacing(orig.lineSpacing)
{
}
FontInfo::~FontInfo()
{
}
void FontInfo::setFontFamily(const tjs_char *name)
{
if (name) {
fontFamily = name;
} else {
fontFamily = TJS_W("");
}
}
// --------------------------------------------------------
// Path クラス
// --------------------------------------------------------
Path::Path() : figureStarted(false)
{
currentPos.x = 0;
currentPos.y = 0;
figureStartPos.x = 0;
figureStartPos.y = 0;
}
Path::~Path()
{
}
void Path::ensureFigureStarted()
{
if (!figureStarted) {
commands.push_back(tvg::PathCommand::MoveTo);
points.push_back(currentPos);
figureStartPos = currentPos;
figureStarted = true;
}
}
void Path::startFigure()
{
figureStarted = false;
}
void Path::closeFigure()
{
if (figureStarted) {
commands.push_back(tvg::PathCommand::Close);
currentPos = figureStartPos;
figureStarted = false;
}
}
void Path::addArcPoints(REAL cx, REAL cy, REAL rx, REAL ry, REAL startAngle, REAL sweepAngle)
{
// 楕円弧をベジェ曲線で近似
const int numSegments = (int)(fabs(sweepAngle) / 90.0f) + 1;
const REAL segmentAngle = sweepAngle / numSegments;
REAL angle = startAngle * (REAL)M_PI / 180.0f;
const REAL deltaAngle = segmentAngle * (REAL)M_PI / 180.0f;
// 開始点
REAL startX = cx + rx * cosf(angle);
REAL startY = cy + ry * sinf(angle);
if (!figureStarted) {
currentPos.x = startX;
currentPos.y = startY;
ensureFigureStarted();
} else {
commands.push_back(tvg::PathCommand::LineTo);
tvg::Point p = {startX, startY};
points.push_back(p);
}
for (int i = 0; i < numSegments; i++) {
REAL endAngle = angle + deltaAngle;
// ベジェ制御点の計算
REAL kappa = 4.0f / 3.0f * tanf(deltaAngle / 4.0f);
REAL x1 = cx + rx * cosf(angle);
REAL y1 = cy + ry * sinf(angle);
REAL x4 = cx + rx * cosf(endAngle);
REAL y4 = cy + ry * sinf(endAngle);
REAL x2 = x1 - kappa * rx * sinf(angle);
REAL y2 = y1 + kappa * ry * cosf(angle);
REAL x3 = x4 + kappa * rx * sinf(endAngle);
REAL y3 = y4 - kappa * ry * cosf(endAngle);
commands.push_back(tvg::PathCommand::CubicTo);
tvg::Point cp1 = {x2, y2};
tvg::Point cp2 = {x3, y3};
tvg::Point ep = {x4, y4};
points.push_back(cp1);
points.push_back(cp2);
points.push_back(ep);
angle = endAngle;
}
currentPos.x = cx + rx * cosf(angle);
currentPos.y = cy + ry * sinf(angle);
}
void Path::drawArc(REAL x, REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
{
REAL cx = x + width / 2;
REAL cy = y + height / 2;
REAL rx = width / 2;
REAL ry = height / 2;
addArcPoints(cx, cy, rx, ry, startAngle, sweepAngle);
}
void Path::drawBezier(REAL x1, REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
{
currentPos.x = x1;
currentPos.y = y1;
ensureFigureStarted();
commands.push_back(tvg::PathCommand::CubicTo);
tvg::Point cp1 = {x2, y2};
tvg::Point cp2 = {x3, y3};
tvg::Point ep = {x4, y4};
points.push_back(cp1);
points.push_back(cp2);
points.push_back(ep);
currentPos = ep;
}
void Path::drawBeziers(tTJSVariant pts)
{
vector<PointF> ps;
getPoints(pts, ps);
if (ps.size() < 4) return;
currentPos.x = ps[0].X;
currentPos.y = ps[0].Y;
ensureFigureStarted();
for (size_t i = 1; i + 2 < ps.size(); i += 3) {
commands.push_back(tvg::PathCommand::CubicTo);
tvg::Point cp1 = {ps[i].X, ps[i].Y};
tvg::Point cp2 = {ps[i+1].X, ps[i+1].Y};
tvg::Point ep = {ps[i+2].X, ps[i+2].Y};
points.push_back(cp1);
points.push_back(cp2);
points.push_back(ep);
currentPos = ep;
}
}
void Path::computeCardinalSpline(const vector<PointF>& pts, REAL tension, bool closed, vector<tvg::Point>& outPts)
{
if (pts.size() < 2) return;
// Cardinal spline をベジェ曲線に変換
REAL t = (1.0f - tension) / 2.0f;
size_t n = pts.size();
for (size_t i = 0; i < n - 1; i++) {
PointF p0 = (i == 0) ? (closed ? pts[n-1] : pts[0]) : pts[i-1];
PointF p1 = pts[i];
PointF p2 = pts[i+1];
PointF p3 = (i == n - 2) ? (closed ? pts[0] : pts[n-1]) : pts[i+2];
REAL cp1x = p1.X + t * (p2.X - p0.X) / 3.0f;
REAL cp1y = p1.Y + t * (p2.Y - p0.Y) / 3.0f;
REAL cp2x = p2.X - t * (p3.X - p1.X) / 3.0f;
REAL cp2y = p2.Y - t * (p3.Y - p1.Y) / 3.0f;
if (i == 0) {
tvg::Point sp = {p1.X, p1.Y};
outPts.push_back(sp);
}
tvg::Point c1 = {cp1x, cp1y};
tvg::Point c2 = {cp2x, cp2y};
tvg::Point ep = {p2.X, p2.Y};
outPts.push_back(c1);
outPts.push_back(c2);
outPts.push_back(ep);
}
if (closed && n > 2) {
// 閉じた曲線の最後のセグメント
PointF p0 = pts[n-2];
PointF p1 = pts[n-1];
PointF p2 = pts[0];
PointF p3 = pts[1];
REAL cp1x = p1.X + t * (p2.X - p0.X) / 3.0f;
REAL cp1y = p1.Y + t * (p2.Y - p0.Y) / 3.0f;
REAL cp2x = p2.X - t * (p3.X - p1.X) / 3.0f;
REAL cp2y = p2.Y - t * (p3.Y - p1.Y) / 3.0f;
tvg::Point c1 = {cp1x, cp1y};
tvg::Point c2 = {cp2x, cp2y};
tvg::Point ep = {p2.X, p2.Y};
outPts.push_back(c1);
outPts.push_back(c2);
outPts.push_back(ep);
}
}
void Path::drawClosedCurve(tTJSVariant pts)
{
drawClosedCurve2(pts, 0.5f);
}
void Path::drawClosedCurve2(tTJSVariant pts, REAL tension)
{
vector<PointF> ps;
getPoints(pts, ps);
if (ps.size() < 3) return;
vector<tvg::Point> splinePts;
computeCardinalSpline(ps, tension, true, splinePts);