import
tkinterdnd2 as dnd2
from
tkinterdnd2
import
DND_FILES
from
tkinter
import
filedialog, messagebox, ttk, simpledialog, font as tkFont
from
fontTools.ttLib
import
TTFont
import
os
import
tkinter as tk
def
select_font_file():
font_path
=
filedialog.askopenfilename(filetypes
=
[(
"TTF 字体文件"
,
"*.ttf"
)])
if
font_path:
update_entries(font_path
=
font_path)
fetch_original_font_name(font_path)
def
select_export_path():
export_path
=
filedialog.askdirectory()
if
export_path:
update_entries(export_path
=
export_path)
def
modify_and_export_font():
font_path
=
font_path_entry.get()
new_name
=
new_name_entry.get().strip()
export_path
=
export_path_entry.get()
or
os.getcwd()
if
not
font_path:
messagebox.showwarning(
"警告"
,
"请选择一个字体文件。"
)
elif
not
new_name:
messagebox.showwarning(
"警告"
,
"请输入新的字体名称。"
)
else
:
try
:
with TTFont(font_path) as font:
font[
'name'
].setName(new_name,
1
,
3
,
1
,
2052
)
font[
'name'
].removeNames(nameID
=
[
2
,
4
], platformID
=
3
, platEncID
=
1
, langID
=
2052
)
new_font_file_path
=
os.path.join(export_path, f
"{os.path.splitext(os.path.basename(font_path))[0]}_new.ttf"
)
font.save(new_font_file_path)
messagebox.showinfo(
"成功"
, f
"字体名称修改为[{new_name}],保存到:{new_font_file_path}"
)
update_font_preview(new_font_file_path)
except
Exception as e:
messagebox.showerror(
"错误"
, f
"修改字体文件时出错:{str(e)}"
)
def
fetch_original_font_name(font_path):
try
:
with TTFont(font_path) as font:
original_name
=
font[
'name'
].getName(
1
,
3
,
1
,
2052
)
if
original_name:
update_entries(font_path
=
font_path, original_name
=
original_name.string.decode(
'utf-16-be'
))
update_font_preview(font_path)
else
:
messagebox.showwarning(
"警告"
,
"字体文件有问题。无法获取原名,您可以直接修改。"
)
except
Exception as e:
messagebox.showerror(
"错误"
, f
"读取字体文件时出错:{str(e)}"
)
def
update_entries(font_path
=
None
, original_name
=
None
, export_path
=
None
):
if
font_path:
font_path_entry.config(state
=
'normal'
)
font_path_entry.delete(
0
, tk.END)
font_path_entry.insert(
0
, font_path)
font_path_entry.config(state
=
'readonly'
)
if
original_name:
original_name_entry.delete(
0
, tk.END)
original_name_entry.insert(
0
, original_name)
if
export_path:
export_path_entry.delete(
0
, tk.END)
export_path_entry.insert(
0
, export_path)
def
on_drop(event):
file_path
=
event.data.strip(
'{}'
)
if
os.path.isfile(file_path)
and
file_path.lower().endswith(
'.ttf'
):
update_entries(font_path
=
file_path)
fetch_original_font_name(file_path)
else
:
messagebox.showwarning(
"警告"
,
"请拖拽一个TTF字体文件。"
)
def
center_window(window, width, height):
x
=
(window.winfo_screenwidth()
/
2
)
-
(width
/
2
)
y
=
(window.winfo_screenheight()
/
2
)
-
(height
/
2
)
window.geometry(
'%dx%d+%d+%d'
%
(width, height, x, y))
def
update_font_preview(font_path):
try
:
font_preview_label.config(font
=
(os.path.splitext(os.path.basename(font_path))[
0
],
20
))
except
Exception as e:
messagebox.showerror(
"错误"
, f
"加载字体文件时出错:{str(e)}"
)
win
=
dnd2.Tk()
win.title(
'TTF字体名称编辑器V1.00'
)
win.geometry(
'410x250'
)
center_window(win,
400
,
200
)
win.attributes(
'-topmost'
,
True
)
win.resizable(
False
,
False
)
win.drop_target_register(DND_FILES)
win.dnd_bind(
'<<Drop>>'
, on_drop)
font_path_frame
=
tk.Frame(win)
font_path_frame.pack(pady
=
2
)
font_path_entry
=
ttk.Entry(font_path_frame, width
=
45
, state
=
'readonly'
)
font_path_entry.pack(side
=
tk.LEFT)
font_path_entry.config(state
=
'normal'
)
font_path_entry.insert(
0
,
"支持拖放操作"
)
font_path_entry.config(state
=
'readonly'
)
ttk.Button(font_path_frame, text
=
'选择字体'
, command
=
select_font_file).pack(side
=
tk.LEFT)
names_frame
=
tk.Frame(win)
names_frame.pack(pady
=
2
)
original_name_frame
=
tk.Frame(names_frame)
original_name_frame.pack(side
=
tk.LEFT)
original_name_label
=
tk.Label(original_name_frame, text
=
'原字体名称'
)
original_name_label.pack(side
=
tk.TOP)
original_name_entry
=
ttk.Entry(original_name_frame, width
=
26
)
original_name_entry.pack(side
=
tk.TOP)
new_name_frame
=
tk.Frame(names_frame)
new_name_frame.pack(side
=
tk.RIGHT)
new_name_label
=
tk.Label(new_name_frame, text
=
'新字体名称'
)
new_name_label.pack(side
=
tk.TOP)
new_name_entry
=
ttk.Entry(new_name_frame, width
=
26
)
new_name_entry.pack(side
=
tk.TOP)
tk.Label(names_frame, text
=
'⮕'
, font
=
('
', 12, '
bold
'), fg='
blue').pack(side
=
tk.BOTTOM)
export_path_frame
=
tk.Frame(win)
export_path_frame.pack(pady
=
2
)
export_path_entry
=
ttk.Entry(export_path_frame, width
=
45
)
export_path_entry.pack(side
=
tk.LEFT)
export_path_entry.insert(
0
, os.getcwd())
ttk.Button(export_path_frame, text
=
'导出路径'
, command
=
select_export_path).pack(side
=
tk.LEFT)
ttk.Button(win, text
=
'修改并导出'
, command
=
modify_and_export_font).pack(pady
=
5
)
font_preview_frame
=
tk.Frame(win)
font_preview_frame.pack(pady
=
5
)
font_preview_label
=
ttk.Label(font_preview_frame, text
=
"此行显示字体预览文本"
, font
=
('
', 20, '
bold
'), foreground='
blue')
font_preview_label.pack()
win.mainloop()