File: Ligature.pm

package info (click to toggle)
libfont-ttf-perl 1.04-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,128 kB
  • ctags: 486
  • sloc: perl: 17,382; makefile: 10
file content (256 lines) | stat: -rw-r--r-- 7,556 bytes parent folder | download
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
package Font::TTF::Mort::Ligature;

=head1 NAME

Font::TTF::Mort::Ligature - Ligature Mort subtable for AAT

=head1 METHODS

=cut

use strict;
use vars qw(@ISA);
use Font::TTF::Utils;
use Font::TTF::AATutils;
use IO::File;

@ISA = qw(Font::TTF::Mort::Subtable);

sub new
{
    my ($class, $direction, $orientation, $subFeatureFlags) = @_;
    my ($self) = {
                    'direction'            => $direction,
                    'orientation'        => $orientation,
                    'subFeatureFlags'    => $subFeatureFlags
                };

    $class = ref($class) || $class;
    bless $self, $class;
}

=head2 $t->read

Reads the table into memory

=cut

sub read
{
    my ($self, $fh) = @_;
    my ($dat);

    my $stateTableStart = $fh->tell();
    my ($classes, $states, $entries) = AAT_read_state_table($fh, 0);
    
    $fh->seek($stateTableStart, IO::File::SEEK_SET);
    $fh->read($dat, 14);
    my ($stateSize, $classTable, $stateArray, $entryTable,
        $ligActionTable, $componentTable, $ligatureTable) = unpack("nnnnnnn", $dat);
    my $limits = [$classTable, $stateArray, $entryTable, $ligActionTable, $componentTable, $ligatureTable, $self->{'length'} - 8];
    
    my %actions;
    my $actionLists;
    foreach (@$entries) {
        my $offset = $_->{'flags'} & 0x3fff;
        $_->{'flags'} &= ~0x3fff;
        if ($offset != 0) {
            if (not defined $actions{$offset}) {
                $fh->seek($stateTableStart + $offset, IO::File::SEEK_SET);
                my $actionList;
                while (1) {
                    $fh->read($dat, 4);
                    my $action = unpack("N", $dat);
                    my ($last, $store, $component) = (($action & 0x80000000) != 0, ($action & 0xC0000000) != 0, ($action & 0x3fffffff));
                    $component -= 0x40000000 if $component > 0x1fffffff;
                    $component -= $componentTable / 2;
                    push @$actionList, { 'store' => $store, 'component' => $component };
                    last if $last;
                }
                push @$actionLists, $actionList;
                $actions{$offset} = $#$actionLists;
            }
            $_->{'actions'} = $actions{$offset};
        }
    }
    
    $self->{'componentTable'} = $componentTable;
    my $components = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $componentTable, $limits))];
    foreach (@$components) {
        $_ = ($_ - $ligatureTable) . " +" if $_ >= $ligatureTable;
    }
    $self->{'components'} = $components;
    
    $self->{'ligatureTable'} = $ligatureTable;
    $self->{'ligatures'} = [unpack("n*", AAT_read_subtable($fh, $stateTableStart, $ligatureTable, $limits))];
    
    $self->{'classes'} = $classes;
    $self->{'states'} = $states;
    $self->{'actionLists'} = $actionLists;
        
    $self;
}

=head2 $t->pack_sub($fh)

=cut

