Barcode/EAN13.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 121 CC 9 SCAR 21.6
CC
9
Cov
0%
init
20.0
plot
12.0
new
6.0
CRAP 8.6
1 package GD::Barcode::EAN13;
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 $leftEvenBar = {
22 '0' => '0100111',
23 '1' => '0110011',
24 '2' => '0011011',
25 '3' => '0100001',
26 '4' => '0011101',
27 '5' => '0111001',
28 '6' => '0000101',
29 '7' => '0010001',
30 '8' => '0001001',
31 '9' => '0010111'
32 };
33 my $rightBar = {
34 '0' => '1110010',
35 '1' => '1100110',
36 '2' => '1101100',
37 '3' => '1000010',
38 '4' => '1011100',
39 '5' => '1001110',
40 '6' => '1010000',
41 '7' => '1000100',
42 '8' => '1001000',
43 '9' => '1110100'
44 };
45 my $guardBar = 'G0G';
46 my $centerBar = '0G0G0';
47 my $oddEven4EAN = {
48 0 => 'OOOOOO',
49 1 => 'OOEOEE',
50 2 => 'OOEEOE',
51 3 => 'OOEEEO',
52 4 => 'OEOOEE',
53 5 => 'OEEOOE',
54 6 => 'OEEEOO',
55 7 => 'OEOEOE',
56 8 => 'OEOEEO',
57 9 => 'OEEOEO'
58 };
59 #------------------------------------------------------------------------------
60 # new (for GD::Barcode::EAN13)
61 #------------------------------------------------------------------------------
62 sub new($$) {
63 my($sClass, $sTxt) = @_;
64 $errStr ='';
65 my $oThis = {};
66 bless $oThis;
67 return undef if($errStr = $oThis->init($sTxt));
68 return $oThis;
69 }
70 #------------------------------------------------------------------------------
71 # init (for GD::Barcode::EAN13)
72 #------------------------------------------------------------------------------
73 sub init($$){
74 my($oThis, $sTxt) =@_;
75 #Check
76 return 'Invalid Characters' if($sTxt =~ /[^0-9]/);
77 #CalcCd
78 if( length($sTxt) == 12 ) {
79 $sTxt .= calcEAN13CD( $sTxt ) ;
80 }
81 elsif(length($sTxt) == 13) {
82 ;
83 }
84 else {
85 return 'Invalid Length';
86 }
87 $oThis->{text} = $sTxt;
88 return '';
89 }
90 #==================================================================
91 # Check digit for EAN13
92 #==================================================================
93 sub calcEAN13CD($) {
94 my( $sTxt ) =@_;
95 my( $i, $iSum);
96 my @aWeight = (1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3);
97 $iSum = 0;
98 for( $i = 0; $i < 12; $i++ ){
99 $iSum += substr($sTxt, $i, 1) * $aWeight[$i];
100 }
101 $iSum %= 10;
102 $iSum = ($iSum == 0)? 0: (10 - $iSum);
103 return "$iSum";
104 }
105 #------------------------------------------------------------------------------
106 # barcode (for GD::Barcode::EAN13)
107 #------------------------------------------------------------------------------
108 sub barcode($) {
109 my ($oThis) = @_;
110 my ($sTxt);
111 my ($oddEven, $i, $sBar);
112 my ($sRes);
113  
114 #(1)Init
115 $sTxt = $oThis->{text};
116 $sRes = $guardBar; #GUARD
117  
118 #(2)Left 7 letters
119 $oddEven = $oddEven4EAN->{substr($sTxt, 0, 1)};
120 for( $i = 1; $i < 7; $i++ ) {
121 $sBar = ( substr($oddEven, $i-1, 1) eq 'O')?
122 $leftOddBar : $leftEvenBar;
123 $sRes .= GD::Barcode::barPtn(
124 substr($sTxt, $i, 1), $sBar);
125 }
126 #(4)Center
127 $sRes .= $centerBar;
128  
129 #(5)Right
130 for( $i = 7; $i < 13; $i++ ) {
131 $sRes .= GD::Barcode::barPtn( substr($sTxt, $i, 1), $rightBar );
132 }
133 #(6)GUARD
134 $sRes .= $guardBar;
135 return $sRes;
136 }
137 #------------------------------------------------------------------------------
138 # plot (for GD::Barcode::EAN13)
139 #------------------------------------------------------------------------------
140 sub plot($;%) {
141 my($oThis, %hParam) = @_;
142  
143 #Barcode Pattern
144 my $sPtn = $oThis->barcode();
145  
146 #Create Image
147 my $iHeight = ($hParam{Height})? $hParam{Height} : 50;
148 my($oGd, $cBlack);
149 if($hParam{NoText}) {
150 ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0);
151 }
152 else {
153 my($fW,$fH) = (GD::Font->Small->width,GD::Font->Small->height);
154 my $iWidth = length($sPtn)+$fW+1;
155 ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, $fW+1);
156 $oGd->string(GD::Font->Small, 0, $iHeight - $fH, substr($oThis->{text}, 0, 1), $cBlack);
157 $oGd->string(GD::Font->Small, $fW +8 , $iHeight - $fH, substr($oThis->{text}, 1, 6), $cBlack);
158 $oGd->string(GD::Font->Small, $fW +55, $iHeight - $fH, substr($oThis->{text}, 7, 6), $cBlack);
159 }
160 return $oGd;
161 }
162 1;
163 __END__