Barcode-Code128

 view release on metacpan or  search on metacpan

lib/Barcode/Code128.pm  view on Meta::CPAN

%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

=item gd_image

Usage:

    $object->png($text)
    $object->png($text, $x, $y)
    $object->png($text, { options... })

    $object->gif($text)
    $object->gif($text, $x, $y)
    $object->gif($text, { options... })

    $object->gd_image($text)
    $object->gd_image($text, $x, $y)
    $object->gd_image($text, { options... })

These methods generate an image using the GD module.  The gd_image()
method returns a GD object, which is useful if you want to do
additional processing to it using the GD object methods.  The other
two create actual images.  NOTE: GIF files require an old version of
GD, and so you probably are not able to create them - see below.

The gif() and png() methods are wrappers around gd_image() that create
the GD object and then run the corresponding GD method to create
output that can be displayed or saved to a file.  Note that only one
of these two methods will work, depending on which version of GD you
have - see below.  The return value from gif() or png() is a binary
file, so if you are working on an operating system (e.g. Microsoft
Windows) that makes a distinction between text and binary files be
sure to call binmode(FILEHANDLE) before writing the image to it, or
the file may get corrupted.  Example:

  open(PNG, ">code128.png") or die "Can't write code128.png: $!\n";
  binmode(PNG);
  print PNG $object->png("CODE 128");
  close(PNG);

If you have GD version 1.20 or newer, the PNG file format is the only
allowed option.  Conversely if you have GD version prior to 1.20, then
the GIF format is the only option.  Check the $object->image_format()
method to find out which you have (q.v.).

Note: All of the arguments to this function are optional.  If you have
previously specified C<$text> to the C<barcode()>, C<encode()>, or
C<text()> methods, you do not need to specify it again.  The C<$x> and
C<$y> variables specify the size of the barcode within the image in
pixels.  If size(s) are not specified, they will be set to the minimum
size, which is the length of the barcode plus 40 pixels horizontally,
and 15% of the length of the barcode vertically.  See also the
$object->width() and $object->height() methods for another way of
specifying this.

If instead of specifying $x and $y, you pass a reference to a hash of
name/value pairs, these will be used as the options, overriding
anything set using the $object->option() (or width/height) method
(q.v.).  However, this will not set the options so any future barcodes
using the same object will revert to the option list of the object.
If you want to set the options permanently use the option, width,
and/or height methods instead.

=cut

sub gd_image
{
    my($self, $text, $x, $y) = @_;
    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};

lib/Barcode/Code128.pm  view on Meta::CPAN


    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)

Computes the bar code for the specified text.  The result will be a
string of '#' and space characters representing the dark and light
bands of the bar code.  You can use this if you have an alternate
printing system besides using GD to create the images.

Note: The C<$text> parameter is optional. If you have previously
specified C<$text> to the C<encode()> or C<text()> methods, you do not
need to specify it again.

=cut

sub barcode
{
    my($self, $text) = @_;
    $self->encode($text) if defined $text;
    my @encoded = @{ $self->{encoded} };
    croak "No encoded text found" unless @encoded;
    join '', map { $_ = $ENCODING[$_]; tr/01/ \#/; $_ } @encoded;
}

###---------------------------------------------------------------------------

=back

=head2 Housekeeping Functions

The rest of the methods defined here are only for internal use, or if
you really know what you are doing.  Some of them may be useful to
authors of classes that inherit from this one, or may be overridden by
subclasses.  If you just want to use this module to generate bar
codes, you can stop reading here.

=over 4

=cut

##----------------------------------------------------------------------------

=item encode

Usage:

    $object->encode
    $object->encode($text)
    $object->encode($text, $preferred_code)

Do the encoding.  If C<$text> is supplied, will automatically call the
text() method to set that as the text value first.  If
C<$preferred_code> is supplied, will try that code first.  Otherwise,
the codes will be tried in the following manner:

lib/Barcode/Code128.pm  view on Meta::CPAN


sub code
{
    my($self, $new_code) = @_;
    if (defined $new_code)
    {
        $new_code = uc $new_code;
        croak "Unknown code ``$new_code'' (should be A, B, or C)"
            unless $new_code eq 'A' || $new_code eq 'B' || $new_code eq 'C';
        $self->{code} = $new_code;
    }
    $self->{code};
}

##----------------------------------------------------------------------------
## _encodable($code, $string)
##
## Internal use only.  Returns array of characters from $string that
## can be encoded using the specified $code (A B or C).  Note: not an
## object-oriented method.

sub _encodable
{
    my($code, $string) = @_;
    my @chars;
    while (length $string)
    {
        my $old = $string;
        push @chars, $1 while($code eq 'C' && $string =~ s/^(\d\d)//);
        my $char;
        while(defined($char = substr($string, 0, 1)))
        {
            last if $code ne 'C' && $string =~ /^\d\d\d\d\d\d/;
            last unless exists $CODE{$code}{$char};
            push @chars, $char;
            $string =~ s/^\Q$char\E//;
        }
        last if $old eq $string; # stop if no more changes made to $string
    }
    @chars;
}

=back

=head1 CLASS VARIABLES

None.

=head1 DIAGNOSTICS

=over 4

=item Unrecognized option ($opt) for $class

The specified option is not valid for the module.  C<$class> should be
"Barcode::Code128" but if it has been inherited into another module,
that module will show instead.  C<$opt> is the attempted option.

=item The gd_image() method of Barcode::Code128 requires the GD module

To call the C<gd_image()>, C<png()>, or C<gif()> methods, the GD
module must be present.  This module is used to create the actual
image.  Without it, you can only use the C<barcode()> method.

=item Scale must be a positive integer

The scale factor for the C<gd_image()>, C<png()>, or C<gif()> methods
must be a positive integer.

=item Border ($border) must be a positive integer or zero

The border option cannot be a fractional or negative number.

=item Invalid font $font

The specified font is not valid.  Note that this is tested using
GD->can(), and so any subroutine in GD.pm will pass this test - but
only the fonts will actually work.  See the GD module documentation
for more.

=item Image width $x is too small for bar code

You have specified an image width that does not allow enough space for
the bar code to be displayed.  The minimum allowable is the size of
the bar code itself plus 40 pixels.  If in doubt, just omit the width
value when calling C<gd_image()>, C<png()>, or C<gif()> and it will
use the minimum.

=item Image height $y is too small for bar code

You have specified an image height that does not allow enough space
for the bar code to be displayed.  The minimum allowable is 15% of the
width of the bar code.  If in doubt, just omit the height value when
calling C<gd_image()>, C<png()>, or C<gif()> and it will use the
minimum.

=item Unable to create $x x $y image

An error occurred when initializing a GD::Image object for the
specified size.  Perhaps C<$x> and C<$y> are too large for memory?

=item The gif() method of Barcode::Code128 requires the GD module

=item The gif() method of Barcode::Code128 requires version less than 1.20 of GD

=item The png() method of Barcode::Code128 requires the GD module

=item The png() method of Barcode::Code128 requires at least version 1.20 of GD

These errors indicate that the GD module, or the correct version of
the GD module for this method, was not present.  You need to install
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



( run in 2.178 seconds using v1.01-cache-2.11-cpan-df04353d9ac )