svg-path-simplify 0.4.4 → 0.4.6

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +9 -1
  3. package/dist/svg-path-simplify.esm.js +1649 -1205
  4. package/dist/svg-path-simplify.esm.min.js +2 -2
  5. package/dist/svg-path-simplify.js +1649 -1204
  6. package/dist/svg-path-simplify.min.js +2 -2
  7. package/dist/svg-path-simplify.pathdata.esm.js +301 -637
  8. package/dist/svg-path-simplify.pathdata.esm.min.js +2 -2
  9. package/dist/svg-path-simplify.poly.cjs +8 -9
  10. package/drawing.svg +62 -0
  11. package/index.html +13 -8
  12. package/package.json +1 -1
  13. package/src/index.js +3 -1
  14. package/src/pathData_get_intersections.js +777 -0
  15. package/src/pathData_offset.js +201 -0
  16. package/src/pathData_simplify_cubic.js +19 -21
  17. package/src/pathData_simplify_cubic_extrapolate.js +147 -8
  18. package/src/pathData_simplify_cubicsToArcs.js +65 -21
  19. package/src/pathSimplify-main.js +267 -64
  20. package/src/pathSimplify-presets.js +1 -1
  21. package/src/poly-fit-curve-schneider.js +171 -132
  22. package/src/poly-fit-curve-schneider_check_bulge.js +131 -0
  23. package/src/poly-offset.js +133 -0
  24. package/src/simplify_poly_RC.js +29 -7
  25. package/src/svg-getAttributes.js +1 -1
  26. package/src/svgii/geometry.js +130 -27
  27. package/src/svgii/geometry_bbox.js +1 -1
  28. package/src/svgii/geometry_length.js +1 -1
  29. package/src/svgii/pathData_analyze.js +6 -28
  30. package/src/svgii/pathData_convert.js +63 -15
  31. package/src/svgii/pathData_remove_collinear.js +32 -36
  32. package/src/svgii/pathData_reorder.js +3 -2
  33. package/src/svgii/pathData_simplify_refineExtremes.js +1 -3
  34. package/src/svgii/pathData_stringify.js +132 -6
  35. package/src/svgii/pathData_toPolygon.js +33 -23
  36. package/src/svgii/poly_analyze.js +272 -125
  37. package/src/svgii/poly_analyze_cleanup.js +311 -0
  38. package/src/svgii/poly_analyze_getTangents.js +125 -0
  39. package/src/svgii/poly_analyze_get_chunks.js +3 -1
  40. package/src/svgii/poly_normalize.js +21 -8
  41. package/src/svgii/poly_to_pathdata.js +477 -8
  42. package/src/svgii/rounding.js +35 -14
  43. package/src/svgii/svg_cleanup.js +44 -51
  44. package/src/svgii/svg_cleanup_convertPathLength.js +5 -0
  45. package/src/svgii/svg_cleanup_normalize_transforms.js +2 -2
  46. package/src/svgii/visualize.js +33 -2
  47. package/v/0.4.5/index.html +1040 -0
  48. package/v/0.4.5/svg-path-simplify.js +13227 -0
@@ -22,7 +22,7 @@ import { cleanupSVGAttributes, removeElAtts } from "./svg_cleanup_general_svg_at
22
22
  import { convertPathLengthAtt } from "./svg_cleanup_convertPathLength";
23
23
  import { removeGroupProps, ungroupElements } from "./svg_cleanup_ungroup";
24
24
  import { parseSvgCss } from "../css_parse";
25
- import { setNormalizedTransformsToEl } from "./svg_cleanup_normalize_transforms";
25
+ import { scaleProps, setNormalizedTransformsToEl } from "./svg_cleanup_normalize_transforms";
26
26
 
27
27
 
