Skip to content

Commit ba40cdb

Browse files
committed
【修复】更新字体构建脚本并优化字形渲染参数
- 修复调整字体高度参数 13 至 12,并添加顶部和底部间距控制 - 修改垂直偏移量从 -2 到 -3 以改善字形对齐 - 优化字形渲染算法,支持可变高度渲染 - 在 .gitignore 中添加 Python 缓存文件忽略规则
1 parent f323e54 commit ba40cdb

4 files changed

Lines changed: 120 additions & 8 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66

77
AGENTS.md
88
QWEN.md
9-
CLAUDE.md
9+
CLAUDE.md
10+
11+
# Python
12+
__pycache__

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.0
1+
2.4.1

build_font_from_bdf.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
# File format version; currently this must be 0.
1919
version = '0'
2020

21-
# 定义字体的高度和宽度
22-
height = 13
21+
# 所读取的字体的高度、宽度和字形上下间隔
22+
height = 12
2323
width = 12
24+
top_gap = 1
25+
bottom_gap = 0
2426

2527
# 定义垂直偏移量
26-
v_offset = -2
28+
v_offset = -3
2729

2830
# 初始化计数器
2931
count = 0
@@ -68,20 +70,22 @@ def PrintCharacters(Characters, count):
6870
# 打印字形的位图表示
6971
print("Bitmap: ", end="")
7072
o_pixels = [[y for y in x] for x in glyph.iter_pixels()]
73+
74+
# 取字形属性
7175
[left, bottom, w, h] = glyph.get_bounding_box()
7276

7377
# 初始化一个空的像素矩阵
74-
pixels = [["-" for j in range(0, width)] for i in range(0, height)]
78+
pixels = [["-" for j in range(0, width)] for i in range(0, height + 1 + top_gap + bottom_gap)]
7579

7680
# 填充像素矩阵
7781
for i in range(0, h):
7882
for j in range(0, w):
7983
pixels[-bottom - h + i + v_offset][left + j] = ("#" if o_pixels[i][j] else "-")
8084

8185
# 打印像素矩阵
82-
for l in range(0, height):
86+
for l in range(top_gap, height + 1 - bottom_gap):
8387
ll = "".join(pixels[l])
84-
if l < height - 1:
88+
if l < height :
8589
ll = ll + " \\"
8690
print(ll)
8791

scripts/switch_1_2.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
将文本文件中每一行的第二个字符移动到行首
5+
"""
6+
7+
import sys
8+
import os
9+
10+
def switch_first_two_chars(line):
11+
"""
12+
将字符串的第二个字符移动到行首
13+
14+
Args:
15+
line: 输入字符串(不含换行符)
16+
17+
Returns:
18+
转换后的字符串
19+
"""
20+
if len(line) < 2:
21+
return line
22+
23+
# 第二个字符移动到行首
24+
return line[1] + line[0] + line[2:]
25+
26+
def process_file(file_path):
27+
"""
28+
处理文件,将每一行的第二个字符移动到行首
29+
30+
Args:
31+
file_path: 文件路径
32+
33+
Returns:
34+
bool: 是否成功
35+
"""
36+
try:
37+
with open(file_path, 'r', encoding='utf-8') as f:
38+
lines = f.readlines()
39+
40+
new_lines = []
41+
modified_count = 0
42+
43+
for line in lines:
44+
# 去除末尾换行符
45+
stripped = line.rstrip('\n')
46+
# 保留原始行结束符(可能是\r\n或其他)
47+
line_ending = line[len(stripped):] if len(line) > len(stripped) else ''
48+
49+
if len(stripped) >= 2:
50+
new_stripped = switch_first_two_chars(stripped)
51+
new_lines.append(new_stripped + line_ending)
52+
if new_stripped != stripped:
53+
modified_count += 1
54+
else:
55+
new_lines.append(line)
56+
57+
# 写回文件
58+
with open(file_path, 'w', encoding='utf-8') as f:
59+
f.writelines(new_lines)
60+
61+
print(f"✅ 处理完成:修改了 {modified_count} 行")
62+
return True
63+
64+
except UnicodeDecodeError:
65+
print(f"错误:无法读取文件 {file_path},请检查文件编码是否为 UTF-8")
66+
return False
67+
except FileNotFoundError:
68+
print(f"错误:文件 {file_path} 不存在")
69+
return False
70+
except PermissionError:
71+
print(f"错误:没有权限写入文件 {file_path}")
72+
return False
73+
except Exception as e:
74+
print(f"错误:处理文件时发生异常:{e}")
75+
return False
76+
77+
def main():
78+
if len(sys.argv) != 2:
79+
print("用法: python3 switch_1_2.py <文件路径>")
80+
print("将文本文件中每一行的第二个字符移动到行首")
81+
return 1
82+
83+
file_path = sys.argv[1]
84+
85+
if not os.path.exists(file_path):
86+
print(f"错误:文件 {file_path} 不存在")
87+
return 1
88+
89+
if os.path.isdir(file_path):
90+
print(f"错误:{file_path} 是一个目录,不是文件")
91+
return 1
92+
93+
print(f"正在处理文件:{file_path}")
94+
print("将每一行的第二个字符移动到行首...")
95+
print("-" * 50)
96+
97+
success = process_file(file_path)
98+
99+
if not success:
100+
return 1
101+
102+
return 0
103+
104+
if __name__ == "__main__":
105+
exit(main())

0 commit comments

Comments
 (0)