API3/Compat/API2/Basic/TTF/Kern.pm untested

filter: Statementstmt 0.0% 0 / 137 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 168 CC 10 SCAR 30.9
CC
10
Cov
0%
read
42.0
out
20.0
XML_element
6.0
CRAP 22.0
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: Kern.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $
25 #
26 #=======================================================================
27 package PDF::API3::Compat::API2::Basic::TTF::Kern;
28
29 =head1 NAME
30
31 PDF::API3::Compat::API2::Basic::TTF::Kern - Kerning tables
32
33 =head1 DESCRIPTION
34
35 Kerning tables are held as an ordered collection of subtables each giving
36 incremental information regarding the kerning of various pairs of glyphs.
37
38 The basic structure of the kerning data structure is:
39
40 $kern = $f->{'kern'}{'tables'}[$tnum]{'kerns'}{$leftnum}{$rightnum};
41
42 Due to the possible complexity of some kerning tables the above information
43 is insufficient. Reference also needs to be made to the type of the table and
44 the coverage field.
45
46 =head1 INSTANCE VARIABLES
47
48 The instance variables for a kerning table are relatively straightforward.
49
50 =over 4
51
52 =item Version
53
54 Version number of the kerning table
55
56 =item Num
57
58 Number of subtables in the kerning table
59
60 =item tables
61
62 Array of subtables in the kerning table
63
64 =over 4
65
66 Each subtable has a number of instance variables.
67
68 =item kern
69
70 A two level hash array containing kerning values. The indexing is left
71 value and then right value. In the case of type 2 tables, the indexing
72 is via left class and right class. It may seem using hashes is strange,
73 but most tables are not type 2 and this method saves empty array values.
74
75 =item type
76
77 Stores the table type. Only type 0 and type 2 tables are specified for
78 TrueType so far.
79
80 =item coverage
81
82 A bit field of coverage information regarding the kerning value. See the
83 TrueType specification for details.
84
85 =item Version
86
87 Contains the version number of the table.
88
89 =item Num
90
91 Number of kerning pairs in this type 0 table.
92
93 =item left
94
95 An array indexed by glyph - left_first which returns a class number for
96 the glyph in type 2 tables.
97
98 =item right
99
100 An array indexed by glyph - right_first which returns a class number for
101 the glyph in type 2 tables.
102
103 =item left_first
104
105 the glyph number of the first element in the left array for type 2 tables.
106
107 =item right_first
108
109 the glyph number of the first element in the right array for type 2 tables.
110
111 =item num_left
112
113 Number of left classes
114
115 =item num_right
116
117 Number of right classes
118
119 =back
120
121 =back
122
123 =head1 METHODS
124
125 =cut
126
127 use strict;
128 use vars qw(@ISA);
129 use PDF::API3::Compat::API2::Basic::TTF::Utils;
130 use PDF::API3::Compat::API2::Basic::TTF::Table;
131
132 @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Table);
133
134 =head2 $t->read
135
136 Reads the whole kerning table into structures
137
138 =cut
139
140 sub read
141 {
142 my ($self) = @_;
143 my ($fh) = $self->{' INFILE'};
144 my ($dat, $i, $numt, $len, $cov, $t);
145
146 $self->SUPER::read or return $self;
147
148 $fh->read($dat, 4);
149 ($self->{'Version'}, $numt) = unpack("n2", $dat);
150 $self->{'Num'} = $numt;
151
152 for ($i = 0; $i < $numt; $i++)
153 {
154 $t = {};
155 $fh->read($dat, 6);
156 ($t->{'Version'}, $len, $cov) = unpack("n3", $dat);
157 $t->{'coverage'} = $cov & 255;
158 $t->{'type'} = $cov >> 8;
159 $fh->read($dat, $len - 6);
160 if ($t->{'Version'} == 0)
161 {
162 my ($j);
163
164 $t->{'Num'} = unpack("n", $dat);
165 for ($j = 0; $j < $t->{'Num'}; $j++)
166 {
167 my ($f, $l, $v) = TTF_Unpack("SSs", substr($dat, $j * 6 + 8, 6));
168 $t->{'kern'}{$f}{$l} = $v;
169 }
170 } elsif ($t->{'Version'} == 2)
171 {
172 my ($wid, $off, $numg, $maxl, $maxr, $j);
173
174 $wid = unpack("n", $dat);
175 $off = unpack("n", substr($dat, 2));
176 ($t->{'left_first'}, $numg) = unpack("n2", substr($dat, $off));
177 $t->{'left'} = [unpack("C$numg", substr($dat, $off + 4))];
178 foreach (@{$t->{'left'}})
179 {
180 $_ /= $wid;
181 $maxl = $_ if ($_ > $maxl);
182 }
183 $t->{'left_max'} = $maxl;
184
185 $off = unpack("n", substr($dat, 4));
186 ($t->{'right_first'}, $numg) = unpack("n2", substr($dat, $off));
187 $t->{'right'} = [unpack("C$numg", substr($dat, $off + 4))];
188 foreach (@{$t->{'right'}})
189 {
190 $_ >>= 1;
191 $maxr = $_ if ($_ > $maxr);
192 }
193 $t->{'right_max'} = $maxr;
194
195 $off = unpack("n", substr($dat, 6));
196 for ($j = 0; $j <= $maxl; $j++)
197 {
198 my ($k) = 0;
199
200 map { $t->{'kern'}{$j}{$k} = $_ if $_; $k++; }
201 unpack("n$maxr", substr($dat, $off + $wid * $j));
202 }
203 }
204 push (@{$self->{'tables'}}, $t);
205 }
206 $self;
207 }
208
209
210 =head2 $t->out($fh)
211
212 Outputs the kerning tables to the given file
213
214 =cut
215
216 sub out
217 {
218 my ($self, $fh) = @_;
219 my ($i, $l, $r, $loc, $loc1, $t);
220
221 return $self->SUPER::out($fh) unless ($self->{' read'});
222
223 $fh->print(pack("n2", $self->{'Version'}, $self->{'Num'}));
224 for ($i = 0; $i < $self->{'Num'}; $i++)
225 {
226 $t = $self->{'tables'}[$i];
227 $loc = $fh->tell();
228
229 $fh->print(pack("nnn", $t->{'Version'}, 0, $t->{'coverage'}));
230 if ($t->{'Version'} == 0)
231 {
232 my ($dat);
233 foreach $l (sort {$a <=> $b} keys %{$t->{'kern'}})
234 {
235 foreach $r (sort {$a <=> $b} keys %{$t->{'kern'}{$l}})
236 { $dat .= TTF_Pack("SSs", $l, $r, $t->{'kern'}{$l}{$r}); }
237 }
238 $fh->print(TTF_Pack("SSSS", PDF::API3::Compat::API2::Basic::TTF::Utils::TTF_bininfo(length($dat) / 6, 6)));
239 $fh->print($dat);
240 } elsif ($t->{'Version'} == 2)
241 {
242 my ($arr);
243
244 $fh->print(pack("nnnn", $t->{'right_max'} << 1, 8, ($#{$t->{'left'}} + 7) << 1,
245 ($#{$t->{'left'}} + $#{$t->{'right'}} + 10) << 1));
246
247 $fh->print(pack("nn", $t->{'left_first'}, $#{$t->{'left'}} + 1));
248 foreach (@{$t->{'left'}})
249 { $fh->print(pack("C", $_ * (($t->{'left_max'} + 1) << 1))); }
250
251 $fh->print(pack("nn", $t->{'right_first'}, $#{$t->{'right'}} + 1));
252 foreach (@{$t->{'right'}})
253 { $fh->print(pack("C", $_ << 1)); }
254
255 $arr = "\000\000" x (($t->{'left_max'} + 1) * ($t->{'right_max'} + 1));
256 foreach $l (keys %{$t->{'kern'}})
257 {
258 foreach $r (keys %{$t->{'kern'}{$l}})
259 { substr($arr, ($l * ($t->{'left_max'} + 1) + $r) << 1, 2)
260 = pack("n", $t->{'kern'}{$l}{$r}); }
261 }
262 $fh->print($arr);
263 }
264 $loc1 = $fh->tell();
265 $fh->seek($loc + 2, 0);
266 $fh->print(pack("n", $loc1 - $loc));
267 $fh->seek($loc1, 0);
268 }
269 $self;
270 }
271
272
273 =head2 $t->XML_element($context, $depth, $key, $value)
274
275 Handles outputting the kern hash into XML a little more tidily
276
277 =cut
278
279 sub XML_element
280 {
281 my ($self) = shift;
282 my ($context, $depth, $key, $value) = @_;
283 my ($fh) = $context->{'fh'};
284 my ($f, $l);
285
286 return $self->SUPER::XML_element(@_) unless ($key eq 'kern');
287 $fh->print("$depth<kern-table>\n");
288 foreach $f (sort {$a <=> $b} keys %{$value})
289 {
290 foreach $l (sort {$a <=> $b} keys %{$value->{$f}})
291 { $fh->print("$depth$context->{'indent'}<adjust first='$f' last='$l' dist='$value->{$f}{$l}'/>\n"); }
292 }
293 $fh->print("$depth</kern-table>\n");
294 $self;
295 }
296
297 1;
298
299 =head1 BUGS
300
301 =over 4
302
303 =item *
304
305 Only supports kerning table types 0 & 2.
306
307 =item *
308
309 No real support functions to I<do> anything with the kerning tables yet.
310
311 =back
312
313 =head1 AUTHOR
314
315 Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and
316 licensing.
317
318 =cut
319