summaryrefslogtreecommitdiffstats
path: root/lib/silfont/scripts/psfremovegliflibkeys.py
blob: 5d59ad7c59475946096b5a0b0ecf66e4390cb426 (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
#!/usr/bin/env python
__doc__ = '''Remove the specified key(s) from glif libs'''
__url__ = 'http://github.com/silnrsi/pysilfont'
__copyright__ = 'Copyright (c) 2017 SIL International (http://www.sil.org)'
__license__ = 'Released under the MIT License (http://opensource.org/licenses/MIT)'
__author__ = 'David Raymond'

from silfont.core import execute

argspec = [
    ('ifont',{'help': 'Input font file'}, {'type': 'infont'}),
    ('key',{'help': 'Key(s) to remove','nargs': '*' }, {}),
    ('-b', '--begins', {'help': 'Remove keys beginning with','nargs': '*' }, {}),
    ('-o', '--ofont',{'help': 'Output font file' }, {'type': 'outfont'}),
    ('-l','--log',{'help': 'Log file'}, {'type': 'outfile', 'def': '_removegliflibkeys.log'})]

def doit(args) :
    font = args.ifont
    logger = args.logger
    keys = args.key
    bkeys=args.begins if args.begins is not None else []
    keycounts = {}
    bkeycounts = {}
    for key in keys : keycounts[key] = 0
    for key in bkeys:
        if key in keycounts: logger.log("--begins key can't be the same as a standard key", "S")
        bkeycounts[key] = 0

    for glyphn in font.deflayer :
        glyph = font.deflayer[glyphn]
        if glyph["lib"] :
            for key in keys :
                if key in glyph["lib"] :
                    val = str( glyph["lib"].getval(key))
                    glyph["lib"].remove(key)
                    keycounts[key] += 1
                    logger.log(key + " removed from " + glyphn + ". Value was " + val, "I" )
                    if key == "com.schriftgestaltung.Glyphs.originalWidth": # Special fix re glyphLib bug
                        if glyph["advance"] is None: glyph.add("advance")
                        adv = (glyph["advance"])
                        if adv.width is None:
                            adv.width = int(float(val))
                            logger.log("Advance width for " + glyphn + " set to " + val, "I")
                        else:
                            logger.log("Advance width for " + glyphn + " is already set to " + str(adv.width) + " so originalWidth not copied", "E")
            for key in bkeys:
                gkeys = list(glyph["lib"])
                for gkey in gkeys:
                    if gkey[:len(key)] == key:
                        val = str(glyph["lib"].getval(gkey))
                        glyph["lib"].remove(gkey)
                        if gkey in keycounts:
                            keycounts[gkey] += 1
                        else:
                            keycounts[gkey] = 1
                        bkeycounts[key] += 1
                        logger.log(gkey + " removed from " + glyphn + ". Value was " + val, "I")

    for key in keycounts :
        count = keycounts[key]
        if count > 0 :
            logger.log(key + " removed from " + str(count) +  " glyphs", "P")
        else :
            logger.log("No lib entries found for " + key, "E")
    for key in bkeycounts:
        if bkeycounts[key] == 0: logger.log("No lib entries found for beginning with " + key, "E")

    return font

def cmd() : execute("UFO",doit,argspec)
if __name__ == "__main__": cmd()