File Coverage

blib/lib/PDF/API3/Compat/API2/Resource/XObject/Form/BarCode/code128.pm
Criterion Covered Total %
statement 20 97 20.6
branch 0 46 0.0
condition n/a
subroutine 7 13 53.8
pod 1 6 16.6
total 28 162 17.2


line stmt bran cond sub pod time code
1             #=======================================================================
2             # ____ ____ _____ _ ____ ___ ____
3             # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
4             # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
5             # | __/| |_| | _| _ _ / ___ \| __/| | / __/
6             # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
7             #
8             # A Perl Module Chain to faciliate the Creation and Modification
9             # of High-Quality "Portable Document Format (PDF)" Files.
10             #
11             # Copyright 1999-2005 Alfred Reibenschuh .
12             #
13             #=======================================================================
14             #
15             # This library is free software; you can redistribute it and/or
16             # modify it under the terms of the GNU Lesser General Public
17             # License as published by the Free Software Foundation; either
18             # version 2 of the License, or (at your option) any later version.
19             #
20             # This library is distributed in the hope that it will be useful,
21             # but WITHOUT ANY WARRANTY; without even the implied warranty of
22             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23             # Lesser General Public License for more details.
24             #
25             # You should have received a copy of the GNU Lesser General Public
26             # License along with this library; if not, write to the
27             # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28             # Boston, MA 02111-1307, USA.
29             #
30             # $Id: code128.pm,v 2.0 2005/11/16 02:18:23 areibens Exp $
31             #
32             #=======================================================================
33             package PDF::API3::Compat::API2::Resource::XObject::Form::BarCode::code128;
34            
35             BEGIN {
36            
37 1     1   9 use PDF::API3::Compat::API2::Util;
  1         2  
  1         301  
38 1     1   9 use PDF::API3::Compat::API2::Basic::PDF::Utils;
  1         2  
  1         182  
39 1     1   8 use PDF::API3::Compat::API2::Resource::XObject::Form::BarCode;
  1         3  
  1         25  
40            
41 1     1   7 use POSIX;
  1         3  
  1         10  
42            
43 1     1   3864 use vars qw(@ISA $VERSION);
  1         4  
  1         178  
44            
45 1     1   47 @ISA = qw( PDF::API3::Compat::API2::Resource::XObject::Form::BarCode );
46            
47 1         45 ( $VERSION ) = sprintf '%i.%03i', split(/\./,('$Revision: 2.0 $' =~ /Revision: (\S+)\s/)[0]); # $Date: 2005/11/16 02:18:23 $
48            
49             }
50 1     1   7 no warnings qw[ deprecated recursion uninitialized ];
  1         3  
  1         1847  
