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