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