1from __future__ import print_function, division, absolute_import 2from fontTools.cffLib import TopDict, PrivateDict, CharStrings 3from fontTools.misc.testTools import parseXML, DataFilesHandler 4from fontTools.ttLib import TTFont 5import copy 6import os 7import sys 8import unittest 9 10 11class CffLibTest(DataFilesHandler): 12 13 def test_topDict_recalcFontBBox(self): 14 topDict = TopDict() 15 topDict.CharStrings = CharStrings(None, None, None, PrivateDict(), None, None) 16 topDict.CharStrings.fromXML(None, None, parseXML(""" 17 <CharString name=".notdef"> 18 endchar 19 </CharString> 20 <CharString name="foo"><!-- [100, -100, 300, 100] --> 21 100 -100 rmoveto 200 hlineto 200 vlineto -200 hlineto endchar 22 </CharString> 23 <CharString name="bar"><!-- [0, 0, 200, 200] --> 24 0 0 rmoveto 200 hlineto 200 vlineto -200 hlineto endchar 25 </CharString> 26 <CharString name="baz"><!-- [-55.1, -55.1, 55.1, 55.1] --> 27 -55.1 -55.1 rmoveto 110.2 hlineto 110.2 vlineto -110.2 hlineto endchar 28 </CharString> 29 """)) 30 31 topDict.recalcFontBBox() 32 self.assertEqual(topDict.FontBBox, [-56, -100, 300, 200]) 33 34 def test_topDict_recalcFontBBox_empty(self): 35 topDict = TopDict() 36 topDict.CharStrings = CharStrings(None, None, None, PrivateDict(), None, None) 37 topDict.CharStrings.fromXML(None, None, parseXML(""" 38 <CharString name=".notdef"> 39 endchar 40 </CharString> 41 <CharString name="space"> 42 123 endchar 43 </CharString> 44 """)) 45 46 topDict.recalcFontBBox() 47 self.assertEqual(topDict.FontBBox, [0, 0, 0, 0]) 48 49 def test_topDict_set_Encoding(self): 50 file_name = 'TestOTF.otf' 51 font_path = self.getpath(file_name) 52 temp_path = self.temp_font(font_path, file_name) 53 save_path = temp_path[:-4] + '2.otf' 54 font = TTFont(temp_path) 55 topDict = font["CFF "].cff.topDictIndex[0] 56 encoding = [".notdef"] * 256 57 encoding[0x20] = "space" 58 topDict.Encoding = encoding 59 font.save(save_path) 60 font2 = TTFont(save_path) 61 topDict2 = font2["CFF "].cff.topDictIndex[0] 62 self.assertEqual(topDict2.Encoding[32], "space") 63 64 def test_CFF_deepcopy(self): 65 """Test that deepcopying a TTFont with a CFF table does not recurse 66 infinitely.""" 67 ttx_path = os.path.join( 68 os.path.dirname(__file__), 69 "..", 70 "varLib", 71 "data", 72 "master_ttx_interpolatable_otf", 73 "TestFamily2-Master0.ttx", 74 ) 75 font = TTFont(recalcBBoxes=False, recalcTimestamp=False) 76 font.importXML(ttx_path) 77 copy.deepcopy(font) 78 79 80if __name__ == "__main__": 81 sys.exit(unittest.main()) 82