Barcode/UPCE.pm

filter: Statementstmt 68.7% 55 / 80 Branchbran 60.7% 17 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 8 / 10 Podpod 50.0% 3 / 6 totaltotal 66.9% 83 / 124 CC 15 SCAR 15.7
CC
15
Cov
67%
plot
12.0
new
6.0
init
5.1
CRAP 4.8
« Barcode.pm ↑ Summary Barcode/EAN13.pm »
1 package GD::Barcode::UPCE;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use GD::Barcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION @ISA $errStr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 @ISA = qw(GD::Barcode Exporter);
8 1 $VERSION = '2.02';
9 1 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 1 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 1 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 1 my $guardBar = 'G0G';
46 1 my $UPCrightGuardBar = '0G0G0G';
47  
48 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
49 0 my ( $sClass, $sTxt ) = @_;
50 0 $errStr = '';
51 0 my $oThis = {};
52 0 bless $oThis, $sClass;
53 0 ▶ return if ( $errStr = $oThis->init($sTxt) );
Branch: 1 branch
BranchTrueFalseTotal
if $GD::Barcode::UPCE::errStr = $oThis->init($sTxt) 0 0 0
54 0 return $oThis;
55 }
56  
57 3 ▶sub init {
Subroutine init: called CC=5 SCAR=16.3
Pod undocumented
58 3 my ( $oThis, $sTxt ) = @_;
59 3 ▶ return 'Invalid characters' if ( $sTxt =~ /[^0-9]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $sTxt =~ /[^0-9]/ 0 3 3
60  
61 #Check
62 3 my $iLen = length($sTxt);
63 3 ▶ if ( $iLen == 6 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($iLen == 6) { } 1 2 3
elsif ($iLen == 7) { } 1 1 2
elsif ($iLen == 8) { } 1 0 1
64 1 $sTxt = '0' . $sTxt;
65 1 $sTxt .= calcUPCECD($sTxt);
66 }
67 elsif ( $iLen == 7 ) {
68 1 $sTxt .= calcUPCECD($sTxt);
69 }
70 elsif ( $iLen == 8 ) {
71 ;
72 }
73 else {
74 0 return 'Invalid Length';
75 }
76 3 $oThis->{text} = $sTxt;
77 3 return '';
78 }
79  
80 2 ▶sub calcUPCACD {
Subroutine calcUPCACD: called CC=3 SCAR=11.0
Pod undocumented
81 2 my ($sTxt) = @_;
82 2 my ( $i, $iSum, @aWeight );
83  
84 2 @aWeight = ( 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3 );
85 2 $iSum = 0;
86 2 ▶ for ( $i = 0 ; $i < 11 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 11) 22 2 24
87 22 $iSum += substr( $sTxt, $i, 1 ) * $aWeight[$i];
88 }
89 2 $iSum %= 10;
90 2 ▶ $iSum = ( $iSum == 0 ) ? 0 : ( 10 - $iSum );
Branch: 1 branch
BranchTrueFalseTotal
$iSum == 0 ? : 1 1 2
91 2 return "$iSum";
92 }
93  
94 2 ▶sub calcUPCECD {
Subroutine calcUPCECD: called CC=4 SCAR=16.1
Pod undocumented
95 2 my ($sTxt) = @_;
96 2 my ($upcA);
97  
98 2 my $cLast = substr( $sTxt, 6, 1 );
99 2 ▶ if ( $cLast =~ /[0-2]/ ) { #0,1,2
Branch: 3 branches
BranchTrueFalseTotal
if ($cLast =~ /[0-2]/) { } 0 2 2
elsif ($cLast eq '3') { } 0 2 2
elsif ($cLast eq '4') { } 0 2 2
100 0 $upcA =
101 substr( $sTxt, 0, 3 ) . $cLast . '0' x 4 . substr( $sTxt, 3, 3 );
102 }
103 elsif ( $cLast eq '3' ) {
104 0 $upcA = substr( $sTxt, 0, 4 ) . '0' x 5 . substr( $sTxt, 4, 2 );
105 }
106 elsif ( $cLast eq '4' ) {
107 0 $upcA = substr( $sTxt, 0, 5 ) . '0' x 5 . substr( $sTxt, 5, 1 );
108 }
109 else { # $cLast =~ /5-9/
110 2 $upcA = substr( $sTxt, 0, 6 ) . '0' x 4 . $cLast;
111 }
112 2 return &calcUPCACD($upcA);
113 }
114  
115 3 ▶sub barcode {
Subroutine barcode: called CC=3 SCAR=11.0
Pod documented
116 3 my ($oThis) = @_;
117 3 my ( $topDigit, $oddEven, $c, $i );
118 3 my ($sRes);
119  
120 #(1)Init
121 3 my $sTxt = $oThis->{text};
122 3 $sRes = $guardBar; #GUARD
123 3 $oddEven = $oddEven4UPCE->{ substr( $sTxt, 7, 1 ) };
124  
125 #(2)Left 6 (Skip 1 character)
126 3 ▶ for ( $i = 1 ; $i < 7 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 7) 18 3 21
127 18 $c = substr( $sTxt, $i, 1 );
128 18 ▶ $sRes .= GD::Barcode::barPtn( $c,
Branch: 1 branch
BranchTrueFalseTotal
substr($oddEven, $i - 1, 1) eq 'O' ? : 9 9 18
129 ( substr( $oddEven, $i - 1, 1 ) eq 'O' )
130 ? $leftOddBar
131 : $leftEvenBar );
132 }
133 #
134 3 $sRes .= $UPCrightGuardBar;
135 3 return $sRes;
136  
137 }
138  
139 0 ▶sub plot {
Subroutine plot: not called CC=3 SCAR=24.8
Pod documented
140 0 my ( $oThis, %hParam ) = @_;
141 0 my $sTxt = $oThis->{text};
142 0 my $sPtn = $oThis->barcode();
143  
144 #Create Image
145 0 require GD;
146 0 ▶ my $iHeight = ( $hParam{Height} ) ? $hParam{Height} : 50;
Branch: 1 branch
BranchTrueFalseTotal
$hParam{'Height'} ? : 0 0 0
147 0 my ( $oGd, $cBlack );
148 0 ▶ if ( $hParam{NoText} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hParam{'NoText'}) { } 0 0 0
149 0 ( $oGd, $cBlack ) =
150 GD::Barcode::plot( $sPtn, length($sPtn), $iHeight, 0, 0 );
151 }
152 else {
153 0 my ( $fW, $fH ) = ( GD::Font->Small->width, GD::Font->Small->height );
154 0 my $iWidth = length($sPtn) + 2 * ( $fW + 1 );
155  
156 #Bar Image
157 0 ( $oGd, $cBlack ) =
158 GD::Barcode::plot( $sPtn, $iWidth, $iHeight, $fH, $fW + 1 );
159  
160 #String
161 0 $oGd->string(
162 GD::Font->Small, 0,
163 $iHeight - $fH,
164 substr( $sTxt, 0, 1 ), $cBlack
165 );
166 0 $oGd->string(
167 GD::Font->Small, $fW + 8,
168 $iHeight - $fH,
169 substr( $sTxt, 1, 6 ), $cBlack
170 );
171 0 $oGd->string(
172 GD::Font->Small, $fW + 54,
173 $iHeight - $fH,
174 substr( $sTxt, 7, 1 ), $cBlack
175 );
176 }
177 0 return $oGd;
178 }
179 1 1;
180 __END__
« Barcode.pm ↑ Summary Barcode/EAN13.pm »