Barcode-Code128
view release on metacpan or search on metacpan
lib/Barcode/Code128.pm view on Meta::CPAN
To use the the GD module, you will need to install it along with this
module. You can obtain it from the CPAN (Comprehensive Perl Archive
Network) repository of your choice under the directory
C<authors/id/LDS>. Visit http://www.cpan.org/ for more information
about CPAN. The GD home page is:
http://stein.cshl.org/WWW/software/GD/GD.html
=head1 METHODS
=over 4
=cut
package Barcode::Code128;
use strict;
use vars qw($GD_VERSION $VERSION %CODE_CHARS %CODE @ENCODING @EXPORT_OK
%EXPORT_TAGS %FUNC_CHARS @ISA %OPTIONS);
use constant CodeA => chr(0xf4);
use constant CodeB => chr(0xf5);
use constant CodeC => chr(0xf6);
use constant FNC1 => chr(0xf7);
use constant FNC2 => chr(0xf8);
use constant FNC3 => chr(0xf9);
use constant FNC4 => chr(0xfa);
use constant Shift => chr(0xfb);
use constant StartA => chr(0xfc);
use constant StartB => chr(0xfd);
use constant StartC => chr(0xfe);
use constant Stop => chr(0xff);
use Carp;
use Exporter;
# Try to load GD. If it succeeds, set $GD_VERSION accordingly.
BEGIN {
$GD_VERSION = undef;
eval "use GD 2.18";
$GD_VERSION = $GD::VERSION
unless $@;
}
%OPTIONS =
(
width => undef,
height => undef,
border => 2,
scale => 2,
font => 'large',
show_text => 1,
font_margin => 2,
top_margin => 0,
bottom_margin => 0,
left_margin => 0,
right_margin => 0,
padding => 20,
font_align => 'left',
transparent_text => 1,
);
@EXPORT_OK = qw(CodeA CodeB CodeC FNC1 FNC2 FNC3 FNC4 Shift StartA
StartB StartC Stop);
%EXPORT_TAGS = (all => \@EXPORT_OK);
@ISA = qw(Exporter);
# Version information
$VERSION = '2.21';
@ENCODING = qw(11011001100 11001101100 11001100110 10010011000
10010001100 10001001100 10011001000 10011000100
10001100100 11001001000 11001000100 11000100100
10110011100 10011011100 10011001110 10111001100
10011101100 10011100110 11001110010 11001011100
11001001110 11011100100 11001110100 11101101110
11101001100 11100101100 11100100110 11101100100
11100110100 11100110010 11011011000 11011000110
11000110110 10100011000 10001011000 10001000110
10110001000 10001101000 10001100010 11010001000
11000101000 11000100010 10110111000 10110001110
10001101110 10111011000 10111000110 10001110110
11101110110 11010001110 11000101110 11011101000
11011100010 11011101110 11101011000 11101000110
11100010110 11101101000 11101100010 11100011010
11101111010 11001000010 11110001010 10100110000
10100001100 10010110000 10010000110 10000101100
10000100110 10110010000 10110000100 10011010000
10011000010 10000110100 10000110010 11000010010
11001010000 11110111010 11000010100 10001111010
10100111100 10010111100 10010011110 10111100100
10011110100 10011110010 11110100100 11110010100
11110010010 11011011110 11011110110 11110110110
10101111000 10100011110 10001011110 10111101000
10111100010 11110101000 11110100010 10111011110
10111101110 11101011110 11110101110 11010000100
11010010000 11010011100 1100011101011);
%CODE_CHARS = ( A => [ (map { chr($_) } 040..0137, 000..037),
FNC3, FNC2, Shift, CodeC, CodeB, FNC4, FNC1,
StartA, StartB, StartC, Stop ],
B => [ (map { chr($_) } 040..0177),
FNC3, FNC2, Shift, CodeC, FNC4, CodeA, FNC1,
StartA, StartB, StartC, Stop ],
C => [ ("00".."99"),
CodeB, CodeA, FNC1, StartA, StartB, StartC, Stop ]);
# Provide string equivalents to the constants
%FUNC_CHARS = ('CodeA' => CodeA,
'CodeB' => CodeB,
'CodeC' => CodeC,
'FNC1' => FNC1,
'FNC2' => FNC2,
'FNC3' => FNC3,
'FNC4' => FNC4,
'Shift' => Shift,
'StartA' => StartA,
'StartB' => StartB,
'StartC' => StartC,
'Stop' => Stop );
# Convert the above into a 2-dimensional hash
%CODE = ( A => { map { $CODE_CHARS{A}[$_] => $_ } 0..106 },
B => { map { $CODE_CHARS{B}[$_] => $_ } 0..106 },
C => { map { $CODE_CHARS{C}[$_] => $_ } 0..106 } );
##----------------------------------------------------------------------------
=item new
Usage:
$object = new Barcode::Code128
Creates a new barcode object.
=cut
sub new
{
my $type = shift;
my $self = bless { @_ }, $type;
$self->{encoded} ||= [];
$self->{text} ||= '';
$self;
}
=item option
Sets or retreives various options. If called with only one parameter,
retrieves the value for that parameter. If called with more than one
parameter, treats the parameters as name/value pairs and sets those
option values accordingly. If called with no parameters, returns a
hash consisting of the values of all the options (hash ref in scalar
context). When an option has not been set, its default value is
returned.
You can also set or retrieve any of these options by using it as a
method name. For example, to set the value of the padding option, you
can use either of these:
$barcode->padding(10);
$barcode->option("padding", 10);
The valid options, and the default value and meaning of each, are:
width undef Width of the image (*)
height undef Height of the image (*)
border 2 Size of the black border around the barcode
scale 2 How many pixels for the smallest barcode stripe
font "large" Font (**) for the text at the bottom
show_text 1 True/False: display the text at the bottom?
font_margin 2 Pixels above, below, and to left of the text
font_align "left" Align the text ("left", "right", or "center")
transparent_text 1/0(***) True/False: use transparent background for text?
top_margin 0 No. of pixels above the barcode
bottom_margin 0 No. of pixels below the barcode (& text)
left_margin 0 No. of pixels to the left of the barcode
right_margin 0 No. of pixels to the right of the barcode
padding 20 Size of whitespace before & after barcode
* Width and height are the default values for the $x and $y arguments
to the png, gif, or gd_image method (q.v.)
** Font may be one of the following: "giant", "large", "medium",
"small", or "tiny". Or, it may be any valid GD font name, such as
"gdMediumFont".
*** The "transparent_text" option is "1" (true) by default for GIF
output, but "0" (false) for PNG. This is because PNG transparency is
not supported well by many viewing software The background color is
grey (#CCCCCC) when not transparent.
=cut
sub AUTOLOAD
{
my($self, @args) = @_;
use vars qw($AUTOLOAD);
(my $opt = lc $AUTOLOAD) =~ s/^.*:://;
return if $opt eq 'destroy';
$self->option($opt, @args);
}
sub option
{
my $self = shift;
my $class = ref $self; # do this so others can inherit from us
my $defaults;
{ no strict 'refs'; $defaults = \%{$class.'::OPTIONS'}; }
if (!@_) {
my %all;
while (my($opt, $def_value) = each %$defaults) {
if (exists $self->{OPTIONS}{$opt}) {
$all{$opt} = $self->{OPTIONS}{$opt};
}
else {
$all{$opt} = $def_value;
}
}
wantarray ? %all : \%all;
}
elsif (@_ == 1) { # return requested value
my $opt = shift;
croak "Unrecognized option ($opt) for $class"
unless exists $defaults->{$opt};
if (exists $self->{OPTIONS}{$opt}) {
return $self->{OPTIONS}{$opt};
}
else {
return $defaults->{$opt};
}
}
else {
my $count = 0;
while(my($opt, $value) = splice(@_, 0, 2)) {
croak "Unrecognized option ($opt) for $class"
unless exists $defaults->{$opt};
$self->{OPTIONS}{$opt} = $value;
$count++;
}
return $count;
}
}
##----------------------------------------------------------------------------
=item gif
=item png
lib/Barcode/Code128.pm view on Meta::CPAN
my %opts;
if (ref($x) && !defined($y)) {
%opts = ($self->option, %$x);
$x = $opts{width};
$y = $opts{height};
}
else {
%opts = $self->option;
$opts{width} = $x if $x;
$opts{height} = $y if $y;
}
croak "The gd_image() method of Barcode::Code128 requires the GD module"
unless $GD_VERSION;
my $scale = $opts{scale};
croak "Scale ($scale) must be a positive integer"
unless $scale > 0 && int($scale) == $scale;
my $border = $opts{border};
croak "Border ($border) must be a positive integer or zero"
unless $border >= 0 && int($border) == $border;
$border *= $scale;
$x ||= $opts{width};
$y ||= $opts{height};
my($font, $font_margin, $font_height, $font_width) = (undef, 0, 0, 0);
if ($opts{show_text}) {
$font = $opts{font};
my %fontTable = (giant => 'gdGiantFont',
large => 'gdLargeFont',
medium => 'gdMediumBoldFont',
small => 'gdSmallFont',
tiny => 'gdTinyFont');
$font = $fontTable{$font} if exists $fontTable{$font};
croak "Invalid font $font" unless GD->can($font);
$font = eval "GD->$font"; die $@ if $@;
$font_margin = $opts{font_margin};
$font_height = $font->height + $font_margin * 2;
$font_width = $font->width;
}
my($lm, $rm, $tm, $bm) = map { $opts{$_."_margin"} }
qw(left right top bottom);
my @barcode = split //, $self->barcode($text);
my $n = scalar(@barcode); # width of string
my $min_x = ($n + $opts{padding}) * $scale + 2 * $border;
my $min_y = $n * $scale * 0.15 + 2 * $border; # 15% of width in pixels
$x ||= $min_x;
$y ||= $min_y;
croak "Image width $x is too small for bar code" if $x < $min_x;
croak "Image height $y is too small for bar code" if $y < $min_y;
my $image = new GD::Image($x + $lm + $rm, $y + $tm + $bm + $font_height)
or croak "Unable to create $x x $y image";
my $grey = $image->colorAllocate(0xCC, 0xCC, 0xCC);
my $white = $image->colorAllocate(0xFF, 0xFF, 0xFF);
my $black = $image->colorAllocate(0x00, 0x00, 0x00);
my $red = $image->colorAllocate(0xFF, 0x00, 0x00);
$image->transparent($grey)
if $opts{transparent_text};
if ($border) {
$image->rectangle($lm, $tm, $lm+$x-1, $tm+$y-1, $black);
$image->rectangle($lm+$border, $tm+$border,
$lm+$x-$border-1, $tm+$y-$border-1, $black);
$image->fill($lm+1, $tm+1, $black);
}
else {
$image->rectangle($lm, $tm, $lm+$x-1, $tm+$y-1, $white);
}
$image->fill($lm+$border+1, $tm+$border+1, $white);
for (my $i = 0; $i < $n; ++$i)
{
next unless $barcode[$i] eq '#';
my $pos = $x/2 - $n * ($scale/2) + $i * $scale;
$image->rectangle($lm+$pos, $tm+$border,
$lm+$pos+$scale-1, $tm+$y-$border-1, $black);
$image->fill($lm+$pos+1, $tm+$border+1, $black)
if $scale > 2;
}
if (defined $font) {
my ($font_x,$font_y);
if ($opts{font_align} eq "center") {
$font_x = int(($x+$lm+$rm-($font_width*length $self->{text}))/2);
} elsif ($opts{font_align} eq "right") {
$font_x = $x +$lm-($font_width * length $self->{text});
} else { # Assume left
$font_x = $lm+$font_margin;
}
$font_y = $tm+$y+$font_margin;
$image->string($font, $font_x, $font_y, $self->{text}, $black)
}
return $image;
}
sub gif
{
my($self, $text, $x, $y, $scale) = @_;
croak "The gif() method of Barcode::Code128 requires the GD module"
unless $GD_VERSION;
my $image = $self->gd_image($text, $x, $y, $scale);
return $image->gif();
}
sub png
{
my($self, $text, $x, $y, $scale) = @_;
croak "The png() method of Barcode::Code128 requires the GD module"
unless $GD_VERSION;
my $image = $self->gd_image($text, $x, $y, $scale);
return $image->png();
}
##----------------------------------------------------------------------------
=item barcode
Usage:
$object->barcode($text)
lib/Barcode/Code128.pm view on Meta::CPAN
GD version 1.20 or greater to create PNG files, or a version of GD
less than 1.20 to create GIF files.
=item No encoded text found
This message from C<barcode()> typically means that there was no text
message supplied either during the current method call or in a
previous method call on the same object. This error occurs when you
are trying to create a barcode by calling one of C<gd_image()>,
C<png()>, C<gif()>, or C<barcode()> without having specified the text
to be encoded.
=item No text defined
This message from C<encode()> typically means that there was no text
message supplied either during the current method call or in a
previous method call on the same object.
=item Invalid preferred code ``$preferred_code''
This error means C<encode()> was called with the C<$preferred_code>
optional parameter but it was not one of ``A'', ``B'', or ``C''.
=item Sanity Check Overflow
This is a serious error in C<encode()> that indicates a serious
problem attempting to encode the requested message. This means that
an infinite loop was generated. If you get this error please contact
the author.
=item Unable to find encoding for ``$text''
Part or all of the message could not be encoded. This may mean that
the message contained characters not encodable in the CODE 128
character set, such as a character with an ASCII value higher than 127
(except the special control characters defined in this module).
=item Unable to switch from ``$old_code'' to ``$new_code''
This is a serious error in C<start()> that indicates a serious problem
occurred when switching between the codes (A, B, or C) of CODE 128.
If you get this error please contact the author.
=item Unable to start with ``$new_code''
This is a serious error in C<start()> that indicates a serious problem
occurred when starting encoding in one of the codes (A, B, or C) of
CODE 128. If you get this error please contact the author.
=item Unknown code ``$new_code'' (should be A, B, or C)
This is a serious error in C<code()> that indicates an invalid
argument was supplied. Only the codes (A, B, or C) of CODE 128 may be
supplied here. If you get this error please contact the author.
=back
=head1 BUGS
At least some Web browsers do not seem to handle PNG files with
transparent backgrounds correctly. As a result, the default for PNG
is to generate barcodes without transparent backgrounds - the
background is grey instead.
=head1 AUTHOR
William R. Ward, wrw@bayview.com
=head1 SEE ALSO
perl(1), GD
=cut
1;
( run in 1.958 second using v1.01-cache-2.11-cpan-140bd7fdf52 )