1#!/usr/bin/env python3 2 3import sys, os, subprocess, hashlib 4 5def shape_cmd(command): 6 global hb_shape, process 7 print (hb_shape + ' ' + " ".join(command)) 8 process.stdin.write ((';'.join (command) + '\n').encode ("utf-8")) 9 process.stdin.flush () 10 return process.stdout.readline().decode ("utf-8").strip () 11 12args = sys.argv[1:] 13 14have_freetype = int(os.getenv ('HAVE_FREETYPE', 1)) 15have_coretext = int(os.getenv ('HAVE_CORETEXT', 0)) 16have_directwrite = int(os.getenv ('HAVE_DIRECTWRITE', 0)) 17have_uniscribe = int(os.getenv ('HAVE_UNISCRIBE', 0)) 18 19if not args or args[0].find('hb-shape') == -1 or not os.path.exists (args[0]): 20 sys.exit ("""First argument does not seem to point to usable hb-shape.""") 21hb_shape, args = args[0], args[1:] 22 23env = os.environ.copy() 24env['LC_ALL'] = 'C' 25process = subprocess.Popen ([hb_shape, '--batch'], 26 stdin=subprocess.PIPE, 27 stdout=subprocess.PIPE, 28 stderr=sys.stdout, 29 env=env) 30 31passes = 0 32fails = 0 33skips = 0 34 35if not len (args): 36 args = ['-'] 37 38for filename in args: 39 if filename == '-': 40 print ("Running tests from standard input") 41 else: 42 print ("Running tests in " + filename) 43 44 if filename == '-': 45 f = sys.stdin 46 else: 47 f = open (filename, encoding='utf8') 48 49 for line in f: 50 comment = False 51 if line.startswith ("#"): 52 comment = True 53 line = line[1:] 54 55 if line.startswith (' '): 56 print ("#%s" % line) 57 continue 58 59 line = line.strip () 60 if not line: 61 continue 62 63 fontfile, options, unicodes, glyphs_expected = line.split (';') 64 options = options.split () 65 if fontfile.startswith ('/') or fontfile.startswith ('"/'): 66 if os.name == 'nt': # Skip on Windows 67 continue 68 69 fontfile, expected_hash = (fontfile.split('@') + [''])[:2] 70 71 try: 72 with open (fontfile, 'rb') as ff: 73 if expected_hash: 74 actual_hash = hashlib.sha1 (ff.read()).hexdigest ().strip () 75 if actual_hash != expected_hash: 76 print ('different version of %s found; Expected hash %s, got %s; skipping.' % 77 (fontfile, expected_hash, actual_hash)) 78 skips += 1 79 continue 80 except IOError: 81 print ('%s not found, skip.' % fontfile) 82 skips += 1 83 continue 84 else: 85 cwd = os.path.dirname(filename) 86 fontfile = os.path.normpath (os.path.join (cwd, fontfile)) 87 88 extra_options = ["--shaper=ot"] 89 if glyphs_expected != '*': 90 extra_options.append("--verify") 91 extra_options.append("--unsafe-to-concat") 92 93 if comment: 94 print ('# %s "%s" --unicodes %s' % (hb_shape, fontfile, unicodes)) 95 continue 96 97 if "--font-funcs=ft" in options and not have_freetype: 98 skips += 1 99 continue 100 101 if "--shaper=coretext" in options and not have_coretext: 102 skips += 1 103 continue 104 105 if "--shaper=directwrite" in options and not have_directwrite: 106 skips += 1 107 continue 108 109 if "--shaper=uniscribe" in options and not have_uniscribe: 110 skips += 1 111 continue 112 113 if "--font-funcs=ot" in options or not have_freetype: 114 glyphs1 = shape_cmd ([fontfile, "--font-funcs=ot"] + extra_options + ["--unicodes", unicodes] + options) 115 else: 116 glyphs1 = shape_cmd ([fontfile, "--font-funcs=ft"] + extra_options + ["--unicodes", unicodes] + options) 117 glyphs2 = shape_cmd ([fontfile, "--font-funcs=ot"] + extra_options + ["--unicodes", unicodes] + options) 118 119 if glyphs1 != glyphs2 and glyphs_expected != '*': 120 print ("FT funcs: " + glyphs1, file=sys.stderr) 121 print ("OT funcs: " + glyphs2, file=sys.stderr) 122 fails += 1 123 else: 124 passes += 1 125 126 if glyphs1.strip() != glyphs_expected and glyphs_expected != '*': 127 print ("hb-shape", fontfile, "--unicodes", unicodes, file=sys.stderr) 128 print ("Actual: " + glyphs1, file=sys.stderr) 129 print ("Expected: " + glyphs_expected, file=sys.stderr) 130 fails += 1 131 else: 132 passes += 1 133 134print ("%d tests passed; %d failed; %d skipped." % (passes, fails, skips), file=sys.stderr) 135if not (fails + passes): 136 print ("No tests ran.") 137elif not (fails + skips): 138 print ("All tests passed.") 139 140if fails: 141 sys.exit (1) 142elif passes: 143 sys.exit (0) 144else: 145 sys.exit (77) 146