for i in selection.byGlyphs: assert isinstance(i, fontforge.glyph) print(i.glyphname) if "creative" not in i.glyphname: font.removeGlyph(i) else: all_fonts.append(i.glyphname) print(len(all_fonts)) font.generate("D:/blog/fontawesome-free-5.15.4-web/webfonts/fa-brands-400.minify.woff2")
# 获取使用到的图标 defget_used_icons(path): icon_dict = {} for filename in glob.glob(path, recursive=True): for extension in extensions: if extension in filename: for line inopen(filename, encoding='utf-8'): for match in re.finditer(pattern, line): icon_dict[match.group()[3:]] = True return icon_dict
# 去除除了传入的字典的图标 defminify(path, used_dict): font = fontforge.open(path) assertisinstance(font, fontforge.font) selection = font.selection assertisinstance(selection, fontforge.selection) selection.all() for i in selection.byGlyphs: assertisinstance(i, fontforge.glyph) if i.glyphname notin used_dict: font.removeGlyph(i) font.generate(path)
if __name__ == '__main__': web_path = "D:/blog/hexo/public/**" font_path = "D:/blog/hexo/public/fontawesome-free-5.15.4-web/webfonts/" font_list = ["fa-brands-400.woff2", "fa-solid-900.woff2"] used_icon_dict = get_used_icons(web_path) for font_name in font_list: minify(font_path + font_name, used_icon_dict)