1 package Font::TTF::GSUB;
5 Font::TTF::GSUB - Module support for the GSUB table in conjunction with TTOpen
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!
13 =head1 INSTANCE VARIABLES
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.
22 This is a string taking one of 4 values indicating the nature of the information
23 in the ACTION array of the rule:
29 The action contains a string of glyphs to replace the match string by
33 The action array contains a list of lookups and offsets to run, in order, on
38 The action array is an unordered set of optional replacements for the matched
39 glyph. The application should make the selection somehow.
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
51 This indicates which type of information the various MATCH arrays (MATCH, PRE,
52 POST) hold in the rule:
58 The array holds a string of glyph ids which should match exactly
62 The array holds a sequence of class definitions which each glyph should
63 correspondingly match to
67 The array holds offsets to coverage tables
73 =head1 CORRESPONDANCE TO LAYOUT TYPES
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:
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
82 Hopefully, the rest of the uses of the variables should make sense from this
92 use Font::TTF::Ttopen;
94 @ISA = qw(Font::TTF::Ttopen);
96 =head2 $t->read_sub($fh, $lookup, $index)
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.
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);
115 ($fmt, $cover) = TTF_Unpack('S2', $dat);
119 $count = TTF_Unpack('S', $dat);
124 ($fmt, $cover, $count) = TTF_Unpack("S3", $dat);
126 unless ($fmt == 3 && ($type == 5 || $type == 6))
127 { $lookup->{'COVERAGE'} = $self->read_cover($cover, $loc, $lookup, $fh, 1); }
129 $lookup->{'FORMAT'} = $fmt;
130 if ($type == 1 && $fmt == 1)
132 $count -= 65536 if ($count > 32767);
133 $lookup->{'ADJUST'} = $count;
134 $lookup->{'ACTION_TYPE'} = 'o';
135 } elsif ($type == 1 && $fmt == 2)
137 $fh->read($dat, $count << 1);
138 @subst = TTF_Unpack('S*', $dat);
140 { push(@{$lookup->{'RULES'}}, [{'ACTION' => [$s]}]); }
141 $lookup->{'ACTION_TYPE'} = 'g';
142 } elsif ($type == 2 || $type == 3)
144 $fh->read($dat, $count << 1); # number of offsets
145 foreach $s (TTF_Unpack('S*', $dat))
147 $fh->seek($loc + $s, 0);
149 $t = TTF_Unpack('S', $dat);
150 $fh->read($dat, $t << 1);
151 push(@{$lookup->{'RULES'}}, [{'ACTION' => [TTF_Unpack('S*', $dat)]}]);
153 $lookup->{'ACTION_TYPE'} = ($type == 2 ? 'g' : 'a');
156 $fh->read($dat, $count << 1);
157 foreach $s (TTF_Unpack('S*', $dat))
160 $fh->seek($loc + $s, 0);
162 $t = TTF_Unpack('S', $dat);
163 $fh->read($dat, $t << 1);
164 foreach $t (TTF_Unpack('S*', $dat))
166 $fh->seek($loc + $s + $t, 0);
168 ($gid, $mcount) = TTF_Unpack('S2', $dat);
169 $fh->read($dat, ($mcount - 1) << 1);
170 push(@subst, {'ACTION' => [$gid], 'MATCH' => [TTF_Unpack('S*', $dat)]});
172 push(@{$lookup->{'RULES'}}, [@subst]);
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); }
184 Returns the table type number for the extension table
192 =head2 $t->out_sub($fh, $lookup, $index)
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.
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;
210 $out = pack("nn", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base));
212 { $out .= pack("n", $lookup->{'ADJUST'}); }
215 $out .= pack("n", $num);
216 foreach $r (@{$lookup->{'RULES'}})
217 { $out .= pack("n", $r->[0]{'ACTION'}[0]); }
219 } elsif ($type == 2 || $type == 3)
221 $out = pack("nnn", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base),
223 $out .= pack('n*', (0) x $num);
224 $offc = length($out);
225 for ($i = 0; $i < $num; $i++)
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);
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]]);
240 Martin Hosken Martin_Hosken@sil.org. See L<Font::TTF::Font> for copyright and