font: ignore multichar entries in overview count
[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         push @fontlist, $fontmeta;
22         $cover{$fontid} = { map { (chr $_ => 1) } @fontrange };
23 }
24
25 my %charlist;
26
27 my $chartables = do 'unicode-table.inc.pl' or warn $@ || $!;
28 if ($chartables) {
29         while (my ($tablegroup, $grouprow) = each %{$chartables}) {
30                 while (my ($tablename, $chars) = each %{$grouprow}) {
31                         next if $tablename =~ /^-/;
32                         my $includerows;  # ignore rows before body row
33                         for (@{$chars}) {
34                                 $includerows ||= m/^[.]/ or next;
35                                 next if /^[.-]/;
36                                 next if $_ eq '>' or $_ eq '=';
37                                 s/^\\//;  # escape
38                                 length $_ == 1 or next;  # multiple characters lost in query
39                                 push @{ $charlist{table}->{"$tablegroup/$tablename"} }, $_;
40                                 push @{ $charlist{table}->{$tablegroup} }, $_;
41                         }
42                 }
43 #               if ($tablegroup eq 'ipa') {
44 #                       @chars = grep { !m/[a-zA-Z]/ } @chars;
45 #               }
46         }
47 }
48
49 eval {
50         require HTML::Entities;
51         our %char2entity;
52         HTML::Entities->import('%char2entity');
53         while (my ($char, $entity) = each %char2entity) {
54                 $entity =~ /[a-zA-Z]/ or next;  # only actual aliases
55                 push @{ $charlist{table}->{html} }, $char;
56         }
57         1;
58 } or warn "Could not include count for html entities: $@";
59
60 use Unicode::UCD 'charinfo';
61 for my $code (0 .. 256**2) {
62         my $charinfo = charinfo($code) or next;
63         next if $charinfo->{category} =~ /^[MC]/;  # ignore Marks and "other" Control chars
64         push @{ $charlist{$_}->{ $charinfo->{$_} } }, chr $code
65                 for qw( script category block );
66 }
67
68 for (values %charlist) {
69 for my $chars (values %{$_}) {
70         my %row;
71         $row{support} = [
72                 map { scalar grep { defined } @{ $cover{$_->{file}} }{ @{$chars} } }
73                 @fontlist
74         ];
75         $row{count} = scalar @{$chars};
76
77         $row{query} = eval {
78                 my @query = map { ord } sort @{$chars};
79                 my $i = 0;
80                 while ($i < @query) {
81                         my $j = $i + 1;
82                         my $v = $query[$i];
83                         while ($j < @query) {
84                                 $v++;
85                                 last if $query[$j] != $v;
86                                 $j++;
87                         }
88                         if ($j - $i > 2) {
89                                 splice(@query, $i, $j - $i, "$query[$i]-$query[$j-1]");
90                         }
91                         $i++;
92                 }
93                 return join '+', @query;
94         };
95
96         $chars = \%row;
97 }
98 }
99
100 $charlist{fonts} = \@fontlist;
101
102 my %osfonts = (
103         win95   => [qw( arial arialuni lucidau verdana timesnew couriernew )],  # microsoft
104         mac10   => [qw( helvetica lucida times garamond palatino )],  # apple
105         android => [qw( roboto noto )],  # google
106         oss     => [qw( dvsans c2k unifont opensans )],
107 );
108 my %fontnum = map { ($fontlist[$_]->{file} => $_) } 0 .. $#fontlist;
109 while (my ($os, $fontids) = each %osfonts) {
110         $charlist{os}->{$os} = [ map { $fontnum{$_} // () } @{$fontids} ];
111 }
112 $charlist{osdefault} = [qw( win95 mac10 oss android )];
113
114 say "# automatically generated by $0";
115 say 'use utf8;';
116 say '+'.pp(\%charlist);
117
118 __END__
119
120 =head1 NAME
121
122 mkfontinfo - Prepare font coverage of various character groups
123
124 =head1 SYNOPSIS
125
126     mkfontinfo > unicode-cover.inc.pl
127
128 Test by finding the number of cyrillic characters in DejaVu Sans:
129
130     perl -E'$f = do "unicode-cover.inc.pl"; say $f->{Cyrillic}->{dvsans}'
131
132 =head1 AUTHOR
133
134 Mischa POSLAWSKY <perl@shiar.org>
135
136 =head1 LICENSE
137
138 Licensed under the GNU Affero General Public License version 3.
139