1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
from fontTools.ttLib import TTFont
from fontTools.varLib.interpolatable import main as interpolatable_main
import os
import shutil
import sys
import tempfile
import unittest
import pytest
try:
import scipy
except:
scipy = None
try:
import munkres
except ImportError:
munkres = None
@unittest.skipUnless(scipy or munkres, "scipy or munkres not installed")
class InterpolatableTest(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)
@staticmethod
def get_file_list(folder, suffix, prefix=""):
all_files = os.listdir(folder)
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 sorted(file_list)
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 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_interpolatable_ttf(self):
suffix = ".ttf"
ttx_dir = self.get_test_input("master_ttx_interpolatable_ttf")
self.temp_dir()
ttx_paths = self.get_file_list(ttx_dir, ".ttx", "TestFamily2-")
for path in ttx_paths:
self.compile_font(path, suffix, self.tempdir)
ttf_paths = self.get_file_list(self.tempdir, suffix)
self.assertIsNone(interpolatable_main(ttf_paths))
def test_interpolatable_otf(self):
suffix = ".otf"
ttx_dir = self.get_test_input("master_ttx_interpolatable_otf")
self.temp_dir()
ttx_paths = self.get_file_list(ttx_dir, ".ttx", "TestFamily2-")
for path in ttx_paths:
self.compile_font(path, suffix, self.tempdir)
otf_paths = self.get_file_list(self.tempdir, suffix)
self.assertIsNone(interpolatable_main(otf_paths))
def test_interpolatable_cff2(self):
suffix = ".otf"
ttx_dir = self.get_test_input("variable_ttx_interpolatable_cff2")
ttx_path = os.path.abspath(os.path.join(ttx_dir, "interpolatable-test.ttx"))
self.temp_dir()
self.compile_font(ttx_path, suffix, self.tempdir)
otf_path = self.get_file_list(self.tempdir, suffix)[0]
problems = interpolatable_main([otf_path])
print(problems)
self.assertEqual(
problems["uni0408"],
[
{
"type": "underweight",
"contour": 0,
"master_1": "'wght=200.0 opsz=20.0'",
"master_2": "'wght=200.0 opsz=60.0'",
"master_1_idx": 2,
"master_2_idx": 3,
"tolerance": pytest.approx(0.9184032411892079),
},
],
)
def test_interpolatable_ufo(self):
ttx_dir = self.get_test_input("master_ufo")
ufo_paths = self.get_file_list(ttx_dir, ".ufo", "TestFamily2-")
self.assertIsNone(interpolatable_main(ufo_paths))
def test_designspace(self):
designspace_path = self.get_test_input("InterpolateLayout.designspace")
self.assertIsNone(interpolatable_main([designspace_path]))
def test_glyphsapp(self):
pytest.importorskip("glyphsLib")
glyphsapp_path = self.get_test_input("InterpolateLayout.glyphs")
self.assertIsNone(interpolatable_main([glyphsapp_path]))
def test_VF(self):
suffix = ".ttf"
ttx_dir = self.get_test_input("master_ttx_varfont_ttf")
self.temp_dir()
ttx_paths = self.get_file_list(ttx_dir, ".ttx", "SparseMasters-")
for path in ttx_paths:
self.compile_font(path, suffix, self.tempdir)
ttf_paths = self.get_file_list(self.tempdir, suffix)
problems = interpolatable_main(["--quiet"] + ttf_paths)
self.assertIsNone(problems)
def test_sparse_interpolatable_ttfs(self):
suffix = ".ttf"
ttx_dir = self.get_test_input("master_ttx_interpolatable_ttf")
self.temp_dir()
ttx_paths = self.get_file_list(ttx_dir, ".ttx", "SparseMasters-")
for path in ttx_paths:
self.compile_font(path, suffix, self.tempdir)
ttf_paths = self.get_file_list(self.tempdir, suffix)
# without --ignore-missing
problems = interpolatable_main(["--quiet"] + ttf_paths)
self.assertEqual(
problems["a"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["s"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["edotabove"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["dotabovecomb"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
# normal order, with --ignore-missing
self.assertIsNone(interpolatable_main(["--ignore-missing"] + ttf_paths))
# purposely putting the sparse master (medium) first
self.assertIsNone(
interpolatable_main(
["--ignore-missing"] + [ttf_paths[1]] + [ttf_paths[0]] + [ttf_paths[2]]
)
)
# purposely putting the sparse master (medium) last
self.assertIsNone(
interpolatable_main(
["--ignore-missing"] + [ttf_paths[0]] + [ttf_paths[2]] + [ttf_paths[1]]
)
)
def test_sparse_interpolatable_ufos(self):
ttx_dir = self.get_test_input("master_ufo")
ufo_paths = self.get_file_list(ttx_dir, ".ufo", "SparseMasters-")
# without --ignore-missing
problems = interpolatable_main(["--quiet"] + ufo_paths)
self.assertEqual(
problems["a"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["s"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["edotabove"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["dotabovecomb"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
# normal order, with --ignore-missing
self.assertIsNone(interpolatable_main(["--ignore-missing"] + ufo_paths))
# purposely putting the sparse master (medium) first
self.assertIsNone(
interpolatable_main(
["--ignore-missing"] + [ufo_paths[1]] + [ufo_paths[0]] + [ufo_paths[2]]
)
)
# purposely putting the sparse master (medium) last
self.assertIsNone(
interpolatable_main(
["--ignore-missing"] + [ufo_paths[0]] + [ufo_paths[2]] + [ufo_paths[1]]
)
)
def test_sparse_designspace(self):
designspace_path = self.get_test_input("SparseMasters_ufo.designspace")
problems = interpolatable_main(["--quiet", designspace_path])
self.assertEqual(
problems["a"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["s"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["edotabove"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["dotabovecomb"],
[{"type": "missing", "master": "SparseMasters-Medium", "master_idx": 1}],
)
# normal order, with --ignore-missing
self.assertIsNone(interpolatable_main(["--ignore-missing", designspace_path]))
def test_sparse_glyphsapp(self):
pytest.importorskip("glyphsLib")
glyphsapp_path = self.get_test_input("SparseMasters.glyphs")
problems = interpolatable_main(["--quiet", glyphsapp_path])
self.assertEqual(
problems["a"],
[{"type": "missing", "master": "Sparse Masters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["s"],
[{"type": "missing", "master": "Sparse Masters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["edotabove"],
[{"type": "missing", "master": "Sparse Masters-Medium", "master_idx": 1}],
)
self.assertEqual(
problems["dotabovecomb"],
[{"type": "missing", "master": "Sparse Masters-Medium", "master_idx": 1}],
)
# normal order, with --ignore-missing
self.assertIsNone(interpolatable_main(["--ignore-missing", glyphsapp_path]))
def test_interpolatable_varComposite(self):
input_path = self.get_test_input(
"..", "..", "ttLib", "data", "varc-ac00-ac01.ttf"
)
# Just make sure the code runs.
interpolatable_main((input_path,))
if __name__ == "__main__":
sys.exit(unittest.main())
|