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