home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / share / extensions / Barcode / Code93.py < prev    next >
Text File  |  2011-07-08  |  3KB  |  119 lines

  1. '''
  2. Copyright (C) 2007 Martin Owens
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. '''
  18.  
  19. from Base import Barcode
  20.  
  21. chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%'
  22. encode = list(chars)
  23. encode.append('($)')
  24. encode.append('(/)')
  25. encode.append('(+)')
  26. encode.append('(%)')
  27. encode.append('MARKER')
  28.  
  29. map = {}
  30.  
  31. i = 0
  32. for char in encode:
  33.     map[char] = i
  34.     i = i + 1
  35.  
  36. # Extended encoding maps for full ASCII Code93
  37. def getMap(array):
  38.  
  39.     result = {}
  40.     y = 10
  41.  
  42.     for x in array:
  43.         result[chr(x)] = encode[y]
  44.         y = y + 1
  45.  
  46.     return result;
  47.  
  48. # MapA is eclectic, but B, C, D are all ASCII ranges
  49. 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]) # %
  50. mapB = getMap(range(1, 26)) # $
  51. mapC = getMap(range(33, 58)) # /
  52. mapD = getMap(range(97, 122)) # +
  53.  
  54. 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()
  55.  
  56. class Object(Barcode):
  57.     def encode(self, text):
  58.         # start marker  
  59.         bits = self.encode93('MARKER')
  60.  
  61.         # Extend to ASCII charset ( return Array )
  62.         text = self.encodeAscii(text)
  63.  
  64.         # Calculate the checksums
  65.         text.append(self.checksum(text, 20)) # C
  66.         text.append(self.checksum(text, 15)) # K
  67.  
  68.         # Now convert text into the encoding bits (black and white stripes)    
  69.         for char in text:
  70.             bits = bits + self.encode93(char)
  71.  
  72.         # end marker  
  73.         bits = bits + self.encode93('MARKER')
  74.  
  75.         # termination bar
  76.         bits = bits + '1'
  77.     
  78.         self.inclabel = text
  79.         return bits
  80.  
  81.     def checksum(self, text, mod):
  82.         weight = len(text) % mod
  83.         check  = 0
  84.         for char in text:
  85.             check = check + (map[char] * weight)
  86.             # Reset the weight is required
  87.             weight = weight - 1
  88.             if weight == 0:
  89.                 weight = mod
  90.  
  91.         return encode[check % 47]
  92.  
  93.     # Some charicters need re-encoding into the code93 specification
  94.     def encodeAscii(self, text):
  95.         result = []
  96.         for char in text:
  97.             if map.has_key(char):
  98.                 result.append(char)
  99.             elif mapA.has_key(char):
  100.                 result.append('(%)')
  101.                 result.append(mapA[char])
  102.             elif mapB.has_key(char):
  103.                 result.append('($)')
  104.                 result.append(mapB[char])
  105.             elif mapC.has_key(char):
  106.                 result.append('(/)')
  107.                 result.append(mapC[char])
  108.             elif mapD.has_key(char):
  109.                 result.append('(+)')
  110.                 result.append(mapD[char])
  111.                 
  112.         return result
  113.  
  114.     def encode93(self, char):
  115.         if map.has_key(char):
  116.             return encoding[map[char]]
  117.         return ''
  118.  
  119.