| 1 | package Font::TTF::Coverage; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Font::TTF::Coverage - Opentype coverage and class definition objects | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | Coverage tables and class definition objects are virtually identical concepts | ||
| 10 | in OpenType. Their difference comes purely in their storage. Therefore we can | ||
| 11 | say that a coverage table is a class definition in which the class definition | ||
| 12 | for each glyph is the corresponding index in the coverage table. The resulting | ||
| 13 | data structure is that a Coverage table has the following fields: | ||
| 14 | |||
| 15 | =over | ||
| 16 | |||
| 17 | =item cover | ||
| 18 | |||
| 19 | A boolean to indicate whether this table is a coverage table (TRUE) or a | ||
| 20 | class definition (FALSE) | ||
| 21 | |||
| 22 | =item val | ||
| 23 | |||
| 24 | A hash of glyph ids against values (either coverage index or class value) | ||
| 25 | |||
| 26 | =item fmt | ||
| 27 | |||
| 28 | The storage format used is given here, but is recalculated when the table | ||
| 29 | is written out. | ||
| 30 | |||
| 31 | =item count | ||
| 32 | |||
| 33 | A count of the elements in a coverage table for use with add. Each subsequent | ||
| 34 | addition is added with the current count and increments the count. | ||
| 35 | |||
| 36 | =item max | ||
| 37 | |||
| 38 | Maximum class value in a class table. | ||
| 39 | |||
| 40 | =back | ||
| 41 | |||
| 42 | =head1 METHODS | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | =head2 new($isCover [, vals]) | ||
| 47 | |||
| 48 | Creates a new coverage table or class definition table, depending upon the | ||
| 49 | value of $isCover. if $isCover then vals may be a list of glyphs to include in order. | ||
| 50 | If no $isCover, then vals is a hash of glyphs against class values. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | our $dontsort; | ||
| 55 | |||
| 56 | sub new | ||
| 57 | { | ||
| 58 | my ($class) = shift; | ||
| 59 | my ($isCover) = shift; | ||
| 60 | my ($self) = {}; | ||
| 61 | |||
| 62 | $self->{'cover'} = $isCover; | ||
| 63 | if ($isCover) | ||
| 64 | { | ||
| 65 | $self->{'count'} = 0; | ||
| 66 | my ($v); | ||
| 67 | foreach $v (@_) | ||
| 68 | { $self->{'val'}{$v} = $self->{'count'}++; } | ||
| 69 | } | ||
| 70 | else | ||
| 71 | { | ||
| 72 | $self->{'max'} = 0; | ||
| 73 | $self->{'val'} = {@_}; | ||
| 74 | foreach (values %{$self->{'val'}}) {$self->{'max'} = $_ if $_ > $self->{'max'}} | ||
| 75 | } | ||
| 76 | bless $self, $class; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | =head2 read($fh) | ||
| 81 | |||
| 82 | Reads the coverage/class table from the given file handle | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub read | ||
| 87 | { | ||
| 88 | my ($self, $fh) = @_; | ||
| 89 | my ($dat, $fmt, $num, $i, $c); | ||
| 90 | |||
| 91 | $fh->read($dat, 4); | ||
| 92 | ($fmt, $num) = unpack("n2", $dat); | ||
| 93 | $self->{'fmt'} = $fmt; | ||
| 94 | |||
| 95 | if ($self->{'cover'}) | ||
| 96 | { | ||
| 97 | if ($fmt == 1) | ||
| 98 | { | ||
| 99 | $fh->read($dat, $num << 1); | ||
| 100 | map {$self->{'val'}{$_} = $i++} unpack("n*", $dat); | ||
| 101 | $self->{'count'} = $num; | ||
| 102 | } elsif ($fmt == 2) | ||
| 103 | { | ||
| 104 | $fh->read($dat, $num * 6); | ||
| 105 | for ($i = 0; $i < $num; $i++) | ||
| 106 | { | ||
| 107 | ($first, $last, $c) = unpack("n3", substr($dat, $i * 6, 6)); | ||
| 108 | map {$self->{'val'}{$_} = $c++} ($first .. $last); | ||
| 109 | } | ||
| 110 | $self->{'count'} = $c; | ||
| 111 | } | ||
| 112 | } elsif ($fmt == 1) | ||
| 113 | { | ||
| 114 | $fh->read($dat, 2); | ||
| 115 | $first = $num; | ||
| 116 | ($num) = unpack("n", $dat); | ||
| 117 | $fh->read($dat, $num << 1); | ||
| 118 | map {$self->{'val'}{$first++} = $_; $self->{'max'} = $_ if ($_ > $self->{'max'})} unpack("n*", $dat); | ||
| 119 | } elsif ($fmt == 2) | ||
| 120 | { | ||
| 121 | $fh->read($dat, $num * 6); | ||
| 122 | for ($i = 0; $i < $num; $i++) | ||
| 123 | { | ||
| 124 | ($first, $last, $c) = unpack("n3", substr($dat, $i * 6, 6)); | ||
| 125 | map {$self->{'val'}{$_} = $c} ($first .. $last); | ||
| 126 | $self->{'max'} = $c if ($c > $self->{'max'}); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | $self; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | =head2 out($fh, $state) | ||
| 134 | |||
| 135 | Writes the coverage/class table to the given file handle. If $state is 1 then | ||
| 136 | the output string is returned rather than being output to a filehandle. | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub out | ||
| 141 | { | ||
| 142 | my ($self, $fh, $state) = @_; | ||
| 143 | my ($g, $eff, $grp, $out); | ||
| 144 | my ($shipout) = ($state ? sub {$out .= $_[0];} : sub {$fh->print($_[0]);}); | ||
| 145 | my (@gids) = sort {$a <=> $b} keys %{$self->{'val'}}; | ||
| 146 | |||
| 147 | if ($self->{'cover'}) | ||
| 148 | { $self->sort() unless ($self->{'dontsort'} or $dontsort); } | ||
| 149 | else | ||
| 150 | { @gids = grep {$self->{'val'}{$_} > 0} @gids;} # class value=0 is not explicitly coded in class table | ||
| 151 | |||
| 152 | $fmt = 1; $grp = 1; $eff = 0; | ||
| 153 | for ($i = 1; $i <= $#gids; $i++) | ||
| 154 | { | ||
| 155 | if ($self->{'val'}{$gids[$i]} < $self->{'val'}{$gids[$i-1]} && $self->{'cover'}) | ||
| 156 | { | ||
| 157 | $fmt = 2; | ||
| 158 | last; | ||
| 159 | } elsif ($gids[$i] == $gids[$i-1] + 1 && ($self->{'cover'} || $self->{'val'}{$gids[$i]} == $self->{'val'}{$gids[$i-1]})) | ||
| 160 | { $eff++; } | ||
| 161 | else | ||
| 162 | { | ||
| 163 | $grp++; | ||
| 164 | $eff += $gids[$i] - $gids[$i-1] if (!$self->{'cover'}); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | # if ($self->{'cover'}) | ||
| 168 | { $fmt = 2 if ($eff / $grp > 3 || scalar (@gids) == 0); } | ||
| 169 | # else | ||
| 170 | # { $fmt = 2 if ($grp > 1); } | ||
| 171 | |||
| 172 | if ($fmt == 1 && $self->{'cover'}) | ||
| 173 | { | ||
| 174 | my ($last) = 0; | ||
| 175 | &$shipout(pack('n2', 1, scalar @gids)); | ||
| 176 | &$shipout(pack('n*', @gids)); | ||
| 177 | } elsif ($fmt == 1) | ||
| 178 | { | ||
| 179 | my ($last) = $gids[0]; | ||
| 180 | &$shipout(pack("n3", 1, $last, $gids[-1] - $last + 1)); | ||
| 181 | foreach $g (@gids) | ||
| 182 | { | ||
| 183 | if ($g > $last + 1) | ||
| 184 | { &$shipout(pack('n*', (0) x ($g - $last - 1))); } | ||
| 185 | &$shipout(pack('n', $self->{'val'}{$g})); | ||
| 186 | $last = $g; | ||
| 187 | } | ||
| 188 | } else | ||
| 189 | { | ||
| 190 | my ($start, $end, $ind, $numloc, $endloc, $num); | ||
| 191 | &$shipout(pack("n2", 2, 0)); | ||
| 192 | $numloc = $fh->tell() - 2 unless $state; | ||
| 193 | |||
| 194 | $start = 0; $end = 0; $num = 0; | ||
| 195 | while ($end < $#gids) | ||
| 196 | { | ||
| 197 | if ($gids[$end + 1] == $gids[$end] + 1 | ||
| 198 | && $self->{'val'}{$gids[$end + 1]} | ||
| 199 | == $self->{'val'}{$gids[$end]} | ||
| 200 | + ($self->{'cover'} ? 1 : 0)) | ||
| 201 | { | ||
| 202 | $end++; | ||
| 203 | next; | ||
| 204 | } | ||
| 205 | |||
| 206 | &$shipout(pack("n3", $gids[$start], $gids[$end], | ||
| 207 | $self->{'val'}{$gids[$start]})); | ||
| 208 | $start = $end + 1; | ||
| 209 | $end++; | ||
| 210 | $num++; | ||
| 211 | } | ||
| 212 | if (scalar(@gids)) | ||
| 213 | { | ||
| 214 | &$shipout(pack("n3", $gids[$start], $gids[$end], | ||
| 215 | $self->{'val'}{$gids[$start]})); | ||
| 216 | $num++; | ||
| 217 | } | ||
| 218 | if ($state) | ||
| 219 | { substr($out, 2, 2) = pack('n', $num); } | ||
| 220 | else | ||
| 221 | { | ||
| 222 | $endloc = $fh->tell(); | ||
| 223 | $fh->seek($numloc, 0); | ||
| 224 | $fh->print(pack("n", $num)); | ||
| 225 | $fh->seek($endloc, 0); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | return ($state ? $out : $self); | ||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | =head2 $c->add($glyphid[, $class]) | ||
| 233 | |||
| 234 | Adds a glyph id to the coverage or class table. | ||
| 235 | Returns the index or class number of the glyphid added. | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | sub add | ||
| 240 | { | ||
| 241 | my ($self, $gid, $class) = @_; | ||
| 242 | |||
| 243 | return $self->{'val'}{$gid} if (defined $self->{'val'}{$gid}); | ||
| 244 | if ($self->{'cover'}) | ||
| 245 | { | ||
| 246 | $self->{'val'}{$gid} = $self->{'count'}; | ||
| 247 | return $self->{'count'}++; | ||
| 248 | } | ||
| 249 | else | ||
| 250 | { | ||
| 251 | $self->{'val'}{$gid} = $class || '0'; | ||
| 252 | $self->{'max'} = $class if ($class > $self->{'max'}); | ||
| 253 | return $class; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | =head2 $c->signature | ||
| 259 | |||
| 260 | Returns a vector of all the glyph ids covered by this coverage table or class | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | sub signature | ||
| 265 | { | ||
| 266 | my ($self) = @_; | ||
| 267 | my ($vec, $range, $size); | ||
| 268 | |||
| 269 | if (0) | ||
| 270 | { | ||
| 271 | if ($self->{'cover'}) | ||
| 272 | { $range = 1; $size = 1; } | ||
| 273 | else | ||
| 274 | { | ||
| 275 | $range = $self->{'max'}; | ||
| 276 | $size = 1; | ||
| 277 | while ($range > 1) | ||
| 278 | { | ||
| 279 | $size = $size << 1; | ||
| 280 | $range = $range >> 1; | ||
| 281 | } | ||
| 282 | $range = $self->{'max'} + 1; | ||
| 283 | } | ||
| 284 | foreach (keys %{$self->{'val'}}) | ||
| 285 | { vec($vec, $_, $size) = $self->{'val'}{$_} > $range ? $range : $self->{'val'}{$_}; } | ||
| 286 | length($vec) . ":" . $vec; | ||
| 287 | } | ||
| 288 | $vec = join(";", map{"$_,$self->{'val'}{$_}"} sort { $a <=> $b} keys %{$self->{'val'}}); | ||
| 289 | } | ||
| 290 | |||
| 291 | =head2 @map=$c->sort | ||
| 292 | |||
| 293 | Sorts the coverage table so that indexes are in ascending order of glyphid. | ||
| 294 | Returns a map such that $map[$new_index]=$old_index. | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | sub sort | ||
| 299 | { | ||
| 300 | my ($self) = @_; | ||
| 301 | my (@res, $i); | ||
| 302 | |||
| 303 | foreach (sort {$a <=> $b} keys %{$self->{'val'}}) | ||
| 304 | { | ||
| 305 | push(@res, $self->{'val'}{$_}); | ||
| 306 | $self->{'val'}{$_} = $i++; | ||
| 307 | } | ||
| 308 | @res; | ||
| 309 | } | ||
| 310 | |||
| 311 | =head2 $c->out_xml($context) | ||
| 312 | |||
| 313 | Outputs this coverage/class in XML | ||
| 314 | |||
| 315 | =cut | ||
| 316 | |||
| 317 | sub out_xml | ||
| 318 | { | ||
| 319 | my ($self, $context, $depth) = @_; | ||
| 320 | my ($fh) = $context->{'fh'}; | ||
| 321 | |||
| 322 | $fh->print("$depth<" . ($self->{'cover'} ? 'coverage' : 'class') . ">\n"); | ||
| 323 | foreach $gid (sort {$a <=> $b} keys %{$self->{'val'}}) | ||
| 324 | { | ||
| 325 | $fh->printf("$depth$context->{'indent'}<gref glyph='%s' val='%s'/>\n", $gid, $self->{'val'}{$gid}); | ||
| 326 | } | ||
| 327 | $fh->print("$depth</" . ($self->{'cover'} ? 'coverage' : 'class') . ">\n"); | ||
| 328 | $self; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub release | ||
| 332 | { } | ||
| 333 | |||
| 334 | 1; | ||
| 335 | |||
| 336 | =head1 AUTHOR | ||
| 337 | |||
| 338 | Martin Hosken L<http://scripts.sil.org/FontUtils>. | ||
| 339 | |||
| 340 | |||
| 341 | =head1 LICENSING | ||
| 342 | |||
| 343 | Copyright (c) 1998-2016, SIL International (http://www.sil.org) | ||
| 344 | |||
| 345 | This module is released under the terms of the Artistic License 2.0. | ||
| 346 | For details, see the full text of the license in the file LICENSE. | ||
| 347 | |||
| 348 | |||
| 349 | |||
| 350 | =cut | ||
| 351 |