Merge pull request #869 from anthrotype/varLib-builder-fixes

[varLib.builder] Fix roundtripping XML
This commit is contained in:
Behdad Esfahbod 2017-03-03 11:31:38 -08:00 committed by GitHub
commit 9d5e38c439
4 changed files with 57 additions and 72 deletions

View File

@ -205,28 +205,6 @@ class VarIdxMap(BaseTable):
mapping.append((outer << 16) | inner)
class VarData(BaseTable):
def preWrite(self, font):
rawTable = self.__dict__.copy()
# TODO Remove this abstraction/optimization and move it varLib.builder?
numShorts = 0
count = len(self.VarRegionIndex)
for item in getattr(self, 'Item', []):
assert len(item) == count, ("Item length mismatch", len(item), count)
for i in range(count - 1, numShorts - 1, -1):
if not (-128 <= item[i] <= 127):
numShorts = i + 1
break
if numShorts == count:
break
rawTable['NumShorts'] = numShorts
return rawTable
class SingleSubst(FormatSwitchingBaseTable):
def populateDefaults(self, propagator=None):

View File

@ -6,7 +6,7 @@ from fontTools.ttLib.tables import otTables as ot
def buildVarRegionAxis(axisSupport):
self = ot.VarRegionAxis()
self.StartCoord, self.PeakCoord, self.EndCoord = axisSupport
self.StartCoord, self.PeakCoord, self.EndCoord = [float(v) for v in axisSupport]
return self
def buildVarRegion(support, axisTags):
@ -20,7 +20,7 @@ def buildVarRegion(support, axisTags):
def buildVarRegionList(supports, axisTags):
self = ot.VarRegionList()
self.AxisCount = len(axisTags)
self.RegionAxisCount = len(axisTags)
self.Region = []
for support in supports:
self.Region.append(buildVarRegion(support, axisTags))
@ -39,8 +39,7 @@ def _reorderItem(lst, narrows):
out.append(lst[i])
return out
def optimizeVarData(self):
# Reorder columns such that all SHORT columns come before UINT8
def calculateNumShorts(self, optimize=True):
count = self.VarRegionCount
items = self.Item
narrows = set(range(count))
@ -51,11 +50,15 @@ def optimizeVarData(self):
break
if not narrows:
break
self.VarRegionIndex = _reorderItem(self.VarRegionIndex, narrows)
for i in range(self.ItemCount):
items[i] = _reorderItem(items[i], narrows)
if optimize:
# Reorder columns such that all SHORT columns come before UINT8
self.VarRegionIndex = _reorderItem(self.VarRegionIndex, narrows)
for i in range(self.ItemCount):
items[i] = _reorderItem(items[i], narrows)
self.NumShorts = count - len(narrows)
else:
wides = set(range(count)) - narrows
self.NumShorts = 1+max(wides) if wides else 0
return self
def buildVarData(varRegionIndices, items, optimize=True):
@ -68,8 +71,7 @@ def buildVarData(varRegionIndices, items, optimize=True):
assert len(item) == regionCount
records.append(list(item))
self.ItemCount = len(self.Item)
if items and optimize:
optimizeVarData(self)
calculateNumShorts(self, optimize=optimize)
return self
@ -119,8 +121,7 @@ class OnlineVarStoreBuilder(object):
self._store.VarDataCount = len(self._store.VarData)
for data in self._store.VarData:
data.ItemCount = len(data.Item)
if optimize:
optimizeVarData(data)
calculateNumShorts(data, optimize)
return self._store
def storeMasters(self, master_values):

View File

@ -6,15 +6,16 @@
<VarStore Format="1">
<Format value="1"/>
<VarRegionList>
<!-- RegionAxisCount=2 -->
<!-- RegionCount=5 -->
<Region index="0">
<VarRegionAxis index="0">
<StartCoord value="0"/>
<PeakCoord value="0"/>
<EndCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="0.0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
@ -23,46 +24,46 @@
<VarRegionAxis index="0">
<StartCoord value="-1.0"/>
<PeakCoord value="-1.0"/>
<EndCoord value="0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<PeakCoord value="0"/>
<EndCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="0.0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
</Region>
<Region index="2">
<VarRegionAxis index="0">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<PeakCoord value="0"/>
<EndCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="0.0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
</Region>
<Region index="3">
<VarRegionAxis index="0">
<StartCoord value="-1.0"/>
<PeakCoord value="-1.0"/>
<EndCoord value="0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
</Region>
<Region index="4">
<VarRegionAxis index="0">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
@ -71,7 +72,7 @@
<!-- VarDataCount=1 -->
<VarData index="0">
<!-- ItemCount=0 -->
<NumShorts value="None"/>
<NumShorts value="0"/>
<!-- VarRegionCount=5 -->
<VarRegionIndex index="0" value="0"/>
<VarRegionIndex index="1" value="1"/>
@ -87,15 +88,16 @@
<VarStore Format="1">
<Format value="1"/>
<VarRegionList>
<!-- RegionAxisCount=2 -->
<!-- RegionCount=5 -->
<Region index="0">
<VarRegionAxis index="0">
<StartCoord value="0"/>
<PeakCoord value="0"/>
<EndCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="0.0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
@ -104,46 +106,46 @@
<VarRegionAxis index="0">
<StartCoord value="-1.0"/>
<PeakCoord value="-1.0"/>
<EndCoord value="0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<PeakCoord value="0"/>
<EndCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="0.0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
</Region>
<Region index="2">
<VarRegionAxis index="0">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<PeakCoord value="0"/>
<EndCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="0.0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
</Region>
<Region index="3">
<VarRegionAxis index="0">
<StartCoord value="-1.0"/>
<PeakCoord value="-1.0"/>
<EndCoord value="0"/>
<EndCoord value="0.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
</Region>
<Region index="4">
<VarRegionAxis index="0">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
<VarRegionAxis index="1">
<StartCoord value="0"/>
<StartCoord value="0.0"/>
<PeakCoord value="1.0"/>
<EndCoord value="1.0"/>
</VarRegionAxis>
@ -152,7 +154,7 @@
<!-- VarDataCount=1 -->
<VarData index="0">
<!-- ItemCount=6 -->
<NumShorts value="None"/>
<NumShorts value="0"/>
<!-- VarRegionCount=5 -->
<VarRegionIndex index="0" value="0"/>
<VarRegionIndex index="1" value="1"/>

View File

@ -97,9 +97,13 @@ class BuildTest(unittest.TestCase):
varfont, model, _ = build(ds_path, finder)
tables = ['GDEF', 'HVAR', 'fvar', 'gvar']
#varfont.save(ds_path.replace('.designspace', suffix))
#varfont.saveXML(ds_path.replace('.designspace', '.ttx'), tables=tables)
self.expect_ttx(varfont, self.get_test_input('BuildTest.ttx'), tables)
expected_ttx = self.get_test_input('BuildTest.ttx')
self.expect_ttx(varfont, expected_ttx, tables)
# ensure the TTX dump is the same after saving and reloading font
varfont_path = os.path.join(self.tempdir, 'BuildTest.ttf')
varfont.save(varfont_path)
self.expect_ttx(TTFont(varfont_path), expected_ttx, tables)
if __name__ == "__main__":