forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgracanvas2d.pas
2455 lines (2170 loc) · 77 KB
/
bgracanvas2d.pas
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
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRACanvas2D;
{ To do :
draw text with a different precision if the matrix is scaled
drawImage(in image, in double sx, in double sy, in double sw, in double sh, in double dx, in double dy, in double dw, in double dh)
-> using FillPoly with texture coordinates
linear gradient any transformation
clearPath clipping
createRadialGradient
globalCompositeOperation
image data functions
}
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, BGRATypes, Types, {$IFNDEF FPC} GraphType,{$ENDIF} BGRAGraphics, BGRABitmapTypes, BGRATransform,
BGRAGradientScanner, BGRAPath, BGRAPen;
type
IBGRACanvasTextureProvider2D = interface
['{22252E61-5DFB-4F0A-8952-3A5E0E75D259}']
function getTexture: IBGRAScanner;
property texture: IBGRAScanner read GetTexture;
end;
IBGRACanvasGradient2D = interface(IBGRACanvasTextureProvider2D)
['{7290D78E-83BE-48BB-A0FF-4E637FDA8AEA}']
procedure addColorStop(APosition: single; AColor: TBGRAPixel); overload;
procedure addColorStop(APosition: single; AColor: TColor); overload;
procedure addColorStop(APosition: single; AColor: string); overload;
procedure setColors(ACustomGradient: TBGRACustomGradient);
function GetGammaCorrection: boolean;
procedure SetGammaCorrection(AValue: boolean);
property gammaCorrection: boolean read GetGammaCorrection write SetGammaCorrection;
end;
{ TBGRACanvasTextureProvider2D }
TBGRACanvasTextureProvider2D = class(TInterfacedObject,IBGRACanvasTextureProvider2D)
function getTexture: IBGRAScanner; virtual; abstract;
end;
{ TBGRACanvasState2D }
TBGRACanvasState2D = class
private
FClipMask: TBGRACustomBitmap;
FClipMaskOwned: boolean;
function GetClipMaskReadWrite: TBGRACustomBitmap;
public
strokeColor: TBGRAPixel;
strokeTextureProvider: IBGRACanvasTextureProvider2D;
fillColor: TBGRAPixel;
fillMode: TFillMode;
fillTextureProvider: IBGRACanvasTextureProvider2D;
globalAlpha: byte;
fontName: string;
fontStyle: TFontStyles;
fontEmHeight: single;
textAlign: TAlignment;
textBaseline: string;
lineWidth: single;
penStroker: TBGRAPenStroker;
shadowOffsetX,shadowOffsetY,shadowBlur: single;
shadowColor: TBGRAPixel;
shadowFastest: boolean;
matrix: TAffineMatrix;
constructor Create(AMatrix: TAffineMatrix; AClipMask: TBGRACustomBitmap; AClipMaskOwned: boolean);
function Duplicate: TBGRACanvasState2D;
destructor Destroy; override;
procedure SetClipMask(AClipMask: TBGRACustomBitmap; AOwned: boolean);
property clipMaskReadOnly: TBGRACustomBitmap read FClipMask;
property clipMaskReadWrite: TBGRACustomBitmap read GetClipMaskReadWrite;
end;
TCanvas2dTextSize = record
width,height: single;
end;
{ TBGRACanvas2D }
TBGRACanvas2D = class(TObject,IBGRAPath)
private
FSurface: TBGRACustomBitmap;
StateStack: TList;
currentState: TBGRACanvasState2D;
FCanvasOffset: TPointF;
FPixelCenteredCoordinates: boolean;
FPathPoints: {$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
FPathPointCount: integer;
FFontRenderer: TBGRACustomFontRenderer;
FLastCoord, FStartCoord: TPointF;
function GetCurrentPathAsPoints: ArrayOfTPointF;
function GetFontName: string;
function GetFontRenderer: TBGRACustomFontRenderer;
function GetFontEmHeight: single;
function GetFontString: string;
function GetFontStyle: TFontStyles;
function GetGlobalAlpha: single;
function GetHasShadow: boolean;
function GetHeight: Integer;
function GetLineCap: string;
function GetLineCapLCL: TPenEndCap;
function GetlineJoin: string;
function GetlineJoinLCL: TPenJoinStyle;
function GetLineWidth: single;
function GetMatrix: TAffineMatrix;
function GetMiterLimit: single;
function GetPixelCenteredCoordinates: boolean;
function GetShadowBlur: single;
function GetShadowFastest: boolean;
function GetShadowOffset: TPointF;
function GetShadowOffsetX: single;
function GetShadowOffsetY: single;
function GetStrokeMatrix: TAffineMatrix;
function GetTextAlign: string;
function GetTextAlignLCL: TAlignment;
function GetTextBaseline: string;
function GetFillMode: TFillMode;
function GetWidth: Integer;
procedure SetFontName(AValue: string);
procedure SetFontRenderer(AValue: TBGRACustomFontRenderer);
procedure SetFontEmHeight(AValue: single);
procedure SetFontString(AValue: string);
procedure SetFontStyle(AValue: TFontStyles);
procedure SetGlobalAlpha(const AValue: single);
procedure SetLineCap(const AValue: string);
procedure SetLineCapLCL(AValue: TPenEndCap);
procedure SetLineJoin(const AValue: string);
procedure FillPoly(const points: array of TPointF);
procedure FillStrokePoly(const points: array of TPointF; fillOver: boolean);
procedure SetLineJoinLCL(AValue: TPenJoinStyle);
procedure SetLineWidth(const AValue: single);
procedure SetMatrix(AValue: TAffineMatrix);
procedure SetMiterLimit(const AValue: single);
procedure SetPixelCenteredCoordinates(const AValue: boolean);
procedure SetShadowBlur(const AValue: single);
procedure SetShadowFastest(AValue: boolean);
procedure SetShadowOffset(const AValue: TPointF);
procedure SetShadowOffsetX(const AValue: single);
procedure SetShadowOffsetY(const AValue: single);
procedure SetStrokeMatrix(AValue: TAffineMatrix);
procedure SetTextAlign(AValue: string);
procedure SetTextAlignLCL(AValue: TAlignment);
procedure SetTextBaseine(AValue: string);
procedure SetFillMode(mode: TFillMode);
procedure StrokePoly(const points: array of TPointF);
procedure DrawShadow(const points, points2: array of TPointF; AFillMode: TFillMode = fmWinding);
procedure ClearPoly(const points: array of TPointF);
function ApplyTransform(const points: array of TPointF; matrix: TAffineMatrix): ArrayOfTPointF; overload;
function ApplyTransform(const points: array of TPointF): ArrayOfTPointF; overload;
function ApplyTransform(point: TPointF): TPointF; overload;
function GetPenPos(defaultX, defaultY: single): TPointF; overload;
function GetPenPos(defaultPt: TPointF): TPointF; overload;
procedure AddPoint(point: TPointF);
procedure AddPoints(const points: array of TPointF);
procedure AddPointsRev(const points: array of TPointF);
function ApplyGlobalAlpha(color: TBGRAPixel): TBGRAPixel;
function GetDrawMode: TDrawMode;
procedure copyTo({%H-}dest: IBGRAPath); //IBGRAPath
function getPoints: ArrayOfTPointF; overload; //IBGRAPath
function getPoints(AMatrix: TAffineMatrix): ArrayOfTPointF; overload; //IBGRAPath
function getCursor: TBGRACustomPathCursor; //IBGRAPath
public
antialiasing, linearBlend, gradientGammaCorrection: boolean;
constructor Create(ASurface: TBGRACustomBitmap);
destructor Destroy; override;
function toDataURL(mimeType: string = 'image/png'): string;
procedure save;
procedure restore;
procedure scale(x,y: single); overload;
procedure scale(factor: single); overload;
procedure rotate(angleRadCW: single);
procedure translate(x,y: single);
procedure skewx(angleRadCW: single);
procedure skewy(angleRadCW: single);
procedure transform(m11,m21, m12,m22, m13,m23: single); overload;
procedure transform(AMatrix: TAffineMatrix); overload;
procedure setTransform(m11,m21, m12,m22, m13,m23: single);
procedure resetTransform;
procedure strokeScale(x,y: single);
procedure strokeSkewx(angleRadCW: single);
procedure strokeSkewy(angleRadCW: single);
procedure strokeResetTransform;
procedure strokeStyle(color: TBGRAPixel); overload;
procedure strokeStyle(color: TColor); overload;
procedure strokeStyle(color: string); overload;
procedure strokeStyle(texture: IBGRAScanner); overload;
procedure strokeStyle(provider: IBGRACanvasTextureProvider2D); overload;
procedure fillStyle(color: TBGRAPixel); overload;
procedure fillStyle(color: TColor); overload;
procedure fillStyle(color: string); overload;
procedure fillStyle(texture: IBGRAScanner); overload;
procedure fillStyle(provider: IBGRACanvasTextureProvider2D); overload;
procedure shadowColor(color: TBGRAPixel); overload;
procedure shadowColor(color: TColor); overload;
procedure shadowColor(color: string); overload;
procedure shadowNone;
function getShadowColor: TBGRAPixel;
function createLinearGradient(x0,y0,x1,y1: single): IBGRACanvasGradient2D; overload;
function createLinearGradient(p0,p1: TPointF): IBGRACanvasGradient2D; overload;
function createLinearGradient(x0,y0,x1,y1: single; Colors: TBGRACustomGradient): IBGRACanvasGradient2D; overload;
function createLinearGradient(p0,p1: TPointF; Colors: TBGRACustomGradient): IBGRACanvasGradient2D; overload;
function createRadialGradient(x0,y0,r0,x1,y1,r1: single; flipGradient: boolean=false): IBGRACanvasGradient2D; overload;
function createRadialGradient(p0: TPointF; r0: single; p1: TPointF; r1: single; flipGradient: boolean=false): IBGRACanvasGradient2D; overload;
function createRadialGradient(x0,y0,r0,x1,y1,r1: single; Colors: TBGRACustomGradient; flipGradient: boolean=false): IBGRACanvasGradient2D; overload;
function createRadialGradient(p0: TPointF; r0: single; p1: TPointF; r1: single; Colors: TBGRACustomGradient; flipGradient: boolean=false): IBGRACanvasGradient2D; overload;
function createPattern(image: TBGRACustomBitmap; repetition: string): IBGRACanvasTextureProvider2D; overload;
function createPattern(texture: IBGRAScanner): IBGRACanvasTextureProvider2D; overload;
procedure fillRect(x,y,w,h: single);
procedure strokeRect(x,y,w,h: single);
procedure clearRect(x,y,w,h: single);
procedure addPath(APath: IBGRAPath); overload;
procedure addPath(ASvgPath: string); overload;
procedure path(APath: IBGRAPath); overload;
procedure path(ASvgPath: string); overload;
procedure beginPath;
procedure closePath;
procedure toSpline(closed: boolean; style: TSplineStyle= ssOutside);
procedure moveTo(x,y: single); overload;
procedure lineTo(x,y: single); overload;
procedure moveTo(const pt: TPointF); overload;
procedure lineTo(const pt: TPointF); overload;
procedure polylineTo(const pts: array of TPointF);
procedure quadraticCurveTo(cpx,cpy,x,y: single); overload;
procedure quadraticCurveTo(const cp,pt: TPointF); overload;
procedure bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y: single); overload;
procedure bezierCurveTo(const cp1,cp2,pt: TPointF); overload;
procedure rect(x,y,w,h: single);
procedure roundRect(x,y,w,h,radius: single); overload;
procedure roundRect(x,y,w,h,rx,ry: single); overload;
procedure openedSpline(const pts: array of TPointF; style: TSplineStyle);
procedure closedSpline(const pts: array of TPointF; style: TSplineStyle);
procedure spline(const pts: array of TPointF; style: TSplineStyle= ssOutside);
procedure splineTo(const pts: array of TPointF; style: TSplineStyle= ssOutside);
procedure arc(x, y, radius, startAngleRadCW, endAngleRadCW: single; anticlockwise: boolean); overload;
procedure arc(x, y, radius, startAngleRadCW, endAngleRadCW: single); overload;
procedure arc(cx, cy, rx,ry, xAngleRadCW, startAngleRadCW, endAngleRadCW: single; anticlockwise: boolean); overload;
procedure arc(cx, cy, rx,ry, xAngleRadCW, startAngleRadCW, endAngleRadCW: single); overload;
procedure arc(const arcDef: TArcDef); overload;
procedure arcTo(x1, y1, x2, y2, radius: single); overload;
procedure arcTo(p1,p2: TPointF; radius: single); overload;
procedure arcTo(rx, ry, xAngleRadCW: single; largeArc,anticlockwise: boolean; x, y: single); overload;
procedure circle(x,y,r: single);
procedure ellipse(x,y,rx,ry: single);
procedure text(AText: string; x,y: single);
procedure fillText(AText: string; x,y: single);
procedure strokeText(AText: string; x,y: single);
function measureText(AText: string): TCanvas2dTextSize;
procedure fill;
procedure stroke;
procedure fillOverStroke;
procedure strokeOverFill;
procedure clearPath;
procedure clip;
procedure unclip;
function isPointInPath(x,y: single): boolean; overload;
function isPointInPath(pt: TPointF): boolean; overload;
procedure drawImage(image: TBGRACustomBitmap; dx,dy: single); overload;
procedure drawImage(image: TBGRACustomBitmap; dx,dy,dw,dh: single); overload;
function getLineStyle: TBGRAPenStyle;
procedure lineStyle(const AValue: array of single); overload;
procedure lineStyle(AStyle: TPenStyle); overload;
property surface: TBGRACustomBitmap read FSurface;
property width: Integer read GetWidth;
property height: Integer read GetHeight;
property pixelCenteredCoordinates: boolean read GetPixelCenteredCoordinates write SetPixelCenteredCoordinates;
property globalAlpha: single read GetGlobalAlpha write SetGlobalAlpha;
property matrix: TAffineMatrix read GetMatrix write SetMatrix;
property strokeMatrix: TAffineMatrix read GetStrokeMatrix write SetStrokeMatrix;
property lineWidth: single read GetLineWidth write SetLineWidth;
property lineCap: string read GetLineCap write SetLineCap;
property lineCapLCL: TPenEndCap read GetLineCapLCL write SetLineCapLCL;
property lineJoin: string read GetlineJoin write SetLineJoin;
property lineJoinLCL: TPenJoinStyle read GetlineJoinLCL write SetLineJoinLCL;
property miterLimit: single read GetMiterLimit write SetMiterLimit;
property shadowOffsetX: single read GetShadowOffsetX write SetShadowOffsetX;
property shadowOffsetY: single read GetShadowOffsetY write SetShadowOffsetY;
property shadowOffset: TPointF read GetShadowOffset write SetShadowOffset;
property shadowBlur: single read GetShadowBlur write SetShadowBlur;
property shadowFastest: boolean read GetShadowFastest write SetShadowFastest;
property hasShadow: boolean read GetHasShadow;
property fontName: string read GetFontName write SetFontName;
property fontEmHeight: single read GetFontEmHeight write SetFontEmHeight;
property fontStyle: TFontStyles read GetFontStyle write SetFontStyle;
property font: string read GetFontString write SetFontString;
property textAlignLCL: TAlignment read GetTextAlignLCL write SetTextAlignLCL;
property textAlign: string read GetTextAlign write SetTextAlign;
property textBaseline: string read GetTextBaseline write SetTextBaseine;
property fillMode: TFillMode read GetFillMode write SetFillMode;
property currentPath: ArrayOfTPointF read GetCurrentPathAsPoints;
property fontRenderer: TBGRACustomFontRenderer read GetFontRenderer write SetFontRenderer;
protected
{$IFNDEF FPC}
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: integer; stdcall;
function _Release: integer; stdcall;
{$ELSE}
function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult; {$IF (not defined(WINDOWS)) AND (not defined(FPC20501))}cdecl{$ELSE}stdcall{$IFEND};
function _AddRef: integer; {$IF (not defined(WINDOWS)) AND (not defined(FPC20501))}cdecl{$ELSE}stdcall{$IFEND};
function _Release: integer; {$IF (not defined(WINDOWS)) AND (not defined(FPC20501))}cdecl{$ELSE}stdcall{$IFEND};
{$ENDIF}
end;
implementation
uses Math, BGRAFillInfo, BGRAPolygon, BGRABlend, FPWriteJPEG, FPWriteBMP, base64;
type
TColorStop = record
position: single;
color: TBGRAPixel;
end;
TGradientArrayOfColors = array of TBGRAPixel;
TGradientArrayOfPositions = array of single;
{ TBGRACanvasGradient2D }
TBGRACanvasGradient2D = class(TBGRACanvasTextureProvider2D, IBGRACanvasGradient2D)
private
colorStops: array of TColorStop;
nbColorStops: integer;
FCustomGradient: TBGRACustomGradient;
FGammaCorrection: boolean;
protected
scanner: TBGRAGradientScanner;
procedure CreateScanner; virtual; abstract;
function getColorArray: TGradientArrayOfColors;
function getPositionArray: TGradientArrayOfPositions;
procedure GetBGRAGradient(out ABGRAGradient: TBGRACustomGradient; out AOwned: boolean);
function GetGammaCorrection: boolean;
procedure SetGammaCorrection(AValue: boolean);
public
constructor Create;
function getTexture: IBGRAScanner; override;
destructor Destroy; override;
procedure addColorStop(APosition: single; AColor: TBGRAPixel); overload;
procedure addColorStop(APosition: single; AColor: TColor); overload;
procedure addColorStop(APosition: single; AColor: string); overload;
procedure setColors(ACustomGradient: TBGRACustomGradient);
property texture: IBGRAScanner read GetTexture;
property colorStopCount: integer read nbColorStops;
property gammaCorrection: boolean read GetGammaCorrection write SetGammaCorrection;
end;
{ TBGRACanvasLinearGradient2D }
TBGRACanvasLinearGradient2D = class(TBGRACanvasGradient2D)
protected
o1,o2: TPointF;
FTransform: TAffineMatrix;
procedure CreateScanner; override;
public
constructor Create(x0,y0,x1,y1: single; transform: TAffineMatrix); overload;
constructor Create(p0,p1: TPointF; transform: TAffineMatrix); overload;
end;
{ TBGRACanvasRadialGradient2D }
TBGRACanvasRadialGradient2D = class(TBGRACanvasGradient2D)
protected
c0,c1: TPointF;
cr0,cr1: single;
FFlipGradient: boolean;
FTransform: TAffineMatrix;
procedure CreateScanner; override;
public
constructor Create(x0,y0,r0,x1,y1,r1: single; transform: TAffineMatrix; flipGradient: boolean=false); overload;
constructor Create(p0: TPointF; r0: single; p1: TPointF; r1: single; transform: TAffineMatrix; flipGradient: boolean=false); overload;
end;
{ TBGRACanvasPattern2D }
TBGRACanvasPattern2D = class(TBGRACanvasTextureProvider2D)
protected
scanner: TBGRACustomScanner;
foreignInterface: IBGRAScanner;
ownScanner: boolean;
public
function getTexture: IBGRAScanner; override;
constructor Create(source: TBGRACustomBitmap; repeatX,repeatY: boolean; Origin, HAxis, VAxis: TPointF); overload;
constructor Create(source: IBGRAScanner; transformation: TAffineMatrix); overload;
destructor Destroy; override;
end;
{ TBGRACanvasPattern2D }
function TBGRACanvasPattern2D.GetTexture: IBGRAScanner;
begin
if ownScanner then
result := scanner
else
result := foreignInterface;
end;
constructor TBGRACanvasPattern2D.Create(source: TBGRACustomBitmap; repeatX,
repeatY: boolean; Origin, HAxis, VAxis: TPointF);
var
affine: TBGRAAffineBitmapTransform;
begin
if (abs(Origin.X-round(Origin.X)) < 1e-6) and
(abs(Origin.Y-round(Origin.Y)) < 1e-6) and
(HAxis = Origin+PointF(1,0)) and
(VAxis = Origin+PointF(0,1)) then
begin
if (round(Origin.X)=0) and (round(Origin.Y)=0) and repeatX and repeatY then
begin
foreignInterface := source;
ownScanner:= false;
end else
begin
scanner := TBGRABitmapScanner.Create(source,repeatX,repeatY,Point(round(Origin.X),round(Origin.Y)));
ownScanner := true;
end;
end
else
begin
affine := TBGRAAffineBitmapTransform.Create(source,repeatX,repeatY);
affine.Fit(Origin,HAxis,VAxis);
scanner := affine;
ownScanner:= true;
end;
end;
constructor TBGRACanvasPattern2D.Create(source: IBGRAScanner;
transformation: TAffineMatrix);
var
affine : TBGRAAffineScannerTransform;
begin
if (abs(transformation[1,1]-1) < 1e-6) and
(abs(transformation[2,2]-1) < 1e-6) and
(abs(transformation[1,2]) < 1e-6) and
(abs(transformation[2,1]) < 1e-6) and
(abs(transformation[1,3]-round(transformation[1,3])) < 1e-6) and
(abs(transformation[2,3]-round(transformation[2,3])) < 1e-6) then
begin
if (abs(transformation[1,3]) < 1e-6) and
(abs(transformation[2,3]) < 1e-6) then
begin
foreignInterface := source;
ownScanner := false;
end else
begin
scanner := TBGRAScannerOffset.Create(source,Point(round(transformation[1,3]),round(transformation[2,3])));
ownScanner := true;
end;
end else
begin
affine := TBGRAAffineScannerTransform.Create(source);
affine.Matrix := transformation;
affine.Invert;
scanner := affine;
ownScanner:= true;
end;
end;
destructor TBGRACanvasPattern2D.Destroy;
begin
fillchar(foreignInterface,sizeof(foreignInterface),0);
if ownScanner then FreeAndNil(scanner);
inherited Destroy;
end;
{ TBGRACanvasLinearGradient2D }
procedure TBGRACanvasLinearGradient2D.CreateScanner;
var GradientOwner: boolean;
GradientColors: TBGRACustomGradient;
begin
GetBGRAGradient(GradientColors,GradientOwner);
scanner := TBGRAGradientScanner.Create(GradientColors,gtLinear,o1,o2,False,GradientOwner);
scanner.Transform := FTransform;
end;
constructor TBGRACanvasLinearGradient2D.Create(x0, y0, x1, y1: single; transform: TAffineMatrix);
begin
o1 := PointF(x0,y0);
o2 := PointF(x1,y1);
FTransform := transform;
end;
constructor TBGRACanvasLinearGradient2D.Create(p0, p1: TPointF; transform: TAffineMatrix);
begin
o1 := p0;
o2 := p1;
FTransform := transform;
end;
{ TBGRACanvasRadialGradient2D }
procedure TBGRACanvasRadialGradient2D.CreateScanner;
var GradientOwner: boolean;
GradientColors: TBGRACustomGradient;
begin
GetBGRAGradient(GradientColors,GradientOwner);
scanner := TBGRAGradientScanner.Create(GradientColors,c0,cr0,c1,cr1,GradientOwner);
scanner.FlipGradient := not FFlipGradient;
scanner.Transform := FTransform;
end;
constructor TBGRACanvasRadialGradient2D.Create(x0, y0, r0, x1, y1, r1: single;
transform: TAffineMatrix; flipGradient: boolean);
begin
self.c0 := PointF(x0,y0);
self.cr0 := r0;
self.c1 := PointF(x1,y1);
self.cr1 := r1;
FTransform := transform;
FFlipGradient := flipGradient;
end;
constructor TBGRACanvasRadialGradient2D.Create(p0: TPointF; r0: single;
p1: TPointF; r1: single; transform: TAffineMatrix; flipGradient: boolean);
begin
self.c0 := p0;
self.cr0 := r0;
self.c1 := p1;
self.cr1 := r1;
FTransform := transform;
FFlipGradient := flipGradient;
end;
{ TBGRACanvasGradient2D }
function TBGRACanvasGradient2D.getTexture: IBGRAScanner;
begin
if scanner = nil then CreateScanner;
result := scanner;
end;
function TBGRACanvasGradient2D.GetGammaCorrection: boolean;
begin
result := FGammaCorrection;
end;
procedure TBGRACanvasGradient2D.SetGammaCorrection(AValue: boolean);
begin
FGammaCorrection:= AValue;
FreeAndNil(scanner);
end;
constructor TBGRACanvasGradient2D.Create;
begin
inherited Create;
scanner := nil;
FGammaCorrection:= false;
end;
function TBGRACanvasGradient2D.getColorArray: TGradientArrayOfColors;
var
i: Integer;
begin
setlength(result, nbColorStops);
for i := 0 to nbColorStops-1 do
result[i] := colorStops[i].color;
end;
function TBGRACanvasGradient2D.getPositionArray: TGradientArrayOfPositions;
var
i: Integer;
begin
setlength(result, nbColorStops);
for i := 0 to nbColorStops-1 do
result[i] := colorStops[i].position;
end;
procedure TBGRACanvasGradient2D.GetBGRAGradient(out
ABGRAGradient: TBGRACustomGradient; out AOwned: boolean);
begin
if FCustomGradient = nil then
begin
if (colorStopCount = 2) and (colorStops[0].position = 0) and (colorStops[1].position = 1) then
begin
if FGammaCorrection then
ABGRAGradient := TBGRASimpleGradientWithGammaCorrection.Create(colorStops[0].color, colorStops[1].color)
else
ABGRAGradient := TBGRASimpleGradientWithoutGammaCorrection.Create(colorStops[0].color, colorStops[1].color);
end
else
ABGRAGradient := TBGRAMultiGradient.Create(getColorArray,getPositionArray,FGammaCorrection,False);
AOwned := true;
end else
begin
ABGRAGradient := FCustomGradient;
AOwned := false;
end;
end;
destructor TBGRACanvasGradient2D.Destroy;
begin
FreeAndNil(scanner);
inherited Destroy;
end;
procedure TBGRACanvasGradient2D.addColorStop(APosition: single;
AColor: TBGRAPixel);
begin
FreeAndNil(scanner);
if nbColorStops = length(colorStops) then
setlength(colorStops, (length(colorStops)+1)*2);
with colorStops[nbColorStops] do
begin
position := APosition;
color := AColor;
end;
inc(nbColorStops);
end;
procedure TBGRACanvasGradient2D.addColorStop(APosition: single; AColor: TColor
);
begin
addColorStop(APosition, ColorToBGRA(ColorToRGB(AColor)));
end;
procedure TBGRACanvasGradient2D.addColorStop(APosition: single; AColor: string
);
begin
addColorStop(APosition, StrToBGRA(AColor));
end;
procedure TBGRACanvasGradient2D.setColors(ACustomGradient: TBGRACustomGradient
);
begin
FCustomGradient := ACustomGradient;
end;
{ TBGRACanvasState2D }
function TBGRACanvasState2D.GetClipMaskReadWrite: TBGRACustomBitmap;
begin
if not FClipMaskOwned then
begin
if FClipMask <> nil then
FClipMask := FClipMask.Duplicate;
FClipMaskOwned := true;
end;
result := FClipMask;
end;
constructor TBGRACanvasState2D.Create(AMatrix: TAffineMatrix;
AClipMask: TBGRACustomBitmap; AClipMaskOwned: boolean);
begin
strokeColor := BGRABlack;
fillColor := BGRABlack;
globalAlpha := 255;
fontName := 'Arial';
fontEmHeight := 10;
fontStyle := [];
textAlign:= taLeftJustify;
textBaseline := 'alphabetic';
lineWidth := 1;
penStroker := TBGRAPenStroker.Create;
penStroker.LineCap := pecFlat;
penStroker.JoinStyle := pjsMiter;
penStroker.CustomPenStyle := DuplicatePenStyle(SolidPenStyle);
penStroker.MiterLimit := 10;
penStroker.StrokeMatrix := AffineMatrixIdentity;
shadowOffsetX := 0;
shadowOffsetY := 0;
shadowBlur := 0;
shadowColor := BGRAPixelTransparent;
shadowFastest:= false;
matrix := AMatrix;
FClipMask := nil;
FClipMaskOwned := true;
SetClipMask(AClipMask,AClipMaskOwned);
end;
function TBGRACanvasState2D.Duplicate: TBGRACanvasState2D;
begin
result := TBGRACanvasState2D.Create(matrix,clipMaskReadOnly,false);
result.strokeColor := strokeColor;
result.strokeTextureProvider := strokeTextureProvider;
result.fillColor := fillColor;
result.fillMode := fillMode;
result.fillTextureProvider := fillTextureProvider;
result.globalAlpha := globalAlpha;
result.fontName:= fontName;
result.fontEmHeight := fontEmHeight;
result.fontStyle := fontStyle;
result.lineWidth := lineWidth;
result.penStroker.LineCap := penStroker.LineCap;
result.penStroker.JoinStyle := penStroker.JoinStyle;
result.penStroker.CustomPenStyle := DuplicatePenStyle(penStroker.CustomPenStyle);
result.penStroker.MiterLimit := penStroker.MiterLimit;
result.penStroker.StrokeMatrix := penStroker.StrokeMatrix;
result.shadowOffsetX := shadowOffsetX;
result.shadowOffsetY := shadowOffsetY;
result.shadowBlur := shadowBlur;
result.shadowColor := shadowColor;
result.shadowFastest := shadowFastest;
end;
destructor TBGRACanvasState2D.Destroy;
begin
if FClipMaskOwned and Assigned(FClipMask) then
FClipMask.Free;
penStroker.Free;
inherited Destroy;
end;
procedure TBGRACanvasState2D.SetClipMask(AClipMask: TBGRACustomBitmap;
AOwned: boolean);
begin
if FClipMaskOwned and Assigned(FClipMask) then FreeAndNil(FClipMask);
FClipMask := AClipMask;
FClipMaskOwned := AOwned;
end;
{ TBGRACanvas2D }
function TBGRACanvas2D.GetHeight: Integer;
begin
if Assigned(surface) then
result := Surface.Height
else
result := 0;
end;
function TBGRACanvas2D.GetLineCap: string;
begin
case currentState.penStroker.LineCap of
pecRound: result := 'round';
pecSquare: result := 'square';
else result := 'butt';
end;
end;
function TBGRACanvas2D.GetLineCapLCL: TPenEndCap;
begin
result := currentState.penStroker.LineCap;
end;
function TBGRACanvas2D.GetlineJoin: string;
begin
case currentState.penStroker.JoinStyle of
pjsBevel: result := 'bevel';
pjsRound: result := 'round';
else result := 'miter';
end;
end;
function TBGRACanvas2D.GetlineJoinLCL: TPenJoinStyle;
begin
result := currentState.penStroker.JoinStyle;
end;
function TBGRACanvas2D.getLineStyle: TBGRAPenStyle;
begin
result := DuplicatePenStyle(currentState.penStroker.CustomPenStyle);
end;
function TBGRACanvas2D.GetLineWidth: single;
begin
result := currentState.lineWidth;
end;
function TBGRACanvas2D.GetMatrix: TAffineMatrix;
begin
result := currentState.matrix;
end;
function TBGRACanvas2D.GetMiterLimit: single;
begin
result := currentState.penStroker.MiterLimit;
end;
function TBGRACanvas2D.GetPixelCenteredCoordinates: boolean;
begin
result := FPixelCenteredCoordinates;
end;
function TBGRACanvas2D.GetShadowBlur: single;
begin
result := currentState.shadowBlur;
end;
function TBGRACanvas2D.GetShadowFastest: boolean;
begin
result := currentState.shadowFastest;
end;
function TBGRACanvas2D.GetShadowOffset: TPointF;
begin
result := PointF(shadowOffsetX,shadowOffsetY);
end;
function TBGRACanvas2D.GetShadowOffsetX: single;
begin
result := currentState.shadowOffsetX;
end;
function TBGRACanvas2D.GetShadowOffsetY: single;
begin
result := currentState.shadowOffsetY;
end;
function TBGRACanvas2D.GetStrokeMatrix: TAffineMatrix;
begin
result := currentState.penStroker.StrokeMatrix;
end;
function TBGRACanvas2D.GetTextAlign: string;
begin
case currentState.textAlign of
taRightJustify: result := 'right';
taCenter: result := 'center';
else
result := 'left';
end;
end;
function TBGRACanvas2D.GetTextAlignLCL: TAlignment;
begin
result := currentState.textAlign;
end;
function TBGRACanvas2D.GetTextBaseline: string;
begin
result := currentState.textBaseline;
end;
function TBGRACanvas2D.GetGlobalAlpha: single;
begin
result := currentState.globalAlpha/255;
end;
function TBGRACanvas2D.GetCurrentPathAsPoints: ArrayOfTPointF;
var i: integer;
begin
setlength(result, FPathPointCount);
for i := 0 to high(result) do
result[i] := FPathPoints[i];
end;
function TBGRACanvas2D.GetFontName: string;
begin
result := currentState.fontName;
end;
function TBGRACanvas2D.GetFontRenderer: TBGRACustomFontRenderer;
var zoom1,zoom2,zoom: single;
begin
if FFontRenderer = nil then
begin
if FSurface <> nil then
result := FSurface.FontRenderer
else
result := nil;
end else
result := FFontRenderer;
if Assigned(result) then
begin
result.FontName := currentState.fontName;
result.FontStyle := currentState.fontStyle;
if antialiasing then
result.FontQuality:= fqFineAntialiasing
else
result.FontQuality := fqSystem;
result.FontOrientation := 0;
zoom1 := VectLen(currentState.matrix[1,1],currentState.matrix[2,1]);
zoom2 := VectLen(currentState.matrix[1,2],currentState.matrix[2,2]);
if zoom1>zoom2 then zoom := zoom1 else zoom := zoom2;
result.FontEmHeight := round(currentState.fontEmHeight*zoom);
end;
end;
function TBGRACanvas2D.GetFontEmHeight: single;
begin
result := currentState.fontEmHeight;
end;
function TBGRACanvas2D.GetFontString: string;
var formats: TFormatSettings;
begin
formats := {$IFDEF FPC}DefaultFormatSettings;{$ELSE}FormatSettings;{$ENDIF}
formats.DecimalSeparator := '.';
result := '';
if fsItalic in currentState.fontStyle then
result := result+'italic ';
if fsBold in currentState.fontStyle then
result := result +'bold ';
result := result +FloatToStrF(currentState.fontEmHeight,ffGeneral,6,0,formats)+'px ';
result := result +currentState.fontName;
result := trim(result);
end;
function TBGRACanvas2D.GetFontStyle: TFontStyles;
begin
result := currentState.fontStyle;
end;
function TBGRACanvas2D.GetHasShadow: boolean;
begin
result := (ApplyGlobalAlpha(currentState.shadowColor).alpha <> 0) and
( (currentState.shadowBlur <> 0) or (currentState.shadowOffsetX <> 0)
or (currentState.shadowOffsetY <> 0) );
end;
function TBGRACanvas2D.GetWidth: Integer;
begin
if Assigned(Surface) then
result := Surface.Width
else
result := 0;
end;
procedure TBGRACanvas2D.SetFontName(AValue: string);
begin
currentState.fontName := AValue;
end;
procedure TBGRACanvas2D.SetFontRenderer(AValue: TBGRACustomFontRenderer);
begin
if AValue = FFontRenderer then exit;
FreeAndNil(FFontRenderer);
FFontRenderer := AValue;
end;
procedure TBGRACanvas2D.SetFontEmHeight(AValue: single);
begin
currentState.fontEmHeight := AValue;
end;
procedure TBGRACanvas2D.SetFontString(AValue: string);
var idxSpace,errPos: integer;
attrib,u: string;
value: single;