| 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: Chain.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::Mort::Chain; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::Mort::Chain | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | use strict; | ||
| 36 | use PDF::API3::Compat::API2::Basic::TTF::Utils; | ||
| 37 | use PDF::API3::Compat::API2::Basic::TTF::AATutils; | ||
| 38 | use PDF::API3::Compat::API2::Basic::TTF::Mort::Subtable; | ||
| 39 | |||
| 40 | =head2 $t->new | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub new | ||
| 45 | { | ||
| 46 | my ($class, %parms) = @_; | ||
| 47 | my ($self) = {}; | ||
| 48 | my ($p); | ||
| 49 | |||
| 50 | $class = ref($class) || $class; | ||
| 51 | foreach $p (keys %parms) | ||
| 52 | { $self->{" $p"} = $parms{$p}; } | ||
| 53 | bless $self, $class; | ||
| 54 | } | ||
| 55 | |||
| 56 | =head2 $t->read($fh) | ||
| 57 | |||
| 58 | Reads the chain into memory | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub read | ||
| 63 | { | ||
| 64 | my ($self, $fh) = @_; | ||
| 65 | my ($dat); | ||
| 66 | |||
| 67 | my $chainStart = $fh->tell(); | ||
| 68 | $fh->read($dat, 12); | ||
| 69 | my ($defaultFlags, $chainLength, $nFeatureEntries, $nSubtables) = TTF_Unpack("LLSS", $dat); | ||
| 70 | |||
| 71 | my $featureEntries = []; | ||
| 72 | foreach (1 .. $nFeatureEntries) { | ||
| 73 | $fh->read($dat, 12); | ||
| 74 | my ($featureType, $featureSetting, $enableFlags, $disableFlags) = TTF_Unpack("SSLL", $dat); | ||
| 75 | push @$featureEntries, { | ||
| 76 | 'type' => $featureType, | ||
| 77 | 'setting' => $featureSetting, | ||
| 78 | 'enable' => $enableFlags, | ||
| 79 | 'disable' => $disableFlags | ||
| 80 | }; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $subtables = []; | ||
| 84 | foreach (1 .. $nSubtables) { | ||
| 85 | my $subtableStart = $fh->tell(); | ||
| 86 | |||
| 87 | $fh->read($dat, 8); | ||
| 88 | my ($length, $coverage, $subFeatureFlags) = TTF_Unpack("SSL", $dat); | ||
| 89 | my $type = $coverage & 0x0007; | ||
| 90 | |||
| 91 | my $subtable = PDF::API3::Compat::API2::Basic::TTF::Mort::Subtable->create($type, $coverage, $subFeatureFlags, $length); | ||
| 92 | $subtable->read($fh); | ||
| 93 | $subtable->{' PARENT'} = $self; | ||
| 94 | |||
| 95 | push @$subtables, $subtable; | ||
| 96 | $fh->seek($subtableStart + $length, IO::File::SEEK_SET); | ||
| 97 | } | ||
| 98 | |||
| 99 | $self->{'defaultFlags'} = $defaultFlags; | ||
| 100 | $self->{'featureEntries'} = $featureEntries; | ||
| 101 | $self->{'subtables'} = $subtables; | ||
| 102 | |||
| 103 | $fh->seek($chainStart + $chainLength, IO::File::SEEK_SET); | ||
| 104 | |||
| 105 | $self; | ||
| 106 | } | ||
| 107 | |||
| 108 | =head2 $t->out($fh) | ||
| 109 | |||
| 110 | Writes the table to a file either from memory or by copying | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub out | ||
| 115 | { | ||
| 116 | my ($self, $fh) = @_; | ||
| 117 | |||
| 118 | my $chainStart = $fh->tell(); | ||
| 119 | my ($featureEntries, $subtables) = ($_->{'featureEntries'}, $_->{'subtables'}); | ||
| 120 | $fh->print(TTF_Pack("LLSS", $_->{'defaultFlags'}, 0, scalar @$featureEntries, scalar @$subtables)); # placeholder for length | ||
| 121 | |||
| 122 | foreach (@$featureEntries) { | ||
| 123 | $fh->print(TTF_Pack("SSLL", $_->{'type'}, $_->{'setting'}, $_->{'enable'}, $_->{'disable'})); | ||
| 124 | } | ||
| 125 | |||
| 126 | foreach (@$subtables) { | ||
| 127 | $_->out($fh); | ||
| 128 | } | ||
| 129 | |||
| 130 | my $chainLength = $fh->tell() - $chainStart; | ||
| 131 | $fh->seek($chainStart + 4, IO::File::SEEK_SET); | ||
| 132 | $fh->print(pack("N", $chainLength)); | ||
| 133 | $fh->seek($chainStart + $chainLength, IO::File::SEEK_SET); | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 $t->print($fh) | ||
| 137 | |||
| 138 | Prints a human-readable representation of the chain | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub feat | ||
| 143 | { | ||
| 144 | my ($self) = @_; | ||
| 145 | |||
| 146 | my $feat = $self->{' PARENT'}{' PARENT'}{'feat'}; | ||
| 147 | if (defined $feat) { | ||
| 148 | $feat->read; | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | $feat = {}; | ||
| 152 | } | ||
| 153 | |||
| 154 | return $feat; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub print | ||
| 158 | { | ||
| 159 | my ($self, $fh) = @_; | ||
| 160 | |||
| 161 | $fh->printf("version %f\n", $self->{'version'}); | ||
| 162 | |||
| 163 | my $defaultFlags = $self->{'defaultFlags'}; | ||
| 164 | $fh->printf("chain: defaultFlags = %08x\n", $defaultFlags); | ||
| 165 | |||
| 166 | my $feat = $self->feat(); | ||
| 167 | my $featureEntries = $self->{'featureEntries'}; | ||
| 168 | foreach (@$featureEntries) { | ||
| 169 | $fh->printf("\tfeature %d, setting %d : enableFlags = %08x, disableFlags = %08x # '%s: %s'\n", | ||
| 170 | $_->{'type'}, $_->{'setting'}, $_->{'enable'}, $_->{'disable'}, | ||
| 171 | $feat->settingName($_->{'type'}, $_->{'setting'})); | ||
| 172 | } | ||
| 173 | |||
| 174 | my $subtables = $self->{'subtables'}; | ||
| 175 | foreach (@$subtables) { | ||
| 176 | my $type = $_->{'type'}; | ||
| 177 | my $subFeatureFlags = $_->{'subFeatureFlags'}; | ||
| 178 | $fh->printf("\n\t%s table, %s, %s, subFeatureFlags = %08x # %s (%s)\n", | ||
| 179 | subtable_type_($type), $_->{'direction'}, $_->{'orientation'}, $subFeatureFlags, | ||
| 180 | "Default " . ((($subFeatureFlags & $defaultFlags) != 0) ? "On" : "Off"), | ||
| 181 | join(", ", | ||
| 182 | map { | ||
| 183 | join(": ", $feat->settingName($_->{'type'}, $_->{'setting'}) ) | ||
| 184 | } grep { ($_->{'enable'} & $subFeatureFlags) != 0 } @$featureEntries | ||
| 185 | ) ); | ||
| 186 | |||
| 187 | $_->print($fh); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | sub subtable_type_ | ||
| 192 | { | ||
| 193 | my ($val) = @_; | ||
| 194 | my ($res); | ||
| 195 | |||
| 196 | my @types = ( | ||
| 197 | 'Rearrangement', | ||
| 198 | 'Contextual', | ||
| 199 | 'Ligature', | ||
| 200 | undef, | ||
| 201 | 'Non-contextual', | ||
| 202 | 'Insertion', | ||
| 203 | ); | ||
| 204 | $res = $types[$val] or ('Undefined (' . $val . ')'); | ||
| 205 | |||
| 206 | $res; | ||
| 207 | } | ||
| 208 | |||
| 209 | 1; | ||
| 210 | |||
| 211 | =head1 BUGS | ||
| 212 | |||
| 213 | None known | ||
| 214 | |||
| 215 | =head1 AUTHOR | ||
| 216 | |||
| 217 | Jonathan Kew L<Jonathan_Kew@sil.org>. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 218 | licensing. | ||
| 219 | |||
| 220 | =cut | ||
| 221 |