• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import \
2    print_function, division, absolute_import, unicode_literals
3from fontTools.misc.py23 import *
4from fontTools.misc.testTools import getXML, parseXML
5from fontTools.misc.textTools import deHexStr, hexStr
6from fontTools.ttLib import TTLibError, getTableModule, newTable
7from fontTools.ttLib.tables.TupleVariation import TupleVariation
8
9import unittest
10
11
12CVAR_DATA = deHexStr(
13    "0001 0000 "      #  0: majorVersion=1 minorVersion=0
14    "8002 0018 "      #  4: tupleVariationCount=2|TUPLES_SHARE_POINT_NUMBERS offsetToData=24
15    "0004 "           #  8: tvHeader[0].variationDataSize=4
16    "8000 "           # 10: tvHeader[0].tupleIndex=EMBEDDED_PEAK
17    "4000 0000 "      # 12: tvHeader[0].peakTuple=[1.0, 0.0]
18    "0004 "           # 16: tvHeader[1].variationDataSize=4
19    "8000 "           # 18: tvHeader[1].tupleIndex=EMBEDDED_PEAK
20    "C000 3333 "      # 20: tvHeader[1].peakTuple=[-1.0, 0.8]
21    "03 02 02 01 01"  # 24: shared_pointCount=03, run_count=2 cvt=[2, 3, 4]
22    "02 03 01 04 "    # 25: deltas=[3, 1, 4]
23    "02 09 07 08")    # 29: deltas=[9, 7, 8]
24
25CVAR_PRIVATE_POINT_DATA = deHexStr(
26    "0001 0000 "                    #  0: majorVersion=1 minorVersion=0
27    "0002 0018 "                    #  4: tupleVariationCount=2 offsetToData=24
28    "0009 "                         #  8: tvHeader[0].variationDataSize=9
29    "A000 "                         # 10: tvHeader[0].tupleIndex=EMBEDDED_PEAK|PRIVATE_POINT_NUMBERS
30    "4000 0000 "                    # 12: tvHeader[0].peakTuple=[1.0, 0.0]
31    "0009 "                         # 16: tvHeader[1].variationDataSize=9
32    "A000 "                         # 18: tvHeader[1].tupleIndex=EMBEDDED_PEAK|PRIVATE_POINT_NUMBERS
33    "C000 3333 "                    # 20: tvHeader[1].peakTuple=[-1.0, 0.8]
34    "03 02 02 01 01 02 03 01 04 "   # 24: pointCount=3 run_count=2 cvt=2 1 1 run_count=2 deltas=[3, 1, 4]
35    "03 02 02 01 01 02 09 07 08 ")  # 33: pointCount=3 run_count=2 cvt=2 1 1 run_count=2 deltas=[9, 7, 8]
36
37CVAR_XML = [
38    '<version major="1" minor="0"/>',
39    '<tuple>',
40    '  <coord axis="wght" value="1.0"/>',
41    '  <delta cvt="2" value="3"/>',
42    '  <delta cvt="3" value="1"/>',
43    '  <delta cvt="4" value="4"/>',
44    '</tuple>',
45    '<tuple>',
46    '  <coord axis="wght" value="-1.0"/>',
47    '  <coord axis="wdth" value="0.8"/>',
48    '  <delta cvt="2" value="9"/>',
49    '  <delta cvt="3" value="7"/>',
50    '  <delta cvt="4" value="8"/>',
51    '</tuple>',
52]
53
54CVAR_VARIATIONS = [
55    TupleVariation({"wght": (0.0, 1.0, 1.0)}, [None, None, 3, 1, 4]),
56    TupleVariation({"wght": (-1, -1.0, 0.0), "wdth": (0.0, 0.8, 0.8)},
57                   [None, None, 9, 7, 8]),
58]
59
60
61class CVARTableTest(unittest.TestCase):
62    def makeFont(self):
63        cvt, cvar, fvar = newTable("cvt "), newTable("cvar"), newTable("fvar")
64        font = {"cvt ": cvt, "cvar": cvar, "fvar": fvar}
65        cvt.values = [0, 0, 0, 1000, -2000]
66        Axis = getTableModule("fvar").Axis
67        fvar.axes = [Axis(), Axis()]
68        fvar.axes[0].axisTag, fvar.axes[1].axisTag = "wght", "wdth"
69        return font, cvar
70
71    def test_compile(self):
72        font, cvar = self.makeFont()
73        cvar.variations = CVAR_VARIATIONS
74        self.assertEqual(hexStr(cvar.compile(font)), hexStr(CVAR_PRIVATE_POINT_DATA))
75
76    def test_compile_shared_points(self):
77        font, cvar = self.makeFont()
78        cvar.variations = CVAR_VARIATIONS
79        self.assertEqual(hexStr(cvar.compile(font, useSharedPoints=True)), hexStr(CVAR_DATA))
80
81    def test_decompile(self):
82        font, cvar = self.makeFont()
83        cvar.decompile(CVAR_PRIVATE_POINT_DATA, font)
84        self.assertEqual(cvar.majorVersion, 1)
85        self.assertEqual(cvar.minorVersion, 0)
86        self.assertEqual(cvar.variations, CVAR_VARIATIONS)
87
88    def test_decompile_shared_points(self):
89        font, cvar = self.makeFont()
90        cvar.decompile(CVAR_DATA, font)
91        self.assertEqual(cvar.majorVersion, 1)
92        self.assertEqual(cvar.minorVersion, 0)
93        self.assertEqual(cvar.variations, CVAR_VARIATIONS)
94
95    def test_fromXML(self):
96        font, cvar = self.makeFont()
97        for name, attrs, content in parseXML(CVAR_XML):
98            cvar.fromXML(name, attrs, content, ttFont=font)
99        self.assertEqual(cvar.majorVersion, 1)
100        self.assertEqual(cvar.minorVersion, 0)
101        self.assertEqual(cvar.variations, CVAR_VARIATIONS)
102
103    def test_toXML(self):
104        font, cvar = self.makeFont()
105        cvar.variations = CVAR_VARIATIONS
106        self.assertEqual(getXML(cvar.toXML, font), CVAR_XML)
107
108
109if __name__ == "__main__":
110    import sys
111    sys.exit(unittest.main())
112