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 / EAN5.py < prev    next >
Text File  |  2011-07-08  |  3KB  |  68 lines

  1. ''' 
  2. Copyright (C) 2007 Martin Owens 
  3. Copyright (C) 2009 Aaron C Spike 
  4.  
  5. Thanks to Lineaire Chez of Inkbar ( www.inkbar.lineaire.net ) 
  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. from Base import Barcode
  23. import sys
  24.  
  25. mapLeftFamily = [
  26.     [ "0001101","0011001","0010011","0111101","0100011","0110001","0101111","0111011","0110111","0001011" ], #L
  27.     [ "0100111","0110011","0011011","0100001","0011101","0111001","0000101","0010001","0001001","0010111" ], #G
  28. ]
  29. mapFamily  = [ '11000','10100','10010','10001','01100','00110','00011','01010','01001','00101' ]
  30.  
  31. startBar = '01011';
  32. sepBar = '01';
  33.  
  34. class Object(Barcode):
  35.     def encode(self, number):
  36.         result = []
  37.         self.x += 110.0             # horizontal offset so it does not overlap EAN13
  38.         self.y -= self.height + 5   # move the text to the top
  39.         if len(number) != 5 or not number.isdigit():
  40.             sys.stderr.write("Can not encode '" + number + "' into EAN5 Barcode, Size must be 5 numbers only\n")
  41.             return
  42.  
  43.         check = self.getChecksum(number)
  44.         family = mapFamily[check]
  45.  
  46.         for i in range(5):
  47.             mapLeft = mapLeftFamily[int(family[i])]
  48.             result.append(mapLeft[int(number[i])])
  49.  
  50.         self.label = number[0]
  51.         for i in range(1,5):
  52.             self.label += ' ' + number[i]
  53.         self.inclabel = self.label
  54.         return startBar + '01'.join(result)
  55.  
  56.     def getChecksum(self, number):
  57.         return sum([int(n)*int(m) for n,m in zip(number, '39393')]) % 10
  58.  
  59.     def getStyle(self, index):
  60.         result = { 'width' : '1', 'top' : int(self.y) + self.height + 5 + int(self.fontSize()), 'write' : True }
  61.         if index==0: # White Space
  62.             result['write'] = False
  63.         elif index==1: # Black Bar
  64.             result['height'] = int(self.height)
  65.         elif index==2: # Guide Bar
  66.             result['height'] = int(self.height) + 5
  67.         return result
  68.