#!/usr/bin/env perl
use 5.014;
-use strict;
use warnings;
use Data::Dump 'pp';
use Getopt::Long;
use Cwd 'abs_path';
-our $VERSION = '1.00';
+our $VERSION = '1.02';
GetOptions(\my %opt,
'verbose|v!',
);
my ($ttfuri, $outfile) = @ARGV;
-$ttfuri or die "usage error\n";
-
-my $fontinc = $ENV{FONTINC} // 'data/font,~/.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";
}
{
- my $ttf = Font::TTF::Font->open($ttfuri)
- or die "Cannot open truetype in $ttfuri: $!";
+ 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 = ($ttfuri =~ m{([^/.]+) (?:[.]ttf)? \z}msx)[0];
+ my ($ttfname, @ttfext) = split /\./, ($ttfuri =~ m{([^/]+)\z}ms)[0];
my $ttfmeta = $ttf->{name}->read;
my %meta = (
source => abs_path($ttfuri) =~ m{(^/usr/.+ | [^/]+) \z}msx,
$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), ',';
+ $meta{os} = $_ for "@ttfext[0 .. $#ttfext-1]" || (
+ $meta{copyright} =~ /\bGoogle\b/ ? "Android" :
+ $ttfname eq 'arialuni' ? 'win2k' :
+ ()
+ );
+ $meta{oscorp} = (
+ s/^mac10/OS X 10./ ? 'Apple' :
+ s/^win(.+)/'Windows '.($1 eq '2k' ? 2000 : $1)/e ? 'Microsoft' :
+ m/^Android/ ? 'Google' :
+ undef
+ ) for $meta{os} || ();
my $support = $ttf->{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);
+ $meta{cover} = [sort { $a <=> $b } keys %$support];
+
+ say "# automatically generated by $0";
+ say '+', pp(\%meta);
}
__END__