svg-path-simplify 0.4.4 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1855,7 +1855,7 @@
1855
1855
 
1856
1856
  let len = pathData.length;
1857
1857
  let decimals = decimalsGlobal;
1858
- let decimalsArc = decimals < 3 ? decimals+2 : decimals;
1858
+ let decimalsArc = decimals < 3 ? decimals + 2 : decimals;
1859
1859
 
1860
1860
  for (let c = 0; c < len; c++) {
1861
1861
  let com = pathData[c];
@@ -1915,23 +1915,47 @@
1915
1915
 
1916
1916
  dimA = dimA ? dimA : getDistManhattan(p0, p);
1917
1917
 
1918
- if (dimA) dims.push(dimA);
1918
+ if (dimA) dims.push(+dimA.toFixed(8));
1919
1919
 
1920
1920
  }
1921
1921
 
1922
1922
  }
1923
1923
 
1924
- let dim_min = dims.sort();
1924
+ dims = dims.sort();
1925
+ let len = dims.length;
1926
+ let dim_mid = dims[Math.floor(len*0.5)];
1927
+
1928
+ // smallest 25% of values
1929
+ let idx_q = Math.ceil(len*0.25);
1930
+ let dims_min = dims.slice(0, idx_q);
1931
+
1932
+ // average smallest values with mid value
1933
+ let dim_min = ((dims_min.reduce((a, b) => a + b, 0) / idx_q) + dim_mid) * 0.5;
1934
+
1935
+ let threshold = 75;
1936
+ let decimalsAuto = dim_min > threshold * 1.5 ? 0 : Math.floor(threshold / dim_min).toString().length;
1937
+
1938
+ // clamp
1939
+ return Math.min(Math.max(0, decimalsAuto), 8)
1940
+
1941
+ /*
1942
+ let dim_min = dims.sort()
1925
1943
 
1926
- let sliceIdx = Math.ceil(dim_min.length / 6);
1944
+ let dim_mid = dim_min[Math.floor(dim_min.length*0.5)]
1945
+
1946
+ let sliceIdx = Math.ceil(dim_min.length / 4);
1927
1947
  dim_min = dim_min.slice(0, sliceIdx);
1928
1948
  let minVal = dim_min.reduce((a, b) => a + b, 0) / sliceIdx;
1929
1949
 
1930
- let threshold = 75;
1931
- let decimalsAuto = minVal > threshold * 1.5 ? 0 : Math.floor(threshold / minVal).toString().length;
1950
+ // average with mid value
1951
+ minVal = (minVal+dim_mid)*0.5
1952
+
1953
+ let threshold = 75
1954
+ let decimalsAuto = minVal > threshold * 1.5 ? 0 : Math.floor(threshold / minVal).toString().length
1932
1955
 
1933
1956
  // clamp
1934
1957
  return Math.min(Math.max(0, decimalsAuto), 8)
1958
+ */
1935
1959
 
1936
1960
  }
1937
1961
 
@@ -2497,7 +2521,7 @@
2497
2521
  attributes.forEach(att=>{
2498
2522
 
2499
2523
  let value = normalizeUnits(el.getAttribute(att), {x, y, width, height});
2500
- atts[att.name] = value;
2524
+ atts[att] = value;
2501
2525
  });
2502
2526
 
2503
2527
  return atts
@@ -3242,7 +3266,7 @@
3242
3266
  error += com.error;
3243
3267
 
3244
3268
  // find next candidates
3245
- for (let n = i + 1; error < tolerance && n < l; n++) {
3269
+ for (let n = i + offset; error < tolerance && n < l; n++) {
3246
3270
  let comN = pathData[n];
3247
3271
 
3248
3272
  if (comN.type !== 'C' ||
@@ -3252,6 +3276,7 @@
3252
3276
  (keepExtremes && com.extreme)
3253
3277
  )
3254
3278
  ) {
3279
+
3255
3280
  break
3256
3281
  }
3257
3282
 
@@ -3259,6 +3284,7 @@
3259
3284
 
3260
3285
  // failure - could not be combined - exit loop
3261
3286
  if (combined.length > 1) {
3287
+
3262
3288
  break
3263
3289
  }
3264
3290
 
@@ -3272,6 +3298,7 @@
3272
3298
 
3273
3299
  // return combined
3274
3300
  com = combined[0];
3301
+
3275
3302
  }
