| 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 Jonathan Kew L<Jonathan_Kew@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: OldMort.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::OldMort; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::OldMort - Glyph Metamorphosis table in a font | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | =head1 INSTANCE VARIABLES | ||
| 36 | |||
| 37 | =item version | ||
| 38 | |||
| 39 | table version number (Fixed: currently 1.0) | ||
| 40 | |||
| 41 | =item chains | ||
| 42 | |||
| 43 | list of metamorphosis chains, each of which has its own fields: | ||
| 44 | |||
| 45 | =over | ||
| 46 | |||
| 47 | =item defaultFlags | ||
| 48 | |||
| 49 | chain's default subfeature flags (UInt32) | ||
| 50 | |||
| 51 | =item featureEntries | ||
| 52 | |||
| 53 | list of feature entries, each of which has fields: | ||
| 54 | |||
| 55 | =over | ||
| 56 | |||
| 57 | =item type | ||
| 58 | |||
| 59 | =item setting | ||
| 60 | |||
| 61 | =item enable | ||
| 62 | |||
| 63 | =item disable | ||
| 64 | |||
| 65 | =back | ||
| 66 | |||
| 67 | =item subtables | ||
| 68 | |||
| 69 | list of metamorphosis subtables, each of which has fields: | ||
| 70 | |||
| 71 | =over | ||
| 72 | |||
| 73 | =item type | ||
| 74 | |||
| 75 | subtable type (0: rearrangement; 1: contextual substitution; 2: ligature; | ||
| 76 | 4: non-contextual substitution; 5: insertion) | ||
| 77 | |||
| 78 | =item direction | ||
| 79 | |||
| 80 | processing direction ('LR' or 'RL') | ||
| 81 | |||
| 82 | =item orientation | ||
| 83 | |||
| 84 | applies to text in which orientation ('VH', 'V', or 'H') | ||
| 85 | |||
| 86 | =item subFeatureFlags | ||
| 87 | |||
| 88 | the subfeature flags controlling whether the table is used (UInt32) | ||
| 89 | |||
| 90 | =back | ||
| 91 | |||
| 92 | Further fields depend on the type of subtable: | ||
| 93 | |||
| 94 | =over | ||
| 95 | |||
| 96 | Rearrangement table: | ||
| 97 | |||
| 98 | =over | ||
| 99 | |||
| 100 | =item classes | ||
| 101 | |||
| 102 | array of lists of glyphs | ||
| 103 | |||
| 104 | =item states | ||
| 105 | |||
| 106 | array of arrays of hashes{'nextState', 'flags'} | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | Contextual substitution table: | ||
| 111 | |||
| 112 | =over | ||
| 113 | |||
| 114 | =item classes | ||
| 115 | |||
| 116 | array of lists of glyphs | ||
| 117 | |||
| 118 | =item states | ||
| 119 | |||
| 120 | array of array of hashes{'nextState', 'flags', 'actions'}, where C<actions> | ||
| 121 | is an array of two elements which are offsets to be added to [marked, current] | ||
| 122 | glyph to get index into C<mappings> (or C<undef> if no mapping to be applied) | ||
| 123 | |||
| 124 | =item mappings | ||
| 125 | |||
| 126 | list of glyph codes mapped to through the state table mappings | ||
| 127 | |||
| 128 | =back | ||
| 129 | |||
| 130 | Ligature table: | ||
| 131 | |||
| 132 | Non-contextual substitution table: | ||
| 133 | |||
| 134 | Insertion table: | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | =back | ||
| 139 | |||
| 140 | =head1 METHODS | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | use strict; | ||
| 145 | use vars qw(@ISA); | ||
| 146 | use PDF::API3::Compat::API2::Basic::TTF::Utils; | ||
| 147 | use PDF::API3::Compat::API2::Basic::TTF::AATutils; | ||
| 148 | |||
| 149 | @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Table); | ||
| 150 | |||
| 151 | =head2 $t->read | ||
| 152 | |||
| 153 | Reads the table into memory | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub read | ||
| 158 | { | ||
| 159 | my ($self) = @_; | ||
| 160 | my ($dat, $fh, $numChains); | ||
| 161 | |||
| 162 | $self->SUPER::read or return $self; | ||
| 163 | |||
| 164 | $fh = $self->{' INFILE'}; | ||
| 165 | |||
| 166 | $fh->read($dat, 8); | ||
| 167 | ($self->{'version'}, $numChains) = TTF_Unpack("fL", $dat); | ||
| 168 | |||
| 169 | my $chains = []; | ||
| 170 | foreach (1 .. $numChains) { | ||
| 171 | my $chainStart = $fh->tell(); | ||
| 172 | $fh->read($dat, 12); | ||
| 173 | my ($defaultFlags, $chainLength, $nFeatureEntries, $nSubtables) = TTF_Unpack("LLSS", $dat); | ||
| 174 | my $featureEntries = []; | ||
| 175 | foreach (1 .. $nFeatureEntries) { | ||
| 176 | $fh->read($dat, 12); | ||
| 177 | my ($featureType, $featureSetting, $enableFlags, $disableFlags) = TTF_Unpack("SSLL", $dat); | ||
| 178 | push @$featureEntries, { | ||
| 179 | 'type' => $featureType, | ||
| 180 | 'setting' => $featureSetting, | ||
| 181 | 'enable' => $enableFlags, | ||
| 182 | 'disable' => $disableFlags | ||
| 183 | }; | ||
| 184 | } | ||
| 185 | my $subtables = []; | ||
| 186 | foreach (1 .. $nSubtables) { | ||
| 187 | my $subtableStart = $fh->tell(); | ||
| 188 | $fh->read($dat, 8); | ||
| 189 | my ($length, $coverage, $subFeatureFlags) = TTF_Unpack("SSL", $dat); | ||
| 190 | my $type = $coverage & 0x0007; | ||
| 191 | |||
| 192 | my $subtable = { | ||
| 193 | 'type' => $type, | ||
| 194 | 'direction' => (($coverage & 0x4000) ? 'RL' : 'LR'), | ||
| 195 | 'orientation' => (($coverage & 0x2000) ? 'VH' : ($coverage & 0x8000) ? 'V' : 'H'), | ||
| 196 | 'subFeatureFlags' => $subFeatureFlags | ||
| 197 | }; | ||
| 198 | |||
| 199 | if ($type == 0) { # rearrangement | ||
| 200 | my ($classes, $states) = AAT_read_state_table($fh, 0); | ||
| 201 | $subtable->{'classes'} = $classes; | ||
| 202 | $subtable->{'states'} = $states; | ||
| 203 | } | ||
| 204 | |||
| 205 | elsif ($type == 1) { # contextual | ||
| 206 | my $stateTableStart = $fh->tell(); | ||
| 207 | my ($classes, $states, $entries) = AAT_read_state_table($fh, 2); | ||
| 208 | |||
| 209 | $fh->seek($stateTableStart, IO::File::SEEK_SET); | ||
| 210 | $fh->read($dat, 10); | ||
| 211 | my ($stateSize, $classTable, $stateArray, $entryTable, $mappingTables) = unpack("nnnnn", $dat); | ||
| 212 | my $limits = [$classTable, $stateArray, $entryTable, $mappingTables, $length - 8]; | ||
| 213 | |||
| 214 | foreach (@$entries) { | ||
| 215 | my $actions = $_->{'actions'}; | ||
| 216 | foreach (@$actions) { | ||
| 217 | $_ = $_ ? $_ - ($mappingTables / 2) : undef; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | $subtable->{'classes'} = $classes; | ||
| 222 | $subtable->{'states'} = $states; | ||
| 223 | $subtable->{'mappings'} = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $mappingTables, $limits))]; | ||
| 224 | } | ||
| 225 | |||
| 226 | elsif ($type == 2) { # ligature | ||
| 227 | my $stateTableStart = $fh->tell(); | ||
| 228 | my ($classes, $states, $entries) = AAT_read_state_table($fh, 0); | ||
| 229 | |||
| 230 | $fh->seek($stateTableStart, IO::File::SEEK_SET); | ||
| 231 | $fh->read($dat, 14); | ||
| 232 | my ($stateSize, $classTable, $stateArray, $entryTable, | ||
| 233 | $ligActionTable, $componentTable, $ligatureTable) = unpack("nnnnnnn", $dat); | ||
| 234 | my $limits = [$classTable, $stateArray, $entryTable, $ligActionTable, $componentTable, $ligatureTable, $length - 8]; | ||
| 235 | |||
| 236 | my %actions; | ||
| 237 | my $actionLists; | ||
| 238 | foreach (@$entries) { | ||
| 239 | my $offset = $_->{'flags'} & 0x3fff; | ||
| 240 | $_->{'flags'} &= ~0x3fff; | ||
| 241 | if ($offset != 0) { | ||
| 242 | if (not defined $actions{$offset}) { | ||
| 243 | $fh->seek($stateTableStart + $offset, IO::File::SEEK_SET); | ||
| 244 | my $actionList; | ||
| 245 | while (1) { | ||
| 246 | $fh->read($dat, 4); | ||
| 247 | my $action = unpack("N", $dat); | ||
| 248 | my ($last, $store, $component) = (($action & 0x80000000) != 0, ($action & 0xC0000000) != 0, ($action & 0x3fffffff)); | ||
| 249 | $component -= 0x40000000 if $component > 0x1fffffff; | ||
| 250 | $component -= $componentTable / 2; | ||
| 251 | push @$actionList, { 'store' => $store, 'component' => $component }; | ||
| 252 | last if $last; | ||
| 253 | } | ||
| 254 | push @$actionLists, $actionList; | ||
| 255 | $actions{$offset} = $#$actionLists; | ||
| 256 | } | ||
| 257 | $_->{'actions'} = $actions{$offset}; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | $subtable->{'componentTable'} = $componentTable; | ||
| 262 | my $components = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $componentTable, $limits))]; | ||
| 263 | foreach (@$components) { | ||
| 264 | $_ = ($_ - $ligatureTable) . " +" if $_ >= $ligatureTable; | ||
| 265 | } | ||
| 266 | $subtable->{'components'} = $components; | ||
| 267 | |||
| 268 | $subtable->{'ligatureTable'} = $ligatureTable; | ||
| 269 | $subtable->{'ligatures'} = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $ligatureTable, $limits))]; | ||
| 270 | |||
| 271 | $subtable->{'classes'} = $classes; | ||
| 272 | $subtable->{'states'} = $states; | ||
| 273 | $subtable->{'actionLists'} = $actionLists; | ||
| 274 | } | ||
| 275 | |||
| 276 | elsif ($type == 4) { # non-contextual | ||
| 277 | my ($format, $lookup) = AAT_read_lookup($fh, 2, $length - 8, undef); | ||
| 278 | $subtable->{'format'} = $format; | ||
| 279 | $subtable->{'lookup'} = $lookup; | ||
| 280 | } | ||
| 281 | |||
| 282 | elsif ($type == 5) { # insertion | ||
| 283 | my $stateTableStart = $fh->tell(); | ||
| 284 | my ($classes, $states, $entries) = AAT_read_state_table($fh, 2); | ||
| 285 | |||
| 286 | my %insertListHash; | ||
| 287 | my $insertLists; | ||
| 288 | foreach (@$entries) { | ||
| 289 | my $flags = $_->{'flags'}; | ||
| 290 | my @insertCount = (($flags & 0x03e0) >> 5, ($flags & 0x001f)); | ||
| 291 | my $actions = $_->{'actions'}; | ||
| 292 | foreach (0 .. 1) { | ||
| 293 | if ($insertCount[$_] > 0) { | ||
| 294 | $fh->seek($stateTableStart + $actions->[$_], IO::File::SEEK_SET); | ||
| 295 | $fh->read($dat, $insertCount[$_] * 2); | ||
| 296 | if (not defined $insertListHash{$dat}) { | ||
| 297 | push @$insertLists, [unpack("n*", $dat)]; | ||
| 298 | $insertListHash{$dat} = $#$insertLists; | ||
| 299 | } | ||
| 300 | $actions->[$_] = $insertListHash{$dat}; | ||
| 301 | } | ||
| 302 | else { | ||
| 303 | $actions->[$_] = undef; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | $subtable->{'classes'} = $classes; | ||
| 309 | $subtable->{'states'} = $states; | ||
| 310 | $subtable->{'insertLists'} = $insertLists; | ||
| 311 | } | ||
| 312 | |||
| 313 | else { | ||
| 314 | die "unknown subtable type"; | ||
| 315 | } | ||
| 316 | |||
| 317 | push @$subtables, $subtable; | ||
| 318 | $fh->seek($subtableStart + $length, IO::File::SEEK_SET); | ||
| 319 | } | ||
| 320 | |||
| 321 | push @$chains, { | ||
| 322 | 'defaultFlags' => $defaultFlags, | ||
| 323 | 'featureEntries' => $featureEntries, | ||
| 324 | 'subtables' => $subtables | ||
| 325 | }; | ||
| 326 | $fh->seek($chainStart + $chainLength, IO::File::SEEK_SET); | ||
| 327 | } | ||
| 328 | |||
| 329 | $self->{'chains'} = $chains; | ||
| 330 | |||
| 331 | $self; | ||
| 332 | } | ||
| 333 | |||
| 334 | =head2 $t->out($fh) | ||
| 335 | |||
| 336 | Writes the table to a file either from memory or by copying | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | sub out | ||
| 341 | { | ||
| 342 | my ($self, $fh) = @_; | ||
| 343 | |||
| 344 | return $self->SUPER::out($fh) unless $self->{' read'}; | ||
| 345 | |||
| 346 | my $chains = $self->{'chains'}; | ||
| 347 | $fh->print(TTF_Pack("fL", $self->{'version'}, scalar @$chains)); | ||
| 348 | |||
| 349 | foreach (@$chains) { | ||
| 350 | my $chainStart = $fh->tell(); | ||
| 351 | my ($featureEntries, $subtables) = ($_->{'featureEntries'}, $_->{'subtables'}); | ||
| 352 | $fh->print(TTF_Pack("LLSS", $_->{'defaultFlags'}, 0, scalar @$featureEntries, scalar @$subtables)); # placeholder for length | ||
| 353 | |||
| 354 | foreach (@$featureEntries) { | ||
| 355 | $fh->print(TTF_Pack("SSLL", $_->{'type'}, $_->{'setting'}, $_->{'enable'}, $_->{'disable'})); | ||
| 356 | } | ||
| 357 | |||
| 358 | foreach (@$subtables) { | ||
| 359 | my $subtableStart = $fh->tell(); | ||
| 360 | my $type = $_->{'type'}; | ||
| 361 | my $coverage = $type; | ||
| 362 | $coverage += 0x4000 if $_->{'direction'} eq 'RL'; | ||
| 363 | $coverage += 0x2000 if $_->{'orientation'} eq 'VH'; | ||
| 364 | $coverage += 0x8000 if $_->{'orientation'} eq 'V'; | ||
| 365 | |||
| 366 | $fh->print(TTF_Pack("SSL", 0, $coverage, $_->{'subFeatureFlags'})); # placeholder for length | ||
| 367 | |||
| 368 | if ($type == 0) { # rearrangement | ||
| 369 | AAT_write_state_table($fh, $_->{'classes'}, $_->{'states'}, 0); | ||
| 370 | } | ||
| 371 | |||
| 372 | elsif ($type == 1) { # contextual | ||
| 373 | my $stHeader = $fh->tell(); | ||
| 374 | $fh->print(pack("nnnnn", (0) x 5)); # placeholders for stateSize, classTable, stateArray, entryTable, mappingTables | ||
| 375 | |||
| 376 | my $classTable = $fh->tell() - $stHeader; | ||
| 377 | my $classes = $_->{'classes'}; | ||
| 378 | AAT_write_classes($fh, $classes); | ||
| 379 | |||
| 380 | my $stateArray = $fh->tell() - $stHeader; | ||
| 381 | my $states = $_->{'states'}; | ||
| 382 | my ($stateSize, $entries) = AAT_write_states($fh, $classes, $stateArray, $states, | ||
| 383 | sub { | ||
| 384 | my $actions = $_->{'actions'}; | ||
| 385 | ( $_->{'flags'}, @$actions ) | ||
| 386 | } | ||
| 387 | ); | ||
| 388 | |||
| 389 | my $entryTable = $fh->tell() - $stHeader; | ||
| 390 | my $offset = ($entryTable + 8 * @$entries) / 2; | ||
| 391 | foreach (@$entries) { | ||
| 392 | my ($nextState, $flags, @parts) = split /,/; | ||
| 393 | $fh->print(pack("nnnn", $nextState, $flags, map { $_ eq "" ? 0 : $_ + $offset } @parts)); | ||
| 394 | } | ||
| 395 | |||
| 396 | my $mappingTables = $fh->tell() - $stHeader; | ||
| 397 | my $mappings = $_->{'mappings'}; | ||
| 398 | $fh->print(pack("n*", @$mappings)); | ||
| 399 | |||
| 400 | my $loc = $fh->tell(); | ||
| 401 | $fh->seek($stHeader, IO::File::SEEK_SET); | ||
| 402 | $fh->print(pack("nnnnn", $stateSize, $classTable, $stateArray, $entryTable, $mappingTables)); | ||
| 403 | $fh->seek($loc, IO::File::SEEK_SET); | ||
| 404 | } | ||
| 405 | |||
| 406 | elsif ($type == 2) { # ligature | ||
| 407 | my $stHeader = $fh->tell(); | ||
| 408 | $fh->print(pack("nnnnnnn", (0) x 7)); # placeholders for stateSize, classTable, stateArray, entryTable, actionLists, components, ligatures | ||
| 409 | |||
| 410 | my $classTable = $fh->tell() - $stHeader; | ||
| 411 | my $classes = $_->{'classes'}; | ||
| 412 | AAT_write_classes($fh, $classes); | ||
| 413 | |||
| 414 | my $stateArray = $fh->tell() - $stHeader; | ||
| 415 | my $states = $_->{'states'}; | ||
| 416 | |||
| 417 | my ($stateSize, $entries) = AAT_write_states($fh, $classes, $stateArray, $states, | ||
| 418 | sub { | ||
| 419 | ( $_->{'flags'} & 0xc000, $_->{'actions'} ) | ||
| 420 | } | ||
| 421 | ); | ||
| 422 | |||
| 423 | my $actionLists = $_->{'actionLists'}; | ||
| 424 | my %actionListOffset; | ||
| 425 | my $actionListDataLength = 0; | ||
| 426 | my @actionListEntries; | ||
| 427 | foreach (0 .. $#$entries) { | ||
| 428 | my ($nextState, $flags, $offset) = split(/,/, $entries->[$_]); | ||
| 429 | if ($offset eq "") { | ||
| 430 | $offset = undef; | ||
| 431 | } | ||
| 432 | else { | ||
| 433 | if (defined $actionListOffset{$offset}) { | ||
| 434 | $offset = $actionListOffset{$offset}; | ||
| 435 | } | ||
| 436 | else { | ||
| 437 | $actionListOffset{$offset} = $actionListDataLength; | ||
| 438 | my $list = $actionLists->[$offset]; | ||
| 439 | $actionListDataLength += 4 * @$list; | ||
| 440 | push @actionListEntries, $list; | ||
| 441 | $offset = $actionListOffset{$offset}; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | $entries->[$_] = [ $nextState, $flags, $offset ]; | ||
| 445 | } | ||
| 446 | my $entryTable = $fh->tell() - $stHeader; | ||
| 447 | my $ligActionLists = ($entryTable + @$entries * 4 + 3) & ~3; | ||
| 448 | foreach (@$entries) { | ||
| 449 | $_->[2] += $ligActionLists if defined $_->[2]; | ||
| 450 | $fh->print(pack("nn", $_->[0], $_->[1] + $_->[2])); | ||
| 451 | } | ||
| 452 | $fh->print(pack("C*", (0) x ($ligActionLists - $entryTable - @$entries * 4))); | ||
| 453 | |||
| 454 | die "internal error" if $fh->tell() != $ligActionLists + $stHeader; | ||
| 455 | |||
| 456 | my $componentTable = $fh->tell() - $stHeader + $actionListDataLength; | ||
| 457 | my $actionList; | ||
| 458 | foreach $actionList (@actionListEntries) { | ||
| 459 | foreach (0 .. $#$actionList) { | ||
| 460 | my $action = $actionList->[$_]; | ||
| 461 | my $val = $action->{'component'} + $componentTable / 2; | ||
| 462 | $val += 0x40000000 if $val < 0; | ||
| 463 | $val &= 0x3fffffff; | ||
| 464 | $val |= 0x40000000 if $action->{'store'}; | ||
| 465 | $val |= 0x80000000 if $_ == $#$actionList; | ||
| 466 | $fh->print(pack("N", $val)); | ||
| 467 | } | ||
| 468 | } | ||
| 469 | |||
| 470 | die "internal error" if $fh->tell() != $componentTable + $stHeader; | ||
| 471 | |||
| 472 | my $components = $_->{'components'}; | ||
| 473 | my $ligatureTable = $componentTable + @$components * 2; | ||
| 474 | $fh->print(pack("n*", map { (index($_, '+') >= 0 ? $ligatureTable : 0) + $_ } @$components)); | ||
| 475 | |||
| 476 | my $ligatures = $_->{'ligatures'}; | ||
| 477 | $fh->print(pack("n*", @$ligatures)); | ||
| 478 | |||
| 479 | my $loc = $fh->tell(); | ||
| 480 | $fh->seek($stHeader, IO::File::SEEK_SET); | ||
| 481 | $fh->print(pack("nnnnnnn", $stateSize, $classTable, $stateArray, $entryTable, $ligActionLists, $componentTable, $ligatureTable)); | ||
| 482 | $fh->seek($loc, IO::File::SEEK_SET); | ||
| 483 | } | ||
| 484 | |||
| 485 | elsif ($type == 4) { # non-contextual | ||
| 486 | AAT_write_lookup($fh, $_->{'format'}, $_->{'lookup'}, 2, undef); | ||
| 487 | } | ||
| 488 | |||
| 489 | elsif ($type == 5) { # insertion | ||
| 490 | } | ||
| 491 | |||
| 492 | else { | ||
| 493 | die "unknown subtable type"; | ||
| 494 | } | ||
| 495 | |||
| 496 | my $length = $fh->tell() - $subtableStart; | ||
| 497 | my $padBytes = (4 - ($length & 3)) & 3; | ||
| 498 | $fh->print(pack("C*", (0) x $padBytes)); | ||
| 499 | $length += $padBytes; | ||
| 500 | $fh->seek($subtableStart, IO::File::SEEK_SET); | ||
| 501 | $fh->print(pack("n", $length)); | ||
| 502 | $fh->seek($subtableStart + $length, IO::File::SEEK_SET); | ||
| 503 | } | ||
| 504 | |||
| 505 | my $chainLength = $fh->tell() - $chainStart; | ||
| 506 | $fh->seek($chainStart + 4, IO::File::SEEK_SET); | ||
| 507 | $fh->print(pack("N", $chainLength)); | ||
| 508 | $fh->seek($chainStart + $chainLength, IO::File::SEEK_SET); | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | =head2 $t->print($fh) | ||
| 513 | |||
| 514 | Prints a human-readable representation of the table | ||
| 515 | |||
| 516 | =cut | ||
| 517 | |||
| 518 | sub print | ||
| 519 | { | ||
| 520 | my ($self, $fh) = @_; | ||
| 521 | |||
| 522 | $self->read; | ||
| 523 | my $feat = $self->{' PARENT'}->{'feat'}; | ||
| 524 | $feat->read; | ||
| 525 | my $post = $self->{' PARENT'}->{'post'}; | ||
| 526 | $post->read; | ||
| 527 | |||
| 528 | $fh = 'STDOUT' unless defined $fh; | ||
| 529 | |||
| 530 | $fh->printf("version %f\n", $self->{'version'}); | ||
| 531 | |||
| 532 | my $chains = $self->{'chains'}; | ||
| 533 | foreach (@$chains) { | ||
| 534 | my $defaultFlags = $_->{'defaultFlags'}; | ||
| 535 | $fh->printf("chain: defaultFlags = %08x\n", $defaultFlags); | ||
| 536 | |||
| 537 | my $featureEntries = $_->{'featureEntries'}; | ||
| 538 | foreach (@$featureEntries) { | ||
| 539 | $fh->printf("\tfeature %d, setting %d : enableFlags = %08x, disableFlags = %08x # '%s: %s'\n", | ||
| 540 | $_->{'type'}, $_->{'setting'}, $_->{'enable'}, $_->{'disable'}, | ||
| 541 | $feat->settingName($_->{'type'}, $_->{'setting'})); | ||
| 542 | } | ||
| 543 | |||
| 544 | my $subtables = $_->{'subtables'}; | ||
| 545 | foreach (@$subtables) { | ||
| 546 | my $type = $_->{'type'}; | ||
| 547 | my $subFeatureFlags = $_->{'subFeatureFlags'}; | ||
| 548 | $fh->printf("\n\t%s table, %s, %s, subFeatureFlags = %08x # %s (%s)\n", | ||
| 549 | subtable_type_($type), $_->{'direction'}, $_->{'orientation'}, $subFeatureFlags, | ||
| 550 | "Default " . ((($subFeatureFlags & $defaultFlags) != 0) ? "On" : "Off"), | ||
| 551 | join(", ", | ||
| 552 | map { | ||
| 553 | join(": ", $feat->settingName($_->{'type'}, $_->{'setting'}) ) | ||
| 554 | } grep { ($_->{'enable'} & $subFeatureFlags) != 0 } @$featureEntries | ||
| 555 | ) ); | ||
| 556 | |||
| 557 | if ($type == 0) { # rearrangement | ||
| 558 | print_classes_($fh, $_, $post); | ||
| 559 | |||
| 560 | $fh->print("\n"); | ||
| 561 | my $states = $_->{'states'}; | ||
| 562 | my @verbs = ( "0", "Ax->xA", "xD->Dx", "AxD->DxA", | ||
| 563 | "ABx->xAB", "ABx->xBA", "xCD->CDx", "xCD->DCx", | ||
| 564 | "AxCD->CDxA", "AxCD->DCxA", "ABxD->DxAB", "ABxD->DxBA", | ||
| 565 | "ABxCD->CDxAB", "ABxCD->CDxBA", "ABxCD->DCxAB", "ABxCD->DCxBA"); | ||
| 566 | foreach (0 .. $#$states) { | ||
| 567 | $fh->printf("\t\tState %d:", $_); | ||
| 568 | my $state = $states->[$_]; | ||
| 569 | foreach (@$state) { | ||
| 570 | my $flags; | ||
| 571 | $flags .= "!" if ($_->{'flags'} & 0x4000); | ||
| 572 | $flags .= "<" if ($_->{'flags'} & 0x8000); | ||
| 573 | $flags .= ">" if ($_->{'flags'} & 0x2000); | ||
| 574 | $fh->printf("\t(%s%d,%s)", $flags, $_->{'nextState'}, $verbs[($_->{'flags'} & 0x000f)]); | ||
| 575 | } | ||
| 576 | $fh->print("\n"); | ||
| 577 | } | ||
| 578 | } | ||
| 579 | |||
| 580 | elsif ($type == 1) { # contextual | ||
| 581 | print_classes_($fh, $_, $post); | ||
| 582 | |||
| 583 | $fh->print("\n"); | ||
| 584 | my $states = $_->{'states'}; | ||
| 585 | foreach (0 .. $#$states) { | ||
| 586 | $fh->printf("\t\tState %d:", $_); | ||
| 587 | my $state = $states->[$_]; | ||
| 588 | foreach (@$state) { | ||
| 589 | my $flags; | ||
| 590 | $flags .= "!" if ($_->{'flags'} & 0x4000); | ||
| 591 | $flags .= "*" if ($_->{'flags'} & 0x8000); | ||
| 592 | my $actions = $_->{'actions'}; | ||
| 593 | $fh->printf("\t(%s%d,%s,%s)", $flags, $_->{'nextState'}, map { defined $_ ? $_ : "=" } @$actions); | ||
| 594 | } | ||
| 595 | $fh->print("\n"); | ||
| 596 | } | ||
| 597 | |||
| 598 | $fh->print("\n"); | ||
| 599 | my $mappings = $_->{'mappings'}; | ||
| 600 | foreach (0 .. $#$mappings) { | ||
| 601 | $fh->printf("\t\tMapping %d: %d [%s]\n", $_, $mappings->[$_], $post->{'VAL'}[$mappings->[$_]]); | ||
| 602 | } | ||
| 603 | } | ||
| 604 | |||
| 605 | elsif ($type == 2) { # ligature | ||
| 606 | print_classes_($fh, $_, $post); | ||
| 607 | |||
| 608 | $fh->print("\n"); | ||
| 609 | my $states = $_->{'states'}; | ||
| 610 | foreach (0 .. $#$states) { | ||
| 611 | $fh->printf("\t\tState %d:", $_); | ||
| 612 | my $state = $states->[$_]; | ||
| 613 | foreach (@$state) { | ||
| 614 | my $flags; | ||
| 615 | $flags .= "!" if ($_->{'flags'} & 0x4000); | ||
| 616 | $flags .= "*" if ($_->{'flags'} & 0x8000); | ||
| 617 | $fh->printf("\t(%s%d,%s)", $flags, $_->{'nextState'}, defined $_->{'actions'} ? $_->{'actions'} : "="); | ||
| 618 | } | ||
| 619 | $fh->print("\n"); | ||
| 620 | } | ||
| 621 | |||
| 622 | $fh->print("\n"); | ||
| 623 | my $actionLists = $_->{'actionLists'}; | ||
| 624 | foreach (0 .. $#$actionLists) { | ||
| 625 | $fh->printf("\t\tList %d:\t", $_); | ||
| 626 | my $actionList = $actionLists->[$_]; | ||
| 627 | $fh->printf("%s\n", join(", ", map { ($_->{'component'} . ($_->{'store'} ? "*" : "") ) } @$actionList)); | ||
| 628 | } | ||
| 629 | |||
| 630 | my $ligatureTable = $_->{'ligatureTable'}; | ||
| 631 | |||
| 632 | $fh->print("\n"); | ||
| 633 | my $components = $_->{'components'}; | ||
| 634 | foreach (0 .. $#$components) { | ||
| 635 | $fh->printf("\t\tComponent %d: %s\n", $_, $components->[$_]); | ||
| 636 | } | ||
| 637 | |||
| 638 | $fh->print("\n"); | ||
| 639 | my $ligatures = $_->{'ligatures'}; | ||
| 640 | foreach (0 .. $#$ligatures) { | ||
| 641 | $fh->printf("\t\tLigature %d: %d [%s]\n", $_, $ligatures->[$_], $post->{'VAL'}[$ligatures->[$_]]); | ||
| 642 | } | ||
| 643 | } | ||
| 644 | |||
| 645 | elsif ($type == 4) { # non-contextual | ||
| 646 | my $lookup = $_->{'lookup'}; | ||
| 647 | $fh->printf("\t\tLookup format %d\n", $_->{'format'}); | ||
| 648 | if (defined $lookup) { | ||
| 649 | foreach (sort { $a <=> $b } keys %$lookup) { | ||
| 650 | $fh->printf("\t\t\t%d [%s] -> %d [%s])\n", $_, $post->{'VAL'}[$_], $lookup->{$_}, $post->{'VAL'}[$lookup->{$_}]); | ||
| 651 | } | ||
| 652 | } | ||
| 653 | } | ||
| 654 | |||
| 655 | elsif ($type == 5) { # insertion | ||
| 656 | print_classes_($fh, $_, $post); | ||
| 657 | |||
| 658 | $fh->print("\n"); | ||
| 659 | my $states = $_->{'states'}; | ||
| 660 | foreach (0 .. $#$states) { | ||
| 661 | $fh->printf("\t\tState %d:", $_); | ||
| 662 | my $state = $states->[$_]; | ||
| 663 | foreach (@$state) { | ||
| 664 | my $flags; | ||
| 665 | $flags .= "!" if ($_->{'flags'} & 0x4000); | ||
| 666 | $flags .= "*" if ($_->{'flags'} & 0x8000); | ||
| 667 | my $actions = $_->{'actions'}; | ||
| 668 | $fh->printf("\t(%s%d,%s,%s)", $flags, $_->{'nextState'}, map { defined $_ ? $_ : "=" } @$actions); | ||
| 669 | } | ||
| 670 | $fh->print("\n"); | ||
| 671 | } | ||
| 672 | |||
| 673 | $fh->print("\n"); | ||
| 674 | my $insertLists = $_->{'insertLists'}; | ||
| 675 | foreach (0 .. $#$insertLists) { | ||
| 676 | my $insertList = $insertLists->[$_]; | ||
| 677 | $fh->printf("\t\tList %d: %s\n", $_, join(", ", map { $_ . " [" . $post->{'VAL'}[$_] . "]" } @$insertList)); | ||
| 678 | } | ||
| 679 | } | ||
| 680 | |||
| 681 | else { | ||
| 682 | # unknown | ||
| 683 | } | ||
| 684 | } | ||
| 685 | } | ||
| 686 | } | ||
| 687 | |||
| 688 | sub print_classes_ | ||
| 689 | { | ||
| 690 | my ($fh, $subtable, $post) = @_; | ||
| 691 | |||
| 692 | my $classes = $subtable->{'classes'}; | ||
| 693 | foreach (0 .. $#$classes) { | ||
| 694 | my $class = $classes->[$_]; | ||
| 695 | if (defined $class) { | ||
| 696 | $fh->printf("\t\tClass %d:\t%s\n", $_, join(", ", map { $_ . " [" . $post->{'VAL'}[$_] . "]" } @$class)); | ||
| 697 | } | ||
| 698 | } | ||
| 699 | } | ||
| 700 | |||
| 701 | sub subtable_type_ | ||
| 702 | { | ||
| 703 | my ($val) = @_; | ||
| 704 | my ($res); | ||
| 705 | |||
| 706 | my @types = ( | ||
| 707 | 'Rearrangement', | ||
| 708 | 'Contextual', | ||
| 709 | 'Ligature', | ||
| 710 | undef, | ||
| 711 | 'Non-contextual', | ||
| 712 | 'Insertion', | ||
| 713 | ); | ||
| 714 | $res = $types[$val] or ('Undefined (' . $val . ')'); | ||
| 715 | |||
| 716 | $res; | ||
| 717 | } | ||
| 718 | |||
| 719 | 1; | ||
| 720 | |||
| 721 | =head1 BUGS | ||
| 722 | |||
| 723 | None known | ||
| 724 | |||
| 725 | =head1 AUTHOR | ||
| 726 | |||
| 727 | Jonathan Kew L<Jonathan_Kew@sil.org>. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 728 | licensing. | ||
| 729 | |||
| 730 | =cut | ||
| 731 |