-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
executable file
·342 lines (309 loc) · 9.57 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env node
var path = require('path');
var async = require('async');
var _ = { defaults: require('lodash.defaults') };
var isGulp = !!module.parent;
if (isGulp) {
var through = require('through2');
var File = require('vinyl');
module.exports = getter;
} else {
var fs = require('fs');
var mkdirp = require('mkdirp');
}
var defaultOptions = {
cssFilename: 'fonts.css',
fontsDir: './',
cssDir: './',
outBaseDir: '',
relativePaths: false,
fontDisplayType: "auto",
host: 'fonts.googleapis.com',
hostPath: 'css',
protocol: 'https',
format: 'woff'
};
var formatData = {
'woff': {
'extension': 'woff',
'agent': 'Mozilla/4.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36',
'format': 'woff'
},
'woff2': {
'extension': 'woff2',
'agent': 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
'format': 'woff2'
},
'svg': {
'extension': 'svg',
'agent': 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16',
'format': 'svg'
},
'eot': {
'extension': 'eot',
'agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)',
'format': undefined, // Meaning no format clause is generated.
},
'ttf': {
'extension': 'ttf',
'agent': 'Unknown',
'format': 'truetype'
}
};
var debug = process.env.debug || process.env.DEBUG;
function verbose(msg) {
if (debug) {
process.stderr.write(msg + '\n');
}
}
if (!isGulp) {
var cmd = require('commander')
.version(require('./package.json').version)
.option('--css-filename [filename]', 'Name for output CSS file', defaultOptions.cssFilename)
.option('--css-dir [path]', 'CSS output directory', defaultOptions.fontsDir)
.option('--fonts-dir [path]', 'Fonts output directory', defaultOptions.cssDir)
.option('--font-display-type [type]', 'Font display type', defaultOptions.fontDisplayType)
.option('--out-base-dir [path]', 'Base path to output directory, prepended to cssDir/fontsDir', defaultOptions.outBaseDir)
.option('--host [domain]', 'Host to query ("fonts.googleapis.com")', defaultOptions.host)
.option('--host-path [path]', 'Host path for query ("css")', defaultOptions.hostPath)
.option('--protocol [protocol]', 'Protocol used for requests [https|http]', defaultOptions.protocol)
.option('--format [format]', 'Format to retrieve [woff|woff2|svg|eot|ttf]', defaultOptions.format)
.option('-v, --verbose', 'Verbose output', false)
.parse(process.argv)
;
debug = cmd.debug;
var src = '';
process.stdin
.on('data', function (buf) { src += buf.toString(); })
.on('end', function () {
getter(cmd)
(src, null, function (err) { if (err) { throw err; } else { process.exit(0); } });
});
}
function getter(options) {
options = _.defaults({}, options, defaultOptions);
if (isGulp) {
if (options.outBaseDir) {
throw new Error('outBaseDir only valid when run from command line, use gulp.dest instead');
}
}
http = require(options.protocol);
return isGulp ? through.obj(processor) : processor;
function processor(file, enc, next) {
var self = this;
function writeFile(filename, contents, next) {
if (options.outBaseDir) {
filename = path.join(options.outBaseDir, filename);
}
verbose('Writing ' + contents.length + ' bytes to "' + filename + '"');
if (isGulp) {
writeFileToGulpStream(filename, contents, next);
} else {
writeFileToDisk(filename, contents, next);
}
return;
function writeFileToGulpStream(filename, contents, next) {
self.push(new File({
path: filename,
contents: contents
}));
next(null, null);
}
function writeFileToDisk(filename, contents, next) {
async.series([
async.apply(mkdirp, path.dirname(filename)),
async.apply(fs.writeFile, filename, contents, null)
], next);
}
}
var data;
if (isGulp) {
if (file.isNull()) {
return self.emit('data', file);
}
if (file.isStream()) {
return self.emit('error', new Error('webfont-getter: Streaming not supported'));
}
data = file.contents.toString(enc);
} else {
data = file;
}
var subsets = [];
var param = data
.split('\n')
.map(function (s) { return s.trim(); })
.filter(function (s) { return s.length > 0 && s.charAt(0) !== '#'; })
.map(parseLine)
.join('|')
.replace(/^\|*|\|*$/g, '');
async.waterfall([initial(param), requestCss, receiveCss, parseCss, downloadFonts], next);
function parseLine(line) {
if (line.indexOf('\t') === -1) {
/* Extract subsets if specified */
var ss = line.match(/&subset=.*$/);
if (ss) {
line = line.substr(0, ss.index);
addSubsets(ss[0].substr(8).split(','));
}
return line.replace(/ /g, '+');
} else {
return parseTabDelimetedLine(line);
}
}
function parseTabDelimetedLine(line) {
var fields = line.split('\t');
var face = fields[0].replace(/ /g, '+');
var style = fields[1] || '400';
var subset = fields[2];
if (subset) {
addSubsets(subset.split(','));
}
return face + ':' + style;
}
function addSubsets(s) {
if (!s || !s.length) {
return;
}
s.forEach(function (subset) {
if (subsets.indexOf(subset) === -1) {
subsets.push(subset);
}
});
}
function initial(value) {
return function (next) { return next(null, value); };
}
function requestCss(param, next) {
if (subsets.length) {
param = param + '&subset=' + subsets.join(',');
}
var req = {
host: options.host,
path: '/' + options.hostPath + '?family=' + param,
headers: {
'User-Agent': formatData[options.format].agent
}
};
verbose('GET ' + req.host + req.path);
http
.get(req, async.apply(next, null))
.on('error', next);
}
function receiveCss(res, next) {
var css = [];
verbose('HTTP ' + res.statusCode);
res.on('data', function (data) { css.push(data.toString()); });
res.on('error', next);
res.on('end', function () { next(null, css.join('')); });
}
function parseCss(css, next) {
var ext = formatData[options.format].extension;
if (css.substr(0, 2) === '<!') {
return next(new Error('Failed to retrieve webfont CSS'));
}
var fontFaces = extractFontFaceBlocks(css);
var classes = extractClasses(css);
var requests = [];
for (var i=0; i<fontFaces.length; i++) {
requests.push(extractData(fontFaces[i], ext));
}
generateFontCss(requests, classes, next);
function extractData(block, ext) {
// We cannot additionally require 'ext' to be part of URL pattern
// since Google doesn't put there '.svg' suffix for some reason
// (but does that for other extensions).
var re = new RegExp([
"\\s*font-family:\\s*'(?<family>[^']+)';",
"\\s*font-style:\\s*(?<style>\\w+);",
"\\s*font-weight:\\s*(?<weight>\\w+);",
".*(?:\\s*font-stretch:\\s(?<stretch>[^;]+);)?",
"\\s*src:(?<src>[^;]*);",
".*(?:unicode-range:(?<range>[^;]+);)?",
].join(''), 'm');
let found = block.match(re, 'm');
if (found === null) {
throw new Error("faild to match block");
}
return formatData(found.groups);
function formatData(data) {
const { family, style, weight, stretch, src, range } = data;
var name = [family, style, weight].join('-') + '.' + ext;
var url = src.match(/url\(([^)]+)\)/)[1];
var locals = src.match(/local\([^)]+\)/g) || [];
return {
family: family,
style: style,
weight: weight,
name: name.replace(/\s/g, '_'),
url: url,
locals: locals,
stretch: stretch || 'normal',
range: range || 'U+0-10FFFF'
};
}
}
function extractFontFaceBlocks(css) {
return css.match(/@font-face\s*{[^}]+}/g);
}
function extractClasses(css) {
return css.match(/\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*\s*{[^}]+}/g);
}
}
function generateFontCss(requests, classes, next) {
var ext = formatData[options.format].format;
var format = ext ? ' format(\'' + ext + '\')' : '';
var template = [
'@font-face {',
' font-family: \'$family\';',
' font-style: $style;',
' font-weight: $weight;',
' font-display: ' + options.fontDisplayType + ';',
' font-stretch: $stretch;',
' src: $src' + format + ';',
' unicode-range: $range;',
'}'
].join('\n');
var css = requests
.map(makeFontFace)
.concat(classes)
.join('\n\n');
writeFile(
path.join(options.cssDir, options.cssFilename),
Buffer.from(css),
function (err) { next(err, requests); }
);
function makeFontFace(request) {
var uri = path.posix.join(
options.relativePaths ? path.posix.relative(options.cssDir, options.fontsDir) : options.fontsDir,
request.name
);
request.src = request.locals.map(local => local + ', ').join('') + 'url(' + uri + ')';
request.name = path.posix.join(options.fontsDir, request.name);
return template
.replace(/\$(\w+)/g, function (m, name) {
return request[name];
});
}
}
function downloadFonts(requests) {
async.each(requests, downloadFont, next);
function downloadFont(request, next) {
async.waterfall([initial(request), requestFont, emitFont], next);
function requestFont(obj, next) {
http
.get(obj.url, async.apply(next, null, obj.name))
.on('error', next);
}
function emitFont(name, res, next) {
if (res.statusCode !== 200) {
next(new Error('HTTP GET returned code ' + res.statusCode + ' for ' + name));
return;
}
var data = [];
res.on('data', function (chunk) { data.push(chunk); });
res.on('end', function () { writeFile(name, Buffer.concat(data), next); });
}
}
}
}
}