Skip to content

Commit

Permalink
allow adding number of lines to <lf /> like <lf lines="3" />
Browse files Browse the repository at this point in the history
  • Loading branch information
sonntagd committed Feb 12, 2017
1 parent 72ce099 commit 4188707
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<lf />` like `<lf lines="3" />`
* 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.

Expand Down
26 changes: 23 additions & 3 deletions lib/XML/Printer/ESCPOS/Tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,37 @@ 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

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
Expand Down
45 changes: 44 additions & 1 deletion t/01-parse.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ subtest 'Simple parsing' => sub {
<color>
<bold>This is printed with the second color (if supported)</bold>
</color>
<lf lines="3" />
<text> with whitespaces </text>
<tab /><text>go on</text>
<upsideDown>some additional text</upsideDown>
Expand Down Expand Up @@ -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' ],
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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#
<escpos>
<bold>bold<lf lines="0" /> text</bold>
</escpos>
#
);
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#
<escpos>
<bold>bold<lf lines="3.17" /> text</bold>
</escpos>
#
);
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#
<escpos>
<bold>bold<lf lines="asd" /> text</bold>
</escpos>
#
);
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 {
Expand Down

0 comments on commit 4188707

Please sign in to comment.