0f0857d73f06a27bdec2d5f937d916121021da9e
[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 #       $row{-chars} = [ map { ord } sort @{$chars} ];
67
68         $chars = \%row;
69 }
70 }
71
72 say 'use utf8;';
73 say '+'.pp(\%charlist);
74
75 __END__
76
77 =head1 NAME
78
79 mkfontinfo - Prepare font coverage of various character groups
80
81 =head1 SYNOPSIS
82
83     mkfontinfo > unicode-cover.inc.pl
84
85 Test by finding the number of cyrillic characters in DejaVu Sans:
86
87     perl -E'$f = do "unicode-cover.inc.pl"; say $f->{Cyrillic}->{dvsans}'
88
89 =head1 AUTHOR
90
91 Mischa POSLAWSKY <perl@shiar.org>
92
93 =head1 LICENSE
94
95 Licensed under the GNU Affero General Public License version 3.
96