Blame Tests/afmLib/afmLib_test.py

rpm-build 6a2e4c
from __future__ import print_function, division, absolute_import
rpm-build 6a2e4c
from fontTools.misc.py23 import *
rpm-build 6a2e4c
import unittest
rpm-build 6a2e4c
import os
rpm-build 6a2e4c
from fontTools import afmLib
rpm-build 6a2e4c
rpm-build 6a2e4c
rpm-build 6a2e4c
CWD = os.path.abspath(os.path.dirname(__file__))
rpm-build 6a2e4c
DATADIR = os.path.join(CWD, 'data')
rpm-build 6a2e4c
AFM = os.path.join(DATADIR, 'TestAFM.afm')
rpm-build 6a2e4c
rpm-build 6a2e4c
rpm-build 6a2e4c
class AFMTest(unittest.TestCase):
rpm-build 6a2e4c
rpm-build 6a2e4c
	def test_read_afm(self):
rpm-build 6a2e4c
		afm = afmLib.AFM(AFM)
rpm-build 6a2e4c
		self.assertEqual(sorted(afm.kernpairs()), 
rpm-build 6a2e4c
			sorted([('V', 'A'), ('T', 'comma'), ('V', 'd'), ('T', 'c'), ('T', 'period')]))
rpm-build 6a2e4c
		self.assertEqual(afm['V', 'A'], -60)
rpm-build 6a2e4c
		self.assertEqual(afm['V', 'd'], 30)
rpm-build 6a2e4c
		self.assertEqual(afm['A'], (65, 668, (8, -25, 660, 666)))
rpm-build 6a2e4c
rpm-build 6a2e4c
	def test_write_afm(self):
rpm-build 6a2e4c
		afm = afmLib.AFM(AFM)
rpm-build 6a2e4c
		newAfm, afmData = self.write(afm)
rpm-build 6a2e4c
		self.assertEqual(afm.kernpairs(), newAfm.kernpairs())
rpm-build 6a2e4c
		self.assertEqual(afm.chars(), newAfm.chars())
rpm-build 6a2e4c
		self.assertEqual(afm.comments(), newAfm.comments()[1:])  # skip the "generated by afmLib" comment
rpm-build 6a2e4c
		for pair in afm.kernpairs():
rpm-build 6a2e4c
			self.assertEqual(afm[pair], newAfm[pair])
rpm-build 6a2e4c
		for char in afm.chars():
rpm-build 6a2e4c
			self.assertEqual(afm[char], newAfm[char])
rpm-build 6a2e4c
		with open(AFM, 'r') as f:
rpm-build 6a2e4c
			originalLines = f.read().splitlines()
rpm-build 6a2e4c
		newLines = afmData.splitlines()
rpm-build 6a2e4c
		del newLines[1]  # remove the "generated by afmLib" comment
rpm-build 6a2e4c
		self.assertEqual(originalLines, newLines)
rpm-build 6a2e4c
rpm-build 6a2e4c
	@staticmethod
rpm-build 6a2e4c
	def write(afm, sep='\r'):
rpm-build 6a2e4c
		temp = os.path.join(DATADIR, 'temp.afm')
rpm-build 6a2e4c
		try:
rpm-build 6a2e4c
			afm.write(temp, sep)
rpm-build 6a2e4c
			with open(temp, 'r') as f:
rpm-build 6a2e4c
				afmData = f.read()
rpm-build 6a2e4c
			afm = afmLib.AFM(temp)
rpm-build 6a2e4c
		finally:
rpm-build 6a2e4c
			if os.path.exists(temp):
rpm-build 6a2e4c
				os.remove(temp)
rpm-build 6a2e4c
		return afm, afmData
rpm-build 6a2e4c
rpm-build 6a2e4c
rpm-build 6a2e4c
if __name__ == '__main__':
rpm-build 6a2e4c
	import sys
rpm-build 6a2e4c
	sys.exit(unittest.main())