1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-06-25 08:38:43 +00:00

fix return types; add user defined kanji function

This commit is contained in:
kymok 2025-04-20 07:46:39 +09:00
parent c7aad43e17
commit bd83c979ed
5 changed files with 35 additions and 15 deletions

View File

@ -45,9 +45,7 @@ p.kanji_text("こんにちは世界!\n")
p.ln() p.ln()
p.define_user_defined_kanji(b"\x77\x7e", checkerboard_kanji) p.define_user_defined_kanji(b"\x77\x7e", checkerboard_kanji)
p._enter_kanji_mode() p.write_user_defined_kanji(b"\x77\x7e")
p._raw(b"\x77\x7e")
p._exit_kanji_mode()
p.kanji_text("←外字\n") p.kanji_text("←外字\n")
p.delete_user_defined_kanji(b"\x77\x7e") p.delete_user_defined_kanji(b"\x77\x7e")
p.cut() p.cut()

View File

@ -75,6 +75,6 @@ all =
pywin32; platform_system=='Windows' pywin32; platform_system=='Windows'
[flake8] [flake8]
exclude = .git,.venv,.tox,.github,.eggs,__pycache__,doc/conf.py,build,dist,capabilities-data,test,src/escpos/constants.py exclude = .git,venv,.venv,.tox,.github,.eggs,__pycache__,doc/conf.py,build,dist,capabilities-data,test,src/escpos/constants.py
max-line-length = 120 max-line-length = 120
extend-ignore = E203, W503 extend-ignore = E203, W503

View File

