-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathitf.py
More file actions
79 lines (65 loc) · 2.22 KB
/
itf.py
File metadata and controls
79 lines (65 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Module: barcode.itf
:Provided barcodes: Interleaved 2 of 5
"""
from __future__ import annotations
__docformat__ = "restructuredtext en"
from barcode.base import Barcode
from barcode.charsets import itf
from barcode.errors import IllegalCharacterError
MIN_SIZE = 0.2
MIN_QUIET_ZONE = 6.4
class ITF(Barcode):
"""Initializes a new ITF instance.
:parameters:
code : String
ITF (Interleaved 2 of 5) numeric string
writer : barcode.writer Instance
The writer to render the barcode (default: SVGWriter).
narrow: Integer
Width of the narrow elements (default: 2)
wide: Integer
Width of the wide elements (default: 5)
wide/narrow must be in the range 2..3
"""
name = "ITF"
def __init__(self, code, writer=None, narrow=2, wide=5) -> None:
if not code.isdigit():
raise IllegalCharacterError("ITF code can only contain numbers.")
# Length must be even, prepend 0 if necessary
if len(code) % 2 != 0:
code = "0" + code
self.code = code
self.writer = writer or self.default_writer()
self.narrow = narrow
self.wide = wide
def __str__(self) -> str:
return self.code
def get_fullcode(self):
return self.code
def build(self) -> list[str]:
data = itf.START
for i in range(0, len(self.code), 2):
bars_digit = int(self.code[i])
spaces_digit = int(self.code[i + 1])
for j in range(5):
data += itf.CODES[bars_digit][j].upper()
data += itf.CODES[spaces_digit][j].lower()
data += itf.STOP
raw = ""
for e in data:
if e == "W":
raw += "1" * self.wide
if e == "w":
raw += "0" * self.wide
if e == "N":
raw += "1" * self.narrow
if e == "n":
raw += "0" * self.narrow
return [raw]
def render(self, writer_options, text=None):
options = {
"module_width": MIN_SIZE / self.narrow,
"quiet_zone": MIN_QUIET_ZONE,
}
options.update(writer_options or {})
return super().render(options, text)