oslllo-svg-fixer 6.0.1 → 7.0.0
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 +140 -63
- package/package.json +12 -11
- package/src/cli.js +4 -2
package/README.md
CHANGED
|
@@ -1,110 +1,187 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
1
3
|
# SVG Fixer
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
Convert SVG strokes and shapes into a single filled path for icon fonts and other tools that require path outlines.
|
|
4
6
|
|
|
5
7
|
[](https://github.com/oslllo/svg-fixer/actions/workflows/ci.test.yml)
|
|
6
8
|
[](https://www.npmjs.com/package/oslllo-svg-fixer)
|
|
7
|
-
[](https://coveralls.io/github/oslllo/svg-fixer)
|
|
10
|
+
|
|
11
|
+
🎉 v3.0.0 is now 11x faster thanks to [@ericfennis](https://github.com/ericfennis). See [PR #89](https://github.com/oslllo/svg-fixer/pull/89).
|
|
12
|
+
|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
## Contents
|
|
16
|
+
|
|
17
|
+
- [Installation](#installation)
|
|
18
|
+
- [Quick start](#quick-start)
|
|
19
|
+
- [Files and directories](#files-and-directories)
|
|
20
|
+
- [SVG strings and Buffers](#svg-strings-and-buffers)
|
|
21
|
+
- [Options](#options)
|
|
22
|
+
- [Command line](#command-line)
|
|
23
|
+
- [How conversion works](#how-conversion-works)
|
|
24
|
+
- [Development](#development)
|
|
25
|
+
- [Changelog and license](#changelog-and-license)
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
**Requires Node.js 22.12 or later.** CI tests Node.js 22 and 24 on Linux, macOS, and Windows.
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install oslllo-svg-fixer
|
|
33
|
+
# Or:
|
|
34
|
+
yarn add oslllo-svg-fixer
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
This README describes the current source on `master`. Check the release's requirements when using an older package version.
|
|
38
|
+
|
|
39
|
+
## Quick start
|
|
8
40
|
|
|
9
|
-
|
|
41
|
+
```js
|
|
42
|
+
const fs = require("node:fs");
|
|
43
|
+
const SVGFixer = require("oslllo-svg-fixer");
|
|
10
44
|
|
|
11
|
-
|
|
45
|
+
async function main() {
|
|
46
|
+
fs.mkdirSync("fixed", { recursive: true });
|
|
47
|
+
await SVGFixer("icons", "fixed").fix();
|
|
48
|
+
console.log("Saved fixed icons");
|
|
49
|
+
}
|
|
12
50
|
|
|
13
|
-
|
|
51
|
+
main().catch(console.error);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The following examples use the same `SVGFixer` import. Examples containing `await` belong inside an async function.
|
|
55
|
+
|
|
56
|
+
## Files and directories
|
|
14
57
|
|
|
15
|
-
|
|
58
|
+
`SVGFixer(source, destination, options?)` creates an instance, with or without `new`.
|
|
16
59
|
|
|
17
|
-
|
|
60
|
+
| Parameter | Meaning |
|
|
61
|
+
| --- | --- |
|
|
62
|
+
| `source` | Path to one `.svg` file or a directory containing `.svg` files |
|
|
63
|
+
| `destination` | Output directory; must already exist by default |
|
|
64
|
+
| `options` | Optional conversion settings described below |
|
|
18
65
|
|
|
19
|
-
|
|
66
|
+
Directory discovery processes lowercase `*.svg` files directly inside the source directory; it does not recurse into subdirectories. Output files keep their original basenames. Existing output files with the same names are overwritten, so choose a separate destination when retaining the originals.
|
|
20
67
|
|
|
21
|
-
|
|
68
|
+
```js
|
|
69
|
+
// One file; destination is still a directory.
|
|
70
|
+
const fixer = await SVGFixer("icons/example.svg", "fixed").fix();
|
|
22
71
|
|
|
23
|
-
|
|
72
|
+
// All SVGs immediately inside a directory.
|
|
73
|
+
await SVGFixer("icons", "fixed", { traceResolution: 1200 }).fix();
|
|
74
|
+
```
|
|
24
75
|
|
|
25
|
-
|
|
76
|
+
`.fix()` returns a Promise resolving to the fixer instance. With a callback, it returns the instance immediately and calls `callback(err, fixer)` when processing finishes:
|
|
26
77
|
|
|
27
|
-
|
|
78
|
+
```js
|
|
79
|
+
SVGFixer("icons", "fixed").fix((err, fixer) => {
|
|
80
|
+
if (err) {
|
|
81
|
+
console.error(err);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
console.log(`Saved ${fixer.source.length} icons`);
|
|
85
|
+
});
|
|
86
|
+
```
|
|
28
87
|
|
|
29
|
-
|
|
88
|
+
Constructor validation can throw synchronously, including when paths are invalid. Handle conversion failures through Promise rejections or callback errors.
|
|
30
89
|
|
|
31
|
-
|
|
90
|
+
## SVG strings and Buffers
|
|
32
91
|
|
|
33
|
-
|
|
92
|
+
`SVGFixer.fixString(svg, resolution = 600)` accepts SVG markup as a string or Buffer and returns a Promise resolving to fixed SVG markup. It does not read a file path, write files, or use a callback.
|
|
34
93
|
|
|
35
|
-
|
|
94
|
+
```js
|
|
95
|
+
const source = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9" fill="none" stroke="black" stroke-width="2"/></svg>';
|
|
36
96
|
|
|
37
|
-
|
|
97
|
+
const fixed = await SVGFixer.fixString(source);
|
|
98
|
+
const higherResolution = await SVGFixer.fixString(Buffer.from(source), 1200);
|
|
99
|
+
```
|
|
38
100
|
|
|
39
|
-
|
|
101
|
+
Invalid input types throw synchronously; rendering and tracing errors reject the returned Promise.
|
|
40
102
|
|
|
41
|
-
|
|
103
|
+
## Options
|
|
42
104
|
|
|
43
|
-
|
|
105
|
+
| Option | Default | Meaning |
|
|
106
|
+
| --- | --- | --- |
|
|
107
|
+
| `showProgressBar` | `false` | Show progress while processing files |
|
|
108
|
+
| `throwIfDestinationDoesNotExist` | `true` | Throw when the output directory is missing |
|
|
109
|
+
| `traceResolution` | `600` | Width in pixels of the raster image used for tracing; height follows the original aspect ratio |
|
|
44
110
|
|
|
45
|
-
|
|
111
|
+
A larger trace resolution can improve detail at the cost of processing time and memory. Use a positive resolution. This value controls the intermediate image, not the final SVG's intended dimensions.
|
|
46
112
|
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
fill="none"
|
|
54
|
-
stroke="currentColor"
|
|
55
|
-
stroke-width="2"
|
|
56
|
-
stroke-linecap="round"
|
|
57
|
-
stroke-linejoin="round">
|
|
58
|
-
<rect x="3" y="3" width="18" height="18" rx="2" ry="2" />
|
|
59
|
-
<line x1="9" y1="9" x2="15" y2="15" />
|
|
60
|
-
<line x1="15" y1="9" x2="9" y2="15" />
|
|
61
|
-
</svg>
|
|
113
|
+
```js
|
|
114
|
+
await SVGFixer("icons", "fixed", {
|
|
115
|
+
showProgressBar: true,
|
|
116
|
+
throwIfDestinationDoesNotExist: false,
|
|
117
|
+
traceResolution: 600,
|
|
118
|
+
}).fix();
|
|
62
119
|
```
|
|
63
120
|
|
|
64
|
-
|
|
121
|
+
With `throwIfDestinationDoesNotExist: false`, the destination directory is created if missing. Its parent directory must already exist; creation is not recursive.
|
|
65
122
|
|
|
66
|
-
|
|
123
|
+
## Command line
|
|
67
124
|
|
|
68
|
-
|
|
125
|
+
Use the locally installed binary:
|
|
69
126
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
127
|
+
```sh
|
|
128
|
+
npx oslllo-svg-fixer --source icons --destination fixed
|
|
129
|
+
npx oslllo-svg-fixer -s icons/example.svg -d fixed --trace-resolution 1200
|
|
130
|
+
npx oslllo-svg-fixer -s icons -d fixed --strict-destination=false --show-progress=false
|
|
131
|
+
npx oslllo-svg-fixer --help
|
|
132
|
+
```
|
|
73
133
|
|
|
74
|
-
|
|
134
|
+
Or install globally:
|
|
75
135
|
|
|
76
|
-
|
|
136
|
+
```sh
|
|
137
|
+
npm install --global oslllo-svg-fixer
|
|
138
|
+
oslllo-svg-fixer -s icons -d fixed
|
|
139
|
+
```
|
|
77
140
|
|
|
78
|
-
|
|
141
|
+
| Flag | Alias | Default | Meaning |
|
|
142
|
+
| --- | --- | --- | --- |
|
|
143
|
+
| `--source` | `-s` | Required | Source file or directory |
|
|
144
|
+
| `--destination` | `-d` | Required | Output directory |
|
|
145
|
+
| `--strict-destination` | `--sd` | `true` | Require the destination to exist |
|
|
146
|
+
| `--show-progress` | `--sp` | `true` | Show progress; enabled by default in the CLI |
|
|
147
|
+
| `--trace-resolution` | `--tr` | `600` | Intermediate raster width |
|
|
148
|
+
| `--help` | | | Show usage |
|
|
149
|
+
| `--version` | | | Show package version |
|
|
79
150
|
|
|
80
|
-
|
|
151
|
+
Pass source and destination as named flags. Exit code `0` means success; `1` means failure.
|
|
81
152
|
|
|
82
|
-
|
|
153
|
+
## How conversion works
|
|
83
154
|
|
|
84
|
-
|
|
155
|
+
SVG Fixer uses **SVG2** to render an enlarged SVG as a PNG, then **Potrace** to trace the image into one path containing the outlines. File conversions run through a Piscina worker pool. `fixString()` calls the conversion pipeline directly.
|
|
85
156
|
|
|
86
|
-
|
|
87
|
-
| -------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
88
|
-
| [](docs/images/icomoon-blank.png) | [](docs/images/fontello-blank.png) | [](docs/images/webfont-blank.png) |
|
|
157
|
+
This is rasterization followed by tracing, so curves and fine details can change. The result is intended for monochrome icon outlines, rather than preserving multicolor artwork, text, filters, or SVG structure. The generated path uses `stroke="none"` and `fill-rule="evenodd"`.
|
|
89
158
|
|
|
90
|
-
|
|
159
|
+
The converter restores original root attributes and selected attributes from the first original path. It normalizes the root `viewBox` origin to `0 0`; do not assume every attribute or coordinate system is preserved exactly. Review the output for artwork with unusual fills, multiple colors, or offset view boxes.
|
|
91
160
|
|
|
92
|
-
|
|
161
|
+
The current package depends on the published **SVG2 v6** and **Potrace v6** releases, even though their sibling repositories may contain newer changes. Editing a sibling checkout does not automatically change the installed dependencies used here.
|
|
93
162
|
|
|
94
|
-
|
|
163
|
+
## Development
|
|
95
164
|
|
|
96
|
-
|
|
165
|
+
CI runs on pull requests targeting `master`, pushes to `master`, and manual dispatch. Tests cover Node.js 22 and 24 on Linux, macOS, and Windows, plus Node.js 22.12.0 on Linux to verify the minimum supported version.
|
|
97
166
|
|
|
98
|
-
|
|
99
|
-
| -------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
100
|
-
| [](docs/images/icomoon-fixed.png) | [](docs/images/fontello-fixed.png) | [](docs/images/webfont-fixed.png) |
|
|
167
|
+
The dependency overrides select patched `glob` and `minimatch` versions for the test tooling. Keep these overrides until upstream dependencies adopt the patched versions.
|
|
101
168
|
|
|
102
|
-
|
|
169
|
+
```sh
|
|
170
|
+
npm ci
|
|
171
|
+
npm test
|
|
172
|
+
```
|
|
103
173
|
|
|
104
|
-
|
|
174
|
+
`npm test` runs the main tests, output checks, and ESLint with coverage collection. Main tests include file and string conversion, options, paths, callbacks, progress, and the CLI. Output tests compare rendering and inspect paths and attributes.
|
|
105
175
|
|
|
106
|
-
```
|
|
107
|
-
|
|
176
|
+
```sh
|
|
177
|
+
npm run test:all # Main tests followed by output checks, without lint
|
|
178
|
+
npm run test:cli # CLI tests only
|
|
179
|
+
npm run lint
|
|
180
|
+
npm run coverage # Generate LCOV after collecting coverage
|
|
108
181
|
```
|
|
109
182
|
|
|
110
|
-
|
|
183
|
+
## Changelog and license
|
|
184
|
+
|
|
185
|
+
See [CHANGELOG.md](CHANGELOG.md) for release history and [LICENSE](LICENSE) for the MIT license of SVG Fixer. Its tracing dependency, `oslllo-potrace`, is licensed separately under GPL-2.0.
|
|
186
|
+
|
|
187
|
+
Created by [oslllo](https://github.com/oslllo). The project was built to prepare icons for tools such as IcoMoon, Fontello, and webfont. Conversion uses SVG2 and Potrace; worker processing uses Piscina.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oslllo-svg-fixer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "Converts SVG Strokes To Fill.",
|
|
5
5
|
"bin": "src/cli.js",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -42,31 +42,32 @@
|
|
|
42
42
|
"bugs": {
|
|
43
43
|
"url": "https://github.com/oslllo/svg-fixer/issues"
|
|
44
44
|
},
|
|
45
|
-
"homepage": "https://docs.oslllo.com/svg-fixer/master",
|
|
46
45
|
"dependencies": {
|
|
47
46
|
"ansi-colors": "^4.1.3",
|
|
48
47
|
"cli-progress": "^3.12.0",
|
|
49
48
|
"fast-glob": "^3.3.1",
|
|
50
|
-
"oslllo-potrace": "^
|
|
51
|
-
"oslllo-svg2": "^
|
|
52
|
-
"oslllo-validator": "^
|
|
53
|
-
"piscina": "^4.
|
|
49
|
+
"oslllo-potrace": "^6.0.0",
|
|
50
|
+
"oslllo-svg2": "^6.0.0",
|
|
51
|
+
"oslllo-validator": "^4.0.0",
|
|
52
|
+
"piscina": "^4.9.4",
|
|
54
53
|
"yargs": "^16.2.0"
|
|
55
54
|
},
|
|
56
55
|
"devDependencies": {
|
|
57
56
|
"chai": "^4.3.8",
|
|
58
57
|
"chai-as-promised": "^7.1.1",
|
|
59
|
-
"coveralls": "^3.1.1",
|
|
60
58
|
"eslint": "^7.32.0",
|
|
61
59
|
"fs-extra": "^10.1.0",
|
|
62
60
|
"inquirer": "^8.2.6",
|
|
63
61
|
"looks-same": "^7.3.0",
|
|
64
|
-
"mocha": "^
|
|
65
|
-
"
|
|
66
|
-
"nyc": "^15.1.0",
|
|
62
|
+
"mocha": "^12.0.3",
|
|
63
|
+
"nyc": "^18.0.0",
|
|
67
64
|
"stopwatch-node": "^1.1.0"
|
|
68
65
|
},
|
|
69
66
|
"engines": {
|
|
70
|
-
"node": ">=
|
|
67
|
+
"node": ">=22.12.0"
|
|
68
|
+
},
|
|
69
|
+
"overrides": {
|
|
70
|
+
"glob@^13.0.0": "^14.0.7",
|
|
71
|
+
"minimatch@^10.0.0": "^11.0.10"
|
|
71
72
|
}
|
|
72
73
|
}
|
package/src/cli.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
const SVGFixer = require("../");
|
|
6
6
|
const colors = require("ansi-colors");
|
|
7
|
+
const fs = require("fs");
|
|
7
8
|
var argvs = require("yargs/yargs")(process.argv.slice(2)); // eslint-disable-line no-magic-numbers
|
|
8
9
|
|
|
9
10
|
argvs
|
|
@@ -55,8 +56,9 @@ const options = {
|
|
|
55
56
|
await SVGFixer(source, destination, options).fix();
|
|
56
57
|
process.exit(0); // eslint-disable-line no-process-exit, no-magic-numbers
|
|
57
58
|
} catch (err) {
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
const message = "\n" + colors.red("ERR: " + err.message + "\n") +
|
|
60
|
+
colors.yellow("STACK: " + err.stack + "\n");
|
|
61
|
+
fs.writeSync(process.stdout.fd, message);
|
|
60
62
|
process.exit(1); // eslint-disable-line no-process-exit, no-magic-numbers
|
|
61
63
|
}
|
|
62
64
|
})();
|