Skip to content

Commit d4d7ac4

Browse files
authored
Added support for Codabar(NW-7) (#115)
1 parent a000836 commit d4d7ac4

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ MANIFEST
1717
barcode/version.py
1818
tests/index.html
1919
tests/test_outputs/
20+
tests/test.py
21+
test.py

barcode/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Dict
99
from typing import Union
1010

11+
from barcode.codabar import CODABAR
1112
from barcode.codex import PZN
1213
from barcode.codex import Code39
1314
from barcode.codex import Code128
@@ -47,6 +48,8 @@
4748
"code128": Code128,
4849
"itf": ITF,
4950
"gs1_128": Gs1_128,
51+
"codabar": CODABAR,
52+
"nw-7": CODABAR,
5053
}
5154

5255
PROVIDED_BARCODES = list(__BARCODE_MAP)

barcode/charsets/codabar.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# W = Wide bar
2+
# w = wide space
3+
# N = Narrow bar
4+
# n = narrow space
5+
6+
CODES = {
7+
"0": "NnNnNwW",
8+
"1": "NnNnWwN",
9+
"2": "NnNwNnW",
10+
"3": "WwNnNnN",
11+
"4": "NnWnNwN",
12+
"5": "WnNnNwN",
13+
"6": "NwNnNnW",
14+
"7": "NwNnWnN",
15+
"8": "NwWnNnN",
16+
"9": "WnNwNnN",
17+
"-": "NnNwWnN",
18+
"$": "NnWwNnN",
19+
":": "WnNnWnW",
20+
"/": "WnWnNnW",
21+
".": "WnWnWnN",
22+
"+": "NnWnWnW",
23+
}
24+
25+
STARTSTOP = {"A": "NnWwNwN", "B": "NwNwNnW", "C": "NnNwNwW", "D": "NnNwWwN"}

barcode/codabar.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Module: barcode.codabar
2+
3+
:Provided barcodes: Codabar (NW-7)
4+
"""
5+
__docformat__ = "restructuredtext en"
6+
7+
from barcode.base import Barcode
8+
from barcode.charsets import codabar
9+
from barcode.errors import BarcodeError
10+
from barcode.errors import IllegalCharacterError
11+
12+
13+
class CODABAR(Barcode):
14+
"""Initializes a new CODABAR instance.
15+
16+
:parameters:
17+
code : String
18+
Codabar (NW-7) string that matches [ABCD][0-9$:/.+-]+[ABCD]
19+
writer : barcode.writer Instance
20+
The writer to render the barcode (default: SVGWriter).
21+
narrow: Integer
22+
Width of the narrow elements (default: 2)
23+
wide: Integer
24+
Width of the wide elements (default: 5)
25+
wide/narrow must be in the range 2..3
26+
"""
27+
28+
name = "Codabar (NW-7)"
29+
30+
def __init__(self, code, writer=None, narrow=2, wide=5):
31+
self.code = code
32+
self.writer = writer or self.default_writer()
33+
self.narrow = narrow
34+
self.wide = wide
35+
36+
def __str__(self):
37+
return self.code
38+
39+
def get_fullcode(self):
40+
return self.code
41+
42+
def build(self):
43+
try:
44+
data = (
45+
codabar.STARTSTOP[self.code[0]] + "n"
46+
) # Start with [A-D], followed by a narrow space
47+
48+
except KeyError:
49+
raise BarcodeError("Codabar should start with either A,B,C or D")
50+
51+
try:
52+
data += "n".join(
53+
[codabar.CODES[c] for c in self.code[1:-1]]
54+
) # separated by a narrow space
55+
except KeyError:
56+
raise IllegalCharacterError("Codabar can only contain numerics or $:/.+-")
57+
58+
try:
59+
data += "n" + codabar.STARTSTOP[self.code[-1]] # End with [A-D]
60+
except KeyError:
61+
raise BarcodeError("Codabar should end with either A,B,C or D")
62+
63+
raw = ""
64+
for e in data:
65+
if e == "W":
66+
raw += "1" * self.wide
67+
if e == "w":
68+
raw += "0" * self.wide
69+
if e == "N":
70+
raw += "1" * self.narrow
71+
if e == "n":
72+
raw += "0" * self.narrow
73+
return [raw]

0 commit comments

Comments
 (0)