e212d94a8aba715362a91c3957b2c2a971229f53
[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 eval {
61         use Unicode::UCD 'charinfo';
62         for my $code (0 .. 256**2) {
63                 my $charinfo = charinfo($code) or next;
64                 next if $charinfo->{category} =~ /^[MC]/;  # ignore Marks and "other" Control chars
65                 push @{ $charlist{$_}->{ $charinfo->{$_} } }, chr $code
66                         for qw( script category block );
67         }
68         1;
69 } or warn "Could not include unicode groups: $@";
70
71 for (values %charlist) {
72 for my $chars (values %{$_}) {
73         my %row;
74         $row{support} = [
75                 map { scalar grep { defined } @{ $cover{$_->{file}} }{ @{$chars} } }
76                 @fontlist
77         ];
78         $row{count} = scalar @{$chars};
79
80         $row{query} = eval {
81                 my @query = map { ord } sort @{$chars};
82                 my $i = 0;
83                 while ($i < @query) {
84                         my $j = $i + 1;
85                         my $v = $query[$i];
86                         while ($j < @query) {
87                                 $v++;
88                                 last if $query[$j] != $v;
89                                 $j++;
90                         }
91                         if ($j - $i > 2) {
92                                 splice(@query, $i, $j - $i, "$query[$i]-$query[$j-1]");
93                         }
94                         $i++;
95                 }
96                 return join '+', @query;
97         };
98
99         $chars = \%row;
100 }
101 }
102
103 $charlist{fonts} = \@fontlist;
104
105 my %osfonts = (
106         win95   => [qw( arial arialuni lucidau verdana timesnew couriernew )],  # microsoft
107         mac10   => [qw( helvetica lucida times garamond palatino )],  # apple
108         android => [qw( roboto noto )],  # google
109         oss     => [qw( dvsans c2k unifont opensans )],
110 );
111 my %fontnum = map { ($fontlist[$_]->{file} => $_) } 0 .. $#fontlist;
112 while (my ($os, $fontids) = each %osfonts) {
113         $charlist{os}->{$os} = [ map { $fontnum{$_} // () } @{$fontids} ];
114 }
115 $charlist{osdefault} = [qw( win95 mac10 oss android )];
116
117 say "# automatically generated by $0";
118 say 'use utf8;';
119 say '+'.pp(\%charlist);
120
121 __END__
122
123 =head1 NAME
124
125 mkfontinfo - Prepare font coverage of various character groups
126
127 =head1 SYNOPSIS
128
129     mkfontinfo > unicode-cover.inc.pl
130
131 Test by finding the number of cyrillic characters in DejaVu Sans:
132
133     perl -E'$f = do "unicode-cover.inc.pl"; say $f->{Cyrillic}->{dvsans}'
134
135 =head1 AUTHOR
136
137 Mischa POSLAWSKY <perl@shiar.org>
138
139 =head1 LICENSE
140
141 Licensed under the GNU Affero General Public License version 3.
142