File Coverage

blib/lib/PDF/API3/Compat/API2/Resource/Font.pm
Criterion Covered Total %
statement 26 156 16.6
branch 0 54 0.0
condition 0 17 0.0
subroutine 9 12 75.0
pod 1 3 33.3
total 36 242 14.8


line stmt bran cond sub pod time code
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             # Copyright 1999-2005 Alfred Reibenschuh .
12             #
13             #=======================================================================
14             #
15             # This library is free software; you can redistribute it and/or
16             # modify it under the terms of the GNU Lesser General Public
17             # License as published by the Free Software Foundation; either
18             # version 2 of the License, or (at your option) any later version.
19             #
20             # This library is distributed in the hope that it will be useful,
21             # but WITHOUT ANY WARRANTY; without even the implied warranty of
22             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23             # Lesser General Public License for more details.
24             #
25             # You should have received a copy of the GNU Lesser General Public
26             # License along with this library; if not, write to the
27             # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28             # Boston, MA 02111-1307, USA.
29             #
30             # $Id: Font.pm,v 2.2 2008/11/04 23:54:51 areibens Exp $
31             #
32             #=======================================================================
33             package PDF::API3::Compat::API2::Resource::Font;
34            
35             BEGIN {
36            
37 1     1   5 use utf8;
  1         2  
  1         6  
38 1     1   31 use Encode qw(:all);
  1         3  
  1         370  
39            
40 1     1   8 use PDF::API3::Compat::API2::Util;
  1         2  
  1         228  
41 1     1   6 use PDF::API3::Compat::API2::Basic::PDF::Utils;
  1         2  
  1         147  
42 1     1   783 use PDF::API3::Compat::API2::Resource::BaseFont;
  1         4  
  1         51  
43            
44 1     1   10 use POSIX;
  1         21  
  1         11  
45            
46 1     1   3085 use vars qw(@ISA $VERSION);
  1         2  
  1         115  
47            
48 1     1   25 @ISA = qw( PDF::API3::Compat::API2::Resource::BaseFont );
49            
50 1         33 ( $VERSION ) = sprintf '%i.%03i', split(/\./,('$Revision: 2.2 $' =~ /Revision: (\S+)\s/)[0]); # $Date: 2008/11/04 23:54:51 $
51            
52             }
53 1     1   5 no warnings qw[ deprecated recursion uninitialized ];
  1         3  
  1         1658  
