Skip to content

Commit

Permalink
added tags set and unset
Browse files Browse the repository at this point in the history
  • Loading branch information
sonntagd committed Apr 22, 2017
1 parent 7429eef commit d436f62
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 7 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ t/10-text-word-wrap.t
t/11-reset-error-message.t
t/12-tab-positions.t
t/13-repeat.t
t/14-set-unset.t
t/manifest.t
t/pod-coverage.t
t/pod.t
Expand Down
73 changes: 66 additions & 7 deletions lib/XML/Printer/ESCPOS/Tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ sub tag_allowed {
invert
lf
printAreaWidth
set
unset
qr
repeat
rot90
Expand Down Expand Up @@ -94,6 +96,21 @@ sub simple_switch {
return 1;
}

=head2 include_global_options
Helper method to add global options to object options.
=cut

sub include_global_options {
my ($self, $options) = @_;

return {
%{ $self->{global_options} // {} },
%$options,
};
}

=head2 _0
Prints plain text and strips out leading and trailing whitespaces.
Expand Down Expand Up @@ -122,7 +139,7 @@ sub _text {
return $self->{caller}->_set_error_message("wrong text tag usage") if @$params != 3;
return $self->{caller}->_set_error_message("wrong text tag usage") if ref $params->[0] ne 'HASH';
return $self->{caller}->_set_error_message("wrong text tag usage") if $params->[1] != 0;
my $options = $params->[0];
my $options = $self->include_global_options($params->[0]);
if ( exists $options->{wordwrap} ) {
my $columns = delete $options->{wordwrap} || 49;
if ( $columns !~ /^\d+$/ or $columns < 1 ) {
Expand Down Expand Up @@ -235,7 +252,7 @@ sub _qr {
return $self->{caller}->_set_error_message("wrong QR code tag usage") if @$params != 3;
return $self->{caller}->_set_error_message("wrong QR code tag usage") if ref $params->[0] ne 'HASH';
return $self->{caller}->_set_error_message("wrong QR code tag usage") if $params->[1] != 0;
my $options = $params->[0];
my $options = $self->include_global_options($params->[0]);
if (%$options) {
$self->{printer}->qr( $params->[2], $options->{ecc} || 'L', $options->{version} || 5, $options->{moduleSize} || 3 );
}
Expand All @@ -258,7 +275,7 @@ sub _barcode {
return $self->{caller}->_set_error_message("wrong barcode tag usage") if @$params != 3;
return $self->{caller}->_set_error_message("wrong barcode tag usage") if ref $params->[0] ne 'HASH';
return $self->{caller}->_set_error_message("wrong barcode tag usage") if $params->[1] != 0;
my $options = $params->[0];
my $options = $self->include_global_options($params->[0]);
if (%$options) {
$self->{printer}->barcode( barcode => $params->[2], map { $_ => $options->{$_} } sort keys %$options );
}
Expand All @@ -284,7 +301,7 @@ sub _utf8ImagedText {
return $self->{caller}->_set_error_message("wrong utf8ImagedText tag usage") if @$params != 3;
return $self->{caller}->_set_error_message("wrong utf8ImagedText tag usage") if ref $params->[0] ne 'HASH';
return $self->{caller}->_set_error_message("wrong utf8ImagedText tag usage") if $params->[1] != 0;
my $options = $params->[0];
my $options = $self->include_global_options($params->[0]);
if (%$options) {
if ( exists $options->{wordwrap} ) {
my $columns = delete $options->{wordwrap} || 49;
Expand Down Expand Up @@ -379,7 +396,7 @@ sub _image {
my $image = GD::Image->newFromPng($filename) or return $self->{caller}->_set_error_message("Error loading image file $filename");

$self->{printer}->print();
$self->{printer}->image( $image );
$self->{printer}->image( $image );
$self->{printer}->print();

return 1;
Expand All @@ -400,7 +417,7 @@ sub _printAreaWidth {
return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if scalar keys %{ $params->[0] } != 1;
return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if not exists $params->[0]->{width};
$self->{printer}->printAreaWidth( $params->[0]->{width} );
$self->{print_area_width} = $params->[0]->{width};
$self->{global_options}->{printAreaWidth} = $params->[0]->{width};
return 1;
}

Expand All @@ -411,7 +428,49 @@ sub _printAreaWidth {
return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if $params->[1] ne '0';

$self->{printer}->printAreaWidth( $params->[2] );
$self->{print_area_width} = $params->[2];
$self->{global_options}->{printAreaWidth} = $params->[2];
return 1;
}

=head2 _set
Sets global variables: paperWidth, wordwrap, fontFamily, fontSize, fontStyle, lineHeight, printAreaWidth
=cut

sub _set {
my ( $self, $params ) = @_;

return $self->{caller}->_set_error_message("wrong set tag usage") if @$params != 1 ;
return $self->{caller}->_set_error_message("wrong set tag usage") if ref $params->[0] ne 'HASH';
return $self->{caller}->_set_error_message("wrong set tag usage") if not scalar keys %{ $params->[0] };

$self->{global_options} ||= {};
for my $var (qw/paperWidth wordwrap fontFamily fontSize fontStyle lineHeight printAreaWidth/) {
$self->{global_options}->{$var} = $params->[0]->{$var} if $params->[0]->{$var};
}

return 1;
}

=head2 _unset
Resets global variables to standard values: paperWidth, wordwrap, fontFamily, fontSize, fontStyle, lineHeight, printAreaWidth
Syntax: <unset fontStyle="" />
=cut

sub _unset {
my ( $self, $params ) = @_;

return $self->{caller}->_set_error_message("wrong unset tag usage") if @$params != 1 ;
return $self->{caller}->_set_error_message("wrong unset tag usage") if ref $params->[0] ne 'HASH';
return $self->{caller}->_set_error_message("wrong unset tag usage") if not scalar keys %{ $params->[0] };

for my $var (keys %{ $params->[0] }) {
delete $self->{global_options}->{$var};
}

return 1;
}

Expand Down
138 changes: 138 additions & 0 deletions t/14-set-unset.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
use XML::Printer::ESCPOS;
use lib 't/lib';
use Mock::Printer::ESCPOS;

plan tests => 10;

my $mockprinter = Mock::Printer::ESCPOS->new();
my $parser = XML::Printer::ESCPOS->new( printer => $mockprinter );

my $ret = $parser->parse(
q#
<escpos>
<set
paperWidth = "815"
wordwrap = "60"
fontFamily = "OverpassMono"
fontSize = "16"
fontStyle = "Bold"
lineHeight = "30"
/>
<utf8ImagedText>This is bold text, size 16.</utf8ImagedText>
<utf8ImagedText
fontStyle = "Normal"
fontSize = "20"
>This is normal text, size 20.</utf8ImagedText>
<utf8ImagedText>This is bold text, size 16.</utf8ImagedText>
</escpos>
#
);
ok $ret => 'parsing successful';
is $parser->errormessage(), undef, 'errormessage is empty';
is_deeply $mockprinter->{calls}, [
[
utf8ImagedText => 'This is bold text, size 16.',
fontFamily => 'OverpassMono',
fontSize => '16',
fontStyle => 'Bold',
lineHeight => '30',
paperWidth => '815'
],
[
utf8ImagedText => 'This is normal text, size 20.',
fontFamily => 'OverpassMono',
fontSize => '20',
fontStyle => 'Normal',
lineHeight => '30',
paperWidth => '815'
],
[
utf8ImagedText => 'This is bold text, size 16.',
fontFamily => 'OverpassMono',
fontSize => '16',
fontStyle => 'Bold',
lineHeight => '30',
paperWidth => '815'
],
], 'XML translated correctly';

$mockprinter = Mock::Printer::ESCPOS->new();
$parser = XML::Printer::ESCPOS->new( printer => $mockprinter );

$ret = $parser->parse(
q#
<escpos>
<set />
</escpos>
#
);
is $ret, undef, 'parsing stopped';
is $parser->errormessage() => 'wrong set tag usage', 'correct error message';

$mockprinter = Mock::Printer::ESCPOS->new();
$parser = XML::Printer::ESCPOS->new( printer => $mockprinter );

$ret = $parser->parse(
q#
<escpos>
<set>815</set>
</escpos>
#
);
is $ret, undef, 'parsing stopped';
is $parser->errormessage() => 'wrong set tag usage', 'correct error message';

$mockprinter = Mock::Printer::ESCPOS->new();
$parser = XML::Printer::ESCPOS->new( printer => $mockprinter );

$ret = $parser->parse(
q#
<escpos>
<utf8ImagedText>This is standard text.</utf8ImagedText>
<set
fontSize = "16"
fontStyle = "Bold"
/>
<utf8ImagedText>This is bold text, size 16.</utf8ImagedText>
<unset
fontSize = ""
/>
<utf8ImagedText>This is bold text.</utf8ImagedText>
<unset
fontStyle = ""
/>
<utf8ImagedText>This is standard text.</utf8ImagedText>
</escpos>
#
);
ok $ret => 'parsing successful';
is $parser->errormessage(), undef, 'errormessage is empty';
is_deeply $mockprinter->{calls}, [
[
'utf8ImagedText',
'This is standard text.'
],
[
'utf8ImagedText',
'This is bold text, size 16.',
'fontSize',
'16',
'fontStyle',
'Bold'
],
[
'utf8ImagedText',
'This is bold text.',
'fontStyle',
'Bold'
],
[
'utf8ImagedText',
'This is standard text.'
],
], 'XML translated correctly';

0 comments on commit d436f62

Please sign in to comment.