font: mkttfinfo: strip file extensions from name
[sheet.git] / tools / mkttfinfo
index 72564bffa9e656f16103fa91d4ef285a0d484291..988fcdccdf51adfd8aa33e3daf77eed864985299 100755 (executable)
@@ -30,12 +30,19 @@ for ($outfile || ()) {
 }
 
 {
-       my $ttf = Font::TTF::Font->open($ttfuri) or do {
-               warn "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,
@@ -59,7 +66,7 @@ for ($outfile || ()) {
        say '+', pp(\%meta), ',';
 
        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);
 }