<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::Ligature;

=head1 NAME

Font::TTF::Mort::Ligature - Ligature 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 $stateTableStart = $fh-&gt;tell();
    my ($classes, $states, $entries) = AAT_read_state_table($fh, 0);
    
    $fh-&gt;seek($stateTableStart, IO::File::SEEK_SET);
    $fh-&gt;read($dat, 14);
    my ($stateSize, $classTable, $stateArray, $entryTable,
        $ligActionTable, $componentTable, $ligatureTable) = unpack("nnnnnnn", $dat);
    my $limits = [$classTable, $stateArray, $entryTable, $ligActionTable, $componentTable, $ligatureTable, $self-&gt;{'length'} - 8];
    
    my %actions;
    my $actionLists;
    foreach (@$entries) {
        my $offset = $_-&gt;{'flags'} &amp; 0x3fff;
        $_-&gt;{'flags'} &amp;= ~0x3fff;
        if ($offset != 0) {
            if (not defined $actions{$offset}) {
                $fh-&gt;seek($stateTableStart + $offset, IO::File::SEEK_SET);
                my $actionList;
                while (1) {
                    $fh-&gt;read($dat, 4);
                    my $action = unpack("N", $dat);
                    my ($last, $store, $component) = (($action &amp; 0x80000000) != 0, ($action &amp; 0xC0000000) != 0, ($action &amp; 0x3fffffff));
                    $component -= 0x40000000 if $component &gt; 0x1fffffff;
                    $component -= $componentTable / 2;
                    push @$actionList, { 'store' =&gt; $store, 'component' =&gt; $component };
                    last if $last;
                }
                push @$actionLists, $actionList;
                $actions{$offset} = $#$actionLists;
            }
            $_-&gt;{'actions'} = $actions{$offset};
        }
    }
    
    $self-&gt;{'componentTable'} = $componentTable;
    my $components = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $componentTable, $limits))];
    foreach (@$components) {
        $_ = ($_ - $ligatureTable) . " +" if $_ &gt;= $ligatureTable;
    }
    $self-&gt;{'components'} = $components;
    
    $self-&gt;{'ligatureTable'} = $ligatureTable;
    $self-&gt;{'ligatures'} = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $ligatureTable, $limits))];
    
    $self-&gt;{'classes'} = $classes;
    $self-&gt;{'states'} = $states;
    $self-&gt;{'actionLists'} = $actionLists;
        
    $self;
}

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

=cut

sub pack_sub
{
    my ($self) = @_;
    my ($dat);
    
    $dat .= pack("nnnnnnn", (0) x 7);    # placeholders for stateSize, classTable, stateArray, entryTable, actionLists, components, ligatures

    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 {
                ( $_-&gt;{'flags'} &amp; 0xc000, $_-&gt;{'actions'} )
            }
        );
    $dat .= $dat1;
    
    my $actionLists = $self-&gt;{'actionLists'};
    my %actionListOffset;
    my $actionListDataLength = 0;
    my @actionListEntries;
    foreach (0 .. $#$entries) {
        my ($nextState, $flags, $offset) = split(/,/, $entries-&gt;[$_]);
        if ($offset eq "") {
            $offset = undef;
        }
        else {
            if (defined $actionListOffset{$offset}) {
                $offset = $actionListOffset{$offset};
            }
            else {
                $actionListOffset{$offset} = $actionListDataLength;
                my $list = $actionLists-&gt;[$offset];
                $actionListDataLength += 4 * @$list;
                push @actionListEntries, $list;
                $offset = $actionListOffset{$offset};
            }
        }
        $entries-&gt;[$_] = [ $nextState, $flags, $offset ];
    }
    my $entryTable = length($dat);
    my $ligActionLists = ($entryTable + @$entries * 4 + 3) &amp; ~3;
    foreach (@$entries) {
        $_-&gt;[2] += $ligActionLists if defined $_-&gt;[2];
        $dat .= pack("nn", $_-&gt;[0], $_-&gt;[1] + $_-&gt;[2]);
    }
    $dat .= pack("C*", (0) x ($ligActionLists - $entryTable - @$entries * 4));
    
    die "internal error" unless length($dat) == $ligActionLists;
    
    my $componentTable = length($dat) + $actionListDataLength;
    my $actionList;
    foreach $actionList (@actionListEntries) {
        foreach (0 .. $#$actionList) {
            my $action = $actionList-&gt;[$_];
            my $val = $action-&gt;{'component'} + $componentTable / 2;
            $val += 0x40000000 if $val &lt; 0;
            $val &amp;= 0x3fffffff;
            $val |= 0x40000000 if $action-&gt;{'store'};
            $val |= 0x80000000 if $_ == $#$actionList;
            $dat .= pack("N", $val);
        }
    }

    die "internal error" unless length($dat) == $componentTable;

    my $components = $self-&gt;{'components'};
    my $ligatureTable = $componentTable + @$components * 2;
    $dat .= pack("n*", map { (index($_, '+') &gt;= 0 ? $ligatureTable : 0) + $_ } @$components);
    
    my $ligatures = $self-&gt;{'ligatures'};
    $dat .= pack("n*", @$ligatures);
    
    $dat1 = pack("nnnnnnn", $stateSize, $classTable, $stateArray, $entryTable, $ligActionLists, $componentTable, $ligatureTable);
    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);
            $fh-&gt;printf("\t(%s%d,%s)", $flags, $_-&gt;{'nextState'}, defined $_-&gt;{'actions'} ? $_-&gt;{'actions'} : "=");
        }
        $fh-&gt;print("\n");
    }

    $fh-&gt;print("\n");
    my $actionLists = $self-&gt;{'actionLists'};
    foreach (0 .. $#$actionLists) {
        $fh-&gt;printf("\t\tList %d:\t", $_);
        my $actionList = $actionLists-&gt;[$_];
        $fh-&gt;printf("%s\n", join(", ", map { ($_-&gt;{'component'} . ($_-&gt;{'store'} ? "*" : "") ) } @$actionList));
    }

    my $ligatureTable = $self-&gt;{'ligatureTable'};

    $fh-&gt;print("\n");
    my $components = $self-&gt;{'components'};
    foreach (0 .. $#$components) {
        $fh-&gt;printf("\t\tComponent %d: %s\n", $_, $components-&gt;[$_]);
    }
    
    $fh-&gt;print("\n");
    my $ligatures = $self-&gt;{'ligatures'};
    foreach (0 .. $#$ligatures) {
        $fh-&gt;printf("\t\tLigature %d: %d [%s]\n", $_, $ligatures-&gt;[$_], $post-&gt;{'VAL'}[$ligatures-&gt;[$_]]);
    }
}

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>