summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/fonts/xits/fontcoverage.py
blob: c17fde46422134f14723803dfe2de4b2ced88f81 (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
import sys
import unicodedata
from fontTools.ttLib import TTFont

# Unicode blocks file from: http://www.unicode.org/Public/UNIDATA/Blocks.txt

blocksfile = open(sys.argv[1], "r")
blocks = [ ]
for line in blocksfile.readlines():
    if not (line.startswith("#") or line == "\n"):
        start, end   = line.split("; ")[0].split("..")
        name         = line.split("; ")[1].strip()
        blocks.append((name, (start, end)))
blocksfile.close()

logfile = open(sys.argv[2], "r")
log = logfile.read()
logfile.close()

for fontfile in sys.argv[3:-1]:
    font = TTFont(fontfile)
    cmap = font['cmap'].getcmap(3, 10)
    if cmap is None:
        cmap = font['cmap'].getcmap(3, 1)
    found = [ ]

    for block in blocks:
        t = f = 0
        name = block[0]
        start, end = int(block[1][0], 16), int(block[1][1], 16)
        i = start
        while (i <= end):
            category = unicodedata.category(chr(i))
            if category != "Cc" and category!= "Cn":
                if i in cmap.cmap:
                    f += 1
                t += 1
            i += 1
        if f:
            found.append((name, (t,f)))

    fullname = str(font['name'].getName(4, 1, 0).string, encoding='ascii')
    coverage  = ""
    coverage += "* %s:\n" %fullname
    for f in found:
        for b in blocks:
            if b[0] == f[0]:
                name = f[0]
                start, end = b[1]
                total, present = f[1]
                percent = present/total*100
                coverage += "  %s (U+%s-%s): %s/%s (%.2f%%)\n" %(name, start, end, present, total, percent)

    log = log.replace("%%{%s}" %fullname, coverage)

outfile = open(sys.argv[-1], "w")
outfile.write(log)
outfile.close()