Skip to content

Commit

Permalink
added hr tag and improved image tag
Browse files Browse the repository at this point in the history
  • Loading branch information
sonntagd committed Apr 24, 2017
1 parent f3f1491 commit 3271a46
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 45 deletions.
4 changes: 3 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -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 <text> and for <utf8ImagedText>.
Expand Down
5 changes: 5 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/XML/Printer/ESCPOS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ template engine of your choice.
=cut

our $VERSION = '0.04';
our $VERSION = '0.05';

=head1 SYNOPSIS
Expand Down
2 changes: 1 addition & 1 deletion lib/XML/Printer/ESCPOS/Debug.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use strict;
use warnings;
use Data::Dumper;

our $VERSION = '0.04';
our $VERSION = '0.05';

our $AUTOLOAD;

Expand Down
69 changes: 56 additions & 13 deletions lib/XML/Printer/ESCPOS/Tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Text::Wrapper;
use GD;


our $VERSION = '0.04';
our $VERSION = '0.05';

=head2 new
Expand Down Expand Up @@ -51,6 +51,7 @@ sub tag_allowed {
upsideDown
utf8ImagedText
justify
hr
/;
}

Expand Down Expand Up @@ -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>image.jpg</image>
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>image.jpg</image>
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;
}

Expand Down Expand Up @@ -480,6 +490,39 @@ sub _unset {
return 1;
}

=head2 _hr
Adds a horizontal line. Use the I<thickness> 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:
Expand Down
71 changes: 42 additions & 29 deletions t/07-images.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,67 @@ 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#
<escpos>
<image filename="header.gif" />
<image filename="# . $filename . q#" />
</escpos>
#,
q#
<escpos>
<image># . $filename . q#</image>
</escpos>
#
);
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#
<escpos>
<image>header.gif</image>
<image size="23">t/images/arrow.png</image>
</escpos>
#
);
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 );

$ret = $parser->parse(
q#
<escpos>
<image size="23">header.gif</image>
<image filename="t/images/arrow.pdf" />
</escpos>
#
);
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';
85 changes: 85 additions & 0 deletions t/16-hr.t
Original file line number Diff line number Diff line change
@@ -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#
<escpos>
<text>text</text>
<hr />
<text>text</text>
</escpos>
#
);
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#
<escpos>
<text>text</text>
<hr thickness="3" />
<text>text</text>
</escpos>
#
);
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#
<escpos>
<text>text</text>
<hr thickness="a" />
<text>text</text>
</escpos>
#
);
is $ret, undef, 'parsing stopped';
is $parser->errormessage() => 'wrong hr tag usage: thickness attribute must be a positive integer', 'correct error message';
Binary file added t/images/arrow.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added t/images/arrow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added t/images/arrow.pdf
Binary file not shown.
Binary file added t/images/arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3271a46

Please sign in to comment.