File Coverage

blib/lib/PDF/Reuse/Barcode.pm
Criterion Covered Total %
statement 12 350 3.4
branch 0 104 0.0
condition n/a
subroutine 4 22 18.1
pod 12 18 66.6
total 28 494 5.6


line stmt bran cond sub pod time code
1             package PDF::Reuse::Barcode;
2            
3 1     1   104579 use 5.006;
  1         4  
  1         83  
4 1     1   6 use PDF::Reuse;
  1         2  
  1         210  
5 1     1   7 use strict;
  1         12  
  1         32  
6 1     1   5 use warnings;
  1         2  
  1         3696  
7            
8             our $VERSION = '0.05';
9            
10             my ($str, $xsize, $ysize, $height, $sPtn, @sizes, $length, $value, %default);
11            
12             sub init
13 0     0 0   { %default = ( value => '0000000',
14             x => 0,
15             y => 0,
16             size => 1,
17             xsize => 1,
18             ysize => 1,
19             rotate => 0,
20             background => '1 1 1',
21             drawbackground => 1,
22             text => 'yes',
23             prolong => 0,
24             hide_asterisk => 0,
25             mode => 'Type3');
26 0           $str = '';
27 0           $xsize = 1;
28 0           $ysize = 1;
29 0           $height = 37;
30 0           $sPtn = '';
31 0           @sizes = ();
32 0           $length = 0;
33 0           $value = ''
34             }
35            
36            
37             sub general1
38 0 0   0 0   { $default{'xsize'} = 1 unless ($default{'xsize'} != 0);
39 0 0         $default{'ysize'} = 1 unless ($default{'ysize'} != 0);
40 0 0         $default{'size'} = 1 unless ($default{'size'} != 0);
41 0           $xsize = $default{'xsize'} * $default{'size'};
42 0           $ysize = $default{'ysize'} * $default{'size'};
43 0           $str = "q\n";
44 0           $str .= "$xsize 0 0 $ysize $default{'x'} $default{'y'} cm\n";
45 0 0         if ($default{'rotate'} != 0)
46 0           { my $radian = sprintf("%.6f", $default{'rotate'} / 57.2957795); # approx.
47 0           my $Cos = sprintf("%.6f", cos($radian));
48 0           my $Sin = sprintf("%.6f", sin($radian));
49 0           my $negSin = $Sin * -1;
50 0           $str .= "$Cos $Sin $negSin $Cos 0 0 cm\n";
51             }
52             }
53            
54             sub general2
55 0     0 0   { $length = 20 + (length($sPtn) * 0.9);
56 0           my $height = 38;
57 0           my $step = 9;
58 0           my $prolong = 0;
59 0 0         if ($default{'prolong'} > 1)
60 0           { $prolong = $default{'prolong'};
61 0           $height = 26 + ($prolong * 12);
62             }
63 0 0         if ($default{'drawbackground'})
64 0           { $str .= "$default{'background'} rg\n";
65 0           $str .= "0 0 $length $height re\n";
66 0           $str .= 'f*' . "\n";
67 0           $str .= "0 0 0 rg\n";
68             }
69            
70 0           prAdd($str);
71            
72 0           @sizes = prFontSize(12);
73            
74 0 0         if ($default{'mode'} eq 'Type3')
75 0           { prBar( 10, $step, $sPtn);
76             }
77             else # graphic mode
78 0           { $str = Bar( 10, $step, $sPtn);
79             }
80            
81 0           $prolong--;
82            
83 0 0         if ($prolong > 0)
84 0           { $sPtn =~ s/G/1/go;
85 0           while ($prolong > 0)
86 0 0         { if ($prolong > 1)
87 0           { $prolong--;
88 0           $step += 12;
89             }
90             else
91 0           { $step += (12 * $prolong);
92 0           $prolong = 0;
93             }
94 0 0         if ($default{'mode'} eq 'Type3')
95 0           { prBar( 10, $step, $sPtn);
96             }
97             else # graphic mode
98 0           { $str .= Bar( 10, $step, $sPtn);
99             }
100             }
101             }
102             # print the graphic mode bars
103 0 0         if ($default{'mode'} ne 'Type3')
104 0           { $str .= "B\n";
105 0           prAdd($str);
106             }
107            
108             }
109            
110             sub general3
111 0     0 0   { $str = "Q\n";
112 0           prAdd($str);
113 0           prFontSize($sizes[1]);
114             }
115            
116             sub standardEnd
117 0     0 0   { general2();
118            
119 0 0         if ($default{'text'})
120 0           { my @vec = prFont('C');
121 0           prFontSize(10);
122 0           my $textLength = length($value) * 6;
123 0           my $start = ($length - $textLength) / 2;
124 0           prText($start, 1.5, $value);
125 0           prFont($vec[3]);
126             }
127 0           general3();
128            
129 0           1;
130             }
131            
132             sub Bar
133 0     0 0   { my ($x, $y, $pattern) = @_;
134 0           my $yEnd = $y + 20;
135 0           my $yG = $y - 3;
136            
137 0           my $string = "0.92 w\n 0 0 0 RG\n";
138 0           for (split(//, $pattern))
139 0 0         { if ($_ eq '1')
    0          
140 0           { $string .= "$x $yEnd m\n $x $y l\n";
141             }
142             elsif($_ eq 'G')
143 0           { $string .= "$x $yEnd m\n $x $yG l\n";
144             }
145 0           $x += 0.91;
146             }
147 0           return $string;
148             }
149            
150             sub Code128
151 0     0 1   { eval 'require Barcode::Code128';
152 0           init();
153 0           my %param = @_;
154 0           for (keys %param)
155 0           { my $lc = lc($_);
156 0 0         if (exists $default{$lc})
157 0           { $default{$lc} = $param{$_};
158             }
159             else
160 0           { print STDERR "Unknown parameter $_ , not used \n";
161             }
162             }
163 0           $value = $default{'value'};
164            
165 0           general1();
166            
167 0           my $oGDBar = Barcode::Code128->new();
168 0 0         if (! $oGDBar)
169 0           { die "The translation of $value to barcodes didn't succeed, aborts\n";
170             }
171             else
172             {
173 0           $sPtn = $oGDBar->barcode($value);
174 0           $sPtn =~ tr/#/1/;
175 0           $sPtn =~ tr/ /0/;
176             }
177 0           standardEnd();
178 0           1;
179             }
180            
181            
182             sub Code39
183 0     0 1   { eval 'require GD::Barcode::Code39';
184 0           init();
185 0           my %param = @_;
186 0           for (keys %param)
187 0           { my $lc = lc($_);
188 0 0         if (exists $default{$lc})
189 0           { $default{$lc} = $param{$_};
190             }
191             else
192 0           { print STDERR "Unknown parameter $_ , not used \n";
193             }
194             }
195 0           $value = $default{'value'};
196            
197 0           general1();
198            
199 0           my $oGDBar = GD::Barcode::Code39->new($value);
200 0 0         if (! $oGDBar)
201 0           { die "$GD::Barcode::Code39::errStr\n";
202             }
203             else
204 0           { $sPtn = $oGDBar->barcode();
205             }
206 0 0         if ($default{hide_asterisk})
207 0           { $value =~ s/^\*//;
208 0           $value =~ s/\*$//;
209             }
210 0           standardEnd();
211 0           1;
212             }
213            
214             sub COOP2of5
215 0     0 1   { eval 'require GD::Barcode::COOP2of5';
216 0           init();
217 0           my %param = @_;
218 0           for (keys %param)
219 0           { my $lc = lc($_);
220 0 0         if (exists $default{$lc})
221 0           { $default{$lc} = $param{$_};
222             }
223             else
224 0           { print STDERR "Unknown parameter $_ , not used \n";
225             }
226             }
227 0           $value = $default{'value'};
228            
229 0           general1();
230            
231 0           my $oGDBar = GD::Barcode::COOP2of5->new($value);
232 0 0         if (! $oGDBar)
233 0           { die "$GD::Barcode::COOP2of5::errStr\n";
234             }
235             else
236 0           { $sPtn = $oGDBar->barcode();
237             }
238            
239 0           standardEnd();
240 0           1;
241             }
242            
243             sub IATA2of5
244 0     0 1   { eval 'require GD::Barcode::IATA2of5';
245 0           init();
246 0           my %param = @_;
247 0           for (keys %param)
248 0           { my $lc = lc($_);
249 0 0         if (exists $default{$lc})
250 0           { $default{$lc} = $param{$_};
251             }
252             else
253 0           { print STDERR "Unknown parameter $_ , not used \n";
254             }
255             }
256 0           $value = $default{'value'};
257            
258 0           general1();
259            
260 0           my $oGDBar = GD::Barcode::IATA2of5->new($value);
261 0 0         if (! $oGDBar)
262 0           { die "$GD::Barcode::IATA2of5::errStr\n";
263             }
264             else
265 0           { $sPtn = $oGDBar->barcode();
266             }
267            
268 0           standardEnd();
269 0           1;
270            
271             }
272            
273             sub Industrial2of5
274 0     0 1   { eval 'require GD::Barcode::Industrial2of5';
275 0           init();
276 0           my %param = @_;
277 0           for (keys %param)
278 0           { my $lc = lc($_);
279 0 0         if (exists $default{$lc})
280 0           { $default{$lc} = $param{$_};
281             }
282             else
283 0           { print STDERR "Unknown parameter $_ , not used \n";
284             }
285             }
286 0           $value = $default{'value'};
287            
288 0           general1();
289            
290 0           my $oGDBar = GD::Barcode::Industrial2of5->new($value);
291 0 0         if (! $oGDBar)
292 0           { die "$GD::Barcode::Industrial2of5::errStr\n";
293             }
294             else
295 0           { $sPtn = $oGDBar->barcode();
296             }
297            
298 0           standardEnd();
299 0           1;
300             }
301            
302             sub Matrix2of5
303 0     0 1   { eval 'require GD::Barcode::Matrix2of5';
304 0           init();
305 0           my %param = @_;
306 0           for (keys %param)
307 0           { my $lc = lc($_);
308 0 0         if (exists $default{$lc})
309 0           { $default{$lc} = $param{$_};
310             }
311             else
312 0           { print STDERR "Unknown parameter $_ , not used \n";
313             }
314             }
315 0           $value = $default{'value'};
316            
317 0           general1();
318            
319 0           my $oGDBar = GD::Barcode::Matrix2of5->new($value);
320 0 0         if (! $oGDBar)
321 0           { die "$GD::Barcode::Matrix2of5::errStr\n";
322             }
323             else
324 0           { $sPtn = $oGDBar->barcode();
325             }
326            
327 0           standardEnd();
328 0           1;
329             }
330            
331             sub NW7
332 0     0 1   { eval 'require GD::Barcode::NW7';
333 0           init();
334 0           my %param = @_;
335 0           for (keys %param)
336 0           { my $lc = lc($_);
337 0 0         if (exists $default{$lc})
338 0           { $default{$lc} = $param{$_};
339             }
340             else
341 0           { print STDERR "Unknown parameter $_ , not used \n";
342             }
343             }
344 0           $value = $default{'value'};
345            
346 0           general1();
347            
348 0           my $oGDBar = GD::Barcode::NW7->new($value);
349 0 0         if (! $oGDBar)
350 0           { die "$GD::Barcode::NW7::errStr\n";
351             }
352             else
353 0           { $sPtn = $oGDBar->barcode();
354             }
355            
356 0           standardEnd();
357 0           1;
358             }
359            
360            
361            
362             sub EAN13
363 0     0 1   { eval 'require GD::Barcode::EAN13';
364 0           init();
365 0           my %param = @_;
366 0           for (keys %param)
367 0           { my $lc = lc($_);
368 0 0         if (exists $default{$lc})
369 0           { $default{$lc} = $param{$_};
370             }
371             else
372 0           { print STDERR "Unknown parameter $_ , not used \n";
373             }
374             }
375 0           $value = $default{'value'};
376            
377 0           general1();
378            
379 0 0         if ($value =~ m'([^0-9]+)'o)
380 0           { die "Invalid character $1, aborts\n";
381             }
382            
383 0 0         if (length($value) == 12)
384 0           { $value .= GD::Barcode::EAN13::calcEAN13CD($value);
385             }
386 0           my $oGDBar = GD::Barcode::EAN13->new($value);
387 0 0         if (! $oGDBar)
388 0           { die "$GD::Barcode::EAN13::errStr\n";
389             }
390             else
391 0           { $sPtn = $oGDBar->barcode();
392             }
393 0           general2();
394            
395 0 0         if ($default{'text'})
396 0           { my $siffra = substr($value, 0, 1);
397 0           my $del1 = substr($value, 1, 6);
398 0           my $del2 = substr($value, 7, 6);
399            
400 0           my @vec = prFont('C');
401            
402 0           prFontSize(10);
403            
404 0           prText(1, 2, $siffra);
405 0           prText(14, 2, $del1);
406 0           prText(56, 2, $del2);
407            
408 0           prFont($vec[3]);
409             }
410 0           general3;
411 0           1;
412             }
413            
414             sub EAN8
415 0     0 1   { eval 'require GD::Barcode::EAN8';
416 0           init();
417 0           my %param = @_;
418 0           for (keys %param)
419 0           { my $lc = lc($_);
420 0 0         if (exists $default{$lc})
421 0           { $default{$lc} = $param{$_};
422             }
423             else
424 0           { print STDERR "Unknown parameter $_ , not used \n";
425             }
426             }
427 0           $value = $default{'value'};
428            
429 0           general1();
430            
431 0 0         if ($value =~ m'([^0-9]+)'o)
432 0           { die "Invalid character $1, aborts\n";
433             }
434            
435 0 0         if (length($value) == 7)
436 0           { $value .= GD::Barcode::EAN8::calcEAN8CD($value);
437             }
438 0           my $oGDBar = GD::Barcode::EAN8->new($value);
439 0 0         if (! $oGDBar)
440 0           { die "$GD::Barcode::EAN8::errStr\n";
441             }
442             else
443 0           { $sPtn = $oGDBar->barcode();
444             }
445 0           general2();
446            
447 0 0         if ($default{'text'})
448 0           { my $del1 = substr($value, 0, 4);
449 0           my $del2 = substr($value, 4, 4);
450 0           my @vec = prFont('C');
451 0           prFontSize(10);
452 0           prText(14, 2, $del1);
453 0           prText(42.5, 2, $del2);
454 0           prFont($vec[3]);
455             }
456 0           general3;
457 0           1;
458             }
459            
460             sub ITF
461 0     0 1   { eval 'require GD::Barcode::ITF';
462 0           init();
463 0           my %param = @_;
464 0           for (keys %param)
465 0           { my $lc = lc($_);
466 0 0         if (exists $default{$lc})
467 0           { $default{$lc} = $param{$_};
468             }
469             else
470 0           { print STDERR "Unknown parameter $_ , not used \n";
471             }
472             }
473 0           $value = $default{'value'};
474            
475 0           general1();
476            
477 0           my $oGDBar = GD::Barcode::ITF->new($value);
478 0 0         if (! $oGDBar)
479 0           { die "$GD::Barcode::ITF::errStr\n";
480             }
481             else
482 0           { $sPtn = $oGDBar->barcode();
483             }
484            
485 0           standardEnd();
486 0           1;
487             }
488            
489             sub UPCA
490 0     0 1   { eval 'require GD::Barcode::UPCA';
491 0           init();
492 0           my %param = @_;
493 0           for (keys %param)
494 0           { my $lc = lc($_);
495 0 0         if (exists $default{$lc})
496 0           { $default{$lc} = $param{$_};
497             }
498             else
499 0           { print STDERR "Unknown parameter $_ , not used \n";
500             }
501             }
502 0           $value = $default{'value'};
503            
504 0           general1();
505            
506 0 0         if ($value =~ m'([^0-9]+)'o)
507 0           { die "Invalid character $1, aborts\n";
508             }
509            
510 0 0         if (length($value) == 11)
511 0           { $value .= GD::Barcode::UPCA::calcUPCACD($value);
512             }
513 0           my $oGDBar = GD::Barcode::UPCA->new($value);
514 0 0         if (! $oGDBar)
515 0           { die "$GD::Barcode::UPCA::errStr\n";
516             }
517             else
518 0           { $sPtn = $oGDBar->barcode();
519             }
520 0           general2();
521            
522 0 0         if ($default{'text'})
523 0           { my $siffra1 = substr($value, 0, 1);
524 0           my $del1 = substr($value, 1, 5);
525 0           my $del2 = substr($value, 6, 5);
526 0           my $siffra2 = substr($value, 11, 1);
527            
528 0           my @vec = prFont('C');
529            
530 0           prFontSize(10);
531            
532 0           prText(2, 2, $siffra1);
533 0           prText(20, 2, $del1);
534 0           prText(56, 2, $del2);
535 0           prText(97, 2, $siffra2);
536            
537 0           prFont($vec[3]);
538             }
539 0           general3;
540 0           1;
541             }
542            
543             sub UPCE
544 0     0 1   { eval 'require GD::Barcode::UPCE';
545 0           init();
546 0           my %param = @_;
547 0           for (keys %param)
548 0           { my $lc = lc($_);
549 0 0         if (exists $default{$lc})
550 0           { $default{$lc} = $param{$_};
551             }
552             else
553 0           { print STDERR "Unknown parameter $_ , not used \n";
554             }
555             }
556 0           $value = $default{'value'};
557            
558 0           general1();
559            
560 0 0         if ($value =~ m'([^0-9]+)'o)
561 0           { die "Invalid character $1, aborts\n";
562             }
563            
564 0 0         if (length($value) == 6)
    0          
565 0           { $value = '0' . $value;
566 0           my $cd = GD::Barcode::UPCE::calcUPCECD($value);
567 0           $value .= $cd;
568             }
569             elsif (length($value) == 7)
570 0           { my $cd = GD::Barcode::UPCE::calcUPCECD($value);
571 0           $value .= $cd;
572             }
573 0           my $oGDBar = GD::Barcode::UPCE->new($value);
574 0 0         if (! $oGDBar)
575 0           { die "$GD::Barcode::UPCE::errStr\n";
576             }
577             else
578 0           { $sPtn = $oGDBar->barcode();
579             }
580 0           general2();
581            
582 0 0         if ($default{'text'})
583 0           { my $siffra = substr($value, 0, 1);
584 0           my $del1 = substr($value, 1, 6);
585 0           my $del2 = substr($value, 7, 1);
586            
587 0           my @vec = prFont('C');
588            
589 0           prFontSize(10);
590            
591 0           prText(2, 2, $siffra);
592 0           prText(14, 2, $del1);
593 0           prText(58, 2, $del2);
594            
595 0           prFont($vec[3]);
596             }
597 0           general3;
598 0           1;
599             }
600            
601            
602             1;
603            
604            
605             __END__