4
      #ˆÆ�þDGÝY¶_Jó#$„óÎ„ZÍÅÃË ,—    #package Font::TTF::Table;

=head1 NAME

Font::TTF::Table - Superclass for tables and used for tables we don't have a class for

=head1 DESCRIPTION

Looks after the purely table aspects of a TTF table, such as whether the table
has been read before, locating the file pointer, etc. Also copies tables from
input to output.

=head1 INSTANCE VARIABLES

Instance variables start with a space

=over 4

=item read

Flag which indicates that the table has already been read from file.

=item dat

Allows the creation of unspecific tables. Data is simply output to any font
file being created.

=item nocompress

If set, overrides the font default for WOFF table compression. Is a scalar integer specifying a 
table size threshold below which this table will not be compressed. Set to -1 to never
compress; 0 to always compress.

=item INFILE

The read file handle

=item OFFSET

Location of the file in the input file

=item LENGTH

Length in the input directory

=item ZLENGTH

Compressed length of the table if a WOFF font. 0 < ZLENGTH < LENGTH implies table is compressed.

=item CSUM

Checksum read from the input file's directory

=item PARENT

The L<Font::TTF::Font> that table is part of

=back

=head1 METHODS

=cut

use strict;
use vars qw($VERSION);
use Font::TTF::Utils;
use IO::String;
$VERSION = 0.0001;

my $havezlib = eval {require Compress::Zlib};

=head2 Font::TTF::Table->new(%parms)

Creates a new table or subclass. Table instance variables are passed in
at this point as an associative array.

=cut

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

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

=head2 $t->read

Reads the table from the input file. Acts as a superclass to all true tables.
This method marks the table as read and then just sets the input file pointer
but does not read any data. If the table has already been read, then returns
C<undef> else returns C<$self>

For WOFF-compressed tables, the table is first decompressed and a
replacement file handle is created for reading the decompressed data. In this
case ORIGINALOFFSET will preserve the original value of OFFSET for 
applications that care.

=cut

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

    return $self->read_dat if (ref($self) eq "Font::TTF::Table");
    return undef if $self->{' read'};
    $self->{' INFILE'}->seek($self->{' OFFSET'}, 0);
    if (0 < $self->{' ZLENGTH'} && $self->{' ZLENGTH'} < $self->{' LENGTH'})
    {
        # WOFF table is compressed. Uncompress it to memory and create new fh
        die ("Cannot uncompress WOFF data: Compress::Zlib not present.\n") unless $havezlib;
        $self->{' ORIGINALOFFSET'} = $self->{' OFFSET'};    # Preserve this for those who care
        my $dat;
        $self->{' INFILE'}->read($dat, $self->{' ZLENGTH'}); 
        $dat = Compress::Zlib::uncompress($dat);
        warn "$self->{' NAME'} table decompressed to wrong length" if $self->{' LENGTH'} != bytes::length($dat);
        $self->{' INFILE'} = IO::String->new($dat);
        binmode $self->{' INFILE'};
        $self->{' OFFSET'} = 0;
    }
    $self->{' read'} = 1;
    $self;
}


=head2 $t->read_dat

Reads the table into the C<dat> instance variable for those tables which don't
know any better

=cut

sub read_dat
{
    my ($self) = @_;

# can't just $self->read here otherwise those tables which start their read sub with
# $self->read_dat are going to permanently loop
    return undef if ($self->{' read'});
#    $self->{' read'} = 1;      # Let read do this, now out will call us for subclasses
    $self->{' INFILE'}->seek($self->{' OFFSET'}, 0);
    if (0 < $self->{' ZLENGTH'} && $self->{' ZLENGTH'} < $self->{' LENGTH'})
    {
        # WOFF table is compressed. Uncompress it directly to ' dat'
        die ("Cannot uncompress WOFF data: Compress::Zlib not present.\n") unless $havezlib;
        my $dat;
        $self->{' INFILE'}->read($dat, $self->{' ZLENGTH'}); 
        $dat = Compress::Zlib::uncompress($dat);
     