[specializer] Reuse list len()'s

This commit is contained in:
Behdad Esfahbod 2024-11-12 20:09:51 -07:00
parent 4e2968462a
commit 751d1383af

View File

@ -80,8 +80,9 @@ def programToCommands(program, getNumRegions=None):
numBlendArgs = numBlends * numSourceFonts + 1 numBlendArgs = numBlends * numSourceFonts + 1
# replace first blend op by a list of the blend ops. # replace first blend op by a list of the blend ops.
stack[-numBlendArgs:] = [stack[-numBlendArgs:]] stack[-numBlendArgs:] = [stack[-numBlendArgs:]]
lenBlendStack += numBlends + len(stack) - 1 lenStack = len(stack)
lastBlendIndex = len(stack) lenBlendStack += numBlends + lenStack - 1
lastBlendIndex = lenStack
# if a blend op exists, this is or will be a CFF2 charstring. # if a blend op exists, this is or will be a CFF2 charstring.
continue continue
@ -153,9 +154,10 @@ def commandsToProgram(commands):
def _everyN(el, n): def _everyN(el, n):
"""Group the list el into groups of size n""" """Group the list el into groups of size n"""
if len(el) % n != 0: l = len(el)
if l % n != 0:
raise ValueError(el) raise ValueError(el)
for i in range(0, len(el), n): for i in range(0, l, n):
yield el[i : i + n] yield el[i : i + n]
@ -218,9 +220,10 @@ class _GeneralizerDecombinerCommandsMap(object):
@staticmethod @staticmethod
def hhcurveto(args): def hhcurveto(args):
if len(args) < 4 or len(args) % 4 > 1: l = len(args)
if l < 4 or l % 4 > 1:
raise ValueError(args) raise ValueError(args)
if len(args) % 2 == 1: if l % 2 == 1:
yield ("rrcurveto", [args[1], args[0], args[2], args[3], args[4], 0]) yield ("rrcurveto", [args[1], args[0], args[2], args[3], args[4], 0])
args = args[5:] args = args[5:]
for args in _everyN(args, 4): for args in _everyN(args, 4):
@ -228,9 +231,10 @@ class _GeneralizerDecombinerCommandsMap(object):
@staticmethod @staticmethod
def vvcurveto(args): def vvcurveto(args):
if len(args) < 4 or len(args) % 4 > 1: l = len(args)
if l < 4 or l % 4 > 1:
raise ValueError(args) raise ValueError(args)
if len(args) % 2 == 1: if l % 2 == 1:
yield ("rrcurveto", [args[0], args[1], args[2], args[3], 0, args[4]]) yield ("rrcurveto", [args[0], args[1], args[2], args[3], 0, args[4]])
args = args[5:] args = args[5:]
for args in _everyN(args, 4): for args in _everyN(args, 4):
@ -238,11 +242,12 @@ class _GeneralizerDecombinerCommandsMap(object):
@staticmethod @staticmethod
def hvcurveto(args): def hvcurveto(args):
if len(args) < 4 or len(args) % 8 not in {0, 1, 4, 5}: l = len(args)
if l < 4 or l % 8 not in {0, 1, 4, 5}:
raise ValueError(args) raise ValueError(args)
last_args = None last_args = None
if len(args) % 2 == 1: if l % 2 == 1:
lastStraight = len(args) % 8 == 5 lastStraight = l % 8 == 5
args, last_args = args[:-5], args[-5:] args, last_args = args[:-5], args[-5:]
it = _everyN(args, 4) it = _everyN(args, 4)
try: try:
@ -262,11 +267,12 @@ class _GeneralizerDecombinerCommandsMap(object):
@staticmethod @staticmethod
def vhcurveto(args): def vhcurveto(args):
if len(args) < 4 or len(args) % 8 not in {0, 1, 4, 5}: l = len(args)
if l < 4 or l % 8 not in {0, 1, 4, 5}:
raise ValueError(args) raise ValueError(args)
last_args = None last_args = None
if len(args) % 2 == 1: if l % 2 == 1:
lastStraight = len(args) % 8 == 5 lastStraight = l % 8 == 5
args, last_args = args[:-5], args[-5:] args, last_args = args[:-5], args[-5:]
it = _everyN(args, 4) it = _everyN(args, 4)
try: try:
@ -286,7 +292,8 @@ class _GeneralizerDecombinerCommandsMap(object):
@staticmethod @staticmethod
def rcurveline(args): def rcurveline(args):
if len(args) < 8 or len(args) % 6 != 2: l = len(args)
if l < 8 or l % 6 != 2:
raise ValueError(args) raise ValueError(args)
args, last_args = args[:-2], args[-2:] args, last_args = args[:-2], args[-2:]
for args in _everyN(args, 6): for args in _everyN(args, 6):
@ -295,7 +302,8 @@ class _GeneralizerDecombinerCommandsMap(object):
@staticmethod @staticmethod
def rlinecurve(args): def rlinecurve(args):
if len(args) < 8 or len(args) % 2 != 0: l = len(args)
if l < 8 or l % 2 != 0:
raise ValueError(args) raise ValueError(args)
args, last_args = args[:-6], args[-6:] args, last_args = args[:-6], args[-6:]
for args in _everyN(args, 2): for args in _everyN(args, 2):
@ -330,8 +338,9 @@ def _convertBlendOpToArgs(blendList):
# comprehension. See calling context # comprehension. See calling context
args = args[:-1] args = args[:-1]
numRegions = len(args) // numBlends - 1 l = len(args)
if not (numBlends * (numRegions + 1) == len(args)): numRegions = l // numBlends - 1
if not (numBlends * (numRegions + 1) == l):
raise ValueError(blendList) raise ValueError(blendList)
defaultArgs = [[arg] for arg in args[:numBlends]] defaultArgs = [[arg] for arg in args[:numBlends]]
@ -726,9 +735,10 @@ def specializeCommands(
if op1 == op2: if op1 == op2:
new_op = op1 new_op = op1
else: else:
if op2 == "rrcurveto" and len(args2) == 6: l = len(args2)
if op2 == "rrcurveto" and l == 6:
new_op = "rlinecurve" new_op = "rlinecurve"
elif len(args2) == 2: elif l == 2:
new_op = "rcurveline" new_op = "rcurveline"
elif (op1, op2) in {("rlineto", "rlinecurve"), ("rrcurveto", "rcurveline")}: elif (op1, op2) in {("rlineto", "rlinecurve"), ("rrcurveto", "rcurveline")}:
@ -783,9 +793,11 @@ def specializeCommands(
continue continue
if op[2:] == "curveto" and op[:2] not in {"rr", "hh", "vv", "vh", "hv"}: if op[2:] == "curveto" and op[:2] not in {"rr", "hh", "vv", "vh", "hv"}:
l = len(args)
op0, op1 = op[:2] op0, op1 = op[:2]
if (op0 == "r") ^ (op1 == "r"): if (op0 == "r") ^ (op1 == "r"):
assert len(args) % 2 == 1 assert l % 2 == 1
if op0 == "0": if op0 == "0":
op0 = "h" op0 = "h"
if op1 == "0": if op1 == "0":
@ -796,9 +808,9 @@ def specializeCommands(
op1 = _negateCategory(op0) op1 = _negateCategory(op0)
assert {op0, op1} <= {"h", "v"}, (op0, op1) assert {op0, op1} <= {"h", "v"}, (op0, op1)
if len(args) % 2: if l % 2:
if op0 != op1: # vhcurveto / hvcurveto if op0 != op1: # vhcurveto / hvcurveto
if (op0 == "h") ^ (len(args) % 8 == 1): if (op0 == "h") ^ (l % 8 == 1):
# Swap last two args order # Swap last two args order
args = args[:-2] + args[-1:] + args[-2:-1] args = args[:-2] + args[-1:] + args[-2:-1]
else: # hhcurveto / vvcurveto else: # hhcurveto / vvcurveto