• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from fontTools.ttLib import TTFont
4from fontTools.varLib.interpolatable import main as interpolatable_main
5import os
6import shutil
7import sys
8import tempfile
9import unittest
10
11try:
12    import scipy
13except:
14    scipy = None
15
16try:
17    import munkres
18except ImportError:
19    munkres = None
20
21
22@unittest.skipUnless(scipy or munkres, "scipy or munkres not installed")
23class InterpolatableTest(unittest.TestCase):
24    def __init__(self, methodName):
25        unittest.TestCase.__init__(self, methodName)
26        # Python 3 renamed assertRaisesRegexp to assertRaisesRegex,
27        # and fires deprecation warnings if a program uses the old name.
28        if not hasattr(self, "assertRaisesRegex"):
29            self.assertRaisesRegex = self.assertRaisesRegexp
30
31    def setUp(self):
32        self.tempdir = None
33        self.num_tempfiles = 0
34
35    def tearDown(self):
36        if self.tempdir:
37            shutil.rmtree(self.tempdir)
38
39    @staticmethod
40    def get_test_input(test_file_or_folder):
41        path, _ = os.path.split(__file__)
42        return os.path.join(path, "data", test_file_or_folder)
43
44    @staticmethod
45    def get_file_list(folder, suffix, prefix=''):
46        all_files = os.listdir(folder)
47        file_list = []
48        for p in all_files:
49            if p.startswith(prefix) and p.endswith(suffix):
50                file_list.append(os.path.abspath(os.path.join(folder, p)))
51        return file_list
52
53    def temp_path(self, suffix):
54        self.temp_dir()
55        self.num_tempfiles += 1
56        return os.path.join(self.tempdir,
57                            "tmp%d%s" % (self.num_tempfiles, suffix))
58
59    def temp_dir(self):
60        if not self.tempdir:
61            self.tempdir = tempfile.mkdtemp()
62
63    def compile_font(self, path, suffix, temp_dir):
64        ttx_filename = os.path.basename(path)
65        savepath = os.path.join(temp_dir, ttx_filename.replace('.ttx', suffix))
66        font = TTFont(recalcBBoxes=False, recalcTimestamp=False)
67        font.importXML(path)
68        font.save(savepath, reorderTables=None)
69        return font, savepath
70
71# -----
72# Tests
73# -----
74
75    def test_interpolatable_ttf(self):
76        suffix = '.ttf'
77        ttx_dir = self.get_test_input('master_ttx_interpolatable_ttf')
78
79        self.temp_dir()
80        ttx_paths = self.get_file_list(ttx_dir, '.ttx', 'TestFamily2-')
81        for path in ttx_paths:
82            self.compile_font(path, suffix, self.tempdir)
83
84        ttf_paths = self.get_file_list(self.tempdir, suffix)
85        self.assertIsNone(interpolatable_main(ttf_paths))
86
87
88    def test_interpolatable_otf(self):
89        suffix = '.otf'
90        ttx_dir = self.get_test_input('master_ttx_interpolatable_otf')
91
92        self.temp_dir()
93        ttx_paths = self.get_file_list(ttx_dir, '.ttx', 'TestFamily2-')
94        for path in ttx_paths:
95            self.compile_font(path, suffix, self.tempdir)
96
97        otf_paths = self.get_file_list(self.tempdir, suffix)
98        self.assertIsNone(interpolatable_main(otf_paths))
99
100
101if __name__ == "__main__":
102    sys.exit(unittest.main())
103