forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgrapen.pas
1748 lines (1549 loc) · 49.9 KB
/
bgrapen.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 BGRAPen;
{$i bgrabitmap.inc}{$H+}
interface
{ This unit handles pen style and width, as well as line caps and join styles.
A line consists in two points.
A polyline consists in one or more lines, defined by two points or more than two points
A poly-polyline consists in a series of polylines, defined by polyline points separated by empty points (see EmptyPointF) }
uses
SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType,{$ENDIF} BGRAGraphics, BGRABitmapTypes, BGRATransform;
var //predefined pen styles
SolidPenStyle, DashPenStyle, DotPenStyle, DashDotPenStyle, DashDotDotPenStyle, ClearPenStyle: TBGRAPenStyle;
type
{ TBGRAPenStroker }
TBGRAPenStroker = class(TBGRACustomPenStroker)
protected
{ Pen style can be defined by PenStyle property of by CustomPenStyle property.
When PenStyle property is assigned, CustomPenStyle property is assigned the actual
pen pattern. }
FCustomPenStyle: TBGRAPenStyle;
FPenStyle: TPenStyle;
FArrow: TBGRACustomArrow;
FArrowOwned: boolean;
FOriginalStrokeMatrix,FStrokeMatrix,FStrokeMatrixInverse: TAffineMatrix;
FStrokeZoom: single;
FStrokeMatrixIdentity: boolean;
FLineCap: TPenEndCap;
FJoinStyle: TPenJoinStyle;
FMiterLimit: single;
function GetArrow: TBGRACustomArrow; override;
function GetArrowOwned: boolean; override;
function GetCustomPenStyle: TBGRAPenStyle; override;
function GetJoinStyle: TPenJoinStyle; override;
function GetLineCap: TPenEndCap; override;
function GetMiterLimit: single; override;
function GetPenStyle: TPenStyle; override;
function GetStrokeMatrix: TAffineMatrix; override;
procedure SetArrow(AValue: TBGRACustomArrow); override;
procedure SetArrowOwned(AValue: boolean); override;
procedure SetCustomPenStyle(AValue: TBGRAPenStyle); override;
procedure SetJoinStyle(AValue: TPenJoinStyle); override;
procedure SetLineCap(AValue: TPenEndCap); override;
procedure SetMiterLimit(AValue: single); override;
procedure SetPenStyle(AValue: TPenStyle); override;
procedure SetStrokeMatrix(const AValue: TAffineMatrix); override;
public
constructor Create;
destructor Destroy; override;
function ComputePolyline(const APoints: array of TPointF; AWidth: single; AClosedCap: boolean = true): ArrayOfTPointF; overload; override;
function ComputePolyline(const APoints: array of TPointF; AWidth: single; APenColor: TBGRAPixel; AClosedCap: boolean = true): ArrayOfTPointF; overload; override;
function ComputePolylineAutocycle(const APoints: array of TPointF; AWidth: single): ArrayOfTPointF; override;
function ComputePolygon(const APoints: array of TPointF; AWidth: single): ArrayOfTPointF; override;
end;
TBGRAPolyLineOption = (plRoundCapOpen, //specifies that the line ending is opened
plCycle, //specifies that it is a polygon
plAutoCycle, //specifies that a cycle must be used if the last point is the first point
plNoStartCap,
plNoEndCap);
TBGRAPolyLineOptions = set of TBGRAPolyLineOption;
TComputeArrowHeadProc = function(const APosition: TPointF; const ADirection: TPointF; const AWidth: single; const ACurrentPos: single): ArrayOfTPointF of object;
{ Compute the path for a polyline }
function ComputeWidePolylinePoints(const linepts: array of TPointF; width: single;
pencolor: TBGRAPixel; linecap: TPenEndCap; joinstyle: TPenJoinStyle; const penstyle: TBGRAPenStyle;
options: TBGRAPolyLineOptions; miterLimit: single = 2; arrow: TBGRACustomArrow = nil): ArrayOfTPointF;
{ Compute the path for a poly-polyline }
function ComputeWidePolyPolylinePoints(const linepts: array of TPointF; width: single;
pencolor: TBGRAPixel; linecap: TPenEndCap; joinstyle: TPenJoinStyle; const penstyle: TBGRAPenStyle;
options: TBGRAPolyLineOptions; miterLimit: single = 2; arrow: TBGRACustomArrow = nil): ArrayOfTPointF;
{--------------------- Pixel line procedures --------------------------}
{ These procedures take integer coordinates as parameters and do not handle pen styles and width.
They are faster and can be useful for drawing a simple frame }
//aliased version
procedure BGRADrawLineAliased(dest: TBGRACustomBitmap; x1, y1, x2, y2: integer; c: TBGRAPixel; DrawLastPixel: boolean; ADrawMode: TDrawMode = dmDrawWithTransparency);
procedure BGRAEraseLineAliased(dest: TBGRACustomBitmap; x1, y1, x2, y2: integer; alpha: byte; DrawLastPixel: boolean);
//antialiased version
procedure BGRADrawLineAntialias({%H-}dest: TBGRACustomBitmap; x1, y1, x2, y2: integer;
c: TBGRAPixel; DrawLastPixel: boolean; LinearBlend : boolean = false); overload;
procedure BGRAEraseLineAntialias(dest: TBGRACustomBitmap; x1, y1, x2, y2: integer;
calpha: byte; DrawLastPixel: boolean); overload;
//antialiased version with bicolor dashes (to draw a frame)
procedure BGRADrawLineAntialias(dest: TBGRACustomBitmap; x1, y1, x2, y2: integer;
c1, c2: TBGRAPixel; dashLen: integer; DrawLastPixel: boolean; var DashPos: integer; LinearBlend : boolean = false); overload;
//length added to ensure accepable alpha join (using TBGRAMultishapeFiller is still better)
function GetAlphaJoinFactor(alpha: byte): single;
//create standard brush texture
function CreateBrushTexture(prototype: TBGRACustomBitmap; brushstyle: TBrushStyle; PatternColor, BackgroundColor: TBGRAPixel;
width: integer = 8; height: integer = 8; penwidth: single = 1): TBGRACustomBitmap;
//check special pen styles
function IsSolidPenStyle(ACustomPenStyle: TBGRAPenStyle): boolean;
function IsClearPenStyle(ACustomPenStyle: TBGRAPenStyle): boolean;
function DuplicatePenStyle(ACustomPenStyle: array of single): TBGRAPenStyle;
implementation
uses math, BGRAPath;
procedure BGRADrawLineAliased(dest: TBGRACustomBitmap; x1, y1, x2, y2: integer;
c: TBGRAPixel; DrawLastPixel: boolean; ADrawMode: TDrawMode);
var
Y, X: integer;
DX, DY, SX, SY, E: integer;
{$IFDEF BDS}_BGRADWord : BGRADWord;{$ENDIF}//#
PixelProc: procedure (x, y: int32or64; c: TBGRAPixel) of object;
begin
if (Y1 = Y2) then
begin
if (X1 = X2) then
begin
if DrawLastPixel then
dest.DrawPixel(X1, Y1, c, ADrawMode);
end else
begin
if not DrawLastPixel then
begin
if X2 > X1 then dec(X2) else inc(X2);
end;
dest.HorizLine(X1,Y1,X2,c, ADrawMode);
end;
Exit;
end else
if (X1 = X2) then
begin
if not DrawLastPixel then
begin
if Y2 > Y1 then dec(Y2) else inc(Y2);
end;
dest.VertLine(X1,Y1,Y2,c, ADrawMode);
Exit;
end;
DX := X2 - X1;
DY := Y2 - Y1;
if (ADrawMode = dmSetExceptTransparent) and (c.alpha <> 255) then exit else
if c.alpha = 0 then
begin//#
if ADrawMode in[dmDrawWithTransparency,dmLinearBlend] then exit;
{$IFDEF BDS}move(c , _BGRADWord, sizeof(BGRADWord));{$ENDIF}
if (ADrawMode = dmXor) and ({$IFDEF BDS}_BGRADWord{$ELSE}BGRADWord(c){$ENDIF}=0) then exit;
end;
case ADrawMode of
dmDrawWithTransparency: PixelProc := {$IFDEF OBJ}@{$ENDIF}dest.DrawPixel;
dmXor: PixelProc := {$IFDEF OBJ}@{$ENDIF}dest.XorPixel;
dmLinearBlend: PixelProc := {$IFDEF OBJ}@{$ENDIF}dest.FastBlendPixel;
else
PixelProc := {$IFDEF OBJ}@{$ENDIF}dest.SetPixel;
end;
if DX < 0 then
begin
SX := -1;
DX := -DX;
end
else
SX := 1;
if DY < 0 then
begin
SY := -1;
DY := -DY;
end
else
SY := 1;
DX := DX shl 1;
DY := DY shl 1;
X := X1;
Y := Y1;
if DX > DY then
begin
E := DY - DX shr 1;
while X <> X2 do
begin
PixelProc(X, Y, c);
if E >= 0 then
begin
Inc(Y, SY);
Dec(E, DX);
end;
Inc(X, SX);
Inc(E, DY);
end;
end
else
begin
E := DX - DY shr 1;
while Y <> Y2 do
begin
PixelProc(X, Y, c);
if E >= 0 then
begin
Inc(X, SX);
Dec(E, DY);
end;
Inc(Y, SY);
Inc(E, DX);
end;
end;
if DrawLastPixel then
PixelProc(X2, Y2, c);
end;
procedure BGRAEraseLineAliased(dest: TBGRACustomBitmap; x1, y1, x2,
y2: integer; alpha: byte; DrawLastPixel: boolean);
var
Y, X: integer;
DX, DY, SX, SY, E: integer;
begin
if (Y1 = Y2) and (X1 = X2) then
begin
if DrawLastPixel then
dest.ErasePixel(X1, Y1, alpha);
Exit;
end;
DX := X2 - X1;
DY := Y2 - Y1;
if DX < 0 then
begin
SX := -1;
DX := -DX;
end
else
SX := 1;
if DY < 0 then
begin
SY := -1;
DY := -DY;
end
else
SY := 1;
DX := DX shl 1;
DY := DY shl 1;
X := X1;
Y := Y1;
if DX > DY then
begin
E := DY - DX shr 1;
while X <> X2 do
begin
dest.ErasePixel(X, Y, alpha);
if E >= 0 then
begin
Inc(Y, SY);
Dec(E, DX);
end;
Inc(X, SX);
Inc(E, DY);
end;
end
else
begin
E := DX - DY shr 1;
while Y <> Y2 do
begin
dest.ErasePixel(X, Y, alpha);
if E >= 0 then
begin
Inc(X, SX);
Dec(E, DY);
end;
Inc(Y, SY);
Inc(E, DX);
end;
end;
if DrawLastPixel then
dest.ErasePixel(X2, Y2, alpha);
end;
procedure BGRADrawLineAntialias(dest: TBGRACustomBitmap; x1, y1, x2, y2: integer;
c: TBGRAPixel; DrawLastPixel: boolean; LinearBlend : boolean);
var
Y, X: integer;
DX, DY, SX, SY, E: integer;
alpha: BGRANativeUInt;
pixelproc: procedure(x,y: int32or64; c: TBGRAPixel) of object;
begin
if LinearBlend then
pixelproc := {$IFDEF OBJ}@{$ENDIF}dest.FastBlendPixel
else
pixelproc := {$IFDEF OBJ}@{$ENDIF}dest.DrawPixel;
if (Y1 = Y2) and (X1 = X2) then
begin
if DrawLastPixel then
pixelproc(X1, Y1, c);
Exit;
end;
DX := X2 - X1;
DY := Y2 - Y1;
if DX < 0 then
begin
SX := -1;
DX := -DX;
end
else
SX := 1;
if DY < 0 then
begin
SY := -1;
DY := -DY;
end
else
SY := 1;
DX := DX shl 1;
DY := DY shl 1;
X := X1;
Y := Y1;
if DX > DY then
begin
E := 0;
while X <> X2 do
begin
alpha := c.alpha * E div DX;
pixelproc(X, Y, BGRA(c.red, c.green, c.blue, c.alpha - alpha));
pixelproc(X, Y + SY, BGRA(c.red, c.green, c.blue, alpha));
Inc(E, DY);
if E >= DX then
begin
Inc(Y, SY);
Dec(E, DX);
end;
Inc(X, SX);
end;
end
else
begin
E := 0;
while Y <> Y2 do
begin
alpha := c.alpha * E div DY;
pixelproc(X, Y, BGRA(c.red, c.green, c.blue, c.alpha - alpha));
pixelproc(X + SX, Y, BGRA(c.red, c.green, c.blue, alpha));
Inc(E, DX);
if E >= DY then
begin
Inc(X, SX);
Dec(E, DY);
end;
Inc(Y, SY);
end;
end;
if DrawLastPixel then
pixelproc(X2, Y2, c);
end;
procedure BGRAEraseLineAntialias(dest: TBGRACustomBitmap; x1, y1, x2,
y2: integer; calpha: byte; DrawLastPixel: boolean);
var
Y, X: integer;
DX, DY, SX, SY, E: integer;
alpha: BGRANativeUInt;
begin
if (Y1 = Y2) and (X1 = X2) then
begin
if DrawLastPixel then
dest.ErasePixel(X1, Y1, calpha);
Exit;
end;
DX := X2 - X1;
DY := Y2 - Y1;
if DX < 0 then
begin
SX := -1;
DX := -DX;
end
else
SX := 1;
if DY < 0 then
begin
SY := -1;
DY := -DY;
end
else
SY := 1;
DX := DX shl 1;
DY := DY shl 1;
X := X1;
Y := Y1;
if DX > DY then
begin
E := 0;
while X <> X2 do
begin
alpha := calpha * E div DX;
dest.ErasePixel(X, Y, calpha - alpha);
dest.ErasePixel(X, Y + SY, alpha);
Inc(E, DY);
if E >= DX then
begin
Inc(Y, SY);
Dec(E, DX);
end;
Inc(X, SX);
end;
end
else
begin
E := 0;
while Y <> Y2 do
begin
alpha := calpha * E div DY;
dest.ErasePixel(X, Y, calpha - alpha);
dest.ErasePixel(X + SX, Y, alpha);
Inc(E, DX);
if E >= DY then
begin
Inc(X, SX);
Dec(E, DY);
end;
Inc(Y, SY);
end;
end;
if DrawLastPixel then
dest.ErasePixel(X2, Y2, calpha);
end;
procedure BGRADrawLineAntialias(dest: TBGRACustomBitmap; x1, y1, x2, y2: integer;
c1, c2: TBGRAPixel; dashLen: integer; DrawLastPixel: boolean; var DashPos: integer; LinearBlend : boolean);
var
Y, X: integer;
DX, DY, SX, SY, E: integer;
alpha: BGRANativeUInt;
c: TBGRAPixel;
begin
if (c1.alpha=0) and (c2.alpha=0) then exit;
if DashLen <= 0 then
begin
BGRADrawLineAntialias(dest,x1,y1,x2,y2,MergeBGRA(c1,c2),DrawLastPixel,LinearBlend);
exit;
end;
DashPos := PositiveMod(DashPos,DashLen+DashLen);
if DashPos < DashLen then c := c1 else c := c2;
if (Y1 = Y2) and (X1 = X2) then
begin
if DrawLastPixel then
dest.DrawPixel(X1, Y1, c);
Exit;
end;
DX := X2 - X1;
DY := Y2 - Y1;
if DX < 0 then
begin
SX := -1;
DX := -DX;
end
else
SX := 1;
if DY < 0 then
begin
SY := -1;
DY := -DY;
end
else
SY := 1;
DX := DX shl 1;
DY := DY shl 1;
X := X1;
Y := Y1;
if DX > DY then
begin
E := 0;
while X <> X2 do
begin
alpha := c.alpha * E div DX;
dest.DrawPixel(X, Y, BGRA(c.red, c.green, c.blue, c.alpha - alpha));
dest.DrawPixel(X, Y + SY, BGRA(c.red, c.green, c.blue, alpha));
Inc(E, DY);
if E >= DX then
begin
Inc(Y, SY);
Dec(E, DX);
end;
Inc(X, SX);
Inc(DashPos);
if DashPos = DashLen then
c := c2
else
if DashPos = DashLen + DashLen then
begin
c := c1;
DashPos := 0;
end;
end;
end
else
begin
E := 0;
while Y <> Y2 do
begin
alpha := c.alpha * E div DY;
dest.DrawPixel(X, Y, BGRA(c.red, c.green, c.blue, c.alpha - alpha));
dest.DrawPixel(X + SX, Y, BGRA(c.red, c.green, c.blue, alpha));
Inc(E, DX);
if E >= DY then
begin
Inc(X, SX);
Dec(E, DY);
end;
Inc(Y, SY);
Inc(DashPos);
if DashPos = DashLen then
c := c2
else
if DashPos = DashLen + DashLen then
begin
c := c1;
DashPos := 0;
end;
end;
end;
if DrawLastPixel then
begin
dest.DrawPixel(X2, Y2, c);
inc(DashPos);
if DashPos = DashLen + DashLen then DashPos := 0;
end;
end;
function GetAlphaJoinFactor(alpha: byte): single;
var t: single;
begin
if alpha = 255 then result := 1 else
begin
result := (power(20,alpha/255)-1)/19*0.5;
t := power(alpha/255,40);
result := result*(1-t)+t*0.82;
end;
end;
function CreateBrushTexture(prototype: TBGRACustomBitmap; brushstyle: TBrushStyle;
PatternColor, BackgroundColor: TBGRAPixel; width: integer = 8; height: integer = 8; penwidth: single = 1): TBGRACustomBitmap;
begin
result := prototype.NewBitmap(width,height);
if brushstyle <> bsClear then
begin
result.Fill(BackgroundColor);
if brushstyle in[bsDiagCross,bsBDiagonal] then
begin
result.DrawLineAntialias(-1,height,width,-1,PatternColor,penwidth);
result.DrawLineAntialias(-1-penwidth,0+penwidth,0+penwidth,-1-penwidth,PatternColor,penwidth);
result.DrawLineAntialias(width-1-penwidth,height+penwidth,width+penwidth,height-1-penwidth,PatternColor,penwidth);
end;
if brushstyle in[bsDiagCross,bsFDiagonal] then
begin
result.DrawLineAntialias(-1,-1,width,height,PatternColor,penwidth);
result.DrawLineAntialias(width-1-penwidth,-1-penwidth,width+penwidth,0+penwidth,PatternColor,penwidth);
result.DrawLineAntialias(-1-penwidth,height-1-penwidth,0+penwidth,height+penwidth,PatternColor,penwidth);
end;
if brushstyle in[bsHorizontal,bsCross] then
result.DrawLineAntialias(-1,height div 2,width,height div 2,PatternColor,penwidth);
if brushstyle in[bsVertical,bsCross] then
result.DrawLineAntialias(width div 2,-1,width div 2,height,PatternColor,penwidth);
end;
end;
function IsSolidPenStyle(ACustomPenStyle: TBGRAPenStyle): boolean;
begin
result := ACustomPenStyle = nil;
end;
function IsClearPenStyle(ACustomPenStyle: TBGRAPenStyle): boolean;
begin
if (length(ACustomPenStyle)=1) and (ACustomPenStyle[0]=0) then
result := true
else
result := false;
end;
function DuplicatePenStyle(ACustomPenStyle: array of single): TBGRAPenStyle;
var
i: Integer;
begin
setlength(result,length(ACustomPenStyle));
for i := 0 to high(result) do
result[i]:= ACustomPenStyle[i];
end;
procedure ApplyPenStyle(const leftPts, rightPts: array of TPointF; const penstyle: TBGRAPenStyle;
width: single; var posstyle: single; out styledPts: ArrayOfTPointF);
var
styleIndex :integer;
remainingDash: single;
procedure NextStyleIndex;
begin
inc(styleIndex);
if styleIndex = length(penstyle) then
styleIndex := 0;
remainingDash := remainingDash +penstyle[styleindex];
end;
var
dashStartIndex: integer;
dashLeftStartPos,dashRightStartPos : TPointF;
betweenDash: boolean;
procedure StartDash(index: integer; t: single);
begin
dashStartIndex := index;
dashLeftStartPos := leftPts[index] + (leftPts[index+1]-leftPts[index])*t;
dashRightStartPos := rightPts[index] + (rightPts[index+1]-rightPts[index])*t;
betweenDash := false;
end;
var
nbStyled: integer;
procedure AddPt(pt: TPointF);
begin
if (nbStyled = 0) or (pt <> styledPts[nbStyled-1]) then
begin
if nbStyled = length(styledPts) then
setlength(styledPts,nbStyled*2+4);
styledPts[nbStyled] := pt;
inc(nbStyled);
end;
end;
procedure StartPolygon;
begin
if nbStyled > 0 then AddPt(EmptyPointF);
end;
procedure EndDash(index: integer; t: single);
var dashLeftEndPos,dashRightEndPos: TPointF;
i: Integer;
begin
if t=0 then
begin
dashLeftEndPos := leftPts[index];
dashRightEndPos := rightPts[index];
end else
begin
dashLeftEndPos := leftPts[index] + (leftPts[index+1]-leftPts[index])*t;
dashRightEndPos := rightPts[index] + (rightPts[index+1]-rightPts[index])*t;
end;
StartPolygon;
AddPt(dashLeftStartPos);
for i := dashStartIndex+1 to index do
AddPt(leftPts[i]);
AddPt(dashLeftEndPos);
AddPt(dashRightEndPos);
for i := index downto dashStartIndex+1 do
AddPt(rightPts[i]);
AddPt(dashRightStartPos);
betweenDash := true;
end;
var
i,nb: integer;
styleLength: single;
len,lenDone: single;
begin
nbStyled := 0;
styledPts := nil;
if IsClearPenStyle(penstyle) then exit;
if IsSolidPenStyle(penstyle) then
begin
for i := 0 to high(leftPts) do AddPt(leftPts[i]);
for i := high(rightPts) downto 0 do AddPt(rightPts[i]);
setlength(styledPts,nbStyled);
exit;
end;
if length(leftPts) <> length(rightPts) then
raise Exception.Create('Dimension mismatch');
nb := length(leftPts);
if length(penstyle) mod 2 <> 0 then
raise Exception.Create('Pen style must contain an even number of values');
styleLength := 0;
styleIndex := -1;
remainingDash := 0;
betweenDash := false;
for i := 0 to high(penstyle) do
if penstyle[i] <= 0 then
raise Exception.Create('Invalid pen dash length')
else
begin
styleLength :=styleLength + penstyle[i];
if styleLength >= posstyle then
begin
styleIndex := i;
remainingDash := styleLength-posstyle;
break;
end;
end;
if styleIndex = -1 then
begin
styleIndex := 0;
remainingDash := penstyle[0];
end;
if styleIndex mod 2 = 0 then
StartDash(0, 0) else
betweenDash := true;
for i := 0 to nb-2 do
begin
len := (sqrt(sqr(leftPts[i+1].x-leftPts[i].x) + sqr(leftPts[i+1].y-leftPts[i].y))+
sqrt(sqr(rightPts[i+1].x-rightPts[i].x) + sqr(rightPts[i+1].y-rightPts[i].y)))/(2*width);
lenDone := 0;
while lenDone < len do
begin
if len-lenDone < remainingDash then
begin
remainingDash := remainingDash -len-lenDone;
if remainingDash = 0 then NextStyleIndex;
lenDone := len;
end else
if betweenDash then
begin
lenDone := lenDone +remainingDash;
StartDash(i, lenDone/len);
remainingDash := 0;
NextStyleIndex;
end else
begin
lenDone := lenDone +remainingDash;
EndDash(i, lenDone/len);
remainingDash := 0;
NextStyleIndex;
end;
end;
end;
if not betweenDash then
EndDash(nb-1,0);
setlength(styledPts,nbStyled);
end;
function ComputeWidePolylinePoints(const linepts: array of TPointF; width: single;
pencolor: TBGRAPixel; linecap: TPenEndCap; joinstyle: TPenJoinStyle; const penstyle: TBGRAPenStyle;
options: TBGRAPolyLineOptions; miterLimit: single; arrow: TBGRACustomArrow): ArrayOfTPointF;
const oneOver512 = 1/512;
var
startArrowPos, startArrowDir, endArrowPos, endArrowDir: TPointF;
startArrowLinePos, endArrowLinePos: single;
borders : array of record
leftSide,rightSide: TLineDef;
len: single;
leftDir: TPointF;
end;
compPts: {$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
nbCompPts: integer;
revCompPts: {$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
nbRevCompPts: integer;
pts: array of TPointF;
roundPrecision: integer;
hw: single; //half-width
procedure AddPt(normal,rev: TPointF); overload;
begin
if (nbCompPts > 0) and (compPts[nbCompPts-1]=normal) and
(nbRevCompPts > 0) and (revCompPts[nbRevCompPts-1]=rev) then exit;
if nbCompPts = length(compPts) then
setlength(compPts, length(compPts)*2);
compPts[nbCompPts] := normal;
inc(nbCompPts);
if nbRevCompPts = length(revCompPts) then
setlength(revCompPts, length(revCompPts)*2);
revCompPts[nbRevCompPts] := rev;
inc(nbRevCompPts);
end;
procedure AddPt(xnormal,ynormal: single; xrev,yrev: single); overload;
begin
AddPt(PointF(xnormal,ynormal),PointF(xrev,yrev));
end;
procedure AddRoundCap(origin: TPointF; dir: TPointF; fromCenter: boolean; flipped: boolean= false);
var i: integer;
a,s,c: single;
offset,flipvalue: single;
begin
if fromCenter then offset := 0 else offset := -Pi/2;
if flipped then flipvalue := -1 else flipvalue := 1;
for i := 1 to RoundPrecision do
begin
a := i/(RoundPrecision+1)*Pi/2 + offset;
s := sin(a)*hw*flipvalue;
c := cos(a)*hw;
AddPt( PointF(origin.x+ dir.x*c - dir.y*s, origin.y + dir.y*c + dir.x*s),
PointF(origin.x+ dir.x*c + dir.y*s, origin.y + dir.y*c - dir.x*s) );
end;
end;
procedure AddRoundCapAlphaJoin(origin: TPointF; dir: TPointF; fromCenter: boolean; flipped: boolean= false);
var i: integer;
a,s,c: single;
offset,flipvalue: single;
t,alphaFactor: single; //antialiasing join
begin
if fromCenter then offset := 0 else offset := -Pi/2;
if flipped then flipvalue := -1 else flipvalue := 1;
alphaFactor := GetAlphaJoinFactor(pencolor.alpha);
for i := 1 to RoundPrecision do
begin
a := i/(RoundPrecision+1)*Pi/2 + offset;
s := sin(a)*hw*flipvalue;
c := cos(a);
t := (1 - c) * (0.2 + alphaFactor*0.3) + alphaFactor;
c := c *hw;
AddPt( PointF(origin.x+ dir.x*(c-t) - dir.y*s, origin.y + dir.y*(c-t) + dir.x*s),
PointF(origin.x+ dir.x*(c-t) + dir.y*s, origin.y + dir.y*(c-t) - dir.x*s) );
end;
end;
function ComputeRoundJoin(origin, pt1,pt2: TPointF): ArrayOfTPointF;
var a1,a2: single;
da: single;
precision,i: integer;
begin
a1 := arctan2(pt1.y-origin.y,pt1.x-origin.x);
a2 := arctan2(pt2.y-origin.y,pt2.x-origin.x);
if a2-a1 > Pi then a2 := a2 -(2*Pi);
if a1-a2 > Pi then a1 := a1 -(2*Pi);
if a2=a1 then
begin
setlength(result,1);
result[0] := pt1;
exit;
end;
da := a2-a1;
precision := round( sqrt( sqr(pt2.x-pt1.x)+sqr(pt2.y-pt1.y) ) ) +2;
setlength(result,precision);
for i := 0 to precision-1 do
result[i] := origin + PointF( cos(a1+i/(precision-1)*da)*hw,
sin(a1+i/(precision-1)*da)*hw );
end;
var
joinLeft,joinRight: array of TPointF;
nbJoinLeft,nbJoinRight: integer;
procedure SetJoinLeft(joinpts: array of TPointF);
var i: integer;
begin
nbJoinLeft := length(joinpts);
if length(joinLeft) < nbJoinLeft then setlength(joinLeft,length(joinLeft)+nbJoinLeft+2);
for i := 0 to nbJoinLeft-1 do
joinLeft[i] := joinpts[i];
end;
procedure SetJoinRight(joinpts: array of TPointF);
var i: integer;
begin
nbJoinRight := length(joinpts);
if length(joinRight) < nbJoinRight then setlength(joinRight,length(joinRight)+nbJoinRight+2);
for i := 0 to nbJoinRight-1 do
joinRight[i] := joinpts[i];
end;
procedure AddJoin(index: integer);
var len,i: integer;
begin
len := nbJoinLeft;
if nbJoinRight > len then
len := nbJoinRight;
if len = 0 then exit;
if (len > 1) and (index <> -1) then
begin
if nbJoinLeft=1 then
AddPt(joinLeft[0], joinLeft[0] - 2*borders[Index].leftDir) else
if nbJoinRight=1 then
AddPt( joinRight[0] + 2* borders[index].leftDir, joinRight[0]);
end;
for i := 0 to len-1 do
begin
AddPt(joinLeft[i*nbJoinLeft div len],
joinRight[i*nbJoinRight div len]);
end;
if (len > 1) and (index <> -1) then
begin
if nbJoinLeft=1 then
AddPt(joinLeft[0], joinLeft[0] - 2*borders[index+1].leftDir) else
if nbJoinRight=1 then
AddPt(joinRight[0]+2*borders[index+1].leftDir, joinRight[0]);
end;
end;
var
NbPolyAcc: integer;
procedure FlushLine(lastPointIndex: integer);
var
enveloppe: arrayOfTPointF;
posstyle: single;
i,idxInsert: Integer;
begin
if lastPointIndex <> -1 then
AddPt( pts[lastPointIndex] + borders[lastPointIndex-1].leftDir,
pts[lastPointIndex] - borders[lastPointIndex-1].leftDir);
if (lastPointIndex = high(pts)) and (linecap = pecRound) and not (plNoEndCap in options) then
begin
if not (plRoundCapOpen in options) then
AddRoundCap(pts[high(pts)],borders[high(pts)-1].leftSide.dir,false)
else
AddRoundCapAlphaJoin(pts[high(pts)],
-borders[high(pts)-1].leftSide.dir, false,true);
end;