-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
300 lines (246 loc) · 11.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
import codecs
import re
import subprocess
import tkinter as tk
from tkinter import scrolledtext, filedialog, ttk
from tkinterdnd2 import DND_FILES, TkinterDnD
import chardet
# 确保 temp 文件夹存在
temp_dir = 'temp'
os.makedirs(temp_dir, exist_ok=True)
# BDF 文件夹路径
bdf_dir = 'bdf'
def extract_chinese(text):
pattern = re.compile(r'[\u4e00-\u9fff]')
chinese_text = ''.join(pattern.findall(text))
return chinese_text
def filter_comments(content):
content = re.sub(r'//.*', '', content)
content = re.sub(r'/\*.*?\*/', '', content, flags=re.DOTALL)
return content
def process_file(input_file, output_file):
with codecs.open(input_file, 'r', 'utf-8') as f:
content = f.read()
content = content.encode('unicode_escape').decode('utf-8')
pattern = re.compile(r'\\u([0-9a-fA-F]{4})')
formatted_content = pattern.sub(r'$\1,\n', content)
unique_lines = sorted(set(formatted_content.splitlines()))
final_content = [line.upper() for line in unique_lines if line.strip()]
header = '32-128,\n'
with open(output_file, 'w', encoding='utf-8', newline='') as f:
if final_content:
f.write(header)
f.write('\n'.join(final_content))
def filter_comments_and_modify_c_content(c_content):
# 过滤注释
c_content = re.sub(r'//.*', '', c_content)
c_content = re.sub(r'/\*.*?\*/', '', c_content, flags=re.DOTALL)
c_content = "\n".join([line for line in c_content.splitlines() if line.strip()])
c_content = re.sub(r' U8G2_FONT_SECTION\("kalicyh"\)', '', c_content)
# 根据复选框的状态决定是否修改 const 为 static const
if add_static_var.get():
c_content = re.sub(r'(?<!static\s)const', r'static const', c_content)
# 根据复选框的状态决定是否移除数组长度
if remove_array_length_var.get():
c_content = re.sub(r'\[\d+\]', '[]', c_content)
return c_content
def run_bdfconv(output_text):
bdf_file = bdf_file_menu.get()
if not bdf_file:
output_text.delete(1.0, tk.END)
output_text.insert(tk.END, "Please select a BDF file.")
return
cmd = [
'./bdfconv',
os.path.join(bdf_dir, bdf_file),
'-b', '0',
'-f', '1',
'-M', os.path.join(temp_dir, 'gb.map'),
'-n', 'kalicyh',
'-o', os.path.join(temp_dir, '_kalicyh_u8g2.c')
]
try:
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
with open(os.path.join(temp_dir, '_kalicyh_u8g2.c'), 'r', encoding='utf-8') as f:
c_content = f.read()
modified_content = filter_comments_and_modify_c_content(c_content)
output_text.delete(1.0, tk.END)
output_text.insert(tk.END, modified_content)
except subprocess.CalledProcessError as e:
output_text.delete(1.0, tk.END)
output_text.insert(tk.END, "Error occurred while running the command:\n" + e.stderr)
def on_convert_click(input_text, output_text):
input_file = os.path.join(temp_dir, 'gb.txt')
output_file = os.path.join(temp_dir, 'gb.map')
text_content = input_text.get(1.0, tk.END).strip()
with open(input_file, 'w', encoding='utf-8', newline='') as f:
f.write(text_content)
process_file(input_file, output_file)
run_bdfconv(output_text)
def choose_file(input_text):
file_path = filedialog.askopenfilename(filetypes=[("All Files", "*.*")])
if file_path:
with codecs.open(file_path, 'r', 'utf-8') as f:
content = f.read()
content_without_comments = filter_comments(content)
chinese_content = extract_chinese(content_without_comments)
input_text.delete(1.0, tk.END)
input_text.insert(tk.END, chinese_content)
def choose_folder():
folder_path = filedialog.askdirectory()
if folder_path:
# 获取用户输入的文件后缀并拆分为列表
extensions = extension_entry.get().strip().split(',')
extensions = [ext.strip().lower() for ext in extensions] # 转换为小写并去掉多余的空格
# 更新进度条
progress['value'] = 0
root.update_idletasks()
# 获取文件总数
file_count = sum([len(files) for r, d, files in os.walk(folder_path) if any(file.lower().endswith(tuple(extensions)) for file in files)])
processed_count = 0
# 使用 os.walk() 递归遍历文件夹
for root_dir, dirs, files in os.walk(folder_path):
for file_name in files:
# 仅处理用户输入的文件后缀
if file_name.lower().endswith(tuple(extensions)):
file_path = os.path.join(root_dir, file_name)
if os.path.isfile(file_path): # 确保只处理文件
try:
# 自动检测文件编码
with open(file_path, 'rb') as f:
raw_data = f.read()
result = chardet.detect(raw_data)
encoding = result['encoding']
# 使用检测到的编码读取文件内容
with codecs.open(file_path, 'r', encoding=encoding, errors='ignore') as f:
content = f.read()
# 过滤注释并提取中文内容
content_without_comments = filter_comments(content)
chinese_content = extract_chinese(content_without_comments)
# 在输入文本框中插入提取到的中文内容
input_text.insert(tk.END, f'{chinese_content}')
# 保存提取的中文内容到临时文件
input_file = os.path.join(temp_dir, 'gb.txt')
with open(input_file, 'w', encoding='utf-8', newline='') as f:
f.write(chinese_content)
# 处理文件并生成输出内容
output_file = os.path.join(temp_dir, 'gb.map')
process_file(input_file, output_file)
# 更新进度条
processed_count += 1
progress['value'] = (processed_count / file_count) * 100
root.update_idletasks()
except Exception as e:
# 错误处理
print(f"Error processing file {file_path}: {e}")
# 完成后将进度条设置为100%
progress['value'] = 100
root.update_idletasks()
def copy_output(output_text):
output_content = output_text.get(1.0, tk.END).strip()
root.clipboard_clear()
root.clipboard_append(output_content)
def on_drop(event):
file_path = event.data
if file_path:
file_path = file_path.replace('{', '').replace('}', '')
if os.path.isfile(file_path):
with codecs.open(file_path, 'r', 'utf-8') as f:
content = f.read()
content_without_comments = filter_comments(content)
chinese_content = extract_chinese(content_without_comments)
input_text.delete(1.0, tk.END)
input_text.insert(tk.END, chinese_content)
def update_bdf_files():
bdf_files = [f for f in os.listdir(bdf_dir) if f.endswith('.bdf')]
menu = bdf_file_dropdown['menu']
menu.delete(0, 'end')
for bdf_file in bdf_files:
menu.add_command(label=bdf_file, command=tk._setit(bdf_file_menu, bdf_file))
def remove_duplicates():
text_content = input_text.get(1.0, tk.END).strip()
unique_content = ''.join(sorted(set(text_content), key=text_content.index))
input_text.delete(1.0, tk.END)
input_text.insert(tk.END, unique_content)
def sort_text_by_unicode():
text_content = input_text.get(1.0, tk.END).strip()
sorted_content = ''.join(sorted(text_content))
input_text.delete(1.0, tk.END)
input_text.insert(tk.END, sorted_content)
# 创建主窗口
root = TkinterDnD.Tk()
root.title("Unicode转换工具")
# 在主窗口中添加一个 Frame 容器用于放置 Label 和 Entry
extension_frame = tk.Frame(root)
extension_frame.pack(pady=5)
# 在 Frame 中添加扩展名的 Label
extension_label = tk.Label(extension_frame, text="输入处理文件的后缀 (如:c,h,txt):")
extension_label.pack(side=tk.LEFT)
# 在 Frame 中添加扩展名的 Entry,默认值为 'c,h'
extension_entry = tk.Entry(extension_frame)
extension_entry.insert(0, "c,h") # 设置默认值为 'c,h'
extension_entry.pack(side=tk.LEFT)
# 创建输入文本框
input_label = tk.Label(root, text="输入文本/选择文件/拖入文件:")
input_label.pack()
input_text = scrolledtext.ScrolledText(root, height=10)
input_text.pack()
add_static_var = tk.BooleanVar(value=True) # 默认选中
remove_array_length_var = tk.BooleanVar(value=True)
# 创建复选框容器 Frame
checkbox_frame = tk.Frame(root)
checkbox_frame.pack(pady=5)
# 添加static复选框
add_static_checkbox = tk.Checkbutton(checkbox_frame, text="在开头添加 static", variable=add_static_var)
add_static_checkbox.pack(side=tk.LEFT, padx=10)
# 移除数组长度复选框
remove_array_length_checkbox = tk.Checkbutton(checkbox_frame, text="移除数组长度", variable=remove_array_length_var)
remove_array_length_checkbox.pack(side=tk.LEFT, padx=10)
# 注册拖放功能
input_text.drop_target_register(DND_FILES)
input_text.dnd_bind('<<Drop>>', on_drop)
# 创建按钮容器 Frame
button_frame = tk.Frame(root)
button_frame.pack(pady=10)
# 创建选择文件按钮
choose_file_button = tk.Button(button_frame, text="选择文件", command=lambda: choose_file(input_text))
choose_file_button.pack(side=tk.LEFT, padx=5)
# 创建选择文件夹按钮
choose_folder_button = tk.Button(button_frame, text="选择文件夹", command=choose_folder)
choose_folder_button.pack(side=tk.LEFT, padx=5)
# 创建去重按钮
remove_duplicates_button = tk.Button(button_frame, text="去重", command=remove_duplicates)
remove_duplicates_button.pack(side=tk.LEFT, padx=5)
# 创建排序按钮
sort_button = tk.Button(button_frame, text="排序", command=sort_text_by_unicode)
sort_button.pack(side=tk.LEFT, padx=5)
# 创建下拉选择框
bdf_frame = tk.Frame(root)
bdf_frame.pack(fill=tk.X, padx=10, pady=5)
bdf_file_label = tk.Label(bdf_frame, text="选择BDF文件:")
bdf_file_label.pack(side=tk.LEFT)
bdf_file_menu = tk.StringVar(root)
bdf_file_menu.set("fusion-pixel-10px-monospaced-zh_hans.bdf")
bdf_file_dropdown = tk.OptionMenu(bdf_frame, bdf_file_menu, [])
bdf_file_dropdown.pack(side=tk.LEFT)
update_bdf_files()
# 创建输出文本框
output_label = tk.Label(root, text="输出文件内容 (.c):")
output_label.pack()
output_text = scrolledtext.ScrolledText(root, height=20)
output_text.pack()
# 创建一个 Frame 容器来并排放置按钮
button_frame = tk.Frame(root)
button_frame.pack()
# 创建转换按钮
convert_button = tk.Button(button_frame, text="转换", command=lambda: on_convert_click(input_text, output_text))
convert_button.pack(side=tk.LEFT, padx=10)
# 创建复制按钮
copy_button = tk.Button(button_frame, text="复制", command=lambda: copy_output(output_text))
copy_button.pack(side=tk.LEFT, padx=10)
# 创建进度条
progress = ttk.Progressbar(root, orient="horizontal", length=400, mode="determinate")
progress.pack(pady=10)
# 启动GUI主循环
root.mainloop()