2018-01-27 01:57:42 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Runs a subsetting test suite. Compares the results of subsetting via harfbuz
|
|
|
|
# to subsetting via fonttools.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import io
|
|
|
|
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
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
from subset_test_suite import SubsetTestSuite
|
|
|
|
|
|
|
|
|
|
|
|
def cmd(command):
|
|
|
|
p = subprocess.Popen (
|
|
|
|
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
p.wait ()
|
|
|
|
print (p.stderr.read (), end="") # file=sys.stderr
|
|
|
|
return p.stdout.read (), p.returncode
|
|
|
|
|
2018-01-31 04:27:11 +01:00
|
|
|
def read_binary(file_path):
|
|
|
|
with open(file_path, 'rb') as f:
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
def fail_test(test, cli_args, message):
|
|
|
|
print ('ERROR: %s' % message)
|
|
|
|
print ('Test State:')
|
|
|
|
print (' test.font_path %s' % os.path.abspath(test.font_path))
|
|
|
|
print (' test.profile_path %s' % os.path.abspath(test.profile_path))
|
2018-02-02 03:22:14 +01:00
|
|
|
print (' test.unicodes %s' % test.unicodes())
|
2018-01-31 04:27:11 +01:00
|
|
|
expected_file = os.path.join(test_suite.get_output_directory(),
|
2018-02-02 03:22:14 +01:00
|
|
|
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
|
|
|
|
|
|
|
def run_test(test):
|
2018-02-02 03:22:14 +01:00
|
|
|
out_file = os.path.join(tempfile.mkdtemp(), test.get_font_name() + '-subset.ttf')
|
|
|
|
cli_args = [hb_subset,
|
|
|
|
"--font-file=" + test.font_path,
|
|
|
|
"--output-file=" + out_file,
|
|
|
|
"--unicodes=%s" % test.unicodes()]
|
2018-02-12 02:46:06 +01:00
|
|
|
print (' '.join(cli_args))
|
2018-01-31 04:27:11 +01:00
|
|
|
_, return_code = cmd(cli_args)
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
if return_code:
|
2018-01-31 04:27:11 +01:00
|
|
|
return fail_test(test, cli_args, "%s returned %d" % (' '.join(cli_args), return_code))
|
|
|
|
|
2018-02-08 20:31:27 +01:00
|
|
|
expected_ttx, return_code = run_ttx(os.path.join(test_suite.get_output_directory(),
|
2018-02-02 03:22:14 +01:00
|
|
|
test.get_font_name()))
|
2018-02-08 20:31:27 +01:00
|
|
|
if return_code:
|
2018-02-08 20:30:36 +01:00
|
|
|
return fail_test(test, cli_args, "ttx (expected) returned %d" % (return_code))
|
2018-01-31 04:27:11 +01:00
|
|
|
|
2018-02-08 20:31:27 +01:00
|
|
|
actual_ttx, return_code = run_ttx(out_file)
|
|
|
|
if return_code:
|
2018-02-08 20:30:36 +01:00
|
|
|
return fail_test(test, cli_args, "ttx (actual) returned %d" % (return_code))
|
2018-01-31 04:27:11 +01:00
|
|
|
|
2018-02-15 02:00:18 +01:00
|
|
|
expected_ttx = strip_check_sum (expected_ttx)
|
|
|
|
actual_ttx = strip_check_sum (expected_ttx)
|
|
|
|
|
2018-02-08 20:30:36 +01:00
|
|
|
if not actual_ttx == expected_ttx:
|
|
|
|
return fail_test(test, cli_args, 'ttx for expected and actual does not match.')
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2018-02-08 20:30:36 +01:00
|
|
|
def run_ttx(file):
|
2018-02-08 20:31:27 +01:00
|
|
|
cli_args = ["ttx",
|
|
|
|
"-o-",
|
|
|
|
file]
|
|
|
|
return cmd(cli_args)
|
2018-02-08 20:30:36 +01:00
|
|
|
|
2018-02-15 02:00:18 +01:00
|
|
|
def strip_check_sum (ttx_string):
|
|
|
|
return re.sub ('checksumAdjustment value=["]0x(\d+)["]',
|
|
|
|
'checksumAdjustment value="0x00000000"',
|
|
|
|
ttx_string, count=1)
|
2018-01-27 01:57:42 +01:00
|
|
|
|
|
|
|
args = sys.argv[1:]
|
|
|
|
if not args or sys.argv[1].find('hb-subset') == -1 or not os.path.exists (sys.argv[1]):
|
|
|
|
print ("First argument does not seem to point to usable hb-subset.")
|
|
|
|
sys.exit (1)
|
|
|
|
hb_subset, args = args[0], args[1:]
|
|
|
|
|
|
|
|
if not len(args):
|
|
|
|
print ("No tests supplied.")
|
|
|
|
sys.exit (1)
|
|
|
|
|
2018-02-08 23:26:18 +01:00
|
|
|
_, returncode = cmd(["which", "ttx"])
|
|
|
|
if returncode:
|
2018-02-09 00:44:24 +01:00
|
|
|
print("TTX is not present, skipping test.")
|
2018-02-09 00:38:58 +01:00
|
|
|
sys.exit (77)
|
2018-02-08 23:26:18 +01:00
|
|
|
|
2018-01-27 01:57:42 +01:00
|
|
|
fails = 0
|
|
|
|
for path in args:
|
|
|
|
with io.open(path, mode="r", encoding="utf-8") as f:
|
|
|
|
print ("Running tests in " + path)
|
|
|
|
test_suite = SubsetTestSuite(path, f.read())
|
|
|
|
for test in test_suite.tests():
|
|
|
|
fails += run_test(test)
|
|
|
|
|
|
|
|
if fails != 0:
|
|
|
|
print (str (fails) + " test(s) failed.")
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
print ("All tests passed.")
|