ESCPOS/Tags.pm untested

filter: Statementstmt 0.0% 0 / 157 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 354 CC 52 SCAR 36.2
CC
52
Cov
0%
_image
90.0
_printAreaWidth
90.0
_utf8ImagedText
56.0
CRAP 37.2
1 package XML::Printer::ESCPOS::Tags;
2  
3 use strict;
4 use warnings;
5 use Text::Wrapper;
6  
7 our $VERSION = '0.04';
8  
9 =head2 new
10  
11 Constructs a tags object.
12  
13 =cut
14  
15 sub new {
16 my ( $class, %options ) = @_;
17 return if not exists $options{printer} or not ref $options{printer};
18 return if not exists $options{caller} or not ref $options{caller};
19 return bless {%options}, $class;
20 }
21  
22 =head2 tag_allowed
23  
24 Returns true if the given tag is defined.
25  
26 =cut
27  
28 sub tag_allowed {
29 my ( $self, $method ) = @_;
30 return !!grep { $method eq $_ } qw/
31 0
32 barcode
33 bold
34 color
35 doubleStrike
36 image
37 invert
38 lf
39 printAreaWidth
40 qr
41 rot90
42 tab
43 text
44 underline
45 upsideDown
46 utf8ImagedText
47 /;
48 }
49  
50 =head2 parse( $element )
51  
52 Method for recursive parsing of tags.
53  
54 =cut
55  
56 sub parse {
57 my ( $self, $tags ) = @_;
58 my @elements = @$tags;
59 my $hashref = shift @elements;
60 if ( ref $hashref ne 'HASH' or %$hashref ) {
61 return $self->{caller}->_set_error_message('first element should be an empty hashref ({})');
62 }
63  
64 while (@elements) {
65 my $tag = shift @elements;
66 my $data = shift @elements;
67 return $self->{caller}->_set_error_message("tag $tag is not allowed") if not $self->tag_allowed($tag);
68 my $method = '_' . $tag;
69 $self->$method($data) or return;
70 }
71 return 1;
72 }
73  
74 =head2 simple_switch
75  
76 Helper method for simple 0/1 switches.
77  
78 =cut
79  
80 sub simple_switch {
81 my ( $self, $method, $tags ) = @_;
82 $self->{states}->{$method} //= 0;
83 $self->{states}->{$method}++;
84 $self->{printer}->$method(1) if $self->{states}->{$method} == 1;
85  
86 $self->parse($tags) or return;
87  
88 $self->{printer}->$method(0) if $self->{states}->{$method} == 1;
89 $self->{states}->{$method}--;
90 return 1;
91 }
92  
93 =head2 _0
94  
95 Prints plain text and strips out leading and trailing whitespaces.
96  
97 =cut
98  
99 sub _0 {
100 my ( $self, $text ) = @_;
101 $text =~ s/^\s+//gm;
102 $text =~ s/\s+$//gm;
103 $self->{printer}->text($text) if $text =~ /\S/;
104 return 1;
105 }
106  
107 =head2 _text
108  
109 Prints plain text. To activate automatic word wrapping, you can use the attribute I<wordwrap> to set the word
110 wrap width (use the maximum number of characters per line.
111 When wordwrap is active, you can set the I<bodystart> parameter to define the characters that should be printed
112 at the beginning of each line (except the first one). You can use this to set some indentation.
113  
114 =cut
115  
116 sub _text {
117 my ( $self, $params ) = @_;
118 return $self->{caller}->_set_error_message("wrong text tag usage") if @$params != 3;
119 return $self->{caller}->_set_error_message("wrong text tag usage") if ref $params->[0] ne 'HASH';
120 return $self->{caller}->_set_error_message("wrong text tag usage") if $params->[1] != 0;
121 my $options = $params->[0];
122 if ( exists $options->{wordwrap} ) {
123 my $columns = delete $options->{wordwrap} || 49;
124 my $body_start = exists $options->{bodystart} ? delete $options->{bodystart} : '';
125 my $wrapper = Text::Wrapper->new( columns => $columns, body_start => $body_start );
126 for my $line ( split /\n/ => $wrapper->wrap( $params->[2] ) ) {
127 $self->{printer}->text($line);
128 }
129 }
130 else {
131 $self->{printer}->text( $params->[2] );
132 }
133 return 1;
134 }
135  
136 =head2 _bold
137  
138 Sets text to be printed bold.
139  
140 =cut
141  
142 sub _bold {
143 my $self = shift;
144 return $self->simple_switch( 'bold', @_ );
145 }
146  
147 =head2 _doubleStrike
148  
149 Sets text to be printed double striked.
150  
151 =cut
152  
153 sub _doubleStrike {
154 my $self = shift;
155 return $self->simple_switch( 'doubleStrike', @_ );
156 }
157  
158 =head2 _invert
159  
160 Sets text to be printed inverted.
161  
162 =cut
163  
164 sub _invert {
165 my $self = shift;
166 return $self->simple_switch( 'invert', @_ );
167 }
168  
169 =head2 _underline
170  
171 Sets text to be printed underlined.
172  
173 =cut
174  
175 sub _underline {
176 my $self = shift;
177 return $self->simple_switch( 'underline', @_ );
178 }
179  
180 =head2 _upsideDown
181  
182 Sets Upside Down Printing.
183  
184 =cut
185  
186 sub _upsideDown {
187 my $self = shift;
188 return $self->simple_switch( 'upsideDown', @_ );
189 }
190  
191 =head2 _color
192  
193 Use this tag to use the second color (if support by your printer).
194  
195 =cut
196  
197 sub _color {
198 my $self = shift;
199 return $self->simple_switch( 'color', @_ );
200 }
201  
202 =head2 _rot90
203  
204 Use this tag to use the second color (if support by your printer).
205  
206 =cut
207  
208 sub _rot90 {
209 my $self = shift;
210 return $self->simple_switch( 'rot90', @_ );
211 }
212  
213 =head2 _qr
214  
215 Prints a QR code. Possible attributes:
216  
217 =head3 ecc
218  
219 =head3 version
220  
221 =head3 moduleSize
222  
223 =cut
224  
225 sub _qr {
226 my ( $self, $params ) = @_;
227 return $self->{caller}->_set_error_message("wrong QR code tag usage") if @$params != 3;
228 return $self->{caller}->_set_error_message("wrong QR code tag usage") if ref $params->[0] ne 'HASH';
229 return $self->{caller}->_set_error_message("wrong QR code tag usage") if $params->[1] != 0;
230 my $options = $params->[0];
231 if (%$options) {
232 $self->{printer}->qr( $params->[2], $options->{ecc} || 'L', $options->{version} || 5, $options->{moduleSize} || 3 );
233 }
234 else {
235 $self->{printer}->qr( $params->[2] );
236 }
237 return 1;
238 }
239  
240 =head2 _barcode
241  
242 Prints a barcode to the printer. See L<Printer::ESCPOS::Manual> for a list of possible options.
243 The barcode content should be set as the tag content like <barcode>content</barcode>. All other
244 options must be set as attributes.
245  
246 =cut
247  
248 sub _barcode {
249 my ( $self, $params ) = @_;
250 return $self->{caller}->_set_error_message("wrong barcode tag usage") if @$params != 3;
251 return $self->{caller}->_set_error_message("wrong barcode tag usage") if ref $params->[0] ne 'HASH';
252 return $self->{caller}->_set_error_message("wrong barcode tag usage") if $params->[1] != 0;
253 my $options = $params->[0];
254 if (%$options) {
255 $self->{printer}->barcode( barcode => $params->[2], map { $_ => $options->{$_} } sort keys %$options );
256 }
257 else {
258 $self->{printer}->barcode( barcode => $params->[2] );
259 }
260 return 1;
261 }
262  
263 =head2 _utf8ImagedText
264  
265 Can print text with special styling. Use the parameters allowed in L<Printer::ESCPOS>'s method utf8ImagedText
266 as attributes for the I<utf8ImagedText> tag. In addition you can use the attribute I<wordwrap> to set the word
267 wrap width. By now, it is only possible to set the maximum number of characters per line. Later we should try
268 to implement a wrapping based in the maximum pixel width of a line.
269 When wordwrap is active, you can set the I<bodystart> parameter to define the characters that should be printed
270 at the beginning of each line (except the first one). You can use this to set some indentation.
271  
272 =cut
273  
274 sub _utf8ImagedText {
275 my ( $self, $params ) = @_;
276 return $self->{caller}->_set_error_message("wrong utf8ImagedText tag usage") if @$params != 3;
277 return $self->{caller}->_set_error_message("wrong utf8ImagedText tag usage") if ref $params->[0] ne 'HASH';
278 return $self->{caller}->_set_error_message("wrong utf8ImagedText tag usage") if $params->[1] != 0;
279 my $options = $params->[0];
280 if (%$options) {
281 if ( exists $options->{wordwrap} ) {
282 my $columns = delete $options->{wordwrap} || 49;
283 my $body_start = exists $options->{bodystart} ? delete $options->{bodystart} : '';
284 my $wrapper = Text::Wrapper->new( columns => $columns, body_start => $body_start );
285 for my $line ( split /\n/ => $wrapper->wrap( $params->[2] ) ) {
286 $self->{printer}->utf8ImagedText( $line, map { $_ => $options->{$_} } sort keys %$options );
287 }
288 }
289 else {
290 $self->{printer}->utf8ImagedText( $params->[2], map { $_ => $options->{$_} } sort keys %$options );
291 }
292 }
293 else {
294 $self->{printer}->utf8ImagedText( $params->[2] );
295 }
296 return 1;
297 }
298  
299 =head2 _lf
300  
301 Moves to the next line. If the lines attribute is given, move that number of lines.
302  
303 =cut
304  
305 sub _lf {
306 my ( $self, $params ) = @_;
307 return $self->{caller}->_set_error_message("wrong lf tag usage") if @$params != 1;
308 return $self->{caller}->_set_error_message("wrong lf tag usage") if ref $params->[0] ne 'HASH';
309 my $lines = 1;
310 if ( %{ $params->[0] } ) {
311 my @keys = keys %{ $params->[0] };
312 return $self->{caller}->_set_error_message("wrong lf tag usage") if @keys != 1;
313 return $self->{caller}->_set_error_message("wrong lf tag usage") if $keys[0] ne 'lines';
314 $lines = $params->[0]->{lines};
315 return $self->{caller}->_set_error_message("wrong lf tag usage: lines attribute must be a positive integer")
316 if $lines !~ /^\d+$/ or $lines < 1;
317 }
318 $self->{printer}->lf() for 1 .. $lines;
319 return 1;
320 }
321  
322 =head2 _tab
323  
324 Moves the cursor to next horizontal tab position.
325  
326 =cut
327  
328 sub _tab {
329 my ( $self, $params ) = @_;
330 return $self->{caller}->_set_error_message("wrong tab tag usage") if @$params != 1;
331 return $self->{caller}->_set_error_message("wrong tab tag usage") if ref $params->[0] ne 'HASH';
332 return $self->{caller}->_set_error_message("wrong tab tag usage") if %{ $params->[0] };
333 $self->{printer}->tab();
334 return 1;
335 }
336  
337 =head2 _image
338  
339 Print image from named file.
340  
341 =cut
342  
343 sub _image {
344 my ( $self, $params ) = @_;
345  
346 # single tag form <image filename="image.jpg" />
347 if ( @$params == 1 ) {
348 return $self->{caller}->_set_error_message("wrong image tag usage") if ref $params->[0] ne 'HASH';
349 return $self->{caller}->_set_error_message("wrong image tag usage") if scalar keys %{ $params->[0] } != 1;
350 return $self->{caller}->_set_error_message("wrong image tag usage") if not exists $params->[0]->{filename};
351 $self->{printer}->image( $params->[0]->{filename} );
352 return 1;
353 }
354  
355 # content tag form <image>image.jpg</image>
356 return $self->{caller}->_set_error_message("wrong image tag usage") if @$params != 3;
357 return $self->{caller}->_set_error_message("wrong image tag usage") if ref $params->[0] ne 'HASH';
358 return $self->{caller}->_set_error_message("wrong image tag usage") if %{ $params->[0] };
359 return $self->{caller}->_set_error_message("wrong image tag usage") if $params->[1] ne '0';
360  
361 $self->{printer}->image( $params->[2] );
362 return 1;
363 }
364  
365 =head2 _printAreaWidth
366  
367 Sets the print area width.
368  
369 =cut
370  
371 sub _printAreaWidth {
372 my ( $self, $params ) = @_;
373  
374 # single tag form <printAreaWidth width="255" />
375 if ( @$params == 1 ) {
376 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if ref $params->[0] ne 'HASH';
377 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if scalar keys %{ $params->[0] } != 1;
378 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if not exists $params->[0]->{width};
379 $self->{printer}->printAreaWidth( $params->[0]->{width} );
380 return 1;
381 }
382  
383 # content tag form <printAreaWidth>255</printAreaWidth>
384 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if @$params != 3;
385 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if ref $params->[0] ne 'HASH';
386 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if %{ $params->[0] };
387 return $self->{caller}->_set_error_message("wrong printAreaWidth tag usage") if $params->[1] ne '0';
388  
389 $self->{printer}->printAreaWidth( $params->[2] );
390 return 1;
391 }
392  
393 =head2 _tabPositions
394  
395 Sets horizontal tab positions for tab stops.
396  
397 =cut
398  
399 sub _tabPositions { }
400  
401 =head2 _font
402  
403 Choose font a, b or c.
404  
405 =cut
406  
407 sub _font { }
408  
409 =head2 _justify
410  
411 Set justification to left, right or center.
412  
413 =cut
414  
415 sub _justify { }
416  
417 =head2 _fontHeight
418  
419 Set font height.
420  
421 =cut
422  
423 sub _fontHeight { }
424  
425 =head2 _fontWidth
426  
427 Set font width.
428  
429 =cut
430  
431 sub _fontWidth { }
432  
433 =head2 _charSpacing
434  
435 Set character spacing.
436  
437 =cut
438  
439 sub _charSpacing { }
440  
441 =head2 _lineSpacing
442  
443 Set line spacing.
444  
445 =cut
446  
447 sub _lineSpacing { }
448  
449 =head2 _selectDefaultLineSpacing
450  
451 Reverts to default line spacing for the printer.
452  
453 =cut
454  
455 sub _selectDefaultLineSpacing { }
456  
457 =head2 _printPosition
458  
459 Sets the distance from the beginning of the line to the position at which characters are to be printed.
460  
461 =cut
462  
463 sub _printPosition { }
464  
465 =head2 _leftMargin
466  
467 Sets the left margin for printing.
468  
469 =cut
470  
471 sub _leftMargin { }
472  
473 =head2 _printNVImage
474  
475 Prints bit image stored in non-volatile (NV) memory of the printer.
476  
477 =cut
478  
479 sub _printNVImage { }
480  
481 =head2 _printImage
482  
483 Prints bit image stored in volatile memory of the printer.
484  
485 =cut
486  
487 sub _printImage { }
488  
489 1;