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