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