diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 958d71b..a9ec19d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,11 +12,15 @@ changes - has - changed +- fix the encoding search so that lower encodings are found first + contributors ^^^^^^^^^^^^ - list - contributors +- Scott Rotondo in `#570 `_ + 2023-05-11 - Version 3.0a9 - "Pride Comes Before A Fall" -------------------------------------------------------- This release is the 10th alpha release of the new version 3.0. diff --git a/doc/spelling_wordlist.txt b/doc/spelling_wordlist.txt index b00b484..f674ca7 100644 --- a/doc/spelling_wordlist.txt +++ b/doc/spelling_wordlist.txt @@ -67,6 +67,7 @@ Heuel Qian Lehtonen Kanzler +Rotondo barcode barcodes diff --git a/src/escpos/magicencode.py b/src/escpos/magicencode.py index c9847e1..7cb17be 100644 --- a/src/escpos/magicencode.py +++ b/src/escpos/magicencode.py @@ -155,7 +155,8 @@ class Encoder(object): def __encoding_sort_func(self, item): key, index = item - return (key in self.used_encodings, index) + used = key in self.used_encodings + return (not used, index) def find_suitable_encoding(self, char): """Search in a specific order for a suitable encoding. diff --git a/test/test_magicencode.py b/test/test_magicencode.py index e7ad258..da45718 100644 --- a/test/test_magicencode.py +++ b/test/test_magicencode.py @@ -32,6 +32,13 @@ class TestEncoder: assert not Encoder({"CP437": 1}).find_suitable_encoding("€") assert Encoder({"CP858": 1}).find_suitable_encoding("€") == "CP858" + def test_find_suitable_encoding_unnecessary_codepage_swap(self): + enc = Encoder({"CP857": 1, "CP437": 2, "CP1252": 3, "CP852": 4, "CP858": 5}) + # desired behavior would be that the encoder always stays in the lower + # available codepages if possible + for character in ("Á", "É", "Í", "Ó", "Ú"): + assert enc.find_suitable_encoding(character) == "CP857" + def test_get_encoding(self): with pytest.raises(ValueError): Encoder({}).get_encoding_name("latin1")