summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/barcodes/codean.pl
blob: 0e3cd01015868fec61dc24ee4dce5657f65a9b71 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# your shell command goes here
#
# Hacking EAN numbers to a form usable with WLEAN.MF.
#
# September 3, 1997
# Peter Willadt
#
# Added hacking any text for coding with code 128
# 1998-01-24
#
# Added checksumming for code 93
# 1998-11-29
#
# 2003-05-24
# cleaned up the code in several places and
# corrected code 93 checksumming due to a hint by
# Jacek Ruzyczka <uv_centcom@yahoo.com>
#
# 2003-05-24
# removed an error introduced the same day
#
# This file is free to use without any further permissions.
# This file comes with no warranty of any kind.
#
# The TeX file to be filtered may contain any number of lines
# that have one of the following commands
# starting at the leftmost position.
#
# \ean{12 or 13 digit number}
# the number gets coded as EAN, 
# if it is only 12 digits long, the checksum gets calculated
#
# \embed{number}
# the number is used as a base for embedding article numbers & c.
# \eean{number}
# a number to be embedded with an ean.
#
# \isbn{number}
# an isbn to make an embedded ean of.
#
# \cxxviii{any 7 bit ascii values}
# code as barcode 128 (see rules below!)
#
# \xciii{uppercase text or number}
# text to be coded as code 128
#
# example:
# You want the isbn 0-201-13448-9 to be embedded.
# so you say \isbn{0201134489},
# but you may also say \embed{9780000000000} and, 
# somewhere later in the file, \eean{020113448}
# In this case you have to leave the last digit out,
# as isbn loose their check digit in favour of the
# ean check digit.
# anyway you do it, you get your command replaced by 
# \EAN{13-digit-number-coded-strange}
# in the output file.
#
#
# code 128 rules:
# you write a line starting with \cxxviii{
# followed by arbitrary 7 bit characters, delimited by a right brace}.
# as perl is greedy, it will be the rightmost right brace (no fence matching),
# but as perl is also nice, you will be warned if there is another
# right brace.  Please note that even the percent character % will be
# included.  So it is better to write the \cxxviii{...} statement onto
# a line of its own.  You may replace any character by ^^00 and
# similiar codes, preferably you will do this to non-printable ascii
# characters, or right braces and the like. This routine will try to find
# an efficent way to make code 128 (sorry, not necessarily the most 
# efficient way) out of your input and then it will
# insert a line like \CXXVIII{3a 70 12 ... @@} in the output file.
# The code 128 special characters can be included by the following codes:
# ^^80 FNC3
# ^^81 FNC2
# ^^82 SHIFT
# ^^83 CODE C/CODE C/99
# ^^84 CODE B/FNC4/CODE B
# ^^85 FNC4/CODE A/CODE A
# ^^86 FNC1
# ^^87 START A
# ^^88 START B
# ^^89 START C
# ^^8a STOP

use strict;

use vars qw ( @ABTAB $ifname $ofname );
# code switch table for ean

@ABTAB=(0,0,0,0,0,0,     #0
	0,0,1,0,1,1,     #1
	0,0,1,1,0,1,     #2
	0,0,1,1,1,0,     #3
	0,1,0,0,1,1,	 #4 and so on
	0,1,1,0,0,1,
	0,1,1,1,0,0,
	0,1,0,1,0,1,
	0,1,0,1,1,0,
	0,1,1,0,1,0,
	);

# command line processing: Need input file

if($ARGV[0]){
    $ifname=$ARGV[0];
}else{
    print "Enter name of file to be processed: ";
    $ifname = <>;
    chomp($ifname);
}

# command line processing: need output file

if($ARGV[1]){
    $ofname=">$ARGV[1]";
}else{
    print "Enter name of output file: ";
    $ofname = <>;
    chomp($ofname);
    $ofname = '>' . $ofname;
}

# make an ean

