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.
@@ -5,7 +5,7 @@ export function normalizePoly(pts, {
5
5
  } = {}) {
6
6
 
7
7
  // is stringified flat point attribute
8
- if(typeof pts === 'string' && !isNaN(pts[0])){
8
+ if (typeof pts === 'string' && !isNaN(pts[0])) {
9
9
  pts = toPointArray(pts.split(/,| /).filter(Boolean).map(Number));
10
10
  return pts
11
11
  }
@@ -16,8 +16,9 @@ export function normalizePoly(pts, {
16
16
  }
17
17
 
18
18
 
19
- export function polyArrayToObject(pts) {
20
-
19
+ export function polyArrayToObject(pts = []) {
20
+ //console.log(pts);
21
+ if (!pts.length) return [];
21
22
  // is point object array
22
23
  if (pts[0].x !== undefined && pts[0].y !== undefined) return pts
23
24
 
@@ -35,7 +36,7 @@ export function polyArrayToObject(pts) {
35
36
  return poly
36
37
  }
37
38
 
38
- else if(pts.length>3){
39
+ else if (pts.length > 3) {
39
40
  pts = toPointArray(pts)
40
41
  return pts
41
42
  }
@@ -65,13 +66,13 @@ export function polyPtsToArray(pts) {
65
66
  export function toPointArray(pts) {
66
67
  let ptArr = [];
67
68
 
68
- if(pts[0].length===2){
69
- for (let i = 0, l = pts.length; i < l; i ++) {
69
+ if (pts[0].length === 2) {
70
+ for (let i = 0, l = pts.length; i < l; i++) {
70
71
  let pt = pts[i]
71
- ptArr.push({ x: pt[0], y:pt[1] });
72
+ ptArr.push({ x: pt[0], y: pt[1] });
72
73
  }
73
74
 
74
- }else{
75
+ } else {
75
76
  for (let i = 1, l = pts.length; i < l; i += 2) {
76
77
  ptArr.push({ x: pts[i - 1], y: pts[i] });
77
78
  }
@@ -19,7 +19,7 @@ export function roundPathData(pathData, decimalsGlobal = -1) {
19
19
 
20
20
  let len = pathData.length;
21
21
  let decimals = decimalsGlobal
22
- let decimalsArc = decimals < 3 ? decimals+2 : decimals
22
+ let decimalsArc = decimals < 3 ? decimals + 2 : decimals
23
23
  //decimalsArc = decimals
24
24
  //console.log(decimalsArc);
25
25
 
@@ -98,25 +98,56 @@ export function detectAccuracy(pathData) {
98
98
  //let dimA = +getDistAv(p0, p).toFixed(8)
99
99
  //console.log('dimA', dimA, com.dimA, type);
100
100
 
101
- if (dimA) dims.push(dimA);
101
+ if (dimA) dims.push(+dimA.toFixed(8));
102
+ //if (dimA) dims.push(dimA);
102
103
  if (dimA && dimA < minDim) minDim = dimA;
103
104
  //if (dimA && dimA > maxDim) maxDim = dimA;
104
105
  }
105
106
 
106
107
  }
107
108
 
109
+
110
+ dims = dims.sort()
111
+ let len = dims.length;
112
+ let dim_mid = dims[Math.floor(len*0.5)]
113
+
114
+ // smallest 25% of values
115
+ let idx_q = Math.ceil(len*0.25);
116
+ let dims_min = dims.slice(0, idx_q);
117
+
118
+ // average smallest values with mid value
119
+ let dim_min = ((dims_min.reduce((a, b) => a + b, 0) / idx_q) + dim_mid) * 0.5;
120
+
121
+
122
+ let threshold = 75
123
+ let decimalsAuto = dim_min > threshold * 1.5 ? 0 : Math.floor(threshold / dim_min).toString().length
124
+
125
+ // clamp
126
+ return Math.min(Math.max(0, decimalsAuto), 8)
127
+
128
+
129
+
130
+ /*
108
131
  let dim_min = dims.sort()
109
132
  //console.log('dim_min', dim_min);
110
133
 
111
- let sliceIdx = Math.ceil(dim_min.length / 6);
134
+ let dim_mid = dim_min[Math.floor(dim_min.length*0.5)]
135
+
136
+ let sliceIdx = Math.ceil(dim_min.length / 4);
112
137
  dim_min = dim_min.slice(0, sliceIdx);
113
138
  let minVal = dim_min.reduce((a, b) => a + b, 0) / sliceIdx;
114
139
 
140
+ // average with mid value
141
+ minVal = (minVal+dim_mid)*0.5
142
+ //console.log('minVal', minVal, dim_mid);
143
+
144
+
115
145
  let threshold = 75
116
146
  let decimalsAuto = minVal > threshold * 1.5 ? 0 : Math.floor(threshold / minVal).toString().length
117
147
 
118
148
  // clamp
119
149
  return Math.min(Math.max(0, decimalsAuto), 8)
150
+ */
120
151
 
121
152
  }
122
153
 
@@ -238,11 +238,16 @@ export function cleanUpSVG(svgMarkup, {
238
238
 
239
239
 
240
240
  // convert pathLength before transforming
241
- if (convertPathLength) {
241
+ if(convertTransforms || attributesToGroup) convertPathLength=true;
242
+
243
+ if (convertPathLength ) {
244
+ //console.log('convertPathLength', convertPathLength, name);
242
245
  styleProps = convertPathLengthAtt(el, { styleProps });
243
246
  remove = [...new Set([...remove, ...styleProps.remove])];
247
+
244
248
  }
245
249
 
250
+ //console.log(styleProps);
246
251
 
247
252
  // get parent styles
248
253
  let { parentStyleProps = [] } = el;
@@ -442,7 +447,7 @@ export function cleanUpSVG(svgMarkup, {
442
447
 
443
448
  // scale props like stroke width or dash-array before conversion
444
449
  if (matrix && transComponents) {
445
- ['stroke-width', 'stroke-dasharray'].forEach(att => {
450
+ ['stroke-width', 'stroke-dasharray', 'stroke-dashoffset'].forEach(att => {
446
451
  let attVal = el.getAttribute(att)
447
452
  let vals = attVal ? attVal.split(' ').filter(Boolean).map(Number).map(val => val * transComponents.scaleX) : []
448
453
  if (vals.length) el.setAttribute(att, vals.join(' '))
@@ -767,7 +772,7 @@ function removeOffCanvasEls(svg, { x = 0, y = 0, width = 0, height = 0 } = {}) {
767
772
  els.forEach(el => {
768
773
  //console.log(el);
769
774
  let bb = getElBBox(el)
770
- //console.log('!!bb', bb);
775
+ //console.log('!!bb', bb, el);
771
776
  let outside = bb.right < bb0.x || bb.bottom < bb0.y || bb.x > bb0.right || bb.y > bb.bottom
772
777
  if (outside) el.remove();
773
778
  })
@@ -16,7 +16,10 @@ export function convertPathLengthAtt(el, {
16
16
  props: styleProps
17
17
  })
18
18
 
19
+
19
20
  let scale = elLength / pathLength
21
+ //console.log('elLength', elLength, scale);
22
+
20
23
  styleProps = scaleProps(styleProps, { props: ['stroke-dasharray', 'stroke-dashoffset'], scale })
21
24
 
22
25
  // set absolute
@@ -33,6 +36,8 @@ export function convertPathLengthAtt(el, {
33
36
 
34
37
  }
35
38
 
39
+ //console.log('pathLength', pathLength);
40
+ //console.log('styleProps', styleProps );
36
41
  return styleProps;
37
42
 
38
43
 
@@ -76,7 +76,7 @@ export function scaleProps(styleProps = {}, { props = [], scale = 1 } = {}, roun
76
76
  let prop = props[i];
77
77
 
78
78
  if (styleProps[prop] !== undefined) {
79
- styleProps[prop] = styleProps[prop].map(val => round ? roundTo(val * scale, 2) : val * scale)
79
+ styleProps[prop] = styleProps[prop].map(val => round ? roundTo(val * scale, 3) : val * scale)
80
80
  }
81
81
  }
82
82
  return styleProps