28
28
  export function cleanUpSVG(svgMarkup, {
@@ -212,6 +212,40 @@ export function cleanUpSVG(svgMarkup, {
212
212
  })
213
213
 
214
214
 
215
+ /**
216
+ * remove els and attributes
217
+ */
218
+
219
+ // remove meta
220
+ if (!allowMeta) removeElements.push('meta', 'metadata', 'desc', 'title')
221
+
222
+ if (removeClassNames) {
223
+ removeSVGAttributes.push('class');
224
+ removeElAttributes.push('class');
225
+ }
226
+
227
+ if (removeIds) {
228
+ removeSVGAttributes.push('id')
229
+ removeElAttributes.push('id')
230
+ }
231
+
232
+
233
+ // remove hidden elements
234
+ removeHiddenSvgEls(svg)
235
+
236
+ // remove SVG elements
237
+ removeSvgEls(svg, { removeElements, removeNameSpaced });
238
+
239
+ // remove SVG attributes
240
+ removeSvgAtts(svg, removeSVGAttributes);
241
+
242
+ // remove SVG child element attributes
243
+ removeSvgChildAtts(svg, removeElAttributes);
244
+
245
+
246
+ // general cleanup
247
+ if (cleanupSVGAtts) cleanupSVGAttributes(svg, { removeIds, removeClassNames, removeDimensions, stylesToAttributes, allowMeta, allowAriaAtts, allowDataAtts });
248
+
215
249
 
216
250
  // collect all elements' properties
217
251
  let svgElProps = []
@@ -234,10 +268,13 @@ export function cleanUpSVG(svgMarkup, {
234
268
  */
235
269
  let styleProps = parseStylesProperties(el, propOptions)
236
270
  let stylePropsFiltered = {}
237
- //console.log('styleProps', name, styleProps);
238
271
 
272
+ // reset remove array
273
+ remove = [];
239
274
 
240
275
  // convert pathLength before transforming
276
+ if (convertTransforms || attributesToGroup) convertPathLength = true;
277
+
241
278
  if (convertPathLength) {
242
279
  styleProps = convertPathLengthAtt(el, { styleProps });
243
280
  remove = [...new Set([...remove, ...styleProps.remove])];
@@ -250,7 +287,6 @@ export function cleanUpSVG(svgMarkup, {
250
287
  let transFormInherited = []
251
288
 
252
289
 
253
-
254
290
  /**
255
291
  * consolidate all properties:
256
292
  * merge with inherited transforms
@@ -283,7 +319,6 @@ export function cleanUpSVG(svgMarkup, {
283
319
  ...stylePropsSVG,
284
320
  ...inheritedProps,
285
321
  };
286
-
287
322
  //console.log('inheritedProps', inheritedProps);
288
323
 
289
324
  // merge with svg props
@@ -299,46 +334,11 @@ export function cleanUpSVG(svgMarkup, {
299
334
  //console.log('styleProps', styleProps);
300
335
  remove = [...new Set([...remove, ...styleProps.remove])];
301
336
 
302
-
303
- /**
304
- * remove els and attributes
305
- */
306
-
307
- // remove meta
308
- if (!allowMeta) removeElements.push('meta', 'metadata', 'desc', 'title')
309
-
310
- if (removeClassNames) {
311
- removeSVGAttributes.push('class');
312
- removeElAttributes.push('class');
313
- }
314
-
315
- if (removeIds) {
316
- removeSVGAttributes.push('id')
317
- removeElAttributes.push('id')
318
- }
319
-
320
-
321
- // remove hidden elements
322
- removeHiddenSvgEls(svg)
323
-
324
- // remove SVG elements
325
- removeSvgEls(svg, { removeElements, removeNameSpaced });
326
-
327
- // remove SVG attributes
328
- removeSvgAtts(svg, removeSVGAttributes);
329
-
330
- // remove SVG child element attributes
331
- removeSvgChildAtts(svg, removeElAttributes);
332
-
333
-
334
- // general cleanup
335
- if (cleanupSVGAtts) cleanupSVGAttributes(svg, { removeIds, removeClassNames, removeDimensions, stylesToAttributes, allowMeta, allowAriaAtts, allowDataAtts });
337
+
336
338
 
337
339
  // all relative units to absolute
338
340
  if (toAbsoluteUnits) {
339
341
  normalizeTransforms = true;
340
- //stylesToAttributes = true
341
- //console.log(name, styleProps);
342
342
 
343
343
  /**
344
344
  * apply consolidated
@@ -376,6 +376,7 @@ export function cleanUpSVG(svgMarkup, {
376
376
 
377
377
 
378
378
 
379
+
379
380
  if (stylesToAttributes) {
380
381
 
381
382
  /**
@@ -385,7 +386,6 @@ export function cleanUpSVG(svgMarkup, {
385
386
  styleProps = setNormalizedTransformsToEl(el, { styleProps });
386
387
  //remove = styleProps.remove;
387
388
  remove = [...new Set([...remove, ...styleProps.remove])];
388
-
389
389
  }
390
390
 
391
391
  /**
@@ -396,6 +396,7 @@ export function cleanUpSVG(svgMarkup, {
396
396
  stylePropsFiltered = filterSvgElProps(name, styleProps,
397
397
  { removeDefaults: true, cleanUpStrokes, allowMeta, allowAriaAtts, allowDataAtts, removeIds, inheritedProps });
398
398
 
399
+
399
400
  remove = [...new Set([...remove, ...stylePropsFiltered.remove])];
400
401
 
401
402
  for (let prop in stylePropsFiltered.propsFiltered) {
@@ -411,14 +412,6 @@ export function cleanUpSVG(svgMarkup, {
411
412
  */
412
413
  removeAtts(el, remove)
413
414
 
414
- /*
415
- for (let i = 0; i < remove.length; i++) {
416
- let att = remove[i];
417
- el.removeAttribute(att)
418
- }
419
- */
420
-
421
-
422
415
  } // endof style processing
423
416
 
424
417
 
@@ -442,7 +435,7 @@ export function cleanUpSVG(svgMarkup, {
442
435
 
443
436
  // scale props like stroke width or dash-array before conversion
444
437
  if (matrix && transComponents) {
445
- ['stroke-width', 'stroke-dasharray'].forEach(att => {
438
+ ['stroke-width', 'stroke-dasharray', 'stroke-dashoffset'].forEach(att => {
446
439
  let attVal = el.getAttribute(att)
447
440
  let vals = attVal ? attVal.split(' ').filter(Boolean).map(Number).map(val => val * transComponents.scaleX) : []
448
441
  if (vals.length) el.setAttribute(att, vals.join(' '))
@@ -767,7 +760,7 @@ function removeOffCanvasEls(svg, { x = 0, y = 0, width = 0, height = 0 } = {}) {
767
760
  els.forEach(el => {
768
761
  //console.log(el);
769
762
  let bb = getElBBox(el)
770
- //console.log('!!bb', bb);
763
+ //console.log('!!bb', bb, el);
771
764
  let outside = bb.right < bb0.x || bb.bottom < bb0.y || bb.x > bb0.right || bb.y > bb.bottom
772
765
  if (outside) el.remove();
773
766
  })
@@ -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
 
@@ -56,7 +56,7 @@ export function setNormalizedTransformsToEl(el, {
56
56
  styleProps.remove.push('transform')
57
57
 
58
58
  // scale props like stroke width or dash-array
59
- styleProps = scaleProps(styleProps, { props: ['stroke-width', 'stroke-dasharray'], scale: scaleX })
59
+ styleProps = scaleProps(styleProps, { props: ['stroke-width', 'stroke-dasharray', 'stroke-dashoffset'], scale: scaleX })
60
60
 
61
61
  } else {
62
62
  el.setAttribute('transform', transComponents.matrixAtt)
@@ -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
@@ -233,6 +233,36 @@ export function renderPoint(
233
233
  }
234
234
 
235
235
 
236
+ export function renderCircle(
237
+ svg,
238
+ coords,
239
+ stroke = "red",
240
+ r = "1%",
241
+ opacity = "1",
242
+ strokeWidth="1%",
243
+ title = '',
244
+ render = true,
245
+ id = "",
246
+ className = ""
247
+ ) {
248
+ if (Array.isArray(coords)) {
249
+ coords = {
250
+ x: coords[0],
251
+ y: coords[1]
252
+ };
253
+ }
254
+ let marker = `<circle class="${className}" opacity="${opacity}" id="${id}" cx="${coords.x}" cy="${coords.y}" r="${r}" stroke="${stroke}" stroke-width="${strokeWidth}">
255
+ <title>${title}</title></circle>`;
256
+
257
+ if (render) {
258
+ svg.insertAdjacentHTML("beforeend", marker);
259
+ } else {
260
+ return marker;
261
+ }
262
+ }
263
+
264
+
265
+
236
266
  export function renderPath(svg, d = '', stroke = 'green', strokeWidth = '1%', opacity="1", render = true) {
237
267
 
238
268
  let path = `<path d="${d}" fill="none" stroke="${stroke}" stroke-width="${strokeWidth}" stroke-opacity="${opacity}" /> `;
@@ -250,7 +280,7 @@ export function renderPath(svg, d = '', stroke = 'green', strokeWidth = '1%', op
250
280
 
251
281
 
252
282
  // debug helper: render lines
253
- export function renderPoly(svg, pts, stroke = "purple", strokeWidth = "1%", fillOpacity="0.5", fill="none", polygon=true , render = true) {
283
+ export function renderPoly(svg, pts, stroke = "purple", strokeWidth = "1%", fillOpacity="0.5", fill="none", polygon=false , render = true) {
254
284
  pts = pts.map(pt => { return [pt.x, pt.y] }).flat().join(' ');
255
285
 
256
286
 
@@ -280,7 +310,7 @@ export function renderPerpendicularLine(pt, len = 10, angle) {
280
310
  }
281
311
 
282
312
 
283
-
313
+ /*
284
314
  export function addMarkers() {
285
315
 
286
316
 
@@ -310,6 +340,7 @@ export function addMarkers() {
310
340
  document.body.insertAdjacentHTML('afterbegin', style + markerMarkup)
311
341
 
312
342
  }
343
+ */
313
344
 
314
345
 
315
346
  /**