summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/fonts/amiri/tools/runtest.py
blob: 4ea7ff3407695c992578e2882a060852899f516c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python

import sys
import os
import csv
import subprocess

def runHB(row, font):
    args = ["hb-shape", "--no-clusters", "--no-positions",
            "--font-file=%s" %font,
            "--direction=%s" %row[0],
            "--script=%s"    %row[1],
            "--language=%s"  %row[2],
            "--features=%s"  %row[3],
            "--text=%s"      %row[4]]
    process = subprocess.Popen(args, stdout=subprocess.PIPE)
    return process.communicate()[0].strip()

def runTest(test, font):
    count = 0
    failed = {}
    passed = []
    for row in test:
        count += 1
        row[4] = ('\\' in row[4]) and row[4].decode('unicode-escape') or row[4]
        text = row[4]
        reference = row[5]
        result = runHB(row, font)
        if reference == result:
            passed.append(count)
        else:
            failed[count] = (text, reference, result)

    return passed, failed

def initTest(test, font):
    out = ""
    for row in test:
        result = runHB(row, font)
        out += "%s;%s\n" %(";".join(row), result)

    return out

if __name__ == '__main__':
    init = False
    args = sys.argv[1:]

    if len (sys.argv) > 2 and sys.argv[1] == "-i":
        init = True
        args = sys.argv[2:]

    for arg in args:
        testname = arg

        reader = csv.reader(open(testname), delimiter=';')

        test = []
        for row in reader:
            test.append(row)

        if init:
            outname = testname+".test"
            outfd = open(outname, "w")
            outfd.write("# %s\n" %fontname)
            outfd.write(initTest(test, fontname))
            outfd.close()
            sys.exit(0)

        for style in ('regular', 'bold', 'slanted', 'boldslanted'):
            fontname = 'amiri-%s.ttf' % style
            passed, failed = runTest(test, fontname)
            message = "%s: font '%s', %d passed, %d failed" %(os.path.basename(testname),
                    fontname, len(passed), len(failed))

            print message
            if failed:
                for test in failed:
                    print test
                    print "string:   \t", failed[test][0]
                    print "reference:\t", failed[test][1]
                    print "result:   \t", failed[test][2]
                sys.exit(1)