This commit is contained in:
jixishi
2024-06-26 15:54:30 +08:00
parent 8c8cf20056
commit 11b8128796
12 changed files with 366 additions and 181 deletions

BIN
tools/HZK12 Normal file

Binary file not shown.

BIN
tools/HZK16 Normal file

Binary file not shown.

View File

@@ -4,18 +4,34 @@ import PIL.Image as pilimage
import PIL.ImageDraw as pildraw
# 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
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
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:
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)
# from bitarray import bitarray
@@ -72,10 +88,12 @@ def generate_chinese_array(input_str, font_str, size):
def format_chinese_array_as_text(chinese_array, size):
text_output = "#pragma pack(1)\n\n"
text_output = "#pragma once\n\n"
text_output += "#ifndef HW_LIB_OLED_FONT_CHUC_H\n"
text_output += "#define HW_LIB_OLED_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] // 8}];\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"uint8_t Hzk_size={size[0]};\n\n"
text_output += "Chinese_t Hzk[] = {\n"
@@ -84,6 +102,7 @@ def format_chinese_array_as_text(chinese_array, size):
# 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'])
@@ -92,14 +111,15 @@ def format_chinese_array_as_text(chinese_array, size):
text_output += '\n '.join(bytes_lines)
text_output += ",\n }\n },\n"
text_output += "};\n\n"
text_output += "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 += "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_OLED_FONT_CHUC_H"
return text_output
@@ -110,7 +130,8 @@ def generate_and_write_chinese_array_output():
# size = (20, 20)
# size = (12, 12)
size = (16, 16)
chinese_array = generate_chinese_array("字库生成测试", 'simsun', size)
# chinese_array = generate_chinese_array("名字班级", 'simsun', size)
chinese_array = generate_chinese_array("姓名学号班级信息显示图片视频专业电子", 'simsun', size)
# 将数组格式化为文本输出并写入文件
text_output = format_chinese_array_as_text(chinese_array, size)