Version bump.
[librarian.git] / librarian / font-optimizer / ext / Font-TTF / lib / Font / TTF / GSUB.pm
1 package Font::TTF::GSUB;
2
3 =head1 NAME
4
5 Font::TTF::GSUB - Module support for the GSUB table in conjunction with TTOpen
6
7 =head1 DESCRIPTION
8
9 Handles the GSUB subtables in relation to Ttopen tables. Due to the variety of
10 different lookup types, the data structures are not all that straightforward,
11 although I have tried to make life easy for myself when using this!
12
13 =head1 INSTANCE VARIABLES
14
15 The structure of a GSUB table is the same as that given in L<Font::TTF::Ttopen>.
16 Here we give some of the semantics specific to GSUB lookups.
17
18 =over 4
19
20 =item ACTION_TYPE
21
22 This is a string taking one of 4 values indicating the nature of the information
23 in the ACTION array of the rule:
24
25 =over 8
26
27 =item g
28
29 The action contains a string of glyphs to replace the match string by
30
31 =item l
32
33 The action array contains a list of lookups and offsets to run, in order, on
34 the matched string
35
36 =item a
37
38 The action array is an unordered set of optional replacements for the matched
39 glyph. The application should make the selection somehow.
40
41 =item o
42
43 The action array is empty (in fact there is no rule array for this type of
44 rule) and the ADJUST value should be added to the glyph id to find the replacement
45 glyph id value
46
47 =back
48
49 =item MATCH_TYPE
50
51 This indicates which type of information the various MATCH arrays (MATCH, PRE,
52 POST) hold in the rule:
53
54 =over 8
55
56 =item g
57
58 The array holds a string of glyph ids which should match exactly
59
60 =item c
61
62 The array holds a sequence of class definitions which each glyph should
63 correspondingly match to
64
65 =item o
66
67 The array holds offsets to coverage tables
68
69 =back
70
71 =back
72
73 =head1 CORRESPONDANCE TO LAYOUT TYPES
74
75 The following table gives the values for ACTION_TYPE and MATCH_TYPE for each
76 of the 11 different lookup types found in the GSUB table definition I have:
77
78                 1.1 1.2 2   3   4   5.1 5.2 5.3 6.1 6.2 6.3
79   ACTION_TYPE    o   g  g   a   g    l   l   l   l   l   l
80   MATCH_TYPE                    g    g   c   o   g   c   o
81
82 Hopefully, the rest of the uses of the variables should make sense from this
83 table.
84
85 =head1 METHODS
86
87 =cut
88
89 use strict;
90 use vars qw(@ISA);
91 use Font::TTF::Utils;
92 use Font::TTF::Ttopen;
93
94 @ISA = qw(Font::TTF::Ttopen);
95
96 =head2 $t->read_sub($fh, $lookup, $index)
97
98 Asked by the superclass to read in from the given file the indexth subtable from
99 lookup number lookup. The file is positioned ready for the read.
100
101 =cut
102
103 sub read_sub
104 {
105     my ($self, $fh, $main_lookup, $sindex) = @_;
106     my ($type) = $main_lookup->{'TYPE'};
107     my ($loc) = $fh->tell();
108     my ($lookup) = $main_lookup->{'SUB'}[$sindex];
109     my ($dat, $s, @subst, $t, $fmt, $cover, $count, $mcount, $scount, $i, $gid);
110     my (@srec);
111
112     if ($type == 6)
113     {
114         $fh->read($dat, 4);
115         ($fmt, $cover) = TTF_Unpack('S2', $dat);
116         if ($fmt < 3)
117         {
118             $fh->read($dat, 2);
119             $count = TTF_Unpack('S', $dat);
120         }
121     } else
122     {
123         $fh->read($dat, 6);
124         ($fmt, $cover, $count) = TTF_Unpack("S3", $dat);
125     }
126     unless ($fmt == 3 && ($type == 5 || $type == 6))
127     { $lookup->{'COVERAGE'} = $self->read_cover($cover, $loc, $lookup, $fh, 1); }
128
129     $lookup->{'FORMAT'} = $fmt;
130     if ($type == 1 && $fmt == 1)
131     {
132         $count -= 65536 if ($count > 32767);
133         $lookup->{'ADJUST'} = $count;
134         $lookup->{'ACTION_TYPE'} = 'o';
135     } elsif ($type == 1 && $fmt == 2)
136     {
137         $fh->read($dat, $count << 1);
138         @subst = TTF_Unpack('S*', $dat);
139         foreach $s (@subst)
140         { push(@{$lookup->{'RULES'}}, [{'ACTION' => [$s]}]); }
141         $lookup->{'ACTION_TYPE'} = 'g';
142     } elsif ($type == 2 || $type == 3)
143     {
144         $fh->read($dat, $count << 1);       # number of offsets
145         foreach $s (TTF_Unpack('S*', $dat))
146         {
147             $fh->seek($loc + $s, 0);
148             $fh->read($dat, 2);
149             $t = TTF_Unpack('S', $dat);
150             $fh->read($dat, $t << 1);
151             push(@{$lookup->{'RULES'}}, [{'ACTION' => [TTF_Unpack('S*', $dat)]}]);
152         }
153         $lookup->{'ACTION_TYPE'} = ($type == 2 ? 'g' : 'a');
154     } elsif ($type == 4)
155     {
156         $fh->read($dat, $count << 1);
157         foreach $s (TTF_Unpack('S*', $dat))
158         {
159             @subst = ();
160             $fh->seek($loc + $s, 0);
161             $fh->read($dat, 2);
162             $t = TTF_Unpack('S', $dat);
163             $fh->read($dat, $t << 1);
164             foreach $t (TTF_Unpack('S*', $dat))
165             {
166                 $fh->seek($loc + $s + $t, 0);
167                 $fh->read($dat, 4);
168                 ($gid, $mcount) = TTF_Unpack('S2', $dat);
169                 $fh->read($dat, ($mcount - 1) << 1);
170                 push(@subst, {'ACTION' => [$gid], 'MATCH' => [TTF_Unpack('S*', $dat)]});
171             }
172             push(@{$lookup->{'RULES'}}, [@subst]);
173         }
174         $lookup->{'ACTION_TYPE'} = 'g';
175         $lookup->{'MATCH_TYPE'} = 'g';
176     } elsif ($type == 5 || $type == 6)
177     { $self->read_context($lookup, $fh, $type, $fmt, $cover, $count, $loc); }
178     $lookup;
179 }
180
181
182 =head2 $t->extension
183
184 Returns the table type number for the extension table
185
186 =cut
187
188 sub extension
189 { return 7; }
190
191
192 =head2 $t->out_sub($fh, $lookup, $index)
193
194 Passed the filehandle to output to, suitably positioned, the lookup and subtable
195 index, this function outputs the subtable to $fh at that point.
196
197 =cut
198
199 sub out_sub
200 {
201     my ($self, $fh, $main_lookup, $index, $ctables, $base) = @_;
202     my ($type) = $main_lookup->{'TYPE'};
203     my ($lookup) = $main_lookup->{'SUB'}[$index];
204     my ($fmt) = $lookup->{'FORMAT'};
205     my ($out, $r, $t, $i, $j, $offc, $offd, $numd);
206     my ($num) = $#{$lookup->{'RULES'}} + 1;
207
208     if ($type == 1)
209     {
210         $out = pack("nn", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base));
211         if ($fmt == 1)
212         { $out .= pack("n", $lookup->{'ADJUST'}); }
213         else
214         {
215             $out .= pack("n", $num);
216             foreach $r (@{$lookup->{'RULES'}})
217             { $out .= pack("n", $r->[0]{'ACTION'}[0]); }
218         }
219     } elsif ($type == 2 || $type == 3)
220     {
221         $out = pack("nnn", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base),
222                             $num);
223         $out .= pack('n*', (0) x $num);
224         $offc = length($out);
225         for ($i = 0; $i < $num; $i++)
226         {
227             $out .= pack("n*", $#{$lookup->{'RULES'}[$i][0]{'ACTION'}} + 1,
228                                     @{$lookup->{'RULES'}[$i][0]{'ACTION'}});
229             substr($out, ($i << 1) + 6, 2) = pack('n', $offc);
230             $offc = length($out);
231         }
232     } elsif ($type == 4 || $type == 5 || $type == 6)
233     { $out = $self->out_context($lookup, $fh, $type, $fmt, $ctables, $out, $num, $base); }
234 #    Font::TTF::Ttopen::out_final($fh, $out, [[$ctables, 0]]);
235     $out;
236 }
237
238 =head1 AUTHOR
239
240 Martin Hosken Martin_Hosken@sil.org. See L<Font::TTF::Font> for copyright and
241 licensing.
242
243 =cut
244
245 1;
246