API2/Resource/Font/CoreFont.pm

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