aboutsummaryrefslogtreecommitdiff
path: root/Tests/ttLib/tables/C_F_F__2_test.py
blob: 2e4d19af44d17a295ccd108abe1fb26ec925c748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""cff2Lib_test.py -- unit test for Adobe CFF fonts."""

from fontTools.ttLib import TTFont
from io import StringIO
import re
import os
import unittest


CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
DATA_DIR = os.path.join(CURR_DIR, "data")

CFF_TTX = os.path.join(DATA_DIR, "C_F_F__2.ttx")
CFF_BIN = os.path.join(DATA_DIR, "C_F_F__2.bin")


def strip_VariableItems(string):
    # ttlib changes with the fontTools version
    string = re.sub(' ttLibVersion=".*"', "", string)
    # head table checksum and mod date changes with each save.
    string = re.sub('<checkSumAdjustment value="[^"]+"/>', "", string)
    string = re.sub('<modified value="[^"]+"/>', "", string)
    return string


class CFFTableTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        with open(CFF_BIN, "rb") as f:
            font = TTFont(file=CFF_BIN)
            cffTable = font["CFF2"]
            cls.cff2Data = cffTable.compile(font)
        with open(CFF_TTX, "r") as f:
            cff2XML = f.read()
            cff2XML = strip_VariableItems(cff2XML)
            cls.cff2XML = cff2XML.splitlines()

    def test_toXML(self):
        font = TTFont(file=CFF_BIN)
        cffTable = font["CFF2"]
        cffData = cffTable.compile(font)
        out = StringIO()
        font.saveXML(out)
        cff2XML = out.getvalue()
        cff2XML = strip_VariableItems(cff2XML)
        cff2XML = cff2XML.splitlines()
        self.assertEqual(cff2XML, self.cff2XML)

    def test_fromXML(self):
        font = TTFont(sfntVersion="OTTO")
        font.importXML(CFF_TTX)
        cffTable = font["CFF2"]
        cff2Data = cffTable.compile(font)
        self.assertEqual(cff2Data, self.cff2Data)


if __name__ == "__main__":
    unittest.main()