[cffLib.specializer_test] specializeProgram tests

The failing tests are due to missed optimization opportunities.
The current tests are not hitting some edge cases.
This commit is contained in:
Miguel Sousa 2017-05-07 03:14:24 -07:00
parent 2f7f36e2f4
commit 2f14200a89

View File

@ -1,5 +1,5 @@
from __future__ import print_function, division, absolute_import
from fontTools.cffLib.specializer import generalizeProgram
from fontTools.cffLib.specializer import generalizeProgram, specializeProgram
import unittest
@ -25,6 +25,10 @@ def get_generalized_charstr(charstr):
return program2charstr(generalizeProgram(charstr2program(charstr)))
def get_specialized_charstr(charstr, **kwargs):
return program2charstr(specializeProgram(charstr2program(charstr), **kwargs))
class CFFGeneralizeProgramTest(unittest.TestCase):
# rmoveto
@ -391,6 +395,299 @@ class CFFGeneralizeProgramTest(unittest.TestCase):
self.assertEqual(get_generalized_charstr(test_charstr), xpct_charstr)
class CFFSpecializeProgramTest(unittest.TestCase):
def __init__(self, methodName):
unittest.TestCase.__init__(self, methodName)
# Python 3 renamed assertRaisesRegexp to assertRaisesRegex,
# and fires deprecation warnings if a program uses the old name.
if not hasattr(self, "assertRaisesRegex"):
self.assertRaisesRegex = self.assertRaisesRegexp
# rmoveto
def test_rmoveto_origin(self):
test_charstr = 'rmoveto'
with self.assertRaisesRegex(ValueError, r'\[\]'):
get_specialized_charstr(test_charstr)
def test_rmoveto_zero(self):
test_charstr = '0 0 rmoveto'
xpct_charstr = '0 hmoveto'
self.assertEqual(get_specialized_charstr(test_charstr,
generalizeFirst=False), xpct_charstr)
def test_rmoveto_zero_mult(self):
test_charstr = '0 0 rmoveto '*3
xpct_charstr = '0 hmoveto'
self.assertEqual(get_specialized_charstr(test_charstr,
generalizeFirst=False), xpct_charstr)
def test_rmoveto_zero_width(self):
test_charstr = '100 0 0 rmoveto'
xpct_charstr = '100 0 hmoveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rmoveto(self):
test_charstr = '.55 -.8 rmoveto'
xpct_charstr = '0.55 -0.8 rmoveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rmoveto_mult(self):
test_charstr = '55 -8 rmoveto '*3
xpct_charstr = '165 -24 rmoveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rmoveto_width(self):
test_charstr = '100.5 50 -5.8 rmoveto'
xpct_charstr = test_charstr
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
# rlineto
def test_rlineto_none(self):
test_charstr = 'rlineto'
xpct_charstr = ''
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rlineto_none_mult(self):
test_charstr = 'rlineto '*3
xpct_charstr = ''
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rlineto_zero(self):
test_charstr = '0 0 rlineto'
xpct_charstr = ''
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rlineto_zero_mult(self):
test_charstr = '0 0 rlineto '*3
xpct_charstr = ''
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rlineto(self):
test_charstr = '.55 -.8 rlineto'
xpct_charstr = '0.55 -0.8 rlineto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rlineto_mult(self):
test_charstr = '.55 -.8 rlineto '*3
xpct_charstr = '0.55 -0.8 0.55 -0.8 0.55 -0.8 rlineto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hlineto(self):
test_charstr = '.67 0 rlineto'
xpct_charstr = '0.67 hlineto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hlineto_zero_mult(self):
test_charstr = '62 0 rlineto '*3
xpct_charstr = '186 hlineto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hlineto_mult(self):
test_charstr = '.67 0 rlineto 0 -6.0 rlineto .67 0 rlineto'
xpct_charstr = '0.67 -6.0 0.67 hlineto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vlineto(self):
test_charstr = '0 -.24 rlineto'
xpct_charstr = '-0.24 vlineto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vlineto_zero_mult(self):
test_charstr = '0 -24 rlineto '*3
xpct_charstr = '-72 vlineto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vlineto_mult(self):
test_charstr = '0 -.24 rlineto +50 0 rlineto 0 30 rlineto -4 0 rlineto'
xpct_charstr = '-0.24 50 30 -4 vlineto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
# rrcurveto
def test_rrcurveto(self):
test_charstr = '-1 56 -2 57 -1 57 rrcurveto'
xpct_charstr = test_charstr
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rrcurveto_mult(self):
test_charstr = '-30 8 -36 15 -37 22 rrcurveto 44 54 31 61 22 68 rrcurveto'
xpct_charstr = '-30 8 -36 15 -37 22 44 54 31 61 22 68 rrcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hhcurveto_4(self):
test_charstr = '10 0 30 0 10 0 rrcurveto'
xpct_charstr = '10 30 0 10 hhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hhcurveto_5(self):
test_charstr = '-38 40 -60 41 -91 0 rrcurveto'
xpct_charstr = '40 -38 -60 41 -91 hhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hhcurveto_mult_4_4(self):
test_charstr = '43 0 23 25 18 0 rrcurveto 29 0 56 42 -84 0 rrcurveto'
xpct_charstr = '43 23 25 18 29 56 42 -84 hhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hhcurveto_mult_5_4(self):
test_charstr = '23 43 25 18 29 0 rrcurveto 56 0 42 -84 79 0 rrcurveto'
xpct_charstr = '43 23 25 18 29 56 42 -84 79 hhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hhcurveto_mult_4_4_4(self):
test_charstr = '1 0 2 3 4 0 rrcurveto 5 0 6 7 8 0 rrcurveto 9 0 10 11 12 0 rrcurveto'
xpct_charstr = '1 2 3 4 5 6 7 8 9 10 11 12 hhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hhcurveto_mult_5_4_4(self):
test_charstr = '2 1 3 4 5 0 rrcurveto 6 0 7 8 9 0 rrcurveto 10 0 11 12 13 0 rrcurveto'
xpct_charstr = '1 2 3 4 5 6 7 8 9 10 11 12 13 hhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vvcurveto_4(self):
test_charstr = '0 61 6 52 0 68 rrcurveto'
xpct_charstr = '61 6 52 68 vvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vvcurveto_5(self):
test_charstr = '61 38 35 56 0 72 rrcurveto'
xpct_charstr = '61 38 35 56 72 vvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vvcurveto_mult_4_4(self):
test_charstr = '0 -84 -88 -30 0 -90 rrcurveto 0 -13 19 23 0 -11 rrcurveto'
xpct_charstr = '-84 -88 -30 -90 -13 19 23 -11 vvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vvcurveto_mult_5_4(self):
test_charstr = '43 12 17 32 0 65 rrcurveto 0 68 -6 52 0 61 rrcurveto'
xpct_charstr = '43 12 17 32 65 68 -6 52 61 vvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vvcurveto_mult_4_4_4(self):
test_charstr = '0 1 2 3 0 4 rrcurveto 0 5 6 7 0 8 rrcurveto 0 9 10 11 0 12 rrcurveto'
xpct_charstr = '1 2 3 4 5 6 7 8 9 10 11 12 vvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vvcurveto_mult_5_4_4(self):
test_charstr = '1 2 3 4 0 5 rrcurveto 0 6 7 8 0 9 rrcurveto 0 10 11 12 0 13 rrcurveto'
xpct_charstr = '1 2 3 4 5 6 7 8 9 10 11 12 13 vvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hvcurveto_4(self):
test_charstr = '1 0 2 3 0 4 rrcurveto'
xpct_charstr = '1 2 3 4 hvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hvcurveto_5(self):
test_charstr = '57 0 44 22 34 40 rrcurveto'
xpct_charstr = '57 44 22 40 34 hvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hvcurveto_4_4(self):
test_charstr = '65 0 33 -19 0 -45 rrcurveto 0 -45 -29 -25 -71 0 rrcurveto'
xpct_charstr = '65 33 -19 -45 -45 -29 -25 -71 hvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hvcurveto_4_5(self):
test_charstr = '97 0 69 41 0 86 rrcurveto 0 58 -36 34 -64 11 rrcurveto'
xpct_charstr = '97 69 41 86 58 -36 34 -64 11 hvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hvcurveto_4_4_4(self):
test_charstr = '1 0 2 3 0 4 rrcurveto 0 5 6 7 8 0 rrcurveto 9 0 10 11 0 12 rrcurveto'
xpct_charstr = '1 2 3 4 5 6 7 8 9 10 11 12 hvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hvcurveto_4_4_5(self):
test_charstr = '-124 0 -79 104 0 165 rrcurveto 0 163 82 102 124 0 rrcurveto 56 0 43 -25 35 -37 rrcurveto'
xpct_charstr = '-124 -79 104 165 163 82 102 124 56 43 -25 -37 35 hvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hvcurveto_4_4_4_4(self):
test_charstr = '32 0 25 22 0 32 rrcurveto 0 31 -25 22 -32 0 rrcurveto -32 0 -25 -22 0 -31 rrcurveto 0 -32 25 -22 32 0 rrcurveto'
xpct_charstr = '32 25 22 32 31 -25 22 -32 -32 -25 -22 -31 -32 25 -22 32 hvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_hvcurveto_4_4_4_4_5(self):
test_charstr = '-170 0 -128 111 0 195 rrcurveto 0 234 172 151 178 0 rrcurveto 182 0 95 -118 0 -161 rrcurveto 0 -130 -71 -77 -63 0 rrcurveto -55 0 -19 38 20 79 rrcurveto'
xpct_charstr = '-170 -128 111 195 234 172 151 178 182 95 -118 -161 -130 -71 -77 -63 -55 -19 38 79 20 hvcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vhcurveto_4(self):
test_charstr = '0 -57 43 -30 53 0 rrcurveto'
xpct_charstr = '-57 43 -30 53 vhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vhcurveto_5(self):
test_charstr = '0 41 -27 19 -46 11 rrcurveto'
xpct_charstr = '41 -27 19 -46 11 vhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vhcurveto_4_4(self):
test_charstr = '0 1 2 3 4 0 rrcurveto 5 0 6 7 0 8 rrcurveto'
xpct_charstr = '1 2 3 4 5 6 7 8 vhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vhcurveto_4_5(self):
test_charstr = '0 -64 -23 -25 -45 0 rrcurveto -30 0 -24 14 -19 33 rrcurveto'
xpct_charstr = '-64 -23 -25 -45 -30 -24 14 33 -19 vhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vhcurveto_4_4_4(self):
test_charstr = '0 1 2 3 4 0 rrcurveto 5 0 6 7 0 8 rrcurveto 0 9 10 11 12 0 rrcurveto'
xpct_charstr = '1 2 3 4 5 6 7 8 9 10 11 12 vhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vhcurveto_4_4_5(self):
test_charstr = '0 108 59 81 98 0 rrcurveto 99 0 59 -81 0 -108 rrcurveto 0 -100 -46 -66 -63 -47 rrcurveto'
xpct_charstr = '108 59 81 98 99 59 -81 -108 -100 -46 -66 -63 -47 vhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_vhcurveto_4_4_4_5(self):
test_charstr = '0 60 -26 37 -43 0 rrcurveto -33 0 -28 -22 0 -36 rrcurveto 0 -37 27 -20 32 0 rrcurveto 3 0 4 0 3 1 rrcurveto'
xpct_charstr = '60 -26 37 -43 -33 -28 -22 -36 -37 27 -20 32 3 4 0 1 3 vhcurveto'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rcurveline_6_2(self):
test_charstr = '21 -76 21 -72 24 -73 rrcurveto 31 -100 rlineto'
xpct_charstr = '21 -76 21 -72 24 -73 31 -100 rcurveline'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rcurveline_6_6_2(self):
test_charstr = '-73 80 -80 121 -49 96 rrcurveto 60 65 55 41 54 17 rrcurveto -8 78 rlineto'
xpct_charstr = '-73 80 -80 121 -49 96 60 65 55 41 54 17 -8 78 rcurveline'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rcurveline_6_6_6_2(self):
test_charstr = '1 64 10 51 29 39 rrcurveto 15 21 15 20 15 18 rrcurveto 47 -89 63 -98 52 -59 rrcurveto 91 8 rlineto'
xpct_charstr = '1 64 10 51 29 39 15 21 15 20 15 18 47 -89 63 -98 52 -59 91 8 rcurveline'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rlinecurve_2_6(self):
test_charstr = '21 -76 rlineto 21 -72 24 -73 31 -100 rrcurveto'
xpct_charstr = '21 -76 21 -72 24 -73 31 -100 rlinecurve'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rlinecurve_2_2_6(self):
test_charstr = '-73 80 rlineto -80 121 rlineto -49 96 60 65 55 41 rrcurveto'
xpct_charstr = '-73 80 -80 121 -49 96 60 65 55 41 rlinecurve'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
def test_rlinecurve_2_2_2_6(self):
test_charstr = '1 64 rlineto 10 51 rlineto 29 39 rlineto 15 21 15 20 15 18 rrcurveto'
xpct_charstr = '1 64 10 51 29 39 15 21 15 20 15 18 rlinecurve'
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
# maxstack CFF=48
def test_maxstack(self):
operands = '1 2 3 4 5 6 '
operator = 'rrcurveto '
test_charstr = (operands + operator)*9
xpct_charstr = (operands + operator + operands*8 + operator).rstrip()
self.assertEqual(get_specialized_charstr(test_charstr), xpct_charstr)
if __name__ == "__main__":
import sys
sys.exit(unittest.main())