code128.py

Klaas Feenstra <[email protected]>
Newsgroups gmane.comp.python.reportlab.user
Message-ID <CAJcivacQgF+_pY-+y8LS3fEjNR8_-2ZuBVWDL4eQtU8YM1UFug@mail.gmail.com>
Hello,

After using reportlab for creating barcodes, I realised the optimization
for Code128 was not correct. It was only optimizing the end of the barcode
string.

I made modifications for the trailingDigitsToC function. See below. Now all
string will be analysed for optimizations. Probably there will be a better
algorithm, but this is working.

   def _trailingDigitsToC(self, l):
        # Optimization: trailing digits -> set C double-digits

        i = 0
        nl = []
        while i < len(l):
            startpos = i
            rl = ['TO_C']
            savings = -1 # the TO_C costs one character
            while i < len(l):
                if l[i] in (['START_B','TO_A','TO_B']):
                    set = 'TO' + l[i][-2:]
                    break
                elif l[i] == '\xf1':
                    rl.append(l[i])
                    i += 1
                    continue
                elif len(l[i]) == 1 and l[i] in digits \
                 and len(l[i+1]) == 1 and l[i+1] in digits:
                    rl.append(l[i] + l[i+1])
                    i += 2
                    savings += 1
                    continue
                else:
                    break

            if savings > 0:
                nl = nl + rl
                if not l[i]=='STOP':
                    nl.append(set)
                nl.append(l[i])
            else:
                nl = nl + l[startpos:i+1]
            i += 1
        return nl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.