diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..73bf5d8 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true +insert_final_newline = true + +[Makefile] +indent_style = tab diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/package.json b/package.json new file mode 100644 index 0000000..0955d57 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "fontasy", + "license": "MIT", + "dependencies": { + "mz": "^2.4.0", + "stream-to-promise": "^2.2.0", + "svg2ttf": "^4.0.1", + "svgicons2svgfont": "^5.0.0", + "ttf2eot": "^2.0.0", + "ttf2woff": "^2.0.1", + "ttf2woff2": "^2.0.3", + "yargs": "^4.8.1" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..27b34aa --- /dev/null +++ b/src/index.js @@ -0,0 +1,64 @@ +const fs = require("mz/fs"); +const path = require("path"); +const streamToPromise = require("stream-to-promise"); +const svgicons2svgfont = require("svgicons2svgfont"); +const svg2ttf = require("svg2ttf"); +const ttf2eot = require("ttf2eot"); +const ttf2woff = require("ttf2woff"); +const ttf2woff2 = require("ttf2woff2"); + +function toSvgFont(files, options) { + const stream = svgicons2svgfont(Object.assign({}, options)); + const promise = streamToPromise(stream); + files.sort().forEach(file => { + const name = path.basename(file, ".svg"); + const glyph = fs.createReadStream(file); + glyph.metadata = { + name, + unicode: [name] + }; + stream.write(glyph); + }); + stream.end(); + return promise.then(data => data.toString()); +} + +const defaultOptions = { + outputDir: ".", + fontName: "iconfont", + fontId: undefined, + fontStyle: '', + fontWeight: '', + fixedWidth: false, + centerHorizontally: false, + fontHeight: undefined, + round: undefined, + descent: undefined, + ascent: undefined, + metadata: "Created by Fontasy.", +}; + +module.exports = function(files, options) { + options = Object.assign({}, defaultOptions, options); + const baseName = path.join(options.outputDir, options.fontName); + return Promise.resolve() + .then(() => toSvgFont(files, options)) + .then(svg => { + const fonts = {}; + fonts.svg = svg; + fonts.ttf = new Buffer(svg2ttf(fonts.svg).buffer); + fonts.eot = new Buffer(ttf2eot(new Uint8Array(fonts.ttf)).buffer); + fonts.woff = new Buffer(ttf2woff(new Uint8Array(fonts.ttf), options).buffer); + fonts.woff2 = ttf2woff2(fonts.ttf); + return fonts; + }) + .then(fonts => { + return Promise.all([ + fs.writeFile(baseName + ".svg", fonts.svg), + fs.writeFile(baseName + ".ttf", fonts.ttf, "binary"), + fs.writeFile(baseName + ".eot", fonts.eot, "binary"), + fs.writeFile(baseName + ".woff", fonts.woff, "binary"), + fs.writeFile(baseName + ".woff2", fonts.woff2, "binary"), + ]); + }); +};