@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/src/shapes/length.js
DELETED
|
@@ -1,707 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the @iconify/tools package.
|
|
3
|
-
*
|
|
4
|
-
* (c) Vjacheslav Trushkin <cyberalien@gmail.com>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
const fs = require('fs');
|
|
13
|
-
const exec = require('child_process').exec;
|
|
14
|
-
const tmp = require('tmp');
|
|
15
|
-
const cheerio = require('cheerio');
|
|
16
|
-
const changeOptions = require('./options');
|
|
17
|
-
const convert = require('./convert');
|
|
18
|
-
const Index = require('./index');
|
|
19
|
-
const SVG = require('../svg');
|
|
20
|
-
const Collection = require('../collection');
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Default options. See also ./options.js
|
|
24
|
-
*
|
|
25
|
-
* @type {object}
|
|
26
|
-
*/
|
|
27
|
-
const defaults = {
|
|
28
|
-
// PhantomJS command
|
|
29
|
-
phantomjs: 'phantomjs',
|
|
30
|
-
|
|
31
|
-
// True if errors should be ignored
|
|
32
|
-
ignoreErrors: false,
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Clean up string value, removing anything that should not be there
|
|
37
|
-
*
|
|
38
|
-
* @param {string} value
|
|
39
|
-
* @return {string}
|
|
40
|
-
*/
|
|
41
|
-
function cleanupString(value) {
|
|
42
|
-
return value.replace(/[^0-9a-zA-Z\s\-+,.%]+/g, '');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Check number for percentages and convert to number
|
|
47
|
-
*
|
|
48
|
-
* @param {string} value Value as string
|
|
49
|
-
* @param {number} total Total length for percentage calculations
|
|
50
|
-
* @param {*} defaultValue Value to return if value is empty
|
|
51
|
-
* @return {number|null}
|
|
52
|
-
*/
|
|
53
|
-
function normalizeNumber(value, total, defaultValue) {
|
|
54
|
-
if (value === '') {
|
|
55
|
-
return defaultValue;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
let units = value.replace(/^[0-9.-]+/g, '');
|
|
59
|
-
value = parseFloat(value);
|
|
60
|
-
if (isNaN(value)) {
|
|
61
|
-
return defaultValue;
|
|
62
|
-
}
|
|
63
|
-
switch (units) {
|
|
64
|
-
case '':
|
|
65
|
-
return value;
|
|
66
|
-
|
|
67
|
-
case '%':
|
|
68
|
-
return (value * total) / 100;
|
|
69
|
-
|
|
70
|
-
default:
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Normalize node attributes
|
|
77
|
-
*
|
|
78
|
-
* @param $node
|
|
79
|
-
* @param {object} params List of attributes. Key = name, typeof value = type
|
|
80
|
-
* @param {object} [defaults] Default values for missing attributes
|
|
81
|
-
* @return {object|null} List of attributes as object, null on error
|
|
82
|
-
*/
|
|
83
|
-
function normalizeAttributes($node, params, defaults) {
|
|
84
|
-
defaults = defaults === void 0 ? Object.create(null) : defaults;
|
|
85
|
-
|
|
86
|
-
let results = Object.create(null),
|
|
87
|
-
keys = Object.keys(params),
|
|
88
|
-
i,
|
|
89
|
-
value,
|
|
90
|
-
key;
|
|
91
|
-
|
|
92
|
-
function checkDefault(key) {
|
|
93
|
-
if (defaults[key] === void 0) {
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
results[key] = defaults[key];
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Check all attributes
|
|
101
|
-
for (i = 0; i < keys.length; i++) {
|
|
102
|
-
key = keys[i];
|
|
103
|
-
value = $node.attr(key);
|
|
104
|
-
if (value === void 0) {
|
|
105
|
-
// Attribute is missing
|
|
106
|
-
if (!checkDefault(key)) {
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (typeof value === typeof params[key]) {
|
|
113
|
-
if (typeof value === 'string') {
|
|
114
|
-
value = cleanupString(value);
|
|
115
|
-
}
|
|
116
|
-
results[key] = value;
|
|
117
|
-
continue;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
switch (typeof params[key]) {
|
|
121
|
-
case 'number':
|
|
122
|
-
value = parseFloat(value);
|
|
123
|
-
if (isNaN(value)) {
|
|
124
|
-
if (!checkDefault(key)) {
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
} else {
|
|
128
|
-
results[key] = value;
|
|
129
|
-
}
|
|
130
|
-
break;
|
|
131
|
-
|
|
132
|
-
case 'boolean':
|
|
133
|
-
switch (typeof value) {
|
|
134
|
-
case 'string':
|
|
135
|
-
value = value.toLowerCase();
|
|
136
|
-
results[key] = value === '1' || value === 'true' || value === key;
|
|
137
|
-
break;
|
|
138
|
-
|
|
139
|
-
default:
|
|
140
|
-
results[key] = !!value;
|
|
141
|
-
}
|
|
142
|
-
break;
|
|
143
|
-
|
|
144
|
-
default:
|
|
145
|
-
// Wrong type
|
|
146
|
-
if (!checkDefault(key)) {
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return results;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Functions for calculating length of shapes
|
|
157
|
-
*/
|
|
158
|
-
let calculate = Object.create(null);
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Calculate line length
|
|
162
|
-
*
|
|
163
|
-
* @param attributes
|
|
164
|
-
* @return {number}
|
|
165
|
-
*/
|
|
166
|
-
function calcLineLength(attributes) {
|
|
167
|
-
return Math.sqrt(
|
|
168
|
-
Math.pow(attributes.x2 - attributes.x1, 2) +
|
|
169
|
-
Math.pow(attributes.y2 - attributes.y1, 2)
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Length of <line>
|
|
175
|
-
*
|
|
176
|
-
* @param $element
|
|
177
|
-
* @return {number|boolean}
|
|
178
|
-
*/
|
|
179
|
-
calculate.line = $element => {
|
|
180
|
-
let attributes = normalizeAttributes(
|
|
181
|
-
$element,
|
|
182
|
-
{
|
|
183
|
-
x1: 0,
|
|
184
|
-
x2: 0,
|
|
185
|
-
y1: 0,
|
|
186
|
-
y2: 0,
|
|
187
|
-
},
|
|
188
|
-
{
|
|
189
|
-
x1: 0,
|
|
190
|
-
x2: 0,
|
|
191
|
-
y1: 0,
|
|
192
|
-
y2: 0,
|
|
193
|
-
}
|
|
194
|
-
);
|
|
195
|
-
if (attributes === null) {
|
|
196
|
-
return false;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return calcLineLength(attributes);
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Calculate length of <circle>
|
|
204
|
-
*
|
|
205
|
-
* @param $element
|
|
206
|
-
* @return {number|boolean}
|
|
207
|
-
*/
|
|
208
|
-
calculate.circle = $element => {
|
|
209
|
-
let attributes = normalizeAttributes($element, {
|
|
210
|
-
r: 0,
|
|
211
|
-
});
|
|
212
|
-
if (attributes === null) {
|
|
213
|
-
return false;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return 2 * Math.PI * attributes.r;
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Calculate length of <rect>
|
|
221
|
-
*
|
|
222
|
-
* @param $element
|
|
223
|
-
* @return {number|boolean}
|
|
224
|
-
*/
|
|
225
|
-
calculate.rect = $element => {
|
|
226
|
-
let attributes = normalizeAttributes(
|
|
227
|
-
$element,
|
|
228
|
-
{
|
|
229
|
-
width: 0,
|
|
230
|
-
height: 0,
|
|
231
|
-
rx: 0,
|
|
232
|
-
ry: 0,
|
|
233
|
-
},
|
|
234
|
-
{
|
|
235
|
-
rx: '',
|
|
236
|
-
ry: '',
|
|
237
|
-
}
|
|
238
|
-
);
|
|
239
|
-
if (attributes === null) {
|
|
240
|
-
return false;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if (attributes.width === 0 || attributes.height === 0) {
|
|
244
|
-
return 0;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (attributes.rx !== '' || attributes.ry !== '') {
|
|
248
|
-
// Complex calculation - convert to shape and use PhantomJS to calculate length
|
|
249
|
-
return false;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return (attributes.width + attributes.height) * 2;
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Calculate length of polygon or polyline
|
|
257
|
-
*
|
|
258
|
-
* @param {string} points
|
|
259
|
-
* @param {boolean} closed
|
|
260
|
-
* @return {number|boolean}
|
|
261
|
-
*/
|
|
262
|
-
function calcPolyLength(points, closed) {
|
|
263
|
-
let result = 0,
|
|
264
|
-
startX,
|
|
265
|
-
startY,
|
|
266
|
-
lastX,
|
|
267
|
-
lastY;
|
|
268
|
-
|
|
269
|
-
points = typeof points === 'string' ? points.split(/\s+/) : points;
|
|
270
|
-
points.forEach((point, index) => {
|
|
271
|
-
let coords =
|
|
272
|
-
typeof point === 'string'
|
|
273
|
-
? point.split(',').map(value => parseFloat(value))
|
|
274
|
-
: point;
|
|
275
|
-
if (coords.length !== 2 || isNaN(coords[0]) || isNaN(coords[1])) {
|
|
276
|
-
throw new Error('Bad coordinates');
|
|
277
|
-
}
|
|
278
|
-
if (!index) {
|
|
279
|
-
startX = lastX = coords[0];
|
|
280
|
-
startY = lastY = coords[1];
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
result += calcLineLength({
|
|
285
|
-
x1: lastX,
|
|
286
|
-
y1: lastY,
|
|
287
|
-
x2: coords[0],
|
|
288
|
-
y2: coords[1],
|
|
289
|
-
});
|
|
290
|
-
lastX = coords[0];
|
|
291
|
-
lastY = coords[1];
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
if (closed) {
|
|
295
|
-
result += calcLineLength({
|
|
296
|
-
x1: lastX,
|
|
297
|
-
y1: lastY,
|
|
298
|
-
x2: startX,
|
|
299
|
-
y2: startY,
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
return result;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Calculate length of <polyline>
|
|
308
|
-
*
|
|
309
|
-
* @param $element
|
|
310
|
-
* @return {number|boolean}
|
|
311
|
-
*/
|
|
312
|
-
calculate.polyline = $element => {
|
|
313
|
-
let attributes = normalizeAttributes($element, {
|
|
314
|
-
points: '',
|
|
315
|
-
});
|
|
316
|
-
if (attributes === null) {
|
|
317
|
-
return false;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
try {
|
|
321
|
-
return calcPolyLength(attributes.points, false);
|
|
322
|
-
} catch (err) {
|
|
323
|
-
return 0;
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Calculate length of <polygon>
|
|
329
|
-
*
|
|
330
|
-
* @param $element
|
|
331
|
-
* @return {number|boolean}
|
|
332
|
-
*/
|
|
333
|
-
calculate.polygon = $element => {
|
|
334
|
-
let attributes = normalizeAttributes($element, {
|
|
335
|
-
points: '',
|
|
336
|
-
});
|
|
337
|
-
if (attributes === null) {
|
|
338
|
-
return false;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
try {
|
|
342
|
-
return calcPolyLength(attributes.points, true);
|
|
343
|
-
} catch (err) {
|
|
344
|
-
return 0;
|
|
345
|
-
}
|
|
346
|
-
};
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* Functions to convert shapes to path.
|
|
350
|
-
*
|
|
351
|
-
* Functions get attributes from node, check attributes and call converter function
|
|
352
|
-
*
|
|
353
|
-
* @type {object}
|
|
354
|
-
*/
|
|
355
|
-
let converter = Object.create(null),
|
|
356
|
-
converterData = {
|
|
357
|
-
rect: {
|
|
358
|
-
attr: {
|
|
359
|
-
x: 0,
|
|
360
|
-
y: 0,
|
|
361
|
-
width: 0,
|
|
362
|
-
height: 0,
|
|
363
|
-
rx: '',
|
|
364
|
-
ry: '',
|
|
365
|
-
},
|
|
366
|
-
def: {
|
|
367
|
-
x: 0,
|
|
368
|
-
y: 0,
|
|
369
|
-
rx: '',
|
|
370
|
-
ry: '',
|
|
371
|
-
},
|
|
372
|
-
test: attributes => {
|
|
373
|
-
if (
|
|
374
|
-
(attributes.rx = normalizeNumber(
|
|
375
|
-
attributes.rx,
|
|
376
|
-
attributes.width,
|
|
377
|
-
'auto'
|
|
378
|
-
)) === null
|
|
379
|
-
) {
|
|
380
|
-
return false;
|
|
381
|
-
}
|
|
382
|
-
if (
|
|
383
|
-
(attributes.ry = normalizeNumber(
|
|
384
|
-
attributes.ry,
|
|
385
|
-
attributes.height,
|
|
386
|
-
'auto'
|
|
387
|
-
)) === null
|
|
388
|
-
) {
|
|
389
|
-
return false;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
// rx and rx are not set
|
|
393
|
-
if (attributes.rx === 'auto' && attributes.ry === 'auto') {
|
|
394
|
-
attributes.rx = attributes.ry = 0;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
// rx and ry are set
|
|
398
|
-
if (attributes.rx !== 'auto' && attributes.ry !== 'auto') {
|
|
399
|
-
return true;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
// one attribute is set
|
|
403
|
-
if (attributes.ry === 'auto') {
|
|
404
|
-
// rx is set
|
|
405
|
-
if (attributes.width === 0) {
|
|
406
|
-
return false;
|
|
407
|
-
}
|
|
408
|
-
attributes.ry =
|
|
409
|
-
(attributes.height * attributes.rx) / attributes.width;
|
|
410
|
-
} else {
|
|
411
|
-
// ry is set
|
|
412
|
-
if (attributes.height === 0) {
|
|
413
|
-
return false;
|
|
414
|
-
}
|
|
415
|
-
attributes.rx =
|
|
416
|
-
(attributes.width * attributes.ry) / attributes.height;
|
|
417
|
-
}
|
|
418
|
-
return true;
|
|
419
|
-
},
|
|
420
|
-
},
|
|
421
|
-
circle: {
|
|
422
|
-
attr: {
|
|
423
|
-
cx: 0,
|
|
424
|
-
cy: 0,
|
|
425
|
-
r: 0,
|
|
426
|
-
},
|
|
427
|
-
def: {
|
|
428
|
-
cx: 0,
|
|
429
|
-
cy: 0,
|
|
430
|
-
},
|
|
431
|
-
},
|
|
432
|
-
ellipse: {
|
|
433
|
-
attr: {
|
|
434
|
-
cx: 0,
|
|
435
|
-
cy: 0,
|
|
436
|
-
rx: 0,
|
|
437
|
-
ry: 0,
|
|
438
|
-
},
|
|
439
|
-
def: {
|
|
440
|
-
cx: 0,
|
|
441
|
-
cy: 0,
|
|
442
|
-
},
|
|
443
|
-
},
|
|
444
|
-
line: {
|
|
445
|
-
attr: {
|
|
446
|
-
x1: 0,
|
|
447
|
-
x2: 0,
|
|
448
|
-
y1: 0,
|
|
449
|
-
y2: 0,
|
|
450
|
-
},
|
|
451
|
-
def: {
|
|
452
|
-
x1: 0,
|
|
453
|
-
x2: 0,
|
|
454
|
-
y1: 0,
|
|
455
|
-
y2: 0,
|
|
456
|
-
},
|
|
457
|
-
},
|
|
458
|
-
polyline: {
|
|
459
|
-
attr: {
|
|
460
|
-
points: '',
|
|
461
|
-
},
|
|
462
|
-
},
|
|
463
|
-
polygon: {
|
|
464
|
-
attr: {
|
|
465
|
-
points: '',
|
|
466
|
-
},
|
|
467
|
-
},
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
/**
|
|
471
|
-
* Convert path to... path
|
|
472
|
-
*
|
|
473
|
-
* @param $element
|
|
474
|
-
* @return {string|boolean}
|
|
475
|
-
*/
|
|
476
|
-
converter.path = $element => {
|
|
477
|
-
let attributes = normalizeAttributes($element, {
|
|
478
|
-
d: '',
|
|
479
|
-
});
|
|
480
|
-
if (attributes === null) {
|
|
481
|
-
return false;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
return attributes.d;
|
|
485
|
-
};
|
|
486
|
-
|
|
487
|
-
/**
|
|
488
|
-
* Convert all other shapes to path
|
|
489
|
-
*
|
|
490
|
-
* @param $element
|
|
491
|
-
* @return {string|boolean}
|
|
492
|
-
*/
|
|
493
|
-
Object.keys(converterData).forEach(key => {
|
|
494
|
-
let data = converterData[key];
|
|
495
|
-
converter[key] = $element => {
|
|
496
|
-
let attributes = normalizeAttributes($element, data.attr, data.def);
|
|
497
|
-
if (attributes === null) {
|
|
498
|
-
return false;
|
|
499
|
-
}
|
|
500
|
-
if (data.test !== void 0 && data.test(attributes) === false) {
|
|
501
|
-
return false;
|
|
502
|
-
}
|
|
503
|
-
return convert[key](attributes);
|
|
504
|
-
};
|
|
505
|
-
});
|
|
506
|
-
|
|
507
|
-
/**
|
|
508
|
-
* Find lengths of all shapes
|
|
509
|
-
*
|
|
510
|
-
* Must run SVGO optimization before using this module!
|
|
511
|
-
*
|
|
512
|
-
* @param {SVG|Collection} svg SVG or Collection object
|
|
513
|
-
* @param {object} options Options
|
|
514
|
-
* @return {Promise}
|
|
515
|
-
*/
|
|
516
|
-
module.exports = (svg, options) => {
|
|
517
|
-
return new Promise((fulfill, reject) => {
|
|
518
|
-
options = options === void 0 ? Object.create(null) : options;
|
|
519
|
-
changeOptions(options, defaults);
|
|
520
|
-
|
|
521
|
-
let shapes = Object.create(null),
|
|
522
|
-
queue = [],
|
|
523
|
-
tempKeyAttribute = '_lengthKey';
|
|
524
|
-
|
|
525
|
-
// Copy items
|
|
526
|
-
let collection = svg instanceof Collection;
|
|
527
|
-
let items;
|
|
528
|
-
if (collection) {
|
|
529
|
-
items = svg;
|
|
530
|
-
items.forEach((svg, key) => {
|
|
531
|
-
svg[tempKeyAttribute] = key;
|
|
532
|
-
shapes[key] = [];
|
|
533
|
-
});
|
|
534
|
-
} else {
|
|
535
|
-
items = new Collection();
|
|
536
|
-
svg[tempKeyAttribute] = 'icon';
|
|
537
|
-
items.add('icon', svg);
|
|
538
|
-
shapes.icon = [];
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
// Copy options to overwrite shapeCallback
|
|
542
|
-
let indexOptions = Object.assign(Object.create(null), options);
|
|
543
|
-
indexOptions.checkFillStroke = true;
|
|
544
|
-
|
|
545
|
-
// Callback for finding shapes
|
|
546
|
-
indexOptions.shapeCallback = item => {
|
|
547
|
-
let key = item.svg[tempKeyAttribute],
|
|
548
|
-
result = {
|
|
549
|
-
node: item.$node,
|
|
550
|
-
index: item.index,
|
|
551
|
-
fill: item.fill,
|
|
552
|
-
stroke: item.stroke,
|
|
553
|
-
},
|
|
554
|
-
returnValue = false;
|
|
555
|
-
|
|
556
|
-
shapes[key].push(result);
|
|
557
|
-
|
|
558
|
-
// Callback
|
|
559
|
-
if (options.shapeCallback !== null) {
|
|
560
|
-
returnValue = options.shapeCallback(item);
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
// Calculate length
|
|
564
|
-
if (calculate[item.tag] !== void 0) {
|
|
565
|
-
let length = calculate[item.tag](item.$node);
|
|
566
|
-
if (length !== false) {
|
|
567
|
-
result.length = length;
|
|
568
|
-
return returnValue;
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
if (converter[item.tag] === void 0) {
|
|
573
|
-
// Unknown tag
|
|
574
|
-
if (!options.ignoreErrors) {
|
|
575
|
-
throw new Error(
|
|
576
|
-
'Cannot calculate length for tag "' +
|
|
577
|
-
item.tag +
|
|
578
|
-
'"' +
|
|
579
|
-
(collection ? ' in icon ' + item.svg[tempKeyAttribute] : '') +
|
|
580
|
-
'.'
|
|
581
|
-
);
|
|
582
|
-
}
|
|
583
|
-
return false;
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
// Get shape
|
|
587
|
-
let path = converter[item.tag](item.$node);
|
|
588
|
-
if (path === false || path === '') {
|
|
589
|
-
// Bad shape
|
|
590
|
-
if (!options.ignoreErrors) {
|
|
591
|
-
throw new Error(
|
|
592
|
-
'Shape "' +
|
|
593
|
-
item.tag +
|
|
594
|
-
'"' +
|
|
595
|
-
(collection ? ' in icon ' + item.svg[tempKeyAttribute] : '') +
|
|
596
|
-
' has bad attributes.'
|
|
597
|
-
);
|
|
598
|
-
}
|
|
599
|
-
return false;
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
// Queue for parsing
|
|
603
|
-
result.queueIndex = queue.indexOf(path);
|
|
604
|
-
if (result.queueIndex === -1) {
|
|
605
|
-
result.queueIndex = queue.length;
|
|
606
|
-
queue.push(path);
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
return returnValue;
|
|
610
|
-
};
|
|
611
|
-
|
|
612
|
-
// Find all shapes
|
|
613
|
-
items
|
|
614
|
-
.promiseAll(svg => Index(svg, indexOptions))
|
|
615
|
-
.then(() => {
|
|
616
|
-
if (!queue.length) {
|
|
617
|
-
// Resolved all shapes
|
|
618
|
-
done();
|
|
619
|
-
return;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
// Some shapes are too complex, running real browser to calculate their length
|
|
623
|
-
tmp.setGracefulCleanup();
|
|
624
|
-
let sourceFile = tmp.tmpNameSync({ postfix: '.json' }),
|
|
625
|
-
targetFile = tmp.tmpNameSync({ postfix: '.json' });
|
|
626
|
-
|
|
627
|
-
fs.writeFileSync(sourceFile, JSON.stringify(queue), 'utf8');
|
|
628
|
-
// fs.writeFileSync('_src.' + Date.now() + '.json', JSON.stringify(queue, null, '\t'), 'utf8');
|
|
629
|
-
|
|
630
|
-
// Execute
|
|
631
|
-
let params = [__dirname + '/length_script.js', sourceFile, targetFile];
|
|
632
|
-
// console.log('Exec params:', params);
|
|
633
|
-
|
|
634
|
-
exec(
|
|
635
|
-
options.phantomjs +
|
|
636
|
-
' ' +
|
|
637
|
-
params.map(param => '"' + param + '"').join(' '),
|
|
638
|
-
{
|
|
639
|
-
cwd: __dirname,
|
|
640
|
-
},
|
|
641
|
-
(error, stdout, stderr) => {
|
|
642
|
-
if (error) {
|
|
643
|
-
console.log(error);
|
|
644
|
-
reject(
|
|
645
|
-
'Error executing phantomjs script. Make sure phantomjs is installed'
|
|
646
|
-
);
|
|
647
|
-
return;
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
let data;
|
|
651
|
-
try {
|
|
652
|
-
data = fs.readFileSync(targetFile, 'utf8');
|
|
653
|
-
// fs.writeFileSync('_data.' + Date.now() + '.json', data, 'utf8');
|
|
654
|
-
data = JSON.parse(data);
|
|
655
|
-
} catch (err) {
|
|
656
|
-
reject('Error executing phantomjs script: temp file is missing');
|
|
657
|
-
return;
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
if (
|
|
661
|
-
typeof data !== 'object' ||
|
|
662
|
-
!(data instanceof Array) ||
|
|
663
|
-
queue.length !== data.length
|
|
664
|
-
) {
|
|
665
|
-
reject('Error executing phantomjs script: wrong result');
|
|
666
|
-
return;
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
// Match data and shapes
|
|
670
|
-
Object.keys(shapes).forEach(key => {
|
|
671
|
-
shapes[key].forEach(shape => {
|
|
672
|
-
if (shape.queueIndex !== void 0) {
|
|
673
|
-
shape.length = data[shape.queueIndex];
|
|
674
|
-
}
|
|
675
|
-
});
|
|
676
|
-
});
|
|
677
|
-
|
|
678
|
-
done();
|
|
679
|
-
}
|
|
680
|
-
);
|
|
681
|
-
})
|
|
682
|
-
.catch(err => {
|
|
683
|
-
reject(err);
|
|
684
|
-
});
|
|
685
|
-
|
|
686
|
-
/**
|
|
687
|
-
* Create results, fulfill promise
|
|
688
|
-
*/
|
|
689
|
-
function done() {
|
|
690
|
-
let results = Object.create(null);
|
|
691
|
-
|
|
692
|
-
Object.keys(shapes).forEach(key => {
|
|
693
|
-
results[key] = [];
|
|
694
|
-
shapes[key].forEach(shape => {
|
|
695
|
-
results[key].push({
|
|
696
|
-
length: shape.length,
|
|
697
|
-
fill: shape.fill,
|
|
698
|
-
stroke: shape.stroke,
|
|
699
|
-
$node: shape.node,
|
|
700
|
-
});
|
|
701
|
-
});
|
|
702
|
-
});
|
|
703
|
-
|
|
704
|
-
fulfill(collection ? results : results.icon);
|
|
705
|
-
}
|
|
706
|
-
});
|
|
707
|
-
};
|