-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbmp2ttf.py
More file actions
executable file
·81 lines (61 loc) · 1.9 KB
/
bmp2ttf.py
File metadata and controls
executable file
·81 lines (61 loc) · 1.9 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
#encoding=utf-8
'''
PURPOSE:
generate a font file(ttf format) with bitmaps
'''
import os
BMP_PATH = './bmp/'
SVG_PATH = './svg/'
FONT_PROJECT= './myfont.sfdir'
FONT_PATH= './myfont.ttf'
import fontforge
# new font.
#font = fontforge.font()
# open exist font.
print("Open font:", FONT_PROJECT)
font=fontforge.open(FONT_PROJECT)
# prepare environment
if not os.path.exists(BMP_PATH):
# make dir.
print('mkdir bmp folder')
os.system('mkdir -p ' + BMP_PATH)
if not os.path.exists(SVG_PATH):
# make dir.
print('mkdir svg folder')
os.system('mkdir -p ' + SVG_PATH)
print('bmp transform to svg...')
for filename in os.listdir(BMP_PATH):
bmp_filepath = os.path.join(BMP_PATH, filename)
svg_filename = filename.replace('.bmp','.svg')
svg_filepath = os.path.join(SVG_PATH, svg_filename)
command='potrace -s ' + bmp_filepath + ' -o ' + svg_filepath
try:
os.system(command)
except Exception as e:
print(e)
print('bmp transform to svg finished, move svg image to svg folder.')
print('generate fonts...')
for filename in os.listdir(SVG_PATH):
svg = os.path.join(SVG_PATH, filename)
try:
#image filename example: "U_001234.svg"
glyph = font.createChar(int('0x'+filename.split('.')[0][-4:], 16) ,filename.split('.')[0])
# force overwrite, must clear before import.
glyph.clear()
# if .clear() clean too many infomation, use below code instead.
#glyph.layers[0] = fontforge.layer()
#glyph.layers[1] = fontforge.layer()
glyph.importOutlines(svg)
#glyph.correctDirection()
glyph.simplify()
glyph.round()
except Exception as e:
print(e)
except Error as err:
print(err)
# not necessary to save, save only for debug purpose.
#font.save(FONT_PROJECT)
font.generate(FONT_PATH)
font.close()
print('generate fonts finished.^_^y')