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