API3/Compat/API2/Resource/Font/CoreFont.pm

filter: Statementstmt 30.2% 33 / 109 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 61.1% 11 / 18 Podpod 75.0% 3 / 4 totaltotal 26.2% 47 / 179 CC 29 SCAR 44.6
CC
29
Cov
21%
new
240.0
deep_copy
30.0
_look_for_font
20.7
CRAP 86.1
« API3/Compat/API2/NamedDestination.pm ↑ Summary API3/Compat/API2/Resource/ColorSpace/DeviceN.pm »
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 <areibens@cpan.org>.
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 FILE IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
21 # AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23 # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 # SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR CONTRIBUTORS
25 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 # ARISING IN ANY WAY OUT OF THE USE OF THIS FILE, EVEN IF
32 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #
34 # SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
35 #
36 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC
37 # LICENSE ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE
38 # FREE SOFTWARE FOUNDATION, INC., 59 TEMPLE PLACE - SUITE 330,
39 # BOSTON, MA 02111-1307, USA.
40 #
41 # $Id: CoreFont.pm,v 2.0 2005/11/16 02:18:14 areibens Exp $
42 #
43 #=======================================================================
44 package PDF::API3::Compat::API2::Resource::Font::CoreFont;
45
46 =head1 NAME
47
48 PDF::API3::Compat::API2::Resource::Font::CoreFont - Module for using the 14 PDF built-in Fonts.
49
50 =head1 SYNOPSIS
51
52 #
53 use PDF::API3::Compat::API2;
54 #
55 $pdf = PDF::API3::Compat::API2->new;
56 $cft = $pdf->corefont('Times-Roman');
57 #
58
59 =head1 METHODS
60
61 =over 4
62
63 =cut
64
65 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
66
67 1 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶ use Encode qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
69
70 1 ▶ use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
71
72 1 ▶ use vars qw( @ISA $fonts $alias $subs $encodings $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶ use PDF::API3::Compat::API2::Resource::Font;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶ use PDF::API3::Compat::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶ use PDF::API3::Compat::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
76
77 1 @ISA=qw(PDF::API3::Compat::API2::Resource::Font);
78
79 1 ( $VERSION ) = sprintf '%i.%03i', split(/\./,('$Revision: 2.0 $' =~ /Revision: (\S+)\s/)[0]); # $Date: 2005/11/16 02:18:14 $
80
81 }
82 1 ▶no warnings qw[ deprecated recursion uninitialized ];
Subroutine BEGIN: called CC=1 SCAR=0.0
83
84 =item $font = PDF::API3::Compat::API2::Resource::Font::CoreFont->new $pdf, $fontname, %options
85
86 Returns a corefont object.
87
88 =cut
89
90 =pod
91
92 Valid %options are:
93
94 I<-encode>
95 ... changes the encoding of the font from its default.
96 See I<perl's Encode> for the supported values.
97
98 I<-pdfname> ... changes the reference-name of the font from its default.
99 The reference-name is normally generated automatically and can be
100 retrived via $pdfname=$font->name.
101
102 =cut
103
104 sub _look_for_font ($)
105 0 ▶{
Subroutine _look_for_font: not called CC=5 SCAR=30.3
106 0 my $fname=shift;
107 ## return(%{$fonts->{$fname}}) if(defined $fonts->{$fname});
108 0 eval "require PDF::API3::Compat::API2::Resource::Font::CoreFont::$fname; ";
109 0 ▶ unless($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
110 {
111 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 0 my $obj = "PDF::API3::Compat::API2::Resource::Font::CoreFont::".$fname;
113 0 $fonts->{$fname} = deep_copy(${$obj."::FONTDATA"});
114 0 ▶ $fonts->{$fname}->{uni}||=[];
Condition0%0/2
$fonts->{$fname}{"uni"} ||= []
l!l
00
MC/DC0%0/1
$fonts->{$fname}{"uni"} ||= []
$fonts->{$fname}{"uni"}
Truth table0%0/2
A
A= $fonts->{$fname}{"uni"}
Aresult
11
00
115 0 foreach my $n (0..255)
116 {
117 0 ▶ $fonts->{$fname}->{uni}->[$n]=uniByName($fonts->{$fname}->{char}->[$n]) unless(defined $fonts->{$fname}->{uni}->[$n]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fonts->{$fname}{"uni"}[$n] 0 0 0
118 }
119 0 return(%{$fonts->{$fname}});
120 }
121 else
122 {
123 0 die "requested font '$fname' not installed ";
124 }
125 }
126
127 #
128 # Deep copy something, thanks to Randal L. Schwartz
129 # Changed to deal w/ CODE refs, in which case it doesn't try to deep copy
130 #
131 sub deep_copy
132 0 ▶{
Subroutine deep_copy: not called CC=5 SCAR=34.0
Pod undocumented
133 0 my $this = shift;
134 0 ▶ if (not ref $this)
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $this) { } 0 0 0
elsif (ref $this eq 'ARRAY') { } 0 0 0
elsif (ref $this eq 'HASH') { } 0 0 0
elsif (ref $this eq 'CODE') { } 0 0 0
135 {
136 0 $this;
137 }
138 elsif (ref $this eq "ARRAY")
139 {
140 0 [map &deep_copy($_), @$this];
141 }
142 elsif (ref $this eq "HASH")
143 {
144 0 +{map { $_ => &deep_copy($this->{$_}) } keys %$this};
145 }
146 elsif (ref $this eq "CODE")
147 {
148 # Can't deep copy code refs
149 0 return $this;
150 }
151 else
152 {
153 0 die "what type is $_?";
154 }
155 }
156
157 sub _look_for_fontfile ($)
158 0 ▶{
Subroutine _look_for_fontfile: not called CC=3 SCAR=24.8
159 0 my $fname=shift;
160 0 my $fpath=undef;
161 0 foreach my $dir (@INC)
162 {
163 0 $fpath="$dir/PDF/API3/Compat/API2/Resource/Font/CoreFont/$fname";
164 0 ▶ last if(-f $fpath);
Branch: 1 branch
BranchTrueFalseTotal
if -f $fpath 0 0 0
165 0 $fpath=undef;
166 }
167 0 return($fpath);
168 }
169
170 sub _look_for_fontmetricfile ($)
171 0 ▶{
Subroutine _look_for_fontmetricfile: not called CC=3 SCAR=24.8
172 0 my $fname=shift;
173 0 my $fpath=undef;
174 0 foreach my $dir (@INC)
175 {
176 0 $fpath="$dir/PDF/API3/Compat/API2/Resource/Font/CoreFont/$fname.fm";
177 0 ▶ last if(-f $fpath);
Branch: 1 branch
BranchTrueFalseTotal
if -f $fpath 0 0 0
178 0 $fpath=undef;
179 }
180 0 return($fpath);
181 }
182
183 sub new
184 0 ▶{
Subroutine new: not called CC=15 SCAR=54.8
Pod documented
185 0 my ($class,$pdf,$name,@opts) = @_;
186 0 my ($self,$data);
187 0 my %opts=();
188 0 ▶ if(-f $name)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $name) 0 0 0
189 {
190 0 eval "require '$name'; ";
191 0 $name=basename($name,'.pm');
192 }
193 0 my $lookname=lc($name);
194 0 $lookname=~s/[^a-z0-9]+//gi;
195 0 ▶ %opts=@opts if((scalar @opts)%2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@opts) % 2 == 0 0 0 0
196 0 ▶ $opts{-encode}||='asis';
Condition0%0/2
$opts{"-encode"} ||= "asis"
l!l
00
MC/DC0%0/1
$opts{"-encode"} ||= "asis"
$opts{"-encode"}
Truth table0%0/2
A
A= $opts{"-encode"}
Aresult
11
00
197
198 0 ▶ $lookname = defined($alias->{$lookname}) ? $alias->{$lookname} : $lookname ;
Branch: 1 branch
BranchTrueFalseTotal
defined $alias->{$lookname} ? : 0 0 0
199
200 0 ▶ if(defined $subs->{$lookname})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subs->{$lookname}) { } 0 0 0
201 {
202 0 $data={_look_for_font($subs->{$lookname}->{-alias})};
203 0 foreach my $k (keys %{$subs->{$lookname}})
204 {
205 0 ▶ next if($k=~/^\-/);
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^\-/ 0 0 0
206 0 $data->{$k}=$subs->{$lookname}->{$k};
207 }
208 }
209 else
210 {
211 0 ▶ unless(defined $opts{-metrics})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'-metrics'}) { } 0 0 0
212 {
213 0 $data={_look_for_font($lookname)};
214 }
215 else
216 {
217 0 $data={%{$opts{-metrics}}};
218 }
219 }
220
221 0 ▶ die "Undefined Font '$name($lookname)'" unless($data->{fontname});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"fontname"} 0 0 0
222
223 # we have data now here so we need to check if
224 # there is a -ttfile or -afmfile/-pfmfile/-pfbfile
225 # and proxy the call to the relevant modules
226 #
227 #if(defined $data->{-ttfile} && $data->{-ttfile}=_look_for_fontfile($data->{-ttfile}))
228 #{
229 # return(PDF::API3::Compat::API2::Resource::CIDFont::TrueType->new($pdf,$data->{-ttfile},@opts));
230 #}
231 #elsif(defined $data->{-pfbfile} && $data->{-pfbfile}=_look_for_fontfile($data->{-pfbfile}))
232 #{
233 # $data->{-afmfile}=_look_for_fontfile($data->{-afmfile});
234 # return(PDF::API3::Compat::API2::Resource::Font::Postscript->new($pdf,$data->{-pfbfile},$data->{-afmfile},@opts));
235 #}
236 #elsif(defined $data->{-gfx})
237 #{ # to be written and tested in 'Maki' first!
238 # return(PDF::API3::Compat::API2::Resource::Font::gFont->new($pdf,$data,@opts);
239 #}
240
241 0 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
242 0 $self = $class->SUPER::new($pdf, $data->{apiname}.pdfkey().'~'.time());
243 0 ▶ $pdf->new_obj($self) unless($self->is_obj($pdf));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 0 0
244 0 $self->{' data'}=$data;
245 0 ▶ $self->{-dokern}=1 if($opts{-dokern});
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-dokern"} 0 0 0
246
247 0 $self->{'Subtype'} = PDFName($self->data->{type});
248 0 $self->{'BaseFont'} = PDFName($self->fontname);
249 0 ▶ if($opts{-pdfname})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-pdfname"}) 0 0 0
250 {
251 0 $self->name($opts{-pdfname});
252 }
253
254 0 ▶ unless($self->data->{iscore})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->data->{"iscore"}) 0 0 0
255 {
256 0 $self->{'FontDescriptor'}=$self->descrByData();
257 }
258
259 0 $self->encodeByData($opts{-encode});
260
261 0 return($self);
262 }
263
264 =item $font = PDF::API3::Compat::API2::Resource::Font::CoreFont->new_api $api, $fontname, %options
265
266 Returns a corefont object. This method is different from 'new' that
267 it needs an PDF::API3::Compat::API2-object rather than a PDF::API3::Compat::API2::PDF::File-object.
268
269 =cut
270
271 sub new_api
272 0 ▶{
Subroutine new_api: not called CC=2 SCAR=17.9
Pod documented
273 0 my ($class,$api,@opts)=@_;
274
275 0 my $obj=$class->new($api->{pdf},@opts);
276
277 0 ▶ $api->{pdf}->new_obj($obj) unless($obj->is_obj($api->{pdf}));
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->is_obj($api->{"pdf"}) 0 0 0
278
279 ## $api->resource('Font',$obj->name,$obj);
280
281 0 $api->{pdf}->out_obj($api->{pages});
282 0 return($obj);
283 }
284
285 =item PDF::API3::Compat::API2::Resource::Font::CoreFont->loadallfonts()
286
287 "Requires in" all fonts available as corefonts.
288
289 =cut
290
291 sub loadallfonts
292 0 ▶{
Subroutine loadallfonts: not called CC=2 SCAR=17.9
Pod documented
293 0 foreach my $f (qw[
294 courier courierbold courierboldoblique courieroblique
295 georgia georgiabold georgiabolditalic georgiaitalic
296 helveticaboldoblique helveticaoblique helveticabold helvetica
297 symbol
298 timesbolditalic timesitalic timesroman timesbold
299 verdana verdanabold verdanabolditalic verdanaitalic
300 webdings
301 wingdings
302 zapfdingbats
303 ])
304 {
305 0 _look_for_font($f);
306 }
307 }
308
309 # andalemono
310 # arialrounded
311 # bankgothic
312 # impact
313 # ozhandicraft
314 # trebuchet
315 # trebuchetbold
316 # trebuchetbolditalic
317 # trebuchetitalic
318
319 BEGIN
320 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
321
322 1 $alias = {
323 ## Windows Fonts with Type1 equivalence
324 'arial' => 'helvetica',
325 'arialitalic' => 'helveticaoblique',
326 'arialbold' => 'helveticabold',
327 'arialbolditalic' => 'helveticaboldoblique',
328
329 'times' => 'timesroman',
330 'timesnewromanbolditalic' => 'timesbolditalic',
331 'timesnewromanbold' => 'timesbold',
332 'timesnewromanitalic' => 'timesitalic',
333 'timesnewroman' => 'timesroman',
334
335 'couriernewbolditalic' => 'courierboldoblique',
336 'couriernewbold' => 'courierbold',
337 'couriernewitalic' => 'courieroblique',
338 'couriernew' => 'courier',
339 };
340
341 1 $subs = {
342 #'bankgothicbold' => {
343 # 'apiname' => 'Bg2',
344 # '-alias' => 'bankgothic',
345 # 'fontname' => 'BankGothicMediumBT,Bold',
346 # 'flags' => 32+262144,
347 #},
348 #'bankgothicbolditalic' => {
349 # 'apiname' => 'Bg3',
350 # '-alias' => 'bankgothic',
351 # 'fontname' => 'BankGothicMediumBT,BoldItalic',
352 # 'italicangle' => -15,
353 # 'flags' => 96+262144,
354 #},
355 #'bankgothicitalic' => {
356 # 'apiname' => 'Bg4',
357 # '-alias' => 'bankgothic',
358 # 'fontname' => 'BankGothicMediumBT,Italic',
359 # 'italicangle' => -15,
360 # 'flags' => 96,
361 #},
362 # 'impactitalic' => {
363 # 'apiname' => 'Imp2',
364 # '-alias' => 'impact',
365 # 'fontname' => 'Impact,Italic',
366 # 'italicangle' => -12,
367 # },
368 # 'ozhandicraftbold' => {
369 # 'apiname' => 'Oz2',
370 # '-alias' => 'ozhandicraft',
371 # 'fontname' => 'OzHandicraftBT,Bold',
372 # 'italicangle' => 0,
373 # 'flags' => 32+262144,
374 # },
375 # 'ozhandicraftitalic' => {
376 # 'apiname' => 'Oz3',
377 # '-alias' => 'ozhandicraft',
378 # 'fontname' => 'OzHandicraftBT,Italic',
379 # 'italicangle' => -15,
380 # 'flags' => 96,
381 # },
382 # 'ozhandicraftbolditalic' => {
383 # 'apiname' => 'Oz4',
384 # '-alias' => 'ozhandicraft',
385 # 'fontname' => 'OzHandicraftBT,BoldItalic',
386 # 'italicangle' => -15,
387 # 'flags' => 96+262144,
388 # },
389 # 'arialroundeditalic' => {
390 # 'apiname' => 'ArRo2',
391 # '-alias' => 'arialrounded',
392 # 'fontname' => 'ArialRoundedMTBold,Italic',
393 # 'italicangle' => -15,
394 # 'flags' => 96+262144,
395 # },
396 # 'arialitalic' => {
397 # 'apiname' => 'Ar2',
398 # '-alias' => 'arial',
399 # 'fontname' => 'Arial,Italic',
400 # 'italicangle' => -15,
401 # 'flags' => 96,
402 # },
403 # 'arialbolditalic' => {
404 # 'apiname' => 'Ar3',
405 # '-alias' => 'arial',
406 # 'fontname' => 'Arial,BoldItalic',
407 # 'italicangle' => -15,
408 # 'flags' => 96+262144,
409 # },
410 # 'arialbold' => {
411 # 'apiname' => 'Ar4',
412 # '-alias' => 'arial',
413 # 'fontname' => 'Arial,Bold',
414 # 'flags' => 32+262144,
415 # },
416 };
417
418 1 $fonts = { };
419
420 }
421
422 1 1;
423
424 __END__
« API3/Compat/API2/NamedDestination.pm ↑ Summary API3/Compat/API2/Resource/ColorSpace/DeviceN.pm »