<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Font::TTF::Kern;

=head1 NAME

Font::TTF::Kern - Kerning tables

=head1 DESCRIPTION

Kerning tables are held as an ordered collection of subtables each giving
incremental information regarding the kerning of various pairs of glyphs.

The basic structure of the kerning data structure is:

    $kern = $f-&gt;{'kern'}{'tables'}[$tnum]{'kerns'}{$leftnum}{$rightnum};

Due to the possible complexity of some kerning tables the above information
is insufficient. Reference also needs to be made to the type of the table and
the coverage field.

=head1 INSTANCE VARIABLES

The instance variables for a kerning table are relatively straightforward.

=over 4

=item Version

Version number of the kerning table

=item Num

Number of subtables in the kerning table

=item tables

Array of subtables in the kerning table

=over 4

Each subtable has a number of instance variables.

=item kern

A two level hash array containing kerning values. The indexing is left
value and then right value. In the case of type 2 tables, the indexing
is via left class and right class. It may seem using hashes is strange,
but most tables are not type 2 and this method saves empty array values.

=item type

Stores the table type. Only type 0 and type 2 tables are specified for
TrueType so far.

=item coverage

A bit field of coverage information regarding the kerning value. See the
TrueType specification for details.

=item Version

Contains the version number of the table.

=item Num

Number of kerning pairs in this type 0 table.

=item left

An array indexed by glyph - left_first which returns a class number for
the glyph in type 2 tables.

=item right

An array indexed by glyph - right_first which returns a class number for
the glyph in type 2 tables.

=item left_first

the glyph number of the first element in the left array for type 2 tables.

=item right_first

the glyph number of the first element in the right array for type 2 tables.

=item num_left

Number of left classes

=item num_right

Number of right classes

=back

=back

=head1 METHODS

=cut

use strict;
use vars qw(@ISA);
use Font::TTF::Utils;
use Font::TTF::Table;

@ISA = qw(Font::TTF::Table);

=head2 $t-&gt;read

Reads the whole kerning table into structures

=cut

sub read
{
    my ($self) = @_;
    my ($fh) = $self-&gt;{' INFILE'};
    my ($dat, $i, $numt, $len, $cov, $t);

    $self-&gt;SUPER::read or return $self;

    $fh-&gt;read($dat, 4);
    ($self-&gt;{'Version'}, $numt) = unpack("n2", $dat);
    $self-&gt;{'Num'} = $numt;

    for ($i = 0; $i &lt; $numt; $i++)
    {
        $t = {};
        $fh-&gt;read($dat, 6);
        ($t-&gt;{'Version'}, $len, $cov) = unpack("n3", $dat);
        $t-&gt;{'coverage'} = $cov &amp; 255;
        $t-&gt;{'type'} = $cov &gt;&gt; 8;
        $fh-&gt;read($dat, $len - 6);
        if ($t-&gt;{'Version'} == 0)
        {
            $t-&gt;{'Num'} = unpack("n", $dat);
            my (@vals) = unpack("n*", substr($dat, 8, $t-&gt;{'Num'} * 6));
            for (0 .. ($t-&gt;{'Num'} - 1))
            {
                my ($f, $l, $v);
                $f = shift @vals;
                $l = shift @vals;
                $v = shift @vals;
                $v -= 65536 if ($v &gt; 32767);
                $t-&gt;{'kern'}{$f}{$l} = $v;
            }
        } elsif ($t-&gt;{'Version'} == 2)
        {
            my ($wid, $off, $numg, $maxl, $maxr, $j);
            
            $wid = unpack("n", $dat);
            $off = unpack("n", substr($dat, 2));
            ($t-&gt;{'left_first'}, $numg) = unpack("n2", substr($dat, $off));
            $t-&gt;{'left'} = [unpack("C$numg", substr($dat, $off + 4))];
            foreach (@{$t-&gt;{'left'}})
            {
                $_ /= $wid;
                $maxl = $_ if ($_ &gt; $maxl);
            }
            $t-&gt;{'left_max'} = $maxl;

            $off = unpack("n", substr($dat, 4));
            ($t-&gt;{'right_first'}, $numg) = unpack("n2", substr($dat, $off));
            $t-&gt;{'right'} = [unpack("C$numg", substr($dat, $off + 4))];
            foreach (@{$t-&gt;{'right'}})
            {
                $_ &gt;&gt;= 1;
                $maxr = $_ if ($_ &gt; $maxr);
            }
            $t-&gt;{'right_max'} = $maxr;

            $off = unpack("n", substr($dat, 6));
            for ($j = 0; $j &lt;= $maxl; $j++)
            {
                my ($k) = 0;

                map { $t-&gt;{'kern'}{$j}{$k} = $_ if $_; $k++; }
                        unpack("n$maxr", substr($dat, $off + $wid * $j));
            }
        }
        push (@{$self-&gt;{'tables'}}, $t);
    }
    $self;
}


