/usr/share/perl5/Font/TTF/Kern.pm is in libfont-ttf-perl 1.04-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | package Font::TTF::Kern;
=head1 NAME
Font::TTF::Kern - Kerning tables
=head1 DESCRIPTION
Kerning tables are held as an ordered collection of subtables each giving
incremental information regarding the kerning of various pairs of glyphs.
The basic structure of the kerning data structure is:
$kern = $f->{'kern'}{'tables'}[$tnum]{'kerns'}{$leftnum}{$rightnum};
Due to the possible complexity of some kerning tables the above information
is insufficient. Reference also needs to be made to the type of the table and
the coverage field.
=head1 INSTANCE VARIABLES
The instance variables for a kerning table are relatively straightforward.
=over 4
=item Version
Version number of the kerning table
=item Num
Number of subtables in the kerning table
=item tables
Array of subtables in the kerning table
Each subtable has a number of instance variables.
=over 4
=item kern
A two level hash array containing kerning values. The indexing is left
is via left class and right class. It may seem using hashes is strange,
but most tables are not type 2 and this method saves empty array values.
=item type
Stores the table type. Only type 0 and type 2 tables are specified for
TrueType so far.
=item coverage
A bit field of coverage information regarding the kerning value. See the
TrueType specification for details.
=item Version
Contains the version number of the table.
=item Num
Number of kerning pairs in this type 0 table.
=item left
An array indexed by glyph - left_first which returns a class number for
the glyph in type 2 tables.
=item right
An array indexed by glyph - right_first which returns a class number for
the glyph in type 2 tables.
=item left_first
the glyph number of the first element in the left array for type 2 tables.
=item right_first
the glyph number of the first element in the right array for type 2 tables.
=item num_left
Number of left classes
=item num_right
Number of right classes
=back
=back
=head1 METHODS
=cut
use strict;
use vars qw(@ISA);
use Font::TTF::Utils;
use Font::TTF::Table;
use Font::TTF::Kern::Subtable;
@ISA = qw(Font::TTF::Table);
my @subtables = qw(OrderedList StateTable ClassArray CompactClassArray);
=head2 $t->read
Reads the whole kerning table into structures
=cut
sub read
{
my ($self) = @_;
$self->SUPER::read or return $self;
my ($fh) = $self->{' INFILE'};
my ($dat, $i, $numt, $len, $cov, $t);
$fh->read($dat, 4);
($self->{'Version'}, $numt) = unpack("n2", $dat);
if ($self->{'Version'} > 0)
{
$fh->read($dat, 4, 4);
($self->{'Version'}, $numt) = TTF_Unpack("vL", $dat);
}
$self->{'Num'} = $numt;
for ($i = 0; $i < $numt; $i++)
{
if ($self->{'Version'} > 0)
{
$fh->read($dat, 8);
my ($length, $coverage, $index) = unpack("Nnn", $dat);
my ($type) = $coverage & 0xFF;
$t = Font::TTF::Kern::Subtable->create($type, $coverage, $length);
$t->read($fh);
}
else
{
$t = $self->read_subtable($fh);
}
push (@{$self->{'tables'}}, $t);
}
$self;
}
sub read_subtable
{
my ($self, $fh) = @_;
my ($dat, $len, $cov, $t);
$t = {};
$fh->read($dat, 6);
($t->{'Version'}, $len, $cov) = unpack("n3", $dat);
$t->{'coverage'} = $cov & 255;
$t->{'type'} = $cov >> 8;
if ($t->{'Version'} == 0)
{
# NB: Cambria is an example of a font that plays an unsual trick: The
# kern table is much larger than can be represented by the header $len
# would allow. So we use the number of pairs to figure out how much to read.
$fh->read($dat, 8);
$t->{'Num'} = unpack("n", $dat);
$fh->read($dat, $t->{'Num'} * 6);
my (@vals) = unpack("n*", $dat);
for (0 .. ($t->{'Num'} - 1))
{
my ($f, $l, $v);
$f = shift @vals;
$l = shift @vals;
$v = shift @vals;
$v -= 65536 if ($v > 32767);
$t->{'kern'}{$f}{$l} = $v;
}
} elsif ($t->{'Version'} == 2)
{
my ($wid, $off, $numg, $maxl, $maxr, $j);
$fh->read($dat, $len - 6);
$wid = unpack("n", $dat);
$off = unpack("n", substr($dat, 2));
($t->{'left_first'}, $numg) = unpack("n2", substr($dat, $off));
$t->{'left'} = [unpack("n$numg", substr($dat, $off + 4))];
foreach (@{$t->{'left'}})
{
$_ /= $wid;
$maxl = $_ if ($_ > $maxl);
}
$t->{'left_max'} = $maxl;
$off = unpack("n", substr($dat, 4));
($t->{'right_first'}, $numg) = unpack("n2", substr($dat, $off));
$t->{'right'} = [unpack("n$numg", substr($dat, $off + 4))];
foreach (@{$t->{'right'}})
{
$_ >>= 1;
$maxr = $_ if ($_ > $maxr);
}
$t->{'right_max'} = $maxr;
$off = unpack("n", substr($dat, 6));
for ($j = 0; $j <= $maxl; $j++)
{
my ($k) = 0;
map { $t->{'kern'}{$j}{$k} = $_ if $_; $k++; }
unpack("n$maxr", substr($dat, $off + $wid * $j));
}
}
return $t;
}
=head2 $t->out($fh)
Outputs the kerning tables to the given file
=cut
sub out
{
my ($self, $fh) = @_;
my ($i, $l, $r, $t);
return $self->SUPER::out($fh) unless ($self->{' read'});
if ($self->{'Version'} > 0)
{ $fh->print(TTF_Pack("vL", $self->{'Version'}, $self->{'Num'})); }
else
{ $fh->print(pack("n2", $self->{'Version'}, $self->{'Num'})); }
for ($i = 0; $i < $self->{'Num'}; $i++)
{
$t = $self->{'tables'}[$i];
if ($self->{'Version'} > 0)
{ $t->out($fh); }
else
{ $self->out_subtable($fh, $t); }
}
$self;
}
sub out_subtable
{
my ($self, $fh, $t) = @_;
my ($loc) = $fh->tell();
my ($loc1, $l, $r);
$fh->print(pack("nnn", $t->{'Version'}, 0, $t->{'coverage'}));
if ($t->{'Version'} == 0)
{
my ($dat);
foreach $l (sort {$a <=> $b} keys %{$t->{'kern'}})
{
foreach $r (sort {$a <=> $b} keys %{$t->{'kern'}{$l}})
{ $dat .= TTF_Pack("SSs", $l, $r, $t->{'kern'}{$l}{$r}); }
}
$fh->print(TTF_Pack("SSSS", Font::TTF::Utils::TTF_bininfo(length($dat) / 6, 6)));
$fh->print($dat);
} elsif ($t->{'Version'} == 2)
{
my ($arr);
$fh->print(pack("nnnn", $t->{'right_max'} << 1, 8, ($#{$t->{'left'}} + 7) << 1,
($#{$t->{'left'}} + $#{$t->{'right'}} + 10) << 1));
$fh->print(pack("nn", $t->{'left_first'}, $#{$t->{'left'}} + 1));
foreach (@{$t->{'left'}})
{ $fh->print(pack("C", $_ * (($t->{'left_max'} + 1) << 1))); }
$fh->print(pack("nn", $t->{'right_first'}, $#{$t->{'right'}} + 1));
foreach (@{$t->{'right'}})
{ $fh->print(pack("C", $_ << 1)); }
$arr = "\000\000" x (($t->{'left_max'} + 1) * ($t->{'right_max'} + 1));
foreach $l (keys %{$t->{'kern'}})
{
foreach $r (keys %{$t->{'kern'}{$l}})
{ substr($arr, ($l * ($t->{'left_max'} + 1) + $r) << 1, 2)
= pack("n", $t->{'kern'}{$l}{$r}); }
}
$fh->print($arr);
}
$loc1 = $fh->tell();
$fh->seek($loc + 2, 0);
$fh->print(pack("n", $loc1 - $loc));
$fh->seek($loc1, 0);
}
=head2 $t->XML_element($context, $depth, $key, $value)
Handles outputting the kern hash into XML a little more tidily
=cut
sub XML_element
{
my ($self) = shift;
my ($context, $depth, $key, $value) = @_;
my ($fh) = $context->{'fh'};
my ($f, $l);
return $self->SUPER::XML_element(@_) unless ($key eq 'kern');
$fh->print("$depth<kern-table>\n");
foreach $f (sort {$a <=> $b} keys %{$value})
{
foreach $l (sort {$a <=> $b} keys %{$value->{$f}})
{ $fh->print("$depth$context->{'indent'}<adjust first='$f' last='$l' dist='$value->{$f}{$l}'/>\n"); }
}
$fh->print("$depth</kern-table>\n");
$self;
}
=head2 $t->minsize()
Returns the minimum size this table can be. If it is smaller than this, then the table
must be bad and should be deleted or whatever.
=cut
sub minsize
{
return 4;
}
1;
=head1 BUGS
=over 4
=item *
Only supports kerning table types 0 & 2.
=item *
No real support functions to I<do> anything with the kerning tables yet.
=back
=head1 AUTHOR
Martin Hosken L<Martin_Hosken@sil.org>.
=head1 LICENSING
Copyright (c) 1998-2013, SIL International (http://www.sil.org)
This module is released under the terms of the Artistic License 2.0.
For details, see the full text of the license in the file LICENSE.
=cut
|