Font/TTF/Kern.pm untested

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