From ba52a32fedcdf17ebadb8895249230ab1e86a351 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 19 Feb 2018 19:06:43 -0800 Subject: [PATCH] [varStore] Refactor code around --- Lib/fontTools/subset/__init__.py | 45 ++++-------------------------- Lib/fontTools/varLib/varStore.py | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 39 deletions(-) diff --git a/Lib/fontTools/subset/__init__.py b/Lib/fontTools/subset/__init__.py index a7f641890..3a970cce4 100644 --- a/Lib/fontTools/subset/__init__.py +++ b/Lib/fontTools/subset/__init__.py @@ -16,7 +16,6 @@ import array import logging from collections import Counter from types import MethodType -from functools import partial __usage__ = "pyftsubset font-file [glyph...] [--option=value]..." @@ -1624,36 +1623,6 @@ def subset_glyphs(self, s): return True -def _Device_recordVarIdx(self, s): - """Add VarIdx in this Device table (if any) to the set s.""" - if self.DeltaFormat == 0x8000: - s.add((self.StartSize<<16)+self.EndSize) - -def _Device_mapVarIdx(self, mapping): - """Add VarIdx in this Device table (if any) to the set s.""" - if self.DeltaFormat == 0x8000: - varIdx = mapping[(self.StartSize<<16)+self.EndSize] - self.StartSize = varIdx >> 16 - self.EndSize = varIdx & 0xFFFF - - -def _visit(self, objType, func): - """Recurse down from self, if type of an object is objType, - call func() on it. Only works for otData-style classes.""" - - if type(self) == objType: - func(self) - return # We don't recurse down; don't need to. - - if isinstance(self, list): - for that in self: - _visit(that, objType, func) - - if hasattr(self, 'getConverters'): - for conv in self.getConverters(): - that = getattr(self, conv.name, None) - _visit(that, objType, func) - def _pruneGDEF(font): if 'GDEF' not in font: return gdef = font['GDEF'] @@ -1665,23 +1634,21 @@ def _pruneGDEF(font): usedVarIdxes = set() # Collect. - adder = partial(_Device_recordVarIdx, s=usedVarIdxes) - _visit(table, otTables.Device, adder) + table.collect_device_varidxes(usedVarIdxes) if 'GSUB' in font: - _visit(font['GSUB'].table, otTables.Device, adder) + font['GSUB'].table.collect_device_varidxes(usedVarIdxes) if 'GPOS' in font: - _visit(font['GPOS'].table, otTables.Device, adder) + font['GPOS'].table.collect_device_varidxes(usedVarIdxes) # Subset. varidx_map = store.subset_varidxes(usedVarIdxes) # Map. - mapper = partial(_Device_mapVarIdx, mapping=varidx_map) - _visit(table, otTables.Device, mapper) + table.remap_device_varidxes(varidx_map) if 'GSUB' in font: - _visit(font['GSUB'].table, otTables.Device, mapper) + font['GSUB'].table.remap_device_varidxes(varidx_map) if 'GPOS' in font: - _visit(font['GPOS'].table, otTables.Device, mapper) + font['GPOS'].table.remap_device_varidxes(varidx_map) @_add_method(ttLib.getTableClass('GDEF')) def prune_post_subset(self, font, options): diff --git a/Lib/fontTools/varLib/varStore.py b/Lib/fontTools/varLib/varStore.py index 67a45800d..7d8035283 100644 --- a/Lib/fontTools/varLib/varStore.py +++ b/Lib/fontTools/varLib/varStore.py @@ -6,6 +6,7 @@ from fontTools.varLib.models import supportScalar from fontTools.varLib.builder import (buildVarRegionList, buildVarStore, buildVarRegion, buildVarData, VarData_CalculateNumShorts) +from functools import partial def _getLocationKey(loc): @@ -175,3 +176,49 @@ def VarStore_subset_varidxes(self, varIdxes, optimize=True): return varDataMap ot.VarStore.subset_varidxes = VarStore_subset_varidxes + + +def _visit(self, objType, func): + """Recurse down from self, if type of an object is objType, + call func() on it. Only works for otData-style classes.""" + + if type(self) == objType: + func(self) + return # We don't recurse down; don't need to. + + if isinstance(self, list): + for that in self: + _visit(that, objType, func) + + if hasattr(self, 'getConverters'): + for conv in self.getConverters(): + that = getattr(self, conv.name, None) + _visit(that, objType, func) + +def _Device_recordVarIdx(self, s): + """Add VarIdx in this Device table (if any) to the set s.""" + if self.DeltaFormat == 0x8000: + s.add((self.StartSize<<16)+self.EndSize) + +def Object_collect_device_varidxes(self, varidxes): + adder = partial(_Device_recordVarIdx, s=varidxes) + _visit(self, ot.Device, adder) + +ot.GDEF.collect_device_varidxes = Object_collect_device_varidxes +ot.GSUB.collect_device_varidxes = Object_collect_device_varidxes +ot.GPOS.collect_device_varidxes = Object_collect_device_varidxes + +def _Device_mapVarIdx(self, mapping): + """Add VarIdx in this Device table (if any) to the set s.""" + if self.DeltaFormat == 0x8000: + varIdx = mapping[(self.StartSize<<16)+self.EndSize] + self.StartSize = varIdx >> 16 + self.EndSize = varIdx & 0xFFFF + +def Object_remap_device_varidxes(self, varidxes_map): + mapper = partial(_Device_mapVarIdx, mapping=varidxes_map) + _visit(self, ot.Device, mapper) + +ot.GDEF.remap_device_varidxes = Object_remap_device_varidxes +ot.GSUB.remap_device_varidxes = Object_remap_device_varidxes +ot.GPOS.remap_device_varidxes = Object_remap_device_varidxes