blib/lib/Imager/Barcode128.pm

filter: Statementstmt 97.1% 171 / 176 Branchbran 71.4% 30 / 42 Conditioncond 62.5% 15 / 24 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 97.0% 33 / 34 Podpod 20.0% 1 / 5 totaltotal 85.7% 252 / 294 SCAR 34.7
CC
31
Cov
89%
encode
13.4
_encodable
9.1
__ANON__
5.4
CRAP 32.2
↑ Summary
1 package Imager::Barcode128;
2 1 $Imager::Barcode128::VERSION = '0.0102';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use constant CodeA => chr(0xf4);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant CodeB => chr(0xf5);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant CodeC => chr(0xf6);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant FNC1 => chr(0xf7);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant FNC2 => chr(0xf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant FNC3 => chr(0xf9);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant FNC4 => chr(0xfa);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant Shift => chr(0xfb);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant StartA => chr(0xfc);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant StartB => chr(0xfd);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant StartC => chr(0xfe);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant Stop => chr(0xff);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 our @EXPORT_OK = qw(FNC1 FNC2 FNC3 FNC4 Shift);
24 1 our %EXPORT_TAGS = (all => \@EXPORT_OK);
25  
26 1 our @ENCODING = qw(11011001100 11001101100 11001100110 10010011000 10010001100 10001001100 10011001000 10011000100 10001100100 11001001000 11001000100 11000100100 10110011100 10011011100 10011001110 10111001100 10011101100 10011100110 11001110010 11001011100 11001001110 11011100100 11001110100 11101101110 11101001100 11100101100 11100100110 11101100100 11100110100 11100110010 11011011000 11011000110 11000110110 10100011000 10001011000 10001000110 10110001000 10001101000 10001100010 11010001000 11000101000 11000100010 10110111000 10110001110 10001101110 10111011000 10111000110 10001110110 11101110110 11010001110 11000101110 11011101000 11011100010 11011101110 11101011000 11101000110 11100010110 11101101000 11101100010 11100011010 11101111010 11001000010 11110001010 10100110000 10100001100 10010110000 10010000110 10000101100 10000100110 10110010000 10110000100 10011010000 10011000010 10000110100 10000110010 11000010010 11001010000 11110111010 11000010100 10001111010 10100111100 10010111100 10010011110 10111100100 10011110100 10011110010 11110100100 11110010100 11110010010 11011011110 11011110110 11110110110 10101111000 10100011110 10001011110 10111101000 10111100010 11110101000 11110100010 10111011110 10111101110 11101011110 11110101110 11010000100 11010010000 11010011100 1100011101011);
27  
28 our %CODE_CHARS = (
29 96 A => [ (map { chr($_) } 040..0137, 000..037), FNC3, FNC2, Shift, CodeC, CodeB, FNC4, FNC1, StartA, StartB, StartC, Stop ],
30 1 B => [ (map { chr($_) } 040..0177), FNC3, FNC2, Shift, CodeC, FNC4, CodeA, FNC1, StartA, StartB, StartC, Stop ],
31 C => [ ("00".."99"), CodeB, CodeA, FNC1, StartA, StartB, StartC, Stop ]
32 );
33  
34 # Provide string equivalents to the constants
35 1 our %FUNC_CHARS = ('CodeA' => CodeA,
36 'CodeB' => CodeB,
37 'CodeC' => CodeC,
38 'FNC1' => FNC1,
39 'FNC2' => FNC2,
40 'FNC3' => FNC3,
41 'FNC4' => FNC4,
42 'Shift' => Shift,
43 'StartA' => StartA,
44 'StartB' => StartB,
45 'StartC' => StartC,
46 'Stop' => Stop );
47  
48 # Convert the above into a 2-dimensional hash
49 107 our %CODE = ( A => { map { $CODE_CHARS{A}[$_] => $_ } 0..106 },
50 107 B => { map { $CODE_CHARS{B}[$_] => $_ } 0..106 },
51 1 C => { map { $CODE_CHARS{C}[$_] => $_ } 0..106 } );
52  
53  
54 =head1 NAME
55  
56 Imager::Barcode128 - Create GS1-128 compliant bar codes using Imager
57  
58 =head1 VERSION
59  
60 version 0.0102
61  
62 =head1 SYNOPSIS
63  
64 use Imager::Barcode128;
65  
66 my $barcode = Imager::Barcode128->new( text => 'My cool barcode' );
67 $barcode->draw;
68 $barcode->image->write(file => 'barcode.png');
69  
70 =head1 DESCRIPTION
71  
72 If you want to generate GS1-128 compliant bar codes using L<Imager> then look no further!
73  
74 =head1 EXPORTS
75  
76 By default this module exports nothing. However, there are a number of constants that represent special characters used in the CODE 128 symbology that you may wish to include. For example if you are using the EAN-128 or UCC-128 code, the string to encode begins with the FNC1 character. To encode the EAN-128 string "00 0 0012345 555555555 8", you would do the following:
77  
78 my $barcode = Imager::Barcode128->new(text => FNC1.'00000123455555555558');
79  
80 To have this module export one or more of these characters, specify them on the use statement or use the special token ':all' instead to include all of them. Examples:
81  
82 use Imager::Barcode128 qw(FNC1 Shift);
83 use Imager::Barcode128 qw(:all);
84  
85 Here is the complete list of the exportable characters. They are assigned to high-order ASCII characters purely arbitrarily for the purposes of this module; the values used do not reflect any part of the GS1-128 standard.
86  
87 FNC1 0xf7
88 FNC2 0xf8
89 FNC3 0xf9
90 FNC4 0xfa
91 Shift 0xfb
92  
93 =head1 METHODS
94  
95 =head2 new(text => 'Product #45')
96  
97 Constructor.
98  
99 =over
100  
101 =item image
102  
103 The L<Imager> object to draw the bar code on to. Required.
104  
105 =item text
106  
107 The text to be encoded into the bar code. Required.
108  
109 =item x
110  
111 The x coordinate of the top left corner to start drawing the bar code. Defaults to 0.
112  
113 =item y
114  
115 The y coordinate of the top left corner to start drawing the bar code. Defaults to 0.
116  
117 =back
118  
119 =cut
120  
121 =head2 x()
122  
123 Get or set the x coordinate of the top left corner of where to start drawing the bar code.
124  
125 =cut
126  
127 has x => (
128 is => 'rw',
129 1 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
130 1 );
131  
132 =head2 y()
133  
134 Get or set the y coordinate of the top left corner of where to start drawing the bar code.
135  
136 =cut
137  
138 has y => (
139 is => 'rw',
140 1 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
141 1 );
142  
143 =head2 color()
144  
145 Get or set the color of the bar code. Defaults to C<black>. You can also pass an L<Imager::Color> object.
146  
147 =cut
148  
149 has color => (
150 is => 'rw',
151 1 ▶ default => sub { 'black' },
Subroutine __ANON__: called CC=1 SCAR=0.0
152 1 );
153  
154 =head2 scale()
155  
156 Get or set the scale of the bar code. Defaults to C<2>. Not recommended to set it to less than 2.
157  
158 A bar in the bar code is 1 pixel wide per unit of scale.
159  
160 =cut
161  
162 has scale => (
163 is => 'rw',
164 1 ▶ default => sub { 2 },
Subroutine __ANON__: called CC=1 SCAR=0.0
165 1 );
166  
167 =head2 height()
168  
169 Get or set the height of the bar code. Defaults to the height of the C<image>.
170  
171 =cut
172  
173 has height => (
174 is => 'rw',
175 lazy => 1,
176 default => sub {
177 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
178 1 ▶ return $self->has_image ? $self->image->getheight : 100;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_image ? : 0 1 1
179 },
180 1 );
181  
182 =head2 image()
183  
184 Get or set the L<Imager> object. Defaults to a 100px tall image with a white background. The image will be however long it needs to be to contain the bar code.
185  
186 =cut
187  
188 has image => (
189 is => 'rw',
190 lazy => 1,
191 predicate => 1,
192 default => sub {
193 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
194 1 my $x = length($self->_barcode) * $self->scale;
195 1 my $image = Imager->new(xsize => $x, ysize => $self->height);
196 1 $image->box(color => 'white', filled => 1);
197 1 return $image;
198 },
199 1 );
200  
201 =head2 text()
202  
203 Get or set the text to be encoded into the bar code.
204  
205 =cut
206  
207 1 has text => (
208 is => 'rw',
209 required => 1,
210 );
211  
212 has _code => ( # private
213 is => 'rw',
214 1 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
215 isa => sub {
216 7 ▶ ouch('invalid code', 'Code must be one of A, B, or C.') unless ($_[0] eq 'A' || $_[0] eq 'B' || $_[0] eq 'C' || $_[0] eq '');
Subroutine __ANON__: called CC=5 SCAR=16.8
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] eq "A" or $_[0] eq "B" or $_[0] eq "C" or $_[0] eq "" 0 7 7
Condition77%7/9
$_[0] eq "A" or $_[0] eq "B" or $_[0] eq "C" or $_[0] eq ""
l!l&&r!l&&!r
430
$_[0] eq "A" or $_[0] eq "B" or $_[0] eq "C"
l!l&&r!l&&!r
223
$_[0] eq "A" or $_[0] eq "B"
l!l&&r!l&&!r
025
MC/DC0%0/4
$_[0] eq "A" or $_[0] eq "B" or $_[0] eq "C" or $_[0] eq ""
$_[0] eq "A" $_[0] eq "B" $_[0] eq "C" $_[0] eq ""
Truth table60%3/5
A or B or C or D
A= $_[0] eq "A"B= $_[0] eq "B"C= $_[0] eq "C"D= $_[0] eq ""
ABCDresult
1XXX1
01XX1
001X1
00011
00000
217 },
218 1 );
219  
220 has _encoded => ( # private
221 is => 'rw',
222 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
223 1 );
224  
225 has _barcode => ( # private
226 is => 'rw',
227 lazy => 1,
228 default => sub {
229 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
230 0 return $self->barcode
231 },
232 1 );
233  
234 =head2 draw()
235  
236 Draws a barcode on the image. Returns C<$self> for method chaining.
237  
238 =cut
239  
240 sub draw {
241 1 ▶ my $self = shift;
Subroutine draw: called CC=3 SCAR=11.0
Pod documented
242 1 my @barcode = split //, $self->barcode;
243 1 my $x = $self->x;
244 1 my $y = $self->y;
245 1 my $scale = $self->scale;
246 1 my $image = $self->image;
247 1 my $height = $self->height;
248 1 my $color = $self->color;
249 1 foreach my $element (@barcode) {
250 123 $x += $scale;
251 123 ▶ next unless $element eq '#';
Branch: 1 branch
BranchTrueFalseTotal
unless $element eq "#" 63 60 123
252 60 $image->box(
253 color => $color,
254 xmin => $x - $scale,
255 ymin => $y,
256 xmax => $x,
257 ymax => $y + $height,
258 filled => 1,
259 );
260 }
261 1 return $self;
262 }
263  
264 sub barcode {
265 2 ▶ my $self = shift;
Subroutine barcode: called CC=2 SCAR=6.9
Pod undocumented
266 2 $self->encode;
267 2 my @encoded = @{ $self->_encoded };
268 2 ▶ ouch('no encoded text',"No encoded text found") unless @encoded;
Branch: 1 branch
BranchTrueFalseTotal
unless @encoded 0 2 2
269 2 return $self->_barcode(join '', map { $_ = $ENCODING[$_]; tr/01/ \#/; $_ } @encoded); # cache it in case we need it for other things
270 }
271  
272 sub encode {
273 2 ▶ my ($self, $preferred_code) = @_;
Subroutine encode: called CC=11 SCAR=25.9
Pod undocumented
274 2 ▶ ouch('invalid preffered code',"Invalid preferred code ``$preferred_code''") if defined $preferred_code && !exists $CODE{$preferred_code};
Branch: 1 branch
BranchTrueFalseTotal
if defined $preferred_code and not exists $Imager::Barcode128::CODE{$preferred_code} 0 2 2
Condition33%1/3
defined $preferred_code and not exists $Imager::Barcode128::CODE{$preferred_code}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $preferred_code and not exists $Imager::Barcode128::CODE{$preferred_code}
defined $preferred_code not exists $Imager::Barcode128::CODE{$preferred_code}
Truth table33%1/3
A and not(B)
A= defined $preferred_codeB= not exists $Imager::Barcode128::CODE{$preferred_code}
ABresult
0X0
100
111
275 2 my $text = $self->text;
276 2 $self->_code('');
277 2 my $encoded = $self->_encoded([]);
278 2 my $sanity = 0;
279 2 ▶ while (length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text) 4 2 6
280 4 ▶ ouch('overflow',"Sanity Check Overflow") if $sanity++ > 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $sanity++ > 1000 0 4 4
281 4 my @chars;
282 4 ▶ if (defined $preferred_code && $preferred_code && (@chars = _encodable($preferred_code, $text))) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $preferred_code and $preferred_code and @chars = _encodable($preferred_code, $text)) { } 0 4 4
elsif (@chars = _encodable('C', $text)) { } 2 2 4
Condition33%2/6
defined $preferred_code and $preferred_code and @chars = _encodable($preferred_code, $text)
!ll&&!rl&&r
400
defined $preferred_code and $preferred_code
!ll&&!rl&&r
400
MC/DC0%0/3
defined $preferred_code and $preferred_code and @chars = _encodable($preferred_code, $text)
defined $preferred_code $preferred_code @chars = _encodable($preferred_code, $text)
Truth table25%1/4
A and B and C
A= defined $preferred_codeB= $preferred_codeC= @chars = _encodable($preferred_code, $text)
ABCresult
0XX0
10X0
1100
1111
283 0 $self->start($preferred_code);
284 0 push @$encoded, map { $CODE{$preferred_code}{$_} } @chars;
285 }
286 elsif (@chars = _encodable('C', $text)) {
287 2 $self->start('C');
288 2 push @$encoded, map { $CODE{C}{$_} } @chars;
289 }
290 else {
291 2 my %x = map { $_ => [ _encodable($_, $text) ] } qw(A B);
292 2 ▶ my $code = (@{$x{A}} >= @{$x{B}} ? 'A' : 'B'); # prefer A if equal
Branch: 1 branch
BranchTrueFalseTotal
@{$x{'A'};} >= @{$x{'B'};} ? : 0 2 2
293 2 $self->start($code);
294 2 @chars = @{ $x{$code} };
295 2 push @$encoded, map { $CODE{$code}{$_} } @chars;
296 }
297 4 ▶ ouch('no encoding', "Unable to find encoding for ``$text''") unless @chars;
Branch: 1 branch
BranchTrueFalseTotal
unless @chars 0 4 4
298 4 substr($text, 0, length join '', @chars) = '';
299 }
300 2 $self->stop;
301 }
302  
303 sub start {
304 4 ▶ my ($self, $new_code) = @_;
Subroutine start: called CC=4 SCAR=13.9
Pod undocumented
305 4 my $old_code = $self->_code;
306 4 ▶ if ($old_code ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_code ne '') { } 2 2 4
307 2 ▶ my $func = $FUNC_CHARS{"Code$new_code"} or ouch('cannot switch codes', "Unable to switch from ``$old_code'' to ``$new_code''");
Branch: 1 branch
BranchTrueFalseTotal
my $func = $Imager::Barcode128::FUNC_CHARS{"Code$new_code"} or ouch("cannot switch codes", "Unable to switch from ``${old_code}'' to ``${new_code}''") 0 2 2
308 2 push @{ $self->_encoded }, $CODE{$old_code}{$func};
309 }
310 else {
311 2 ▶ my $func = $FUNC_CHARS{"Start$new_code"} or ouch('bad start code',"Unable to start with ``$new_code''");
Branch: 1 branch
BranchTrueFalseTotal
my $func = $Imager::Barcode128::FUNC_CHARS{"Start$new_code"} or ouch("bad start code", "Unable to start with ``${new_code}''") 0 2 2
312 2 @{ $self->_encoded } = $CODE{$new_code}{$func};
313 }
314 4 $self->_code($new_code);
315 }
316  
317 sub stop {
318 2 ▶ my ($self) = @_;
Subroutine stop: called CC=2 SCAR=6.9
Pod undocumented
319 2 my $encoded = $self->_encoded;
320 2 my $sum = $encoded->[0];
321 2 ▶ for (my $i = 1; $i < @{ $encoded }; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$encoded;}) 16 2 18
322 16 $sum += $i * $encoded->[$i];
323 }
324 2 my $stop = Stop;
325 2 push @{ $encoded }, ($sum % 103), $CODE{C}{$stop};
326 }
327  
328 sub _encodable {
329 11 ▶ my ($code, $string) = @_;
Subroutine _encodable: called CC=9 SCAR=22.1
330 11 my @chars;
331 11 ▶ while (length $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string) 17 3 20
332 17 my $old = $string;
333 17 ▶ push @chars, $1 while($code eq 'C' && $string =~ s/^(\d\d)//);
Branch: 1 branch
BranchTrueFalseTotal
$code eq "C" and $string =~ s/^(\d\d)// and do { push @chars, $1 } 6 17 23
Condition100%3/3
$code eq "C" and $string =~ s/^(\d\d)//
!ll&&!rl&&r
986
MC/DC100%2/2
$code eq "C" and $string =~ s/^(\d\d)//
$code eq "C" $string =~ s/^(\d\d)//
Truth table100%3/3
A and B
A= $code eq "C"B= $string =~ s/^(\d\d)//
ABresult
0X0
100
111
334 17 my $char;
335 17 ▶ while (defined($char = substr($string, 0, 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($char = substr($string, 0, 1))) 43 0 43
336 43 ▶ last if $code ne 'C' && $string =~ /^\d\d\d\d\d\d/;
Branch: 1 branch
BranchTrueFalseTotal
if $code ne "C" and $string =~ /^\d\d\d\d\d\d/ 0 43 43
Condition66%2/3
$code ne "C" and $string =~ /^\d\d\d\d\d\d/
!ll&&!rl&&r
8350
MC/DC0%0/2
$code ne "C" and $string =~ /^\d\d\d\d\d\d/
$code ne "C" $string =~ /^\d\d\d\d\d\d/
Truth table66%2/3
A and B
A= $code ne "C"B= $string =~ /^\d\d\d\d\d\d/
ABresult
0X0
100
111
337 43 ▶ last unless exists $CODE{$code}{$char};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Imager::Barcode128::CODE{$code}{$char} 17 26 43
338 26 push @chars, $char;
339 26 $string =~ s/^\Q$char\E//;
340 }
341 17 ▶ last if $old eq $string; # stop if no more changes made to $string
Branch: 1 branch
BranchTrueFalseTotal
if $old eq $string 8 9 17
342 }
343 11 return @chars;
344 }
345  
346 =head1 EXCEPTIONS
347  
348 This module will throw an L<Ouch> if anything goes wrong. Under normal circumstances you should not expect to need to handle exceptions.
349  
350 =head1 TODO
351  
352 None that I can think of at this time.
353  
354 =head2 SEE ALSO
355  
356 Most of the logic of this module was stolen from an older module called L<Barcode::Code128>. I build this because I wanted to generate the bar codes with L<Imager> rather than L<GD>.
357  
358 =head1 PREREQS
359  
360 L<Moo>
361 L<Imager>
362 L<Ouch>
363  
364 =head1 SUPPORT
365  
366 =over
367  
368 =item Repository
369  
370 L<http://github.com/rizen/Imager-Barcode128>
371  
372 =item Bug Reports
373  
374 L<http://github.com/rizen/Imager-Barcode128/issues>
375  
376 =back
377  
378  
379 =head1 AUTHOR
380  
381 =over
382  
383 =item JT Smith <jt_at_plainblack_dot_com>
384  
385 =back
386  
387 =head1 LEGAL
388  
389 Imager::Barcode128 is Copyright 2015 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
390  
391 =cut
392  
393 1 1;
↑ Summary