xref: /aosp_15_r20/external/fonttools/Tests/ttLib/scaleUpem_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.ttLib import TTFont
2*e1fe3e4aSElliott Hughesfrom fontTools.ttLib.scaleUpem import scale_upem
3*e1fe3e4aSElliott Hughesimport difflib
4*e1fe3e4aSElliott Hughesimport os
5*e1fe3e4aSElliott Hughesimport shutil
6*e1fe3e4aSElliott Hughesimport sys
7*e1fe3e4aSElliott Hughesimport tempfile
8*e1fe3e4aSElliott Hughesimport unittest
9*e1fe3e4aSElliott Hughesimport pytest
10*e1fe3e4aSElliott Hughes
11*e1fe3e4aSElliott Hughes
12*e1fe3e4aSElliott Hughesclass ScaleUpemTest(unittest.TestCase):
13*e1fe3e4aSElliott Hughes    def setUp(self):
14*e1fe3e4aSElliott Hughes        self.tempdir = None
15*e1fe3e4aSElliott Hughes        self.num_tempfiles = 0
16*e1fe3e4aSElliott Hughes
17*e1fe3e4aSElliott Hughes    def tearDown(self):
18*e1fe3e4aSElliott Hughes        if self.tempdir:
19*e1fe3e4aSElliott Hughes            shutil.rmtree(self.tempdir)
20*e1fe3e4aSElliott Hughes
21*e1fe3e4aSElliott Hughes    @staticmethod
22*e1fe3e4aSElliott Hughes    def get_path(test_file_or_folder):
23*e1fe3e4aSElliott Hughes        parent_dir = os.path.dirname(__file__)
24*e1fe3e4aSElliott Hughes        return os.path.join(parent_dir, "data", test_file_or_folder)
25*e1fe3e4aSElliott Hughes
26*e1fe3e4aSElliott Hughes    def temp_path(self, suffix):
27*e1fe3e4aSElliott Hughes        self.temp_dir()
28*e1fe3e4aSElliott Hughes        self.num_tempfiles += 1
29*e1fe3e4aSElliott Hughes        return os.path.join(self.tempdir, "tmp%d%s" % (self.num_tempfiles, suffix))
30*e1fe3e4aSElliott Hughes
31*e1fe3e4aSElliott Hughes    def temp_dir(self):
32*e1fe3e4aSElliott Hughes        if not self.tempdir:
33*e1fe3e4aSElliott Hughes            self.tempdir = tempfile.mkdtemp()
34*e1fe3e4aSElliott Hughes
35*e1fe3e4aSElliott Hughes    def read_ttx(self, path):
36*e1fe3e4aSElliott Hughes        lines = []
37*e1fe3e4aSElliott Hughes        with open(path, "r", encoding="utf-8") as ttx:
38*e1fe3e4aSElliott Hughes            for line in ttx.readlines():
39*e1fe3e4aSElliott Hughes                # Elide ttFont attributes because ttLibVersion may change.
40*e1fe3e4aSElliott Hughes                if line.startswith("<ttFont "):
41*e1fe3e4aSElliott Hughes                    lines.append("<ttFont>\n")
42*e1fe3e4aSElliott Hughes                else:
43*e1fe3e4aSElliott Hughes                    lines.append(line.rstrip() + "\n")
44*e1fe3e4aSElliott Hughes        return lines
45*e1fe3e4aSElliott Hughes
46*e1fe3e4aSElliott Hughes    def expect_ttx(self, font, expected_ttx, tables):
47*e1fe3e4aSElliott Hughes        path = self.temp_path(suffix=".ttx")
48*e1fe3e4aSElliott Hughes        font.saveXML(path, tables=tables)
49*e1fe3e4aSElliott Hughes        actual = self.read_ttx(path)
50*e1fe3e4aSElliott Hughes        expected = self.read_ttx(expected_ttx)
51*e1fe3e4aSElliott Hughes        if actual != expected:
52*e1fe3e4aSElliott Hughes            for line in difflib.unified_diff(
53*e1fe3e4aSElliott Hughes                expected, actual, fromfile=expected_ttx, tofile=path
54*e1fe3e4aSElliott Hughes            ):
55*e1fe3e4aSElliott Hughes                sys.stdout.write(line)
56*e1fe3e4aSElliott Hughes            self.fail("TTX output is different from expected")
57*e1fe3e4aSElliott Hughes
58*e1fe3e4aSElliott Hughes    def test_scale_upem_ttf(self):
59*e1fe3e4aSElliott Hughes        font = TTFont(self.get_path("I.ttf"))
60*e1fe3e4aSElliott Hughes        tables = [table_tag for table_tag in font.keys() if table_tag != "head"]
61*e1fe3e4aSElliott Hughes
62*e1fe3e4aSElliott Hughes        scale_upem(font, 512)
63*e1fe3e4aSElliott Hughes
64*e1fe3e4aSElliott Hughes        expected_ttx_path = self.get_path("I-512upem.ttx")
65*e1fe3e4aSElliott Hughes        self.expect_ttx(font, expected_ttx_path, tables)
66*e1fe3e4aSElliott Hughes
67*e1fe3e4aSElliott Hughes    def test_scale_upem_varComposite(self):
68*e1fe3e4aSElliott Hughes        font = TTFont(self.get_path("varc-ac00-ac01.ttf"))
69*e1fe3e4aSElliott Hughes        tables = [table_tag for table_tag in font.keys() if table_tag != "head"]
70*e1fe3e4aSElliott Hughes
71*e1fe3e4aSElliott Hughes        scale_upem(font, 500)
72*e1fe3e4aSElliott Hughes
73*e1fe3e4aSElliott Hughes        expected_ttx_path = self.get_path("varc-ac00-ac01-500upem.ttx")
74*e1fe3e4aSElliott Hughes        self.expect_ttx(font, expected_ttx_path, tables)
75*e1fe3e4aSElliott Hughes
76*e1fe3e4aSElliott Hughes        # Scale our other varComposite font as well; without checking the expected
77*e1fe3e4aSElliott Hughes        font = TTFont(self.get_path("varc-6868.ttf"))
78*e1fe3e4aSElliott Hughes        scale_upem(font, 500)
79*e1fe3e4aSElliott Hughes
80*e1fe3e4aSElliott Hughes    def test_scale_upem_otf(self):
81*e1fe3e4aSElliott Hughes        # Just test that it doesn't crash
82*e1fe3e4aSElliott Hughes
83*e1fe3e4aSElliott Hughes        font = TTFont(self.get_path("TestVGID-Regular.otf"))
84*e1fe3e4aSElliott Hughes
85*e1fe3e4aSElliott Hughes        scale_upem(font, 500)
86