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