RiderLty / fontInAss

实时将字体子集化后嵌入ass的小工具,用于在未安装对应字体的系统上正确显示字幕。
44 stars 1 forks source link

uharfbuzz子集化同时保留table中的name,修正README.MD中某些参数数值与实际不符 #14

Closed mcratt closed 1 week ago

mcratt commented 1 week ago

看看这样对不对,是不是这个。

image

import logging
from io import BytesIO
import uharfbuzz
from fontTools.ttLib import TTFont

from src import utils

logger = logging.getLogger(f'{"main"}:{"loger"}')

filepath = "../fonts/收集字体/方正中雅宋_GBK.TTF"
fontBytes = open(filepath, "rb").read()
fontName = filepath

unicodeSet = {24320, 32773, 24207, 27792, 38, 40, 41, 22312, 44, 20013, 48, 52, 53, 54, 55, 72, 78, 39118, 25299, 26202, 92, 31456, 98, 99, 100, 102, 111, 112, 114, 115, 28020, 125}
# test_freetype(fontBytes,fontName)
# test_fonttools(fontBytes,fontName)
face = uharfbuzz.Face(fontBytes,0)

bio = BytesIO()
bio.write(fontBytes)
bio.seek(0)
target = TTFont(bio)
print(fontName + "那我是之前的:" + str(target["name"].names))
print(fontName + "那我是之前的长度:" + str(len(target["name"].names)))

font = uharfbuzz.Font(face)
# 初始化子集化UNICODE
inp = uharfbuzz.SubsetInput()
inp.sets(uharfbuzz.SubsetInputSets.UNICODE).set(unicodeSet)
# inp.keep_everything()
# inp.sets(uharfbuzz.SubsetFlags.PASSTHROUGH_UNRECOGNIZED)
# print(face.get_table_script_tags("cmap"))
# inp.sets(uharfbuzz.SubsetInputSets.NO_SUBSET_TABLE_TAG).set()
# inp.sets(uharfbuzz.SubsetFlags.PASSTHROUGH_UNRECOGNIZED)
# inp.sets(uharfbuzz.SubsetInputSets.NO_SUBSET_TABLE_TAG).set({1146308935, 1161970772, 1161972803, 1161974595, 1181049204, 1198285172, 1198288739, 1246975046, 1280594760, 1346587732, 1398163232, 1399417958, 1399417964, 1801810542, 1801810552, 1836020340, 1836020344})
print(face.table_tags)
# print(uharfbuzz.ot_tag_to_script('cmap'))
# inp.sets(uharfbuzz.SubsetInputSets.NO_SUBSET_TABLE_TAG).set(uharfbuzz.ot_tag_to_language("name"))
# s = inp.sets(uharfbuzz.SubsetInputSets.NO_SUBSET_TABLE_TAG).set({1852070485})
# print(s)
# 需要保留的table的tag
if "name" in face.table_tags:
    inp.sets(uharfbuzz.SubsetInputSets.NO_SUBSET_TABLE_TAG).set({utils.tag_to_integer("name")})

# 子集化
face = uharfbuzz.subset(face, inp)
# ================================================

bio = BytesIO()
bio.write(face.blob.data)
bio.seek(0)
target = TTFont(bio)
print(fontName + "我是处理后的:" + str(target["name"].names))
print(fontName + "我是处理后的长度:" + str(len(target["name"].names)))
RiderLty commented 1 week ago

image image 可以了,这就去更新下docker gj👍