50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
// 待抽取字体的汉字
|
|
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); |