-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_font_from_bdf_new.py
More file actions
executable file
·133 lines (112 loc) · 4.2 KB
/
Copy pathbuild_font_from_bdf_new.py
File metadata and controls
executable file
·133 lines (112 loc) · 4.2 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
# 导入所需的库
import sys
from bdflib import reader
# 检查命令行参数
if len(sys.argv) < 2:
print("错误:请提供 BDF 字体文件路径作为参数")
print("用法:python3 build_font_from_bdf.py <bdf 文件路径>")
exit(1)
bdf_file_path = sys.argv[1]
# 读取 BDF 格式的字体文件
font = reader.read_bdf(open(bdf_file_path, "rb"))
# File format version; currently this must be 0.
version = '0'
# 所读取的字体的高度、宽度和字形上下间隔
height = 12
width = 12
top_gap = 1
bottom_gap = 0
# 定义垂直偏移量
v_offset = -3
# 初始化计数器
count = 0
print(f'''%PSF2
Version: {version}
Flags: 1
Length: 512
Width: {width}
Height: {height * 2}''', end='')
def PrintNonChineseCharacter(index):
count = index
# 输出空格
PrintCharacters(' ', count)
count += 1
# 打开包含其他字符的参考文件
with open("original/references/other_characters_new", encoding="utf-8") as f:
for lines in f:
line = lines.split()
PrintCharacters(line[0], count)
count += 1
return count
def PrintCharacters(Characters, count, is_fullwidth_cha = False):
Character = Characters[0] # 取第一个字符作为展示字
try:
# 将字符编码为对应编码
encoding = "utf-8"
# _enc = codecs.encode(Character, encoding=encoding)
# 获取对应的字形:即取 ENCODING 变量为字的 Unicode 编码的字形,不同字体中,ENCODING 变量数值的含义可能不同
try:
glyph = font[ord(Character)]
except KeyError:
print(f"警告:字符“{Character}”的 {encoding} 编码:“{ord(Character)}”无法在文件中找到。")
exit(1)
print("\n%")
print("//", count, Character)
# 打印字形的位图表示
print("Bitmap: ", end="")
o_pixels = [[y for y in x] for x in glyph.iter_pixels()]
# 取字形属性
[left, bottom, w, h] = glyph.get_bounding_box()
# 初始化一个空的像素矩阵
pixels = [["-" for j in range(0, width)] for i in range(0, height + 1 + top_gap + bottom_gap)]
# 填充像素矩阵
for i in range(0, h):
for j in range(0, w):
pixels[-bottom - h + i + v_offset][left + j] = ("#" if o_pixels[i][j] else "-")
# 打印像素矩阵
if is_fullwidth_cha: # 全角字符:一倍打印,上下补全
blank_row = "".join("-" for i in range(width))
for i in range(height // 2):
print(blank_row + " \\")
for row_h in range(top_gap, height + 1 - bottom_gap):
row = "".join(pixels[row_h])
print(row + " \\")
for i in range(height // 2 - 1):
print(blank_row + " \\")
print(blank_row)
else: # 半角字符:去掉右半边,把左半边缩放两倍打印
for row_h in range(top_gap, height + 1 - bottom_gap):
row = "".join(pixels[row_h])
row = row[:len(row)//2] # 去掉右半边
row = "".join(c * 2 for c in row) # 左右缩放两倍
if row_h < height:
print(row + " \\")
print(row + " \\")
else:
print(row + " \\")
print(row)
# 打印字符的 Unicode 编码
print("Unicode: ", end="")
for h in Characters:
# 遇到 # 则停止
if h == "#":
break
print("[{0:08x}];".format(ord(h)), end="")
except UnicodeError:
# 捕获并处理 Unicode 错误
pass
# 打开包含拼音和对应汉字的参考文件
with open("original/references/pinyin_hanzi", encoding="utf-8") as f:
count = 0
for line in f:
if count == 32:
count = PrintNonChineseCharacter(count)
# 跳过注释行
if line.startswith("#"):
continue
s = line.split() # 分开拼音和汉字
if len(s) >= 2:
hanzis = s[1] # 取汉字那一条
PrintCharacters(hanzis, count, True)
count += 1