|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import gzip |
2 | 4 | import os |
3 | 5 | import xml.dom |
| 6 | +from typing import TYPE_CHECKING |
4 | 7 | from typing import BinaryIO |
| 8 | +from typing import Callable |
| 9 | +from typing import TypedDict |
5 | 10 |
|
6 | 11 | from barcode.version import version |
7 | 12 |
|
| 13 | +if TYPE_CHECKING: |
| 14 | + |
| 15 | + class InternalText(TypedDict): |
| 16 | + start: list |
| 17 | + end: list |
| 18 | + xpos: list |
| 19 | + was_guard: bool |
| 20 | + |
| 21 | + class Callbacks(TypedDict): |
| 22 | + initialize: Callable | None |
| 23 | + paint_module: Callable |
| 24 | + paint_text: Callable | None |
| 25 | + finish: Callable |
| 26 | + |
| 27 | + |
8 | 28 | try: |
9 | 29 | import Image |
10 | 30 | import ImageDraw |
@@ -61,30 +81,27 @@ class BaseWriter: |
61 | 81 | attributes and can set them directly or using |
62 | 82 | `self.set_options(option=value)`. |
63 | 83 |
|
64 | | - :parameters: |
65 | | - initialize : Function |
66 | | - Callback for initializing the inheriting writer. |
67 | | - Is called: `callback_initialize(raw_code)` |
68 | | - paint_module : Function |
69 | | - Callback for painting one barcode module. |
70 | | - Is called: `callback_paint_module(xpos, ypos, width, color)` |
71 | | - paint_text : Function |
72 | | - Callback for painting the text under the barcode. |
73 | | - Is called: `callback_paint_text(xpos, ypos)` using `self.text` |
74 | | - as text. |
75 | | - finish : Function |
76 | | - Callback for doing something with the completely rendered |
77 | | - output. |
78 | | - Is called: `return callback_finish()` and must return the |
79 | | - rendered output. |
| 84 | + :param initialize: Callback for initializing the inheriting writer. |
| 85 | + Is called: `callback_initialize(raw_code)` |
| 86 | + :param paint_module: |
| 87 | + Callback for painting one barcode module. |
| 88 | + Is called: `callback_paint_module(xpos, ypos, width, color)` |
| 89 | + :param paint_text: Callback for painting the text under the barcode. |
| 90 | + Is called: `callback_paint_text(xpos, ypos)` using `self.text` |
| 91 | + as text. |
| 92 | + :param finish: Callback for doing something with the completely rendered |
| 93 | + output. Is called: `return callback_finish()` and must return the |
| 94 | + rendered output. |
80 | 95 | """ |
81 | 96 |
|
| 97 | + _callbacks: Callbacks |
| 98 | + |
82 | 99 | def __init__( |
83 | 100 | self, |
84 | | - initialize=None, |
85 | | - paint_module=None, |
86 | | - paint_text=None, |
87 | | - finish=None, |
| 101 | + initialize: Callable | None, |
| 102 | + paint_module: Callable, |
| 103 | + paint_text: Callable | None, |
| 104 | + finish: Callable, |
88 | 105 | ) -> None: |
89 | 106 | self._callbacks = { |
90 | 107 | "initialize": initialize, |
@@ -218,7 +235,7 @@ def render(self, code): |
218 | 235 | # Left quiet zone is x startposition |
219 | 236 | xpos = self.quiet_zone |
220 | 237 | bxs = xpos # x start of barcode |
221 | | - text = { |
| 238 | + text: InternalText = { |
222 | 239 | "start": [], # The x start of a guard |
223 | 240 | "end": [], # The x end of a guard |
224 | 241 | "xpos": [], # The x position where to write a text block |
@@ -281,12 +298,10 @@ def render(self, code): |
281 | 298 | # The last text block is always put after the last guard end |
282 | 299 | text["xpos"].append(text["end"][-1] + 4 * self.module_width) |
283 | 300 |
|
284 | | - # Split the ean into its blocks |
285 | | - self.text = self.text.split(" ") |
286 | | - |
287 | 301 | ypos += pt2mm(self.font_size) |
288 | 302 |
|
289 | | - blocks = self.text |
| 303 | + # Split the ean into its blocks |
| 304 | + blocks = self.text.split(" ") |
290 | 305 | for text_, xpos in zip(blocks, text["xpos"]): |
291 | 306 | self.text = text_ |
292 | 307 | self._callbacks["paint_text"](xpos, ypos) |
|
0 commit comments