forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgracanvas.pas
1664 lines (1494 loc) · 47.3 KB
/
bgracanvas.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 | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRACanvas;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, BGRATypes, FPCanvas, BGRAGraphics, Types, FPImage, BGRABitmapTypes, GraphType;
type
{ TBGRAColoredObject }
TBGRAColoredObject = class
private
function GetColor: TColor;
function GetOpacity: Byte;
procedure SetColor(const AValue: TColor);
procedure SetOpacity(const AValue: Byte);
public
BGRAColor: TBGRAPixel;
procedure Assign(Source: TObject); virtual;
property Color: TColor read GetColor write SetColor;
property Opacity: Byte read GetOpacity write SetOpacity;
end;
{ TBGRAPen }
TBGRAPen = class(TBGRAColoredObject)
private
function GetActualColor: TBGRAPixel;
function GetActualWidth: integer;
function GetCustomPenStyle: TBGRAPenStyle;
function GetPenStyle: TPenStyle;
procedure SetCustomPenStyle(const AValue: TBGRAPenStyle);
procedure SetPenStyle(const AValue: TPenStyle);
protected
FCustomPenStyle: TBGRAPenStyle;
FPenStyle: TPenStyle;
public
Width: Integer;
EndCap: TPenEndCap;
JoinStyle: TPenJoinStyle;
constructor Create;
procedure Assign(Source: TObject); override;
property Style: TPenStyle read GetPenStyle Write SetPenStyle;
property CustomStyle: TBGRAPenStyle read GetCustomPenStyle write SetCustomPenStyle;
property ActualWidth: integer read GetActualWidth;
property ActualColor: TBGRAPixel read GetActualColor;
end;
{$IFNDEF FPC}
TPen = TBGRAPen;
{$ENDIF}
{ TBGRABrush }
TBGRABrush = class(TBGRAColoredObject)
private
function GetActualColor: TBGRAPixel;
function GetInvisible: boolean;
procedure SetBackColor(const AValue: TBGRAPixel);
procedure SetBrushStyle(const AValue: TBrushStyle);
protected
FStyle: TBrushStyle;
FBackColor: TBGRAPixel;
InternalBitmap: TBGRACustomBitmap;
InternalBitmapColor: TBGRAPixel;
public
Texture: IBGRAScanner;
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TObject); override;
function BuildTexture(Prototype: TBGRACustomBitmap): IBGRAScanner;
property Style: TBrushStyle read FStyle write SetBrushStyle;
property BackColor: TBGRAPixel read FBackColor write SetBackColor;
property ActualColor: TBGRAPixel read GetActualColor;
property Invisible: boolean read GetInvisible;
end;
{$IFNDEF FPC}
TBrush = TBGRABrush;
{$ENDIF}
{ TBGRAFont }
TBGRAFont = class(TBGRAColoredObject)
private
function GetAntialiasing: Boolean;
procedure SetAntialiasing(const AValue: Boolean);
public
Name: string;
Height: Integer;
Style: TFontStyles;
Quality : TBGRAFontQuality;
Orientation: integer;
Texture: IBGRAScanner;
constructor Create;
procedure Assign(Source: TObject); override;
property Antialiasing: Boolean read GetAntialiasing write SetAntialiasing;
end;
{$IFNDEF FPC}
TFont = TBGRAFont;
{$ENDIF}
{ TBGRACanvas }
TBGRACanvas = class
procedure SetBrush(const AValue: TBGRABrush);
procedure SetPen(const AValue: TBGRAPen);
function GetPixelColor(X, Y: Integer): TColor;
procedure SetPixelColor(X, Y: Integer; const AValue: TColor);
private
function GetClipping: Boolean;
function GetClipRect: TRect;
function GetExpandedPixel(X, Y: Integer): TExpandedPixel;
function GetFPPixelColor(X, Y: Integer): TFPColor;
function GetHeight: integer;
function GetWidth: integer;
procedure SetClipping(const AValue: Boolean);
procedure SetClipRect(const AValue: TRect);
procedure SetExpandedPixel(X, Y: Integer; const AValue: TExpandedPixel);
procedure SetFont(const AValue: TBGRAFont);
procedure SetFPPixelColor(X, Y: Integer; const AValue: TFPColor);
function ComputeEllipseC(x1, y1, x2, y2: integer; out cx,cy,rx,ry: single): boolean;
function CheckRectangle(var x1, y1, x2, y2: integer; out tx,ty: integer): boolean;
protected
FBitmap: TBGRACustomBitmap;
FBrush: TBGRABrush;
FPen: TBGRAPen;
FPenPos: TPoint;
FFont : TBGRAFont;
FInactiveClipRect: TRect;
FClippingOn: Boolean;
procedure ApplyPenStyle;
procedure ApplyFont;
function NoPen: boolean;
function NoBrush: boolean;
public
AntialiasingMode: TAntialiasingMode;
FillMode : TFillMode;
TextStyle : TTextStyle;
DrawFontBackground : boolean;
constructor Create(ABitmap: TBGRACustomBitmap);
destructor Destroy; override;
procedure MoveTo(x,y: integer); overload;
procedure MoveTo(p: TPoint); overload;
procedure LineTo(x,y: integer); overload;
procedure LineTo(p: TPoint); overload;
procedure Arc(x1,y1,x2,y2,sx,sy,ex,ey: integer); overload;
procedure Arc(x1,y1,x2,y2,StartDeg16,LengthDeg16: integer); overload;
procedure Arc65536(x1,y1,x2,y2: integer; start65536,end65536: BGRAWord; Options: TArcOptions);
procedure Chord(x1,y1,x2,y2,sx,sy,ex,ey: integer); overload;
procedure Chord(x1,y1,x2,y2,StartDeg16,LengthDeg16: integer); overload;
procedure Pie(x1,y1,x2,y2,sx,sy,ex,ey: integer); overload;
procedure Pie(x1,y1,x2,y2,StartDeg16,LengthDeg16: integer); overload;
procedure RadialPie(x1,y1,x2,y2,StartDeg16,LengthDeg16: integer);
procedure Ellipse(x1,y1,x2,y2: integer); overload;
procedure Ellipse(const bounds: TRect); overload;
procedure Rectangle(x1,y1,x2,y2: integer; Filled: Boolean = True); overload;
procedure Rectangle(const bounds: TRect; Filled: Boolean = True); overload;
procedure Frame(x1,y1,x2,y2: integer); overload;
procedure Frame(const bounds: TRect); overload;
procedure RoundRect(x1,y1,x2,y2: integer; dx,dy: integer); overload;
procedure RoundRect(const bounds: TRect; dx,dy: integer); overload;
procedure EllipseC(x,y,rx,ry: integer);
procedure FillRect(x1,y1,x2,y2: integer); overload;
procedure FillRect(const bounds: TRect); overload;
procedure FrameRect(x1,y1,x2,y2: integer; width: integer = 1); overload;
procedure FrameRect(const bounds: TRect; width: integer = 1); overload;
procedure Frame3D(var bounds: TRect; width: integer; Style: TGraphicsBevelCut); overload;
procedure Frame3D(var bounds: TRect; width: integer; Style: TGraphicsBevelCut; LightColor: TBGRAPixel; ShadowColor: TBGRAPixel); overload;
procedure GradientFill(ARect: TRect; AStart, AStop: TColor;
ADirection: TGradientDirection; GammaCorrection: Boolean = false);
procedure FloodFill(X, Y: Integer; FillColor: TColor; FillStyle: TFillStyle); overload;
procedure FloodFill(X, Y: Integer; FillColor: TBGRAPixel; FillStyle: TFillStyle); overload;
procedure FloodFill(X, Y: Integer); overload;
procedure Polygon(const APoints: array of TPoint); overload;
procedure Polygon(const Points: array of TPoint;
Winding: Boolean;
StartIndex: Integer = 0;
NumPts: Integer = -1); overload;
procedure Polygon(Points: PPoint; NumPts: Integer;
Winding: boolean = False); overload;
procedure PolygonF(const APoints: array of TPointF); overload;
procedure PolygonF(const APoints: array of TPointF; Winding: Boolean; FillOnly: Boolean = False); overload;
procedure Polyline(const APoints: array of TPoint); overload;
procedure Polyline(const Points: array of TPoint;
StartIndex: Integer;
NumPts: Integer = -1); overload;
procedure Polyline(Points: PPoint; NumPts: Integer); overload;
procedure PolylineF(const APoints: array of TPointF);
procedure PolyBezier(Points: PPoint; NumPts: Integer;
Filled: boolean = False;
Continuous: boolean = False); overload;
procedure PolyBezier(const Points: array of TPoint;
Filled: boolean = False;
Continuous: boolean = False); overload;
procedure Draw(X,Y: Integer; SrcBitmap: TBGRACustomBitmap); overload;
procedure Draw(X,Y: Integer; SrcBitmap: TBitmap); overload;
procedure CopyRect(X,Y: Integer; SrcBitmap: TBGRACustomBitmap; SrcRect: TRect); overload;
procedure StretchDraw(DestRect: TRect; SrcBitmap: TBGRACustomBitmap; HorizFlip: Boolean = false; VertFlip: Boolean = false);
procedure DrawFocusRect(bounds: TRect);
procedure CopyRect(Dest: TRect; SrcBmp: TBGRACustomBitmap;
Source: TRect); overload; virtual;
procedure TextOut(X,Y: Integer; const Text: String);
procedure TextRect(const ARect: TRect; X, Y: integer; const Text: string); overload;
procedure TextRect(ARect: TRect; X, Y: integer; const Text: string;
const Style: TTextStyle); overload;
function TextExtent(const Text: string): TSize;
function TextHeight(const Text: string): integer;
function TextWidth(const Text: string): integer;
property Pen: TBGRAPen read FPen write SetPen;
property PenPos : TPoint read FPenPos write FPenPos;
property Brush: TBGRABrush read FBrush write SetBrush;
property Font: TBGRAFont read FFont write SetFont;
property Pixels[X,Y: integer]: TColor read GetPixelColor write SetPixelColor;
property GammaExpandedPixels[X,Y: integer]: TExpandedPixel read GetExpandedPixel write SetExpandedPixel;
property Colors[X,Y: integer]: TFPColor read GetFPPixelColor write SetFPPixelColor;
property Height: integer read GetHeight;
property Width : integer read GetWidth;
property ClipRect: TRect read GetClipRect write SetClipRect;
property Clipping: Boolean read GetClipping write SetClipping;
end;
{$IFNDEF FPC}
// TCanvas = TBGRACanvas;
{$ENDIF}
implementation
uses BGRAPen, BGRAPath, BGRAPolygon, BGRAPolygonAliased, Math;
{ TBGRAFont }
function TBGRAFont.GetAntialiasing: Boolean;
begin
result := Quality <> fqSystem;
end;
procedure TBGRAFont.SetAntialiasing(const AValue: Boolean);
begin
if AValue = Antialiasing then exit;
if AValue then
Quality := fqFineAntialiasing
else
Quality := fqSystem;
end;
constructor TBGRAFont.Create;
begin
Name := 'default';
Height := 12;
Style := [];
Antialiasing := False;
Orientation := 0;
Texture := nil;
BGRAColor := BGRABlack;
end;
procedure TBGRAFont.Assign(Source: TObject);
var sf: TBGRAFont;
f: TFont;
cf: TFPCustomFont;
begin
if Source is TFont then
begin
f := TFont(Source);
Color := f.Color;
Opacity := 255;
Style := f.Style;
Name := f.Name;
Orientation := f.Orientation;
if f.Height= 0 then
Height := 16 else
Height := f.Height;
end else
if Source is TBGRAFont then
begin
sf := Source as TBGRAFont;
Name := sf.Name;
Height := sf.Height;
Style := sf.Style;
Quality := sf.Quality;
Orientation := sf.Orientation;
Texture := sf.Texture;
end else
if Source is TFPCustomFont then
begin
cf := Source as TFPCustomFont;
Color := FPColorToTColor(cf.FPColor);
Style := [];
if cf.Bold then Style := Style +[fsBold];
if cf.Italic then Style := Style +[fsItalic];
if cf.Underline then Style := Style +[fsUnderline];
{$IFNDEF FPC20602} //changed in 2.6.2 and 2.7
if cf.StrikeThrough then Style := Style +[fsStrikeOut];
{$ELSE}
if cf.StrikeTrough then Style := Style +[fsStrikeOut];
{$ENDIF}
Name := cf.Name;
//Orientation := cf.Orientation;
if cf.Size = 0 then
Height := 16 else
Height := round(cf.Size*1.8);
end;
inherited Assign(Source);
end;
{ TBGRABrush }
function TBGRABrush.GetActualColor: TBGRAPixel;
begin
if (Style = bsClear) or (Opacity = 0) then
result := BGRAPixelTransparent
else
result := BGRAColor;
end;
function TBGRABrush.GetInvisible: boolean;
begin
result := (texture = nil) and ((style = bsClear) or ((style= bsSolid) and (bgracolor.alpha = 0))
or ((bgracolor.alpha = 0) and (BackColor.alpha = 0)));
end;
procedure TBGRABrush.SetBackColor(const AValue: TBGRAPixel);
begin
if BGRAPixelEqual(FBackColor,{=}AValue) then exit;
FBackColor:=AValue;
FreeAndNil(InternalBitmap);
end;
procedure TBGRABrush.SetBrushStyle(const AValue: TBrushStyle);
begin
if FStyle=AValue then exit;
FStyle:=AValue;
FreeAndNil(InternalBitmap);
end;
constructor TBGRABrush.Create;
begin
BGRAColor := BGRAWhite;
InternalBitmap := nil;
InternalBitmapColor := BGRAPixelTransparent;
Style := bsSolid;
Texture := nil;
BackColor := BGRAPixelTransparent;
end;
destructor TBGRABrush.Destroy;
begin
Texture := nil;
InternalBitmap.Free;
inherited Destroy;
end;
procedure TBGRABrush.Assign(Source: TObject);
var sb: TBGRABrush;
b: TBrush;
begin
if Source is TBGRABrush then
begin
sb := Source as TBGRABrush;
Texture := sb.Texture;
BackColor := sb.BackColor;
Style := sb.Style;
end else
if Source is TBrush then
begin
b := Source as TBrush;
Color := b.Color;
Opacity := 255;
Style := b.Style;
end;
inherited Assign(Source);
end;
function TBGRABrush.BuildTexture(Prototype: TBGRACustomBitmap): IBGRAScanner;
begin
//user-defined texture
if Texture <> nil then
result := texture
else
begin
//free pattern if color has changed
if (InternalBitmap <> nil) and not BGRAPixelEqual(InternalBitmapColor, {<>} BGRAColor) then
FreeAndNil(InternalBitmap);
//styles that do not have pattern
if Style in[bsSolid,bsClear] then
result := nil
else
begin
//create pattern if needed
if InternalBitmap = nil then
begin
InternalBitmap := CreateBrushTexture(Prototype, Style, BGRAColor,BackColor);
InternalBitmapColor := BGRAColor;
end;
result := InternalBitmap;
end;
end;
end;
{ TBGRAPen }
function TBGRAPen.GetActualColor: TBGRAPixel;
begin
if (Style = psClear) or (Opacity = 0) then
result := BGRAPixelTransparent
else
result := BGRAColor;
end;
function TBGRAPen.GetActualWidth: integer;
begin
if width < 1 then result := 1 else
result := Width;
end;
function TBGRAPen.GetCustomPenStyle: TBGRAPenStyle;
begin
result := DuplicatePenStyle(FCustomPenStyle);
end;
function TBGRAPen.GetPenStyle: TPenStyle;
begin
Result:= FPenStyle;
end;
procedure TBGRAPen.SetCustomPenStyle(const AValue: TBGRAPenStyle);
begin
FCustomPenStyle := DuplicatePenStyle(AValue);
if IsSolidPenStyle(AValue) then FPenStyle := psSolid else
if IsClearPenStyle(AValue) then FPenStyle := psClear
else
FPenStyle := psPattern;
end;
procedure TBGRAPen.SetPenStyle(const AValue: TPenStyle);
begin
Case AValue of
psSolid: FCustomPenStyle := SolidPenStyle;
psDash: FCustomPenStyle := DashPenStyle;
psDot: FCustomPenStyle := DotPenStyle;
psDashDot: FCustomPenStyle := DashDotPenStyle;
psDashDotDot: FCustomPenStyle := DashDotDotPenStyle;
else FCustomPenStyle := ClearPenStyle;
end;
FPenStyle := AValue;
end;
constructor TBGRAPen.Create;
begin
Width := 1;
EndCap := pecRound;
JoinStyle := pjsRound;
Style := psSolid;
BGRAColor := BGRABlack;
end;
procedure TBGRAPen.Assign(Source: TObject);
var sp: TBGRAPen;
p: TPen;
begin
if Source is TBGRAPen then
begin
sp := Source as TBGRAPen;
Width := sp.Width;
EndCap := sp.EndCap;
JoinStyle := sp.JoinStyle;
Style := sp.Style;
CustomStyle := sp.CustomStyle;
end else
if Source is TPen then
begin
p := Source as TPen;
Width := p.Width;
EndCap := p.EndCap;
JoinStyle := p.JoinStyle;
Style := p.Style;
Color := p.Color;
Opacity := 255;
end;
inherited Assign(Source);
end;
{ TBGRAColoredObject }
function TBGRAColoredObject.GetColor: TColor;
begin
result := BGRAToColor(BGRAColor);
end;
function TBGRAColoredObject.GetOpacity: Byte;
begin
result := BGRAColor.alpha;
end;
procedure TBGRAColoredObject.SetColor(const AValue: TColor);
begin
BGRAColor := ColorToBGRA(ColorToRGB(AValue),BGRAColor.alpha);
end;
procedure TBGRAColoredObject.SetOpacity(const AValue: Byte);
begin
BGRAColor.alpha := AValue;
end;
procedure TBGRAColoredObject.Assign(Source: TObject);
var so: TBGRAColoredObject;
begin
if Source is TBGRAColoredObject then
begin
so := Source as TBGRAColoredObject;
BGRAColor := so.BGRAColor;
end;
end;
{ TBGRACanvas }
procedure TBGRACanvas.SetBrush(const AValue: TBGRABrush);
begin
if FBrush=AValue then exit;
FBrush.Assign(AValue);
end;
procedure TBGRACanvas.SetPen(const AValue: TBGRAPen);
begin
if FPen=AValue then exit;
FPen.Assign(AValue);
end;
function TBGRACanvas.GetPixelColor(X, Y: Integer): TColor;
begin
result := BGRAToColor(FBitmap.GetPixel(x,y));
end;
procedure TBGRACanvas.SetPixelColor(X, Y: Integer; const AValue: TColor);
begin
FBitmap.SetPixel(x,y,ColorToBGRA(AValue));
end;
function TBGRACanvas.GetClipping: Boolean;
begin
result := FClippingOn;
end;
function TBGRACanvas.GetClipRect: TRect;
begin
if not Clipping then
result := FInactiveClipRect else
result := FBitmap.ClipRect;
end;
function TBGRACanvas.GetExpandedPixel(X, Y: Integer): TExpandedPixel;
begin
result := GammaExpansion(FBitmap.GetPixel(X,Y));
end;
function TBGRACanvas.GetFPPixelColor(X, Y: Integer): TFPColor;
begin
result := BGRAToFPColor(FBitmap.GetPixel(x,y));
end;
function TBGRACanvas.GetHeight: integer;
begin
result := FBitmap.Height;
end;
function TBGRACanvas.GetWidth: integer;
begin
result := FBitmap.Width;
end;
procedure TBGRACanvas.SetClipping(const AValue: Boolean);
begin
FClippingOn := AValue;
if not AValue then FBitmap.NoClip else
FBitmap.ClipRect := FInactiveClipRect;
end;
procedure TBGRACanvas.SetClipRect(const AValue: TRect);
begin
FInactiveClipRect := AValue;
if FClippingOn then
begin
FBitmap.ClipRect := AValue;
FInactiveClipRect := FBitmap.ClipRect;
end;
end;
procedure TBGRACanvas.SetExpandedPixel(X, Y: Integer;
const AValue: TExpandedPixel);
begin
FBitmap.SetPixel(x,y,GammaCompression(AValue));
end;
procedure TBGRACanvas.SetFont(const AValue: TBGRAFont);
begin
if FFont=AValue then exit;
FFont.Assign(AValue);
end;
procedure TBGRACanvas.SetFPPixelColor(X, Y: Integer; const AValue: TFPColor);
begin
FBitmap.SetPixel(x,y,FPColorToBGRA(AValue));
end;
function TBGRACanvas.ComputeEllipseC(x1, y1, x2, y2: integer; out cx, cy, rx,
ry: single): boolean;
begin
cx := (x1+x2-1)/2;
cy := (y1+y2-1)/2;
rx := abs((x2-x1)/2);
ry := abs((y2-y1)/2);
result := (rx<>0) and (ry<>0);
end;
function TBGRACanvas.CheckRectangle(var x1, y1, x2, y2: integer; out tx, ty: integer
): boolean;
var
temp: integer;
begin
if x1 > x2 then
begin
temp := x1;
x1 := x2;
x2 := temp;
end;
if y1 > y2 then
begin
temp := y1;
y1 := y2;
y2 := temp;
end;
tx := x2-x1;
ty := y2-y1;
result := (tx<>0) and (ty <>0);
end;
procedure TBGRACanvas.ApplyPenStyle;
var
TempPenStyle: TBGRAPenStyle;
i: Integer;
begin
FBitmap.JoinStyle := FPen.JoinStyle;
FBitmap.LineCap := FPen.EndCap;
if FPen.Width = 1 then
begin
SetLength(TempPenStyle, length(FPen.CustomStyle));
for i := 0 to high(TempPenStyle) do
TempPenStyle[i] := FPen.CustomStyle[i]*4;
FBitmap.CustomPenStyle := TempPenStyle;
end else
FBitmap.CustomPenStyle := FPen.CustomStyle;
end;
procedure TBGRACanvas.ApplyFont;
begin
FBitmap.FontName := Font.Name;
FBitmap.FontHeight := -Font.Height;
FBitmap.FontStyle := Font.Style;
FBitmap.FontQuality := Font.Quality;
FBitmap.FontOrientation := Font.Orientation;
end;
function TBGRACanvas.NoPen: boolean;
begin
result := Pen.ActualColor.alpha = 0;
end;
function TBGRACanvas.NoBrush: boolean;
begin
result := Brush.Invisible;
end;
constructor TBGRACanvas.Create(ABitmap: TBGRACustomBitmap);
begin
FBitmap := ABitmap;
AntialiasingMode := amOn;
FPen := TBGRAPen.Create;
FPenPos := Point(0,0);
FFont := TBGRAFont.Create;
FBrush := TBGRABrush.Create;
FClippingOn := False;
FInactiveClipRect := FBitmap.ClipRect;
FillMode := fmWinding;
DrawFontBackground := True;
end;
destructor TBGRACanvas.Destroy;
begin
FPen.Free;
FBrush.Free;
FFont.Free;
end;
procedure TBGRACanvas.MoveTo(x, y: integer);
begin
MoveTo(Point(x,y));
end;
procedure TBGRACanvas.MoveTo(p: TPoint);
begin
FPenPos := p;
end;
procedure TBGRACanvas.LineTo(x, y: integer);
var pts: {$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
begin
if not NoPen then
begin
//1 pixel-wide solid pen is rendered with pixel line
if (Pen.Style = psSolid) and (Pen.ActualWidth = 1) then
begin
if AntialiasingMode = amOff then
FBitmap.DrawLine(FPenPos.x,FPenPos.y,x,y,Pen.ActualColor,False)
else
FBitmap.DrawLineAntialias(FPenPos.x,FPenPos.y,x,y,Pen.ActualColor,False);
end else
begin
ApplyPenStyle;
if AntialiasingMode = amOff then
begin
pts := FBitmap.ComputeWidePolyline([PointF(FPenPos.x,FPenPos.y),PointF(x,y)],Pen.ActualWidth);
FBitmap.FillPoly(pts,Pen.ActualColor,dmDrawWithTransparency);
end else
FBitmap.DrawLineAntialias(FPenPos.x,FPenPos.y,x,y,Pen.ActualColor,Pen.ActualWidth);
end;
end;
MoveTo(x,y);
end;
procedure TBGRACanvas.LineTo(p: TPoint);
begin
LineTo(p.x,p.y);
end;
procedure TBGRACanvas.Arc(x1, y1, x2, y2, sx, sy, ex, ey: integer);
var
angle1,angle2: BGRAWord;
cx,cy,rx,ry: single;
begin
if not ComputeEllipseC(x1,y1,x2,y2,cx,cy,rx,ry) then exit;
angle1 := round(arctan2(-(sy-cy)/ry,(sx-cx)/rx)*65536/(2*Pi));
angle2 := round(arctan2(-(ey-cy)/ry,(ex-cx)/rx)*65536/(2*Pi));
Arc65536(x1,y1,x2,y2,angle1, angle2, []);
end;
procedure TBGRACanvas.Arc(x1, y1, x2, y2, StartDeg16, LengthDeg16: integer);
begin
if LengthDeg16 > 360*16 then LengthDeg16 := 360*16;
Arc65536(x1,y1,x2,y2,StartDeg16*512 div 45, (StartDeg16+LengthDeg16)*512 div 45, []);
end;
procedure TBGRACanvas.Arc65536(x1, y1, x2, y2: integer; start65536, end65536: BGRAWord; Options: TArcOptions);
var cx,cy,rx,ry,w: single;
arcPts,penPts: {$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
multi: TBGRAMultishapeFiller;
tex: IBGRAScanner;
begin
if NoPen and NoBrush then exit;
if not ComputeEllipseC(x1,y1,x2,y2,cx,cy,rx,ry) then exit;
rx :=rx -0.50;
ry :=ry -0.50;
w := Pen.ActualWidth;
if AntialiasingMode = amOff then
begin
if not NoPen and not Odd(Pen.ActualWidth) then
begin
rx := rx -0.01;
ry := ry -0.01;
end;
end;
if NoPen then
begin
cx :=cx -0.5;
cy :=cy -0.5;
rx :=rx -0.2;
ry :=ry -0.2;
if (rx<0) or (ry<0) then exit;
end;
multi := TBGRAMultishapeFiller.Create;
multi.Antialiasing := AntialiasingMode <> amOff;
multi.FillMode := FillMode;
multi.PolygonOrder := poLastOnTop;
multi.AliasingIncludeBottomRight := True;
arcPts := ComputeArc65536(cx,cy,rx,ry,start65536,end65536);
if (aoPie in Options) and (start65536 <> end65536) then
begin
setlength(arcPts,length(arcPts)+1);
arcPts[high(arcPts)] := PointF(cx,cy);
end;
if (aoFillPath in Options) and not NoBrush then
begin
tex := Brush.BuildTexture(FBitmap);
if tex <> nil then
multi.AddPolygon(arcPts,tex) else
multi.AddPolygon(arcPts,Brush.ActualColor);
end;
if not NoPen then
begin
ApplyPenStyle;
if (aoClosePath in Options) or (aoPie in Options) then
penPts := FBitmap.ComputeWidePolygon(arcPts,w)
else
penPts := FBitmap.ComputeWidePolyline(arcPts,w);
multi.AddPolygon( penPts, Pen.ActualColor );
end;
multi.Draw(FBitmap);
multi.Free;
end;
procedure TBGRACanvas.Chord(x1, y1, x2, y2, sx, sy, ex, ey: integer);
var
angle1,angle2: BGRAWord;
cx,cy,rx,ry: single;
begin
if not ComputeEllipseC(x1,y1,x2,y2,cx,cy,rx,ry) then exit;
angle1 := round(arctan2(-(sy-cy)/ry,(sx-cx)/rx)*65536/(2*Pi)) and 65535;
angle2 := round(arctan2(-(ey-cy)/ry,(ex-cx)/rx)*65536/(2*Pi)) and 65535;
Arc65536(x1,y1,x2,y2,angle1, angle2, [aoClosePath,aoFillPath]);
end;
procedure TBGRACanvas.Chord(x1, y1, x2, y2, StartDeg16, LengthDeg16: integer);
begin
if LengthDeg16 > 360*16 then LengthDeg16 := 360*16;
Arc65536(x1,y1,x2,y2,StartDeg16*512 div 45, (StartDeg16+LengthDeg16)*512 div 45,[aoClosePath,aoFillPath]);
end;
procedure TBGRACanvas.Pie(x1, y1, x2, y2, sx, sy, ex, ey: integer);
var
angle1,angle2: BGRAWord;
cx,cy,rx,ry: single;
begin
if not ComputeEllipseC(x1,y1,x2,y2,cx,cy,rx,ry) then exit;
angle1 := round(arctan2(-(sy-cy)/ry,(sx-cx)/rx)*65536/(2*Pi)) and 65535;
angle2 := round(arctan2(-(ey-cy)/ry,(ex-cx)/rx)*65536/(2*Pi)) and 65535;
Arc65536(x1,y1,x2,y2,angle1, angle2, [aoPie,aoFillPath]);
end;
procedure TBGRACanvas.Pie(x1, y1, x2, y2, StartDeg16, LengthDeg16: integer);
begin
if LengthDeg16 > 360*16 then LengthDeg16 := 360*16;
Arc65536(x1,y1,x2,y2,StartDeg16*512 div 45, (StartDeg16+LengthDeg16)*512 div 45,[aoPie,aoFillPath]);
end;
procedure TBGRACanvas.RadialPie(x1, y1, x2, y2, StartDeg16, LengthDeg16: integer
);
begin
Pie(x1,y1,x2,y2,StartDeg16,LengthDeg16);
end;
procedure TBGRACanvas.Ellipse(x1, y1, x2, y2: integer);
var cx,cy,rx,ry,w: single;
tex: IBGRAScanner;
multi: TBGRAMultishapeFiller;
begin
if NoPen and NoBrush then exit;
tex := Brush.BuildTexture(FBitmap);
if (AntialiasingMode = amOff) and not NoPen and (Pen.Style = psSolid) and (Pen.ActualWidth = 1) then
begin
BGRARoundRectAliased(FBitmap,x1,y1,x2,y2,abs(x2-x1),abs(y2-y1),Pen.ActualColor,Brush.ActualColor,tex);
exit;
end;
if not ComputeEllipseC(x1,y1,x2,y2,cx,cy,rx,ry) then exit;
tex := Brush.BuildTexture(FBitmap);
w := Pen.ActualWidth;
rx :=rx -0.50;
ry :=ry -0.50;
if AntialiasingMode = amOff then
begin
if not NoPen and not Odd(Pen.ActualWidth) then
begin
rx := rx -0.01;
ry := ry -0.01;
end;
end;
if NoPen then
begin
cx :=cx -0.5;
cy :=cy -0.5;
rx :=rx -0.2;
ry :=ry -0.2;
if (rx<0) or (ry<0) then exit;
end;
multi := TBGRAMultishapeFiller.Create;
multi.Antialiasing := AntialiasingMode <> amOff;
multi.PolygonOrder := poLastOnTop;
multi.AliasingIncludeBottomRight := True;
if not NoBrush then
begin
if tex <> nil then
multi.AddEllipse(cx,cy,rx,ry,tex)
else
multi.AddEllipse(cx,cy,rx,ry,Brush.ActualColor);
end;
if not NoPen then
begin
ApplyPenStyle;
if (Pen.Style = psSolid) and multi.Antialiasing then
multi.AddEllipseBorder(cx,cy,rx,ry,w,Pen.ActualColor)
else
multi.AddPolygon(FBitmap.ComputeWidePolygon(ComputeEllipse(cx,cy,rx,ry),w),Pen.ActualColor);
end;
multi.Draw(FBitmap);
multi.Free;
end;
procedure TBGRACanvas.Ellipse(const bounds: TRect);
begin
Ellipse(bounds.left,bounds.top,bounds.right,bounds.Bottom);
end;
procedure TBGRACanvas.Rectangle(x1, y1, x2, y2: integer; Filled: Boolean = True);
var tx,ty: integer;
w: single;
tex: IBGRAScanner;
multi: TBGRAMultishapeFiller;
begin
if NoPen and NoBrush then exit;
if not CheckRectangle(x1,y1,x2,y2,tx,ty) then exit;
if NoPen then
FillRect(x1,y1,x2-1,y2-1) //one pixel
else
begin
dec(x2);
dec(y2);
if (Pen.Style = psSolid) and not Filled then
begin
ApplyPenStyle;
FBitmap.RectangleAntialias(x1,y1,x2,y2,Pen.ActualColor,Pen.ActualWidth);
exit;
end;
tex := Brush.BuildTexture(FBitmap);
if (Pen.Style = psSolid) and (tex=nil) then
begin
ApplyPenStyle;
FBitmap.RectangleAntialias(x1,y1,x2,y2,Pen.ActualColor,Pen.ActualWidth,Brush.ActualColor);
exit;
end;
w := Pen.ActualWidth;
multi := TBGRAMultishapeFiller.Create;
multi.Antialiasing := AntialiasingMode <> amOff;
multi.PolygonOrder := poLastOnTop;
if not NoBrush and Filled then
begin
if tex <> nil then
multi.AddRectangle(x1,y1,x2,y2,tex)