Skip to content

Conversation

@kymok
Copy link

@kymok kymok commented Mar 6, 2025

Description

This PR adds Kanji-related feature.

Tested with

  • EPSON TM-m10 (Japanese model)
    • Supports only ISO-2022-JP and Shift-JIS encoding

IMG_3368

Note

  • Neither Korean, Traditional Chinese, Simplified Chinese nor Japanese (Shift-JIS 2004) are tested since I don't have any printer that accepts those encodings.

@kymok kymok marked this pull request as draft March 7, 2025 04:30
@kymok
Copy link
Author

kymok commented Mar 7, 2025

Marking as draft until being able to build docs locally

@kymok kymok marked this pull request as ready for review March 7, 2025 07:07
@codecov
Copy link

codecov bot commented Mar 7, 2025

Codecov Report

❌ Patch coverage is 90.24390% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.71%. Comparing base (b85d5b9) to head (9d77f63).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
src/escpos/escpos.py 88.57% 6 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #680      +/-   ##
==========================================
+ Coverage   81.29%   81.71%   +0.41%     
==========================================
  Files          21       21              
  Lines        1679     1761      +82     
  Branches      262      275      +13     
==========================================
+ Hits         1365     1439      +74     
- Misses        234      240       +6     
- Partials       80       82       +2     
Flag Coverage Δ
unittests 81.54% <90.24%> (+0.42%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/escpos/constants.py 100.00% <100.00%> (ø)
src/escpos/escpos.py 76.30% <88.57%> (+1.76%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@patkan patkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for this change request. This looks good to me and I will merge it, I just have some small remarks. Please go through them and accept or comment depending on if you think it fits.

Comment on lines 48 to 50
p._enter_kanji_mode()
p._raw(b"\x77\x7e")
p._exit_kanji_mode()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand this feature correctly, this sequence is used to write the user defined kanji? In this case I would propose an API function such as write_user_defined_kanji(string) in order to write the kanji and prevent forcing users to use the "internal" functions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That seems to be a better way to go. I'll implement them but the input will be hex value.

In some encoding system, some code points are left undefined, and in this example we're writing a new glyph to one of those code point. The undefined zone differs by both encoding and maybe printers, so basically user need to input hex value of code point manually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, a hex value is better for the code point (and aligns with the rest of the code that you wrote).

self._raw(encoded_text)
self._exit_kanji_mode()

def _iso2022jp_text(self, text: str) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now this could be kept in escpos.py, but if this functionality grows more, we should think about a separate module (like magicencode)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _iso2022jp_text(self, text: str) -> None:
def _iso2022jp_text(self, text: str) -> None:

Please annotate a return type None for completeness.

Comment on lines +1645 to +1646
:param code: Encoding.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:param code: Encoding.
.. todo:: Test the encodings marked above with `FIXME` with a real device.

Please add a short todo for the FIXME above so that it shows up in this list: https://python-escpos--680.org.readthedocs.build/en/680/dev/todo.html

self._raw(BUZZER + six.int2byte(times) + six.int2byte(duration))

def _enter_kanji_mode(self):
"""Enter Kanji mode."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Enter Kanji mode."""
def _enter_kanji_mode(self) -> None:

Please annotate a return type None for completeness.

self._raw(KANJI_ENTER_KANJI_MODE)

def _exit_kanji_mode(self):
"""Exit Kanji mode."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Exit Kanji mode."""
def _exit_kanji_mode(self) -> None:

Please annotate a return type None for completeness.

"gb18030", # FIXME test with real device,
],
):
"""Select the Kanji encoding.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please annotate a return type None for completeness.

left_spacing: int,
right_spacing: int,
):
"""Set the Kanji spacing.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Set the Kanji spacing.
) -> None:

Please annotate a return type None for completeness.

self,
enable: bool,
):
"""Set the Kanji quadruple size.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Set the Kanji quadruple size.
) -> None:

Please annotate a return type None for completeness.

self,
font: Literal[0, 1, 2],
):
"""Set the Kanji font.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Set the Kanji font.
) -> None:

Please annotate a return type None for completeness.

Comment on lines +57 to +58


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_kanji_text_without_encoding() -> None:
"""Test behavior when no encoding is set."""
instance = printer.Dummy()
with pytest.raises(ValueError):
instance.kanji_text("Hello世界Hello")

Please test also the negative case when no encoding is set.

@patkan
Copy link
Member

patkan commented Mar 16, 2025

I am sorry, somehow my suggestions moved to the wrong lines 😢

@kymok kymok requested a review from patkan May 4, 2025 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants