svg-path-simplify 0.4.5 → 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 (44) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +8 -0
  3. package/dist/svg-path-simplify.esm.js +1617 -1219
  4. package/dist/svg-path-simplify.esm.min.js +2 -2
  5. package/dist/svg-path-simplify.js +1617 -1218
  6. package/dist/svg-path-simplify.min.js +2 -2
  7. package/dist/svg-path-simplify.pathdata.esm.js +293 -665
  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 +11 -5
  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 +14 -34
  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 +256 -59
  20. package/src/pathSimplify-presets.js +1 -0
  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/svgii/geometry.js +130 -27
  26. package/src/svgii/geometry_bbox.js +1 -1
  27. package/src/svgii/pathData_analyze.js +6 -28
  28. package/src/svgii/pathData_convert.js +63 -15
  29. package/src/svgii/pathData_remove_collinear.js +32 -36
  30. package/src/svgii/pathData_simplify_refineExtremes.js +1 -3
  31. package/src/svgii/pathData_stringify.js +132 -6
  32. package/src/svgii/pathData_toPolygon.js +33 -23
  33. package/src/svgii/poly_analyze.js +272 -125
  34. package/src/svgii/poly_analyze_cleanup.js +311 -0
  35. package/src/svgii/poly_analyze_getTangents.js +125 -0
  36. package/src/svgii/poly_analyze_get_chunks.js +3 -1
  37. package/src/svgii/poly_normalize.js +12 -0
  38. package/src/svgii/poly_to_pathdata.js +477 -8
  39. package/src/svgii/rounding.js +29 -39
  40. package/src/svgii/svg_cleanup.js +42 -54
  41. package/src/svgii/svg_cleanup_normalize_transforms.js +1 -1
  42. package/src/svgii/visualize.js +33 -2
  43. package/v/0.4.5/index.html +1040 -0
  44. 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,20 +268,18 @@ 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
241
- if(convertTransforms || attributesToGroup) convertPathLength=true;
276
+ if (convertTransforms || attributesToGroup) convertPathLength = true;
242
277
 
243
- if (convertPathLength ) {
244
- //console.log('convertPathLength', convertPathLength, name);
278
+ if (convertPathLength) {
245
279
  styleProps = convertPathLengthAtt(el, { styleProps });
246
280
  remove = [...new Set([...remove, ...styleProps.remove])];
247
-
248
281
  }
249
282
 
250
- //console.log(styleProps);
251
283
 
252
284
  // get parent styles
253
285
  let { parentStyleProps = [] } = el;
@@ -255,7 +287,6 @@ export function cleanUpSVG(svgMarkup, {
255
287
  let transFormInherited = []
256
288
 
257
289
 
258
-
259
290
  /**
260
291
  * consolidate all properties:
261
292
  * merge with inherited transforms
@@ -288,7 +319,6 @@ export function cleanUpSVG(svgMarkup, {
288
319
  ...stylePropsSVG,
289
320
  ...inheritedProps,
290
321
  };
291
-
292
322
  //console.log('inheritedProps', inheritedProps);
293
323
 
294
324
  // merge with svg props
@@ -304,46 +334,11 @@ export function cleanUpSVG(svgMarkup, {
304
334
  //console.log('styleProps', styleProps);
305
335
  remove = [...new Set([...remove, ...styleProps.remove])];
306
336
 
307
-
308
- /**
309
- * remove els and attributes
310
- */
311
-
312
- // remove meta
313
- if (!allowMeta) removeElements.push('meta', 'metadata', 'desc', 'title')
314
-
315
- if (removeClassNames) {
316
- removeSVGAttributes.push('class');
317
- removeElAttributes.push('class');
318
- }
319
-
320
- if (removeIds) {
321
- removeSVGAttributes.push('id')
322
- removeElAttributes.push('id')
323
- }
324
-
325
-
326
- // remove hidden elements
327
- removeHiddenSvgEls(svg)
328
-
329
- // remove SVG elements
330
- removeSvgEls(svg, { removeElements, removeNameSpaced });
331
-
332
- // remove SVG attributes
333
- removeSvgAtts(svg, removeSVGAttributes);
334
-
335
- // remove SVG child element attributes
336
- removeSvgChildAtts(svg, removeElAttributes);
337
-
338
-
339
- // general cleanup
340
- if (cleanupSVGAtts) cleanupSVGAttributes(svg, { removeIds, removeClassNames, removeDimensions, stylesToAttributes, allowMeta, allowAriaAtts, allowDataAtts });
337
+
341
338
 
342
339
  // all relative units to absolute
343
340
  if (toAbsoluteUnits) {
344
341
  normalizeTransforms = true;
345
- //stylesToAttributes = true
346
- //console.log(name, styleProps);
347
342
 
348
343
  /**
349
344
  * apply consolidated
@@ -381,6 +376,7 @@ export function cleanUpSVG(svgMarkup, {
381
376
 
382
377
 
383
378
 
379
+
384
380
  if (stylesToAttributes) {
385
381
 
386
382
  /**
@@ -390,7 +386,6 @@ export function cleanUpSVG(svgMarkup, {
390
386
  styleProps = setNormalizedTransformsToEl(el, { styleProps });
391
387
  //remove = styleProps.remove;
392
388
  remove = [...new Set([...remove, ...styleProps.remove])];
393
-
394
389
  }
395
390
 
396
391
  /**
@@ -401,6 +396,7 @@ export function cleanUpSVG(svgMarkup, {
401
396
  stylePropsFiltered = filterSvgElProps(name, styleProps,
402
397
  { removeDefaults: true, cleanUpStrokes, allowMeta, allowAriaAtts, allowDataAtts, removeIds, inheritedProps });
403
398
 
399
+
404
400
  remove = [...new Set([...remove, ...stylePropsFiltered.remove])];
405
401
 
406
402
  for (let prop in stylePropsFiltered.propsFiltered) {
@@ -416,14 +412,6 @@ export function cleanUpSVG(svgMarkup, {
416
412
  */
417
413
  removeAtts(el, remove)
418
414
 
419
- /*
420
- for (let i = 0; i < remove.length; i++) {
421
- let att = remove[i];
422
- el.removeAttribute(att)
423
- }
424
- */
425
-
426
-
427
415
  } // endof style processing
428
416
 
429
417
 
@@ -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)
@@ -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
  /**