font: mkttfinfo: no glob; exit codes
[sheet.git] / tools / mkttfinfo
index 1d8e69906269d95af82fb6844798c4b126f9d06d..72564bffa9e656f16103fa91d4ef285a0d484291 100755 (executable)
@@ -1,58 +1,47 @@
 #!/usr/bin/env perl
-use 5.010;
+use 5.014;
 use strict;
 use warnings;
 
 use Data::Dump 'pp';
 use Font::TTF::Font;
 use Getopt::Long;
+use Cwd 'abs_path';
 
-our $VERSION = '1.00';
+our $VERSION = '1.01';
 
-GetOptions(my %opt,
+GetOptions(\my %opt,
        'verbose|v!',
 );
 
 my ($ttfuri, $outfile) = @ARGV;
-$ttfuri or die "usage error\n";
-
-my $fontinc = $ENV{FONTINC} // '~/.fonts,/usr/share/fonts/truetype/*';
-if (!-e $ttfuri) {
-       my $found = (grep {-e} glob "{$fontinc}/$ttfuri")[0] or do {
-               warn "font not found: $ttfuri\n";
-               exit 1;
-       };
-       $ttfuri = $found;
-}
+$ttfuri or do {
+       warn "usage error: input font not specified\n";
+       exit 64; # EX_USAGE
+};
+-e $ttfuri or do {
+       warn "font not found: $ttfuri\n";
+       exit 66; # EX_NOINPUT
+};
 
 for ($outfile || ()) {
        open my $output, '>', $_ or die "Cannot write to $outfile: $!\n";
        select $output;
 }
 
-my %FONTID = (
-       'Times New Roman' => 'times',
-       'DejaVu Sans'     => 'dv ss',
-       'DejaVu Serif'    => 'dv serif',
-       'DejaVu Sans Mono'=> 'dv mono',
-       'Code2000'        => 'c2k',
-       'GNU Unifont'     => 'guf',
-       'Droid Sans'      => 'droid',
-       'Droid Serif'     => 'droid serif',
-       'Droid Sans Mono' => 'droid mono',
-);
-
 {
-       my $ttf = Font::TTF::Font->open($ttfuri)
-               or die "Cannot open truetype in $ttfuri: $!";
+       my $ttf = Font::TTF::Font->open($ttfuri) or do {
+               warn "Cannot open truetype in $ttfuri: $!";
+               exit 65; # EX_DATAERR
+       };
 
        my $ttfname = ($ttfuri =~ m{([^/.]+) (?:[.]ttf)? \z}msx)[0];
        my $ttfmeta = $ttf->{name}->read;
        my %meta = (
-               source   => $ttfuri =~ m{(^/usr/.+ | [^/]+) \z}msx,
+               source   => abs_path($ttfuri) =~ m{(^/usr/.+ | [^/]+) \z}msx,
                name     => $ttfmeta->find_name(4) || $ttfname,
-               revision => $ttf->{head}->{fontRevision},
-               version  => scalar $ttfmeta->find_name(5),
+               revision => sprintf('%g', $ttf->{head}->{fontRevision}),
+               version  => $ttfmeta->find_name(5) =~ s/\Aversion //ri =~ s/\s+\z//r,
                copyright=> scalar $ttfmeta->find_name(0),
                license  => $ttfmeta->find_name(14) || undef,
                date     => (map {
@@ -62,7 +51,9 @@ my %FONTID = (
                        }
                } $ttf->{head}->getdate),
        );
-       $meta{abbr} = $FONTID{ $meta{name} } // lc $ttfname;
+       $meta{abbr} = lc join '', $meta{name} =~ s/ MS$//r =~ m{
+               (?!Sans) (?<! [0-9]) ([[:upper:]0-9])
+       }gx;
 
        say "# automatically generated by $0";
        say '+', pp(\%meta), ',';