diff --git a/Tests/afmLib/afmLib_test.py b/Tests/afmLib/afmLib_test.py new file mode 100644 index 000000000..2dedb3066 --- /dev/null +++ b/Tests/afmLib/afmLib_test.py @@ -0,0 +1,55 @@ +from __future__ import print_function, division, absolute_import +from fontTools.misc.py23 import * +import unittest +import os +from fontTools import afmLib + + +CWD = os.path.abspath(os.path.dirname(__file__)) +DATADIR = os.path.join(CWD, 'data') +AFM = os.path.join(DATADIR, 'TestAFM.afm') + + +class AFMTest(unittest.TestCase): + + def test_read_afm(self): + afm = afmLib.AFM(AFM) + self.assertEqual(sorted(afm.kernpairs()), + sorted([('V', 'A'), ('T', 'comma'), ('V', 'd'), ('T', 'c'), ('T', 'period')])) + self.assertEqual(afm['V', 'A'], -60) + self.assertEqual(afm['V', 'd'], 30) + self.assertEqual(afm['A'], (65, 668, (8, -25, 660, 666))) + + def test_write_afm(self): + afm = afmLib.AFM(AFM) + newAfm, afmData = self.write(afm) + self.assertEqual(afm.kernpairs(), newAfm.kernpairs()) + self.assertEqual(afm.chars(), newAfm.chars()) + self.assertEqual(afm.comments(), newAfm.comments()[1:]) # skip the "generated by afmLib" comment + for pair in afm.kernpairs(): + self.assertEqual(afm[pair], newAfm[pair]) + for char in afm.chars(): + self.assertEqual(afm[char], newAfm[char]) + with open(AFM, 'r') as f: + originalLines = f.read().splitlines() + newLines = afmData.splitlines() + del newLines[1] # remove the + self.assertEqual(originalLines, newLines) + + @staticmethod + def write(afm, sep='\r'): + temp = os.path.join(DATADIR, 'temp.afm') + try: + afm.write(temp, sep) + with open(temp, 'r') as f: + afmData = f.read() + afm = afmLib.AFM(temp) + finally: + if os.path.exists(temp): + os.remove(temp) + return afm, afmData + + +if __name__ == '__main__': + import sys + sys.exit(unittest.main()) diff --git a/Tests/afmLib/data/TestAFM.afm b/Tests/afmLib/data/TestAFM.afm new file mode 100644 index 000000000..634c66a3f --- /dev/null +++ b/Tests/afmLib/data/TestAFM.afm @@ -0,0 +1,37 @@ +StartFontMetrics 2.0 +Comment UniqueID 2123703 +Comment Panose 2 0 6 3 3 0 0 2 0 4 +FontName TestFont-Regular +FullName TestFont-Regular +FamilyName TestFont +Weight Regular +ItalicAngle 0.00 +IsFixedPitch false +FontBBox -94 -317 1316 1009 +UnderlinePosition -296 +UnderlineThickness 111 +Version 001.000 +Notice [c] Copyright 2017. All Rights Reserved. +EncodingScheme FontSpecific +CapHeight 700 +XHeight 500 +Ascender 750 +Descender -250 +StdHW 181 +StdVW 194 +StartCharMetrics 4 +C 32 ; WX 200 ; N space ; B 0 0 0 0 ; +C 65 ; WX 668 ; N A ; B 8 -25 660 666 ; +C 66 ; WX 543 ; N B ; B 36 0 522 666 ; +C 67 ; WX 582 ; N C ; B 24 -21 564 687 ; +EndCharMetrics +StartKernData +StartKernPairs 5 +KPX T c 30 +KPX T comma -100 +KPX T period -100 +KPX V A -60 +KPX V d 30 +EndKernPairs +EndKernData +EndFontMetrics