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