1 from __future__ import print_function, division, absolute_import, unicode_literals 2 from fontTools.misc.py23 import * 3 from fontTools.misc.testTools import parseXML 4 from fontTools.misc.textTools import deHexStr 5 from fontTools.misc.xmlWriter import XMLWriter 6 from fontTools.ttLib import TTLibError 7 from fontTools.ttLib.tables._m_e_t_a import table__m_e_t_a 8 import unittest 9 10 11 # From a real font on MacOS X, but substituted 'bild' tag by 'TEST', 12 # and shortened the payload. 13 META_DATA = deHexStr( 14 "00 00 00 01 00 00 00 00 00 00 00 1C 00 00 00 01 " 15 "54 45 53 54 00 00 00 1C 00 00 00 04 CA FE BE EF") 16 17 # The 'dlng' and 'slng' tag with text data containing "augmented" BCP 47 18 # comma-separated or comma-space-separated tags. These should be UTF-8 encoded 19 # text. 20 META_DATA_TEXT = deHexStr( 21 "00 00 00 01 00 00 00 00 00 00 00 28 00 00 00 02 " 22 "64 6C 6E 67 00 00 00 28 00 00 00 0E 73 6C 6E 67 " 23 "00 00 00 36 00 00 00 0E 4C 61 74 6E 2C 47 72 65 " 24 "6B 2C 43 79 72 6C 4C 61 74 6E 2C 47 72 65 6B 2C " 25 "43 79 72 6C") 26 27 class MetaTableTest(unittest.TestCase): 28 def test_decompile(self): 29 table = table__m_e_t_a() 30 table.decompile(META_DATA, ttFont={"meta": table}) 31 self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data) 32 33 def test_compile(self): 34 table = table__m_e_t_a() 35 table.data["TEST"] = b"\xCA\xFE\xBE\xEF" 36 self.assertEqual(META_DATA, table.compile(ttFont={"meta": table})) 37 38 def test_decompile_text(self): 39 table = table__m_e_t_a() 40 table.decompile(META_DATA_TEXT, ttFont={"meta": table}) 41 self.assertEqual({"dlng": u"Latn,Grek,Cyrl", 42 "slng": u"Latn,Grek,Cyrl"}, table.data) 43 44 def test_compile_text(self): 45 table = table__m_e_t_a() 46 table.data["dlng"] = u"Latn,Grek,Cyrl" 47 table.data["slng"] = u"Latn,Grek,Cyrl" 48 self.assertEqual(META_DATA_TEXT, table.compile(ttFont={"meta": table})) 49 50 def test_toXML(self): 51 table = table__m_e_t_a() 52 table.data["TEST"] = b"\xCA\xFE\xBE\xEF" 53 writer = XMLWriter(BytesIO()) 54 table.toXML(writer, {"meta": table}) 55 xml = writer.file.getvalue().decode("utf-8") 56 self.assertEqual([ 57 '<hexdata tag="TEST">', 58 'cafebeef', 59 '</hexdata>' 60 ], [line.strip() for line in xml.splitlines()][1:]) 61 62 def test_fromXML(self): 63 table = table__m_e_t_a() 64 for name, attrs, content in parseXML( 65 '<hexdata tag="TEST">' 66 ' cafebeef' 67 '</hexdata>'): 68 table.fromXML(name, attrs, content, ttFont=None) 69 self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data) 70 71 def test_toXML_text(self): 72 table = table__m_e_t_a() 73 table.data["dlng"] = u"Latn,Grek,Cyrl" 74 writer = XMLWriter(BytesIO()) 75 table.toXML(writer, {"meta": table}) 76 xml = writer.file.getvalue().decode("utf-8") 77 self.assertEqual([ 78 '<text tag="dlng">', 79 'Latn,Grek,Cyrl', 80 '</text>' 81 ], [line.strip() for line in xml.splitlines()][1:]) 82 83 def test_fromXML_text(self): 84 table = table__m_e_t_a() 85 for name, attrs, content in parseXML( 86 '<text tag="dlng">' 87 ' Latn,Grek,Cyrl' 88 '</text>'): 89 table.fromXML(name, attrs, content, ttFont=None) 90 self.assertEqual({"dlng": u"Latn,Grek,Cyrl"}, table.data) 91 92 93 if __name__ == "__main__": 94 import sys 95 sys.exit(unittest.main()) 96