fe7059fd31a34b9df07531c034f52031df94b10f
[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 @chargroups = qw(
27         N Z Math
28         Assigned
29         Latin Greek Cyrillic Georgian Arabic Thai Hangul Han
30 );
31
32 say 'use utf8;';
33 say '+{';
34 for my $name (@chargroups) {
35         my $match = qr/\A\p{$name}\z/;
36         my @chars = eval {
37                 grep { m/$match/ } map { chr $_ }
38                 0..0xD7FF, 0xE000..0xFDCF, 0xFDF0..0xFFFD,
39         } or do {
40                 warn $@;
41                 next;
42         };
43
44         my %cover = map {
45                 my $fontcover = $font{$_};
46                 ($_ => scalar grep { $fontcover->{$_} } @chars);
47         } keys %font;
48         $cover{-count} = scalar @chars;
49         $cover{-chars} = [ map { ord } sort @chars ];
50
51         say $name.' => '.pp(\%cover).',';
52 }
53 say '}';
54
55 __END__
56
57 =head1 NAME
58
59 mkfontinfo - Prepare font coverage of various character groups
60
61 =head1 SYNOPSIS
62
63     mkfontinfo > unicode-cover.inc.pl
64
65 Test by finding the number of cyrillic characters in DejaVu Sans:
66
67     perl -E'$f = do "unicode-cover.inc.pl"; say $f->{Cyrillic}->{dvsans}'
68
69 =head1 AUTHOR
70
71 Mischa POSLAWSKY <perl@shiar.org>
72
73 =head1 LICENSE
74
75 Licensed under the GNU Affero General Public License version 3.
76