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

=head1 NAME

Font::TTF::Loca - the Locations table, which is intimately tied to the glyf table

=head1 DESCRIPTION

The location table holds the directory of locations of each glyph within the
glyf table. Due to this relationship and the unimportance of the actual locations
when it comes to holding glyphs in memory, reading the location table results
in the creation of glyph objects for each glyph and stores them here.
So if you are looking for glyphs, don't look in the C&lt;glyf&gt; table, look here
instead.

Things get complicated if you try to change the glyph list within the one table.
The recommendation is to create another clean location object to replace this
table in the font, ensuring that the old table is read first and to transfer
or copy glyphs across from the read table to the new table.

=head1 INSTANCE VARIABLES

The instance variables do not start with a space

=over 4

=item glyphs

An array of glyph objects for each glyph.

=item glyphtype

A string containing the class name to create for each new glyph. If empty,
defaults to L&lt;Font::TTF::Glyph&gt;.

=back

=head1 METHODS

=cut

use strict;
use vars qw(@ISA);
@ISA = qw(Font::TTF::Table);

require Font::TTF::Glyph;


=head2 $t-&gt;new

Creates a new location table making sure it has a glyphs array

=cut

sub new
{
    my ($class) = shift;
    my ($res) = $class-&gt;SUPER::new(@_);
    $res-&gt;{'glyphs'} = [];
    $res;
}

=head2 $t-&gt;read

Reads the location table creating glyph objects (L&lt;Font::TTF::Glyph&gt;) for each glyph
allowing their later reading.

=cut

sub read
{
    my ($self) = @_;
    my ($fh) = $self-&gt;{' INFILE'};
    my ($locFmt) = $self-&gt;{' PARENT'}{'head'}{'indexToLocFormat'};
    my ($numGlyphs) = $self-&gt;{' PARENT'}{'maxp'}{'numGlyphs'};
    my ($glyfLoc) = $self-&gt;{' PARENT'}{'glyf'}{' OFFSET'};
    my ($dat, $last, $i, $loc);

    $self-&gt;SUPER::read or return $self;
    $fh-&gt;read($dat, $locFmt ? 4 : 2);
    $last = unpack($locFmt ? "N" : "n", $dat);
    for ($i = 0; $i &lt; $numGlyphs; $i++)
    {
        $fh-&gt;read($dat, $locFmt ? 4 : 2);
        $loc = unpack($locFmt ? "N" : "n", $dat);
        $self-&gt;{'glyphs'}[$i] = ($self-&gt;{'glyphtype'} || "Font::TTF::Glyph")-&gt;new(
                LOC =&gt; $last &lt;&lt; ($locFmt ? 0 : 1),
                OUTLOC =&gt; $last &lt;&lt; ($locFmt ? 0 : 1),
                PARENT =&gt; $self-&gt;{' PARENT'},
                INFILE =&gt; $fh,
                BASE =&gt; $glyfLoc,
                OUTLEN =&gt; ($loc - $last) &lt;&lt; ($locFmt ? 0 : 1),
                LEN =&gt; ($loc - $last) &lt;&lt; ($locFmt ? 0 : 1)) if ($loc != $last);
        $last = $loc;
    }
    $self;
}


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

Writes the location table out to $fh. Notice that not having read the location
table implies that the glyf table has not been read either, so the numbers in
the location table are still valid. Let's hope that C&lt;maxp/numGlyphs&gt; and
C&lt;head/indexToLocFmt&gt; haven't changed otherwise we are in big trouble.

The function uses the OUTLOC location in the glyph calculated when the glyf
table was attempted to be output.

=cut

sub out
{
    my ($self, $fh) = @_;
    my ($locFmt) = $self-&gt;{' PARENT'}{'head'}{'indexToLocFormat'};
    my ($numGlyphs) = $self-&gt;{' PARENT'}{'maxp'}{'numGlyphs'};
    my ($count, $i, $offset, $g);

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

    $count = 0;
    for ($i = 0; $i &lt; $numGlyphs; $i++)
    {
        $g = ($self-&gt;{'glyphs'}[$i]) || "";
        unless ($g)
        {
            $count++;
            next;
        } else
        {
            if ($locFmt)
            { $fh-&gt;print(pack("N", $g-&gt;{' OUTLOC'}) x ($count + 1)); }
            else
            { $fh-&gt;print(pack("n", $g-&gt;{' OUTLOC'} &gt;&gt; 1) x ($count + 1)); }
            $count = 0;
            $offset = $g-&gt;{' OUTLOC'} + $g-&gt;{' OUTLEN'};
        }
    }
    $fh-&gt;print(pack($locFmt ? "N" : "n", ($locFmt ? $offset: $offset &gt;&gt; 1)) x ($count + 1));
}


=head2 $t-&gt;out_xml($context, $depth)

No need to output a loca table, this is dynamically generated

=cut

sub out_xml
{ return $_[0]; }


=head2 $t-&gt;glyphs_do(&amp;func)

Calls func for each glyph in this location table in numerical order:

    &amp;func($glyph, $glyph_num)

=cut

sub glyphs_do
{
    my ($self, $func) = @_;
    my ($i);

    for ($i = 0; $i &lt;= $#{$self-&gt;{'glyphs'}}; $i++)
    { &amp;$func($self-&gt;{'glyphs'}[$i], $i) if defined $self-&gt;{'glyphs'}[$i]; }
    $self;
}

1;

=head1 BUGS

None known

=head1 AUTHOR

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

=cut

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