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