node实现简化字体文件到3500字

2023-08-03  本文已影响0人  NanaCti

在three中导入字体文件需要先将ttf文件转成json
转换地址: http://gero3.github.io/facetype.js/
转换后的字体json文件太大了, 处理成3500常用汉字的json

const keys = require('./3500.json'); // 3500汉字+ 其他字符的集合
const fs = require('fs');
const fileName = 'HYXiaoBoZheZhiTiJ_Regular';

(() => {
  try {
    const data = fs.readFileSync(`./${fileName}.json`, "utf-8")
    let fontAll = JSON.parse(data);
    const glyphs = {}
    keys.map(key => {
      if (fontAll.glyphs[key] === undefined) return
      glyphs[key] = fontAll.glyphs[key]
    })
    fontAll.glyphs = glyphs
    // let str = JSON.stringify(fontAll, "", "\t");
    let str = JSON.stringify(fontAll);
    
    const err = fs.writeFileSync(`./${fileName}_3500.json`, str)
    if (err) {console.error(err);}

    
  } catch (error) { console.log(error) }
})()

上一篇 下一篇

猜你喜欢

热点阅读