Last active
March 15, 2019 23:49
-
-
Save bobh0303/9e29be8b3dbf161a0a1cfe69f8c77153 to your computer and use it in GitHub Desktop.
Find cmap based on caller-specified order of preference [updated]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use Font::TTF::Font; | |
sub find_cmap | |
{ | |
my ($f, $order) = @_; | |
# Should do parameter validation here | |
my ($cmap, %cmaps, $i); | |
$cmap = $f->{'cmap'}->read; | |
for ($i = 0; $i < $cmap->{'Num'}; $i++) | |
{ | |
my $s = $cmap->{'Tables'}[$i]; | |
my $key = "$s->{'Platform'}/$s->{'Encoding'}"; | |
$cmaps{$key} = $s; | |
print "candidate: $key\n"; | |
} | |
foreach (@{$order}) | |
{ | |
if (exists $cmaps{$_}) | |
{ | |
$cmap->{' mstable'} = $cmaps{$_}; | |
last; | |
} | |
} | |
$cmap->{' mstable'}; | |
} | |
my $fontfile = $ARGV[0] || "NotoSansJP-Regular.otf"; | |
my $f = Font::TTF::Font->open($fontfile) || die "error opening TrueType font '$fontfile'.\n"; | |
my $cmap = find_cmap($f, [qw( 0/6 0/4 3/10 0/3 3/1)]); | |
die "No useful cmap found" unless $cmap; | |
printf "found: %d/%d\n", $cmap->{'Platform'}, $cmap->{'Encoding'}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment