| 1 | package Font::TTF::GPOS; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Font::TTF::GPOS - Support for Opentype GPOS tables in conjunction with TTOpen | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | The GPOS table is one of the most complicated tables in the TTF spec and the | ||
| 10 | corresponding data structure abstraction is also not trivial. While much of the | ||
| 11 | structure of a GPOS is shared with a GSUB table via the L<Font::TTF::Ttopen> | ||
| 12 | |||
| 13 | =head1 INSTANCE VARIABLES | ||
| 14 | |||
| 15 | Here we describe the additions and lookup specific information for GPOS tables. | ||
| 16 | Unfortunately there is no one abstraction which seems to work comfortable for | ||
| 17 | all GPOS tables, so we will also examine how the variables are used for different | ||
| 18 | lookup types. | ||
| 19 | |||
| 20 | The following are the values allowed in the ACTION_TYPE and MATCH_TYPE variables: | ||
| 21 | |||
| 22 | =over 4 | ||
| 23 | |||
| 24 | =item ACTION_TYPE | ||
| 25 | |||
| 26 | This can take any of the following values | ||
| 27 | |||
| 28 | =over 8 | ||
| 29 | |||
| 30 | =item a | ||
| 31 | |||
| 32 | The ACTION is an array of anchor tables | ||
| 33 | |||
| 34 | =item o | ||
| 35 | |||
| 36 | Offset. There is no RULE array. The ADJUST variable contains a value record (see | ||
| 37 | later in this description) | ||
| 38 | |||
| 39 | =item v | ||
| 40 | |||
| 41 | The ACTION is a value record. | ||
| 42 | |||
| 43 | =item p | ||
| 44 | |||
| 45 | Pair adjustment. The ACTION contains an array of two value records for the matched | ||
| 46 | two glyphs. | ||
| 47 | |||
| 48 | =item e | ||
| 49 | |||
| 50 | Exit and Entry records. The ACTION contains an array of two anchors corresponding | ||
| 51 | to the exit and entry anchors for the glyph. | ||
| 52 | |||
| 53 | =item l | ||
| 54 | |||
| 55 | Indicates a lookup based contextual rule as per the GSUB table. | ||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | =item MATCH_TYPE | ||
| 60 | |||
| 61 | This can take any of the following values | ||
| 62 | |||
| 63 | =over 8 | ||
| 64 | |||
| 65 | =item g | ||
| 66 | |||
| 67 | A glyph array | ||
| 68 | |||
| 69 | =item c | ||
| 70 | |||
| 71 | An array of class values | ||
| 72 | |||
| 73 | =item o | ||
| 74 | |||
| 75 | An array of coverage tables | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | =back | ||
| 80 | |||
| 81 | The following variables are added for Attachment Positioning Subtables: | ||
| 82 | |||
| 83 | =over 4 | ||
| 84 | |||
| 85 | =item MATCH | ||
| 86 | |||
| 87 | This contains an array of glyphs to match against for all RULES. It is much like | ||
| 88 | having the same MATCH string in all RULES. In the cases it is used so far, it only | ||
| 89 | ever contains one element. | ||
| 90 | |||
| 91 | =item MARKS | ||
| 92 | |||
| 93 | This contains a Mark array consisting of each element being a subarray of two | ||
| 94 | elements: | ||
| 95 | |||
| 96 | =over 8 | ||
| 97 | |||
| 98 | =item CLASS | ||
| 99 | |||
| 100 | The class that this mark uses on its base | ||
| 101 | |||
| 102 | =item ANCHOR | ||
| 103 | |||
| 104 | The anchor with which to attach this mark glyph | ||
| 105 | |||
| 106 | =back | ||
| 107 | |||
| 108 | The base table for mark to base, ligature or mark attachment positioning is | ||
| 109 | structured with the ACTION containing an array of anchors corresponding to each | ||
| 110 | attachment class. For ligatures, there is more than one RULE in the RULE array | ||
| 111 | corresponding to each glyph in the coverage table. | ||
| 112 | |||
| 113 | =back | ||
| 114 | |||
| 115 | Other variables which are provided for informational purposes are: | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item VFMT | ||
| 120 | |||
| 121 | Value format for the adjustment of the glyph matched by the coverage table. | ||
| 122 | |||
| 123 | =item VFMT2 | ||
| 124 | |||
| 125 | Value format used in pair adjustment for the second glyph in the pair | ||
| 126 | |||
| 127 | =back | ||
| 128 | |||
| 129 | =head2 Value Records | ||
| 130 | |||
| 131 | There is a subtype used in GPOS tables called a value record. It is used to adjust | ||
| 132 | the position of a glyph from its default position. The value record is variable | ||
| 133 | length with a bitfield at the beginning to indicate which of the following | ||
| 134 | entries are included. The bitfield is not stored since it is recalculated at | ||
| 135 | write time. | ||
| 136 | |||
| 137 | =over 4 | ||
| 138 | |||
| 139 | =item XPlacement | ||
| 140 | |||
| 141 | Horizontal adjustment for placement (not affecting other unattached glyphs) | ||
| 142 | |||
| 143 | =item YPlacement | ||
| 144 | |||
| 145 | Vertical adjustment for placement (not affecting other unattached glyphs) | ||
| 146 | |||
| 147 | =item XAdvance | ||
| 148 | |||
| 149 | Adjust the advance width glyph (used only in horizontal writing systems) | ||
| 150 | |||
| 151 | =item YAdvance | ||
| 152 | |||
| 153 | Adjust the vertical advance (used only in vertical writing systems) | ||
| 154 | |||
| 155 | =item XPlaDevice | ||
| 156 | |||
| 157 | Device table for device specific adjustment of horizontal placement | ||
| 158 | |||
| 159 | =item YPlaDevice | ||
| 160 | |||
| 161 | Device table for device specific adjustment of vertical placement | ||
| 162 | |||
| 163 | =item XAdvDevice | ||
| 164 | |||
| 165 | Device table for device specific adjustment of horizontal advance | ||
| 166 | |||
| 167 | =item YAdDevice | ||
| 168 | |||
| 169 | Device table for device specific adjustment of vertical advance | ||
| 170 | |||
| 171 | =item XIdPlacement | ||
| 172 | |||
| 173 | Horizontal placement metric id (for Multiple Master fonts - but that is all I know!) | ||
| 174 | |||
| 175 | =item YIdPlacement | ||
| 176 | |||
| 177 | Vertical placement metric id | ||
| 178 | |||
| 179 | =item XIdAdvance | ||
| 180 | |||
| 181 | Horizontal advance metric id | ||
| 182 | |||
| 183 | =item YIdAdvance | ||
| 184 | |||
| 185 | Vertical advance metric id | ||
| 186 | |||
| 187 | =back | ||
| 188 | |||
| 189 | =head1 CORRESPONDANCE TO LAYOUT TYPES | ||
| 190 | |||
| 191 | Here is what is stored in the ACTION_TYPE and MATCH_TYPE for each of the known | ||
| 192 | GPOS subtable types: | ||
| 193 | |||
| 194 | 1.1 1.2 2.1 2.2 3 4 5 6 7.1 7.2 7.3 8.1 8.2 8.3 | ||
| 195 | ACTION_TYPE o v p p e a a a l l l l l l | ||
| 196 | MATCH_TYPE g c g c o g c o | ||
| 197 | |||
| 198 | |||
| 199 | =head1 METHODS | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | use strict; | ||
| 204 | use Font::TTF::Ttopen; | ||
| 205 | use Font::TTF::Delta; | ||
| 206 | use Font::TTF::Anchor; | ||
| 207 | use Font::TTF::Utils; | ||
| 208 | use vars qw(@ISA); | ||
| 209 | |||
| 210 | @ISA = qw(Font::TTF::Ttopen); | ||
| 211 | |||
| 212 | |||
| 213 | =head2 read_sub | ||
| 214 | |||
| 215 | Reads the subtable into the data structures | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | sub read_sub | ||
| 220 | { | ||
| 221 | my ($self, $fh, $main_lookup, $sindex) = @_; | ||
| 222 | my ($type) = $main_lookup->{'TYPE'}; | ||
| 223 | my ($loc) = $fh->tell(); | ||
| 224 | my ($lookup) = $main_lookup->{'SUB'}[$sindex]; | ||
| 225 | my ($dat, $mcount, $scount, $i, $j, $count, $fmt, $fmt2, $cover, $srec, $subst); | ||
| 226 | my ($c1, $c2, $s, $moff, $boff); | ||
| 227 | |||
| 228 | |||
| 229 | if ($type == 8) | ||
| 230 | { | ||
| 231 | $fh->read($dat, 4); | ||
| 232 | ($fmt, $cover) = TTF_Unpack('S2', $dat); | ||
| 233 | if ($fmt < 3) | ||
| 234 | { | ||
| 235 | $fh->read($dat, 2); | ||
| 236 | $count = TTF_Unpack('S', $dat); | ||
| 237 | } | ||
| 238 | } else | ||
| 239 | { | ||
| 240 | $fh->read($dat, 6); | ||
| 241 | ($fmt, $cover, $count) = TTF_Unpack("S3", $dat); | ||
| 242 | } | ||
| 243 | unless ($fmt == 3 && ($type == 7 || $type == 8)) | ||
| 244 | { $lookup->{'COVERAGE'} = $self->read_cover($cover, $loc, $lookup, $fh, 1); } | ||
| 245 | |||
| 246 | $lookup->{'FORMAT'} = $fmt; | ||
| 247 | if ($type == 1 && $fmt == 1) | ||
| 248 | { | ||
| 249 | $lookup->{'VFMT'} = $count; | ||
| 250 | $lookup->{'ADJUST'} = $self->read_value($count, $loc, $lookup, $fh); | ||
| 251 | $lookup->{'ACTION_TYPE'} = 'o'; | ||
| 252 | } elsif ($type == 1 && $fmt == 2) | ||
| 253 | { | ||
| 254 | $lookup->{'VFMT'} = $count; | ||
| 255 | $fh->read($dat, 2); | ||
| 256 | $mcount = unpack('n', $dat); | ||
| 257 | for ($i = 0; $i < $mcount; $i++) | ||
| 258 | { push (@{$lookup->{'RULES'}}, [{'ACTION' => | ||
| 259 | [$self->read_value($count, $loc, $lookup, $fh)]}]); } | ||
| 260 | $lookup->{'ACTION_TYPE'} = 'v'; | ||
| 261 | } elsif ($type == 2 && $fmt == 1) | ||
| 262 | { | ||
| 263 | $lookup->{'VFMT'} = $count; | ||
| 264 | $fh->read($dat, 4); | ||
| 265 | ($fmt2, $mcount) = unpack('n2', $dat); | ||
| 266 | $lookup->{'VFMT2'} = $fmt2; | ||
| 267 | $fh->read($dat, $mcount << 1); | ||
| 268 | foreach $s (unpack('n*', $dat)) | ||
| 269 | { | ||
| 270 | $fh->seek($loc + $s, 0); | ||
| 271 | $fh->read($dat, 2); | ||
| 272 | $scount = TTF_Unpack('S', $dat); | ||
| 273 | $subst = []; | ||
| 274 | for ($i = 0; $i < $scount; $i++) | ||
| 275 | { | ||
| 276 | $srec = {}; | ||
| 277 | $fh->read($dat, 2); | ||
| 278 | $srec->{'MATCH'} = [TTF_Unpack('S', $dat)]; | ||
| 279 | $srec->{'ACTION'} = [$self->read_value($count, $loc, $lookup, $fh), | ||
| 280 | $self->read_value($fmt2, $loc, $lookup, $fh)]; | ||
| 281 | push (@$subst, $srec); | ||
| 282 | } | ||
| 283 | push (@{$lookup->{'RULES'}}, $subst); | ||
| 284 | } | ||
| 285 | $lookup->{'ACTION_TYPE'} = 'p'; | ||
| 286 | $lookup->{'MATCH_TYPE'} = 'g'; | ||
| 287 | } elsif ($type == 2 && $fmt == 2) | ||
| 288 | { | ||
| 289 | $fh->read($dat, 10); | ||
| 290 | ($lookup->{'VFMT2'}, $c1, $c2, $mcount, $scount) = TTF_Unpack('S*', $dat); | ||
| 291 | $lookup->{'CLASS'} = $self->read_cover($c1, $loc, $lookup, $fh, 0); | ||
| 292 | $lookup->{'MATCH'} = [$self->read_cover($c2, $loc, $lookup, $fh, 0)]; | ||
| 293 | $lookup->{'VFMT'} = $count; | ||
| 294 | for ($i = 0; $i < $mcount; $i++) | ||
| 295 | { | ||
| 296 | $subst = []; | ||
| 297 | for ($j = 0; $j < $scount; $j++) | ||
| 298 | { | ||
| 299 | $srec = {}; | ||
| 300 | $srec->{'ACTION'} = [$self->read_value($lookup->{'VFMT'}, $loc, $lookup, $fh), | ||
| 301 | $self->read_value($lookup->{'VFMT2'}, $loc, $lookup, $fh)]; | ||
| 302 | push (@$subst, $srec); | ||
| 303 | } | ||
| 304 | push (@{$lookup->{'RULES'}}, $subst); | ||
| 305 | } | ||
| 306 | $lookup->{'ACTION_TYPE'} = 'p'; | ||
| 307 | $lookup->{'MATCH_TYPE'} = 'c'; | ||
| 308 | } elsif ($type == 3 && $fmt == 1) | ||
| 309 | { | ||
| 310 | $fh->read($dat, $count << 2); | ||
| 311 | for ($i = 0; $i < $count; $i++) | ||
| 312 | { push (@{$lookup->{'RULES'}}, [{'ACTION' => | ||
| 313 | [$self->read_anchor(TTF_Unpack('S', substr($dat, $i << 2, 2)), | ||
| 314 | $loc, $lookup, $fh), | ||
| 315 | $self->read_anchor(TTF_Unpack('S', substr($dat, ($i << 2) + 2, 2)), | ||
| 316 | $loc, $lookup, $fh)]}]); } | ||
| 317 | $lookup->{'ACTION_TYPE'} = 'e'; | ||
| 318 | } elsif ($type == 4 || $type == 5 || $type == 6) | ||
| 319 | { | ||
| 320 | my (@offs, $mloc, $thisloc, $ncomp, $k); | ||
| 321 | |||
| 322 | $lookup->{'MATCH'} = [$lookup->{'COVERAGE'}]; # Attaching mark | ||
| 323 | $lookup->{'COVERAGE'} = $self->read_cover($count, $loc, $lookup, $fh, 1); # base/lig/mark being attached to | ||
| 324 | $fh->read($dat, 6); | ||
| 325 | ($mcount, $moff, $boff) = TTF_Unpack('S*', $dat); | ||
| 326 | # Read MarkArray | ||
| 327 | $fh->seek($loc + $moff, 0); | ||
| 328 | $fh->read($dat, 2); | ||
| 329 | $count = TTF_Unpack('S', $dat); | ||
| 330 | for ($i = 0; $i < $count; $i++) | ||
| 331 | { | ||
| 332 | $fh->read($dat, 4); | ||
| 333 | push (@{$lookup->{'MARKS'}}, [TTF_Unpack('S', $dat), | ||
| 334 | $self->read_anchor(TTF_Unpack('S', substr($dat, 2, 2)) + $moff, | ||
| 335 | $loc, $lookup, $fh)]); | ||
| 336 | } | ||
| 337 | # Read BaseArray/LigatureArray/Mark2Array | ||
| 338 | $fh->seek($loc + $boff, 0); | ||
| 339 | $fh->read($dat, 2); | ||
| 340 | $count = TTF_Unpack('S', $dat); | ||
| 341 | $mloc = $fh->tell() - 2; | ||
| 342 | $thisloc = $mloc; | ||
| 343 | if ($type == 5) | ||
| 344 | { | ||
| 345 | $fh->read($dat, $count << 1); | ||
| 346 | @offs = TTF_Unpack('S*', $dat); | ||
| 347 | } | ||
| 348 | for ($i = 0; $i < $count; $i++) | ||
| 349 | { | ||
| 350 | if ($type == 5) | ||
| 351 | { | ||
| 352 | $thisloc = $mloc + $offs[$i]; | ||
| 353 | $fh->seek($thisloc, 0); | ||
| 354 | $fh->read($dat, 2); | ||
| 355 | $ncomp = TTF_Unpack('S', $dat); | ||
| 356 | } else | ||
| 357 | { $ncomp = 1; } | ||
| 358 | for ($j = 0; $j < $ncomp; $j++) | ||
| 359 | { | ||
| 360 | $subst = []; | ||
| 361 | $fh->read($dat, $mcount << 1); | ||
| 362 | for ($k = 0; $k < $mcount; $k++) | ||
| 363 | { push (@$subst, $self->read_anchor(TTF_Unpack('S', substr($dat, $k << 1, 2)), | ||
| 364 | $thisloc, $lookup, $fh)); } | ||
| 365 | |||
| 366 | push (@{$lookup->{'RULES'}[$i]}, {'ACTION' => $subst}); | ||
| 367 | } | ||
| 368 | } | ||
| 369 | $lookup->{'ACTION_TYPE'} = 'a'; | ||
| 370 | } elsif ($type == 7 || $type == 8) | ||
| 371 | { $self->read_context($lookup, $fh, $type - 2, $fmt, $cover, $count, $loc); } | ||
| 372 | $lookup; | ||
| 373 | } | ||
| 374 | |||
| 375 | |||
| 376 | =head2 $t->extension | ||
| 377 | |||
| 378 | Returns the table type number for the extension table | ||
| 379 | |||
| 380 | =cut | ||
| 381 | |||
| 382 | sub extension | ||
| 383 | { return 9; } | ||
| 384 | |||
| 385 | |||
| 386 | =head2 $t->out_sub | ||
| 387 | |||
| 388 | Outputs the subtable to the given filehandle | ||
| 389 | |||
| 390 | =cut | ||
| 391 | |||
| 392 | sub out_sub | ||
| 393 | { | ||
| 394 | my ($self, $fh, $main_lookup, $index, $ctables, $base) = @_; | ||
| 395 | my ($type) = $main_lookup->{'TYPE'}; | ||
| 396 | my ($lookup) = $main_lookup->{'SUB'}[$index]; | ||
| 397 | my ($fmt) = $lookup->{'FORMAT'}; | ||
| 398 | my ($out, $r, $s, $t, $i, $j, $vfmt, $vfmt2, $loc1); | ||
| 399 | my ($num) = $#{$lookup->{'RULES'}} + 1; | ||
| 400 | my ($mtables) = {}; | ||
| 401 | my (@reftables); | ||
| 402 | |||
| 403 | if ($type == 1 && $fmt == 1) | ||
| 404 | { | ||
| 405 | $out = pack('n2', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base)); | ||
| 406 | $vfmt = $self->fmt_value($lookup->{'ADJUST'}); | ||
| 407 | $out .= pack('n', $vfmt) . $self->out_value($lookup->{'ADJUST'}, $vfmt, $ctables, 6 + $base); | ||
| 408 | } elsif ($type == 1 && $fmt == 2) | ||
| 409 | { | ||
| 410 | $vfmt = 0; | ||
| 411 | foreach $r (@{$lookup->{'RULES'}}) | ||
| 412 | { $vfmt |= $self->fmt_value($r->[0]{'ACTION'}[0]); } | ||
| 413 | $out = pack('n4', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base), | ||
| 414 | $vfmt, $#{$lookup->{'RULES'}} + 1); | ||
| 415 | foreach $r (@{$lookup->{'RULES'}}) | ||
| 416 | { $out .= $self->out_value($r->[0]{'ACTION'}[0], $vfmt, $ctables, length($out) + $base); } | ||
| 417 | } elsif ($type == 2 && $fmt < 3) | ||
| 418 | { | ||
| 419 | $vfmt = 0; | ||
| 420 | $vfmt2 = 0; | ||
| 421 | foreach $r (@{$lookup->{'RULES'}}) | ||
| 422 | { | ||
| 423 | foreach $t (@$r) | ||
| 424 | { | ||
| 425 | $vfmt |= $self->fmt_value($t->{'ACTION'}[0]); | ||
| 426 | $vfmt2 |= $self->fmt_value($t->{'ACTION'}[1]); | ||
| 427 | } | ||
| 428 | } | ||
| 429 | if ($fmt == 1) | ||
| 430 | { | ||
| 431 | # start PairPosFormat1 subtable | ||
| 432 | $out = pack('n5', | ||
| 433 | $fmt, | ||
| 434 | Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base), | ||
| 435 | $vfmt, | ||
| 436 | $vfmt2, | ||
| 437 | $#{$lookup->{'RULES'}} + 1); # PairSetCount | ||
| 438 | my $off = 0; | ||
| 439 | $off += length($out); | ||
| 440 | $off += 2 * ($#{$lookup->{'RULES'}} + 1); # there will be PairSetCount offsets here | ||
| 441 | my $pairsets = ''; | ||
| 442 | my (%cache); | ||
| 443 | foreach $r (@{$lookup->{'RULES'}}) # foreach PairSet table | ||
| 444 | { | ||
| 445 | # write offset to this PairSet at end of PairPosFormat1 table | ||
| 446 | if (defined $cache{"$r"}) | ||
| 447 | { $out .= pack('n', $cache{"$r"}); } | ||
| 448 | else | ||
| 449 | { | ||
| 450 | $out .= pack('n', $off); | ||
| 451 | $cache{"$r"} = $off; | ||
| 452 | |||
| 453 | # generate PairSet itself (using $off as eventual offset within PairPos subtable) | ||
| 454 | my $pairset = pack('n', $#{$r} + 1); # PairValueCount | ||
| 455 | foreach $t (@$r) # foreach PairValueRecord | ||
| 456 | { | ||
| 457 | $pairset .= pack('n', $t->{'MATCH'}[0]); # SecondGlyph - MATCH has only one entry | ||
| 458 | $pairset .= | ||
| 459 | $self->out_value($t->{'ACTION'}[0], $vfmt, $ctables, $off + length($pairset) + $base); | ||
| 460 | $pairset .= | ||
| 461 | $self->out_value($t->{'ACTION'}[1], $vfmt2, $ctables, $off + length($pairset) + $base); | ||
| 462 | } | ||
| 463 | $off += length($pairset); | ||
| 464 | $pairsets .= $pairset; | ||
| 465 | } | ||
| 466 | } | ||
| 467 | $out .= $pairsets; | ||
| 468 | die "internal error: PairPos size not as calculated" if (length($out) != $off); | ||
| 469 | } else | ||
| 470 | { | ||
| 471 | $out = pack('n8', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base), | ||
| 472 | $vfmt, $vfmt2, | ||
| 473 | Font::TTF::Ttopen::ref_cache($lookup->{'CLASS'}, $ctables, 8 + $base), | ||
| 474 | Font::TTF::Ttopen::ref_cache($lookup->{'MATCH'}[0], $ctables, 10 + $base), | ||
| 475 | $lookup->{'CLASS'}{'max'} + 1, $lookup->{'MATCH'}[0]{'max'} + 1); | ||
| 476 | |||
| 477 | for ($i = 0; $i <= $lookup->{'CLASS'}{'max'}; $i++) | ||
| 478 | { | ||
| 479 | for ($j = 0; $j <= $lookup->{'MATCH'}[0]{'max'}; $j++) | ||
| 480 | { | ||
| 481 | $out .= $self->out_value($lookup->{'RULES'}[$i][$j]{'ACTION'}[0], $vfmt, $ctables, length($out) + $base); | ||
| 482 | $out .= $self->out_value($lookup->{'RULES'}[$i][$j]{'ACTION'}[1], $vfmt2, $ctables, length($out) + $base); | ||
| 483 | } | ||
| 484 | } | ||
| 485 | } | ||
| 486 | } elsif ($type == 3 && $fmt == 1) | ||
| 487 | { | ||
| 488 | $out = pack('n3', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2 + $base), | ||
| 489 | $#{$lookup->{'RULES'}} + 1); | ||
| 490 | foreach $r (@{$lookup->{'RULES'}}) | ||
| 491 | { | ||
| 492 | $out .= pack('n2', Font::TTF::Ttopen::ref_cache($r->[0]{'ACTION'}[0], $ctables, length($out) + $base), | ||
| 493 | Font::TTF::Ttopen::ref_cache($r->[0]{'ACTION'}[1], $ctables, length($out) + 2 + $base)); | ||
| 494 | } | ||
| 495 | } elsif ($type == 4 || $type == 5 || $type == 6) | ||
| 496 | { | ||
| 497 | my ($loc_off, $loc_t, $ltables); | ||
| 498 | |||
| 499 | $out = pack('n7', $fmt, Font::TTF::Ttopen::ref_cache($lookup->{'MATCH'}[0], $ctables, 2 + $base), | ||
| 500 | Font::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 4 + $base), | ||
| 501 | $#{$lookup->{'RULES'}[0][0]{'ACTION'}} + 1, 12, ($#{$lookup->{'MARKS'}} + 4) << 2, | ||
| 502 | $#{$lookup->{'MARKS'}} + 1); | ||
| 503 | foreach $r (@{$lookup->{'MARKS'}}) | ||
| 504 | { $out .= pack('n2', $r->[0], Font::TTF::Ttopen::ref_cache($r->[1], $mtables, length($out) + 2)); } | ||
| 505 | push (@reftables, [$mtables, 12]); | ||
| 506 | |||
| 507 | $loc_t = length($out); | ||
| 508 | substr($out, 10, 2) = pack('n', $loc_t); | ||
| 509 | $out .= pack('n', $#{$lookup->{'RULES'}} + 1); | ||
| 510 | if ($type == 5) | ||
| 511 | { | ||
| 512 | $loc1 = length($out); | ||
| 513 | $out .= pack('n*', (0) x ($#{$lookup->{'RULES'}} + 1)); | ||
| 514 | } | ||
| 515 | $ltables = {}; | ||
| 516 | for ($i = 0; $i <= $#{$lookup->{'RULES'}}; $i++) | ||
| 517 | { | ||
| 518 | if ($type == 5) | ||
| 519 | { | ||
| 520 | $ltables = {}; | ||
| 521 | $loc_t = length($out); | ||
| 522 | substr($out, $loc1 + ($i << 1), 2) = TTF_Pack('S', $loc_t - $loc1 + 2); | ||
| 523 | } | ||
| 524 | |||
| 525 | $r = $lookup->{'RULES'}[$i]; | ||
| 526 | $out .= pack('n', $#{$r} + 1) if ($type == 5); | ||
| 527 | foreach $t (@$r) | ||
| 528 | { | ||
| 529 | foreach $s (@{$t->{'ACTION'}}) | ||
| 530 | { $out .= pack('n', Font::TTF::Ttopen::ref_cache($s, $ltables, length($out))); } | ||
| 531 | } | ||
| 532 | push (@reftables, [$ltables, $loc_t]) if ($type == 5); | ||
| 533 | } | ||
| 534 | push (@reftables, [$ltables, $loc_t]) unless ($type == 5); | ||
| 535 | $out = Font::TTF::Ttopen::out_final($fh, $out, \@reftables, 1); | ||
| 536 | } elsif ($type == 7 || $type == 8) | ||
| 537 | { $out = $self->out_context($lookup, $fh, $type - 2, $fmt, $ctables, $out, $num, $base); } | ||
| 538 | # push (@reftables, [$ctables, 0]); | ||
| 539 | $out; | ||
| 540 | } | ||
| 541 | |||
| 542 | |||
| 543 | =head2 $t->read_value($format, $base, $lookup, $fh) | ||
| 544 | |||
| 545 | Reads a value record from the current location in the file, according to the | ||
| 546 | format given. | ||
| 547 | |||
| 548 | =cut | ||
| 549 | |||
| 550 | sub read_value | ||
| 551 | { | ||
| 552 | my ($self, $fmt, $base, $lookup, $fh) = @_; | ||
| 553 | my ($flag) = 1; | ||
| 554 | my ($res) = {}; | ||
| 555 | my ($s, $i, $dat); | ||
| 556 | |||
| 557 | $s = 0; | ||
| 558 | for ($i = 0; $i < 12; $i++) | ||
| 559 | { | ||
| 560 | $s++ if ($flag & $fmt); | ||
| 561 | $flag <<= 1; | ||
| 562 | } | ||
| 563 | |||
| 564 | $fh->read($dat, $s << 1); | ||
| 565 | $flag = 1; $i = 0; | ||
| 566 | foreach $s (qw(XPlacement YPlacement XAdvance YAdvance)) | ||
| 567 | { | ||
| 568 | $res->{$s} = TTF_Unpack('s', substr($dat, $i++ << 1, 2)) if ($fmt & $flag); | ||
| 569 | $flag <<= 1; | ||
| 570 | } | ||
| 571 | |||
| 572 | foreach $s (qw(XPlaDevice YPlaDevice XAdvDevice YAdvDevice)) | ||
| 573 | { | ||
| 574 | if ($fmt & $flag) | ||
| 575 | { $res->{$s} = $self->read_delta(TTF_Unpack('S', substr($dat, $i++ << 1, 2)), | ||
| 576 | $base, $lookup, $fh); } | ||
| 577 | $flag <<= 1; | ||
| 578 | } | ||
| 579 | |||
| 580 | foreach $s (qw(XIdPlacement YIdPlacement XIdAdvance YIdAdvance)) | ||
| 581 | { | ||
| 582 | $res->{$s} = TTF_Unpack('S', substr($dat, $i++ << 1, 2)) if ($fmt & $flag); | ||
| 583 | $flag <<= 1; | ||
| 584 | } | ||
| 585 | $res; | ||
| 586 | } | ||
| 587 | |||
| 588 | |||
| 589 | =head2 $t->read_delta($offset, $base, $lookup, $fh) | ||
| 590 | |||
| 591 | Reads a delta (device table) at the given offset if it hasn not already been read. | ||
| 592 | Store the offset and item in the lookup cache ($lookup->{' CACHE'}) | ||
| 593 | |||
| 594 | =cut | ||
| 595 | |||
| 596 | sub read_delta | ||
| 597 | { | ||
| 598 | my ($self, $offset, $base, $lookup, $fh) = @_; | ||
| 599 | my ($loc) = $fh->tell(); | ||
| 600 | my ($res, $str); | ||
| 601 | |||
| 602 | return undef unless $offset; | ||
| 603 | $str = sprintf("%X", $base + $offset); | ||
| 604 | return $lookup->{' CACHE'}{$str} if defined $lookup->{' CACHE'}{$str}; | ||
| 605 | $fh->seek($base + $offset, 0); | ||
| 606 | $res = Font::TTF::Delta->new->read($fh); | ||
| 607 | $fh->seek($loc, 0); | ||
| 608 | $lookup->{' CACHE'}{$str} = $res; | ||
| 609 | return $res; | ||
| 610 | } | ||
| 611 | |||
| 612 | |||
| 613 | =head2 $t->read_anchor($offset, $base, $lookup, $fh) | ||
| 614 | |||
| 615 | Reads an Anchor table at the given offset if it has not already been read. | ||
| 616 | |||
| 617 | =cut | ||
| 618 | |||
| 619 | sub read_anchor | ||
| 620 | { | ||
| 621 | my ($self, $offset, $base, $lookup, $fh) = @_; | ||
| 622 | my ($loc) = $fh->tell(); | ||
| 623 | my ($res, $str); | ||
| 624 | |||
| 625 | return undef unless $offset; | ||
| 626 | $str = sprintf("%X", $base + $offset); | ||
| 627 | return $lookup->{' CACHE'}{$str} if defined $lookup->{' CACHE'}{$str}; | ||
| 628 | $fh->seek($base + $offset, 0); | ||
| 629 | $res = Font::TTF::Anchor->new->read($fh); | ||
| 630 | $fh->seek($loc, 0); | ||
| 631 | $lookup->{' CACHE'}{$str} = $res; | ||
| 632 | return $res; | ||
| 633 | } | ||
| 634 | |||
| 635 | |||
| 636 | =head2 $t->fmt_value | ||
| 637 | |||
| 638 | Returns the value format for a given value record | ||
| 639 | |||
| 640 | =cut | ||
| 641 | |||
| 642 | sub fmt_value | ||
| 643 | { | ||
| 644 | my ($self, $value) = @_; | ||
| 645 | my ($fmt) = 0; | ||
| 646 | my ($n); | ||
| 647 | |||
| 648 | foreach $n (reverse qw(XPlacement YPlacement XAdvance YAdvance XPlaDevice YPlaDevice | ||
| 649 | XAdvDevice YAdvDevice XIdPlacement YIdPlacement XIdAdvance | ||
| 650 | YIdAdvance)) | ||
| 651 | { | ||
| 652 | $fmt <<= 1; | ||
| 653 | $fmt |= 1 if (defined $value->{$n} && (ref $value->{$n} || $value->{$n})); | ||
| 654 | } | ||
| 655 | $fmt; | ||
| 656 | } | ||
| 657 | |||
| 658 | |||
| 659 | =head2 $t->out_value | ||
| 660 | |||
| 661 | Returns the output string for the outputting of the value for a given format. Also | ||
| 662 | updates the offset cache for any device tables referenced. | ||
| 663 | |||
| 664 | =cut | ||
| 665 | |||
| 666 | sub out_value | ||
| 667 | { | ||
| 668 | my ($self, $value, $fmt, $tables, $offset) = @_; | ||
| 669 | my ($n, $flag, $out); | ||
| 670 | |||
| 671 | $flag = 1; | ||
| 672 | foreach $n (qw(XPlacement YPlacement XAdvance YAdvance)) | ||
| 673 | { | ||
| 674 | $out .= pack('n', $value->{$n}) if ($flag & $fmt); | ||
| 675 | $flag <<= 1; | ||
| 676 | } | ||
| 677 | foreach $n (qw(XPlaDevice YPlaDevice XAdvDevice YAdvDevice)) | ||
| 678 | { | ||
| 679 | if ($flag & $fmt) | ||
| 680 | { | ||
| 681 | $out .= pack('n', Font::TTF::Ttopen::ref_cache( | ||
| 682 | $value->{$n}, $tables, $offset + length($out))); | ||
| 683 | } | ||
| 684 | $flag <<= 1; | ||
| 685 | } | ||
| 686 | foreach $n (qw(XIdPlacement YIdPlacement XIdAdvance YIdAdvance)) | ||
| 687 | { | ||
| 688 | $out .= pack('n', $value->{$n}) if ($flag & $fmt); | ||
| 689 | $flag <<= 1; | ||
| 690 | } | ||
| 691 | $out; | ||
| 692 | } | ||
| 693 | |||
| 694 | 1; | ||
| 695 | |||
| 696 | =head1 AUTHOR | ||
| 697 | |||
| 698 | Martin Hosken L<http://scripts.sil.org/FontUtils>. | ||
| 699 | |||
| 700 | |||
| 701 | =head1 LICENSING | ||
| 702 | |||
| 703 | Copyright (c) 1998-2016, SIL International (http://www.sil.org) | ||
| 704 | |||
| 705 | This module is released under the terms of the Artistic License 2.0. | ||
| 706 | For details, see the full text of the license in the file LICENSE. | ||
| 707 | |||
| 708 | |||
| 709 | |||
| 710 | =cut | ||
| 711 |