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

filter: Statementstmt 0.0% 0 / 268 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 61 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 488 CC 52 SCAR 55.9
CC
52
Cov
0%
out
552.0
read
210.0
find_ms
110.0
CRAP 267.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: Cmap.pm,v 2.1 2007/08/27 20:06:02 areibens Exp $
25 #
26 #=======================================================================
27 package PDF::API3::Compat::API2::Basic::TTF::Cmap;
28
29 =head1 NAME
30
31 PDF::API3::Compat::API2::Basic::TTF::Cmap - Character map table
32
33 =head1 DESCRIPTION
34
35 Looks after the character map. For ease of use, the actual cmap is held in
36 a hash against codepoint. Thus for a given table:
37
38 $gid = $font->{'cmap'}{'Tables'}[0]{'val'}{$code};
39
40 Note that C<$code> should be a true value (0x1234) rather than a string representation.
41
42 =head1 INSTANCE VARIABLES
43
44 The instance variables listed here are not preceeded by a space due to their
45 emulating structural information in the font.
46
47 =over 4
48
49 =item Num
50
51 Number of subtables in this table
52
53 =item Tables
54
55 An array of subtables ([0..Num-1])
56
57 =back
58
59 Each subtables also has its own instance variables which are, again, not
60 preceeded by a space.
61
62 =over 4
63
64 =item Platform
65
66 The platform number for this subtable
67
68 =item Encoding
69
70 The encoding number for this subtable
71
72 =item Format
73
74 Gives the stored format of this subtable
75
76 =item Ver
77
78 Gives the version (or language) information for this subtable
79
80 =item val
81
82 A hash keyed by the codepoint value (not a string) storing the glyph id
83
84 =back
85
86 =head1 METHODS
87
88 =cut
89
90 use strict;
91 use vars qw(@ISA);
92 use PDF::API3::Compat::API2::Basic::TTF::Table;
93 use PDF::API3::Compat::API2::Basic::TTF::Utils;
94
95 @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Table);
96
97
98 =head2 $t->read
99
100 Reads the cmap into memory. Format 4 subtables read the whole subtable and
101 fill in the segmented array accordingly.
102
103 Format 2 subtables are not read at all.
104
105 =cut
106
107 sub read
108 {
109 my ($self) = @_;
110 my ($dat, $i, $j, $k, $id, @ids, $s);
111 my ($start, $end, $range, $delta, $form, $len, $num, $ver, $sg);
112 my ($fh) = $self->{' INFILE'};
113
114 $self->SUPER::read or return $self;
115 $fh->read($dat, 4);
116 $self->{'Num'} = unpack("x2n", $dat);
117 $self->{'Tables'} = [];
118 for ($i = 0; $i < $self->{'Num'}; $i++)
119 {
120 $s = {};
121 $fh->read($dat, 8);
122 ($s->{'Platform'}, $s->{'Encoding'}, $s->{'LOC'}) = (unpack("nnN", $dat));
123 $s->{'LOC'} += $self->{' OFFSET'};
124 push(@{$self->{'Tables'}}, $s);
125 }
126 for ($i = 0; $i < $self->{'Num'}; $i++)
127 {
128 $s = $self->{'Tables'}[$i];
129 $fh->seek($s->{'LOC'}, 0);
130 $fh->read($dat, 2);
131 $form = unpack("n", $dat);
132
133 $s->{'Format'} = $form;
134 if ($form == 0)
135 {
136 my ($j) = 0;
137
138 $fh->read($dat, 4);
139 ($len, $s->{'Ver'}) = unpack('n2', $dat);
140 $fh->read($dat, 256);
141 $s->{'val'} = {map {$j++; ($_ ? ($j - 1, $_) : ())} unpack("C*", $dat)};
142 } elsif ($form == 6)
143 {
144 my ($start, $ecount);
145
146 $fh->read($dat, 8);
147 ($len, $s->{'Ver'}, $start, $ecount) = unpack('n4', $dat);
148 $fh->read($dat, $ecount << 1);
149 $s->{'val'} = {map {$start++; ($_ ? ($start - 1, $_) : ())} unpack("n*", $dat)};
150 } elsif ($form == 2)
151 {
152 # no idea what to do here yet
153 } elsif ($form == 4)
154 {
155 $fh->read($dat, 12);
156 ($len, $s->{'Ver'}, $num) = unpack('n3', $dat);
157 $num >>= 1;
158 $fh->read($dat, $len - 14);
159 for ($j = 0; $j < $num; $j++)
160 {
161 $end = unpack("n", substr($dat, $j << 1, 2));
162 $start = unpack("n", substr($dat, ($j << 1) + ($num << 1) + 2, 2));
163 $delta = unpack("n", substr($dat, ($j << 1) + ($num << 2) + 2, 2));
164 $delta -= 65536 if $delta > 32767;
165 $range = unpack("n", substr($dat, ($j << 1) + $num * 6 + 2, 2));
166 for ($k = $start; $k <= $end; $k++)
167 {
168 if ($range == 0 || $range == 65535) # support the buggy FOG with its range=65535 for final segment
169 { $id = $k + $delta; }
170 else
171 { $id = unpack("n", substr($dat, ($j << 1) + $num * 6 +
172 2 + ($k - $start) * 2 + $range, 2)) + $delta; }
173 $id -= 65536 if $id >= 65536;
174 $s->{'val'}{$k} = $id if ($id);
175 }
176 }
177 } elsif ($form == 8 || $form == 12)
178 {
179 $fh->read($dat, 10);
180 ($len, $s->{'Ver'}) = unpack('x2N2', $dat);
181 if ($form == 8)
182 {
183 $fh->read($dat, 8196);
184 $num = unpack("N", substr($dat, 8192, 4)); # don't need the map
185 } else
186 {
187 $fh->read($dat, 4);
188 $num = unpack("N", $dat);
189 }
190 $fh->read($dat, 12 * $num);
191 for ($j = 0; $j < $num; $j++)
192 {
193 ($start, $end, $sg) = unpack("N3", substr($dat, $j * 12, 12));
194 for ($k = $start; $k <= $end; $k++)
195 { $s->{'val'}{$k} = $sg++; }
196 }
197 } elsif ($form == 10)
198 {
199 $fh->read($dat, 18);
200 ($len, $s->{'Ver'}, $start, $num) = unpack('x2N4', $dat);
201 $fh->read($dat, $num << 1);
202 for ($j = 0; $j < $num; $j++)
203 { $s->{'val'}{$start + $j} = unpack("n", substr($dat, $j << 1, 2)); }
204 }
205 }
206 $self;
207 }
208
209
210 =head2 $t->ms_lookup($uni)
211
212 Finds a Unicode table, giving preference to the MS one, and looks up the given
213 Unicode codepoint in it to find the glyph id.
214
215 =cut
216
217 sub ms_lookup
218 {
219 my ($self, $uni) = @_;
220
221 $self->find_ms || return undef unless (defined $self->{' mstable'});
222 return $self->{' mstable'}{'val'}{$uni};
223 }
224
225
226 =head2 $t->find_ms
227
228 Finds the a Unicode table, giving preference to the Microsoft one, and sets the C<mstable> instance variable
229 to it if found. Returns the table it finds.
230
231 =cut
232
233
234 sub find_ms
235 {
236 my ($self,$iso) = @_;
237 my ($i, $s, $alt, $found);
238 my %cmap_pref=(
239 '3:10' => 100,
240 '3:1' => 75,
241 '3:0' => 25,
242 '2:0' => 50,
243 '0:4' => 80,
244 '0:3' => 30,
245 );
246
247 return($self->{' mstable'}) if(defined $self->{' mstable'});
248
249 $self->read unless($self->{' read'});
250
251 $self->{' mstable'} = $self->{Tables}[0];
252 $self->{' mstable'}->{Pref}=$cmap_pref{$self->{' mstable'}->{Platform}.':'.$self->{' mstable'}->{Encoding}};
253 foreach $i (0..($self->{Num}-1))
254 {
255 $s = $self->{Tables}[$i];
256 $s->{Pref}=$cmap_pref{$s->{Platform}.':'.$s->{Encoding}};
257
258 if($s->{Pref} > $self->{' mstable'}->{Pref})
259 {
260 $self->{' mstable'} = $s;
261 $found = 1;
262 }
263 elsif (($s->{'Platform'} == 3 && ($s->{'Encoding'} == 1 || $s->{'Encoding'} == 0))
264 || $s->{'Platform'} == 0
265 || ($s->{'Platform'} == 2 && $s->{'Encoding'} == 1))
266 {
267 $alt = $s;
268 }
269 }
270 $self->{' mstable'} = $alt unless $found;
271
272 if($iso)
273 {
274 foreach $i (0..($self->{Num}-1))
275 {
276 if ($s->{'Platform'} == 0)
277 {
278 if(!$self->{' mstable'} || $self->{' mstable'}->{'Platform'} != 0)
279 {
280 $self->{' mstable'} = $s;
281 }
282 else
283 {
284 $self->{' mstable'} = $s if($s->{'Encoding'} >= $self->{' mstable'}->{'Encoding'});
285 }
286 }
287 }
288 }
289
290 return($self->{' mstable'});
291 }
292
293
294 =head2 $t->ms_enc
295
296 Returns the encoding of the microsoft table (0 => symbol, etc.). Returns undef if there is
297 no Microsoft cmap.
298
299 =cut
300
301 sub ms_enc
302 {
303 my ($self) = @_;
304 my ($s);
305
306 return $self->{' mstable'}{'Encoding'}
307 if (defined $self->{' mstable'} && $self->{' mstable'}{'Platform'} == 3);
308
309 foreach $s (@{$self->{'Tables'}})
310 {
311 return $s->{'Encoding'} if ($s->{'Platform'} == 3);
312 }
313 return undef;
314 }
315
316
317 =head2 $t->out($fh)
318
319 Writes out a cmap table to a filehandle. If it has not been read, then
320 just copies from input file to output
321
322 =cut
323
324 sub out
325 {
326 my ($self, $fh) = @_;
327 my ($loc, $s, $i, $base_loc, $j, @keys);
328
329 return $self->SUPER::out($fh) unless $self->{' read'};
330
331 $base_loc = $fh->tell();
332 $fh->print(pack("n2", 0, $self->{'Num'}));
333
334 for ($i = 0; $i < $self->{'Num'}; $i++)
335 { $fh->print(pack("nnN", $self->{'Tables'}[$i]{'Platform'}, $self->{'Tables'}[$i]{'Encoding'}, 0)); }
336
337 for ($i = 0; $i < $self->{'Num'}; $i++)
338 {
339 $s = $self->{'Tables'}[$i];
340 @keys = sort {$a <=> $b} keys %{$s->{'val'}};
341 $s->{' outloc'} = $fh->tell();
342 if ($s->{'Format'} < 8)
343 { $fh->print(pack("n3", $s->{'Format'}, 0, $s->{'Ver'})); } # come back for length
344 else
345 { $fh->print(pack("n2N2", $s->{'Format'}, 0, 0, $s->{'Ver'})); }
346
347 if ($s->{'Format'} == 0)
348 {
349 $fh->print(pack("C256", @{$s->{'val'}}{0 .. 255}));
350 } elsif ($s->{'Format'} == 6)
351 {
352 $fh->print(pack("n2", $keys[0], $keys[-1] - $keys[0] + 1));
353 $fh->print(pack("n*", @{$s->{'val'}}{$keys[0] .. $keys[-1]}));
354 } elsif ($s->{'Format'} == 2)
355 {
356 } elsif ($s->{'Format'} == 4)
357 {
358 my ($num, $sRange, $eSel, $eShift, @starts, @ends, $doff);
359 my (@deltas, $delta, @range, $flat, $k, $segs, $count, $newseg, $v);
360
361 push(@keys, 0xFFFF) unless ($keys[-1] == 0xFFFF);
362 $newseg = 1; $num = 0;
363 for ($j = 0; $j <= $#keys && $keys[$j] <= 0xFFFF; $j++)
364 {
365 $v = $s->{'val'}{$keys[$j]};
366 if ($newseg)
367 {
368 $delta = $v;
369 $doff = $j;
370 $flat = 1;
371 push(@starts, $keys[$j]);
372 $newseg = 0;
373 }
374 $delta = 0 if ($delta + $j - $doff != $v);
375 $flat = 0 if ($v == 0);
376 if ($j == $#keys || $keys[$j] + 1 != $keys[$j+1])
377 {
378 push (@ends, $keys[$j]);
379 push (@deltas, $delta ? $delta - $keys[$doff] : 0);
380 push (@range, $flat);
381 $num++;
382 $newseg = 1;
383 }
384 }
385
386 ($num, $sRange, $eSel, $eShift) = PDF::API3::Compat::API2::Basic::TTF::Utils::TTF_bininfo($num, 2);
387 $fh->print(pack("n4", $num * 2, $sRange, $eSel, $eShift));
388 $fh->print(pack("n*", @ends));
389 $fh->print(pack("n", 0));
390 $fh->print(pack("n*", @starts));
391 $fh->print(pack("n*", @deltas));
392
393 $count = 0;
394 for ($j = 0; $j < $num; $j++)
395 {
396 $delta = $deltas[$j];
397 if ($delta != 0 && $range[$j] == 1)
398 { $range[$j] = 0; }
399 else
400 {
401 $range[$j] = ($count + $num - $j) << 1;
402 $count += $ends[$j] - $starts[$j] + 1;
403 }
404 }
405
406 $fh->print(pack("n*", @range));
407
408 for ($j = 0; $j < $num; $j++)
409 {
410 next if ($range[$j] == 0);
411 $fh->print(pack("n*", @{$s->{'val'}}{$starts[$j] .. $ends[$j]}));
412 }
413 } elsif ($s->{'Format'} == 8 || $s->{'Format'} == 12)
414 {
415 my (@jobs, $start, $current, $curr_glyf, $map);
416
417 $map = "\000" x 8192;
418 foreach $j (@keys)
419 {
420 if ($j > 0xFFFF)
421 {
422 if (defined $s->{'val'}{$j >> 16})
423 { $s->{'Format'} = 12; }
424 vec($map, $j >> 16, 1) = 1;
425 }
426 if ($j != $current + 1 || $s->{'val'}{$j} != $curr_glyf + 1)
427 {
428 push (@jobs, [$start, $current, $curr_glyf]) if (defined $start);
429 $start = $j; $current = $j; $curr_glyf = $s->{'val'}{$j};
430 }
431 }
432 $fh->print($map) if ($s->{'Format'} == 8);
433 $fh->print(pack('N', $#jobs + 1));
434 foreach $j (@jobs)
435 { $fh->print(pack('N3', @{$j})); }
436 } elsif ($s->{'Format'} == 10)
437 {
438 $fh->print(pack('N2', $keys[0], $keys[-1] - $keys[0] + 1));
439 $fh->print(pack('n*', $s->{'val'}{$keys[0] .. $keys[-1]}));
440 }
441
442 $loc = $fh->tell();
443 if ($s->{'Format'} < 8)
444 {
445 $fh->seek($s->{' outloc'} + 2, 0);
446 $fh->print(pack("n", $loc - $s->{' outloc'}));
447 } else
448 {
449 $fh->seek($s->{' outloc'} + 4, 0);
450 $fh->print(pack("N", $loc - $s->{' outloc'}));
451 }
452 $fh->seek($base_loc + 8 + ($i << 3), 0);
453 $fh->print(pack("N", $s->{' outloc'} - $base_loc));
454 $fh->seek($loc, 0);
455 }
456 $self;
457 }
458
459
460 =head2 $t->XML_element($context, $depth, $name, $val)
461
462 Outputs the elements of the cmap in XML. We only need to process val here
463
464 =cut
465
466 sub XML_element
467 {
468 my ($self, $context, $depth, $k, $val) = @_;
469 my ($fh) = $context->{'fh'};
470 my ($i);
471
472 return $self if ($k eq 'LOC');
473 return $self->SUPER::XML_element($context, $depth, $k, $val) unless ($k eq 'val');
474
475 $fh->print("$depth<mappings>\n");
476 foreach $i (sort {$a <=> $b} keys %{$val})
477 { $fh->printf("%s<map code='%04X' glyph='%s'/>\n", $depth . $context->{'indent'}, $i, $val->{$i}); }
478 $fh->print("$depth</mappings>\n");
479 $self;
480 }
481
482 =head2 @map = $t->reverse([$num])
483
484 Returns a reverse map of the table of given number or the Unicode
485 cmap. I.e. given a glyph gives the Unicode value for it.
486
487 =cut
488
489 sub reverse
490 {
491 my ($self, $tnum) = @_;
492 my ($table) = defined $tnum ? $self->{'Tables'}[$tnum] : $self->find_ms;
493 my (@res, $code, $gid);
494
495 while (($code, $gid) = each(%{$table->{'val'}}))
496 { $res[$gid] = $code unless (defined $res[$gid] && $res[$gid] > 0 && $res[$gid] < $code); }
497 @res;
498 }
499
500
501 =head2 is_unicode($index)
502
503 Returns whether the table of a given index is known to be a unicode table
504 (as specified in the specifications)
505
506 =cut
507
508 sub is_unicode
509 {
510 my ($self, $index) = @_;
511 my ($pid, $eid) = ($self->{'Tables'}[$index]{'Platform'}, $self->{'Tables'}[$index]{'Encoding'});
512
513 return ($pid == 3 || $pid == 0 || ($pid == 2 && $eid == 1));
514 }
515
516 1;
517
518 =head1 BUGS
519
520 =over 4
521
522 =item *
523
524 No support for format 2 tables (MBCS)
525
526 =back
527
528 =head1 AUTHOR
529
530 Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and
531 licensing.
532
533 =cut
534