3276
3303
 
3277
3304
  pathDataN.push(com);
@@ -3385,11 +3412,19 @@
3385
3412
 
3386
3413
  comS.dimA = getDistManhattan(comS.p0, comS.p);
3387
3414
  comS.type = 'C';
3415
+
3388
3416
  comS.extreme = com2.extreme;
3389
3417
  comS.directionChange = com2.directionChange;
3390
-
3391
3418
  comS.corner = com2.corner;
3392
3419
 
3420
+ if (comS.extreme || comS.corner) ;
3421
+
3422
+ /*
3423
+ comS.extreme = com1.extreme;
3424
+ comS.directionChange = com1.directionChange;
3425
+ comS.corner = com1.corner;
3426
+ */
3427
+
3393
3428
  comS.values = [comS.cp1.x, comS.cp1.y, comS.cp2.x, comS.cp2.y, comS.p.x, comS.p.y];
3394
3429
 
3395
3430
  // relative error
@@ -6558,7 +6593,8 @@
6558
6593
  let { type, values } = com;
6559
6594
  let valsLen = values.length;
6560
6595
  if (valsLen) {
6561
- let p = { type: type, x: values[valsLen - 2], y: values[valsLen - 1], index: 0 };
6596
+ // we need rounding otherwise sorting may crash due to e notation
6597
+ let p = { type: type, x: +values[valsLen - 2].toFixed(8), y: +values[valsLen - 1].toFixed(8), index: 0 };
6562
6598
  p.index = i;
6563
6599
  indices.push(p);
6564
6600
  }
@@ -6566,7 +6602,7 @@
6566
6602
 
6567
6603
  // reorder to top left most
6568
6604
 
6569
- indices = indices.sort((a, b) => +a.y.toFixed(8) - +b.y.toFixed(8) || a.x - b.x);
6605
+ indices = indices.sort((a, b) => a.y - b.y || a.x - b.x);
6570
6606
  newIndex = indices[0].index;
6571
6607
 
6572
6608
  return newIndex ? shiftSvgStartingPoint(pathData, newIndex) : pathData;
