| 1 | package GD::Barcode::Code39; | ||
| 2 | use strict; | ||
| 3 | #use GD; | ||
| 4 | BEGIN { eval{require 'GD.pm';}; }; | ||
| 5 | use GD::Barcode; | ||
| 6 | require Exporter; | ||
| 7 | use vars qw($VERSION @ISA $errStr); | ||
| 8 | @ISA = qw(GD::Barcode Exporter); | ||
| 9 | $VERSION=1.10; | ||
| 10 | my $code39Bar = { | ||
| 11 | '0' => '000110100', | ||
| 12 | '1' => '100100001', | ||
| 13 | '2' => '001100001', | ||
| 14 | '3' => '101100000', | ||
| 15 | '4' => '000110001', | ||
| 16 | '5' => '100110000', | ||
| 17 | '6' => '001110000', | ||
| 18 | '7' => '000100101', | ||
| 19 | '8' => '100100100', | ||
| 20 | '9' => '001100100', | ||
| 21 | 'A' => '100001001', | ||
| 22 | 'B' => '001001001', | ||
| 23 | 'C' => '101001000', | ||
| 24 | 'D' => '000011001', | ||
| 25 | 'E' => '100011000', | ||
| 26 | 'F' => '001011000', | ||
| 27 | 'G' => '000001101', | ||
| 28 | 'H' => '100001100', | ||
| 29 | 'I' => '001001100', | ||
| 30 | 'J' => '000011100', | ||
| 31 | 'K' => '100000011', | ||
| 32 | 'L' => '001000011', | ||
| 33 | 'M' => '101000010', | ||
| 34 | 'N' => '000010011', | ||
| 35 | 'O' => '100010010', | ||
| 36 | 'P' => '001010010', | ||
| 37 | 'Q' => '000000111', | ||
| 38 | 'R' => '100000110', | ||
| 39 | 'S' => '001000110', | ||
| 40 | 'T' => '000010110', | ||
| 41 | 'U' => '110000001', | ||
| 42 | 'V' => '011000001', | ||
| 43 | 'W' => '111000000', | ||
| 44 | 'X' => '010010001', | ||
| 45 | 'Y' => '110010000', | ||
| 46 | 'Z' => '011010000', | ||
| 47 | '-' => '010000101', | ||
| 48 | '*' => '010010100', | ||
| 49 | '+' => '010001010', | ||
| 50 | '$' => '010101000', | ||
| 51 | '%' => '000101010', | ||
| 52 | '/' => '010100010', | ||
| 53 | '.' => '110000100', | ||
| 54 | ' ' => '011000100' | ||
| 55 | }; | ||
| 56 | #------------------------------------------------------------------------------ | ||
| 57 | # new (for GD::Barcode::Code39) | ||
| 58 | #------------------------------------------------------------------------------ | ||
| 59 | sub new($$) { | ||
| 60 | my($sClass, $sTxt) = @_; | ||
| 61 | $errStr =''; | ||
| 62 | my $oThis = {}; | ||
| 63 | bless $oThis; | ||
| 64 | return undef if($errStr = $oThis->init($sTxt)); | ||
| 65 | return $oThis; | ||
| 66 | } | ||
| 67 | #------------------------------------------------------------------------------ | ||
| 68 | # init (for GD::Barcode::Code39) | ||
| 69 | #------------------------------------------------------------------------------ | ||
| 70 | sub init($$){ | ||
| 71 | my($oThis, $sTxt) =@_; | ||
| 72 | #Check | ||
| 73 | return 'Invalid Characters' if($sTxt =~ /[^0-9A-Z\-*+\$%\/. ]/); | ||
| 74 | $oThis->{text} = $sTxt; | ||
| 75 | return ''; | ||
| 76 | } | ||
| 77 | #------------------------------------------------------------------------------ | ||
| 78 | # barcode (for GD::Barcode::Code39) | ||
| 79 | #------------------------------------------------------------------------------ | ||
| 80 | sub barcode($) { | ||
| 81 | my($oThis) = @_; | ||
| 82 | my($sTxt); | ||
| 83 | my($sWk, $sRes); | ||
| 84 | |||
| 85 | # $sTxt = '*'. $oThis->{text} . '*'; | ||
| 86 | $sTxt = $oThis->{text}; | ||
| 87 | $sRes = ''; | ||
| 88 | foreach $sWk (split(//, $sTxt)) { | ||
| 89 | $sRes .= GD::Barcode::dumpCode( $code39Bar->{$sWk} .'0' ); | ||
| 90 | } | ||
| 91 | return $sRes; | ||
| 92 | } | ||
| 93 | #------------------------------------------------------------------------------ | ||
| 94 | # plot (for GD::Barcode::Code39) | ||
| 95 | #------------------------------------------------------------------------------ | ||
| 96 | sub plot($;%) { | ||
| 97 | my($oThis, %hParam) = @_; | ||
| 98 | |||
| 99 | #Barcode Pattern | ||
| 100 | # my $sTxtWk = '*' . $oThis->{text} . '*'; | ||
| 101 | my $sTxtWk = $oThis->{text}; | ||
| 102 | my $sPtn = $oThis->barcode(); | ||
| 103 | |||
| 104 | #Create Image | ||
| 105 | my $iHeight = ($hParam{Height})? $hParam{Height} : 50; | ||
| 106 | my ($oGd, $cBlack); | ||
| 107 | if($hParam{NoText}) { | ||
| 108 | ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0); | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | my($fW,$fH) = (GD::Font->Small->width, GD::Font->Small->height); | ||
| 112 | my $iWidth = length($sPtn); | ||
| 113 | #Bar Image | ||
| 114 | ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, 0); | ||
| 115 | |||
| 116 | #String | ||
| 117 | $oGd->string(GD::Font->Small, (length($sPtn)-$fW*(length($sTxtWk)))/2, $iHeight - $fH, | ||
| 118 | $sTxtWk, $cBlack); | ||
| 119 | } | ||
| 120 | return $oGd; | ||
| 121 | } | ||
| 122 | 1; | ||
| 123 | __END__ |