/usr/share/perl5/Font/TTF/Table.pm is in libfont-ttf-perl 1.04-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | 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
{
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);
warn "$self->{' NAME'} table decompressed to wrong length" if $self->{' LENGTH'} != bytes::length($dat);
$self->{' dat'} = $dat;
}
else
{
$self->{' INFILE'}->read($self->{' dat'}, $self->{' LENGTH'});
}
$self;
}
=head2 $t->out($fh)
Writes out the table to the font file. If there is anything in the
C<dat> instance variable then this is output, otherwise the data is copied
from the input file to the output
=cut
sub out
{
my ($self, $fh) = @_;
my ($dat, $i, $len, $count);
if (defined $self->{' dat'})
{
$fh->print($self->{' dat'});
return $self;
}
return undef unless defined $self->{' INFILE'};
$self->{' INFILE'}->seek($self->{' OFFSET'}, 0);
$len = $self->{' LENGTH'};
while ($len > 0)
{
$count = ($len > 4096) ? 4096 : $len;
$self->{' INFILE'}->read($dat, $count);
$fh->print($dat);
$len -= $count;
}
$self;
}
=head2 $t->out_xml($context)
Outputs this table in XML format. The table is first read (if not already read) and then if
there is no subclass, then the data is dumped as hex data
=cut
sub out_xml
{
my ($self, $context, $depth) = @_;
my ($k);
if (ref($self) eq __PACKAGE__)
{
$self->read_dat;
Font::TTF::Utils::XML_hexdump($context, $depth, $self->{' dat'});
}
else
{
$self->read;
foreach $k (sort grep {$_ !~ m/^\s/o} keys %{$self})
{
$self->XML_element($context, $depth, $k, $self->{$k});
}
}
$self;
}
=head2 $t->XML_element
Output a particular element based on its contents.
=cut
sub XML_element
{
my ($self, $context, $depth, $k, $dat, $ind) = @_;
my ($fh) = $context->{'fh'};
my ($ndepth, $d);
return unless defined $dat;
if (!ref($dat))
{
$fh->printf("%s<%s>%s</%s>\n", $depth, $k, $dat, $k);
return $self;
}
if ($ind)
{ $fh->printf("%s<%s i='%d'>\n", $depth, $k, $ind); }
else
{ $fh->printf("%s<%s>\n", $depth, $k); }
$ndepth = $depth . $context->{'indent'};
if (ref($dat) eq 'SCALAR')
{ $self->XML_element($context, $ndepth, 'scalar', $$dat); }
elsif (ref($dat) eq 'ARRAY')
{
my ($c) = 1;
foreach $d (@{$dat})
{ $self->XML_element($context, $ndepth, 'elem', $d, $c++); }
}
elsif (ref($dat) eq 'HASH')
{
foreach $d (sort grep {$_ !~ m/^\s/o} keys %{$dat})
{ $self->XML_element($context, $ndepth, $d, $dat->{$d}); }
}
else
{
$context->{'name'} = ref($dat);
$context->{'name'} =~ s/^.*://o;
$dat->out_xml($context, $ndepth);
}
$fh->printf("%s</%s>\n", $depth, $k);
$self;
}
=head2 $t->XML_end($context, $tag, %attrs)
Handles the default type of <data> for those tables which aren't subclassed
=cut
sub XML_end
{
my ($self, $context, $tag, %attrs) = @_;
my ($dat, $addr);
return undef unless ($tag eq 'data');
$dat = $context->{'text'};
$dat =~ s/([0-9a-f]{2})\s*/hex($1)/oig;
if (defined $attrs{'addr'})
{ $addr = hex($attrs{'addr'}); }
else
{ $addr = length($self->{' dat'}); }
substr($self->{' dat'}, $addr, length($dat)) = $dat;
return $context;
}
=head2 $t->minsize()
Returns the minimum size this table can be. If it is smaller than this, then the table
must be bad and should be deleted or whatever.
=cut
sub minsize
{
return 0;
}
=head2 $t->dirty($val)
This sets the dirty flag to the given value or 1 if no given value. It returns the
value of the flag
=cut
sub dirty
{
my ($self, $val) = @_;
my ($res) = $self->{' isDirty'};
$self->{' isDirty'} = defined $val ? $val : 1;
$res;
}
=head2 $t->update
Each table knows how to update itself. This consists of doing whatever work
is required to ensure that the memory version of the table is consistent
and that other parameters in other tables have been updated accordingly.
I.e. by the end of sending C<update> to all the tables, the memory version
of the font should be entirely consistent.
Some tables which do no work indicate to themselves the need to update
themselves by setting isDirty above 1. This method resets that accordingly.
=cut
sub update
{
my ($self) = @_;
if ($self->{' isDirty'})
{
$self->read;
$self->{' isDirty'} = 0;
return $self;
}
else
{ return undef; }
}
=head2 $t->empty
Clears a table of all data to the level of not having been read
=cut
sub empty
{
my ($self) = @_;
my (%keep);
foreach (qw(INFILE LENGTH OFFSET CSUM PARENT))
{ $keep{" $_"} = 1; }
map {delete $self->{$_} unless $keep{$_}} keys %$self;
$self;
}
=head2 $t->release
Releases ALL of the memory used by this table, and all of its component/child
objects. This method is called automatically by
'Font::TTF::Font-E<gt>release' (so you don't have to call it yourself).
B<NOTE>, that it is important that this method get called at some point prior
to the actual destruction of the object. Internally, we track things in a
structure that can result in circular references, and without calling
'C<release()>' these will not properly get cleaned up by Perl. Once this
method has been called, though, don't expect to be able to do anything with the
C<Font::TTF::Table> object; it'll have B<no> internal state whatsoever.
B<Developer note:> As part of the brute-force cleanup done here, this method
will throw a warning message whenever unexpected key values are found within
the C<Font::TTF::Table> object. This is done to help ensure that any
unexpected and unfreed values are brought to your attention so that you can bug
us to keep the module updated properly; otherwise the potential for memory
leaks due to dangling circular references will exist.
=cut
sub release
{
my ($self) = @_;
# delete stuff that we know we can, here
my @tofree = map { delete $self->{$_} } keys %{$self};
while (my $item = shift @tofree)
{
my $ref = ref($item);
if (UNIVERSAL::can($item, 'release'))
{ $item->release(); }
elsif ($ref eq 'ARRAY')
{ push( @tofree, @{$item} ); }
elsif (UNIVERSAL::isa($ref, 'HASH'))
{ release($item); }
}
# check that everything has gone - it better had!
foreach my $key (keys %{$self})
{ warn ref($self) . " still has '$key' key left after release.\n"; }
}
sub __dumpvar__
{
my ($self, $key) = @_;
return ($key eq ' PARENT' ? '...parent...' : $self->{$key});
}
1;
=head1 BUGS
No known bugs
=head1 AUTHOR
Martin Hosken L<Martin_Hosken@sil.org>.
=head1 LICENSING
Copyright (c) 1998-2013, SIL International (http://www.sil.org)
This module is released under the terms of the Artistic License 2.0.
For details, see the full text of the license in the file LICENSE.
=cut
|