2017-03-03 14:54:40 -08:00
|
|
|
from fontTools.ttLib import TTFont
|
|
|
|
from fontTools.varLib import build
|
|
|
|
from fontTools.varLib.mutator import main as mutator
|
2018-11-15 17:56:37 -08:00
|
|
|
from fontTools.varLib.mutator import instantiateVariableFont as make_instance
|
2017-03-03 14:54:40 -08:00
|
|
|
import difflib
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class MutatorTest(unittest.TestCase):
|
|
|
|
def __init__(self, methodName):
|
|
|
|
unittest.TestCase.__init__(self, methodName)
|
|
|
|
# Python 3 renamed assertRaisesRegexp to assertRaisesRegex,
|
|
|
|
# and fires deprecation warnings if a program uses the old name.
|
|
|
|
if not hasattr(self, "assertRaisesRegex"):
|
|
|
|
self.assertRaisesRegex = self.assertRaisesRegexp
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.tempdir = None
|
|
|
|
self.num_tempfiles = 0
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if self.tempdir:
|
|
|
|
shutil.rmtree(self.tempdir)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_test_input(test_file_or_folder):
|
|
|
|
path, _ = os.path.split(__file__)
|
|
|
|
return os.path.join(path, "data", test_file_or_folder)
|
|
|
|
|
2017-03-05 13:44:32 -08:00
|
|
|
@staticmethod
|
|
|
|
def get_test_output(test_file_or_folder):
|
|
|
|
path, _ = os.path.split(__file__)
|
|
|
|
return os.path.join(path, "data", "test_results", test_file_or_folder)
|
|
|
|
|
2017-03-03 14:54:40 -08:00
|
|
|
@staticmethod
|
2017-03-06 00:59:21 -08:00
|
|
|
def get_file_list(folder, suffix, prefix=''):
|
2017-03-03 14:54:40 -08:00
|
|
|
all_files = os.listdir(folder)
|
2017-03-06 00:59:21 -08:00
|
|
|
file_list = []
|
|
|
|
for p in all_files:
|
|
|
|
if p.startswith(prefix) and p.endswith(suffix):
|
|
|
|
file_list.append(os.path.abspath(os.path.join(folder, p)))
|
|
|
|
return file_list
|
2017-03-03 14:54:40 -08:00
|
|
|
|
|
|
|
def temp_path(self, suffix):
|
|
|
|
self.temp_dir()
|
|
|
|
self.num_tempfiles += 1
|
|
|
|
return os.path.join(self.tempdir,
|
|
|
|
"tmp%d%s" % (self.num_tempfiles, suffix))
|
|
|
|
|
|
|
|
def temp_dir(self):
|
|
|
|
if not self.tempdir:
|
|
|
|
self.tempdir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
def read_ttx(self, path):
|
|
|
|
lines = []
|
|
|
|
with open(path, "r", encoding="utf-8") as ttx:
|
|
|
|
for line in ttx.readlines():
|
2021-07-30 03:58:20 +02:00
|
|
|
# Elide ttFont attributes because ttLibVersion may change.
|
2017-03-03 14:54:40 -08:00
|
|
|
if line.startswith("<ttFont "):
|
2021-07-30 03:58:20 +02:00
|
|
|
lines.append("<ttFont>\n")
|
2017-03-03 14:54:40 -08:00
|
|
|
else:
|
2021-07-30 03:58:20 +02:00
|
|
|
lines.append(line.rstrip() + "\n")
|
2017-03-03 14:54:40 -08:00
|
|
|
return lines
|
|
|
|
|
|
|
|
def expect_ttx(self, font, expected_ttx, tables):
|
|
|
|
path = self.temp_path(suffix=".ttx")
|
|
|
|
font.saveXML(path, tables=tables)
|
|
|
|
actual = self.read_ttx(path)
|
|
|
|
expected = self.read_ttx(expected_ttx)
|
|
|
|
if actual != expected:
|
|
|
|
for line in difflib.unified_diff(
|
|
|
|
expected, actual, fromfile=expected_ttx, tofile=path):
|
|
|
|
sys.stdout.write(line)
|
|
|
|
self.fail("TTX output is different from expected")
|
|
|
|
|
|
|
|
def compile_font(self, path, suffix, temp_dir):
|
|
|
|
ttx_filename = os.path.basename(path)
|
|
|
|
savepath = os.path.join(temp_dir, ttx_filename.replace('.ttx', suffix))
|
|
|
|
font = TTFont(recalcBBoxes=False, recalcTimestamp=False)
|
|
|
|
font.importXML(path)
|
|
|
|
font.save(savepath, reorderTables=None)
|
|
|
|
return font, savepath
|
|
|
|
|
|
|
|
# -----
|
|
|
|
# Tests
|
|
|
|
# -----
|
|
|
|
|
|
|
|
def test_varlib_mutator_ttf(self):
|
|
|
|
suffix = '.ttf'
|
2017-03-05 12:05:16 -08:00
|
|
|
ds_path = self.get_test_input('Build.designspace')
|
2017-03-03 14:54:40 -08:00
|
|
|
ufo_dir = self.get_test_input('master_ufo')
|
|
|
|
ttx_dir = self.get_test_input('master_ttx_interpolatable_ttf')
|
|
|
|
|
|
|
|
self.temp_dir()
|
2017-03-06 00:59:21 -08:00
|
|
|
ttx_paths = self.get_file_list(ttx_dir, '.ttx', 'TestFamily-')
|
2017-03-03 14:54:40 -08:00
|
|
|
for path in ttx_paths:
|
|
|
|
self.compile_font(path, suffix, self.tempdir)
|
|
|
|
|
|
|
|
finder = lambda s: s.replace(ufo_dir, self.tempdir).replace('.ufo', suffix)
|
|
|
|
varfont, _, _ = build(ds_path, finder)
|
2017-03-05 13:44:32 -08:00
|
|
|
varfont_name = 'Mutator'
|
|
|
|
varfont_path = os.path.join(self.tempdir, varfont_name + suffix)
|
2017-03-03 14:54:40 -08:00
|
|
|
varfont.save(varfont_path)
|
|
|
|
|
|
|
|
args = [varfont_path, 'wght=500', 'cntr=50']
|
|
|
|
mutator(args)
|
|
|
|
|
|
|
|
instfont_path = os.path.splitext(varfont_path)[0] + '-instance' + suffix
|
|
|
|
instfont = TTFont(instfont_path)
|
|
|
|
tables = [table_tag for table_tag in instfont.keys() if table_tag != 'head']
|
2017-03-06 00:59:21 -08:00
|
|
|
expected_ttx_path = self.get_test_output(varfont_name + '.ttx')
|
|
|
|
self.expect_ttx(instfont, expected_ttx_path, tables)
|
2018-11-15 15:18:03 -05:00
|
|
|
|
|
|
|
def test_varlib_mutator_getvar_ttf(self):
|
|
|
|
suffix = '.ttf'
|
|
|
|
ttx_dir = self.get_test_input('master_ttx_getvar_ttf')
|
|
|
|
|
|
|
|
self.temp_dir()
|
|
|
|
ttx_paths = self.get_file_list(ttx_dir, '.ttx', 'Mutator_Getvar')
|
|
|
|
for path in ttx_paths:
|
|
|
|
self.compile_font(path, suffix, self.tempdir)
|
|
|
|
|
|
|
|
varfont_name = 'Mutator_Getvar'
|
|
|
|
varfont_path = os.path.join(self.tempdir, varfont_name + suffix)
|
|
|
|
|
|
|
|
args = [varfont_path, 'wdth=80', 'ASCN=628']
|
|
|
|
mutator(args)
|
|
|
|
|
|
|
|
instfont_path = os.path.splitext(varfont_path)[0] + '-instance' + suffix
|
|
|
|
instfont = TTFont(instfont_path)
|
|
|
|
tables = [table_tag for table_tag in instfont.keys() if table_tag != 'head']
|
|
|
|
expected_ttx_path = self.get_test_output(varfont_name + '-instance.ttx')
|
|
|
|
self.expect_ttx(instfont, expected_ttx_path, tables)
|
2017-03-03 14:54:40 -08:00
|
|
|
|
2017-05-03 11:31:47 +02:00
|
|
|
def test_varlib_mutator_iup_ttf(self):
|
|
|
|
suffix = '.ttf'
|
|
|
|
ufo_dir = self.get_test_input('master_ufo')
|
|
|
|
ttx_dir = self.get_test_input('master_ttx_varfont_ttf')
|
|
|
|
|
|
|
|
self.temp_dir()
|
|
|
|
ttx_paths = self.get_file_list(ttx_dir, '.ttx', 'Mutator_IUP')
|
|
|
|
for path in ttx_paths:
|
|
|
|
self.compile_font(path, suffix, self.tempdir)
|
|
|
|
|
|
|
|
varfont_name = 'Mutator_IUP'
|
|
|
|
varfont_path = os.path.join(self.tempdir, varfont_name + suffix)
|
|
|
|
|
|
|
|
args = [varfont_path, 'wdth=80', 'ASCN=628']
|
|
|
|
mutator(args)
|
|
|
|
|
|
|
|
instfont_path = os.path.splitext(varfont_path)[0] + '-instance' + suffix
|
|
|
|
instfont = TTFont(instfont_path)
|
|
|
|
tables = [table_tag for table_tag in instfont.keys() if table_tag != 'head']
|
|
|
|
expected_ttx_path = self.get_test_output(varfont_name + '-instance.ttx')
|
|
|
|
self.expect_ttx(instfont, expected_ttx_path, tables)
|
|
|
|
|
2018-11-15 17:56:37 -08:00
|
|
|
def test_varlib_mutator_CFF2(self):
|
2019-05-01 16:01:43 -07:00
|
|
|
suffix = '.otf'
|
|
|
|
ttx_dir = self.get_test_input('master_ttx_varfont_otf')
|
|
|
|
|
|
|
|
self.temp_dir()
|
|
|
|
ttx_paths = self.get_file_list(ttx_dir, '.ttx', 'TestCFF2VF')
|
|
|
|
for path in ttx_paths:
|
|
|
|
self.compile_font(path, suffix, self.tempdir)
|
|
|
|
|
|
|
|
varfont_name = 'TestCFF2VF'
|
|
|
|
varfont_path = os.path.join(self.tempdir, varfont_name + suffix)
|
2018-11-15 17:56:37 -08:00
|
|
|
|
|
|
|
expected_ttx_name = 'InterpolateTestCFF2VF'
|
2018-11-20 12:44:21 -08:00
|
|
|
tables = ["hmtx", "CFF2"]
|
2018-11-15 17:56:37 -08:00
|
|
|
loc = {'wght':float(200)}
|
|
|
|
|
2019-05-01 16:01:43 -07:00
|
|
|
varfont = TTFont(varfont_path)
|
2018-11-15 17:56:37 -08:00
|
|
|
new_font = make_instance(varfont, loc)
|
|
|
|
expected_ttx_path = self.get_test_output(expected_ttx_name + '.ttx')
|
|
|
|
self.expect_ttx(new_font, expected_ttx_path, tables)
|
|
|
|
|
2017-03-03 14:54:40 -08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(unittest.main())
|