1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from fontTools.misc.testTools import FakeFont, getXML, parseXML
4from fontTools.misc.textTools import deHexStr, hexStr
5from fontTools.ttLib.tables._f_v_a_r import Axis
6from fontTools.ttLib import newTable, TTFont
7import unittest
8
9
10MVAR_DATA = deHexStr(
11    '0001 0000 '  # 0:   version=1.0
12    '0000 0008 '  # 4:   reserved=0, valueRecordSize=8
13    '0007 '       # 8:   valueRecordCount=7
14    '0044 '       # 10:  offsetToItemVariationStore=68
15    '6861 7363 '  # 12:  ValueRecord.valueTag="hasc"
16    '0000 '       # 16:  ValueRecord.deltaSetOuterIndex
17    '0003 '       # 18:  ValueRecord.deltaSetInnerIndex
18    '6863 6C61 '  # 20:  ValueRecord.valueTag="hcla"
19    '0000 '       # 24:  ValueRecord.deltaSetOuterIndex
20    '0003 '       # 26:  ValueRecord.deltaSetInnerIndex
21    '6863 6C64 '  # 28:  ValueRecord.valueTag="hcld"
22    '0000 '       # 32:  ValueRecord.deltaSetOuterIndex
23    '0003 '       # 34:  ValueRecord.deltaSetInnerIndex
24    '6864 7363 '  # 36:  ValueRecord.valueTag="hdsc"
25    '0000 '       # 40:  ValueRecord.deltaSetOuterIndex
26    '0000 '       # 42:  ValueRecord.deltaSetInnerIndex
27    '686C 6770 '  # 44:  ValueRecord.valueTag="hlgp"
28    '0000 '       # 48:  ValueRecord.deltaSetOuterIndex
29    '0002 '       # 50:  ValueRecord.deltaSetInnerIndex
30    '7362 796F '  # 52:  ValueRecord.valueTag="sbyo"
31    '0000 '       # 56:  ValueRecord.deltaSetOuterIndex
32    '0001 '       # 58:  ValueRecord.deltaSetInnerIndex
33    '7370 796F '  # 60:  ValueRecord.valueTag="spyo"
34    '0000 '       # 64:  ValueRecord.deltaSetOuterIndex
35    '0002 '       # 66:  ValueRecord.deltaSetInnerIndex
36    '0001 '       # 68:  VarStore.format=1
37    '0000 000C '  # 70:  VarStore.offsetToVariationRegionList=12
38    '0001 '       # 74:  VarStore.itemVariationDataCount=1
39    '0000 0016 '  # 76:  VarStore.itemVariationDataOffsets[0]=22
40    '0001 '       # 80:  VarRegionList.axisCount=1
41    '0001 '       # 82:  VarRegionList.regionCount=1
42    '0000 '       # 84:  variationRegions[0].regionAxes[0].startCoord=0.0
43    '4000 '       # 86:  variationRegions[0].regionAxes[0].peakCoord=1.0
44    '4000 '       # 88:  variationRegions[0].regionAxes[0].endCoord=1.0
45    '0004 '       # 90:  VarData.ItemCount=4
46    '0001 '       # 92:  VarData.NumShorts=1
47    '0001 '       # 94:  VarData.VarRegionCount=1
48    '0000 '       # 96:  VarData.VarRegionIndex[0]=0
49    'FF38 '       # 98:  VarData.deltaSets[0]=-200
50    'FFCE '       # 100: VarData.deltaSets[0]=-50
51    '0064 '       # 102: VarData.deltaSets[0]=100
52    '00C8 '       # 104: VarData.deltaSets[0]=200
53)
54
55MVAR_XML = [
56    '<Version value="0x00010000"/>',
57    '<Reserved value="0"/>',
58    '<ValueRecordSize value="8"/>',
59    '<!-- ValueRecordCount=7 -->',
60    '<VarStore Format="1">',
61    '  <Format value="1"/>',
62    '  <VarRegionList>',
63    '    <!-- RegionAxisCount=1 -->',
64    '    <!-- RegionCount=1 -->',
65    '    <Region index="0">',
66    '      <VarRegionAxis index="0">',
67    '        <StartCoord value="0.0"/>',
68    '        <PeakCoord value="1.0"/>',
69    '        <EndCoord value="1.0"/>',
70    '      </VarRegionAxis>',
71    '    </Region>',
72    '  </VarRegionList>',
73    '  <!-- VarDataCount=1 -->',
74    '  <VarData index="0">',
75    '    <!-- ItemCount=4 -->',
76    '    <NumShorts value="1"/>',
77    '    <!-- VarRegionCount=1 -->',
78    '    <VarRegionIndex index="0" value="0"/>',
79    '    <Item index="0" value="[-200]"/>',
80    '    <Item index="1" value="[-50]"/>',
81    '    <Item index="2" value="[100]"/>',
82    '    <Item index="3" value="[200]"/>',
83    '  </VarData>',
84    '</VarStore>',
85    '<ValueRecord index="0">',
86    '  <ValueTag value="hasc"/>',
87    '  <VarIdx value="3"/>',
88    '</ValueRecord>',
89    '<ValueRecord index="1">',
90    '  <ValueTag value="hcla"/>',
91    '  <VarIdx value="3"/>',
92    '</ValueRecord>',
93    '<ValueRecord index="2">',
94    '  <ValueTag value="hcld"/>',
95    '  <VarIdx value="3"/>',
96    '</ValueRecord>',
97    '<ValueRecord index="3">',
98    '  <ValueTag value="hdsc"/>',
99    '  <VarIdx value="0"/>',
100    '</ValueRecord>',
101    '<ValueRecord index="4">',
102    '  <ValueTag value="hlgp"/>',
103    '  <VarIdx value="2"/>',
104    '</ValueRecord>',
105    '<ValueRecord index="5">',
106    '  <ValueTag value="sbyo"/>',
107    '  <VarIdx value="1"/>',
108    '</ValueRecord>',
109    '<ValueRecord index="6">',
110    '  <ValueTag value="spyo"/>',
111    '  <VarIdx value="2"/>',
112    '</ValueRecord>',
113]
114
115
116class MVARTest(unittest.TestCase):
117
118    @classmethod
119    def setUpClass(cls):
120        cls.maxDiff = None
121
122    def test_decompile_toXML(self):
123        mvar = newTable('MVAR')
124        font = TTFont()
125        mvar.decompile(MVAR_DATA, font)
126        self.assertEqual(getXML(mvar.toXML), MVAR_XML)
127
128    def test_compile_fromXML(self):
129        mvar = newTable('MVAR')
130        font = TTFont()
131        for name, attrs, content in parseXML(MVAR_XML):
132            mvar.fromXML(name, attrs, content, font=font)
133        data = MVAR_DATA
134        self.assertEqual(hexStr(mvar.compile(font)), hexStr(data))
135
136
137if __name__ == '__main__':
138    import sys
139    sys.exit(unittest.main())
140