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