2020-02-19 12:26:55 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-01-27 01:57:42 +01:00
|
|
|
|
2019-05-28 11:18:39 +02:00
|
|
|
# Runs a subsetting test suite. Compares the results of subsetting via harfbuzz
|
2018-01-27 01:57:42 +01:00
|
|
|
# to subsetting via fonttools.
|
|
|
|
|
2018-02-22 23:07:52 +01:00
|
|
|
from difflib import unified_diff
|
2018-01-27 01:57:42 +01:00
|
|
|
import os
|
2018-02-15 02:00:18 +01:00
|
|
|
import re
|
2018-01-27 01:57:42 +01:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2018-01-31 04:27:11 +01:00
|
|
|
import tempfile
|
2020-03-18 21:28:20 +01:00
|
|
|
import shutil
|
2020-07-27 06:33:35 +02:00
|
|
|
import io
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
from subset_test_suite import SubsetTestSuite
|
2020-07-27 06:33:35 +02:00
|
|
|
|
2020-07-26 14:46:07 +02:00
|
|
|
try:
|
2020-07-27 06:33:35 +02:00
|
|
|
from fontTools.ttLib import TTFont
|
2020-07-26 14:46:07 +02:00
|
|
|
except ImportError:
|
2021-08-04 04:20:14 +02:00
|
|
|
TTFont = None
|
2018-01-27 01:57:42 +01:00
|
|
|
|
2020-07-26 14:46:07 +02:00
|
|
|
ots_sanitize = shutil.which ("ots-sanitize")
|
|
|
|
|
2021-08-01 11:11:12 +02:00
|
|
|
def subset_cmd (command):
|
2021-08-10 19:05:40 +02:00
|
|
|
global hb_subset, process
|
|
|
|
print (hb_subset + ' ' + " ".join(command))
|
|
|
|
process.stdin.write ((';'.join (command) + '\n').encode ("utf-8"))
|
2021-08-01 11:11:12 +02:00
|
|
|
process.stdin.flush ()
|
|
|
|
return process.stdout.readline().decode ("utf-8").strip ()
|
|
|
|
|
2020-04-20 14:37:09 +02:00
|
|
|
def cmd (command):
|
2018-01-27 01:57:42 +01:00
|
|
|
p = subprocess.Popen (
|
2020-04-18 20:28:25 +02:00
|
|
|
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
universal_newlines=True)
|
2018-06-25 16:15:49 +02:00
|
|
|
(stdoutdata, stderrdata) = p.communicate ()
|
2020-05-28 21:13:55 +02:00
|
|
|
print (stderrdata, end="", file=sys.stderr)
|
2018-03-07 02:47:40 +01:00
|
|
|
return stdoutdata, p.returncode
|
2018-01-27 01:57:42 +01:00
|
|
|
|
2019-05-28 11:18:39 +02:00
|
|
|
def fail_test (test, cli_args, message):
|
2018-01-31 04:27:11 +01:00
|
|
|
print ('ERROR: %s' % message)
|
|
|
|
print ('Test State:')
|
2018-06-25 16:15:49 +02:00
|
|
|
print (' test.font_path %s' % os.path.abspath (test.font_path))
|
|
|
|
print (' test.profile_path %s' % os.path.abspath (test.profile_path))
|
|
|
|
print (' test.unicodes %s' % test.unicodes ())
|
2020-07-27 06:49:29 +02:00
|
|
|
expected_file = os.path.join (test_suite.get_output_directory (),
|
2021-08-04 00:07:23 +02:00
|
|
|
test.get_font_name ())
|
2018-06-25 16:15:49 +02:00
|
|
|
print (' expected_file %s' % os.path.abspath (expected_file))
|
2018-01-31 04:27:11 +01:00
|
|
|
return 1
|
2018-01-27 01:57:42 +01:00
|
|
|
|
2022-12-05 21:18:41 +01:00
|
|
|
def run_test (test, should_check_ots, preprocess):
|
2019-05-28 11:18:39 +02:00
|
|
|
out_file = os.path.join (tempfile.mkdtemp (), test.get_font_name () + '-subset' + test.get_font_extension ())
|
2021-08-10 19:05:40 +02:00
|
|
|
cli_args = ["--font-file=" + test.font_path,
|
2018-02-02 03:22:14 +01:00
|
|
|
"--output-file=" + out_file,
|
2019-05-14 22:55:11 +02:00
|
|
|
"--unicodes=%s" % test.unicodes (),
|
2021-04-02 00:34:17 +02:00
|
|
|
"--drop-tables+=DSIG",
|
2020-07-27 06:49:29 +02:00
|
|
|
"--drop-tables-=sbix"]
|
2022-12-05 21:18:41 +01:00
|
|
|
if preprocess:
|
|
|
|
cli_args.extend(["--preprocess-face",])
|
|
|
|
|
2018-06-25 16:15:49 +02:00
|
|
|
cli_args.extend (test.get_profile_flags ())
|
2022-07-01 01:22:15 +02:00
|
|
|
if test.get_instance_flags ():
|
2022-08-26 17:34:12 +02:00
|
|
|
cli_args.extend (["--instance=%s" % ','.join(test.get_instance_flags ())])
|
2021-08-01 11:11:12 +02:00
|
|
|
ret = subset_cmd (cli_args)
|
2018-01-27 01:57:42 +01:00
|
|
|
|
2021-08-01 11:11:12 +02:00
|
|
|
if ret != "success":
|
|
|
|
return fail_test (test, cli_args, "%s failed" % ' '.join (cli_args))
|
2018-01-31 04:27:11 +01:00
|
|
|
|
2021-08-04 03:57:18 +02:00
|
|
|
expected_file = os.path.join (test_suite.get_output_directory (), test.get_font_name ())
|
|
|
|
with open (expected_file, "rb") as fp:
|
2022-02-12 21:52:42 +01:00
|
|
|
expected_contents = fp.read()
|
2021-08-04 03:57:18 +02:00
|
|
|
with open (out_file, "rb") as fp:
|
2022-02-12 21:52:42 +01:00
|
|
|
actual_contents = fp.read()
|
2021-08-04 03:57:18 +02:00
|
|
|
|
2022-02-12 21:52:42 +01:00
|
|
|
if expected_contents == actual_contents:
|
2021-08-04 12:08:18 +02:00
|
|
|
if should_check_ots:
|
|
|
|
print ("Checking output with ots-sanitize.")
|
|
|
|
if not check_ots (out_file):
|
|
|
|
return fail_test (test, cli_args, 'ots for subsetted file fails.')
|
2021-08-04 03:57:18 +02:00
|
|
|
return 0
|
|
|
|
|
2021-08-04 04:20:14 +02:00
|
|
|
if TTFont is None:
|
2021-08-04 12:08:18 +02:00
|
|
|
print ("fonttools is not present, skipping TTX diff.")
|
|
|
|
return fail_test (test, cli_args, "hash for expected and actual does not match.")
|
|
|
|
|
|
|
|
with io.StringIO () as fp:
|
|
|
|
try:
|
|
|
|
with TTFont (expected_file) as font:
|
|
|
|
font.saveXML (fp)
|
|
|
|
except Exception as e:
|
|
|
|
print (e)
|
|
|
|
return fail_test (test, cli_args, "ttx failed to parse the expected result")
|
|
|
|
expected_ttx = fp.getvalue ()
|
|
|
|
|
|
|
|
with io.StringIO () as fp:
|
|
|
|
try:
|
|
|
|
with TTFont (out_file) as font:
|
|
|
|
font.saveXML (fp)
|
|
|
|
except Exception as e:
|
|
|
|
print (e)
|
|
|
|
return fail_test (test, cli_args, "ttx failed to parse the actual result")
|
|
|
|
actual_ttx = fp.getvalue ()
|
|
|
|
|
|
|
|
if actual_ttx != expected_ttx:
|
|
|
|
for line in unified_diff (expected_ttx.splitlines (1), actual_ttx.splitlines (1)):
|
2018-06-25 16:15:49 +02:00
|
|
|
sys.stdout.write (line)
|
|
|
|
sys.stdout.flush ()
|
2019-05-28 11:18:39 +02:00
|
|
|
return fail_test (test, cli_args, 'ttx for expected and actual does not match.')
|
2018-01-27 01:57:42 +01:00
|
|
|
|
2021-08-05 00:05:26 +02:00
|
|
|
return fail_test (test, cli_args, 'hash for expected and actual does not match, '
|
|
|
|
'but the ttx matches. Expected file needs to be updated?')
|
|
|
|
|
2018-01-27 01:57:42 +01:00
|
|
|
|
2018-03-09 13:13:03 +01:00
|
|
|
def has_ots ():
|
2018-06-25 16:15:49 +02:00
|
|
|
if not ots_sanitize:
|
2020-07-27 06:49:29 +02:00
|
|
|
print ("OTS is not present, skipping all ots checks.")
|
2018-03-01 00:44:00 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def check_ots (path):
|
2018-06-25 16:15:49 +02:00
|
|
|
ots_report, returncode = cmd ([ots_sanitize, path])
|
2018-03-01 00:44:00 +01:00
|
|
|
if returncode:
|
2020-05-28 22:12:32 +02:00
|
|
|
print ("OTS Failure: %s" % ots_report)
|
2018-03-01 00:44:00 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2018-01-27 01:57:42 +01:00
|
|
|
args = sys.argv[1:]
|
2019-05-28 11:18:39 +02:00
|
|
|
if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]):
|
2020-05-28 20:21:29 +02:00
|
|
|
sys.exit ("First argument does not seem to point to usable hb-subset.")
|
2018-01-27 01:57:42 +01:00
|
|
|
hb_subset, args = args[0], args[1:]
|
|
|
|
|
2018-06-25 16:15:49 +02:00
|
|
|
if not len (args):
|
2020-05-28 20:21:29 +02:00
|
|
|
sys.exit ("No tests supplied.")
|
2018-01-27 01:57:42 +01:00
|
|
|
|
2018-03-01 00:44:00 +01:00
|
|
|
has_ots = has_ots()
|
|
|
|
|
2021-08-01 11:11:12 +02:00
|
|
|
process = subprocess.Popen ([hb_subset, '--batch'],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=sys.stdout)
|
|
|
|
|
2018-01-27 01:57:42 +01:00
|
|
|
fails = 0
|
|
|
|
for path in args:
|
2020-05-28 21:41:19 +02:00
|
|
|
with open (path, mode="r", encoding="utf-8") as f:
|
2018-01-27 01:57:42 +01:00
|
|
|
print ("Running tests in " + path)
|
2019-05-28 11:18:39 +02:00
|
|
|
test_suite = SubsetTestSuite (path, f.read ())
|
2018-06-25 16:15:49 +02:00
|
|
|
for test in test_suite.tests ():
|
2022-12-05 21:18:41 +01:00
|
|
|
# Tests are run with and without preprocessing, results should be the
|
|
|
|
# same between them.
|
|
|
|
fails += run_test (test, has_ots, False)
|
|
|
|
fails += run_test (test, has_ots, True)
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
if fails != 0:
|
2020-05-28 20:21:29 +02:00
|
|
|
sys.exit ("%d test(s) failed." % fails)
|
2018-01-27 01:57:42 +01:00
|
|
|
else:
|
|
|
|
print ("All tests passed.")
|