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