X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/b5b3537710ed9f73e1c867e0cc27d50439eaf4cd..c133e6ad3027e095811931f3e0d21b818865e0e8:/tools/mkttfinfo diff --git a/tools/mkttfinfo b/tools/mkttfinfo index f69bbbb..2523b8f 100755 --- a/tools/mkttfinfo +++ b/tools/mkttfinfo @@ -1,43 +1,54 @@ #!/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.01'; + +GetOptions(\my %opt, + 'verbose|v!', +); my ($ttfuri, $outfile) = @ARGV; -$ttfuri or die "usage error\n"; +$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 || ()) { - !-e $_ or die "Output file $outfile already exists\n"; 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 $ttfname = ($ttfuri =~ m{([^/.]+) (?:[.]ttf)? \z}msx)[0]; + my $ttf = eval { + if ($ttfuri =~ /\.ttc\z/) { + require Font::TTF::Ttc; + my $collection = Font::TTF::Ttc->open($ttfuri) or die $!; + return $collection->{directs}->[0]; # first sub-font object + } + return Font::TTF::Font->open($ttfuri); + } or do { + warn "Cannot open font file $ttfuri: ", $@ // $!; + exit 65; # EX_DATAERR + }; + + my ($ttfname, @ttfext) = split /\./, ($ttfuri =~ m{([^/]+)\z}ms)[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 { @@ -47,11 +58,27 @@ my %FONTID = ( } } $ttf->{head}->getdate), ); - $meta{id} = $FONTID{ $meta{name} } // lc $ttfname; - say pp(\%meta), ','; + $meta{abbr} = lc join '', $meta{name} =~ s/ MS$//r =~ m{ + (?!Sans) (?{cmap}->find_ms->{val}; - warn scalar keys %$support, " characters read from $ttfname\n"; + warn scalar keys %$support, " characters read from $ttfuri\n" + if $opt{verbose}; say pp(sort { $a <=> $b } keys %$support); } @@ -63,7 +90,7 @@ mkttfinfo - Extract character coverage and metadata in TrueType font =head1 SYNOPSIS - mkttfinfo [] + mkttfinfo [-v] [] =head1 AUTHOR