Files
lishanpi/tools/fix_font.py
T

40 lines
1.5 KiB
Python
Raw Normal View History

2026-05-02 09:10:36 +08:00
import re, os, glob
2026-05-02 08:51:08 +08:00
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
font_files = glob.glob(os.path.join(project_root, 'app/tasks/custom_cjk_*.c'))
2026-05-02 08:51:08 +08:00
2026-05-02 09:10:36 +08:00
for filepath in font_files:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
2026-05-02 08:51:08 +08:00
2026-05-02 09:10:36 +08:00
def fix_adv_w(match):
full_line = match.group(0)
2026-05-02 09:10:36 +08:00
adv_w = int(match.group(1))
box_w = int(match.group(2))
ofs_x = int(match.group(3)) if match.group(3) else 0
2026-05-02 09:10:36 +08:00
if adv_w >= 250 and box_w >= 10 and adv_w % 16 == 0:
new_adv_w = (box_w + ofs_x) * 16
return full_line.replace(f'adv_w = {adv_w}', f'adv_w = {new_adv_w}')
return full_line
2026-05-02 08:51:08 +08:00
pattern = r'\.adv_w = (\d+), \.box_w = (\d+)(?:, \.box_h = \d+)?(?:, \.ofs_x = (\d+))?'
2026-05-02 09:10:36 +08:00
fixed_content = re.sub(pattern, fix_adv_w, content)
2026-05-02 08:51:08 +08:00
2026-05-02 09:10:36 +08:00
with open(filepath, 'w', encoding='utf-8') as f:
f.write(fixed_content)
2026-05-02 08:51:08 +08:00
2026-05-02 09:10:36 +08:00
print(f"=== {os.path.basename(filepath)} ===")
original = content
changed_lines = []
for line_num, (orig_line, fix_line) in enumerate(zip(original.split('\n'), fixed_content.split('\n')), 1):
if orig_line != fix_line and 'adv_w' in fix_line:
orig_adv = re.search(r'adv_w = (\d+)', orig_line)
fix_adv = re.search(r'adv_w = (\d+)', fix_line)
if orig_adv and fix_adv:
changed_lines.append(f" L{line_num}: {orig_adv.group(1)} -> {fix_adv.group(1)}")
for l in changed_lines[:5]:
print(l)
print(f" Total changed: {len(changed_lines)}")
2026-05-02 09:10:36 +08:00
print()