sub eancod{
    my ($i, $j, $checksum, $disone);
    my $srcstr=shift;
    # first digits first
    my $precod=substr($srcstr,0,1);
    # Starting output string
    my $eastring=$precod . " +";
    # digits 2--7
    for($i=0;$i<6;$i++){
	my $disone=substr($srcstr,$i+1,1);
	$disone =~ tr/0123456789/ABCDEFGHIJ/;
	$disone= lc ($disone) if( @ABTAB[$precod*6 + $i]==1);
	$eastring .=$disone;
    }$eastring .= "-";
    # digits 8--13
    for($i=0;$i<6;$i++){
	# if checksum misses, do your own
	if(($i==5) && (length($srcstr)==12)){
	    for($j=0,$checksum=0;$j<12;$j++){
		$checksum+=substr($srcstr,$j,1)*(1+($j&1)*2);
	    };
	    $checksum%=10;
	    $checksum=10-$checksum;
	    $checksum%=10;
	    $disone="$checksum";
	}else {
	    $disone=substr($srcstr,$i+7,1);
	}
	$disone =~ tr/0123456789/KLMNOPQRST/;
        $eastring .=$disone;
    }$eastring .="+";
    return $eastring;
}

##################################################
# here starts the code 128 stuff
#
##################################################
# get the numerical value of a hex character, 
# e.g. 65 from 41
#  
sub hexchar{
    my $src=shift;
    my ($i, $j, $result);
    $src =~ tr/a-f/A-F/;
    $i=ord(substr($src,0,1));
    $j=ord(substr($src,1,1));
    if($i >= ord("A")){
	$i += (10-ord('A'));
    }else{
	$i -= ord("0");
    }
    if($j >= ord("A")){
	$j += (10-ord("A"));
    }else{
	$j -= ord("0");
    }
    $result=16*($i)+$j;
    return $result;
}


# globals:
# @cxxchars holds the characters the user wants to code
# @cxxlength is the size of this array
# @ctbl holds the possible codings for these chars
# @cxxout holds the codes to be output for code 128
# @cxxoutout holds the codes to be output for code 128

use vars qw ( @cxxchars $cxxlength @ctbl @cxxout @cxxoutout);

##################################################
# build up the switching table for code 128

sub makectbl{
    # locals
    my $i;
    for($i=0;$i < $cxxlength; $i++){
	if(($cxxchars[$i] >= ord("0"))&&($cxxchars[$i] <= ord("9"))){
            # digits
	    $ctbl[$i]=7;
	}elsif(($cxxchars[$i] >= ord(" "))&&($cxxchars[$i] <= ord("_"))){
	    # common Chars
	    $ctbl[$i]=3;
	}elsif($cxxchars[$i] < ord(" ")){
            # ascii control chars
	    $ctbl[$i]=1;
	}elsif(($cxxchars[$i] >=ord("`"))&&($cxxchars[$i] <= ord("\x7f"))){
	    # lowercase
	    $ctbl[$i]=2;
	    if($cxxchars[$i] == ord("}")){
		print "Encountered right brace in argument to cxxviii\n";
	    }
	}else{	                                              # Function Codes
	    $ctbl[$i] =7;
	}
    }
    $ctbl[$i]=0; # makes a stop.
}

##################################################
# make a character array from a string 
# looking like aBc\x41def^^41 or so.
#
sub unhex{
    my ($i, $j, $b);
    my $srcstr=shift;
    $j=0;
    for($i=0;($b=ord(substr($srcstr,$i,1))) > 0;$i++){
	if($b == ord("\\")){
	    if(substr($srcstr,$i+1,1) =~ /[xX]/){	 # hex input
		$cxxchars[$j] = hexchar(substr($srcstr,$i+2,2));
		$i += 3;
	    }else{
		$cxxchars[$j] = ord("\\");
	    }
	}elsif($b == ord("^")){
	    if(ord(substr($srcstr,$i+1,1)) == ord("^")){ # hex input
		$cxxchars[$j] = hexchar(substr($srcstr,$i+2,2));
		$i += 3;
	    }else{
		$cxxchars[$j] = ord("^");
	    }
	}else{
	    $cxxchars[$j] = $b;
	}
	$j++;
    }
    return $j;
}

