move all generated includes into data/
[sheet.git] / tools / mkttfinfo
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4
5 use Data::Dump 'pp';
6 use Font::TTF::Font;
7 use Getopt::Long;
8 use Cwd 'abs_path';
9
10 our $VERSION = '1.01';
11
12 GetOptions(\my %opt,
13         'verbose|v!',
14 );
15
16 my ($ttfuri, $outfile) = @ARGV;
17 $ttfuri or do {
18         warn "usage error: input font not specified\n";
19         exit 64; # EX_USAGE
20 };
21 -e $ttfuri or do {
22         warn "font not found: $ttfuri\n";
23         exit 66; # EX_NOINPUT
24 };
25
26 for ($outfile || ()) {
27         open my $output, '>', $_ or die "Cannot write to $outfile: $!\n";
28         select $output;
29 }
30
31 {
32         my $ttf = eval {
33                 if ($ttfuri =~ /\.ttc\z/) {
34                         require Font::TTF::Ttc;
35                         my $collection = Font::TTF::Ttc->open($ttfuri) or die $!;
36                         return $collection->{directs}->[0];  # first sub-font object
37                 }
38                 return Font::TTF::Font->open($ttfuri);
39         } or do {
40                 warn "Cannot open font file $ttfuri: ", $@ // $!;
41                 exit 65; # EX_DATAERR
42         };
43
44         my ($ttfname, @ttfext) = split /\./, ($ttfuri =~ m{([^/]+)\z}ms)[0];
45         my $ttfmeta = $ttf->{name}->read;
46         my %meta = (
47                 source   => abs_path($ttfuri) =~ m{(^/usr/.+ | [^/]+) \z}msx,
48                 name     => $ttfmeta->find_name(4) || $ttfname,
49                 revision => sprintf('%g', $ttf->{head}->{fontRevision}),
50                 version  => $ttfmeta->find_name(5) =~ s/\Aversion //ri =~ s/\s+\z//r,
51                 copyright=> scalar $ttfmeta->find_name(0),
52                 license  => $ttfmeta->find_name(14) || undef,
53                 date     => (map {
54                         $_ && eval {
55                                 require Time::Piece;
56                                 Time::Piece->new($_)->datetime;
57                         }
58                 } $ttf->{head}->getdate),
59         );
60         $meta{abbr} = lc join '', $meta{name} =~ s/ MS$//r =~ m{
61                 (?!Sans) (?<! [0-9]) ([[:upper:]0-9])
62         }gx;
63         $meta{os} = $_ for "@ttfext[0 .. $#ttfext-1]" || (
64                 $meta{copyright} =~ /\bGoogle\b/ ? "Android" :
65                 $ttfname eq 'arialuni' ? 'win2k' :
66                 ()
67         );
68         $meta{oscorp} = (
69                 s/^mac10/OS X 10./ ? 'Apple' :
70                 s/^win(.+)/'Windows '.($1 eq '2k' ? 2000 : $1)/e ? 'Microsoft' :
71                 m/^Android/ ? 'Google' :
72                 undef
73         ) for $meta{os} || ();
74
75         say "# automatically generated by $0";
76         say '+', pp(\%meta), ',';
77
78         my $support = $ttf->{cmap}->find_ms->{val};
79         warn scalar keys %$support, " characters read from $ttfuri\n"
80                 if $opt{verbose};
81         say pp(sort { $a <=> $b } keys %$support);
82 }
83
84 __END__
85
86 =head1 NAME
87
88 mkttfinfo - Extract character coverage and metadata in TrueType font
89
90 =head1 SYNOPSIS
91
92     mkttfinfo [-v] <font.ttf> [<output.inc.pl>]
93
94 =head1 AUTHOR
95
96 Mischa POSLAWSKY <perl@shiar.org>
97
98 =head1 LICENSE
99
100 Licensed under the GNU Affero General Public License version 3.
101