Barcode/EAN8.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 110 CC 8 SCAR 21.5
CC
8
Cov
0%
init
20.0
plot
12.0
new
6.0
CRAP 8.6
1 package GD::Barcode::EAN8;
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 $leftOddBar ={
10 '0' => '0001101',
11 '1' => '0011001',
12 '2' => '0010011',
13 '3' => '0111101',
14 '4' => '0100011',
15 '5' => '0110001',
16 '6' => '0101111',
17 '7' => '0111011',
18 '8' => '0110111',
19 '9' => '0001011'
20 };
21 my $rightBar = {
22 '0' => '1110010',
23 '1' => '1100110',
24 '2' => '1101100',
25 '3' => '1000010',
26 '4' => '1011100',
27 '5' => '1001110',
28 '6' => '1010000',
29 '7' => '1000100',
30 '8' => '1001000',
31 '9' => '1110100'
32 };
33 my $guardBar = 'G0G';
34 my $centerBar = '0G0G0';
35 #------------------------------------------------------------------------------
36 # new (for GD::Barcode::EAN8)
37 #------------------------------------------------------------------------------
38 sub new($$) {
39 my($sClass, $sTxt) = @_;
40 $errStr ='';
41 my $oThis = {};
42 bless $oThis;
43 return undef if($errStr = $oThis->init($sTxt));
44 return $oThis;
45 }
46 #------------------------------------------------------------------------------
47 # init (for GD::Barcode::EAN8)
48 #------------------------------------------------------------------------------
49 sub init($$){
50 my($oThis, $sTxt) =@_;
51 #Check
52 return 'Invalid Characters' if($sTxt =~ /[^0-9]/);
53  
54 #CalcCd
55 if( length($sTxt) == 7 ) {
56 $sTxt .= calcEAN8CD( $sTxt ) ;
57 }
58 elsif(length($sTxt) == 8) {
59 ;
60 }
61 else {
62 return 'Invalid Length';
63 }
64 $oThis->{text} = $sTxt;
65 return '';
66 }
67 #------------------------------------------------------------------------------
68 # calcEAN8CD (for GD::Barcode::EAN8)
69 #------------------------------------------------------------------------------
70 sub calcEAN8CD($) {
71 my( $sTxt ) = @_;
72 my( $i, $iSum);
73  
74 my @aWeight = (3, 1, 3, 1, 3, 1, 3);
75 $iSum = 0;
76 for( $i = 0; $i < 7; $i++ ){
77 $iSum += substr($sTxt, $i, 1) * $aWeight[$i];
78 }
79 $iSum %= 10;
80 $iSum = ($iSum == 0)? 0: (10 - $iSum);
81 return "$iSum";
82 }
83 #------------------------------------------------------------------------------
84 # new (for GD::Barcode::EAN8)
85 #------------------------------------------------------------------------------
86 sub barcode($) {
87 my ($oThis) = @_;
88 my($i, $sRes);
89 #(1) Init
90 my $sTxt = $oThis->{text};
91 $sRes = $guardBar; #GUARD
92  
93 #(2) Left 4
94 for( $i = 0; $i < 4; $i++ ) {
95 $sRes .= GD::Barcode::barPtn( substr($sTxt, $i, 1), $leftOddBar );
96 }
97  
98 #(3) Center
99 $sRes .= $centerBar;
100  
101 #(4) Right 4 bytes
102 for( $i = 4; $i < 8; $i++ ) {
103 $sRes .= GD::Barcode::barPtn( substr($sTxt, $i, 1), $rightBar );
104 }
105 #(5)GUARD
106 $sRes .= $guardBar;
107 return $sRes;
108 }
109 #------------------------------------------------------------------------------
110 # plot (for GD::Barcode::EAN8)
111 #------------------------------------------------------------------------------
112 sub plot($;%) {
113 my($oThis, %hParam) =@_;
114  
115 my $sPtn = $oThis->barcode();
116  
117 #Create Image
118 my $iHeight = ($hParam{Height})? $hParam{Height} : 50;
119 my($oGd, $cBlack);
120 if($hParam{NoText}) {
121 ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0);
122 }
123 else {
124 my($fW,$fH) = (GD::Font->Small->width,GD::Font->Small->height);
125 my $iWidth = length($sPtn);
126 #$ ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, $fW+1);
127 ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, 0);
128 $oGd->string(GD::Font->Small, $fW + 1, $iHeight - $fH, substr($oThis->{text}, 0, 4), $cBlack);
129 $oGd->string(GD::Font->Small, $fW + 33, $iHeight - $fH, substr($oThis->{text}, 4, 4), $cBlack);
130 }
131 return $oGd;
132 }
133 1;
134 __END__