Support changing the line spacing.

This commit is contained in:
Michael Elsdörfer 2016-08-25 15:56:32 +02:00
parent 798893caee
commit 854b75be30
3 changed files with 50 additions and 0 deletions

View File

@ -101,6 +101,13 @@ TXT_ALIGN_RT = ESC + b'\x61\x02' # Right justification
TXT_INVERT_ON = GS + b'\x42\x01' # Inverse Printing ON TXT_INVERT_ON = GS + b'\x42\x01' # Inverse Printing ON
TXT_INVERT_OFF = GS + b'\x42\x00' # Inverse Printing OFF TXT_INVERT_OFF = GS + b'\x42\x00' # Inverse Printing OFF
# Spacing
LINESPACING_FUNCS = {
60: ESC + b'A', # line_spacing/60 of an inch, 0 <= line_spacing <= 85
360: ESC + b'+', # line_spacing/360 of an inch, 0 <= line_spacing <= 255
180: ESC + b'3', # line_spacing/180 of an inch, 0 <= line_spacing <= 255
}
# Char code table # Char code table
CHARCODE_PC437 = ESC + b'\x74\x00' # USA: Standard Europe CHARCODE_PC437 = ESC + b'\x74\x00' # USA: Standard Europe
CHARCODE_JIS = ESC + b'\x74\x01' # Japanese Katakana CHARCODE_JIS = ESC + b'\x74\x01' # Japanese Katakana

View File

@ -565,6 +565,29 @@ class Escpos(object):
else: else:
self._raw(TXT_INVERT_OFF) self._raw(TXT_INVERT_OFF)
def line_spacing(self, spacing=30, divisor=180):
""" Set line character spacing.
There are different commands for setting the line spacing, using
a different denominator:
+ line_spacing/360 of an inch, 0 <= line_spacing <= 255
3 line_spacing/180 of an inch, 0 <= line_spacing <= 255
A line_spacing/60 of an inch, 0 <= line_spacing <= 85
Some printers may not support all of them. The most commonly
available command (using a divisor of 180) is chosen.
"""
if divisor not in LINESPACING_FUNCS:
raise ValueError("divisor must be either 360, 180 or 60")
if (divisor in [360, 180] \
and (not(0 <= spacing <= 255))):
raise ValueError("spacing must be a int between 0 and 255 when divisor is 360 or 180")
if divisor == 60 and (not(0 <= spacing <= 85)):
raise ValueError("spacing must be a int between 0 and 85 when divisor is 60")
self._raw(LINESPACING_FUNCS[divisor] + six.int2byte(spacing))
def cut(self, mode=''): def cut(self, mode=''):
""" Cut paper. """ Cut paper.

20
test/test_functions.py Normal file
View File

@ -0,0 +1,20 @@
from nose.tools import assert_raises
from escpos.printer import Dummy
def test_line_spacing_code_gen():
printer = Dummy()
printer.line_spacing(10)
assert printer.output == '\x1b3\n'
def test_line_spacing_error_handling():
printer = Dummy()
with assert_raises(ValueError):
printer.line_spacing(divisor=44)
with assert_raises(ValueError):
printer.line_spacing(divisor=80, spacing=86)
with assert_raises(ValueError):
printer.line_spacing(divisor=360, spacing=256)
with assert_raises(ValueError):
printer.line_spacing(divisor=180, spacing=256)