API2/Resource/Font/CoreFont.pm

filter: Statementstmt 90.4% 76 / 84 Branchbran 64.7% 22 / 34 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 86.3% 127 / 147 CC 22 SCAR 20.0
CC
22
Cov
84%
new
14.9
_deep_copy
5.4
_look_for_font
5.0
CRAP 7.4
« API2/Resource/XObject/Form/BarCode/ean13.pm ↑ Summary API2/Basic/PDF/String.pm »
1 package PDF::API2::Resource::Font::CoreFont;
2  
3 9 ▶use base 'PDF::API2::Resource::Font';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 our $VERSION = '2.049'; # VERSION
9  
10 9 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use PDF::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 my @standard_fonts = qw(
16 Courier Courier-Bold Courier-BoldOblique Courier-Oblique
17 Helvetica Helvetica-Bold Helvetica-BoldOblique Helvetica-Oblique
18 Symbol
19 Times-Bold Times-BoldItalic Times-Italic Times-Roman
20 ZapfDingbats
21 );
22  
23 # Windows fonts with Type1 equivalents
24 9 my $alias = {
25 'arial' => 'helvetica',
26 'arialitalic' => 'helveticaoblique',
27 'arialbold' => 'helveticabold',
28 'arialbolditalic' => 'helveticaboldoblique',
29  
30 'times' => 'timesroman',
31 'timesnewromanbolditalic' => 'timesbolditalic',
32 'timesnewromanbold' => 'timesbold',
33 'timesnewromanitalic' => 'timesitalic',
34 'timesnewroman' => 'timesroman',
35  
36 'couriernewbolditalic' => 'courierboldoblique',
37 'couriernewbold' => 'courierbold',
38 'couriernewitalic' => 'courieroblique',
39 'couriernew' => 'courier',
40 };
41  
42 =head1 NAME
43  
44 PDF::API2::Resource::Font::CoreFont - Module for using the 14 standard PDF fonts.
45  
46 =head1 SYNOPSIS
47  
48 my $pdf = PDF::API2->new();
49 my $font = $pdf->font('Times-Roman');
50  
51 my $page = $pdf->page();
52 my $text = $page->text();
53 $text->font($font, 20);
54 $text->translate(200, 700);
55 $text->text('Hello world!');
56  
57 $pdf->save('/path/to/new.pdf');
58  
59 =head1 STANDARD FONTS
60  
61 The following fourteen fonts are available in all PDF readers that conform to
62 the PDF specification:
63  
64 =over
65  
66 =item * Courier
67  
68 =item * Courier-Bold
69  
70 =item * Courier-BoldOblique
71  
72 =item * Courier-Oblique
73  
74 =item * Helvetica
75  
76 =item * Helvetica-Bold
77  
78 =item * Helvetica-BoldOblique
79  
80 =item * Helvetica-Oblique
81  
82 =item * Symbol
83  
84 =item * Times-Bold
85  
86 =item * Times-BoldItalic
87  
88 =item * Times-Italic
89  
90 =item * Times-Roman
91  
92 =item * ZapfDingbats
93  
94 =back
95  
96 These fonts (except Symbol and ZapfDingbats) include glyphs for ASCII and
97 certain Latin characters only. If other characters are needed, you will need to
98 embed a font file.
99  
100 =cut
101  
102 53 ▶sub _look_for_font {
Subroutine _look_for_font: called CC=5 SCAR=16.2
103 53 my $name = shift();
104 53 eval "require PDF::API2::Resource::Font::CoreFont::$name";
105 53 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 53 53
106 0 die "requested font '$name' not installed";
107 }
108  
109 53 my $class = "PDF::API2::Resource::Font::CoreFont::$name";
110 53 my $font = _deep_copy($class->data());
111 53 ▶ $font->{'uni'} ||= [];
Condition100%2/2
$font->{"uni"} ||= []
l!l
152
MC/DC100%1/1
$font->{"uni"} ||= []
$font->{"uni"}
Truth table100%2/2
A
A= $font->{"uni"}
Aresult
11
00
112 53 foreach my $n (0..255) {
113 13568 ▶ unless (defined $font->{'uni'}->[$n]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $font->{"uni"}[$n]) 13312 256 13568
114 13312 $font->{'uni'}->[$n] = uniByName($font->{'char'}->[$n]);
115 }
116 }
117 53 return %$font;
118 }
119  
120 # Deep copy something, thanks to Randal L. Schwartz
121 # Changed to deal with code refs, in which case it doesn't try to deep copy
122 31743 ▶sub _deep_copy {
Subroutine _deep_copy: called CC=5 SCAR=16.8
123 31743 my $this = shift();
124 9 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
125 31743 ▶ unless (ref($this)) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $this) { } 398 31345 31743
if (do { ref $this eq 'ARRAY' }) { } 291 107 398
elsif (ref $this eq 'HASH') { } 107 0 107
elsif (ref $this eq 'CODE') { } 0 0 0
126 31345 return $this;
127 }
128 elsif (ref($this) eq 'ARRAY') {
129 291 return [ map { _deep_copy($_) } @$this];
130 }
131 elsif (ref($this) eq 'HASH') {
132 107 return +{ map { $_ => _deep_copy($this->{$_}) } keys %$this };
133 }
134 elsif (ref $this eq "CODE") {
135 # Can't deep copy code refs
136 0 return $this;
137 }
138 else {
139 0 die 'Unable to copy a ' . ref($this);
140 }
141 }
142  
143 53 ▶sub new {
Subroutine new: called CC=13 SCAR=27.0
Pod documented
144 53 my ($class, $pdf, $name, %options) = @_;
145 53 my $is_standard = is_standard($name);
146  
147 53 ▶ if (-f $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $name) 0 53 53
148 0 eval "require '$name'";
149 0 $name = basename($name, '.pm');
150 }
151  
152 53 my $lookname = lc($name);
153 53 $lookname =~ s/[^a-z0-9]+//gi;
154 53 ▶ $lookname = $alias->{$lookname} if $alias->{$lookname};
Branch: 1 branch
BranchTrueFalseTotal
if $alias->{$lookname} 0 53 53
155  
156 53 my $data;
157 53 ▶ unless (defined $options{'-metrics'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'-metrics'}) { } 0 53 53
158 53 $data = { _look_for_font($lookname) };
159 }
160 else {
161 0 $data = { %{$options{'-metrics'}} };
162 }
163  
164 53 ▶ die "Undefined font '$name($lookname)'" unless $data->{'fontname'};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"fontname"} 0 53 53
165  
166 53 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 53 53
167 53 my $self = $class->SUPER::new($pdf, $data->{'apiname'} . pdfkey() . '~' . time());
168 53 ▶ $pdf->new_obj($self) unless $self->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 53 53
169 53 $self->{' data'} = $data;
170 53 ▶ $self->{'-dokern'} = 1 if $options{'-dokern'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-dokern"} 0 53 53
171  
172 53 $self->{'Subtype'} = PDFName($self->data->{'type'});
173 53 $self->{'BaseFont'} = PDFName($self->fontname());
174 53 ▶ if ($options{'-pdfname'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-pdfname"}) 0 53 53
175 0 $self->name($options{'-pdfname'});
176 }
177  
178 53 ▶ unless ($self->data->{'iscore'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->data->{"iscore"}) 15 38 53
179 15 $self->{'FontDescriptor'} = $self->descrByData();
180 }
181  
182 53 $self->encodeByData($options{'-encode'});
183  
184 # The standard non-symbolic fonts use unmodified WinAnsiEncoding.
185 53 ▶ if ($is_standard and not $self->issymbol() and not $options{'-encode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_standard and not $self->issymbol and not $options{"-encode"}) 21 32 53
Condition100%6/6
$is_standard and not $self->issymbol and not $options{"-encode"}
!ll&&!rl&&r
30221
$is_standard and not $self->issymbol
!ll&&!rl&&r
29123
MC/DC100%3/3
$is_standard and not $self->issymbol and not $options{"-encode"}
$is_standard not $self->issymbol not $options{"-encode"}
Truth table100%4/4
A and not(B) and not(C)
A= $is_standardB= not $self->issymbolC= not $options{"-encode"}
ABCresult
0XX0
10X0
1100
1111
186 21 $self->{'Encoding'} = PDFName('WinAnsiEncoding');
187 21 delete $self->{'FirstChar'};
188 21 delete $self->{'LastChar'};
189 21 delete $self->{'Widths'};
190 }
191  
192 53 return $self;
193 }
194  
195 =head1 METHODS
196  
197 =head2 is_standard
198  
199 my $boolean = PDF::API2::Resource::Font::CoreFont->is_standard($name);
200  
201 Returns true if C<$name> is an exact, case-sensitive match for one of the
202 standard font names shown above.
203  
204 =cut
205  
206 70 ▶sub is_standard {
Subroutine is_standard: called CC=1 SCAR=0.0
Pod documented
207 70 my $name = pop();
208 70 ▶ return any { $_ eq $name } @standard_fonts;
Subroutine __ANON__: called CC=1 SCAR=0.0
209 }
210  
211 =head2 names
212  
213 my @font_names = PDF::API2::Resource::Font::CoreFont->list();
214 my $array_ref = PDF::API2::Resource::Font::CoreFont->list();
215  
216 Returns an array or a reference to an array containing the names of the built-in
217 core (standard) fonts.
218  
219 =cut
220  
221 3 ▶sub names {
Subroutine names: called CC=2 SCAR=6.9
Pod documented
222 3 ▶ return wantarray() ? @standard_fonts : [@standard_fonts];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
223 }
224  
225 9 1;
« API2/Resource/XObject/Form/BarCode/ean13.pm ↑ Summary API2/Basic/PDF/String.pm »