From e899c29c7a20ba79c7efcffa2e1714856717e8d2 Mon Sep 17 00:00:00 2001 From: subframe7536 <1667077010@qq.com> Date: Fri, 27 Dec 2024 10:17:56 +0800 Subject: [PATCH] parse `.glyphs` using `glyphsLib` --- build.py | 59 ++++++++++++++++++---------------------------- requirements.txt | 3 ++- source/py/utils.py | 24 +++++++++++-------- 3 files changed, 39 insertions(+), 47 deletions(-) diff --git a/build.py b/build.py index ca68c9a..688bd24 100755 --- a/build.py +++ b/build.py @@ -532,42 +532,34 @@ def drop_mac_names(dir: str): def rename_glyph_name( font: TTFont, - old_glyph_name: str, - new_glyph_name: str, map: dict[str, str], post_extra_names: bool = True, ): + not_ci = not is_ci() glyph_names = font.getGlyphOrder() modified = False for i, _ in enumerate(glyph_names): _old = str(glyph_names[i]) - if _old == old_glyph_name: - glyph_names[i] = new_glyph_name - print(f"{old_glyph_name} renamed to {new_glyph_name}") - modified = True - # elif _old.startswith("uni"): - # new_name = map.get(_old) - # if new_name: - # print(f"{_old} renamed to {new_name}") - # glyph_names[i] = new_name - # modified = True - # else: - # arr = re.split(r"[\._]", _old, maxsplit=2) - # print(arr[0]) - # _new = map.get(arr[0]) - # if _new: - # print(f"{_old} renamed to {_new + arr[1]}") - # glyph_names[i] = _new + arr[1] - # modified = True + if not _old.startswith("uni"): + continue + + _new = map.get(_old) + if not _new or _new == _old: + continue + + if not_ci: + print(f"[Rename] {_old} -> {_new}") + glyph_names[i] = _new + modified = True + + if post_extra_names: + index = font["post"].extraNames.index(_old) + if index != -1: + font["post"].extraNames[index] = _new if modified: font.setGlyphOrder(glyph_names) - if post_extra_names: - index = font["post"].extraNames.index(old_glyph_name) - if index != -1: - font["post"].extraNames[index] = new_glyph_name - def get_unique_identifier( font_config: FontConfig, @@ -927,17 +919,12 @@ def main(): font = TTFont(input_file) # fix auto rename by FontLab - with open( - input_file.replace(".ttf", ".glyphs").replace("-VF", ""), - "r", - encoding="utf-8", - ) as f: - rename_glyph_name( - font=font, - old_glyph_name="uni2047.liga", - new_glyph_name="question_question.liga", - map=match_unicode_names(f.read()), - ) + rename_glyph_name( + font=font, + map=match_unicode_names( + input_file.replace(".ttf", ".glyphs").replace("-VF", "") + ), + ) font.save( input_file.replace(build_option.src_dir, build_option.output_variable) diff --git a/requirements.txt b/requirements.txt index fb29d03..24270bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -foundrytools-cli==1.1.22 \ No newline at end of file +foundrytools-cli==1.1.22 +glyphsLib==6.6.1 \ No newline at end of file diff --git a/source/py/utils.py b/source/py/utils.py index 9c5fe9c..ee51b5f 100644 --- a/source/py/utils.py +++ b/source/py/utils.py @@ -1,12 +1,11 @@ from os import environ, path, remove import platform -import re import shutil import subprocess from urllib.request import Request, urlopen from zipfile import ZipFile from fontTools.ttLib import TTFont - +from glyphsLib import GSFont def is_ci(): ci_envs = [ @@ -170,11 +169,16 @@ def download_cn_base_font( ) -def match_unicode_names(text: str) -> dict[str, str]: - pattern = r"glyphname = ([^;]+);[\s\S]*?unicode = (\d+);" - matches = re.findall(pattern, text) - return { - f"uni{int(match[1]):04X}": match[0].strip('"') - for match in matches - if not str(match[0]).startswith("uni") - } +def match_unicode_names(file_path: str) -> dict[str, str]: + font = GSFont(file_path) + result = {} + + for glyph in font.glyphs: + glyph_name = glyph.name + unicode_values = glyph.unicode + + if glyph_name and unicode_values: + unicode_str = f"uni{''.join(unicode_values).upper().zfill(4)}" + result[unicode_str]=glyph_name + + return result