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

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