style(fonttool): 把 fonttool 使用 pyinstall 进行打包

This commit is contained in:
YuleBest committed 2026-02-04 14:07:20 +08:00
1 parent d8a70b93ad
commit 33030685d5
7 files changed
+74 -71

No files matched your search

+6 -2
View File
@@ -8,6 +8,7 @@
SCRIPT_DIR="$(dirname "$(dirname "$(readlink -f "$0")")")"
WORK_DIR="$SCRIPT_DIR/WORK"
export PATH="$PATH:$SCRIPT_DIR/功能/bin"
FT_BIN="$SCRIPT_DIR/功能/bin/fonttool"
clear
# 环境检查
@@ -31,10 +32,13 @@ if ! file "$ttc_file" | grep -q "TrueType font collection"; then
exit 2
fi
if ttc_splitter "$ttc_file" -o "$WORK_DIR/ttc_separated"; then
echo "~ 正在使用 fonttool 拆分 TTC..."
"$FT_BIN" split "$ttc_file" "$WORK_DIR/ttc_separated"
if [ $? -eq 0 ]; then
echo "~ 拆分成功"
exit 0
else
echo "! 有文件拆分失败"
echo "! 拆分失败"
exit 3
fi
+3
View File
@@ -2,3 +2,6 @@
## yq
- MIT license
- https://github.com/mikefarah/yq/releases/tag/v4.52.2
## fonttool
- MIT license
- https://github.com/fonttools/fonttools
Binary file not shown.
+64
View File
@@ -0,0 +1,64 @@
import sys
import os
import importlib
from fontTools.ttLib import TTCollection
# 配置工具映射
TOOLS = {
'ttx': 'fontTools.ttx',
'subset': 'fontTools.subset',
'merge': 'fontTools.merge',
}
def split_ttc(ttc_path, out_dir):
if not os.path.exists(out_dir):
os.makedirs(out_dir)
try:
print(f"[*] 正在读取: {ttc_path}")
collection = TTCollection(ttc_path)
print(f"[*] 检测到 {len(collection)} 个子字体")
for i, font in enumerate(collection):
# 这里的获取逻辑更稳健一点
name_record = font['name'].getDebugName(1)
name = name_record if name_record else f"subfont_{i}"
safe_name = "".join([c for c in name if c.isalnum() or c in (' ', '_')]).strip()
save_path = os.path.join(out_dir, f"{safe_name}.ttf")
font.save(save_path)
print(f"[+] 已保存: {save_path}")
return True
except Exception as e:
print(f"[!] 拆分失败: {e}")
return False
def main():
# 打印当前收到的参数,方便调试(打包后如果报错能看到原因)
# print(f"DEBUG: sys.argv = {sys.argv}")
if len(sys.argv) < 2:
print("FontTool Binary Toolkit")
print("用法: fonttool [split|ttx|subset|merge] [args...]")
sys.exit(1)
cmd = sys.argv[1]
if cmd == 'split':
if len(sys.argv) < 4:
print("用法: fonttool split <TTC文件> <输出目录>")
sys.exit(1)
success = split_ttc(sys.argv[2], sys.argv[3])
sys.exit(0 if success else 1)
elif cmd in TOOLS:
module = importlib.import_module(TOOLS[cmd])
# 彻底重构 argv,去掉 'fonttool' 和 'ttx' 这类前缀
sys.argv = sys.argv[1:]
module.main()
else:
# 如果不是内置命令,也不是映射命令,报错退出,而不是回退给 ttx
print(f"[错误] 未知指令: {cmd}")
print("支持: split, ttx, subset, merge")
sys.exit(1)
if __name__ == "__main__":
main()
+1
View File
@@ -0,0 +1 @@
889a660beb54dad3cb1e96712869bd1f1fee18bcd819809fbfae5da49bf0bb02 fonttool
-68
View File
@@ -1,68 +0,0 @@
import os
import sys
import argparse
from fontTools.ttLib import TTCollection
def get_font_name(font):
"""从字体元数据中提取可读名称"""
name = ""
# 优先寻找英文全名 (nameID 4, platformID 3)
for record in font['name'].names:
if record.nameID == 4:
try:
name = record.toUnicode().replace(" ", "_")
if record.platformID == 3: # Windows 平台 ID 比较稳
break
except:
continue
return name if name else "unknown_font"
def main():
parser = argparse.ArgumentParser(description="TTC 字体提取工具 - 由于乐 Yule 维护")
parser.add_argument("input", help="输入的 .ttc 文件路径")
parser.add_argument("-o", "--output", help="输出目录 (默认为当前目录下的 extracted_fonts)")
args = parser.parse_args()
input_file = args.input
output_base = args.output if args.output else os.path.join(os.getcwd(), "extracted_fonts")
if not os.path.isfile(input_file):
print(f"[-] 错误: 找不到输入文件 '{input_file}'")
return
if not os.path.exists(output_base):
os.makedirs(output_base)
print(f"[*] 已创建输出目录: {output_base}")
print("="*50)
print(f"[*] 正在读取集合: {os.path.basename(input_file)}")
try:
collection = TTCollection(input_file)
total = len(collection)
print(f"[*] 发现子字体总数: {total}")
print("="*50)
for i, font in enumerate(collection):
font_name = get_font_name(font)
file_name = f"{font_name}.ttf"
output_path = os.path.join(output_base, file_name)
# 处理重名问题
counter = 1
while os.path.exists(output_path):
output_path = os.path.join(output_base, f"{font_name}_{counter}.ttf")
counter += 1
print(f"[{i+1}/{total}] 正在导出: {os.path.basename(output_path)} ...", end="\r")
font.save(output_path)
print(f"\n" + "="*50)
print(f"[+] 提取完成!所有文件保存在: {output_base}")
except Exception as e:
print(f"\n[-] 发生致命错误: {e}")
if __name__ == "__main__":
main()
-1
View File
@@ -1 +0,0 @@
943df550b1fe5725230ca6ceae92fee3dde549a6c7b9080508bf6d945089b762