[varStore] Refactor code around
This commit is contained in:
parent
7657f0c899
commit
ba52a32fed
@ -16,7 +16,6 @@ import array
|
|||||||
import logging
|
import logging
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
from types import MethodType
|
from types import MethodType
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
__usage__ = "pyftsubset font-file [glyph...] [--option=value]..."
|
__usage__ = "pyftsubset font-file [glyph...] [--option=value]..."
|
||||||
|
|
||||||
@ -1624,36 +1623,6 @@ def subset_glyphs(self, s):
|
|||||||
return True
|
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):
|
def _pruneGDEF(font):
|
||||||
if 'GDEF' not in font: return
|
if 'GDEF' not in font: return
|
||||||
gdef = font['GDEF']
|
gdef = font['GDEF']
|
||||||
@ -1665,23 +1634,21 @@ def _pruneGDEF(font):
|
|||||||
usedVarIdxes = set()
|
usedVarIdxes = set()
|
||||||
|
|
||||||
# Collect.
|
# Collect.
|
||||||
adder = partial(_Device_recordVarIdx, s=usedVarIdxes)
|
table.collect_device_varidxes(usedVarIdxes)
|
||||||
_visit(table, otTables.Device, adder)
|
|
||||||
if 'GSUB' in font:
|
if 'GSUB' in font:
|
||||||
_visit(font['GSUB'].table, otTables.Device, adder)
|
font['GSUB'].table.collect_device_varidxes(usedVarIdxes)
|
||||||
if 'GPOS' in font:
|
if 'GPOS' in font:
|
||||||
_visit(font['GPOS'].table, otTables.Device, adder)
|
font['GPOS'].table.collect_device_varidxes(usedVarIdxes)
|
||||||
|
|
||||||
# Subset.
|
# Subset.
|
||||||
varidx_map = store.subset_varidxes(usedVarIdxes)
|
varidx_map = store.subset_varidxes(usedVarIdxes)
|
||||||
|
|
||||||
# Map.
|
# Map.
|
||||||
mapper = partial(_Device_mapVarIdx, mapping=varidx_map)
|
table.remap_device_varidxes(varidx_map)
|
||||||
_visit(table, otTables.Device, mapper)
|
|
||||||
if 'GSUB' in font:
|
if 'GSUB' in font:
|
||||||
_visit(font['GSUB'].table, otTables.Device, mapper)
|
font['GSUB'].table.remap_device_varidxes(varidx_map)
|
||||||
if 'GPOS' in font:
|
if 'GPOS' in font:
|
||||||
_visit(font['GPOS'].table, otTables.Device, mapper)
|
font['GPOS'].table.remap_device_varidxes(varidx_map)
|
||||||
|
|
||||||
@_add_method(ttLib.getTableClass('GDEF'))
|
@_add_method(ttLib.getTableClass('GDEF'))
|
||||||
def prune_post_subset(self, font, options):
|
def prune_post_subset(self, font, options):
|
||||||
|
@ -6,6 +6,7 @@ from fontTools.varLib.models import supportScalar
|
|||||||
from fontTools.varLib.builder import (buildVarRegionList, buildVarStore,
|
from fontTools.varLib.builder import (buildVarRegionList, buildVarStore,
|
||||||
buildVarRegion, buildVarData,
|
buildVarRegion, buildVarData,
|
||||||
VarData_CalculateNumShorts)
|
VarData_CalculateNumShorts)
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
|
||||||
def _getLocationKey(loc):
|
def _getLocationKey(loc):
|
||||||
@ -175,3 +176,49 @@ def VarStore_subset_varidxes(self, varIdxes, optimize=True):
|
|||||||
return varDataMap
|
return varDataMap
|
||||||
|
|
||||||
ot.VarStore.subset_varidxes = VarStore_subset_varidxes
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user