This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
75 lines (57 loc) · 1.76 KB
/
main.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
#!/usr/bin/env node
var fs = require("fs");
var path = require("path");
var supportedFormats = ["ttf", "woff", "eot"];
if (require.main === module) {
var optimist = require("optimist")
.usage("Build fonts from SVG font.\nUsage: $0 <file> -f [string] -d [string]")
.alias('d', 'dest')
.alias('f', 'formats')
.check(function(argv){
if (argv._.length === 0) {
throw "Input file missing!";
}
});
var argv = optimist.argv;
var formats = supportedFormats;
if (argv.formats) {
formats = argv.formats.split(",").filter(function (format) {
return (supportedFormats.indexOf(format) > -1);
});
}
build(argv._[0], argv.dest, formats);
}
function build (source, dest, formats) {
var filename = path.basename(source, path.extname(source));
if (Array.isArray(dest)) {
formats = dest;
dest = null;
}
if (typeof dest !== "string") {
dest = path.dirname(source);
}
if (!fs.existsSync(dest)) {
var mkdirp = require("mkdirp");
mkdirp.sync(dest);
}
formats = formats || supportedFormats;
if (formats.indexOf("ttf") > -1 || (formats.indexOf("woff") > -1 || formats.indexOf("eot") > -1)) {
var svg = fs.readFileSync(source, {encoding: "utf8"});
var svg2ttf = require("svg2ttf");
var ttf = svg2ttf(svg);
if (formats.indexOf("ttf") > -1) {
fs.writeFileSync(path.join(dest, filename + ".ttf"), new Buffer(ttf.buffer));
}
if (formats.indexOf("woff") > -1) {
var ttf2woff = require("ttf2woff");
var woff = ttf2woff(ttf.buffer);
fs.writeFileSync(path.join(dest, filename + ".woff"), new Buffer(woff.buffer));
}
if (formats.indexOf("eot") > -1) {
var ttf2eot = require("ttf2eot");
var eot = ttf2eot(ttf.buffer);
fs.writeFileSync(path.join(dest, filename + ".eot"), new Buffer(eot.buffer));
}
}
}
module.exports = build;