-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFMX.Graphics.Native.pas
More file actions
996 lines (839 loc) · 35.9 KB
/
FMX.Graphics.Native.pas
File metadata and controls
996 lines (839 loc) · 35.9 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
{------------------------------------------}
{ }
{ (c) 2017 by Aone }
{ }
{ QQ: 1467948783 }
{ }
{ http://www.cnblogs.com/onechen }
{ }
{------------------------------------------}
{ Start: 2017.01.16 }
{------------------------------------------}
// [原创] 改善 Firemonkey Canvas 几何绘图质量问题(移动平台)by Aone
unit FMX.Graphics.Native;
{$i NativeDraw.inc}
interface
uses
System.Types,
System.Classes,
System.SysUtils,
System.UITypes,
System.UIConsts,
System.Math,
System.Math.Vectors,
System.Generics.Collections,
{$IFDEF ANDROID}
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNIBridge,
Androidapi.Helpers,
Androidapi.Bitmap,
Androidapi.JNI.App,
FMX.Surfaces,
FMX.Helpers.Android,
{$ENDIF}
{$IFDEF IOS}
iOSapi.CocoaTypes,
iOSapi.UIKit,
iOSapi.Foundation,
iOSapi.CoreGraphics,
Macapi.CoreFoundation,
Macapi.ObjectiveC,
Macapi.Helpers,
FMX.Helpers.iOS,
{$ENDIF}
FMX.Types,
FMX.Graphics,
FMX.Platform;
type
TDrawProc = reference to procedure;
TCanvasHelper = class helper for TCanvas
public
// 原生绘图
procedure NativeDraw(const ARect: TRectF; const ADrawProc: TDrawProc);
// 涂色 + 线色一次完成
procedure DrawRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush; const ACornerType: TCornerType = TCornerType.Round; const Inside: Boolean = False); overload;
procedure DrawPath(const APath: TPathData; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush); overload;
procedure DrawEllipse(const ARect: TRectF; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush; const Inside: Boolean = False); overload;
procedure DrawArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush; const Inside: Boolean = False); overload;
procedure DrawPolygon(const Points: TPolygon; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush); overload;
{$IFDEF UseNativeDraw}
private
{$IF Defined(IOS) or Defined(ANDROID)}
procedure ApplyGradient({$IFDEF ANDROID}const Paint1: JPaint;{$ENDIF} const ABrush: TBrush; const ARect: TRectF);
procedure ApplyFill({$IFDEF ANDROID}const Paint1: JPaint;{$ENDIF} const ABrush: TBrush; const ARect: TRectF; const AOpacity: Single);
procedure DrawFill({$IFDEF ANDROID}const Paint1: JPaint;{$ENDIF} const ABrush: TBrush; const SrcRect, DesRect: TRectF; const AOpacity: Single);
procedure ApplyStroke({$IFDEF ANDROID}const Paint1: JPaint;{$ENDIF} const AStroke: TStrokeBrush; const ARect: TRectF; const AOpacity: Single);
{$ENDIF}
public
{$IF Defined(IOS) or Defined(ANDROID)}
// 下列为 Canvas 原有函数
procedure DrawLine(const APt1, APt2: TPointF; const AOpacity: Single); overload;
procedure DrawLine(const APt1, APt2: TPointF; const AOpacity: Single; const ABrush: TStrokeBrush); overload;
procedure FillRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const ACornerType: TCornerType = TCornerType.Round); overload;
procedure FillRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const ABrush: TBrush; const ACornerType: TCornerType = TCornerType.Round); overload;
procedure DrawRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const ACornerType: TCornerType = TCornerType.Round); overload;
procedure DrawRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const ABrush: TStrokeBrush; const ACornerType: TCornerType = TCornerType.Round); overload;
procedure FillPath(const APath: TPathData; const AOpacity: Single); overload;
procedure FillPath(const APath: TPathData; const AOpacity: Single; const ABrush: TBrush); overload;
procedure DrawPath(const APath: TPathData; const AOpacity: Single); overload;
procedure DrawPath(const APath: TPathData; const AOpacity: Single; const ABrush: TStrokeBrush); overload;
procedure FillEllipse(const ARect: TRectF; const AOpacity: Single); overload;
procedure FillEllipse(const ARect: TRectF; const AOpacity: Single; const ABrush: TBrush); overload;
procedure DrawEllipse(const ARect: TRectF; const AOpacity: Single); overload;
procedure DrawEllipse(const ARect: TRectF; const AOpacity: Single; const ABrush: TStrokeBrush); overload;
procedure FillArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single); overload;
procedure FillArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single; const ABrush: TBrush); overload;
procedure DrawArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single); overload;
procedure DrawArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single; const ABrush: TStrokeBrush); overload;
procedure FillPolygon(const Points: TPolygon; const AOpacity: Single); overload;
procedure DrawPolygon(const Points: TPolygon; const AOpacity: Single); overload;
procedure IntersectClipRect(const ARect: TRectF); overload;
procedure ExcludeClipRect(const ARect: TRectF); overload;
{$ENDIF}
{$ENDIF}
end;
implementation
{$IF not Defined(UseNativeDraw) or Defined(MSWINDOWS) or Defined(MACOSONLY)}
procedure TCanvasHelper.NativeDraw(const ARect: TRectF; const ADrawProc: TDrawProc);
begin
// 绘图函数
if Assigned(ADrawProc) then
ADrawProc;
end;
procedure TCanvasHelper.DrawRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush; const ACornerType: TCornerType = TCornerType.Round; const Inside: Boolean = False);
var R: TRectF;
begin
R := ARect;
// 线在区内
if Inside and (AStroke <> nil) and (AStroke.Kind <> TBrushKind.None) then
InflateRect(R, -(AStroke.Thickness / 2), -(AStroke.Thickness / 2));
FillRect(R, XRadius, YRadius, ACorners, AOpacity, AFill, ACornerType);
DrawRect(R, XRadius, YRadius, ACorners, AOpacity, AStroke, ACornerType);
end;
procedure TCanvasHelper.DrawPath(const APath: TPathData; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush);
begin
FillPath(APath, AOpacity, AFill);
DrawPath(APath, AOpacity, AStroke);
end;
procedure TCanvasHelper.DrawEllipse(const ARect: TRectF; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush; const Inside: Boolean = False);
var R: TRectF;
begin
R := ARect;
// 线在区内
if Inside and (AStroke <> nil) and (AStroke.Kind <> TBrushKind.None) then
InflateRect(R, -(AStroke.Thickness / 2), -(AStroke.Thickness / 2));
FillEllipse(R, AOpacity, AFill);
DrawEllipse(R, AOpacity, AStroke);
end;
procedure TCanvasHelper.DrawArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush; const Inside: Boolean = False);
var R: TRectF;
P: TPointF;
begin
R := RectF(Center.X - Radius.X, Center.Y - Radius.Y, Center.X + Radius.X, Center.Y + Radius.Y);
// 线在区内
if Inside and (AStroke <> nil) and (AStroke.Kind <> TBrushKind.None) then
begin
R.Offset(-R.Left, -R.Top);
InflateRect(R, -(AStroke.Thickness / 2), -(AStroke.Thickness / 2));
end;
P := PointF(R.Width / 2, R.Height / 2);
FillArc(Center, P, StartAngle, SweepAngle, AOpacity, AFill);
DrawArc(Center, P, StartAngle, SweepAngle, AOpacity, AStroke);
end;
procedure TCanvasHelper.DrawPolygon(const Points: TPolygon; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush);
begin
if AFill <> nil then
Self.Fill.Assign(AFill);
if AStroke <> nil then
Self.Stroke.Assign(AStroke);
FillPolygon(Points, AOpacity);
DrawPolygon(Points, AOpacity);
end;
{$ENDIF}
{$IFDEF UseNativeDraw}
{$IFDEF ANDROID}
var GlobalCanvas: JCanvas;
function JBitmapToBitmap(const AImage: JBitmap): TBitmap;
var Surface: TBitmapSurface;
begin
Surface := TBitmapSurface.Create;
Result := nil;
try
if JBitmapToSurface(AImage, Surface) then
begin
Result := TBitmap.Create;
Result.Assign(Surface);
end;
finally
Surface.Free;
end;
end;
procedure TCanvasHelper.NativeDraw(const ARect: TRectF; const ADrawProc: TDrawProc);
var Bitmap1: JBitmap;
Paint: JPaint;
Bitmap: TBitmap;
ScreenService: IFMXScreenService;
Scale1: Single;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, ScreenService) then
Scale1 := ScreenService.GetScreenScale
else Scale1 := 1;
Bitmap1 := TJBitmap.JavaClass.createBitmap(Ceil(ARect.Width * Scale1), Ceil(ARect.Height * Scale1), TJBitmap_Config.JavaClass.ARGB_8888);
GlobalCanvas := TJCanvas.JavaClass.init(Bitmap1);
GlobalCanvas.save;
GlobalCanvas.scale(Scale1, Scale1);
// 透明底色
Paint := TJPaint.Wrap(TJPaint.JavaClass.init(TJPaint.JavaClass.ANTI_ALIAS_FLAG));
Paint.setStyle(TJPaint_Style.Wrap(TJPaint_Style.JavaClass.FILL));
Paint.setARGB(0, 255, 255, 255);
GlobalCanvas.drawRect(GlobalCanvas.getClipBounds, Paint);
// 绘图函数
if Assigned(ADrawProc) then
ADrawProc;
GlobalCanvas.restore;
// 显示
Bitmap := JBitmapToBitmap(Bitmap1);
DrawBitmap(Bitmap, RectF(0, 0, Bitmap.Width, Bitmap.Height), ARect, 1);
FreeAndNil(Bitmap);
end;
procedure TCanvasHelper.ApplyGradient(const Paint1: JPaint; const ABrush: TBrush; const ARect: TRectF);
var i: Integer;
aColors: TJavaArray<Integer>;
aStops: TJavaArray<Single>;
aLinearShader: JLinearGradient;
aRadialShader: JRadialGradient;
begin
aColors := TJavaArray<Integer>.Create(ABrush.Gradient.Points.Count);
aStops := TJavaArray<Single>.Create(ABrush.Gradient.Points.Count);
for i:=0 to ABrush.Gradient.Points.Count - 1 do
begin
aColors[ABrush.Gradient.Points.Count - 1 - i] := integer(ABrush.Gradient.Points[i].Color);
aStops[ABrush.Gradient.Points.Count - 1 - i] := 1 - ABrush.Gradient.Points[i].Offset;
end;
case ABrush.Gradient.Style of
// 线渐层
TGradientStyle.Linear:
begin
aLinearShader := TJLinearGradient.JavaClass.init(ARect.Left + ABrush.Gradient.StopPosition.X * ARect.Width, ARect.Top + ABrush.Gradient.StopPosition.Y * ARect.Height,
ARect.Left + ABrush.Gradient.StartPosition.X * ARect.Width, ARect.Top + ABrush.Gradient.StartPosition.Y * ARect.Height,
aColors, aStops, TJShader_TileMode.JavaClass.CLAMP);
Paint1.setShader(aLinearShader);
aLinearShader := nil;
end;
// 圆渐层
TGradientStyle.Radial:
begin
aRadialShader := TJRadialGradient.JavaClass.init(ARect.CenterPoint.X, ARect.CenterPoint.Y, ARect.width / 2, aColors, aStops, TJShader_TileMode.JavaClass.CLAMP);
Paint1.setShader(aRadialShader);
aRadialShader := nil;
end;
else Paint1.setShader(nil);
end;
FreeAndNil(aColors);
FreeAndNil(aStops);
end;
procedure TCanvasHelper.ApplyFill(const Paint1: JPaint; const ABrush: TBrush; const ARect: TRectF; const AOpacity: Single);
begin
if (ABrush.Kind = TBrushKind.Resource) and (ABrush.Resource <> nil) and (ABrush.Resource.Brush <> nil) then
ABrush.Assign(ABrush.Resource.Brush);
Paint1.setStyle(TJPaint_Style.Wrap(TJPaint_Style.JavaClass.Fill));
case ABrush.Kind of
TBrushKind.Solid: Paint1.setARGB(TColorRec(ABrush.Color).A, TColorRec(ABrush.Color).B, TColorRec(ABrush.Color).G, TColorRec(ABrush.Color).R);
TBrushKind.Gradient: ApplyGradient(Paint1, ABrush, ARect);
else Paint1.setARGB(0, 255, 255, 255);
end;
end;
procedure TCanvasHelper.DrawFill(const Paint1: JPaint; const ABrush: TBrush; const SrcRect, DesRect: TRectF; const AOpacity: Single);
begin
if (ABrush.Kind = TBrushKind.Resource) and (ABrush.Resource <> nil) and (ABrush.Resource.Brush <> nil) then
ABrush.Assign(ABrush.Resource.Brush);
if ABrush.Kind = TBrushKind.Bitmap then
begin
// 未完成
end;
end;
procedure TCanvasHelper.ApplyStroke(const Paint1: JPaint; const AStroke: TStrokeBrush; const ARect: TRectF; const AOpacity: Single);
var i: Integer;
Dash: TJavaArray<Single>;
begin
if (AStroke.Kind = TBrushKind.Resource) and (AStroke.Resource <> nil) and (AStroke.Resource.Brush <> nil) then
AStroke.Assign(AStroke.Resource.Brush);
Paint1.setStyle(TJPaint_Style.Wrap(TJPaint_Style.JavaClass.Stroke));
// Thickness = 0 还是有线
if AStroke.Thickness > 0 then
begin
Paint1.setStrokeWidth(AStroke.Thickness);
case AStroke.Kind of
TBrushKind.Solid,
TBrushKind.Bitmap: Paint1.setARGB(TColorRec(AStroke.Color).A, TColorRec(AStroke.Color).B, TColorRec(AStroke.Color).G, TColorRec(AStroke.Color).R);
TBrushKind.Gradient: ApplyGradient(Paint1, AStroke, ARect);
else Paint1.setARGB(0, 0, 0, 0);
end;
case AStroke.Cap of
TStrokeCap.Flat: Paint1.setStrokeCap(TJPaint_Cap.JavaClass.BUTT);
TStrokeCap.Round: Paint1.setStrokeCap(TJPaint_Cap.JavaClass.ROUND);
end;
if Length(AStroke.DashArray) > 0 then
begin
Dash := TJavaArray<Single>.Create(Length(AStroke.DashArray));
for i:=0 to High(AStroke.DashArray) do
begin
Dash[i] := AStroke.DashArray[i] * AStroke.Thickness;
if AStroke.Cap = TStrokeCap.Round then
begin
if Odd(i) then
Dash[i] := (AStroke.DashArray[i] + 0.9) * AStroke.Thickness
else Dash[i] := (AStroke.DashArray[i] - 0.9) * AStroke.Thickness;
end;
end;
Paint1.setPathEffect(TJDashPathEffect.JavaClass.init(Dash, 0));
end;
case AStroke.Join of
TStrokeJoin.Miter: Paint1.setStrokeJoin(TJPaint_Join.JavaClass.MITER);
TStrokeJoin.Round: Paint1.setStrokeJoin(TJPaint_Join.JavaClass.ROUND);
TStrokeJoin.Bevel: Paint1.setStrokeJoin(TJPaint_Join.JavaClass.BEVEL);
end;
end
else Paint1.setARGB(0, 0, 0, 0);
end;
procedure TCanvasHelper.DrawLine(const APt1, APt2: TPointF; const AOpacity: Single);
begin
DrawLine(APt1, APt2, AOpacity, Self.Stroke);
end;
procedure TCanvasHelper.DrawLine(const APt1, APt2: TPointF; const AOpacity: Single; const ABrush: TStrokeBrush);
var Paint1: JPaint;
begin
if GlobalCanvas = nil then
Exit;
if ABrush.Kind <> TBrushKind.None then
begin
Paint1 := TJPaint.Wrap(TJPaint.JavaClass.init(TJPaint.JavaClass.ANTI_ALIAS_FLAG));
ApplyStroke(Paint1, ABrush, TRectF.Create(APt1.X, APt1.Y, APt2.X, APt2.Y), AOpacity);
GlobalCanvas.drawLine(APt1.X, APt1.Y, APt2.X, APt2.Y, Paint1);
end;
end;
procedure TCanvasHelper.DrawPath(const APath: TPathData; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush);
var i: Integer;
Path1: JPath;
Paint1: JPaint;
SrcRect: TRectF;
begin
if GlobalCanvas = nil then
Exit;
SrcRect := APath.GetBounds;
Path1 := TJPath.Wrap(TJPath.JavaClass.init);
i := 0;
while i < APath.Count do
begin
case APath.Points[i].Kind of
// 移到
TPathPointKind.MoveTo:
Path1.moveTo(APath.Points[i].Point.X, APath.Points[i].Point.Y);
// 线到
TPathPointKind.LineTo:
Path1.lineTo(APath.Points[i].Point.X, APath.Points[i].Point.Y);
// 曲线
TPathPointKind.CurveTo:
begin
Path1.cubicTo(APath.Points[i ].Point.X, APath.Points[i ].Point.Y,
APath.Points[i + 1].Point.X, APath.Points[i + 1].Point.Y,
APath.Points[i + 2].Point.X, APath.Points[i + 2].Point.Y);
Inc(i, 2);
end;
// 关闭
TPathPointKind.Close:
Path1.close;
end;
Inc(i);
end;
GlobalCanvas.save;
Paint1 := TJPaint.Wrap(TJPaint.JavaClass.init(TJPaint.JavaClass.ANTI_ALIAS_FLAG));
if (AFill <> nil) and (AFill.Kind <> TBrushKind.None) then
begin
if AFill.Kind = TBrushKind.Bitmap then
begin
GlobalCanvas.drawPath(Path1, Paint1);
DrawFill(Paint1, AFill, SrcRect, SrcRect, AOpacity);
end
else
begin
Path1.setFillType(TJPath_FillType.Wrap(TJPath_FillType.JavaClass.EVEN_ODD));
ApplyFill(Paint1, AFill, SrcRect, AOpacity);
GlobalCanvas.drawPath(Path1, Paint1);
end;
end;
if (AStroke <> nil) and (AStroke.Kind <> TBrushKind.None) then
begin
ApplyStroke(Paint1, AStroke, SrcRect, AOpacity);
GlobalCanvas.drawPath(Path1, Paint1);
end;
GlobalCanvas.restore;
end;
procedure TCanvasHelper.IntersectClipRect(const ARect: TRectF);
var JR: JRectF;
begin
if GlobalCanvas <> nil then
begin
JR := TJRectF.JavaClass.init(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
GlobalCanvas.clipRect(JR, TJRegion_Op.JavaClass.INTERSECT);
end;
end;
procedure TCanvasHelper.ExcludeClipRect(const ARect: TRectF);
var JR: JRectF;
begin
if GlobalCanvas <> nil then
begin
JR := TJRectF.JavaClass.init(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
GlobalCanvas.clipRect(JR, TJRegion_Op.JavaClass.REPLACE);
end;
end;
{$ENDIF}
{$IFDEF IOS}
var GlobalCanvas: CGContextRef;
function PointToCGPoint(const P: TPointF): CGPoint;
begin
Result := CGPointMake(P.X, P.Y);
end;
procedure TCanvasHelper.NativeDraw(const ARect: TRectF; const ADrawProc: TDrawProc);
var NativeImage: UIImage;
PNGRepresentation: NSData;
PNGMemoryStream: TMemoryStream;
Bitmap: TBitmap;
begin
NativeImage := nil;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(ARect.Width, ARect.Height), False, 0{目前机子Scale});
GlobalCanvas := UIGraphicsGetCurrentContext;
CGContextSaveGState(GlobalCanvas);
// 透明底色
TUIColor.Wrap(TUIColor.OCClass.colorWithHue(0, 0, 1, 0)).setFill;
CGContextFillRect(GlobalCanvas, CGContextGetClipBoundingBox(GlobalCanvas));
// 绘图函数
if Assigned(ADrawProc) then
ADrawProc;
CGContextRestoreGState(GlobalCanvas);
NativeImage := TUIImage.Wrap(UIGraphicsGetImageFromCurrentImageContext);
if Assigned(NativeImage) then
begin
PNGRepresentation := TNSData.Wrap(UIImagePNGRepresentation((NativeImage as ILocalObject).GetObjectID));
if Assigned(PNGRepresentation) then
begin
Bitmap := TBitmap.Create(0, 0);
PNGMemoryStream := TMemoryStream.Create;
PNGMemoryStream.Write(PNGRepresentation.bytes^, PNGRepresentation.length);
Bitmap.LoadFromStream(PNGMemoryStream);
PNGMemoryStream.Free;
// 显示
DrawBitmap(Bitmap, RectF(0, 0, Bitmap.Width, Bitmap.Height), ARect, 1);
FreeAndNil(Bitmap);
end;
end;
UIGraphicsEndImageContext;
end;
procedure TCanvasHelper.ApplyGradient(const ABrush: TBrush; const ARect: TRectF);
var i: Integer;
Locations: TArray<CGFloat>;
colorSpace: CGColorSpaceRef;
gradient: CGGradientRef;
colors: NSMutableArray;
RCenter: CGPoint;
begin
if GlobalCanvas = nil then
Exit;
SetLength(Locations, ABrush.Gradient.Points.Count);
colors := TNSMutableArray.Wrap(TNSMutableArray.OCClass.arrayWithCapacity(ABrush.Gradient.Points.Count));
for i := 0 to ABrush.Gradient.Points.Count - 1 do
begin
colors.addObject(AlphaColorToUIColor(ABrush.Gradient.Points[i].Color).CGColor);
Locations[i] := ABrush.Gradient.Points[i].Offset;
end;
colorSpace := CGColorSpaceCreateDeviceRGB;
gradient := CGGradientCreateWithColors(colorSpace, (colors as ILocalObject).GetObjectID, @Locations[0]);
case ABrush.Gradient.Style of
// 线渐层
TGradientStyle.Linear:
begin
CGContextDrawLinearGradient(GlobalCanvas, gradient,
CGPointMake(ARect.Left + ABrush.Gradient.StartPosition.X * ARect.Width, ARect.Top + ABrush.Gradient.StartPosition.Y * ARect.Height),
CGPointMake(ARect.Left + ABrush.Gradient.StopPosition.X * ARect.Width, ARect.Top + ABrush.Gradient.StopPosition.Y * ARect.Height),
0);
end;
// 圆渐层
TGradientStyle.Radial:
begin
RCenter.Create(PointF(ABrush.Gradient.RadialTransform.RotationCenter.X * ARect.Width, ABrush.Gradient.RadialTransform.RotationCenter.Y * ARect.Height) + ARect.TopLeft);
CGContextDrawRadialGradient(GlobalCanvas, gradient,
RCenter,
ARect.Width / 2,
RCenter,
0,
kCGGradientDrawsBeforeStartLocation or kCGGradientDrawsAfterEndLocation);
end;
end;
CFRelease(colorSpace);
CFRelease(gradient);
end;
procedure TCanvasHelper.ApplyFill(const ABrush: TBrush; const ARect: TRectF; const AOpacity: Single);
var
LColor: TAlphaColorF;
begin
if GlobalCanvas = nil then
Exit;
if (ABrush.Kind = TBrushKind.Resource) and (ABrush.Resource <> nil) and (ABrush.Resource.Brush <> nil) then
ABrush.Assign(ABrush.Resource.Brush);
case ABrush.Kind of
TBrushKind.Solid:
begin
LColor := TAlphaColorF.Create(MakeColor(ABrush.Color, AOpacity));
CGContextSetRGBFillColor(GlobalCanvas, LColor.R, LColor.G, LColor.B, LColor.A);
end;
else
CGContextSetRGBFillColor(GlobalCanvas, 0, 0, 0, 0);
end;
// 渐层
if (ABrush.Kind = TBrushKind.Gradient) and
(CGContextIsPathEmpty(GlobalCanvas) = 0) then
begin
CGContextClip(GlobalCanvas);
ApplyGradient(ABrush, ARect);
end;
end;
procedure TCanvasHelper.DrawFill(const ABrush: TBrush; const SrcRect, DesRect: TRectF; const AOpacity: Single);
begin
if ABrush.Kind = TBrushKind.Bitmap then
begin
// 未完成
end
else
begin
ApplyFill(ABrush, DesRect, AOpacity);
CGContextEOFillPath(GlobalCanvas);
end;
end;
procedure TCanvasHelper.ApplyStroke(const AStroke: TStrokeBrush; const ARect: TRectF; const AOpacity: Single);
var
Dash: TDashArray;
I: Integer;
LColor: TAlphaColorF;
R: TRectF;
begin
if GlobalCanvas = nil then
Exit;
if (AStroke.Kind = TBrushKind.Resource) and (AStroke.Resource <> nil) and (AStroke.Resource.Brush <> nil) then
AStroke.Assign(AStroke.Resource.Brush);
case AStroke.Kind of
TBrushKind.Solid,
TBrushKind.Bitmap:
begin
LColor := TAlphaColorF.Create(MakeColor(AStroke.Color, AOpacity));
CGContextSetRGBStrokeColor(GlobalCanvas, LColor.R, LColor.G, LColor.B, LColor.A);
end;
else
CGContextSetRGBStrokeColor(GlobalCanvas, 0, 0, 0, 0);
end;
case AStroke.Cap of
TStrokeCap.Flat:
CGContextSetLineCap(GlobalCanvas, kCGLineCapButt);
TStrokeCap.Round:
CGContextSetLineCap(GlobalCanvas, kCGLineCapRound);
end;
if Length(AStroke.DashArray) > 0 then
begin
// select the proper dash array for the printer
if FPrinter <> nil then
if AStroke.Dash <> TStrokeDash.Custom then
Dash := TStrokeBrush.StdDash[TStrokeBrush.TDashDevice.Printer, AStroke.Dash].DashArray
else
Dash := AStroke.DashArray
else // adjust the line dashes for the screen
begin
SetLength(Dash, Length(AStroke.DashArray));
for I := 0 to High(AStroke.DashArray) do
begin
Dash[I] := AStroke.DashArray[I] * AStroke.Thickness;
if AStroke.Cap = TStrokeCap.Round then
begin
if Odd(I) then
Dash[I] := (AStroke.DashArray[I] + 1) * AStroke.Thickness
else
Dash[I] := (AStroke.DashArray[I] - 1) * AStroke.Thickness;
end;
end;
end;
CGContextSetLineDash(GlobalCanvas, AStroke.DashOffset, @Dash[0], Length(AStroke.DashArray));
end
else
CGContextSetLineDash(GlobalCanvas, 0, nil, 0);
case AStroke.Join of
TStrokeJoin.Miter:
CGContextSetLineJoin(GlobalCanvas, kCGLineJoinMiter);
TStrokeJoin.Round:
CGContextSetLineJoin(GlobalCanvas, kCGLineJoinRound);
TStrokeJoin.Bevel:
CGContextSetLineJoin(GlobalCanvas, kCGLineJoinBevel);
end;
CGContextSetLineWidth(GlobalCanvas, AStroke.Thickness);
// 渐层
if (AStroke.Kind = TBrushKind.Gradient) and
(CGContextIsPathEmpty(GlobalCanvas) = 0) then
begin
CGContextReplacePathWithStrokedPath(GlobalCanvas);
CGContextClip(GlobalCanvas);
R := ARect;
InflateRect(R, AStroke.Thickness / 2, AStroke.Thickness / 2);
ApplyGradient(AStroke, R);
end;
end;
procedure TCanvasHelper.DrawLine(const APt1, APt2: TPointF; const AOpacity: Single);
begin
DrawLine(APt1, APt2, AOpacity, Self.Stroke);
end;
procedure TCanvasHelper.DrawLine(const APt1, APt2: TPointF; const AOpacity: Single; const ABrush: TStrokeBrush);
var R: TRectF;
begin
if GlobalCanvas = nil then
Exit;
if ABrush.Kind <> TBrushKind.None then
begin
CGContextSaveGState(GlobalCanvas);
CGContextBeginPath(GlobalCanvas);
CGContextMoveToPoint(GlobalCanvas, APt1.X, APt1.Y);
CGContextAddLineToPoint(GlobalCanvas, APt2.X, APt2.Y);
// 加上线粗
R := TRectF.Create(APt1.X, APt1.Y, APt2.X, APt2.Y);
InflateRect(R, ABrush.Thickness / 2, ABrush.Thickness / 2);
ApplyStroke(ABrush, R, AOpacity);
CGContextStrokePath(GlobalCanvas);
CGContextRestoreGState(GlobalCanvas);
end;
end;
procedure TCanvasHelper.DrawPath(const APath: TPathData; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush);
procedure ShowPath;
var i: Integer;
CurvePoint1, CurvePoint2: TPointF;
begin
i := 0;
while i < APath.Count do
begin
case APath[i].Kind of
TPathPointKind.MoveTo:
CGContextMoveToPoint(GlobalCanvas, APath[i].Point.X, APath[i].Point.Y);
TPathPointKind.LineTo:
CGContextAddLineToPoint(GlobalCanvas, APath[i].Point.X, APath[i].Point.Y);
TPathPointKind.CurveTo:
begin
CurvePoint1 := APath[i].Point;
Inc(i);
CurvePoint2 := APath[i].Point;
Inc(i);
CGContextAddCurveToPoint(GlobalCanvas, CurvePoint1.X, CurvePoint1.Y, CurvePoint2.X, CurvePoint2.Y, APath[i].Point.X, APath[i].Point.Y);
end;
TPathPointKind.Close:
CGContextClosePath(GlobalCanvas);
end;
Inc(i);
end;
end;
var SrcRect: TRectF;
begin
if GlobalCanvas = nil then
Exit;
SrcRect := APath.GetBounds;
// 涂色
if (AFill <> nil) and (AFill.Kind <> TBrushKind.None) then
begin
CGContextSaveGState(GlobalCanvas);
CGContextBeginPath(GlobalCanvas);
ShowPath;
DrawFill(AFill, SrcRect, SrcRect, AOpacity);
CGContextRestoreGState(GlobalCanvas);
end;
// 画线
if (AStroke <> nil) and (AStroke.Kind <> TBrushKind.None) then
begin
CGContextSaveGState(GlobalCanvas);
CGContextBeginPath(GlobalCanvas);
ShowPath;
ApplyStroke(AStroke, SrcRect, AOpacity);
CGContextStrokePath(GlobalCanvas);
CGContextRestoreGState(GlobalCanvas);
end;
end;
procedure TCanvasHelper.IntersectClipRect(const ARect: TRectF);
begin
if GlobalCanvas <> nil then
CGContextClipToRect(GlobalCanvas, CGRectFromRect(ARect));
end;
procedure TCanvasHelper.ExcludeClipRect(const ARect: TRectF);
var LRect: array[0..3] of CGRect;
begin
if GlobalCanvas <> nil then
begin
LRect[0] := CGRectFromRect(TRectF.Create(-FWidth, -FWidth, ARect.Left, FHeight));
LRect[1] := CGRectFromRect(TRectF.Create(ARect.Right, -FHeight, FWidth, FHeight));
LRect[2] := CGRectFromRect(TRectF.Create(ARect.Left, -FHeight, ARect.Right, ARect.Top));
LRect[3] := CGRectFromRect(TRectF.Create(ARect.Left, ARect.Bottom, ARect.Right, FHeight));
CGContextClipToRects(GlobalCanvas, @LRect[0], 4);
end;
end;
{$ENDIF}
{$IF Defined(IOS) or Defined(ANDROID)}
procedure TCanvasHelper.DrawRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush; const ACornerType: TCornerType = TCornerType.Round; const Inside: Boolean = False);
var Path: TPathData;
R: TRectF;
begin
R := ARect;
// 线在区内
if Inside and (AStroke <> nil) and (AStroke.Kind <> TBrushKind.None) then
InflateRect(R, -(AStroke.Thickness / 2), -(AStroke.Thickness / 2));
Path := TPathData.Create;
try
Path.AddRectangle(R, XRadius, YRadius, ACorners, ACornerType);
DrawPath(Path, AOpacity, AFill, AStroke);
finally
Path.Free;
end;
end;
procedure TCanvasHelper.DrawEllipse(const ARect: TRectF; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush; const Inside: Boolean = False);
var Path: TPathData;
R: TRectF;
begin
R := ARect;
// 线在区内
if Inside and (AStroke <> nil) and (AStroke.Kind <> TBrushKind.None) then
begin
R.Offset(-R.Left, -R.Top);
InflateRect(R, -(AStroke.Thickness / 2), -(AStroke.Thickness / 2));
end;
Path := TPathData.Create;
try
Path.AddEllipse(R);
DrawPath(Path, AOpacity, AFill, AStroke);
finally
Path.Free;
end;
end;
procedure TCanvasHelper.DrawArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush; const Inside: Boolean = False);
var R: TRectF;
P: TPointF;
Path: TPathData;
begin
R := RectF(Center.X - Radius.X, Center.Y - Radius.Y, Center.X + Radius.X, Center.Y + Radius.Y);
// 线在区内
if Inside and (AStroke <> nil) and (AStroke.Kind <> TBrushKind.None) then
begin
R.Offset(-R.Left, -R.Top);
InflateRect(R, -(AStroke.Thickness / 2), -(AStroke.Thickness / 2));
end;
P := PointF(R.Width / 2, R.Height / 2);
Path := TPathData.Create;
try
Path.AddArc(Center, P, StartAngle, SweepAngle);
DrawPath(Path, AOpacity, AFill, AStroke);
finally
Path.Free;
end;
end;
procedure TCanvasHelper.DrawPolygon(const Points: TPolygon; const AOpacity: Single; const AFill: TBrush; const AStroke: TStrokeBrush);
var i: Integer;
Path: TPathData;
PathBreakFound: Boolean;
begin
Path := TPathData.Create;
try
PathBreakFound := False;
for i := 0 to High(Points) do
begin
if i = 0 then
begin
Path.MoveTo(Points[i]);
end
else
if (Points[i].X = PolygonPointBreak.X) and (Points[i].Y = PolygonPointBreak.Y) then
begin
Path.ClosePath;
PathBreakFound := True;
end
else Path.LineTo(Points[i]);
end;
if not PathBreakFound then
Path.ClosePath;
DrawPath(Path, AOpacity, AFill, AStroke);
finally
Path.Free;
end;
end;
procedure TCanvasHelper.FillRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const ACornerType: TCornerType = TCornerType.Round);
begin
DrawRect(ARect, XRadius, YRadius, ACorners, AOpacity, Self.Fill, nil, ACornerType);
end;
procedure TCanvasHelper.FillRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const ABrush: TBrush; const ACornerType: TCornerType = TCornerType.Round);
begin
DrawRect(ARect, XRadius, YRadius, ACorners, AOpacity, ABrush, nil, ACornerType);
end;
procedure TCanvasHelper.DrawRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const ACornerType: TCornerType = TCornerType.Round);
begin
DrawRect(ARect, XRadius, YRadius, ACorners, AOpacity, nil, Self.Stroke, ACornerType);
end;
procedure TCanvasHelper.DrawRect(const ARect: TRectF; const XRadius, YRadius: Single; const ACorners: TCorners; const AOpacity: Single; const ABrush: TStrokeBrush; const ACornerType: TCornerType = TCornerType.Round);
begin
DrawRect(ARect, XRadius, YRadius, ACorners, AOpacity, nil, ABrush, ACornerType);
end;
procedure TCanvasHelper.FillPath(const APath: TPathData; const AOpacity: Single);
begin
DrawPath(APath, AOpacity, Self.Fill, nil);
end;
procedure TCanvasHelper.FillPath(const APath: TPathData; const AOpacity: Single; const ABrush: TBrush);
begin
DrawPath(APath, AOpacity, ABrush, nil);
end;
procedure TCanvasHelper.DrawPath(const APath: TPathData; const AOpacity: Single);
begin
DrawPath(APath, AOpacity, nil, Self.Stroke);
end;
procedure TCanvasHelper.DrawPath(const APath: TPathData; const AOpacity: Single; const ABrush: TStrokeBrush);
begin
DrawPath(APath, AOpacity, nil, ABrush);
end;
procedure TCanvasHelper.FillEllipse(const ARect: TRectF; const AOpacity: Single);
begin
DrawEllipse(ARect, AOpacity, Self.Fill, nil);
end;
procedure TCanvasHelper.FillEllipse(const ARect: TRectF; const AOpacity: Single; const ABrush: TBrush);
begin
DrawEllipse(ARect, AOpacity, ABrush, nil);
end;
procedure TCanvasHelper.DrawEllipse(const ARect: TRectF; const AOpacity: Single);
begin
DrawEllipse(ARect, AOpacity, nil, Self.Stroke);
end;
procedure TCanvasHelper.DrawEllipse(const ARect: TRectF; const AOpacity: Single; const ABrush: TStrokeBrush);
begin
DrawEllipse(ARect, AOpacity, nil, ABrush);
end;
procedure TCanvasHelper.FillArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single);
begin
DrawArc(Center, Radius, StartAngle, SweepAngle, AOpacity, Self.Fill, nil);
end;
procedure TCanvasHelper.FillArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single; const ABrush: TBrush);
begin
DrawArc(Center, Radius, StartAngle, SweepAngle, AOpacity, ABrush, nil);
end;
procedure TCanvasHelper.DrawArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single);
begin
DrawArc(Center, Radius, StartAngle, SweepAngle, AOpacity, nil, Self.Stroke);
end;
procedure TCanvasHelper.DrawArc(const Center, Radius: TPointF; StartAngle, SweepAngle: Single; const AOpacity: Single; const ABrush: TStrokeBrush);
begin
DrawArc(Center, Radius, StartAngle, SweepAngle, AOpacity, nil, ABrush);
end;
procedure TCanvasHelper.FillPolygon(const Points: TPolygon; const AOpacity: Single);
begin
DrawPolygon(Points, AOpacity, Self.Fill, nil);
end;
procedure TCanvasHelper.DrawPolygon(const Points: TPolygon; const AOpacity: Single);
begin
DrawPolygon(Points, AOpacity, nil, Self.Stroke);
end;
{$ENDIF}
{$ENDIF}
end.