[GlyphCoordinates] Store int data as 4byte, not 2byte

The memory increase is negligible, but means that we don't have to
worry about integer overflows anymore.
This commit is contained in:
Behdad Esfahbod 2021-04-06 18:00:55 -06:00
parent 8be2f49b84
commit e9acd9634f
2 changed files with 5 additions and 8 deletions

View File

@ -1432,7 +1432,8 @@ class GlyphComponent(object):
class GlyphCoordinates(object):
def __init__(self, iterable=[], typecode="h"):
def __init__(self, iterable=[], typecode='i'):
if typecode == 'h': typecode = 'i'
self._a = array.array(typecode)
self.extend(iterable)
@ -1451,9 +1452,6 @@ class GlyphCoordinates(object):
def _checkFloat(self, p):
if self.isFloat():
return p
if any(v > 0x7FFF or v < -0x8000 for v in p):
self._ensureFloat()
return p
if any(isinstance(v, float) for v in p):
p = [int(v) if int(v) == v else v for v in p]
if any(isinstance(v, float) for v in p):
@ -1508,7 +1506,7 @@ class GlyphCoordinates(object):
def toInt(self, *, round=otRound):
if not self.isFloat():
return
a = array.array("h")
a = array.array("i")
for n in self._a:
a.append(round(n))
self._a = a

View File

@ -173,10 +173,9 @@ class GlyphCoordinatesTest(object):
assert g[0][0] == otRound(afloat)
def test__checkFloat_overflow(self):
g = GlyphCoordinates([(1, 1)], typecode="h")
g = GlyphCoordinates([(1, 1)])
g.append((0x8000, 0))
assert g.array.typecode == "d"
assert g.array == array.array("d", [1.0, 1.0, 32768.0, 0.0])
assert list(g.array) == [1.0, 1.0, 32768.0, 0.0]
CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))