Skip to content

Commit f59619f

Browse files
committed
Add scannability (image correctness) tests to ensure proper addon rendering. Fix addon rendering by adding quiet zone.
1 parent cc7d103 commit f59619f

File tree

9 files changed

+392
-6
lines changed

9 files changed

+392
-6
lines changed

.github/workflows/tests.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ jobs:
2727
cache: pip
2828
cache-dependency-path: |
2929
pyproject.toml
30+
- name: Install system dependencies (Linux)
31+
if: runner.os == 'Linux'
32+
run: sudo apt-get update && sudo apt-get install -y libzbar0 libcairo2-dev
33+
- name: Install system dependencies (macOS)
34+
if: runner.os == 'macOS'
35+
run: brew install zbar cairo
36+
- name: Install system dependencies (Windows)
37+
if: runner.os == 'Windows'
38+
run: choco install zbar
3039
- name: Install test dependency
3140
run: pip install tox
3241
- name: Run tests

barcode/charsets/addons.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
# Addon guard patterns
10+
ADDON_QUIET_ZONE = "000000000" # 9-module separator between main code and addon (GS1 spec)
1011
ADDON_START = "1011" # Start guard for addon
1112
ADDON_SEPARATOR = "01" # Separator between addon digits
1213

barcode/charsets/ean.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from barcode.charsets.addons import ADDON2_PARITY
44
from barcode.charsets.addons import ADDON5_PARITY
5+
from barcode.charsets.addons import ADDON_QUIET_ZONE
56
from barcode.charsets.addons import ADDON_SEPARATOR
67
from barcode.charsets.addons import ADDON_START
78

@@ -64,6 +65,7 @@
6465
__all__ = [
6566
"ADDON2_PARITY",
6667
"ADDON5_PARITY",
68+
"ADDON_QUIET_ZONE",
6769
"ADDON_SEPARATOR",
6870
"ADDON_START",
6971
"CODES",

barcode/charsets/upc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from barcode.charsets.addons import ADDON2_PARITY
44
from barcode.charsets.addons import ADDON5_PARITY
55
from barcode.charsets.addons import ADDON_CODES
6+
from barcode.charsets.addons import ADDON_QUIET_ZONE
67
from barcode.charsets.addons import ADDON_SEPARATOR
78
from barcode.charsets.addons import ADDON_START
89

@@ -40,6 +41,7 @@
4041
"ADDON2_PARITY",
4142
"ADDON5_PARITY",
4243
"ADDON_CODES",
44+
"ADDON_QUIET_ZONE",
4345
"ADDON_SEPARATOR",
4446
"ADDON_START",
4547
"CODES",

barcode/ean.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,20 @@ def build(self) -> list[str]:
146146
def _build_addon(self) -> str:
147147
"""Builds the addon barcode pattern (EAN-2 or EAN-5).
148148
149-
:returns: The addon pattern as string
149+
:returns: The addon pattern as string (including quiet zone separator)
150150
"""
151151
if not self.addon:
152152
return ""
153153

154+
# Add quiet zone (9 modules) before addon per GS1 specification
155+
code = _ean.ADDON_QUIET_ZONE
156+
154157
if len(self.addon) == 2:
155-
return self._build_addon2()
156-
return self._build_addon5()
158+
code += self._build_addon2()
159+
else:
160+
code += self._build_addon5()
161+
162+
return code
157163

158164
def _build_addon2(self) -> str:
159165
"""Builds EAN-2 addon pattern.

barcode/upc.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,20 @@ def build(self) -> list[str]:
128128
def _build_addon(self) -> str:
129129
"""Builds the addon barcode pattern (EAN-2 or EAN-5).
130130
131-
:returns: The addon pattern as string
131+
:returns: The addon pattern as string (including quiet zone separator)
132132
"""
133133
if not self.addon:
134134
return ""
135135

136+
# Add quiet zone (9 modules) before addon per GS1 specification
137+
code = _upc.ADDON_QUIET_ZONE
138+
136139
if len(self.addon) == 2:
137-
return self._build_addon2()
138-
return self._build_addon5()
140+
code += self._build_addon2()
141+
else:
142+
code += self._build_addon5()
143+
144+
return code
139145

140146
def _build_addon2(self) -> str:
141147
"""Builds EAN-2 addon pattern.

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ current
99
book prices.
1010
* Added support for EAN-2 and EAN-5 addons to UPC-A barcodes via the ``addon``
1111
parameter, following the same interface as EAN-13.
12+
* Addon rendering includes a 9-module quiet zone separator between the
13+
main barcode and the addon, as required by the GS1 specification. This ensures
14+
proper scanning of barcodes with addons.
15+
* Added scannability tests for EAN-2 and EAN-5 addons using the ``pyzbar`` library.
1216
* Fixed ISSN to accept full EAN-13 format (13 digits starting with 977) and
1317
preserve digits 11-12 (sequence variant) instead of always replacing them
1418
with "00".

0 commit comments

Comments
 (0)