svg-path-simplify 0.0.9 → 0.1.2
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.
- package/README.md +114 -46
- package/debug.cjs +11 -0
- package/dist/svg-path-simplify.esm.js +613 -98
- package/dist/svg-path-simplify.esm.min.js +1 -1
- package/dist/svg-path-simplify.js +613 -98
- package/dist/svg-path-simplify.min.js +1 -1
- package/dist/svg-path-simplify.poly.cjs +29 -0
- package/index.html +5 -1
- package/package.json +36 -16
- package/src/index-node.js +15 -0
- package/src/index-poly.js +27 -0
- package/src/index.js +1 -6
- package/src/pathSimplify-main.js +47 -14
- package/src/svgii/geometry.js +54 -4
- package/src/svgii/geometry_deduceRadius.js +50 -0
- package/src/svgii/pathData_convert.js +339 -5
- package/src/svgii/pathData_refine_round.js +222 -0
- package/src/svgii/pathData_remove_collinear.js +8 -3
- package/src/svgii/pathData_remove_short.js +66 -0
- package/src/svgii/pathData_reorder.js +27 -11
- package/src/svgii/pathData_simplify_refineCorners.js +57 -37
- package/src/svgii/svg_cleanup.js +28 -29
- package/src/svgii/visualize.js +2 -2
- package/testSVG.js +27 -20
- package/dist/svg-path-simplify.node.js +0 -5129
- package/dist/svg-path-simplify.node.min.js +0 -1
- package/src/dom-polyfill.js +0 -29
- package/src/dom-polyfill_back.js +0 -22
package/README.md
CHANGED
|
@@ -34,6 +34,35 @@ Unlike most existing approaches (e.g in graphic applications), it checks where s
|
|
|
34
34
|
* optimize either path data strings or SVG markup code
|
|
35
35
|
|
|
36
36
|
|
|
37
|
+
## TOC
|
|
38
|
+
* [Usage](#usage)
|
|
39
|
+
+ [Browser](#browser)
|
|
40
|
+
- [Example 1: parse and simplify (using defaults)](#example-1-parse-and-simplify-using-defaults)
|
|
41
|
+
- [ESM version](#esm-version)
|
|
42
|
+
+ [node.js](#nodejs)
|
|
43
|
+
- [Example 2: Apply options](#example-2-apply-options)
|
|
44
|
+
+ [API](#api)
|
|
45
|
+
+ [Simplification parameters](#simplification-parameters)
|
|
46
|
+
+ [Advanced simplifications](#advanced-simplifications)
|
|
47
|
+
+ [Output options](#output-options)
|
|
48
|
+
+ [SVG output optimizations](#svg-output-optimizations)
|
|
49
|
+
+ [SVG input normalization](#svg-input-normalization)
|
|
50
|
+
* [Demos](#demos)
|
|
51
|
+
+ [Web app](#web-app)
|
|
52
|
+
+ [Demo files](#demo-files)
|
|
53
|
+
* [Limitations](#limitations)
|
|
54
|
+
+ [Optimization of complete SVG files](#optimization-of-complete-svg-files)
|
|
55
|
+
+ [»Natural« limitations of vector/curve simplification](#natural-limitations-of-vectorcurve-simplification)
|
|
56
|
+
+ [The sad truth about »gigantic« SVG files](#the-sad-truth-about-gigantic-svg-files)
|
|
57
|
+
* [Changelog, Updates and rollback](#changelog-updates-and-rollback)
|
|
58
|
+
+ [Changelog](#changelog)
|
|
59
|
+
+ [Rollback](#rollback)
|
|
60
|
+
* [Bug reporting](#bug-reporting)
|
|
61
|
+
* [Related libraries](#related-libraries)
|
|
62
|
+
* [Other SVG related projects](#other-svg-related-projects)
|
|
63
|
+
* [Credits](#credits)
|
|
64
|
+
|
|
65
|
+
|
|
37
66
|
## Usage
|
|
38
67
|
|
|
39
68
|
### Browser
|
|
@@ -84,57 +113,24 @@ Install module via npm:
|
|
|
84
113
|
```
|
|
85
114
|
npm install svg-path-simplify
|
|
86
115
|
```
|
|
87
|
-
|
|
116
|
+
|
|
117
|
+
To simplify entire SVG documents in node.js we need to emulate the browsers DOM methods `DOMParser` and `XMLSerializer`. I opted for [linkedom](https://github.com/WebReflection/linkedom). Just make sure to import the `svg-path-simplify/node` module. It will load linkedom's methods `DOMParser` and add a polyfill for `XMLSerializer`
|
|
118
|
+
|
|
88
119
|
```
|
|
89
|
-
|
|
120
|
+
/**
|
|
121
|
+
* load node polyfills for DOM parsing
|
|
122
|
+
* loads linkedom npm module for DOM parsing and emulation
|
|
123
|
+
*/
|
|
124
|
+
import 'svg-path-simplify/node';
|
|
90
125
|
|
|
91
126
|
// use it as in the above examples
|
|
127
|
+
import { svgPathSimplify } from 'svg-path-simplify';
|
|
128
|
+
|
|
92
129
|
let pathDataString = `M 57.13 15.5c 13.28 0 24.53 8.67 28.42 20.65c 0.94 2.91 1.45 6.01 1.45 9.23`;
|
|
93
130
|
let pathDataOpt = svgPathSimplify(pathDataString);
|
|
94
|
-
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
### API
|
|
98
|
-
|
|
99
|
-
```
|
|
100
|
-
let options = {}
|
|
101
|
-
let output = svgPathSimplify(input, options);
|
|
102
131
|
```
|
|
103
132
|
|
|
104
|
-
|
|
105
|
-
* a path data string – as used in SVG `<path>` element's `d` attribute
|
|
106
|
-
* a polygon string – as used in SVG `<polygon>` element's `points` attribute
|
|
107
|
-
* an entire `<svg>` markup
|
|
108
|
-
|
|
109
|
-
While svg-path-simplify aims at a convenient config-free usage you can tweak the simplification and output via these options passed as an object.
|
|
110
|
-
|
|
111
|
-
| parameter | effect | type | default |
|
|
112
|
-
| -- | -- | -- | -- |
|
|
113
|
-
| simplifyBezier | main Bézier simplification. When disabled you get the common optimization similar to SVGO (rounding, to all relative and shorthand conversions) | Boolean | true |
|
|
114
|
-
| getObject | whether to return the SVG/pathdata markup directly or a detailed object (containing more info) | Boolean | true |
|
|
115
|
-
| tolerance | increase or decrease tolerance: higher values allow more distortions, lower ones more shape fidelity | Number | 1 |
|
|
116
|
-
| optimizeOrder | reorders commands to get more adjacent simplification candidates. Improves optimization efficiency | Boolean | true |
|
|
117
|
-
| removeColinear | removes unnecessary zero-length or colinear lineto commands | Boolean | true |
|
|
118
|
-
| flatBezierToLinetos | replaces flat Béziers with linetos which also can be stripped via previous colinear removal | Boolean | true |
|
|
119
|
-
| revertToQuadratics | replaces cubic Béziers with quadratic (more compact) ones when applicable | Boolean | true |
|
|
120
|
-
| keepExtremes | skips simplification accross x/y extrema – improves shape fidelity | Boolean | true |
|
|
121
|
-
| keepCorners | skips simplification corners – improves shape fidelity | Boolean | true |
|
|
122
|
-
| extrapolateDominant | tries to extrapolate adjacent curves based on dominant larger segment. Effective but adds computational load. Disable it for better performance | Boolean | true |
|
|
123
|
-
| keepInflections | retains commands introducing direction changes – adds complexity but may help for editing in a graphic application | Boolean | false |
|
|
124
|
-
| addExtremes | adds commands at x/y extrema – adds complexity but may help for editing in a graphic application | Boolean | false |
|
|
125
|
-
| autoAccuracy | calculates a suitable floating point precision for coordinate rounding. Usually rather conservative – decreasing by one decimal should work without significant distortions | Boolean | true |
|
|
126
|
-
| decimals | manual floating point rounding precision – overriden when `autoAccuracy` is enabled | Number | 3 |
|
|
127
|
-
| minifyD | path data microoptimization: removes recurring command type tokens, whitespace and leading zeroes: 0: maximum optimization; 1: "verbose" dont't omit command type tokes; 2: "beautify" separate each command with new lines (e.g for educational purposes) | Number | 0 |
|
|
128
|
-
| quadraticToCubic | converts all quadratic Béziers to cubics – recommended for efficiency | Boolean | true |
|
|
129
|
-
| toRelative | converts all commands to relative – reduces file size | Boolean | true |
|
|
130
|
-
| toShorthands | converts all commands to shorthand when applicable – reduces file size | Boolean | true |
|
|
131
|
-
| arcToCubic | converts elliptic arc `A` commands to cubic approximations – not recommended | Boolean | false |
|
|
132
|
-
| removeHidden | removes hidden elements for SVG inputs | Boolean | true |
|
|
133
|
-
| mergePaths | concatenates paths into single one – does not respect individual styles! | Boolean | false |
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
### Example 2: Apply options
|
|
133
|
+
#### Example 2: Apply options
|
|
138
134
|
The following example would return a detailed object containing the stringified "normalized" pathdata (all absolute and "longhand" command notation).
|
|
139
135
|
|
|
140
136
|
```
|
|
@@ -201,9 +197,81 @@ Z
|
|
|
201
197
|
*/
|
|
202
198
|
```
|
|
203
199
|
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
### API
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
let options = {}
|
|
206
|
+
let output = svgPathSimplify(input, options);
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
The first parameter is the SVG input:
|
|
210
|
+
* a path data string – as used in SVG `<path>` element's `d` attribute
|
|
211
|
+
* a polygon string – as used in SVG `<polygon>` element's `points` attribute
|
|
212
|
+
* an entire `<svg>` markup
|
|
213
|
+
|
|
214
|
+
While svg-path-simplify aims at a convenient config-free usage you can tweak the simplification and output via these options passed as an object.
|
|
215
|
+
|
|
216
|
+
### Simplification parameters
|
|
217
|
+
These params control shich simplifications are applied. The default settings aim at a safe or balanced performance-to-minification ratio. However if your main goal is to get the most compact result you can enable additional options which also require more processing time.
|
|
218
|
+
| parameter | effect | type | default |
|
|
219
|
+
| -- | -- | -- | -- |
|
|
220
|
+
| simplifyBezier | main Bézier simplification. When disabled you get the common optimization similar to SVGO (rounding, to all relative and shorthand conversions) | Boolean | true |
|
|
221
|
+
| tolerance | increase or decrease tolerance: higher values allow more distortions, lower ones more shape fidelity | Number | 1 |
|
|
222
|
+
| optimizeOrder | reorders commands to get more adjacent simplification candidates. Improves optimization efficiency | Boolean | true |
|
|
223
|
+
| removeColinear | removes unnecessary zero-length or colinear lineto commands | Boolean | true |
|
|
224
|
+
| flatBezierToLinetos | replaces flat Béziers with linetos which also can be stripped via previous colinear removal | Boolean | true |
|
|
225
|
+
| revertToQuadratics | replaces cubic Béziers with quadratic (more compact) ones when applicable | Boolean | true |
|
|
226
|
+
| keepExtremes | skips simplification accross x/y extrema – improves shape fidelity | Boolean | true |
|
|
227
|
+
| keepCorners | skips simplification corners – improves shape fidelity | Boolean | true |
|
|
228
|
+
| keepInflections | retains commands introducing direction changes – adds complexity but may help for editing in a graphic application | Boolean | false |
|
|
229
|
+
|
|
230
|
+
### Advanced simplifications
|
|
231
|
+
| parameter | effect | type | default |
|
|
232
|
+
| -- | -- | -- | -- |
|
|
233
|
+
| refineExtremes | tries to combine commands close to an adjacent x/y extreme segment | Boolean | false |
|
|
234
|
+
| addExtremes | adds commands at x/y extrema – adds complexity but may help for editing in a graphic application – when using this option enable `refineExtremes` as well to avoid tiny adjacent segments | Boolean | false |
|
|
235
|
+
| simplifyCorners | replaces Bézier segments enclosed by linetos with single quadratic commands – handy to reduce overly complex tiny corner roundings. See example in [webapp](https://herrstrietzel.github.io/svg-path-simplify?samples=fontawesome_gears&refineExtremes=true&simplifyCorners=true) | Boolean | false |
|
|
236
|
+
| cubicToArc | replaces Bézier segments by elliptic arc `A` commands where applicable – can reduce complexity for semi- or full circles | Boolean | false |
|
|
237
|
+
| simplifyRound | replaces small round segments encloses by linetos – helps to simplify shapes like gears/cogs | Boolean | false |
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
### Output options
|
|
241
|
+
|
|
242
|
+
| parameter | effect | type | default |
|
|
243
|
+
| -- | -- | -- | -- |
|
|
244
|
+
| getObject | whether to return the SVG/pathdata markup directly or a detailed object (containing more info) | Boolean | true |
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
### SVG output optimizations
|
|
248
|
+
| parameter | effect | type | default |
|
|
249
|
+
| -- | -- | -- | -- |
|
|
250
|
+
| autoAccuracy | calculates a suitable floating point precision for coordinate rounding. Usually rather conservative – decreasing by one decimal should work without significant distortions | Boolean | true |
|
|
251
|
+
| decimals | manual floating point rounding precision – overriden when `autoAccuracy` is enabled | Number | 3 |
|
|
252
|
+
| minifyD | path data microoptimization: removes recurring command type tokens, whitespace and leading zeroes: 0: maximum optimization; 1: "verbose" dont't omit command type tokes; 2: "beautify" separate each command with new lines (e.g for educational purposes) | Number | 0 |
|
|
253
|
+
| toRelative | converts all commands to relative – reduces file size | Boolean | true |
|
|
254
|
+
| toShorthands | converts all commands to shorthand when applicable – reduces file size | Boolean | true |
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
### SVG input normalization
|
|
258
|
+
| parameter | effect | type | default |
|
|
259
|
+
| -- | -- | -- | -- |
|
|
260
|
+
| quadraticToCubic | converts all quadratic Béziers to cubics – recommended for efficiency | Boolean | true |
|
|
261
|
+
| arcToCubic | converts elliptic arc `A` commands to cubic approximations – not recommended | Boolean | false |
|
|
262
|
+
| removeHidden | removes hidden elements for SVG inputs | Boolean | true |
|
|
263
|
+
| mergePaths | concatenates paths into single one – does not respect individual styles! | Boolean | false |
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
204
267
|
## Demos
|
|
205
|
-
|
|
268
|
+
### Web app
|
|
269
|
+
You can easily test this library via the [**webapp**](https://herrstrietzel.github.io/svg-path-simplify/) or by checking the demo folder.
|
|
270
|
+
|
|
271
|
+

|
|
272
|
+
|
|
206
273
|
|
|
274
|
+
### Demo files
|
|
207
275
|
* [simple setup IIFE](./demo/simple-iife.html)
|
|
208
276
|
* [simple setup esm](./demo/simple-esm.html)
|
|
209
277
|
|
|
@@ -229,7 +297,7 @@ SVGs > 1 MB are most of the time not salvagable. At least if they contain 10K+ o
|
|
|
229
297
|
|
|
230
298
|
## Changelog, Updates and rollback
|
|
231
299
|
### Changelog
|
|
232
|
-
|
|
300
|
+
* 0.1.0 fixed node support for complete svg files
|
|
233
301
|
|
|
234
302
|
### Rollback
|
|
235
303
|
If you encounter any issues with the recent versions you can rollback to a previous version.
|
package/debug.cjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// debug.cjs
|
|
2
|
+
const { createRequire } = require('module');
|
|
3
|
+
const requireModule = createRequire(__filename);
|
|
4
|
+
|
|
5
|
+
console.log('require.resolve →', requireModule.resolve('svg-path-simplify'));
|
|
6
|
+
|
|
7
|
+
(async () => {
|
|
8
|
+
const mod = await import('svg-path-simplify'); // dynamic import works in CJS
|
|
9
|
+
console.log('imported keys →', Object.keys(mod));
|
|
10
|
+
console.log('default keys →', mod.default && Object.keys(mod.default));
|
|
11
|
+
})();
|