@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/optimize/crop.js
DELETED
|
@@ -1,554 +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 SVGO = require('./svgo');
|
|
16
|
-
const SVG = require('../svg');
|
|
17
|
-
const Collection = require('../collection');
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Default options
|
|
21
|
-
*
|
|
22
|
-
* @type {object}
|
|
23
|
-
*/
|
|
24
|
-
const defaults = {
|
|
25
|
-
// True if SVG needs to be optimized before cropping. Should be enabled for SVG with transformations because
|
|
26
|
-
// PhantomJS has some bugs dealing with transformations
|
|
27
|
-
optimize: false,
|
|
28
|
-
|
|
29
|
-
// True if "svg" is list of multiple images {key: svg}
|
|
30
|
-
multiple: false,
|
|
31
|
-
|
|
32
|
-
// Round values to grid
|
|
33
|
-
round: true,
|
|
34
|
-
|
|
35
|
-
// Cache file
|
|
36
|
-
cropCache: '',
|
|
37
|
-
|
|
38
|
-
// PhantomJS command
|
|
39
|
-
phantomjs: 'phantomjs',
|
|
40
|
-
|
|
41
|
-
// Format of result. Values: 'collection' = Collection instance, 'svg' = SVG instance, 'string' = string, 'object' = object, '' = auto
|
|
42
|
-
format: '',
|
|
43
|
-
|
|
44
|
-
// Grid to round sides to. null = automatic, false = none, value in pixels = fixed value
|
|
45
|
-
defaultCropGrid: null,
|
|
46
|
-
|
|
47
|
-
// Limit cropping to descent property. Ignored for objects that do not have descent
|
|
48
|
-
defaultCropLimit: null,
|
|
49
|
-
|
|
50
|
-
// Callback for crop: function(item)
|
|
51
|
-
// item = {source: {}, result: {}, options}
|
|
52
|
-
// Modifies item.result
|
|
53
|
-
cropCallback: null,
|
|
54
|
-
|
|
55
|
-
// Separate limits for each side, null = ignored
|
|
56
|
-
defaultLeftLimit: null,
|
|
57
|
-
defaultRightLimit: null,
|
|
58
|
-
defaultTopLimit: null,
|
|
59
|
-
defaultBottomLimit: null,
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Generate SVG code
|
|
64
|
-
*
|
|
65
|
-
* @param width
|
|
66
|
-
* @param height
|
|
67
|
-
* @param body
|
|
68
|
-
* @return {string}
|
|
69
|
-
*/
|
|
70
|
-
function generateSVG(width, height, body) {
|
|
71
|
-
return (
|
|
72
|
-
'<svg ' +
|
|
73
|
-
'width="' +
|
|
74
|
-
width +
|
|
75
|
-
'" ' +
|
|
76
|
-
'height="' +
|
|
77
|
-
height +
|
|
78
|
-
'" ' +
|
|
79
|
-
'viewBox="0 0 ' +
|
|
80
|
-
width +
|
|
81
|
-
' ' +
|
|
82
|
-
height +
|
|
83
|
-
'" ' +
|
|
84
|
-
'xmlns="http://www.w3.org/2000/svg" ' +
|
|
85
|
-
'xmlns:xlink="http://www.w3.org/1999/xlink"' +
|
|
86
|
-
'>' +
|
|
87
|
-
body +
|
|
88
|
-
'</svg>'
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Crop SVG
|
|
94
|
-
*
|
|
95
|
-
* @param {object} svg SVG object or collection. It can also be SVG item or list of items with properties "body", "width", "ascent", "descent"
|
|
96
|
-
* @param {object} [options]
|
|
97
|
-
* @return {Promise}
|
|
98
|
-
*/
|
|
99
|
-
module.exports = (svg, options) => {
|
|
100
|
-
return new Promise((fulfill, reject) => {
|
|
101
|
-
options = options === void 0 ? Object.create(null) : options;
|
|
102
|
-
Object.keys(defaults).forEach(key => {
|
|
103
|
-
if (options[key] === void 0) {
|
|
104
|
-
options[key] = defaults[key];
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
let cache = Object.create(null),
|
|
109
|
-
results = [];
|
|
110
|
-
|
|
111
|
-
let items = [],
|
|
112
|
-
collection = null;
|
|
113
|
-
|
|
114
|
-
// Check for collection
|
|
115
|
-
if (svg instanceof Collection) {
|
|
116
|
-
options.multiple = true;
|
|
117
|
-
collection = svg;
|
|
118
|
-
svg = collection.items;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Convert items to array
|
|
122
|
-
if (!options.multiple) {
|
|
123
|
-
items.push({
|
|
124
|
-
key: 'icon',
|
|
125
|
-
source: svg,
|
|
126
|
-
});
|
|
127
|
-
} else {
|
|
128
|
-
Object.keys(svg).forEach(key => {
|
|
129
|
-
items.push({
|
|
130
|
-
key: key,
|
|
131
|
-
source: svg[key],
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Convert SVG objects to strings or clone object and adjust properties
|
|
137
|
-
items.forEach(item => {
|
|
138
|
-
if (typeof item.source === 'string') {
|
|
139
|
-
let obj = new SVG(item.source);
|
|
140
|
-
item.source = {
|
|
141
|
-
body: obj.getBody(),
|
|
142
|
-
width: obj.width,
|
|
143
|
-
height: obj.height,
|
|
144
|
-
left: obj.left,
|
|
145
|
-
top: obj.top,
|
|
146
|
-
};
|
|
147
|
-
} else if (item.source instanceof SVG) {
|
|
148
|
-
item.source = {
|
|
149
|
-
body: item.source.getBody(),
|
|
150
|
-
width: item.source.width,
|
|
151
|
-
height: item.source.height,
|
|
152
|
-
left: item.source.left,
|
|
153
|
-
top: item.source.top,
|
|
154
|
-
};
|
|
155
|
-
} else {
|
|
156
|
-
item.source = Object.assign(Object.create(null), item.source);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (item.source.grid === void 0 && options.defaultCropGrid !== null) {
|
|
160
|
-
item.source.grid = options.defaultCropGrid;
|
|
161
|
-
}
|
|
162
|
-
if (item.source.limit === void 0) {
|
|
163
|
-
if (options.defaultCropLimit !== null) {
|
|
164
|
-
item.source.limit = options.defaultCropLimit;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
['Left', 'Right', 'Top', 'Bottom'].forEach(side => {
|
|
168
|
-
if (
|
|
169
|
-
item.source['limit' + side] === void 0 &&
|
|
170
|
-
options['default' + side + 'Limit'] !== null
|
|
171
|
-
) {
|
|
172
|
-
item.source['limit' + side] = options['default' + side + 'Limit'];
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
// Check cache
|
|
178
|
-
if (options.cropCache && options.multiple) {
|
|
179
|
-
try {
|
|
180
|
-
let data = JSON.parse(fs.readFileSync(options.cropCache, 'utf8'));
|
|
181
|
-
if (typeof data === 'object') {
|
|
182
|
-
cache = data;
|
|
183
|
-
|
|
184
|
-
let found = false;
|
|
185
|
-
items.forEach(item => {
|
|
186
|
-
if (!item.key || cache[item.key] === void 0) {
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
let cachedData = cache[item.key];
|
|
190
|
-
if (!cachedData.source || !cachedData.result || !cachedData.body) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Check if all source attributes match
|
|
195
|
-
let match = true;
|
|
196
|
-
let keys = Object.keys(item.source);
|
|
197
|
-
if (Object.keys(cachedData.source).length !== keys.length) {
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
keys.forEach(key => {
|
|
201
|
-
if (cachedData.source[key] !== item.source[key]) {
|
|
202
|
-
match = false;
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
if (!match) {
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// Match!
|
|
210
|
-
Object.keys(cachedData).forEach(attr => {
|
|
211
|
-
if (attr !== 'source') {
|
|
212
|
-
item[attr] = cachedData[attr];
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
results.push(item);
|
|
216
|
-
found = true;
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
if (found) {
|
|
220
|
-
// Check if all items have been resolved
|
|
221
|
-
items = items.filter(item => item.result === void 0);
|
|
222
|
-
if (!items.length) {
|
|
223
|
-
// All results have been cached
|
|
224
|
-
returnResults();
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
} catch (err) {}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Optimize images before cropping, if option is enabled
|
|
234
|
-
*/
|
|
235
|
-
if (options.optimize) {
|
|
236
|
-
let promises = [];
|
|
237
|
-
items.forEach(item => {
|
|
238
|
-
let source = item.source,
|
|
239
|
-
height,
|
|
240
|
-
width;
|
|
241
|
-
|
|
242
|
-
if (source.descent === void 0 || source.descent > 0) {
|
|
243
|
-
height = source.height === void 0 ? source.ascent : source.height;
|
|
244
|
-
} else {
|
|
245
|
-
height =
|
|
246
|
-
source.height === void 0
|
|
247
|
-
? source.ascent - source.descent
|
|
248
|
-
: source.height;
|
|
249
|
-
}
|
|
250
|
-
width =
|
|
251
|
-
source.width === void 0
|
|
252
|
-
? source['horiz-adv-x'] === void 0
|
|
253
|
-
? height
|
|
254
|
-
: source['horiz-adv-x']
|
|
255
|
-
: source.width;
|
|
256
|
-
|
|
257
|
-
promises.push(
|
|
258
|
-
SVGO(new SVG(generateSVG(width, height, item.source.body)), {
|
|
259
|
-
'id-prefix': null,
|
|
260
|
-
})
|
|
261
|
-
);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
Promise.all(promises)
|
|
265
|
-
.then(results => {
|
|
266
|
-
results.forEach((result, index) => {
|
|
267
|
-
items[index].body = result.getBody();
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
cropImages();
|
|
271
|
-
})
|
|
272
|
-
.catch(err => {
|
|
273
|
-
reject(err);
|
|
274
|
-
});
|
|
275
|
-
} else {
|
|
276
|
-
cropImages();
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Crop images
|
|
281
|
-
*/
|
|
282
|
-
function cropImages() {
|
|
283
|
-
// Generate source data
|
|
284
|
-
let data = Object.create(null);
|
|
285
|
-
items.forEach(item => {
|
|
286
|
-
data[item.key] = item.source;
|
|
287
|
-
if (item.body !== void 0) {
|
|
288
|
-
// Copy object and overwrite body
|
|
289
|
-
data[item.key] = Object.assign(Object.create(null), data[item.key], {
|
|
290
|
-
body: item.body,
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
// Files
|
|
296
|
-
tmp.setGracefulCleanup();
|
|
297
|
-
let sourceFile = tmp.tmpNameSync({ postfix: '.json' }),
|
|
298
|
-
targetFile = tmp.tmpNameSync({ postfix: '.json' });
|
|
299
|
-
|
|
300
|
-
fs.writeFileSync(sourceFile, JSON.stringify(data), 'utf8');
|
|
301
|
-
// fs.writeFileSync('_src.' + Date.now() + '.json', JSON.stringify(data, null, '\t'), 'utf8');
|
|
302
|
-
|
|
303
|
-
// Execute
|
|
304
|
-
let params = [__dirname + '/crop_script.js', sourceFile, targetFile];
|
|
305
|
-
// console.log('Exec params:', params);
|
|
306
|
-
|
|
307
|
-
exec(
|
|
308
|
-
options.phantomjs +
|
|
309
|
-
' ' +
|
|
310
|
-
params.map(param => '"' + param + '"').join(' '),
|
|
311
|
-
{
|
|
312
|
-
cwd: __dirname,
|
|
313
|
-
},
|
|
314
|
-
(error, stdout, stderr) => {
|
|
315
|
-
if (error) {
|
|
316
|
-
console.log(error);
|
|
317
|
-
reject(
|
|
318
|
-
'Error executing phantomjs script. Make sure phantomjs is installed'
|
|
319
|
-
);
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
let data;
|
|
324
|
-
try {
|
|
325
|
-
data = fs.readFileSync(targetFile, 'utf8');
|
|
326
|
-
// fs.writeFileSync('_data.' + Date.now() + '.json', data, 'utf8');
|
|
327
|
-
data = JSON.parse(data);
|
|
328
|
-
} catch (err) {
|
|
329
|
-
reject('Error executing phantomjs script: temp file is missing');
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
if (typeof data !== 'object') {
|
|
334
|
-
reject('Error executing phantomjs script: wrong result');
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
// Set data
|
|
339
|
-
let found = false;
|
|
340
|
-
items.forEach(item => {
|
|
341
|
-
if (data[item.key] !== void 0) {
|
|
342
|
-
item.result = data[item.key];
|
|
343
|
-
found = true;
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
if (!found) {
|
|
348
|
-
reject('Error executing phantomjs script: no matches');
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
// Check for missing items
|
|
353
|
-
if (items.filter(item => item.result === void 0).length) {
|
|
354
|
-
reject('Not all images have been cropped');
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
cropResults();
|
|
359
|
-
}
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Crop results and move origin to 0 0
|
|
365
|
-
*/
|
|
366
|
-
function cropResults() {
|
|
367
|
-
// fs.writeFileSync(__dirname + '/_results' + Object.keys(items).length + '.json', JSON.stringify(items, null, '\t'));
|
|
368
|
-
|
|
369
|
-
let promises = [],
|
|
370
|
-
indexes = [];
|
|
371
|
-
items.forEach((item, index) => {
|
|
372
|
-
// Adjust to grid
|
|
373
|
-
let result = item.result;
|
|
374
|
-
|
|
375
|
-
if (options.cropCallback !== null) {
|
|
376
|
-
options.cropCallback(result, options);
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
if (options.round) {
|
|
380
|
-
let grid = result.grid;
|
|
381
|
-
result.left = Math.floor(result.left / grid) * grid;
|
|
382
|
-
result.top = Math.floor(result.top / grid) * grid;
|
|
383
|
-
result.right = Math.ceil(result.right / grid) * grid;
|
|
384
|
-
result.bottom = Math.ceil(result.bottom / grid) * grid;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
if (result.left !== 0 || result.top !== 0) {
|
|
388
|
-
let width = result.right - result.left,
|
|
389
|
-
height = result.bottom - result.top,
|
|
390
|
-
body =
|
|
391
|
-
'<g transform="translate(' +
|
|
392
|
-
(0 - result.left) +
|
|
393
|
-
' ' +
|
|
394
|
-
(0 - result.top) +
|
|
395
|
-
')">' +
|
|
396
|
-
(item.body === void 0 ? item.source.body : item.body) +
|
|
397
|
-
'</g>';
|
|
398
|
-
|
|
399
|
-
indexes.push(index);
|
|
400
|
-
promises.push(
|
|
401
|
-
SVGO(new SVG(generateSVG(width, height, body)), {
|
|
402
|
-
'id-prefix': null,
|
|
403
|
-
})
|
|
404
|
-
);
|
|
405
|
-
}
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
if (promises.length) {
|
|
409
|
-
Promise.all(promises)
|
|
410
|
-
.then(results => {
|
|
411
|
-
results.forEach((result, index) => {
|
|
412
|
-
items[indexes[index]].body = result.getBody();
|
|
413
|
-
});
|
|
414
|
-
mergeAndCacheResults();
|
|
415
|
-
returnResults();
|
|
416
|
-
})
|
|
417
|
-
.catch(err => {
|
|
418
|
-
reject(err);
|
|
419
|
-
});
|
|
420
|
-
} else {
|
|
421
|
-
mergeAndCacheResults();
|
|
422
|
-
returnResults();
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* Merge and cache results
|
|
428
|
-
*/
|
|
429
|
-
function mergeAndCacheResults() {
|
|
430
|
-
results = results.concat(items);
|
|
431
|
-
|
|
432
|
-
if (options.cropCache && options.multiple) {
|
|
433
|
-
results.forEach(item => {
|
|
434
|
-
cache[item.key] = item;
|
|
435
|
-
});
|
|
436
|
-
try {
|
|
437
|
-
fs.writeFileSync(
|
|
438
|
-
options.cropCache,
|
|
439
|
-
JSON.stringify(cache, null, '\t'),
|
|
440
|
-
'utf8'
|
|
441
|
-
);
|
|
442
|
-
} catch (err) {}
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
/**
|
|
447
|
-
* Parse data, return results
|
|
448
|
-
*/
|
|
449
|
-
function returnResults() {
|
|
450
|
-
let data;
|
|
451
|
-
|
|
452
|
-
/**
|
|
453
|
-
* Generate result object
|
|
454
|
-
*
|
|
455
|
-
* @param item
|
|
456
|
-
* @return {*}
|
|
457
|
-
*/
|
|
458
|
-
function generateResultObject(item) {
|
|
459
|
-
return Object.assign(
|
|
460
|
-
{
|
|
461
|
-
body: !item.body ? item.source.body : item.body,
|
|
462
|
-
left: 0,
|
|
463
|
-
top: 0,
|
|
464
|
-
width: item.result.right - item.result.left,
|
|
465
|
-
height: item.result.bottom - item.result.top,
|
|
466
|
-
},
|
|
467
|
-
item.result
|
|
468
|
-
);
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* Generate result
|
|
473
|
-
*
|
|
474
|
-
* @param data
|
|
475
|
-
* @param format
|
|
476
|
-
* @param original
|
|
477
|
-
* @param item
|
|
478
|
-
* @return {*}
|
|
479
|
-
*/
|
|
480
|
-
function generateResultItem(data, format, original, item) {
|
|
481
|
-
switch (format) {
|
|
482
|
-
case 'string':
|
|
483
|
-
return generateSVG(data.width, data.height, data.body);
|
|
484
|
-
|
|
485
|
-
case 'svg':
|
|
486
|
-
case 'collection':
|
|
487
|
-
let content = generateSVG(data.width, data.height, data.body),
|
|
488
|
-
result;
|
|
489
|
-
|
|
490
|
-
if (original instanceof SVG) {
|
|
491
|
-
original.load(content);
|
|
492
|
-
result = original;
|
|
493
|
-
} else {
|
|
494
|
-
result = new SVG(content);
|
|
495
|
-
}
|
|
496
|
-
// {left, top, right, bottom, grid}
|
|
497
|
-
result._cropData = item.result;
|
|
498
|
-
return result;
|
|
499
|
-
|
|
500
|
-
case 'object':
|
|
501
|
-
return Object.assign(
|
|
502
|
-
typeof original === 'object' ? original : Object.create(null),
|
|
503
|
-
data
|
|
504
|
-
);
|
|
505
|
-
|
|
506
|
-
default:
|
|
507
|
-
return generateResultItem(
|
|
508
|
-
data,
|
|
509
|
-
typeof original === 'string'
|
|
510
|
-
? 'string'
|
|
511
|
-
: original instanceof SVG
|
|
512
|
-
? 'svg'
|
|
513
|
-
: 'object',
|
|
514
|
-
original,
|
|
515
|
-
item
|
|
516
|
-
);
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
// Return in same format as source: string, SVG instance or object
|
|
521
|
-
let result;
|
|
522
|
-
if (!options.multiple) {
|
|
523
|
-
result = generateResultItem(
|
|
524
|
-
generateResultObject(results[0]),
|
|
525
|
-
options.format,
|
|
526
|
-
svg,
|
|
527
|
-
results[0]
|
|
528
|
-
);
|
|
529
|
-
} else {
|
|
530
|
-
// Check for collection object
|
|
531
|
-
// Make shallow copy to keep old stuff
|
|
532
|
-
svg = Object.assign(Object.create(null), svg);
|
|
533
|
-
results.forEach(item => {
|
|
534
|
-
if (
|
|
535
|
-
typeof svg[item.key] === 'object' &&
|
|
536
|
-
!(svg[item.key] instanceof SVG)
|
|
537
|
-
) {
|
|
538
|
-
svg[item.key] = Object.assign(Object.create(null), svg[item.key]);
|
|
539
|
-
}
|
|
540
|
-
svg[item.key] = generateResultItem(
|
|
541
|
-
generateResultObject(item),
|
|
542
|
-
options.format,
|
|
543
|
-
svg[item.key] === void 0 ? Object.create(null) : svg[item.key],
|
|
544
|
-
item
|
|
545
|
-
);
|
|
546
|
-
});
|
|
547
|
-
|
|
548
|
-
// Check for collection object
|
|
549
|
-
result = collection !== null ? collection : svg;
|
|
550
|
-
}
|
|
551
|
-
fulfill(result);
|
|
552
|
-
}
|
|
553
|
-
});
|
|
554
|
-
};
|