<commit_before>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>

=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;
        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<commit_msg>[FTLS-115] Fixed various problems with ttftable and WOFF fonts<commit_after>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
{'fraction_non_alphanumeric': 0.11632970451010886, 'fraction_numerical': 0.006687402799377916, 'mean_word_length': 3.548090523338048, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2058884', 'n_tokens_mistral': 2086, 'n_tokens_neox': 2013, 'n_words': 876}