Font/TTF/OldCmap.pm untested

filter: Statementstmt 0.0% 0 / 197 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 280 CC 25 SCAR 42.2
CC
25
Cov
0%
out
132.0
read
72.0
find_ms
30.0
CRAP 67.7
1 package Font::TTF::OldCmap;
2  
3 =head1 NAME
4  
5 Font::TTF::OldCmap - Character map table
6  
7 This module is deprecated
8  
9 =head1 DESCRIPTION
10  
11 Looks after the character map. The primary structure used for handling a cmap
12 is the L<Font::TTF::Segarr> which handles the segmented arrays of format 4 tables,
13 and in a simpler form for format 0 tables.
14  
15 Due to the complexity of working with segmented arrays, most of the handling of
16 such arrays is via methods rather than via instance variables.
17  
18 One important feature of a format 4 table is that it always contains a segment
19 with a final address of 0xFFFF. If you are creating a table from scratch this is
20 important (although L<Font::TTF::Segarr> can work quite happily without it).
21  
22  
23 =head1 INSTANCE VARIABLES
24  
25 The instance variables listed here are not preceded by a space due to their
26 emulating structural information in the font.
27  
28 =over 4
29  
30 =item Num
31  
32 Number of subtables in this table
33  
34 =item Tables
35  
36 An array of subtables ([0..Num-1])
37  
38 =back
39  
40 Each subtables also has its own instance variables which are, again, not
41 preceded by a space.
42  
43 =over 4
44  
45 =item Platform
46  
47 The platform number for this subtable
48  
49 =item Encoding
50  
51 The encoding number for this subtable
52  
53 =item Format
54  
55 Gives the stored format of this subtable
56  
57 =item Ver
58  
59 Gives the version (or language) information for this subtable
60  
61 =item val
62  
63 This points to a L<Font::TTF::Segarr> which contains the content of the particular
64 subtable.
65  
66 =back
67  
68 =head1 METHODS
69  
70 =cut
71  
72 use strict;
73 use vars qw(@ISA);
74 require Font::TTF::Table;
75 require Font::TTF::Segarr;
76  
77 @ISA = qw(Font::TTF::Table);
78  
79  
80 =head2 $t->read
81  
82 Reads the cmap into memory. Format 4 subtables read the whole subtable and
83 fill in the segmented array accordingly.
84  
85 Format 2 subtables are not read at all.
86  
87 =cut
88  
89 sub read
90 {
91 my ($self) = @_;
92 $self->SUPER::read or return $self;
93  
94 my ($dat, $i, $j, $k, $id, @ids, $s);
95 my ($start, $end, $range, $delta, $form, $len, $num, $ver);
96 my ($fh) = $self->{' INFILE'};
97  
98 $fh->read($dat, 4);
99 $self->{'Num'} = unpack("x2n", $dat);
100 $self->{'Tables'} = [];
101 for ($i = 0; $i < $self->{'Num'}; $i++)
102 {
103 $s = {};
104 $fh->read($dat, 8);
105 ($s->{'Platform'}, $s->{'Encoding'}, $s->{'LOC'}) = (unpack("nnN", $dat));
106 $s->{'LOC'} += $self->{' OFFSET'};
107 push(@{$self->{'Tables'}}, $s);
108 }
109 for ($i = 0; $i < $self->{'Num'}; $i++)
110 {
111 $s = $self->{'Tables'}[$i];
112 $fh->seek($s->{'LOC'}, 0);
113 $fh->read($dat, 6);
114 ($form, $len, $ver) = (unpack("n3", $dat));
115  
116 $s->{'Format'} = $form;
117 $s->{'Ver'} = $ver;
118 if ($form == 0)
119 {
120 $s->{'val'} = Font::TTF::Segarr->new;
121 $fh->read($dat, 256);
122 $s->{'val'}->fastadd_segment(0, 2, unpack("C*", $dat));
123 $s->{'Start'} = 0;
124 $s->{'Num'} = 256;
125 } elsif ($form == 6)
126 {
127 my ($start, $ecount);
128
129 $fh->read($dat, 4);
130 ($start, $ecount) = unpack("n2", $dat);
131 $fh->read($dat, $ecount << 1);
132 $s->{'val'} = Font::TTF::Segarr->new;
133 $s->{'val'}->fastadd_segment($start, 2, unpack("n*", $dat));
134 $s->{'Start'} = $start;
135 $s->{'Num'} = $ecount;
136 } elsif ($form == 2)
137 {
138 # no idea what to do here yet
139 } elsif ($form == 4)
140 {
141 $fh->read($dat, 8);
142 $num = unpack("n", $dat);
143 $num >>= 1;
144 $fh->read($dat, $len - 14);
145 $s->{'val'} = Font::TTF::Segarr->new;
146 for ($j = 0; $j < $num; $j++)
147 {
148 $end = unpack("n", substr($dat, $j << 1, 2));
149 $start = unpack("n", substr($dat, ($j << 1) + ($num << 1) + 2, 2));
150 $delta = unpack("n", substr($dat, ($j << 1) + ($num << 2) + 2, 2));
151 $delta -= 65536 if $delta > 32767;
152 $range = unpack("n", substr($dat, ($j << 1) + $num * 6 + 2, 2));
153 @ids = ();
154 for ($k = $start; $k <= $end; $k++)
155 {
156 if ($range == 0)
157 { $id = $k + $delta; }
158 else
159 { $id = unpack("n", substr($dat, ($j << 1) + $num * 6 +
160 2 + ($k - $start) * 2 + $range, 2)) + $delta; }
161 $id -= 65536 if $id > 65536;
162 push (@ids, $id);
163 }
164 $s->{'val'}->fastadd_segment($start, 0, @ids);
165 }
166 $s->{'val'}->tidy;
167 $s->{'Num'} = 0x10000; # always ends here
168 $s->{'Start'} = $s->{'val'}[0]{'START'};
169 }
170 }
171 $self;
172 }
173  
174  
175 =head2 $t->ms_lookup($uni)
176  
177 Given a Unicode value in the MS table (Platform 3, Encoding 1) locates that
178 table and looks up the appropriate glyph number from it.
179  
180 =cut
181  
182 sub ms_lookup
183 {
184 my ($self, $uni) = @_;
185  
186 $self->find_ms || return undef unless (defined $self->{' mstable'});
187 return $self->{' mstable'}{'val'}->at($uni);
188 }
189  
190  
191 =head2 $t->find_ms
192  
193 Finds the Microsoft Unicode table and sets the C<mstable> instance variable
194 to it if found. Returns the table it finds.
195  
196 =cut
197  
198 sub find_ms
199 {
200 my ($self) = @_;
201 my ($i, $s, $alt);
202  
203 return $self->{' mstable'} if defined $self->{' mstable'};
204 $self->read;
205 for ($i = 0; $i < $self->{'Num'}; $i++)
206 {
207 $s = $self->{'Tables'}[$i];
208 if ($s->{'Platform'} == 3)
209 {
210 $self->{' mstable'} = $s;
211 last if ($s->{'Encoding'} == 1);
212 } elsif ($s->{'Platform'} == 0 || ($s->{'Platform'} == 2 && $s->{'Encoding'} == 1))
213 { $self->{' mstable'} = $s; }
214 }
215 $self->{' mstable'};
216 }
217  
218  
219 =head2 $t->out($fh)
220  
221 Writes out a cmap table to a filehandle. If it has not been read, then
222 just copies from input file to output
223  
224 =cut
225  
226 sub out
227 {
228 my ($self, $fh) = @_;
229 my ($loc, $s, $i, $base_loc, $j);
230  
231 return $self->SUPER::out($fh) unless $self->{' read'};
232  
233 $base_loc = $fh->tell();
234 $fh->print(pack("n2", 0, $self->{'Num'}));
235  
236 for ($i = 0; $i < $self->{'Num'}; $i++)
237 { $fh->print(pack("nnN", $self->{'Tables'}[$i]{'Platform'}, $self->{'Tables'}[$i]{'Encoding'}, 0)); }
238
239 for ($i = 0; $i < $self->{'Num'}; $i++)
240 {
241 $s = $self->{'Tables'}[$i];
242 $s->{'val'}->tidy;
243 $s->{' outloc'} = $fh->tell();
244 $fh->print(pack("n3", $s->{'Format'}, 0, $s->{'Ver'})); # come back for length
245 if ($s->{'Format'} == 0)
246 {
247 $fh->print(pack("C256", $s->{'val'}->at(0, 256)));
248 } elsif ($s->{'Format'} == 6)
249 {
250 $fh->print(pack("n2", $s->{'Start'}, $s->{'Num'}));
251 $fh->print(pack("n*", $s->{'val'}->at($s->{'Start'}, $s->{'Num'})));
252 } elsif ($s->{'Format'} == 2)
253 {
254 } elsif ($s->{'Format'} == 4)
255 {
256 my ($num, $sRange, $eSel);
257 my (@deltas, $delta, @range, $flat, $k, $segs, $count);
258  
259 $num = $#{$s->{'val'}} + 1;
260 $segs = $s->{'val'};
261 for ($sRange = 1, $eSel = 0; $sRange <= $num; $eSel++)
262 { $sRange <<= 1;}
263 $eSel--;
264 $fh->print(pack("n4", $num * 2, $sRange, $eSel, ($num * 2) - $sRange));
265 $fh->print(pack("n*", map {$_->{'START'} + $_->{'LEN'} - 1} @$segs));
266 $fh->print(pack("n", 0));
267 $fh->print(pack("n*", map {$_->{'START'}} @$segs));
268  
269 for ($j = 0; $j < $num; $j++)
270 {
271 $delta = $segs->[$j]{'VAL'}[0]; $flat = 1;
272 for ($k = 1; $k < $segs->[$j]{'LEN'}; $k++)
273 {
274 if ($segs->[$j]{'VAL'}[$k] == 0)
275 { $flat = 0; }
276 if ($delta + $k != $segs->[$j]{'VAL'}[$k])
277 {
278 $delta = 0;
279 last;
280 }
281 }
282 push (@range, $flat);
283 push (@deltas, ($delta ? $delta - $segs->[$j]{'START'} : 0));
284 }
285 $fh->print(pack("n*", @deltas));
286  
287 $count = 0;
288 for ($j = 0; $j < $num; $j++)
289 {
290 $delta = $deltas[$j];
291 if ($delta != 0 && $range[$j] == 1)
292 { $range[$j] = 0; }
293 else
294 {
295 $range[$j] = ($count + $num - $j) << 1;
296 $count += $segs->[$j]{'LEN'};
297 }
298 }
299  
300 $fh->print(pack("n*", @range));
301  
302 for ($j = 0; $j < $num; $j++)
303 {
304 next if ($range[$j] == 0);
305 for ($k = 0; $k < $segs->[$j]{'LEN'}; $k++)
306 { $fh->print(pack("n", $segs->[$j]{'VAL'}[$k])); }
307 }
308 }
309  
310 $loc = $fh->tell();
311 $fh->seek($s->{' outloc'} + 2, 0);
312 $fh->print(pack("n", $loc - $s->{' outloc'}));
313 $fh->seek($base_loc + 8 + ($i << 3), 0);
314 $fh->print(pack("N", $s->{' outloc'} - $base_loc));
315 $fh->seek($loc, 0);
316 }
317 $self;
318 }
319  
320  
321 =head2 @map = $t->reverse([$num])
322  
323 Returns a reverse map of the table of given number or the Microsoft
324 cmap. I.e. given a glyph gives the Unicode value for it.
325  
326 =cut
327  
328 sub reverse
329 {
330 my ($self, $tnum) = @_;
331 my ($table) = defined $tnum ? $self->{'Tables'}[$tnum] : $self->find_ms;
332 my (@res, $i, $s, $first);
333  
334 foreach $s (@{$table->{'val'}})
335 {
336 $first = $s->{'START'};
337 map {$res[$_] = $first unless $res[$_]; $first++;} @{$s->{'VAL'}};
338 }
339 @res;
340 }
341  
342 1;
343  
344 =head1 BUGS
345  
346 =over 4
347  
348 =item *
349  
350 No support for format 2 tables (MBCS)
351  
352 =back
353  
354 =head1 AUTHOR
355  
356 Martin Hosken L<http://scripts.sil.org/FontUtils>.
357  
358  
359 =head1 LICENSING
360  
361 Copyright (c) 1998-2016, SIL International (http://www.sil.org)
362  
363 This module is released under the terms of the Artistic License 2.0.
364 For details, see the full text of the license in the file LICENSE.
365  
366  
367  
368 =cut
369  
370