Compare commits

...

2 Commits

Author SHA1 Message Date
JiXieShi 790d26fbaa Merge remote-tracking branch 'origin/main'
# Conflicts:
#	tools/unicode_ch_gen.py
2024-06-23 21:27:35 +08:00
JiXieShi 3dcc2f8f20 UP 字库生成脚本 2024-06-23 21:26:28 +08:00
1 changed files with 38 additions and 17 deletions

View File

@ -4,40 +4,58 @@ import PIL.Image as pilimage
import PIL.ImageDraw as pildraw import PIL.ImageDraw as pildraw
def generate_chinese_struct(char_code, font): def generate_chinese_struct(char_code, font, size):
image = pilimage.new('L', (16, 16)) image = pilimage.new('L', size)
draw = pildraw.Draw(image) draw = pildraw.Draw(image)
draw.text((0, 0), char_code, font=font, fill=255) draw.text((0, 0), char_code, font=font, fill=255)
pixel_array = np.array(image) pixel_array = np.array(image)
result = np.zeros(32, dtype=np.uint8) result = np.zeros(size[0] * size[1] // 8, dtype=np.uint8)
for i in range(16): for i in range(size[1]):
for j in range(2): for j in range(size[0] // 8):
for k in range(8): for k in range(8):
if pixel_array[j * 8 + k, i]: if pixel_array[j * 8 + k, i]:
result[j * 16 + i] |= (1 << k) result[j * size[1] + i] |= (1 << k)
return result return result
def generate_chinese_array(input_str, font_str): def generate_chinese_array(input_str, font_str, size):
font = pilfont.truetype(font_str, size=16) font = pilfont.truetype(font_str, size=size[1])
chinese_array = [] chinese_array = []
for char_code in input_str: for char_code in input_str:
char_struct = generate_chinese_struct(char_code, font) char_struct = generate_chinese_struct(char_code, font, size)
chinese_array.append({'name': char_code, 'data': char_struct}) chinese_array.append({'name': char_code, 'data': char_struct})
return chinese_array return chinese_array
def format_chinese_array_as_text(chinese_array): def format_chinese_array_as_text(chinese_array, size):
text_output = "Chinese_t Hzk[] = {\n" text_output = "#pragma pack(1)\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 += "} Chinese_t;\n\n"
text_output += f"uint8_t Hzk_size={size[0]};\n\n"
text_output += "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: for item in chinese_array:
unicode_hex = ', '.join(f"0x{ord(char) >> 8:02X}, 0x{ord(char) & 0xFF:02X}" for char in item['name']) unicode_hex = ', '.join(f"0x{ord(char) >> 8:02X}, 0x{ord(char) & 0xFF:02X}" for char in item['name'])
text_output += f" {{\n // Original: {item['name']}\n" text_output += f" {{\n // Original: {item['name']}\n"
text_output += f" {{ {unicode_hex} }},\n {{" text_output += f" {{ {unicode_hex} }},\n {{\n "
bytes_str = ', '.join(f"0x{byte:02X}" for byte in item['data']) bytes_str = ', '.join(f"0x{byte:02X}" for byte in item['data'])
bytes_lines = [bytes_str[i:i + 24 * 4] for i in range(0, len(bytes_str), 24 * 4)] # 每行显示16个数组 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 '.join(bytes_lines)
text_output += "\n }},\n" text_output += ",\n }\n },\n"
text_output += "};" 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"
return text_output return text_output
@ -45,10 +63,13 @@ def generate_and_write_chinese_array_output():
# 生成包含汉字结构体的数组 # 生成包含汉字结构体的数组
# simsun: 宋体 # simsun: 宋体
# kaiti: 楷体 # kaiti: 楷体
chinese_array = generate_chinese_array("字库生成测试", 'simsun') # size = (20, 20)
# size = (20, 20)
size = (16, 16)
chinese_array = generate_chinese_array("字库生成测试", 'simsun', size)
# 将数组格式化为文本输出并写入文件 # 将数组格式化为文本输出并写入文件
text_output = format_chinese_array_as_text(chinese_array) text_output = format_chinese_array_as_text(chinese_array, size)
with open('chinese_array_output.txt', 'w') as file: with open('chinese_array_output.txt', 'w') as file:
file.write(text_output) file.write(text_output)