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