blob: 3a9067ad752d5f2e5e6ae55e69c41e285faaf4f3 [file] [log] [blame]
Solomon Peachye8100bd2020-04-01 23:09:32 -04001#!/usr/bin/env python2
Solomon Peachya0eef362020-04-02 10:05:23 -04002##################################
3# * __________ __ ___.
4# * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5# * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6# * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7# * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8# * \/ \/ \/ \/ \/
9# * Copyright (C) 2010 Jonas Häggqvist
10# *
11# * This program is free software; you can redistribute it and/or
12# * modify it under the terms of the GNU General Public License
13# * as published by the Free Software Foundation; either version 2
14# * of the License, or (at your option) any later version.
15# *
16# * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17# * KIND, either express or implied.
18# *
19##################################
20
Jonas Häggqvist45f3dfb2011-04-18 21:32:14 +000021import re
Frank Gevaertsf6a9d682012-04-04 21:26:38 +020022import sys
23import locale
Jonas Häggqvist45f3dfb2011-04-18 21:32:14 +000024import codecs
25from glob import glob
Frank Gevaertsf6a9d682012-04-04 21:26:38 +020026from os.path import basename, dirname, join
Jonas Häggqvist45f3dfb2011-04-18 21:32:14 +000027from pprint import pprint
28
29def langs():
Frank Gevaertsf6a9d682012-04-04 21:26:38 +020030 return glob(join(dirname(__file__), 'rockbox/apps/lang/*.lang'))
Jonas Häggqvist45f3dfb2011-04-18 21:32:14 +000031
32def fonts():
Frank Gevaertsf6a9d682012-04-04 21:26:38 +020033 return glob(join(dirname(__file__), 'rockbox/fonts/*.bdf'))
Jonas Häggqvist45f3dfb2011-04-18 21:32:14 +000034
35def charusage(langfile):
36 usage = {}
37 fp = codecs.open(langfile, 'r', 'UTF-8')
38 indest = False
39 for line in fp:
40 if re.match(r'^\s*<dest>\s*$', line):
41 indest = True
42 elif re.match(r'^\s*</dest>\s*$', line):
43 indest = False
44
45 if indest:
46 string = re.match(r'\s*\S*\s*:\s*"([^"]*)"\s*', line)
47 if string:
48 for char in string.group(1):
49 if char not in usage:
50 usage[char] = 0
51 usage[char] += 1
52 return usage
53
54def charsavailable(fontfile):
55 chars = []
56 fp = open(fontfile, 'r')
57 for line in fp:
58 encoding = re.match(r'ENCODING\s+(\d+)\s*', line)
59 if encoding:
60 chars.append(unichr(int(encoding.group(1))))
61 return chars
62
63def calculatecoverage(charsused, charsavailable):
64 total = 0
65 covered = 0
66 for char, uses in charsused.iteritems():
67 if char == u' ':
68 continue
69 total += uses
70 if char in charsavailable:
71 covered += uses
72 return float(covered)/float(total)
73
Frank Gevaertsf6a9d682012-04-04 21:26:38 +020074def generate_summary(fontstats, langusage):
75 for langfile, charsused in sorted(langusage.items()):
76 print "[%s]" % basename(langfile).replace('.lang', '')
77 for fontfile, charsavailable in sorted(fontstats.items()):
78 coverage = calculatecoverage(charsused, charsavailable)
79 print " %s = %f" % (basename(fontfile).replace('.bdf', ''), coverage)
Jonas Häggqvist45f3dfb2011-04-18 21:32:14 +000080
Frank Gevaertsf6a9d682012-04-04 21:26:38 +020081def generate_missing(fontstats, langusage):
82 for langfile, charsused in sorted(langusage.items()):
83 print "[%s]" % basename(langfile).replace('.lang', '')
84 for fontfile, charsavailable in sorted(fontstats.items()):
85 missingchars = []
86 for char, uses in charsused.iteritems():
87 if char not in charsavailable:
88 missingchars.append(char)
89 # If more than 50 characters are missing, don't print them all
90 if 25 > len(missingchars) > 0:
91 print " %s = %s" % (basename(fontfile).replace('.bdf', ''), " ".join(["%s (u+%X)" % (c, ord(c)) for c in missingchars]))
92
93
94if __name__ == '__main__':
95 sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout);
96
97 fontstats = dict([(font, charsavailable(font)) for font in fonts()])
98 langusage = dict([(lang, charusage(lang)) for lang in langs()])
99
100 if len(sys.argv) > 1 and sys.argv[1] == 'missing':
101 generate_missing(fontstats, langusage)
102 else:
103 generate_summary(fontstats, langusage)