File Coverage

blib/lib/PDF/API2/Resource/XObject/Form/BarCode/code3of9.pm
Criterion Covered Total %
statement 42 46 91.3
branch 6 8 75.0
condition n/a
subroutine 7 11 63.6
pod 1 8 12.5
total 56 73 76.7


line stmt bran cond sub pod time code
1             package PDF::API2::Resource::XObject::Form::BarCode::code3of9;
2              
3 2     2   1022 use base 'PDF::API2::Resource::XObject::Form::BarCode';
  2         5  
  2         219  
4              
5 2     2   13 use strict;
  2         5  
  2         43  
6 2     2   10 use warnings;
  2         4  
  2         1687  
7              
8             our $VERSION = '2.045'; # VERSION
9              
10             sub new {
11 2     2 1 10 my ($class, $pdf, %options) = @_;
12 2         17 my $self = $class->SUPER::new($pdf, %options);
13              
14             my @bars = encode_3of9($options{'-code'},
15             $options{'-chk'} ? 1 : 0,
16 2 50       13 $options{'-ext'} ? 1 : 0);
    50          
17              
18 2         17 $self->drawbar([@bars], $options{'caption'});
19              
20 2         12 return $self;
21             }
22              
23             my $code3of9 = q(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*);
24              
25             my @bar3of9 = qw(
26             1112212111 2112111121 1122111121 2122111111
27             1112211121 2112211111 1122211111 1112112121
28             2112112111 1122112111 2111121121 1121121121
29             2121121111 1111221121 2111221111 1121221111
30             1111122121 2111122111 1121122111 1111222111
31             2111111221 1121111221 2121111211 1111211221
32             2111211211 1121211211 1111112221 2111112211
33             1121112211 1111212211 2211111121 1221111121
34             2221111111 1211211121 2211211111 1221211111
35             1211112121 2211112111 1221112111 1212121111
36             1212111211 1211121211 1112121211 abaababaa1
37             );
38              
39             my @extended_map = (
40             '%U', '$A', '$B', '$C', '$D', '$E', '$F', '$G', '$H', '$I',
41             '$J', '$K', '$L', '$M', '$N', '$O', '$P', '$Q', '$R', '$S',
42             '$T', '$U', '$V', '$W', '$X', '$Y', '$Z', '%A', '%B', '%C',
43             '%D', '$E', ' ', '/A', '/B', '/C', '/D', '/E', '/F', '/G',
44             '/H', '/I', '/J', '/K', '/L', '-', '.', '/O', '0', '1',
45             '2', '3', '4', '5', '6', '7', '8', '9', '/Z', '%F',
46             '%G', '%H', '%I', '%J', '%V', 'A', 'B', 'C', 'D', 'E',
47             'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
48             'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
49             'Z', '%K', '%L', '%M', '%N', '%O', '%W', '+A', '+B', '+C',
50             '+D', '+E', '+F', '+G', '+H', '+I', '+J', '+K', '+L', '+M',
51             '+N', '+O', '+P', '+Q', '+R', '+S', '+T', '+U', '+V', '+W',
52             '+X', '+Y', '+Z', '%P', '%Q', '%R', '%S', '%T',
53             );
54              
55             sub encode_3of9_char {
56 36     36 0 57 my $character = shift();
57 36         82 return $bar3of9[index($code3of9, $character)];
58             }
59              
60             sub encode_3of9_string {
61 6     6 0 16 my ($string, $is_mod43) = @_;
62              
63 6         9 my $bar;
64 6         10 my $checksum = 0;
65 6         29 foreach my $char (split //, $string) {
66 24         42 $bar .= encode_3of9_char($char);
67 24         44 $checksum += index($code3of9, $char);
68             }
69              
70 6 100       16 if ($is_mod43) {
71 2         5 $checksum %= 43;
72 2         5 $bar .= $bar3of9[$checksum];
73             }
74              
75 6         19 return $bar;
76             }
77              
78             # Deprecated (rolled into encode_3of9_string)
79 0     0 0 0 sub encode_3of9_string_w_chk { return encode_3of9_string(shift(), 1); }
80              
81             sub encode_3of9 {
82 6     6 0 2758 my ($string, $is_mod43, $is_extended) = @_;
83              
84 6         10 my $display;
85 6 100       18 unless ($is_extended) {
86 4         13 $string = uc $string;
87 4         14 $string =~ s/[^0-9A-Z\-\.\ \$\/\+\%]+//g;
88 4         7 $display = $string;
89             }
90             else {
91             # Extended Code39 supports all 7-bit ASCII characters
92 2         8 $string =~ s/[^\x00-\x7f]//g;
93 2         4 $display = $string;
94              
95             # Encode, but don't display, non-printable characters
96 2         4 $display =~ s/[[:cntrl:]]//g;
97              
98 2         8 $string = join('', map { $extended_map[ord($_)] } split //, $string);
  8         22  
99             }
100              
101 6         14 my @bars;
102 6         15 push @bars, encode_3of9_char('*');
103 6         18 push @bars, [ encode_3of9_string($string, $is_mod43), $display ];
104 6         14 push @bars, encode_3of9_char('*');
105 6         29 return @bars;
106             }
107              
108             # Deprecated (rolled into encode_3of9)
109 0     0 0   sub encode_3of9_w_chk { return encode_3of9(shift(), 1, 0); }
110 0     0 0   sub encode_3of9_ext { return encode_3of9(shift(), 0, 1); }
111 0     0 0   sub encode_3of9_ext_w_chk { return encode_3of9(shift(), 1, 1); }
112              
113             1;