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

=head1 NAME

Font::TTF::GDEF - Opentype GDEF table support

=head1 DESCRIPTION

The GDEF table contains various global lists of information which are apparantly
used in other places in an OpenType renderer. But precisely where is open to
speculation...

=head1 INSTANCE VARIABLES

There are 4 tables in the GDEF table, each with their own structure:

=over 4

=item GLYPH

This is an L&lt;Font::TTF::Coverage&gt; Class Definition table containing information
as to what type each glyph is.

=item ATTACH

The attach table consists of a coverage table and then attachment points for
each glyph in the coverage table:

=over 8

=item COVERAGE

This is a coverage table

=item POINTS

This is an array of point elements. Each element is an array of curve points
corresponding to the attachment points on that glyph. The order of the curve points
in the array corresponds to the attachment point number specified in the MARKS
coverage table (see below).

=back

=item LIG

This contains the ligature caret positioning information for ligature glyphs

=over 8

=item COVERAGE

A coverage table to say which glyphs are ligatures

=item LIGS

An array of elements for each ligature. Each element is an array of information
for each caret position in the ligature (there being number of components - 1 of
these, generally)

=over 12

=item FMT

This is the format of the information and is important to provide the semantics
for the value. This value must be set correctly before output

=item VAL

The value which has meaning according to FMT

=item DEVICE

For FMT = 3, a device table is also referenced which is stored here

=back

=back

=item MARKS

Due to confusion in the GDEF specification, this field is currently withdrawn until
the confusion is resolved. That way, perhaps this stuff will work!

This class definition table stores the mark attachment point numbers for each
attachment mark, to indicate which attachment point the mark attaches to on its
base glyph.

=back


=head1 METHODS

=cut

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

@ISA = qw(Font::TTF::Table);
$new_gdef = 1;

=head2 $t-&gt;read

Reads the table into the data structure

=cut

sub read
{
    my ($self) = @_;
    my ($fh) = $self-&gt;{' INFILE'};
    my ($boff) = $self-&gt;{' OFFSET'};
    my ($dat, $goff, $loff, $aoff, $moff, $r, $s, $bloc);

    $self-&gt;SUPER::read or return $self;
    $bloc = $fh-&gt;tell();
    if ($new_gdef)
    {
        $fh-&gt;read($dat, 12);
        ($self-&gt;{'Version'}, $goff, $aoff, $loff, $moff) = TTF_Unpack('fS4', $dat);
    }
    else
    {
        $fh-&gt;read($dat, 10);
        ($self-&gt;{'Version'}, $goff, $aoff, $loff) = TTF_Unpack('fS3', $dat);
    }

    if ($goff &gt; 0)
    {
        $fh-&gt;seek($goff + $boff, 0);
        $self-&gt;{'GLYPH'} = Font::TTF::Coverage-&gt;new(0)-&gt;read($fh);
    }

    if ($new_gdef &amp;&amp; $moff &gt; 0 &amp;&amp; $moff &lt; $self-&gt;{' LENGTH'})
    {
        $fh-&gt;seek($moff + $boff, 0);
        $self-&gt;{'MARKS'} = Font::TTF::Coverage-&gt;new(0)-&gt;read($fh);
    }

    if ($aoff &gt; 0)
    {
        my ($off, $gcount, $pcount);
        
        $fh-&gt;seek($aoff + $boff, 0);
        $fh-&gt;read($dat, 4);
        ($off, $gcount) = TTF_Unpack('SS', $dat);
        $fh-&gt;read($dat, $gcount &lt;&lt; 1);

        $fh-&gt;seek($aoff + $boff +  $off, 0);
        $self-&gt;{'ATTACH'}{'COVERAGE'} = Font::TTF::Coverage-&gt;new(1)-&gt;read($fh);

        foreach $r (TTF_Unpack('S*', $dat))
        {
            unless ($r)
            {
                push (@{$self-&gt;{'ATTACH'}{'POINTS'}}, []);
                next;
            }
            $fh-&gt;seek($aoff + $boff + $r, 0);
            $fh-&gt;read($dat, 2);
            $pcount = TTF_Unpack('S', $dat);
            $fh-&gt;read($dat, $pcount &lt;&lt; 1);
            push (@{$self-&gt;{'ATTACH'}{'POINTS'}}, [TTF_Unpack('S*', $dat)]);
        }
    }

    if ($loff &gt; 0)
    {
        my ($lcount, $off, $ccount, $srec, $comp);

        $fh-&gt;seek($loff + $boff, 0);
        $fh-&gt;read($dat, 4);
        ($off, $lcount) = TTF_Unpack('SS', $dat);
        $fh-&gt;read($dat, $lcount &lt;&lt; 1);

        $fh-&gt;seek($off + $loff + $boff, 0);
        $self-&gt;{'LIG'}{'COVERAGE'} = Font::TTF::Coverage-&gt;new(1)-&gt;read($fh);

        foreach $r (TTF_Unpack('S*', $dat))
        {
            $fh-&gt;seek($r + $loff + $boff, 0);
            $fh-&gt;read($dat, 2);
            $ccount = TTF_Unpack('S', $dat);
            $fh-&gt;read($dat, $ccount &lt;&lt; 1);

            $srec = [];
            foreach $s (TTF_Unpack('S*', $dat))
            {
                $comp = {};
                $fh-&gt;seek($s + $r + $loff + $boff, 0);
                $fh-&gt;read($dat, 4);
                ($comp-&gt;{'FMT'}, $comp-&gt;{'VAL'}) = TTF_Unpack('S*', $dat);
                if ($comp-&gt;{'FMT'} == 3)
                {
                    $fh-&gt;read($dat, 2);
                    $off = TTF_Unpack('S', $dat);
                    if (defined $self-&gt;{' CACHE'}{$off + $s + $r})
                    { $comp-&gt;{'DEVICE'} = $self-&gt;{' CACHE'}{$off + $s + $r}; }
                    else
                    {
                        $fh-&gt;seek($off + $s + $r + $loff + $boff, 0);
                        $comp-&gt;{'DEVICE'} = Font::TTF::Delta-&gt;new-&gt;read($fh);
                        $self-&gt;{' CACHE'}{$off + $s + $r} = $comp-&gt;{'DEVICE'};
                    }
                }
                push (@$srec, $comp);
            }
            push (@{$self-&gt;{'LIG'}{'LIGS'}}, $srec);
        }
    }

    $self;
}


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

