otlLib: return namedtuple from _makeDeviceTuple

makes code slightly more readable.
This commit is contained in:
Cosimo Lupo 2019-06-18 16:00:39 +01:00
parent 8c90878fcf
commit 7f82d11156
No known key found for this signature in database
GPG Key ID: 20D4A261E4A0E642

View File

@ -1,4 +1,5 @@
from __future__ import print_function, division, absolute_import from __future__ import print_function, division, absolute_import
from collections import namedtuple
from fontTools import ttLib from fontTools import ttLib
from fontTools.ttLib.tables import otTables as ot from fontTools.ttLib.tables import otTables as ot
from fontTools.ttLib.tables.otBase import ValueRecord, valueRecordFormatDict from fontTools.ttLib.tables.otBase import ValueRecord, valueRecordFormatDict
@ -479,10 +480,7 @@ def _getSinglePosValueKey(valueRecord):
valueFormat, result = 0, [] valueFormat, result = 0, []
for name, value in valueRecord.__dict__.items(): for name, value in valueRecord.__dict__.items():
if isinstance(value, ot.Device): if isinstance(value, ot.Device):
if value.DeltaFormat & 0x8000: result.append((name, _makeDeviceTuple(value)))
result.append((name, _makeVariationInstance(value)))
else:
result.append((name, _makeDeviceTuple(value)))
else: else:
result.append((name, value)) result.append((name, value))
valueFormat |= valueRecordFormatDict[name][0] valueFormat |= valueRecordFormatDict[name][0]
@ -491,25 +489,25 @@ def _getSinglePosValueKey(valueRecord):
return tuple(result) return tuple(result)
_DeviceTuple = namedtuple("_DeviceTuple", "DeltaFormat StartSize EndSize DeltaValue")
def _makeDeviceTuple(device): def _makeDeviceTuple(device):
"""otTables.Device --> tuple, for making device tables unique""" """otTables.Device --> tuple, for making device tables unique"""
return (device.DeltaFormat, device.StartSize, device.EndSize, return _DeviceTuple(
tuple(device.DeltaValue)) device.DeltaFormat,
device.StartSize,
device.EndSize,
() if device.DeltaFormat & 0x8000 else tuple(device.DeltaValue)
)
def _makeVariationInstance(device):
"""otTables.Device --> tuple, for making device tables unique"""
return (device.DeltaFormat, device.StartSize, device.EndSize)
def _getSinglePosValueSize(valueKey): def _getSinglePosValueSize(valueKey):
"""Returns how many ushorts this valueKey (short form of ValueRecord) takes up""" """Returns how many ushorts this valueKey (short form of ValueRecord) takes up"""
count = 0 count = 0
for k in valueKey[1:]: for _, v in valueKey[1:]:
if hasattr(k[1], '__len__') and len(k[1]): if isinstance(v, _DeviceTuple):
# it is a device table key count += len(v.DeltaValue) + 3
if k[1][0] & 0x8000:
count += 3
else:
count += len(k[1][3]) + 3
else: else:
count += 1 count += 1
return count return count