API3/Compat/API2/Basic/TTF/Mort/Ligature.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 204 CC 20 SCAR 40.9
CC
20
Cov
0%
pack_sub
110.0
read
42.0
print
42.0
CRAP 59.8
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: Ligature.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $
25 #
26 #=======================================================================
27 package PDF::API3::Compat::API2::Basic::TTF::Mort::Ligature;
28
29 =head1 NAME
30
31 PDF::API3::Compat::API2::Basic::TTF::Mort::Ligature
32
33 =head1 METHODS
34
35 =cut
36
37 use strict;
38 use vars qw(@ISA);
39 use PDF::API3::Compat::API2::Basic::TTF::Utils;
40 use PDF::API3::Compat::API2::Basic::TTF::AATutils;
41
42 @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Mort::Subtable);
43
44 sub new
45 {
46 my ($class, $direction, $orientation, $subFeatureFlags) = @_;
47 my ($self) = {
48 'direction' => $direction,
49 'orientation' => $orientation,
50 'subFeatureFlags' => $subFeatureFlags
51 };
52
53 $class = ref($class) || $class;
54 bless $self, $class;
55 }
56
57 =head2 $t->read
58
59 Reads the table into memory
60
61 =cut
62
63 sub read
64 {
65 my ($self, $fh) = @_;
66 my ($dat);
67
68 my $stateTableStart = $fh->tell();
69 my ($classes, $states, $entries) = AAT_read_state_table($fh, 0);
70
71 $fh->seek($stateTableStart, IO::File::SEEK_SET);
72 $fh->read($dat, 14);
73 my ($stateSize, $classTable, $stateArray, $entryTable,
74 $ligActionTable, $componentTable, $ligatureTable) = unpack("nnnnnnn", $dat);
75 my $limits = [$classTable, $stateArray, $entryTable, $ligActionTable, $componentTable, $ligatureTable, $self->{'length'} - 8];
76
77 my %actions;
78 my $actionLists;
79 foreach (@$entries) {
80 my $offset = $_->{'flags'} & 0x3fff;
81 $_->{'flags'} &= ~0x3fff;
82 if ($offset != 0) {
83 if (not defined $actions{$offset}) {
84 $fh->seek($stateTableStart + $offset, IO::File::SEEK_SET);
85 my $actionList;
86 while (1) {
87 $fh->read($dat, 4);
88 my $action = unpack("N", $dat);
89 my ($last, $store, $component) = (($action & 0x80000000) != 0, ($action & 0xC0000000) != 0, ($action & 0x3fffffff));
90 $component -= 0x40000000 if $component > 0x1fffffff;
91 $component -= $componentTable / 2;
92 push @$actionList, { 'store' => $store, 'component' => $component };
93 last if $last;
94 }
95 push @$actionLists, $actionList;
96 $actions{$offset} = $#$actionLists;
97 }
98 $_->{'actions'} = $actions{$offset};
99 }
100 }
101
102 $self->{'componentTable'} = $componentTable;
103 my $components = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $componentTable, $limits))];
104 foreach (@$components) {
105 $_ = ($_ - $ligatureTable) . " +" if $_ >= $ligatureTable;
106 }
107 $self->{'components'} = $components;
108
109 $self->{'ligatureTable'} = $ligatureTable;
110 $self->{'ligatures'} = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $ligatureTable, $limits))];
111
112 $self->{'classes'} = $classes;
113 $self->{'states'} = $states;
114 $self->{'actionLists'} = $actionLists;
115
116 $self;
117 }
118
119 =head2 $t->pack_sub($fh)
120
121 =cut
122
123 sub pack_sub
124 {
125 my ($self) = @_;
126 my ($dat);
127
128 $dat .= pack("nnnnnnn", (0) x 7); # placeholders for stateSize, classTable, stateArray, entryTable, actionLists, components, ligatures
129
130 my $classTable = length($dat);
131 my $classes = $self->{'classes'};
132 $dat .= AAT_pack_classes($classes);
133
134 my $stateArray = length($dat);
135 my $states = $self->{'states'};
136
137 my ($dat1, $stateSize, $entries) = AAT_pack_states($classes, $stateArray, $states,
138 sub {
139 ( $_->{'flags'} & 0xc000, $_->{'actions'} )
140 }
141 );
142 $dat .= $dat1;
143
144 my $actionLists = $self->{'actionLists'};
145 my %actionListOffset;
146 my $actionListDataLength = 0;
147 my @actionListEntries;
148 foreach (0 .. $#$entries) {
149 my ($nextState, $flags, $offset) = split(/,/, $entries->[$_]);
150 if ($offset eq "") {
151 $offset = undef;
152 }
153 else {
154 if (defined $actionListOffset{$offset}) {
155 $offset = $actionListOffset{$offset};
156 }
157 else {
158 $actionListOffset{$offset} = $actionListDataLength;
159 my $list = $actionLists->[$offset];
160 $actionListDataLength += 4 * @$list;
161 push @actionListEntries, $list;
162 $offset = $actionListOffset{$offset};
163 }
164 }
165 $entries->[$_] = [ $nextState, $flags, $offset ];
166 }
167 my $entryTable = length($dat);
168 my $ligActionLists = ($entryTable + @$entries * 4 + 3) & ~3;
169 foreach (@$entries) {
170 $_->[2] += $ligActionLists if defined $_->[2];
171 $dat .= pack("nn", $_->[0], $_->[1] + $_->[2]);
172 }
173 $dat .= pack("C*", (0) x ($ligActionLists - $entryTable - @$entries * 4));
174
175 die "internal error" unless length($dat) == $ligActionLists;
176
177 my $componentTable = length($dat) + $actionListDataLength;
178 my $actionList;
179 foreach $actionList (@actionListEntries) {
180 foreach (0 .. $#$actionList) {
181 my $action = $actionList->[$_];
182 my $val = $action->{'component'} + $componentTable / 2;
183 $val += 0x40000000 if $val < 0;
184 $val &= 0x3fffffff;
185 $val |= 0x40000000 if $action->{'store'};
186 $val |= 0x80000000 if $_ == $#$actionList;
187 $dat .= pack("N", $val);
188 }
189 }
190
191 die "internal error" unless length($dat) == $componentTable;
192
193 my $components = $self->{'components'};
194 my $ligatureTable = $componentTable + @$components * 2;
195 $dat .= pack("n*", map { (index($_, '+') >= 0 ? $ligatureTable : 0) + $_ } @$components);
196
197 my $ligatures = $self->{'ligatures'};
198 $dat .= pack("n*", @$ligatures);
199
200 $dat1 = pack("nnnnnnn", $stateSize, $classTable, $stateArray, $entryTable, $ligActionLists, $componentTable, $ligatureTable);
201 substr($dat, 0, length($dat1)) = $dat1;
202
203 return $dat;
204 }
205
206 =head2 $t->print($fh)
207
208 Prints a human-readable representation of the table
209
210 =cut
211
212 sub print
213 {
214 my ($self, $fh) = @_;
215
216 my $post = $self->post();
217
218 $fh = 'STDOUT' unless defined $fh;
219
220 $self->print_classes($fh);
221
222 $fh->print("\n");
223 my $states = $self->{'states'};
224 foreach (0 .. $#$states) {
225 $fh->printf("\t\tState %d:", $_);
226 my $state = $states->[$_];
227 foreach (@$state) {
228 my $flags;
229 $flags .= "!" if ($_->{'flags'} & 0x4000);
230 $flags .= "*" if ($_->{'flags'} & 0x8000);
231 $fh->printf("\t(%s%d,%s)", $flags, $_->{'nextState'}, defined $_->{'actions'} ? $_->{'actions'} : "=");
232 }
233 $fh->print("\n");
234 }
235
236 $fh->print("\n");
237 my $actionLists = $self->{'actionLists'};
238 foreach (0 .. $#$actionLists) {
239 $fh->printf("\t\tList %d:\t", $_);
240 my $actionList = $actionLists->[$_];
241 $fh->printf("%s\n", join(", ", map { ($_->{'component'} . ($_->{'store'} ? "*" : "") ) } @$actionList));
242 }
243
244 my $ligatureTable = $self->{'ligatureTable'};
245
246 $fh->print("\n");
247 my $components = $self->{'components'};
248 foreach (0 .. $#$components) {
249 $fh->printf("\t\tComponent %d: %s\n", $_, $components->[$_]);
250 }
251
252 $fh->print("\n");
253 my $ligatures = $self->{'ligatures'};
254 foreach (0 .. $#$ligatures) {
255 $fh->printf("\t\tLigature %d: %d [%s]\n", $_, $ligatures->[$_], $post->{'VAL'}[$ligatures->[$_]]);
256 }
257 }
258
259 1;
260
261 =head1 BUGS
262
263 None known
264
265 =head1 AUTHOR
266
267 Jonathan Kew L<Jonathan_Kew@sil.org>. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and
268 licensing.
269
270 =cut
271