FontTools的使用

一、FontTools的安装

pip install fontTools

二、FontTools的基本操作

1、字体读取

from fontTools.ttLib import TTFont

# 加载字体文件:
font = TTFont('maoyan.woff')

# 转为xml文件:
font.saveXML('maoyan.xml')

2、各节点名称:font.keys()

from fontTools.ttLib import TTFont

# 加载字体文件:
font = TTFont('XRXV3I6Li01BKofINeaB.woff2')

kv = font.keys()

print(kv)

运行结果:

['GlyphOrder', 'head', 'hhea', 'maxp', 'OS/2', 'hmtx', 'cmap', 'fpgm', 'prep', 'cvt ', 'loca', 'glyf', 'name', 'post', 'gasp', 'GDEF', 'GPOS', 'GSUB']

3、获取getGlyphOrder节点name值

font.getGlyphOrder()

运行结果:

['.notdef', 'cid00001', 'cid00017', 'cid00018', 'cid00019', 'cid00020', 'cid00021', 'cid00022', 'cid00023', 'cid00024', 'cid00025', 'cid00026']

4、获取cmap节点code与name值映射

print(font.getGlyphOrder())

运行结果:

{48: 'cid00026', 49: 'cid00017', 50: 'cid00019', 51: 'cid00024', 52: 'cid00022', 53: 'cid00021', 54: 'cid00020', 55: 'cid00018', 56: 'cid00025', 57: 'cid00023'}

5、获取字体坐标信息

#font['glyf'][字体编码].coordinates
font['glyf']['one'].coordinates

运行结果:

GlyphCoordinates([(510, 69),(530, 51),(530, 35),(530, 18),(511, 0),(492, 0),(161, 0),(142, 0),(123, 18),(123, 35),(123, 51),(143, 69),(161, 69),(285, 69),(285, 605),(176, 534),(168, 529),(160, 529),(146, 529),(125, 555),(125, 570),(125, 591),(141, 600),(293, 699),(310, 710),(328, 710),(345, 710),(367, 688),(367, 668),(367, 69),(492, 69)])

6、获取坐标的0或10表示弧形区域 1表示矩形

#font['glyf'][字体'].flags
font['glyf']['one'].flags

运行结果:

array('B', [0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1])

三、扩展

1、一个最基本的字体文件一定会包含以下的表:

cmap: Char­ac­ter to glyph map­ping unicode跟 Name的映射关系
head: Font header 字体全局信息
hhea: Hor­i­zon­tal header 定义了水平header
hmtx: Hor­i­zon­tal met­rics 定义了水平metric
maxp: Max­i­mum pro­file 用于为字体分配内存
name: Nam­ing ta­ble 定义字体名称、风格名以及版权说明等
glyf: 字形数据即轮廓定义和调整指令
OS__2: OS__2 and Win­dows spe­cific met­rics
post: Post­Script in­for­ma­tion
2、高级书法特性的字体包含表:

BASE: Base­line data
GDEF: Glyph de­f­i­n­i­tion data
GPOS: Glyph po­si­tion­ing data
GSUB: Glyph sub­sti­tu­tion data
JSTF: Jus­ti­fi­ca­tion data
MATH: Math lay­out data

  • 9
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值