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

=head1 NAME

Font::TTF::GSUB - Module support for the GSUB table in conjunction with TTOpen

=head1 DESCRIPTION

Handles the GSUB subtables in relation to Ttopen tables. Due to the variety of
different lookup types, the data structures are not all that straightforward,
although I have tried to make life easy for myself when using this!

=head1 INSTANCE VARIABLES

The structure of a GSUB table is the same as that given in L&lt;Font::TTF::Ttopen&gt;.
Here we give some of the semantics specific to GSUB lookups.

=over 4

=item ACTION_TYPE

This is a string taking one of 4 values indicating the nature of the information
in the ACTION array of the rule:

=over 8

=item g

The action contains a string of glyphs to replace the match string by

=item l

The action array contains a list of lookups and offsets to run, in order, on
the matched string

=item a

The action array is an unordered set of optional replacements for the matched
glyph. The application should make the selection somehow.

=item o

The action array is empty (in fact there is no rule array for this type of
rule) and the ADJUST value should be added to the glyph id to find the replacement
glyph id value

=back

=item MATCH_TYPE

This indicates which type of information the various MATCH arrays (MATCH, PRE,
POST) hold in the rule:

=over 8

=item g

The array holds a string of glyph ids which should match exactly

=item c

The array holds a sequence of class definitions which each glyph should
correspondingly match to

=item o

The array holds offsets to coverage tables

=back

=back

=head1 CORRESPONDANCE TO LAYOUT TYPES

The following table gives the values for ACTION_TYPE and MATCH_TYPE for each
of the 11 different lookup types found in the GSUB table definition I have:

                1.1 1.2 2   3   4   5.1 5.2 5.3 6.1 6.2 6.3
  ACTION_TYPE    o   g  g   a   g    l   l   l   l   l   l
  MATCH_TYPE                    g    g   c   o   g   c   o

Hopefully, the rest of the uses of the variables should make sense from this
table.

=head1 METHODS

=cut

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

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

=head2 $t-&gt;read_sub($fh, $lookup, $index)

Asked by the superclass to read in from the given file the indexth subtable from
lookup number lookup. The file is positioned ready for the read.

=cut

sub read_sub
{
    my ($self, $fh, $main_lookup, $sindex) = @_;
    my ($type) = $main_lookup-&gt;{'TYPE'};
    my ($loc) = $fh-&gt;tell();
    my ($lookup) = $main_lookup-&gt;{'SUB'}[$sindex];
    my ($dat, $s, @subst, $t, $fmt, $cover, $count, $mcount, $scount, $i, $gid);
    my (@srec);

    if ($type == 6)
    {
        $fh-&gt;read($dat, 4);
        ($fmt, $cover) = TTF_Unpack('S2', $dat);
        if ($fmt &lt; 3)
        {
            $fh-&gt;read($dat, 2);
            $count = TTF_Unpack('S', $dat);
        }
    } else
    {
        $fh-&gt;read($dat, 6);
        ($fmt, $cover, $count) = TTF_Unpack("S3", $dat);
    }
    unless ($fmt == 3 &amp;&amp; ($type == 5 || $type == 6))
    { $lookup-&gt;{'COVERAGE'} = $self-&gt;read_cover($cover, $loc, $lookup, $fh, 1); }

    $lookup-&gt;{'FORMAT'} = $fmt;
    if ($type == 1 &amp;&amp; $fmt == 1)
    {
        $count -= 65536 if ($count &gt; 32767);
        $lookup-&gt;{'ADJUST'} = $count;
        $lookup-&gt;{'ACTION_TYPE'} = 'o';
    } elsif ($type == 1 &amp;&amp; $fmt == 2)
    {
        $fh-&gt;read($dat, $count &lt;&lt; 1);
        @subst = TTF_Unpack('S*', $dat);
        foreach $s (@subst)
        { push(@{$lookup-&gt;{'RULES'}}, [{'ACTION' =&gt; [$s]}]); }
        $lookup-&gt;{'ACTION_TYPE'} = 'g';
    } elsif ($type == 2 || $type == 3)
    {
        $fh-&gt;read($dat, $count &lt;&lt; 1);       # number of offsets
        foreach $s (TTF_Unpack('S*', $dat))
        {
            $fh-&gt;seek($loc + $s, 0);
            $fh-&gt;read($dat, 2);
            $t = TTF_Unpack('S', $dat);
            $fh-&gt;read($dat, $t &lt;&lt; 1);
            push(@{$lookup-&gt;{'RULES'}}, [{'ACTION' =&gt; [TTF_Unpack('S*', $dat)]}]);
        }
        $lookup-&gt;{'ACTION_TYPE'} = ($type == 2 ? 'g' : 'a');
    } elsif ($type == 4)
    {
        $fh-&gt;read($dat, $count &lt;&lt; 1);
        foreach $s (TTF_Unpack('S*', $dat))
        {
            @subst = ();
            $fh-&gt;seek($loc + $s, 0);
            $fh-&gt;read($dat, 2);
            $t = TTF_Unpack('S', $dat);
            $fh-&gt;read($dat, $t &lt;&lt; 1);
            foreach $t (TTF_Unpack('S*', $dat))
            {
                $fh-&gt;seek($loc + $s + $t, 0);
                $fh-&gt;read($dat, 4);
                ($gid, $mcount) = TTF_Unpack('S2', $dat);
                $fh-&gt;read($dat, ($mcount - 1) &lt;&lt; 1);
                push(@subst, {'ACTION' =&gt; [$gid], 'MATCH' =&gt; [TTF_Unpack('S*', $dat)]});
            }
            push(@{$lookup-&gt;{'RULES'}}, [@subst]);
        }
        $lookup-&gt;{'ACTION_TYPE'} = 'g';
        $lookup-&gt;{'MATCH_TYPE'} = 'g';
    } elsif ($type == 5 || $type == 6)
    { $self-&gt;read_context($lookup, $fh, $type, $fmt, $cover, $count, $loc); }
    $lookup;
}


