Skip to content

Commit 286c6ce

Browse files
committed
fix TTC/OTC searching, improve FontFamilyName searching.
1 parent cda0362 commit 286c6ce

File tree

1 file changed

+68
-40
lines changed

1 file changed

+68
-40
lines changed

ASFMKV_dev.py

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from fontTools import ttLib, subset
1313
from chardet.universaldetector import UniversalDetector
1414
from chardet import detect
15-
import os, sys, re, winreg, zlib, json, copy, traceback, shutil, configparser, time, locale, codecs
15+
import os, sys, re, winreg, zlib, json, copy, traceback, shutil, configparser, time, locale, codecs, argparse
1616
from os import path
1717
from colorama import init
1818
from datetime import datetime
@@ -1143,6 +1143,9 @@ def outputSameLength(s: str) -> str:
11431143
return output + ''.join([' ' for _ in range(0, 60 - length)])
11441144

11451145

1146+
# 当前字体缓存版本,小于该版本的字体缓存会被删除重建
1147+
currentFontCacheVer = 2
1148+
11461149
# font_info 列表结构
11471150
# [ font_name, font_n_lower, font_family, warning_font, font_all ]
11481151
# font_name 词典结构
@@ -1228,6 +1231,11 @@ def fontProgress(fl: list, font_info: list = [{}, {}, {}, {}, {}], overwrite: bo
12281231
jsonFile = open(fontCacheFile, mode='rb')
12291232
fontCache[dirCRCKey] = json.loads(zlib.decompress(jsonFile.read()).decode('utf-8'))
12301233
jsonFile.close()
1234+
1235+
if fontCache[dirCRCKey].get('?version', 0) < currentFontCacheVer:
1236+
print('\n\033[1;31m[WARNING] 已移除旧版本字体缓存文件 \"{}\"\033[0m'.format(dirCRCKey))
1237+
os.remove(fontCacheFile)
1238+
fontCache.pop(dirCRCKey)
12311239
except:
12321240
print('\n\033[1;31m[ERROR] 缓存文件读取失败 \"{}\"\033[0m'.format(dirCRCKey))
12331241
os.remove(fontCacheFile)
@@ -1298,13 +1306,13 @@ def fontProgress(fl: list, font_info: list = [{}, {}, {}, {}, {}], overwrite: bo
12981306
if len(fontCRCKey) == 0:
12991307
fontCRCKey = hex(zlib.crc32((s.lower() + str(path.getsize(s)) + str(path.getctime(s))).encode('utf-8')))[2:].upper().rjust(8, '0')
13001308
if not fontCacheN.get(dirCRCKey, False):
1301-
fontCacheN[dirCRCKey] = {fontCRCKey: {}}
1309+
fontCacheN[dirCRCKey] = {fontCRCKey: {}, '?version': currentFontCacheVer}
13021310
else:
13031311
fontCacheN[dirCRCKey].update({fontCRCKey: {}})
13041312

13051313
# 处理字体信息
13061314
for ti, t in enumerate(tc):
1307-
1315+
13081316
tencodings = []
13091317

13101318
if readFontCache is None:
@@ -1438,7 +1446,15 @@ def fontProgress(fl: list, font_info: list = [{}, {}, {}, {}, {}], overwrite: bo
14381446
else:
14391447
font_family[fN].setdefault((isItalic, isBold), [namestr])
14401448

1441-
if readFontCache is None: t.close()
1449+
if len(dictFamilyN) >= 1:
1450+
for k, v in dictFamilyN.items():
1451+
if k == 'other': continue
1452+
if k not in dictNameStr:
1453+
namestr = list(dictNameStr.values())[-1][0]
1454+
if namestr.strip(' '):
1455+
font_family[v].setdefault((isItalic, isBold), [namestr])
1456+
1457+
if readFontCache is None and ti == len(tc) - 1: t.close()
14421458

14431459
f_all_item = (dictNameStr, isItalic, isBold, dictFstyle, dictFamilyN)
14441460
if s in f_all:
@@ -1455,7 +1471,12 @@ def fontProgress(fl: list, font_info: list = [{}, {}, {}, {}, {}], overwrite: bo
14551471
keys = list(font_family.keys())
14561472
for k in keys:
14571473
if not len(font_family[k]) > 1:
1458-
font_family.pop(k)
1474+
keepFamily = False
1475+
for v in font_family[k].values():
1476+
if v != k:
1477+
keepFamily = True
1478+
if not keepFamily:
1479+
font_family.pop(k)
14591480
del keys
14601481

14611482
dupfont_cache = dupfont
@@ -1525,6 +1546,7 @@ def checkAssFont(fontlist: dict, font_info: list, fn_lines: list = [], onlycheck
15251546
ignoreLostFonts = noRequestFont
15261547

15271548
keys = list(fontlist.keys())
1549+
15281550
for s in keys:
15291551
isbold = s[2]
15301552
isitalic = s[1]
@@ -3687,31 +3709,31 @@ def cFontSubset(font_info):
36873709
elif work in [1, 2, 20, 21]:
36883710
cls()
36893711
if work == 1:
3690-
print('''子集化字体
3691-
搜索子目录(视频): \033[1;33m{0}\033[0m
3692-
搜索子目录(字幕): \033[1;33m{1}\033[0m
3693-
严格字幕匹配: \033[1;33m{2}\033[0m
3694-
字幕文件输出文件夹: \033[1;33m{3}\033[0m
3695-
字体文件输出文件夹: \033[1;33m{4}\033[0m
3696-
广兼容性子集化: \033[1;33m{5}\033[0m
3697-
忽略字体所缺字: \033[1;33m{8}\033[0m
3698-
子集化失败中断: \033[1;33m{6}\033[0m
3699-
使用工作目录字体:\033[1;33m{7}\033[0m
3700-
'''.format(v_subdir, s_subdir, matchStrict, assout, fontout, char_compatible, errorStop, fontload, ignoreLost))
3712+
print(f'''子集化字体
3713+
搜索子目录(视频): \033[1;33m{v_subdir}\033[0m
3714+
搜索子目录(字幕): \033[1;33m{s_subdir}\033[0m
3715+
严格字幕匹配: \033[1;33m{matchStrict}\033[0m
3716+
字幕文件输出文件夹: \033[1;33m{assout}\033[0m
3717+
字体文件输出文件夹: \033[1;33m{fontout}\033[0m
3718+
广兼容性子集化: \033[1;33m{char_compatible}\033[0m
3719+
忽略字体所缺字: \033[1;33m{ignoreLost}\033[0m
3720+
子集化失败中断: \033[1;33m{errorStop}\033[0m
3721+
使用工作目录字体:\033[1;33m{fontload}\033[0m
3722+
''')
37013723
else:
3702-
print('''子集化字体并封装
3703-
搜索子目录(视频): \033[1;33m{4}\033[0m
3704-
搜索子目录(字幕): \033[1;33m{5}\033[0m
3705-
移除内挂字幕: \033[1;33m{0}\033[0m
3706-
移除原有附件: \033[1;33m{1}\033[0m
3707-
不封装字体: \033[1;33m{2}\033[0m
3708-
严格字幕匹配: \033[1;33m{6}\033[0m
3709-
媒体文件输出文件夹: \033[1;33m{3}\033[0m
3710-
广兼容性子集化: \033[1;33m{8}\033[0m
3711-
忽略字体所缺字: \033[1;33m{10}\033[0m
3712-
子集化失败中断: \033[1;33m{7}\033[0m
3713-
使用工作目录字体:\033[1;33m{9}\033[0m
3714-
'''.format(rmAssIn, rmAttach, notfont, mkvout, v_subdir, s_subdir, matchStrict, errorStop, char_compatible, fontload, ignoreLost))
3724+
print(f'''子集化字体并封装
3725+
搜索子目录(视频): \033[1;33m{v_subdir}\033[0m
3726+
搜索子目录(字幕): \033[1;33m{s_subdir}\033[0m
3727+
移除内挂字幕: \033[1;33m{rmAssIn}\033[0m
3728+
移除原有附件: \033[1;33m{rmAttach}\033[0m
3729+
不封装字体: \033[1;33m{notfont}\033[0m
3730+
严格字幕匹配: \033[1;33m{matchStrict}\033[0m
3731+
媒体文件输出文件夹: \033[1;33m{mkvout}\033[0m
3732+
广兼容性子集化: \033[1;33m{char_compatible}\033[0m
3733+
忽略字体所缺字: \033[1;33m{ignoreLost}\033[0m
3734+
子集化失败中断: \033[1;33m{errorStop}\033[0m
3735+
使用工作目录字体:\033[1;33m{fontload}\033[0m
3736+
''')
37153737
cpath = ''
37163738
directout = False
37173739
subonly = False
@@ -3722,9 +3744,9 @@ def cFontSubset(font_info):
37223744
if cpath == '':
37233745
print('没有输入,回到上级菜单')
37243746
elif not path.isabs(cpath):
3725-
print('\033[1;31m[ERROR] 输入的必须是绝对路径!: \"{0}\"\033[0m'.format(cpath))
3747+
print(f'\033[1;31m[ERROR] 输入的必须是绝对路径!: \"{cpath}\"\033[0m')
37263748
elif not path.exists(cpath):
3727-
print('\033[1;31m[ERROR] 找不到路径: \"{0}\"\033[0m'.format(cpath))
3749+
print(f'\033[1;31m[ERROR] 找不到路径: \"{cpath}\"\033[0m')
37283750
elif path.isfile(cpath):
37293751
testext = path.splitext(cpath)[1][1:].lower()
37303752
if testext in extlist:
@@ -3733,9 +3755,9 @@ def cFontSubset(font_info):
37333755
directout = False
37343756
subonly = True
37353757
else:
3736-
print('\033[1;31m[ERROR] 扩展名不正确: \"{0}\"\033[0m'.format(cpath))
3758+
print(f'\033[1;31m[ERROR] 扩展名不正确: \"{cpath}\"\033[0m')
37373759
elif not path.isdir(cpath):
3738-
print('\033[1;31m[ERROR] 输入的应该是目录或媒体文件!: \"{0}\"\033[0m'.format(cpath))
3760+
print(f'\033[1;31m[ERROR] 输入的应该是目录或媒体文件!: \"{cpath}\"\033[0m')
37393761
else:
37403762
directout = False
37413763
# print(directout)
@@ -3857,7 +3879,7 @@ def cFontSubset(font_info):
38573879

38583880
def cLicense():
38593881
cls()
3860-
print('''AddSubFontMKV Python Remake Preview 23
3882+
print('''AddSubFontMKV Python Remake Preview 24
38613883
38623884
Apache-2.0 License
38633885
https://www.apache.org/licenses/
@@ -4105,11 +4127,17 @@ def cTextCodingTranscode():
41054127
print('输出文件夹: \033[33;1m\"{}\"\033[0m'.format(outPath))
41064128
os.system('pause')
41074129

4130+
4131+
def commandInput():
4132+
'''计划中的命令行输入支持'''
4133+
pass
4134+
4135+
41084136
no_mkvm = False
41094137
no_cmdc = False
41104138
mkvmv = ''
41114139
ffmv = ''
4112-
font_info = [{}, {}, {}, {}]
4140+
font_info = [{}, {}, {}, {}, {}]
41134141

41144142
def checkFF():
41154143
'''检查ffmpeg和ffprobe可用性'''
@@ -4123,7 +4151,7 @@ def checkFF():
41234151
def loadMain(reload: bool = False):
41244152
global extlist, no_mkvm, no_cmdc, dupfont, mkvmv, font_info, fontin, langlist, ffmv, insteadFF
41254153
# 初始化字体列表 和 mkvmerge 相关参数
4126-
os.system('title ASFMKV Python Remake Pre23 ^| (c) 2022-2024 yyfll ^| Apache-2.0')
4154+
os.system('title ASFMKV Python Remake Pre24 ^| (c) 2022-2024 yyfll ^| Apache-2.0')
41274155
if not reload:
41284156
if not o_fontload:
41294157
font_list = getFontFileList(fontin)
@@ -4170,7 +4198,7 @@ def loadMain(reload: bool = False):
41704198
if not len(ffmv) > 0:
41714199
ffMessage = '\n[F] 检查并启用FFmpeg'
41724200
ffSelect = 'F'
4173-
print('''ASFMKV Python Remake Pre23 | (c) 2022-2024 yyfll{0}{5}
4201+
print('''ASFMKV Python Remake Pre24 | (c) 2022-2024 yyfll{0}{5}
41744202
字体名称数: [\033[1;33m{2}\033[0m]({4})
41754203
请选择功能:
41764204
[A] 列出字幕所用字体
@@ -4234,16 +4262,16 @@ def loadMain(reload: bool = False):
42344262
appdata = os.getenv('APPDATA')
42354263
fontCacheDir = path.join(appdata, 'ASFMKVpy')
42364264
if not path.isdir(fontCacheDir): fontCacheDir = None
4237-
print('''字体信息重载
4238-
缓存目录: \"\033[1;33m{}\033[0m\"
4265+
print(f'''字体信息重载
4266+
缓存目录: \"\033[1;33m{fontCacheDir}\033[0m\"
42394267
42404268
[0] 回到主菜单
42414269
[1] 更新缓存
42424270
[2] 仅覆盖当前使用的缓存文件并重载
42434271
[3] 删除所有缓存文件并重载
42444272
[4] 新增自定义字体目录并重载
42454273
4246-
请选择:'''.format(fontCacheDir))
4274+
请选择:''')
42474275
work2 = os.system('choice /C 01234')
42484276

42494277
def reloadFont(usingCache: bool = True):

0 commit comments

Comments
 (0)