API3/Compat/API2/Basic/TTF/AATutils.pm untested

filter: Statementstmt 0.0% 0 / 422 Branchbran 0.0% 0 / 238 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 723 CC 120 SCAR 55.0
CC
120
Cov
0%
AAT_pack_lookup
380.0
AAT_write_lookup
380.0
xmldump
342.0
CRAP 245.1
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: AATutils.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $
25 #
26 #=======================================================================
27 package PDF::API3::Compat::API2::Basic::TTF::AATutils;
28
29 use strict;
30 use vars qw(@ISA @EXPORT);
31 require Exporter;
32
33 use PDF::API3::Compat::API2::Basic::TTF::Utils;
34
35 @ISA = qw(Exporter);
36 @EXPORT = qw(
37 AAT_read_lookup
38 AAT_pack_lookup
39 AAT_write_lookup
40 AAT_pack_classes
41 AAT_write_classes
42 AAT_pack_states
43 AAT_write_states
44 AAT_read_state_table
45 AAT_read_subtable
46 xmldump
47 );
48
49 sub xmldump
50 {
51 my ($var, $links, $depth, $processedVars, $type) = @_;
52
53 $processedVars = {} unless (defined $processedVars);
54 print("<?xml version='1.0' encoding='UTF-8'?>\n") if $depth == 0; # not necessarily true encoding for all text!
55
56 my $indent = "\t" x $depth;
57
58 my ($objType, $addr) = ($var =~ m/^.+=(.+)\((.+)\)$/);
59 unless (defined $type) {
60 if (defined $addr) {
61 if (defined $processedVars->{$addr}) {
62 if ($links) {
63 printf("%s%s\n", $indent, "<a href=\"#$addr\">$objType</a>");
64 }
65 else {
66 printf("%s%s\n", $indent, "<a>$objType</a>");
67 }
68 return;
69 }
70 $processedVars->{$addr} = 1;
71 }
72 }
73
74 $type = ref $var unless defined $type;
75
76 if ($type eq 'REF') {
77 printf("%s<ref val=\"%s\"/>\n", $indent, $$var);
78 }
79 elsif ($type eq 'SCALAR') {
80 printf("%s<scalar>%s</scalar>\n", $indent, $var);
81 }
82 elsif ($type eq 'ARRAY') {
83 # printf("%s<array>\n", $indent);
84 foreach (0 .. $#$var) {
85 if (ref($var->[$_])) {
86 printf("%s<arrayItem index=\"%d\">\n", $indent, $_);
87 xmldump($var->[$_], $links, $depth + 1, $processedVars);
88 printf("%s</arrayItem>\n", $indent);
89 }
90 else {
91 printf("%s<arrayItem index=\"%d\">%s</arrayItem>\n", $indent, $_, $var->[$_]);
92 }
93 }
94 # printf("%s</array>\n", $indent);
95 }
96 elsif ($type eq 'HASH') {
97 # printf("%s<hash>\n", $indent);
98 foreach (sort keys %$var) {
99 if (ref($var->{$_})) {
100 printf("%s<hashElem key=\"%s\">\n", $indent, $_);
101 xmldump($var->{$_}, $links, $depth + 1, $processedVars);
102 printf("%s</hashElem>\n", $indent);
103 }
104 else {
105 printf("%s<hashElem key=\"%s\">%s</hashElem>\n", $indent, $_, $var->{$_});
106 }
107 }
108 # printf("%s</hash>\n", $indent);
109 }
110 elsif ($type eq 'CODE') {
111 printf("%s<CODE/>\n", $indent, $var);
112 }
113 elsif ($type eq 'GLOB') {
114 printf("%s<GLOB/>\n", $indent, $var);
115 }
116 elsif ($type eq '') {
117 printf("%s<val>%s</val>\n", $indent, $var);
118 }
119 else {
120 if ($links) {
121 printf("%s<obj class=\"%s\" id=\"#%s\">\n", $indent, $type, $addr);
122 }
123 else {
124 printf("%s<obj class=\"%s\">\n", $indent, $type);
125 }
126 xmldump($var, $links, $depth + 1, $processedVars, $objType);
127 printf("%s</obj>\n", $indent);
128 }
129 }
130
131 =head2 ($classes, $states) = AAT_read_subtable($fh, $baseOffset, $subtableStart, $limits)
132
133 =cut
134
135 sub AAT_read_subtable
136 {
137 my ($fh, $baseOffset, $subtableStart, $limits) = @_;
138
139 my $limit = 0xffffffff;
140 foreach (@$limits) {
141 $limit = $_ if ($_ > $subtableStart and $_ < $limit);
142 }
143 die if $limit == 0xffffffff;
144
145 my $dat;
146 $fh->seek($baseOffset + $subtableStart, IO::File::SEEK_SET);
147 $fh->read($dat, $limit - $subtableStart);
148
149 $dat;
150 }
151
152 =head2 $length = AAT_write_state_table($fh, $classes, $states, $numExtraTables, $packEntry)
153
154 $packEntry is a subroutine for packing an entry into binary form, called as
155
156 $dat = $packEntry($entry, $entryTable, $numEntries)
157
158 where the entry is a comma-separated list of nextStateOffset, flags, actions
159
160 =cut
161
162 sub AAT_pack_state_table
163 {
164 my ($classes, $states, $numExtraTables, $packEntry) = @_;
165
166 my ($dat) = pack("n*", (0) x (4 + $numExtraTables)); # placeholders for stateSize, classTable, stateArray, entryTable
167
168 my ($firstGlyph, $lastGlyph) = (0xffff, 0, 0);
169 my (@classTable, $i);
170 foreach $i (0 .. $#$classes) {
171 my $class = $classes->[$i];
172 foreach (@$class) {
173 $firstGlyph = $_ if $_ < $firstGlyph;
174 $lastGlyph = $_ if $_ > $lastGlyph;
175 $classTable[$_] = $i;
176 }
177 }
178
179 my $classTable = length($dat);
180 $dat .= pack("nnC*", $firstGlyph, $lastGlyph - $firstGlyph + 1,
181 map { defined $classTable[$_] ? $classTable[$_] : 1 } ($firstGlyph .. $lastGlyph));
182 $dat .= pack("C", 0) if (($lastGlyph - $firstGlyph) & 1) == 0; # pad if odd number of glyphs
183
184 my $stateArray = length($dat);
185 my (@entries, %entries);
186 my $state = $states->[0];
187 my $stateSize = @$state;
188 die "stateSize below minimum allowed (4)" if $stateSize < 4;
189 die "stateSize (" . $stateSize . ") too small for max class number (" . $#$classes . ")" if $stateSize < $#$classes + 1;
190 warn "state array has unreachable columns" if $stateSize > $#$classes + 1;
191
192 foreach (@$states) {
193 die "inconsistent state size" if @$_ != $stateSize;
194 foreach (@$_) {
195 my $actions = $_->{'actions'};
196 my $entry = join(",", $stateArray + $_->{'nextState'} * $stateSize, $_->{'flags'}, ref($actions) eq 'ARRAY' ? @$actions : $actions);
197 if (not defined $entries{$entry}) {
198 push @entries, $entry;
199 $entries{$entry} = $#entries;
200 die "too many different state array entries" if $#entries == 256;
201 }
202 $dat .= pack("C", $entries{$entry});
203 }
204 }
205 $dat .= pack("C", 0) if (@$states & 1) != 0 and ($stateSize & 1) != 0; # pad if state array size is odd
206
207 my $entryTable = length($dat);
208 $dat .= map { &$packEntry($_, $entryTable, $#entries + 1) } @entries;
209
210 my ($dat1) = pack("nnnn", $stateSize, $classTable, $stateArray, $entryTable);
211 substr($dat, 0, length($dat1)) = $dat1;
212
213 return $dat;
214 }
215
216 sub AAT_write_state_table
217 {
218 my ($fh, $classes, $states, $numExtraTables, $packEntry) = @_;
219
220 my $stateTableStart = $fh->tell();
221
222 $fh->print(pack("n*", (0) x (4 + $numExtraTables))); # placeholders for stateSize, classTable, stateArray, entryTable
223
224 my ($firstGlyph, $lastGlyph) = (0xffff, 0, 0);
225 my (@classTable, $i);
226 foreach $i (0 .. $#$classes) {
227 my $class = $classes->[$i];
228 foreach (@$class) {
229 $firstGlyph = $_ if $_ < $firstGlyph;
230 $lastGlyph = $_ if $_ > $lastGlyph;
231 $classTable[$_] = $i;
232 }
233 }
234
235 my $classTable = $fh->tell() - $stateTableStart;
236 $fh->print(pack("nnC*", $firstGlyph, $lastGlyph - $firstGlyph + 1,
237 map { defined $classTable[$_] ? $classTable[$_] : 1 } ($firstGlyph .. $lastGlyph)));
238 $fh->print(pack("C", 0)) if (($lastGlyph - $firstGlyph) & 1) == 0; # pad if odd number of glyphs
239
240 my $stateArray = $fh->tell() - $stateTableStart;
241 my (@entries, %entries);
242 my $state = $states->[0];
243 my $stateSize = @$state;
244 die "stateSize below minimum allowed (4)" if $stateSize < 4;
245 die "stateSize (" . $stateSize . ") too small for max class number (" . $#$classes . ")" if $stateSize < $#$classes + 1;
246 warn "state array has unreachable columns" if $stateSize > $#$classes + 1;
247
248 foreach (@$states) {
249 die "inconsistent state size" if @$_ != $stateSize;
250 foreach (@$_) {
251 my $actions = $_->{'actions'};
252 my $entry = join(",", $stateArray + $_->{'nextState'} * $stateSize, $_->{'flags'}, ref($actions) eq 'ARRAY' ? @$actions : $actions);
253 if (not defined $entries{$entry}) {
254 push @entries, $entry;
255 $entries{$entry} = $#entries;
256 die "too many different state array entries" if $#entries == 256;
257 }
258 $fh->print(pack("C", $entries{$entry}));
259 }
260 }
261 $fh->print(pack("C", 0)) if (@$states & 1) != 0 and ($stateSize & 1) != 0; # pad if state array size is odd
262
263 my $entryTable = $fh->tell() - $stateTableStart;
264 $fh->print(map { &$packEntry($_, $entryTable, $#entries + 1) } @entries);
265
266 my $length = $fh->tell() - $stateTableStart;
267 $fh->seek($stateTableStart, IO::File::SEEK_SET);
268 $fh->print(pack("nnnn", $stateSize, $classTable, $stateArray, $entryTable));
269
270 $fh->seek($stateTableStart + $length, IO::File::SEEK_SET);
271 $length;
272 }
273
274 sub AAT_pack_classes
275 {
276 my ($classes) = @_;
277
278 my ($firstGlyph, $lastGlyph) = (0xffff, 0, 0);
279 my (@classTable, $i);
280 foreach $i (0 .. $#$classes) {
281 my $class = $classes->[$i];
282 foreach (@$class) {
283 $firstGlyph = $_ if $_ < $firstGlyph;
284 $lastGlyph = $_ if $_ > $lastGlyph;
285 $classTable[$_] = $i;
286 }
287 }
288
289 my ($dat) = pack("nnC*", $firstGlyph, $lastGlyph - $firstGlyph + 1,
290 map { defined $classTable[$_] ? $classTable[$_] : 1 } ($firstGlyph .. $lastGlyph));
291 $dat .= pack("C", 0) if (($lastGlyph - $firstGlyph) & 1) == 0; # pad if odd number of glyphs
292
293 return $dat;
294 }
295
296 sub AAT_write_classes
297 {
298 my ($fh, $classes) = @_;
299
300 $fh->print(AAT_pack_classes($fh, $classes));
301 }
302
303 sub AAT_pack_states
304 {
305 my ($classes, $stateArray, $states, $buildEntryProc) = @_;
306
307 my ($entries, %entryHash);
308 my $state = $states->[0];
309 my $stateSize = @$state;
310
311 die "stateSize below minimum allowed (4)" if $stateSize < 4;
312 die "stateSize (" . $stateSize . ") too small for max class number (" . $#$classes . ")" if $stateSize < $#$classes + 1;
313 warn "state array has unreachable columns" if $stateSize > $#$classes + 1;
314
315 my ($dat);
316 foreach (@$states) {
317 die "inconsistent state size" if @$_ != $stateSize;
318 foreach (@$_) {
319 my $entry = join(",", $stateArray + $_->{'nextState'} * $stateSize, &$buildEntryProc($_));
320 if (not defined $entryHash{$entry}) {
321 push @$entries, $entry;
322 $entryHash{$entry} = $#$entries;
323 die "too many different state array entries" if $#$entries == 256;
324 }
325 $dat .= pack("C", $entryHash{$entry});
326 }
327 }
328 $dat .= pack("C", 0) if (@$states & 1) != 0 and ($stateSize & 1) != 0; # pad if state array size is odd
329
330 ($dat, $stateSize, $entries);
331 }
332
333 sub AAT_write_states
334 {
335 my ($fh, $classes, $stateArray, $states, $buildEntryProc) = @_;
336
337 my ($entries, %entryHash);
338 my $state = $states->[0];
339 my $stateSize = @$state;
340
341 die "stateSize below minimum allowed (4)" if $stateSize < 4;
342 die "stateSize (" . $stateSize . ") too small for max class number (" . $#$classes . ")" if $stateSize < $#$classes + 1;
343 warn "state array has unreachable columns" if $stateSize > $#$classes + 1;
344
345 foreach (@$states) {
346 die "inconsistent state size" if @$_ != $stateSize;
347 foreach (@$_) {
348 my $entry = join(",", $stateArray + $_->{'nextState'} * $stateSize, &$buildEntryProc($_));
349 if (not defined $entryHash{$entry}) {
350 push @$entries, $entry;
351 $entryHash{$entry} = $#$entries;
352 die "too many different state array entries" if $#$entries == 256;
353 }
354 $fh->print(pack("C", $entryHash{$entry}));
355 }
356 }
357 $fh->print(pack("C", 0)) if (@$states & 1) != 0 and ($stateSize & 1) != 0; # pad if state array size is odd
358
359 ($stateSize, $entries);
360 }
361
362 =head2 ($classes, $states, $entries) = AAT_read_state_table($fh, $numActionWords)
363
364 =cut
365
366 sub AAT_read_state_table
367 {
368 my ($fh, $numActionWords) = @_;
369
370 my $stateTableStart = $fh->tell();
371 my $dat;
372 $fh->read($dat, 8);
373 my ($stateSize, $classTable, $stateArray, $entryTable) = unpack("nnnn", $dat);
374
375 my $classes; # array of lists of glyphs
376
377 $fh->seek($stateTableStart + $classTable, IO::File::SEEK_SET);
378 $fh->read($dat, 4);
379 my ($firstGlyph, $nGlyphs) = unpack("nn", $dat);
380 $fh->read($dat, $nGlyphs);
381 foreach (unpack("C*", $dat)) {
382 if ($_ != 1) {
383 my $class = $classes->[$_];
384 push(@$class, $firstGlyph);
385 $classes->[$_] = $class unless defined $classes->[$_];
386 }
387 $firstGlyph++;
388 }
389
390 $fh->seek($stateTableStart + $stateArray, IO::File::SEEK_SET);
391 my $states; # array of arrays of hashes{nextState, flags, actions}
392
393 my $entrySize = 4 + ($numActionWords * 2);
394 my $lastState = 1;
395 my $entries;
396 while ($#$states < $lastState) {
397 $fh->read($dat, $stateSize);
398 my @stateEntries = unpack("C*", $dat);
399 my $state;
400 foreach (@stateEntries) {
401 if (not defined $entries->[$_]) {
402 my $loc = $fh->tell();
403 $fh->seek($stateTableStart + $entryTable + ($_ * $entrySize), IO::File::SEEK_SET);
404 $fh->read($dat, $entrySize);
405 my ($nextState, $flags, $actions);
406 ($nextState, $flags, @$actions) = unpack("n*", $dat);
407 $nextState -= $stateArray;
408 $nextState /= $stateSize;
409 $entries->[$_] = { 'nextState' => $nextState, 'flags' => $flags };
410 $entries->[$_]->{'actions'} = $actions if $numActionWords > 0;
411 $lastState = $nextState if ($nextState > $lastState);
412 $fh->seek($loc, IO::File::SEEK_SET);
413 }
414 push(@$state, $entries->[$_]);
415 }
416 push(@$states, $state);
417 }
418
419 ($classes, $states, $entries);
420 }
421
422 =head2 ($format, $lookup) = AAT_read_lookup($fh, $valueSize, $length, $default)
423
424 =cut
425
426 sub AAT_read_lookup
427 {
428 my ($fh, $valueSize, $length, $default) = @_;
429
430 my $lookupStart = $fh->tell();
431 my ($dat, $unpackChar);
432 if ($valueSize == 1) {
433 $unpackChar = "C";
434 }
435 elsif ($valueSize == 2) {
436 $unpackChar = "n";
437 }
438 elsif ($valueSize == 4) {
439 $unpackChar = "N";
440 }
441 else {
442 die "unsupported value size";
443 }
444
445 $fh->read($dat, 2);
446 my $format = unpack("n", $dat);
447 my $lookup;
448
449 if ($format == 0) {
450 $fh->read($dat, $length - 2);
451 my $i = -1;
452 $lookup = { map { $i++; ($_ != $default) ? ($i, $_) : () } unpack($unpackChar . "*", $dat) };
453 }
454
455 elsif ($format == 2) {
456 $fh->read($dat, 10);
457 my ($unitSize, $nUnits, $searchRange, $entrySelector, $rangeShift) = unpack("nnnnn", $dat);
458 die if $unitSize != 4 + $valueSize;
459 foreach (1 .. $nUnits) {
460 $fh->read($dat, $unitSize);
461 my ($lastGlyph, $firstGlyph, $value) = unpack("nn" . $unpackChar, $dat);
462 if ($firstGlyph != 0xffff and $value != $default) {
463 foreach ($firstGlyph .. $lastGlyph) {
464 $lookup->{$_} = $value;
465 }
466 }
467 }
468 }
469
470 elsif ($format == 4) {
471 $fh->read($dat, 10);
472 my ($unitSize, $nUnits, $searchRange, $entrySelector, $rangeShift) = unpack("nnnnn", $dat);
473 die if $unitSize != 6;
474 foreach (1 .. $nUnits) {
475 $fh->read($dat, $unitSize);
476 my ($lastGlyph, $firstGlyph, $offset) = unpack("nnn", $dat);
477 if ($firstGlyph != 0xffff) {
478 my $loc = $fh->tell();
479 $fh->seek($lookupStart + $offset, IO::File::SEEK_SET);
480 $fh->read($dat, ($lastGlyph - $firstGlyph + 1) * $valueSize);
481 my @values = unpack($unpackChar . "*", $dat);
482 foreach (0 .. $lastGlyph - $firstGlyph) {
483 $lookup->{$firstGlyph + $_} = $values[$_] if $values[$_] != $default;
484 }
485 $fh->seek($loc, IO::File::SEEK_SET);
486 }
487 }
488 }
489
490 elsif ($format == 6) {
491 $fh->read($dat, 10);
492 my ($unitSize, $nUnits, $searchRange, $entrySelector, $rangeShift) = unpack("nnnnn", $dat);
493 die if $unitSize != 2 + $valueSize;
494 foreach (1 .. $nUnits) {
495 $fh->read($dat, $unitSize);
496 my ($glyph, $value) = unpack("n" . $unpackChar, $dat);
497 $lookup->{$glyph} = $value if $glyph != 0xffff and $value != $default;
498 }
499 }
500
501 elsif ($format == 8) {
502 $fh->read($dat, 4);
503 my ($firstGlyph, $glyphCount) = unpack("nn", $dat);
504 $fh->read($dat, $glyphCount * $valueSize);
505 $firstGlyph--;
506 $lookup = { map { $firstGlyph++; $_ != $default ? ($firstGlyph, $_) : () } unpack($unpackChar . "*", $dat) };
507 }
508
509 else {
510 die "unknown lookup format";
511 }
512
513 $fh->seek($lookupStart + $length, IO::File::SEEK_SET);
514
515 ($format, $lookup);
516 }
517
518 =head2 AAT_write_lookup($fh, $format, $lookup, $valueSize, $default)
519
520 =cut
521
522 sub AAT_pack_lookup
523 {
524 my ($format, $lookup, $valueSize, $default) = @_;
525
526 my $packChar;
527 if ($valueSize == 1) {
528 $packChar = "C";
529 }
530 elsif ($valueSize == 2) {
531 $packChar = "n";
532 }
533 elsif ($valueSize == 4) {
534 $packChar = "N";
535 }
536 else {
537 die "unsupported value size";
538 }
539
540 my ($dat) = pack("n", $format);
541
542 my ($firstGlyph, $lastGlyph) = (0xffff, 0);
543 foreach (keys %$lookup) {
544 $firstGlyph = $_ if $_ < $firstGlyph;
545 $lastGlyph = $_ if $_ > $lastGlyph;
546 }
547 my $glyphCount = $lastGlyph - $firstGlyph + 1;
548
549 if ($format == 0) {
550 $dat .= pack($packChar . "*", map { defined $lookup->{$_} ? $lookup->{$_} : defined $default ? $default : $_ } (0 .. $lastGlyph));
551 }
552
553 elsif ($format == 2) {
554 my $prev = $default;
555 my $segStart = $firstGlyph;
556 my $dat1;
557 foreach ($firstGlyph .. $lastGlyph + 1) {
558 my $val = $lookup->{$_};
559 $val = $default unless defined $val;
560 if ($val != $prev) {
561 $dat1 .= pack("nn" . $packChar, $_ - 1, $segStart, $prev) if $prev != $default;
562 $prev = $val;
563 $segStart = $_;
564 }
565 }
566 $dat1 .= pack("nn" . $packChar, 0xffff, 0xffff, 0);
567 my $unitSize = 4 + $valueSize;
568 $dat .= pack("nnnnn", $unitSize, TTF_bininfo(length($dat1) / $unitSize, $unitSize));
569 $dat .= $dat1;
570 }
571
572 elsif ($format == 4) {
573 my $segArray = new PDF::API3::Compat::API2::Basic::TTF::Segarr($valueSize);
574 $segArray->add_segment($firstGlyph, 1, map { $lookup->{$_} } ($firstGlyph .. $lastGlyph));
575 my ($start, $end, $offset);
576 $offset = 12 + @$segArray * 6 + 6; # 12 is size of format word + binSearchHeader; 6 bytes per segment; 6 for terminating segment
577 my $dat1;
578 foreach (@$segArray) {
579 $start = $_->{'START'};
580 $end = $start + $_->{'LEN'} - 1;
581 $dat1 .= pack("nnn", $end, $start, $offset);
582 $offset += $_->{'LEN'} * 2;
583 }
584 $dat1 .= pack("nnn", 0xffff, 0xffff, 0);
585 $dat .= pack("nnnnn", 6, TTF_bininfo(length($dat1) / 6, 6));
586 $dat .= $dat1;
587 foreach (@$segArray) {
588 $dat1 = $_->{'VAL'};
589 $dat .= pack($packChar . "*", @$dat1);
590 }
591 }
592
593 elsif ($format == 6) {
594 die "unsupported" if $valueSize != 2;
595 my $dat1 = pack("n*", map { $_, $lookup->{$_} } sort { $a <=> $b } grep { $lookup->{$_} ne $default } keys %$lookup);
596 my $unitSize = 2 + $valueSize;
597 $dat .= pack("nnnnn", $unitSize, TTF_bininfo(length($dat1) / $unitSize, $unitSize));
598 $dat .= $dat1;
599 }
600
601 elsif ($format == 8) {
602 $dat .= pack("nn", $firstGlyph, $lastGlyph - $firstGlyph + 1);
603 $dat .= pack($packChar . "*", map { defined $lookup->{$_} ? $lookup->{$_} : defined $default ? $default : $_ } ($firstGlyph .. $lastGlyph));
604 }
605
606 else {
607 die "unknown lookup format";
608 }
609
610 my $padBytes = (4 - (length($dat) & 3)) & 3;
611 $dat .= pack("C*", (0) x $padBytes);
612
613 return $dat;
614 }
615
616 sub AAT_write_lookup
617 {
618 my ($fh, $format, $lookup, $valueSize, $default) = @_;
619
620 my $lookupStart = $fh->tell();
621 my $packChar;
622 if ($valueSize == 1) {
623 $packChar = "C";
624 }
625 elsif ($valueSize == 2) {
626 $packChar = "n";
627 }
628 elsif ($valueSize == 4) {
629 $packChar = "N";
630 }
631 else {
632 die "unsupported value size";
633 }
634
635 $fh->print(pack("n", $format));
636
637 my ($firstGlyph, $lastGlyph) = (0xffff, 0);
638 foreach (keys %$lookup) {
639 $firstGlyph = $_ if $_ < $firstGlyph;
640 $lastGlyph = $_ if $_ > $lastGlyph;
641 }
642 my $glyphCount = $lastGlyph - $firstGlyph + 1;
643
644 if ($format == 0) {
645 $fh->print(pack($packChar . "*", map { defined $lookup->{$_} ? $lookup->{$_} : defined $default ? $default : $_ } (0 .. $lastGlyph)));
646 }
647
648 elsif ($format == 2) {
649 my $prev = $default;
650 my $segStart = $firstGlyph;
651 my $dat;
652 foreach ($firstGlyph .. $lastGlyph + 1) {
653 my $val = $lookup->{$_};
654 $val = $default unless defined $val;
655 if ($val != $prev) {
656 $dat .= pack("nn" . $packChar, $_ - 1, $segStart, $prev) if $prev != $default;
657 $prev = $val;
658 $segStart = $_;
659 }
660 }
661 $dat .= pack("nn" . $packChar, 0xffff, 0xffff, 0);
662 my $unitSize = 4 + $valueSize;
663 $fh->print(pack("nnnnn", $unitSize, TTF_bininfo(length($dat) / $unitSize, $unitSize)));
664 $fh->print($dat);
665 }
666
667 elsif ($format == 4) {
668 my $segArray = new PDF::API3::Compat::API2::Basic::TTF::Segarr($valueSize);
669 $segArray->add_segment($firstGlyph, 1, map { $lookup->{$_} } ($firstGlyph .. $lastGlyph));
670 my ($start, $end, $offset);
671 $offset = 12 + @$segArray * 6 + 6; # 12 is size of format word + binSearchHeader; 6 bytes per segment; 6 for terminating segment
672 my $dat;
673 foreach (@$segArray) {
674 $start = $_->{'START'};
675 $end = $start + $_->{'LEN'} - 1;
676 $dat .= pack("nnn", $end, $start, $offset);
677 $offset += $_->{'LEN'} * 2;
678 }
679 $dat .= pack("nnn", 0xffff, 0xffff, 0);
680 $fh->print(pack("nnnnn", 6, TTF_bininfo(length($dat) / 6, 6)));
681 $fh->print($dat);
682 foreach (@$segArray) {
683 $dat = $_->{'VAL'};
684 $fh->print(pack($packChar . "*", @$dat));
685 }
686 }
687
688 elsif ($format == 6) {
689 die "unsupported" if $valueSize != 2;
690 my $dat = pack("n*", map { $_, $lookup->{$_} } sort { $a <=> $b } grep { $lookup->{$_} ne $default } keys %$lookup);
691 my $unitSize = 2 + $valueSize;
692 $fh->print(pack("nnnnn", $unitSize, TTF_bininfo(length($dat) / $unitSize, $unitSize)));
693 $fh->print($dat);
694 }
695
696 elsif ($format == 8) {
697 $fh->print(pack("nn", $firstGlyph, $lastGlyph - $firstGlyph + 1));
698 $fh->print(pack($packChar . "*", map { defined $lookup->{$_} ? $lookup->{$_} : defined $default ? $default : $_ } ($firstGlyph .. $lastGlyph)));
699 }
700
701 else {
702 die "unknown lookup format";
703 }
704
705 my $length = $fh->tell() - $lookupStart;
706 my $padBytes = (4 - ($length & 3)) & 3;
707 $fh->print(pack("C*", (0) x $padBytes));
708 $length += $padBytes;
709
710 $length;
711 }
712
713 1;
714