2015-04-26 02:01:01 -04:00
|
|
|
"""psCharStrings.py -- module implementing various kinds of CharStrings:
|
1999-12-16 21:34:53 +00:00
|
|
|
CFF dictionary data and Type1/Type2 CharStrings.
|
|
|
|
"""
|
|
|
|
|
2019-10-07 11:46:16 +01:00
|
|
|
from fontTools.misc.fixedTools import (
|
|
|
|
fixedToFloat,
|
|
|
|
floatToFixed,
|
|
|
|
floatToFixedToStr,
|
|
|
|
strToFixedToFloat,
|
|
|
|
)
|
2021-08-20 00:45:43 +02:00
|
|
|
from fontTools.misc.textTools import bytechr, byteord, bytesjoin, strjoin
|
2017-05-18 15:01:05 +09:00
|
|
|
from fontTools.pens.boundsPen import BoundsPen
|
1999-12-16 21:34:53 +00:00
|
|
|
import struct
|
2016-01-24 14:51:24 +00:00
|
|
|
import logging
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
|
2016-01-24 14:51:24 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2002-05-17 07:07:26 +00:00
|
|
|
|
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
def read_operator(self, b0, data, index):
|
|
|
|
if b0 == 12:
|
|
|
|
op = (b0, byteord(data[index]))
|
|
|
|
index = index + 1
|
|
|
|
else:
|
|
|
|
op = b0
|
2017-01-12 15:23:12 -08:00
|
|
|
try:
|
|
|
|
operator = self.operators[op]
|
|
|
|
except KeyError:
|
|
|
|
return None, index
|
2014-06-16 19:39:23 -04:00
|
|
|
value = self.handle_operator(operator)
|
|
|
|
return value, index
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
|
|
|
|
def read_byte(self, b0, data, index):
|
|
|
|
return b0 - 139, index
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
|
|
|
|
def read_smallInt1(self, b0, data, index):
|
|
|
|
b1 = byteord(data[index])
|
|
|
|
return (b0 - 247) * 256 + b1 + 108, index + 1
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
|
|
|
|
def read_smallInt2(self, b0, data, index):
|
|
|
|
b1 = byteord(data[index])
|
|
|
|
return -(b0 - 251) * 256 - b1 - 108, index + 1
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
|
|
|
|
def read_shortInt(self, b0, data, index):
|
|
|
|
(value,) = struct.unpack(">h", data[index : index + 2])
|
|
|
|
return value, index + 2
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
|
|
|
|
def read_longInt(self, b0, data, index):
|
|
|
|
(value,) = struct.unpack(">l", data[index : index + 4])
|
|
|
|
return value, index + 4
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
|
|
|
|
def read_fixed1616(self, b0, data, index):
|
|
|
|
(value,) = struct.unpack(">l", data[index : index + 4])
|
2016-02-01 10:52:33 +00:00
|
|
|
return fixedToFloat(value, precisionBits=16), index + 4
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
|
|
|
|
def read_reserved(self, b0, data, index):
|
|
|
|
assert NotImplementedError
|
|
|
|
return NotImplemented, index
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
|
|
|
|
def read_realNumber(self, b0, data, index):
|
|
|
|
number = ""
|
|
|
|
while True:
|
|
|
|
b = byteord(data[index])
|
|
|
|
index = index + 1
|
|
|
|
nibble0 = (b & 0xF0) >> 4
|
|
|
|
nibble1 = b & 0x0F
|
|
|
|
if nibble0 == 0xF:
|
|
|
|
break
|
|
|
|
number = number + realNibbles[nibble0]
|
|
|
|
if nibble1 == 0xF:
|
|
|
|
break
|
|
|
|
number = number + realNibbles[nibble1]
|
|
|
|
return float(number), index
|
|
|
|
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
t1OperandEncoding = [None] * 256
|
2014-06-16 19:39:23 -04:00
|
|
|
t1OperandEncoding[0:32] = (32) * [read_operator]
|
|
|
|
t1OperandEncoding[32:247] = (247 - 32) * [read_byte]
|
|
|
|
t1OperandEncoding[247:251] = (251 - 247) * [read_smallInt1]
|
|
|
|
t1OperandEncoding[251:255] = (255 - 251) * [read_smallInt2]
|
|
|
|
t1OperandEncoding[255] = read_longInt
|
1999-12-16 21:34:53 +00:00
|
|
|
assert len(t1OperandEncoding) == 256
|
|
|
|
|
|
|
|
t2OperandEncoding = t1OperandEncoding[:]
|
2014-06-16 19:39:23 -04:00
|
|
|
t2OperandEncoding[28] = read_shortInt
|
|
|
|
t2OperandEncoding[255] = read_fixed1616
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
cffDictOperandEncoding = t2OperandEncoding[:]
|
2014-06-16 19:39:23 -04:00
|
|
|
cffDictOperandEncoding[29] = read_longInt
|
|
|
|
cffDictOperandEncoding[30] = read_realNumber
|
|
|
|
cffDictOperandEncoding[255] = read_reserved
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
|
2015-04-26 02:01:01 -04:00
|
|
|
realNibbles = [
|
|
|
|
"0",
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
"3",
|
|
|
|
"4",
|
1999-12-16 21:34:53 +00:00
|
|
|
"5",
|
|
|
|
"6",
|
2022-12-13 11:26:36 +00:00
|
|
|
"7",
|
|
|
|
"8",
|
|
|
|
"9",
|
|
|
|
".",
|
|
|
|
"E",
|
1999-12-16 21:34:53 +00:00
|
|
|
"E-",
|
|
|
|
None,
|
2022-12-13 11:26:36 +00:00
|
|
|
"-",
|
1999-12-16 21:34:53 +00:00
|
|
|
]
|
2015-06-11 17:05:15 -07:00
|
|
|
realNibblesDict = {v: i for i, v in enumerate(realNibbles)}
|
1999-12-16 21:34:53 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
maxOpStack = 193
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
|
2002-05-13 16:19:51 +00:00
|
|
|
def buildOperatorDict(operatorList):
|
2002-05-17 07:07:26 +00:00
|
|
|
oper = {}
|
|
|
|
opc = {}
|
1999-12-16 21:34:53 +00:00
|
|
|
for item in operatorList:
|
|
|
|
if len(item) == 2:
|
2002-05-17 07:07:26 +00:00
|
|
|
oper[item[0]] = item[1]
|
|
|
|
else:
|
|
|
|
oper[item[0]] = item[1:]
|
2013-11-27 04:48:20 -05:00
|
|
|
if isinstance(item[0], tuple):
|
2002-05-17 07:07:26 +00:00
|
|
|
opc[item[1]] = item[0]
|
1999-12-16 21:34:53 +00:00
|
|
|
else:
|
2002-05-17 07:07:26 +00:00
|
|
|
opc[item[1]] = (item[0],)
|
|
|
|
return oper, opc
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
t2Operators = [
|
2015-04-26 00:54:30 -04:00
|
|
|
# opcode name
|
|
|
|
(1, "hstem"),
|
|
|
|
(3, "vstem"),
|
|
|
|
(4, "vmoveto"),
|
|
|
|
(5, "rlineto"),
|
|
|
|
(6, "hlineto"),
|
|
|
|
(7, "vlineto"),
|
|
|
|
(8, "rrcurveto"),
|
|
|
|
(10, "callsubr"),
|
|
|
|
(11, "return"),
|
|
|
|
(14, "endchar"),
|
2017-05-09 11:25:28 -07:00
|
|
|
(15, "vsindex"),
|
2015-04-26 00:54:30 -04:00
|
|
|
(16, "blend"),
|
|
|
|
(18, "hstemhm"),
|
|
|
|
(19, "hintmask"),
|
|
|
|
(20, "cntrmask"),
|
|
|
|
(21, "rmoveto"),
|
|
|
|
(22, "hmoveto"),
|
|
|
|
(23, "vstemhm"),
|
|
|
|
(24, "rcurveline"),
|
|
|
|
(25, "rlinecurve"),
|
|
|
|
(26, "vvcurveto"),
|
|
|
|
(27, "hhcurveto"),
|
|
|
|
# (28, 'shortint'), # not really an operator
|
|
|
|
(29, "callgsubr"),
|
|
|
|
(30, "vhcurveto"),
|
|
|
|
(31, "hvcurveto"),
|
2015-08-09 00:33:50 -07:00
|
|
|
((12, 0), "ignore"), # dotsection. Yes, there a few very early OTF/CFF
|
|
|
|
# fonts with this deprecated operator. Just ignore it.
|
2015-04-26 00:54:30 -04:00
|
|
|
((12, 3), "and"),
|
|
|
|
((12, 4), "or"),
|
|
|
|
((12, 5), "not"),
|
|
|
|
((12, 8), "store"),
|
|
|
|
((12, 9), "abs"),
|
|
|
|
((12, 10), "add"),
|
|
|
|
((12, 11), "sub"),
|
|
|
|
((12, 12), "div"),
|
|
|
|
((12, 13), "load"),
|
|
|
|
((12, 14), "neg"),
|
|
|
|
((12, 15), "eq"),
|
|
|
|
((12, 18), "drop"),
|
|
|
|
((12, 20), "put"),
|
|
|
|
((12, 21), "get"),
|
|
|
|
((12, 22), "ifelse"),
|
|
|
|
((12, 23), "random"),
|
|
|
|
((12, 24), "mul"),
|
|
|
|
((12, 26), "sqrt"),
|
|
|
|
((12, 27), "dup"),
|
|
|
|
((12, 28), "exch"),
|
|
|
|
((12, 29), "index"),
|
|
|
|
((12, 30), "roll"),
|
|
|
|
((12, 34), "hflex"),
|
|
|
|
((12, 35), "flex"),
|
|
|
|
((12, 36), "hflex1"),
|
|
|
|
((12, 37), "flex1"),
|
1999-12-16 21:34:53 +00:00
|
|
|
]
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2002-05-23 21:50:36 +00:00
|
|
|
def getIntEncoder(format):
|
|
|
|
if format == "cff":
|
2023-07-06 15:45:31 -04:00
|
|
|
twoByteOp = bytechr(28)
|
2013-11-27 15:19:40 -05:00
|
|
|
fourByteOp = bytechr(29)
|
2002-05-23 21:50:36 +00:00
|
|
|
elif format == "t1":
|
2023-07-06 15:45:31 -04:00
|
|
|
twoByteOp = None
|
2013-11-27 15:19:40 -05:00
|
|
|
fourByteOp = bytechr(255)
|
2002-05-23 21:50:36 +00:00
|
|
|
else:
|
|
|
|
assert format == "t2"
|
2023-07-06 15:45:31 -04:00
|
|
|
twoByteOp = bytechr(28)
|
2005-02-23 21:22:27 +00:00
|
|
|
fourByteOp = None
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-11-27 15:19:40 -05:00
|
|
|
def encodeInt(
|
|
|
|
value,
|
|
|
|
fourByteOp=fourByteOp,
|
|
|
|
bytechr=bytechr,
|
2005-02-23 21:22:27 +00:00
|
|
|
pack=struct.pack,
|
|
|
|
unpack=struct.unpack,
|
2023-07-06 15:45:31 -04:00
|
|
|
twoByteOp=twoByteOp,
|
2005-02-23 21:22:27 +00:00
|
|
|
):
|
2002-05-23 21:50:36 +00:00
|
|
|
if -107 <= value <= 107:
|
2013-11-27 15:19:40 -05:00
|
|
|
code = bytechr(value + 139)
|
2002-05-23 21:50:36 +00:00
|
|
|
elif 108 <= value <= 1131:
|
|
|
|
value = value - 108
|
2013-11-27 15:19:40 -05:00
|
|
|
code = bytechr((value >> 8) + 247) + bytechr(value & 0xFF)
|
2002-05-23 21:50:36 +00:00
|
|
|
elif -1131 <= value <= -108:
|
|
|
|
value = -value - 108
|
2013-11-27 15:19:40 -05:00
|
|
|
code = bytechr((value >> 8) + 251) + bytechr(value & 0xFF)
|
2023-07-06 15:45:31 -04:00
|
|
|
elif twoByteOp is not None and -32768 <= value <= 32767:
|
|
|
|
code = twoByteOp + pack(">h", value)
|
2005-02-23 21:22:27 +00:00
|
|
|
elif fourByteOp is None:
|
2023-07-06 15:45:31 -04:00
|
|
|
# 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)
|
2002-05-23 21:50:36 +00:00
|
|
|
else:
|
|
|
|
code = fourByteOp + pack(">l", value)
|
|
|
|
return code
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2002-05-23 21:50:36 +00:00
|
|
|
return encodeInt
|
|
|
|
|
|
|
|
|
|
|
|
encodeIntCFF = getIntEncoder("cff")
|
|
|
|
encodeIntT1 = getIntEncoder("t1")
|
|
|
|
encodeIntT2 = getIntEncoder("t2")
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2005-02-23 21:22:27 +00:00
|
|
|
def encodeFixed(f, pack=struct.pack):
|
2018-06-27 13:04:56 -07:00
|
|
|
"""For T2 only"""
|
2019-10-07 11:46:16 +01:00
|
|
|
value = floatToFixed(f, precisionBits=16)
|
2018-06-27 13:04:56 -07:00
|
|
|
if value & 0xFFFF == 0: # check if the fractional part is zero
|
|
|
|
return encodeIntT2(value >> 16) # encode only the integer part
|
|
|
|
else:
|
|
|
|
return b"\xff" + pack(">l", value) # encode the entire fixed point value
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2005-02-23 21:22:27 +00:00
|
|
|
|
2019-01-04 15:48:14 -08:00
|
|
|
realZeroBytes = bytechr(30) + bytechr(0xF)
|
|
|
|
|
|
|
|
|
2002-05-23 21:50:36 +00:00
|
|
|
def encodeFloat(f):
|
2005-02-23 21:22:27 +00:00
|
|
|
# For CFF only, used in cffLib
|
2019-01-04 15:48:14 -08:00
|
|
|
if f == 0.0: # 0.0 == +0.0 == -0.0
|
|
|
|
return realZeroBytes
|
2019-01-05 11:06:59 -08:00
|
|
|
# Note: 14 decimal digits seems to be the limitation for CFF real numbers
|
2019-01-05 11:18:45 -08:00
|
|
|
# in macOS. However, we use 8 here to match the implementation of AFDKO.
|
2019-01-05 11:06:59 -08:00
|
|
|
s = "%.8G" % f
|
2002-05-23 21:50:36 +00:00
|
|
|
if s[:2] == "0.":
|
|
|
|
s = s[1:]
|
|
|
|
elif s[:3] == "-0.":
|
|
|
|
s = "-" + s[2:]
|
2024-05-08 16:43:20 -04:00
|
|
|
elif s.endswith("000"):
|
|
|
|
significantDigits = s.rstrip("0")
|
|
|
|
s = "%sE%d" % (significantDigits, len(s) - len(significantDigits))
|
|
|
|
else:
|
|
|
|
dotIndex = s.find(".")
|
|
|
|
eIndex = s.find("E")
|
|
|
|
if dotIndex != -1 and eIndex != -1:
|
|
|
|
integerPart = s[:dotIndex]
|
|
|
|
fractionalPart = s[dotIndex + 1 : eIndex]
|
|
|
|
exponent = int(s[eIndex + 1 :])
|
|
|
|
newExponent = exponent - len(fractionalPart)
|
|
|
|
if newExponent == 1:
|
|
|
|
s = "%s%s0" % (integerPart, fractionalPart)
|
|
|
|
else:
|
|
|
|
s = "%s%sE%d" % (integerPart, fractionalPart, newExponent)
|
|
|
|
if s.startswith((".0", "-.0")):
|
|
|
|
sign, s = s.split(".", 1)
|
|
|
|
s = "%s%sE-%d" % (sign, s.lstrip("0"), len(s))
|
2002-05-23 21:50:36 +00:00
|
|
|
nibbles = []
|
|
|
|
while s:
|
|
|
|
c = s[0]
|
|
|
|
s = s[1:]
|
2019-01-04 15:48:14 -08:00
|
|
|
if c == "E":
|
|
|
|
c2 = s[:1]
|
|
|
|
if c2 == "-":
|
2019-01-04 15:58:59 -08:00
|
|
|
s = s[1:]
|
2019-01-04 15:48:14 -08:00
|
|
|
c = "E-"
|
|
|
|
elif c2 == "+":
|
|
|
|
s = s[1:]
|
2024-05-08 16:43:20 -04:00
|
|
|
if s.startswith("0"):
|
|
|
|
s = s[1:]
|
2002-05-23 21:50:36 +00:00
|
|
|
nibbles.append(realNibblesDict[c])
|
|
|
|
nibbles.append(0xF)
|
|
|
|
if len(nibbles) % 2:
|
|
|
|
nibbles.append(0xF)
|
2013-11-27 15:19:40 -05:00
|
|
|
d = bytechr(30)
|
2002-05-23 21:50:36 +00:00
|
|
|
for i in range(0, len(nibbles), 2):
|
2013-11-27 15:19:40 -05:00
|
|
|
d = d + bytechr(nibbles[i] << 4 | nibbles[i + 1])
|
2002-05-23 21:50:36 +00:00
|
|
|
return d
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
|
2002-05-24 09:58:04 +00:00
|
|
|
class CharStringCompileError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-11-28 14:26:58 -05:00
|
|
|
class SimpleT2Decompiler(object):
|
2022-08-26 20:33:03 -06:00
|
|
|
def __init__(self, localSubrs, globalSubrs, private=None, blender=None):
|
1999-12-16 21:34:53 +00:00
|
|
|
self.localSubrs = localSubrs
|
|
|
|
self.localBias = calcSubrBias(localSubrs)
|
|
|
|
self.globalSubrs = globalSubrs
|
|
|
|
self.globalBias = calcSubrBias(globalSubrs)
|
2017-05-09 11:26:53 -07:00
|
|
|
self.private = private
|
2022-08-26 20:33:03 -06:00
|
|
|
self.blender = blender
|
1999-12-16 21:34:53 +00:00
|
|
|
self.reset()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def reset(self):
|
|
|
|
self.callingStack = []
|
|
|
|
self.operandStack = []
|
|
|
|
self.hintCount = 0
|
|
|
|
self.hintMaskBytes = 0
|
2017-05-09 11:26:53 -07:00
|
|
|
self.numRegions = 0
|
2022-08-26 21:23:04 -06:00
|
|
|
self.vsIndex = 0
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def execute(self, charString):
|
|
|
|
self.callingStack.append(charString)
|
|
|
|
needsDecompilation = charString.needsDecompilation()
|
|
|
|
if needsDecompilation:
|
|
|
|
program = []
|
|
|
|
pushToProgram = program.append
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
1999-12-16 21:34:53 +00:00
|
|
|
pushToProgram = lambda x: None
|
|
|
|
pushToStack = self.operandStack.append
|
|
|
|
index = 0
|
2013-11-27 04:15:34 -05:00
|
|
|
while True:
|
1999-12-16 21:34:53 +00:00
|
|
|
token, isOperator, index = charString.getToken(index)
|
|
|
|
if token is None:
|
|
|
|
break # we're done!
|
|
|
|
pushToProgram(token)
|
|
|
|
if isOperator:
|
|
|
|
handlerName = "op_" + token
|
2014-06-16 20:35:12 -04:00
|
|
|
handler = getattr(self, handlerName, None)
|
|
|
|
if handler is not None:
|
1999-12-16 21:34:53 +00:00
|
|
|
rv = handler(index)
|
2022-12-13 11:26:36 +00:00
|
|
|
if rv:
|
1999-12-16 21:34:53 +00:00
|
|
|
hintMaskBytes, index = rv
|
|
|
|
pushToProgram(hintMaskBytes)
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
1999-12-16 21:34:53 +00:00
|
|
|
self.popall()
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
1999-12-16 21:34:53 +00:00
|
|
|
pushToStack(token)
|
|
|
|
if needsDecompilation:
|
|
|
|
charString.setProgram(program)
|
|
|
|
del self.callingStack[-1]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def pop(self):
|
|
|
|
value = self.operandStack[-1]
|
|
|
|
del self.operandStack[-1]
|
|
|
|
return value
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def popall(self):
|
|
|
|
stack = self.operandStack[:]
|
|
|
|
self.operandStack[:] = []
|
|
|
|
return stack
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def push(self, value):
|
|
|
|
self.operandStack.append(value)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_return(self, index):
|
|
|
|
if self.operandStack:
|
2022-12-13 11:26:36 +00:00
|
|
|
pass
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_endchar(self, index):
|
2022-12-13 11:26:36 +00:00
|
|
|
pass
|
|
|
|
|
2006-02-25 21:39:33 +00:00
|
|
|
def op_ignore(self, index):
|
2022-12-13 11:26:36 +00:00
|
|
|
pass
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_callsubr(self, index):
|
|
|
|
subrIndex = self.pop()
|
|
|
|
subr = self.localSubrs[subrIndex + self.localBias]
|
|
|
|
self.execute(subr)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_callgsubr(self, index):
|
|
|
|
subrIndex = self.pop()
|
|
|
|
subr = self.globalSubrs[subrIndex + self.globalBias]
|
|
|
|
self.execute(subr)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hstem(self, index):
|
|
|
|
self.countHints()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2002-05-17 18:37:07 +00:00
|
|
|
def op_vstem(self, index):
|
1999-12-16 21:34:53 +00:00
|
|
|
self.countHints()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hstemhm(self, index):
|
|
|
|
self.countHints()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2002-05-17 18:37:07 +00:00
|
|
|
def op_vstemhm(self, index):
|
1999-12-16 21:34:53 +00:00
|
|
|
self.countHints()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hintmask(self, index):
|
|
|
|
if not self.hintMaskBytes:
|
|
|
|
self.countHints()
|
2013-11-27 17:46:17 -05:00
|
|
|
self.hintMaskBytes = (self.hintCount + 7) // 8
|
1999-12-16 21:34:53 +00:00
|
|
|
hintMaskBytes, index = self.callingStack[-1].getBytes(index, self.hintMaskBytes)
|
|
|
|
return hintMaskBytes, index
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
op_cntrmask = op_hintmask
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def countHints(self):
|
|
|
|
args = self.popall()
|
2013-11-27 17:46:17 -05:00
|
|
|
self.hintCount = self.hintCount + len(args) // 2
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
# misc
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_and(self, index):
|
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_or(self, index):
|
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_not(self, index):
|
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_store(self, index):
|
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_abs(self, index):
|
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_add(self, index):
|
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_sub(self, index):
|
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_div(self, index):
|
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_load(self, index):
|
|
|
|
raise NotImplementedError
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_neg(self, index):
|
2013-08-14 17:48:03 -04:00
|
|
|
raise NotImplementedError
|
2017-01-12 15:23:12 -08:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_eq(self, index):
|
|
|
|
raise NotImplementedError
|
2017-01-12 15:23:12 -08:00
|
|
|
|
|
|
|
def op_drop(self, index):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_put(self, index):
|
|
|
|
raise NotImplementedError
|
2017-01-12 15:23:12 -08:00
|
|
|
|
2017-01-26 11:55:13 +00:00
|
|
|
def op_get(self, index):
|
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-08-14 17:48:03 -04:00
|
|
|
def op_ifelse(self, index):
|
2017-01-26 11:55:13 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2022-08-26 20:33:03 -06:00
|
|
|
def op_random(self, index):
|
|
|
|
raise NotImplementedError
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_mul(self, index):
|
|
|
|
raise NotImplementedError
|
2017-01-26 11:55:13 +00:00
|
|
|
|
|
|
|
def op_sqrt(self, index):
|
2018-12-11 17:07:51 -08:00
|
|
|
raise NotImplementedError
|
2017-01-26 11:55:13 +00:00
|
|
|
|
|
|
|
def op_dup(self, index):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def op_exch(self, index):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def op_index(self, index):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def op_roll(self, index):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def op_blend(self, index):
|
|
|
|
if self.numRegions == 0:
|
|
|
|
self.numRegions = self.private.getNumRegions()
|
|
|
|
numBlends = self.pop()
|
2017-03-22 17:53:45 -07:00
|
|
|
numOps = numBlends * (self.numRegions + 1)
|
2022-08-26 20:33:03 -06:00
|
|
|
if self.blender is None:
|
|
|
|
del self.operandStack[
|
|
|
|
-(numOps - numBlends) :
|
|
|
|
] # Leave the default operands on the stack.
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
2022-08-26 20:33:03 -06:00
|
|
|
argi = len(self.operandStack) - numOps
|
|
|
|
end_args = tuplei = argi + numBlends
|
|
|
|
while argi < end_args:
|
|
|
|
next_ti = tuplei + self.numRegions
|
|
|
|
deltas = self.operandStack[tuplei:next_ti]
|
2022-08-26 21:23:04 -06:00
|
|
|
delta = self.blender(self.vsIndex, deltas)
|
2022-08-27 12:20:15 -06:00
|
|
|
self.operandStack[argi] += delta
|
2022-08-26 20:33:03 -06:00
|
|
|
tuplei = next_ti
|
|
|
|
argi += 1
|
|
|
|
self.operandStack[end_args:] = []
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def op_vsindex(self, index):
|
|
|
|
vi = self.pop()
|
2022-08-26 21:23:04 -06:00
|
|
|
self.vsIndex = vi
|
2017-01-12 15:23:12 -08:00
|
|
|
self.numRegions = self.private.getNumRegions(vi)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
t1Operators = [
|
|
|
|
# opcode name
|
|
|
|
(1, "hstem"),
|
|
|
|
(3, "vstem"),
|
|
|
|
(4, "vmoveto"),
|
|
|
|
(5, "rlineto"),
|
|
|
|
(6, "hlineto"),
|
|
|
|
(7, "vlineto"),
|
|
|
|
(8, "rrcurveto"),
|
|
|
|
(9, "closepath"),
|
|
|
|
(10, "callsubr"),
|
|
|
|
(11, "return"),
|
|
|
|
(13, "hsbw"),
|
|
|
|
(14, "endchar"),
|
|
|
|
(21, "rmoveto"),
|
|
|
|
(22, "hmoveto"),
|
|
|
|
(30, "vhcurveto"),
|
|
|
|
(31, "hvcurveto"),
|
|
|
|
((12, 0), "dotsection"),
|
|
|
|
((12, 1), "vstem3"),
|
|
|
|
((12, 2), "hstem3"),
|
|
|
|
((12, 6), "seac"),
|
|
|
|
((12, 7), "sbw"),
|
|
|
|
((12, 12), "div"),
|
|
|
|
((12, 16), "callothersubr"),
|
|
|
|
((12, 17), "pop"),
|
|
|
|
((12, 33), "setcurrentpoint"),
|
2022-12-13 11:26:36 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-01-26 11:55:13 +00:00
|
|
|
class T2WidthExtractor(SimpleT2Decompiler):
|
2022-08-26 20:33:03 -06:00
|
|
|
def __init__(
|
2022-12-13 11:26:36 +00:00
|
|
|
self,
|
2022-08-26 20:33:03 -06:00
|
|
|
localSubrs,
|
|
|
|
globalSubrs,
|
|
|
|
nominalWidthX,
|
|
|
|
defaultWidthX,
|
|
|
|
private=None,
|
|
|
|
blender=None,
|
2022-12-13 11:26:36 +00:00
|
|
|
):
|
2022-08-26 20:33:03 -06:00
|
|
|
SimpleT2Decompiler.__init__(self, localSubrs, globalSubrs, private, blender)
|
1999-12-16 21:34:53 +00:00
|
|
|
self.nominalWidthX = nominalWidthX
|
|
|
|
self.defaultWidthX = defaultWidthX
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-26 11:55:13 +00:00
|
|
|
def reset(self):
|
1999-12-16 21:34:53 +00:00
|
|
|
SimpleT2Decompiler.reset(self)
|
|
|
|
self.gotWidth = 0
|
|
|
|
self.width = 0
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def popallWidth(self, evenOdd=0):
|
|
|
|
args = self.popall()
|
2017-01-26 11:55:13 +00:00
|
|
|
if not self.gotWidth:
|
|
|
|
if evenOdd ^ (len(args) % 2):
|
[varLib subset CFF2] Set PrivateDict nominal and default WidthX to None
@bedhad
Address issues raised in #1403
I do think setting the dummy CFF2 PrivateDict nominalWidthX and defaultWidthX to None, which leads to the charstring.width also being None, is a good idea. I originally set them to 0, which produces a charstring width of 0, in order to avoid problems with logic that assumes that the field is good for math. However, I now think that it is better to find errors around charstring type assumptions earlier than later.
"drop_hints()" is actually not wrong - I did look at this when making the changes. For CFF2 charstrings, self.width is always equal to self.private.defaultWidthX, so the width is never inserted. This is because in psCharstrings.py::T2WidthExtractor.popallWidth(), the test "evenOdd ^ (len(args) % 2)" is alway False. Left to myself, I would not change this code. If the CFF2 charstring is correct, there is not a problem. if the CFF2 charstring is not correct, then both in drop_hints() and in T2WidthExtractor.popallWidth(), the logic will stack dump. I did add asserts, but am not totally sure it is worth the extra calls.
2018-12-06 11:55:48 -08:00
|
|
|
# For CFF2 charstrings, this should never happen
|
2022-12-13 11:26:36 +00:00
|
|
|
assert (
|
2018-12-11 17:07:51 -08:00
|
|
|
self.defaultWidthX is not None
|
|
|
|
), "CFF2 CharStrings must not have an initial width value"
|
2017-01-26 11:55:13 +00:00
|
|
|
self.width = self.nominalWidthX + args[0]
|
|
|
|
args = args[1:]
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
2017-01-26 11:55:13 +00:00
|
|
|
self.width = self.defaultWidthX
|
|
|
|
self.gotWidth = 1
|
|
|
|
return args
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def countHints(self):
|
2003-08-28 20:43:06 +00:00
|
|
|
args = self.popallWidth()
|
2013-11-27 17:46:17 -05:00
|
|
|
self.hintCount = self.hintCount + len(args) // 2
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_rmoveto(self, index):
|
2003-08-24 19:56:16 +00:00
|
|
|
self.popallWidth()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hmoveto(self, index):
|
2003-08-24 19:56:16 +00:00
|
|
|
self.popallWidth(1)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_vmoveto(self, index):
|
2003-08-24 19:56:16 +00:00
|
|
|
self.popallWidth(1)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-26 11:55:13 +00:00
|
|
|
def op_endchar(self, index):
|
|
|
|
self.popallWidth()
|
|
|
|
|
|
|
|
|
|
|
|
class T2OutlineExtractor(T2WidthExtractor):
|
2022-08-26 20:33:03 -06:00
|
|
|
def __init__(
|
2022-12-13 11:26:36 +00:00
|
|
|
self,
|
|
|
|
pen,
|
2022-08-26 20:33:03 -06:00
|
|
|
localSubrs,
|
|
|
|
globalSubrs,
|
|
|
|
nominalWidthX,
|
|
|
|
defaultWidthX,
|
|
|
|
private=None,
|
|
|
|
blender=None,
|
2022-12-13 11:26:36 +00:00
|
|
|
):
|
2017-01-26 11:55:13 +00:00
|
|
|
T2WidthExtractor.__init__(
|
2022-12-13 11:26:36 +00:00
|
|
|
self,
|
2022-08-26 20:33:03 -06:00
|
|
|
localSubrs,
|
|
|
|
globalSubrs,
|
|
|
|
nominalWidthX,
|
|
|
|
defaultWidthX,
|
2022-12-13 11:26:36 +00:00
|
|
|
private,
|
|
|
|
blender,
|
|
|
|
)
|
2017-01-26 11:55:13 +00:00
|
|
|
self.pen = pen
|
2022-04-14 07:19:44 -07:00
|
|
|
self.subrLevel = 0
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-26 11:55:13 +00:00
|
|
|
def reset(self):
|
|
|
|
T2WidthExtractor.reset(self)
|
2003-08-24 19:56:16 +00:00
|
|
|
self.currentPoint = (0, 0)
|
|
|
|
self.sawMoveTo = 0
|
2022-04-14 07:19:44 -07:00
|
|
|
self.subrLevel = 0
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-04-14 07:19:44 -07:00
|
|
|
def execute(self, charString):
|
|
|
|
self.subrLevel += 1
|
|
|
|
super().execute(charString)
|
|
|
|
self.subrLevel -= 1
|
|
|
|
if self.subrLevel == 0:
|
2003-09-16 09:48:28 +00:00
|
|
|
self.endPath()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2003-08-24 19:56:16 +00:00
|
|
|
def _nextPoint(self, point):
|
|
|
|
x, y = self.currentPoint
|
|
|
|
point = x + point[0], y + point[1]
|
|
|
|
self.currentPoint = point
|
|
|
|
return point
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2003-08-24 19:56:16 +00:00
|
|
|
def rMoveTo(self, point):
|
|
|
|
self.pen.moveTo(self._nextPoint(point))
|
|
|
|
self.sawMoveTo = 1
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2003-08-24 19:56:16 +00:00
|
|
|
def rLineTo(self, point):
|
|
|
|
if not self.sawMoveTo:
|
|
|
|
self.rMoveTo((0, 0))
|
|
|
|
self.pen.lineTo(self._nextPoint(point))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2003-08-24 19:56:16 +00:00
|
|
|
def rCurveTo(self, pt1, pt2, pt3):
|
|
|
|
if not self.sawMoveTo:
|
|
|
|
self.rMoveTo((0, 0))
|
|
|
|
nextPoint = self._nextPoint
|
|
|
|
self.pen.curveTo(nextPoint(pt1), nextPoint(pt2), nextPoint(pt3))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def closePath(self):
|
2022-04-14 07:19:44 -07:00
|
|
|
if self.sawMoveTo:
|
2003-08-24 19:56:16 +00:00
|
|
|
self.pen.closePath()
|
|
|
|
self.sawMoveTo = 0
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2003-09-16 09:48:28 +00:00
|
|
|
def endPath(self):
|
|
|
|
# In T2 there are no open paths, so always do a closePath when
|
2022-04-14 07:19:44 -07:00
|
|
|
# finishing a sub path. We avoid spurious calls to closePath()
|
|
|
|
# because its a real T1 op we're emulating in T2 whereas
|
|
|
|
# endPath() is just a means to that emulation
|
|
|
|
if self.sawMoveTo:
|
|
|
|
self.closePath()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
# hint operators
|
2022-12-13 11:26:36 +00:00
|
|
|
#
|
2002-05-17 18:37:07 +00:00
|
|
|
# def op_hstem(self, index):
|
1999-12-16 21:34:53 +00:00
|
|
|
# self.countHints()
|
2002-05-17 18:37:07 +00:00
|
|
|
# def op_vstem(self, index):
|
1999-12-16 21:34:53 +00:00
|
|
|
# self.countHints()
|
2002-05-17 18:37:07 +00:00
|
|
|
# def op_hstemhm(self, index):
|
1999-12-16 21:34:53 +00:00
|
|
|
# self.countHints()
|
2002-05-17 18:37:07 +00:00
|
|
|
# def op_vstemhm(self, index):
|
1999-12-16 21:34:53 +00:00
|
|
|
# self.countHints()
|
|
|
|
# def op_hintmask(self, index):
|
|
|
|
# self.countHints()
|
|
|
|
# def op_cntrmask(self, index):
|
|
|
|
# self.countHints()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
# path constructors, moveto
|
2022-12-13 11:26:36 +00:00
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_rmoveto(self, index):
|
2003-09-16 09:48:28 +00:00
|
|
|
self.endPath()
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rMoveTo(self.popallWidth())
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hmoveto(self, index):
|
2003-09-16 09:48:28 +00:00
|
|
|
self.endPath()
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rMoveTo((self.popallWidth(1)[0], 0))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_vmoveto(self, index):
|
2003-09-16 09:48:28 +00:00
|
|
|
self.endPath()
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rMoveTo((0, self.popallWidth(1)[0]))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_endchar(self, index):
|
2003-09-16 09:48:28 +00:00
|
|
|
self.endPath()
|
2003-08-28 20:43:06 +00:00
|
|
|
args = self.popallWidth()
|
2022-12-13 11:26:36 +00:00
|
|
|
if args:
|
2003-08-28 20:43:06 +00:00
|
|
|
from fontTools.encodings.StandardEncoding import StandardEncoding
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2003-08-28 20:43:06 +00:00
|
|
|
# endchar can do seac accent bulding; The T2 spec says it's deprecated,
|
|
|
|
# but recent software that shall remain nameless does output it.
|
|
|
|
adx, ady, bchar, achar = args
|
|
|
|
baseGlyph = StandardEncoding[bchar]
|
|
|
|
self.pen.addComponent(baseGlyph, (1, 0, 0, 1, 0, 0))
|
|
|
|
accentGlyph = StandardEncoding[achar]
|
|
|
|
self.pen.addComponent(accentGlyph, (1, 0, 0, 1, adx, ady))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
# path constructors, lines
|
2022-12-13 11:26:36 +00:00
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_rlineto(self, index):
|
|
|
|
args = self.popall()
|
|
|
|
for i in range(0, len(args), 2):
|
|
|
|
point = args[i : i + 2]
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rLineTo(point)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hlineto(self, index):
|
|
|
|
self.alternatingLineto(1)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_vlineto(self, index):
|
|
|
|
self.alternatingLineto(0)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
# path constructors, curves
|
2022-12-13 11:26:36 +00:00
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_rrcurveto(self, index):
|
|
|
|
"""{dxa dya dxb dyb dxc dyc}+ rrcurveto"""
|
|
|
|
args = self.popall()
|
|
|
|
for i in range(0, len(args), 6):
|
2022-12-13 11:26:36 +00:00
|
|
|
(
|
|
|
|
dxa,
|
|
|
|
dya,
|
|
|
|
dxb,
|
|
|
|
dyb,
|
|
|
|
dxc,
|
|
|
|
dyc,
|
2017-01-12 15:23:12 -08:00
|
|
|
) = args[i : i + 6]
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rCurveTo((dxa, dya), (dxb, dyb), (dxc, dyc))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_rcurveline(self, index):
|
|
|
|
"""{dxa dya dxb dyb dxc dyc}+ dxd dyd rcurveline"""
|
|
|
|
args = self.popall()
|
|
|
|
for i in range(0, len(args) - 2, 6):
|
|
|
|
dxb, dyb, dxc, dyc, dxd, dyd = args[i : i + 6]
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rCurveTo((dxb, dyb), (dxc, dyc), (dxd, dyd))
|
|
|
|
self.rLineTo(args[-2:])
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_rlinecurve(self, index):
|
|
|
|
"""{dxa dya}+ dxb dyb dxc dyc dxd dyd rlinecurve"""
|
|
|
|
args = self.popall()
|
|
|
|
lineArgs = args[:-6]
|
|
|
|
for i in range(0, len(lineArgs), 2):
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rLineTo(lineArgs[i : i + 2])
|
1999-12-16 21:34:53 +00:00
|
|
|
dxb, dyb, dxc, dyc, dxd, dyd = args[-6:]
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rCurveTo((dxb, dyb), (dxc, dyc), (dxd, dyd))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_vvcurveto(self, index):
|
|
|
|
"dx1? {dya dxb dyb dyc}+ vvcurveto"
|
|
|
|
args = self.popall()
|
|
|
|
if len(args) % 2:
|
|
|
|
dx1 = args[0]
|
|
|
|
args = args[1:]
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
|
|
|
dx1 = 0
|
1999-12-16 21:34:53 +00:00
|
|
|
for i in range(0, len(args), 4):
|
|
|
|
dya, dxb, dyb, dyc = args[i : i + 4]
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rCurveTo((dx1, dya), (dxb, dyb), (0, dyc))
|
2022-12-13 11:26:36 +00:00
|
|
|
dx1 = 0
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hhcurveto(self, index):
|
|
|
|
"""dy1? {dxa dxb dyb dxc}+ hhcurveto"""
|
|
|
|
args = self.popall()
|
|
|
|
if len(args) % 2:
|
|
|
|
dy1 = args[0]
|
|
|
|
args = args[1:]
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
|
|
|
dy1 = 0
|
1999-12-16 21:34:53 +00:00
|
|
|
for i in range(0, len(args), 4):
|
|
|
|
dxa, dxb, dyb, dxc = args[i : i + 4]
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rCurveTo((dxa, dy1), (dxb, dyb), (dxc, 0))
|
2022-12-13 11:26:36 +00:00
|
|
|
dy1 = 0
|
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_vhcurveto(self, index):
|
|
|
|
"""dy1 dx2 dy2 dx3 {dxa dxb dyb dyc dyd dxe dye dxf}* dyf? vhcurveto (30)
|
|
|
|
{dya dxb dyb dxc dxd dxe dye dyf}+ dxf? vhcurveto
|
2022-12-13 11:26:36 +00:00
|
|
|
"""
|
1999-12-16 21:34:53 +00:00
|
|
|
args = self.popall()
|
|
|
|
while args:
|
|
|
|
args = self.vcurveto(args)
|
2022-12-13 11:26:36 +00:00
|
|
|
if args:
|
1999-12-16 21:34:53 +00:00
|
|
|
args = self.hcurveto(args)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hvcurveto(self, index):
|
|
|
|
"""dx1 dx2 dy2 dy3 {dya dxb dyb dxc dxd dxe dye dyf}* dxf?
|
|
|
|
{dxa dxb dyb dyc dyd dxe dye dxf}+ dyf?
|
2022-12-13 11:26:36 +00:00
|
|
|
"""
|
1999-12-16 21:34:53 +00:00
|
|
|
args = self.popall()
|
|
|
|
while args:
|
|
|
|
args = self.hcurveto(args)
|
2022-12-13 11:26:36 +00:00
|
|
|
if args:
|
1999-12-16 21:34:53 +00:00
|
|
|
args = self.vcurveto(args)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
# path constructors, flex
|
2022-12-13 11:26:36 +00:00
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hflex(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
dx1, dx2, dy2, dx3, dx4, dx5, dx6 = self.popall()
|
2005-05-07 08:41:12 +00:00
|
|
|
dy1 = dy3 = dy4 = dy6 = 0
|
|
|
|
dy5 = -dy2
|
2003-09-17 17:32:11 +00:00
|
|
|
self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
|
|
|
|
self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_flex(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4, dx5, dy5, dx6, dy6, fd = self.popall()
|
|
|
|
self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
|
|
|
|
self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hflex1(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
dx1, dy1, dx2, dy2, dx3, dx4, dx5, dy5, dx6 = self.popall()
|
2005-05-07 08:41:12 +00:00
|
|
|
dy3 = dy4 = 0
|
|
|
|
dy6 = -(dy1 + dy2 + dy3 + dy4 + dy5)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2003-09-17 17:32:11 +00:00
|
|
|
self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
|
|
|
|
self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_flex1(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4, dx5, dy5, d6 = self.popall()
|
|
|
|
dx = dx1 + dx2 + dx3 + dx4 + dx5
|
|
|
|
dy = dy1 + dy2 + dy3 + dy4 + dy5
|
|
|
|
if abs(dx) > abs(dy):
|
2022-12-13 11:26:36 +00:00
|
|
|
dx6 = d6
|
2005-05-07 08:41:12 +00:00
|
|
|
dy6 = -dy
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
2005-05-07 08:41:12 +00:00
|
|
|
dx6 = -dx
|
2022-12-13 11:26:36 +00:00
|
|
|
dy6 = d6
|
2003-09-17 17:32:11 +00:00
|
|
|
self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
|
|
|
|
self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
# misc
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_and(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_or(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_not(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_store(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_abs(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_add(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_sub(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_div(self, index):
|
|
|
|
num2 = self.pop()
|
|
|
|
num1 = self.pop()
|
2013-11-27 17:46:17 -05:00
|
|
|
d1 = num1 // num2
|
|
|
|
d2 = num1 / num2
|
1999-12-16 21:34:53 +00:00
|
|
|
if d1 == d2:
|
|
|
|
self.push(d1)
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
1999-12-16 21:34:53 +00:00
|
|
|
self.push(d2)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_load(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_neg(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_eq(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_drop(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_put(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_get(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_ifelse(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_random(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_mul(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_sqrt(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_dup(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_exch(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_index(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_roll(self, index):
|
2003-09-17 17:32:11 +00:00
|
|
|
raise NotImplementedError
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
#
|
2013-11-29 17:12:43 -05:00
|
|
|
# miscellaneous helpers
|
2022-12-13 11:26:36 +00:00
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
def alternatingLineto(self, isHorizontal):
|
|
|
|
args = self.popall()
|
|
|
|
for arg in args:
|
|
|
|
if isHorizontal:
|
|
|
|
point = (arg, 0)
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
1999-12-16 21:34:53 +00:00
|
|
|
point = (0, arg)
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rLineTo(point)
|
1999-12-16 21:34:53 +00:00
|
|
|
isHorizontal = not isHorizontal
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def vcurveto(self, args):
|
|
|
|
dya, dxb, dyb, dxc = args[:4]
|
|
|
|
args = args[4:]
|
|
|
|
if len(args) == 1:
|
|
|
|
dyc = args[0]
|
|
|
|
args = []
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
|
|
|
dyc = 0
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rCurveTo((0, dya), (dxb, dyb), (dxc, dyc))
|
1999-12-16 21:34:53 +00:00
|
|
|
return args
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def hcurveto(self, args):
|
|
|
|
dxa, dxb, dyb, dyc = args[:4]
|
|
|
|
args = args[4:]
|
|
|
|
if len(args) == 1:
|
|
|
|
dxc = args[0]
|
|
|
|
args = []
|
2022-12-13 11:26:36 +00:00
|
|
|
else:
|
|
|
|
dxc = 0
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rCurveTo((dxa, 0), (dxb, dyb), (dxc, dyc))
|
1999-12-16 21:34:53 +00:00
|
|
|
return args
|
2017-01-26 11:55:13 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
class T1OutlineExtractor(T2OutlineExtractor):
|
2003-08-24 19:56:16 +00:00
|
|
|
def __init__(self, pen, subrs):
|
|
|
|
self.pen = pen
|
1999-12-16 21:34:53 +00:00
|
|
|
self.subrs = subrs
|
|
|
|
self.reset()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def reset(self):
|
|
|
|
self.flexing = 0
|
|
|
|
self.width = 0
|
|
|
|
self.sbx = 0
|
|
|
|
T2OutlineExtractor.reset(self)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2003-09-16 09:48:28 +00:00
|
|
|
def endPath(self):
|
|
|
|
if self.sawMoveTo:
|
|
|
|
self.pen.endPath()
|
|
|
|
self.sawMoveTo = 0
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def popallWidth(self, evenOdd=0):
|
|
|
|
return self.popall()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def exch(self):
|
|
|
|
stack = self.operandStack
|
|
|
|
stack[-1], stack[-2] = stack[-2], stack[-1]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
# path constructors
|
2022-12-13 11:26:36 +00:00
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_rmoveto(self, index):
|
|
|
|
if self.flexing:
|
2022-12-13 11:26:36 +00:00
|
|
|
return
|
2003-09-16 09:48:28 +00:00
|
|
|
self.endPath()
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rMoveTo(self.popall())
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hmoveto(self, index):
|
|
|
|
if self.flexing:
|
|
|
|
# We must add a parameter to the stack if we are flexing
|
|
|
|
self.push(0)
|
2022-12-13 11:26:36 +00:00
|
|
|
return
|
2003-09-16 09:48:28 +00:00
|
|
|
self.endPath()
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rMoveTo((self.popall()[0], 0))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_vmoveto(self, index):
|
|
|
|
if self.flexing:
|
|
|
|
# We must add a parameter to the stack if we are flexing
|
|
|
|
self.push(0)
|
|
|
|
self.exch()
|
2022-12-13 11:26:36 +00:00
|
|
|
return
|
2003-09-16 09:48:28 +00:00
|
|
|
self.endPath()
|
2003-08-24 19:56:16 +00:00
|
|
|
self.rMoveTo((0, self.popall()[0]))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_closepath(self, index):
|
|
|
|
self.closePath()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_setcurrentpoint(self, index):
|
|
|
|
args = self.popall()
|
|
|
|
x, y = args
|
2003-08-24 19:56:16 +00:00
|
|
|
self.currentPoint = x, y
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_endchar(self, index):
|
2003-09-16 09:48:28 +00:00
|
|
|
self.endPath()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hsbw(self, index):
|
|
|
|
sbx, wx = self.popall()
|
|
|
|
self.width = wx
|
|
|
|
self.sbx = sbx
|
2003-08-24 19:56:16 +00:00
|
|
|
self.currentPoint = sbx, self.currentPoint[1]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_sbw(self, index):
|
|
|
|
self.popall() # XXX
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
#
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_callsubr(self, index):
|
|
|
|
subrIndex = self.pop()
|
|
|
|
subr = self.subrs[subrIndex]
|
|
|
|
self.execute(subr)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_callothersubr(self, index):
|
|
|
|
subrIndex = self.pop()
|
|
|
|
nArgs = self.pop()
|
|
|
|
# print nArgs, subrIndex, "callothersubr"
|
|
|
|
if subrIndex == 0 and nArgs == 3:
|
|
|
|
self.doFlex()
|
|
|
|
self.flexing = 0
|
|
|
|
elif subrIndex == 1 and nArgs == 0:
|
|
|
|
self.flexing = 1
|
|
|
|
# ignore...
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_pop(self, index):
|
|
|
|
pass # ignore...
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def doFlex(self):
|
|
|
|
finaly = self.pop()
|
|
|
|
finalx = self.pop()
|
|
|
|
self.pop() # flex height is unused
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
p3y = self.pop()
|
|
|
|
p3x = self.pop()
|
|
|
|
bcp4y = self.pop()
|
|
|
|
bcp4x = self.pop()
|
|
|
|
bcp3y = self.pop()
|
|
|
|
bcp3x = self.pop()
|
|
|
|
p2y = self.pop()
|
|
|
|
p2x = self.pop()
|
|
|
|
bcp2y = self.pop()
|
|
|
|
bcp2x = self.pop()
|
|
|
|
bcp1y = self.pop()
|
|
|
|
bcp1x = self.pop()
|
|
|
|
rpy = self.pop()
|
|
|
|
rpx = self.pop()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
# call rrcurveto
|
|
|
|
self.push(bcp1x + rpx)
|
|
|
|
self.push(bcp1y + rpy)
|
|
|
|
self.push(bcp2x)
|
|
|
|
self.push(bcp2y)
|
|
|
|
self.push(p2x)
|
|
|
|
self.push(p2y)
|
|
|
|
self.op_rrcurveto(None)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
# call rrcurveto
|
|
|
|
self.push(bcp3x)
|
|
|
|
self.push(bcp3y)
|
|
|
|
self.push(bcp4x)
|
|
|
|
self.push(bcp4y)
|
|
|
|
self.push(p3x)
|
|
|
|
self.push(p3y)
|
|
|
|
self.op_rrcurveto(None)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
# Push back final coords so subr 0 can find them
|
|
|
|
self.push(finalx)
|
|
|
|
self.push(finaly)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_dotsection(self, index):
|
|
|
|
self.popall() # XXX
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_hstem3(self, index):
|
|
|
|
self.popall() # XXX
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_seac(self, index):
|
|
|
|
"asb adx ady bchar achar seac"
|
2003-08-24 19:56:16 +00:00
|
|
|
from fontTools.encodings.StandardEncoding import StandardEncoding
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2003-08-24 19:56:16 +00:00
|
|
|
asb, adx, ady, bchar, achar = self.popall()
|
|
|
|
baseGlyph = StandardEncoding[bchar]
|
|
|
|
self.pen.addComponent(baseGlyph, (1, 0, 0, 1, 0, 0))
|
|
|
|
accentGlyph = StandardEncoding[achar]
|
|
|
|
adx = adx + self.sbx - asb # seac weirdness
|
|
|
|
self.pen.addComponent(accentGlyph, (1, 0, 0, 1, adx, ady))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def op_vstem3(self, index):
|
|
|
|
self.popall() # XXX
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
|
2017-07-19 18:18:58 +01:00
|
|
|
class T2CharString(object):
|
2017-01-12 15:23:12 -08:00
|
|
|
operandEncoding = t2OperandEncoding
|
|
|
|
operators, opcodes = buildOperatorDict(t2Operators)
|
|
|
|
decompilerClass = SimpleT2Decompiler
|
|
|
|
outlineExtractor = T2OutlineExtractor
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-07-19 18:18:58 +01:00
|
|
|
def __init__(self, bytecode=None, program=None, private=None, globalSubrs=None):
|
2017-01-12 15:23:12 -08:00
|
|
|
if program is None:
|
|
|
|
program = []
|
|
|
|
self.bytecode = bytecode
|
|
|
|
self.program = program
|
|
|
|
self.private = private
|
|
|
|
self.globalSubrs = globalSubrs if globalSubrs is not None else []
|
Sparse cff2vf support (#1591)
* Added getter (in the form of a property decorator) for T2Charstring.vsindex. Fixes endless compile loop in some circumstances.
Fixed bug in mutator: need to remove vsindex from snapshotted charstrings, plus formatting clean up
* Fix for subsetting HVAR tables that have an AdvanceWidthMap when the --retain-gid option is used. Needed to make subset_test.py::test_retain_gids_cff2 tests pass.
* in varLib/cffLib.py, add support for sparse sources, and sources with more than one model, and hence more than one VarData element in the VarStore.
CFF2 source fonts with multiple FontDicts in the FDArray need some extra work. With sparse fonts, some of the source fonts may have a fewer FontDicts than the default font. The getfd_map function() builds a map from the FontDict indices in the default font to those in each region font. This is needed when building up the blend value lists in the master font FontDict PrivateDicts, in order to fetch PrivateDict values from the correct FontDict in each region font.
In specializer.py, add support for CFF2 CharStrings with blend operators. 1) In generalizeCommands, convert a blend op to a list of args that are blend lists for the following regular operator. A blend list as a default font value, followed by the delta tuple. 2) In specializeCommands(), convert these back to blend ops, combining as many successive blend lists as allowed by the stack limit.
Add test case for sparse CFF2 sources.
The test font has 55 glyphs. 2 glyphs use only 2 sources (weight = 0 and 100). The rest use 4 source fonts: the two end points of the weight axis, and two intermediate masters. The intermediate masters are only 1 design space unit apart, and are used to change glyph design at the point in design space. For the rest, at most 2 glyphs use the same set of source fonts. There are 12 source fonts.
Add test case for specializer programToCommands() and commandsToProgram by converting each CharString.program in the font to a command list, and back again, and comparing original and final versions.
2019-04-26 09:33:52 -07:00
|
|
|
self._cur_vsindex = None
|
2022-12-13 11:26:36 +00:00
|
|
|
|
Sparse cff2vf support (#1591)
* Added getter (in the form of a property decorator) for T2Charstring.vsindex. Fixes endless compile loop in some circumstances.
Fixed bug in mutator: need to remove vsindex from snapshotted charstrings, plus formatting clean up
* Fix for subsetting HVAR tables that have an AdvanceWidthMap when the --retain-gid option is used. Needed to make subset_test.py::test_retain_gids_cff2 tests pass.
* in varLib/cffLib.py, add support for sparse sources, and sources with more than one model, and hence more than one VarData element in the VarStore.
CFF2 source fonts with multiple FontDicts in the FDArray need some extra work. With sparse fonts, some of the source fonts may have a fewer FontDicts than the default font. The getfd_map function() builds a map from the FontDict indices in the default font to those in each region font. This is needed when building up the blend value lists in the master font FontDict PrivateDicts, in order to fetch PrivateDict values from the correct FontDict in each region font.
In specializer.py, add support for CFF2 CharStrings with blend operators. 1) In generalizeCommands, convert a blend op to a list of args that are blend lists for the following regular operator. A blend list as a default font value, followed by the delta tuple. 2) In specializeCommands(), convert these back to blend ops, combining as many successive blend lists as allowed by the stack limit.
Add test case for sparse CFF2 sources.
The test font has 55 glyphs. 2 glyphs use only 2 sources (weight = 0 and 100). The rest use 4 source fonts: the two end points of the weight axis, and two intermediate masters. The intermediate masters are only 1 design space unit apart, and are used to change glyph design at the point in design space. For the rest, at most 2 glyphs use the same set of source fonts. There are 12 source fonts.
Add test case for specializer programToCommands() and commandsToProgram by converting each CharString.program in the font to a command list, and back again, and comparing original and final versions.
2019-04-26 09:33:52 -07:00
|
|
|
def getNumRegions(self, vsindex=None):
|
|
|
|
pd = self.private
|
|
|
|
assert pd is not None
|
|
|
|
if vsindex is not None:
|
|
|
|
self._cur_vsindex = vsindex
|
|
|
|
elif self._cur_vsindex is None:
|
|
|
|
self._cur_vsindex = pd.vsindex if hasattr(pd, "vsindex") else 0
|
|
|
|
return pd.getNumRegions(self._cur_vsindex)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def __repr__(self):
|
|
|
|
if self.bytecode is None:
|
|
|
|
return "<%s (source) at %x>" % (self.__class__.__name__, id(self))
|
|
|
|
else:
|
|
|
|
return "<%s (bytecode) at %x>" % (self.__class__.__name__, id(self))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def getIntEncoder(self):
|
|
|
|
return encodeIntT2
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def getFixedEncoder(self):
|
|
|
|
return encodeFixed
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def decompile(self):
|
|
|
|
if not self.needsDecompilation():
|
|
|
|
return
|
|
|
|
subrs = getattr(self.private, "Subrs", [])
|
2017-05-09 11:23:14 -07:00
|
|
|
decompiler = self.decompilerClass(subrs, self.globalSubrs, self.private)
|
2017-01-12 15:23:12 -08:00
|
|
|
decompiler.execute(self)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-08-26 20:33:03 -06:00
|
|
|
def draw(self, pen, blender=None):
|
2017-01-12 15:23:12 -08:00
|
|
|
subrs = getattr(self.private, "Subrs", [])
|
|
|
|
extractor = self.outlineExtractor(
|
|
|
|
pen,
|
|
|
|
subrs,
|
|
|
|
self.globalSubrs,
|
2018-12-05 15:50:10 -08:00
|
|
|
self.private.nominalWidthX,
|
|
|
|
self.private.defaultWidthX,
|
2022-08-26 20:33:03 -06:00
|
|
|
self.private,
|
|
|
|
blender,
|
|
|
|
)
|
2017-01-12 15:23:12 -08:00
|
|
|
extractor.execute(self)
|
|
|
|
self.width = extractor.width
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2018-01-26 16:53:04 -08:00
|
|
|
def calcBounds(self, glyphSet):
|
|
|
|
boundsPen = BoundsPen(glyphSet)
|
2017-05-18 15:01:05 +09:00
|
|
|
self.draw(boundsPen)
|
2017-08-01 10:35:39 +09:00
|
|
|
return boundsPen.bounds
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-07-19 18:18:58 +01:00
|
|
|
def compile(self, isCFF2=False):
|
2017-01-12 15:23:12 -08:00
|
|
|
if self.bytecode is not None:
|
|
|
|
return
|
|
|
|
opcodes = self.opcodes
|
|
|
|
program = self.program
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2019-01-17 16:01:15 +01:00
|
|
|
if isCFF2:
|
|
|
|
# If present, remove return and endchar operators.
|
|
|
|
if program and program[-1] in ("return", "endchar"):
|
|
|
|
program = program[:-1]
|
2021-03-27 10:23:29 -04:00
|
|
|
elif program and not isinstance(program[-1], str):
|
2019-01-17 16:01:15 +01:00
|
|
|
raise CharStringCompileError(
|
|
|
|
"T2CharString or Subr has items on the stack after last operator."
|
|
|
|
)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
bytecode = []
|
|
|
|
encodeInt = self.getIntEncoder()
|
|
|
|
encodeFixed = self.getFixedEncoder()
|
|
|
|
i = 0
|
|
|
|
end = len(program)
|
|
|
|
while i < end:
|
|
|
|
token = program[i]
|
|
|
|
i = i + 1
|
2021-03-27 10:23:29 -04:00
|
|
|
if isinstance(token, str):
|
2017-01-12 15:23:12 -08:00
|
|
|
try:
|
|
|
|
bytecode.extend(bytechr(b) for b in opcodes[token])
|
|
|
|
except KeyError:
|
|
|
|
raise CharStringCompileError("illegal operator: %s" % token)
|
|
|
|
if token in ("hintmask", "cntrmask"):
|
|
|
|
bytecode.append(program[i]) # hint mask
|
|
|
|
i = i + 1
|
2018-06-27 13:04:56 -07:00
|
|
|
elif isinstance(token, int):
|
2017-01-12 15:23:12 -08:00
|
|
|
bytecode.append(encodeInt(token))
|
2018-06-27 13:04:56 -07:00
|
|
|
elif isinstance(token, float):
|
2017-01-12 15:23:12 -08:00
|
|
|
bytecode.append(encodeFixed(token))
|
|
|
|
else:
|
2018-06-27 13:04:56 -07:00
|
|
|
assert 0, "unsupported type: %s" % type(token)
|
2017-01-12 15:23:12 -08:00
|
|
|
try:
|
|
|
|
bytecode = bytesjoin(bytecode)
|
|
|
|
except TypeError:
|
|
|
|
log.error(bytecode)
|
|
|
|
raise
|
|
|
|
self.setBytecode(bytecode)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def needsDecompilation(self):
|
|
|
|
return self.bytecode is not None
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def setProgram(self, program):
|
|
|
|
self.program = program
|
|
|
|
self.bytecode = None
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def setBytecode(self, bytecode):
|
|
|
|
self.bytecode = bytecode
|
|
|
|
self.program = None
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2021-03-27 10:23:29 -04:00
|
|
|
def getToken(self, index, len=len, byteord=byteord, isinstance=isinstance):
|
2017-01-12 15:23:12 -08:00
|
|
|
if self.bytecode is not None:
|
|
|
|
if index >= len(self.bytecode):
|
|
|
|
return None, 0, 0
|
|
|
|
b0 = byteord(self.bytecode[index])
|
|
|
|
index = index + 1
|
|
|
|
handler = self.operandEncoding[b0]
|
|
|
|
token, index = handler(self, b0, self.bytecode, index)
|
|
|
|
else:
|
|
|
|
if index >= len(self.program):
|
|
|
|
return None, 0, 0
|
|
|
|
token = self.program[index]
|
|
|
|
index = index + 1
|
2021-03-27 10:23:29 -04:00
|
|
|
isOperator = isinstance(token, str)
|
2017-01-12 15:23:12 -08:00
|
|
|
return token, isOperator, index
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def getBytes(self, index, nBytes):
|
|
|
|
if self.bytecode is not None:
|
|
|
|
newIndex = index + nBytes
|
|
|
|
bytes = self.bytecode[index:newIndex]
|
|
|
|
index = newIndex
|
|
|
|
else:
|
|
|
|
bytes = self.program[index]
|
|
|
|
index = index + 1
|
|
|
|
assert len(bytes) == nBytes
|
|
|
|
return bytes, index
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def handle_operator(self, operator):
|
|
|
|
return operator
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2019-10-07 11:46:16 +01:00
|
|
|
def toXML(self, xmlWriter, ttFont=None):
|
2017-01-12 15:23:12 -08:00
|
|
|
from fontTools.misc.textTools import num2binary
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
if self.bytecode is not None:
|
|
|
|
xmlWriter.dumphex(self.bytecode)
|
|
|
|
else:
|
|
|
|
index = 0
|
|
|
|
args = []
|
|
|
|
while True:
|
|
|
|
token, isOperator, index = self.getToken(index)
|
|
|
|
if token is None:
|
|
|
|
break
|
|
|
|
if isOperator:
|
|
|
|
if token in ("hintmask", "cntrmask"):
|
|
|
|
hintMask, isOperator, index = self.getToken(index)
|
|
|
|
bits = []
|
|
|
|
for byte in hintMask:
|
|
|
|
bits.append(num2binary(byteord(byte), 8))
|
|
|
|
hintMask = strjoin(bits)
|
|
|
|
line = " ".join(args + [token, hintMask])
|
|
|
|
else:
|
|
|
|
line = " ".join(args + [token])
|
|
|
|
xmlWriter.write(line)
|
|
|
|
xmlWriter.newline()
|
|
|
|
args = []
|
|
|
|
else:
|
2019-10-07 11:46:16 +01:00
|
|
|
if isinstance(token, float):
|
|
|
|
token = floatToFixedToStr(token, precisionBits=16)
|
|
|
|
else:
|
|
|
|
token = str(token)
|
2017-01-12 15:23:12 -08:00
|
|
|
args.append(token)
|
2017-11-02 11:36:10 -07:00
|
|
|
if args:
|
2019-01-17 11:48:35 +00:00
|
|
|
# NOTE: only CFF2 charstrings/subrs can have numeric arguments on
|
|
|
|
# the stack after the last operator. Compiling this would fail if
|
|
|
|
# this is part of CFF 1.0 table.
|
|
|
|
line = " ".join(args)
|
|
|
|
xmlWriter.write(line)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def fromXML(self, name, attrs, content):
|
|
|
|
from fontTools.misc.textTools import binary2num, readHex
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
if attrs.get("raw"):
|
|
|
|
self.setBytecode(readHex(content))
|
|
|
|
return
|
|
|
|
content = strjoin(content)
|
|
|
|
content = content.split()
|
|
|
|
program = []
|
|
|
|
end = len(content)
|
|
|
|
i = 0
|
|
|
|
while i < end:
|
|
|
|
token = content[i]
|
|
|
|
i = i + 1
|
|
|
|
try:
|
|
|
|
token = int(token)
|
|
|
|
except ValueError:
|
|
|
|
try:
|
2019-10-07 11:46:16 +01:00
|
|
|
token = strToFixedToFloat(token, precisionBits=16)
|
2017-01-12 15:23:12 -08:00
|
|
|
except ValueError:
|
|
|
|
program.append(token)
|
|
|
|
if token in ("hintmask", "cntrmask"):
|
|
|
|
mask = content[i]
|
|
|
|
maskBytes = b""
|
|
|
|
for j in range(0, len(mask), 8):
|
|
|
|
maskBytes = maskBytes + bytechr(binary2num(mask[j : j + 8]))
|
|
|
|
program.append(maskBytes)
|
|
|
|
i = i + 1
|
|
|
|
else:
|
|
|
|
program.append(token)
|
|
|
|
else:
|
|
|
|
program.append(token)
|
|
|
|
self.setProgram(program)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
|
|
|
|
class T1CharString(T2CharString):
|
|
|
|
operandEncoding = t1OperandEncoding
|
|
|
|
operators, opcodes = buildOperatorDict(t1Operators)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def __init__(self, bytecode=None, program=None, subrs=None):
|
2020-05-07 21:21:40 +01:00
|
|
|
super().__init__(bytecode, program)
|
2017-01-12 15:23:12 -08:00
|
|
|
self.subrs = subrs
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def getIntEncoder(self):
|
|
|
|
return encodeIntT1
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def getFixedEncoder(self):
|
|
|
|
def encodeFixed(value):
|
|
|
|
raise TypeError("Type 1 charstrings don't support floating point operands")
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def decompile(self):
|
|
|
|
if self.bytecode is None:
|
|
|
|
return
|
|
|
|
program = []
|
|
|
|
index = 0
|
|
|
|
while True:
|
|
|
|
token, isOperator, index = self.getToken(index)
|
|
|
|
if token is None:
|
|
|
|
break
|
|
|
|
program.append(token)
|
|
|
|
self.setProgram(program)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def draw(self, pen):
|
|
|
|
extractor = T1OutlineExtractor(pen, self.subrs)
|
|
|
|
extractor.execute(self)
|
|
|
|
self.width = extractor.width
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
|
2017-07-19 18:18:58 +01:00
|
|
|
class DictDecompiler(object):
|
1999-12-16 21:34:53 +00:00
|
|
|
operandEncoding = cffDictOperandEncoding
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-05-25 08:49:20 -07:00
|
|
|
def __init__(self, strings, parent=None):
|
1999-12-16 21:34:53 +00:00
|
|
|
self.stack = []
|
|
|
|
self.strings = strings
|
|
|
|
self.dict = {}
|
2017-05-09 11:28:11 -07:00
|
|
|
self.parent = parent
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def getDict(self):
|
|
|
|
assert len(self.stack) == 0, "non-empty stack"
|
|
|
|
return self.dict
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def decompile(self, data):
|
|
|
|
index = 0
|
|
|
|
lenData = len(data)
|
|
|
|
push = self.stack.append
|
|
|
|
while index < lenData:
|
2013-11-27 18:13:48 -05:00
|
|
|
b0 = byteord(data[index])
|
1999-12-16 21:34:53 +00:00
|
|
|
index = index + 1
|
2014-06-16 19:39:23 -04:00
|
|
|
handler = self.operandEncoding[b0]
|
|
|
|
value, index = handler(self, b0, data, index)
|
1999-12-16 21:34:53 +00:00
|
|
|
if value is not None:
|
|
|
|
push(value)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def pop(self):
|
|
|
|
value = self.stack[-1]
|
|
|
|
del self.stack[-1]
|
|
|
|
return value
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def popall(self):
|
2013-12-04 01:15:46 -05:00
|
|
|
args = self.stack[:]
|
1999-12-16 21:34:53 +00:00
|
|
|
del self.stack[:]
|
2013-12-04 01:15:46 -05:00
|
|
|
return args
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2014-06-16 19:39:23 -04:00
|
|
|
def handle_operator(self, operator):
|
|
|
|
operator, argType = operator
|
2017-01-12 15:23:12 -08:00
|
|
|
if isinstance(argType, tuple):
|
1999-12-16 21:34:53 +00:00
|
|
|
value = ()
|
2002-05-14 13:49:50 +00:00
|
|
|
for i in range(len(argType) - 1, -1, -1):
|
|
|
|
arg = argType[i]
|
1999-12-16 21:34:53 +00:00
|
|
|
arghandler = getattr(self, "arg_" + arg)
|
|
|
|
value = (arghandler(operator),) + value
|
|
|
|
else:
|
|
|
|
arghandler = getattr(self, "arg_" + argType)
|
|
|
|
value = arghandler(operator)
|
2017-01-12 15:23:12 -08:00
|
|
|
if operator == "blend":
|
|
|
|
self.stack.extend(value)
|
|
|
|
else:
|
|
|
|
self.dict[operator] = value
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def arg_number(self, name):
|
2017-01-12 15:23:12 -08:00
|
|
|
if isinstance(self.stack[0], list):
|
|
|
|
out = self.arg_blend_number(self.stack)
|
|
|
|
else:
|
|
|
|
out = self.pop()
|
|
|
|
return out
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def arg_blend_number(self, name):
|
|
|
|
out = []
|
|
|
|
blendArgs = self.pop()
|
|
|
|
numMasters = len(blendArgs)
|
|
|
|
out.append(blendArgs)
|
|
|
|
out.append("blend")
|
|
|
|
dummy = self.popall()
|
|
|
|
return blendArgs
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def arg_SID(self, name):
|
|
|
|
return self.strings[self.pop()]
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def arg_array(self, name):
|
|
|
|
return self.popall()
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2017-01-12 15:23:12 -08:00
|
|
|
def arg_blendList(self, name):
|
2017-11-02 11:31:47 -07:00
|
|
|
"""
|
|
|
|
There may be non-blend args at the top of the stack. We first calculate
|
|
|
|
where the blend args start in the stack. These are the last
|
2018-06-27 13:04:56 -07:00
|
|
|
numMasters*numBlends) +1 args.
|
2017-11-02 11:31:47 -07:00
|
|
|
The blend args starts with numMasters relative coordinate values, the BlueValues in the list from the default master font. This is followed by
|
|
|
|
numBlends list of values. Each of value in one of these lists is the
|
|
|
|
Variable Font delta for the matching region.
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2018-06-27 13:04:56 -07:00
|
|
|
We re-arrange this to be a list of numMaster entries. Each entry starts with the corresponding default font relative value, and is followed by
|
2017-11-02 11:31:47 -07:00
|
|
|
the delta values. We then convert the default values, the first item in each entry, to an absolute value.
|
|
|
|
"""
|
2017-01-12 15:23:12 -08:00
|
|
|
vsindex = self.dict.get("vsindex", 0)
|
2017-03-22 17:53:45 -07:00
|
|
|
numMasters = (
|
|
|
|
self.parent.getNumRegions(vsindex) + 1
|
|
|
|
) # only a PrivateDict has blended ops.
|
2017-11-02 11:31:47 -07:00
|
|
|
numBlends = self.pop()
|
|
|
|
args = self.popall()
|
2017-03-09 22:39:14 -08:00
|
|
|
numArgs = len(args)
|
2017-11-02 11:31:47 -07:00
|
|
|
# The spec says that there should be no non-blended Blue Values,.
|
|
|
|
assert numArgs == numMasters * numBlends
|
|
|
|
value = [None] * numBlends
|
2017-01-12 15:23:12 -08:00
|
|
|
numDeltas = numMasters - 1
|
|
|
|
i = 0
|
|
|
|
prevVal = 0
|
2017-11-02 11:31:47 -07:00
|
|
|
while i < numBlends:
|
2017-01-12 15:23:12 -08:00
|
|
|
newVal = args[i] + prevVal
|
|
|
|
prevVal = newVal
|
2017-11-02 11:31:47 -07:00
|
|
|
masterOffset = numBlends + (i * numDeltas)
|
|
|
|
blendList = [newVal] + args[masterOffset : masterOffset + numDeltas]
|
2017-01-12 15:23:12 -08:00
|
|
|
value[i] = blendList
|
|
|
|
i += 1
|
|
|
|
return value
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2002-05-15 07:40:33 +00:00
|
|
|
def arg_delta(self, name):
|
2017-01-12 15:23:12 -08:00
|
|
|
valueList = self.popall()
|
2002-05-15 07:40:33 +00:00
|
|
|
out = []
|
[cffLib/psCharStrings] fix IndexError with empty deltas
After 2b2aca1, DictCompiler/Decompiler's `arg_delta` method unconditionally attempts to get the first item to check if it's a list, but this fails with `IndexError` when the value is empty.
```
Traceback (most recent call last):
[...]
File "/Users/cosimolupo/Documents/Github/ufo2ft/Lib/ufo2ft/otfPostProcessor.py", line 15, in __init__
otf.save(stream)
File "/Users/cosimolupo/Documents/Github/fonttools/Lib/fontTools/ttLib/__init__.py", line 219, in save
self._writeTable(tag, writer, done)
File "/Users/cosimolupo/Documents/Github/fonttools/Lib/fontTools/ttLib/__init__.py", line 658, in _writeTable
tabledata = self.getTableData(tag)
File "/Users/cosimolupo/Documents/Github/fonttools/Lib/fontTools/ttLib/__init__.py", line 669, in getTableData
return self.tables[tag].compile(self)
File "/Users/cosimolupo/Documents/Github/fonttools/Lib/fontTools/ttLib/tables/C_F_F_.py", line 20, in compile
self.cff.compile(f, otFont)
File "/Users/cosimolupo/Documents/Github/fonttools/Lib/fontTools/cffLib.py", line 124, in compile
writer.toFile(file)
File "/Users/cosimolupo/Documents/Github/fonttools/Lib/fontTools/cffLib.py", line 300, in toFile
endPos = pos + item.getDataLength()
File "/Users/cosimolupo/Documents/Github/fonttools/Lib/fontTools/cffLib.py", line 1858, in getDataLength
return len(self.compile("getDataLength"))
File "/Users/cosimolupo/Documents/Github/fonttools/Lib/fontTools/cffLib.py", line 1879, in compile
data.append(arghandler(value))
File "/Users/cosimolupo/Documents/Github/fonttools/Lib/fontTools/cffLib.py", line 1910, in arg_delta
val0 = value[0]
IndexError: list index out of range
``
2017-03-09 14:21:38 +00:00
|
|
|
if valueList and isinstance(valueList[0], list):
|
|
|
|
# arg_blendList() has already converted these to absolute values.
|
2017-01-12 15:23:12 -08:00
|
|
|
out = valueList
|
|
|
|
else:
|
|
|
|
current = 0
|
|
|
|
for v in valueList:
|
2017-11-05 19:34:17 -08:00
|
|
|
current = current + v
|
2017-01-12 15:23:12 -08:00
|
|
|
out.append(current)
|
2002-05-15 07:40:33 +00:00
|
|
|
return out
|
1999-12-16 21:34:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def calcSubrBias(subrs):
|
|
|
|
nSubrs = len(subrs)
|
|
|
|
if nSubrs < 1240:
|
|
|
|
bias = 107
|
|
|
|
elif nSubrs < 33900:
|
|
|
|
bias = 1131
|
|
|
|
else:
|
|
|
|
bias = 32768
|
|
|
|
return bias
|