大数据 爬虫Python AI Sqlreportlab教程和源码大全python源码大全

python 代码库之遍历ttf字库编码和字符(含demo)

2019-07-27  本文已影响4人  iCloudEnd

在网上搜索了很久,一直没有找到如果解析ttf文件的代码。于是不得不自己手动编写了。下面代码有如下作用

from fontTools.ttLib import TTFont as t1
import json

def toUnicode(oneStr):
    t=oneStr
    if  t[:3] == 'uni':t=t.replace('uni','\\u') 
    if  t[:2] == 'uF':t=t.replace('uF','\\u') 
    return json.loads(f'"{t}"') 

def printUNI(fontName,imagePath):
    font = t1(fontName)
    gs = font.getGlyphSet()
    glyphNames = font.getGlyphNames()
    for i in glyphNames:
        if i[0] == '.':#跳过'.notdef', '.null'
            continue
        print (i)
        print (i ,toUnicode(i)  )
        
fontName="font/FSung-F.ttf"
imagePath="images/FSung-F"
printUNI(fontName,imagePath)

运行结果

Jietu20190727-075123@2x.jpg

更多精彩代码请关注我的专栏

python源码大全

上一篇 下一篇

猜你喜欢

热点阅读