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 / Code128.py < prev    next >
Text File  |  2011-07-08  |  5KB  |  130 lines

  1. '''
  2. Copyright (C) 2007 Martin Owens
  3.  
  4. Debugged by Ralf Heinecke & Martin Siepmann 09/07/2007
  5. Debugged by Horst Schottky Feb. 27. 2010
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  
  21.  
  22.  
  23. '''
  24.  
  25. from Base import Barcode
  26. import math
  27. import re
  28.  
  29. map = [ '11011001100','11001101100','11001100110','10010011000','10010001100','10001001100','10011001000','10011000100','10001100100','11001001000','11001000100','11000100100','10110011100','10011011100','10011001110','10111001100','10011101100','10011100110','11001110010','11001011100','11001001110','11011100100','11001110100','11101101110','11101001100','11100101100','11100100110','11101100100','11100110100','11100110010','11011011000','11011000110','11000110110','10100011000','10001011000','10001000110','10110001000','10001101000','10001100010','11010001000','11000101000','11000100010','10110111000','10110001110','10001101110','10111011000','10111000110','10001110110','11101110110','11010001110','11000101110','11011101000','11011100010','11011101110','11101011000','11101000110','11100010110','11101101000','11101100010','11100011010','11101111010','11001000010','11110001010','10100110000','10100001100','10010110000','10010000110','10000101100','10000100110','10110010000','10110000100','10011010000','10011000010','10000110100','10000110010','11000010010','11001010000','11110111010','11000010100','10001111010','10100111100','10010111100','10010011110','10111100100','10011110100','10011110010','11110100100','11110010100','11110010010','11011011110','11011110110','11110110110','10101111000','10100011110','10001011110','10111101000','10111100010','11110101000','11110100010','10111011110','10111101110','11101011110','11110101110','11010000100','11010010000','11010011100','11000111010','11' ]
  30.  
  31. def mapExtra(sd, chars):
  32.     result = list(sd)
  33.     for char in chars:
  34.         result.append(chr(char))
  35.     result.append('FNC3')
  36.     result.append('FNC2')
  37.     result.append('SHIFT')
  38.     return result
  39.  
  40. # The mapExtra method is used to slim down the amount
  41. # of pre code and instead we generate the lists
  42. charAB = list(' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_')
  43. charA = mapExtra(charAB, range(0, 31)) # Offset 64
  44. charB = mapExtra(charAB, range(96, 125)) # Offset -32
  45.  
  46. class Object(Barcode):
  47.     def encode(self, text):
  48.         result = ''
  49.         blocks = []
  50.         block  = ''
  51.  
  52.         # Split up into sections of numbers, or charicters
  53.         # This makes sure that all the charicters are encoded
  54.         # In the best way posible for Code128
  55.         for datum in re.findall(r'(?:(?:\d\d){2,})|(?:^\d\d)|.', text):
  56.             if len(datum) == 1:
  57.                 block = block + datum
  58.             else:
  59.                 if block:
  60.                     blocks.append(self.bestBlock(block))
  61.                     block = ''
  62.                 blocks.append( [ 'C', datum ] )
  63.  
  64.         if block:
  65.             blocks.append(self.bestBlock(block))
  66.             block = '';
  67.         
  68.         self.inclabel = text
  69.         return self.encodeBlocks(blocks)
  70.  
  71.     def bestBlock(self, block):
  72.         # If this has lower case then select B over A
  73.         if block.upper() == block:
  74.             return [ 'A', block ]
  75.         return [ 'B', block ]
  76.         
  77.     def encodeBlocks(self, blocks):
  78.         total  = 0
  79.         pos    = 0
  80.         encode = '';
  81.     
  82.         for block in blocks:
  83.             set   = block[0]
  84.             datum = block[1]
  85.  
  86.             # POS :   0,   1
  87.             # A   : 101, 103
  88.             # B   : 100, 104
  89.             # C   :  99, 105
  90.             num = 0;
  91.             if set == 'A':
  92.                 num = 103
  93.             elif set == 'B':
  94.                 num = 104
  95.             elif set == 'C':
  96.                 num = 105
  97.  
  98.             i = pos
  99.             if pos:
  100.                 num = 204 - num
  101.             else:
  102.                 i = 1
  103.  
  104.             total = total + num * i
  105.             encode = encode + map[num]
  106.             pos = pos + 1
  107.  
  108.             if set == 'A' or set == 'B':
  109.                 chars = charB
  110.                 if set == 'A':
  111.                     chars = charA
  112.  
  113.                 for char in datum:
  114.                     total = total + (chars.index(char) * pos)
  115.                     encode = encode + map[chars.index(char)]
  116.                     pos = pos + 1
  117.             else:
  118.                 for char in (datum[i:i+2] for i in range(0, len(datum), 2)):
  119.                     total = total + (int(char) * pos)
  120.                     encode = encode + map[int(char)]
  121.                     pos = pos + 1
  122.     
  123.         checksum = total % 103
  124.         encode = encode + map[checksum]
  125.         encode = encode + map[106]
  126.         encode = encode + map[107]
  127.     
  128.         return encode
  129.  
  130.