This commit is contained in:
2018-06-11 17:06:22 +08:00
commit 1b99c33beb
4 changed files with 90 additions and 0 deletions

3
.gitignore vendored Executable file
View File

@ -0,0 +1,3 @@
out
node_modules
.DS_Store

50
index.js Normal file
View File

@ -0,0 +1,50 @@
// 待抽取字体的汉字
var text = '';
// 输入字体路径
var sourceFontFile = 'input.ttf';
// 输出字体名称
var outputFontFile = 'output';
var fs = require('fs');
var path = require('path');
const outDir = 'out';
if (!fs.existsSync(outDir)) fs.mkdirSync(outDir);
var fontCarrier = require('font-carrier');
var transFont = fontCarrier.transfer(sourceFontFile);
// 创建目标字体
var font = fontCarrier.create();
// 向字体中写入抽取的字形信息
if (text.length === 1)
font.setGlyph(text, transFont.getGlyph(text));
else
font.setGlyph(transFont.getGlyph(text));
// 将字体写入文件
font.output({
path: path.join(outDir, outputFontFile)
});
var testFile=`<!DOCTYPE html>
<html>
<style>
@font-face {
font-family: "output";
src: url('${outputFontFile}.ttf') format('truetype');
}
.test-font {
font-family: output;
}
</style>
<p>Make sure following text is displayed as expected:</p>
<p class="test-font">
${text}
</p>
</html>
`
fs.writeFileSync(path.join(outDir, 'test.htm'),testFile);

11
package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "font",
"version": "1.0.0",
"description": "",
"main": "index.js",
"author": "",
"license": "MIT",
"dependencies": {
"font-carrier": "0.0.12"
}
}

26
readme.md Normal file
View File

@ -0,0 +1,26 @@
# 关于
此项目用于精简 web 项目的字体。
## 如何使用
#### 修改 index.js将以下三个变量按实际需要配置
```js
// 待抽取字体的字符
var text = '待抽取字体的字符';
// 输入字体路径
var sourceFontFile = 'input.ttf';
// 输出字体名称
var outputFontFile = 'output';
```
#### 执行这个文件
```sh
node "index.js"
```
#### 检查输出
前往`out`目录,打开`test.htm`检查所需的字符是否正常显示。若正常out 目录下的文件即为精简后的字体。