File Coverage

blib/lib/PDF/Builder/Resource/XObject/Form/BarCode/ean13.pm
Criterion Covered Total %
statement 32 42 76.1
branch 3 8 37.5
condition 1 3 33.3
subroutine 5 6 83.3
pod 1 3 33.3
total 42 62 67.7


line stmt bran cond sub pod time code
1             package PDF::Builder::Resource::XObject::Form::BarCode::ean13;
2              
3 2     2   895 use base 'PDF::Builder::Resource::XObject::Form::BarCode';
  2         3  
  2         184  
4              
5 2     2   12 use strict;
  2         4  
  2         36  
6 2     2   9 use warnings;
  2         3  
  2         950  
7              
8             our $VERSION = '3.024'; # VERSION
9             our $LAST_UPDATE = '3.024'; # manually update whenever code is changed
10              
11             =head1 NAME
12              
13             PDF::Builder::Resource::XObject::Form::BarCode::ean13 - specific information for EAN-13 bar codes. Inherits from L
14              
15             =cut
16              
17             sub new {
18 1     1 1 4 my ($class, $pdf, %options) = @_;
19             # copy dashed option names to preferred undashed names
20 1 50 33     6 if (defined $options{'-code'} && !defined $options{'code'}) { $options{'code'} = delete($options{'-code'}); }
  1         4  
21              
22 1         7 my $self = $class->SUPER::new($pdf, %options);
23              
24 1         3 my @bars = $self->encode($options{'code'});
25              
26 1         17 $self->drawbar([@bars], $options{'caption'});
27              
28 1         7 return $self;
29             }
30              
31             my @ean_code_odd = qw(3211 2221 2122 1411 1132 1231 1114 1312 1213 3112);
32             my @ean_code_even = qw(1123 1222 2212 1141 2311 1321 4111 2131 3121 2113);
33             my @parity = qw(OOOOOO OOEOEE OOEEOE OOEEEO OEOOEE OEEOOE OEEEOO OEOEOE OEOEEO OEEOEO);
34              
35             sub encode {
36 11     11 0 4669 my ($self, $string) = @_;
37              
38 11         41 my @digits = split(//, $string);
39              
40             # The first digit determines the even/odd pattern of the next six
41             # digits, and is printed to the left of the barcode
42 11         22 my $first = shift(@digits);
43 11         36 my @bars = (['07', $first]);
44              
45             # Start Guard
46 11         19 push @bars, 'a1a';
47              
48             # Digits 2-7
49 11         24 foreach my $i (0 .. 5) {
50 66         82 my $digit = shift(@digits);
51 66 100       114 if (substr($parity[$first], $i, 1) eq 'O') {
52 39         91 push @bars, [$ean_code_odd[$digit], $digit];
53             } else {
54 27         57 push @bars, [$ean_code_even[$digit], $digit];
55             }
56             }
57              
58             # Center Guard
59 11         18 push @bars, '1a1a1';
60              
61             # Digits 8-13
62 11         20 for (0..5) {
63 66         86 my $digit = shift @digits;
64 66         107 push @bars, [$ean_code_odd[$digit], $digit];
65             }
66              
67             # Right Guard
68 11         17 push @bars, 'a1a';
69              
70 11         35 return @bars;
71             }
72              
73             sub calculate_check_digit {
74 0     0 0   my ($self, $string) = @_;
75              
76 0           my @digits = split(//, $string);
77 0           my $weight = 1;
78 0           my $checksum = 0;
79 0           foreach my $i (0..11) {
80 0           $checksum += $digits[$i] * $weight;
81 0 0         $weight = $weight == 1? 3: 1;
82             }
83              
84 0           $checksum = $checksum % 10;
85 0 0         return 0 unless $checksum;
86 0           return 10 - $checksum;
87             }
88              
89             1;