| 1 | package GD::Barcode::UPCA; | ||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use GD::Barcode; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use parent qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use vars qw($VERSION @ISA $errStr); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | @ISA = qw(GD::Barcode Exporter); | |||||||||||||
| 9 | 1 | our $VERSION = '2.02'; | |||||||||||||
| 10 | 1 | my $leftOddBar = { | |||||||||||||
| 11 | "0" => "0001101", | ||||||||||||||
| 12 | "1" => "0011001", | ||||||||||||||
| 13 | "2" => "0010011", | ||||||||||||||
| 14 | "3" => "0111101", | ||||||||||||||
| 15 | "4" => "0100011", | ||||||||||||||
| 16 | "5" => "0110001", | ||||||||||||||
| 17 | "6" => "0101111", | ||||||||||||||
| 18 | "7" => "0111011", | ||||||||||||||
| 19 | "8" => "0110111", | ||||||||||||||
| 20 | "9" => "0001011" | ||||||||||||||
| 21 | }; | ||||||||||||||
| 22 | 1 | my $rightBar = { | |||||||||||||
| 23 | "0" => "1110010", | ||||||||||||||
| 24 | "1" => "1100110", | ||||||||||||||
| 25 | "2" => "1101100", | ||||||||||||||
| 26 | "3" => "1000010", | ||||||||||||||
| 27 | "4" => "1011100", | ||||||||||||||
| 28 | "5" => "1001110", | ||||||||||||||
| 29 | "6" => "1010000", | ||||||||||||||
| 30 | "7" => "1000100", | ||||||||||||||
| 31 | "8" => "1001000", | ||||||||||||||
| 32 | "9" => "1110100" | ||||||||||||||
| 33 | }; | ||||||||||||||
| 34 | 1 | my $guardBar = "G0G"; | |||||||||||||
| 35 | 1 | my $centerBar = "0G0G0"; | |||||||||||||
| 36 | |||||||||||||||
| 37 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 38 | 0 | my ( $sClass, $sTxt ) = @_; | |||||||||||||
| 39 | 0 | $errStr = ''; | |||||||||||||
| 40 | 0 | my $oThis = {}; | |||||||||||||
| 41 | 0 | bless $oThis, $sClass; | |||||||||||||
| 42 | 0 | ▶ | return if ( $errStr = $oThis->init($sTxt) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | 0 | return $oThis; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 2 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||
| 47 | 2 | my ( $oThis, $sTxt ) = @_; | |||||||||||||
| 48 | 2 | ▶ | return 'Invalid characters' if ( $sTxt =~ /[^0-9]/ ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | |||||||||||||||
| 50 | #Check | ||||||||||||||
| 51 | 2 | my $iLen = length($sTxt); | |||||||||||||
| 52 | 2 | ▶ | if ( $iLen == 11 ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 53 | 1 | $sTxt .= calcUPCACD($sTxt); | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | elsif ( $iLen == 12 ) { | ||||||||||||||
| 56 | ; | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | else { | ||||||||||||||
| 59 | 0 | return 'Invalid Length'; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | 2 | $oThis->{text} = $sTxt; | |||||||||||||
| 62 | 2 | return ''; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 1 | ▶ | sub calcUPCACD { | ||||||||||||
|
Subroutine
calcUPCACD: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 66 | 1 | my ($sTxt) = @_; | |||||||||||||
| 67 | 1 | my ( $i, $iSum, @aWeight ); | |||||||||||||
| 68 | |||||||||||||||
| 69 | 1 | @aWeight = ( 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3 ); | |||||||||||||
| 70 | 1 | $iSum = 0; | |||||||||||||
| 71 | 1 | ▶ | for ( $i = 0 ; $i < 11 ; $i++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 11 | $iSum += substr( $sTxt, $i, 1 ) * $aWeight[$i]; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | 1 | $iSum %= 10; | |||||||||||||
| 75 | 1 | ▶ | $iSum = ( $iSum == 0 ) ? 0 : ( 10 - $iSum ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 1 | return "$iSum"; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 2 | ▶ | sub barcode { | ||||||||||||
|
Subroutine
barcode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 80 | 2 | my ($oThis) = @_; | |||||||||||||
| 81 | 2 | my ( $topDigit, $oddEven, $c, $i ); | |||||||||||||
| 82 | 2 | my ($sRes); | |||||||||||||
| 83 | |||||||||||||||
| 84 | #(1)Init | ||||||||||||||
| 85 | 2 | my $sTxt = $oThis->{text}; | |||||||||||||
| 86 | 2 | $sRes = $guardBar; #GUARD | |||||||||||||
| 87 | |||||||||||||||
| 88 | #(2)Left 6 letters | ||||||||||||||
| 89 | 2 | my $s1st = GD::Barcode::barPtn( substr( $sTxt, 0, 1 ), $leftOddBar ); | |||||||||||||
| 90 | 2 | $s1st =~ tr/1/G/; | |||||||||||||
| 91 | 2 | $sRes .= $s1st; | |||||||||||||
| 92 | 2 | ▶ | for ( $i = 1 ; $i < 6 ; $i++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 10 | $sRes .= GD::Barcode::barPtn( substr( $sTxt, $i, 1 ), $leftOddBar ); | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | #(4)Center | ||||||||||||||
| 97 | 2 | $sRes .= $centerBar; | |||||||||||||
| 98 | |||||||||||||||
| 99 | #(5)Right | ||||||||||||||
| 100 | 2 | ▶ | for ( $i = 6 ; $i < 11 ; $i++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 10 | $sRes .= GD::Barcode::barPtn( substr( $sTxt, $i, 1 ), $rightBar ); | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | 2 | my $sLast = GD::Barcode::barPtn( substr( $sTxt, 11, 1 ), $rightBar ); | |||||||||||||
| 104 | 2 | $sLast =~ tr/1/G/; | |||||||||||||
| 105 | 2 | $sRes .= $sLast; | |||||||||||||
| 106 | |||||||||||||||
| 107 | #(6)GUARD | ||||||||||||||
| 108 | 2 | $sRes .= $guardBar; | |||||||||||||
| 109 | 2 | return $sRes; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 0 | ▶ | sub plot { | ||||||||||||
|
Subroutine
plot: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 113 | 0 | my ( $oThis, %hParam ) = @_; | |||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | my $sTxt = $oThis->{text}; | |||||||||||||
| 116 | 0 | my $sPtn = $oThis->barcode(); | |||||||||||||
| 117 | |||||||||||||||
| 118 | #Create Image | ||||||||||||||
| 119 | 0 | require GD; | |||||||||||||
| 120 | 0 | ▶ | my $iHeight = ( $hParam{Height} ) ? $hParam{Height} : 50; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 0 | my ( $oGd, $cBlack ); | |||||||||||||
| 122 | 0 | ▶ | if ( $hParam{NoText} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 0 | ( $oGd, $cBlack ) = | |||||||||||||
| 124 | GD::Barcode::plot( $sPtn, length($sPtn), $iHeight, 0, 0 ); | ||||||||||||||
| 125 | } | ||||||||||||||
| 126 | else { | ||||||||||||||
| 127 | 0 | my ( $fW, $fH ) = ( GD::Font->Small->width, GD::Font->Small->height ); | |||||||||||||
| 128 | 0 | my $iWidth = length($sPtn) + 2 * ( $fW + 1 ); | |||||||||||||
| 129 | |||||||||||||||
| 130 | #Bar Image | ||||||||||||||
| 131 | 0 | ( $oGd, $cBlack ) = | |||||||||||||
| 132 | GD::Barcode::plot( $sPtn, $iWidth, $iHeight, $fH, $fW + 1 ); | ||||||||||||||
| 133 | |||||||||||||||
| 134 | #String | ||||||||||||||
| 135 | 0 | $oGd->string( | |||||||||||||
| 136 | GD::Font->Small, 0, | ||||||||||||||
| 137 | $iHeight - $fH, | ||||||||||||||
| 138 | substr( $sTxt, 0, 1 ), $cBlack | ||||||||||||||
| 139 | ); | ||||||||||||||
| 140 | 0 | $oGd->string( | |||||||||||||
| 141 | GD::Font->Small, $fW + 14, | ||||||||||||||
| 142 | $iHeight - $fH, | ||||||||||||||
| 143 | substr( $sTxt, 1, 5 ), $cBlack | ||||||||||||||
| 144 | ); | ||||||||||||||
| 145 | 0 | $oGd->string( | |||||||||||||
| 146 | GD::Font->Small, $fW + 53, | ||||||||||||||
| 147 | $iHeight - $fH, | ||||||||||||||
| 148 | substr( $sTxt, 6, 5 ), $cBlack | ||||||||||||||
| 149 | ); | ||||||||||||||
| 150 | 0 | $oGd->string( | |||||||||||||
| 151 | GD::Font->Small, $fW + 98, | ||||||||||||||
| 152 | $iHeight - $fH, | ||||||||||||||
| 153 | substr( $sTxt, 11, 1 ), $cBlack | ||||||||||||||
| 154 | ); | ||||||||||||||
| 155 | } | ||||||||||||||
| 156 | 0 | return $oGd; | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | 1 | 1; | |||||||||||||
| 159 | __END__ | ||||||||||||||