package GD::Barcode::Code93;
use strict;
#use GD;
BEGIN { eval{require 'GD.pm';}; };
use GD::Barcode;
require Exporter;
use vars qw($VERSION @ISA $errStr);
@ISA = qw(GD::Barcode Exporter);
$VERSION=1.10;

my $code93bar = {
	'0'	=>'100010100',
	'1'	=>'101001000',
	'2'	=>'101000100',
	'3'	=>'101000010',
	'4'	=>'100101000',
	'5'	=>'100100100',
	'6'	=>'100100010',
	'7'	=>'101010000',
	'8'	=>'100010010',
	'9'	=>'100001010',
	'A'	=>'110101000',
	'B'	=>'110100100',
	'C'	=>'110100010',
	'D'	=>'110010100',
	'E'	=>'110010010',
	'F'	=>'110001010',
	'G'	=>'101101000',
	'H'	=>'101100100',
	'I'	=>'101100010',
	'J'	=>'100110100',
	'K'	=>'100011010',
	'L'	=>'101011000',
	'M'	=>'101001100',
	'N'	=>'101000110',
	'O'	=>'100101100',
	'P'	=>'100010110',
	'Q'	=>'110110100',
	'R'	=>'110110010',
	'S'	=>'110101100',
	'T'	=>'110100110',
	'U'	=>'110010110',
	'V'	=>'110011010',
	'W'	=>'101101100',
	'X'	=>'101100110',
	'Y'	=>'100110110',
	'Z'	=>'100111010',
	' '	=>'111010010',
	'$'	=>'111001010',
	'%'	=>'110101110',
	'($)'	=>'100100110',
	'(%)'	=>'111011010',
	'(+)'	=>'100110010',
	'(/)'	=>'111010110',
	'+'	=>'101110110',
	'-'	=>'100101110',
	'.'	=>'111010100',
	'/'	=>'101101110',
	'*'	=>'101011110',  ##Start/Stop
};

my %code93values	= (
	'0'	=>'0',
	'1'	=>'1',
	'2'	=>'2',
	'3'	=>'3',
	'4'	=>'4',
	'5'	=>'5',
	'6'	=>'6',
	'7'	=>'7',
	'8'	=>'8',
	'9'	=>'9',
	'A'	=>'10',
	'B'	=>'11',
	'C'	=>'12',
	'D'	=>'13',
	'E'	=>'14',
	'F'	=>'15',
	'G'	=>'16',
	'H'	=>'17',
	'I'	=>'18',
	'J'	=>'19',
	'K'	=>'20',
	'L'	=>'21',
	'M'	=>'22',
	'N'	=>'23',
	'O'	=>'24',
	'P'	=>'25',
	'Q'	=>'26',
	'R'	=>'27',
	'S'	=>'28',
	'T'	=>'29',
	'U'	=>'30',
	'V'	=>'31',
	'W'	=>'32',
	'X'	=>'33',
	'Y'	=>'34',
	'Z'	=>'35',
	'-'	=>'36',
	'.'	=>'37',
	' '	=>'38',
	'$'	=>'39',
	'/'	=>'40',
	'+'	=>'41',
	'%'	=>'42',
	'($)'	=>'43',
	'(%)'	=>'44',
	'(/)'	=>'45',
	'(+)'	=>'46',
	'*'		=> '',
);

