1#!/usr/bin/env python3 2 3# Runs a subsetting test suite. Compares the results of subsetting via harfbuzz 4# to subsetting via fonttools. 5 6from difflib import unified_diff 7import os 8import re 9import subprocess 10import sys 11import tempfile 12import shutil 13import io 14 15from repack_test import RepackTest 16 17try: 18 from fontTools.ttLib import TTFont 19except ImportError: 20 print ("fonttools is not present, skipping test.") 21 sys.exit (77) 22 23ots_sanitize = shutil.which ("ots-sanitize") 24 25def cmd (command): 26 p = subprocess.Popen ( 27 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, 28 universal_newlines=True) 29 (stdoutdata, stderrdata) = p.communicate () 30 print (stderrdata, end="", file=sys.stderr) 31 return stdoutdata, p.returncode 32 33def fail_test (test, cli_args, message): 34 print ('ERROR: %s' % message) 35 print ('Test State:') 36 print (' test.font_name %s' % test.font_name) 37 print (' test.test_path %s' % os.path.abspath (test.test_path)) 38 return 1 39 40def run_test (test, should_check_ots): 41 out_file = os.path.join (tempfile.mkdtemp (), test.font_name + '-subset.ttf') 42 cli_args = [hb_subset, 43 "--font-file=" + test.font_path (), 44 "--output-file=" + out_file, 45 "--unicodes=%s" % test.codepoints_string (), 46 "--drop-tables-=GPOS,GSUB,GDEF",] 47 print (' '.join (cli_args)) 48 _, return_code = cmd (cli_args) 49 50 if return_code: 51 return fail_test (test, cli_args, "%s returned %d" % (' '.join (cli_args), return_code)) 52 53 try: 54 with TTFont (out_file) as font: 55 pass 56 except Exception as e: 57 print (e) 58 return fail_test (test, cli_args, "ttx failed to parse the result") 59 60 if should_check_ots: 61 print ("Checking output with ots-sanitize.") 62 if not check_ots (out_file): 63 return fail_test (test, cli_args, 'ots for subsetted file fails.') 64 65 return 0 66 67def has_ots (): 68 if not ots_sanitize: 69 print ("OTS is not present, skipping all ots checks.") 70 return False 71 return True 72 73def check_ots (path): 74 ots_report, returncode = cmd ([ots_sanitize, path]) 75 if returncode: 76 print ("OTS Failure: %s" % ots_report) 77 return False 78 return True 79 80args = sys.argv[1:] 81if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]): 82 sys.exit ("First argument does not seem to point to usable hb-subset.") 83hb_subset, args = args[0], args[1:] 84 85if len (args) != 1: 86 sys.exit ("No tests supplied.") 87 88has_ots = has_ots() 89 90fails = 0 91 92path = args[0] 93if not path.endswith(".tests"): 94 sys.exit ("Not a valid test case path.") 95 96with open (path, mode="r", encoding="utf-8") as f: 97 # TODO(garretrieger): re-enable OTS checking. 98 fails += run_test (RepackTest (path, f.read ()), False) 99 100 101if fails != 0: 102 sys.exit ("%d test(s) failed." % fails) 103else: 104 print ("All tests passed.") 105