Font/TTF/AATutils.pm untested

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