#------------------------------------------------------------------------------
# new (for GD::Barcode::Code93)
#------------------------------------------------------------------------------
sub new($$) {
  my($sClass, $sTxt) = @_;
  $errStr ='';
  my $oThis = {};
  bless $oThis;
  return undef if($errStr = $oThis->init($sTxt));
  return $oThis;
}
#------------------------------------------------------------------------------
# init (for GD::Barcode::Code93)
#------------------------------------------------------------------------------
sub init($$){
        my($oThis, $sTxt) =@_;
#Check
    return 'Invalid Characters' if($sTxt =~ /[^0-9A-Za-z\-*+\$%\/. ]/);
        $oThis->{text} = uc($sTxt);
        return '';
}
#------------------------------------------------------------------------------
# barcode (for GD::Barcode::Code93)
#------------------------------------------------------------------------------
sub barcode($) {
        my($oThis) = @_;
        my($sTxt);
    	my($sWk, $sRes);

#       $sTxt = '*'. $oThis->{text} . '*';
        $sTxt = $oThis->{text};
	
	my $sText	= calculateSums($sTxt);
	$sTxt	= '*'.$sText.'*';

    $sRes = '';
    foreach $sWk (split(//, $sTxt)) {
      #$sRes .= GD::Barcode::dumpCode( $code93bar->{$sWk} .'0' );
	  $sRes	.= $code93bar->{$sWk};
    }
    return $sRes . "1";
}
#------------------------------------------------------------------------------
# plot (for GD::Barcode::Code93)
#------------------------------------------------------------------------------
sub plot($;%) {
  my($oThis, %hParam) = @_;

#Barcode Pattern
#  my $sTxtWk = '*' . $oThis->{text} . '*';
  my $sTxtWk = $oThis->{text};
  my $sPtn = $oThis->barcode();

#Create Image
  my $iHeight = ($hParam{Height}) ? $hParam{Height} : 50;
  my ($oGd, $cBlack);
  if($hParam{NoText}) {
        ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0);
  }
  else {
        my($fW,$fH) = (GD::Font->Small->width, GD::Font->Small->height);
        my $iWidth = length($sPtn);
        #Bar Image
        ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, 0);

        #String
        $oGd->string(GD::Font->Small, (length($sPtn)-$fW*(length($sTxtWk)))/2, $iHeight - $fH, 
                        $sTxtWk, $cBlack);
  }
  return $oGd;
}
#-----------------------------------------------------------------------------
# calculateSums (for GD::Barcode::Code93)
#-----------------------------------------------------------------------------
sub calculateSums($) {
	my @array   = split(//, scalar reverse shift);

	my %invCode93Values = reverse %code93values;
	my $weighted_sum;

	for (my $counter = 4; $counter >= 3; $counter--) {
		for (my $i = 0, my $x = 1; $i <= $#array; $i++, $x++) {
			my $letter  = $array[$i];

			if ($x > ($counter * 5)) { $x = 1 }
			$weighted_sum   += ($code93values{$letter} * $x);
		}

		my $check   = $invCode93Values{($weighted_sum % 47)};
		unshift (@array,$check);
		$weighted_sum   = ();
	}

	return(reverse @array);
}
1;
__END__


=head1 NAME

GD::Barcode::Code93 - Create Code93 barcode image with GD 

=head1 SYNOPSIS

I<ex. CGI>

  use GD::Barcode::Code93;
  binmode(STDOUT);
  print "Content-Type: image/png\n\n";
  print GD::Barcode::Code93->new('*CODE39IMG*')->plot->png;

I<with Error Check>

  my $oGdBar = GD::Barcode::Code93->new('*123456789;*');
  die $GD::Barcode::Code93::errStr unless($oGdBar);     #Invalid Characters


=head1 DESCRIPTION

GD::Barcode::Code93 is a subclass of GD::Barcode and allows you to
create CODE-39 barcode image with GD.
This module based on "Generate Barcode Ver 1.02 By Shisei Hanai 97/08/22".

=head2 new

I<$oGdBar> = GD::Barcode::Code93->new(I<$sTxt>);

Constructor. 
Creates a GD::Barcode::Code93 object for I<$sTxt>.

=head2 plot()

I<$oGd> = $oGdBar->plot([Height => I<$iHeight>, NoText => I<0 | 1>]);

creates GD object with barcode image for the I<$sTxt> specified at L<new> method.
I<$iHeight> is height of the image. If I<NoText> is 1, the image has no text image of I<$sTxt>.

 ex.
  my $oGdB = GD::Barcode::Code93->new('*12345*');
  my $oGD = $oGdB->plot(NoText=>1, Height => 20);
  # $sGD is a GD image with Height=>20 pixels, with no text.

=head2 barcode()

I<$sPtn> = $oGdBar->barcode();

returns a barcode pattern in string with '1' and '0'. 
'1' means black, '0' means white.

 ex.
  my $oGdB = GD::Barcode::Code93->new('*12345*');
  my $sPtn = $oGdB->barcode();
  # $sPtn = '';

=head2 $errStr

$GD::Barcode::Code93::errStr

has error message.

=head2 $text

$oGdBar->{text}

has barcode text based on I<$sTxt> specified in L<new> method.

=head1 AUTHOR

Kawai Takanori GCD00051@nifty.ne.jp

=head1 COPYRIGHT

The GD::Barcode::Code93 module is based on code provided by Kawai Takanori. Japan.
The GD::Barcode::Code93 module was written by Chris DiMartino, 2004.
All rights reserved.

You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file.

=head1 SEE ALSO

GD::Barcode

=cut

