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

=head1 NAME

Font::TTF::Mort::Insertion - Insertion Mort subtable for AAT

=head1 METHODS

=cut

use strict;
use vars qw(@ISA);
use Font::TTF::Utils;
use Font::TTF::AATutils;
use IO::File;

@ISA = qw(Font::TTF::Mort::Subtable);

sub new
{
    my ($class, $direction, $orientation, $subFeatureFlags) = @_;
    my ($self) = {
                    'direction'            =&gt; $direction,
                    'orientation'        =&gt; $orientation,
                    'subFeatureFlags'    =&gt; $subFeatureFlags
                };

    $class = ref($class) || $class;
    bless $self, $class;
}

=head2 $t-&gt;read

Reads the table into memory

=cut

sub read
{
    my ($self, $fh) = @_;
    my ($dat);
    
    my $subtableStart = $fh-&gt;tell();

    my $stateTableStart = $fh-&gt;tell();
    my ($classes, $states, $entries) = AAT_read_state_table($fh, 2);
    
    my %insertListHash;
    my $insertLists;
    foreach (@$entries) {
        my $flags = $_-&gt;{'flags'};
        my @insertCount = (($flags &amp; 0x03e0) &gt;&gt; 5, ($flags &amp; 0x001f));
        my $actions = $_-&gt;{'actions'};
        foreach (0 .. 1) {
            if ($insertCount[$_] &gt; 0) {
                $fh-&gt;seek($stateTableStart + $actions-&gt;[$_], IO::File::SEEK_SET);
                $fh-&gt;read($dat, $insertCount[$_] * 2);
                if (not defined $insertListHash{$dat}) {
                    push @$insertLists, [unpack("n*", $dat)];
                    $insertListHash{$dat} = $#$insertLists;
                }
                $actions-&gt;[$_] = $insertListHash{$dat};
            }
            else {
                $actions-&gt;[$_] = undef;
            }
        }
    }

    $self-&gt;{'classes'} = $classes;
    $self-&gt;{'states'} = $states;
    $self-&gt;{'insertLists'} = $insertLists;
            
    $self;
}

=head2 $t-&gt;pack_sub()

=cut

sub pack_sub
{
    my ($self) = @_;
    
    my ($dat) = pack("nnnn", (0) x 4);
    
    my $classTable = length($dat);
    my $classes = $self-&gt;{'classes'};
    $dat .= AAT_pack_classes($classes);
    
    my $stateArray = length($dat);
    my $states = $self-&gt;{'states'};
    my ($dat1, $stateSize, $entries) = AAT_pack_states($classes, $stateArray, $states, 
            sub {
                my $actions = $_-&gt;{'actions'};
                ( $_-&gt;{'flags'}, @$actions )
            }
        );
    $dat .= $dat1;

    my $entryTable = length($dat);
    my $offset = ($entryTable + 8 * @$entries);
    my @insListOffsets;
    my $insertLists = $self-&gt;{'insertLists'};
    foreach (@$insertLists) {
        push @insListOffsets, $offset;
        $offset += 2 * scalar @$_;
    }
    foreach (@$entries) {
        my ($nextState, $flags, @lists) = split /,/;
        $flags &amp;= ~0x03ff;
        $flags |= (scalar @{$insertLists-&gt;[$lists[0]]}) &lt;&lt; 5 if $lists[0] ne '';
        $flags |= (scalar @{$insertLists-&gt;[$lists[1]]}) if $lists[1] ne '';
        $dat .= pack("nnnn", $nextState, $flags,
                    map { $_ eq '' ? 0 : $insListOffsets[$_] } @lists);
    }
    
    foreach (@$insertLists) {
        $dat .= pack("n*", @$_);
    }

    $dat1 = pack("nnnn", $stateSize, $classTable, $stateArray, $entryTable);
    substr($dat, 0, length($dat1)) = $dat1;

    return $dat;
}

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

Prints a human-readable representation of the table

=cut

sub print
{
    my ($self, $fh) = @_;
    
    my $post = $self-&gt;post();
    
    $fh = 'STDOUT' unless defined $fh;

    $self-&gt;print_classes($fh);
    
    $fh-&gt;print("\n");
    my $states = $self-&gt;{'states'};
    foreach (0 .. $#$states) {
        $fh-&gt;printf("\t\tState %d:", $_);
        my $state = $states-&gt;[$_];
        foreach (@$state) {
            my $flags;
            $flags .= "!" if ($_-&gt;{'flags'} &amp; 0x4000);
            $flags .= "*" if ($_-&gt;{'flags'} &amp; 0x8000);
            my $actions = $_-&gt;{'actions'};
            $fh-&gt;printf("\t(%s%d,%s,%s)", $flags, $_-&gt;{'nextState'}, map { defined $_ ? $_ : "=" } @$actions);
        }
        $fh-&gt;print("\n");
    }

    $fh-&gt;print("\n");
    my $insertLists = $self-&gt;{'insertLists'};
    foreach (0 .. $#$insertLists) {
        my $insertList = $insertLists-&gt;[$_];
        $fh-&gt;printf("\t\tList %d: %s\n", $_, join(", ", map { $_ . " [" . $post-&gt;{'VAL'}[$_] . "]" } @$insertList));
    }
}

1;

=head1 BUGS

None known

=head1 AUTHOR

Jonathan Kew L&lt;Jonathan_Kew@sil.org&gt;. See L&lt;Font::TTF::Font&gt; for copyright and
licensing.

=cut

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