1from __future__ import print_function, division, absolute_import 2from fontTools.misc.py23 import * 3import unittest 4import os 5from fontTools import afmLib 6 7 8CWD = os.path.abspath(os.path.dirname(__file__)) 9DATADIR = os.path.join(CWD, 'data') 10AFM = os.path.join(DATADIR, 'TestAFM.afm') 11 12 13class AFMTest(unittest.TestCase): 14 15 def test_read_afm(self): 16 afm = afmLib.AFM(AFM) 17 self.assertEqual(sorted(afm.kernpairs()), 18 sorted([('V', 'A'), ('T', 'comma'), ('V', 'd'), ('T', 'c'), ('T', 'period')])) 19 self.assertEqual(afm['V', 'A'], -60) 20 self.assertEqual(afm['V', 'd'], 30) 21 self.assertEqual(afm['A'], (65, 668, (8, -25, 660, 666))) 22 23 def test_write_afm(self): 24 afm = afmLib.AFM(AFM) 25 newAfm, afmData = self.write(afm) 26 self.assertEqual(afm.kernpairs(), newAfm.kernpairs()) 27 self.assertEqual(afm.chars(), newAfm.chars()) 28 self.assertEqual(afm.comments(), newAfm.comments()[1:]) # skip the "generated by afmLib" comment 29 for pair in afm.kernpairs(): 30 self.assertEqual(afm[pair], newAfm[pair]) 31 for char in afm.chars(): 32 self.assertEqual(afm[char], newAfm[char]) 33 with open(AFM, 'r') as f: 34 originalLines = f.read().splitlines() 35 newLines = afmData.splitlines() 36 del newLines[1] # remove the "generated by afmLib" comment 37 self.assertEqual(originalLines, newLines) 38 39 @staticmethod 40 def write(afm, sep='\r'): 41 temp = os.path.join(DATADIR, 'temp.afm') 42 try: 43 afm.write(temp, sep) 44 with open(temp, 'r') as f: 45 afmData = f.read() 46 afm = afmLib.AFM(temp) 47 finally: 48 if os.path.exists(temp): 49 os.remove(temp) 50 return afm, afmData 51 52 53if __name__ == '__main__': 54 import sys 55 sys.exit(unittest.main()) 56