=head2 $t-&gt;out($fh)

Outputs the kerning tables to the given file

=cut

sub out
{
    my ($self, $fh) = @_;
    my ($i, $l, $r, $loc, $loc1, $t);

    return $self-&gt;SUPER::out($fh) unless ($self-&gt;{' read'});

    $fh-&gt;print(pack("n2", $self-&gt;{'Version'}, $self-&gt;{'Num'}));
    for ($i = 0; $i &lt; $self-&gt;{'Num'}; $i++)
    {
        $t = $self-&gt;{'tables'}[$i];
        $loc = $fh-&gt;tell();

        $fh-&gt;print(pack("nnn", $t-&gt;{'Version'}, 0, $t-&gt;{'coverage'}));
        if ($t-&gt;{'Version'} == 0)
        {
            my ($dat);
            foreach $l (sort {$a &lt;=&gt; $b} keys %{$t-&gt;{'kern'}})
            {
                foreach $r (sort {$a &lt;=&gt; $b} keys %{$t-&gt;{'kern'}{$l}})
                { $dat .= TTF_Pack("SSs", $l, $r, $t-&gt;{'kern'}{$l}{$r}); }
            }
            $fh-&gt;print(TTF_Pack("SSSS", Font::TTF::Utils::TTF_bininfo(length($dat) / 6, 6)));
            $fh-&gt;print($dat);
        } elsif ($t-&gt;{'Version'} == 2)
        {
            my ($arr);

            $fh-&gt;print(pack("nnnn", $t-&gt;{'right_max'} &lt;&lt; 1, 8, ($#{$t-&gt;{'left'}} + 7) &lt;&lt; 1,
                    ($#{$t-&gt;{'left'}} + $#{$t-&gt;{'right'}} + 10) &lt;&lt; 1));

            $fh-&gt;print(pack("nn", $t-&gt;{'left_first'}, $#{$t-&gt;{'left'}} + 1));
            foreach (@{$t-&gt;{'left'}})
            { $fh-&gt;print(pack("C", $_ * (($t-&gt;{'left_max'} + 1) &lt;&lt; 1))); }

            $fh-&gt;print(pack("nn", $t-&gt;{'right_first'}, $#{$t-&gt;{'right'}} + 1));
            foreach (@{$t-&gt;{'right'}})
            { $fh-&gt;print(pack("C", $_ &lt;&lt; 1)); }

            $arr = "\000\000" x (($t-&gt;{'left_max'} + 1) * ($t-&gt;{'right_max'} + 1));
            foreach $l (keys %{$t-&gt;{'kern'}})
            {
                foreach $r (keys %{$t-&gt;{'kern'}{$l}})
                { substr($arr, ($l * ($t-&gt;{'left_max'} + 1) + $r) &lt;&lt; 1, 2)
                        = pack("n", $t-&gt;{'kern'}{$l}{$r}); }
            }
            $fh-&gt;print($arr);
        }
        $loc1 = $fh-&gt;tell();
        $fh-&gt;seek($loc + 2, 0);
        $fh-&gt;print(pack("n", $loc1 - $loc));
        $fh-&gt;seek($loc1, 0);
    }
    $self;
}


=head2 $t-&gt;XML_element($context, $depth, $key, $value)

Handles outputting the kern hash into XML a little more tidily

=cut

sub XML_element
{
    my ($self) = shift;
    my ($context, $depth, $key, $value) = @_;
    my ($fh) = $context-&gt;{'fh'};
    my ($f, $l);

    return $self-&gt;SUPER::XML_element(@_) unless ($key eq 'kern');
    $fh-&gt;print("$depth&lt;kern-table&gt;\n");
    foreach $f (sort {$a &lt;=&gt; $b} keys %{$value})
    {
        foreach $l (sort {$a &lt;=&gt; $b} keys %{$value-&gt;{$f}})
        { $fh-&gt;print("$depth$context-&gt;{'indent'}&lt;adjust first='$f' last='$l' dist='$value-&gt;{$f}{$l}'/&gt;\n"); }
    }
    $fh-&gt;print("$depth&lt;/kern-table&gt;\n");
    $self;
}

1;

=head1 BUGS

=over 4

=item *

Only supports kerning table types 0 &amp; 2.

=item *

No real support functions to I&lt;do&gt; anything with the kerning tables yet.

=back

=head1 AUTHOR

Martin Hosken Martin_Hosken@sil.org. See L&lt;Font::TTF::Font&gt; for copyright and
licensing.

=cut

</pre></body></html>