Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two feature flags and charset selection on init #223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions barcode/codex.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,13 @@ class Code128(Barcode):

name = "Code 128"

def __init__(self, code, writer=None) -> None:
def __init__(self, code, writer=None, charset="B", enable_optimization=True, enable_hybrid_charset=True) -> None:
self.code = code
self.writer = writer or self.default_writer()
self._charset = "B"
self._charset = charset
self._buffer = ""
self._enable_optimization = enable_optimization
self._enable_hybrid_charset = enable_hybrid_charset
check_code(self.code, self.name, code128.ALL)

def __str__(self) -> str:
Expand Down Expand Up @@ -229,7 +231,8 @@ def _calculate_checksum(self, encoded):
def _build(self):
encoded = [code128.START_CODES[self._charset]]
for i, char in enumerate(self.code):
encoded.extend(self._maybe_switch_charset(i))
if self._enable_hybrid_charset == True:
encoded.extend(self._maybe_switch_charset(i))
code_num = self._convert(char)
if code_num is not None:
encoded.append(code_num)
Expand All @@ -238,7 +241,10 @@ def _build(self):
encoded.extend(self._new_charset("B"))
encoded.append(self._convert(self._buffer[0]))
self._buffer = ""
return self._try_to_optimize(encoded)
if self._enable_optimization == True:
return self._try_to_optimize(encoded)
else:
return encoded

def build(self):
encoded = self._build()
Expand Down