From 4188707d77f201f0d59e07e5218696579e404571 Mon Sep 17 00:00:00 2001 From: Dominic Sonntag Date: Sun, 12 Feb 2017 23:37:18 +0100 Subject: [PATCH] allow adding number of lines to like --- TODO.md | 1 - lib/XML/Printer/ESCPOS/Tags.pm | 26 +++++++++++++++++--- t/01-parse.t | 45 +++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index b93fa52..3dd4155 100644 --- a/TODO.md +++ b/TODO.md @@ -20,7 +20,6 @@ This document tries to summarize what needs to be done to make `XML::Printer::ES ## Ideas for convenience functions -* Add number of lines to `` like `` * Automatic word wrap for utf8ImagedText and normal text (could be based on [Text::Wrapper](https://metacpan.org/pod/Text::Wrapper), especially for mono-spaced fonts) * Add option to send calls to printer object only after the full document was parsed. This would allow to signal illegal document structure before sending anything to the printer object. diff --git a/lib/XML/Printer/ESCPOS/Tags.pm b/lib/XML/Printer/ESCPOS/Tags.pm index 66ece8d..16766e6 100644 --- a/lib/XML/Printer/ESCPOS/Tags.pm +++ b/lib/XML/Printer/ESCPOS/Tags.pm @@ -268,7 +268,7 @@ sub _utf8ImagedText { =head2 _lf -Moves to the next line. +Moves to the next line. If the lines attribute is given, move that number of lines. =cut @@ -276,9 +276,29 @@ sub _lf { my ( $self, $params ) = @_; return $self->{caller}->_set_error_message("wrong lf tag usage") if @$params != 1; return $self->{caller}->_set_error_message("wrong lf tag usage") if ref $params->[0] ne 'HASH'; - return $self->{caller}->_set_error_message("wrong lf tag usage") if %{ $params->[0] }; - $self->{printer}->lf(); + my $lines = 1; + if (%{ $params->[0] }) { + my @keys = keys %{ $params->[0] }; + return $self->{caller}->_set_error_message("wrong lf tag usage") if @keys != 1; + return $self->{caller}->_set_error_message("wrong lf tag usage") if $keys[0] ne 'lines'; + $lines = $params->[0]->{lines}; + return $self->{caller}->_set_error_message("wrong lf tag usage: lines attribute must be a positive integer") if $lines !~ /^\d+$/ or $lines < 1; + } + $self->{printer}->lf() for 1..$lines; return 1; + +# my ( $self, $params ) = @_; +# 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]; +# if (%$options) { +# $self->{printer}->qr( $params->[2], $options->{ecc} || 'L', $options->{version} || 5, $options->{moduleSize} || 3 ); +# } +# else { +# $self->{printer}->qr( $params->[2] ); +# } +# return 1; } =head2 _tab diff --git a/t/01-parse.t b/t/01-parse.t index 83c1ef0..26183fe 100644 --- a/t/01-parse.t +++ b/t/01-parse.t @@ -42,6 +42,7 @@ subtest 'Simple parsing' => sub { This is printed with the second color (if supported) + with whitespaces go on some additional text @@ -79,6 +80,9 @@ subtest 'Simple parsing' => sub { [ text => 'This is printed with the second color (if supported)' ], [ bold => 0 ], [ color => 0 ], + [ lf => ], + [ lf => ], + [ lf => ], [ text => ' with whitespaces ' ], [ tab => ], [ text => 'go on' ], @@ -276,7 +280,7 @@ subtest 'barcodes' => sub { subtest 'linefeed' => sub { - plan tests => 5; + plan tests => 11; my $mockprinter = Mock::Printer::ESCPOS->new(); my $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); @@ -306,6 +310,45 @@ subtest 'linefeed' => sub { ); is $ret, undef, 'parsing stopped'; is $parser->errormessage() => 'wrong lf tag usage', 'correct error message'; + + $mockprinter = Mock::Printer::ESCPOS->new(); + $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); + + $ret = $parser->parse( + q# + + bold text + + # + ); + is $ret, undef, 'parsing stopped'; + is $parser->errormessage() => 'wrong lf tag usage: lines attribute must be a positive integer', 'correct error message'; + + $mockprinter = Mock::Printer::ESCPOS->new(); + $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); + + $ret = $parser->parse( + q# + + bold text + + # + ); + is $ret, undef, 'parsing stopped'; + is $parser->errormessage() => 'wrong lf tag usage: lines attribute must be a positive integer', 'correct error message'; + + $mockprinter = Mock::Printer::ESCPOS->new(); + $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); + + $ret = $parser->parse( + q# + + bold text + + # + ); + is $ret, undef, 'parsing stopped'; + is $parser->errormessage() => 'wrong lf tag usage: lines attribute must be a positive integer', 'correct error message'; }; subtest 'images' => sub {