Writes out this table.

=cut

sub out
{
    my ($self, $fh) = @_;
    my ($goff, $aoff, $loff, $moff, @offs, $loc1, $coff, $loc);

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

    $loc = $fh-&gt;tell();
    if ($new_gdef)
    { $fh-&gt;print(TTF_Pack('fSSSS', $self-&gt;{'Version'}, 0, 0, 0, 0)); }
    else
    { $fh-&gt;print(TTF_Pack('fSSS', $self-&gt;{'Version'}, 0, 0, 0)); }

    if (defined $self-&gt;{'GLYPH'})
    {
        $goff = $fh-&gt;tell() - $loc;
        $self-&gt;{'GLYPH'}-&gt;out($fh);
    }

    if (defined $self-&gt;{'ATTACH'})
    {
        my ($r);
        
        $aoff = $fh-&gt;tell() - $loc;
        $fh-&gt;print(pack('n*', (0) x ($#{$self-&gt;{'ATTACH'}{'POINTS'}} + 3)));
        foreach $r (@{$self-&gt;{'ATTACH'}{'POINTS'}})
        {
            push (@offs, $fh-&gt;tell() - $loc - $aoff);
            $fh-&gt;print(pack('n*', $#{$r} + 1, @$r));
        }
        $coff = $fh-&gt;tell() - $loc - $aoff;
        $self-&gt;{'ATTACH'}{'COVERAGE'}-&gt;out($fh);
        $loc1 = $fh-&gt;tell();
        $fh-&gt;seek($aoff + $loc, 0);
        $fh-&gt;print(pack('n*', $coff, $#offs + 1, @offs));
        $fh-&gt;seek($loc1, 0);
    }

    if (defined $self-&gt;{'LIG'})
    {
        my (@reftables, $ltables, $i, $j, $out, $r, $s);

        $ltables = {};
        $loff = $fh-&gt;tell() - $loc;
        $out = pack('n*',
                        Font::TTF::Ttopen::ref_cache($self-&gt;{'LIG'}{'COVERAGE'}, $ltables, 0),
                        $#{$self-&gt;{'LIG'}{'LIGS'}} + 1,
                        (0) x ($#{$self-&gt;{'LIG'}{'LIGS'}} + 1));
        push (@reftables, [$ltables, 0]);
        $i = 0;
        foreach $r (@{$self-&gt;{'LIG'}{'LIGS'}})
        {
            $ltables = {};
            $loc1 = length($out);
            substr($out, ($i &lt;&lt; 1) + 4, 2) = TTF_Pack('S', $loc1);
            $out .= pack('n*', $#{$r} + 1, (0) x ($#{$r} + 1));
            @offs = (); $j = 0;
            foreach $s (@$r)
            {
                substr($out, ($j &lt;&lt; 1) + 2 + $loc1, 2) =
                        TTF_Pack('S', length($out) - $loc1);
                $out .= TTF_Pack('SS', $s-&gt;{'FMT'}, $s-&gt;{'VAL'});
                $out .= pack('n', Font::TTF::Ttopen::ref_cache($s-&gt;{'DEVICE'},
                        $ltables, length($out))) if ($s-&gt;{'FMT'} == 3);
                $j++;
            }
            push (@reftables, [$ltables, $loc1]);
            $i++;
        }
        Font::TTF::Ttopen::out_final($fh, $out, \@reftables);
    }

    if ($new_gdef &amp;&amp; defined $self-&gt;{'MARKS'})
    {
        $moff = $fh-&gt;tell() - $loc;
        $self-&gt;{'MARKS'}-&gt;out($fh);
    }

    $loc1 = $fh-&gt;tell();
    $fh-&gt;seek($loc + 4, 0);
    if ($new_gdef)
    { $fh-&gt;print(TTF_Pack('S4', $goff, $aoff, $loff, $moff)); }
    else
    { $fh-&gt;print(TTF_Pack('S3', $goff, $aoff, $loff)); }
    $fh-&gt;seek($loc1, 0);
    $self;
}

1;

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