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.
|
|
|
|
|
|
|
|
import io
|
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
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
from subset_test_suite import SubsetTestSuite
|
|
|
|
|
2020-03-18 21:28:20 +01:00
|
|
|
fonttools = shutil.which ("fonttools")
|
|
|
|
ots_sanitize = shutil.which ("ots-sanitize")
|
2018-06-25 16:15:49 +02:00
|
|
|
|
2019-05-03 01:19:34 +02:00
|
|
|
if not fonttools:
|
2019-05-28 11:18:39 +02:00
|
|
|
print ("fonttools is not present, skipping test.")
|
2018-06-25 16:15:49 +02:00
|
|
|
sys.exit (77)
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
def cmd(command):
|
|
|
|
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 ()
|
2018-03-07 02:47:40 +01:00
|
|
|
print (stderrdata, end="") # file=sys.stderr
|
|
|
|
return stdoutdata, p.returncode
|
2018-01-27 01:57:42 +01:00
|
|
|
|
2018-06-25 16:15:49 +02:00
|
|
|
def read_binary (file_path):
|
|
|
|
with open (file_path, 'rb') as f:
|
|
|
|
return f.read ()
|
2018-01-31 04:27:11 +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 ())
|
|
|
|
expected_file = os.path.join(test_suite.get_output_directory (),
|
|
|
|
test.get_font_name ())
|
|
|
|
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
|
|
|
|
2019-05-28 11:18:39 +02:00
|
|
|
def run_test (test, should_check_ots):
|
|
|
|
out_file = os.path.join (tempfile.mkdtemp (), test.get_font_name () + '-subset' + test.get_font_extension ())
|
2018-02-02 03:22:14 +01:00
|
|
|
cli_args = [hb_subset,
|
|
|
|
"--font-file=" + test.font_path,
|
|
|
|
"--output-file=" + out_file,
|
2019-05-14 22:55:11 +02:00
|
|
|
"--unicodes=%s" % test.unicodes (),
|
2020-01-09 16:44:20 +01:00
|
|
|
"--drop-tables+=DSIG,GPOS,GSUB,GDEF",
|
|
|
|
"--drop-tables-=sbix"]
|
2018-06-25 16:15:49 +02:00
|
|
|
cli_args.extend (test.get_profile_flags ())
|
|
|
|
print (' '.join (cli_args))
|
|
|
|
_, return_code = cmd (cli_args)
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
if return_code:
|
2018-06-25 16:15:49 +02:00
|
|
|
return fail_test (test, cli_args, "%s returned %d" % (' '.join (cli_args), return_code))
|
2018-01-31 04:27:11 +01:00
|
|
|
|
2018-06-25 16:15:49 +02:00
|
|
|
expected_ttx, return_code = run_ttx (os.path.join (test_suite.get_output_directory (),
|
2019-05-28 11:18:39 +02:00
|
|
|
test.get_font_name ()))
|
2018-02-08 20:31:27 +01:00
|
|
|
if return_code:
|
2018-06-25 16:15:49 +02:00
|
|
|
return fail_test (test, cli_args, "ttx (expected) returned %d" % (return_code))
|
2018-01-31 04:27:11 +01:00
|
|
|
|
2019-05-28 11:18:39 +02:00
|
|
|
actual_ttx, return_code = run_ttx (out_file)
|
2018-02-08 20:31:27 +01:00
|
|
|
if return_code:
|
2018-06-25 16:15:49 +02:00
|
|
|
return fail_test (test, cli_args, "ttx (actual) returned %d" % (return_code))
|
2018-01-31 04:27:11 +01:00
|
|
|
|
2018-03-07 00:43:08 +01:00
|
|
|
print ("stripping checksums.")
|
2018-02-15 02:00:18 +01:00
|
|
|
expected_ttx = strip_check_sum (expected_ttx)
|
2018-02-22 23:07:52 +01:00
|
|
|
actual_ttx = strip_check_sum (actual_ttx)
|
2018-02-15 02:00:18 +01:00
|
|
|
|
2018-02-08 20:30:36 +01:00
|
|
|
if not actual_ttx == expected_ttx:
|
2018-06-25 16:15:49 +02:00
|
|
|
for line in unified_diff (expected_ttx.splitlines (1), actual_ttx.splitlines (1)):
|
|
|
|
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
|
|
|
|
2018-03-01 00:44:00 +01:00
|
|
|
if should_check_ots:
|
|
|
|
print ("Checking output with ots-sanitize.")
|
2018-06-25 16:15:49 +02:00
|
|
|
if not check_ots (out_file):
|
|
|
|
return fail_test (test, cli_args, 'ots for subsetted file fails.')
|
2018-03-01 00:44:00 +01:00
|
|
|
|
2018-01-27 01:57:42 +01:00
|
|
|
return 0
|
|
|
|
|
2018-06-25 16:15:49 +02:00
|
|
|
def run_ttx (file):
|
2019-05-03 01:19:34 +02:00
|
|
|
print ("fonttools ttx %s" % file)
|
2019-05-28 11:18:39 +02:00
|
|
|
return cmd ([fonttools, "ttx", "-q", "-o-", file])
|
2018-02-08 20:30:36 +01:00
|
|
|
|
2018-02-15 02:00:18 +01:00
|
|
|
def strip_check_sum (ttx_string):
|
2018-02-22 23:28:18 +01:00
|
|
|
return re.sub ('checkSumAdjustment value=["]0x([0-9a-fA-F])+["]',
|
2018-02-27 22:15:40 +01:00
|
|
|
'checkSumAdjustment value="0x00000000"',
|
2020-04-18 20:28:25 +02:00
|
|
|
ttx_string, count=1)
|
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:
|
2018-03-01 00:44:00 +01:00
|
|
|
print("OTS is not present, skipping all ots checks.")
|
|
|
|
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:
|
|
|
|
print("OTS Failure: %s" % ots_report);
|
|
|
|
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]):
|
2018-01-27 01:57:42 +01:00
|
|
|
print ("First argument does not seem to point to usable hb-subset.")
|
|
|
|
sys.exit (1)
|
|
|
|
hb_subset, args = args[0], args[1:]
|
|
|
|
|
2018-06-25 16:15:49 +02:00
|
|
|
if not len (args):
|
2018-01-27 01:57:42 +01:00
|
|
|
print ("No tests supplied.")
|
|
|
|
sys.exit (1)
|
|
|
|
|
2018-03-01 00:44:00 +01:00
|
|
|
has_ots = has_ots()
|
|
|
|
|
2018-01-27 01:57:42 +01:00
|
|
|
fails = 0
|
|
|
|
for path in args:
|
2018-06-25 16:15:49 +02:00
|
|
|
with io.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 ():
|
|
|
|
fails += run_test (test, has_ots)
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
if fails != 0:
|
|
|
|
print (str (fails) + " test(s) failed.")
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
print ("All tests passed.")
|