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