This file is indexed.

/usr/share/perl5/Font/TTF/GPOS.pm is in libfont-ttf-perl 1.04-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
package Font::TTF::GPOS;

=head1 NAME

Font::TTF::GPOS - Support for Opentype GPOS tables in conjunction with TTOpen

=head1 DESCRIPTION

The GPOS table is one of the most complicated tables in the TTF spec and the
corresponding data structure abstraction is also not trivial. While much of the
structure of a GPOS is shared with a GSUB table via the L<Font::TTF::Ttopen>

=head1 INSTANCE VARIABLES

Here we describe the additions and lookup specific information for GPOS tables.
Unfortunately there is no one abstraction which seems to work comfortable for
all GPOS tables, so we will also examine how the variables are used for different
lookup types.

The following are the values allowed in the ACTION_TYPE and MATCH_TYPE variables:

=over 4

=item ACTION_TYPE

This can take any of the following values

=over 8

=item a

The ACTION is an array of anchor tables

=item o

Offset. There is no RULE array. The ADJUST variable contains a value record (see
later in this description)

=item v

The ACTION is a value record.

=item p

Pair adjustment. The ACTION contains an array of two value records for the matched
two glyphs.

=item e

Exit and Entry records. The ACTION contains an array of two anchors corresponding
to the exit and entry anchors for the glyph.

=item l

Indicates a lookup based contextual rule as per the GSUB table.

=back

=item MATCH_TYPE

This can take any of the following values

=over 8

=item g

A glyph array

=item c

An array of class values

=item o

An array of coverage tables

=back

=back

The following variables are added for Attachment Positioning Subtables:

=over 4

=item MATCH

This contains an array of glyphs to match against for all RULES. It is much like
having the same MATCH string in all RULES. In the cases it is used so far, it only
ever contains one element.

=item MARKS

This contains a Mark array consisting of each element being a subarray of two
elements:

=over 8

=item CLASS

The class that this mark uses on its base

=item ANCHOR

The anchor with which to attach this mark glyph

=back

The base table for mark to base, ligature or mark attachment positioning is
structured with the ACTION containing an array of anchors corresponding to each
attachment class. For ligatures, there is more than one RULE in the RULE array
corresponding to each glyph in the coverage table.

=back

Other variables which are provided for informational purposes are:

=over 4

=item VFMT

Value format for the adjustment of the glyph matched by the coverage table.

=item VFMT2

Value format used in pair adjustment for the second glyph in the pair

=back

=head2 Value Records

There is a subtype used in GPOS tables called a value record. It is used to adjust
the position of a glyph from its default position. The value record is variable
length with a bitfield at the beginning to indicate which of the following
entries are included. The bitfield is not stored since it is recalculated at
write time.

=over 4

=item XPlacement

Horizontal adjustment for placement (not affecting other unattached glyphs)

=item YPlacement

Vertical adjustment for placement (not affecting other unattached glyphs)

=item XAdvance

Adjust the advance width glyph (used only in horizontal writing systems)

=item YAdvance

Adjust the vertical advance (used only in vertical writing systems)

=item XPlaDevice

Device table for device specific adjustment of horizontal placement

=item YPlaDevice

Device table for device specific adjustment of vertical placement

=item XAdvDevice

Device table for device specific adjustment of horizontal advance

=item YAdDevice

Device table for device specific adjustment of vertical advance

=item XIdPlacement

Horizontal placement metric id (for Multiple Master fonts - but that is all I know!)

=item YIdPlacement

Vertical placement metric id

=item XIdAdvance

Horizontal advance metric id

=item YIdAdvance

Vertical advance metric id

=back

=head1 CORRESPONDANCE TO LAYOUT TYPES

Here is what is stored in the ACTION_TYPE and MATCH_TYPE for each of the known
GPOS subtable types:

                1.1 1.2 2.1 2.2 3   4   5   6   7.1 7.2 7.3 8.1 8.2 8.3
  ACTION_TYPE    o   v   p   p  e   a   a   a    l   l   l   l   l   l
  MATCH_TYPE             g   c                   g   c   o   g   c   o