##################################################
# write out a chunk of code 128 in hex symbols
#
sub cxxchunk{
    my $j=shift;
    my ($i, $sum, $k);
    $sum=7;
    for($i=0;$sum & $ctbl[$j+$i]; $i++){
	$sum &= $ctbl[$j+$i];
    }
    if($sum==1){
	$cxxout[$j]=101;
    }else{
	$cxxout[$j]=100;
    }
    for($k=0;$k<$i;$k++){
	if(($sum==1)&&($cxxchars[$j+$k] < ord(" "))){
	    $cxxout[$j+$k+1]=$cxxchars[$j+$k] + 64;
	}else{
	    $cxxout[$j+$k+1]=$cxxchars[$j+$k] - ord(" ");
	}
    }
    $k++;
    return $k;
}

##################################################
# consecutive digits may perhaps be efficiently
# coded with charset C
#
sub pastedigits{
    my $digitcount=shift;
    my $j=shift;
    my $firstdigit=shift;
    my $lastset=shift;
    my $k;
    if($digitcount==0){      # nothing to do
	return $j;
    }elsif($digitcount<4){
	# digits here, but unfortunately not enough digits.
	# so just copy them
	for($k=0;$k<$digitcount;$k++){
	    $cxxoutout[$j] = $cxxout[$firstdigit+$k];
	    $j++;
	}
	return $j;
    }else{
	# is there an odd number of digits?
	if(($digitcount & 1)==1){
	    $cxxoutout[$j] = $cxxout[$firstdigit];
	    $firstdigit++;
	    $digitcount--;
	    $j++;
	}elsif(($cxxout[$j-1]>=99)&&($cxxout[$j-1]<=101)){
	    # Switched immediately before digits.
	    # so overwrite the switch
	    $j--;
	}
	$cxxoutout[$j] = 99; # switch to set C
	$j++;
	# copy digits in compressed format
	for($k=0;$k<$digitcount;$k+=2){
	    $cxxoutout[$j] = ($cxxout[$firstdigit+$k]-16)*10
		+$cxxout[$firstdigit+$k+1]-16;
	    $j++;
	}
	# reset char set, if you have to
	if($lastset > 0){
	    $cxxoutout[$j] = $lastset;
	    $j++;
	}
    }
    return $j;
}

sub digitoptimize{
    # change to charset C if there are at least four numbers in a row.
    # copy to @cxxoutout
    my ($lastset, $firstdigit, $digitcount, $i,$j,$k);
    $lastset   =0;
    $firstdigit=0;
    $digitcount=0;
    $j=0;
    for($i = 0; $i < $cxxlength; $i++){
	if(($cxxout[$i]>=16)&&($cxxout[$i]<=25)){
	    # it's a number
	    if($digitcount == 0){
		$firstdigit=$i;
	    }
	    $digitcount++;
	    next;
	}
	if($digitcount >0){
	    $j=pastedigits($digitcount,$j,$firstdigit,$lastset);
	    $digitcount=0;
	}
	$cxxoutout[$j] = $cxxout[$i];
	$j++;
	if(($cxxout[$i]>=99)&&($cxxout[$i]<=101)){
	    # it's a code switch
	    $lastset=$cxxout[$i];
	}else {
	    $lastset=0;
	}
    }
    # $lastset is zero here, as there is no further need 
    # to switch the char set any more - we're at the end
    $j=pastedigits($digitcount,$j,$firstdigit, 0);
    return $j;
}

