• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
11class ScaleUpemTest(unittest.TestCase):
12
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,
30                            "tmp%d%s" % (self.num_tempfiles, suffix))
31
32    def temp_dir(self):
33        if not self.tempdir:
34            self.tempdir = tempfile.mkdtemp()
35
36    def read_ttx(self, path):
37        lines = []
38        with open(path, "r", encoding="utf-8") as ttx:
39            for line in ttx.readlines():
40                # Elide ttFont attributes because ttLibVersion may change.
41                if line.startswith("<ttFont "):
42                    lines.append("<ttFont>\n")
43                else:
44                    lines.append(line.rstrip() + "\n")
45        return lines
46
47    def expect_ttx(self, font, expected_ttx, tables):
48        path = self.temp_path(suffix=".ttx")
49        font.saveXML(path, tables=tables)
50        actual = self.read_ttx(path)
51        expected = self.read_ttx(expected_ttx)
52        if actual != expected:
53            for line in difflib.unified_diff(
54                    expected, actual, fromfile=expected_ttx, tofile=path):
55                sys.stdout.write(line)
56            self.fail("TTX output is different from expected")
57
58    def test_scale_upem_ttf(self):
59
60        font = TTFont(self.get_path("I.ttf"))
61        tables = [table_tag for table_tag in font.keys() if table_tag != "head"]
62
63        scale_upem(font, 512)
64
65        expected_ttx_path = self.get_path("I-512upem.ttx")
66        self.expect_ttx(font, expected_ttx_path, tables)
67
68
69    def test_scale_upem_otf(self):
70
71        # Just test that it doesn't crash
72
73        font = TTFont(self.get_path("TestVGID-Regular.otf"))
74
75        scale_upem(font, 500)
76