aboutsummaryrefslogtreecommitdiff
path: root/Snippets/fix-dflt-langsys.py
blob: c072117a792dbf82468c23ef1b62a108f15ac9a6 (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
83
84
85
#!/usr/bin/env python3

import argparse
import logging
import os
import sys

from fontTools.ttLib import TTFont


def ProcessTable(table):
    found = set()

    for rec in table.ScriptList.ScriptRecord:
        if rec.ScriptTag == "DFLT" and rec.Script.LangSysCount != 0:
            tags = [r.LangSysTag for r in rec.Script.LangSysRecord]
            logging.info("Removing %d extraneous LangSys records: %s",
                         rec.Script.LangSysCount, " ".join(tags))
            rec.Script.LangSysRecord = []
            rec.Script.LangSysCount = 0
            found.update(tags)

    if not found:
        logging.info("All fine")
        return False
    else:
        for rec in table.ScriptList.ScriptRecord:
            tags = set([r.LangSysTag for r in rec.Script.LangSysRecord])
            found -= tags

        if found:
            logging.warning("Records are missing from non-DFLT scripts: %s",
                            " ".join(found))
        return True


def ProcessFont(font):
    found = False
    for tag in ("GSUB", "GPOS"):
        if tag in font:
            logging.info("Processing %s table", tag)
            if ProcessTable(font[tag].table):
                found = True
            else:
                # Unmark the table as loaded so that it is read from disk when
                # writing the font, to avoid any unnecessary changes caused by
                # decompiling then recompiling again.
                del font.tables[tag]

    return found


def ProcessFiles(filenames):
    for filename in filenames:
        logging.info("Processing %s", filename)
        font = TTFont(filename)
        name, ext = os.path.splitext(filename)
        fixedname = name + ".fixed" + ext
        if ProcessFont(font):
            logging.info("Saving fixed font to %s\n", fixedname)
            font.save(fixedname)
        else:
            logging.info("Font file is fine, nothing to fix\n")


def main():
    parser = argparse.ArgumentParser(
            description="Fix LangSys records for DFLT script")
    parser.add_argument("files", metavar="FILE", type=str, nargs="+",
                        help="input font to process")
    parser.add_argument("-s", "--silent", action='store_true',
                        help="suppress normal messages")

    args = parser.parse_args()

    logformat = "%(levelname)s: %(message)s"
    if args.silent:
        logging.basicConfig(format=logformat, level=logging.DEBUG)
    else:
        logging.basicConfig(format=logformat, level=logging.INFO)

    ProcessFiles(args.files)

if __name__ == "__main__":
    sys.exit(main())