« API3/Compat/API2/Resource/ColorSpace/Indexed/Hue.pm
↑ Summary
API3/Compat/API2/Basic/TTF/Mort/Insertion.pm »
| 1 | #======================================================================= | ||
| 2 | # ____ ____ _____ _ ____ ___ ____ | ||
| 3 | # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \ | ||
| 4 | # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) | | ||
| 5 | # | __/| |_| | _| _ _ / ___ \| __/| | / __/ | ||
| 6 | # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____| | ||
| 7 | # | ||
| 8 | # A Perl Module Chain to faciliate the Creation and Modification | ||
| 9 | # of High-Quality "Portable Document Format (PDF)" Files. | ||
| 10 | # | ||
| 11 | #======================================================================= | ||
| 12 | # | ||
| 13 | # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW: | ||
| 14 | # | ||
| 15 | # | ||
| 16 | # Copyright Martin Hosken <Martin_Hosken@sil.org> | ||
| 17 | # | ||
| 18 | # No warranty or expression of effectiveness, least of all regarding | ||
| 19 | # anyone's safety, is implied in this software or documentation. | ||
| 20 | # | ||
| 21 | # This specific module is licensed under the Perl Artistic License. | ||
| 22 | # | ||
| 23 | # | ||
| 24 | # $Id: Table.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::Table; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::Table - Superclass for tables and used for tables we don't have a class for | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Looks after the purely table aspects of a TTF table, such as whether the table | ||
| 36 | has been read before, locating the file pointer, etc. Also copies tables from | ||
| 37 | input to output. | ||
| 38 | |||
| 39 | =head1 INSTANCE VARIABLES | ||
| 40 | |||
| 41 | Instance variables start with a space | ||
| 42 | |||
| 43 | =over 4 | ||
| 44 | |||
| 45 | =item read | ||
| 46 | |||
| 47 | Flag which indicates that the table has already been read from file. | ||
| 48 | |||
| 49 | =item dat | ||
| 50 | |||
| 51 | Allows the creation of unspecific tables. Data is simply output to any font | ||
| 52 | file being created. | ||
| 53 | |||
| 54 | =item INFILE | ||
| 55 | |||
| 56 | The read file handle | ||
| 57 | |||
| 58 | =item OFFSET | ||
| 59 | |||
| 60 | Location of the file in the input file | ||
| 61 | |||
| 62 | =item LENGTH | ||
| 63 | |||
| 64 | Length in the input directory | ||
| 65 | |||
| 66 | =item CSUM | ||
| 67 | |||
| 68 | Checksum read from the input file's directory | ||
| 69 | |||
| 70 | =item PARENT | ||
| 71 | |||
| 72 | The L<PDF::API3::Compat::API2::Basic::TTF::Font> that table is part of | ||
| 73 | |||
| 74 | =back | ||
| 75 | |||
| 76 | =head1 METHODS | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | use strict; | ||
| 81 | use vars qw($VERSION); | ||
| 82 | use PDF::API3::Compat::API2::Basic::TTF::Utils; | ||
| 83 | |||
| 84 | $VERSION = 0.0001; | ||
| 85 | |||
| 86 | =head2 PDF::API3::Compat::API2::Basic::TTF::Table->new(%parms) | ||
| 87 | |||
| 88 | Creates a new table or subclass. Table instance variables are passed in | ||
| 89 | at this point as an associative array. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub new | ||
| 94 | { | ||
| 95 | my ($class, %parms) = @_; | ||
| 96 | my ($self) = {}; | ||
| 97 | my ($p); | ||
| 98 | |||
| 99 | $class = ref($class) || $class; | ||
| 100 | foreach $p (keys %parms) | ||
| 101 | { $self->{" $p"} = $parms{$p}; } | ||
| 102 | bless $self, $class; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | =head2 $t->read | ||
| 107 | |||
| 108 | Reads the table from the input file. Acts as a superclass to all true tables. | ||
| 109 | This method marks the table as read and then just sets the input file pointer | ||
| 110 | but does not read any data. If the table has already been read, then returns | ||
| 111 | C<undef> else returns C<$self> | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub read | ||
| 116 | { | ||
| 117 | my ($self) = @_; | ||
| 118 | |||
| 119 | return $self->read_dat if (ref($self) eq qq/__PACKAGE__/); | ||
| 120 | return undef if $self->{' read'}; | ||
| 121 | $self->{' INFILE'}->seek($self->{' OFFSET'}, 0); | ||
| 122 | $self->{' read'} = 1; | ||
| 123 | $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | =head2 $t->read_dat | ||
| 128 | |||
| 129 | Reads the table into the C<dat> instance variable for those tables which don't | ||
| 130 | know any better | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | sub read_dat | ||
| 135 | { | ||
| 136 | my ($self) = @_; | ||
| 137 | |||
| 138 | # can't just $self->read here otherwise those tables which start their read sub with | ||
| 139 | # $self->read_dat are going to permanently loop | ||
| 140 | return undef if ($self->{' read'}); | ||
| 141 | # $self->{' read'} = 1; # Let read do this, now out will call us for subclasses | ||
| 142 | $self->{' INFILE'}->seek($self->{' OFFSET'}, 0); | ||
| 143 | $self->{' INFILE'}->read($self->{' dat'}, $self->{' LENGTH'}); | ||
| 144 | $self; | ||
| 145 | } | ||
| 146 | |||
| 147 | =head2 $t->out($fh) | ||
| 148 | |||
| 149 | Writes out the table to the font file. If there is anything in the | ||
| 150 | C<data> instance variable then this is output, otherwise the data is copied | ||
| 151 | from the input file to the output | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub out | ||
| 156 | { | ||
| 157 | my ($self, $fh) = @_; | ||
| 158 | my ($dat, $i, $len, $count); | ||
| 159 | |||
| 160 | if (defined $self->{' dat'}) | ||
| 161 | { | ||
| 162 | $fh->print($self->{' dat'}); | ||
| 163 | return $self; | ||
| 164 | } | ||
| 165 | |||
| 166 | return undef unless defined $self->{' INFILE'}; | ||
| 167 | $self->{' INFILE'}->seek($self->{' OFFSET'}, 0); | ||
| 168 | $len = $self->{' LENGTH'}; | ||
| 169 | while ($len > 0) | ||
| 170 | { | ||
| 171 | $count = ($len > 4096) ? 4096 : $len; | ||
| 172 | $self->{' INFILE'}->read($dat, $count); | ||
| 173 | $fh->print($dat); | ||
| 174 | $len -= $count; | ||
| 175 | } | ||
| 176 | $self; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | =head2 $t->out_xml($context) | ||
| 181 | |||
| 182 | Outputs this table in XML format. The table is first read (if not already read) and then if | ||
| 183 | there is no subclass, then the data is dumped as hex data | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | sub out_xml | ||
| 188 | { | ||
| 189 | my ($self, $context, $depth) = @_; | ||
| 190 | my ($k); | ||
| 191 | |||
| 192 | if (ref($self) eq __PACKAGE__) | ||
| 193 | { | ||
| 194 | $self->read_dat; | ||
| 195 | PDF::API3::Compat::API2::Basic::TTF::Utils::XML_hexdump($context, $depth, $self->{' dat'}); | ||
| 196 | } | ||
| 197 | else | ||
| 198 | { | ||
| 199 | $self->read; | ||
| 200 | foreach $k (sort grep {$_ !~ m/^\s/o} keys %{$self}) | ||
| 201 | { | ||
| 202 | $self->XML_element($context, $depth, $k, $self->{$k}); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | $self; | ||
| 206 | } | ||
| 207 | |||
| 208 | |||
| 209 | =head2 $t->XML_element | ||
| 210 | |||
| 211 | Output a particular element based on its contents. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub XML_element | ||
| 216 | { | ||
| 217 | my ($self, $context, $depth, $k, $dat) = @_; | ||
| 218 | my ($fh) = $context->{'fh'}; | ||
| 219 | my ($ndepth, $d); | ||
| 220 | |||
| 221 | return unless defined $dat; | ||
| 222 | |||
| 223 | if (!ref($dat)) | ||
| 224 | { | ||
| 225 | $fh->printf("%s<%s>%s</%s>\n", $depth, $k, $dat, $k); | ||
| 226 | return $self; | ||
| 227 | } | ||
| 228 | |||
| 229 | $fh->printf("%s<%s>\n", $depth, $k); | ||
| 230 | $ndepth = $depth . $context->{'indent'}; | ||
| 231 | |||
| 232 | if (ref($dat) eq 'SCALAR') | ||
| 233 | { $self->XML_element($context, $ndepth, 'scalar', $$dat); } | ||
| 234 | elsif (ref($dat) eq 'ARRAY') | ||
| 235 | { | ||
| 236 | foreach $d (@{$dat}) | ||
| 237 | { $self->XML_element($context, $ndepth, 'elem', $d); } | ||
| 238 | } | ||
| 239 | elsif (ref($dat) eq 'HASH') | ||
| 240 | { | ||
| 241 | foreach $d (sort grep {$_ !~ m/^\s/o} keys %{$dat}) | ||
| 242 | { $self->XML_element($context, $ndepth, $d, $dat->{$d}); } | ||
| 243 | } | ||
| 244 | else | ||
| 245 | { | ||
| 246 | $context->{'name'} = ref($dat); | ||
| 247 | $context->{'name'} =~ s/^.*://o; | ||
| 248 | $dat->out_xml($context, $ndepth); | ||
| 249 | } | ||
| 250 | |||
| 251 | $fh->printf("%s</%s>\n", $depth, $k); | ||
| 252 | $self; | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | =head2 $t->XML_end($context, $tag, %attrs) | ||
| 257 | |||
| 258 | Handles the default type of <data> for those tables which aren't subclassed | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | sub XML_end | ||
| 263 | { | ||
| 264 | my ($self, $context, $tag, %attrs) = @_; | ||
| 265 | my ($dat, $addr); | ||
| 266 | |||
| 267 | return undef unless ($tag eq 'data'); | ||
| 268 | $dat = $context->{'text'}; | ||
| 269 | $dat =~ s/([0-9a-f]{2})\s*/hex($1)/oig; | ||
| 270 | if (defined $attrs{'addr'}) | ||
| 271 | { $addr = hex($attrs{'addr'}); } | ||
| 272 | else | ||
| 273 | { $addr = length($self->{' dat'}); } | ||
| 274 | substr($self->{' dat'}, $addr, length($dat)) = $dat; | ||
| 275 | return $context; | ||
| 276 | } | ||
| 277 | |||
| 278 | |||
| 279 | =head2 $t->dirty($val) | ||
| 280 | |||
| 281 | This sets the dirty flag to the given value or 1 if no given value. It returns the | ||
| 282 | value of the flag | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | sub dirty | ||
| 287 | { | ||
| 288 | my ($self, $val) = @_; | ||
| 289 | my ($res) = $self->{' isDirty'}; | ||
| 290 | |||
| 291 | $self->{' isDirty'} = defined $val ? $val : 1; | ||
| 292 | $res; | ||
| 293 | } | ||
| 294 | |||
| 295 | =head2 $t->update | ||
| 296 | |||
| 297 | Each table knows how to update itself. This consists of doing whatever work | ||
| 298 | is required to ensure that the memory version of the table is consistent | ||
| 299 | and that other parameters in other tables have been updated accordingly. | ||
| 300 | I.e. by the end of sending C<update> to all the tables, the memory version | ||
| 301 | of the font should be entirely consistent. | ||
| 302 | |||
| 303 | Some tables which do no work indicate to themselves the need to update | ||
| 304 | themselves by setting isDirty above 1. This method resets that accordingly. | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | sub update | ||
| 309 | { | ||
| 310 | my ($self) = @_; | ||
| 311 | |||
| 312 | if ($self->{' isDirty'}) | ||
| 313 | { | ||
| 314 | $self->read; | ||
| 315 | $self->{' isDirty'} = 0; | ||
| 316 | return $self; | ||
| 317 | } | ||
| 318 | else | ||
| 319 | { return undef; } | ||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | =head2 $t->empty | ||
| 324 | |||
| 325 | Clears a table of all data to the level of not having been read | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | sub empty | ||
| 330 | { | ||
| 331 | my ($self) = @_; | ||
| 332 | my (%keep); | ||
| 333 | |||
| 334 | foreach (qw(INFILE LENGTH OFFSET CSUM PARENT)) | ||
| 335 | { $keep{" $_"} = 1; } | ||
| 336 | |||
| 337 | map {delete $self->{$_} unless $keep{$_}} keys %$self; | ||
| 338 | $self; | ||
| 339 | } | ||
| 340 | |||
| 341 | |||
| 342 | =head2 $t->release | ||
| 343 | |||
| 344 | Releases ALL of the memory used by this table, and all of its component/child | ||
| 345 | objects. This method is called automatically by | ||
| 346 | 'C<PDF::API3::Compat::API2::Basic::TTF::Font-E<GT>release>' (so you don't have to call it yourself). | ||
| 347 | |||
| 348 | B<NOTE>, that it is important that this method get called at some point prior | ||
| 349 | to the actual destruction of the object. Internally, we track things in a | ||
| 350 | structure that can result in circular references, and without calling | ||
| 351 | 'C<release()>' these will not properly get cleaned up by Perl. Once this | ||
| 352 | method has been called, though, don't expect to be able to do anything with the | ||
| 353 | C<PDF::API3::Compat::API2::Basic::TTF::Table> object; it'll have B<no> internal state whatsoever. | ||
| 354 | |||
| 355 | B<Developer note:> As part of the brute-force cleanup done here, this method | ||
| 356 | will throw a warning message whenever unexpected key values are found within | ||
| 357 | the C<PDF::API3::Compat::API2::Basic::TTF::Table> object. This is done to help ensure that any | ||
| 358 | unexpected and unfreed values are brought to your attention so that you can bug | ||
| 359 | us to keep the module updated properly; otherwise the potential for memory | ||
| 360 | leaks due to dangling circular references will exist. | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | sub release | ||
| 365 | { | ||
| 366 | my ($self) = @_; | ||
| 367 | |||
| 368 | return($self) unless(ref $self); | ||
| 369 | # delete stuff that we know we can, here | ||
| 370 | |||
| 371 | my @tofree = map { delete $self->{$_} } keys %{$self}; | ||
| 372 | |||
| 373 | while (my $item = shift @tofree) | ||
| 374 | { | ||
| 375 | my $ref = ref($item); | ||
| 376 | if (UNIVERSAL::can($item, 'release')) | ||
| 377 | { $item->release(); } | ||
| 378 | elsif ($ref eq 'ARRAY') | ||
| 379 | { push( @tofree, @{$item} ); } | ||
| 380 | elsif (UNIVERSAL::isa($ref, 'HASH')) | ||
| 381 | { release($item); } | ||
| 382 | } | ||
| 383 | |||
| 384 | # check that everything has gone - it better had! | ||
| 385 | foreach my $key (keys %{$self}) | ||
| 386 | { warn ref($self) . " still has '$key' key left after release.\n"; } | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | sub __dumpvar__ | ||
| 391 | { | ||
| 392 | my ($self, $key) = @_; | ||
| 393 | |||
| 394 | return ($key eq ' PARENT' ? '...parent...' : $self->{$key}); | ||
| 395 | } | ||
| 396 | |||
| 397 | 1; | ||
| 398 | |||
| 399 | =head1 BUGS | ||
| 400 | |||
| 401 | No known bugs | ||
| 402 | |||
| 403 | =head1 AUTHOR | ||
| 404 | |||
| 405 | Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 406 | licensing. | ||
| 407 | |||
| 408 | =cut | ||
| 409 |