|
4 | 4 | import os |
5 | 5 | import xml.dom.minidom |
6 | 6 | from typing import TYPE_CHECKING |
| 7 | +from typing import Any |
7 | 8 | from typing import BinaryIO |
8 | 9 | from typing import Callable |
9 | 10 | from typing import TypedDict |
@@ -96,6 +97,21 @@ class BaseWriter: |
96 | 97 | """ |
97 | 98 |
|
98 | 99 | _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 |
99 | 115 |
|
100 | 116 | def __init__( |
101 | 117 | self, |
@@ -204,65 +220,60 @@ def packed(self, line: str) -> Generator[tuple[int, float], str, None]: |
204 | 220 | yield (-c, self.guard_height_factor) |
205 | 221 | c = 1 |
206 | 222 |
|
207 | | - def render(self, code): |
| 223 | + def render(self, code: list[str]) -> Any: |
208 | 224 | """Renders the barcode to whatever the inheriting writer provides, |
209 | 225 | using the registered callbacks. |
210 | 226 |
|
211 | 227 | :parameters: |
212 | 228 | code : List |
213 | | - List of strings matching the writer spec |
| 229 | + List consisting of a single string matching the writer spec |
214 | 230 | (only contain 0 or 1 or G). |
215 | 231 | """ |
216 | 232 | if self._callbacks["initialize"] is not None: |
217 | 233 | self._callbacks["initialize"](code) |
218 | 234 | ypos = self.margin_top |
219 | 235 | 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 |
252 | 252 | 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 |
254 | 257 | 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 |
266 | 277 |
|
267 | 278 | if self.text and self._callbacks["paint_text"] is not None: |
268 | 279 | if not text["start"]: |
|
0 commit comments