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

=head1 NAME

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

=cut

use strict;
use Font::TTF::Utils;
use Font::TTF::AATutils;
use Font::TTF::Mort::Subtable;
use IO::File;

=head2 $t-&gt;new

=cut

sub new
{
    my ($class, %parms) = @_;
    my ($self) = {};
    my ($p);

    $class = ref($class) || $class;
    foreach $p (keys %parms)
    { $self-&gt;{" $p"} = $parms{$p}; }
    bless $self, $class;
}

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

Reads the chain into memory

=cut

sub read
{
    my ($self, $fh) = @_;
    my ($dat);

    my $chainStart = $fh-&gt;tell();
    $fh-&gt;read($dat, 12);
    my ($defaultFlags, $chainLength, $nFeatureEntries, $nSubtables) = TTF_Unpack("LLSS", $dat);

    my $featureEntries = [];
    foreach (1 .. $nFeatureEntries) {
        $fh-&gt;read($dat, 12);
        my ($featureType, $featureSetting, $enableFlags, $disableFlags) = TTF_Unpack("SSLL", $dat);
        push @$featureEntries,    {
                                    'type'        =&gt; $featureType,
                                    'setting'    =&gt; $featureSetting,
                                    'enable'    =&gt; $enableFlags,
                                    'disable'    =&gt; $disableFlags
                                };
    }

    my $subtables = [];
    foreach (1 .. $nSubtables) {
        my $subtableStart = $fh-&gt;tell();
        
        $fh-&gt;read($dat, 8);
        my ($length, $coverage, $subFeatureFlags) = TTF_Unpack("SSL", $dat);
        my $type = $coverage &amp; 0x0007;

        my $subtable = Font::TTF::Mort::Subtable-&gt;create($type, $coverage, $subFeatureFlags, $length);
        $subtable-&gt;read($fh);
        $subtable-&gt;{' PARENT'} = $self;
        
        push @$subtables, $subtable;
        $fh-&gt;seek($subtableStart + $length, IO::File::SEEK_SET);
    }
    
    $self-&gt;{'defaultFlags'} = $defaultFlags;
    $self-&gt;{'featureEntries'} = $featureEntries;
    $self-&gt;{'subtables'} = $subtables;

    $fh-&gt;seek($chainStart + $chainLength, IO::File::SEEK_SET);

    $self;
}

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

Writes the table to a file either from memory or by copying

=cut

sub out
{
    my ($self, $fh) = @_;
    
    my $chainStart = $fh-&gt;tell();
    my ($featureEntries, $subtables) = ($_-&gt;{'featureEntries'}, $_-&gt;{'subtables'});
    $fh-&gt;print(TTF_Pack("LLSS", $_-&gt;{'defaultFlags'}, 0, scalar @$featureEntries, scalar @$subtables)); # placeholder for length
    
    foreach (@$featureEntries) {
        $fh-&gt;print(TTF_Pack("SSLL", $_-&gt;{'type'}, $_-&gt;{'setting'}, $_-&gt;{'enable'}, $_-&gt;{'disable'}));
    }
    
    foreach (@$subtables) {
        $_-&gt;out($fh);
    }
    
    my $chainLength = $fh-&gt;tell() - $chainStart;
    $fh-&gt;seek($chainStart + 4, IO::File::SEEK_SET);
    $fh-&gt;print(pack("N", $chainLength));
    $fh-&gt;seek($chainStart + $chainLength, IO::File::SEEK_SET);
}

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

Prints a human-readable representation of the chain

=cut

sub feat
{
    my ($self) = @_;
    
    my $feat = $self-&gt;{' PARENT'}{' PARENT'}{'feat'};
    if (defined $feat) {
        $feat-&gt;read;
    }
    else {
        $feat = {};
    }
    
    return $feat;
}

sub print
{
    my ($self, $fh) = @_;
    
    $fh-&gt;printf("version %f\n", $self-&gt;{'version'});
    
    my $defaultFlags = $self-&gt;{'defaultFlags'};
    $fh-&gt;printf("chain: defaultFlags = %08x\n", $defaultFlags);
    
    my $feat = $self-&gt;feat();
    my $featureEntries = $self-&gt;{'featureEntries'};
    foreach (@$featureEntries) {
        $fh-&gt;printf("\tfeature %d, setting %d : enableFlags = %08x, disableFlags = %08x # '%s: %s'\n",
                    $_-&gt;{'type'}, $_-&gt;{'setting'}, $_-&gt;{'enable'}, $_-&gt;{'disable'},
                    $feat-&gt;settingName($_-&gt;{'type'}, $_-&gt;{'setting'}));
    }
    
    my $subtables = $self-&gt;{'subtables'};
    foreach (@$subtables) {
        my $type = $_-&gt;{'type'};
        my $subFeatureFlags = $_-&gt;{'subFeatureFlags'};
        $fh-&gt;printf("\n\t%s table, %s, %s, subFeatureFlags = %08x # %s (%s)\n",
                    subtable_type_($type), $_-&gt;{'direction'}, $_-&gt;{'orientation'}, $subFeatureFlags,
                    "Default " . ((($subFeatureFlags &amp; $defaultFlags) != 0) ? "On" : "Off"),
                    join(", ",
                        map {
                            join(": ", $feat-&gt;settingName($_-&gt;{'type'}, $_-&gt;{'setting'}) )
                        } grep { ($_-&gt;{'enable'} &amp; $subFeatureFlags) != 0 } @$featureEntries
                    ) );
        
        $_-&gt;print($fh);
    }
}

sub subtable_type_
{
    my ($val) = @_;
    my ($res);
    
    my @types =    (
                    'Rearrangement',
                    'Contextual',
                    'Ligature',
                    undef,
                    'Non-contextual',
                    'Insertion',
                );
    $res = $types[$val] or ('Undefined (' . $val . ')');
    
    $res;
}

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>