Barcode/UPCE.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 136 CC 13 SCAR 25.7
CC
13
Cov
0%
init
30.0
calcUPCECD
20.0
plot
12.0
CRAP 13.1
1 package GD::Barcode::UPCE;
2 use strict;
3 BEGIN { eval{require 'GD.pm';}; };
4 use GD::Barcode;
5 require Exporter;
6 use vars qw($VERSION @ISA $errStr);
7 @ISA = qw(GD::Barcode Exporter);
8 $VERSION=1.10;
9 my $oddEven4UPCE = {
10 0 => 'EEEOOO',
11 1 => 'EEOEOO',
12 2 => 'EEOOEO',
13 3 => 'EEOOOE',
14 4 => 'EOEEOO',
15 5 => 'EOOEEO',
16 6 => 'EOOOEE',
17 7 => 'EOEOEO',
18 8 => 'EOEOOE',
19 9 => 'EOOEOE'
20 };
21 my $leftOddBar ={
22 '0' => '0001101',
23 '1' => '0011001',
24 '2' => '0010011',
25 '3' => '0111101',
26 '4' => '0100011',
27 '5' => '0110001',
28 '6' => '0101111',
29 '7' => '0111011',
30 '8' => '0110111',
31 '9' => '0001011'
32 };
33 my $leftEvenBar = {
34 '0' => '0100111',
35 '1' => '0110011',
36 '2' => '0011011',
37 '3' => '0100001',
38 '4' => '0011101',
39 '5' => '0111001',
40 '6' => '0000101',
41 '7' => '0010001',
42 '8' => '0001001',
43 '9' => '0010111'
44 };
45 my $guardBar = 'G0G';
46 my $UPCrightGuardBar = '0G0G0G';
47 #------------------------------------------------------------------------------
48 # new (for Spreadsheet::ParseExcel)
49 #------------------------------------------------------------------------------
50 sub new($$) {
51 my($sClass, $sTxt) = @_;
52 $errStr ='';
53 my $oThis = {};
54 bless $oThis;
55 return undef if($errStr = $oThis->init($sTxt));
56 return $oThis;
57 }
58 #------------------------------------------------------------------------------
59 # init (for Spreadsheet::ParseExcel)
60 #------------------------------------------------------------------------------
61 sub init($$){
62 my($oThis, $sTxt) =@_;
63 return 'Invalid characters' if($sTxt =~ /[^0-9]/);
64  
65 #Check
66 my $iLen = length($sTxt);
67 if( $iLen == 6 ) {
68 $sTxt = '0' . $sTxt;
69 $sTxt .= calcUPCECD( $sTxt );
70 }
71 elsif($iLen == 7) {
72 $sTxt .= calcUPCECD( $sTxt );
73 }
74 elsif($iLen == 8) {
75 ;
76 }
77 else {
78 return 'Invalid Length';
79 }
80 $oThis->{text} = $sTxt;
81 return '';
82 }
83 #------------------------------------------------------------------------------
84 # calcUPCACD (for GD::Barcode::UPCE)
85 #------------------------------------------------------------------------------
86 sub calcUPCACD {
87 my( $sTxt ) = @_;
88 my( $i, $iSum, @aWeight);
89  
90 @aWeight = (3,1,3,1,3,1,3,1,3,1,3);
91 $iSum = 0;
92 for( $i = 0; $i < 11; $i++ ) {
93 $iSum += substr($sTxt, $i, 1) * $aWeight[$i];
94 }
95 $iSum %= 10;
96 $iSum = ($iSum == 0)? 0: (10 - $iSum);
97 return "$iSum";
98 }
99 #------------------------------------------------------------------------------
100 # calcUPCECD (for GD::Barcode::UPCE)
101 #------------------------------------------------------------------------------
102 sub calcUPCECD {
103 my( $sTxt ) =@_;
104 my( $upcA );
105  
106 my $cLast = substr($sTxt, 6, 1);
107 if ($cLast =~ /[0-2]/) { #0,1,2
108 $upcA = substr($sTxt, 0, 3). $cLast . '0' x 4 . substr($sTxt, 3, 3);
109 }
110 elsif ($cLast eq '3') {
111 $upcA = substr($sTxt, 0, 4) . '0' x 5 . substr($sTxt, 4, 2);
112 }
113 elsif ($cLast eq '4') {
114 $upcA = substr($sTxt, 0, 5) . '0' x 5 . substr($sTxt, 5, 1);
115 }
116 else { # $cLast =~ /5-9/
117 $upcA = substr($sTxt, 0, 6) . '0' x 4 . $cLast;
118 }
119 return &calcUPCACD( $upcA );
120 }
121 #------------------------------------------------------------------------------
122 # barcode (for GD::Barcode::UPCE)
123 #------------------------------------------------------------------------------
124 sub barcode($) {
125 my ($oThis) = @_;
126 my ($topDigit, $oddEven, $c, $i);
127 my ($sRes);
128  
129 #(1)Init
130 my $sTxt = $oThis->{text};
131 $sRes = $guardBar; #GUARD
132 $oddEven = $oddEven4UPCE->{substr($sTxt, 7, 1)};
133  
134 #(2)Left 6 (Skip 1 character)
135 for( $i = 1; $i < 7; $i++ ){
136 $c = substr($sTxt, $i, 1);
137 $sRes .= GD::Barcode::barPtn($c,
138 ( substr($oddEven, $i-1, 1) eq 'O' )?
139 $leftOddBar : $leftEvenBar);
140 }
141 #
142 $sRes .= $UPCrightGuardBar;
143 return $sRes;
144  
145 }
146 #------------------------------------------------------------------------------
147 # plot (for Spreadsheet::ParseExcel)
148 #------------------------------------------------------------------------------
149 sub plot($%) {
150 my($oThis, %hParam) =@_;
151 my $sTxt = $oThis->{text};
152 my $sPtn = $oThis->barcode();
153  
154 #Create Image
155 my $iHeight = ($hParam{Height})? $hParam{Height} : 50;
156 my ($oGd, $cBlack);
157 if($hParam{NoText}) {
158 ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0);
159 }
160 else {
161 my($fW,$fH) = (GD::Font->Small->width, GD::Font->Small->height);
162 my $iWidth = length($sPtn)+ 2*($fW+1);
163 #Bar Image
164 ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, $fW+1);
165 #String
166 $oGd->string(GD::Font->Small, 0, $iHeight - $fH, substr($sTxt, 0, 1), $cBlack);
167 $oGd->string(GD::Font->Small, $fW + 8, $iHeight - $fH, substr($sTxt, 1, 6), $cBlack);
168 $oGd->string(GD::Font->Small, $fW + 54, $iHeight - $fH, substr($sTxt, 7, 1), $cBlack);
169 }
170 return $oGd;
171  
172 return $oGd;
173 }
174 1;
175 __END__