@@ -87,6 +87,7 @@ def __init__(self, initialize=None, paint_module=None, paint_text=None,
8787 self .background = 'white'
8888 self .foreground = 'black'
8989 self .text = ''
90+ self .human = '' # human readable text
9091 self .text_distance = 5
9192 self .center_text = True
9293
@@ -95,7 +96,7 @@ def calculate_size(self, modules_per_line, number_of_lines, dpi=300):
9596
9697 :parameters:
9798 modules_per_line : Integer
98- Number of mudules in one line.
99+ Number of modules in one line.
99100 number_of_lines : Integer
100101 Number of lines of the barcode.
101102 dpi : Integer
@@ -164,27 +165,44 @@ def render(self, code):
164165 if self ._callbacks ['initialize' ] is not None :
165166 self ._callbacks ['initialize' ](code )
166167 ypos = 1.0
167- for line in code :
168+ for cc , line in enumerate (code ):
169+ """
170+ Pack line to list give better gfx result, otherwise in can result in aliasing gaps
171+ '11010111' -> [2, -1, 1, -1, 3]
172+ """
173+ line += ' '
174+ c = 1
175+ mlist = []
176+ for i in range (0 , len (line ) - 1 ):
177+ if line [i ] == line [i + 1 ]:
178+ c += 1
179+ else :
180+ if line [i ] == "1" :
181+ mlist .append (c )
182+ else :
183+ mlist .append (- c )
184+ c = 1
168185 # Left quiet zone is x startposition
169186 xpos = self .quiet_zone
170- for mod in line :
171- if mod == '0' :
187+ bxs = xpos # x start of barcode
188+ for mod in mlist :
189+ if mod < 1 :
172190 color = self .background
173191 else :
174192 color = self .foreground
175- self ._callbacks ['paint_module' ](xpos , ypos , self .module_width ,
176- color )
177- xpos += self .module_width
178- # Add right quiet zone to every line
179- self . _callbacks [ 'paint_module' ]( xpos , ypos , self . quiet_zone ,
180- self .background )
193+ self ._callbacks ['paint_module' ](xpos , ypos , self .module_width * abs ( mod ), color ) # remove painting for background colored tiles?
194+ xpos += self . module_width * abs ( mod )
195+ bxe = xpos - self .module_width * abs ( mod ) # x end of barcode
196+ # Add right quiet zone to every line, except last line, quiet zone already provided with background, should it be removed complety?
197+ if ( cc + 1 ) != len ( code ):
198+ self . _callbacks [ 'paint_module' ]( xpos , ypos , self . quiet_zone , self .background )
181199 ypos += self .module_height
182200 if self .text and self ._callbacks ['paint_text' ] is not None :
183201 ypos += self .text_distance
184202 if self .center_text :
185- xpos = xpos / 2.0
203+ xpos = bxs + (( bxe - bxs ) / 2.0 ) # better center position for text
186204 else :
187- xpos = self . quiet_zone + 4.0
205+ xpos = bxs
188206 self ._callbacks ['paint_text' ](xpos , ypos )
189207 return self ._callbacks ['finish' ]()
190208
@@ -198,6 +216,7 @@ def __init__(self):
198216 self .dpi = 25.4
199217 self ._document = None
200218 self ._root = None
219+ self ._group = None
201220
202221 def _init (self , code ):
203222 width , height = self .calculate_size (len (code [0 ]), len (code ), self .dpi )
@@ -206,11 +225,16 @@ def _init(self, code):
206225 attributes = dict (width = SIZE .format (width ), height = SIZE .format (height ))
207226 _set_attributes (self ._root , ** attributes )
208227 self ._root .appendChild (self ._document .createComment (COMMENT ))
228+ # create group for easier handling in 3th party software like corel draw, inkscape, ...
229+ group = self ._document .createElement ('g' )
230+ attributes = dict (id = 'barcode_group' )
231+ _set_attributes (group , ** attributes )
232+ self ._group = self ._root .appendChild (group )
209233 background = self ._document .createElement ('rect' )
210234 attributes = dict (width = '100%' , height = '100%' ,
211235 style = 'fill:{0}' .format (self .background ))
212236 _set_attributes (background , ** attributes )
213- self ._root .appendChild (background )
237+ self ._group .appendChild (background )
214238
215239 def _create_module (self , xpos , ypos , width , color ):
216240 element = self ._document .createElement ('rect' )
@@ -219,7 +243,7 @@ def _create_module(self, xpos, ypos, width, color):
219243 height = SIZE .format (self .module_height ),
220244 style = 'fill:{0};' .format (color ))
221245 _set_attributes (element , ** attributes )
222- self ._root .appendChild (element )
246+ self ._group .appendChild (element )
223247
224248 def _create_text (self , xpos , ypos ):
225249 element = self ._document .createElement ('text' )
@@ -228,9 +252,14 @@ def _create_text(self, xpos, ypos):
228252 'middle;' .format (self .foreground ,
229253 self .font_size ))
230254 _set_attributes (element , ** attributes )
231- text_element = self ._document .createTextNode (self .text )
255+ # check option to override self.text with self.human (barcode as human readable data, can be used to print own formats)
256+ if self .human != '' :
257+ barcodetext = self .human
258+ else :
259+ barcodetext = self .text
260+ text_element = self ._document .createTextNode (barcodetext )
232261 element .appendChild (text_element )
233- self ._root .appendChild (element )
262+ self ._group .appendChild (element )
234263
235264 def _finish (self ):
236265 if self .compress :
0 commit comments