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