• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from fontTools.misc.testTools import parseXML
2from fontTools.misc.textTools import deHexStr
3from fontTools.misc.xmlWriter import XMLWriter
4from fontTools.ttLib import TTLibError
5from fontTools.ttLib.tables._a_v_a_r import table__a_v_a_r
6from fontTools.ttLib.tables._f_v_a_r import table__f_v_a_r, Axis
7from io import BytesIO
8import unittest
9
10
11TEST_DATA = deHexStr(
12    "00 01 00 00 00 00 00 02 "
13    "00 04 C0 00 C0 00 00 00 00 00 13 33 33 33 40 00 40 00 "
14    "00 03 C0 00 C0 00 00 00 00 00 40 00 40 00")
15
16
17class AxisVariationTableTest(unittest.TestCase):
18    def assertAvarAlmostEqual(self, segments1, segments2):
19        self.assertSetEqual(set(segments1.keys()), set(segments2.keys()))
20        for axisTag, mapping1 in segments1.items():
21            mapping2 = segments2[axisTag]
22            self.assertEqual(len(mapping1), len(mapping2))
23            for (k1, v1), (k2, v2) in zip(
24                sorted(mapping1.items()), sorted(mapping2.items())
25            ):
26                self.assertAlmostEqual(k1, k2)
27                self.assertAlmostEqual(v1, v2)
28
29    def test_compile(self):
30        avar = table__a_v_a_r()
31        avar.segments["wdth"] = {-1.0: -1.0, 0.0: 0.0, 0.3: 0.8, 1.0: 1.0}
32        avar.segments["wght"] = {-1.0: -1.0, 0.0: 0.0, 1.0: 1.0}
33        self.assertEqual(TEST_DATA, avar.compile(self.makeFont(["wdth", "wght"])))
34
35    def test_decompile(self):
36        avar = table__a_v_a_r()
37        avar.decompile(TEST_DATA, self.makeFont(["wdth", "wght"]))
38        self.assertAvarAlmostEqual({
39            "wdth": {-1.0: -1.0, 0.0: 0.0, 0.2999878: 0.7999878, 1.0: 1.0},
40            "wght": {-1.0: -1.0, 0.0: 0.0, 1.0: 1.0}
41        }, avar.segments)
42
43    def test_decompile_unsupportedVersion(self):
44        avar = table__a_v_a_r()
45        font = self.makeFont(["wdth", "wght"])
46        self.assertRaises(TTLibError, avar.decompile, deHexStr("02 01 03 06 00 00 00 00"), font)
47
48    def test_toXML(self):
49        avar = table__a_v_a_r()
50        avar.segments["opsz"] = {-1.0: -1.0, 0.0: 0.0, 0.2999878: 0.7999878, 1.0: 1.0}
51        writer = XMLWriter(BytesIO())
52        avar.toXML(writer, self.makeFont(["opsz"]))
53        self.assertEqual([
54            '<segment axis="opsz">',
55                '<mapping from="-1.0" to="-1.0"/>',
56                '<mapping from="0.0" to="0.0"/>',
57                '<mapping from="0.3" to="0.8"/>',
58                '<mapping from="1.0" to="1.0"/>',
59            '</segment>'
60        ], self.xml_lines(writer))
61
62    def test_fromXML(self):
63        avar = table__a_v_a_r()
64        for name, attrs, content in parseXML(
65                '<segment axis="wdth">'
66                '    <mapping from="-1.0" to="-1.0"/>'
67                '    <mapping from="0.0" to="0.0"/>'
68                '    <mapping from="0.7" to="0.2"/>'
69                '    <mapping from="1.0" to="1.0"/>'
70                '</segment>'):
71            avar.fromXML(name, attrs, content, ttFont=None)
72        self.assertAvarAlmostEqual(
73            {"wdth": {-1: -1, 0: 0, 0.7000122: 0.2000122, 1.0: 1.0}},
74            avar.segments
75        )
76
77    @staticmethod
78    def makeFont(axisTags):
79        """['opsz', 'wdth'] --> ttFont"""
80        fvar = table__f_v_a_r()
81        for tag in axisTags:
82            axis = Axis()
83            axis.axisTag = tag
84            fvar.axes.append(axis)
85        return {"fvar": fvar}
86
87    @staticmethod
88    def xml_lines(writer):
89        content = writer.file.getvalue().decode("utf-8")
90        return [line.strip() for line in content.splitlines()][1:]
91
92
93if __name__ == "__main__":
94    import sys
95    sys.exit(unittest.main())
96