Return three-byte operands from encodeIntCFF

This commit is contained in:
David Corbett 2023-07-06 15:45:31 -04:00 committed by Behdad Esfahbod
parent 3235454fcd
commit 4660b8c359

View File

@ -197,11 +197,14 @@ t2Operators = [
def getIntEncoder(format): def getIntEncoder(format):
if format == "cff": if format == "cff":
twoByteOp = bytechr(28)
fourByteOp = bytechr(29) fourByteOp = bytechr(29)
elif format == "t1": elif format == "t1":
twoByteOp = None
fourByteOp = bytechr(255) fourByteOp = bytechr(255)
else: else:
assert format == "t2" assert format == "t2"
twoByteOp = bytechr(28)
fourByteOp = None fourByteOp = None
def encodeInt( def encodeInt(
@ -210,6 +213,7 @@ def getIntEncoder(format):
bytechr=bytechr, bytechr=bytechr,
pack=struct.pack, pack=struct.pack,
unpack=struct.unpack, unpack=struct.unpack,
twoByteOp=twoByteOp,
): ):
if -107 <= value <= 107: if -107 <= value <= 107:
code = bytechr(value + 139) code = bytechr(value + 139)
@ -219,25 +223,23 @@ def getIntEncoder(format):
elif -1131 <= value <= -108: elif -1131 <= value <= -108:
value = -value - 108 value = -value - 108
code = bytechr((value >> 8) + 251) + bytechr(value & 0xFF) code = bytechr((value >> 8) + 251) + bytechr(value & 0xFF)
elif twoByteOp is not None and -32768 <= value <= 32767:
code = twoByteOp + pack(">h", value)
elif fourByteOp is None: elif fourByteOp is None:
# T2 only supports 2 byte ints # Backwards compatible hack: due to a previous bug in FontTools,
if -32768 <= value <= 32767: # 16.16 fixed numbers were written out as 4-byte ints. When
code = bytechr(28) + pack(">h", value) # these numbers were small, they were wrongly written back as
else: # small ints instead of 4-byte ints, breaking round-tripping.
# Backwards compatible hack: due to a previous bug in FontTools, # This here workaround doesn't do it any better, since we can't
# 16.16 fixed numbers were written out as 4-byte ints. When # distinguish anymore between small ints that were supposed to
# these numbers were small, they were wrongly written back as # be small fixed numbers and small ints that were just small
# small ints instead of 4-byte ints, breaking round-tripping. # ints. Hence the warning.
# This here workaround doesn't do it any better, since we can't log.warning(
# distinguish anymore between small ints that were supposed to "4-byte T2 number got passed to the "
# be small fixed numbers and small ints that were just small "IntType handler. This should happen only when reading in "
# ints. Hence the warning. "old XML files.\n"
log.warning( )
"4-byte T2 number got passed to the " code = bytechr(255) + pack(">l", value)
"IntType handler. This should happen only when reading in "
"old XML files.\n"
)
code = bytechr(255) + pack(">l", value)
else: else:
code = fourByteOp + pack(">l", value) code = fourByteOp + pack(">l", value)
return code return code