-
Notifications
You must be signed in to change notification settings - Fork 12
/
bgrapath.pas
2997 lines (2750 loc) · 91.9 KB
/
bgrapath.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 BGRAPath;
{$i bgrabitmap.inc}{$H+}
{.$POINTERMATH ON}
interface
//todo: tangent interpolation
{ There are different conventions for angles.
First is about the unit. It can be one of the following:
- degrees (0..360)
- radian (0..2*Pi)
- tenth of degrees (0..3600)
- from 0 to 65536
Second is about the origin. It can be one of the following:
- right-most position (this is the default origin for radian and 65536)
- top-most position (this is the default origin for degrees)
Third is about the sign. It can be one of the following:
- positive is clockwise (this is the default for degrees)
- positive is counterclockwise (this is the default for radian and 65536)
TBGRAPath and TBGRACanvas2D follow HTML5 convention which is:
(radian, right-most, clockwise) that can be shortened to (radian, clockwise)
because right-most is the default for radian. This is abbreviated as "radCW".
When radian are CCW, it is also specified in order to make it clear, even
if it is the default convention in mathematics.
In order to make things easier, there are some functions that accept angles
in degrees. The convention used here is the usual degree convention:
(degrees, top-most, clockwise) that can be shortened to (degree)
because top-most and clockwise is the default for degrees.
}
uses
Classes, BGRATypes, BGRABitmapTypes, {$IFNDEF FPC}GraphType,{$ENDIF} BGRAGraphics, BGRATransform;
type
TBGRAPathElementType = (peNone, peMoveTo, peLineTo, peCloseSubPath,
peQuadraticBezierTo, peCubicBezierTo, peArc, peOpenedSpline,
peClosedSpline);
TBGRAPathDrawProc = procedure(const APoints: array of TPointF; AClosed: boolean; AData: Pointer) of object;
TBGRAPathFillProc = procedure(const APoints: array of TPointF; AData: pointer) of object;
TBGRAPath = class;
{ TBGRAPathCursor }
TBGRAPathCursor = class(TBGRACustomPathCursor)
protected
FPath: TBGRAPath;
FDataPos: BGRAPtrInt;
FAcceptedDeviation: single;
FPathLength: single;
FPathLengthComputed: boolean;
FBounds: TRectF;
FBoundsComputed: boolean;
FArcPos: Single;
FStartCoordinate: TPointF;
FEndCoordinate: TPointF;
FLoopClosedShapes,FLoopPath: boolean;
FCurrentElementType: TBGRAPathElementType;
FCurrentElement: Pointer;
FCurrentElementArcPos,
FCurrentElementArcPosScale: single;
FCurrentElementStartCoord,
FCurrentElementEndCoord: TPointF;
FCurrentElementLength: single;
FCurrentElementPoints: ArrayOfTPointF;
FCurrentSegment: BGRANativeInt;
FCurrentSegmentPos: single;
function GoToNextElement(ACanJump: boolean): boolean;
function GoToPreviousElement(ACanJump: boolean): boolean;
procedure MoveToEndOfElement;
procedure MoveForwardInElement(ADistance: single);
procedure MoveBackwardInElement(ADistance: single);
function NeedPolygonalApprox: boolean;
procedure OnPathFree; virtual;
function GetLoopClosedShapes: boolean; override;
function GetLoopPath: boolean; override;
function GetStartCoordinate: TPointF; override;
procedure SetLoopClosedShapes(AValue: boolean); override;
procedure SetLoopPath(AValue: boolean); override;
function GetArcPos: single; override;
function GetCurrentTangent: TPointF; override;
procedure SetArcPos(AValue: single); override;
function GetBounds: TRectF; override;
function GetPathLength: single; override;
procedure PrepareCurrentElement; virtual;
function GetCurrentCoord: TPointF; override;
function GetPath: TBGRAPath; virtual;
public
constructor Create(APath: TBGRAPath; AAcceptedDeviation: single = 0.1);
function MoveForward(ADistance: single; ACanJump: boolean = true): single; override;
function MoveBackward(ADistance: single; ACanJump: boolean = true): single; override;
destructor Destroy; override;
property CurrentCoordinate: TPointF read GetCurrentCoord;
property CurrentTangent: TPointF read GetCurrentTangent;
property Position: single read GetArcPos write SetArcPos;
property PathLength: single read GetPathLength;
property Path: TBGRAPath read GetPath;
property Bounds: TRectF read GetBounds;
property StartCoordinate: TPointF read GetStartCoordinate;
property LoopClosedShapes: boolean read GetLoopClosedShapes write SetLoopClosedShapes;
property LoopPath: boolean read GetLoopPath write SetLoopPath;
property AcceptedDeviation: single read FAcceptedDeviation;
end;
{ TBGRAPath }
TBGRAPath = class(TObject, IBGRAPath)
protected
FData: PByte;
FDataCapacity: BGRAPtrInt;
FDataPos: BGRAPtrInt;
FLastSubPathElementType, FLastStoredElementType: TBGRAPathElementType;
FLastMoveToDataPos: BGRAPtrInt;
FLastCoord,FLastTransformedCoord,
FSubPathStartCoord, FSubPathTransformedStartCoord: TPointF;
FExpectedTransformedControlPoint: TPointF;
FMatrix: TAffineMatrix; //this matrix must have a base of vectors
//orthogonal, of same length and with positive
//orientation in order to preserve arcs
FScale,FAngleRadCW: single;
FCursors: array of TBGRAPathCursor;
FInternalDrawOffset: TPointF;
procedure OnModify;
procedure OnMatrixChange;
procedure NeedSpace(count: integer);
function AllocateElement(AElementType: TBGRAPathElementType;
AExtraBytes: BGRAPtrInt = 0): Pointer;
procedure Init;
procedure DoClear;
function CheckElementType(AElementType: TBGRAPathElementType): boolean;
function GoToNextElement(var APos: BGRAPtrInt): boolean;
function GoToPreviousElement(var APos: BGRAPtrInt): boolean;
function PeekNextElement(APos: BGRAPtrInt): TBGRAPathElementType;
function GetElementStartCoord(APos: BGRAPtrInt): TPointF;
function GetElementEndCoord(APos: BGRAPtrInt): TPointF;
function GetElementLength(APos: BGRAPtrInt; AAcceptedDeviation: single): single;
procedure GetElementAt(APos: BGRAPtrInt;
out AElementType: TBGRAPathElementType; out AElement: pointer);
function GetSvgString: string; virtual;
procedure SetSvgString(const AValue: string); virtual;
procedure RegisterCursor(ACursor: TBGRAPathCursor);
procedure UnregisterCursor(ACursor: TBGRAPathCursor);
function SetLastCoord(ACoord: TPointF): TPointF; {$ifdef inline}inline;{$endif}
procedure ClearLastCoord;
procedure BezierCurveFromTransformed(tcp1, cp2, pt:TPointF);
procedure QuadraticCurveFromTransformed(tcp, pt: TPointF);
function LastCoordDefined: boolean; {$ifdef inline}inline;{$endif}
function GetPolygonalApprox(APos: BGRAPtrInt; AAcceptedDeviation: single; AIncludeFirstPoint: boolean): ArrayOfTPointF;
function getPoints: ArrayOfTPointF; overload;
function getPoints(AMatrix: TAffineMatrix): ArrayOfTPointF; overload;
function getCursor: TBGRACustomPathCursor;
procedure InternalDraw(ADrawProc: TBGRAPathDrawProc; const AMatrix: TAffineMatrix; AAcceptedDeviation: single; AData: pointer);
procedure BitmapDrawSubPathProc(const APoints: array of TPointF; AClosed: boolean; AData: pointer);
function CorrectAcceptedDeviation(AAcceptedDeviation: single; const AMatrix: TAffineMatrix): single;
public
constructor Create; overload;
constructor Create(ASvgString: string); overload;
constructor Create(const APoints: ArrayOfTPointF); overload;
constructor Create(APath: IBGRAPath); overload;
destructor Destroy; override;
procedure beginPath;
procedure beginSubPath;
procedure closePath;
procedure translate(x,y: single);
procedure resetTransform;
procedure rotate(angleRadCW: single); overload;
procedure rotateDeg(angleDeg: single); overload;
procedure rotate(angleRadCW: single; center: TPointF); overload;
procedure rotateDeg(angleDeg: single; center: TPointF); overload;
procedure scale(factor: single);
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 polyline(const pts: array of TPointF);
procedure polylineTo(const pts: array of TPointF);
procedure polygon(const pts: array of TPointF);
procedure quadraticCurveTo(cpx,cpy,x,y: single); overload;
procedure quadraticCurveTo(const cp,pt: TPointF); overload;
procedure quadraticCurve(const curve: TQuadraticBezierCurve); overload;
procedure quadraticCurve(p1,cp,p2: TPointF); overload;
procedure smoothQuadraticCurveTo(x,y: single); overload;
procedure smoothQuadraticCurveTo(const pt: TPointF); overload;
procedure bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y: single); overload;
procedure bezierCurveTo(const cp1,cp2,pt: TPointF); overload;
procedure bezierCurve(const curve: TCubicBezierCurve); overload;
procedure bezierCurve(p1,cp1,cp2,p2: TPointF); overload;
procedure smoothBezierCurveTo(cp2x,cp2y,x,y: single); overload;
procedure smoothBezierCurveTo(const cp2,pt: TPointF); overload;
procedure rect(x,y,w,h: single);
procedure roundRect(x,y,w,h,radius: single);
procedure arc(cx, cy, radius, startAngleRadCW, endAngleRadCW: single; anticlockwise: boolean); overload;
procedure arc(cx, cy, radius, startAngleRadCW, endAngleRadCW: single); overload;
procedure arcDeg(cx, cy, radius, startAngleDeg, endAngleDeg: single; anticlockwise: boolean); overload;
procedure arcDeg(cx, cy, radius, startAngleDeg, endAngleDeg: single); overload;
procedure arcTo(x1, y1, x2, y2, radius: single); overload;
procedure arcTo(const p1,p2: TPointF; radius: single); overload;
procedure arc(const arcDef: TArcDef); overload;
procedure arc(cx, cy, rx,ry: single; xAngleRadCW, startAngleRadCW, endAngleRadCW: single); overload;
procedure arc(cx, cy, rx,ry, xAngleRadCW, startAngleRadCW, endAngleRadCW: single; anticlockwise: boolean); overload;
procedure arcTo(rx,ry, xAngleRadCW: single; largeArc, anticlockwise: boolean; x,y:single); overload;
procedure copyTo(dest: IBGRAPath);
procedure addPath(const AValue: string); overload;
procedure addPath(source: IBGRAPath); overload;
procedure openedSpline(const pts: array of TPointF; style: TSplineStyle);
procedure closedSpline(const pts: array of TPointF; style: TSplineStyle);
property SvgString: string read GetSvgString write SetSvgString;
function ComputeLength(AAcceptedDeviation: single = 0.1): single;
function ToPoints(AAcceptedDeviation: single = 0.1): ArrayOfTPointF; overload;
function ToPoints(AMatrix: TAffineMatrix; AAcceptedDeviation: single = 0.1): ArrayOfTPointF; overload;
function IsEmpty: boolean;
function GetBounds(AAcceptedDeviation: single = 0.1): TRectF;
procedure SetPoints(const APoints: ArrayOfTPointF);
procedure stroke(ABitmap: TBGRACustomBitmap; AColor: TBGRAPixel; AWidth: single; AAcceptedDeviation: single = 0.1); overload;
procedure stroke(ABitmap: TBGRACustomBitmap; ATexture: IBGRAScanner; AWidth: single; AAcceptedDeviation: single = 0.1); overload;
procedure stroke(ABitmap: TBGRACustomBitmap; x,y: single; AColor: TBGRAPixel; AWidth: single; AAcceptedDeviation: single = 0.1); overload;
procedure stroke(ABitmap: TBGRACustomBitmap; x,y: single; ATexture: IBGRAScanner; AWidth: single; AAcceptedDeviation: single = 0.1); overload;
procedure stroke(ABitmap: TBGRACustomBitmap; const AMatrix: TAffineMatrix; AColor: TBGRAPixel; AWidth: single; AAcceptedDeviation: single = 0.1); overload;
procedure stroke(ABitmap: TBGRACustomBitmap; const AMatrix: TAffineMatrix; ATexture: IBGRAScanner; AWidth: single; AAcceptedDeviation: single = 0.1); overload;
procedure stroke(ADrawProc: TBGRAPathDrawProc; const AMatrix: TAffineMatrix; AAcceptedDeviation: single = 0.1; AData: pointer = nil); overload;
procedure fill(ABitmap: TBGRACustomBitmap; AColor: TBGRAPixel; AAcceptedDeviation: single = 0.1); overload;
procedure fill(ABitmap: TBGRACustomBitmap; ATexture: IBGRAScanner; AAcceptedDeviation: single = 0.1); overload;
procedure fill(ABitmap: TBGRACustomBitmap; x,y: single; AColor: TBGRAPixel; AAcceptedDeviation: single = 0.1); overload;
procedure fill(ABitmap: TBGRACustomBitmap; x,y: single; ATexture: IBGRAScanner; AAcceptedDeviation: single = 0.1); overload;
procedure fill(ABitmap: TBGRACustomBitmap; const AMatrix: TAffineMatrix; AColor: TBGRAPixel; AAcceptedDeviation: single = 0.1); overload;
procedure fill(ABitmap: TBGRACustomBitmap; const AMatrix: TAffineMatrix; ATexture: IBGRAScanner; AAcceptedDeviation: single = 0.1); overload;
procedure fill(AFillProc: TBGRAPathFillProc; const AMatrix: TAffineMatrix; AAcceptedDeviation: single = 0.1; AData: pointer = nil); overload;
function CreateCursor(AAcceptedDeviation: single = 0.1): TBGRAPathCursor;
procedure Fit(ARect: TRectF; AAcceptedDeviation: single = 0.1);
procedure FitInto(ADest: TBGRAPath; ARect: TRectF; AAcceptedDeviation: single = 0.1);
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;
{----------------------- Spline ------------------}
function SplineVertexToSide(y0, y1, y2, y3: single; t: single): single;
function ComputeBezierCurve(const curve: TCubicBezierCurve; AAcceptedDeviation: single = 0.1): ArrayOfTPointF; overload;
function ComputeBezierCurve(const curve: TQuadraticBezierCurve; AAcceptedDeviation: single = 0.1): ArrayOfTPointF; overload;
function ComputeBezierSpline(const spline: array of TCubicBezierCurve; AAcceptedDeviation: single = 0.1): ArrayOfTPointF; overload;
function ComputeBezierSpline(const spline: array of TQuadraticBezierCurve; AAcceptedDeviation: single = 0.1): ArrayOfTPointF; overload;
function ComputeClosedSpline(const points: array of TPointF; Style: TSplineStyle; AAcceptedDeviation: single = 0.1): ArrayOfTPointF;
function ComputeOpenedSpline(const points: array of TPointF; Style: TSplineStyle; EndCoeff: single = 0.25; AAcceptedDeviation: single = 0.1): ArrayOfTPointF;
function ClosedSplineStartPoint(const points: array of TPointF; Style: TSplineStyle): TPointF;
{ Compute points to draw an antialiased ellipse }
function ComputeEllipse(x,y,rx,ry: single; quality: single = 1): ArrayOfTPointF;
function ComputeArc65536(x, y, rx, ry: single; start65536,end65536: BGRAWord; quality: single = 1): ArrayOfTPointF;
function ComputeArcRad(x, y, rx, ry: single; startRadCCW,endRadCCW: single; quality: single = 1): ArrayOfTPointF;
function ComputeArc(const arc: TArcDef; quality: single = 1): ArrayOfTPointF;
function ComputeRoundRect(x1,y1,x2,y2,rx,ry: single; quality: single = 1): ArrayOfTPointF; overload;
function ComputeRoundRect(x1,y1,x2,y2,rx,ry: single; options: TRoundRectangleOptions; quality: single = 1): ArrayOfTPointF; overload;
function Html5ArcTo(const p0, p1, p2: TPointF; radius: single): TArcDef;
function SvgArcTo(const p0: TPointF; rx, ry, xAngleRadCW: single; largeArc,
anticlockwise: boolean; const p1: TPointF): TArcDef;
function ArcStartPoint(const arc: TArcDef): TPointF;
function ArcEndPoint(const arc: TArcDef): TPointF;
function IsLargeArc(const arc: TArcDef): boolean;
implementation
uses Math, BGRAResample, SysUtils;
type
TStrokeData = record
Bitmap: TBGRACustomBitmap;
Texture: IBGRAScanner;
Color: TBGRAPixel;
Width: Single;
end;
PPathElementHeader = ^TPathElementHeader;
TPathElementHeader = record
ElementType: TBGRAPathElementType;
PreviousElementType: TBGRAPathElementType;
end;
PMoveToElement = ^TMoveToElement;
TMoveToElement = record
StartCoordinate: TPointF;
LoopDataPos: BGRAPtrInt; //if the path is closed
end;
PClosePathElement = ^TClosePathElement;
TClosePathElement = type TMoveToElement;
PQuadraticBezierToElement = ^TQuadraticBezierToElement;
TQuadraticBezierToElement = record
ControlPoint, Destination: TPointF;
end;
PCubicBezierToElement = ^TCubicBezierToElement;
TCubicBezierToElement = record
ControlPoint1, ControlPoint2, Destination: TPointF;
end;
PArcElement = ^TArcElement;
TArcElement = TArcDef;
PSplineElement = ^TSplineElement;
TSplineElement = record
SplineStyle: TSplineStyle;
NbControlPoints: integer;
end;
const
PathElementSize : array[TBGRAPathElementType] of BGRAPtrInt =
(0, Sizeof(TMoveToElement), Sizeof(TClosePathElement), sizeof(TPointF),
sizeof(TQuadraticBezierToElement), sizeof(TCubicBezierToElement),
sizeof(TArcElement), sizeof(TSplineElement)+sizeof(integer),
sizeof(TSplineElement)+sizeof(integer));
function SplineVertexToSide(y0, y1, y2, y3: single; t: single): single;
var
a0, a1, a2, a3: single;
t2: single;
begin
t2 := t * t;
a0 := y3 - y2 - y0 + y1;
a1 := y0 - y1 - a0;
a2 := y2 - y0;
a3 := y1;
Result := a0 * t * t2 + a1 * t2 + a2 * t + a3;
end;
function ComputeCurvePartPrecision(pt1, pt2, pt3, pt4: TPointF; AAcceptedDeviation: single = 0.1): integer;
var
len: single;
begin
len := sqr(pt1.x - pt2.x) + sqr(pt1.y - pt2.y);
len := max(len, sqr(pt3.x - pt2.x) + sqr(pt3.y - pt2.y));
len := max(len, sqr(pt3.x - pt4.x) + sqr(pt3.y - pt4.y));
Result := round(sqrt(sqrt(len)/AAcceptedDeviation) * 0.9);
if Result<=0 then Result:=1;
end;
function ComputeBezierCurve(const curve: TCubicBezierCurve; AAcceptedDeviation: single = 0.1): ArrayOfTPointF; overload;
begin
result := curve.ToPoints(AAcceptedDeviation);
end;
function ComputeBezierCurve(const curve: TQuadraticBezierCurve; AAcceptedDeviation: single = 0.1): ArrayOfTPointF; overload;
begin
result := curve.ToPoints(AAcceptedDeviation);
end;
function ComputeBezierSpline(const spline: array of TCubicBezierCurve; AAcceptedDeviation: single = 0.1): ArrayOfTPointF;
var
curves: array of ArrayOfTPointF;
nb: integer;
lastPt: TPointF;
i: Integer;
j: Integer;
procedure AddPt(pt: TPointF); //@{$ifdef inline}inline;{$endif}
begin
result[nb]:= pt;
inc(nb);
lastPt := pt;
end;
function EqLast(pt: TPointF): boolean;
begin
result := (pt.x = lastPt.x) and (pt.y = lastPt.y);
end;
begin
if length(spline)= 0 then
begin
setlength(result,0);
exit;
end;
setlength(curves, length(spline));
for i := 0 to high(spline) do
curves[i] := ComputeBezierCurve(spline[i],AAcceptedDeviation);
nb := length(curves[0]);
lastPt := curves[0][high(curves[0])];
for i := 1 to high(curves) do
begin
inc(nb,length(curves[i]));
if EqLast(curves[i][0]) then dec(nb);
lastPt := curves[i][high(curves[i])];
end;
setlength(result,nb);
nb := 0;
for j := 0 to high(curves[0]) do
AddPt(curves[0][j]);
for i := 1 to high(curves) do
begin
if not EqLast(curves[i][0]) then AddPt(curves[i][0]);
for j := 1 to high(curves[i]) do
AddPt(curves[i][j]);
end;
end;
function ComputeBezierSpline(const spline: array of TQuadraticBezierCurve;
AAcceptedDeviation: single = 0.1): ArrayOfTPointF;
var
curves: array of ArrayOfTPointF;
nb: integer;
lastPt: TPointF;
i: Integer;
j: Integer;
procedure AddPt(pt: TPointF); //@{$ifdef inline}inline;{$endif}
begin
result[nb]:= pt;
inc(nb);
lastPt := pt;
end;
function EqLast(pt: TPointF): boolean;
begin
result := (pt.x = lastPt.x) and (pt.y = lastPt.y);
end;
begin
if length(spline)= 0 then
begin
setlength(result,0);
exit;
end;
setlength(curves, length(spline));
for i := 0 to high(spline) do
curves[i] := ComputeBezierCurve(spline[i],AAcceptedDeviation);
nb := length(curves[0]);
lastPt := curves[0][high(curves[0])];
for i := 1 to high(curves) do
begin
inc(nb,length(curves[i]));
if EqLast(curves[i][0]) then dec(nb);
lastPt := curves[i][high(curves[i])];
end;
setlength(result,nb);
nb := 0;
for j := 0 to high(curves[0]) do
AddPt(curves[0][j]);
for i := 1 to high(curves) do
begin
if not EqLast(curves[i][0]) then AddPt(curves[i][0]);
for j := 1 to high(curves[i]) do
AddPt(curves[i][j]);
end;
end;
function ComputeClosedSpline(const points: array of TPointF; Style: TSplineStyle; AAcceptedDeviation: single = 0.1): ArrayOfTPointF;
var
i, j, nb, idx, pre: integer;
ptPrev, ptPrev2, ptNext, ptNext2: TPointF;
t: single;
kernel: TWideKernelFilter;
begin
if length(points) <= 2 then
begin
setlength(result,length(points));
for i := 0 to high(result) do
result[i] := points[i];
exit;
end;
nb := 1;
for i := 0 to high(points) do
begin
ptPrev2 := points[(i + length(points) - 1) mod length(points)];
ptPrev := points[i];
ptNext := points[(i + 1) mod length(points)];
ptNext2 := points[(i + 2) mod length(points)];
nb := nb +ComputeCurvePartPrecision(ptPrev2, ptPrev, ptNext, ptNext2, AAcceptedDeviation);
end;
kernel := CreateInterpolator(style);
setlength(Result, nb);
idx := 0;
for i := 0 to high(points) do
begin
ptPrev2 := points[(i + length(points) - 1) mod length(points)];
ptPrev := points[i];
ptNext := points[(i + 1) mod length(points)];
ptNext2 := points[(i + 2) mod length(points)];
pre := ComputeCurvePartPrecision(ptPrev2, ptPrev, ptNext, ptNext2, AAcceptedDeviation);
if i=0 then
j := 0
else
j := 1;
while j <= pre do
begin
t := j/pre;
result[idx] := ptPrev2*kernel.Interpolation(t+1) + ptPrev*kernel.Interpolation(t) +
ptNext*kernel.Interpolation(t-1) + ptNext2*kernel.Interpolation(t-2);
Inc(idx);
inc(j);
end;
end;
kernel.Free;
end;
function ComputeOpenedSpline(const points: array of TPointF; Style: TSplineStyle; EndCoeff: single; AAcceptedDeviation: single): ArrayOfTPointF;
var
i, j, nb, idx, pre: integer;
ptPrev, ptPrev2, ptNext, ptNext2: TPointF;
t: single;
kernel: TWideKernelFilter;
begin
if length(points) <= 2 then
begin
setlength(result,length(points));
for i := 0 to high(result) do
result[i] := points[i];
exit;
end;
if style in[ssInsideWithEnds,ssCrossingWithEnds] then EndCoeff := 0;
if EndCoeff < -0.3 then EndCoeff := -0.3;
nb := 1;
for i := 0 to high(points) - 1 do
begin
ptPrev := points[i];
ptNext := points[i + 1];
if i=0 then
ptPrev2 := (ptPrev+(ptNext+points[i + 2])*EndCoeff)*(1/(1+2*EndCoeff))
else
ptPrev2 := points[i - 1];
if i = high(points)-1 then
ptNext2 := (ptNext+(ptPrev+points[i - 1])*EndCoeff)*(1/(1+2*EndCoeff))
else
ptNext2 := points[i + 2];
nb := nb +ComputeCurvePartPrecision(ptPrev2, ptPrev, ptNext, ptNext2, AAcceptedDeviation);
end;
kernel := CreateInterpolator(style);
if Style in[ssInsideWithEnds,ssCrossingWithEnds] then
begin
inc(nb,2);
setlength(Result, nb);
result[0] := points[0];
idx := 1;
end else
begin
idx := 0;
setlength(Result, nb);
end;
for i := 0 to high(points) - 1 do
begin
ptPrev := points[i];
ptNext := points[i + 1];
if i=0 then
ptPrev2 := (ptPrev+(ptNext+points[i + 2])*EndCoeff)*(1/(1+2*EndCoeff))
else
ptPrev2 := points[i - 1];
if i = high(points)-1 then
ptNext2 := (ptNext+(ptPrev+points[i - 1])*EndCoeff)*(1/(1+2*EndCoeff))
else
ptNext2 := points[i + 2];
pre := ComputeCurvePartPrecision(ptPrev2, ptPrev, ptNext, ptNext2, AAcceptedDeviation);
if i=0 then
begin
j := 0;
end else j := 1;
while j <= pre do
begin
t := j/pre;
result[idx] := ptPrev2*kernel.Interpolation(t+1) + ptPrev*kernel.Interpolation(t) +
ptNext*kernel.Interpolation(t-1) + ptNext2*kernel.Interpolation(t-2);
Inc(idx);
inc(j);
end;
end;
kernel.Free;
if Style in[ssInsideWithEnds,ssCrossingWithEnds] then
result[idx] := points[high(points)];
end;
function ClosedSplineStartPoint(const points: array of TPointF;
Style: TSplineStyle): TPointF;
var
kernel: TWideKernelFilter;
ptPrev2: TPointF;
ptPrev: TPointF;
ptNext: TPointF;
ptNext2: TPointF;
begin
if length(points) = 0 then
result := EmptyPointF
else
if length(points)<=2 then
result := points[0]
else
begin
kernel := CreateInterpolator(style);
ptPrev2 := points[high(points)];
ptPrev := points[0];
ptNext := points[1];
ptNext2 := points[2];
result := ptPrev2*kernel.Interpolation(1) + ptPrev*kernel.Interpolation(0) +
ptNext*kernel.Interpolation(-1) + ptNext2*kernel.Interpolation(-2);
kernel.free;
end;
end;
function ComputeArc65536(x, y, rx, ry: single; start65536,end65536: BGRAWord; quality: single): ArrayOfTPointF;
var i,nb: integer;
arclen: integer;
pos: BGRAWord;
begin
if end65536 > start65536 then
arclen := end65536-start65536 else
arclen := 65536-(start65536-end65536);
if quality < 0 then quality := 0;
nb := round(((rx+ry)*2*quality+8)*arclen/65536) and not 3;
if arclen <= 16384 then
begin
if nb < 2 then nb := 2;
end else
if arclen <= 32768 then
begin
if nb < 3 then nb := 3;
end else
if arclen <= 32768+16384 then
begin
if nb < 4 then nb := 4;
end else
if nb < 5 then nb := 5;
if nb > arclen+1 then nb := arclen+1;
setlength(result,nb);
for i := 0 to nb-1 do
begin
{.$PUSH}{$R-}
pos := start65536+BGRAInt64(i)*arclen div (BGRAInt64(nb)-1);
{.$POP}
result[i] := PointF(x+rx*(Cos65536(pos)-32768)/32768,
y-ry*(Sin65536(pos)-32768)/32768);
end;
end;
function ComputeEllipse(x, y, rx, ry: single; quality: single): ArrayOfTPointF;
begin
result := ComputeArc65536(x,y,rx,ry,0,0,quality);
end;
function ComputeArcRad(x, y, rx, ry: single; startRadCCW, endRadCCW: single;
quality: single): ArrayOfTPointF;
begin
result := ComputeArc65536(x,y,rx,ry,round(startRadCCW*32768/Pi) and $ffff,round(endRadCCW*32768/Pi) and $ffff,quality);
result[0] := PointF(x+cos(startRadCCW)*rx,y-sin(startRadCCW)*ry);
result[high(result)] := PointF(x+cos(endRadCCW)*rx,y-sin(endRadCCW)*ry);
end;
function ComputeArc(const arc: TArcDef; quality: single): ArrayOfTPointF;
var startAngle,endAngle: single;
i,n: integer;
temp: TPointF;
m: TAffineMatrix;
begin
startAngle := -arc.startAngleRadCW;
endAngle:= -arc.endAngleRadCW;
if not arc.anticlockwise then
begin
result := ComputeArcRad(arc.center.x,arc.center.y,arc.radius.x,arc.radius.y,endAngle,startAngle,quality);
n := length(result);
if n>1 then
for i := 0 to (n-2) div 2 do
begin
temp := result[i];
result[i] := result[n-1-i];
result[n-1-i] := temp;
end;
end else
result := ComputeArcRad(arc.center.x,arc.center.y,arc.radius.x,arc.radius.y,startAngle,endAngle,quality);
if arc.xAngleRadCW <> 0 then
begin
m := AffMatrixMult(AffMatrixMult(AffineMatrixTranslation(arc.center.x,arc.center.y),AffineMatrixRotationRad(-arc.xAngleRadCW)),AffineMatrixTranslation(-arc.center.x,-arc.center.y));
for i := 0 to high(result) do
result[i] := AffMatrixMult(m,result[i]);
end;
end;
function ComputeRoundRect(x1,y1,x2,y2,rx,ry: single; quality: single): ArrayOfTPointF;
begin
result := ComputeRoundRect(x1,y1,x2,y2,rx,ry,[],quality);
end;
function ComputeRoundRect(x1, y1, x2, y2, rx, ry: single;
options: TRoundRectangleOptions; quality: single): ArrayOfTPointF;
var q0,q1,q2,q3,q4: ArrayOfTPointF;
temp: Single;
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;
rx := abs(rx);
ry := abs(ry);
if 2*rx > x2-x1 then
rx := (x2-x1)/2;
if 2*ry > y2-y1 then
ry := (y2-y1)/2;
q0 := PointsF([PointF(x2,(y1+y2)/2)]);
if rrTopRightBevel in options then
q1 := PointsF([PointF(x2,y1+ry),PointF(x2-rx,y1)]) else
if rrTopRightSquare in options then
q1 := PointsF([PointF(x2,y1)])
else
q1 := ComputeArc65536(x2-rx,y1+ry,rx,ry,0,16384,quality);
if rrTopLeftBevel in options then
q2 := PointsF([PointF(x1+rx,y1),PointF(x1,y1+ry)]) else
if rrTopLeftSquare in options then
q2 := PointsF([PointF(x1,y1)])
else
q2 := ComputeArc65536(x1+rx,y1+ry,rx,ry,16384,32768,quality);
if rrBottomLeftBevel in options then
q3 := PointsF([PointF(x1,y2-ry),PointF(x1+rx,y2)]) else
if rrBottomLeftSquare in options then
q3 := PointsF([PointF(x1,y2)])
else
q3 := ComputeArc65536(x1+rx,y2-ry,rx,ry,32768,32768+16384,quality);
if rrBottomRightBevel in options then
q4 := PointsF([PointF(x2-rx,y2),PointF(x2,y2-ry)]) else
if rrBottomRightSquare in options then
q4 := PointsF([PointF(x2,y2)])
else
q4 := ComputeArc65536(x2-rx,y2-ry,rx,ry,32768+16384,0,quality);
result := ConcatPointsF([q0,q1,q2,q3,q4]);
end;
function Html5ArcTo(const p0, p1, p2: TPointF; radius: single
): TArcDef;
var p3,p4,an,bn,cn,c: TPointF;
dir, a2, b2, c2, cosx, sinx, d: single;
anticlockwise: boolean;
begin
result.center := p1;
result.radius := PointF(0,0);
result.xAngleRadCW:= 0;
result.startAngleRadCW := 0;
result.endAngleRadCW:= 0;
result.anticlockwise:= false;
radius := abs(radius);
if (p0 = p1) or (p1 = p2) or (radius = 0) then exit;
dir := (p2.x-p1.x)*(p0.y-p1.y) + (p2.y-p1.y)*(p1.x-p0.x);
if dir = 0 then exit;
a2 := (p0.x-p1.x)*(p0.x-p1.x) + (p0.y-p1.y)*(p0.y-p1.y);
b2 := (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y);
c2 := (p0.x-p2.x)*(p0.x-p2.x) + (p0.y-p2.y)*(p0.y-p2.y);
cosx := (a2+b2-c2)/(2*sqrt(a2*b2));
sinx := sqrt(1 - cosx*cosx);
if (sinx = 0) or (cosx = 1) then exit;
d := radius / ((1 - cosx) / sinx);
an := (p1-p0)*(1/sqrt(a2));
bn := (p1-p2)*(1/sqrt(b2));
p3 := p1 - an*d;
p4 := p1 - bn*d;
anticlockwise := (dir < 0);
cn := PointF(an.y,-an.x)*radius;
if not anticlockwise then cn := -cn;
c := p3 + cn;
result.center := c;
result.radius:= PointF(radius,radius);
result.startAngleRadCW := arctan2((p3.y-c.y), (p3.x-c.x));
result.endAngleRadCW := arctan2((p4.y-c.y), (p4.x-c.x));
result.anticlockwise:= anticlockwise;
end;
function SvgArcTo(const p0: TPointF; rx, ry, xAngleRadCW: single; largeArc,
anticlockwise: boolean; const p1: TPointF): TArcDef;
var
p0p,cp: TPointF;
cross1,cross2,lambda: single;
begin
if (rx=0) or (ry=0) or (p0 = p1) then
begin
result.radius := PointF(0,0);
result.xAngleRadCW:= 0;
result.anticlockwise := false;
result.endAngleRadCW := 0;
result.startAngleRadCW:= 0;
result.center := p1;
exit;
end;
result.xAngleRadCW := xAngleRadCW;
result.anticlockwise := anticlockwise;
p0p := AffMatrixMult(AffineMatrixRotationRad(xAngleRadCW),( (p0-p1)*0.5 ));
//ensure radius is big enough
lambda := sqr(p0p.x/rx) + sqr(p0p.y/ry);
if lambda > 1 then
begin
lambda := sqrt(lambda);
rx := rx *lambda;
ry := ry *lambda;
end;
result.radius := PointF(rx,ry);
//compute center
cross2 := sqr(rx*p0p.y) + sqr(ry*p0p.x);
cross1 := sqr(rx*ry);
if cross1 <= cross2 then
cp := PointF(0,0)
else
cp := sqrt((cross1-cross2)/cross2)*
PointF(rx*p0p.y/ry, -ry*p0p.x/rx);
if largeArc <> anticlockwise then cp := -cp;
result.center := AffMatrixMult(AffineMatrixRotationRad(-xAngleRadCW),cp) +
(p0+p1)*0.5;
result.startAngleRadCW := arctan2((p0p.y-cp.y)/ry,(p0p.x-cp.x)/rx);
result.endAngleRadCW := arctan2((-p0p.y-cp.y)/ry,(-p0p.x-cp.x)/rx);
end;
function ArcStartPoint(const arc: TArcDef): TPointF;
begin
result := AffMatrixMult(AffineMatrixRotationRad(-arc.xAngleRadCW),PointF(cos(arc.startAngleRadCW)*arc.radius.x,
sin(arc.startAngleRadCW)*arc.radius.y)) + arc.center;
end;
function ArcEndPoint(const arc: TArcDef): TPointF;
begin
result := AffMatrixMult(AffineMatrixRotationRad(-arc.xAngleRadCW),PointF(cos(arc.endAngleRadCW)*arc.radius.x,
sin(arc.endAngleRadCW)*arc.radius.y)) + arc.center;
end;
function IsLargeArc(const arc: TArcDef): boolean;
var diff,a1,a2: single;
begin
a1 := arc.startAngleRadCW - floor(arc.startAngleRadCW/(2*Pi))*(2*Pi);
a2 := arc.endAngleRadCW - floor(arc.endAngleRadCW/(2*Pi))*(2*Pi);
if not arc.anticlockwise then
diff := a2 - a1
else
diff := a1 - a2;
result := (diff < 0) or (diff >= Pi);
end;
{ TBGRAPathCursor }
function TBGRAPathCursor.GetCurrentCoord: TPointF;
begin
case FCurrentElementType of
peNone: result := EmptyPointF;
peMoveTo,peLineTo,peCloseSubPath:
if FCurrentElementLength <= 0 then
result := FCurrentElementStartCoord
else
result := FCurrentElementStartCoord + (FCurrentElementEndCoord-FCurrentElementStartCoord)*(FCurrentElementArcPos/FCurrentElementLength);
peCubicBezierTo,peQuadraticBezierTo,peArc,peOpenedSpline,peClosedSpline:
begin
NeedPolygonalApprox;
if FCurrentSegment >= high(FCurrentElementPoints) then
result := FCurrentElementEndCoord
else
result := FCurrentElementPoints[FCurrentSegment]+
(FCurrentElementPoints[FCurrentSegment+1]-
FCurrentElementPoints[FCurrentSegment])*FCurrentSegmentPos;
end;
else
raise Exception.Create('Unknown element type');
end;
end;
function TBGRAPathCursor.GetPath: TBGRAPath;
begin
if not Assigned(FPath) then
raise exception.Create('Path does not exist');
result := FPath;
end;
procedure TBGRAPathCursor.MoveToEndOfElement;
begin
FCurrentElementArcPos := FCurrentElementLength;
if not NeedPolygonalApprox then exit;
if length(FCurrentElementPoints) > 1 then
begin
FCurrentSegment := high(FCurrentElementPoints)-1;
FCurrentSegmentPos := 1;
end else
begin
FCurrentSegment := high(FCurrentElementPoints);
FCurrentSegmentPos := 0;
end;
end;
procedure TBGRAPathCursor.MoveForwardInElement(ADistance: single);
var segLen,rightSpace,remaining: single;
begin
if not NeedPolygonalApprox then exit;
ADistance := ADistance *FCurrentElementArcPosScale;
remaining := ADistance;
while remaining > 0 do
begin
if FCurrentSegment < high(FCurrentElementPoints) then
segLen := VectLen(FCurrentElementPoints[FCurrentSegment+1]-FCurrentElementPoints[FCurrentSegment])
else
segLen := 0;
rightSpace := segLen*(1-FCurrentSegmentPos);
if (segLen > 0) and (remaining <= rightSpace) then
begin
FCurrentSegmentPos := FCurrentSegmentPos +(remaining/segLen);
exit;
end else
begin
remaining := remaining -rightSpace;
if FCurrentSegment < high(FCurrentElementPoints)-1 then
begin
inc(FCurrentSegment);
FCurrentSegmentPos := 0;
end else
begin
FCurrentSegmentPos := 1;
exit;
end;
end;
end;
end;
procedure TBGRAPathCursor.MoveBackwardInElement(ADistance: single);
var
segLen,leftSpace,remaining: Single;
begin
if not NeedPolygonalApprox then exit;
ADistance := ADistance *FCurrentElementArcPosScale;
remaining := ADistance;
while remaining > 0 do