ESCPOS/Tags.pm untested

filter: Statementstmt 0.0% 0 / 268 Branchbran 0.0% 0 / 176 Conditioncond 0.0% 0 / 88 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 41 Podpod 0.0% 0 / 37 totaltotal 0.0% 0 / 610 SCAR 89.9
CC
89
Cov
0%
_image
182.0
_text
90.0
_printAreaWidth
90.0
CRAP 8010.0
1 package XML::Printer::ESCPOS::Tags;
2  
3 use strict;
4 use warnings;
5 use Text::Wrapper;
6 use GD;
7  
8  
9 our $VERSION = '0.05';
10  
11 =head2 new
12  
13 Constructs a tags object.
14  
15 =cut
16  
17 sub new {
18 my ( $class, %options ) = @_;
19 return if not exists $options{printer} or not ref $options{printer};
20 return if not exists $options{caller} or not ref $options{caller};
21 return bless {%options}, $class;
22 }
23  
24 =head2 tag_allowed
25  
26 Returns true if the given tag is defined.
27  
28 =cut
29  
30 sub tag_allowed {
31 my ( $self, $method ) = @_;
32 return !!grep { $method eq $_ } qw/
33 0
34 barcode
35 bold
36 color
37 doubleStrike
38 image
39 invert
40 lf
41 printAreaWidth
42 set
43 unset
44 qr
45 repeat
46 rot90
47 tab
48 tabpositions
49 text
50 underline
51 upsideDown
52 utf8ImagedText
53 justify
54 hr
55 /;
56 }
57  
58 =head2 parse( $element )
59  
60 Method for recursive parsing of tags.
61  
62 =cut
63  
64 sub parse {
65 my ( $self, $tags ) = @_;
66 my @elements = @$tags;
67 my $hashref = shift @elements;
68 if ( ref $hashref ne 'HASH' or %$hashref ) {
69 return $self->{caller}->_set_error_message('first element should be an empty hashref ({})');
70 }
71  
72 while (@elements) {
73 my $tag = shift @elements;
74 my $data = shift @elements;
75 return $self->{caller}->_set_error_message("tag $tag is not allowed") if not $self->tag_allowed($tag);
76 my $method = '_' . $tag;
77 $self->$method($data) or return;
78 }
79 return 1;
80 }
81  
82 =head2 simple_switch
83  
84 Helper method for simple 0/1 switches.
85  
86 =cut
87  
88 sub simple_switch {
89 my ( $self, $method, $tags ) = @_;
90 $self->{states}->{$method} //= 0;
91 $self->{states}->{$method}++;
92 $self->{printer}->$method(1) if $self->{states}->{$method} == 1;
93  
94 $self->parse($tags) or return;
95  
96 $self->{printer}->$method(0) if $self->{states}->{$method} == 1;
97 $self->{states}->{$method}--;
98 return 1;
99 }
100  
101 =head2 include_global_options
102  
103 Helper method to add global options to object options.
104  
105 =cut
106  
107 sub include_global_options {
108 my ($self, $options) = @_;
109  
110 return {
111 %{ $self->{global_options} // {} },
112 %$options,
113 };
114 }
115  
116 =head2 _0
117  
118 Prints plain text and strips out leading and trailing whitespaces.
119  
120 =cut
121  
122 sub _0 {
123 my ( $self, $text ) = @_;
124 $text =~ s/^\s+//gm;
125 $text =~ s/\s+$//gm;
126 if ($text =~ /\S/) {
127 $self->{printer}->justify( $self->{justify_state} ) if $self->{justify_state};
128 $self->{printer}->text($text);
129 }
130 return 1;
131 }
132  
133 =head2 _text
134  
135 Prints plain text. To activate automatic word wrapping, you can use the attribute I<wordwrap> to set the word
136 wrap width (use the maximum number of characters per line.
137 When wordwrap is active, you can set the I<bodystart> parameter to define the characters that should be printed
138 at the beginning of each line (except the first one). You can use this to set some indentation.
139  
140 =cut
141  
142 sub _text {
143 my ( $self, $params ) = @_;
144 return $self->{caller}->_set_error_message("wrong text tag usage") if @$params != 3;
145 return $self->{caller}->_set_error_message("wrong text tag usage") if ref $params->[0] ne 'HASH';
146 return $self->{caller}->_set_error_message("wrong text tag usage") if $params->[1] != 0;
147 my $options = $self->include_global_options($params->[0]);
148 if ( exists $options->{wordwrap} ) {
149 my $columns = delete $options->{wordwrap} || 49;
150 if ( $columns !~ /^\d+$/ or $columns < 1 ) {
151 return $self->{caller}->_set_error_message("wrong text tag usage: wordwrap attribute must be a positive integer");
152 }
153  
154 my $body_start = exists $options->{bodystart} ? delete $options->{bodystart} : '';
155 my $wrapper = Text::Wrapper->new( columns => $columns, body_start => $body_start );
156 for my $line ( split /\n/ => $wrapper->wrap( $params->[2] ) ) {
157 $self->{printer}->justify( $self->{justify_state} ) if $self->{justify_state};
158 $self->{printer}->text($line);
159 }
160 }
161 else {
162 $self->{printer}->justify( $self->{justify_state} ) if $self->{justify_state};
163 $self->{printer}->text( $params->[2] );
164 }
165 return 1;
166 }
167  
168 =head2 _bold
169  
170 Sets text to be printed bold.
171  
172 =cut
173  
174 sub _bold {
175 my $self = shift;
176 return $self->simple_switch( 'bold', @_ );
177 }
178  
179 =head2 _doubleStrike
180  
181 Sets text to be printed double striked.
182  
183 =cut
184  
185 sub _doubleStrike {
186 my $self = shift;
187 return $self->simple_switch( 'doubleStrike', @_ );
188 }
189  
190 =head2 _invert
191  
192 Sets text to be printed inverted.
193  
194 =cut
195  
196 sub _invert {
197 my $self = shift;
198 return $self->simple_switch( 'invert', @_ );
199 }
200  
201 =head2 _underline
202  
203 Sets text to be printed underlined.
204  
205 =cut
206  
207 sub _underline {
208 my $self = shift;
209 return $self->simple_switch( 'underline', @_ );
210 }
211  
212 =head2 _upsideDown
213  
214 Sets Upside Down Printing.
215  
216 =cut
217  
218 sub _upsideDown {
219 my $self = shift;
220 return $self->simple_switch( 'upsideDown', @_ );
221 }
222  
223 =head2 _color
224  
225 Use this tag to use the second color (if support by your printer).
226  
227 =cut
228  
229 sub _color {
230 my $self = shift;
231 return $self->simple_switch( 'color', @_ );
232 }
233  
234 =head2 _rot90
235  
236 Use this tag to use the second color (if support by your printer).
237  
238 =cut
239  
240 sub _rot90 {
241 my $self = shift;
242 return $self->simple_switch( 'rot90', @_ );
243 }
244  
245 =head2 _qr
246  
247 Prints a QR code. Possible attributes:
248  
249 =head3 ecc
250  
251 =head3 version
252  
253 =head3 moduleSize
254  
255 =cut
256  
257 sub _qr {
258 my ( $self, $params ) = @_;
259 return $self->{caller}->_set_error_message("wrong QR code tag usage") if @$params != 3;
260 return $self->{caller}->_set_error_message("wrong QR code tag usage") if ref $params->[0] ne 'HASH';
261 return $self->{caller}->_set_error_message("wrong QR code tag usage") if $params->[1] != 0;
262 my $options = $self->include_global_options($params->[0]);
263 if (%$options) {
264 $self->{printer}->qr( $params->[2], $options->{ecc} || 'L', $options->{version} || 5, $options->{moduleSize} || 3 );
265 }
266 else {
267 $self->{printer}->qr( $params->[2] );
268 }
269 return 1;
270 }
271  
272 =head2 _barcode
273  
274 Prints a barcode to the printer. See L<Printer::ESCPOS::Manual> for a list of possible options.
275 The barcode content should be set as the tag content like <barcode>content</barcode>. All other
276 options must be set as attributes.
277  
278 =cut
279  
280 sub _barcode {
281 my ( $self, $params ) = @_;
282 return $self->{caller}->_set_error_message("wrong barcode tag usage") if @$params != 3;
283 return $self->{caller}->_set_error_message("wrong barcode tag usage") if ref $params->[0] ne 'HASH';
284 return $self->{caller}->_set_error_message("wrong barcode tag usage") if $params->[1] != 0;
285 my $options = $self->include_global_options($params->[0]);
286 if (%$options) {
287 $self->{printer}->barcode( barcode => $params->[2], map { $_ => $options->{$_} } sort keys %$options );
288 }
289 else {
290 $self->{printer}->barcode( barcode => $params->[2] );
291 }
292 return 1;
293 }
294  
295 =head2 _utf8ImagedText
296  
297 Can print text with special styling. Use the parameters allowed in L<Printer::ESCPOS>'s method utf8ImagedText
298 as attributes for the I<utf8ImagedText> tag. In addition you can use the attribute I<wordwrap> to set the word
299 wrap width. By now, it is only possible to set the maximum number of characters per line. Later we should try
300 to implement a wrapping based in the maximum pixel width of a line.
301 When wordwrap is active, you can set the I<bodystart> parameter to define the characters that should be printed
302 at the beginning of each line (except the first one). You can use this to set some indentation.
303  
304 =cut
305  
306 sub _utf8ImagedText {
307 my ( $self, $params ) = @_;
308 return $self->{caller}->_set_error_message("wrong utf8ImagedText tag usage") if @$params != 3;
309 return $self->{caller}->_set_error_message("wrong utf8ImagedText tag usage") if ref $params->[0] ne 'HASH';
310 return $self->{caller}->_set_error_message("wrong utf8ImagedText tag usage") if $params->[1] != 0;
311 my $options = $self->include_global_options($params->[0]);
312 if (%$options) {
313 if ( exists $options->{wordwrap} ) {
314 my $columns = delete $options->{wordwrap} || 49;
315 if ( $columns !~ /^\d+$/ or $columns < 1 ) {
316 return $self->{caller}
317 ->_set_error_message("wrong utf8ImagedText tag usage: wordwrap attribute must be a positive integer");
318 }
319  
320 my $body_start = exists $options->{bodystart} ? delete $options->{bodystart} : '';
321 my $wrapper = Text::Wrapper->new( columns => $columns, body_start => $body_start );
322 for my $line ( split /\n/ => $wrapper->wrap( $params->[2] ) ) {
323 $self->{printer}->utf8ImagedText( $line, map { $_ => $options->{$_} } sort keys %$options );
324 }
325 }
326 else {
327 $self->{printer}->utf8ImagedText( $params->[2], map { $_ => $options->{$_} } sort keys %$options );
328 }
329 }
330 else {
331 $self->{printer}->utf8ImagedText( $params->[2] );
332 }
333 return 1;
334 }
335  
336 =head2 _lf
337  
338 Moves to the next line. If the lines attribute is given, move that number of lines.
339  
340 =cut
341  
342 sub _lf {
343 my ( $self, $params ) = @_;
344 return $self->{caller}->_set_error_message("wrong lf tag usage") if @$params != 1;
345 return $self->{caller}->_set_error_message("wrong lf tag usage") if ref $params->[0] ne 'HASH';
346 my $lines = 1;
347 if ( %{ $params->[0] } ) {
348 my @keys = keys %{ $params->[0] };
349 return $self->{caller}->_set_error_message("wrong lf tag usage") if @keys != 1;
350 return $self->{caller}->_set_error_message("wrong lf tag usage") if $keys[0] ne 'lines';
351 $lines = $params->[0]->{lines};
352 return $self->{caller}->_set_error_message("wrong lf tag usage: lines attribute must be a positive integer")
353 if $lines !~ /^\d+$/ or $lines < 1;
354 }
355 $self->{printer}->lf() for 1 .. $lines;
356 return 1;
357 }
358  
359 =head2 _tab
360  
361 Moves the cursor to next horizontal tab position.
362  
363 =cut
364  
365 sub _tab {
366 my ( $self, $params ) = @_;
367 return $self->{caller}->_set_error_message("wrong tab tag usage") if @$params != 1;
368 return $self->{caller}->_set_error_message("wrong tab tag usage") if ref $params->[0] ne 'HASH';
369 return $self->{caller}->_set_error_message("wrong tab tag usage") if %{ $params->[0] };
370 $self->{printer}->tab();
371 return 1;
372 }
373  
374 =head2 _image
375  
376 Print image from named file.
377  
378 =cut
379  
380 sub _image {
381 my ( $self, $params ) = @_;
382  
383 my $filename;
384  
385 # single tag form <image filename="image.jpg" />
386 if ( @$params == 1 ) {
387 return $self->{caller}->_set_error_message("wrong image tag usage") if ref $params->[0] ne 'HASH';
388 return $self->{caller}->_set_error_message("wrong image tag usage") if scalar keys %{ $params->[0] } != 1;
389 return $self->{caller}->_set_error_message("wrong image tag usage") if not exists $params->[0]->{filename};
390 $filename = $params->[0]->{filename};
391 }
392 else {
393 # content tag form <image>image.jpg</image>
394 return $self->{caller}->_set_error_message("wrong image tag usage") if @$params != 3;
395 return $self->{caller}->_set_error_message("wrong image tag usage") if ref $params->[0] ne 'HASH';
396 return $self->{caller}->_set_error_message("wrong image tag usage") if %{ $params->[0] };
397 return $self->{caller}->_set_error_message("wrong image tag usage") if $params->[1] ne '0';
398 $filename = $params->[2];
399 }
400  
401 return $self->{caller}->_set_error_message("wrong image tag usage: file does not exist") if !-f $filename;
402  
403 my $image;
404 if ($filename =~ m/\.png$/) {
405 $image = GD::Image->newFromPng($filename) or return $self->{caller}->_set_error_message("Error loading image file $filename");
406 }
407 elsif ($filename =~ m/\.gif$/) {
408 $image = GD::Image->newFromGif($filename) or return $self->{caller}->_set_error_message("Error loading image file $filename");
409 }
410 elsif ($filename =~ m/\.jpe?g$/) {
411 $image = GD::Image->newFromJpeg($filename) or return $self->{caller}->_set_error_message("Error loading image file $filename");
412 }
413 else {
414 return $self->{caller}->_set_error_message("wrong image tag usage: file format not supported");
415 }
416  
417 $self->{printer}->image( $image );
418 return 1;
419 }
420  
421 =head2 _printAreaWidth
422  
423 Sets the print area width.
424  
425 =cut
426  
427 sub _printAreaWidth {
428 my ( $self, $params ) = @_;
429  
430 # single tag form <printAreaWidth width="255" />
431 if ( @$params == 1 ) {
432 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if ref $params->[0] ne 'HASH';
433 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if scalar keys %{ $params->[0] } != 1;
434 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if not exists $params->[0]->{width};
435 $self->{printer}->printAreaWidth( $params->[0]->{width} );
436 $self->{global_options}->{printAreaWidth} = $params->[0]->{width};
437 return 1;
438 }
439  
440 # content tag form <printAreaWidth>255</printAreaWidth>
441 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if @$params != 3;
442 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if ref $params->[0] ne 'HASH';
443 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if %{ $params->[0] };
444 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if $params->[1] ne '0';
445  
446 $self->{printer}->printAreaWidth( $params->[2] );
447 $self->{global_options}->{printAreaWidth} = $params->[2];
448 return 1;
449 }
450  
451 =head2 _set
452  
453 Sets global variables: paperWidth, wordwrap, fontFamily, fontSize, fontStyle, lineHeight, printAreaWidth
454  
455 =cut
456  
457 sub _set {
458 my ( $self, $params ) = @_;
459  
460 return $self->{caller}->_set_error_message("wrong set tag usage") if @$params != 1 ;
461 return $self->{caller}->_set_error_message("wrong set tag usage") if ref $params->[0] ne 'HASH';
462 return $self->{caller}->_set_error_message("wrong set tag usage") if not scalar keys %{ $params->[0] };
463  
464 $self->{global_options} ||= {};
465 for my $var (qw/paperWidth wordwrap fontFamily fontSize fontStyle lineHeight printAreaWidth/) {
466 $self->{global_options}->{$var} = $params->[0]->{$var} if $params->[0]->{$var};
467 }
468
469 return 1;
470 }
471  
472 =head2 _unset
473  
474 Resets global variables to standard values: paperWidth, wordwrap, fontFamily, fontSize, fontStyle, lineHeight, printAreaWidth
475 Syntax: <unset fontStyle="" />
476  
477 =cut
478  
479 sub _unset {
480 my ( $self, $params ) = @_;
481  
482 return $self->{caller}->_set_error_message("wrong unset tag usage") if @$params != 1 ;
483 return $self->{caller}->_set_error_message("wrong unset tag usage") if ref $params->[0] ne 'HASH';
484 return $self->{caller}->_set_error_message("wrong unset tag usage") if not scalar keys %{ $params->[0] };
485  
486 for my $var (keys %{ $params->[0] }) {
487 delete $self->{global_options}->{$var};
488 }
489
490 return 1;
491 }
492  
493 =head2 _hr
494  
495 Adds a horizontal line. Use the I<thickness> attribute to set the line's thickness. Defaults to 2.
496  
497 =cut
498  
499 sub _hr {
500 my ( $self, $params ) = @_;
501 return $self->{caller}->_set_error_message("wrong hr tag usage") if @$params != 1;
502 return $self->{caller}->_set_error_message("wrong hr tag usage") if ref $params->[0] ne 'HASH';
503 my $thickness = 2;
504 if ( %{ $params->[0] } ) {
505 my @keys = keys %{ $params->[0] };
506 return $self->{caller}->_set_error_message("wrong hr tag usage") if !@keys;
507 $thickness = $params->[0]->{thickness};
508 return $self->{caller}->_set_error_message("wrong hr tag usage") if !$thickness;
509 return $self->{caller}->_set_error_message("wrong hr tag usage: thickness attribute must be a positive integer")
510 if $thickness !~ /^\d+$/ or $thickness < 1;
511 }
512  
513 my $width = $params->[0]->{width} || $self->{paperWidth} || $self->{print_area_width} || 512;
514 my $img = GD::Image->new( $width, $thickness );
515 my $black = $img->colorAllocate( 0, 0, 0 );
516  
517 for my $line ( 1 .. $thickness ) {
518 $img->line( 0, $line, $width, $line, $black );
519 }
520  
521 $self->{printer}->image($img);
522  
523 return 1;
524 }
525  
526 =head2 _tabpositions
527  
528 Sets horizontal tab positions for tab stops. Syntax for XML is the following:
529  
530 <tabpositions>
531 <tabposition>5</tabposition>
532 <tabposition>9</tabposition>
533 <tabposition>13</tabposition>
534 </tabpositions>
535  
536 =cut
537  
538 sub _tabpositions {
539 my ( $self, $tags ) = @_;
540  
541 my @elements = @$tags;
542 my $hashref = shift @elements;
543 if ( ref $hashref ne 'HASH' or %$hashref ) {
544 return $self->{caller}->_set_error_message('first element should be an empty hashref ({})');
545 }
546  
547 my @tabpositions = ();
548 while (@elements) {
549 my $tag = shift @elements;
550 my $data = shift @elements;
551 next if $tag eq '0' and $data =~ /^\s*$/;
552  
553 if ( $tag ne 'tabposition' ) {
554 return $self->{caller}
555 ->_set_error_message("wrong tabpositions tag usage: must not contain anything else than tabposition tags");
556 }
557 if ( ref $data ne 'ARRAY'
558 or ref $data->[0] ne 'HASH'
559 or keys %{ $data->[0] }
560 or $data->[1] ne '0'
561 or $data->[2] !~ /^\d+$/
562 or $data->[2] < 1
563 or @$data != 3 )
564 {
565 return $self->{caller}->_set_error_message("wrong tabposition tag usage: value must be a positive integer");
566 }
567 push @tabpositions, $data->[2];
568 }
569  
570 if ( !@tabpositions ) {
571 return $self->{caller}
572 ->_set_error_message("wrong tabpositions tag usage: must contain at least one tabposition tag as child");
573 }
574  
575 $self->{printer}->tabPositions(@tabpositions);
576 return 1;
577 }
578  
579 =head2 _repeat
580  
581 Repeats the content I<n> times. Syntax:
582  
583 <repeat times="3">
584 [tags to repeat]
585 </repeat>
586  
587 =cut
588  
589 sub _repeat {
590 my ( $self, $params ) = @_;
591 return $self->{caller}->_set_error_message("wrong repeat tag usage") if ref $params ne 'ARRAY';
592 return $self->{caller}->_set_error_message("wrong repeat tag usage") if !@$params;
593 return $self->{caller}->_set_error_message("wrong repeat tag usage") if ref $params->[0] ne 'HASH';
594  
595 my $times = 1;
596 my $options = $params->[0];
597 if ( exists $options->{times} ) {
598 $times = $options->{times};
599 return $self->{caller}->_set_error_message("wrong repeat tag usage: only positive integers are allowed") if $times =~ /\D/ or $times < 1;
600 }
601  
602 for ( 1 .. $times ) {
603 $self->parse( [ {}, @$params[ 1.. $#$params ] ] ) or return;
604 }
605  
606 return 1;
607 }
608  
609 =head2 _justify
610  
611 Set justification to left, right or center. Syntax for XML is the following:
612 <justify align="right">text</justify>
613  
614 =cut
615  
616 sub _justify {
617 my ( $self, $params ) = @_;
618  
619 return $self->{caller}->_set_error_message("wrong justify tag usage") if @$params < 3;
620 return $self->{caller}->_set_error_message("wrong justify tag usage") if ref $params->[0] ne 'HASH';
621 return $self->{caller}->_set_error_message("wrong justify tag usage") if !$params->[0]->{align};
622 return $self->{caller}->_set_error_message("wrong justify tag usage") if keys %{ $params->[0] } != 1;
623 return $self->{caller}->_set_error_message("wrong justify tag usage") if not grep { $params->[0]->{align} eq $_ } qw/left center right/;
624  
625 $self->{justify_state} ||= 'left';
626  
627 my $justify_state_before = $self->{justify_state};
628 $self->{justify_state} = $params->[0]->{align};
629  
630 $params->[0] = {};
631 $self->parse($params) or return;
632  
633 $self->{justify_state} = $justify_state_before;
634 return 1;
635 }
636  
637 =head1 NOT YET IMPLEMENTED TAGS
638  
639 =head2 _font
640  
641 Choose font a, b or c.
642  
643 =cut
644  
645 sub _font { }
646  
647 =head2 _fontHeight
648  
649 Set font height.
650  
651 =cut
652  
653 sub _fontHeight { }
654  
655 =head2 _fontWidth
656  
657 Set font width.
658  
659 =cut
660  
661 sub _fontWidth { }
662  
663 =head2 _charSpacing
664  
665 Set character spacing.
666  
667 =cut
668  
669 sub _charSpacing { }
670  
671 =head2 _lineSpacing
672  
673 Set line spacing.
674  
675 =cut
676  
677 sub _lineSpacing { }
678  
679 =head2 _selectDefaultLineSpacing
680  
681 Reverts to default line spacing for the printer.
682  
683 =cut
684  
685 sub _selectDefaultLineSpacing { }
686  
687 =head2 _printPosition
688  
689 Sets the distance from the beginning of the line to the position at which characters are to be printed.
690  
691 =cut
692  
693 sub _printPosition { }
694  
695 =head2 _leftMargin
696  
697 Sets the left margin for printing.
698  
699 =cut
700  
701 sub _leftMargin { }
702  
703 =head2 _printNVImage
704  
705 Prints bit image stored in non-volatile (NV) memory of the printer.
706  
707 =cut
708  
709 sub _printNVImage { }
710  
711 =head2 _printImage
712  
713 Prints bit image stored in volatile memory of the printer.
714  
715 =cut
716  
717 sub _printImage { }
718  
719 1;