-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
107 lines (101 loc) · 2.97 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
var fs = require('fs')
, path = require('path')
, async = require('async')
, svg2ttf = require('svg2ttf')
, ttf2woff = require('ttf2woff')
, _ = require('underscore')
, svg = require('./lib/svg')
;
var CONFIG_FILE = 'config.json';
function createSvg (dir, config, done) {
config = config || {};
var mapFunction = function (charConfig, done) {
var file = path.resolve(dir, charConfig.file);
svg.optimize(config, file, function (err, data) {
if (err) {
return done(err);
}
return done(null, {
unicode: charConfig.unicode,
d: data
});
});
}
async.map(config.charmap, mapFunction, function (err, data) {
if (err) {
return done(err);
}
svg.create(_.extend({}, config, { charmap: data }), done);
});
}
function loadConfig (configFile, done) {
fs.readFile(configFile, 'utf-8', function (err, data) {
var config;
if (err) {
return done(err);
}
try {
config = JSON.parse(data);
} catch (err) {
return done("Invalid JSON file (" + err + ")");
}
return done(null, config);
});
}
function generateFont (inputDir, outputDir, done) {
async.auto({
loadConfig: function (next) {
loadConfig(path.join(inputDir, '/', CONFIG_FILE), next);
},
createSvg: ['loadConfig', function (next, data) {
createSvg(inputDir, data.loadConfig, next);
}],
saveSvg: ['loadConfig', 'createSvg', function (next, data) {
var file = path.resolve(outputDir, data.loadConfig.id + '.svg');
fs.writeFile(file, data.createSvg, function (err) {
if (err) {
return next(err);
}
return next(null, file);
});
}],
createTtf: ['loadConfig', 'createSvg', function (next, data) {
var ttf = svg2ttf(data.createSvg, data.loadConfig);
if (! ttf) {
return next('Could not create TTF file');
}
return next(null, ttf.buffer);
}],
saveTtf: ['loadConfig', 'createTtf', function (next, data) {
var file = path.resolve(outputDir, data.loadConfig.id + '.ttf');
fs.writeFile(file, new Buffer(data.createTtf), function (err) {
if (err) {
return next(err);
}
return next(null, file);
});
}],
createWoff: ['loadConfig', 'createTtf', function (next, data) {
var woff = ttf2woff(data.createTtf, data.loadConfig);
if (! woff) {
return next('Could not create WOFF file');
}
return next(null, woff.buffer);
}],
saveWoff: ['loadConfig', 'createWoff', function (next, data) {
var file = path.resolve(outputDir, data.loadConfig.id + '.woff');
fs.writeFile(file, new Buffer(data.createWoff), function (err) {
if (err) {
return next(err);
}
return next(null, file);
});
}]
}, function (err, data) {
if (err) {
return done(err);
}
return done(null, { svg: data.saveSvg, ttf: data.saveTtf, woff: data.saveWoff });
});
}
exports.generateFont = generateFont;