UP font generate
This commit is contained in:
BIN
tools/font_lib/HZK16C
Normal file
BIN
tools/font_lib/HZK16C
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK16F
Normal file
BIN
tools/font_lib/HZK16F
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK16H
Normal file
BIN
tools/font_lib/HZK16H
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK16K
Normal file
BIN
tools/font_lib/HZK16K
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK16L
Normal file
BIN
tools/font_lib/HZK16L
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK16S
Normal file
BIN
tools/font_lib/HZK16S
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK16V
Normal file
BIN
tools/font_lib/HZK16V
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK16X
Normal file
BIN
tools/font_lib/HZK16X
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK16Y
Normal file
BIN
tools/font_lib/HZK16Y
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK24F
Normal file
BIN
tools/font_lib/HZK24F
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK24H
Normal file
BIN
tools/font_lib/HZK24H
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK24K
Normal file
BIN
tools/font_lib/HZK24K
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK24S
Normal file
BIN
tools/font_lib/HZK24S
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK24T
Normal file
BIN
tools/font_lib/HZK24T
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK32
Normal file
BIN
tools/font_lib/HZK32
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK40S
Normal file
BIN
tools/font_lib/HZK40S
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK40T
Normal file
BIN
tools/font_lib/HZK40T
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK48S
Normal file
BIN
tools/font_lib/HZK48S
Normal file
Binary file not shown.
BIN
tools/font_lib/HZK48T
Normal file
BIN
tools/font_lib/HZK48T
Normal file
Binary file not shown.
@@ -1,154 +1,106 @@
|
||||
import numpy as np
|
||||
import PIL.ImageFont as pilfont
|
||||
import PIL.Image as pilimage
|
||||
import PIL.ImageDraw as pildraw
|
||||
Font_Lib = {
|
||||
"HZK12": {"size": 12, "desc": "宋体优化"},
|
||||
"HZK16": {"size": 16, "desc": "宋体优化"},
|
||||
"HZK32": {"size": 32, "desc": "宋体优化"},
|
||||
"HZK16S": {"size": 16, "desc": "宋体"},
|
||||
"HZK16F": {"size": 16, "desc": "仿宋"},
|
||||
"HZK16H": {"size": 16, "desc": "黑体"},
|
||||
"HZK16K": {"size": 16, "desc": "楷体"},
|
||||
"HZK16Y": {"size": 16, "desc": "幼圆"},
|
||||
"HZK16L": {"size": 16, "desc": "隶书(效果较差)"},
|
||||
"HZK16C": {"size": 16, "desc": "粗体"},
|
||||
"HZK16X": {"size": 16, "desc": "细"},
|
||||
"HZK16V": {"size": 16, "desc": "繁"},
|
||||
"HZK24F": {"size": 24, "desc": "仿宋汉字打印点阵"},
|
||||
"HZK24H": {"size": 24, "desc": "黑体汉字打印点阵"},
|
||||
"HZK24K": {"size": 24, "desc": "楷体汉字打印点阵"},
|
||||
"HZK24S": {"size": 24, "desc": "宋体汉字打印点阵"},
|
||||
"HZK24T": {"size": 24, "desc": "宋体符号打印点阵"},
|
||||
"HZK40S": {"size": 40, "desc": "宋体汉字"},
|
||||
"HZK40T": {"size": 40, "desc": "宋体符号"},
|
||||
"HZK48S": {"size": 48, "desc": "宋体汉字"},
|
||||
"HZK48T": {"size": 48, "desc": "宋体符号"},
|
||||
}
|
||||
|
||||
|
||||
# def generate_chinese_struct(char_code, font, size):
|
||||
# image = pilimage.new('L', size)
|
||||
# draw = pildraw.Draw(image)
|
||||
# draw.text((0, 0), char_code, font=font, fill=255)
|
||||
# pixel_array = np.array(image)
|
||||
# result = np.zeros(size[0] * size[1] // 8, dtype=np.uint8)
|
||||
# for i in range(size[1]):
|
||||
# for j in range(size[0] // 8):
|
||||
# for k in range(8):
|
||||
# if pixel_array[j * 8 + k, i]:
|
||||
# result[j * size[1] + i] |= (1 << k)
|
||||
# return result
|
||||
class ChineseFontLibrary:
|
||||
def __init__(self, name, path="./font_lib/"):
|
||||
self.size = Font_Lib.get(name)['size']
|
||||
self.path = path
|
||||
self.fontname = name
|
||||
self.chinese_array = []
|
||||
self.linecache = self.size * ((self.size + 7) // 8 * 8) // 8
|
||||
|
||||
def format_chinese_array_as_text(self):
|
||||
text_output = "#pragma once\n\n"
|
||||
text_output += "#ifndef HW_LIB_FONT_CHUC_H\n"
|
||||
text_output += "#define HW_LIB_FONT_CHUC_H\n\n"
|
||||
text_output += f"typedef struct {{\n"
|
||||
text_output += " uint8_t unicode[2];\n"
|
||||
text_output += f" uint8_t data[{self.size * ((self.size + 7) // 8 * 8) // 8}];\n"
|
||||
text_output += "} Chinese_t;\n\n"
|
||||
text_output += f"static uint8_t Hzk_size={self.size};\n\n"
|
||||
text_output += "static Chinese_t Hzk[] = {\n"
|
||||
line_size = self.size * self.size // 8
|
||||
line_size = line_size // 2
|
||||
for item in self.chinese_array:
|
||||
unicode_hex = ', '.join(f"0x{ord(char) >> 8:02X}, 0x{ord(char) & 0xFF:02X}" for char in item['name'])
|
||||
print(unicode_hex, end=",")
|
||||
text_output += f" {{\n // Original: {item['name']}\n"
|
||||
text_output += f" {{ {unicode_hex} }},\n {{\n "
|
||||
bytes_str = ', '.join(f"0x{byte:02X}" for byte in item['data'])
|
||||
bytes_lines = [bytes_str[i:i + 6 * line_size] for i in range(0, len(bytes_str), 6 * line_size)]
|
||||
# 每行显示line_size个字节
|
||||
text_output += '\n '.join(bytes_lines)
|
||||
text_output += ",\n }\n },\n"
|
||||
text_output += "};\n\n"
|
||||
text_output += "static Chinese_t* find_chinese_data(uint8_t unicode_high, uint8_t unicode_low) {\n"
|
||||
text_output += " for (int i = 0; i < sizeof(Hzk) / sizeof(Chinese_t); ++i) {\n"
|
||||
text_output += " if (Hzk[i].unicode[0] == unicode_high && Hzk[i].unicode[1] == unicode_low) {\n"
|
||||
text_output += " return &Hzk[i];\n"
|
||||
text_output += " }\n"
|
||||
text_output += " }\n"
|
||||
text_output += " return NULL;\n"
|
||||
text_output += "}\n"
|
||||
text_output += "\n#endif //HW_LIB_FONT_CHUC_H"
|
||||
return text_output
|
||||
|
||||
def generate_chinese_struct(char_code, font, size):
|
||||
incode = char_code.encode('gb2312') # 要读出的汉字
|
||||
qh, wh = incode[0] - 0xa0, incode[1] - 0xa0 # 占两个字节, 取其区位号
|
||||
|
||||
if size[0] == 12:
|
||||
offset = (94 * (qh - 1) + (wh - 1)) * 24 # 得到偏移位置
|
||||
with open("HZK12", "rb") as HZK:
|
||||
def generate_chinese_struct(self, code):
|
||||
incode = code.encode('gb2312') # 要读出的汉字
|
||||
qh, wh = incode[0] - 0xa0, incode[1] - 0xa0 # 占两个字节, 取其区位号
|
||||
offset = (94 * (qh - 1) + (wh - 1)) * self.linecache # 得到偏移位置
|
||||
with open(self.path + self.fontname, "rb") as HZK:
|
||||
HZK.seek(offset)
|
||||
return HZK.read(24)
|
||||
if size[0] == 16:
|
||||
offset = (94 * (qh - 1) + (wh - 1)) * 32 # 得到偏移位置
|
||||
with open("HZK16", "rb") as HZK:
|
||||
HZK.seek(offset)
|
||||
return HZK.read(32)
|
||||
return HZK.read(self.linecache)
|
||||
|
||||
def add_chinese_array(self, input_str: str):
|
||||
for char_code in input_str:
|
||||
char_struct = self.generate_chinese_struct(char_code)
|
||||
self.chinese_array.append({'name': char_code, 'data': char_struct})
|
||||
return self
|
||||
|
||||
def write_chinese_array_output(self, filename='font_chuc.h'):
|
||||
text_output = self.format_chinese_array_as_text()
|
||||
with open(filename, 'w', encoding="utf8") as file:
|
||||
file.write(text_output)
|
||||
|
||||
|
||||
# from bitarray import bitarray
|
||||
# # from PIL import Image, ImageDraw
|
||||
# # from bitarray import bitarray
|
||||
if __name__ == '__main__':
|
||||
i = 0
|
||||
for k, v in Font_Lib.items():
|
||||
print(f"{i}:{k} - {v['size']} {v['desc']}")
|
||||
i += 1
|
||||
|
||||
|
||||
# def generate_chinese_struct(char_code, font, size):
|
||||
# height=(size[1] + 7) // 8 * 8
|
||||
# wight=(size[0] + 7) // 8 * 8
|
||||
# image = Image.new('1', size, 1)
|
||||
# draw = ImageDraw.Draw(image)
|
||||
# draw.text((0, -1), char_code, font=font, fill=0)
|
||||
# image.show()
|
||||
# bitmap = bitarray()
|
||||
# for w in range(size[1]):
|
||||
# for h in range(size[0]):
|
||||
# # if h > size[1] or w > size[0]:
|
||||
# # bitmap.append(False)
|
||||
# # else:
|
||||
# if image.getpixel((w, h)) == 0:
|
||||
# bitmap.append(True)
|
||||
# print('■', end=' ')
|
||||
# else:
|
||||
# bitmap.append(False)
|
||||
# print('0', end=' ')
|
||||
# print()
|
||||
# result = np.zeros(size[0] * size[1] // 8, dtype=np.uint8)
|
||||
# # for i in range(height):
|
||||
# # for j in range(wight // 8):
|
||||
# # for k in range(8):
|
||||
# # if bitmap[j * 8 + k, i]==1:
|
||||
# # result[j * height + i] |= (1 << k)
|
||||
# for h in range(height):
|
||||
# for w in range(wight):
|
||||
# if bitmap[w+h]:
|
||||
# #前景字符(即用来表示汉字笔画的输出字符)
|
||||
# print('■', end=' ')
|
||||
# else:
|
||||
#
|
||||
# # 背景字符(即用来表示背景的输出字符)
|
||||
# print('0', end=' ')
|
||||
# print()
|
||||
# return result
|
||||
|
||||
|
||||
def generate_chinese_array(input_str, font_str, size):
|
||||
font = pilfont.truetype(font_str, size=size[1])
|
||||
chinese_array = []
|
||||
for char_code in input_str:
|
||||
char_struct = generate_chinese_struct(char_code, font, size)
|
||||
chinese_array.append({'name': char_code, 'data': char_struct})
|
||||
return chinese_array
|
||||
|
||||
|
||||
def format_chinese_array_as_text(chinese_array, size):
|
||||
text_output = "#pragma once\n\n"
|
||||
text_output += "#ifndef HW_LIB_FONT_CHUC_H\n"
|
||||
text_output += "#define HW_LIB_FONT_CHUC_H\n\n"
|
||||
text_output += f"typedef struct {{\n"
|
||||
text_output += " uint8_t unicode[2];\n"
|
||||
text_output += f" uint8_t data[{size[0] * ((size[1] + 7) // 8 * 8) // 8}];\n"
|
||||
text_output += "} Chinese_t;\n\n"
|
||||
text_output += f"static uint8_t Hzk_size={size[0]};\n\n"
|
||||
text_output += "static Chinese_t Hzk[] = {\n"
|
||||
line_size = size[0] * size[1] // 8
|
||||
line_size = line_size // 2
|
||||
# line_size = 16 if line_size >= 16 else 8
|
||||
for item in chinese_array:
|
||||
unicode_hex = ', '.join(f"0x{ord(char) >> 8:02X}, 0x{ord(char) & 0xFF:02X}" for char in item['name'])
|
||||
print(unicode_hex, end=",")
|
||||
text_output += f" {{\n // Original: {item['name']}\n"
|
||||
text_output += f" {{ {unicode_hex} }},\n {{\n "
|
||||
bytes_str = ', '.join(f"0x{byte:02X}" for byte in item['data'])
|
||||
bytes_lines = [bytes_str[i:i + 6 * line_size] for i in range(0, len(bytes_str), 6 * line_size)]
|
||||
# 每行显示line_size个字节
|
||||
text_output += '\n '.join(bytes_lines)
|
||||
text_output += ",\n }\n },\n"
|
||||
text_output += "};\n\n"
|
||||
text_output += "static Chinese_t* find_chinese_data(uint8_t unicode_high, uint8_t unicode_low) {\n"
|
||||
text_output += " for (int i = 0; i < sizeof(Hzk) / sizeof(Chinese_t); ++i) {\n"
|
||||
text_output += " if (Hzk[i].unicode[0] == unicode_high && Hzk[i].unicode[1] == unicode_low) {\n"
|
||||
text_output += " return &Hzk[i];\n"
|
||||
text_output += " }\n"
|
||||
text_output += " }\n"
|
||||
text_output += " return NULL;\n"
|
||||
text_output += "}\n"
|
||||
text_output += "\n#endif //HW_LIB_FONT_CHUC_H"
|
||||
return text_output
|
||||
|
||||
|
||||
def generate_and_write_chinese_array_output():
|
||||
# 生成包含汉字结构体的数组
|
||||
# simsun: 宋体
|
||||
# kaiti: 楷体
|
||||
# size = (12, 12)
|
||||
size = (16, 16)
|
||||
# chinese_array = generate_chinese_array("当前液位", 'simsun', size)
|
||||
chinese_array = generate_chinese_array("星海科技机械师", 'simsun', size)
|
||||
|
||||
# 将数组格式化为文本输出并写入文件
|
||||
text_output = format_chinese_array_as_text(chinese_array, size)
|
||||
with open('font_chuc.h', 'w', encoding="utf8") as file:
|
||||
file.write(text_output)
|
||||
|
||||
|
||||
# 调用函数生成并写入汉字数组输出文件
|
||||
generate_and_write_chinese_array_output()
|
||||
|
||||
|
||||
def generate_unicode_bin_file():
|
||||
font = pilfont.truetype('simsun', size=16)
|
||||
with open('cao.bin', 'wb') as file:
|
||||
for char_code in range(0x4e00, 0xa000):
|
||||
result = generate_chinese_struct(chr(char_code), font)
|
||||
file.write(result)
|
||||
file.close()
|
||||
|
||||
# 调用函数生成Unicode二进制文件
|
||||
# generate_unicode_bin_file()
|
||||
input_c = input(f"请输入需要的字体序号(q退出):")
|
||||
if input_c == 'q':
|
||||
exit()
|
||||
values_list = list(Font_Lib.values())
|
||||
keys_list = list(Font_Lib.keys())
|
||||
key_index = list(Font_Lib.values()).index(values_list[int(input_c)])
|
||||
print(
|
||||
f"已选择{int(input_c)}:{keys_list[key_index]} - {Font_Lib[keys_list[key_index]]['size']} {Font_Lib[keys_list[key_index]]['desc']}")
|
||||
font = ChineseFontLibrary(keys_list[key_index])
|
||||
input_c = input(f"请输入需要生成的字符(顺序无关):")
|
||||
print("将生成以下字符\n" + input_c)
|
||||
font.add_chinese_array(input_c)
|
||||
font.write_chinese_array_output()
|
||||
|
Reference in New Issue
Block a user