1 '''
2 Copyright (C) 2007 Martin Owens
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.
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.
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 '''
19 import itertools
20 import sys
22 class Barcode:
23 def __init__(self, param={}):
24 self.document = None
25 self.x = 0
26 self.y = 0
28 if param.has_key('document'):
29 self.document = param['document']
30 if param.has_key('x'):
31 self.x = param['x']
32 if param.has_key('y'):
33 self.y = param['y']
35 if param.has_key('height'):
36 self.height = param['height']
37 else:
38 self.height = 30
40 self.text = param['text']
41 self.label = self.text
42 self.string = self.encode( self.text )
43 if not self.string:
44 return
45 self.width = len(self.string)
46 self.data = self.graphicalArray(self.string)
48 def generate(self):
49 if not self.string or not self.data:
50 return
52 data = self.data;
54 # create an SVG document if required
55 # if not self.document:
56 # self.document = UNKNOWN
58 if not self.document:
59 sys.stderr.write("No document defined to add barcode to\n")
60 return
62 # We don't have svg documents so lets do something raw:
63 name = 'barcode'
65 # Make sure that the id/name is inique
66 index = 0
67 while (self.document.getElementById(name)):
68 name = 'barcode' + str(index)
69 index = index + 1
71 # use an svg group element to contain the barcode
72 barcode = self.document.createElement('svg:g')
73 barcode.setAttribute('id', name)
74 barcode.setAttribute('style', 'fill: black;')
76 draw = 1
77 wOffset = int(self.x)
78 id = 1
80 for datum in data:
81 # Datum 0 tells us what style of bar is to come next
82 style = self.getStyle(int(datum[0]))
83 # Datum 1 tells us what width in units,
84 # style tells us how wide a unit is
85 width = int(datum[1]) * int(style['width'])
87 if style['write']:
88 # Add height for styles such as EA8 where
89 # the barcode goes into the text
91 rect = self.document.createElement('svg:rect')
92 rect.setAttribute('x', str(wOffset))
93 rect.setAttribute('y', str(style['top']))
94 rect.setAttribute('width', str(width))
95 rect.setAttribute('height', str(style['height']))
96 rect.setAttribute('id', name + '_bar' + str(id))
97 barcode.appendChild(rect)
98 wOffset = int(wOffset) + int(width)
99 id = id + 1
101 barwidth = wOffset - int(self.x)
102 # Add text at the bottom of the barcode
103 text = self.document.createElement('svg:text')
104 text.setAttribute( 'x', str(int(self.x) + int(barwidth / 2)) )
105 text.setAttribute( 'y', str(int(self.height) + 10 + int(self.y)) )
106 text.setAttribute( 'style', 'font-size:' + self.fontSize() + 'px;text-align:center;text-anchor:middle;' )
107 text.setAttribute( 'xml:space', 'preserve' )
108 text.setAttribute( 'id', name + '_bottomtext' )
110 text.appendChild(self.document.createTextNode(str(self.label)))
111 barcode.appendChild(text)
113 return barcode
115 # Converts black and white markers into a space array
116 def graphicalArray(self, code):
117 return [(x,len(list(y))) for x, y in itertools.groupby(code)]
119 def getStyle(self, index):
120 result = { 'width' : 1, 'top' : int(self.y), 'write' : False }
121 if index==1: # Black Bar
122 result['height'] = int(self.height)
123 result['write'] = True
124 return result
126 def fontSize(self):
127 return '9'