@iconify/tools 1.3.17 → 2.0.0-dev.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/.editorconfig +13 -0
- package/.eslintignore +2 -0
- package/lib/colors/attribs.d.ts +16 -0
- package/lib/colors/attribs.js +26 -0
- package/lib/colors/attribs.mjs +28 -0
- package/lib/colors/parse.d.ts +37 -0
- package/lib/colors/parse.js +261 -0
- package/lib/colors/parse.mjs +212 -0
- package/lib/css/parse.d.ts +4 -0
- package/lib/css/parse.js +23 -0
- package/lib/css/parse.mjs +20 -0
- package/lib/css/parser/error.d.ts +11 -0
- package/lib/css/parser/error.js +27 -0
- package/lib/css/parser/error.mjs +23 -0
- package/lib/css/parser/export.d.ts +5 -0
- package/lib/css/parser/export.js +69 -0
- package/lib/css/parser/export.mjs +46 -0
- package/lib/css/parser/strings.d.ts +13 -0
- package/lib/css/parser/strings.js +93 -0
- package/lib/css/parser/strings.mjs +74 -0
- package/lib/css/parser/text.d.ts +17 -0
- package/lib/css/parser/text.js +174 -0
- package/lib/css/parser/text.mjs +133 -0
- package/lib/css/parser/tokens.d.ts +6 -0
- package/lib/css/parser/tokens.js +200 -0
- package/lib/css/parser/tokens.mjs +186 -0
- package/lib/css/parser/tree.d.ts +5 -0
- package/lib/css/parser/tree.js +44 -0
- package/lib/css/parser/tree.mjs +40 -0
- package/lib/css/parser/types.d.ts +51 -0
- package/lib/css/parser/types.js +2 -0
- package/lib/css/parser/types.mjs +0 -0
- package/lib/icon-set/index.d.ts +134 -0
- package/lib/icon-set/index.js +776 -0
- package/lib/icon-set/index.mjs +617 -0
- package/lib/icon-set/match.d.ts +6 -0
- package/lib/icon-set/match.js +66 -0
- package/lib/icon-set/match.mjs +55 -0
- package/lib/icon-set/merge.d.ts +5 -0
- package/lib/icon-set/merge.js +91 -0
- package/lib/icon-set/merge.mjs +75 -0
- package/lib/icon-set/props.d.ts +10 -0
- package/lib/icon-set/props.js +33 -0
- package/lib/icon-set/props.mjs +25 -0
- package/lib/icon-set/types.d.ts +68 -0
- package/lib/icon-set/types.js +2 -0
- package/lib/icon-set/types.mjs +0 -0
- package/lib/import/directory.d.ts +35 -0
- package/lib/import/directory.js +59 -0
- package/lib/import/directory.mjs +47 -0
- package/lib/misc/keyword.d.ts +4 -0
- package/lib/misc/keyword.js +31 -0
- package/lib/misc/keyword.mjs +17 -0
- package/lib/misc/scan.d.ts +24 -0
- package/lib/misc/scan.js +48 -0
- package/lib/misc/scan.mjs +43 -0
- package/lib/optimise/flags.d.ts +5 -0
- package/lib/optimise/flags.js +303 -0
- package/lib/optimise/flags.mjs +241 -0
- package/lib/optimise/scale.d.ts +5 -0
- package/lib/optimise/scale.js +42 -0
- package/lib/optimise/scale.mjs +22 -0
- package/lib/optimise/svgo.d.ts +27 -0
- package/lib/optimise/svgo.js +88 -0
- package/lib/optimise/svgo.mjs +75 -0
- package/lib/svg/cleanup/attribs.d.ts +5 -0
- package/lib/svg/cleanup/attribs.js +43 -0
- package/lib/svg/cleanup/attribs.mjs +36 -0
- package/lib/svg/cleanup/bad-tags.d.ts +5 -0
- package/lib/svg/cleanup/bad-tags.js +69 -0
- package/lib/svg/cleanup/bad-tags.mjs +68 -0
- package/lib/svg/cleanup/inline-style.d.ts +5 -0
- package/lib/svg/cleanup/inline-style.js +77 -0
- package/lib/svg/cleanup/inline-style.mjs +65 -0
- package/lib/svg/cleanup/root-svg.d.ts +5 -0
- package/lib/svg/cleanup/root-svg.js +106 -0
- package/lib/svg/cleanup/root-svg.mjs +88 -0
- package/lib/svg/cleanup/svgo-style.d.ts +5 -0
- package/lib/svg/cleanup/svgo-style.js +35 -0
- package/lib/svg/cleanup/svgo-style.mjs +29 -0
- package/lib/svg/cleanup.d.ts +5 -0
- package/lib/svg/cleanup.js +24 -0
- package/lib/svg/cleanup.mjs +16 -0
- package/lib/svg/data/attributes.d.ts +71 -0
- package/lib/svg/data/attributes.js +403 -0
- package/lib/svg/data/attributes.mjs +352 -0
- package/lib/svg/data/tags.d.ts +89 -0
- package/lib/svg/data/tags.js +185 -0
- package/lib/svg/data/tags.mjs +136 -0
- package/lib/svg/index.d.ts +33 -0
- package/lib/svg/index.js +122 -0
- package/lib/svg/index.mjs +85 -0
- package/lib/svg/parse-style.d.ts +40 -0
- package/lib/svg/parse-style.js +131 -0
- package/lib/svg/parse-style.mjs +109 -0
- package/lib/svg/parse.d.ts +30 -0
- package/lib/svg/parse.js +49 -0
- package/lib/svg/parse.mjs +40 -0
- package/package.json +167 -13
- package/README.md +0 -576
- package/license.txt +0 -21
- package/sample/parse.js +0 -74
- package/sample/source/icon-close.svg +0 -15
- package/sample/source/icon-confirm.svg +0 -14
- package/sample/source/icon-search.svg +0 -16
- package/src/collection.js +0 -641
- package/src/colors/change_palette.js +0 -227
- package/src/colors/get_palette.js +0 -143
- package/src/colors/opacify.js +0 -195
- package/src/export/component.js +0 -482
- package/src/export/dir.js +0 -109
- package/src/export/json.js +0 -329
- package/src/export/phantomjs.js +0 -76
- package/src/export/phantomjs_script.js +0 -125
- package/src/export/png.js +0 -193
- package/src/export/svg.js +0 -55
- package/src/export/templates/component.md +0 -79
- package/src/export/templates/info.md +0 -3
- package/src/export/templates/sample-react-1.md +0 -21
- package/src/export/templates/sample-react-2.md +0 -15
- package/src/export/templates/sample-react.md +0 -11
- package/src/export/templates/sample-svelte.md +0 -11
- package/src/export/templates/sample-svelte1.md +0 -22
- package/src/export/templates/sample-svelte2.md +0 -13
- package/src/export/templates/sample-vue-0.md +0 -30
- package/src/export/templates/sample-vue-1.md +0 -25
- package/src/export/templates/sample-vue-2.md +0 -27
- package/src/export/templates/sample-vue.md +0 -28
- package/src/helpers.js +0 -43
- package/src/import/dir.js +0 -234
- package/src/import/font.js +0 -402
- package/src/import/json.js +0 -200
- package/src/import/svg.js +0 -60
- package/src/import/web_icons.js +0 -248
- package/src/modules.js +0 -50
- package/src/optimize/crop.js +0 -554
- package/src/optimize/crop_script.js +0 -525
- package/src/optimize/flags.js +0 -430
- package/src/optimize/scale.js +0 -72
- package/src/optimize/svgo.js +0 -161
- package/src/optimize/tags.js +0 -522
- package/src/shapes/convert.js +0 -264
- package/src/shapes/index.js +0 -135
- package/src/shapes/length.js +0 -707
- package/src/shapes/length_script.js +0 -105
- package/src/shapes/options.js +0 -60
- package/src/svg.js +0 -162
package/README.md
DELETED
|
@@ -1,576 +0,0 @@
|
|
|
1
|
-
# Iconify Tools
|
|
2
|
-
|
|
3
|
-
This library is a collection of tools for importing, exporting and processing SVG images.
|
|
4
|
-
|
|
5
|
-
Its main purpose is to convert icon sets and fonts to Iconify JSON collections, but it can be used for other purposes.
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
First install it by running this command:
|
|
10
|
-
|
|
11
|
-
```
|
|
12
|
-
npm install @iconify/tools --save
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
Then you can use it in your Node.js files:
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
const tools = require('@iconify/tools');
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## What tools are available?
|
|
22
|
-
|
|
23
|
-
* Import SVG from various sources
|
|
24
|
-
* Export SVG to various formats
|
|
25
|
-
* Optimize images
|
|
26
|
-
* Crop images
|
|
27
|
-
* Clean up images
|
|
28
|
-
* Get/change color palette
|
|
29
|
-
* Find shapes, get lengths of shapes
|
|
30
|
-
* Convert shapes to paths
|
|
31
|
-
|
|
32
|
-
## Core
|
|
33
|
-
|
|
34
|
-
Core of tools are SVG and Collections classes. All tools create or manipulate instances of SVG or Collection classes.
|
|
35
|
-
|
|
36
|
-
All tools are based on Promises. If you are not familiar with JavaScript Promises, do read up. There are many tutorials
|
|
37
|
-
available online. Make sure you are reading something from 2016 or newer, not tutorials for old implementations.
|
|
38
|
-
|
|
39
|
-
### SVG class
|
|
40
|
-
|
|
41
|
-
SVG class represents one SVG image. It is a simple class, it does not manipulate anything, except for
|
|
42
|
-
cleaning up junk code that could otherwise cause XML parser to fail.
|
|
43
|
-
|
|
44
|
-
You can find code in src/svg.js
|
|
45
|
-
|
|
46
|
-
Creating SVG instance is easy:
|
|
47
|
-
|
|
48
|
-
```
|
|
49
|
-
let svg = new tools.SVG('<svg ...></svg>');
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
That code will load SVG from string, extract image dimensions and clean up a bit, removing all junk image editors left
|
|
53
|
-
behind.
|
|
54
|
-
|
|
55
|
-
SVG instance has multiple methods to get SVG as string:
|
|
56
|
-
|
|
57
|
-
* svg.toString() - returns SVG as string
|
|
58
|
-
* svg.toMinifiedString() - same as toString(), but without white spaces
|
|
59
|
-
* svg.getBody() - returns body as string: child elements of `<svg>` element.
|
|
60
|
-
|
|
61
|
-
Then you can get dimensions:
|
|
62
|
-
|
|
63
|
-
* svg.getDimensions() - returns object containing width and height properties
|
|
64
|
-
* svg.width - returns width
|
|
65
|
-
* svg.height - returns height
|
|
66
|
-
|
|
67
|
-
Last method replaces content of SVG instance:
|
|
68
|
-
|
|
69
|
-
* svg.load(content) - same as constructor, but instead of making new instance it changes existing instance.
|
|
70
|
-
|
|
71
|
-
You can see usage examples in unit tests: tests/core/svg_test.js
|
|
72
|
-
|
|
73
|
-
### Collection class
|
|
74
|
-
|
|
75
|
-
Collection is a set of SVG instances.
|
|
76
|
-
|
|
77
|
-
You can find code in src/collection.js
|
|
78
|
-
|
|
79
|
-
To create Collection instance use this:
|
|
80
|
-
|
|
81
|
-
```
|
|
82
|
-
let collection = new tools.Collection();
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
That will create empty collection.
|
|
86
|
-
|
|
87
|
-
To clone another collection add collection as parameter to constructor:
|
|
88
|
-
|
|
89
|
-
```
|
|
90
|
-
let newCollection = new tools.Collection(oldCollection);
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
To add/remove items there are several methods:
|
|
94
|
-
|
|
95
|
-
* collection.add('icon-name', svg) - add new item to collection
|
|
96
|
-
* collection.remove('icon-name') - remove item from collection
|
|
97
|
-
* collection.rename('old-name', 'new-name') - rename item
|
|
98
|
-
|
|
99
|
-
where "svg" is instance of SVG class
|
|
100
|
-
|
|
101
|
-
To access any item use this:
|
|
102
|
-
|
|
103
|
-
* collection.items['item-name']
|
|
104
|
-
|
|
105
|
-
There are other helpful functions:
|
|
106
|
-
|
|
107
|
-
* collection.length() - returns number of items in collection
|
|
108
|
-
* collection.keys() - returns list of all icon names
|
|
109
|
-
|
|
110
|
-
Then there are main functions that are used to manipulate SVG instances in collection:
|
|
111
|
-
|
|
112
|
-
* collection.forEach(callback) - iterates through all SVG instances. Callback function arguments: (svg, name), where svg is SVG instance, name is icon name.
|
|
113
|
-
* collection.promiseAll(promise) - runs promise on all items in collection. It is similar to Promise.all()
|
|
114
|
-
* collection.promiseEach(promise, stopOnError) - runs promise on all items in collection, but only one at a time.
|
|
115
|
-
|
|
116
|
-
How to use forEach():
|
|
117
|
-
|
|
118
|
-
```
|
|
119
|
-
collection.forEach((svg, name) {
|
|
120
|
-
console.log('Found icon ' + name + ': ' + svg.toString());
|
|
121
|
-
});
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
How to use promiseAll() and promiseEach():
|
|
125
|
-
|
|
126
|
-
```
|
|
127
|
-
collection.promiseAll((svg, name) => {
|
|
128
|
-
return new Promise((fulfill, reject) {
|
|
129
|
-
// do stuff to "svg" variable
|
|
130
|
-
fulfill('Result for icon ' + name);
|
|
131
|
-
});
|
|
132
|
-
}).then(results => {
|
|
133
|
-
// Results of all promises as object. Key = icon name, value = result for that icon
|
|
134
|
-
|
|
135
|
-
Object.keys(results).forEach(name => {
|
|
136
|
-
console.log('Result for icon ' + name + ':', results[name]);
|
|
137
|
-
});
|
|
138
|
-
}).catch(err => {
|
|
139
|
-
console.error('Promise failed:', err);
|
|
140
|
-
});
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
promiseAll() and promiseEach() are almost identical. First argument is callback that returns Promise for one icon.
|
|
144
|
-
|
|
145
|
-
The only difference is promiseAll() runs all promises at the same time using Promise.all() function, promiseEach() runs promises one after another. It is better to use promiseEach() when dealing with large collections.
|
|
146
|
-
|
|
147
|
-
You can find examples throughout this library and unit tests. Everything in this library is based on promises.
|
|
148
|
-
|
|
149
|
-
## Importing
|
|
150
|
-
|
|
151
|
-
There are several importers available. Some import one file, some import collections.
|
|
152
|
-
|
|
153
|
-
### Importing one SVG file
|
|
154
|
-
|
|
155
|
-
```
|
|
156
|
-
tools.ImportSVG('path-to-file.svg').then(svg => {
|
|
157
|
-
// SVG was imported
|
|
158
|
-
// Variable 'svg' is instance of SVG class
|
|
159
|
-
console.log(svg.toString());
|
|
160
|
-
}).catch(err => {
|
|
161
|
-
console.error(err);
|
|
162
|
-
});
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
### Importing directory
|
|
166
|
-
|
|
167
|
-
```
|
|
168
|
-
tools.ImportDir('directory').then(collection => {
|
|
169
|
-
// Collection was imported
|
|
170
|
-
// Variable 'collection' is instance of Collection class
|
|
171
|
-
console.log('Found icons: ' + collection.keys().join(', '));
|
|
172
|
-
}).catch(err => {
|
|
173
|
-
console.error(err);
|
|
174
|
-
});
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
There are several options available for importing directory, options should be passed as second parameter to ImportDir
|
|
178
|
-
function:
|
|
179
|
-
|
|
180
|
-
* 'include-subdirs' - check subdirectories. Default = true
|
|
181
|
-
* keywordCallback - custom callback to get image name from file. It is a function with 2 arguments: function(file_without_extension, filename). Function should return string. See default function in src/import/dir.js for example
|
|
182
|
-
* ignoreDuplicates - if true, when files with duplicate keywords are found script will log a error message.
|
|
183
|
-
* ignoreFiles - array of files to ignore. Values are keywords, not file names.
|
|
184
|
-
* contentCallback - callback to change content. Use it if content contains some weird stuff you need to remove before importing SVG. function(content). Function should return modified content as string.
|
|
185
|
-
|
|
186
|
-
```
|
|
187
|
-
tools.ImportDir('directory', {
|
|
188
|
-
'include-subdirs': false,
|
|
189
|
-
ignoreFiles: ['bad-icon'],
|
|
190
|
-
keywordCallback: (file, filename) => 'prefix-' + file
|
|
191
|
-
}).then(collection => {
|
|
192
|
-
// Collection was imported
|
|
193
|
-
// Variable 'collection' is instance of Collection class
|
|
194
|
-
console.log('Found icons: ' + collection.keys().join(', '));
|
|
195
|
-
}).catch(err => {
|
|
196
|
-
console.error(err);
|
|
197
|
-
});
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
### Importing WebIcon
|
|
201
|
-
|
|
202
|
-
WebIcon format is one big SVG image that contains multiple images.
|
|
203
|
-
|
|
204
|
-
```
|
|
205
|
-
tools.ImportWebIcon('path-to-file.svg').then(collection => {
|
|
206
|
-
// Collection was imported
|
|
207
|
-
// Variable 'collection' is instance of Collection class
|
|
208
|
-
console.log('Found icons: ' + collection.keys().join(', '));
|
|
209
|
-
}).catch(err => {
|
|
210
|
-
console.error(err);
|
|
211
|
-
});
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
### Importing SVG font
|
|
215
|
-
|
|
216
|
-
There are many popular glyph fonts, such as FontAwesome, that are not available as individual files. This importer will
|
|
217
|
-
import SVG font as collection. It will not import keywords though for each icon - that is different for every
|
|
218
|
-
collection and should be done separately.
|
|
219
|
-
|
|
220
|
-
```
|
|
221
|
-
tools.ImportFont('path-to-file.svg').then(collection => {
|
|
222
|
-
// Collection was imported
|
|
223
|
-
// Variable 'collection' is instance of Collection class
|
|
224
|
-
console.log('Found icons: ' + collection.keys().join(', '));
|
|
225
|
-
}).catch(err => {
|
|
226
|
-
console.error(err);
|
|
227
|
-
});
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
There are several options available for importing SVG font, options should be passed as second parameter to ImportFont
|
|
231
|
-
function:
|
|
232
|
-
|
|
233
|
-
* ignoreCharacters - list of characters to ignore. Array
|
|
234
|
-
* characterChanges - changes for each character, object. Key is character's hexadecimal code, value is list of changes for that character.
|
|
235
|
-
* fontChanges - changes for all characters
|
|
236
|
-
|
|
237
|
-
Keys for characterChanges and fontChanges objects are similar:
|
|
238
|
-
|
|
239
|
-
* height, width - custom height and width
|
|
240
|
-
* left, bottom - custom left and bottom indexes. Why bottom instead of top? SVG fonts flip icons vertically, so height is counted from bottom
|
|
241
|
-
|
|
242
|
-
Each value can be a number or a function(oldValue) that should return new value.
|
|
243
|
-
|
|
244
|
-
Example:
|
|
245
|
-
|
|
246
|
-
```
|
|
247
|
-
tools.ImportFont('path-to-file.svg', {
|
|
248
|
-
fontChanges: {
|
|
249
|
-
height = height => Math.ceil(height / 16) * 16 // Round up height to 16px grid
|
|
250
|
-
},
|
|
251
|
-
characterChanges: {
|
|
252
|
-
f19c: { width: 1920 },
|
|
253
|
-
f0fc: { left: 64, width: 1600 },
|
|
254
|
-
}
|
|
255
|
-
}).then(collection => {
|
|
256
|
-
// Collection was imported
|
|
257
|
-
// Variable 'collection' is instance of Collection class
|
|
258
|
-
console.log('Found icons: ' + collection.keys().join(', '));
|
|
259
|
-
}).catch(err => {
|
|
260
|
-
console.error(err);
|
|
261
|
-
});
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
If you are going to crop images after import, there is no point in worrying about fixing characters. Crop will fix it (unless font is really badly messed up).
|
|
265
|
-
|
|
266
|
-
## Exporting
|
|
267
|
-
|
|
268
|
-
There are several exporters available that work with Collection or SVG instances:
|
|
269
|
-
|
|
270
|
-
### Exporting SVG
|
|
271
|
-
|
|
272
|
-
Exports one SVG instance.
|
|
273
|
-
|
|
274
|
-
```
|
|
275
|
-
tools.ExportSVG(svg, 'filename.svg').then(() => {
|
|
276
|
-
console.log('Exported!');
|
|
277
|
-
}).catch(err => {
|
|
278
|
-
console.error(err);
|
|
279
|
-
});
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
### Exporting collection to directory
|
|
283
|
-
|
|
284
|
-
```
|
|
285
|
-
tools.ExportDir(collection, 'directory').then(count => {
|
|
286
|
-
console.log('Exported ' + count + ' files');
|
|
287
|
-
}).catch(err => {
|
|
288
|
-
console.error(err);
|
|
289
|
-
});
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
### Exporting collection to JSON file
|
|
293
|
-
|
|
294
|
-
This is main export function, it exports collection to JSON format used by Iconify.
|
|
295
|
-
|
|
296
|
-
```
|
|
297
|
-
tools.ExportJSON(collection, 'filename.json').then(json => {
|
|
298
|
-
console.log('Exported collection. JSON data: ' + JSON.stringify(json));
|
|
299
|
-
}).catch(err => {
|
|
300
|
-
console.error(err);
|
|
301
|
-
});
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
### Exporting image to PNG
|
|
305
|
-
|
|
306
|
-
You can also export images to PNG.
|
|
307
|
-
|
|
308
|
-
Important: this function requires PhantomJS to be installed and accessible from command line!
|
|
309
|
-
|
|
310
|
-
```
|
|
311
|
-
tools.ExportPNG(svg, 'filename.png', {
|
|
312
|
-
// options object here
|
|
313
|
-
}).then(() => {
|
|
314
|
-
console.log('Exported filename.png');
|
|
315
|
-
}).catch(err => {
|
|
316
|
-
console.error(err);
|
|
317
|
-
});
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
Options object has following properties:
|
|
321
|
-
* width, height: dimensions of PNG image. Default = same as SVG dimensions.
|
|
322
|
-
* color: color for monotone images that use currentColor (see ChangePalette function). Default = '#000'
|
|
323
|
-
* background: background color, default = 'transparent'
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
## Manipulating
|
|
327
|
-
|
|
328
|
-
Most functions require SVG to be optimized. Therefore before doing anything else, you should optimize icon.
|
|
329
|
-
|
|
330
|
-
### SVGO optimization
|
|
331
|
-
|
|
332
|
-
This module optimizes SVG images, removing any unnecessary code and cleaning up stuff. It does not merge shapes to avoid
|
|
333
|
-
potential problems with images that rely on specific order of shapes.
|
|
334
|
-
|
|
335
|
-
```
|
|
336
|
-
tools.SVGO(svg).then(svg => {
|
|
337
|
-
// svg variable is the same as in argument. Optimizer does not create new instance of SVG class
|
|
338
|
-
console.log('Optimized SVG');
|
|
339
|
-
}).catch(err => {
|
|
340
|
-
console.error(err);
|
|
341
|
-
});
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
Want to optimize entire collection? Use collection's promiseAll function:
|
|
345
|
-
|
|
346
|
-
```
|
|
347
|
-
collection.promiseAll(svg => tools.SVGO(svg)).then(results => {
|
|
348
|
-
console.log('Optimized entire collection');
|
|
349
|
-
}).catch(err => {
|
|
350
|
-
console.error(err);
|
|
351
|
-
});
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
### Cropping images
|
|
355
|
-
|
|
356
|
-
This module crops SVG icons imported from fonts.
|
|
357
|
-
|
|
358
|
-
Important! This function requires PhantomJS to be installed. See http://phantomjs.org/
|
|
359
|
-
|
|
360
|
-
How to crop one image:
|
|
361
|
-
|
|
362
|
-
```
|
|
363
|
-
tools.Crop(svg).then(svg => {
|
|
364
|
-
console.log('Cropped SVG: ' + svg.toString());
|
|
365
|
-
}).catch(err => {
|
|
366
|
-
console.error(err);
|
|
367
|
-
});
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
How to crop entire collection:
|
|
371
|
-
|
|
372
|
-
```
|
|
373
|
-
tools.Crop(collection).then(collection => {
|
|
374
|
-
console.log('Cropped ' + svg.length() + ' images');
|
|
375
|
-
}).catch(err => {
|
|
376
|
-
console.error(err);
|
|
377
|
-
});
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
How does crop work? By drawing SVG on canvas and checking all 4 sides for transparent pixels. First it appends space on
|
|
381
|
-
all sides of SVG, until all sides have nothing but empty pixels. Then it slowly cuts all sides until it reaches pixels
|
|
382
|
-
that aren't empty. Process doesn't take long because script checks pixels in bulk by zooming in.
|
|
383
|
-
|
|
384
|
-
When it crops SVG or Collection instance (like in examples above), each SVG instance receives additional properties
|
|
385
|
-
that you might want to use later: _cropData. _cropData is an object with list of cropped edges: left, top, right, bottom.
|
|
386
|
-
|
|
387
|
-
Node.js is not a browser, it does not support canvas and cannot render SVG images. Because of that calculations are
|
|
388
|
-
done in PhantomJS and script requires PhantomJS to be installed.
|
|
389
|
-
|
|
390
|
-
### Tags validation
|
|
391
|
-
|
|
392
|
-
This module checks elements used in SVG image, changes stylesheet into inline style and removes useless attributes.
|
|
393
|
-
|
|
394
|
-
Stylesheet parser is very simple, it parses only basic stylesheets that are generated by some image editors. If SVG
|
|
395
|
-
contains fishy elements that aren't vector images, such as bitmaps or scripts, it will reject with error message.
|
|
396
|
-
|
|
397
|
-
Important! Before running this function you need to optimize SVG image using tools.SVGO()
|
|
398
|
-
|
|
399
|
-
Usage:
|
|
400
|
-
|
|
401
|
-
```
|
|
402
|
-
tools.Tags(svg).then(svg => {
|
|
403
|
-
console.log('Cleaned up tags in SVG: ' + svg.toString());
|
|
404
|
-
}).catch(err => {
|
|
405
|
-
console.error(err);
|
|
406
|
-
});
|
|
407
|
-
```
|
|
408
|
-
|
|
409
|
-
This function is used for optimizing SVG images that contain a lot of junk code.
|
|
410
|
-
|
|
411
|
-
### Extracting palette from SVG
|
|
412
|
-
|
|
413
|
-
If you don't know if SVG is colored or monotone or maybe doesn't have any colors at all, this tool will find all colors
|
|
414
|
-
used in SVG and return you array of used colors.
|
|
415
|
-
|
|
416
|
-
Result is object with 2 properties:
|
|
417
|
-
|
|
418
|
-
* colors - array of color strings
|
|
419
|
-
* notices - array of error messages
|
|
420
|
-
|
|
421
|
-
Usage:
|
|
422
|
-
|
|
423
|
-
```
|
|
424
|
-
tools.GetPalette(svg).then(result => {
|
|
425
|
-
console.log('Colors used in SVG: ' + result.colors.join(', ');
|
|
426
|
-
if (result.notices.length) {
|
|
427
|
-
result.notices.forEach(notice => console.warn(notice));
|
|
428
|
-
}
|
|
429
|
-
}).catch(err => {
|
|
430
|
-
console.error(err);
|
|
431
|
-
});
|
|
432
|
-
```
|
|
433
|
-
|
|
434
|
-
Using it with collection:
|
|
435
|
-
|
|
436
|
-
```
|
|
437
|
-
collection.promiseAll(svg => tools.GetPalette(svg)).then(result => {
|
|
438
|
-
Object.keys(results).forEach(key => {
|
|
439
|
-
console.log('Colors found in image ' + key + ': ' + result[key].colors.join(', ');
|
|
440
|
-
});
|
|
441
|
-
}).catch(err => {
|
|
442
|
-
console.error(err);
|
|
443
|
-
});
|
|
444
|
-
```
|
|
445
|
-
|
|
446
|
-
### Replacing palette in SVG
|
|
447
|
-
|
|
448
|
-
This tool can be used to change colors in SVG or add colors to shapes that are missing color values.
|
|
449
|
-
|
|
450
|
-
```
|
|
451
|
-
tools.ChangePalette(svg, {
|
|
452
|
-
'#ff8000': '#0080ff',
|
|
453
|
-
'#123': '#234'
|
|
454
|
-
}).then(svg => {
|
|
455
|
-
// Variable "svg" in result is the same as in first parameter of ChangePalette.
|
|
456
|
-
// Function changes existing SVG instance
|
|
457
|
-
console.log('Changed palette');
|
|
458
|
-
}).catch(err => {
|
|
459
|
-
console.error(err);
|
|
460
|
-
});
|
|
461
|
-
```
|
|
462
|
-
|
|
463
|
-
Code above will change #ff8000 to #0080ff and #123 to #234.
|
|
464
|
-
|
|
465
|
-
But there are more options:
|
|
466
|
-
|
|
467
|
-
```
|
|
468
|
-
tools.ChangePalette(svg, 'red').then(svg => {
|
|
469
|
-
console.log('Added palette to elements without palette');
|
|
470
|
-
}).catch(err => {
|
|
471
|
-
console.error(err);
|
|
472
|
-
});
|
|
473
|
-
```
|
|
474
|
-
|
|
475
|
-
Code above will add color "red" to all elements that do not have color. It is identical to this:
|
|
476
|
-
|
|
477
|
-
```
|
|
478
|
-
tools.ChangePalette(svg, {
|
|
479
|
-
add: 'red'
|
|
480
|
-
}).then(svg => {
|
|
481
|
-
console.log('Added palette to elements without palette');
|
|
482
|
-
}).catch(err => {
|
|
483
|
-
console.error(err);
|
|
484
|
-
});
|
|
485
|
-
```
|
|
486
|
-
|
|
487
|
-
You can also change all colors by using keyword "default":
|
|
488
|
-
|
|
489
|
-
```
|
|
490
|
-
tools.ChangePalette(svg, {
|
|
491
|
-
default: 'green'
|
|
492
|
-
}).then(svg => {
|
|
493
|
-
console.log('Changed all colors to green');
|
|
494
|
-
}).catch(err => {
|
|
495
|
-
console.error(err);
|
|
496
|
-
});
|
|
497
|
-
```
|
|
498
|
-
|
|
499
|
-
Or you can combine all those attributes:
|
|
500
|
-
|
|
501
|
-
```
|
|
502
|
-
tools.ChangePalette(svg, {
|
|
503
|
-
default: 'green',
|
|
504
|
-
add: '#000',
|
|
505
|
-
'#123': '#234
|
|
506
|
-
}).then(svg => {
|
|
507
|
-
console.log('Changed all colors to green, except for #123 that was changed to #234, added black color to elements without color');
|
|
508
|
-
}).catch(err => {
|
|
509
|
-
console.error(err);
|
|
510
|
-
});
|
|
511
|
-
```
|
|
512
|
-
|
|
513
|
-
Primary use of this tool in Iconify is to replace all colors in monotone with "currentColor" keyword and add it to shapes that are missing color values:
|
|
514
|
-
|
|
515
|
-
```
|
|
516
|
-
collection.promiseAll(svg => tools.ChangePalette(svg, {
|
|
517
|
-
default: 'currentColor',
|
|
518
|
-
add: 'currentColor'
|
|
519
|
-
})).then(() => {
|
|
520
|
-
console.log('Changed color to currentColor');
|
|
521
|
-
}).catch(err => {
|
|
522
|
-
console.error(err);
|
|
523
|
-
});
|
|
524
|
-
```
|
|
525
|
-
|
|
526
|
-
## Indexing shapes and getting shape lengths
|
|
527
|
-
|
|
528
|
-
If you are doing stroke animations, you might need to index shapes (add unique class names for them to apply different
|
|
529
|
-
animations to different shapes) and get length of those shapes.
|
|
530
|
-
|
|
531
|
-
tools.IndexShapes adds custom attribute to all shapes:
|
|
532
|
-
|
|
533
|
-
```
|
|
534
|
-
tools.IndexShapes(svg, {
|
|
535
|
-
// Attribute to set
|
|
536
|
-
shapeAttribute: 'data-shape-index',
|
|
537
|
-
// Attribute value. {index} is replaced with index, incrementing with each shape
|
|
538
|
-
shapeAttributeValue: '{index}',
|
|
539
|
-
}).then(shapesCount => {
|
|
540
|
-
console.log('Added data-shape-index attribute to ' + shapesCount + ' shapes.');
|
|
541
|
-
}).catch(err => {
|
|
542
|
-
console.error(err);
|
|
543
|
-
});
|
|
544
|
-
```
|
|
545
|
-
|
|
546
|
-
You can also get those shapes by setting option returnNodes to true. Instead of number of shapes Promise will return
|
|
547
|
-
array of nodes. Each node is cheerio node object. See cheerio documentation for details.
|
|
548
|
-
|
|
549
|
-
tools.ShapeLengths counts lengths of all shapes. It also returns all nodes, so you can change shapes (add/remove
|
|
550
|
-
attributes or class name):
|
|
551
|
-
|
|
552
|
-
```
|
|
553
|
-
tools.ShapeLength(svg).then(results => {
|
|
554
|
-
console.log('Found ' + results.length + ' shapes:');
|
|
555
|
-
results.forEach(result => {
|
|
556
|
-
console.log('Shape ' + result.$node.get(0).tagName + ' has length of ' + result.length);
|
|
557
|
-
});
|
|
558
|
-
}).catch(err => {
|
|
559
|
-
console.error(err);
|
|
560
|
-
});
|
|
561
|
-
```
|
|
562
|
-
|
|
563
|
-
## Sample
|
|
564
|
-
|
|
565
|
-
In directory "sample" you will find a sample script that parses the directory of SVG images. It optimizes images, exports
|
|
566
|
-
them as optimized SVG images and as JSON collection.
|
|
567
|
-
|
|
568
|
-
## More tools
|
|
569
|
-
|
|
570
|
-
If you need any other tool to help with custom SVG icon sets development, suggest it by opening an issue on GitHub repository.
|
|
571
|
-
|
|
572
|
-
## License
|
|
573
|
-
|
|
574
|
-
Library is released with MIT license.
|
|
575
|
-
|
|
576
|
-
© 2016 - 2019 Vjacheslav Trushkin
|
package/license.txt
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2016 - 2019 Vjacheslav Trushkin
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/sample/parse.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This is a sample file showing how to optimize SVG files.
|
|
3
|
-
*
|
|
4
|
-
* In directory "source" there are several unoptimized SVG files exported from Sketch.
|
|
5
|
-
*
|
|
6
|
-
* Run this file in Node.js: node parse
|
|
7
|
-
*
|
|
8
|
-
* It will optimize SVG images from directory "source" and save them to "optimized/svg".
|
|
9
|
-
* It will also save icons as JSON collection in SimpleSVG format, which you can use to add custom icons to SimpleSVG.
|
|
10
|
-
*/
|
|
11
|
-
"use strict";
|
|
12
|
-
|
|
13
|
-
const fs = require('fs');
|
|
14
|
-
// const tools = require('simple-svg-tools');
|
|
15
|
-
const tools = require('../src/modules');
|
|
16
|
-
|
|
17
|
-
let collection;
|
|
18
|
-
let sourceDir = 'source';
|
|
19
|
-
|
|
20
|
-
// Create directories
|
|
21
|
-
try {
|
|
22
|
-
fs.mkdirSync('optimized');
|
|
23
|
-
} catch (err) {
|
|
24
|
-
}
|
|
25
|
-
try {
|
|
26
|
-
fs.mkdirSync('optimized/svg');
|
|
27
|
-
} catch (err) {
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Do stuff
|
|
31
|
-
tools.ImportDir(sourceDir).then(result => {
|
|
32
|
-
collection = result;
|
|
33
|
-
console.log('Found ' + collection.length() + ' icons.');
|
|
34
|
-
|
|
35
|
-
// SVGO optimization
|
|
36
|
-
return collection.promiseAll(svg => tools.SVGO(svg));
|
|
37
|
-
}).then(() => {
|
|
38
|
-
// Clean up tags
|
|
39
|
-
return collection.promiseAll(svg => tools.Tags(svg));
|
|
40
|
-
}).then(() => {
|
|
41
|
-
// Get palette
|
|
42
|
-
return collection.promiseAll(svg => tools.GetPalette(svg));
|
|
43
|
-
}).then(results => {
|
|
44
|
-
// Replace colors. Remove this bit if all your images are supposed to have preset color palette.
|
|
45
|
-
let promises = [];
|
|
46
|
-
|
|
47
|
-
Object.keys(results).forEach(key => {
|
|
48
|
-
if (results[key].colors.length < 2) {
|
|
49
|
-
// Add/change color for images with less than 2 colors
|
|
50
|
-
promises.push(tools.ChangePalette(collection.items[key], {
|
|
51
|
-
add: 'currentColor',
|
|
52
|
-
default: 'currentColor'
|
|
53
|
-
}));
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
return Promise.all(promises);
|
|
58
|
-
}).then(() => {
|
|
59
|
-
// SVGO optimization again. Might make files smaller after color/tags changes
|
|
60
|
-
return collection.promiseAll(svg => tools.SVGO(svg));
|
|
61
|
-
}).then(() => {
|
|
62
|
-
// Export as optimized SVG icons
|
|
63
|
-
return tools.ExportDir(collection, 'optimized/svg');
|
|
64
|
-
}).then(() => {
|
|
65
|
-
// Export as JSON
|
|
66
|
-
return tools.ExportJSON(collection, 'optimized/icons.json', {
|
|
67
|
-
minify: false,
|
|
68
|
-
optimize: true
|
|
69
|
-
});
|
|
70
|
-
}).then(() => {
|
|
71
|
-
console.log('Parsed ' + collection.length() + ' icons.');
|
|
72
|
-
}).catch(err => {
|
|
73
|
-
console.log(err);
|
|
74
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<svg width="128px" height="128px" viewBox="0 0 128 128" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
3
|
-
<!-- Generator: Sketch 43.1 (39012) - http://www.bohemiancoding.com/sketch -->
|
|
4
|
-
<title>icon-close</title>
|
|
5
|
-
<desc>Created with Sketch.</desc>
|
|
6
|
-
<defs></defs>
|
|
7
|
-
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
8
|
-
<g id="16x16-(144)-fill" transform="translate(-152.000000, -8.000000)">
|
|
9
|
-
<g id="_slices" transform="translate(8.000000, 8.000000)"></g>
|
|
10
|
-
<g id="close" transform="translate(152.000000, 8.000000)" fill-rule="nonzero" fill="#000000">
|
|
11
|
-
<path d="M52.6862915,64 L2.34314575,114.343146 C-0.781048584,117.46734 -0.781048584,122.53266 2.34314575,125.656854 C5.46734008,128.781049 10.5326599,128.781049 13.6568542,125.656854 L64,75.3137085 L114.343146,125.656854 C117.46734,128.781049 122.53266,128.781049 125.656854,125.656854 C128.781049,122.53266 128.781049,117.46734 125.656854,114.343146 L75.3137085,64 L125.656854,13.6568542 C128.781049,10.5326599 128.781049,5.46734008 125.656854,2.34314575 C122.53266,-0.781048584 117.46734,-0.781048584 114.343146,2.34314575 L64,52.6862915 L13.6568542,2.34314575 C10.5326599,-0.781048584 5.46734008,-0.781048584 2.34314575,2.34314575 C-0.781048584,5.46734008 -0.781048584,10.5326599 2.34314575,13.6568542 L52.6862915,64 Z" id="Combined-Shape"></path>
|
|
12
|
-
</g>
|
|
13
|
-
</g>
|
|
14
|
-
</g>
|
|
15
|
-
</svg>
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<svg width="128px" height="128px" viewBox="0 0 128 128" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
3
|
-
<!-- Generator: Sketch 43.1 (39012) - http://www.bohemiancoding.com/sketch -->
|
|
4
|
-
<title>icon-confirm</title>
|
|
5
|
-
<desc>Created with Sketch.</desc>
|
|
6
|
-
<defs></defs>
|
|
7
|
-
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
8
|
-
<g id="16x16-(144)-fill" transform="translate(-296.000000, -8.000000)" fill-rule="nonzero" fill="#000000">
|
|
9
|
-
<g id="confirm" transform="translate(296.000000, 15.000000)">
|
|
10
|
-
<path d="M54.7566014,92.4428929 L13.6568542,51.3431458 C10.5326599,48.2189514 5.46734008,48.2189514 2.34314575,51.3431458 C-0.781048584,54.4673401 -0.781048584,59.5326599 2.34314575,62.6568542 L50.3431458,110.656854 C53.9128834,114.226592 59.8560698,113.6381 62.6564024,109.437602 L126.656402,13.4376016 C129.107222,9.76137206 128.113831,4.79441732 124.437602,2.34359765 C120.761372,-0.107222028 115.794417,0.886168921 113.343598,4.56239843 L54.7566014,92.4428929 Z" id="Line"></path>
|
|
11
|
-
</g>
|
|
12
|
-
</g>
|
|
13
|
-
</g>
|
|
14
|
-
</svg>
|