Protocol-Zebra-Label/lib/Protocol/Zebra/Label.pm

132 lines
2.5 KiB
Perl

use strict;
use warnings;
package Protocol::Zebra::Label;
use 5.14.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});
# 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 $code = ""; # EAN13 of Code128C
$code .= "^BEN,,Y,N"; # EAN13
# Code128C code = "^BCN,,Y,N";
# barcode = ">;" + 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
=cut