| 1 | package Font::TTF::GDEF; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Font::TTF::GDEF - Opentype GDEF table support | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | The GDEF table contains various global lists of information which are apparantly | ||
| 10 | used in other places in an OpenType renderer. But precisely where is open to | ||
| 11 | speculation... | ||
| 12 | |||
| 13 | =head1 INSTANCE VARIABLES | ||
| 14 | |||
| 15 | There are up to 5 tables in the GDEF table, each with their own structure: | ||
| 16 | |||
| 17 | =over 4 | ||
| 18 | |||
| 19 | =item GLYPH | ||
| 20 | |||
| 21 | This is an L<Font::TTF::Coverage> Class Definition table containing information | ||
| 22 | as to what type each glyph is. | ||
| 23 | |||
| 24 | =item ATTACH | ||
| 25 | |||
| 26 | The attach table consists of a coverage table and then attachment points for | ||
| 27 | each glyph in the coverage table: | ||
| 28 | |||
| 29 | =over 8 | ||
| 30 | |||
| 31 | =item COVERAGE | ||
| 32 | |||
| 33 | This is a coverage table | ||
| 34 | |||
| 35 | =item POINTS | ||
| 36 | |||
| 37 | This is an array of point elements. Each element is an array of curve points | ||
| 38 | corresponding to the attachment points on that glyph. The order of the curve points | ||
| 39 | in the array corresponds to the attachment point number specified in the MARKS | ||
| 40 | coverage table (see below). | ||
| 41 | |||
| 42 | =back | ||
| 43 | |||
| 44 | =item LIG | ||
| 45 | |||
| 46 | This contains the ligature caret positioning information for ligature glyphs | ||
| 47 | |||
| 48 | =over 8 | ||
| 49 | |||
| 50 | =item COVERAGE | ||
| 51 | |||
| 52 | A coverage table to say which glyphs are ligatures | ||
| 53 | |||
| 54 | =item LIGS | ||
| 55 | |||
| 56 | An array of elements for each ligature. Each element is an array of information | ||
| 57 | for each caret position in the ligature (there being number of components - 1 of | ||
| 58 | these, generally) | ||
| 59 | |||
| 60 | =over 12 | ||
| 61 | |||
| 62 | =item FMT | ||
| 63 | |||
| 64 | This is the format of the information and is important to provide the semantics | ||
| 65 | for the value. This value must be set correctly before output | ||
| 66 | |||
| 67 | =item VAL | ||
| 68 | |||
| 69 | The value which has meaning according to FMT | ||
| 70 | |||
| 71 | =item DEVICE | ||
| 72 | |||
| 73 | For FMT = 3, a device table is also referenced which is stored here | ||
| 74 | |||
| 75 | =back | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | =item MARKS | ||
| 80 | |||
| 81 | This class definition table defines the classes of mark glyphs that can be selected | ||
| 82 | for processing using the MarkAttachmentType field of lookup FLAG words. | ||
| 83 | |||
| 84 | =item MARKSETS | ||
| 85 | |||
| 86 | Contains an array of coverage tables indexed by the FILTER value of a lookup. | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | |||
| 91 | =head1 METHODS | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | use strict; | ||
| 96 | use Font::TTF::Table; | ||
| 97 | use Font::TTF::Utils; | ||
| 98 | use Font::TTF::Ttopen; | ||
| 99 | use vars qw(@ISA $new_gdef); | ||
| 100 | |||
| 101 | @ISA = qw(Font::TTF::Table); | ||
| 102 | $new_gdef = 1; # Prior to 2012-07, this config variable did more than it does today. | ||
| 103 | # Currently all it does is force the GDEF to include a field in the | ||
| 104 | # header for the MARKS class definition. That is, it makes sure the | ||
| 105 | # font is compatible with at least the OT 1.2 specification. | ||
| 106 | |||
| 107 | =head2 $t->read | ||
| 108 | |||
| 109 | Reads the table into the data structure | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub read | ||
| 114 | { | ||
| 115 | my ($self) = @_; | ||
| 116 | $self->SUPER::read or return $self; | ||
| 117 | |||
| 118 | my ($fh) = $self->{' INFILE'}; | ||
| 119 | my ($boff) = $self->{' OFFSET'}; | ||
| 120 | my ($dat, $goff, $aoff, $loff, $macoff, $mgsoff, $r, $s, $bloc); | ||
| 121 | |||
| 122 | $bloc = $fh->tell(); | ||
| 123 | $fh->read($dat, 10); | ||
| 124 | ($self->{'Version'}, $goff, $aoff, $loff) = TTF_Unpack('LS3', $dat); | ||
| 125 | |||
| 126 | # GDEF is the ONLY table that uses a ULONG for version rather than a Fixed or USHORT, | ||
| 127 | # and this seems to clearly be a hack. | ||
| 128 | # OpenType 1.2 introduced MarkAttachClassDef but left the table version at 0x00010000 | ||
| 129 | # Up through OpenType 1.5, the Version field was typed as Fixed. | ||
| 130 | # OpenType 1.6 introduced MarkGlyphSetsDef and bumped table version to 0x00010002 (sic) | ||
| 131 | # and changed it to ULONG. | ||
| 132 | |||
| 133 | |||
| 134 | # Thus some trickery here to read the table correctly! | ||
| 135 | |||
| 136 | if ($self->{'Version'} > 0x00010000) | ||
| 137 | { | ||
| 138 | # Ok, header guaranteed to have both MarAttachClassDef MarkGlyphSetsDef | ||
| 139 | $fh->read($dat, 4); | ||
| 140 | ($macoff, $mgsoff) = TTF_Unpack('S2', $dat); | ||
| 141 | } | ||
| 142 | else | ||
| 143 | { | ||
| 144 | # What I've seen in other code (examples: | ||
| 145 | # http://skia.googlecode.com/svn/trunk/third_party/harfbuzz/src/harfbuzz-gdef.c and | ||
| 146 | # http://doxygen.reactos.org/d0/d55/otvalid_8h_a0487daffceceb98ba425bbf2806fbaff.html | ||
| 147 | # ) is to read GPOS and GDEF and see if any lookups have a | ||
| 148 | # MarkAttachType in the upper byte of their flag word and if so then assume that the | ||
| 149 | # MarkAttachClassDef field is in the header. While this is probably the most | ||
| 150 | # reliable way to do it, it would require us to read GSUB and GPOS. | ||
| 151 | # Prior to 2012-07 what we did is depend on our $new_gdef class variable to tell us | ||
| 152 | # whether to assume a MarkAttachClassDef. | ||
| 153 | # What we do now is see if the header actually has room for the MarkAttachClassDef field. | ||
| 154 | |||
| 155 | my $minOffset = $self->{' LENGTH'}; | ||
| 156 | foreach ($goff, $aoff, $loff) | ||
| 157 | { | ||
| 158 | $minOffset = $_ if $_ > 0 && $_ < $minOffset; | ||
| 159 | } | ||
| 160 | if ($minOffset >= 12) | ||
| 161 | { | ||
| 162 | # There is room for the field, so read it: | ||
| 163 | $fh->read($dat, 2); | ||
| 164 | ($macoff) = TTF_Unpack('S', $dat); | ||
| 165 | # Sanity check: | ||
| 166 | $macoff = 0 if $macoff >= $self->{' LENGTH'}; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | if ($goff > 0) | ||
| 171 | { | ||
| 172 | $fh->seek($goff + $boff, 0); | ||
| 173 | $self->{'GLYPH'} = Font::TTF::Coverage->new(0)->read($fh); | ||
| 174 | } | ||
| 175 | |||
| 176 | if ($aoff > 0) | ||
| 177 | { | ||
| 178 | my ($off, $gcount, $pcount); | ||
| 179 | |||
| 180 | $fh->seek($aoff + $boff, 0); | ||
| 181 | $fh->read($dat, 4); | ||
| 182 | ($off, $gcount) = TTF_Unpack('SS', $dat); | ||
| 183 | $fh->read($dat, $gcount << 1); | ||
| 184 | |||
| 185 | $fh->seek($aoff + $boff + $off, 0); | ||
| 186 | $self->{'ATTACH'}{'COVERAGE'} = Font::TTF::Coverage->new(1)->read($fh); | ||
| 187 | |||
| 188 | foreach $r (TTF_Unpack('S*', $dat)) | ||
| 189 | { | ||
| 190 | unless ($r) | ||
| 191 | { | ||
| 192 | push (@{$self->{'ATTACH'}{'POINTS'}}, []); | ||
| 193 | next; | ||
| 194 | } | ||
| 195 | $fh->seek($aoff + $boff + $r, 0); | ||
| 196 | $fh->read($dat, 2); | ||
| 197 | $pcount = TTF_Unpack('S', $dat); | ||
| 198 | $fh->read($dat, $pcount << 1); | ||
| 199 | push (@{$self->{'ATTACH'}{'POINTS'}}, [TTF_Unpack('S*', $dat)]); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | if ($loff > 0) | ||
| 204 | { | ||
| 205 | my ($lcount, $off, $ccount, $srec, $comp); | ||
| 206 | |||
| 207 | $fh->seek($loff + $boff, 0); | ||
| 208 | $fh->read($dat, 4); | ||
| 209 | ($off, $lcount) = TTF_Unpack('SS', $dat); | ||
| 210 | $fh->read($dat, $lcount << 1); | ||
| 211 | |||
| 212 | $fh->seek($off + $loff + $boff, 0); | ||
| 213 | $self->{'LIG'}{'COVERAGE'} = Font::TTF::Coverage->new(1)->read($fh); | ||
| 214 | |||
| 215 | foreach $r (TTF_Unpack('S*', $dat)) | ||
| 216 | { | ||
| 217 | $fh->seek($r + $loff + $boff, 0); | ||
| 218 | $fh->read($dat, 2); | ||
| 219 | $ccount = TTF_Unpack('S', $dat); | ||
| 220 | $fh->read($dat, $ccount << 1); | ||
| 221 | |||
| 222 | $srec = []; | ||
| 223 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 224 | { | ||
| 225 | $comp = {}; | ||
| 226 | $fh->seek($s + $r + $loff + $boff, 0); | ||
| 227 | $fh->read($dat, 4); | ||
| 228 | ($comp->{'FMT'}, $comp->{'VAL'}) = TTF_Unpack('S*', $dat); | ||
| 229 | if ($comp->{'FMT'} == 3) | ||
| 230 | { | ||
| 231 | $fh->read($dat, 2); | ||
| 232 | $off = TTF_Unpack('S', $dat); | ||
| 233 | if (defined $self->{' CACHE'}{$off + $s + $r + $loff}) | ||
| 234 | { $comp->{'DEVICE'} = $self->{' CACHE'}{$off + $s + $r + $loff}; } | ||
| 235 | else | ||
| 236 | { | ||
| 237 | $fh->seek($off + $s + $r + $loff + $boff, 0); | ||
| 238 | $comp->{'DEVICE'} = Font::TTF::Delta->new->read($fh); | ||
| 239 | $self->{' CACHE'}{$off + $s + $r + $loff} = $comp->{'DEVICE'}; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | push (@$srec, $comp); | ||
| 243 | } | ||
| 244 | push (@{$self->{'LIG'}{'LIGS'}}, $srec); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | if ($macoff > 0) | ||
| 249 | { | ||
| 250 | $fh->seek($macoff + $boff, 0); | ||
| 251 | $self->{'MARKS'} = Font::TTF::Coverage->new(0)->read($fh); | ||
| 252 | } | ||
| 253 | |||
| 254 | if ($mgsoff > 0) | ||
| 255 | { | ||
| 256 | my ($fmt, $count, $off); | ||
| 257 | $fh->seek($mgsoff + $boff, 0); | ||
| 258 | $fh->read($dat, 4); | ||
| 259 | ($fmt, $count) = TTF_Unpack('SS', $dat); | ||
| 260 | # Sanity check opportunity: Could verify $fmt == 1, but I don't. | ||
| 261 | $self->{'MARKSETS'} = []; | ||
| 262 | $fh->read($dat, $count << 2); # NB: These offets are ULONGs! | ||
| 263 | foreach $off (TTF_Unpack('L*', $dat)) | ||
| 264 | { | ||
| 265 | unless (defined $self->{' CACHE'}{$off + $mgsoff}) | ||
| 266 | { | ||
| 267 | $fh->seek($off + $mgsoff + $boff, 0); | ||
| 268 | $self->{' CACHE'}{$off + $mgsoff} = Font::TTF::Coverage->new(1)->read($fh); | ||
| 269 | } | ||
| 270 | push @{$self->{'MARKSETS'}}, $self->{' CACHE'}{$off + $mgsoff}; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | $self; | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | =head2 $t->out($fh) | ||
| 279 | |||
| 280 | Writes out this table. | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub out | ||
| 285 | { | ||
| 286 | my ($self, $fh) = @_; | ||
| 287 | my ($goff, $aoff, $loff, $macoff, $mgsoff, @offs, $loc1, $coff, $loc); | ||
| 288 | |||
| 289 | return $self->SUPER::out($fh) unless $self->{' read'}; | ||
| 290 | |||
| 291 | $loc = $fh->tell(); | ||
| 292 | if (defined $self->{'MARKSETS'} && @{$self->{'MARKSETS'}} > 0) | ||
| 293 | { | ||
| 294 | $self->{'Version'} = 0x00010002 if ($self->{'Version'} < 0x00010002); | ||
| 295 | $fh->print(TTF_Pack('LSSSSS', $self->{'Version'}, 0, 0, 0, 0, 0)); | ||
| 296 | } | ||
| 297 | else | ||
| 298 | { | ||
| 299 | $self->{'Version'} = 0x00010000; | ||
| 300 | if ($new_gdef || defined $self->{'MARKS'}) | ||
| 301 | { $fh->print(TTF_Pack('LSSSS', $self->{'Version'}, 0, 0, 0, 0)); } | ||
| 302 | else | ||
| 303 | { $fh->print(TTF_Pack('LSSS', $self->{'Version'}, 0, 0, 0)); } | ||
| 304 | } | ||
| 305 | |||
| 306 | if (defined $self->{'GLYPH'}) | ||
| 307 | { | ||
| 308 | $goff = $fh->tell() - $loc; | ||
| 309 | $self->{'GLYPH'}->out($fh); | ||
| 310 | } | ||
| 311 | |||
| 312 | if (defined $self->{'ATTACH'}) | ||
| 313 | { | ||
| 314 | my ($r); | ||
| 315 | |||
| 316 | $aoff = $fh->tell() - $loc; | ||
| 317 | $fh->print(pack('n*', (0) x ($#{$self->{'ATTACH'}{'POINTS'}} + 3))); | ||
| 318 | foreach $r (@{$self->{'ATTACH'}{'POINTS'}}) | ||
| 319 | { | ||
| 320 | push (@offs, $fh->tell() - $loc - $aoff); | ||
| 321 | $fh->print(pack('n*', $#{$r} + 1, @$r)); | ||
| 322 | } | ||
| 323 | $coff = $fh->tell() - $loc - $aoff; | ||
| 324 | $self->{'ATTACH'}{'COVERAGE'}->out($fh); | ||
| 325 | $loc1 = $fh->tell(); | ||
| 326 | $fh->seek($aoff + $loc, 0); | ||
| 327 | $fh->print(pack('n*', $coff, $#offs + 1, @offs)); | ||
| 328 | $fh->seek($loc1, 0); | ||
| 329 | } | ||
| 330 | |||
| 331 | if (defined $self->{'LIG'}) | ||
| 332 | { | ||
| 333 | my (@reftables, $ltables, $i, $j, $out, $r, $s); | ||
| 334 | |||
| 335 | $ltables = {}; | ||
| 336 | $loff = $fh->tell() - $loc; | ||
| 337 | $out = pack('n*', | ||
| 338 | Font::TTF::Ttopen::ref_cache($self->{'LIG'}{'COVERAGE'}, $ltables, 0), | ||
| 339 | $#{$self->{'LIG'}{'LIGS'}} + 1, | ||
| 340 | (0) x ($#{$self->{'LIG'}{'LIGS'}} + 1)); | ||
| 341 | push (@reftables, [$ltables, 0]); | ||
| 342 | $i = 0; | ||
| 343 | foreach $r (@{$self->{'LIG'}{'LIGS'}}) | ||
| 344 | { | ||
| 345 | $ltables = {}; | ||
| 346 | $loc1 = length($out); | ||
| 347 | substr($out, ($i << 1) + 4, 2) = TTF_Pack('S', $loc1); | ||
| 348 | $out .= pack('n*', $#{$r} + 1, (0) x ($#{$r} + 1)); | ||
| 349 | @offs = (); $j = 0; | ||
| 350 | foreach $s (@$r) | ||
| 351 | { | ||
| 352 | substr($out, ($j << 1) + 2 + $loc1, 2) = | ||
| 353 | TTF_Pack('S', length($out) - $loc1); | ||
| 354 | $out .= TTF_Pack('SS', $s->{'FMT'}, $s->{'VAL'}); | ||
| 355 | $out .= pack('n', Font::TTF::Ttopen::ref_cache($s->{'DEVICE'}, | ||
| 356 | $ltables, length($out))) if ($s->{'FMT'} == 3); | ||
| 357 | $j++; | ||
| 358 | } | ||
| 359 | push (@reftables, [$ltables, $loc1]); | ||
| 360 | $i++; | ||
| 361 | } | ||
| 362 | Font::TTF::Ttopen::out_final($fh, $out, \@reftables); | ||
| 363 | } | ||
| 364 | |||
| 365 | if (defined $self->{'MARKS'}) | ||
| 366 | { | ||
| 367 | $macoff = $fh->tell() - $loc; | ||
| 368 | $self->{'MARKS'}->out($fh); | ||
| 369 | } | ||
| 370 | |||
| 371 | if (defined $self->{'MARKSETS'}) | ||
| 372 | { | ||
| 373 | |||
| 374 | my (@reftables, $ctables, $c, $out); | ||
| 375 | $ctables = {}; | ||
| 376 | $mgsoff = $fh->tell() - $loc; | ||
| 377 | $out = TTF_Pack('SS', 1, $#{$self->{'MARKSETS'}}+1); | ||
| 378 | foreach $c (@{$self->{'MARKSETS'}}) | ||
| 379 | { | ||
| 380 | $out .= pack('N', Font::TTF::Ttopen::ref_cache($c, $ctables, length($out), 'N')); | ||
| 381 | } | ||
| 382 | push (@reftables, [$ctables, 0]); | ||
| 383 | Font::TTF::Ttopen::out_final($fh, $out, \@reftables); | ||
| 384 | } | ||
| 385 | |||
| 386 | $loc1 = $fh->tell(); | ||
| 387 | $fh->seek($loc + 4, 0); | ||
| 388 | if ($mgsoff) | ||
| 389 | { $fh->print(TTF_Pack('S5', $goff, $aoff, $loff, $macoff, $mgsoff)); } | ||
| 390 | elsif ($macoff) | ||
| 391 | { $fh->print(TTF_Pack('S4', $goff, $aoff, $loff, $macoff)); } | ||
| 392 | else | ||
| 393 | { $fh->print(TTF_Pack('S3', $goff, $aoff, $loff)); } | ||
| 394 | $fh->seek($loc1, 0); | ||
| 395 | $self; | ||
| 396 | } | ||
| 397 | |||
| 398 | |||
| 399 | =head2 $t->minsize() | ||
| 400 | |||
| 401 | Returns the minimum size this table can be. If it is smaller than this, then the table | ||
| 402 | must be bad and should be deleted or whatever. | ||
| 403 | |||
| 404 | =cut | ||
| 405 | |||
| 406 | sub minsize | ||
| 407 | { | ||
| 408 | return 10; | ||
| 409 | } | ||
| 410 | |||
| 411 | |||
| 412 | =head2 $t->update | ||
| 413 | |||
| 414 | Sort COVERAGE tables. | ||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | sub update | ||
| 419 | { | ||
| 420 | my ($self) = @_; | ||
| 421 | |||
| 422 | return undef unless ($self->SUPER::update); | ||
| 423 | |||
| 424 | unless ($Font::TTF::Coverage::dontsort) | ||
| 425 | { | ||
| 426 | if (defined $self->{'ATTACH'} and defined $self->{'ATTACH'}{'COVERAGE'} and !$self->{'ATTACH'}{'COVERAGE'}{'dontsort'} ) | ||
| 427 | { | ||
| 428 | my @map = $self->{'ATTACH'}{'COVERAGE'}->sort(); | ||
| 429 | if (defined $self->{'ATTACH'}{'POINTS'}) | ||
| 430 | { | ||
| 431 | # And also a POINTS array which now needs to be re-sorted | ||
| 432 | my $newpoints = []; | ||
| 433 | foreach (0 .. $#map) | ||
| 434 | { push @{$newpoints}, $self->{'ATTACH'}{'POINTS'}[$map[$_]]; } | ||
| 435 | $self->{'ATTACH'}{'POINTS'} = $newpoints; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | if (defined $self->{'LIG'} and defined $self->{'LIG'}{'COVERAGE'} and !$self->{'LIG'}{'COVERAGE'}{'dontsort'} ) | ||
| 439 | { | ||
| 440 | my @map = $self->{'LIG'}{'COVERAGE'}->sort(); | ||
| 441 | if (defined $self->{'LIG'}{'LIGS'}) | ||
| 442 | { | ||
| 443 | # And also a LIGS array which now needs to be re-sorted | ||
| 444 | my $newligs = []; | ||
| 445 | foreach (0 .. $#map) | ||
| 446 | { push @{$newligs}, $self->{'LIG'}{'LIGS'}[$map[$_]]; } | ||
| 447 | $self->{'LIG'}{'LIGS'} = $newligs; | ||
| 448 | } | ||
| 449 | } | ||
| 450 | if (defined $self->{'MARKSETS'}) | ||
| 451 | { | ||
| 452 | foreach (@{$self->{'MARKSETS'}}) | ||
| 453 | {$_->sort();} # Don't care about map | ||
| 454 | } | ||
| 455 | } | ||
| 456 | |||
| 457 | $self; | ||
| 458 | } | ||
| 459 | |||
| 460 | 1; | ||
| 461 | |||
| 462 | =head1 AUTHOR | ||
| 463 | |||
| 464 | Martin Hosken L<http://scripts.sil.org/FontUtils>. | ||
| 465 | |||
| 466 | |||
| 467 | =head1 LICENSING | ||
| 468 | |||
| 469 | Copyright (c) 1998-2016, SIL International (http://www.sil.org) | ||
| 470 | |||
| 471 | This module is released under the terms of the Artistic License 2.0. | ||
| 472 | For details, see the full text of the license in the file LICENSE. | ||
| 473 | |||
| 474 | |||
| 475 | |||
| 476 | =cut |