svg-path-simplify 0.0.2 → 0.0.4

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.
@@ -8,25 +8,27 @@ export function pathDataToD(pathData, optimize = 0) {
8
8
 
9
9
  optimize = parseFloat(optimize)
10
10
 
11
+
12
+
13
+ let len = pathData.length;
11
14
  let beautify = optimize > 1;
12
15
  let minify = beautify || optimize ? false : true;
13
16
 
14
17
  // Convert first "M" to "m" if followed by "l" (when minified)
18
+ /*
15
19
  if (pathData[1].type === "l" && minify) {
16
20
  pathData[0].type = "m";
17
21
  }
22
+ */
18
23
 
19
24
  let d = '';
20
- let suff = beautify ? `\n` : ' ';
25
+ let separator_command = beautify ? `\n` : (minify ? '' : ' ');
26
+ let separator_type = !minify ? ' ' : '';
21
27
 
28
+ d = `${pathData[0].type}${separator_type}${pathData[0].values.join(" ")}${separator_command}`;
22
29
 
23
- if (minify) {
24
- d = `${pathData[0].type} ${pathData[0].values.join(" ")}`;
25
- } else {
26
- d = `${pathData[0].type} ${pathData[0].values.join(" ")}${suff}`;
27
- }
28
30
 
29
- for (let i = 1, len = pathData.length; i < len; i++) {
31
+ for (let i = 1; i < len; i++) {
30
32
  let com0 = pathData[i - 1];
31
33
  let com = pathData[i];
32
34
  let { type, values } = com;
@@ -44,8 +46,6 @@ export function pathDataToD(pathData, optimize = 0) {
44
46
  type = (com0.type === com.type && com.type.toLowerCase() !== 'm' && minify)
45
47
  ? " "
46
48
  : (
47
- (com0.type === "m" && com.type === "l") ||
48
- (com0.type === "M" && com.type === "l") ||
49
49
  (com0.type === "M" && com.type === "L")
50
50
  ) && minify
51
51
  ? " "
@@ -79,17 +79,16 @@ export function pathDataToD(pathData, optimize = 0) {
79
79
  //console.log(isSmallFloat, prevWasFloat, valStr);
80
80
 
81
81
  valsString += valStr
82
- //.replace(/-0./g, '-.').replace(/ -./g, '-.')
83
82
  prevWasFloat = isSmallFloat;
84
83
  }
85
84
 
86
85
  //console.log('minify', valsString);
87
- d += `${type}${valsString}`;
86
+ d += `${type}${separator_type}${valsString}${separator_command}`;
88
87
 
89
88
  }
90
89
  // regular non-minified output
91
90
  else {
92
- d += `${type} ${values.join(' ')}${suff}`;
91
+ d += `${type}${separator_type}${values.join(' ')}${separator_command}`;
93
92
  }
94
93
  }
95
94
 
@@ -21,6 +21,8 @@ export function detectAccuracy(pathData) {
21
21
 
22
22
  //console.log('detectAccuracy');
23
23
 
24
+ let dims = new Set();
25
+
24
26
  // add average distances
25
27
  for (let i = 0, len = pathData.length; i < len; i++) {
26
28
  let com = pathData[i];
@@ -34,8 +36,10 @@ export function detectAccuracy(pathData) {
34
36
  //let dimA = +getDistAv(p0, p).toFixed(8)
35
37
  //console.log('dimA', dimA, com.dimA, type);
36
38
 
39
+ if(dimA) dims.add(dimA);
40
+
37
41
  if(dimA && dimA<minDim) minDim = dimA;
38
- //if(dimA && dimA>maxDim) maxDim = dimA;
42
+ if(dimA && dimA>maxDim) maxDim = dimA;
39
43
 
40
44
 
41
45
  if(type==='M'){
@@ -44,9 +48,15 @@ export function detectAccuracy(pathData) {
44
48
  p0 = p;
45
49
  }
46
50
 
47
- //minDim = +minDim.toFixed(8)
48
- let decimalsAuto = Math.floor(50 / minDim).toString().length
49
- //console.log('!!!minDim', minDim, 'maxDim', maxDim, decimalsAuto);
51
+
52
+ let dim_min = Array.from(dims).sort()
53
+ let sliceIdx = Math.ceil(dim_min.length/8);
54
+ dim_min = dim_min.slice(0, sliceIdx );
55
+
56
+ let dimVal = dim_min.reduce((a,b)=>a+b, 0) / sliceIdx;
57
+
58
+ let threshold = 50
59
+ let decimalsAuto = dimVal > threshold ? 0 : Math.floor(threshold / dimVal).toString().length
50
60
 
51
61
  // clamp
52
62
  return Math.min(Math.max(0, decimalsAuto), 8)
@@ -55,8 +65,6 @@ export function detectAccuracy(pathData) {
55
65
 
56
66
 
57
67
 
58
-
59
-
60
68
  export function detectAccuracy_back(pathData) {
61
69
 
62
70
  // Reference first MoveTo command (M)
@@ -1,4 +1,8 @@
1
- export function cleanUpSVG(svgMarkup, removeHidden=true) {
1
+ export function cleanUpSVG(svgMarkup, {
2
+ returnDom=false,
3
+ removeHidden=true,
4
+ removeUnused=true,
5
+ }={}) {
2
6
  svgMarkup = cleanSvgPrologue(svgMarkup);
3
7
 
4
8
  // replace namespaced refs
@@ -29,6 +33,8 @@ export function cleanUpSVG(svgMarkup, removeHidden=true) {
29
33
  }
30
34
  })
31
35
 
36
+ if(returnDom) return svg
37
+
32
38
  let markup = stringifySVG(svg)
33
39
  console.log(markup);
34
40