Solomon Peachy | e8100bd | 2020-04-01 23:09:32 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Solomon Peachy | a0eef36 | 2020-04-02 10:05:23 -0400 | [diff] [blame] | 2 | ################################## |
| 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äggqvist | 45f3dfb | 2011-04-18 21:32:14 +0000 | [diff] [blame] | 21 | import re |
Frank Gevaerts | f6a9d68 | 2012-04-04 21:26:38 +0200 | [diff] [blame] | 22 | import sys |
| 23 | import locale |
Jonas Häggqvist | 45f3dfb | 2011-04-18 21:32:14 +0000 | [diff] [blame] | 24 | import codecs |
| 25 | from glob import glob |
Frank Gevaerts | f6a9d68 | 2012-04-04 21:26:38 +0200 | [diff] [blame] | 26 | from os.path import basename, dirname, join |
Jonas Häggqvist | 45f3dfb | 2011-04-18 21:32:14 +0000 | [diff] [blame] | 27 | from pprint import pprint |
| 28 | |
| 29 | def langs(): |
Frank Gevaerts | f6a9d68 | 2012-04-04 21:26:38 +0200 | [diff] [blame] | 30 | return glob(join(dirname(__file__), 'rockbox/apps/lang/*.lang')) |
Jonas Häggqvist | 45f3dfb | 2011-04-18 21:32:14 +0000 | [diff] [blame] | 31 | |
| 32 | def fonts(): |
Frank Gevaerts | f6a9d68 | 2012-04-04 21:26:38 +0200 | [diff] [blame] | 33 | return glob(join(dirname(__file__), 'rockbox/fonts/*.bdf')) |
Jonas Häggqvist | 45f3dfb | 2011-04-18 21:32:14 +0000 | [diff] [blame] | 34 | |
| 35 | def 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 | |
| 54 | def 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 | |
| 63 | def 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 Gevaerts | f6a9d68 | 2012-04-04 21:26:38 +0200 | [diff] [blame] | 74 | def 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äggqvist | 45f3dfb | 2011-04-18 21:32:14 +0000 | [diff] [blame] | 80 | |
Frank Gevaerts | f6a9d68 | 2012-04-04 21:26:38 +0200 | [diff] [blame] | 81 | def 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 | |
| 94 | if __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) |