Barcode/EAN13.pm

filter: Statementstmt 73.3% 55 / 75 Branchbran 59.0% 13 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 8 / 10 Podpod 60.0% 3 / 5 totaltotal 70.5% 79 / 112 CC 12 SCAR 15.0
CC
12
Cov
70%
plot
12.0
new
6.0
init
4.1
CRAP 4.5
« Barcode/UPCE.pm ↑ Summary Barcode/UPCA.pm »
1 package GD::Barcode::EAN13;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use GD::Barcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '2.02';
9 1 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use vars qw($VERSION @ISA $errStr);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 @ISA = qw(GD::Barcode Exporter);
12 1 my $leftOddBar = {
13 '0' => '0001101',
14 '1' => '0011001',
15 '2' => '0010011',
16 '3' => '0111101',
17 '4' => '0100011',
18 '5' => '0110001',
19 '6' => '0101111',
20 '7' => '0111011',
21 '8' => '0110111',
22 '9' => '0001011'
23 };
24 1 my $leftEvenBar = {
25 '0' => '0100111',
26 '1' => '0110011',
27 '2' => '0011011',
28 '3' => '0100001',
29 '4' => '0011101',
30 '5' => '0111001',
31 '6' => '0000101',
32 '7' => '0010001',
33 '8' => '0001001',
34 '9' => '0010111'
35 };
36 1 my $rightBar = {
37 '0' => '1110010',
38 '1' => '1100110',
39 '2' => '1101100',
40 '3' => '1000010',
41 '4' => '1011100',
42 '5' => '1001110',
43 '6' => '1010000',
44 '7' => '1000100',
45 '8' => '1001000',
46 '9' => '1110100'
47 };
48 1 my $guardBar = 'G0G';
49 1 my $centerBar = '0G0G0';
50 1 my $oddEven4EAN = {
51 0 => 'OOOOOO',
52 1 => 'OOEOEE',
53 2 => 'OOEEOE',
54 3 => 'OOEEEO',
55 4 => 'OEOOEE',
56 5 => 'OEEOOE',
57 6 => 'OEEEOO',
58 7 => 'OEOEOE',
59 8 => 'OEOEEO',
60 9 => 'OEEOEO'
61 };
62  
63 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
64 0 my ( $sClass, $sTxt ) = @_;
65 0 $errStr = '';
66 0 my $oThis = {};
67 0 bless $oThis, $sClass;
68 0 ▶ return if ( $errStr = $oThis->init($sTxt) );
Branch: 1 branch
BranchTrueFalseTotal
if $GD::Barcode::EAN13::errStr = $oThis->init($sTxt) 0 0 0
69 0 return $oThis;
70 }
71  
72 2 ▶sub init {
Subroutine init: called CC=4 SCAR=14.0
Pod undocumented
73 2 my ( $oThis, $sTxt ) = @_;
74  
75 #Check
76 2 ▶ return 'Invalid Characters' if ( $sTxt =~ /[^0-9]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $sTxt =~ /[^0-9]/ 0 2 2
77  
78 #CalcCd
79 2 ▶ if ( length($sTxt) == 12 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $sTxt == 12) { } 1 1 2
elsif (length $sTxt == 13) { } 0 1 1
80 1 $sTxt .= calcEAN13CD($sTxt);
81 }
82 elsif ( length($sTxt) == 13 ) {
83 ;
84 }
85 else {
86 1 return 'Invalid Length';
87 }
88 1 $oThis->{text} = $sTxt;
89 1 return '';
90 }
91  
92 1 ▶sub calcEAN13CD {
Subroutine calcEAN13CD: called CC=3 SCAR=11.0
Pod undocumented
93 1 my ($sTxt) = @_;
94 1 my ( $i, $iSum );
95 1 my @aWeight = ( 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3 );
96 1 $iSum = 0;
97 1 ▶ for ( $i = 0 ; $i < 12 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 12) 12 1 13
98 12 $iSum += substr( $sTxt, $i, 1 ) * $aWeight[$i];
99 }
100 1 $iSum %= 10;
101 1 ▶ $iSum = ( $iSum == 0 ) ? 0 : ( 10 - $iSum );
Branch: 1 branch
BranchTrueFalseTotal
$iSum == 0 ? : 0 1 1
102 1 return "$iSum";
103 }
104  
105 1 ▶sub barcode {
Subroutine barcode: called CC=4 SCAR=13.9
Pod documented
106 1 my ($oThis) = @_;
107 1 my ($sTxt);
108 1 my ( $oddEven, $i, $sBar );
109 1 my ($sRes);
110  
111 #(1)Init
112 1 $sTxt = $oThis->{text};
113 1 $sRes = $guardBar; #GUARD
114  
115 #(2)Left 7 letters
116 1 $oddEven = $oddEven4EAN->{ substr( $sTxt, 0, 1 ) };
117 1 ▶ for ( $i = 1 ; $i < 7 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 7) 6 1 7
118 6 ▶ $sBar =
Branch: 1 branch
BranchTrueFalseTotal
substr($oddEven, $i - 1, 1) eq 'O' ? : 3 3 6
119 ( substr( $oddEven, $i - 1, 1 ) eq 'O' ) ? $leftOddBar : $leftEvenBar;
120 6 $sRes .= GD::Barcode::barPtn( substr( $sTxt, $i, 1 ), $sBar );
121 }
122  
123 #(4)Center
124 1 $sRes .= $centerBar;
125  
126 #(5)Right
127 1 ▶ for ( $i = 7 ; $i < 13 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 13) 6 1 7
128 6 $sRes .= GD::Barcode::barPtn( substr( $sTxt, $i, 1 ), $rightBar );
129 }
130  
131 #(6)GUARD
132 1 $sRes .= $guardBar;
133 1 return $sRes;
134 }
135  
136 0 ▶sub plot {
Subroutine plot: not called CC=3 SCAR=24.8
Pod documented
137 0 my ( $oThis, %hParam ) = @_;
138  
139 #Barcode Pattern
140 0 my $sPtn = $oThis->barcode();
141  
142 #Create Image
143 0 require GD;
144 0 ▶ my $iHeight = ( $hParam{Height} ) ? $hParam{Height} : 50;
Branch: 1 branch
BranchTrueFalseTotal
$hParam{'Height'} ? : 0 0 0
145 0 my ( $oGd, $cBlack );
146 0 ▶ if ( $hParam{NoText} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hParam{'NoText'}) { } 0 0 0
147 0 ( $oGd, $cBlack ) =
148 GD::Barcode::plot( $sPtn, length($sPtn), $iHeight, 0, 0 );
149 }
150 else {
151 0 my ( $fW, $fH ) = ( GD::Font->Small->width, GD::Font->Small->height );
152 0 my $iWidth = length($sPtn) + $fW + 1;
153 0 ( $oGd, $cBlack ) =
154 GD::Barcode::plot( $sPtn, $iWidth, $iHeight, $fH, $fW + 1 );
155 $oGd->string(
156 GD::Font->Small, 0,
157 $iHeight - $fH,
158 0 substr( $oThis->{text}, 0, 1 ), $cBlack
159 );
160 $oGd->string(
161 GD::Font->Small, $fW + 8,
162 $iHeight - $fH,
163 0 substr( $oThis->{text}, 1, 6 ), $cBlack
164 );
165 $oGd->string(
166 GD::Font->Small, $fW + 55,
167 $iHeight - $fH,
168 0 substr( $oThis->{text}, 7, 6 ), $cBlack
169 );
170 }
171 0 return $oGd;
172 }
173 1 1;
174 __END__
« Barcode/UPCE.pm ↑ Summary Barcode/UPCA.pm »