summaryrefslogtreecommitdiff
path: root/lib/PDF/Builder/Resource/XObject/Form/BarCode/code128.pm
blob: 1d582ae2282096c59ed4c71a681bd7ab0802edca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package PDF::Builder::Resource::XObject::Form::BarCode::code128;

use base 'PDF::Builder::Resource::XObject::Form::BarCode';

use strict;
use warnings;

our $VERSION = '3.025'; # VERSION
our $LAST_UPDATE = '3.024'; # manually update whenever code is changed

=head1 NAME

PDF::Builder::Resource::XObject::Form::BarCode::code128 - Code 128 and EAN-128 barcode support

=head1 METHODS

=over

=item $res = PDF::Builder::Resource::XObject::Form::BarCode::code128->new($pdf, %options)

Returns a code128 object. Use 'ean' option to encode using EAN128 mode.

=back

=cut

sub new {
    my ($class, $pdf, %options) = @_;
    # copy dashed option names to preferred undashed names
    if (defined $options{'-ean'} && !defined $options{'ean'}) { $options{'ean'} = delete($options{'-ean'}); }
    if (defined $options{'-code'} && !defined $options{'code'}) { $options{'code'} = delete($options{'-code'}); }
    if (defined $options{'-type'} && !defined $options{'type'}) { $options{'type'} = delete($options{'-type'}); }

    $class = ref($class) if ref($class);

    my $self = $class->SUPER::new($pdf, %options);

    my @bars;
    if ($options{'ean'}) {
        @bars = $self->encode_ean128($options{'code'});
    } else {
        @bars = $self->encode_128($options{'type'}, $options{'code'});
    }

    $self->drawbar(\@bars, $options{'caption'});

    return $self;
}

# CODE-A Encoding Table
my $code128a = q| !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_| . join('', map { chr($_) } (0..31)) . qq/\xf3\xf2\x80\xcc\xcb\xf4\xf1\x8a\x8b\x8c\xff/;

# CODE-B Encoding Table
my $code128b = q| !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|.qq/|}~\x7f\xf3\xf2\x80\xcc\xf4\xca\xf1\x8a\x8b\x8c\xff/;

# CODE-C Encoding Table (00-99 are placeholders)
my $code128c = ("\xfe" x 100) . qq/\xcb\xca\xf1\x8a\x8b\x8c\xff/;

# START A-C
my $bar128Sa = "\x8a";
my $bar128Sb = "\x8b";
my $bar128Sc = "\x8c";

# FNC1-FNC4
my $bar128F1 = "\xf1";
my $bar128F2 = "\xf2";
my $bar128F3 = "\xf3";
my $bar128F4 = "\xf4";

# CODE A-C
my $bar128Ca = "\xca";
my $bar128Cb = "\xcb";
my $bar128Cc = "\xcc";

# SHIFT
my $bar128sh = "\x80";

# STOP
my $bar128St = "\xff";

# Note: The stop code (last position) is longer than the other codes because 
# it also has the termination bar appended, rather than requiring it be added 
# as a separate call.
my @bar128 = qw(
    212222 222122 222221 121223 121322 131222 122213 122312 132212 221213
    221312 231212 112232 122132 122231 113222 123122 123221 223211 221132
    221231 213212 223112 312131 311222 321122 321221 312212 322112 322211
    212123 212321 232121 111323 131123 131321 112313 132113 132311 211313
    231113 231311 112133 112331 132131 113123 113321 133121 313121 211331
    231131 213113 213311 213131 311123 311321 331121 312113 312311 332111
    314111 221411 431111 111224 111422 121124 121421 141122 141221 112214
    112412 122114 122411 142112 142211 241211 221114 413111 241112 134111
    111242 121142 121241 114212 124112 124211 411212 421112 421211 212141
    214121 412121 111143 111341 131141 114113 114311 411113 411311 113141
    114131 311141 411131 b1a4a2 b1a2a4 b1a2c2 b3c1a1b
);

sub encode_128_char_idx {
    my ($code, $char) = @_;

    my $index;

    if      (lc($code) eq 'a') {
        # Ignore CODE-A request if we're already in CODE-A
        return if $char eq $bar128Ca;

        $index = index($code128a, $char);
    } elsif (lc($code) eq 'b') {
        # Ignore CODE-B request if we're already in CODE-B
        return if $char eq $bar128Cb;
        $index = index($code128b, $char);
    } elsif (lc($code) eq 'c') {
        # Ignore CODE-C request if we're already in CODE-C
        return if $char eq $bar128Cc;

        if ($char =~ /^([0-9][0-9])$/) {
            $index = $1;
        } else {
            $index = index($code128c, $char);
        }
    }

    return ($bar128[$index], $index);
}

