Skip to content

Commit

Permalink
add core function
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiaoyan committed Aug 2, 2016
0 parents commit 8ee2fcf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
64 changes: 64 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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"),
]);
});
};

0 comments on commit 8ee2fcf

Please sign in to comment.