f5b503c53071d9ffcc3586cef4dc85bb4baa2adb
[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                                 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 use Unicode::UCD 'charinfo';
49 for my $code (0 .. 256**2) {
50         my $charinfo = charinfo($code) or next;
51         next if $charinfo->{category} =~ /^[MC]/;  # ignore Marks and "other" Control chars
52         push @{ $charlist{$_}->{ $charinfo->{$_} } }, chr $code
53                 for qw( script category block );
54 }
55
56 for (values %charlist) {
57 for my $chars (values %{$_}) {
58         my %row = map {
59                 my $fontcover = $font{$_};
60                 ($_ => scalar grep { $fontcover->{$_} } @{$chars});
61         } keys %font;
62         $row{-count} = scalar @{$chars};
63
64         $row{-query} = eval {
65                 my @query = map { ord } sort @{$chars};
66                 my $i = 0;
67                 while ($i < @query) {
68                         my $j = $i + 1;
69                         my $v = $query[$i];
70                         while ($j < @query) {
71                                 $v++;
72                                 last if $query[$j] != $v;
73                                 $j++;
74                         }
75                         if ($j - $i > 2) {
76                                 splice(@query, $i, $j - $i, "$query[$i]-$query[$j-1]");
77                         }
78                         $i++;
79                 }
80                 return join '+', @query;
81         };
82
83         $chars = \%row;
84 }
85 }
86
87 say 'use utf8;';
88 say '+'.pp(\%charlist);
89
90 __END__
91
92 =head1 NAME
93
94 mkfontinfo - Prepare font coverage of various character groups
95
96 =head1 SYNOPSIS
97
98     mkfontinfo > unicode-cover.inc.pl
99
100 Test by finding the number of cyrillic characters in DejaVu Sans:
101
102     perl -E'$f = do "unicode-cover.inc.pl"; say $f->{Cyrillic}->{dvsans}'
103
104 =head1 AUTHOR
105
106 Mischa POSLAWSKY <perl@shiar.org>
107
108 =head1 LICENSE
109
110 Licensed under the GNU Affero General Public License version 3.
111