| 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 offsets and lookups 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 | =item r | ||
| 48 | |||
| 49 | The action array is a list of replacement glyphs in coverage order. This ACTION_TYPE | ||
| 50 | is used only for Type 8 Reverse Chaining lookups which, by design, are single glyph | ||
| 51 | substitution. | ||
| 52 | |||
| 53 | =back | ||
| 54 | |||
| 55 | =item MATCH_TYPE | ||
| 56 | |||
| 57 | This indicates which type of information the various MATCH arrays (MATCH, PRE, | ||
| 58 | POST) hold in the rule: | ||
| 59 | |||
| 60 | =over 8 | ||
| 61 | |||
| 62 | =item g | ||
| 63 | |||
| 64 | The array holds a string of glyph ids which should match exactly | ||
| 65 | |||
| 66 | =item c | ||
| 67 | |||
| 68 | The array holds a sequence of class definitions which each glyph should | ||
| 69 | correspondingly match to | ||
| 70 | |||
| 71 | =item o | ||
| 72 | |||
| 73 | The array holds offsets to coverage tables | ||
| 74 | |||
| 75 | =back | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | =head1 CORRESPONDANCE TO LAYOUT TYPES | ||
| 80 | |||
| 81 | The following table gives the values for ACTION_TYPE and MATCH_TYPE for each | ||
| 82 | of the 12 different lookup types found in the GSUB table definition: | ||
| 83 | |||
| 84 | 1.1 1.2 2 3 4 5.1 5.2 5.3 6.1 6.2 6.3 8 | ||
| 85 | ACTION_TYPE o g g a g l l l l l l r | ||
| 86 | MATCH_TYPE g g c o g c o o | ||
| 87 | |||
| 88 | Hopefully, the rest of the uses of the variables should make sense from this | ||
| 89 | table. | ||
| 90 | |||
| 91 | =head1 METHODS | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | use strict; | ||
| 96 | use vars qw(@ISA); | ||
| 97 | use Font::TTF::Utils; | ||
| 98 | use Font::TTF::Ttopen; | ||
| 99 | |||
| 100 | @ISA = qw(Font::TTF::Ttopen); | ||
| 101 | |||
| 102 | =head2 $t->read_sub($fh, $lookup, $index) | ||
| 103 | |||
| 104 | Asked by the superclass to read in from the given file the indexth subtable from | ||
| 105 | lookup number lookup. The file is positioned ready for the read. | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub read_sub | ||
| 110 | { | ||
| 111 | my ($self, $fh, $main_lookup, $sindex) = @_; | ||
| 112 | my ($type) = $main_lookup->{'TYPE'}; | ||
| 113 | my ($loc) = $fh->tell(); | ||
| 114 | my ($lookup) = $main_lookup->{'SUB'}[$sindex]; | ||
| 115 | my ($dat, $s, @subst, $t, $fmt, $cover, $count, $mcount, $scount, $i, $gid); | ||
| 116 | my (@srec); | ||
| 117 | |||
| 118 | if ($type == 6) | ||
| 119 | { | ||
| 120 | $fh->read($dat, 4); | ||
| 121 | ($fmt, $cover) = TTF_Unpack('S2', $dat); | ||
| 122 | if ($fmt < 3) | ||
| 123 | { | ||
| 124 | $fh->read($dat, 2); | ||
| 125 | $count = TTF_Unpack('S', $dat); | ||
| 126 | } | ||
| 127 | } else | ||
| 128 | { | ||
| 129 | $fh->read($dat, 6); | ||
| 130 | ($fmt, $cover, $count) = TTF_Unpack("S3", $dat); | ||
| 131 | } | ||
| 132 | unless ($fmt == 3 && ($type == 5 || $type == 6)) | ||
| 133 | { $lookup->{'COVERAGE'} = $self->read_cover($cover, $loc, $lookup, $fh, 1); } | ||
| 134 | |||
| 135 | $lookup->{'FORMAT'} = $fmt; | ||
| 136 | if ($type == 1 && $fmt == 1) | ||
| 137 | { | ||
| 138 | $count -= 65536 if ($count > 32767); | ||
| 139 | $lookup->{'ADJUST'} = $count; | ||
| 140 | $lookup->{'ACTION_TYPE'} = 'o'; | ||
| 141 | } elsif ($type == 1 && $fmt == 2) | ||
| 142 | { | ||
| 143 | $fh->read($dat, $count << 1); | ||
| 144 | @subst = TTF_Unpack('S*', $dat); | ||
| 145 | foreach $s (@subst) | ||
| 146 | { push(@{$lookup->{'RULES'}}, [{'ACTION' => [$s]}]); } | ||
| 147 | $lookup->{'ACTION_TYPE'} = 'g'; | ||
| 148 | } elsif ($type == 2 || $type == 3) | ||
| 149 | { | ||
| 150 | $fh->read($dat, $count << 1); # number of offsets | ||
| 151 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 152 | { | ||
| 153 | $fh->seek($loc + $s, 0); | ||
| 154 | $fh->read($dat, 2); | ||
| 155 | $t = TTF_Unpack('S', $dat); | ||
| 156 | $fh->read($dat, $t << 1); | ||
| 157 | push(@{$lookup->{'RULES'}}, [{'ACTION' => [TTF_Unpack('S*', $dat)]}]); | ||
| 158 | } | ||
| 159 | $lookup->{'ACTION_TYPE'} = ($type == 2 ? 'g' : 'a'); | ||
| 160 | } elsif ($type == 4) | ||
| 161 | { | ||
| 162 | $fh->read($dat, $count << 1); | ||
| 163 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 164 | { | ||
| 165 | @subst = (); | ||
| 166 | $fh->seek($loc + $s, 0); | ||
| 167 | $fh->read($dat, 2); | ||
| 168 | $t = TTF_Unpack('S', $dat); | ||
| 169 | $fh->read($dat, $t << 1); | ||
| 170 | foreach $t (TTF_Unpack('S*', $dat)) | ||
| 171 | { | ||
| 172 | $fh->seek($loc + $s + $t, 0); | ||
| 173 | $fh->read($dat, 4); | ||
| 174 | ($gid, $mcount) = TTF_Unpack('S2', $dat); | ||
| 175 | $fh->read($dat, ($mcount - 1) << 1); | ||
| 176 | push(@subst, {'ACTION' => [$gid], 'MATCH' => [TTF_Unpack('S*', $dat)]}); | ||
| 177 | } | ||
| 178 | push(@{$lookup->{'RULES'}}, [@subst]); | ||
| 179 | } | ||
| 180 | $lookup->{'ACTION_TYPE'} = 'g'; | ||
| 181 | $lookup->{'MATCH_TYPE'} = 'g'; | ||
| 182 | } elsif ($type == 8) | ||
| 183 | { | ||
| 184 | $t = {}; | ||
| 185 | unless ($count == 0) | ||
| 186 | { | ||
| 187 | @subst = (); | ||
| 188 | $fh->read($dat, $count << 1); | ||
| 189 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 190 | { push(@subst, $self->read_cover($s, $loc, $lookup, $fh, 1)); } | ||
| 191 | $t->{'PRE'} = [@subst]; | ||
| 192 | } | ||
| 193 | $fh->read($dat, 2); | ||
| 194 | $count = TTF_Unpack('S', $dat); | ||
| 195 | unless ($count == 0) | ||
| 196 | { | ||
| 197 | @subst = (); | ||
| 198 | $fh->read($dat, $count << 1); | ||
| 199 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 200 | { push(@subst, $self->read_cover($s, $loc, $lookup, $fh, 1)); } | ||
| 201 | $t->{'POST'} = [@subst]; | ||
| 202 | } | ||
| 203 | $fh->read($dat, 2); | ||
| 204 | $count = TTF_Unpack('S', $dat); | ||
| 205 | $fh->read($dat, $count << 1); | ||
| 206 | $t->{'ACTION'} = [TTF_Unpack('S*', $dat)]; | ||
| 207 | $lookup->{'RULES'} = [[$t]]; | ||
| 208 | $lookup->{'ACTION_TYPE'} = 'r'; | ||
| 209 | $lookup->{'MATCH_TYPE'} = 'o'; | ||
| 210 | } elsif ($type == 5 || $type == 6) | ||
| 211 | { $self->read_context($lookup, $fh, $type, $fmt, $cover, $count, $loc); } | ||
| 212 | $lookup; | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | =head2 $t->extension | ||
| 217 | |||
| 218 | Returns the table type number for the extension table | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub extension | ||
| 223 | { return 7; } | ||
| 224 | |||
| 225 | |||
| 226 | =head2 $t->out_sub($fh, $lookup, $index) | ||
| 227 | |||
| 228 | Passed the filehandle to output to, suitably positioned, the lookup and subtable | ||
| 229 | index, this function outputs the subtable to $fh at that point. | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | sub out_sub | ||
| 234 | { | ||
| 235 | my ($self, $fh, $main_lookup, $index, $ctables, $base) = @_; | ||
| 236 | my ($type) = $main_lookup->{'TYPE'}; | ||
| 237 | my ($lookup) = $main_lookup->{'SUB'}[$index]; | ||
| 238 | my ($fmt) = $lookup->{'FORMAT'}; | ||
| 239 | my ($out, $r, $t, $i, $j, $offc, $offd, $numd); | ||
| 240 | my ($num) = $#{$lookup->{'RULES'}} + 1; | ||
| 241 | |||
| 242 | if ($type == 1) | ||
| 243 | { | ||
| 244 | $out = pack("nn", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base)); | ||
| 245 | if ($fmt == 1) | ||
| 246 | { $out .= pack("n", $lookup->{'ADJUST'}); } | ||
| 247 | else | ||
| 248 | { | ||
| 249 | $out .= pack("n", $num); | ||
| 250 | foreach $r (@{$lookup->{'RULES'}}) | ||
| 251 | { $out .= pack("n", $r->[0]{'ACTION'}[0]); } | ||
| 252 | } | ||
| 253 | } elsif ($type == 2 || $type == 3) | ||
| 254 | { | ||
| 255 | $out = pack("nnn", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base), | ||
| 256 | $num); | ||
| 257 | $out .= pack('n*', (0) x $num); | ||
| 258 | $offc = length($out); | ||
| 259 | for ($i = 0; $i < $num; $i++) | ||
| 260 | { | ||
| 261 | $out .= pack("n*", $#{$lookup->{'RULES'}[$i][0]{'ACTION'}} + 1, | ||
| 262 | @{$lookup->{'RULES'}[$i][0]{'ACTION'}}); | ||
| 263 | substr($out, ($i << 1) + 6, 2) = pack('n', $offc); | ||
| 264 | $offc = length($out); | ||
| 265 | } | ||
| 266 | } elsif ($type == 8) | ||
| 267 | { | ||
| 268 | $out = pack("nn", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base)); | ||
| 269 | $r = $lookup->{'RULES'}[0][0]; | ||
| 270 | $out .= pack('n', defined $r->{'PRE'} ? scalar @{$r->{'PRE'}} : 0); | ||
| 271 | foreach $t (@{$r->{'PRE'}}) | ||
| 272 | { $out .= pack('n', Font::TTF::Ttopen::ref_cache($t, $ctables, length($out) + $base)); } | ||
| 273 | $out .= pack('n', defined $r->{'POST'} ? scalar @{$r->{'POST'}} : 0); | ||
| 274 | foreach $t (@{$r->{'POST'}}) | ||
| 275 | { $out .= pack('n', Font::TTF::Ttopen::ref_cache($t, $ctables, length($out) + $base)); } | ||
| 276 | $out .= pack("n*", $#{$r->{'ACTION'}} + 1, @{$r->{'ACTION'}}); | ||
| 277 | } elsif ($type == 4 || $type == 5 || $type == 6) | ||
| 278 | { $out = $self->out_context($lookup, $fh, $type, $fmt, $ctables, $out, $num, $base); } | ||
| 279 | # Font::TTF::Ttopen::out_final($fh, $out, [[$ctables, 0]]); | ||
| 280 | $out; | ||
| 281 | } | ||
| 282 | |||
| 283 | 1; | ||
| 284 | |||
| 285 | =head1 AUTHOR | ||
| 286 | |||
| 287 | Martin Hosken L<http://scripts.sil.org/FontUtils>. | ||
| 288 | |||
| 289 | =head1 LICENSING | ||
| 290 | |||
| 291 | Copyright (c) 1998-2016, SIL International (http://www.sil.org) | ||
| 292 | |||
| 293 | This module is released under the terms of the Artistic License 2.0. | ||
| 294 | For details, see the full text of the license in the file LICENSE. | ||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | =cut | ||
| 299 |