Builder/Resource/XObject/Form/BarCode/code128.pm

filter: Statementstmt 88.4% 107 / 121 Branchbran 64.0% 41 / 64 Conditioncond 45.0% 9 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 100% 9 / 9 Podpod 16.6% 1 / 6 totaltotal 72.1% 168 / 233 CC 41 SCAR 28.6
CC
41
Cov
77%
encode_128_string
31.6
new
14.7
encode_128
9.2
CRAP 17.4
« Builder/Resource/XObject/Image/JPEG.pm ↑ Summary Builder/Resource/ColorSpace/Indexed/ACTFile.pm »
1 package PDF::Builder::Resource::XObject::Form::BarCode::code128;
2  
3 2 ▶use base 'PDF::Builder::Resource::XObject::Form::BarCode';
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 =head1 NAME
12  
13 PDF::Builder::Resource::XObject::Form::BarCode::code128 - Code 128 and EAN-128 barcode support
14  
15 Inherits from L<PDF::Builder::Resource::XObject::Form::BarCode>
16  
17 =head1 METHODS
18  
19 =head2 new
20  
21 $res = PDF::Builder::Resource::XObject::Form::BarCode::code128->new($pdf, %options)
22  
23 =over
24  
25 Returns a code128 object. Use 'ean' option to encode using EAN128 mode.
26 Note that this should be invoked via the Builder.pm method!
27  
28 =back
29  
30 =cut
31  
32 1 ▶sub new {
Subroutine new: called CC=9 SCAR=26.8
Pod documented
33 1 my ($class, $pdf, %options) = @_;
34 # copy dashed option names to preferred undashed names
35 0 ▶ if (defined $options{'-ean'} && !defined $options{'ean'}) { $options{'ean'} = delete($options{'-ean'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-ean"} and not defined $options{"ean"}) 0 1 1
Condition33%1/3
defined $options{"-ean"} and not defined $options{"ean"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $options{"-ean"} and not defined $options{"ean"}
defined $options{"-ean"} not defined $options{"ean"}
Truth table33%1/3
A and not(B)
A= defined $options{"-ean"}B= not defined $options{"ean"}
ABresult
0X0
100
111
36 1 ▶ if (defined $options{'-code'} && !defined $options{'code'}) { $options{'code'} = delete($options{'-code'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-code"} and not defined $options{"code"}) 1 0 1
Condition33%1/3
defined $options{"-code"} and not defined $options{"code"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $options{"-code"} and not defined $options{"code"}
defined $options{"-code"} not defined $options{"code"}
Truth table33%1/3
A and not(B)
A= defined $options{"-code"}B= not defined $options{"code"}
ABresult
0X0
100
111
37 0 ▶ if (defined $options{'-type'} && !defined $options{'type'}) { $options{'type'} = delete($options{'-type'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-type"} and not defined $options{"type"}) 0 1 1
Condition33%1/3
defined $options{"-type"} and not defined $options{"type"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $options{"-type"} and not defined $options{"type"}
defined $options{"-type"} not defined $options{"type"}
Truth table33%1/3
A and not(B)
A= defined $options{"-type"}B= not defined $options{"type"}
ABresult
0X0
100
111
38  
39 1 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 1 1
40  
41 1 my $self = $class->SUPER::new($pdf, %options);
42  
43 1 my @bars;
44 1 ▶ if ($options{'ean'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'ean'}) { } 0 1 1
45 0 @bars = $self->encode_ean128($options{'code'});
46 } else {
47 1 @bars = $self->encode_128($options{'type'}, $options{'code'});
48 }
49  
50 1 $self->drawbar(\@bars, $options{'caption'});
51  
52 1 return $self;
53 }
54  
55 # CODE-A Encoding Table
56 2 my $code128a = q| !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_| . join('', map { chr($_) } (0..31)) . qq/\xf3\xf2\x80\xcc\xcb\xf4\xf1\x8a\x8b\x8c\xff/;
57  
58 # CODE-B Encoding Table
59 2 my $code128b = q| !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|.qq/|}~\x7f\xf3\xf2\x80\xcc\xf4\xca\xf1\x8a\x8b\x8c\xff/;
60  
61 # CODE-C Encoding Table (00-99 are placeholders)
62 2 my $code128c = ("\xfe" x 100) . qq/\xcb\xca\xf1\x8a\x8b\x8c\xff/;
63  
64 # START A-C
65 2 my $bar128Sa = "\x8a";
66 2 my $bar128Sb = "\x8b";
67 2 my $bar128Sc = "\x8c";
68  
69 # FNC1-FNC4
70 2 my $bar128F1 = "\xf1";
71 2 my $bar128F2 = "\xf2";
72 2 my $bar128F3 = "\xf3";
73 2 my $bar128F4 = "\xf4";
74  
75 # CODE A-C
76 2 my $bar128Ca = "\xca";
77 2 my $bar128Cb = "\xcb";
78 2 my $bar128Cc = "\xcc";
79  
80 # SHIFT
81 2 my $bar128sh = "\x80";
82  
83 # STOP
84 2 my $bar128St = "\xff";
85  
86 # Note: The stop code (last position) is longer than the other codes because
87 # it also has the termination bar appended, rather than requiring it be added
88 # as a separate call.
89 2 my @bar128 = qw(
90 212222 222122 222221 121223 121322 131222 122213 122312 132212 221213
91 221312 231212 112232 122132 122231 113222 123122 123221 223211 221132
92 221231 213212 223112 312131 311222 321122 321221 312212 322112 322211
93 212123 212321 232121 111323 131123 131321 112313 132113 132311 211313
94 231113 231311 112133 112331 132131 113123 113321 133121 313121 211331
95 231131 213113 213311 213131 311123 311321 331121 312113 312311 332111
96 314111 221411 431111 111224 111422 121124 121421 141122 141221 112214
97 112412 122114 122411 142112 142211 241211 221114 413111 241112 134111
98 111242 121142 121241 114212 124112 124211 411212 421112 421211 212141
99 214121 412121 111143 111341 131141 114113 114311 411113 411311 113141
100 114131 311141 411131 b1a4a2 b1a2a4 b1a2c2 b3c1a1b
101 );
102  
103 37 ▶sub encode_128_char_idx {
Subroutine encode_128_char_idx: called CC=8 SCAR=21.1
Pod undocumented
104 37 my ($code, $char) = @_;
105  
106 37 my $index;
107  
108 37 ▶ if (lc($code) eq 'a') {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $code eq 'a') { } 6 31 37
elsif (lc $code eq 'b') { } 10 21 31
elsif (lc $code eq 'c') { } 21 0 21
109 # Ignore CODE-A request if we're already in CODE-A
110 6 ▶ return if $char eq $bar128Ca;
Branch: 1 branch
BranchTrueFalseTotal
if $char eq $bar128Ca 0 6 6
111  
112 6 $index = index($code128a, $char);
113 } elsif (lc($code) eq 'b') {
114 # Ignore CODE-B request if we're already in CODE-B
115 10 ▶ return if $char eq $bar128Cb;
Branch: 1 branch
BranchTrueFalseTotal
if $char eq $bar128Cb 0 10 10
116 10 $index = index($code128b, $char);
117 } elsif (lc($code) eq 'c') {
118 # Ignore CODE-C request if we're already in CODE-C
119 21 ▶ return if $char eq $bar128Cc;
Branch: 1 branch
BranchTrueFalseTotal
if $char eq $bar128Cc 0 21 21
120  
121 21 ▶ if ($char =~ /^([0-9][0-9])$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /^([0-9][0-9])$/) { } 15 6 21
122 15 $index = $1;
123 } else {
124 6 $index = index($code128c, $char);
125 }
126 }
127  
128 37 return ($bar128[$index], $index);
129 }
130  
131 10 ▶sub encode_128_char {
Subroutine encode_128_char: called CC=1 SCAR=0.0
Pod undocumented
132 10 my ($code, $char) = @_;
133  
134 10 my ($b) = encode_128_char_idx($code, $char);
135 10 return $b;
136 }
137  
138 5 ▶sub encode_128_string {
Subroutine encode_128_string: called CC=18 SCAR=34.5
Pod undocumented
139 5 my ($code, $string) = @_;
140  
141 5 my ($bar, $index, @bars, @checksum);
142 5 my @characters = split(//, $string);
143  
144 5 my $character;
145 5 ▶ while (defined($character = shift @characters)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($character = shift @characters)) 26 5 31
146 26 ▶ if ($character =~ /[\xf1-\xf4]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($character =~ /[\xf1-\xf4]/) { } 1 25 26
elsif ($character =~ /[\xCA-\xCC]/) { } 0 25 25
147 # CODE-C doesn't have FNC2-FNC4
148 1 ▶ if ($character =~ /[\xf2-\xf4]/ and $code eq 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($character =~ /[\xf2-\xf4]/ and $code eq "c") 0 1 1
Condition33%1/3
$character =~ /[\xf2-\xf4]/ and $code eq "c"
!ll&&!rl&&r
100
MC/DC0%0/2
$character =~ /[\xf2-\xf4]/ and $code eq "c"
$character =~ /[\xf2-\xf4]/ $code eq "c"
Truth table33%1/3
A and B
A= $character =~ /[\xf2-\xf4]/B= $code eq "c"
ABresult
0X0
100
111
149 0 ($bar, $index) = encode_128_char_idx($code, "\xCB");
150 0 push @bars, $bar;
151 0 push @checksum, $index;
152 0 $code = 'b';
153 }
154  
155 1 ($bar, $index) = encode_128_char_idx($code, $character);
156 } elsif ($character =~ /[\xCA-\xCC]/) {
157 0 ($bar, $index) = encode_128_char_idx($code, $character);
158 0 ▶ $code = ($character eq "\xCA"? 'a':
Branch: 2 branches
BranchTrueFalseTotal
$character eq "\312" ? : 0 0 0
$character eq "\313" ? : 0 0 0
159 $character eq "\xCB"? 'b': 'c');
160 } else {
161 25 ▶ if ($code ne 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 'c') { } 9 16 25
162 # SHIFT: Switch codes for the following character only
163 9 ▶ if ($character eq $bar128sh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character eq $bar128sh) { } 0 9 9
164 0 ($bar, $index) = encode_128_char_idx($code, $character);
165 0 push @bars, $bar;
166 0 push @checksum, $index;
167 0 $character = shift(@characters);
168 0 ▶ ($bar, $index) = encode_128_char_idx($code eq 'a'? 'b': 'a', $character);
Branch: 1 branch
BranchTrueFalseTotal
$code eq 'a' ? : 0 0 0
169 } else {
170 9 ($bar, $index) = encode_128_char_idx($code, $character);
171 }
172 } else {
173 16 ▶ $character .= shift(@characters) if $character =~ /\d/ and scalar @characters;
Branch: 1 branch
BranchTrueFalseTotal
if $character =~ /\d/ and scalar @characters 15 1 16
Condition50%1/2
$character =~ /\d/ and scalar @characters
!ll
016
MC/DC0%0/1
$character =~ /\d/ and scalar @characters
$character =~ /\d/
Truth table50%1/2
A
A= $character =~ /\d/
Aresult
00
11
174 16 ▶ if ($character =~ /^[^\d]*$/ or $character =~ /^\d[^\d]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character =~ /^[^\d]*$/ or $character =~ /^\d[^\d]*$/) 1 15 16
Condition66%2/3
$character =~ /^[^\d]*$/ or $character =~ /^\d[^\d]*$/
l!l&&r!l&&!r
0115
MC/DC50%1/2
$character =~ /^[^\d]*$/ or $character =~ /^\d[^\d]*$/
$character =~ /^[^\d]*$/ $character =~ /^\d[^\d]*$/
Truth table66%2/3
A or B
A= $character =~ /^[^\d]*$/B= $character =~ /^\d[^\d]*$/
ABresult
1X1
011
000
175 1 ($bar, $index) = encode_128_char_idx($code, "\xCB");
176 1 push @bars, $bar;
177 1 push @checksum, $index;
178 1 $code = 'b';
179 }
180 16 ▶ if ($character =~ /^\d[^\d]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character =~ /^\d[^\d]*$/) 1 15 16
181 1 ▶ unshift(@characters, substr($character, 1, 1)) if length($character) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $character > 1 0 1 1
182 1 $character = substr($character, 0, 1);
183 }
184 16 ($bar, $index) = encode_128_char_idx($code, $character);
185 }
186 }
187 26 ▶ $character = '' if $character =~ /[^\x20-\x7e]/;
Branch: 1 branch
BranchTrueFalseTotal
if $character =~ /[^\x20-\x7e]/ 1 25 26
188 26 push @bars, [$bar, $character];
189 26 push @checksum, $index;
190 }
191 5 return ([@bars], @checksum);
192 }
193  
194 5 ▶sub encode_128 {
Subroutine encode_128: called CC=9 SCAR=22.2
Pod undocumented
195 5 my ($self, $code, $string) = @_;
196 5 my @bars;
197 my $checksum_value;
198  
199 # Default to Code C if all characters are digits (and there are at
200 # least two of them). Otherwise, default to Code B.
201 5 ▶ $code ||= $string =~ /^\d{2,}$/? 'c': 'b';
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /^\d{2,}$/ ? : 0 1 1
Condition66%2/3
$code ||= $string =~ /^\d{2,}$/ ? "c" : "b"
l!l&&r!l&&!r
410
MC/DC0%0/2
$code ||= $string =~ /^\d{2,}$/ ? "c" : "b"
$code $string =~ /^\d{2,}$/ ? "c" : "b"
Truth table66%2/3
A ||= B
A= $codeB= $string =~ /^\d{2,}$/ ? "c" : "b"
ABresult
1X1
011
000
202  
203 # Allow the character set to be passed as a capital letter
204 # (consistent with the specification).
205 5 ▶ $code = lc($code) if $code =~ /^[A-C]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /^[A-C]$/ 0 5 5
206  
207 # Ensure a valid character set has been chosen.
208 5 ▶ die "Character set must be A, B, or C (not '$code')" unless $code =~ /^[a-c]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /^[a-c]$/ 0 5 5
209  
210 5 ▶ if ($code eq 'a') {
Branch: 3 branches
BranchTrueFalseTotal
if ($code eq 'a') { } 1 4 5
elsif ($code eq 'b') { } 2 2 4
elsif ($code eq 'c') { } 2 0 2
211 1 push @bars, encode_128_char($code, $bar128Sa);
212 1 $checksum_value = 103;
213 } elsif ($code eq 'b') {
214 2 push @bars, encode_128_char($code, $bar128Sb);
215 2 $checksum_value = 104;
216 } elsif ($code eq 'c') {
217 2 push @bars, encode_128_char($code, $bar128Sc);
218 2 $checksum_value = 105;
219 }
220 5 my ($bar, @checksum_values) = encode_128_string($code, $string);
221  
222 5 push @bars, @{$bar};
223  
224 # Calculate the checksum value
225 5 foreach my $i (1 .. scalar @checksum_values) {
226 27 $checksum_value += $i * $checksum_values[$i - 1];
227 }
228 5 $checksum_value %= 103;
229 5 push @bars, $bar128[$checksum_value];
230 5 push @bars, encode_128_char($code, $bar128St);
231  
232 5 return @bars;
233 }
234  
235 1 ▶sub encode_ean128 {
Subroutine encode_ean128: called CC=1 SCAR=0.0
Pod undocumented
236 1 my ($self, $string) = @_;
237  
238 1 $string =~ s/[^a-zA-Z\d]+//g;
239 1 $string =~ s/(\d+)([a-zA-Z]+)/$1\xcb$2/g;
240 1 $string =~ s/([a-zA-Z]+)(\d+)/$1\xcc$2/g;
241  
242 1 return $self->encode_128('c', "\xf1$string");
243 }
244  
245 2 1;
« Builder/Resource/XObject/Image/JPEG.pm ↑ Summary Builder/Resource/ColorSpace/Indexed/ACTFile.pm »