This commit is contained in:
jeremygan2021
2026-03-03 22:45:09 +08:00
parent 700bc55657
commit 05f02a1454
14 changed files with 574 additions and 149 deletions

View File

@@ -0,0 +1,109 @@
import freetype
import os
FONT_FILE = "/Users/jeremygan/Desktop/python_dev/epaper2/websocket_server/GB2312.ttf"
OUTPUT_FILE = "../static_font_data.py"
FONT_SIZE = 16
# Fixed strings from the project
FIXED_STRINGS = [
"语音识别",
"松开停止",
"说完了吗?",
"未识别到文字",
"短按确认",
"长按重录",
"AI 生成中",
"正在思考...",
"优化提示词中",
"正在绘画...",
"AI作画中",
"生成完成!",
"生成失败",
"提示词:",
"返回录音"
]
def generate_static_font():
# Extract unique characters
chars = set()
for s in FIXED_STRINGS:
for c in s:
if ord(c) > 127: # Only non-ASCII
chars.add(c)
sorted_chars = sorted(list(chars))
print(f"Generating font data for {len(sorted_chars)} characters: {''.join(sorted_chars)}")
try:
face = freetype.Face(FONT_FILE)
except Exception as e:
print(f"Error loading font: {e}")
return
face.set_pixel_sizes(FONT_SIZE, FONT_SIZE)
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
f.write("# Static font data generated for specific characters\n")
f.write("import ubinascii\n\n")
f.write("FONTS = {\n")
for char in sorted_chars:
face.load_char(char, freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
bitmap = face.glyph.bitmap
# Create 32 bytes buffer (16x16 / 8)
# Similar logic to generate_font.py but simplified for single char
char_buffer = bytearray(32)
glyph_width = bitmap.width
glyph_rows = bitmap.rows
# Center the glyph
x_off = (FONT_SIZE - glyph_width) // 2
y_off = (FONT_SIZE - glyph_rows) // 2
# Adjust y_off based on baseline if needed, but let's stick to centering for consistency
# Usually for 16px font, baseline is around 12-13.
# bitmap_top is distance from baseline to top.
# We want to position it such that baseline is consistent.
# But let's use the simple centering logic from generate_font.py for now
# as it seems to be what was used before.
src_buf = bitmap.buffer
for row in range(glyph_rows):
dst_row = row + y_off
if dst_row < 0 or dst_row >= FONT_SIZE:
continue
for col in range(glyph_width):
dst_col = col + x_off
if dst_col < 0 or dst_col >= FONT_SIZE:
continue
# Extract bit from source
byte_idx = row * bitmap.pitch + (col >> 3)
bit_idx = 7 - (col & 7)
if byte_idx < len(src_buf):
pixel = (src_buf[byte_idx] >> bit_idx) & 1
if pixel:
# Set bit in destination
dst_byte_idx = dst_row * 2 + (dst_col >> 3)
dst_bit_idx = 7 - (dst_col & 7)
char_buffer[dst_byte_idx] |= (1 << dst_bit_idx)
# Write to file
hex_str = "".join([f"\\x{b:02x}" for b in char_buffer])
# Use ubinascii.unhexlify in generated code to save space?
# Or just bytes literal.
# bytes literal is fine.
f.write(f" {ord(char)}: b'{hex_str}', # {char}\n")
f.write("}\n")
print(f"Generated {OUTPUT_FILE}")
if __name__ == "__main__":
generate_static_font()