-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathaddon_utils.py
More file actions
81 lines (60 loc) · 2.28 KB
/
addon_utils.py
File metadata and controls
81 lines (60 loc) · 2.28 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
80
81
"""Utility functions for building EAN-2 and EAN-5 addon barcodes.
This module provides shared functionality for addon barcode generation
used by EAN and UPC barcode classes.
"""
from __future__ import annotations
from barcode.charsets.addons import ADDON2_PARITY
from barcode.charsets.addons import ADDON5_PARITY
from barcode.charsets.addons import ADDON_CODES
from barcode.charsets.addons import ADDON_QUIET_ZONE
from barcode.charsets.addons import ADDON_SEPARATOR
from barcode.charsets.addons import ADDON_START
def build_addon(addon: str) -> str:
"""Build the complete addon barcode pattern (EAN-2 or EAN-5).
:param addon: The addon digits (2 or 5 digits)
:returns: The addon pattern as string (including quiet zone separator)
"""
if not addon:
return ""
# Add quiet zone (9 modules) before addon per GS1 specification
code = ADDON_QUIET_ZONE
if len(addon) == 2:
code += build_addon2(addon)
else:
code += build_addon5(addon)
return code
def build_addon2(addon: str) -> str:
"""Build EAN-2 addon pattern.
Parity is determined by the 2-digit value mod 4.
:param addon: The 2-digit addon string
:returns: The EAN-2 addon pattern (using 'A' for addon bars)
"""
value = int(addon)
parity = ADDON2_PARITY[value % 4]
code = ADDON_START
for i, digit in enumerate(addon):
if i > 0:
code += ADDON_SEPARATOR
code += ADDON_CODES[parity[i]][int(digit)]
# Replace '1' with 'A' to mark addon bars for special rendering
return code.replace("1", "A")
def build_addon5(addon: str) -> str:
"""Build EAN-5 addon pattern.
Parity is determined by a checksum calculation.
:param addon: The 5-digit addon string
:returns: The EAN-5 addon pattern (using 'A' for addon bars)
"""
# Calculate checksum for parity pattern
checksum = 0
for i, digit in enumerate(addon):
weight = 3 if i % 2 == 0 else 9
checksum += int(digit) * weight
checksum %= 10
parity = ADDON5_PARITY[checksum]
code = ADDON_START
for i, digit in enumerate(addon):
if i > 0:
code += ADDON_SEPARATOR
code += ADDON_CODES[parity[i]][int(digit)]
# Replace '1' with 'A' to mark addon bars for special rendering
return code.replace("1", "A")