Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix_glyph_names() #446

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Lib/gftools/fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down