@@ -15,25 +15,15 @@ exports.name = 'convertPathData';
15
15
exports . description =
16
16
'optimizes path data: writes in shorter form, applies transformations' ;
17
17
18
- /**
19
- * @type {(data: number[]) => number[] }
20
- */
18
+ /** @type {(data: number[]) => number[] } */
21
19
let roundData ;
22
- /**
23
- * @type {number | false }
24
- */
20
+ /** @type {number | false } */
25
21
let precision ;
26
- /**
27
- * @type {number }
28
- */
22
+ /** @type {number } */
29
23
let error ;
30
- /**
31
- * @type {number }
32
- */
24
+ /** @type {number } */
33
25
let arcThreshold ;
34
- /**
35
- * @type {number }
36
- */
26
+ /** @type {number } */
37
27
let arcTolerance ;
38
28
39
29
/**
@@ -393,16 +383,17 @@ function filters(
393
383
params ,
394
384
{ isSafeToUseZ, maybeHasStrokeAndLinecap, hasMarkerMid } ,
395
385
) {
396
- var stringify = data2Path . bind ( null , params ) ,
397
- relSubpoint = [ 0 , 0 ] ,
398
- pathBase = [ 0 , 0 ] ,
399
- prev = { } ;
386
+ const stringify = data2Path . bind ( null , params ) ;
387
+ const relSubpoint = [ 0 , 0 ] ;
388
+ const pathBase = [ 0 , 0 ] ;
389
+ /** @type {any } */
390
+ let prev = { } ;
400
391
/** @type {Point | undefined } */
401
- let qControlPoint ;
392
+ let prevQControlPoint ;
402
393
403
394
path = path . filter ( function ( item , index , path ) {
404
- const qPoint = qControlPoint ;
405
- qControlPoint = undefined ;
395
+ const qControlPoint = prevQControlPoint ;
396
+ prevQControlPoint = undefined ;
406
397
407
398
let command = item . command ;
408
399
let data = item . args ;
@@ -415,9 +406,8 @@ function filters(
415
406
if ( command === 's' ) {
416
407
sdata = [ 0 , 0 ] . concat ( data ) ;
417
408
418
- // @ts -ignore
419
- var pdata = prev . args ,
420
- n = pdata . length ;
409
+ const pdata = prev . args ;
410
+ const n = pdata . length ;
421
411
422
412
// (-x, -y) of the prev tangent point relative to the current point
423
413
sdata [ 0 ] = pdata [ n - 2 ] - pdata [ n - 4 ] ;
@@ -464,24 +454,18 @@ function filters(
464
454
nextLonghand ;
465
455
466
456
if (
467
- // @ts -ignore
468
457
( prev . command == 'c' &&
469
- // @ts -ignore
470
458
isConvex ( prev . args ) &&
471
- // @ts -ignore
472
459
isArcPrev ( prev . args , circle ) ) ||
473
- // @ts -ignore
474
460
( prev . command == 'a' && prev . sdata && isArcPrev ( prev . sdata , circle ) )
475
461
) {
476
- // @ts -ignore
477
462
arcCurves . unshift ( prev ) ;
478
463
// @ts -ignore
479
464
arc . base = prev . base ;
480
465
// @ts -ignore
481
466
arc . args [ 5 ] = arc . coords [ 0 ] - arc . base [ 0 ] ;
482
467
// @ts -ignore
483
468
arc . args [ 6 ] = arc . coords [ 1 ] - arc . base [ 1 ] ;
484
- // @ts -ignore
485
469
var prevData = prev . command == 'a' ? prev . sdata : prev . args ;
486
470
var prevAngle = findArcAngle ( prevData , {
487
471
center : [
@@ -498,7 +482,7 @@ function filters(
498
482
// check if next curves are fitting the arc
499
483
for (
500
484
var j = index ;
501
- ( next = path [ ++ j ] ) && ~ 'cs' . indexOf ( next . command ) ;
485
+ ( next = path [ ++ j ] ) && 'cs' . includes ( next . command ) ;
502
486
503
487
) {
504
488
var nextData = next . args ;
@@ -574,7 +558,6 @@ function filters(
574
558
relSubpoint [ 0 ] += prevArc . args [ 5 ] - prev . args [ prev . args . length - 2 ] ;
575
559
// @ts -ignore
576
560
relSubpoint [ 1 ] += prevArc . args [ 6 ] - prev . args [ prev . args . length - 1 ] ;
577
- // @ts -ignore
578
561
prev . command = 'a' ;
579
562
// @ts -ignore
580
563
prev . args = prevArc . args ;
@@ -588,11 +571,7 @@ function filters(
588
571
item . sdata = sdata . slice ( ) ; // preserve curve data for future checks
589
572
} else if ( arcCurves . length - 1 - hasPrev > 0 ) {
590
573
// filter out consumed next items
591
- path . splice . apply (
592
- path ,
593
- // @ts -ignore
594
- [ index + 1 , arcCurves . length - 1 - hasPrev ] . concat ( output ) ,
595
- ) ;
574
+ path . splice ( index + 1 , arcCurves . length - 1 - hasPrev , ...output ) ;
596
575
}
597
576
if ( ! arc ) return false ;
598
577
command = 'a' ;
@@ -679,9 +658,7 @@ function filters(
679
658
data = data . slice ( - 2 ) ;
680
659
} else if (
681
660
command === 't' &&
682
- // @ts -ignore
683
661
prev . command !== 'q' &&
684
- // @ts -ignore
685
662
prev . command !== 't'
686
663
) {
687
664
command = 'l' ;
@@ -716,39 +693,29 @@ function filters(
716
693
params . collapseRepeated &&
717
694
hasMarkerMid === false &&
718
695
( command === 'm' || command === 'h' || command === 'v' ) &&
719
- // @ts -ignore
720
696
prev . command &&
721
- // @ts -ignore
722
697
command == prev . command . toLowerCase ( ) &&
723
698
( ( command != 'h' && command != 'v' ) ||
724
- // @ts -ignore
725
699
prev . args [ 0 ] >= 0 == data [ 0 ] >= 0 )
726
700
) {
727
- // @ts -ignore
728
701
prev . args [ 0 ] += data [ 0 ] ;
729
702
if ( command != 'h' && command != 'v' ) {
730
- // @ts -ignore
731
703
prev . args [ 1 ] += data [ 1 ] ;
732
704
}
733
705
// @ts -ignore
734
706
prev . coords = item . coords ;
735
- // @ts -ignore
736
707
path [ index ] = prev ;
737
708
return false ;
738
709
}
739
710
740
711
// convert curves into smooth shorthands
741
- // @ts -ignore
742
712
if ( params . curveSmoothShorthands && prev . command ) {
743
713
// curveto
744
714
if ( command === 'c' ) {
745
715
// c + c → c + s
746
716
if (
747
- // @ts -ignore
748
717
prev . command === 'c' &&
749
- // @ts -ignore
750
718
Math . abs ( data [ 0 ] - - ( prev . args [ 2 ] - prev . args [ 4 ] ) ) < error &&
751
- // @ts -ignore
752
719
Math . abs ( data [ 1 ] - - ( prev . args [ 3 ] - prev . args [ 5 ] ) ) < error
753
720
) {
754
721
command = 's' ;
@@ -757,11 +724,8 @@ function filters(
757
724
758
725
// s + c → s + s
759
726
else if (
760
- // @ts -ignore
761
727
prev . command === 's' &&
762
- // @ts -ignore
763
728
Math . abs ( data [ 0 ] - - ( prev . args [ 0 ] - prev . args [ 2 ] ) ) < error &&
764
- // @ts -ignore
765
729
Math . abs ( data [ 1 ] - - ( prev . args [ 1 ] - prev . args [ 3 ] ) ) < error
766
730
) {
767
731
command = 's' ;
@@ -770,9 +734,7 @@ function filters(
770
734
771
735
// [^cs] + c → [^cs] + s
772
736
else if (
773
- // @ts -ignore
774
737
prev . command !== 'c' &&
775
- // @ts -ignore
776
738
prev . command !== 's' &&
777
739
Math . abs ( data [ 0 ] ) < error &&
778
740
Math . abs ( data [ 1 ] ) < error
@@ -786,24 +748,22 @@ function filters(
786
748
else if ( command === 'q' ) {
787
749
// q + q → q + t
788
750
if (
789
- // @ts -ignore
790
751
prev . command === 'q' &&
791
- // @ts -ignore
792
752
Math . abs ( data [ 0 ] - ( prev . args [ 2 ] - prev . args [ 0 ] ) ) < error &&
793
- // @ts -ignore
794
753
Math . abs ( data [ 1 ] - ( prev . args [ 3 ] - prev . args [ 1 ] ) ) < error
795
754
) {
796
755
command = 't' ;
797
756
data = data . slice ( 2 ) ;
798
757
}
799
758
800
759
// t + q → t + t
801
- else if (
802
- // @ts -ignore
803
- prev . command === 't'
804
- ) {
805
- // @ts -ignore
806
- const predictedControlPoint = reflectPoint ( qPoint , item . base ) ;
760
+ else if ( prev . command === 't' ) {
761
+ const predictedControlPoint = reflectPoint (
762
+ // @ts -ignore
763
+ qControlPoint ,
764
+ // @ts -ignore
765
+ item . base ,
766
+ ) ;
807
767
const realControlPoint = [
808
768
// @ts -ignore
809
769
data [ 0 ] + item . base [ 0 ] ,
@@ -837,14 +797,12 @@ function filters(
837
797
return i === 0 ;
838
798
} )
839
799
) {
840
- // @ts -ignore
841
800
path [ index ] = prev ;
842
801
return false ;
843
802
}
844
803
845
804
// a 25,25 -30 0,1 0,0
846
805
if ( command === 'a' && data [ 5 ] === 0 && data [ 6 ] === 0 ) {
847
- // @ts -ignore
848
806
path [ index ] = prev ;
849
807
return false ;
850
808
}
@@ -874,7 +832,6 @@ function filters(
874
832
// z resets coordinates
875
833
relSubpoint [ 0 ] = pathBase [ 0 ] ;
876
834
relSubpoint [ 1 ] = pathBase [ 1 ] ;
877
- // @ts -ignore
878
835
if ( prev . command === 'Z' || prev . command === 'z' ) return false ;
879
836
}
880
837
if (
@@ -890,14 +847,14 @@ function filters(
890
847
891
848
if ( command === 'q' ) {
892
849
// @ts -ignore
893
- qControlPoint = [ data [ 0 ] + item . base [ 0 ] , data [ 1 ] + item . base [ 1 ] ] ;
850
+ prevQControlPoint = [ data [ 0 ] + item . base [ 0 ] , data [ 1 ] + item . base [ 1 ] ] ;
894
851
} else if ( command === 't' ) {
895
- if ( qPoint ) {
852
+ if ( qControlPoint ) {
896
853
// @ts -ignore
897
- qControlPoint = reflectPoint ( qPoint , item . base ) ;
854
+ prevQControlPoint = reflectPoint ( qControlPoint , item . base ) ;
898
855
} else {
899
856
// @ts -ignore
900
- qControlPoint = item . coords ;
857
+ prevQControlPoint = item . coords ;
901
858
}
902
859
}
903
860
prev = item ;
@@ -971,8 +928,9 @@ function convertToMixed(path, params) {
971
928
prev . command . charCodeAt ( 0 ) > 96 &&
972
929
absoluteDataStr . length == relativeDataStr . length - 1 &&
973
930
( data [ 0 ] < 0 ||
974
- // @ts -ignore
975
- ( / ^ 0 \. / . test ( data [ 0 ] ) && prev . args [ prev . args . length - 1 ] % 1 ) )
931
+ ( Math . floor ( data [ 0 ] ) === 0 &&
932
+ ! Number . isInteger ( data [ 0 ] ) &&
933
+ prev . args [ prev . args . length - 1 ] % 1 ) )
976
934
) )
977
935
) {
978
936
// @ts -ignore
@@ -981,7 +939,6 @@ function convertToMixed(path, params) {
981
939
}
982
940
983
941
prev = item ;
984
-
985
942
return true ;
986
943
} ) ;
987
944
@@ -1098,7 +1055,6 @@ function round(data) {
1098
1055
*
1099
1056
* @type {(data: number[]) => boolean }
1100
1057
*/
1101
-
1102
1058
function isCurveStraightLine ( data ) {
1103
1059
// Get line equation a·x + b·y + c = 0 coefficients a, b (c = 0) by start and end points.
1104
1060
var i = data . length - 2 ,
@@ -1125,7 +1081,6 @@ function isCurveStraightLine(data) {
1125
1081
*/
1126
1082
function calculateSagitta ( data ) {
1127
1083
if ( data [ 3 ] === 1 ) return undefined ;
1128
-
1129
1084
const [ rx , ry ] = data ;
1130
1085
if ( Math . abs ( rx - ry ) > error ) return undefined ;
1131
1086
const chord = Math . sqrt ( data [ 5 ] ** 2 + data [ 6 ] ** 2 ) ;
@@ -1138,7 +1093,6 @@ function calculateSagitta(data) {
1138
1093
*
1139
1094
* @type {(item: PathDataItem, data: number[]) => PathDataItem }
1140
1095
*/
1141
-
1142
1096
function makeLonghand ( item , data ) {
1143
1097
switch ( item . command ) {
1144
1098
case 's' :
@@ -1160,20 +1114,19 @@ function makeLonghand(item, data) {
1160
1114
*
1161
1115
* @type {(point1: Point, point2: Point) => number }
1162
1116
*/
1163
-
1164
1117
function getDistance ( point1 , point2 ) {
1165
- return Math . hypot ( point1 [ 0 ] - point2 [ 0 ] , point1 [ 1 ] - point2 [ 1 ] ) ;
1118
+ return Math . sqrt ( ( point1 [ 0 ] - point2 [ 0 ] ) ** 2 + ( point1 [ 1 ] - point2 [ 1 ] ) ** 2 ) ;
1166
1119
}
1167
1120
1168
1121
/**
1169
- * Reflects point across another point
1122
+ * Reflects point across another point.
1170
1123
*
1171
- * @param {Point } input
1124
+ * @param {Point } controlPoint
1172
1125
* @param {Point } base
1173
1126
* @returns {Point }
1174
1127
*/
1175
- function reflectPoint ( input , base ) {
1176
- return [ 2 * base [ 0 ] - input [ 0 ] , 2 * base [ 1 ] - input [ 1 ] ] ;
1128
+ function reflectPoint ( controlPoint , base ) {
1129
+ return [ 2 * base [ 0 ] - controlPoint [ 0 ] , 2 * base [ 1 ] - controlPoint [ 1 ] ] ;
1177
1130
}
1178
1131
1179
1132
/**
@@ -1183,7 +1136,6 @@ function reflectPoint(input, base) {
1183
1136
*
1184
1137
* @type {(curve: number[], t: number) => Point }
1185
1138
*/
1186
-
1187
1139
function getCubicBezierPoint ( curve , t ) {
1188
1140
var sqrT = t * t ,
1189
1141
cubT = sqrT * t ,
@@ -1201,7 +1153,6 @@ function getCubicBezierPoint(curve, t) {
1201
1153
*
1202
1154
* @type {(curve: number[]) => undefined | Circle }
1203
1155
*/
1204
-
1205
1156
function findCircle ( curve ) {
1206
1157
var midPoint = getCubicBezierPoint ( curve , 1 / 2 ) ,
1207
1158
m1 = [ midPoint [ 0 ] / 2 , midPoint [ 1 ] / 2 ] ,
@@ -1242,7 +1193,6 @@ function findCircle(curve) {
1242
1193
*
1243
1194
* @type {(curve: number[], circle: Circle) => boolean }
1244
1195
*/
1245
-
1246
1196
function isArc ( curve , circle ) {
1247
1197
var tolerance = Math . min (
1248
1198
arcThreshold * error ,
@@ -1264,7 +1214,6 @@ function isArc(curve, circle) {
1264
1214
*
1265
1215
* @type {(curve: number[], circle: Circle) => boolean }
1266
1216
*/
1267
-
1268
1217
function isArcPrev ( curve , circle ) {
1269
1218
return isArc ( curve , {
1270
1219
center : [ circle . center [ 0 ] + curve [ 4 ] , circle . center [ 1 ] + curve [ 5 ] ] ,
@@ -1277,7 +1226,6 @@ function isArcPrev(curve, circle) {
1277
1226
1278
1227
* @type {(curve: number[], relCircle: Circle) => number }
1279
1228
*/
1280
-
1281
1229
function findArcAngle ( curve , relCircle ) {
1282
1230
var x1 = - relCircle . center [ 0 ] ,
1283
1231
y1 = - relCircle . center [ 1 ] ,
@@ -1294,7 +1242,6 @@ function findArcAngle(curve, relCircle) {
1294
1242
*
1295
1243
* @type {(params: InternalParams, pathData: PathDataItem[]) => string }
1296
1244
*/
1297
-
1298
1245
function data2Path ( params , pathData ) {
1299
1246
return pathData . reduce ( function ( pathString , item ) {
1300
1247
var strData = '' ;
0 commit comments