=head2 $t-&gt;extension

Returns the table type number for the extension table

=cut

sub extension
{ return 7; }


=head2 $t-&gt;out_sub($fh, $lookup, $index)

Passed the filehandle to output to, suitably positioned, the lookup and subtable
index, this function outputs the subtable to $fh at that point.

=cut

sub out_sub
{
    my ($self, $fh, $main_lookup, $index, $ctables, $base) = @_;
    my ($type) = $main_lookup-&gt;{'TYPE'};
    my ($lookup) = $main_lookup-&gt;{'SUB'}[$index];
    my ($fmt) = $lookup-&gt;{'FORMAT'};
    my ($out, $r, $t, $i, $j, $offc, $offd, $numd);
    my ($num) = $#{$lookup-&gt;{'RULES'}} + 1;

    if ($type == 1)
    {
        $out = pack("nn", $fmt, Font::TTF::Ttopen::ref_cache($lookup-&gt;{'COVERAGE'}, $ctables, 2 + $base));
        if ($fmt == 1)
        { $out .= pack("n", $lookup-&gt;{'ADJUST'}); }
        else
        {
            $out .= pack("n", $num);
            foreach $r (@{$lookup-&gt;{'RULES'}})
            { $out .= pack("n", $r-&gt;[0]{'ACTION'}[0]); }
        }
    } elsif ($type == 2 || $type == 3)
    {
        $out = pack("nnn", $fmt, Font::TTF::Ttopen::ref_cache($lookup-&gt;{'COVERAGE'}, $ctables, 2 + $base),
                            $num);
        $out .= pack('n*', (0) x $num);
        $offc = length($out);
        for ($i = 0; $i &lt; $num; $i++)
        {
            $out .= pack("n*", $#{$lookup-&gt;{'RULES'}[$i][0]{'ACTION'}} + 1,
                                    @{$lookup-&gt;{'RULES'}[$i][0]{'ACTION'}});
            substr($out, ($i &lt;&lt; 1) + 6, 2) = pack('n', $offc);
            $offc = length($out);
        }
    } elsif ($type == 4 || $type == 5 || $type == 6)
    { $out = $self-&gt;out_context($lookup, $fh, $type, $fmt, $ctables, $out, $num, $base); }
#    Font::TTF::Ttopen::out_final($fh, $out, [[$ctables, 0]]);
    $out;
}

=head1 AUTHOR

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

=cut

1;

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