API3/Compat/API2/Resource/XObject/Form/BarCode/code128.pm

filter: Statementstmt 33.0% 38 / 115 Branchbran 0.0% 0 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 53.8% 7 / 13 Podpod 16.6% 1 / 6 totaltotal 25.2% 46 / 182 CC 26 SCAR 44.1
CC
26
Cov
23%
encode_128_string
182.0
encode_128_char_idx
72.0
encode_128
30.0
CRAP 82.6
« API3/Compat/API2/Basic/TTF/Post.pm ↑ Summary API3/Compat/API2/Outline.pm »
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 <areibens@cpan.org>.
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 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
36
37 1 ▶ use PDF::API3::Compat::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶ use PDF::API3::Compat::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶ use PDF::API3::Compat::API2::Resource::XObject::Form::BarCode;
Subroutine BEGIN: called CC=1 SCAR=0.0
40
41 1 ▶ use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
42
43 1 ▶ use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
44
45 1 @ISA = qw( PDF::API3::Compat::API2::Resource::XObject::Form::BarCode );
46
47 1 ( $VERSION ) = sprintf '%i.%03i', split(/\./,('$Revision: 2.0 $' =~ /Revision: (\S+)\s/)[0]); # $Date: 2005/11/16 02:18:23 $
48
49 }
50 1 ▶no warnings qw[ deprecated recursion uninitialized ];
Subroutine BEGIN: called CC=1 SCAR=0.0
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 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
59 0 my ($class,$pdf,%opts) = @_;
60 0 my $self;
61
62 0 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
63
64 0 $self=$class->SUPER::new($pdf,%opts);
65
66 0 ▶ my @bar = $opts{-ean} ? $self->encode_ean128($opts{-code}) : $self->encode_128($opts{-type},$opts{-code});
Branch: 1 branch
BranchTrueFalseTotal
$opts{'-ean'} ? : 0 0 0
67
68 0 $self->drawbar([@bar]);
69
70 0 return($self);
71 }
72
73
74 1 my $code128a=q| !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_|.join('',map{chr($_)}(0..31)).qq/\xf3\xf2\x80\xcc\xcb\xf4\xf1\x8a\x8b\x8c\xff/;
75 1 my $code128b=q| !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|.qq/|}~\x7f\xf3\xf2\x80\xcc\xf4\xca\xf1\x8a\x8b\x8c\xff/;
76 1 my $code128c=("\xfe" x 100).qq/\xcb\xca\xf1\x8a\x8b\x8c\xff/;
77
78 1 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 1 my $bar128F1="\xf1";
104 1 my $bar128F2="\xf2";
105 1 my $bar128F3="\xf3";
106 1 my $bar128F4="\xf4";
107
108 1 my $bar128Ca="\xca";
109 1 my $bar128Cb="\xcb";
110 1 my $bar128Cc="\xcc";
111
112 1 my $bar128sh="\x80";
113
114 1 my $bar128Sa="\x8a";
115 1 my $bar128Sb="\x8b";
116 1 my $bar128Sc="\x8c";
117
118 1 my $bar128St="\xff";
119
120 0 ▶sub encode_128_char_idx {
Subroutine encode_128_char_idx: not called CC=8 SCAR=42.8
Pod undocumented
121 0 my ($code,$char)=@_;
122 0 my ($idx);
123 0 ▶ if(lc($code) eq 'a') {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $code eq 'a') { } 0 0 0
elsif (lc $code eq 'b') { } 0 0 0
elsif (lc $code eq 'c') { } 0 0 0
124 0 ▶ return if($char eq $bar128Ca);
Branch: 1 branch
BranchTrueFalseTotal
if $char eq $bar128Ca 0 0 0
125 0 $idx=index($code128a,$char);
126 } elsif(lc($code) eq 'b') {
127 0 ▶ return if($char eq $bar128Cb);
Branch: 1 branch
BranchTrueFalseTotal
if $char eq $bar128Cb 0 0 0
128 0 $idx=index($code128b,$char);
129 } elsif(lc($code) eq 'c') {
130 0 ▶ return if($char eq $bar128Cc);
Branch: 1 branch
BranchTrueFalseTotal
if $char eq $bar128Cc 0 0 0
131 0 ▶ if($char=~/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /^\d+$/) { } 0 0 0
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 0 ▶sub encode_128_char {
Subroutine encode_128_char: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my ($code,$char)=@_;
142 0 my ($b)=encode_128_char_idx($code,$char);
143 0 return($b);
144 }
145
146 0 ▶sub encode_128_string {
Subroutine encode_128_string: not called CC=13 SCAR=52.0
Pod undocumented
147 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)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($c = shift @chars)) 0 0 0
151 0 ▶ if($c=~/[\xf1-\xf4]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c =~ /[\xf1-\xf4]/) { } 0 0 0
elsif ($c =~ /[\xca-\xcc]/) { } 0 0 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 ▶ if($c eq "\xca") {
Branch: 3 branches
BranchTrueFalseTotal
if ($c eq "\312") { } 0 0 0
elsif ($c eq "\313") { } 0 0 0
elsif ($c eq "\314") { } 0 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 ▶ if($code ne 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 'c') { } 0 0 0
164 0 ▶ if($c eq $bar128sh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq $bar128sh) { } 0 0 0
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 ▶ ($b,$i)=encode_128_char_idx($code eq 'a' ? 'b':'a',$c);
Branch: 1 branch
BranchTrueFalseTotal
$code eq 'a' ? : 0 0 0
170 } else {
171 0 ($b,$i)=encode_128_char_idx($code,$c);
172 }
173 } else {
174 0 ▶ $c.=shift(@chars) if($c=~/\d/);
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /\d/ 0 0 0
175 0 ▶ if($c=~/^\d[^\d]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^\d[^\d]*$/) 0 0 0
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 ▶ $c='' if($c=~/[^\x20-\x7e]/);
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /[^\x20-\x7e]/ 0 0 0
187 0 push(@bars,[$b,$c]);
188 0 push(@chk,$i);
189 }
190 0 return([@bars],@chk);
191 }
192
193 0 ▶sub encode_128 {
Subroutine encode_128: not called CC=5 SCAR=34.0
Pod undocumented
194 0 my ($self,$code,$str)=@_;
195 0 my (@bar,$b,@chk,$c);
196 0 ▶ if($code eq 'a') {
Branch: 3 branches
BranchTrueFalseTotal
if ($code eq 'a') { } 0 0 0
elsif ($code eq 'b') { } 0 0 0
elsif ($code eq 'c') { } 0 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});
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 0 ▶sub encode_ean128 {
Subroutine encode_ean128: not called CC=1 SCAR=6.9
Pod undocumented
221 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 1;
229
230 __END__
« API3/Compat/API2/Basic/TTF/Post.pm ↑ Summary API3/Compat/API2/Outline.pm »