Title: fix: reset MagicEncode after font switch (ESC M) and hw init (ESC @) by larsblumberg · Pull Request #729 · python-escpos/python-escpos · GitHub
Open Graph Title: fix: reset MagicEncode after font switch (ESC M) and hw init (ESC @) by larsblumberg · Pull Request #729 · python-escpos/python-escpos
X Title: fix: reset MagicEncode after font switch (ESC M) and hw init (ESC @) by larsblumberg · Pull Request #729 · python-escpos/python-escpos
Description: Bug description Some printers (confirmed: Netum NT-5890K) silently reset their active code page back to the factory default after a font switch (ESC M n). MagicEncode was unaware of this hardware-side reset: its cached self.encoding remained stale, causing subsequent text() calls to skip CODEPAGE_CHANGE. Non-ASCII characters were then sent in the previously-active encoding (e.g. CP1257) but interpreted by the printer under its default code page, producing garbled output. ESC @ (hw("INIT")) is defined in the ESC/POS spec as a full printer reset that restores all settings to factory defaults, including the active code page — so the same fix applies there by spec. Proposed fix Fix: add MagicEncode.reset_encoding() which clears both self.encoding and self.encoder.used_encodings, and call it from set() after every font change and from hw() after INIT. This forces a fresh code page selection and a CODEPAGE_CHANGE re-emission before the next text output. Why used_encodings must also be cleared: self.encoding = None is enough to ensure a CODEPAGE_CHANGE is emitted. However, used_encodings biases find_suitable_encoding() toward previously- used code pages. After a reset, that preference is stale: on NT-5890K the previously-used CP1257 does not function correctly after ESC M, so MagicEncode would re-select it, emit CODEPAGE_CHANGE → CP1257, and send e.g. ü as 0xFC — wrong in the printer's default code page. Clearing used_encodings removes the stale bias and lets slot-number ordering take over, landing on CP850 where ü = 0x81, a byte that is correct in virtually every Western code page regardless of whether the printer honours the code page switch. Demo of the the bug and the applied fix: Future work Follow-up: used_encodings could be removed from MagicEncode entirely. self.encoding is the mechanism that actually avoids redundant switches: it keeps the current code page as long as it can encode the next character, without consulting used_encodings at all. used_encodings is only consulted when a switch is already unavoidable — at which point it cannot prevent any switch, it can only gamble on which encoding might be needed again later. That saves at most one future switch in the rare case where the same non-default code page is needed again after having been forced away. Removing used_encodings would also make reset_encoding() unnecessary: with only self.encoding to clear, callers would just write self.magic.encoding = None directly, with no need for a helper method.
Open Graph Description: Bug description Some printers (confirmed: Netum NT-5890K) silently reset their active code page back to the factory default after a font switch (ESC M n). MagicEncode was unaware of this hardware-s...
X Description: Bug description Some printers (confirmed: Netum NT-5890K) silently reset their active code page back to the factory default after a font switch (ESC M n). MagicEncode was unaware of this hardware-s...
Opengraph URL: https://github.com/python-escpos/python-escpos/pull/729
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:9fb533e3-34b2-dd45-b5e9-f9f8f8e7bbda |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9D62:1138FF:9F76F9:D90431:6A4D4FA8 |
| html-safe-nonce | 4c9816587337e9d36de09a8c7bc42364932c75db061eb5e1027a7972d58ab617 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDYyOjExMzhGRjo5Rjc2Rjk6RDkwNDMxOjZBNEQ0RkE4IiwidmlzaXRvcl9pZCI6IjcyNjE3MDUzNTc1MTYxNjUwMzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d1121092f5f512885d3428f7258ca64f21dda285be14c9e612e3024e069fdda3 |
| hovercard-subject-tag | pull_request:3428498864 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python-escpos/python-escpos/pull/729/files |
| twitter:image | https://avatars.githubusercontent.com/u/5980316?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/5980316?s=400&v=4 |
| og:image:alt | Bug description Some printers (confirmed: Netum NT-5890K) silently reset their active code page back to the factory default after a font switch (ESC M n). MagicEncode was unaware of this hardware-s... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 92571a8944142227b7e19cd10918b1ddd06e5066c1ad5bc7e4769cf6140a87e6 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python-escpos/python-escpos git https://github.com/python-escpos/python-escpos.git |
| octolytics-dimension-user_id | 16302939 |
| octolytics-dimension-user_login | python-escpos |
| octolytics-dimension-repository_id | 32363483 |
| octolytics-dimension-repository_nwo | python-escpos/python-escpos |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 32363483 |
| octolytics-dimension-repository_network_root_nwo | python-escpos/python-escpos |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 56fc8347865a14e2ec811533d68f929cf4e0ec19 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width