This repository was archived by the owner on May 29, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathnode.js
225 lines (196 loc) · 4.94 KB
/
node.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
/**
* grunt-webfont: Node.js engine
*
* @requires ttfautohint 1.00+ (optional)
* @author Artem Sapegin (http://sapegin.me)
*/
module.exports = function(o, allDone) {
'use strict';
var fs = require('fs');
var path = require('path');
var async = require('async');
var temp = require('temp');
var exec = require('child_process').exec;
var _ = require('lodash');
var StringDecoder = require('string_decoder').StringDecoder;
var SVGIcons2SVGFontStream = require('svgicons2svgfont');
var svg2ttf = require('svg2ttf');
var ttf2woff = require('ttf2woff');
var ttf2eot = require('ttf2eot');
var SVGO = require('svgo');
var MemoryStream = require('memorystream');
var logger = o.logger || require('winston');
var wf = require('../util/util');
// @todo Ligatures
var fonts = {};
var generators = {
svg: function(done) {
var font = '';
var decoder = new StringDecoder('utf8');
svgFilesToStreams(o.files, function(streams) {
var stream = new SVGIcons2SVGFontStream({
fontName: o.fontFamilyName,
fontHeight: o.fontHeight,
descent: o.descent,
normalize: o.normalize,
round: o.round,
log: logger.verbose.bind(logger),
error: logger.error.bind(logger)
});
stream.on('data', function(chunk) {
font += decoder.write(chunk);
}).on('finish',function() {
fonts.svg = font;
done(font);
});
streams.forEach(function(glyph) {
stream.write(glyph);
});
stream.end();
});
},
ttf: function(done) {
getFont('svg', function(svgFont) {
var font = svg2ttf(svgFont, {});
font = new Buffer(font.buffer);
autohintTtfFont(font, function(hintedFont) {
// ttfautohint is optional
if (hintedFont) {
font = hintedFont;
}
fonts.ttf = font;
done(font);
});
});
},
woff: function(done) {
getFont('ttf', function(ttfFont) {
var font = ttf2woff(new Uint8Array(ttfFont), {});
font = new Buffer(font.buffer);
fonts.woff = font;
done(font);
});
},
woff2: function(done) {
// Will be converted from TTF later
done();
},
eot: function(done) {
getFont('ttf', function(ttfFont) {
var font = ttf2eot(new Uint8Array(ttfFont));
font = new Buffer(font.buffer);
fonts.eot = font;
done(font);
});
}
};
var steps = [];
// Font types
var typesToGenerate = o.types.slice();
if (o.types.indexOf('woff2') !== -1 && o.types.indexOf('ttf' === -1)) typesToGenerate.push('ttf');
typesToGenerate.forEach(function(type) {
steps.push(createFontWriter(type));
});
// Run!
async.waterfall(steps, allDone);
function getFont(type, done) {
if (fonts[type]) {
done(fonts[type]);
}
else {
generators[type](done);
}
}
function createFontWriter(type) {
return function(done) {
getFont(type, function(font) {
fs.writeFileSync(wf.getFontPath(o, type), font);
done();
});
};
}
function svgFilesToStreams(files, done) {
async.map(files, function(file, fileDone) {
function fileStreamed(name, stream) {
stream.metadata = {
unicode: [String.fromCodePoint(o.codepoints[name])],
name: name
};
if (o.addLigatures) {
stream.metadata.unicode.push(name);
}
fileDone(null, stream);
}
function streamSVG(name, file) {
var stream = fs.createReadStream(file);
fileStreamed(name, stream);
}
function streamSVGO(name, file) {
var svg = fs.readFileSync(file, 'utf8');
var svgo = new SVGO();
try {
svgo.optimize(svg, function(res) {
var stream = new MemoryStream(res.data, {
writable: false
});
fileStreamed(name, stream);
});
} catch(err) {
logger.error('Can’t simplify SVG file with SVGO.\n\n' + err);
fileDone(err);
}
}
var idx = files.indexOf(file);
var name = o.glyphs[idx];
if(o.optimize === true) {
streamSVGO(name, file);
} else {
streamSVG(name, file);
}
}, function(err, streams) {
if (err) {
logger.error('Can’t stream SVG file.\n\n' + err);
allDone(false);
}
else {
done(streams);
}
});
}
function autohintTtfFont(font, done) {
var tempDir = temp.mkdirSync();
var originalFilepath = path.join(tempDir, 'font.ttf');
var hintedFilepath = path.join(tempDir, 'hinted.ttf');
if (!o.autoHint){
done(false);
return;
}
// Save original font to temporary directory
fs.writeFileSync(originalFilepath, font);
// Run ttfautohint
var args = [
'ttfautohint',
'--symbol',
'--fallback-script=latn',
'--windows-compatibility',
'--no-info',
originalFilepath,
hintedFilepath
].join(' ');
exec(args, {maxBuffer: o.execMaxBuffer}, function(err, out, code) {
if (err) {
if (err.code === 127) {
logger.verbose('Hinting skipped, ttfautohint not found.');
done(false);
return;
}
logger.error('Can’t run ttfautohint.\n\n' + err.message);
done(false);
return;
}
// Read hinted font back
var hintedFont = fs.readFileSync(hintedFilepath);
done(hintedFont);
});
}
};