blib/lib/Imager/IMBarcode/JP.pm

filter: Statementstmt 100% 106 / 106 Branchbran 100% 22 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 50.0% 1 / 2 totaltotal 99.3% 143 / 144 CC 20 SCAR 16.0
CC
20
Cov
100%
make_bars
10.0
_find_bar_by_check
5.0
__ANON__
4.0
CRAP 4.9
↑ Summary
1 package Imager::IMBarcode::JP;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.02';
11  
12 2 has zipcode => (
13 is => 'rw',
14 isa => 'Int',
15 default => '0000000',
16 );
17  
18 2 has address => (
19 is => 'rw',
20 isa => 'Str',
21 default => '',
22 );
23  
24 2 has _pos => (
25 is => 'rw',
26 isa => 'Int',
27 default => 0,
28 );
29  
30 2 has _base => (
31 is => 'ro',
32 isa => 'Imager',
33 lazy_build => 1,
34 );
35  
36 has _char_code => (
37 is => 'ro',
38 isa => 'HashRef',
39 4 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
40 4 my %code = (
41 STC => +{
42 bar => 13,
43 },
44 SPC => +{
45 bar => 31,
46 },
47 1 => +{
48 check => 1,
49 bar => 114,
50 },
51 2 => +{
52 check => 2,
53 bar => 132,
54 },
55 3 => +{
56 check => 3,
57 bar => 312,
58 },
59 4 => +{
60 check => 4,
61 bar => 123,
62 },
63 5 => +{
64 check => 5,
65 bar => 141,
66 },
67 6 => +{
68 check => 6,
69 bar => 321,
70 },
71 7 => +{
72 check => 7,
73 bar => 213,
74 },
75 8 => +{
76 check => 8,
77 bar => 231,
78 },
79 9 => +{
80 check => 9,
81 bar => 411,
82 },
83 0 => +{
84 check => 0,
85 bar => 144,
86 },
87 '-' => +{
88 check => 10,
89 bar => 414,
90 },
91 CC1 => +{
92 check => 11,
93 bar => 324,
94 },
95 CC2 => +{
96 check => 12,
97 bar => 342,
98 },
99 CC3 => => +{
100 check => 13,
101 bar => 234,
102 },
103 CC4 => +{
104 check => 14,
105 bar => 432,
106 },
107 CC5 => +{
108 check => 15,
109 bar => 243,
110 },
111 CC6 => +{
112 check => 16,
113 bar => 423,
114 },
115 CC7 => +{
116 check => 17,
117 bar => 441,
118 },
119 CC8 => +{
120 check => 18,
121 bar => 111,
122 },
123 );
124 4 my @cc = @code{qw(CC1 CC2 CC3)};
125 4 for my $i (0 .. $#cc) {
126 12 for my $num (0 .. 9) {
127 104 my $k = chr(65 + (10 * $i) + $num);
128 104 my $v = [ $cc[$i], $code{$num} ];
129 $code{$k} = +{
130 check => [ $cc[$i]->{check}, $code{$num}->{check} ],
131 104 bar => [ $cc[$i]->{bar}, $code{$num}->{bar} ],
132 };
133 104 ▶ last if $k ge 'Z';
Branch: 1 branch
BranchTrueFalseTotal
if $k ge "Z" 4 100 104
134 }
135 }
136 4 return \%code;
137 },
138 2 );
139  
140 2 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 2 __PACKAGE__->meta->make_immutable;
143  
144 38 ▶sub _to_code {
Subroutine _to_code: called CC=1 SCAR=0.0
145 38 my($self, $char) = @_;
146 38 return $self->_char_code->{uc($char)};
147 }
148  
149 2 ▶sub _find_bar_by_check {
Subroutine _find_bar_by_check: called CC=5 SCAR=16.1
150 2 my($self, $check) = @_;
151 2 my $code = $self->_char_code;
152 2 for my $v (values %$code) {
153 60 ▶ next unless exists $v->{check};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $v->{"check"} 2 58 60
154 58 my $val = $v->{check};
155 58 ▶ next if ref($val) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "ARRAY" 31 27 58
156 27 ▶ next if $val != $check;
Branch: 1 branch
BranchTrueFalseTotal
if $val != $check 25 2 27
157 2 return $v->{bar};
158 }
159 }
160  
161 4 ▶sub draw {
Subroutine draw: called CC=2 SCAR=6.9
Pod documented
162 4 my $self = shift;
163 4 my $bars = $self->make_bars;
164 2 for my $bar (@$bars) {
165 46 $self->_draw_num(split //, $bar);
166 }
167 2 return $self->_base;
168 }
169  
170 4 ▶sub make_bars {
Subroutine make_bars: called CC=10 SCAR=23.0
Pod undocumented
171 4 my $self = shift;
172 4 ▶ unless ($self->zipcode =~ /^\d{7}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->zipcode =~ /^\d{7}$/) 1 3 4
173 1 croak('Invalid zipcode(): ' . $self->zipcode);
174 }
175 3 ▶ unless ($self->address =~ /^[-0-9A-Z]*$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->address =~ /^[-0-9A-Z]*$/i) 1 2 3
176 1 croak('Invalid address(): ' . $self->address);
177 }
178 2 my @bars = ();
179 2 my $checksum = 0;
180  
181 2 my $start = $self->_to_code('STC');
182 2 push @bars, $start->{bar};
183  
184 2 for my $chr (split //, $self->zipcode) {
185 14 my $code = $self->_to_code($chr);
186 14 $checksum += $code->{check};
187 14 push @bars, $code->{bar};
188 }
189  
190 2 for my $chr (split //, $self->address) {
191 7 my $code = $self->_to_code($chr);
192 7 my $check = $code->{check};
193 7 ▶ if (ref($check) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $check eq 'ARRAY') { } 6 1 7
194 6 my $bar = $code->{bar};
195 6 for my $i (0 .. $#{$check}) {
196 12 my $c = $check->[$i];
197 12 my $b = $bar->[$i];
198 12 $checksum += $c;
199 12 push @bars, $b;
200 12 ▶ last if @bars >= 21;
Branch: 1 branch
BranchTrueFalseTotal
if @bars >= 21 1 11 12
201 }
202 }
203 else {
204 1 $checksum += $code->{check};
205 1 push @bars, $code->{bar};
206 }
207 7 ▶ last if @bars >= 21;
Branch: 1 branch
BranchTrueFalseTotal
if @bars >= 21 1 6 7
208 }
209  
210 2 ▶ while (scalar(@bars) < 21) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @bars < 21) 13 2 15
211 13 my $code = $self->_to_code('CC4');
212 13 $checksum += $code->{check};
213 13 push @bars, $code->{bar};
214 }
215  
216 2 my $checkdigit = 19 - ($checksum % 19);
217 2 my $bar = $self->_find_bar_by_check($checkdigit);
218 2 push @bars, $bar;
219  
220 2 my $stop = $self->_to_code('SPC');
221 2 push @bars, $stop->{bar};
222 2 return \@bars;
223 }
224  
225 2 ▶sub _build__base {
Subroutine _build__base: called CC=1 SCAR=0.0
226 2 my $self = shift;
227 2 my $img = Imager->new(
228 xsize => 979,
229 ysize => 90,
230 );
231 2 $img->settag(name => 'i_xres', value => 300);
232 2 $img->settag(name => 'i_yres', value => 300);
233 2 $img->box(filled => 1, color => '#ffffff');
234 2 return $img;
235 }
236  
237 46 ▶sub _draw_num {
Subroutine _draw_num: called CC=3 SCAR=11.0
238 46 my $self = shift;
239 46 my @nums = @_;
240 46 for my $num (@nums) {
241 134 my $pos = $self->_pos;
242 134 my $x = 24 + $pos * 14;
243 134 ▶ my $ymin = $num =~ m{^[12]$} ? 24 : 38;
Branch: 1 branch
BranchTrueFalseTotal
$num =~ /^[12]$/ ? : 60 74 134
244 my $ymax = +{
245 1 => $ymin + 41,
246 2 => $ymin + 27,
247 3 => $ymin + 27,
248 4 => $ymin + 13,
249 134 }->{$num};
250 134 $self->_base->box(
251 xmin => $x,
252 ymin => $ymin,
253 xmax => $x + 6,
254 ymax => $ymax,
255 color => '#000000',
256 filled => 1,
257 );
258 134 $self->_pos($self->_pos + 1);
259 }
260 }
261  
262 2 1;
263 __END__
↑ Summary