| 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: GDEF.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::GDEF; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::GDEF - Opentype GDEF table support | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | The GDEF table contains various global lists of information which are apparantly | ||
| 36 | used in other places in an OpenType renderer. But precisely where is open to | ||
| 37 | speculation... | ||
| 38 | |||
| 39 | =head1 INSTANCE VARIABLES | ||
| 40 | |||
| 41 | There are 4 tables in the GDEF table, each with their own structure: | ||
| 42 | |||
| 43 | =over 4 | ||
| 44 | |||
| 45 | =item GLYPH | ||
| 46 | |||
| 47 | This is an L<PDF::API3::Compat::API2::Basic::TTF::Coverage> Class Definition table containing information | ||
| 48 | as to what type each glyph is. | ||
| 49 | |||
| 50 | =item ATTACH | ||
| 51 | |||
| 52 | The attach table consists of a coverage table and then attachment points for | ||
| 53 | each glyph in the coverage table: | ||
| 54 | |||
| 55 | =over 8 | ||
| 56 | |||
| 57 | =item COVERAGE | ||
| 58 | |||
| 59 | This is a coverage table | ||
| 60 | |||
| 61 | =item POINTS | ||
| 62 | |||
| 63 | This is an array of point elements. Each element is an array of curve points | ||
| 64 | corresponding to the attachment points on that glyph. The order of the curve points | ||
| 65 | in the array corresponds to the attachment point number specified in the MARKS | ||
| 66 | coverage table (see below). | ||
| 67 | |||
| 68 | =back | ||
| 69 | |||
| 70 | =item LIG | ||
| 71 | |||
| 72 | This contains the ligature caret positioning information for ligature glyphs | ||
| 73 | |||
| 74 | =over 8 | ||
| 75 | |||
| 76 | =item COVERAGE | ||
| 77 | |||
| 78 | A coverage table to say which glyphs are ligatures | ||
| 79 | |||
| 80 | =item LIGS | ||
| 81 | |||
| 82 | An array of elements for each ligature. Each element is an array of information | ||
| 83 | for each caret position in the ligature (there being number of components - 1 of | ||
| 84 | these, generally) | ||
| 85 | |||
| 86 | =over 12 | ||
| 87 | |||
| 88 | =item FMT | ||
| 89 | |||
| 90 | This is the format of the information and is important to provide the semantics | ||
| 91 | for the value. This value must be set correctly before output | ||
| 92 | |||
| 93 | =item VAL | ||
| 94 | |||
| 95 | The value which has meaning according to FMT | ||
| 96 | |||
| 97 | =item DEVICE | ||
| 98 | |||
| 99 | For FMT = 3, a device table is also referenced which is stored here | ||
| 100 | |||
| 101 | =back | ||
| 102 | |||
| 103 | =back | ||
| 104 | |||
| 105 | =item MARKS | ||
| 106 | |||
| 107 | Due to confusion in the GDEF specification, this field is currently withdrawn until | ||
| 108 | the confusion is resolved. That way, perhaps this stuff will work! | ||
| 109 | |||
| 110 | This class definition table stores the mark attachment point numbers for each | ||
| 111 | attachment mark, to indicate which attachment point the mark attaches to on its | ||
| 112 | base glyph. | ||
| 113 | |||
| 114 | =back | ||
| 115 | |||
| 116 | |||
| 117 | =head1 METHODS | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | use strict; | ||
| 122 | use PDF::API3::Compat::API2::Basic::TTF::Table; | ||
| 123 | use PDF::API3::Compat::API2::Basic::TTF::Utils; | ||
| 124 | use PDF::API3::Compat::API2::Basic::TTF::Ttopen; | ||
| 125 | use vars qw(@ISA $new_gdef); | ||
| 126 | |||
| 127 | @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Table); | ||
| 128 | |||
| 129 | |||
| 130 | =head2 $t->read | ||
| 131 | |||
| 132 | Reads the table into the data structure | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub read | ||
| 137 | { | ||
| 138 | my ($self) = @_; | ||
| 139 | my ($fh) = $self->{' INFILE'}; | ||
| 140 | my ($boff) = $self->{' OFFSET'}; | ||
| 141 | my ($dat, $goff, $loff, $aoff, $moff, $r, $s, $bloc); | ||
| 142 | |||
| 143 | $self->SUPER::read or return $self; | ||
| 144 | $bloc = $fh->tell(); | ||
| 145 | $fh->read($dat, 10); | ||
| 146 | ($self->{'Version'}, $goff, $aoff, $loff) = TTF_Unpack('fS3', $dat); | ||
| 147 | if ($new_gdef) | ||
| 148 | { | ||
| 149 | $fh->read($dat, 12); | ||
| 150 | ($self->{'Version'}, $goff, $aoff, $loff, $moff) = TTF_Unpack('fS4', $dat); | ||
| 151 | } | ||
| 152 | |||
| 153 | if ($goff > 0) | ||
| 154 | { | ||
| 155 | $fh->seek($goff + $boff, 0); | ||
| 156 | $self->{'GLYPH'} = PDF::API3::Compat::API2::Basic::TTF::Coverage->new(0)->read($fh); | ||
| 157 | } | ||
| 158 | |||
| 159 | if ($new_gdef && $moff > 0) | ||
| 160 | { | ||
| 161 | $fh->seek($moff + $boff, 0); | ||
| 162 | $self->{'MARKS'} = PDF::API3::Compat::API2::Basic::TTF::Coverage->new(0)->read($fh); | ||
| 163 | } | ||
| 164 | |||
| 165 | if ($aoff > 0) | ||
| 166 | { | ||
| 167 | my ($off, $gcount, $pcount); | ||
| 168 | |||
| 169 | $fh->seek($aoff + $boff, 0); | ||
| 170 | $fh->read($dat, 4); | ||
| 171 | ($off, $gcount) = TTF_Unpack('SS', $dat); | ||
| 172 | $fh->read($dat, $gcount << 1); | ||
| 173 | |||
| 174 | $fh->seek($aoff + $boff + $off, 0); | ||
| 175 | $self->{'ATTACH'}{'COVERAGE'} = PDF::API3::Compat::API2::Basic::TTF::Coverage->new(1)->read($fh); | ||
| 176 | |||
| 177 | foreach $r (TTF_Unpack('S*', $dat)) | ||
| 178 | { | ||
| 179 | unless ($r) | ||
| 180 | { | ||
| 181 | push (@{$self->{'ATTACH'}{'POINTS'}}, []); | ||
| 182 | next; | ||
| 183 | } | ||
| 184 | $fh->seek($aoff + $boff + $r, 0); | ||
| 185 | $fh->read($dat, 2); | ||
| 186 | $pcount = TTF_Unpack('S', $dat); | ||
| 187 | $fh->read($dat, $pcount << 1); | ||
| 188 | push (@{$self->{'ATTACH'}{'POINTS'}}, [TTF_Unpack('S*', $dat)]); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | if ($loff > 0) | ||
| 193 | { | ||
| 194 | my ($lcount, $off, $ccount, $srec, $comp); | ||
| 195 | |||
| 196 | $fh->seek($loff + $boff, 0); | ||
| 197 | $fh->read($dat, 4); | ||
| 198 | ($off, $lcount) = TTF_Unpack('SS', $dat); | ||
| 199 | $fh->read($dat, $lcount << 1); | ||
| 200 | |||
| 201 | $fh->seek($off + $loff + $boff, 0); | ||
| 202 | $self->{'LIG'}{'COVERAGE'} = PDF::API3::Compat::API2::Basic::TTF::Coverage->new(1)->read($fh); | ||
| 203 | |||
| 204 | foreach $r (TTF_Unpack('S*', $dat)) | ||
| 205 | { | ||
| 206 | $fh->seek($r + $loff + $boff, 0); | ||
| 207 | $fh->read($dat, 2); | ||
| 208 | $ccount = TTF_Unpack('S', $dat); | ||
| 209 | $fh->read($dat, $ccount << 1); | ||
| 210 | |||
| 211 | $srec = []; | ||
| 212 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 213 | { | ||
| 214 | $comp = {}; | ||
| 215 | $fh->seek($s + $r + $loff + $boff, 0); | ||
| 216 | $fh->read($dat, 4); | ||
| 217 | ($comp->{'FMT'}, $comp->{'VAL'}) = TTF_Unpack('S*', $dat); | ||
| 218 | if ($comp->{'FMT'} == 3) | ||
| 219 | { | ||
| 220 | $fh->read($dat, 2); | ||
| 221 | $off = TTF_Unpack('S', $dat); | ||
| 222 | if (defined $self->{' CACHE'}{$off + $s + $r}) | ||
| 223 | { $comp->{'DEVICE'} = $self->{' CACHE'}{$off + $s + $r}; } | ||
| 224 | else | ||
| 225 | { | ||
| 226 | $fh->seek($off + $s + $r + $loff + $boff, 0); | ||
| 227 | $comp->{'DEVICE'} = PDF::API3::Compat::API2::Basic::TTF::Delta->new->read($fh); | ||
| 228 | $self->{' CACHE'}{$off + $s + $r} = $comp->{'DEVICE'}; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | push (@$srec, $comp); | ||
| 232 | } | ||
| 233 | push (@{$self->{'LIG'}{'LIGS'}}, $srec); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | $self; | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | =head2 $t->out($fh) | ||
| 242 | |||
| 243 | Writes out this table. | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | sub out | ||
| 248 | { | ||
| 249 | my ($self, $fh) = @_; | ||
| 250 | my ($goff, $aoff, $loff, $moff, @offs, $loc1, $coff, $loc); | ||
| 251 | |||
| 252 | return $self->SUPER::out($fh) unless $self->{' read'}; | ||
| 253 | |||
| 254 | $loc = $fh->tell(); | ||
| 255 | if ($new_gdef) | ||
| 256 | { $fh->print(TTF_Pack('fSSSS', $self->{'Version'}, 0, 0, 0, 0)); } | ||
| 257 | else | ||
| 258 | { $fh->print(TTF_Pack('fSSS', $self->{'Version'}, 0, 0, 0)); } | ||
| 259 | |||
| 260 | if (defined $self->{'GLYPH'}) | ||
| 261 | { | ||
| 262 | $goff = $fh->tell() - $loc; | ||
| 263 | $self->{'GLYPH'}->out($fh); | ||
| 264 | } | ||
| 265 | |||
| 266 | if (defined $self->{'ATTACH'}) | ||
| 267 | { | ||
| 268 | my ($r); | ||
| 269 | |||
| 270 | $aoff = $fh->tell() - $loc; | ||
| 271 | $fh->print(pack('n*', (0) x ($#{$self->{'ATTACH'}{'POINTS'}} + 3))); | ||
| 272 | foreach $r (@{$self->{'ATTACH'}{'POINTS'}}) | ||
| 273 | { | ||
| 274 | push (@offs, $fh->tell() - $loc - $aoff); | ||
| 275 | $fh->print(pack('n*', $#{$r} + 1, @$r)); | ||
| 276 | } | ||
| 277 | $coff = $fh->tell() - $loc - $aoff; | ||
| 278 | $self->{'ATTACH'}{'COVERAGE'}->out($fh); | ||
| 279 | $loc1 = $fh->tell(); | ||
| 280 | $fh->seek($aoff + $loc, 0); | ||
| 281 | $fh->print(pack('n*', $coff, $#offs + 1, @offs)); | ||
| 282 | $fh->seek($loc1, 0); | ||
| 283 | } | ||
| 284 | |||
| 285 | if (defined $self->{'LIG'}) | ||
| 286 | { | ||
| 287 | my (@reftables, $ltables, $i, $j, $out, $r, $s); | ||
| 288 | |||
| 289 | $ltables = {}; | ||
| 290 | $loff = $fh->tell() - $loc; | ||
| 291 | $out = pack('n*', | ||
| 292 | PDF::API3::Compat::API2::Basic::TTF::Ttopen->ref_cache($self->{'LIG'}{'COVERAGE'}, $ltables, 0), | ||
| 293 | 0, $#{$self->{'LIG'}{'LIGS'}} + 1, | ||
| 294 | (0) x ($#{$self->{'LIG'}{'LIGS'}} + 1)); | ||
| 295 | push (@reftables, [$ltables, 0]); | ||
| 296 | $i = 0; | ||
| 297 | foreach $r (@{$self->{'LIG'}{'LIGS'}}) | ||
| 298 | { | ||
| 299 | $ltables = {}; | ||
| 300 | $loc1 = length($out); | ||
| 301 | substr($out, ($i << 1) + 4, 2) = TTF_Pack('S', $loc1); | ||
| 302 | $out .= pack('n*', $#{$r} + 1, (0) x ($#{$r} + 1)); | ||
| 303 | @offs = (); $j = 0; | ||
| 304 | foreach $s (@$r) | ||
| 305 | { | ||
| 306 | substr($out, ($j << 1) + 2 + $loc1, 2) = | ||
| 307 | TTF_Pack('S', length($out) - $loc1); | ||
| 308 | $out .= TTF_Pack('SS', $s->{'FMT'}, $s->{'VAL'}); | ||
| 309 | $out .= pack('n', PDF::API3::Compat::API2::Basic::TTF::Ttopen->ref_cache($s->{'DEVICE'}, | ||
| 310 | $ltables, length($out))) if ($s->{'FMT'} == 3); | ||
| 311 | $j++; | ||
| 312 | } | ||
| 313 | push (@reftables, [$ltables, $loc1]); | ||
| 314 | $i++; | ||
| 315 | } | ||
| 316 | PDF::API3::Compat::API2::Basic::TTF::Ttopen::out_final($fh, $out, \@reftables); | ||
| 317 | } | ||
| 318 | |||
| 319 | if ($new_gdef && defined $self->{'MARKS'}) | ||
| 320 | { | ||
| 321 | $moff = $fh->tell() - $loc; | ||
| 322 | $self->{'MARKS'}->out($fh); | ||
| 323 | } | ||
| 324 | |||
| 325 | $loc1 = $fh->tell(); | ||
| 326 | $fh->seek($loc + 4, 0); | ||
| 327 | if ($new_gdef) | ||
| 328 | { $fh->print(TTF_Pack('S4', $goff, $aoff, $loff, $moff)); } | ||
| 329 | else | ||
| 330 | { $fh->print(TTF_Pack('S3', $goff, $aoff, $loff)); } | ||
| 331 | $fh->seek($loc1, 0); | ||
| 332 | $self; | ||
| 333 | } | ||
| 334 | |||
| 335 | 1; | ||
| 336 |