=head1 METHODS

=cut

use strict;
use Font::TTF::Ttopen;
use Font::TTF::Delta;
use Font::TTF::Anchor;
use Font::TTF::Utils;
use vars qw(@ISA);

@ISA = qw(Font::TTF::Ttopen);


=head2 read_sub

Reads the subtable into the data structures

=cut

sub read_sub
{
    my ($self, $fh, $main_lookup, $sindex) = @_;
    my ($type) = $main_lookup->{'TYPE'};
    my ($loc) = $fh->tell();
    my ($lookup) = $main_lookup->{'SUB'}[$sindex];
    my ($dat, $mcount, $scount, $i, $j, $count, $fmt, $fmt2, $cover, $srec, $subst);
    my ($c1, $c2, $s, $moff, $boff);


    if ($type == 8)
    {
        $fh->read($dat, 4);
        ($fmt, $cover) = TTF_Unpack('S2', $dat);
        if ($fmt < 3)
        {
            $fh->read($dat, 2);
            $count = TTF_Unpack('S', $dat);
        }
    } else
    {
        $fh->read($dat, 6);
        ($fmt, $cover, $count) = TTF_Unpack("S3", $dat);
    }
    unless ($fmt == 3 && ($type == 7 || $type == 8))
    { $lookup->{'COVERAGE'} = $self->read_cover($cover, $loc, $lookup, $fh, 1); }

    $lookup->{'FORMAT'} = $fmt;
    if ($type == 1 && $fmt == 1)
    {
        $lookup->{'VFMT'} = $count;
        $lookup->{'ADJUST'} = $self->read_value($count, $loc, $lookup, $fh);
        $lookup->{'ACTION_TYPE'} = 'o';
    } elsif ($type == 1 && $fmt == 2)
    {
        $lookup->{'VFMT'} = $count;
        $fh->read($dat, 2);
        $mcount = unpack('n', $dat);
        for ($i = 0; $i < $mcount; $i++)
        { push (@{$lookup->{'RULES'}}, [{'ACTION' =>
                                    [$self->read_value($count, $loc, $lookup, $fh)]}]); }
        $lookup->{'ACTION_TYPE'} = 'v';
    } elsif ($type == 2 && $fmt == 1)
    {
        $lookup->{'VFMT'} = $count;
        $fh->read($dat, 4);
        ($fmt2, $mcount) = unpack('n2', $dat);
        $lookup->{'VFMT2'} = $fmt2;
        $fh->read($dat, $mcount << 1);
        foreach $s (unpack('n*', $dat))
        {
            $fh->seek($loc + $s, 0);
            $fh->read($dat, 2);
            $scount = TTF_Unpack('S', $dat);
            $subst = [];
            for ($i = 0; $i < $scount; $i++)
            {
                $srec = {};
                $fh->read($dat, 2);
                $srec->{'MATCH'} = [TTF_Unpack('S', $dat)];
                $srec->{'ACTION'} = [$self->read_value($count, $loc, $lookup, $fh),
                                     $self->read_value($fmt2, $loc, $lookup, $fh)];
                push (@$subst, $srec);
            }
            push (@{$lookup->{'RULES'}}, $subst);
        }
        $lookup->{'ACTION_TYPE'} = 'p';
        $lookup->{'MATCH_TYPE'} = 'g';
    } elsif ($type == 2 && $fmt == 2)
    {
        $fh->read($dat, 10);
        ($lookup->{'VFMT2'}, $c1, $c2, $mcount, $scount) = TTF_Unpack('S*', $dat);
        $lookup->{'CLASS'} = $self->read_cover($c1, $loc, $lookup, $fh, 0);
        $lookup->{'MATCH'} = [$self->read_cover($c2, $loc, $lookup, $fh, 0)];
        $lookup->{'VFMT'} = $count;
        for ($i = 0; $i < $mcount; $i++)
        {
            $subst = [];
            for ($j = 0; $j < $scount; $j++)
            {
                $srec = {};
                $srec->{'ACTION'} = [$self->read_value($lookup->{'VFMT'}, $loc, $lookup, $fh),
                                     $self->read_value($lookup->{'VFMT2'}, $loc, $lookup, $fh)];
                push (@$subst, $srec);
            }
            push (@{$lookup->{'RULES'}}, $subst);
        }
        $lookup->{'ACTION_TYPE'} = 'p';
        $lookup->{'MATCH_TYPE'} = 'c';
    } elsif ($type == 3 && $fmt == 1)
    {
        $fh->read($dat, $count << 2);
        for ($i = 0; $i < $count; $i++)
        { push (@{$lookup->{'RULES'}}, [{'ACTION' =>
                [$self->read_anchor(TTF_Unpack('S', substr($dat, $i << 2, 2)),
                        $loc, $lookup, $fh),
                 $self->read_anchor(TTF_Unpack('S', substr($dat, ($i << 2) + 2, 2)),
                        $loc, $lookup, $fh)]}]); }
        $lookup->{'ACTION_TYPE'} = 'e';
    } elsif ($type == 4 || $type == 5 || $type == 6)
    {
        my (@offs, $mloc, $thisloc, $ncomp, $k);

        $lookup->{'MATCH'} = [$lookup->{'COVERAGE'}];
        $lookup->{'COVERAGE'} = $self->read_cover($count, $loc, $lookup, $fh, 1);
        $fh->read($dat, 6);
        ($mcount, $moff, $boff) = TTF_Unpack('S*', $dat);
        $fh->seek($loc + $moff, 0);
        $fh->read($dat, 2);
        $count = TTF_Unpack('S', $dat);
        for ($i = 0; $i < $count; $i++)
        {
            $fh->read($dat, 4);
            push (@{$lookup->{'MARKS'}}, [TTF_Unpack('S', $dat),
                    $self->read_anchor(TTF_Unpack('S', substr($dat, 2, 2)) + $moff,
                            $loc, $lookup, $fh)]);
        }
        $fh->seek($loc + $boff, 0);
        $fh->read($dat, 2);
        $count = TTF_Unpack('S', $dat);
        $mloc = $fh->tell() - 2;
        $thisloc = $mloc;
        if ($type == 5)
        {
            $fh->read($dat, $count << 1);
            @offs = TTF_Unpack('S*', $dat);
        }
        for ($i = 0; $i < $count; $i++)
        {
            if ($type == 5)
            {
                $thisloc = $mloc + $offs[$i];
                $fh->seek($thisloc, 0);
                $fh->read($dat, 2);
                $ncomp = TTF_Unpack('S', $dat);
            } else
            { $ncomp = 1; }
            for ($j = 0; $j < $ncomp; $j++)
            {
                $subst = [];
                $fh->read($dat, $mcount << 1);
                for ($k = 0; $k < $mcount; $k++)
                { push (@$subst, $self->read_anchor(TTF_Unpack('S', substr($dat, $k << 1, 2)) + $thisloc - $loc,
                        $loc, $lookup, $fh)); }

                push (@{$lookup->{'RULES'}[$i]}, {'ACTION' => $subst});
            }
        }
        $lookup->{'ACTION_TYPE'} = 'a';
    } elsif ($type == 7 || $type == 8)
    { $self->read_context($lookup, $fh, $type - 2, $fmt, $cover, $count, $loc); }        
    $lookup;
}


