1*e1fe3e4aSElliott Hughesimport os 2*e1fe3e4aSElliott Hughesimport shutil 3*e1fe3e4aSElliott Hughesimport unittest 4*e1fe3e4aSElliott Hughesimport tempfile 5*e1fe3e4aSElliott Hughesfrom io import open 6*e1fe3e4aSElliott Hughesfrom fontTools.ufoLib import UFOReader, UFOWriter, UFOLibError 7*e1fe3e4aSElliott Hughesfrom fontTools.ufoLib import plistlib 8*e1fe3e4aSElliott Hughesfrom .testSupport import fontInfoVersion1, fontInfoVersion2 9*e1fe3e4aSElliott Hughes 10*e1fe3e4aSElliott Hughes 11*e1fe3e4aSElliott Hughesclass TestInfoObject: 12*e1fe3e4aSElliott Hughes pass 13*e1fe3e4aSElliott Hughes 14*e1fe3e4aSElliott Hughes 15*e1fe3e4aSElliott Hughesclass ReadFontInfoVersion1TestCase(unittest.TestCase): 16*e1fe3e4aSElliott Hughes def setUp(self): 17*e1fe3e4aSElliott Hughes self.dstDir = tempfile.mktemp() 18*e1fe3e4aSElliott Hughes os.mkdir(self.dstDir) 19*e1fe3e4aSElliott Hughes metaInfo = {"creator": "test", "formatVersion": 1} 20*e1fe3e4aSElliott Hughes path = os.path.join(self.dstDir, "metainfo.plist") 21*e1fe3e4aSElliott Hughes with open(path, "wb") as f: 22*e1fe3e4aSElliott Hughes plistlib.dump(metaInfo, f) 23*e1fe3e4aSElliott Hughes 24*e1fe3e4aSElliott Hughes def tearDown(self): 25*e1fe3e4aSElliott Hughes shutil.rmtree(self.dstDir) 26*e1fe3e4aSElliott Hughes 27*e1fe3e4aSElliott Hughes def _writeInfoToPlist(self, info): 28*e1fe3e4aSElliott Hughes path = os.path.join(self.dstDir, "fontinfo.plist") 29*e1fe3e4aSElliott Hughes with open(path, "wb") as f: 30*e1fe3e4aSElliott Hughes plistlib.dump(info, f) 31*e1fe3e4aSElliott Hughes 32*e1fe3e4aSElliott Hughes def testRead(self): 33*e1fe3e4aSElliott Hughes originalData = dict(fontInfoVersion1) 34*e1fe3e4aSElliott Hughes self._writeInfoToPlist(originalData) 35*e1fe3e4aSElliott Hughes infoObject = TestInfoObject() 36*e1fe3e4aSElliott Hughes reader = UFOReader(self.dstDir, validate=True) 37*e1fe3e4aSElliott Hughes reader.readInfo(infoObject) 38*e1fe3e4aSElliott Hughes for attr in dir(infoObject): 39*e1fe3e4aSElliott Hughes if attr not in fontInfoVersion2: 40*e1fe3e4aSElliott Hughes continue 41*e1fe3e4aSElliott Hughes originalValue = fontInfoVersion2[attr] 42*e1fe3e4aSElliott Hughes readValue = getattr(infoObject, attr) 43*e1fe3e4aSElliott Hughes self.assertEqual(originalValue, readValue) 44*e1fe3e4aSElliott Hughes 45*e1fe3e4aSElliott Hughes def testFontStyleConversion(self): 46*e1fe3e4aSElliott Hughes fontStyle1To2 = {64: "regular", 1: "italic", 32: "bold", 33: "bold italic"} 47*e1fe3e4aSElliott Hughes for old, new in list(fontStyle1To2.items()): 48*e1fe3e4aSElliott Hughes info = dict(fontInfoVersion1) 49*e1fe3e4aSElliott Hughes info["fontStyle"] = old 50*e1fe3e4aSElliott Hughes self._writeInfoToPlist(info) 51*e1fe3e4aSElliott Hughes reader = UFOReader(self.dstDir, validate=True) 52*e1fe3e4aSElliott Hughes infoObject = TestInfoObject() 53*e1fe3e4aSElliott Hughes reader.readInfo(infoObject) 54*e1fe3e4aSElliott Hughes self.assertEqual(new, infoObject.styleMapStyleName) 55*e1fe3e4aSElliott Hughes 56*e1fe3e4aSElliott Hughes def testWidthNameConversion(self): 57*e1fe3e4aSElliott Hughes widthName1To2 = { 58*e1fe3e4aSElliott Hughes "Ultra-condensed": 1, 59*e1fe3e4aSElliott Hughes "Extra-condensed": 2, 60*e1fe3e4aSElliott Hughes "Condensed": 3, 61*e1fe3e4aSElliott Hughes "Semi-condensed": 4, 62*e1fe3e4aSElliott Hughes "Medium (normal)": 5, 63*e1fe3e4aSElliott Hughes "Semi-expanded": 6, 64*e1fe3e4aSElliott Hughes "Expanded": 7, 65*e1fe3e4aSElliott Hughes "Extra-expanded": 8, 66*e1fe3e4aSElliott Hughes "Ultra-expanded": 9, 67*e1fe3e4aSElliott Hughes } 68*e1fe3e4aSElliott Hughes for old, new in list(widthName1To2.items()): 69*e1fe3e4aSElliott Hughes info = dict(fontInfoVersion1) 70*e1fe3e4aSElliott Hughes info["widthName"] = old 71*e1fe3e4aSElliott Hughes self._writeInfoToPlist(info) 72*e1fe3e4aSElliott Hughes reader = UFOReader(self.dstDir, validate=True) 73*e1fe3e4aSElliott Hughes infoObject = TestInfoObject() 74*e1fe3e4aSElliott Hughes reader.readInfo(infoObject) 75*e1fe3e4aSElliott Hughes self.assertEqual(new, infoObject.openTypeOS2WidthClass) 76*e1fe3e4aSElliott Hughes 77*e1fe3e4aSElliott Hughes 78*e1fe3e4aSElliott Hughesclass WriteFontInfoVersion1TestCase(unittest.TestCase): 79*e1fe3e4aSElliott Hughes def setUp(self): 80*e1fe3e4aSElliott Hughes self.tempDir = tempfile.mktemp() 81*e1fe3e4aSElliott Hughes os.mkdir(self.tempDir) 82*e1fe3e4aSElliott Hughes self.dstDir = os.path.join(self.tempDir, "test.ufo") 83*e1fe3e4aSElliott Hughes 84*e1fe3e4aSElliott Hughes def tearDown(self): 85*e1fe3e4aSElliott Hughes shutil.rmtree(self.tempDir) 86*e1fe3e4aSElliott Hughes 87*e1fe3e4aSElliott Hughes def makeInfoObject(self): 88*e1fe3e4aSElliott Hughes infoObject = TestInfoObject() 89*e1fe3e4aSElliott Hughes for attr, value in list(fontInfoVersion2.items()): 90*e1fe3e4aSElliott Hughes setattr(infoObject, attr, value) 91*e1fe3e4aSElliott Hughes return infoObject 92*e1fe3e4aSElliott Hughes 93*e1fe3e4aSElliott Hughes def readPlist(self): 94*e1fe3e4aSElliott Hughes path = os.path.join(self.dstDir, "fontinfo.plist") 95*e1fe3e4aSElliott Hughes with open(path, "rb") as f: 96*e1fe3e4aSElliott Hughes plist = plistlib.load(f) 97*e1fe3e4aSElliott Hughes return plist 98*e1fe3e4aSElliott Hughes 99*e1fe3e4aSElliott Hughes def testWrite(self): 100*e1fe3e4aSElliott Hughes infoObject = self.makeInfoObject() 101*e1fe3e4aSElliott Hughes writer = UFOWriter(self.dstDir, formatVersion=1) 102*e1fe3e4aSElliott Hughes writer.writeInfo(infoObject) 103*e1fe3e4aSElliott Hughes writtenData = self.readPlist() 104*e1fe3e4aSElliott Hughes for attr, originalValue in list(fontInfoVersion1.items()): 105*e1fe3e4aSElliott Hughes newValue = writtenData[attr] 106*e1fe3e4aSElliott Hughes self.assertEqual(newValue, originalValue) 107*e1fe3e4aSElliott Hughes 108*e1fe3e4aSElliott Hughes def testFontStyleConversion(self): 109*e1fe3e4aSElliott Hughes fontStyle1To2 = {64: "regular", 1: "italic", 32: "bold", 33: "bold italic"} 110*e1fe3e4aSElliott Hughes for old, new in list(fontStyle1To2.items()): 111*e1fe3e4aSElliott Hughes infoObject = self.makeInfoObject() 112*e1fe3e4aSElliott Hughes infoObject.styleMapStyleName = new 113*e1fe3e4aSElliott Hughes writer = UFOWriter(self.dstDir, formatVersion=1) 114*e1fe3e4aSElliott Hughes writer.writeInfo(infoObject) 115*e1fe3e4aSElliott Hughes writtenData = self.readPlist() 116*e1fe3e4aSElliott Hughes self.assertEqual(writtenData["fontStyle"], old) 117*e1fe3e4aSElliott Hughes 118*e1fe3e4aSElliott Hughes def testWidthNameConversion(self): 119*e1fe3e4aSElliott Hughes widthName1To2 = { 120*e1fe3e4aSElliott Hughes "Ultra-condensed": 1, 121*e1fe3e4aSElliott Hughes "Extra-condensed": 2, 122*e1fe3e4aSElliott Hughes "Condensed": 3, 123*e1fe3e4aSElliott Hughes "Semi-condensed": 4, 124*e1fe3e4aSElliott Hughes "Medium (normal)": 5, 125*e1fe3e4aSElliott Hughes "Semi-expanded": 6, 126*e1fe3e4aSElliott Hughes "Expanded": 7, 127*e1fe3e4aSElliott Hughes "Extra-expanded": 8, 128*e1fe3e4aSElliott Hughes "Ultra-expanded": 9, 129*e1fe3e4aSElliott Hughes } 130*e1fe3e4aSElliott Hughes for old, new in list(widthName1To2.items()): 131*e1fe3e4aSElliott Hughes infoObject = self.makeInfoObject() 132*e1fe3e4aSElliott Hughes infoObject.openTypeOS2WidthClass = new 133*e1fe3e4aSElliott Hughes writer = UFOWriter(self.dstDir, formatVersion=1) 134*e1fe3e4aSElliott Hughes writer.writeInfo(infoObject) 135*e1fe3e4aSElliott Hughes writtenData = self.readPlist() 136*e1fe3e4aSElliott Hughes self.assertEqual(writtenData["widthName"], old) 137