From 36dd271cd5d22692226bce23081a3b26a27323e1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Apr 2021 15:58:45 -0600 Subject: [PATCH] [otBase/otConverters] Add array-writers for int types --- Lib/fontTools/ttLib/tables/otBase.py | 12 ++++++++++++ Lib/fontTools/ttLib/tables/otConverters.py | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Lib/fontTools/ttLib/tables/otBase.py b/Lib/fontTools/ttLib/tables/otBase.py index 31ff2d75f..facef94fe 100644 --- a/Lib/fontTools/ttLib/tables/otBase.py +++ b/Lib/fontTools/ttLib/tables/otBase.py @@ -428,22 +428,34 @@ class OTTableWriter(object): def writeValue(self, typecode, value): self.items.append(struct.pack(f">{typecode}", value)) + def writeArray(self, typecode, values): + a = array.array(typecode, values) + if sys.byteorder != "big": a.byteswap() + self.items.append(a.tobytes()) def writeUShort(self, value): assert 0 <= value < 0x10000, value self.items.append(struct.pack(">H", value)) + def writeUShortArray(self, values): + self.writeArray('H', values) def writeShort(self, value): assert -32768 <= value < 32768, value self.items.append(struct.pack(">h", value)) + def writeShortArray(self, values): + self.writeArray('h', values) def writeUInt8(self, value): assert 0 <= value < 256, value self.items.append(struct.pack(">B", value)) + def writeUInt8Array(self, values): + self.writeArray('B', values) def writeInt8(self, value): assert -128 <= value < 128, value self.items.append(struct.pack(">b", value)) + def writeInt8Array(self, values): + self.writeArray('b', values) def writeUInt24(self, value): assert 0 <= value < 0x1000000, value diff --git a/Lib/fontTools/ttLib/tables/otConverters.py b/Lib/fontTools/ttLib/tables/otConverters.py index c443f1aa0..ab2037f00 100644 --- a/Lib/fontTools/ttLib/tables/otConverters.py +++ b/Lib/fontTools/ttLib/tables/otConverters.py @@ -234,6 +234,8 @@ class Long(IntValue): return reader.readLongArray(count) def write(self, writer, font, tableDict, value, repeatIndex=None): writer.writeLong(value) + def writeArray(self, writer, font, tableDict, values): + writer.writeLongArray(values) class ULong(IntValue): staticSize = 4 @@ -243,6 +245,8 @@ class ULong(IntValue): return reader.readULongArray(count) def write(self, writer, font, tableDict, value, repeatIndex=None): writer.writeULong(value) + def writeArray(self, writer, font, tableDict, values): + writer.writeULongArray(values) class Flags32(ULong): @staticmethod @@ -257,6 +261,8 @@ class Short(IntValue): return reader.readShortArray(count) def write(self, writer, font, tableDict, value, repeatIndex=None): writer.writeShort(value) + def writeArray(self, writer, font, tableDict, values): + writer.writeShortArray(values) class UShort(IntValue): staticSize = 2 @@ -266,6 +272,8 @@ class UShort(IntValue): return reader.readUShortArray(count) def write(self, writer, font, tableDict, value, repeatIndex=None): writer.writeUShort(value) + def writeArray(self, writer, font, tableDict, values): + writer.writeUShortArray(values) class Int8(IntValue): staticSize = 1 @@ -275,6 +283,8 @@ class Int8(IntValue): return reader.readInt8Array(count) def write(self, writer, font, tableDict, value, repeatIndex=None): writer.writeInt8(value) + def writeArray(self, writer, font, tableDict, values): + writer.writeInt8Array(values) class UInt8(IntValue): staticSize = 1 @@ -284,6 +294,8 @@ class UInt8(IntValue): return reader.readUInt8Array(count) def write(self, writer, font, tableDict, value, repeatIndex=None): writer.writeUInt8(value) + def writeArray(self, writer, font, tableDict, values): + writer.writeUInt8Array(values) class UInt24(IntValue): staticSize = 3