=head2 $t->extension

Returns the table type number for the extension table

=cut

sub extension
{ return 9; }


=head2 $t->out_sub

Outputs the subtable to the given filehandle

=cut

sub out_sub
{
    my ($self, $fh, $main_lookup, $index, $ctables, $base) = @_;
    my ($type) = $main_lookup->{'TYPE'};
    my ($lookup) = $main_lookup->{'SUB'}[$index];
    my ($fmt) = $lookup->{'FORMAT'};
    my ($out, $r, $s, $t, $i, $j, $vfmt, $vfmt2, $loc1);
    my ($num) = $#{$lookup->{'RULES'}} + 1;
    my ($mtables) = {};
    my (@reftables);
    
    if ($type == 1 && $fmt == 1)
    {
        $out = pack('n2', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base));
        $vfmt = $self->fmt_value($lookup->{'ADJUST'});
        $out .= pack('n', $vfmt) . $self->out_value($lookup->{'ADJUST'}, $vfmt, $ctables, 6 + $base);
    } elsif ($type == 1 && $fmt == 2)
    {
        $vfmt = 0;
        foreach $r (@{$lookup->{'RULES'}})
        { $vfmt |= $self->fmt_value($r->[0]{'ACTION'}[0]); }
        $out = pack('n4', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base),
                            $vfmt, $#{$lookup->{'RULES'}} + 1);
        foreach $r (@{$lookup->{'RULES'}})
        { $out .= $self->out_value($r->[0]{'ACTION'}[0], $vfmt, $ctables, length($out) + $base); }
    } elsif ($type == 2 && $fmt < 3)
    {
        $vfmt = 0;
        $vfmt2 = 0;
        foreach $r (@{$lookup->{'RULES'}})
        {
            foreach $t (@$r)
            {
                $vfmt |= $self->fmt_value($t->{'ACTION'}[0]);
                $vfmt2 |= $self->fmt_value($t->{'ACTION'}[1]);
            }
        }
        if ($fmt == 1)
        {
            # start PairPosFormat1 subtable
            $out = pack('n5', 
                        $fmt, 
                        Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base),
                        $vfmt, 
                        $vfmt2, 
                        $#{$lookup->{'RULES'}} + 1); # PairSetCount
            my $off = 0;
            $off += length($out);
            $off += 2 * ($#{$lookup->{'RULES'}} + 1); # there will be PairSetCount offsets here
            my $pairsets = '';
            my (%cache);
            foreach $r (@{$lookup->{'RULES'}}) # foreach PairSet table
            {
                # write offset to this PairSet at end of PairPosFormat1 table
                if (defined $cache{"$r"})
                { $out .= pack('n', $cache{"$r"}); }
                else
                {
                    $out .= pack('n', $off);
                    $cache{"$r"} = $off;

                    # generate PairSet itself (using $off as eventual offset within PairPos subtable)
                    my $pairset = pack('n', $#{$r} + 1); # PairValueCount
                    foreach $t (@$r) # foreach PairValueRecord
                    {
                        $pairset .= pack('n', $t->{'MATCH'}[0]); # SecondGlyph - MATCH has only one entry
                        $pairset .= 
                            $self->out_value($t->{'ACTION'}[0], $vfmt,  $ctables, $off + length($pairset) + $base);
                        $pairset .= 
                            $self->out_value($t->{'ACTION'}[1], $vfmt2, $ctables, $off + length($pairset) + $base);
                    }
                    $off += length($pairset);
                    $pairsets .= $pairset;
                }
            }
            $out .= $pairsets;
            die "internal error: PairPos size not as calculated" if (length($out) != $off);
        } else
        {
            $out = pack('n8', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base),
                            $vfmt, $vfmt2,
                            Font::TTF::Ttopen::ref_cache($lookup->{'CLASS'}, $ctables, 8 + $base),
                            Font::TTF::Ttopen::ref_cache($lookup->{'MATCH'}[0], $ctables, 10 + $base),
                            $lookup->{'CLASS'}{'max'} + 1, $lookup->{'MATCH'}[0]{'max'} + 1);

            for ($i = 0; $i <= $lookup->{'CLASS'}{'max'}; $i++)
            {
                for ($j = 0; $j <= $lookup->{'MATCH'}[0]{'max'}; $j++)
                {
                    $out .= $self->out_value($lookup->{'RULES'}[$i][$j]{'ACTION'}[0], $vfmt, $ctables, length($out) + $base);
                    $out .= $self->out_value($lookup->{'RULES'}[$i][$j]{'ACTION'}[1], $vfmt2, $ctables, length($out) + $base);
                }
            }
        }
    } elsif ($type == 3 && $fmt == 1)
    {
        $out = pack('n3', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base),
                            $#{$lookup->{'RULES'}} + 1);
        foreach $r (@{$lookup->{'RULES'}})
        {
            $out .= pack('n2', Font::TTF::Ttopen::ref_cache($r->[0]{'ACTION'}[0], $ctables, length($out) + $base),
                            Font::TTF::Ttopen::ref_cache($r->[0]{'ACTION'}[1], $ctables, length($out) + 2 + $base));
        }
    } elsif ($type == 4 || $type == 5 || $type == 6)
    {
        my ($loc_off, $loc_t, $ltables);
        
        $out = pack('n7', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'MATCH'}[0], $ctables, 2 + $base),
                            Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 4 + $base),
                            $#{$lookup->{'RULES'}[0][0]{'ACTION'}} + 1, 12, ($#{$lookup->{'MARKS'}} + 4) << 2,
                            $#{$lookup->{'MARKS'}} + 1);
        foreach $r (@{$lookup->{'MARKS'}})
        { $out .= pack('n2', $r->[0], Font::TTF::Ttopen::ref_cache($r->[1], $mtables, length($out) + 2)); }
        push (@reftables, [$mtables, 12]);

        $loc_t = length($out);
        substr($out, 10, 2) = pack('n', $loc_t);
        $out .= pack('n', $#{$lookup->{'RULES'}} + 1);
        if ($type == 5)
        {
            $loc1 = length($out);
            $out .= pack('n*', (0) x ($#{$lookup->{'RULES'}} + 1));
        }
        $ltables = {};
        for ($i = 0; $i <= $#{$lookup->{'RULES'}}; $i++)
        {
            if ($type == 5)
            {
                $ltables = {};
                $loc_t = length($out);
                substr($out, $loc1 + ($i << 1), 2) = TTF_Pack('S', $loc_t - $loc1 + 2);
            }

            $r = $lookup->{'RULES'}[$i];
            $out .= pack('n', $#{$r} + 1) if ($type == 5);
            foreach $t (@$r)
            {
                foreach $s (@{$t->{'ACTION'}})
                { $out .= pack('n', Font::TTF::Ttopen::ref_cache($s, $ltables, length($out))); }
            }
            push (@reftables, [$ltables, $loc_t]) if ($type == 5);
        }
        push (@reftables, [$ltables, $loc_t]) unless ($type == 5);
        $out = Font::TTF::Ttopen::out_final($fh, $out, \@reftables, 1);
    } elsif ($type == 7 || $type == 8)
    { $out = $self->out_context($lookup, $fh, $type - 2, $fmt, $ctables, $out, $num, $base); }
#    push (@reftables, [$ctables, 0]);
    $out;
}
            

=head2 $t->read_value($format, $base, $lookup, $fh)

Reads a value record from the current location in the file, according to the
format given.

=cut

sub read_value
{
    my ($self, $fmt, $base, $lookup, $fh) = @_;
    my ($flag) = 1;
    my ($res) = {};
    my ($s, $i, $dat);

    $s = 0;
    for ($i = 0; $i < 12; $i++)
    {
        $s++ if ($flag & $fmt);
        $flag <<= 1;
    }

    $fh->read($dat, $s << 1);
    $flag = 1; $i = 0;
    foreach $s (qw(XPlacement YPlacement XAdvance YAdvance))
    {
        $res->{$s} = TTF_Unpack('s', substr($dat, $i++ << 1, 2)) if ($fmt & $flag);
        $flag <<= 1;
    }

    foreach $s (qw(XPlaDevice YPlaDevice XAdvDevice YAdvDevice))
    {
        if ($fmt & $flag)
        { $res->{$s} = $self->read_delta(TTF_Unpack('S', substr($dat, $i++ << 1, 2)),
                            $base, $lookup, $fh); }
        $flag <<= 1;
    }

    foreach $s (qw(XIdPlacement YIdPlacement XIdAdvance YIdAdvance))
    {
        $res->{$s} = TTF_Unpack('S', substr($dat, $i++ << 1, 2)) if ($fmt & $flag);
        $flag <<= 1;
    }
    $res;
}


=head2 $t->read_delta($offset, $base, $lookup, $fh)

Reads a delta (device table) at the given offset if it hasn not already been read.
Store the offset and item in the lookup cache ($lookup->{' CACHE'})

=cut

sub read_delta
{
    my ($self, $offset, $base, $lookup, $fh) = @_;
    my ($loc) = $fh->tell();
    my ($res, $str);

    return undef unless $offset;
    $str = sprintf("%X", $base + $offset);
    return $lookup->{' CACHE'}{$str} if defined $lookup->{' CACHE'}{$str};
    $fh->seek($base + $offset, 0);
    $res = Font::TTF::Delta->new->read($fh);
    $fh->seek($loc, 0);
    $lookup->{' CACHE'}{$str} = $res;
    return $res;
}


=head2 $t->read_anchor($offset, $base, $lookup, $fh)

Reads an Anchor table at the given offset if it has not already been read.

=cut

sub read_anchor
{
    my ($self, $offset, $base, $lookup, $fh) = @_;
    my ($loc) = $fh->tell();
    my ($res, $str);

    return undef unless $offset;
    $str = sprintf("%X", $base + $offset);
    return $lookup->{' CACHE'}{$str} if defined $lookup->{' CACHE'}{$str};
    $fh->seek($base + $offset, 0);
    $res = Font::TTF::Anchor->new->read($fh);
    $fh->seek($loc, 0);
    $lookup->{' CACHE'}{$str} = $res;
    return $res;
}


=head2 $t->fmt_value

Returns the value format for a given value record

=cut

sub fmt_value
{
    my ($self, $value) = @_;
    my ($fmt) = 0;
    my ($n);

    foreach $n (reverse qw(XPlacement YPlacement XAdvance YAdvance XPlaDevice YPlaDevice
                  XAdvDevice YAdvDevice XIdPlacement YIdPlacement XIdAdvance
                  YIdAdvance))
    {
        $fmt <<= 1;
        $fmt |= 1 if (defined $value->{$n} && (ref $value->{$n} || $value->{$n}));
    }
    $fmt;
}


=head2 $t->out_value

Returns the output string for the outputting of the value for a given format. Also
updates the offset cache for any device tables referenced.

=cut

sub out_value
{
    my ($self, $value, $fmt, $tables, $offset) = @_;
    my ($n, $flag, $out);

    $flag = 1;
    foreach $n (qw(XPlacement YPlacement XAdvance YAdvance))
    {
        $out .= pack('n', $value->{$n}) if ($flag & $fmt);
        $flag <<= 1;
    }
    foreach $n (qw(XPlaDevice YPlaDevice XAdvDevice YAdvDevice))
    {
        if ($flag & $fmt)
        {
            $out .= pack('n', Font::TTF::Ttopen::ref_cache(
                        $value->{$n}, $tables, $offset + length($out)));
        }
        $flag <<= 1;
    }
    foreach $n (qw(XIdPlacement YIdPlacement XIdAdvance YIdAdvance))
    {
        $out .= pack('n', $value->{$n}) if ($flag & $fmt);
        $flag <<= 1;
    }
    $out;
}

1;

=head1 AUTHOR

Martin Hosken L<Martin_Hosken@sil.org>. 


=head1 LICENSING

Copyright (c) 1998-2013, 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