-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgenerate.js
292 lines (258 loc) · 12.4 KB
/
generate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
var mapnik = require('mapnik');
var assert = require('assert');
var ShelfPack = require('@mapbox/shelf-pack');
var queue = require('queue-async');
var extractMetadata = require('./extract-svg-metadata');
var emptyPNG = new mapnik.Image(1, 1).encodeSync('png');
module.exports.generateLayout = generateLayout;
module.exports.generateLayoutUnique = generateLayoutUnique;
module.exports.generateImage = generateImage;
module.exports.generateOptimizedImage = generateOptimizedImage;
function heightAscThanNameComparator(a, b) {
return (b.height - a.height) || ((a.id === b.id) ? 0 : (a.id < b.id ? -1 : 1));
}
/**
* Pack a list of images with width and height into a sprite layout.
* options object with the following keys:
*
* @param {Object} [options]
* @param {Object[]} [options.imgs] Array of `{ svg: Buffer, id: String }`
* @param {number} [options.pixelRatio] Ratio of a 72dpi screen pixel to the destination pixel density
* @param {boolean} [options.format] If true, generate {@link DataLayout}; if false, generate {@link ImgLayout}
* @param {boolean} [options.maxIconSize] optional, overrides the max_size in mapnik
* @param {boolean} [options.removeOversizedIcons] optional, if set, filters out icons that mapnik says are too big
* @param {boolean} [options.extractMetadata] optional, defaults to true; if set to false, omits stretch and placeholder metadata (`content`, `stretchX`, `stretchY`, `placeholder`) in {@link DataLayout}
* @param {Function} callback Accepts two arguments, `err` and `layout` Object
* @return {DataLayout|ImgLayout} layout Generated Layout Object with sprite contents
*/
function generateLayout(options, callback) {
options = options || {};
options.unique = false;
options.extractMetadata = options.extractMetadata === undefined ? true : options.extractMetadata;
return generateLayoutInternal(options, callback);
}
/**
* Same as generateLayout but can be used to dedupe identical SVGs
* and still preserve the reference.
*
* For example if `A.svg` and `B.svg` are identical, a single icon
* will be in the sprite image and both A and B will reference the same image
*
* options object with the following keys:
*
* @param {Object} [options]
* @param {Object[]} [options.imgs] Array of `{ svg: Buffer, id: String }`
* @param {number} [options.pixelRatio] Ratio of a 72dpi screen pixel to the destination pixel density
* @param {boolean} [options.format] If true, generate {@link DataLayout} as first argument to callback and {@link ImgLayout} as the second argument; if false, generate only {@link ImgLayout} as the first argument to the callback function
* @param {boolean} [options.maxIconSize] optional, overrides the max_size in mapnik
* @param {boolean} [options.removeOversizedIcons] optional, if set, filters out icons that mapnik says are too big
* @param {boolean} [options.extractMetadata] optional, defaults to true; if set to false, omits stretch and placeholder metadata (`content`, `stretchX`, `stretchY`, `placeholder`) in {@link DataLayout}
* @param {Function} callback Accepts two arguments, `err` and `layout` Object
* @return {DataLayout|ImgLayout} layout Generated Layout Object with sprite contents
*/
function generateLayoutUnique(options, callback) {
options = options || {};
options.unique = true;
options.extractMetadata = options.extractMetadata === undefined ? true : options.extractMetadata;
return generateLayoutInternal(options, callback);
}
/**
* Internally called by `generateLayout()` and `generateLayoutUnique()`
*
* @private
* @param {Object} [options]
* @param {Object[]} [options.imgs] Array of `{ svg: Buffer, id: String }`
* @param {number} [options.pixelRatio] Ratio of a 72dpi screen pixel to the destination pixel density
* @param {boolean} [options.format] If true, generate {@link DataLayout} as first argument to callback and {@link ImgLayout} as the second argument; if false, generate only {@link ImgLayout} as the first argument to the callback function
* @param {boolean} [options.unique] If true, deduplicate identical SVG images
* @param {boolean} [options.maxIconSize] optional, overrides the max_size in mapnik
* @param {boolean} [options.removeOversizedIcons] optional, if set, filters out icons that mapnik says are too big
* @param {boolean} [options.extractMetadata] optional, defaults to true; if set to false, omits stretch metadata (`content`, `stretchX`, `stretchY`) in {@link DataLayout}
* @param {Function} callback Accepts two arguments, `err` and `layout` Object
* @return {DataLayout|ImgLayout} layout Generated Layout Object with sprite contents
*/
function generateLayoutInternal(options, callback) {
assert(typeof options.pixelRatio === 'number' && Array.isArray(options.imgs));
if (options.unique) {
/* If 2 items are pointing to identical buffers (svg icons)
* create a single image in the sprite but have all ids point to it
* Remove duplicates from imgs, but if format == true then when creating the
* resulting layout, make sure all item that had the same signature
* of an item are also updated with the same layout information.
*/
/* The svg signature of each item */
var svgPerItemId = {};
/* The items for each SVG signature */
var itemIdsPerSvg = {};
options.imgs.forEach((item) => {
var svg = item.svg.toString('base64');
svgPerItemId[item.id] = svg;
if (svg in itemIdsPerSvg) {
itemIdsPerSvg[svg].push(item.id);
} else {
itemIdsPerSvg[svg] = [item.id];
}
});
/* Only keep 1 item per svg signature for packing */
options.imgs = options.imgs.filter((item) => {
var svg = svgPerItemId[item.id];
return item.id === itemIdsPerSvg[svg][0];
});
}
function createImages(img, callback) {
var mapnikOpts = { scale: options.pixelRatio };
if (options.maxIconSize) {
mapnikOpts.max_size = options.maxIconSize;
}
mapnik.Image.fromSVGBytes(img.svg, mapnikOpts, (err, image) => {
if (err && err.message.match(/image created from svg must be \d+ pixels or fewer on each side/) && options.removeOversizedIcons) return callback(null, null);
// Produce a null result if no width or height attributes. The error message from mapnik has a typo "then"; account for potential future fix to "than".
if (err && err.message.match(/image created from svg must have a width and height greater (then|than) zero/)) return callback(null, null);
if (err) return callback(err);
if (!image.width() || !image.height()) return callback(null, null);
// For the data layout JSON (when options.format is true), extract stretch and placeholder metadata if present
if (
options.extractMetadata &&
options.format &&
(img.svg.includes('mapbox-stretch') || img.svg.includes('mapbox-text-placeholder'))
) {
const metaOps = { svg: img.svg, pixelRatio: options.pixelRatio };
extractMetadata(metaOps, (err, metadataProps) => {
return callback(err, {
...img,
width: image.width(),
height: image.height(),
buffer: image,
...metadataProps
});
});
} else {
return callback(null, {
...img,
width: image.width(),
height: image.height(),
buffer: image
});
}
});
}
var q = new queue();
options.imgs.forEach((img) => {
q.defer(createImages, img);
});
q.awaitAll((err, imagesWithSizes) => {
if (err) return callback(err);
// remove nulls that get introduced if removeOversizedIcons is true
imagesWithSizes = imagesWithSizes.filter((img) => img);
imagesWithSizes.sort(heightAscThanNameComparator);
var sprite = new ShelfPack(1, 1, { autoResize: true });
sprite.pack(imagesWithSizes, { inPlace: true });
// object needed for generateImage
const imageLayout = {
width: sprite.w,
height: sprite.h,
items: imagesWithSizes
};
if (options.format) {
var dataLayout = {};
imagesWithSizes.forEach((item) => {
var itemIdsToUpdate = [item.id];
if (options.unique) {
var svg = svgPerItemId[item.id];
itemIdsToUpdate = itemIdsPerSvg[svg];
}
itemIdsToUpdate.forEach((itemIdToUpdate) => {
dataLayout[itemIdToUpdate] = {
width: item.width,
height: item.height,
x: item.x,
y: item.y,
pixelRatio: options.pixelRatio
};
['content', 'placeholder', 'stretchX', 'stretchY'].forEach(key => {
if (item[key]) { dataLayout[itemIdToUpdate][key] = item[key]; }
});
});
});
return callback(null, dataLayout, imageLayout);
} else {
return callback(null, imageLayout);
}
});
}
/**
* Generate a PNG image with positioned icons on a sprite.
*
* @param {ImgLayout} layout An {@link ImgLayout} Object used to generate the image
* @param {Function} callback Accepts two arguments, `err` and `image` data
*/
function generateImage(layout, callback) {
assert(typeof layout === 'object' && typeof callback === 'function');
if (!layout.items.length) return callback(null, emptyPNG);
mapnik.blend(layout.items, {
width: layout.width,
height: layout.height
}, callback);
}
/**
* Generate a PNG image with positioned icons on a sprite.
*
* @param {ImgLayout} layout An {@link ImgLayout} Object used to generate the image
* @param {Object} [options]
* @param {Object[]} [options.quality] Number of colors to crush the PNG to (using color quantization) (default 128)
* @param {Function} callback Accepts two arguments, `err` and `image` data
*/
function generateOptimizedImage(layout, options, callback) {
assert(typeof layout === 'object' && typeof options === 'object' && typeof callback === 'function');
if (!layout.items.length) return callback(null, emptyPNG);
mapnik.blend(layout.items, {
type: 'png',
quality: options.quality || 128,
mode: 'hextree',
width: layout.width,
height: layout.height
}, callback);
}
/**
* Spritezero can generate 2 kinds of layout objects: {@link DataLayout} and {@link ImgLayout}.
*
* A `ImgLayout` Object contains the array of image items along with dimensions
* and a buffer of image data that can be used for generating the output image.
*
* @typedef {Object} ImgLayout
* @example
* {
* width: 512,
* height: 512,
* items: [
* {
* "height": 12,
* "width": 12,
* "x": 133,
* "y": 282,
* "buffer": "..."
* }, ... etc ...
* ]
* }
*/
/**
* Spritezero can generate 2 kinds of layout objects: {@link DataLayout} and {@link ImgLayout}.
*
* A `DataLayout` Object contains all the metadata about the contents of the sprite.
* This data can be exported to a JSON sprite manifest file.
*
* The keys of the Object are the icon ids.
* The values of the Object are the structured data about each icon.
*
* @typedef {Object} DataLayout
* @example
* {
* "aerialway-12": {
* "width": 12,
* "height": 12,
* "pixelRatio": 1,
* "x": 133,
* "y": 282
* }, ... etc ...
* }
*/