Skip to content

Commit c4d49ba

Browse files
author
Hugo Barrera
authored
Merge pull request #102 from Governa/extensibility
Extensibility
2 parents 6adb011 + 582969a commit c4d49ba

7 files changed

Lines changed: 48 additions & 45 deletions

File tree

barcode/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def render(self, writer_options=None, text=None):
9393
9494
:returns: Output of the writers render method.
9595
"""
96-
options = Barcode.default_writer_options.copy()
96+
options = self.default_writer_options.copy()
9797
options.update(writer_options or {})
9898
if options["write_text"] or text is not None:
9999
if text is not None:

barcode/codex.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, code: str, writer=None, add_checksum: bool = True):
4545
self.code = code.upper()
4646
if add_checksum:
4747
self.code += self.calculate_checksum()
48-
self.writer = writer or Barcode.default_writer()
48+
self.writer = writer or self.default_writer()
4949
check_code(self.code, self.name, code39.REF)
5050

5151
def __str__(self):
@@ -71,7 +71,7 @@ def build(self):
7171
def render(self, writer_options=None, text=None):
7272
options = {"module_width": MIN_SIZE, "quiet_zone": MIN_QUIET_ZONE}
7373
options.update(writer_options or {})
74-
return Barcode.render(self, options, text)
74+
return super().render(options, text)
7575

7676

7777
class PZN7(Code39):
@@ -98,7 +98,7 @@ def __init__(self, pzn, writer=None):
9898
)
9999
self.pzn = pzn
100100
self.pzn = "{}{}".format(pzn, self.calculate_checksum())
101-
Code39.__init__(self, "PZN-{}".format(self.pzn), writer, add_checksum=False)
101+
super().__init__("PZN-{}".format(self.pzn), writer, add_checksum=False)
102102

103103
def get_fullcode(self):
104104
return "PZN-{}".format(self.pzn)
@@ -133,7 +133,7 @@ class Code128(Barcode):
133133

134134
def __init__(self, code, writer=None):
135135
self.code = code
136-
self.writer = writer or Barcode.default_writer()
136+
self.writer = writer or self.default_writer()
137137
self._charset = "B"
138138
self._buffer = ""
139139
check_code(self.code, self.name, code128.ALL)
@@ -248,7 +248,7 @@ def build(self):
248248
def render(self, writer_options=None, text=None):
249249
options = {"module_width": MIN_SIZE, "quiet_zone": MIN_QUIET_ZONE}
250250
options.update(writer_options or {})
251-
return Barcode.render(self, options, text)
251+
return super().render(options, text)
252252

253253

254254
class Gs1_128(Code128):

barcode/ean.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self, ean, writer=None, no_checksum=False, guardbar=False):
7272
else:
7373
self.EDGE = _ean.EDGE
7474
self.MIDDLE = _ean.MIDDLE
75-
self.writer = writer or Barcode.default_writer()
75+
self.writer = writer or self.default_writer()
7676

7777
def __str__(self):
7878
return self.ean
@@ -125,7 +125,7 @@ def to_ascii(self):
125125
def render(self, writer_options=None, text=None):
126126
options = {"module_width": SIZES["SC2"]}
127127
options.update(writer_options or {})
128-
return Barcode.render(self, options, text)
128+
return super().render(options, text)
129129

130130

131131
class EuropeanArticleNumber13WithGuard(EuropeanArticleNumber13):
@@ -150,12 +150,12 @@ class JapanArticleNumber(EuropeanArticleNumber13):
150150

151151
valid_country_codes = list(range(450, 460)) + list(range(490, 500))
152152

153-
def __init__(self, jan, writer=None):
154-
if int(jan[:3]) not in JapanArticleNumber.valid_country_codes:
153+
def __init__(self, jan, *args, **kwargs):
154+
if int(jan[:3]) not in self.valid_country_codes:
155155
raise WrongCountryCodeError(
156156
"Country code isn't between 450-460 or 490-500."
157157
)
158-
EuropeanArticleNumber13.__init__(self, jan, writer)
158+
super().__init__(jan, *args, **kwargs)
159159

160160

161161
class EuropeanArticleNumber8(EuropeanArticleNumber13):
@@ -172,9 +172,6 @@ class EuropeanArticleNumber8(EuropeanArticleNumber13):
172172

173173
digits = 7
174174

175-
def __init__(self, ean, writer=None, guardbar=False):
176-
EuropeanArticleNumber13.__init__(self, ean, writer, guardbar=guardbar)
177-
178175
def build(self):
179176
"""Builds the barcode pattern from `self.ean`.
180177

barcode/isxn.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self, isbn, writer=None):
4848
if isbn[:3] == "979":
4949
if isbn[3:5] not in ("10", "11"):
5050
raise BarcodeError("ISBN must start with 97910 or 97911.")
51-
EuropeanArticleNumber13.__init__(self, isbn, writer)
51+
super().__init__(isbn, writer)
5252

5353

5454
class InternationalStandardBookNumber10(InternationalStandardBookNumber13):
@@ -71,7 +71,7 @@ def __init__(self, isbn, writer=None):
7171
isbn = isbn[: self.digits]
7272
self.isbn10 = isbn
7373
self.isbn10 = "{}{}".format(isbn, self._calculate_checksum())
74-
InternationalStandardBookNumber13.__init__(self, "978" + isbn, writer)
74+
super().__init__("978" + isbn, writer)
7575

7676
def _calculate_checksum(self):
7777
tmp = sum(x * int(y) for x, y in enumerate(self.isbn10[:9], start=1)) % 11
@@ -104,7 +104,7 @@ def __init__(self, issn, writer=None):
104104
issn = issn[: self.digits]
105105
self.issn = issn
106106
self.issn = "{}{}".format(issn, self._calculate_checksum())
107-
EuropeanArticleNumber13.__init__(self, self.make_ean(), writer)
107+
super().__init__(self.make_ean(), writer)
108108

109109
def _calculate_checksum(self):
110110
tmp = (

barcode/itf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, code, writer=None, narrow=2, wide=5):
3636
if len(code) % 2 != 0:
3737
code = "0" + code
3838
self.code = code
39-
self.writer = writer or Barcode.default_writer()
39+
self.writer = writer or self.default_writer()
4040
self.narrow = narrow
4141
self.wide = wide
4242

@@ -73,4 +73,4 @@ def render(self, writer_options, text=None):
7373
"quiet_zone": MIN_QUIET_ZONE,
7474
}
7575
options.update(writer_options or {})
76-
return Barcode.render(self, options, text)
76+
return super().render(options, text)

barcode/upc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(self, upc, writer=None, make_ean=False):
4141
)
4242
self.upc = upc
4343
self.upc = "{}{}".format(upc, self.calculate_checksum())
44-
self.writer = writer or Barcode.default_writer()
44+
self.writer = writer or self.default_writer()
4545

4646
def __str__(self):
4747
if self.ean:
@@ -108,7 +108,7 @@ def to_ascii(self):
108108
def render(self, writer_options=None, text=None):
109109
options = {"module_width": 0.33}
110110
options.update(writer_options or {})
111-
return Barcode.render(self, options, text)
111+
return super().render(options, text)
112112

113113

114114
UPCA = UniversalProductCodeA

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)