##################################################
# code 128 is a little complicated
# if you have read till here, you already know.
#
sub codcxxviii{
    # locals
    my ($i, $j, $sum);
    my $srcstr=shift;
    # first step: unhexing
    $cxxlength = unhex($srcstr);
    # @cxxchars now holds the characters the user wants
    makectbl();
    # @ctbl now contains the possible tables for the chars in $j;
    for($i =0; $i < $cxxlength;){
	$i += cxxchunk($i);
    }
    # change codeset switch to start
    $cxxlength=$i;
    $j=digitoptimize();
    # Start symbols are different from switch symbols
    if($cxxoutout[0]==99){
	$cxxoutout[0]=105;
    }elsif($cxxoutout[0]==100){
	$cxxoutout[0]=104;
    }else{
	$cxxoutout[0]=103;
    }
    # calculate checksum and build output string
    for($i=1,$sum=$cxxoutout[0];$i<$j;$i++){
	$sum+=$cxxoutout[$i]*$i;
    }
    $sum %=103;
    $cxxoutout[$j]=$sum;
    $j++;
    $cxxoutout[$j]=106; #stop sign
    $srcstr="";
    for($i=0;$i<($j+1);$i++){
	$srcstr .= sprintf "%02X", $cxxoutout[$i];
    }
    $srcstr .= "@@";
    return $srcstr;
}

##################################################
# do code 93 -- it's easy
##################################################
sub codxciii{
    my $srcstr=shift;
    my $cstbl='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%()[]';
    my ($i, $j, $sumc, $sumh, $checkchar);
    $sumc=$sumh=0;
    for ($i=0;$i<length($srcstr);$i++){
	$j=index($cstbl, substr($srcstr,$i,1),0);
	# $j is the check value of the character.
	$sumh=$sumh+$j;
	$sumc=$sumc+$sumh;
    }
    $srcstr=$srcstr . substr($cstbl,$sumc%47,1);
    $sumc=$sumc+$sumh+($sumc%47);
    $checkchar=substr($cstbl,$sumc%47,1);    
    $srcstr =  $srcstr . $checkchar;
    $srcstr =~ s/\$/\\\$/g;
    $srcstr =~ s/%/\\%/g;
    $srcstr =~ s/ /\\char\'040/g;
    return $srcstr;
}

##################################################
# we got both input and output file,
# we defined all subroutines,
# so here we go...
#
use vars qw ( $line $embedded $embtmp $mycod $eastring);

open(EINGABE, $ifname) or die "No file";
open(AUSGABE, $ofname) or die "Can't open output file";
while($line=<EINGABE>){
    # save 0.0005 (est.) % running time
    unless ($line =~ /^\\/){
	print AUSGABE $line;
	next;
    }
    if($line =~ /^\\embed{(\d+)\}/){
	$embedded=$1;
	print AUSGABE $line;
    }elsif($line =~ /^\\eean\{(\d+)\}(.*)/){
	# embedded EAN
	$embtmp=substr($embedded,0,12-length($1));
	$mycod=$embtmp . $1;
	$eastring=eancod($mycod);
	print AUSGABE "\\EAN{$eastring}$2 % embedded($1)\n";
    }elsif ($line =~ /^\\ean\{(\d+)\}(.*)/){
	# normal ean
	$eastring=eancod($1);
	print AUSGABE "\\EAN{$eastring}$2 %($1)\n";
    }elsif($line =~ /^\\isbn\{([\dxX]+)\}(.*)/){
	# isbn to be embedded
	$embtmp=substr($1,0,9);
	$mycod='978' . $embtmp;
	$eastring=eancod($mycod);
	print AUSGABE "\\EAN{$eastring}$2 % ISBN($1)\n";
    }elsif($line =~ /^\\cxxviii\{(.+)\}(.*)/){
	# code 128
	$eastring=codcxxviii($1);
	print AUSGABE "\\CXXVIII $eastring $2 % Code128($1)\n";
    }elsif($line =~ /^\\xciii\{(.+)\}(.*)/){
	# code 93
	$eastring=codxciii($1);
	print AUSGABE "\\XCIII{$eastring}$2 % Code93($1)\n";
    }else {
	print AUSGABE $line;
    }
}

##################################################
# we're done, so we do some cleanup and quit.
close (EINGABE);
close (AUSGABE);
print "Done.\n";

##################################################
# what we do here is called
# 'falling off the edge of the world'
# in the camel book.
##################################################