| 1 | package Font::TTF::Mort::Contextual; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Font::TTF::Mort::Contextual - Contextual Mort subtable for AAT | ||
| 6 | |||
| 7 | =head1 METHODS | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw(@ISA); | ||
| 13 | use Font::TTF::Utils; | ||
| 14 | use Font::TTF::AATutils; | ||
| 15 | use IO::File; | ||
| 16 | |||
| 17 | @ISA = qw(Font::TTF::Mort::Subtable); | ||
| 18 | |||
| 19 | sub new | ||
| 20 | { | ||
| 21 | my ($class, $direction, $orientation, $subFeatureFlags) = @_; | ||
| 22 | my ($self) = { | ||
| 23 | 'direction' => $direction, | ||
| 24 | 'orientation' => $orientation, | ||
| 25 | 'subFeatureFlags' => $subFeatureFlags | ||
| 26 | }; | ||
| 27 | |||
| 28 | $class = ref($class) || $class; | ||
| 29 | bless $self, $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | =head2 $t->read | ||
| 33 | |||
| 34 | Reads the table into memory | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub read | ||
| 39 | { | ||
| 40 | my ($self, $fh) = @_; | ||
| 41 | my ($dat); | ||
| 42 | |||
| 43 | my $stateTableStart = $fh->tell(); | ||
| 44 | my ($classes, $states, $entries) = AAT_read_state_table($fh, 2); | ||
| 45 | |||
| 46 | $fh->seek($stateTableStart, IO::File::SEEK_SET); | ||
| 47 | $fh->read($dat, 10); | ||
| 48 | my ($stateSize, $classTable, $stateArray, $entryTable, $mappingTables) = unpack("nnnnn", $dat); | ||
| 49 | my $limits = [$classTable, $stateArray, $entryTable, $mappingTables, $self->{'length'} - 8]; | ||
| 50 | |||
| 51 | foreach (@$entries) { | ||
| 52 | my $actions = $_->{'actions'}; | ||
| 53 | foreach (@$actions) { | ||
| 54 | $_ = $_ ? $_ - ($mappingTables / 2) : undef; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | $self->{'classes'} = $classes; | ||
| 59 | $self->{'states'} = $states; | ||
| 60 | $self->{'mappings'} = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $mappingTables, $limits))]; | ||
| 61 | |||
| 62 | $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | =head2 $t->pack_sub() | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub pack_sub | ||
| 70 | { | ||
| 71 | my ($self) = @_; | ||
| 72 | |||
| 73 | my ($dat) = pack("nnnnn", (0) x 5); # placeholders for stateSize, classTable, stateArray, entryTable, mappingTables | ||
| 74 | |||
| 75 | my $classTable = length($dat); | ||
| 76 | my $classes = $self->{'classes'}; | ||
| 77 | $dat .= AAT_pack_classes($classes); | ||
| 78 | |||
| 79 | my $stateArray = length($dat); | ||
| 80 | my $states = $self->{'states'}; | ||
| 81 | my ($dat1, $stateSize, $entries) = AAT_pack_states($classes, $stateArray, $states, | ||
| 82 | sub { | ||
| 83 | my $actions = $_->{'actions'}; | ||
| 84 | ( $_->{'flags'}, @$actions ) | ||
| 85 | } | ||
| 86 | ); | ||
| 87 | $dat .= $dat1; | ||
| 88 | |||
| 89 | my $entryTable = length($dat); | ||
| 90 | my $offset = ($entryTable + 8 * @$entries) / 2; | ||
| 91 | foreach (@$entries) { | ||
| 92 | my ($nextState, $flags, @parts) = split /,/; | ||
| 93 | $dat .= pack("nnnn", $nextState, $flags, map { $_ eq "" ? 0 : $_ + $offset } @parts); | ||
| 94 | } | ||
| 95 | |||
| 96 | my $mappingTables = length($dat); | ||
| 97 | my $mappings = $self->{'mappings'}; | ||
| 98 | $dat .= pack("n*", @$mappings); | ||
| 99 | |||
| 100 | $dat1 = pack("nnnnn", $stateSize, $classTable, $stateArray, $entryTable, $mappingTables); | ||
| 101 | substr($dat, 0, length($dat1)) = $dat1; | ||
| 102 | |||
| 103 | return $dat; | ||
| 104 | } | ||
| 105 | |||
| 106 | =head2 $t->print($fh) | ||
| 107 | |||
| 108 | Prints a human-readable representation of the table | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub print | ||
| 113 | { | ||
| 114 | my ($self, $fh) = @_; | ||
| 115 | |||
| 116 | my $post = $self->post(); | ||
| 117 | |||
| 118 | $fh = 'STDOUT' unless defined $fh; | ||
| 119 | |||
| 120 | $self->print_classes($fh); | ||
| 121 | |||
| 122 | $fh->print("\n"); | ||
| 123 | my $states = $self->{'states'}; | ||
| 124 | foreach (0 .. $#$states) { | ||
| 125 | $fh->printf("\t\tState %d:", $_); | ||
| 126 | my $state = $states->[$_]; | ||
| 127 | foreach (@$state) { | ||
| 128 | my $flags; | ||
| 129 | $flags .= "!" if ($_->{'flags'} & 0x4000); | ||
| 130 | $flags .= "*" if ($_->{'flags'} & 0x8000); | ||
| 131 | my $actions = $_->{'actions'}; | ||
| 132 | $fh->printf("\t(%s%d,%s,%s)", $flags, $_->{'nextState'}, map { defined $_ ? $_ : "=" } @$actions); | ||
| 133 | } | ||
| 134 | $fh->print("\n"); | ||
| 135 | } | ||
| 136 | |||
| 137 | $fh->print("\n"); | ||
| 138 | my $mappings = $self->{'mappings'}; | ||
| 139 | foreach (0 .. $#$mappings) { | ||
| 140 | $fh->printf("\t\tMapping %d: %d [%s]\n", $_, $mappings->[$_], $post->{'VAL'}[$mappings->[$_]]); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; | ||
| 145 | |||
| 146 | =head1 BUGS | ||
| 147 | |||
| 148 | None known | ||
| 149 | |||
| 150 | =head1 AUTHOR | ||
| 151 | |||
| 152 | Jonathan Kew L<http://scripts.sil.org/FontUtils>. | ||
| 153 | |||
| 154 | |||
| 155 | =head1 LICENSING | ||
| 156 | |||
| 157 | Copyright (c) 1998-2016, SIL International (http://www.sil.org) | ||
| 158 | |||
| 159 | This module is released under the terms of the Artistic License 2.0. | ||
| 160 | For details, see the full text of the license in the file LICENSE. | ||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 |