You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
2.9 KiB

package Protocol::Zebra::Label;
use strict;
use warnings;
use 5.14.2;
8 years ago
our $VERSION='0.0.2';
use Moose;
use Text::Wrap;
has 'data' => (
traits => [qw/String/],
is => 'rw',
isa => 'Str',
default => "",
handles => {
append_data => 'append',
},
);
has 'line_height' => (
is => 'rw',
isa => 'Int',
default => 2,
);
has 'height' => (
traits => ['Number'],
is => 'rw',
isa => 'Int',
default => 32,
handles => {
add_height => 'add',
},
);
sub header {
my $self = shift;
return "^XA~TA000~JSN~LT0^MNW^MTD^PON^PMN^LH0,0^JMA^PR2,2~SD25~JO^JUS^LRY^CI0^XZ^XA^MMT^PW254^LL0203^LS0";
}
sub reset {
my $self = shift;
$self->data("");
$self->height(32);
$self->line_height(2);
}
sub format {
my $self = shift;
my $vars = shift;
# split lines
my $name = $vars->{name};
$Text::Wrap::columns = 27;
my @line = split/\n/,wrap("", "", $name);
$self->append_data($self->header);
# add_line
$self->add_line($line[0]) if @line > 0;
$self->add_line($line[1]) if @line > 1;
# add_price
$self->add_line($vars->{price}, 32);
# add_barcode
$self->add_barcode($vars->{barcode}, $vars->{barcode_type} // 'EAN13');
# add_count
$self->add_count($vars->{count});
my $str = $self->data;
$self->reset;
return $str;
}
sub add_line {
my $self = shift;
my $line = shift;
my $font_height = shift;
$font_height //= 20;
if (length $line > 27) {
die "Long line";
}
my $font_width = $font_height;
if ($font_height == 32) {
$self->add_height(16);
}
$self->append_data("^FT16," . $self->height . "^A0N," . $font_height . "," . $font_width . "^FH\\^FD" . $line . "^FS");
$self->add_height($font_height + $self->line_height);
return;
}
sub add_barcode {
my $self = shift;
my $barcode = shift;
my $type = shift;
my $code = ""; # EAN13 of Code128C
my $ean = $type eq 'EAN13';
if ($type eq 'EAN13') {
$code .= "^BEN,,Y,N"; # EAN13
}
elsif ($type eq 'Code128C') { # Code128C
$code = "^BCN,,Y,N";
$barcode = ">;" . $barcode;
}
elsif ($type eq 'UPC-A') { # UPC-A
$code = "^BUN,,Y,N,Y";
}
else {
die "Unknown type $type for barcode";
}
$self->append_data("^BY2,2,56^FT32,158" . $code . "\r\n^FD" . $barcode . "^FS");
return;
}
# RawPrinterHelper.SendStringToPrinter(printer_name, header+"^XA^MMT^PW254^LL0203^LS0"+text+"^BY2,2,56^FT32,158" + code + "\r\n^FD" + barcode + "^FS^PQ" + count + ",0,1,Y^XZ\r\n");
sub add_count {
my $self = shift;
my $count = shift;
$self->append_data("^PQ" . $count . ",0,1,Y^XZ\r\n");
return;
}
no Moose;
1;
__END__
=head1 NAME
Protocol::Zebra::Label - Labels or barcode printer
8 years ago
=head1 AUTHOR
Peter Stuifzand <peter@stuifzand.eu>
=head1 LICENSE
8 years ago
All rights reserved.
=cut