diff --git a/Lib/gftools/fix.py b/Lib/gftools/fix.py index 2a4de04e9..f4a097f0a 100644 --- a/Lib/gftools/fix.py +++ b/Lib/gftools/fix.py @@ -8,6 +8,7 @@ from fontTools.ttLib.tables import ttProgram from fontTools.ttLib.tables._c_m_a_p import CmapSubtable from fontTools.ttLib.tables._f_v_a_r import NamedInstance +from ufo2ft.postProcessor import PostProcessor from gftools.util.google_fonts import _KNOWN_WEIGHTS from gftools.utils import ( download_family_from_Google_Fonts, @@ -25,6 +26,7 @@ from gftools.util.styles import (get_stylename, is_regular, is_bold, is_italic) from gftools.stat import gen_stat_tables +from collections import namedtuple from os.path import basename, splitext from copy import deepcopy import logging @@ -752,6 +754,29 @@ def drop_mac_names(ttfont): changed = True return changed +def fix_glyph_names(ttFont): + """Renames the font's glyphs to production names.""" + # Piggy-back ufo2ft's implementation of production names + # This wants a glyphSet which maps names to objects which + # contain .name and .unicode attributes + fake_layer = namedtuple("fake_layer", ["name", "unicode"]) + cmap = ttFont["cmap"].buildReversed() + glyphset = {} + old_names = ttFont.getGlyphOrder() + for glyph in old_names: + if glyph in cmap: + codepoint = list(cmap.get(glyph))[0] + else: + codepoint = None + glyphset[glyph] = fake_layer(glyph, codepoint) + # We have no UFO, so we have to fake up the object + pp = PostProcessor.__new__(PostProcessor) + pp.otf = ttFont + pp.glyphSet = glyphset + pp._postscriptNames = None + pp.process_glyph_names(useProductionNames=True) + return old_names != ttFont.getGlyphOrder() + def fix_font(font, include_source_fixes=False, new_family_name=None): if new_family_name: