MAJOR=4
MINOR=63
DEVNAME=tty63
  7   #ˆl–ßi~” ”Ð?Je@‚ãn\ªbÑ¿�þYiÀ¬Ü£�’>Ñ[”€þÒ„-ÿÑÄÏ 4a    ##!/usr/bin/perl
# Debian required shebang above
# Original shebang below
# #! /usr/bin/env perl

=begin COPYRIGHT

----------------------------------------------------------------------------

    Copyright (C) 2005-2022 Marc Penninga.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation, either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to
        Free Software Foundation, Inc.,
        59 Temple Place,
        Suite 330,
        Boston, MA 02111-1307,
        USA

----------------------------------------------------------------------------

=end COPYRIGHT

=cut

use strict;
use warnings;

use File::Basename;
use Getopt::Long;
use Pod::Usage;

my $VERSION = "20220124";

parse_commandline();

my $afm = read_afm($ARGV[0]);
$afm->{kernpairs} = read_kpx($ARGV{kpx}, $afm->{kernpairs}) if $ARGV{kpx};

if ($ARGV{output}) {
    $ARGV{output} .= '.afm' unless $ARGV{output} =~ m/[.]afm\z/xms;
    open my $output, '>', $ARGV{output}
        or die "[ERROR] can't open '$ARGV{output}': $!";
    select $output;
}

# If user specified encoding, use that; else, use encoding from .afm file
my $enc = $ARGV{encoding} ? read_enc($ARGV{encoding})
        :                   get_enc($afm)
        ;
# Make lookup table to quickly see if a character is present in the encoding
my %char_in_enc = map { ($_ => 1) } @{$enc->{vector}};

print_fontinfo($afm->{fontinfo}, $enc->{scheme});
print_charmetrics($afm->{metrics}, $enc->{vector});
print_kerndata($afm->{kernpairs}, $afm->{trackkerns}, \%char_in_enc);
print_composites($afm->{composites}, \%char_in_enc);
print "EndFontMetrics\n";

close select *STDOUT if $ARGV{output};

print_mapentry($afm->{fontinfo}, , $enc->{scheme})
    if $ARGV{encoding} && $ARGV{output};

#-----------------------------------------------------------------------
# Read the command-line options
#-----------------------------------------------------------------------
sub parse_commandline {
    Getopt::Long::GetOptions(
        'help|?'     =>  sub { pod2usage(-verbose => 1) },
        'version'    =>  sub { print "$VERSION\n"; exit },
        'encoding=s' => \$ARGV{encoding},
        'kpx=s'      => \$ARGV{kpx},
        'output=s'   => \$ARGV{output},
    )
    or pod2usage(-verbose => 0);

    pod2usage(-verbose => 0) if @ARGV != 1;

    return;
}

#-----------------------------------------------------------------------
# Read the input afm file
#-----------------------------------------------------------------------
sub read_afm {
    my $filename = shift;
    open my $afmfile, '<', $filename
        or die "[ERROR] can't open '$filename': $!";

    my %afm = (
        fontinfo   => [],
        metrics    => [],
        kernpairs  => [],
        trackkerns => [],
        composites => [],
    );

    until ((my $line = <$afmfile>) =~ m/StartCharMetrics/xms) {
        push @{$afm{fontinfo}}, $line;
    }

    for (<$afmfile>) {
        if (m/\A\s* C         \s/xms) { push @{$afm{metrics}},    $_; next }
        if (m/\A\s* KPX       \s/xms) { push @{$afm{kernpairs}},  $_; next }
        if (m/\A\s* TrackKern \s/xms) { push @{$afm{trackkerns}}, $_; next }
        if (m/\A\s* CC        \s/xms) { push @{$afm{composites}}, $_; next }
    }

    close $afmfile;

    return \%afm;
}

#-----------------------------------------------------------------------
# Read additional kerning pairs from the kpx file
#-----------------------------------------------------------------------
sub read_kpx {
    my ($filename, $kpx) = @_;
 