countries: rename ez title to eurozone
[sheet.git] / tools / mkfontinfo
index fe7059fd31a34b9df07531c034f52031df94b10f..cdd508d3cd5a5cf300474d9ac73a8aba85282809 100755 (executable)
@@ -5,52 +5,138 @@ use warnings;
 use utf8;
 
 use open OUT => ':utf8', ':std';
-use List::Util 'reduce';
 use File::Basename 'basename';
 use Data::Dump 'pp';
 
-our $VERSION = '1.00';
+our $VERSION = '1.01';
 
-my %font;
+my @fontlist;
+
+my %cover;
 my $incsuffix = '.inc.pl';
 for my $fontfile (glob 'ttfsupport/*'.$incsuffix) {
        my ($fontid) = basename($fontfile, $incsuffix);
        my ($fontmeta, @fontrange) = do $fontfile or next;
-       $font{$fontid} = {
-               -id   => $fontmeta->{id} || $fontid,
-               -name => $fontmeta->{name},
-               map { (chr $_ => 1) } @fontrange
-       };
+       $fontmeta->{file} = $fontid;
+       my $year = substr $fontmeta->{date}, 0, 4;
+       $fontmeta->{description} = join(' ',
+               (map { "version $_" } $fontmeta->{version} || ()),
+               $fontmeta->{version} && $fontmeta->{version} =~ /\Q$year/ ? () :
+               (map { "($_)" } $year || ()),
+       );
+       push @fontlist, $fontmeta;
+       $cover{$fontid} = { map { (chr $_ => 1) } @fontrange };
 }
 
-my @chargroups = qw(
-       N Z Math
-       Assigned
-       Latin Greek Cyrillic Georgian Arabic Thai Hangul Han
-);
+my %charlist;
+
+my $chartables = do 'unicode-table.inc.pl' or warn $@ || $!;
+if ($chartables) {
+       while (my ($tablegroup, $grouprow) = each %{$chartables}) {
+               while (my ($tablename, $chars) = each %{$grouprow}) {
+                       next if $tablename =~ /^-/;
+                       my $includerows;  # ignore rows before body row
+                       for (@{$chars}) {
+                               $includerows ||= m/^[.]/ or next;
+                               next if /^[.-]/;
+                               next if $_ eq '>' or $_ eq '=';
+                               s/^\\//;  # escape
+                               length $_ == 1 or next;  # multiple characters lost in query
+                               push @{ $charlist{table}->{"$tablegroup/$tablename"} }, $_;
+                               push @{ $charlist{table}->{$tablegroup} }, $_;
+                       }
+               }
+#              if ($tablegroup eq 'ipa') {
+#                      @chars = grep { !m/[a-zA-Z]/ } @chars;
+#              }
+       }
+}
 
-say 'use utf8;';
-say '+{';
-for my $name (@chargroups) {
-       my $match = qr/\A\p{$name}\z/;
-       my @chars = eval {
-               grep { m/$match/ } map { chr $_ }
-               0..0xD7FF, 0xE000..0xFDCF, 0xFDF0..0xFFFD,
-       } or do {
-               warn $@;
-               next;
+eval {
+       require HTML::Entities;
+       our %char2entity;
+       HTML::Entities->import('%char2entity');
+       while (my ($char, $entity) = each %char2entity) {
+               $entity =~ /[a-zA-Z]/ or next;  # only actual aliases
+               push @{ $charlist{table}->{html} }, $char;
+       }
+       1;
+} or warn "Could not include count for html entities: $@";
+
+eval {
+       my $agemap = do 'unicode-age.inc.pl'
+               or warn "Could not include unicode version data: $!";
+
+       use Unicode::UCD 'charinfo';
+       for my $code (0 .. 256**2*2) {
+               my $charinfo = charinfo($code) or next;
+               next if $charinfo->{category} =~ /^[MC]/;  # ignore Marks and "other" Control chars
+               push @{ $charlist{$_}->{ $charinfo->{$_} } }, chr $code
+                       for qw( script category block );
+               push @{ $charlist{version}->{$_} }, (chr $code) x ($agemap->{$code} <= $_)
+                       for 11, 30, 63;
+       }
+       1;
+} or warn "Could not include unicode groups: $@";
+
+for (values %charlist) {
+for my $chars (values %{$_}) {
+       my %row;
+       $row{support} = [
+               map { scalar grep { defined } @{ $cover{$_->{file}} }{ @{$chars} } }
+               @fontlist
+       ];
+       $row{count} = scalar @{$chars};
+
+       $row{query} = eval {
+               my @query = map { ord } sort @{$chars};
+               my $i = 0;
+               while ($i < @query) {
+                       my $j = $i + 1;
+                       my $v = $query[$i];
+                       while ($j < @query) {
+                               $v++;
+                               last if $query[$j] != $v;
+                               $j++;
+                       }
+                       if ($j - $i > 2) {
+                               splice(@query, $i, $j - $i, "$query[$i]-$query[$j-1]");
+                       }
+                       $i++;
+               }
+               return join '+', @query;
        };
 
-       my %cover = map {
-               my $fontcover = $font{$_};
-               ($_ => scalar grep { $fontcover->{$_} } @chars);
-       } keys %font;
-       $cover{-count} = scalar @chars;
-       $cover{-chars} = [ map { ord } sort @chars ];
+       $chars = \%row;
+}
+}
 
-       say $name.' => '.pp(\%cover).',';
+$charlist{fonts} = \@fontlist;
+
+my %osfonts = (
+       win2k   => [qw( arial.win2k arialuni lucidau verdana.win2k times.win2k cour.win2k )],  # microsoft
+       win8    => [map {"$_.win8"} qw( arial verdana times georgia pala cour )],
+       mac109  => [map {"$_.mac109"} qw( helv lucida times pala )],  # apple
+       android => [qw( roboto droidmono notosans )],  # google
+       oss     => [qw( dvsans freesans code2000 unifont )],
+);
+if (0) {
+       # copy rows to derive older os versions (same list with different trailing number)
+       s/8$/7/ for @{ $osfonts{  win7} = [@{ $osfonts{  win8} }] };
+       s/9$/7/ for @{ $osfonts{mac107} = [@{ $osfonts{mac109} }] };
+}
+
+my %fontnum = map { ($fontlist[$_]->{file} => $_) } 0 .. $#fontlist;
+while (my ($os, $fontids) = each %osfonts) {
+       $charlist{os}->{$os} = [ map { $fontnum{$_} // () } @{$fontids} ];
 }
-say '}';
+$charlist{osdefault} = [qw( win2k win8 mac109 android oss )];
+
+say "# automatically generated by $0";
+say 'use utf8;';
+say '+', pp(\%charlist) =~ s{
+       ( \[ \s* \d [^]]* ) ,\s* (?= \] )  # arrays of numbers, excluding trailing comma
+}{ $1 =~ s/\s+//gr }msxgre;  # strip whitespace
 
 __END__