54            
55             =item $font->encodeByData $encoding
56            
57             Encodes the font in the specified byte-encoding.
58            
59             =cut
60            
61             sub encodeByData {
62 0     0 1   my ($self,$encoding)=@_;
63 0           my $data=$self->data;
64            
65 0           my ($firstChar,$lastChar);
66            
67 0 0 0       if($self->issymbol || $encoding eq 'asis') {
68 0           $encoding=undef;
69             }
70            
71 0 0 0       if(defined $encoding && $encoding=~m|^uni(\d+)$|o)
    0          
    0          
72             {
73 0           my $blk=$1;
74 0           $data->{e2u}=[ map { $blk*256+$_ } (0..255) ];
  0            
75 0 0         $data->{e2n}=[ map { nameByUni($_) || '.notdef' } @{$data->{e2u}} ];
  0            
  0            
76 0           $data->{firstchar} = 0;
77             }
78             elsif(defined $encoding)
79             {
80 0           $data->{e2u}=[ unpack('U*',decode($encoding,pack('C*',(0..255)))) ];
81 0 0         $data->{e2n}=[ map { nameByUni($_) || '.notdef' } @{$data->{e2u}} ];
  0            
  0            
82             }
83             elsif(defined $data->{uni})
84             {
85 0           $data->{e2u}=[ @{$data->{uni}} ];
  0            
86 0 0         $data->{e2n}=[ map { $_ || '.notdef' } @{$data->{char}} ];
  0            
  0            
87             }
88             else
89             {
90 0           $data->{e2u}=[ map { uniByName($_) } @{$data->{char}} ];
  0            
  0            
91 0 0         $data->{e2n}=[ map { $_ || '.notdef' } @{$data->{char}} ];
  0            
  0            
92             }
93            
94 0           $data->{u2c}={};
95 0           $data->{u2e}={};
96 0           $data->{u2n}={};
97 0           $data->{n2c}={};
98 0           $data->{n2e}={};
99 0           $data->{n2u}={};
100            
101 0           foreach my $n (0..255) {
102 0           my $xchar=undef;
103 0           my $xuni=undef;
104 0 0         if(defined $data->{char}->[$n]) {
105 0           $xchar=$data->{char}->[$n];
106             } else {
107 0           $xchar='.notdef';
108             }
109 0 0         $data->{n2c}->{$xchar}=$n unless(defined $data->{n2c}->{$xchar});
110            
111 0 0         if(defined $data->{e2n}->[$n]) {
112 0           $xchar=$data->{e2n}->[$n];
113             } else {
114 0           $xchar='.notdef';
115             }
116 0 0         $data->{n2e}->{$xchar}=$n unless(defined $data->{n2e}->{$xchar});
117            
118 0 0         $data->{n2u}->{$xchar}=$data->{e2u}->[$n] unless(defined $data->{n2u}->{$xchar});
119            
120 0 0         if(defined $data->{char}->[$n]) {
121 0           $xchar=$data->{char}->[$n];
122             } else {
123 0           $xchar='.notdef';
124             }
125 0 0         if(defined $data->{uni}->[$n]) {
126 0           $xuni=$data->{uni}->[$n];
127             } else {
128 0           $xuni=0;
129             }
130 0 0         $data->{n2u}->{$xchar}=$xuni unless(defined $data->{n2u}->{$xchar});
131            
132 0 0 0       $data->{u2c}->{$xuni}||=$n unless(defined $data->{u2c}->{$xuni});
133            
134 0 0         if(defined $data->{e2u}->[$n]) {
135 0           $xuni=$data->{e2u}->[$n];
136             } else {
137 0           $xuni=0;
138             }
139 0 0 0       $data->{u2e}->{$xuni}||=$n unless(defined $data->{u2e}->{$xuni});
140            
141 0 0         if(defined $data->{e2n}->[$n]) {
142 0           $xchar=$data->{e2n}->[$n];
143             } else {
144 0           $xchar='.notdef';
145             }
146 0 0         $data->{u2n}->{$xuni}=$xchar unless(defined $data->{u2n}->{$xuni});
147            
148 0 0         if(defined $data->{char}->[$n]) {
149 0           $xchar=$data->{char}->[$n];
150             } else {
151 0           $xchar='.notdef';
152             }
153 0 0         if(defined $data->{uni}->[$n]) {
154 0           $xuni=$data->{uni}->[$n];
155             } else {
156 0           $xuni=0;
157             }
158 0 0         $data->{u2n}->{$xuni}=$xchar unless(defined $data->{u2n}->{$xuni});
159             }
160            
161 0           my $en = PDFDict();
162 0           $self->{Encoding}=$en;
163            
164 0           $en->{Type}=PDFName('Encoding');
165 0           $en->{BaseEncoding}=PDFName('WinAnsiEncoding');
166            
167 0           $en->{Differences}=PDFArray(PDFNum(0));
168 0           foreach my $n (0..255) {
169 0   0       $en->{Differences}->add_elements( PDFName($self->glyphByEnc($n) || '.notdef') );
170             }
171            
172 0           $self->{'FirstChar'} = PDFNum($data->{firstchar});
173 0           $self->{'LastChar'} = PDFNum($data->{lastchar});
174            
175 0           $self->{Widths}=PDFArray();
176 0           foreach my $n ($data->{firstchar}..$data->{lastchar}) {
177 0           $self->{Widths}->add_elements( PDFNum($self->wxByEnc($n)) );
178             }
179            
180             #use Data::Dumper;
181             # print Dumper($data);
182            
183 0           return($self);
184             }
185            
186             sub automap {
187 0     0 0   my ($self)=@_;
188 0           my $data=$self->data;
189            
190 0           my %gl=map { $_=>defineName($_) } keys %{$data->{wx}};
  0            
  0            
191            
192 0           foreach my $n (0..255) {
193 0           delete $gl{$data->{e2n}->[$n]};
194             }
195            
196 0 0 0       if(defined $data->{comps} && !$self->{-nocomps})
197             {
198 0           foreach my $n (keys %{$data->{comps}})
  0            
199             {
200 0           delete $gl{$n};
201             }
202             }
203            
204 0           my @nm=sort { $gl{$a} <=> $gl{$b} } keys %gl;
  0            
205            
206 0           my @fnts=();
207 0           my $count=0;
208 0           while(@glyphs=splice(@nm,0,223))
209             {
210 0           my $obj=$self->SUPER::new($self->{' apipdf'},$self->name.'am'.$count);
211 0           $obj->{' data'}={ %{$data} };
  0            
212 0           $obj->data->{firstchar}=32;
213 0           $obj->data->{lastchar}=32+scalar(@glyphs);
214 0           push @fnts,$obj;
215 0           foreach my $key (qw( Subtype BaseFont FontDescriptor ))
216             {
217 0 0         $obj->{$key}=$self->{$key} if(defined $self->{$key});
218             }
219 0           $obj->data->{char}=[];
220 0           $obj->data->{uni}=[];
221 0           foreach my $n (0..31)
222             {
223 0           $obj->data->{char}->[$n]='.notdef';
224 0           $obj->data->{uni}->[$n]=0;
225             }
226 0           $obj->data->{char}->[32]='space';
227 0           $obj->data->{uni}->[32]=32;
228 0           foreach my $n (33..$obj->data->{lastchar})
229             {
230 0           $obj->data->{char}->[$n]=$glyphs[$n-33];
231 0           $obj->data->{uni}->[$n]=$gl{$glyphs[$n-33]};
232             }
233 0           foreach my $n (($obj->data->{lastchar}+1)..255)
234             {
235 0           $obj->data->{char}->[$n]='.notdef';
236 0           $obj->data->{uni}->[$n]=0;
237             }
238 0           $obj->encodeByData(undef);
239            
240 0           $count++;
241             }
242            
243 0           return(@fnts);
244             }
245            
246             sub remap {
247 0     0 0   my ($self,$enc)=@_;
248            
249 0           my $obj=$self->SUPER::new($self->{' apipdf'},$self->name.'rm'.pdfkey());
250 0           $obj->{' data'}={ %{$self->data} };
  0            
251 0           foreach my $key (qw( Subtype BaseFont FontDescriptor )) {
252 0 0         $obj->{$key}=$self->{$key} if(defined $self->{$key});
253             }
254            
255 0           $obj->encodeByData($enc);
256            
257 0           return($obj);
258             }
259            
260             1;
261            
262             __END__