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