美文网首页
iconfont:借助opentype.js生成android/

iconfont:借助opentype.js生成android/

作者: 笑出zhu声 | 来源:发表于2020-04-09 11:29 被阅读0次

    不管是android还是iOS在使用自定义iconfont的时候都需要使用unicode编码,但是unicode编码并没有语义,也不方便维护。下面借助opentype.js(npm安装)生成android,iOS,RN三端的unicode定义文件:

    index.js:

    var opentype = require('opentype.js')
    const fs = require('fs')
    
    const argv = process.argv
    const ttfPath = argv[2] //命令行参数
    
    if (!isFileExists(ttfPath)) {
      console.log('输入错误,请重试!')
      process.exit(0)
    }
    else {
      parseFont(ttfPath)
    }
    
    function parseFont(fontPath) {
      let font = opentype.loadSync(fontPath)
      let fontFamily = font.glyphs.font.names.fontFamily.en
      let upCaseFontFamily = fontFamily.toUpperCase()
      let version = font.glyphs.font.names.version.en
      let copyright = font.glyphs.font.names.copyright.en
    
      let iosHeaderStr = `NSString * const icon_${fontFamily} = @"${fontFamily}"; //fontFamily \n\n`
    
      let androidStr = ``
      let rnStr = ''
    
      let glyphs = font.glyphs.glyphs
      for (let key in glyphs) {
        let glyph = glyphs[key]
        if (glyph.name.length == 0 || typeof (glyph.unicode) == "undefined") {
          continue
        }
    
        let constName = glyph.name.replace(/-/g, '_').toUpperCase()
    
        iosHeaderStr += `NSString * const ${constName} = @"\\U0000${glyph.unicode.toString(16)}";\n\n`
    
        androidStr += `<string name="${constName}">&#x${glyph.unicode.toString(16)};</string>\n`
    
        let rnConstName =  glyph.name.replace(/@2x/g,'').toLowerCase()
        if (rnStr == ``) {
            rnStr += `\n"${rnConstName.replace('@2X')}" : ${glyph.unicode.toString(10)}`
        } else {
            rnStr += `,\n"${rnConstName.replace('@2X')}" : ${glyph.unicode.toString(10)}`
        }
    
      }
    
      //iOS
      generateiOS(fontFamily, upCaseFontFamily, version, copyright, iosHeaderStr)
    
      //android
      generateAndroid(fontFamily, androidStr)
    
      //RN
      generateRN(fontFamily, rnStr)
    }
    
    function generateiOS(fontFamily, upCaseFontFamily, version, copyright, headerStr) {
      let hContent = `
    //
    // icon_${fontFamily}.h
    // fontFamily:${fontFamily}
    // 
    // ${version}
    // ${copyright}
    //
    
    #ifndef ICON_${upCaseFontFamily}_H
    #define ICON_${upCaseFontFamily}_H
    
    ${headerStr}
    
    #endif
    `
    
      writeFile(`icon_${fontFamily}.h`, hContent)
    }
    
    
    function generateAndroid(fontFamily, str) {
      let content = `
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    ${str}
    </resources>
    `
      writeFile(`icon_${fontFamily}.xml`, content)
    }
    
    function generateRN(fontFamily, str) {
      let content = `
    {
       ${str}
    }
    `
      writeFile(`icon_${fontFamily}.json`, content)
    }
    
    
    function writeFile(file, content) {
      let fd = fs.openSync(file, 'w');
      fs.writeFileSync(fd, content);
    }
    
    function isFileExists(arg) {
      if (typeof arg == "undefined" || arg == null || arg == "") {
        return false;
      }
      return fs.existsSync(arg);
    }
    

    使用方式: node index.js ttf路径

    相关文章

      网友评论

          本文标题:iconfont:借助opentype.js生成android/

          本文链接:https://www.haomeiwen.com/subject/bewqmhtx.html