sub encode_128_char {
    my ($code, $char) = @_;

    my ($b) = encode_128_char_idx($code, $char);
    return $b;
}

sub encode_128_string {
    my ($code, $string) = @_;

    my ($bar, $index, @bars, @checksum);
    my @characters = split(//, $string);

    my $character;
    while (defined($character = shift @characters)) {
        if      ($character =~ /[\xf1-\xf4]/) {
            # CODE-C doesn't have FNC2-FNC4
            if ($character =~ /[\xf2-\xf4]/ and $code eq 'c') {
                ($bar, $index) = encode_128_char_idx($code, "\xCB");
                push @bars, $bar;
                push @checksum, $index;
                $code = 'b';
            }

            ($bar, $index) = encode_128_char_idx($code, $character);
        } elsif ($character =~ /[\xCA-\xCC]/) {
            ($bar, $index) = encode_128_char_idx($code, $character);
            $code = ($character eq "\xCA"? 'a':
                     $character eq "\xCB"? 'b': 'c');
        } else {
            if ($code ne 'c') {
                # SHIFT: Switch codes for the following character only
                if ($character eq $bar128sh) {
                    ($bar, $index) = encode_128_char_idx($code, $character);
                    push @bars, $bar;
                    push @checksum, $index;
                    $character = shift(@characters);
                    ($bar, $index) = encode_128_char_idx($code eq 'a'? 'b': 'a', $character);
                } else {
                    ($bar, $index) = encode_128_char_idx($code, $character);
                }
            } else {
                $character .= shift(@characters) if $character =~ /\d/ and scalar @characters;
                if ($character =~ /^[^\d]*$/ or $character =~ /^\d[^\d]*$/) {
                    ($bar, $index) = encode_128_char_idx($code, "\xCB");
                    push @bars, $bar;
                    push @checksum, $index;
                    $code = 'b';
                }
                if ($character =~ /^\d[^\d]*$/) {
                    unshift(@characters, substr($character, 1, 1)) if length($character) > 1;
                    $character = substr($character, 0, 1);
                }
                ($bar, $index) = encode_128_char_idx($code, $character);
            }
        }
        $character = '' if $character =~ /[^\x20-\x7e]/;
        push @bars, [$bar, $character];
        push @checksum, $index;
    }
    return ([@bars], @checksum);
}

sub encode_128 {
    my ($self, $code, $string) = @_;
    my @bars;
    my $checksum_value;

    # Default to Code C if all characters are digits (and there are at
    # least two of them).  Otherwise, default to Code B.
    $code ||= $string =~ /^\d{2,}$/? 'c': 'b';

    # Allow the character set to be passed as a capital letter
    # (consistent with the specification).
    $code = lc($code) if $code =~ /^[A-C]$/;

    # Ensure a valid character set has been chosen.
    die "Character set must be A, B, or C (not '$code')" unless $code =~ /^[a-c]$/;

    if      ($code eq 'a') {
        push @bars, encode_128_char($code, $bar128Sa);
        $checksum_value = 103;
    } elsif ($code eq 'b') {
        push @bars, encode_128_char($code, $bar128Sb);
        $checksum_value = 104;
    } elsif ($code eq 'c') {
        push @bars, encode_128_char($code, $bar128Sc);
        $checksum_value = 105;
    }
    my ($bar, @checksum_values) = encode_128_string($code, $string);

    push @bars, @{$bar};

    # Calculate the checksum value
    foreach my $i (1 .. scalar @checksum_values) {
        $checksum_value += $i * $checksum_values[$i - 1];
    }
    $checksum_value %= 103;
    push @bars, $bar128[$checksum_value];
    push @bars, encode_128_char($code, $bar128St);

    return @bars;
}

sub encode_ean128 {
    my ($self, $string) = @_;

    $string =~ s/[^a-zA-Z\d]+//g;
    $string =~ s/(\d+)([a-zA-Z]+)/$1\xcb$2/g;
    $string =~ s/([a-zA-Z]+)(\d+)/$1\xcc$2/g;

    return $self->encode_128('c', "\xf1$string");
}

1;