Code

Extensions. Barcode extension refactoring (see https://code.launchpad.net/~doctormo...
[inkscape.git] / share / extensions / Barcode / Code39Ext.py
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 Python barcode renderer for Code39 Extended barcodes. Designed for Inkscape.
20 """
22 import Code39
24 encode = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
26 map = {}
28 i = 0
29 for char in encode:
30     map[char] = i
31     i = i + 1
33 # Extended encoding maps for full ASCII Code93
34 def getMap(array):
35     result = {}
36     y = 0
37     for x in array:
38         result[chr(x)] = encode[y]
39         y = y + 1
41     return result;
43 # MapA is eclectic, but B, C, D are all ASCII ranges
44 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]) # %
45 mapB = getMap(range(1, 26)) # $
46 mapC = getMap(range(33, 58)) # /
47 mapD = getMap(range(97, 122)) # +
49 class Object(Code39.Object):
50     def encode(self, text):
51         # We are only going to extend the Code39 barcodes
52         result = ''
53         for char in text:
54             if mapA.has_key(char):
55                 char = '%' + mapA[char]
56             elif mapB.has_key(char):
57                 char = '$' + mapB[char]
58             elif mapC.has_key(char):
59                 char = '/' + mapC[char]
60             elif mapD.has_key(char):
61                 char = '+' + mapD[char]
62             result = result + char
64         return Code39.Object.encode(self, result);