diff options
| author | Martin Owens <doctormo@gmail.com> | 2016-04-14 00:50:01 +0000 |
|---|---|---|
| committer | Martin Owens <doctormo@gmail.com> | 2016-04-14 00:50:01 +0000 |
| commit | ff7d0a611e38ba0415bef8c22a4ca9c81b3a584a (patch) | |
| tree | 823d10e6a4769e3081561c038c8be1c8b4269501 /share/extensions/Barcode/Code93.py | |
| parent | icons.svg can be reduced in size (fix missing radial gradients) (diff) | |
| download | inkscape-ff7d0a611e38ba0415bef8c22a4ca9c81b3a584a.tar.gz inkscape-ff7d0a611e38ba0415bef8c22a4ca9c81b3a584a.zip | |
Upgrade barcodes, more pylint, new Ean2 code and extended Ean13 with automatic Ean2 and Ean5 additions
(bzr r14842)
Diffstat (limited to 'share/extensions/Barcode/Code93.py')
| -rw-r--r-- | share/extensions/Barcode/Code93.py | 120 |
1 files changed, 56 insertions, 64 deletions
diff --git a/share/extensions/Barcode/Code93.py b/share/extensions/Barcode/Code93.py index 2b90fdda1..939a739dd 100644 --- a/share/extensions/Barcode/Code93.py +++ b/share/extensions/Barcode/Code93.py @@ -13,109 +13,101 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Python barcode renderer for Code93 barcodes. Designed for use with Inkscape. """ -from Base import Barcode +from .Base import Barcode -chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%' -encode = list(chars) -encode.append('($)') -encode.append('(/)') -encode.append('(+)') -encode.append('(%)') -encode.append('MARKER') +PALLET = list('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%') +PALLET.append('($)') +PALLET.append('(/)') +PALLET.append('(+)') +PALLET.append('(%)') +PALLET.append('MARKER') -map = {} - -i = 0 -for char in encode: - map[char] = i - i = i + 1 - -# Extended encoding maps for full ASCII Code93 -def getMap(array): +MAP = dict((PALLET[i], i) for i in range(len(PALLET))) +def get_map(array): + """Extended ENCODE maps for full ASCII Code93""" result = {} - y = 10 - - for x in array: - result[chr(x)] = encode[y] - y = y + 1 - - return result; + pos = 10 + for char in array: + result[chr(char)] = PALLET[pos] + pos = pos + 1 + return result # MapA is eclectic, but B, C, D are all ASCII ranges -mapA = getMap([27,28,29,30,31,59,60,61,62,63,91,92,93,94,95,123,124,125,126,127,0,64,96,127,127,127]) # % -mapB = getMap(range(1, 26)) # $ -mapC = getMap(range(33, 58)) # / -mapD = getMap(range(97, 122)) # + - -encoding = '100010100 101001000 101000100 101000010 100101000 100100100 100100010 101010000 100010010 100001010 110101000 110100100 110100010 110010100 110010010 110001010 101101000 101100100 101100010 100110100 100011010 101011000 101001100 101000110 100101100 100010110 110110100 110110010 110101100 110100110 110010110 110011010 101101100 101100110 100110110 100111010 100101110 111010100 111010010 111001010 101101110 101110110 110101110 100100110 111011010 111010110 100110010 101011110'.split() +MAP_A = get_map([27, 28, 29, 30, 31, 59, 60, 61, 62, 63, 91, 92, 93, 94, 95, + 123, 124, 125, 126, 127, 0, 64, 96, 127, 127, 127]) # % +MAP_B = get_map(range(1, 26)) # $ +MAP_C = get_map(range(33, 58)) # / +MAP_D = get_map(range(97, 122)) # + + +ENCODE = [ + '100010100', '101001000', '101000100', '101000010', '100101000', + '100100100', '100100010', '101010000', '100010010', '100001010', + '110101000', '110100100', '110100010', '110010100', '110010010', + '110001010', '101101000', '101100100', '101100010', '100110100', + '100011010', '101011000', '101001100', '101000110', '100101100', + '100010110', '110110100', '110110010', '110101100', '110100110', + '110010110', '110011010', '101101100', '101100110', '100110110', + '100111010', '100101110', '111010100', '111010010', '111001010', + '101101110', '101110110', '110101110', '100100110', '111011010', + '111010110', '100110010', '101011110', '' +] class Code93(Barcode): def encode(self, text): - # start marker - bits = self.encode93('MARKER') + # start marker + bits = ENCODE[MAP.get('MARKER', -1)] # Extend to ASCII charset ( return Array ) - text = self.encodeAscii(text) + text = self.encode_ascii(text) # Calculate the checksums text.append(self.checksum(text, 20)) # C text.append(self.checksum(text, 15)) # K - # Now convert text into the encoding bits (black and white stripes) + # Now convert text into the ENCODE bits (black and white stripes) for char in text: - bits = bits + self.encode93(char) - - # end marker - bits = bits + self.encode93('MARKER') + bits = bits + ENCODE[MAP.get(char, -1)] - # termination bar - bits = bits + '1' - - self.inclabel = text - return bits + # end marker and termination bar + return bits + ENCODE[MAP.get('MARKER', -1)] + '1' def checksum(self, text, mod): + """Generate a code 93 checksum""" weight = len(text) % mod - check = 0 + check = 0 for char in text: - check = check + (map[char] * weight) + check = check + (MAP[char] * weight) # Reset the weight is required weight = weight - 1 if weight == 0: weight = mod - return encode[check % 47] + return PALLET[check % 47] - # Some charicters need re-encoding into the code93 specification - def encodeAscii(self, text): + # Some charicters need re-ENCODE into the code93 specification + def encode_ascii(self, text): result = [] for char in text: - if map.has_key(char): + if MAP.has_key(char): result.append(char) - elif mapA.has_key(char): + elif MAP_A.has_key(char): result.append('(%)') - result.append(mapA[char]) - elif mapB.has_key(char): + result.append(MAP_A[char]) + elif MAP_B.has_key(char): result.append('($)') - result.append(mapB[char]) - elif mapC.has_key(char): + result.append(MAP_B[char]) + elif MAP_C.has_key(char): result.append('(/)') - result.append(mapC[char]) - elif mapD.has_key(char): + result.append(MAP_C[char]) + elif MAP_D.has_key(char): result.append('(+)') - result.append(mapD[char]) - + result.append(MAP_D[char]) return result - def encode93(self, char): - if map.has_key(char): - return encoding[map[char]] - return '' - |
