summaryrefslogtreecommitdiffstats
path: root/fontstats.pl
blob: 123de3614755f5a8f982ee2c3e449d9c0f29d39f (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/perl -w
##################
# *             __________               __   ___.
# *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
# *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
# *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
# *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
# *                     \/            \/     \/    \/            \/
# * Copyright (C) 2020 Solomon Peachy
# *
# * This program is free software; you can redistribute it and/or
# * modify it under the terms of the GNU General Public License
# * as published by the Free Software Foundation; either version 2
# * of the License, or (at your option) any later version.
# *
# * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# * KIND, either express or implied.
# *
##################################

use strict;
use feature 'unicode_strings';
binmode(STDOUT, ":utf8");

my @langs;
my @fonts;

my %fontchars;
my %langchars;
my %missing;

sub fontcoverage($) {
    my ($font) = @_;

    open(FILE, "<rockbox/fonts/$font") || die ("can't open $font!\n");
    while(<FILE>) {
	if (/^ENCODING\s+(\d+)\s*/) {
	    $fontchars{$font}{chr($1)} = $1;
	}
    }
    close(FILE);
}

sub langcoverage($) {
    my ($lang) = @_;

    open(FILE, "<rockbox/apps/lang/$lang") || die ("can't open $lang!\n");
    binmode(FILE, ":utf8");
    my $indest = 0;
    while(<FILE>) {
	if (/^\s*<dest>\s*$/) {
	    $indest = 1;
	} elsif (/^\s*<\/dest>\s*$/) {
	    $indest = 0;
	} elsif ($indest) {
	    if (/\s*\S*\s*:\s*"(\S*)"\s*/u) {
		next if ($1 eq "none");
		foreach my $char (split(//, $1)) {
		    if (!defined($langchars{$lang}{$char})) {
			$langchars{$lang}{$char} = 0;
		    }
		    $langchars{$lang}{$char}++;
		}
	    }
	}
    }
    close(FILE);
}

sub calccoverage($$) {
    my ($lang, $font) = @_;
    my $total = 0;
    my $covered = 0;
    my $str = "";

    foreach my $l (sort(keys(%{$langchars{$lang}}))) {
	next if ($l eq " ");
	$str .= $l;
	$total++;
	#$total += $langchars{$lang}{$l};
	if (defined($fontchars{$font}{$l})) {
	    $covered++;
	    #$covered += $langchars{$lang}{$l};
	} else {
	    $missing{$font}{$lang}{$l} = 1;
	}
    }

#    return "$covered/$total - '$str'";
    return $covered/$total;
}

###################

# Populate font and language lists
opendir(DIR, "rockbox/apps/lang");
@langs = grep(/\.lang$/,readdir(DIR));
closedir(DIR);

opendir(DIR, "rockbox/fonts");
@fonts = grep(/\.bdf$/,readdir(DIR));
closedir(DIR);

# Generate coverage maps
foreach my $x (@fonts) {
    fontcoverage($x);
}
foreach my $x (@langs) {
    langcoverage($x);
}

# Geneate INI files
# (standard summary)
foreach my $lang (sort(@langs)) {
    $lang =~ /(.*)\.lang/;
    print "[$1]\n";
    foreach my $font (sort(@fonts)) {
	my $coverage = calccoverage($lang, $font);
        $font =~/(.*).bdf/;
	printf "  $1 = %1.6f\n", $coverage;
    }
}

# (missing)
#foreach my $lang (sort(@langs)) {
#    print "[missing|$lang]\n";
#    foreach my $font (sort(@fonts)) {
#	my $str = "";
#	foreach my $missing (keys(%{$missing{$font}{$lang}})) {
#	    $str .= "'$missing' (u+".ord($missing).") ";
#	}
#	print "  $font = $str\n" if ($str);
#    }
#}