• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from fontTools.cffLib import PrivateDict
2from fontTools.cffLib.specializer import stringToProgram
3from fontTools.misc.testTools import getXML, parseXML
4from fontTools.misc.psCharStrings import (
5    T2CharString,
6    encodeFloat,
7    encodeFixed,
8    read_fixed1616,
9    read_realNumber,
10)
11import unittest
12
13
14def hexenc(s):
15    return ' '.join('%02x' % x for x in s)
16
17
18class T2CharStringTest(unittest.TestCase):
19
20    @classmethod
21    def stringToT2CharString(cls, string):
22        return T2CharString(program=stringToProgram(string), private=PrivateDict())
23
24    def test_calcBounds_empty(self):
25        cs = self.stringToT2CharString("endchar")
26        bounds = cs.calcBounds(None)
27        self.assertEqual(bounds, None)
28
29    def test_calcBounds_line(self):
30        cs = self.stringToT2CharString("100 100 rmoveto 40 10 rlineto -20 50 rlineto endchar")
31        bounds = cs.calcBounds(None)
32        self.assertEqual(bounds, (100, 100, 140, 160))
33
34    def test_calcBounds_curve(self):
35        cs = self.stringToT2CharString("100 100 rmoveto -50 -150 200 0 -50 150 rrcurveto endchar")
36        bounds = cs.calcBounds(None)
37        self.assertEqual(bounds, (91.90524980688875, -12.5, 208.09475019311125, 100))
38
39    def test_charstring_bytecode_optimization(self):
40        cs = self.stringToT2CharString(
41            "100.0 100 rmoveto -50.0 -150 200.5 0.0 -50 150 rrcurveto endchar")
42        cs.isCFF2 = False
43        cs.private._isCFF2 = False
44        cs.compile()
45        cs.decompile()
46        self.assertEqual(
47            cs.program, [100, 100, 'rmoveto', -50, -150, 200.5, 0, -50, 150,
48                         'rrcurveto', 'endchar'])
49
50        cs2 = self.stringToT2CharString(
51            "100.0 rmoveto -50.0 -150 200.5 0.0 -50 150 rrcurveto")
52        cs2.isCFF2 = True
53        cs2.private._isCFF2 = True
54        cs2.compile(isCFF2=True)
55        cs2.decompile()
56        self.assertEqual(
57            cs2.program, [100, 'rmoveto', -50, -150, 200.5, 0, -50, 150,
58                          'rrcurveto'])
59
60    def test_encodeFloat(self):
61        testNums = [
62            # value                expected result
63            (-9.399999999999999,   '1e e9 a4 ff'),  # -9.4
64            (9.399999999999999999, '1e 9a 4f'),  # 9.4
65            (456.8,                '1e 45 6a 8f'),  # 456.8
66            (0.0,                  '1e 0f'),  # 0
67            (-0.0,                 '1e 0f'),  # 0
68            (1.0,                  '1e 1f'),  # 1
69            (-1.0,                 '1e e1 ff'),  # -1
70            (98765.37e2,           '1e 98 76 53 7f'),  # 9876537
71            (1234567890.0,         '1e 1a 23 45 67 9b 09 ff'),  # 1234567890
72            (9.876537e-4,          '1e a0 00 98 76 53 7f'),  # 9.876537e-24
73            (9.876537e+4,          '1e 98 76 5a 37 ff'),  # 9.876537e+24
74        ]
75
76        for sample in testNums:
77            encoded_result = encodeFloat(sample[0])
78
79            # check to see if we got the expected bytes
80            self.assertEqual(hexenc(encoded_result), sample[1])
81
82            # check to see if we get the same value by decoding the data
83            decoded_result = read_realNumber(
84                None,
85                None,
86                encoded_result,
87                1,
88            )
89            self.assertEqual(decoded_result[0], float('%.8g' % sample[0]))
90            # We limit to 8 digits of precision to match the implementation
91            # of encodeFloat.
92
93    def test_encode_decode_fixed(self):
94        testNums = [
95            # value                expected hex      expected float
96            (-9.399999999999999,   'ff ff f6 99 9a', -9.3999939),
97            (-9.4,                 'ff ff f6 99 9a', -9.3999939),
98            (9.399999999999999999, 'ff 00 09 66 66', 9.3999939),
99            (9.4,                  'ff 00 09 66 66', 9.3999939),
100            (456.8,                'ff 01 c8 cc cd', 456.8000031),
101            (-456.8,               'ff fe 37 33 33', -456.8000031),
102        ]
103
104        for (value, expected_hex, expected_float) in testNums:
105            encoded_result = encodeFixed(value)
106
107            # check to see if we got the expected bytes
108            self.assertEqual(hexenc(encoded_result), expected_hex)
109
110            # check to see if we get the same value by decoding the data
111            decoded_result = read_fixed1616(
112                None,
113                None,
114                encoded_result,
115                1,
116            )
117            self.assertAlmostEqual(decoded_result[0], expected_float)
118
119    def test_toXML(self):
120        program = [
121            '107 53.4004 166.199 hstem',
122            '174.6 163.801 vstem',
123            '338.4 142.8 rmoveto',
124            '28 0 21.9 9 15.8 18 15.8 18 7.9 20.79959 0 23.6 rrcurveto',
125            'endchar'
126        ]
127        cs = self.stringToT2CharString(" ".join(program))
128
129        self.assertEqual(getXML(cs.toXML), program)
130
131    def test_fromXML(self):
132        cs = T2CharString()
133        for name, attrs, content in parseXML(
134            [
135                '<CharString name="period">'
136                '  338.4 142.8 rmoveto',
137                '  28 0 21.9 9 15.8 18 15.8 18 7.9 20.79959 0 23.6 rrcurveto',
138                '  endchar'
139                '</CharString>'
140            ]
141        ):
142            cs.fromXML(name, attrs, content)
143
144        expected_program = [
145            338.3999939, 142.8000031, 'rmoveto',
146            28, 0, 21.8999939, 9, 15.8000031,
147            18, 15.8000031, 18, 7.8999939,
148            20.7995911, 0, 23.6000061, 'rrcurveto',
149            'endchar'
150        ]
151
152        self.assertEqual(len(cs.program), len(expected_program))
153        for arg, expected_arg in zip(cs.program, expected_program):
154            if isinstance(arg, str):
155                self.assertIsInstance(expected_arg, str)
156                self.assertEqual(arg, expected_arg)
157            else:
158                self.assertNotIsInstance(expected_arg, str)
159                self.assertAlmostEqual(arg, expected_arg)
160
161
162if __name__ == "__main__":
163    import sys
164    sys.exit(unittest.main())
165