@@ -95,7 +95,7 @@ def calculate_size(self, modules_per_line, number_of_lines, dpi=300):
9595
9696 :parameters:
9797 modules_per_line : Integer
98- Number of mudules in one line.
98+ Number of modules in one line.
9999 number_of_lines : Integer
100100 Number of lines of the barcode.
101101 dpi : Integer
@@ -164,27 +164,44 @@ def render(self, code):
164164 if self ._callbacks ['initialize' ] is not None :
165165 self ._callbacks ['initialize' ](code )
166166 ypos = 1.0
167- for line in code :
167+ for cc , line in enumerate (code ):
168+ """
169+ Pack line to list give better gfx result, otherwise in can result in aliasing gaps
170+ '11010111' -> [2, -1, 1, -1, 3]
171+ """
172+ line += ' '
173+ c = 1
174+ mlist = []
175+ for i in range (0 , len (line ) - 1 ):
176+ if line [i ] == line [i + 1 ]:
177+ c += 1
178+ else :
179+ if line [i ] == "1" :
180+ mlist .append (c )
181+ else :
182+ mlist .append (- c )
183+ c = 1
168184 # Left quiet zone is x startposition
169185 xpos = self .quiet_zone
170- for mod in line :
171- if mod == '0' :
186+ bxs = xpos # x start of barcode
187+ for mod in mlist :
188+ if mod < 1 :
172189 color = self .background
173190 else :
174191 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 )
192+ self ._callbacks ['paint_module' ](xpos , ypos , self .module_width * abs ( mod ), color ) # remove painting for background colored tiles?
193+ xpos += self . module_width * abs ( mod )
194+ bxe = xpos - self .module_width * abs ( mod ) # x end of barcode
195+ # Add right quiet zone to every line, except last line, quiet zone already provided with background, should it be removed complety?
196+ if ( cc + 1 ) != len ( code ):
197+ self . _callbacks [ 'paint_module' ]( xpos , ypos , self . quiet_zone , self .background )
181198 ypos += self .module_height
182199 if self .text and self ._callbacks ['paint_text' ] is not None :
183200 ypos += self .text_distance
184201 if self .center_text :
185- xpos = xpos / 2.0
202+ xpos = bxs + (( bxe - bxs ) / 2.0 ) # better center position for text
186203 else :
187- xpos = self . quiet_zone + 4.0
204+ xpos = bxs
188205 self ._callbacks ['paint_text' ](xpos , ypos )
189206 return self ._callbacks ['finish' ]()
190207
@@ -198,6 +215,7 @@ def __init__(self):
198215 self .dpi = 25.4
199216 self ._document = None
200217 self ._root = None
218+ self ._group = None
201219
202220 def _init (self , code ):
203221 width , height = self .calculate_size (len (code [0 ]), len (code ), self .dpi )
@@ -206,11 +224,16 @@ def _init(self, code):
206224 attributes = dict (width = SIZE .format (width ), height = SIZE .format (height ))
207225 _set_attributes (self ._root , ** attributes )
208226 self ._root .appendChild (self ._document .createComment (COMMENT ))
227+ # create group for easier handling in 3th party software like corel draw, inkscape, ...
228+ group = self ._document .createElement ('g' )
229+ attributes = dict (id = 'barcode_group' )
230+ _set_attributes (group , ** attributes )
231+ self ._group = self ._root .appendChild (group )
209232 background = self ._document .createElement ('rect' )
210233 attributes = dict (width = '100%' , height = '100%' ,
211234 style = 'fill:{0}' .format (self .background ))
212235 _set_attributes (background , ** attributes )
213- self ._root .appendChild (background )
236+ self ._group .appendChild (background )
214237
215238 def _create_module (self , xpos , ypos , width , color ):
216239 element = self ._document .createElement ('rect' )
@@ -219,7 +242,7 @@ def _create_module(self, xpos, ypos, width, color):
219242 height = SIZE .format (self .module_height ),
220243 style = 'fill:{0};' .format (color ))
221244 _set_attributes (element , ** attributes )
222- self ._root .appendChild (element )
245+ self ._group .appendChild (element )
223246
224247 def _create_text (self , xpos , ypos ):
225248 element = self ._document .createElement ('text' )
@@ -230,7 +253,7 @@ def _create_text(self, xpos, ypos):
230253 _set_attributes (element , ** attributes )
231254 text_element = self ._document .createTextNode (self .text )
232255 element .appendChild (text_element )
233- self ._root .appendChild (element )
256+ self ._group .appendChild (element )
234257
235258 def _finish (self ):
236259 if self .compress :
0 commit comments