Skip to content

Commit

Permalink
added tag justify
Browse files Browse the repository at this point in the history
  • Loading branch information
sonntagd committed Apr 22, 2017
1 parent 04904a3 commit f3f1491
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Revision history for XML-Printer-ESCPOS
0.05 2017-04-22
- Added repeat tag.
- Added tags set and unset for defining global defaults.
- Added justify tag.

0.03 2017-02-15
- Added automatic word wrapping for simple <text> and for <utf8ImagedText>.
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ t/11-reset-error-message.t
t/12-tab-positions.t
t/13-repeat.t
t/14-set-unset.t
t/15-justify.t
t/manifest.t
t/pod-coverage.t
t/pod.t
Expand Down
44 changes: 35 additions & 9 deletions lib/XML/Printer/ESCPOS/Tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ sub tag_allowed {
underline
upsideDown
utf8ImagedText
justify
/;
}

Expand Down Expand Up @@ -121,7 +122,10 @@ sub _0 {
my ( $self, $text ) = @_;
$text =~ s/^\s+//gm;
$text =~ s/\s+$//gm;
$self->{printer}->text($text) if $text =~ /\S/;
if ($text =~ /\S/) {
$self->{printer}->justify( $self->{justify_state} ) if $self->{justify_state};
$self->{printer}->text($text);
}
return 1;
}

Expand Down Expand Up @@ -149,10 +153,12 @@ sub _text {
my $body_start = exists $options->{bodystart} ? delete $options->{bodystart} : '';
my $wrapper = Text::Wrapper->new( columns => $columns, body_start => $body_start );
for my $line ( split /\n/ => $wrapper->wrap( $params->[2] ) ) {
$self->{printer}->justify( $self->{justify_state} ) if $self->{justify_state};
$self->{printer}->text($line);
}
}
else {
$self->{printer}->justify( $self->{justify_state} ) if $self->{justify_state};
$self->{printer}->text( $params->[2] );
}
return 1;
Expand Down Expand Up @@ -474,8 +480,6 @@ sub _unset {
return 1;
}

=head1 NOT YET IMPLEMENTED TAGS
=head2 _tabpositions
Sets horizontal tab positions for tab stops. Syntax for XML is the following:
Expand Down Expand Up @@ -559,21 +563,43 @@ sub _repeat {
return 1;
}

=head2 _font
=head2 _justify
Choose font a, b or c.
Set justification to left, right or center. Syntax for XML is the following:
<justify align="right">text</justify>
=cut

sub _font { }
sub _justify {
my ( $self, $params ) = @_;

=head2 _justify
return $self->{caller}->_set_error_message("wrong justify tag usage") if @$params < 3;
return $self->{caller}->_set_error_message("wrong justify tag usage") if ref $params->[0] ne 'HASH';
return $self->{caller}->_set_error_message("wrong justify tag usage") if !$params->[0]->{align};
return $self->{caller}->_set_error_message("wrong justify tag usage") if keys %{ $params->[0] } != 1;
return $self->{caller}->_set_error_message("wrong justify tag usage") if not grep { $params->[0]->{align} eq $_ } qw/left center right/;

$self->{justify_state} ||= 'left';

my $justify_state_before = $self->{justify_state};
$self->{justify_state} = $params->[0]->{align};

Set justification to left, right or center.
$params->[0] = {};
$self->parse($params) or return;

$self->{justify_state} = $justify_state_before;
return 1;
}

=head1 NOT YET IMPLEMENTED TAGS
=head2 _font
Choose font a, b or c.
=cut

sub _justify { }
sub _font { }

=head2 _fontHeight
Expand Down
85 changes: 85 additions & 0 deletions t/15-justify.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 => 10;

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

my $ret = $parser->parse(
q#
<escpos>
<justify align="right"
>text <bold>bold</bold>
</justify>
</escpos>
#
);
ok $ret => 'parsing successful';
is $parser->errormessage(), undef, 'errormessage is empty';
is_deeply $mockprinter->{calls}, [
[ justify => 'right' ],
[ text => 'text' ],
[ bold => 1 ],
[ justify => 'right' ],
[ text => 'bold' ],
[ bold => 0 ],
], 'XML translated correctly';

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

$ret = $parser->parse(
q#
<escpos>
<justify align="right">First line is right aligned.<lf />
<justify align="center">Second line is centered.</justify><lf />
Third line is right aligned.
</justify>
</escpos>
#
);
ok $ret => 'parsing successful';
is $parser->errormessage(), undef, 'errormessage is empty';
is_deeply $mockprinter->{calls}, [
[ justify => 'right' ],
[ text => 'First line is right aligned.' ],
[ lf => ],
[ justify => 'center' ],
[ text => 'Second line is centered.' ],
[ lf => ],
[ justify => 'right' ],
[ text => 'Third line is right aligned.' ],
], 'XML translated correctly';

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

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

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

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

0 comments on commit f3f1491

Please sign in to comment.