Skip to content

Commit 2ea3b65

Browse files
committed
Enforce that code is a singleton list for BaseWriter.render
1 parent 9127bc7 commit 2ea3b65

File tree

1 file changed

+57
-46
lines changed

1 file changed

+57
-46
lines changed

barcode/writer.py

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import xml.dom.minidom
66
from typing import TYPE_CHECKING
7+
from typing import Any
78
from typing import BinaryIO
89
from typing import Callable
910
from typing import TypedDict
@@ -96,6 +97,21 @@ class BaseWriter:
9697
"""
9798

9899
_callbacks: Callbacks
100+
module_width: float
101+
module_height: float
102+
font_path: str
103+
font_size: float
104+
quiet_zone: float
105+
background: str | int
106+
foreground: str | int
107+
text: str
108+
human: str
109+
text_distance: float
110+
text_line_distance: float
111+
center_text: bool
112+
guard_height_factor: float
113+
margin_top: float
114+
margin_bottom: float
99115

100116
def __init__(
101117
self,
@@ -204,65 +220,60 @@ def packed(self, line: str) -> Generator[tuple[int, float], str, None]:
204220
yield (-c, self.guard_height_factor)
205221
c = 1
206222

207-
def render(self, code):
223+
def render(self, code: list[str]) -> Any:
208224
"""Renders the barcode to whatever the inheriting writer provides,
209225
using the registered callbacks.
210226
211227
:parameters:
212228
code : List
213-
List of strings matching the writer spec
229+
List consisting of a single string matching the writer spec
214230
(only contain 0 or 1 or G).
215231
"""
216232
if self._callbacks["initialize"] is not None:
217233
self._callbacks["initialize"](code)
218234
ypos = self.margin_top
219235
base_height = self.module_height
220-
for cc, line in enumerate(code):
221-
# Left quiet zone is x startposition
222-
xpos = self.quiet_zone
223-
bxs = xpos # x start of barcode
224-
text: InternalText = {
225-
"start": [], # The x start of a guard
226-
"end": [], # The x end of a guard
227-
"xpos": [], # The x position where to write a text block
228-
# Flag that indicates if the previous mod was part of an guard block:
229-
"was_guard": False,
230-
}
231-
for mod, height_factor in self.packed(line):
232-
if mod < 1:
233-
color = self.background
234-
else:
235-
color = self.foreground
236-
237-
if text["was_guard"] and height_factor == 1:
238-
# The current guard ended, store its x position
239-
text["end"].append(xpos)
240-
text["was_guard"] = False
241-
elif not text["was_guard"] and height_factor != 1:
242-
# A guard started, store its x position
243-
text["start"].append(xpos)
244-
text["was_guard"] = True
245-
246-
self.module_height = base_height * height_factor
247-
# remove painting for background colored tiles?
248-
self._callbacks["paint_module"](
249-
xpos, ypos, self.module_width * abs(mod), color
250-
)
251-
xpos += self.module_width * abs(mod)
236+
if len(code) != 1:
237+
raise NotImplementedError("Only one line of code is supported")
238+
line = code[0]
239+
# Left quiet zone is x startposition
240+
xpos = self.quiet_zone
241+
bxs = xpos # x start of barcode
242+
text: InternalText = {
243+
"start": [], # The x start of a guard
244+
"end": [], # The x end of a guard
245+
"xpos": [], # The x position where to write a text block
246+
# Flag that indicates if the previous mod was part of an guard block:
247+
"was_guard": False,
248+
}
249+
for mod, height_factor in self.packed(line):
250+
if mod < 1:
251+
color = self.background
252252
else:
253-
if height_factor != 1:
253+
color = self.foreground
254+
255+
if text["was_guard"] and height_factor == 1:
256+
# The current guard ended, store its x position
254257
text["end"].append(xpos)
255-
self.module_height = base_height
256-
257-
bxe = xpos
258-
# Add right quiet zone to every line, except last line,
259-
# quiet zone already provided with background,
260-
# should it be removed completely?
261-
if (cc + 1) != len(code):
262-
self._callbacks["paint_module"](
263-
xpos, ypos, self.quiet_zone, self.background
264-
)
265-
ypos += self.module_height
258+
text["was_guard"] = False
259+
elif not text["was_guard"] and height_factor != 1:
260+
# A guard started, store its x position
261+
text["start"].append(xpos)
262+
text["was_guard"] = True
263+
264+
self.module_height = base_height * height_factor
265+
# remove painting for background colored tiles?
266+
self._callbacks["paint_module"](
267+
xpos, ypos, self.module_width * abs(mod), color
268+
)
269+
xpos += self.module_width * abs(mod)
270+
else:
271+
if height_factor != 1:
272+
text["end"].append(xpos)
273+
self.module_height = base_height
274+
275+
bxe = xpos
276+
ypos += self.module_height
266277

267278
if self.text and self._callbacks["paint_text"] is not None:
268279
if not text["start"]:

0 commit comments

Comments
 (0)