deada09937691bd4e94aa7676e86ddb5b7a1153e
[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 List::Util 'reduce';
9 use File::Basename 'basename';
10 use Data::Dump 'pp';
11
12 our $VERSION = '1.00';
13
14 my %font;
15 my $incsuffix = '.inc.pl';
16 for my $fontfile (glob 'ttfsupport/*'.$incsuffix) {
17         my ($fontid) = basename($fontfile, $incsuffix);
18         my ($fontmeta, @fontrange) = do $fontfile or next;
19         $font{$fontid} = {
20                 -id   => $fontmeta->{id} || $fontid,
21                 -name => $fontmeta->{name},
22                 map { (chr $_ => 1) } @fontrange
23         };
24 }
25
26 my %charlist;
27
28 my $chartables = do 'unicode-table.inc.pl' or warn $@ || $!;
29 if ($chartables) {
30         while (my ($tablegroup, $grouprow) = each %{$chartables}) {
31                 while (my ($tablename, $chars) = each %{$grouprow}) {
32                         next if $tablename =~ /^-/;
33                         my $includerows;  # ignore rows before body row
34                         for (@{$chars}) {
35                                 $includerows ||= m/^[.]/ or next;
36                                 next if /^[.-]/;
37                                 next if $_ eq '>' or $_ eq '=';
38                                 s/^\\//;  # escape
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 = map {
71                 my $fontcover = $font{$_};
72                 ($_ => scalar grep { $fontcover->{$_} } @{$chars});
73         } keys %font;
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 say "# automatically generated by $0";
100 say 'use utf8;';
101 say '+'.pp(\%charlist);
102
103 __END__
104
105 =head1 NAME
106
107 mkfontinfo - Prepare font coverage of various character groups
108
109 =head1 SYNOPSIS
110
111     mkfontinfo > unicode-cover.inc.pl
112
113 Test by finding the number of cyrillic characters in DejaVu Sans:
114
115     perl -E'$f = do "unicode-cover.inc.pl"; say $f->{Cyrillic}->{dvsans}'
116
117 =head1 AUTHOR
118
119 Mischa POSLAWSKY <perl@shiar.org>
120
121 =head1 LICENSE
122
123 Licensed under the GNU Affero General Public License version 3.
124