1from fontTools.cffLib import TopDict, PrivateDict, CharStrings 2from fontTools.misc.testTools import parseXML, DataFilesHandler 3from fontTools.ttLib import TTFont 4import copy 5import os 6import sys 7import unittest 8 9 10class CffLibTest(DataFilesHandler): 11 def test_topDict_recalcFontBBox(self): 12 topDict = TopDict() 13 topDict.CharStrings = CharStrings(None, None, None, PrivateDict(), None, None) 14 topDict.CharStrings.fromXML( 15 None, 16 None, 17 parseXML( 18 """ 19 <CharString name=".notdef"> 20 endchar 21 </CharString> 22 <CharString name="foo"><!-- [100, -100, 300, 100] --> 23 100 -100 rmoveto 200 hlineto 200 vlineto -200 hlineto endchar 24 </CharString> 25 <CharString name="bar"><!-- [0, 0, 200, 200] --> 26 0 0 rmoveto 200 hlineto 200 vlineto -200 hlineto endchar 27 </CharString> 28 <CharString name="baz"><!-- [-55.1, -55.1, 55.1, 55.1] --> 29 -55.1 -55.1 rmoveto 110.2 hlineto 110.2 vlineto -110.2 hlineto endchar 30 </CharString> 31 """ 32 ), 33 ) 34 35 topDict.recalcFontBBox() 36 self.assertEqual(topDict.FontBBox, [-56, -100, 300, 200]) 37 38 def test_topDict_recalcFontBBox_empty(self): 39 topDict = TopDict() 40 topDict.CharStrings = CharStrings(None, None, None, PrivateDict(), None, None) 41 topDict.CharStrings.fromXML( 42 None, 43 None, 44 parseXML( 45 """ 46 <CharString name=".notdef"> 47 endchar 48 </CharString> 49 <CharString name="space"> 50 123 endchar 51 </CharString> 52 """ 53 ), 54 ) 55 56 topDict.recalcFontBBox() 57 self.assertEqual(topDict.FontBBox, [0, 0, 0, 0]) 58 59 def test_topDict_set_Encoding(self): 60 ttx_path = self.getpath("TestOTF.ttx") 61 font = TTFont(recalcBBoxes=False, recalcTimestamp=False) 62 font.importXML(ttx_path) 63 64 topDict = font["CFF "].cff.topDictIndex[0] 65 encoding = [".notdef"] * 256 66 encoding[0x20] = "space" 67 topDict.Encoding = encoding 68 69 self.temp_dir() 70 save_path = os.path.join(self.tempdir, "TestOTF.otf") 71 font.save(save_path) 72 73 font2 = TTFont(save_path) 74 topDict2 = font2["CFF "].cff.topDictIndex[0] 75 self.assertEqual(topDict2.Encoding[32], "space") 76 77 def test_CFF_deepcopy(self): 78 """Test that deepcopying a TTFont with a CFF table does not recurse 79 infinitely.""" 80 ttx_path = os.path.join( 81 os.path.dirname(__file__), 82 "..", 83 "varLib", 84 "data", 85 "master_ttx_interpolatable_otf", 86 "TestFamily2-Master0.ttx", 87 ) 88 font = TTFont(recalcBBoxes=False, recalcTimestamp=False) 89 font.importXML(ttx_path) 90 copy.deepcopy(font) 91 92 def test_FDSelect_format_4(self): 93 ttx_path = self.getpath("TestFDSelect4.ttx") 94 font = TTFont(recalcBBoxes=False, recalcTimestamp=False) 95 font.importXML(ttx_path) 96 97 self.temp_dir() 98 save_path = os.path.join(self.tempdir, "TestOTF.otf") 99 font.save(save_path) 100 101 font2 = TTFont(save_path) 102 topDict2 = font2["CFF2"].cff.topDictIndex[0] 103 self.assertEqual(topDict2.FDSelect.format, 4) 104 self.assertEqual(topDict2.FDSelect.gidArray, [0, 0, 1]) 105 106 def test_unique_glyph_names(self): 107 font_path = self.getpath("LinLibertine_RBI.otf") 108 font = TTFont(font_path, recalcBBoxes=False, recalcTimestamp=False) 109 110 glyphOrder = font.getGlyphOrder() 111 self.assertEqual(len(glyphOrder), len(set(glyphOrder))) 112 113 self.temp_dir() 114 save_path = os.path.join(self.tempdir, "TestOTF.otf") 115 font.save(save_path) 116 117 font2 = TTFont(save_path) 118 glyphOrder = font2.getGlyphOrder() 119 self.assertEqual(len(glyphOrder), len(set(glyphOrder))) 120 121 122if __name__ == "__main__": 123 sys.exit(unittest.main()) 124