summaryrefslogtreecommitdiffstats
path: root/examples/fontforge-old/FFcheckDupUSV.py
blob: d6d948cdea4e586d2415739a898bbac6dcaa1410 (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
#!/usr/bin/env python
'FontForge: Check for duplicate USVs in unicode or altuni fields'
__url__ = 'http://github.com/silnrsi/pysilfont'
__copyright__ = 'Copyright (c) 2015 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'}),
    ('-o','--output',{'help': 'Output text file'}, {'type': 'outfile', 'def': 'DupUSV.txt'})]

def doit(args) :
    font = args.ifont
    outf = args.output

    # Process unicode and altunicode for all glyphs
    usvs={}
    for glyph in font:
        g = font[glyph]
        if g.unicode != -1:
            usv=UniStr(g.unicode)
            AddUSV(usvs,usv,glyph)
        # Check any alternate usvs
        altuni=g.altuni
        if altuni != None:
            for au in altuni:
                usv=UniStr(au[0]) # (may need to check variant flag)
                AddUSV(usvs,usv,glyph + ' (alt)')
                
    items = usvs.items()
    items = filter(lambda x: len(x[1]) > 1, items)
    items.sort()

    for i in items:
        usv = i[0]
        print usv + ' has duplicates'
        gl = i[1]
        glyphs = gl[0]
        for j in range(1,len(gl)):
            glyphs = glyphs + ', ' + gl[j]

        outf.write('%s: %s\n' % (usv,glyphs))

    outf.close()
    print "Done!"

def UniStr(u):
    if u:
        return "U+{0:04X}".format(u)
    else:
        return "No USV" #length same as above

def AddUSV(usvs,usv,glyph):
    if not usvs.has_key(usv):
        usvs[usv] = [glyph]
    else:
        usvs[usv].append(glyph)
        
def cmd() : execute("FF",doit,argspec) 
if __name__ == "__main__": cmd()