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