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