@ -1507,11 +1507,11 @@ class Escpos(object, metaclass=ABCMeta):
self._raw(BUZZER + six.int2byte(times) + six.int2byte(duration)) self._raw(BUZZER + six.int2byte(times) + six.int2byte(duration))
def _enter_kanji_mode(self): def _enter_kanji_mode(self) -> None:
"""Enter Kanji mode.""" """Enter Kanji mode."""
self._raw(KANJI_ENTER_KANJI_MODE) self._raw(KANJI_ENTER_KANJI_MODE)
def _exit_kanji_mode(self): def _exit_kanji_mode(self) -> None:
"""Exit Kanji mode.""" """Exit Kanji mode."""
self._raw(KANJI_EXIT_KANJI_MODE) self._raw(KANJI_EXIT_KANJI_MODE)
@ -1574,7 +1574,7 @@ class Escpos(object, metaclass=ABCMeta):
double_width: bool = False, double_width: bool = False,
double_height: bool = False, double_height: bool = False,
underline: Literal[0, 1, 2] = 0, underline: Literal[0, 1, 2] = 0,
): ) -> None:
"""Set the Kanji print mode. """Set the Kanji print mode.
:param double_width: Doubles the width of the text. :param double_width: Doubles the width of the text.
@ -1592,7 +1592,7 @@ class Escpos(object, metaclass=ABCMeta):
def set_kanji_underline( def set_kanji_underline(
self, self,
underline: Literal[0, 1, 2] = 0, underline: Literal[0, 1, 2] = 0,
): ) -> None:
"""Set the Kanji underline mode. """Set the Kanji underline mode.
Some printers may only support 1 dot width underline. Some printers may only support 1 dot width underline.
@ -1609,7 +1609,7 @@ class Escpos(object, metaclass=ABCMeta):
self, self,
code: bytes, code: bytes,
data: bytes, data: bytes,
): ) -> None:
"""Set a user defined Kanji character. """Set a user defined Kanji character.
:param code: The Kanji code. :param code: The Kanji code.
@ -1620,13 +1620,25 @@ class Escpos(object, metaclass=ABCMeta):
def delete_user_defined_kanji( def delete_user_defined_kanji(
self, self,
code: bytes, code: bytes,
): ) -> None:
"""Delete a user defined Kanji character. """Delete a user defined Kanji character.
:param code: The Kanji code. :param code: The Kanji code.
""" """
self._raw(KANJI_DELETE_USER_DEFINED + code) self._raw(KANJI_DELETE_USER_DEFINED + code)
def write_user_defined_kanji(
self,
code: bytes,
) -> None:
"""Write a user defined Kanji character.
:param code: The Kanji code.
"""
self._enter_kanji_mode()
self._raw(code)
self._exit_kanji_mode()
def set_kanji_encoding( def set_kanji_encoding(
self, self,
encoding: Literal[ encoding: Literal[
@ -1638,13 +1650,15 @@ class Escpos(object, metaclass=ABCMeta):
"gb2312", # FIXME test with real device, "gb2312", # FIXME test with real device,
"gb18030", # FIXME test with real device, "gb18030", # FIXME test with real device,
], ],
): ) -> None:
"""Select the Kanji encoding. """Select the Kanji encoding.
This command is available only for Japanese model printers. This command is available only for Japanese model printers.
:param code: Encoding. :param code: Encoding.
:raises ValueError: If the encoding is invalid. :raises ValueError: If the encoding is invalid.
.. todo:: Test the encodings marked above with `FIXME` with a real device.
""" """
# Japanese model printer have several Kanji encoding modes. # Japanese model printer have several Kanji encoding modes.
if ( if (
@ -1669,7 +1683,7 @@ class Escpos(object, metaclass=ABCMeta):
self, self,
left_spacing: int, left_spacing: int,
right_spacing: int, right_spacing: int,
): ) -> None:
"""Set the Kanji spacing. """Set the Kanji spacing.
Spacing is either 0-255 or 0-32 according to the printer model. Spacing is either 0-255 or 0-32 according to the printer model.
@ -1684,7 +1698,7 @@ class Escpos(object, metaclass=ABCMeta):
def set_kanji_quadruple_size( def set_kanji_quadruple_size(
self, self,
enable: bool, enable: bool,
): ) -> None:
"""Set the Kanji quadruple size. """Set the Kanji quadruple size.
:param enable: Enable quadruple size. :param enable: Enable quadruple size.
@ -1694,7 +1708,7 @@ class Escpos(object, metaclass=ABCMeta):
def set_kanji_font( def set_kanji_font(
self, self,
font: Literal[0, 1, 2], font: Literal[0, 1, 2],
): ) -> None:
"""Set the Kanji font. """Set the Kanji font.
:param font: The Kanji font. :param font: The Kanji font.

View File

@ -88,6 +88,13 @@ def test_kanji_text_shift_jis() -> None:
) )
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")
def test_set_kanji_decoration() -> None: def test_set_kanji_decoration() -> None:
"""should set kanji decoration.""" """should set kanji decoration."""
instance = printer.Dummy() instance = printer.Dummy()
@ -96,7 +103,7 @@ def test_set_kanji_decoration() -> None:
instance = printer.Dummy() instance = printer.Dummy()
instance.set_kanji_decoration(double_width=True, double_height=True, underline=1) instance.set_kanji_decoration(double_width=True, double_height=True, underline=1)
assert instance.output == KANJI_PRINT_MODE + b"\x0C" + KANJI_UNDERLINE + b"\x01" assert instance.output == KANJI_PRINT_MODE + b"\x0c" + KANJI_UNDERLINE + b"\x01"
def test_define_user_defined_kanji() -> None: def test_define_user_defined_kanji() -> None:

View File

@ -30,6 +30,7 @@ setenv = PY_IGNORE_IMPORTMISMATCH=1
[testenv:docs] [testenv:docs]
basepython = python basepython = python
changedir = doc changedir = doc
passenv = PYENCHANT_LIBRARY_PATH
deps = sphinx>=7.2.3 deps = sphinx>=7.2.3
setuptools_scm setuptools_scm
python-barcode>=0.15.0,<1 python-barcode>=0.15.0,<1