File Coverage

blib/lib/Barcode/Code93.pm
Criterion Covered Total %
statement 34 35 97.1
branch 3 6 50.0
condition n/a
subroutine 6 6 100.0
pod 1 2 50.0
total 44 49 89.8


line stmt bran cond sub pod time code
1             package Barcode::Code93;
2 2     2   16381 use strict; use warnings;
  2     2   4  
  2         76  
  2         10  
  2         4  
  2         72  
3 2     2   1034 use Moo;
  2         24032  
  2         11  
4              
5             =head1 NAME
6              
7             Barcode::Code93 - Generate data for Code 93 barcodes
8              
9             =cut
10              
11             our $VERSION = '0.04';
12              
13              
14             =head1 SYNOPSIS
15              
16             use Barcode::Code93;
17             my $data = Barcode::Code93->new->barcode('MONKEY');
18             print for map { $_ ? "#" : ' ' } @$data;
19              
20             =head1 DESCRIPTION
21              
22             This class is used to generate data for Code 93 barcodes. It is primarily
23             useful as a data source for barcode modules that do rendering,
24             such as L. You can easily make a version that
25             renders an image, PDF, or anything else.
26              
27             =head1 METHODS
28              
29             =head2 new
30              
31             Instantiate a new Barcode::Code93 object.
32              
33             =head2 barcode ($text)
34              
35             Generate barcode data representing the C<$text> string. This returns
36             an array (or arrayref in scalar context) containing true and false values
37             that represent lines and spaces.
38              
39             =cut
40              
41             sub barcode {
42 1     1 1 740 my ($self, $text) = @_;
43 1         3 my @data = $self->_barcode(uc $text);
44 1 50       6 return wantarray ? @data : \@data;
45             }
46              
47             =head1 AUTHOR
48              
49             Chris DiMartino C<> (L, from which this distribution originates)
50              
51             Mark A. Stratman, C<< >>
52              
53             Jan Bieron L
54              
55             =head1 CONTRIBUTERS
56              
57             The L module, from which this module originates, was based on code provided by Kawai Takanori. Japan.
58              
59             The L module was written by Chris DiMartino, 2004. Thanks to Lobanov Igor, Joel Richard, and Joshua Fortriede for their excellent Bug Reports and patches. All rights reserved.
60              
61             =head1 SOURCE REPOSITORY
62              
63             L
64              
65             =head1 SEE ALSO
66              
67             =over 4
68              
69             =item L
70              
71             =item L
72              
73             =back
74              
75             =head1 LICENSE AND COPYRIGHT
76              
77             Copyright 2011 the AUTHORs and CONTRIBUTERS listed above.
78              
79             This program is free software; you can redistribute it and/or modify it
80             under the terms of either: the GNU General Public License as published
81             by the Free Software Foundation; or the Artistic License.
82              
83             See http://dev.perl.org/licenses/ for more information.
84              
85              
86             =cut
87              
88             #------------------------------------------------------------------------------
89             # barcode (from GD::Barcode::Code93)
90             #------------------------------------------------------------------------------
91             sub _barcode {
92 1     1   2 my ($self, $text) = @_;
93              
94 1         30 my %code93bar = (
95             0 =>'100010100',
96             1 =>'101001000',
97             2 =>'101000100',
98             3 =>'101000010',
99             4 =>'100101000',
100             5 =>'100100100',
101             6 =>'100100010',
102             7 =>'101010000',
103             8 =>'100010010',
104             9 =>'100001010',
105             A =>'110101000',
106             B =>'110100100',
107             C =>'110100010',
108             D =>'110010100',
109             E =>'110010010',
110             F =>'110001010',
111             G =>'101101000',
112             H =>'101100100',
113             I =>'101100010',
114             J =>'100110100',
115             K =>'100011010',
116             L =>'101011000',
117             M =>'101001100',
118             N =>'101000110',
119             O =>'100101100',
120             P =>'100010110',
121             Q =>'110110100',
122             R =>'110110010',
123             S =>'110101100',
124             T =>'110100110',
125             U =>'110010110',
126             V =>'110011010',
127             W =>'101101100',
128             X =>'101100110',
129             Y =>'100110110',
130             Z =>'100111010',
131             ' ' =>'111010010',
132             '$' =>'111001010',
133             '%' =>'110101110',
134             '($)'=>'100100110',
135             '(%)'=>'111011010',
136             '(+)'=>'100110010',
137             '(/)'=>'111010110',
138             '+' =>'101110110',
139             '-' =>'100101110',
140             '.' =>'111010100',
141             '/' =>'101101110',
142             '*' =>'101011110', ##Start/Stop
143             );
144              
145 1         2 my @sum_text = ('*', $self->calculateSums($text), '*');
146              
147 1         3 my @rv = map { split //, $code93bar{$_} } @sum_text;
  10         30  
148 1         5 push @rv, 1;
149 1         24 return @rv;
150             }
151              
152              
153             #-----------------------------------------------------------------------------
154             # calculateSums (from GD::Barcode::Code93)
155             #-----------------------------------------------------------------------------
156             sub calculateSums {
157 1     1 0 2 my ($self, $text) = @_;
158 1 50       4 $text = '' unless defined $text;
159 1         8 my @array = split(//, scalar reverse $text);
160              
161 1         19 my %code93values = (
162             '0' =>'0',
163             '1' =>'1',
164             '2' =>'2',
165             '3' =>'3',
166             '4' =>'4',
167             '5' =>'5',
168             '6' =>'6',
169             '7' =>'7',
170             '8' =>'8',
171             '9' =>'9',
172             'A' =>'10',
173             'B' =>'11',
174             'C' =>'12',
175             'D' =>'13',
176             'E' =>'14',
177             'F' =>'15',
178             'G' =>'16',
179             'H' =>'17',
180             'I' =>'18',
181             'J' =>'19',
182             'K' =>'20',
183             'L' =>'21',
184             'M' =>'22',
185             'N' =>'23',
186             'O' =>'24',
187             'P' =>'25',
188             'Q' =>'26',
189             'R' =>'27',
190             'S' =>'28',
191             'T' =>'29',
192             'U' =>'30',
193             'V' =>'31',
194             'W' =>'32',
195             'X' =>'33',
196             'Y' =>'34',
197             'Z' =>'35',
198             '-' =>'36',
199             '.' =>'37',
200             ' ' =>'38',
201             '$' =>'39',
202             '/' =>'40',
203             '+' =>'41',
204             '%' =>'42',
205             '($)' =>'43',
206             '(%)' =>'44',
207             '(/)' =>'45',
208             '(+)' =>'46',
209             '*' => '',
210             );
211              
212 1         25 my %invCode93Values = reverse %code93values;
213              
214 1         4 foreach my $counter (20, 15) {
215 2         2 my $weighted_sum = 0;
216 2         2 my $x = 1;
217 2         3 foreach my $letter (@array) {
218 13 50       19 if ($x > $counter) { $x = 1 }
  0         0  
219 13         16 $weighted_sum += ($code93values{$letter} * $x);
220 13         13 $x++;
221             }
222              
223 2         8 my $check = $invCode93Values{($weighted_sum % 47)};
224 2         3 unshift @array, $check;
225             }
226              
227 1         12 return reverse @array;
228             }
229              
230             1;