Skip to content

Commit afe780a

Browse files
author
Fernando Governatore
committed
Extracts method packed from render
Makes the render method easier to understand and the packing easier to override. Avoids building a list unnecessarily by yielding the values one by one. Updates documentation to allow the character "G"(guard) in the writer spec. Also adds `guard_height_factor` to allow changing the default proportion between the guard bars and normal ones.
1 parent 545fef6 commit afe780a

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

barcode/writer.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def __init__(
9898
self.text_distance = 5
9999
self.text_line_distance = 1
100100
self.center_text = True
101+
self.guard_height_factor = 1.1
101102

102103
def calculate_size(self, modules_per_line, number_of_lines):
103104
"""Calculates the size of the barcode in pixel.
@@ -163,39 +164,47 @@ def set_options(self, options):
163164
if hasattr(self, key):
164165
setattr(self, key, val)
165166

167+
def packed(self, line):
168+
"""
169+
Pack line to list give better gfx result, otherwise in can
170+
result in aliasing gaps
171+
'11010111' -> [2, -1, 1, -1, 3]
172+
173+
This method will yield a sequence of pairs (width, height_factor).
174+
175+
:parameters:
176+
line: String
177+
A string matching the writer spec
178+
(only contain 0 or 1 or G).
179+
"""
180+
line += " "
181+
c = 1
182+
for i in range(0, len(line) - 1):
183+
if line[i] == line[i + 1]:
184+
c += 1
185+
else:
186+
if line[i] == "1":
187+
yield (c, 1)
188+
elif line[i] == "G":
189+
yield (c, self.guard_height_factor)
190+
else:
191+
yield (-c, self.guard_height_factor)
192+
c = 1
193+
166194
def render(self, code):
167195
"""Renders the barcode to whatever the inheriting writer provides,
168196
using the registered callbacks.
169197
170198
:parameters:
171199
code : List
172200
List of strings matching the writer spec
173-
(only contain 0 or 1).
201+
(only contain 0 or 1 or G).
174202
"""
175203
if self._callbacks["initialize"] is not None:
176204
self._callbacks["initialize"](code)
177205
ypos = 1.0
178206
base_height = self.module_height
179207
for cc, line in enumerate(code):
180-
"""
181-
Pack line to list give better gfx result, otherwise in can
182-
result in aliasing gaps
183-
'11010111' -> [2, -1, 1, -1, 3]
184-
"""
185-
line += " "
186-
c = 1
187-
mlist = []
188-
for i in range(0, len(line) - 1):
189-
if line[i] == line[i + 1]:
190-
c += 1
191-
else:
192-
if line[i] == "1":
193-
mlist.append((c, 1))
194-
elif line[i] == "G":
195-
mlist.append((c, 1.1))
196-
else:
197-
mlist.append((-c, 1.1))
198-
c = 1
199208
# Left quiet zone is x startposition
200209
xpos = self.quiet_zone
201210
bxs = xpos # x start of barcode
@@ -206,10 +215,7 @@ def render(self, code):
206215
# Flag that indicates if the previous mod was part of an guard block:
207216
"was_guard": False,
208217
}
209-
for mod in mlist:
210-
height_factor = mod[1]
211-
mod = mod[0]
212-
218+
for mod, height_factor in self.packed(line):
213219
if mod < 1:
214220
color = self.background
215221
else:

0 commit comments

Comments
 (0)