1from __future__ import print_function, division, absolute_import, unicode_literals 2from fontTools.misc.py23 import * 3from fontTools.misc.testTools import parseXML 4from fontTools.misc.textTools import deHexStr 5from fontTools.misc.xmlWriter import XMLWriter 6from fontTools.ttLib import TTLibError 7from fontTools.ttLib.tables._a_v_a_r import table__a_v_a_r 8from fontTools.ttLib.tables._f_v_a_r import table__f_v_a_r, Axis 9import collections 10import logging 11import unittest 12 13 14TEST_DATA = deHexStr( 15 "00 01 00 00 00 00 00 02 " 16 "00 04 C0 00 C0 00 00 00 00 00 13 33 33 33 40 00 40 00 " 17 "00 03 C0 00 C0 00 00 00 00 00 40 00 40 00") 18 19 20class AxisVariationTableTest(unittest.TestCase): 21 def test_compile(self): 22 avar = table__a_v_a_r() 23 avar.segments["wdth"] = {-1.0: -1.0, 0.0: 0.0, 0.3: 0.8, 1.0: 1.0} 24 avar.segments["wght"] = {-1.0: -1.0, 0.0: 0.0, 1.0: 1.0} 25 self.assertEqual(TEST_DATA, avar.compile(self.makeFont(["wdth", "wght"]))) 26 27 def test_decompile(self): 28 avar = table__a_v_a_r() 29 avar.decompile(TEST_DATA, self.makeFont(["wdth", "wght"])) 30 self.assertEqual({ 31 "wdth": {-1.0: -1.0, 0.0: 0.0, 0.3: 0.8, 1.0: 1.0}, 32 "wght": {-1.0: -1.0, 0.0: 0.0, 1.0: 1.0} 33 }, avar.segments) 34 35 def test_decompile_unsupportedVersion(self): 36 avar = table__a_v_a_r() 37 font = self.makeFont(["wdth", "wght"]) 38 self.assertRaises(TTLibError, avar.decompile, deHexStr("02 01 03 06 00 00 00 00"), font) 39 40 def test_toXML(self): 41 avar = table__a_v_a_r() 42 avar.segments["opsz"] = {-1.0: -1.0, 0.0: 0.0, 0.3: 0.8, 1.0: 1.0} 43 writer = XMLWriter(BytesIO()) 44 avar.toXML(writer, self.makeFont(["opsz"])) 45 self.assertEqual([ 46 '<segment axis="opsz">', 47 '<mapping from="-1.0" to="-1.0"/>', 48 '<mapping from="0.0" to="0.0"/>', 49 '<mapping from="0.3" to="0.8"/>', 50 '<mapping from="1.0" to="1.0"/>', 51 '</segment>' 52 ], self.xml_lines(writer)) 53 54 def test_fromXML(self): 55 avar = table__a_v_a_r() 56 for name, attrs, content in parseXML( 57 '<segment axis="wdth">' 58 ' <mapping from="-1.0" to="-1.0"/>' 59 ' <mapping from="0.0" to="0.0"/>' 60 ' <mapping from="0.7" to="0.2"/>' 61 ' <mapping from="1.0" to="1.0"/>' 62 '</segment>'): 63 avar.fromXML(name, attrs, content, ttFont=None) 64 self.assertEqual({"wdth": {-1: -1, 0: 0, 0.7: 0.2, 1.0: 1.0}}, 65 avar.segments) 66 67 @staticmethod 68 def makeFont(axisTags): 69 """['opsz', 'wdth'] --> ttFont""" 70 fvar = table__f_v_a_r() 71 for tag in axisTags: 72 axis = Axis() 73 axis.axisTag = tag 74 fvar.axes.append(axis) 75 return {"fvar": fvar} 76 77 @staticmethod 78 def xml_lines(writer): 79 content = writer.file.getvalue().decode("utf-8") 80 return [line.strip() for line in content.splitlines()][1:] 81 82 83if __name__ == "__main__": 84 import sys 85 sys.exit(unittest.main()) 86