File Coverage

blib/lib/PDF/Builder/Resource/XObject/Form/BarCode/int2of5.pm
Criterion Covered Total %
statement 32 32 100.0
branch 3 4 75.0
condition 1 3 33.3
subroutine 5 5 100.0
pod 1 2 50.0
total 42 46 91.3


line stmt bran cond sub pod time code
1             package PDF::Builder::Resource::XObject::Form::BarCode::int2of5;
2              
3 2     2   1489 use base 'PDF::Builder::Resource::XObject::Form::BarCode';
  2         4  
  2         282  
4              
5 2     2   15 use strict;
  2         4  
  2         77  
6 2     2   10 use warnings;
  2         4  
  2         1061  
7              
8             our $VERSION = '3.028'; # VERSION
9             our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10              
11             =head1 NAME
12              
13             PDF::Builder::Resource::XObject::Form::BarCode::int2of5 - Specific information for interleaved 2-of-5 bar codes
14              
15             Inherits from L<PDF::Builder::Resource::XObject::Form::BarCode>
16              
17             =head1 METHODS
18              
19             =head2 new
20              
21             PDF::Builder::Resource::XObject::Form::BarCode::int2of5->new()
22              
23             =over
24              
25             Create an Interleaved 2 of 5 bar code object. Note that it is invoked from the
26             Builder.pm level method!
27              
28             =back
29              
30             =cut
31              
32             # Interleaved 2 of 5 Barcodes
33              
34             # Pairs of digits are encoded; the first digit is represented by five
35             # bars, and the second digit is represented by five spaces interleaved
36             # with the bars.
37              
38             sub new {
39 1     1 1 5 my ($class, $pdf, %options) = @_;
40             # copy dashed option names to preferred undashed names
41 1 50 33     9 if (defined $options{'-code'} && !defined $options{'code'}) { $options{'code'} = delete($options{'-code'}); }
  1         4  
42              
43 1         10 my $self = $class->SUPER::new($pdf,%options);
44              
45 1         20 my @bars = $self->encode($options{'code'});
46              
47 1         13 $self->drawbar([@bars], $options{'caption'});
48              
49 1         7 return $self;
50             }
51              
52             my @bar25interleaved = qw(11221 21112 12112 22111 11212 21211 12211 11122 21121 12121);
53              
54             sub encode {
55 2     2 0 625 my ($self, $string) = @_;
56              
57             # Remove any character that isn't a digit
58 2         9 $string =~ s/[^0-9]//g;
59              
60             # Prepend a 0 if there are an odd number of digits
61 2 100       10 $string = '0' . $string if length($string) % 2;
62              
63             # Start Code
64 2         5 my @bars = ('aaaa');
65              
66             # Encode pairs of digits
67 2         4 my ($c1, $c2, $s1, $s2, $pair);
68 2         7 while (length($string)) {
69 6         14 ($c1, $c2, $string) = split(//, $string, 3);
70              
71 6         12 $s1 = $bar25interleaved[$c1];
72 6         8 $s2 = $bar25interleaved[$c2];
73 6         8 $pair = '';
74 6         14 foreach my $i (0 .. 4) {
75 30         35 $pair .= substr($s1, $i, 1);
76 30         39 $pair .= substr($s2, $i, 1);
77             }
78 6         13 push @bars, [$pair, ($c1 . $c2)];
79             }
80              
81             # Stop Code
82 2         5 push @bars, 'baaa';
83              
84 2         7 return @bars;
85             }
86              
87             1;