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

=head1 NAME

Font::TTF::Segarr - Segmented array

=head1 DESCRIPTION

Holds data either directly or indirectly as a series of arrays. This class
looks after the set of arrays and masks the individual sub-arrays, thus saving
a class, we hope.

=head1 INSTANCE VARIABLES

All instance variables do not start with a space.

The segmented array is simply an array of segments

Each segment is a more complex affair:

=over 4

=item START

In terms of the array, the address for the 0th element in this segment.

=item LEN

Number of elements in this segment

=item VAL

The array which contains the elements

=back

=head1 METHODS

=cut

use strict;
use vars qw(@types $VERSION);
$VERSION = 0.0001;

@types = ('', 'C', 'n', '', 'N');

=head2 Font::TTF::Segarr-&gt;new($size)

Creates a new segmented array with a given data size

=cut

sub new
{
    my ($class) = @_;
    my ($self) = [];

    bless $self, (ref($class) || $class);
}


=head2 $s-&gt;fastadd_segment($start, $is_sparse, @dat)

Creates a new segment and adds it to the array assuming no overlap between
the new segment and any others in the array. $is_sparse indicates whether the
passed in array contains C&lt;undef&gt;s or not. If false no checking is done (which
is faster, but riskier). If equal to 2 then 0 is considered undef as well.

Returns the number of segments inserted.

=cut

