| 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: GSUB.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::GSUB; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::GSUB - Module support for the GSUB table in conjunction with TTOpen | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Handles the GSUB subtables in relation to Ttopen tables. Due to the variety of | ||
| 36 | different lookup types, the data structures are not all that straightforward, | ||
| 37 | although I have tried to make life easy for myself when using this! | ||
| 38 | |||
| 39 | =head1 INSTANCE VARIABLES | ||
| 40 | |||
| 41 | The structure of a GSUB table is the same as that given in L<PDF::API3::Compat::API2::Basic::TTF::Ttopen>. | ||
| 42 | Here we give some of the semantics specific to GSUB lookups. | ||
| 43 | |||
| 44 | =over 4 | ||
| 45 | |||
| 46 | =item ACTION_TYPE | ||
| 47 | |||
| 48 | This is a string taking one of 4 values indicating the nature of the information | ||
| 49 | in the ACTION array of the rule: | ||
| 50 | |||
| 51 | =over 8 | ||
| 52 | |||
| 53 | =item g | ||
| 54 | |||
| 55 | The action contains a string of glyphs to replace the match string by | ||
| 56 | |||
| 57 | =item l | ||
| 58 | |||
| 59 | The action array contains a list of lookups and offsets to run, in order, on | ||
| 60 | the matched string | ||
| 61 | |||
| 62 | =item a | ||
| 63 | |||
| 64 | The action array is an unordered set of optional replacements for the matched | ||
| 65 | glyph. The application should make the selection somehow. | ||
| 66 | |||
| 67 | =item o | ||
| 68 | |||
| 69 | The action array is empty (in fact there is no rule array for this type of | ||
| 70 | rule) and the ADJUST value should be added to the glyph id to find the replacement | ||
| 71 | glyph id value | ||
| 72 | |||
| 73 | =back | ||
| 74 | |||
| 75 | =item MATCH_TYPE | ||
| 76 | |||
| 77 | This indicates which type of information the various MATCH arrays (MATCH, PRE, | ||
| 78 | POST) hold in the rule: | ||
| 79 | |||
| 80 | =over 8 | ||
| 81 | |||
| 82 | =item g | ||
| 83 | |||
| 84 | The array holds a string of glyph ids which should match exactly | ||
| 85 | |||
| 86 | =item c | ||
| 87 | |||
| 88 | The array holds a sequence of class definitions which each glyph should | ||
| 89 | correspondingly match to | ||
| 90 | |||
| 91 | =item o | ||
| 92 | |||
| 93 | The array holds offsets to coverage tables | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | =head1 CORRESPONDANCE TO LAYOUT TYPES | ||
| 100 | |||
| 101 | The following table gives the values for ACTION_TYPE and MATCH_TYPE for each | ||
| 102 | of the 11 different lookup types found in the GSUB table definition I have: | ||
| 103 | |||
| 104 | 1.1 1.2 2 3 4 5.1 5.2 5.3 6.1 6.2 6.3 | ||
| 105 | ACTION_TYPE o g g a g l l l l l l | ||
| 106 | MATCH_TYPE g g c o g c o | ||
| 107 | |||
| 108 | Hopefully, the rest of the uses of the variables should make sense from this | ||
| 109 | table. | ||
| 110 | |||
| 111 | =head1 METHODS | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | use strict; | ||
| 116 | use vars qw(@ISA); | ||
| 117 | use PDF::API3::Compat::API2::Basic::TTF::Utils; | ||
| 118 | use PDF::API3::Compat::API2::Basic::TTF::Ttopen; | ||
| 119 | |||
| 120 | @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Ttopen); | ||
| 121 | |||
| 122 | =head2 $t->read_sub($fh, $lookup, $index) | ||
| 123 | |||
| 124 | Asked by the superclass to read in from the given file the indexth subtable from | ||
| 125 | lookup number lookup. The file is positioned ready for the read. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub read_sub | ||
| 130 | { | ||
| 131 | my ($self, $fh, $main_lookup, $sindex) = @_; | ||
| 132 | my ($type) = $main_lookup->{'TYPE'}; | ||
| 133 | my ($loc) = $fh->tell(); | ||
| 134 | my ($lookup) = $main_lookup->{'SUB'}[$sindex]; | ||
| 135 | my ($dat, $s, @subst, $t, $fmt, $cover, $count, $mcount, $scount, $i, $gid); | ||
| 136 | my (@srec); | ||
| 137 | |||
| 138 | if ($type == 6) | ||
| 139 | { | ||
| 140 | $fh->read($dat, 4); | ||
| 141 | ($fmt, $cover) = TTF_Unpack('S2', $dat); | ||
| 142 | if ($fmt < 3) | ||
| 143 | { | ||
| 144 | $fh->read($dat, 2); | ||
| 145 | $count = TTF_Unpack('S', $dat); | ||
| 146 | } | ||
| 147 | } else | ||
| 148 | { | ||
| 149 | $fh->read($dat, 6); | ||
| 150 | ($fmt, $cover, $count) = TTF_Unpack("S3", $dat); | ||
| 151 | } | ||
| 152 | unless ($fmt == 3 && ($type == 5 || $type == 6)) | ||
| 153 | { $lookup->{'COVERAGE'} = $self->read_cover($cover, $loc, $lookup, $fh, 1); } | ||
| 154 | |||
| 155 | $lookup->{'FORMAT'} = $fmt; | ||
| 156 | if ($type == 1 && $fmt == 1) | ||
| 157 | { | ||
| 158 | $lookup->{'ADJUST'} = $count; | ||
| 159 | $lookup->{'ACTION_TYPE'} = 'o'; | ||
| 160 | } elsif ($type == 1 && $fmt == 2) | ||
| 161 | { | ||
| 162 | $fh->read($dat, $count << 1); | ||
| 163 | @subst = TTF_Unpack('S*', $dat); | ||
| 164 | foreach $s (@subst) | ||
| 165 | { push(@{$lookup->{'RULES'}}, [{'ACTION' => [$s]}]); } | ||
| 166 | $lookup->{'ACTION_TYPE'} = 'g'; | ||
| 167 | } elsif ($type == 2 || $type == 3) | ||
| 168 | { | ||
| 169 | $fh->read($dat, $count << 1); # number of offsets | ||
| 170 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 171 | { | ||
| 172 | $fh->seek($loc + $s, 0); | ||
| 173 | $fh->read($dat, 2); | ||
| 174 | $t = TTF_Unpack('S', $dat); | ||
| 175 | $fh->read($dat, $t << 1); | ||
| 176 | push(@{$lookup->{'RULES'}}, [{'ACTION' => [TTF_Unpack('S*', $dat)]}]); | ||
| 177 | } | ||
| 178 | $lookup->{'ACTION_TYPE'} = ($type == 2 ? 'g' : 'a'); | ||
| 179 | } elsif ($type == 4) | ||
| 180 | { | ||
| 181 | $fh->read($dat, $count << 1); | ||
| 182 | foreach $s (TTF_Unpack('S*', $dat)) | ||
| 183 | { | ||
| 184 | @subst = (); | ||
| 185 | $fh->seek($loc + $s, 0); | ||
| 186 | $fh->read($dat, 2); | ||
| 187 | $t = TTF_Unpack('S', $dat); | ||
| 188 | $fh->read($dat, $t << 1); | ||
| 189 | foreach $t (TTF_Unpack('S*', $dat)) | ||
| 190 | { | ||
| 191 | $fh->seek($loc + $s + $t, 0); | ||
| 192 | $fh->read($dat, 4); | ||
| 193 | ($gid, $mcount) = TTF_Unpack('S2', $dat); | ||
| 194 | $fh->read($dat, ($mcount - 1) << 1); | ||
| 195 | push(@subst, {'ACTION' => [$gid], 'MATCH' => [TTF_Unpack('S*', $dat)]}); | ||
| 196 | } | ||
| 197 | push(@{$lookup->{'RULES'}}, [@subst]); | ||
| 198 | } | ||
| 199 | $lookup->{'ACTION_TYPE'} = 'g'; | ||
| 200 | $lookup->{'MATCH_TYPE'} = 'g'; | ||
| 201 | } elsif ($type == 5 || $type == 6) | ||
| 202 | { $self->read_context($lookup, $fh, $type, $fmt, $cover, $count, $loc); } | ||
| 203 | $lookup; | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | =head2 $t->extension | ||
| 208 | |||
| 209 | Returns the table type number for the extension table | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | sub extension | ||
| 214 | { return 7; } | ||
| 215 | |||
| 216 | |||
| 217 | =head2 $t->out_sub($fh, $lookup, $index) | ||
| 218 | |||
| 219 | Passed the filehandle to output to, suitably positioned, the lookup and subtable | ||
| 220 | index, this function outputs the subtable to $fh at that point. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub out_sub | ||
| 225 | { | ||
| 226 | my ($self, $fh, $main_lookup, $index) = @_; | ||
| 227 | my ($type) = $main_lookup->{'TYPE'}; | ||
| 228 | my ($lookup) = $main_lookup->{'SUB'}[$index]; | ||
| 229 | my ($fmt) = $lookup->{'FORMAT'}; | ||
| 230 | my ($out, $r, $t, $i, $j, $offc, $offd, $numd); | ||
| 231 | my ($num) = $#{$lookup->{'RULES'}} + 1; | ||
| 232 | my ($ctables) = {}; | ||
| 233 | |||
| 234 | if ($type == 1) | ||
| 235 | { | ||
| 236 | $out = pack("nn", $fmt, PDF::API3::Compat::API2::Basic::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2)); | ||
| 237 | if ($fmt == 1) | ||
| 238 | { $out .= pack("n", $lookup->{'ADJUST'}); } | ||
| 239 | else | ||
| 240 | { | ||
| 241 | $out .= pack("n", $num); | ||
| 242 | foreach $r (@{$lookup->{'RULES'}}) | ||
| 243 | { $out .= pack("n", $r->[0]{'ACTION'}[0]); } | ||
| 244 | } | ||
| 245 | } elsif ($type == 2 || $type == 3) | ||
| 246 | { | ||
| 247 | $out = pack("nnn", $fmt, PDF::API3::Compat::API2::Basic::TTF::Ttopen::ref_cache($lookup->{'COVERAGE'}, $ctables, 2), | ||
| 248 | $num); | ||
| 249 | $out .= pack('n*', 0 x $num); | ||
| 250 | $offc = length($out); | ||
| 251 | for ($i = 0; $i < $num; $i++) | ||
| 252 | { | ||
| 253 | $out .= pack("n*", $#{$lookup->{'RULES'}[$i][0]{'ACTION'}} + 1, | ||
| 254 | @{$lookup->{'RULES'}[$i][0]{'ACTION'}}); | ||
| 255 | substr($out, ($i << 1) + 6, 2) = pack('n', $offc); | ||
| 256 | $offc = length($out); | ||
| 257 | } | ||
| 258 | } elsif ($type == 4 || $type == 5 || $type == 6) | ||
| 259 | { $out = $self->out_context($lookup, $fh, $type, $fmt, $ctables, $out, $num); } | ||
| 260 | PDF::API3::Compat::API2::Basic::TTF::Ttopen::out_final($fh, $out, [[$ctables, 0]]); | ||
| 261 | $lookup; | ||
| 262 | } | ||
| 263 | |||
| 264 | =head1 AUTHOR | ||
| 265 | |||
| 266 | Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 267 | licensing. | ||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | 1; | ||
| 272 |