Skip to content

Instantly share code, notes, and snippets.

@bobh0303
Last active March 15, 2019 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobh0303/9e29be8b3dbf161a0a1cfe69f8c77153 to your computer and use it in GitHub Desktop.
Save bobh0303/9e29be8b3dbf161a0a1cfe69f8c77153 to your computer and use it in GitHub Desktop.
Find cmap based on caller-specified order of preference [updated]
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