From a4345043f54a5bacb52dd57103690d98e7140cd2 Mon Sep 17 00:00:00 2001 From: Erik Wolf Date: Fri, 29 Jul 2022 18:04:53 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Add=20FNC4=20to=20encode=20high=20(160?= =?UTF-8?q?=E2=80=93255)=20characters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for latin1 (ISO-8859-1) characters in range 160-255. Therefore the FNC4 is used to encode high characters in charset A and B. --- barcode/charsets/code128.py | 5 ++++- barcode/codex.py | 26 +++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/barcode/charsets/code128.py b/barcode/charsets/code128.py index 4941aa2..8b4084b 100644 --- a/barcode/charsets/code128.py +++ b/barcode/charsets/code128.py @@ -103,7 +103,10 @@ ) ) -ALL = set(_common + _charset_a + _charset_b) +ISO_8859_1 = {bytes([i]).decode('latin1'): i - 128 for i in range(160, 256)} + +ALL = set(_common + _charset_a + _charset_b + tuple(ISO_8859_1.keys())) + A = {c: i for i, c in enumerate(_charset_a)} B = {c: i for i, c in enumerate(_charset_b)} C = {"TO_B": 100, "TO_A": 101, "\xf1": 102} diff --git a/barcode/codex.py b/barcode/codex.py index 74665be..a371891 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -173,7 +173,7 @@ def look_next(): codes = [] if self._charset == "C" and not char.isdigit(): - if char in code128.B: + if char in code128.B or char in code128.ISO_8859_1: codes = self._new_charset("B") elif char in code128.A: codes = self._new_charset("A") @@ -183,31 +183,39 @@ def look_next(): elif self._charset == "B": if look_next(): codes = self._new_charset("C") - elif char not in code128.B: + elif char not in code128.B and char not in code128.ISO_8859_1: if char in code128.A: codes = self._new_charset("A") elif self._charset == "A": if look_next(): codes = self._new_charset("C") - elif char not in code128.A: + elif char not in code128.A and char not in code128.ISO_8859_1: if char in code128.B: codes = self._new_charset("B") return codes def _convert(self, char): if self._charset == "A": - return code128.A[char] + if char in code128.ISO_8859_1: + yield code128.A['\xf4'] + yield code128.ISO_8859_1[char] + else: + yield code128.A[char] elif self._charset == "B": - return code128.B[char] + if char not in code128.B: + yield code128.B['\xf4'] + yield code128.ISO_8859_1[char] + else: + yield code128.B[char] elif self._charset == "C": if char in code128.C: - return code128.C[char] + yield code128.C[char] elif char.isdigit(): self._buffer += char if len(self._buffer) == 2: value = int(self._buffer) self._buffer = "" - return value + yield value def _try_to_optimize(self, encoded): if encoded[1] in code128.TO: @@ -226,11 +234,11 @@ def _build(self): encoded.extend(self._maybe_switch_charset(i)) code_num = self._convert(char) if code_num is not None: - encoded.append(code_num) + encoded.extend(code_num) # Finally look in the buffer if len(self._buffer) == 1: encoded.extend(self._new_charset("B")) - encoded.append(self._convert(self._buffer[0])) + encoded.extend(self._convert(self._buffer[0])) self._buffer = "" encoded = self._try_to_optimize(encoded) return encoded From 2628d270f7f1e39fd1c0f36fe24fa15d0bc55d16 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Jul 2022 16:08:12 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- barcode/charsets/code128.py | 2 +- barcode/codex.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/barcode/charsets/code128.py b/barcode/charsets/code128.py index 8b4084b..db24a64 100644 --- a/barcode/charsets/code128.py +++ b/barcode/charsets/code128.py @@ -103,7 +103,7 @@ ) ) -ISO_8859_1 = {bytes([i]).decode('latin1'): i - 128 for i in range(160, 256)} +ISO_8859_1 = {bytes([i]).decode("latin1"): i - 128 for i in range(160, 256)} ALL = set(_common + _charset_a + _charset_b + tuple(ISO_8859_1.keys())) diff --git a/barcode/codex.py b/barcode/codex.py index a371891..33c8fd0 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -197,13 +197,13 @@ def look_next(): def _convert(self, char): if self._charset == "A": if char in code128.ISO_8859_1: - yield code128.A['\xf4'] + yield code128.A["\xf4"] yield code128.ISO_8859_1[char] else: yield code128.A[char] elif self._charset == "B": if char not in code128.B: - yield code128.B['\xf4'] + yield code128.B["\xf4"] yield code128.ISO_8859_1[char] else: yield code128.B[char]