sub pack_sub
{
    my ($self) = @_;
    my ($dat);
    
    $dat .= pack("nnnnnnn", (0) x 7);    # placeholders for stateSize, classTable, stateArray, entryTable, actionLists, components, ligatures

    my $classTable = length($dat);
    my $classes = $self->{'classes'};
    $dat .= AAT_pack_classes($classes);
    
    my $stateArray = length($dat);
    my $states = $self->{'states'};
    
    my ($dat1, $stateSize, $entries) = AAT_pack_states($classes, $stateArray, $states,
            sub {
                ( $_->{'flags'} & 0xc000, $_->{'actions'} )
            }
        );
    $dat .= $dat1;
    
    my $actionLists = $self->{'actionLists'};
    my %actionListOffset;
    my $actionListDataLength = 0;
    my @actionListEntries;
    foreach (0 .. $#$entries) {
        my ($nextState, $flags, $offset) = split(/,/, $entries->[$_]);
        if ($offset eq "") {
            $offset = undef;
        }
        else {
            if (defined $actionListOffset{$offset}) {
                $offset = $actionListOffset{$offset};
            }
            else {
                $actionListOffset{$offset} = $actionListDataLength;
                my $list = $actionLists->[$offset];
                $actionListDataLength += 4 * @$list;
                push @actionListEntries, $list;
                $offset = $actionListOffset{$offset};
            }
        }
        $entries->[$_] = [ $nextState, $flags, $offset ];
    }
    my $entryTable = length($dat);
    my $ligActionLists = ($entryTable + @$entries * 4 + 3) & ~3;
    foreach (@$entries) {
        $_->[2] += $ligActionLists if defined $_->[2];
        $dat .= pack("nn", $_->[0], $_->[1] + $_->[2]);
    }
    $dat .= pack("C*", (0) x ($ligActionLists - $entryTable - @$entries * 4));
    
    die "internal error" unless length($dat) == $ligActionLists;
    
    my $componentTable = length($dat) + $actionListDataLength;
    my $actionList;
    foreach $actionList (@actionListEntries) {
        foreach (0 .. $#$actionList) {
            my $action = $actionList->[$_];
            my $val = $action->{'component'} + $componentTable / 2;
            $val += 0x40000000 if $val < 0;
            $val &= 0x3fffffff;
            $val |= 0x40000000 if $action->{'store'};
            $val |= 0x80000000 if $_ == $#$actionList;
            $dat .= pack("N", $val);
        }
    }

    die "internal error" unless length($dat) == $componentTable;

    my $components = $self->{'components'};
    my $ligatureTable = $componentTable + @$components * 2;
    $dat .= pack("n*", map { (index($_, '+') >= 0 ? $ligatureTable : 0) + $_ } @$components);
    
    my $ligatures = $self->{'ligatures'};
    $dat .= pack("n*", @$ligatures);
    
    $dat1 = pack("nnnnnnn", $stateSize, $classTable, $stateArray, $entryTable, $ligActionLists, $componentTable, $ligatureTable);
    substr($dat, 0, length($dat1)) = $dat1;

    return $dat;
}

=head2 $t->print($fh)

Prints a human-readable representation of the table

=cut

sub print
{
    my ($self, $fh) = @_;
    
    my $post = $self->post();
    
    $fh = 'STDOUT' unless defined $fh;

    $self->print_classes($fh);
    
    $fh->print("\n");
    my $states = $self->{'states'};
    foreach (0 .. $#$states) {
        $fh->printf("\t\tState %d:", $_);
        my $state = $states->[$_];
        foreach (@$state) {
            my $flags;
            $flags .= "!" if ($_->{'flags'} & 0x4000);
            $flags .= "*" if ($_->{'flags'} & 0x8000);
            $fh->printf("\t(%s%d,%s)", $flags, $_->{'nextState'}, defined $_->{'actions'} ? $_->{'actions'} : "=");
        }
        $fh->print("\n");
    }

    $fh->print("\n");
    my $actionLists = $self->{'actionLists'};
    foreach (0 .. $#$actionLists) {
        $fh->printf("\t\tList %d:\t", $_);
        my $actionList = $actionLists->[$_];
        $fh->printf("%s\n", join(", ", map { ($_->{'component'} . ($_->{'store'} ? "*" : "") ) } @$actionList));
    }

    my $ligatureTable = $self->{'ligatureTable'};

    $fh->print("\n");
    my $components = $self->{'components'};
    foreach (0 .. $#$components) {
        $fh->printf("\t\tComponent %d: %s\n", $_, $components->[$_]);
    }
    
    $fh->print("\n");
    my $ligatures = $self->{'ligatures'};
    foreach (0 .. $#$ligatures) {
        $fh->printf("\t\tLigature %d: %d [%s]\n", $_, $ligatures->[$_], $post->{'VAL'}[$ligatures->[$_]]);
    }
}

1;

=head1 BUGS

None known

=head1 AUTHOR

Jonathan Kew L<Jonathan_Kew@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