b27b779fa86766b1fc2cd7e7c0fae0351e100ec4
[sheet.git] / tools / mkfontinfo
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4 use utf8;
5
6 use open OUT => ':utf8', ':std';
7 use File::Basename 'basename';
8 use Data::Dump 'pp';
9
10 our $VERSION = '1.01';
11
12 my @fontlist;
13
14 my %cover;
15 my $incsuffix = '.inc.pl';
16 for my $fontfile (glob 'data/font/*'.$incsuffix) {
17         my ($fontid) = basename($fontfile, $incsuffix);
18         my ($fontmeta, @fontrange) = do $fontfile or next;
19         $fontmeta->{file} = $fontid;
20         my $year = substr $fontmeta->{date}, 0, 4;
21         $fontmeta->{description} = join(' ',
22                 (map { "version $_" } $fontmeta->{version} || ()),
23                 $fontmeta->{version} && $fontmeta->{version} =~ /\Q$year/ ? () :
24                 (map { "($_)" } $year || ()),
25         );
26         push @fontlist, $fontmeta;
27         $cover{$fontid} = { map { (chr $_ => 1) } @fontrange };
28 }
29
30 my %charlist;
31
32 $charlist{table}->{abc} = ['A'..'Z', 'a'..'z'];
33
34 my $chartables = do 'unicode-table.inc.pl' or warn $@ || $!;
35 if ($chartables) {
36         while (my ($tablegroup, $grouprow) = each %{$chartables}) {
37                 while (my ($tablename, $chars) = each %{$grouprow}) {
38                         next if $tablename =~ /^-/;
39                         my $includerows;  # ignore rows before body row
40                         for (@{$chars}) {
41                                 $includerows ||= m/^[.]/ or next;
42                                 next if /^[.-]/;
43                                 next if $_ eq '>' or $_ eq '=';
44                                 s/^\\//;  # escape
45                                 length $_ == 1 or next;  # multiple characters lost in query
46                                 push @{ $charlist{table}->{"$tablegroup/$tablename"} }, $_;
47                                 push @{ $charlist{table}->{$tablegroup} }, $_;
48                         }
49                 }
50 #               if ($tablegroup eq 'ipa') {
51 #                       @chars = grep { !m/[a-zA-Z]/ } @chars;
52 #               }
53         }
54 }
55
56 eval {
57         require HTML::Entities;
58         our %char2entity;
59         HTML::Entities->import('%char2entity');
60         while (my ($char, $entity) = each %char2entity) {
61                 $entity =~ /[a-zA-Z]/ or next;  # only actual aliases
62                 push @{ $charlist{table}->{html} }, $char;
63         }
64         1;
65 } or warn "Could not include count for html entities: $@";
66
67 eval {
68         my $agemap = do 'data/unicode-age.inc.pl'
69                 or warn "Could not include unicode version data: $!";
70
71         use Unicode::UCD 'charinfo';
72         for my $code (0 .. 256**2*2) {
73                 my $charinfo = charinfo($code) or next;
74                 next if $charinfo->{category} =~ /^[MC]/;  # ignore Marks and "other" Control chars
75                 push @{ $charlist{$_}->{ $charinfo->{$_} } }, chr $code
76                         for qw( script category block );
77                 push @{ $charlist{version}->{$_} }, (chr $code) x ($agemap->{$code} <= $_)
78                         for 11, 30, 63;
79         }
80         1;
81 } or warn "Could not include unicode groups: $@";
82
83 for (values %charlist) {
84 for my $chars (values %{$_}) {
85         my %row;
86         $row{support} = [
87                 map { scalar grep { defined } @{ $cover{$_->{file}} }{ @{$chars} } }
88                 @fontlist
89         ];
90         $row{count} = scalar @{$chars};
91
92         $row{query} = eval {
93                 my @query = map { ord } sort @{$chars};
94                 my $i = 0;
95                 while ($i < @query) {
96                         my $j = $i + 1;
97                         my $v = $query[$i];
98                         while ($j < @query) {
99                                 $v++;
100                                 last if $query[$j] != $v;
101                                 $j++;
102                         }
103                         if ($j - $i > 2) {
104                                 splice(@query, $i, $j - $i, "$query[$i]-$query[$j-1]");
105                         }
106                         $i++;
107                 }
108                 return join '+', @query;
109         };
110
111         $chars = \%row;
112 }
113 }
114
115 $charlist{fonts} = \@fontlist;
116
117 my %osfonts = (
118         win2k   => [qw( arial.win2k arialuni lucidau verdana.win2k times.win2k cour.win2k )],  # microsoft
119         win8    => [map {"$_.win8"} qw( arial verdana times georgia pala cour )],
120         mac109  => [map {"$_.mac109"} qw( helv lucida times pala )],  # apple
121         android => [qw( roboto droidmono notosans )],  # google
122         oss     => [qw( dvsans freesans code2000 unifont )],
123 );
124 if (0) {
125         # copy rows to derive older os versions (same list with different trailing number)
126         s/8$/7/ for @{ $osfonts{  win7} = [@{ $osfonts{  win8} }] };
127         s/9$/7/ for @{ $osfonts{mac107} = [@{ $osfonts{mac109} }] };
128 }
129
130 my %fontnum = map { ($fontlist[$_]->{file} => $_) } 0 .. $#fontlist;
131 while (my ($os, $fontids) = each %osfonts) {
132         $charlist{os}->{$os} = [ map { $fontnum{$_} // () } @{$fontids} ];
133 }
134 $charlist{osdefault} = [qw( win2k win8 mac109 android oss )];
135
136 say "# automatically generated by $0";
137 say 'use utf8;';
138 say '+', pp(\%charlist) =~ s{
139         ( \[ \s* \d [^]]* ) ,\s* (?= \] )  # arrays of numbers, excluding trailing comma
140 }{ $1 =~ s/\s+//gr }msxgre;  # strip whitespace
141
142 __END__
143
144 =head1 NAME
145
146 mkfontinfo - Prepare font coverage of various character groups
147
148 =head1 SYNOPSIS
149
150     mkfontinfo > unicode-cover.inc.pl
151
152 Test by finding the number of cyrillic characters in DejaVu Sans:
153
154     perl -E'$f = do "unicode-cover.inc.pl"; say $f->{Cyrillic}->{dvsans}'
155
156 =head1 AUTHOR
157
158 Mischa POSLAWSKY <perl@shiar.org>
159
160 =head1 LICENSE
161
162 Licensed under the GNU Affero General Public License version 3.
163