| 1 |
# your shell command goes here |
| 2 |
# |
| 3 |
# Hacking EAN numbers to a form usable with WLEAN.MF. |
| 4 |
# |
| 5 |
# September 3, 1997 |
| 6 |
# Peter Willadt |
| 7 |
# |
| 8 |
# Added hacking any text for coding with code 128 |
| 9 |
# 1998-01-24 |
| 10 |
# |
| 11 |
# Added checksumming for code 93 |
| 12 |
# 1998-11-29 |
| 13 |
# |
| 14 |
# 2003-05-24 |
| 15 |
# cleaned up the code in several places and |
| 16 |
# corrected code 93 checksumming due to a hint by |
| 17 |
# Jacek Ruzyczka <[email protected]> |
| 18 |
# |
| 19 |
# 2003-05-24 |
| 20 |
# removed an error introduced the same day |
| 21 |
# |
| 22 |
# This file is free to use without any further permissions. |
| 23 |
# This file comes with no warranty of any kind. |
| 24 |
# |
| 25 |
# The TeX file to be filtered may contain any number of lines |
| 26 |
# that have one of the following commands |
| 27 |
# starting at the leftmost position. |
| 28 |
# |
| 29 |
# \ean{12 or 13 digit number} |
| 30 |
# the number gets coded as EAN, |
| 31 |
# if it is only 12 digits long, the checksum gets calculated |
| 32 |
# |
| 33 |
# \embed{number} |
| 34 |
# the number is used as a base for embedding article numbers & c. |
| 35 |
# \eean{number} |
| 36 |
# a number to be embedded with an ean. |
| 37 |
# |
| 38 |
# \isbn{number} |
| 39 |
# an isbn to make an embedded ean of. |
| 40 |
# |
| 41 |
# \cxxviii{any 7 bit ascii values} |
| 42 |
# code as barcode 128 (see rules below!) |
| 43 |
# |
| 44 |
# \xciii{uppercase text or number} |
| 45 |
# text to be coded as code 128 |
| 46 |
# |
| 47 |
# example: |
| 48 |
# You want the isbn 0-201-13448-9 to be embedded. |
| 49 |
# so you say \isbn{0201134489}, |
| 50 |
# but you may also say \embed{9780000000000} and, |
| 51 |
# somewhere later in the file, \eean{020113448} |
| 52 |
# In this case you have to leave the last digit out, |
| 53 |
# as isbn loose their check digit in favour of the |
| 54 |
# ean check digit. |
| 55 |
# anyway you do it, you get your command replaced by |
| 56 |
# \EAN{13-digit-number-coded-strange} |
| 57 |
# in the output file. |
| 58 |
# |
| 59 |
# |
| 60 |
# code 128 rules: |
| 61 |
# you write a line starting with \cxxviii{ |
| 62 |
# followed by arbitrary 7 bit characters, delimited by a right brace}. |
| 63 |
# as perl is greedy, it will be the rightmost right brace (no fence matching), |
| 64 |
# but as perl is also nice, you will be warned if there is another |
| 65 |
# right brace. Please note that even the percent character % will be |
| 66 |
# included. So it is better to write the \cxxviii{...} statement onto |
| 67 |
# a line of its own. You may replace any character by ^^00 and |
| 68 |
# similiar codes, preferably you will do this to non-printable ascii |
| 69 |
# characters, or right braces and the like. This routine will try to find |
| 70 |
# an efficent way to make code 128 (sorry, not necessarily the most |
| 71 |
# efficient way) out of your input and then it will |
| 72 |
# insert a line like \CXXVIII{3a 70 12 ... @@} in the output file. |
| 73 |
# The code 128 special characters can be included by the following codes: |
| 74 |
# ^^80 FNC3 |
| 75 |
# ^^81 FNC2 |
| 76 |
# ^^82 SHIFT |
| 77 |
# ^^83 CODE C/CODE C/99 |
| 78 |
# ^^84 CODE B/FNC4/CODE B |
| 79 |
# ^^85 FNC4/CODE A/CODE A |
| 80 |
# ^^86 FNC1 |
| 81 |
# ^^87 START A |
| 82 |
# ^^88 START B |
| 83 |
# ^^89 START C |
| 84 |
# ^^8a STOP |
| 85 |
|
| 86 |
use strict; |
| 87 |
|
| 88 |
use vars qw ( @ABTAB $ifname $ofname ); |
| 89 |
# code switch table for ean |
| 90 |
|
| 91 |
@ABTAB=(0,0,0,0,0,0, #0 |
| 92 |
0,0,1,0,1,1, #1 |
| 93 |
0,0,1,1,0,1, #2 |
| 94 |
0,0,1,1,1,0, #3 |
| 95 |
0,1,0,0,1,1, #4 and so on |
| 96 |
0,1,1,0,0,1, |
| 97 |
0,1,1,1,0,0, |
| 98 |
0,1,0,1,0,1, |
| 99 |
0,1,0,1,1,0, |
| 100 |
0,1,1,0,1,0, |
| 101 |
); |
| 102 |
|
| 103 |
# command line processing: Need input file |
| 104 |
|
| 105 |
if($ARGV[0]){ |
| 106 |
$ifname=$ARGV[0]; |
| 107 |
}else{ |
| 108 |
print "Enter name of file to be processed: "; |
| 109 |
$ifname = <>; |
| 110 |
chomp($ifname); |
| 111 |
} |
| 112 |
|
| 113 |
# command line processing: need output file |
| 114 |
|
| 115 |
if($ARGV[1]){ |
| 116 |
$ofname=">$ARGV[1]"; |
| 117 |
}else{ |
| 118 |
print "Enter name of output file: "; |
| 119 |
$ofname = <>; |
| 120 |
chomp($ofname); |
| 121 |
$ofname = '>' . $ofname; |
| 122 |
} |
| 123 |
|
| 124 |
# make an ean |
| 125 |
|
| 126 |
sub eancod{ |
| 127 |
my ($i, $j, $checksum, $disone); |
| 128 |
my $srcstr=shift; |
| 129 |
# first digits first |
| 130 |
my $precod=substr($srcstr,0,1); |
| 131 |
# Starting output string |
| 132 |
my $eastring=$precod . " +"; |
| 133 |
# digits 2--7 |
| 134 |
for($i=0;$i<6;$i++){ |
| 135 |
my $disone=substr($srcstr,$i+1,1); |
| 136 |
$disone =~ tr/0123456789/ABCDEFGHIJ/; |
| 137 |
$disone= lc ($disone) if( @ABTAB[$precod*6 + $i]==1); |
| 138 |
$eastring .=$disone; |
| 139 |
}$eastring .= "-"; |
| 140 |
# digits 8--13 |
| 141 |
for($i=0;$i<6;$i++){ |
| 142 |
# if checksum misses, do your own |
| 143 |
if(($i==5) && (length($srcstr)==12)){ |
| 144 |
for($j=0,$checksum=0;$j<12;$j++){ |
| 145 |
$checksum+=substr($srcstr,$j,1)*(1+($j&1)*2); |
| 146 |
}; |
| 147 |
$checksum%=10; |
| 148 |
$checksum=10-$checksum; |
| 149 |
$checksum%=10; |
| 150 |
$disone="$checksum"; |
| 151 |
}else { |
| 152 |
$disone=substr($srcstr,$i+7,1); |
| 153 |
} |
| 154 |
$disone =~ tr/0123456789/KLMNOPQRST/; |
| 155 |
$eastring .=$disone; |
| 156 |
}$eastring .="+"; |
| 157 |
return $eastring; |
| 158 |
} |
| 159 |
|
| 160 |
################################################## |
| 161 |
# here starts the code 128 stuff |
| 162 |
# |
| 163 |
################################################## |
| 164 |
# get the numerical value of a hex character, |
| 165 |
# e.g. 65 from 41 |
| 166 |
# |
| 167 |
sub hexchar{ |
| 168 |
my $src=shift; |
| 169 |
my ($i, $j, $result); |
| 170 |
$src =~ tr/a-f/A-F/; |
| 171 |
$i=ord(substr($src,0,1)); |
| 172 |
$j=ord(substr($src,1,1)); |
| 173 |
if($i >= ord("A")){ |
| 174 |
$i += (10-ord('A')); |
| 175 |
}else{ |
| 176 |
$i -= ord("0"); |
| 177 |
} |
| 178 |
if($j >= ord("A")){ |
| 179 |
$j += (10-ord("A")); |
| 180 |
}else{ |
| 181 |
$j -= ord("0"); |
| 182 |
} |
| 183 |
$result=16*($i)+$j; |
| 184 |
return $result; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
# globals: |
| 189 |
# @cxxchars holds the characters the user wants to code |
| 190 |
# @cxxlength is the size of this array |
| 191 |
# @ctbl holds the possible codings for these chars |
| 192 |
# @cxxout holds the codes to be output for code 128 |
| 193 |
# @cxxoutout holds the codes to be output for code 128 |
| 194 |
|
| 195 |
use vars qw ( @cxxchars $cxxlength @ctbl @cxxout @cxxoutout); |
| 196 |
|
| 197 |
################################################## |
| 198 |
# build up the switching table for code 128 |
| 199 |
|
| 200 |
sub makectbl{ |
| 201 |
# locals |
| 202 |
my $i; |
| 203 |
for($i=0;$i < $cxxlength; $i++){ |
| 204 |
if(($cxxchars[$i] >= ord("0"))&&($cxxchars[$i] <= ord("9"))){ |
| 205 |
# digits |
| 206 |
$ctbl[$i]=7; |
| 207 |
}elsif(($cxxchars[$i] >= ord(" "))&&($cxxchars[$i] <= ord("_"))){ |
| 208 |
# common Chars |
| 209 |
$ctbl[$i]=3; |
| 210 |
}elsif($cxxchars[$i] < ord(" ")){ |
| 211 |
# ascii control chars |
| 212 |
$ctbl[$i]=1; |
| 213 |
}elsif(($cxxchars[$i] >=ord("`"))&&($cxxchars[$i] <= ord("\x7f"))){ |
| 214 |
# lowercase |
| 215 |
$ctbl[$i]=2; |
| 216 |
if($cxxchars[$i] == ord("}")){ |
| 217 |
print "Encountered right brace in argument to cxxviii\n"; |
| 218 |
} |
| 219 |
}else{ # Function Codes |
| 220 |
$ctbl[$i] =7; |
| 221 |
} |
| 222 |
} |
| 223 |
$ctbl[$i]=0; # makes a stop. |
| 224 |
} |
| 225 |
|
| 226 |
################################################## |
| 227 |
# make a character array from a string |
| 228 |
# looking like aBc\x41def^^41 or so. |
| 229 |
# |
| 230 |
sub unhex{ |
| 231 |
my ($i, $j, $b); |
| 232 |
my $srcstr=shift; |
| 233 |
$j=0; |
| 234 |
for($i=0;($b=ord(substr($srcstr,$i,1))) > 0;$i++){ |
| 235 |
if($b == ord("\\")){ |
| 236 |
if(substr($srcstr,$i+1,1) =~ /[xX]/){ # hex input |
| 237 |
$cxxchars[$j] = hexchar(substr($srcstr,$i+2,2)); |
| 238 |
$i += 3; |
| 239 |
}else{ |
| 240 |
$cxxchars[$j] = ord("\\"); |
| 241 |
} |
| 242 |
}elsif($b == ord("^")){ |
| 243 |
if(ord(substr($srcstr,$i+1,1)) == ord("^")){ # hex input |
| 244 |
$cxxchars[$j] = hexchar(substr($srcstr,$i+2,2)); |
| 245 |
$i += 3; |
| 246 |
}else{ |
| 247 |
$cxxchars[$j] = ord("^"); |
| 248 |
} |
| 249 |
}else{ |
| 250 |
$cxxchars[$j] = $b; |
| 251 |
} |
| 252 |
$j++; |
| 253 |
} |
| 254 |
return $j; |
| 255 |
} |
| 256 |
|
| 257 |
################################################## |
| 258 |
# write out a chunk of code 128 in hex symbols |
| 259 |
# |
| 260 |
sub cxxchunk{ |
| 261 |
my $j=shift; |
| 262 |
my ($i, $sum, $k); |
| 263 |
$sum=7; |
| 264 |
for($i=0;$sum & $ctbl[$j+$i]; $i++){ |
| 265 |
$sum &= $ctbl[$j+$i]; |
| 266 |
} |
| 267 |
if($sum==1){ |
| 268 |
$cxxout[$j]=101; |
| 269 |
}else{ |
| 270 |
$cxxout[$j]=100; |
| 271 |
} |
| 272 |
for($k=0;$k<$i;$k++){ |
| 273 |
if(($sum==1)&&($cxxchars[$j+$k] < ord(" "))){ |
| 274 |
$cxxout[$j+$k+1]=$cxxchars[$j+$k] + 64; |
| 275 |
}else{ |
| 276 |
$cxxout[$j+$k+1]=$cxxchars[$j+$k] - ord(" "); |
| 277 |
} |
| 278 |
} |
| 279 |
$k++; |
| 280 |
return $k; |
| 281 |
} |
| 282 |
|
| 283 |
################################################## |
| 284 |
# consecutive digits may perhaps be efficiently |
| 285 |
# coded with charset C |
| 286 |
# |
| 287 |
sub pastedigits{ |
| 288 |
my $digitcount=shift; |
| 289 |
my $j=shift; |
| 290 |
my $firstdigit=shift; |
| 291 |
my $lastset=shift; |
| 292 |
my $k; |
| 293 |
if($digitcount==0){ # nothing to do |
| 294 |
return $j; |
| 295 |
}elsif($digitcount<4){ |
| 296 |
# digits here, but unfortunately not enough digits. |
| 297 |
# so just copy them |
| 298 |
for($k=0;$k<$digitcount;$k++){ |
| 299 |
$cxxoutout[$j] = $cxxout[$firstdigit+$k]; |
| 300 |
$j++; |
| 301 |
} |
| 302 |
return $j; |
| 303 |
}else{ |
| 304 |
# is there an odd number of digits? |
| 305 |
if(($digitcount & 1)==1){ |
| 306 |
$cxxoutout[$j] = $cxxout[$firstdigit]; |
| 307 |
$firstdigit++; |
| 308 |
$digitcount--; |
| 309 |
$j++; |
| 310 |
}elsif(($cxxout[$j-1]>=99)&&($cxxout[$j-1]<=101)){ |
| 311 |
# Switched immediately before digits. |
| 312 |
# so overwrite the switch |
| 313 |
$j--; |
| 314 |
} |
| 315 |
$cxxoutout[$j] = 99; # switch to set C |
| 316 |
$j++; |
| 317 |
# copy digits in compressed format |
| 318 |
for($k=0;$k<$digitcount;$k+=2){ |
| 319 |
$cxxoutout[$j] = ($cxxout[$firstdigit+$k]-16)*10 |
| 320 |
+$cxxout[$firstdigit+$k+1]-16; |
| 321 |
$j++; |
| 322 |
} |
| 323 |
# reset char set, if you have to |
| 324 |
if($lastset > 0){ |
| 325 |
$cxxoutout[$j] = $lastset; |
| 326 |
$j++; |
| 327 |
} |
| 328 |
} |
| 329 |
return $j; |
| 330 |
} |
| 331 |
|
| 332 |
sub digitoptimize{ |
| 333 |
# change to charset C if there are at least four numbers in a row. |
| 334 |
# copy to @cxxoutout |
| 335 |
my ($lastset, $firstdigit, $digitcount, $i,$j,$k); |
| 336 |
$lastset =0; |
| 337 |
$firstdigit=0; |
| 338 |
$digitcount=0; |
| 339 |
$j=0; |
| 340 |
for($i = 0; $i < $cxxlength; $i++){ |
| 341 |
if(($cxxout[$i]>=16)&&($cxxout[$i]<=25)){ |
| 342 |
# it's a number |
| 343 |
if($digitcount == 0){ |
| 344 |
$firstdigit=$i; |
| 345 |
} |
| 346 |
$digitcount++; |
| 347 |
next; |
| 348 |
} |
| 349 |
if($digitcount >0){ |
| 350 |
$j=pastedigits($digitcount,$j,$firstdigit,$lastset); |
| 351 |
$digitcount=0; |
| 352 |
} |
| 353 |
$cxxoutout[$j] = $cxxout[$i]; |
| 354 |
$j++; |
| 355 |
if(($cxxout[$i]>=99)&&($cxxout[$i]<=101)){ |
| 356 |
# it's a code switch |
| 357 |
$lastset=$cxxout[$i]; |
| 358 |
}else { |
| 359 |
$lastset=0; |
| 360 |
} |
| 361 |
} |
| 362 |
# $lastset is zero here, as there is no further need |
| 363 |
# to switch the char set any more - we're at the end |
| 364 |
$j=pastedigits($digitcount,$j,$firstdigit, 0); |
| 365 |
return $j; |
| 366 |
} |
| 367 |
|
| 368 |
################################################## |
| 369 |
# code 128 is a little complicated |
| 370 |
# if you have read till here, you already know. |
| 371 |
# |
| 372 |
sub codcxxviii{ |
| 373 |
# locals |
| 374 |
my ($i, $j, $sum); |
| 375 |
my $srcstr=shift; |
| 376 |
# first step: unhexing |
| 377 |
$cxxlength = unhex($srcstr); |
| 378 |
# @cxxchars now holds the characters the user wants |
| 379 |
makectbl(); |
| 380 |
# @ctbl now contains the possible tables for the chars in $j; |
| 381 |
for($i =0; $i < $cxxlength;){ |
| 382 |
$i += cxxchunk($i); |
| 383 |
} |
| 384 |
# change codeset switch to start |
| 385 |
$cxxlength=$i; |
| 386 |
$j=digitoptimize(); |
| 387 |
# Start symbols are different from switch symbols |
| 388 |
if($cxxoutout[0]==99){ |
| 389 |
$cxxoutout[0]=105; |
| 390 |
}elsif($cxxoutout[0]==100){ |
| 391 |
$cxxoutout[0]=104; |
| 392 |
}else{ |
| 393 |
$cxxoutout[0]=103; |
| 394 |
} |
| 395 |
# calculate checksum and build output string |
| 396 |
for($i=1,$sum=$cxxoutout[0];$i<$j;$i++){ |
| 397 |
$sum+=$cxxoutout[$i]*$i; |
| 398 |
} |
| 399 |
$sum %=103; |
| 400 |
$cxxoutout[$j]=$sum; |
| 401 |
$j++; |
| 402 |
$cxxoutout[$j]=106; #stop sign |
| 403 |
$srcstr=""; |
| 404 |
for($i=0;$i<($j+1);$i++){ |
| 405 |
$srcstr .= sprintf "%02X", $cxxoutout[$i]; |
| 406 |
} |
| 407 |
$srcstr .= "@@"; |
| 408 |
return $srcstr; |
| 409 |
} |
| 410 |
|
| 411 |
################################################## |
| 412 |
# do code 93 -- it's easy |
| 413 |
################################################## |
| 414 |
sub codxciii{ |
| 415 |
my $srcstr=shift; |
| 416 |
my $cstbl='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%()[]'; |
| 417 |
my ($i, $j, $sumc, $sumh, $checkchar); |
| 418 |
$sumc=$sumh=0; |
| 419 |
for ($i=0;$i<length($srcstr);$i++){ |
| 420 |
$j=index($cstbl, substr($srcstr,$i,1),0); |
| 421 |
# $j is the check value of the character. |
| 422 |
$sumh=$sumh+$j; |
| 423 |
$sumc=$sumc+$sumh; |
| 424 |
} |
| 425 |
$srcstr=$srcstr . substr($cstbl,$sumc%47,1); |
| 426 |
$sumc=$sumc+$sumh+($sumc%47); |
| 427 |
$checkchar=substr($cstbl,$sumc%47,1); |
| 428 |
$srcstr = $srcstr . $checkchar; |
| 429 |
$srcstr =~ s/\$/\\\$/g; |
| 430 |
$srcstr =~ s/%/\\%/g; |
| 431 |
$srcstr =~ s/ /\\char\'040/g; |
| 432 |
return $srcstr; |
| 433 |
} |
| 434 |
|
| 435 |
################################################## |
| 436 |
# we got both input and output file, |
| 437 |
# we defined all subroutines, |
| 438 |
# so here we go... |
| 439 |
# |
| 440 |
use vars qw ( $line $embedded $embtmp $mycod $eastring); |
| 441 |
|
| 442 |
open(EINGABE, $ifname) or die "No file"; |
| 443 |
open(AUSGABE, $ofname) or die "Can't open output file"; |
| 444 |
while($line=<EINGABE>){ |
| 445 |
# save 0.0005 (est.) % running time |
| 446 |
unless ($line =~ /^\\/){ |
| 447 |
print AUSGABE $line; |
| 448 |
next; |
| 449 |
} |
| 450 |
if($line =~ /^\\embed{(\d+)\}/){ |
| 451 |
$embedded=$1; |
| 452 |
print AUSGABE $line; |
| 453 |
}elsif($line =~ /^\\eean\{(\d+)\}(.*)/){ |
| 454 |
# embedded EAN |
| 455 |
$embtmp=substr($embedded,0,12-length($1)); |
| 456 |
$mycod=$embtmp . $1; |
| 457 |
$eastring=eancod($mycod); |
| 458 |
print AUSGABE "\\EAN{$eastring}$2 % embedded($1)\n"; |
| 459 |
}elsif ($line =~ /^\\ean\{(\d+)\}(.*)/){ |
| 460 |
# normal ean |
| 461 |
$eastring=eancod($1); |
| 462 |
print AUSGABE "\\EAN{$eastring}$2 %($1)\n"; |
| 463 |
}elsif($line =~ /^\\isbn\{([\dxX]+)\}(.*)/){ |
| 464 |
# isbn to be embedded |
| 465 |
$embtmp=substr($1,0,9); |
| 466 |
$mycod='978' . $embtmp; |
| 467 |
$eastring=eancod($mycod); |
| 468 |
print AUSGABE "\\EAN{$eastring}$2 % ISBN($1)\n"; |
| 469 |
}elsif($line =~ /^\\cxxviii\{(.+)\}(.*)/){ |
| 470 |
# code 128 |
| 471 |
$eastring=codcxxviii($1); |
| 472 |
print AUSGABE "\\CXXVIII $eastring $2 % Code128($1)\n"; |
| 473 |
}elsif($line =~ /^\\xciii\{(.+)\}(.*)/){ |
| 474 |
# code 93 |
| 475 |
$eastring=codxciii($1); |
| 476 |
print AUSGABE "\\XCIII{$eastring}$2 % Code93($1)\n"; |
| 477 |
}else { |
| 478 |
print AUSGABE $line; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
################################################## |
| 483 |
# we're done, so we do some cleanup and quit. |
| 484 |
close (EINGABE); |
| 485 |
close (AUSGABE); |
| 486 |
print "Done.\n"; |
| 487 |
|
| 488 |
################################################## |
| 489 |
# what we do here is called |
| 490 |
# 'falling off the edge of the world' |
| 491 |
# in the camel book. |
| 492 |
################################################## |