Builder/Resource/XObject/Form/BarCode.pm

filter: Statementstmt 81.3% 109 / 134 Branchbran 72.2% 26 / 36 Conditioncond 52.8% 37 / 70 MC/DCmcdc 29.5% 13 / 44 Subroutinesub 81.8% 9 / 11 Podpod 50.0% 3 / 6 totaltotal 65.4% 197 / 301 CC 48 SCAR 36.2
CC
48
Cov
72%
new
54.8
drawbar
29.0
encode_string
2.0
CRAP 37.2
« Builder/Resource/ColorSpace/Separation.pm ↑ Summary Builder/Resource/CIDFont/CJKFont.pm »
1 package PDF::Builder::Resource::XObject::Form::BarCode;
2  
3 2 ▶use base 'PDF::Builder::Resource::XObject::Form::Hybrid';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '3.028'; # VERSION
9 2 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10  
11 2 ▶use PDF::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PDF::Builder::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 PDF::Builder::Resource::XObject::Form::BarCode - Base class for one-dimensional barcodes
17  
18 Inherits from L<PDF::Builder::Resource::XObject::Form::Hybrid>
19  
20 =head1 METHODS
21  
22 =head2 new
23  
24 $barcode = PDF::Builder::Resource::XObject::Form::BarCode->new($pdf, %options)
25  
26 =over
27  
28 Creates a barcode form resource.
29  
30 =back
31  
32 =cut
33  
34 6 ▶sub new {
Subroutine new: called CC=29 SCAR=40.0
Pod documented
35 6 my ($class, $pdf, %options) = @_;
36 # copy dashed option names to preferred undashed names
37 6 ▶ if (defined $options{'-font'} && !defined $options{'font'}) { $options{'font'} = delete($options{'-font'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-font"} and not defined $options{"font"}) 1 5 6
Condition66%2/3
defined $options{"-font"} and not defined $options{"font"}
!ll&&!rl&&r
501
MC/DC50%1/2
defined $options{"-font"} and not defined $options{"font"}
defined $options{"-font"} not defined $options{"font"}
Truth table66%2/3
A and not(B)
A= defined $options{"-font"}B= not defined $options{"font"}
ABresult
0X0
100
111
38 6 ▶ if (defined $options{'-umzn'} && !defined $options{'umzn'}) { $options{'umzn'} = delete($options{'-umzn'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-umzn"} and not defined $options{"umzn"}) 1 5 6
Condition66%2/3
defined $options{"-umzn"} and not defined $options{"umzn"}
!ll&&!rl&&r
501
MC/DC50%1/2
defined $options{"-umzn"} and not defined $options{"umzn"}
defined $options{"-umzn"} not defined $options{"umzn"}
Truth table66%2/3
A and not(B)
A= defined $options{"-umzn"}B= not defined $options{"umzn"}
ABresult
0X0
100
111
39 6 ▶ if (defined $options{'-lmzn'} && !defined $options{'lmzn'}) { $options{'lmzn'} = delete($options{'-lmzn'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-lmzn"} and not defined $options{"lmzn"}) 1 5 6
Condition66%2/3
defined $options{"-lmzn"} and not defined $options{"lmzn"}
!ll&&!rl&&r
501
MC/DC50%1/2
defined $options{"-lmzn"} and not defined $options{"lmzn"}
defined $options{"-lmzn"} not defined $options{"lmzn"}
Truth table66%2/3
A and not(B)
A= defined $options{"-lmzn"}B= not defined $options{"lmzn"}
ABresult
0X0
100
111
40 6 ▶ if (defined $options{'-zone'} && !defined $options{'zone'}) { $options{'zone'} = delete($options{'-zone'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-zone"} and not defined $options{"zone"}) 1 5 6
Condition66%2/3
defined $options{"-zone"} and not defined $options{"zone"}
!ll&&!rl&&r
501
MC/DC50%1/2
defined $options{"-zone"} and not defined $options{"zone"}
defined $options{"-zone"} not defined $options{"zone"}
Truth table66%2/3
A and not(B)
A= defined $options{"-zone"}B= not defined $options{"zone"}
ABresult
0X0
100
111
41 0 ▶ if (defined $options{'-quzn'} && !defined $options{'quzn'}) { $options{'quzn'} = delete($options{'-quzn'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-quzn"} and not defined $options{"quzn"}) 0 6 6
Condition33%1/3
defined $options{"-quzn"} and not defined $options{"quzn"}
!ll&&!rl&&r
600
MC/DC0%0/2
defined $options{"-quzn"} and not defined $options{"quzn"}
defined $options{"-quzn"} not defined $options{"quzn"}
Truth table33%1/3
A and not(B)
A= defined $options{"-quzn"}B= not defined $options{"quzn"}
ABresult
0X0
100
111
42 0 ▶ if (defined $options{'-ofwt'} && !defined $options{'ofwt'}) { $options{'ofwt'} = delete($options{'-ofwt'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-ofwt"} and not defined $options{"ofwt"}) 0 6 6
Condition33%1/3
defined $options{"-ofwt"} and not defined $options{"ofwt"}
!ll&&!rl&&r
600
MC/DC0%0/2
defined $options{"-ofwt"} and not defined $options{"ofwt"}
defined $options{"-ofwt"} not defined $options{"ofwt"}
Truth table33%1/3
A and not(B)
A= defined $options{"-ofwt"}B= not defined $options{"ofwt"}
ABresult
0X0
100
111
43 6 ▶ if (defined $options{'-fnsz'} && !defined $options{'fnsz'}) { $options{'fnsz'} = delete($options{'-fnsz'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-fnsz"} and not defined $options{"fnsz"}) 1 5 6
Condition66%2/3
defined $options{"-fnsz"} and not defined $options{"fnsz"}
!ll&&!rl&&r
501
MC/DC50%1/2
defined $options{"-fnsz"} and not defined $options{"fnsz"}
defined $options{"-fnsz"} not defined $options{"fnsz"}
Truth table66%2/3
A and not(B)
A= defined $options{"-fnsz"}B= not defined $options{"fnsz"}
ABresult
0X0
100
111
44 0 ▶ if (defined $options{'-spcr'} && !defined $options{'spcr'}) { $options{'spcr'} = delete($options{'-spcr'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-spcr"} and not defined $options{"spcr"}) 0 6 6
Condition33%1/3
defined $options{"-spcr"} and not defined $options{"spcr"}
!ll&&!rl&&r
600
MC/DC0%0/2
defined $options{"-spcr"} and not defined $options{"spcr"}
defined $options{"-spcr"} not defined $options{"spcr"}
Truth table33%1/3
A and not(B)
A= defined $options{"-spcr"}B= not defined $options{"spcr"}
ABresult
0X0
100
111
45 0 ▶ if (defined $options{'-mils'} && !defined $options{'mils'}) { $options{'mils'} = delete($options{'-mils'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-mils"} and not defined $options{"mils"}) 0 6 6
Condition33%1/3
defined $options{"-mils"} and not defined $options{"mils"}
!ll&&!rl&&r
600
MC/DC0%0/2
defined $options{"-mils"} and not defined $options{"mils"}
defined $options{"-mils"} not defined $options{"mils"}
Truth table33%1/3
A and not(B)
A= defined $options{"-mils"}B= not defined $options{"mils"}
ABresult
0X0
100
111
46 0 ▶ if (defined $options{'-color'} && !defined $options{'color'}) { $options{'color'} = delete($options{'-color'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-color"} and not defined $options{"color"}) 0 6 6
Condition33%1/3
defined $options{"-color"} and not defined $options{"color"}
!ll&&!rl&&r
600
MC/DC0%0/2
defined $options{"-color"} and not defined $options{"color"}
defined $options{"-color"} not defined $options{"color"}
Truth table33%1/3
A and not(B)
A= defined $options{"-color"}B= not defined $options{"color"}
ABresult
0X0
100
111
47  
48 6 my $self = $class->SUPER::new($pdf);
49  
50 6 $self->{' bfont'} = $options{'font'};
51  
52 6 ▶ $self->{' umzn'} = $options{'umzn'} || 0; # (u)pper (m)ending (z)o(n)e
Condition50%1/2
$options{'umzn'} || 0
l!l
06
MC/DC0%0/1
$options{'umzn'} || 0
$options{'umzn'}
Truth table50%1/2
A
A= $options{'umzn'}
Aresult
11
00
53 6 ▶ $self->{' lmzn'} = $options{'lmzn'} || 0; # (l)ower (m)ending (z)o(n)e
Condition100%2/2
$options{'lmzn'} || 0
l!l
15
MC/DC100%1/1
$options{'lmzn'} || 0
$options{'lmzn'}
Truth table100%2/2
A
A= $options{'lmzn'}
Aresult
11
00
54 6 ▶ $self->{' zone'} = $options{'zone'} || 0; # barcode height
Condition100%2/2
$options{'zone'} || 0
l!l
15
MC/DC100%1/1
$options{'zone'} || 0
$options{'zone'}
Truth table100%2/2
A
A= $options{'zone'}
Aresult
11
00
55 6 ▶ $self->{' quzn'} = $options{'quzn'} || 0; # (qu)iet (z)o(n)e
Condition50%1/2
$options{'quzn'} || 0
l!l
06
MC/DC0%0/1
$options{'quzn'} || 0
$options{'quzn'}
Truth table50%1/2
A
A= $options{'quzn'}
Aresult
11
00
56 6 ▶ $self->{' ofwt'} = $options{'ofwt'} || 0.01; # (o)ver(f)low (w)id(t)h
Condition50%1/2
$options{'ofwt'} || 0.01
l!l
06
MC/DC0%0/1
$options{'ofwt'} || 0.01
$options{'ofwt'}
Truth table50%1/2
A
A= $options{'ofwt'}
Aresult
11
00
57 6 $self->{' fnsz'} = $options{'fnsz'}; # (f)o(n)t(s)i(z)e
58 6 ▶ $self->{' spcr'} = $options{'spcr'} || ''; # (sp)a(c)e(r) between chars in label
Condition50%1/2
$options{'spcr'} || ''
l!l
06
MC/DC0%0/1
$options{'spcr'} || ''
$options{'spcr'}
Truth table50%1/2
A
A= $options{'spcr'}
Aresult
11
00
59 6 ▶ $self->{' mils'} = $options{'mils'} || 1000/72; # single barcode unit width. 1 mil = 1/1000 of one inch. 1000/72 - for backward compatibility
Condition50%1/2
$options{'mils'} || 13.888888888888889284
l!l
06
MC/DC0%0/1
$options{'mils'} || 13.888888888888889284
$options{'mils'}
Truth table50%1/2
A
A= $options{'mils'}
Aresult
11
00
60 6 ▶ $self->{' color'} = $options{'color'} || 'black'; # barcode color
Condition50%1/2
$options{'color'} || 'black'
l!l
06
MC/DC0%0/1
$options{'color'} || 'black'
$options{'color'}
Truth table50%1/2
A
A= $options{'color'}
Aresult
11
00
61  
62 6 return $self;
63 }
64  
65 2 my %bar_widths = (
66 0 => 0,
67 1 => 1, 'a' => 1, 'A' => 1,
68 2 => 2, 'b' => 2, 'B' => 2,
69 3 => 3, 'c' => 3, 'C' => 3,
70 4 => 4, 'd' => 4, 'D' => 4,
71 5 => 5, 'e' => 5, 'E' => 5,
72 6 => 6, 'f' => 6, 'F' => 6,
73 7 => 7, 'g' => 7, 'G' => 7,
74 8 => 8, 'h' => 8, 'H' => 8,
75 9 => 9, 'i' => 9, 'I' => 9,
76 );
77  
78 2 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod undocumented
79 2 my ($self, $string) = @_;
80  
81 2 my @bars = map { [ $self->encode_string($_), $_ ] } split(//, $string);
82 2 return @bars;
83 }
84  
85 17 ▶sub encode_string {
Subroutine encode_string: called CC=2 SCAR=6.9
Pod undocumented
86 17 my ($self, $string) = @_;
87  
88 17 my $bar;
89 17 foreach my $character (split(//, $string)) {
90 17 $bar .= $self->encode_char($character);
91 }
92 17 return $bar;
93 }
94  
95 6 ▶sub drawbar {
Subroutine drawbar: called CC=19 SCAR=33.7
Pod undocumented
96 6 my $self = shift();
97 6 my @sets = @{shift()};
98 6 my $caption = shift();
99  
100 6 $self->fillcolor($self->{' color'});
101 6 $self->strokecolor($self->{' color'});
102 6 $self->linedash();
103  
104 6 my $x = $self->{' quzn'};
105 6 my $is_space_next = 0;
106 6 my $wdt_factor = $self->{' mils'} / 1000 * 72;
107 6 foreach my $set (@sets) {
108 30 my ($code, $label);
109 30 ▶ if (ref($set)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $set) { } 18 12 30
110 18 ($code, $label) = @{$set};
111 } else {
112 12 $code = $set;
113 12 $label = undef;
114 }
115  
116 30 my $code_width = 0;
117 30 my ($font_size, $y_label);
118 30 foreach my $bar (split(//, $code)) {
119 172 my $bar_width = $bar_widths{$bar} * $wdt_factor;
120  
121 172 my ($y0, $y1);
122 172 ▶ if ($bar =~ /[0-9]/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($bar =~ /[0-9]/) { } 115 57 172
elsif ($bar =~ /[a-z]/) { } 57 0 57
elsif ($bar =~ /[A-Z]/) { } 0 0 0
123 115 $y0 = $self->{' quzn'} + $self->{' lmzn'};
124 115 $y1 = $self->{' quzn'} + $self->{' lmzn'} + $self->{' zone'} + $self->{' umzn'};
125 115 $y_label = $self->{' quzn'};
126 115 ▶ if ($self->{' fnsz'} and $self->{' lmzn'} < $self->{' fnsz'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" fnsz"} and $self->{" lmzn"} < $self->{" fnsz"}) 0 115 115
Condition66%2/3
$self->{" fnsz"} and $self->{" lmzn"} < $self->{" fnsz"}
!ll&&!rl&&r
103120
MC/DC0%0/2
$self->{" fnsz"} and $self->{" lmzn"} < $self->{" fnsz"}
$self->{" fnsz"} $self->{" lmzn"} < $self->{" fnsz"}
Truth table66%2/3
A and B
A= $self->{" fnsz"}B= $self->{" lmzn"} < $self->{" fnsz"}
ABresult
0X0
100
111
127 0 $y_label -= $self->{' fnsz'} * 0.8 - $self->{' lmzn'};
128 }
129 115 ▶ $font_size = $self->{' fnsz'} || $self->{' lmzn'};
Condition66%2/3
$self->{' fnsz'} || $self->{' lmzn'}
l!l&&r!l&&!r
120103
MC/DC50%1/2
$self->{' fnsz'} || $self->{' lmzn'}
$self->{' fnsz'} $self->{' lmzn'}
Truth table66%2/3
A || B
A= $self->{' fnsz'}B= $self->{' lmzn'}
ABresult
1X1
011
000
130 } elsif ($bar =~ /[a-z]/) {
131 57 $y0 = $self->{' quzn'};
132 57 $y1 = $self->{' quzn'} + $self->{' lmzn'} + $self->{' zone'} + $self->{' umzn'};
133 57 $y_label = $self->{' quzn'} + $self->{' lmzn'} + $self->{' zone'} + $self->{' umzn'} + 2;
134 57 ▶ $font_size = $self->{' fnsz'} || $self->{' umzn'};
Condition66%2/3
$self->{' fnsz'} || $self->{' umzn'}
l!l&&r!l&&!r
18039
MC/DC50%1/2
$self->{' fnsz'} || $self->{' umzn'}
$self->{' fnsz'} $self->{' umzn'}
Truth table66%2/3
A || B
A= $self->{' fnsz'}B= $self->{' umzn'}
ABresult
1X1
011
000
135 } elsif ($bar =~ /[A-Z]/) {
136 0 $y0 = $self->{' quzn'};
137 0 $y1 = $self->{' quzn'} + $self->{' lmzn'} + $self->{' zone'};
138 0 ▶ $font_size = $self->{' fnsz'} || $self->{' umzn'};
Condition0%0/3
$self->{' fnsz'} || $self->{' umzn'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{' fnsz'} || $self->{' umzn'}
$self->{' fnsz'} $self->{' umzn'}
Truth table0%0/3
A || B
A= $self->{' fnsz'}B= $self->{' umzn'}
ABresult
1X1
011
000
139 0 $y_label = $self->{' quzn'} + $self->{' lmzn'} + $self->{' zone'} + $self->{' umzn'} - $font_size;
140 } else {
141 0 $y0 = $self->{' quzn'} + $self->{' lmzn'};
142 0 $y1 = $self->{' quzn'} + $self->{' lmzn'} + $self->{' zone'} + $self->{' umzn'};
143 0 $y_label = $self->{' quzn'};
144 0 ▶ $font_size = $self->{' fnsz'} || $self->{' lmzn'};
Condition0%0/3
$self->{' fnsz'} || $self->{' lmzn'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{' fnsz'} || $self->{' lmzn'}
$self->{' fnsz'} $self->{' lmzn'}
Truth table0%0/3
A || B
A= $self->{' fnsz'}B= $self->{' lmzn'}
ABresult
1X1
011
000
145 }
146  
147 172 ▶ unless ($is_space_next or $bar eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_space_next or $bar eq 0) 86 86 172
Condition100%3/3
$is_space_next or $bar eq 0
l!l&&r!l&&!r
85186
MC/DC100%2/2
$is_space_next or $bar eq 0
$is_space_next $bar eq 0
Truth table100%3/3
A or B
A= $is_space_nextB= $bar eq 0
ABresult
1X1
011
000
148 86 $self->linewidth($bar_width - $self->{' ofwt'});
149 86 $self->move($x + $code_width + $bar_width / 2, $y0);
150 86 $self->line($x + $code_width + $bar_width / 2, $y1);
151 86 $self->stroke();
152 }
153 172 $is_space_next = not $is_space_next;
154  
155 172 $code_width += $bar_width;
156 }
157  
158 30 ▶ if (defined($label) and $self->{' lmzn'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label and $self->{" lmzn"}) 1 29 30
Condition100%3/3
defined $label and $self->{" lmzn"}
!ll&&!rl&&r
12171
MC/DC100%2/2
defined $label and $self->{" lmzn"}
defined $label $self->{" lmzn"}
Truth table100%3/3
A and B
A= defined $labelB= $self->{" lmzn"}
ABresult
0X0
100
111
159 1 $label = join($self->{' spcr'}, split(//, $label));
160 1 $self->textstart();
161 1 $self->translate($x + ($code_width / 2), $y_label);
162 1 $self->font($self->{' bfont'}, $font_size);
163 1 $self->text_center($label);
164 1 $self->textend();
165 }
166  
167 30 $x += $code_width;
168 }
169  
170 6 $x += $self->{' quzn'};
171  
172 6 ▶ if (defined $caption) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $caption) 0 6 6
173 0 ▶ my $font_size = $self->{' fnsz'} || $self->{' lmzn'};
Condition0%0/3
$self->{' fnsz'} || $self->{' lmzn'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{' fnsz'} || $self->{' lmzn'}
$self->{' fnsz'} $self->{' lmzn'}
Truth table0%0/3
A || B
A= $self->{' fnsz'}B= $self->{' lmzn'}
ABresult
1X1
011
000
174 0 my $y_caption = $self->{' quzn'} - $font_size;
175 0 $self->textstart();
176 0 $self->translate($x / 2, $y_caption);
177 0 $self->font($self->{' bfont'}, $font_size);
178 0 $self->text_center($caption);
179 0 $self->textend();
180 }
181  
182 6 $self->{' w'} = $x;
183 6 $self->{' h'} = 2 * $self->{' quzn'} + $self->{' lmzn'} + $self->{' zone'} + $self->{' umzn'};
184 6 $self->bbox(0, 0, $self->{' w'}, $self->{' h'});
185 6 return;
186 }
187  
188 =head2 width
189  
190 $width = $barcode->width()
191  
192 =over
193  
194 Returns the width of the bar code.
195  
196 =back
197  
198 =cut
199  
200 0 ▶sub width {
Subroutine width: not called CC=1 SCAR=6.9
Pod documented
201 0 my $self = shift;
202  
203 0 return $self->{' w'};
204 }
205  
206 =head2 height
207  
208 $height = $barcode->height()
209  
210 =over
211  
212 Returns the height of the bar code.
213  
214 =back
215  
216 =cut
217  
218  
219 0 ▶sub height {
Subroutine height: not called CC=1 SCAR=6.9
Pod documented
220 0 my $self = shift;
221  
222 0 return $self->{' h'};
223 }
224  
225 2 1;
« Builder/Resource/ColorSpace/Separation.pm ↑ Summary Builder/Resource/CIDFont/CJKFont.pm »