diff --git a/Changes b/Changes index edce0c9..099a7f8 100644 --- a/Changes +++ b/Changes @@ -1,9 +1,11 @@ Revision history for XML-Printer-ESCPOS -0.05 2017-04-22 +0.05 2017-04-24 - Added repeat tag. - Added tags set and unset for defining global defaults. - Added justify tag. + - Added hr tag. + - Improved image tag to not only allow png, but also jpeg and gif. Added tests. 0.03 2017-02-15 - Added automatic word wrapping for simple and for . diff --git a/MANIFEST b/MANIFEST index 4ddad53..60a8e72 100644 --- a/MANIFEST +++ b/MANIFEST @@ -22,7 +22,12 @@ t/12-tab-positions.t t/13-repeat.t t/14-set-unset.t t/15-justify.t +t/16-hr.t t/manifest.t t/pod-coverage.t t/pod.t t/lib/Mock/Printer/ESCPOS.pm +t/images/arrow.png +t/images/arrow.pdf +t/images/arrow.gif +t/images/arrow.jpg diff --git a/lib/XML/Printer/ESCPOS.pm b/lib/XML/Printer/ESCPOS.pm index dd1975a..2938147 100644 --- a/lib/XML/Printer/ESCPOS.pm +++ b/lib/XML/Printer/ESCPOS.pm @@ -20,7 +20,7 @@ template engine of your choice. =cut -our $VERSION = '0.04'; +our $VERSION = '0.05'; =head1 SYNOPSIS diff --git a/lib/XML/Printer/ESCPOS/Debug.pm b/lib/XML/Printer/ESCPOS/Debug.pm index 4be4b01..55560a7 100644 --- a/lib/XML/Printer/ESCPOS/Debug.pm +++ b/lib/XML/Printer/ESCPOS/Debug.pm @@ -4,7 +4,7 @@ use strict; use warnings; use Data::Dumper; -our $VERSION = '0.04'; +our $VERSION = '0.05'; our $AUTOLOAD; diff --git a/lib/XML/Printer/ESCPOS/Tags.pm b/lib/XML/Printer/ESCPOS/Tags.pm index 5fda3a1..430accc 100644 --- a/lib/XML/Printer/ESCPOS/Tags.pm +++ b/lib/XML/Printer/ESCPOS/Tags.pm @@ -6,7 +6,7 @@ use Text::Wrapper; use GD; -our $VERSION = '0.04'; +our $VERSION = '0.05'; =head2 new @@ -51,6 +51,7 @@ sub tag_allowed { upsideDown utf8ImagedText justify + hr /; } @@ -387,24 +388,33 @@ sub _image { return $self->{caller}->_set_error_message("wrong image tag usage") if scalar keys %{ $params->[0] } != 1; return $self->{caller}->_set_error_message("wrong image tag usage") if not exists $params->[0]->{filename}; $filename = $params->[0]->{filename}; - return 1; } - - # content tag form image.jpg - return $self->{caller}->_set_error_message("wrong image tag usage") if @$params != 3; - return $self->{caller}->_set_error_message("wrong image tag usage") if ref $params->[0] ne 'HASH'; - return $self->{caller}->_set_error_message("wrong image tag usage") if %{ $params->[0] }; - return $self->{caller}->_set_error_message("wrong image tag usage") if $params->[1] ne '0'; - $filename = $params->[2]; + else { + # content tag form image.jpg + return $self->{caller}->_set_error_message("wrong image tag usage") if @$params != 3; + return $self->{caller}->_set_error_message("wrong image tag usage") if ref $params->[0] ne 'HASH'; + return $self->{caller}->_set_error_message("wrong image tag usage") if %{ $params->[0] }; + return $self->{caller}->_set_error_message("wrong image tag usage") if $params->[1] ne '0'; + $filename = $params->[2]; + } return $self->{caller}->_set_error_message("wrong image tag usage: file does not exist") if !-f $filename; - my $image = GD::Image->newFromPng($filename) or return $self->{caller}->_set_error_message("Error loading image file $filename"); + my $image; + if ($filename =~ m/\.png$/) { + $image = GD::Image->newFromPng($filename) or return $self->{caller}->_set_error_message("Error loading image file $filename"); + } + elsif ($filename =~ m/\.gif$/) { + $image = GD::Image->newFromGif($filename) or return $self->{caller}->_set_error_message("Error loading image file $filename"); + } + elsif ($filename =~ m/\.jpe?g$/) { + $image = GD::Image->newFromJpeg($filename) or return $self->{caller}->_set_error_message("Error loading image file $filename"); + } + else { + return $self->{caller}->_set_error_message("wrong image tag usage: file format not supported"); + } - $self->{printer}->print(); $self->{printer}->image( $image ); - $self->{printer}->print(); - return 1; } @@ -480,6 +490,39 @@ sub _unset { return 1; } +=head2 _hr + +Adds a horizontal line. Use the I attribute to set the line's thickness. Defaults to 2. + +=cut + +sub _hr { + my ( $self, $params ) = @_; + return $self->{caller}->_set_error_message("wrong hr tag usage") if @$params != 1; + return $self->{caller}->_set_error_message("wrong hr tag usage") if ref $params->[0] ne 'HASH'; + my $thickness = 2; + if ( %{ $params->[0] } ) { + my @keys = keys %{ $params->[0] }; + return $self->{caller}->_set_error_message("wrong hr tag usage") if !@keys; + $thickness = $params->[0]->{thickness}; + return $self->{caller}->_set_error_message("wrong hr tag usage") if !$thickness; + return $self->{caller}->_set_error_message("wrong hr tag usage: thickness attribute must be a positive integer") + if $thickness !~ /^\d+$/ or $thickness < 1; + } + + my $width = $params->[0]->{width} || $self->{paperWidth} || $self->{print_area_width} || 512; + my $img = GD::Image->new( $width, $thickness ); + my $black = $img->colorAllocate( 0, 0, 0 ); + + for my $line ( 1 .. $thickness ) { + $img->line( 0, $line, $width, $line, $black ); + } + + $self->{printer}->image($img); + + return 1; +} + =head2 _tabpositions Sets horizontal tab positions for tab stops. Syntax for XML is the following: diff --git a/t/07-images.t b/t/07-images.t index 0efdab3..99f2791 100644 --- a/t/07-images.t +++ b/t/07-images.t @@ -7,44 +7,57 @@ use XML::Printer::ESCPOS; use lib 't/lib'; use Mock::Printer::ESCPOS; -plan skip_all => 'image tests must be rewritten, add sample image files to work with'; +plan tests => 22; -my $mockprinter = Mock::Printer::ESCPOS->new(); -my $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); - -my $ret = $parser->parse( - q# +my @filenames = ("t/images/arrow.png", "t/images/arrow.gif", "t/images/arrow.jpg"); +for my $filename (@filenames) { + my @xmls = ( + q# - + + + #, + q# + + # . $filename . q# # -); -ok $ret => 'parsing successful'; -is $parser->errormessage(), undef, 'errormessage is empty'; -my $calls = $mockprinter->{calls}; -ok( ( ref $calls eq 'ARRAY' - or @$calls == 1 - or ref $calls->[0] eq 'ARRAY' - or @{ $calls->[0] } == 2 - or $calls->[0]->[1] eq 'image' - or ref $calls->[0]->[2] eq 'GD::Image' - ), - 'XML translated correctly' -); + ); -$mockprinter = Mock::Printer::ESCPOS->new(); -$parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); + for my $xml (@xmls) { + my $mockprinter = Mock::Printer::ESCPOS->new(); + my $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); -$ret = $parser->parse( + my $ret = $parser->parse($xml); + ok $ret => 'parsing successful'; + is $parser->errormessage(), undef, 'errormessage is empty'; + my $calls = $mockprinter->{calls}; + ok( ( ref $calls eq 'ARRAY' + and @$calls == 1 + and ref $calls->[0] eq 'ARRAY' + and @{$calls->[0]} == 2 + and $calls->[0]->[0] eq 'image' + and ref $calls->[0]->[1] eq 'GD::Image' + and $calls->[0]->[1]->height == 60 + and $calls->[0]->[1]->width == 83 + ), + 'XML translated correctly' + ); + } +} + +my $mockprinter = Mock::Printer::ESCPOS->new(); +my $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); + +my $ret = $parser->parse( q# - header.gif + t/images/arrow.png # ); -ok $ret => 'parsing successful'; -is $parser->errormessage(), undef, 'errormessage is empty'; -is_deeply $mockprinter->{calls}, [ [ image => 'header.gif' ] ], 'XML translated correctly'; +is $ret, undef, 'parsing stopped'; +is $parser->errormessage() => 'wrong image tag usage', 'correct error message'; $mockprinter = Mock::Printer::ESCPOS->new(); $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); @@ -52,9 +65,9 @@ $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); $ret = $parser->parse( q# - header.gif + # ); is $ret, undef, 'parsing stopped'; -is $parser->errormessage() => 'wrong image tag usage', 'correct error message'; +is $parser->errormessage() => 'wrong image tag usage: file format not supported', 'correct error message'; diff --git a/t/16-hr.t b/t/16-hr.t new file mode 100644 index 0000000..c3cf3a5 --- /dev/null +++ b/t/16-hr.t @@ -0,0 +1,85 @@ +#!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 => 12; + +my $mockprinter = Mock::Printer::ESCPOS->new(); +my $parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); + +my $ret = $parser->parse( + q# + + text +
+ text +
+ # +); +ok $ret => 'parsing successful'; +is $parser->errormessage(), undef, 'errormessage is empty'; +my $calls = $mockprinter->{calls}; +ok( ( ref $calls eq 'ARRAY' + and @$calls == 3 + and ref $calls->[0] eq 'ARRAY' + and is_deeply $calls->[0], [ text => 'text' ] + and ref $calls->[1] eq 'ARRAY' + and @{$calls->[1]} == 2 + and $calls->[1]->[0] eq 'image' + and ref $calls->[1]->[1] eq 'GD::Image' + and ref $calls->[2] eq 'ARRAY' + and is_deeply $calls->[2], [ text => 'text' ] + ), + 'XML translated correctly' +); + + +$mockprinter = Mock::Printer::ESCPOS->new(); +$parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); + +$ret = $parser->parse( + q# + + text +
+ text +
+ # +); +ok $ret => 'parsing successful'; +is $parser->errormessage(), undef, 'errormessage is empty'; +$calls = $mockprinter->{calls}; +ok( ( ref $calls eq 'ARRAY' + and @$calls == 3 + and ref $calls->[0] eq 'ARRAY' + and is_deeply $calls->[0], [ text => 'text' ] + and ref $calls->[1] eq 'ARRAY' + and @{$calls->[1]} == 2 + and $calls->[1]->[0] eq 'image' + and ref $calls->[1]->[1] eq 'GD::Image' + and ref $calls->[2] eq 'ARRAY' + and is_deeply $calls->[2], [ text => 'text' ] + ), + 'XML translated correctly' +); + + +$mockprinter = Mock::Printer::ESCPOS->new(); +$parser = XML::Printer::ESCPOS->new( printer => $mockprinter ); + +$ret = $parser->parse( + q# + + text +
+ text +
+ # +); +is $ret, undef, 'parsing stopped'; +is $parser->errormessage() => 'wrong hr tag usage: thickness attribute must be a positive integer', 'correct error message'; \ No newline at end of file diff --git a/t/images/arrow.gif b/t/images/arrow.gif new file mode 100644 index 0000000..62a0ea0 Binary files /dev/null and b/t/images/arrow.gif differ diff --git a/t/images/arrow.jpg b/t/images/arrow.jpg new file mode 100644 index 0000000..b5bcba1 Binary files /dev/null and b/t/images/arrow.jpg differ diff --git a/t/images/arrow.pdf b/t/images/arrow.pdf new file mode 100644 index 0000000..c82d9cd Binary files /dev/null and b/t/images/arrow.pdf differ diff --git a/t/images/arrow.png b/t/images/arrow.png new file mode 100644 index 0000000..cd770ce Binary files /dev/null and b/t/images/arrow.png differ