forked from yeungchenwa/FontDiffuser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_ttf.py
61 lines (52 loc) · 1.74 KB
/
to_ttf.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
52
53
54
55
56
57
58
59
60
61
import fontforge, os, psMat
import argparse
def main():
# 定义参数
img_list = list()
# 获取文件列表
for file in os.listdir(img_dir):
if os.path.splitext(file)[1].lower() in '.svg':
img_list.append(file)
print('当前总图片数量: %d' % len(img_list))
# 循环处理图片
# 创建字体
font = fontforge.font()
font.encoding = 'UnicodeFull'
font.version = opt.version
font.weight = 'Regular'
if font_name:
font.fontname = font_name
else:
font.fontname = 'test'
for img_path in img_list:
# 获取unicode
codestr = os.path.splitext(img_path)[0]
try:
code = ord(codestr)
except Exception as e:
print("err:", codestr)
print(e)
continue
# 创建字符
glyph = font.createChar(code, "uni" + codestr)
glyph.importOutlines(os.path.join(img_dir, img_path))
# 位移调整
base_matrix = psMat.translate(0, 0)
glyph.transform(base_matrix)
# 写入ttf
font.generate(output)
print('全部处理结束,svg文件若要删除自行处理')
if __name__ == "__main__":
"""
cd /mnt/data/llch/FontDiffuser
/usr/bin/python3 to_ttf.py --name cpp --v v1.0
"""
parser = argparse.ArgumentParser()
parser.add_argument('--input', dest='img_dir', default='pico/', help='Please set the input path')
parser.add_argument('--name', dest='name', default='cpp', help='Please set the output path')
parser.add_argument('--v', dest='version', default='v1.0', help='Please set the output path')
opt = parser.parse_args()
img_dir = opt.img_dir
output = f'{opt.name}.ttf'
font_name = f'torch-{opt.name}'
main()