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