| 1 | package Font::TTF::Ttopen; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Font::TTF::Ttopen - Opentype superclass for standard Opentype lookup based tables | ||
| 6 | (GSUB and GPOS) | ||
| 7 | |||
| 8 | =head1 DESCRIPTION | ||
| 9 | |||
| 10 | Handles all the script, lang, feature, lookup stuff for a | ||
| 11 | L<Font::TTF::Gsub>/L<Font::TTF::Gpos> table leaving the class specifics to the | ||
| 12 | subclass | ||
| 13 | |||
| 14 | =head1 INSTANCE VARIABLES | ||
| 15 | |||
| 16 | The instance variables of an opentype table form a complex sub-module hierarchy. | ||
| 17 | |||
| 18 | =over 4 | ||
| 19 | |||
| 20 | =item Version | ||
| 21 | |||
| 22 | This contains the version of the table as a floating point number | ||
| 23 | |||
| 24 | =item SCRIPTS | ||
| 25 | |||
| 26 | The scripts list is a hash of script tags. Each script tag (of the form | ||
| 27 | $t->{'SCRIPTS'}{$tag}) has information below it. | ||
| 28 | |||
| 29 | =over 8 | ||
| 30 | |||
| 31 | =item OFFSET | ||
| 32 | |||
| 33 | This variable is preceded by a space and gives the offset from the start of the | ||
| 34 | table (not the table section) to the script table for this script | ||
| 35 | |||
| 36 | =item REFTAG | ||
| 37 | |||
| 38 | This variable is preceded by a space and gives a corresponding script tag to this | ||
| 39 | one such that the offsets in the file are the same. When writing, it is up to the | ||
| 40 | caller to ensure that the REFTAGs are set correctly, since these will be used to | ||
| 41 | assume that the scripts are identical. Note that REFTAG must refer to a script which | ||
| 42 | has no REFTAG of its own. | ||
| 43 | |||
| 44 | =item DEFAULT | ||
| 45 | |||
| 46 | This corresponds to the default language for this script, if there is one, and | ||
| 47 | contains the same information as an itemised language | ||
| 48 | |||
| 49 | =item LANG_TAGS | ||
| 50 | |||
| 51 | This contains an array of language tag strings (each 4 bytes) corresponding to | ||
| 52 | the languages listed by this script | ||
| 53 | |||
| 54 | =item $lang | ||
| 55 | |||
| 56 | Each language is a hash containing its information: | ||
| 57 | |||
| 58 | =over 12 | ||
| 59 | |||
| 60 | =item OFFSET | ||
| 61 | |||
| 62 | This variable is preceded by a a space and gives the offset from the start of | ||
| 63 | the whole table to the language table for this language | ||
| 64 | |||
| 65 | =item REFTAG | ||
| 66 | |||
| 67 | This variable is preceded by a space and has the same function as for the script | ||
| 68 | REFTAG, only for the languages within a script. | ||
| 69 | |||
| 70 | =item RE-ORDER | ||
| 71 | |||
| 72 | This indicates re-ordering information, and has not been set. The value should | ||
| 73 | always be 0. | ||
| 74 | |||
| 75 | =item DEFAULT | ||
| 76 | |||
| 77 | This holds the index of the default feature, if there is one, or -1 otherwise. | ||
| 78 | |||
| 79 | =item FEATURES | ||
| 80 | |||
| 81 | This is an array of feature tags for all the features enabled for this language | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | =back | ||
| 86 | |||
| 87 | =item FEATURES | ||
| 88 | |||
| 89 | The features section of instance variables corresponds to the feature table in | ||
| 90 | the opentype table. | ||
| 91 | |||
| 92 | =over 8 | ||
| 93 | |||
| 94 | =item FEAT_TAGS | ||
| 95 | |||
| 96 | This array gives the ordered list of feature tags for this table. It is used during | ||
| 97 | reading and writing for converting between feature index and feature tag. | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | The rest of the FEATURES variable is itself a hash based on the feature tag for | ||
| 102 | each feature. Each feature has the following structure: | ||
| 103 | |||
| 104 | =over 8 | ||
| 105 | |||
| 106 | =item OFFSET | ||
| 107 | |||
| 108 | This attribute is preceded by a space and gives the offset relative to the start of the whole | ||
| 109 | table of this particular feature. | ||
| 110 | |||
| 111 | =item PARMS | ||
| 112 | |||
| 113 | If FeatureParams are defined for this feature, this contains a reference to the corresponding FeatureParams object. Otherwise set to null. | ||
| 114 | |||
| 115 | =item LOOKUPS | ||
| 116 | |||
| 117 | This is an array containing indices to lookups in the LOOKUP instance variable of the table | ||
| 118 | |||
| 119 | =item INDEX | ||
| 120 | |||
| 121 | This gives the feature index for this feature and is used during reading and writing for | ||
| 122 | converting between feature tag and feature index. | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | =item LOOKUP | ||
| 127 | |||
| 128 | This variable is an array of lookups in order and is indexed via the features of a language of a | ||
| 129 | script. Each lookup contains subtables and other information: | ||
| 130 | |||
| 131 | =over 8 | ||
| 132 | |||
| 133 | =item OFFSET | ||
| 134 | |||
| 135 | This name is preceded by a space and contains the offset from the start of the table to this | ||
| 136 | particular lookup | ||
| 137 | |||
| 138 | =item TYPE | ||
| 139 | |||
| 140 | This is a subclass specific type for a lookup. It stipulates the type of lookup and hence subtables | ||
| 141 | within the lookup | ||
| 142 | |||
| 143 | =item FLAG | ||
| 144 | |||
| 145 | Holds the lookup flag bits | ||
| 146 | |||
| 147 | =item FILTER | ||
| 148 | |||
| 149 | Holds the MarkFilteringSet (that is, the index into GDEF->MARKSETS) for the lookup. | ||
| 150 | |||
| 151 | =item SUB | ||
| 152 | |||
| 153 | This holds an array of subtables which are subclass specific. Each subtable must have | ||
| 154 | an OFFSET. The other variables described here are an abstraction used in both the | ||
| 155 | GSUB and GPOS tables which are the target subclasses of this class. | ||
| 156 | |||
| 157 | =over 12 | ||
| 158 | |||
| 159 | =item OFFSET | ||
| 160 | |||
| 161 | This is preceded by a space and gives the offset relative to the start of the table for this | ||
| 162 | subtable | ||
| 163 | |||
| 164 | =item FORMAT | ||
| 165 | |||
| 166 | Gives the sub-table sub format for this GSUB subtable. It is assumed that this | ||
| 167 | value is correct when it comes time to write the subtable. | ||
| 168 | |||
| 169 | =item COVERAGE | ||
| 170 | |||
| 171 | Most lookups consist of a coverage table corresponding to the first | ||
| 172 | glyph to match. The offset of this coverage table is stored here and the coverage | ||
| 173 | table looked up against the GSUB table proper. There are two lookups | ||
| 174 | without this initial coverage table which is used to index into the RULES array. | ||
| 175 | These lookups have one element in the RULES array which is used for the whole | ||
| 176 | match. | ||
| 177 | |||
| 178 | =item RULES | ||
| 179 | |||
| 180 | The rules are a complex array. In most cases, each element of the array | ||
| 181 | corresponds to an element in the coverage table (governed by the coverage index). | ||
| 182 | In a few caess, such as when there is | ||
| 183 | no coverage table, then there is considered to be only one element in the rules | ||
| 184 | array. Each element of the array is itself an array corresponding to the | ||
| 185 | possibly multiple string matches which may follow the initial glyph. Each | ||
| 186 | element of this array is a hash with fixed keys corresponding to information | ||
| 187 | needed to match a glyph string or act upon it. Thus the RULES element is an | ||
| 188 | array of arrays of hashes which contain the following keys: | ||
| 189 | |||
| 190 | =over 16 | ||
| 191 | |||
| 192 | =item MATCH | ||
| 193 | |||
| 194 | This contains a sequence of elements held as an array. The elements may be | ||
| 195 | glyph ids (gid), class ids (cids), or offsets to coverage tables. Each element | ||
| 196 | corresponds to one glyph in the glyph string. See MATCH_TYPE for details of | ||
| 197 | how the different element types are marked. | ||
| 198 | |||
| 199 | =item PRE | ||
| 200 | |||
| 201 | This array holds the sequence of elements preceding the first match element | ||
| 202 | and has the same form as the MATCH array. | ||
| 203 | |||
| 204 | =item POST | ||
| 205 | |||
| 206 | This array holds the sequence of elements to be tested for following the match | ||
| 207 | string and is of the same form as the MATCH array. | ||
| 208 | |||
| 209 | =item ACTION | ||
| 210 | |||
| 211 | This array holds information regarding what should be done if a match is found. | ||
| 212 | The array may either hold glyph ids (which are used to replace or insert or | ||
| 213 | whatever glyphs in the glyph string) or 2 element arrays consisting of: | ||
| 214 | |||
| 215 | =over 20 | ||
| 216 | |||
| 217 | =item OFFSET | ||
| 218 | |||
| 219 | Offset from the start of the matched string that the lookup should start at | ||
| 220 | when processing the substring. | ||
| 221 | |||
| 222 | =item LOOKUP_INDEX | ||
| 223 | |||
| 224 | The index to a lookup to be acted upon on the match string. | ||
| 225 | |||
| 226 | =back | ||
| 227 | |||
| 228 | =back | ||
| 229 | |||
| 230 | =item CLASS | ||
| 231 | |||
| 232 | For those lookups which use class categories rather than glyph ids for matching | ||
| 233 | this is the offset to the class definition used to categories glyphs in the | ||
| 234 | match string. | ||
| 235 | |||
| 236 | =item PRE_CLASS | ||
| 237 | |||
| 238 | This is the offset to the class definition for the before match glyphs | ||
| 239 | |||
| 240 | =item POST_CLASS | ||
| 241 | |||
| 242 | This is the offset to the class definition for the after match glyphs. | ||
| 243 | |||
| 244 | =item ACTION_TYPE | ||
| 245 | |||
| 246 | This string holds the type of information held in the ACTION variable of a RULE. | ||
| 247 | It is subclass specific. | ||
| 248 | |||
| 249 | =item MATCH_TYPE | ||
| 250 | |||
| 251 | This holds the type of information in the MATCH array of a RULE. This is subclass | ||
| 252 | specific. | ||
| 253 | |||
| 254 | =item ADJUST | ||
| 255 | |||
| 256 | This corresponds to a single action for all items in a coverage table. The meaning | ||
| 257 | is subclass specific. | ||
| 258 | |||
| 259 | =item CACHE | ||
| 260 | |||
| 261 | This key starts with a space | ||
| 262 | |||
| 263 | A hash of other tables (such as coverage tables, classes, anchors, device tables) | ||
| 264 | based on the offset given in the subtable to that other information. | ||
| 265 | Note that the documentation is particularly | ||
| 266 | unhelpful here in that such tables are given as offsets relative to the | ||
| 267 | beginning of the subtable not the whole GSUB table. This includes those items which | ||
| 268 | are stored relative to another base within the subtable. | ||
| 269 | |||
| 270 | =back | ||
| 271 | |||
| 272 | =back | ||
| 273 | |||
| 274 | =back | ||
| 275 | |||
| 276 | =head1 METHODS | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | use Font::TTF::Table; | ||
| 281 | use Font::TTF::Utils; | ||
| 282 | use Font::TTF::Coverage; | ||
| 283 | |||
| 284 | use strict; | ||
| 285 | use vars qw(@ISA %FeatParams); | ||
| 286 | |||
| 287 | @ISA = qw(Font::TTF::Table); | ||
| 288 | |||
| 289 | %FeatParams = ( | ||
| 290 | 'ss' => 'Font::TTF::Features::Sset', | ||
| 291 | 'cv' => 'Font::TTF::Features::Cvar', | ||
| 292 | 'si' => 'Font::TTF::Features::Size', | ||
| 293 | ); | ||
| 294 | |||
| 295 | =head2 $t->read | ||
| 296 | |||
| 297 | Reads the table passing control to the subclass to handle the subtable specifics | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | sub read | ||
| 302 | { | ||
| 303 | my ($self) = @_; | ||
| 304 | $self->SUPER::read or return $self; | ||
| 305 | |||
| 306 | my ($dat, $i, $l, $oScript, $oFeat, $oLook, $tag, $nScript, $off, $dLang, $nLang, $lTag); | ||
| 307 | my ($nFeat, $oParms, $FType, $nLook, $nSub, $j, $temp, $t); | ||
| 308 | my ($fh) = $self->{' INFILE'}; | ||
| 309 | my ($moff) = $self->{' OFFSET'}; | ||
| 310 | |||
| 311 | $fh->read($dat, 10); | ||
| 312 | ($self->{'Version'}, $oScript, $oFeat, $oLook) = TTF_Unpack("vSSS", $dat); | ||
| 313 | |||
| 314 | # read features first so that in the script/lang hierarchy we can use feature tags | ||
| 315 | |||
| 316 | $fh->seek($moff + $oFeat, 0); | ||
| 317 | $fh->read($dat, 2); | ||
| 318 | $nFeat = unpack("n", $dat); | ||
| 319 | $self->{'FEATURES'} = {}; | ||
| 320 | $l = $self->{'FEATURES'}; | ||
| 321 | $fh->read($dat, 6 * $nFeat); | ||
| 322 | for ($i = 0; $i < $nFeat; $i++) | ||
| 323 | { | ||
| 324 | ($tag, $off) = unpack("a4n", substr($dat, $i * 6, 6)); | ||
| 325 | while (defined $l->{$tag}) | ||
| 326 | { | ||
| 327 | if ($tag =~ m/(.*?)\s_(\d+)$/o) | ||
| 328 | { $tag = $1 . " _" . ($2 + 1); } | ||
| 329 | else | ||
| 330 | { $tag .= " _0"; } | ||
| 331 | } | ||
| 332 | $l->{$tag}{' OFFSET'} = $off + $oFeat; | ||
| 333 | $l->{$tag}{'INDEX'} = $i; | ||
| 334 | push (@{$l->{'FEAT_TAGS'}}, $tag); | ||
| 335 | } | ||
| 336 | |||
| 337 | foreach $tag (grep {m/^.{4}(?:\s_\d+)?$/o} keys %$l) | ||
| 338 | { | ||
| 339 | $oFeat=$moff + $l->{$tag}{' OFFSET'}; | ||
| 340 | $fh->seek($oFeat, 0); | ||
| 341 | $fh->read($dat, 4); | ||
| 342 | ($oParms, $nLook) = unpack("n2", $dat); | ||
| 343 | $fh->read($dat, $nLook * 2); | ||
| 344 | $l->{$tag}{'LOOKUPS'} = [unpack("n*", $dat)]; | ||
| 345 | $l->{$tag}{'PARMS'}=""; | ||
| 346 | if ($oParms > 0) | ||
| 347 | { | ||
| 348 | $FType=$FeatParams{substr($tag,0,2)}; | ||
| 349 | if ($FType) | ||
| 350 | { | ||
| 351 | $t=$FType; | ||
| 352 | if ($^O eq "MacOS") | ||
| 353 | { $t =~ s/^|::/:/oig; } | ||
| 354 | else | ||
| 355 | { $t =~ s|::|/|oig; } | ||
| 356 | require "$t.pm"; | ||
| 357 | $l->{$tag}{'PARMS'} = $FType->new( INFILE => $fh, | ||
| 358 | OFFSET => $oFeat+$oParms); | ||
| 359 | $l->{$tag}{'PARMS'}->read; | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | } | ||
| 364 | |||
| 365 | # Now the script/lang hierarchy | ||
| 366 | |||
| 367 | $fh->seek($moff + $oScript, 0); | ||
| 368 | $fh->read($dat, 2); | ||
| 369 | $nScript = unpack("n", $dat); | ||
| 370 | $self->{'SCRIPTS'} = {}; | ||
| 371 | $l = $self->{'SCRIPTS'}; | ||
| 372 | $fh->read($dat, 6 * $nScript); | ||
| 373 | for ($i = 0; $i < $nScript; $i++) | ||
| 374 | { | ||
| 375 | ($tag, $off) = unpack("a4n", substr($dat, $i * 6, 6)); | ||
| 376 | $off += $oScript; | ||
| 377 | foreach (keys %$l) | ||
| 378 | { $l->{$tag}{' REFTAG'} = $_ if ($l->{$_}{' OFFSET'} == $off | ||
| 379 | && !defined $l->{$_}{' REFTAG'}); } | ||
| 380 | $l->{$tag}{' OFFSET'} = $off; | ||
| 381 | } | ||
| 382 | |||
| 383 | foreach $tag (keys %$l) | ||
| 384 | { | ||
| 385 | next if ($l->{$tag}{' REFTAG'}); | ||
| 386 | $fh->seek($moff + $l->{$tag}{' OFFSET'}, 0); | ||
| 387 | $fh->read($dat, 4); | ||
| 388 | ($dLang, $nLang) = unpack("n2", $dat); | ||
| 389 | $l->{$tag}{'DEFAULT'}{' OFFSET'} = | ||
| 390 | $dLang + $l->{$tag}{' OFFSET'} if $dLang; | ||
| 391 | $fh->read($dat, 6 * $nLang); | ||
| 392 | for ($i = 0; $i < $nLang; $i++) | ||
| 393 | { | ||
| 394 | ($lTag, $off) = unpack("a4n", substr($dat, $i * 6, 6)); | ||
| 395 | $off += $l->{$tag}{' OFFSET'}; | ||
| 396 | $l->{$tag}{$lTag}{' OFFSET'} = $off; | ||
| 397 | foreach (@{$l->{$tag}{'LANG_TAGS'}}, 'DEFAULT') | ||
| 398 | { $l->{$tag}{$lTag}{' REFTAG'} = $_ if ($l->{$tag}{$_}{' OFFSET'} == $off | ||
| 399 | && !$l->{$tag}{$_}{' REFTAG'}); } | ||
| 400 | push (@{$l->{$tag}{'LANG_TAGS'}}, $lTag); | ||
| 401 | } | ||
| 402 | foreach $lTag (@{$l->{$tag}{'LANG_TAGS'}}, 'DEFAULT') | ||
| 403 | { | ||
| 404 | next unless defined $l->{$tag}{$lTag}; | ||
| 405 | next if ($l->{$tag}{$lTag}{' REFTAG'}); | ||
| 406 | $fh->seek($moff + $l->{$tag}{$lTag}{' OFFSET'}, 0); | ||
| 407 | $fh->read($dat, 6); | ||
| 408 | ($l->{$tag}{$lTag}{'RE-ORDER'}, $l->{$tag}{$lTag}{'DEFAULT'}, $nFeat) | ||
| 409 | = unpack("n3", $dat); | ||
| 410 | $fh->read($dat, $nFeat * 2); | ||
| 411 | $l->{$tag}{$lTag}{'FEATURES'} = [map {$self->{'FEATURES'}{'FEAT_TAGS'}[$_]} unpack("n*", $dat)]; | ||
| 412 | } | ||
| 413 | foreach $lTag (@{$l->{$tag}{'LANG_TAGS'}}, 'DEFAULT') | ||
| 414 | { | ||
| 415 | # Make copies of referenced languages for each reference. | ||
| 416 | next unless $l->{$tag}{$lTag}{' REFTAG'}; | ||
| 417 | $temp = $l->{$tag}{$lTag}{' REFTAG'}; | ||
| 418 | $l->{$tag}{$lTag} = ©($l->{$tag}{$temp}); | ||
| 419 | $l->{$tag}{$lTag}{' REFTAG'} = $temp; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | foreach $tag (keys %$l) | ||
| 423 | { | ||
| 424 | next unless $l->{$tag}{' REFTAG'}; | ||
| 425 | $temp = $l->{$tag}{' REFTAG'}; | ||
| 426 | $l->{$tag} = ©($l->{$temp}); | ||
| 427 | $l->{$tag}{' REFTAG'} = $temp; | ||
| 428 | } | ||
| 429 | |||
| 430 | # And finally the lookups | ||
| 431 | |||
| 432 | $fh->seek($moff + $oLook, 0); | ||
| 433 | $fh->read($dat, 2); | ||
| 434 | $nLook = unpack("n", $dat); | ||
| 435 | $fh->read($dat, $nLook * 2); | ||
| 436 | $i = 0; | ||
| 437 | map { $self->{'LOOKUP'}[$i++]{' OFFSET'} = $_; } unpack("n*", $dat); | ||
| 438 | |||
| 439 | for ($i = 0; $i < $nLook; $i++) | ||
| 440 | { | ||
| 441 | $l = $self->{'LOOKUP'}[$i]; | ||
| 442 | $fh->seek($l->{' OFFSET'} + $moff + $oLook, 0); | ||
| 443 | $fh->read($dat, 6); | ||
| 444 | ($l->{'TYPE'}, $l->{'FLAG'}, $nSub) = unpack("n3", $dat); | ||
| 445 | $fh->read($dat, $nSub * 2); | ||
| 446 | my @offsets = unpack("n*", $dat); | ||
| 447 | if ($l->{'FLAG'} & 0x0010) | ||
| 448 | { | ||
| 449 | $fh->read($dat, 2); | ||
| 450 | $l->{'FILTER'} = unpack("n", $dat); | ||
| 451 | } | ||
| 452 | my $isExtension = ($l->{'TYPE'} == $self->extension()); | ||
| 453 | for ($j = 0; $j < $nSub; $j++) | ||
| 454 | { | ||
| 455 | $l->{'SUB'}[$j]{' OFFSET'} = $offsets[$j]; | ||
| 456 | $fh->seek($moff + $oLook + $l->{' OFFSET'} + $l->{'SUB'}[$j]{' OFFSET'}, 0); | ||
| 457 | if ($isExtension) | ||
| 458 | { | ||
| 459 | $fh->read($dat, 8); | ||
| 460 | my $longOff; | ||
| 461 | (undef, $l->{'TYPE'}, $longOff) = unpack("nnN", $dat); | ||
| 462 | $l->{'SUB'}[$j]{' OFFSET'} += $longOff; | ||
| 463 | $fh->seek($moff + $oLook + $l->{' OFFSET'} + $l->{'SUB'}[$j]{' OFFSET'}, 0); | ||
| 464 | } | ||
| 465 | $self->read_sub($fh, $l, $j); | ||
| 466 | } | ||
| 467 | } | ||
| 468 | return $self; | ||
| 469 | } | ||
| 470 | |||
| 471 | =head2 $t->read_sub($fh, $lookup, $index) | ||
| 472 | |||
| 473 | This stub is to allow subclasses to read subtables of lookups in a table specific manner. A | ||
| 474 | reference to the lookup is passed in along with the subtable index. The file is located at the | ||
| 475 | start of the subtable to be read | ||
| 476 | |||
| 477 | =cut | ||
| 478 | |||
| 479 | sub read_sub | ||
| 480 | { } | ||
| 481 | |||
| 482 | |||
| 483 | =head2 $t->extension() | ||
| 484 | |||
| 485 | Returns the lookup number for the extension table that allows access to 32-bit offsets. | ||
| 486 | |||
| 487 | =cut | ||
| 488 | |||
| 489 | sub extension | ||
| 490 | { } | ||
| 491 | |||
| 492 | |||
| 493 | =head2 $t->out($fh) | ||
| 494 | |||
| 495 | Writes this Opentype table to the output calling $t->out_sub for each sub table | ||
| 496 | at the appropriate point in the output. The assumption is that on entry the | ||
| 497 | number of scripts, languages, features, lookups, etc. are all resolved and | ||
| 498 | the relationships fixed. This includes a LANG_TAGS list for a script, and that all | ||
| 499 | scripts and languages in their respective dictionaries either have a REFTAG or contain | ||
| 500 | real data. | ||
| 501 | |||
| 502 | =cut | ||
| 503 | |||
| 504 | sub out | ||
| 505 | { | ||
| 506 | my ($self, $fh) = @_; | ||
| 507 | my ($i, $j, $base, $off, $tag, $t, $l, $lTag, $oScript, @script, @tags); | ||
| 508 | my ($end, $nTags, @offs, $oFeat, $oFtable, $oParms, $FType, $oLook, $nSub, $nSubs, $big, $out); | ||
| 509 | |||
| 510 | return $self->SUPER::out($fh) unless $self->{' read'}; | ||
| 511 | |||
| 512 | # First sort the features | ||
| 513 | $i = 0; | ||
| 514 | $self->{'FEATURES'}{'FEAT_TAGS'} = [sort grep {m/^.{4}(?:\s_\d+)?$/o} %{$self->{'FEATURES'}}] | ||
| 515 | if (!defined $self->{'FEATURES'}{'FEAT_TAGS'}); | ||
| 516 | foreach $t (@{$self->{'FEATURES'}{'FEAT_TAGS'}}) | ||
| 517 | { $self->{'FEATURES'}{$t}{'INDEX'} = $i++; } | ||
| 518 | |||
| 519 | $base = $fh->tell(); | ||
| 520 | $fh->print(TTF_Pack("v", $self->{'Version'})); | ||
| 521 | $fh->print(pack("n3", 10, 0, 0)); | ||
| 522 | $oScript = $fh->tell() - $base; | ||
| 523 | @script = sort grep {length($_) == 4} keys %{$self->{'SCRIPTS'}}; | ||
| 524 | $fh->print(pack("n", $#script + 1)); | ||
| 525 | foreach $t (@script) | ||
| 526 | { $fh->print(pack("a4n", $t, 0)); } | ||
| 527 | |||
| 528 | $end = $fh->tell(); | ||
| 529 | foreach $t (@script) | ||
| 530 | { | ||
| 531 | $fh->seek($end, 0); | ||
| 532 | $tag = $self->{'SCRIPTS'}{$t}; | ||
| 533 | next if ($tag->{' REFTAG'}); | ||
| 534 | $tag->{' OFFSET'} = tell($fh) - $base - $oScript; | ||
| 535 | $fh->print(pack("n2", 0, $#{$tag->{'LANG_TAGS'}} + 1)); | ||
| 536 | foreach $lTag (sort @{$tag->{'LANG_TAGS'}}) | ||
| 537 | { $fh->print(pack("a4n", $lTag, 0)); } | ||
| 538 | foreach $lTag (@{$tag->{'LANG_TAGS'}}, 'DEFAULT') | ||
| 539 | { | ||
| 540 | my ($def); | ||
| 541 | $l = $tag->{$lTag}; | ||
| 542 | next if (!defined $l || (defined $l->{' REFTAG'} && $l->{' REFTAG'} ne '')); | ||
| 543 | $l->{' OFFSET'} = $fh->tell() - $base - $oScript - $tag->{' OFFSET'}; | ||
| 544 | if (defined $l->{'DEFAULT'}) | ||
| 545 | # { $def = $self->{'FEATURES'}{$l->{'FEATURES'}[$l->{'DEFAULT'}]}{'INDEX'}; } | ||
| 546 | { $def = $l->{'DEFAULT'}; } | ||
| 547 | else | ||
| 548 | { $def = -1; } | ||
| 549 | $fh->print(pack("n*", $l->{'RE_ORDER'} || 0, $def, $#{$l->{'FEATURES'}} + 1, | ||
| 550 | map {$self->{'FEATURES'}{$_}{'INDEX'} || 0} @{$l->{'FEATURES'}})); | ||
| 551 | } | ||
| 552 | $end = $fh->tell(); | ||
| 553 | if ($tag->{'DEFAULT'}{' REFTAG'} || defined $tag->{'DEFAULT'}{'FEATURES'}) | ||
| 554 | { | ||
| 555 | $fh->seek($base + $oScript + $tag->{' OFFSET'}, 0); | ||
| 556 | if (defined $tag->{'DEFAULT'}{' REFTAG'}) | ||
| 557 | { | ||
| 558 | my ($ttag); | ||
| 559 | for ($ttag = $tag->{'DEFAULT'}{' REFTAG'}; defined $tag->{$ttag}{' REFTAG'}; $ttag = $tag->{$ttag}{' REFTAG'}) | ||
| 560 | { } | ||
| 561 | $off = $tag->{$ttag}{' OFFSET'}; | ||
| 562 | } | ||
| 563 | else | ||
| 564 | { $off = $tag->{'DEFAULT'}{' OFFSET'}; } | ||
| 565 | $fh->print(pack("n", $off)); | ||
| 566 | } | ||
| 567 | $fh->seek($base + $oScript + $tag->{' OFFSET'} + 4, 0); | ||
| 568 | foreach (sort @{$tag->{'LANG_TAGS'}}) | ||
| 569 | { | ||
| 570 | if (defined $tag->{$_}{' REFTAG'}) | ||
| 571 | { | ||
| 572 | my ($ttag); | ||
| 573 | for ($ttag = $tag->{$_}{' REFTAG'}; defined $tag->{$ttag}{' REFTAG'}; $ttag = $tag->{$ttag}{' REFTAG'}) | ||
| 574 | { } | ||
| 575 | $off = $tag->{$ttag}{' OFFSET'}; | ||
| 576 | } | ||
| 577 | else | ||
| 578 | { $off = $tag->{$_}{' OFFSET'}; } | ||
| 579 | $fh->print(pack("a4n", $_, $off)); | ||
| 580 | } | ||
| 581 | } | ||
| 582 | $fh->seek($base + $oScript + 2, 0); | ||
| 583 | foreach $t (@script) | ||
| 584 | { | ||
| 585 | $tag = $self->{'SCRIPTS'}{$t}; | ||
| 586 | $off = $tag->{' REFTAG'} ? $tag->{$tag->{' REFTAG'}}{' OFFSET'} : $tag->{' OFFSET'}; | ||
| 587 | $fh->print(pack("a4n", $t, $off)); | ||
| 588 | } | ||
| 589 | |||
| 590 | $fh->seek($end, 0); | ||
| 591 | $oFeat = $end - $base; | ||
| 592 | $nTags = $#{$self->{'FEATURES'}{'FEAT_TAGS'}} + 1; | ||
| 593 | $fh->print(pack("n", $nTags)); | ||
| 594 | $fh->print(pack("a4n", " ", 0) x $nTags); | ||
| 595 | |||
| 596 | foreach $t (@{$self->{'FEATURES'}{'FEAT_TAGS'}}) | ||
| 597 | { | ||
| 598 | $tag = $self->{'FEATURES'}{$t}; | ||
| 599 | $oFtable = tell($fh) - $base - $oFeat; | ||
| 600 | $tag->{' OFFSET'} = $oFtable; | ||
| 601 | $fh->print(pack("n*", 0, $#{$tag->{'LOOKUPS'}} + 1, @{$tag->{'LOOKUPS'}})); | ||
| 602 | if ($tag->{'PARMS'}) | ||
| 603 | { | ||
| 604 | $end = $fh->tell(); | ||
| 605 | $oParms = $end - $oFtable - $base - $oFeat; | ||
| 606 | $fh->seek($oFtable + $base + $oFeat,0); | ||
| 607 | $fh->print(pack("n",$oParms)); | ||
| 608 | $fh->seek($end,0); | ||
| 609 | $tag->{'PARMS'}->out($fh); | ||
| 610 | } | ||
| 611 | } | ||
| 612 | $end = $fh->tell(); | ||
| 613 | $fh->seek($oFeat + $base + 2, 0); | ||
| 614 | foreach $t (@{$self->{'FEATURES'}{'FEAT_TAGS'}}) | ||
| 615 | { $fh->print(pack("a4n", $t, $self->{'FEATURES'}{$t}{' OFFSET'})); } | ||
| 616 | |||
| 617 | undef $big; | ||
| 618 | $fh->seek($end, 0); | ||
| 619 | $oLook = $end - $base; | ||
| 620 | |||
| 621 | # LookupListTable (including room for offsets to LookupTables) | ||
| 622 | $nTags = $#{$self->{'LOOKUP'}} + 1; | ||
| 623 | $fh->print(pack("n", $nTags)); | ||
| 624 | $fh->print(pack("n", 0) x $nTags); | ||
| 625 | $end = $fh->tell(); # end of LookupListTable = start of Lookups | ||
| 626 | foreach $tag (@{$self->{'LOOKUP'}}) | ||
| 627 | { $nSubs += $self->num_sub($tag); } | ||
| 628 | for ($i = 0; $i < $nTags; $i++) | ||
| 629 | { | ||
| 630 | $fh->seek($end, 0); | ||
| 631 | $tag = $self->{'LOOKUP'}[$i]; | ||
| 632 | $off = $end - $base - $oLook; # BH 2004-03-04 | ||
| 633 | # Is there room, from the start of this i'th lookup, for this and the remaining | ||
| 634 | # lookups to be wrapped in extension lookups? | ||
| 635 | if (!defined $big && $off + ($nTags - $i) * 6 + $nSubs * 10 > 65535) # BH 2004-03-04 | ||
| 636 | { | ||
| 637 | # Not enough room -- need to start an extension! | ||
| 638 | my ($k, $ext); | ||
| 639 | $ext = $self->extension(); | ||
| 640 | # Must turn previous lookup into the first extension | ||
| 641 | $i--; | ||
| 642 | $tag = $self->{'LOOKUP'}[$i]; | ||
| 643 | $end = $tag->{' OFFSET'} + $base + $oLook; | ||
| 644 | $fh->seek($end, 0); | ||
| 645 | $big = $i; | ||
| 646 | # For this and the remaining lookups, build extensions lookups | ||
| 647 | for ($j = $i; $j < $nTags; $j++) | ||
| 648 | { | ||
| 649 | $tag = $self->{'LOOKUP'}[$j]; | ||
| 650 | $nSub = $self->num_sub($tag); | ||
| 651 | $tag->{' OFFSET'} = $fh->tell() - $base - $oLook; # offset to this extension lookup | ||
| 652 | # LookupTable (including actual offsets to subtables) | ||
| 653 | $fh->print(pack("nnn", $ext, $tag->{'FLAG'}, $nSub)); | ||
| 654 | $fh->print(pack("n*", map {6 + $nSub * 2 + $_ * 8 + ($tag->{'FLAG'} & 0x0010 ? 2 : 0) } (0 .. $nSub-1))); | ||
| 655 | $fh->print(pack("n", $tag->{'FILTER'})) if $tag->{'FLAG'} & 0x0010; | ||
| 656 | $tag->{' EXT_OFFSET'} = $fh->tell(); # = first extension lookup subtable | ||
| 657 | for ($k = 0; $k < $nSub; $k++) | ||
| 658 | { $fh->print(pack('nnN', 1, $tag->{'TYPE'}, 0)); } | ||
| 659 | } | ||
| 660 | |||
| 661 | $tag = $self->{'LOOKUP'}[$i]; | ||
| 662 | # Leave file positioned after all the extension lookups -- where the referenced lookups will start. | ||
| 663 | } | ||
| 664 | $tag->{' OFFSET'} = $off unless defined $big; # BH 2004-03-04 | ||
| 665 | $nSub = $self->num_sub($tag); | ||
| 666 | if (!defined $big) | ||
| 667 | { | ||
| 668 | # LookupTable (including room for subtable offsets) | ||
| 669 | $fh->print(pack("nnn", $tag->{'TYPE'}, $tag->{'FLAG'}, $nSub)); | ||
| 670 | $fh->print(pack("n", 0) x $nSub); | ||
| 671 | $fh->print(pack("n", $tag->{'FILTER'})) if $tag->{'FLAG'} & 0x0010; | ||
| 672 | } | ||
| 673 | else | ||
| 674 | { $end = $tag->{' EXT_OFFSET'}; } # Extension offsets computed relative to start of first Extension subtable -- corrected later | ||
| 675 | my (@offs, $out, @refs); | ||
| 676 | for ($j = 0; $j < $nSub; $j++) | ||
| 677 | { | ||
| 678 | my ($ctables) = {}; | ||
| 679 | my ($base) = length($out); | ||
| 680 | push(@offs, tell($fh) - $end + $base); | ||
| 681 | $out .= $self->out_sub($fh, $tag, $j, $ctables, $base); | ||
| 682 | push (@refs, [$ctables, $base]); | ||
| 683 | } | ||
| 684 | out_final($fh, $out, \@refs); | ||
| 685 | $end = $fh->tell(); | ||
| 686 | if (!defined $big) | ||
| 687 | { | ||
| 688 | $fh->seek($tag->{' OFFSET'} + $base + $oLook + 6, 0); | ||
| 689 | $fh->print(pack("n*", @offs)); | ||
| 690 | } | ||
| 691 | else | ||
| 692 | { | ||
| 693 | $fh->seek($tag->{' EXT_OFFSET'}, 0); | ||
| 694 | for ($j = 0; $j < $nSub; $j++) | ||
| 695 | { $fh->print(pack('nnN', 1, $tag->{'TYPE'}, $offs[$j] - $j * 8)); } | ||
| 696 | } | ||
| 697 | } | ||
| 698 | $fh->seek($oLook + $base + 2, 0); | ||
| 699 | $fh->print(pack("n*", map {$self->{'LOOKUP'}[$_]{' OFFSET'}} (0 .. $nTags - 1))); | ||
| 700 | $fh->seek($base + 6, 0); | ||
| 701 | $fh->print(pack('n2', $oFeat, $oLook)); | ||
| 702 | $fh->seek($end, 0); | ||
| 703 | $self; | ||
| 704 | } | ||
| 705 | |||
| 706 | |||
| 707 | =head2 $t->num_sub($lookup) | ||
| 708 | |||
| 709 | Asks the subclass to count the number of subtables for a particular lookup and to | ||
| 710 | return that value. Used in out(). | ||
| 711 | |||
| 712 | =cut | ||
| 713 | |||
| 714 | sub num_sub | ||
| 715 | { | ||
| 716 | my ($self, $lookup) = @_; | ||
| 717 | |||
| 718 | return $#{$lookup->{'SUB'}} + 1; | ||
| 719 | } | ||
| 720 | |||
| 721 | |||
| 722 | =head2 $t->out_sub($fh, $lookup, $index) | ||
| 723 | |||
| 724 | This stub is to allow subclasses to output subtables of lookups in a table specific manner. A | ||
| 725 | reference to the lookup is passed in along with the subtable index. The file is located at the | ||
| 726 | start of the subtable to be output | ||
| 727 | |||
| 728 | =cut | ||
| 729 | |||
| 730 | sub out_sub | ||
| 731 | { } | ||
| 732 | |||
| 733 | =head2 $t->dirty | ||
| 734 | |||
| 735 | Setting GPOS or GSUB dirty means that OS/2 may need updating, so set it dirty. | ||
| 736 | |||
| 737 | =cut | ||
| 738 | |||
| 739 | sub dirty | ||
| 740 | { | ||
| 741 | my ($self, $val) = @_; | ||
| 742 | my $res = $self->SUPER::dirty ($val); | ||
| 743 | $self->{' PARENT'}{'OS/2'}->read->dirty($val) if exists $self->{' PARENT'}{'OS/2'}; | ||
| 744 | $res; | ||
| 745 | } | ||
| 746 | |||
| 747 | =head2 $t->maxContext | ||
| 748 | |||
| 749 | Returns the length of the longest opentype rule in this table. | ||
| 750 | |||
| 751 | =cut | ||
| 752 | |||
| 753 | sub maxContext | ||
| 754 | { | ||
| 755 | my ($self) = @_; | ||
| 756 | |||
| 757 | # Make sure table is read | ||
| 758 | $self->read; | ||
| 759 | |||
| 760 | # Calculate my contribution to OS/2 usMaxContext | ||
| 761 | |||
| 762 | my ($maxcontext, $l, $s, $r, $m); | ||
| 763 | |||
| 764 | for $l (@{$self->{'LOOKUP'}}) # Examine each lookup | ||
| 765 | { | ||
| 766 | for $s (@{$l->{'SUB'}}) # Multiple possible subtables for this lookup | ||
| 767 | { | ||
| 768 | for $r (@{$s->{'RULES'}}) # One ruleset for each covered glyph | ||
| 769 | { | ||
| 770 | for $m (@{$r}) # Multiple possible matches for this covered glyph | ||
| 771 | { | ||
| 772 | my $lgt; | ||
| 773 | $lgt++ if exists $s->{'COVERAGE'}; # Count 1 for the coverage table if it exists | ||
| 774 | for (qw(MATCH POST)) # only Input and Lookahead sequences count (Lookbehind doesn't) -- see OT spec. | ||
| 775 | { | ||
| 776 | $lgt += @{$m->{$_}} if exists $m->{$_}; | ||
| 777 | } | ||
| 778 | $maxcontext = $lgt if $lgt > $maxcontext; | ||
| 779 | } | ||
| 780 | } | ||
| 781 | |||
| 782 | } | ||
| 783 | } | ||
| 784 | |||
| 785 | $maxcontext; | ||
| 786 | } | ||
| 787 | |||
| 788 | |||
| 789 | =head2 $t->update | ||
| 790 | |||
| 791 | Perform various housekeeping items: | ||
| 792 | |||
| 793 | For all lookups, set/clear 0x0010 bit of flag words based on 'FILTER' value. | ||
| 794 | |||
| 795 | Sort COVERAGE table and RULES for all lookups. | ||
| 796 | |||
| 797 | Unless $t->{' PARENT'}{' noharmony'} is true, update will make sure that GPOS and GSUB include | ||
| 798 | the same scripts and languages. Any added scripts and languages will have empty feature sets. | ||
| 799 | |||
| 800 | =cut | ||
| 801 | |||
| 802 | # Assumes we are called on both GSUB and GPOS. So simply ADDS scripts and languages to $self that it finds | ||
| 803 | # in the other table. | ||
| 804 | |||
| 805 | sub update | ||
| 806 | { | ||
| 807 | my ($self) = @_; | ||
| 808 | |||
| 809 | return undef unless ($self->SUPER::update); | ||
| 810 | |||
| 811 | if (defined ($self->{'LOOKUP'})) | ||
| 812 | { | ||
| 813 | |||
| 814 | # make flag word agree with mark filter setting: | ||
| 815 | for my $l (@{$self->{'LOOKUP'}}) | ||
| 816 | { | ||
| 817 | if (defined $l->{'FILTER'}) | ||
| 818 | { $l->{'FLAG'} |= 0x0010; } | ||
| 819 | else | ||
| 820 | { $l->{'FLAG'} &= ~0x0010; } | ||
| 821 | } | ||
| 822 | |||
| 823 | unless ($Font::TTF::Coverage::dontsort) | ||
| 824 | { | ||
| 825 | # Sort coverage tables and rules of all lookups by glyphID | ||
| 826 | # The lookup types that need to be sorted are: | ||
| 827 | # GSUB: 1.2 2 3 4 5.1 6.1 8 (However GSUB type 8 lookups are not yet supported by Font::TTF) | ||
| 828 | # GPOS: 1.2 2.1 3 4 5 6 7.1 8.1 | ||
| 829 | |||
| 830 | for my $l (@{$self->{'LOOKUP'}}) | ||
| 831 | { | ||
| 832 | next unless defined $l->{'SUB'}; | ||
| 833 | for my $sub (@{$l->{'SUB'}}) | ||
| 834 | { | ||
| 835 | if (defined $sub->{'COVERAGE'} and $sub->{'COVERAGE'}{'cover'} and !$sub->{'COVERAGE'}{'dontsort'}) | ||
| 836 | { | ||
| 837 | # OK! Found a lookup with coverage table: | ||
| 838 | my @map = $sub->{'COVERAGE'}->sort(); | ||
| 839 | if (defined $sub->{'RULES'} and ($sub->{'MATCH_TYPE'} =~ /g/ or $sub->{'ACTION_TYPE'} =~ /[gvea]/)) | ||
| 840 | { | ||
| 841 | # And also a RULES table which now needs to be re-sorted | ||
| 842 | my $newrules = []; | ||
| 843 | foreach (0 .. $#map) | ||
| 844 | { push @{$newrules}, $sub->{'RULES'}[$map[$_]]; } | ||
| 845 | $sub->{'RULES'} = $newrules; | ||
| 846 | } | ||
| 847 | } | ||
| 848 | |||
| 849 | # Special case for Mark positioning -- need to also sort the MarkArray | ||
| 850 | if (exists($sub->{'MARKS'}) and ref($sub->{'MATCH'}[0]) =~ /Cover/ and $sub->{'MATCH'}[0]{'cover'} and !$sub->{'MATCH'}[0]{'dontsort'}) | ||
| 851 | { | ||
| 852 | my @map = $sub->{'MATCH'}[0]->sort(); | ||
| 853 | my $newmarks = []; | ||
| 854 | foreach (0 .. $#map) | ||
| 855 | { push @{$newmarks}, $sub->{'MARKS'}[$map[$_]]; } | ||
| 856 | $sub->{'MARKS'} = $newmarks; | ||
| 857 | } | ||
| 858 | } | ||
| 859 | } | ||
| 860 | } | ||
| 861 | } | ||
| 862 | |||
| 863 | # Enforce script/lang congruence unless asked not to: | ||
| 864 | return $self if $self->{' PARENT'}{' noharmony'}; | ||
| 865 | |||
| 866 | # Find my sibling (GSUB or GPOS, depending on which I am) | ||
| 867 | my $sibling = ref($self) eq 'Font::TTF::GSUB' ? 'GPOS' : ref($self) eq 'Font::TTF::GPOS' ? 'GSUB' : undef; | ||
| 868 | return $self unless $sibling && defined $self->{' PARENT'}{$sibling}; | ||
| 869 | $sibling = $self->{' PARENT'}{$sibling}; | ||
| 870 | |||
| 871 | # Look through scripts defined in sibling: | ||
| 872 | for my $sTag (grep {length($_) == 4} keys %{$sibling->{'SCRIPTS'}}) | ||
| 873 | { | ||
| 874 | my $sibScript = $sibling->{'SCRIPTS'}{$sTag}; | ||
| 875 | $sibScript = $sibling->{$sibScript->{' REFTAG'}} if exists $sibScript->{' REFTAG'} && $sibScript->{' REFTAG'} ne ''; | ||
| 876 | |||
| 877 | $self->{'SCRIPTS'}{$sTag} = {} unless defined $self->{'SCRIPTS'}{$sTag}; # Create script if not present in $self | ||
| 878 | |||
| 879 | my $myScript = $self->{'SCRIPTS'}{$sTag}; | ||
| 880 | $myScript = $self->{$myScript->{' REFTAG'}} if exists $myScript->{' REFTAG'} && $myScript->{' REFTAG'} ne ''; | ||
| 881 | |||
| 882 | foreach my $lTag (@{$sibScript->{'LANG_TAGS'}}) | ||
| 883 | { | ||
| 884 | # Ok, found a script/lang that is in our sibling. | ||
| 885 | next if exists $myScript->{$lTag}; # Already in $self | ||
| 886 | |||
| 887 | # Need to create this lang: | ||
| 888 | push @{$myScript->{'LANG_TAGS'}}, $lTag; | ||
| 889 | $myScript->{$lTag} = { 'FEATURES' => [] }; | ||
| 890 | } | ||
| 891 | |||
| 892 | if (defined $sibScript->{'DEFAULT'} && !defined $myScript->{'DEFAULT'}) | ||
| 893 | { | ||
| 894 | # Create default lang for this script. | ||
| 895 | $myScript->{'DEFAULT'} = { 'FEATURES' => [] }; | ||
| 896 | } | ||
| 897 | } | ||
| 898 | $self; | ||
| 899 | } | ||
| 900 | |||
| 901 | =head1 Internal Functions & Methods | ||
| 902 | |||
| 903 | Most of these methods are used by subclasses for handling such things as coverage | ||
| 904 | tables. | ||
| 905 | |||
| 906 | =head2 copy($ref) | ||
| 907 | |||
| 908 | Internal function to copy the top level of a dictionary to create a new dictionary. | ||
| 909 | Only the top level is copied. | ||
| 910 | |||
| 911 | =cut | ||
| 912 | |||
| 913 | sub copy | ||
| 914 | { | ||
| 915 | my ($ref) = @_; | ||
| 916 | my ($res) = {}; | ||
| 917 | |||
| 918 | foreach (keys %$ref) | ||
| 919 | { $res->{$_} = $ref->{$_}; } | ||
| 920 | $res; | ||
| 921 | } | ||
| 922 | |||
| 923 | |||
| 924 | =head2 $t->read_cover($cover_offset, $lookup_loc, $lookup, $fh, $is_cover) | ||
| 925 | |||
| 926 | Reads a coverage table and stores the results in $lookup->{' CACHE'}, that is, if | ||
| 927 | it has not been read already. | ||
| 928 | |||
| 929 | =cut | ||
| 930 | |||
| 931 | sub read_cover | ||
| 932 | { | ||
| 933 | my ($self, $offset, $base, $lookup, $fh, $is_cover) = @_; | ||
| 934 | my ($loc) = $fh->tell(); | ||
| 935 | my ($cover, $str); | ||
| 936 | |||
| 937 | return undef unless $offset; | ||
| 938 | $str = sprintf("%X", $base + $offset); | ||
| 939 | return $lookup->{' CACHE'}{$str} if defined $lookup->{' CACHE'}{$str}; | ||
| 940 | $fh->seek($base + $offset, 0); | ||
| 941 | $cover = Font::TTF::Coverage->new($is_cover)->read($fh); | ||
| 942 | $fh->seek($loc, 0); | ||
| 943 | $lookup->{' CACHE'}{$str} = $cover; | ||
| 944 | return $cover; | ||
| 945 | } | ||
| 946 | |||
| 947 | |||
| 948 | =head2 ref_cache($obj, $cache, $offset [, $template]) | ||
| 949 | |||
| 950 | Internal function to keep track of the local positioning of subobjects such as | ||
| 951 | coverage and class definition tables, and their offsets. | ||
| 952 | What happens is that the cache is a hash of | ||
| 953 | sub objects indexed by the reference (using a string mashing of the | ||
| 954 | reference name which is valid for the duration of the reference) and holds a | ||
| 955 | list of locations in the output string which should be filled in with the | ||
| 956 | offset to the sub object when the final string is output in out_final. | ||
| 957 | |||
| 958 | Uses tricks for Tie::Refhash | ||
| 959 | |||
| 960 | =cut | ||
| 961 | |||
| 962 | sub ref_cache | ||
| 963 | { | ||
| 964 | my ($obj, $cache, $offset, $template) = @_; | ||
| 965 | |||
| 966 | return 0 unless defined $obj; | ||
| 967 | $template ||= 'n'; | ||
| 968 | unless (defined $cache->{"$obj"}) | ||
| 969 | { push (@{$cache->{''}}, $obj); } | ||
| 970 | push (@{$cache->{"$obj"}}, [$offset, $template]); | ||
| 971 | return 0; | ||
| 972 | } | ||
| 973 | |||
| 974 | |||
| 975 | =head2 out_final($fh, $out, $cache_list, $state) | ||
| 976 | |||
| 977 | Internal function to actually output everything to the file handle given that | ||
| 978 | now we know the offset to the first sub object to be output and which sub objects | ||
| 979 | are to be output and what locations need to be updated, we can now | ||
| 980 | generate everything. $cache_list is an array of two element arrays. The first element | ||
| 981 | is a cache object, the second is an offset to be subtracted from each reference | ||
| 982 | to that object made in the cache. | ||
| 983 | |||
| 984 | If $state is 1, then the output is not sent to the filehandle and the return value | ||
| 985 | is the string to be output. If $state is absent or 0 then output is not limited | ||
| 986 | by storing in a string first and the return value is ""; | ||
| 987 | |||
| 988 | =cut | ||
| 989 | |||
| 990 | sub out_final | ||
| 991 | { | ||
| 992 | my ($fh, $out, $cache_list, $state) = @_; | ||
| 993 | my ($len) = length($out || ''); | ||
| 994 | my ($base_loc) = $state ? 0 : $fh->tell(); | ||
| 995 | my ($loc, $t, $r, $s, $master_cache, $offs, $str, %vecs); | ||
| 996 | |||
| 997 | $fh->print($out || '') unless $state; # first output the current attempt | ||
| 998 | foreach $r (@$cache_list) | ||
| 999 | { | ||
| 1000 | $offs = $r->[1]; | ||
| 1001 | foreach $t (@{$r->[0]{''}}) | ||
| 1002 | { | ||
| 1003 | $str = "$t"; | ||
| 1004 | if (!defined $master_cache->{$str}) | ||
| 1005 | { | ||
| 1006 | my ($vec) = $t->signature(); | ||
| 1007 | if ($vecs{$vec}) | ||
| 1008 | { $master_cache->{$str} = $master_cache->{$vecs{$vec}}; } | ||
| 1009 | else | ||
| 1010 | { | ||
| 1011 | $vecs{$vec} = $str; | ||
| 1012 | $master_cache->{$str} = ($state ? length($out) : $fh->tell()) | ||
| 1013 | - $base_loc; | ||
| 1014 | if ($state) | ||
| 1015 | { $out .= $t->out($fh, 1); } | ||
| 1016 | else | ||
| 1017 | { $t->out($fh, 0); } | ||
| 1018 | } | ||
| 1019 | } | ||
| 1020 | foreach (@{$r->[0]{$str}}) | ||
| 1021 | { | ||
| 1022 | $s = pack($_->[1], $master_cache->{$str} - $offs); | ||
| 1023 | substr($out, $_->[0], length($s)) = $s; | ||
| 1024 | } | ||
| 1025 | } | ||
| 1026 | } | ||
| 1027 | if ($state) | ||
| 1028 | { return $out; } | ||
| 1029 | else | ||
| 1030 | { | ||
| 1031 | $loc = $fh->tell(); | ||
| 1032 | $fh->seek($base_loc, 0); | ||
| 1033 | $fh->print($out || ''); # the corrected version | ||
| 1034 | $fh->seek($loc, 0); | ||
| 1035 | } | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | |||
| 1039 | =head2 $self->read_context($lookup, $fh, $type, $fmt, $cover, $count, $loc) | ||
| 1040 | |||
| 1041 | Internal method to read context (simple and chaining context) lookup subtables for | ||
| 1042 | the GSUB and GPOS table types. The assumed values for $type correspond to those | ||
| 1043 | for GSUB, so GPOS should adjust the values upon calling. | ||
| 1044 | |||
| 1045 | =cut | ||
| 1046 | |||
| 1047 | sub read_context | ||
| 1048 | { | ||
| 1049 | my ($self, $lookup, $fh, $type, $fmt, $cover, $count, $loc) = @_; | ||
| 1050 | my ($dat, $i, $s, $t, @subst, @srec, $mcount, $scount); | ||
| 1051 | |||
| 1052 | if ($type == 5 && $fmt < 3) | ||
| 1053 | { | ||
| 1054 | if ($fmt == 2) | ||
| 1055 | { | ||
| 1056 | $fh->read($dat, 2); | ||
| 1057 | $lookup->{'CLASS'} = $self->read_cover($count, $loc, $lookup, $fh, 0); | ||
| 1058 | $count = TTF_Unpack('S', $dat); | ||
| 1059 | } | ||
| 1060 | $fh->read($dat, $count << 1); | ||
| 1061 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 1062 | { | ||
| 1063 | if ($s == 0) | ||
| 1064 | { | ||
| 1065 | push (@{$lookup->{'RULES'}}, []); | ||
| 1066 | next; | ||
| 1067 | } | ||
| 1068 | @subst = (); | ||
| 1069 | $fh->seek($loc + $s, 0); | ||
| 1070 | $fh->read($dat, 2); | ||
| 1071 | $t = TTF_Unpack('S', $dat); | ||
| 1072 | $fh->read($dat, $t << 1); | ||
| 1073 | foreach $t (TTF_Unpack('S*', $dat)) | ||
| 1074 | { | ||
| 1075 | $fh->seek($loc + $s + $t, 0); | ||
| 1076 | @srec = (); | ||
| 1077 | $fh->read($dat, 4); | ||
| 1078 | ($mcount, $scount) = TTF_Unpack('S2', $dat); | ||
| 1079 | $mcount--; | ||
| 1080 | $fh->read($dat, ($mcount << 1) + ($scount << 2)); | ||
| 1081 | for ($i = 0; $i < $scount; $i++) | ||
| 1082 | { push (@srec, [TTF_Unpack('S2', substr($dat, | ||
| 1083 | ($mcount << 1) + ($i << 2), 4))]); } | ||
| 1084 | push (@subst, {'ACTION' => [@srec], | ||
| 1085 | 'MATCH' => [TTF_Unpack('S*', | ||
| 1086 | substr($dat, 0, $mcount << 1))]}); | ||
| 1087 | } | ||
| 1088 | push (@{$lookup->{'RULES'}}, [@subst]); | ||
| 1089 | } | ||
| 1090 | $lookup->{'ACTION_TYPE'} = 'l'; | ||
| 1091 | $lookup->{'MATCH_TYPE'} = ($fmt == 2 ? 'c' : 'g'); | ||
| 1092 | } elsif ($type == 5 && $fmt == 3) | ||
| 1093 | { | ||
| 1094 | $fh->read($dat, ($cover << 1) + ($count << 2)); | ||
| 1095 | @subst = (); @srec = (); | ||
| 1096 | for ($i = 0; $i < $cover; $i++) | ||
| 1097 | { push (@subst, $self->read_cover(TTF_Unpack('S', substr($dat, $i << 1, 2)), | ||
| 1098 | $loc, $lookup, $fh, 1)); } | ||
| 1099 | for ($i = 0; $i < $count; $i++) | ||
| 1100 | { push (@srec, [TTF_Unpack('S2', substr($dat, ($count << 1) + ($i << 2), 4))]); } | ||
| 1101 | $lookup->{'RULES'} = [[{'ACTION' => [@srec], 'MATCH' => [@subst]}]]; | ||
| 1102 | $lookup->{'ACTION_TYPE'} = 'l'; | ||
| 1103 | $lookup->{'MATCH_TYPE'} = 'o'; | ||
| 1104 | } elsif ($type == 6 && $fmt < 3) | ||
| 1105 | { | ||
| 1106 | if ($fmt == 2) | ||
| 1107 | { | ||
| 1108 | $fh->read($dat, 6); | ||
| 1109 | $lookup->{'PRE_CLASS'} = $self->read_cover($count, $loc, $lookup, $fh, 0) if $count; | ||
| 1110 | ($i, $mcount, $count) = TTF_Unpack('S3', $dat); # messy: 2 classes & count | ||
| 1111 | $lookup->{'CLASS'} = $self->read_cover($i, $loc, $lookup, $fh, 0) if $i; | ||
| 1112 | $lookup->{'POST_CLASS'} = $self->read_cover($mcount, $loc, $lookup, $fh, 0) if $mcount; | ||
| 1113 | } | ||
| 1114 | $fh->read($dat, $count << 1); | ||
| 1115 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 1116 | { | ||
| 1117 | if ($s == 0) | ||
| 1118 | { | ||
| 1119 | push (@{$lookup->{'RULES'}}, []); | ||
| 1120 | next; | ||
| 1121 | } | ||
| 1122 | @subst = (); | ||
| 1123 | $fh->seek($loc + $s, 0); | ||
| 1124 | $fh->read($dat, 2); | ||
| 1125 | $t = TTF_Unpack('S', $dat); | ||
| 1126 | $fh->read($dat, $t << 1); | ||
| 1127 | foreach $i (TTF_Unpack('S*', $dat)) | ||
| 1128 | { | ||
| 1129 | $fh->seek($loc + $s + $i, 0); | ||
| 1130 | @srec = (); | ||
| 1131 | $t = {}; | ||
| 1132 | $fh->read($dat, 2); | ||
| 1133 | $mcount = TTF_Unpack('S', $dat); | ||
| 1134 | if ($mcount > 0) | ||
| 1135 | { | ||
| 1136 | $fh->read($dat, $mcount << 1); | ||
| 1137 | $t->{'PRE'} = [TTF_Unpack('S*', $dat)]; | ||
| 1138 | } | ||
| 1139 | $fh->read($dat, 2); | ||
| 1140 | $mcount = TTF_Unpack('S', $dat); | ||
| 1141 | if ($mcount > 1) | ||
| 1142 | { | ||
| 1143 | $fh->read($dat, ($mcount - 1) << 1); | ||
| 1144 | $t->{'MATCH'} = [TTF_Unpack('S*', $dat)]; | ||
| 1145 | } | ||
| 1146 | $fh->read($dat, 2); | ||
| 1147 | $mcount = TTF_Unpack('S', $dat); | ||
| 1148 | if ($mcount > 0) | ||
| 1149 | { | ||
| 1150 | $fh->read($dat, $mcount << 1); | ||
| 1151 | $t->{'POST'} = [TTF_Unpack('S*', $dat)]; | ||
| 1152 | } | ||
| 1153 | $fh->read($dat, 2); | ||
| 1154 | $scount = TTF_Unpack('S', $dat); | ||
| 1155 | $fh->read($dat, $scount << 2); | ||
| 1156 | for ($i = 0; $i < $scount; $i++) | ||
| 1157 | { push (@srec, [TTF_Unpack('S2', substr($dat, $i << 2))]); } | ||
| 1158 | $t->{'ACTION'} = [@srec]; | ||
| 1159 | push (@subst, $t); | ||
| 1160 | } | ||
| 1161 | push (@{$lookup->{'RULES'}}, [@subst]); | ||
| 1162 | } | ||
| 1163 | $lookup->{'ACTION_TYPE'} = 'l'; | ||
| 1164 | $lookup->{'MATCH_TYPE'} = ($fmt == 2 ? 'c' : 'g'); | ||
| 1165 | } elsif ($type == 6 && $fmt == 3) | ||
| 1166 | { | ||
| 1167 | $t = {}; | ||
| 1168 | unless ($cover == 0) | ||
| 1169 | { | ||
| 1170 | @subst = (); | ||
| 1171 | $fh->read($dat, $cover << 1); | ||
| 1172 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 1173 | { push(@subst, $self->read_cover($s, $loc, $lookup, $fh, 1)); } | ||
| 1174 | $t->{'PRE'} = [@subst]; | ||
| 1175 | } | ||
| 1176 | $fh->read($dat, 2); | ||
| 1177 | $count = TTF_Unpack('S', $dat); | ||
| 1178 | unless ($count == 0) | ||
| 1179 | { | ||
| 1180 | @subst = (); | ||
| 1181 | $fh->read($dat, $count << 1); | ||
| 1182 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 1183 | { push(@subst, $self->read_cover($s, $loc, $lookup, $fh, 1)); } | ||
| 1184 | $t->{'MATCH'} = [@subst]; | ||
| 1185 | } | ||
| 1186 | $fh->read($dat, 2); | ||
| 1187 | $count = TTF_Unpack('S', $dat); | ||
| 1188 | unless ($count == 0) | ||
| 1189 | { | ||
| 1190 | @subst = (); | ||
| 1191 | $fh->read($dat, $count << 1); | ||
| 1192 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 1193 | { push(@subst, $self->read_cover($s, $loc, $lookup, $fh, 1)); } | ||
| 1194 | $t->{'POST'} = [@subst]; | ||
| 1195 | } | ||
| 1196 | $fh->read($dat, 2); | ||
| 1197 | $count = TTF_Unpack('S', $dat); | ||
| 1198 | @subst = (); | ||
| 1199 | $fh->read($dat, $count << 2); | ||
| 1200 | for ($i = 0; $i < $count; $i++) | ||
| 1201 | { push (@subst, [TTF_Unpack('S2', substr($dat, $i << 2, 4))]); } | ||
| 1202 | $t->{'ACTION'} = [@subst]; | ||
| 1203 | $lookup->{'RULES'} = [[$t]]; | ||
| 1204 | $lookup->{'ACTION_TYPE'} = 'l'; | ||
| 1205 | $lookup->{'MATCH_TYPE'} = 'o'; | ||
| 1206 | } | ||
| 1207 | $lookup; | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | |||
| 1211 | =head2 $self->out_context($lookup, $fh, $type, $fmt, $ctables, $out, $num) | ||
| 1212 | |||
| 1213 | Provides shared behaviour between GSUB and GPOS tables during output for context | ||
| 1214 | (chained and simple) rules. In addition, support is provided here for type 4 GSUB | ||
| 1215 | tables, which are not used in GPOS. The value for $type corresponds to the type | ||
| 1216 | in a GSUB table so calling from GPOS should adjust the value accordingly. | ||
| 1217 | |||
| 1218 | =cut | ||
| 1219 | |||
| 1220 | sub out_context | ||
| 1221 | { | ||
| 1222 | my ($self, $lookup, $fh, $type, $fmt, $ctables, $out, $num, $base) = @_; | ||
| 1223 | my ($offc, $offd, $i, $j, $r, $t, $numd); | ||
| 1224 | |||
| 1225 | $out ||= ''; | ||
| 1226 | if (($type == 4 || $type == 5 || $type == 6) && ($fmt == 1 || $fmt == 2)) | ||
| 1227 | { | ||
| 1228 | my ($base_off); | ||
| 1229 | |||
| 1230 | if ($fmt == 1) | ||
| 1231 | { | ||
| 1232 | $out = pack("nnn", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base), | ||
| 1233 | $num); | ||
| 1234 | $base_off = 6; | ||
| 1235 | } elsif ($type == 5) | ||
| 1236 | { | ||
| 1237 | $out = pack("nnnn", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base), | ||
| 1238 | Font::TTF::Ttopen::ref_cache($lookup->{'CLASS'}, $ctables, 4 + $base), $num); | ||
| 1239 | $base_off = 8; | ||
| 1240 | } elsif ($type == 6) | ||
| 1241 | { | ||
| 1242 | $out = pack("n6", $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base), | ||
| 1243 | Font::TTF::Ttopen::ref_cache($lookup->{'PRE_CLASS'}, $ctables, 4 + $base), | ||
| 1244 | Font::TTF::Ttopen::ref_cache($lookup->{'CLASS'}, $ctables, 6 + $base), | ||
| 1245 | Font::TTF::Ttopen::ref_cache($lookup->{'POST_CLASS'}, $ctables, 8 + $base), | ||
| 1246 | $num); | ||
| 1247 | $base_off = 12; | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | $out .= pack('n*', (0) x $num); | ||
| 1251 | $offc = length($out); | ||
| 1252 | for ($i = 0; $i < $num; $i++) | ||
| 1253 | { | ||
| 1254 | $r = $lookup->{'RULES'}[$i]; | ||
| 1255 | next unless exists $r->[0]{'ACTION'}; | ||
| 1256 | $numd = $#{$r} + 1; | ||
| 1257 | substr($out, ($i << 1) + $base_off, 2) = pack('n', $offc); | ||
| 1258 | $out .= pack('n*', $numd, (0) x $numd); | ||
| 1259 | $offd = length($out) - $offc; | ||
| 1260 | for ($j = 0; $j < $numd; $j++) | ||
| 1261 | { | ||
| 1262 | substr($out, $offc + 2 + ($j << 1), 2) = pack('n', $offd); | ||
| 1263 | if ($type == 4) | ||
| 1264 | { | ||
| 1265 | $out .= pack('n*', $r->[$j]{'ACTION'}[0], $#{$r->[$j]{'MATCH'}} + 2, | ||
| 1266 | @{$r->[$j]{'MATCH'}}); | ||
| 1267 | } elsif ($type == 5) | ||
| 1268 | { | ||
| 1269 | $out .= pack('n*', $#{$r->[$j]{'MATCH'}} + 2, | ||
| 1270 | $#{$r->[$j]{'ACTION'}} + 1, | ||
| 1271 | @{$r->[$j]{'MATCH'}}); | ||
| 1272 | foreach $t (@{$r->[$j]{'ACTION'}}) | ||
| 1273 | { $out .= pack('n2', @$t); } | ||
| 1274 | } elsif ($type == 6) | ||
| 1275 | { | ||
| 1276 | $out .= pack('n*', $#{$r->[$j]{'PRE'}} + 1, @{$r->[$j]{'PRE'}}, | ||
| 1277 | $#{$r->[$j]{'MATCH'}} + 2, @{$r->[$j]{'MATCH'}}, | ||
| 1278 | $#{$r->[$j]{'POST'}} + 1, @{$r->[$j]{'POST'}}, | ||
| 1279 | $#{$r->[$j]{'ACTION'}} + 1); | ||
| 1280 | foreach $t (@{$r->[$j]{'ACTION'}}) | ||
| 1281 | { $out .= pack('n2', @$t); } | ||
| 1282 | } | ||
| 1283 | $offd = length($out) - $offc; | ||
| 1284 | } | ||
| 1285 | $offc = length($out); | ||
| 1286 | } | ||
| 1287 | } elsif ($type == 5 && $fmt == 3) | ||
| 1288 | { | ||
| 1289 | $out .= pack('n3', $fmt, $#{$lookup->{'RULES'}[0][0]{'MATCH'}} + 1, | ||
| 1290 | $#{$lookup->{'RULES'}[0][0]{'ACTION'}} + 1); | ||
| 1291 | foreach $t (@{$lookup->{'RULES'}[0][0]{'MATCH'}}) | ||
| 1292 | { $out .= pack('n', Font::TTF::Ttopen::ref_cache($t, $ctables, length($out) + $base)); } | ||
| 1293 | foreach $t (@{$lookup->{'RULES'}[0][0]{'ACTION'}}) | ||
| 1294 | { $out .= pack('n2', @$t); } | ||
| 1295 | } elsif ($type == 6 && $fmt == 3) | ||
| 1296 | { | ||
| 1297 | $r = $lookup->{'RULES'}[0][0]; | ||
| 1298 | no strict 'refs'; # temp fix - more code needed (probably "if" statements in the event 'PRE' or 'POST' are empty) | ||
| 1299 | $out .= pack('n2', $fmt, defined $r->{'PRE'} ? scalar @{$r->{'PRE'}} : 0); | ||
| 1300 | foreach $t (@{$r->{'PRE'}}) | ||
| 1301 | { $out .= pack('n', Font::TTF::Ttopen::ref_cache($t, $ctables, length($out) + $base)); } | ||
| 1302 | $out .= pack('n', defined $r->{'MATCH'} ? scalar @{$r->{'MATCH'}} : 0); | ||
| 1303 | foreach $t (@{$r->{'MATCH'}}) | ||
| 1304 | { $out .= pack('n', Font::TTF::Ttopen::ref_cache($t, $ctables, length($out) + $base)); } | ||
| 1305 | $out .= pack('n', defined $r->{'POST'} ? scalar @{$r->{'POST'}} : 0); | ||
| 1306 | foreach $t (@{$r->{'POST'}}) | ||
| 1307 | { $out .= pack('n', Font::TTF::Ttopen::ref_cache($t, $ctables, length($out) + $base)); } | ||
| 1308 | $out .= pack('n', defined $r->{'ACTION'} ? scalar @{$r->{'ACTION'}} : 0); | ||
| 1309 | foreach $t (@{$r->{'ACTION'}}) | ||
| 1310 | { $out .= pack('n2', @$t); } | ||
| 1311 | } | ||
| 1312 | $out; | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | 1; | ||
| 1316 | |||
| 1317 | |||
| 1318 | =head1 BUGS | ||
| 1319 | |||
| 1320 | =over 4 | ||
| 1321 | |||
| 1322 | =item * | ||
| 1323 | |||
| 1324 | No way to share cachable items (coverage tables, classes, anchors, device tables) | ||
| 1325 | across different lookups. The items are always output after the lookup and | ||
| 1326 | repeated if necessary. Within lookup sharing is possible. | ||
| 1327 | |||
| 1328 | =back | ||
| 1329 | |||
| 1330 | =head1 AUTHOR | ||
| 1331 | |||
| 1332 | Martin Hosken L<http://scripts.sil.org/FontUtils>. | ||
| 1333 | |||
| 1334 | |||
| 1335 | =head1 LICENSING | ||
| 1336 | |||
| 1337 | Copyright (c) 1998-2016, SIL International (http://www.sil.org) | ||
| 1338 | |||
| 1339 | This module is released under the terms of the Artistic License 2.0. | ||
| 1340 | For details, see the full text of the license in the file LICENSE. | ||
| 1341 | |||
| 1342 | |||
| 1343 | |||
| 1344 | =cut | ||
| 1345 |