51            
52             =item $res = PDF::API3::Compat::API2::Resource::XObject::Form::BarCode::code128->new $pdf, %opts
53            
54             Returns a code128 object. Use '-ean' to encode using EAN128 mode.
55            
56             =cut
57            
58             sub new {
59 0     0 1   my ($class,$pdf,%opts) = @_;
60 0           my $self;
61            
62 0 0         $class = ref $class if ref $class;
63            
64 0           $self=$class->SUPER::new($pdf,%opts);
65            
66 0 0         my @bar = $opts{-ean} ? $self->encode_ean128($opts{-code}) : $self->encode_128($opts{-type},$opts{-code});
67            
68 0           $self->drawbar([@bar]);
69            
70 0           return($self);
71             }
72            
73            
74             my $code128a=q| !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_|.join('',map{chr($_)}(0..31)).qq/\xf3\xf2\x80\xcc\xcb\xf4\xf1\x8a\x8b\x8c\xff/;
75             my $code128b=q| !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|.qq/|}~\x7f\xf3\xf2\x80\xcc\xf4\xca\xf1\x8a\x8b\x8c\xff/;
76             my $code128c=("\xfe" x 100).qq/\xcb\xca\xf1\x8a\x8b\x8c\xff/;
77            
78             my @bar128=qw(
79             212222 222122 222221 121223 121322
80             131222 122213 122312 132212 221213
81             221312 231212 112232 122132 122231
82             113222 123122 123221 223211 221132
83             221231 213212 223112 312131 311222
84             321122 321221 312212 322112 322211
85             212123 212321 232121 111323 131123
86             131321 112313 132113 132311 211313
87             231113 231311 112133 112331 132131
88             113123 113321 133121 313121 211331
89             231131 213113 213311 213131 311123
90             311321 331121 312113 312311 332111
91             314111 221411 431111 111224 111422
92             121124 121421 141122 141221 112214
93             112412 122114 122411 142112 142211
94             241211 221114 413111 241112 134111
95             111242 121142 121241 114212 124112
96             124211 411212 421112 421211 212141
97             214121 412121 111143 111341 131141
98             114113 114311 411113 411311 113141
99             114131 311141 411131 b1a4a2 b1a2a4
100             b1a2c2 b3c1a1b
101             );
102            
103             my $bar128F1="\xf1";
104             my $bar128F2="\xf2";
105             my $bar128F3="\xf3";
106             my $bar128F4="\xf4";
107            
108             my $bar128Ca="\xca";
109             my $bar128Cb="\xcb";
110             my $bar128Cc="\xcc";
111            
112             my $bar128sh="\x80";
113            
114             my $bar128Sa="\x8a";
115             my $bar128Sb="\x8b";
116             my $bar128Sc="\x8c";
117            
118             my $bar128St="\xff";
119            
120             sub encode_128_char_idx {
121 0     0 0   my ($code,$char)=@_;
122 0           my ($idx);
123 0 0         if(lc($code) eq 'a') {
    0          
    0          
124 0 0         return if($char eq $bar128Ca);
125 0           $idx=index($code128a,$char);
126             } elsif(lc($code) eq 'b') {
127 0 0         return if($char eq $bar128Cb);
128 0           $idx=index($code128b,$char);
129             } elsif(lc($code) eq 'c') {
130 0 0         return if($char eq $bar128Cc);
131 0 0         if($char=~/^\d+$/) {
132 0           $idx=substr($char,0,1)*10+substr($char,1,1)*1;
133             } else {
134 0           $idx=index($code128c,$char);
135             }
136             }
137 0           return($bar128[$idx],$idx);
138             }
139            
140             sub encode_128_char {
141 0     0 0   my ($code,$char)=@_;
142 0           my ($b)=encode_128_char_idx($code,$char);
143 0           return($b);
144             }
145            
146             sub encode_128_string {
147 0     0 0   my ($code,$str)=@_;
148 0           my ($bar,@chk,$c,$desc,$b,$i,@bars);
149 0           my @chars=split(//,$str);
150 0           while(defined($c=shift @chars)) {
151 0 0         if($c=~/[\xf1-\xf4]/) {
    0          
152 0           ($b,$i)=encode_128_char_idx($code,$c);
153             } elsif($c=~/[\xca-\xcc]/) {
154 0           ($b,$i)=encode_128_char_idx($code,$c);
155 0 0         if($c eq "\xca") {
    0          
    0          
156 0           $code='a';
157             } elsif($c eq "\xcb") {
158 0           $code='b';
159             } elsif($c eq "\xcc") {
160 0           $code='c';
161             }
162             } else {
163 0 0         if($code ne 'c') {
164 0 0         if($c eq $bar128sh) {
165 0           ($b,$i)=encode_128_char_idx($code,$c);
166 0           push(@bars,$b);
167 0           push(@chk,$i);
168 0           $c=shift(@chars);
169 0 0         ($b,$i)=encode_128_char_idx($code eq 'a' ? 'b':'a',$c);
170             } else {
171 0           ($b,$i)=encode_128_char_idx($code,$c);
172             }
173             } else {
174 0 0         $c.=shift(@chars) if($c=~/\d/);
175 0 0         if($c=~/^\d[^\d]*$/) {
176 0           ($b,$i)=encode_128_char_idx($code,"\xcb");
177 0           push(@bars,$b);
178 0           push(@chk,$i);
179 0           $code='b';
180 0           unshift(@chars,substr($c,1,1));
181 0           $c=substr($c,0,1);
182             }
183 0           ($b,$i)=encode_128_char_idx($code,$c);
184             }
185             }
186 0 0         $c='' if($c=~/[^\x20-\x7e]/);
187 0           push(@bars,[$b,$c]);
188 0           push(@chk,$i);
189             }
190 0           return([@bars],@chk);
191             }
192            
193             sub encode_128 {
194 0     0 0   my ($self,$code,$str)=@_;
195 0           my (@bar,$b,@chk,$c);
196 0 0         if($code eq 'a') {
    0          
    0          
197 0           push(@bar,encode_128_char($code,$bar128Sa));
198 0           $c=103;
199             } elsif($code eq 'b') {
200 0           push(@bar,encode_128_char($code,$bar128Sb));
201 0           $c=104;
202             } elsif($code eq 'c') {
203 0           push(@bar,encode_128_char($code,$bar128Sc));
204 0           $c=105;
205             }
206 0           ($b,@chk)=encode_128_string($code,$str);
207             # b ... bars
208             # chk ... chknums
209 0           push(@bar,@{$b});
  0            
210             #calc chksum
211 0           foreach my $i (1..scalar @chk) {
212 0           $c+=$i*$chk[$i-1];
213             }
214 0           $c%=103;
215 0           push(@bar,$bar128[$c]);
216 0           push(@bar,encode_128_char($code,$bar128St));
217 0           return(@bar);
218             }
219            
220             sub encode_ean128 {
221 0     0 0   my ($self,$str)=@_;
222 0           $str=~s/[^a-zA-Z\d]+//g;
223 0           $str=~s/(\d+)([a-zA-Z]+)/$1\xcb$2/g;
224 0           $str=~s/([a-zA-Z]+)(\d+)/$1\xcc$2/g;
225 0           return(encode_128('c',"\xf1$str"));
226             }
227            
228             1;
229            
230             __END__