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._m_e_t_a import table__m_e_t_a 6from io import BytesIO 7import unittest 8 9 10# From a real font on MacOS X, but substituted 'bild' tag by 'TEST', 11# and shortened the payload. 12META_DATA = deHexStr( 13 "00 00 00 01 00 00 00 00 00 00 00 1C 00 00 00 01 " 14 "54 45 53 54 00 00 00 1C 00 00 00 04 CA FE BE EF" 15) 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. 20META_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 28 29class MetaTableTest(unittest.TestCase): 30 def test_decompile(self): 31 table = table__m_e_t_a() 32 table.decompile(META_DATA, ttFont={"meta": table}) 33 self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data) 34 35 def test_compile(self): 36 table = table__m_e_t_a() 37 table.data["TEST"] = b"\xCA\xFE\xBE\xEF" 38 self.assertEqual(META_DATA, table.compile(ttFont={"meta": table})) 39 40 def test_decompile_text(self): 41 table = table__m_e_t_a() 42 table.decompile(META_DATA_TEXT, ttFont={"meta": table}) 43 self.assertEqual( 44 {"dlng": "Latn,Grek,Cyrl", "slng": "Latn,Grek,Cyrl"}, table.data 45 ) 46 47 def test_compile_text(self): 48 table = table__m_e_t_a() 49 table.data["dlng"] = "Latn,Grek,Cyrl" 50 table.data["slng"] = "Latn,Grek,Cyrl" 51 self.assertEqual(META_DATA_TEXT, table.compile(ttFont={"meta": table})) 52 53 def test_toXML(self): 54 table = table__m_e_t_a() 55 table.data["TEST"] = b"\xCA\xFE\xBE\xEF" 56 writer = XMLWriter(BytesIO()) 57 table.toXML(writer, {"meta": table}) 58 xml = writer.file.getvalue().decode("utf-8") 59 self.assertEqual( 60 ['<hexdata tag="TEST">', "cafebeef", "</hexdata>"], 61 [line.strip() for line in xml.splitlines()][1:], 62 ) 63 64 def test_toXML_ascii_data(self): 65 table = table__m_e_t_a() 66 table.data["TEST"] = b"Hello!" 67 writer = XMLWriter(BytesIO()) 68 table.toXML(writer, {"meta": table}) 69 xml = writer.file.getvalue().decode("utf-8") 70 self.assertEqual( 71 [ 72 '<hexdata tag="TEST">', 73 "<!-- ascii: Hello! -->", 74 "48656c6c 6f21", 75 "</hexdata>", 76 ], 77 [line.strip() for line in xml.splitlines()][1:], 78 ) 79 80 def test_fromXML(self): 81 table = table__m_e_t_a() 82 for name, attrs, content in parseXML( 83 '<hexdata tag="TEST">' " cafebeef" "</hexdata>" 84 ): 85 table.fromXML(name, attrs, content, ttFont=None) 86 self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data) 87 88 def test_toXML_text(self): 89 table = table__m_e_t_a() 90 table.data["dlng"] = "Latn,Grek,Cyrl" 91 writer = XMLWriter(BytesIO()) 92 table.toXML(writer, {"meta": table}) 93 xml = writer.file.getvalue().decode("utf-8") 94 self.assertEqual( 95 ['<text tag="dlng">', "Latn,Grek,Cyrl", "</text>"], 96 [line.strip() for line in xml.splitlines()][1:], 97 ) 98 99 def test_fromXML_text(self): 100 table = table__m_e_t_a() 101 for name, attrs, content in parseXML( 102 '<text tag="dlng">' " Latn,Grek,Cyrl" "</text>" 103 ): 104 table.fromXML(name, attrs, content, ttFont=None) 105 self.assertEqual({"dlng": "Latn,Grek,Cyrl"}, table.data) 106 107 108if __name__ == "__main__": 109 import sys 110 111 sys.exit(unittest.main()) 112