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