-
Notifications
You must be signed in to change notification settings - Fork 21
/
iconfont-mapper.py
52 lines (41 loc) · 990 Bytes
/
iconfont-mapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, os
from fontTools.ttLib import TTFont
tmpl = """
var map = {%s};
;module.exports = (name)=>String.fromCharCode(String(map[name]));
;module.exports.map = map;
"""
def main(fontFile, output):
try:
font = TTFont(fontFile)
glyphMap = font["cmap"].getcmap(3,1).cmap
tmp = ""
for k in glyphMap:
tmp += '"%s":%d,' % (glyphMap[k],int(k))
f=file(output,"w+")
f.write(tmpl % tmp)
f.close()
except Exception as ex:
print ex
def showHelp():
print """
Iconfont map generator.
usage:
iconfont-maper <iconfont> <output> generate map file from iconfont file .
iconfont-maper -h show this help.
"""
if __name__ == '__main__':
if len(sys.argv) < 2:
showHelp()
sys.exit()
if len(sys.argv) == 2 and sys.argv[1] == "-h":
showHelp()
sys.exit()
if len(sys.argv) > 2:
if os.path.exists(sys.argv[1]):
main(sys.argv[1],sys.argv[2])
else:
print "Font file not found."
sys.exit()