Skip to content

Instantly share code, notes, and snippets.

@behdad
Created January 14, 2019 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behdad/30c216614e048418bc80a2fb63a9cca3 to your computer and use it in GitHub Desktop.
Save behdad/30c216614e048418bc80a2fb63a9cca3 to your computer and use it in GitHub Desktop.
diff --git a/Lib/fontTools/subset/__init__.py b/Lib/fontTools/subset/__init__.py
index 2ed17cc9..22d1e371 100644
--- a/Lib/fontTools/subset/__init__.py
+++ b/Lib/fontTools/subset/__init__.py
@@ -1719,6 +1719,8 @@ def subset_glyphs(self, s):
@_add_method(ttLib.getTableClass('hmtx'))
def subset_glyphs(self, s):
self.metrics = _dict_subset(self.metrics, s.glyphs)
+ for g in s.glyphs_emptied:
+ self.metrics[g] = (0,0)
return True # Required table
@_add_method(ttLib.getTableClass('hdmx'))
@@ -2063,13 +2065,16 @@ def prune_pre_subset(self, font, options):
@_add_method(ttLib.getTableClass('glyf'))
def subset_glyphs(self, s):
self.glyphs = _dict_subset(self.glyphs, s.glyphs)
- indices = [i for i,g in enumerate(self.glyphOrder) if g in s.glyphs]
- for v in self.glyphs.values():
- if hasattr(v, "data"):
- v.remapComponentsFast(indices)
- else:
- pass # No need
- self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
+ if s.options.renumber_glyphs:
+ indices = [i for i,g in enumerate(self.glyphOrder) if g in s.glyphs]
+ for v in self.glyphs.values():
+ if hasattr(v, "data"):
+ v.remapComponentsFast(indices)
+ Glyph = ttLib.getTableModule('glyf').Glyph
+ for g in s.glyphs_emptied:
+ self.glyphs[g] = Glyph()
+ self.glyphs[g].data = ''
+ self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs or g in s.glyphs_emptied]
# Don't drop empty 'glyf' tables, otherwise 'loca' doesn't get subset.
return True
@@ -2275,6 +2280,7 @@ class Options(object):
self.name_legacy = False
self.name_languages = [0x0409] # English
self.obfuscate_names = False # to make webfont unusable as a system font
+ self.renumber_glyphs = True
self.notdef_glyph = True # gid0 for TrueType / .notdef for CFF
self.notdef_outline = False # No need for notdef to have an outline really
self.recommended_glyphs = False # gid1, gid2, gid3 for TrueType
@@ -2533,12 +2539,17 @@ class Subsetter(object):
log.glyphs(self.glyphs, font=font)
self.glyphs_cffed = frozenset(self.glyphs)
- self.glyphs_all = frozenset(self.glyphs)
+ self.glyphs_retained = frozenset(self.glyphs)
+
+ self.glyphs_emptied = frozenset()
+ if not self.options.renumber_glyphs:
+ self.glyphs_emptied = realGlyphs - self.glyphs_retained
+ # TODO Drop empty glyphs at the end of GlyphOrder vector.
order = font.getReverseGlyphMap()
- self.reverseOrigGlyphMap = {g:order[g] for g in self.glyphs_all}
+ self.reverseOrigGlyphMap = {g:order[g] for g in self.glyphs_retained}
- log.info("Retaining %d glyphs", len(self.glyphs_all))
+ log.info("Retaining %d glyphs", len(self.glyphs_retained))
del self.glyphs
@@ -2551,7 +2562,7 @@ class Subsetter(object):
elif hasattr(clazz, 'subset_glyphs'):
with timer("subset '%s'" % tag):
table = font[tag]
- self.glyphs = self.glyphs_all
+ self.glyphs = self.glyphs_retained
retain = table.subset_glyphs(self)
del self.glyphs
if not retain:
@@ -2565,11 +2576,12 @@ class Subsetter(object):
log.warning("%s NOT subset; don't know how to subset; dropped", tag)
del font[tag]
- with timer("subset GlyphOrder"):
- glyphOrder = font.getGlyphOrder()
- glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
- font.setGlyphOrder(glyphOrder)
- font._buildReverseGlyphOrderDict()
+ if self.options.renumber_glyphs:
+ with timer("subset GlyphOrder"):
+ glyphOrder = font.getGlyphOrder()
+ glyphOrder = [g for g in glyphOrder if g in self.glyphs_retained]
+ font.setGlyphOrder(glyphOrder)
+ font._buildReverseGlyphOrderDict()
def _prune_post_subset(self, font):
for tag in font.keys():
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment