forked from mapbox/makizushi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (158 loc) · 5.54 KB
/
index.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
var fs = require('fs'),
path = require('path'),
blend = require('@mapbox/blend'),
xtend = require('xtend'),
errcode = require('err-code');
maki = require('maki');
var markerCache = require('./cache');
var offsets = {
's': {x:4,y:4},
'm': {x:6,y:5},
'l': {x:5,y:7},
's@2x': {x:8,y:8},
'm@2x': {x:12,y:10},
'l@2x': {x:10,y:14}
},
sizes = { s: 12, m: 18, l: 24 },
makiRenders = maki.dirname + '/renders/';
var makiAvailable = fs.readdirSync(makiRenders)
.reduce(function(mem, file) {
mem[file.replace('.png', '')] = true;
return mem;
}, {});
module.exports = getMarker;
/**
* Given a marker object like
*
* { base, tint, symbol, name }
*
* Call callback with buffer.
*
* @param {object} options
* @param {function} callback
*/
function getMarker(options, callback) {
// prevent .parsedTint from being attached to options
options = xtend({}, options);
if (options.tint) {
// Expand hex shorthand (3 chars) to 6, e.g. 333 => 333333.
// This is not done upstream in `node-tint` as some such
// shorthand cannot be disambiguated from other tintspec strings,
// e.g. 123 (rgb shorthand) vs. 123 (hue).
if (options.tint.length === 3) {
options.tint =
options.tint[0] + options.tint[0] +
options.tint[1] + options.tint[1] +
options.tint[2] + options.tint[2];
}
options.parsedTint = blend.parseTintString(options.tint);
}
if (!options.symbol ||
(options.symbol && options.symbol.length === 1) ||
(options.symbol.length === 2 && !isNaN(parseInt(options.symbol)))) {
loadCached(options, callback);
} else {
loadMaki(options, callback);
}
}
/**
* Load & composite a marker from the maki icon set.
*
* @param {object} options
* @param {function} callback
*/
function loadMaki(options, callback) {
var base = options.base + '-' + options.size + (options.retina ? '@2x' : ''),
size = options.size,
symbol = options.symbol + '-' + sizes[size] + (options.retina ? '@2x' : '');
if (!base || !size) {
return callback(errcode('Marker is invalid because it lacks base or size.', 'EINVALID'));
}
if (!makiAvailable[symbol]) {
return callback(errcode('Marker symbol "' + options.symbol + '" is invalid.', 'EINVALID'));
}
fs.readFile(makiRenders + symbol + '.png', function(err, data) {
if (err) return callback(new Error('Marker "' + JSON.stringify(options) + '" is invalid because the symbol is not found.'));
// Base marker gets tint applied.
var parts = [{
buffer: markerCache.base[base],
tint: options.parsedTint
}];
// If symbol is present, find correct offset (varies by marker size).
if (symbol) {
parts.push(xtend({
buffer: data,
tint: blend.parseTintString('0x0;0x0;1.4x0'),
}, offsets[size + (options.retina ? '@2x' : '')]));
}
// Add mask layer.
parts.push({
buffer: markerCache.mask[base]
});
// Extract width and height from the IHDR. The IHDR chunk must appear
// first, so the location is always fixed.
var width = markerCache.base[base].readUInt32BE(16),
height = markerCache.base[base].readUInt32BE(20);
// Combine base, (optional) symbol, to supply the final marker.
blend(parts, {
format: 'png',
quality: 256,
width: width,
height: height
}, function(err, data) {
if (err) return callback(err);
return callback(null, data);
});
});
}
/**
* Load & generate a cached [a-z0-9] marker.
*
* @param {object} options
* @param {function} callback
*/
function loadCached(options, callback) {
var base = options.base + '-' + options.size + (options.retina ? '@2x' : ''),
size = options.size,
symbol;
if (options.symbol) {
symbol = options.symbol + '-' + options.size + (options.retina ? '@2x' : '');
}
if (!base || !size) {
return callback(errcode('Marker is invalid because it lacks base or size.', 'EINVALID'));
}
if (!markerCache.base[base]) {
return callback(errcode('Marker base "' + options.base + '" is invalid.', 'EINVALID'));
}
if (symbol && !markerCache.symbol[symbol]) {
return callback(errcode('Marker symbol "' + options.symbol + '" is invalid.', 'EINVALID'));
}
// Base marker gets tint applied.
var parts = [{
buffer: markerCache.base[base],
tint: options.parsedTint
}];
// If symbol is present, find correct offset (varies by marker size).
if (symbol) {
parts.push(xtend({
buffer: markerCache.symbol[symbol],
tint: blend.parseTintString('0x0;0x0;1.4x0'),
}, offsets[size + (options.retina ? '@2x' : '')]));
}
// Add mask layer.
parts.push({ buffer:markerCache.mask[base] });
// Extract width and height from the IHDR. The IHDR chunk must appear
// first, so the location is always fixed.
var width = markerCache.base[base].readUInt32BE(16),
height = markerCache.base[base].readUInt32BE(20);
// Combine base, (optional) symbol, to supply the final marker.
blend(parts, {
format: 'png',
quality: 256,
width: width,
height: height
}, function(err, data) {
if (err) return callback(err);
return callback(null, data);
});
}