@@ -7102,7 +7138,7 @@
7102
7138
  } = {}) {
7103
7139
 
7104
7140
  // is stringified flat point attribute
7105
- if(typeof pts === 'string' && !isNaN(pts[0])){
7141
+ if (typeof pts === 'string' && !isNaN(pts[0])) {
7106
7142
  pts = toPointArray(pts.split(/,| /).filter(Boolean).map(Number));
7107
7143
  return pts
7108
7144
  }
@@ -7112,8 +7148,9 @@
7112
7148
  return poly
7113
7149
  }
7114
7150
 
7115
- function polyArrayToObject(pts) {
7151
+ function polyArrayToObject(pts = []) {
7116
7152
 
7153
+ if (!pts.length) return [];
7117
7154
  // is point object array
7118
7155
  if (pts[0].x !== undefined && pts[0].y !== undefined) return pts
7119
7156
 
@@ -7131,7 +7168,7 @@
7131
7168
  return poly
7132
7169
  }
7133
7170
 
7134
- else if(pts.length>3){
7171
+ else if (pts.length > 3) {
7135
7172
  pts = toPointArray(pts);
7136
7173
  return pts
7137
7174
  }
@@ -7160,13 +7197,13 @@
7160
7197
  function toPointArray(pts) {
7161
7198
  let ptArr = [];
7162
7199
 
7163
- if(pts[0].length===2){
7164
- for (let i = 0, l = pts.length; i < l; i ++) {
7200
+ if (pts[0].length === 2) {
7201
+ for (let i = 0, l = pts.length; i < l; i++) {
7165
7202
  let pt = pts[i];
7166
- ptArr.push({ x: pt[0], y:pt[1] });
7203
+ ptArr.push({ x: pt[0], y: pt[1] });
7167
7204
  }
7168
7205
 
7169
- }else {
7206
+ } else {
7170
7207
  for (let i = 1, l = pts.length; i < l; i += 2) {
7171
7208
  ptArr.push({ x: pts[i - 1], y: pts[i] });
7172
7209
  }
@@ -8008,7 +8045,6 @@
8008
8045
 
8009
8046
  // LG weight/abscissae generator
8010
8047
  function getLegendreGaussValues(n, x1 = -1, x2 = 1) {
8011
- console.log('add new LG', n);
8012
8048
 
8013
8049
  let waArr = [];
8014
8050
  let z1, z, xm, xl, pp, p3, p2, p1;
@@ -8485,7 +8521,7 @@
8485
8521
  let prop = props[i];
8486
8522
 
8487
8523
  if (styleProps[prop] !== undefined) {
8488
- styleProps[prop] = styleProps[prop].map(val => round ? roundTo(val * scale, 2) : val * scale);
8524
+ styleProps[prop] = styleProps[prop].map(val => round ? roundTo(val * scale, 3) : val * scale);
8489
8525
  }
8490
8526
  }
8491
8527
  return styleProps
@@ -8506,6 +8542,7 @@
8506
8542
  });
8507
8543
 
8508
8544
  let scale = elLength / pathLength;
8545
+
8509
8546
  styleProps = scaleProps(styleProps, { props: ['stroke-dasharray', 'stroke-dashoffset'], scale });
8510
8547
 
8511
8548
  // set absolute
@@ -9034,9 +9071,13 @@
9034
9071
  let stylePropsFiltered = {};
9035
9072
 
9036
9073
  // convert pathLength before transforming
9037
- if (convertPathLength) {
9074
+ if(convertTransforms || attributesToGroup) convertPathLength=true;
9075
+
9076
+ if (convertPathLength ) {
9077
+
9038
9078
  styleProps = convertPathLengthAtt(el, { styleProps });
9039
9079
  remove = [...new Set([...remove, ...styleProps.remove])];
9080
+
9040
9081
  }
9041
9082
 
9042
9083
  // get parent styles
@@ -9214,7 +9255,7 @@
9214
9255
 
9215
9256
  // scale props like stroke width or dash-array before conversion
9216
9257
  if (matrix && transComponents) {
9217
- ['stroke-width', 'stroke-dasharray'].forEach(att => {
9258
+ ['stroke-width', 'stroke-dasharray', 'stroke-dashoffset'].forEach(att => {
9218
9259
  let attVal = el.getAttribute(att);
9219
9260
  let vals = attVal ? attVal.split(' ').filter(Boolean).map(Number).map(val => val * transComponents.scaleX) : [];
9220
9261
  if (vals.length) el.setAttribute(att, vals.join(' '));
@@ -12345,7 +12386,6 @@
12345
12386
  addViewBox: true,
12346
12387
  removeDimensions: true,
12347
12388
  removeOffCanvas: true,
12348
-
12349
12389
  /*
12350
12390
  */
12351
12391
  }
@@ -12532,11 +12572,11 @@
12532
12572
  original: 0,
12533
12573
  new: 0,
12534
12574
  saved: 0,
12535
- svgSize:0,
12536
- svgSizeOpt:0,
12537
- compression:0,
12538
- decimals:0,
12539
- invalid:true
12575
+ svgSize: 0,
12576
+ svgSizeOpt: 0,
12577
+ compression: 0,
12578
+ decimals: 0,
12579
+ invalid: true
12540
12580
  };
12541
12581
 
12542
12582
  return { svg: dummySVG, d: '', polys: [], report, pathDataPlusArr: [], pathDataPlusArr_global: [], inputType: 'invalid', dOriginal: '' };
@@ -12706,6 +12746,12 @@
12706
12746
  let { d, el } = path;
12707
12747
  let isPoly = false;
12708
12748
 
12749
+ // disable reordering for elements with stroke dash-array
12750
+ if (el && (el.hasAttribute('stroke-dasharray') || el.hasAttribute('stroke-dashoffset'))) {
12751
+ optimizeOrder = false;
12752
+
12753
+ }
12754
+
12709
12755
  // if polygon we already heave absolute coordinates
12710
12756
 
12711
12757
  let pathData = parsePathDataNormalized(d, { quadraticToCubic, arcToCubic });