sub fastadd_segment
{
    my ($self) = shift;
    my ($start) = shift;
    my ($sparse) = shift;
    my ($p, $i, $seg, @seg);


    if ($sparse)
    {
        for ($i = 0; $i &lt;= $#_; $i++)
        {
            if (!defined $seg &amp;&amp; (($sparse != 2 &amp;&amp; defined $_[$i]) || $_[$i] != 0))
            { $seg-&gt;{'START'} = $start + $i; $seg-&gt;{'VAL'} = []; }
            
            if (defined $seg &amp;&amp; (($sparse == 2 &amp;&amp; $_[$i] == 0) || !defined $_[$i]))
            {
                $seg-&gt;{'LEN'} = $start + $i - $seg-&gt;{'START'};
                push(@seg, $seg);
                $seg = undef;
            } elsif (defined $seg)
            { push (@{$seg-&gt;{'VAL'}}, $_[$i]); }
        }
        if (defined $seg)
        {
            push(@seg, $seg);
            $seg-&gt;{'LEN'} = $start + $i - $seg-&gt;{'START'};
        }
    } else
    {
        $seg-&gt;{'START'} = $start;
        $seg-&gt;{'LEN'} = $#_ + 1;
        $seg-&gt;{'VAL'} = [@_];
        @seg = ($seg);
    }

    for ($i = 0; $i &lt;= $#$self; $i++)
    {
        if ($self-&gt;[$i]{'START'} &gt; $start)
        {
            splice(@$self, $i, 0, @seg);
            return wantarray ? @seg : scalar(@seg);
        }
    }
    push(@$self, @seg);
    return wantarray ? @seg : scalar(@seg);
}


=head2 $s-&gt;add_segment($start, $overwrite, @dat)

Creates a new segment and adds it to the array allowing for possible overlaps
between the new segment and the existing ones. In the case of overlaps, elements
from the new segment are deleted unless $overwrite is set in which case the
elements already there are over-written.

This method also checks the data coming in to see if it is sparse (i.e. contains
undef values). Gaps cause new segments to be created or not to over-write existing
values.

=cut

sub add_segment
{
    my ($self) = shift;
    my ($start) = shift;
    my ($over) = shift;
    my ($seg, $i, $s, $offset, $j, $newi);

    return $self-&gt;fastadd_segment($start, $over, @_) if ($#$self &lt; 0);
    $offset = 0;
    for ($i = 0; $i &lt;= $#$self &amp;&amp; $offset &lt;= $#_; $i++)
    {
        $s = $self-&gt;[$i];
        if ($s-&gt;{'START'} &lt;= $start + $offset)              # only &lt; for $offset == 0
        {
            if ($s-&gt;{'START'} + $s-&gt;{'LEN'} &gt; $start + $#_)
            {
                for ($j = $offset; $j &lt;= $#_; $j++)
                {
                    if ($over)
                    { $s-&gt;{'VAL'}[$start - $s-&gt;{'START'} + $j] = $_[$j] if defined $_[$j]; }
                    else
                    { $s-&gt;{'VAL'}[$start - $s-&gt;{'START'} + $j] ||= $_[$j] if defined $_[$j]; }
                }
                $offset = $#_ + 1;
                last;
            } elsif ($s-&gt;{'START'} + $s-&gt;{'LEN'} &gt; $start + $offset)        # is $offset needed here?
            {
                for ($j = $offset; $j &lt; $s-&gt;{'START'} + $s-&gt;{'LEN'} - $start; $j++)
                {
                    if ($over)
                    { $s-&gt;{'VAL'}[$start - $s-&gt;{'START'} + $j] = $_[$j] if defined $_[$j]; }
                    else
                    { $s-&gt;{'VAL'}[$start - $s-&gt;{'START'} + $j] ||= $_[$j] if defined $_[$j]; }
                }
                $offset = $s-&gt;{'START'} + $s-&gt;{'LEN'} - $start;
            }
        } else                                              # new seg please
        {
            if ($s-&gt;{'START'} &gt; $start + $#_ + 1)
            {
                $i += $self-&gt;fastadd_segment($start + $offset, 1, @_[$offset .. $#_]) - 1;
                $offset = $#_ + 1;
            }
            else
            {
                $i += $self-&gt;fastadd_segment($start + $offset, 1, @_[$offset .. $s-&gt;{'START'} - $start]) - 1;
                $offset = $s-&gt;{'START'} - $start + 1;
            }
        }
    }
    if ($offset &lt;= $#_)
    {
        $seg-&gt;{'START'} = $start + $offset;
        $seg-&gt;{'LEN'} = $#_ - $offset + 1;
        $seg-&gt;{'VAL'} = [@_[$offset .. $#_]];
        push (@$self, $seg);
    }
    $self-&gt;tidy;
}


=head2 $s-&gt;tidy

Merges any immediately adjacent segments

=cut

sub tidy
{
    my ($self) = @_;
    my ($i, $sl, $s);

    for ($i = 1; $i &lt;= $#$self; $i++)
    {
        $sl = $self-&gt;[$i - 1];
        $s = $self-&gt;[$i];
        if ($s-&gt;{'START'} == $sl-&gt;{'START'} + $sl-&gt;{'LEN'})
        {
            $sl-&gt;{'LEN'} += $s-&gt;{'LEN'};
            push (@{$sl-&gt;{'VAL'}}, @{$s-&gt;{'VAL'}});
            splice(@$self, $i, 1);
            $i--;
        }
    }
    $self;
}


=head2 $s-&gt;at($addr, [$len])

Looks up the data held at the given address by locating the appropriate segment
etc. If $len &gt; 1 then returns an array of values, spaces being filled with undef.

=cut

sub at
{
    my ($self, $addr, $len) = @_;
    my ($i, $dat, $s, @res, $offset);

    $len = 1 unless defined $len;
    $offset = 0;
    for ($i = 0; $i &lt;= $#$self; $i++)
    {
        $s = $self-&gt;[$i];
        next if ($s-&gt;{'START'} + $s-&gt;{'LEN'} &lt; $addr + $offset);        # only fires on $offset == 0
        if ($s-&gt;{'START'} &gt; $addr + $offset)
        {
            push (@res, (undef) x ($s-&gt;{'START'} &gt; $addr + $len ?
                    $len - $offset : $s-&gt;{'START'} - $addr - $offset));
            $offset = $s-&gt;{'START'} - $addr;
        }
        last if ($s-&gt;{'START'} &gt;= $addr + $len);
        
        if ($s-&gt;{'START'} + $s-&gt;{'LEN'} &gt;= $addr + $len)
        {
            push (@res, @{$s-&gt;{'VAL'}}[$addr + $offset - $s-&gt;{'START'} ..
                    $addr + $len - $s-&gt;{'START'} - 1]);
            $offset = $len;
            last;
        } else
        {
            push (@res, @{$s-&gt;{'VAL'}}[$addr + $offset - $s-&gt;{'START'} .. $s-&gt;{'LEN'} - 1]);
            $offset = $s-&gt;{'START'} + $s-&gt;{'LEN'} - $addr;
        }
    }
    push (@res, (undef) x ($len - $offset)) if ($offset &lt; $len);
    return wantarray ? @res : $res[0];
}


=head2 $s-&gt;remove($addr, [$len])

Removes the item or items from addr returning them as an array or the first
value in a scalar context. This is very like C&lt;at&gt;, including padding with
undef, but it deletes stuff as it goes.

=cut

sub remove
{
    my ($self, $addr, $len) = @_;
    my ($i, $dat, $s, @res, $offset);

    $len = 1 unless defined $len;
    $offset = 0;
    for ($i = 0; $i &lt;= $#$self; $i++)
    {
        $s = $self-&gt;[$i];
        next if ($s-&gt;{'START'} + $s-&gt;{'LEN'} &lt; $addr + $offset);
        if ($s-&gt;{'START'} &gt; $addr + $offset)
        {
            push (@res, (undef) x ($s-&gt;{'START'} &gt; $addr + $len ?
                    $len - $offset : $s-&gt;{'START'} - $addr - $offset));
            $offset = $s-&gt;{'START'} - $addr;
        }
        last if ($s-&gt;{'START'} &gt;= $addr + $len);
        
        unless ($s-&gt;{'START'} == $addr + $offset)
        {
            my ($seg) = {};

            $seg-&gt;{'START'} = $s-&gt;{'START'};
            $seg-&gt;{'LEN'} = $addr + $offset - $s-&gt;{'START'};
            $seg-&gt;{'VAL'} = [splice(@{$s-&gt;{'VAL'}}, 0, $addr + $offset - $s-&gt;{'START'})];
            $s-&gt;{'LEN'} -= $addr + $offset - $s-&gt;{'START'};
            $s-&gt;{'START'} = $addr + $offset;

            splice(@$self, $i, 0, $seg);
            $i++;
        }

        if ($s-&gt;{'START'} + $s-&gt;{'LEN'} &gt;= $addr + $len)
        {
            push (@res, splice(@{$s-&gt;{'VAL'}}, 0, $len - $offset));
            $s-&gt;{'LEN'} -= $len - $offset;
            $s-&gt;{'START'} += $len - $offset;
            $offset = $len;
            last;
        } else
        {
            push (@res, @{$s-&gt;{'VAL'}});
            $offset = $s-&gt;{'START'} + $s-&gt;{'LEN'} - $addr;
            splice(@$self, $i, 0);
            $i--;
        }
    }
    push (@res, (undef) x ($len - $offset)) if ($offset &lt; $len);
    return wantarray ? @res : $res[0];
}
    

=head2 $s-&gt;copy

Deep copies this array

=cut

sub copy
{
    my ($self) = @_;
    my ($res, $p);

    $res = [];
    foreach $p (@$self)
    { push (@$res, $self-&gt;copy_seg($p)); }
    $res;
}
    

=head2 $s-&gt;copy_seg($seg)

Creates a deep copy of a segment

=cut

sub copy_seg
{
    my ($self, $seg) = @_;
    my ($p, $res);

    $res = {};
    $res-&gt;{'VAL'} = [@{$seg-&gt;{'VAL'}}];
    foreach $p (keys %$seg)
    { $res-&gt;{$p} = $seg-&gt;{$p} unless defined $res-&gt;{$p}; }
    $res;
}


1;

=head1 BUGS

No known bugs.

=head1 AUTHOR

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

=cut

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